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(_dp, cmd) (VDO((_dp)->alt->svid, 1, cmd) | \
18 VDO_OPOS(USB_TYPEC_DP_MODE))
27 /* Pin assignments that use USB3.1 Gen2 signaling to carry DP protocol */
28 #define DP_PIN_ASSIGN_GEN2_BR_MASK (BIT(DP_PIN_ASSIGN_A) | \
31 /* Pin assignments that use DP v1.3 signaling to carry DP protocol */
32 #define DP_PIN_ASSIGN_DP_BR_MASK (BIT(DP_PIN_ASSIGN_C) | \
33 BIT(DP_PIN_ASSIGN_D) | \
34 BIT(DP_PIN_ASSIGN_E) | \
37 /* DP only pin assignments */
38 #define DP_PIN_ASSIGN_DP_ONLY_MASK (BIT(DP_PIN_ASSIGN_A) | \
39 BIT(DP_PIN_ASSIGN_C) | \
42 /* Pin assignments where one channel is for USB */
43 #define DP_PIN_ASSIGN_MULTI_FUNC_MASK (BIT(DP_PIN_ASSIGN_B) | \
44 BIT(DP_PIN_ASSIGN_D) | \
56 struct typec_displayport_data data;
60 struct mutex lock; /* device lock */
61 struct work_struct work;
62 struct typec_altmode *alt;
63 const struct typec_altmode *port;
66 static int dp_altmode_notify(struct dp_altmode *dp)
68 u8 state = get_count_order(DP_CONF_GET_PIN_ASSIGN(dp->data.conf));
70 return typec_altmode_notify(dp->alt, TYPEC_MODAL_STATE(state),
74 static int dp_altmode_configure(struct dp_altmode *dp, u8 con)
76 u32 conf = DP_CONF_SIGNALING_DP; /* Only DP signaling supported */
80 case DP_STATUS_CON_DISABLED:
82 case DP_STATUS_CON_DFP_D:
83 conf |= DP_CONF_UFP_U_AS_DFP_D;
84 pin_assign = DP_CAP_UFP_D_PIN_ASSIGN(dp->alt->vdo) &
85 DP_CAP_DFP_D_PIN_ASSIGN(dp->port->vdo);
87 case DP_STATUS_CON_UFP_D:
88 case DP_STATUS_CON_BOTH: /* NOTE: First acting as DP source */
89 conf |= DP_CONF_UFP_U_AS_UFP_D;
90 pin_assign = DP_CAP_DFP_D_PIN_ASSIGN(dp->alt->vdo) &
91 DP_CAP_UFP_D_PIN_ASSIGN(dp->port->vdo);
97 /* Determining the initial pin assignment. */
98 if (!DP_CONF_GET_PIN_ASSIGN(dp->data.conf)) {
99 /* Is USB together with DP preferred */
100 if (dp->data.status & DP_STATUS_PREFER_MULTI_FUNC &&
101 pin_assign & DP_PIN_ASSIGN_MULTI_FUNC_MASK)
102 pin_assign &= DP_PIN_ASSIGN_MULTI_FUNC_MASK;
103 else if (pin_assign & DP_PIN_ASSIGN_DP_ONLY_MASK)
104 pin_assign &= DP_PIN_ASSIGN_DP_ONLY_MASK;
109 conf |= DP_CONF_SET_PIN_ASSIGN(pin_assign);
112 dp->data.conf = conf;
117 static int dp_altmode_status_update(struct dp_altmode *dp)
119 bool configured = !!DP_CONF_GET_PIN_ASSIGN(dp->data.conf);
120 u8 con = DP_STATUS_CONNECTION(dp->data.status);
123 if (configured && (dp->data.status & DP_STATUS_SWITCH_TO_USB)) {
125 dp->state = DP_STATE_CONFIGURE;
126 } else if (dp->data.status & DP_STATUS_EXIT_DP_MODE) {
127 dp->state = DP_STATE_EXIT;
128 } else if (!(con & DP_CONF_CURRENTLY(dp->data.conf))) {
129 ret = dp_altmode_configure(dp, con);
131 dp->state = DP_STATE_CONFIGURE;
137 static int dp_altmode_configured(struct dp_altmode *dp)
141 sysfs_notify(&dp->alt->dev.kobj, "displayport", "configuration");
144 return typec_altmode_notify(dp->alt, TYPEC_STATE_USB,
147 ret = dp_altmode_notify(dp);
151 sysfs_notify(&dp->alt->dev.kobj, "displayport", "pin_assignment");
156 static int dp_altmode_configure_vdm(struct dp_altmode *dp, u32 conf)
158 u32 header = DP_HEADER(dp, DP_CMD_CONFIGURE);
161 ret = typec_altmode_notify(dp->alt, TYPEC_STATE_SAFE, &dp->data);
163 dev_err(&dp->alt->dev,
164 "unable to put to connector to safe mode\n");
168 ret = typec_altmode_vdm(dp->alt, header, &conf, 2);
170 if (DP_CONF_GET_PIN_ASSIGN(dp->data.conf))
171 dp_altmode_notify(dp);
173 typec_altmode_notify(dp->alt, TYPEC_STATE_USB,
180 static void dp_altmode_work(struct work_struct *work)
182 struct dp_altmode *dp = container_of(work, struct dp_altmode, work);
187 mutex_lock(&dp->lock);
191 ret = typec_altmode_enter(dp->alt);
193 dev_err(&dp->alt->dev, "failed to enter mode\n");
195 case DP_STATE_UPDATE:
196 header = DP_HEADER(dp, DP_CMD_STATUS_UPDATE);
198 ret = typec_altmode_vdm(dp->alt, header, &vdo, 2);
200 dev_err(&dp->alt->dev,
201 "unable to send Status Update command (%d)\n",
204 case DP_STATE_CONFIGURE:
205 ret = dp_altmode_configure_vdm(dp, dp->data.conf);
207 dev_err(&dp->alt->dev,
208 "unable to send Configure command (%d)\n", ret);
211 if (typec_altmode_exit(dp->alt))
212 dev_err(&dp->alt->dev, "Exit Mode Failed!\n");
218 dp->state = DP_STATE_IDLE;
220 mutex_unlock(&dp->lock);
223 static void dp_altmode_attention(struct typec_altmode *alt, const u32 vdo)
225 struct dp_altmode *dp = typec_altmode_get_drvdata(alt);
228 mutex_lock(&dp->lock);
230 old_state = dp->state;
231 dp->data.status = vdo;
233 if (old_state != DP_STATE_IDLE)
234 dev_warn(&alt->dev, "ATTENTION while processing state %d\n",
237 if (dp_altmode_status_update(dp))
238 dev_warn(&alt->dev, "%s: status update failed\n", __func__);
240 if (dp_altmode_notify(dp))
241 dev_err(&alt->dev, "%s: notification failed\n", __func__);
243 if (old_state == DP_STATE_IDLE && dp->state != DP_STATE_IDLE)
244 schedule_work(&dp->work);
246 mutex_unlock(&dp->lock);
249 static int dp_altmode_vdm(struct typec_altmode *alt,
250 const u32 hdr, const u32 *vdo, int count)
252 struct dp_altmode *dp = typec_altmode_get_drvdata(alt);
253 int cmd_type = PD_VDO_CMDT(hdr);
254 int cmd = PD_VDO_CMD(hdr);
257 mutex_lock(&dp->lock);
259 if (dp->state != DP_STATE_IDLE) {
268 dp->state = DP_STATE_UPDATE;
274 case DP_CMD_STATUS_UPDATE:
275 dp->data.status = *vdo;
276 ret = dp_altmode_status_update(dp);
278 case DP_CMD_CONFIGURE:
279 ret = dp_altmode_configured(dp);
287 case DP_CMD_CONFIGURE:
289 ret = dp_altmode_configured(dp);
299 if (dp->state != DP_STATE_IDLE)
300 schedule_work(&dp->work);
303 mutex_unlock(&dp->lock);
307 static int dp_altmode_activate(struct typec_altmode *alt, int activate)
309 return activate ? typec_altmode_enter(alt) : typec_altmode_exit(alt);
312 static const struct typec_altmode_ops dp_altmode_ops = {
313 .attention = dp_altmode_attention,
314 .vdm = dp_altmode_vdm,
315 .activate = dp_altmode_activate,
318 static const char * const configurations[] = {
319 [DP_CONF_USB] = "USB",
320 [DP_CONF_DFP_D] = "source",
321 [DP_CONF_UFP_D] = "sink",
325 configuration_store(struct device *dev, struct device_attribute *attr,
326 const char *buf, size_t size)
328 struct dp_altmode *dp = dev_get_drvdata(dev);
334 con = sysfs_match_string(configurations, buf);
338 mutex_lock(&dp->lock);
340 if (dp->state != DP_STATE_IDLE) {
345 cap = DP_CAP_CAPABILITY(dp->alt->vdo);
347 if ((con == DP_CONF_DFP_D && !(cap & DP_CAP_DFP_D)) ||
348 (con == DP_CONF_UFP_D && !(cap & DP_CAP_UFP_D))) {
353 conf = dp->data.conf & ~DP_CONF_DUAL_D;
356 if (dp->alt->active) {
357 ret = dp_altmode_configure_vdm(dp, conf);
362 dp->data.conf = conf;
365 mutex_unlock(&dp->lock);
367 return ret ? ret : size;
370 static ssize_t configuration_show(struct device *dev,
371 struct device_attribute *attr, char *buf)
373 struct dp_altmode *dp = dev_get_drvdata(dev);
379 mutex_lock(&dp->lock);
381 cap = DP_CAP_CAPABILITY(dp->alt->vdo);
382 cur = DP_CONF_CURRENTLY(dp->data.conf);
384 len = sprintf(buf, "%s ", cur ? "USB" : "[USB]");
386 for (i = 1; i < ARRAY_SIZE(configurations); i++) {
388 len += sprintf(buf + len, "[%s] ", configurations[i]);
389 else if ((i == DP_CONF_DFP_D && cap & DP_CAP_DFP_D) ||
390 (i == DP_CONF_UFP_D && cap & DP_CAP_UFP_D))
391 len += sprintf(buf + len, "%s ", configurations[i]);
394 mutex_unlock(&dp->lock);
399 static DEVICE_ATTR_RW(configuration);
401 static const char * const pin_assignments[] = {
402 [DP_PIN_ASSIGN_A] = "A",
403 [DP_PIN_ASSIGN_B] = "B",
404 [DP_PIN_ASSIGN_C] = "C",
405 [DP_PIN_ASSIGN_D] = "D",
406 [DP_PIN_ASSIGN_E] = "E",
407 [DP_PIN_ASSIGN_F] = "F",
411 pin_assignment_store(struct device *dev, struct device_attribute *attr,
412 const char *buf, size_t size)
414 struct dp_altmode *dp = dev_get_drvdata(dev);
419 ret = sysfs_match_string(pin_assignments, buf);
423 conf = DP_CONF_SET_PIN_ASSIGN(BIT(ret));
426 mutex_lock(&dp->lock);
428 if (conf & dp->data.conf)
431 if (dp->state != DP_STATE_IDLE) {
436 if (DP_CONF_CURRENTLY(dp->data.conf) == DP_CONF_DFP_D)
437 assignments = DP_CAP_UFP_D_PIN_ASSIGN(dp->alt->vdo);
439 assignments = DP_CAP_DFP_D_PIN_ASSIGN(dp->alt->vdo);
441 if (!(DP_CONF_GET_PIN_ASSIGN(conf) & assignments)) {
446 conf |= dp->data.conf & ~DP_CONF_PIN_ASSIGNEMENT_MASK;
448 /* Only send Configure command if a configuration has been set */
449 if (dp->alt->active && DP_CONF_CURRENTLY(dp->data.conf)) {
450 ret = dp_altmode_configure_vdm(dp, conf);
455 dp->data.conf = conf;
458 mutex_unlock(&dp->lock);
460 return ret ? ret : size;
463 static ssize_t pin_assignment_show(struct device *dev,
464 struct device_attribute *attr, char *buf)
466 struct dp_altmode *dp = dev_get_drvdata(dev);
472 mutex_lock(&dp->lock);
474 cur = get_count_order(DP_CONF_GET_PIN_ASSIGN(dp->data.conf));
476 if (DP_CONF_CURRENTLY(dp->data.conf) == DP_CONF_DFP_D)
477 assignments = DP_CAP_UFP_D_PIN_ASSIGN(dp->alt->vdo);
479 assignments = DP_CAP_DFP_D_PIN_ASSIGN(dp->alt->vdo);
481 for (i = 0; assignments; assignments >>= 1, i++) {
482 if (assignments & 1) {
484 len += sprintf(buf + len, "[%s] ",
487 len += sprintf(buf + len, "%s ",
492 mutex_unlock(&dp->lock);
497 static DEVICE_ATTR_RW(pin_assignment);
499 static struct attribute *dp_altmode_attrs[] = {
500 &dev_attr_configuration.attr,
501 &dev_attr_pin_assignment.attr,
505 static const struct attribute_group dp_altmode_group = {
506 .name = "displayport",
507 .attrs = dp_altmode_attrs,
510 int dp_altmode_probe(struct typec_altmode *alt)
512 const struct typec_altmode *port = typec_altmode_get_partner(alt);
513 struct dp_altmode *dp;
516 /* FIXME: Port can only be DFP_U. */
518 /* Make sure we have compatiple pin configurations */
519 if (!(DP_CAP_DFP_D_PIN_ASSIGN(port->vdo) &
520 DP_CAP_UFP_D_PIN_ASSIGN(alt->vdo)) &&
521 !(DP_CAP_UFP_D_PIN_ASSIGN(port->vdo) &
522 DP_CAP_DFP_D_PIN_ASSIGN(alt->vdo)))
525 ret = sysfs_create_group(&alt->dev.kobj, &dp_altmode_group);
529 dp = devm_kzalloc(&alt->dev, sizeof(*dp), GFP_KERNEL);
533 INIT_WORK(&dp->work, dp_altmode_work);
534 mutex_init(&dp->lock);
538 alt->desc = "DisplayPort";
539 alt->ops = &dp_altmode_ops;
541 typec_altmode_set_drvdata(alt, dp);
543 dp->state = DP_STATE_ENTER;
544 schedule_work(&dp->work);
548 EXPORT_SYMBOL_GPL(dp_altmode_probe);
550 void dp_altmode_remove(struct typec_altmode *alt)
552 struct dp_altmode *dp = typec_altmode_get_drvdata(alt);
554 sysfs_remove_group(&alt->dev.kobj, &dp_altmode_group);
555 cancel_work_sync(&dp->work);
557 EXPORT_SYMBOL_GPL(dp_altmode_remove);
559 static const struct typec_device_id dp_typec_id[] = {
560 { USB_TYPEC_DP_SID, USB_TYPEC_DP_MODE },
563 MODULE_DEVICE_TABLE(typec, dp_typec_id);
565 static struct typec_altmode_driver dp_altmode_driver = {
566 .id_table = dp_typec_id,
567 .probe = dp_altmode_probe,
568 .remove = dp_altmode_remove,
570 .name = "typec_displayport",
571 .owner = THIS_MODULE,
574 module_typec_altmode_driver(dp_altmode_driver);
576 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
577 MODULE_LICENSE("GPL v2");
578 MODULE_DESCRIPTION("DisplayPort Alternate Mode");