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