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 
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 
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_DFP_D_PIN_ASSIGN(dp->alt->vdo) &
92 			     DP_CAP_UFP_D_PIN_ASSIGN(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 
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 
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 
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 
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)
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 
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 
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 
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
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 
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 static ssize_t
413 pin_assignment_store(struct device *dev, struct device_attribute *attr,
414 		     const char *buf, size_t size)
415 {
416 	struct dp_altmode *dp = dev_get_drvdata(dev);
417 	u8 assignments;
418 	u32 conf;
419 	int ret;
420 
421 	ret = sysfs_match_string(pin_assignments, buf);
422 	if (ret < 0)
423 		return ret;
424 
425 	conf = DP_CONF_SET_PIN_ASSIGN(BIT(ret));
426 	ret = 0;
427 
428 	mutex_lock(&dp->lock);
429 
430 	if (conf & dp->data.conf)
431 		goto out_unlock;
432 
433 	if (dp->state != DP_STATE_IDLE) {
434 		ret = -EBUSY;
435 		goto out_unlock;
436 	}
437 
438 	if (DP_CONF_CURRENTLY(dp->data.conf) == DP_CONF_DFP_D)
439 		assignments = DP_CAP_UFP_D_PIN_ASSIGN(dp->alt->vdo);
440 	else
441 		assignments = DP_CAP_DFP_D_PIN_ASSIGN(dp->alt->vdo);
442 
443 	if (!(DP_CONF_GET_PIN_ASSIGN(conf) & assignments)) {
444 		ret = -EINVAL;
445 		goto out_unlock;
446 	}
447 
448 	conf |= dp->data.conf & ~DP_CONF_PIN_ASSIGNEMENT_MASK;
449 
450 	/* Only send Configure command if a configuration has been set */
451 	if (dp->alt->active && DP_CONF_CURRENTLY(dp->data.conf)) {
452 		ret = dp_altmode_configure_vdm(dp, conf);
453 		if (ret)
454 			goto out_unlock;
455 	}
456 
457 	dp->data.conf = conf;
458 
459 out_unlock:
460 	mutex_unlock(&dp->lock);
461 
462 	return ret ? ret : size;
463 }
464 
465 static ssize_t pin_assignment_show(struct device *dev,
466 				   struct device_attribute *attr, char *buf)
467 {
468 	struct dp_altmode *dp = dev_get_drvdata(dev);
469 	u8 assignments;
470 	int len = 0;
471 	u8 cur;
472 	int i;
473 
474 	mutex_lock(&dp->lock);
475 
476 	cur = get_count_order(DP_CONF_GET_PIN_ASSIGN(dp->data.conf));
477 
478 	if (DP_CONF_CURRENTLY(dp->data.conf) == DP_CONF_DFP_D)
479 		assignments = DP_CAP_UFP_D_PIN_ASSIGN(dp->alt->vdo);
480 	else
481 		assignments = DP_CAP_DFP_D_PIN_ASSIGN(dp->alt->vdo);
482 
483 	for (i = 0; assignments; assignments >>= 1, i++) {
484 		if (assignments & 1) {
485 			if (i == cur)
486 				len += sprintf(buf + len, "[%s] ",
487 					       pin_assignments[i]);
488 			else
489 				len += sprintf(buf + len, "%s ",
490 					       pin_assignments[i]);
491 		}
492 	}
493 
494 	mutex_unlock(&dp->lock);
495 
496 	buf[len - 1] = '\n';
497 	return len;
498 }
499 static DEVICE_ATTR_RW(pin_assignment);
500 
501 static struct attribute *dp_altmode_attrs[] = {
502 	&dev_attr_configuration.attr,
503 	&dev_attr_pin_assignment.attr,
504 	NULL
505 };
506 
507 static const struct attribute_group dp_altmode_group = {
508 	.name = "displayport",
509 	.attrs = dp_altmode_attrs,
510 };
511 
512 int dp_altmode_probe(struct typec_altmode *alt)
513 {
514 	const struct typec_altmode *port = typec_altmode_get_partner(alt);
515 	struct dp_altmode *dp;
516 	int ret;
517 
518 	/* FIXME: Port can only be DFP_U. */
519 
520 	/* Make sure we have compatiple pin configurations */
521 	if (!(DP_CAP_DFP_D_PIN_ASSIGN(port->vdo) &
522 	      DP_CAP_UFP_D_PIN_ASSIGN(alt->vdo)) &&
523 	    !(DP_CAP_UFP_D_PIN_ASSIGN(port->vdo) &
524 	      DP_CAP_DFP_D_PIN_ASSIGN(alt->vdo)))
525 		return -ENODEV;
526 
527 	ret = sysfs_create_group(&alt->dev.kobj, &dp_altmode_group);
528 	if (ret)
529 		return ret;
530 
531 	dp = devm_kzalloc(&alt->dev, sizeof(*dp), GFP_KERNEL);
532 	if (!dp)
533 		return -ENOMEM;
534 
535 	INIT_WORK(&dp->work, dp_altmode_work);
536 	mutex_init(&dp->lock);
537 	dp->port = port;
538 	dp->alt = alt;
539 
540 	alt->desc = "DisplayPort";
541 	alt->ops = &dp_altmode_ops;
542 
543 	typec_altmode_set_drvdata(alt, dp);
544 
545 	dp->state = DP_STATE_ENTER;
546 	schedule_work(&dp->work);
547 
548 	return 0;
549 }
550 EXPORT_SYMBOL_GPL(dp_altmode_probe);
551 
552 void dp_altmode_remove(struct typec_altmode *alt)
553 {
554 	struct dp_altmode *dp = typec_altmode_get_drvdata(alt);
555 
556 	sysfs_remove_group(&alt->dev.kobj, &dp_altmode_group);
557 	cancel_work_sync(&dp->work);
558 }
559 EXPORT_SYMBOL_GPL(dp_altmode_remove);
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