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