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 /* Ignore bogus data in CCI if busy indicator is set. */
42 if (cci & UCSI_CCI_BUSY)
45 if (UCSI_CCI_CONNECTOR(cci))
46 ucsi_connector_change(ucsi, UCSI_CCI_CONNECTOR(cci));
48 if (cci & UCSI_CCI_ACK_COMPLETE &&
49 test_and_clear_bit(ACK_PENDING, &ucsi->flags))
50 complete(&ucsi->complete);
52 if (cci & UCSI_CCI_COMMAND_COMPLETE &&
53 test_and_clear_bit(COMMAND_PENDING, &ucsi->flags))
54 complete(&ucsi->complete);
56 EXPORT_SYMBOL_GPL(ucsi_notify_common);
58 int ucsi_sync_control_common(struct ucsi *ucsi, u64 command)
60 bool ack = UCSI_COMMAND(command) == UCSI_ACK_CC_CI;
64 set_bit(ACK_PENDING, &ucsi->flags);
66 set_bit(COMMAND_PENDING, &ucsi->flags);
68 reinit_completion(&ucsi->complete);
70 ret = ucsi->ops->async_control(ucsi, command);
74 if (!wait_for_completion_timeout(&ucsi->complete, 5 * HZ))
79 clear_bit(ACK_PENDING, &ucsi->flags);
81 clear_bit(COMMAND_PENDING, &ucsi->flags);
85 EXPORT_SYMBOL_GPL(ucsi_sync_control_common);
87 static int ucsi_acknowledge(struct ucsi *ucsi, bool conn_ack)
91 ctrl = UCSI_ACK_CC_CI;
92 ctrl |= UCSI_ACK_COMMAND_COMPLETE;
94 clear_bit(EVENT_PENDING, &ucsi->flags);
95 ctrl |= UCSI_ACK_CONNECTOR_CHANGE;
98 return ucsi->ops->sync_control(ucsi, ctrl);
101 static int ucsi_run_command(struct ucsi *ucsi, u64 command, u32 *cci,
102 void *data, size_t size, bool conn_ack)
108 if (size > UCSI_MAX_DATA_LENGTH(ucsi))
111 ret = ucsi->ops->sync_control(ucsi, command);
112 if (ucsi->ops->read_cci(ucsi, cci))
115 if (*cci & UCSI_CCI_BUSY)
116 return ucsi_run_command(ucsi, UCSI_CANCEL, cci, NULL, 0, false) ?: -EBUSY;
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);
140 return err ?: UCSI_CCI_LENGTH(*cci);
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, &error, sizeof(error), false);
156 case UCSI_ERROR_INCOMPATIBLE_PARTNER:
158 case UCSI_ERROR_CC_COMMUNICATION_ERR:
160 case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
162 case UCSI_ERROR_DEAD_BATTERY:
163 dev_warn(ucsi->dev, "Dead battery condition!\n");
165 case UCSI_ERROR_INVALID_CON_NUM:
166 case UCSI_ERROR_UNREGONIZED_CMD:
167 case UCSI_ERROR_INVALID_CMD_ARGUMENT:
168 dev_err(ucsi->dev, "possible UCSI driver bug %u\n", error);
170 case UCSI_ERROR_OVERCURRENT:
171 dev_warn(ucsi->dev, "Overcurrent condition\n");
173 case UCSI_ERROR_PARTNER_REJECTED_SWAP:
174 dev_warn(ucsi->dev, "Partner rejected swap\n");
176 case UCSI_ERROR_HARD_RESET:
177 dev_warn(ucsi->dev, "Hard reset occurred\n");
179 case UCSI_ERROR_PPM_POLICY_CONFLICT:
180 dev_warn(ucsi->dev, "PPM Policy conflict\n");
182 case UCSI_ERROR_SWAP_REJECTED:
183 dev_warn(ucsi->dev, "Swap rejected\n");
185 case UCSI_ERROR_REVERSE_CURRENT_PROTECTION:
186 dev_warn(ucsi->dev, "Reverse Current Protection detected\n");
188 case UCSI_ERROR_SET_SINK_PATH_REJECTED:
189 dev_warn(ucsi->dev, "Set Sink Path rejected\n");
191 case UCSI_ERROR_UNDEFINED:
193 dev_err(ucsi->dev, "unknown error %u\n", error);
200 static int ucsi_send_command_common(struct ucsi *ucsi, u64 cmd,
201 void *data, size_t size, bool conn_ack)
207 if (ucsi->version > UCSI_VERSION_1_2) {
208 switch (UCSI_COMMAND(cmd)) {
209 case UCSI_GET_ALTERNATE_MODES:
210 connector_num = UCSI_GET_ALTMODE_GET_CONNECTOR_NUMBER(cmd);
215 case UCSI_SET_NOTIFICATION_ENABLE:
216 case UCSI_GET_CAPABILITY:
220 connector_num = UCSI_DEFAULT_GET_CONNECTOR_NUMBER(cmd);
227 mutex_lock(&ucsi->ppm_lock);
229 ret = ucsi_run_command(ucsi, cmd, &cci, data, size, conn_ack);
231 if (cci & UCSI_CCI_ERROR)
232 ret = ucsi_read_error(ucsi, connector_num);
234 mutex_unlock(&ucsi->ppm_lock);
238 int ucsi_send_command(struct ucsi *ucsi, u64 command,
239 void *data, size_t size)
241 return ucsi_send_command_common(ucsi, command, data, size, false);
243 EXPORT_SYMBOL_GPL(ucsi_send_command);
245 /* -------------------------------------------------------------------------- */
248 struct delayed_work work;
249 struct list_head node;
252 struct ucsi_connector *con;
253 int (*cb)(struct ucsi_connector *);
256 static void ucsi_poll_worker(struct work_struct *work)
258 struct ucsi_work *uwork = container_of(work, struct ucsi_work, work.work);
259 struct ucsi_connector *con = uwork->con;
262 mutex_lock(&con->lock);
265 list_del(&uwork->node);
266 mutex_unlock(&con->lock);
271 ret = uwork->cb(con);
273 if (uwork->count-- && (ret == -EBUSY || ret == -ETIMEDOUT)) {
274 queue_delayed_work(con->wq, &uwork->work, uwork->delay);
276 list_del(&uwork->node);
280 mutex_unlock(&con->lock);
283 static int ucsi_partner_task(struct ucsi_connector *con,
284 int (*cb)(struct ucsi_connector *),
285 int retries, unsigned long delay)
287 struct ucsi_work *uwork;
292 uwork = kzalloc(sizeof(*uwork), GFP_KERNEL);
296 INIT_DELAYED_WORK(&uwork->work, ucsi_poll_worker);
297 uwork->count = retries;
298 uwork->delay = delay;
302 list_add_tail(&uwork->node, &con->partner_tasks);
303 queue_delayed_work(con->wq, &uwork->work, delay);
308 /* -------------------------------------------------------------------------- */
310 void ucsi_altmode_update_active(struct ucsi_connector *con)
312 const struct typec_altmode *altmode = NULL;
318 command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
319 ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur));
321 if (con->ucsi->version > 0x0100) {
322 dev_err(con->ucsi->dev,
323 "GET_CURRENT_CAM command failed\n");
329 if (cur < UCSI_MAX_ALTMODES)
330 altmode = typec_altmode_get_partner(con->port_altmode[cur]);
332 for (i = 0; con->partner_altmode[i]; i++)
333 typec_altmode_update_active(con->partner_altmode[i],
334 con->partner_altmode[i] == altmode);
337 static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
342 for (i = 0; alt[i]; i++) {
343 if (i > MODE_DISCOVERY_MAX)
346 if (alt[i]->svid == svid)
353 static int ucsi_next_altmode(struct typec_altmode **alt)
357 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
364 static int ucsi_get_num_altmode(struct typec_altmode **alt)
368 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
375 static int ucsi_register_altmode(struct ucsi_connector *con,
376 struct typec_altmode_desc *desc,
379 struct typec_altmode *alt;
384 override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
387 case UCSI_RECIPIENT_CON:
388 i = ucsi_next_altmode(con->port_altmode);
394 ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid);
400 switch (desc->svid) {
401 case USB_TYPEC_DP_SID:
402 alt = ucsi_register_displayport(con, override, i, desc);
404 case USB_TYPEC_NVIDIA_VLINK_SID:
405 if (desc->vdo == USB_TYPEC_NVIDIA_VLINK_DBG_VDO)
406 alt = typec_port_register_altmode(con->port,
409 alt = ucsi_register_displayport(con, override,
413 alt = typec_port_register_altmode(con->port, desc);
422 con->port_altmode[i] = alt;
424 case UCSI_RECIPIENT_SOP:
425 i = ucsi_next_altmode(con->partner_altmode);
431 ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid);
437 alt = typec_partner_register_altmode(con->partner, desc);
443 con->partner_altmode[i] = alt;
445 case UCSI_RECIPIENT_SOP_P:
446 i = ucsi_next_altmode(con->plug_altmode);
452 ret = ucsi_altmode_next_mode(con->plug_altmode, desc->svid);
458 alt = typec_plug_register_altmode(con->plug, desc);
464 con->plug_altmode[i] = alt;
470 trace_ucsi_register_altmode(recipient, alt);
475 dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n",
476 desc->svid, desc->mode);
482 ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient)
484 int max_altmodes = UCSI_MAX_ALTMODES;
485 struct typec_altmode_desc desc;
486 struct ucsi_altmode alt;
487 struct ucsi_altmode orig[UCSI_MAX_ALTMODES];
488 struct ucsi_altmode updated[UCSI_MAX_ALTMODES];
489 struct ucsi *ucsi = con->ucsi;
490 bool multi_dp = false;
497 if (recipient == UCSI_RECIPIENT_CON)
498 max_altmodes = con->ucsi->cap.num_alt_modes;
500 memset(orig, 0, sizeof(orig));
501 memset(updated, 0, sizeof(updated));
503 /* First get all the alternate modes */
504 for (i = 0; i < max_altmodes; i++) {
505 memset(&alt, 0, sizeof(alt));
506 command = UCSI_GET_ALTERNATE_MODES;
507 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
508 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
509 command |= UCSI_GET_ALTMODE_OFFSET(i);
510 len = ucsi_send_command(con->ucsi, command, &alt, sizeof(alt));
512 * We are collecting all altmodes first and then registering.
513 * Some type-C device will return zero length data beyond last
514 * alternate modes. We should not return if length is zero.
519 /* We got all altmodes, now break out and register them */
520 if (!len || !alt.svid)
523 orig[k].mid = alt.mid;
524 orig[k].svid = alt.svid;
528 * Update the original altmode table as some ppms may report
529 * multiple DP altmodes.
531 if (recipient == UCSI_RECIPIENT_CON)
532 multi_dp = ucsi->ops->update_altmodes(ucsi, orig, updated);
534 /* now register altmodes */
535 for (i = 0; i < max_altmodes; i++) {
536 memset(&desc, 0, sizeof(desc));
537 if (multi_dp && recipient == UCSI_RECIPIENT_CON) {
538 desc.svid = updated[i].svid;
539 desc.vdo = updated[i].mid;
541 desc.svid = orig[i].svid;
542 desc.vdo = orig[i].mid;
544 desc.roles = TYPEC_PORT_DRD;
549 ret = ucsi_register_altmode(con, &desc, recipient);
557 static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
559 int max_altmodes = UCSI_MAX_ALTMODES;
560 struct typec_altmode_desc desc;
561 struct ucsi_altmode alt[2];
569 if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
572 if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
575 if (con->ucsi->ops->update_altmodes)
576 return ucsi_register_altmodes_nvidia(con, recipient);
578 if (recipient == UCSI_RECIPIENT_CON)
579 max_altmodes = con->ucsi->cap.num_alt_modes;
581 for (i = 0; i < max_altmodes;) {
582 memset(alt, 0, sizeof(alt));
583 command = UCSI_GET_ALTERNATE_MODES;
584 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
585 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
586 command |= UCSI_GET_ALTMODE_OFFSET(i);
587 len = ucsi_send_command(con->ucsi, command, alt, sizeof(alt));
594 * This code is requesting one alt mode at a time, but some PPMs
595 * may still return two. If that happens both alt modes need be
596 * registered and the offset for the next alt mode has to be
599 num = len / sizeof(alt[0]);
602 for (j = 0; j < num; j++) {
606 memset(&desc, 0, sizeof(desc));
607 desc.vdo = alt[j].mid;
608 desc.svid = alt[j].svid;
609 desc.roles = TYPEC_PORT_DRD;
611 ret = ucsi_register_altmode(con, &desc, recipient);
620 static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
622 const struct typec_altmode *pdev;
623 struct typec_altmode **adev;
627 case UCSI_RECIPIENT_CON:
628 adev = con->port_altmode;
630 case UCSI_RECIPIENT_SOP:
631 adev = con->partner_altmode;
633 case UCSI_RECIPIENT_SOP_P:
634 adev = con->plug_altmode;
641 if (recipient == UCSI_RECIPIENT_SOP &&
642 (adev[i]->svid == USB_TYPEC_DP_SID ||
643 (adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID &&
644 adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) {
645 pdev = typec_altmode_get_partner(adev[i]);
646 ucsi_displayport_remove_partner((void *)pdev);
648 typec_unregister_altmode(adev[i]);
653 static int ucsi_get_connector_status(struct ucsi_connector *con, bool conn_ack)
655 u64 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
656 size_t size = min(sizeof(con->status),
657 UCSI_MAX_DATA_LENGTH(con->ucsi));
660 ret = ucsi_send_command_common(con->ucsi, command, &con->status, size, conn_ack);
662 return ret < 0 ? ret : 0;
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 (UCSI_CONSTAT(con, PWR_DIR) || !is_source(role)))
678 command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num);
679 command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner);
680 command |= UCSI_GET_PDOS_PDO_OFFSET(offset);
681 command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1);
682 command |= is_source(role) ? UCSI_GET_PDOS_SRC_PDOS : 0;
683 ret = ucsi_send_command(ucsi, command, pdos + offset,
684 num_pdos * sizeof(u32));
685 if (ret < 0 && ret != -ETIMEDOUT)
686 dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret);
691 static int ucsi_get_pdos(struct ucsi_connector *con, enum typec_role role,
692 int is_partner, u32 *pdos)
694 struct ucsi *ucsi = con->ucsi;
698 if (!(ucsi->cap.features & UCSI_CAP_PDO_DETAILS))
701 /* UCSI max payload means only getting at most 4 PDOs at a time */
702 ret = ucsi_read_pdos(con, role, is_partner, pdos, 0, UCSI_MAX_PDOS);
706 num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */
707 if (num_pdos < UCSI_MAX_PDOS)
710 /* get the remaining PDOs, if any */
711 ret = ucsi_read_pdos(con, role, is_partner, pdos, UCSI_MAX_PDOS,
712 PDO_MAX_OBJECTS - UCSI_MAX_PDOS);
716 return ret / sizeof(u32) + num_pdos;
719 static int ucsi_get_src_pdos(struct ucsi_connector *con)
723 ret = ucsi_get_pdos(con, TYPEC_SOURCE, 1, con->src_pdos);
729 ucsi_port_psy_changed(con);
734 static struct usb_power_delivery_capabilities *ucsi_get_pd_caps(struct ucsi_connector *con,
735 enum typec_role role,
738 struct usb_power_delivery_capabilities_desc pd_caps;
741 ret = ucsi_get_pdos(con, role, is_partner, pd_caps.pdo);
745 if (ret < PDO_MAX_OBJECTS)
746 pd_caps.pdo[ret] = 0;
750 return usb_power_delivery_register_capabilities(is_partner ? con->partner_pd : con->pd,
754 static int ucsi_get_pd_message(struct ucsi_connector *con, u8 recipient,
755 size_t bytes, void *data, u8 type)
757 size_t len = min(bytes, UCSI_MAX_DATA_LENGTH(con->ucsi));
762 for (offset = 0; offset < bytes; offset += len) {
763 len = min(len, bytes - offset);
765 command = UCSI_COMMAND(UCSI_GET_PD_MESSAGE) | UCSI_CONNECTOR_NUMBER(con->num);
766 command |= UCSI_GET_PD_MESSAGE_RECIPIENT(recipient);
767 command |= UCSI_GET_PD_MESSAGE_OFFSET(offset);
768 command |= UCSI_GET_PD_MESSAGE_BYTES(len);
769 command |= UCSI_GET_PD_MESSAGE_TYPE(type);
771 ret = ucsi_send_command(con->ucsi, command, data + offset, len);
779 static int ucsi_get_partner_identity(struct ucsi_connector *con)
784 ret = ucsi_get_pd_message(con, UCSI_RECIPIENT_SOP, sizeof(vdo), vdo,
785 UCSI_GET_PD_MESSAGE_TYPE_IDENTITY);
789 /* VDM Header is not part of struct usb_pd_identity, so dropping it. */
790 con->partner_identity = *(struct usb_pd_identity *)&vdo[1];
792 ret = typec_partner_set_identity(con->partner);
794 dev_err(con->ucsi->dev, "Failed to set partner identity (%d)\n", ret);
799 static int ucsi_get_cable_identity(struct ucsi_connector *con)
804 ret = ucsi_get_pd_message(con, UCSI_RECIPIENT_SOP_P, sizeof(vdo), vdo,
805 UCSI_GET_PD_MESSAGE_TYPE_IDENTITY);
809 con->cable_identity = *(struct usb_pd_identity *)&vdo[1];
811 ret = typec_cable_set_identity(con->cable);
813 dev_err(con->ucsi->dev, "Failed to set cable identity (%d)\n", ret);
818 static int ucsi_check_altmodes(struct ucsi_connector *con)
820 int ret, num_partner_am;
822 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
823 if (ret && ret != -ETIMEDOUT)
824 dev_err(con->ucsi->dev,
825 "con%d: failed to register partner alt modes (%d)\n",
828 /* Ignoring the errors in this case. */
829 if (con->partner_altmode[0]) {
830 num_partner_am = ucsi_get_num_altmode(con->partner_altmode);
831 typec_partner_set_num_altmodes(con->partner, num_partner_am);
832 ucsi_altmode_update_active(con);
835 typec_partner_set_num_altmodes(con->partner, 0);
841 static void ucsi_register_device_pdos(struct ucsi_connector *con)
843 struct ucsi *ucsi = con->ucsi;
844 struct usb_power_delivery_desc desc = { ucsi->cap.pd_version };
845 struct usb_power_delivery_capabilities *pd_cap;
850 con->pd = usb_power_delivery_register(ucsi->dev, &desc);
852 pd_cap = ucsi_get_pd_caps(con, TYPEC_SOURCE, false);
854 con->port_source_caps = pd_cap;
856 pd_cap = ucsi_get_pd_caps(con, TYPEC_SINK, false);
858 con->port_sink_caps = pd_cap;
860 typec_port_set_usb_power_delivery(con->port, con->pd);
863 static int ucsi_register_partner_pdos(struct ucsi_connector *con)
865 struct usb_power_delivery_desc desc = { con->ucsi->cap.pd_version };
866 struct usb_power_delivery_capabilities *cap;
871 con->partner_pd = typec_partner_usb_power_delivery_register(con->partner, &desc);
872 if (IS_ERR(con->partner_pd))
873 return PTR_ERR(con->partner_pd);
875 cap = ucsi_get_pd_caps(con, TYPEC_SOURCE, true);
879 con->partner_source_caps = cap;
881 cap = ucsi_get_pd_caps(con, TYPEC_SINK, true);
885 con->partner_sink_caps = cap;
887 return typec_partner_set_usb_power_delivery(con->partner, con->partner_pd);
890 static void ucsi_unregister_partner_pdos(struct ucsi_connector *con)
892 usb_power_delivery_unregister_capabilities(con->partner_sink_caps);
893 con->partner_sink_caps = NULL;
894 usb_power_delivery_unregister_capabilities(con->partner_source_caps);
895 con->partner_source_caps = NULL;
896 usb_power_delivery_unregister(con->partner_pd);
897 con->partner_pd = NULL;
900 static int ucsi_register_plug(struct ucsi_connector *con)
902 struct typec_plug *plug;
903 struct typec_plug_desc desc = {.index = TYPEC_PLUG_SOP_P};
905 plug = typec_register_plug(con->cable, &desc);
907 dev_err(con->ucsi->dev,
908 "con%d: failed to register plug (%ld)\n", con->num,
910 return PTR_ERR(plug);
917 static void ucsi_unregister_plug(struct ucsi_connector *con)
922 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP_P);
923 typec_unregister_plug(con->plug);
927 static int ucsi_register_cable(struct ucsi_connector *con)
929 struct ucsi_cable_property cable_prop;
930 struct typec_cable *cable;
931 struct typec_cable_desc desc = {};
935 command = UCSI_GET_CABLE_PROPERTY | UCSI_CONNECTOR_NUMBER(con->num);
936 ret = ucsi_send_command(con->ucsi, command, &cable_prop, sizeof(cable_prop));
938 dev_err(con->ucsi->dev, "GET_CABLE_PROPERTY failed (%d)\n", ret);
942 switch (UCSI_CABLE_PROP_FLAG_PLUG_TYPE(cable_prop.flags)) {
943 case UCSI_CABLE_PROPERTY_PLUG_TYPE_A:
944 desc.type = USB_PLUG_TYPE_A;
946 case UCSI_CABLE_PROPERTY_PLUG_TYPE_B:
947 desc.type = USB_PLUG_TYPE_B;
949 case UCSI_CABLE_PROPERTY_PLUG_TYPE_C:
950 desc.type = USB_PLUG_TYPE_C;
953 desc.type = USB_PLUG_NONE;
957 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
958 desc.identity = &con->cable_identity;
959 desc.active = !!(UCSI_CABLE_PROP_FLAG_ACTIVE_CABLE & cable_prop.flags);
961 if (con->ucsi->version >= UCSI_VERSION_2_1)
962 desc.pd_revision = UCSI_CABLE_PROP_FLAG_PD_MAJOR_REV_AS_BCD(cable_prop.flags);
964 cable = typec_register_cable(con->port, &desc);
966 dev_err(con->ucsi->dev,
967 "con%d: failed to register cable (%ld)\n", con->num,
969 return PTR_ERR(cable);
976 static void ucsi_unregister_cable(struct ucsi_connector *con)
981 ucsi_unregister_plug(con);
982 typec_unregister_cable(con->cable);
983 memset(&con->cable_identity, 0, sizeof(con->cable_identity));
987 static int ucsi_check_connector_capability(struct ucsi_connector *con)
993 if (!con->partner || con->ucsi->version < UCSI_VERSION_2_1)
996 command = UCSI_GET_CONNECTOR_CAPABILITY | UCSI_CONNECTOR_NUMBER(con->num);
997 ret = ucsi_send_command(con->ucsi, command, &con->cap, sizeof(con->cap));
999 dev_err(con->ucsi->dev, "GET_CONNECTOR_CAPABILITY failed (%d)\n", ret);
1003 pd_revision = UCSI_CONCAP(con, PARTNER_PD_REVISION_V2_1);
1004 typec_partner_set_pd_revision(con->partner, UCSI_SPEC_REVISION_TO_BCD(pd_revision));
1009 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
1011 switch (UCSI_CONSTAT(con, PWR_OPMODE)) {
1012 case UCSI_CONSTAT_PWR_OPMODE_PD:
1013 con->rdo = UCSI_CONSTAT(con, RDO);
1014 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
1015 ucsi_partner_task(con, ucsi_get_src_pdos, 30, 0);
1016 ucsi_partner_task(con, ucsi_check_altmodes, 30, HZ);
1017 ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ);
1018 ucsi_partner_task(con, ucsi_check_connector_capability, 1, HZ);
1020 case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
1022 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
1024 case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
1026 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
1030 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
1035 static int ucsi_register_partner(struct ucsi_connector *con)
1037 u8 pwr_opmode = UCSI_CONSTAT(con, PWR_OPMODE);
1038 struct typec_partner_desc desc;
1039 struct typec_partner *partner;
1044 memset(&desc, 0, sizeof(desc));
1046 switch (UCSI_CONSTAT(con, PARTNER_TYPE)) {
1047 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
1048 desc.accessory = TYPEC_ACCESSORY_DEBUG;
1050 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
1051 desc.accessory = TYPEC_ACCESSORY_AUDIO;
1057 if (pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD)
1058 ucsi_register_device_pdos(con);
1060 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
1061 desc.identity = &con->partner_identity;
1062 desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD;
1064 if (con->ucsi->version >= UCSI_VERSION_2_1) {
1065 u64 pd_revision = UCSI_CONCAP(con, PARTNER_PD_REVISION_V2_1);
1066 desc.pd_revision = UCSI_SPEC_REVISION_TO_BCD(pd_revision);
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;
1079 if (con->ucsi->version >= UCSI_VERSION_3_0 &&
1080 UCSI_CONSTAT(con, PARTNER_FLAG_USB4_GEN4))
1081 typec_partner_set_usb_mode(partner, USB_MODE_USB4);
1082 else if (con->ucsi->version >= UCSI_VERSION_2_0 &&
1083 UCSI_CONSTAT(con, PARTNER_FLAG_USB4_GEN3))
1084 typec_partner_set_usb_mode(partner, USB_MODE_USB4);
1089 static void ucsi_unregister_partner(struct ucsi_connector *con)
1094 typec_set_mode(con->port, TYPEC_STATE_SAFE);
1096 typec_partner_set_usb_power_delivery(con->partner, NULL);
1097 ucsi_unregister_partner_pdos(con);
1098 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
1099 ucsi_unregister_cable(con);
1100 typec_unregister_partner(con->partner);
1101 memset(&con->partner_identity, 0, sizeof(con->partner_identity));
1102 con->partner = NULL;
1105 static void ucsi_partner_change(struct ucsi_connector *con)
1107 enum usb_role u_role = USB_ROLE_NONE;
1110 switch (UCSI_CONSTAT(con, PARTNER_TYPE)) {
1111 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1112 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1113 u_role = USB_ROLE_HOST;
1115 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1116 typec_set_data_role(con->port, TYPEC_HOST);
1118 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1119 u_role = USB_ROLE_DEVICE;
1120 typec_set_data_role(con->port, TYPEC_DEVICE);
1126 if (UCSI_CONSTAT(con, CONNECTED)) {
1127 switch (UCSI_CONSTAT(con, PARTNER_TYPE)) {
1128 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
1129 typec_set_mode(con->port, TYPEC_MODE_DEBUG);
1131 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
1132 typec_set_mode(con->port, TYPEC_MODE_AUDIO);
1135 if (UCSI_CONSTAT(con, PARTNER_FLAG_USB))
1136 typec_set_mode(con->port, TYPEC_STATE_USB);
1140 /* Only notify USB controller if partner supports USB data */
1141 if (!(UCSI_CONSTAT(con, PARTNER_FLAG_USB)))
1142 u_role = USB_ROLE_NONE;
1144 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1146 dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n",
1150 static int ucsi_check_connection(struct ucsi_connector *con)
1152 u8 prev_state = UCSI_CONSTAT(con, CONNECTED);
1155 ret = ucsi_get_connector_status(con, false);
1157 dev_err(con->ucsi->dev, "GET_CONNECTOR_STATUS failed (%d)\n", ret);
1161 if (UCSI_CONSTAT(con, CONNECTED)) {
1164 ucsi_register_partner(con);
1165 ucsi_pwr_opmode_change(con);
1166 ucsi_partner_change(con);
1168 ucsi_partner_change(con);
1169 ucsi_port_psy_changed(con);
1170 ucsi_unregister_partner(con);
1176 static int ucsi_check_cable(struct ucsi_connector *con)
1178 int ret, num_plug_am;
1183 ret = ucsi_register_cable(con);
1187 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE) {
1188 ret = ucsi_get_cable_identity(con);
1193 if (con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS) {
1194 ret = ucsi_register_plug(con);
1198 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP_P);
1202 if (con->plug_altmode[0]) {
1203 num_plug_am = ucsi_get_num_altmode(con->plug_altmode);
1204 typec_plug_set_num_altmodes(con->plug, num_plug_am);
1206 typec_plug_set_num_altmodes(con->plug, 0);
1213 static void ucsi_handle_connector_change(struct work_struct *work)
1215 struct ucsi_connector *con = container_of(work, struct ucsi_connector,
1217 struct ucsi *ucsi = con->ucsi;
1218 enum typec_role role;
1222 mutex_lock(&con->lock);
1224 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
1225 dev_err_once(ucsi->dev, "%s entered without EVENT_PENDING\n",
1228 ret = ucsi_get_connector_status(con, true);
1230 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
1232 clear_bit(EVENT_PENDING, &con->ucsi->flags);
1236 trace_ucsi_connector_change(con->num, con);
1238 if (ucsi->ops->connector_status)
1239 ucsi->ops->connector_status(con);
1241 change = UCSI_CONSTAT(con, CHANGE);
1242 role = UCSI_CONSTAT(con, PWR_DIR);
1244 if (change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
1245 typec_set_pwr_role(con->port, role);
1247 /* Complete pending power role swap */
1248 if (!completion_done(&con->complete))
1249 complete(&con->complete);
1252 if (change & UCSI_CONSTAT_CONNECT_CHANGE) {
1253 typec_set_pwr_role(con->port, role);
1254 ucsi_port_psy_changed(con);
1255 ucsi_partner_change(con);
1257 if (UCSI_CONSTAT(con, CONNECTED)) {
1258 ucsi_register_partner(con);
1259 ucsi_partner_task(con, ucsi_check_connection, 1, HZ);
1260 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
1261 ucsi_partner_task(con, ucsi_get_partner_identity, 1, HZ);
1262 if (con->ucsi->cap.features & UCSI_CAP_CABLE_DETAILS)
1263 ucsi_partner_task(con, ucsi_check_cable, 1, HZ);
1265 if (UCSI_CONSTAT(con, PWR_OPMODE) == UCSI_CONSTAT_PWR_OPMODE_PD) {
1266 ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ);
1267 ucsi_partner_task(con, ucsi_check_connector_capability, 1, HZ);
1270 ucsi_unregister_partner(con);
1274 if (change & (UCSI_CONSTAT_POWER_OPMODE_CHANGE | UCSI_CONSTAT_POWER_LEVEL_CHANGE))
1275 ucsi_pwr_opmode_change(con);
1277 if (con->partner && (change & UCSI_CONSTAT_PARTNER_CHANGE)) {
1278 ucsi_partner_change(con);
1280 /* Complete pending data role swap */
1281 if (!completion_done(&con->complete))
1282 complete(&con->complete);
1285 if (change & UCSI_CONSTAT_CAM_CHANGE)
1286 ucsi_partner_task(con, ucsi_check_altmodes, 1, HZ);
1288 if (change & UCSI_CONSTAT_BC_CHANGE)
1289 ucsi_port_psy_changed(con);
1292 mutex_unlock(&con->lock);
1296 * ucsi_connector_change - Process Connector Change Event
1297 * @ucsi: UCSI Interface
1298 * @num: Connector number
1300 void ucsi_connector_change(struct ucsi *ucsi, u8 num)
1302 struct ucsi_connector *con = &ucsi->connector[num - 1];
1304 if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
1305 dev_dbg(ucsi->dev, "Early connector change event\n");
1309 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
1310 schedule_work(&con->work);
1312 EXPORT_SYMBOL_GPL(ucsi_connector_change);
1314 /* -------------------------------------------------------------------------- */
1317 * Hard Reset bit field was defined with value 1 in UCSI spec version 1.0.
1318 * Starting with spec version 1.1, Hard Reset bit field was removed from the
1319 * CONNECTOR_RESET command, until spec 2.0 reintroduced it with value 0, so, in effect,
1320 * the value to pass in to the command for a Hard Reset is different depending
1321 * on the supported UCSI version by the LPM.
1323 * For performing a Data Reset on LPMs supporting version 2.0 and greater,
1324 * this function needs to be called with the second argument set to 0.
1326 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
1330 command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num);
1332 if (con->ucsi->version < UCSI_VERSION_1_1)
1333 command |= hard ? UCSI_CONNECTOR_RESET_HARD_VER_1_0 : 0;
1334 else if (con->ucsi->version >= UCSI_VERSION_2_0)
1335 command |= hard ? 0 : UCSI_CONNECTOR_RESET_DATA_VER_2_0;
1337 return ucsi_send_command(con->ucsi, command, NULL, 0);
1340 static int ucsi_reset_ppm(struct ucsi *ucsi)
1347 mutex_lock(&ucsi->ppm_lock);
1349 ret = ucsi->ops->read_cci(ucsi, &cci);
1354 * If UCSI_CCI_RESET_COMPLETE is already set we must clear
1355 * the flag before we start another reset. Send a
1356 * UCSI_SET_NOTIFICATION_ENABLE command to achieve this.
1357 * Ignore a timeout and try the reset anyway if this fails.
1359 if (cci & UCSI_CCI_RESET_COMPLETE) {
1360 command = UCSI_SET_NOTIFICATION_ENABLE;
1361 ret = ucsi->ops->async_control(ucsi, command);
1365 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
1367 ret = ucsi->ops->read_cci(ucsi, &cci);
1370 if (cci & UCSI_CCI_COMMAND_COMPLETE)
1372 if (time_is_before_jiffies(tmo))
1377 WARN_ON(cci & UCSI_CCI_RESET_COMPLETE);
1380 command = UCSI_PPM_RESET;
1381 ret = ucsi->ops->async_control(ucsi, command);
1385 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
1388 if (time_is_before_jiffies(tmo)) {
1393 /* Give the PPM time to process a reset before reading CCI */
1396 ret = ucsi->ops->read_cci(ucsi, &cci);
1400 /* If the PPM is still doing something else, reset it again. */
1401 if (cci & ~UCSI_CCI_RESET_COMPLETE) {
1402 ret = ucsi->ops->async_control(ucsi, command);
1407 } while (!(cci & UCSI_CCI_RESET_COMPLETE));
1410 mutex_unlock(&ucsi->ppm_lock);
1414 static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)
1418 ret = ucsi_send_command(con->ucsi, command, NULL, 0);
1419 if (ret == -ETIMEDOUT) {
1422 /* PPM most likely stopped responding. Resetting everything. */
1423 ucsi_reset_ppm(con->ucsi);
1425 c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
1426 ucsi_send_command(con->ucsi, c, NULL, 0);
1428 ucsi_reset_connector(con, true);
1434 static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
1436 struct ucsi_connector *con = typec_get_drvdata(port);
1441 mutex_lock(&con->lock);
1443 if (!con->partner) {
1448 partner_type = UCSI_CONSTAT(con, PARTNER_TYPE);
1449 if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
1450 role == TYPEC_DEVICE) ||
1451 (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
1452 role == TYPEC_HOST))
1455 reinit_completion(&con->complete);
1457 command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num);
1458 command |= UCSI_SET_UOR_ROLE(role);
1459 command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS;
1460 ret = ucsi_role_cmd(con, command);
1464 mutex_unlock(&con->lock);
1466 if (!wait_for_completion_timeout(&con->complete,
1467 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1473 mutex_unlock(&con->lock);
1478 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
1480 struct ucsi_connector *con = typec_get_drvdata(port);
1481 enum typec_role cur_role;
1485 mutex_lock(&con->lock);
1487 if (!con->partner) {
1492 cur_role = UCSI_CONSTAT(con, PWR_DIR);
1494 if (cur_role == role)
1497 reinit_completion(&con->complete);
1499 command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num);
1500 command |= UCSI_SET_PDR_ROLE(role);
1501 command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS;
1502 ret = ucsi_role_cmd(con, command);
1506 mutex_unlock(&con->lock);
1508 if (!wait_for_completion_timeout(&con->complete,
1509 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1512 mutex_lock(&con->lock);
1514 /* Something has gone wrong while swapping the role */
1515 if (UCSI_CONSTAT(con, PWR_OPMODE) != UCSI_CONSTAT_PWR_OPMODE_PD) {
1516 ucsi_reset_connector(con, true);
1521 mutex_unlock(&con->lock);
1526 static const struct typec_operations ucsi_ops = {
1527 .dr_set = ucsi_dr_swap,
1528 .pr_set = ucsi_pr_swap
1531 /* Caller must call fwnode_handle_put() after use */
1532 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
1534 struct fwnode_handle *fwnode;
1537 device_for_each_child_node(con->ucsi->dev, fwnode)
1538 if (i++ == con->num)
1543 static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con)
1545 struct typec_capability *cap = &con->typec_cap;
1546 enum typec_accessory *accessory = cap->accessory;
1547 enum usb_role u_role = USB_ROLE_NONE;
1552 name = kasprintf(GFP_KERNEL, "%s-con%d", dev_name(ucsi->dev), con->num);
1556 con->wq = create_singlethread_workqueue(name);
1561 INIT_WORK(&con->work, ucsi_handle_connector_change);
1562 init_completion(&con->complete);
1563 mutex_init(&con->lock);
1564 INIT_LIST_HEAD(&con->partner_tasks);
1567 cap->fwnode = ucsi_find_fwnode(con);
1568 con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode);
1569 if (IS_ERR(con->usb_role_sw))
1570 return dev_err_probe(ucsi->dev, PTR_ERR(con->usb_role_sw),
1571 "con%d: failed to get usb role switch\n", con->num);
1573 /* Delay other interactions with the con until registration is complete */
1574 mutex_lock(&con->lock);
1576 /* Get connector capability */
1577 command = UCSI_GET_CONNECTOR_CAPABILITY;
1578 command |= UCSI_CONNECTOR_NUMBER(con->num);
1579 ret = ucsi_send_command(ucsi, command, &con->cap, sizeof(con->cap));
1583 if (UCSI_CONCAP(con, OPMODE_DRP))
1584 cap->data = TYPEC_PORT_DRD;
1585 else if (UCSI_CONCAP(con, OPMODE_DFP))
1586 cap->data = TYPEC_PORT_DFP;
1587 else if (UCSI_CONCAP(con, OPMODE_UFP))
1588 cap->data = TYPEC_PORT_UFP;
1590 if (UCSI_CONCAP(con, PROVIDER) && UCSI_CONCAP(con, CONSUMER))
1591 cap->type = TYPEC_PORT_DRP;
1592 else if (UCSI_CONCAP(con, PROVIDER))
1593 cap->type = TYPEC_PORT_SRC;
1594 else if (UCSI_CONCAP(con, CONSUMER))
1595 cap->type = TYPEC_PORT_SNK;
1597 cap->revision = ucsi->cap.typec_version;
1598 cap->pd_revision = ucsi->cap.pd_version;
1599 cap->svdm_version = SVDM_VER_2_0;
1600 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
1602 if (UCSI_CONCAP(con, OPMODE_AUDIO_ACCESSORY))
1603 *accessory++ = TYPEC_ACCESSORY_AUDIO;
1604 if (UCSI_CONCAP(con, OPMODE_DEBUG_ACCESSORY))
1605 *accessory = TYPEC_ACCESSORY_DEBUG;
1607 if (UCSI_CONCAP_USB2_SUPPORT(con))
1608 cap->usb_capability |= USB_CAPABILITY_USB2;
1609 if (UCSI_CONCAP_USB3_SUPPORT(con))
1610 cap->usb_capability |= USB_CAPABILITY_USB3;
1611 if (UCSI_CONCAP_USB4_SUPPORT(con))
1612 cap->usb_capability |= USB_CAPABILITY_USB4;
1614 cap->driver_data = con;
1615 cap->ops = &ucsi_ops;
1617 if (ucsi->ops->update_connector)
1618 ucsi->ops->update_connector(con);
1620 ret = ucsi_register_port_psy(con);
1624 /* Register the connector */
1625 con->port = typec_register_port(ucsi->dev, cap);
1626 if (IS_ERR(con->port)) {
1627 ret = PTR_ERR(con->port);
1631 if (!(ucsi->quirks & UCSI_DELAY_DEVICE_PDOS))
1632 ucsi_register_device_pdos(con);
1634 /* Alternate modes */
1635 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON);
1637 dev_err(ucsi->dev, "con%d: failed to register alt modes\n",
1642 /* Get the status */
1643 ret = ucsi_get_connector_status(con, false);
1645 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
1649 if (ucsi->ops->connector_status)
1650 ucsi->ops->connector_status(con);
1652 switch (UCSI_CONSTAT(con, PARTNER_TYPE)) {
1653 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1654 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1655 u_role = USB_ROLE_HOST;
1657 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1658 typec_set_data_role(con->port, TYPEC_HOST);
1660 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1661 u_role = USB_ROLE_DEVICE;
1662 typec_set_data_role(con->port, TYPEC_DEVICE);
1668 /* Check if there is already something connected */
1669 if (UCSI_CONSTAT(con, CONNECTED)) {
1670 typec_set_pwr_role(con->port, UCSI_CONSTAT(con, PWR_DIR));
1671 ucsi_register_partner(con);
1672 ucsi_pwr_opmode_change(con);
1673 ucsi_port_psy_changed(con);
1674 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
1675 ucsi_get_partner_identity(con);
1676 if (con->ucsi->cap.features & UCSI_CAP_CABLE_DETAILS)
1677 ucsi_check_cable(con);
1680 /* Only notify USB controller if partner supports USB data */
1681 if (!(UCSI_CONSTAT(con, PARTNER_FLAG_USB)))
1682 u_role = USB_ROLE_NONE;
1684 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1686 dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n",
1691 if (con->partner && UCSI_CONSTAT(con, PWR_OPMODE) == UCSI_CONSTAT_PWR_OPMODE_PD) {
1692 ucsi_register_device_pdos(con);
1693 ucsi_get_src_pdos(con);
1694 ucsi_check_altmodes(con);
1695 ucsi_check_connector_capability(con);
1698 trace_ucsi_register_port(con->num, con);
1701 fwnode_handle_put(cap->fwnode);
1703 mutex_unlock(&con->lock);
1705 if (ret && con->wq) {
1706 destroy_workqueue(con->wq);
1713 static u64 ucsi_get_supported_notifications(struct ucsi *ucsi)
1715 u16 features = ucsi->cap.features;
1716 u64 ntfy = UCSI_ENABLE_NTFY_ALL;
1718 if (!(features & UCSI_CAP_ALT_MODE_DETAILS))
1719 ntfy &= ~UCSI_ENABLE_NTFY_CAM_CHANGE;
1721 if (!(features & UCSI_CAP_PDO_DETAILS))
1722 ntfy &= ~(UCSI_ENABLE_NTFY_PWR_LEVEL_CHANGE |
1723 UCSI_ENABLE_NTFY_CAP_CHANGE);
1725 if (!(features & UCSI_CAP_EXT_SUPPLY_NOTIFICATIONS))
1726 ntfy &= ~UCSI_ENABLE_NTFY_EXT_PWR_SRC_CHANGE;
1728 if (!(features & UCSI_CAP_PD_RESET))
1729 ntfy &= ~UCSI_ENABLE_NTFY_PD_RESET_COMPLETE;
1731 if (ucsi->version <= UCSI_VERSION_1_2)
1734 ntfy |= UCSI_ENABLE_NTFY_SINK_PATH_STS_CHANGE;
1736 if (features & UCSI_CAP_GET_ATTENTION_VDO)
1737 ntfy |= UCSI_ENABLE_NTFY_ATTENTION;
1739 if (features & UCSI_CAP_FW_UPDATE_REQUEST)
1740 ntfy |= UCSI_ENABLE_NTFY_LPM_FW_UPDATE_REQ;
1742 if (features & UCSI_CAP_SECURITY_REQUEST)
1743 ntfy |= UCSI_ENABLE_NTFY_SECURITY_REQ_PARTNER;
1745 if (features & UCSI_CAP_SET_RETIMER_MODE)
1746 ntfy |= UCSI_ENABLE_NTFY_SET_RETIMER_MODE;
1752 * ucsi_init - Initialize UCSI interface
1753 * @ucsi: UCSI to be initialized
1755 * Registers all ports @ucsi has and enables all notification events.
1757 static int ucsi_init(struct ucsi *ucsi)
1759 struct ucsi_connector *con, *connector;
1766 ret = ucsi_reset_ppm(ucsi);
1768 dev_err(ucsi->dev, "failed to reset PPM!\n");
1772 /* Enable basic notifications */
1773 ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
1774 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1775 ret = ucsi_send_command(ucsi, command, NULL, 0);
1779 /* Get PPM capabilities */
1780 command = UCSI_GET_CAPABILITY;
1781 ret = ucsi_send_command(ucsi, command, &ucsi->cap,
1782 BITS_TO_BYTES(UCSI_GET_CAPABILITY_SIZE));
1786 if (!ucsi->cap.num_connectors) {
1791 /* Allocate the connectors. Released in ucsi_unregister() */
1792 connector = kcalloc(ucsi->cap.num_connectors + 1, sizeof(*connector), GFP_KERNEL);
1798 /* Register all connectors */
1799 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1800 connector[i].num = i + 1;
1801 ret = ucsi_register_port(ucsi, &connector[i]);
1803 goto err_unregister;
1806 /* Enable all supported notifications */
1807 ntfy = ucsi_get_supported_notifications(ucsi);
1808 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1809 ret = ucsi_send_command(ucsi, command, NULL, 0);
1811 goto err_unregister;
1813 ucsi->connector = connector;
1816 mutex_lock(&ucsi->ppm_lock);
1817 ret = ucsi->ops->read_cci(ucsi, &cci);
1818 mutex_unlock(&ucsi->ppm_lock);
1821 if (UCSI_CCI_CONNECTOR(cci))
1822 ucsi_connector_change(ucsi, UCSI_CCI_CONNECTOR(cci));
1827 for (con = connector; con->port; con++) {
1828 ucsi_unregister_partner(con);
1829 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON);
1830 ucsi_unregister_port_psy(con);
1832 destroy_workqueue(con->wq);
1834 usb_power_delivery_unregister_capabilities(con->port_sink_caps);
1835 con->port_sink_caps = NULL;
1836 usb_power_delivery_unregister_capabilities(con->port_source_caps);
1837 con->port_source_caps = NULL;
1838 usb_power_delivery_unregister(con->pd);
1840 typec_unregister_port(con->port);
1845 memset(&ucsi->cap, 0, sizeof(ucsi->cap));
1846 ucsi_reset_ppm(ucsi);
1851 static void ucsi_resume_work(struct work_struct *work)
1853 struct ucsi *ucsi = container_of(work, struct ucsi, resume_work);
1854 struct ucsi_connector *con;
1858 /* Restore UCSI notification enable mask after system resume */
1859 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1860 ret = ucsi_send_command(ucsi, command, NULL, 0);
1862 dev_err(ucsi->dev, "failed to re-enable notifications (%d)\n", ret);
1866 for (con = ucsi->connector; con->port; con++) {
1867 mutex_lock(&con->lock);
1868 ucsi_partner_task(con, ucsi_check_connection, 1, 0);
1869 mutex_unlock(&con->lock);
1873 int ucsi_resume(struct ucsi *ucsi)
1875 if (ucsi->connector)
1876 queue_work(system_long_wq, &ucsi->resume_work);
1879 EXPORT_SYMBOL_GPL(ucsi_resume);
1881 static void ucsi_init_work(struct work_struct *work)
1883 struct ucsi *ucsi = container_of(work, struct ucsi, work.work);
1886 ret = ucsi_init(ucsi);
1888 dev_err_probe(ucsi->dev, ret, "PPM init failed\n");
1890 if (ret == -EPROBE_DEFER) {
1891 if (ucsi->work_count++ > UCSI_ROLE_SWITCH_WAIT_COUNT) {
1892 dev_err(ucsi->dev, "PPM init failed, stop trying\n");
1896 queue_delayed_work(system_long_wq, &ucsi->work,
1897 UCSI_ROLE_SWITCH_INTERVAL);
1902 * ucsi_get_drvdata - Return private driver data pointer
1903 * @ucsi: UCSI interface
1905 void *ucsi_get_drvdata(struct ucsi *ucsi)
1907 return ucsi->driver_data;
1909 EXPORT_SYMBOL_GPL(ucsi_get_drvdata);
1912 * ucsi_set_drvdata - Assign private driver data pointer
1913 * @ucsi: UCSI interface
1914 * @data: Private data pointer
1916 void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
1918 ucsi->driver_data = data;
1920 EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
1923 * ucsi_create - Allocate UCSI instance
1924 * @dev: Device interface to the PPM (Platform Policy Manager)
1925 * @ops: I/O routines
1927 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
1932 !ops->read_version || !ops->read_cci || !ops->read_message_in ||
1933 !ops->sync_control || !ops->async_control)
1934 return ERR_PTR(-EINVAL);
1936 ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
1938 return ERR_PTR(-ENOMEM);
1940 INIT_WORK(&ucsi->resume_work, ucsi_resume_work);
1941 INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work);
1942 mutex_init(&ucsi->ppm_lock);
1943 init_completion(&ucsi->complete);
1949 EXPORT_SYMBOL_GPL(ucsi_create);
1952 * ucsi_destroy - Free UCSI instance
1953 * @ucsi: UCSI instance to be freed
1955 void ucsi_destroy(struct ucsi *ucsi)
1957 ucsi_debugfs_unregister(ucsi);
1960 EXPORT_SYMBOL_GPL(ucsi_destroy);
1963 * ucsi_register - Register UCSI interface
1964 * @ucsi: UCSI instance
1966 int ucsi_register(struct ucsi *ucsi)
1970 ret = ucsi->ops->read_version(ucsi, &ucsi->version);
1978 * Version format is JJ.M.N (JJ = Major version, M = Minor version,
1979 * N = sub-minor version).
1981 dev_dbg(ucsi->dev, "Registered UCSI interface with version %x.%x.%x",
1982 UCSI_BCD_GET_MAJOR(ucsi->version),
1983 UCSI_BCD_GET_MINOR(ucsi->version),
1984 UCSI_BCD_GET_SUBMINOR(ucsi->version));
1986 queue_delayed_work(system_long_wq, &ucsi->work, 0);
1988 ucsi_debugfs_register(ucsi);
1991 EXPORT_SYMBOL_GPL(ucsi_register);
1994 * ucsi_unregister - Unregister UCSI interface
1995 * @ucsi: UCSI interface to be unregistered
1997 * Unregister UCSI interface that was created with ucsi_register().
1999 void ucsi_unregister(struct ucsi *ucsi)
2001 u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
2004 /* Make sure that we are not in the middle of driver initialization */
2005 cancel_delayed_work_sync(&ucsi->work);
2006 cancel_work_sync(&ucsi->resume_work);
2008 /* Disable notifications */
2009 ucsi->ops->async_control(ucsi, cmd);
2011 if (!ucsi->connector)
2014 for (i = 0; i < ucsi->cap.num_connectors; i++) {
2015 cancel_work_sync(&ucsi->connector[i].work);
2016 ucsi_unregister_partner(&ucsi->connector[i]);
2017 ucsi_unregister_altmodes(&ucsi->connector[i],
2018 UCSI_RECIPIENT_CON);
2019 ucsi_unregister_port_psy(&ucsi->connector[i]);
2021 if (ucsi->connector[i].wq) {
2022 struct ucsi_work *uwork;
2024 mutex_lock(&ucsi->connector[i].lock);
2026 * queue delayed items immediately so they can execute
2027 * and free themselves before the wq is destroyed
2029 list_for_each_entry(uwork, &ucsi->connector[i].partner_tasks, node)
2030 mod_delayed_work(ucsi->connector[i].wq, &uwork->work, 0);
2031 mutex_unlock(&ucsi->connector[i].lock);
2032 destroy_workqueue(ucsi->connector[i].wq);
2035 usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_sink_caps);
2036 ucsi->connector[i].port_sink_caps = NULL;
2037 usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_source_caps);
2038 ucsi->connector[i].port_source_caps = NULL;
2039 usb_power_delivery_unregister(ucsi->connector[i].pd);
2040 ucsi->connector[i].pd = NULL;
2041 typec_unregister_port(ucsi->connector[i].port);
2044 kfree(ucsi->connector);
2046 EXPORT_SYMBOL_GPL(ucsi_unregister);
2048 static int __init ucsi_module_init(void)
2050 ucsi_debugfs_init();
2053 module_init(ucsi_module_init);
2055 static void __exit ucsi_module_exit(void)
2057 ucsi_debugfs_exit();
2059 module_exit(ucsi_module_exit);
2062 MODULE_LICENSE("GPL v2");
2063 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");