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 /* -------------------------------------------------------------------------- */
194 void ucsi_altmode_update_active(struct ucsi_connector *con)
196 const struct typec_altmode *altmode = NULL;
202 command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
203 ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur));
205 if (con->ucsi->version > 0x0100) {
206 dev_err(con->ucsi->dev,
207 "GET_CURRENT_CAM command failed\n");
213 if (cur < UCSI_MAX_ALTMODES)
214 altmode = typec_altmode_get_partner(con->port_altmode[cur]);
216 for (i = 0; con->partner_altmode[i]; i++)
217 typec_altmode_update_active(con->partner_altmode[i],
218 con->partner_altmode[i] == altmode);
221 static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
226 for (i = 0; alt[i]; i++) {
227 if (i > MODE_DISCOVERY_MAX)
230 if (alt[i]->svid == svid)
237 static int ucsi_next_altmode(struct typec_altmode **alt)
241 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
248 static int ucsi_register_altmode(struct ucsi_connector *con,
249 struct typec_altmode_desc *desc,
252 struct typec_altmode *alt;
257 override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
260 case UCSI_RECIPIENT_CON:
261 i = ucsi_next_altmode(con->port_altmode);
267 ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid);
273 switch (desc->svid) {
274 case USB_TYPEC_DP_SID:
275 alt = ucsi_register_displayport(con, override, i, desc);
277 case USB_TYPEC_NVIDIA_VLINK_SID:
278 if (desc->vdo == USB_TYPEC_NVIDIA_VLINK_DBG_VDO)
279 alt = typec_port_register_altmode(con->port,
282 alt = ucsi_register_displayport(con, override,
286 alt = typec_port_register_altmode(con->port, desc);
295 con->port_altmode[i] = alt;
297 case UCSI_RECIPIENT_SOP:
298 i = ucsi_next_altmode(con->partner_altmode);
304 ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid);
310 alt = typec_partner_register_altmode(con->partner, desc);
316 con->partner_altmode[i] = alt;
322 trace_ucsi_register_altmode(recipient, alt);
327 dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n",
328 desc->svid, desc->mode);
334 ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient)
336 int max_altmodes = UCSI_MAX_ALTMODES;
337 struct typec_altmode_desc desc;
338 struct ucsi_altmode alt;
339 struct ucsi_altmode orig[UCSI_MAX_ALTMODES];
340 struct ucsi_altmode updated[UCSI_MAX_ALTMODES];
341 struct ucsi *ucsi = con->ucsi;
342 bool multi_dp = false;
349 if (recipient == UCSI_RECIPIENT_CON)
350 max_altmodes = con->ucsi->cap.num_alt_modes;
352 memset(orig, 0, sizeof(orig));
353 memset(updated, 0, sizeof(updated));
355 /* First get all the alternate modes */
356 for (i = 0; i < max_altmodes; i++) {
357 memset(&alt, 0, sizeof(alt));
358 command = UCSI_GET_ALTERNATE_MODES;
359 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
360 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
361 command |= UCSI_GET_ALTMODE_OFFSET(i);
362 len = ucsi_send_command(con->ucsi, command, &alt, sizeof(alt));
364 * We are collecting all altmodes first and then registering.
365 * Some type-C device will return zero length data beyond last
366 * alternate modes. We should not return if length is zero.
371 /* We got all altmodes, now break out and register them */
372 if (!len || !alt.svid)
375 orig[k].mid = alt.mid;
376 orig[k].svid = alt.svid;
380 * Update the original altmode table as some ppms may report
381 * multiple DP altmodes.
383 if (recipient == UCSI_RECIPIENT_CON)
384 multi_dp = ucsi->ops->update_altmodes(ucsi, orig, updated);
386 /* now register altmodes */
387 for (i = 0; i < max_altmodes; i++) {
388 memset(&desc, 0, sizeof(desc));
389 if (multi_dp && recipient == UCSI_RECIPIENT_CON) {
390 desc.svid = updated[i].svid;
391 desc.vdo = updated[i].mid;
393 desc.svid = orig[i].svid;
394 desc.vdo = orig[i].mid;
396 desc.roles = TYPEC_PORT_DRD;
401 ret = ucsi_register_altmode(con, &desc, recipient);
409 static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
411 int max_altmodes = UCSI_MAX_ALTMODES;
412 struct typec_altmode_desc desc;
413 struct ucsi_altmode alt[2];
421 if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
424 if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
427 if (con->ucsi->ops->update_altmodes)
428 return ucsi_register_altmodes_nvidia(con, recipient);
430 if (recipient == UCSI_RECIPIENT_CON)
431 max_altmodes = con->ucsi->cap.num_alt_modes;
433 for (i = 0; i < max_altmodes;) {
434 memset(alt, 0, sizeof(alt));
435 command = UCSI_GET_ALTERNATE_MODES;
436 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
437 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
438 command |= UCSI_GET_ALTMODE_OFFSET(i);
439 len = ucsi_send_command(con->ucsi, command, alt, sizeof(alt));
446 * This code is requesting one alt mode at a time, but some PPMs
447 * may still return two. If that happens both alt modes need be
448 * registered and the offset for the next alt mode has to be
451 num = len / sizeof(alt[0]);
454 for (j = 0; j < num; j++) {
458 memset(&desc, 0, sizeof(desc));
459 desc.vdo = alt[j].mid;
460 desc.svid = alt[j].svid;
461 desc.roles = TYPEC_PORT_DRD;
463 ret = ucsi_register_altmode(con, &desc, recipient);
472 static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
474 const struct typec_altmode *pdev;
475 struct typec_altmode **adev;
479 case UCSI_RECIPIENT_CON:
480 adev = con->port_altmode;
482 case UCSI_RECIPIENT_SOP:
483 adev = con->partner_altmode;
490 if (recipient == UCSI_RECIPIENT_SOP &&
491 (adev[i]->svid == USB_TYPEC_DP_SID ||
492 (adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID &&
493 adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) {
494 pdev = typec_altmode_get_partner(adev[i]);
495 ucsi_displayport_remove_partner((void *)pdev);
497 typec_unregister_altmode(adev[i]);
502 static int ucsi_get_pdos(struct ucsi_connector *con, int is_partner,
503 u32 *pdos, int offset, int num_pdos)
505 struct ucsi *ucsi = con->ucsi;
509 command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num);
510 command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner);
511 command |= UCSI_GET_PDOS_PDO_OFFSET(offset);
512 command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1);
513 command |= UCSI_GET_PDOS_SRC_PDOS;
514 ret = ucsi_send_command(ucsi, command, pdos + offset,
515 num_pdos * sizeof(u32));
517 dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret);
518 if (ret == 0 && offset == 0)
519 dev_warn(ucsi->dev, "UCSI_GET_PDOS returned 0 bytes\n");
524 static void ucsi_get_src_pdos(struct ucsi_connector *con, int is_partner)
528 /* UCSI max payload means only getting at most 4 PDOs at a time */
529 ret = ucsi_get_pdos(con, 1, con->src_pdos, 0, UCSI_MAX_PDOS);
533 con->num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */
534 if (con->num_pdos < UCSI_MAX_PDOS)
537 /* get the remaining PDOs, if any */
538 ret = ucsi_get_pdos(con, 1, con->src_pdos, UCSI_MAX_PDOS,
539 PDO_MAX_OBJECTS - UCSI_MAX_PDOS);
543 con->num_pdos += ret / sizeof(u32);
546 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
548 switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
549 case UCSI_CONSTAT_PWR_OPMODE_PD:
550 con->rdo = con->status.request_data_obj;
551 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
552 ucsi_get_src_pdos(con, 1);
554 case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
556 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
558 case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
560 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
564 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
569 static int ucsi_register_partner(struct ucsi_connector *con)
571 u8 pwr_opmode = UCSI_CONSTAT_PWR_OPMODE(con->status.flags);
572 struct typec_partner_desc desc;
573 struct typec_partner *partner;
578 memset(&desc, 0, sizeof(desc));
580 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
581 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
582 desc.accessory = TYPEC_ACCESSORY_DEBUG;
584 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
585 desc.accessory = TYPEC_ACCESSORY_AUDIO;
591 desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD;
593 partner = typec_register_partner(con->port, &desc);
594 if (IS_ERR(partner)) {
595 dev_err(con->ucsi->dev,
596 "con%d: failed to register partner (%ld)\n", con->num,
598 return PTR_ERR(partner);
601 con->partner = partner;
606 static void ucsi_unregister_partner(struct ucsi_connector *con)
611 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
612 typec_unregister_partner(con->partner);
616 static void ucsi_partner_change(struct ucsi_connector *con)
618 enum usb_role u_role = USB_ROLE_NONE;
624 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
625 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
626 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
627 u_role = USB_ROLE_HOST;
629 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
630 typec_set_data_role(con->port, TYPEC_HOST);
632 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
633 u_role = USB_ROLE_DEVICE;
634 typec_set_data_role(con->port, TYPEC_DEVICE);
640 /* Complete pending data role swap */
641 if (!completion_done(&con->complete))
642 complete(&con->complete);
644 /* Only notify USB controller if partner supports USB data */
645 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
646 u_role = USB_ROLE_NONE;
648 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
650 dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n",
653 /* Can't rely on Partner Flags field. Always checking the alt modes. */
654 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
656 dev_err(con->ucsi->dev,
657 "con%d: failed to register partner alternate modes\n",
660 ucsi_altmode_update_active(con);
663 static void ucsi_handle_connector_change(struct work_struct *work)
665 struct ucsi_connector *con = container_of(work, struct ucsi_connector,
667 struct ucsi *ucsi = con->ucsi;
668 struct ucsi_connector_status pre_ack_status;
669 struct ucsi_connector_status post_ack_status;
670 enum typec_role role;
671 enum usb_role u_role = USB_ROLE_NONE;
672 u16 inferred_changes;
677 mutex_lock(&con->lock);
680 * Some/many PPMs have an issue where all fields in the change bitfield
681 * are cleared when an ACK is send. This will causes any change
682 * between GET_CONNECTOR_STATUS and ACK to be lost.
684 * We work around this by re-fetching the connector status afterwards.
685 * We then infer any changes that we see have happened but that may not
686 * be represented in the change bitfield.
688 * Also, even though we don't need to know the currently supported alt
689 * modes, we run the GET_CAM_SUPPORTED command to ensure the PPM does
690 * not get stuck in case it assumes we do.
691 * Always do this, rather than relying on UCSI_CONSTAT_CAM_CHANGE to be
692 * set in the change bitfield.
694 * We end up with the following actions:
695 * 1. UCSI_GET_CONNECTOR_STATUS, store result, update unprocessed_changes
696 * 2. UCSI_GET_CAM_SUPPORTED, discard result
697 * 3. ACK connector change
698 * 4. UCSI_GET_CONNECTOR_STATUS, store result
699 * 5. Infere lost changes by comparing UCSI_GET_CONNECTOR_STATUS results
700 * 6. If PPM reported a new change, then restart in order to ACK
701 * 7. Process everything as usual.
703 * We may end up seeing a change twice, but we can only miss extremely
704 * short transitional changes.
707 /* 1. First UCSI_GET_CONNECTOR_STATUS */
708 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
709 ret = ucsi_send_command(ucsi, command, &pre_ack_status,
710 sizeof(pre_ack_status));
712 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
716 con->unprocessed_changes |= pre_ack_status.change;
718 /* 2. Run UCSI_GET_CAM_SUPPORTED and discard the result. */
719 command = UCSI_GET_CAM_SUPPORTED;
720 command |= UCSI_CONNECTOR_NUMBER(con->num);
721 ucsi_send_command(con->ucsi, command, NULL, 0);
723 /* 3. ACK connector change */
724 ret = ucsi_acknowledge_connector_change(ucsi);
725 clear_bit(EVENT_PENDING, &ucsi->flags);
727 dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret);
731 /* 4. Second UCSI_GET_CONNECTOR_STATUS */
732 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
733 ret = ucsi_send_command(ucsi, command, &post_ack_status,
734 sizeof(post_ack_status));
736 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
741 /* 5. Inferre any missing changes */
742 changed_flags = pre_ack_status.flags ^ post_ack_status.flags;
743 inferred_changes = 0;
744 if (UCSI_CONSTAT_PWR_OPMODE(changed_flags) != 0)
745 inferred_changes |= UCSI_CONSTAT_POWER_OPMODE_CHANGE;
747 if (changed_flags & UCSI_CONSTAT_CONNECTED)
748 inferred_changes |= UCSI_CONSTAT_CONNECT_CHANGE;
750 if (changed_flags & UCSI_CONSTAT_PWR_DIR)
751 inferred_changes |= UCSI_CONSTAT_POWER_DIR_CHANGE;
753 if (UCSI_CONSTAT_PARTNER_FLAGS(changed_flags) != 0)
754 inferred_changes |= UCSI_CONSTAT_PARTNER_CHANGE;
756 if (UCSI_CONSTAT_PARTNER_TYPE(changed_flags) != 0)
757 inferred_changes |= UCSI_CONSTAT_PARTNER_CHANGE;
759 /* Mask out anything that was correctly notified in the later call. */
760 inferred_changes &= ~post_ack_status.change;
761 if (inferred_changes)
762 dev_dbg(ucsi->dev, "%s: Inferred changes that would have been lost: 0x%04x\n",
763 __func__, inferred_changes);
765 con->unprocessed_changes |= inferred_changes;
767 /* 6. If PPM reported a new change, then restart in order to ACK */
768 if (post_ack_status.change)
771 /* 7. Continue as if nothing happened */
772 con->status = post_ack_status;
773 con->status.change = con->unprocessed_changes;
774 con->unprocessed_changes = 0;
776 role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
778 if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE ||
779 con->status.change & UCSI_CONSTAT_POWER_LEVEL_CHANGE) {
780 ucsi_pwr_opmode_change(con);
781 ucsi_port_psy_changed(con);
784 if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
785 typec_set_pwr_role(con->port, role);
787 /* Complete pending power role swap */
788 if (!completion_done(&con->complete))
789 complete(&con->complete);
792 if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
793 typec_set_pwr_role(con->port, role);
795 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
796 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
797 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
798 u_role = USB_ROLE_HOST;
800 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
801 typec_set_data_role(con->port, TYPEC_HOST);
803 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
804 u_role = USB_ROLE_DEVICE;
805 typec_set_data_role(con->port, TYPEC_DEVICE);
811 if (con->status.flags & UCSI_CONSTAT_CONNECTED)
812 ucsi_register_partner(con);
814 ucsi_unregister_partner(con);
816 ucsi_port_psy_changed(con);
818 /* Only notify USB controller if partner supports USB data */
819 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) &
820 UCSI_CONSTAT_PARTNER_FLAG_USB))
821 u_role = USB_ROLE_NONE;
823 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
825 dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n",
829 if (con->status.change & UCSI_CONSTAT_PARTNER_CHANGE)
830 ucsi_partner_change(con);
832 trace_ucsi_connector_change(con->num, &con->status);
835 if (test_and_clear_bit(EVENT_PENDING, &ucsi->flags)) {
836 schedule_work(&con->work);
837 mutex_unlock(&con->lock);
841 clear_bit(EVENT_PROCESSING, &ucsi->flags);
842 mutex_unlock(&con->lock);
846 * ucsi_connector_change - Process Connector Change Event
847 * @ucsi: UCSI Interface
848 * @num: Connector number
850 void ucsi_connector_change(struct ucsi *ucsi, u8 num)
852 struct ucsi_connector *con = &ucsi->connector[num - 1];
854 if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
855 dev_dbg(ucsi->dev, "Bogus connector change event\n");
859 set_bit(EVENT_PENDING, &ucsi->flags);
861 if (!test_and_set_bit(EVENT_PROCESSING, &ucsi->flags))
862 schedule_work(&con->work);
864 EXPORT_SYMBOL_GPL(ucsi_connector_change);
866 /* -------------------------------------------------------------------------- */
868 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
872 command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num);
873 command |= hard ? UCSI_CONNECTOR_RESET_HARD : 0;
875 return ucsi_send_command(con->ucsi, command, NULL, 0);
878 static int ucsi_reset_ppm(struct ucsi *ucsi)
880 u64 command = UCSI_PPM_RESET;
885 mutex_lock(&ucsi->ppm_lock);
887 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL, &command,
892 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
895 if (time_is_before_jiffies(tmo)) {
900 ret = ucsi->ops->read(ucsi, UCSI_CCI, &cci, sizeof(cci));
904 /* If the PPM is still doing something else, reset it again. */
905 if (cci & ~UCSI_CCI_RESET_COMPLETE) {
906 ret = ucsi->ops->async_write(ucsi, UCSI_CONTROL,
914 } while (!(cci & UCSI_CCI_RESET_COMPLETE));
917 mutex_unlock(&ucsi->ppm_lock);
921 static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)
925 ret = ucsi_send_command(con->ucsi, command, NULL, 0);
926 if (ret == -ETIMEDOUT) {
929 /* PPM most likely stopped responding. Resetting everything. */
930 ucsi_reset_ppm(con->ucsi);
932 c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
933 ucsi_send_command(con->ucsi, c, NULL, 0);
935 ucsi_reset_connector(con, true);
941 static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
943 struct ucsi_connector *con = typec_get_drvdata(port);
948 mutex_lock(&con->lock);
955 partner_type = UCSI_CONSTAT_PARTNER_TYPE(con->status.flags);
956 if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
957 role == TYPEC_DEVICE) ||
958 (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
962 command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num);
963 command |= UCSI_SET_UOR_ROLE(role);
964 command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS;
965 ret = ucsi_role_cmd(con, command);
969 if (!wait_for_completion_timeout(&con->complete,
970 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
974 mutex_unlock(&con->lock);
976 return ret < 0 ? ret : 0;
979 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
981 struct ucsi_connector *con = typec_get_drvdata(port);
982 enum typec_role cur_role;
986 mutex_lock(&con->lock);
993 cur_role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
995 if (cur_role == role)
998 command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num);
999 command |= UCSI_SET_PDR_ROLE(role);
1000 command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS;
1001 ret = ucsi_role_cmd(con, command);
1005 if (!wait_for_completion_timeout(&con->complete,
1006 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS))) {
1011 /* Something has gone wrong while swapping the role */
1012 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) !=
1013 UCSI_CONSTAT_PWR_OPMODE_PD) {
1014 ucsi_reset_connector(con, true);
1019 mutex_unlock(&con->lock);
1024 static const struct typec_operations ucsi_ops = {
1025 .dr_set = ucsi_dr_swap,
1026 .pr_set = ucsi_pr_swap
1029 /* Caller must call fwnode_handle_put() after use */
1030 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
1032 struct fwnode_handle *fwnode;
1035 device_for_each_child_node(con->ucsi->dev, fwnode)
1036 if (i++ == con->num)
1041 static int ucsi_register_port(struct ucsi *ucsi, int index)
1043 struct ucsi_connector *con = &ucsi->connector[index];
1044 struct typec_capability *cap = &con->typec_cap;
1045 enum typec_accessory *accessory = cap->accessory;
1046 enum usb_role u_role = USB_ROLE_NONE;
1050 INIT_WORK(&con->work, ucsi_handle_connector_change);
1051 init_completion(&con->complete);
1052 mutex_init(&con->lock);
1053 con->num = index + 1;
1056 /* Delay other interactions with the con until registration is complete */
1057 mutex_lock(&con->lock);
1059 /* Get connector capability */
1060 command = UCSI_GET_CONNECTOR_CAPABILITY;
1061 command |= UCSI_CONNECTOR_NUMBER(con->num);
1062 ret = ucsi_send_command(ucsi, command, &con->cap, sizeof(con->cap));
1066 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP)
1067 cap->data = TYPEC_PORT_DRD;
1068 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP)
1069 cap->data = TYPEC_PORT_DFP;
1070 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP)
1071 cap->data = TYPEC_PORT_UFP;
1073 if ((con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER) &&
1074 (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER))
1075 cap->type = TYPEC_PORT_DRP;
1076 else if (con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER)
1077 cap->type = TYPEC_PORT_SRC;
1078 else if (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER)
1079 cap->type = TYPEC_PORT_SNK;
1081 cap->revision = ucsi->cap.typec_version;
1082 cap->pd_revision = ucsi->cap.pd_version;
1083 cap->svdm_version = SVDM_VER_2_0;
1084 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
1086 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY)
1087 *accessory++ = TYPEC_ACCESSORY_AUDIO;
1088 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
1089 *accessory = TYPEC_ACCESSORY_DEBUG;
1091 cap->fwnode = ucsi_find_fwnode(con);
1092 cap->driver_data = con;
1093 cap->ops = &ucsi_ops;
1095 ret = ucsi_register_port_psy(con);
1099 /* Register the connector */
1100 con->port = typec_register_port(ucsi->dev, cap);
1101 if (IS_ERR(con->port)) {
1102 ret = PTR_ERR(con->port);
1106 /* Alternate modes */
1107 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON);
1109 dev_err(ucsi->dev, "con%d: failed to register alt modes\n",
1114 /* Get the status */
1115 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
1116 ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
1118 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
1122 ret = 0; /* ucsi_send_command() returns length on success */
1124 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1125 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1126 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1127 u_role = USB_ROLE_HOST;
1129 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1130 typec_set_data_role(con->port, TYPEC_HOST);
1132 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1133 u_role = USB_ROLE_DEVICE;
1134 typec_set_data_role(con->port, TYPEC_DEVICE);
1140 /* Check if there is already something connected */
1141 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1142 typec_set_pwr_role(con->port,
1143 !!(con->status.flags & UCSI_CONSTAT_PWR_DIR));
1144 ucsi_pwr_opmode_change(con);
1145 ucsi_register_partner(con);
1146 ucsi_port_psy_changed(con);
1149 con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode);
1150 if (IS_ERR(con->usb_role_sw)) {
1151 dev_err(ucsi->dev, "con%d: failed to get usb role switch\n",
1153 con->usb_role_sw = NULL;
1156 /* Only notify USB controller if partner supports USB data */
1157 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
1158 u_role = USB_ROLE_NONE;
1160 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1162 dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n",
1168 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
1171 "con%d: failed to register alternate modes\n",
1175 ucsi_altmode_update_active(con);
1179 trace_ucsi_register_port(con->num, &con->status);
1182 fwnode_handle_put(cap->fwnode);
1184 mutex_unlock(&con->lock);
1189 * ucsi_init - Initialize UCSI interface
1190 * @ucsi: UCSI to be initialized
1192 * Registers all ports @ucsi has and enables all notification events.
1194 static int ucsi_init(struct ucsi *ucsi)
1196 struct ucsi_connector *con;
1202 ret = ucsi_reset_ppm(ucsi);
1204 dev_err(ucsi->dev, "failed to reset PPM!\n");
1208 /* Enable basic notifications */
1209 ucsi->ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
1210 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1211 ret = ucsi_send_command(ucsi, command, NULL, 0);
1215 /* Get PPM capabilities */
1216 command = UCSI_GET_CAPABILITY;
1217 ret = ucsi_send_command(ucsi, command, &ucsi->cap, sizeof(ucsi->cap));
1221 if (!ucsi->cap.num_connectors) {
1226 /* Allocate the connectors. Released in ucsi_unregister() */
1227 ucsi->connector = kcalloc(ucsi->cap.num_connectors + 1,
1228 sizeof(*ucsi->connector), GFP_KERNEL);
1229 if (!ucsi->connector) {
1234 /* Register all connectors */
1235 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1236 ret = ucsi_register_port(ucsi, i);
1238 goto err_unregister;
1241 /* Enable all notifications */
1242 ucsi->ntfy = UCSI_ENABLE_NTFY_ALL;
1243 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1244 ret = ucsi_send_command(ucsi, command, NULL, 0);
1246 goto err_unregister;
1251 for (con = ucsi->connector; con->port; con++) {
1252 ucsi_unregister_partner(con);
1253 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON);
1254 ucsi_unregister_port_psy(con);
1255 typec_unregister_port(con->port);
1260 memset(&ucsi->cap, 0, sizeof(ucsi->cap));
1261 ucsi_reset_ppm(ucsi);
1266 static void ucsi_init_work(struct work_struct *work)
1268 struct ucsi *ucsi = container_of(work, struct ucsi, work);
1271 ret = ucsi_init(ucsi);
1273 dev_err(ucsi->dev, "PPM init failed (%d)\n", ret);
1277 * ucsi_get_drvdata - Return private driver data pointer
1278 * @ucsi: UCSI interface
1280 void *ucsi_get_drvdata(struct ucsi *ucsi)
1282 return ucsi->driver_data;
1284 EXPORT_SYMBOL_GPL(ucsi_get_drvdata);
1287 * ucsi_set_drvdata - Assign private driver data pointer
1288 * @ucsi: UCSI interface
1289 * @data: Private data pointer
1291 void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
1293 ucsi->driver_data = data;
1295 EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
1298 * ucsi_create - Allocate UCSI instance
1299 * @dev: Device interface to the PPM (Platform Policy Manager)
1300 * @ops: I/O routines
1302 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
1306 if (!ops || !ops->read || !ops->sync_write || !ops->async_write)
1307 return ERR_PTR(-EINVAL);
1309 ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
1311 return ERR_PTR(-ENOMEM);
1313 INIT_WORK(&ucsi->work, ucsi_init_work);
1314 mutex_init(&ucsi->ppm_lock);
1320 EXPORT_SYMBOL_GPL(ucsi_create);
1323 * ucsi_destroy - Free UCSI instance
1324 * @ucsi: UCSI instance to be freed
1326 void ucsi_destroy(struct ucsi *ucsi)
1330 EXPORT_SYMBOL_GPL(ucsi_destroy);
1333 * ucsi_register - Register UCSI interface
1334 * @ucsi: UCSI instance
1336 int ucsi_register(struct ucsi *ucsi)
1340 ret = ucsi->ops->read(ucsi, UCSI_VERSION, &ucsi->version,
1341 sizeof(ucsi->version));
1348 queue_work(system_long_wq, &ucsi->work);
1352 EXPORT_SYMBOL_GPL(ucsi_register);
1355 * ucsi_unregister - Unregister UCSI interface
1356 * @ucsi: UCSI interface to be unregistered
1358 * Unregister UCSI interface that was created with ucsi_register().
1360 void ucsi_unregister(struct ucsi *ucsi)
1362 u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
1365 /* Make sure that we are not in the middle of driver initialization */
1366 cancel_work_sync(&ucsi->work);
1368 /* Disable notifications */
1369 ucsi->ops->async_write(ucsi, UCSI_CONTROL, &cmd, sizeof(cmd));
1371 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1372 cancel_work_sync(&ucsi->connector[i].work);
1373 ucsi_unregister_partner(&ucsi->connector[i]);
1374 ucsi_unregister_altmodes(&ucsi->connector[i],
1375 UCSI_RECIPIENT_CON);
1376 ucsi_unregister_port_psy(&ucsi->connector[i]);
1377 typec_unregister_port(ucsi->connector[i].port);
1380 kfree(ucsi->connector);
1382 EXPORT_SYMBOL_GPL(ucsi_unregister);
1384 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1385 MODULE_LICENSE("GPL v2");
1386 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");