GNU Linux-libre 4.14.253-gnu1
[releases.git] / drivers / usb / typec / ucsi / ucsi.c
1 /*
2  * USB Type-C Connector System Software Interface driver
3  *
4  * Copyright (C) 2017, Intel Corporation
5  * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 #include <linux/completion.h>
13 #include <linux/property.h>
14 #include <linux/device.h>
15 #include <linux/module.h>
16 #include <linux/delay.h>
17 #include <linux/slab.h>
18 #include <linux/usb/typec.h>
19
20 #include "ucsi.h"
21 #include "trace.h"
22
23 #define to_ucsi_connector(_cap_) container_of(_cap_, struct ucsi_connector, \
24                                               typec_cap)
25
26 /*
27  * UCSI_TIMEOUT_MS - PPM communication timeout
28  *
29  * Ideally we could use MIN_TIME_TO_RESPOND_WITH_BUSY (which is defined in UCSI
30  * specification) here as reference, but unfortunately we can't. It is very
31  * difficult to estimate the time it takes for the system to process the command
32  * before it is actually passed to the PPM.
33  */
34 #define UCSI_TIMEOUT_MS         5000
35
36 /*
37  * UCSI_SWAP_TIMEOUT_MS - Timeout for role swap requests
38  *
39  * 5 seconds is close to the time it takes for CapsCounter to reach 0, so even
40  * if the PPM does not generate Connector Change events before that with
41  * partners that do not support USB Power Delivery, this should still work.
42  */
43 #define UCSI_SWAP_TIMEOUT_MS    5000
44
45 enum ucsi_status {
46         UCSI_IDLE = 0,
47         UCSI_BUSY,
48         UCSI_ERROR,
49 };
50
51 struct ucsi_connector {
52         int num;
53
54         struct ucsi *ucsi;
55         struct work_struct work;
56         struct completion complete;
57
58         struct typec_port *port;
59         struct typec_partner *partner;
60
61         struct typec_capability typec_cap;
62
63         struct ucsi_connector_status status;
64         struct ucsi_connector_capability cap;
65 };
66
67 struct ucsi {
68         struct device *dev;
69         struct ucsi_ppm *ppm;
70
71         enum ucsi_status status;
72         struct completion complete;
73         struct ucsi_capability cap;
74         struct ucsi_connector *connector;
75
76         struct work_struct work;
77
78         /* PPM Communication lock */
79         struct mutex ppm_lock;
80
81         /* PPM communication flags */
82         unsigned long flags;
83 #define EVENT_PENDING   0
84 #define COMMAND_PENDING 1
85 #define ACK_PENDING     2
86 };
87
88 static inline int ucsi_sync(struct ucsi *ucsi)
89 {
90         if (ucsi->ppm && ucsi->ppm->sync)
91                 return ucsi->ppm->sync(ucsi->ppm);
92         return 0;
93 }
94
95 static int ucsi_command(struct ucsi *ucsi, struct ucsi_control *ctrl)
96 {
97         int ret;
98
99         trace_ucsi_command(ctrl);
100
101         set_bit(COMMAND_PENDING, &ucsi->flags);
102
103         ret = ucsi->ppm->cmd(ucsi->ppm, ctrl);
104         if (ret)
105                 goto err_clear_flag;
106
107         if (!wait_for_completion_timeout(&ucsi->complete,
108                                          msecs_to_jiffies(UCSI_TIMEOUT_MS))) {
109                 dev_warn(ucsi->dev, "PPM NOT RESPONDING\n");
110                 ret = -ETIMEDOUT;
111         }
112
113 err_clear_flag:
114         clear_bit(COMMAND_PENDING, &ucsi->flags);
115
116         return ret;
117 }
118
119 static int ucsi_ack(struct ucsi *ucsi, u8 ack)
120 {
121         struct ucsi_control ctrl;
122         int ret;
123
124         trace_ucsi_ack(ack);
125
126         set_bit(ACK_PENDING, &ucsi->flags);
127
128         UCSI_CMD_ACK(ctrl, ack);
129         ret = ucsi->ppm->cmd(ucsi->ppm, &ctrl);
130         if (ret)
131                 goto out_clear_bit;
132
133         /* Waiting for ACK with ACK CMD, but not with EVENT for now */
134         if (ack == UCSI_ACK_EVENT)
135                 goto out_clear_bit;
136
137         if (!wait_for_completion_timeout(&ucsi->complete,
138                                          msecs_to_jiffies(UCSI_TIMEOUT_MS)))
139                 ret = -ETIMEDOUT;
140
141 out_clear_bit:
142         clear_bit(ACK_PENDING, &ucsi->flags);
143
144         if (ret)
145                 dev_err(ucsi->dev, "%s: failed\n", __func__);
146
147         return ret;
148 }
149
150 static int ucsi_run_command(struct ucsi *ucsi, struct ucsi_control *ctrl,
151                             void *data, size_t size)
152 {
153         struct ucsi_control _ctrl;
154         u8 data_length;
155         u16 error;
156         int ret;
157
158         ret = ucsi_command(ucsi, ctrl);
159         if (ret)
160                 goto err;
161
162         switch (ucsi->status) {
163         case UCSI_IDLE:
164                 ret = ucsi_sync(ucsi);
165                 if (ret)
166                         dev_warn(ucsi->dev, "%s: sync failed\n", __func__);
167
168                 if (data)
169                         memcpy(data, ucsi->ppm->data->message_in, size);
170
171                 data_length = ucsi->ppm->data->cci.data_length;
172
173                 ret = ucsi_ack(ucsi, UCSI_ACK_CMD);
174                 if (!ret)
175                         ret = data_length;
176                 break;
177         case UCSI_BUSY:
178                 /* The caller decides whether to cancel or not */
179                 ret = -EBUSY;
180                 break;
181         case UCSI_ERROR:
182                 ret = ucsi_ack(ucsi, UCSI_ACK_CMD);
183                 if (ret)
184                         break;
185
186                 _ctrl.raw_cmd = 0;
187                 _ctrl.cmd.cmd = UCSI_GET_ERROR_STATUS;
188                 ret = ucsi_command(ucsi, &_ctrl);
189                 if (ret) {
190                         dev_err(ucsi->dev, "reading error failed!\n");
191                         break;
192                 }
193
194                 memcpy(&error, ucsi->ppm->data->message_in, sizeof(error));
195
196                 /* Something has really gone wrong */
197                 if (WARN_ON(ucsi->status == UCSI_ERROR)) {
198                         ret = -ENODEV;
199                         break;
200                 }
201
202                 ret = ucsi_ack(ucsi, UCSI_ACK_CMD);
203                 if (ret)
204                         break;
205
206                 switch (error) {
207                 case UCSI_ERROR_INCOMPATIBLE_PARTNER:
208                         ret = -EOPNOTSUPP;
209                         break;
210                 case UCSI_ERROR_CC_COMMUNICATION_ERR:
211                         ret = -ECOMM;
212                         break;
213                 case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
214                         ret = -EPROTO;
215                         break;
216                 case UCSI_ERROR_DEAD_BATTERY:
217                         dev_warn(ucsi->dev, "Dead battery condition!\n");
218                         ret = -EPERM;
219                         break;
220                 /* The following mean a bug in this driver */
221                 case UCSI_ERROR_INVALID_CON_NUM:
222                 case UCSI_ERROR_UNREGONIZED_CMD:
223                 case UCSI_ERROR_INVALID_CMD_ARGUMENT:
224                         dev_warn(ucsi->dev,
225                                  "%s: possible UCSI driver bug - error 0x%x\n",
226                                  __func__, error);
227                         ret = -EINVAL;
228                         break;
229                 default:
230                         dev_warn(ucsi->dev,
231                                  "%s: error without status\n", __func__);
232                         ret = -EIO;
233                         break;
234                 }
235                 break;
236         }
237
238 err:
239         trace_ucsi_run_command(ctrl, ret);
240
241         return ret;
242 }
243
244 /* -------------------------------------------------------------------------- */
245
246 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
247 {
248         switch (con->status.pwr_op_mode) {
249         case UCSI_CONSTAT_PWR_OPMODE_PD:
250                 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
251                 break;
252         case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
253                 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
254                 break;
255         case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
256                 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
257                 break;
258         default:
259                 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
260                 break;
261         }
262 }
263
264 static int ucsi_register_partner(struct ucsi_connector *con)
265 {
266         struct typec_partner_desc partner;
267
268         if (con->partner)
269                 return 0;
270
271         memset(&partner, 0, sizeof(partner));
272
273         switch (con->status.partner_type) {
274         case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
275                 partner.accessory = TYPEC_ACCESSORY_DEBUG;
276                 break;
277         case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
278                 partner.accessory = TYPEC_ACCESSORY_AUDIO;
279                 break;
280         default:
281                 break;
282         }
283
284         partner.usb_pd = con->status.pwr_op_mode == UCSI_CONSTAT_PWR_OPMODE_PD;
285
286         con->partner = typec_register_partner(con->port, &partner);
287         if (!con->partner) {
288                 dev_err(con->ucsi->dev, "con%d: failed to register partner\n",
289                         con->num);
290                 return -ENODEV;
291         }
292
293         return 0;
294 }
295
296 static void ucsi_unregister_partner(struct ucsi_connector *con)
297 {
298         typec_unregister_partner(con->partner);
299         con->partner = NULL;
300 }
301
302 static void ucsi_connector_change(struct work_struct *work)
303 {
304         struct ucsi_connector *con = container_of(work, struct ucsi_connector,
305                                                   work);
306         struct ucsi *ucsi = con->ucsi;
307         struct ucsi_control ctrl;
308         int ret;
309
310         mutex_lock(&ucsi->ppm_lock);
311
312         UCSI_CMD_GET_CONNECTOR_STATUS(ctrl, con->num);
313         ret = ucsi_run_command(ucsi, &ctrl, &con->status, sizeof(con->status));
314         if (ret < 0) {
315                 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
316                         __func__, ret);
317                 goto out_unlock;
318         }
319
320         if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE)
321                 ucsi_pwr_opmode_change(con);
322
323         if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
324                 typec_set_pwr_role(con->port, con->status.pwr_dir);
325
326                 /* Complete pending power role swap */
327                 if (!completion_done(&con->complete))
328                         complete(&con->complete);
329         }
330
331         if (con->status.change & UCSI_CONSTAT_PARTNER_CHANGE) {
332                 switch (con->status.partner_type) {
333                 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
334                         typec_set_data_role(con->port, TYPEC_HOST);
335                         break;
336                 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
337                         typec_set_data_role(con->port, TYPEC_DEVICE);
338                         break;
339                 default:
340                         break;
341                 }
342
343                 /* Complete pending data role swap */
344                 if (!completion_done(&con->complete))
345                         complete(&con->complete);
346         }
347
348         if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
349                 typec_set_pwr_role(con->port, con->status.pwr_dir);
350
351                 switch (con->status.partner_type) {
352                 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
353                         typec_set_data_role(con->port, TYPEC_HOST);
354                         break;
355                 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
356                         typec_set_data_role(con->port, TYPEC_DEVICE);
357                         break;
358                 default:
359                         break;
360                 }
361
362                 if (con->status.connected)
363                         ucsi_register_partner(con);
364                 else
365                         ucsi_unregister_partner(con);
366         }
367
368         ret = ucsi_ack(ucsi, UCSI_ACK_EVENT);
369         if (ret)
370                 dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret);
371
372         trace_ucsi_connector_change(con->num, &con->status);
373
374 out_unlock:
375         clear_bit(EVENT_PENDING, &ucsi->flags);
376         mutex_unlock(&ucsi->ppm_lock);
377 }
378
379 /**
380  * ucsi_notify - PPM notification handler
381  * @ucsi: Source UCSI Interface for the notifications
382  *
383  * Handle notifications from PPM of @ucsi.
384  */
385 void ucsi_notify(struct ucsi *ucsi)
386 {
387         struct ucsi_cci *cci;
388
389         /* There is no requirement to sync here, but no harm either. */
390         ucsi_sync(ucsi);
391
392         cci = &ucsi->ppm->data->cci;
393
394         if (cci->error)
395                 ucsi->status = UCSI_ERROR;
396         else if (cci->busy)
397                 ucsi->status = UCSI_BUSY;
398         else
399                 ucsi->status = UCSI_IDLE;
400
401         if (cci->cmd_complete && test_bit(COMMAND_PENDING, &ucsi->flags)) {
402                 complete(&ucsi->complete);
403         } else if (cci->ack_complete && test_bit(ACK_PENDING, &ucsi->flags)) {
404                 complete(&ucsi->complete);
405         } else if (cci->connector_change) {
406                 struct ucsi_connector *con;
407
408                 con = &ucsi->connector[cci->connector_change - 1];
409
410                 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
411                         schedule_work(&con->work);
412         }
413
414         trace_ucsi_notify(ucsi->ppm->data->raw_cci);
415 }
416 EXPORT_SYMBOL_GPL(ucsi_notify);
417
418 /* -------------------------------------------------------------------------- */
419
420 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
421 {
422         struct ucsi_control ctrl;
423
424         UCSI_CMD_CONNECTOR_RESET(ctrl, con, hard);
425
426         return ucsi_run_command(con->ucsi, &ctrl, NULL, 0);
427 }
428
429 static int ucsi_reset_ppm(struct ucsi *ucsi)
430 {
431         struct ucsi_control ctrl;
432         unsigned long tmo;
433         int ret;
434
435         ctrl.raw_cmd = 0;
436         ctrl.cmd.cmd = UCSI_PPM_RESET;
437         trace_ucsi_command(&ctrl);
438         ret = ucsi->ppm->cmd(ucsi->ppm, &ctrl);
439         if (ret)
440                 goto err;
441
442         tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
443
444         do {
445                 /* Here sync is critical. */
446                 ret = ucsi_sync(ucsi);
447                 if (ret)
448                         goto err;
449
450                 if (ucsi->ppm->data->cci.reset_complete)
451                         break;
452
453                 /* If the PPM is still doing something else, reset it again. */
454                 if (ucsi->ppm->data->raw_cci) {
455                         dev_warn_ratelimited(ucsi->dev,
456                                 "Failed to reset PPM! Trying again..\n");
457
458                         trace_ucsi_command(&ctrl);
459                         ret = ucsi->ppm->cmd(ucsi->ppm, &ctrl);
460                         if (ret)
461                                 goto err;
462                 }
463
464                 /* Letting the PPM settle down. */
465                 msleep(20);
466
467                 ret = -ETIMEDOUT;
468         } while (time_is_after_jiffies(tmo));
469
470 err:
471         trace_ucsi_reset_ppm(&ctrl, ret);
472
473         return ret;
474 }
475
476 static int ucsi_role_cmd(struct ucsi_connector *con, struct ucsi_control *ctrl)
477 {
478         int ret;
479
480         ret = ucsi_run_command(con->ucsi, ctrl, NULL, 0);
481         if (ret == -ETIMEDOUT) {
482                 struct ucsi_control c;
483
484                 /* PPM most likely stopped responding. Resetting everything. */
485                 ucsi_reset_ppm(con->ucsi);
486
487                 UCSI_CMD_SET_NTFY_ENABLE(c, UCSI_ENABLE_NTFY_ALL);
488                 ucsi_run_command(con->ucsi, &c, NULL, 0);
489
490                 ucsi_reset_connector(con, true);
491         }
492
493         return ret;
494 }
495
496 static int
497 ucsi_dr_swap(const struct typec_capability *cap, enum typec_data_role role)
498 {
499         struct ucsi_connector *con = to_ucsi_connector(cap);
500         struct ucsi_control ctrl;
501         int ret = 0;
502
503         if (!con->partner)
504                 return -ENOTCONN;
505
506         mutex_lock(&con->ucsi->ppm_lock);
507
508         if ((con->status.partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
509              role == TYPEC_DEVICE) ||
510             (con->status.partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
511              role == TYPEC_HOST))
512                 goto out_unlock;
513
514         UCSI_CMD_SET_UOR(ctrl, con, role);
515         ret = ucsi_role_cmd(con, &ctrl);
516         if (ret < 0)
517                 goto out_unlock;
518
519         mutex_unlock(&con->ucsi->ppm_lock);
520
521         if (!wait_for_completion_timeout(&con->complete,
522                                         msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
523                 return -ETIMEDOUT;
524
525         return 0;
526
527 out_unlock:
528         mutex_unlock(&con->ucsi->ppm_lock);
529
530         return ret;
531 }
532
533 static int
534 ucsi_pr_swap(const struct typec_capability *cap, enum typec_role role)
535 {
536         struct ucsi_connector *con = to_ucsi_connector(cap);
537         struct ucsi_control ctrl;
538         int ret = 0;
539
540         if (!con->partner)
541                 return -ENOTCONN;
542
543         mutex_lock(&con->ucsi->ppm_lock);
544
545         if (con->status.pwr_dir == role)
546                 goto out_unlock;
547
548         UCSI_CMD_SET_PDR(ctrl, con, role);
549         ret = ucsi_role_cmd(con, &ctrl);
550         if (ret < 0)
551                 goto out_unlock;
552
553         mutex_unlock(&con->ucsi->ppm_lock);
554
555         if (!wait_for_completion_timeout(&con->complete,
556                                         msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
557                 return -ETIMEDOUT;
558
559         mutex_lock(&con->ucsi->ppm_lock);
560
561         /* Something has gone wrong while swapping the role */
562         if (con->status.pwr_op_mode != UCSI_CONSTAT_PWR_OPMODE_PD) {
563                 ucsi_reset_connector(con, true);
564                 ret = -EPROTO;
565         }
566
567 out_unlock:
568         mutex_unlock(&con->ucsi->ppm_lock);
569
570         return ret;
571 }
572
573 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
574 {
575         struct fwnode_handle *fwnode;
576         int i = 1;
577
578         device_for_each_child_node(con->ucsi->dev, fwnode)
579                 if (i++ == con->num)
580                         return fwnode;
581         return NULL;
582 }
583
584 static int ucsi_register_port(struct ucsi *ucsi, int index)
585 {
586         struct ucsi_connector *con = &ucsi->connector[index];
587         struct typec_capability *cap = &con->typec_cap;
588         enum typec_accessory *accessory = cap->accessory;
589         struct ucsi_control ctrl;
590         int ret;
591
592         INIT_WORK(&con->work, ucsi_connector_change);
593         init_completion(&con->complete);
594         con->num = index + 1;
595         con->ucsi = ucsi;
596
597         /* Get connector capability */
598         UCSI_CMD_GET_CONNECTOR_CAPABILITY(ctrl, con->num);
599         ret = ucsi_run_command(ucsi, &ctrl, &con->cap, sizeof(con->cap));
600         if (ret < 0)
601                 return ret;
602
603         if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP)
604                 cap->type = TYPEC_PORT_DRP;
605         else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP)
606                 cap->type = TYPEC_PORT_DFP;
607         else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP)
608                 cap->type = TYPEC_PORT_UFP;
609
610         cap->revision = ucsi->cap.typec_version;
611         cap->pd_revision = ucsi->cap.pd_version;
612         cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
613
614         if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY)
615                 *accessory++ = TYPEC_ACCESSORY_AUDIO;
616         if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
617                 *accessory = TYPEC_ACCESSORY_DEBUG;
618
619         cap->fwnode = ucsi_find_fwnode(con);
620         cap->dr_set = ucsi_dr_swap;
621         cap->pr_set = ucsi_pr_swap;
622
623         /* Register the connector */
624         con->port = typec_register_port(ucsi->dev, cap);
625         if (!con->port)
626                 return -ENODEV;
627
628         /* Get the status */
629         UCSI_CMD_GET_CONNECTOR_STATUS(ctrl, con->num);
630         ret = ucsi_run_command(ucsi, &ctrl, &con->status, sizeof(con->status));
631         if (ret < 0) {
632                 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
633                 return 0;
634         }
635
636         ucsi_pwr_opmode_change(con);
637         typec_set_pwr_role(con->port, con->status.pwr_dir);
638
639         switch (con->status.partner_type) {
640         case UCSI_CONSTAT_PARTNER_TYPE_UFP:
641                 typec_set_data_role(con->port, TYPEC_HOST);
642                 break;
643         case UCSI_CONSTAT_PARTNER_TYPE_DFP:
644                 typec_set_data_role(con->port, TYPEC_DEVICE);
645                 break;
646         default:
647                 break;
648         }
649
650         /* Check if there is already something connected */
651         if (con->status.connected)
652                 ucsi_register_partner(con);
653
654         trace_ucsi_register_port(con->num, &con->status);
655
656         return 0;
657 }
658
659 static void ucsi_init(struct work_struct *work)
660 {
661         struct ucsi *ucsi = container_of(work, struct ucsi, work);
662         struct ucsi_connector *con;
663         struct ucsi_control ctrl;
664         int ret;
665         int i;
666
667         mutex_lock(&ucsi->ppm_lock);
668
669         /* Reset the PPM */
670         ret = ucsi_reset_ppm(ucsi);
671         if (ret) {
672                 dev_err(ucsi->dev, "failed to reset PPM!\n");
673                 goto err;
674         }
675
676         /* Enable basic notifications */
677         UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_CMD_COMPLETE |
678                                         UCSI_ENABLE_NTFY_ERROR);
679         ret = ucsi_run_command(ucsi, &ctrl, NULL, 0);
680         if (ret < 0)
681                 goto err_reset;
682
683         /* Get PPM capabilities */
684         UCSI_CMD_GET_CAPABILITY(ctrl);
685         ret = ucsi_run_command(ucsi, &ctrl, &ucsi->cap, sizeof(ucsi->cap));
686         if (ret < 0)
687                 goto err_reset;
688
689         if (!ucsi->cap.num_connectors) {
690                 ret = -ENODEV;
691                 goto err_reset;
692         }
693
694         /* Allocate the connectors. Released in ucsi_unregister_ppm() */
695         ucsi->connector = kcalloc(ucsi->cap.num_connectors + 1,
696                                   sizeof(*ucsi->connector), GFP_KERNEL);
697         if (!ucsi->connector) {
698                 ret = -ENOMEM;
699                 goto err_reset;
700         }
701
702         /* Register all connectors */
703         for (i = 0; i < ucsi->cap.num_connectors; i++) {
704                 ret = ucsi_register_port(ucsi, i);
705                 if (ret)
706                         goto err_unregister;
707         }
708
709         /* Enable all notifications */
710         UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_ALL);
711         ret = ucsi_run_command(ucsi, &ctrl, NULL, 0);
712         if (ret < 0)
713                 goto err_unregister;
714
715         mutex_unlock(&ucsi->ppm_lock);
716
717         return;
718
719 err_unregister:
720         for (con = ucsi->connector; con->port; con++) {
721                 ucsi_unregister_partner(con);
722                 typec_unregister_port(con->port);
723                 con->port = NULL;
724         }
725
726 err_reset:
727         memset(&ucsi->cap, 0, sizeof(ucsi->cap));
728         ucsi_reset_ppm(ucsi);
729 err:
730         mutex_unlock(&ucsi->ppm_lock);
731         dev_err(ucsi->dev, "PPM init failed (%d)\n", ret);
732 }
733
734 /**
735  * ucsi_register_ppm - Register UCSI PPM Interface
736  * @dev: Device interface to the PPM
737  * @ppm: The PPM interface
738  *
739  * Allocates UCSI instance, associates it with @ppm and returns it to the
740  * caller, and schedules initialization of the interface.
741  */
742 struct ucsi *ucsi_register_ppm(struct device *dev, struct ucsi_ppm *ppm)
743 {
744         struct ucsi *ucsi;
745
746         ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
747         if (!ucsi)
748                 return ERR_PTR(-ENOMEM);
749
750         INIT_WORK(&ucsi->work, ucsi_init);
751         init_completion(&ucsi->complete);
752         mutex_init(&ucsi->ppm_lock);
753
754         ucsi->dev = dev;
755         ucsi->ppm = ppm;
756
757         /*
758          * Communication with the PPM takes a lot of time. It is not reasonable
759          * to initialize the driver here. Using a work for now.
760          */
761         queue_work(system_long_wq, &ucsi->work);
762
763         return ucsi;
764 }
765 EXPORT_SYMBOL_GPL(ucsi_register_ppm);
766
767 /**
768  * ucsi_unregister_ppm - Unregister UCSI PPM Interface
769  * @ucsi: struct ucsi associated with the PPM
770  *
771  * Unregister UCSI PPM that was created with ucsi_register().
772  */
773 void ucsi_unregister_ppm(struct ucsi *ucsi)
774 {
775         struct ucsi_control ctrl;
776         int i;
777
778         /* Make sure that we are not in the middle of driver initialization */
779         cancel_work_sync(&ucsi->work);
780
781         mutex_lock(&ucsi->ppm_lock);
782
783         /* Disable everything except command complete notification */
784         UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_CMD_COMPLETE)
785         ucsi_run_command(ucsi, &ctrl, NULL, 0);
786
787         mutex_unlock(&ucsi->ppm_lock);
788
789         for (i = 0; i < ucsi->cap.num_connectors; i++) {
790                 cancel_work_sync(&ucsi->connector[i].work);
791                 ucsi_unregister_partner(&ucsi->connector[i]);
792                 typec_unregister_port(ucsi->connector[i].port);
793         }
794
795         ucsi_reset_ppm(ucsi);
796
797         kfree(ucsi->connector);
798         kfree(ucsi);
799 }
800 EXPORT_SYMBOL_GPL(ucsi_unregister_ppm);
801
802 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
803 MODULE_LICENSE("GPL v2");
804 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");