xref: /linux/drivers/usb/typec/altmodes/displayport.c (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce) !
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_ENTER_PRIME,
54 	DP_STATE_UPDATE,
55 	DP_STATE_CONFIGURE,
56 	DP_STATE_CONFIGURE_PRIME,
57 	DP_STATE_EXIT,
58 	DP_STATE_EXIT_PRIME,
59 };
60 
61 struct dp_altmode {
62 	struct typec_displayport_data data;
63 	struct typec_displayport_data data_prime;
64 
65 	enum dp_state state;
66 	bool hpd;
67 	bool pending_hpd;
68 	u32 irq_hpd_count;
69 	/*
70 	 * hpd is mandatory for irq_hpd assertion, so irq_hpd also needs its own pending flag if
71 	 * both hpd and irq_hpd are asserted in the first Status Update before the pin assignment
72 	 * is configured.
73 	 */
74 	bool pending_irq_hpd;
75 
76 	struct mutex lock; /* device lock */
77 	struct work_struct work;
78 	struct typec_altmode *alt;
79 	const struct typec_altmode *port;
80 	struct fwnode_handle *connector_fwnode;
81 	struct typec_altmode *plug_prime;
82 };
83 
dp_altmode_notify(struct dp_altmode * dp)84 static int dp_altmode_notify(struct dp_altmode *dp)
85 {
86 	unsigned long conf;
87 	u8 state;
88 
89 	if (dp->data.conf) {
90 		state = get_count_order(DP_CONF_GET_PIN_ASSIGN(dp->data.conf));
91 		conf = TYPEC_MODAL_STATE(state);
92 	} else {
93 		conf = TYPEC_STATE_USB;
94 	}
95 
96 	return typec_altmode_notify(dp->alt, conf, &dp->data);
97 }
98 
dp_altmode_configure(struct dp_altmode * dp,u8 con)99 static int dp_altmode_configure(struct dp_altmode *dp, u8 con)
100 {
101 	u8 pin_assign = 0;
102 	u32 conf;
103 
104 	/* DP Signalling */
105 	conf = (dp->data.conf & DP_CONF_SIGNALLING_MASK) >> DP_CONF_SIGNALLING_SHIFT;
106 
107 	switch (con) {
108 	case DP_STATUS_CON_DISABLED:
109 		return 0;
110 	case DP_STATUS_CON_DFP_D:
111 		conf |= DP_CONF_UFP_U_AS_DFP_D;
112 		pin_assign = DP_CAP_UFP_D_PIN_ASSIGN(dp->alt->vdo) &
113 			     DP_CAP_DFP_D_PIN_ASSIGN(dp->port->vdo);
114 		/* Account for active cable capabilities */
115 		if (dp->plug_prime)
116 			pin_assign &= DP_CAP_DFP_D_PIN_ASSIGN(dp->plug_prime->vdo);
117 		break;
118 	case DP_STATUS_CON_UFP_D:
119 	case DP_STATUS_CON_BOTH: /* NOTE: First acting as DP source */
120 		conf |= DP_CONF_UFP_U_AS_UFP_D;
121 		pin_assign = DP_CAP_PIN_ASSIGN_UFP_D(dp->alt->vdo) &
122 				 DP_CAP_PIN_ASSIGN_DFP_D(dp->port->vdo);
123 		/* Account for active cable capabilities */
124 		if (dp->plug_prime)
125 			pin_assign &= DP_CAP_UFP_D_PIN_ASSIGN(dp->plug_prime->vdo);
126 		break;
127 	default:
128 		break;
129 	}
130 
131 	/* Determining the initial pin assignment. */
132 	if (!DP_CONF_GET_PIN_ASSIGN(dp->data.conf)) {
133 		/* Is USB together with DP preferred */
134 		if (dp->data.status & DP_STATUS_PREFER_MULTI_FUNC &&
135 		    pin_assign & DP_PIN_ASSIGN_MULTI_FUNC_MASK)
136 			pin_assign &= DP_PIN_ASSIGN_MULTI_FUNC_MASK;
137 		else if (pin_assign & DP_PIN_ASSIGN_DP_ONLY_MASK) {
138 			pin_assign &= DP_PIN_ASSIGN_DP_ONLY_MASK;
139 			/* Default to pin assign C if available */
140 			if (pin_assign & BIT(DP_PIN_ASSIGN_C))
141 				pin_assign = BIT(DP_PIN_ASSIGN_C);
142 		}
143 
144 		if (!pin_assign)
145 			return -EINVAL;
146 
147 		conf |= DP_CONF_SET_PIN_ASSIGN(pin_assign);
148 	}
149 
150 	dp->data.conf = conf;
151 	if (dp->plug_prime)
152 		dp->data_prime.conf = conf;
153 
154 	return 0;
155 }
156 
dp_altmode_status_update(struct dp_altmode * dp)157 static int dp_altmode_status_update(struct dp_altmode *dp)
158 {
159 	bool configured = !!DP_CONF_GET_PIN_ASSIGN(dp->data.conf);
160 	bool hpd = !!(dp->data.status & DP_STATUS_HPD_STATE);
161 	bool irq_hpd = !!(dp->data.status & DP_STATUS_IRQ_HPD);
162 	u8 con = DP_STATUS_CONNECTION(dp->data.status);
163 	int ret = 0;
164 
165 	if (configured && (dp->data.status & DP_STATUS_SWITCH_TO_USB)) {
166 		dp->data.conf = 0;
167 		dp->data_prime.conf = 0;
168 		dp->state = dp->plug_prime ? DP_STATE_CONFIGURE_PRIME :
169 					     DP_STATE_CONFIGURE;
170 	} else if (dp->data.status & DP_STATUS_EXIT_DP_MODE) {
171 		dp->state = DP_STATE_EXIT;
172 	} else if (!(con & DP_CONF_CURRENTLY(dp->data.conf))) {
173 		ret = dp_altmode_configure(dp, con);
174 		if (!ret) {
175 			dp->state = dp->plug_prime ? DP_STATE_CONFIGURE_PRIME :
176 						     DP_STATE_CONFIGURE;
177 			if (dp->hpd != hpd) {
178 				dp->hpd = hpd;
179 				dp->pending_hpd = true;
180 			}
181 			if (dp->hpd && dp->pending_hpd && irq_hpd)
182 				dp->pending_irq_hpd = true;
183 		}
184 	} else {
185 		drm_connector_oob_hotplug_event(dp->connector_fwnode,
186 						hpd ? connector_status_connected :
187 						      connector_status_disconnected);
188 		dp->hpd = hpd;
189 		sysfs_notify(&dp->alt->dev.kobj, "displayport", "hpd");
190 		if (hpd && irq_hpd) {
191 			dp->irq_hpd_count++;
192 			sysfs_notify(&dp->alt->dev.kobj, "displayport", "irq_hpd");
193 		}
194 	}
195 
196 	return ret;
197 }
198 
dp_altmode_configured(struct dp_altmode * dp)199 static int dp_altmode_configured(struct dp_altmode *dp)
200 {
201 	sysfs_notify(&dp->alt->dev.kobj, "displayport", "configuration");
202 	sysfs_notify(&dp->alt->dev.kobj, "displayport", "pin_assignment");
203 	/*
204 	 * If the DFP_D/UFP_D sends a change in HPD when first notifying the
205 	 * DisplayPort driver that it is connected, then we wait until
206 	 * configuration is complete to signal HPD.
207 	 */
208 	if (dp->pending_hpd) {
209 		drm_connector_oob_hotplug_event(dp->connector_fwnode,
210 						connector_status_connected);
211 		sysfs_notify(&dp->alt->dev.kobj, "displayport", "hpd");
212 		dp->pending_hpd = false;
213 		if (dp->pending_irq_hpd) {
214 			dp->irq_hpd_count++;
215 			sysfs_notify(&dp->alt->dev.kobj, "displayport", "irq_hpd");
216 			dp->pending_irq_hpd = false;
217 		}
218 	}
219 
220 	return dp_altmode_notify(dp);
221 }
222 
dp_altmode_configure_vdm(struct dp_altmode * dp,u32 conf)223 static int dp_altmode_configure_vdm(struct dp_altmode *dp, u32 conf)
224 {
225 	int svdm_version = typec_altmode_get_svdm_version(dp->alt);
226 	u32 header;
227 	int ret;
228 
229 	if (svdm_version < 0)
230 		return svdm_version;
231 
232 	header = DP_HEADER(dp, svdm_version, DP_CMD_CONFIGURE);
233 	ret = typec_altmode_notify(dp->alt, TYPEC_STATE_SAFE, &dp->data);
234 	if (ret) {
235 		dev_err(&dp->alt->dev,
236 			"unable to put to connector to safe mode\n");
237 		return ret;
238 	}
239 
240 	ret = typec_altmode_vdm(dp->alt, header, &conf, 2);
241 	if (ret)
242 		dp_altmode_notify(dp);
243 
244 	return ret;
245 }
246 
dp_altmode_configure_vdm_cable(struct dp_altmode * dp,u32 conf)247 static int dp_altmode_configure_vdm_cable(struct dp_altmode *dp, u32 conf)
248 {
249 	int svdm_version = typec_altmode_get_cable_svdm_version(dp->plug_prime);
250 	u32 header;
251 
252 	if (svdm_version < 0)
253 		return svdm_version;
254 
255 	header = DP_HEADER(dp, svdm_version, DP_CMD_CONFIGURE);
256 
257 	return typec_cable_altmode_vdm(dp->plug_prime, TYPEC_PLUG_SOP_P, header, &conf, 2);
258 }
259 
dp_altmode_work(struct work_struct * work)260 static void dp_altmode_work(struct work_struct *work)
261 {
262 	struct dp_altmode *dp = container_of(work, struct dp_altmode, work);
263 	int svdm_version;
264 	u32 header;
265 	u32 vdo;
266 	int ret;
267 
268 	mutex_lock(&dp->lock);
269 
270 	switch (dp->state) {
271 	case DP_STATE_ENTER:
272 		ret = typec_altmode_enter(dp->alt, NULL);
273 		if (ret && ret != -EBUSY)
274 			dev_err(&dp->alt->dev, "failed to enter mode: %d\n", ret);
275 		break;
276 	case DP_STATE_ENTER_PRIME:
277 		ret = typec_cable_altmode_enter(dp->alt, TYPEC_PLUG_SOP_P, NULL);
278 		/*
279 		 * If we fail to enter Alt Mode on SOP', then we should drop the
280 		 * plug from the driver and attempt to run the driver without
281 		 * it.
282 		 */
283 		if (ret && ret != -EBUSY) {
284 			dev_err(&dp->alt->dev, "plug failed to enter mode\n");
285 			dp->state = DP_STATE_ENTER;
286 			goto disable_prime;
287 		}
288 		break;
289 	case DP_STATE_UPDATE:
290 		svdm_version = typec_altmode_get_svdm_version(dp->alt);
291 		if (svdm_version < 0)
292 			break;
293 		header = DP_HEADER(dp, svdm_version, DP_CMD_STATUS_UPDATE);
294 		vdo = 1;
295 		ret = typec_altmode_vdm(dp->alt, header, &vdo, 2);
296 		if (ret)
297 			dev_err(&dp->alt->dev,
298 				"unable to send Status Update command (%d)\n",
299 				ret);
300 		break;
301 	case DP_STATE_CONFIGURE:
302 		ret = dp_altmode_configure_vdm(dp, dp->data.conf);
303 		if (ret)
304 			dev_err(&dp->alt->dev,
305 				"unable to send Configure command (%d)\n", ret);
306 		break;
307 	case DP_STATE_CONFIGURE_PRIME:
308 		ret = dp_altmode_configure_vdm_cable(dp, dp->data_prime.conf);
309 		if (ret) {
310 			dev_err(&dp->plug_prime->dev,
311 				"unable to send Configure command (%d)\n",
312 				ret);
313 			dp->state = DP_STATE_CONFIGURE;
314 			goto disable_prime;
315 		}
316 		break;
317 	case DP_STATE_EXIT:
318 		if (typec_altmode_exit(dp->alt))
319 			dev_err(&dp->alt->dev, "Exit Mode Failed!\n");
320 		break;
321 	case DP_STATE_EXIT_PRIME:
322 		if (typec_cable_altmode_exit(dp->plug_prime, TYPEC_PLUG_SOP_P))
323 			dev_err(&dp->plug_prime->dev, "Exit Mode Failed!\n");
324 		break;
325 	default:
326 		break;
327 	}
328 
329 	dp->state = DP_STATE_IDLE;
330 
331 	mutex_unlock(&dp->lock);
332 	return;
333 
334 disable_prime:
335 	typec_altmode_put_plug(dp->plug_prime);
336 	dp->plug_prime = NULL;
337 	schedule_work(&dp->work);
338 	mutex_unlock(&dp->lock);
339 }
340 
dp_altmode_attention(struct typec_altmode * alt,const u32 vdo)341 static void dp_altmode_attention(struct typec_altmode *alt, const u32 vdo)
342 {
343 	struct dp_altmode *dp = typec_altmode_get_drvdata(alt);
344 	u8 old_state;
345 
346 	mutex_lock(&dp->lock);
347 
348 	old_state = dp->state;
349 	dp->data.status = vdo;
350 
351 	if (old_state != DP_STATE_IDLE)
352 		dev_warn(&alt->dev, "ATTENTION while processing state %d\n",
353 			 old_state);
354 
355 	if (dp_altmode_status_update(dp))
356 		dev_warn(&alt->dev, "%s: status update failed\n", __func__);
357 
358 	if (dp_altmode_notify(dp))
359 		dev_err(&alt->dev, "%s: notification failed\n", __func__);
360 
361 	if (old_state == DP_STATE_IDLE && dp->state != DP_STATE_IDLE)
362 		schedule_work(&dp->work);
363 
364 	mutex_unlock(&dp->lock);
365 }
366 
dp_altmode_vdm(struct typec_altmode * alt,const u32 hdr,const u32 * vdo,int count)367 static int dp_altmode_vdm(struct typec_altmode *alt,
368 			  const u32 hdr, const u32 *vdo, int count)
369 {
370 	struct dp_altmode *dp = typec_altmode_get_drvdata(alt);
371 	int cmd_type = PD_VDO_CMDT(hdr);
372 	int cmd = PD_VDO_CMD(hdr);
373 	int ret = 0;
374 
375 	mutex_lock(&dp->lock);
376 
377 	if (dp->state != DP_STATE_IDLE) {
378 		ret = -EBUSY;
379 		goto err_unlock;
380 	}
381 
382 	switch (cmd_type) {
383 	case CMDT_RSP_ACK:
384 		switch (cmd) {
385 		case CMD_ENTER_MODE:
386 			typec_altmode_update_active(alt, true);
387 			dp->state = DP_STATE_UPDATE;
388 			break;
389 		case CMD_EXIT_MODE:
390 			typec_altmode_update_active(alt, false);
391 			dp->data.status = 0;
392 			dp->data.conf = 0;
393 			if (dp->hpd) {
394 				drm_connector_oob_hotplug_event(dp->connector_fwnode,
395 								connector_status_disconnected);
396 				dp->hpd = false;
397 				sysfs_notify(&dp->alt->dev.kobj, "displayport", "hpd");
398 			}
399 			if (dp->plug_prime)
400 				dp->state = DP_STATE_EXIT_PRIME;
401 			break;
402 		case DP_CMD_STATUS_UPDATE:
403 			dp->data.status = *vdo;
404 			ret = dp_altmode_status_update(dp);
405 			break;
406 		case DP_CMD_CONFIGURE:
407 			ret = dp_altmode_configured(dp);
408 			break;
409 		default:
410 			break;
411 		}
412 		break;
413 	case CMDT_RSP_NAK:
414 		switch (cmd) {
415 		case DP_CMD_STATUS_UPDATE:
416 			dp->state = DP_STATE_EXIT;
417 			break;
418 		case DP_CMD_CONFIGURE:
419 			dp->data.conf = 0;
420 			ret = dp_altmode_configured(dp);
421 			break;
422 		default:
423 			break;
424 		}
425 		break;
426 	default:
427 		break;
428 	}
429 
430 	if (dp->state != DP_STATE_IDLE)
431 		schedule_work(&dp->work);
432 
433 err_unlock:
434 	mutex_unlock(&dp->lock);
435 	return ret;
436 }
437 
dp_cable_altmode_vdm(struct typec_altmode * alt,enum typec_plug_index sop,const u32 hdr,const u32 * vdo,int count)438 static int dp_cable_altmode_vdm(struct typec_altmode *alt, enum typec_plug_index sop,
439 				const u32 hdr, const u32 *vdo, int count)
440 {
441 	struct dp_altmode *dp = typec_altmode_get_drvdata(alt);
442 	int cmd_type = PD_VDO_CMDT(hdr);
443 	int cmd = PD_VDO_CMD(hdr);
444 	int ret = 0;
445 
446 	mutex_lock(&dp->lock);
447 
448 	if (dp->state != DP_STATE_IDLE) {
449 		ret = -EBUSY;
450 		goto err_unlock;
451 	}
452 
453 	switch (cmd_type) {
454 	case CMDT_RSP_ACK:
455 		switch (cmd) {
456 		case CMD_ENTER_MODE:
457 			typec_altmode_update_active(dp->plug_prime, true);
458 			dp->state = DP_STATE_ENTER;
459 			break;
460 		case CMD_EXIT_MODE:
461 			dp->data_prime.status = 0;
462 			dp->data_prime.conf = 0;
463 			typec_altmode_update_active(dp->plug_prime, false);
464 			break;
465 		case DP_CMD_CONFIGURE:
466 			dp->state = DP_STATE_CONFIGURE;
467 			break;
468 		default:
469 			break;
470 		}
471 		break;
472 	case CMDT_RSP_NAK:
473 		switch (cmd) {
474 		case DP_CMD_CONFIGURE:
475 			dp->data_prime.conf = 0;
476 			/* Attempt to configure on SOP, drop plug */
477 			typec_altmode_put_plug(dp->plug_prime);
478 			dp->plug_prime = NULL;
479 			dp->state = DP_STATE_CONFIGURE;
480 			break;
481 		default:
482 			break;
483 		}
484 		break;
485 	default:
486 		break;
487 	}
488 
489 	if (dp->state != DP_STATE_IDLE)
490 		schedule_work(&dp->work);
491 
492 err_unlock:
493 	mutex_unlock(&dp->lock);
494 	return ret;
495 }
496 
dp_altmode_activate(struct typec_altmode * alt,int activate)497 static int dp_altmode_activate(struct typec_altmode *alt, int activate)
498 {
499 	struct dp_altmode *dp = typec_altmode_get_drvdata(alt);
500 	int ret;
501 
502 	if (activate) {
503 		if (dp->plug_prime) {
504 			ret = typec_cable_altmode_enter(alt, TYPEC_PLUG_SOP_P, NULL);
505 			if (ret < 0) {
506 				typec_altmode_put_plug(dp->plug_prime);
507 				dp->plug_prime = NULL;
508 			} else {
509 				return ret;
510 			}
511 		}
512 		return typec_altmode_enter(alt, NULL);
513 	} else {
514 		return typec_altmode_exit(alt);
515 	}
516 }
517 
518 static const struct typec_altmode_ops dp_altmode_ops = {
519 	.attention = dp_altmode_attention,
520 	.vdm = dp_altmode_vdm,
521 	.activate = dp_altmode_activate,
522 };
523 
524 static const struct typec_cable_ops dp_cable_ops = {
525 	.vdm = dp_cable_altmode_vdm,
526 };
527 
528 static const char * const configurations[] = {
529 	[DP_CONF_USB]	= "USB",
530 	[DP_CONF_DFP_D]	= "source",
531 	[DP_CONF_UFP_D]	= "sink",
532 };
533 
534 static ssize_t
configuration_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)535 configuration_store(struct device *dev, struct device_attribute *attr,
536 		    const char *buf, size_t size)
537 {
538 	struct dp_altmode *dp = dev_get_drvdata(dev);
539 	u32 conf;
540 	u32 cap;
541 	int con;
542 	int ret = 0;
543 
544 	con = sysfs_match_string(configurations, buf);
545 	if (con < 0)
546 		return con;
547 
548 	mutex_lock(&dp->lock);
549 
550 	if (dp->state != DP_STATE_IDLE) {
551 		ret = -EBUSY;
552 		goto err_unlock;
553 	}
554 
555 	cap = DP_CAP_CAPABILITY(dp->alt->vdo);
556 
557 	if ((con == DP_CONF_DFP_D && !(cap & DP_CAP_DFP_D)) ||
558 	    (con == DP_CONF_UFP_D && !(cap & DP_CAP_UFP_D))) {
559 		ret = -EINVAL;
560 		goto err_unlock;
561 	}
562 
563 	conf = dp->data.conf & ~DP_CONF_DUAL_D;
564 	conf |= con;
565 
566 	if (dp->alt->active) {
567 		ret = dp_altmode_configure_vdm(dp, conf);
568 		if (ret)
569 			goto err_unlock;
570 	}
571 
572 	dp->data.conf = conf;
573 
574 err_unlock:
575 	mutex_unlock(&dp->lock);
576 
577 	return ret ? ret : size;
578 }
579 
configuration_show(struct device * dev,struct device_attribute * attr,char * buf)580 static ssize_t configuration_show(struct device *dev,
581 				  struct device_attribute *attr, char *buf)
582 {
583 	struct dp_altmode *dp = dev_get_drvdata(dev);
584 	int len;
585 	u8 cap;
586 	u8 cur;
587 	int i;
588 
589 	mutex_lock(&dp->lock);
590 
591 	cap = DP_CAP_CAPABILITY(dp->alt->vdo);
592 	cur = DP_CONF_CURRENTLY(dp->data.conf);
593 
594 	len = sprintf(buf, "%s ", cur ? "USB" : "[USB]");
595 
596 	for (i = 1; i < ARRAY_SIZE(configurations); i++) {
597 		if (i == cur)
598 			len += sprintf(buf + len, "[%s] ", configurations[i]);
599 		else if ((i == DP_CONF_DFP_D && cap & DP_CAP_DFP_D) ||
600 			 (i == DP_CONF_UFP_D && cap & DP_CAP_UFP_D))
601 			len += sprintf(buf + len, "%s ", configurations[i]);
602 	}
603 
604 	mutex_unlock(&dp->lock);
605 
606 	buf[len - 1] = '\n';
607 	return len;
608 }
609 static DEVICE_ATTR_RW(configuration);
610 
611 static const char * const pin_assignments[] = {
612 	[DP_PIN_ASSIGN_A] = "A",
613 	[DP_PIN_ASSIGN_B] = "B",
614 	[DP_PIN_ASSIGN_C] = "C",
615 	[DP_PIN_ASSIGN_D] = "D",
616 	[DP_PIN_ASSIGN_E] = "E",
617 	[DP_PIN_ASSIGN_F] = "F",
618 };
619 
620 /*
621  * Helper function to extract a peripheral's currently supported
622  * Pin Assignments from its DisplayPort alternate mode state.
623  */
get_current_pin_assignments(struct dp_altmode * dp)624 static u8 get_current_pin_assignments(struct dp_altmode *dp)
625 {
626 	if (DP_CONF_CURRENTLY(dp->data.conf) == DP_CONF_UFP_U_AS_DFP_D)
627 		return DP_CAP_PIN_ASSIGN_DFP_D(dp->alt->vdo);
628 	else
629 		return DP_CAP_PIN_ASSIGN_UFP_D(dp->alt->vdo);
630 }
631 
632 static ssize_t
pin_assignment_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)633 pin_assignment_store(struct device *dev, struct device_attribute *attr,
634 		     const char *buf, size_t size)
635 {
636 	struct dp_altmode *dp = dev_get_drvdata(dev);
637 	u8 assignments;
638 	u32 conf;
639 	int ret;
640 
641 	ret = sysfs_match_string(pin_assignments, buf);
642 	if (ret < 0)
643 		return ret;
644 
645 	conf = DP_CONF_SET_PIN_ASSIGN(BIT(ret));
646 	ret = 0;
647 
648 	mutex_lock(&dp->lock);
649 
650 	if (conf & dp->data.conf)
651 		goto out_unlock;
652 
653 	if (dp->state != DP_STATE_IDLE) {
654 		ret = -EBUSY;
655 		goto out_unlock;
656 	}
657 
658 	assignments = get_current_pin_assignments(dp);
659 
660 	if (!(DP_CONF_GET_PIN_ASSIGN(conf) & assignments)) {
661 		ret = -EINVAL;
662 		goto out_unlock;
663 	}
664 
665 	conf |= dp->data.conf & ~DP_CONF_PIN_ASSIGNEMENT_MASK;
666 
667 	/* Only send Configure command if a configuration has been set */
668 	if (dp->alt->active && DP_CONF_CURRENTLY(dp->data.conf)) {
669 		/* todo: send manual configure over SOP'*/
670 		ret = dp_altmode_configure_vdm(dp, conf);
671 		if (ret)
672 			goto out_unlock;
673 	}
674 
675 	dp->data.conf = conf;
676 
677 out_unlock:
678 	mutex_unlock(&dp->lock);
679 
680 	return ret ? ret : size;
681 }
682 
pin_assignment_show(struct device * dev,struct device_attribute * attr,char * buf)683 static ssize_t pin_assignment_show(struct device *dev,
684 				   struct device_attribute *attr, char *buf)
685 {
686 	struct dp_altmode *dp = dev_get_drvdata(dev);
687 	u8 assignments;
688 	int len = 0;
689 	u8 cur;
690 	int i;
691 
692 	mutex_lock(&dp->lock);
693 
694 	cur = get_count_order(DP_CONF_GET_PIN_ASSIGN(dp->data.conf));
695 
696 	assignments = get_current_pin_assignments(dp);
697 
698 	for (i = 0; assignments && i < DP_PIN_ASSIGN_MAX; assignments >>= 1, i++) {
699 		if (assignments & 1) {
700 			if (i == cur)
701 				len += sprintf(buf + len, "[%s] ",
702 					       pin_assignments[i]);
703 			else
704 				len += sprintf(buf + len, "%s ",
705 					       pin_assignments[i]);
706 		}
707 	}
708 
709 	mutex_unlock(&dp->lock);
710 
711 	/* get_current_pin_assignments can return 0 when no matching pin assignments are found */
712 	if (len == 0)
713 		len++;
714 
715 	buf[len - 1] = '\n';
716 	return len;
717 }
718 static DEVICE_ATTR_RW(pin_assignment);
719 
hpd_show(struct device * dev,struct device_attribute * attr,char * buf)720 static ssize_t hpd_show(struct device *dev, struct device_attribute *attr, char *buf)
721 {
722 	struct dp_altmode *dp = dev_get_drvdata(dev);
723 
724 	return sysfs_emit(buf, "%d\n", dp->hpd);
725 }
726 static DEVICE_ATTR_RO(hpd);
727 
irq_hpd_show(struct device * dev,struct device_attribute * attr,char * buf)728 static ssize_t irq_hpd_show(struct device *dev, struct device_attribute *attr, char *buf)
729 {
730 	struct dp_altmode *dp = dev_get_drvdata(dev);
731 
732 	return sysfs_emit(buf, "%d\n", dp->irq_hpd_count);
733 }
734 static DEVICE_ATTR_RO(irq_hpd);
735 
736 static struct attribute *displayport_attrs[] = {
737 	&dev_attr_configuration.attr,
738 	&dev_attr_pin_assignment.attr,
739 	&dev_attr_hpd.attr,
740 	&dev_attr_irq_hpd.attr,
741 	NULL
742 };
743 
744 static const struct attribute_group displayport_group = {
745 	.name = "displayport",
746 	.attrs = displayport_attrs,
747 };
748 
749 static const struct attribute_group *displayport_groups[] = {
750 	&displayport_group,
751 	NULL,
752 };
753 
dp_altmode_probe(struct typec_altmode * alt)754 int dp_altmode_probe(struct typec_altmode *alt)
755 {
756 	const struct typec_altmode *port = typec_altmode_get_partner(alt);
757 	struct typec_altmode *plug = typec_altmode_get_plug(alt, TYPEC_PLUG_SOP_P);
758 	struct fwnode_handle *fwnode;
759 	struct dp_altmode *dp;
760 
761 	/* FIXME: Port can only be DFP_U. */
762 
763 	/* Make sure we have compatible pin configurations */
764 	if (!(DP_CAP_PIN_ASSIGN_DFP_D(port->vdo) &
765 	      DP_CAP_PIN_ASSIGN_UFP_D(alt->vdo)) &&
766 	    !(DP_CAP_PIN_ASSIGN_UFP_D(port->vdo) &
767 	      DP_CAP_PIN_ASSIGN_DFP_D(alt->vdo)))
768 		return -ENODEV;
769 
770 	dp = devm_kzalloc(&alt->dev, sizeof(*dp), GFP_KERNEL);
771 	if (!dp)
772 		return -ENOMEM;
773 
774 	INIT_WORK(&dp->work, dp_altmode_work);
775 	mutex_init(&dp->lock);
776 	dp->port = port;
777 	dp->alt = alt;
778 
779 	alt->desc = "DisplayPort";
780 	typec_altmode_set_ops(alt, &dp_altmode_ops);
781 
782 	if (plug) {
783 		plug->desc = "Displayport";
784 		plug->cable_ops = &dp_cable_ops;
785 	}
786 
787 	dp->plug_prime = plug;
788 
789 	fwnode = dev_fwnode(alt->dev.parent->parent); /* typec_port fwnode */
790 	if (fwnode_property_present(fwnode, "displayport"))
791 		dp->connector_fwnode = fwnode_find_reference(fwnode, "displayport", 0);
792 	else
793 		dp->connector_fwnode = fwnode_handle_get(fwnode); /* embedded DP */
794 	if (IS_ERR(dp->connector_fwnode))
795 		dp->connector_fwnode = NULL;
796 
797 	typec_altmode_set_drvdata(alt, dp);
798 	if (plug)
799 		typec_altmode_set_drvdata(plug, dp);
800 
801 	dp->state = plug ? DP_STATE_ENTER_PRIME : DP_STATE_ENTER;
802 	schedule_work(&dp->work);
803 
804 	return 0;
805 }
806 EXPORT_SYMBOL_GPL(dp_altmode_probe);
807 
dp_altmode_remove(struct typec_altmode * alt)808 void dp_altmode_remove(struct typec_altmode *alt)
809 {
810 	struct dp_altmode *dp = typec_altmode_get_drvdata(alt);
811 
812 	cancel_work_sync(&dp->work);
813 	typec_altmode_put_plug(dp->plug_prime);
814 
815 	if (dp->connector_fwnode) {
816 		drm_connector_oob_hotplug_event(dp->connector_fwnode,
817 						connector_status_disconnected);
818 
819 		fwnode_handle_put(dp->connector_fwnode);
820 	}
821 }
822 EXPORT_SYMBOL_GPL(dp_altmode_remove);
823 
824 static const struct typec_device_id dp_typec_id[] = {
825 	{ USB_TYPEC_DP_SID },
826 	{ },
827 };
828 MODULE_DEVICE_TABLE(typec, dp_typec_id);
829 
830 static struct typec_altmode_driver dp_altmode_driver = {
831 	.id_table = dp_typec_id,
832 	.probe = dp_altmode_probe,
833 	.remove = dp_altmode_remove,
834 	.driver = {
835 		.name = "typec_displayport",
836 		.dev_groups = displayport_groups,
837 	},
838 };
839 module_typec_altmode_driver(dp_altmode_driver);
840 
841 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
842 MODULE_LICENSE("GPL v2");
843 MODULE_DESCRIPTION("DisplayPort Alternate Mode");
844