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
699 ret = ucsi_get_pdos(con, TYPEC_SINK, 1, caps.pdo);
700 if (ret > 0) {
701 if (ret < PDO_MAX_OBJECTS)
702 caps.pdo[ret] = 0;
703
704 caps.role = TYPEC_SINK;
705
706 cap = usb_power_delivery_register_capabilities(con->partner_pd, &caps);
707 if (IS_ERR(cap))
708 return PTR_ERR(cap);
709
710 con->partner_sink_caps = cap;
711 }
712
713 return typec_partner_set_usb_power_delivery(con->partner, con->partner_pd);
714 }
715
ucsi_unregister_partner_pdos(struct ucsi_connector * con)716 static void ucsi_unregister_partner_pdos(struct ucsi_connector *con)
717 {
718 usb_power_delivery_unregister_capabilities(con->partner_sink_caps);
719 con->partner_sink_caps = NULL;
720 usb_power_delivery_unregister_capabilities(con->partner_source_caps);
721 con->partner_source_caps = NULL;
722 usb_power_delivery_unregister(con->partner_pd);
723 con->partner_pd = NULL;
724 }
725
ucsi_pwr_opmode_change(struct ucsi_connector * con)726 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
727 {
728 switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
729 case UCSI_CONSTAT_PWR_OPMODE_PD:
730 con->rdo = con->status.request_data_obj;
731 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
732 ucsi_partner_task(con, ucsi_get_src_pdos, 30, 0);
733 ucsi_partner_task(con, ucsi_check_altmodes, 30, 0);
734 ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ);
735 break;
736 case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
737 con->rdo = 0;
738 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
739 break;
740 case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
741 con->rdo = 0;
742 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
743 break;
744 default:
745 con->rdo = 0;
746 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
747 break;
748 }
749 }
750
ucsi_register_partner(struct ucsi_connector * con)751 static int ucsi_register_partner(struct ucsi_connector *con)
752 {
753 u8 pwr_opmode = UCSI_CONSTAT_PWR_OPMODE(con->status.flags);
754 struct typec_partner_desc desc;
755 struct typec_partner *partner;
756
757 if (con->partner)
758 return 0;
759
760 memset(&desc, 0, sizeof(desc));
761
762 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
763 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
764 desc.accessory = TYPEC_ACCESSORY_DEBUG;
765 break;
766 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
767 desc.accessory = TYPEC_ACCESSORY_AUDIO;
768 break;
769 default:
770 break;
771 }
772
773 desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD;
774
775 partner = typec_register_partner(con->port, &desc);
776 if (IS_ERR(partner)) {
777 dev_err(con->ucsi->dev,
778 "con%d: failed to register partner (%ld)\n", con->num,
779 PTR_ERR(partner));
780 return PTR_ERR(partner);
781 }
782
783 con->partner = partner;
784
785 return 0;
786 }
787
ucsi_unregister_partner(struct ucsi_connector * con)788 static void ucsi_unregister_partner(struct ucsi_connector *con)
789 {
790 if (!con->partner)
791 return;
792
793 typec_set_mode(con->port, TYPEC_STATE_SAFE);
794
795 typec_partner_set_usb_power_delivery(con->partner, NULL);
796 ucsi_unregister_partner_pdos(con);
797 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
798 typec_unregister_partner(con->partner);
799 con->partner = NULL;
800 }
801
ucsi_partner_change(struct ucsi_connector * con)802 static void ucsi_partner_change(struct ucsi_connector *con)
803 {
804 enum usb_role u_role = USB_ROLE_NONE;
805 int ret;
806
807 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
808 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
809 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
810 u_role = USB_ROLE_HOST;
811 fallthrough;
812 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
813 typec_set_data_role(con->port, TYPEC_HOST);
814 break;
815 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
816 u_role = USB_ROLE_DEVICE;
817 typec_set_data_role(con->port, TYPEC_DEVICE);
818 break;
819 default:
820 break;
821 }
822
823 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
824 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
825 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
826 typec_set_mode(con->port, TYPEC_MODE_DEBUG);
827 break;
828 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
829 typec_set_mode(con->port, TYPEC_MODE_AUDIO);
830 break;
831 default:
832 if (UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) ==
833 UCSI_CONSTAT_PARTNER_FLAG_USB)
834 typec_set_mode(con->port, TYPEC_STATE_USB);
835 }
836 }
837
838 /* Only notify USB controller if partner supports USB data */
839 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
840 u_role = USB_ROLE_NONE;
841
842 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
843 if (ret)
844 dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n",
845 con->num, u_role);
846 }
847
ucsi_check_connection(struct ucsi_connector * con)848 static int ucsi_check_connection(struct ucsi_connector *con)
849 {
850 u8 prev_flags = con->status.flags;
851 u64 command;
852 int ret;
853
854 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
855 ret = ucsi_send_command(con->ucsi, command, &con->status, sizeof(con->status));
856 if (ret < 0) {
857 dev_err(con->ucsi->dev, "GET_CONNECTOR_STATUS failed (%d)\n", ret);
858 return ret;
859 }
860
861 if (con->status.flags == prev_flags)
862 return 0;
863
864 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
865 ucsi_register_partner(con);
866 ucsi_pwr_opmode_change(con);
867 ucsi_partner_change(con);
868 } else {
869 ucsi_partner_change(con);
870 ucsi_port_psy_changed(con);
871 ucsi_unregister_partner(con);
872 }
873
874 return 0;
875 }
876
ucsi_handle_connector_change(struct work_struct * work)877 static void ucsi_handle_connector_change(struct work_struct *work)
878 {
879 struct ucsi_connector *con = container_of(work, struct ucsi_connector,
880 work);
881 struct ucsi *ucsi = con->ucsi;
882 enum typec_role role;
883 u64 command;
884 int ret;
885
886 mutex_lock(&con->lock);
887
888 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
889 ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
890 if (ret < 0) {
891 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
892 __func__, ret);
893 clear_bit(EVENT_PENDING, &con->ucsi->flags);
894 goto out_unlock;
895 }
896
897 trace_ucsi_connector_change(con->num, &con->status);
898
899 role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
900
901 if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
902 typec_set_pwr_role(con->port, role);
903
904 /* Complete pending power role swap */
905 if (!completion_done(&con->complete))
906 complete(&con->complete);
907 }
908
909 if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
910 typec_set_pwr_role(con->port, role);
911 ucsi_port_psy_changed(con);
912 ucsi_partner_change(con);
913
914 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
915 ucsi_register_partner(con);
916 ucsi_partner_task(con, ucsi_check_connection, 1, HZ);
917
918 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) ==
919 UCSI_CONSTAT_PWR_OPMODE_PD)
920 ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ);
921 } else {
922 ucsi_unregister_partner(con);
923 }
924 }
925
926 if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE ||
927 con->status.change & UCSI_CONSTAT_POWER_LEVEL_CHANGE)
928 ucsi_pwr_opmode_change(con);
929
930 if (con->partner && con->status.change & UCSI_CONSTAT_PARTNER_CHANGE) {
931 ucsi_partner_change(con);
932
933 /* Complete pending data role swap */
934 if (!completion_done(&con->complete))
935 complete(&con->complete);
936 }
937
938 if (con->status.change & UCSI_CONSTAT_CAM_CHANGE)
939 ucsi_partner_task(con, ucsi_check_altmodes, 1, 0);
940
941 mutex_lock(&ucsi->ppm_lock);
942 clear_bit(EVENT_PENDING, &con->ucsi->flags);
943 ret = ucsi_acknowledge_connector_change(ucsi);
944 mutex_unlock(&ucsi->ppm_lock);
945
946 if (ret)
947 dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret);
948
949 out_unlock:
950 mutex_unlock(&con->lock);
951 }
952
953 /**
954 * ucsi_connector_change - Process Connector Change Event
955 * @ucsi: UCSI Interface
956 * @num: Connector number
957 */
ucsi_connector_change(struct ucsi * ucsi,u8 num)958 void ucsi_connector_change(struct ucsi *ucsi, u8 num)
959 {
960 struct ucsi_connector *con = &ucsi->connector[num - 1];
961
962 if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
963 dev_dbg(ucsi->dev, "Early connector change event\n");
964 return;
965 }
966
967 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
968 schedule_work(&con->work);
969 }
970 EXPORT_SYMBOL_GPL(ucsi_connector_change);
971
972 /* -------------------------------------------------------------------------- */
973
ucsi_reset_connector(struct ucsi_connector * con,bool hard)974 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
975 {
976 u64 command;
977
978 command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num);
979 command |= hard ? UCSI_CONNECTOR_RESET_HARD : 0;
980
981 return ucsi_send_command(con->ucsi, command, NULL, 0);
982 }
983
ucsi_reset_ppm(struct ucsi * ucsi)984 static int ucsi_reset_ppm(struct ucsi *ucsi)
985 {
986 u64 command;
987 unsigned long tmo;
988 u32 cci;
989 int ret;
990
991 mutex_lock(&ucsi->ppm_lock);
992
993 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
994 if (ret < 0)
995 goto out;
996
997 /*
998 * If UCSI_CCI_RESET_COMPLETE is already set we must clear
999 * the flag before we start another reset. Send a
1000 * UCSI_SET_NOTIFICATION_ENABLE command to achieve this.
1001 * Ignore a timeout and try the reset anyway if this fails.
1002 */
1003 if (cci & UCSI_CCI_RESET_COMPLETE) {
1004 command = UCSI_SET_NOTIFICATION_ENABLE;
1005 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command,
1006 sizeof(command));
1007 if (ret < 0)
1008 goto out;
1009
1010 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
1011 do {
1012 ret = ucsi->ops->read(ucsi, UCSI_CCI,
1013 &cci, sizeof(cci));
1014 if (ret < 0)
1015 goto out;
1016 if (cci & UCSI_CCI_COMMAND_COMPLETE)
1017 break;
1018 if (time_is_before_jiffies(tmo))
1019 break;
1020 msleep(20);
1021 } while (1);
1022
1023 WARN_ON(cci & UCSI_CCI_RESET_COMPLETE);
1024 }
1025
1026 command = UCSI_PPM_RESET;
1027 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command,
1028 sizeof(command));
1029 if (ret < 0)
1030 goto out;
1031
1032 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
1033
1034 do {
1035 if (time_is_before_jiffies(tmo)) {
1036 ret = -ETIMEDOUT;
1037 goto out;
1038 }
1039
1040 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
1041 if (ret)
1042 goto out;
1043
1044 /* If the PPM is still doing something else, reset it again. */
1045 if (cci & ~UCSI_CCI_RESET_COMPLETE) {
1046 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL,
1047 &command,
1048 sizeof(command));
1049 if (ret < 0)
1050 goto out;
1051 }
1052
1053 msleep(20);
1054 } while (!(cci & UCSI_CCI_RESET_COMPLETE));
1055
1056 out:
1057 mutex_unlock(&ucsi->ppm_lock);
1058 return ret;
1059 }
1060
ucsi_role_cmd(struct ucsi_connector * con,u64 command)1061 static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)
1062 {
1063 int ret;
1064
1065 ret = ucsi_send_command(con->ucsi, command, NULL, 0);
1066 if (ret == -ETIMEDOUT) {
1067 u64 c;
1068
1069 /* PPM most likely stopped responding. Resetting everything. */
1070 ucsi_reset_ppm(con->ucsi);
1071
1072 c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
1073 ucsi_send_command(con->ucsi, c, NULL, 0);
1074
1075 ucsi_reset_connector(con, true);
1076 }
1077
1078 return ret;
1079 }
1080
ucsi_dr_swap(struct typec_port * port,enum typec_data_role role)1081 static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
1082 {
1083 struct ucsi_connector *con = typec_get_drvdata(port);
1084 u8 partner_type;
1085 u64 command;
1086 int ret = 0;
1087
1088 mutex_lock(&con->lock);
1089
1090 if (!con->partner) {
1091 ret = -ENOTCONN;
1092 goto out_unlock;
1093 }
1094
1095 partner_type = UCSI_CONSTAT_PARTNER_TYPE(con->status.flags);
1096 if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
1097 role == TYPEC_DEVICE) ||
1098 (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
1099 role == TYPEC_HOST))
1100 goto out_unlock;
1101
1102 reinit_completion(&con->complete);
1103
1104 command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num);
1105 command |= UCSI_SET_UOR_ROLE(role);
1106 command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS;
1107 ret = ucsi_role_cmd(con, command);
1108 if (ret < 0)
1109 goto out_unlock;
1110
1111 mutex_unlock(&con->lock);
1112
1113 if (!wait_for_completion_timeout(&con->complete,
1114 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1115 return -ETIMEDOUT;
1116
1117 return 0;
1118
1119 out_unlock:
1120 mutex_unlock(&con->lock);
1121
1122 return ret;
1123 }
1124
ucsi_pr_swap(struct typec_port * port,enum typec_role role)1125 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
1126 {
1127 struct ucsi_connector *con = typec_get_drvdata(port);
1128 enum typec_role cur_role;
1129 u64 command;
1130 int ret = 0;
1131
1132 mutex_lock(&con->lock);
1133
1134 if (!con->partner) {
1135 ret = -ENOTCONN;
1136 goto out_unlock;
1137 }
1138
1139 cur_role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
1140
1141 if (cur_role == role)
1142 goto out_unlock;
1143
1144 reinit_completion(&con->complete);
1145
1146 command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num);
1147 command |= UCSI_SET_PDR_ROLE(role);
1148 command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS;
1149 ret = ucsi_role_cmd(con, command);
1150 if (ret < 0)
1151 goto out_unlock;
1152
1153 mutex_unlock(&con->lock);
1154
1155 if (!wait_for_completion_timeout(&con->complete,
1156 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1157 return -ETIMEDOUT;
1158
1159 mutex_lock(&con->lock);
1160
1161 /* Something has gone wrong while swapping the role */
1162 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) !=
1163 UCSI_CONSTAT_PWR_OPMODE_PD) {
1164 ucsi_reset_connector(con, true);
1165 ret = -EPROTO;
1166 }
1167
1168 out_unlock:
1169 mutex_unlock(&con->lock);
1170
1171 return ret;
1172 }
1173
1174 static const struct typec_operations ucsi_ops = {
1175 .dr_set = ucsi_dr_swap,
1176 .pr_set = ucsi_pr_swap
1177 };
1178
1179 /* Caller must call fwnode_handle_put() after use */
ucsi_find_fwnode(struct ucsi_connector * con)1180 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
1181 {
1182 struct fwnode_handle *fwnode;
1183 int i = 1;
1184
1185 device_for_each_child_node(con->ucsi->dev, fwnode)
1186 if (i++ == con->num)
1187 return fwnode;
1188 return NULL;
1189 }
1190
ucsi_register_port(struct ucsi * ucsi,struct ucsi_connector * con)1191 static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con)
1192 {
1193 struct usb_power_delivery_desc desc = { ucsi->cap.pd_version};
1194 struct usb_power_delivery_capabilities_desc pd_caps;
1195 struct usb_power_delivery_capabilities *pd_cap;
1196 struct typec_capability *cap = &con->typec_cap;
1197 enum typec_accessory *accessory = cap->accessory;
1198 enum usb_role u_role = USB_ROLE_NONE;
1199 u64 command;
1200 char *name;
1201 int ret;
1202
1203 name = kasprintf(GFP_KERNEL, "%s-con%d", dev_name(ucsi->dev), con->num);
1204 if (!name)
1205 return -ENOMEM;
1206
1207 con->wq = create_singlethread_workqueue(name);
1208 kfree(name);
1209 if (!con->wq)
1210 return -ENOMEM;
1211
1212 INIT_WORK(&con->work, ucsi_handle_connector_change);
1213 init_completion(&con->complete);
1214 mutex_init(&con->lock);
1215 INIT_LIST_HEAD(&con->partner_tasks);
1216 con->ucsi = ucsi;
1217
1218 cap->fwnode = ucsi_find_fwnode(con);
1219 con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode);
1220 if (IS_ERR(con->usb_role_sw))
1221 return dev_err_probe(ucsi->dev, PTR_ERR(con->usb_role_sw),
1222 "con%d: failed to get usb role switch\n", con->num);
1223
1224 /* Delay other interactions with the con until registration is complete */
1225 mutex_lock(&con->lock);
1226
1227 /* Get connector capability */
1228 command = UCSI_GET_CONNECTOR_CAPABILITY;
1229 command |= UCSI_CONNECTOR_NUMBER(con->num);
1230 ret = ucsi_send_command(ucsi, command, &con->cap, sizeof(con->cap));
1231 if (ret < 0)
1232 goto out_unlock;
1233
1234 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP)
1235 cap->data = TYPEC_PORT_DRD;
1236 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP)
1237 cap->data = TYPEC_PORT_DFP;
1238 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP)
1239 cap->data = TYPEC_PORT_UFP;
1240
1241 if ((con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER) &&
1242 (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER))
1243 cap->type = TYPEC_PORT_DRP;
1244 else if (con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER)
1245 cap->type = TYPEC_PORT_SRC;
1246 else if (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER)
1247 cap->type = TYPEC_PORT_SNK;
1248
1249 cap->revision = ucsi->cap.typec_version;
1250 cap->pd_revision = ucsi->cap.pd_version;
1251 cap->svdm_version = SVDM_VER_2_0;
1252 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
1253
1254 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY)
1255 *accessory++ = TYPEC_ACCESSORY_AUDIO;
1256 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
1257 *accessory = TYPEC_ACCESSORY_DEBUG;
1258
1259 cap->driver_data = con;
1260 cap->ops = &ucsi_ops;
1261
1262 ret = ucsi_register_port_psy(con);
1263 if (ret)
1264 goto out;
1265
1266 /* Register the connector */
1267 con->port = typec_register_port(ucsi->dev, cap);
1268 if (IS_ERR(con->port)) {
1269 ret = PTR_ERR(con->port);
1270 goto out;
1271 }
1272
1273 con->pd = usb_power_delivery_register(ucsi->dev, &desc);
1274
1275 ret = ucsi_get_pdos(con, TYPEC_SOURCE, 0, pd_caps.pdo);
1276 if (ret > 0) {
1277 if (ret < PDO_MAX_OBJECTS)
1278 pd_caps.pdo[ret] = 0;
1279
1280 pd_caps.role = TYPEC_SOURCE;
1281 pd_cap = usb_power_delivery_register_capabilities(con->pd, &pd_caps);
1282 if (IS_ERR(pd_cap)) {
1283 ret = PTR_ERR(pd_cap);
1284 goto out;
1285 }
1286
1287 con->port_source_caps = pd_cap;
1288 }
1289
1290 memset(&pd_caps, 0, sizeof(pd_caps));
1291 ret = ucsi_get_pdos(con, TYPEC_SINK, 0, pd_caps.pdo);
1292 if (ret > 0) {
1293 if (ret < PDO_MAX_OBJECTS)
1294 pd_caps.pdo[ret] = 0;
1295
1296 pd_caps.role = TYPEC_SINK;
1297 pd_cap = usb_power_delivery_register_capabilities(con->pd, &pd_caps);
1298 if (IS_ERR(pd_cap)) {
1299 ret = PTR_ERR(pd_cap);
1300 goto out;
1301 }
1302
1303 con->port_sink_caps = pd_cap;
1304 }
1305
1306 typec_port_set_usb_power_delivery(con->port, con->pd);
1307
1308 /* Alternate modes */
1309 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON);
1310 if (ret) {
1311 dev_err(ucsi->dev, "con%d: failed to register alt modes\n",
1312 con->num);
1313 goto out;
1314 }
1315
1316 /* Get the status */
1317 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
1318 ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
1319 if (ret < 0) {
1320 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
1321 ret = 0;
1322 goto out;
1323 }
1324 ret = 0; /* ucsi_send_command() returns length on success */
1325
1326 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1327 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1328 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1329 u_role = USB_ROLE_HOST;
1330 fallthrough;
1331 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1332 typec_set_data_role(con->port, TYPEC_HOST);
1333 break;
1334 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1335 u_role = USB_ROLE_DEVICE;
1336 typec_set_data_role(con->port, TYPEC_DEVICE);
1337 break;
1338 default:
1339 break;
1340 }
1341
1342 /* Check if there is already something connected */
1343 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1344 typec_set_pwr_role(con->port,
1345 !!(con->status.flags & UCSI_CONSTAT_PWR_DIR));
1346 ucsi_register_partner(con);
1347 ucsi_pwr_opmode_change(con);
1348 ucsi_port_psy_changed(con);
1349 }
1350
1351 /* Only notify USB controller if partner supports USB data */
1352 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
1353 u_role = USB_ROLE_NONE;
1354
1355 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1356 if (ret) {
1357 dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n",
1358 con->num, u_role);
1359 ret = 0;
1360 }
1361
1362 if (con->partner &&
1363 UCSI_CONSTAT_PWR_OPMODE(con->status.flags) ==
1364 UCSI_CONSTAT_PWR_OPMODE_PD) {
1365 ucsi_get_src_pdos(con);
1366 ucsi_check_altmodes(con);
1367 }
1368
1369 trace_ucsi_register_port(con->num, &con->status);
1370
1371 out:
1372 fwnode_handle_put(cap->fwnode);
1373 out_unlock:
1374 mutex_unlock(&con->lock);
1375
1376 if (ret && con->wq) {
1377 destroy_workqueue(con->wq);
1378 con->wq = NULL;
1379 }
1380
1381 return ret;
1382 }
1383
1384 /**
1385 * ucsi_init - Initialize UCSI interface
1386 * @ucsi: UCSI to be initialized
1387 *
1388 * Registers all ports @ucsi has and enables all notification events.
1389 */
ucsi_init(struct ucsi * ucsi)1390 static int ucsi_init(struct ucsi *ucsi)
1391 {
1392 struct ucsi_connector *con, *connector;
1393 u64 command, ntfy;
1394 u32 cci;
1395 int ret;
1396 int i;
1397
1398 /* Reset the PPM */
1399 ret = ucsi_reset_ppm(ucsi);
1400 if (ret) {
1401 dev_err(ucsi->dev, "failed to reset PPM!\n");
1402 goto err;
1403 }
1404
1405 /* Enable basic notifications */
1406 ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
1407 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1408 ret = ucsi_send_command(ucsi, command, NULL, 0);
1409 if (ret < 0)
1410 goto err_reset;
1411
1412 /* Get PPM capabilities */
1413 command = UCSI_GET_CAPABILITY;
1414 ret = ucsi_send_command(ucsi, command, &ucsi->cap, sizeof(ucsi->cap));
1415 if (ret < 0)
1416 goto err_reset;
1417
1418 if (!ucsi->cap.num_connectors) {
1419 ret = -ENODEV;
1420 goto err_reset;
1421 }
1422
1423 /* Allocate the connectors. Released in ucsi_unregister() */
1424 connector = kcalloc(ucsi->cap.num_connectors + 1, sizeof(*connector), GFP_KERNEL);
1425 if (!connector) {
1426 ret = -ENOMEM;
1427 goto err_reset;
1428 }
1429
1430 /* Register all connectors */
1431 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1432 connector[i].num = i + 1;
1433 ret = ucsi_register_port(ucsi, &connector[i]);
1434 if (ret)
1435 goto err_unregister;
1436 }
1437
1438 /* Enable all notifications */
1439 ntfy = UCSI_ENABLE_NTFY_ALL;
1440 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1441 ret = ucsi_send_command(ucsi, command, NULL, 0);
1442 if (ret < 0)
1443 goto err_unregister;
1444
1445 ucsi->connector = connector;
1446 ucsi->ntfy = ntfy;
1447
1448 mutex_lock(&ucsi->ppm_lock);
1449 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
1450 mutex_unlock(&ucsi->ppm_lock);
1451 if (ret)
1452 return ret;
1453 if (UCSI_CCI_CONNECTOR(cci))
1454 ucsi_connector_change(ucsi, UCSI_CCI_CONNECTOR(cci));
1455
1456 return 0;
1457
1458 err_unregister:
1459 for (con = connector; con->port; con++) {
1460 ucsi_unregister_partner(con);
1461 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON);
1462 ucsi_unregister_port_psy(con);
1463 if (con->wq)
1464 destroy_workqueue(con->wq);
1465
1466 usb_power_delivery_unregister_capabilities(con->port_sink_caps);
1467 con->port_sink_caps = NULL;
1468 usb_power_delivery_unregister_capabilities(con->port_source_caps);
1469 con->port_source_caps = NULL;
1470 usb_power_delivery_unregister(con->pd);
1471 con->pd = NULL;
1472 typec_unregister_port(con->port);
1473 con->port = NULL;
1474 }
1475 kfree(connector);
1476 err_reset:
1477 memset(&ucsi->cap, 0, sizeof(ucsi->cap));
1478 ucsi_reset_ppm(ucsi);
1479 err:
1480 return ret;
1481 }
1482
ucsi_resume_work(struct work_struct * work)1483 static void ucsi_resume_work(struct work_struct *work)
1484 {
1485 struct ucsi *ucsi = container_of(work, struct ucsi, resume_work);
1486 struct ucsi_connector *con;
1487 u64 command;
1488 int ret;
1489
1490 /* Restore UCSI notification enable mask after system resume */
1491 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1492 ret = ucsi_send_command(ucsi, command, NULL, 0);
1493 if (ret < 0) {
1494 dev_err(ucsi->dev, "failed to re-enable notifications (%d)\n", ret);
1495 return;
1496 }
1497
1498 for (con = ucsi->connector; con->port; con++) {
1499 mutex_lock(&con->lock);
1500 ucsi_partner_task(con, ucsi_check_connection, 1, 0);
1501 mutex_unlock(&con->lock);
1502 }
1503 }
1504
ucsi_resume(struct ucsi * ucsi)1505 int ucsi_resume(struct ucsi *ucsi)
1506 {
1507 if (ucsi->connector)
1508 queue_work(system_long_wq, &ucsi->resume_work);
1509 return 0;
1510 }
1511 EXPORT_SYMBOL_GPL(ucsi_resume);
1512
ucsi_init_work(struct work_struct * work)1513 static void ucsi_init_work(struct work_struct *work)
1514 {
1515 struct ucsi *ucsi = container_of(work, struct ucsi, work.work);
1516 int ret;
1517
1518 ret = ucsi_init(ucsi);
1519 if (ret)
1520 dev_err_probe(ucsi->dev, ret, "PPM init failed\n");
1521
1522 if (ret == -EPROBE_DEFER) {
1523 if (ucsi->work_count++ > UCSI_ROLE_SWITCH_WAIT_COUNT) {
1524 dev_err(ucsi->dev, "PPM init failed, stop trying\n");
1525 return;
1526 }
1527
1528 queue_delayed_work(system_long_wq, &ucsi->work,
1529 UCSI_ROLE_SWITCH_INTERVAL);
1530 }
1531 }
1532
1533 /**
1534 * ucsi_get_drvdata - Return private driver data pointer
1535 * @ucsi: UCSI interface
1536 */
ucsi_get_drvdata(struct ucsi * ucsi)1537 void *ucsi_get_drvdata(struct ucsi *ucsi)
1538 {
1539 return ucsi->driver_data;
1540 }
1541 EXPORT_SYMBOL_GPL(ucsi_get_drvdata);
1542
1543 /**
1544 * ucsi_set_drvdata - Assign private driver data pointer
1545 * @ucsi: UCSI interface
1546 * @data: Private data pointer
1547 */
ucsi_set_drvdata(struct ucsi * ucsi,void * data)1548 void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
1549 {
1550 ucsi->driver_data = data;
1551 }
1552 EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
1553
1554 /**
1555 * ucsi_create - Allocate UCSI instance
1556 * @dev: Device interface to the PPM (Platform Policy Manager)
1557 * @ops: I/O routines
1558 */
ucsi_create(struct device * dev,const struct ucsi_operations * ops)1559 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
1560 {
1561 struct ucsi *ucsi;
1562
1563 if (!ops || !ops->read || !ops->sync_write || !ops->async_write)
1564 return ERR_PTR(-EINVAL);
1565
1566 ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
1567 if (!ucsi)
1568 return ERR_PTR(-ENOMEM);
1569
1570 INIT_WORK(&ucsi->resume_work, ucsi_resume_work);
1571 INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work);
1572 mutex_init(&ucsi->ppm_lock);
1573 ucsi->dev = dev;
1574 ucsi->ops = ops;
1575
1576 return ucsi;
1577 }
1578 EXPORT_SYMBOL_GPL(ucsi_create);
1579
1580 /**
1581 * ucsi_destroy - Free UCSI instance
1582 * @ucsi: UCSI instance to be freed
1583 */
ucsi_destroy(struct ucsi * ucsi)1584 void ucsi_destroy(struct ucsi *ucsi)
1585 {
1586 ucsi_debugfs_unregister(ucsi);
1587 kfree(ucsi);
1588 }
1589 EXPORT_SYMBOL_GPL(ucsi_destroy);
1590
1591 /**
1592 * ucsi_register - Register UCSI interface
1593 * @ucsi: UCSI instance
1594 */
ucsi_register(struct ucsi * ucsi)1595 int ucsi_register(struct ucsi *ucsi)
1596 {
1597 int ret;
1598
1599 ret = ucsi->ops->read(ucsi, UCSI_VERSION, &ucsi->version,
1600 sizeof(ucsi->version));
1601 if (ret)
1602 return ret;
1603
1604 if (!ucsi->version)
1605 return -ENODEV;
1606
1607 /*
1608 * Version format is JJ.M.N (JJ = Major version, M = Minor version,
1609 * N = sub-minor version).
1610 */
1611 dev_dbg(ucsi->dev, "Registered UCSI interface with version %x.%x.%x",
1612 UCSI_BCD_GET_MAJOR(ucsi->version),
1613 UCSI_BCD_GET_MINOR(ucsi->version),
1614 UCSI_BCD_GET_SUBMINOR(ucsi->version));
1615
1616 queue_delayed_work(system_long_wq, &ucsi->work, 0);
1617
1618 ucsi_debugfs_register(ucsi);
1619 return 0;
1620 }
1621 EXPORT_SYMBOL_GPL(ucsi_register);
1622
1623 /**
1624 * ucsi_unregister - Unregister UCSI interface
1625 * @ucsi: UCSI interface to be unregistered
1626 *
1627 * Unregister UCSI interface that was created with ucsi_register().
1628 */
ucsi_unregister(struct ucsi * ucsi)1629 void ucsi_unregister(struct ucsi *ucsi)
1630 {
1631 u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
1632 int i;
1633
1634 /* Make sure that we are not in the middle of driver initialization */
1635 cancel_delayed_work_sync(&ucsi->work);
1636 cancel_work_sync(&ucsi->resume_work);
1637
1638 /* Disable notifications */
1639 ucsi->ops->async_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
1640
1641 if (!ucsi->connector)
1642 return;
1643
1644 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1645 cancel_work_sync(&ucsi->connector[i].work);
1646 ucsi_unregister_partner(&ucsi->connector[i]);
1647 ucsi_unregister_altmodes(&ucsi->connector[i],
1648 UCSI_RECIPIENT_CON);
1649 ucsi_unregister_port_psy(&ucsi->connector[i]);
1650
1651 if (ucsi->connector[i].wq) {
1652 struct ucsi_work *uwork;
1653
1654 mutex_lock(&ucsi->connector[i].lock);
1655 /*
1656 * queue delayed items immediately so they can execute
1657 * and free themselves before the wq is destroyed
1658 */
1659 list_for_each_entry(uwork, &ucsi->connector[i].partner_tasks, node)
1660 mod_delayed_work(ucsi->connector[i].wq, &uwork->work, 0);
1661 mutex_unlock(&ucsi->connector[i].lock);
1662 destroy_workqueue(ucsi->connector[i].wq);
1663 }
1664
1665 usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_sink_caps);
1666 ucsi->connector[i].port_sink_caps = NULL;
1667 usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_source_caps);
1668 ucsi->connector[i].port_source_caps = NULL;
1669 usb_power_delivery_unregister(ucsi->connector[i].pd);
1670 ucsi->connector[i].pd = NULL;
1671 typec_unregister_port(ucsi->connector[i].port);
1672 }
1673
1674 kfree(ucsi->connector);
1675 }
1676 EXPORT_SYMBOL_GPL(ucsi_unregister);
1677
ucsi_module_init(void)1678 static int __init ucsi_module_init(void)
1679 {
1680 ucsi_debugfs_init();
1681 return 0;
1682 }
1683 module_init(ucsi_module_init);
1684
ucsi_module_exit(void)1685 static void __exit ucsi_module_exit(void)
1686 {
1687 ucsi_debugfs_exit();
1688 }
1689 module_exit(ucsi_module_exit);
1690
1691 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1692 MODULE_LICENSE("GPL v2");
1693 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");
1694