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 5000
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_read_message_in(struct ucsi * ucsi,void * buf,size_t buf_size)39 static int ucsi_read_message_in(struct ucsi *ucsi, void *buf,
40 size_t buf_size)
41 {
42 /*
43 * Below UCSI 2.0, MESSAGE_IN was limited to 16 bytes. Truncate the
44 * reads here.
45 */
46 if (ucsi->version <= UCSI_VERSION_1_2)
47 buf_size = clamp(buf_size, 0, 16);
48
49 return ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, buf, buf_size);
50 }
51
ucsi_acknowledge(struct ucsi * ucsi,bool conn_ack)52 static int ucsi_acknowledge(struct ucsi *ucsi, bool conn_ack)
53 {
54 u64 ctrl;
55
56 ctrl = UCSI_ACK_CC_CI;
57 ctrl |= UCSI_ACK_COMMAND_COMPLETE;
58 if (conn_ack) {
59 clear_bit(EVENT_PENDING, &ucsi->flags);
60 ctrl |= UCSI_ACK_CONNECTOR_CHANGE;
61 }
62
63 return ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &ctrl, sizeof(ctrl));
64 }
65
66 static int ucsi_exec_command(struct ucsi *ucsi, u64 command);
67
ucsi_read_error(struct ucsi * ucsi)68 static int ucsi_read_error(struct ucsi *ucsi)
69 {
70 u16 error;
71 int ret;
72
73 /* Acknowledge the command that failed */
74 ret = ucsi_acknowledge(ucsi, false);
75 if (ret)
76 return ret;
77
78 ret = ucsi_exec_command(ucsi, UCSI_GET_ERROR_STATUS);
79 if (ret < 0)
80 return ret;
81
82 ret = ucsi_read_message_in(ucsi, &error, sizeof(error));
83 if (ret)
84 return ret;
85
86 ret = ucsi_acknowledge(ucsi, false);
87 if (ret)
88 return ret;
89
90 switch (error) {
91 case UCSI_ERROR_INCOMPATIBLE_PARTNER:
92 return -EOPNOTSUPP;
93 case UCSI_ERROR_CC_COMMUNICATION_ERR:
94 return -ECOMM;
95 case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
96 return -EPROTO;
97 case UCSI_ERROR_DEAD_BATTERY:
98 dev_warn(ucsi->dev, "Dead battery condition!\n");
99 return -EPERM;
100 case UCSI_ERROR_INVALID_CON_NUM:
101 case UCSI_ERROR_UNREGONIZED_CMD:
102 case UCSI_ERROR_INVALID_CMD_ARGUMENT:
103 dev_err(ucsi->dev, "possible UCSI driver bug %u\n", error);
104 return -EINVAL;
105 case UCSI_ERROR_OVERCURRENT:
106 dev_warn(ucsi->dev, "Overcurrent condition\n");
107 break;
108 case UCSI_ERROR_PARTNER_REJECTED_SWAP:
109 dev_warn(ucsi->dev, "Partner rejected swap\n");
110 break;
111 case UCSI_ERROR_HARD_RESET:
112 dev_warn(ucsi->dev, "Hard reset occurred\n");
113 break;
114 case UCSI_ERROR_PPM_POLICY_CONFLICT:
115 dev_warn(ucsi->dev, "PPM Policy conflict\n");
116 break;
117 case UCSI_ERROR_SWAP_REJECTED:
118 dev_warn(ucsi->dev, "Swap rejected\n");
119 break;
120 case UCSI_ERROR_UNDEFINED:
121 default:
122 dev_err(ucsi->dev, "unknown error %u\n", error);
123 break;
124 }
125
126 return -EIO;
127 }
128
ucsi_exec_command(struct ucsi * ucsi,u64 cmd)129 static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd)
130 {
131 u32 cci;
132 int ret;
133
134 ret = ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
135 if (ret)
136 return ret;
137
138 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
139 if (ret)
140 return ret;
141
142 if (cmd != UCSI_CANCEL && cci & UCSI_CCI_BUSY)
143 return ucsi_exec_command(ucsi, UCSI_CANCEL);
144
145 if (!(cci & UCSI_CCI_COMMAND_COMPLETE))
146 return -EIO;
147
148 if (cci & UCSI_CCI_NOT_SUPPORTED) {
149 if (ucsi_acknowledge(ucsi, false) < 0)
150 dev_err(ucsi->dev,
151 "ACK of unsupported command failed\n");
152 return -EOPNOTSUPP;
153 }
154
155 if (cci & UCSI_CCI_ERROR) {
156 if (cmd == UCSI_GET_ERROR_STATUS) {
157 ret = ucsi_acknowledge(ucsi, false);
158 if (ret)
159 return ret;
160
161 return -EIO;
162 }
163 return ucsi_read_error(ucsi);
164 }
165
166 if (cmd == UCSI_CANCEL && cci & UCSI_CCI_CANCEL_COMPLETE) {
167 ret = ucsi_acknowledge(ucsi, false);
168 return ret ? ret : -EBUSY;
169 }
170
171 return UCSI_CCI_LENGTH(cci);
172 }
173
ucsi_send_command_common(struct ucsi * ucsi,u64 command,void * data,size_t size,bool conn_ack)174 static int ucsi_send_command_common(struct ucsi *ucsi, u64 command,
175 void *data, size_t size, bool conn_ack)
176 {
177 u8 length;
178 int ret;
179
180 mutex_lock(&ucsi->ppm_lock);
181
182 ret = ucsi_exec_command(ucsi, command);
183 if (ret < 0)
184 goto out;
185
186 length = ret;
187
188 if (data) {
189 ret = ucsi_read_message_in(ucsi, data, size);
190 if (ret)
191 goto out;
192 }
193
194 ret = ucsi_acknowledge(ucsi, conn_ack);
195 if (ret)
196 goto out;
197
198 ret = length;
199 out:
200 mutex_unlock(&ucsi->ppm_lock);
201 return ret;
202 }
203
ucsi_send_command(struct ucsi * ucsi,u64 command,void * data,size_t size)204 int ucsi_send_command(struct ucsi *ucsi, u64 command,
205 void *data, size_t size)
206 {
207 return ucsi_send_command_common(ucsi, command, data, size, false);
208 }
209 EXPORT_SYMBOL_GPL(ucsi_send_command);
210
211 /* -------------------------------------------------------------------------- */
212
213 struct ucsi_work {
214 struct delayed_work work;
215 struct list_head node;
216 unsigned long delay;
217 unsigned int count;
218 struct ucsi_connector *con;
219 int (*cb)(struct ucsi_connector *);
220 };
221
ucsi_poll_worker(struct work_struct * work)222 static void ucsi_poll_worker(struct work_struct *work)
223 {
224 struct ucsi_work *uwork = container_of(work, struct ucsi_work, work.work);
225 struct ucsi_connector *con = uwork->con;
226 int ret;
227
228 mutex_lock(&con->lock);
229
230 if (!con->partner) {
231 list_del(&uwork->node);
232 mutex_unlock(&con->lock);
233 kfree(uwork);
234 return;
235 }
236
237 ret = uwork->cb(con);
238
239 if (uwork->count-- && (ret == -EBUSY || ret == -ETIMEDOUT)) {
240 queue_delayed_work(con->wq, &uwork->work, uwork->delay);
241 } else {
242 list_del(&uwork->node);
243 kfree(uwork);
244 }
245
246 mutex_unlock(&con->lock);
247 }
248
ucsi_partner_task(struct ucsi_connector * con,int (* cb)(struct ucsi_connector *),int retries,unsigned long delay)249 static int ucsi_partner_task(struct ucsi_connector *con,
250 int (*cb)(struct ucsi_connector *),
251 int retries, unsigned long delay)
252 {
253 struct ucsi_work *uwork;
254
255 if (!con->partner)
256 return 0;
257
258 uwork = kzalloc(sizeof(*uwork), GFP_KERNEL);
259 if (!uwork)
260 return -ENOMEM;
261
262 INIT_DELAYED_WORK(&uwork->work, ucsi_poll_worker);
263 uwork->count = retries;
264 uwork->delay = delay;
265 uwork->con = con;
266 uwork->cb = cb;
267
268 list_add_tail(&uwork->node, &con->partner_tasks);
269 queue_delayed_work(con->wq, &uwork->work, delay);
270
271 return 0;
272 }
273
274 /* -------------------------------------------------------------------------- */
275
ucsi_altmode_update_active(struct ucsi_connector * con)276 void ucsi_altmode_update_active(struct ucsi_connector *con)
277 {
278 const struct typec_altmode *altmode = NULL;
279 u64 command;
280 int ret;
281 u8 cur;
282 int i;
283
284 command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
285 ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur));
286 if (ret < 0) {
287 if (con->ucsi->version > 0x0100) {
288 dev_err(con->ucsi->dev,
289 "GET_CURRENT_CAM command failed\n");
290 return;
291 }
292 cur = 0xff;
293 }
294
295 if (cur < UCSI_MAX_ALTMODES)
296 altmode = typec_altmode_get_partner(con->port_altmode[cur]);
297
298 for (i = 0; con->partner_altmode[i]; i++)
299 typec_altmode_update_active(con->partner_altmode[i],
300 con->partner_altmode[i] == altmode);
301 }
302
ucsi_altmode_next_mode(struct typec_altmode ** alt,u16 svid)303 static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
304 {
305 u8 mode = 1;
306 int i;
307
308 for (i = 0; alt[i]; i++) {
309 if (i > MODE_DISCOVERY_MAX)
310 return -ERANGE;
311
312 if (alt[i]->svid == svid)
313 mode++;
314 }
315
316 return mode;
317 }
318
ucsi_next_altmode(struct typec_altmode ** alt)319 static int ucsi_next_altmode(struct typec_altmode **alt)
320 {
321 int i = 0;
322
323 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
324 if (!alt[i])
325 return i;
326
327 return -ENOENT;
328 }
329
ucsi_get_num_altmode(struct typec_altmode ** alt)330 static int ucsi_get_num_altmode(struct typec_altmode **alt)
331 {
332 int i;
333
334 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
335 if (!alt[i])
336 break;
337
338 return i;
339 }
340
ucsi_register_altmode(struct ucsi_connector * con,struct typec_altmode_desc * desc,u8 recipient)341 static int ucsi_register_altmode(struct ucsi_connector *con,
342 struct typec_altmode_desc *desc,
343 u8 recipient)
344 {
345 struct typec_altmode *alt;
346 bool override;
347 int ret;
348 int i;
349
350 override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
351
352 switch (recipient) {
353 case UCSI_RECIPIENT_CON:
354 i = ucsi_next_altmode(con->port_altmode);
355 if (i < 0) {
356 ret = i;
357 goto err;
358 }
359
360 ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid);
361 if (ret < 0)
362 return ret;
363
364 desc->mode = ret;
365
366 switch (desc->svid) {
367 case USB_TYPEC_DP_SID:
368 alt = ucsi_register_displayport(con, override, i, desc);
369 break;
370 case USB_TYPEC_NVIDIA_VLINK_SID:
371 if (desc->vdo == USB_TYPEC_NVIDIA_VLINK_DBG_VDO)
372 alt = typec_port_register_altmode(con->port,
373 desc);
374 else
375 alt = ucsi_register_displayport(con, override,
376 i, desc);
377 break;
378 default:
379 alt = typec_port_register_altmode(con->port, desc);
380 break;
381 }
382
383 if (IS_ERR(alt)) {
384 ret = PTR_ERR(alt);
385 goto err;
386 }
387
388 con->port_altmode[i] = alt;
389 break;
390 case UCSI_RECIPIENT_SOP:
391 i = ucsi_next_altmode(con->partner_altmode);
392 if (i < 0) {
393 ret = i;
394 goto err;
395 }
396
397 ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid);
398 if (ret < 0)
399 return ret;
400
401 desc->mode = ret;
402
403 alt = typec_partner_register_altmode(con->partner, desc);
404 if (IS_ERR(alt)) {
405 ret = PTR_ERR(alt);
406 goto err;
407 }
408
409 con->partner_altmode[i] = alt;
410 break;
411 default:
412 return -EINVAL;
413 }
414
415 trace_ucsi_register_altmode(recipient, alt);
416
417 return 0;
418
419 err:
420 dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n",
421 desc->svid, desc->mode);
422
423 return ret;
424 }
425
426 static int
ucsi_register_altmodes_nvidia(struct ucsi_connector * con,u8 recipient)427 ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient)
428 {
429 int max_altmodes = UCSI_MAX_ALTMODES;
430 struct typec_altmode_desc desc;
431 struct ucsi_altmode alt;
432 struct ucsi_altmode orig[UCSI_MAX_ALTMODES];
433 struct ucsi_altmode updated[UCSI_MAX_ALTMODES];
434 struct ucsi *ucsi = con->ucsi;
435 bool multi_dp = false;
436 u64 command;
437 int ret;
438 int len;
439 int i;
440 int k = 0;
441
442 if (recipient == UCSI_RECIPIENT_CON)
443 max_altmodes = con->ucsi->cap.num_alt_modes;
444
445 memset(orig, 0, sizeof(orig));
446 memset(updated, 0, sizeof(updated));
447
448 /* First get all the alternate modes */
449 for (i = 0; i < max_altmodes; i++) {
450 memset(&alt, 0, sizeof(alt));
451 command = UCSI_GET_ALTERNATE_MODES;
452 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
453 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
454 command |= UCSI_GET_ALTMODE_OFFSET(i);
455 len = ucsi_send_command(con->ucsi, command, &alt, sizeof(alt));
456 /*
457 * We are collecting all altmodes first and then registering.
458 * Some type-C device will return zero length data beyond last
459 * alternate modes. We should not return if length is zero.
460 */
461 if (len < 0)
462 return len;
463
464 /* We got all altmodes, now break out and register them */
465 if (!len || !alt.svid)
466 break;
467
468 orig[k].mid = alt.mid;
469 orig[k].svid = alt.svid;
470 k++;
471 }
472 /*
473 * Update the original altmode table as some ppms may report
474 * multiple DP altmodes.
475 */
476 if (recipient == UCSI_RECIPIENT_CON)
477 multi_dp = ucsi->ops->update_altmodes(ucsi, orig, updated);
478
479 /* now register altmodes */
480 for (i = 0; i < max_altmodes; i++) {
481 memset(&desc, 0, sizeof(desc));
482 if (multi_dp && recipient == UCSI_RECIPIENT_CON) {
483 desc.svid = updated[i].svid;
484 desc.vdo = updated[i].mid;
485 } else {
486 desc.svid = orig[i].svid;
487 desc.vdo = orig[i].mid;
488 }
489 desc.roles = TYPEC_PORT_DRD;
490
491 if (!desc.svid)
492 return 0;
493
494 ret = ucsi_register_altmode(con, &desc, recipient);
495 if (ret)
496 return ret;
497 }
498
499 return 0;
500 }
501
ucsi_register_altmodes(struct ucsi_connector * con,u8 recipient)502 static int ucsi_register_altmodes(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[2];
507 u64 command;
508 int num;
509 int ret;
510 int len;
511 int j;
512 int i;
513
514 if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
515 return 0;
516
517 if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
518 return 0;
519
520 if (con->ucsi->ops->update_altmodes)
521 return ucsi_register_altmodes_nvidia(con, recipient);
522
523 if (recipient == UCSI_RECIPIENT_CON)
524 max_altmodes = con->ucsi->cap.num_alt_modes;
525
526 for (i = 0; i < max_altmodes;) {
527 memset(alt, 0, sizeof(alt));
528 command = UCSI_GET_ALTERNATE_MODES;
529 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
530 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
531 command |= UCSI_GET_ALTMODE_OFFSET(i);
532 len = ucsi_send_command(con->ucsi, command, alt, sizeof(alt));
533 if (len == -EBUSY)
534 continue;
535 if (len <= 0)
536 return len;
537
538 /*
539 * This code is requesting one alt mode at a time, but some PPMs
540 * may still return two. If that happens both alt modes need be
541 * registered and the offset for the next alt mode has to be
542 * incremented.
543 */
544 num = len / sizeof(alt[0]);
545 i += num;
546
547 for (j = 0; j < num; j++) {
548 if (!alt[j].svid)
549 return 0;
550
551 memset(&desc, 0, sizeof(desc));
552 desc.vdo = alt[j].mid;
553 desc.svid = alt[j].svid;
554 desc.roles = TYPEC_PORT_DRD;
555
556 ret = ucsi_register_altmode(con, &desc, recipient);
557 if (ret)
558 return ret;
559 }
560 }
561
562 return 0;
563 }
564
ucsi_unregister_altmodes(struct ucsi_connector * con,u8 recipient)565 static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
566 {
567 const struct typec_altmode *pdev;
568 struct typec_altmode **adev;
569 int i = 0;
570
571 switch (recipient) {
572 case UCSI_RECIPIENT_CON:
573 adev = con->port_altmode;
574 break;
575 case UCSI_RECIPIENT_SOP:
576 adev = con->partner_altmode;
577 break;
578 default:
579 return;
580 }
581
582 while (adev[i]) {
583 if (recipient == UCSI_RECIPIENT_SOP &&
584 (adev[i]->svid == USB_TYPEC_DP_SID ||
585 (adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID &&
586 adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) {
587 pdev = typec_altmode_get_partner(adev[i]);
588 ucsi_displayport_remove_partner((void *)pdev);
589 }
590 typec_unregister_altmode(adev[i]);
591 adev[i++] = NULL;
592 }
593 }
594
ucsi_read_pdos(struct ucsi_connector * con,enum typec_role role,int is_partner,u32 * pdos,int offset,int num_pdos)595 static int ucsi_read_pdos(struct ucsi_connector *con,
596 enum typec_role role, int is_partner,
597 u32 *pdos, int offset, int num_pdos)
598 {
599 struct ucsi *ucsi = con->ucsi;
600 u64 command;
601 int ret;
602
603 command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num);
604 command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner);
605 command |= UCSI_GET_PDOS_PDO_OFFSET(offset);
606 command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1);
607 command |= is_source(role) ? UCSI_GET_PDOS_SRC_PDOS : 0;
608 ret = ucsi_send_command(ucsi, command, pdos + offset,
609 num_pdos * sizeof(u32));
610 if (ret < 0 && ret != -ETIMEDOUT)
611 dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret);
612
613 return ret;
614 }
615
ucsi_get_pdos(struct ucsi_connector * con,enum typec_role role,int is_partner,u32 * pdos)616 static int ucsi_get_pdos(struct ucsi_connector *con, enum typec_role role,
617 int is_partner, u32 *pdos)
618 {
619 u8 num_pdos;
620 int ret;
621
622 /* UCSI max payload means only getting at most 4 PDOs at a time */
623 ret = ucsi_read_pdos(con, role, is_partner, pdos, 0, UCSI_MAX_PDOS);
624 if (ret < 0)
625 return ret;
626
627 num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */
628 if (num_pdos < UCSI_MAX_PDOS)
629 return num_pdos;
630
631 /* get the remaining PDOs, if any */
632 ret = ucsi_read_pdos(con, role, is_partner, pdos, UCSI_MAX_PDOS,
633 PDO_MAX_OBJECTS - UCSI_MAX_PDOS);
634 if (ret < 0)
635 return ret;
636
637 return ret / sizeof(u32) + num_pdos;
638 }
639
ucsi_get_src_pdos(struct ucsi_connector * con)640 static int ucsi_get_src_pdos(struct ucsi_connector *con)
641 {
642 int ret;
643
644 ret = ucsi_get_pdos(con, TYPEC_SOURCE, 1, con->src_pdos);
645 if (ret < 0)
646 return ret;
647
648 con->num_pdos = ret;
649
650 ucsi_port_psy_changed(con);
651
652 return ret;
653 }
654
ucsi_check_altmodes(struct ucsi_connector * con)655 static int ucsi_check_altmodes(struct ucsi_connector *con)
656 {
657 int ret, num_partner_am;
658
659 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
660 if (ret && ret != -ETIMEDOUT)
661 dev_err(con->ucsi->dev,
662 "con%d: failed to register partner alt modes (%d)\n",
663 con->num, ret);
664
665 /* Ignoring the errors in this case. */
666 if (con->partner_altmode[0]) {
667 num_partner_am = ucsi_get_num_altmode(con->partner_altmode);
668 if (num_partner_am > 0)
669 typec_partner_set_num_altmodes(con->partner, num_partner_am);
670 ucsi_altmode_update_active(con);
671 return 0;
672 }
673
674 return ret;
675 }
676
ucsi_register_partner_pdos(struct ucsi_connector * con)677 static int ucsi_register_partner_pdos(struct ucsi_connector *con)
678 {
679 struct usb_power_delivery_desc desc = { con->ucsi->cap.pd_version };
680 struct usb_power_delivery_capabilities_desc caps;
681 struct usb_power_delivery_capabilities *cap;
682 int ret;
683
684 if (con->partner_pd)
685 return 0;
686
687 con->partner_pd = usb_power_delivery_register(NULL, &desc);
688 if (IS_ERR(con->partner_pd))
689 return PTR_ERR(con->partner_pd);
690
691 ret = ucsi_get_pdos(con, TYPEC_SOURCE, 1, caps.pdo);
692 if (ret > 0) {
693 if (ret < PDO_MAX_OBJECTS)
694 caps.pdo[ret] = 0;
695
696 caps.role = TYPEC_SOURCE;
697 cap = usb_power_delivery_register_capabilities(con->partner_pd, &caps);
698 if (IS_ERR(cap))
699 return PTR_ERR(cap);
700
701 con->partner_source_caps = cap;
702 }
703
704 ret = ucsi_get_pdos(con, TYPEC_SINK, 1, caps.pdo);
705 if (ret > 0) {
706 if (ret < PDO_MAX_OBJECTS)
707 caps.pdo[ret] = 0;
708
709 caps.role = TYPEC_SINK;
710
711 cap = usb_power_delivery_register_capabilities(con->partner_pd, &caps);
712 if (IS_ERR(cap))
713 return PTR_ERR(cap);
714
715 con->partner_sink_caps = cap;
716 }
717
718 return typec_partner_set_usb_power_delivery(con->partner, con->partner_pd);
719 }
720
ucsi_unregister_partner_pdos(struct ucsi_connector * con)721 static void ucsi_unregister_partner_pdos(struct ucsi_connector *con)
722 {
723 usb_power_delivery_unregister_capabilities(con->partner_sink_caps);
724 con->partner_sink_caps = NULL;
725 usb_power_delivery_unregister_capabilities(con->partner_source_caps);
726 con->partner_source_caps = NULL;
727 usb_power_delivery_unregister(con->partner_pd);
728 con->partner_pd = NULL;
729 }
730
ucsi_pwr_opmode_change(struct ucsi_connector * con)731 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
732 {
733 switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
734 case UCSI_CONSTAT_PWR_OPMODE_PD:
735 con->rdo = con->status.request_data_obj;
736 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
737 ucsi_partner_task(con, ucsi_get_src_pdos, 30, 0);
738 ucsi_partner_task(con, ucsi_check_altmodes, 30, 0);
739 ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ);
740 break;
741 case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
742 con->rdo = 0;
743 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
744 break;
745 case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
746 con->rdo = 0;
747 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
748 break;
749 default:
750 con->rdo = 0;
751 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
752 break;
753 }
754 }
755
ucsi_register_partner(struct ucsi_connector * con)756 static int ucsi_register_partner(struct ucsi_connector *con)
757 {
758 u8 pwr_opmode = UCSI_CONSTAT_PWR_OPMODE(con->status.flags);
759 struct typec_partner_desc desc;
760 struct typec_partner *partner;
761
762 if (con->partner)
763 return 0;
764
765 memset(&desc, 0, sizeof(desc));
766
767 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
768 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
769 desc.accessory = TYPEC_ACCESSORY_DEBUG;
770 break;
771 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
772 desc.accessory = TYPEC_ACCESSORY_AUDIO;
773 break;
774 default:
775 break;
776 }
777
778 desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD;
779
780 partner = typec_register_partner(con->port, &desc);
781 if (IS_ERR(partner)) {
782 dev_err(con->ucsi->dev,
783 "con%d: failed to register partner (%ld)\n", con->num,
784 PTR_ERR(partner));
785 return PTR_ERR(partner);
786 }
787
788 con->partner = partner;
789
790 return 0;
791 }
792
ucsi_unregister_partner(struct ucsi_connector * con)793 static void ucsi_unregister_partner(struct ucsi_connector *con)
794 {
795 if (!con->partner)
796 return;
797
798 typec_set_mode(con->port, TYPEC_STATE_SAFE);
799
800 typec_partner_set_usb_power_delivery(con->partner, NULL);
801 ucsi_unregister_partner_pdos(con);
802 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
803 typec_unregister_partner(con->partner);
804 con->partner = NULL;
805 }
806
ucsi_partner_change(struct ucsi_connector * con)807 static void ucsi_partner_change(struct ucsi_connector *con)
808 {
809 enum usb_role u_role = USB_ROLE_NONE;
810 int ret;
811
812 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
813 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
814 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
815 u_role = USB_ROLE_HOST;
816 fallthrough;
817 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
818 typec_set_data_role(con->port, TYPEC_HOST);
819 break;
820 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
821 u_role = USB_ROLE_DEVICE;
822 typec_set_data_role(con->port, TYPEC_DEVICE);
823 break;
824 default:
825 break;
826 }
827
828 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
829 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
830 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
831 typec_set_mode(con->port, TYPEC_MODE_DEBUG);
832 break;
833 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
834 typec_set_mode(con->port, TYPEC_MODE_AUDIO);
835 break;
836 default:
837 if (UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) ==
838 UCSI_CONSTAT_PARTNER_FLAG_USB)
839 typec_set_mode(con->port, TYPEC_STATE_USB);
840 }
841 }
842
843 /* Only notify USB controller if partner supports USB data */
844 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
845 u_role = USB_ROLE_NONE;
846
847 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
848 if (ret)
849 dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n",
850 con->num, u_role);
851 }
852
ucsi_check_connection(struct ucsi_connector * con)853 static int ucsi_check_connection(struct ucsi_connector *con)
854 {
855 u8 prev_flags = con->status.flags;
856 u64 command;
857 int ret;
858
859 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
860 ret = ucsi_send_command(con->ucsi, command, &con->status, sizeof(con->status));
861 if (ret < 0) {
862 dev_err(con->ucsi->dev, "GET_CONNECTOR_STATUS failed (%d)\n", ret);
863 return ret;
864 }
865
866 if (con->status.flags == prev_flags)
867 return 0;
868
869 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
870 ucsi_register_partner(con);
871 ucsi_pwr_opmode_change(con);
872 ucsi_partner_change(con);
873 } else {
874 ucsi_partner_change(con);
875 ucsi_port_psy_changed(con);
876 ucsi_unregister_partner(con);
877 }
878
879 return 0;
880 }
881
ucsi_handle_connector_change(struct work_struct * work)882 static void ucsi_handle_connector_change(struct work_struct *work)
883 {
884 struct ucsi_connector *con = container_of(work, struct ucsi_connector,
885 work);
886 struct ucsi *ucsi = con->ucsi;
887 enum typec_role role;
888 u64 command;
889 int ret;
890
891 mutex_lock(&con->lock);
892
893 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
894
895 ret = ucsi_send_command_common(ucsi, command, &con->status,
896 sizeof(con->status), true);
897 if (ret < 0) {
898 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
899 __func__, ret);
900 clear_bit(EVENT_PENDING, &con->ucsi->flags);
901 goto out_unlock;
902 }
903
904 trace_ucsi_connector_change(con->num, &con->status);
905
906 role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
907
908 if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
909 typec_set_pwr_role(con->port, role);
910
911 /* Complete pending power role swap */
912 if (!completion_done(&con->complete))
913 complete(&con->complete);
914 }
915
916 if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
917 typec_set_pwr_role(con->port, role);
918 ucsi_port_psy_changed(con);
919 ucsi_partner_change(con);
920
921 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
922 ucsi_register_partner(con);
923 ucsi_partner_task(con, ucsi_check_connection, 1, HZ);
924
925 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) ==
926 UCSI_CONSTAT_PWR_OPMODE_PD)
927 ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ);
928 } else {
929 ucsi_unregister_partner(con);
930 }
931 }
932
933 if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE ||
934 con->status.change & UCSI_CONSTAT_POWER_LEVEL_CHANGE)
935 ucsi_pwr_opmode_change(con);
936
937 if (con->partner && con->status.change & UCSI_CONSTAT_PARTNER_CHANGE) {
938 ucsi_partner_change(con);
939
940 /* Complete pending data role swap */
941 if (!completion_done(&con->complete))
942 complete(&con->complete);
943 }
944
945 if (con->status.change & UCSI_CONSTAT_CAM_CHANGE)
946 ucsi_partner_task(con, ucsi_check_altmodes, 1, 0);
947
948 out_unlock:
949 mutex_unlock(&con->lock);
950 }
951
952 /**
953 * ucsi_connector_change - Process Connector Change Event
954 * @ucsi: UCSI Interface
955 * @num: Connector number
956 */
ucsi_connector_change(struct ucsi * ucsi,u8 num)957 void ucsi_connector_change(struct ucsi *ucsi, u8 num)
958 {
959 struct ucsi_connector *con = &ucsi->connector[num - 1];
960
961 if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
962 dev_dbg(ucsi->dev, "Early connector change event\n");
963 return;
964 }
965
966 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
967 schedule_work(&con->work);
968 }
969 EXPORT_SYMBOL_GPL(ucsi_connector_change);
970
971 /* -------------------------------------------------------------------------- */
972
ucsi_reset_connector(struct ucsi_connector * con,bool hard)973 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
974 {
975 u64 command;
976
977 command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num);
978 command |= hard ? UCSI_CONNECTOR_RESET_HARD : 0;
979
980 return ucsi_send_command(con->ucsi, command, NULL, 0);
981 }
982
ucsi_reset_ppm(struct ucsi * ucsi)983 static int ucsi_reset_ppm(struct ucsi *ucsi)
984 {
985 u64 command;
986 unsigned long tmo;
987 u32 cci;
988 int ret;
989
990 mutex_lock(&ucsi->ppm_lock);
991
992 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
993 if (ret < 0)
994 goto out;
995
996 /*
997 * If UCSI_CCI_RESET_COMPLETE is already set we must clear
998 * the flag before we start another reset. Send a
999 * UCSI_SET_NOTIFICATION_ENABLE command to achieve this.
1000 * Ignore a timeout and try the reset anyway if this fails.
1001 */
1002 if (cci & UCSI_CCI_RESET_COMPLETE) {
1003 command = UCSI_SET_NOTIFICATION_ENABLE;
1004 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command,
1005 sizeof(command));
1006 if (ret < 0)
1007 goto out;
1008
1009 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
1010 do {
1011 ret = ucsi->ops->read(ucsi, UCSI_CCI,
1012 &cci, sizeof(cci));
1013 if (ret < 0)
1014 goto out;
1015 if (cci & UCSI_CCI_COMMAND_COMPLETE)
1016 break;
1017 if (time_is_before_jiffies(tmo))
1018 break;
1019 msleep(20);
1020 } while (1);
1021
1022 WARN_ON(cci & UCSI_CCI_RESET_COMPLETE);
1023 }
1024
1025 command = UCSI_PPM_RESET;
1026 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command,
1027 sizeof(command));
1028 if (ret < 0)
1029 goto out;
1030
1031 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
1032
1033 do {
1034 if (time_is_before_jiffies(tmo)) {
1035 ret = -ETIMEDOUT;
1036 goto out;
1037 }
1038
1039 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
1040 if (ret)
1041 goto out;
1042
1043 /* If the PPM is still doing something else, reset it again. */
1044 if (cci & ~UCSI_CCI_RESET_COMPLETE) {
1045 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL,
1046 &command,
1047 sizeof(command));
1048 if (ret < 0)
1049 goto out;
1050 }
1051
1052 msleep(20);
1053 } while (!(cci & UCSI_CCI_RESET_COMPLETE));
1054
1055 out:
1056 mutex_unlock(&ucsi->ppm_lock);
1057 return ret;
1058 }
1059
ucsi_role_cmd(struct ucsi_connector * con,u64 command)1060 static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)
1061 {
1062 int ret;
1063
1064 ret = ucsi_send_command(con->ucsi, command, NULL, 0);
1065 if (ret == -ETIMEDOUT) {
1066 u64 c;
1067
1068 /* PPM most likely stopped responding. Resetting everything. */
1069 ucsi_reset_ppm(con->ucsi);
1070
1071 c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
1072 ucsi_send_command(con->ucsi, c, NULL, 0);
1073
1074 ucsi_reset_connector(con, true);
1075 }
1076
1077 return ret;
1078 }
1079
ucsi_dr_swap(struct typec_port * port,enum typec_data_role role)1080 static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
1081 {
1082 struct ucsi_connector *con = typec_get_drvdata(port);
1083 u8 partner_type;
1084 u64 command;
1085 int ret = 0;
1086
1087 mutex_lock(&con->lock);
1088
1089 if (!con->partner) {
1090 ret = -ENOTCONN;
1091 goto out_unlock;
1092 }
1093
1094 partner_type = UCSI_CONSTAT_PARTNER_TYPE(con->status.flags);
1095 if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
1096 role == TYPEC_DEVICE) ||
1097 (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
1098 role == TYPEC_HOST))
1099 goto out_unlock;
1100
1101 reinit_completion(&con->complete);
1102
1103 command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num);
1104 command |= UCSI_SET_UOR_ROLE(role);
1105 command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS;
1106 ret = ucsi_role_cmd(con, command);
1107 if (ret < 0)
1108 goto out_unlock;
1109
1110 mutex_unlock(&con->lock);
1111
1112 if (!wait_for_completion_timeout(&con->complete,
1113 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1114 return -ETIMEDOUT;
1115
1116 return 0;
1117
1118 out_unlock:
1119 mutex_unlock(&con->lock);
1120
1121 return ret;
1122 }
1123
ucsi_pr_swap(struct typec_port * port,enum typec_role role)1124 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
1125 {
1126 struct ucsi_connector *con = typec_get_drvdata(port);
1127 enum typec_role cur_role;
1128 u64 command;
1129 int ret = 0;
1130
1131 mutex_lock(&con->lock);
1132
1133 if (!con->partner) {
1134 ret = -ENOTCONN;
1135 goto out_unlock;
1136 }
1137
1138 cur_role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
1139
1140 if (cur_role == role)
1141 goto out_unlock;
1142
1143 reinit_completion(&con->complete);
1144
1145 command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num);
1146 command |= UCSI_SET_PDR_ROLE(role);
1147 command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS;
1148 ret = ucsi_role_cmd(con, command);
1149 if (ret < 0)
1150 goto out_unlock;
1151
1152 mutex_unlock(&con->lock);
1153
1154 if (!wait_for_completion_timeout(&con->complete,
1155 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1156 return -ETIMEDOUT;
1157
1158 mutex_lock(&con->lock);
1159
1160 /* Something has gone wrong while swapping the role */
1161 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) !=
1162 UCSI_CONSTAT_PWR_OPMODE_PD) {
1163 ucsi_reset_connector(con, true);
1164 ret = -EPROTO;
1165 }
1166
1167 out_unlock:
1168 mutex_unlock(&con->lock);
1169
1170 return ret;
1171 }
1172
1173 static const struct typec_operations ucsi_ops = {
1174 .dr_set = ucsi_dr_swap,
1175 .pr_set = ucsi_pr_swap
1176 };
1177
1178 /* Caller must call fwnode_handle_put() after use */
ucsi_find_fwnode(struct ucsi_connector * con)1179 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
1180 {
1181 struct fwnode_handle *fwnode;
1182 int i = 1;
1183
1184 device_for_each_child_node(con->ucsi->dev, fwnode)
1185 if (i++ == con->num)
1186 return fwnode;
1187 return NULL;
1188 }
1189
ucsi_register_port(struct ucsi * ucsi,struct ucsi_connector * con)1190 static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con)
1191 {
1192 struct usb_power_delivery_desc desc = { ucsi->cap.pd_version};
1193 struct usb_power_delivery_capabilities_desc pd_caps;
1194 struct usb_power_delivery_capabilities *pd_cap;
1195 struct typec_capability *cap = &con->typec_cap;
1196 enum typec_accessory *accessory = cap->accessory;
1197 enum usb_role u_role = USB_ROLE_NONE;
1198 u64 command;
1199 char *name;
1200 int ret;
1201
1202 name = kasprintf(GFP_KERNEL, "%s-con%d", dev_name(ucsi->dev), con->num);
1203 if (!name)
1204 return -ENOMEM;
1205
1206 con->wq = create_singlethread_workqueue(name);
1207 kfree(name);
1208 if (!con->wq)
1209 return -ENOMEM;
1210
1211 INIT_WORK(&con->work, ucsi_handle_connector_change);
1212 init_completion(&con->complete);
1213 mutex_init(&con->lock);
1214 INIT_LIST_HEAD(&con->partner_tasks);
1215 con->ucsi = ucsi;
1216
1217 cap->fwnode = ucsi_find_fwnode(con);
1218 con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode);
1219 if (IS_ERR(con->usb_role_sw))
1220 return dev_err_probe(ucsi->dev, PTR_ERR(con->usb_role_sw),
1221 "con%d: failed to get usb role switch\n", con->num);
1222
1223 /* Delay other interactions with the con until registration is complete */
1224 mutex_lock(&con->lock);
1225
1226 /* Get connector capability */
1227 command = UCSI_GET_CONNECTOR_CAPABILITY;
1228 command |= UCSI_CONNECTOR_NUMBER(con->num);
1229 ret = ucsi_send_command(ucsi, command, &con->cap, sizeof(con->cap));
1230 if (ret < 0)
1231 goto out_unlock;
1232
1233 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP)
1234 cap->data = TYPEC_PORT_DRD;
1235 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP)
1236 cap->data = TYPEC_PORT_DFP;
1237 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP)
1238 cap->data = TYPEC_PORT_UFP;
1239
1240 if ((con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER) &&
1241 (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER))
1242 cap->type = TYPEC_PORT_DRP;
1243 else if (con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER)
1244 cap->type = TYPEC_PORT_SRC;
1245 else if (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER)
1246 cap->type = TYPEC_PORT_SNK;
1247
1248 cap->revision = ucsi->cap.typec_version;
1249 cap->pd_revision = ucsi->cap.pd_version;
1250 cap->svdm_version = SVDM_VER_2_0;
1251 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
1252
1253 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY)
1254 *accessory++ = TYPEC_ACCESSORY_AUDIO;
1255 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
1256 *accessory = TYPEC_ACCESSORY_DEBUG;
1257
1258 cap->driver_data = con;
1259 cap->ops = &ucsi_ops;
1260
1261 ret = ucsi_register_port_psy(con);
1262 if (ret)
1263 goto out;
1264
1265 /* Register the connector */
1266 con->port = typec_register_port(ucsi->dev, cap);
1267 if (IS_ERR(con->port)) {
1268 ret = PTR_ERR(con->port);
1269 goto out;
1270 }
1271
1272 con->pd = usb_power_delivery_register(ucsi->dev, &desc);
1273
1274 ret = ucsi_get_pdos(con, TYPEC_SOURCE, 0, pd_caps.pdo);
1275 if (ret > 0) {
1276 if (ret < PDO_MAX_OBJECTS)
1277 pd_caps.pdo[ret] = 0;
1278
1279 pd_caps.role = TYPEC_SOURCE;
1280 pd_cap = usb_power_delivery_register_capabilities(con->pd, &pd_caps);
1281 if (IS_ERR(pd_cap)) {
1282 ret = PTR_ERR(pd_cap);
1283 goto out;
1284 }
1285
1286 con->port_source_caps = pd_cap;
1287 }
1288
1289 memset(&pd_caps, 0, sizeof(pd_caps));
1290 ret = ucsi_get_pdos(con, TYPEC_SINK, 0, pd_caps.pdo);
1291 if (ret > 0) {
1292 if (ret < PDO_MAX_OBJECTS)
1293 pd_caps.pdo[ret] = 0;
1294
1295 pd_caps.role = TYPEC_SINK;
1296 pd_cap = usb_power_delivery_register_capabilities(con->pd, &pd_caps);
1297 if (IS_ERR(pd_cap)) {
1298 ret = PTR_ERR(pd_cap);
1299 goto out;
1300 }
1301
1302 con->port_sink_caps = pd_cap;
1303 }
1304
1305 typec_port_set_usb_power_delivery(con->port, con->pd);
1306
1307 /* Alternate modes */
1308 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON);
1309 if (ret) {
1310 dev_err(ucsi->dev, "con%d: failed to register alt modes\n",
1311 con->num);
1312 goto out;
1313 }
1314
1315 /* Get the status */
1316 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
1317 ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
1318 if (ret < 0) {
1319 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
1320 ret = 0;
1321 goto out;
1322 }
1323 ret = 0; /* ucsi_send_command() returns length on success */
1324
1325 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1326 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1327 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1328 u_role = USB_ROLE_HOST;
1329 fallthrough;
1330 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1331 typec_set_data_role(con->port, TYPEC_HOST);
1332 break;
1333 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1334 u_role = USB_ROLE_DEVICE;
1335 typec_set_data_role(con->port, TYPEC_DEVICE);
1336 break;
1337 default:
1338 break;
1339 }
1340
1341 /* Check if there is already something connected */
1342 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1343 typec_set_pwr_role(con->port,
1344 !!(con->status.flags & UCSI_CONSTAT_PWR_DIR));
1345 ucsi_register_partner(con);
1346 ucsi_pwr_opmode_change(con);
1347 ucsi_port_psy_changed(con);
1348 }
1349
1350 /* Only notify USB controller if partner supports USB data */
1351 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
1352 u_role = USB_ROLE_NONE;
1353
1354 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1355 if (ret) {
1356 dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n",
1357 con->num, u_role);
1358 ret = 0;
1359 }
1360
1361 if (con->partner &&
1362 UCSI_CONSTAT_PWR_OPMODE(con->status.flags) ==
1363 UCSI_CONSTAT_PWR_OPMODE_PD) {
1364 ucsi_get_src_pdos(con);
1365 ucsi_check_altmodes(con);
1366 }
1367
1368 trace_ucsi_register_port(con->num, &con->status);
1369
1370 out:
1371 fwnode_handle_put(cap->fwnode);
1372 out_unlock:
1373 mutex_unlock(&con->lock);
1374
1375 if (ret && con->wq) {
1376 destroy_workqueue(con->wq);
1377 con->wq = NULL;
1378 }
1379
1380 return ret;
1381 }
1382
1383 /**
1384 * ucsi_init - Initialize UCSI interface
1385 * @ucsi: UCSI to be initialized
1386 *
1387 * Registers all ports @ucsi has and enables all notification events.
1388 */
ucsi_init(struct ucsi * ucsi)1389 static int ucsi_init(struct ucsi *ucsi)
1390 {
1391 struct ucsi_connector *con, *connector;
1392 u64 command, ntfy;
1393 u32 cci;
1394 int ret;
1395 int i;
1396
1397 /* Reset the PPM */
1398 ret = ucsi_reset_ppm(ucsi);
1399 if (ret) {
1400 dev_err(ucsi->dev, "failed to reset PPM!\n");
1401 goto err;
1402 }
1403
1404 /* Enable basic notifications */
1405 ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
1406 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1407 ret = ucsi_send_command(ucsi, command, NULL, 0);
1408 if (ret < 0)
1409 goto err_reset;
1410
1411 /* Get PPM capabilities */
1412 command = UCSI_GET_CAPABILITY;
1413 ret = ucsi_send_command(ucsi, command, &ucsi->cap, sizeof(ucsi->cap));
1414 if (ret < 0)
1415 goto err_reset;
1416
1417 if (!ucsi->cap.num_connectors) {
1418 ret = -ENODEV;
1419 goto err_reset;
1420 }
1421
1422 /* Allocate the connectors. Released in ucsi_unregister() */
1423 connector = kcalloc(ucsi->cap.num_connectors + 1, sizeof(*connector), GFP_KERNEL);
1424 if (!connector) {
1425 ret = -ENOMEM;
1426 goto err_reset;
1427 }
1428
1429 /* Register all connectors */
1430 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1431 connector[i].num = i + 1;
1432 ret = ucsi_register_port(ucsi, &connector[i]);
1433 if (ret)
1434 goto err_unregister;
1435 }
1436
1437 /* Enable all notifications */
1438 ntfy = UCSI_ENABLE_NTFY_ALL;
1439 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1440 ret = ucsi_send_command(ucsi, command, NULL, 0);
1441 if (ret < 0)
1442 goto err_unregister;
1443
1444 ucsi->connector = connector;
1445 ucsi->ntfy = ntfy;
1446
1447 mutex_lock(&ucsi->ppm_lock);
1448 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
1449 mutex_unlock(&ucsi->ppm_lock);
1450 if (ret)
1451 return ret;
1452 if (UCSI_CCI_CONNECTOR(cci))
1453 ucsi_connector_change(ucsi, UCSI_CCI_CONNECTOR(cci));
1454
1455 return 0;
1456
1457 err_unregister:
1458 for (con = connector; con->port; con++) {
1459 ucsi_unregister_partner(con);
1460 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON);
1461 ucsi_unregister_port_psy(con);
1462 if (con->wq)
1463 destroy_workqueue(con->wq);
1464
1465 usb_power_delivery_unregister_capabilities(con->port_sink_caps);
1466 con->port_sink_caps = NULL;
1467 usb_power_delivery_unregister_capabilities(con->port_source_caps);
1468 con->port_source_caps = NULL;
1469 usb_power_delivery_unregister(con->pd);
1470 con->pd = NULL;
1471 typec_unregister_port(con->port);
1472 con->port = NULL;
1473 }
1474 kfree(connector);
1475 err_reset:
1476 memset(&ucsi->cap, 0, sizeof(ucsi->cap));
1477 ucsi_reset_ppm(ucsi);
1478 err:
1479 return ret;
1480 }
1481
ucsi_resume_work(struct work_struct * work)1482 static void ucsi_resume_work(struct work_struct *work)
1483 {
1484 struct ucsi *ucsi = container_of(work, struct ucsi, resume_work);
1485 struct ucsi_connector *con;
1486 u64 command;
1487 int ret;
1488
1489 /* Restore UCSI notification enable mask after system resume */
1490 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1491 ret = ucsi_send_command(ucsi, command, NULL, 0);
1492 if (ret < 0) {
1493 dev_err(ucsi->dev, "failed to re-enable notifications (%d)\n", ret);
1494 return;
1495 }
1496
1497 for (con = ucsi->connector; con->port; con++) {
1498 mutex_lock(&con->lock);
1499 ucsi_partner_task(con, ucsi_check_connection, 1, 0);
1500 mutex_unlock(&con->lock);
1501 }
1502 }
1503
ucsi_resume(struct ucsi * ucsi)1504 int ucsi_resume(struct ucsi *ucsi)
1505 {
1506 if (ucsi->connector)
1507 queue_work(system_long_wq, &ucsi->resume_work);
1508 return 0;
1509 }
1510 EXPORT_SYMBOL_GPL(ucsi_resume);
1511
ucsi_init_work(struct work_struct * work)1512 static void ucsi_init_work(struct work_struct *work)
1513 {
1514 struct ucsi *ucsi = container_of(work, struct ucsi, work.work);
1515 int ret;
1516
1517 ret = ucsi_init(ucsi);
1518 if (ret)
1519 dev_err_probe(ucsi->dev, ret, "PPM init failed\n");
1520
1521 if (ret == -EPROBE_DEFER) {
1522 if (ucsi->work_count++ > UCSI_ROLE_SWITCH_WAIT_COUNT) {
1523 dev_err(ucsi->dev, "PPM init failed, stop trying\n");
1524 return;
1525 }
1526
1527 queue_delayed_work(system_long_wq, &ucsi->work,
1528 UCSI_ROLE_SWITCH_INTERVAL);
1529 }
1530 }
1531
1532 /**
1533 * ucsi_get_drvdata - Return private driver data pointer
1534 * @ucsi: UCSI interface
1535 */
ucsi_get_drvdata(struct ucsi * ucsi)1536 void *ucsi_get_drvdata(struct ucsi *ucsi)
1537 {
1538 return ucsi->driver_data;
1539 }
1540 EXPORT_SYMBOL_GPL(ucsi_get_drvdata);
1541
1542 /**
1543 * ucsi_set_drvdata - Assign private driver data pointer
1544 * @ucsi: UCSI interface
1545 * @data: Private data pointer
1546 */
ucsi_set_drvdata(struct ucsi * ucsi,void * data)1547 void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
1548 {
1549 ucsi->driver_data = data;
1550 }
1551 EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
1552
1553 /**
1554 * ucsi_create - Allocate UCSI instance
1555 * @dev: Device interface to the PPM (Platform Policy Manager)
1556 * @ops: I/O routines
1557 */
ucsi_create(struct device * dev,const struct ucsi_operations * ops)1558 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
1559 {
1560 struct ucsi *ucsi;
1561
1562 if (!ops || !ops->read || !ops->sync_write || !ops->async_write)
1563 return ERR_PTR(-EINVAL);
1564
1565 ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
1566 if (!ucsi)
1567 return ERR_PTR(-ENOMEM);
1568
1569 INIT_WORK(&ucsi->resume_work, ucsi_resume_work);
1570 INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work);
1571 mutex_init(&ucsi->ppm_lock);
1572 ucsi->dev = dev;
1573 ucsi->ops = ops;
1574
1575 return ucsi;
1576 }
1577 EXPORT_SYMBOL_GPL(ucsi_create);
1578
1579 /**
1580 * ucsi_destroy - Free UCSI instance
1581 * @ucsi: UCSI instance to be freed
1582 */
ucsi_destroy(struct ucsi * ucsi)1583 void ucsi_destroy(struct ucsi *ucsi)
1584 {
1585 ucsi_debugfs_unregister(ucsi);
1586 kfree(ucsi);
1587 }
1588 EXPORT_SYMBOL_GPL(ucsi_destroy);
1589
1590 /**
1591 * ucsi_register - Register UCSI interface
1592 * @ucsi: UCSI instance
1593 */
ucsi_register(struct ucsi * ucsi)1594 int ucsi_register(struct ucsi *ucsi)
1595 {
1596 int ret;
1597
1598 ret = ucsi->ops->read(ucsi, UCSI_VERSION, &ucsi->version,
1599 sizeof(ucsi->version));
1600 if (ret)
1601 return ret;
1602
1603 if (!ucsi->version)
1604 return -ENODEV;
1605
1606 /*
1607 * Version format is JJ.M.N (JJ = Major version, M = Minor version,
1608 * N = sub-minor version).
1609 */
1610 dev_dbg(ucsi->dev, "Registered UCSI interface with version %x.%x.%x",
1611 UCSI_BCD_GET_MAJOR(ucsi->version),
1612 UCSI_BCD_GET_MINOR(ucsi->version),
1613 UCSI_BCD_GET_SUBMINOR(ucsi->version));
1614
1615 queue_delayed_work(system_long_wq, &ucsi->work, 0);
1616
1617 ucsi_debugfs_register(ucsi);
1618 return 0;
1619 }
1620 EXPORT_SYMBOL_GPL(ucsi_register);
1621
1622 /**
1623 * ucsi_unregister - Unregister UCSI interface
1624 * @ucsi: UCSI interface to be unregistered
1625 *
1626 * Unregister UCSI interface that was created with ucsi_register().
1627 */
ucsi_unregister(struct ucsi * ucsi)1628 void ucsi_unregister(struct ucsi *ucsi)
1629 {
1630 u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
1631 int i;
1632
1633 /* Make sure that we are not in the middle of driver initialization */
1634 cancel_delayed_work_sync(&ucsi->work);
1635 cancel_work_sync(&ucsi->resume_work);
1636
1637 /* Disable notifications */
1638 ucsi->ops->async_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
1639
1640 if (!ucsi->connector)
1641 return;
1642
1643 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1644 cancel_work_sync(&ucsi->connector[i].work);
1645 ucsi_unregister_partner(&ucsi->connector[i]);
1646 ucsi_unregister_altmodes(&ucsi->connector[i],
1647 UCSI_RECIPIENT_CON);
1648 ucsi_unregister_port_psy(&ucsi->connector[i]);
1649
1650 if (ucsi->connector[i].wq) {
1651 struct ucsi_work *uwork;
1652
1653 mutex_lock(&ucsi->connector[i].lock);
1654 /*
1655 * queue delayed items immediately so they can execute
1656 * and free themselves before the wq is destroyed
1657 */
1658 list_for_each_entry(uwork, &ucsi->connector[i].partner_tasks, node)
1659 mod_delayed_work(ucsi->connector[i].wq, &uwork->work, 0);
1660 mutex_unlock(&ucsi->connector[i].lock);
1661 destroy_workqueue(ucsi->connector[i].wq);
1662 }
1663
1664 usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_sink_caps);
1665 ucsi->connector[i].port_sink_caps = NULL;
1666 usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_source_caps);
1667 ucsi->connector[i].port_source_caps = NULL;
1668 usb_power_delivery_unregister(ucsi->connector[i].pd);
1669 ucsi->connector[i].pd = NULL;
1670 typec_unregister_port(ucsi->connector[i].port);
1671 }
1672
1673 kfree(ucsi->connector);
1674 }
1675 EXPORT_SYMBOL_GPL(ucsi_unregister);
1676
ucsi_module_init(void)1677 static int __init ucsi_module_init(void)
1678 {
1679 ucsi_debugfs_init();
1680 return 0;
1681 }
1682 module_init(ucsi_module_init);
1683
ucsi_module_exit(void)1684 static void __exit ucsi_module_exit(void)
1685 {
1686 ucsi_debugfs_exit();
1687 }
1688 module_exit(ucsi_module_exit);
1689
1690 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1691 MODULE_LICENSE("GPL v2");
1692 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");
1693