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_acknowledge_command(struct ucsi *ucsi)
43 ctrl = UCSI_ACK_CC_CI;
44 ctrl |= UCSI_ACK_COMMAND_COMPLETE;
46 return ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &ctrl, sizeof(ctrl));
49 static int ucsi_acknowledge_connector_change(struct ucsi *ucsi)
53 ctrl = UCSI_ACK_CC_CI;
54 ctrl |= UCSI_ACK_CONNECTOR_CHANGE;
56 return ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &ctrl, sizeof(ctrl));
59 static int ucsi_exec_command(struct ucsi *ucsi, u64 command);
61 static int ucsi_read_error(struct ucsi *ucsi)
66 /* Acknowledge the command that failed */
67 ret = ucsi_acknowledge_command(ucsi);
71 ret = ucsi_exec_command(ucsi, UCSI_GET_ERROR_STATUS);
75 ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, &error, sizeof(error));
79 ret = ucsi_acknowledge_command(ucsi);
84 case UCSI_ERROR_INCOMPATIBLE_PARTNER:
86 case UCSI_ERROR_CC_COMMUNICATION_ERR:
88 case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
90 case UCSI_ERROR_DEAD_BATTERY:
91 dev_warn(ucsi->dev, "Dead battery condition!\n");
93 case UCSI_ERROR_INVALID_CON_NUM:
94 case UCSI_ERROR_UNREGONIZED_CMD:
95 case UCSI_ERROR_INVALID_CMD_ARGUMENT:
96 dev_err(ucsi->dev, "possible UCSI driver bug %u\n", error);
98 case UCSI_ERROR_OVERCURRENT:
99 dev_warn(ucsi->dev, "Overcurrent condition\n");
101 case UCSI_ERROR_PARTNER_REJECTED_SWAP:
102 dev_warn(ucsi->dev, "Partner rejected swap\n");
104 case UCSI_ERROR_HARD_RESET:
105 dev_warn(ucsi->dev, "Hard reset occurred\n");
107 case UCSI_ERROR_PPM_POLICY_CONFLICT:
108 dev_warn(ucsi->dev, "PPM Policy conflict\n");
110 case UCSI_ERROR_SWAP_REJECTED:
111 dev_warn(ucsi->dev, "Swap rejected\n");
113 case UCSI_ERROR_UNDEFINED:
115 dev_err(ucsi->dev, "unknown error %u\n", error);
122 static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd)
127 ret = ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
131 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
135 if (cmd != UCSI_CANCEL && cci & UCSI_CCI_BUSY)
136 return ucsi_exec_command(ucsi, UCSI_CANCEL);
138 if (!(cci & UCSI_CCI_COMMAND_COMPLETE))
141 if (cci & UCSI_CCI_NOT_SUPPORTED)
144 if (cci & UCSI_CCI_ERROR) {
145 if (cmd == UCSI_GET_ERROR_STATUS)
147 return ucsi_read_error(ucsi);
150 if (cmd == UCSI_CANCEL && cci & UCSI_CCI_CANCEL_COMPLETE) {
151 ret = ucsi_acknowledge_command(ucsi);
152 return ret ? ret : -EBUSY;
155 return UCSI_CCI_LENGTH(cci);
158 int ucsi_send_command(struct ucsi *ucsi, u64 command,
159 void *data, size_t size)
164 mutex_lock(&ucsi->ppm_lock);
166 ret = ucsi_exec_command(ucsi, command);
173 ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, data, size);
178 ret = ucsi_acknowledge_command(ucsi);
184 mutex_unlock(&ucsi->ppm_lock);
187 EXPORT_SYMBOL_GPL(ucsi_send_command);
189 /* -------------------------------------------------------------------------- */
192 struct delayed_work work;
193 struct list_head node;
196 struct ucsi_connector *con;
197 int (*cb)(struct ucsi_connector *);
200 static void ucsi_poll_worker(struct work_struct *work)
202 struct ucsi_work *uwork = container_of(work, struct ucsi_work, work.work);
203 struct ucsi_connector *con = uwork->con;
206 mutex_lock(&con->lock);
209 list_del(&uwork->node);
210 mutex_unlock(&con->lock);
215 ret = uwork->cb(con);
217 if (uwork->count-- && (ret == -EBUSY || ret == -ETIMEDOUT)) {
218 queue_delayed_work(con->wq, &uwork->work, uwork->delay);
220 list_del(&uwork->node);
224 mutex_unlock(&con->lock);
227 static int ucsi_partner_task(struct ucsi_connector *con,
228 int (*cb)(struct ucsi_connector *),
229 int retries, unsigned long delay)
231 struct ucsi_work *uwork;
236 uwork = kzalloc(sizeof(*uwork), GFP_KERNEL);
240 INIT_DELAYED_WORK(&uwork->work, ucsi_poll_worker);
241 uwork->count = retries;
242 uwork->delay = delay;
246 list_add_tail(&uwork->node, &con->partner_tasks);
247 queue_delayed_work(con->wq, &uwork->work, delay);
252 /* -------------------------------------------------------------------------- */
254 void ucsi_altmode_update_active(struct ucsi_connector *con)
256 const struct typec_altmode *altmode = NULL;
262 command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
263 ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur));
265 if (con->ucsi->version > 0x0100) {
266 dev_err(con->ucsi->dev,
267 "GET_CURRENT_CAM command failed\n");
273 if (cur < UCSI_MAX_ALTMODES)
274 altmode = typec_altmode_get_partner(con->port_altmode[cur]);
276 for (i = 0; con->partner_altmode[i]; i++)
277 typec_altmode_update_active(con->partner_altmode[i],
278 con->partner_altmode[i] == altmode);
281 static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
286 for (i = 0; alt[i]; i++) {
287 if (i > MODE_DISCOVERY_MAX)
290 if (alt[i]->svid == svid)
297 static int ucsi_next_altmode(struct typec_altmode **alt)
301 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
308 static int ucsi_get_num_altmode(struct typec_altmode **alt)
312 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
319 static int ucsi_register_altmode(struct ucsi_connector *con,
320 struct typec_altmode_desc *desc,
323 struct typec_altmode *alt;
328 override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
331 case UCSI_RECIPIENT_CON:
332 i = ucsi_next_altmode(con->port_altmode);
338 ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid);
344 switch (desc->svid) {
345 case USB_TYPEC_DP_SID:
346 alt = ucsi_register_displayport(con, override, i, desc);
348 case USB_TYPEC_NVIDIA_VLINK_SID:
349 if (desc->vdo == USB_TYPEC_NVIDIA_VLINK_DBG_VDO)
350 alt = typec_port_register_altmode(con->port,
353 alt = ucsi_register_displayport(con, override,
357 alt = typec_port_register_altmode(con->port, desc);
366 con->port_altmode[i] = alt;
368 case UCSI_RECIPIENT_SOP:
369 i = ucsi_next_altmode(con->partner_altmode);
375 ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid);
381 alt = typec_partner_register_altmode(con->partner, desc);
387 con->partner_altmode[i] = alt;
393 trace_ucsi_register_altmode(recipient, alt);
398 dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n",
399 desc->svid, desc->mode);
405 ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient)
407 int max_altmodes = UCSI_MAX_ALTMODES;
408 struct typec_altmode_desc desc;
409 struct ucsi_altmode alt;
410 struct ucsi_altmode orig[UCSI_MAX_ALTMODES];
411 struct ucsi_altmode updated[UCSI_MAX_ALTMODES];
412 struct ucsi *ucsi = con->ucsi;
413 bool multi_dp = false;
420 if (recipient == UCSI_RECIPIENT_CON)
421 max_altmodes = con->ucsi->cap.num_alt_modes;
423 memset(orig, 0, sizeof(orig));
424 memset(updated, 0, sizeof(updated));
426 /* First get all the alternate modes */
427 for (i = 0; i < max_altmodes; i++) {
428 memset(&alt, 0, sizeof(alt));
429 command = UCSI_GET_ALTERNATE_MODES;
430 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
431 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
432 command |= UCSI_GET_ALTMODE_OFFSET(i);
433 len = ucsi_send_command(con->ucsi, command, &alt, sizeof(alt));
435 * We are collecting all altmodes first and then registering.
436 * Some type-C device will return zero length data beyond last
437 * alternate modes. We should not return if length is zero.
442 /* We got all altmodes, now break out and register them */
443 if (!len || !alt.svid)
446 orig[k].mid = alt.mid;
447 orig[k].svid = alt.svid;
451 * Update the original altmode table as some ppms may report
452 * multiple DP altmodes.
454 if (recipient == UCSI_RECIPIENT_CON)
455 multi_dp = ucsi->ops->update_altmodes(ucsi, orig, updated);
457 /* now register altmodes */
458 for (i = 0; i < max_altmodes; i++) {
459 memset(&desc, 0, sizeof(desc));
460 if (multi_dp && recipient == UCSI_RECIPIENT_CON) {
461 desc.svid = updated[i].svid;
462 desc.vdo = updated[i].mid;
464 desc.svid = orig[i].svid;
465 desc.vdo = orig[i].mid;
467 desc.roles = TYPEC_PORT_DRD;
472 ret = ucsi_register_altmode(con, &desc, recipient);
480 static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
482 int max_altmodes = UCSI_MAX_ALTMODES;
483 struct typec_altmode_desc desc;
484 struct ucsi_altmode alt[2];
492 if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
495 if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
498 if (con->ucsi->ops->update_altmodes)
499 return ucsi_register_altmodes_nvidia(con, recipient);
501 if (recipient == UCSI_RECIPIENT_CON)
502 max_altmodes = con->ucsi->cap.num_alt_modes;
504 for (i = 0; i < max_altmodes;) {
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));
517 * This code is requesting one alt mode at a time, but some PPMs
518 * may still return two. If that happens both alt modes need be
519 * registered and the offset for the next alt mode has to be
522 num = len / sizeof(alt[0]);
525 for (j = 0; j < num; j++) {
529 memset(&desc, 0, sizeof(desc));
530 desc.vdo = alt[j].mid;
531 desc.svid = alt[j].svid;
532 desc.roles = TYPEC_PORT_DRD;
534 ret = ucsi_register_altmode(con, &desc, recipient);
543 static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
545 const struct typec_altmode *pdev;
546 struct typec_altmode **adev;
550 case UCSI_RECIPIENT_CON:
551 adev = con->port_altmode;
553 case UCSI_RECIPIENT_SOP:
554 adev = con->partner_altmode;
561 if (recipient == UCSI_RECIPIENT_SOP &&
562 (adev[i]->svid == USB_TYPEC_DP_SID ||
563 (adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID &&
564 adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) {
565 pdev = typec_altmode_get_partner(adev[i]);
566 ucsi_displayport_remove_partner((void *)pdev);
568 typec_unregister_altmode(adev[i]);
573 static int ucsi_read_pdos(struct ucsi_connector *con,
574 enum typec_role role, int is_partner,
575 u32 *pdos, int offset, int num_pdos)
577 struct ucsi *ucsi = con->ucsi;
581 if (ucsi->quirks & UCSI_NO_PARTNER_PDOS)
584 command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num);
585 command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner);
586 command |= UCSI_GET_PDOS_PDO_OFFSET(offset);
587 command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1);
588 command |= is_source(role) ? UCSI_GET_PDOS_SRC_PDOS : 0;
589 ret = ucsi_send_command(ucsi, command, pdos + offset,
590 num_pdos * sizeof(u32));
591 if (ret < 0 && ret != -ETIMEDOUT)
592 dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret);
597 static int ucsi_get_pdos(struct ucsi_connector *con, enum typec_role role,
598 int is_partner, u32 *pdos)
603 /* UCSI max payload means only getting at most 4 PDOs at a time */
604 ret = ucsi_read_pdos(con, role, is_partner, pdos, 0, UCSI_MAX_PDOS);
608 num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */
609 if (num_pdos < UCSI_MAX_PDOS)
612 /* get the remaining PDOs, if any */
613 ret = ucsi_read_pdos(con, role, is_partner, pdos, UCSI_MAX_PDOS,
614 PDO_MAX_OBJECTS - UCSI_MAX_PDOS);
618 return ret / sizeof(u32) + num_pdos;
621 static int ucsi_get_src_pdos(struct ucsi_connector *con)
625 ret = ucsi_get_pdos(con, TYPEC_SOURCE, 1, con->src_pdos);
631 ucsi_port_psy_changed(con);
636 static int ucsi_check_altmodes(struct ucsi_connector *con)
638 int ret, num_partner_am;
640 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
641 if (ret && ret != -ETIMEDOUT)
642 dev_err(con->ucsi->dev,
643 "con%d: failed to register partner alt modes (%d)\n",
646 /* Ignoring the errors in this case. */
647 if (con->partner_altmode[0]) {
648 num_partner_am = ucsi_get_num_altmode(con->partner_altmode);
649 if (num_partner_am > 0)
650 typec_partner_set_num_altmodes(con->partner, num_partner_am);
651 ucsi_altmode_update_active(con);
658 static int ucsi_register_partner_pdos(struct ucsi_connector *con)
660 struct usb_power_delivery_desc desc = { con->ucsi->cap.pd_version };
661 struct usb_power_delivery_capabilities_desc caps;
662 struct usb_power_delivery_capabilities *cap;
668 con->partner_pd = usb_power_delivery_register(NULL, &desc);
669 if (IS_ERR(con->partner_pd))
670 return PTR_ERR(con->partner_pd);
672 ret = ucsi_get_pdos(con, TYPEC_SOURCE, 1, caps.pdo);
674 if (ret < PDO_MAX_OBJECTS)
677 caps.role = TYPEC_SOURCE;
678 cap = usb_power_delivery_register_capabilities(con->partner_pd, &caps);
682 con->partner_source_caps = cap;
684 ret = typec_partner_set_usb_power_delivery(con->partner, con->partner_pd);
686 usb_power_delivery_unregister_capabilities(con->partner_source_caps);
691 ret = ucsi_get_pdos(con, TYPEC_SINK, 1, caps.pdo);
693 if (ret < PDO_MAX_OBJECTS)
696 caps.role = TYPEC_SINK;
698 cap = usb_power_delivery_register_capabilities(con->partner_pd, &caps);
702 con->partner_sink_caps = cap;
704 ret = typec_partner_set_usb_power_delivery(con->partner, con->partner_pd);
706 usb_power_delivery_unregister_capabilities(con->partner_sink_caps);
714 static void ucsi_unregister_partner_pdos(struct ucsi_connector *con)
716 usb_power_delivery_unregister_capabilities(con->partner_sink_caps);
717 con->partner_sink_caps = NULL;
718 usb_power_delivery_unregister_capabilities(con->partner_source_caps);
719 con->partner_source_caps = NULL;
720 usb_power_delivery_unregister(con->partner_pd);
721 con->partner_pd = NULL;
724 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
726 switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
727 case UCSI_CONSTAT_PWR_OPMODE_PD:
728 con->rdo = con->status.request_data_obj;
729 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
730 ucsi_partner_task(con, ucsi_get_src_pdos, 30, 0);
731 ucsi_partner_task(con, ucsi_check_altmodes, 30, 0);
732 ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ);
734 case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
736 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
738 case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
740 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
744 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
749 static int ucsi_register_partner(struct ucsi_connector *con)
751 u8 pwr_opmode = UCSI_CONSTAT_PWR_OPMODE(con->status.flags);
752 struct typec_partner_desc desc;
753 struct typec_partner *partner;
758 memset(&desc, 0, sizeof(desc));
760 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
761 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
762 desc.accessory = TYPEC_ACCESSORY_DEBUG;
764 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
765 desc.accessory = TYPEC_ACCESSORY_AUDIO;
771 desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD;
773 partner = typec_register_partner(con->port, &desc);
774 if (IS_ERR(partner)) {
775 dev_err(con->ucsi->dev,
776 "con%d: failed to register partner (%ld)\n", con->num,
778 return PTR_ERR(partner);
781 con->partner = partner;
786 static void ucsi_unregister_partner(struct ucsi_connector *con)
791 typec_set_mode(con->port, TYPEC_STATE_SAFE);
793 typec_partner_set_usb_power_delivery(con->partner, NULL);
794 ucsi_unregister_partner_pdos(con);
795 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
796 typec_unregister_partner(con->partner);
800 static void ucsi_partner_change(struct ucsi_connector *con)
802 enum usb_role u_role = USB_ROLE_NONE;
805 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
806 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
807 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
808 u_role = USB_ROLE_HOST;
810 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
811 typec_set_data_role(con->port, TYPEC_HOST);
813 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
814 u_role = USB_ROLE_DEVICE;
815 typec_set_data_role(con->port, TYPEC_DEVICE);
821 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
822 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
823 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
824 typec_set_mode(con->port, TYPEC_MODE_DEBUG);
826 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
827 typec_set_mode(con->port, TYPEC_MODE_AUDIO);
830 if (UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) ==
831 UCSI_CONSTAT_PARTNER_FLAG_USB)
832 typec_set_mode(con->port, TYPEC_STATE_USB);
836 /* Only notify USB controller if partner supports USB data */
837 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
838 u_role = USB_ROLE_NONE;
840 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
842 dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n",
846 static int ucsi_check_connection(struct ucsi_connector *con)
848 u8 prev_flags = con->status.flags;
852 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
853 ret = ucsi_send_command(con->ucsi, command, &con->status, sizeof(con->status));
855 dev_err(con->ucsi->dev, "GET_CONNECTOR_STATUS failed (%d)\n", ret);
859 if (con->status.flags == prev_flags)
862 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
863 ucsi_register_partner(con);
864 ucsi_pwr_opmode_change(con);
865 ucsi_partner_change(con);
867 ucsi_partner_change(con);
868 ucsi_port_psy_changed(con);
869 ucsi_unregister_partner(con);
875 static void ucsi_handle_connector_change(struct work_struct *work)
877 struct ucsi_connector *con = container_of(work, struct ucsi_connector,
879 struct ucsi *ucsi = con->ucsi;
880 enum typec_role role;
884 mutex_lock(&con->lock);
886 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
887 ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
889 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
891 clear_bit(EVENT_PENDING, &con->ucsi->flags);
895 trace_ucsi_connector_change(con->num, &con->status);
897 role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
899 if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
900 typec_set_pwr_role(con->port, role);
902 /* Complete pending power role swap */
903 if (!completion_done(&con->complete))
904 complete(&con->complete);
907 if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
908 typec_set_pwr_role(con->port, role);
909 ucsi_port_psy_changed(con);
910 ucsi_partner_change(con);
912 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
913 ucsi_register_partner(con);
914 ucsi_partner_task(con, ucsi_check_connection, 1, HZ);
916 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) ==
917 UCSI_CONSTAT_PWR_OPMODE_PD)
918 ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ);
920 ucsi_unregister_partner(con);
924 if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE ||
925 con->status.change & UCSI_CONSTAT_POWER_LEVEL_CHANGE)
926 ucsi_pwr_opmode_change(con);
928 if (con->partner && con->status.change & UCSI_CONSTAT_PARTNER_CHANGE) {
929 ucsi_partner_change(con);
931 /* Complete pending data role swap */
932 if (!completion_done(&con->complete))
933 complete(&con->complete);
936 if (con->status.change & UCSI_CONSTAT_CAM_CHANGE)
937 ucsi_partner_task(con, ucsi_check_altmodes, 1, 0);
939 clear_bit(EVENT_PENDING, &con->ucsi->flags);
941 mutex_lock(&ucsi->ppm_lock);
942 ret = ucsi_acknowledge_connector_change(ucsi);
943 mutex_unlock(&ucsi->ppm_lock);
945 dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret);
948 mutex_unlock(&con->lock);
952 * ucsi_connector_change - Process Connector Change Event
953 * @ucsi: UCSI Interface
954 * @num: Connector number
956 void ucsi_connector_change(struct ucsi *ucsi, u8 num)
958 struct ucsi_connector *con = &ucsi->connector[num - 1];
960 if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
961 dev_dbg(ucsi->dev, "Bogus connector change event\n");
965 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
966 schedule_work(&con->work);
968 EXPORT_SYMBOL_GPL(ucsi_connector_change);
970 /* -------------------------------------------------------------------------- */
972 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
976 command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num);
977 command |= hard ? UCSI_CONNECTOR_RESET_HARD : 0;
979 return ucsi_send_command(con->ucsi, command, NULL, 0);
982 static int ucsi_reset_ppm(struct ucsi *ucsi)
984 u64 command = UCSI_PPM_RESET;
989 mutex_lock(&ucsi->ppm_lock);
991 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command,
996 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
999 if (time_is_before_jiffies(tmo)) {
1004 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
1008 /* If the PPM is still doing something else, reset it again. */
1009 if (cci & ~UCSI_CCI_RESET_COMPLETE) {
1010 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL,
1018 } while (!(cci & UCSI_CCI_RESET_COMPLETE));
1021 mutex_unlock(&ucsi->ppm_lock);
1025 static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)
1029 ret = ucsi_send_command(con->ucsi, command, NULL, 0);
1030 if (ret == -ETIMEDOUT) {
1033 /* PPM most likely stopped responding. Resetting everything. */
1034 ucsi_reset_ppm(con->ucsi);
1036 c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
1037 ucsi_send_command(con->ucsi, c, NULL, 0);
1039 ucsi_reset_connector(con, true);
1045 static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
1047 struct ucsi_connector *con = typec_get_drvdata(port);
1052 mutex_lock(&con->lock);
1054 if (!con->partner) {
1059 partner_type = UCSI_CONSTAT_PARTNER_TYPE(con->status.flags);
1060 if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
1061 role == TYPEC_DEVICE) ||
1062 (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
1063 role == TYPEC_HOST))
1066 reinit_completion(&con->complete);
1068 command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num);
1069 command |= UCSI_SET_UOR_ROLE(role);
1070 command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS;
1071 ret = ucsi_role_cmd(con, command);
1075 mutex_unlock(&con->lock);
1077 if (!wait_for_completion_timeout(&con->complete,
1078 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1084 mutex_unlock(&con->lock);
1089 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
1091 struct ucsi_connector *con = typec_get_drvdata(port);
1092 enum typec_role cur_role;
1096 mutex_lock(&con->lock);
1098 if (!con->partner) {
1103 cur_role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
1105 if (cur_role == role)
1108 reinit_completion(&con->complete);
1110 command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num);
1111 command |= UCSI_SET_PDR_ROLE(role);
1112 command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS;
1113 ret = ucsi_role_cmd(con, command);
1117 mutex_unlock(&con->lock);
1119 if (!wait_for_completion_timeout(&con->complete,
1120 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1123 mutex_lock(&con->lock);
1125 /* Something has gone wrong while swapping the role */
1126 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) !=
1127 UCSI_CONSTAT_PWR_OPMODE_PD) {
1128 ucsi_reset_connector(con, true);
1133 mutex_unlock(&con->lock);
1138 static const struct typec_operations ucsi_ops = {
1139 .dr_set = ucsi_dr_swap,
1140 .pr_set = ucsi_pr_swap
1143 /* Caller must call fwnode_handle_put() after use */
1144 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
1146 struct fwnode_handle *fwnode;
1149 device_for_each_child_node(con->ucsi->dev, fwnode)
1150 if (i++ == con->num)
1155 static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con)
1157 struct usb_power_delivery_desc desc = { ucsi->cap.pd_version};
1158 struct usb_power_delivery_capabilities_desc pd_caps;
1159 struct usb_power_delivery_capabilities *pd_cap;
1160 struct typec_capability *cap = &con->typec_cap;
1161 enum typec_accessory *accessory = cap->accessory;
1162 enum usb_role u_role = USB_ROLE_NONE;
1167 name = kasprintf(GFP_KERNEL, "%s-con%d", dev_name(ucsi->dev), con->num);
1171 con->wq = create_singlethread_workqueue(name);
1176 INIT_WORK(&con->work, ucsi_handle_connector_change);
1177 init_completion(&con->complete);
1178 mutex_init(&con->lock);
1179 INIT_LIST_HEAD(&con->partner_tasks);
1182 cap->fwnode = ucsi_find_fwnode(con);
1183 con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode);
1184 if (IS_ERR(con->usb_role_sw))
1185 return dev_err_probe(ucsi->dev, PTR_ERR(con->usb_role_sw),
1186 "con%d: failed to get usb role switch\n", con->num);
1188 /* Delay other interactions with the con until registration is complete */
1189 mutex_lock(&con->lock);
1191 /* Get connector capability */
1192 command = UCSI_GET_CONNECTOR_CAPABILITY;
1193 command |= UCSI_CONNECTOR_NUMBER(con->num);
1194 ret = ucsi_send_command(ucsi, command, &con->cap, sizeof(con->cap));
1198 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP)
1199 cap->data = TYPEC_PORT_DRD;
1200 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP)
1201 cap->data = TYPEC_PORT_DFP;
1202 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP)
1203 cap->data = TYPEC_PORT_UFP;
1205 if ((con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER) &&
1206 (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER))
1207 cap->type = TYPEC_PORT_DRP;
1208 else if (con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER)
1209 cap->type = TYPEC_PORT_SRC;
1210 else if (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER)
1211 cap->type = TYPEC_PORT_SNK;
1213 cap->revision = ucsi->cap.typec_version;
1214 cap->pd_revision = ucsi->cap.pd_version;
1215 cap->svdm_version = SVDM_VER_2_0;
1216 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
1218 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY)
1219 *accessory++ = TYPEC_ACCESSORY_AUDIO;
1220 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
1221 *accessory = TYPEC_ACCESSORY_DEBUG;
1223 cap->driver_data = con;
1224 cap->ops = &ucsi_ops;
1226 ret = ucsi_register_port_psy(con);
1230 /* Register the connector */
1231 con->port = typec_register_port(ucsi->dev, cap);
1232 if (IS_ERR(con->port)) {
1233 ret = PTR_ERR(con->port);
1237 con->pd = usb_power_delivery_register(ucsi->dev, &desc);
1239 ret = ucsi_get_pdos(con, TYPEC_SOURCE, 0, pd_caps.pdo);
1241 if (ret < PDO_MAX_OBJECTS)
1242 pd_caps.pdo[ret] = 0;
1244 pd_caps.role = TYPEC_SOURCE;
1245 pd_cap = usb_power_delivery_register_capabilities(con->pd, &pd_caps);
1246 if (IS_ERR(pd_cap)) {
1247 ret = PTR_ERR(pd_cap);
1251 con->port_source_caps = pd_cap;
1252 typec_port_set_usb_power_delivery(con->port, con->pd);
1255 memset(&pd_caps, 0, sizeof(pd_caps));
1256 ret = ucsi_get_pdos(con, TYPEC_SINK, 0, pd_caps.pdo);
1258 if (ret < PDO_MAX_OBJECTS)
1259 pd_caps.pdo[ret] = 0;
1261 pd_caps.role = TYPEC_SINK;
1262 pd_cap = usb_power_delivery_register_capabilities(con->pd, &pd_caps);
1263 if (IS_ERR(pd_cap)) {
1264 ret = PTR_ERR(pd_cap);
1268 con->port_sink_caps = pd_cap;
1269 typec_port_set_usb_power_delivery(con->port, con->pd);
1272 /* Alternate modes */
1273 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON);
1275 dev_err(ucsi->dev, "con%d: failed to register alt modes\n",
1280 /* Get the status */
1281 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
1282 ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
1284 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
1288 ret = 0; /* ucsi_send_command() returns length on success */
1290 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1291 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1292 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1293 u_role = USB_ROLE_HOST;
1295 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1296 typec_set_data_role(con->port, TYPEC_HOST);
1298 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1299 u_role = USB_ROLE_DEVICE;
1300 typec_set_data_role(con->port, TYPEC_DEVICE);
1306 /* Check if there is already something connected */
1307 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1308 typec_set_pwr_role(con->port,
1309 !!(con->status.flags & UCSI_CONSTAT_PWR_DIR));
1310 ucsi_register_partner(con);
1311 ucsi_pwr_opmode_change(con);
1312 ucsi_port_psy_changed(con);
1315 /* Only notify USB controller if partner supports USB data */
1316 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
1317 u_role = USB_ROLE_NONE;
1319 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1321 dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n",
1327 UCSI_CONSTAT_PWR_OPMODE(con->status.flags) ==
1328 UCSI_CONSTAT_PWR_OPMODE_PD) {
1329 ucsi_get_src_pdos(con);
1330 ucsi_check_altmodes(con);
1333 trace_ucsi_register_port(con->num, &con->status);
1336 fwnode_handle_put(cap->fwnode);
1338 mutex_unlock(&con->lock);
1340 if (ret && con->wq) {
1341 destroy_workqueue(con->wq);
1349 * ucsi_init - Initialize UCSI interface
1350 * @ucsi: UCSI to be initialized
1352 * Registers all ports @ucsi has and enables all notification events.
1354 static int ucsi_init(struct ucsi *ucsi)
1356 struct ucsi_connector *con, *connector;
1362 ret = ucsi_reset_ppm(ucsi);
1364 dev_err(ucsi->dev, "failed to reset PPM!\n");
1368 /* Enable basic notifications */
1369 ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
1370 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1371 ret = ucsi_send_command(ucsi, command, NULL, 0);
1375 /* Get PPM capabilities */
1376 command = UCSI_GET_CAPABILITY;
1377 ret = ucsi_send_command(ucsi, command, &ucsi->cap, sizeof(ucsi->cap));
1381 if (!ucsi->cap.num_connectors) {
1386 /* Allocate the connectors. Released in ucsi_unregister() */
1387 connector = kcalloc(ucsi->cap.num_connectors + 1, sizeof(*connector), GFP_KERNEL);
1393 /* Register all connectors */
1394 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1395 connector[i].num = i + 1;
1396 ret = ucsi_register_port(ucsi, &connector[i]);
1398 goto err_unregister;
1401 /* Enable all notifications */
1402 ntfy = UCSI_ENABLE_NTFY_ALL;
1403 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1404 ret = ucsi_send_command(ucsi, command, NULL, 0);
1406 goto err_unregister;
1408 ucsi->connector = connector;
1413 for (con = connector; con->port; con++) {
1414 ucsi_unregister_partner(con);
1415 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON);
1416 ucsi_unregister_port_psy(con);
1418 destroy_workqueue(con->wq);
1420 usb_power_delivery_unregister_capabilities(con->port_sink_caps);
1421 con->port_sink_caps = NULL;
1422 usb_power_delivery_unregister_capabilities(con->port_source_caps);
1423 con->port_source_caps = NULL;
1424 usb_power_delivery_unregister(con->pd);
1426 typec_unregister_port(con->port);
1431 memset(&ucsi->cap, 0, sizeof(ucsi->cap));
1432 ucsi_reset_ppm(ucsi);
1437 static void ucsi_resume_work(struct work_struct *work)
1439 struct ucsi *ucsi = container_of(work, struct ucsi, resume_work);
1440 struct ucsi_connector *con;
1444 /* Restore UCSI notification enable mask after system resume */
1445 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1446 ret = ucsi_send_command(ucsi, command, NULL, 0);
1448 dev_err(ucsi->dev, "failed to re-enable notifications (%d)\n", ret);
1452 for (con = ucsi->connector; con->port; con++) {
1453 mutex_lock(&con->lock);
1454 ucsi_partner_task(con, ucsi_check_connection, 1, 0);
1455 mutex_unlock(&con->lock);
1459 int ucsi_resume(struct ucsi *ucsi)
1461 if (ucsi->connector)
1462 queue_work(system_long_wq, &ucsi->resume_work);
1465 EXPORT_SYMBOL_GPL(ucsi_resume);
1467 static void ucsi_init_work(struct work_struct *work)
1469 struct ucsi *ucsi = container_of(work, struct ucsi, work.work);
1472 ret = ucsi_init(ucsi);
1474 dev_err_probe(ucsi->dev, ret, "PPM init failed\n");
1476 if (ret == -EPROBE_DEFER) {
1477 if (ucsi->work_count++ > UCSI_ROLE_SWITCH_WAIT_COUNT) {
1478 dev_err(ucsi->dev, "PPM init failed, stop trying\n");
1482 queue_delayed_work(system_long_wq, &ucsi->work,
1483 UCSI_ROLE_SWITCH_INTERVAL);
1488 * ucsi_get_drvdata - Return private driver data pointer
1489 * @ucsi: UCSI interface
1491 void *ucsi_get_drvdata(struct ucsi *ucsi)
1493 return ucsi->driver_data;
1495 EXPORT_SYMBOL_GPL(ucsi_get_drvdata);
1498 * ucsi_set_drvdata - Assign private driver data pointer
1499 * @ucsi: UCSI interface
1500 * @data: Private data pointer
1502 void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
1504 ucsi->driver_data = data;
1506 EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
1509 * ucsi_create - Allocate UCSI instance
1510 * @dev: Device interface to the PPM (Platform Policy Manager)
1511 * @ops: I/O routines
1513 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
1517 if (!ops || !ops->read || !ops->sync_write || !ops->async_write)
1518 return ERR_PTR(-EINVAL);
1520 ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
1522 return ERR_PTR(-ENOMEM);
1524 INIT_WORK(&ucsi->resume_work, ucsi_resume_work);
1525 INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work);
1526 mutex_init(&ucsi->ppm_lock);
1532 EXPORT_SYMBOL_GPL(ucsi_create);
1535 * ucsi_destroy - Free UCSI instance
1536 * @ucsi: UCSI instance to be freed
1538 void ucsi_destroy(struct ucsi *ucsi)
1540 ucsi_debugfs_unregister(ucsi);
1543 EXPORT_SYMBOL_GPL(ucsi_destroy);
1546 * ucsi_register - Register UCSI interface
1547 * @ucsi: UCSI instance
1549 int ucsi_register(struct ucsi *ucsi)
1553 ret = ucsi->ops->read(ucsi, UCSI_VERSION, &ucsi->version,
1554 sizeof(ucsi->version));
1561 queue_delayed_work(system_long_wq, &ucsi->work, 0);
1563 ucsi_debugfs_register(ucsi);
1566 EXPORT_SYMBOL_GPL(ucsi_register);
1569 * ucsi_unregister - Unregister UCSI interface
1570 * @ucsi: UCSI interface to be unregistered
1572 * Unregister UCSI interface that was created with ucsi_register().
1574 void ucsi_unregister(struct ucsi *ucsi)
1576 u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
1579 /* Make sure that we are not in the middle of driver initialization */
1580 cancel_delayed_work_sync(&ucsi->work);
1581 cancel_work_sync(&ucsi->resume_work);
1583 /* Disable notifications */
1584 ucsi->ops->async_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
1586 if (!ucsi->connector)
1589 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1590 cancel_work_sync(&ucsi->connector[i].work);
1591 ucsi_unregister_partner(&ucsi->connector[i]);
1592 ucsi_unregister_altmodes(&ucsi->connector[i],
1593 UCSI_RECIPIENT_CON);
1594 ucsi_unregister_port_psy(&ucsi->connector[i]);
1596 if (ucsi->connector[i].wq) {
1597 struct ucsi_work *uwork;
1599 mutex_lock(&ucsi->connector[i].lock);
1601 * queue delayed items immediately so they can execute
1602 * and free themselves before the wq is destroyed
1604 list_for_each_entry(uwork, &ucsi->connector[i].partner_tasks, node)
1605 mod_delayed_work(ucsi->connector[i].wq, &uwork->work, 0);
1606 mutex_unlock(&ucsi->connector[i].lock);
1607 destroy_workqueue(ucsi->connector[i].wq);
1610 usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_sink_caps);
1611 ucsi->connector[i].port_sink_caps = NULL;
1612 usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_source_caps);
1613 ucsi->connector[i].port_source_caps = NULL;
1614 usb_power_delivery_unregister(ucsi->connector[i].pd);
1615 ucsi->connector[i].pd = NULL;
1616 typec_unregister_port(ucsi->connector[i].port);
1619 kfree(ucsi->connector);
1621 EXPORT_SYMBOL_GPL(ucsi_unregister);
1623 static int __init ucsi_module_init(void)
1625 ucsi_debugfs_init();
1628 module_init(ucsi_module_init);
1630 static void __exit ucsi_module_exit(void)
1632 ucsi_debugfs_exit();
1634 module_exit(ucsi_module_exit);
1636 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1637 MODULE_LICENSE("GPL v2");
1638 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");