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