1 // SPDX-License-Identifier: GPL-2.0
3 * USB Typec-C DisplayPort Alternate Mode driver
5 * Copyright (C) 2018 Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
8 * DisplayPort is trademark of VESA (www.vesa.org)
11 #include <linux/delay.h>
12 #include <linux/mutex.h>
13 #include <linux/module.h>
14 #include <linux/usb/pd_vdo.h>
15 #include <linux/usb/typec_dp.h>
17 #define DP_HEADER(cmd) (VDO(USB_TYPEC_DP_SID, 1, cmd) | \
18 VDO_OPOS(USB_TYPEC_DP_MODE))
27 /* Helper for setting/getting the pin assignement value to the configuration */
28 #define DP_CONF_SET_PIN_ASSIGN(_a_) ((_a_) << 8)
29 #define DP_CONF_GET_PIN_ASSIGN(_conf_) (((_conf_) & GENMASK(15, 8)) >> 8)
31 /* Pin assignments that use USB3.1 Gen2 signaling to carry DP protocol */
32 #define DP_PIN_ASSIGN_GEN2_BR_MASK (BIT(DP_PIN_ASSIGN_A) | \
35 /* Pin assignments that use DP v1.3 signaling to carry DP protocol */
36 #define DP_PIN_ASSIGN_DP_BR_MASK (BIT(DP_PIN_ASSIGN_C) | \
37 BIT(DP_PIN_ASSIGN_D) | \
38 BIT(DP_PIN_ASSIGN_E) | \
41 /* DP only pin assignments */
42 #define DP_PIN_ASSIGN_DP_ONLY_MASK (BIT(DP_PIN_ASSIGN_A) | \
43 BIT(DP_PIN_ASSIGN_C) | \
46 /* Pin assignments where one channel is for USB */
47 #define DP_PIN_ASSIGN_MULTI_FUNC_MASK (BIT(DP_PIN_ASSIGN_B) | \
48 BIT(DP_PIN_ASSIGN_D) | \
60 struct typec_displayport_data data;
64 struct mutex lock; /* device lock */
65 struct work_struct work;
66 struct typec_altmode *alt;
67 const struct typec_altmode *port;
70 static int dp_altmode_notify(struct dp_altmode *dp)
72 u8 state = get_count_order(DP_CONF_GET_PIN_ASSIGN(dp->data.conf));
74 return typec_altmode_notify(dp->alt, TYPEC_MODAL_STATE(state),
78 static int dp_altmode_configure(struct dp_altmode *dp, u8 con)
80 u32 conf = DP_CONF_SIGNALING_DP; /* Only DP signaling supported */
84 case DP_STATUS_CON_DISABLED:
86 case DP_STATUS_CON_DFP_D:
87 conf |= DP_CONF_UFP_U_AS_DFP_D;
88 pin_assign = DP_CAP_UFP_D_PIN_ASSIGN(dp->alt->vdo) &
89 DP_CAP_DFP_D_PIN_ASSIGN(dp->port->vdo);
91 case DP_STATUS_CON_UFP_D:
92 case DP_STATUS_CON_BOTH: /* NOTE: First acting as DP source */
93 conf |= DP_CONF_UFP_U_AS_UFP_D;
94 pin_assign = DP_CAP_DFP_D_PIN_ASSIGN(dp->alt->vdo) &
95 DP_CAP_UFP_D_PIN_ASSIGN(dp->port->vdo);
101 /* Determining the initial pin assignment. */
102 if (!DP_CONF_GET_PIN_ASSIGN(dp->data.conf)) {
103 /* Is USB together with DP preferred */
104 if (dp->data.status & DP_STATUS_PREFER_MULTI_FUNC &&
105 pin_assign & DP_PIN_ASSIGN_MULTI_FUNC_MASK)
106 pin_assign &= DP_PIN_ASSIGN_MULTI_FUNC_MASK;
108 pin_assign &= DP_PIN_ASSIGN_DP_ONLY_MASK;
113 conf |= DP_CONF_SET_PIN_ASSIGN(pin_assign);
116 dp->data.conf = conf;
121 static int dp_altmode_status_update(struct dp_altmode *dp)
123 bool configured = !!DP_CONF_GET_PIN_ASSIGN(dp->data.conf);
124 u8 con = DP_STATUS_CONNECTION(dp->data.status);
127 if (configured && (dp->data.status & DP_STATUS_SWITCH_TO_USB)) {
129 dp->state = DP_STATE_CONFIGURE;
130 } else if (dp->data.status & DP_STATUS_EXIT_DP_MODE) {
131 dp->state = DP_STATE_EXIT;
132 } else if (!(con & DP_CONF_CURRENTLY(dp->data.conf))) {
133 ret = dp_altmode_configure(dp, con);
135 dp->state = DP_STATE_CONFIGURE;
141 static int dp_altmode_configured(struct dp_altmode *dp)
145 sysfs_notify(&dp->alt->dev.kobj, "displayport", "configuration");
148 return typec_altmode_notify(dp->alt, TYPEC_STATE_USB,
151 ret = dp_altmode_notify(dp);
155 sysfs_notify(&dp->alt->dev.kobj, "displayport", "pin_assignment");
160 static int dp_altmode_configure_vdm(struct dp_altmode *dp, u32 conf)
162 u32 header = DP_HEADER(DP_CMD_CONFIGURE);
165 ret = typec_altmode_notify(dp->alt, TYPEC_STATE_SAFE, &dp->data);
167 dev_err(&dp->alt->dev,
168 "unable to put to connector to safe mode\n");
172 ret = typec_altmode_vdm(dp->alt, header, &conf, 2);
174 if (DP_CONF_GET_PIN_ASSIGN(dp->data.conf))
175 dp_altmode_notify(dp);
177 typec_altmode_notify(dp->alt, TYPEC_STATE_USB,
184 static void dp_altmode_work(struct work_struct *work)
186 struct dp_altmode *dp = container_of(work, struct dp_altmode, work);
191 mutex_lock(&dp->lock);
195 ret = typec_altmode_enter(dp->alt);
197 dev_err(&dp->alt->dev, "failed to enter mode\n");
199 case DP_STATE_UPDATE:
200 header = DP_HEADER(DP_CMD_STATUS_UPDATE);
202 ret = typec_altmode_vdm(dp->alt, header, &vdo, 2);
204 dev_err(&dp->alt->dev,
205 "unable to send Status Update command (%d)\n",
208 case DP_STATE_CONFIGURE:
209 ret = dp_altmode_configure_vdm(dp, dp->data.conf);
211 dev_err(&dp->alt->dev,
212 "unable to send Configure command (%d)\n", ret);
215 if (typec_altmode_exit(dp->alt))
216 dev_err(&dp->alt->dev, "Exit Mode Failed!\n");
222 dp->state = DP_STATE_IDLE;
224 mutex_unlock(&dp->lock);
227 static void dp_altmode_attention(struct typec_altmode *alt, const u32 vdo)
229 struct dp_altmode *dp = typec_altmode_get_drvdata(alt);
232 mutex_lock(&dp->lock);
234 old_state = dp->state;
235 dp->data.status = vdo;
237 if (old_state != DP_STATE_IDLE)
238 dev_warn(&alt->dev, "ATTENTION while processing state %d\n",
241 if (dp_altmode_status_update(dp))
242 dev_warn(&alt->dev, "%s: status update failed\n", __func__);
244 if (dp_altmode_notify(dp))
245 dev_err(&alt->dev, "%s: notification failed\n", __func__);
247 if (old_state == DP_STATE_IDLE && dp->state != DP_STATE_IDLE)
248 schedule_work(&dp->work);
250 mutex_unlock(&dp->lock);
253 static int dp_altmode_vdm(struct typec_altmode *alt,
254 const u32 hdr, const u32 *vdo, int count)
256 struct dp_altmode *dp = typec_altmode_get_drvdata(alt);
257 int cmd_type = PD_VDO_CMDT(hdr);
258 int cmd = PD_VDO_CMD(hdr);
261 mutex_lock(&dp->lock);
263 if (dp->state != DP_STATE_IDLE) {
272 dp->state = DP_STATE_UPDATE;
278 case DP_CMD_STATUS_UPDATE:
279 dp->data.status = *vdo;
280 ret = dp_altmode_status_update(dp);
282 case DP_CMD_CONFIGURE:
283 ret = dp_altmode_configured(dp);
291 case DP_CMD_CONFIGURE:
293 ret = dp_altmode_configured(dp);
303 if (dp->state != DP_STATE_IDLE)
304 schedule_work(&dp->work);
307 mutex_unlock(&dp->lock);
311 static int dp_altmode_activate(struct typec_altmode *alt, int activate)
313 return activate ? typec_altmode_enter(alt) : typec_altmode_exit(alt);
316 static const struct typec_altmode_ops dp_altmode_ops = {
317 .attention = dp_altmode_attention,
318 .vdm = dp_altmode_vdm,
319 .activate = dp_altmode_activate,
322 static const char * const configurations[] = {
323 [DP_CONF_USB] = "USB",
324 [DP_CONF_DFP_D] = "source",
325 [DP_CONF_UFP_D] = "sink",
329 configuration_store(struct device *dev, struct device_attribute *attr,
330 const char *buf, size_t size)
332 struct dp_altmode *dp = dev_get_drvdata(dev);
338 con = sysfs_match_string(configurations, buf);
342 mutex_lock(&dp->lock);
344 if (dp->state != DP_STATE_IDLE) {
349 cap = DP_CAP_CAPABILITY(dp->alt->vdo);
351 if ((con == DP_CONF_DFP_D && !(cap & DP_CAP_DFP_D)) ||
352 (con == DP_CONF_UFP_D && !(cap & DP_CAP_UFP_D))) {
357 conf = dp->data.conf & ~DP_CONF_DUAL_D;
360 if (dp->alt->active) {
361 ret = dp_altmode_configure_vdm(dp, conf);
366 dp->data.conf = conf;
369 mutex_unlock(&dp->lock);
371 return ret ? ret : size;
374 static ssize_t configuration_show(struct device *dev,
375 struct device_attribute *attr, char *buf)
377 struct dp_altmode *dp = dev_get_drvdata(dev);
383 mutex_lock(&dp->lock);
385 cap = DP_CAP_CAPABILITY(dp->alt->vdo);
386 cur = DP_CONF_CURRENTLY(dp->data.conf);
388 len = sprintf(buf, "%s ", cur ? "USB" : "[USB]");
390 for (i = 1; i < ARRAY_SIZE(configurations); i++) {
392 len += sprintf(buf + len, "[%s] ", configurations[i]);
393 else if ((i == DP_CONF_DFP_D && cap & DP_CAP_DFP_D) ||
394 (i == DP_CONF_UFP_D && cap & DP_CAP_UFP_D))
395 len += sprintf(buf + len, "%s ", configurations[i]);
398 mutex_unlock(&dp->lock);
403 static DEVICE_ATTR_RW(configuration);
405 static const char * const pin_assignments[] = {
406 [DP_PIN_ASSIGN_A] = "A",
407 [DP_PIN_ASSIGN_B] = "B",
408 [DP_PIN_ASSIGN_C] = "C",
409 [DP_PIN_ASSIGN_D] = "D",
410 [DP_PIN_ASSIGN_E] = "E",
411 [DP_PIN_ASSIGN_F] = "F",
415 pin_assignment_store(struct device *dev, struct device_attribute *attr,
416 const char *buf, size_t size)
418 struct dp_altmode *dp = dev_get_drvdata(dev);
423 ret = sysfs_match_string(pin_assignments, buf);
427 conf = DP_CONF_SET_PIN_ASSIGN(BIT(ret));
430 mutex_lock(&dp->lock);
432 if (conf & dp->data.conf)
435 if (dp->state != DP_STATE_IDLE) {
440 if (DP_CONF_CURRENTLY(dp->data.conf) == DP_CONF_DFP_D)
441 assignments = DP_CAP_UFP_D_PIN_ASSIGN(dp->alt->vdo);
443 assignments = DP_CAP_DFP_D_PIN_ASSIGN(dp->alt->vdo);
445 if (!(DP_CONF_GET_PIN_ASSIGN(conf) & assignments)) {
450 conf |= dp->data.conf & ~DP_CONF_PIN_ASSIGNEMENT_MASK;
452 /* Only send Configure command if a configuration has been set */
453 if (dp->alt->active && DP_CONF_CURRENTLY(dp->data.conf)) {
454 ret = dp_altmode_configure_vdm(dp, conf);
459 dp->data.conf = conf;
462 mutex_unlock(&dp->lock);
464 return ret ? ret : size;
467 static ssize_t pin_assignment_show(struct device *dev,
468 struct device_attribute *attr, char *buf)
470 struct dp_altmode *dp = dev_get_drvdata(dev);
476 mutex_lock(&dp->lock);
478 cur = get_count_order(DP_CONF_GET_PIN_ASSIGN(dp->data.conf));
480 if (DP_CONF_CURRENTLY(dp->data.conf) == DP_CONF_DFP_D)
481 assignments = DP_CAP_UFP_D_PIN_ASSIGN(dp->alt->vdo);
483 assignments = DP_CAP_DFP_D_PIN_ASSIGN(dp->alt->vdo);
485 for (i = 0; assignments; assignments >>= 1, i++) {
486 if (assignments & 1) {
488 len += sprintf(buf + len, "[%s] ",
491 len += sprintf(buf + len, "%s ",
496 mutex_unlock(&dp->lock);
501 static DEVICE_ATTR_RW(pin_assignment);
503 static struct attribute *dp_altmode_attrs[] = {
504 &dev_attr_configuration.attr,
505 &dev_attr_pin_assignment.attr,
509 static const struct attribute_group dp_altmode_group = {
510 .name = "displayport",
511 .attrs = dp_altmode_attrs,
514 static int dp_altmode_probe(struct typec_altmode *alt)
516 const struct typec_altmode *port = typec_altmode_get_partner(alt);
517 struct dp_altmode *dp;
520 /* FIXME: Port can only be DFP_U. */
522 /* Make sure we have compatiple pin configurations */
523 if (!(DP_CAP_DFP_D_PIN_ASSIGN(port->vdo) &
524 DP_CAP_UFP_D_PIN_ASSIGN(alt->vdo)) &&
525 !(DP_CAP_UFP_D_PIN_ASSIGN(port->vdo) &
526 DP_CAP_DFP_D_PIN_ASSIGN(alt->vdo)))
529 ret = sysfs_create_group(&alt->dev.kobj, &dp_altmode_group);
533 dp = devm_kzalloc(&alt->dev, sizeof(*dp), GFP_KERNEL);
537 INIT_WORK(&dp->work, dp_altmode_work);
538 mutex_init(&dp->lock);
542 alt->desc = "DisplayPort";
543 alt->ops = &dp_altmode_ops;
545 typec_altmode_set_drvdata(alt, dp);
547 dp->state = DP_STATE_ENTER;
548 schedule_work(&dp->work);
553 static void dp_altmode_remove(struct typec_altmode *alt)
555 struct dp_altmode *dp = typec_altmode_get_drvdata(alt);
557 sysfs_remove_group(&alt->dev.kobj, &dp_altmode_group);
558 cancel_work_sync(&dp->work);
561 static const struct typec_device_id dp_typec_id[] = {
562 { USB_TYPEC_DP_SID, USB_TYPEC_DP_MODE },
565 MODULE_DEVICE_TABLE(typec, dp_typec_id);
567 static struct typec_altmode_driver dp_altmode_driver = {
568 .id_table = dp_typec_id,
569 .probe = dp_altmode_probe,
570 .remove = dp_altmode_remove,
572 .name = "typec_displayport",
573 .owner = THIS_MODULE,
576 module_typec_altmode_driver(dp_altmode_driver);
578 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
579 MODULE_LICENSE("GPL v2");
580 MODULE_DESCRIPTION("DisplayPort Alternate Mode");