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