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