xref: /openbmc/linux/drivers/usb/typec/ucsi/ucsi.c (revision 06b72824)
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->async_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 	/* Acknowlege 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 	switch (error) {
80 	case UCSI_ERROR_INCOMPATIBLE_PARTNER:
81 		return -EOPNOTSUPP;
82 	case UCSI_ERROR_CC_COMMUNICATION_ERR:
83 		return -ECOMM;
84 	case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
85 		return -EPROTO;
86 	case UCSI_ERROR_DEAD_BATTERY:
87 		dev_warn(ucsi->dev, "Dead battery condition!\n");
88 		return -EPERM;
89 	case UCSI_ERROR_INVALID_CON_NUM:
90 	case UCSI_ERROR_UNREGONIZED_CMD:
91 	case UCSI_ERROR_INVALID_CMD_ARGUMENT:
92 		dev_err(ucsi->dev, "possible UCSI driver bug %u\n", error);
93 		return -EINVAL;
94 	case UCSI_ERROR_OVERCURRENT:
95 		dev_warn(ucsi->dev, "Overcurrent condition\n");
96 		break;
97 	case UCSI_ERROR_PARTNER_REJECTED_SWAP:
98 		dev_warn(ucsi->dev, "Partner rejected swap\n");
99 		break;
100 	case UCSI_ERROR_HARD_RESET:
101 		dev_warn(ucsi->dev, "Hard reset occurred\n");
102 		break;
103 	case UCSI_ERROR_PPM_POLICY_CONFLICT:
104 		dev_warn(ucsi->dev, "PPM Policy conflict\n");
105 		break;
106 	case UCSI_ERROR_SWAP_REJECTED:
107 		dev_warn(ucsi->dev, "Swap rejected\n");
108 		break;
109 	case UCSI_ERROR_UNDEFINED:
110 	default:
111 		dev_err(ucsi->dev, "unknown error %u\n", error);
112 		break;
113 	}
114 
115 	return -EIO;
116 }
117 
118 static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd)
119 {
120 	u32 cci;
121 	int ret;
122 
123 	ret = ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
124 	if (ret)
125 		return ret;
126 
127 	ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
128 	if (ret)
129 		return ret;
130 
131 	if (cci & UCSI_CCI_BUSY)
132 		return -EBUSY;
133 
134 	if (!(cci & UCSI_CCI_COMMAND_COMPLETE))
135 		return -EIO;
136 
137 	if (cci & UCSI_CCI_NOT_SUPPORTED)
138 		return -EOPNOTSUPP;
139 
140 	if (cci & UCSI_CCI_ERROR) {
141 		if (cmd == UCSI_GET_ERROR_STATUS)
142 			return -EIO;
143 		return ucsi_read_error(ucsi);
144 	}
145 
146 	return UCSI_CCI_LENGTH(cci);
147 }
148 
149 static int ucsi_run_command(struct ucsi *ucsi, u64 command,
150 			    void *data, size_t size)
151 {
152 	u8 length;
153 	int ret;
154 
155 	ret = ucsi_exec_command(ucsi, command);
156 	if (ret < 0)
157 		return ret;
158 
159 	length = ret;
160 
161 	if (data) {
162 		ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, data, size);
163 		if (ret)
164 			return ret;
165 	}
166 
167 	ret = ucsi_acknowledge_command(ucsi);
168 	if (ret)
169 		return ret;
170 
171 	return length;
172 }
173 
174 int ucsi_send_command(struct ucsi *ucsi, u64 command,
175 		      void *retval, size_t size)
176 {
177 	int ret;
178 
179 	mutex_lock(&ucsi->ppm_lock);
180 	ret = ucsi_run_command(ucsi, command, retval, size);
181 	mutex_unlock(&ucsi->ppm_lock);
182 
183 	return ret;
184 }
185 EXPORT_SYMBOL_GPL(ucsi_send_command);
186 
187 int ucsi_resume(struct ucsi *ucsi)
188 {
189 	u64 command;
190 
191 	/* Restore UCSI notification enable mask after system resume */
192 	command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
193 
194 	return ucsi_send_command(ucsi, command, NULL, 0);
195 }
196 EXPORT_SYMBOL_GPL(ucsi_resume);
197 /* -------------------------------------------------------------------------- */
198 
199 void ucsi_altmode_update_active(struct ucsi_connector *con)
200 {
201 	const struct typec_altmode *altmode = NULL;
202 	u64 command;
203 	int ret;
204 	u8 cur;
205 	int i;
206 
207 	command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
208 	ret = ucsi_run_command(con->ucsi, command, &cur, sizeof(cur));
209 	if (ret < 0) {
210 		if (con->ucsi->version > 0x0100) {
211 			dev_err(con->ucsi->dev,
212 				"GET_CURRENT_CAM command failed\n");
213 			return;
214 		}
215 		cur = 0xff;
216 	}
217 
218 	if (cur < UCSI_MAX_ALTMODES)
219 		altmode = typec_altmode_get_partner(con->port_altmode[cur]);
220 
221 	for (i = 0; con->partner_altmode[i]; i++)
222 		typec_altmode_update_active(con->partner_altmode[i],
223 					    con->partner_altmode[i] == altmode);
224 }
225 
226 static u8 ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
227 {
228 	u8 mode = 1;
229 	int i;
230 
231 	for (i = 0; alt[i]; i++)
232 		if (alt[i]->svid == svid)
233 			mode++;
234 
235 	return mode;
236 }
237 
238 static int ucsi_next_altmode(struct typec_altmode **alt)
239 {
240 	int i = 0;
241 
242 	for (i = 0; i < UCSI_MAX_ALTMODES; i++)
243 		if (!alt[i])
244 			return i;
245 
246 	return -ENOENT;
247 }
248 
249 static int ucsi_register_altmode(struct ucsi_connector *con,
250 				 struct typec_altmode_desc *desc,
251 				 u8 recipient)
252 {
253 	struct typec_altmode *alt;
254 	bool override;
255 	int ret;
256 	int i;
257 
258 	override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
259 
260 	switch (recipient) {
261 	case UCSI_RECIPIENT_CON:
262 		i = ucsi_next_altmode(con->port_altmode);
263 		if (i < 0) {
264 			ret = i;
265 			goto err;
266 		}
267 
268 		desc->mode = ucsi_altmode_next_mode(con->port_altmode,
269 						    desc->svid);
270 
271 		switch (desc->svid) {
272 		case USB_TYPEC_DP_SID:
273 		case USB_TYPEC_NVIDIA_VLINK_SID:
274 			alt = ucsi_register_displayport(con, override, i, desc);
275 			break;
276 		default:
277 			alt = typec_port_register_altmode(con->port, desc);
278 			break;
279 		}
280 
281 		if (IS_ERR(alt)) {
282 			ret = PTR_ERR(alt);
283 			goto err;
284 		}
285 
286 		con->port_altmode[i] = alt;
287 		break;
288 	case UCSI_RECIPIENT_SOP:
289 		i = ucsi_next_altmode(con->partner_altmode);
290 		if (i < 0) {
291 			ret = i;
292 			goto err;
293 		}
294 
295 		desc->mode = ucsi_altmode_next_mode(con->partner_altmode,
296 						    desc->svid);
297 
298 		alt = typec_partner_register_altmode(con->partner, desc);
299 		if (IS_ERR(alt)) {
300 			ret = PTR_ERR(alt);
301 			goto err;
302 		}
303 
304 		con->partner_altmode[i] = alt;
305 		break;
306 	default:
307 		return -EINVAL;
308 	}
309 
310 	trace_ucsi_register_altmode(recipient, alt);
311 
312 	return 0;
313 
314 err:
315 	dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n",
316 		desc->svid, desc->mode);
317 
318 	return ret;
319 }
320 
321 static int
322 ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient)
323 {
324 	int max_altmodes = UCSI_MAX_ALTMODES;
325 	struct typec_altmode_desc desc;
326 	struct ucsi_altmode alt;
327 	struct ucsi_altmode orig[UCSI_MAX_ALTMODES];
328 	struct ucsi_altmode updated[UCSI_MAX_ALTMODES];
329 	struct ucsi *ucsi = con->ucsi;
330 	bool multi_dp = false;
331 	u64 command;
332 	int ret;
333 	int len;
334 	int i;
335 	int k = 0;
336 
337 	if (recipient == UCSI_RECIPIENT_CON)
338 		max_altmodes = con->ucsi->cap.num_alt_modes;
339 
340 	memset(orig, 0, sizeof(orig));
341 	memset(updated, 0, sizeof(updated));
342 
343 	/* First get all the alternate modes */
344 	for (i = 0; i < max_altmodes; i++) {
345 		memset(&alt, 0, sizeof(alt));
346 		command = UCSI_GET_ALTERNATE_MODES;
347 		command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
348 		command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
349 		command |= UCSI_GET_ALTMODE_OFFSET(i);
350 		len = ucsi_run_command(con->ucsi, command, &alt, sizeof(alt));
351 		/*
352 		 * We are collecting all altmodes first and then registering.
353 		 * Some type-C device will return zero length data beyond last
354 		 * alternate modes. We should not return if length is zero.
355 		 */
356 		if (len < 0)
357 			return len;
358 
359 		/* We got all altmodes, now break out and register them */
360 		if (!len || !alt.svid)
361 			break;
362 
363 		orig[k].mid = alt.mid;
364 		orig[k].svid = alt.svid;
365 		k++;
366 	}
367 	/*
368 	 * Update the original altmode table as some ppms may report
369 	 * multiple DP altmodes.
370 	 */
371 	if (recipient == UCSI_RECIPIENT_CON)
372 		multi_dp = ucsi->ops->update_altmodes(ucsi, orig, updated);
373 
374 	/* now register altmodes */
375 	for (i = 0; i < max_altmodes; i++) {
376 		memset(&desc, 0, sizeof(desc));
377 		if (multi_dp && recipient == UCSI_RECIPIENT_CON) {
378 			desc.svid = updated[i].svid;
379 			desc.vdo = updated[i].mid;
380 		} else {
381 			desc.svid = orig[i].svid;
382 			desc.vdo = orig[i].mid;
383 		}
384 		desc.roles = TYPEC_PORT_DRD;
385 
386 		if (!desc.svid)
387 			return 0;
388 
389 		ret = ucsi_register_altmode(con, &desc, recipient);
390 		if (ret)
391 			return ret;
392 	}
393 
394 	return 0;
395 }
396 
397 static int ucsi_register_altmodes(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[2];
402 	u64 command;
403 	int num = 1;
404 	int ret;
405 	int len;
406 	int j;
407 	int i;
408 
409 	if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
410 		return 0;
411 
412 	if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
413 		return 0;
414 
415 	if (con->ucsi->ops->update_altmodes)
416 		return ucsi_register_altmodes_nvidia(con, recipient);
417 
418 	if (recipient == UCSI_RECIPIENT_CON)
419 		max_altmodes = con->ucsi->cap.num_alt_modes;
420 
421 	for (i = 0; i < max_altmodes;) {
422 		memset(alt, 0, sizeof(alt));
423 		command = UCSI_GET_ALTERNATE_MODES;
424 		command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
425 		command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
426 		command |= UCSI_GET_ALTMODE_OFFSET(i);
427 		len = ucsi_run_command(con->ucsi, command, alt, sizeof(alt));
428 		if (len <= 0)
429 			return len;
430 
431 		/*
432 		 * This code is requesting one alt mode at a time, but some PPMs
433 		 * may still return two. If that happens both alt modes need be
434 		 * registered and the offset for the next alt mode has to be
435 		 * incremented.
436 		 */
437 		num = len / sizeof(alt[0]);
438 		i += num;
439 
440 		for (j = 0; j < num; j++) {
441 			if (!alt[j].svid)
442 				return 0;
443 
444 			memset(&desc, 0, sizeof(desc));
445 			desc.vdo = alt[j].mid;
446 			desc.svid = alt[j].svid;
447 			desc.roles = TYPEC_PORT_DRD;
448 
449 			ret = ucsi_register_altmode(con, &desc, recipient);
450 			if (ret)
451 				return ret;
452 		}
453 	}
454 
455 	return 0;
456 }
457 
458 static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
459 {
460 	const struct typec_altmode *pdev;
461 	struct typec_altmode **adev;
462 	int i = 0;
463 
464 	switch (recipient) {
465 	case UCSI_RECIPIENT_CON:
466 		adev = con->port_altmode;
467 		break;
468 	case UCSI_RECIPIENT_SOP:
469 		adev = con->partner_altmode;
470 		break;
471 	default:
472 		return;
473 	}
474 
475 	while (adev[i]) {
476 		if (recipient == UCSI_RECIPIENT_SOP &&
477 		    (adev[i]->svid == USB_TYPEC_DP_SID ||
478 			adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID)) {
479 			pdev = typec_altmode_get_partner(adev[i]);
480 			ucsi_displayport_remove_partner((void *)pdev);
481 		}
482 		typec_unregister_altmode(adev[i]);
483 		adev[i++] = NULL;
484 	}
485 }
486 
487 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
488 {
489 	switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
490 	case UCSI_CONSTAT_PWR_OPMODE_PD:
491 		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
492 		break;
493 	case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
494 		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
495 		break;
496 	case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
497 		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
498 		break;
499 	default:
500 		typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
501 		break;
502 	}
503 }
504 
505 static int ucsi_register_partner(struct ucsi_connector *con)
506 {
507 	u8 pwr_opmode = UCSI_CONSTAT_PWR_OPMODE(con->status.flags);
508 	struct typec_partner_desc desc;
509 	struct typec_partner *partner;
510 
511 	if (con->partner)
512 		return 0;
513 
514 	memset(&desc, 0, sizeof(desc));
515 
516 	switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
517 	case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
518 		desc.accessory = TYPEC_ACCESSORY_DEBUG;
519 		break;
520 	case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
521 		desc.accessory = TYPEC_ACCESSORY_AUDIO;
522 		break;
523 	default:
524 		break;
525 	}
526 
527 	desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD;
528 
529 	partner = typec_register_partner(con->port, &desc);
530 	if (IS_ERR(partner)) {
531 		dev_err(con->ucsi->dev,
532 			"con%d: failed to register partner (%ld)\n", con->num,
533 			PTR_ERR(partner));
534 		return PTR_ERR(partner);
535 	}
536 
537 	con->partner = partner;
538 
539 	return 0;
540 }
541 
542 static void ucsi_unregister_partner(struct ucsi_connector *con)
543 {
544 	if (!con->partner)
545 		return;
546 
547 	ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
548 	typec_unregister_partner(con->partner);
549 	con->partner = NULL;
550 }
551 
552 static void ucsi_partner_change(struct ucsi_connector *con)
553 {
554 	int ret;
555 
556 	if (!con->partner)
557 		return;
558 
559 	switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
560 	case UCSI_CONSTAT_PARTNER_TYPE_UFP:
561 		typec_set_data_role(con->port, TYPEC_HOST);
562 		break;
563 	case UCSI_CONSTAT_PARTNER_TYPE_DFP:
564 		typec_set_data_role(con->port, TYPEC_DEVICE);
565 		break;
566 	default:
567 		break;
568 	}
569 
570 	/* Complete pending data role swap */
571 	if (!completion_done(&con->complete))
572 		complete(&con->complete);
573 
574 	/* Can't rely on Partner Flags field. Always checking the alt modes. */
575 	ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
576 	if (ret)
577 		dev_err(con->ucsi->dev,
578 			"con%d: failed to register partner alternate modes\n",
579 			con->num);
580 	else
581 		ucsi_altmode_update_active(con);
582 }
583 
584 static void ucsi_handle_connector_change(struct work_struct *work)
585 {
586 	struct ucsi_connector *con = container_of(work, struct ucsi_connector,
587 						  work);
588 	struct ucsi *ucsi = con->ucsi;
589 	enum typec_role role;
590 	u64 command;
591 	int ret;
592 
593 	mutex_lock(&con->lock);
594 
595 	command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
596 	ret = ucsi_send_command(ucsi, command, &con->status,
597 				sizeof(con->status));
598 	if (ret < 0) {
599 		dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
600 			__func__, ret);
601 		goto out_unlock;
602 	}
603 
604 	role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
605 
606 	if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE)
607 		ucsi_pwr_opmode_change(con);
608 
609 	if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
610 		typec_set_pwr_role(con->port, role);
611 
612 		/* Complete pending power role swap */
613 		if (!completion_done(&con->complete))
614 			complete(&con->complete);
615 	}
616 
617 	if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
618 		typec_set_pwr_role(con->port, role);
619 
620 		switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
621 		case UCSI_CONSTAT_PARTNER_TYPE_UFP:
622 			typec_set_data_role(con->port, TYPEC_HOST);
623 			break;
624 		case UCSI_CONSTAT_PARTNER_TYPE_DFP:
625 			typec_set_data_role(con->port, TYPEC_DEVICE);
626 			break;
627 		default:
628 			break;
629 		}
630 
631 		if (con->status.flags & UCSI_CONSTAT_CONNECTED)
632 			ucsi_register_partner(con);
633 		else
634 			ucsi_unregister_partner(con);
635 	}
636 
637 	if (con->status.change & UCSI_CONSTAT_CAM_CHANGE) {
638 		/*
639 		 * We don't need to know the currently supported alt modes here.
640 		 * Running GET_CAM_SUPPORTED command just to make sure the PPM
641 		 * does not get stuck in case it assumes we do so.
642 		 */
643 		command = UCSI_GET_CAM_SUPPORTED;
644 		command |= UCSI_CONNECTOR_NUMBER(con->num);
645 		ucsi_run_command(con->ucsi, command, NULL, 0);
646 	}
647 
648 	if (con->status.change & UCSI_CONSTAT_PARTNER_CHANGE)
649 		ucsi_partner_change(con);
650 
651 	ret = ucsi_acknowledge_connector_change(ucsi);
652 	if (ret)
653 		dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret);
654 
655 	trace_ucsi_connector_change(con->num, &con->status);
656 
657 out_unlock:
658 	clear_bit(EVENT_PENDING, &ucsi->flags);
659 	mutex_unlock(&con->lock);
660 }
661 
662 /**
663  * ucsi_connector_change - Process Connector Change Event
664  * @ucsi: UCSI Interface
665  * @num: Connector number
666  */
667 void ucsi_connector_change(struct ucsi *ucsi, u8 num)
668 {
669 	struct ucsi_connector *con = &ucsi->connector[num - 1];
670 
671 	if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
672 		dev_dbg(ucsi->dev, "Bogus connector change event\n");
673 		return;
674 	}
675 
676 	if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
677 		schedule_work(&con->work);
678 }
679 EXPORT_SYMBOL_GPL(ucsi_connector_change);
680 
681 /* -------------------------------------------------------------------------- */
682 
683 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
684 {
685 	u64 command;
686 
687 	command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num);
688 	command |= hard ? UCSI_CONNECTOR_RESET_HARD : 0;
689 
690 	return ucsi_send_command(con->ucsi, command, NULL, 0);
691 }
692 
693 static int ucsi_reset_ppm(struct ucsi *ucsi)
694 {
695 	u64 command = UCSI_PPM_RESET;
696 	unsigned long tmo;
697 	u32 cci;
698 	int ret;
699 
700 	ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command,
701 				     sizeof(command));
702 	if (ret < 0)
703 		return ret;
704 
705 	tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
706 
707 	do {
708 		if (time_is_before_jiffies(tmo))
709 			return -ETIMEDOUT;
710 
711 		ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
712 		if (ret)
713 			return ret;
714 
715 		/* If the PPM is still doing something else, reset it again. */
716 		if (cci & ~UCSI_CCI_RESET_COMPLETE) {
717 			ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL,
718 						     &command,
719 						     sizeof(command));
720 			if (ret < 0)
721 				return ret;
722 		}
723 
724 		msleep(20);
725 	} while (!(cci & UCSI_CCI_RESET_COMPLETE));
726 
727 	return 0;
728 }
729 
730 static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)
731 {
732 	int ret;
733 
734 	ret = ucsi_send_command(con->ucsi, command, NULL, 0);
735 	if (ret == -ETIMEDOUT) {
736 		u64 c;
737 
738 		/* PPM most likely stopped responding. Resetting everything. */
739 		mutex_lock(&con->ucsi->ppm_lock);
740 		ucsi_reset_ppm(con->ucsi);
741 		mutex_unlock(&con->ucsi->ppm_lock);
742 
743 		c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
744 		ucsi_send_command(con->ucsi, c, NULL, 0);
745 
746 		ucsi_reset_connector(con, true);
747 	}
748 
749 	return ret;
750 }
751 
752 static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
753 {
754 	struct ucsi_connector *con = typec_get_drvdata(port);
755 	u8 partner_type;
756 	u64 command;
757 	int ret = 0;
758 
759 	mutex_lock(&con->lock);
760 
761 	if (!con->partner) {
762 		ret = -ENOTCONN;
763 		goto out_unlock;
764 	}
765 
766 	partner_type = UCSI_CONSTAT_PARTNER_TYPE(con->status.flags);
767 	if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
768 	     role == TYPEC_DEVICE) ||
769 	    (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
770 	     role == TYPEC_HOST))
771 		goto out_unlock;
772 
773 	command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num);
774 	command |= UCSI_SET_UOR_ROLE(role);
775 	command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS;
776 	ret = ucsi_role_cmd(con, command);
777 	if (ret < 0)
778 		goto out_unlock;
779 
780 	if (!wait_for_completion_timeout(&con->complete,
781 					msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
782 		ret = -ETIMEDOUT;
783 
784 out_unlock:
785 	mutex_unlock(&con->lock);
786 
787 	return ret < 0 ? ret : 0;
788 }
789 
790 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
791 {
792 	struct ucsi_connector *con = typec_get_drvdata(port);
793 	enum typec_role cur_role;
794 	u64 command;
795 	int ret = 0;
796 
797 	mutex_lock(&con->lock);
798 
799 	if (!con->partner) {
800 		ret = -ENOTCONN;
801 		goto out_unlock;
802 	}
803 
804 	cur_role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
805 
806 	if (cur_role == role)
807 		goto out_unlock;
808 
809 	command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num);
810 	command |= UCSI_SET_PDR_ROLE(role);
811 	command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS;
812 	ret = ucsi_role_cmd(con, command);
813 	if (ret < 0)
814 		goto out_unlock;
815 
816 	if (!wait_for_completion_timeout(&con->complete,
817 				msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS))) {
818 		ret = -ETIMEDOUT;
819 		goto out_unlock;
820 	}
821 
822 	/* Something has gone wrong while swapping the role */
823 	if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) !=
824 	    UCSI_CONSTAT_PWR_OPMODE_PD) {
825 		ucsi_reset_connector(con, true);
826 		ret = -EPROTO;
827 	}
828 
829 out_unlock:
830 	mutex_unlock(&con->lock);
831 
832 	return ret;
833 }
834 
835 static const struct typec_operations ucsi_ops = {
836 	.dr_set = ucsi_dr_swap,
837 	.pr_set = ucsi_pr_swap
838 };
839 
840 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
841 {
842 	struct fwnode_handle *fwnode;
843 	int i = 1;
844 
845 	device_for_each_child_node(con->ucsi->dev, fwnode)
846 		if (i++ == con->num)
847 			return fwnode;
848 	return NULL;
849 }
850 
851 static int ucsi_register_port(struct ucsi *ucsi, int index)
852 {
853 	struct ucsi_connector *con = &ucsi->connector[index];
854 	struct typec_capability *cap = &con->typec_cap;
855 	enum typec_accessory *accessory = cap->accessory;
856 	u64 command;
857 	int ret;
858 
859 	INIT_WORK(&con->work, ucsi_handle_connector_change);
860 	init_completion(&con->complete);
861 	mutex_init(&con->lock);
862 	con->num = index + 1;
863 	con->ucsi = ucsi;
864 
865 	/* Get connector capability */
866 	command = UCSI_GET_CONNECTOR_CAPABILITY;
867 	command |= UCSI_CONNECTOR_NUMBER(con->num);
868 	ret = ucsi_run_command(ucsi, command, &con->cap, sizeof(con->cap));
869 	if (ret < 0)
870 		return ret;
871 
872 	if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP)
873 		cap->data = TYPEC_PORT_DRD;
874 	else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP)
875 		cap->data = TYPEC_PORT_DFP;
876 	else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP)
877 		cap->data = TYPEC_PORT_UFP;
878 
879 	if ((con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER) &&
880 	    (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER))
881 		cap->type = TYPEC_PORT_DRP;
882 	else if (con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER)
883 		cap->type = TYPEC_PORT_SRC;
884 	else if (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER)
885 		cap->type = TYPEC_PORT_SNK;
886 
887 	cap->revision = ucsi->cap.typec_version;
888 	cap->pd_revision = ucsi->cap.pd_version;
889 	cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
890 
891 	if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY)
892 		*accessory++ = TYPEC_ACCESSORY_AUDIO;
893 	if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
894 		*accessory = TYPEC_ACCESSORY_DEBUG;
895 
896 	cap->fwnode = ucsi_find_fwnode(con);
897 	cap->driver_data = con;
898 	cap->ops = &ucsi_ops;
899 
900 	/* Register the connector */
901 	con->port = typec_register_port(ucsi->dev, cap);
902 	if (IS_ERR(con->port))
903 		return PTR_ERR(con->port);
904 
905 	/* Alternate modes */
906 	ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON);
907 	if (ret)
908 		dev_err(ucsi->dev, "con%d: failed to register alt modes\n",
909 			con->num);
910 
911 	/* Get the status */
912 	command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
913 	ret = ucsi_run_command(ucsi, command, &con->status,
914 			       sizeof(con->status));
915 	if (ret < 0) {
916 		dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
917 		return 0;
918 	}
919 
920 	switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
921 	case UCSI_CONSTAT_PARTNER_TYPE_UFP:
922 		typec_set_data_role(con->port, TYPEC_HOST);
923 		break;
924 	case UCSI_CONSTAT_PARTNER_TYPE_DFP:
925 		typec_set_data_role(con->port, TYPEC_DEVICE);
926 		break;
927 	default:
928 		break;
929 	}
930 
931 	/* Check if there is already something connected */
932 	if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
933 		typec_set_pwr_role(con->port,
934 				  !!(con->status.flags & UCSI_CONSTAT_PWR_DIR));
935 		ucsi_pwr_opmode_change(con);
936 		ucsi_register_partner(con);
937 	}
938 
939 	if (con->partner) {
940 		ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
941 		if (ret)
942 			dev_err(ucsi->dev,
943 				"con%d: failed to register alternate modes\n",
944 				con->num);
945 		else
946 			ucsi_altmode_update_active(con);
947 	}
948 
949 	trace_ucsi_register_port(con->num, &con->status);
950 
951 	return 0;
952 }
953 
954 /**
955  * ucsi_init - Initialize UCSI interface
956  * @ucsi: UCSI to be initialized
957  *
958  * Registers all ports @ucsi has and enables all notification events.
959  */
960 int ucsi_init(struct ucsi *ucsi)
961 {
962 	struct ucsi_connector *con;
963 	u64 command;
964 	int ret;
965 	int i;
966 
967 	mutex_lock(&ucsi->ppm_lock);
968 
969 	/* Reset the PPM */
970 	ret = ucsi_reset_ppm(ucsi);
971 	if (ret) {
972 		dev_err(ucsi->dev, "failed to reset PPM!\n");
973 		goto err;
974 	}
975 
976 	/* Enable basic notifications */
977 	ucsi->ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
978 	command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
979 	ret = ucsi_run_command(ucsi, command, NULL, 0);
980 	if (ret < 0)
981 		goto err_reset;
982 
983 	/* Get PPM capabilities */
984 	command = UCSI_GET_CAPABILITY;
985 	ret = ucsi_run_command(ucsi, command, &ucsi->cap, sizeof(ucsi->cap));
986 	if (ret < 0)
987 		goto err_reset;
988 
989 	if (!ucsi->cap.num_connectors) {
990 		ret = -ENODEV;
991 		goto err_reset;
992 	}
993 
994 	/* Allocate the connectors. Released in ucsi_unregister_ppm() */
995 	ucsi->connector = kcalloc(ucsi->cap.num_connectors + 1,
996 				  sizeof(*ucsi->connector), GFP_KERNEL);
997 	if (!ucsi->connector) {
998 		ret = -ENOMEM;
999 		goto err_reset;
1000 	}
1001 
1002 	/* Register all connectors */
1003 	for (i = 0; i < ucsi->cap.num_connectors; i++) {
1004 		ret = ucsi_register_port(ucsi, i);
1005 		if (ret)
1006 			goto err_unregister;
1007 	}
1008 
1009 	/* Enable all notifications */
1010 	ucsi->ntfy = UCSI_ENABLE_NTFY_ALL;
1011 	command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1012 	ret = ucsi_run_command(ucsi, command, NULL, 0);
1013 	if (ret < 0)
1014 		goto err_unregister;
1015 
1016 	mutex_unlock(&ucsi->ppm_lock);
1017 
1018 	return 0;
1019 
1020 err_unregister:
1021 	for (con = ucsi->connector; con->port; con++) {
1022 		ucsi_unregister_partner(con);
1023 		ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON);
1024 		typec_unregister_port(con->port);
1025 		con->port = NULL;
1026 	}
1027 
1028 err_reset:
1029 	ucsi_reset_ppm(ucsi);
1030 err:
1031 	mutex_unlock(&ucsi->ppm_lock);
1032 
1033 	return ret;
1034 }
1035 EXPORT_SYMBOL_GPL(ucsi_init);
1036 
1037 static void ucsi_init_work(struct work_struct *work)
1038 {
1039 	struct ucsi *ucsi = container_of(work, struct ucsi, work);
1040 	int ret;
1041 
1042 	ret = ucsi_init(ucsi);
1043 	if (ret)
1044 		dev_err(ucsi->dev, "PPM init failed (%d)\n", ret);
1045 }
1046 
1047 /**
1048  * ucsi_get_drvdata - Return private driver data pointer
1049  * @ucsi: UCSI interface
1050  */
1051 void *ucsi_get_drvdata(struct ucsi *ucsi)
1052 {
1053 	return ucsi->driver_data;
1054 }
1055 EXPORT_SYMBOL_GPL(ucsi_get_drvdata);
1056 
1057 /**
1058  * ucsi_get_drvdata - Assign private driver data pointer
1059  * @ucsi: UCSI interface
1060  * @data: Private data pointer
1061  */
1062 void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
1063 {
1064 	ucsi->driver_data = data;
1065 }
1066 EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
1067 
1068 /**
1069  * ucsi_create - Allocate UCSI instance
1070  * @dev: Device interface to the PPM (Platform Policy Manager)
1071  * @ops: I/O routines
1072  */
1073 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
1074 {
1075 	struct ucsi *ucsi;
1076 
1077 	if (!ops || !ops->read || !ops->sync_write || !ops->async_write)
1078 		return ERR_PTR(-EINVAL);
1079 
1080 	ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
1081 	if (!ucsi)
1082 		return ERR_PTR(-ENOMEM);
1083 
1084 	INIT_WORK(&ucsi->work, ucsi_init_work);
1085 	mutex_init(&ucsi->ppm_lock);
1086 	ucsi->dev = dev;
1087 	ucsi->ops = ops;
1088 
1089 	return ucsi;
1090 }
1091 EXPORT_SYMBOL_GPL(ucsi_create);
1092 
1093 /**
1094  * ucsi_destroy - Free UCSI instance
1095  * @ucsi: UCSI instance to be freed
1096  */
1097 void ucsi_destroy(struct ucsi *ucsi)
1098 {
1099 	kfree(ucsi);
1100 }
1101 EXPORT_SYMBOL_GPL(ucsi_destroy);
1102 
1103 /**
1104  * ucsi_register - Register UCSI interface
1105  * @ucsi: UCSI instance
1106  */
1107 int ucsi_register(struct ucsi *ucsi)
1108 {
1109 	int ret;
1110 
1111 	ret = ucsi->ops->read(ucsi, UCSI_VERSION, &ucsi->version,
1112 			      sizeof(ucsi->version));
1113 	if (ret)
1114 		return ret;
1115 
1116 	if (!ucsi->version)
1117 		return -ENODEV;
1118 
1119 	queue_work(system_long_wq, &ucsi->work);
1120 
1121 	return 0;
1122 }
1123 EXPORT_SYMBOL_GPL(ucsi_register);
1124 
1125 /**
1126  * ucsi_unregister - Unregister UCSI interface
1127  * @ucsi: UCSI interface to be unregistered
1128  *
1129  * Unregister UCSI interface that was created with ucsi_register().
1130  */
1131 void ucsi_unregister(struct ucsi *ucsi)
1132 {
1133 	u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
1134 	int i;
1135 
1136 	/* Make sure that we are not in the middle of driver initialization */
1137 	cancel_work_sync(&ucsi->work);
1138 
1139 	/* Disable notifications */
1140 	ucsi->ops->async_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
1141 
1142 	for (i = 0; i < ucsi->cap.num_connectors; i++) {
1143 		cancel_work_sync(&ucsi->connector[i].work);
1144 		ucsi_unregister_partner(&ucsi->connector[i]);
1145 		ucsi_unregister_altmodes(&ucsi->connector[i],
1146 					 UCSI_RECIPIENT_CON);
1147 		typec_unregister_port(ucsi->connector[i].port);
1148 	}
1149 
1150 	kfree(ucsi->connector);
1151 }
1152 EXPORT_SYMBOL_GPL(ucsi_unregister);
1153 
1154 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1155 MODULE_LICENSE("GPL v2");
1156 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");
1157