xref: /linux/drivers/usb/typec/ucsi/ucsi.c (revision f5e9d31e79c1ce8ba948ecac74d75e9c8d2f0c87)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * USB Type-C Connector System Software Interface driver
4  *
5  * Copyright (C) 2017, Intel Corporation
6  * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7  */
8 
9 #include <linux/completion.h>
10 #include <linux/property.h>
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/delay.h>
14 #include <linux/slab.h>
15 #include <linux/usb/typec_dp.h>
16 
17 #include "ucsi.h"
18 #include "trace.h"
19 
20 /*
21  * UCSI_TIMEOUT_MS - PPM communication timeout
22  *
23  * Ideally we could use MIN_TIME_TO_RESPOND_WITH_BUSY (which is defined in UCSI
24  * specification) here as reference, but unfortunately we can't. It is very
25  * difficult to estimate the time it takes for the system to process the command
26  * before it is actually passed to the PPM.
27  */
28 #define UCSI_TIMEOUT_MS		10000
29 
30 /*
31  * UCSI_SWAP_TIMEOUT_MS - Timeout for role swap requests
32  *
33  * 5 seconds is close to the time it takes for CapsCounter to reach 0, so even
34  * if the PPM does not generate Connector Change events before that with
35  * partners that do not support USB Power Delivery, this should still work.
36  */
37 #define UCSI_SWAP_TIMEOUT_MS	5000
38 
ucsi_notify_common(struct ucsi * ucsi,u32 cci)39 void ucsi_notify_common(struct ucsi *ucsi, u32 cci)
40 {
41 	/* Ignore bogus data in CCI if busy indicator is set. */
42 	if (cci & UCSI_CCI_BUSY)
43 		return;
44 
45 	if (UCSI_CCI_CONNECTOR(cci))
46 		ucsi_connector_change(ucsi, UCSI_CCI_CONNECTOR(cci));
47 
48 	if (cci & UCSI_CCI_ACK_COMPLETE &&
49 	    test_and_clear_bit(ACK_PENDING, &ucsi->flags))
50 		complete(&ucsi->complete);
51 
52 	if (cci & UCSI_CCI_COMMAND_COMPLETE &&
53 	    test_and_clear_bit(COMMAND_PENDING, &ucsi->flags))
54 		complete(&ucsi->complete);
55 }
56 EXPORT_SYMBOL_GPL(ucsi_notify_common);
57 
ucsi_sync_control_common(struct ucsi * ucsi,u64 command,u32 * cci)58 int ucsi_sync_control_common(struct ucsi *ucsi, u64 command, u32 *cci)
59 {
60 	bool ack = UCSI_COMMAND(command) == UCSI_ACK_CC_CI;
61 	int ret;
62 
63 	if (ack)
64 		set_bit(ACK_PENDING, &ucsi->flags);
65 	else
66 		set_bit(COMMAND_PENDING, &ucsi->flags);
67 
68 	reinit_completion(&ucsi->complete);
69 
70 	if (ucsi->message_out_size > 0) {
71 		if (!ucsi->ops->write_message_out) {
72 			ucsi->message_out_size = 0;
73 			ret = -EOPNOTSUPP;
74 			goto out_clear_bit;
75 		}
76 
77 		ret = ucsi->ops->write_message_out(ucsi, ucsi->message_out,
78 						   ucsi->message_out_size);
79 		ucsi->message_out_size = 0;
80 		if (ret)
81 			goto out_clear_bit;
82 	}
83 
84 	ret = ucsi->ops->async_control(ucsi, command);
85 	if (ret)
86 		goto out_clear_bit;
87 
88 	if (!wait_for_completion_timeout(&ucsi->complete, 5 * HZ))
89 		ret = -ETIMEDOUT;
90 
91 out_clear_bit:
92 	if (ack)
93 		clear_bit(ACK_PENDING, &ucsi->flags);
94 	else
95 		clear_bit(COMMAND_PENDING, &ucsi->flags);
96 
97 	if (!ret && cci)
98 		ret = ucsi->ops->read_cci(ucsi, cci);
99 
100 	if (!ret && ucsi->message_in_size > 0 &&
101 	    (*cci & UCSI_CCI_COMMAND_COMPLETE))
102 		ret = ucsi->ops->read_message_in(ucsi, ucsi->message_in,
103 						 ucsi->message_in_size);
104 
105 	return ret;
106 }
107 EXPORT_SYMBOL_GPL(ucsi_sync_control_common);
108 
ucsi_acknowledge(struct ucsi * ucsi,bool conn_ack)109 static int ucsi_acknowledge(struct ucsi *ucsi, bool conn_ack)
110 {
111 	u64 ctrl;
112 
113 	ctrl = UCSI_ACK_CC_CI;
114 	ctrl |= UCSI_ACK_COMMAND_COMPLETE;
115 	if (conn_ack) {
116 		clear_bit(EVENT_PENDING, &ucsi->flags);
117 		ctrl |= UCSI_ACK_CONNECTOR_CHANGE;
118 	}
119 
120 	ucsi->message_in_size = 0;
121 	return ucsi->ops->sync_control(ucsi, ctrl, NULL);
122 }
123 
ucsi_run_command(struct ucsi * ucsi,u64 command,u32 * cci,bool conn_ack)124 static int ucsi_run_command(struct ucsi *ucsi, u64 command, u32 *cci, bool conn_ack)
125 {
126 	int ret, err;
127 
128 	*cci = 0;
129 
130 	if (ucsi->message_in_size > UCSI_MAX_DATA_LENGTH(ucsi))
131 		return -EINVAL;
132 
133 	ret = ucsi->ops->sync_control(ucsi, command, cci);
134 
135 	if (*cci & UCSI_CCI_BUSY) {
136 		ucsi->message_in_size = 0;
137 		return ucsi_run_command(ucsi, UCSI_CANCEL, cci, false) ?: -EBUSY;
138 	}
139 	if (ret)
140 		return ret;
141 
142 	if (!(*cci & UCSI_CCI_COMMAND_COMPLETE))
143 		return -EIO;
144 
145 	if (*cci & UCSI_CCI_NOT_SUPPORTED)
146 		err = -EOPNOTSUPP;
147 	else if (*cci & UCSI_CCI_ERROR)
148 		err = -EIO;
149 	else
150 		err = 0;
151 
152 	/*
153 	 * Don't ACK connection change if there was an error.
154 	 */
155 	ret = ucsi_acknowledge(ucsi, err ? false : conn_ack);
156 	if (ret)
157 		return ret;
158 
159 	return err ?: UCSI_CCI_LENGTH(*cci);
160 }
161 
ucsi_read_error(struct ucsi * ucsi,u8 connector_num)162 static int ucsi_read_error(struct ucsi *ucsi, u8 connector_num)
163 {
164 	u64 command;
165 	u16 error;
166 	u32 cci;
167 	int ret;
168 
169 	command = UCSI_GET_ERROR_STATUS | UCSI_CONNECTOR_NUMBER(connector_num);
170 	ucsi->message_in_size = sizeof(error);
171 	ret = ucsi_run_command(ucsi, command, &cci, false);
172 	if (ret < 0)
173 		return ret;
174 
175 	memcpy(&error, ucsi->message_in, sizeof(error));
176 
177 	switch (error) {
178 	case UCSI_ERROR_INCOMPATIBLE_PARTNER:
179 		return -EOPNOTSUPP;
180 	case UCSI_ERROR_CC_COMMUNICATION_ERR:
181 		return -ECOMM;
182 	case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
183 		return -EPROTO;
184 	case UCSI_ERROR_DEAD_BATTERY:
185 		dev_warn(ucsi->dev, "Dead battery condition!\n");
186 		return -EPERM;
187 	case UCSI_ERROR_INVALID_CON_NUM:
188 	case UCSI_ERROR_UNREGONIZED_CMD:
189 	case UCSI_ERROR_INVALID_CMD_ARGUMENT:
190 		dev_err(ucsi->dev, "possible UCSI driver bug %u\n", error);
191 		return -EINVAL;
192 	case UCSI_ERROR_OVERCURRENT:
193 		dev_warn(ucsi->dev, "Overcurrent condition\n");
194 		break;
195 	case UCSI_ERROR_PARTNER_REJECTED_SWAP:
196 		dev_warn(ucsi->dev, "Partner rejected swap\n");
197 		break;
198 	case UCSI_ERROR_HARD_RESET:
199 		dev_warn(ucsi->dev, "Hard reset occurred\n");
200 		break;
201 	case UCSI_ERROR_PPM_POLICY_CONFLICT:
202 		dev_warn(ucsi->dev, "PPM Policy conflict\n");
203 		break;
204 	case UCSI_ERROR_SWAP_REJECTED:
205 		dev_warn(ucsi->dev, "Swap rejected\n");
206 		break;
207 	case UCSI_ERROR_REVERSE_CURRENT_PROTECTION:
208 		dev_warn(ucsi->dev, "Reverse Current Protection detected\n");
209 		break;
210 	case UCSI_ERROR_SET_SINK_PATH_REJECTED:
211 		dev_warn(ucsi->dev, "Set Sink Path rejected\n");
212 		break;
213 	case UCSI_ERROR_UNDEFINED:
214 	default:
215 		dev_err(ucsi->dev, "unknown error %u\n", error);
216 		break;
217 	}
218 
219 	return -EIO;
220 }
221 
ucsi_send_command_common(struct ucsi * ucsi,u64 cmd,bool conn_ack)222 static int ucsi_send_command_common(struct ucsi *ucsi, u64 cmd, bool conn_ack)
223 {
224 	u8 connector_num;
225 	u32 cci;
226 	int ret;
227 
228 	if (ucsi->version > UCSI_VERSION_1_2) {
229 		switch (UCSI_COMMAND(cmd)) {
230 		case UCSI_GET_ALTERNATE_MODES:
231 			connector_num = UCSI_GET_ALTMODE_GET_CONNECTOR_NUMBER(cmd);
232 			break;
233 		case UCSI_PPM_RESET:
234 		case UCSI_CANCEL:
235 		case UCSI_ACK_CC_CI:
236 		case UCSI_SET_NOTIFICATION_ENABLE:
237 		case UCSI_GET_CAPABILITY:
238 			connector_num = 0;
239 			break;
240 		default:
241 			connector_num = UCSI_DEFAULT_GET_CONNECTOR_NUMBER(cmd);
242 			break;
243 		}
244 	} else {
245 		connector_num = 0;
246 	}
247 
248 	mutex_lock(&ucsi->ppm_lock);
249 
250 	ret = ucsi_run_command(ucsi, cmd, &cci, conn_ack);
251 
252 	if (cci & UCSI_CCI_ERROR)
253 		ret = ucsi_read_error(ucsi, connector_num);
254 
255 	mutex_unlock(&ucsi->ppm_lock);
256 	return ret;
257 }
258 
ucsi_send_command(struct ucsi * ucsi,u64 command)259 int ucsi_send_command(struct ucsi *ucsi, u64 command)
260 {
261 	return ucsi_send_command_common(ucsi, command, false);
262 }
263 EXPORT_SYMBOL_GPL(ucsi_send_command);
264 
265 /* -------------------------------------------------------------------------- */
266 
267 struct ucsi_work {
268 	struct delayed_work work;
269 	struct list_head node;
270 	unsigned long delay;
271 	unsigned int count;
272 	struct ucsi_connector *con;
273 	int (*cb)(struct ucsi_connector *);
274 };
275 
ucsi_poll_worker(struct work_struct * work)276 static void ucsi_poll_worker(struct work_struct *work)
277 {
278 	struct ucsi_work *uwork = container_of(work, struct ucsi_work, work.work);
279 	struct ucsi_connector *con = uwork->con;
280 	int ret;
281 
282 	mutex_lock(&con->lock);
283 
284 	if (!con->partner) {
285 		list_del(&uwork->node);
286 		mutex_unlock(&con->lock);
287 		kfree(uwork);
288 		return;
289 	}
290 
291 	ret = uwork->cb(con);
292 
293 	if (uwork->count-- && (ret == -EBUSY || ret == -ETIMEDOUT)) {
294 		queue_delayed_work(con->wq, &uwork->work, uwork->delay);
295 	} else {
296 		list_del(&uwork->node);
297 		kfree(uwork);
298 	}
299 
300 	mutex_unlock(&con->lock);
301 }
302 
ucsi_partner_task(struct ucsi_connector * con,int (* cb)(struct ucsi_connector *),int retries,unsigned long delay)303 static int ucsi_partner_task(struct ucsi_connector *con,
304 			     int (*cb)(struct ucsi_connector *),
305 			     int retries, unsigned long delay)
306 {
307 	struct ucsi_work *uwork;
308 
309 	if (!con->partner)
310 		return 0;
311 
312 	uwork = kzalloc(sizeof(*uwork), GFP_KERNEL);
313 	if (!uwork)
314 		return -ENOMEM;
315 
316 	INIT_DELAYED_WORK(&uwork->work, ucsi_poll_worker);
317 	uwork->count = retries;
318 	uwork->delay = delay;
319 	uwork->con = con;
320 	uwork->cb = cb;
321 
322 	list_add_tail(&uwork->node, &con->partner_tasks);
323 	queue_delayed_work(con->wq, &uwork->work, delay);
324 
325 	return 0;
326 }
327 
328 /* -------------------------------------------------------------------------- */
329 
ucsi_altmode_update_active(struct ucsi_connector * con)330 void ucsi_altmode_update_active(struct ucsi_connector *con)
331 {
332 	const struct typec_altmode *altmode = NULL;
333 	u64 command;
334 	int ret;
335 	u8 cur;
336 	int i;
337 
338 	command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
339 	con->ucsi->message_in_size = sizeof(cur);
340 	ret = ucsi_send_command(con->ucsi, command);
341 	if (ret < 0) {
342 		if (con->ucsi->version > 0x0100) {
343 			dev_err(con->ucsi->dev,
344 				"GET_CURRENT_CAM command failed\n");
345 			return;
346 		}
347 		cur = 0xff;
348 	} else {
349 		memcpy(&cur, con->ucsi->message_in, sizeof(cur));
350 	}
351 
352 	if (cur < UCSI_MAX_ALTMODES)
353 		altmode = typec_altmode_get_partner(con->port_altmode[cur]);
354 
355 	for (i = 0; con->partner_altmode[i]; i++)
356 		typec_altmode_update_active(con->partner_altmode[i],
357 					    con->partner_altmode[i] == altmode);
358 }
359 
ucsi_altmode_next_mode(struct typec_altmode ** alt,u16 svid)360 static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
361 {
362 	u8 mode = 1;
363 	int i;
364 
365 	for (i = 0; alt[i]; i++) {
366 		if (i > MODE_DISCOVERY_MAX)
367 			return -ERANGE;
368 
369 		if (alt[i]->svid == svid)
370 			mode++;
371 	}
372 
373 	return mode;
374 }
375 
ucsi_next_altmode(struct typec_altmode ** alt)376 static int ucsi_next_altmode(struct typec_altmode **alt)
377 {
378 	int i = 0;
379 
380 	for (i = 0; i < UCSI_MAX_ALTMODES; i++)
381 		if (!alt[i])
382 			return i;
383 
384 	return -ENOENT;
385 }
386 
ucsi_get_num_altmode(struct typec_altmode ** alt)387 static int ucsi_get_num_altmode(struct typec_altmode **alt)
388 {
389 	int i;
390 
391 	for (i = 0; i < UCSI_MAX_ALTMODES; i++)
392 		if (!alt[i])
393 			break;
394 
395 	return i;
396 }
397 
ucsi_register_altmode(struct ucsi_connector * con,struct typec_altmode_desc * desc,u8 recipient)398 static int ucsi_register_altmode(struct ucsi_connector *con,
399 				 struct typec_altmode_desc *desc,
400 				 u8 recipient)
401 {
402 	struct typec_altmode *alt;
403 	bool override;
404 	int ret;
405 	int i;
406 
407 	override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
408 
409 	switch (recipient) {
410 	case UCSI_RECIPIENT_CON:
411 		i = ucsi_next_altmode(con->port_altmode);
412 		if (i < 0) {
413 			ret = i;
414 			goto err;
415 		}
416 
417 		ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid);
418 		if (ret < 0)
419 			return ret;
420 
421 		desc->mode = ret;
422 
423 		switch (desc->svid) {
424 		case USB_TYPEC_DP_SID:
425 			alt = ucsi_register_displayport(con, override, i, desc);
426 			break;
427 		case USB_TYPEC_NVIDIA_VLINK_SID:
428 			if (desc->vdo == USB_TYPEC_NVIDIA_VLINK_DBG_VDO)
429 				alt = typec_port_register_altmode(con->port,
430 								  desc);
431 			else
432 				alt = ucsi_register_displayport(con, override,
433 								i, desc);
434 			break;
435 		default:
436 			alt = typec_port_register_altmode(con->port, desc);
437 			break;
438 		}
439 
440 		if (IS_ERR(alt)) {
441 			ret = PTR_ERR(alt);
442 			goto err;
443 		}
444 
445 		con->port_altmode[i] = alt;
446 		break;
447 	case UCSI_RECIPIENT_SOP:
448 		i = ucsi_next_altmode(con->partner_altmode);
449 		if (i < 0) {
450 			ret = i;
451 			goto err;
452 		}
453 
454 		ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid);
455 		if (ret < 0)
456 			return ret;
457 
458 		desc->mode = ret;
459 
460 		alt = typec_partner_register_altmode(con->partner, desc);
461 		if (IS_ERR(alt)) {
462 			ret = PTR_ERR(alt);
463 			goto err;
464 		}
465 
466 		con->partner_altmode[i] = alt;
467 		break;
468 	case UCSI_RECIPIENT_SOP_P:
469 		i = ucsi_next_altmode(con->plug_altmode);
470 		if (i < 0) {
471 			ret = i;
472 			goto err;
473 		}
474 
475 		ret = ucsi_altmode_next_mode(con->plug_altmode, desc->svid);
476 		if (ret < 0)
477 			return ret;
478 
479 		desc->mode = ret;
480 
481 		alt = typec_plug_register_altmode(con->plug, desc);
482 		if (IS_ERR(alt)) {
483 			ret = PTR_ERR(alt);
484 			goto err;
485 		}
486 
487 		con->plug_altmode[i] = alt;
488 		break;
489 	default:
490 		return -EINVAL;
491 	}
492 
493 	trace_ucsi_register_altmode(recipient, alt);
494 
495 	return 0;
496 
497 err:
498 	dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n",
499 		desc->svid, desc->mode);
500 
501 	return ret;
502 }
503 
504 static int
ucsi_register_altmodes_nvidia(struct ucsi_connector * con,u8 recipient)505 ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient)
506 {
507 	int max_altmodes = UCSI_MAX_ALTMODES;
508 	struct typec_altmode_desc desc;
509 	struct ucsi_altmode alt;
510 	struct ucsi_altmode orig[UCSI_MAX_ALTMODES];
511 	struct ucsi_altmode updated[UCSI_MAX_ALTMODES];
512 	struct ucsi *ucsi = con->ucsi;
513 	bool multi_dp = false;
514 	u64 command;
515 	int ret;
516 	int len;
517 	int i;
518 	int k = 0;
519 
520 	if (recipient == UCSI_RECIPIENT_CON)
521 		max_altmodes = con->ucsi->cap.num_alt_modes;
522 
523 	memset(orig, 0, sizeof(orig));
524 	memset(updated, 0, sizeof(updated));
525 
526 	/* First get all the alternate modes */
527 	for (i = 0; i < max_altmodes; i++) {
528 		memset(&alt, 0, sizeof(alt));
529 		command = UCSI_GET_ALTERNATE_MODES;
530 		command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
531 		command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
532 		command |= UCSI_GET_ALTMODE_OFFSET(i);
533 		ucsi->message_in_size = sizeof(alt);
534 		len = ucsi_send_command(con->ucsi, command);
535 		/*
536 		 * We are collecting all altmodes first and then registering.
537 		 * Some type-C device will return zero length data beyond last
538 		 * alternate modes. We should not return if length is zero.
539 		 */
540 		if (len < 0)
541 			return len;
542 
543 		memcpy(&alt, ucsi->message_in, sizeof(alt));
544 
545 		/* We got all altmodes, now break out and register them */
546 		if (!len || !alt.svid)
547 			break;
548 
549 		orig[k].mid = alt.mid;
550 		orig[k].svid = alt.svid;
551 		k++;
552 	}
553 	/*
554 	 * Update the original altmode table as some ppms may report
555 	 * multiple DP altmodes.
556 	 */
557 	multi_dp = ucsi->ops->update_altmodes(ucsi, recipient, orig, updated);
558 
559 	/* now register altmodes */
560 	for (i = 0; i < max_altmodes; i++) {
561 		memset(&desc, 0, sizeof(desc));
562 		if (multi_dp) {
563 			desc.svid = updated[i].svid;
564 			desc.vdo = updated[i].mid;
565 		} else {
566 			desc.svid = orig[i].svid;
567 			desc.vdo = orig[i].mid;
568 		}
569 		desc.roles = TYPEC_PORT_DRD;
570 
571 		if (!desc.svid)
572 			return 0;
573 
574 		ret = ucsi_register_altmode(con, &desc, recipient);
575 		if (ret)
576 			return ret;
577 	}
578 
579 	return 0;
580 }
581 
ucsi_register_altmodes(struct ucsi_connector * con,u8 recipient)582 static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
583 {
584 	int max_altmodes = UCSI_MAX_ALTMODES;
585 	struct typec_altmode_desc desc;
586 	struct ucsi_altmode alt[2];
587 	u64 command;
588 	int num;
589 	int ret;
590 	int len;
591 	int j;
592 	int i;
593 
594 	if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
595 		return 0;
596 
597 	if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
598 		return 0;
599 
600 	if (con->ucsi->ops->update_altmodes)
601 		return ucsi_register_altmodes_nvidia(con, recipient);
602 
603 	if (recipient == UCSI_RECIPIENT_CON)
604 		max_altmodes = con->ucsi->cap.num_alt_modes;
605 
606 	for (i = 0; i < max_altmodes;) {
607 		memset(alt, 0, sizeof(alt));
608 		command = UCSI_GET_ALTERNATE_MODES;
609 		command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
610 		command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
611 		command |= UCSI_GET_ALTMODE_OFFSET(i);
612 		con->ucsi->message_in_size = sizeof(alt);
613 		len = ucsi_send_command(con->ucsi, command);
614 		if (len == -EBUSY)
615 			continue;
616 		if (len <= 0)
617 			return len;
618 
619 		memcpy(&alt, con->ucsi->message_in, sizeof(alt));
620 
621 		/*
622 		 * This code is requesting one alt mode at a time, but some PPMs
623 		 * may still return two. If that happens both alt modes need be
624 		 * registered and the offset for the next alt mode has to be
625 		 * incremented.
626 		 */
627 		num = len / sizeof(alt[0]);
628 		i += num;
629 
630 		for (j = 0; j < num; j++) {
631 			if (!alt[j].svid)
632 				return 0;
633 
634 			memset(&desc, 0, sizeof(desc));
635 			desc.vdo = alt[j].mid;
636 			desc.svid = alt[j].svid;
637 			desc.roles = TYPEC_PORT_DRD;
638 
639 			ret = ucsi_register_altmode(con, &desc, recipient);
640 			if (ret)
641 				return ret;
642 		}
643 	}
644 
645 	return 0;
646 }
647 
ucsi_unregister_altmodes(struct ucsi_connector * con,u8 recipient)648 static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
649 {
650 	const struct typec_altmode *pdev;
651 	struct typec_altmode **adev;
652 	int i = 0;
653 
654 	switch (recipient) {
655 	case UCSI_RECIPIENT_CON:
656 		adev = con->port_altmode;
657 		break;
658 	case UCSI_RECIPIENT_SOP:
659 		adev = con->partner_altmode;
660 		break;
661 	case UCSI_RECIPIENT_SOP_P:
662 		adev = con->plug_altmode;
663 		break;
664 	default:
665 		return;
666 	}
667 
668 	while (adev[i]) {
669 		if (recipient == UCSI_RECIPIENT_SOP &&
670 		    (adev[i]->svid == USB_TYPEC_DP_SID ||
671 			(adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID &&
672 			adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) {
673 			pdev = typec_altmode_get_partner(adev[i]);
674 			ucsi_displayport_remove_partner((void *)pdev);
675 		}
676 		typec_unregister_altmode(adev[i]);
677 		adev[i++] = NULL;
678 	}
679 }
680 
ucsi_get_connector_status(struct ucsi_connector * con,bool conn_ack)681 static int ucsi_get_connector_status(struct ucsi_connector *con, bool conn_ack)
682 {
683 	u64 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
684 	size_t size = min(sizeof(con->status),
685 			  UCSI_MAX_DATA_LENGTH(con->ucsi));
686 	int ret;
687 
688 	con->ucsi->message_in_size = size;
689 	ret = ucsi_send_command_common(con->ucsi, command, conn_ack);
690 	memcpy(&con->status, con->ucsi->message_in, size);
691 
692 	return ret < 0 ? ret : 0;
693 }
694 
ucsi_read_pdos(struct ucsi_connector * con,enum typec_role role,int is_partner,u32 * pdos,int offset,int num_pdos)695 static int ucsi_read_pdos(struct ucsi_connector *con,
696 			  enum typec_role role, int is_partner,
697 			  u32 *pdos, int offset, int num_pdos)
698 {
699 	struct ucsi *ucsi = con->ucsi;
700 	u64 command;
701 	int ret;
702 
703 	if (is_partner &&
704 	    ucsi->quirks & UCSI_NO_PARTNER_PDOS &&
705 	    (UCSI_CONSTAT(con, PWR_DIR) || !is_source(role)))
706 		return 0;
707 
708 	command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num);
709 	command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner);
710 	command |= UCSI_GET_PDOS_PDO_OFFSET(offset);
711 	command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1);
712 	command |= is_source(role) ? UCSI_GET_PDOS_SRC_PDOS : 0;
713 	ucsi->message_in_size = num_pdos * sizeof(u32);
714 	ret = ucsi_send_command(ucsi, command);
715 	memcpy(pdos + offset, ucsi->message_in, num_pdos * sizeof(u32));
716 	if (ret < 0 && ret != -ETIMEDOUT)
717 		dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret);
718 
719 	return ret;
720 }
721 
ucsi_get_pdos(struct ucsi_connector * con,enum typec_role role,int is_partner,u32 * pdos)722 static int ucsi_get_pdos(struct ucsi_connector *con, enum typec_role role,
723 			 int is_partner, u32 *pdos)
724 {
725 	struct ucsi *ucsi = con->ucsi;
726 	u8 num_pdos;
727 	int ret;
728 
729 	if (!(ucsi->cap.features & UCSI_CAP_PDO_DETAILS))
730 		return 0;
731 
732 	/* UCSI max payload means only getting at most 4 PDOs at a time */
733 	ret = ucsi_read_pdos(con, role, is_partner, pdos, 0, UCSI_MAX_PDOS);
734 	if (ret < 0)
735 		return ret;
736 
737 	num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */
738 	if (num_pdos < UCSI_MAX_PDOS)
739 		return num_pdos;
740 
741 	/* get the remaining PDOs, if any */
742 	ret = ucsi_read_pdos(con, role, is_partner, pdos, UCSI_MAX_PDOS,
743 			     PDO_MAX_OBJECTS - UCSI_MAX_PDOS);
744 	if (ret < 0)
745 		return ret;
746 
747 	return ret / sizeof(u32) + num_pdos;
748 }
749 
ucsi_get_src_pdos(struct ucsi_connector * con)750 static int ucsi_get_src_pdos(struct ucsi_connector *con)
751 {
752 	int ret;
753 
754 	ret = ucsi_get_pdos(con, TYPEC_SOURCE, 1, con->src_pdos);
755 	if (ret < 0)
756 		return ret;
757 
758 	con->num_pdos = ret;
759 
760 	ucsi_port_psy_changed(con);
761 
762 	return ret;
763 }
764 
ucsi_get_pd_caps(struct ucsi_connector * con,enum typec_role role,bool is_partner)765 static struct usb_power_delivery_capabilities *ucsi_get_pd_caps(struct ucsi_connector *con,
766 								enum typec_role role,
767 								bool is_partner)
768 {
769 	struct usb_power_delivery_capabilities_desc pd_caps;
770 	int ret;
771 
772 	ret = ucsi_get_pdos(con, role, is_partner, pd_caps.pdo);
773 	if (ret <= 0)
774 		return ERR_PTR(ret);
775 
776 	if (ret < PDO_MAX_OBJECTS)
777 		pd_caps.pdo[ret] = 0;
778 
779 	pd_caps.role = role;
780 
781 	return usb_power_delivery_register_capabilities(is_partner ? con->partner_pd : con->pd,
782 							&pd_caps);
783 }
784 
ucsi_get_pd_message(struct ucsi_connector * con,u8 recipient,size_t bytes,void * data,u8 type)785 static int ucsi_get_pd_message(struct ucsi_connector *con, u8 recipient,
786 			       size_t bytes, void *data, u8 type)
787 {
788 	size_t len = min(bytes, UCSI_MAX_DATA_LENGTH(con->ucsi));
789 	u64 command;
790 	u8 offset;
791 	int ret;
792 
793 	for (offset = 0; offset < bytes; offset += len) {
794 		len = min(len, bytes - offset);
795 
796 		command = UCSI_COMMAND(UCSI_GET_PD_MESSAGE) | UCSI_CONNECTOR_NUMBER(con->num);
797 		command |= UCSI_GET_PD_MESSAGE_RECIPIENT(recipient);
798 		command |= UCSI_GET_PD_MESSAGE_OFFSET(offset);
799 		command |= UCSI_GET_PD_MESSAGE_BYTES(len);
800 		command |= UCSI_GET_PD_MESSAGE_TYPE(type);
801 
802 		con->ucsi->message_in_size = len;
803 		ret = ucsi_send_command(con->ucsi, command);
804 		memcpy(data + offset, con->ucsi->message_in, len);
805 		if (ret < 0)
806 			return ret;
807 	}
808 
809 	return 0;
810 }
811 
ucsi_get_partner_identity(struct ucsi_connector * con)812 static int ucsi_get_partner_identity(struct ucsi_connector *con)
813 {
814 	u32 vdo[7] = {};
815 	int ret;
816 
817 	ret = ucsi_get_pd_message(con, UCSI_RECIPIENT_SOP, sizeof(vdo), vdo,
818 				  UCSI_GET_PD_MESSAGE_TYPE_IDENTITY);
819 	if (ret < 0)
820 		return ret;
821 
822 	/* VDM Header is not part of struct usb_pd_identity, so dropping it. */
823 	con->partner_identity = *(struct usb_pd_identity *)&vdo[1];
824 
825 	ret = typec_partner_set_identity(con->partner);
826 	if (ret < 0)
827 		dev_err(con->ucsi->dev, "Failed to set partner identity (%d)\n", ret);
828 
829 	return ret;
830 }
831 
ucsi_get_cable_identity(struct ucsi_connector * con)832 static int ucsi_get_cable_identity(struct ucsi_connector *con)
833 {
834 	u32 vdo[7] = {};
835 	int ret;
836 
837 	ret = ucsi_get_pd_message(con, UCSI_RECIPIENT_SOP_P, sizeof(vdo), vdo,
838 				  UCSI_GET_PD_MESSAGE_TYPE_IDENTITY);
839 	if (ret < 0)
840 		return ret;
841 
842 	con->cable_identity = *(struct usb_pd_identity *)&vdo[1];
843 
844 	ret = typec_cable_set_identity(con->cable);
845 	if (ret < 0)
846 		dev_err(con->ucsi->dev, "Failed to set cable identity (%d)\n", ret);
847 
848 	return ret;
849 }
850 
ucsi_check_altmodes(struct ucsi_connector * con)851 static int ucsi_check_altmodes(struct ucsi_connector *con)
852 {
853 	int ret, num_partner_am;
854 
855 	ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
856 	if (ret && ret != -ETIMEDOUT)
857 		dev_err(con->ucsi->dev,
858 			"con%d: failed to register partner alt modes (%d)\n",
859 			con->num, ret);
860 
861 	/* Ignoring the errors in this case. */
862 	if (con->partner_altmode[0]) {
863 		num_partner_am = ucsi_get_num_altmode(con->partner_altmode);
864 		typec_partner_set_num_altmodes(con->partner, num_partner_am);
865 		ucsi_altmode_update_active(con);
866 		return 0;
867 	} else {
868 		typec_partner_set_num_altmodes(con->partner, 0);
869 	}
870 
871 	return ret;
872 }
873 
ucsi_register_device_pdos(struct ucsi_connector * con)874 static void ucsi_register_device_pdos(struct ucsi_connector *con)
875 {
876 	struct ucsi *ucsi = con->ucsi;
877 	struct usb_power_delivery_desc desc = { ucsi->cap.pd_version };
878 	struct usb_power_delivery_capabilities *pd_cap;
879 
880 	if (con->pd)
881 		return;
882 
883 	con->pd = usb_power_delivery_register(ucsi->dev, &desc);
884 
885 	pd_cap = ucsi_get_pd_caps(con, TYPEC_SOURCE, false);
886 	if (!IS_ERR(pd_cap))
887 		con->port_source_caps = pd_cap;
888 
889 	pd_cap = ucsi_get_pd_caps(con, TYPEC_SINK, false);
890 	if (!IS_ERR(pd_cap))
891 		con->port_sink_caps = pd_cap;
892 
893 	typec_port_set_usb_power_delivery(con->port, con->pd);
894 }
895 
ucsi_register_partner_pdos(struct ucsi_connector * con)896 static int ucsi_register_partner_pdos(struct ucsi_connector *con)
897 {
898 	struct usb_power_delivery_desc desc = { con->ucsi->cap.pd_version };
899 	struct usb_power_delivery_capabilities *cap;
900 
901 	if (con->partner_pd)
902 		return 0;
903 
904 	con->partner_pd = typec_partner_usb_power_delivery_register(con->partner, &desc);
905 	if (IS_ERR(con->partner_pd))
906 		return PTR_ERR(con->partner_pd);
907 
908 	cap = ucsi_get_pd_caps(con, TYPEC_SOURCE, true);
909 	if (IS_ERR(cap))
910 	    return PTR_ERR(cap);
911 
912 	con->partner_source_caps = cap;
913 
914 	cap = ucsi_get_pd_caps(con, TYPEC_SINK, true);
915 	if (IS_ERR(cap))
916 	    return PTR_ERR(cap);
917 
918 	con->partner_sink_caps = cap;
919 
920 	return typec_partner_set_usb_power_delivery(con->partner, con->partner_pd);
921 }
922 
ucsi_unregister_partner_pdos(struct ucsi_connector * con)923 static void ucsi_unregister_partner_pdos(struct ucsi_connector *con)
924 {
925 	usb_power_delivery_unregister_capabilities(con->partner_sink_caps);
926 	con->partner_sink_caps = NULL;
927 	usb_power_delivery_unregister_capabilities(con->partner_source_caps);
928 	con->partner_source_caps = NULL;
929 	usb_power_delivery_unregister(con->partner_pd);
930 	con->partner_pd = NULL;
931 }
932 
ucsi_register_plug(struct ucsi_connector * con)933 static int ucsi_register_plug(struct ucsi_connector *con)
934 {
935 	struct typec_plug *plug;
936 	struct typec_plug_desc desc = {.index = TYPEC_PLUG_SOP_P};
937 
938 	plug = typec_register_plug(con->cable, &desc);
939 	if (IS_ERR(plug)) {
940 		dev_err(con->ucsi->dev,
941 			"con%d: failed to register plug (%ld)\n", con->num,
942 			PTR_ERR(plug));
943 		return PTR_ERR(plug);
944 	}
945 
946 	con->plug = plug;
947 	return 0;
948 }
949 
ucsi_unregister_plug(struct ucsi_connector * con)950 static void ucsi_unregister_plug(struct ucsi_connector *con)
951 {
952 	if (!con->plug)
953 		return;
954 
955 	ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP_P);
956 	typec_unregister_plug(con->plug);
957 	con->plug = NULL;
958 }
959 
ucsi_register_cable(struct ucsi_connector * con)960 static int ucsi_register_cable(struct ucsi_connector *con)
961 {
962 	struct ucsi_cable_property cable_prop;
963 	struct typec_cable *cable;
964 	struct typec_cable_desc desc = {};
965 	u64 command;
966 	int ret;
967 
968 	command = UCSI_GET_CABLE_PROPERTY | UCSI_CONNECTOR_NUMBER(con->num);
969 	con->ucsi->message_in_size = sizeof(cable_prop);
970 	ret = ucsi_send_command(con->ucsi, command);
971 	memcpy(&cable_prop, con->ucsi->message_in, sizeof(cable_prop));
972 	if (ret < 0) {
973 		dev_err(con->ucsi->dev, "GET_CABLE_PROPERTY failed (%d)\n", ret);
974 		return ret;
975 	}
976 
977 	switch (UCSI_CABLE_PROP_FLAG_PLUG_TYPE(cable_prop.flags)) {
978 	case UCSI_CABLE_PROPERTY_PLUG_TYPE_A:
979 		desc.type = USB_PLUG_TYPE_A;
980 		break;
981 	case UCSI_CABLE_PROPERTY_PLUG_TYPE_B:
982 		desc.type = USB_PLUG_TYPE_B;
983 		break;
984 	case UCSI_CABLE_PROPERTY_PLUG_TYPE_C:
985 		desc.type = USB_PLUG_TYPE_C;
986 		break;
987 	default:
988 		desc.type = USB_PLUG_NONE;
989 		break;
990 	}
991 
992 	if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
993 		desc.identity = &con->cable_identity;
994 	desc.active = !!(UCSI_CABLE_PROP_FLAG_ACTIVE_CABLE & cable_prop.flags);
995 
996 	if (con->ucsi->version >= UCSI_VERSION_2_1)
997 		desc.pd_revision = UCSI_CABLE_PROP_FLAG_PD_MAJOR_REV_AS_BCD(cable_prop.flags);
998 
999 	cable = typec_register_cable(con->port, &desc);
1000 	if (IS_ERR(cable)) {
1001 		dev_err(con->ucsi->dev,
1002 			"con%d: failed to register cable (%ld)\n", con->num,
1003 			PTR_ERR(cable));
1004 		return PTR_ERR(cable);
1005 	}
1006 
1007 	con->cable = cable;
1008 	return 0;
1009 }
1010 
ucsi_unregister_cable(struct ucsi_connector * con)1011 static void ucsi_unregister_cable(struct ucsi_connector *con)
1012 {
1013 	if (!con->cable)
1014 		return;
1015 
1016 	ucsi_unregister_plug(con);
1017 	typec_unregister_cable(con->cable);
1018 	memset(&con->cable_identity, 0, sizeof(con->cable_identity));
1019 	con->cable = NULL;
1020 }
1021 
ucsi_check_connector_capability(struct ucsi_connector * con)1022 static int ucsi_check_connector_capability(struct ucsi_connector *con)
1023 {
1024 	u64 pd_revision;
1025 	u64 command;
1026 	int ret;
1027 
1028 	if (!con->partner || con->ucsi->version < UCSI_VERSION_2_1)
1029 		return 0;
1030 
1031 	command = UCSI_GET_CONNECTOR_CAPABILITY | UCSI_CONNECTOR_NUMBER(con->num);
1032 	con->ucsi->message_in_size = sizeof(con->cap);
1033 	ret = ucsi_send_command(con->ucsi, command);
1034 	memcpy(&con->cap, con->ucsi->message_in, sizeof(con->cap));
1035 	if (ret < 0) {
1036 		dev_err(con->ucsi->dev, "GET_CONNECTOR_CAPABILITY failed (%d)\n", ret);
1037 		return ret;
1038 	}
1039 
1040 	pd_revision = UCSI_CONCAP(con, PARTNER_PD_REVISION_V2_1);
1041 	typec_partner_set_pd_revision(con->partner, UCSI_SPEC_REVISION_TO_BCD(pd_revision));
1042 
1043 	return ret;
1044 }
1045 
ucsi_orientation(struct ucsi_connector * con)1046 static void ucsi_orientation(struct ucsi_connector *con)
1047 {
1048 	if (con->ucsi->version < UCSI_VERSION_2_0)
1049 		return;
1050 
1051 	if (!UCSI_CONSTAT(con, CONNECTED)) {
1052 		typec_set_orientation(con->port, TYPEC_ORIENTATION_NONE);
1053 		return;
1054 	}
1055 
1056 	switch (UCSI_CONSTAT(con, ORIENTATION)) {
1057 	case UCSI_CONSTAT_ORIENTATION_NORMAL:
1058 		typec_set_orientation(con->port, TYPEC_ORIENTATION_NORMAL);
1059 		break;
1060 	case UCSI_CONSTAT_ORIENTATION_REVERSE:
1061 		typec_set_orientation(con->port, TYPEC_ORIENTATION_REVERSE);
1062 		break;
1063 	default:
1064 		break;
1065 	}
1066 }
1067 
ucsi_pwr_opmode_change(struct ucsi_connector * con)1068 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
1069 {
1070 	switch (UCSI_CONSTAT(con, PWR_OPMODE)) {
1071 	case UCSI_CONSTAT_PWR_OPMODE_PD:
1072 		con->rdo = UCSI_CONSTAT(con, RDO);
1073 		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
1074 		ucsi_partner_task(con, ucsi_get_src_pdos, 30, 0);
1075 		ucsi_partner_task(con, ucsi_check_altmodes, 30, HZ);
1076 		ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ);
1077 		ucsi_partner_task(con, ucsi_check_connector_capability, 1, HZ);
1078 		break;
1079 	case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
1080 		con->rdo = 0;
1081 		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
1082 		ucsi_port_psy_changed(con);
1083 		break;
1084 	case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
1085 		con->rdo = 0;
1086 		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
1087 		ucsi_port_psy_changed(con);
1088 		break;
1089 	default:
1090 		con->rdo = 0;
1091 		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
1092 		ucsi_port_psy_changed(con);
1093 		break;
1094 	}
1095 }
1096 
ucsi_register_partner(struct ucsi_connector * con)1097 static int ucsi_register_partner(struct ucsi_connector *con)
1098 {
1099 	u8 pwr_opmode = UCSI_CONSTAT(con, PWR_OPMODE);
1100 	struct typec_partner_desc desc;
1101 	struct typec_partner *partner;
1102 
1103 	if (con->partner)
1104 		return 0;
1105 
1106 	memset(&desc, 0, sizeof(desc));
1107 
1108 	switch (UCSI_CONSTAT(con, PARTNER_TYPE)) {
1109 	case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
1110 		desc.accessory = TYPEC_ACCESSORY_DEBUG;
1111 		break;
1112 	case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
1113 		desc.accessory = TYPEC_ACCESSORY_AUDIO;
1114 		break;
1115 	default:
1116 		break;
1117 	}
1118 
1119 	if (pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD)
1120 		ucsi_register_device_pdos(con);
1121 
1122 	if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
1123 		desc.identity = &con->partner_identity;
1124 	desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD;
1125 
1126 	if (con->ucsi->version >= UCSI_VERSION_2_1) {
1127 		u64 pd_revision = UCSI_CONCAP(con, PARTNER_PD_REVISION_V2_1);
1128 		desc.pd_revision = UCSI_SPEC_REVISION_TO_BCD(pd_revision);
1129 	}
1130 
1131 	partner = typec_register_partner(con->port, &desc);
1132 	if (IS_ERR(partner)) {
1133 		dev_err(con->ucsi->dev,
1134 			"con%d: failed to register partner (%ld)\n", con->num,
1135 			PTR_ERR(partner));
1136 		return PTR_ERR(partner);
1137 	}
1138 
1139 	con->partner = partner;
1140 
1141 	if (con->ucsi->version >= UCSI_VERSION_3_0 &&
1142 	    UCSI_CONSTAT(con, PARTNER_FLAG_USB4_GEN4))
1143 		typec_partner_set_usb_mode(partner, USB_MODE_USB4);
1144 	else if (con->ucsi->version >= UCSI_VERSION_2_0 &&
1145 		 UCSI_CONSTAT(con, PARTNER_FLAG_USB4_GEN3))
1146 		typec_partner_set_usb_mode(partner, USB_MODE_USB4);
1147 
1148 	return 0;
1149 }
1150 
ucsi_unregister_partner(struct ucsi_connector * con)1151 static void ucsi_unregister_partner(struct ucsi_connector *con)
1152 {
1153 	if (!con->partner)
1154 		return;
1155 
1156 	typec_set_mode(con->port, TYPEC_STATE_SAFE);
1157 
1158 	typec_partner_set_usb_power_delivery(con->partner, NULL);
1159 	ucsi_unregister_partner_pdos(con);
1160 	ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
1161 	ucsi_unregister_cable(con);
1162 	typec_unregister_partner(con->partner);
1163 	memset(&con->partner_identity, 0, sizeof(con->partner_identity));
1164 	con->partner = NULL;
1165 }
1166 
ucsi_partner_change(struct ucsi_connector * con)1167 static void ucsi_partner_change(struct ucsi_connector *con)
1168 {
1169 	enum usb_role u_role = USB_ROLE_NONE;
1170 	int ret;
1171 
1172 	switch (UCSI_CONSTAT(con, PARTNER_TYPE)) {
1173 	case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1174 	case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1175 		u_role = USB_ROLE_HOST;
1176 		fallthrough;
1177 	case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1178 		typec_set_data_role(con->port, TYPEC_HOST);
1179 		break;
1180 	case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1181 		u_role = USB_ROLE_DEVICE;
1182 		typec_set_data_role(con->port, TYPEC_DEVICE);
1183 		break;
1184 	default:
1185 		break;
1186 	}
1187 
1188 	if (UCSI_CONSTAT(con, CONNECTED)) {
1189 		switch (UCSI_CONSTAT(con, PARTNER_TYPE)) {
1190 		case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
1191 			typec_set_mode(con->port, TYPEC_MODE_DEBUG);
1192 			break;
1193 		case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
1194 			typec_set_mode(con->port, TYPEC_MODE_AUDIO);
1195 			break;
1196 		default:
1197 			if (UCSI_CONSTAT(con, PARTNER_FLAG_USB))
1198 				typec_set_mode(con->port, TYPEC_STATE_USB);
1199 		}
1200 	}
1201 
1202 	/* Only notify USB controller if partner supports USB data */
1203 	if (!(UCSI_CONSTAT(con, PARTNER_FLAG_USB)))
1204 		u_role = USB_ROLE_NONE;
1205 
1206 	ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1207 	if (ret)
1208 		dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n",
1209 			con->num, u_role);
1210 }
1211 
ucsi_check_connection(struct ucsi_connector * con)1212 static int ucsi_check_connection(struct ucsi_connector *con)
1213 {
1214 	u8 prev_state = UCSI_CONSTAT(con, CONNECTED);
1215 	int ret;
1216 
1217 	ret = ucsi_get_connector_status(con, false);
1218 	if (ret) {
1219 		dev_err(con->ucsi->dev, "GET_CONNECTOR_STATUS failed (%d)\n", ret);
1220 		return ret;
1221 	}
1222 
1223 	if (UCSI_CONSTAT(con, CONNECTED)) {
1224 		if (prev_state)
1225 			return 0;
1226 		ucsi_register_partner(con);
1227 		ucsi_pwr_opmode_change(con);
1228 		ucsi_partner_change(con);
1229 	} else {
1230 		ucsi_partner_change(con);
1231 		ucsi_port_psy_changed(con);
1232 		ucsi_unregister_partner(con);
1233 	}
1234 
1235 	return 0;
1236 }
1237 
ucsi_check_cable(struct ucsi_connector * con)1238 static int ucsi_check_cable(struct ucsi_connector *con)
1239 {
1240 	int ret, num_plug_am;
1241 
1242 	if (con->cable)
1243 		return 0;
1244 
1245 	ret = ucsi_register_cable(con);
1246 	if (ret < 0)
1247 		return ret;
1248 
1249 	if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE) {
1250 		ret = ucsi_get_cable_identity(con);
1251 		if (ret < 0)
1252 			return ret;
1253 	}
1254 
1255 	if (con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS) {
1256 		ret = ucsi_register_plug(con);
1257 		if (ret < 0)
1258 			return ret;
1259 
1260 		ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP_P);
1261 		if (ret < 0)
1262 			return ret;
1263 
1264 		if (con->plug_altmode[0]) {
1265 			num_plug_am = ucsi_get_num_altmode(con->plug_altmode);
1266 			typec_plug_set_num_altmodes(con->plug, num_plug_am);
1267 		} else {
1268 			typec_plug_set_num_altmodes(con->plug, 0);
1269 		}
1270 	}
1271 
1272 	return 0;
1273 }
1274 
ucsi_handle_connector_change(struct work_struct * work)1275 static void ucsi_handle_connector_change(struct work_struct *work)
1276 {
1277 	struct ucsi_connector *con = container_of(work, struct ucsi_connector,
1278 						  work);
1279 	struct ucsi *ucsi = con->ucsi;
1280 	u8 curr_scale, volt_scale;
1281 	enum typec_role role;
1282 	u16 change;
1283 	int ret;
1284 	u32 val;
1285 
1286 	mutex_lock(&con->lock);
1287 
1288 	if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
1289 		dev_err_once(ucsi->dev, "%s entered without EVENT_PENDING\n",
1290 			     __func__);
1291 
1292 	ret = ucsi_get_connector_status(con, true);
1293 	if (ret) {
1294 		dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
1295 			__func__, ret);
1296 		clear_bit(EVENT_PENDING, &con->ucsi->flags);
1297 		goto out_unlock;
1298 	}
1299 
1300 	trace_ucsi_connector_change(con->num, con);
1301 
1302 	if (ucsi->ops->connector_status)
1303 		ucsi->ops->connector_status(con);
1304 
1305 	change = UCSI_CONSTAT(con, CHANGE);
1306 	role = UCSI_CONSTAT(con, PWR_DIR);
1307 
1308 	if (change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
1309 		typec_set_pwr_role(con->port, role);
1310 		ucsi_port_psy_changed(con);
1311 
1312 		/* Complete pending power role swap */
1313 		if (!completion_done(&con->complete))
1314 			complete(&con->complete);
1315 	}
1316 
1317 	if (change & UCSI_CONSTAT_CONNECT_CHANGE) {
1318 		typec_set_pwr_role(con->port, role);
1319 		ucsi_port_psy_changed(con);
1320 		ucsi_partner_change(con);
1321 		ucsi_orientation(con);
1322 
1323 		if (UCSI_CONSTAT(con, CONNECTED)) {
1324 			ucsi_register_partner(con);
1325 			ucsi_partner_task(con, ucsi_check_connection, 1, HZ);
1326 			if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
1327 				ucsi_partner_task(con, ucsi_get_partner_identity, 1, HZ);
1328 			if (con->ucsi->cap.features & UCSI_CAP_CABLE_DETAILS)
1329 				ucsi_partner_task(con, ucsi_check_cable, 1, HZ);
1330 
1331 			if (UCSI_CONSTAT(con, PWR_OPMODE) == UCSI_CONSTAT_PWR_OPMODE_PD) {
1332 				ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ);
1333 				ucsi_partner_task(con, ucsi_check_connector_capability, 1, HZ);
1334 			}
1335 		} else {
1336 			ucsi_unregister_partner(con);
1337 		}
1338 	}
1339 
1340 	if (change & (UCSI_CONSTAT_POWER_OPMODE_CHANGE | UCSI_CONSTAT_POWER_LEVEL_CHANGE))
1341 		ucsi_pwr_opmode_change(con);
1342 
1343 	if (con->partner && (change & UCSI_CONSTAT_PARTNER_CHANGE)) {
1344 		ucsi_partner_change(con);
1345 
1346 		/* Complete pending data role swap */
1347 		if (!completion_done(&con->complete))
1348 			complete(&con->complete);
1349 	}
1350 
1351 	if (change & UCSI_CONSTAT_CAM_CHANGE)
1352 		ucsi_partner_task(con, ucsi_check_altmodes, 1, HZ);
1353 
1354 	if (change & (UCSI_CONSTAT_BC_CHANGE | UCSI_CONSTAT_SINK_PATH_CHANGE))
1355 		ucsi_port_psy_changed(con);
1356 
1357 	if (con->ucsi->version >= UCSI_VERSION_2_1 &&
1358 	    UCSI_CONSTAT(con, PWR_READING_READY_V2_1)) {
1359 		curr_scale = UCSI_CONSTAT(con, CURRENT_SCALE_V2_1);
1360 		volt_scale = UCSI_CONSTAT(con, VOLTAGE_SCALE_V2_1);
1361 
1362 		val = UCSI_CONSTAT(con, PEAK_CURRENT_V2_1);
1363 		con->peak_current = UCSI_CONSTAT_CURR_SCALE_MULT * curr_scale * val;
1364 
1365 		val = UCSI_CONSTAT(con, AVG_CURRENT_V2_1);
1366 		con->avg_current = UCSI_CONSTAT_CURR_SCALE_MULT * curr_scale * val;
1367 
1368 		val = UCSI_CONSTAT(con, VBUS_VOLTAGE_V2_1);
1369 		con->vbus_voltage = UCSI_CONSTAT_VOLT_SCALE_MULT * volt_scale * val;
1370 	}
1371 
1372 out_unlock:
1373 	mutex_unlock(&con->lock);
1374 }
1375 
1376 /**
1377  * ucsi_connector_change - Process Connector Change Event
1378  * @ucsi: UCSI Interface
1379  * @num: Connector number
1380  */
ucsi_connector_change(struct ucsi * ucsi,u8 num)1381 void ucsi_connector_change(struct ucsi *ucsi, u8 num)
1382 {
1383 	struct ucsi_connector *con = &ucsi->connector[num - 1];
1384 
1385 	if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
1386 		dev_dbg(ucsi->dev, "Early connector change event\n");
1387 		return;
1388 	}
1389 
1390 	if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
1391 		schedule_work(&con->work);
1392 }
1393 EXPORT_SYMBOL_GPL(ucsi_connector_change);
1394 
1395 /* -------------------------------------------------------------------------- */
1396 
1397 /*
1398  * Hard Reset bit field was defined with value 1 in UCSI spec version 1.0.
1399  * Starting with spec version 1.1, Hard Reset bit field was removed from the
1400  * CONNECTOR_RESET command, until spec 2.0 reintroduced it with value 0, so, in effect,
1401  * the value to pass in to the command for a Hard Reset is different depending
1402  * on the supported UCSI version by the LPM.
1403  *
1404  * For performing a Data Reset on LPMs supporting version 2.0 and greater,
1405  * this function needs to be called with the second argument set to 0.
1406  */
ucsi_reset_connector(struct ucsi_connector * con,bool hard)1407 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
1408 {
1409 	u64 command;
1410 
1411 	command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num);
1412 
1413 	if (con->ucsi->version < UCSI_VERSION_1_1)
1414 		command |= hard ? UCSI_CONNECTOR_RESET_HARD_VER_1_0 : 0;
1415 	else if (con->ucsi->version >= UCSI_VERSION_2_0)
1416 		command |= hard ? 0 : UCSI_CONNECTOR_RESET_DATA_VER_2_0;
1417 
1418 	con->ucsi->message_in_size = 0;
1419 	return ucsi_send_command(con->ucsi, command);
1420 }
1421 
ucsi_reset_ppm(struct ucsi * ucsi)1422 static int ucsi_reset_ppm(struct ucsi *ucsi)
1423 {
1424 	u64 command;
1425 	unsigned long tmo;
1426 	u32 cci;
1427 	int ret;
1428 
1429 	mutex_lock(&ucsi->ppm_lock);
1430 
1431 	ret = ucsi->ops->poll_cci(ucsi, &cci);
1432 	if (ret < 0)
1433 		goto out;
1434 
1435 	/*
1436 	 * If UCSI_CCI_RESET_COMPLETE is already set we must clear
1437 	 * the flag before we start another reset. Send a
1438 	 * UCSI_SET_NOTIFICATION_ENABLE command to achieve this.
1439 	 * Ignore a timeout and try the reset anyway if this fails.
1440 	 */
1441 	if (cci & UCSI_CCI_RESET_COMPLETE) {
1442 		command = UCSI_SET_NOTIFICATION_ENABLE;
1443 		ret = ucsi->ops->async_control(ucsi, command);
1444 		if (ret < 0)
1445 			goto out;
1446 
1447 		tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
1448 		do {
1449 			ret = ucsi->ops->poll_cci(ucsi, &cci);
1450 			if (ret < 0)
1451 				goto out;
1452 			if (cci & UCSI_CCI_COMMAND_COMPLETE)
1453 				break;
1454 			if (time_is_before_jiffies(tmo))
1455 				break;
1456 			msleep(20);
1457 		} while (1);
1458 
1459 		WARN_ON(cci & UCSI_CCI_RESET_COMPLETE);
1460 	}
1461 
1462 	command = UCSI_PPM_RESET;
1463 	ret = ucsi->ops->async_control(ucsi, command);
1464 	if (ret < 0)
1465 		goto out;
1466 
1467 	tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
1468 
1469 	do {
1470 		if (time_is_before_jiffies(tmo)) {
1471 			ret = -ETIMEDOUT;
1472 			goto out;
1473 		}
1474 
1475 		/* Give the PPM time to process a reset before reading CCI */
1476 		msleep(20);
1477 
1478 		ret = ucsi->ops->poll_cci(ucsi, &cci);
1479 		if (ret)
1480 			goto out;
1481 
1482 		/* If the PPM is still doing something else, reset it again. */
1483 		if (cci & ~UCSI_CCI_RESET_COMPLETE) {
1484 			ret = ucsi->ops->async_control(ucsi, command);
1485 			if (ret < 0)
1486 				goto out;
1487 		}
1488 
1489 	} while (!(cci & UCSI_CCI_RESET_COMPLETE));
1490 
1491 out:
1492 	mutex_unlock(&ucsi->ppm_lock);
1493 	return ret;
1494 }
1495 
ucsi_role_cmd(struct ucsi_connector * con,u64 command)1496 static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)
1497 {
1498 	int ret;
1499 
1500 	con->ucsi->message_in_size = 0;
1501 	ret = ucsi_send_command(con->ucsi, command);
1502 	if (ret == -ETIMEDOUT) {
1503 		u64 c;
1504 
1505 		/* PPM most likely stopped responding. Resetting everything. */
1506 		ucsi_reset_ppm(con->ucsi);
1507 
1508 		c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
1509 		con->ucsi->message_in_size = 0;
1510 		ucsi_send_command(con->ucsi, c);
1511 
1512 		ucsi_reset_connector(con, true);
1513 	}
1514 
1515 	return ret;
1516 }
1517 
ucsi_dr_swap(struct typec_port * port,enum typec_data_role role)1518 static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
1519 {
1520 	struct ucsi_connector *con = typec_get_drvdata(port);
1521 	u8 partner_type;
1522 	u64 command;
1523 	int ret = 0;
1524 
1525 	mutex_lock(&con->lock);
1526 
1527 	if (!con->partner) {
1528 		ret = -ENOTCONN;
1529 		goto out_unlock;
1530 	}
1531 
1532 	partner_type = UCSI_CONSTAT(con, PARTNER_TYPE);
1533 	if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
1534 	     role == TYPEC_DEVICE) ||
1535 	    (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
1536 	     role == TYPEC_HOST))
1537 		goto out_unlock;
1538 
1539 	reinit_completion(&con->complete);
1540 
1541 	command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num);
1542 	command |= UCSI_SET_UOR_ROLE(role);
1543 	command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS;
1544 	ret = ucsi_role_cmd(con, command);
1545 	if (ret < 0)
1546 		goto out_unlock;
1547 
1548 	mutex_unlock(&con->lock);
1549 
1550 	if (!wait_for_completion_timeout(&con->complete,
1551 					 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1552 		return -ETIMEDOUT;
1553 
1554 	return 0;
1555 
1556 out_unlock:
1557 	mutex_unlock(&con->lock);
1558 
1559 	return ret;
1560 }
1561 
ucsi_pr_swap(struct typec_port * port,enum typec_role role)1562 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
1563 {
1564 	struct ucsi_connector *con = typec_get_drvdata(port);
1565 	enum typec_role cur_role;
1566 	u64 command;
1567 	int ret = 0;
1568 
1569 	mutex_lock(&con->lock);
1570 
1571 	if (!con->partner) {
1572 		ret = -ENOTCONN;
1573 		goto out_unlock;
1574 	}
1575 
1576 	cur_role = UCSI_CONSTAT(con, PWR_DIR);
1577 
1578 	if (cur_role == role)
1579 		goto out_unlock;
1580 
1581 	reinit_completion(&con->complete);
1582 
1583 	command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num);
1584 	command |= UCSI_SET_PDR_ROLE(role);
1585 	command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS;
1586 	ret = ucsi_role_cmd(con, command);
1587 	if (ret < 0)
1588 		goto out_unlock;
1589 
1590 	mutex_unlock(&con->lock);
1591 
1592 	if (!wait_for_completion_timeout(&con->complete,
1593 					 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1594 		return -ETIMEDOUT;
1595 
1596 	mutex_lock(&con->lock);
1597 
1598 	/* Something has gone wrong while swapping the role */
1599 	if (UCSI_CONSTAT(con, PWR_OPMODE) != UCSI_CONSTAT_PWR_OPMODE_PD) {
1600 		ucsi_reset_connector(con, true);
1601 		ret = -EPROTO;
1602 	}
1603 
1604 out_unlock:
1605 	mutex_unlock(&con->lock);
1606 
1607 	return ret;
1608 }
1609 
1610 static const struct typec_operations ucsi_ops = {
1611 	.dr_set = ucsi_dr_swap,
1612 	.pr_set = ucsi_pr_swap
1613 };
1614 
1615 /* Caller must call fwnode_handle_put() after use */
ucsi_find_fwnode(struct ucsi_connector * con)1616 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
1617 {
1618 	struct fwnode_handle *fwnode;
1619 	int i = 1;
1620 
1621 	device_for_each_child_node(con->ucsi->dev, fwnode)
1622 		if (i++ == con->num)
1623 			return fwnode;
1624 	return NULL;
1625 }
1626 
ucsi_register_port(struct ucsi * ucsi,struct ucsi_connector * con)1627 static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con)
1628 {
1629 	struct typec_capability *cap = &con->typec_cap;
1630 	enum typec_accessory *accessory = cap->accessory;
1631 	enum usb_role u_role = USB_ROLE_NONE;
1632 	u64 command;
1633 	char *name;
1634 	int ret;
1635 
1636 	name = kasprintf(GFP_KERNEL, "%s-con%d", dev_name(ucsi->dev), con->num);
1637 	if (!name)
1638 		return -ENOMEM;
1639 
1640 	con->wq = create_singlethread_workqueue(name);
1641 	kfree(name);
1642 	if (!con->wq)
1643 		return -ENOMEM;
1644 
1645 	INIT_WORK(&con->work, ucsi_handle_connector_change);
1646 	init_completion(&con->complete);
1647 	mutex_init(&con->lock);
1648 	INIT_LIST_HEAD(&con->partner_tasks);
1649 	con->ucsi = ucsi;
1650 
1651 	cap->fwnode = ucsi_find_fwnode(con);
1652 	con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode);
1653 	if (IS_ERR(con->usb_role_sw))
1654 		return dev_err_probe(ucsi->dev, PTR_ERR(con->usb_role_sw),
1655 			"con%d: failed to get usb role switch\n", con->num);
1656 
1657 	/* Delay other interactions with the con until registration is complete */
1658 	mutex_lock(&con->lock);
1659 
1660 	/* Get connector capability */
1661 	command = UCSI_GET_CONNECTOR_CAPABILITY;
1662 	command |= UCSI_CONNECTOR_NUMBER(con->num);
1663 	ucsi->message_in_size = sizeof(con->cap);
1664 	ret = ucsi_send_command(ucsi, command);
1665 	if (ret < 0)
1666 		goto out_unlock;
1667 
1668 	memcpy(&con->cap, ucsi->message_in, sizeof(con->cap));
1669 
1670 	if (UCSI_CONCAP(con, OPMODE_DRP))
1671 		cap->data = TYPEC_PORT_DRD;
1672 	else if (UCSI_CONCAP(con, OPMODE_DFP))
1673 		cap->data = TYPEC_PORT_DFP;
1674 	else if (UCSI_CONCAP(con, OPMODE_UFP))
1675 		cap->data = TYPEC_PORT_UFP;
1676 
1677 	if (UCSI_CONCAP(con, PROVIDER) && UCSI_CONCAP(con, CONSUMER))
1678 		cap->type = TYPEC_PORT_DRP;
1679 	else if (UCSI_CONCAP(con, PROVIDER))
1680 		cap->type = TYPEC_PORT_SRC;
1681 	else if (UCSI_CONCAP(con, CONSUMER))
1682 		cap->type = TYPEC_PORT_SNK;
1683 
1684 	cap->revision = ucsi->cap.typec_version;
1685 	cap->pd_revision = ucsi->cap.pd_version;
1686 	cap->svdm_version = SVDM_VER_2_0;
1687 	cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
1688 
1689 	if (UCSI_CONCAP(con, OPMODE_AUDIO_ACCESSORY))
1690 		*accessory++ = TYPEC_ACCESSORY_AUDIO;
1691 	if (UCSI_CONCAP(con, OPMODE_DEBUG_ACCESSORY))
1692 		*accessory = TYPEC_ACCESSORY_DEBUG;
1693 
1694 	if (UCSI_CONCAP_USB2_SUPPORT(con))
1695 		cap->usb_capability |= USB_CAPABILITY_USB2;
1696 	if (UCSI_CONCAP_USB3_SUPPORT(con))
1697 		cap->usb_capability |= USB_CAPABILITY_USB3;
1698 	if (UCSI_CONCAP_USB4_SUPPORT(con))
1699 		cap->usb_capability |= USB_CAPABILITY_USB4;
1700 
1701 	cap->driver_data = con;
1702 	cap->ops = &ucsi_ops;
1703 
1704 	if (ucsi->version >= UCSI_VERSION_2_0)
1705 		con->typec_cap.orientation_aware = true;
1706 
1707 	if (ucsi->ops->update_connector)
1708 		ucsi->ops->update_connector(con);
1709 
1710 	ret = ucsi_register_port_psy(con);
1711 	if (ret)
1712 		goto out;
1713 
1714 	/* Register the connector */
1715 	con->port = typec_register_port(ucsi->dev, cap);
1716 	if (IS_ERR(con->port)) {
1717 		ret = PTR_ERR(con->port);
1718 		goto out;
1719 	}
1720 
1721 	if (!(ucsi->quirks & UCSI_DELAY_DEVICE_PDOS))
1722 		ucsi_register_device_pdos(con);
1723 
1724 	/* Alternate modes */
1725 	ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON);
1726 	if (ret) {
1727 		dev_err(ucsi->dev, "con%d: failed to register alt modes\n",
1728 			con->num);
1729 		goto out;
1730 	}
1731 
1732 	/* Get the status */
1733 	ret = ucsi_get_connector_status(con, false);
1734 	if (ret) {
1735 		dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
1736 		goto out;
1737 	}
1738 
1739 	if (ucsi->ops->connector_status)
1740 		ucsi->ops->connector_status(con);
1741 
1742 	switch (UCSI_CONSTAT(con, PARTNER_TYPE)) {
1743 	case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1744 	case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1745 		u_role = USB_ROLE_HOST;
1746 		fallthrough;
1747 	case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1748 		typec_set_data_role(con->port, TYPEC_HOST);
1749 		break;
1750 	case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1751 		u_role = USB_ROLE_DEVICE;
1752 		typec_set_data_role(con->port, TYPEC_DEVICE);
1753 		break;
1754 	default:
1755 		break;
1756 	}
1757 
1758 	/* Check if there is already something connected */
1759 	if (UCSI_CONSTAT(con, CONNECTED)) {
1760 		typec_set_pwr_role(con->port, UCSI_CONSTAT(con, PWR_DIR));
1761 		ucsi_register_partner(con);
1762 		ucsi_pwr_opmode_change(con);
1763 		ucsi_orientation(con);
1764 		ucsi_port_psy_changed(con);
1765 		if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
1766 			ucsi_get_partner_identity(con);
1767 		if (con->ucsi->cap.features & UCSI_CAP_CABLE_DETAILS)
1768 			ucsi_check_cable(con);
1769 	}
1770 
1771 	/* Only notify USB controller if partner supports USB data */
1772 	if (!(UCSI_CONSTAT(con, PARTNER_FLAG_USB)))
1773 		u_role = USB_ROLE_NONE;
1774 
1775 	ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1776 	if (ret) {
1777 		dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n",
1778 			con->num, u_role);
1779 		ret = 0;
1780 	}
1781 
1782 	if (con->partner && UCSI_CONSTAT(con, PWR_OPMODE) == UCSI_CONSTAT_PWR_OPMODE_PD) {
1783 		ucsi_register_device_pdos(con);
1784 		ucsi_get_src_pdos(con);
1785 		ucsi_check_altmodes(con);
1786 		ucsi_check_connector_capability(con);
1787 	}
1788 
1789 	trace_ucsi_register_port(con->num, con);
1790 
1791 out:
1792 	fwnode_handle_put(cap->fwnode);
1793 out_unlock:
1794 	mutex_unlock(&con->lock);
1795 
1796 	if (ret && con->wq) {
1797 		destroy_workqueue(con->wq);
1798 		con->wq = NULL;
1799 	}
1800 
1801 	return ret;
1802 }
1803 
ucsi_get_supported_notifications(struct ucsi * ucsi)1804 static u64 ucsi_get_supported_notifications(struct ucsi *ucsi)
1805 {
1806 	u16 features = ucsi->cap.features;
1807 	u64 ntfy = UCSI_ENABLE_NTFY_ALL;
1808 
1809 	if (!(features & UCSI_CAP_ALT_MODE_DETAILS))
1810 		ntfy &= ~UCSI_ENABLE_NTFY_CAM_CHANGE;
1811 
1812 	if (!(features & UCSI_CAP_PDO_DETAILS))
1813 		ntfy &= ~(UCSI_ENABLE_NTFY_PWR_LEVEL_CHANGE |
1814 			  UCSI_ENABLE_NTFY_CAP_CHANGE);
1815 
1816 	if (!(features & UCSI_CAP_EXT_SUPPLY_NOTIFICATIONS))
1817 		ntfy &= ~UCSI_ENABLE_NTFY_EXT_PWR_SRC_CHANGE;
1818 
1819 	if (!(features & UCSI_CAP_PD_RESET))
1820 		ntfy &= ~UCSI_ENABLE_NTFY_PD_RESET_COMPLETE;
1821 
1822 	if (ucsi->version <= UCSI_VERSION_1_2)
1823 		return ntfy;
1824 
1825 	ntfy |= UCSI_ENABLE_NTFY_SINK_PATH_STS_CHANGE;
1826 
1827 	if (features & UCSI_CAP_GET_ATTENTION_VDO)
1828 		ntfy |= UCSI_ENABLE_NTFY_ATTENTION;
1829 
1830 	if (features & UCSI_CAP_FW_UPDATE_REQUEST)
1831 		ntfy |= UCSI_ENABLE_NTFY_LPM_FW_UPDATE_REQ;
1832 
1833 	if (features & UCSI_CAP_SECURITY_REQUEST)
1834 		ntfy |= UCSI_ENABLE_NTFY_SECURITY_REQ_PARTNER;
1835 
1836 	if (features & UCSI_CAP_SET_RETIMER_MODE)
1837 		ntfy |= UCSI_ENABLE_NTFY_SET_RETIMER_MODE;
1838 
1839 	return ntfy;
1840 }
1841 
1842 /**
1843  * ucsi_init - Initialize UCSI interface
1844  * @ucsi: UCSI to be initialized
1845  *
1846  * Registers all ports @ucsi has and enables all notification events.
1847  */
ucsi_init(struct ucsi * ucsi)1848 static int ucsi_init(struct ucsi *ucsi)
1849 {
1850 	struct ucsi_connector *con, *connector;
1851 	u64 command, ntfy;
1852 	u32 cci;
1853 	int ret;
1854 	int i;
1855 
1856 	/* Reset the PPM */
1857 	ret = ucsi_reset_ppm(ucsi);
1858 	if (ret) {
1859 		dev_err(ucsi->dev, "failed to reset PPM!\n");
1860 		goto err;
1861 	}
1862 
1863 	/* Enable basic notifications */
1864 	ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
1865 	command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1866 	ucsi->message_in_size = 0;
1867 	ret = ucsi_send_command(ucsi, command);
1868 	if (ret < 0)
1869 		goto err_reset;
1870 
1871 	/* Get PPM capabilities */
1872 	command = UCSI_GET_CAPABILITY;
1873 	ucsi->message_in_size = BITS_TO_BYTES(UCSI_GET_CAPABILITY_SIZE);
1874 	ret = ucsi_send_command(ucsi, command);
1875 	if (ret < 0)
1876 		goto err_reset;
1877 
1878 	memcpy(&ucsi->cap, ucsi->message_in, BITS_TO_BYTES(UCSI_GET_CAPABILITY_SIZE));
1879 
1880 	if (!ucsi->cap.num_connectors) {
1881 		ret = -ENODEV;
1882 		goto err_reset;
1883 	}
1884 	/* Check if reserved bit set. This is out of spec but happens in buggy FW */
1885 	if (ucsi->cap.num_connectors & 0x80) {
1886 		dev_warn(ucsi->dev, "UCSI: Invalid num_connectors %d. Likely buggy FW\n",
1887 			 ucsi->cap.num_connectors);
1888 		ucsi->cap.num_connectors &= 0x7f; // clear bit and carry on
1889 	}
1890 
1891 	/* Allocate the connectors. Released in ucsi_unregister() */
1892 	connector = kcalloc(ucsi->cap.num_connectors + 1, sizeof(*connector), GFP_KERNEL);
1893 	if (!connector) {
1894 		ret = -ENOMEM;
1895 		goto err_reset;
1896 	}
1897 
1898 	/* Register all connectors */
1899 	for (i = 0; i < ucsi->cap.num_connectors; i++) {
1900 		connector[i].num = i + 1;
1901 		ret = ucsi_register_port(ucsi, &connector[i]);
1902 		if (ret)
1903 			goto err_unregister;
1904 	}
1905 
1906 	/* Enable all supported notifications */
1907 	ntfy = ucsi_get_supported_notifications(ucsi);
1908 	command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1909 	ucsi->message_in_size = 0;
1910 	ret = ucsi_send_command(ucsi, command);
1911 	if (ret < 0)
1912 		goto err_unregister;
1913 
1914 	ucsi->connector = connector;
1915 	ucsi->ntfy = ntfy;
1916 
1917 	mutex_lock(&ucsi->ppm_lock);
1918 	ret = ucsi->ops->read_cci(ucsi, &cci);
1919 	mutex_unlock(&ucsi->ppm_lock);
1920 	if (ret)
1921 		return ret;
1922 	if (UCSI_CCI_CONNECTOR(cci))
1923 		ucsi_connector_change(ucsi, UCSI_CCI_CONNECTOR(cci));
1924 
1925 	return 0;
1926 
1927 err_unregister:
1928 	for (con = connector; con->port; con++) {
1929 		if (con->wq)
1930 			destroy_workqueue(con->wq);
1931 		ucsi_unregister_partner(con);
1932 		ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON);
1933 		ucsi_unregister_port_psy(con);
1934 
1935 		usb_power_delivery_unregister_capabilities(con->port_sink_caps);
1936 		con->port_sink_caps = NULL;
1937 		usb_power_delivery_unregister_capabilities(con->port_source_caps);
1938 		con->port_source_caps = NULL;
1939 		usb_power_delivery_unregister(con->pd);
1940 		con->pd = NULL;
1941 		typec_unregister_port(con->port);
1942 		con->port = NULL;
1943 	}
1944 	kfree(connector);
1945 err_reset:
1946 	memset(&ucsi->cap, 0, sizeof(ucsi->cap));
1947 	ucsi_reset_ppm(ucsi);
1948 err:
1949 	return ret;
1950 }
1951 
ucsi_resume_work(struct work_struct * work)1952 static void ucsi_resume_work(struct work_struct *work)
1953 {
1954 	struct ucsi *ucsi = container_of(work, struct ucsi, resume_work);
1955 	struct ucsi_connector *con;
1956 	u64 command;
1957 	int ret;
1958 
1959 	/* Restore UCSI notification enable mask after system resume */
1960 	command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1961 	ucsi->message_in_size = 0;
1962 	ret = ucsi_send_command(ucsi, command);
1963 	if (ret < 0) {
1964 		dev_err(ucsi->dev, "failed to re-enable notifications (%d)\n", ret);
1965 		return;
1966 	}
1967 
1968 	for (con = ucsi->connector; con->port; con++) {
1969 		mutex_lock(&con->lock);
1970 		ucsi_partner_task(con, ucsi_check_connection, 1, 0);
1971 		mutex_unlock(&con->lock);
1972 	}
1973 }
1974 
ucsi_resume(struct ucsi * ucsi)1975 int ucsi_resume(struct ucsi *ucsi)
1976 {
1977 	if (ucsi->connector)
1978 		queue_work(system_long_wq, &ucsi->resume_work);
1979 	return 0;
1980 }
1981 EXPORT_SYMBOL_GPL(ucsi_resume);
1982 
ucsi_init_work(struct work_struct * work)1983 static void ucsi_init_work(struct work_struct *work)
1984 {
1985 	struct ucsi *ucsi = container_of(work, struct ucsi, work.work);
1986 	int ret;
1987 
1988 	ret = ucsi_init(ucsi);
1989 	if (ret)
1990 		dev_err_probe(ucsi->dev, ret, "PPM init failed\n");
1991 
1992 	if (ret == -EPROBE_DEFER) {
1993 		if (ucsi->work_count++ > UCSI_ROLE_SWITCH_WAIT_COUNT) {
1994 			dev_err(ucsi->dev, "PPM init failed, stop trying\n");
1995 			return;
1996 		}
1997 
1998 		queue_delayed_work(system_long_wq, &ucsi->work,
1999 				   UCSI_ROLE_SWITCH_INTERVAL);
2000 	}
2001 }
2002 
2003 /**
2004  * ucsi_get_drvdata - Return private driver data pointer
2005  * @ucsi: UCSI interface
2006  */
ucsi_get_drvdata(struct ucsi * ucsi)2007 void *ucsi_get_drvdata(struct ucsi *ucsi)
2008 {
2009 	return ucsi->driver_data;
2010 }
2011 EXPORT_SYMBOL_GPL(ucsi_get_drvdata);
2012 
2013 /**
2014  * ucsi_set_drvdata - Assign private driver data pointer
2015  * @ucsi: UCSI interface
2016  * @data: Private data pointer
2017  */
ucsi_set_drvdata(struct ucsi * ucsi,void * data)2018 void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
2019 {
2020 	ucsi->driver_data = data;
2021 }
2022 EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
2023 
2024 /**
2025  * ucsi_con_mutex_lock - Acquire the connector mutex
2026  * @con: The connector interface to lock
2027  *
2028  * Returns true on success, false if the connector is disconnected
2029  */
ucsi_con_mutex_lock(struct ucsi_connector * con)2030 bool ucsi_con_mutex_lock(struct ucsi_connector *con)
2031 {
2032 	bool mutex_locked = false;
2033 	bool connected = true;
2034 
2035 	while (connected && !mutex_locked) {
2036 		mutex_locked = mutex_trylock(&con->lock) != 0;
2037 		connected = UCSI_CONSTAT(con, CONNECTED);
2038 		if (connected && !mutex_locked)
2039 			msleep(20);
2040 	}
2041 
2042 	connected = connected && con->partner;
2043 	if (!connected && mutex_locked)
2044 		mutex_unlock(&con->lock);
2045 
2046 	return connected;
2047 }
2048 
2049 /**
2050  * ucsi_con_mutex_unlock - Release the connector mutex
2051  * @con: The connector interface to unlock
2052  */
ucsi_con_mutex_unlock(struct ucsi_connector * con)2053 void ucsi_con_mutex_unlock(struct ucsi_connector *con)
2054 {
2055 	mutex_unlock(&con->lock);
2056 }
2057 
2058 /**
2059  * ucsi_create - Allocate UCSI instance
2060  * @dev: Device interface to the PPM (Platform Policy Manager)
2061  * @ops: I/O routines
2062  */
ucsi_create(struct device * dev,const struct ucsi_operations * ops)2063 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
2064 {
2065 	struct ucsi *ucsi;
2066 
2067 	if (!ops ||
2068 	    !ops->read_version || !ops->read_cci || !ops->poll_cci ||
2069 	    !ops->read_message_in || !ops->sync_control || !ops->async_control)
2070 		return ERR_PTR(-EINVAL);
2071 
2072 	ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
2073 	if (!ucsi)
2074 		return ERR_PTR(-ENOMEM);
2075 
2076 	INIT_WORK(&ucsi->resume_work, ucsi_resume_work);
2077 	INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work);
2078 	mutex_init(&ucsi->ppm_lock);
2079 	init_completion(&ucsi->complete);
2080 	ucsi->dev = dev;
2081 	ucsi->ops = ops;
2082 
2083 	return ucsi;
2084 }
2085 EXPORT_SYMBOL_GPL(ucsi_create);
2086 
2087 /**
2088  * ucsi_destroy - Free UCSI instance
2089  * @ucsi: UCSI instance to be freed
2090  */
ucsi_destroy(struct ucsi * ucsi)2091 void ucsi_destroy(struct ucsi *ucsi)
2092 {
2093 	ucsi_debugfs_unregister(ucsi);
2094 	kfree(ucsi);
2095 }
2096 EXPORT_SYMBOL_GPL(ucsi_destroy);
2097 
2098 /**
2099  * ucsi_register - Register UCSI interface
2100  * @ucsi: UCSI instance
2101  */
ucsi_register(struct ucsi * ucsi)2102 int ucsi_register(struct ucsi *ucsi)
2103 {
2104 	int ret;
2105 
2106 	ret = ucsi->ops->read_version(ucsi, &ucsi->version);
2107 	if (ret)
2108 		return ret;
2109 
2110 	if (!ucsi->version)
2111 		return -ENODEV;
2112 
2113 	/*
2114 	 * Version format is JJ.M.N (JJ = Major version, M = Minor version,
2115 	 * N = sub-minor version).
2116 	 */
2117 	dev_dbg(ucsi->dev, "Registered UCSI interface with version %x.%x.%x",
2118 		UCSI_BCD_GET_MAJOR(ucsi->version),
2119 		UCSI_BCD_GET_MINOR(ucsi->version),
2120 		UCSI_BCD_GET_SUBMINOR(ucsi->version));
2121 
2122 	queue_delayed_work(system_long_wq, &ucsi->work, 0);
2123 
2124 	ucsi_debugfs_register(ucsi);
2125 	return 0;
2126 }
2127 EXPORT_SYMBOL_GPL(ucsi_register);
2128 
2129 /**
2130  * ucsi_unregister - Unregister UCSI interface
2131  * @ucsi: UCSI interface to be unregistered
2132  *
2133  * Unregister UCSI interface that was created with ucsi_register().
2134  */
ucsi_unregister(struct ucsi * ucsi)2135 void ucsi_unregister(struct ucsi *ucsi)
2136 {
2137 	u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
2138 	int i;
2139 
2140 	/* Make sure that we are not in the middle of driver initialization */
2141 	cancel_delayed_work_sync(&ucsi->work);
2142 	cancel_work_sync(&ucsi->resume_work);
2143 
2144 	/* Disable notifications */
2145 	ucsi->ops->async_control(ucsi, cmd);
2146 
2147 	if (!ucsi->connector)
2148 		return;
2149 
2150 	for (i = 0; i < ucsi->cap.num_connectors; i++) {
2151 		cancel_work_sync(&ucsi->connector[i].work);
2152 
2153 		if (ucsi->connector[i].wq) {
2154 			struct ucsi_work *uwork;
2155 
2156 			mutex_lock(&ucsi->connector[i].lock);
2157 			/*
2158 			 * queue delayed items immediately so they can execute
2159 			 * and free themselves before the wq is destroyed
2160 			 */
2161 			list_for_each_entry(uwork, &ucsi->connector[i].partner_tasks, node)
2162 				mod_delayed_work(ucsi->connector[i].wq, &uwork->work, 0);
2163 			mutex_unlock(&ucsi->connector[i].lock);
2164 			destroy_workqueue(ucsi->connector[i].wq);
2165 		}
2166 
2167 		ucsi_unregister_partner(&ucsi->connector[i]);
2168 		ucsi_unregister_altmodes(&ucsi->connector[i],
2169 					 UCSI_RECIPIENT_CON);
2170 		ucsi_unregister_port_psy(&ucsi->connector[i]);
2171 
2172 		usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_sink_caps);
2173 		ucsi->connector[i].port_sink_caps = NULL;
2174 		usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_source_caps);
2175 		ucsi->connector[i].port_source_caps = NULL;
2176 		usb_power_delivery_unregister(ucsi->connector[i].pd);
2177 		ucsi->connector[i].pd = NULL;
2178 		typec_unregister_port(ucsi->connector[i].port);
2179 	}
2180 
2181 	kfree(ucsi->connector);
2182 }
2183 EXPORT_SYMBOL_GPL(ucsi_unregister);
2184 
ucsi_module_init(void)2185 static int __init ucsi_module_init(void)
2186 {
2187 	ucsi_debugfs_init();
2188 	return 0;
2189 }
2190 module_init(ucsi_module_init);
2191 
ucsi_module_exit(void)2192 static void __exit ucsi_module_exit(void)
2193 {
2194 	ucsi_debugfs_exit();
2195 }
2196 module_exit(ucsi_module_exit);
2197 
2198 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
2199 MODULE_LICENSE("GPL v2");
2200 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");
2201