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