1 // SPDX-License-Identifier: GPL-2.0
3 * USB Type-C Connector System Software Interface driver
5 * Copyright (C) 2017, Intel Corporation
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 void ucsi_notify_common(struct ucsi *ucsi, u32 cci)
41 if (UCSI_CCI_CONNECTOR(cci))
42 ucsi_connector_change(ucsi, UCSI_CCI_CONNECTOR(cci));
44 if (cci & UCSI_CCI_ACK_COMPLETE &&
45 test_bit(ACK_PENDING, &ucsi->flags))
46 complete(&ucsi->complete);
48 if (cci & UCSI_CCI_COMMAND_COMPLETE &&
49 test_bit(COMMAND_PENDING, &ucsi->flags))
50 complete(&ucsi->complete);
52 EXPORT_SYMBOL_GPL(ucsi_notify_common);
54 int ucsi_sync_control_common(struct ucsi *ucsi, u64 command)
56 bool ack = UCSI_COMMAND(command) == UCSI_ACK_CC_CI;
60 set_bit(ACK_PENDING, &ucsi->flags);
62 set_bit(COMMAND_PENDING, &ucsi->flags);
64 ret = ucsi->ops->async_control(ucsi, command);
68 if (!wait_for_completion_timeout(&ucsi->complete, 5 * HZ))
73 clear_bit(ACK_PENDING, &ucsi->flags);
75 clear_bit(COMMAND_PENDING, &ucsi->flags);
79 EXPORT_SYMBOL_GPL(ucsi_sync_control_common);
81 static int ucsi_acknowledge(struct ucsi *ucsi, bool conn_ack)
85 ctrl = UCSI_ACK_CC_CI;
86 ctrl |= UCSI_ACK_COMMAND_COMPLETE;
88 clear_bit(EVENT_PENDING, &ucsi->flags);
89 ctrl |= UCSI_ACK_CONNECTOR_CHANGE;
92 return ucsi->ops->sync_control(ucsi, ctrl);
95 static int ucsi_run_command(struct ucsi *ucsi, u64 command, u32 *cci,
96 void *data, size_t size, bool conn_ack)
103 * Below UCSI 2.0, MESSAGE_IN was limited to 16 bytes. Truncate the
106 if (ucsi->version <= UCSI_VERSION_1_2)
107 size = clamp(size, 0, 16);
109 ret = ucsi->ops->sync_control(ucsi, command);
113 ret = ucsi->ops->read_cci(ucsi, cci);
117 if (*cci & UCSI_CCI_BUSY)
120 if (!(*cci & UCSI_CCI_COMMAND_COMPLETE))
123 if (*cci & UCSI_CCI_NOT_SUPPORTED)
125 else if (*cci & UCSI_CCI_ERROR)
130 if (!err && data && UCSI_CCI_LENGTH(*cci))
131 err = ucsi->ops->read_message_in(ucsi, data, size);
134 * Don't ACK connection change if there was an error.
136 ret = ucsi_acknowledge(ucsi, err ? false : conn_ack);
143 static int ucsi_read_error(struct ucsi *ucsi, u8 connector_num)
150 command = UCSI_GET_ERROR_STATUS | UCSI_CONNECTOR_NUMBER(connector_num);
151 ret = ucsi_run_command(ucsi, command, &cci,
152 &error, sizeof(error), false);
154 if (cci & UCSI_CCI_BUSY) {
155 ret = ucsi_run_command(ucsi, UCSI_CANCEL, &cci, NULL, 0, false);
157 return ret ? ret : -EBUSY;
163 if (cci & UCSI_CCI_ERROR)
167 case UCSI_ERROR_INCOMPATIBLE_PARTNER:
169 case UCSI_ERROR_CC_COMMUNICATION_ERR:
171 case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
173 case UCSI_ERROR_DEAD_BATTERY:
174 dev_warn(ucsi->dev, "Dead battery condition!\n");
176 case UCSI_ERROR_INVALID_CON_NUM:
177 case UCSI_ERROR_UNREGONIZED_CMD:
178 case UCSI_ERROR_INVALID_CMD_ARGUMENT:
179 dev_err(ucsi->dev, "possible UCSI driver bug %u\n", error);
181 case UCSI_ERROR_OVERCURRENT:
182 dev_warn(ucsi->dev, "Overcurrent condition\n");
184 case UCSI_ERROR_PARTNER_REJECTED_SWAP:
185 dev_warn(ucsi->dev, "Partner rejected swap\n");
187 case UCSI_ERROR_HARD_RESET:
188 dev_warn(ucsi->dev, "Hard reset occurred\n");
190 case UCSI_ERROR_PPM_POLICY_CONFLICT:
191 dev_warn(ucsi->dev, "PPM Policy conflict\n");
193 case UCSI_ERROR_SWAP_REJECTED:
194 dev_warn(ucsi->dev, "Swap rejected\n");
196 case UCSI_ERROR_REVERSE_CURRENT_PROTECTION:
197 dev_warn(ucsi->dev, "Reverse Current Protection detected\n");
199 case UCSI_ERROR_SET_SINK_PATH_REJECTED:
200 dev_warn(ucsi->dev, "Set Sink Path rejected\n");
202 case UCSI_ERROR_UNDEFINED:
204 dev_err(ucsi->dev, "unknown error %u\n", error);
211 static int ucsi_send_command_common(struct ucsi *ucsi, u64 cmd,
212 void *data, size_t size, bool conn_ack)
218 if (ucsi->version > UCSI_VERSION_1_2) {
219 switch (UCSI_COMMAND(cmd)) {
220 case UCSI_GET_ALTERNATE_MODES:
221 connector_num = UCSI_GET_ALTMODE_GET_CONNECTOR_NUMBER(cmd);
226 case UCSI_SET_NOTIFICATION_ENABLE:
227 case UCSI_GET_CAPABILITY:
231 connector_num = UCSI_DEFAULT_GET_CONNECTOR_NUMBER(cmd);
238 mutex_lock(&ucsi->ppm_lock);
240 ret = ucsi_run_command(ucsi, cmd, &cci, data, size, conn_ack);
241 if (cci & UCSI_CCI_BUSY)
242 ret = ucsi_run_command(ucsi, UCSI_CANCEL, &cci, NULL, 0, false) ?: -EBUSY;
243 else if (cci & UCSI_CCI_ERROR)
244 ret = ucsi_read_error(ucsi, connector_num);
246 mutex_unlock(&ucsi->ppm_lock);
250 int ucsi_send_command(struct ucsi *ucsi, u64 command,
251 void *data, size_t size)
253 return ucsi_send_command_common(ucsi, command, data, size, false);
255 EXPORT_SYMBOL_GPL(ucsi_send_command);
257 /* -------------------------------------------------------------------------- */
260 struct delayed_work work;
261 struct list_head node;
264 struct ucsi_connector *con;
265 int (*cb)(struct ucsi_connector *);
268 static void ucsi_poll_worker(struct work_struct *work)
270 struct ucsi_work *uwork = container_of(work, struct ucsi_work, work.work);
271 struct ucsi_connector *con = uwork->con;
274 mutex_lock(&con->lock);
277 list_del(&uwork->node);
278 mutex_unlock(&con->lock);
283 ret = uwork->cb(con);
285 if (uwork->count-- && (ret == -EBUSY || ret == -ETIMEDOUT)) {
286 queue_delayed_work(con->wq, &uwork->work, uwork->delay);
288 list_del(&uwork->node);
292 mutex_unlock(&con->lock);
295 static int ucsi_partner_task(struct ucsi_connector *con,
296 int (*cb)(struct ucsi_connector *),
297 int retries, unsigned long delay)
299 struct ucsi_work *uwork;
304 uwork = kzalloc(sizeof(*uwork), GFP_KERNEL);
308 INIT_DELAYED_WORK(&uwork->work, ucsi_poll_worker);
309 uwork->count = retries;
310 uwork->delay = delay;
314 list_add_tail(&uwork->node, &con->partner_tasks);
315 queue_delayed_work(con->wq, &uwork->work, delay);
320 /* -------------------------------------------------------------------------- */
322 void ucsi_altmode_update_active(struct ucsi_connector *con)
324 const struct typec_altmode *altmode = NULL;
330 command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
331 ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur));
333 if (con->ucsi->version > 0x0100) {
334 dev_err(con->ucsi->dev,
335 "GET_CURRENT_CAM command failed\n");
341 if (cur < UCSI_MAX_ALTMODES)
342 altmode = typec_altmode_get_partner(con->port_altmode[cur]);
344 for (i = 0; con->partner_altmode[i]; i++)
345 typec_altmode_update_active(con->partner_altmode[i],
346 con->partner_altmode[i] == altmode);
349 static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
354 for (i = 0; alt[i]; i++) {
355 if (i > MODE_DISCOVERY_MAX)
358 if (alt[i]->svid == svid)
365 static int ucsi_next_altmode(struct typec_altmode **alt)
369 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
376 static int ucsi_get_num_altmode(struct typec_altmode **alt)
380 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
387 static int ucsi_register_altmode(struct ucsi_connector *con,
388 struct typec_altmode_desc *desc,
391 struct typec_altmode *alt;
396 override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
399 case UCSI_RECIPIENT_CON:
400 i = ucsi_next_altmode(con->port_altmode);
406 ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid);
412 switch (desc->svid) {
413 case USB_TYPEC_DP_SID:
414 alt = ucsi_register_displayport(con, override, i, desc);
416 case USB_TYPEC_NVIDIA_VLINK_SID:
417 if (desc->vdo == USB_TYPEC_NVIDIA_VLINK_DBG_VDO)
418 alt = typec_port_register_altmode(con->port,
421 alt = ucsi_register_displayport(con, override,
425 alt = typec_port_register_altmode(con->port, desc);
434 con->port_altmode[i] = alt;
436 case UCSI_RECIPIENT_SOP:
437 i = ucsi_next_altmode(con->partner_altmode);
443 ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid);
449 alt = typec_partner_register_altmode(con->partner, desc);
455 con->partner_altmode[i] = alt;
457 case UCSI_RECIPIENT_SOP_P:
458 i = ucsi_next_altmode(con->plug_altmode);
464 ret = ucsi_altmode_next_mode(con->plug_altmode, desc->svid);
470 alt = typec_plug_register_altmode(con->plug, desc);
476 con->plug_altmode[i] = alt;
482 trace_ucsi_register_altmode(recipient, alt);
487 dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n",
488 desc->svid, desc->mode);
494 ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient)
496 int max_altmodes = UCSI_MAX_ALTMODES;
497 struct typec_altmode_desc desc;
498 struct ucsi_altmode alt;
499 struct ucsi_altmode orig[UCSI_MAX_ALTMODES];
500 struct ucsi_altmode updated[UCSI_MAX_ALTMODES];
501 struct ucsi *ucsi = con->ucsi;
502 bool multi_dp = false;
509 if (recipient == UCSI_RECIPIENT_CON)
510 max_altmodes = con->ucsi->cap.num_alt_modes;
512 memset(orig, 0, sizeof(orig));
513 memset(updated, 0, sizeof(updated));
515 /* First get all the alternate modes */
516 for (i = 0; i < max_altmodes; i++) {
517 memset(&alt, 0, sizeof(alt));
518 command = UCSI_GET_ALTERNATE_MODES;
519 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
520 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
521 command |= UCSI_GET_ALTMODE_OFFSET(i);
522 len = ucsi_send_command(con->ucsi, command, &alt, sizeof(alt));
524 * We are collecting all altmodes first and then registering.
525 * Some type-C device will return zero length data beyond last
526 * alternate modes. We should not return if length is zero.
531 /* We got all altmodes, now break out and register them */
532 if (!len || !alt.svid)
535 orig[k].mid = alt.mid;
536 orig[k].svid = alt.svid;
540 * Update the original altmode table as some ppms may report
541 * multiple DP altmodes.
543 if (recipient == UCSI_RECIPIENT_CON)
544 multi_dp = ucsi->ops->update_altmodes(ucsi, orig, updated);
546 /* now register altmodes */
547 for (i = 0; i < max_altmodes; i++) {
548 memset(&desc, 0, sizeof(desc));
549 if (multi_dp && recipient == UCSI_RECIPIENT_CON) {
550 desc.svid = updated[i].svid;
551 desc.vdo = updated[i].mid;
553 desc.svid = orig[i].svid;
554 desc.vdo = orig[i].mid;
556 desc.roles = TYPEC_PORT_DRD;
561 ret = ucsi_register_altmode(con, &desc, recipient);
569 static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
571 int max_altmodes = UCSI_MAX_ALTMODES;
572 struct typec_altmode_desc desc;
573 struct ucsi_altmode alt[2];
581 if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
584 if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
587 if (con->ucsi->ops->update_altmodes)
588 return ucsi_register_altmodes_nvidia(con, recipient);
590 if (recipient == UCSI_RECIPIENT_CON)
591 max_altmodes = con->ucsi->cap.num_alt_modes;
593 for (i = 0; i < max_altmodes;) {
594 memset(alt, 0, sizeof(alt));
595 command = UCSI_GET_ALTERNATE_MODES;
596 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
597 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
598 command |= UCSI_GET_ALTMODE_OFFSET(i);
599 len = ucsi_send_command(con->ucsi, command, alt, sizeof(alt));
606 * This code is requesting one alt mode at a time, but some PPMs
607 * may still return two. If that happens both alt modes need be
608 * registered and the offset for the next alt mode has to be
611 num = len / sizeof(alt[0]);
614 for (j = 0; j < num; j++) {
618 memset(&desc, 0, sizeof(desc));
619 desc.vdo = alt[j].mid;
620 desc.svid = alt[j].svid;
621 desc.roles = TYPEC_PORT_DRD;
623 ret = ucsi_register_altmode(con, &desc, recipient);
632 static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
634 const struct typec_altmode *pdev;
635 struct typec_altmode **adev;
639 case UCSI_RECIPIENT_CON:
640 adev = con->port_altmode;
642 case UCSI_RECIPIENT_SOP:
643 adev = con->partner_altmode;
645 case UCSI_RECIPIENT_SOP_P:
646 adev = con->plug_altmode;
653 if (recipient == UCSI_RECIPIENT_SOP &&
654 (adev[i]->svid == USB_TYPEC_DP_SID ||
655 (adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID &&
656 adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) {
657 pdev = typec_altmode_get_partner(adev[i]);
658 ucsi_displayport_remove_partner((void *)pdev);
660 typec_unregister_altmode(adev[i]);
665 static int ucsi_read_pdos(struct ucsi_connector *con,
666 enum typec_role role, int is_partner,
667 u32 *pdos, int offset, int num_pdos)
669 struct ucsi *ucsi = con->ucsi;
674 ucsi->quirks & UCSI_NO_PARTNER_PDOS &&
675 ((con->status.flags & UCSI_CONSTAT_PWR_DIR) ||
679 command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num);
680 command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner);
681 command |= UCSI_GET_PDOS_PDO_OFFSET(offset);
682 command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1);
683 command |= is_source(role) ? UCSI_GET_PDOS_SRC_PDOS : 0;
684 ret = ucsi_send_command(ucsi, command, pdos + offset,
685 num_pdos * sizeof(u32));
686 if (ret < 0 && ret != -ETIMEDOUT)
687 dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret);
692 static int ucsi_get_pdos(struct ucsi_connector *con, enum typec_role role,
693 int is_partner, u32 *pdos)
695 struct ucsi *ucsi = con->ucsi;
699 if (!(ucsi->cap.features & UCSI_CAP_PDO_DETAILS))
702 /* UCSI max payload means only getting at most 4 PDOs at a time */
703 ret = ucsi_read_pdos(con, role, is_partner, pdos, 0, UCSI_MAX_PDOS);
707 num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */
708 if (num_pdos < UCSI_MAX_PDOS)
711 /* get the remaining PDOs, if any */
712 ret = ucsi_read_pdos(con, role, is_partner, pdos, UCSI_MAX_PDOS,
713 PDO_MAX_OBJECTS - UCSI_MAX_PDOS);
717 return ret / sizeof(u32) + num_pdos;
720 static int ucsi_get_src_pdos(struct ucsi_connector *con)
724 ret = ucsi_get_pdos(con, TYPEC_SOURCE, 1, con->src_pdos);
730 ucsi_port_psy_changed(con);
735 static struct usb_power_delivery_capabilities *ucsi_get_pd_caps(struct ucsi_connector *con,
736 enum typec_role role,
739 struct usb_power_delivery_capabilities_desc pd_caps;
742 ret = ucsi_get_pdos(con, role, is_partner, pd_caps.pdo);
746 if (ret < PDO_MAX_OBJECTS)
747 pd_caps.pdo[ret] = 0;
751 return usb_power_delivery_register_capabilities(is_partner ? con->partner_pd : con->pd,
755 static int ucsi_read_identity(struct ucsi_connector *con, u8 recipient,
756 u8 offset, u8 bytes, void *resp)
758 struct ucsi *ucsi = con->ucsi;
762 command = UCSI_COMMAND(UCSI_GET_PD_MESSAGE) |
763 UCSI_CONNECTOR_NUMBER(con->num);
764 command |= UCSI_GET_PD_MESSAGE_RECIPIENT(recipient);
765 command |= UCSI_GET_PD_MESSAGE_OFFSET(offset);
766 command |= UCSI_GET_PD_MESSAGE_BYTES(bytes);
767 command |= UCSI_GET_PD_MESSAGE_TYPE(UCSI_GET_PD_MESSAGE_TYPE_IDENTITY);
769 ret = ucsi_send_command(ucsi, command, resp, bytes);
771 dev_err(ucsi->dev, "UCSI_GET_PD_MESSAGE failed (%d)\n", ret);
776 static int ucsi_get_identity(struct ucsi_connector *con, u8 recipient,
777 struct usb_pd_identity *id)
779 struct ucsi *ucsi = con->ucsi;
780 struct ucsi_pd_message_disc_id resp = {};
783 if (ucsi->version < UCSI_VERSION_2_0) {
785 * Before UCSI v2.0, MESSAGE_IN is 16 bytes which cannot fit
786 * the 28 byte identity response including the VDM header.
787 * First request the VDM header, ID Header VDO, Cert Stat VDO
790 ret = ucsi_read_identity(con, recipient, 0, 0x10, &resp);
795 /* Then request Product Type VDO1 through Product Type VDO3. */
796 ret = ucsi_read_identity(con, recipient, 0x10, 0xc,
803 * In UCSI v2.0 and after, MESSAGE_IN is large enough to request
804 * the large enough to request the full Discover Identity
807 ret = ucsi_read_identity(con, recipient, 0x0, 0x1c, &resp);
812 id->id_header = resp.id_header;
813 id->cert_stat = resp.cert_stat;
814 id->product = resp.product;
815 id->vdo[0] = resp.vdo[0];
816 id->vdo[1] = resp.vdo[1];
817 id->vdo[2] = resp.vdo[2];
821 static int ucsi_get_partner_identity(struct ucsi_connector *con)
825 ret = ucsi_get_identity(con, UCSI_RECIPIENT_SOP,
826 &con->partner_identity);
830 ret = typec_partner_set_identity(con->partner);
832 dev_err(con->ucsi->dev, "Failed to set partner identity (%d)\n",
839 static int ucsi_get_cable_identity(struct ucsi_connector *con)
843 ret = ucsi_get_identity(con, UCSI_RECIPIENT_SOP_P,
844 &con->cable_identity);
848 ret = typec_cable_set_identity(con->cable);
850 dev_err(con->ucsi->dev, "Failed to set cable identity (%d)\n",
857 static int ucsi_check_altmodes(struct ucsi_connector *con)
859 int ret, num_partner_am;
861 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
862 if (ret && ret != -ETIMEDOUT)
863 dev_err(con->ucsi->dev,
864 "con%d: failed to register partner alt modes (%d)\n",
867 /* Ignoring the errors in this case. */
868 if (con->partner_altmode[0]) {
869 num_partner_am = ucsi_get_num_altmode(con->partner_altmode);
870 typec_partner_set_num_altmodes(con->partner, num_partner_am);
871 ucsi_altmode_update_active(con);
874 typec_partner_set_num_altmodes(con->partner, 0);
880 static void ucsi_register_device_pdos(struct ucsi_connector *con)
882 struct ucsi *ucsi = con->ucsi;
883 struct usb_power_delivery_desc desc = { ucsi->cap.pd_version };
884 struct usb_power_delivery_capabilities *pd_cap;
889 con->pd = usb_power_delivery_register(ucsi->dev, &desc);
891 pd_cap = ucsi_get_pd_caps(con, TYPEC_SOURCE, false);
893 con->port_source_caps = pd_cap;
895 pd_cap = ucsi_get_pd_caps(con, TYPEC_SINK, false);
897 con->port_sink_caps = pd_cap;
899 typec_port_set_usb_power_delivery(con->port, con->pd);
902 static int ucsi_register_partner_pdos(struct ucsi_connector *con)
904 struct usb_power_delivery_desc desc = { con->ucsi->cap.pd_version };
905 struct usb_power_delivery_capabilities *cap;
910 con->partner_pd = typec_partner_usb_power_delivery_register(con->partner, &desc);
911 if (IS_ERR(con->partner_pd))
912 return PTR_ERR(con->partner_pd);
914 cap = ucsi_get_pd_caps(con, TYPEC_SOURCE, true);
918 con->partner_source_caps = cap;
920 cap = ucsi_get_pd_caps(con, TYPEC_SINK, true);
924 con->partner_sink_caps = cap;
926 return typec_partner_set_usb_power_delivery(con->partner, con->partner_pd);
929 static void ucsi_unregister_partner_pdos(struct ucsi_connector *con)
931 usb_power_delivery_unregister_capabilities(con->partner_sink_caps);
932 con->partner_sink_caps = NULL;
933 usb_power_delivery_unregister_capabilities(con->partner_source_caps);
934 con->partner_source_caps = NULL;
935 usb_power_delivery_unregister(con->partner_pd);
936 con->partner_pd = NULL;
939 static int ucsi_register_plug(struct ucsi_connector *con)
941 struct typec_plug *plug;
942 struct typec_plug_desc desc = {.index = TYPEC_PLUG_SOP_P};
944 plug = typec_register_plug(con->cable, &desc);
946 dev_err(con->ucsi->dev,
947 "con%d: failed to register plug (%ld)\n", con->num,
949 return PTR_ERR(plug);
956 static void ucsi_unregister_plug(struct ucsi_connector *con)
961 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP_P);
962 typec_unregister_plug(con->plug);
966 static int ucsi_register_cable(struct ucsi_connector *con)
968 struct typec_cable *cable;
969 struct typec_cable_desc desc = {};
971 switch (UCSI_CABLE_PROP_FLAG_PLUG_TYPE(con->cable_prop.flags)) {
972 case UCSI_CABLE_PROPERTY_PLUG_TYPE_A:
973 desc.type = USB_PLUG_TYPE_A;
975 case UCSI_CABLE_PROPERTY_PLUG_TYPE_B:
976 desc.type = USB_PLUG_TYPE_B;
978 case UCSI_CABLE_PROPERTY_PLUG_TYPE_C:
979 desc.type = USB_PLUG_TYPE_C;
982 desc.type = USB_PLUG_NONE;
986 desc.identity = &con->cable_identity;
987 desc.active = !!(UCSI_CABLE_PROP_FLAG_ACTIVE_CABLE &
988 con->cable_prop.flags);
989 desc.pd_revision = UCSI_CABLE_PROP_FLAG_PD_MAJOR_REV_AS_BCD(
990 con->cable_prop.flags);
992 cable = typec_register_cable(con->port, &desc);
994 dev_err(con->ucsi->dev,
995 "con%d: failed to register cable (%ld)\n", con->num,
997 return PTR_ERR(cable);
1004 static void ucsi_unregister_cable(struct ucsi_connector *con)
1009 ucsi_unregister_plug(con);
1010 typec_unregister_cable(con->cable);
1011 memset(&con->cable_identity, 0, sizeof(con->cable_identity));
1015 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
1017 switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
1018 case UCSI_CONSTAT_PWR_OPMODE_PD:
1019 con->rdo = con->status.request_data_obj;
1020 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
1021 ucsi_partner_task(con, ucsi_get_src_pdos, 30, 0);
1022 ucsi_partner_task(con, ucsi_check_altmodes, 30, HZ);
1023 ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ);
1025 case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
1027 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
1029 case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
1031 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
1035 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
1040 static int ucsi_register_partner(struct ucsi_connector *con)
1042 u8 pwr_opmode = UCSI_CONSTAT_PWR_OPMODE(con->status.flags);
1043 struct typec_partner_desc desc;
1044 struct typec_partner *partner;
1049 memset(&desc, 0, sizeof(desc));
1051 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1052 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
1053 desc.accessory = TYPEC_ACCESSORY_DEBUG;
1055 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
1056 desc.accessory = TYPEC_ACCESSORY_AUDIO;
1062 if (pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD)
1063 ucsi_register_device_pdos(con);
1065 desc.identity = &con->partner_identity;
1066 desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD;
1067 desc.pd_revision = UCSI_CONCAP_FLAG_PARTNER_PD_MAJOR_REV_AS_BCD(con->cap.flags);
1069 partner = typec_register_partner(con->port, &desc);
1070 if (IS_ERR(partner)) {
1071 dev_err(con->ucsi->dev,
1072 "con%d: failed to register partner (%ld)\n", con->num,
1074 return PTR_ERR(partner);
1077 con->partner = partner;
1082 static void ucsi_unregister_partner(struct ucsi_connector *con)
1087 typec_set_mode(con->port, TYPEC_STATE_SAFE);
1089 typec_partner_set_usb_power_delivery(con->partner, NULL);
1090 ucsi_unregister_partner_pdos(con);
1091 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
1092 ucsi_unregister_cable(con);
1093 typec_unregister_partner(con->partner);
1094 memset(&con->partner_identity, 0, sizeof(con->partner_identity));
1095 con->partner = NULL;
1098 static void ucsi_partner_change(struct ucsi_connector *con)
1100 enum usb_role u_role = USB_ROLE_NONE;
1103 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1104 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1105 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1106 u_role = USB_ROLE_HOST;
1108 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1109 typec_set_data_role(con->port, TYPEC_HOST);
1111 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1112 u_role = USB_ROLE_DEVICE;
1113 typec_set_data_role(con->port, TYPEC_DEVICE);
1119 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1120 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1121 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
1122 typec_set_mode(con->port, TYPEC_MODE_DEBUG);
1124 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
1125 typec_set_mode(con->port, TYPEC_MODE_AUDIO);
1128 if (UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) ==
1129 UCSI_CONSTAT_PARTNER_FLAG_USB)
1130 typec_set_mode(con->port, TYPEC_STATE_USB);
1134 /* Only notify USB controller if partner supports USB data */
1135 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
1136 u_role = USB_ROLE_NONE;
1138 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1140 dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n",
1144 static int ucsi_check_connector_capability(struct ucsi_connector *con)
1149 if (!con->partner || con->ucsi->version < UCSI_VERSION_2_0)
1152 command = UCSI_GET_CONNECTOR_CAPABILITY | UCSI_CONNECTOR_NUMBER(con->num);
1153 ret = ucsi_send_command(con->ucsi, command, &con->cap, sizeof(con->cap));
1155 dev_err(con->ucsi->dev, "GET_CONNECTOR_CAPABILITY failed (%d)\n", ret);
1159 typec_partner_set_pd_revision(con->partner,
1160 UCSI_CONCAP_FLAG_PARTNER_PD_MAJOR_REV_AS_BCD(con->cap.flags));
1165 static int ucsi_check_connection(struct ucsi_connector *con)
1167 u8 prev_flags = con->status.flags;
1171 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
1172 ret = ucsi_send_command(con->ucsi, command, &con->status, sizeof(con->status));
1174 dev_err(con->ucsi->dev, "GET_CONNECTOR_STATUS failed (%d)\n", ret);
1178 if (con->status.flags == prev_flags)
1181 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1182 ucsi_register_partner(con);
1183 ucsi_pwr_opmode_change(con);
1184 ucsi_partner_change(con);
1186 ucsi_partner_change(con);
1187 ucsi_port_psy_changed(con);
1188 ucsi_unregister_partner(con);
1194 static int ucsi_check_cable(struct ucsi_connector *con)
1197 int ret, num_plug_am;
1202 command = UCSI_GET_CABLE_PROPERTY | UCSI_CONNECTOR_NUMBER(con->num);
1203 ret = ucsi_send_command(con->ucsi, command, &con->cable_prop,
1204 sizeof(con->cable_prop));
1206 dev_err(con->ucsi->dev, "GET_CABLE_PROPERTY failed (%d)\n",
1211 ret = ucsi_register_cable(con);
1215 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE) {
1216 ret = ucsi_get_cable_identity(con);
1221 if (con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS) {
1222 ret = ucsi_register_plug(con);
1226 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP_P);
1230 if (con->plug_altmode[0]) {
1231 num_plug_am = ucsi_get_num_altmode(con->plug_altmode);
1232 typec_plug_set_num_altmodes(con->plug, num_plug_am);
1234 typec_plug_set_num_altmodes(con->plug, 0);
1241 static void ucsi_handle_connector_change(struct work_struct *work)
1243 struct ucsi_connector *con = container_of(work, struct ucsi_connector,
1245 struct ucsi *ucsi = con->ucsi;
1246 enum typec_role role;
1250 mutex_lock(&con->lock);
1252 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
1254 ret = ucsi_send_command_common(ucsi, command, &con->status,
1255 sizeof(con->status), true);
1257 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
1259 clear_bit(EVENT_PENDING, &con->ucsi->flags);
1263 trace_ucsi_connector_change(con->num, &con->status);
1265 if (ucsi->ops->connector_status)
1266 ucsi->ops->connector_status(con);
1268 role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
1270 if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
1271 typec_set_pwr_role(con->port, role);
1273 /* Complete pending power role swap */
1274 if (!completion_done(&con->complete))
1275 complete(&con->complete);
1278 if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
1279 typec_set_pwr_role(con->port, role);
1280 ucsi_port_psy_changed(con);
1281 ucsi_partner_change(con);
1283 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1284 ucsi_register_partner(con);
1285 ucsi_partner_task(con, ucsi_check_connection, 1, HZ);
1286 ucsi_partner_task(con, ucsi_check_connector_capability, 1, HZ);
1287 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
1288 ucsi_partner_task(con, ucsi_get_partner_identity, 1, HZ);
1289 if (con->ucsi->cap.features & UCSI_CAP_CABLE_DETAILS)
1290 ucsi_partner_task(con, ucsi_check_cable, 1, HZ);
1292 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) ==
1293 UCSI_CONSTAT_PWR_OPMODE_PD)
1294 ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ);
1296 ucsi_unregister_partner(con);
1300 if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE ||
1301 con->status.change & UCSI_CONSTAT_POWER_LEVEL_CHANGE)
1302 ucsi_pwr_opmode_change(con);
1304 if (con->partner && con->status.change & UCSI_CONSTAT_PARTNER_CHANGE) {
1305 ucsi_partner_change(con);
1307 /* Complete pending data role swap */
1308 if (!completion_done(&con->complete))
1309 complete(&con->complete);
1312 if (con->status.change & UCSI_CONSTAT_CAM_CHANGE)
1313 ucsi_partner_task(con, ucsi_check_altmodes, 1, HZ);
1315 if (con->status.change & UCSI_CONSTAT_BC_CHANGE)
1316 ucsi_port_psy_changed(con);
1319 mutex_unlock(&con->lock);
1323 * ucsi_connector_change - Process Connector Change Event
1324 * @ucsi: UCSI Interface
1325 * @num: Connector number
1327 void ucsi_connector_change(struct ucsi *ucsi, u8 num)
1329 struct ucsi_connector *con = &ucsi->connector[num - 1];
1331 if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
1332 dev_dbg(ucsi->dev, "Early connector change event\n");
1336 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
1337 schedule_work(&con->work);
1339 EXPORT_SYMBOL_GPL(ucsi_connector_change);
1341 /* -------------------------------------------------------------------------- */
1343 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
1347 command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num);
1348 command |= hard ? UCSI_CONNECTOR_RESET_HARD : 0;
1350 return ucsi_send_command(con->ucsi, command, NULL, 0);
1353 static int ucsi_reset_ppm(struct ucsi *ucsi)
1360 mutex_lock(&ucsi->ppm_lock);
1362 ret = ucsi->ops->read_cci(ucsi, &cci);
1367 * If UCSI_CCI_RESET_COMPLETE is already set we must clear
1368 * the flag before we start another reset. Send a
1369 * UCSI_SET_NOTIFICATION_ENABLE command to achieve this.
1370 * Ignore a timeout and try the reset anyway if this fails.
1372 if (cci & UCSI_CCI_RESET_COMPLETE) {
1373 command = UCSI_SET_NOTIFICATION_ENABLE;
1374 ret = ucsi->ops->async_control(ucsi, command);
1378 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
1380 ret = ucsi->ops->read_cci(ucsi, &cci);
1383 if (cci & UCSI_CCI_COMMAND_COMPLETE)
1385 if (time_is_before_jiffies(tmo))
1390 WARN_ON(cci & UCSI_CCI_RESET_COMPLETE);
1393 command = UCSI_PPM_RESET;
1394 ret = ucsi->ops->async_control(ucsi, command);
1398 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
1401 if (time_is_before_jiffies(tmo)) {
1406 /* Give the PPM time to process a reset before reading CCI */
1409 ret = ucsi->ops->read_cci(ucsi, &cci);
1413 /* If the PPM is still doing something else, reset it again. */
1414 if (cci & ~UCSI_CCI_RESET_COMPLETE) {
1415 ret = ucsi->ops->async_control(ucsi, command);
1420 } while (!(cci & UCSI_CCI_RESET_COMPLETE));
1423 mutex_unlock(&ucsi->ppm_lock);
1427 static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)
1431 ret = ucsi_send_command(con->ucsi, command, NULL, 0);
1432 if (ret == -ETIMEDOUT) {
1435 /* PPM most likely stopped responding. Resetting everything. */
1436 ucsi_reset_ppm(con->ucsi);
1438 c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
1439 ucsi_send_command(con->ucsi, c, NULL, 0);
1441 ucsi_reset_connector(con, true);
1447 static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
1449 struct ucsi_connector *con = typec_get_drvdata(port);
1454 mutex_lock(&con->lock);
1456 if (!con->partner) {
1461 partner_type = UCSI_CONSTAT_PARTNER_TYPE(con->status.flags);
1462 if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
1463 role == TYPEC_DEVICE) ||
1464 (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
1465 role == TYPEC_HOST))
1468 reinit_completion(&con->complete);
1470 command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num);
1471 command |= UCSI_SET_UOR_ROLE(role);
1472 command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS;
1473 ret = ucsi_role_cmd(con, command);
1477 mutex_unlock(&con->lock);
1479 if (!wait_for_completion_timeout(&con->complete,
1480 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1486 mutex_unlock(&con->lock);
1491 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
1493 struct ucsi_connector *con = typec_get_drvdata(port);
1494 enum typec_role cur_role;
1498 mutex_lock(&con->lock);
1500 if (!con->partner) {
1505 cur_role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
1507 if (cur_role == role)
1510 reinit_completion(&con->complete);
1512 command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num);
1513 command |= UCSI_SET_PDR_ROLE(role);
1514 command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS;
1515 ret = ucsi_role_cmd(con, command);
1519 mutex_unlock(&con->lock);
1521 if (!wait_for_completion_timeout(&con->complete,
1522 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1525 mutex_lock(&con->lock);
1527 /* Something has gone wrong while swapping the role */
1528 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) !=
1529 UCSI_CONSTAT_PWR_OPMODE_PD) {
1530 ucsi_reset_connector(con, true);
1535 mutex_unlock(&con->lock);
1540 static const struct typec_operations ucsi_ops = {
1541 .dr_set = ucsi_dr_swap,
1542 .pr_set = ucsi_pr_swap
1545 /* Caller must call fwnode_handle_put() after use */
1546 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
1548 struct fwnode_handle *fwnode;
1551 device_for_each_child_node(con->ucsi->dev, fwnode)
1552 if (i++ == con->num)
1557 static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con)
1559 struct typec_capability *cap = &con->typec_cap;
1560 enum typec_accessory *accessory = cap->accessory;
1561 enum usb_role u_role = USB_ROLE_NONE;
1566 name = kasprintf(GFP_KERNEL, "%s-con%d", dev_name(ucsi->dev), con->num);
1570 con->wq = create_singlethread_workqueue(name);
1575 INIT_WORK(&con->work, ucsi_handle_connector_change);
1576 init_completion(&con->complete);
1577 mutex_init(&con->lock);
1578 INIT_LIST_HEAD(&con->partner_tasks);
1581 cap->fwnode = ucsi_find_fwnode(con);
1582 con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode);
1583 if (IS_ERR(con->usb_role_sw))
1584 return dev_err_probe(ucsi->dev, PTR_ERR(con->usb_role_sw),
1585 "con%d: failed to get usb role switch\n", con->num);
1587 /* Delay other interactions with the con until registration is complete */
1588 mutex_lock(&con->lock);
1590 /* Get connector capability */
1591 command = UCSI_GET_CONNECTOR_CAPABILITY;
1592 command |= UCSI_CONNECTOR_NUMBER(con->num);
1593 ret = ucsi_send_command(ucsi, command, &con->cap, sizeof(con->cap));
1597 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP)
1598 cap->data = TYPEC_PORT_DRD;
1599 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP)
1600 cap->data = TYPEC_PORT_DFP;
1601 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP)
1602 cap->data = TYPEC_PORT_UFP;
1604 if ((con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER) &&
1605 (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER))
1606 cap->type = TYPEC_PORT_DRP;
1607 else if (con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER)
1608 cap->type = TYPEC_PORT_SRC;
1609 else if (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER)
1610 cap->type = TYPEC_PORT_SNK;
1612 cap->revision = ucsi->cap.typec_version;
1613 cap->pd_revision = ucsi->cap.pd_version;
1614 cap->svdm_version = SVDM_VER_2_0;
1615 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
1617 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY)
1618 *accessory++ = TYPEC_ACCESSORY_AUDIO;
1619 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
1620 *accessory = TYPEC_ACCESSORY_DEBUG;
1622 cap->driver_data = con;
1623 cap->ops = &ucsi_ops;
1625 if (ucsi->ops->update_connector)
1626 ucsi->ops->update_connector(con);
1628 ret = ucsi_register_port_psy(con);
1632 /* Register the connector */
1633 con->port = typec_register_port(ucsi->dev, cap);
1634 if (IS_ERR(con->port)) {
1635 ret = PTR_ERR(con->port);
1639 if (!(ucsi->quirks & UCSI_DELAY_DEVICE_PDOS))
1640 ucsi_register_device_pdos(con);
1642 /* Alternate modes */
1643 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON);
1645 dev_err(ucsi->dev, "con%d: failed to register alt modes\n",
1650 /* Get the status */
1651 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
1652 ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
1654 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
1658 ret = 0; /* ucsi_send_command() returns length on success */
1660 if (ucsi->ops->connector_status)
1661 ucsi->ops->connector_status(con);
1663 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1664 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1665 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1666 u_role = USB_ROLE_HOST;
1668 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1669 typec_set_data_role(con->port, TYPEC_HOST);
1671 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1672 u_role = USB_ROLE_DEVICE;
1673 typec_set_data_role(con->port, TYPEC_DEVICE);
1679 /* Check if there is already something connected */
1680 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1681 typec_set_pwr_role(con->port,
1682 !!(con->status.flags & UCSI_CONSTAT_PWR_DIR));
1683 ucsi_register_partner(con);
1684 ucsi_pwr_opmode_change(con);
1685 ucsi_port_psy_changed(con);
1686 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
1687 ucsi_get_partner_identity(con);
1688 if (con->ucsi->cap.features & UCSI_CAP_CABLE_DETAILS)
1689 ucsi_check_cable(con);
1692 /* Only notify USB controller if partner supports USB data */
1693 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
1694 u_role = USB_ROLE_NONE;
1696 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1698 dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n",
1704 UCSI_CONSTAT_PWR_OPMODE(con->status.flags) ==
1705 UCSI_CONSTAT_PWR_OPMODE_PD) {
1706 ucsi_register_device_pdos(con);
1707 ucsi_get_src_pdos(con);
1708 ucsi_check_altmodes(con);
1711 trace_ucsi_register_port(con->num, &con->status);
1714 fwnode_handle_put(cap->fwnode);
1716 mutex_unlock(&con->lock);
1718 if (ret && con->wq) {
1719 destroy_workqueue(con->wq);
1726 static u64 ucsi_get_supported_notifications(struct ucsi *ucsi)
1728 u16 features = ucsi->cap.features;
1729 u64 ntfy = UCSI_ENABLE_NTFY_ALL;
1731 if (!(features & UCSI_CAP_ALT_MODE_DETAILS))
1732 ntfy &= ~UCSI_ENABLE_NTFY_CAM_CHANGE;
1734 if (!(features & UCSI_CAP_PDO_DETAILS))
1735 ntfy &= ~(UCSI_ENABLE_NTFY_PWR_LEVEL_CHANGE |
1736 UCSI_ENABLE_NTFY_CAP_CHANGE);
1738 if (!(features & UCSI_CAP_EXT_SUPPLY_NOTIFICATIONS))
1739 ntfy &= ~UCSI_ENABLE_NTFY_EXT_PWR_SRC_CHANGE;
1741 if (!(features & UCSI_CAP_PD_RESET))
1742 ntfy &= ~UCSI_ENABLE_NTFY_PD_RESET_COMPLETE;
1744 if (ucsi->version <= UCSI_VERSION_1_2)
1747 ntfy |= UCSI_ENABLE_NTFY_SINK_PATH_STS_CHANGE;
1749 if (features & UCSI_CAP_GET_ATTENTION_VDO)
1750 ntfy |= UCSI_ENABLE_NTFY_ATTENTION;
1752 if (features & UCSI_CAP_FW_UPDATE_REQUEST)
1753 ntfy |= UCSI_ENABLE_NTFY_LPM_FW_UPDATE_REQ;
1755 if (features & UCSI_CAP_SECURITY_REQUEST)
1756 ntfy |= UCSI_ENABLE_NTFY_SECURITY_REQ_PARTNER;
1758 if (features & UCSI_CAP_SET_RETIMER_MODE)
1759 ntfy |= UCSI_ENABLE_NTFY_SET_RETIMER_MODE;
1765 * ucsi_init - Initialize UCSI interface
1766 * @ucsi: UCSI to be initialized
1768 * Registers all ports @ucsi has and enables all notification events.
1770 static int ucsi_init(struct ucsi *ucsi)
1772 struct ucsi_connector *con, *connector;
1779 ret = ucsi_reset_ppm(ucsi);
1781 dev_err(ucsi->dev, "failed to reset PPM!\n");
1785 /* Enable basic notifications */
1786 ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
1787 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1788 ret = ucsi_send_command(ucsi, command, NULL, 0);
1792 /* Get PPM capabilities */
1793 command = UCSI_GET_CAPABILITY;
1794 ret = ucsi_send_command(ucsi, command, &ucsi->cap, sizeof(ucsi->cap));
1798 if (!ucsi->cap.num_connectors) {
1803 /* Allocate the connectors. Released in ucsi_unregister() */
1804 connector = kcalloc(ucsi->cap.num_connectors + 1, sizeof(*connector), GFP_KERNEL);
1810 /* Register all connectors */
1811 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1812 connector[i].num = i + 1;
1813 ret = ucsi_register_port(ucsi, &connector[i]);
1815 goto err_unregister;
1818 /* Enable all supported notifications */
1819 ntfy = ucsi_get_supported_notifications(ucsi);
1820 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1821 ret = ucsi_send_command(ucsi, command, NULL, 0);
1823 goto err_unregister;
1825 ucsi->connector = connector;
1828 mutex_lock(&ucsi->ppm_lock);
1829 ret = ucsi->ops->read_cci(ucsi, &cci);
1830 mutex_unlock(&ucsi->ppm_lock);
1833 if (UCSI_CCI_CONNECTOR(cci))
1834 ucsi_connector_change(ucsi, UCSI_CCI_CONNECTOR(cci));
1839 for (con = connector; con->port; con++) {
1840 ucsi_unregister_partner(con);
1841 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON);
1842 ucsi_unregister_port_psy(con);
1844 destroy_workqueue(con->wq);
1846 usb_power_delivery_unregister_capabilities(con->port_sink_caps);
1847 con->port_sink_caps = NULL;
1848 usb_power_delivery_unregister_capabilities(con->port_source_caps);
1849 con->port_source_caps = NULL;
1850 usb_power_delivery_unregister(con->pd);
1852 typec_unregister_port(con->port);
1857 memset(&ucsi->cap, 0, sizeof(ucsi->cap));
1858 ucsi_reset_ppm(ucsi);
1863 static void ucsi_resume_work(struct work_struct *work)
1865 struct ucsi *ucsi = container_of(work, struct ucsi, resume_work);
1866 struct ucsi_connector *con;
1870 /* Restore UCSI notification enable mask after system resume */
1871 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1872 ret = ucsi_send_command(ucsi, command, NULL, 0);
1874 dev_err(ucsi->dev, "failed to re-enable notifications (%d)\n", ret);
1878 for (con = ucsi->connector; con->port; con++) {
1879 mutex_lock(&con->lock);
1880 ucsi_partner_task(con, ucsi_check_connection, 1, 0);
1881 mutex_unlock(&con->lock);
1885 int ucsi_resume(struct ucsi *ucsi)
1887 if (ucsi->connector)
1888 queue_work(system_long_wq, &ucsi->resume_work);
1891 EXPORT_SYMBOL_GPL(ucsi_resume);
1893 static void ucsi_init_work(struct work_struct *work)
1895 struct ucsi *ucsi = container_of(work, struct ucsi, work.work);
1898 ret = ucsi_init(ucsi);
1900 dev_err_probe(ucsi->dev, ret, "PPM init failed\n");
1902 if (ret == -EPROBE_DEFER) {
1903 if (ucsi->work_count++ > UCSI_ROLE_SWITCH_WAIT_COUNT) {
1904 dev_err(ucsi->dev, "PPM init failed, stop trying\n");
1908 queue_delayed_work(system_long_wq, &ucsi->work,
1909 UCSI_ROLE_SWITCH_INTERVAL);
1914 * ucsi_get_drvdata - Return private driver data pointer
1915 * @ucsi: UCSI interface
1917 void *ucsi_get_drvdata(struct ucsi *ucsi)
1919 return ucsi->driver_data;
1921 EXPORT_SYMBOL_GPL(ucsi_get_drvdata);
1924 * ucsi_set_drvdata - Assign private driver data pointer
1925 * @ucsi: UCSI interface
1926 * @data: Private data pointer
1928 void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
1930 ucsi->driver_data = data;
1932 EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
1935 * ucsi_create - Allocate UCSI instance
1936 * @dev: Device interface to the PPM (Platform Policy Manager)
1937 * @ops: I/O routines
1939 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
1944 !ops->read_version || !ops->read_cci || !ops->read_message_in ||
1945 !ops->sync_control || !ops->async_control)
1946 return ERR_PTR(-EINVAL);
1948 ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
1950 return ERR_PTR(-ENOMEM);
1952 INIT_WORK(&ucsi->resume_work, ucsi_resume_work);
1953 INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work);
1954 mutex_init(&ucsi->ppm_lock);
1955 init_completion(&ucsi->complete);
1961 EXPORT_SYMBOL_GPL(ucsi_create);
1964 * ucsi_destroy - Free UCSI instance
1965 * @ucsi: UCSI instance to be freed
1967 void ucsi_destroy(struct ucsi *ucsi)
1969 ucsi_debugfs_unregister(ucsi);
1972 EXPORT_SYMBOL_GPL(ucsi_destroy);
1975 * ucsi_register - Register UCSI interface
1976 * @ucsi: UCSI instance
1978 int ucsi_register(struct ucsi *ucsi)
1982 ret = ucsi->ops->read_version(ucsi, &ucsi->version);
1990 * Version format is JJ.M.N (JJ = Major version, M = Minor version,
1991 * N = sub-minor version).
1993 dev_dbg(ucsi->dev, "Registered UCSI interface with version %x.%x.%x",
1994 UCSI_BCD_GET_MAJOR(ucsi->version),
1995 UCSI_BCD_GET_MINOR(ucsi->version),
1996 UCSI_BCD_GET_SUBMINOR(ucsi->version));
1998 queue_delayed_work(system_long_wq, &ucsi->work, 0);
2000 ucsi_debugfs_register(ucsi);
2003 EXPORT_SYMBOL_GPL(ucsi_register);
2006 * ucsi_unregister - Unregister UCSI interface
2007 * @ucsi: UCSI interface to be unregistered
2009 * Unregister UCSI interface that was created with ucsi_register().
2011 void ucsi_unregister(struct ucsi *ucsi)
2013 u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
2016 /* Make sure that we are not in the middle of driver initialization */
2017 cancel_delayed_work_sync(&ucsi->work);
2018 cancel_work_sync(&ucsi->resume_work);
2020 /* Disable notifications */
2021 ucsi->ops->async_control(ucsi, cmd);
2023 if (!ucsi->connector)
2026 for (i = 0; i < ucsi->cap.num_connectors; i++) {
2027 cancel_work_sync(&ucsi->connector[i].work);
2028 ucsi_unregister_partner(&ucsi->connector[i]);
2029 ucsi_unregister_altmodes(&ucsi->connector[i],
2030 UCSI_RECIPIENT_CON);
2031 ucsi_unregister_port_psy(&ucsi->connector[i]);
2033 if (ucsi->connector[i].wq) {
2034 struct ucsi_work *uwork;
2036 mutex_lock(&ucsi->connector[i].lock);
2038 * queue delayed items immediately so they can execute
2039 * and free themselves before the wq is destroyed
2041 list_for_each_entry(uwork, &ucsi->connector[i].partner_tasks, node)
2042 mod_delayed_work(ucsi->connector[i].wq, &uwork->work, 0);
2043 mutex_unlock(&ucsi->connector[i].lock);
2044 destroy_workqueue(ucsi->connector[i].wq);
2047 usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_sink_caps);
2048 ucsi->connector[i].port_sink_caps = NULL;
2049 usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_source_caps);
2050 ucsi->connector[i].port_source_caps = NULL;
2051 usb_power_delivery_unregister(ucsi->connector[i].pd);
2052 ucsi->connector[i].pd = NULL;
2053 typec_unregister_port(ucsi->connector[i].port);
2056 kfree(ucsi->connector);
2058 EXPORT_SYMBOL_GPL(ucsi_unregister);
2060 static int __init ucsi_module_init(void)
2062 ucsi_debugfs_init();
2065 module_init(ucsi_module_init);
2067 static void __exit ucsi_module_exit(void)
2069 ucsi_debugfs_exit();
2071 module_exit(ucsi_module_exit);
2074 MODULE_LICENSE("GPL v2");
2075 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");