xref: /openbmc/linux/drivers/usb/typec/ucsi/ucsi.c (revision 6c8c1406)
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		5000
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 
39 static int ucsi_acknowledge_command(struct ucsi *ucsi)
40 {
41 	u64 ctrl;
42 
43 	ctrl = UCSI_ACK_CC_CI;
44 	ctrl |= UCSI_ACK_COMMAND_COMPLETE;
45 
46 	return ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &ctrl, sizeof(ctrl));
47 }
48 
49 static int ucsi_acknowledge_connector_change(struct ucsi *ucsi)
50 {
51 	u64 ctrl;
52 
53 	ctrl = UCSI_ACK_CC_CI;
54 	ctrl |= UCSI_ACK_CONNECTOR_CHANGE;
55 
56 	return ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &ctrl, sizeof(ctrl));
57 }
58 
59 static int ucsi_exec_command(struct ucsi *ucsi, u64 command);
60 
61 static int ucsi_read_error(struct ucsi *ucsi)
62 {
63 	u16 error;
64 	int ret;
65 
66 	/* Acknowledge the command that failed */
67 	ret = ucsi_acknowledge_command(ucsi);
68 	if (ret)
69 		return ret;
70 
71 	ret = ucsi_exec_command(ucsi, UCSI_GET_ERROR_STATUS);
72 	if (ret < 0)
73 		return ret;
74 
75 	ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, &error, sizeof(error));
76 	if (ret)
77 		return ret;
78 
79 	ret = ucsi_acknowledge_command(ucsi);
80 	if (ret)
81 		return ret;
82 
83 	switch (error) {
84 	case UCSI_ERROR_INCOMPATIBLE_PARTNER:
85 		return -EOPNOTSUPP;
86 	case UCSI_ERROR_CC_COMMUNICATION_ERR:
87 		return -ECOMM;
88 	case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
89 		return -EPROTO;
90 	case UCSI_ERROR_DEAD_BATTERY:
91 		dev_warn(ucsi->dev, "Dead battery condition!\n");
92 		return -EPERM;
93 	case UCSI_ERROR_INVALID_CON_NUM:
94 	case UCSI_ERROR_UNREGONIZED_CMD:
95 	case UCSI_ERROR_INVALID_CMD_ARGUMENT:
96 		dev_err(ucsi->dev, "possible UCSI driver bug %u\n", error);
97 		return -EINVAL;
98 	case UCSI_ERROR_OVERCURRENT:
99 		dev_warn(ucsi->dev, "Overcurrent condition\n");
100 		break;
101 	case UCSI_ERROR_PARTNER_REJECTED_SWAP:
102 		dev_warn(ucsi->dev, "Partner rejected swap\n");
103 		break;
104 	case UCSI_ERROR_HARD_RESET:
105 		dev_warn(ucsi->dev, "Hard reset occurred\n");
106 		break;
107 	case UCSI_ERROR_PPM_POLICY_CONFLICT:
108 		dev_warn(ucsi->dev, "PPM Policy conflict\n");
109 		break;
110 	case UCSI_ERROR_SWAP_REJECTED:
111 		dev_warn(ucsi->dev, "Swap rejected\n");
112 		break;
113 	case UCSI_ERROR_UNDEFINED:
114 	default:
115 		dev_err(ucsi->dev, "unknown error %u\n", error);
116 		break;
117 	}
118 
119 	return -EIO;
120 }
121 
122 static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd)
123 {
124 	u32 cci;
125 	int ret;
126 
127 	ret = ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
128 	if (ret)
129 		return ret;
130 
131 	ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
132 	if (ret)
133 		return ret;
134 
135 	if (cci & UCSI_CCI_BUSY) {
136 		ucsi->ops->async_write(ucsi, UCSI_CANCEL, NULL, 0);
137 		return -EBUSY;
138 	}
139 
140 	if (!(cci & UCSI_CCI_COMMAND_COMPLETE))
141 		return -EIO;
142 
143 	if (cci & UCSI_CCI_NOT_SUPPORTED)
144 		return -EOPNOTSUPP;
145 
146 	if (cci & UCSI_CCI_ERROR) {
147 		if (cmd == UCSI_GET_ERROR_STATUS)
148 			return -EIO;
149 		return ucsi_read_error(ucsi);
150 	}
151 
152 	return UCSI_CCI_LENGTH(cci);
153 }
154 
155 int ucsi_send_command(struct ucsi *ucsi, u64 command,
156 		      void *data, size_t size)
157 {
158 	u8 length;
159 	int ret;
160 
161 	mutex_lock(&ucsi->ppm_lock);
162 
163 	ret = ucsi_exec_command(ucsi, command);
164 	if (ret < 0)
165 		goto out;
166 
167 	length = ret;
168 
169 	if (data) {
170 		ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, data, size);
171 		if (ret)
172 			goto out;
173 	}
174 
175 	ret = ucsi_acknowledge_command(ucsi);
176 	if (ret)
177 		goto out;
178 
179 	ret = length;
180 out:
181 	mutex_unlock(&ucsi->ppm_lock);
182 	return ret;
183 }
184 EXPORT_SYMBOL_GPL(ucsi_send_command);
185 
186 /* -------------------------------------------------------------------------- */
187 
188 struct ucsi_work {
189 	struct delayed_work work;
190 	unsigned long delay;
191 	unsigned int count;
192 	struct ucsi_connector *con;
193 	int (*cb)(struct ucsi_connector *);
194 };
195 
196 static void ucsi_poll_worker(struct work_struct *work)
197 {
198 	struct ucsi_work *uwork = container_of(work, struct ucsi_work, work.work);
199 	struct ucsi_connector *con = uwork->con;
200 	int ret;
201 
202 	mutex_lock(&con->lock);
203 
204 	if (!con->partner) {
205 		mutex_unlock(&con->lock);
206 		kfree(uwork);
207 		return;
208 	}
209 
210 	ret = uwork->cb(con);
211 
212 	if (uwork->count-- && (ret == -EBUSY || ret == -ETIMEDOUT))
213 		queue_delayed_work(con->wq, &uwork->work, uwork->delay);
214 	else
215 		kfree(uwork);
216 
217 	mutex_unlock(&con->lock);
218 }
219 
220 static int ucsi_partner_task(struct ucsi_connector *con,
221 			     int (*cb)(struct ucsi_connector *),
222 			     int retries, unsigned long delay)
223 {
224 	struct ucsi_work *uwork;
225 
226 	if (!con->partner)
227 		return 0;
228 
229 	uwork = kzalloc(sizeof(*uwork), GFP_KERNEL);
230 	if (!uwork)
231 		return -ENOMEM;
232 
233 	INIT_DELAYED_WORK(&uwork->work, ucsi_poll_worker);
234 	uwork->count = retries;
235 	uwork->delay = delay;
236 	uwork->con = con;
237 	uwork->cb = cb;
238 
239 	queue_delayed_work(con->wq, &uwork->work, delay);
240 
241 	return 0;
242 }
243 
244 /* -------------------------------------------------------------------------- */
245 
246 void ucsi_altmode_update_active(struct ucsi_connector *con)
247 {
248 	const struct typec_altmode *altmode = NULL;
249 	u64 command;
250 	int ret;
251 	u8 cur;
252 	int i;
253 
254 	command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
255 	ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur));
256 	if (ret < 0) {
257 		if (con->ucsi->version > 0x0100) {
258 			dev_err(con->ucsi->dev,
259 				"GET_CURRENT_CAM command failed\n");
260 			return;
261 		}
262 		cur = 0xff;
263 	}
264 
265 	if (cur < UCSI_MAX_ALTMODES)
266 		altmode = typec_altmode_get_partner(con->port_altmode[cur]);
267 
268 	for (i = 0; con->partner_altmode[i]; i++)
269 		typec_altmode_update_active(con->partner_altmode[i],
270 					    con->partner_altmode[i] == altmode);
271 }
272 
273 static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
274 {
275 	u8 mode = 1;
276 	int i;
277 
278 	for (i = 0; alt[i]; i++) {
279 		if (i > MODE_DISCOVERY_MAX)
280 			return -ERANGE;
281 
282 		if (alt[i]->svid == svid)
283 			mode++;
284 	}
285 
286 	return mode;
287 }
288 
289 static int ucsi_next_altmode(struct typec_altmode **alt)
290 {
291 	int i = 0;
292 
293 	for (i = 0; i < UCSI_MAX_ALTMODES; i++)
294 		if (!alt[i])
295 			return i;
296 
297 	return -ENOENT;
298 }
299 
300 static int ucsi_get_num_altmode(struct typec_altmode **alt)
301 {
302 	int i;
303 
304 	for (i = 0; i < UCSI_MAX_ALTMODES; i++)
305 		if (!alt[i])
306 			break;
307 
308 	return i;
309 }
310 
311 static int ucsi_register_altmode(struct ucsi_connector *con,
312 				 struct typec_altmode_desc *desc,
313 				 u8 recipient)
314 {
315 	struct typec_altmode *alt;
316 	bool override;
317 	int ret;
318 	int i;
319 
320 	override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
321 
322 	switch (recipient) {
323 	case UCSI_RECIPIENT_CON:
324 		i = ucsi_next_altmode(con->port_altmode);
325 		if (i < 0) {
326 			ret = i;
327 			goto err;
328 		}
329 
330 		ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid);
331 		if (ret < 0)
332 			return ret;
333 
334 		desc->mode = ret;
335 
336 		switch (desc->svid) {
337 		case USB_TYPEC_DP_SID:
338 			alt = ucsi_register_displayport(con, override, i, desc);
339 			break;
340 		case USB_TYPEC_NVIDIA_VLINK_SID:
341 			if (desc->vdo == USB_TYPEC_NVIDIA_VLINK_DBG_VDO)
342 				alt = typec_port_register_altmode(con->port,
343 								  desc);
344 			else
345 				alt = ucsi_register_displayport(con, override,
346 								i, desc);
347 			break;
348 		default:
349 			alt = typec_port_register_altmode(con->port, desc);
350 			break;
351 		}
352 
353 		if (IS_ERR(alt)) {
354 			ret = PTR_ERR(alt);
355 			goto err;
356 		}
357 
358 		con->port_altmode[i] = alt;
359 		break;
360 	case UCSI_RECIPIENT_SOP:
361 		i = ucsi_next_altmode(con->partner_altmode);
362 		if (i < 0) {
363 			ret = i;
364 			goto err;
365 		}
366 
367 		ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid);
368 		if (ret < 0)
369 			return ret;
370 
371 		desc->mode = ret;
372 
373 		alt = typec_partner_register_altmode(con->partner, desc);
374 		if (IS_ERR(alt)) {
375 			ret = PTR_ERR(alt);
376 			goto err;
377 		}
378 
379 		con->partner_altmode[i] = alt;
380 		break;
381 	default:
382 		return -EINVAL;
383 	}
384 
385 	trace_ucsi_register_altmode(recipient, alt);
386 
387 	return 0;
388 
389 err:
390 	dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n",
391 		desc->svid, desc->mode);
392 
393 	return ret;
394 }
395 
396 static int
397 ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient)
398 {
399 	int max_altmodes = UCSI_MAX_ALTMODES;
400 	struct typec_altmode_desc desc;
401 	struct ucsi_altmode alt;
402 	struct ucsi_altmode orig[UCSI_MAX_ALTMODES];
403 	struct ucsi_altmode updated[UCSI_MAX_ALTMODES];
404 	struct ucsi *ucsi = con->ucsi;
405 	bool multi_dp = false;
406 	u64 command;
407 	int ret;
408 	int len;
409 	int i;
410 	int k = 0;
411 
412 	if (recipient == UCSI_RECIPIENT_CON)
413 		max_altmodes = con->ucsi->cap.num_alt_modes;
414 
415 	memset(orig, 0, sizeof(orig));
416 	memset(updated, 0, sizeof(updated));
417 
418 	/* First get all the alternate modes */
419 	for (i = 0; i < max_altmodes; i++) {
420 		memset(&alt, 0, sizeof(alt));
421 		command = UCSI_GET_ALTERNATE_MODES;
422 		command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
423 		command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
424 		command |= UCSI_GET_ALTMODE_OFFSET(i);
425 		len = ucsi_send_command(con->ucsi, command, &alt, sizeof(alt));
426 		/*
427 		 * We are collecting all altmodes first and then registering.
428 		 * Some type-C device will return zero length data beyond last
429 		 * alternate modes. We should not return if length is zero.
430 		 */
431 		if (len < 0)
432 			return len;
433 
434 		/* We got all altmodes, now break out and register them */
435 		if (!len || !alt.svid)
436 			break;
437 
438 		orig[k].mid = alt.mid;
439 		orig[k].svid = alt.svid;
440 		k++;
441 	}
442 	/*
443 	 * Update the original altmode table as some ppms may report
444 	 * multiple DP altmodes.
445 	 */
446 	if (recipient == UCSI_RECIPIENT_CON)
447 		multi_dp = ucsi->ops->update_altmodes(ucsi, orig, updated);
448 
449 	/* now register altmodes */
450 	for (i = 0; i < max_altmodes; i++) {
451 		memset(&desc, 0, sizeof(desc));
452 		if (multi_dp && recipient == UCSI_RECIPIENT_CON) {
453 			desc.svid = updated[i].svid;
454 			desc.vdo = updated[i].mid;
455 		} else {
456 			desc.svid = orig[i].svid;
457 			desc.vdo = orig[i].mid;
458 		}
459 		desc.roles = TYPEC_PORT_DRD;
460 
461 		if (!desc.svid)
462 			return 0;
463 
464 		ret = ucsi_register_altmode(con, &desc, recipient);
465 		if (ret)
466 			return ret;
467 	}
468 
469 	return 0;
470 }
471 
472 static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
473 {
474 	int max_altmodes = UCSI_MAX_ALTMODES;
475 	struct typec_altmode_desc desc;
476 	struct ucsi_altmode alt[2];
477 	u64 command;
478 	int num;
479 	int ret;
480 	int len;
481 	int j;
482 	int i;
483 
484 	if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
485 		return 0;
486 
487 	if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
488 		return 0;
489 
490 	if (con->ucsi->ops->update_altmodes)
491 		return ucsi_register_altmodes_nvidia(con, recipient);
492 
493 	if (recipient == UCSI_RECIPIENT_CON)
494 		max_altmodes = con->ucsi->cap.num_alt_modes;
495 
496 	for (i = 0; i < max_altmodes;) {
497 		memset(alt, 0, sizeof(alt));
498 		command = UCSI_GET_ALTERNATE_MODES;
499 		command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
500 		command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
501 		command |= UCSI_GET_ALTMODE_OFFSET(i);
502 		len = ucsi_send_command(con->ucsi, command, alt, sizeof(alt));
503 		if (len == -EBUSY)
504 			continue;
505 		if (len <= 0)
506 			return len;
507 
508 		/*
509 		 * This code is requesting one alt mode at a time, but some PPMs
510 		 * may still return two. If that happens both alt modes need be
511 		 * registered and the offset for the next alt mode has to be
512 		 * incremented.
513 		 */
514 		num = len / sizeof(alt[0]);
515 		i += num;
516 
517 		for (j = 0; j < num; j++) {
518 			if (!alt[j].svid)
519 				return 0;
520 
521 			memset(&desc, 0, sizeof(desc));
522 			desc.vdo = alt[j].mid;
523 			desc.svid = alt[j].svid;
524 			desc.roles = TYPEC_PORT_DRD;
525 
526 			ret = ucsi_register_altmode(con, &desc, recipient);
527 			if (ret)
528 				return ret;
529 		}
530 	}
531 
532 	return 0;
533 }
534 
535 static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
536 {
537 	const struct typec_altmode *pdev;
538 	struct typec_altmode **adev;
539 	int i = 0;
540 
541 	switch (recipient) {
542 	case UCSI_RECIPIENT_CON:
543 		adev = con->port_altmode;
544 		break;
545 	case UCSI_RECIPIENT_SOP:
546 		adev = con->partner_altmode;
547 		break;
548 	default:
549 		return;
550 	}
551 
552 	while (adev[i]) {
553 		if (recipient == UCSI_RECIPIENT_SOP &&
554 		    (adev[i]->svid == USB_TYPEC_DP_SID ||
555 			(adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID &&
556 			adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) {
557 			pdev = typec_altmode_get_partner(adev[i]);
558 			ucsi_displayport_remove_partner((void *)pdev);
559 		}
560 		typec_unregister_altmode(adev[i]);
561 		adev[i++] = NULL;
562 	}
563 }
564 
565 static int ucsi_get_pdos(struct ucsi_connector *con, int is_partner,
566 			 u32 *pdos, int offset, int num_pdos)
567 {
568 	struct ucsi *ucsi = con->ucsi;
569 	u64 command;
570 	int ret;
571 
572 	command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num);
573 	command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner);
574 	command |= UCSI_GET_PDOS_PDO_OFFSET(offset);
575 	command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1);
576 	command |= UCSI_GET_PDOS_SRC_PDOS;
577 	ret = ucsi_send_command(ucsi, command, pdos + offset,
578 				num_pdos * sizeof(u32));
579 	if (ret < 0 && ret != -ETIMEDOUT)
580 		dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret);
581 
582 	return ret;
583 }
584 
585 static int ucsi_get_src_pdos(struct ucsi_connector *con)
586 {
587 	int ret;
588 
589 	/* UCSI max payload means only getting at most 4 PDOs at a time */
590 	ret = ucsi_get_pdos(con, 1, con->src_pdos, 0, UCSI_MAX_PDOS);
591 	if (ret < 0)
592 		return ret;
593 
594 	con->num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */
595 	if (con->num_pdos < UCSI_MAX_PDOS)
596 		return 0;
597 
598 	/* get the remaining PDOs, if any */
599 	ret = ucsi_get_pdos(con, 1, con->src_pdos, UCSI_MAX_PDOS,
600 			    PDO_MAX_OBJECTS - UCSI_MAX_PDOS);
601 	if (ret < 0)
602 		return ret;
603 
604 	con->num_pdos += ret / sizeof(u32);
605 
606 	ucsi_port_psy_changed(con);
607 
608 	return 0;
609 }
610 
611 static int ucsi_check_altmodes(struct ucsi_connector *con)
612 {
613 	int ret, num_partner_am;
614 
615 	ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
616 	if (ret && ret != -ETIMEDOUT)
617 		dev_err(con->ucsi->dev,
618 			"con%d: failed to register partner alt modes (%d)\n",
619 			con->num, ret);
620 
621 	/* Ignoring the errors in this case. */
622 	if (con->partner_altmode[0]) {
623 		num_partner_am = ucsi_get_num_altmode(con->partner_altmode);
624 		if (num_partner_am > 0)
625 			typec_partner_set_num_altmodes(con->partner, num_partner_am);
626 		ucsi_altmode_update_active(con);
627 		return 0;
628 	}
629 
630 	return ret;
631 }
632 
633 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
634 {
635 	switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
636 	case UCSI_CONSTAT_PWR_OPMODE_PD:
637 		con->rdo = con->status.request_data_obj;
638 		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
639 		ucsi_partner_task(con, ucsi_get_src_pdos, 30, 0);
640 		ucsi_partner_task(con, ucsi_check_altmodes, 30, 0);
641 		break;
642 	case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
643 		con->rdo = 0;
644 		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
645 		break;
646 	case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
647 		con->rdo = 0;
648 		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
649 		break;
650 	default:
651 		con->rdo = 0;
652 		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
653 		break;
654 	}
655 }
656 
657 static int ucsi_register_partner(struct ucsi_connector *con)
658 {
659 	u8 pwr_opmode = UCSI_CONSTAT_PWR_OPMODE(con->status.flags);
660 	struct typec_partner_desc desc;
661 	struct typec_partner *partner;
662 
663 	if (con->partner)
664 		return 0;
665 
666 	memset(&desc, 0, sizeof(desc));
667 
668 	switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
669 	case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
670 		desc.accessory = TYPEC_ACCESSORY_DEBUG;
671 		break;
672 	case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
673 		desc.accessory = TYPEC_ACCESSORY_AUDIO;
674 		break;
675 	default:
676 		break;
677 	}
678 
679 	desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD;
680 
681 	partner = typec_register_partner(con->port, &desc);
682 	if (IS_ERR(partner)) {
683 		dev_err(con->ucsi->dev,
684 			"con%d: failed to register partner (%ld)\n", con->num,
685 			PTR_ERR(partner));
686 		return PTR_ERR(partner);
687 	}
688 
689 	con->partner = partner;
690 
691 	return 0;
692 }
693 
694 static void ucsi_unregister_partner(struct ucsi_connector *con)
695 {
696 	if (!con->partner)
697 		return;
698 
699 	ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
700 	typec_unregister_partner(con->partner);
701 	con->partner = NULL;
702 }
703 
704 static void ucsi_partner_change(struct ucsi_connector *con)
705 {
706 	enum usb_role u_role = USB_ROLE_NONE;
707 	int ret;
708 
709 	switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
710 	case UCSI_CONSTAT_PARTNER_TYPE_UFP:
711 	case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
712 		u_role = USB_ROLE_HOST;
713 		fallthrough;
714 	case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
715 		typec_set_data_role(con->port, TYPEC_HOST);
716 		break;
717 	case UCSI_CONSTAT_PARTNER_TYPE_DFP:
718 		u_role = USB_ROLE_DEVICE;
719 		typec_set_data_role(con->port, TYPEC_DEVICE);
720 		break;
721 	default:
722 		break;
723 	}
724 
725 	/* Only notify USB controller if partner supports USB data */
726 	if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
727 		u_role = USB_ROLE_NONE;
728 
729 	ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
730 	if (ret)
731 		dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n",
732 			con->num, u_role);
733 }
734 
735 static int ucsi_check_connection(struct ucsi_connector *con)
736 {
737 	u8 prev_flags = con->status.flags;
738 	u64 command;
739 	int ret;
740 
741 	command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
742 	ret = ucsi_send_command(con->ucsi, command, &con->status, sizeof(con->status));
743 	if (ret < 0) {
744 		dev_err(con->ucsi->dev, "GET_CONNECTOR_STATUS failed (%d)\n", ret);
745 		return ret;
746 	}
747 
748 	if (con->status.flags == prev_flags)
749 		return 0;
750 
751 	if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
752 		ucsi_register_partner(con);
753 		ucsi_pwr_opmode_change(con);
754 		ucsi_partner_change(con);
755 	} else {
756 		ucsi_partner_change(con);
757 		ucsi_port_psy_changed(con);
758 		ucsi_unregister_partner(con);
759 	}
760 
761 	return 0;
762 }
763 
764 static void ucsi_handle_connector_change(struct work_struct *work)
765 {
766 	struct ucsi_connector *con = container_of(work, struct ucsi_connector,
767 						  work);
768 	struct ucsi *ucsi = con->ucsi;
769 	enum typec_role role;
770 	u64 command;
771 	int ret;
772 
773 	mutex_lock(&con->lock);
774 
775 	command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
776 	ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
777 	if (ret < 0) {
778 		dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
779 			__func__, ret);
780 		goto out_unlock;
781 	}
782 
783 	trace_ucsi_connector_change(con->num, &con->status);
784 
785 	role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
786 
787 	if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
788 		typec_set_pwr_role(con->port, role);
789 
790 		/* Complete pending power role swap */
791 		if (!completion_done(&con->complete))
792 			complete(&con->complete);
793 	}
794 
795 	if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
796 		typec_set_pwr_role(con->port, role);
797 		ucsi_port_psy_changed(con);
798 		ucsi_partner_change(con);
799 
800 		if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
801 			ucsi_register_partner(con);
802 			ucsi_partner_task(con, ucsi_check_connection, 1, HZ);
803 		} else {
804 			ucsi_unregister_partner(con);
805 		}
806 	}
807 
808 	if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE ||
809 	    con->status.change & UCSI_CONSTAT_POWER_LEVEL_CHANGE)
810 		ucsi_pwr_opmode_change(con);
811 
812 	if (con->partner && con->status.change & UCSI_CONSTAT_PARTNER_CHANGE) {
813 		ucsi_partner_change(con);
814 
815 		/* Complete pending data role swap */
816 		if (!completion_done(&con->complete))
817 			complete(&con->complete);
818 	}
819 
820 	if (con->status.change & UCSI_CONSTAT_CAM_CHANGE)
821 		ucsi_partner_task(con, ucsi_check_altmodes, 1, 0);
822 
823 	clear_bit(EVENT_PENDING, &con->ucsi->flags);
824 
825 	ret = ucsi_acknowledge_connector_change(ucsi);
826 	if (ret)
827 		dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret);
828 
829 out_unlock:
830 	mutex_unlock(&con->lock);
831 }
832 
833 /**
834  * ucsi_connector_change - Process Connector Change Event
835  * @ucsi: UCSI Interface
836  * @num: Connector number
837  */
838 void ucsi_connector_change(struct ucsi *ucsi, u8 num)
839 {
840 	struct ucsi_connector *con = &ucsi->connector[num - 1];
841 
842 	if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
843 		dev_dbg(ucsi->dev, "Bogus connector change event\n");
844 		return;
845 	}
846 
847 	if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
848 		schedule_work(&con->work);
849 }
850 EXPORT_SYMBOL_GPL(ucsi_connector_change);
851 
852 /* -------------------------------------------------------------------------- */
853 
854 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
855 {
856 	u64 command;
857 
858 	command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num);
859 	command |= hard ? UCSI_CONNECTOR_RESET_HARD : 0;
860 
861 	return ucsi_send_command(con->ucsi, command, NULL, 0);
862 }
863 
864 static int ucsi_reset_ppm(struct ucsi *ucsi)
865 {
866 	u64 command = UCSI_PPM_RESET;
867 	unsigned long tmo;
868 	u32 cci;
869 	int ret;
870 
871 	mutex_lock(&ucsi->ppm_lock);
872 
873 	ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command,
874 				     sizeof(command));
875 	if (ret < 0)
876 		goto out;
877 
878 	tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
879 
880 	do {
881 		if (time_is_before_jiffies(tmo)) {
882 			ret = -ETIMEDOUT;
883 			goto out;
884 		}
885 
886 		ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
887 		if (ret)
888 			goto out;
889 
890 		/* If the PPM is still doing something else, reset it again. */
891 		if (cci & ~UCSI_CCI_RESET_COMPLETE) {
892 			ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL,
893 						     &command,
894 						     sizeof(command));
895 			if (ret < 0)
896 				goto out;
897 		}
898 
899 		msleep(20);
900 	} while (!(cci & UCSI_CCI_RESET_COMPLETE));
901 
902 out:
903 	mutex_unlock(&ucsi->ppm_lock);
904 	return ret;
905 }
906 
907 static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)
908 {
909 	int ret;
910 
911 	ret = ucsi_send_command(con->ucsi, command, NULL, 0);
912 	if (ret == -ETIMEDOUT) {
913 		u64 c;
914 
915 		/* PPM most likely stopped responding. Resetting everything. */
916 		ucsi_reset_ppm(con->ucsi);
917 
918 		c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
919 		ucsi_send_command(con->ucsi, c, NULL, 0);
920 
921 		ucsi_reset_connector(con, true);
922 	}
923 
924 	return ret;
925 }
926 
927 static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
928 {
929 	struct ucsi_connector *con = typec_get_drvdata(port);
930 	u8 partner_type;
931 	u64 command;
932 	int ret = 0;
933 
934 	mutex_lock(&con->lock);
935 
936 	if (!con->partner) {
937 		ret = -ENOTCONN;
938 		goto out_unlock;
939 	}
940 
941 	partner_type = UCSI_CONSTAT_PARTNER_TYPE(con->status.flags);
942 	if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
943 	     role == TYPEC_DEVICE) ||
944 	    (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
945 	     role == TYPEC_HOST))
946 		goto out_unlock;
947 
948 	reinit_completion(&con->complete);
949 
950 	command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num);
951 	command |= UCSI_SET_UOR_ROLE(role);
952 	command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS;
953 	ret = ucsi_role_cmd(con, command);
954 	if (ret < 0)
955 		goto out_unlock;
956 
957 	mutex_unlock(&con->lock);
958 
959 	if (!wait_for_completion_timeout(&con->complete,
960 					 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
961 		return -ETIMEDOUT;
962 
963 	return 0;
964 
965 out_unlock:
966 	mutex_unlock(&con->lock);
967 
968 	return ret;
969 }
970 
971 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
972 {
973 	struct ucsi_connector *con = typec_get_drvdata(port);
974 	enum typec_role cur_role;
975 	u64 command;
976 	int ret = 0;
977 
978 	mutex_lock(&con->lock);
979 
980 	if (!con->partner) {
981 		ret = -ENOTCONN;
982 		goto out_unlock;
983 	}
984 
985 	cur_role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
986 
987 	if (cur_role == role)
988 		goto out_unlock;
989 
990 	reinit_completion(&con->complete);
991 
992 	command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num);
993 	command |= UCSI_SET_PDR_ROLE(role);
994 	command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS;
995 	ret = ucsi_role_cmd(con, command);
996 	if (ret < 0)
997 		goto out_unlock;
998 
999 	mutex_unlock(&con->lock);
1000 
1001 	if (!wait_for_completion_timeout(&con->complete,
1002 					 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1003 		return -ETIMEDOUT;
1004 
1005 	mutex_lock(&con->lock);
1006 
1007 	/* Something has gone wrong while swapping the role */
1008 	if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) !=
1009 	    UCSI_CONSTAT_PWR_OPMODE_PD) {
1010 		ucsi_reset_connector(con, true);
1011 		ret = -EPROTO;
1012 	}
1013 
1014 out_unlock:
1015 	mutex_unlock(&con->lock);
1016 
1017 	return ret;
1018 }
1019 
1020 static const struct typec_operations ucsi_ops = {
1021 	.dr_set = ucsi_dr_swap,
1022 	.pr_set = ucsi_pr_swap
1023 };
1024 
1025 /* Caller must call fwnode_handle_put() after use */
1026 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
1027 {
1028 	struct fwnode_handle *fwnode;
1029 	int i = 1;
1030 
1031 	device_for_each_child_node(con->ucsi->dev, fwnode)
1032 		if (i++ == con->num)
1033 			return fwnode;
1034 	return NULL;
1035 }
1036 
1037 static int ucsi_register_port(struct ucsi *ucsi, int index)
1038 {
1039 	struct ucsi_connector *con = &ucsi->connector[index];
1040 	struct typec_capability *cap = &con->typec_cap;
1041 	enum typec_accessory *accessory = cap->accessory;
1042 	enum usb_role u_role = USB_ROLE_NONE;
1043 	u64 command;
1044 	char *name;
1045 	int ret;
1046 
1047 	name = kasprintf(GFP_KERNEL, "%s-con%d", dev_name(ucsi->dev), con->num);
1048 	if (!name)
1049 		return -ENOMEM;
1050 
1051 	con->wq = create_singlethread_workqueue(name);
1052 	kfree(name);
1053 	if (!con->wq)
1054 		return -ENOMEM;
1055 
1056 	INIT_WORK(&con->work, ucsi_handle_connector_change);
1057 	init_completion(&con->complete);
1058 	mutex_init(&con->lock);
1059 	con->num = index + 1;
1060 	con->ucsi = ucsi;
1061 
1062 	cap->fwnode = ucsi_find_fwnode(con);
1063 	con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode);
1064 	if (IS_ERR(con->usb_role_sw))
1065 		return dev_err_probe(ucsi->dev, PTR_ERR(con->usb_role_sw),
1066 			"con%d: failed to get usb role switch\n", con->num);
1067 
1068 	/* Delay other interactions with the con until registration is complete */
1069 	mutex_lock(&con->lock);
1070 
1071 	/* Get connector capability */
1072 	command = UCSI_GET_CONNECTOR_CAPABILITY;
1073 	command |= UCSI_CONNECTOR_NUMBER(con->num);
1074 	ret = ucsi_send_command(ucsi, command, &con->cap, sizeof(con->cap));
1075 	if (ret < 0)
1076 		goto out_unlock;
1077 
1078 	if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP)
1079 		cap->data = TYPEC_PORT_DRD;
1080 	else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP)
1081 		cap->data = TYPEC_PORT_DFP;
1082 	else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP)
1083 		cap->data = TYPEC_PORT_UFP;
1084 
1085 	if ((con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER) &&
1086 	    (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER))
1087 		cap->type = TYPEC_PORT_DRP;
1088 	else if (con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER)
1089 		cap->type = TYPEC_PORT_SRC;
1090 	else if (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER)
1091 		cap->type = TYPEC_PORT_SNK;
1092 
1093 	cap->revision = ucsi->cap.typec_version;
1094 	cap->pd_revision = ucsi->cap.pd_version;
1095 	cap->svdm_version = SVDM_VER_2_0;
1096 	cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
1097 
1098 	if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY)
1099 		*accessory++ = TYPEC_ACCESSORY_AUDIO;
1100 	if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
1101 		*accessory = TYPEC_ACCESSORY_DEBUG;
1102 
1103 	cap->driver_data = con;
1104 	cap->ops = &ucsi_ops;
1105 
1106 	ret = ucsi_register_port_psy(con);
1107 	if (ret)
1108 		goto out;
1109 
1110 	/* Register the connector */
1111 	con->port = typec_register_port(ucsi->dev, cap);
1112 	if (IS_ERR(con->port)) {
1113 		ret = PTR_ERR(con->port);
1114 		goto out;
1115 	}
1116 
1117 	/* Alternate modes */
1118 	ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON);
1119 	if (ret) {
1120 		dev_err(ucsi->dev, "con%d: failed to register alt modes\n",
1121 			con->num);
1122 		goto out;
1123 	}
1124 
1125 	/* Get the status */
1126 	command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
1127 	ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
1128 	if (ret < 0) {
1129 		dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
1130 		ret = 0;
1131 		goto out;
1132 	}
1133 	ret = 0; /* ucsi_send_command() returns length on success */
1134 
1135 	switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1136 	case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1137 	case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1138 		u_role = USB_ROLE_HOST;
1139 		fallthrough;
1140 	case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1141 		typec_set_data_role(con->port, TYPEC_HOST);
1142 		break;
1143 	case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1144 		u_role = USB_ROLE_DEVICE;
1145 		typec_set_data_role(con->port, TYPEC_DEVICE);
1146 		break;
1147 	default:
1148 		break;
1149 	}
1150 
1151 	/* Check if there is already something connected */
1152 	if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1153 		typec_set_pwr_role(con->port,
1154 				  !!(con->status.flags & UCSI_CONSTAT_PWR_DIR));
1155 		ucsi_pwr_opmode_change(con);
1156 		ucsi_register_partner(con);
1157 		ucsi_port_psy_changed(con);
1158 	}
1159 
1160 	/* Only notify USB controller if partner supports USB data */
1161 	if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
1162 		u_role = USB_ROLE_NONE;
1163 
1164 	ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1165 	if (ret) {
1166 		dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n",
1167 			con->num, u_role);
1168 		ret = 0;
1169 	}
1170 
1171 	if (con->partner &&
1172 	    UCSI_CONSTAT_PWR_OPMODE(con->status.flags) ==
1173 	    UCSI_CONSTAT_PWR_OPMODE_PD) {
1174 		ucsi_get_src_pdos(con);
1175 		ucsi_check_altmodes(con);
1176 	}
1177 
1178 	trace_ucsi_register_port(con->num, &con->status);
1179 
1180 out:
1181 	fwnode_handle_put(cap->fwnode);
1182 out_unlock:
1183 	mutex_unlock(&con->lock);
1184 
1185 	if (ret && con->wq) {
1186 		destroy_workqueue(con->wq);
1187 		con->wq = NULL;
1188 	}
1189 
1190 	return ret;
1191 }
1192 
1193 /**
1194  * ucsi_init - Initialize UCSI interface
1195  * @ucsi: UCSI to be initialized
1196  *
1197  * Registers all ports @ucsi has and enables all notification events.
1198  */
1199 static int ucsi_init(struct ucsi *ucsi)
1200 {
1201 	struct ucsi_connector *con;
1202 	u64 command;
1203 	int ret;
1204 	int i;
1205 
1206 	/* Reset the PPM */
1207 	ret = ucsi_reset_ppm(ucsi);
1208 	if (ret) {
1209 		dev_err(ucsi->dev, "failed to reset PPM!\n");
1210 		goto err;
1211 	}
1212 
1213 	/* Enable basic notifications */
1214 	ucsi->ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
1215 	command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1216 	ret = ucsi_send_command(ucsi, command, NULL, 0);
1217 	if (ret < 0)
1218 		goto err_reset;
1219 
1220 	/* Get PPM capabilities */
1221 	command = UCSI_GET_CAPABILITY;
1222 	ret = ucsi_send_command(ucsi, command, &ucsi->cap, sizeof(ucsi->cap));
1223 	if (ret < 0)
1224 		goto err_reset;
1225 
1226 	if (!ucsi->cap.num_connectors) {
1227 		ret = -ENODEV;
1228 		goto err_reset;
1229 	}
1230 
1231 	/* Allocate the connectors. Released in ucsi_unregister() */
1232 	ucsi->connector = kcalloc(ucsi->cap.num_connectors + 1,
1233 				  sizeof(*ucsi->connector), GFP_KERNEL);
1234 	if (!ucsi->connector) {
1235 		ret = -ENOMEM;
1236 		goto err_reset;
1237 	}
1238 
1239 	/* Register all connectors */
1240 	for (i = 0; i < ucsi->cap.num_connectors; i++) {
1241 		ret = ucsi_register_port(ucsi, i);
1242 		if (ret)
1243 			goto err_unregister;
1244 	}
1245 
1246 	/* Enable all notifications */
1247 	ucsi->ntfy = UCSI_ENABLE_NTFY_ALL;
1248 	command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1249 	ret = ucsi_send_command(ucsi, command, NULL, 0);
1250 	if (ret < 0)
1251 		goto err_unregister;
1252 
1253 	return 0;
1254 
1255 err_unregister:
1256 	for (con = ucsi->connector; con->port; con++) {
1257 		ucsi_unregister_partner(con);
1258 		ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON);
1259 		ucsi_unregister_port_psy(con);
1260 		if (con->wq)
1261 			destroy_workqueue(con->wq);
1262 		typec_unregister_port(con->port);
1263 		con->port = NULL;
1264 	}
1265 
1266 err_reset:
1267 	memset(&ucsi->cap, 0, sizeof(ucsi->cap));
1268 	ucsi_reset_ppm(ucsi);
1269 err:
1270 	return ret;
1271 }
1272 
1273 int ucsi_resume(struct ucsi *ucsi)
1274 {
1275 	struct ucsi_connector *con;
1276 	u64 command;
1277 	int ret;
1278 
1279 	/* Restore UCSI notification enable mask after system resume */
1280 	command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1281 	ret = ucsi_send_command(ucsi, command, NULL, 0);
1282 	if (ret < 0)
1283 		return ret;
1284 
1285 	for (con = ucsi->connector; con->port; con++) {
1286 		mutex_lock(&con->lock);
1287 		ucsi_check_connection(con);
1288 		mutex_unlock(&con->lock);
1289 	}
1290 
1291 	return 0;
1292 }
1293 EXPORT_SYMBOL_GPL(ucsi_resume);
1294 
1295 static void ucsi_init_work(struct work_struct *work)
1296 {
1297 	struct ucsi *ucsi = container_of(work, struct ucsi, work.work);
1298 	int ret;
1299 
1300 	ret = ucsi_init(ucsi);
1301 	if (ret)
1302 		dev_err(ucsi->dev, "PPM init failed (%d)\n", ret);
1303 
1304 	if (ret == -EPROBE_DEFER) {
1305 		if (ucsi->work_count++ > UCSI_ROLE_SWITCH_WAIT_COUNT)
1306 			return;
1307 
1308 		queue_delayed_work(system_long_wq, &ucsi->work,
1309 				   UCSI_ROLE_SWITCH_INTERVAL);
1310 	}
1311 }
1312 
1313 /**
1314  * ucsi_get_drvdata - Return private driver data pointer
1315  * @ucsi: UCSI interface
1316  */
1317 void *ucsi_get_drvdata(struct ucsi *ucsi)
1318 {
1319 	return ucsi->driver_data;
1320 }
1321 EXPORT_SYMBOL_GPL(ucsi_get_drvdata);
1322 
1323 /**
1324  * ucsi_set_drvdata - Assign private driver data pointer
1325  * @ucsi: UCSI interface
1326  * @data: Private data pointer
1327  */
1328 void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
1329 {
1330 	ucsi->driver_data = data;
1331 }
1332 EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
1333 
1334 /**
1335  * ucsi_create - Allocate UCSI instance
1336  * @dev: Device interface to the PPM (Platform Policy Manager)
1337  * @ops: I/O routines
1338  */
1339 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
1340 {
1341 	struct ucsi *ucsi;
1342 
1343 	if (!ops || !ops->read || !ops->sync_write || !ops->async_write)
1344 		return ERR_PTR(-EINVAL);
1345 
1346 	ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
1347 	if (!ucsi)
1348 		return ERR_PTR(-ENOMEM);
1349 
1350 	INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work);
1351 	mutex_init(&ucsi->ppm_lock);
1352 	ucsi->dev = dev;
1353 	ucsi->ops = ops;
1354 
1355 	return ucsi;
1356 }
1357 EXPORT_SYMBOL_GPL(ucsi_create);
1358 
1359 /**
1360  * ucsi_destroy - Free UCSI instance
1361  * @ucsi: UCSI instance to be freed
1362  */
1363 void ucsi_destroy(struct ucsi *ucsi)
1364 {
1365 	kfree(ucsi);
1366 }
1367 EXPORT_SYMBOL_GPL(ucsi_destroy);
1368 
1369 /**
1370  * ucsi_register - Register UCSI interface
1371  * @ucsi: UCSI instance
1372  */
1373 int ucsi_register(struct ucsi *ucsi)
1374 {
1375 	int ret;
1376 
1377 	ret = ucsi->ops->read(ucsi, UCSI_VERSION, &ucsi->version,
1378 			      sizeof(ucsi->version));
1379 	if (ret)
1380 		return ret;
1381 
1382 	if (!ucsi->version)
1383 		return -ENODEV;
1384 
1385 	queue_delayed_work(system_long_wq, &ucsi->work, 0);
1386 
1387 	return 0;
1388 }
1389 EXPORT_SYMBOL_GPL(ucsi_register);
1390 
1391 /**
1392  * ucsi_unregister - Unregister UCSI interface
1393  * @ucsi: UCSI interface to be unregistered
1394  *
1395  * Unregister UCSI interface that was created with ucsi_register().
1396  */
1397 void ucsi_unregister(struct ucsi *ucsi)
1398 {
1399 	u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
1400 	int i;
1401 
1402 	/* Make sure that we are not in the middle of driver initialization */
1403 	cancel_delayed_work_sync(&ucsi->work);
1404 
1405 	/* Disable notifications */
1406 	ucsi->ops->async_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
1407 
1408 	for (i = 0; i < ucsi->cap.num_connectors; i++) {
1409 		cancel_work_sync(&ucsi->connector[i].work);
1410 		ucsi_unregister_partner(&ucsi->connector[i]);
1411 		ucsi_unregister_altmodes(&ucsi->connector[i],
1412 					 UCSI_RECIPIENT_CON);
1413 		ucsi_unregister_port_psy(&ucsi->connector[i]);
1414 		if (ucsi->connector[i].wq)
1415 			destroy_workqueue(ucsi->connector[i].wq);
1416 		typec_unregister_port(ucsi->connector[i].port);
1417 	}
1418 
1419 	kfree(ucsi->connector);
1420 }
1421 EXPORT_SYMBOL_GPL(ucsi_unregister);
1422 
1423 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1424 MODULE_LICENSE("GPL v2");
1425 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");
1426