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