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