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