Merge tag 'usb-6.1-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb
[platform/kernel/linux-starfive.git] / drivers / usb / typec / ucsi / ucsi.c
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
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
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
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
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                 ucsi->ops->async_write(ucsi, UCSI_CANCEL, NULL, 0);
137                 return -EBUSY;
138         }
139
140         if (!(cci & UCSI_CCI_COMMAND_COMPLETE))
141                 return -EIO;
142
143         if (cci & UCSI_CCI_NOT_SUPPORTED)
144                 return -EOPNOTSUPP;
145
146         if (cci & UCSI_CCI_ERROR) {
147                 if (cmd == UCSI_GET_ERROR_STATUS)
148                         return -EIO;
149                 return ucsi_read_error(ucsi);
150         }
151
152         return UCSI_CCI_LENGTH(cci);
153 }
154
155 int ucsi_send_command(struct ucsi *ucsi, u64 command,
156                       void *data, size_t size)
157 {
158         u8 length;
159         int ret;
160
161         mutex_lock(&ucsi->ppm_lock);
162
163         ret = ucsi_exec_command(ucsi, command);
164         if (ret < 0)
165                 goto out;
166
167         length = ret;
168
169         if (data) {
170                 ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, data, size);
171                 if (ret)
172                         goto out;
173         }
174
175         ret = ucsi_acknowledge_command(ucsi);
176         if (ret)
177                 goto out;
178
179         ret = length;
180 out:
181         mutex_unlock(&ucsi->ppm_lock);
182         return ret;
183 }
184 EXPORT_SYMBOL_GPL(ucsi_send_command);
185
186 int ucsi_resume(struct ucsi *ucsi)
187 {
188         u64 command;
189
190         /* Restore UCSI notification enable mask after system resume */
191         command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
192
193         return ucsi_send_command(ucsi, command, NULL, 0);
194 }
195 EXPORT_SYMBOL_GPL(ucsi_resume);
196 /* -------------------------------------------------------------------------- */
197
198 struct ucsi_work {
199         struct delayed_work work;
200         unsigned long delay;
201         unsigned int count;
202         struct ucsi_connector *con;
203         int (*cb)(struct ucsi_connector *);
204 };
205
206 static void ucsi_poll_worker(struct work_struct *work)
207 {
208         struct ucsi_work *uwork = container_of(work, struct ucsi_work, work.work);
209         struct ucsi_connector *con = uwork->con;
210         int ret;
211
212         mutex_lock(&con->lock);
213
214         if (!con->partner) {
215                 mutex_unlock(&con->lock);
216                 kfree(uwork);
217                 return;
218         }
219
220         ret = uwork->cb(con);
221
222         if (uwork->count-- && (ret == -EBUSY || ret == -ETIMEDOUT))
223                 queue_delayed_work(con->wq, &uwork->work, uwork->delay);
224         else
225                 kfree(uwork);
226
227         mutex_unlock(&con->lock);
228 }
229
230 static int ucsi_partner_task(struct ucsi_connector *con,
231                              int (*cb)(struct ucsi_connector *),
232                              int retries, unsigned long delay)
233 {
234         struct ucsi_work *uwork;
235
236         if (!con->partner)
237                 return 0;
238
239         uwork = kzalloc(sizeof(*uwork), GFP_KERNEL);
240         if (!uwork)
241                 return -ENOMEM;
242
243         INIT_DELAYED_WORK(&uwork->work, ucsi_poll_worker);
244         uwork->count = retries;
245         uwork->delay = delay;
246         uwork->con = con;
247         uwork->cb = cb;
248
249         queue_delayed_work(con->wq, &uwork->work, delay);
250
251         return 0;
252 }
253
254 /* -------------------------------------------------------------------------- */
255
256 void ucsi_altmode_update_active(struct ucsi_connector *con)
257 {
258         const struct typec_altmode *altmode = NULL;
259         u64 command;
260         int ret;
261         u8 cur;
262         int i;
263
264         command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
265         ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur));
266         if (ret < 0) {
267                 if (con->ucsi->version > 0x0100) {
268                         dev_err(con->ucsi->dev,
269                                 "GET_CURRENT_CAM command failed\n");
270                         return;
271                 }
272                 cur = 0xff;
273         }
274
275         if (cur < UCSI_MAX_ALTMODES)
276                 altmode = typec_altmode_get_partner(con->port_altmode[cur]);
277
278         for (i = 0; con->partner_altmode[i]; i++)
279                 typec_altmode_update_active(con->partner_altmode[i],
280                                             con->partner_altmode[i] == altmode);
281 }
282
283 static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
284 {
285         u8 mode = 1;
286         int i;
287
288         for (i = 0; alt[i]; i++) {
289                 if (i > MODE_DISCOVERY_MAX)
290                         return -ERANGE;
291
292                 if (alt[i]->svid == svid)
293                         mode++;
294         }
295
296         return mode;
297 }
298
299 static int ucsi_next_altmode(struct typec_altmode **alt)
300 {
301         int i = 0;
302
303         for (i = 0; i < UCSI_MAX_ALTMODES; i++)
304                 if (!alt[i])
305                         return i;
306
307         return -ENOENT;
308 }
309
310 static int ucsi_get_num_altmode(struct typec_altmode **alt)
311 {
312         int i;
313
314         for (i = 0; i < UCSI_MAX_ALTMODES; i++)
315                 if (!alt[i])
316                         break;
317
318         return i;
319 }
320
321 static int ucsi_register_altmode(struct ucsi_connector *con,
322                                  struct typec_altmode_desc *desc,
323                                  u8 recipient)
324 {
325         struct typec_altmode *alt;
326         bool override;
327         int ret;
328         int i;
329
330         override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
331
332         switch (recipient) {
333         case UCSI_RECIPIENT_CON:
334                 i = ucsi_next_altmode(con->port_altmode);
335                 if (i < 0) {
336                         ret = i;
337                         goto err;
338                 }
339
340                 ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid);
341                 if (ret < 0)
342                         return ret;
343
344                 desc->mode = ret;
345
346                 switch (desc->svid) {
347                 case USB_TYPEC_DP_SID:
348                         alt = ucsi_register_displayport(con, override, i, desc);
349                         break;
350                 case USB_TYPEC_NVIDIA_VLINK_SID:
351                         if (desc->vdo == USB_TYPEC_NVIDIA_VLINK_DBG_VDO)
352                                 alt = typec_port_register_altmode(con->port,
353                                                                   desc);
354                         else
355                                 alt = ucsi_register_displayport(con, override,
356                                                                 i, desc);
357                         break;
358                 default:
359                         alt = typec_port_register_altmode(con->port, desc);
360                         break;
361                 }
362
363                 if (IS_ERR(alt)) {
364                         ret = PTR_ERR(alt);
365                         goto err;
366                 }
367
368                 con->port_altmode[i] = alt;
369                 break;
370         case UCSI_RECIPIENT_SOP:
371                 i = ucsi_next_altmode(con->partner_altmode);
372                 if (i < 0) {
373                         ret = i;
374                         goto err;
375                 }
376
377                 ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid);
378                 if (ret < 0)
379                         return ret;
380
381                 desc->mode = ret;
382
383                 alt = typec_partner_register_altmode(con->partner, desc);
384                 if (IS_ERR(alt)) {
385                         ret = PTR_ERR(alt);
386                         goto err;
387                 }
388
389                 con->partner_altmode[i] = alt;
390                 break;
391         default:
392                 return -EINVAL;
393         }
394
395         trace_ucsi_register_altmode(recipient, alt);
396
397         return 0;
398
399 err:
400         dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n",
401                 desc->svid, desc->mode);
402
403         return ret;
404 }
405
406 static int
407 ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient)
408 {
409         int max_altmodes = UCSI_MAX_ALTMODES;
410         struct typec_altmode_desc desc;
411         struct ucsi_altmode alt;
412         struct ucsi_altmode orig[UCSI_MAX_ALTMODES];
413         struct ucsi_altmode updated[UCSI_MAX_ALTMODES];
414         struct ucsi *ucsi = con->ucsi;
415         bool multi_dp = false;
416         u64 command;
417         int ret;
418         int len;
419         int i;
420         int k = 0;
421
422         if (recipient == UCSI_RECIPIENT_CON)
423                 max_altmodes = con->ucsi->cap.num_alt_modes;
424
425         memset(orig, 0, sizeof(orig));
426         memset(updated, 0, sizeof(updated));
427
428         /* First get all the alternate modes */
429         for (i = 0; i < max_altmodes; i++) {
430                 memset(&alt, 0, sizeof(alt));
431                 command = UCSI_GET_ALTERNATE_MODES;
432                 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
433                 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
434                 command |= UCSI_GET_ALTMODE_OFFSET(i);
435                 len = ucsi_send_command(con->ucsi, command, &alt, sizeof(alt));
436                 /*
437                  * We are collecting all altmodes first and then registering.
438                  * Some type-C device will return zero length data beyond last
439                  * alternate modes. We should not return if length is zero.
440                  */
441                 if (len < 0)
442                         return len;
443
444                 /* We got all altmodes, now break out and register them */
445                 if (!len || !alt.svid)
446                         break;
447
448                 orig[k].mid = alt.mid;
449                 orig[k].svid = alt.svid;
450                 k++;
451         }
452         /*
453          * Update the original altmode table as some ppms may report
454          * multiple DP altmodes.
455          */
456         if (recipient == UCSI_RECIPIENT_CON)
457                 multi_dp = ucsi->ops->update_altmodes(ucsi, orig, updated);
458
459         /* now register altmodes */
460         for (i = 0; i < max_altmodes; i++) {
461                 memset(&desc, 0, sizeof(desc));
462                 if (multi_dp && recipient == UCSI_RECIPIENT_CON) {
463                         desc.svid = updated[i].svid;
464                         desc.vdo = updated[i].mid;
465                 } else {
466                         desc.svid = orig[i].svid;
467                         desc.vdo = orig[i].mid;
468                 }
469                 desc.roles = TYPEC_PORT_DRD;
470
471                 if (!desc.svid)
472                         return 0;
473
474                 ret = ucsi_register_altmode(con, &desc, recipient);
475                 if (ret)
476                         return ret;
477         }
478
479         return 0;
480 }
481
482 static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
483 {
484         int max_altmodes = UCSI_MAX_ALTMODES;
485         struct typec_altmode_desc desc;
486         struct ucsi_altmode alt[2];
487         u64 command;
488         int num;
489         int ret;
490         int len;
491         int j;
492         int i;
493
494         if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
495                 return 0;
496
497         if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
498                 return 0;
499
500         if (con->ucsi->ops->update_altmodes)
501                 return ucsi_register_altmodes_nvidia(con, recipient);
502
503         if (recipient == UCSI_RECIPIENT_CON)
504                 max_altmodes = con->ucsi->cap.num_alt_modes;
505
506         for (i = 0; i < max_altmodes;) {
507                 memset(alt, 0, sizeof(alt));
508                 command = UCSI_GET_ALTERNATE_MODES;
509                 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
510                 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
511                 command |= UCSI_GET_ALTMODE_OFFSET(i);
512                 len = ucsi_send_command(con->ucsi, command, alt, sizeof(alt));
513                 if (len == -EBUSY)
514                         continue;
515                 if (len <= 0)
516                         return len;
517
518                 /*
519                  * This code is requesting one alt mode at a time, but some PPMs
520                  * may still return two. If that happens both alt modes need be
521                  * registered and the offset for the next alt mode has to be
522                  * incremented.
523                  */
524                 num = len / sizeof(alt[0]);
525                 i += num;
526
527                 for (j = 0; j < num; j++) {
528                         if (!alt[j].svid)
529                                 return 0;
530
531                         memset(&desc, 0, sizeof(desc));
532                         desc.vdo = alt[j].mid;
533                         desc.svid = alt[j].svid;
534                         desc.roles = TYPEC_PORT_DRD;
535
536                         ret = ucsi_register_altmode(con, &desc, recipient);
537                         if (ret)
538                                 return ret;
539                 }
540         }
541
542         return 0;
543 }
544
545 static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
546 {
547         const struct typec_altmode *pdev;
548         struct typec_altmode **adev;
549         int i = 0;
550
551         switch (recipient) {
552         case UCSI_RECIPIENT_CON:
553                 adev = con->port_altmode;
554                 break;
555         case UCSI_RECIPIENT_SOP:
556                 adev = con->partner_altmode;
557                 break;
558         default:
559                 return;
560         }
561
562         while (adev[i]) {
563                 if (recipient == UCSI_RECIPIENT_SOP &&
564                     (adev[i]->svid == USB_TYPEC_DP_SID ||
565                         (adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID &&
566                         adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) {
567                         pdev = typec_altmode_get_partner(adev[i]);
568                         ucsi_displayport_remove_partner((void *)pdev);
569                 }
570                 typec_unregister_altmode(adev[i]);
571                 adev[i++] = NULL;
572         }
573 }
574
575 static int ucsi_get_pdos(struct ucsi_connector *con, int is_partner,
576                          u32 *pdos, int offset, int num_pdos)
577 {
578         struct ucsi *ucsi = con->ucsi;
579         u64 command;
580         int ret;
581
582         command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num);
583         command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner);
584         command |= UCSI_GET_PDOS_PDO_OFFSET(offset);
585         command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1);
586         command |= UCSI_GET_PDOS_SRC_PDOS;
587         ret = ucsi_send_command(ucsi, command, pdos + offset,
588                                 num_pdos * sizeof(u32));
589         if (ret < 0 && ret != -ETIMEDOUT)
590                 dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret);
591
592         return ret;
593 }
594
595 static int ucsi_get_src_pdos(struct ucsi_connector *con)
596 {
597         int ret;
598
599         /* UCSI max payload means only getting at most 4 PDOs at a time */
600         ret = ucsi_get_pdos(con, 1, con->src_pdos, 0, UCSI_MAX_PDOS);
601         if (ret < 0)
602                 return ret;
603
604         con->num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */
605         if (con->num_pdos < UCSI_MAX_PDOS)
606                 return 0;
607
608         /* get the remaining PDOs, if any */
609         ret = ucsi_get_pdos(con, 1, con->src_pdos, UCSI_MAX_PDOS,
610                             PDO_MAX_OBJECTS - UCSI_MAX_PDOS);
611         if (ret < 0)
612                 return ret;
613
614         con->num_pdos += ret / sizeof(u32);
615
616         ucsi_port_psy_changed(con);
617
618         return 0;
619 }
620
621 static int ucsi_check_altmodes(struct ucsi_connector *con)
622 {
623         int ret, num_partner_am;
624
625         ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
626         if (ret && ret != -ETIMEDOUT)
627                 dev_err(con->ucsi->dev,
628                         "con%d: failed to register partner alt modes (%d)\n",
629                         con->num, ret);
630
631         /* Ignoring the errors in this case. */
632         if (con->partner_altmode[0]) {
633                 num_partner_am = ucsi_get_num_altmode(con->partner_altmode);
634                 if (num_partner_am > 0)
635                         typec_partner_set_num_altmodes(con->partner, num_partner_am);
636                 ucsi_altmode_update_active(con);
637                 return 0;
638         }
639
640         return ret;
641 }
642
643 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
644 {
645         switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
646         case UCSI_CONSTAT_PWR_OPMODE_PD:
647                 con->rdo = con->status.request_data_obj;
648                 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
649                 ucsi_partner_task(con, ucsi_get_src_pdos, 30, 0);
650                 ucsi_partner_task(con, ucsi_check_altmodes, 30, 0);
651                 break;
652         case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
653                 con->rdo = 0;
654                 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
655                 break;
656         case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
657                 con->rdo = 0;
658                 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
659                 break;
660         default:
661                 con->rdo = 0;
662                 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
663                 break;
664         }
665 }
666
667 static int ucsi_register_partner(struct ucsi_connector *con)
668 {
669         u8 pwr_opmode = UCSI_CONSTAT_PWR_OPMODE(con->status.flags);
670         struct typec_partner_desc desc;
671         struct typec_partner *partner;
672
673         if (con->partner)
674                 return 0;
675
676         memset(&desc, 0, sizeof(desc));
677
678         switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
679         case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
680                 desc.accessory = TYPEC_ACCESSORY_DEBUG;
681                 break;
682         case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
683                 desc.accessory = TYPEC_ACCESSORY_AUDIO;
684                 break;
685         default:
686                 break;
687         }
688
689         desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD;
690
691         partner = typec_register_partner(con->port, &desc);
692         if (IS_ERR(partner)) {
693                 dev_err(con->ucsi->dev,
694                         "con%d: failed to register partner (%ld)\n", con->num,
695                         PTR_ERR(partner));
696                 return PTR_ERR(partner);
697         }
698
699         con->partner = partner;
700
701         return 0;
702 }
703
704 static void ucsi_unregister_partner(struct ucsi_connector *con)
705 {
706         if (!con->partner)
707                 return;
708
709         ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
710         typec_unregister_partner(con->partner);
711         con->partner = NULL;
712 }
713
714 static void ucsi_partner_change(struct ucsi_connector *con)
715 {
716         enum usb_role u_role = USB_ROLE_NONE;
717         int ret;
718
719         switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
720         case UCSI_CONSTAT_PARTNER_TYPE_UFP:
721         case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
722                 u_role = USB_ROLE_HOST;
723                 fallthrough;
724         case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
725                 typec_set_data_role(con->port, TYPEC_HOST);
726                 break;
727         case UCSI_CONSTAT_PARTNER_TYPE_DFP:
728                 u_role = USB_ROLE_DEVICE;
729                 typec_set_data_role(con->port, TYPEC_DEVICE);
730                 break;
731         default:
732                 break;
733         }
734
735         /* Only notify USB controller if partner supports USB data */
736         if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
737                 u_role = USB_ROLE_NONE;
738
739         ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
740         if (ret)
741                 dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n",
742                         con->num, u_role);
743 }
744
745 static int ucsi_check_connection(struct ucsi_connector *con)
746 {
747         u64 command;
748         int ret;
749
750         command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
751         ret = ucsi_send_command(con->ucsi, command, &con->status, sizeof(con->status));
752         if (ret < 0) {
753                 dev_err(con->ucsi->dev, "GET_CONNECTOR_STATUS failed (%d)\n", ret);
754                 return ret;
755         }
756
757         if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
758                 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) ==
759                     UCSI_CONSTAT_PWR_OPMODE_PD)
760                         ucsi_partner_task(con, ucsi_check_altmodes, 30, 0);
761         } else {
762                 ucsi_partner_change(con);
763                 ucsi_port_psy_changed(con);
764                 ucsi_unregister_partner(con);
765         }
766
767         return 0;
768 }
769
770 static void ucsi_handle_connector_change(struct work_struct *work)
771 {
772         struct ucsi_connector *con = container_of(work, struct ucsi_connector,
773                                                   work);
774         struct ucsi *ucsi = con->ucsi;
775         enum typec_role role;
776         u64 command;
777         int ret;
778
779         mutex_lock(&con->lock);
780
781         command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
782         ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
783         if (ret < 0) {
784                 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
785                         __func__, ret);
786                 goto out_unlock;
787         }
788
789         trace_ucsi_connector_change(con->num, &con->status);
790
791         role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
792
793         if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
794                 typec_set_pwr_role(con->port, role);
795
796                 /* Complete pending power role swap */
797                 if (!completion_done(&con->complete))
798                         complete(&con->complete);
799         }
800
801         if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
802                 typec_set_pwr_role(con->port, role);
803                 ucsi_port_psy_changed(con);
804                 ucsi_partner_change(con);
805
806                 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
807                         ucsi_register_partner(con);
808                         ucsi_partner_task(con, ucsi_check_connection, 1, HZ);
809                 } else {
810                         ucsi_unregister_partner(con);
811                 }
812         }
813
814         if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE ||
815             con->status.change & UCSI_CONSTAT_POWER_LEVEL_CHANGE)
816                 ucsi_pwr_opmode_change(con);
817
818         if (con->partner && con->status.change & UCSI_CONSTAT_PARTNER_CHANGE) {
819                 ucsi_partner_change(con);
820
821                 /* Complete pending data role swap */
822                 if (!completion_done(&con->complete))
823                         complete(&con->complete);
824         }
825
826         if (con->status.change & UCSI_CONSTAT_CAM_CHANGE)
827                 ucsi_partner_task(con, ucsi_check_altmodes, 1, 0);
828
829         clear_bit(EVENT_PENDING, &con->ucsi->flags);
830
831         ret = ucsi_acknowledge_connector_change(ucsi);
832         if (ret)
833                 dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret);
834
835 out_unlock:
836         mutex_unlock(&con->lock);
837 }
838
839 /**
840  * ucsi_connector_change - Process Connector Change Event
841  * @ucsi: UCSI Interface
842  * @num: Connector number
843  */
844 void ucsi_connector_change(struct ucsi *ucsi, u8 num)
845 {
846         struct ucsi_connector *con = &ucsi->connector[num - 1];
847
848         if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
849                 dev_dbg(ucsi->dev, "Bogus connector change event\n");
850                 return;
851         }
852
853         if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
854                 schedule_work(&con->work);
855 }
856 EXPORT_SYMBOL_GPL(ucsi_connector_change);
857
858 /* -------------------------------------------------------------------------- */
859
860 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
861 {
862         u64 command;
863
864         command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num);
865         command |= hard ? UCSI_CONNECTOR_RESET_HARD : 0;
866
867         return ucsi_send_command(con->ucsi, command, NULL, 0);
868 }
869
870 static int ucsi_reset_ppm(struct ucsi *ucsi)
871 {
872         u64 command = UCSI_PPM_RESET;
873         unsigned long tmo;
874         u32 cci;
875         int ret;
876
877         mutex_lock(&ucsi->ppm_lock);
878
879         ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command,
880                                      sizeof(command));
881         if (ret < 0)
882                 goto out;
883
884         tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
885
886         do {
887                 if (time_is_before_jiffies(tmo)) {
888                         ret = -ETIMEDOUT;
889                         goto out;
890                 }
891
892                 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
893                 if (ret)
894                         goto out;
895
896                 /* If the PPM is still doing something else, reset it again. */
897                 if (cci & ~UCSI_CCI_RESET_COMPLETE) {
898                         ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL,
899                                                      &command,
900                                                      sizeof(command));
901                         if (ret < 0)
902                                 goto out;
903                 }
904
905                 msleep(20);
906         } while (!(cci & UCSI_CCI_RESET_COMPLETE));
907
908 out:
909         mutex_unlock(&ucsi->ppm_lock);
910         return ret;
911 }
912
913 static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)
914 {
915         int ret;
916
917         ret = ucsi_send_command(con->ucsi, command, NULL, 0);
918         if (ret == -ETIMEDOUT) {
919                 u64 c;
920
921                 /* PPM most likely stopped responding. Resetting everything. */
922                 ucsi_reset_ppm(con->ucsi);
923
924                 c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
925                 ucsi_send_command(con->ucsi, c, NULL, 0);
926
927                 ucsi_reset_connector(con, true);
928         }
929
930         return ret;
931 }
932
933 static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
934 {
935         struct ucsi_connector *con = typec_get_drvdata(port);
936         u8 partner_type;
937         u64 command;
938         int ret = 0;
939
940         mutex_lock(&con->lock);
941
942         if (!con->partner) {
943                 ret = -ENOTCONN;
944                 goto out_unlock;
945         }
946
947         partner_type = UCSI_CONSTAT_PARTNER_TYPE(con->status.flags);
948         if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
949              role == TYPEC_DEVICE) ||
950             (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
951              role == TYPEC_HOST))
952                 goto out_unlock;
953
954         reinit_completion(&con->complete);
955
956         command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num);
957         command |= UCSI_SET_UOR_ROLE(role);
958         command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS;
959         ret = ucsi_role_cmd(con, command);
960         if (ret < 0)
961                 goto out_unlock;
962
963         mutex_unlock(&con->lock);
964
965         if (!wait_for_completion_timeout(&con->complete,
966                                          msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
967                 return -ETIMEDOUT;
968
969         return 0;
970
971 out_unlock:
972         mutex_unlock(&con->lock);
973
974         return ret;
975 }
976
977 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
978 {
979         struct ucsi_connector *con = typec_get_drvdata(port);
980         enum typec_role cur_role;
981         u64 command;
982         int ret = 0;
983
984         mutex_lock(&con->lock);
985
986         if (!con->partner) {
987                 ret = -ENOTCONN;
988                 goto out_unlock;
989         }
990
991         cur_role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
992
993         if (cur_role == role)
994                 goto out_unlock;
995
996         reinit_completion(&con->complete);
997
998         command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num);
999         command |= UCSI_SET_PDR_ROLE(role);
1000         command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS;
1001         ret = ucsi_role_cmd(con, command);
1002         if (ret < 0)
1003                 goto out_unlock;
1004
1005         mutex_unlock(&con->lock);
1006
1007         if (!wait_for_completion_timeout(&con->complete,
1008                                          msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1009                 return -ETIMEDOUT;
1010
1011         mutex_lock(&con->lock);
1012
1013         /* Something has gone wrong while swapping the role */
1014         if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) !=
1015             UCSI_CONSTAT_PWR_OPMODE_PD) {
1016                 ucsi_reset_connector(con, true);
1017                 ret = -EPROTO;
1018         }
1019
1020 out_unlock:
1021         mutex_unlock(&con->lock);
1022
1023         return ret;
1024 }
1025
1026 static const struct typec_operations ucsi_ops = {
1027         .dr_set = ucsi_dr_swap,
1028         .pr_set = ucsi_pr_swap
1029 };
1030
1031 /* Caller must call fwnode_handle_put() after use */
1032 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
1033 {
1034         struct fwnode_handle *fwnode;
1035         int i = 1;
1036
1037         device_for_each_child_node(con->ucsi->dev, fwnode)
1038                 if (i++ == con->num)
1039                         return fwnode;
1040         return NULL;
1041 }
1042
1043 static int ucsi_register_port(struct ucsi *ucsi, int index)
1044 {
1045         struct ucsi_connector *con = &ucsi->connector[index];
1046         struct typec_capability *cap = &con->typec_cap;
1047         enum typec_accessory *accessory = cap->accessory;
1048         enum usb_role u_role = USB_ROLE_NONE;
1049         u64 command;
1050         char *name;
1051         int ret;
1052
1053         name = kasprintf(GFP_KERNEL, "%s-con%d", dev_name(ucsi->dev), con->num);
1054         if (!name)
1055                 return -ENOMEM;
1056
1057         con->wq = create_singlethread_workqueue(name);
1058         kfree(name);
1059         if (!con->wq)
1060                 return -ENOMEM;
1061
1062         INIT_WORK(&con->work, ucsi_handle_connector_change);
1063         init_completion(&con->complete);
1064         mutex_init(&con->lock);
1065         con->num = index + 1;
1066         con->ucsi = ucsi;
1067
1068         cap->fwnode = ucsi_find_fwnode(con);
1069         con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode);
1070         if (IS_ERR(con->usb_role_sw))
1071                 return dev_err_probe(ucsi->dev, PTR_ERR(con->usb_role_sw),
1072                         "con%d: failed to get usb role switch\n", con->num);
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             UCSI_CONSTAT_PWR_OPMODE(con->status.flags) ==
1179             UCSI_CONSTAT_PWR_OPMODE_PD) {
1180                 ucsi_get_src_pdos(con);
1181                 ucsi_check_altmodes(con);
1182         }
1183
1184         trace_ucsi_register_port(con->num, &con->status);
1185
1186 out:
1187         fwnode_handle_put(cap->fwnode);
1188 out_unlock:
1189         mutex_unlock(&con->lock);
1190
1191         if (ret && con->wq) {
1192                 destroy_workqueue(con->wq);
1193                 con->wq = NULL;
1194         }
1195
1196         return ret;
1197 }
1198
1199 /**
1200  * ucsi_init - Initialize UCSI interface
1201  * @ucsi: UCSI to be initialized
1202  *
1203  * Registers all ports @ucsi has and enables all notification events.
1204  */
1205 static int ucsi_init(struct ucsi *ucsi)
1206 {
1207         struct ucsi_connector *con;
1208         u64 command;
1209         int ret;
1210         int i;
1211
1212         /* Reset the PPM */
1213         ret = ucsi_reset_ppm(ucsi);
1214         if (ret) {
1215                 dev_err(ucsi->dev, "failed to reset PPM!\n");
1216                 goto err;
1217         }
1218
1219         /* Enable basic notifications */
1220         ucsi->ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
1221         command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1222         ret = ucsi_send_command(ucsi, command, NULL, 0);
1223         if (ret < 0)
1224                 goto err_reset;
1225
1226         /* Get PPM capabilities */
1227         command = UCSI_GET_CAPABILITY;
1228         ret = ucsi_send_command(ucsi, command, &ucsi->cap, sizeof(ucsi->cap));
1229         if (ret < 0)
1230                 goto err_reset;
1231
1232         if (!ucsi->cap.num_connectors) {
1233                 ret = -ENODEV;
1234                 goto err_reset;
1235         }
1236
1237         /* Allocate the connectors. Released in ucsi_unregister() */
1238         ucsi->connector = kcalloc(ucsi->cap.num_connectors + 1,
1239                                   sizeof(*ucsi->connector), GFP_KERNEL);
1240         if (!ucsi->connector) {
1241                 ret = -ENOMEM;
1242                 goto err_reset;
1243         }
1244
1245         /* Register all connectors */
1246         for (i = 0; i < ucsi->cap.num_connectors; i++) {
1247                 ret = ucsi_register_port(ucsi, i);
1248                 if (ret)
1249                         goto err_unregister;
1250         }
1251
1252         /* Enable all notifications */
1253         ucsi->ntfy = UCSI_ENABLE_NTFY_ALL;
1254         command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1255         ret = ucsi_send_command(ucsi, command, NULL, 0);
1256         if (ret < 0)
1257                 goto err_unregister;
1258
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                 if (con->wq)
1267                         destroy_workqueue(con->wq);
1268                 typec_unregister_port(con->port);
1269                 con->port = NULL;
1270         }
1271
1272 err_reset:
1273         memset(&ucsi->cap, 0, sizeof(ucsi->cap));
1274         ucsi_reset_ppm(ucsi);
1275 err:
1276         return ret;
1277 }
1278
1279 static void ucsi_init_work(struct work_struct *work)
1280 {
1281         struct ucsi *ucsi = container_of(work, struct ucsi, work.work);
1282         int ret;
1283
1284         ret = ucsi_init(ucsi);
1285         if (ret)
1286                 dev_err(ucsi->dev, "PPM init failed (%d)\n", ret);
1287
1288         if (ret == -EPROBE_DEFER) {
1289                 if (ucsi->work_count++ > UCSI_ROLE_SWITCH_WAIT_COUNT)
1290                         return;
1291
1292                 queue_delayed_work(system_long_wq, &ucsi->work,
1293                                    UCSI_ROLE_SWITCH_INTERVAL);
1294         }
1295 }
1296
1297 /**
1298  * ucsi_get_drvdata - Return private driver data pointer
1299  * @ucsi: UCSI interface
1300  */
1301 void *ucsi_get_drvdata(struct ucsi *ucsi)
1302 {
1303         return ucsi->driver_data;
1304 }
1305 EXPORT_SYMBOL_GPL(ucsi_get_drvdata);
1306
1307 /**
1308  * ucsi_set_drvdata - Assign private driver data pointer
1309  * @ucsi: UCSI interface
1310  * @data: Private data pointer
1311  */
1312 void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
1313 {
1314         ucsi->driver_data = data;
1315 }
1316 EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
1317
1318 /**
1319  * ucsi_create - Allocate UCSI instance
1320  * @dev: Device interface to the PPM (Platform Policy Manager)
1321  * @ops: I/O routines
1322  */
1323 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
1324 {
1325         struct ucsi *ucsi;
1326
1327         if (!ops || !ops->read || !ops->sync_write || !ops->async_write)
1328                 return ERR_PTR(-EINVAL);
1329
1330         ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
1331         if (!ucsi)
1332                 return ERR_PTR(-ENOMEM);
1333
1334         INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work);
1335         mutex_init(&ucsi->ppm_lock);
1336         ucsi->dev = dev;
1337         ucsi->ops = ops;
1338
1339         return ucsi;
1340 }
1341 EXPORT_SYMBOL_GPL(ucsi_create);
1342
1343 /**
1344  * ucsi_destroy - Free UCSI instance
1345  * @ucsi: UCSI instance to be freed
1346  */
1347 void ucsi_destroy(struct ucsi *ucsi)
1348 {
1349         kfree(ucsi);
1350 }
1351 EXPORT_SYMBOL_GPL(ucsi_destroy);
1352
1353 /**
1354  * ucsi_register - Register UCSI interface
1355  * @ucsi: UCSI instance
1356  */
1357 int ucsi_register(struct ucsi *ucsi)
1358 {
1359         int ret;
1360
1361         ret = ucsi->ops->read(ucsi, UCSI_VERSION, &ucsi->version,
1362                               sizeof(ucsi->version));
1363         if (ret)
1364                 return ret;
1365
1366         if (!ucsi->version)
1367                 return -ENODEV;
1368
1369         queue_delayed_work(system_long_wq, &ucsi->work, 0);
1370
1371         return 0;
1372 }
1373 EXPORT_SYMBOL_GPL(ucsi_register);
1374
1375 /**
1376  * ucsi_unregister - Unregister UCSI interface
1377  * @ucsi: UCSI interface to be unregistered
1378  *
1379  * Unregister UCSI interface that was created with ucsi_register().
1380  */
1381 void ucsi_unregister(struct ucsi *ucsi)
1382 {
1383         u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
1384         int i;
1385
1386         /* Make sure that we are not in the middle of driver initialization */
1387         cancel_delayed_work_sync(&ucsi->work);
1388
1389         /* Disable notifications */
1390         ucsi->ops->async_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
1391
1392         for (i = 0; i < ucsi->cap.num_connectors; i++) {
1393                 cancel_work_sync(&ucsi->connector[i].work);
1394                 ucsi_unregister_partner(&ucsi->connector[i]);
1395                 ucsi_unregister_altmodes(&ucsi->connector[i],
1396                                          UCSI_RECIPIENT_CON);
1397                 ucsi_unregister_port_psy(&ucsi->connector[i]);
1398                 if (ucsi->connector[i].wq)
1399                         destroy_workqueue(ucsi->connector[i].wq);
1400                 typec_unregister_port(ucsi->connector[i].port);
1401         }
1402
1403         kfree(ucsi->connector);
1404 }
1405 EXPORT_SYMBOL_GPL(ucsi_unregister);
1406
1407 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1408 MODULE_LICENSE("GPL v2");
1409 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");