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 10000
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_notify_common(struct ucsi * ucsi,u32 cci)39 void ucsi_notify_common(struct ucsi *ucsi, u32 cci)
40 {
41 /* Ignore bogus data in CCI if busy indicator is set. */
42 if (cci & UCSI_CCI_BUSY)
43 return;
44
45 if (UCSI_CCI_CONNECTOR(cci))
46 ucsi_connector_change(ucsi, UCSI_CCI_CONNECTOR(cci));
47
48 if (cci & UCSI_CCI_ACK_COMPLETE &&
49 test_and_clear_bit(ACK_PENDING, &ucsi->flags))
50 complete(&ucsi->complete);
51
52 if (cci & UCSI_CCI_COMMAND_COMPLETE &&
53 test_and_clear_bit(COMMAND_PENDING, &ucsi->flags))
54 complete(&ucsi->complete);
55 }
56 EXPORT_SYMBOL_GPL(ucsi_notify_common);
57
ucsi_sync_control_common(struct ucsi * ucsi,u64 command,u32 * cci,void * data,size_t size)58 int ucsi_sync_control_common(struct ucsi *ucsi, u64 command, u32 *cci,
59 void *data, size_t size)
60 {
61 bool ack = UCSI_COMMAND(command) == UCSI_ACK_CC_CI;
62 int ret;
63
64 if (ack)
65 set_bit(ACK_PENDING, &ucsi->flags);
66 else
67 set_bit(COMMAND_PENDING, &ucsi->flags);
68
69 reinit_completion(&ucsi->complete);
70
71 ret = ucsi->ops->async_control(ucsi, command);
72 if (ret)
73 goto out_clear_bit;
74
75 if (!wait_for_completion_timeout(&ucsi->complete, 5 * HZ))
76 ret = -ETIMEDOUT;
77
78 out_clear_bit:
79 if (ack)
80 clear_bit(ACK_PENDING, &ucsi->flags);
81 else
82 clear_bit(COMMAND_PENDING, &ucsi->flags);
83
84 if (!ret && cci)
85 ret = ucsi->ops->read_cci(ucsi, cci);
86
87 if (!ret && data &&
88 (*cci & UCSI_CCI_COMMAND_COMPLETE))
89 ret = ucsi->ops->read_message_in(ucsi, data, size);
90
91 return ret;
92 }
93 EXPORT_SYMBOL_GPL(ucsi_sync_control_common);
94
ucsi_acknowledge(struct ucsi * ucsi,bool conn_ack)95 static int ucsi_acknowledge(struct ucsi *ucsi, bool conn_ack)
96 {
97 u64 ctrl;
98
99 ctrl = UCSI_ACK_CC_CI;
100 ctrl |= UCSI_ACK_COMMAND_COMPLETE;
101 if (conn_ack) {
102 clear_bit(EVENT_PENDING, &ucsi->flags);
103 ctrl |= UCSI_ACK_CONNECTOR_CHANGE;
104 }
105
106 return ucsi->ops->sync_control(ucsi, ctrl, NULL, NULL, 0);
107 }
108
ucsi_run_command(struct ucsi * ucsi,u64 command,u32 * cci,void * data,size_t size,bool conn_ack)109 static int ucsi_run_command(struct ucsi *ucsi, u64 command, u32 *cci,
110 void *data, size_t size, bool conn_ack)
111 {
112 int ret, err;
113
114 *cci = 0;
115
116 if (size > UCSI_MAX_DATA_LENGTH(ucsi))
117 return -EINVAL;
118
119 ret = ucsi->ops->sync_control(ucsi, command, cci, data, size);
120
121 if (*cci & UCSI_CCI_BUSY)
122 return ucsi_run_command(ucsi, UCSI_CANCEL, cci, NULL, 0, false) ?: -EBUSY;
123 if (ret)
124 return ret;
125
126 if (!(*cci & UCSI_CCI_COMMAND_COMPLETE))
127 return -EIO;
128
129 if (*cci & UCSI_CCI_NOT_SUPPORTED)
130 err = -EOPNOTSUPP;
131 else if (*cci & UCSI_CCI_ERROR)
132 err = -EIO;
133 else
134 err = 0;
135
136 /*
137 * Don't ACK connection change if there was an error.
138 */
139 ret = ucsi_acknowledge(ucsi, err ? false : conn_ack);
140 if (ret)
141 return ret;
142
143 return err ?: UCSI_CCI_LENGTH(*cci);
144 }
145
ucsi_read_error(struct ucsi * ucsi,u8 connector_num)146 static int ucsi_read_error(struct ucsi *ucsi, u8 connector_num)
147 {
148 u64 command;
149 u16 error;
150 u32 cci;
151 int ret;
152
153 command = UCSI_GET_ERROR_STATUS | UCSI_CONNECTOR_NUMBER(connector_num);
154 ret = ucsi_run_command(ucsi, command, &cci, &error, sizeof(error), false);
155 if (ret < 0)
156 return ret;
157
158 switch (error) {
159 case UCSI_ERROR_INCOMPATIBLE_PARTNER:
160 return -EOPNOTSUPP;
161 case UCSI_ERROR_CC_COMMUNICATION_ERR:
162 return -ECOMM;
163 case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
164 return -EPROTO;
165 case UCSI_ERROR_DEAD_BATTERY:
166 dev_warn(ucsi->dev, "Dead battery condition!\n");
167 return -EPERM;
168 case UCSI_ERROR_INVALID_CON_NUM:
169 case UCSI_ERROR_UNREGONIZED_CMD:
170 case UCSI_ERROR_INVALID_CMD_ARGUMENT:
171 dev_err(ucsi->dev, "possible UCSI driver bug %u\n", error);
172 return -EINVAL;
173 case UCSI_ERROR_OVERCURRENT:
174 dev_warn(ucsi->dev, "Overcurrent condition\n");
175 break;
176 case UCSI_ERROR_PARTNER_REJECTED_SWAP:
177 dev_warn(ucsi->dev, "Partner rejected swap\n");
178 break;
179 case UCSI_ERROR_HARD_RESET:
180 dev_warn(ucsi->dev, "Hard reset occurred\n");
181 break;
182 case UCSI_ERROR_PPM_POLICY_CONFLICT:
183 dev_warn(ucsi->dev, "PPM Policy conflict\n");
184 break;
185 case UCSI_ERROR_SWAP_REJECTED:
186 dev_warn(ucsi->dev, "Swap rejected\n");
187 break;
188 case UCSI_ERROR_REVERSE_CURRENT_PROTECTION:
189 dev_warn(ucsi->dev, "Reverse Current Protection detected\n");
190 break;
191 case UCSI_ERROR_SET_SINK_PATH_REJECTED:
192 dev_warn(ucsi->dev, "Set Sink Path rejected\n");
193 break;
194 case UCSI_ERROR_UNDEFINED:
195 default:
196 dev_err(ucsi->dev, "unknown error %u\n", error);
197 break;
198 }
199
200 return -EIO;
201 }
202
ucsi_send_command_common(struct ucsi * ucsi,u64 cmd,void * data,size_t size,bool conn_ack)203 static int ucsi_send_command_common(struct ucsi *ucsi, u64 cmd,
204 void *data, size_t size, bool conn_ack)
205 {
206 u8 connector_num;
207 u32 cci;
208 int ret;
209
210 if (ucsi->version > UCSI_VERSION_1_2) {
211 switch (UCSI_COMMAND(cmd)) {
212 case UCSI_GET_ALTERNATE_MODES:
213 connector_num = UCSI_GET_ALTMODE_GET_CONNECTOR_NUMBER(cmd);
214 break;
215 case UCSI_PPM_RESET:
216 case UCSI_CANCEL:
217 case UCSI_ACK_CC_CI:
218 case UCSI_SET_NOTIFICATION_ENABLE:
219 case UCSI_GET_CAPABILITY:
220 connector_num = 0;
221 break;
222 default:
223 connector_num = UCSI_DEFAULT_GET_CONNECTOR_NUMBER(cmd);
224 break;
225 }
226 } else {
227 connector_num = 0;
228 }
229
230 mutex_lock(&ucsi->ppm_lock);
231
232 ret = ucsi_run_command(ucsi, cmd, &cci, data, size, conn_ack);
233
234 if (cci & UCSI_CCI_ERROR)
235 ret = ucsi_read_error(ucsi, connector_num);
236
237 mutex_unlock(&ucsi->ppm_lock);
238 return ret;
239 }
240
ucsi_send_command(struct ucsi * ucsi,u64 command,void * data,size_t size)241 int ucsi_send_command(struct ucsi *ucsi, u64 command,
242 void *data, size_t size)
243 {
244 return ucsi_send_command_common(ucsi, command, data, size, false);
245 }
246 EXPORT_SYMBOL_GPL(ucsi_send_command);
247
248 /* -------------------------------------------------------------------------- */
249
250 struct ucsi_work {
251 struct delayed_work work;
252 struct list_head node;
253 unsigned long delay;
254 unsigned int count;
255 struct ucsi_connector *con;
256 int (*cb)(struct ucsi_connector *);
257 };
258
ucsi_poll_worker(struct work_struct * work)259 static void ucsi_poll_worker(struct work_struct *work)
260 {
261 struct ucsi_work *uwork = container_of(work, struct ucsi_work, work.work);
262 struct ucsi_connector *con = uwork->con;
263 int ret;
264
265 mutex_lock(&con->lock);
266
267 if (!con->partner) {
268 list_del(&uwork->node);
269 mutex_unlock(&con->lock);
270 kfree(uwork);
271 return;
272 }
273
274 ret = uwork->cb(con);
275
276 if (uwork->count-- && (ret == -EBUSY || ret == -ETIMEDOUT)) {
277 queue_delayed_work(con->wq, &uwork->work, uwork->delay);
278 } else {
279 list_del(&uwork->node);
280 kfree(uwork);
281 }
282
283 mutex_unlock(&con->lock);
284 }
285
ucsi_partner_task(struct ucsi_connector * con,int (* cb)(struct ucsi_connector *),int retries,unsigned long delay)286 static int ucsi_partner_task(struct ucsi_connector *con,
287 int (*cb)(struct ucsi_connector *),
288 int retries, unsigned long delay)
289 {
290 struct ucsi_work *uwork;
291
292 if (!con->partner)
293 return 0;
294
295 uwork = kzalloc(sizeof(*uwork), GFP_KERNEL);
296 if (!uwork)
297 return -ENOMEM;
298
299 INIT_DELAYED_WORK(&uwork->work, ucsi_poll_worker);
300 uwork->count = retries;
301 uwork->delay = delay;
302 uwork->con = con;
303 uwork->cb = cb;
304
305 list_add_tail(&uwork->node, &con->partner_tasks);
306 queue_delayed_work(con->wq, &uwork->work, delay);
307
308 return 0;
309 }
310
311 /* -------------------------------------------------------------------------- */
312
ucsi_altmode_update_active(struct ucsi_connector * con)313 void ucsi_altmode_update_active(struct ucsi_connector *con)
314 {
315 const struct typec_altmode *altmode = NULL;
316 u64 command;
317 int ret;
318 u8 cur;
319 int i;
320
321 command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
322 ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur));
323 if (ret < 0) {
324 if (con->ucsi->version > 0x0100) {
325 dev_err(con->ucsi->dev,
326 "GET_CURRENT_CAM command failed\n");
327 return;
328 }
329 cur = 0xff;
330 }
331
332 if (cur < UCSI_MAX_ALTMODES)
333 altmode = typec_altmode_get_partner(con->port_altmode[cur]);
334
335 for (i = 0; con->partner_altmode[i]; i++)
336 typec_altmode_update_active(con->partner_altmode[i],
337 con->partner_altmode[i] == altmode);
338 }
339
ucsi_altmode_next_mode(struct typec_altmode ** alt,u16 svid)340 static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
341 {
342 u8 mode = 1;
343 int i;
344
345 for (i = 0; alt[i]; i++) {
346 if (i > MODE_DISCOVERY_MAX)
347 return -ERANGE;
348
349 if (alt[i]->svid == svid)
350 mode++;
351 }
352
353 return mode;
354 }
355
ucsi_next_altmode(struct typec_altmode ** alt)356 static int ucsi_next_altmode(struct typec_altmode **alt)
357 {
358 int i = 0;
359
360 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
361 if (!alt[i])
362 return i;
363
364 return -ENOENT;
365 }
366
ucsi_get_num_altmode(struct typec_altmode ** alt)367 static int ucsi_get_num_altmode(struct typec_altmode **alt)
368 {
369 int i;
370
371 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
372 if (!alt[i])
373 break;
374
375 return i;
376 }
377
ucsi_register_altmode(struct ucsi_connector * con,struct typec_altmode_desc * desc,u8 recipient)378 static int ucsi_register_altmode(struct ucsi_connector *con,
379 struct typec_altmode_desc *desc,
380 u8 recipient)
381 {
382 struct typec_altmode *alt;
383 bool override;
384 int ret;
385 int i;
386
387 override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
388
389 switch (recipient) {
390 case UCSI_RECIPIENT_CON:
391 i = ucsi_next_altmode(con->port_altmode);
392 if (i < 0) {
393 ret = i;
394 goto err;
395 }
396
397 ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid);
398 if (ret < 0)
399 return ret;
400
401 desc->mode = ret;
402
403 switch (desc->svid) {
404 case USB_TYPEC_DP_SID:
405 alt = ucsi_register_displayport(con, override, i, desc);
406 break;
407 case USB_TYPEC_NVIDIA_VLINK_SID:
408 if (desc->vdo == USB_TYPEC_NVIDIA_VLINK_DBG_VDO)
409 alt = typec_port_register_altmode(con->port,
410 desc);
411 else
412 alt = ucsi_register_displayport(con, override,
413 i, desc);
414 break;
415 default:
416 alt = typec_port_register_altmode(con->port, desc);
417 break;
418 }
419
420 if (IS_ERR(alt)) {
421 ret = PTR_ERR(alt);
422 goto err;
423 }
424
425 con->port_altmode[i] = alt;
426 break;
427 case UCSI_RECIPIENT_SOP:
428 i = ucsi_next_altmode(con->partner_altmode);
429 if (i < 0) {
430 ret = i;
431 goto err;
432 }
433
434 ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid);
435 if (ret < 0)
436 return ret;
437
438 desc->mode = ret;
439
440 alt = typec_partner_register_altmode(con->partner, desc);
441 if (IS_ERR(alt)) {
442 ret = PTR_ERR(alt);
443 goto err;
444 }
445
446 con->partner_altmode[i] = alt;
447 break;
448 case UCSI_RECIPIENT_SOP_P:
449 i = ucsi_next_altmode(con->plug_altmode);
450 if (i < 0) {
451 ret = i;
452 goto err;
453 }
454
455 ret = ucsi_altmode_next_mode(con->plug_altmode, desc->svid);
456 if (ret < 0)
457 return ret;
458
459 desc->mode = ret;
460
461 alt = typec_plug_register_altmode(con->plug, desc);
462 if (IS_ERR(alt)) {
463 ret = PTR_ERR(alt);
464 goto err;
465 }
466
467 con->plug_altmode[i] = alt;
468 break;
469 default:
470 return -EINVAL;
471 }
472
473 trace_ucsi_register_altmode(recipient, alt);
474
475 return 0;
476
477 err:
478 dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n",
479 desc->svid, desc->mode);
480
481 return ret;
482 }
483
484 static int
ucsi_register_altmodes_nvidia(struct ucsi_connector * con,u8 recipient)485 ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient)
486 {
487 int max_altmodes = UCSI_MAX_ALTMODES;
488 struct typec_altmode_desc desc;
489 struct ucsi_altmode alt;
490 struct ucsi_altmode orig[UCSI_MAX_ALTMODES];
491 struct ucsi_altmode updated[UCSI_MAX_ALTMODES];
492 struct ucsi *ucsi = con->ucsi;
493 bool multi_dp = false;
494 u64 command;
495 int ret;
496 int len;
497 int i;
498 int k = 0;
499
500 if (recipient == UCSI_RECIPIENT_CON)
501 max_altmodes = con->ucsi->cap.num_alt_modes;
502
503 memset(orig, 0, sizeof(orig));
504 memset(updated, 0, sizeof(updated));
505
506 /* First get all the alternate modes */
507 for (i = 0; i < max_altmodes; i++) {
508 memset(&alt, 0, sizeof(alt));
509 command = UCSI_GET_ALTERNATE_MODES;
510 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
511 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
512 command |= UCSI_GET_ALTMODE_OFFSET(i);
513 len = ucsi_send_command(con->ucsi, command, &alt, sizeof(alt));
514 /*
515 * We are collecting all altmodes first and then registering.
516 * Some type-C device will return zero length data beyond last
517 * alternate modes. We should not return if length is zero.
518 */
519 if (len < 0)
520 return len;
521
522 /* We got all altmodes, now break out and register them */
523 if (!len || !alt.svid)
524 break;
525
526 orig[k].mid = alt.mid;
527 orig[k].svid = alt.svid;
528 k++;
529 }
530 /*
531 * Update the original altmode table as some ppms may report
532 * multiple DP altmodes.
533 */
534 multi_dp = ucsi->ops->update_altmodes(ucsi, recipient, orig, updated);
535
536 /* now register altmodes */
537 for (i = 0; i < max_altmodes; i++) {
538 memset(&desc, 0, sizeof(desc));
539 if (multi_dp) {
540 desc.svid = updated[i].svid;
541 desc.vdo = updated[i].mid;
542 } else {
543 desc.svid = orig[i].svid;
544 desc.vdo = orig[i].mid;
545 }
546 desc.roles = TYPEC_PORT_DRD;
547
548 if (!desc.svid)
549 return 0;
550
551 ret = ucsi_register_altmode(con, &desc, recipient);
552 if (ret)
553 return ret;
554 }
555
556 return 0;
557 }
558
ucsi_register_altmodes(struct ucsi_connector * con,u8 recipient)559 static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
560 {
561 int max_altmodes = UCSI_MAX_ALTMODES;
562 struct typec_altmode_desc desc;
563 struct ucsi_altmode alt[2];
564 u64 command;
565 int num;
566 int ret;
567 int len;
568 int j;
569 int i;
570
571 if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
572 return 0;
573
574 if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
575 return 0;
576
577 if (con->ucsi->ops->update_altmodes)
578 return ucsi_register_altmodes_nvidia(con, recipient);
579
580 if (recipient == UCSI_RECIPIENT_CON)
581 max_altmodes = con->ucsi->cap.num_alt_modes;
582
583 for (i = 0; i < max_altmodes;) {
584 memset(alt, 0, sizeof(alt));
585 command = UCSI_GET_ALTERNATE_MODES;
586 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
587 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
588 command |= UCSI_GET_ALTMODE_OFFSET(i);
589 len = ucsi_send_command(con->ucsi, command, alt, sizeof(alt));
590 if (len == -EBUSY)
591 continue;
592 if (len <= 0)
593 return len;
594
595 /*
596 * This code is requesting one alt mode at a time, but some PPMs
597 * may still return two. If that happens both alt modes need be
598 * registered and the offset for the next alt mode has to be
599 * incremented.
600 */
601 num = len / sizeof(alt[0]);
602 i += num;
603
604 for (j = 0; j < num; j++) {
605 if (!alt[j].svid)
606 return 0;
607
608 memset(&desc, 0, sizeof(desc));
609 desc.vdo = alt[j].mid;
610 desc.svid = alt[j].svid;
611 desc.roles = TYPEC_PORT_DRD;
612
613 ret = ucsi_register_altmode(con, &desc, recipient);
614 if (ret)
615 return ret;
616 }
617 }
618
619 return 0;
620 }
621
ucsi_unregister_altmodes(struct ucsi_connector * con,u8 recipient)622 static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
623 {
624 const struct typec_altmode *pdev;
625 struct typec_altmode **adev;
626 int i = 0;
627
628 switch (recipient) {
629 case UCSI_RECIPIENT_CON:
630 adev = con->port_altmode;
631 break;
632 case UCSI_RECIPIENT_SOP:
633 adev = con->partner_altmode;
634 break;
635 case UCSI_RECIPIENT_SOP_P:
636 adev = con->plug_altmode;
637 break;
638 default:
639 return;
640 }
641
642 while (adev[i]) {
643 if (recipient == UCSI_RECIPIENT_SOP &&
644 (adev[i]->svid == USB_TYPEC_DP_SID ||
645 (adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID &&
646 adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) {
647 pdev = typec_altmode_get_partner(adev[i]);
648 ucsi_displayport_remove_partner((void *)pdev);
649 }
650 typec_unregister_altmode(adev[i]);
651 adev[i++] = NULL;
652 }
653 }
654
ucsi_get_connector_status(struct ucsi_connector * con,bool conn_ack)655 static int ucsi_get_connector_status(struct ucsi_connector *con, bool conn_ack)
656 {
657 u64 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
658 size_t size = min(sizeof(con->status),
659 UCSI_MAX_DATA_LENGTH(con->ucsi));
660 int ret;
661
662 ret = ucsi_send_command_common(con->ucsi, command, &con->status, size, conn_ack);
663
664 return ret < 0 ? ret : 0;
665 }
666
ucsi_read_pdos(struct ucsi_connector * con,enum typec_role role,int is_partner,u32 * pdos,int offset,int num_pdos)667 static int ucsi_read_pdos(struct ucsi_connector *con,
668 enum typec_role role, int is_partner,
669 u32 *pdos, int offset, int num_pdos)
670 {
671 struct ucsi *ucsi = con->ucsi;
672 u64 command;
673 int ret;
674
675 if (is_partner &&
676 ucsi->quirks & UCSI_NO_PARTNER_PDOS &&
677 (UCSI_CONSTAT(con, PWR_DIR) || !is_source(role)))
678 return 0;
679
680 command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num);
681 command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner);
682 command |= UCSI_GET_PDOS_PDO_OFFSET(offset);
683 command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1);
684 command |= is_source(role) ? UCSI_GET_PDOS_SRC_PDOS : 0;
685 ret = ucsi_send_command(ucsi, command, pdos + offset,
686 num_pdos * sizeof(u32));
687 if (ret < 0 && ret != -ETIMEDOUT)
688 dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret);
689
690 return ret;
691 }
692
ucsi_get_pdos(struct ucsi_connector * con,enum typec_role role,int is_partner,u32 * pdos)693 static int ucsi_get_pdos(struct ucsi_connector *con, enum typec_role role,
694 int is_partner, u32 *pdos)
695 {
696 struct ucsi *ucsi = con->ucsi;
697 u8 num_pdos;
698 int ret;
699
700 if (!(ucsi->cap.features & UCSI_CAP_PDO_DETAILS))
701 return 0;
702
703 /* UCSI max payload means only getting at most 4 PDOs at a time */
704 ret = ucsi_read_pdos(con, role, is_partner, pdos, 0, UCSI_MAX_PDOS);
705 if (ret < 0)
706 return ret;
707
708 num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */
709 if (num_pdos < UCSI_MAX_PDOS)
710 return num_pdos;
711
712 /* get the remaining PDOs, if any */
713 ret = ucsi_read_pdos(con, role, is_partner, pdos, UCSI_MAX_PDOS,
714 PDO_MAX_OBJECTS - UCSI_MAX_PDOS);
715 if (ret < 0)
716 return ret;
717
718 return ret / sizeof(u32) + num_pdos;
719 }
720
ucsi_get_src_pdos(struct ucsi_connector * con)721 static int ucsi_get_src_pdos(struct ucsi_connector *con)
722 {
723 int ret;
724
725 ret = ucsi_get_pdos(con, TYPEC_SOURCE, 1, con->src_pdos);
726 if (ret < 0)
727 return ret;
728
729 con->num_pdos = ret;
730
731 ucsi_port_psy_changed(con);
732
733 return ret;
734 }
735
ucsi_get_pd_caps(struct ucsi_connector * con,enum typec_role role,bool is_partner)736 static struct usb_power_delivery_capabilities *ucsi_get_pd_caps(struct ucsi_connector *con,
737 enum typec_role role,
738 bool is_partner)
739 {
740 struct usb_power_delivery_capabilities_desc pd_caps;
741 int ret;
742
743 ret = ucsi_get_pdos(con, role, is_partner, pd_caps.pdo);
744 if (ret <= 0)
745 return ERR_PTR(ret);
746
747 if (ret < PDO_MAX_OBJECTS)
748 pd_caps.pdo[ret] = 0;
749
750 pd_caps.role = role;
751
752 return usb_power_delivery_register_capabilities(is_partner ? con->partner_pd : con->pd,
753 &pd_caps);
754 }
755
ucsi_get_pd_message(struct ucsi_connector * con,u8 recipient,size_t bytes,void * data,u8 type)756 static int ucsi_get_pd_message(struct ucsi_connector *con, u8 recipient,
757 size_t bytes, void *data, u8 type)
758 {
759 size_t len = min(bytes, UCSI_MAX_DATA_LENGTH(con->ucsi));
760 u64 command;
761 u8 offset;
762 int ret;
763
764 for (offset = 0; offset < bytes; offset += len) {
765 len = min(len, bytes - offset);
766
767 command = UCSI_COMMAND(UCSI_GET_PD_MESSAGE) | UCSI_CONNECTOR_NUMBER(con->num);
768 command |= UCSI_GET_PD_MESSAGE_RECIPIENT(recipient);
769 command |= UCSI_GET_PD_MESSAGE_OFFSET(offset);
770 command |= UCSI_GET_PD_MESSAGE_BYTES(len);
771 command |= UCSI_GET_PD_MESSAGE_TYPE(type);
772
773 ret = ucsi_send_command(con->ucsi, command, data + offset, len);
774 if (ret < 0)
775 return ret;
776 }
777
778 return 0;
779 }
780
ucsi_get_partner_identity(struct ucsi_connector * con)781 static int ucsi_get_partner_identity(struct ucsi_connector *con)
782 {
783 u32 vdo[7] = {};
784 int ret;
785
786 ret = ucsi_get_pd_message(con, UCSI_RECIPIENT_SOP, sizeof(vdo), vdo,
787 UCSI_GET_PD_MESSAGE_TYPE_IDENTITY);
788 if (ret < 0)
789 return ret;
790
791 /* VDM Header is not part of struct usb_pd_identity, so dropping it. */
792 con->partner_identity = *(struct usb_pd_identity *)&vdo[1];
793
794 ret = typec_partner_set_identity(con->partner);
795 if (ret < 0)
796 dev_err(con->ucsi->dev, "Failed to set partner identity (%d)\n", ret);
797
798 return ret;
799 }
800
ucsi_get_cable_identity(struct ucsi_connector * con)801 static int ucsi_get_cable_identity(struct ucsi_connector *con)
802 {
803 u32 vdo[7] = {};
804 int ret;
805
806 ret = ucsi_get_pd_message(con, UCSI_RECIPIENT_SOP_P, sizeof(vdo), vdo,
807 UCSI_GET_PD_MESSAGE_TYPE_IDENTITY);
808 if (ret < 0)
809 return ret;
810
811 con->cable_identity = *(struct usb_pd_identity *)&vdo[1];
812
813 ret = typec_cable_set_identity(con->cable);
814 if (ret < 0)
815 dev_err(con->ucsi->dev, "Failed to set cable identity (%d)\n", ret);
816
817 return ret;
818 }
819
ucsi_check_altmodes(struct ucsi_connector * con)820 static int ucsi_check_altmodes(struct ucsi_connector *con)
821 {
822 int ret, num_partner_am;
823
824 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
825 if (ret && ret != -ETIMEDOUT)
826 dev_err(con->ucsi->dev,
827 "con%d: failed to register partner alt modes (%d)\n",
828 con->num, ret);
829
830 /* Ignoring the errors in this case. */
831 if (con->partner_altmode[0]) {
832 num_partner_am = ucsi_get_num_altmode(con->partner_altmode);
833 typec_partner_set_num_altmodes(con->partner, num_partner_am);
834 ucsi_altmode_update_active(con);
835 return 0;
836 } else {
837 typec_partner_set_num_altmodes(con->partner, 0);
838 }
839
840 return ret;
841 }
842
ucsi_register_device_pdos(struct ucsi_connector * con)843 static void ucsi_register_device_pdos(struct ucsi_connector *con)
844 {
845 struct ucsi *ucsi = con->ucsi;
846 struct usb_power_delivery_desc desc = { ucsi->cap.pd_version };
847 struct usb_power_delivery_capabilities *pd_cap;
848
849 if (con->pd)
850 return;
851
852 con->pd = usb_power_delivery_register(ucsi->dev, &desc);
853
854 pd_cap = ucsi_get_pd_caps(con, TYPEC_SOURCE, false);
855 if (!IS_ERR(pd_cap))
856 con->port_source_caps = pd_cap;
857
858 pd_cap = ucsi_get_pd_caps(con, TYPEC_SINK, false);
859 if (!IS_ERR(pd_cap))
860 con->port_sink_caps = pd_cap;
861
862 typec_port_set_usb_power_delivery(con->port, con->pd);
863 }
864
ucsi_register_partner_pdos(struct ucsi_connector * con)865 static int ucsi_register_partner_pdos(struct ucsi_connector *con)
866 {
867 struct usb_power_delivery_desc desc = { con->ucsi->cap.pd_version };
868 struct usb_power_delivery_capabilities *cap;
869
870 if (con->partner_pd)
871 return 0;
872
873 con->partner_pd = typec_partner_usb_power_delivery_register(con->partner, &desc);
874 if (IS_ERR(con->partner_pd))
875 return PTR_ERR(con->partner_pd);
876
877 cap = ucsi_get_pd_caps(con, TYPEC_SOURCE, true);
878 if (IS_ERR(cap))
879 return PTR_ERR(cap);
880
881 con->partner_source_caps = cap;
882
883 cap = ucsi_get_pd_caps(con, TYPEC_SINK, true);
884 if (IS_ERR(cap))
885 return PTR_ERR(cap);
886
887 con->partner_sink_caps = cap;
888
889 return typec_partner_set_usb_power_delivery(con->partner, con->partner_pd);
890 }
891
ucsi_unregister_partner_pdos(struct ucsi_connector * con)892 static void ucsi_unregister_partner_pdos(struct ucsi_connector *con)
893 {
894 usb_power_delivery_unregister_capabilities(con->partner_sink_caps);
895 con->partner_sink_caps = NULL;
896 usb_power_delivery_unregister_capabilities(con->partner_source_caps);
897 con->partner_source_caps = NULL;
898 usb_power_delivery_unregister(con->partner_pd);
899 con->partner_pd = NULL;
900 }
901
ucsi_register_plug(struct ucsi_connector * con)902 static int ucsi_register_plug(struct ucsi_connector *con)
903 {
904 struct typec_plug *plug;
905 struct typec_plug_desc desc = {.index = TYPEC_PLUG_SOP_P};
906
907 plug = typec_register_plug(con->cable, &desc);
908 if (IS_ERR(plug)) {
909 dev_err(con->ucsi->dev,
910 "con%d: failed to register plug (%ld)\n", con->num,
911 PTR_ERR(plug));
912 return PTR_ERR(plug);
913 }
914
915 con->plug = plug;
916 return 0;
917 }
918
ucsi_unregister_plug(struct ucsi_connector * con)919 static void ucsi_unregister_plug(struct ucsi_connector *con)
920 {
921 if (!con->plug)
922 return;
923
924 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP_P);
925 typec_unregister_plug(con->plug);
926 con->plug = NULL;
927 }
928
ucsi_register_cable(struct ucsi_connector * con)929 static int ucsi_register_cable(struct ucsi_connector *con)
930 {
931 struct ucsi_cable_property cable_prop;
932 struct typec_cable *cable;
933 struct typec_cable_desc desc = {};
934 u64 command;
935 int ret;
936
937 command = UCSI_GET_CABLE_PROPERTY | UCSI_CONNECTOR_NUMBER(con->num);
938 ret = ucsi_send_command(con->ucsi, command, &cable_prop, sizeof(cable_prop));
939 if (ret < 0) {
940 dev_err(con->ucsi->dev, "GET_CABLE_PROPERTY failed (%d)\n", ret);
941 return ret;
942 }
943
944 switch (UCSI_CABLE_PROP_FLAG_PLUG_TYPE(cable_prop.flags)) {
945 case UCSI_CABLE_PROPERTY_PLUG_TYPE_A:
946 desc.type = USB_PLUG_TYPE_A;
947 break;
948 case UCSI_CABLE_PROPERTY_PLUG_TYPE_B:
949 desc.type = USB_PLUG_TYPE_B;
950 break;
951 case UCSI_CABLE_PROPERTY_PLUG_TYPE_C:
952 desc.type = USB_PLUG_TYPE_C;
953 break;
954 default:
955 desc.type = USB_PLUG_NONE;
956 break;
957 }
958
959 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
960 desc.identity = &con->cable_identity;
961 desc.active = !!(UCSI_CABLE_PROP_FLAG_ACTIVE_CABLE & cable_prop.flags);
962
963 if (con->ucsi->version >= UCSI_VERSION_2_1)
964 desc.pd_revision = UCSI_CABLE_PROP_FLAG_PD_MAJOR_REV_AS_BCD(cable_prop.flags);
965
966 cable = typec_register_cable(con->port, &desc);
967 if (IS_ERR(cable)) {
968 dev_err(con->ucsi->dev,
969 "con%d: failed to register cable (%ld)\n", con->num,
970 PTR_ERR(cable));
971 return PTR_ERR(cable);
972 }
973
974 con->cable = cable;
975 return 0;
976 }
977
ucsi_unregister_cable(struct ucsi_connector * con)978 static void ucsi_unregister_cable(struct ucsi_connector *con)
979 {
980 if (!con->cable)
981 return;
982
983 ucsi_unregister_plug(con);
984 typec_unregister_cable(con->cable);
985 memset(&con->cable_identity, 0, sizeof(con->cable_identity));
986 con->cable = NULL;
987 }
988
ucsi_check_connector_capability(struct ucsi_connector * con)989 static int ucsi_check_connector_capability(struct ucsi_connector *con)
990 {
991 u64 pd_revision;
992 u64 command;
993 int ret;
994
995 if (!con->partner || con->ucsi->version < UCSI_VERSION_2_1)
996 return 0;
997
998 command = UCSI_GET_CONNECTOR_CAPABILITY | UCSI_CONNECTOR_NUMBER(con->num);
999 ret = ucsi_send_command(con->ucsi, command, &con->cap, sizeof(con->cap));
1000 if (ret < 0) {
1001 dev_err(con->ucsi->dev, "GET_CONNECTOR_CAPABILITY failed (%d)\n", ret);
1002 return ret;
1003 }
1004
1005 pd_revision = UCSI_CONCAP(con, PARTNER_PD_REVISION_V2_1);
1006 typec_partner_set_pd_revision(con->partner, UCSI_SPEC_REVISION_TO_BCD(pd_revision));
1007
1008 return ret;
1009 }
1010
ucsi_pwr_opmode_change(struct ucsi_connector * con)1011 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
1012 {
1013 switch (UCSI_CONSTAT(con, PWR_OPMODE)) {
1014 case UCSI_CONSTAT_PWR_OPMODE_PD:
1015 con->rdo = UCSI_CONSTAT(con, RDO);
1016 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
1017 ucsi_partner_task(con, ucsi_get_src_pdos, 30, 0);
1018 ucsi_partner_task(con, ucsi_check_altmodes, 30, HZ);
1019 ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ);
1020 ucsi_partner_task(con, ucsi_check_connector_capability, 1, HZ);
1021 break;
1022 case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
1023 con->rdo = 0;
1024 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
1025 break;
1026 case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
1027 con->rdo = 0;
1028 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
1029 break;
1030 default:
1031 con->rdo = 0;
1032 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
1033 break;
1034 }
1035 }
1036
ucsi_register_partner(struct ucsi_connector * con)1037 static int ucsi_register_partner(struct ucsi_connector *con)
1038 {
1039 u8 pwr_opmode = UCSI_CONSTAT(con, PWR_OPMODE);
1040 struct typec_partner_desc desc;
1041 struct typec_partner *partner;
1042
1043 if (con->partner)
1044 return 0;
1045
1046 memset(&desc, 0, sizeof(desc));
1047
1048 switch (UCSI_CONSTAT(con, PARTNER_TYPE)) {
1049 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
1050 desc.accessory = TYPEC_ACCESSORY_DEBUG;
1051 break;
1052 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
1053 desc.accessory = TYPEC_ACCESSORY_AUDIO;
1054 break;
1055 default:
1056 break;
1057 }
1058
1059 if (pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD)
1060 ucsi_register_device_pdos(con);
1061
1062 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
1063 desc.identity = &con->partner_identity;
1064 desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD;
1065
1066 if (con->ucsi->version >= UCSI_VERSION_2_1) {
1067 u64 pd_revision = UCSI_CONCAP(con, PARTNER_PD_REVISION_V2_1);
1068 desc.pd_revision = UCSI_SPEC_REVISION_TO_BCD(pd_revision);
1069 }
1070
1071 partner = typec_register_partner(con->port, &desc);
1072 if (IS_ERR(partner)) {
1073 dev_err(con->ucsi->dev,
1074 "con%d: failed to register partner (%ld)\n", con->num,
1075 PTR_ERR(partner));
1076 return PTR_ERR(partner);
1077 }
1078
1079 con->partner = partner;
1080
1081 if (con->ucsi->version >= UCSI_VERSION_3_0 &&
1082 UCSI_CONSTAT(con, PARTNER_FLAG_USB4_GEN4))
1083 typec_partner_set_usb_mode(partner, USB_MODE_USB4);
1084 else if (con->ucsi->version >= UCSI_VERSION_2_0 &&
1085 UCSI_CONSTAT(con, PARTNER_FLAG_USB4_GEN3))
1086 typec_partner_set_usb_mode(partner, USB_MODE_USB4);
1087
1088 return 0;
1089 }
1090
ucsi_unregister_partner(struct ucsi_connector * con)1091 static void ucsi_unregister_partner(struct ucsi_connector *con)
1092 {
1093 if (!con->partner)
1094 return;
1095
1096 typec_set_mode(con->port, TYPEC_STATE_SAFE);
1097
1098 typec_partner_set_usb_power_delivery(con->partner, NULL);
1099 ucsi_unregister_partner_pdos(con);
1100 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
1101 ucsi_unregister_cable(con);
1102 typec_unregister_partner(con->partner);
1103 memset(&con->partner_identity, 0, sizeof(con->partner_identity));
1104 con->partner = NULL;
1105 }
1106
ucsi_partner_change(struct ucsi_connector * con)1107 static void ucsi_partner_change(struct ucsi_connector *con)
1108 {
1109 enum usb_role u_role = USB_ROLE_NONE;
1110 int ret;
1111
1112 switch (UCSI_CONSTAT(con, PARTNER_TYPE)) {
1113 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1114 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1115 u_role = USB_ROLE_HOST;
1116 fallthrough;
1117 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1118 typec_set_data_role(con->port, TYPEC_HOST);
1119 break;
1120 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1121 u_role = USB_ROLE_DEVICE;
1122 typec_set_data_role(con->port, TYPEC_DEVICE);
1123 break;
1124 default:
1125 break;
1126 }
1127
1128 if (UCSI_CONSTAT(con, CONNECTED)) {
1129 switch (UCSI_CONSTAT(con, PARTNER_TYPE)) {
1130 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
1131 typec_set_mode(con->port, TYPEC_MODE_DEBUG);
1132 break;
1133 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
1134 typec_set_mode(con->port, TYPEC_MODE_AUDIO);
1135 break;
1136 default:
1137 if (UCSI_CONSTAT(con, PARTNER_FLAG_USB))
1138 typec_set_mode(con->port, TYPEC_STATE_USB);
1139 }
1140 }
1141
1142 /* Only notify USB controller if partner supports USB data */
1143 if (!(UCSI_CONSTAT(con, PARTNER_FLAG_USB)))
1144 u_role = USB_ROLE_NONE;
1145
1146 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1147 if (ret)
1148 dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n",
1149 con->num, u_role);
1150 }
1151
ucsi_check_connection(struct ucsi_connector * con)1152 static int ucsi_check_connection(struct ucsi_connector *con)
1153 {
1154 u8 prev_state = UCSI_CONSTAT(con, CONNECTED);
1155 int ret;
1156
1157 ret = ucsi_get_connector_status(con, false);
1158 if (ret) {
1159 dev_err(con->ucsi->dev, "GET_CONNECTOR_STATUS failed (%d)\n", ret);
1160 return ret;
1161 }
1162
1163 if (UCSI_CONSTAT(con, CONNECTED)) {
1164 if (prev_state)
1165 return 0;
1166 ucsi_register_partner(con);
1167 ucsi_pwr_opmode_change(con);
1168 ucsi_partner_change(con);
1169 } else {
1170 ucsi_partner_change(con);
1171 ucsi_port_psy_changed(con);
1172 ucsi_unregister_partner(con);
1173 }
1174
1175 return 0;
1176 }
1177
ucsi_check_cable(struct ucsi_connector * con)1178 static int ucsi_check_cable(struct ucsi_connector *con)
1179 {
1180 int ret, num_plug_am;
1181
1182 if (con->cable)
1183 return 0;
1184
1185 ret = ucsi_register_cable(con);
1186 if (ret < 0)
1187 return ret;
1188
1189 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE) {
1190 ret = ucsi_get_cable_identity(con);
1191 if (ret < 0)
1192 return ret;
1193 }
1194
1195 if (con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS) {
1196 ret = ucsi_register_plug(con);
1197 if (ret < 0)
1198 return ret;
1199
1200 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP_P);
1201 if (ret < 0)
1202 return ret;
1203
1204 if (con->plug_altmode[0]) {
1205 num_plug_am = ucsi_get_num_altmode(con->plug_altmode);
1206 typec_plug_set_num_altmodes(con->plug, num_plug_am);
1207 } else {
1208 typec_plug_set_num_altmodes(con->plug, 0);
1209 }
1210 }
1211
1212 return 0;
1213 }
1214
ucsi_handle_connector_change(struct work_struct * work)1215 static void ucsi_handle_connector_change(struct work_struct *work)
1216 {
1217 struct ucsi_connector *con = container_of(work, struct ucsi_connector,
1218 work);
1219 struct ucsi *ucsi = con->ucsi;
1220 enum typec_role role;
1221 u16 change;
1222 int ret;
1223
1224 mutex_lock(&con->lock);
1225
1226 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
1227 dev_err_once(ucsi->dev, "%s entered without EVENT_PENDING\n",
1228 __func__);
1229
1230 ret = ucsi_get_connector_status(con, true);
1231 if (ret) {
1232 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
1233 __func__, ret);
1234 clear_bit(EVENT_PENDING, &con->ucsi->flags);
1235 goto out_unlock;
1236 }
1237
1238 trace_ucsi_connector_change(con->num, con);
1239
1240 if (ucsi->ops->connector_status)
1241 ucsi->ops->connector_status(con);
1242
1243 change = UCSI_CONSTAT(con, CHANGE);
1244 role = UCSI_CONSTAT(con, PWR_DIR);
1245
1246 if (change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
1247 typec_set_pwr_role(con->port, role);
1248 ucsi_port_psy_changed(con);
1249
1250 /* Complete pending power role swap */
1251 if (!completion_done(&con->complete))
1252 complete(&con->complete);
1253 }
1254
1255 if (change & UCSI_CONSTAT_CONNECT_CHANGE) {
1256 typec_set_pwr_role(con->port, role);
1257 ucsi_port_psy_changed(con);
1258 ucsi_partner_change(con);
1259
1260 if (UCSI_CONSTAT(con, CONNECTED)) {
1261 ucsi_register_partner(con);
1262 ucsi_partner_task(con, ucsi_check_connection, 1, HZ);
1263 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
1264 ucsi_partner_task(con, ucsi_get_partner_identity, 1, HZ);
1265 if (con->ucsi->cap.features & UCSI_CAP_CABLE_DETAILS)
1266 ucsi_partner_task(con, ucsi_check_cable, 1, HZ);
1267
1268 if (UCSI_CONSTAT(con, PWR_OPMODE) == UCSI_CONSTAT_PWR_OPMODE_PD) {
1269 ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ);
1270 ucsi_partner_task(con, ucsi_check_connector_capability, 1, HZ);
1271 }
1272 } else {
1273 ucsi_unregister_partner(con);
1274 }
1275 }
1276
1277 if (change & (UCSI_CONSTAT_POWER_OPMODE_CHANGE | UCSI_CONSTAT_POWER_LEVEL_CHANGE))
1278 ucsi_pwr_opmode_change(con);
1279
1280 if (con->partner && (change & UCSI_CONSTAT_PARTNER_CHANGE)) {
1281 ucsi_partner_change(con);
1282
1283 /* Complete pending data role swap */
1284 if (!completion_done(&con->complete))
1285 complete(&con->complete);
1286 }
1287
1288 if (change & UCSI_CONSTAT_CAM_CHANGE)
1289 ucsi_partner_task(con, ucsi_check_altmodes, 1, HZ);
1290
1291 if (change & UCSI_CONSTAT_BC_CHANGE)
1292 ucsi_port_psy_changed(con);
1293
1294 out_unlock:
1295 mutex_unlock(&con->lock);
1296 }
1297
1298 /**
1299 * ucsi_connector_change - Process Connector Change Event
1300 * @ucsi: UCSI Interface
1301 * @num: Connector number
1302 */
ucsi_connector_change(struct ucsi * ucsi,u8 num)1303 void ucsi_connector_change(struct ucsi *ucsi, u8 num)
1304 {
1305 struct ucsi_connector *con = &ucsi->connector[num - 1];
1306
1307 if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
1308 dev_dbg(ucsi->dev, "Early connector change event\n");
1309 return;
1310 }
1311
1312 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
1313 schedule_work(&con->work);
1314 }
1315 EXPORT_SYMBOL_GPL(ucsi_connector_change);
1316
1317 /* -------------------------------------------------------------------------- */
1318
1319 /*
1320 * Hard Reset bit field was defined with value 1 in UCSI spec version 1.0.
1321 * Starting with spec version 1.1, Hard Reset bit field was removed from the
1322 * CONNECTOR_RESET command, until spec 2.0 reintroduced it with value 0, so, in effect,
1323 * the value to pass in to the command for a Hard Reset is different depending
1324 * on the supported UCSI version by the LPM.
1325 *
1326 * For performing a Data Reset on LPMs supporting version 2.0 and greater,
1327 * this function needs to be called with the second argument set to 0.
1328 */
ucsi_reset_connector(struct ucsi_connector * con,bool hard)1329 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
1330 {
1331 u64 command;
1332
1333 command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num);
1334
1335 if (con->ucsi->version < UCSI_VERSION_1_1)
1336 command |= hard ? UCSI_CONNECTOR_RESET_HARD_VER_1_0 : 0;
1337 else if (con->ucsi->version >= UCSI_VERSION_2_0)
1338 command |= hard ? 0 : UCSI_CONNECTOR_RESET_DATA_VER_2_0;
1339
1340 return ucsi_send_command(con->ucsi, command, NULL, 0);
1341 }
1342
ucsi_reset_ppm(struct ucsi * ucsi)1343 static int ucsi_reset_ppm(struct ucsi *ucsi)
1344 {
1345 u64 command;
1346 unsigned long tmo;
1347 u32 cci;
1348 int ret;
1349
1350 mutex_lock(&ucsi->ppm_lock);
1351
1352 ret = ucsi->ops->poll_cci(ucsi, &cci);
1353 if (ret < 0)
1354 goto out;
1355
1356 /*
1357 * If UCSI_CCI_RESET_COMPLETE is already set we must clear
1358 * the flag before we start another reset. Send a
1359 * UCSI_SET_NOTIFICATION_ENABLE command to achieve this.
1360 * Ignore a timeout and try the reset anyway if this fails.
1361 */
1362 if (cci & UCSI_CCI_RESET_COMPLETE) {
1363 command = UCSI_SET_NOTIFICATION_ENABLE;
1364 ret = ucsi->ops->async_control(ucsi, command);
1365 if (ret < 0)
1366 goto out;
1367
1368 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
1369 do {
1370 ret = ucsi->ops->poll_cci(ucsi, &cci);
1371 if (ret < 0)
1372 goto out;
1373 if (cci & UCSI_CCI_COMMAND_COMPLETE)
1374 break;
1375 if (time_is_before_jiffies(tmo))
1376 break;
1377 msleep(20);
1378 } while (1);
1379
1380 WARN_ON(cci & UCSI_CCI_RESET_COMPLETE);
1381 }
1382
1383 command = UCSI_PPM_RESET;
1384 ret = ucsi->ops->async_control(ucsi, command);
1385 if (ret < 0)
1386 goto out;
1387
1388 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
1389
1390 do {
1391 if (time_is_before_jiffies(tmo)) {
1392 ret = -ETIMEDOUT;
1393 goto out;
1394 }
1395
1396 /* Give the PPM time to process a reset before reading CCI */
1397 msleep(20);
1398
1399 ret = ucsi->ops->poll_cci(ucsi, &cci);
1400 if (ret)
1401 goto out;
1402
1403 /* If the PPM is still doing something else, reset it again. */
1404 if (cci & ~UCSI_CCI_RESET_COMPLETE) {
1405 ret = ucsi->ops->async_control(ucsi, command);
1406 if (ret < 0)
1407 goto out;
1408 }
1409
1410 } while (!(cci & UCSI_CCI_RESET_COMPLETE));
1411
1412 out:
1413 mutex_unlock(&ucsi->ppm_lock);
1414 return ret;
1415 }
1416
ucsi_role_cmd(struct ucsi_connector * con,u64 command)1417 static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)
1418 {
1419 int ret;
1420
1421 ret = ucsi_send_command(con->ucsi, command, NULL, 0);
1422 if (ret == -ETIMEDOUT) {
1423 u64 c;
1424
1425 /* PPM most likely stopped responding. Resetting everything. */
1426 ucsi_reset_ppm(con->ucsi);
1427
1428 c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
1429 ucsi_send_command(con->ucsi, c, NULL, 0);
1430
1431 ucsi_reset_connector(con, true);
1432 }
1433
1434 return ret;
1435 }
1436
ucsi_dr_swap(struct typec_port * port,enum typec_data_role role)1437 static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
1438 {
1439 struct ucsi_connector *con = typec_get_drvdata(port);
1440 u8 partner_type;
1441 u64 command;
1442 int ret = 0;
1443
1444 mutex_lock(&con->lock);
1445
1446 if (!con->partner) {
1447 ret = -ENOTCONN;
1448 goto out_unlock;
1449 }
1450
1451 partner_type = UCSI_CONSTAT(con, PARTNER_TYPE);
1452 if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
1453 role == TYPEC_DEVICE) ||
1454 (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
1455 role == TYPEC_HOST))
1456 goto out_unlock;
1457
1458 reinit_completion(&con->complete);
1459
1460 command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num);
1461 command |= UCSI_SET_UOR_ROLE(role);
1462 command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS;
1463 ret = ucsi_role_cmd(con, command);
1464 if (ret < 0)
1465 goto out_unlock;
1466
1467 mutex_unlock(&con->lock);
1468
1469 if (!wait_for_completion_timeout(&con->complete,
1470 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1471 return -ETIMEDOUT;
1472
1473 return 0;
1474
1475 out_unlock:
1476 mutex_unlock(&con->lock);
1477
1478 return ret;
1479 }
1480
ucsi_pr_swap(struct typec_port * port,enum typec_role role)1481 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
1482 {
1483 struct ucsi_connector *con = typec_get_drvdata(port);
1484 enum typec_role cur_role;
1485 u64 command;
1486 int ret = 0;
1487
1488 mutex_lock(&con->lock);
1489
1490 if (!con->partner) {
1491 ret = -ENOTCONN;
1492 goto out_unlock;
1493 }
1494
1495 cur_role = UCSI_CONSTAT(con, PWR_DIR);
1496
1497 if (cur_role == role)
1498 goto out_unlock;
1499
1500 reinit_completion(&con->complete);
1501
1502 command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num);
1503 command |= UCSI_SET_PDR_ROLE(role);
1504 command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS;
1505 ret = ucsi_role_cmd(con, command);
1506 if (ret < 0)
1507 goto out_unlock;
1508
1509 mutex_unlock(&con->lock);
1510
1511 if (!wait_for_completion_timeout(&con->complete,
1512 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1513 return -ETIMEDOUT;
1514
1515 mutex_lock(&con->lock);
1516
1517 /* Something has gone wrong while swapping the role */
1518 if (UCSI_CONSTAT(con, PWR_OPMODE) != UCSI_CONSTAT_PWR_OPMODE_PD) {
1519 ucsi_reset_connector(con, true);
1520 ret = -EPROTO;
1521 }
1522
1523 out_unlock:
1524 mutex_unlock(&con->lock);
1525
1526 return ret;
1527 }
1528
1529 static const struct typec_operations ucsi_ops = {
1530 .dr_set = ucsi_dr_swap,
1531 .pr_set = ucsi_pr_swap
1532 };
1533
1534 /* Caller must call fwnode_handle_put() after use */
ucsi_find_fwnode(struct ucsi_connector * con)1535 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
1536 {
1537 struct fwnode_handle *fwnode;
1538 int i = 1;
1539
1540 device_for_each_child_node(con->ucsi->dev, fwnode)
1541 if (i++ == con->num)
1542 return fwnode;
1543 return NULL;
1544 }
1545
ucsi_register_port(struct ucsi * ucsi,struct ucsi_connector * con)1546 static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con)
1547 {
1548 struct typec_capability *cap = &con->typec_cap;
1549 enum typec_accessory *accessory = cap->accessory;
1550 enum usb_role u_role = USB_ROLE_NONE;
1551 u64 command;
1552 char *name;
1553 int ret;
1554
1555 name = kasprintf(GFP_KERNEL, "%s-con%d", dev_name(ucsi->dev), con->num);
1556 if (!name)
1557 return -ENOMEM;
1558
1559 con->wq = create_singlethread_workqueue(name);
1560 kfree(name);
1561 if (!con->wq)
1562 return -ENOMEM;
1563
1564 INIT_WORK(&con->work, ucsi_handle_connector_change);
1565 init_completion(&con->complete);
1566 mutex_init(&con->lock);
1567 INIT_LIST_HEAD(&con->partner_tasks);
1568 con->ucsi = ucsi;
1569
1570 cap->fwnode = ucsi_find_fwnode(con);
1571 con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode);
1572 if (IS_ERR(con->usb_role_sw))
1573 return dev_err_probe(ucsi->dev, PTR_ERR(con->usb_role_sw),
1574 "con%d: failed to get usb role switch\n", con->num);
1575
1576 /* Delay other interactions with the con until registration is complete */
1577 mutex_lock(&con->lock);
1578
1579 /* Get connector capability */
1580 command = UCSI_GET_CONNECTOR_CAPABILITY;
1581 command |= UCSI_CONNECTOR_NUMBER(con->num);
1582 ret = ucsi_send_command(ucsi, command, &con->cap, sizeof(con->cap));
1583 if (ret < 0)
1584 goto out_unlock;
1585
1586 if (UCSI_CONCAP(con, OPMODE_DRP))
1587 cap->data = TYPEC_PORT_DRD;
1588 else if (UCSI_CONCAP(con, OPMODE_DFP))
1589 cap->data = TYPEC_PORT_DFP;
1590 else if (UCSI_CONCAP(con, OPMODE_UFP))
1591 cap->data = TYPEC_PORT_UFP;
1592
1593 if (UCSI_CONCAP(con, PROVIDER) && UCSI_CONCAP(con, CONSUMER))
1594 cap->type = TYPEC_PORT_DRP;
1595 else if (UCSI_CONCAP(con, PROVIDER))
1596 cap->type = TYPEC_PORT_SRC;
1597 else if (UCSI_CONCAP(con, CONSUMER))
1598 cap->type = TYPEC_PORT_SNK;
1599
1600 cap->revision = ucsi->cap.typec_version;
1601 cap->pd_revision = ucsi->cap.pd_version;
1602 cap->svdm_version = SVDM_VER_2_0;
1603 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
1604
1605 if (UCSI_CONCAP(con, OPMODE_AUDIO_ACCESSORY))
1606 *accessory++ = TYPEC_ACCESSORY_AUDIO;
1607 if (UCSI_CONCAP(con, OPMODE_DEBUG_ACCESSORY))
1608 *accessory = TYPEC_ACCESSORY_DEBUG;
1609
1610 if (UCSI_CONCAP_USB2_SUPPORT(con))
1611 cap->usb_capability |= USB_CAPABILITY_USB2;
1612 if (UCSI_CONCAP_USB3_SUPPORT(con))
1613 cap->usb_capability |= USB_CAPABILITY_USB3;
1614 if (UCSI_CONCAP_USB4_SUPPORT(con))
1615 cap->usb_capability |= USB_CAPABILITY_USB4;
1616
1617 cap->driver_data = con;
1618 cap->ops = &ucsi_ops;
1619
1620 if (ucsi->ops->update_connector)
1621 ucsi->ops->update_connector(con);
1622
1623 ret = ucsi_register_port_psy(con);
1624 if (ret)
1625 goto out;
1626
1627 /* Register the connector */
1628 con->port = typec_register_port(ucsi->dev, cap);
1629 if (IS_ERR(con->port)) {
1630 ret = PTR_ERR(con->port);
1631 goto out;
1632 }
1633
1634 if (!(ucsi->quirks & UCSI_DELAY_DEVICE_PDOS))
1635 ucsi_register_device_pdos(con);
1636
1637 /* Alternate modes */
1638 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON);
1639 if (ret) {
1640 dev_err(ucsi->dev, "con%d: failed to register alt modes\n",
1641 con->num);
1642 goto out;
1643 }
1644
1645 /* Get the status */
1646 ret = ucsi_get_connector_status(con, false);
1647 if (ret) {
1648 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
1649 goto out;
1650 }
1651
1652 if (ucsi->ops->connector_status)
1653 ucsi->ops->connector_status(con);
1654
1655 switch (UCSI_CONSTAT(con, PARTNER_TYPE)) {
1656 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1657 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1658 u_role = USB_ROLE_HOST;
1659 fallthrough;
1660 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1661 typec_set_data_role(con->port, TYPEC_HOST);
1662 break;
1663 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1664 u_role = USB_ROLE_DEVICE;
1665 typec_set_data_role(con->port, TYPEC_DEVICE);
1666 break;
1667 default:
1668 break;
1669 }
1670
1671 /* Check if there is already something connected */
1672 if (UCSI_CONSTAT(con, CONNECTED)) {
1673 typec_set_pwr_role(con->port, UCSI_CONSTAT(con, PWR_DIR));
1674 ucsi_register_partner(con);
1675 ucsi_pwr_opmode_change(con);
1676 ucsi_port_psy_changed(con);
1677 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
1678 ucsi_get_partner_identity(con);
1679 if (con->ucsi->cap.features & UCSI_CAP_CABLE_DETAILS)
1680 ucsi_check_cable(con);
1681 }
1682
1683 /* Only notify USB controller if partner supports USB data */
1684 if (!(UCSI_CONSTAT(con, PARTNER_FLAG_USB)))
1685 u_role = USB_ROLE_NONE;
1686
1687 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1688 if (ret) {
1689 dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n",
1690 con->num, u_role);
1691 ret = 0;
1692 }
1693
1694 if (con->partner && UCSI_CONSTAT(con, PWR_OPMODE) == UCSI_CONSTAT_PWR_OPMODE_PD) {
1695 ucsi_register_device_pdos(con);
1696 ucsi_get_src_pdos(con);
1697 ucsi_check_altmodes(con);
1698 ucsi_check_connector_capability(con);
1699 }
1700
1701 trace_ucsi_register_port(con->num, con);
1702
1703 out:
1704 fwnode_handle_put(cap->fwnode);
1705 out_unlock:
1706 mutex_unlock(&con->lock);
1707
1708 if (ret && con->wq) {
1709 destroy_workqueue(con->wq);
1710 con->wq = NULL;
1711 }
1712
1713 return ret;
1714 }
1715
ucsi_get_supported_notifications(struct ucsi * ucsi)1716 static u64 ucsi_get_supported_notifications(struct ucsi *ucsi)
1717 {
1718 u16 features = ucsi->cap.features;
1719 u64 ntfy = UCSI_ENABLE_NTFY_ALL;
1720
1721 if (!(features & UCSI_CAP_ALT_MODE_DETAILS))
1722 ntfy &= ~UCSI_ENABLE_NTFY_CAM_CHANGE;
1723
1724 if (!(features & UCSI_CAP_PDO_DETAILS))
1725 ntfy &= ~(UCSI_ENABLE_NTFY_PWR_LEVEL_CHANGE |
1726 UCSI_ENABLE_NTFY_CAP_CHANGE);
1727
1728 if (!(features & UCSI_CAP_EXT_SUPPLY_NOTIFICATIONS))
1729 ntfy &= ~UCSI_ENABLE_NTFY_EXT_PWR_SRC_CHANGE;
1730
1731 if (!(features & UCSI_CAP_PD_RESET))
1732 ntfy &= ~UCSI_ENABLE_NTFY_PD_RESET_COMPLETE;
1733
1734 if (ucsi->version <= UCSI_VERSION_1_2)
1735 return ntfy;
1736
1737 ntfy |= UCSI_ENABLE_NTFY_SINK_PATH_STS_CHANGE;
1738
1739 if (features & UCSI_CAP_GET_ATTENTION_VDO)
1740 ntfy |= UCSI_ENABLE_NTFY_ATTENTION;
1741
1742 if (features & UCSI_CAP_FW_UPDATE_REQUEST)
1743 ntfy |= UCSI_ENABLE_NTFY_LPM_FW_UPDATE_REQ;
1744
1745 if (features & UCSI_CAP_SECURITY_REQUEST)
1746 ntfy |= UCSI_ENABLE_NTFY_SECURITY_REQ_PARTNER;
1747
1748 if (features & UCSI_CAP_SET_RETIMER_MODE)
1749 ntfy |= UCSI_ENABLE_NTFY_SET_RETIMER_MODE;
1750
1751 return ntfy;
1752 }
1753
1754 /**
1755 * ucsi_init - Initialize UCSI interface
1756 * @ucsi: UCSI to be initialized
1757 *
1758 * Registers all ports @ucsi has and enables all notification events.
1759 */
ucsi_init(struct ucsi * ucsi)1760 static int ucsi_init(struct ucsi *ucsi)
1761 {
1762 struct ucsi_connector *con, *connector;
1763 u64 command, ntfy;
1764 u32 cci;
1765 int ret;
1766 int i;
1767
1768 /* Reset the PPM */
1769 ret = ucsi_reset_ppm(ucsi);
1770 if (ret) {
1771 dev_err(ucsi->dev, "failed to reset PPM!\n");
1772 goto err;
1773 }
1774
1775 /* Enable basic notifications */
1776 ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
1777 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1778 ret = ucsi_send_command(ucsi, command, NULL, 0);
1779 if (ret < 0)
1780 goto err_reset;
1781
1782 /* Get PPM capabilities */
1783 command = UCSI_GET_CAPABILITY;
1784 ret = ucsi_send_command(ucsi, command, &ucsi->cap,
1785 BITS_TO_BYTES(UCSI_GET_CAPABILITY_SIZE));
1786 if (ret < 0)
1787 goto err_reset;
1788
1789 if (!ucsi->cap.num_connectors) {
1790 ret = -ENODEV;
1791 goto err_reset;
1792 }
1793
1794 /* Allocate the connectors. Released in ucsi_unregister() */
1795 connector = kcalloc(ucsi->cap.num_connectors + 1, sizeof(*connector), GFP_KERNEL);
1796 if (!connector) {
1797 ret = -ENOMEM;
1798 goto err_reset;
1799 }
1800
1801 /* Register all connectors */
1802 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1803 connector[i].num = i + 1;
1804 ret = ucsi_register_port(ucsi, &connector[i]);
1805 if (ret)
1806 goto err_unregister;
1807 }
1808
1809 /* Enable all supported notifications */
1810 ntfy = ucsi_get_supported_notifications(ucsi);
1811 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1812 ret = ucsi_send_command(ucsi, command, NULL, 0);
1813 if (ret < 0)
1814 goto err_unregister;
1815
1816 ucsi->connector = connector;
1817 ucsi->ntfy = ntfy;
1818
1819 mutex_lock(&ucsi->ppm_lock);
1820 ret = ucsi->ops->read_cci(ucsi, &cci);
1821 mutex_unlock(&ucsi->ppm_lock);
1822 if (ret)
1823 return ret;
1824 if (UCSI_CCI_CONNECTOR(cci))
1825 ucsi_connector_change(ucsi, UCSI_CCI_CONNECTOR(cci));
1826
1827 return 0;
1828
1829 err_unregister:
1830 for (con = connector; con->port; con++) {
1831 if (con->wq)
1832 destroy_workqueue(con->wq);
1833 ucsi_unregister_partner(con);
1834 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON);
1835 ucsi_unregister_port_psy(con);
1836
1837 usb_power_delivery_unregister_capabilities(con->port_sink_caps);
1838 con->port_sink_caps = NULL;
1839 usb_power_delivery_unregister_capabilities(con->port_source_caps);
1840 con->port_source_caps = NULL;
1841 usb_power_delivery_unregister(con->pd);
1842 con->pd = NULL;
1843 typec_unregister_port(con->port);
1844 con->port = NULL;
1845 }
1846 kfree(connector);
1847 err_reset:
1848 memset(&ucsi->cap, 0, sizeof(ucsi->cap));
1849 ucsi_reset_ppm(ucsi);
1850 err:
1851 return ret;
1852 }
1853
ucsi_resume_work(struct work_struct * work)1854 static void ucsi_resume_work(struct work_struct *work)
1855 {
1856 struct ucsi *ucsi = container_of(work, struct ucsi, resume_work);
1857 struct ucsi_connector *con;
1858 u64 command;
1859 int ret;
1860
1861 /* Restore UCSI notification enable mask after system resume */
1862 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1863 ret = ucsi_send_command(ucsi, command, NULL, 0);
1864 if (ret < 0) {
1865 dev_err(ucsi->dev, "failed to re-enable notifications (%d)\n", ret);
1866 return;
1867 }
1868
1869 for (con = ucsi->connector; con->port; con++) {
1870 mutex_lock(&con->lock);
1871 ucsi_partner_task(con, ucsi_check_connection, 1, 0);
1872 mutex_unlock(&con->lock);
1873 }
1874 }
1875
ucsi_resume(struct ucsi * ucsi)1876 int ucsi_resume(struct ucsi *ucsi)
1877 {
1878 if (ucsi->connector)
1879 queue_work(system_long_wq, &ucsi->resume_work);
1880 return 0;
1881 }
1882 EXPORT_SYMBOL_GPL(ucsi_resume);
1883
ucsi_init_work(struct work_struct * work)1884 static void ucsi_init_work(struct work_struct *work)
1885 {
1886 struct ucsi *ucsi = container_of(work, struct ucsi, work.work);
1887 int ret;
1888
1889 ret = ucsi_init(ucsi);
1890 if (ret)
1891 dev_err_probe(ucsi->dev, ret, "PPM init failed\n");
1892
1893 if (ret == -EPROBE_DEFER) {
1894 if (ucsi->work_count++ > UCSI_ROLE_SWITCH_WAIT_COUNT) {
1895 dev_err(ucsi->dev, "PPM init failed, stop trying\n");
1896 return;
1897 }
1898
1899 queue_delayed_work(system_long_wq, &ucsi->work,
1900 UCSI_ROLE_SWITCH_INTERVAL);
1901 }
1902 }
1903
1904 /**
1905 * ucsi_get_drvdata - Return private driver data pointer
1906 * @ucsi: UCSI interface
1907 */
ucsi_get_drvdata(struct ucsi * ucsi)1908 void *ucsi_get_drvdata(struct ucsi *ucsi)
1909 {
1910 return ucsi->driver_data;
1911 }
1912 EXPORT_SYMBOL_GPL(ucsi_get_drvdata);
1913
1914 /**
1915 * ucsi_set_drvdata - Assign private driver data pointer
1916 * @ucsi: UCSI interface
1917 * @data: Private data pointer
1918 */
ucsi_set_drvdata(struct ucsi * ucsi,void * data)1919 void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
1920 {
1921 ucsi->driver_data = data;
1922 }
1923 EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
1924
1925 /**
1926 * ucsi_con_mutex_lock - Acquire the connector mutex
1927 * @con: The connector interface to lock
1928 *
1929 * Returns true on success, false if the connector is disconnected
1930 */
ucsi_con_mutex_lock(struct ucsi_connector * con)1931 bool ucsi_con_mutex_lock(struct ucsi_connector *con)
1932 {
1933 bool mutex_locked = false;
1934 bool connected = true;
1935
1936 while (connected && !mutex_locked) {
1937 mutex_locked = mutex_trylock(&con->lock) != 0;
1938 connected = UCSI_CONSTAT(con, CONNECTED);
1939 if (connected && !mutex_locked)
1940 msleep(20);
1941 }
1942
1943 connected = connected && con->partner;
1944 if (!connected && mutex_locked)
1945 mutex_unlock(&con->lock);
1946
1947 return connected;
1948 }
1949
1950 /**
1951 * ucsi_con_mutex_unlock - Release the connector mutex
1952 * @con: The connector interface to unlock
1953 */
ucsi_con_mutex_unlock(struct ucsi_connector * con)1954 void ucsi_con_mutex_unlock(struct ucsi_connector *con)
1955 {
1956 mutex_unlock(&con->lock);
1957 }
1958
1959 /**
1960 * ucsi_create - Allocate UCSI instance
1961 * @dev: Device interface to the PPM (Platform Policy Manager)
1962 * @ops: I/O routines
1963 */
ucsi_create(struct device * dev,const struct ucsi_operations * ops)1964 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
1965 {
1966 struct ucsi *ucsi;
1967
1968 if (!ops ||
1969 !ops->read_version || !ops->read_cci || !ops->poll_cci ||
1970 !ops->read_message_in || !ops->sync_control || !ops->async_control)
1971 return ERR_PTR(-EINVAL);
1972
1973 ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
1974 if (!ucsi)
1975 return ERR_PTR(-ENOMEM);
1976
1977 INIT_WORK(&ucsi->resume_work, ucsi_resume_work);
1978 INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work);
1979 mutex_init(&ucsi->ppm_lock);
1980 init_completion(&ucsi->complete);
1981 ucsi->dev = dev;
1982 ucsi->ops = ops;
1983
1984 return ucsi;
1985 }
1986 EXPORT_SYMBOL_GPL(ucsi_create);
1987
1988 /**
1989 * ucsi_destroy - Free UCSI instance
1990 * @ucsi: UCSI instance to be freed
1991 */
ucsi_destroy(struct ucsi * ucsi)1992 void ucsi_destroy(struct ucsi *ucsi)
1993 {
1994 ucsi_debugfs_unregister(ucsi);
1995 kfree(ucsi);
1996 }
1997 EXPORT_SYMBOL_GPL(ucsi_destroy);
1998
1999 /**
2000 * ucsi_register - Register UCSI interface
2001 * @ucsi: UCSI instance
2002 */
ucsi_register(struct ucsi * ucsi)2003 int ucsi_register(struct ucsi *ucsi)
2004 {
2005 int ret;
2006
2007 ret = ucsi->ops->read_version(ucsi, &ucsi->version);
2008 if (ret)
2009 return ret;
2010
2011 if (!ucsi->version)
2012 return -ENODEV;
2013
2014 /*
2015 * Version format is JJ.M.N (JJ = Major version, M = Minor version,
2016 * N = sub-minor version).
2017 */
2018 dev_dbg(ucsi->dev, "Registered UCSI interface with version %x.%x.%x",
2019 UCSI_BCD_GET_MAJOR(ucsi->version),
2020 UCSI_BCD_GET_MINOR(ucsi->version),
2021 UCSI_BCD_GET_SUBMINOR(ucsi->version));
2022
2023 queue_delayed_work(system_long_wq, &ucsi->work, 0);
2024
2025 ucsi_debugfs_register(ucsi);
2026 return 0;
2027 }
2028 EXPORT_SYMBOL_GPL(ucsi_register);
2029
2030 /**
2031 * ucsi_unregister - Unregister UCSI interface
2032 * @ucsi: UCSI interface to be unregistered
2033 *
2034 * Unregister UCSI interface that was created with ucsi_register().
2035 */
ucsi_unregister(struct ucsi * ucsi)2036 void ucsi_unregister(struct ucsi *ucsi)
2037 {
2038 u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
2039 int i;
2040
2041 /* Make sure that we are not in the middle of driver initialization */
2042 cancel_delayed_work_sync(&ucsi->work);
2043 cancel_work_sync(&ucsi->resume_work);
2044
2045 /* Disable notifications */
2046 ucsi->ops->async_control(ucsi, cmd);
2047
2048 if (!ucsi->connector)
2049 return;
2050
2051 for (i = 0; i < ucsi->cap.num_connectors; i++) {
2052 cancel_work_sync(&ucsi->connector[i].work);
2053
2054 if (ucsi->connector[i].wq) {
2055 struct ucsi_work *uwork;
2056
2057 mutex_lock(&ucsi->connector[i].lock);
2058 /*
2059 * queue delayed items immediately so they can execute
2060 * and free themselves before the wq is destroyed
2061 */
2062 list_for_each_entry(uwork, &ucsi->connector[i].partner_tasks, node)
2063 mod_delayed_work(ucsi->connector[i].wq, &uwork->work, 0);
2064 mutex_unlock(&ucsi->connector[i].lock);
2065 destroy_workqueue(ucsi->connector[i].wq);
2066 }
2067
2068 ucsi_unregister_partner(&ucsi->connector[i]);
2069 ucsi_unregister_altmodes(&ucsi->connector[i],
2070 UCSI_RECIPIENT_CON);
2071 ucsi_unregister_port_psy(&ucsi->connector[i]);
2072
2073 usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_sink_caps);
2074 ucsi->connector[i].port_sink_caps = NULL;
2075 usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_source_caps);
2076 ucsi->connector[i].port_source_caps = NULL;
2077 usb_power_delivery_unregister(ucsi->connector[i].pd);
2078 ucsi->connector[i].pd = NULL;
2079 typec_unregister_port(ucsi->connector[i].port);
2080 }
2081
2082 kfree(ucsi->connector);
2083 }
2084 EXPORT_SYMBOL_GPL(ucsi_unregister);
2085
ucsi_module_init(void)2086 static int __init ucsi_module_init(void)
2087 {
2088 ucsi_debugfs_init();
2089 return 0;
2090 }
2091 module_init(ucsi_module_init);
2092
ucsi_module_exit(void)2093 static void __exit ucsi_module_exit(void)
2094 {
2095 ucsi_debugfs_exit();
2096 }
2097 module_exit(ucsi_module_exit);
2098
2099 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
2100 MODULE_LICENSE("GPL v2");
2101 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");
2102