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