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));
80 case UCSI_ERROR_INCOMPATIBLE_PARTNER:
82 case UCSI_ERROR_CC_COMMUNICATION_ERR:
84 case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
86 case UCSI_ERROR_DEAD_BATTERY:
87 dev_warn(ucsi->dev, "Dead battery condition!\n");
89 case UCSI_ERROR_INVALID_CON_NUM:
90 case UCSI_ERROR_UNREGONIZED_CMD:
91 case UCSI_ERROR_INVALID_CMD_ARGUMENT:
92 dev_err(ucsi->dev, "possible UCSI driver bug %u\n", error);
94 case UCSI_ERROR_OVERCURRENT:
95 dev_warn(ucsi->dev, "Overcurrent condition\n");
97 case UCSI_ERROR_PARTNER_REJECTED_SWAP:
98 dev_warn(ucsi->dev, "Partner rejected swap\n");
100 case UCSI_ERROR_HARD_RESET:
101 dev_warn(ucsi->dev, "Hard reset occurred\n");
103 case UCSI_ERROR_PPM_POLICY_CONFLICT:
104 dev_warn(ucsi->dev, "PPM Policy conflict\n");
106 case UCSI_ERROR_SWAP_REJECTED:
107 dev_warn(ucsi->dev, "Swap rejected\n");
109 case UCSI_ERROR_UNDEFINED:
111 dev_err(ucsi->dev, "unknown error %u\n", error);
118 static int ucsi_exec_command(struct ucsi *ucsi, u64 cmd)
123 ret = ucsi->ops->sync_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
127 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
131 if (cci & UCSI_CCI_BUSY) {
132 ucsi->ops->async_write(ucsi, UCSI_CANCEL, NULL, 0);
136 if (!(cci & UCSI_CCI_COMMAND_COMPLETE))
139 if (cci & UCSI_CCI_NOT_SUPPORTED)
142 if (cci & UCSI_CCI_ERROR) {
143 if (cmd == UCSI_GET_ERROR_STATUS)
145 return ucsi_read_error(ucsi);
148 return UCSI_CCI_LENGTH(cci);
151 int ucsi_send_command(struct ucsi *ucsi, u64 command,
152 void *data, size_t size)
157 mutex_lock(&ucsi->ppm_lock);
159 ret = ucsi_exec_command(ucsi, command);
166 ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, data, size);
171 ret = ucsi_acknowledge_command(ucsi);
177 mutex_unlock(&ucsi->ppm_lock);
180 EXPORT_SYMBOL_GPL(ucsi_send_command);
182 int ucsi_resume(struct ucsi *ucsi)
186 /* Restore UCSI notification enable mask after system resume */
187 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
189 return ucsi_send_command(ucsi, command, NULL, 0);
191 EXPORT_SYMBOL_GPL(ucsi_resume);
192 /* -------------------------------------------------------------------------- */
195 struct delayed_work work;
198 struct ucsi_connector *con;
199 int (*cb)(struct ucsi_connector *);
202 static void ucsi_poll_worker(struct work_struct *work)
204 struct ucsi_work *uwork = container_of(work, struct ucsi_work, work.work);
205 struct ucsi_connector *con = uwork->con;
208 mutex_lock(&con->lock);
211 mutex_unlock(&con->lock);
216 ret = uwork->cb(con);
218 if (uwork->count-- && (ret == -EBUSY || ret == -ETIMEDOUT))
219 queue_delayed_work(con->wq, &uwork->work, uwork->delay);
223 mutex_unlock(&con->lock);
226 static int ucsi_partner_task(struct ucsi_connector *con,
227 int (*cb)(struct ucsi_connector *),
228 int retries, unsigned long delay)
230 struct ucsi_work *uwork;
235 uwork = kzalloc(sizeof(*uwork), GFP_KERNEL);
239 INIT_DELAYED_WORK(&uwork->work, ucsi_poll_worker);
240 uwork->count = retries;
241 uwork->delay = delay;
245 queue_delayed_work(con->wq, &uwork->work, delay);
250 /* -------------------------------------------------------------------------- */
252 void ucsi_altmode_update_active(struct ucsi_connector *con)
254 const struct typec_altmode *altmode = NULL;
260 command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
261 ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur));
263 if (con->ucsi->version > 0x0100) {
264 dev_err(con->ucsi->dev,
265 "GET_CURRENT_CAM command failed\n");
271 if (cur < UCSI_MAX_ALTMODES)
272 altmode = typec_altmode_get_partner(con->port_altmode[cur]);
274 for (i = 0; con->partner_altmode[i]; i++)
275 typec_altmode_update_active(con->partner_altmode[i],
276 con->partner_altmode[i] == altmode);
279 static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
284 for (i = 0; alt[i]; i++) {
285 if (i > MODE_DISCOVERY_MAX)
288 if (alt[i]->svid == svid)
295 static int ucsi_next_altmode(struct typec_altmode **alt)
299 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
306 static int ucsi_register_altmode(struct ucsi_connector *con,
307 struct typec_altmode_desc *desc,
310 struct typec_altmode *alt;
315 override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
318 case UCSI_RECIPIENT_CON:
319 i = ucsi_next_altmode(con->port_altmode);
325 ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid);
331 switch (desc->svid) {
332 case USB_TYPEC_DP_SID:
333 alt = ucsi_register_displayport(con, override, i, desc);
335 case USB_TYPEC_NVIDIA_VLINK_SID:
336 if (desc->vdo == USB_TYPEC_NVIDIA_VLINK_DBG_VDO)
337 alt = typec_port_register_altmode(con->port,
340 alt = ucsi_register_displayport(con, override,
344 alt = typec_port_register_altmode(con->port, desc);
353 con->port_altmode[i] = alt;
355 case UCSI_RECIPIENT_SOP:
356 i = ucsi_next_altmode(con->partner_altmode);
362 ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid);
368 alt = typec_partner_register_altmode(con->partner, desc);
374 con->partner_altmode[i] = alt;
380 trace_ucsi_register_altmode(recipient, alt);
385 dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n",
386 desc->svid, desc->mode);
392 ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient)
394 int max_altmodes = UCSI_MAX_ALTMODES;
395 struct typec_altmode_desc desc;
396 struct ucsi_altmode alt;
397 struct ucsi_altmode orig[UCSI_MAX_ALTMODES];
398 struct ucsi_altmode updated[UCSI_MAX_ALTMODES];
399 struct ucsi *ucsi = con->ucsi;
400 bool multi_dp = false;
407 if (recipient == UCSI_RECIPIENT_CON)
408 max_altmodes = con->ucsi->cap.num_alt_modes;
410 memset(orig, 0, sizeof(orig));
411 memset(updated, 0, sizeof(updated));
413 /* First get all the alternate modes */
414 for (i = 0; i < max_altmodes; i++) {
415 memset(&alt, 0, sizeof(alt));
416 command = UCSI_GET_ALTERNATE_MODES;
417 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
418 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
419 command |= UCSI_GET_ALTMODE_OFFSET(i);
420 len = ucsi_send_command(con->ucsi, command, &alt, sizeof(alt));
422 * We are collecting all altmodes first and then registering.
423 * Some type-C device will return zero length data beyond last
424 * alternate modes. We should not return if length is zero.
429 /* We got all altmodes, now break out and register them */
430 if (!len || !alt.svid)
433 orig[k].mid = alt.mid;
434 orig[k].svid = alt.svid;
438 * Update the original altmode table as some ppms may report
439 * multiple DP altmodes.
441 if (recipient == UCSI_RECIPIENT_CON)
442 multi_dp = ucsi->ops->update_altmodes(ucsi, orig, updated);
444 /* now register altmodes */
445 for (i = 0; i < max_altmodes; i++) {
446 memset(&desc, 0, sizeof(desc));
447 if (multi_dp && recipient == UCSI_RECIPIENT_CON) {
448 desc.svid = updated[i].svid;
449 desc.vdo = updated[i].mid;
451 desc.svid = orig[i].svid;
452 desc.vdo = orig[i].mid;
454 desc.roles = TYPEC_PORT_DRD;
459 ret = ucsi_register_altmode(con, &desc, recipient);
467 static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
469 int max_altmodes = UCSI_MAX_ALTMODES;
470 struct typec_altmode_desc desc;
471 struct ucsi_altmode alt[2];
479 if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
482 if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
485 if (con->ucsi->ops->update_altmodes)
486 return ucsi_register_altmodes_nvidia(con, recipient);
488 if (recipient == UCSI_RECIPIENT_CON)
489 max_altmodes = con->ucsi->cap.num_alt_modes;
491 for (i = 0; i < max_altmodes;) {
492 memset(alt, 0, sizeof(alt));
493 command = UCSI_GET_ALTERNATE_MODES;
494 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
495 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
496 command |= UCSI_GET_ALTMODE_OFFSET(i);
497 len = ucsi_send_command(con->ucsi, command, alt, sizeof(alt));
504 * This code is requesting one alt mode at a time, but some PPMs
505 * may still return two. If that happens both alt modes need be
506 * registered and the offset for the next alt mode has to be
509 num = len / sizeof(alt[0]);
512 for (j = 0; j < num; j++) {
516 memset(&desc, 0, sizeof(desc));
517 desc.vdo = alt[j].mid;
518 desc.svid = alt[j].svid;
519 desc.roles = TYPEC_PORT_DRD;
521 ret = ucsi_register_altmode(con, &desc, recipient);
530 static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
532 const struct typec_altmode *pdev;
533 struct typec_altmode **adev;
537 case UCSI_RECIPIENT_CON:
538 adev = con->port_altmode;
540 case UCSI_RECIPIENT_SOP:
541 adev = con->partner_altmode;
548 if (recipient == UCSI_RECIPIENT_SOP &&
549 (adev[i]->svid == USB_TYPEC_DP_SID ||
550 (adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID &&
551 adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) {
552 pdev = typec_altmode_get_partner(adev[i]);
553 ucsi_displayport_remove_partner((void *)pdev);
555 typec_unregister_altmode(adev[i]);
560 static int ucsi_get_pdos(struct ucsi_connector *con, int is_partner,
561 u32 *pdos, int offset, int num_pdos)
563 struct ucsi *ucsi = con->ucsi;
567 command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num);
568 command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner);
569 command |= UCSI_GET_PDOS_PDO_OFFSET(offset);
570 command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1);
571 command |= UCSI_GET_PDOS_SRC_PDOS;
572 ret = ucsi_send_command(ucsi, command, pdos + offset,
573 num_pdos * sizeof(u32));
575 dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret);
576 if (ret == 0 && offset == 0)
577 dev_warn(ucsi->dev, "UCSI_GET_PDOS returned 0 bytes\n");
582 static void ucsi_get_src_pdos(struct ucsi_connector *con, int is_partner)
586 /* UCSI max payload means only getting at most 4 PDOs at a time */
587 ret = ucsi_get_pdos(con, 1, con->src_pdos, 0, UCSI_MAX_PDOS);
591 con->num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */
592 if (con->num_pdos < UCSI_MAX_PDOS)
595 /* get the remaining PDOs, if any */
596 ret = ucsi_get_pdos(con, 1, con->src_pdos, UCSI_MAX_PDOS,
597 PDO_MAX_OBJECTS - UCSI_MAX_PDOS);
601 con->num_pdos += ret / sizeof(u32);
604 static int ucsi_check_altmodes(struct ucsi_connector *con)
608 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
609 if (ret && ret != -ETIMEDOUT)
610 dev_err(con->ucsi->dev,
611 "con%d: failed to register partner alt modes (%d)\n",
614 /* Ignoring the errors in this case. */
615 if (con->partner_altmode[0]) {
616 ucsi_altmode_update_active(con);
623 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
625 switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
626 case UCSI_CONSTAT_PWR_OPMODE_PD:
627 con->rdo = con->status.request_data_obj;
628 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
629 ucsi_get_src_pdos(con, 1);
631 case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
633 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
635 case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
637 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
641 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
646 static int ucsi_register_partner(struct ucsi_connector *con)
648 u8 pwr_opmode = UCSI_CONSTAT_PWR_OPMODE(con->status.flags);
649 struct typec_partner_desc desc;
650 struct typec_partner *partner;
655 memset(&desc, 0, sizeof(desc));
657 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
658 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
659 desc.accessory = TYPEC_ACCESSORY_DEBUG;
661 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
662 desc.accessory = TYPEC_ACCESSORY_AUDIO;
668 desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD;
670 partner = typec_register_partner(con->port, &desc);
671 if (IS_ERR(partner)) {
672 dev_err(con->ucsi->dev,
673 "con%d: failed to register partner (%ld)\n", con->num,
675 return PTR_ERR(partner);
678 con->partner = partner;
683 static void ucsi_unregister_partner(struct ucsi_connector *con)
688 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
689 typec_unregister_partner(con->partner);
693 static void ucsi_partner_change(struct ucsi_connector *con)
695 enum usb_role u_role = USB_ROLE_NONE;
701 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
702 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
703 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
704 u_role = USB_ROLE_HOST;
706 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
707 typec_set_data_role(con->port, TYPEC_HOST);
709 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
710 u_role = USB_ROLE_DEVICE;
711 typec_set_data_role(con->port, TYPEC_DEVICE);
717 /* Complete pending data role swap */
718 if (!completion_done(&con->complete))
719 complete(&con->complete);
721 /* Only notify USB controller if partner supports USB data */
722 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
723 u_role = USB_ROLE_NONE;
725 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
727 dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n",
730 ucsi_partner_task(con, ucsi_check_altmodes, 30, 0);
733 static void ucsi_handle_connector_change(struct work_struct *work)
735 struct ucsi_connector *con = container_of(work, struct ucsi_connector,
737 struct ucsi *ucsi = con->ucsi;
738 struct ucsi_connector_status pre_ack_status;
739 struct ucsi_connector_status post_ack_status;
740 enum typec_role role;
741 enum usb_role u_role = USB_ROLE_NONE;
742 u16 inferred_changes;
747 mutex_lock(&con->lock);
750 * Some/many PPMs have an issue where all fields in the change bitfield
751 * are cleared when an ACK is send. This will causes any change
752 * between GET_CONNECTOR_STATUS and ACK to be lost.
754 * We work around this by re-fetching the connector status afterwards.
755 * We then infer any changes that we see have happened but that may not
756 * be represented in the change bitfield.
758 * Also, even though we don't need to know the currently supported alt
759 * modes, we run the GET_CAM_SUPPORTED command to ensure the PPM does
760 * not get stuck in case it assumes we do.
761 * Always do this, rather than relying on UCSI_CONSTAT_CAM_CHANGE to be
762 * set in the change bitfield.
764 * We end up with the following actions:
765 * 1. UCSI_GET_CONNECTOR_STATUS, store result, update unprocessed_changes
766 * 2. UCSI_GET_CAM_SUPPORTED, discard result
767 * 3. ACK connector change
768 * 4. UCSI_GET_CONNECTOR_STATUS, store result
769 * 5. Infere lost changes by comparing UCSI_GET_CONNECTOR_STATUS results
770 * 6. If PPM reported a new change, then restart in order to ACK
771 * 7. Process everything as usual.
773 * We may end up seeing a change twice, but we can only miss extremely
774 * short transitional changes.
777 /* 1. First UCSI_GET_CONNECTOR_STATUS */
778 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
779 ret = ucsi_send_command(ucsi, command, &pre_ack_status,
780 sizeof(pre_ack_status));
782 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
786 con->unprocessed_changes |= pre_ack_status.change;
788 /* 2. Run UCSI_GET_CAM_SUPPORTED and discard the result. */
789 command = UCSI_GET_CAM_SUPPORTED;
790 command |= UCSI_CONNECTOR_NUMBER(con->num);
791 ucsi_send_command(con->ucsi, command, NULL, 0);
793 /* 3. ACK connector change */
794 ret = ucsi_acknowledge_connector_change(ucsi);
795 clear_bit(EVENT_PENDING, &ucsi->flags);
797 dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret);
801 /* 4. Second UCSI_GET_CONNECTOR_STATUS */
802 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
803 ret = ucsi_send_command(ucsi, command, &post_ack_status,
804 sizeof(post_ack_status));
806 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
811 /* 5. Inferre any missing changes */
812 changed_flags = pre_ack_status.flags ^ post_ack_status.flags;
813 inferred_changes = 0;
814 if (UCSI_CONSTAT_PWR_OPMODE(changed_flags) != 0)
815 inferred_changes |= UCSI_CONSTAT_POWER_OPMODE_CHANGE;
817 if (changed_flags & UCSI_CONSTAT_CONNECTED)
818 inferred_changes |= UCSI_CONSTAT_CONNECT_CHANGE;
820 if (changed_flags & UCSI_CONSTAT_PWR_DIR)
821 inferred_changes |= UCSI_CONSTAT_POWER_DIR_CHANGE;
823 if (UCSI_CONSTAT_PARTNER_FLAGS(changed_flags) != 0)
824 inferred_changes |= UCSI_CONSTAT_PARTNER_CHANGE;
826 if (UCSI_CONSTAT_PARTNER_TYPE(changed_flags) != 0)
827 inferred_changes |= UCSI_CONSTAT_PARTNER_CHANGE;
829 /* Mask out anything that was correctly notified in the later call. */
830 inferred_changes &= ~post_ack_status.change;
831 if (inferred_changes)
832 dev_dbg(ucsi->dev, "%s: Inferred changes that would have been lost: 0x%04x\n",
833 __func__, inferred_changes);
835 con->unprocessed_changes |= inferred_changes;
837 /* 6. If PPM reported a new change, then restart in order to ACK */
838 if (post_ack_status.change)
841 /* 7. Continue as if nothing happened */
842 con->status = post_ack_status;
843 con->status.change = con->unprocessed_changes;
844 con->unprocessed_changes = 0;
846 role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
848 if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE ||
849 con->status.change & UCSI_CONSTAT_POWER_LEVEL_CHANGE) {
850 ucsi_pwr_opmode_change(con);
851 ucsi_port_psy_changed(con);
854 if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
855 typec_set_pwr_role(con->port, role);
857 /* Complete pending power role swap */
858 if (!completion_done(&con->complete))
859 complete(&con->complete);
862 if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
863 typec_set_pwr_role(con->port, role);
865 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
866 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
867 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
868 u_role = USB_ROLE_HOST;
870 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
871 typec_set_data_role(con->port, TYPEC_HOST);
873 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
874 u_role = USB_ROLE_DEVICE;
875 typec_set_data_role(con->port, TYPEC_DEVICE);
881 if (con->status.flags & UCSI_CONSTAT_CONNECTED)
882 ucsi_register_partner(con);
884 ucsi_unregister_partner(con);
886 ucsi_port_psy_changed(con);
888 /* Only notify USB controller if partner supports USB data */
889 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) &
890 UCSI_CONSTAT_PARTNER_FLAG_USB))
891 u_role = USB_ROLE_NONE;
893 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
895 dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n",
899 if (con->status.change & UCSI_CONSTAT_PARTNER_CHANGE)
900 ucsi_partner_change(con);
902 trace_ucsi_connector_change(con->num, &con->status);
905 if (test_and_clear_bit(EVENT_PENDING, &ucsi->flags)) {
906 schedule_work(&con->work);
907 mutex_unlock(&con->lock);
911 clear_bit(EVENT_PROCESSING, &ucsi->flags);
912 mutex_unlock(&con->lock);
916 * ucsi_connector_change - Process Connector Change Event
917 * @ucsi: UCSI Interface
918 * @num: Connector number
920 void ucsi_connector_change(struct ucsi *ucsi, u8 num)
922 struct ucsi_connector *con = &ucsi->connector[num - 1];
924 if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
925 dev_dbg(ucsi->dev, "Bogus connector change event\n");
929 set_bit(EVENT_PENDING, &ucsi->flags);
931 if (!test_and_set_bit(EVENT_PROCESSING, &ucsi->flags))
932 schedule_work(&con->work);
934 EXPORT_SYMBOL_GPL(ucsi_connector_change);
936 /* -------------------------------------------------------------------------- */
938 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
942 command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num);
943 command |= hard ? UCSI_CONNECTOR_RESET_HARD : 0;
945 return ucsi_send_command(con->ucsi, command, NULL, 0);
948 static int ucsi_reset_ppm(struct ucsi *ucsi)
950 u64 command = UCSI_PPM_RESET;
955 mutex_lock(&ucsi->ppm_lock);
957 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command,
962 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
965 if (time_is_before_jiffies(tmo)) {
970 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
974 /* If the PPM is still doing something else, reset it again. */
975 if (cci & ~UCSI_CCI_RESET_COMPLETE) {
976 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL,
984 } while (!(cci & UCSI_CCI_RESET_COMPLETE));
987 mutex_unlock(&ucsi->ppm_lock);
991 static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)
995 ret = ucsi_send_command(con->ucsi, command, NULL, 0);
996 if (ret == -ETIMEDOUT) {
999 /* PPM most likely stopped responding. Resetting everything. */
1000 ucsi_reset_ppm(con->ucsi);
1002 c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
1003 ucsi_send_command(con->ucsi, c, NULL, 0);
1005 ucsi_reset_connector(con, true);
1011 static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
1013 struct ucsi_connector *con = typec_get_drvdata(port);
1018 mutex_lock(&con->lock);
1020 if (!con->partner) {
1025 partner_type = UCSI_CONSTAT_PARTNER_TYPE(con->status.flags);
1026 if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
1027 role == TYPEC_DEVICE) ||
1028 (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
1029 role == TYPEC_HOST))
1032 command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num);
1033 command |= UCSI_SET_UOR_ROLE(role);
1034 command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS;
1035 ret = ucsi_role_cmd(con, command);
1039 if (!wait_for_completion_timeout(&con->complete,
1040 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1044 mutex_unlock(&con->lock);
1046 return ret < 0 ? ret : 0;
1049 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
1051 struct ucsi_connector *con = typec_get_drvdata(port);
1052 enum typec_role cur_role;
1056 mutex_lock(&con->lock);
1058 if (!con->partner) {
1063 cur_role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
1065 if (cur_role == role)
1068 command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num);
1069 command |= UCSI_SET_PDR_ROLE(role);
1070 command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS;
1071 ret = ucsi_role_cmd(con, command);
1075 if (!wait_for_completion_timeout(&con->complete,
1076 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS))) {
1081 /* Something has gone wrong while swapping the role */
1082 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) !=
1083 UCSI_CONSTAT_PWR_OPMODE_PD) {
1084 ucsi_reset_connector(con, true);
1089 mutex_unlock(&con->lock);
1094 static const struct typec_operations ucsi_ops = {
1095 .dr_set = ucsi_dr_swap,
1096 .pr_set = ucsi_pr_swap
1099 /* Caller must call fwnode_handle_put() after use */
1100 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
1102 struct fwnode_handle *fwnode;
1105 device_for_each_child_node(con->ucsi->dev, fwnode)
1106 if (i++ == con->num)
1111 static int ucsi_register_port(struct ucsi *ucsi, int index)
1113 struct ucsi_connector *con = &ucsi->connector[index];
1114 struct typec_capability *cap = &con->typec_cap;
1115 enum typec_accessory *accessory = cap->accessory;
1116 enum usb_role u_role = USB_ROLE_NONE;
1121 name = kasprintf(GFP_KERNEL, "%s-con%d", dev_name(ucsi->dev), con->num);
1125 con->wq = create_singlethread_workqueue(name);
1130 INIT_WORK(&con->work, ucsi_handle_connector_change);
1131 init_completion(&con->complete);
1132 mutex_init(&con->lock);
1133 con->num = index + 1;
1136 /* Delay other interactions with the con until registration is complete */
1137 mutex_lock(&con->lock);
1139 /* Get connector capability */
1140 command = UCSI_GET_CONNECTOR_CAPABILITY;
1141 command |= UCSI_CONNECTOR_NUMBER(con->num);
1142 ret = ucsi_send_command(ucsi, command, &con->cap, sizeof(con->cap));
1146 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP)
1147 cap->data = TYPEC_PORT_DRD;
1148 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP)
1149 cap->data = TYPEC_PORT_DFP;
1150 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP)
1151 cap->data = TYPEC_PORT_UFP;
1153 if ((con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER) &&
1154 (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER))
1155 cap->type = TYPEC_PORT_DRP;
1156 else if (con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER)
1157 cap->type = TYPEC_PORT_SRC;
1158 else if (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER)
1159 cap->type = TYPEC_PORT_SNK;
1161 cap->revision = ucsi->cap.typec_version;
1162 cap->pd_revision = ucsi->cap.pd_version;
1163 cap->svdm_version = SVDM_VER_2_0;
1164 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
1166 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY)
1167 *accessory++ = TYPEC_ACCESSORY_AUDIO;
1168 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
1169 *accessory = TYPEC_ACCESSORY_DEBUG;
1171 cap->fwnode = ucsi_find_fwnode(con);
1172 cap->driver_data = con;
1173 cap->ops = &ucsi_ops;
1175 ret = ucsi_register_port_psy(con);
1179 /* Register the connector */
1180 con->port = typec_register_port(ucsi->dev, cap);
1181 if (IS_ERR(con->port)) {
1182 ret = PTR_ERR(con->port);
1186 /* Alternate modes */
1187 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON);
1189 dev_err(ucsi->dev, "con%d: failed to register alt modes\n",
1194 /* Get the status */
1195 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
1196 ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
1198 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
1202 ret = 0; /* ucsi_send_command() returns length on success */
1204 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1205 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1206 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1207 u_role = USB_ROLE_HOST;
1209 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1210 typec_set_data_role(con->port, TYPEC_HOST);
1212 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1213 u_role = USB_ROLE_DEVICE;
1214 typec_set_data_role(con->port, TYPEC_DEVICE);
1220 /* Check if there is already something connected */
1221 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1222 typec_set_pwr_role(con->port,
1223 !!(con->status.flags & UCSI_CONSTAT_PWR_DIR));
1224 ucsi_pwr_opmode_change(con);
1225 ucsi_register_partner(con);
1226 ucsi_port_psy_changed(con);
1229 con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode);
1230 if (IS_ERR(con->usb_role_sw)) {
1231 dev_err(ucsi->dev, "con%d: failed to get usb role switch\n",
1233 con->usb_role_sw = NULL;
1236 /* Only notify USB controller if partner supports USB data */
1237 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
1238 u_role = USB_ROLE_NONE;
1240 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1242 dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n",
1248 ucsi_check_altmodes(con);
1250 trace_ucsi_register_port(con->num, &con->status);
1253 fwnode_handle_put(cap->fwnode);
1255 mutex_unlock(&con->lock);
1257 if (ret && con->wq) {
1258 destroy_workqueue(con->wq);
1266 * ucsi_init - Initialize UCSI interface
1267 * @ucsi: UCSI to be initialized
1269 * Registers all ports @ucsi has and enables all notification events.
1271 static int ucsi_init(struct ucsi *ucsi)
1273 struct ucsi_connector *con;
1279 ret = ucsi_reset_ppm(ucsi);
1281 dev_err(ucsi->dev, "failed to reset PPM!\n");
1285 /* Enable basic notifications */
1286 ucsi->ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
1287 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1288 ret = ucsi_send_command(ucsi, command, NULL, 0);
1292 /* Get PPM capabilities */
1293 command = UCSI_GET_CAPABILITY;
1294 ret = ucsi_send_command(ucsi, command, &ucsi->cap, sizeof(ucsi->cap));
1298 if (!ucsi->cap.num_connectors) {
1303 /* Allocate the connectors. Released in ucsi_unregister() */
1304 ucsi->connector = kcalloc(ucsi->cap.num_connectors + 1,
1305 sizeof(*ucsi->connector), GFP_KERNEL);
1306 if (!ucsi->connector) {
1311 /* Register all connectors */
1312 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1313 ret = ucsi_register_port(ucsi, i);
1315 goto err_unregister;
1318 /* Enable all notifications */
1319 ucsi->ntfy = UCSI_ENABLE_NTFY_ALL;
1320 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1321 ret = ucsi_send_command(ucsi, command, NULL, 0);
1323 goto err_unregister;
1328 for (con = ucsi->connector; con->port; con++) {
1329 ucsi_unregister_partner(con);
1330 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON);
1331 ucsi_unregister_port_psy(con);
1333 destroy_workqueue(con->wq);
1334 typec_unregister_port(con->port);
1339 memset(&ucsi->cap, 0, sizeof(ucsi->cap));
1340 ucsi_reset_ppm(ucsi);
1345 static void ucsi_init_work(struct work_struct *work)
1347 struct ucsi *ucsi = container_of(work, struct ucsi, work);
1350 ret = ucsi_init(ucsi);
1352 dev_err(ucsi->dev, "PPM init failed (%d)\n", ret);
1356 * ucsi_get_drvdata - Return private driver data pointer
1357 * @ucsi: UCSI interface
1359 void *ucsi_get_drvdata(struct ucsi *ucsi)
1361 return ucsi->driver_data;
1363 EXPORT_SYMBOL_GPL(ucsi_get_drvdata);
1366 * ucsi_set_drvdata - Assign private driver data pointer
1367 * @ucsi: UCSI interface
1368 * @data: Private data pointer
1370 void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
1372 ucsi->driver_data = data;
1374 EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
1377 * ucsi_create - Allocate UCSI instance
1378 * @dev: Device interface to the PPM (Platform Policy Manager)
1379 * @ops: I/O routines
1381 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
1385 if (!ops || !ops->read || !ops->sync_write || !ops->async_write)
1386 return ERR_PTR(-EINVAL);
1388 ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
1390 return ERR_PTR(-ENOMEM);
1392 INIT_WORK(&ucsi->work, ucsi_init_work);
1393 mutex_init(&ucsi->ppm_lock);
1399 EXPORT_SYMBOL_GPL(ucsi_create);
1402 * ucsi_destroy - Free UCSI instance
1403 * @ucsi: UCSI instance to be freed
1405 void ucsi_destroy(struct ucsi *ucsi)
1409 EXPORT_SYMBOL_GPL(ucsi_destroy);
1412 * ucsi_register - Register UCSI interface
1413 * @ucsi: UCSI instance
1415 int ucsi_register(struct ucsi *ucsi)
1419 ret = ucsi->ops->read(ucsi, UCSI_VERSION, &ucsi->version,
1420 sizeof(ucsi->version));
1427 queue_work(system_long_wq, &ucsi->work);
1431 EXPORT_SYMBOL_GPL(ucsi_register);
1434 * ucsi_unregister - Unregister UCSI interface
1435 * @ucsi: UCSI interface to be unregistered
1437 * Unregister UCSI interface that was created with ucsi_register().
1439 void ucsi_unregister(struct ucsi *ucsi)
1441 u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
1444 /* Make sure that we are not in the middle of driver initialization */
1445 cancel_work_sync(&ucsi->work);
1447 /* Disable notifications */
1448 ucsi->ops->async_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
1450 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1451 cancel_work_sync(&ucsi->connector[i].work);
1452 ucsi_unregister_partner(&ucsi->connector[i]);
1453 ucsi_unregister_altmodes(&ucsi->connector[i],
1454 UCSI_RECIPIENT_CON);
1455 ucsi_unregister_port_psy(&ucsi->connector[i]);
1456 if (ucsi->connector[i].wq)
1457 destroy_workqueue(ucsi->connector[i].wq);
1458 typec_unregister_port(ucsi->connector[i].port);
1461 kfree(ucsi->connector);
1463 EXPORT_SYMBOL_GPL(ucsi_unregister);
1465 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1466 MODULE_LICENSE("GPL v2");
1467 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");