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