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