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