Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * USB Type-C Connector System Software Interface driver
0004  *
0005  * Copyright (C) 2017, Intel Corporation
0006  * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
0007  */
0008 
0009 #include <linux/completion.h>
0010 #include <linux/property.h>
0011 #include <linux/device.h>
0012 #include <linux/module.h>
0013 #include <linux/delay.h>
0014 #include <linux/slab.h>
0015 #include <linux/usb/typec_dp.h>
0016 
0017 #include "ucsi.h"
0018 #include "trace.h"
0019 
0020 /*
0021  * UCSI_TIMEOUT_MS - PPM communication timeout
0022  *
0023  * Ideally we could use MIN_TIME_TO_RESPOND_WITH_BUSY (which is defined in UCSI
0024  * specification) here as reference, but unfortunately we can't. It is very
0025  * difficult to estimate the time it takes for the system to process the command
0026  * before it is actually passed to the PPM.
0027  */
0028 #define UCSI_TIMEOUT_MS     5000
0029 
0030 /*
0031  * UCSI_SWAP_TIMEOUT_MS - Timeout for role swap requests
0032  *
0033  * 5 seconds is close to the time it takes for CapsCounter to reach 0, so even
0034  * if the PPM does not generate Connector Change events before that with
0035  * partners that do not support USB Power Delivery, this should still work.
0036  */
0037 #define UCSI_SWAP_TIMEOUT_MS    5000
0038 
0039 static int ucsi_acknowledge_command(struct ucsi *ucsi)
0040 {
0041     u64 ctrl;
0042 
0043     ctrl = UCSI_ACK_CC_CI;
0044     ctrl |= UCSI_ACK_COMMAND_COMPLETE;
0045 
0046     return ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &ctrl, sizeof(ctrl));
0047 }
0048 
0049 static int ucsi_acknowledge_connector_change(struct ucsi *ucsi)
0050 {
0051     u64 ctrl;
0052 
0053     ctrl = UCSI_ACK_CC_CI;
0054     ctrl |= UCSI_ACK_CONNECTOR_CHANGE;
0055 
0056     return ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &ctrl, sizeof(ctrl));
0057 }
0058 
0059 static int ucsi_exec_command(struct ucsi *ucsi, u64 command);
0060 
0061 static int ucsi_read_error(struct ucsi *ucsi)
0062 {
0063     u16 error;
0064     int ret;
0065 
0066     /* Acknowledge the command that failed */
0067     ret = ucsi_acknowledge_command(ucsi);
0068     if (ret)
0069         return ret;
0070 
0071     ret = ucsi_exec_command(ucsi, UCSI_GET_ERROR_STATUS);
0072     if (ret < 0)
0073         return ret;
0074 
0075     ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, &error, sizeof(error));
0076     if (ret)
0077         return ret;
0078 
0079     ret = ucsi_acknowledge_command(ucsi);
0080     if (ret)
0081         return ret;
0082 
0083     switch (error) {
0084     case UCSI_ERROR_INCOMPATIBLE_PARTNER:
0085         return -EOPNOTSUPP;
0086     case UCSI_ERROR_CC_COMMUNICATION_ERR:
0087         return -ECOMM;
0088     case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
0089         return -EPROTO;
0090     case UCSI_ERROR_DEAD_BATTERY:
0091         dev_warn(ucsi->dev, "Dead battery condition!\n");
0092         return -EPERM;
0093     case UCSI_ERROR_INVALID_CON_NUM:
0094     case UCSI_ERROR_UNREGONIZED_CMD:
0095     case UCSI_ERROR_INVALID_CMD_ARGUMENT:
0096         dev_err(ucsi->dev, "possible UCSI driver bug %u\n", error);
0097         return -EINVAL;
0098     case UCSI_ERROR_OVERCURRENT:
0099         dev_warn(ucsi->dev, "Overcurrent condition\n");
0100         break;
0101     case UCSI_ERROR_PARTNER_REJECTED_SWAP:
0102         dev_warn(ucsi->dev, "Partner rejected swap\n");
0103         break;
0104     case UCSI_ERROR_HARD_RESET:
0105         dev_warn(ucsi->dev, "Hard reset occurred\n");
0106         break;
0107     case UCSI_ERROR_PPM_POLICY_CONFLICT:
0108         dev_warn(ucsi->dev, "PPM Policy conflict\n");
0109         break;
0110     case UCSI_ERROR_SWAP_REJECTED:
0111         dev_warn(ucsi->dev, "Swap rejected\n");
0112         break;
0113     case UCSI_ERROR_UNDEFINED:
0114     default:
0115         dev_err(ucsi->dev, "unknown error %u\n", error);
0116         break;
0117     }
0118 
0119     return -EIO;
0120 }
0121 
0122 static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd)
0123 {
0124     u32 cci;
0125     int ret;
0126 
0127     ret = ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
0128     if (ret)
0129         return ret;
0130 
0131     ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
0132     if (ret)
0133         return ret;
0134 
0135     if (cci & UCSI_CCI_BUSY) {
0136         ucsi->ops->async_write(ucsi, UCSI_CANCEL, NULL, 0);
0137         return -EBUSY;
0138     }
0139 
0140     if (!(cci & UCSI_CCI_COMMAND_COMPLETE))
0141         return -EIO;
0142 
0143     if (cci & UCSI_CCI_NOT_SUPPORTED)
0144         return -EOPNOTSUPP;
0145 
0146     if (cci & UCSI_CCI_ERROR) {
0147         if (cmd == UCSI_GET_ERROR_STATUS)
0148             return -EIO;
0149         return ucsi_read_error(ucsi);
0150     }
0151 
0152     return UCSI_CCI_LENGTH(cci);
0153 }
0154 
0155 int ucsi_send_command(struct ucsi *ucsi, u64 command,
0156               void *data, size_t size)
0157 {
0158     u8 length;
0159     int ret;
0160 
0161     mutex_lock(&ucsi->ppm_lock);
0162 
0163     ret = ucsi_exec_command(ucsi, command);
0164     if (ret < 0)
0165         goto out;
0166 
0167     length = ret;
0168 
0169     if (data) {
0170         ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, data, size);
0171         if (ret)
0172             goto out;
0173     }
0174 
0175     ret = ucsi_acknowledge_command(ucsi);
0176     if (ret)
0177         goto out;
0178 
0179     ret = length;
0180 out:
0181     mutex_unlock(&ucsi->ppm_lock);
0182     return ret;
0183 }
0184 EXPORT_SYMBOL_GPL(ucsi_send_command);
0185 
0186 int ucsi_resume(struct ucsi *ucsi)
0187 {
0188     u64 command;
0189 
0190     /* Restore UCSI notification enable mask after system resume */
0191     command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
0192 
0193     return ucsi_send_command(ucsi, command, NULL, 0);
0194 }
0195 EXPORT_SYMBOL_GPL(ucsi_resume);
0196 /* -------------------------------------------------------------------------- */
0197 
0198 struct ucsi_work {
0199     struct delayed_work work;
0200     unsigned long delay;
0201     unsigned int count;
0202     struct ucsi_connector *con;
0203     int (*cb)(struct ucsi_connector *);
0204 };
0205 
0206 static void ucsi_poll_worker(struct work_struct *work)
0207 {
0208     struct ucsi_work *uwork = container_of(work, struct ucsi_work, work.work);
0209     struct ucsi_connector *con = uwork->con;
0210     int ret;
0211 
0212     mutex_lock(&con->lock);
0213 
0214     if (!con->partner) {
0215         mutex_unlock(&con->lock);
0216         kfree(uwork);
0217         return;
0218     }
0219 
0220     ret = uwork->cb(con);
0221 
0222     if (uwork->count-- && (ret == -EBUSY || ret == -ETIMEDOUT))
0223         queue_delayed_work(con->wq, &uwork->work, uwork->delay);
0224     else
0225         kfree(uwork);
0226 
0227     mutex_unlock(&con->lock);
0228 }
0229 
0230 static int ucsi_partner_task(struct ucsi_connector *con,
0231                  int (*cb)(struct ucsi_connector *),
0232                  int retries, unsigned long delay)
0233 {
0234     struct ucsi_work *uwork;
0235 
0236     if (!con->partner)
0237         return 0;
0238 
0239     uwork = kzalloc(sizeof(*uwork), GFP_KERNEL);
0240     if (!uwork)
0241         return -ENOMEM;
0242 
0243     INIT_DELAYED_WORK(&uwork->work, ucsi_poll_worker);
0244     uwork->count = retries;
0245     uwork->delay = delay;
0246     uwork->con = con;
0247     uwork->cb = cb;
0248 
0249     queue_delayed_work(con->wq, &uwork->work, delay);
0250 
0251     return 0;
0252 }
0253 
0254 /* -------------------------------------------------------------------------- */
0255 
0256 void ucsi_altmode_update_active(struct ucsi_connector *con)
0257 {
0258     const struct typec_altmode *altmode = NULL;
0259     u64 command;
0260     int ret;
0261     u8 cur;
0262     int i;
0263 
0264     command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
0265     ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur));
0266     if (ret < 0) {
0267         if (con->ucsi->version > 0x0100) {
0268             dev_err(con->ucsi->dev,
0269                 "GET_CURRENT_CAM command failed\n");
0270             return;
0271         }
0272         cur = 0xff;
0273     }
0274 
0275     if (cur < UCSI_MAX_ALTMODES)
0276         altmode = typec_altmode_get_partner(con->port_altmode[cur]);
0277 
0278     for (i = 0; con->partner_altmode[i]; i++)
0279         typec_altmode_update_active(con->partner_altmode[i],
0280                         con->partner_altmode[i] == altmode);
0281 }
0282 
0283 static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
0284 {
0285     u8 mode = 1;
0286     int i;
0287 
0288     for (i = 0; alt[i]; i++) {
0289         if (i > MODE_DISCOVERY_MAX)
0290             return -ERANGE;
0291 
0292         if (alt[i]->svid == svid)
0293             mode++;
0294     }
0295 
0296     return mode;
0297 }
0298 
0299 static int ucsi_next_altmode(struct typec_altmode **alt)
0300 {
0301     int i = 0;
0302 
0303     for (i = 0; i < UCSI_MAX_ALTMODES; i++)
0304         if (!alt[i])
0305             return i;
0306 
0307     return -ENOENT;
0308 }
0309 
0310 static int ucsi_get_num_altmode(struct typec_altmode **alt)
0311 {
0312     int i;
0313 
0314     for (i = 0; i < UCSI_MAX_ALTMODES; i++)
0315         if (!alt[i])
0316             break;
0317 
0318     return i;
0319 }
0320 
0321 static int ucsi_register_altmode(struct ucsi_connector *con,
0322                  struct typec_altmode_desc *desc,
0323                  u8 recipient)
0324 {
0325     struct typec_altmode *alt;
0326     bool override;
0327     int ret;
0328     int i;
0329 
0330     override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
0331 
0332     switch (recipient) {
0333     case UCSI_RECIPIENT_CON:
0334         i = ucsi_next_altmode(con->port_altmode);
0335         if (i < 0) {
0336             ret = i;
0337             goto err;
0338         }
0339 
0340         ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid);
0341         if (ret < 0)
0342             return ret;
0343 
0344         desc->mode = ret;
0345 
0346         switch (desc->svid) {
0347         case USB_TYPEC_DP_SID:
0348             alt = ucsi_register_displayport(con, override, i, desc);
0349             break;
0350         case USB_TYPEC_NVIDIA_VLINK_SID:
0351             if (desc->vdo == USB_TYPEC_NVIDIA_VLINK_DBG_VDO)
0352                 alt = typec_port_register_altmode(con->port,
0353                                   desc);
0354             else
0355                 alt = ucsi_register_displayport(con, override,
0356                                 i, desc);
0357             break;
0358         default:
0359             alt = typec_port_register_altmode(con->port, desc);
0360             break;
0361         }
0362 
0363         if (IS_ERR(alt)) {
0364             ret = PTR_ERR(alt);
0365             goto err;
0366         }
0367 
0368         con->port_altmode[i] = alt;
0369         break;
0370     case UCSI_RECIPIENT_SOP:
0371         i = ucsi_next_altmode(con->partner_altmode);
0372         if (i < 0) {
0373             ret = i;
0374             goto err;
0375         }
0376 
0377         ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid);
0378         if (ret < 0)
0379             return ret;
0380 
0381         desc->mode = ret;
0382 
0383         alt = typec_partner_register_altmode(con->partner, desc);
0384         if (IS_ERR(alt)) {
0385             ret = PTR_ERR(alt);
0386             goto err;
0387         }
0388 
0389         con->partner_altmode[i] = alt;
0390         break;
0391     default:
0392         return -EINVAL;
0393     }
0394 
0395     trace_ucsi_register_altmode(recipient, alt);
0396 
0397     return 0;
0398 
0399 err:
0400     dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n",
0401         desc->svid, desc->mode);
0402 
0403     return ret;
0404 }
0405 
0406 static int
0407 ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient)
0408 {
0409     int max_altmodes = UCSI_MAX_ALTMODES;
0410     struct typec_altmode_desc desc;
0411     struct ucsi_altmode alt;
0412     struct ucsi_altmode orig[UCSI_MAX_ALTMODES];
0413     struct ucsi_altmode updated[UCSI_MAX_ALTMODES];
0414     struct ucsi *ucsi = con->ucsi;
0415     bool multi_dp = false;
0416     u64 command;
0417     int ret;
0418     int len;
0419     int i;
0420     int k = 0;
0421 
0422     if (recipient == UCSI_RECIPIENT_CON)
0423         max_altmodes = con->ucsi->cap.num_alt_modes;
0424 
0425     memset(orig, 0, sizeof(orig));
0426     memset(updated, 0, sizeof(updated));
0427 
0428     /* First get all the alternate modes */
0429     for (i = 0; i < max_altmodes; i++) {
0430         memset(&alt, 0, sizeof(alt));
0431         command = UCSI_GET_ALTERNATE_MODES;
0432         command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
0433         command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
0434         command |= UCSI_GET_ALTMODE_OFFSET(i);
0435         len = ucsi_send_command(con->ucsi, command, &alt, sizeof(alt));
0436         /*
0437          * We are collecting all altmodes first and then registering.
0438          * Some type-C device will return zero length data beyond last
0439          * alternate modes. We should not return if length is zero.
0440          */
0441         if (len < 0)
0442             return len;
0443 
0444         /* We got all altmodes, now break out and register them */
0445         if (!len || !alt.svid)
0446             break;
0447 
0448         orig[k].mid = alt.mid;
0449         orig[k].svid = alt.svid;
0450         k++;
0451     }
0452     /*
0453      * Update the original altmode table as some ppms may report
0454      * multiple DP altmodes.
0455      */
0456     if (recipient == UCSI_RECIPIENT_CON)
0457         multi_dp = ucsi->ops->update_altmodes(ucsi, orig, updated);
0458 
0459     /* now register altmodes */
0460     for (i = 0; i < max_altmodes; i++) {
0461         memset(&desc, 0, sizeof(desc));
0462         if (multi_dp && recipient == UCSI_RECIPIENT_CON) {
0463             desc.svid = updated[i].svid;
0464             desc.vdo = updated[i].mid;
0465         } else {
0466             desc.svid = orig[i].svid;
0467             desc.vdo = orig[i].mid;
0468         }
0469         desc.roles = TYPEC_PORT_DRD;
0470 
0471         if (!desc.svid)
0472             return 0;
0473 
0474         ret = ucsi_register_altmode(con, &desc, recipient);
0475         if (ret)
0476             return ret;
0477     }
0478 
0479     return 0;
0480 }
0481 
0482 static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
0483 {
0484     int max_altmodes = UCSI_MAX_ALTMODES;
0485     struct typec_altmode_desc desc;
0486     struct ucsi_altmode alt[2];
0487     u64 command;
0488     int num;
0489     int ret;
0490     int len;
0491     int j;
0492     int i;
0493 
0494     if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
0495         return 0;
0496 
0497     if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
0498         return 0;
0499 
0500     if (con->ucsi->ops->update_altmodes)
0501         return ucsi_register_altmodes_nvidia(con, recipient);
0502 
0503     if (recipient == UCSI_RECIPIENT_CON)
0504         max_altmodes = con->ucsi->cap.num_alt_modes;
0505 
0506     for (i = 0; i < max_altmodes;) {
0507         memset(alt, 0, sizeof(alt));
0508         command = UCSI_GET_ALTERNATE_MODES;
0509         command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
0510         command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
0511         command |= UCSI_GET_ALTMODE_OFFSET(i);
0512         len = ucsi_send_command(con->ucsi, command, alt, sizeof(alt));
0513         if (len == -EBUSY)
0514             continue;
0515         if (len <= 0)
0516             return len;
0517 
0518         /*
0519          * This code is requesting one alt mode at a time, but some PPMs
0520          * may still return two. If that happens both alt modes need be
0521          * registered and the offset for the next alt mode has to be
0522          * incremented.
0523          */
0524         num = len / sizeof(alt[0]);
0525         i += num;
0526 
0527         for (j = 0; j < num; j++) {
0528             if (!alt[j].svid)
0529                 return 0;
0530 
0531             memset(&desc, 0, sizeof(desc));
0532             desc.vdo = alt[j].mid;
0533             desc.svid = alt[j].svid;
0534             desc.roles = TYPEC_PORT_DRD;
0535 
0536             ret = ucsi_register_altmode(con, &desc, recipient);
0537             if (ret)
0538                 return ret;
0539         }
0540     }
0541 
0542     return 0;
0543 }
0544 
0545 static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
0546 {
0547     const struct typec_altmode *pdev;
0548     struct typec_altmode **adev;
0549     int i = 0;
0550 
0551     switch (recipient) {
0552     case UCSI_RECIPIENT_CON:
0553         adev = con->port_altmode;
0554         break;
0555     case UCSI_RECIPIENT_SOP:
0556         adev = con->partner_altmode;
0557         break;
0558     default:
0559         return;
0560     }
0561 
0562     while (adev[i]) {
0563         if (recipient == UCSI_RECIPIENT_SOP &&
0564             (adev[i]->svid == USB_TYPEC_DP_SID ||
0565             (adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID &&
0566             adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) {
0567             pdev = typec_altmode_get_partner(adev[i]);
0568             ucsi_displayport_remove_partner((void *)pdev);
0569         }
0570         typec_unregister_altmode(adev[i]);
0571         adev[i++] = NULL;
0572     }
0573 }
0574 
0575 static int ucsi_get_pdos(struct ucsi_connector *con, int is_partner,
0576              u32 *pdos, int offset, int num_pdos)
0577 {
0578     struct ucsi *ucsi = con->ucsi;
0579     u64 command;
0580     int ret;
0581 
0582     command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num);
0583     command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner);
0584     command |= UCSI_GET_PDOS_PDO_OFFSET(offset);
0585     command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1);
0586     command |= UCSI_GET_PDOS_SRC_PDOS;
0587     ret = ucsi_send_command(ucsi, command, pdos + offset,
0588                 num_pdos * sizeof(u32));
0589     if (ret < 0 && ret != -ETIMEDOUT)
0590         dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret);
0591 
0592     return ret;
0593 }
0594 
0595 static int ucsi_get_src_pdos(struct ucsi_connector *con)
0596 {
0597     int ret;
0598 
0599     /* UCSI max payload means only getting at most 4 PDOs at a time */
0600     ret = ucsi_get_pdos(con, 1, con->src_pdos, 0, UCSI_MAX_PDOS);
0601     if (ret < 0)
0602         return ret;
0603 
0604     con->num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */
0605     if (con->num_pdos < UCSI_MAX_PDOS)
0606         return 0;
0607 
0608     /* get the remaining PDOs, if any */
0609     ret = ucsi_get_pdos(con, 1, con->src_pdos, UCSI_MAX_PDOS,
0610                 PDO_MAX_OBJECTS - UCSI_MAX_PDOS);
0611     if (ret < 0)
0612         return ret;
0613 
0614     con->num_pdos += ret / sizeof(u32);
0615 
0616     ucsi_port_psy_changed(con);
0617 
0618     return 0;
0619 }
0620 
0621 static int ucsi_check_altmodes(struct ucsi_connector *con)
0622 {
0623     int ret, num_partner_am;
0624 
0625     ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
0626     if (ret && ret != -ETIMEDOUT)
0627         dev_err(con->ucsi->dev,
0628             "con%d: failed to register partner alt modes (%d)\n",
0629             con->num, ret);
0630 
0631     /* Ignoring the errors in this case. */
0632     if (con->partner_altmode[0]) {
0633         num_partner_am = ucsi_get_num_altmode(con->partner_altmode);
0634         if (num_partner_am > 0)
0635             typec_partner_set_num_altmodes(con->partner, num_partner_am);
0636         ucsi_altmode_update_active(con);
0637         return 0;
0638     }
0639 
0640     return ret;
0641 }
0642 
0643 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
0644 {
0645     switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
0646     case UCSI_CONSTAT_PWR_OPMODE_PD:
0647         con->rdo = con->status.request_data_obj;
0648         typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
0649         ucsi_partner_task(con, ucsi_get_src_pdos, 30, 0);
0650         ucsi_partner_task(con, ucsi_check_altmodes, 30, 0);
0651         break;
0652     case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
0653         con->rdo = 0;
0654         typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
0655         break;
0656     case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
0657         con->rdo = 0;
0658         typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
0659         break;
0660     default:
0661         con->rdo = 0;
0662         typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
0663         break;
0664     }
0665 }
0666 
0667 static int ucsi_register_partner(struct ucsi_connector *con)
0668 {
0669     u8 pwr_opmode = UCSI_CONSTAT_PWR_OPMODE(con->status.flags);
0670     struct typec_partner_desc desc;
0671     struct typec_partner *partner;
0672 
0673     if (con->partner)
0674         return 0;
0675 
0676     memset(&desc, 0, sizeof(desc));
0677 
0678     switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
0679     case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
0680         desc.accessory = TYPEC_ACCESSORY_DEBUG;
0681         break;
0682     case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
0683         desc.accessory = TYPEC_ACCESSORY_AUDIO;
0684         break;
0685     default:
0686         break;
0687     }
0688 
0689     desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD;
0690 
0691     partner = typec_register_partner(con->port, &desc);
0692     if (IS_ERR(partner)) {
0693         dev_err(con->ucsi->dev,
0694             "con%d: failed to register partner (%ld)\n", con->num,
0695             PTR_ERR(partner));
0696         return PTR_ERR(partner);
0697     }
0698 
0699     con->partner = partner;
0700 
0701     return 0;
0702 }
0703 
0704 static void ucsi_unregister_partner(struct ucsi_connector *con)
0705 {
0706     if (!con->partner)
0707         return;
0708 
0709     ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
0710     typec_unregister_partner(con->partner);
0711     con->partner = NULL;
0712 }
0713 
0714 static void ucsi_partner_change(struct ucsi_connector *con)
0715 {
0716     enum usb_role u_role = USB_ROLE_NONE;
0717     int ret;
0718 
0719     switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
0720     case UCSI_CONSTAT_PARTNER_TYPE_UFP:
0721     case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
0722         u_role = USB_ROLE_HOST;
0723         fallthrough;
0724     case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
0725         typec_set_data_role(con->port, TYPEC_HOST);
0726         break;
0727     case UCSI_CONSTAT_PARTNER_TYPE_DFP:
0728         u_role = USB_ROLE_DEVICE;
0729         typec_set_data_role(con->port, TYPEC_DEVICE);
0730         break;
0731     default:
0732         break;
0733     }
0734 
0735     /* Only notify USB controller if partner supports USB data */
0736     if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
0737         u_role = USB_ROLE_NONE;
0738 
0739     ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
0740     if (ret)
0741         dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n",
0742             con->num, u_role);
0743 }
0744 
0745 static int ucsi_check_connection(struct ucsi_connector *con)
0746 {
0747     u64 command;
0748     int ret;
0749 
0750     command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
0751     ret = ucsi_send_command(con->ucsi, command, &con->status, sizeof(con->status));
0752     if (ret < 0) {
0753         dev_err(con->ucsi->dev, "GET_CONNECTOR_STATUS failed (%d)\n", ret);
0754         return ret;
0755     }
0756 
0757     if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
0758         if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) ==
0759             UCSI_CONSTAT_PWR_OPMODE_PD)
0760             ucsi_partner_task(con, ucsi_check_altmodes, 30, 0);
0761     } else {
0762         ucsi_partner_change(con);
0763         ucsi_port_psy_changed(con);
0764         ucsi_unregister_partner(con);
0765     }
0766 
0767     return 0;
0768 }
0769 
0770 static void ucsi_handle_connector_change(struct work_struct *work)
0771 {
0772     struct ucsi_connector *con = container_of(work, struct ucsi_connector,
0773                           work);
0774     struct ucsi *ucsi = con->ucsi;
0775     enum typec_role role;
0776     u64 command;
0777     int ret;
0778 
0779     mutex_lock(&con->lock);
0780 
0781     command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
0782     ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
0783     if (ret < 0) {
0784         dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
0785             __func__, ret);
0786         goto out_unlock;
0787     }
0788 
0789     trace_ucsi_connector_change(con->num, &con->status);
0790 
0791     role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
0792 
0793     if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
0794         typec_set_pwr_role(con->port, role);
0795 
0796         /* Complete pending power role swap */
0797         if (!completion_done(&con->complete))
0798             complete(&con->complete);
0799     }
0800 
0801     if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
0802         typec_set_pwr_role(con->port, role);
0803         ucsi_port_psy_changed(con);
0804         ucsi_partner_change(con);
0805 
0806         if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
0807             ucsi_register_partner(con);
0808             ucsi_partner_task(con, ucsi_check_connection, 1, HZ);
0809         } else {
0810             ucsi_unregister_partner(con);
0811         }
0812     }
0813 
0814     if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE ||
0815         con->status.change & UCSI_CONSTAT_POWER_LEVEL_CHANGE)
0816         ucsi_pwr_opmode_change(con);
0817 
0818     if (con->partner && con->status.change & UCSI_CONSTAT_PARTNER_CHANGE) {
0819         ucsi_partner_change(con);
0820 
0821         /* Complete pending data role swap */
0822         if (!completion_done(&con->complete))
0823             complete(&con->complete);
0824     }
0825 
0826     if (con->status.change & UCSI_CONSTAT_CAM_CHANGE)
0827         ucsi_partner_task(con, ucsi_check_altmodes, 1, 0);
0828 
0829     clear_bit(EVENT_PENDING, &con->ucsi->flags);
0830 
0831     ret = ucsi_acknowledge_connector_change(ucsi);
0832     if (ret)
0833         dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret);
0834 
0835 out_unlock:
0836     mutex_unlock(&con->lock);
0837 }
0838 
0839 /**
0840  * ucsi_connector_change - Process Connector Change Event
0841  * @ucsi: UCSI Interface
0842  * @num: Connector number
0843  */
0844 void ucsi_connector_change(struct ucsi *ucsi, u8 num)
0845 {
0846     struct ucsi_connector *con = &ucsi->connector[num - 1];
0847 
0848     if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
0849         dev_dbg(ucsi->dev, "Bogus connector change event\n");
0850         return;
0851     }
0852 
0853     if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
0854         schedule_work(&con->work);
0855 }
0856 EXPORT_SYMBOL_GPL(ucsi_connector_change);
0857 
0858 /* -------------------------------------------------------------------------- */
0859 
0860 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
0861 {
0862     u64 command;
0863 
0864     command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num);
0865     command |= hard ? UCSI_CONNECTOR_RESET_HARD : 0;
0866 
0867     return ucsi_send_command(con->ucsi, command, NULL, 0);
0868 }
0869 
0870 static int ucsi_reset_ppm(struct ucsi *ucsi)
0871 {
0872     u64 command = UCSI_PPM_RESET;
0873     unsigned long tmo;
0874     u32 cci;
0875     int ret;
0876 
0877     mutex_lock(&ucsi->ppm_lock);
0878 
0879     ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command,
0880                      sizeof(command));
0881     if (ret < 0)
0882         goto out;
0883 
0884     tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
0885 
0886     do {
0887         if (time_is_before_jiffies(tmo)) {
0888             ret = -ETIMEDOUT;
0889             goto out;
0890         }
0891 
0892         ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
0893         if (ret)
0894             goto out;
0895 
0896         /* If the PPM is still doing something else, reset it again. */
0897         if (cci & ~UCSI_CCI_RESET_COMPLETE) {
0898             ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL,
0899                              &command,
0900                              sizeof(command));
0901             if (ret < 0)
0902                 goto out;
0903         }
0904 
0905         msleep(20);
0906     } while (!(cci & UCSI_CCI_RESET_COMPLETE));
0907 
0908 out:
0909     mutex_unlock(&ucsi->ppm_lock);
0910     return ret;
0911 }
0912 
0913 static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)
0914 {
0915     int ret;
0916 
0917     ret = ucsi_send_command(con->ucsi, command, NULL, 0);
0918     if (ret == -ETIMEDOUT) {
0919         u64 c;
0920 
0921         /* PPM most likely stopped responding. Resetting everything. */
0922         ucsi_reset_ppm(con->ucsi);
0923 
0924         c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
0925         ucsi_send_command(con->ucsi, c, NULL, 0);
0926 
0927         ucsi_reset_connector(con, true);
0928     }
0929 
0930     return ret;
0931 }
0932 
0933 static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
0934 {
0935     struct ucsi_connector *con = typec_get_drvdata(port);
0936     u8 partner_type;
0937     u64 command;
0938     int ret = 0;
0939 
0940     mutex_lock(&con->lock);
0941 
0942     if (!con->partner) {
0943         ret = -ENOTCONN;
0944         goto out_unlock;
0945     }
0946 
0947     partner_type = UCSI_CONSTAT_PARTNER_TYPE(con->status.flags);
0948     if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
0949          role == TYPEC_DEVICE) ||
0950         (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
0951          role == TYPEC_HOST))
0952         goto out_unlock;
0953 
0954     reinit_completion(&con->complete);
0955 
0956     command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num);
0957     command |= UCSI_SET_UOR_ROLE(role);
0958     command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS;
0959     ret = ucsi_role_cmd(con, command);
0960     if (ret < 0)
0961         goto out_unlock;
0962 
0963     mutex_unlock(&con->lock);
0964 
0965     if (!wait_for_completion_timeout(&con->complete,
0966                      msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
0967         return -ETIMEDOUT;
0968 
0969     return 0;
0970 
0971 out_unlock:
0972     mutex_unlock(&con->lock);
0973 
0974     return ret;
0975 }
0976 
0977 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
0978 {
0979     struct ucsi_connector *con = typec_get_drvdata(port);
0980     enum typec_role cur_role;
0981     u64 command;
0982     int ret = 0;
0983 
0984     mutex_lock(&con->lock);
0985 
0986     if (!con->partner) {
0987         ret = -ENOTCONN;
0988         goto out_unlock;
0989     }
0990 
0991     cur_role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
0992 
0993     if (cur_role == role)
0994         goto out_unlock;
0995 
0996     reinit_completion(&con->complete);
0997 
0998     command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num);
0999     command |= UCSI_SET_PDR_ROLE(role);
1000     command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS;
1001     ret = ucsi_role_cmd(con, command);
1002     if (ret < 0)
1003         goto out_unlock;
1004 
1005     mutex_unlock(&con->lock);
1006 
1007     if (!wait_for_completion_timeout(&con->complete,
1008                      msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1009         return -ETIMEDOUT;
1010 
1011     mutex_lock(&con->lock);
1012 
1013     /* Something has gone wrong while swapping the role */
1014     if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) !=
1015         UCSI_CONSTAT_PWR_OPMODE_PD) {
1016         ucsi_reset_connector(con, true);
1017         ret = -EPROTO;
1018     }
1019 
1020 out_unlock:
1021     mutex_unlock(&con->lock);
1022 
1023     return ret;
1024 }
1025 
1026 static const struct typec_operations ucsi_ops = {
1027     .dr_set = ucsi_dr_swap,
1028     .pr_set = ucsi_pr_swap
1029 };
1030 
1031 /* Caller must call fwnode_handle_put() after use */
1032 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
1033 {
1034     struct fwnode_handle *fwnode;
1035     int i = 1;
1036 
1037     device_for_each_child_node(con->ucsi->dev, fwnode)
1038         if (i++ == con->num)
1039             return fwnode;
1040     return NULL;
1041 }
1042 
1043 static int ucsi_register_port(struct ucsi *ucsi, int index)
1044 {
1045     struct ucsi_connector *con = &ucsi->connector[index];
1046     struct typec_capability *cap = &con->typec_cap;
1047     enum typec_accessory *accessory = cap->accessory;
1048     enum usb_role u_role = USB_ROLE_NONE;
1049     u64 command;
1050     char *name;
1051     int ret;
1052 
1053     name = kasprintf(GFP_KERNEL, "%s-con%d", dev_name(ucsi->dev), con->num);
1054     if (!name)
1055         return -ENOMEM;
1056 
1057     con->wq = create_singlethread_workqueue(name);
1058     kfree(name);
1059     if (!con->wq)
1060         return -ENOMEM;
1061 
1062     INIT_WORK(&con->work, ucsi_handle_connector_change);
1063     init_completion(&con->complete);
1064     mutex_init(&con->lock);
1065     con->num = index + 1;
1066     con->ucsi = ucsi;
1067 
1068     cap->fwnode = ucsi_find_fwnode(con);
1069     con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode);
1070     if (IS_ERR(con->usb_role_sw)) {
1071         dev_err(ucsi->dev, "con%d: failed to get usb role switch\n",
1072             con->num);
1073         return PTR_ERR(con->usb_role_sw);
1074     }
1075 
1076     /* Delay other interactions with the con until registration is complete */
1077     mutex_lock(&con->lock);
1078 
1079     /* Get connector capability */
1080     command = UCSI_GET_CONNECTOR_CAPABILITY;
1081     command |= UCSI_CONNECTOR_NUMBER(con->num);
1082     ret = ucsi_send_command(ucsi, command, &con->cap, sizeof(con->cap));
1083     if (ret < 0)
1084         goto out_unlock;
1085 
1086     if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP)
1087         cap->data = TYPEC_PORT_DRD;
1088     else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP)
1089         cap->data = TYPEC_PORT_DFP;
1090     else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP)
1091         cap->data = TYPEC_PORT_UFP;
1092 
1093     if ((con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER) &&
1094         (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER))
1095         cap->type = TYPEC_PORT_DRP;
1096     else if (con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER)
1097         cap->type = TYPEC_PORT_SRC;
1098     else if (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER)
1099         cap->type = TYPEC_PORT_SNK;
1100 
1101     cap->revision = ucsi->cap.typec_version;
1102     cap->pd_revision = ucsi->cap.pd_version;
1103     cap->svdm_version = SVDM_VER_2_0;
1104     cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
1105 
1106     if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY)
1107         *accessory++ = TYPEC_ACCESSORY_AUDIO;
1108     if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
1109         *accessory = TYPEC_ACCESSORY_DEBUG;
1110 
1111     cap->driver_data = con;
1112     cap->ops = &ucsi_ops;
1113 
1114     ret = ucsi_register_port_psy(con);
1115     if (ret)
1116         goto out;
1117 
1118     /* Register the connector */
1119     con->port = typec_register_port(ucsi->dev, cap);
1120     if (IS_ERR(con->port)) {
1121         ret = PTR_ERR(con->port);
1122         goto out;
1123     }
1124 
1125     /* Alternate modes */
1126     ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON);
1127     if (ret) {
1128         dev_err(ucsi->dev, "con%d: failed to register alt modes\n",
1129             con->num);
1130         goto out;
1131     }
1132 
1133     /* Get the status */
1134     command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
1135     ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
1136     if (ret < 0) {
1137         dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
1138         ret = 0;
1139         goto out;
1140     }
1141     ret = 0; /* ucsi_send_command() returns length on success */
1142 
1143     switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1144     case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1145     case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1146         u_role = USB_ROLE_HOST;
1147         fallthrough;
1148     case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1149         typec_set_data_role(con->port, TYPEC_HOST);
1150         break;
1151     case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1152         u_role = USB_ROLE_DEVICE;
1153         typec_set_data_role(con->port, TYPEC_DEVICE);
1154         break;
1155     default:
1156         break;
1157     }
1158 
1159     /* Check if there is already something connected */
1160     if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1161         typec_set_pwr_role(con->port,
1162                   !!(con->status.flags & UCSI_CONSTAT_PWR_DIR));
1163         ucsi_pwr_opmode_change(con);
1164         ucsi_register_partner(con);
1165         ucsi_port_psy_changed(con);
1166     }
1167 
1168     /* Only notify USB controller if partner supports USB data */
1169     if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
1170         u_role = USB_ROLE_NONE;
1171 
1172     ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1173     if (ret) {
1174         dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n",
1175             con->num, u_role);
1176         ret = 0;
1177     }
1178 
1179     if (con->partner &&
1180         UCSI_CONSTAT_PWR_OPMODE(con->status.flags) ==
1181         UCSI_CONSTAT_PWR_OPMODE_PD) {
1182         ucsi_get_src_pdos(con);
1183         ucsi_check_altmodes(con);
1184     }
1185 
1186     trace_ucsi_register_port(con->num, &con->status);
1187 
1188 out:
1189     fwnode_handle_put(cap->fwnode);
1190 out_unlock:
1191     mutex_unlock(&con->lock);
1192 
1193     if (ret && con->wq) {
1194         destroy_workqueue(con->wq);
1195         con->wq = NULL;
1196     }
1197 
1198     return ret;
1199 }
1200 
1201 /**
1202  * ucsi_init - Initialize UCSI interface
1203  * @ucsi: UCSI to be initialized
1204  *
1205  * Registers all ports @ucsi has and enables all notification events.
1206  */
1207 static int ucsi_init(struct ucsi *ucsi)
1208 {
1209     struct ucsi_connector *con;
1210     u64 command;
1211     int ret;
1212     int i;
1213 
1214     /* Reset the PPM */
1215     ret = ucsi_reset_ppm(ucsi);
1216     if (ret) {
1217         dev_err(ucsi->dev, "failed to reset PPM!\n");
1218         goto err;
1219     }
1220 
1221     /* Enable basic notifications */
1222     ucsi->ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
1223     command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1224     ret = ucsi_send_command(ucsi, command, NULL, 0);
1225     if (ret < 0)
1226         goto err_reset;
1227 
1228     /* Get PPM capabilities */
1229     command = UCSI_GET_CAPABILITY;
1230     ret = ucsi_send_command(ucsi, command, &ucsi->cap, sizeof(ucsi->cap));
1231     if (ret < 0)
1232         goto err_reset;
1233 
1234     if (!ucsi->cap.num_connectors) {
1235         ret = -ENODEV;
1236         goto err_reset;
1237     }
1238 
1239     /* Allocate the connectors. Released in ucsi_unregister() */
1240     ucsi->connector = kcalloc(ucsi->cap.num_connectors + 1,
1241                   sizeof(*ucsi->connector), GFP_KERNEL);
1242     if (!ucsi->connector) {
1243         ret = -ENOMEM;
1244         goto err_reset;
1245     }
1246 
1247     /* Register all connectors */
1248     for (i = 0; i < ucsi->cap.num_connectors; i++) {
1249         ret = ucsi_register_port(ucsi, i);
1250         if (ret)
1251             goto err_unregister;
1252     }
1253 
1254     /* Enable all notifications */
1255     ucsi->ntfy = UCSI_ENABLE_NTFY_ALL;
1256     command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1257     ret = ucsi_send_command(ucsi, command, NULL, 0);
1258     if (ret < 0)
1259         goto err_unregister;
1260 
1261     return 0;
1262 
1263 err_unregister:
1264     for (con = ucsi->connector; con->port; con++) {
1265         ucsi_unregister_partner(con);
1266         ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON);
1267         ucsi_unregister_port_psy(con);
1268         if (con->wq)
1269             destroy_workqueue(con->wq);
1270         typec_unregister_port(con->port);
1271         con->port = NULL;
1272     }
1273 
1274 err_reset:
1275     memset(&ucsi->cap, 0, sizeof(ucsi->cap));
1276     ucsi_reset_ppm(ucsi);
1277 err:
1278     return ret;
1279 }
1280 
1281 static void ucsi_init_work(struct work_struct *work)
1282 {
1283     struct ucsi *ucsi = container_of(work, struct ucsi, work.work);
1284     int ret;
1285 
1286     ret = ucsi_init(ucsi);
1287     if (ret)
1288         dev_err(ucsi->dev, "PPM init failed (%d)\n", ret);
1289 
1290     if (ret == -EPROBE_DEFER) {
1291         if (ucsi->work_count++ > UCSI_ROLE_SWITCH_WAIT_COUNT)
1292             return;
1293 
1294         queue_delayed_work(system_long_wq, &ucsi->work,
1295                    UCSI_ROLE_SWITCH_INTERVAL);
1296     }
1297 }
1298 
1299 /**
1300  * ucsi_get_drvdata - Return private driver data pointer
1301  * @ucsi: UCSI interface
1302  */
1303 void *ucsi_get_drvdata(struct ucsi *ucsi)
1304 {
1305     return ucsi->driver_data;
1306 }
1307 EXPORT_SYMBOL_GPL(ucsi_get_drvdata);
1308 
1309 /**
1310  * ucsi_set_drvdata - Assign private driver data pointer
1311  * @ucsi: UCSI interface
1312  * @data: Private data pointer
1313  */
1314 void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
1315 {
1316     ucsi->driver_data = data;
1317 }
1318 EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
1319 
1320 /**
1321  * ucsi_create - Allocate UCSI instance
1322  * @dev: Device interface to the PPM (Platform Policy Manager)
1323  * @ops: I/O routines
1324  */
1325 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
1326 {
1327     struct ucsi *ucsi;
1328 
1329     if (!ops || !ops->read || !ops->sync_write || !ops->async_write)
1330         return ERR_PTR(-EINVAL);
1331 
1332     ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
1333     if (!ucsi)
1334         return ERR_PTR(-ENOMEM);
1335 
1336     INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work);
1337     mutex_init(&ucsi->ppm_lock);
1338     ucsi->dev = dev;
1339     ucsi->ops = ops;
1340 
1341     return ucsi;
1342 }
1343 EXPORT_SYMBOL_GPL(ucsi_create);
1344 
1345 /**
1346  * ucsi_destroy - Free UCSI instance
1347  * @ucsi: UCSI instance to be freed
1348  */
1349 void ucsi_destroy(struct ucsi *ucsi)
1350 {
1351     kfree(ucsi);
1352 }
1353 EXPORT_SYMBOL_GPL(ucsi_destroy);
1354 
1355 /**
1356  * ucsi_register - Register UCSI interface
1357  * @ucsi: UCSI instance
1358  */
1359 int ucsi_register(struct ucsi *ucsi)
1360 {
1361     int ret;
1362 
1363     ret = ucsi->ops->read(ucsi, UCSI_VERSION, &ucsi->version,
1364                   sizeof(ucsi->version));
1365     if (ret)
1366         return ret;
1367 
1368     if (!ucsi->version)
1369         return -ENODEV;
1370 
1371     queue_delayed_work(system_long_wq, &ucsi->work, 0);
1372 
1373     return 0;
1374 }
1375 EXPORT_SYMBOL_GPL(ucsi_register);
1376 
1377 /**
1378  * ucsi_unregister - Unregister UCSI interface
1379  * @ucsi: UCSI interface to be unregistered
1380  *
1381  * Unregister UCSI interface that was created with ucsi_register().
1382  */
1383 void ucsi_unregister(struct ucsi *ucsi)
1384 {
1385     u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
1386     int i;
1387 
1388     /* Make sure that we are not in the middle of driver initialization */
1389     cancel_delayed_work_sync(&ucsi->work);
1390 
1391     /* Disable notifications */
1392     ucsi->ops->async_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
1393 
1394     for (i = 0; i < ucsi->cap.num_connectors; i++) {
1395         cancel_work_sync(&ucsi->connector[i].work);
1396         ucsi_unregister_partner(&ucsi->connector[i]);
1397         ucsi_unregister_altmodes(&ucsi->connector[i],
1398                      UCSI_RECIPIENT_CON);
1399         ucsi_unregister_port_psy(&ucsi->connector[i]);
1400         if (ucsi->connector[i].wq)
1401             destroy_workqueue(ucsi->connector[i].wq);
1402         typec_unregister_port(ucsi->connector[i].port);
1403     }
1404 
1405     kfree(ucsi->connector);
1406 }
1407 EXPORT_SYMBOL_GPL(ucsi_unregister);
1408 
1409 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1410 MODULE_LICENSE("GPL v2");
1411 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");