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_acknowledge_command(struct ucsi * ucsi)39 static int ucsi_acknowledge_command(struct ucsi *ucsi)
40 {
41 u64 ctrl;
42
43 ctrl = UCSI_ACK_CC_CI;
44 ctrl |= UCSI_ACK_COMMAND_COMPLETE;
45
46 return ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &ctrl, sizeof(ctrl));
47 }
48
ucsi_acknowledge_connector_change(struct ucsi * ucsi)49 static int ucsi_acknowledge_connector_change(struct ucsi *ucsi)
50 {
51 u64 ctrl;
52
53 ctrl = UCSI_ACK_CC_CI;
54 ctrl |= UCSI_ACK_CONNECTOR_CHANGE;
55
56 return ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &ctrl, sizeof(ctrl));
57 }
58
59 static int ucsi_exec_command(struct ucsi *ucsi, u64 command);
60
ucsi_read_error(struct ucsi * ucsi)61 static int ucsi_read_error(struct ucsi *ucsi)
62 {
63 u16 error;
64 int ret;
65
66 /* Acknowledge the command that failed */
67 ret = ucsi_acknowledge_command(ucsi);
68 if (ret)
69 return ret;
70
71 ret = ucsi_exec_command(ucsi, UCSI_GET_ERROR_STATUS);
72 if (ret < 0)
73 return ret;
74
75 ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, &error, sizeof(error));
76 if (ret)
77 return ret;
78
79 ret = ucsi_acknowledge_command(ucsi);
80 if (ret)
81 return ret;
82
83 switch (error) {
84 case UCSI_ERROR_INCOMPATIBLE_PARTNER:
85 return -EOPNOTSUPP;
86 case UCSI_ERROR_CC_COMMUNICATION_ERR:
87 return -ECOMM;
88 case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
89 return -EPROTO;
90 case UCSI_ERROR_DEAD_BATTERY:
91 dev_warn(ucsi->dev, "Dead battery condition!\n");
92 return -EPERM;
93 case UCSI_ERROR_INVALID_CON_NUM:
94 case UCSI_ERROR_UNREGONIZED_CMD:
95 case UCSI_ERROR_INVALID_CMD_ARGUMENT:
96 dev_err(ucsi->dev, "possible UCSI driver bug %u\n", error);
97 return -EINVAL;
98 case UCSI_ERROR_OVERCURRENT:
99 dev_warn(ucsi->dev, "Overcurrent condition\n");
100 break;
101 case UCSI_ERROR_PARTNER_REJECTED_SWAP:
102 dev_warn(ucsi->dev, "Partner rejected swap\n");
103 break;
104 case UCSI_ERROR_HARD_RESET:
105 dev_warn(ucsi->dev, "Hard reset occurred\n");
106 break;
107 case UCSI_ERROR_PPM_POLICY_CONFLICT:
108 dev_warn(ucsi->dev, "PPM Policy conflict\n");
109 break;
110 case UCSI_ERROR_SWAP_REJECTED:
111 dev_warn(ucsi->dev, "Swap rejected\n");
112 break;
113 case UCSI_ERROR_UNDEFINED:
114 default:
115 dev_err(ucsi->dev, "unknown error %u\n", error);
116 break;
117 }
118
119 return -EIO;
120 }
121
ucsi_exec_command(struct ucsi * ucsi,u64 cmd)122 static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd)
123 {
124 u32 cci;
125 int ret;
126
127 ret = ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
128 if (ret)
129 return ret;
130
131 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
132 if (ret)
133 return ret;
134
135 if (cci & UCSI_CCI_BUSY)
136 return -EBUSY;
137
138 if (!(cci & UCSI_CCI_COMMAND_COMPLETE))
139 return -EIO;
140
141 if (cci & UCSI_CCI_NOT_SUPPORTED)
142 return -EOPNOTSUPP;
143
144 if (cci & UCSI_CCI_ERROR) {
145 if (cmd == UCSI_GET_ERROR_STATUS)
146 return -EIO;
147 return ucsi_read_error(ucsi);
148 }
149
150 return UCSI_CCI_LENGTH(cci);
151 }
152
ucsi_send_command(struct ucsi * ucsi,u64 command,void * data,size_t size)153 int ucsi_send_command(struct ucsi *ucsi, u64 command,
154 void *data, size_t size)
155 {
156 u8 length;
157 int ret;
158
159 mutex_lock(&ucsi->ppm_lock);
160
161 ret = ucsi_exec_command(ucsi, command);
162 if (ret < 0)
163 goto out;
164
165 length = ret;
166
167 if (data) {
168 ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, data, size);
169 if (ret)
170 goto out;
171 }
172
173 ret = ucsi_acknowledge_command(ucsi);
174 if (ret)
175 goto out;
176
177 ret = length;
178 out:
179 mutex_unlock(&ucsi->ppm_lock);
180 return ret;
181 }
182 EXPORT_SYMBOL_GPL(ucsi_send_command);
183
ucsi_resume(struct ucsi * ucsi)184 int ucsi_resume(struct ucsi *ucsi)
185 {
186 u64 command;
187
188 /* Restore UCSI notification enable mask after system resume */
189 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
190
191 return ucsi_send_command(ucsi, command, NULL, 0);
192 }
193 EXPORT_SYMBOL_GPL(ucsi_resume);
194 /* -------------------------------------------------------------------------- */
195
ucsi_altmode_update_active(struct ucsi_connector * con)196 void ucsi_altmode_update_active(struct ucsi_connector *con)
197 {
198 const struct typec_altmode *altmode = NULL;
199 u64 command;
200 int ret;
201 u8 cur;
202 int i;
203
204 command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
205 ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur));
206 if (ret < 0) {
207 if (con->ucsi->version > 0x0100) {
208 dev_err(con->ucsi->dev,
209 "GET_CURRENT_CAM command failed\n");
210 return;
211 }
212 cur = 0xff;
213 }
214
215 if (cur < UCSI_MAX_ALTMODES)
216 altmode = typec_altmode_get_partner(con->port_altmode[cur]);
217
218 for (i = 0; con->partner_altmode[i]; i++)
219 typec_altmode_update_active(con->partner_altmode[i],
220 con->partner_altmode[i] == altmode);
221 }
222
ucsi_altmode_next_mode(struct typec_altmode ** alt,u16 svid)223 static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
224 {
225 u8 mode = 1;
226 int i;
227
228 for (i = 0; alt[i]; i++) {
229 if (i > MODE_DISCOVERY_MAX)
230 return -ERANGE;
231
232 if (alt[i]->svid == svid)
233 mode++;
234 }
235
236 return mode;
237 }
238
ucsi_next_altmode(struct typec_altmode ** alt)239 static int ucsi_next_altmode(struct typec_altmode **alt)
240 {
241 int i = 0;
242
243 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
244 if (!alt[i])
245 return i;
246
247 return -ENOENT;
248 }
249
ucsi_register_altmode(struct ucsi_connector * con,struct typec_altmode_desc * desc,u8 recipient)250 static int ucsi_register_altmode(struct ucsi_connector *con,
251 struct typec_altmode_desc *desc,
252 u8 recipient)
253 {
254 struct typec_altmode *alt;
255 bool override;
256 int ret;
257 int i;
258
259 override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
260
261 switch (recipient) {
262 case UCSI_RECIPIENT_CON:
263 i = ucsi_next_altmode(con->port_altmode);
264 if (i < 0) {
265 ret = i;
266 goto err;
267 }
268
269 ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid);
270 if (ret < 0)
271 return ret;
272
273 desc->mode = ret;
274
275 switch (desc->svid) {
276 case USB_TYPEC_DP_SID:
277 alt = ucsi_register_displayport(con, override, i, desc);
278 break;
279 case USB_TYPEC_NVIDIA_VLINK_SID:
280 if (desc->vdo == USB_TYPEC_NVIDIA_VLINK_DBG_VDO)
281 alt = typec_port_register_altmode(con->port,
282 desc);
283 else
284 alt = ucsi_register_displayport(con, override,
285 i, desc);
286 break;
287 default:
288 alt = typec_port_register_altmode(con->port, desc);
289 break;
290 }
291
292 if (IS_ERR(alt)) {
293 ret = PTR_ERR(alt);
294 goto err;
295 }
296
297 con->port_altmode[i] = alt;
298 break;
299 case UCSI_RECIPIENT_SOP:
300 i = ucsi_next_altmode(con->partner_altmode);
301 if (i < 0) {
302 ret = i;
303 goto err;
304 }
305
306 ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid);
307 if (ret < 0)
308 return ret;
309
310 desc->mode = ret;
311
312 alt = typec_partner_register_altmode(con->partner, desc);
313 if (IS_ERR(alt)) {
314 ret = PTR_ERR(alt);
315 goto err;
316 }
317
318 con->partner_altmode[i] = alt;
319 break;
320 default:
321 return -EINVAL;
322 }
323
324 trace_ucsi_register_altmode(recipient, alt);
325
326 return 0;
327
328 err:
329 dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n",
330 desc->svid, desc->mode);
331
332 return ret;
333 }
334
335 static int
ucsi_register_altmodes_nvidia(struct ucsi_connector * con,u8 recipient)336 ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient)
337 {
338 int max_altmodes = UCSI_MAX_ALTMODES;
339 struct typec_altmode_desc desc;
340 struct ucsi_altmode alt;
341 struct ucsi_altmode orig[UCSI_MAX_ALTMODES];
342 struct ucsi_altmode updated[UCSI_MAX_ALTMODES];
343 struct ucsi *ucsi = con->ucsi;
344 bool multi_dp = false;
345 u64 command;
346 int ret;
347 int len;
348 int i;
349 int k = 0;
350
351 if (recipient == UCSI_RECIPIENT_CON)
352 max_altmodes = con->ucsi->cap.num_alt_modes;
353
354 memset(orig, 0, sizeof(orig));
355 memset(updated, 0, sizeof(updated));
356
357 /* First get all the alternate modes */
358 for (i = 0; i < max_altmodes; i++) {
359 memset(&alt, 0, sizeof(alt));
360 command = UCSI_GET_ALTERNATE_MODES;
361 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
362 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
363 command |= UCSI_GET_ALTMODE_OFFSET(i);
364 len = ucsi_send_command(con->ucsi, command, &alt, sizeof(alt));
365 /*
366 * We are collecting all altmodes first and then registering.
367 * Some type-C device will return zero length data beyond last
368 * alternate modes. We should not return if length is zero.
369 */
370 if (len < 0)
371 return len;
372
373 /* We got all altmodes, now break out and register them */
374 if (!len || !alt.svid)
375 break;
376
377 orig[k].mid = alt.mid;
378 orig[k].svid = alt.svid;
379 k++;
380 }
381 /*
382 * Update the original altmode table as some ppms may report
383 * multiple DP altmodes.
384 */
385 if (recipient == UCSI_RECIPIENT_CON)
386 multi_dp = ucsi->ops->update_altmodes(ucsi, orig, updated);
387
388 /* now register altmodes */
389 for (i = 0; i < max_altmodes; i++) {
390 memset(&desc, 0, sizeof(desc));
391 if (multi_dp && recipient == UCSI_RECIPIENT_CON) {
392 desc.svid = updated[i].svid;
393 desc.vdo = updated[i].mid;
394 } else {
395 desc.svid = orig[i].svid;
396 desc.vdo = orig[i].mid;
397 }
398 desc.roles = TYPEC_PORT_DRD;
399
400 if (!desc.svid)
401 return 0;
402
403 ret = ucsi_register_altmode(con, &desc, recipient);
404 if (ret)
405 return ret;
406 }
407
408 return 0;
409 }
410
ucsi_register_altmodes(struct ucsi_connector * con,u8 recipient)411 static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
412 {
413 int max_altmodes = UCSI_MAX_ALTMODES;
414 struct typec_altmode_desc desc;
415 struct ucsi_altmode alt[2];
416 u64 command;
417 int num;
418 int ret;
419 int len;
420 int j;
421 int i;
422
423 if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
424 return 0;
425
426 if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
427 return 0;
428
429 if (con->ucsi->ops->update_altmodes)
430 return ucsi_register_altmodes_nvidia(con, recipient);
431
432 if (recipient == UCSI_RECIPIENT_CON)
433 max_altmodes = con->ucsi->cap.num_alt_modes;
434
435 for (i = 0; i < max_altmodes;) {
436 memset(alt, 0, sizeof(alt));
437 command = UCSI_GET_ALTERNATE_MODES;
438 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
439 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
440 command |= UCSI_GET_ALTMODE_OFFSET(i);
441 len = ucsi_send_command(con->ucsi, command, alt, sizeof(alt));
442 if (len <= 0)
443 return len;
444
445 /*
446 * This code is requesting one alt mode at a time, but some PPMs
447 * may still return two. If that happens both alt modes need be
448 * registered and the offset for the next alt mode has to be
449 * incremented.
450 */
451 num = len / sizeof(alt[0]);
452 i += num;
453
454 for (j = 0; j < num; j++) {
455 if (!alt[j].svid)
456 return 0;
457
458 memset(&desc, 0, sizeof(desc));
459 desc.vdo = alt[j].mid;
460 desc.svid = alt[j].svid;
461 desc.roles = TYPEC_PORT_DRD;
462
463 ret = ucsi_register_altmode(con, &desc, recipient);
464 if (ret)
465 return ret;
466 }
467 }
468
469 return 0;
470 }
471
ucsi_unregister_altmodes(struct ucsi_connector * con,u8 recipient)472 static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
473 {
474 const struct typec_altmode *pdev;
475 struct typec_altmode **adev;
476 int i = 0;
477
478 switch (recipient) {
479 case UCSI_RECIPIENT_CON:
480 adev = con->port_altmode;
481 break;
482 case UCSI_RECIPIENT_SOP:
483 adev = con->partner_altmode;
484 break;
485 default:
486 return;
487 }
488
489 while (adev[i]) {
490 if (recipient == UCSI_RECIPIENT_SOP &&
491 (adev[i]->svid == USB_TYPEC_DP_SID ||
492 (adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID &&
493 adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) {
494 pdev = typec_altmode_get_partner(adev[i]);
495 ucsi_displayport_remove_partner((void *)pdev);
496 }
497 typec_unregister_altmode(adev[i]);
498 adev[i++] = NULL;
499 }
500 }
501
ucsi_get_pdos(struct ucsi_connector * con,int is_partner,u32 * pdos,int offset,int num_pdos)502 static int ucsi_get_pdos(struct ucsi_connector *con, int is_partner,
503 u32 *pdos, int offset, int num_pdos)
504 {
505 struct ucsi *ucsi = con->ucsi;
506 u64 command;
507 int ret;
508
509 command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num);
510 command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner);
511 command |= UCSI_GET_PDOS_PDO_OFFSET(offset);
512 command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1);
513 command |= UCSI_GET_PDOS_SRC_PDOS;
514 ret = ucsi_send_command(ucsi, command, pdos + offset,
515 num_pdos * sizeof(u32));
516 if (ret < 0)
517 dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret);
518
519 return ret;
520 }
521
ucsi_get_src_pdos(struct ucsi_connector * con,int is_partner)522 static void ucsi_get_src_pdos(struct ucsi_connector *con, int is_partner)
523 {
524 int ret;
525
526 /* UCSI max payload means only getting at most 4 PDOs at a time */
527 ret = ucsi_get_pdos(con, 1, con->src_pdos, 0, UCSI_MAX_PDOS);
528 if (ret < 0)
529 return;
530
531 con->num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */
532 if (con->num_pdos < UCSI_MAX_PDOS)
533 return;
534
535 /* get the remaining PDOs, if any */
536 ret = ucsi_get_pdos(con, 1, con->src_pdos, UCSI_MAX_PDOS,
537 PDO_MAX_OBJECTS - UCSI_MAX_PDOS);
538 if (ret < 0)
539 return;
540
541 con->num_pdos += ret / sizeof(u32);
542 }
543
ucsi_pwr_opmode_change(struct ucsi_connector * con)544 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
545 {
546 switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
547 case UCSI_CONSTAT_PWR_OPMODE_PD:
548 con->rdo = con->status.request_data_obj;
549 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
550 ucsi_get_src_pdos(con, 1);
551 break;
552 case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
553 con->rdo = 0;
554 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
555 break;
556 case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
557 con->rdo = 0;
558 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
559 break;
560 default:
561 con->rdo = 0;
562 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
563 break;
564 }
565 }
566
ucsi_register_partner(struct ucsi_connector * con)567 static int ucsi_register_partner(struct ucsi_connector *con)
568 {
569 u8 pwr_opmode = UCSI_CONSTAT_PWR_OPMODE(con->status.flags);
570 struct typec_partner_desc desc;
571 struct typec_partner *partner;
572
573 if (con->partner)
574 return 0;
575
576 memset(&desc, 0, sizeof(desc));
577
578 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
579 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
580 desc.accessory = TYPEC_ACCESSORY_DEBUG;
581 break;
582 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
583 desc.accessory = TYPEC_ACCESSORY_AUDIO;
584 break;
585 default:
586 break;
587 }
588
589 desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD;
590
591 partner = typec_register_partner(con->port, &desc);
592 if (IS_ERR(partner)) {
593 dev_err(con->ucsi->dev,
594 "con%d: failed to register partner (%ld)\n", con->num,
595 PTR_ERR(partner));
596 return PTR_ERR(partner);
597 }
598
599 con->partner = partner;
600
601 return 0;
602 }
603
ucsi_unregister_partner(struct ucsi_connector * con)604 static void ucsi_unregister_partner(struct ucsi_connector *con)
605 {
606 if (!con->partner)
607 return;
608
609 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
610 typec_unregister_partner(con->partner);
611 con->partner = NULL;
612 }
613
ucsi_partner_change(struct ucsi_connector * con)614 static void ucsi_partner_change(struct ucsi_connector *con)
615 {
616 enum usb_role u_role = USB_ROLE_NONE;
617 int ret;
618
619 if (!con->partner)
620 return;
621
622 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
623 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
624 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
625 u_role = USB_ROLE_HOST;
626 fallthrough;
627 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
628 typec_set_data_role(con->port, TYPEC_HOST);
629 break;
630 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
631 u_role = USB_ROLE_DEVICE;
632 typec_set_data_role(con->port, TYPEC_DEVICE);
633 break;
634 default:
635 break;
636 }
637
638 /* Complete pending data role swap */
639 if (!completion_done(&con->complete))
640 complete(&con->complete);
641
642 /* Only notify USB controller if partner supports USB data */
643 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
644 u_role = USB_ROLE_NONE;
645
646 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
647 if (ret)
648 dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n",
649 con->num, u_role);
650
651 /* Can't rely on Partner Flags field. Always checking the alt modes. */
652 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
653 if (ret)
654 dev_err(con->ucsi->dev,
655 "con%d: failed to register partner alternate modes\n",
656 con->num);
657 else
658 ucsi_altmode_update_active(con);
659 }
660
ucsi_handle_connector_change(struct work_struct * work)661 static void ucsi_handle_connector_change(struct work_struct *work)
662 {
663 struct ucsi_connector *con = container_of(work, struct ucsi_connector,
664 work);
665 struct ucsi *ucsi = con->ucsi;
666 struct ucsi_connector_status pre_ack_status;
667 struct ucsi_connector_status post_ack_status;
668 enum typec_role role;
669 enum usb_role u_role = USB_ROLE_NONE;
670 u16 inferred_changes;
671 u16 changed_flags;
672 u64 command;
673 int ret;
674
675 mutex_lock(&con->lock);
676
677 /*
678 * Some/many PPMs have an issue where all fields in the change bitfield
679 * are cleared when an ACK is send. This will causes any change
680 * between GET_CONNECTOR_STATUS and ACK to be lost.
681 *
682 * We work around this by re-fetching the connector status afterwards.
683 * We then infer any changes that we see have happened but that may not
684 * be represented in the change bitfield.
685 *
686 * Also, even though we don't need to know the currently supported alt
687 * modes, we run the GET_CAM_SUPPORTED command to ensure the PPM does
688 * not get stuck in case it assumes we do.
689 * Always do this, rather than relying on UCSI_CONSTAT_CAM_CHANGE to be
690 * set in the change bitfield.
691 *
692 * We end up with the following actions:
693 * 1. UCSI_GET_CONNECTOR_STATUS, store result, update unprocessed_changes
694 * 2. UCSI_GET_CAM_SUPPORTED, discard result
695 * 3. ACK connector change
696 * 4. UCSI_GET_CONNECTOR_STATUS, store result
697 * 5. Infere lost changes by comparing UCSI_GET_CONNECTOR_STATUS results
698 * 6. If PPM reported a new change, then restart in order to ACK
699 * 7. Process everything as usual.
700 *
701 * We may end up seeing a change twice, but we can only miss extremely
702 * short transitional changes.
703 */
704
705 /* 1. First UCSI_GET_CONNECTOR_STATUS */
706 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
707 ret = ucsi_send_command(ucsi, command, &pre_ack_status,
708 sizeof(pre_ack_status));
709 if (ret < 0) {
710 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
711 __func__, ret);
712 goto out_unlock;
713 }
714 con->unprocessed_changes |= pre_ack_status.change;
715
716 /* 2. Run UCSI_GET_CAM_SUPPORTED and discard the result. */
717 command = UCSI_GET_CAM_SUPPORTED;
718 command |= UCSI_CONNECTOR_NUMBER(con->num);
719 ucsi_send_command(con->ucsi, command, NULL, 0);
720
721 /* 3. ACK connector change */
722 ret = ucsi_acknowledge_connector_change(ucsi);
723 clear_bit(EVENT_PENDING, &ucsi->flags);
724 if (ret) {
725 dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret);
726 goto out_unlock;
727 }
728
729 /* 4. Second UCSI_GET_CONNECTOR_STATUS */
730 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
731 ret = ucsi_send_command(ucsi, command, &post_ack_status,
732 sizeof(post_ack_status));
733 if (ret < 0) {
734 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
735 __func__, ret);
736 clear_bit(EVENT_PENDING, &con->ucsi->flags);
737 goto out_unlock;
738 }
739
740 /* 5. Inferre any missing changes */
741 changed_flags = pre_ack_status.flags ^ post_ack_status.flags;
742 inferred_changes = 0;
743 if (UCSI_CONSTAT_PWR_OPMODE(changed_flags) != 0)
744 inferred_changes |= UCSI_CONSTAT_POWER_OPMODE_CHANGE;
745
746 if (changed_flags & UCSI_CONSTAT_CONNECTED)
747 inferred_changes |= UCSI_CONSTAT_CONNECT_CHANGE;
748
749 if (changed_flags & UCSI_CONSTAT_PWR_DIR)
750 inferred_changes |= UCSI_CONSTAT_POWER_DIR_CHANGE;
751
752 if (UCSI_CONSTAT_PARTNER_FLAGS(changed_flags) != 0)
753 inferred_changes |= UCSI_CONSTAT_PARTNER_CHANGE;
754
755 if (UCSI_CONSTAT_PARTNER_TYPE(changed_flags) != 0)
756 inferred_changes |= UCSI_CONSTAT_PARTNER_CHANGE;
757
758 /* Mask out anything that was correctly notified in the later call. */
759 inferred_changes &= ~post_ack_status.change;
760 if (inferred_changes)
761 dev_dbg(ucsi->dev, "%s: Inferred changes that would have been lost: 0x%04x\n",
762 __func__, inferred_changes);
763
764 con->unprocessed_changes |= inferred_changes;
765
766 /* 6. If PPM reported a new change, then restart in order to ACK */
767 if (post_ack_status.change)
768 goto out_unlock;
769
770 /* 7. Continue as if nothing happened */
771 con->status = post_ack_status;
772 con->status.change = con->unprocessed_changes;
773 con->unprocessed_changes = 0;
774
775 role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
776
777 if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE ||
778 con->status.change & UCSI_CONSTAT_POWER_LEVEL_CHANGE) {
779 ucsi_pwr_opmode_change(con);
780 ucsi_port_psy_changed(con);
781 }
782
783 if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
784 typec_set_pwr_role(con->port, role);
785
786 /* Complete pending power role swap */
787 if (!completion_done(&con->complete))
788 complete(&con->complete);
789 }
790
791 if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
792 typec_set_pwr_role(con->port, role);
793
794 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
795 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
796 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
797 u_role = USB_ROLE_HOST;
798 fallthrough;
799 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
800 typec_set_data_role(con->port, TYPEC_HOST);
801 break;
802 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
803 u_role = USB_ROLE_DEVICE;
804 typec_set_data_role(con->port, TYPEC_DEVICE);
805 break;
806 default:
807 break;
808 }
809
810 if (con->status.flags & UCSI_CONSTAT_CONNECTED)
811 ucsi_register_partner(con);
812 else
813 ucsi_unregister_partner(con);
814
815 ucsi_port_psy_changed(con);
816
817 /* Only notify USB controller if partner supports USB data */
818 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) &
819 UCSI_CONSTAT_PARTNER_FLAG_USB))
820 u_role = USB_ROLE_NONE;
821
822 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
823 if (ret)
824 dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n",
825 con->num, u_role);
826 }
827
828 if (con->status.change & UCSI_CONSTAT_PARTNER_CHANGE)
829 ucsi_partner_change(con);
830
831 trace_ucsi_connector_change(con->num, &con->status);
832
833 out_unlock:
834 if (test_and_clear_bit(EVENT_PENDING, &ucsi->flags)) {
835 schedule_work(&con->work);
836 mutex_unlock(&con->lock);
837 return;
838 }
839
840 clear_bit(EVENT_PROCESSING, &ucsi->flags);
841 mutex_unlock(&con->lock);
842 }
843
844 /**
845 * ucsi_connector_change - Process Connector Change Event
846 * @ucsi: UCSI Interface
847 * @num: Connector number
848 */
ucsi_connector_change(struct ucsi * ucsi,u8 num)849 void ucsi_connector_change(struct ucsi *ucsi, u8 num)
850 {
851 struct ucsi_connector *con = &ucsi->connector[num - 1];
852
853 if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
854 dev_dbg(ucsi->dev, "Bogus connector change event\n");
855 return;
856 }
857
858 set_bit(EVENT_PENDING, &ucsi->flags);
859
860 if (!test_and_set_bit(EVENT_PROCESSING, &ucsi->flags))
861 schedule_work(&con->work);
862 }
863 EXPORT_SYMBOL_GPL(ucsi_connector_change);
864
865 /* -------------------------------------------------------------------------- */
866
ucsi_reset_connector(struct ucsi_connector * con,bool hard)867 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
868 {
869 u64 command;
870
871 command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num);
872 command |= hard ? UCSI_CONNECTOR_RESET_HARD : 0;
873
874 return ucsi_send_command(con->ucsi, command, NULL, 0);
875 }
876
ucsi_reset_ppm(struct ucsi * ucsi)877 static int ucsi_reset_ppm(struct ucsi *ucsi)
878 {
879 u64 command = UCSI_PPM_RESET;
880 unsigned long tmo;
881 u32 cci;
882 int ret;
883
884 mutex_lock(&ucsi->ppm_lock);
885
886 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command,
887 sizeof(command));
888 if (ret < 0)
889 goto out;
890
891 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
892
893 do {
894 if (time_is_before_jiffies(tmo)) {
895 ret = -ETIMEDOUT;
896 goto out;
897 }
898
899 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
900 if (ret)
901 goto out;
902
903 /* If the PPM is still doing something else, reset it again. */
904 if (cci & ~UCSI_CCI_RESET_COMPLETE) {
905 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL,
906 &command,
907 sizeof(command));
908 if (ret < 0)
909 goto out;
910 }
911
912 msleep(20);
913 } while (!(cci & UCSI_CCI_RESET_COMPLETE));
914
915 out:
916 mutex_unlock(&ucsi->ppm_lock);
917 return ret;
918 }
919
ucsi_role_cmd(struct ucsi_connector * con,u64 command)920 static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)
921 {
922 int ret;
923
924 ret = ucsi_send_command(con->ucsi, command, NULL, 0);
925 if (ret == -ETIMEDOUT) {
926 u64 c;
927
928 /* PPM most likely stopped responding. Resetting everything. */
929 ucsi_reset_ppm(con->ucsi);
930
931 c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
932 ucsi_send_command(con->ucsi, c, NULL, 0);
933
934 ucsi_reset_connector(con, true);
935 }
936
937 return ret;
938 }
939
ucsi_dr_swap(struct typec_port * port,enum typec_data_role role)940 static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
941 {
942 struct ucsi_connector *con = typec_get_drvdata(port);
943 u8 partner_type;
944 u64 command;
945 int ret = 0;
946
947 mutex_lock(&con->lock);
948
949 if (!con->partner) {
950 ret = -ENOTCONN;
951 goto out_unlock;
952 }
953
954 partner_type = UCSI_CONSTAT_PARTNER_TYPE(con->status.flags);
955 if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
956 role == TYPEC_DEVICE) ||
957 (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
958 role == TYPEC_HOST))
959 goto out_unlock;
960
961 reinit_completion(&con->complete);
962
963 command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num);
964 command |= UCSI_SET_UOR_ROLE(role);
965 command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS;
966 ret = ucsi_role_cmd(con, command);
967 if (ret < 0)
968 goto out_unlock;
969
970 mutex_unlock(&con->lock);
971
972 if (!wait_for_completion_timeout(&con->complete,
973 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
974 return -ETIMEDOUT;
975
976 return 0;
977
978 out_unlock:
979 mutex_unlock(&con->lock);
980
981 return ret;
982 }
983
ucsi_pr_swap(struct typec_port * port,enum typec_role role)984 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
985 {
986 struct ucsi_connector *con = typec_get_drvdata(port);
987 enum typec_role cur_role;
988 u64 command;
989 int ret = 0;
990
991 mutex_lock(&con->lock);
992
993 if (!con->partner) {
994 ret = -ENOTCONN;
995 goto out_unlock;
996 }
997
998 cur_role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
999
1000 if (cur_role == role)
1001 goto out_unlock;
1002
1003 reinit_completion(&con->complete);
1004
1005 command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num);
1006 command |= UCSI_SET_PDR_ROLE(role);
1007 command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS;
1008 ret = ucsi_role_cmd(con, command);
1009 if (ret < 0)
1010 goto out_unlock;
1011
1012 mutex_unlock(&con->lock);
1013
1014 if (!wait_for_completion_timeout(&con->complete,
1015 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1016 return -ETIMEDOUT;
1017
1018 mutex_lock(&con->lock);
1019
1020 /* Something has gone wrong while swapping the role */
1021 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) !=
1022 UCSI_CONSTAT_PWR_OPMODE_PD) {
1023 ucsi_reset_connector(con, true);
1024 ret = -EPROTO;
1025 }
1026
1027 out_unlock:
1028 mutex_unlock(&con->lock);
1029
1030 return ret;
1031 }
1032
1033 static const struct typec_operations ucsi_ops = {
1034 .dr_set = ucsi_dr_swap,
1035 .pr_set = ucsi_pr_swap
1036 };
1037
1038 /* Caller must call fwnode_handle_put() after use */
ucsi_find_fwnode(struct ucsi_connector * con)1039 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
1040 {
1041 struct fwnode_handle *fwnode;
1042 int i = 1;
1043
1044 device_for_each_child_node(con->ucsi->dev, fwnode)
1045 if (i++ == con->num)
1046 return fwnode;
1047 return NULL;
1048 }
1049
ucsi_register_port(struct ucsi * ucsi,int index)1050 static int ucsi_register_port(struct ucsi *ucsi, int index)
1051 {
1052 struct ucsi_connector *con = &ucsi->connector[index];
1053 struct typec_capability *cap = &con->typec_cap;
1054 enum typec_accessory *accessory = cap->accessory;
1055 enum usb_role u_role = USB_ROLE_NONE;
1056 u64 command;
1057 int ret;
1058
1059 INIT_WORK(&con->work, ucsi_handle_connector_change);
1060 init_completion(&con->complete);
1061 mutex_init(&con->lock);
1062 con->num = index + 1;
1063 con->ucsi = ucsi;
1064
1065 cap->fwnode = ucsi_find_fwnode(con);
1066 con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode);
1067 if (IS_ERR(con->usb_role_sw)) {
1068 dev_err(ucsi->dev, "con%d: failed to get usb role switch\n",
1069 con->num);
1070 return PTR_ERR(con->usb_role_sw);
1071 }
1072
1073
1074 /* Delay other interactions with the con until registration is complete */
1075 mutex_lock(&con->lock);
1076
1077 /* Get connector capability */
1078 command = UCSI_GET_CONNECTOR_CAPABILITY;
1079 command |= UCSI_CONNECTOR_NUMBER(con->num);
1080 ret = ucsi_send_command(ucsi, command, &con->cap, sizeof(con->cap));
1081 if (ret < 0)
1082 goto out_unlock;
1083
1084 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP)
1085 cap->data = TYPEC_PORT_DRD;
1086 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP)
1087 cap->data = TYPEC_PORT_DFP;
1088 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP)
1089 cap->data = TYPEC_PORT_UFP;
1090
1091 if ((con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER) &&
1092 (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER))
1093 cap->type = TYPEC_PORT_DRP;
1094 else if (con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER)
1095 cap->type = TYPEC_PORT_SRC;
1096 else if (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER)
1097 cap->type = TYPEC_PORT_SNK;
1098
1099 cap->revision = ucsi->cap.typec_version;
1100 cap->pd_revision = ucsi->cap.pd_version;
1101 cap->svdm_version = SVDM_VER_2_0;
1102 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
1103
1104 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY)
1105 *accessory++ = TYPEC_ACCESSORY_AUDIO;
1106 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
1107 *accessory = TYPEC_ACCESSORY_DEBUG;
1108
1109 cap->driver_data = con;
1110 cap->ops = &ucsi_ops;
1111
1112 ret = ucsi_register_port_psy(con);
1113 if (ret)
1114 goto out;
1115
1116 /* Register the connector */
1117 con->port = typec_register_port(ucsi->dev, cap);
1118 if (IS_ERR(con->port)) {
1119 ret = PTR_ERR(con->port);
1120 goto out;
1121 }
1122
1123 /* Alternate modes */
1124 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON);
1125 if (ret) {
1126 dev_err(ucsi->dev, "con%d: failed to register alt modes\n",
1127 con->num);
1128 goto out;
1129 }
1130
1131 /* Get the status */
1132 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
1133 ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
1134 if (ret < 0) {
1135 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
1136 ret = 0;
1137 goto out;
1138 }
1139 ret = 0; /* ucsi_send_command() returns length on success */
1140
1141 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1142 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1143 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1144 u_role = USB_ROLE_HOST;
1145 fallthrough;
1146 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1147 typec_set_data_role(con->port, TYPEC_HOST);
1148 break;
1149 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1150 u_role = USB_ROLE_DEVICE;
1151 typec_set_data_role(con->port, TYPEC_DEVICE);
1152 break;
1153 default:
1154 break;
1155 }
1156
1157 /* Check if there is already something connected */
1158 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1159 typec_set_pwr_role(con->port,
1160 !!(con->status.flags & UCSI_CONSTAT_PWR_DIR));
1161 ucsi_pwr_opmode_change(con);
1162 ucsi_register_partner(con);
1163 ucsi_port_psy_changed(con);
1164 }
1165
1166 /* Only notify USB controller if partner supports USB data */
1167 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
1168 u_role = USB_ROLE_NONE;
1169
1170 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1171 if (ret) {
1172 dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n",
1173 con->num, u_role);
1174 ret = 0;
1175 }
1176
1177 if (con->partner) {
1178 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
1179 if (ret) {
1180 dev_err(ucsi->dev,
1181 "con%d: failed to register alternate modes\n",
1182 con->num);
1183 ret = 0;
1184 } else {
1185 ucsi_altmode_update_active(con);
1186 }
1187 }
1188
1189 trace_ucsi_register_port(con->num, &con->status);
1190
1191 out:
1192 fwnode_handle_put(cap->fwnode);
1193 out_unlock:
1194 mutex_unlock(&con->lock);
1195 return ret;
1196 }
1197
1198 /**
1199 * ucsi_init - Initialize UCSI interface
1200 * @ucsi: UCSI to be initialized
1201 *
1202 * Registers all ports @ucsi has and enables all notification events.
1203 */
ucsi_init(struct ucsi * ucsi)1204 static int ucsi_init(struct ucsi *ucsi)
1205 {
1206 struct ucsi_connector *con;
1207 u64 command, ntfy;
1208 int ret;
1209 int i;
1210
1211 /* Reset the PPM */
1212 ret = ucsi_reset_ppm(ucsi);
1213 if (ret) {
1214 dev_err(ucsi->dev, "failed to reset PPM!\n");
1215 goto err;
1216 }
1217
1218 /* Enable basic notifications */
1219 ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
1220 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1221 ret = ucsi_send_command(ucsi, command, NULL, 0);
1222 if (ret < 0)
1223 goto err_reset;
1224
1225 /* Get PPM capabilities */
1226 command = UCSI_GET_CAPABILITY;
1227 ret = ucsi_send_command(ucsi, command, &ucsi->cap, sizeof(ucsi->cap));
1228 if (ret < 0)
1229 goto err_reset;
1230
1231 if (!ucsi->cap.num_connectors) {
1232 ret = -ENODEV;
1233 goto err_reset;
1234 }
1235
1236 /* Allocate the connectors. Released in ucsi_unregister() */
1237 ucsi->connector = kcalloc(ucsi->cap.num_connectors + 1,
1238 sizeof(*ucsi->connector), GFP_KERNEL);
1239 if (!ucsi->connector) {
1240 ret = -ENOMEM;
1241 goto err_reset;
1242 }
1243
1244 /* Register all connectors */
1245 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1246 ret = ucsi_register_port(ucsi, i);
1247 if (ret)
1248 goto err_unregister;
1249 }
1250
1251 /* Enable all notifications */
1252 ntfy = UCSI_ENABLE_NTFY_ALL;
1253 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1254 ret = ucsi_send_command(ucsi, command, NULL, 0);
1255 if (ret < 0)
1256 goto err_unregister;
1257
1258 ucsi->ntfy = ntfy;
1259 return 0;
1260
1261 err_unregister:
1262 for (con = ucsi->connector; con->port; con++) {
1263 ucsi_unregister_partner(con);
1264 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON);
1265 ucsi_unregister_port_psy(con);
1266 typec_unregister_port(con->port);
1267 con->port = NULL;
1268 }
1269
1270 err_reset:
1271 memset(&ucsi->cap, 0, sizeof(ucsi->cap));
1272 ucsi_reset_ppm(ucsi);
1273 err:
1274 return ret;
1275 }
1276
ucsi_init_work(struct work_struct * work)1277 static void ucsi_init_work(struct work_struct *work)
1278 {
1279 struct ucsi_android *aucsi = container_of(work,
1280 struct ucsi_android, work.work);
1281 int ret;
1282
1283 ret = ucsi_init(&aucsi->ucsi);
1284 if (ret)
1285 dev_err(aucsi->ucsi.dev, "PPM init failed (%d)\n", ret);
1286
1287 if (ret == -EPROBE_DEFER) {
1288 if (aucsi->work_count++ > UCSI_ROLE_SWITCH_WAIT_COUNT)
1289 return;
1290
1291 queue_delayed_work(system_long_wq, &aucsi->work,
1292 UCSI_ROLE_SWITCH_INTERVAL);
1293 }
1294 }
1295
1296 /**
1297 * ucsi_get_drvdata - Return private driver data pointer
1298 * @ucsi: UCSI interface
1299 */
ucsi_get_drvdata(struct ucsi * ucsi)1300 void *ucsi_get_drvdata(struct ucsi *ucsi)
1301 {
1302 return ucsi->driver_data;
1303 }
1304 EXPORT_SYMBOL_GPL(ucsi_get_drvdata);
1305
1306 /**
1307 * ucsi_set_drvdata - Assign private driver data pointer
1308 * @ucsi: UCSI interface
1309 * @data: Private data pointer
1310 */
ucsi_set_drvdata(struct ucsi * ucsi,void * data)1311 void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
1312 {
1313 ucsi->driver_data = data;
1314 }
1315 EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
1316
1317 /**
1318 * ucsi_create - Allocate UCSI instance
1319 * @dev: Device interface to the PPM (Platform Policy Manager)
1320 * @ops: I/O routines
1321 */
ucsi_create(struct device * dev,const struct ucsi_operations * ops)1322 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
1323 {
1324 struct ucsi *ucsi;
1325 struct ucsi_android *aucsi;
1326
1327 if (!ops || !ops->read || !ops->sync_write || !ops->async_write)
1328 return ERR_PTR(-EINVAL);
1329
1330 aucsi = kzalloc(sizeof(*aucsi), GFP_KERNEL);
1331 if (!aucsi)
1332 return ERR_PTR(-ENOMEM);
1333
1334 ucsi = &aucsi->ucsi;
1335 INIT_DELAYED_WORK(&aucsi->work, ucsi_init_work);
1336 mutex_init(&ucsi->ppm_lock);
1337 ucsi->dev = dev;
1338 ucsi->ops = ops;
1339
1340 return ucsi;
1341 }
1342 EXPORT_SYMBOL_GPL(ucsi_create);
1343
1344 /**
1345 * ucsi_destroy - Free UCSI instance
1346 * @ucsi: UCSI instance to be freed
1347 */
ucsi_destroy(struct ucsi * ucsi)1348 void ucsi_destroy(struct ucsi *ucsi)
1349 {
1350 struct ucsi_android *aucsi = container_of(ucsi,
1351 struct ucsi_android, ucsi);
1352 kfree(aucsi);
1353 }
1354 EXPORT_SYMBOL_GPL(ucsi_destroy);
1355
1356 /**
1357 * ucsi_register - Register UCSI interface
1358 * @ucsi: UCSI instance
1359 */
ucsi_register(struct ucsi * ucsi)1360 int ucsi_register(struct ucsi *ucsi)
1361 {
1362 struct ucsi_android *aucsi = container_of(ucsi,
1363 struct ucsi_android, ucsi);
1364 int ret;
1365
1366 ret = ucsi->ops->read(ucsi, UCSI_VERSION, &ucsi->version,
1367 sizeof(ucsi->version));
1368 if (ret)
1369 return ret;
1370
1371 if (!ucsi->version)
1372 return -ENODEV;
1373
1374 queue_delayed_work(system_long_wq, &aucsi->work, 0);
1375
1376 return 0;
1377 }
1378 EXPORT_SYMBOL_GPL(ucsi_register);
1379
1380 /**
1381 * ucsi_unregister - Unregister UCSI interface
1382 * @ucsi: UCSI interface to be unregistered
1383 *
1384 * Unregister UCSI interface that was created with ucsi_register().
1385 */
ucsi_unregister(struct ucsi * ucsi)1386 void ucsi_unregister(struct ucsi *ucsi)
1387 {
1388 struct ucsi_android *aucsi = container_of(ucsi,
1389 struct ucsi_android, ucsi);
1390 u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
1391 int i;
1392
1393 /* Make sure that we are not in the middle of driver initialization */
1394 cancel_delayed_work_sync(&aucsi->work);
1395
1396 /* Disable notifications */
1397 ucsi->ops->async_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
1398
1399 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1400 cancel_work_sync(&ucsi->connector[i].work);
1401 ucsi_unregister_partner(&ucsi->connector[i]);
1402 ucsi_unregister_altmodes(&ucsi->connector[i],
1403 UCSI_RECIPIENT_CON);
1404 ucsi_unregister_port_psy(&ucsi->connector[i]);
1405 typec_unregister_port(ucsi->connector[i].port);
1406 }
1407
1408 kfree(ucsi->connector);
1409 }
1410 EXPORT_SYMBOL_GPL(ucsi_unregister);
1411
1412 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1413 MODULE_LICENSE("GPL v2");
1414 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");
1415