1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * USB Typec-C DisplayPort Alternate Mode driver
4 *
5 * Copyright (C) 2018 Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7 *
8 * DisplayPort is trademark of VESA (www.vesa.org)
9 */
10
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>
16 #include "displayport.h"
17
18 #define DP_HEADER(_dp, cmd) (VDO((_dp)->alt->svid, 1, cmd) | \
19 VDO_OPOS(USB_TYPEC_DP_MODE))
20
21 enum {
22 DP_CONF_USB,
23 DP_CONF_DFP_D,
24 DP_CONF_UFP_D,
25 DP_CONF_DUAL_D,
26 };
27
28 /* Pin assignments that use USB3.1 Gen2 signaling to carry DP protocol */
29 #define DP_PIN_ASSIGN_GEN2_BR_MASK (BIT(DP_PIN_ASSIGN_A) | \
30 BIT(DP_PIN_ASSIGN_B))
31
32 /* Pin assignments that use DP v1.3 signaling to carry DP protocol */
33 #define DP_PIN_ASSIGN_DP_BR_MASK (BIT(DP_PIN_ASSIGN_C) | \
34 BIT(DP_PIN_ASSIGN_D) | \
35 BIT(DP_PIN_ASSIGN_E) | \
36 BIT(DP_PIN_ASSIGN_F))
37
38 /* DP only pin assignments */
39 #define DP_PIN_ASSIGN_DP_ONLY_MASK (BIT(DP_PIN_ASSIGN_A) | \
40 BIT(DP_PIN_ASSIGN_C) | \
41 BIT(DP_PIN_ASSIGN_E))
42
43 /* Pin assignments where one channel is for USB */
44 #define DP_PIN_ASSIGN_MULTI_FUNC_MASK (BIT(DP_PIN_ASSIGN_B) | \
45 BIT(DP_PIN_ASSIGN_D) | \
46 BIT(DP_PIN_ASSIGN_F))
47
48 enum dp_state {
49 DP_STATE_IDLE,
50 DP_STATE_ENTER,
51 DP_STATE_UPDATE,
52 DP_STATE_CONFIGURE,
53 DP_STATE_EXIT,
54 };
55
56 struct dp_altmode {
57 struct typec_displayport_data data;
58
59 enum dp_state state;
60
61 struct mutex lock; /* device lock */
62 struct work_struct work;
63 struct typec_altmode *alt;
64 const struct typec_altmode *port;
65 };
66
dp_altmode_notify(struct dp_altmode * dp)67 static int dp_altmode_notify(struct dp_altmode *dp)
68 {
69 u8 state = get_count_order(DP_CONF_GET_PIN_ASSIGN(dp->data.conf));
70
71 return typec_altmode_notify(dp->alt, TYPEC_MODAL_STATE(state),
72 &dp->data);
73 }
74
dp_altmode_configure(struct dp_altmode * dp,u8 con)75 static int dp_altmode_configure(struct dp_altmode *dp, u8 con)
76 {
77 u32 conf = DP_CONF_SIGNALING_DP; /* Only DP signaling supported */
78 u8 pin_assign = 0;
79
80 switch (con) {
81 case DP_STATUS_CON_DISABLED:
82 return 0;
83 case DP_STATUS_CON_DFP_D:
84 conf |= DP_CONF_UFP_U_AS_DFP_D;
85 pin_assign = DP_CAP_UFP_D_PIN_ASSIGN(dp->alt->vdo) &
86 DP_CAP_DFP_D_PIN_ASSIGN(dp->port->vdo);
87 break;
88 case DP_STATUS_CON_UFP_D:
89 case DP_STATUS_CON_BOTH: /* NOTE: First acting as DP source */
90 conf |= DP_CONF_UFP_U_AS_UFP_D;
91 pin_assign = DP_CAP_PIN_ASSIGN_UFP_D(dp->alt->vdo) &
92 DP_CAP_PIN_ASSIGN_DFP_D(dp->port->vdo);
93 break;
94 default:
95 break;
96 }
97
98 /* Determining the initial pin assignment. */
99 if (!DP_CONF_GET_PIN_ASSIGN(dp->data.conf)) {
100 /* Is USB together with DP preferred */
101 if (dp->data.status & DP_STATUS_PREFER_MULTI_FUNC &&
102 pin_assign & DP_PIN_ASSIGN_MULTI_FUNC_MASK)
103 pin_assign &= DP_PIN_ASSIGN_MULTI_FUNC_MASK;
104 else if (pin_assign & DP_PIN_ASSIGN_DP_ONLY_MASK)
105 pin_assign &= DP_PIN_ASSIGN_DP_ONLY_MASK;
106
107 if (!pin_assign)
108 return -EINVAL;
109
110 conf |= DP_CONF_SET_PIN_ASSIGN(pin_assign);
111 }
112
113 dp->data.conf = conf;
114
115 return 0;
116 }
117
dp_altmode_status_update(struct dp_altmode * dp)118 static int dp_altmode_status_update(struct dp_altmode *dp)
119 {
120 bool configured = !!DP_CONF_GET_PIN_ASSIGN(dp->data.conf);
121 u8 con = DP_STATUS_CONNECTION(dp->data.status);
122 int ret = 0;
123
124 if (configured && (dp->data.status & DP_STATUS_SWITCH_TO_USB)) {
125 dp->data.conf = 0;
126 dp->state = DP_STATE_CONFIGURE;
127 } else if (dp->data.status & DP_STATUS_EXIT_DP_MODE) {
128 dp->state = DP_STATE_EXIT;
129 } else if (!(con & DP_CONF_CURRENTLY(dp->data.conf))) {
130 ret = dp_altmode_configure(dp, con);
131 if (!ret)
132 dp->state = DP_STATE_CONFIGURE;
133 }
134
135 return ret;
136 }
137
dp_altmode_configured(struct dp_altmode * dp)138 static int dp_altmode_configured(struct dp_altmode *dp)
139 {
140 int ret;
141
142 sysfs_notify(&dp->alt->dev.kobj, "displayport", "configuration");
143
144 if (!dp->data.conf)
145 return typec_altmode_notify(dp->alt, TYPEC_STATE_USB,
146 &dp->data);
147
148 ret = dp_altmode_notify(dp);
149 if (ret)
150 return ret;
151
152 sysfs_notify(&dp->alt->dev.kobj, "displayport", "pin_assignment");
153
154 return 0;
155 }
156
dp_altmode_configure_vdm(struct dp_altmode * dp,u32 conf)157 static int dp_altmode_configure_vdm(struct dp_altmode *dp, u32 conf)
158 {
159 u32 header = DP_HEADER(dp, DP_CMD_CONFIGURE);
160 int ret;
161
162 ret = typec_altmode_notify(dp->alt, TYPEC_STATE_SAFE, &dp->data);
163 if (ret) {
164 dev_err(&dp->alt->dev,
165 "unable to put to connector to safe mode\n");
166 return ret;
167 }
168
169 ret = typec_altmode_vdm(dp->alt, header, &conf, 2);
170 if (ret) {
171 if (DP_CONF_GET_PIN_ASSIGN(dp->data.conf))
172 dp_altmode_notify(dp);
173 else
174 typec_altmode_notify(dp->alt, TYPEC_STATE_USB,
175 &dp->data);
176 }
177
178 return ret;
179 }
180
dp_altmode_work(struct work_struct * work)181 static void dp_altmode_work(struct work_struct *work)
182 {
183 struct dp_altmode *dp = container_of(work, struct dp_altmode, work);
184 u32 header;
185 u32 vdo;
186 int ret;
187
188 mutex_lock(&dp->lock);
189
190 switch (dp->state) {
191 case DP_STATE_ENTER:
192 ret = typec_altmode_enter(dp->alt, NULL);
193 if (ret && ret != -EBUSY)
194 dev_err(&dp->alt->dev, "failed to enter mode\n");
195 break;
196 case DP_STATE_UPDATE:
197 header = DP_HEADER(dp, DP_CMD_STATUS_UPDATE);
198 vdo = 1;
199 ret = typec_altmode_vdm(dp->alt, header, &vdo, 2);
200 if (ret)
201 dev_err(&dp->alt->dev,
202 "unable to send Status Update command (%d)\n",
203 ret);
204 break;
205 case DP_STATE_CONFIGURE:
206 ret = dp_altmode_configure_vdm(dp, dp->data.conf);
207 if (ret)
208 dev_err(&dp->alt->dev,
209 "unable to send Configure command (%d)\n", ret);
210 break;
211 case DP_STATE_EXIT:
212 if (typec_altmode_exit(dp->alt))
213 dev_err(&dp->alt->dev, "Exit Mode Failed!\n");
214 break;
215 default:
216 break;
217 }
218
219 dp->state = DP_STATE_IDLE;
220
221 mutex_unlock(&dp->lock);
222 }
223
dp_altmode_attention(struct typec_altmode * alt,const u32 vdo)224 static void dp_altmode_attention(struct typec_altmode *alt, const u32 vdo)
225 {
226 struct dp_altmode *dp = typec_altmode_get_drvdata(alt);
227 u8 old_state;
228
229 mutex_lock(&dp->lock);
230
231 old_state = dp->state;
232 dp->data.status = vdo;
233
234 if (old_state != DP_STATE_IDLE)
235 dev_warn(&alt->dev, "ATTENTION while processing state %d\n",
236 old_state);
237
238 if (dp_altmode_status_update(dp))
239 dev_warn(&alt->dev, "%s: status update failed\n", __func__);
240
241 if (dp_altmode_notify(dp))
242 dev_err(&alt->dev, "%s: notification failed\n", __func__);
243
244 if (old_state == DP_STATE_IDLE && dp->state != DP_STATE_IDLE)
245 schedule_work(&dp->work);
246
247 mutex_unlock(&dp->lock);
248 }
249
dp_altmode_vdm(struct typec_altmode * alt,const u32 hdr,const u32 * vdo,int count)250 static int dp_altmode_vdm(struct typec_altmode *alt,
251 const u32 hdr, const u32 *vdo, int count)
252 {
253 struct dp_altmode *dp = typec_altmode_get_drvdata(alt);
254 int cmd_type = PD_VDO_CMDT(hdr);
255 int cmd = PD_VDO_CMD(hdr);
256 int ret = 0;
257
258 mutex_lock(&dp->lock);
259
260 if (dp->state != DP_STATE_IDLE) {
261 ret = -EBUSY;
262 goto err_unlock;
263 }
264
265 switch (cmd_type) {
266 case CMDT_RSP_ACK:
267 switch (cmd) {
268 case CMD_ENTER_MODE:
269 dp->state = DP_STATE_UPDATE;
270 break;
271 case CMD_EXIT_MODE:
272 dp->data.status = 0;
273 dp->data.conf = 0;
274 break;
275 case DP_CMD_STATUS_UPDATE:
276 dp->data.status = *vdo;
277 ret = dp_altmode_status_update(dp);
278 break;
279 case DP_CMD_CONFIGURE:
280 ret = dp_altmode_configured(dp);
281 break;
282 default:
283 break;
284 }
285 break;
286 case CMDT_RSP_NAK:
287 switch (cmd) {
288 case DP_CMD_CONFIGURE:
289 dp->data.conf = 0;
290 ret = dp_altmode_configured(dp);
291 break;
292 default:
293 break;
294 }
295 break;
296 default:
297 break;
298 }
299
300 if (dp->state != DP_STATE_IDLE)
301 schedule_work(&dp->work);
302
303 err_unlock:
304 mutex_unlock(&dp->lock);
305 return ret;
306 }
307
dp_altmode_activate(struct typec_altmode * alt,int activate)308 static int dp_altmode_activate(struct typec_altmode *alt, int activate)
309 {
310 return activate ? typec_altmode_enter(alt, NULL) :
311 typec_altmode_exit(alt);
312 }
313
314 static const struct typec_altmode_ops dp_altmode_ops = {
315 .attention = dp_altmode_attention,
316 .vdm = dp_altmode_vdm,
317 .activate = dp_altmode_activate,
318 };
319
320 static const char * const configurations[] = {
321 [DP_CONF_USB] = "USB",
322 [DP_CONF_DFP_D] = "source",
323 [DP_CONF_UFP_D] = "sink",
324 };
325
326 static ssize_t
configuration_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)327 configuration_store(struct device *dev, struct device_attribute *attr,
328 const char *buf, size_t size)
329 {
330 struct dp_altmode *dp = dev_get_drvdata(dev);
331 u32 conf;
332 u32 cap;
333 int con;
334 int ret = 0;
335
336 con = sysfs_match_string(configurations, buf);
337 if (con < 0)
338 return con;
339
340 mutex_lock(&dp->lock);
341
342 if (dp->state != DP_STATE_IDLE) {
343 ret = -EBUSY;
344 goto err_unlock;
345 }
346
347 cap = DP_CAP_CAPABILITY(dp->alt->vdo);
348
349 if ((con == DP_CONF_DFP_D && !(cap & DP_CAP_DFP_D)) ||
350 (con == DP_CONF_UFP_D && !(cap & DP_CAP_UFP_D))) {
351 ret = -EINVAL;
352 goto err_unlock;
353 }
354
355 conf = dp->data.conf & ~DP_CONF_DUAL_D;
356 conf |= con;
357
358 if (dp->alt->active) {
359 ret = dp_altmode_configure_vdm(dp, conf);
360 if (ret)
361 goto err_unlock;
362 }
363
364 dp->data.conf = conf;
365
366 err_unlock:
367 mutex_unlock(&dp->lock);
368
369 return ret ? ret : size;
370 }
371
configuration_show(struct device * dev,struct device_attribute * attr,char * buf)372 static ssize_t configuration_show(struct device *dev,
373 struct device_attribute *attr, char *buf)
374 {
375 struct dp_altmode *dp = dev_get_drvdata(dev);
376 int len;
377 u8 cap;
378 u8 cur;
379 int i;
380
381 mutex_lock(&dp->lock);
382
383 cap = DP_CAP_CAPABILITY(dp->alt->vdo);
384 cur = DP_CONF_CURRENTLY(dp->data.conf);
385
386 len = sprintf(buf, "%s ", cur ? "USB" : "[USB]");
387
388 for (i = 1; i < ARRAY_SIZE(configurations); i++) {
389 if (i == cur)
390 len += sprintf(buf + len, "[%s] ", configurations[i]);
391 else if ((i == DP_CONF_DFP_D && cap & DP_CAP_DFP_D) ||
392 (i == DP_CONF_UFP_D && cap & DP_CAP_UFP_D))
393 len += sprintf(buf + len, "%s ", configurations[i]);
394 }
395
396 mutex_unlock(&dp->lock);
397
398 buf[len - 1] = '\n';
399 return len;
400 }
401 static DEVICE_ATTR_RW(configuration);
402
403 static const char * const pin_assignments[] = {
404 [DP_PIN_ASSIGN_A] = "A",
405 [DP_PIN_ASSIGN_B] = "B",
406 [DP_PIN_ASSIGN_C] = "C",
407 [DP_PIN_ASSIGN_D] = "D",
408 [DP_PIN_ASSIGN_E] = "E",
409 [DP_PIN_ASSIGN_F] = "F",
410 };
411
412 /*
413 * Helper function to extract a peripheral's currently supported
414 * Pin Assignments from its DisplayPort alternate mode state.
415 */
get_current_pin_assignments(struct dp_altmode * dp)416 static u8 get_current_pin_assignments(struct dp_altmode *dp)
417 {
418 if (DP_CONF_CURRENTLY(dp->data.conf) == DP_CONF_DFP_D)
419 return DP_CAP_PIN_ASSIGN_DFP_D(dp->alt->vdo);
420 else
421 return DP_CAP_PIN_ASSIGN_UFP_D(dp->alt->vdo);
422 }
423
424 static ssize_t
pin_assignment_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)425 pin_assignment_store(struct device *dev, struct device_attribute *attr,
426 const char *buf, size_t size)
427 {
428 struct dp_altmode *dp = dev_get_drvdata(dev);
429 u8 assignments;
430 u32 conf;
431 int ret;
432
433 ret = sysfs_match_string(pin_assignments, buf);
434 if (ret < 0)
435 return ret;
436
437 conf = DP_CONF_SET_PIN_ASSIGN(BIT(ret));
438 ret = 0;
439
440 mutex_lock(&dp->lock);
441
442 if (conf & dp->data.conf)
443 goto out_unlock;
444
445 if (dp->state != DP_STATE_IDLE) {
446 ret = -EBUSY;
447 goto out_unlock;
448 }
449
450 assignments = get_current_pin_assignments(dp);
451
452 if (!(DP_CONF_GET_PIN_ASSIGN(conf) & assignments)) {
453 ret = -EINVAL;
454 goto out_unlock;
455 }
456
457 conf |= dp->data.conf & ~DP_CONF_PIN_ASSIGNEMENT_MASK;
458
459 /* Only send Configure command if a configuration has been set */
460 if (dp->alt->active && DP_CONF_CURRENTLY(dp->data.conf)) {
461 ret = dp_altmode_configure_vdm(dp, conf);
462 if (ret)
463 goto out_unlock;
464 }
465
466 dp->data.conf = conf;
467
468 out_unlock:
469 mutex_unlock(&dp->lock);
470
471 return ret ? ret : size;
472 }
473
pin_assignment_show(struct device * dev,struct device_attribute * attr,char * buf)474 static ssize_t pin_assignment_show(struct device *dev,
475 struct device_attribute *attr, char *buf)
476 {
477 struct dp_altmode *dp = dev_get_drvdata(dev);
478 u8 assignments;
479 int len = 0;
480 u8 cur;
481 int i;
482
483 mutex_lock(&dp->lock);
484
485 cur = get_count_order(DP_CONF_GET_PIN_ASSIGN(dp->data.conf));
486
487 assignments = get_current_pin_assignments(dp);
488
489 for (i = 0; assignments; assignments >>= 1, i++) {
490 if (assignments & 1) {
491 if (i == cur)
492 len += sprintf(buf + len, "[%s] ",
493 pin_assignments[i]);
494 else
495 len += sprintf(buf + len, "%s ",
496 pin_assignments[i]);
497 }
498 }
499
500 mutex_unlock(&dp->lock);
501
502 buf[len - 1] = '\n';
503 return len;
504 }
505 static DEVICE_ATTR_RW(pin_assignment);
506
507 static struct attribute *dp_altmode_attrs[] = {
508 &dev_attr_configuration.attr,
509 &dev_attr_pin_assignment.attr,
510 NULL
511 };
512
513 static const struct attribute_group dp_altmode_group = {
514 .name = "displayport",
515 .attrs = dp_altmode_attrs,
516 };
517
dp_altmode_probe(struct typec_altmode * alt)518 int dp_altmode_probe(struct typec_altmode *alt)
519 {
520 const struct typec_altmode *port = typec_altmode_get_partner(alt);
521 struct dp_altmode *dp;
522 int ret;
523
524 /* FIXME: Port can only be DFP_U. */
525
526 /* Make sure we have compatiple pin configurations */
527 if (!(DP_CAP_DFP_D_PIN_ASSIGN(port->vdo) &
528 DP_CAP_UFP_D_PIN_ASSIGN(alt->vdo)) &&
529 !(DP_CAP_UFP_D_PIN_ASSIGN(port->vdo) &
530 DP_CAP_DFP_D_PIN_ASSIGN(alt->vdo)))
531 return -ENODEV;
532
533 ret = sysfs_create_group(&alt->dev.kobj, &dp_altmode_group);
534 if (ret)
535 return ret;
536
537 dp = devm_kzalloc(&alt->dev, sizeof(*dp), GFP_KERNEL);
538 if (!dp)
539 return -ENOMEM;
540
541 INIT_WORK(&dp->work, dp_altmode_work);
542 mutex_init(&dp->lock);
543 dp->port = port;
544 dp->alt = alt;
545
546 alt->desc = "DisplayPort";
547 alt->ops = &dp_altmode_ops;
548
549 typec_altmode_set_drvdata(alt, dp);
550
551 dp->state = DP_STATE_ENTER;
552 schedule_work(&dp->work);
553
554 return 0;
555 }
556 EXPORT_SYMBOL_GPL(dp_altmode_probe);
557
dp_altmode_remove(struct typec_altmode * alt)558 void dp_altmode_remove(struct typec_altmode *alt)
559 {
560 struct dp_altmode *dp = typec_altmode_get_drvdata(alt);
561
562 sysfs_remove_group(&alt->dev.kobj, &dp_altmode_group);
563 cancel_work_sync(&dp->work);
564 }
565 EXPORT_SYMBOL_GPL(dp_altmode_remove);
566
567 static const struct typec_device_id dp_typec_id[] = {
568 { USB_TYPEC_DP_SID, USB_TYPEC_DP_MODE },
569 { },
570 };
571 MODULE_DEVICE_TABLE(typec, dp_typec_id);
572
573 static struct typec_altmode_driver dp_altmode_driver = {
574 .id_table = dp_typec_id,
575 .probe = dp_altmode_probe,
576 .remove = dp_altmode_remove,
577 .driver = {
578 .name = "typec_displayport",
579 .owner = THIS_MODULE,
580 },
581 };
582 module_typec_altmode_driver(dp_altmode_driver);
583
584 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
585 MODULE_LICENSE("GPL v2");
586 MODULE_DESCRIPTION("DisplayPort Alternate Mode");
587