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) {
155 if (ucsi_acknowledge_command(ucsi) < 0)
157 "ACK of unsupported command failed\n");
161 if (cci & UCSI_CCI_ERROR) {
162 if (cmd == UCSI_GET_ERROR_STATUS)
164 return ucsi_read_error(ucsi);
167 if (cmd == UCSI_CANCEL && cci & UCSI_CCI_CANCEL_COMPLETE) {
168 ret = ucsi_acknowledge_command(ucsi);
169 return ret ? ret : -EBUSY;
172 return UCSI_CCI_LENGTH(cci);
175 int ucsi_send_command(struct ucsi *ucsi, u64 command,
176 void *data, size_t size)
181 mutex_lock(&ucsi->ppm_lock);
183 ret = ucsi_exec_command(ucsi, command);
190 ret = ucsi_read_message_in(ucsi, data, size);
195 ret = ucsi_acknowledge_command(ucsi);
201 mutex_unlock(&ucsi->ppm_lock);
204 EXPORT_SYMBOL_GPL(ucsi_send_command);
206 /* -------------------------------------------------------------------------- */
209 struct delayed_work work;
210 struct list_head node;
213 struct ucsi_connector *con;
214 int (*cb)(struct ucsi_connector *);
217 static void ucsi_poll_worker(struct work_struct *work)
219 struct ucsi_work *uwork = container_of(work, struct ucsi_work, work.work);
220 struct ucsi_connector *con = uwork->con;
223 mutex_lock(&con->lock);
226 list_del(&uwork->node);
227 mutex_unlock(&con->lock);
232 ret = uwork->cb(con);
234 if (uwork->count-- && (ret == -EBUSY || ret == -ETIMEDOUT)) {
235 queue_delayed_work(con->wq, &uwork->work, uwork->delay);
237 list_del(&uwork->node);
241 mutex_unlock(&con->lock);
244 static int ucsi_partner_task(struct ucsi_connector *con,
245 int (*cb)(struct ucsi_connector *),
246 int retries, unsigned long delay)
248 struct ucsi_work *uwork;
253 uwork = kzalloc(sizeof(*uwork), GFP_KERNEL);
257 INIT_DELAYED_WORK(&uwork->work, ucsi_poll_worker);
258 uwork->count = retries;
259 uwork->delay = delay;
263 list_add_tail(&uwork->node, &con->partner_tasks);
264 queue_delayed_work(con->wq, &uwork->work, delay);
269 /* -------------------------------------------------------------------------- */
271 void ucsi_altmode_update_active(struct ucsi_connector *con)
273 const struct typec_altmode *altmode = NULL;
279 command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
280 ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur));
282 if (con->ucsi->version > 0x0100) {
283 dev_err(con->ucsi->dev,
284 "GET_CURRENT_CAM command failed\n");
290 if (cur < UCSI_MAX_ALTMODES)
291 altmode = typec_altmode_get_partner(con->port_altmode[cur]);
293 for (i = 0; con->partner_altmode[i]; i++)
294 typec_altmode_update_active(con->partner_altmode[i],
295 con->partner_altmode[i] == altmode);
298 static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
303 for (i = 0; alt[i]; i++) {
304 if (i > MODE_DISCOVERY_MAX)
307 if (alt[i]->svid == svid)
314 static int ucsi_next_altmode(struct typec_altmode **alt)
318 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
325 static int ucsi_get_num_altmode(struct typec_altmode **alt)
329 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
336 static int ucsi_register_altmode(struct ucsi_connector *con,
337 struct typec_altmode_desc *desc,
340 struct typec_altmode *alt;
345 override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
348 case UCSI_RECIPIENT_CON:
349 i = ucsi_next_altmode(con->port_altmode);
355 ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid);
361 switch (desc->svid) {
362 case USB_TYPEC_DP_SID:
363 alt = ucsi_register_displayport(con, override, i, desc);
365 case USB_TYPEC_NVIDIA_VLINK_SID:
366 if (desc->vdo == USB_TYPEC_NVIDIA_VLINK_DBG_VDO)
367 alt = typec_port_register_altmode(con->port,
370 alt = ucsi_register_displayport(con, override,
374 alt = typec_port_register_altmode(con->port, desc);
383 con->port_altmode[i] = alt;
385 case UCSI_RECIPIENT_SOP:
386 i = ucsi_next_altmode(con->partner_altmode);
392 ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid);
398 alt = typec_partner_register_altmode(con->partner, desc);
404 con->partner_altmode[i] = alt;
406 case UCSI_RECIPIENT_SOP_P:
407 i = ucsi_next_altmode(con->plug_altmode);
413 ret = ucsi_altmode_next_mode(con->plug_altmode, desc->svid);
419 alt = typec_plug_register_altmode(con->plug, desc);
425 con->plug_altmode[i] = alt;
431 trace_ucsi_register_altmode(recipient, alt);
436 dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n",
437 desc->svid, desc->mode);
443 ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient)
445 int max_altmodes = UCSI_MAX_ALTMODES;
446 struct typec_altmode_desc desc;
447 struct ucsi_altmode alt;
448 struct ucsi_altmode orig[UCSI_MAX_ALTMODES];
449 struct ucsi_altmode updated[UCSI_MAX_ALTMODES];
450 struct ucsi *ucsi = con->ucsi;
451 bool multi_dp = false;
458 if (recipient == UCSI_RECIPIENT_CON)
459 max_altmodes = con->ucsi->cap.num_alt_modes;
461 memset(orig, 0, sizeof(orig));
462 memset(updated, 0, sizeof(updated));
464 /* First get all the alternate modes */
465 for (i = 0; i < max_altmodes; i++) {
466 memset(&alt, 0, sizeof(alt));
467 command = UCSI_GET_ALTERNATE_MODES;
468 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
469 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
470 command |= UCSI_GET_ALTMODE_OFFSET(i);
471 len = ucsi_send_command(con->ucsi, command, &alt, sizeof(alt));
473 * We are collecting all altmodes first and then registering.
474 * Some type-C device will return zero length data beyond last
475 * alternate modes. We should not return if length is zero.
480 /* We got all altmodes, now break out and register them */
481 if (!len || !alt.svid)
484 orig[k].mid = alt.mid;
485 orig[k].svid = alt.svid;
489 * Update the original altmode table as some ppms may report
490 * multiple DP altmodes.
492 if (recipient == UCSI_RECIPIENT_CON)
493 multi_dp = ucsi->ops->update_altmodes(ucsi, orig, updated);
495 /* now register altmodes */
496 for (i = 0; i < max_altmodes; i++) {
497 memset(&desc, 0, sizeof(desc));
498 if (multi_dp && recipient == UCSI_RECIPIENT_CON) {
499 desc.svid = updated[i].svid;
500 desc.vdo = updated[i].mid;
502 desc.svid = orig[i].svid;
503 desc.vdo = orig[i].mid;
505 desc.roles = TYPEC_PORT_DRD;
510 ret = ucsi_register_altmode(con, &desc, recipient);
518 static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
520 int max_altmodes = UCSI_MAX_ALTMODES;
521 struct typec_altmode_desc desc;
522 struct ucsi_altmode alt[2];
530 if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
533 if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
536 if (con->ucsi->ops->update_altmodes)
537 return ucsi_register_altmodes_nvidia(con, recipient);
539 if (recipient == UCSI_RECIPIENT_CON)
540 max_altmodes = con->ucsi->cap.num_alt_modes;
542 for (i = 0; i < max_altmodes;) {
543 memset(alt, 0, sizeof(alt));
544 command = UCSI_GET_ALTERNATE_MODES;
545 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
546 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
547 command |= UCSI_GET_ALTMODE_OFFSET(i);
548 len = ucsi_send_command(con->ucsi, command, alt, sizeof(alt));
555 * This code is requesting one alt mode at a time, but some PPMs
556 * may still return two. If that happens both alt modes need be
557 * registered and the offset for the next alt mode has to be
560 num = len / sizeof(alt[0]);
563 for (j = 0; j < num; j++) {
567 memset(&desc, 0, sizeof(desc));
568 desc.vdo = alt[j].mid;
569 desc.svid = alt[j].svid;
570 desc.roles = TYPEC_PORT_DRD;
572 ret = ucsi_register_altmode(con, &desc, recipient);
581 static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
583 const struct typec_altmode *pdev;
584 struct typec_altmode **adev;
588 case UCSI_RECIPIENT_CON:
589 adev = con->port_altmode;
591 case UCSI_RECIPIENT_SOP:
592 adev = con->partner_altmode;
594 case UCSI_RECIPIENT_SOP_P:
595 adev = con->plug_altmode;
602 if (recipient == UCSI_RECIPIENT_SOP &&
603 (adev[i]->svid == USB_TYPEC_DP_SID ||
604 (adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID &&
605 adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) {
606 pdev = typec_altmode_get_partner(adev[i]);
607 ucsi_displayport_remove_partner((void *)pdev);
609 typec_unregister_altmode(adev[i]);
614 static int ucsi_read_pdos(struct ucsi_connector *con,
615 enum typec_role role, int is_partner,
616 u32 *pdos, int offset, int num_pdos)
618 struct ucsi *ucsi = con->ucsi;
622 if (ucsi->quirks & UCSI_NO_PARTNER_PDOS)
625 command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num);
626 command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner);
627 command |= UCSI_GET_PDOS_PDO_OFFSET(offset);
628 command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1);
629 command |= is_source(role) ? UCSI_GET_PDOS_SRC_PDOS : 0;
630 ret = ucsi_send_command(ucsi, command, pdos + offset,
631 num_pdos * sizeof(u32));
632 if (ret < 0 && ret != -ETIMEDOUT)
633 dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret);
638 static int ucsi_get_pdos(struct ucsi_connector *con, enum typec_role role,
639 int is_partner, u32 *pdos)
644 /* UCSI max payload means only getting at most 4 PDOs at a time */
645 ret = ucsi_read_pdos(con, role, is_partner, pdos, 0, UCSI_MAX_PDOS);
649 num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */
650 if (num_pdos < UCSI_MAX_PDOS)
653 /* get the remaining PDOs, if any */
654 ret = ucsi_read_pdos(con, role, is_partner, pdos, UCSI_MAX_PDOS,
655 PDO_MAX_OBJECTS - UCSI_MAX_PDOS);
659 return ret / sizeof(u32) + num_pdos;
662 static int ucsi_get_src_pdos(struct ucsi_connector *con)
666 ret = ucsi_get_pdos(con, TYPEC_SOURCE, 1, con->src_pdos);
672 ucsi_port_psy_changed(con);
677 static int ucsi_read_identity(struct ucsi_connector *con, u8 recipient,
678 u8 offset, u8 bytes, void *resp)
680 struct ucsi *ucsi = con->ucsi;
684 command = UCSI_COMMAND(UCSI_GET_PD_MESSAGE) |
685 UCSI_CONNECTOR_NUMBER(con->num);
686 command |= UCSI_GET_PD_MESSAGE_RECIPIENT(recipient);
687 command |= UCSI_GET_PD_MESSAGE_OFFSET(offset);
688 command |= UCSI_GET_PD_MESSAGE_BYTES(bytes);
689 command |= UCSI_GET_PD_MESSAGE_TYPE(UCSI_GET_PD_MESSAGE_TYPE_IDENTITY);
691 ret = ucsi_send_command(ucsi, command, resp, bytes);
693 dev_err(ucsi->dev, "UCSI_GET_PD_MESSAGE failed (%d)\n", ret);
698 static int ucsi_get_identity(struct ucsi_connector *con, u8 recipient,
699 struct usb_pd_identity *id)
701 struct ucsi *ucsi = con->ucsi;
702 struct ucsi_pd_message_disc_id resp = {};
705 if (ucsi->version < UCSI_VERSION_2_0) {
707 * Before UCSI v2.0, MESSAGE_IN is 16 bytes which cannot fit
708 * the 28 byte identity response including the VDM header.
709 * First request the VDM header, ID Header VDO, Cert Stat VDO
712 ret = ucsi_read_identity(con, recipient, 0, 0x10, &resp);
717 /* Then request Product Type VDO1 through Product Type VDO3. */
718 ret = ucsi_read_identity(con, recipient, 0x10, 0xc,
725 * In UCSI v2.0 and after, MESSAGE_IN is large enough to request
726 * the large enough to request the full Discover Identity
729 ret = ucsi_read_identity(con, recipient, 0x0, 0x1c, &resp);
734 id->id_header = resp.id_header;
735 id->cert_stat = resp.cert_stat;
736 id->product = resp.product;
737 id->vdo[0] = resp.vdo[0];
738 id->vdo[1] = resp.vdo[1];
739 id->vdo[2] = resp.vdo[2];
743 static int ucsi_get_partner_identity(struct ucsi_connector *con)
747 ret = ucsi_get_identity(con, UCSI_RECIPIENT_SOP,
748 &con->partner_identity);
752 ret = typec_partner_set_identity(con->partner);
754 dev_err(con->ucsi->dev, "Failed to set partner identity (%d)\n",
761 static int ucsi_get_cable_identity(struct ucsi_connector *con)
765 ret = ucsi_get_identity(con, UCSI_RECIPIENT_SOP_P,
766 &con->cable_identity);
770 ret = typec_cable_set_identity(con->cable);
772 dev_err(con->ucsi->dev, "Failed to set cable identity (%d)\n",
779 static int ucsi_check_altmodes(struct ucsi_connector *con)
781 int ret, num_partner_am;
783 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
784 if (ret && ret != -ETIMEDOUT)
785 dev_err(con->ucsi->dev,
786 "con%d: failed to register partner alt modes (%d)\n",
789 /* Ignoring the errors in this case. */
790 if (con->partner_altmode[0]) {
791 num_partner_am = ucsi_get_num_altmode(con->partner_altmode);
792 if (num_partner_am > 0)
793 typec_partner_set_num_altmodes(con->partner, num_partner_am);
794 ucsi_altmode_update_active(con);
801 static int ucsi_register_partner_pdos(struct ucsi_connector *con)
803 struct usb_power_delivery_desc desc = { con->ucsi->cap.pd_version };
804 struct usb_power_delivery_capabilities_desc caps;
805 struct usb_power_delivery_capabilities *cap;
811 con->partner_pd = usb_power_delivery_register(NULL, &desc);
812 if (IS_ERR(con->partner_pd))
813 return PTR_ERR(con->partner_pd);
815 ret = ucsi_get_pdos(con, TYPEC_SOURCE, 1, caps.pdo);
817 if (ret < PDO_MAX_OBJECTS)
820 caps.role = TYPEC_SOURCE;
821 cap = usb_power_delivery_register_capabilities(con->partner_pd, &caps);
825 con->partner_source_caps = cap;
827 ret = typec_partner_set_usb_power_delivery(con->partner, con->partner_pd);
829 usb_power_delivery_unregister_capabilities(con->partner_source_caps);
834 ret = ucsi_get_pdos(con, TYPEC_SINK, 1, caps.pdo);
836 if (ret < PDO_MAX_OBJECTS)
839 caps.role = TYPEC_SINK;
841 cap = usb_power_delivery_register_capabilities(con->partner_pd, &caps);
845 con->partner_sink_caps = cap;
847 ret = typec_partner_set_usb_power_delivery(con->partner, con->partner_pd);
849 usb_power_delivery_unregister_capabilities(con->partner_sink_caps);
857 static void ucsi_unregister_partner_pdos(struct ucsi_connector *con)
859 usb_power_delivery_unregister_capabilities(con->partner_sink_caps);
860 con->partner_sink_caps = NULL;
861 usb_power_delivery_unregister_capabilities(con->partner_source_caps);
862 con->partner_source_caps = NULL;
863 usb_power_delivery_unregister(con->partner_pd);
864 con->partner_pd = NULL;
867 static int ucsi_register_plug(struct ucsi_connector *con)
869 struct typec_plug *plug;
870 struct typec_plug_desc desc = {.index = TYPEC_PLUG_SOP_P};
872 plug = typec_register_plug(con->cable, &desc);
874 dev_err(con->ucsi->dev,
875 "con%d: failed to register plug (%ld)\n", con->num,
877 return PTR_ERR(plug);
884 static void ucsi_unregister_plug(struct ucsi_connector *con)
889 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP_P);
890 typec_unregister_plug(con->plug);
894 static int ucsi_register_cable(struct ucsi_connector *con)
896 struct typec_cable *cable;
897 struct typec_cable_desc desc = {};
899 switch (UCSI_CABLE_PROP_FLAG_PLUG_TYPE(con->cable_prop.flags)) {
900 case UCSI_CABLE_PROPERTY_PLUG_TYPE_A:
901 desc.type = USB_PLUG_TYPE_A;
903 case UCSI_CABLE_PROPERTY_PLUG_TYPE_B:
904 desc.type = USB_PLUG_TYPE_B;
906 case UCSI_CABLE_PROPERTY_PLUG_TYPE_C:
907 desc.type = USB_PLUG_TYPE_C;
910 desc.type = USB_PLUG_NONE;
914 desc.identity = &con->cable_identity;
915 desc.active = !!(UCSI_CABLE_PROP_FLAG_ACTIVE_CABLE &
916 con->cable_prop.flags);
917 desc.pd_revision = UCSI_CABLE_PROP_FLAG_PD_MAJOR_REV_AS_BCD(
918 con->cable_prop.flags);
920 cable = typec_register_cable(con->port, &desc);
922 dev_err(con->ucsi->dev,
923 "con%d: failed to register cable (%ld)\n", con->num,
925 return PTR_ERR(cable);
932 static void ucsi_unregister_cable(struct ucsi_connector *con)
937 ucsi_unregister_plug(con);
938 typec_unregister_cable(con->cable);
939 memset(&con->cable_identity, 0, sizeof(con->cable_identity));
943 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
945 switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
946 case UCSI_CONSTAT_PWR_OPMODE_PD:
947 con->rdo = con->status.request_data_obj;
948 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
949 ucsi_partner_task(con, ucsi_get_src_pdos, 30, 0);
950 ucsi_partner_task(con, ucsi_check_altmodes, 30, 0);
951 ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ);
953 case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
955 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
957 case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
959 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
963 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
968 static int ucsi_register_partner(struct ucsi_connector *con)
970 u8 pwr_opmode = UCSI_CONSTAT_PWR_OPMODE(con->status.flags);
971 struct typec_partner_desc desc;
972 struct typec_partner *partner;
977 memset(&desc, 0, sizeof(desc));
979 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
980 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
981 desc.accessory = TYPEC_ACCESSORY_DEBUG;
983 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
984 desc.accessory = TYPEC_ACCESSORY_AUDIO;
990 desc.identity = &con->partner_identity;
991 desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD;
992 desc.pd_revision = UCSI_CONCAP_FLAG_PARTNER_PD_MAJOR_REV_AS_BCD(con->cap.flags);
994 partner = typec_register_partner(con->port, &desc);
995 if (IS_ERR(partner)) {
996 dev_err(con->ucsi->dev,
997 "con%d: failed to register partner (%ld)\n", con->num,
999 return PTR_ERR(partner);
1002 con->partner = partner;
1007 static void ucsi_unregister_partner(struct ucsi_connector *con)
1012 typec_set_mode(con->port, TYPEC_STATE_SAFE);
1014 typec_partner_set_usb_power_delivery(con->partner, NULL);
1015 ucsi_unregister_partner_pdos(con);
1016 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
1017 ucsi_unregister_cable(con);
1018 typec_unregister_partner(con->partner);
1019 memset(&con->partner_identity, 0, sizeof(con->partner_identity));
1020 con->partner = NULL;
1023 static void ucsi_partner_change(struct ucsi_connector *con)
1025 enum usb_role u_role = USB_ROLE_NONE;
1028 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1029 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1030 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1031 u_role = USB_ROLE_HOST;
1033 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1034 typec_set_data_role(con->port, TYPEC_HOST);
1036 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1037 u_role = USB_ROLE_DEVICE;
1038 typec_set_data_role(con->port, TYPEC_DEVICE);
1044 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1045 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1046 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
1047 typec_set_mode(con->port, TYPEC_MODE_DEBUG);
1049 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
1050 typec_set_mode(con->port, TYPEC_MODE_AUDIO);
1053 if (UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) ==
1054 UCSI_CONSTAT_PARTNER_FLAG_USB)
1055 typec_set_mode(con->port, TYPEC_STATE_USB);
1059 /* Only notify USB controller if partner supports USB data */
1060 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
1061 u_role = USB_ROLE_NONE;
1063 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1065 dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n",
1069 static int ucsi_check_connector_capability(struct ucsi_connector *con)
1074 if (!con->partner || con->ucsi->version < UCSI_VERSION_2_0)
1077 command = UCSI_GET_CONNECTOR_CAPABILITY | UCSI_CONNECTOR_NUMBER(con->num);
1078 ret = ucsi_send_command(con->ucsi, command, &con->cap, sizeof(con->cap));
1080 dev_err(con->ucsi->dev, "GET_CONNECTOR_CAPABILITY failed (%d)\n", ret);
1084 typec_partner_set_pd_revision(con->partner,
1085 UCSI_CONCAP_FLAG_PARTNER_PD_MAJOR_REV_AS_BCD(con->cap.flags));
1090 static int ucsi_check_connection(struct ucsi_connector *con)
1092 u8 prev_flags = con->status.flags;
1096 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
1097 ret = ucsi_send_command(con->ucsi, command, &con->status, sizeof(con->status));
1099 dev_err(con->ucsi->dev, "GET_CONNECTOR_STATUS failed (%d)\n", ret);
1103 if (con->status.flags == prev_flags)
1106 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1107 ucsi_register_partner(con);
1108 ucsi_pwr_opmode_change(con);
1109 ucsi_partner_change(con);
1111 ucsi_partner_change(con);
1112 ucsi_port_psy_changed(con);
1113 ucsi_unregister_partner(con);
1119 static int ucsi_check_cable(struct ucsi_connector *con)
1127 command = UCSI_GET_CABLE_PROPERTY | UCSI_CONNECTOR_NUMBER(con->num);
1128 ret = ucsi_send_command(con->ucsi, command, &con->cable_prop,
1129 sizeof(con->cable_prop));
1131 dev_err(con->ucsi->dev, "GET_CABLE_PROPERTY failed (%d)\n",
1136 ret = ucsi_register_cable(con);
1140 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE) {
1141 ret = ucsi_get_cable_identity(con);
1146 if (con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS) {
1147 ret = ucsi_register_plug(con);
1151 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP_P);
1159 static void ucsi_handle_connector_change(struct work_struct *work)
1161 struct ucsi_connector *con = container_of(work, struct ucsi_connector,
1163 struct ucsi *ucsi = con->ucsi;
1164 enum typec_role role;
1168 mutex_lock(&con->lock);
1170 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
1171 ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
1173 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
1175 clear_bit(EVENT_PENDING, &con->ucsi->flags);
1179 trace_ucsi_connector_change(con->num, &con->status);
1181 role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
1183 if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
1184 typec_set_pwr_role(con->port, role);
1186 /* Complete pending power role swap */
1187 if (!completion_done(&con->complete))
1188 complete(&con->complete);
1191 if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
1192 typec_set_pwr_role(con->port, role);
1193 ucsi_port_psy_changed(con);
1194 ucsi_partner_change(con);
1196 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1197 ucsi_register_partner(con);
1198 ucsi_partner_task(con, ucsi_check_connection, 1, HZ);
1199 ucsi_partner_task(con, ucsi_check_connector_capability, 1, HZ);
1200 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
1201 ucsi_partner_task(con, ucsi_get_partner_identity, 1, HZ);
1202 if (con->ucsi->cap.features & UCSI_CAP_CABLE_DETAILS)
1203 ucsi_partner_task(con, ucsi_check_cable, 1, HZ);
1205 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) ==
1206 UCSI_CONSTAT_PWR_OPMODE_PD)
1207 ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ);
1209 ucsi_unregister_partner(con);
1213 if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE ||
1214 con->status.change & UCSI_CONSTAT_POWER_LEVEL_CHANGE)
1215 ucsi_pwr_opmode_change(con);
1217 if (con->partner && con->status.change & UCSI_CONSTAT_PARTNER_CHANGE) {
1218 ucsi_partner_change(con);
1220 /* Complete pending data role swap */
1221 if (!completion_done(&con->complete))
1222 complete(&con->complete);
1225 if (con->status.change & UCSI_CONSTAT_CAM_CHANGE)
1226 ucsi_partner_task(con, ucsi_check_altmodes, 1, 0);
1228 mutex_lock(&ucsi->ppm_lock);
1229 clear_bit(EVENT_PENDING, &con->ucsi->flags);
1230 ret = ucsi_acknowledge_connector_change(ucsi);
1231 mutex_unlock(&ucsi->ppm_lock);
1234 dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret);
1237 mutex_unlock(&con->lock);
1241 * ucsi_connector_change - Process Connector Change Event
1242 * @ucsi: UCSI Interface
1243 * @num: Connector number
1245 void ucsi_connector_change(struct ucsi *ucsi, u8 num)
1247 struct ucsi_connector *con = &ucsi->connector[num - 1];
1249 if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
1250 dev_dbg(ucsi->dev, "Early connector change event\n");
1254 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
1255 schedule_work(&con->work);
1257 EXPORT_SYMBOL_GPL(ucsi_connector_change);
1259 /* -------------------------------------------------------------------------- */
1261 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
1265 command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num);
1266 command |= hard ? UCSI_CONNECTOR_RESET_HARD : 0;
1268 return ucsi_send_command(con->ucsi, command, NULL, 0);
1271 static int ucsi_reset_ppm(struct ucsi *ucsi)
1278 mutex_lock(&ucsi->ppm_lock);
1280 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
1285 * If UCSI_CCI_RESET_COMPLETE is already set we must clear
1286 * the flag before we start another reset. Send a
1287 * UCSI_SET_NOTIFICATION_ENABLE command to achieve this.
1288 * Ignore a timeout and try the reset anyway if this fails.
1290 if (cci & UCSI_CCI_RESET_COMPLETE) {
1291 command = UCSI_SET_NOTIFICATION_ENABLE;
1292 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command,
1297 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
1299 ret = ucsi->ops->read(ucsi, UCSI_CCI,
1303 if (cci & UCSI_CCI_COMMAND_COMPLETE)
1305 if (time_is_before_jiffies(tmo))
1310 WARN_ON(cci & UCSI_CCI_RESET_COMPLETE);
1313 command = UCSI_PPM_RESET;
1314 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command,
1319 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
1322 if (time_is_before_jiffies(tmo)) {
1327 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
1331 /* If the PPM is still doing something else, reset it again. */
1332 if (cci & ~UCSI_CCI_RESET_COMPLETE) {
1333 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL,
1341 } while (!(cci & UCSI_CCI_RESET_COMPLETE));
1344 mutex_unlock(&ucsi->ppm_lock);
1348 static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)
1352 ret = ucsi_send_command(con->ucsi, command, NULL, 0);
1353 if (ret == -ETIMEDOUT) {
1356 /* PPM most likely stopped responding. Resetting everything. */
1357 ucsi_reset_ppm(con->ucsi);
1359 c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
1360 ucsi_send_command(con->ucsi, c, NULL, 0);
1362 ucsi_reset_connector(con, true);
1368 static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
1370 struct ucsi_connector *con = typec_get_drvdata(port);
1375 mutex_lock(&con->lock);
1377 if (!con->partner) {
1382 partner_type = UCSI_CONSTAT_PARTNER_TYPE(con->status.flags);
1383 if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
1384 role == TYPEC_DEVICE) ||
1385 (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
1386 role == TYPEC_HOST))
1389 reinit_completion(&con->complete);
1391 command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num);
1392 command |= UCSI_SET_UOR_ROLE(role);
1393 command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS;
1394 ret = ucsi_role_cmd(con, command);
1398 mutex_unlock(&con->lock);
1400 if (!wait_for_completion_timeout(&con->complete,
1401 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1407 mutex_unlock(&con->lock);
1412 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
1414 struct ucsi_connector *con = typec_get_drvdata(port);
1415 enum typec_role cur_role;
1419 mutex_lock(&con->lock);
1421 if (!con->partner) {
1426 cur_role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
1428 if (cur_role == role)
1431 reinit_completion(&con->complete);
1433 command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num);
1434 command |= UCSI_SET_PDR_ROLE(role);
1435 command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS;
1436 ret = ucsi_role_cmd(con, command);
1440 mutex_unlock(&con->lock);
1442 if (!wait_for_completion_timeout(&con->complete,
1443 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1446 mutex_lock(&con->lock);
1448 /* Something has gone wrong while swapping the role */
1449 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) !=
1450 UCSI_CONSTAT_PWR_OPMODE_PD) {
1451 ucsi_reset_connector(con, true);
1456 mutex_unlock(&con->lock);
1461 static const struct typec_operations ucsi_ops = {
1462 .dr_set = ucsi_dr_swap,
1463 .pr_set = ucsi_pr_swap
1466 /* Caller must call fwnode_handle_put() after use */
1467 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
1469 struct fwnode_handle *fwnode;
1472 device_for_each_child_node(con->ucsi->dev, fwnode)
1473 if (i++ == con->num)
1478 static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con)
1480 struct usb_power_delivery_desc desc = { ucsi->cap.pd_version};
1481 struct usb_power_delivery_capabilities_desc pd_caps;
1482 struct usb_power_delivery_capabilities *pd_cap;
1483 struct typec_capability *cap = &con->typec_cap;
1484 enum typec_accessory *accessory = cap->accessory;
1485 enum usb_role u_role = USB_ROLE_NONE;
1490 name = kasprintf(GFP_KERNEL, "%s-con%d", dev_name(ucsi->dev), con->num);
1494 con->wq = create_singlethread_workqueue(name);
1499 INIT_WORK(&con->work, ucsi_handle_connector_change);
1500 init_completion(&con->complete);
1501 mutex_init(&con->lock);
1502 INIT_LIST_HEAD(&con->partner_tasks);
1505 cap->fwnode = ucsi_find_fwnode(con);
1506 con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode);
1507 if (IS_ERR(con->usb_role_sw))
1508 return dev_err_probe(ucsi->dev, PTR_ERR(con->usb_role_sw),
1509 "con%d: failed to get usb role switch\n", con->num);
1511 /* Delay other interactions with the con until registration is complete */
1512 mutex_lock(&con->lock);
1514 /* Get connector capability */
1515 command = UCSI_GET_CONNECTOR_CAPABILITY;
1516 command |= UCSI_CONNECTOR_NUMBER(con->num);
1517 ret = ucsi_send_command(ucsi, command, &con->cap, sizeof(con->cap));
1521 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP)
1522 cap->data = TYPEC_PORT_DRD;
1523 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP)
1524 cap->data = TYPEC_PORT_DFP;
1525 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP)
1526 cap->data = TYPEC_PORT_UFP;
1528 if ((con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER) &&
1529 (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER))
1530 cap->type = TYPEC_PORT_DRP;
1531 else if (con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER)
1532 cap->type = TYPEC_PORT_SRC;
1533 else if (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER)
1534 cap->type = TYPEC_PORT_SNK;
1536 cap->revision = ucsi->cap.typec_version;
1537 cap->pd_revision = ucsi->cap.pd_version;
1538 cap->svdm_version = SVDM_VER_2_0;
1539 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
1541 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY)
1542 *accessory++ = TYPEC_ACCESSORY_AUDIO;
1543 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
1544 *accessory = TYPEC_ACCESSORY_DEBUG;
1546 cap->driver_data = con;
1547 cap->ops = &ucsi_ops;
1549 ret = ucsi_register_port_psy(con);
1553 /* Register the connector */
1554 con->port = typec_register_port(ucsi->dev, cap);
1555 if (IS_ERR(con->port)) {
1556 ret = PTR_ERR(con->port);
1560 con->pd = usb_power_delivery_register(ucsi->dev, &desc);
1562 ret = ucsi_get_pdos(con, TYPEC_SOURCE, 0, pd_caps.pdo);
1564 if (ret < PDO_MAX_OBJECTS)
1565 pd_caps.pdo[ret] = 0;
1567 pd_caps.role = TYPEC_SOURCE;
1568 pd_cap = usb_power_delivery_register_capabilities(con->pd, &pd_caps);
1569 if (IS_ERR(pd_cap)) {
1570 ret = PTR_ERR(pd_cap);
1574 con->port_source_caps = pd_cap;
1575 typec_port_set_usb_power_delivery(con->port, con->pd);
1578 memset(&pd_caps, 0, sizeof(pd_caps));
1579 ret = ucsi_get_pdos(con, TYPEC_SINK, 0, pd_caps.pdo);
1581 if (ret < PDO_MAX_OBJECTS)
1582 pd_caps.pdo[ret] = 0;
1584 pd_caps.role = TYPEC_SINK;
1585 pd_cap = usb_power_delivery_register_capabilities(con->pd, &pd_caps);
1586 if (IS_ERR(pd_cap)) {
1587 ret = PTR_ERR(pd_cap);
1591 con->port_sink_caps = pd_cap;
1592 typec_port_set_usb_power_delivery(con->port, con->pd);
1595 /* Alternate modes */
1596 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON);
1598 dev_err(ucsi->dev, "con%d: failed to register alt modes\n",
1603 /* Get the status */
1604 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
1605 ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
1607 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
1611 ret = 0; /* ucsi_send_command() returns length on success */
1613 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1614 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1615 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1616 u_role = USB_ROLE_HOST;
1618 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1619 typec_set_data_role(con->port, TYPEC_HOST);
1621 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1622 u_role = USB_ROLE_DEVICE;
1623 typec_set_data_role(con->port, TYPEC_DEVICE);
1629 /* Check if there is already something connected */
1630 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1631 typec_set_pwr_role(con->port,
1632 !!(con->status.flags & UCSI_CONSTAT_PWR_DIR));
1633 ucsi_register_partner(con);
1634 ucsi_pwr_opmode_change(con);
1635 ucsi_port_psy_changed(con);
1636 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
1637 ucsi_get_partner_identity(con);
1638 if (con->ucsi->cap.features & UCSI_CAP_CABLE_DETAILS)
1639 ucsi_check_cable(con);
1642 /* Only notify USB controller if partner supports USB data */
1643 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
1644 u_role = USB_ROLE_NONE;
1646 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1648 dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n",
1654 UCSI_CONSTAT_PWR_OPMODE(con->status.flags) ==
1655 UCSI_CONSTAT_PWR_OPMODE_PD) {
1656 ucsi_get_src_pdos(con);
1657 ucsi_check_altmodes(con);
1660 trace_ucsi_register_port(con->num, &con->status);
1663 fwnode_handle_put(cap->fwnode);
1665 mutex_unlock(&con->lock);
1667 if (ret && con->wq) {
1668 destroy_workqueue(con->wq);
1676 * ucsi_init - Initialize UCSI interface
1677 * @ucsi: UCSI to be initialized
1679 * Registers all ports @ucsi has and enables all notification events.
1681 static int ucsi_init(struct ucsi *ucsi)
1683 struct ucsi_connector *con, *connector;
1690 ret = ucsi_reset_ppm(ucsi);
1692 dev_err(ucsi->dev, "failed to reset PPM!\n");
1696 /* Enable basic notifications */
1697 ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
1698 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1699 ret = ucsi_send_command(ucsi, command, NULL, 0);
1703 /* Get PPM capabilities */
1704 command = UCSI_GET_CAPABILITY;
1705 ret = ucsi_send_command(ucsi, command, &ucsi->cap, sizeof(ucsi->cap));
1709 if (!ucsi->cap.num_connectors) {
1714 /* Allocate the connectors. Released in ucsi_unregister() */
1715 connector = kcalloc(ucsi->cap.num_connectors + 1, sizeof(*connector), GFP_KERNEL);
1721 /* Register all connectors */
1722 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1723 connector[i].num = i + 1;
1724 ret = ucsi_register_port(ucsi, &connector[i]);
1726 goto err_unregister;
1729 /* Enable all notifications */
1730 ntfy = UCSI_ENABLE_NTFY_ALL;
1731 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1732 ret = ucsi_send_command(ucsi, command, NULL, 0);
1734 goto err_unregister;
1736 ucsi->connector = connector;
1739 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
1742 if (UCSI_CCI_CONNECTOR(READ_ONCE(cci)))
1743 ucsi_connector_change(ucsi, cci);
1748 for (con = connector; con->port; con++) {
1749 ucsi_unregister_partner(con);
1750 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON);
1751 ucsi_unregister_port_psy(con);
1753 destroy_workqueue(con->wq);
1755 usb_power_delivery_unregister_capabilities(con->port_sink_caps);
1756 con->port_sink_caps = NULL;
1757 usb_power_delivery_unregister_capabilities(con->port_source_caps);
1758 con->port_source_caps = NULL;
1759 usb_power_delivery_unregister(con->pd);
1761 typec_unregister_port(con->port);
1766 memset(&ucsi->cap, 0, sizeof(ucsi->cap));
1767 ucsi_reset_ppm(ucsi);
1772 static void ucsi_resume_work(struct work_struct *work)
1774 struct ucsi *ucsi = container_of(work, struct ucsi, resume_work);
1775 struct ucsi_connector *con;
1779 /* Restore UCSI notification enable mask after system resume */
1780 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1781 ret = ucsi_send_command(ucsi, command, NULL, 0);
1783 dev_err(ucsi->dev, "failed to re-enable notifications (%d)\n", ret);
1787 for (con = ucsi->connector; con->port; con++) {
1788 mutex_lock(&con->lock);
1789 ucsi_partner_task(con, ucsi_check_connection, 1, 0);
1790 mutex_unlock(&con->lock);
1794 int ucsi_resume(struct ucsi *ucsi)
1796 if (ucsi->connector)
1797 queue_work(system_long_wq, &ucsi->resume_work);
1800 EXPORT_SYMBOL_GPL(ucsi_resume);
1802 static void ucsi_init_work(struct work_struct *work)
1804 struct ucsi *ucsi = container_of(work, struct ucsi, work.work);
1807 ret = ucsi_init(ucsi);
1809 dev_err_probe(ucsi->dev, ret, "PPM init failed\n");
1811 if (ret == -EPROBE_DEFER) {
1812 if (ucsi->work_count++ > UCSI_ROLE_SWITCH_WAIT_COUNT) {
1813 dev_err(ucsi->dev, "PPM init failed, stop trying\n");
1817 queue_delayed_work(system_long_wq, &ucsi->work,
1818 UCSI_ROLE_SWITCH_INTERVAL);
1823 * ucsi_get_drvdata - Return private driver data pointer
1824 * @ucsi: UCSI interface
1826 void *ucsi_get_drvdata(struct ucsi *ucsi)
1828 return ucsi->driver_data;
1830 EXPORT_SYMBOL_GPL(ucsi_get_drvdata);
1833 * ucsi_set_drvdata - Assign private driver data pointer
1834 * @ucsi: UCSI interface
1835 * @data: Private data pointer
1837 void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
1839 ucsi->driver_data = data;
1841 EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
1844 * ucsi_create - Allocate UCSI instance
1845 * @dev: Device interface to the PPM (Platform Policy Manager)
1846 * @ops: I/O routines
1848 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
1852 if (!ops || !ops->read || !ops->sync_write || !ops->async_write)
1853 return ERR_PTR(-EINVAL);
1855 ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
1857 return ERR_PTR(-ENOMEM);
1859 INIT_WORK(&ucsi->resume_work, ucsi_resume_work);
1860 INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work);
1861 mutex_init(&ucsi->ppm_lock);
1867 EXPORT_SYMBOL_GPL(ucsi_create);
1870 * ucsi_destroy - Free UCSI instance
1871 * @ucsi: UCSI instance to be freed
1873 void ucsi_destroy(struct ucsi *ucsi)
1875 ucsi_debugfs_unregister(ucsi);
1878 EXPORT_SYMBOL_GPL(ucsi_destroy);
1881 * ucsi_register - Register UCSI interface
1882 * @ucsi: UCSI instance
1884 int ucsi_register(struct ucsi *ucsi)
1888 ret = ucsi->ops->read(ucsi, UCSI_VERSION, &ucsi->version,
1889 sizeof(ucsi->version));
1897 * Version format is JJ.M.N (JJ = Major version, M = Minor version,
1898 * N = sub-minor version).
1900 dev_dbg(ucsi->dev, "Registered UCSI interface with version %x.%x.%x",
1901 UCSI_BCD_GET_MAJOR(ucsi->version),
1902 UCSI_BCD_GET_MINOR(ucsi->version),
1903 UCSI_BCD_GET_SUBMINOR(ucsi->version));
1905 queue_delayed_work(system_long_wq, &ucsi->work, 0);
1907 ucsi_debugfs_register(ucsi);
1910 EXPORT_SYMBOL_GPL(ucsi_register);
1913 * ucsi_unregister - Unregister UCSI interface
1914 * @ucsi: UCSI interface to be unregistered
1916 * Unregister UCSI interface that was created with ucsi_register().
1918 void ucsi_unregister(struct ucsi *ucsi)
1920 u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
1923 /* Make sure that we are not in the middle of driver initialization */
1924 cancel_delayed_work_sync(&ucsi->work);
1925 cancel_work_sync(&ucsi->resume_work);
1927 /* Disable notifications */
1928 ucsi->ops->async_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
1930 if (!ucsi->connector)
1933 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1934 cancel_work_sync(&ucsi->connector[i].work);
1935 ucsi_unregister_partner(&ucsi->connector[i]);
1936 ucsi_unregister_altmodes(&ucsi->connector[i],
1937 UCSI_RECIPIENT_CON);
1938 ucsi_unregister_port_psy(&ucsi->connector[i]);
1940 if (ucsi->connector[i].wq) {
1941 struct ucsi_work *uwork;
1943 mutex_lock(&ucsi->connector[i].lock);
1945 * queue delayed items immediately so they can execute
1946 * and free themselves before the wq is destroyed
1948 list_for_each_entry(uwork, &ucsi->connector[i].partner_tasks, node)
1949 mod_delayed_work(ucsi->connector[i].wq, &uwork->work, 0);
1950 mutex_unlock(&ucsi->connector[i].lock);
1951 destroy_workqueue(ucsi->connector[i].wq);
1954 usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_sink_caps);
1955 ucsi->connector[i].port_sink_caps = NULL;
1956 usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_source_caps);
1957 ucsi->connector[i].port_source_caps = NULL;
1958 usb_power_delivery_unregister(ucsi->connector[i].pd);
1959 ucsi->connector[i].pd = NULL;
1960 typec_unregister_port(ucsi->connector[i].port);
1963 kfree(ucsi->connector);
1965 EXPORT_SYMBOL_GPL(ucsi_unregister);
1967 static int __init ucsi_module_init(void)
1969 ucsi_debugfs_init();
1972 module_init(ucsi_module_init);
1974 static void __exit ucsi_module_exit(void)
1976 ucsi_debugfs_exit();
1978 module_exit(ucsi_module_exit);
1980 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1981 MODULE_LICENSE("GPL v2");
1982 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");