1
2
3
4
5
6
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#define to_ucsi_connector(_cap_) container_of(_cap_, struct ucsi_connector, \
21 typec_cap)
22
23
24
25
26
27
28
29
30
31#define UCSI_TIMEOUT_MS 5000
32
33
34
35
36
37
38
39
40#define UCSI_SWAP_TIMEOUT_MS 5000
41
42static inline int ucsi_sync(struct ucsi *ucsi)
43{
44 if (ucsi->ppm && ucsi->ppm->sync)
45 return ucsi->ppm->sync(ucsi->ppm);
46 return 0;
47}
48
49static int ucsi_command(struct ucsi *ucsi, struct ucsi_control *ctrl)
50{
51 int ret;
52
53 trace_ucsi_command(ctrl);
54
55 set_bit(COMMAND_PENDING, &ucsi->flags);
56
57 ret = ucsi->ppm->cmd(ucsi->ppm, ctrl);
58 if (ret)
59 goto err_clear_flag;
60
61 if (!wait_for_completion_timeout(&ucsi->complete,
62 msecs_to_jiffies(UCSI_TIMEOUT_MS))) {
63 dev_warn(ucsi->dev, "PPM NOT RESPONDING\n");
64 ret = -ETIMEDOUT;
65 }
66
67err_clear_flag:
68 clear_bit(COMMAND_PENDING, &ucsi->flags);
69
70 return ret;
71}
72
73static int ucsi_ack(struct ucsi *ucsi, u8 ack)
74{
75 struct ucsi_control ctrl;
76 int ret;
77
78 trace_ucsi_ack(ack);
79
80 set_bit(ACK_PENDING, &ucsi->flags);
81
82 UCSI_CMD_ACK(ctrl, ack);
83 ret = ucsi->ppm->cmd(ucsi->ppm, &ctrl);
84 if (ret)
85 goto out_clear_bit;
86
87
88 if (ack == UCSI_ACK_EVENT)
89 goto out_clear_bit;
90
91 if (!wait_for_completion_timeout(&ucsi->complete,
92 msecs_to_jiffies(UCSI_TIMEOUT_MS)))
93 ret = -ETIMEDOUT;
94
95out_clear_bit:
96 clear_bit(ACK_PENDING, &ucsi->flags);
97
98 if (ret)
99 dev_err(ucsi->dev, "%s: failed\n", __func__);
100
101 return ret;
102}
103
104static int ucsi_run_command(struct ucsi *ucsi, struct ucsi_control *ctrl,
105 void *data, size_t size)
106{
107 struct ucsi_control _ctrl;
108 u8 data_length;
109 u16 error;
110 int ret;
111
112 ret = ucsi_command(ucsi, ctrl);
113 if (ret)
114 goto err;
115
116 switch (ucsi->status) {
117 case UCSI_IDLE:
118 ret = ucsi_sync(ucsi);
119 if (ret)
120 dev_warn(ucsi->dev, "%s: sync failed\n", __func__);
121
122 if (data)
123 memcpy(data, ucsi->ppm->data->message_in, size);
124
125 data_length = ucsi->ppm->data->cci.data_length;
126
127 ret = ucsi_ack(ucsi, UCSI_ACK_CMD);
128 if (!ret)
129 ret = data_length;
130 break;
131 case UCSI_BUSY:
132
133 ret = -EBUSY;
134 break;
135 case UCSI_ERROR:
136 ret = ucsi_ack(ucsi, UCSI_ACK_CMD);
137 if (ret)
138 break;
139
140 _ctrl.raw_cmd = 0;
141 _ctrl.cmd.cmd = UCSI_GET_ERROR_STATUS;
142 ret = ucsi_command(ucsi, &_ctrl);
143 if (ret) {
144 dev_err(ucsi->dev, "reading error failed!\n");
145 break;
146 }
147
148 memcpy(&error, ucsi->ppm->data->message_in, sizeof(error));
149
150
151 if (WARN_ON(ucsi->status == UCSI_ERROR)) {
152 ret = -ENODEV;
153 break;
154 }
155
156 ret = ucsi_ack(ucsi, UCSI_ACK_CMD);
157 if (ret)
158 break;
159
160 switch (error) {
161 case UCSI_ERROR_INCOMPATIBLE_PARTNER:
162 ret = -EOPNOTSUPP;
163 break;
164 case UCSI_ERROR_CC_COMMUNICATION_ERR:
165 ret = -ECOMM;
166 break;
167 case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
168 ret = -EPROTO;
169 break;
170 case UCSI_ERROR_DEAD_BATTERY:
171 dev_warn(ucsi->dev, "Dead battery condition!\n");
172 ret = -EPERM;
173 break;
174
175 case UCSI_ERROR_INVALID_CON_NUM:
176 case UCSI_ERROR_UNREGONIZED_CMD:
177 case UCSI_ERROR_INVALID_CMD_ARGUMENT:
178 dev_warn(ucsi->dev,
179 "%s: possible UCSI driver bug - error 0x%x\n",
180 __func__, error);
181 ret = -EINVAL;
182 break;
183 default:
184 dev_warn(ucsi->dev,
185 "%s: error without status\n", __func__);
186 ret = -EIO;
187 break;
188 }
189 break;
190 }
191
192err:
193 trace_ucsi_run_command(ctrl, ret);
194
195 return ret;
196}
197
198int ucsi_send_command(struct ucsi *ucsi, struct ucsi_control *ctrl,
199 void *retval, size_t size)
200{
201 int ret;
202
203 mutex_lock(&ucsi->ppm_lock);
204 ret = ucsi_run_command(ucsi, ctrl, retval, size);
205 mutex_unlock(&ucsi->ppm_lock);
206
207 return ret;
208}
209EXPORT_SYMBOL_GPL(ucsi_send_command);
210
211int ucsi_resume(struct ucsi *ucsi)
212{
213 struct ucsi_control ctrl;
214
215
216 UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_ALL);
217 return ucsi_send_command(ucsi, &ctrl, NULL, 0);
218}
219EXPORT_SYMBOL_GPL(ucsi_resume);
220
221
222void ucsi_altmode_update_active(struct ucsi_connector *con)
223{
224 const struct typec_altmode *altmode = NULL;
225 struct ucsi_control ctrl;
226 int ret;
227 u8 cur;
228 int i;
229
230 UCSI_CMD_GET_CURRENT_CAM(ctrl, con->num);
231 ret = ucsi_run_command(con->ucsi, &ctrl, &cur, sizeof(cur));
232 if (ret < 0) {
233 if (con->ucsi->ppm->data->version > 0x0100) {
234 dev_err(con->ucsi->dev,
235 "GET_CURRENT_CAM command failed\n");
236 return;
237 }
238 cur = 0xff;
239 }
240
241 if (cur < UCSI_MAX_ALTMODES)
242 altmode = typec_altmode_get_partner(con->port_altmode[cur]);
243
244 for (i = 0; con->partner_altmode[i]; i++)
245 typec_altmode_update_active(con->partner_altmode[i],
246 con->partner_altmode[i] == altmode);
247}
248
249static u8 ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
250{
251 u8 mode = 1;
252 int i;
253
254 for (i = 0; alt[i]; i++)
255 if (alt[i]->svid == svid)
256 mode++;
257
258 return mode;
259}
260
261static int ucsi_next_altmode(struct typec_altmode **alt)
262{
263 int i = 0;
264
265 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
266 if (!alt[i])
267 return i;
268
269 return -ENOENT;
270}
271
272static int ucsi_register_altmode(struct ucsi_connector *con,
273 struct typec_altmode_desc *desc,
274 u8 recipient)
275{
276 struct typec_altmode *alt;
277 bool override;
278 int ret;
279 int i;
280
281 override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
282
283 switch (recipient) {
284 case UCSI_RECIPIENT_CON:
285 i = ucsi_next_altmode(con->port_altmode);
286 if (i < 0) {
287 ret = i;
288 goto err;
289 }
290
291 desc->mode = ucsi_altmode_next_mode(con->port_altmode,
292 desc->svid);
293
294 switch (desc->svid) {
295 case USB_TYPEC_DP_SID:
296 case USB_TYPEC_NVIDIA_VLINK_SID:
297 alt = ucsi_register_displayport(con, override, i, desc);
298 break;
299 default:
300 alt = typec_port_register_altmode(con->port, desc);
301 break;
302 }
303
304 if (IS_ERR(alt)) {
305 ret = PTR_ERR(alt);
306 goto err;
307 }
308
309 con->port_altmode[i] = alt;
310 break;
311 case UCSI_RECIPIENT_SOP:
312 i = ucsi_next_altmode(con->partner_altmode);
313 if (i < 0) {
314 ret = i;
315 goto err;
316 }
317
318 desc->mode = ucsi_altmode_next_mode(con->partner_altmode,
319 desc->svid);
320
321 alt = typec_partner_register_altmode(con->partner, desc);
322 if (IS_ERR(alt)) {
323 ret = PTR_ERR(alt);
324 goto err;
325 }
326
327 con->partner_altmode[i] = alt;
328 break;
329 default:
330 return -EINVAL;
331 }
332
333 trace_ucsi_register_altmode(recipient, alt);
334
335 return 0;
336
337err:
338 dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n",
339 desc->svid, desc->mode);
340
341 return ret;
342}
343
344static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
345{
346 int max_altmodes = UCSI_MAX_ALTMODES;
347 struct typec_altmode_desc desc;
348 struct ucsi_altmode alt[2];
349 struct ucsi_control ctrl;
350 int num = 1;
351 int ret;
352 int len;
353 int j;
354 int i;
355
356 if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
357 return 0;
358
359 if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
360 return 0;
361
362 if (recipient == UCSI_RECIPIENT_CON)
363 max_altmodes = con->ucsi->cap.num_alt_modes;
364
365 for (i = 0; i < max_altmodes;) {
366 memset(alt, 0, sizeof(alt));
367 UCSI_CMD_GET_ALTERNATE_MODES(ctrl, recipient, con->num, i, 1);
368 len = ucsi_run_command(con->ucsi, &ctrl, alt, sizeof(alt));
369 if (len <= 0)
370 return len;
371
372
373
374
375
376
377
378 num = len / sizeof(alt[0]);
379 i += num;
380
381 for (j = 0; j < num; j++) {
382 if (!alt[j].svid)
383 return 0;
384
385 memset(&desc, 0, sizeof(desc));
386 desc.vdo = alt[j].mid;
387 desc.svid = alt[j].svid;
388 desc.roles = TYPEC_PORT_DRD;
389
390 ret = ucsi_register_altmode(con, &desc, recipient);
391 if (ret)
392 return ret;
393 }
394 }
395
396 return 0;
397}
398
399static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
400{
401 const struct typec_altmode *pdev;
402 struct typec_altmode **adev;
403 int i = 0;
404
405 switch (recipient) {
406 case UCSI_RECIPIENT_CON:
407 adev = con->port_altmode;
408 break;
409 case UCSI_RECIPIENT_SOP:
410 adev = con->partner_altmode;
411 break;
412 default:
413 return;
414 }
415
416 while (adev[i]) {
417 if (recipient == UCSI_RECIPIENT_SOP &&
418 (adev[i]->svid == USB_TYPEC_DP_SID ||
419 adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID)) {
420 pdev = typec_altmode_get_partner(adev[i]);
421 ucsi_displayport_remove_partner((void *)pdev);
422 }
423 typec_unregister_altmode(adev[i]);
424 adev[i++] = NULL;
425 }
426}
427
428static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
429{
430 switch (con->status.pwr_op_mode) {
431 case UCSI_CONSTAT_PWR_OPMODE_PD:
432 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
433 break;
434 case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
435 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
436 break;
437 case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
438 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
439 break;
440 default:
441 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
442 break;
443 }
444}
445
446static int ucsi_register_partner(struct ucsi_connector *con)
447{
448 struct typec_partner_desc desc;
449 struct typec_partner *partner;
450
451 if (con->partner)
452 return 0;
453
454 memset(&desc, 0, sizeof(desc));
455
456 switch (con->status.partner_type) {
457 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
458 desc.accessory = TYPEC_ACCESSORY_DEBUG;
459 break;
460 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
461 desc.accessory = TYPEC_ACCESSORY_AUDIO;
462 break;
463 default:
464 break;
465 }
466
467 desc.usb_pd = con->status.pwr_op_mode == UCSI_CONSTAT_PWR_OPMODE_PD;
468
469 partner = typec_register_partner(con->port, &desc);
470 if (IS_ERR(partner)) {
471 dev_err(con->ucsi->dev,
472 "con%d: failed to register partner (%ld)\n", con->num,
473 PTR_ERR(partner));
474 return PTR_ERR(partner);
475 }
476
477 con->partner = partner;
478
479 return 0;
480}
481
482static void ucsi_unregister_partner(struct ucsi_connector *con)
483{
484 if (!con->partner)
485 return;
486
487 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
488 typec_unregister_partner(con->partner);
489 con->partner = NULL;
490}
491
492static void ucsi_partner_change(struct ucsi_connector *con)
493{
494 int ret;
495
496 if (!con->partner)
497 return;
498
499 switch (con->status.partner_type) {
500 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
501 typec_set_data_role(con->port, TYPEC_HOST);
502 break;
503 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
504 typec_set_data_role(con->port, TYPEC_DEVICE);
505 break;
506 default:
507 break;
508 }
509
510
511 if (!completion_done(&con->complete))
512 complete(&con->complete);
513
514
515 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
516 if (ret)
517 dev_err(con->ucsi->dev,
518 "con%d: failed to register partner alternate modes\n",
519 con->num);
520 else
521 ucsi_altmode_update_active(con);
522}
523
524static void ucsi_connector_change(struct work_struct *work)
525{
526 struct ucsi_connector *con = container_of(work, struct ucsi_connector,
527 work);
528 struct ucsi *ucsi = con->ucsi;
529 struct ucsi_control ctrl;
530 int ret;
531
532 mutex_lock(&con->lock);
533
534 UCSI_CMD_GET_CONNECTOR_STATUS(ctrl, con->num);
535 ret = ucsi_send_command(ucsi, &ctrl, &con->status, sizeof(con->status));
536 if (ret < 0) {
537 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
538 __func__, ret);
539 goto out_unlock;
540 }
541
542 if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE)
543 ucsi_pwr_opmode_change(con);
544
545 if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
546 typec_set_pwr_role(con->port, con->status.pwr_dir);
547
548
549 if (!completion_done(&con->complete))
550 complete(&con->complete);
551 }
552
553 if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
554 typec_set_pwr_role(con->port, con->status.pwr_dir);
555
556 switch (con->status.partner_type) {
557 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
558 typec_set_data_role(con->port, TYPEC_HOST);
559 break;
560 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
561 typec_set_data_role(con->port, TYPEC_DEVICE);
562 break;
563 default:
564 break;
565 }
566
567 if (con->status.connected)
568 ucsi_register_partner(con);
569 else
570 ucsi_unregister_partner(con);
571 }
572
573 if (con->status.change & UCSI_CONSTAT_CAM_CHANGE) {
574
575
576
577
578
579 UCSI_CMD_GET_CAM_SUPPORTED(ctrl, con->num);
580 ucsi_run_command(con->ucsi, &ctrl, NULL, 0);
581 }
582
583 if (con->status.change & UCSI_CONSTAT_PARTNER_CHANGE)
584 ucsi_partner_change(con);
585
586 ret = ucsi_ack(ucsi, UCSI_ACK_EVENT);
587 if (ret)
588 dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret);
589
590 trace_ucsi_connector_change(con->num, &con->status);
591
592out_unlock:
593 clear_bit(EVENT_PENDING, &ucsi->flags);
594 mutex_unlock(&con->lock);
595}
596
597
598
599
600
601
602
603void ucsi_notify(struct ucsi *ucsi)
604{
605 struct ucsi_cci *cci;
606
607
608 ucsi_sync(ucsi);
609
610 cci = &ucsi->ppm->data->cci;
611
612 if (cci->error)
613 ucsi->status = UCSI_ERROR;
614 else if (cci->busy)
615 ucsi->status = UCSI_BUSY;
616 else
617 ucsi->status = UCSI_IDLE;
618
619 if (cci->cmd_complete && test_bit(COMMAND_PENDING, &ucsi->flags)) {
620 complete(&ucsi->complete);
621 } else if (cci->ack_complete && test_bit(ACK_PENDING, &ucsi->flags)) {
622 complete(&ucsi->complete);
623 } else if (cci->connector_change) {
624 struct ucsi_connector *con;
625
626 con = &ucsi->connector[cci->connector_change - 1];
627
628 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
629 schedule_work(&con->work);
630 }
631
632 trace_ucsi_notify(ucsi->ppm->data->raw_cci);
633}
634EXPORT_SYMBOL_GPL(ucsi_notify);
635
636
637
638static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
639{
640 struct ucsi_control ctrl;
641
642 UCSI_CMD_CONNECTOR_RESET(ctrl, con, hard);
643
644 return ucsi_send_command(con->ucsi, &ctrl, NULL, 0);
645}
646
647static int ucsi_reset_ppm(struct ucsi *ucsi)
648{
649 struct ucsi_control ctrl;
650 unsigned long tmo;
651 int ret;
652
653 ctrl.raw_cmd = 0;
654 ctrl.cmd.cmd = UCSI_PPM_RESET;
655 trace_ucsi_command(&ctrl);
656 ret = ucsi->ppm->cmd(ucsi->ppm, &ctrl);
657 if (ret)
658 goto err;
659
660 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
661
662 do {
663
664 ret = ucsi_sync(ucsi);
665 if (ret)
666 goto err;
667
668 if (ucsi->ppm->data->cci.reset_complete)
669 break;
670
671
672 if (ucsi->ppm->data->raw_cci) {
673 dev_warn_ratelimited(ucsi->dev,
674 "Failed to reset PPM! Trying again..\n");
675
676 trace_ucsi_command(&ctrl);
677 ret = ucsi->ppm->cmd(ucsi->ppm, &ctrl);
678 if (ret)
679 goto err;
680 }
681
682
683 msleep(20);
684
685 ret = -ETIMEDOUT;
686 } while (time_is_after_jiffies(tmo));
687
688err:
689 trace_ucsi_reset_ppm(&ctrl, ret);
690
691 return ret;
692}
693
694static int ucsi_role_cmd(struct ucsi_connector *con, struct ucsi_control *ctrl)
695{
696 int ret;
697
698 ret = ucsi_send_command(con->ucsi, ctrl, NULL, 0);
699 if (ret == -ETIMEDOUT) {
700 struct ucsi_control c;
701
702
703 mutex_lock(&con->ucsi->ppm_lock);
704 ucsi_reset_ppm(con->ucsi);
705 mutex_unlock(&con->ucsi->ppm_lock);
706
707 UCSI_CMD_SET_NTFY_ENABLE(c, UCSI_ENABLE_NTFY_ALL);
708 ucsi_send_command(con->ucsi, &c, NULL, 0);
709
710 ucsi_reset_connector(con, true);
711 }
712
713 return ret;
714}
715
716static int
717ucsi_dr_swap(const struct typec_capability *cap, enum typec_data_role role)
718{
719 struct ucsi_connector *con = to_ucsi_connector(cap);
720 struct ucsi_control ctrl;
721 int ret = 0;
722
723 mutex_lock(&con->lock);
724
725 if (!con->partner) {
726 ret = -ENOTCONN;
727 goto out_unlock;
728 }
729
730 if ((con->status.partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
731 role == TYPEC_DEVICE) ||
732 (con->status.partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
733 role == TYPEC_HOST))
734 goto out_unlock;
735
736 UCSI_CMD_SET_UOR(ctrl, con, role);
737 ret = ucsi_role_cmd(con, &ctrl);
738 if (ret < 0)
739 goto out_unlock;
740
741 if (!wait_for_completion_timeout(&con->complete,
742 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
743 ret = -ETIMEDOUT;
744
745out_unlock:
746 mutex_unlock(&con->lock);
747
748 return ret < 0 ? ret : 0;
749}
750
751static int
752ucsi_pr_swap(const struct typec_capability *cap, enum typec_role role)
753{
754 struct ucsi_connector *con = to_ucsi_connector(cap);
755 struct ucsi_control ctrl;
756 int ret = 0;
757
758 mutex_lock(&con->lock);
759
760 if (!con->partner) {
761 ret = -ENOTCONN;
762 goto out_unlock;
763 }
764
765 if (con->status.pwr_dir == role)
766 goto out_unlock;
767
768 UCSI_CMD_SET_PDR(ctrl, con, role);
769 ret = ucsi_role_cmd(con, &ctrl);
770 if (ret < 0)
771 goto out_unlock;
772
773 if (!wait_for_completion_timeout(&con->complete,
774 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS))) {
775 ret = -ETIMEDOUT;
776 goto out_unlock;
777 }
778
779
780 if (con->status.pwr_op_mode != UCSI_CONSTAT_PWR_OPMODE_PD) {
781 ucsi_reset_connector(con, true);
782 ret = -EPROTO;
783 }
784
785out_unlock:
786 mutex_unlock(&con->lock);
787
788 return ret;
789}
790
791static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
792{
793 struct fwnode_handle *fwnode;
794 int i = 1;
795
796 device_for_each_child_node(con->ucsi->dev, fwnode)
797 if (i++ == con->num)
798 return fwnode;
799 return NULL;
800}
801
802static int ucsi_register_port(struct ucsi *ucsi, int index)
803{
804 struct ucsi_connector *con = &ucsi->connector[index];
805 struct typec_capability *cap = &con->typec_cap;
806 enum typec_accessory *accessory = cap->accessory;
807 struct ucsi_control ctrl;
808 int ret;
809
810 INIT_WORK(&con->work, ucsi_connector_change);
811 init_completion(&con->complete);
812 mutex_init(&con->lock);
813 con->num = index + 1;
814 con->ucsi = ucsi;
815
816
817 UCSI_CMD_GET_CONNECTOR_CAPABILITY(ctrl, con->num);
818 ret = ucsi_run_command(ucsi, &ctrl, &con->cap, sizeof(con->cap));
819 if (ret < 0)
820 return ret;
821
822 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP)
823 cap->data = TYPEC_PORT_DRD;
824 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP)
825 cap->data = TYPEC_PORT_DFP;
826 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP)
827 cap->data = TYPEC_PORT_UFP;
828
829 if (con->cap.provider && con->cap.consumer)
830 cap->type = TYPEC_PORT_DRP;
831 else if (con->cap.provider)
832 cap->type = TYPEC_PORT_SRC;
833 else if (con->cap.consumer)
834 cap->type = TYPEC_PORT_SNK;
835
836 cap->revision = ucsi->cap.typec_version;
837 cap->pd_revision = ucsi->cap.pd_version;
838 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
839
840 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY)
841 *accessory++ = TYPEC_ACCESSORY_AUDIO;
842 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
843 *accessory = TYPEC_ACCESSORY_DEBUG;
844
845 cap->fwnode = ucsi_find_fwnode(con);
846 cap->dr_set = ucsi_dr_swap;
847 cap->pr_set = ucsi_pr_swap;
848
849
850 con->port = typec_register_port(ucsi->dev, cap);
851 if (IS_ERR(con->port))
852 return PTR_ERR(con->port);
853
854
855 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON);
856 if (ret)
857 dev_err(ucsi->dev, "con%d: failed to register alt modes\n",
858 con->num);
859
860
861 UCSI_CMD_GET_CONNECTOR_STATUS(ctrl, con->num);
862 ret = ucsi_run_command(ucsi, &ctrl, &con->status, sizeof(con->status));
863 if (ret < 0) {
864 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
865 return 0;
866 }
867
868 ucsi_pwr_opmode_change(con);
869 typec_set_pwr_role(con->port, con->status.pwr_dir);
870
871 switch (con->status.partner_type) {
872 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
873 typec_set_data_role(con->port, TYPEC_HOST);
874 break;
875 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
876 typec_set_data_role(con->port, TYPEC_DEVICE);
877 break;
878 default:
879 break;
880 }
881
882
883 if (con->status.connected)
884 ucsi_register_partner(con);
885
886 if (con->partner) {
887 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
888 if (ret)
889 dev_err(ucsi->dev,
890 "con%d: failed to register alternate modes\n",
891 con->num);
892 else
893 ucsi_altmode_update_active(con);
894 }
895
896 trace_ucsi_register_port(con->num, &con->status);
897
898 return 0;
899}
900
901static void ucsi_init(struct work_struct *work)
902{
903 struct ucsi *ucsi = container_of(work, struct ucsi, work);
904 struct ucsi_connector *con;
905 struct ucsi_control ctrl;
906 int ret;
907 int i;
908
909 mutex_lock(&ucsi->ppm_lock);
910
911
912 ret = ucsi_reset_ppm(ucsi);
913 if (ret) {
914 dev_err(ucsi->dev, "failed to reset PPM!\n");
915 goto err;
916 }
917
918
919 UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_CMD_COMPLETE |
920 UCSI_ENABLE_NTFY_ERROR);
921 ret = ucsi_run_command(ucsi, &ctrl, NULL, 0);
922 if (ret < 0)
923 goto err_reset;
924
925
926 UCSI_CMD_GET_CAPABILITY(ctrl);
927 ret = ucsi_run_command(ucsi, &ctrl, &ucsi->cap, sizeof(ucsi->cap));
928 if (ret < 0)
929 goto err_reset;
930
931 if (!ucsi->cap.num_connectors) {
932 ret = -ENODEV;
933 goto err_reset;
934 }
935
936
937 ucsi->connector = kcalloc(ucsi->cap.num_connectors + 1,
938 sizeof(*ucsi->connector), GFP_KERNEL);
939 if (!ucsi->connector) {
940 ret = -ENOMEM;
941 goto err_reset;
942 }
943
944
945 for (i = 0; i < ucsi->cap.num_connectors; i++) {
946 ret = ucsi_register_port(ucsi, i);
947 if (ret)
948 goto err_unregister;
949 }
950
951
952 UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_ALL);
953 ret = ucsi_run_command(ucsi, &ctrl, NULL, 0);
954 if (ret < 0)
955 goto err_unregister;
956
957 mutex_unlock(&ucsi->ppm_lock);
958
959 return;
960
961err_unregister:
962 for (con = ucsi->connector; con->port; con++) {
963 ucsi_unregister_partner(con);
964 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON);
965 typec_unregister_port(con->port);
966 con->port = NULL;
967 }
968
969err_reset:
970 ucsi_reset_ppm(ucsi);
971err:
972 mutex_unlock(&ucsi->ppm_lock);
973 dev_err(ucsi->dev, "PPM init failed (%d)\n", ret);
974}
975
976
977
978
979
980
981
982
983
984struct ucsi *ucsi_register_ppm(struct device *dev, struct ucsi_ppm *ppm)
985{
986 struct ucsi *ucsi;
987
988 ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
989 if (!ucsi)
990 return ERR_PTR(-ENOMEM);
991
992 INIT_WORK(&ucsi->work, ucsi_init);
993 init_completion(&ucsi->complete);
994 mutex_init(&ucsi->ppm_lock);
995
996 ucsi->dev = dev;
997 ucsi->ppm = ppm;
998
999
1000
1001
1002
1003 queue_work(system_long_wq, &ucsi->work);
1004
1005 return ucsi;
1006}
1007EXPORT_SYMBOL_GPL(ucsi_register_ppm);
1008
1009
1010
1011
1012
1013
1014
1015void ucsi_unregister_ppm(struct ucsi *ucsi)
1016{
1017 struct ucsi_control ctrl;
1018 int i;
1019
1020
1021 cancel_work_sync(&ucsi->work);
1022
1023
1024 UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_CMD_COMPLETE)
1025 ucsi_send_command(ucsi, &ctrl, NULL, 0);
1026
1027 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1028 cancel_work_sync(&ucsi->connector[i].work);
1029 ucsi_unregister_partner(&ucsi->connector[i]);
1030 ucsi_unregister_altmodes(&ucsi->connector[i],
1031 UCSI_RECIPIENT_CON);
1032 typec_unregister_port(ucsi->connector[i].port);
1033 }
1034
1035 ucsi_reset_ppm(ucsi);
1036
1037 kfree(ucsi->connector);
1038 kfree(ucsi);
1039}
1040EXPORT_SYMBOL_GPL(ucsi_unregister_ppm);
1041
1042MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1043MODULE_LICENSE("GPL v2");
1044MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");
1045