1 // SPDX-License-Identifier: GPL-2.0
3 * USB Type-C Connector System Software Interface driver
5 * Copyright (C) 2017, Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
9 #include <linux/completion.h>
10 #include <linux/property.h>
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/delay.h>
14 #include <linux/slab.h>
15 #include <linux/usb/typec_dp.h>
21 * UCSI_TIMEOUT_MS - PPM communication timeout
23 * Ideally we could use MIN_TIME_TO_RESPOND_WITH_BUSY (which is defined in UCSI
24 * specification) here as reference, but unfortunately we can't. It is very
25 * difficult to estimate the time it takes for the system to process the command
26 * before it is actually passed to the PPM.
28 #define UCSI_TIMEOUT_MS 5000
31 * UCSI_SWAP_TIMEOUT_MS - Timeout for role swap requests
33 * 5 seconds is close to the time it takes for CapsCounter to reach 0, so even
34 * if the PPM does not generate Connector Change events before that with
35 * partners that do not support USB Power Delivery, this should still work.
37 #define UCSI_SWAP_TIMEOUT_MS 5000
39 static int ucsi_read_message_in(struct ucsi *ucsi, void *buf,
43 * Below UCSI 2.0, MESSAGE_IN was limited to 16 bytes. Truncate the
46 if (ucsi->version <= UCSI_VERSION_1_2)
47 buf_size = clamp(buf_size, 0, 16);
49 return ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, buf, buf_size);
52 static int ucsi_acknowledge_command(struct ucsi *ucsi)
56 ctrl = UCSI_ACK_CC_CI;
57 ctrl |= UCSI_ACK_COMMAND_COMPLETE;
59 return ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &ctrl, sizeof(ctrl));
62 static int ucsi_acknowledge_connector_change(struct ucsi *ucsi)
66 ctrl = UCSI_ACK_CC_CI;
67 ctrl |= UCSI_ACK_CONNECTOR_CHANGE;
69 return ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &ctrl, sizeof(ctrl));
72 static int ucsi_exec_command(struct ucsi *ucsi, u64 command);
74 static int ucsi_read_error(struct ucsi *ucsi)
79 /* Acknowledge the command that failed */
80 ret = ucsi_acknowledge_command(ucsi);
84 ret = ucsi_exec_command(ucsi, UCSI_GET_ERROR_STATUS);
88 ret = ucsi_read_message_in(ucsi, &error, sizeof(error));
92 ret = ucsi_acknowledge_command(ucsi);
97 case UCSI_ERROR_INCOMPATIBLE_PARTNER:
99 case UCSI_ERROR_CC_COMMUNICATION_ERR:
101 case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
103 case UCSI_ERROR_DEAD_BATTERY:
104 dev_warn(ucsi->dev, "Dead battery condition!\n");
106 case UCSI_ERROR_INVALID_CON_NUM:
107 case UCSI_ERROR_UNREGONIZED_CMD:
108 case UCSI_ERROR_INVALID_CMD_ARGUMENT:
109 dev_err(ucsi->dev, "possible UCSI driver bug %u\n", error);
111 case UCSI_ERROR_OVERCURRENT:
112 dev_warn(ucsi->dev, "Overcurrent condition\n");
114 case UCSI_ERROR_PARTNER_REJECTED_SWAP:
115 dev_warn(ucsi->dev, "Partner rejected swap\n");
117 case UCSI_ERROR_HARD_RESET:
118 dev_warn(ucsi->dev, "Hard reset occurred\n");
120 case UCSI_ERROR_PPM_POLICY_CONFLICT:
121 dev_warn(ucsi->dev, "PPM Policy conflict\n");
123 case UCSI_ERROR_SWAP_REJECTED:
124 dev_warn(ucsi->dev, "Swap rejected\n");
126 case UCSI_ERROR_UNDEFINED:
128 dev_err(ucsi->dev, "unknown error %u\n", error);
135 static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd)
140 ret = ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
144 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
148 if (cmd != UCSI_CANCEL && cci & UCSI_CCI_BUSY)
149 return ucsi_exec_command(ucsi, UCSI_CANCEL);
151 if (!(cci & UCSI_CCI_COMMAND_COMPLETE))
154 if (cci & UCSI_CCI_NOT_SUPPORTED)
157 if (cci & UCSI_CCI_ERROR) {
158 if (cmd == UCSI_GET_ERROR_STATUS)
160 return ucsi_read_error(ucsi);
163 if (cmd == UCSI_CANCEL && cci & UCSI_CCI_CANCEL_COMPLETE) {
164 ret = ucsi_acknowledge_command(ucsi);
165 return ret ? ret : -EBUSY;
168 return UCSI_CCI_LENGTH(cci);
171 int ucsi_send_command(struct ucsi *ucsi, u64 command,
172 void *data, size_t size)
177 mutex_lock(&ucsi->ppm_lock);
179 ret = ucsi_exec_command(ucsi, command);
186 ret = ucsi_read_message_in(ucsi, data, size);
191 ret = ucsi_acknowledge_command(ucsi);
197 mutex_unlock(&ucsi->ppm_lock);
200 EXPORT_SYMBOL_GPL(ucsi_send_command);
202 /* -------------------------------------------------------------------------- */
205 struct delayed_work work;
206 struct list_head node;
209 struct ucsi_connector *con;
210 int (*cb)(struct ucsi_connector *);
213 static void ucsi_poll_worker(struct work_struct *work)
215 struct ucsi_work *uwork = container_of(work, struct ucsi_work, work.work);
216 struct ucsi_connector *con = uwork->con;
219 mutex_lock(&con->lock);
222 list_del(&uwork->node);
223 mutex_unlock(&con->lock);
228 ret = uwork->cb(con);
230 if (uwork->count-- && (ret == -EBUSY || ret == -ETIMEDOUT)) {
231 queue_delayed_work(con->wq, &uwork->work, uwork->delay);
233 list_del(&uwork->node);
237 mutex_unlock(&con->lock);
240 static int ucsi_partner_task(struct ucsi_connector *con,
241 int (*cb)(struct ucsi_connector *),
242 int retries, unsigned long delay)
244 struct ucsi_work *uwork;
249 uwork = kzalloc(sizeof(*uwork), GFP_KERNEL);
253 INIT_DELAYED_WORK(&uwork->work, ucsi_poll_worker);
254 uwork->count = retries;
255 uwork->delay = delay;
259 list_add_tail(&uwork->node, &con->partner_tasks);
260 queue_delayed_work(con->wq, &uwork->work, delay);
265 /* -------------------------------------------------------------------------- */
267 void ucsi_altmode_update_active(struct ucsi_connector *con)
269 const struct typec_altmode *altmode = NULL;
275 command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
276 ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur));
278 if (con->ucsi->version > 0x0100) {
279 dev_err(con->ucsi->dev,
280 "GET_CURRENT_CAM command failed\n");
286 if (cur < UCSI_MAX_ALTMODES)
287 altmode = typec_altmode_get_partner(con->port_altmode[cur]);
289 for (i = 0; con->partner_altmode[i]; i++)
290 typec_altmode_update_active(con->partner_altmode[i],
291 con->partner_altmode[i] == altmode);
294 static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
299 for (i = 0; alt[i]; i++) {
300 if (i > MODE_DISCOVERY_MAX)
303 if (alt[i]->svid == svid)
310 static int ucsi_next_altmode(struct typec_altmode **alt)
314 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
321 static int ucsi_get_num_altmode(struct typec_altmode **alt)
325 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
332 static int ucsi_register_altmode(struct ucsi_connector *con,
333 struct typec_altmode_desc *desc,
336 struct typec_altmode *alt;
341 override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
344 case UCSI_RECIPIENT_CON:
345 i = ucsi_next_altmode(con->port_altmode);
351 ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid);
357 switch (desc->svid) {
358 case USB_TYPEC_DP_SID:
359 alt = ucsi_register_displayport(con, override, i, desc);
361 case USB_TYPEC_NVIDIA_VLINK_SID:
362 if (desc->vdo == USB_TYPEC_NVIDIA_VLINK_DBG_VDO)
363 alt = typec_port_register_altmode(con->port,
366 alt = ucsi_register_displayport(con, override,
370 alt = typec_port_register_altmode(con->port, desc);
379 con->port_altmode[i] = alt;
381 case UCSI_RECIPIENT_SOP:
382 i = ucsi_next_altmode(con->partner_altmode);
388 ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid);
394 alt = typec_partner_register_altmode(con->partner, desc);
400 con->partner_altmode[i] = alt;
402 case UCSI_RECIPIENT_SOP_P:
403 i = ucsi_next_altmode(con->plug_altmode);
409 ret = ucsi_altmode_next_mode(con->plug_altmode, desc->svid);
415 alt = typec_plug_register_altmode(con->plug, desc);
421 con->plug_altmode[i] = alt;
427 trace_ucsi_register_altmode(recipient, alt);
432 dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n",
433 desc->svid, desc->mode);
439 ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient)
441 int max_altmodes = UCSI_MAX_ALTMODES;
442 struct typec_altmode_desc desc;
443 struct ucsi_altmode alt;
444 struct ucsi_altmode orig[UCSI_MAX_ALTMODES];
445 struct ucsi_altmode updated[UCSI_MAX_ALTMODES];
446 struct ucsi *ucsi = con->ucsi;
447 bool multi_dp = false;
454 if (recipient == UCSI_RECIPIENT_CON)
455 max_altmodes = con->ucsi->cap.num_alt_modes;
457 memset(orig, 0, sizeof(orig));
458 memset(updated, 0, sizeof(updated));
460 /* First get all the alternate modes */
461 for (i = 0; i < max_altmodes; i++) {
462 memset(&alt, 0, sizeof(alt));
463 command = UCSI_GET_ALTERNATE_MODES;
464 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
465 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
466 command |= UCSI_GET_ALTMODE_OFFSET(i);
467 len = ucsi_send_command(con->ucsi, command, &alt, sizeof(alt));
469 * We are collecting all altmodes first and then registering.
470 * Some type-C device will return zero length data beyond last
471 * alternate modes. We should not return if length is zero.
476 /* We got all altmodes, now break out and register them */
477 if (!len || !alt.svid)
480 orig[k].mid = alt.mid;
481 orig[k].svid = alt.svid;
485 * Update the original altmode table as some ppms may report
486 * multiple DP altmodes.
488 if (recipient == UCSI_RECIPIENT_CON)
489 multi_dp = ucsi->ops->update_altmodes(ucsi, orig, updated);
491 /* now register altmodes */
492 for (i = 0; i < max_altmodes; i++) {
493 memset(&desc, 0, sizeof(desc));
494 if (multi_dp && recipient == UCSI_RECIPIENT_CON) {
495 desc.svid = updated[i].svid;
496 desc.vdo = updated[i].mid;
498 desc.svid = orig[i].svid;
499 desc.vdo = orig[i].mid;
501 desc.roles = TYPEC_PORT_DRD;
506 ret = ucsi_register_altmode(con, &desc, recipient);
514 static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
516 int max_altmodes = UCSI_MAX_ALTMODES;
517 struct typec_altmode_desc desc;
518 struct ucsi_altmode alt[2];
526 if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
529 if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
532 if (con->ucsi->ops->update_altmodes)
533 return ucsi_register_altmodes_nvidia(con, recipient);
535 if (recipient == UCSI_RECIPIENT_CON)
536 max_altmodes = con->ucsi->cap.num_alt_modes;
538 for (i = 0; i < max_altmodes;) {
539 memset(alt, 0, sizeof(alt));
540 command = UCSI_GET_ALTERNATE_MODES;
541 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
542 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
543 command |= UCSI_GET_ALTMODE_OFFSET(i);
544 len = ucsi_send_command(con->ucsi, command, alt, sizeof(alt));
551 * This code is requesting one alt mode at a time, but some PPMs
552 * may still return two. If that happens both alt modes need be
553 * registered and the offset for the next alt mode has to be
556 num = len / sizeof(alt[0]);
559 for (j = 0; j < num; j++) {
563 memset(&desc, 0, sizeof(desc));
564 desc.vdo = alt[j].mid;
565 desc.svid = alt[j].svid;
566 desc.roles = TYPEC_PORT_DRD;
568 ret = ucsi_register_altmode(con, &desc, recipient);
577 static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
579 const struct typec_altmode *pdev;
580 struct typec_altmode **adev;
584 case UCSI_RECIPIENT_CON:
585 adev = con->port_altmode;
587 case UCSI_RECIPIENT_SOP:
588 adev = con->partner_altmode;
590 case UCSI_RECIPIENT_SOP_P:
591 adev = con->plug_altmode;
598 if (recipient == UCSI_RECIPIENT_SOP &&
599 (adev[i]->svid == USB_TYPEC_DP_SID ||
600 (adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID &&
601 adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) {
602 pdev = typec_altmode_get_partner(adev[i]);
603 ucsi_displayport_remove_partner((void *)pdev);
605 typec_unregister_altmode(adev[i]);
610 static int ucsi_read_pdos(struct ucsi_connector *con,
611 enum typec_role role, int is_partner,
612 u32 *pdos, int offset, int num_pdos)
614 struct ucsi *ucsi = con->ucsi;
618 if (ucsi->quirks & UCSI_NO_PARTNER_PDOS)
621 command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num);
622 command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner);
623 command |= UCSI_GET_PDOS_PDO_OFFSET(offset);
624 command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1);
625 command |= is_source(role) ? UCSI_GET_PDOS_SRC_PDOS : 0;
626 ret = ucsi_send_command(ucsi, command, pdos + offset,
627 num_pdos * sizeof(u32));
628 if (ret < 0 && ret != -ETIMEDOUT)
629 dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret);
634 static int ucsi_get_pdos(struct ucsi_connector *con, enum typec_role role,
635 int is_partner, u32 *pdos)
640 /* UCSI max payload means only getting at most 4 PDOs at a time */
641 ret = ucsi_read_pdos(con, role, is_partner, pdos, 0, UCSI_MAX_PDOS);
645 num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */
646 if (num_pdos < UCSI_MAX_PDOS)
649 /* get the remaining PDOs, if any */
650 ret = ucsi_read_pdos(con, role, is_partner, pdos, UCSI_MAX_PDOS,
651 PDO_MAX_OBJECTS - UCSI_MAX_PDOS);
655 return ret / sizeof(u32) + num_pdos;
658 static int ucsi_get_src_pdos(struct ucsi_connector *con)
662 ret = ucsi_get_pdos(con, TYPEC_SOURCE, 1, con->src_pdos);
668 ucsi_port_psy_changed(con);
673 static int ucsi_read_identity(struct ucsi_connector *con, u8 recipient,
674 u8 offset, u8 bytes, void *resp)
676 struct ucsi *ucsi = con->ucsi;
680 command = UCSI_COMMAND(UCSI_GET_PD_MESSAGE) |
681 UCSI_CONNECTOR_NUMBER(con->num);
682 command |= UCSI_GET_PD_MESSAGE_RECIPIENT(recipient);
683 command |= UCSI_GET_PD_MESSAGE_OFFSET(offset);
684 command |= UCSI_GET_PD_MESSAGE_BYTES(bytes);
685 command |= UCSI_GET_PD_MESSAGE_TYPE(UCSI_GET_PD_MESSAGE_TYPE_IDENTITY);
687 ret = ucsi_send_command(ucsi, command, resp, bytes);
689 dev_err(ucsi->dev, "UCSI_GET_PD_MESSAGE failed (%d)\n", ret);
694 static int ucsi_get_identity(struct ucsi_connector *con, u8 recipient,
695 struct usb_pd_identity *id)
697 struct ucsi *ucsi = con->ucsi;
698 struct ucsi_pd_message_disc_id resp = {};
701 if (ucsi->version < UCSI_VERSION_2_0) {
703 * Before UCSI v2.0, MESSAGE_IN is 16 bytes which cannot fit
704 * the 28 byte identity response including the VDM header.
705 * First request the VDM header, ID Header VDO, Cert Stat VDO
708 ret = ucsi_read_identity(con, recipient, 0, 0x10, &resp);
713 /* Then request Product Type VDO1 through Product Type VDO3. */
714 ret = ucsi_read_identity(con, recipient, 0x10, 0xc,
721 * In UCSI v2.0 and after, MESSAGE_IN is large enough to request
722 * the large enough to request the full Discover Identity
725 ret = ucsi_read_identity(con, recipient, 0x0, 0x1c, &resp);
730 id->id_header = resp.id_header;
731 id->cert_stat = resp.cert_stat;
732 id->product = resp.product;
733 id->vdo[0] = resp.vdo[0];
734 id->vdo[1] = resp.vdo[1];
735 id->vdo[2] = resp.vdo[2];
739 static int ucsi_get_partner_identity(struct ucsi_connector *con)
743 ret = ucsi_get_identity(con, UCSI_RECIPIENT_SOP,
744 &con->partner_identity);
748 ret = typec_partner_set_identity(con->partner);
750 dev_err(con->ucsi->dev, "Failed to set partner identity (%d)\n",
757 static int ucsi_get_cable_identity(struct ucsi_connector *con)
761 ret = ucsi_get_identity(con, UCSI_RECIPIENT_SOP_P,
762 &con->cable_identity);
766 ret = typec_cable_set_identity(con->cable);
768 dev_err(con->ucsi->dev, "Failed to set cable identity (%d)\n",
775 static int ucsi_check_altmodes(struct ucsi_connector *con)
777 int ret, num_partner_am;
779 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
780 if (ret && ret != -ETIMEDOUT)
781 dev_err(con->ucsi->dev,
782 "con%d: failed to register partner alt modes (%d)\n",
785 /* Ignoring the errors in this case. */
786 if (con->partner_altmode[0]) {
787 num_partner_am = ucsi_get_num_altmode(con->partner_altmode);
788 if (num_partner_am > 0)
789 typec_partner_set_num_altmodes(con->partner, num_partner_am);
790 ucsi_altmode_update_active(con);
797 static int ucsi_register_partner_pdos(struct ucsi_connector *con)
799 struct usb_power_delivery_desc desc = { con->ucsi->cap.pd_version };
800 struct usb_power_delivery_capabilities_desc caps;
801 struct usb_power_delivery_capabilities *cap;
807 con->partner_pd = usb_power_delivery_register(NULL, &desc);
808 if (IS_ERR(con->partner_pd))
809 return PTR_ERR(con->partner_pd);
811 ret = ucsi_get_pdos(con, TYPEC_SOURCE, 1, caps.pdo);
813 if (ret < PDO_MAX_OBJECTS)
816 caps.role = TYPEC_SOURCE;
817 cap = usb_power_delivery_register_capabilities(con->partner_pd, &caps);
821 con->partner_source_caps = cap;
823 ret = typec_partner_set_usb_power_delivery(con->partner, con->partner_pd);
825 usb_power_delivery_unregister_capabilities(con->partner_source_caps);
830 ret = ucsi_get_pdos(con, TYPEC_SINK, 1, caps.pdo);
832 if (ret < PDO_MAX_OBJECTS)
835 caps.role = TYPEC_SINK;
837 cap = usb_power_delivery_register_capabilities(con->partner_pd, &caps);
841 con->partner_sink_caps = cap;
843 ret = typec_partner_set_usb_power_delivery(con->partner, con->partner_pd);
845 usb_power_delivery_unregister_capabilities(con->partner_sink_caps);
853 static void ucsi_unregister_partner_pdos(struct ucsi_connector *con)
855 usb_power_delivery_unregister_capabilities(con->partner_sink_caps);
856 con->partner_sink_caps = NULL;
857 usb_power_delivery_unregister_capabilities(con->partner_source_caps);
858 con->partner_source_caps = NULL;
859 usb_power_delivery_unregister(con->partner_pd);
860 con->partner_pd = NULL;
863 static int ucsi_register_plug(struct ucsi_connector *con)
865 struct typec_plug *plug;
866 struct typec_plug_desc desc = {.index = TYPEC_PLUG_SOP_P};
868 plug = typec_register_plug(con->cable, &desc);
870 dev_err(con->ucsi->dev,
871 "con%d: failed to register plug (%ld)\n", con->num,
873 return PTR_ERR(plug);
880 static void ucsi_unregister_plug(struct ucsi_connector *con)
885 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP_P);
886 typec_unregister_plug(con->plug);
890 static int ucsi_register_cable(struct ucsi_connector *con)
892 struct typec_cable *cable;
893 struct typec_cable_desc desc = {};
895 switch (UCSI_CABLE_PROP_FLAG_PLUG_TYPE(con->cable_prop.flags)) {
896 case UCSI_CABLE_PROPERTY_PLUG_TYPE_A:
897 desc.type = USB_PLUG_TYPE_A;
899 case UCSI_CABLE_PROPERTY_PLUG_TYPE_B:
900 desc.type = USB_PLUG_TYPE_B;
902 case UCSI_CABLE_PROPERTY_PLUG_TYPE_C:
903 desc.type = USB_PLUG_TYPE_C;
906 desc.type = USB_PLUG_NONE;
910 desc.identity = &con->cable_identity;
911 desc.active = !!(UCSI_CABLE_PROP_FLAG_ACTIVE_CABLE &
912 con->cable_prop.flags);
913 desc.pd_revision = UCSI_CABLE_PROP_FLAG_PD_MAJOR_REV_AS_BCD(
914 con->cable_prop.flags);
916 cable = typec_register_cable(con->port, &desc);
918 dev_err(con->ucsi->dev,
919 "con%d: failed to register cable (%ld)\n", con->num,
921 return PTR_ERR(cable);
928 static void ucsi_unregister_cable(struct ucsi_connector *con)
933 ucsi_unregister_plug(con);
934 typec_unregister_cable(con->cable);
935 memset(&con->cable_identity, 0, sizeof(con->cable_identity));
939 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
941 switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
942 case UCSI_CONSTAT_PWR_OPMODE_PD:
943 con->rdo = con->status.request_data_obj;
944 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
945 ucsi_partner_task(con, ucsi_get_src_pdos, 30, 0);
946 ucsi_partner_task(con, ucsi_check_altmodes, 30, 0);
947 ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ);
949 case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
951 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
953 case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
955 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
959 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
964 static int ucsi_register_partner(struct ucsi_connector *con)
966 u8 pwr_opmode = UCSI_CONSTAT_PWR_OPMODE(con->status.flags);
967 struct typec_partner_desc desc;
968 struct typec_partner *partner;
973 memset(&desc, 0, sizeof(desc));
975 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
976 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
977 desc.accessory = TYPEC_ACCESSORY_DEBUG;
979 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
980 desc.accessory = TYPEC_ACCESSORY_AUDIO;
986 desc.identity = &con->partner_identity;
987 desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD;
988 desc.pd_revision = UCSI_CONCAP_FLAG_PARTNER_PD_MAJOR_REV_AS_BCD(con->cap.flags);
990 partner = typec_register_partner(con->port, &desc);
991 if (IS_ERR(partner)) {
992 dev_err(con->ucsi->dev,
993 "con%d: failed to register partner (%ld)\n", con->num,
995 return PTR_ERR(partner);
998 con->partner = partner;
1003 static void ucsi_unregister_partner(struct ucsi_connector *con)
1008 typec_set_mode(con->port, TYPEC_STATE_SAFE);
1010 typec_partner_set_usb_power_delivery(con->partner, NULL);
1011 ucsi_unregister_partner_pdos(con);
1012 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
1013 ucsi_unregister_cable(con);
1014 typec_unregister_partner(con->partner);
1015 memset(&con->partner_identity, 0, sizeof(con->partner_identity));
1016 con->partner = NULL;
1019 static void ucsi_partner_change(struct ucsi_connector *con)
1021 enum usb_role u_role = USB_ROLE_NONE;
1024 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1025 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1026 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1027 u_role = USB_ROLE_HOST;
1029 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1030 typec_set_data_role(con->port, TYPEC_HOST);
1032 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1033 u_role = USB_ROLE_DEVICE;
1034 typec_set_data_role(con->port, TYPEC_DEVICE);
1040 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1041 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1042 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
1043 typec_set_mode(con->port, TYPEC_MODE_DEBUG);
1045 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
1046 typec_set_mode(con->port, TYPEC_MODE_AUDIO);
1049 if (UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) ==
1050 UCSI_CONSTAT_PARTNER_FLAG_USB)
1051 typec_set_mode(con->port, TYPEC_STATE_USB);
1055 /* Only notify USB controller if partner supports USB data */
1056 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
1057 u_role = USB_ROLE_NONE;
1059 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1061 dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n",
1065 static int ucsi_check_connector_capability(struct ucsi_connector *con)
1070 if (!con->partner || con->ucsi->version < UCSI_VERSION_2_0)
1073 command = UCSI_GET_CONNECTOR_CAPABILITY | UCSI_CONNECTOR_NUMBER(con->num);
1074 ret = ucsi_send_command(con->ucsi, command, &con->cap, sizeof(con->cap));
1076 dev_err(con->ucsi->dev, "GET_CONNECTOR_CAPABILITY failed (%d)\n", ret);
1080 typec_partner_set_pd_revision(con->partner,
1081 UCSI_CONCAP_FLAG_PARTNER_PD_MAJOR_REV_AS_BCD(con->cap.flags));
1086 static int ucsi_check_connection(struct ucsi_connector *con)
1088 u8 prev_flags = con->status.flags;
1092 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
1093 ret = ucsi_send_command(con->ucsi, command, &con->status, sizeof(con->status));
1095 dev_err(con->ucsi->dev, "GET_CONNECTOR_STATUS failed (%d)\n", ret);
1099 if (con->status.flags == prev_flags)
1102 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1103 ucsi_register_partner(con);
1104 ucsi_pwr_opmode_change(con);
1105 ucsi_partner_change(con);
1107 ucsi_partner_change(con);
1108 ucsi_port_psy_changed(con);
1109 ucsi_unregister_partner(con);
1115 static int ucsi_check_cable(struct ucsi_connector *con)
1123 command = UCSI_GET_CABLE_PROPERTY | UCSI_CONNECTOR_NUMBER(con->num);
1124 ret = ucsi_send_command(con->ucsi, command, &con->cable_prop,
1125 sizeof(con->cable_prop));
1127 dev_err(con->ucsi->dev, "GET_CABLE_PROPERTY failed (%d)\n",
1132 ret = ucsi_register_cable(con);
1136 ret = ucsi_get_cable_identity(con);
1140 ret = ucsi_register_plug(con);
1144 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP_P);
1151 static void ucsi_handle_connector_change(struct work_struct *work)
1153 struct ucsi_connector *con = container_of(work, struct ucsi_connector,
1155 struct ucsi *ucsi = con->ucsi;
1156 enum typec_role role;
1160 mutex_lock(&con->lock);
1162 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
1163 ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
1165 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
1167 clear_bit(EVENT_PENDING, &con->ucsi->flags);
1171 trace_ucsi_connector_change(con->num, &con->status);
1173 role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
1175 if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
1176 typec_set_pwr_role(con->port, role);
1178 /* Complete pending power role swap */
1179 if (!completion_done(&con->complete))
1180 complete(&con->complete);
1183 if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
1184 typec_set_pwr_role(con->port, role);
1185 ucsi_port_psy_changed(con);
1186 ucsi_partner_change(con);
1188 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1189 ucsi_register_partner(con);
1190 ucsi_partner_task(con, ucsi_check_connection, 1, HZ);
1191 ucsi_partner_task(con, ucsi_check_connector_capability, 1, HZ);
1192 ucsi_partner_task(con, ucsi_get_partner_identity, 1, HZ);
1193 ucsi_partner_task(con, ucsi_check_cable, 1, HZ);
1195 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) ==
1196 UCSI_CONSTAT_PWR_OPMODE_PD)
1197 ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ);
1199 ucsi_unregister_partner(con);
1203 if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE ||
1204 con->status.change & UCSI_CONSTAT_POWER_LEVEL_CHANGE)
1205 ucsi_pwr_opmode_change(con);
1207 if (con->partner && con->status.change & UCSI_CONSTAT_PARTNER_CHANGE) {
1208 ucsi_partner_change(con);
1210 /* Complete pending data role swap */
1211 if (!completion_done(&con->complete))
1212 complete(&con->complete);
1215 if (con->status.change & UCSI_CONSTAT_CAM_CHANGE)
1216 ucsi_partner_task(con, ucsi_check_altmodes, 1, 0);
1218 clear_bit(EVENT_PENDING, &con->ucsi->flags);
1220 mutex_lock(&ucsi->ppm_lock);
1221 ret = ucsi_acknowledge_connector_change(ucsi);
1222 mutex_unlock(&ucsi->ppm_lock);
1224 dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret);
1227 mutex_unlock(&con->lock);
1231 * ucsi_connector_change - Process Connector Change Event
1232 * @ucsi: UCSI Interface
1233 * @num: Connector number
1235 void ucsi_connector_change(struct ucsi *ucsi, u8 num)
1237 struct ucsi_connector *con = &ucsi->connector[num - 1];
1239 if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
1240 dev_dbg(ucsi->dev, "Bogus connector change event\n");
1244 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
1245 schedule_work(&con->work);
1247 EXPORT_SYMBOL_GPL(ucsi_connector_change);
1249 /* -------------------------------------------------------------------------- */
1251 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
1255 command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num);
1256 command |= hard ? UCSI_CONNECTOR_RESET_HARD : 0;
1258 return ucsi_send_command(con->ucsi, command, NULL, 0);
1261 static int ucsi_reset_ppm(struct ucsi *ucsi)
1263 u64 command = UCSI_PPM_RESET;
1268 mutex_lock(&ucsi->ppm_lock);
1270 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command,
1275 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
1278 if (time_is_before_jiffies(tmo)) {
1283 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
1287 /* If the PPM is still doing something else, reset it again. */
1288 if (cci & ~UCSI_CCI_RESET_COMPLETE) {
1289 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL,
1297 } while (!(cci & UCSI_CCI_RESET_COMPLETE));
1300 mutex_unlock(&ucsi->ppm_lock);
1304 static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)
1308 ret = ucsi_send_command(con->ucsi, command, NULL, 0);
1309 if (ret == -ETIMEDOUT) {
1312 /* PPM most likely stopped responding. Resetting everything. */
1313 ucsi_reset_ppm(con->ucsi);
1315 c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
1316 ucsi_send_command(con->ucsi, c, NULL, 0);
1318 ucsi_reset_connector(con, true);
1324 static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
1326 struct ucsi_connector *con = typec_get_drvdata(port);
1331 mutex_lock(&con->lock);
1333 if (!con->partner) {
1338 partner_type = UCSI_CONSTAT_PARTNER_TYPE(con->status.flags);
1339 if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
1340 role == TYPEC_DEVICE) ||
1341 (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
1342 role == TYPEC_HOST))
1345 reinit_completion(&con->complete);
1347 command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num);
1348 command |= UCSI_SET_UOR_ROLE(role);
1349 command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS;
1350 ret = ucsi_role_cmd(con, command);
1354 mutex_unlock(&con->lock);
1356 if (!wait_for_completion_timeout(&con->complete,
1357 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1363 mutex_unlock(&con->lock);
1368 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
1370 struct ucsi_connector *con = typec_get_drvdata(port);
1371 enum typec_role cur_role;
1375 mutex_lock(&con->lock);
1377 if (!con->partner) {
1382 cur_role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
1384 if (cur_role == role)
1387 reinit_completion(&con->complete);
1389 command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num);
1390 command |= UCSI_SET_PDR_ROLE(role);
1391 command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS;
1392 ret = ucsi_role_cmd(con, command);
1396 mutex_unlock(&con->lock);
1398 if (!wait_for_completion_timeout(&con->complete,
1399 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1402 mutex_lock(&con->lock);
1404 /* Something has gone wrong while swapping the role */
1405 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) !=
1406 UCSI_CONSTAT_PWR_OPMODE_PD) {
1407 ucsi_reset_connector(con, true);
1412 mutex_unlock(&con->lock);
1417 static const struct typec_operations ucsi_ops = {
1418 .dr_set = ucsi_dr_swap,
1419 .pr_set = ucsi_pr_swap
1422 /* Caller must call fwnode_handle_put() after use */
1423 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
1425 struct fwnode_handle *fwnode;
1428 device_for_each_child_node(con->ucsi->dev, fwnode)
1429 if (i++ == con->num)
1434 static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con)
1436 struct usb_power_delivery_desc desc = { ucsi->cap.pd_version};
1437 struct usb_power_delivery_capabilities_desc pd_caps;
1438 struct usb_power_delivery_capabilities *pd_cap;
1439 struct typec_capability *cap = &con->typec_cap;
1440 enum typec_accessory *accessory = cap->accessory;
1441 enum usb_role u_role = USB_ROLE_NONE;
1446 name = kasprintf(GFP_KERNEL, "%s-con%d", dev_name(ucsi->dev), con->num);
1450 con->wq = create_singlethread_workqueue(name);
1455 INIT_WORK(&con->work, ucsi_handle_connector_change);
1456 init_completion(&con->complete);
1457 mutex_init(&con->lock);
1458 INIT_LIST_HEAD(&con->partner_tasks);
1461 cap->fwnode = ucsi_find_fwnode(con);
1462 con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode);
1463 if (IS_ERR(con->usb_role_sw))
1464 return dev_err_probe(ucsi->dev, PTR_ERR(con->usb_role_sw),
1465 "con%d: failed to get usb role switch\n", con->num);
1467 /* Delay other interactions with the con until registration is complete */
1468 mutex_lock(&con->lock);
1470 /* Get connector capability */
1471 command = UCSI_GET_CONNECTOR_CAPABILITY;
1472 command |= UCSI_CONNECTOR_NUMBER(con->num);
1473 ret = ucsi_send_command(ucsi, command, &con->cap, sizeof(con->cap));
1477 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP)
1478 cap->data = TYPEC_PORT_DRD;
1479 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP)
1480 cap->data = TYPEC_PORT_DFP;
1481 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP)
1482 cap->data = TYPEC_PORT_UFP;
1484 if ((con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER) &&
1485 (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER))
1486 cap->type = TYPEC_PORT_DRP;
1487 else if (con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER)
1488 cap->type = TYPEC_PORT_SRC;
1489 else if (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER)
1490 cap->type = TYPEC_PORT_SNK;
1492 cap->revision = ucsi->cap.typec_version;
1493 cap->pd_revision = ucsi->cap.pd_version;
1494 cap->svdm_version = SVDM_VER_2_0;
1495 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
1497 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY)
1498 *accessory++ = TYPEC_ACCESSORY_AUDIO;
1499 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
1500 *accessory = TYPEC_ACCESSORY_DEBUG;
1502 cap->driver_data = con;
1503 cap->ops = &ucsi_ops;
1505 ret = ucsi_register_port_psy(con);
1509 /* Register the connector */
1510 con->port = typec_register_port(ucsi->dev, cap);
1511 if (IS_ERR(con->port)) {
1512 ret = PTR_ERR(con->port);
1516 con->pd = usb_power_delivery_register(ucsi->dev, &desc);
1518 ret = ucsi_get_pdos(con, TYPEC_SOURCE, 0, pd_caps.pdo);
1520 if (ret < PDO_MAX_OBJECTS)
1521 pd_caps.pdo[ret] = 0;
1523 pd_caps.role = TYPEC_SOURCE;
1524 pd_cap = usb_power_delivery_register_capabilities(con->pd, &pd_caps);
1525 if (IS_ERR(pd_cap)) {
1526 ret = PTR_ERR(pd_cap);
1530 con->port_source_caps = pd_cap;
1531 typec_port_set_usb_power_delivery(con->port, con->pd);
1534 memset(&pd_caps, 0, sizeof(pd_caps));
1535 ret = ucsi_get_pdos(con, TYPEC_SINK, 0, pd_caps.pdo);
1537 if (ret < PDO_MAX_OBJECTS)
1538 pd_caps.pdo[ret] = 0;
1540 pd_caps.role = TYPEC_SINK;
1541 pd_cap = usb_power_delivery_register_capabilities(con->pd, &pd_caps);
1542 if (IS_ERR(pd_cap)) {
1543 ret = PTR_ERR(pd_cap);
1547 con->port_sink_caps = pd_cap;
1548 typec_port_set_usb_power_delivery(con->port, con->pd);
1551 /* Alternate modes */
1552 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON);
1554 dev_err(ucsi->dev, "con%d: failed to register alt modes\n",
1559 /* Get the status */
1560 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
1561 ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
1563 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
1567 ret = 0; /* ucsi_send_command() returns length on success */
1569 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1570 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1571 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1572 u_role = USB_ROLE_HOST;
1574 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1575 typec_set_data_role(con->port, TYPEC_HOST);
1577 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1578 u_role = USB_ROLE_DEVICE;
1579 typec_set_data_role(con->port, TYPEC_DEVICE);
1585 /* Check if there is already something connected */
1586 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1587 typec_set_pwr_role(con->port,
1588 !!(con->status.flags & UCSI_CONSTAT_PWR_DIR));
1589 ucsi_register_partner(con);
1590 ucsi_pwr_opmode_change(con);
1591 ucsi_port_psy_changed(con);
1592 ucsi_get_partner_identity(con);
1593 ucsi_check_cable(con);
1596 /* Only notify USB controller if partner supports USB data */
1597 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
1598 u_role = USB_ROLE_NONE;
1600 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1602 dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n",
1608 UCSI_CONSTAT_PWR_OPMODE(con->status.flags) ==
1609 UCSI_CONSTAT_PWR_OPMODE_PD) {
1610 ucsi_get_src_pdos(con);
1611 ucsi_check_altmodes(con);
1614 trace_ucsi_register_port(con->num, &con->status);
1617 fwnode_handle_put(cap->fwnode);
1619 mutex_unlock(&con->lock);
1621 if (ret && con->wq) {
1622 destroy_workqueue(con->wq);
1630 * ucsi_init - Initialize UCSI interface
1631 * @ucsi: UCSI to be initialized
1633 * Registers all ports @ucsi has and enables all notification events.
1635 static int ucsi_init(struct ucsi *ucsi)
1637 struct ucsi_connector *con, *connector;
1643 ret = ucsi_reset_ppm(ucsi);
1645 dev_err(ucsi->dev, "failed to reset PPM!\n");
1649 /* Enable basic notifications */
1650 ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
1651 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1652 ret = ucsi_send_command(ucsi, command, NULL, 0);
1656 /* Get PPM capabilities */
1657 command = UCSI_GET_CAPABILITY;
1658 ret = ucsi_send_command(ucsi, command, &ucsi->cap, sizeof(ucsi->cap));
1662 if (!ucsi->cap.num_connectors) {
1667 /* Allocate the connectors. Released in ucsi_unregister() */
1668 connector = kcalloc(ucsi->cap.num_connectors + 1, sizeof(*connector), GFP_KERNEL);
1674 /* Register all connectors */
1675 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1676 connector[i].num = i + 1;
1677 ret = ucsi_register_port(ucsi, &connector[i]);
1679 goto err_unregister;
1682 /* Enable all notifications */
1683 ntfy = UCSI_ENABLE_NTFY_ALL;
1684 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1685 ret = ucsi_send_command(ucsi, command, NULL, 0);
1687 goto err_unregister;
1689 ucsi->connector = connector;
1694 for (con = connector; con->port; con++) {
1695 ucsi_unregister_partner(con);
1696 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON);
1697 ucsi_unregister_port_psy(con);
1699 destroy_workqueue(con->wq);
1701 usb_power_delivery_unregister_capabilities(con->port_sink_caps);
1702 con->port_sink_caps = NULL;
1703 usb_power_delivery_unregister_capabilities(con->port_source_caps);
1704 con->port_source_caps = NULL;
1705 usb_power_delivery_unregister(con->pd);
1707 typec_unregister_port(con->port);
1712 memset(&ucsi->cap, 0, sizeof(ucsi->cap));
1713 ucsi_reset_ppm(ucsi);
1718 static void ucsi_resume_work(struct work_struct *work)
1720 struct ucsi *ucsi = container_of(work, struct ucsi, resume_work);
1721 struct ucsi_connector *con;
1725 /* Restore UCSI notification enable mask after system resume */
1726 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1727 ret = ucsi_send_command(ucsi, command, NULL, 0);
1729 dev_err(ucsi->dev, "failed to re-enable notifications (%d)\n", ret);
1733 for (con = ucsi->connector; con->port; con++) {
1734 mutex_lock(&con->lock);
1735 ucsi_partner_task(con, ucsi_check_connection, 1, 0);
1736 mutex_unlock(&con->lock);
1740 int ucsi_resume(struct ucsi *ucsi)
1742 if (ucsi->connector)
1743 queue_work(system_long_wq, &ucsi->resume_work);
1746 EXPORT_SYMBOL_GPL(ucsi_resume);
1748 static void ucsi_init_work(struct work_struct *work)
1750 struct ucsi *ucsi = container_of(work, struct ucsi, work.work);
1753 ret = ucsi_init(ucsi);
1755 dev_err_probe(ucsi->dev, ret, "PPM init failed\n");
1757 if (ret == -EPROBE_DEFER) {
1758 if (ucsi->work_count++ > UCSI_ROLE_SWITCH_WAIT_COUNT) {
1759 dev_err(ucsi->dev, "PPM init failed, stop trying\n");
1763 queue_delayed_work(system_long_wq, &ucsi->work,
1764 UCSI_ROLE_SWITCH_INTERVAL);
1769 * ucsi_get_drvdata - Return private driver data pointer
1770 * @ucsi: UCSI interface
1772 void *ucsi_get_drvdata(struct ucsi *ucsi)
1774 return ucsi->driver_data;
1776 EXPORT_SYMBOL_GPL(ucsi_get_drvdata);
1779 * ucsi_set_drvdata - Assign private driver data pointer
1780 * @ucsi: UCSI interface
1781 * @data: Private data pointer
1783 void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
1785 ucsi->driver_data = data;
1787 EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
1790 * ucsi_create - Allocate UCSI instance
1791 * @dev: Device interface to the PPM (Platform Policy Manager)
1792 * @ops: I/O routines
1794 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
1798 if (!ops || !ops->read || !ops->sync_write || !ops->async_write)
1799 return ERR_PTR(-EINVAL);
1801 ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
1803 return ERR_PTR(-ENOMEM);
1805 INIT_WORK(&ucsi->resume_work, ucsi_resume_work);
1806 INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work);
1807 mutex_init(&ucsi->ppm_lock);
1813 EXPORT_SYMBOL_GPL(ucsi_create);
1816 * ucsi_destroy - Free UCSI instance
1817 * @ucsi: UCSI instance to be freed
1819 void ucsi_destroy(struct ucsi *ucsi)
1821 ucsi_debugfs_unregister(ucsi);
1824 EXPORT_SYMBOL_GPL(ucsi_destroy);
1827 * ucsi_register - Register UCSI interface
1828 * @ucsi: UCSI instance
1830 int ucsi_register(struct ucsi *ucsi)
1834 ret = ucsi->ops->read(ucsi, UCSI_VERSION, &ucsi->version,
1835 sizeof(ucsi->version));
1843 * Version format is JJ.M.N (JJ = Major version, M = Minor version,
1844 * N = sub-minor version).
1846 dev_dbg(ucsi->dev, "Registered UCSI interface with version %x.%x.%x",
1847 UCSI_BCD_GET_MAJOR(ucsi->version),
1848 UCSI_BCD_GET_MINOR(ucsi->version),
1849 UCSI_BCD_GET_SUBMINOR(ucsi->version));
1851 queue_delayed_work(system_long_wq, &ucsi->work, 0);
1853 ucsi_debugfs_register(ucsi);
1856 EXPORT_SYMBOL_GPL(ucsi_register);
1859 * ucsi_unregister - Unregister UCSI interface
1860 * @ucsi: UCSI interface to be unregistered
1862 * Unregister UCSI interface that was created with ucsi_register().
1864 void ucsi_unregister(struct ucsi *ucsi)
1866 u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
1869 /* Make sure that we are not in the middle of driver initialization */
1870 cancel_delayed_work_sync(&ucsi->work);
1871 cancel_work_sync(&ucsi->resume_work);
1873 /* Disable notifications */
1874 ucsi->ops->async_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
1876 if (!ucsi->connector)
1879 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1880 cancel_work_sync(&ucsi->connector[i].work);
1881 ucsi_unregister_partner(&ucsi->connector[i]);
1882 ucsi_unregister_altmodes(&ucsi->connector[i],
1883 UCSI_RECIPIENT_CON);
1884 ucsi_unregister_port_psy(&ucsi->connector[i]);
1886 if (ucsi->connector[i].wq) {
1887 struct ucsi_work *uwork;
1889 mutex_lock(&ucsi->connector[i].lock);
1891 * queue delayed items immediately so they can execute
1892 * and free themselves before the wq is destroyed
1894 list_for_each_entry(uwork, &ucsi->connector[i].partner_tasks, node)
1895 mod_delayed_work(ucsi->connector[i].wq, &uwork->work, 0);
1896 mutex_unlock(&ucsi->connector[i].lock);
1897 destroy_workqueue(ucsi->connector[i].wq);
1900 usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_sink_caps);
1901 ucsi->connector[i].port_sink_caps = NULL;
1902 usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_source_caps);
1903 ucsi->connector[i].port_source_caps = NULL;
1904 usb_power_delivery_unregister(ucsi->connector[i].pd);
1905 ucsi->connector[i].pd = NULL;
1906 typec_unregister_port(ucsi->connector[i].port);
1909 kfree(ucsi->connector);
1911 EXPORT_SYMBOL_GPL(ucsi_unregister);
1913 static int __init ucsi_module_init(void)
1915 ucsi_debugfs_init();
1918 module_init(ucsi_module_init);
1920 static void __exit ucsi_module_exit(void)
1922 ucsi_debugfs_exit();
1924 module_exit(ucsi_module_exit);
1926 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1927 MODULE_LICENSE("GPL v2");
1928 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");