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 if (ucsi->ops->connector_status)
907 ucsi->ops->connector_status(con);
908
909 role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
910
911 if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
912 typec_set_pwr_role(con->port, role);
913
914 /* Complete pending power role swap */
915 if (!completion_done(&con->complete))
916 complete(&con->complete);
917 }
918
919 if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
920 typec_set_pwr_role(con->port, role);
921 ucsi_port_psy_changed(con);
922 ucsi_partner_change(con);
923
924 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
925 ucsi_register_partner(con);
926 ucsi_partner_task(con, ucsi_check_connection, 1, HZ);
927
928 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) ==
929 UCSI_CONSTAT_PWR_OPMODE_PD)
930 ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ);
931 } else {
932 ucsi_unregister_partner(con);
933 }
934 }
935
936 if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE ||
937 con->status.change & UCSI_CONSTAT_POWER_LEVEL_CHANGE)
938 ucsi_pwr_opmode_change(con);
939
940 if (con->partner && con->status.change & UCSI_CONSTAT_PARTNER_CHANGE) {
941 ucsi_partner_change(con);
942
943 /* Complete pending data role swap */
944 if (!completion_done(&con->complete))
945 complete(&con->complete);
946 }
947
948 if (con->status.change & UCSI_CONSTAT_CAM_CHANGE)
949 ucsi_partner_task(con, ucsi_check_altmodes, 1, 0);
950
951 out_unlock:
952 mutex_unlock(&con->lock);
953 }
954
955 /**
956 * ucsi_connector_change - Process Connector Change Event
957 * @ucsi: UCSI Interface
958 * @num: Connector number
959 */
ucsi_connector_change(struct ucsi * ucsi,u8 num)960 void ucsi_connector_change(struct ucsi *ucsi, u8 num)
961 {
962 struct ucsi_connector *con = &ucsi->connector[num - 1];
963
964 if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
965 dev_dbg(ucsi->dev, "Early connector change event\n");
966 return;
967 }
968
969 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
970 schedule_work(&con->work);
971 }
972 EXPORT_SYMBOL_GPL(ucsi_connector_change);
973
974 /* -------------------------------------------------------------------------- */
975
ucsi_reset_connector(struct ucsi_connector * con,bool hard)976 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
977 {
978 u64 command;
979
980 command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num);
981 command |= hard ? UCSI_CONNECTOR_RESET_HARD : 0;
982
983 return ucsi_send_command(con->ucsi, command, NULL, 0);
984 }
985
ucsi_reset_ppm(struct ucsi * ucsi)986 static int ucsi_reset_ppm(struct ucsi *ucsi)
987 {
988 u64 command;
989 unsigned long tmo;
990 u32 cci;
991 int ret;
992
993 mutex_lock(&ucsi->ppm_lock);
994
995 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
996 if (ret < 0)
997 goto out;
998
999 /*
1000 * If UCSI_CCI_RESET_COMPLETE is already set we must clear
1001 * the flag before we start another reset. Send a
1002 * UCSI_SET_NOTIFICATION_ENABLE command to achieve this.
1003 * Ignore a timeout and try the reset anyway if this fails.
1004 */
1005 if (cci & UCSI_CCI_RESET_COMPLETE) {
1006 command = UCSI_SET_NOTIFICATION_ENABLE;
1007 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command,
1008 sizeof(command));
1009 if (ret < 0)
1010 goto out;
1011
1012 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
1013 do {
1014 ret = ucsi->ops->read(ucsi, UCSI_CCI,
1015 &cci, sizeof(cci));
1016 if (ret < 0)
1017 goto out;
1018 if (cci & UCSI_CCI_COMMAND_COMPLETE)
1019 break;
1020 if (time_is_before_jiffies(tmo))
1021 break;
1022 msleep(20);
1023 } while (1);
1024
1025 WARN_ON(cci & UCSI_CCI_RESET_COMPLETE);
1026 }
1027
1028 command = UCSI_PPM_RESET;
1029 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command,
1030 sizeof(command));
1031 if (ret < 0)
1032 goto out;
1033
1034 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
1035
1036 do {
1037 if (time_is_before_jiffies(tmo)) {
1038 ret = -ETIMEDOUT;
1039 goto out;
1040 }
1041
1042 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
1043 if (ret)
1044 goto out;
1045
1046 /* If the PPM is still doing something else, reset it again. */
1047 if (cci & ~UCSI_CCI_RESET_COMPLETE) {
1048 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL,
1049 &command,
1050 sizeof(command));
1051 if (ret < 0)
1052 goto out;
1053 }
1054
1055 msleep(20);
1056 } while (!(cci & UCSI_CCI_RESET_COMPLETE));
1057
1058 out:
1059 mutex_unlock(&ucsi->ppm_lock);
1060 return ret;
1061 }
1062
ucsi_role_cmd(struct ucsi_connector * con,u64 command)1063 static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)
1064 {
1065 int ret;
1066
1067 ret = ucsi_send_command(con->ucsi, command, NULL, 0);
1068 if (ret == -ETIMEDOUT) {
1069 u64 c;
1070
1071 /* PPM most likely stopped responding. Resetting everything. */
1072 ucsi_reset_ppm(con->ucsi);
1073
1074 c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
1075 ucsi_send_command(con->ucsi, c, NULL, 0);
1076
1077 ucsi_reset_connector(con, true);
1078 }
1079
1080 return ret;
1081 }
1082
ucsi_dr_swap(struct typec_port * port,enum typec_data_role role)1083 static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
1084 {
1085 struct ucsi_connector *con = typec_get_drvdata(port);
1086 u8 partner_type;
1087 u64 command;
1088 int ret = 0;
1089
1090 mutex_lock(&con->lock);
1091
1092 if (!con->partner) {
1093 ret = -ENOTCONN;
1094 goto out_unlock;
1095 }
1096
1097 partner_type = UCSI_CONSTAT_PARTNER_TYPE(con->status.flags);
1098 if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
1099 role == TYPEC_DEVICE) ||
1100 (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
1101 role == TYPEC_HOST))
1102 goto out_unlock;
1103
1104 reinit_completion(&con->complete);
1105
1106 command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num);
1107 command |= UCSI_SET_UOR_ROLE(role);
1108 command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS;
1109 ret = ucsi_role_cmd(con, command);
1110 if (ret < 0)
1111 goto out_unlock;
1112
1113 mutex_unlock(&con->lock);
1114
1115 if (!wait_for_completion_timeout(&con->complete,
1116 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1117 return -ETIMEDOUT;
1118
1119 return 0;
1120
1121 out_unlock:
1122 mutex_unlock(&con->lock);
1123
1124 return ret;
1125 }
1126
ucsi_pr_swap(struct typec_port * port,enum typec_role role)1127 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
1128 {
1129 struct ucsi_connector *con = typec_get_drvdata(port);
1130 enum typec_role cur_role;
1131 u64 command;
1132 int ret = 0;
1133
1134 mutex_lock(&con->lock);
1135
1136 if (!con->partner) {
1137 ret = -ENOTCONN;
1138 goto out_unlock;
1139 }
1140
1141 cur_role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
1142
1143 if (cur_role == role)
1144 goto out_unlock;
1145
1146 reinit_completion(&con->complete);
1147
1148 command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num);
1149 command |= UCSI_SET_PDR_ROLE(role);
1150 command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS;
1151 ret = ucsi_role_cmd(con, command);
1152 if (ret < 0)
1153 goto out_unlock;
1154
1155 mutex_unlock(&con->lock);
1156
1157 if (!wait_for_completion_timeout(&con->complete,
1158 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1159 return -ETIMEDOUT;
1160
1161 mutex_lock(&con->lock);
1162
1163 /* Something has gone wrong while swapping the role */
1164 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) !=
1165 UCSI_CONSTAT_PWR_OPMODE_PD) {
1166 ucsi_reset_connector(con, true);
1167 ret = -EPROTO;
1168 }
1169
1170 out_unlock:
1171 mutex_unlock(&con->lock);
1172
1173 return ret;
1174 }
1175
1176 static const struct typec_operations ucsi_ops = {
1177 .dr_set = ucsi_dr_swap,
1178 .pr_set = ucsi_pr_swap
1179 };
1180
1181 /* Caller must call fwnode_handle_put() after use */
ucsi_find_fwnode(struct ucsi_connector * con)1182 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
1183 {
1184 struct fwnode_handle *fwnode;
1185 int i = 1;
1186
1187 device_for_each_child_node(con->ucsi->dev, fwnode)
1188 if (i++ == con->num)
1189 return fwnode;
1190 return NULL;
1191 }
1192
ucsi_register_port(struct ucsi * ucsi,struct ucsi_connector * con)1193 static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con)
1194 {
1195 struct usb_power_delivery_desc desc = { ucsi->cap.pd_version};
1196 struct usb_power_delivery_capabilities_desc pd_caps;
1197 struct usb_power_delivery_capabilities *pd_cap;
1198 struct typec_capability *cap = &con->typec_cap;
1199 enum typec_accessory *accessory = cap->accessory;
1200 enum usb_role u_role = USB_ROLE_NONE;
1201 u64 command;
1202 char *name;
1203 int ret;
1204
1205 name = kasprintf(GFP_KERNEL, "%s-con%d", dev_name(ucsi->dev), con->num);
1206 if (!name)
1207 return -ENOMEM;
1208
1209 con->wq = create_singlethread_workqueue(name);
1210 kfree(name);
1211 if (!con->wq)
1212 return -ENOMEM;
1213
1214 INIT_WORK(&con->work, ucsi_handle_connector_change);
1215 init_completion(&con->complete);
1216 mutex_init(&con->lock);
1217 INIT_LIST_HEAD(&con->partner_tasks);
1218 con->ucsi = ucsi;
1219
1220 cap->fwnode = ucsi_find_fwnode(con);
1221 con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode);
1222 if (IS_ERR(con->usb_role_sw))
1223 return dev_err_probe(ucsi->dev, PTR_ERR(con->usb_role_sw),
1224 "con%d: failed to get usb role switch\n", con->num);
1225
1226 /* Delay other interactions with the con until registration is complete */
1227 mutex_lock(&con->lock);
1228
1229 /* Get connector capability */
1230 command = UCSI_GET_CONNECTOR_CAPABILITY;
1231 command |= UCSI_CONNECTOR_NUMBER(con->num);
1232 ret = ucsi_send_command(ucsi, command, &con->cap, sizeof(con->cap));
1233 if (ret < 0)
1234 goto out_unlock;
1235
1236 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP)
1237 cap->data = TYPEC_PORT_DRD;
1238 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP)
1239 cap->data = TYPEC_PORT_DFP;
1240 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP)
1241 cap->data = TYPEC_PORT_UFP;
1242
1243 if ((con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER) &&
1244 (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER))
1245 cap->type = TYPEC_PORT_DRP;
1246 else if (con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER)
1247 cap->type = TYPEC_PORT_SRC;
1248 else if (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER)
1249 cap->type = TYPEC_PORT_SNK;
1250
1251 cap->revision = ucsi->cap.typec_version;
1252 cap->pd_revision = ucsi->cap.pd_version;
1253 cap->svdm_version = SVDM_VER_2_0;
1254 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
1255
1256 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY)
1257 *accessory++ = TYPEC_ACCESSORY_AUDIO;
1258 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
1259 *accessory = TYPEC_ACCESSORY_DEBUG;
1260
1261 cap->driver_data = con;
1262 cap->ops = &ucsi_ops;
1263
1264 if (ucsi->ops->update_connector)
1265 ucsi->ops->update_connector(con);
1266
1267 ret = ucsi_register_port_psy(con);
1268 if (ret)
1269 goto out;
1270
1271 /* Register the connector */
1272 con->port = typec_register_port(ucsi->dev, cap);
1273 if (IS_ERR(con->port)) {
1274 ret = PTR_ERR(con->port);
1275 goto out;
1276 }
1277
1278 con->pd = usb_power_delivery_register(ucsi->dev, &desc);
1279
1280 ret = ucsi_get_pdos(con, TYPEC_SOURCE, 0, pd_caps.pdo);
1281 if (ret > 0) {
1282 if (ret < PDO_MAX_OBJECTS)
1283 pd_caps.pdo[ret] = 0;
1284
1285 pd_caps.role = TYPEC_SOURCE;
1286 pd_cap = usb_power_delivery_register_capabilities(con->pd, &pd_caps);
1287 if (IS_ERR(pd_cap)) {
1288 ret = PTR_ERR(pd_cap);
1289 goto out;
1290 }
1291
1292 con->port_source_caps = pd_cap;
1293 }
1294
1295 memset(&pd_caps, 0, sizeof(pd_caps));
1296 ret = ucsi_get_pdos(con, TYPEC_SINK, 0, pd_caps.pdo);
1297 if (ret > 0) {
1298 if (ret < PDO_MAX_OBJECTS)
1299 pd_caps.pdo[ret] = 0;
1300
1301 pd_caps.role = TYPEC_SINK;
1302 pd_cap = usb_power_delivery_register_capabilities(con->pd, &pd_caps);
1303 if (IS_ERR(pd_cap)) {
1304 ret = PTR_ERR(pd_cap);
1305 goto out;
1306 }
1307
1308 con->port_sink_caps = pd_cap;
1309 }
1310
1311 typec_port_set_usb_power_delivery(con->port, con->pd);
1312
1313 /* Alternate modes */
1314 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON);
1315 if (ret) {
1316 dev_err(ucsi->dev, "con%d: failed to register alt modes\n",
1317 con->num);
1318 goto out;
1319 }
1320
1321 /* Get the status */
1322 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
1323 ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
1324 if (ret < 0) {
1325 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
1326 ret = 0;
1327 goto out;
1328 }
1329 ret = 0; /* ucsi_send_command() returns length on success */
1330
1331 if (ucsi->ops->connector_status)
1332 ucsi->ops->connector_status(con);
1333
1334 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1335 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1336 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1337 u_role = USB_ROLE_HOST;
1338 fallthrough;
1339 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1340 typec_set_data_role(con->port, TYPEC_HOST);
1341 break;
1342 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1343 u_role = USB_ROLE_DEVICE;
1344 typec_set_data_role(con->port, TYPEC_DEVICE);
1345 break;
1346 default:
1347 break;
1348 }
1349
1350 /* Check if there is already something connected */
1351 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1352 typec_set_pwr_role(con->port,
1353 !!(con->status.flags & UCSI_CONSTAT_PWR_DIR));
1354 ucsi_register_partner(con);
1355 ucsi_pwr_opmode_change(con);
1356 ucsi_port_psy_changed(con);
1357 }
1358
1359 /* Only notify USB controller if partner supports USB data */
1360 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
1361 u_role = USB_ROLE_NONE;
1362
1363 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1364 if (ret) {
1365 dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n",
1366 con->num, u_role);
1367 ret = 0;
1368 }
1369
1370 if (con->partner &&
1371 UCSI_CONSTAT_PWR_OPMODE(con->status.flags) ==
1372 UCSI_CONSTAT_PWR_OPMODE_PD) {
1373 ucsi_get_src_pdos(con);
1374 ucsi_check_altmodes(con);
1375 }
1376
1377 trace_ucsi_register_port(con->num, &con->status);
1378
1379 out:
1380 fwnode_handle_put(cap->fwnode);
1381 out_unlock:
1382 mutex_unlock(&con->lock);
1383
1384 if (ret && con->wq) {
1385 destroy_workqueue(con->wq);
1386 con->wq = NULL;
1387 }
1388
1389 return ret;
1390 }
1391
1392 /**
1393 * ucsi_init - Initialize UCSI interface
1394 * @ucsi: UCSI to be initialized
1395 *
1396 * Registers all ports @ucsi has and enables all notification events.
1397 */
ucsi_init(struct ucsi * ucsi)1398 static int ucsi_init(struct ucsi *ucsi)
1399 {
1400 struct ucsi_connector *con, *connector;
1401 u64 command, ntfy;
1402 u32 cci;
1403 int ret;
1404 int i;
1405
1406 /* Reset the PPM */
1407 ret = ucsi_reset_ppm(ucsi);
1408 if (ret) {
1409 dev_err(ucsi->dev, "failed to reset PPM!\n");
1410 goto err;
1411 }
1412
1413 /* Enable basic notifications */
1414 ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
1415 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1416 ret = ucsi_send_command(ucsi, command, NULL, 0);
1417 if (ret < 0)
1418 goto err_reset;
1419
1420 /* Get PPM capabilities */
1421 command = UCSI_GET_CAPABILITY;
1422 ret = ucsi_send_command(ucsi, command, &ucsi->cap, sizeof(ucsi->cap));
1423 if (ret < 0)
1424 goto err_reset;
1425
1426 if (!ucsi->cap.num_connectors) {
1427 ret = -ENODEV;
1428 goto err_reset;
1429 }
1430
1431 /* Allocate the connectors. Released in ucsi_unregister() */
1432 connector = kcalloc(ucsi->cap.num_connectors + 1, sizeof(*connector), GFP_KERNEL);
1433 if (!connector) {
1434 ret = -ENOMEM;
1435 goto err_reset;
1436 }
1437
1438 /* Register all connectors */
1439 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1440 connector[i].num = i + 1;
1441 ret = ucsi_register_port(ucsi, &connector[i]);
1442 if (ret)
1443 goto err_unregister;
1444 }
1445
1446 /* Enable all notifications */
1447 ntfy = UCSI_ENABLE_NTFY_ALL;
1448 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1449 ret = ucsi_send_command(ucsi, command, NULL, 0);
1450 if (ret < 0)
1451 goto err_unregister;
1452
1453 ucsi->connector = connector;
1454 ucsi->ntfy = ntfy;
1455
1456 mutex_lock(&ucsi->ppm_lock);
1457 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
1458 mutex_unlock(&ucsi->ppm_lock);
1459 if (ret)
1460 return ret;
1461 if (UCSI_CCI_CONNECTOR(cci))
1462 ucsi_connector_change(ucsi, UCSI_CCI_CONNECTOR(cci));
1463
1464 return 0;
1465
1466 err_unregister:
1467 for (con = connector; con->port; con++) {
1468 ucsi_unregister_partner(con);
1469 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON);
1470 ucsi_unregister_port_psy(con);
1471 if (con->wq)
1472 destroy_workqueue(con->wq);
1473
1474 usb_power_delivery_unregister_capabilities(con->port_sink_caps);
1475 con->port_sink_caps = NULL;
1476 usb_power_delivery_unregister_capabilities(con->port_source_caps);
1477 con->port_source_caps = NULL;
1478 usb_power_delivery_unregister(con->pd);
1479 con->pd = NULL;
1480 typec_unregister_port(con->port);
1481 con->port = NULL;
1482 }
1483 kfree(connector);
1484 err_reset:
1485 memset(&ucsi->cap, 0, sizeof(ucsi->cap));
1486 ucsi_reset_ppm(ucsi);
1487 err:
1488 return ret;
1489 }
1490
ucsi_resume_work(struct work_struct * work)1491 static void ucsi_resume_work(struct work_struct *work)
1492 {
1493 struct ucsi *ucsi = container_of(work, struct ucsi, resume_work);
1494 struct ucsi_connector *con;
1495 u64 command;
1496 int ret;
1497
1498 /* Restore UCSI notification enable mask after system resume */
1499 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1500 ret = ucsi_send_command(ucsi, command, NULL, 0);
1501 if (ret < 0) {
1502 dev_err(ucsi->dev, "failed to re-enable notifications (%d)\n", ret);
1503 return;
1504 }
1505
1506 for (con = ucsi->connector; con->port; con++) {
1507 mutex_lock(&con->lock);
1508 ucsi_partner_task(con, ucsi_check_connection, 1, 0);
1509 mutex_unlock(&con->lock);
1510 }
1511 }
1512
ucsi_resume(struct ucsi * ucsi)1513 int ucsi_resume(struct ucsi *ucsi)
1514 {
1515 if (ucsi->connector)
1516 queue_work(system_long_wq, &ucsi->resume_work);
1517 return 0;
1518 }
1519 EXPORT_SYMBOL_GPL(ucsi_resume);
1520
ucsi_init_work(struct work_struct * work)1521 static void ucsi_init_work(struct work_struct *work)
1522 {
1523 struct ucsi *ucsi = container_of(work, struct ucsi, work.work);
1524 int ret;
1525
1526 ret = ucsi_init(ucsi);
1527 if (ret)
1528 dev_err_probe(ucsi->dev, ret, "PPM init failed\n");
1529
1530 if (ret == -EPROBE_DEFER) {
1531 if (ucsi->work_count++ > UCSI_ROLE_SWITCH_WAIT_COUNT) {
1532 dev_err(ucsi->dev, "PPM init failed, stop trying\n");
1533 return;
1534 }
1535
1536 queue_delayed_work(system_long_wq, &ucsi->work,
1537 UCSI_ROLE_SWITCH_INTERVAL);
1538 }
1539 }
1540
1541 /**
1542 * ucsi_get_drvdata - Return private driver data pointer
1543 * @ucsi: UCSI interface
1544 */
ucsi_get_drvdata(struct ucsi * ucsi)1545 void *ucsi_get_drvdata(struct ucsi *ucsi)
1546 {
1547 return ucsi->driver_data;
1548 }
1549 EXPORT_SYMBOL_GPL(ucsi_get_drvdata);
1550
1551 /**
1552 * ucsi_set_drvdata - Assign private driver data pointer
1553 * @ucsi: UCSI interface
1554 * @data: Private data pointer
1555 */
ucsi_set_drvdata(struct ucsi * ucsi,void * data)1556 void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
1557 {
1558 ucsi->driver_data = data;
1559 }
1560 EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
1561
1562 /**
1563 * ucsi_create - Allocate UCSI instance
1564 * @dev: Device interface to the PPM (Platform Policy Manager)
1565 * @ops: I/O routines
1566 */
ucsi_create(struct device * dev,const struct ucsi_operations * ops)1567 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
1568 {
1569 struct ucsi *ucsi;
1570
1571 if (!ops || !ops->read || !ops->sync_write || !ops->async_write)
1572 return ERR_PTR(-EINVAL);
1573
1574 ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
1575 if (!ucsi)
1576 return ERR_PTR(-ENOMEM);
1577
1578 INIT_WORK(&ucsi->resume_work, ucsi_resume_work);
1579 INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work);
1580 mutex_init(&ucsi->ppm_lock);
1581 ucsi->dev = dev;
1582 ucsi->ops = ops;
1583
1584 return ucsi;
1585 }
1586 EXPORT_SYMBOL_GPL(ucsi_create);
1587
1588 /**
1589 * ucsi_destroy - Free UCSI instance
1590 * @ucsi: UCSI instance to be freed
1591 */
ucsi_destroy(struct ucsi * ucsi)1592 void ucsi_destroy(struct ucsi *ucsi)
1593 {
1594 ucsi_debugfs_unregister(ucsi);
1595 kfree(ucsi);
1596 }
1597 EXPORT_SYMBOL_GPL(ucsi_destroy);
1598
1599 /**
1600 * ucsi_register - Register UCSI interface
1601 * @ucsi: UCSI instance
1602 */
ucsi_register(struct ucsi * ucsi)1603 int ucsi_register(struct ucsi *ucsi)
1604 {
1605 int ret;
1606
1607 ret = ucsi->ops->read(ucsi, UCSI_VERSION, &ucsi->version,
1608 sizeof(ucsi->version));
1609 if (ret)
1610 return ret;
1611
1612 if (!ucsi->version)
1613 return -ENODEV;
1614
1615 /*
1616 * Version format is JJ.M.N (JJ = Major version, M = Minor version,
1617 * N = sub-minor version).
1618 */
1619 dev_dbg(ucsi->dev, "Registered UCSI interface with version %x.%x.%x",
1620 UCSI_BCD_GET_MAJOR(ucsi->version),
1621 UCSI_BCD_GET_MINOR(ucsi->version),
1622 UCSI_BCD_GET_SUBMINOR(ucsi->version));
1623
1624 queue_delayed_work(system_long_wq, &ucsi->work, 0);
1625
1626 ucsi_debugfs_register(ucsi);
1627 return 0;
1628 }
1629 EXPORT_SYMBOL_GPL(ucsi_register);
1630
1631 /**
1632 * ucsi_unregister - Unregister UCSI interface
1633 * @ucsi: UCSI interface to be unregistered
1634 *
1635 * Unregister UCSI interface that was created with ucsi_register().
1636 */
ucsi_unregister(struct ucsi * ucsi)1637 void ucsi_unregister(struct ucsi *ucsi)
1638 {
1639 u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
1640 int i;
1641
1642 /* Make sure that we are not in the middle of driver initialization */
1643 cancel_delayed_work_sync(&ucsi->work);
1644 cancel_work_sync(&ucsi->resume_work);
1645
1646 /* Disable notifications */
1647 ucsi->ops->async_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
1648
1649 if (!ucsi->connector)
1650 return;
1651
1652 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1653 cancel_work_sync(&ucsi->connector[i].work);
1654 ucsi_unregister_partner(&ucsi->connector[i]);
1655 ucsi_unregister_altmodes(&ucsi->connector[i],
1656 UCSI_RECIPIENT_CON);
1657 ucsi_unregister_port_psy(&ucsi->connector[i]);
1658
1659 if (ucsi->connector[i].wq) {
1660 struct ucsi_work *uwork;
1661
1662 mutex_lock(&ucsi->connector[i].lock);
1663 /*
1664 * queue delayed items immediately so they can execute
1665 * and free themselves before the wq is destroyed
1666 */
1667 list_for_each_entry(uwork, &ucsi->connector[i].partner_tasks, node)
1668 mod_delayed_work(ucsi->connector[i].wq, &uwork->work, 0);
1669 mutex_unlock(&ucsi->connector[i].lock);
1670 destroy_workqueue(ucsi->connector[i].wq);
1671 }
1672
1673 usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_sink_caps);
1674 ucsi->connector[i].port_sink_caps = NULL;
1675 usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_source_caps);
1676 ucsi->connector[i].port_source_caps = NULL;
1677 usb_power_delivery_unregister(ucsi->connector[i].pd);
1678 ucsi->connector[i].pd = NULL;
1679 typec_unregister_port(ucsi->connector[i].port);
1680 }
1681
1682 kfree(ucsi->connector);
1683 }
1684 EXPORT_SYMBOL_GPL(ucsi_unregister);
1685
ucsi_module_init(void)1686 static int __init ucsi_module_init(void)
1687 {
1688 ucsi_debugfs_init();
1689 return 0;
1690 }
1691 module_init(ucsi_module_init);
1692
ucsi_module_exit(void)1693 static void __exit ucsi_module_exit(void)
1694 {
1695 ucsi_debugfs_exit();
1696 }
1697 module_exit(ucsi_module_exit);
1698
1699 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1700 MODULE_LICENSE("GPL v2");
1701 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");
1702