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 if (recipient == UCSI_RECIPIENT_CON)
535 multi_dp = ucsi->ops->update_altmodes(ucsi, orig, updated);
536
537 /* now register altmodes */
538 for (i = 0; i < max_altmodes; i++) {
539 memset(&desc, 0, sizeof(desc));
540 if (multi_dp && recipient == UCSI_RECIPIENT_CON) {
541 desc.svid = updated[i].svid;
542 desc.vdo = updated[i].mid;
543 } else {
544 desc.svid = orig[i].svid;
545 desc.vdo = orig[i].mid;
546 }
547 desc.roles = TYPEC_PORT_DRD;
548
549 if (!desc.svid)
550 return 0;
551
552 ret = ucsi_register_altmode(con, &desc, recipient);
553 if (ret)
554 return ret;
555 }
556
557 return 0;
558 }
559
ucsi_register_altmodes(struct ucsi_connector * con,u8 recipient)560 static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
561 {
562 int max_altmodes = UCSI_MAX_ALTMODES;
563 struct typec_altmode_desc desc;
564 struct ucsi_altmode alt[2];
565 u64 command;
566 int num;
567 int ret;
568 int len;
569 int j;
570 int i;
571
572 if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
573 return 0;
574
575 if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
576 return 0;
577
578 if (con->ucsi->ops->update_altmodes)
579 return ucsi_register_altmodes_nvidia(con, recipient);
580
581 if (recipient == UCSI_RECIPIENT_CON)
582 max_altmodes = con->ucsi->cap.num_alt_modes;
583
584 for (i = 0; i < max_altmodes;) {
585 memset(alt, 0, sizeof(alt));
586 command = UCSI_GET_ALTERNATE_MODES;
587 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
588 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
589 command |= UCSI_GET_ALTMODE_OFFSET(i);
590 len = ucsi_send_command(con->ucsi, command, alt, sizeof(alt));
591 if (len == -EBUSY)
592 continue;
593 if (len <= 0)
594 return len;
595
596 /*
597 * This code is requesting one alt mode at a time, but some PPMs
598 * may still return two. If that happens both alt modes need be
599 * registered and the offset for the next alt mode has to be
600 * incremented.
601 */
602 num = len / sizeof(alt[0]);
603 i += num;
604
605 for (j = 0; j < num; j++) {
606 if (!alt[j].svid)
607 return 0;
608
609 memset(&desc, 0, sizeof(desc));
610 desc.vdo = alt[j].mid;
611 desc.svid = alt[j].svid;
612 desc.roles = TYPEC_PORT_DRD;
613
614 ret = ucsi_register_altmode(con, &desc, recipient);
615 if (ret)
616 return ret;
617 }
618 }
619
620 return 0;
621 }
622
ucsi_unregister_altmodes(struct ucsi_connector * con,u8 recipient)623 static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
624 {
625 const struct typec_altmode *pdev;
626 struct typec_altmode **adev;
627 int i = 0;
628
629 switch (recipient) {
630 case UCSI_RECIPIENT_CON:
631 adev = con->port_altmode;
632 break;
633 case UCSI_RECIPIENT_SOP:
634 adev = con->partner_altmode;
635 break;
636 case UCSI_RECIPIENT_SOP_P:
637 adev = con->plug_altmode;
638 break;
639 default:
640 return;
641 }
642
643 while (adev[i]) {
644 if (recipient == UCSI_RECIPIENT_SOP &&
645 (adev[i]->svid == USB_TYPEC_DP_SID ||
646 (adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID &&
647 adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) {
648 pdev = typec_altmode_get_partner(adev[i]);
649 ucsi_displayport_remove_partner((void *)pdev);
650 }
651 typec_unregister_altmode(adev[i]);
652 adev[i++] = NULL;
653 }
654 }
655
ucsi_get_connector_status(struct ucsi_connector * con,bool conn_ack)656 static int ucsi_get_connector_status(struct ucsi_connector *con, bool conn_ack)
657 {
658 u64 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
659 size_t size = min(sizeof(con->status),
660 UCSI_MAX_DATA_LENGTH(con->ucsi));
661 int ret;
662
663 ret = ucsi_send_command_common(con->ucsi, command, &con->status, size, conn_ack);
664
665 return ret < 0 ? ret : 0;
666 }
667
ucsi_read_pdos(struct ucsi_connector * con,enum typec_role role,int is_partner,u32 * pdos,int offset,int num_pdos)668 static int ucsi_read_pdos(struct ucsi_connector *con,
669 enum typec_role role, int is_partner,
670 u32 *pdos, int offset, int num_pdos)
671 {
672 struct ucsi *ucsi = con->ucsi;
673 u64 command;
674 int ret;
675
676 if (is_partner &&
677 ucsi->quirks & UCSI_NO_PARTNER_PDOS &&
678 (UCSI_CONSTAT(con, PWR_DIR) || !is_source(role)))
679 return 0;
680
681 command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num);
682 command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner);
683 command |= UCSI_GET_PDOS_PDO_OFFSET(offset);
684 command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1);
685 command |= is_source(role) ? UCSI_GET_PDOS_SRC_PDOS : 0;
686 ret = ucsi_send_command(ucsi, command, pdos + offset,
687 num_pdos * sizeof(u32));
688 if (ret < 0 && ret != -ETIMEDOUT)
689 dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret);
690
691 return ret;
692 }
693
ucsi_get_pdos(struct ucsi_connector * con,enum typec_role role,int is_partner,u32 * pdos)694 static int ucsi_get_pdos(struct ucsi_connector *con, enum typec_role role,
695 int is_partner, u32 *pdos)
696 {
697 struct ucsi *ucsi = con->ucsi;
698 u8 num_pdos;
699 int ret;
700
701 if (!(ucsi->cap.features & UCSI_CAP_PDO_DETAILS))
702 return 0;
703
704 /* UCSI max payload means only getting at most 4 PDOs at a time */
705 ret = ucsi_read_pdos(con, role, is_partner, pdos, 0, UCSI_MAX_PDOS);
706 if (ret < 0)
707 return ret;
708
709 num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */
710 if (num_pdos < UCSI_MAX_PDOS)
711 return num_pdos;
712
713 /* get the remaining PDOs, if any */
714 ret = ucsi_read_pdos(con, role, is_partner, pdos, UCSI_MAX_PDOS,
715 PDO_MAX_OBJECTS - UCSI_MAX_PDOS);
716 if (ret < 0)
717 return ret;
718
719 return ret / sizeof(u32) + num_pdos;
720 }
721
ucsi_get_src_pdos(struct ucsi_connector * con)722 static int ucsi_get_src_pdos(struct ucsi_connector *con)
723 {
724 int ret;
725
726 ret = ucsi_get_pdos(con, TYPEC_SOURCE, 1, con->src_pdos);
727 if (ret < 0)
728 return ret;
729
730 con->num_pdos = ret;
731
732 ucsi_port_psy_changed(con);
733
734 return ret;
735 }
736
ucsi_get_pd_caps(struct ucsi_connector * con,enum typec_role role,bool is_partner)737 static struct usb_power_delivery_capabilities *ucsi_get_pd_caps(struct ucsi_connector *con,
738 enum typec_role role,
739 bool is_partner)
740 {
741 struct usb_power_delivery_capabilities_desc pd_caps;
742 int ret;
743
744 ret = ucsi_get_pdos(con, role, is_partner, pd_caps.pdo);
745 if (ret <= 0)
746 return ERR_PTR(ret);
747
748 if (ret < PDO_MAX_OBJECTS)
749 pd_caps.pdo[ret] = 0;
750
751 pd_caps.role = role;
752
753 return usb_power_delivery_register_capabilities(is_partner ? con->partner_pd : con->pd,
754 &pd_caps);
755 }
756
ucsi_get_pd_message(struct ucsi_connector * con,u8 recipient,size_t bytes,void * data,u8 type)757 static int ucsi_get_pd_message(struct ucsi_connector *con, u8 recipient,
758 size_t bytes, void *data, u8 type)
759 {
760 size_t len = min(bytes, UCSI_MAX_DATA_LENGTH(con->ucsi));
761 u64 command;
762 u8 offset;
763 int ret;
764
765 for (offset = 0; offset < bytes; offset += len) {
766 len = min(len, bytes - offset);
767
768 command = UCSI_COMMAND(UCSI_GET_PD_MESSAGE) | UCSI_CONNECTOR_NUMBER(con->num);
769 command |= UCSI_GET_PD_MESSAGE_RECIPIENT(recipient);
770 command |= UCSI_GET_PD_MESSAGE_OFFSET(offset);
771 command |= UCSI_GET_PD_MESSAGE_BYTES(len);
772 command |= UCSI_GET_PD_MESSAGE_TYPE(type);
773
774 ret = ucsi_send_command(con->ucsi, command, data + offset, len);
775 if (ret < 0)
776 return ret;
777 }
778
779 return 0;
780 }
781
ucsi_get_partner_identity(struct ucsi_connector * con)782 static int ucsi_get_partner_identity(struct ucsi_connector *con)
783 {
784 u32 vdo[7] = {};
785 int ret;
786
787 ret = ucsi_get_pd_message(con, UCSI_RECIPIENT_SOP, sizeof(vdo), vdo,
788 UCSI_GET_PD_MESSAGE_TYPE_IDENTITY);
789 if (ret < 0)
790 return ret;
791
792 /* VDM Header is not part of struct usb_pd_identity, so dropping it. */
793 con->partner_identity = *(struct usb_pd_identity *)&vdo[1];
794
795 ret = typec_partner_set_identity(con->partner);
796 if (ret < 0)
797 dev_err(con->ucsi->dev, "Failed to set partner identity (%d)\n", ret);
798
799 return ret;
800 }
801
ucsi_get_cable_identity(struct ucsi_connector * con)802 static int ucsi_get_cable_identity(struct ucsi_connector *con)
803 {
804 u32 vdo[7] = {};
805 int ret;
806
807 ret = ucsi_get_pd_message(con, UCSI_RECIPIENT_SOP_P, sizeof(vdo), vdo,
808 UCSI_GET_PD_MESSAGE_TYPE_IDENTITY);
809 if (ret < 0)
810 return ret;
811
812 con->cable_identity = *(struct usb_pd_identity *)&vdo[1];
813
814 ret = typec_cable_set_identity(con->cable);
815 if (ret < 0)
816 dev_err(con->ucsi->dev, "Failed to set cable identity (%d)\n", ret);
817
818 return ret;
819 }
820
ucsi_check_altmodes(struct ucsi_connector * con)821 static int ucsi_check_altmodes(struct ucsi_connector *con)
822 {
823 int ret, num_partner_am;
824
825 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
826 if (ret && ret != -ETIMEDOUT)
827 dev_err(con->ucsi->dev,
828 "con%d: failed to register partner alt modes (%d)\n",
829 con->num, ret);
830
831 /* Ignoring the errors in this case. */
832 if (con->partner_altmode[0]) {
833 num_partner_am = ucsi_get_num_altmode(con->partner_altmode);
834 typec_partner_set_num_altmodes(con->partner, num_partner_am);
835 ucsi_altmode_update_active(con);
836 return 0;
837 } else {
838 typec_partner_set_num_altmodes(con->partner, 0);
839 }
840
841 return ret;
842 }
843
ucsi_register_device_pdos(struct ucsi_connector * con)844 static void ucsi_register_device_pdos(struct ucsi_connector *con)
845 {
846 struct ucsi *ucsi = con->ucsi;
847 struct usb_power_delivery_desc desc = { ucsi->cap.pd_version };
848 struct usb_power_delivery_capabilities *pd_cap;
849
850 if (con->pd)
851 return;
852
853 con->pd = usb_power_delivery_register(ucsi->dev, &desc);
854
855 pd_cap = ucsi_get_pd_caps(con, TYPEC_SOURCE, false);
856 if (!IS_ERR(pd_cap))
857 con->port_source_caps = pd_cap;
858
859 pd_cap = ucsi_get_pd_caps(con, TYPEC_SINK, false);
860 if (!IS_ERR(pd_cap))
861 con->port_sink_caps = pd_cap;
862
863 typec_port_set_usb_power_delivery(con->port, con->pd);
864 }
865
ucsi_register_partner_pdos(struct ucsi_connector * con)866 static int ucsi_register_partner_pdos(struct ucsi_connector *con)
867 {
868 struct usb_power_delivery_desc desc = { con->ucsi->cap.pd_version };
869 struct usb_power_delivery_capabilities *cap;
870
871 if (con->partner_pd)
872 return 0;
873
874 con->partner_pd = typec_partner_usb_power_delivery_register(con->partner, &desc);
875 if (IS_ERR(con->partner_pd))
876 return PTR_ERR(con->partner_pd);
877
878 cap = ucsi_get_pd_caps(con, TYPEC_SOURCE, true);
879 if (IS_ERR(cap))
880 return PTR_ERR(cap);
881
882 con->partner_source_caps = cap;
883
884 cap = ucsi_get_pd_caps(con, TYPEC_SINK, true);
885 if (IS_ERR(cap))
886 return PTR_ERR(cap);
887
888 con->partner_sink_caps = cap;
889
890 return typec_partner_set_usb_power_delivery(con->partner, con->partner_pd);
891 }
892
ucsi_unregister_partner_pdos(struct ucsi_connector * con)893 static void ucsi_unregister_partner_pdos(struct ucsi_connector *con)
894 {
895 usb_power_delivery_unregister_capabilities(con->partner_sink_caps);
896 con->partner_sink_caps = NULL;
897 usb_power_delivery_unregister_capabilities(con->partner_source_caps);
898 con->partner_source_caps = NULL;
899 usb_power_delivery_unregister(con->partner_pd);
900 con->partner_pd = NULL;
901 }
902
ucsi_register_plug(struct ucsi_connector * con)903 static int ucsi_register_plug(struct ucsi_connector *con)
904 {
905 struct typec_plug *plug;
906 struct typec_plug_desc desc = {.index = TYPEC_PLUG_SOP_P};
907
908 plug = typec_register_plug(con->cable, &desc);
909 if (IS_ERR(plug)) {
910 dev_err(con->ucsi->dev,
911 "con%d: failed to register plug (%ld)\n", con->num,
912 PTR_ERR(plug));
913 return PTR_ERR(plug);
914 }
915
916 con->plug = plug;
917 return 0;
918 }
919
ucsi_unregister_plug(struct ucsi_connector * con)920 static void ucsi_unregister_plug(struct ucsi_connector *con)
921 {
922 if (!con->plug)
923 return;
924
925 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP_P);
926 typec_unregister_plug(con->plug);
927 con->plug = NULL;
928 }
929
ucsi_register_cable(struct ucsi_connector * con)930 static int ucsi_register_cable(struct ucsi_connector *con)
931 {
932 struct ucsi_cable_property cable_prop;
933 struct typec_cable *cable;
934 struct typec_cable_desc desc = {};
935 u64 command;
936 int ret;
937
938 command = UCSI_GET_CABLE_PROPERTY | UCSI_CONNECTOR_NUMBER(con->num);
939 ret = ucsi_send_command(con->ucsi, command, &cable_prop, sizeof(cable_prop));
940 if (ret < 0) {
941 dev_err(con->ucsi->dev, "GET_CABLE_PROPERTY failed (%d)\n", ret);
942 return ret;
943 }
944
945 switch (UCSI_CABLE_PROP_FLAG_PLUG_TYPE(cable_prop.flags)) {
946 case UCSI_CABLE_PROPERTY_PLUG_TYPE_A:
947 desc.type = USB_PLUG_TYPE_A;
948 break;
949 case UCSI_CABLE_PROPERTY_PLUG_TYPE_B:
950 desc.type = USB_PLUG_TYPE_B;
951 break;
952 case UCSI_CABLE_PROPERTY_PLUG_TYPE_C:
953 desc.type = USB_PLUG_TYPE_C;
954 break;
955 default:
956 desc.type = USB_PLUG_NONE;
957 break;
958 }
959
960 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
961 desc.identity = &con->cable_identity;
962 desc.active = !!(UCSI_CABLE_PROP_FLAG_ACTIVE_CABLE & cable_prop.flags);
963
964 if (con->ucsi->version >= UCSI_VERSION_2_1)
965 desc.pd_revision = UCSI_CABLE_PROP_FLAG_PD_MAJOR_REV_AS_BCD(cable_prop.flags);
966
967 cable = typec_register_cable(con->port, &desc);
968 if (IS_ERR(cable)) {
969 dev_err(con->ucsi->dev,
970 "con%d: failed to register cable (%ld)\n", con->num,
971 PTR_ERR(cable));
972 return PTR_ERR(cable);
973 }
974
975 con->cable = cable;
976 return 0;
977 }
978
ucsi_unregister_cable(struct ucsi_connector * con)979 static void ucsi_unregister_cable(struct ucsi_connector *con)
980 {
981 if (!con->cable)
982 return;
983
984 ucsi_unregister_plug(con);
985 typec_unregister_cable(con->cable);
986 memset(&con->cable_identity, 0, sizeof(con->cable_identity));
987 con->cable = NULL;
988 }
989
ucsi_check_connector_capability(struct ucsi_connector * con)990 static int ucsi_check_connector_capability(struct ucsi_connector *con)
991 {
992 u64 pd_revision;
993 u64 command;
994 int ret;
995
996 if (!con->partner || con->ucsi->version < UCSI_VERSION_2_1)
997 return 0;
998
999 command = UCSI_GET_CONNECTOR_CAPABILITY | UCSI_CONNECTOR_NUMBER(con->num);
1000 ret = ucsi_send_command(con->ucsi, command, &con->cap, sizeof(con->cap));
1001 if (ret < 0) {
1002 dev_err(con->ucsi->dev, "GET_CONNECTOR_CAPABILITY failed (%d)\n", ret);
1003 return ret;
1004 }
1005
1006 pd_revision = UCSI_CONCAP(con, PARTNER_PD_REVISION_V2_1);
1007 typec_partner_set_pd_revision(con->partner, UCSI_SPEC_REVISION_TO_BCD(pd_revision));
1008
1009 return ret;
1010 }
1011
ucsi_pwr_opmode_change(struct ucsi_connector * con)1012 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
1013 {
1014 switch (UCSI_CONSTAT(con, PWR_OPMODE)) {
1015 case UCSI_CONSTAT_PWR_OPMODE_PD:
1016 con->rdo = UCSI_CONSTAT(con, RDO);
1017 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
1018 ucsi_partner_task(con, ucsi_get_src_pdos, 30, 0);
1019 ucsi_partner_task(con, ucsi_check_altmodes, 30, HZ);
1020 ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ);
1021 ucsi_partner_task(con, ucsi_check_connector_capability, 1, HZ);
1022 break;
1023 case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
1024 con->rdo = 0;
1025 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
1026 break;
1027 case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
1028 con->rdo = 0;
1029 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
1030 break;
1031 default:
1032 con->rdo = 0;
1033 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
1034 break;
1035 }
1036 }
1037
ucsi_register_partner(struct ucsi_connector * con)1038 static int ucsi_register_partner(struct ucsi_connector *con)
1039 {
1040 u8 pwr_opmode = UCSI_CONSTAT(con, PWR_OPMODE);
1041 struct typec_partner_desc desc;
1042 struct typec_partner *partner;
1043
1044 if (con->partner)
1045 return 0;
1046
1047 memset(&desc, 0, sizeof(desc));
1048
1049 switch (UCSI_CONSTAT(con, PARTNER_TYPE)) {
1050 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
1051 desc.accessory = TYPEC_ACCESSORY_DEBUG;
1052 break;
1053 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
1054 desc.accessory = TYPEC_ACCESSORY_AUDIO;
1055 break;
1056 default:
1057 break;
1058 }
1059
1060 if (pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD)
1061 ucsi_register_device_pdos(con);
1062
1063 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
1064 desc.identity = &con->partner_identity;
1065 desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD;
1066
1067 if (con->ucsi->version >= UCSI_VERSION_2_1) {
1068 u64 pd_revision = UCSI_CONCAP(con, PARTNER_PD_REVISION_V2_1);
1069 desc.pd_revision = UCSI_SPEC_REVISION_TO_BCD(pd_revision);
1070 }
1071
1072 partner = typec_register_partner(con->port, &desc);
1073 if (IS_ERR(partner)) {
1074 dev_err(con->ucsi->dev,
1075 "con%d: failed to register partner (%ld)\n", con->num,
1076 PTR_ERR(partner));
1077 return PTR_ERR(partner);
1078 }
1079
1080 con->partner = partner;
1081
1082 if (con->ucsi->version >= UCSI_VERSION_3_0 &&
1083 UCSI_CONSTAT(con, PARTNER_FLAG_USB4_GEN4))
1084 typec_partner_set_usb_mode(partner, USB_MODE_USB4);
1085 else if (con->ucsi->version >= UCSI_VERSION_2_0 &&
1086 UCSI_CONSTAT(con, PARTNER_FLAG_USB4_GEN3))
1087 typec_partner_set_usb_mode(partner, USB_MODE_USB4);
1088
1089 return 0;
1090 }
1091
ucsi_unregister_partner(struct ucsi_connector * con)1092 static void ucsi_unregister_partner(struct ucsi_connector *con)
1093 {
1094 if (!con->partner)
1095 return;
1096
1097 typec_set_mode(con->port, TYPEC_STATE_SAFE);
1098
1099 typec_partner_set_usb_power_delivery(con->partner, NULL);
1100 ucsi_unregister_partner_pdos(con);
1101 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
1102 ucsi_unregister_cable(con);
1103 typec_unregister_partner(con->partner);
1104 memset(&con->partner_identity, 0, sizeof(con->partner_identity));
1105 con->partner = NULL;
1106 }
1107
ucsi_partner_change(struct ucsi_connector * con)1108 static void ucsi_partner_change(struct ucsi_connector *con)
1109 {
1110 enum usb_role u_role = USB_ROLE_NONE;
1111 int ret;
1112
1113 switch (UCSI_CONSTAT(con, PARTNER_TYPE)) {
1114 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1115 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1116 u_role = USB_ROLE_HOST;
1117 fallthrough;
1118 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1119 typec_set_data_role(con->port, TYPEC_HOST);
1120 break;
1121 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1122 u_role = USB_ROLE_DEVICE;
1123 typec_set_data_role(con->port, TYPEC_DEVICE);
1124 break;
1125 default:
1126 break;
1127 }
1128
1129 if (UCSI_CONSTAT(con, CONNECTED)) {
1130 switch (UCSI_CONSTAT(con, PARTNER_TYPE)) {
1131 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
1132 typec_set_mode(con->port, TYPEC_MODE_DEBUG);
1133 break;
1134 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
1135 typec_set_mode(con->port, TYPEC_MODE_AUDIO);
1136 break;
1137 default:
1138 if (UCSI_CONSTAT(con, PARTNER_FLAG_USB))
1139 typec_set_mode(con->port, TYPEC_STATE_USB);
1140 }
1141 }
1142
1143 /* Only notify USB controller if partner supports USB data */
1144 if (!(UCSI_CONSTAT(con, PARTNER_FLAG_USB)))
1145 u_role = USB_ROLE_NONE;
1146
1147 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1148 if (ret)
1149 dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n",
1150 con->num, u_role);
1151 }
1152
ucsi_check_connection(struct ucsi_connector * con)1153 static int ucsi_check_connection(struct ucsi_connector *con)
1154 {
1155 u8 prev_state = UCSI_CONSTAT(con, CONNECTED);
1156 int ret;
1157
1158 ret = ucsi_get_connector_status(con, false);
1159 if (ret) {
1160 dev_err(con->ucsi->dev, "GET_CONNECTOR_STATUS failed (%d)\n", ret);
1161 return ret;
1162 }
1163
1164 if (UCSI_CONSTAT(con, CONNECTED)) {
1165 if (prev_state)
1166 return 0;
1167 ucsi_register_partner(con);
1168 ucsi_pwr_opmode_change(con);
1169 ucsi_partner_change(con);
1170 } else {
1171 ucsi_partner_change(con);
1172 ucsi_port_psy_changed(con);
1173 ucsi_unregister_partner(con);
1174 }
1175
1176 return 0;
1177 }
1178
ucsi_check_cable(struct ucsi_connector * con)1179 static int ucsi_check_cable(struct ucsi_connector *con)
1180 {
1181 int ret, num_plug_am;
1182
1183 if (con->cable)
1184 return 0;
1185
1186 ret = ucsi_register_cable(con);
1187 if (ret < 0)
1188 return ret;
1189
1190 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE) {
1191 ret = ucsi_get_cable_identity(con);
1192 if (ret < 0)
1193 return ret;
1194 }
1195
1196 if (con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS) {
1197 ret = ucsi_register_plug(con);
1198 if (ret < 0)
1199 return ret;
1200
1201 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP_P);
1202 if (ret < 0)
1203 return ret;
1204
1205 if (con->plug_altmode[0]) {
1206 num_plug_am = ucsi_get_num_altmode(con->plug_altmode);
1207 typec_plug_set_num_altmodes(con->plug, num_plug_am);
1208 } else {
1209 typec_plug_set_num_altmodes(con->plug, 0);
1210 }
1211 }
1212
1213 return 0;
1214 }
1215
ucsi_handle_connector_change(struct work_struct * work)1216 static void ucsi_handle_connector_change(struct work_struct *work)
1217 {
1218 struct ucsi_connector *con = container_of(work, struct ucsi_connector,
1219 work);
1220 struct ucsi *ucsi = con->ucsi;
1221 enum typec_role role;
1222 u16 change;
1223 int ret;
1224
1225 mutex_lock(&con->lock);
1226
1227 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
1228 dev_err_once(ucsi->dev, "%s entered without EVENT_PENDING\n",
1229 __func__);
1230
1231 ret = ucsi_get_connector_status(con, true);
1232 if (ret) {
1233 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
1234 __func__, ret);
1235 clear_bit(EVENT_PENDING, &con->ucsi->flags);
1236 goto out_unlock;
1237 }
1238
1239 trace_ucsi_connector_change(con->num, con);
1240
1241 if (ucsi->ops->connector_status)
1242 ucsi->ops->connector_status(con);
1243
1244 change = UCSI_CONSTAT(con, CHANGE);
1245 role = UCSI_CONSTAT(con, PWR_DIR);
1246
1247 if (change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
1248 typec_set_pwr_role(con->port, role);
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