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)
134 if (!(cci & UCSI_CCI_COMMAND_COMPLETE))
137 if (cci & UCSI_CCI_NOT_SUPPORTED)
140 if (cci & UCSI_CCI_ERROR) {
141 if (cmd == UCSI_GET_ERROR_STATUS)
143 return ucsi_read_error(ucsi);
146 return UCSI_CCI_LENGTH(cci);
149 int ucsi_send_command(struct ucsi *ucsi, u64 command,
150 void *data, size_t size)
155 mutex_lock(&ucsi->ppm_lock);
157 ret = ucsi_exec_command(ucsi, command);
164 ret = ucsi->ops->read(ucsi, UCSI_MESSAGE_IN, data, size);
169 ret = ucsi_acknowledge_command(ucsi);
175 mutex_unlock(&ucsi->ppm_lock);
178 EXPORT_SYMBOL_GPL(ucsi_send_command);
180 int ucsi_resume(struct ucsi *ucsi)
184 /* Restore UCSI notification enable mask after system resume */
185 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
187 return ucsi_send_command(ucsi, command, NULL, 0);
189 EXPORT_SYMBOL_GPL(ucsi_resume);
190 /* -------------------------------------------------------------------------- */
192 void ucsi_altmode_update_active(struct ucsi_connector *con)
194 const struct typec_altmode *altmode = NULL;
200 command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
201 ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur));
203 if (con->ucsi->version > 0x0100) {
204 dev_err(con->ucsi->dev,
205 "GET_CURRENT_CAM command failed\n");
211 if (cur < UCSI_MAX_ALTMODES)
212 altmode = typec_altmode_get_partner(con->port_altmode[cur]);
214 for (i = 0; con->partner_altmode[i]; i++)
215 typec_altmode_update_active(con->partner_altmode[i],
216 con->partner_altmode[i] == altmode);
219 static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
224 for (i = 0; alt[i]; i++) {
225 if (i > MODE_DISCOVERY_MAX)
228 if (alt[i]->svid == svid)
235 static int ucsi_next_altmode(struct typec_altmode **alt)
239 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
246 static int ucsi_register_altmode(struct ucsi_connector *con,
247 struct typec_altmode_desc *desc,
250 struct typec_altmode *alt;
255 override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
258 case UCSI_RECIPIENT_CON:
259 i = ucsi_next_altmode(con->port_altmode);
265 ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid);
271 switch (desc->svid) {
272 case USB_TYPEC_DP_SID:
273 alt = ucsi_register_displayport(con, override, i, desc);
275 case USB_TYPEC_NVIDIA_VLINK_SID:
276 if (desc->vdo == USB_TYPEC_NVIDIA_VLINK_DBG_VDO)
277 alt = typec_port_register_altmode(con->port,
280 alt = ucsi_register_displayport(con, override,
284 alt = typec_port_register_altmode(con->port, desc);
293 con->port_altmode[i] = alt;
295 case UCSI_RECIPIENT_SOP:
296 i = ucsi_next_altmode(con->partner_altmode);
302 ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid);
308 alt = typec_partner_register_altmode(con->partner, desc);
314 con->partner_altmode[i] = alt;
320 trace_ucsi_register_altmode(recipient, alt);
325 dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n",
326 desc->svid, desc->mode);
332 ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient)
334 int max_altmodes = UCSI_MAX_ALTMODES;
335 struct typec_altmode_desc desc;
336 struct ucsi_altmode alt;
337 struct ucsi_altmode orig[UCSI_MAX_ALTMODES];
338 struct ucsi_altmode updated[UCSI_MAX_ALTMODES];
339 struct ucsi *ucsi = con->ucsi;
340 bool multi_dp = false;
347 if (recipient == UCSI_RECIPIENT_CON)
348 max_altmodes = con->ucsi->cap.num_alt_modes;
350 memset(orig, 0, sizeof(orig));
351 memset(updated, 0, sizeof(updated));
353 /* First get all the alternate modes */
354 for (i = 0; i < max_altmodes; i++) {
355 memset(&alt, 0, sizeof(alt));
356 command = UCSI_GET_ALTERNATE_MODES;
357 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
358 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
359 command |= UCSI_GET_ALTMODE_OFFSET(i);
360 len = ucsi_send_command(con->ucsi, command, &alt, sizeof(alt));
362 * We are collecting all altmodes first and then registering.
363 * Some type-C device will return zero length data beyond last
364 * alternate modes. We should not return if length is zero.
369 /* We got all altmodes, now break out and register them */
370 if (!len || !alt.svid)
373 orig[k].mid = alt.mid;
374 orig[k].svid = alt.svid;
378 * Update the original altmode table as some ppms may report
379 * multiple DP altmodes.
381 if (recipient == UCSI_RECIPIENT_CON)
382 multi_dp = ucsi->ops->update_altmodes(ucsi, orig, updated);
384 /* now register altmodes */
385 for (i = 0; i < max_altmodes; i++) {
386 memset(&desc, 0, sizeof(desc));
387 if (multi_dp && recipient == UCSI_RECIPIENT_CON) {
388 desc.svid = updated[i].svid;
389 desc.vdo = updated[i].mid;
391 desc.svid = orig[i].svid;
392 desc.vdo = orig[i].mid;
394 desc.roles = TYPEC_PORT_DRD;
399 ret = ucsi_register_altmode(con, &desc, recipient);
407 static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
409 int max_altmodes = UCSI_MAX_ALTMODES;
410 struct typec_altmode_desc desc;
411 struct ucsi_altmode alt[2];
419 if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
422 if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
425 if (con->ucsi->ops->update_altmodes)
426 return ucsi_register_altmodes_nvidia(con, recipient);
428 if (recipient == UCSI_RECIPIENT_CON)
429 max_altmodes = con->ucsi->cap.num_alt_modes;
431 for (i = 0; i < max_altmodes;) {
432 memset(alt, 0, sizeof(alt));
433 command = UCSI_GET_ALTERNATE_MODES;
434 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
435 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
436 command |= UCSI_GET_ALTMODE_OFFSET(i);
437 len = ucsi_send_command(con->ucsi, command, alt, sizeof(alt));
442 * This code is requesting one alt mode at a time, but some PPMs
443 * may still return two. If that happens both alt modes need be
444 * registered and the offset for the next alt mode has to be
447 num = len / sizeof(alt[0]);
450 for (j = 0; j < num; j++) {
454 memset(&desc, 0, sizeof(desc));
455 desc.vdo = alt[j].mid;
456 desc.svid = alt[j].svid;
457 desc.roles = TYPEC_PORT_DRD;
459 ret = ucsi_register_altmode(con, &desc, recipient);
468 static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
470 const struct typec_altmode *pdev;
471 struct typec_altmode **adev;
475 case UCSI_RECIPIENT_CON:
476 adev = con->port_altmode;
478 case UCSI_RECIPIENT_SOP:
479 adev = con->partner_altmode;
486 if (recipient == UCSI_RECIPIENT_SOP &&
487 (adev[i]->svid == USB_TYPEC_DP_SID ||
488 (adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID &&
489 adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) {
490 pdev = typec_altmode_get_partner(adev[i]);
491 ucsi_displayport_remove_partner((void *)pdev);
493 typec_unregister_altmode(adev[i]);
498 static int ucsi_get_pdos(struct ucsi_connector *con, int is_partner,
499 u32 *pdos, int offset, int num_pdos)
501 struct ucsi *ucsi = con->ucsi;
505 command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num);
506 command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner);
507 command |= UCSI_GET_PDOS_PDO_OFFSET(offset);
508 command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1);
509 command |= UCSI_GET_PDOS_SRC_PDOS;
510 ret = ucsi_send_command(ucsi, command, pdos + offset,
511 num_pdos * sizeof(u32));
513 dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret);
514 if (ret == 0 && offset == 0)
515 dev_warn(ucsi->dev, "UCSI_GET_PDOS returned 0 bytes\n");
520 static void ucsi_get_src_pdos(struct ucsi_connector *con, int is_partner)
524 /* UCSI max payload means only getting at most 4 PDOs at a time */
525 ret = ucsi_get_pdos(con, 1, con->src_pdos, 0, UCSI_MAX_PDOS);
529 con->num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */
530 if (con->num_pdos < UCSI_MAX_PDOS)
533 /* get the remaining PDOs, if any */
534 ret = ucsi_get_pdos(con, 1, con->src_pdos, UCSI_MAX_PDOS,
535 PDO_MAX_OBJECTS - UCSI_MAX_PDOS);
539 con->num_pdos += ret / sizeof(u32);
542 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
544 switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
545 case UCSI_CONSTAT_PWR_OPMODE_PD:
546 con->rdo = con->status.request_data_obj;
547 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
548 ucsi_get_src_pdos(con, 1);
550 case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
552 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
554 case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
556 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
560 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
565 static int ucsi_register_partner(struct ucsi_connector *con)
567 u8 pwr_opmode = UCSI_CONSTAT_PWR_OPMODE(con->status.flags);
568 struct typec_partner_desc desc;
569 struct typec_partner *partner;
574 memset(&desc, 0, sizeof(desc));
576 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
577 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
578 desc.accessory = TYPEC_ACCESSORY_DEBUG;
580 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
581 desc.accessory = TYPEC_ACCESSORY_AUDIO;
587 desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD;
589 partner = typec_register_partner(con->port, &desc);
590 if (IS_ERR(partner)) {
591 dev_err(con->ucsi->dev,
592 "con%d: failed to register partner (%ld)\n", con->num,
594 return PTR_ERR(partner);
597 con->partner = partner;
602 static void ucsi_unregister_partner(struct ucsi_connector *con)
607 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
608 typec_unregister_partner(con->partner);
612 static void ucsi_partner_change(struct ucsi_connector *con)
614 enum usb_role u_role = USB_ROLE_NONE;
620 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
621 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
622 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
623 u_role = USB_ROLE_HOST;
625 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
626 typec_set_data_role(con->port, TYPEC_HOST);
628 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
629 u_role = USB_ROLE_DEVICE;
630 typec_set_data_role(con->port, TYPEC_DEVICE);
636 /* Complete pending data role swap */
637 if (!completion_done(&con->complete))
638 complete(&con->complete);
640 /* Only notify USB controller if partner supports USB data */
641 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
642 u_role = USB_ROLE_NONE;
644 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
646 dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n",
649 /* Can't rely on Partner Flags field. Always checking the alt modes. */
650 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
652 dev_err(con->ucsi->dev,
653 "con%d: failed to register partner alternate modes\n",
656 ucsi_altmode_update_active(con);
659 static void ucsi_handle_connector_change(struct work_struct *work)
661 struct ucsi_connector *con = container_of(work, struct ucsi_connector,
663 struct ucsi *ucsi = con->ucsi;
664 struct ucsi_connector_status pre_ack_status;
665 struct ucsi_connector_status post_ack_status;
666 enum typec_role role;
667 enum usb_role u_role = USB_ROLE_NONE;
668 u16 inferred_changes;
673 mutex_lock(&con->lock);
676 * Some/many PPMs have an issue where all fields in the change bitfield
677 * are cleared when an ACK is send. This will causes any change
678 * between GET_CONNECTOR_STATUS and ACK to be lost.
680 * We work around this by re-fetching the connector status afterwards.
681 * We then infer any changes that we see have happened but that may not
682 * be represented in the change bitfield.
684 * Also, even though we don't need to know the currently supported alt
685 * modes, we run the GET_CAM_SUPPORTED command to ensure the PPM does
686 * not get stuck in case it assumes we do.
687 * Always do this, rather than relying on UCSI_CONSTAT_CAM_CHANGE to be
688 * set in the change bitfield.
690 * We end up with the following actions:
691 * 1. UCSI_GET_CONNECTOR_STATUS, store result, update unprocessed_changes
692 * 2. UCSI_GET_CAM_SUPPORTED, discard result
693 * 3. ACK connector change
694 * 4. UCSI_GET_CONNECTOR_STATUS, store result
695 * 5. Infere lost changes by comparing UCSI_GET_CONNECTOR_STATUS results
696 * 6. If PPM reported a new change, then restart in order to ACK
697 * 7. Process everything as usual.
699 * We may end up seeing a change twice, but we can only miss extremely
700 * short transitional changes.
703 /* 1. First UCSI_GET_CONNECTOR_STATUS */
704 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
705 ret = ucsi_send_command(ucsi, command, &pre_ack_status,
706 sizeof(pre_ack_status));
708 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
712 con->unprocessed_changes |= pre_ack_status.change;
714 /* 2. Run UCSI_GET_CAM_SUPPORTED and discard the result. */
715 command = UCSI_GET_CAM_SUPPORTED;
716 command |= UCSI_CONNECTOR_NUMBER(con->num);
717 ucsi_send_command(con->ucsi, command, NULL, 0);
719 /* 3. ACK connector change */
720 ret = ucsi_acknowledge_connector_change(ucsi);
721 clear_bit(EVENT_PENDING, &ucsi->flags);
723 dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret);
727 /* 4. Second UCSI_GET_CONNECTOR_STATUS */
728 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
729 ret = ucsi_send_command(ucsi, command, &post_ack_status,
730 sizeof(post_ack_status));
732 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
737 /* 5. Inferre any missing changes */
738 changed_flags = pre_ack_status.flags ^ post_ack_status.flags;
739 inferred_changes = 0;
740 if (UCSI_CONSTAT_PWR_OPMODE(changed_flags) != 0)
741 inferred_changes |= UCSI_CONSTAT_POWER_OPMODE_CHANGE;
743 if (changed_flags & UCSI_CONSTAT_CONNECTED)
744 inferred_changes |= UCSI_CONSTAT_CONNECT_CHANGE;
746 if (changed_flags & UCSI_CONSTAT_PWR_DIR)
747 inferred_changes |= UCSI_CONSTAT_POWER_DIR_CHANGE;
749 if (UCSI_CONSTAT_PARTNER_FLAGS(changed_flags) != 0)
750 inferred_changes |= UCSI_CONSTAT_PARTNER_CHANGE;
752 if (UCSI_CONSTAT_PARTNER_TYPE(changed_flags) != 0)
753 inferred_changes |= UCSI_CONSTAT_PARTNER_CHANGE;
755 /* Mask out anything that was correctly notified in the later call. */
756 inferred_changes &= ~post_ack_status.change;
757 if (inferred_changes)
758 dev_dbg(ucsi->dev, "%s: Inferred changes that would have been lost: 0x%04x\n",
759 __func__, inferred_changes);
761 con->unprocessed_changes |= inferred_changes;
763 /* 6. If PPM reported a new change, then restart in order to ACK */
764 if (post_ack_status.change)
767 /* 7. Continue as if nothing happened */
768 con->status = post_ack_status;
769 con->status.change = con->unprocessed_changes;
770 con->unprocessed_changes = 0;
772 role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
774 if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE ||
775 con->status.change & UCSI_CONSTAT_POWER_LEVEL_CHANGE) {
776 ucsi_pwr_opmode_change(con);
777 ucsi_port_psy_changed(con);
780 if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
781 typec_set_pwr_role(con->port, role);
783 /* Complete pending power role swap */
784 if (!completion_done(&con->complete))
785 complete(&con->complete);
788 if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
789 typec_set_pwr_role(con->port, role);
791 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
792 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
793 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
794 u_role = USB_ROLE_HOST;
796 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
797 typec_set_data_role(con->port, TYPEC_HOST);
799 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
800 u_role = USB_ROLE_DEVICE;
801 typec_set_data_role(con->port, TYPEC_DEVICE);
807 if (con->status.flags & UCSI_CONSTAT_CONNECTED)
808 ucsi_register_partner(con);
810 ucsi_unregister_partner(con);
812 ucsi_port_psy_changed(con);
814 /* Only notify USB controller if partner supports USB data */
815 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) &
816 UCSI_CONSTAT_PARTNER_FLAG_USB))
817 u_role = USB_ROLE_NONE;
819 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
821 dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n",
825 if (con->status.change & UCSI_CONSTAT_PARTNER_CHANGE)
826 ucsi_partner_change(con);
828 trace_ucsi_connector_change(con->num, &con->status);
831 if (test_and_clear_bit(EVENT_PENDING, &ucsi->flags)) {
832 schedule_work(&con->work);
833 mutex_unlock(&con->lock);
837 clear_bit(EVENT_PROCESSING, &ucsi->flags);
838 mutex_unlock(&con->lock);
842 * ucsi_connector_change - Process Connector Change Event
843 * @ucsi: UCSI Interface
844 * @num: Connector number
846 void ucsi_connector_change(struct ucsi *ucsi, u8 num)
848 struct ucsi_connector *con = &ucsi->connector[num - 1];
850 if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
851 dev_dbg(ucsi->dev, "Bogus connector change event\n");
855 set_bit(EVENT_PENDING, &ucsi->flags);
857 if (!test_and_set_bit(EVENT_PROCESSING, &ucsi->flags))
858 schedule_work(&con->work);
860 EXPORT_SYMBOL_GPL(ucsi_connector_change);
862 /* -------------------------------------------------------------------------- */
864 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
868 command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num);
869 command |= hard ? UCSI_CONNECTOR_RESET_HARD : 0;
871 return ucsi_send_command(con->ucsi, command, NULL, 0);
874 static int ucsi_reset_ppm(struct ucsi *ucsi)
876 u64 command = UCSI_PPM_RESET;
881 mutex_lock(&ucsi->ppm_lock);
883 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command,
888 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
891 if (time_is_before_jiffies(tmo)) {
896 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
900 /* If the PPM is still doing something else, reset it again. */
901 if (cci & ~UCSI_CCI_RESET_COMPLETE) {
902 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL,
910 } while (!(cci & UCSI_CCI_RESET_COMPLETE));
913 mutex_unlock(&ucsi->ppm_lock);
917 static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)
921 ret = ucsi_send_command(con->ucsi, command, NULL, 0);
922 if (ret == -ETIMEDOUT) {
925 /* PPM most likely stopped responding. Resetting everything. */
926 ucsi_reset_ppm(con->ucsi);
928 c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
929 ucsi_send_command(con->ucsi, c, NULL, 0);
931 ucsi_reset_connector(con, true);
937 static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
939 struct ucsi_connector *con = typec_get_drvdata(port);
944 mutex_lock(&con->lock);
951 partner_type = UCSI_CONSTAT_PARTNER_TYPE(con->status.flags);
952 if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
953 role == TYPEC_DEVICE) ||
954 (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
958 command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num);
959 command |= UCSI_SET_UOR_ROLE(role);
960 command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS;
961 ret = ucsi_role_cmd(con, command);
965 if (!wait_for_completion_timeout(&con->complete,
966 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
970 mutex_unlock(&con->lock);
972 return ret < 0 ? ret : 0;
975 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
977 struct ucsi_connector *con = typec_get_drvdata(port);
978 enum typec_role cur_role;
982 mutex_lock(&con->lock);
989 cur_role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
991 if (cur_role == role)
994 command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num);
995 command |= UCSI_SET_PDR_ROLE(role);
996 command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS;
997 ret = ucsi_role_cmd(con, command);
1001 if (!wait_for_completion_timeout(&con->complete,
1002 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS))) {
1007 /* Something has gone wrong while swapping the role */
1008 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) !=
1009 UCSI_CONSTAT_PWR_OPMODE_PD) {
1010 ucsi_reset_connector(con, true);
1015 mutex_unlock(&con->lock);
1020 static const struct typec_operations ucsi_ops = {
1021 .dr_set = ucsi_dr_swap,
1022 .pr_set = ucsi_pr_swap
1025 /* Caller must call fwnode_handle_put() after use */
1026 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
1028 struct fwnode_handle *fwnode;
1031 device_for_each_child_node(con->ucsi->dev, fwnode)
1032 if (i++ == con->num)
1037 static int ucsi_register_port(struct ucsi *ucsi, int index)
1039 struct ucsi_connector *con = &ucsi->connector[index];
1040 struct typec_capability *cap = &con->typec_cap;
1041 enum typec_accessory *accessory = cap->accessory;
1042 enum usb_role u_role = USB_ROLE_NONE;
1046 INIT_WORK(&con->work, ucsi_handle_connector_change);
1047 init_completion(&con->complete);
1048 mutex_init(&con->lock);
1049 con->num = index + 1;
1052 /* Delay other interactions with the con until registration is complete */
1053 mutex_lock(&con->lock);
1055 /* Get connector capability */
1056 command = UCSI_GET_CONNECTOR_CAPABILITY;
1057 command |= UCSI_CONNECTOR_NUMBER(con->num);
1058 ret = ucsi_send_command(ucsi, command, &con->cap, sizeof(con->cap));
1062 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP)
1063 cap->data = TYPEC_PORT_DRD;
1064 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP)
1065 cap->data = TYPEC_PORT_DFP;
1066 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP)
1067 cap->data = TYPEC_PORT_UFP;
1069 if ((con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER) &&
1070 (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER))
1071 cap->type = TYPEC_PORT_DRP;
1072 else if (con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER)
1073 cap->type = TYPEC_PORT_SRC;
1074 else if (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER)
1075 cap->type = TYPEC_PORT_SNK;
1077 cap->revision = ucsi->cap.typec_version;
1078 cap->pd_revision = ucsi->cap.pd_version;
1079 cap->svdm_version = SVDM_VER_2_0;
1080 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
1082 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY)
1083 *accessory++ = TYPEC_ACCESSORY_AUDIO;
1084 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
1085 *accessory = TYPEC_ACCESSORY_DEBUG;
1087 cap->fwnode = ucsi_find_fwnode(con);
1088 cap->driver_data = con;
1089 cap->ops = &ucsi_ops;
1091 ret = ucsi_register_port_psy(con);
1095 /* Register the connector */
1096 con->port = typec_register_port(ucsi->dev, cap);
1097 if (IS_ERR(con->port)) {
1098 ret = PTR_ERR(con->port);
1102 /* Alternate modes */
1103 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON);
1105 dev_err(ucsi->dev, "con%d: failed to register alt modes\n",
1110 /* Get the status */
1111 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
1112 ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
1114 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
1118 ret = 0; /* ucsi_send_command() returns length on success */
1120 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1121 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1122 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1123 u_role = USB_ROLE_HOST;
1125 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1126 typec_set_data_role(con->port, TYPEC_HOST);
1128 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1129 u_role = USB_ROLE_DEVICE;
1130 typec_set_data_role(con->port, TYPEC_DEVICE);
1136 /* Check if there is already something connected */
1137 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1138 typec_set_pwr_role(con->port,
1139 !!(con->status.flags & UCSI_CONSTAT_PWR_DIR));
1140 ucsi_pwr_opmode_change(con);
1141 ucsi_register_partner(con);
1142 ucsi_port_psy_changed(con);
1145 con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode);
1146 if (IS_ERR(con->usb_role_sw)) {
1147 dev_err(ucsi->dev, "con%d: failed to get usb role switch\n",
1149 con->usb_role_sw = NULL;
1152 /* Only notify USB controller if partner supports USB data */
1153 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
1154 u_role = USB_ROLE_NONE;
1156 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1158 dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n",
1164 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
1167 "con%d: failed to register alternate modes\n",
1171 ucsi_altmode_update_active(con);
1175 trace_ucsi_register_port(con->num, &con->status);
1178 fwnode_handle_put(cap->fwnode);
1180 mutex_unlock(&con->lock);
1185 * ucsi_init - Initialize UCSI interface
1186 * @ucsi: UCSI to be initialized
1188 * Registers all ports @ucsi has and enables all notification events.
1190 static int ucsi_init(struct ucsi *ucsi)
1192 struct ucsi_connector *con;
1198 ret = ucsi_reset_ppm(ucsi);
1200 dev_err(ucsi->dev, "failed to reset PPM!\n");
1204 /* Enable basic notifications */
1205 ucsi->ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
1206 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1207 ret = ucsi_send_command(ucsi, command, NULL, 0);
1211 /* Get PPM capabilities */
1212 command = UCSI_GET_CAPABILITY;
1213 ret = ucsi_send_command(ucsi, command, &ucsi->cap, sizeof(ucsi->cap));
1217 if (!ucsi->cap.num_connectors) {
1222 /* Allocate the connectors. Released in ucsi_unregister_ppm() */
1223 ucsi->connector = kcalloc(ucsi->cap.num_connectors + 1,
1224 sizeof(*ucsi->connector), GFP_KERNEL);
1225 if (!ucsi->connector) {
1230 /* Register all connectors */
1231 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1232 ret = ucsi_register_port(ucsi, i);
1234 goto err_unregister;
1237 /* Enable all notifications */
1238 ucsi->ntfy = UCSI_ENABLE_NTFY_ALL;
1239 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1240 ret = ucsi_send_command(ucsi, command, NULL, 0);
1242 goto err_unregister;
1247 for (con = ucsi->connector; con->port; con++) {
1248 ucsi_unregister_partner(con);
1249 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON);
1250 ucsi_unregister_port_psy(con);
1251 typec_unregister_port(con->port);
1256 memset(&ucsi->cap, 0, sizeof(ucsi->cap));
1257 ucsi_reset_ppm(ucsi);
1262 static void ucsi_init_work(struct work_struct *work)
1264 struct ucsi *ucsi = container_of(work, struct ucsi, work);
1267 ret = ucsi_init(ucsi);
1269 dev_err(ucsi->dev, "PPM init failed (%d)\n", ret);
1273 * ucsi_get_drvdata - Return private driver data pointer
1274 * @ucsi: UCSI interface
1276 void *ucsi_get_drvdata(struct ucsi *ucsi)
1278 return ucsi->driver_data;
1280 EXPORT_SYMBOL_GPL(ucsi_get_drvdata);
1283 * ucsi_get_drvdata - Assign private driver data pointer
1284 * @ucsi: UCSI interface
1285 * @data: Private data pointer
1287 void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
1289 ucsi->driver_data = data;
1291 EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
1294 * ucsi_create - Allocate UCSI instance
1295 * @dev: Device interface to the PPM (Platform Policy Manager)
1296 * @ops: I/O routines
1298 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
1302 if (!ops || !ops->read || !ops->sync_write || !ops->async_write)
1303 return ERR_PTR(-EINVAL);
1305 ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
1307 return ERR_PTR(-ENOMEM);
1309 INIT_WORK(&ucsi->work, ucsi_init_work);
1310 mutex_init(&ucsi->ppm_lock);
1316 EXPORT_SYMBOL_GPL(ucsi_create);
1319 * ucsi_destroy - Free UCSI instance
1320 * @ucsi: UCSI instance to be freed
1322 void ucsi_destroy(struct ucsi *ucsi)
1326 EXPORT_SYMBOL_GPL(ucsi_destroy);
1329 * ucsi_register - Register UCSI interface
1330 * @ucsi: UCSI instance
1332 int ucsi_register(struct ucsi *ucsi)
1336 ret = ucsi->ops->read(ucsi, UCSI_VERSION, &ucsi->version,
1337 sizeof(ucsi->version));
1344 queue_work(system_long_wq, &ucsi->work);
1348 EXPORT_SYMBOL_GPL(ucsi_register);
1351 * ucsi_unregister - Unregister UCSI interface
1352 * @ucsi: UCSI interface to be unregistered
1354 * Unregister UCSI interface that was created with ucsi_register().
1356 void ucsi_unregister(struct ucsi *ucsi)
1358 u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
1361 /* Make sure that we are not in the middle of driver initialization */
1362 cancel_work_sync(&ucsi->work);
1364 /* Disable notifications */
1365 ucsi->ops->async_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
1367 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1368 cancel_work_sync(&ucsi->connector[i].work);
1369 ucsi_unregister_partner(&ucsi->connector[i]);
1370 ucsi_unregister_altmodes(&ucsi->connector[i],
1371 UCSI_RECIPIENT_CON);
1372 ucsi_unregister_port_psy(&ucsi->connector[i]);
1373 typec_unregister_port(ucsi->connector[i].port);
1376 kfree(ucsi->connector);
1378 EXPORT_SYMBOL_GPL(ucsi_unregister);
1380 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1381 MODULE_LICENSE("GPL v2");
1382 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");