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