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