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