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