1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2015-2017 Google, Inc
5 * USB Power Delivery protocol stack.
8 #include <linux/completion.h>
9 #include <linux/debugfs.h>
10 #include <linux/device.h>
11 #include <linux/hrtimer.h>
12 #include <linux/jiffies.h>
13 #include <linux/kernel.h>
14 #include <linux/kthread.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/power_supply.h>
18 #include <linux/proc_fs.h>
19 #include <linux/property.h>
20 #include <linux/sched/clock.h>
21 #include <linux/seq_file.h>
22 #include <linux/slab.h>
23 #include <linux/spinlock.h>
24 #include <linux/string_choices.h>
25 #include <linux/usb.h>
26 #include <linux/usb/pd.h>
27 #include <linux/usb/pd_ado.h>
28 #include <linux/usb/pd_bdo.h>
29 #include <linux/usb/pd_ext_sdb.h>
30 #include <linux/usb/pd_vdo.h>
31 #include <linux/usb/role.h>
32 #include <linux/usb/tcpm.h>
33 #include <linux/usb/typec_altmode.h>
35 #include <uapi/linux/sched/types.h>
37 #define FOREACH_STATE(S) \
40 S(CHECK_CONTAMINANT), \
45 S(SRC_SEND_CAPABILITIES), \
46 S(SRC_SEND_CAPABILITIES_TIMEOUT), \
47 S(SRC_NEGOTIATE_CAPABILITIES), \
48 S(SRC_TRANSITION_SUPPLY), \
50 S(SRC_WAIT_NEW_CAPABILITIES), \
58 S(SNK_DISCOVERY_DEBOUNCE), \
59 S(SNK_DISCOVERY_DEBOUNCE_DONE), \
60 S(SNK_WAIT_CAPABILITIES), \
61 S(SNK_WAIT_CAPABILITIES_TIMEOUT), \
62 S(SNK_NEGOTIATE_CAPABILITIES), \
63 S(SNK_NEGOTIATE_PPS_CAPABILITIES), \
64 S(SNK_TRANSITION_SINK), \
65 S(SNK_TRANSITION_SINK_VBUS), \
69 S(DEBUG_ACC_ATTACHED), \
70 S(DEBUG_ACC_DEBOUNCE), \
71 S(AUDIO_ACC_ATTACHED), \
72 S(AUDIO_ACC_DEBOUNCE), \
75 S(HARD_RESET_START), \
76 S(SRC_HARD_RESET_VBUS_OFF), \
77 S(SRC_HARD_RESET_VBUS_ON), \
78 S(SNK_HARD_RESET_SINK_OFF), \
79 S(SNK_HARD_RESET_WAIT_VBUS), \
80 S(SNK_HARD_RESET_SINK_ON), \
83 S(SRC_SOFT_RESET_WAIT_SNK_TX), \
89 S(DR_SWAP_SEND_TIMEOUT), \
91 S(DR_SWAP_CHANGE_DR), \
95 S(PR_SWAP_SEND_TIMEOUT), \
98 S(PR_SWAP_SRC_SNK_TRANSITION_OFF), \
99 S(PR_SWAP_SRC_SNK_SOURCE_OFF), \
100 S(PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED), \
101 S(PR_SWAP_SRC_SNK_SINK_ON), \
102 S(PR_SWAP_SNK_SRC_SINK_OFF), \
103 S(PR_SWAP_SNK_SRC_SOURCE_ON), \
104 S(PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP), \
106 S(VCONN_SWAP_ACCEPT), \
107 S(VCONN_SWAP_SEND), \
108 S(VCONN_SWAP_SEND_TIMEOUT), \
109 S(VCONN_SWAP_CANCEL), \
110 S(VCONN_SWAP_START), \
111 S(VCONN_SWAP_WAIT_FOR_VCONN), \
112 S(VCONN_SWAP_TURN_ON_VCONN), \
113 S(VCONN_SWAP_TURN_OFF_VCONN), \
114 S(VCONN_SWAP_SEND_SOFT_RESET), \
117 S(FR_SWAP_SEND_TIMEOUT), \
118 S(FR_SWAP_SNK_SRC_TRANSITION_TO_OFF), \
119 S(FR_SWAP_SNK_SRC_NEW_SINK_READY), \
120 S(FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED), \
125 S(SNK_TRY_WAIT_DEBOUNCE), \
126 S(SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS), \
128 S(SRC_TRYWAIT_DEBOUNCE), \
129 S(SRC_TRYWAIT_UNATTACHED), \
133 S(SRC_TRY_DEBOUNCE), \
135 S(SNK_TRYWAIT_DEBOUNCE), \
136 S(SNK_TRYWAIT_VBUS), \
139 S(GET_STATUS_SEND), \
140 S(GET_STATUS_SEND_TIMEOUT), \
141 S(GET_PPS_STATUS_SEND), \
142 S(GET_PPS_STATUS_SEND_TIMEOUT), \
145 S(GET_SINK_CAP_TIMEOUT), \
149 S(PORT_RESET_WAIT_OFF), \
154 S(SRC_VDM_IDENTITY_REQUEST)
156 #define FOREACH_AMS(S) \
158 S(POWER_NEGOTIATION), \
163 S(GET_SOURCE_CAPABILITIES), \
164 S(GET_SINK_CAPABILITIES), \
165 S(POWER_ROLE_SWAP), \
170 S(GETTING_SOURCE_EXTENDED_CAPABILITIES),\
171 S(GETTING_SOURCE_SINK_STATUS), \
172 S(GETTING_BATTERY_CAPABILITIES), \
173 S(GETTING_BATTERY_STATUS), \
174 S(GETTING_MANUFACTURER_INFORMATION), \
176 S(FIRMWARE_UPDATE), \
177 S(DISCOVER_IDENTITY), \
178 S(SOURCE_STARTUP_CABLE_PLUG_DISCOVER_IDENTITY), \
181 S(DFP_TO_UFP_ENTER_MODE), \
182 S(DFP_TO_UFP_EXIT_MODE), \
183 S(DFP_TO_CABLE_PLUG_ENTER_MODE), \
184 S(DFP_TO_CABLE_PLUG_EXIT_MODE), \
187 S(UNSTRUCTURED_VDMS), \
188 S(STRUCTURED_VDMS), \
191 S(REVISION_INFORMATION)
193 #define GENERATE_ENUM(e) e
194 #define GENERATE_STRING(s) #s
197 FOREACH_STATE(GENERATE_ENUM)
200 static const char * const tcpm_states[] = {
201 FOREACH_STATE(GENERATE_STRING)
205 FOREACH_AMS(GENERATE_ENUM)
208 static const char * const tcpm_ams_str[] = {
209 FOREACH_AMS(GENERATE_STRING)
213 VDM_STATE_ERR_BUSY = -3,
214 VDM_STATE_ERR_SEND = -2,
215 VDM_STATE_ERR_TMOUT = -1,
217 /* Anything >0 represents an active state */
220 VDM_STATE_WAIT_RSP_BUSY = 3,
221 VDM_STATE_SEND_MESSAGE = 4,
224 enum pd_msg_request {
228 PD_MSG_CTRL_NOT_SUPP,
229 PD_MSG_DATA_SINK_CAP,
230 PD_MSG_DATA_SOURCE_CAP,
236 ADEV_NOTIFY_USB_AND_QUEUE_VDM,
238 ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL,
243 * Initial current capability of the new source when vSafe5V is applied during PD3.0 Fast Role Swap.
244 * Based on "Table 6-14 Fixed Supply PDO - Sink" of "USB Power Delivery Specification Revision 3.0,
247 enum frs_typec_current {
254 /* Events from low level driver */
256 #define TCPM_CC_EVENT BIT(0)
257 #define TCPM_VBUS_EVENT BIT(1)
258 #define TCPM_RESET_EVENT BIT(2)
259 #define TCPM_FRS_EVENT BIT(3)
260 #define TCPM_SOURCING_VBUS BIT(4)
261 #define TCPM_PORT_CLEAN BIT(5)
262 #define TCPM_PORT_ERROR BIT(6)
264 #define LOG_BUFFER_ENTRIES 1024
265 #define LOG_BUFFER_ENTRY_SIZE 128
267 /* Alternate mode support */
269 #define SVID_DISCOVERY_MAX 16
270 #define ALTMODE_DISCOVERY_MAX (SVID_DISCOVERY_MAX * MODE_DISCOVERY_MAX)
272 #define GET_SINK_CAP_RETRY_MS 100
273 #define SEND_DISCOVER_RETRY_MS 100
275 struct pd_mode_data {
276 int svid_index; /* current SVID index */
278 u16 svids[SVID_DISCOVERY_MAX];
279 int altmodes; /* number of alternate modes */
280 struct typec_altmode_desc altmode_desc[ALTMODE_DISCOVERY_MAX];
284 * @min_volt: Actual min voltage at the local port
285 * @req_min_volt: Requested min voltage to the port partner
286 * @max_volt: Actual max voltage at the local port
287 * @req_max_volt: Requested max voltage to the port partner
288 * @max_curr: Actual max current at the local port
289 * @req_max_curr: Requested max current of the port partner
290 * @req_out_volt: Requested output voltage to the port partner
291 * @req_op_curr: Requested operating current to the port partner
292 * @supported: Parter has at least one APDO hence supports PPS
293 * @active: PPS mode is active
309 struct usb_power_delivery *pd;
310 struct usb_power_delivery_capabilities *source_cap;
311 struct usb_power_delivery_capabilities_desc source_desc;
312 struct usb_power_delivery_capabilities *sink_cap;
313 struct usb_power_delivery_capabilities_desc sink_desc;
314 unsigned int operating_snk_mw;
317 #define PD_CAP_REV10 0x1
318 #define PD_CAP_REV20 0x2
319 #define PD_CAP_REV30 0x3
321 struct pd_revision_info {
329 * @sink_wait_cap_time: Deadline (in ms) for tTypeCSinkWaitCap timer
330 * @ps_src_wait_off_time: Deadline (in ms) for tPSSourceOff timer
331 * @cc_debounce_time: Deadline (in ms) for tCCDebounce timer
334 u32 sink_wait_cap_time;
336 u32 cc_debounce_time;
337 u32 snk_bc12_cmpletion_time;
343 struct mutex lock; /* tcpm state machine lock */
344 struct kthread_worker *wq;
346 struct typec_capability typec_caps;
347 struct typec_port *typec_port;
349 struct tcpc_dev *tcpc;
350 struct usb_role_switch *role_sw;
352 enum typec_role vconn_role;
353 enum typec_role pwr_role;
354 enum typec_data_role data_role;
355 enum typec_pwr_opmode pwr_opmode;
357 struct usb_pd_identity partner_ident;
358 struct typec_partner_desc partner_desc;
359 struct typec_partner *partner;
361 struct usb_pd_identity cable_ident;
362 struct typec_cable_desc cable_desc;
363 struct typec_cable *cable;
364 struct typec_plug_desc plug_prime_desc;
365 struct typec_plug *plug_prime;
367 enum typec_cc_status cc_req;
368 enum typec_cc_status src_rp; /* work only if pd_supported == false */
370 enum typec_cc_status cc1;
371 enum typec_cc_status cc2;
372 enum typec_cc_polarity polarity;
378 enum typec_port_type port_type;
381 * Set to true when vbus is greater than VSAFE5V min.
382 * Set to false when vbus falls below vSinkDisconnect max threshold.
387 * Set to true when vbus is less than VSAFE0V max.
388 * Set to false when vbus is greater than VSAFE0V max.
396 /* Set to true when Discover_Identity Command is expected to be sent in Ready states. */
404 enum pd_msg_request queued_message;
406 enum tcpm_state enter_state;
407 enum tcpm_state prev_state;
408 enum tcpm_state state;
409 enum tcpm_state delayed_state;
410 ktime_t delayed_runtime;
411 unsigned long delay_ms;
413 spinlock_t pd_event_lock;
416 struct kthread_work event_work;
417 struct hrtimer state_machine_timer;
418 struct kthread_work state_machine;
419 struct hrtimer vdm_state_machine_timer;
420 struct kthread_work vdm_state_machine;
421 struct hrtimer enable_frs_timer;
422 struct kthread_work enable_frs;
423 struct hrtimer send_discover_timer;
424 struct kthread_work send_discover_work;
425 bool state_machine_running;
426 /* Set to true when VDM State Machine has following actions. */
429 struct completion tx_complete;
430 enum tcpm_transmit_status tx_status;
432 struct mutex swap_lock; /* swap command lock */
434 bool non_pd_role_swap;
435 struct completion swap_complete;
438 unsigned int negotiated_rev;
439 unsigned int message_id;
440 unsigned int caps_count;
441 unsigned int hard_reset_count;
443 bool explicit_contract;
444 unsigned int rx_msgid;
447 struct usb_power_delivery **pds;
448 struct pd_data **pd_list;
449 struct usb_power_delivery_capabilities *port_source_caps;
450 struct usb_power_delivery_capabilities *port_sink_caps;
451 struct usb_power_delivery *partner_pd;
452 struct usb_power_delivery_capabilities *partner_source_caps;
453 struct usb_power_delivery_capabilities *partner_sink_caps;
454 struct usb_power_delivery *selected_pd;
456 /* Partner capabilities/requests */
458 u32 source_caps[PDO_MAX_OBJECTS];
459 unsigned int nr_source_caps;
460 u32 sink_caps[PDO_MAX_OBJECTS];
461 unsigned int nr_sink_caps;
463 /* Local capabilities */
464 unsigned int pd_count;
465 u32 src_pdo[PDO_MAX_OBJECTS];
466 unsigned int nr_src_pdo;
467 u32 snk_pdo[PDO_MAX_OBJECTS];
468 unsigned int nr_snk_pdo;
469 u32 snk_vdo_v1[VDO_MAX_OBJECTS];
470 unsigned int nr_snk_vdo_v1;
471 u32 snk_vdo[VDO_MAX_OBJECTS];
472 unsigned int nr_snk_vdo;
474 unsigned int operating_snk_mw;
475 bool update_sink_caps;
477 /* Requested current / voltage to the port partner */
478 u32 req_current_limit;
479 u32 req_supply_voltage;
480 /* Actual current / voltage limit of the local port */
484 /* Used to export TA voltage and current */
485 struct power_supply *psy;
486 struct power_supply_desc psy_desc;
487 enum power_supply_usb_type usb_type;
491 /* PD state for Vendor Defined Messages */
492 enum vdm_states vdm_state;
494 /* next Vendor Defined Message to send */
495 u32 vdo_data[VDO_MAX_SIZE];
497 /* VDO to retry if UFP responder replied busy */
501 struct pd_pps_data pps_data;
502 struct completion pps_complete;
506 /* Alternate mode data */
507 struct pd_mode_data mode_data;
508 struct pd_mode_data mode_data_prime;
509 struct typec_altmode *partner_altmode[ALTMODE_DISCOVERY_MAX];
510 struct typec_altmode *plug_prime_altmode[ALTMODE_DISCOVERY_MAX];
511 struct typec_altmode *port_altmode[ALTMODE_DISCOVERY_MAX];
513 /* Deadline in jiffies to exit src_try_wait state */
514 unsigned long max_wait;
516 /* port belongs to a self powered device */
520 enum frs_typec_current new_source_frs_current;
522 /* Sink caps have been queried */
525 /* Collision Avoidance and Atomic Message Sequence */
526 enum tcpm_state upcoming_state;
528 enum tcpm_ams next_ams;
531 /* Auto vbus discharge status */
532 bool auto_vbus_discharge_enabled;
535 * When set, port requests PD_P_SNK_STDBY_MW upon entering SNK_DISCOVERY and
536 * the actual current limit after RX of PD_CTRL_PSRDY for PD link,
537 * SNK_READY for non-pd link.
539 bool slow_charger_loop;
542 * When true indicates that the lower level drivers indicate potential presence
543 * of contaminant in the connector pins based on the tcpm state machine
546 bool potential_contaminant;
548 /* SOP* Related Fields */
550 * Flag to determine if SOP' Discover Identity is available. The flag
551 * is set if Discover Identity on SOP' does not immediately follow
552 * Discover Identity on SOP.
554 bool send_discover_prime;
556 * tx_sop_type determines which SOP* a message is being sent on.
557 * For messages that are queued and not sent immediately such as in
558 * tcpm_queue_message or messages that send after state changes,
559 * the tx_sop_type is set accordingly.
561 enum tcpm_transmit_type tx_sop_type;
563 * Prior to discovering the port partner's Specification Revision, the
564 * Vconn source and cable plug will use the lower of their two revisions.
566 * When the port partner's Specification Revision is discovered, the following
567 * rules are put in place.
568 * 1. If the cable revision (1) is lower than the revision negotiated
569 * between the port and partner (2), the port and partner will communicate
570 * on revision (2), but the port and cable will communicate on revision (1).
571 * 2. If the cable revision (1) is higher than the revision negotiated
572 * between the port and partner (2), the port and partner will communicate
573 * on revision (2), and the port and cable will communicate on revision (2)
576 unsigned int negotiated_rev_prime;
578 * Each SOP* type must maintain their own tx and rx message IDs
580 unsigned int message_id_prime;
581 unsigned int rx_msgid_prime;
583 /* Timer deadline values configured at runtime */
584 struct pd_timings timings;
586 /* Indicates maximum (revision, version) supported */
587 struct pd_revision_info pd_rev;
588 #ifdef CONFIG_DEBUG_FS
589 struct dentry *dentry;
590 struct mutex logbuffer_lock; /* log buffer access lock */
593 u8 *logbuffer[LOG_BUFFER_ENTRIES];
598 struct kthread_work work;
599 struct tcpm_port *port;
600 struct pd_message msg;
601 enum tcpm_transmit_type rx_sop_type;
604 struct altmode_vdm_event {
605 struct kthread_work work;
606 struct tcpm_port *port;
610 enum tcpm_transmit_type tx_sop_type;
613 static const char * const pd_rev[] = {
619 #define tcpm_cc_is_sink(cc) \
620 ((cc) == TYPEC_CC_RP_DEF || (cc) == TYPEC_CC_RP_1_5 || \
621 (cc) == TYPEC_CC_RP_3_0)
623 /* As long as cc is pulled up, we can consider it as sink. */
624 #define tcpm_port_is_sink(port) \
625 (tcpm_cc_is_sink((port)->cc1) || tcpm_cc_is_sink((port)->cc2))
627 #define tcpm_cc_is_source(cc) ((cc) == TYPEC_CC_RD)
628 #define tcpm_cc_is_audio(cc) ((cc) == TYPEC_CC_RA)
629 #define tcpm_cc_is_open(cc) ((cc) == TYPEC_CC_OPEN)
631 #define tcpm_port_is_source(port) \
632 ((tcpm_cc_is_source((port)->cc1) && \
633 !tcpm_cc_is_source((port)->cc2)) || \
634 (tcpm_cc_is_source((port)->cc2) && \
635 !tcpm_cc_is_source((port)->cc1)))
637 #define tcpm_port_is_debug(port) \
638 ((tcpm_cc_is_source((port)->cc1) && tcpm_cc_is_source((port)->cc2)) || \
639 (tcpm_cc_is_sink((port)->cc1) && tcpm_cc_is_sink((port)->cc2)))
641 #define tcpm_port_is_audio(port) \
642 (tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_audio((port)->cc2))
644 #define tcpm_port_is_audio_detached(port) \
645 ((tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_open((port)->cc2)) || \
646 (tcpm_cc_is_audio((port)->cc2) && tcpm_cc_is_open((port)->cc1)))
648 #define tcpm_try_snk(port) \
649 ((port)->try_snk_count == 0 && (port)->try_role == TYPEC_SINK && \
650 (port)->port_type == TYPEC_PORT_DRP)
652 #define tcpm_try_src(port) \
653 ((port)->try_src_count == 0 && (port)->try_role == TYPEC_SOURCE && \
654 (port)->port_type == TYPEC_PORT_DRP)
656 #define tcpm_data_role_for_source(port) \
657 ((port)->typec_caps.data == TYPEC_PORT_UFP ? \
658 TYPEC_DEVICE : TYPEC_HOST)
660 #define tcpm_data_role_for_sink(port) \
661 ((port)->typec_caps.data == TYPEC_PORT_DFP ? \
662 TYPEC_HOST : TYPEC_DEVICE)
664 #define tcpm_sink_tx_ok(port) \
665 (tcpm_port_is_sink(port) && \
666 ((port)->cc1 == TYPEC_CC_RP_3_0 || (port)->cc2 == TYPEC_CC_RP_3_0))
668 #define tcpm_wait_for_discharge(port) \
669 (((port)->auto_vbus_discharge_enabled && !(port)->vbus_vsafe0v) ? PD_T_SAFE_0V : 0)
671 static enum tcpm_state tcpm_default_state(struct tcpm_port *port)
673 if (port->port_type == TYPEC_PORT_DRP) {
674 if (port->try_role == TYPEC_SINK)
675 return SNK_UNATTACHED;
676 else if (port->try_role == TYPEC_SOURCE)
677 return SRC_UNATTACHED;
678 /* Fall through to return SRC_UNATTACHED */
679 } else if (port->port_type == TYPEC_PORT_SNK) {
680 return SNK_UNATTACHED;
682 return SRC_UNATTACHED;
685 static bool tcpm_port_is_disconnected(struct tcpm_port *port)
687 return (!port->attached && port->cc1 == TYPEC_CC_OPEN &&
688 port->cc2 == TYPEC_CC_OPEN) ||
689 (port->attached && ((port->polarity == TYPEC_POLARITY_CC1 &&
690 port->cc1 == TYPEC_CC_OPEN) ||
691 (port->polarity == TYPEC_POLARITY_CC2 &&
692 port->cc2 == TYPEC_CC_OPEN)));
699 #ifdef CONFIG_DEBUG_FS
701 static bool tcpm_log_full(struct tcpm_port *port)
703 return port->logbuffer_tail ==
704 (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
708 static void _tcpm_log(struct tcpm_port *port, const char *fmt, va_list args)
710 char tmpbuffer[LOG_BUFFER_ENTRY_SIZE];
711 u64 ts_nsec = local_clock();
712 unsigned long rem_nsec;
714 mutex_lock(&port->logbuffer_lock);
715 if (!port->logbuffer[port->logbuffer_head]) {
716 port->logbuffer[port->logbuffer_head] =
717 kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL);
718 if (!port->logbuffer[port->logbuffer_head]) {
719 mutex_unlock(&port->logbuffer_lock);
724 vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args);
726 if (tcpm_log_full(port)) {
727 port->logbuffer_head = max(port->logbuffer_head - 1, 0);
728 strcpy(tmpbuffer, "overflow");
731 if (port->logbuffer_head < 0 ||
732 port->logbuffer_head >= LOG_BUFFER_ENTRIES) {
734 "Bad log buffer index %d\n", port->logbuffer_head);
738 if (!port->logbuffer[port->logbuffer_head]) {
740 "Log buffer index %d is NULL\n", port->logbuffer_head);
744 rem_nsec = do_div(ts_nsec, 1000000000);
745 scnprintf(port->logbuffer[port->logbuffer_head],
746 LOG_BUFFER_ENTRY_SIZE, "[%5lu.%06lu] %s",
747 (unsigned long)ts_nsec, rem_nsec / 1000,
749 port->logbuffer_head = (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
752 mutex_unlock(&port->logbuffer_lock);
756 static void tcpm_log(struct tcpm_port *port, const char *fmt, ...)
760 /* Do not log while disconnected and unattached */
761 if (tcpm_port_is_disconnected(port) &&
762 (port->state == SRC_UNATTACHED || port->state == SNK_UNATTACHED ||
763 port->state == TOGGLING || port->state == CHECK_CONTAMINANT))
767 _tcpm_log(port, fmt, args);
772 static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...)
777 _tcpm_log(port, fmt, args);
781 static void tcpm_log_source_caps(struct tcpm_port *port)
785 for (i = 0; i < port->nr_source_caps; i++) {
786 u32 pdo = port->source_caps[i];
787 enum pd_pdo_type type = pdo_type(pdo);
792 scnprintf(msg, sizeof(msg),
793 "%u mV, %u mA [%s%s%s%s%s%s]",
794 pdo_fixed_voltage(pdo),
795 pdo_max_current(pdo),
796 (pdo & PDO_FIXED_DUAL_ROLE) ?
798 (pdo & PDO_FIXED_SUSPEND) ?
800 (pdo & PDO_FIXED_HIGHER_CAP) ?
802 (pdo & PDO_FIXED_USB_COMM) ?
804 (pdo & PDO_FIXED_DATA_SWAP) ?
806 (pdo & PDO_FIXED_EXTPOWER) ?
810 scnprintf(msg, sizeof(msg),
812 pdo_min_voltage(pdo),
813 pdo_max_voltage(pdo),
814 pdo_max_current(pdo));
817 scnprintf(msg, sizeof(msg),
819 pdo_min_voltage(pdo),
820 pdo_max_voltage(pdo),
824 if (pdo_apdo_type(pdo) == APDO_TYPE_PPS)
825 scnprintf(msg, sizeof(msg),
827 pdo_pps_apdo_min_voltage(pdo),
828 pdo_pps_apdo_max_voltage(pdo),
829 pdo_pps_apdo_max_current(pdo));
831 strcpy(msg, "undefined APDO");
834 strcpy(msg, "undefined");
837 tcpm_log(port, " PDO %d: type %d, %s",
842 static int tcpm_debug_show(struct seq_file *s, void *v)
844 struct tcpm_port *port = s->private;
847 mutex_lock(&port->logbuffer_lock);
848 tail = port->logbuffer_tail;
849 while (tail != port->logbuffer_head) {
850 seq_printf(s, "%s\n", port->logbuffer[tail]);
851 tail = (tail + 1) % LOG_BUFFER_ENTRIES;
853 if (!seq_has_overflowed(s))
854 port->logbuffer_tail = tail;
855 mutex_unlock(&port->logbuffer_lock);
859 DEFINE_SHOW_ATTRIBUTE(tcpm_debug);
861 static void tcpm_debugfs_init(struct tcpm_port *port)
865 mutex_init(&port->logbuffer_lock);
866 snprintf(name, NAME_MAX, "tcpm-%s", dev_name(port->dev));
867 port->dentry = debugfs_create_dir(name, usb_debug_root);
868 debugfs_create_file("log", S_IFREG | 0444, port->dentry, port,
872 static void tcpm_debugfs_exit(struct tcpm_port *port)
876 mutex_lock(&port->logbuffer_lock);
877 for (i = 0; i < LOG_BUFFER_ENTRIES; i++) {
878 kfree(port->logbuffer[i]);
879 port->logbuffer[i] = NULL;
881 mutex_unlock(&port->logbuffer_lock);
883 debugfs_remove(port->dentry);
889 static void tcpm_log(const struct tcpm_port *port, const char *fmt, ...) { }
891 static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...) { }
892 static void tcpm_log_source_caps(struct tcpm_port *port) { }
893 static void tcpm_debugfs_init(const struct tcpm_port *port) { }
894 static void tcpm_debugfs_exit(const struct tcpm_port *port) { }
898 static void tcpm_set_cc(struct tcpm_port *port, enum typec_cc_status cc)
900 tcpm_log(port, "cc:=%d", cc);
902 port->tcpc->set_cc(port->tcpc, cc);
905 static int tcpm_enable_auto_vbus_discharge(struct tcpm_port *port, bool enable)
909 if (port->tcpc->enable_auto_vbus_discharge) {
910 ret = port->tcpc->enable_auto_vbus_discharge(port->tcpc, enable);
911 tcpm_log_force(port, "%s vbus discharge ret:%d",
912 str_enable_disable(enable), ret);
914 port->auto_vbus_discharge_enabled = enable;
920 static void tcpm_apply_rc(struct tcpm_port *port)
923 * TCPCI: Move to APPLY_RC state to prevent disconnect during PR_SWAP
924 * when Vbus auto discharge on disconnect is enabled.
926 if (port->tcpc->enable_auto_vbus_discharge && port->tcpc->apply_rc) {
927 tcpm_log(port, "Apply_RC");
928 port->tcpc->apply_rc(port->tcpc, port->cc_req, port->polarity);
929 tcpm_enable_auto_vbus_discharge(port, false);
934 * Determine RP value to set based on maximum current supported
935 * by a port if configured as source.
936 * Returns CC value to report to link partner.
938 static enum typec_cc_status tcpm_rp_cc(struct tcpm_port *port)
940 const u32 *src_pdo = port->src_pdo;
941 int nr_pdo = port->nr_src_pdo;
944 if (!port->pd_supported)
948 * Search for first entry with matching voltage.
949 * It should report the maximum supported current.
951 for (i = 0; i < nr_pdo; i++) {
952 const u32 pdo = src_pdo[i];
954 if (pdo_type(pdo) == PDO_TYPE_FIXED &&
955 pdo_fixed_voltage(pdo) == 5000) {
956 unsigned int curr = pdo_max_current(pdo);
959 return TYPEC_CC_RP_3_0;
960 else if (curr >= 1500)
961 return TYPEC_CC_RP_1_5;
962 return TYPEC_CC_RP_DEF;
966 return TYPEC_CC_RP_DEF;
969 static void tcpm_ams_finish(struct tcpm_port *port)
971 tcpm_log(port, "AMS %s finished", tcpm_ams_str[port->ams]);
973 if (port->pd_capable && port->pwr_role == TYPEC_SOURCE) {
974 if (port->negotiated_rev >= PD_REV30)
975 tcpm_set_cc(port, SINK_TX_OK);
977 tcpm_set_cc(port, SINK_TX_NG);
978 } else if (port->pwr_role == TYPEC_SOURCE) {
979 tcpm_set_cc(port, tcpm_rp_cc(port));
982 port->in_ams = false;
983 port->ams = NONE_AMS;
986 static int tcpm_pd_transmit(struct tcpm_port *port,
987 enum tcpm_transmit_type tx_sop_type,
988 const struct pd_message *msg)
990 unsigned long time_left;
992 unsigned int negotiated_rev;
994 switch (tx_sop_type) {
995 case TCPC_TX_SOP_PRIME:
996 negotiated_rev = port->negotiated_rev_prime;
1000 negotiated_rev = port->negotiated_rev;
1005 tcpm_log(port, "PD TX, header: %#x", le16_to_cpu(msg->header));
1007 tcpm_log(port, "PD TX, type: %#x", tx_sop_type);
1009 reinit_completion(&port->tx_complete);
1010 ret = port->tcpc->pd_transmit(port->tcpc, tx_sop_type, msg, negotiated_rev);
1014 mutex_unlock(&port->lock);
1015 time_left = wait_for_completion_timeout(&port->tx_complete,
1016 msecs_to_jiffies(PD_T_TCPC_TX_TIMEOUT));
1017 mutex_lock(&port->lock);
1021 switch (port->tx_status) {
1022 case TCPC_TX_SUCCESS:
1023 switch (tx_sop_type) {
1024 case TCPC_TX_SOP_PRIME:
1025 port->message_id_prime = (port->message_id_prime + 1) &
1030 port->message_id = (port->message_id + 1) &
1035 * USB PD rev 2.0, 8.3.2.2.1:
1036 * USB PD rev 3.0, 8.3.2.1.3:
1037 * "... Note that every AMS is Interruptible until the first
1038 * Message in the sequence has been successfully sent (GoodCRC
1039 * Message received)."
1041 if (port->ams != NONE_AMS)
1042 port->in_ams = true;
1044 case TCPC_TX_DISCARDED:
1047 case TCPC_TX_FAILED:
1053 /* Some AMS don't expect responses. Finish them here. */
1054 if (port->ams == ATTENTION || port->ams == SOURCE_ALERT)
1055 tcpm_ams_finish(port);
1060 void tcpm_pd_transmit_complete(struct tcpm_port *port,
1061 enum tcpm_transmit_status status)
1063 tcpm_log(port, "PD TX complete, status: %u", status);
1064 port->tx_status = status;
1065 complete(&port->tx_complete);
1067 EXPORT_SYMBOL_GPL(tcpm_pd_transmit_complete);
1069 static int tcpm_mux_set(struct tcpm_port *port, int state,
1070 enum usb_role usb_role,
1071 enum typec_orientation orientation)
1075 tcpm_log(port, "Requesting mux state %d, usb-role %d, orientation %d",
1076 state, usb_role, orientation);
1078 ret = typec_set_orientation(port->typec_port, orientation);
1082 if (port->role_sw) {
1083 ret = usb_role_switch_set_role(port->role_sw, usb_role);
1088 return typec_set_mode(port->typec_port, state);
1091 static int tcpm_set_polarity(struct tcpm_port *port,
1092 enum typec_cc_polarity polarity)
1096 tcpm_log(port, "polarity %d", polarity);
1098 ret = port->tcpc->set_polarity(port->tcpc, polarity);
1102 port->polarity = polarity;
1107 static int tcpm_set_vconn(struct tcpm_port *port, bool enable)
1111 tcpm_log(port, "vconn:=%d", enable);
1113 ret = port->tcpc->set_vconn(port->tcpc, enable);
1115 port->vconn_role = enable ? TYPEC_SOURCE : TYPEC_SINK;
1116 typec_set_vconn_role(port->typec_port, port->vconn_role);
1122 static u32 tcpm_get_current_limit(struct tcpm_port *port)
1124 enum typec_cc_status cc;
1127 cc = port->polarity ? port->cc2 : port->cc1;
1129 case TYPEC_CC_RP_1_5:
1132 case TYPEC_CC_RP_3_0:
1135 case TYPEC_CC_RP_DEF:
1137 if (port->tcpc->get_current_limit)
1138 limit = port->tcpc->get_current_limit(port->tcpc);
1147 static int tcpm_set_current_limit(struct tcpm_port *port, u32 max_ma, u32 mv)
1149 int ret = -EOPNOTSUPP;
1151 tcpm_log(port, "Setting voltage/current limit %u mV %u mA", mv, max_ma);
1153 port->supply_voltage = mv;
1154 port->current_limit = max_ma;
1155 power_supply_changed(port->psy);
1157 if (port->tcpc->set_current_limit)
1158 ret = port->tcpc->set_current_limit(port->tcpc, max_ma, mv);
1163 static int tcpm_set_attached_state(struct tcpm_port *port, bool attached)
1165 return port->tcpc->set_roles(port->tcpc, attached, port->pwr_role,
1169 static int tcpm_set_roles(struct tcpm_port *port, bool attached, int state,
1170 enum typec_role role, enum typec_data_role data)
1172 enum typec_orientation orientation;
1173 enum usb_role usb_role;
1176 if (port->polarity == TYPEC_POLARITY_CC1)
1177 orientation = TYPEC_ORIENTATION_NORMAL;
1179 orientation = TYPEC_ORIENTATION_REVERSE;
1181 if (port->typec_caps.data == TYPEC_PORT_DRD) {
1182 if (data == TYPEC_HOST)
1183 usb_role = USB_ROLE_HOST;
1185 usb_role = USB_ROLE_DEVICE;
1186 } else if (port->typec_caps.data == TYPEC_PORT_DFP) {
1187 if (data == TYPEC_HOST) {
1188 if (role == TYPEC_SOURCE)
1189 usb_role = USB_ROLE_HOST;
1191 usb_role = USB_ROLE_NONE;
1196 if (data == TYPEC_DEVICE) {
1197 if (role == TYPEC_SINK)
1198 usb_role = USB_ROLE_DEVICE;
1200 usb_role = USB_ROLE_NONE;
1206 ret = tcpm_mux_set(port, state, usb_role, orientation);
1210 ret = port->tcpc->set_roles(port->tcpc, attached, role, data);
1214 if (port->tcpc->set_orientation) {
1215 ret = port->tcpc->set_orientation(port->tcpc, orientation);
1220 port->pwr_role = role;
1221 port->data_role = data;
1222 typec_set_data_role(port->typec_port, data);
1223 typec_set_pwr_role(port->typec_port, role);
1228 static int tcpm_set_pwr_role(struct tcpm_port *port, enum typec_role role)
1232 ret = port->tcpc->set_roles(port->tcpc, true, role,
1237 port->pwr_role = role;
1238 typec_set_pwr_role(port->typec_port, role);
1244 * Transform the PDO to be compliant to PD rev2.0.
1245 * Return 0 if the PDO type is not defined in PD rev2.0.
1246 * Otherwise, return the converted PDO.
1248 static u32 tcpm_forge_legacy_pdo(struct tcpm_port *port, u32 pdo, enum typec_role role)
1250 switch (pdo_type(pdo)) {
1251 case PDO_TYPE_FIXED:
1252 if (role == TYPEC_SINK)
1253 return pdo & ~PDO_FIXED_FRS_CURR_MASK;
1255 return pdo & ~PDO_FIXED_UNCHUNK_EXT;
1265 static int tcpm_pd_send_revision(struct tcpm_port *port)
1267 struct pd_message msg;
1270 memset(&msg, 0, sizeof(msg));
1271 rmdo = RMDO(port->pd_rev.rev_major, port->pd_rev.rev_minor,
1272 port->pd_rev.ver_major, port->pd_rev.ver_minor);
1273 msg.payload[0] = cpu_to_le32(rmdo);
1274 msg.header = PD_HEADER_LE(PD_DATA_REVISION,
1277 port->negotiated_rev,
1280 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
1283 static int tcpm_pd_send_source_caps(struct tcpm_port *port)
1285 struct pd_message msg;
1287 unsigned int i, nr_pdo = 0;
1289 memset(&msg, 0, sizeof(msg));
1291 for (i = 0; i < port->nr_src_pdo; i++) {
1292 if (port->negotiated_rev >= PD_REV30) {
1293 msg.payload[nr_pdo++] = cpu_to_le32(port->src_pdo[i]);
1295 pdo = tcpm_forge_legacy_pdo(port, port->src_pdo[i], TYPEC_SOURCE);
1297 msg.payload[nr_pdo++] = cpu_to_le32(pdo);
1302 /* No source capabilities defined, sink only */
1303 msg.header = PD_HEADER_LE(PD_CTRL_REJECT,
1306 port->negotiated_rev,
1307 port->message_id, 0);
1309 msg.header = PD_HEADER_LE(PD_DATA_SOURCE_CAP,
1312 port->negotiated_rev,
1317 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
1320 static int tcpm_pd_send_sink_caps(struct tcpm_port *port)
1322 struct pd_message msg;
1324 unsigned int i, nr_pdo = 0;
1326 memset(&msg, 0, sizeof(msg));
1328 for (i = 0; i < port->nr_snk_pdo; i++) {
1329 if (port->negotiated_rev >= PD_REV30) {
1330 msg.payload[nr_pdo++] = cpu_to_le32(port->snk_pdo[i]);
1332 pdo = tcpm_forge_legacy_pdo(port, port->snk_pdo[i], TYPEC_SINK);
1334 msg.payload[nr_pdo++] = cpu_to_le32(pdo);
1339 /* No sink capabilities defined, source only */
1340 msg.header = PD_HEADER_LE(PD_CTRL_REJECT,
1343 port->negotiated_rev,
1344 port->message_id, 0);
1346 msg.header = PD_HEADER_LE(PD_DATA_SINK_CAP,
1349 port->negotiated_rev,
1354 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
1357 static void mod_tcpm_delayed_work(struct tcpm_port *port, unsigned int delay_ms)
1360 hrtimer_start(&port->state_machine_timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL);
1362 hrtimer_cancel(&port->state_machine_timer);
1363 kthread_queue_work(port->wq, &port->state_machine);
1367 static void mod_vdm_delayed_work(struct tcpm_port *port, unsigned int delay_ms)
1370 hrtimer_start(&port->vdm_state_machine_timer, ms_to_ktime(delay_ms),
1373 hrtimer_cancel(&port->vdm_state_machine_timer);
1374 kthread_queue_work(port->wq, &port->vdm_state_machine);
1378 static void mod_enable_frs_delayed_work(struct tcpm_port *port, unsigned int delay_ms)
1381 hrtimer_start(&port->enable_frs_timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL);
1383 hrtimer_cancel(&port->enable_frs_timer);
1384 kthread_queue_work(port->wq, &port->enable_frs);
1388 static void mod_send_discover_delayed_work(struct tcpm_port *port, unsigned int delay_ms)
1391 hrtimer_start(&port->send_discover_timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL);
1393 hrtimer_cancel(&port->send_discover_timer);
1394 kthread_queue_work(port->wq, &port->send_discover_work);
1398 static void tcpm_set_state(struct tcpm_port *port, enum tcpm_state state,
1399 unsigned int delay_ms)
1402 tcpm_log(port, "pending state change %s -> %s @ %u ms [%s %s]",
1403 tcpm_states[port->state], tcpm_states[state], delay_ms,
1404 pd_rev[port->negotiated_rev], tcpm_ams_str[port->ams]);
1405 port->delayed_state = state;
1406 mod_tcpm_delayed_work(port, delay_ms);
1407 port->delayed_runtime = ktime_add(ktime_get(), ms_to_ktime(delay_ms));
1408 port->delay_ms = delay_ms;
1410 tcpm_log(port, "state change %s -> %s [%s %s]",
1411 tcpm_states[port->state], tcpm_states[state],
1412 pd_rev[port->negotiated_rev], tcpm_ams_str[port->ams]);
1413 port->delayed_state = INVALID_STATE;
1414 port->prev_state = port->state;
1415 port->state = state;
1417 * Don't re-queue the state machine work item if we're currently
1418 * in the state machine and we're immediately changing states.
1419 * tcpm_state_machine_work() will continue running the state
1422 if (!port->state_machine_running)
1423 mod_tcpm_delayed_work(port, 0);
1427 static void tcpm_set_state_cond(struct tcpm_port *port, enum tcpm_state state,
1428 unsigned int delay_ms)
1430 if (port->enter_state == port->state)
1431 tcpm_set_state(port, state, delay_ms);
1434 "skipped %sstate change %s -> %s [%u ms], context state %s [%s %s]",
1435 delay_ms ? "delayed " : "",
1436 tcpm_states[port->state], tcpm_states[state],
1437 delay_ms, tcpm_states[port->enter_state],
1438 pd_rev[port->negotiated_rev], tcpm_ams_str[port->ams]);
1441 static void tcpm_queue_message(struct tcpm_port *port,
1442 enum pd_msg_request message)
1444 port->queued_message = message;
1445 mod_tcpm_delayed_work(port, 0);
1448 static bool tcpm_vdm_ams(struct tcpm_port *port)
1450 switch (port->ams) {
1451 case DISCOVER_IDENTITY:
1452 case SOURCE_STARTUP_CABLE_PLUG_DISCOVER_IDENTITY:
1453 case DISCOVER_SVIDS:
1454 case DISCOVER_MODES:
1455 case DFP_TO_UFP_ENTER_MODE:
1456 case DFP_TO_UFP_EXIT_MODE:
1457 case DFP_TO_CABLE_PLUG_ENTER_MODE:
1458 case DFP_TO_CABLE_PLUG_EXIT_MODE:
1460 case UNSTRUCTURED_VDMS:
1461 case STRUCTURED_VDMS:
1470 static bool tcpm_ams_interruptible(struct tcpm_port *port)
1472 switch (port->ams) {
1473 /* Interruptible AMS */
1476 case FIRMWARE_UPDATE:
1477 case DISCOVER_IDENTITY:
1478 case SOURCE_STARTUP_CABLE_PLUG_DISCOVER_IDENTITY:
1479 case DISCOVER_SVIDS:
1480 case DISCOVER_MODES:
1481 case DFP_TO_UFP_ENTER_MODE:
1482 case DFP_TO_UFP_EXIT_MODE:
1483 case DFP_TO_CABLE_PLUG_ENTER_MODE:
1484 case DFP_TO_CABLE_PLUG_EXIT_MODE:
1485 case UNSTRUCTURED_VDMS:
1486 case STRUCTURED_VDMS:
1490 /* Non-Interruptible AMS */
1500 static int tcpm_ams_start(struct tcpm_port *port, enum tcpm_ams ams)
1504 tcpm_log(port, "AMS %s start", tcpm_ams_str[ams]);
1506 if (!tcpm_ams_interruptible(port) &&
1507 !(ams == HARD_RESET || ams == SOFT_RESET_AMS)) {
1508 port->upcoming_state = INVALID_STATE;
1509 tcpm_log(port, "AMS %s not interruptible, aborting",
1510 tcpm_ams_str[port->ams]);
1514 if (port->pwr_role == TYPEC_SOURCE) {
1515 enum typec_cc_status cc_req = port->cc_req;
1519 if (ams == HARD_RESET) {
1520 tcpm_set_cc(port, tcpm_rp_cc(port));
1521 tcpm_pd_transmit(port, TCPC_TX_HARD_RESET, NULL);
1522 tcpm_set_state(port, HARD_RESET_START, 0);
1524 } else if (ams == SOFT_RESET_AMS) {
1525 if (!port->explicit_contract)
1526 tcpm_set_cc(port, tcpm_rp_cc(port));
1527 tcpm_set_state(port, SOFT_RESET_SEND, 0);
1529 } else if (tcpm_vdm_ams(port)) {
1530 /* tSinkTx is enforced in vdm_run_state_machine */
1531 if (port->negotiated_rev >= PD_REV30)
1532 tcpm_set_cc(port, SINK_TX_NG);
1536 if (port->negotiated_rev >= PD_REV30)
1537 tcpm_set_cc(port, SINK_TX_NG);
1539 switch (port->state) {
1542 case SRC_SOFT_RESET_WAIT_SNK_TX:
1544 case SOFT_RESET_SEND:
1545 if (port->negotiated_rev >= PD_REV30)
1546 tcpm_set_state(port, AMS_START,
1547 cc_req == SINK_TX_OK ?
1550 tcpm_set_state(port, AMS_START, 0);
1553 if (port->negotiated_rev >= PD_REV30)
1554 tcpm_set_state(port, SRC_READY,
1555 cc_req == SINK_TX_OK ?
1558 tcpm_set_state(port, SRC_READY, 0);
1562 if (port->negotiated_rev >= PD_REV30 &&
1563 !tcpm_sink_tx_ok(port) &&
1564 ams != SOFT_RESET_AMS &&
1565 ams != HARD_RESET) {
1566 port->upcoming_state = INVALID_STATE;
1567 tcpm_log(port, "Sink TX No Go");
1573 if (ams == HARD_RESET) {
1574 tcpm_pd_transmit(port, TCPC_TX_HARD_RESET, NULL);
1575 tcpm_set_state(port, HARD_RESET_START, 0);
1577 } else if (tcpm_vdm_ams(port)) {
1581 if (port->state == SNK_READY ||
1582 port->state == SNK_SOFT_RESET)
1583 tcpm_set_state(port, AMS_START, 0);
1585 tcpm_set_state(port, SNK_READY, 0);
1592 * VDM/VDO handling functions
1594 static void tcpm_queue_vdm(struct tcpm_port *port, const u32 header,
1595 const u32 *data, int cnt, enum tcpm_transmit_type tx_sop_type)
1597 u32 vdo_hdr = port->vdo_data[0];
1599 WARN_ON(!mutex_is_locked(&port->lock));
1601 /* If is sending discover_identity, handle received message first */
1602 if (PD_VDO_SVDM(vdo_hdr) && PD_VDO_CMD(vdo_hdr) == CMD_DISCOVER_IDENT) {
1603 if (tx_sop_type == TCPC_TX_SOP_PRIME)
1604 port->send_discover_prime = true;
1606 port->send_discover = true;
1607 mod_send_discover_delayed_work(port, SEND_DISCOVER_RETRY_MS);
1609 /* Make sure we are not still processing a previous VDM packet */
1610 WARN_ON(port->vdm_state > VDM_STATE_DONE);
1613 port->vdo_count = cnt + 1;
1614 port->vdo_data[0] = header;
1615 memcpy(&port->vdo_data[1], data, sizeof(u32) * cnt);
1616 /* Set ready, vdm state machine will actually send */
1617 port->vdm_retries = 0;
1618 port->vdm_state = VDM_STATE_READY;
1619 port->vdm_sm_running = true;
1621 port->tx_sop_type = tx_sop_type;
1623 mod_vdm_delayed_work(port, 0);
1626 static void tcpm_queue_vdm_work(struct kthread_work *work)
1628 struct altmode_vdm_event *event = container_of(work,
1629 struct altmode_vdm_event,
1631 struct tcpm_port *port = event->port;
1633 mutex_lock(&port->lock);
1634 if (port->state != SRC_READY && port->state != SNK_READY &&
1635 port->state != SRC_VDM_IDENTITY_REQUEST) {
1636 tcpm_log_force(port, "dropping altmode_vdm_event");
1640 tcpm_queue_vdm(port, event->header, event->data, event->cnt, event->tx_sop_type);
1645 mutex_unlock(&port->lock);
1648 static int tcpm_queue_vdm_unlocked(struct tcpm_port *port, const u32 header,
1649 const u32 *data, int cnt, enum tcpm_transmit_type tx_sop_type)
1651 struct altmode_vdm_event *event;
1655 event = kzalloc(sizeof(*event), GFP_KERNEL);
1659 data_cpy = kcalloc(cnt, sizeof(u32), GFP_KERNEL);
1663 kthread_init_work(&event->work, tcpm_queue_vdm_work);
1665 event->header = header;
1666 memcpy(data_cpy, data, sizeof(u32) * cnt);
1667 event->data = data_cpy;
1669 event->tx_sop_type = tx_sop_type;
1671 ret = kthread_queue_work(port->wq, &event->work);
1684 tcpm_log_force(port, "failed to queue altmode vdm, err:%d", ret);
1688 static void svdm_consume_identity(struct tcpm_port *port, const u32 *p, int cnt)
1690 u32 vdo = p[VDO_INDEX_IDH];
1691 u32 product = p[VDO_INDEX_PRODUCT];
1693 memset(&port->mode_data, 0, sizeof(port->mode_data));
1695 port->partner_ident.id_header = vdo;
1696 port->partner_ident.cert_stat = p[VDO_INDEX_CSTAT];
1697 port->partner_ident.product = product;
1700 typec_partner_set_identity(port->partner);
1702 tcpm_log(port, "Identity: %04x:%04x.%04x",
1704 PD_PRODUCT_PID(product), product & 0xffff);
1707 static void svdm_consume_identity_sop_prime(struct tcpm_port *port, const u32 *p, int cnt)
1709 u32 idh = p[VDO_INDEX_IDH];
1710 u32 product = p[VDO_INDEX_PRODUCT];
1714 * Attempt to consume identity only if cable currently is not set
1716 if (!IS_ERR_OR_NULL(port->cable))
1719 /* Reset cable identity */
1720 memset(&port->cable_ident, 0, sizeof(port->cable_ident));
1722 /* Fill out id header, cert, product, cable VDO 1 */
1723 port->cable_ident.id_header = idh;
1724 port->cable_ident.cert_stat = p[VDO_INDEX_CSTAT];
1725 port->cable_ident.product = product;
1726 port->cable_ident.vdo[0] = p[VDO_INDEX_CABLE_1];
1728 /* Fill out cable desc, infer svdm_version from pd revision */
1729 port->cable_desc.type = (enum typec_plug_type) (VDO_TYPEC_CABLE_TYPE(p[VDO_INDEX_CABLE_1]) +
1731 port->cable_desc.active = PD_IDH_PTYPE(idh) == IDH_PTYPE_ACABLE ? 1 : 0;
1732 /* Log PD Revision and additional cable VDO from negotiated revision */
1733 switch (port->negotiated_rev_prime) {
1735 port->cable_desc.pd_revision = 0x0300;
1736 if (port->cable_desc.active)
1737 port->cable_ident.vdo[1] = p[VDO_INDEX_CABLE_2];
1740 port->cable_desc.pd_revision = 0x0200;
1743 port->cable_desc.pd_revision = 0x0200;
1746 port->cable_desc.identity = &port->cable_ident;
1747 /* Register Cable, set identity and svdm_version */
1748 port->cable = typec_register_cable(port->typec_port, &port->cable_desc);
1749 if (IS_ERR_OR_NULL(port->cable))
1751 typec_cable_set_identity(port->cable);
1752 /* Get SVDM version */
1753 svdm_version = PD_VDO_SVDM_VER(p[VDO_INDEX_HDR]);
1754 typec_cable_set_svdm_version(port->cable, svdm_version);
1757 if (IS_ERR_OR_NULL(port->plug_prime)) {
1758 port->plug_prime_desc.index = TYPEC_PLUG_SOP_P;
1759 port->plug_prime = typec_register_plug(port->cable,
1760 &port->plug_prime_desc);
1764 static bool svdm_consume_svids(struct tcpm_port *port, const u32 *p, int cnt,
1765 enum tcpm_transmit_type rx_sop_type)
1767 struct pd_mode_data *pmdata = rx_sop_type == TCPC_TX_SOP_PRIME ?
1768 &port->mode_data_prime : &port->mode_data;
1771 for (i = 1; i < cnt; i++) {
1774 svid = (p[i] >> 16) & 0xffff;
1778 if (pmdata->nsvids >= SVID_DISCOVERY_MAX)
1781 pmdata->svids[pmdata->nsvids++] = svid;
1782 tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid);
1784 svid = p[i] & 0xffff;
1788 if (pmdata->nsvids >= SVID_DISCOVERY_MAX)
1791 pmdata->svids[pmdata->nsvids++] = svid;
1792 tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid);
1796 * PD3.0 Spec 6.4.4.3.2: The SVIDs are returned 2 per VDO (see Table
1797 * 6-43), and can be returned maximum 6 VDOs per response (see Figure
1798 * 6-19). If the Respondersupports 12 or more SVID then the Discover
1799 * SVIDs Command Shall be executed multiple times until a Discover
1800 * SVIDs VDO is returned ending either with a SVID value of 0x0000 in
1801 * the last part of the last VDO or with a VDO containing two SVIDs
1802 * with values of 0x0000.
1804 * However, some odd dockers support SVIDs less than 12 but without
1805 * 0x0000 in the last VDO, so we need to break the Discover SVIDs
1806 * request and return false here.
1810 tcpm_log(port, "SVID_DISCOVERY_MAX(%d) too low!", SVID_DISCOVERY_MAX);
1814 static void svdm_consume_modes(struct tcpm_port *port, const u32 *p, int cnt,
1815 enum tcpm_transmit_type rx_sop_type)
1817 struct pd_mode_data *pmdata = &port->mode_data;
1818 struct typec_altmode_desc *paltmode;
1821 switch (rx_sop_type) {
1822 case TCPC_TX_SOP_PRIME:
1823 pmdata = &port->mode_data_prime;
1824 if (pmdata->altmodes >= ARRAY_SIZE(port->plug_prime_altmode)) {
1825 /* Already logged in svdm_consume_svids() */
1830 pmdata = &port->mode_data;
1831 if (pmdata->altmodes >= ARRAY_SIZE(port->partner_altmode)) {
1832 /* Already logged in svdm_consume_svids() */
1840 for (i = 1; i < cnt; i++) {
1841 paltmode = &pmdata->altmode_desc[pmdata->altmodes];
1842 memset(paltmode, 0, sizeof(*paltmode));
1844 paltmode->svid = pmdata->svids[pmdata->svid_index];
1846 paltmode->vdo = p[i];
1848 tcpm_log(port, " Alternate mode %d: SVID 0x%04x, VDO %d: 0x%08x",
1849 pmdata->altmodes, paltmode->svid,
1850 paltmode->mode, paltmode->vdo);
1856 static void tcpm_register_partner_altmodes(struct tcpm_port *port)
1858 struct pd_mode_data *modep = &port->mode_data;
1859 struct typec_altmode *altmode;
1865 for (i = 0; i < modep->altmodes; i++) {
1866 altmode = typec_partner_register_altmode(port->partner,
1867 &modep->altmode_desc[i]);
1868 if (IS_ERR(altmode)) {
1869 tcpm_log(port, "Failed to register partner SVID 0x%04x",
1870 modep->altmode_desc[i].svid);
1873 port->partner_altmode[i] = altmode;
1877 static void tcpm_register_plug_altmodes(struct tcpm_port *port)
1879 struct pd_mode_data *modep = &port->mode_data_prime;
1880 struct typec_altmode *altmode;
1883 typec_plug_set_num_altmodes(port->plug_prime, modep->altmodes);
1885 for (i = 0; i < modep->altmodes; i++) {
1886 altmode = typec_plug_register_altmode(port->plug_prime,
1887 &modep->altmode_desc[i]);
1888 if (IS_ERR(altmode)) {
1889 tcpm_log(port, "Failed to register plug SVID 0x%04x",
1890 modep->altmode_desc[i].svid);
1893 port->plug_prime_altmode[i] = altmode;
1897 #define supports_modal(port) PD_IDH_MODAL_SUPP((port)->partner_ident.id_header)
1898 #define supports_modal_cable(port) PD_IDH_MODAL_SUPP((port)->cable_ident.id_header)
1899 #define supports_host(port) PD_IDH_HOST_SUPP((port->partner_ident.id_header))
1902 * Helper to determine whether the port is capable of SOP' communication at the
1903 * current point in time.
1905 static bool tcpm_can_communicate_sop_prime(struct tcpm_port *port)
1907 /* Check to see if tcpc supports SOP' communication */
1908 if (!port->tcpc->cable_comm_capable || !port->tcpc->cable_comm_capable(port->tcpc))
1911 * Power Delivery 2.0 Section 6.3.11
1912 * Before communicating with a Cable Plug a Port Should ensure that it
1913 * is the Vconn Source and that the Cable Plugs are powered by
1914 * performing a Vconn swap if necessary. Since it cannot be guaranteed
1915 * that the present Vconn Source is supplying Vconn, the only means to
1916 * ensure that the Cable Plugs are powered is for a Port wishing to
1917 * communicate with a Cable Plug is to become the Vconn Source.
1919 * Power Delivery 3.0 Section 6.3.11
1920 * Before communicating with a Cable Plug a Port Shall ensure that it
1921 * is the Vconn source.
1923 if (port->vconn_role != TYPEC_SOURCE)
1926 * Power Delivery 2.0 Section 2.4.4
1927 * When no Contract or an Implicit Contract is in place the Source can
1928 * communicate with a Cable Plug using SOP' packets in order to discover
1929 * its characteristics.
1931 * Power Delivery 3.0 Section 2.4.4
1932 * When no Contract or an Implicit Contract is in place only the Source
1933 * port that is supplying Vconn is allowed to send packets to a Cable
1934 * Plug and is allowed to respond to packets from the Cable Plug.
1936 if (!port->explicit_contract)
1937 return port->pwr_role == TYPEC_SOURCE;
1938 if (port->negotiated_rev == PD_REV30)
1941 * Power Delivery 2.0 Section 2.4.4
1943 * When an Explicit Contract is in place the DFP (either the Source or
1944 * the Sink) can communicate with the Cable Plug(s) using SOP’/SOP”
1945 * Packets (see Figure 2-3).
1947 if (port->negotiated_rev == PD_REV20)
1948 return port->data_role == TYPEC_HOST;
1952 static bool tcpm_attempt_vconn_swap_discovery(struct tcpm_port *port)
1954 if (!port->tcpc->attempt_vconn_swap_discovery)
1957 /* Port is already source, no need to perform swap */
1958 if (port->vconn_role == TYPEC_SOURCE)
1962 * Partner needs to support Alternate Modes with modal support. If
1963 * partner is also capable of being a USB Host, it could be a device
1964 * that supports Alternate Modes as the DFP.
1966 if (!supports_modal(port) || supports_host(port))
1969 if ((port->negotiated_rev == PD_REV20 && port->data_role == TYPEC_HOST) ||
1970 port->negotiated_rev == PD_REV30)
1971 return port->tcpc->attempt_vconn_swap_discovery(port->tcpc);
1977 static bool tcpm_cable_vdm_supported(struct tcpm_port *port)
1979 return !IS_ERR_OR_NULL(port->cable) &&
1980 typec_cable_is_active(port->cable) &&
1981 supports_modal_cable(port) &&
1982 tcpm_can_communicate_sop_prime(port);
1985 static int tcpm_pd_svdm(struct tcpm_port *port, struct typec_altmode *adev,
1986 const u32 *p, int cnt, u32 *response,
1987 enum adev_actions *adev_action,
1988 enum tcpm_transmit_type rx_sop_type,
1989 enum tcpm_transmit_type *response_tx_sop_type)
1991 struct typec_port *typec = port->typec_port;
1992 struct typec_altmode *pdev, *pdev_prime;
1993 struct pd_mode_data *modep, *modep_prime;
2001 cmd_type = PD_VDO_CMDT(p[0]);
2002 cmd = PD_VDO_CMD(p[0]);
2004 tcpm_log(port, "Rx VDM cmd 0x%x type %d cmd %d len %d",
2005 p[0], cmd_type, cmd, cnt);
2007 switch (rx_sop_type) {
2008 case TCPC_TX_SOP_PRIME:
2009 modep_prime = &port->mode_data_prime;
2010 pdev_prime = typec_match_altmode(port->plug_prime_altmode,
2011 ALTMODE_DISCOVERY_MAX,
2014 svdm_version = typec_get_cable_svdm_version(typec);
2016 * Update SVDM version if cable was discovered before port partner.
2018 if (!IS_ERR_OR_NULL(port->cable) &&
2019 PD_VDO_SVDM_VER(p[0]) < svdm_version)
2020 typec_cable_set_svdm_version(port->cable, svdm_version);
2023 modep = &port->mode_data;
2024 pdev = typec_match_altmode(port->partner_altmode,
2025 ALTMODE_DISCOVERY_MAX,
2028 svdm_version = typec_get_negotiated_svdm_version(typec);
2029 if (svdm_version < 0)
2033 modep = &port->mode_data;
2034 pdev = typec_match_altmode(port->partner_altmode,
2035 ALTMODE_DISCOVERY_MAX,
2038 svdm_version = typec_get_negotiated_svdm_version(typec);
2039 if (svdm_version < 0)
2047 * Only the port or port partner is allowed to initialize SVDM
2048 * commands over SOP'. In case the port partner initializes a
2049 * sequence when it is not allowed to send SOP' messages, drop
2050 * the message should the TCPM port try to process it.
2052 if (rx_sop_type == TCPC_TX_SOP_PRIME)
2056 case CMD_DISCOVER_IDENT:
2057 if (PD_VDO_VID(p[0]) != USB_SID_PD)
2060 if (IS_ERR_OR_NULL(port->partner))
2063 if (PD_VDO_SVDM_VER(p[0]) < svdm_version) {
2064 typec_partner_set_svdm_version(port->partner,
2065 PD_VDO_SVDM_VER(p[0]));
2066 svdm_version = PD_VDO_SVDM_VER(p[0]);
2069 port->ams = DISCOVER_IDENTITY;
2071 * PD2.0 Spec 6.10.3: respond with NAK as DFP (data host)
2072 * PD3.1 Spec 6.4.4.2.5.1: respond with NAK if "invalid field" or
2073 * "wrong configuation" or "Unrecognized"
2075 if ((port->data_role == TYPEC_DEVICE || svdm_version >= SVDM_VER_2_0) &&
2077 if (svdm_version < SVDM_VER_2_0) {
2078 for (i = 0; i < port->nr_snk_vdo_v1; i++)
2079 response[i + 1] = port->snk_vdo_v1[i];
2080 rlen = port->nr_snk_vdo_v1 + 1;
2083 for (i = 0; i < port->nr_snk_vdo; i++)
2084 response[i + 1] = port->snk_vdo[i];
2085 rlen = port->nr_snk_vdo + 1;
2089 case CMD_DISCOVER_SVID:
2090 port->ams = DISCOVER_SVIDS;
2092 case CMD_DISCOVER_MODES:
2093 port->ams = DISCOVER_MODES;
2095 case CMD_ENTER_MODE:
2096 port->ams = DFP_TO_UFP_ENTER_MODE;
2099 port->ams = DFP_TO_UFP_EXIT_MODE;
2102 /* Attention command does not have response */
2103 *adev_action = ADEV_ATTENTION;
2109 response[0] = p[0] | VDO_CMDT(CMDT_RSP_ACK);
2110 } else if (rlen == 0) {
2111 response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK);
2114 response[0] = p[0] | VDO_CMDT(CMDT_RSP_BUSY);
2117 response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) |
2118 (VDO_SVDM_VERS(typec_get_negotiated_svdm_version(typec)));
2122 * Silently drop message if we are not connected, but can process
2123 * if SOP' Discover Identity prior to explicit contract.
2125 if (IS_ERR_OR_NULL(port->partner) &&
2126 !(rx_sop_type == TCPC_TX_SOP_PRIME && cmd == CMD_DISCOVER_IDENT))
2129 tcpm_ams_finish(port);
2133 * SVDM Command Flow for SOP and SOP':
2134 * SOP Discover Identity
2135 * SOP' Discover Identity
2136 * SOP Discover SVIDs
2139 * SOP' Discover SVIDs
2142 * Perform Discover SOP' if the port can communicate with cable
2145 case CMD_DISCOVER_IDENT:
2146 switch (rx_sop_type) {
2148 if (PD_VDO_SVDM_VER(p[0]) < svdm_version) {
2149 typec_partner_set_svdm_version(port->partner,
2150 PD_VDO_SVDM_VER(p[0]));
2151 /* If cable is discovered before partner, downgrade svdm */
2152 if (!IS_ERR_OR_NULL(port->cable) &&
2153 (typec_get_cable_svdm_version(port->typec_port) >
2155 typec_cable_set_svdm_version(port->cable,
2159 svdm_consume_identity(port, p, cnt);
2160 /* Attempt Vconn swap, delay SOP' discovery if necessary */
2161 if (tcpm_attempt_vconn_swap_discovery(port)) {
2162 port->send_discover_prime = true;
2163 port->upcoming_state = VCONN_SWAP_SEND;
2164 ret = tcpm_ams_start(port, VCONN_SWAP);
2167 /* Cannot perform Vconn swap */
2168 port->upcoming_state = INVALID_STATE;
2169 port->send_discover_prime = false;
2173 * Attempt Discover Identity on SOP' if the
2174 * cable was not discovered previously, and use
2175 * the SVDM version of the partner to probe.
2177 if (IS_ERR_OR_NULL(port->cable) &&
2178 tcpm_can_communicate_sop_prime(port)) {
2179 *response_tx_sop_type = TCPC_TX_SOP_PRIME;
2180 port->send_discover_prime = true;
2181 response[0] = VDO(USB_SID_PD, 1,
2182 typec_get_negotiated_svdm_version(typec),
2183 CMD_DISCOVER_IDENT);
2186 *response_tx_sop_type = TCPC_TX_SOP;
2187 response[0] = VDO(USB_SID_PD, 1,
2188 typec_get_negotiated_svdm_version(typec),
2193 case TCPC_TX_SOP_PRIME:
2195 * svdm_consume_identity_sop_prime will determine
2196 * the svdm_version for the cable moving forward.
2198 svdm_consume_identity_sop_prime(port, p, cnt);
2201 * If received in SRC_VDM_IDENTITY_REQUEST, continue
2202 * to SRC_SEND_CAPABILITIES
2204 if (port->state == SRC_VDM_IDENTITY_REQUEST) {
2205 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
2209 *response_tx_sop_type = TCPC_TX_SOP;
2210 response[0] = VDO(USB_SID_PD, 1,
2211 typec_get_negotiated_svdm_version(typec),
2219 case CMD_DISCOVER_SVID:
2220 *response_tx_sop_type = rx_sop_type;
2222 if (svdm_consume_svids(port, p, cnt, rx_sop_type)) {
2223 response[0] = VDO(USB_SID_PD, 1, svdm_version, CMD_DISCOVER_SVID);
2226 if (rx_sop_type == TCPC_TX_SOP) {
2227 if (modep->nsvids && supports_modal(port)) {
2228 response[0] = VDO(modep->svids[0], 1, svdm_version,
2229 CMD_DISCOVER_MODES);
2232 } else if (rx_sop_type == TCPC_TX_SOP_PRIME) {
2233 if (modep_prime->nsvids) {
2234 response[0] = VDO(modep_prime->svids[0], 1,
2235 svdm_version, CMD_DISCOVER_MODES);
2241 case CMD_DISCOVER_MODES:
2242 if (rx_sop_type == TCPC_TX_SOP) {
2244 svdm_consume_modes(port, p, cnt, rx_sop_type);
2245 modep->svid_index++;
2246 if (modep->svid_index < modep->nsvids) {
2247 u16 svid = modep->svids[modep->svid_index];
2248 *response_tx_sop_type = TCPC_TX_SOP;
2249 response[0] = VDO(svid, 1, svdm_version,
2250 CMD_DISCOVER_MODES);
2252 } else if (tcpm_cable_vdm_supported(port)) {
2253 *response_tx_sop_type = TCPC_TX_SOP_PRIME;
2254 response[0] = VDO(USB_SID_PD, 1,
2255 typec_get_cable_svdm_version(typec),
2259 tcpm_register_partner_altmodes(port);
2261 } else if (rx_sop_type == TCPC_TX_SOP_PRIME) {
2263 svdm_consume_modes(port, p, cnt, rx_sop_type);
2264 modep_prime->svid_index++;
2265 if (modep_prime->svid_index < modep_prime->nsvids) {
2266 u16 svid = modep_prime->svids[modep_prime->svid_index];
2267 *response_tx_sop_type = TCPC_TX_SOP_PRIME;
2268 response[0] = VDO(svid, 1,
2269 typec_get_cable_svdm_version(typec),
2270 CMD_DISCOVER_MODES);
2273 tcpm_register_plug_altmodes(port);
2274 tcpm_register_partner_altmodes(port);
2278 case CMD_ENTER_MODE:
2279 *response_tx_sop_type = rx_sop_type;
2280 if (rx_sop_type == TCPC_TX_SOP) {
2282 typec_altmode_update_active(pdev, true);
2283 *adev_action = ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL;
2285 } else if (rx_sop_type == TCPC_TX_SOP_PRIME) {
2286 if (adev && pdev_prime) {
2287 typec_altmode_update_active(pdev_prime, true);
2288 *adev_action = ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL;
2293 *response_tx_sop_type = rx_sop_type;
2294 if (rx_sop_type == TCPC_TX_SOP) {
2296 typec_altmode_update_active(pdev, false);
2297 /* Back to USB Operation */
2298 *adev_action = ADEV_NOTIFY_USB_AND_QUEUE_VDM;
2303 case VDO_CMD_VENDOR(0) ... VDO_CMD_VENDOR(15):
2306 /* Unrecognized SVDM */
2307 response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK);
2309 response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) |
2310 (VDO_SVDM_VERS(svdm_version));
2315 tcpm_ams_finish(port);
2317 case CMD_DISCOVER_IDENT:
2318 case CMD_DISCOVER_SVID:
2319 case CMD_DISCOVER_MODES:
2320 case VDO_CMD_VENDOR(0) ... VDO_CMD_VENDOR(15):
2322 case CMD_ENTER_MODE:
2323 /* Back to USB Operation */
2324 *adev_action = ADEV_NOTIFY_USB_AND_QUEUE_VDM;
2327 /* Unrecognized SVDM */
2328 response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK);
2330 response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) |
2331 (VDO_SVDM_VERS(svdm_version));
2336 response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK);
2338 response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) |
2339 (VDO_SVDM_VERS(svdm_version));
2343 /* Informing the alternate mode drivers about everything */
2344 *adev_action = ADEV_QUEUE_VDM;
2348 static void tcpm_pd_handle_msg(struct tcpm_port *port,
2349 enum pd_msg_request message,
2352 static void tcpm_handle_vdm_request(struct tcpm_port *port,
2353 const __le32 *payload, int cnt,
2354 enum tcpm_transmit_type rx_sop_type)
2356 enum adev_actions adev_action = ADEV_NONE;
2357 struct typec_altmode *adev;
2358 u32 p[PD_MAX_PAYLOAD];
2359 u32 response[8] = { };
2361 enum tcpm_transmit_type response_tx_sop_type = TCPC_TX_SOP;
2363 for (i = 0; i < cnt; i++)
2364 p[i] = le32_to_cpu(payload[i]);
2366 adev = typec_match_altmode(port->port_altmode, ALTMODE_DISCOVERY_MAX,
2367 PD_VDO_VID(p[0]), PD_VDO_OPOS(p[0]));
2369 if (port->vdm_state == VDM_STATE_BUSY) {
2370 /* If UFP responded busy retry after timeout */
2371 if (PD_VDO_CMDT(p[0]) == CMDT_RSP_BUSY) {
2372 port->vdm_state = VDM_STATE_WAIT_RSP_BUSY;
2373 port->vdo_retry = (p[0] & ~VDO_CMDT_MASK) |
2375 mod_vdm_delayed_work(port, PD_T_VDM_BUSY);
2378 port->vdm_state = VDM_STATE_DONE;
2381 if (PD_VDO_SVDM(p[0]) && (adev || tcpm_vdm_ams(port) || port->nr_snk_vdo)) {
2383 * Here a SVDM is received (INIT or RSP or unknown). Set the vdm_sm_running in
2384 * advance because we are dropping the lock but may send VDMs soon.
2385 * For the cases of INIT received:
2386 * - If no response to send, it will be cleared later in this function.
2387 * - If there are responses to send, it will be cleared in the state machine.
2388 * For the cases of RSP received:
2389 * - If no further INIT to send, it will be cleared later in this function.
2390 * - Otherwise, it will be cleared in the state machine if timeout or it will go
2391 * back here until no further INIT to send.
2392 * For the cases of unknown type received:
2393 * - We will send NAK and the flag will be cleared in the state machine.
2395 port->vdm_sm_running = true;
2396 rlen = tcpm_pd_svdm(port, adev, p, cnt, response, &adev_action,
2397 rx_sop_type, &response_tx_sop_type);
2399 if (port->negotiated_rev >= PD_REV30)
2400 tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS);
2404 * We are done with any state stored in the port struct now, except
2405 * for any port struct changes done by the tcpm_queue_vdm() call
2406 * below, which is a separate operation.
2408 * So we can safely release the lock here; and we MUST release the
2409 * lock here to avoid an AB BA lock inversion:
2411 * If we keep the lock here then the lock ordering in this path is:
2412 * 1. tcpm_pd_rx_handler take the tcpm port lock
2413 * 2. One of the typec_altmode_* calls below takes the alt-mode's lock
2415 * And we also have this ordering:
2416 * 1. alt-mode driver takes the alt-mode's lock
2417 * 2. alt-mode driver calls tcpm_altmode_enter which takes the
2420 * Dropping our lock here avoids this.
2422 mutex_unlock(&port->lock);
2425 switch (adev_action) {
2428 case ADEV_NOTIFY_USB_AND_QUEUE_VDM:
2429 WARN_ON(typec_altmode_notify(adev, TYPEC_STATE_USB, NULL));
2430 typec_altmode_vdm(adev, p[0], &p[1], cnt);
2432 case ADEV_QUEUE_VDM:
2433 if (response_tx_sop_type == TCPC_TX_SOP_PRIME)
2434 typec_cable_altmode_vdm(adev, TYPEC_PLUG_SOP_P, p[0], &p[1], cnt);
2436 typec_altmode_vdm(adev, p[0], &p[1], cnt);
2438 case ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL:
2439 if (response_tx_sop_type == TCPC_TX_SOP_PRIME) {
2440 if (typec_cable_altmode_vdm(adev, TYPEC_PLUG_SOP_P,
2441 p[0], &p[1], cnt)) {
2442 int svdm_version = typec_get_cable_svdm_version(
2444 if (svdm_version < 0)
2447 response[0] = VDO(adev->svid, 1, svdm_version,
2449 response[0] |= VDO_OPOS(adev->mode);
2453 if (typec_altmode_vdm(adev, p[0], &p[1], cnt)) {
2454 int svdm_version = typec_get_negotiated_svdm_version(
2456 if (svdm_version < 0)
2459 response[0] = VDO(adev->svid, 1, svdm_version,
2461 response[0] |= VDO_OPOS(adev->mode);
2466 case ADEV_ATTENTION:
2467 if (typec_altmode_attention(adev, p[1]))
2468 tcpm_log(port, "typec_altmode_attention no port partner altmode");
2474 * We must re-take the lock here to balance the unlock in
2475 * tcpm_pd_rx_handler, note that no changes, other then the
2476 * tcpm_queue_vdm call, are made while the lock is held again.
2477 * All that is done after the call is unwinding the call stack until
2478 * we return to tcpm_pd_rx_handler and do the unlock there.
2480 mutex_lock(&port->lock);
2483 tcpm_queue_vdm(port, response[0], &response[1], rlen - 1, response_tx_sop_type);
2485 port->vdm_sm_running = false;
2488 static void tcpm_send_vdm(struct tcpm_port *port, u32 vid, int cmd,
2489 const u32 *data, int count, enum tcpm_transmit_type tx_sop_type)
2494 switch (tx_sop_type) {
2495 case TCPC_TX_SOP_PRIME:
2497 * If the port partner is discovered, then the port partner's
2498 * SVDM Version will be returned
2500 svdm_version = typec_get_cable_svdm_version(port->typec_port);
2501 if (svdm_version < 0)
2502 svdm_version = SVDM_VER_MAX;
2505 svdm_version = typec_get_negotiated_svdm_version(port->typec_port);
2506 if (svdm_version < 0)
2510 svdm_version = typec_get_negotiated_svdm_version(port->typec_port);
2511 if (svdm_version < 0)
2516 if (WARN_ON(count > VDO_MAX_SIZE - 1))
2517 count = VDO_MAX_SIZE - 1;
2519 /* set VDM header with VID & CMD */
2520 header = VDO(vid, ((vid & USB_SID_PD) == USB_SID_PD) ?
2521 1 : (PD_VDO_CMD(cmd) <= CMD_ATTENTION),
2523 tcpm_queue_vdm(port, header, data, count, tx_sop_type);
2526 static unsigned int vdm_ready_timeout(u32 vdm_hdr)
2528 unsigned int timeout;
2529 int cmd = PD_VDO_CMD(vdm_hdr);
2531 /* its not a structured VDM command */
2532 if (!PD_VDO_SVDM(vdm_hdr))
2533 return PD_T_VDM_UNSTRUCTURED;
2535 switch (PD_VDO_CMDT(vdm_hdr)) {
2537 if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE)
2538 timeout = PD_T_VDM_WAIT_MODE_E;
2540 timeout = PD_T_VDM_SNDR_RSP;
2543 if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE)
2544 timeout = PD_T_VDM_E_MODE;
2546 timeout = PD_T_VDM_RCVR_RSP;
2552 static void vdm_run_state_machine(struct tcpm_port *port)
2554 struct pd_message msg;
2556 u32 vdo_hdr = port->vdo_data[0];
2557 u32 response[8] = { };
2559 switch (port->vdm_state) {
2560 case VDM_STATE_READY:
2561 /* Only transmit VDM if attached */
2562 if (!port->attached) {
2563 port->vdm_state = VDM_STATE_ERR_BUSY;
2568 * if there's traffic or we're not in PDO ready state don't send
2571 if (port->state != SRC_READY && port->state != SNK_READY &&
2572 port->state != SRC_VDM_IDENTITY_REQUEST) {
2573 port->vdm_sm_running = false;
2577 /* TODO: AMS operation for Unstructured VDM */
2578 if (PD_VDO_SVDM(vdo_hdr) && PD_VDO_CMDT(vdo_hdr) == CMDT_INIT) {
2579 switch (PD_VDO_CMD(vdo_hdr)) {
2580 case CMD_DISCOVER_IDENT:
2581 res = tcpm_ams_start(port, DISCOVER_IDENTITY);
2583 switch (port->tx_sop_type) {
2584 case TCPC_TX_SOP_PRIME:
2585 port->send_discover_prime = false;
2588 port->send_discover = false;
2591 port->send_discover = false;
2594 } else if (res == -EAGAIN) {
2595 port->vdo_data[0] = 0;
2596 mod_send_discover_delayed_work(port,
2597 SEND_DISCOVER_RETRY_MS);
2600 case CMD_DISCOVER_SVID:
2601 res = tcpm_ams_start(port, DISCOVER_SVIDS);
2603 case CMD_DISCOVER_MODES:
2604 res = tcpm_ams_start(port, DISCOVER_MODES);
2606 case CMD_ENTER_MODE:
2607 res = tcpm_ams_start(port, DFP_TO_UFP_ENTER_MODE);
2610 res = tcpm_ams_start(port, DFP_TO_UFP_EXIT_MODE);
2613 res = tcpm_ams_start(port, ATTENTION);
2615 case VDO_CMD_VENDOR(0) ... VDO_CMD_VENDOR(15):
2616 res = tcpm_ams_start(port, STRUCTURED_VDMS);
2624 port->vdm_state = VDM_STATE_ERR_BUSY;
2629 port->vdm_state = VDM_STATE_SEND_MESSAGE;
2630 mod_vdm_delayed_work(port, (port->negotiated_rev >= PD_REV30 &&
2631 port->pwr_role == TYPEC_SOURCE &&
2632 PD_VDO_SVDM(vdo_hdr) &&
2633 PD_VDO_CMDT(vdo_hdr) == CMDT_INIT) ?
2636 case VDM_STATE_WAIT_RSP_BUSY:
2637 port->vdo_data[0] = port->vdo_retry;
2638 port->vdo_count = 1;
2639 port->vdm_state = VDM_STATE_READY;
2640 tcpm_ams_finish(port);
2642 case VDM_STATE_BUSY:
2643 port->vdm_state = VDM_STATE_ERR_TMOUT;
2644 if (port->ams != NONE_AMS)
2645 tcpm_ams_finish(port);
2647 case VDM_STATE_ERR_SEND:
2649 * When sending Discover Identity to SOP' before establishing an
2650 * explicit contract, do not retry. Instead, weave sending
2651 * Source_Capabilities over SOP and Discover Identity over SOP'.
2653 if (port->state == SRC_VDM_IDENTITY_REQUEST) {
2654 tcpm_ams_finish(port);
2655 port->vdm_state = VDM_STATE_DONE;
2656 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
2658 * A partner which does not support USB PD will not reply,
2659 * so this is not a fatal error. At the same time, some
2660 * devices may not return GoodCRC under some circumstances,
2661 * so we need to retry.
2663 } else if (port->vdm_retries < 3) {
2664 tcpm_log(port, "VDM Tx error, retry");
2665 port->vdm_retries++;
2666 port->vdm_state = VDM_STATE_READY;
2667 if (PD_VDO_SVDM(vdo_hdr) && PD_VDO_CMDT(vdo_hdr) == CMDT_INIT)
2668 tcpm_ams_finish(port);
2670 tcpm_ams_finish(port);
2671 if (port->tx_sop_type == TCPC_TX_SOP)
2673 /* Handle SOP' Transmission Errors */
2674 switch (PD_VDO_CMD(vdo_hdr)) {
2676 * If Discover Identity fails on SOP', then resume
2677 * discovery process on SOP only.
2679 case CMD_DISCOVER_IDENT:
2680 port->vdo_data[0] = 0;
2681 response[0] = VDO(USB_SID_PD, 1,
2682 typec_get_negotiated_svdm_version(
2685 tcpm_queue_vdm(port, response[0], &response[1],
2689 * If Discover SVIDs or Discover Modes fail, then
2690 * proceed with Alt Mode discovery process on SOP.
2692 case CMD_DISCOVER_SVID:
2693 tcpm_register_partner_altmodes(port);
2695 case CMD_DISCOVER_MODES:
2696 tcpm_register_partner_altmodes(port);
2703 case VDM_STATE_SEND_MESSAGE:
2704 /* Prepare and send VDM */
2705 memset(&msg, 0, sizeof(msg));
2706 if (port->tx_sop_type == TCPC_TX_SOP_PRIME) {
2707 msg.header = PD_HEADER_LE(PD_DATA_VENDOR_DEF,
2708 0, /* Cable Plug Indicator for DFP/UFP */
2710 port->negotiated_rev_prime,
2711 port->message_id_prime,
2714 msg.header = PD_HEADER_LE(PD_DATA_VENDOR_DEF,
2717 port->negotiated_rev,
2721 for (i = 0; i < port->vdo_count; i++)
2722 msg.payload[i] = cpu_to_le32(port->vdo_data[i]);
2723 res = tcpm_pd_transmit(port, port->tx_sop_type, &msg);
2725 port->vdm_state = VDM_STATE_ERR_SEND;
2727 unsigned long timeout;
2729 port->vdm_retries = 0;
2730 port->vdo_data[0] = 0;
2731 port->vdm_state = VDM_STATE_BUSY;
2732 timeout = vdm_ready_timeout(vdo_hdr);
2733 mod_vdm_delayed_work(port, timeout);
2741 static void vdm_state_machine_work(struct kthread_work *work)
2743 struct tcpm_port *port = container_of(work, struct tcpm_port, vdm_state_machine);
2744 enum vdm_states prev_state;
2746 mutex_lock(&port->lock);
2749 * Continue running as long as the port is not busy and there was
2753 prev_state = port->vdm_state;
2754 vdm_run_state_machine(port);
2755 } while (port->vdm_state != prev_state &&
2756 port->vdm_state != VDM_STATE_BUSY &&
2757 port->vdm_state != VDM_STATE_SEND_MESSAGE);
2759 if (port->vdm_state < VDM_STATE_READY)
2760 port->vdm_sm_running = false;
2762 mutex_unlock(&port->lock);
2768 PDO_ERR_VSAFE5V_NOT_FIRST,
2769 PDO_ERR_PDO_TYPE_NOT_IN_ORDER,
2770 PDO_ERR_FIXED_NOT_SORTED,
2771 PDO_ERR_VARIABLE_BATT_NOT_SORTED,
2773 PDO_ERR_PPS_APDO_NOT_SORTED,
2774 PDO_ERR_DUPE_PPS_APDO,
2777 static const char * const pdo_err_msg[] = {
2778 [PDO_ERR_NO_VSAFE5V] =
2779 " err: source/sink caps should at least have vSafe5V",
2780 [PDO_ERR_VSAFE5V_NOT_FIRST] =
2781 " err: vSafe5V Fixed Supply Object Shall always be the first object",
2782 [PDO_ERR_PDO_TYPE_NOT_IN_ORDER] =
2783 " err: PDOs should be in the following order: Fixed; Battery; Variable",
2784 [PDO_ERR_FIXED_NOT_SORTED] =
2785 " err: Fixed supply pdos should be in increasing order of their fixed voltage",
2786 [PDO_ERR_VARIABLE_BATT_NOT_SORTED] =
2787 " err: Variable/Battery supply pdos should be in increasing order of their minimum voltage",
2788 [PDO_ERR_DUPE_PDO] =
2789 " err: Variable/Batt supply pdos cannot have same min/max voltage",
2790 [PDO_ERR_PPS_APDO_NOT_SORTED] =
2791 " err: Programmable power supply apdos should be in increasing order of their maximum voltage",
2792 [PDO_ERR_DUPE_PPS_APDO] =
2793 " err: Programmable power supply apdos cannot have same min/max voltage and max current",
2796 static enum pdo_err tcpm_caps_err(struct tcpm_port *port, const u32 *pdo,
2797 unsigned int nr_pdo)
2801 /* Should at least contain vSafe5v */
2803 return PDO_ERR_NO_VSAFE5V;
2805 /* The vSafe5V Fixed Supply Object Shall always be the first object */
2806 if (pdo_type(pdo[0]) != PDO_TYPE_FIXED ||
2807 pdo_fixed_voltage(pdo[0]) != VSAFE5V)
2808 return PDO_ERR_VSAFE5V_NOT_FIRST;
2810 for (i = 1; i < nr_pdo; i++) {
2811 if (pdo_type(pdo[i]) < pdo_type(pdo[i - 1])) {
2812 return PDO_ERR_PDO_TYPE_NOT_IN_ORDER;
2813 } else if (pdo_type(pdo[i]) == pdo_type(pdo[i - 1])) {
2814 enum pd_pdo_type type = pdo_type(pdo[i]);
2818 * The remaining Fixed Supply Objects, if
2819 * present, shall be sent in voltage order;
2820 * lowest to highest.
2822 case PDO_TYPE_FIXED:
2823 if (pdo_fixed_voltage(pdo[i]) <=
2824 pdo_fixed_voltage(pdo[i - 1]))
2825 return PDO_ERR_FIXED_NOT_SORTED;
2828 * The Battery Supply Objects and Variable
2829 * supply, if present shall be sent in Minimum
2830 * Voltage order; lowest to highest.
2834 if (pdo_min_voltage(pdo[i]) <
2835 pdo_min_voltage(pdo[i - 1]))
2836 return PDO_ERR_VARIABLE_BATT_NOT_SORTED;
2837 else if ((pdo_min_voltage(pdo[i]) ==
2838 pdo_min_voltage(pdo[i - 1])) &&
2839 (pdo_max_voltage(pdo[i]) ==
2840 pdo_max_voltage(pdo[i - 1])))
2841 return PDO_ERR_DUPE_PDO;
2844 * The Programmable Power Supply APDOs, if present,
2845 * shall be sent in Maximum Voltage order;
2846 * lowest to highest.
2849 if (pdo_apdo_type(pdo[i]) != APDO_TYPE_PPS)
2852 if (pdo_pps_apdo_max_voltage(pdo[i]) <
2853 pdo_pps_apdo_max_voltage(pdo[i - 1]))
2854 return PDO_ERR_PPS_APDO_NOT_SORTED;
2855 else if (pdo_pps_apdo_min_voltage(pdo[i]) ==
2856 pdo_pps_apdo_min_voltage(pdo[i - 1]) &&
2857 pdo_pps_apdo_max_voltage(pdo[i]) ==
2858 pdo_pps_apdo_max_voltage(pdo[i - 1]) &&
2859 pdo_pps_apdo_max_current(pdo[i]) ==
2860 pdo_pps_apdo_max_current(pdo[i - 1]))
2861 return PDO_ERR_DUPE_PPS_APDO;
2864 tcpm_log_force(port, " Unknown pdo type");
2872 static int tcpm_validate_caps(struct tcpm_port *port, const u32 *pdo,
2873 unsigned int nr_pdo)
2875 enum pdo_err err_index = tcpm_caps_err(port, pdo, nr_pdo);
2877 if (err_index != PDO_NO_ERR) {
2878 tcpm_log_force(port, " %s", pdo_err_msg[err_index]);
2885 static int tcpm_altmode_enter(struct typec_altmode *altmode, u32 *vdo)
2887 struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
2891 svdm_version = typec_get_negotiated_svdm_version(port->typec_port);
2892 if (svdm_version < 0)
2893 return svdm_version;
2895 header = VDO(altmode->svid, vdo ? 2 : 1, svdm_version, CMD_ENTER_MODE);
2896 header |= VDO_OPOS(altmode->mode);
2898 return tcpm_queue_vdm_unlocked(port, header, vdo, vdo ? 1 : 0, TCPC_TX_SOP);
2901 static int tcpm_altmode_exit(struct typec_altmode *altmode)
2903 struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
2907 svdm_version = typec_get_negotiated_svdm_version(port->typec_port);
2908 if (svdm_version < 0)
2909 return svdm_version;
2911 header = VDO(altmode->svid, 1, svdm_version, CMD_EXIT_MODE);
2912 header |= VDO_OPOS(altmode->mode);
2914 return tcpm_queue_vdm_unlocked(port, header, NULL, 0, TCPC_TX_SOP);
2917 static int tcpm_altmode_vdm(struct typec_altmode *altmode,
2918 u32 header, const u32 *data, int count)
2920 struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
2922 return tcpm_queue_vdm_unlocked(port, header, data, count - 1, TCPC_TX_SOP);
2925 static const struct typec_altmode_ops tcpm_altmode_ops = {
2926 .enter = tcpm_altmode_enter,
2927 .exit = tcpm_altmode_exit,
2928 .vdm = tcpm_altmode_vdm,
2932 static int tcpm_cable_altmode_enter(struct typec_altmode *altmode, enum typec_plug_index sop,
2935 struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
2939 svdm_version = typec_get_cable_svdm_version(port->typec_port);
2940 if (svdm_version < 0)
2941 return svdm_version;
2943 header = VDO(altmode->svid, vdo ? 2 : 1, svdm_version, CMD_ENTER_MODE);
2944 header |= VDO_OPOS(altmode->mode);
2946 return tcpm_queue_vdm_unlocked(port, header, vdo, vdo ? 1 : 0, TCPC_TX_SOP_PRIME);
2949 static int tcpm_cable_altmode_exit(struct typec_altmode *altmode, enum typec_plug_index sop)
2951 struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
2955 svdm_version = typec_get_cable_svdm_version(port->typec_port);
2956 if (svdm_version < 0)
2957 return svdm_version;
2959 header = VDO(altmode->svid, 1, svdm_version, CMD_EXIT_MODE);
2960 header |= VDO_OPOS(altmode->mode);
2962 return tcpm_queue_vdm_unlocked(port, header, NULL, 0, TCPC_TX_SOP_PRIME);
2965 static int tcpm_cable_altmode_vdm(struct typec_altmode *altmode, enum typec_plug_index sop,
2966 u32 header, const u32 *data, int count)
2968 struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
2970 return tcpm_queue_vdm_unlocked(port, header, data, count - 1, TCPC_TX_SOP_PRIME);
2973 static const struct typec_cable_ops tcpm_cable_ops = {
2974 .enter = tcpm_cable_altmode_enter,
2975 .exit = tcpm_cable_altmode_exit,
2976 .vdm = tcpm_cable_altmode_vdm,
2980 * PD (data, control) command handling functions
2982 static inline enum tcpm_state ready_state(struct tcpm_port *port)
2984 if (port->pwr_role == TYPEC_SOURCE)
2990 static int tcpm_pd_send_control(struct tcpm_port *port,
2991 enum pd_ctrl_msg_type type,
2992 enum tcpm_transmit_type tx_sop_type);
2994 static void tcpm_handle_alert(struct tcpm_port *port, const __le32 *payload,
2997 u32 p0 = le32_to_cpu(payload[0]);
2998 unsigned int type = usb_pd_ado_type(p0);
3001 tcpm_log(port, "Alert message received with no type");
3002 tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP);
3006 /* Just handling non-battery alerts for now */
3007 if (!(type & USB_PD_ADO_TYPE_BATT_STATUS_CHANGE)) {
3008 if (port->pwr_role == TYPEC_SOURCE) {
3009 port->upcoming_state = GET_STATUS_SEND;
3010 tcpm_ams_start(port, GETTING_SOURCE_SINK_STATUS);
3013 * Do not check SinkTxOk here in case the Source doesn't set its Rp to
3016 port->ams = GETTING_SOURCE_SINK_STATUS;
3017 tcpm_set_state(port, GET_STATUS_SEND, 0);
3020 tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP);
3024 static int tcpm_set_auto_vbus_discharge_threshold(struct tcpm_port *port,
3025 enum typec_pwr_opmode mode, bool pps_active,
3026 u32 requested_vbus_voltage)
3030 if (!port->tcpc->set_auto_vbus_discharge_threshold)
3033 ret = port->tcpc->set_auto_vbus_discharge_threshold(port->tcpc, mode, pps_active,
3034 requested_vbus_voltage,
3035 port->pps_data.min_volt);
3036 tcpm_log_force(port,
3037 "set_auto_vbus_discharge_threshold mode:%d pps_active:%c vbus:%u pps_apdo_min_volt:%u ret:%d",
3038 mode, pps_active ? 'y' : 'n', requested_vbus_voltage,
3039 port->pps_data.min_volt, ret);
3044 static void tcpm_pd_handle_state(struct tcpm_port *port,
3045 enum tcpm_state state,
3047 unsigned int delay_ms)
3049 switch (port->state) {
3053 tcpm_set_state(port, state, delay_ms);
3055 /* 8.3.3.4.1.1 and 6.8.1 power transitioning */
3056 case SNK_TRANSITION_SINK:
3057 case SNK_TRANSITION_SINK_VBUS:
3058 case SRC_TRANSITION_SUPPLY:
3059 tcpm_set_state(port, HARD_RESET_SEND, 0);
3062 if (!tcpm_ams_interruptible(port)) {
3063 tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ?
3064 SRC_SOFT_RESET_WAIT_SNK_TX :
3068 /* process the Message 6.8.1 */
3069 port->upcoming_state = state;
3070 port->next_ams = ams;
3071 tcpm_set_state(port, ready_state(port), delay_ms);
3077 static void tcpm_pd_handle_msg(struct tcpm_port *port,
3078 enum pd_msg_request message,
3081 switch (port->state) {
3085 tcpm_queue_message(port, message);
3087 /* PD 3.0 Spec 8.3.3.4.1.1 and 6.8.1 */
3088 case SNK_TRANSITION_SINK:
3089 case SNK_TRANSITION_SINK_VBUS:
3090 case SRC_TRANSITION_SUPPLY:
3091 tcpm_set_state(port, HARD_RESET_SEND, 0);
3094 if (!tcpm_ams_interruptible(port)) {
3095 tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ?
3096 SRC_SOFT_RESET_WAIT_SNK_TX :
3100 port->next_ams = ams;
3101 tcpm_set_state(port, ready_state(port), 0);
3102 /* 6.8.1 process the Message */
3103 tcpm_queue_message(port, message);
3109 static int tcpm_register_source_caps(struct tcpm_port *port)
3111 struct usb_power_delivery_desc desc = { port->negotiated_rev };
3112 struct usb_power_delivery_capabilities_desc caps = { };
3113 struct usb_power_delivery_capabilities *cap = port->partner_source_caps;
3115 if (!port->partner_pd)
3116 port->partner_pd = usb_power_delivery_register(NULL, &desc);
3117 if (IS_ERR(port->partner_pd))
3118 return PTR_ERR(port->partner_pd);
3120 memcpy(caps.pdo, port->source_caps, sizeof(u32) * port->nr_source_caps);
3121 caps.role = TYPEC_SOURCE;
3124 usb_power_delivery_unregister_capabilities(cap);
3125 port->partner_source_caps = NULL;
3128 cap = usb_power_delivery_register_capabilities(port->partner_pd, &caps);
3130 return PTR_ERR(cap);
3132 port->partner_source_caps = cap;
3137 static int tcpm_register_sink_caps(struct tcpm_port *port)
3139 struct usb_power_delivery_desc desc = { port->negotiated_rev };
3140 struct usb_power_delivery_capabilities_desc caps = { };
3141 struct usb_power_delivery_capabilities *cap;
3143 if (!port->partner_pd)
3144 port->partner_pd = usb_power_delivery_register(NULL, &desc);
3145 if (IS_ERR(port->partner_pd))
3146 return PTR_ERR(port->partner_pd);
3148 memcpy(caps.pdo, port->sink_caps, sizeof(u32) * port->nr_sink_caps);
3149 caps.role = TYPEC_SINK;
3151 cap = usb_power_delivery_register_capabilities(port->partner_pd, &caps);
3153 return PTR_ERR(cap);
3155 port->partner_sink_caps = cap;
3160 static void tcpm_pd_data_request(struct tcpm_port *port,
3161 const struct pd_message *msg,
3162 enum tcpm_transmit_type rx_sop_type)
3164 enum pd_data_msg_type type = pd_header_type_le(msg->header);
3165 unsigned int cnt = pd_header_cnt_le(msg->header);
3166 unsigned int rev = pd_header_rev_le(msg->header);
3168 enum frs_typec_current partner_frs_current;
3172 if (tcpm_vdm_ams(port) && type != PD_DATA_VENDOR_DEF) {
3173 port->vdm_state = VDM_STATE_ERR_BUSY;
3174 tcpm_ams_finish(port);
3175 mod_vdm_delayed_work(port, 0);
3179 case PD_DATA_SOURCE_CAP:
3180 for (i = 0; i < cnt; i++)
3181 port->source_caps[i] = le32_to_cpu(msg->payload[i]);
3183 port->nr_source_caps = cnt;
3185 tcpm_log_source_caps(port);
3187 tcpm_validate_caps(port, port->source_caps,
3188 port->nr_source_caps);
3190 tcpm_register_source_caps(port);
3193 * Adjust revision in subsequent message headers, as required,
3194 * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't
3195 * support Rev 1.0 so just do nothing in that scenario.
3197 if (rev == PD_REV10) {
3198 if (port->ams == GET_SOURCE_CAPABILITIES)
3199 tcpm_ams_finish(port);
3203 if (rev < PD_MAX_REV) {
3204 port->negotiated_rev = rev;
3205 if (port->negotiated_rev_prime > port->negotiated_rev)
3206 port->negotiated_rev_prime = port->negotiated_rev;
3209 if (port->pwr_role == TYPEC_SOURCE) {
3210 if (port->ams == GET_SOURCE_CAPABILITIES)
3211 tcpm_pd_handle_state(port, SRC_READY, NONE_AMS, 0);
3212 /* Unexpected Source Capabilities */
3214 tcpm_pd_handle_msg(port,
3215 port->negotiated_rev < PD_REV30 ?
3216 PD_MSG_CTRL_REJECT :
3217 PD_MSG_CTRL_NOT_SUPP,
3219 } else if (port->state == SNK_WAIT_CAPABILITIES ||
3220 port->state == SNK_WAIT_CAPABILITIES_TIMEOUT) {
3222 * This message may be received even if VBUS is not
3223 * present. This is quite unexpected; see USB PD
3224 * specification, sections 8.3.3.6.3.1 and 8.3.3.6.3.2.
3225 * However, at the same time, we must be ready to
3226 * receive this message and respond to it 15ms after
3227 * receiving PS_RDY during power swap operations, no matter
3228 * if VBUS is available or not (USB PD specification,
3230 * So we need to accept the message either way,
3231 * but be prepared to keep waiting for VBUS after it was
3234 port->ams = POWER_NEGOTIATION;
3235 port->in_ams = true;
3236 tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
3238 if (port->ams == GET_SOURCE_CAPABILITIES)
3239 tcpm_ams_finish(port);
3240 tcpm_pd_handle_state(port, SNK_NEGOTIATE_CAPABILITIES,
3241 POWER_NEGOTIATION, 0);
3244 case PD_DATA_REQUEST:
3246 * Adjust revision in subsequent message headers, as required,
3247 * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't
3248 * support Rev 1.0 so just reject in that scenario.
3250 if (rev == PD_REV10) {
3251 tcpm_pd_handle_msg(port,
3252 port->negotiated_rev < PD_REV30 ?
3253 PD_MSG_CTRL_REJECT :
3254 PD_MSG_CTRL_NOT_SUPP,
3259 if (rev < PD_MAX_REV) {
3260 port->negotiated_rev = rev;
3261 if (port->negotiated_rev_prime > port->negotiated_rev)
3262 port->negotiated_rev_prime = port->negotiated_rev;
3265 if (port->pwr_role != TYPEC_SOURCE || cnt != 1) {
3266 tcpm_pd_handle_msg(port,
3267 port->negotiated_rev < PD_REV30 ?
3268 PD_MSG_CTRL_REJECT :
3269 PD_MSG_CTRL_NOT_SUPP,
3274 port->sink_request = le32_to_cpu(msg->payload[0]);
3276 if (port->vdm_sm_running && port->explicit_contract) {
3277 tcpm_pd_handle_msg(port, PD_MSG_CTRL_WAIT, port->ams);
3281 if (port->state == SRC_SEND_CAPABILITIES)
3282 tcpm_set_state(port, SRC_NEGOTIATE_CAPABILITIES, 0);
3284 tcpm_pd_handle_state(port, SRC_NEGOTIATE_CAPABILITIES,
3285 POWER_NEGOTIATION, 0);
3287 case PD_DATA_SINK_CAP:
3288 /* We don't do anything with this at the moment... */
3289 for (i = 0; i < cnt; i++)
3290 port->sink_caps[i] = le32_to_cpu(msg->payload[i]);
3292 partner_frs_current = (port->sink_caps[0] & PDO_FIXED_FRS_CURR_MASK) >>
3293 PDO_FIXED_FRS_CURR_SHIFT;
3294 frs_enable = partner_frs_current && (partner_frs_current <=
3295 port->new_source_frs_current);
3297 "Port partner FRS capable partner_frs_current:%u port_frs_current:%u enable:%c",
3298 partner_frs_current, port->new_source_frs_current, frs_enable ? 'y' : 'n');
3300 ret = port->tcpc->enable_frs(port->tcpc, true);
3301 tcpm_log(port, "Enable FRS %s, ret:%d\n", ret ? "fail" : "success", ret);
3304 port->nr_sink_caps = cnt;
3305 port->sink_cap_done = true;
3306 tcpm_register_sink_caps(port);
3308 if (port->ams == GET_SINK_CAPABILITIES)
3309 tcpm_set_state(port, ready_state(port), 0);
3310 /* Unexpected Sink Capabilities */
3312 tcpm_pd_handle_msg(port,
3313 port->negotiated_rev < PD_REV30 ?
3314 PD_MSG_CTRL_REJECT :
3315 PD_MSG_CTRL_NOT_SUPP,
3318 case PD_DATA_VENDOR_DEF:
3319 tcpm_handle_vdm_request(port, msg->payload, cnt, rx_sop_type);
3322 port->bist_request = le32_to_cpu(msg->payload[0]);
3323 tcpm_pd_handle_state(port, BIST_RX, BIST, 0);
3326 if (port->state != SRC_READY && port->state != SNK_READY)
3327 tcpm_pd_handle_state(port, port->pwr_role == TYPEC_SOURCE ?
3328 SRC_SOFT_RESET_WAIT_SNK_TX : SNK_SOFT_RESET,
3331 tcpm_handle_alert(port, msg->payload, cnt);
3333 case PD_DATA_BATT_STATUS:
3334 case PD_DATA_GET_COUNTRY_INFO:
3335 /* Currently unsupported */
3336 tcpm_pd_handle_msg(port, port->negotiated_rev < PD_REV30 ?
3337 PD_MSG_CTRL_REJECT :
3338 PD_MSG_CTRL_NOT_SUPP,
3342 tcpm_pd_handle_msg(port, port->negotiated_rev < PD_REV30 ?
3343 PD_MSG_CTRL_REJECT :
3344 PD_MSG_CTRL_NOT_SUPP,
3346 tcpm_log(port, "Unrecognized data message type %#x", type);
3351 static void tcpm_pps_complete(struct tcpm_port *port, int result)
3353 if (port->pps_pending) {
3354 port->pps_status = result;
3355 port->pps_pending = false;
3356 complete(&port->pps_complete);
3360 static void tcpm_pd_ctrl_request(struct tcpm_port *port,
3361 const struct pd_message *msg,
3362 enum tcpm_transmit_type rx_sop_type)
3364 enum pd_ctrl_msg_type type = pd_header_type_le(msg->header);
3365 enum tcpm_state next_state;
3366 unsigned int rev = pd_header_rev_le(msg->header);
3369 * Stop VDM state machine if interrupted by other Messages while NOT_SUPP is allowed in
3370 * VDM AMS if waiting for VDM responses and will be handled later.
3372 if (tcpm_vdm_ams(port) && type != PD_CTRL_NOT_SUPP && type != PD_CTRL_GOOD_CRC) {
3373 port->vdm_state = VDM_STATE_ERR_BUSY;
3374 tcpm_ams_finish(port);
3375 mod_vdm_delayed_work(port, 0);
3379 case PD_CTRL_GOOD_CRC:
3382 case PD_CTRL_GET_SOURCE_CAP:
3383 tcpm_pd_handle_msg(port, PD_MSG_DATA_SOURCE_CAP, GET_SOURCE_CAPABILITIES);
3385 case PD_CTRL_GET_SINK_CAP:
3386 tcpm_pd_handle_msg(port, PD_MSG_DATA_SINK_CAP, GET_SINK_CAPABILITIES);
3388 case PD_CTRL_GOTO_MIN:
3390 case PD_CTRL_PS_RDY:
3391 switch (port->state) {
3392 case SNK_TRANSITION_SINK:
3393 if (port->vbus_present) {
3394 tcpm_set_current_limit(port,
3395 port->req_current_limit,
3396 port->req_supply_voltage);
3397 port->explicit_contract = true;
3398 tcpm_set_auto_vbus_discharge_threshold(port,
3400 port->pps_data.active,
3401 port->supply_voltage);
3402 tcpm_set_state(port, SNK_READY, 0);
3405 * Seen after power swap. Keep waiting for VBUS
3406 * in a transitional state.
3408 tcpm_set_state(port,
3409 SNK_TRANSITION_SINK_VBUS, 0);
3412 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED:
3413 tcpm_set_state(port, PR_SWAP_SRC_SNK_SINK_ON, 0);
3415 case PR_SWAP_SNK_SRC_SINK_OFF:
3416 tcpm_set_state(port, PR_SWAP_SNK_SRC_SOURCE_ON, 0);
3418 case VCONN_SWAP_WAIT_FOR_VCONN:
3419 tcpm_set_state(port, VCONN_SWAP_TURN_OFF_VCONN, 0);
3421 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
3422 tcpm_set_state(port, FR_SWAP_SNK_SRC_NEW_SINK_READY, 0);
3425 tcpm_pd_handle_state(port,
3426 port->pwr_role == TYPEC_SOURCE ?
3427 SRC_SOFT_RESET_WAIT_SNK_TX :
3433 case PD_CTRL_REJECT:
3435 case PD_CTRL_NOT_SUPP:
3436 switch (port->state) {
3437 case SNK_NEGOTIATE_CAPABILITIES:
3438 /* USB PD specification, Figure 8-43 */
3439 if (port->explicit_contract)
3440 next_state = SNK_READY;
3442 next_state = SNK_WAIT_CAPABILITIES;
3444 /* Threshold was relaxed before sending Request. Restore it back. */
3445 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
3446 port->pps_data.active,
3447 port->supply_voltage);
3448 tcpm_set_state(port, next_state, 0);
3450 case SNK_NEGOTIATE_PPS_CAPABILITIES:
3451 /* Revert data back from any requested PPS updates */
3452 port->pps_data.req_out_volt = port->supply_voltage;
3453 port->pps_data.req_op_curr = port->current_limit;
3454 port->pps_status = (type == PD_CTRL_WAIT ?
3455 -EAGAIN : -EOPNOTSUPP);
3457 /* Threshold was relaxed before sending Request. Restore it back. */
3458 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
3459 port->pps_data.active,
3460 port->supply_voltage);
3462 tcpm_set_state(port, SNK_READY, 0);
3465 port->swap_status = (type == PD_CTRL_WAIT ?
3466 -EAGAIN : -EOPNOTSUPP);
3467 tcpm_set_state(port, DR_SWAP_CANCEL, 0);
3470 port->swap_status = (type == PD_CTRL_WAIT ?
3471 -EAGAIN : -EOPNOTSUPP);
3472 tcpm_set_state(port, PR_SWAP_CANCEL, 0);
3474 case VCONN_SWAP_SEND:
3475 port->swap_status = (type == PD_CTRL_WAIT ?
3476 -EAGAIN : -EOPNOTSUPP);
3477 tcpm_set_state(port, VCONN_SWAP_CANCEL, 0);
3480 tcpm_set_state(port, FR_SWAP_CANCEL, 0);
3483 port->sink_cap_done = true;
3484 tcpm_set_state(port, ready_state(port), 0);
3487 * Some port partners do not support GET_STATUS, avoid soft reset the link to
3488 * prevent redundant power re-negotiation
3490 case GET_STATUS_SEND:
3491 tcpm_set_state(port, ready_state(port), 0);
3495 if (port->vdm_state > VDM_STATE_READY) {
3496 port->vdm_state = VDM_STATE_DONE;
3497 if (tcpm_vdm_ams(port))
3498 tcpm_ams_finish(port);
3499 mod_vdm_delayed_work(port, 0);
3504 tcpm_pd_handle_state(port,
3505 port->pwr_role == TYPEC_SOURCE ?
3506 SRC_SOFT_RESET_WAIT_SNK_TX :
3512 case PD_CTRL_ACCEPT:
3513 switch (port->state) {
3514 case SNK_NEGOTIATE_CAPABILITIES:
3515 port->pps_data.active = false;
3516 tcpm_set_state(port, SNK_TRANSITION_SINK, 0);
3518 case SNK_NEGOTIATE_PPS_CAPABILITIES:
3519 port->pps_data.active = true;
3520 port->pps_data.min_volt = port->pps_data.req_min_volt;
3521 port->pps_data.max_volt = port->pps_data.req_max_volt;
3522 port->pps_data.max_curr = port->pps_data.req_max_curr;
3523 port->req_supply_voltage = port->pps_data.req_out_volt;
3524 port->req_current_limit = port->pps_data.req_op_curr;
3525 power_supply_changed(port->psy);
3526 tcpm_set_state(port, SNK_TRANSITION_SINK, 0);
3528 case SOFT_RESET_SEND:
3529 if (port->ams == SOFT_RESET_AMS)
3530 tcpm_ams_finish(port);
3532 * SOP' Soft Reset is done after Vconn Swap,
3533 * which returns to ready state
3535 if (rx_sop_type == TCPC_TX_SOP_PRIME) {
3536 if (rev < port->negotiated_rev_prime)
3537 port->negotiated_rev_prime = rev;
3538 tcpm_set_state(port, ready_state(port), 0);
3541 if (port->pwr_role == TYPEC_SOURCE) {
3542 port->upcoming_state = SRC_SEND_CAPABILITIES;
3543 tcpm_ams_start(port, POWER_NEGOTIATION);
3545 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
3549 tcpm_set_state(port, DR_SWAP_CHANGE_DR, 0);
3552 tcpm_set_state(port, PR_SWAP_START, 0);
3554 case VCONN_SWAP_SEND:
3555 tcpm_set_state(port, VCONN_SWAP_START, 0);
3558 tcpm_set_state(port, FR_SWAP_SNK_SRC_TRANSITION_TO_OFF, 0);
3561 tcpm_pd_handle_state(port,
3562 port->pwr_role == TYPEC_SOURCE ?
3563 SRC_SOFT_RESET_WAIT_SNK_TX :
3569 case PD_CTRL_SOFT_RESET:
3570 port->ams = SOFT_RESET_AMS;
3571 tcpm_set_state(port, SOFT_RESET, 0);
3573 case PD_CTRL_DR_SWAP:
3576 * 6.3.9: If an alternate mode is active, a request to swap
3577 * alternate modes shall trigger a port reset.
3579 if (port->typec_caps.data != TYPEC_PORT_DRD) {
3580 tcpm_pd_handle_msg(port,
3581 port->negotiated_rev < PD_REV30 ?
3582 PD_MSG_CTRL_REJECT :
3583 PD_MSG_CTRL_NOT_SUPP,
3586 if (port->send_discover && port->negotiated_rev < PD_REV30) {
3587 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
3591 tcpm_pd_handle_state(port, DR_SWAP_ACCEPT, DATA_ROLE_SWAP, 0);
3594 case PD_CTRL_PR_SWAP:
3595 if (port->port_type != TYPEC_PORT_DRP) {
3596 tcpm_pd_handle_msg(port,
3597 port->negotiated_rev < PD_REV30 ?
3598 PD_MSG_CTRL_REJECT :
3599 PD_MSG_CTRL_NOT_SUPP,
3602 if (port->send_discover && port->negotiated_rev < PD_REV30) {
3603 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
3607 tcpm_pd_handle_state(port, PR_SWAP_ACCEPT, POWER_ROLE_SWAP, 0);
3610 case PD_CTRL_VCONN_SWAP:
3611 if (port->send_discover && port->negotiated_rev < PD_REV30) {
3612 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
3616 tcpm_pd_handle_state(port, VCONN_SWAP_ACCEPT, VCONN_SWAP, 0);
3618 case PD_CTRL_GET_SOURCE_CAP_EXT:
3619 case PD_CTRL_GET_STATUS:
3620 case PD_CTRL_FR_SWAP:
3621 case PD_CTRL_GET_PPS_STATUS:
3622 case PD_CTRL_GET_COUNTRY_CODES:
3623 /* Currently not supported */
3624 tcpm_pd_handle_msg(port,
3625 port->negotiated_rev < PD_REV30 ?
3626 PD_MSG_CTRL_REJECT :
3627 PD_MSG_CTRL_NOT_SUPP,
3630 case PD_CTRL_GET_REVISION:
3631 if (port->negotiated_rev >= PD_REV30 && port->pd_rev.rev_major)
3632 tcpm_pd_handle_msg(port, PD_MSG_DATA_REV,
3633 REVISION_INFORMATION);
3635 tcpm_pd_handle_msg(port,
3636 port->negotiated_rev < PD_REV30 ?
3637 PD_MSG_CTRL_REJECT :
3638 PD_MSG_CTRL_NOT_SUPP,
3642 tcpm_pd_handle_msg(port,
3643 port->negotiated_rev < PD_REV30 ?
3644 PD_MSG_CTRL_REJECT :
3645 PD_MSG_CTRL_NOT_SUPP,
3647 tcpm_log(port, "Unrecognized ctrl message type %#x", type);
3652 static void tcpm_pd_ext_msg_request(struct tcpm_port *port,
3653 const struct pd_message *msg)
3655 enum pd_ext_msg_type type = pd_header_type_le(msg->header);
3656 unsigned int data_size = pd_ext_header_data_size_le(msg->ext_msg.header);
3658 /* stopping VDM state machine if interrupted by other Messages */
3659 if (tcpm_vdm_ams(port)) {
3660 port->vdm_state = VDM_STATE_ERR_BUSY;
3661 tcpm_ams_finish(port);
3662 mod_vdm_delayed_work(port, 0);
3665 if (!(le16_to_cpu(msg->ext_msg.header) & PD_EXT_HDR_CHUNKED)) {
3666 tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS);
3667 tcpm_log(port, "Unchunked extended messages unsupported");
3671 if (data_size > PD_EXT_MAX_CHUNK_DATA) {
3672 tcpm_pd_handle_state(port, CHUNK_NOT_SUPP, NONE_AMS, PD_T_CHUNK_NOT_SUPP);
3673 tcpm_log(port, "Chunk handling not yet supported");
3679 case PD_EXT_PPS_STATUS:
3680 if (port->ams == GETTING_SOURCE_SINK_STATUS) {
3681 tcpm_ams_finish(port);
3682 tcpm_set_state(port, ready_state(port), 0);
3684 /* unexpected Status or PPS_Status Message */
3685 tcpm_pd_handle_state(port, port->pwr_role == TYPEC_SOURCE ?
3686 SRC_SOFT_RESET_WAIT_SNK_TX : SNK_SOFT_RESET,
3690 case PD_EXT_SOURCE_CAP_EXT:
3691 case PD_EXT_GET_BATT_CAP:
3692 case PD_EXT_GET_BATT_STATUS:
3693 case PD_EXT_BATT_CAP:
3694 case PD_EXT_GET_MANUFACTURER_INFO:
3695 case PD_EXT_MANUFACTURER_INFO:
3696 case PD_EXT_SECURITY_REQUEST:
3697 case PD_EXT_SECURITY_RESPONSE:
3698 case PD_EXT_FW_UPDATE_REQUEST:
3699 case PD_EXT_FW_UPDATE_RESPONSE:
3700 case PD_EXT_COUNTRY_INFO:
3701 case PD_EXT_COUNTRY_CODES:
3702 tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS);
3705 tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS);
3706 tcpm_log(port, "Unrecognized extended message type %#x", type);
3711 static void tcpm_pd_rx_handler(struct kthread_work *work)
3713 struct pd_rx_event *event = container_of(work,
3714 struct pd_rx_event, work);
3715 const struct pd_message *msg = &event->msg;
3716 unsigned int cnt = pd_header_cnt_le(msg->header);
3717 struct tcpm_port *port = event->port;
3718 enum tcpm_transmit_type rx_sop_type = event->rx_sop_type;
3720 mutex_lock(&port->lock);
3722 tcpm_log(port, "PD RX, header: %#x [%d]", le16_to_cpu(msg->header),
3725 if (port->attached) {
3726 enum pd_ctrl_msg_type type = pd_header_type_le(msg->header);
3727 unsigned int msgid = pd_header_msgid_le(msg->header);
3730 * Drop SOP' messages if cannot receive via
3731 * tcpm_can_communicate_sop_prime
3733 if (rx_sop_type == TCPC_TX_SOP_PRIME &&
3734 !tcpm_can_communicate_sop_prime(port))
3738 * USB PD standard, 6.6.1.2:
3739 * "... if MessageID value in a received Message is the
3740 * same as the stored value, the receiver shall return a
3741 * GoodCRC Message with that MessageID value and drop
3742 * the Message (this is a retry of an already received
3743 * Message). Note: this shall not apply to the Soft_Reset
3744 * Message which always has a MessageID value of zero."
3746 switch (rx_sop_type) {
3747 case TCPC_TX_SOP_PRIME:
3748 if (msgid == port->rx_msgid_prime)
3750 port->rx_msgid_prime = msgid;
3754 if (msgid == port->rx_msgid && type != PD_CTRL_SOFT_RESET)
3756 port->rx_msgid = msgid;
3761 * If both ends believe to be DFP/host, we have a data role
3764 if (!!(le16_to_cpu(msg->header) & PD_HEADER_DATA_ROLE) ==
3765 (port->data_role == TYPEC_HOST) && rx_sop_type == TCPC_TX_SOP) {
3767 "Data role mismatch, initiating error recovery");
3768 tcpm_set_state(port, ERROR_RECOVERY, 0);
3770 if (le16_to_cpu(msg->header) & PD_HEADER_EXT_HDR)
3771 tcpm_pd_ext_msg_request(port, msg);
3773 tcpm_pd_data_request(port, msg, rx_sop_type);
3775 tcpm_pd_ctrl_request(port, msg, rx_sop_type);
3780 mutex_unlock(&port->lock);
3784 void tcpm_pd_receive(struct tcpm_port *port, const struct pd_message *msg,
3785 enum tcpm_transmit_type rx_sop_type)
3787 struct pd_rx_event *event;
3789 event = kzalloc(sizeof(*event), GFP_ATOMIC);
3793 kthread_init_work(&event->work, tcpm_pd_rx_handler);
3795 event->rx_sop_type = rx_sop_type;
3796 memcpy(&event->msg, msg, sizeof(*msg));
3797 kthread_queue_work(port->wq, &event->work);
3799 EXPORT_SYMBOL_GPL(tcpm_pd_receive);
3801 static int tcpm_pd_send_control(struct tcpm_port *port,
3802 enum pd_ctrl_msg_type type,
3803 enum tcpm_transmit_type tx_sop_type)
3805 struct pd_message msg;
3807 memset(&msg, 0, sizeof(msg));
3808 switch (tx_sop_type) {
3809 case TCPC_TX_SOP_PRIME:
3810 msg.header = PD_HEADER_LE(type,
3811 0, /* Cable Plug Indicator for DFP/UFP */
3813 port->negotiated_rev,
3814 port->message_id_prime,
3818 msg.header = PD_HEADER_LE(type,
3821 port->negotiated_rev,
3826 msg.header = PD_HEADER_LE(type,
3829 port->negotiated_rev,
3835 return tcpm_pd_transmit(port, tx_sop_type, &msg);
3839 * Send queued message without affecting state.
3840 * Return true if state machine should go back to sleep,
3843 static bool tcpm_send_queued_message(struct tcpm_port *port)
3845 enum pd_msg_request queued_message;
3849 queued_message = port->queued_message;
3850 port->queued_message = PD_MSG_NONE;
3852 switch (queued_message) {
3853 case PD_MSG_CTRL_WAIT:
3854 tcpm_pd_send_control(port, PD_CTRL_WAIT, TCPC_TX_SOP);
3856 case PD_MSG_CTRL_REJECT:
3857 tcpm_pd_send_control(port, PD_CTRL_REJECT, TCPC_TX_SOP);
3859 case PD_MSG_CTRL_NOT_SUPP:
3860 tcpm_pd_send_control(port, PD_CTRL_NOT_SUPP, TCPC_TX_SOP);
3862 case PD_MSG_DATA_SINK_CAP:
3863 ret = tcpm_pd_send_sink_caps(port);
3865 tcpm_log(port, "Unable to send snk caps, ret=%d", ret);
3866 tcpm_set_state(port, SNK_SOFT_RESET, 0);
3868 tcpm_ams_finish(port);
3870 case PD_MSG_DATA_SOURCE_CAP:
3871 ret = tcpm_pd_send_source_caps(port);
3874 "Unable to send src caps, ret=%d",
3876 tcpm_set_state(port, SOFT_RESET_SEND, 0);
3877 } else if (port->pwr_role == TYPEC_SOURCE) {
3878 tcpm_ams_finish(port);
3879 tcpm_set_state(port, HARD_RESET_SEND,
3880 PD_T_SENDER_RESPONSE);
3882 tcpm_ams_finish(port);
3885 case PD_MSG_DATA_REV:
3886 ret = tcpm_pd_send_revision(port);
3889 "Unable to send revision msg, ret=%d",
3891 tcpm_ams_finish(port);
3896 } while (port->queued_message != PD_MSG_NONE);
3898 if (port->delayed_state != INVALID_STATE) {
3899 if (ktime_after(port->delayed_runtime, ktime_get())) {
3900 mod_tcpm_delayed_work(port, ktime_to_ms(ktime_sub(port->delayed_runtime,
3904 port->delayed_state = INVALID_STATE;
3909 static int tcpm_pd_check_request(struct tcpm_port *port)
3911 u32 pdo, rdo = port->sink_request;
3912 unsigned int max, op, pdo_max, index;
3913 enum pd_pdo_type type;
3915 index = rdo_index(rdo);
3916 if (!index || index > port->nr_src_pdo)
3919 pdo = port->src_pdo[index - 1];
3920 type = pdo_type(pdo);
3922 case PDO_TYPE_FIXED:
3924 max = rdo_max_current(rdo);
3925 op = rdo_op_current(rdo);
3926 pdo_max = pdo_max_current(pdo);
3930 if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH))
3933 if (type == PDO_TYPE_FIXED)
3935 "Requested %u mV, %u mA for %u / %u mA",
3936 pdo_fixed_voltage(pdo), pdo_max, op, max);
3939 "Requested %u -> %u mV, %u mA for %u / %u mA",
3940 pdo_min_voltage(pdo), pdo_max_voltage(pdo),
3944 max = rdo_max_power(rdo);
3945 op = rdo_op_power(rdo);
3946 pdo_max = pdo_max_power(pdo);
3950 if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH))
3953 "Requested %u -> %u mV, %u mW for %u / %u mW",
3954 pdo_min_voltage(pdo), pdo_max_voltage(pdo),
3961 port->op_vsafe5v = index == 1;
3966 #define min_power(x, y) min(pdo_max_power(x), pdo_max_power(y))
3967 #define min_current(x, y) min(pdo_max_current(x), pdo_max_current(y))
3969 static int tcpm_pd_select_pdo(struct tcpm_port *port, int *sink_pdo,
3972 unsigned int i, j, max_src_mv = 0, min_src_mv = 0, max_mw = 0,
3973 max_mv = 0, src_mw = 0, src_ma = 0, max_snk_mv = 0,
3977 port->pps_data.supported = false;
3978 port->usb_type = POWER_SUPPLY_USB_TYPE_PD;
3979 power_supply_changed(port->psy);
3982 * Select the source PDO providing the most power which has a
3985 for (i = 0; i < port->nr_source_caps; i++) {
3986 u32 pdo = port->source_caps[i];
3987 enum pd_pdo_type type = pdo_type(pdo);
3990 case PDO_TYPE_FIXED:
3991 max_src_mv = pdo_fixed_voltage(pdo);
3992 min_src_mv = max_src_mv;
3996 max_src_mv = pdo_max_voltage(pdo);
3997 min_src_mv = pdo_min_voltage(pdo);
4000 if (pdo_apdo_type(pdo) == APDO_TYPE_PPS) {
4001 port->pps_data.supported = true;
4003 POWER_SUPPLY_USB_TYPE_PD_PPS;
4004 power_supply_changed(port->psy);
4008 tcpm_log(port, "Invalid source PDO type, ignoring");
4013 case PDO_TYPE_FIXED:
4015 src_ma = pdo_max_current(pdo);
4016 src_mw = src_ma * min_src_mv / 1000;
4019 src_mw = pdo_max_power(pdo);
4024 tcpm_log(port, "Invalid source PDO type, ignoring");
4028 for (j = 0; j < port->nr_snk_pdo; j++) {
4029 pdo = port->snk_pdo[j];
4031 switch (pdo_type(pdo)) {
4032 case PDO_TYPE_FIXED:
4033 max_snk_mv = pdo_fixed_voltage(pdo);
4034 min_snk_mv = max_snk_mv;
4038 max_snk_mv = pdo_max_voltage(pdo);
4039 min_snk_mv = pdo_min_voltage(pdo);
4044 tcpm_log(port, "Invalid sink PDO type, ignoring");
4048 if (max_src_mv <= max_snk_mv &&
4049 min_src_mv >= min_snk_mv) {
4050 /* Prefer higher voltages if available */
4051 if ((src_mw == max_mw && min_src_mv > max_mv) ||
4056 max_mv = min_src_mv;
4066 static unsigned int tcpm_pd_select_pps_apdo(struct tcpm_port *port)
4068 unsigned int i, src_ma, max_temp_mw = 0, max_op_ma, op_mw;
4069 unsigned int src_pdo = 0;
4072 for (i = 1; i < port->nr_source_caps; ++i) {
4073 pdo = port->source_caps[i];
4075 switch (pdo_type(pdo)) {
4077 if (pdo_apdo_type(pdo) != APDO_TYPE_PPS) {
4078 tcpm_log(port, "Not PPS APDO (source), ignoring");
4082 if (port->pps_data.req_out_volt > pdo_pps_apdo_max_voltage(pdo) ||
4083 port->pps_data.req_out_volt < pdo_pps_apdo_min_voltage(pdo))
4086 src_ma = pdo_pps_apdo_max_current(pdo);
4087 max_op_ma = min(src_ma, port->pps_data.req_op_curr);
4088 op_mw = max_op_ma * port->pps_data.req_out_volt / 1000;
4089 if (op_mw > max_temp_mw) {
4091 max_temp_mw = op_mw;
4095 tcpm_log(port, "Not APDO type (source), ignoring");
4101 src = port->source_caps[src_pdo];
4103 port->pps_data.req_min_volt = pdo_pps_apdo_min_voltage(src);
4104 port->pps_data.req_max_volt = pdo_pps_apdo_max_voltage(src);
4105 port->pps_data.req_max_curr = pdo_pps_apdo_max_current(src);
4106 port->pps_data.req_op_curr = min(port->pps_data.req_max_curr,
4107 port->pps_data.req_op_curr);
4113 static int tcpm_pd_build_request(struct tcpm_port *port, u32 *rdo)
4115 unsigned int mv, ma, mw, flags;
4116 unsigned int max_ma, max_mw;
4117 enum pd_pdo_type type;
4118 u32 pdo, matching_snk_pdo;
4119 int src_pdo_index = 0;
4120 int snk_pdo_index = 0;
4123 ret = tcpm_pd_select_pdo(port, &snk_pdo_index, &src_pdo_index);
4127 pdo = port->source_caps[src_pdo_index];
4128 matching_snk_pdo = port->snk_pdo[snk_pdo_index];
4129 type = pdo_type(pdo);
4132 case PDO_TYPE_FIXED:
4133 mv = pdo_fixed_voltage(pdo);
4137 mv = pdo_min_voltage(pdo);
4140 tcpm_log(port, "Invalid PDO selected!");
4144 /* Select maximum available current within the sink pdo's limit */
4145 if (type == PDO_TYPE_BATT) {
4146 mw = min_power(pdo, matching_snk_pdo);
4147 ma = 1000 * mw / mv;
4149 ma = min_current(pdo, matching_snk_pdo);
4150 mw = ma * mv / 1000;
4153 flags = RDO_USB_COMM | RDO_NO_SUSPEND;
4155 /* Set mismatch bit if offered power is less than operating power */
4158 if (mw < port->operating_snk_mw) {
4159 flags |= RDO_CAP_MISMATCH;
4160 if (type == PDO_TYPE_BATT &&
4161 (pdo_max_power(matching_snk_pdo) > pdo_max_power(pdo)))
4162 max_mw = pdo_max_power(matching_snk_pdo);
4163 else if (pdo_max_current(matching_snk_pdo) >
4164 pdo_max_current(pdo))
4165 max_ma = pdo_max_current(matching_snk_pdo);
4168 tcpm_log(port, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d",
4169 port->cc_req, port->cc1, port->cc2, port->vbus_source,
4170 port->vconn_role == TYPEC_SOURCE ? "source" : "sink",
4173 if (type == PDO_TYPE_BATT) {
4174 *rdo = RDO_BATT(src_pdo_index + 1, mw, max_mw, flags);
4176 tcpm_log(port, "Requesting PDO %d: %u mV, %u mW%s",
4177 src_pdo_index, mv, mw,
4178 flags & RDO_CAP_MISMATCH ? " [mismatch]" : "");
4180 *rdo = RDO_FIXED(src_pdo_index + 1, ma, max_ma, flags);
4182 tcpm_log(port, "Requesting PDO %d: %u mV, %u mA%s",
4183 src_pdo_index, mv, ma,
4184 flags & RDO_CAP_MISMATCH ? " [mismatch]" : "");
4187 port->req_current_limit = ma;
4188 port->req_supply_voltage = mv;
4193 static int tcpm_pd_send_request(struct tcpm_port *port)
4195 struct pd_message msg;
4199 ret = tcpm_pd_build_request(port, &rdo);
4204 * Relax the threshold as voltage will be adjusted after Accept Message plus tSrcTransition.
4205 * It is safer to modify the threshold here.
4207 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0);
4209 memset(&msg, 0, sizeof(msg));
4210 msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
4213 port->negotiated_rev,
4214 port->message_id, 1);
4215 msg.payload[0] = cpu_to_le32(rdo);
4217 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
4220 static int tcpm_pd_build_pps_request(struct tcpm_port *port, u32 *rdo)
4222 unsigned int out_mv, op_ma, op_mw, max_mv, max_ma, flags;
4223 unsigned int src_pdo_index;
4225 src_pdo_index = tcpm_pd_select_pps_apdo(port);
4229 max_mv = port->pps_data.req_max_volt;
4230 max_ma = port->pps_data.req_max_curr;
4231 out_mv = port->pps_data.req_out_volt;
4232 op_ma = port->pps_data.req_op_curr;
4234 flags = RDO_USB_COMM | RDO_NO_SUSPEND;
4236 op_mw = (op_ma * out_mv) / 1000;
4237 if (op_mw < port->operating_snk_mw) {
4239 * Try raising current to meet power needs. If that's not enough
4240 * then try upping the voltage. If that's still not enough
4241 * then we've obviously chosen a PPS APDO which really isn't
4242 * suitable so abandon ship.
4244 op_ma = (port->operating_snk_mw * 1000) / out_mv;
4245 if ((port->operating_snk_mw * 1000) % out_mv)
4247 op_ma += RDO_PROG_CURR_MA_STEP - (op_ma % RDO_PROG_CURR_MA_STEP);
4249 if (op_ma > max_ma) {
4251 out_mv = (port->operating_snk_mw * 1000) / op_ma;
4252 if ((port->operating_snk_mw * 1000) % op_ma)
4254 out_mv += RDO_PROG_VOLT_MV_STEP -
4255 (out_mv % RDO_PROG_VOLT_MV_STEP);
4257 if (out_mv > max_mv) {
4258 tcpm_log(port, "Invalid PPS APDO selected!");
4264 tcpm_log(port, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d",
4265 port->cc_req, port->cc1, port->cc2, port->vbus_source,
4266 port->vconn_role == TYPEC_SOURCE ? "source" : "sink",
4269 *rdo = RDO_PROG(src_pdo_index + 1, out_mv, op_ma, flags);
4271 tcpm_log(port, "Requesting APDO %d: %u mV, %u mA",
4272 src_pdo_index, out_mv, op_ma);
4274 port->pps_data.req_op_curr = op_ma;
4275 port->pps_data.req_out_volt = out_mv;
4280 static int tcpm_pd_send_pps_request(struct tcpm_port *port)
4282 struct pd_message msg;
4286 ret = tcpm_pd_build_pps_request(port, &rdo);
4290 /* Relax the threshold as voltage will be adjusted right after Accept Message. */
4291 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0);
4293 memset(&msg, 0, sizeof(msg));
4294 msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
4297 port->negotiated_rev,
4298 port->message_id, 1);
4299 msg.payload[0] = cpu_to_le32(rdo);
4301 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
4304 static int tcpm_set_vbus(struct tcpm_port *port, bool enable)
4308 if (enable && port->vbus_charge)
4311 tcpm_log(port, "vbus:=%d charge=%d", enable, port->vbus_charge);
4313 ret = port->tcpc->set_vbus(port->tcpc, enable, port->vbus_charge);
4317 port->vbus_source = enable;
4321 static int tcpm_set_charge(struct tcpm_port *port, bool charge)
4325 if (charge && port->vbus_source)
4328 if (charge != port->vbus_charge) {
4329 tcpm_log(port, "vbus=%d charge:=%d", port->vbus_source, charge);
4330 ret = port->tcpc->set_vbus(port->tcpc, port->vbus_source,
4335 port->vbus_charge = charge;
4336 power_supply_changed(port->psy);
4340 static bool tcpm_start_toggling(struct tcpm_port *port, enum typec_cc_status cc)
4344 if (!port->tcpc->start_toggling)
4347 tcpm_log_force(port, "Start toggling");
4348 ret = port->tcpc->start_toggling(port->tcpc, port->port_type, cc);
4352 static int tcpm_init_vbus(struct tcpm_port *port)
4356 ret = port->tcpc->set_vbus(port->tcpc, false, false);
4357 port->vbus_source = false;
4358 port->vbus_charge = false;
4362 static int tcpm_init_vconn(struct tcpm_port *port)
4366 ret = port->tcpc->set_vconn(port->tcpc, false);
4367 port->vconn_role = TYPEC_SINK;
4371 static void tcpm_typec_connect(struct tcpm_port *port)
4373 struct typec_partner *partner;
4375 if (!port->connected) {
4376 port->connected = true;
4377 /* Make sure we don't report stale identity information */
4378 memset(&port->partner_ident, 0, sizeof(port->partner_ident));
4379 port->partner_desc.usb_pd = port->pd_capable;
4380 if (tcpm_port_is_debug(port))
4381 port->partner_desc.accessory = TYPEC_ACCESSORY_DEBUG;
4382 else if (tcpm_port_is_audio(port))
4383 port->partner_desc.accessory = TYPEC_ACCESSORY_AUDIO;
4385 port->partner_desc.accessory = TYPEC_ACCESSORY_NONE;
4386 partner = typec_register_partner(port->typec_port, &port->partner_desc);
4387 if (IS_ERR(partner)) {
4388 dev_err(port->dev, "Failed to register partner (%ld)\n", PTR_ERR(partner));
4392 port->partner = partner;
4393 typec_partner_set_usb_power_delivery(port->partner, port->partner_pd);
4397 static int tcpm_src_attach(struct tcpm_port *port)
4399 enum typec_cc_polarity polarity =
4400 port->cc2 == TYPEC_CC_RD ? TYPEC_POLARITY_CC2
4401 : TYPEC_POLARITY_CC1;
4407 ret = tcpm_set_polarity(port, polarity);
4411 tcpm_enable_auto_vbus_discharge(port, true);
4414 * USB Type-C specification, version 1.2,
4415 * chapter 4.5.2.2.8.1 (Attached.SRC Requirements)
4416 * Enable VCONN only if the non-RD port is set to RA.
4418 if ((polarity == TYPEC_POLARITY_CC1 && port->cc2 == TYPEC_CC_RA) ||
4419 (polarity == TYPEC_POLARITY_CC2 && port->cc1 == TYPEC_CC_RA)) {
4420 ret = tcpm_set_vconn(port, true);
4425 ret = tcpm_set_vbus(port, true);
4427 goto out_disable_vconn;
4429 ret = tcpm_set_roles(port, true, TYPEC_STATE_USB, TYPEC_SOURCE,
4430 tcpm_data_role_for_source(port));
4432 goto out_disable_vbus;
4434 if (port->pd_supported) {
4435 ret = port->tcpc->set_pd_rx(port->tcpc, true);
4437 goto out_disable_mux;
4440 port->pd_capable = false;
4442 port->partner = NULL;
4444 port->attached = true;
4445 port->send_discover = true;
4446 port->send_discover_prime = false;
4451 tcpm_mux_set(port, TYPEC_STATE_SAFE, USB_ROLE_NONE,
4452 TYPEC_ORIENTATION_NONE);
4454 tcpm_set_vbus(port, false);
4456 tcpm_set_vconn(port, false);
4461 static void tcpm_typec_disconnect(struct tcpm_port *port)
4464 * Unregister plug/cable outside of port->connected because cable can
4465 * be discovered before SRC_READY/SNK_READY states where port->connected
4468 typec_unregister_plug(port->plug_prime);
4469 typec_unregister_cable(port->cable);
4470 port->plug_prime = NULL;
4472 if (port->connected) {
4473 if (port->partner) {
4474 typec_partner_set_usb_power_delivery(port->partner, NULL);
4475 typec_unregister_partner(port->partner);
4476 port->partner = NULL;
4478 port->connected = false;
4482 static void tcpm_unregister_altmodes(struct tcpm_port *port)
4484 struct pd_mode_data *modep = &port->mode_data;
4485 struct pd_mode_data *modep_prime = &port->mode_data_prime;
4488 for (i = 0; i < modep->altmodes; i++) {
4489 typec_unregister_altmode(port->partner_altmode[i]);
4490 port->partner_altmode[i] = NULL;
4492 for (i = 0; i < modep_prime->altmodes; i++) {
4493 typec_unregister_altmode(port->plug_prime_altmode[i]);
4494 port->plug_prime_altmode[i] = NULL;
4497 memset(modep, 0, sizeof(*modep));
4498 memset(modep_prime, 0, sizeof(*modep_prime));
4501 static void tcpm_set_partner_usb_comm_capable(struct tcpm_port *port, bool capable)
4503 tcpm_log(port, "Setting usb_comm capable %s", str_true_false(capable));
4505 if (port->tcpc->set_partner_usb_comm_capable)
4506 port->tcpc->set_partner_usb_comm_capable(port->tcpc, capable);
4509 static void tcpm_reset_port(struct tcpm_port *port)
4511 tcpm_enable_auto_vbus_discharge(port, false);
4512 port->in_ams = false;
4513 port->ams = NONE_AMS;
4514 port->vdm_sm_running = false;
4515 tcpm_unregister_altmodes(port);
4516 tcpm_typec_disconnect(port);
4517 port->attached = false;
4518 port->pd_capable = false;
4519 port->pps_data.supported = false;
4520 tcpm_set_partner_usb_comm_capable(port, false);
4523 * First Rx ID should be 0; set this to a sentinel of -1 so that
4524 * we can check tcpm_pd_rx_handler() if we had seen it before.
4526 port->rx_msgid = -1;
4527 port->rx_msgid_prime = -1;
4529 port->tcpc->set_pd_rx(port->tcpc, false);
4530 tcpm_init_vbus(port); /* also disables charging */
4531 tcpm_init_vconn(port);
4532 tcpm_set_current_limit(port, 0, 0);
4533 tcpm_set_polarity(port, TYPEC_POLARITY_CC1);
4534 tcpm_mux_set(port, TYPEC_STATE_SAFE, USB_ROLE_NONE,
4535 TYPEC_ORIENTATION_NONE);
4536 tcpm_set_attached_state(port, false);
4537 port->try_src_count = 0;
4538 port->try_snk_count = 0;
4539 port->usb_type = POWER_SUPPLY_USB_TYPE_C;
4540 power_supply_changed(port->psy);
4541 port->nr_sink_caps = 0;
4542 port->sink_cap_done = false;
4543 if (port->tcpc->enable_frs)
4544 port->tcpc->enable_frs(port->tcpc, false);
4546 usb_power_delivery_unregister_capabilities(port->partner_sink_caps);
4547 port->partner_sink_caps = NULL;
4548 usb_power_delivery_unregister_capabilities(port->partner_source_caps);
4549 port->partner_source_caps = NULL;
4550 usb_power_delivery_unregister(port->partner_pd);
4551 port->partner_pd = NULL;
4554 static void tcpm_detach(struct tcpm_port *port)
4556 if (tcpm_port_is_disconnected(port))
4557 port->hard_reset_count = 0;
4559 if (!port->attached)
4562 if (port->tcpc->set_bist_data) {
4563 tcpm_log(port, "disable BIST MODE TESTDATA");
4564 port->tcpc->set_bist_data(port->tcpc, false);
4567 tcpm_reset_port(port);
4570 static void tcpm_src_detach(struct tcpm_port *port)
4575 static int tcpm_snk_attach(struct tcpm_port *port)
4582 ret = tcpm_set_polarity(port, port->cc2 != TYPEC_CC_OPEN ?
4583 TYPEC_POLARITY_CC2 : TYPEC_POLARITY_CC1);
4587 tcpm_enable_auto_vbus_discharge(port, true);
4589 ret = tcpm_set_roles(port, true, TYPEC_STATE_USB,
4590 TYPEC_SINK, tcpm_data_role_for_sink(port));
4594 port->pd_capable = false;
4596 port->partner = NULL;
4598 port->attached = true;
4599 port->send_discover = true;
4600 port->send_discover_prime = false;
4605 static void tcpm_snk_detach(struct tcpm_port *port)
4610 static int tcpm_acc_attach(struct tcpm_port *port)
4613 enum typec_role role;
4614 enum typec_data_role data;
4615 int state = TYPEC_STATE_USB;
4620 role = tcpm_port_is_sink(port) ? TYPEC_SINK : TYPEC_SOURCE;
4621 data = tcpm_port_is_sink(port) ? tcpm_data_role_for_sink(port)
4622 : tcpm_data_role_for_source(port);
4624 if (tcpm_port_is_audio(port))
4625 state = TYPEC_MODE_AUDIO;
4627 if (tcpm_port_is_debug(port))
4628 state = TYPEC_MODE_DEBUG;
4630 ret = tcpm_set_roles(port, true, state, role, data);
4634 port->partner = NULL;
4636 tcpm_typec_connect(port);
4638 port->attached = true;
4643 static void tcpm_acc_detach(struct tcpm_port *port)
4648 static inline enum tcpm_state hard_reset_state(struct tcpm_port *port)
4650 if (port->hard_reset_count < PD_N_HARD_RESET_COUNT)
4651 return HARD_RESET_SEND;
4652 if (port->pd_capable)
4653 return ERROR_RECOVERY;
4654 if (port->pwr_role == TYPEC_SOURCE)
4655 return SRC_UNATTACHED;
4656 if (port->state == SNK_WAIT_CAPABILITIES ||
4657 port->state == SNK_WAIT_CAPABILITIES_TIMEOUT)
4659 return SNK_UNATTACHED;
4662 static inline enum tcpm_state unattached_state(struct tcpm_port *port)
4664 if (port->port_type == TYPEC_PORT_DRP) {
4665 if (port->pwr_role == TYPEC_SOURCE)
4666 return SRC_UNATTACHED;
4668 return SNK_UNATTACHED;
4669 } else if (port->port_type == TYPEC_PORT_SRC) {
4670 return SRC_UNATTACHED;
4673 return SNK_UNATTACHED;
4676 static void tcpm_swap_complete(struct tcpm_port *port, int result)
4678 if (port->swap_pending) {
4679 port->swap_status = result;
4680 port->swap_pending = false;
4681 port->non_pd_role_swap = false;
4682 complete(&port->swap_complete);
4686 static enum typec_pwr_opmode tcpm_get_pwr_opmode(enum typec_cc_status cc)
4689 case TYPEC_CC_RP_1_5:
4690 return TYPEC_PWR_MODE_1_5A;
4691 case TYPEC_CC_RP_3_0:
4692 return TYPEC_PWR_MODE_3_0A;
4693 case TYPEC_CC_RP_DEF:
4695 return TYPEC_PWR_MODE_USB;
4699 static enum typec_cc_status tcpm_pwr_opmode_to_rp(enum typec_pwr_opmode opmode)
4702 case TYPEC_PWR_MODE_USB:
4703 return TYPEC_CC_RP_DEF;
4704 case TYPEC_PWR_MODE_1_5A:
4705 return TYPEC_CC_RP_1_5;
4706 case TYPEC_PWR_MODE_3_0A:
4707 case TYPEC_PWR_MODE_PD:
4709 return TYPEC_CC_RP_3_0;
4713 static void tcpm_set_initial_svdm_version(struct tcpm_port *port)
4718 switch (port->negotiated_rev) {
4722 * 6.4.4.2.3 Structured VDM Version
4723 * 2.0 states "At this time, there is only one version (1.0) defined.
4724 * This field Shall be set to zero to indicate Version 1.0."
4725 * 3.0 states "This field Shall be set to 01b to indicate Version 2.0."
4726 * To ensure that we follow the Power Delivery revision we are currently
4727 * operating on, downgrade the SVDM version to the highest one supported
4728 * by the Power Delivery revision.
4731 typec_partner_set_svdm_version(port->partner, SVDM_VER_1_0);
4734 typec_partner_set_svdm_version(port->partner, SVDM_VER_1_0);
4739 static void tcpm_set_initial_negotiated_rev(struct tcpm_port *port)
4741 switch (port->pd_rev.rev_major) {
4743 port->negotiated_rev = PD_REV10;
4746 port->negotiated_rev = PD_REV20;
4749 port->negotiated_rev = PD_REV30;
4752 port->negotiated_rev = PD_MAX_REV;
4755 port->negotiated_rev_prime = port->negotiated_rev;
4758 static void run_state_machine(struct tcpm_port *port)
4761 enum typec_pwr_opmode opmode;
4763 enum tcpm_state upcoming_state;
4765 if (port->tcpc->check_contaminant && port->state != CHECK_CONTAMINANT)
4766 port->potential_contaminant = ((port->enter_state == SRC_ATTACH_WAIT &&
4767 port->state == SRC_UNATTACHED) ||
4768 (port->enter_state == SNK_ATTACH_WAIT &&
4769 port->state == SNK_UNATTACHED) ||
4770 (port->enter_state == SNK_DEBOUNCED &&
4771 port->state == SNK_UNATTACHED));
4773 port->enter_state = port->state;
4774 switch (port->state) {
4777 case CHECK_CONTAMINANT:
4778 port->tcpc->check_contaminant(port->tcpc);
4781 case SRC_UNATTACHED:
4782 if (!port->non_pd_role_swap)
4783 tcpm_swap_complete(port, -ENOTCONN);
4784 tcpm_src_detach(port);
4785 if (port->potential_contaminant) {
4786 tcpm_set_state(port, CHECK_CONTAMINANT, 0);
4789 if (tcpm_start_toggling(port, tcpm_rp_cc(port))) {
4790 tcpm_set_state(port, TOGGLING, 0);
4793 tcpm_set_cc(port, tcpm_rp_cc(port));
4794 if (port->port_type == TYPEC_PORT_DRP)
4795 tcpm_set_state(port, SNK_UNATTACHED, PD_T_DRP_SNK);
4797 case SRC_ATTACH_WAIT:
4798 if (tcpm_port_is_debug(port))
4799 tcpm_set_state(port, DEBUG_ACC_ATTACHED,
4800 port->timings.cc_debounce_time);
4801 else if (tcpm_port_is_audio(port))
4802 tcpm_set_state(port, AUDIO_ACC_ATTACHED,
4803 port->timings.cc_debounce_time);
4804 else if (tcpm_port_is_source(port) && port->vbus_vsafe0v)
4805 tcpm_set_state(port,
4806 tcpm_try_snk(port) ? SNK_TRY
4808 port->timings.cc_debounce_time);
4812 port->try_snk_count++;
4815 * - Do not drive vconn or vbus
4816 * - Terminate CC pins (both) to Rd
4818 * - Wait for tDRPTry (PD_T_DRP_TRY).
4819 * Until then, ignore any state changes.
4821 tcpm_set_cc(port, TYPEC_CC_RD);
4822 tcpm_set_state(port, SNK_TRY_WAIT, PD_T_DRP_TRY);
4825 if (tcpm_port_is_sink(port)) {
4826 tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE, 0);
4828 tcpm_set_state(port, SRC_TRYWAIT, 0);
4832 case SNK_TRY_WAIT_DEBOUNCE:
4833 tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS,
4834 PD_T_TRY_CC_DEBOUNCE);
4836 case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS:
4837 if (port->vbus_present && tcpm_port_is_sink(port))
4838 tcpm_set_state(port, SNK_ATTACHED, 0);
4843 tcpm_set_cc(port, tcpm_rp_cc(port));
4844 if (port->max_wait == 0) {
4845 port->max_wait = jiffies +
4846 msecs_to_jiffies(PD_T_DRP_TRY);
4847 tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED,
4850 if (time_is_after_jiffies(port->max_wait))
4851 tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED,
4852 jiffies_to_msecs(port->max_wait -
4855 tcpm_set_state(port, SNK_UNATTACHED, 0);
4858 case SRC_TRYWAIT_DEBOUNCE:
4859 tcpm_set_state(port, SRC_ATTACHED, port->timings.cc_debounce_time);
4861 case SRC_TRYWAIT_UNATTACHED:
4862 tcpm_set_state(port, SNK_UNATTACHED, 0);
4866 ret = tcpm_src_attach(port);
4867 tcpm_set_state(port, SRC_UNATTACHED,
4868 ret < 0 ? 0 : PD_T_PS_SOURCE_ON);
4871 opmode = tcpm_get_pwr_opmode(tcpm_rp_cc(port));
4872 typec_set_pwr_opmode(port->typec_port, opmode);
4873 port->pwr_opmode = TYPEC_PWR_MODE_USB;
4874 port->caps_count = 0;
4875 tcpm_set_initial_negotiated_rev(port);
4876 port->message_id = 0;
4877 port->message_id_prime = 0;
4878 port->rx_msgid = -1;
4879 port->rx_msgid_prime = -1;
4880 port->explicit_contract = false;
4881 /* SNK -> SRC POWER/FAST_ROLE_SWAP finished */
4882 if (port->ams == POWER_ROLE_SWAP ||
4883 port->ams == FAST_ROLE_SWAP)
4884 tcpm_ams_finish(port);
4885 if (!port->pd_supported) {
4886 tcpm_set_state(port, SRC_READY, 0);
4889 port->upcoming_state = SRC_SEND_CAPABILITIES;
4890 tcpm_ams_start(port, POWER_NEGOTIATION);
4892 case SRC_SEND_CAPABILITIES:
4894 if (port->caps_count > PD_N_CAPS_COUNT) {
4895 tcpm_set_state(port, SRC_READY, 0);
4898 ret = tcpm_pd_send_source_caps(port);
4900 if (tcpm_can_communicate_sop_prime(port) &&
4901 IS_ERR_OR_NULL(port->cable))
4902 tcpm_set_state(port, SRC_VDM_IDENTITY_REQUEST, 0);
4904 tcpm_set_state(port, SRC_SEND_CAPABILITIES,
4905 PD_T_SEND_SOURCE_CAP);
4908 * Per standard, we should clear the reset counter here.
4909 * However, that can result in state machine hang-ups.
4910 * Reset it only in READY state to improve stability.
4912 /* port->hard_reset_count = 0; */
4913 port->caps_count = 0;
4914 port->pd_capable = true;
4915 tcpm_set_state_cond(port, SRC_SEND_CAPABILITIES_TIMEOUT,
4916 PD_T_SENDER_RESPONSE);
4919 case SRC_SEND_CAPABILITIES_TIMEOUT:
4921 * Error recovery for a PD_DATA_SOURCE_CAP reply timeout.
4923 * PD 2.0 sinks are supposed to accept src-capabilities with a
4924 * 3.0 header and simply ignore any src PDOs which the sink does
4925 * not understand such as PPS but some 2.0 sinks instead ignore
4926 * the entire PD_DATA_SOURCE_CAP message, causing contract
4927 * negotiation to fail.
4929 * After PD_N_HARD_RESET_COUNT hard-reset attempts, we try
4930 * sending src-capabilities with a lower PD revision to
4931 * make these broken sinks work.
4933 if (port->hard_reset_count < PD_N_HARD_RESET_COUNT) {
4934 tcpm_set_state(port, HARD_RESET_SEND, 0);
4935 } else if (port->negotiated_rev > PD_REV20) {
4936 port->negotiated_rev--;
4937 port->hard_reset_count = 0;
4938 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
4940 tcpm_set_state(port, hard_reset_state(port), 0);
4943 case SRC_NEGOTIATE_CAPABILITIES:
4944 ret = tcpm_pd_check_request(port);
4946 tcpm_pd_send_control(port, PD_CTRL_REJECT, TCPC_TX_SOP);
4947 if (!port->explicit_contract) {
4948 tcpm_set_state(port,
4949 SRC_WAIT_NEW_CAPABILITIES, 0);
4951 tcpm_set_state(port, SRC_READY, 0);
4954 tcpm_pd_send_control(port, PD_CTRL_ACCEPT, TCPC_TX_SOP);
4955 tcpm_set_partner_usb_comm_capable(port,
4956 !!(port->sink_request & RDO_USB_COMM));
4957 tcpm_set_state(port, SRC_TRANSITION_SUPPLY,
4958 PD_T_SRC_TRANSITION);
4961 case SRC_TRANSITION_SUPPLY:
4962 /* XXX: regulator_set_voltage(vbus, ...) */
4963 tcpm_pd_send_control(port, PD_CTRL_PS_RDY, TCPC_TX_SOP);
4964 port->explicit_contract = true;
4965 typec_set_pwr_opmode(port->typec_port, TYPEC_PWR_MODE_PD);
4966 port->pwr_opmode = TYPEC_PWR_MODE_PD;
4967 tcpm_set_state_cond(port, SRC_READY, 0);
4971 port->hard_reset_count = 0;
4973 port->try_src_count = 0;
4975 tcpm_swap_complete(port, 0);
4976 tcpm_typec_connect(port);
4978 if (port->ams != NONE_AMS)
4979 tcpm_ams_finish(port);
4980 if (port->next_ams != NONE_AMS) {
4981 port->ams = port->next_ams;
4982 port->next_ams = NONE_AMS;
4986 * If previous AMS is interrupted, switch to the upcoming
4989 if (port->upcoming_state != INVALID_STATE) {
4990 upcoming_state = port->upcoming_state;
4991 port->upcoming_state = INVALID_STATE;
4992 tcpm_set_state(port, upcoming_state, 0);
4997 * 6.4.4.3.1 Discover Identity
4998 * "The Discover Identity Command Shall only be sent to SOP when there is an
4999 * Explicit Contract."
5001 * Discover Identity on SOP' should be discovered prior to the
5002 * ready state, but if done after a Vconn Swap following Discover
5003 * Identity on SOP then the discovery process can be run here
5006 if (port->explicit_contract) {
5007 if (port->send_discover_prime) {
5008 port->tx_sop_type = TCPC_TX_SOP_PRIME;
5010 port->tx_sop_type = TCPC_TX_SOP;
5011 tcpm_set_initial_svdm_version(port);
5013 mod_send_discover_delayed_work(port, 0);
5015 port->send_discover = false;
5016 port->send_discover_prime = false;
5021 * Sending ping messages is not necessary if
5022 * - the source operates at vSafe5V
5024 * - The system is not operating in PD mode
5026 * - Both partners are connected using a Type-C connector
5028 * There is no actual need to send PD messages since the local
5029 * port type-c and the spec does not clearly say whether PD is
5030 * possible when type-c is connected to Type-A/B
5033 case SRC_WAIT_NEW_CAPABILITIES:
5034 /* Nothing to do... */
5038 case SNK_UNATTACHED:
5039 if (!port->non_pd_role_swap)
5040 tcpm_swap_complete(port, -ENOTCONN);
5041 tcpm_pps_complete(port, -ENOTCONN);
5042 tcpm_snk_detach(port);
5043 if (port->potential_contaminant) {
5044 tcpm_set_state(port, CHECK_CONTAMINANT, 0);
5047 if (tcpm_start_toggling(port, TYPEC_CC_RD)) {
5048 tcpm_set_state(port, TOGGLING, 0);
5051 tcpm_set_cc(port, TYPEC_CC_RD);
5052 if (port->port_type == TYPEC_PORT_DRP)
5053 tcpm_set_state(port, SRC_UNATTACHED, PD_T_DRP_SRC);
5055 case SNK_ATTACH_WAIT:
5056 if (tcpm_port_is_debug(port))
5057 tcpm_set_state(port, DEBUG_ACC_ATTACHED,
5059 else if (tcpm_port_is_audio(port))
5060 tcpm_set_state(port, AUDIO_ACC_ATTACHED,
5062 else if ((port->cc1 == TYPEC_CC_OPEN &&
5063 port->cc2 != TYPEC_CC_OPEN) ||
5064 (port->cc1 != TYPEC_CC_OPEN &&
5065 port->cc2 == TYPEC_CC_OPEN))
5066 tcpm_set_state(port, SNK_DEBOUNCED,
5067 port->timings.cc_debounce_time);
5068 else if (tcpm_port_is_disconnected(port))
5069 tcpm_set_state(port, SNK_UNATTACHED,
5073 if (tcpm_port_is_disconnected(port))
5074 tcpm_set_state(port, SNK_UNATTACHED,
5076 else if (tcpm_port_is_debug(port))
5077 tcpm_set_state(port, DEBUG_ACC_ATTACHED,
5079 else if (tcpm_port_is_audio(port))
5080 tcpm_set_state(port, AUDIO_ACC_ATTACHED,
5082 else if (port->vbus_present)
5083 tcpm_set_state(port,
5084 tcpm_try_src(port) ? SRC_TRY
5089 port->try_src_count++;
5090 tcpm_set_cc(port, tcpm_rp_cc(port));
5092 tcpm_set_state(port, SRC_TRY_WAIT, 0);
5095 if (port->max_wait == 0) {
5096 port->max_wait = jiffies +
5097 msecs_to_jiffies(PD_T_DRP_TRY);
5098 msecs = PD_T_DRP_TRY;
5100 if (time_is_after_jiffies(port->max_wait))
5101 msecs = jiffies_to_msecs(port->max_wait -
5106 tcpm_set_state(port, SNK_TRYWAIT, msecs);
5108 case SRC_TRY_DEBOUNCE:
5109 tcpm_set_state(port, SRC_ATTACHED, PD_T_PD_DEBOUNCE);
5112 tcpm_set_cc(port, TYPEC_CC_RD);
5113 tcpm_set_state(port, SNK_TRYWAIT_VBUS, port->timings.cc_debounce_time);
5115 case SNK_TRYWAIT_VBUS:
5117 * TCPM stays in this state indefinitely until VBUS
5118 * is detected as long as Rp is not detected for
5119 * more than a time period of tPDDebounce.
5121 if (port->vbus_present && tcpm_port_is_sink(port)) {
5122 tcpm_set_state(port, SNK_ATTACHED, 0);
5125 if (!tcpm_port_is_sink(port))
5126 tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, 0);
5128 case SNK_TRYWAIT_DEBOUNCE:
5129 tcpm_set_state(port, SNK_UNATTACHED, PD_T_PD_DEBOUNCE);
5132 ret = tcpm_snk_attach(port);
5134 tcpm_set_state(port, SNK_UNATTACHED, 0);
5137 * For Type C port controllers that use Battery Charging
5138 * Detection (based on BCv1.2 spec) to detect USB
5139 * charger type, add a delay of "snk_bc12_cmpletion_time"
5140 * before transitioning to SNK_STARTUP to allow BC1.2
5141 * detection to complete before PD is eventually enabled
5144 tcpm_set_state(port, SNK_STARTUP,
5145 port->timings.snk_bc12_cmpletion_time);
5148 opmode = tcpm_get_pwr_opmode(port->polarity ?
5149 port->cc2 : port->cc1);
5150 typec_set_pwr_opmode(port->typec_port, opmode);
5151 port->pwr_opmode = TYPEC_PWR_MODE_USB;
5152 tcpm_set_initial_negotiated_rev(port);
5153 port->message_id = 0;
5154 port->message_id_prime = 0;
5155 port->rx_msgid = -1;
5156 port->rx_msgid_prime = -1;
5157 port->explicit_contract = false;
5159 if (port->ams == POWER_ROLE_SWAP ||
5160 port->ams == FAST_ROLE_SWAP)
5161 /* SRC -> SNK POWER/FAST_ROLE_SWAP finished */
5162 tcpm_ams_finish(port);
5164 tcpm_set_state(port, SNK_DISCOVERY, 0);
5167 if (port->vbus_present) {
5168 u32 current_lim = tcpm_get_current_limit(port);
5170 if (port->slow_charger_loop && (current_lim > PD_P_SNK_STDBY_MW / 5))
5171 current_lim = PD_P_SNK_STDBY_MW / 5;
5172 tcpm_set_current_limit(port, current_lim, 5000);
5173 /* Not sink vbus if operational current is 0mA */
5174 tcpm_set_charge(port, !port->pd_supported ||
5175 pdo_max_current(port->snk_pdo[0]));
5177 if (!port->pd_supported)
5178 tcpm_set_state(port, SNK_READY, 0);
5180 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
5184 * For DRP, timeouts differ. Also, handling is supposed to be
5185 * different and much more complex (dead battery detection;
5186 * see USB power delivery specification, section 8.3.3.6.1.5.1).
5188 tcpm_set_state(port, hard_reset_state(port),
5189 port->port_type == TYPEC_PORT_DRP ?
5190 PD_T_DB_DETECT : PD_T_NO_RESPONSE);
5192 case SNK_DISCOVERY_DEBOUNCE:
5193 tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE_DONE,
5194 port->timings.cc_debounce_time);
5196 case SNK_DISCOVERY_DEBOUNCE_DONE:
5197 if (!tcpm_port_is_disconnected(port) &&
5198 tcpm_port_is_sink(port) &&
5199 ktime_after(port->delayed_runtime, ktime_get())) {
5200 tcpm_set_state(port, SNK_DISCOVERY,
5201 ktime_to_ms(ktime_sub(port->delayed_runtime, ktime_get())));
5204 tcpm_set_state(port, unattached_state(port), 0);
5206 case SNK_WAIT_CAPABILITIES:
5207 ret = port->tcpc->set_pd_rx(port->tcpc, true);
5209 tcpm_set_state(port, SNK_READY, 0);
5213 * If VBUS has never been low, and we time out waiting
5214 * for source cap, try a soft reset first, in case we
5215 * were already in a stable contract before this boot.
5216 * Do this only once.
5218 if (port->vbus_never_low) {
5219 port->vbus_never_low = false;
5220 upcoming_state = SNK_SOFT_RESET;
5222 if (!port->self_powered)
5223 upcoming_state = SNK_WAIT_CAPABILITIES_TIMEOUT;
5225 upcoming_state = hard_reset_state(port);
5228 tcpm_set_state(port, upcoming_state,
5229 port->timings.sink_wait_cap_time);
5231 case SNK_WAIT_CAPABILITIES_TIMEOUT:
5233 * There are some USB PD sources in the field, which do not
5234 * properly implement the specification and fail to start
5235 * sending Source Capability messages after a soft reset. The
5236 * specification suggests to do a hard reset when no Source
5237 * capability message is received within PD_T_SINK_WAIT_CAP,
5238 * but that might effectively kil the machine's power source.
5240 * This slightly diverges from the specification and tries to
5241 * recover from this by explicitly asking for the capabilities
5242 * using the Get_Source_Cap control message before falling back
5243 * to a hard reset. The control message should also be supported
5244 * and handled by all USB PD source and dual role devices
5245 * according to the specification.
5247 if (tcpm_pd_send_control(port, PD_CTRL_GET_SOURCE_CAP, TCPC_TX_SOP))
5248 tcpm_set_state_cond(port, hard_reset_state(port), 0);
5250 tcpm_set_state(port, hard_reset_state(port),
5251 port->timings.sink_wait_cap_time);
5253 case SNK_NEGOTIATE_CAPABILITIES:
5254 port->pd_capable = true;
5255 tcpm_set_partner_usb_comm_capable(port,
5256 !!(port->source_caps[0] & PDO_FIXED_USB_COMM));
5257 port->hard_reset_count = 0;
5258 ret = tcpm_pd_send_request(port);
5260 /* Restore back to the original state */
5261 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
5262 port->pps_data.active,
5263 port->supply_voltage);
5264 /* Let the Source send capabilities again. */
5265 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
5267 tcpm_set_state_cond(port, hard_reset_state(port),
5268 PD_T_SENDER_RESPONSE);
5271 case SNK_NEGOTIATE_PPS_CAPABILITIES:
5272 ret = tcpm_pd_send_pps_request(port);
5274 /* Restore back to the original state */
5275 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
5276 port->pps_data.active,
5277 port->supply_voltage);
5278 port->pps_status = ret;
5280 * If this was called due to updates to sink
5281 * capabilities, and pps is no longer valid, we should
5282 * safely fall back to a standard PDO.
5284 if (port->update_sink_caps)
5285 tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
5287 tcpm_set_state(port, SNK_READY, 0);
5289 tcpm_set_state_cond(port, hard_reset_state(port),
5290 PD_T_SENDER_RESPONSE);
5293 case SNK_TRANSITION_SINK:
5294 /* From the USB PD spec:
5295 * "The Sink Shall transition to Sink Standby before a positive or
5296 * negative voltage transition of VBUS. During Sink Standby
5297 * the Sink Shall reduce its power draw to pSnkStdby."
5299 * This is not applicable to PPS though as the port can continue
5300 * to draw negotiated power without switching to standby.
5302 if (port->supply_voltage != port->req_supply_voltage && !port->pps_data.active &&
5303 port->current_limit * port->supply_voltage / 1000 > PD_P_SNK_STDBY_MW) {
5304 u32 stdby_ma = PD_P_SNK_STDBY_MW * 1000 / port->supply_voltage;
5306 tcpm_log(port, "Setting standby current %u mV @ %u mA",
5307 port->supply_voltage, stdby_ma);
5308 tcpm_set_current_limit(port, stdby_ma, port->supply_voltage);
5311 case SNK_TRANSITION_SINK_VBUS:
5312 tcpm_set_state(port, hard_reset_state(port),
5313 PD_T_PS_TRANSITION);
5316 port->try_snk_count = 0;
5317 port->update_sink_caps = false;
5318 if (port->explicit_contract) {
5319 typec_set_pwr_opmode(port->typec_port,
5321 port->pwr_opmode = TYPEC_PWR_MODE_PD;
5324 if (!port->pd_capable && port->slow_charger_loop)
5325 tcpm_set_current_limit(port, tcpm_get_current_limit(port), 5000);
5326 tcpm_swap_complete(port, 0);
5327 tcpm_typec_connect(port);
5328 if (port->pd_capable && port->source_caps[0] & PDO_FIXED_DUAL_ROLE)
5329 mod_enable_frs_delayed_work(port, 0);
5330 tcpm_pps_complete(port, port->pps_status);
5332 if (port->ams != NONE_AMS)
5333 tcpm_ams_finish(port);
5334 if (port->next_ams != NONE_AMS) {
5335 port->ams = port->next_ams;
5336 port->next_ams = NONE_AMS;
5340 * If previous AMS is interrupted, switch to the upcoming
5343 if (port->upcoming_state != INVALID_STATE) {
5344 upcoming_state = port->upcoming_state;
5345 port->upcoming_state = INVALID_STATE;
5346 tcpm_set_state(port, upcoming_state, 0);
5351 * 6.4.4.3.1 Discover Identity
5352 * "The Discover Identity Command Shall only be sent to SOP when there is an
5353 * Explicit Contract."
5355 * Discover Identity on SOP' should be discovered prior to the
5356 * ready state, but if done after a Vconn Swap following Discover
5357 * Identity on SOP then the discovery process can be run here
5360 if (port->explicit_contract) {
5361 if (port->send_discover_prime) {
5362 port->tx_sop_type = TCPC_TX_SOP_PRIME;
5364 port->tx_sop_type = TCPC_TX_SOP;
5365 tcpm_set_initial_svdm_version(port);
5367 mod_send_discover_delayed_work(port, 0);
5369 port->send_discover = false;
5370 port->send_discover_prime = false;
5373 power_supply_changed(port->psy);
5376 /* Accessory states */
5377 case ACC_UNATTACHED:
5378 tcpm_acc_detach(port);
5379 if (port->port_type == TYPEC_PORT_SRC)
5380 tcpm_set_state(port, SRC_UNATTACHED, 0);
5382 tcpm_set_state(port, SNK_UNATTACHED, 0);
5384 case DEBUG_ACC_ATTACHED:
5385 case AUDIO_ACC_ATTACHED:
5386 ret = tcpm_acc_attach(port);
5388 tcpm_set_state(port, ACC_UNATTACHED, 0);
5390 case DEBUG_ACC_DEBOUNCE:
5391 case AUDIO_ACC_DEBOUNCE:
5392 tcpm_set_state(port, ACC_UNATTACHED, port->timings.cc_debounce_time);
5395 /* Hard_Reset states */
5396 case HARD_RESET_SEND:
5397 if (port->ams != NONE_AMS)
5398 tcpm_ams_finish(port);
5399 if (!port->self_powered && port->port_type == TYPEC_PORT_SNK)
5400 dev_err(port->dev, "Initiating hard-reset, which might result in machine power-loss.\n");
5402 * State machine will be directed to HARD_RESET_START,
5403 * thus set upcoming_state to INVALID_STATE.
5405 port->upcoming_state = INVALID_STATE;
5406 tcpm_ams_start(port, HARD_RESET);
5408 case HARD_RESET_START:
5409 port->sink_cap_done = false;
5410 if (port->tcpc->enable_frs)
5411 port->tcpc->enable_frs(port->tcpc, false);
5412 port->hard_reset_count++;
5413 port->tcpc->set_pd_rx(port->tcpc, false);
5414 tcpm_unregister_altmodes(port);
5415 port->nr_sink_caps = 0;
5416 port->send_discover = true;
5417 port->send_discover_prime = false;
5418 if (port->pwr_role == TYPEC_SOURCE)
5419 tcpm_set_state(port, SRC_HARD_RESET_VBUS_OFF,
5420 PD_T_PS_HARD_RESET);
5422 tcpm_set_state(port, SNK_HARD_RESET_SINK_OFF, 0);
5424 case SRC_HARD_RESET_VBUS_OFF:
5426 * 7.1.5 Response to Hard Resets
5427 * Hard Reset Signaling indicates a communication failure has occurred and the
5428 * Source Shall stop driving VCONN, Shall remove Rp from the VCONN pin and Shall
5429 * drive VBUS to vSafe0V as shown in Figure 7-9.
5431 tcpm_set_vconn(port, false);
5432 tcpm_set_vbus(port, false);
5433 tcpm_set_roles(port, port->self_powered, TYPEC_STATE_USB, TYPEC_SOURCE,
5434 tcpm_data_role_for_source(port));
5436 * If tcpc fails to notify vbus off, TCPM will wait for PD_T_SAFE_0V +
5437 * PD_T_SRC_RECOVER before turning vbus back on.
5438 * From Table 7-12 Sequence Description for a Source Initiated Hard Reset:
5439 * 4. Policy Engine waits tPSHardReset after sending Hard Reset Signaling and then
5440 * tells the Device Policy Manager to instruct the power supply to perform a
5441 * Hard Reset. The transition to vSafe0V Shall occur within tSafe0V (t2).
5442 * 5. After tSrcRecover the Source applies power to VBUS in an attempt to
5443 * re-establish communication with the Sink and resume USB Default Operation.
5444 * The transition to vSafe5V Shall occur within tSrcTurnOn(t4).
5446 tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SAFE_0V + PD_T_SRC_RECOVER);
5448 case SRC_HARD_RESET_VBUS_ON:
5449 tcpm_set_vconn(port, true);
5450 tcpm_set_vbus(port, true);
5451 if (port->ams == HARD_RESET)
5452 tcpm_ams_finish(port);
5453 if (port->pd_supported)
5454 port->tcpc->set_pd_rx(port->tcpc, true);
5455 tcpm_set_attached_state(port, true);
5456 tcpm_set_state(port, SRC_UNATTACHED, PD_T_PS_SOURCE_ON);
5458 case SNK_HARD_RESET_SINK_OFF:
5459 /* Do not discharge/disconnect during hard reset */
5460 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0);
5461 memset(&port->pps_data, 0, sizeof(port->pps_data));
5462 tcpm_set_vconn(port, false);
5463 if (port->pd_capable)
5464 tcpm_set_charge(port, false);
5465 tcpm_set_roles(port, port->self_powered, TYPEC_STATE_USB, TYPEC_SINK,
5466 tcpm_data_role_for_sink(port));
5468 * VBUS may or may not toggle, depending on the adapter.
5469 * If it doesn't toggle, transition to SNK_HARD_RESET_SINK_ON
5470 * directly after timeout.
5472 tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, PD_T_SAFE_0V);
5474 case SNK_HARD_RESET_WAIT_VBUS:
5475 if (port->ams == HARD_RESET)
5476 tcpm_ams_finish(port);
5477 /* Assume we're disconnected if VBUS doesn't come back. */
5478 tcpm_set_state(port, SNK_UNATTACHED,
5479 PD_T_SRC_RECOVER_MAX + PD_T_SRC_TURN_ON);
5481 case SNK_HARD_RESET_SINK_ON:
5482 /* Note: There is no guarantee that VBUS is on in this state */
5485 * The specification suggests that dual mode ports in sink
5486 * mode should transition to state PE_SRC_Transition_to_default.
5487 * See USB power delivery specification chapter 8.3.3.6.1.3.
5488 * This would mean to
5489 * - turn off VCONN, reset power supply
5490 * - request hardware reset
5492 * - Transition to state PE_Src_Startup
5493 * SNK only ports shall transition to state Snk_Startup
5494 * (see chapter 8.3.3.3.8).
5495 * Similar, dual-mode ports in source mode should transition
5496 * to PE_SNK_Transition_to_default.
5498 if (port->pd_capable) {
5499 tcpm_set_current_limit(port,
5500 tcpm_get_current_limit(port),
5502 /* Not sink vbus if operational current is 0mA */
5503 tcpm_set_charge(port, !!pdo_max_current(port->snk_pdo[0]));
5505 if (port->ams == HARD_RESET)
5506 tcpm_ams_finish(port);
5507 tcpm_set_attached_state(port, true);
5508 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, VSAFE5V);
5509 tcpm_set_state(port, SNK_STARTUP, 0);
5512 /* Soft_Reset states */
5514 port->message_id = 0;
5515 port->rx_msgid = -1;
5516 /* remove existing capabilities */
5517 usb_power_delivery_unregister_capabilities(port->partner_source_caps);
5518 port->partner_source_caps = NULL;
5519 tcpm_pd_send_control(port, PD_CTRL_ACCEPT, TCPC_TX_SOP);
5520 tcpm_ams_finish(port);
5521 if (port->pwr_role == TYPEC_SOURCE) {
5522 port->upcoming_state = SRC_SEND_CAPABILITIES;
5523 tcpm_ams_start(port, POWER_NEGOTIATION);
5525 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
5528 case SRC_SOFT_RESET_WAIT_SNK_TX:
5529 case SNK_SOFT_RESET:
5530 if (port->ams != NONE_AMS)
5531 tcpm_ams_finish(port);
5532 port->upcoming_state = SOFT_RESET_SEND;
5533 tcpm_ams_start(port, SOFT_RESET_AMS);
5535 case SOFT_RESET_SEND:
5537 * Power Delivery 3.0 Section 6.3.13
5539 * A Soft_Reset Message Shall be targeted at a specific entity
5540 * depending on the type of SOP* packet used.
5542 if (port->tx_sop_type == TCPC_TX_SOP_PRIME) {
5543 port->message_id_prime = 0;
5544 port->rx_msgid_prime = -1;
5545 tcpm_pd_send_control(port, PD_CTRL_SOFT_RESET, TCPC_TX_SOP_PRIME);
5546 tcpm_set_state_cond(port, ready_state(port), PD_T_SENDER_RESPONSE);
5548 port->message_id = 0;
5549 port->rx_msgid = -1;
5550 /* remove existing capabilities */
5551 usb_power_delivery_unregister_capabilities(port->partner_source_caps);
5552 port->partner_source_caps = NULL;
5553 if (tcpm_pd_send_control(port, PD_CTRL_SOFT_RESET, TCPC_TX_SOP))
5554 tcpm_set_state_cond(port, hard_reset_state(port), 0);
5556 tcpm_set_state_cond(port, hard_reset_state(port),
5557 PD_T_SENDER_RESPONSE);
5561 /* DR_Swap states */
5563 tcpm_pd_send_control(port, PD_CTRL_DR_SWAP, TCPC_TX_SOP);
5564 if (port->data_role == TYPEC_DEVICE || port->negotiated_rev > PD_REV20) {
5565 port->send_discover = true;
5566 port->send_discover_prime = false;
5568 tcpm_set_state_cond(port, DR_SWAP_SEND_TIMEOUT,
5569 PD_T_SENDER_RESPONSE);
5571 case DR_SWAP_ACCEPT:
5572 tcpm_pd_send_control(port, PD_CTRL_ACCEPT, TCPC_TX_SOP);
5573 if (port->data_role == TYPEC_DEVICE || port->negotiated_rev > PD_REV20) {
5574 port->send_discover = true;
5575 port->send_discover_prime = false;
5577 tcpm_set_state_cond(port, DR_SWAP_CHANGE_DR, 0);
5579 case DR_SWAP_SEND_TIMEOUT:
5580 tcpm_swap_complete(port, -ETIMEDOUT);
5581 port->send_discover = false;
5582 port->send_discover_prime = false;
5583 tcpm_ams_finish(port);
5584 tcpm_set_state(port, ready_state(port), 0);
5586 case DR_SWAP_CHANGE_DR:
5587 tcpm_unregister_altmodes(port);
5588 if (port->data_role == TYPEC_HOST)
5589 tcpm_set_roles(port, true, TYPEC_STATE_USB, port->pwr_role,
5592 tcpm_set_roles(port, true, TYPEC_STATE_USB, port->pwr_role,
5594 tcpm_ams_finish(port);
5595 tcpm_set_state(port, ready_state(port), 0);
5599 if (tcpm_pd_send_control(port, PD_CTRL_FR_SWAP, TCPC_TX_SOP)) {
5600 tcpm_set_state(port, ERROR_RECOVERY, 0);
5603 tcpm_set_state_cond(port, FR_SWAP_SEND_TIMEOUT, PD_T_SENDER_RESPONSE);
5605 case FR_SWAP_SEND_TIMEOUT:
5606 tcpm_set_state(port, ERROR_RECOVERY, 0);
5608 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
5609 tcpm_set_state(port, ERROR_RECOVERY, port->timings.ps_src_off_time);
5611 case FR_SWAP_SNK_SRC_NEW_SINK_READY:
5612 if (port->vbus_source)
5613 tcpm_set_state(port, FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED, 0);
5615 tcpm_set_state(port, ERROR_RECOVERY, PD_T_RECEIVER_RESPONSE);
5617 case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED:
5618 tcpm_set_pwr_role(port, TYPEC_SOURCE);
5619 if (tcpm_pd_send_control(port, PD_CTRL_PS_RDY, TCPC_TX_SOP)) {
5620 tcpm_set_state(port, ERROR_RECOVERY, 0);
5623 tcpm_set_cc(port, tcpm_rp_cc(port));
5624 tcpm_set_state(port, SRC_STARTUP, PD_T_SWAP_SRC_START);
5627 /* PR_Swap states */
5628 case PR_SWAP_ACCEPT:
5629 tcpm_pd_send_control(port, PD_CTRL_ACCEPT, TCPC_TX_SOP);
5630 tcpm_set_state(port, PR_SWAP_START, 0);
5633 tcpm_pd_send_control(port, PD_CTRL_PR_SWAP, TCPC_TX_SOP);
5634 tcpm_set_state_cond(port, PR_SWAP_SEND_TIMEOUT,
5635 PD_T_SENDER_RESPONSE);
5637 case PR_SWAP_SEND_TIMEOUT:
5638 tcpm_swap_complete(port, -ETIMEDOUT);
5639 tcpm_set_state(port, ready_state(port), 0);
5642 tcpm_apply_rc(port);
5643 if (port->pwr_role == TYPEC_SOURCE)
5644 tcpm_set_state(port, PR_SWAP_SRC_SNK_TRANSITION_OFF,
5645 PD_T_SRC_TRANSITION);
5647 tcpm_set_state(port, PR_SWAP_SNK_SRC_SINK_OFF, 0);
5649 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
5651 * Prevent vbus discharge circuit from turning on during PR_SWAP
5652 * as this is not a disconnect.
5654 tcpm_set_vbus(port, false);
5655 port->explicit_contract = false;
5656 /* allow time for Vbus discharge, must be < tSrcSwapStdby */
5657 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF,
5660 case PR_SWAP_SRC_SNK_SOURCE_OFF:
5661 tcpm_set_cc(port, TYPEC_CC_RD);
5662 /* allow CC debounce */
5663 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED,
5664 port->timings.cc_debounce_time);
5666 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED:
5668 * USB-PD standard, 6.2.1.4, Port Power Role:
5669 * "During the Power Role Swap Sequence, for the initial Source
5670 * Port, the Port Power Role field shall be set to Sink in the
5671 * PS_RDY Message indicating that the initial Source’s power
5672 * supply is turned off"
5674 tcpm_set_pwr_role(port, TYPEC_SINK);
5675 if (tcpm_pd_send_control(port, PD_CTRL_PS_RDY, TCPC_TX_SOP)) {
5676 tcpm_set_state(port, ERROR_RECOVERY, 0);
5679 tcpm_set_state(port, ERROR_RECOVERY, PD_T_PS_SOURCE_ON_PRS);
5681 case PR_SWAP_SRC_SNK_SINK_ON:
5682 tcpm_enable_auto_vbus_discharge(port, true);
5683 /* Set the vbus disconnect threshold for implicit contract */
5684 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, VSAFE5V);
5685 tcpm_set_state(port, SNK_STARTUP, 0);
5687 case PR_SWAP_SNK_SRC_SINK_OFF:
5688 /* will be source, remove existing capabilities */
5689 usb_power_delivery_unregister_capabilities(port->partner_source_caps);
5690 port->partner_source_caps = NULL;
5692 * Prevent vbus discharge circuit from turning on during PR_SWAP
5693 * as this is not a disconnect.
5695 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB,
5696 port->pps_data.active, 0);
5697 tcpm_set_charge(port, false);
5698 tcpm_set_state(port, ERROR_RECOVERY, port->timings.ps_src_off_time);
5700 case PR_SWAP_SNK_SRC_SOURCE_ON:
5701 tcpm_enable_auto_vbus_discharge(port, true);
5702 tcpm_set_cc(port, tcpm_rp_cc(port));
5703 tcpm_set_vbus(port, true);
5705 * allow time VBUS ramp-up, must be < tNewSrc
5706 * Also, this window overlaps with CC debounce as well.
5707 * So, Wait for the max of two which is PD_T_NEWSRC
5709 tcpm_set_state(port, PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP,
5712 case PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP:
5714 * USB PD standard, 6.2.1.4:
5715 * "Subsequent Messages initiated by the Policy Engine,
5716 * such as the PS_RDY Message sent to indicate that Vbus
5717 * is ready, will have the Port Power Role field set to
5720 tcpm_set_pwr_role(port, TYPEC_SOURCE);
5721 tcpm_pd_send_control(port, PD_CTRL_PS_RDY, TCPC_TX_SOP);
5722 tcpm_set_state(port, SRC_STARTUP, PD_T_SWAP_SRC_START);
5725 case VCONN_SWAP_ACCEPT:
5726 tcpm_pd_send_control(port, PD_CTRL_ACCEPT, TCPC_TX_SOP);
5727 tcpm_ams_finish(port);
5728 tcpm_set_state(port, VCONN_SWAP_START, 0);
5730 case VCONN_SWAP_SEND:
5731 tcpm_pd_send_control(port, PD_CTRL_VCONN_SWAP, TCPC_TX_SOP);
5732 tcpm_set_state(port, VCONN_SWAP_SEND_TIMEOUT,
5733 PD_T_SENDER_RESPONSE);
5735 case VCONN_SWAP_SEND_TIMEOUT:
5736 tcpm_swap_complete(port, -ETIMEDOUT);
5737 tcpm_set_state(port, ready_state(port), 0);
5739 case VCONN_SWAP_START:
5740 if (port->vconn_role == TYPEC_SOURCE)
5741 tcpm_set_state(port, VCONN_SWAP_WAIT_FOR_VCONN, 0);
5743 tcpm_set_state(port, VCONN_SWAP_TURN_ON_VCONN, 0);
5745 case VCONN_SWAP_WAIT_FOR_VCONN:
5746 tcpm_set_state(port, hard_reset_state(port),
5747 PD_T_VCONN_SOURCE_ON);
5749 case VCONN_SWAP_TURN_ON_VCONN:
5750 ret = tcpm_set_vconn(port, true);
5751 tcpm_pd_send_control(port, PD_CTRL_PS_RDY, TCPC_TX_SOP);
5753 * USB PD 3.0 Section 6.4.4.3.1
5755 * Note that a Cable Plug or VPD will not be ready for PD
5756 * Communication until tVCONNStable after VCONN has been applied
5759 tcpm_set_state(port, VCONN_SWAP_SEND_SOFT_RESET,
5762 tcpm_set_state(port, ready_state(port), 0);
5764 case VCONN_SWAP_TURN_OFF_VCONN:
5765 tcpm_set_vconn(port, false);
5766 tcpm_set_state(port, ready_state(port), 0);
5768 case VCONN_SWAP_SEND_SOFT_RESET:
5769 tcpm_swap_complete(port, port->swap_status);
5770 if (tcpm_can_communicate_sop_prime(port)) {
5771 port->tx_sop_type = TCPC_TX_SOP_PRIME;
5772 port->upcoming_state = SOFT_RESET_SEND;
5773 tcpm_ams_start(port, SOFT_RESET_AMS);
5775 tcpm_set_state(port, ready_state(port), 0);
5779 case DR_SWAP_CANCEL:
5780 case PR_SWAP_CANCEL:
5781 case VCONN_SWAP_CANCEL:
5782 tcpm_swap_complete(port, port->swap_status);
5783 if (port->pwr_role == TYPEC_SOURCE)
5784 tcpm_set_state(port, SRC_READY, 0);
5786 tcpm_set_state(port, SNK_READY, 0);
5788 case FR_SWAP_CANCEL:
5789 if (port->pwr_role == TYPEC_SOURCE)
5790 tcpm_set_state(port, SRC_READY, 0);
5792 tcpm_set_state(port, SNK_READY, 0);
5796 switch (BDO_MODE_MASK(port->bist_request)) {
5797 case BDO_MODE_CARRIER2:
5798 tcpm_pd_transmit(port, TCPC_TX_BIST_MODE_2, NULL);
5799 tcpm_set_state(port, unattached_state(port),
5800 PD_T_BIST_CONT_MODE);
5802 case BDO_MODE_TESTDATA:
5803 if (port->tcpc->set_bist_data) {
5804 tcpm_log(port, "Enable BIST MODE TESTDATA");
5805 port->tcpc->set_bist_data(port->tcpc, true);
5812 case GET_STATUS_SEND:
5813 tcpm_pd_send_control(port, PD_CTRL_GET_STATUS, TCPC_TX_SOP);
5814 tcpm_set_state(port, GET_STATUS_SEND_TIMEOUT,
5815 PD_T_SENDER_RESPONSE);
5817 case GET_STATUS_SEND_TIMEOUT:
5818 tcpm_set_state(port, ready_state(port), 0);
5820 case GET_PPS_STATUS_SEND:
5821 tcpm_pd_send_control(port, PD_CTRL_GET_PPS_STATUS, TCPC_TX_SOP);
5822 tcpm_set_state(port, GET_PPS_STATUS_SEND_TIMEOUT,
5823 PD_T_SENDER_RESPONSE);
5825 case GET_PPS_STATUS_SEND_TIMEOUT:
5826 tcpm_set_state(port, ready_state(port), 0);
5829 tcpm_pd_send_control(port, PD_CTRL_GET_SINK_CAP, TCPC_TX_SOP);
5830 tcpm_set_state(port, GET_SINK_CAP_TIMEOUT, PD_T_SENDER_RESPONSE);
5832 case GET_SINK_CAP_TIMEOUT:
5833 port->sink_cap_done = true;
5834 tcpm_set_state(port, ready_state(port), 0);
5836 case ERROR_RECOVERY:
5837 tcpm_swap_complete(port, -EPROTO);
5838 tcpm_pps_complete(port, -EPROTO);
5839 tcpm_set_state(port, PORT_RESET, 0);
5842 tcpm_reset_port(port);
5843 if (port->self_powered)
5844 tcpm_set_cc(port, TYPEC_CC_OPEN);
5846 tcpm_set_cc(port, tcpm_default_state(port) == SNK_UNATTACHED ?
5847 TYPEC_CC_RD : tcpm_rp_cc(port));
5848 tcpm_set_state(port, PORT_RESET_WAIT_OFF,
5849 PD_T_ERROR_RECOVERY);
5851 case PORT_RESET_WAIT_OFF:
5852 tcpm_set_state(port,
5853 tcpm_default_state(port),
5854 port->vbus_present ? port->timings.ps_src_off_time : 0);
5857 /* AMS intermediate state */
5859 if (port->upcoming_state == INVALID_STATE) {
5860 tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ?
5861 SRC_READY : SNK_READY, 0);
5865 upcoming_state = port->upcoming_state;
5866 port->upcoming_state = INVALID_STATE;
5867 tcpm_set_state(port, upcoming_state, 0);
5871 case CHUNK_NOT_SUPP:
5872 tcpm_pd_send_control(port, PD_CTRL_NOT_SUPP, TCPC_TX_SOP);
5873 tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ? SRC_READY : SNK_READY, 0);
5877 case SRC_VDM_IDENTITY_REQUEST:
5878 port->send_discover_prime = true;
5879 port->tx_sop_type = TCPC_TX_SOP_PRIME;
5880 mod_send_discover_delayed_work(port, 0);
5881 port->upcoming_state = SRC_SEND_CAPABILITIES;
5885 WARN(1, "Unexpected port state %d\n", port->state);
5890 static void tcpm_state_machine_work(struct kthread_work *work)
5892 struct tcpm_port *port = container_of(work, struct tcpm_port, state_machine);
5893 enum tcpm_state prev_state;
5895 mutex_lock(&port->lock);
5896 port->state_machine_running = true;
5898 if (port->queued_message && tcpm_send_queued_message(port))
5901 /* If we were queued due to a delayed state change, update it now */
5902 if (port->delayed_state) {
5903 tcpm_log(port, "state change %s -> %s [delayed %ld ms]",
5904 tcpm_states[port->state],
5905 tcpm_states[port->delayed_state], port->delay_ms);
5906 port->prev_state = port->state;
5907 port->state = port->delayed_state;
5908 port->delayed_state = INVALID_STATE;
5912 * Continue running as long as we have (non-delayed) state changes
5916 prev_state = port->state;
5917 run_state_machine(port);
5918 if (port->queued_message)
5919 tcpm_send_queued_message(port);
5920 } while (port->state != prev_state && !port->delayed_state);
5923 port->state_machine_running = false;
5924 mutex_unlock(&port->lock);
5927 static void _tcpm_cc_change(struct tcpm_port *port, enum typec_cc_status cc1,
5928 enum typec_cc_status cc2)
5930 enum typec_cc_status old_cc1, old_cc2;
5931 enum tcpm_state new_state;
5933 old_cc1 = port->cc1;
5934 old_cc2 = port->cc2;
5938 tcpm_log_force(port,
5939 "CC1: %u -> %u, CC2: %u -> %u [state %s, polarity %d, %s]",
5940 old_cc1, cc1, old_cc2, cc2, tcpm_states[port->state],
5942 tcpm_port_is_disconnected(port) ? "disconnected"
5945 switch (port->state) {
5947 if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) ||
5948 tcpm_port_is_source(port))
5949 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
5950 else if (tcpm_port_is_sink(port))
5951 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
5953 case CHECK_CONTAMINANT:
5954 /* Wait for Toggling to be resumed */
5956 case SRC_UNATTACHED:
5957 case ACC_UNATTACHED:
5958 if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) ||
5959 tcpm_port_is_source(port))
5960 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
5962 case SRC_ATTACH_WAIT:
5963 if (tcpm_port_is_disconnected(port) ||
5964 tcpm_port_is_audio_detached(port))
5965 tcpm_set_state(port, SRC_UNATTACHED, 0);
5966 else if (cc1 != old_cc1 || cc2 != old_cc2)
5967 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
5971 case SRC_SEND_CAPABILITIES:
5973 if (tcpm_port_is_disconnected(port) ||
5974 !tcpm_port_is_source(port)) {
5975 if (port->port_type == TYPEC_PORT_SRC)
5976 tcpm_set_state(port, SRC_UNATTACHED, tcpm_wait_for_discharge(port));
5978 tcpm_set_state(port, SNK_UNATTACHED, tcpm_wait_for_discharge(port));
5981 case SNK_UNATTACHED:
5982 if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) ||
5983 tcpm_port_is_sink(port))
5984 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
5986 case SNK_ATTACH_WAIT:
5987 if ((port->cc1 == TYPEC_CC_OPEN &&
5988 port->cc2 != TYPEC_CC_OPEN) ||
5989 (port->cc1 != TYPEC_CC_OPEN &&
5990 port->cc2 == TYPEC_CC_OPEN))
5991 new_state = SNK_DEBOUNCED;
5992 else if (tcpm_port_is_disconnected(port))
5993 new_state = SNK_UNATTACHED;
5996 if (new_state != port->delayed_state)
5997 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
6000 if (tcpm_port_is_disconnected(port))
6001 new_state = SNK_UNATTACHED;
6002 else if (port->vbus_present)
6003 new_state = tcpm_try_src(port) ? SRC_TRY : SNK_ATTACHED;
6005 new_state = SNK_UNATTACHED;
6006 if (new_state != port->delayed_state)
6007 tcpm_set_state(port, SNK_DEBOUNCED, 0);
6011 * EXIT condition is based primarily on vbus disconnect and CC is secondary.
6012 * "A port that has entered into USB PD communications with the Source and
6013 * has seen the CC voltage exceed vRd-USB may monitor the CC pin to detect
6014 * cable disconnect in addition to monitoring VBUS.
6016 * A port that is monitoring the CC voltage for disconnect (but is not in
6017 * the process of a USB PD PR_Swap or USB PD FR_Swap) shall transition to
6018 * Unattached.SNK within tSinkDisconnect after the CC voltage remains below
6019 * vRd-USB for tPDDebounce."
6021 * When set_auto_vbus_discharge_threshold is enabled, CC pins go
6022 * away before vbus decays to disconnect threshold. Allow
6023 * disconnect to be driven by vbus disconnect when auto vbus
6024 * discharge is enabled.
6026 if (!port->auto_vbus_discharge_enabled && tcpm_port_is_disconnected(port))
6027 tcpm_set_state(port, unattached_state(port), 0);
6028 else if (!port->pd_capable &&
6029 (cc1 != old_cc1 || cc2 != old_cc2))
6030 tcpm_set_current_limit(port,
6031 tcpm_get_current_limit(port),
6035 case AUDIO_ACC_ATTACHED:
6036 if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN)
6037 tcpm_set_state(port, AUDIO_ACC_DEBOUNCE, 0);
6039 case AUDIO_ACC_DEBOUNCE:
6040 if (tcpm_port_is_audio(port))
6041 tcpm_set_state(port, AUDIO_ACC_ATTACHED, 0);
6044 case DEBUG_ACC_ATTACHED:
6045 if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN)
6046 tcpm_set_state(port, DEBUG_ACC_DEBOUNCE, 0);
6049 case DEBUG_ACC_DEBOUNCE:
6050 if (tcpm_port_is_debug(port))
6051 tcpm_set_state(port, DEBUG_ACC_ATTACHED, 0);
6055 /* Do nothing, waiting for timeout */
6059 /* CC line is unstable, wait for debounce */
6060 if (tcpm_port_is_disconnected(port))
6061 tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE, 0);
6063 case SNK_DISCOVERY_DEBOUNCE:
6067 /* Hand over to state machine if needed */
6068 if (!port->vbus_present && tcpm_port_is_source(port))
6069 tcpm_set_state(port, SRC_TRYWAIT_DEBOUNCE, 0);
6071 case SRC_TRYWAIT_DEBOUNCE:
6072 if (port->vbus_present || !tcpm_port_is_source(port))
6073 tcpm_set_state(port, SRC_TRYWAIT, 0);
6075 case SNK_TRY_WAIT_DEBOUNCE:
6076 if (!tcpm_port_is_sink(port)) {
6078 tcpm_set_state(port, SRC_TRYWAIT, PD_T_PD_DEBOUNCE);
6082 if (tcpm_port_is_source(port))
6083 tcpm_set_state(port, SRC_TRY_DEBOUNCE, 0);
6085 case SRC_TRY_DEBOUNCE:
6086 tcpm_set_state(port, SRC_TRY_WAIT, 0);
6088 case SNK_TRYWAIT_DEBOUNCE:
6089 if (tcpm_port_is_sink(port))
6090 tcpm_set_state(port, SNK_TRYWAIT_VBUS, 0);
6092 case SNK_TRYWAIT_VBUS:
6093 if (!tcpm_port_is_sink(port))
6094 tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, 0);
6096 case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS:
6097 if (!tcpm_port_is_sink(port))
6098 tcpm_set_state(port, SRC_TRYWAIT, PD_T_TRY_CC_DEBOUNCE);
6100 tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS, 0);
6103 /* Do nothing, waiting for tCCDebounce */
6105 case PR_SWAP_SNK_SRC_SINK_OFF:
6106 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
6107 case PR_SWAP_SRC_SNK_SOURCE_OFF:
6108 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED:
6109 case PR_SWAP_SNK_SRC_SOURCE_ON:
6111 * CC state change is expected in PR_SWAP
6116 case FR_SWAP_SEND_TIMEOUT:
6117 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
6118 case FR_SWAP_SNK_SRC_NEW_SINK_READY:
6119 case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED:
6120 /* Do nothing, CC change expected */
6124 case PORT_RESET_WAIT_OFF:
6126 * State set back to default mode once the timer completes.
6127 * Ignore CC changes here.
6132 * While acting as sink and auto vbus discharge is enabled, Allow disconnect
6133 * to be driven by vbus disconnect.
6135 if (tcpm_port_is_disconnected(port) && !(port->pwr_role == TYPEC_SINK &&
6136 port->auto_vbus_discharge_enabled))
6137 tcpm_set_state(port, unattached_state(port), 0);
6142 static void _tcpm_pd_vbus_on(struct tcpm_port *port)
6144 tcpm_log_force(port, "VBUS on");
6145 port->vbus_present = true;
6147 * When vbus_present is true i.e. Voltage at VBUS is greater than VSAFE5V implicitly
6148 * states that vbus is not at VSAFE0V, hence clear the vbus_vsafe0v flag here.
6150 port->vbus_vsafe0v = false;
6152 switch (port->state) {
6153 case SNK_TRANSITION_SINK_VBUS:
6154 port->explicit_contract = true;
6155 tcpm_set_state(port, SNK_READY, 0);
6158 tcpm_set_state(port, SNK_DISCOVERY, 0);
6162 tcpm_set_state(port, tcpm_try_src(port) ? SRC_TRY
6166 case SNK_HARD_RESET_WAIT_VBUS:
6167 tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, 0);
6170 tcpm_set_state(port, SRC_STARTUP, 0);
6172 case SRC_HARD_RESET_VBUS_ON:
6173 tcpm_set_state(port, SRC_STARTUP, 0);
6177 /* Do nothing, waiting for timeout */
6180 /* Do nothing, Waiting for Rd to be detected */
6182 case SRC_TRYWAIT_DEBOUNCE:
6183 tcpm_set_state(port, SRC_TRYWAIT, 0);
6185 case SNK_TRY_WAIT_DEBOUNCE:
6186 /* Do nothing, waiting for PD_DEBOUNCE to do be done */
6189 /* Do nothing, waiting for tCCDebounce */
6191 case SNK_TRYWAIT_VBUS:
6192 if (tcpm_port_is_sink(port))
6193 tcpm_set_state(port, SNK_ATTACHED, 0);
6195 case SNK_TRYWAIT_DEBOUNCE:
6196 /* Do nothing, waiting for Rp */
6198 case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS:
6199 if (port->vbus_present && tcpm_port_is_sink(port))
6200 tcpm_set_state(port, SNK_ATTACHED, 0);
6203 case SRC_TRY_DEBOUNCE:
6204 /* Do nothing, waiting for sink detection */
6207 case FR_SWAP_SEND_TIMEOUT:
6208 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
6209 case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED:
6210 if (port->tcpc->frs_sourcing_vbus)
6211 port->tcpc->frs_sourcing_vbus(port->tcpc);
6213 case FR_SWAP_SNK_SRC_NEW_SINK_READY:
6214 if (port->tcpc->frs_sourcing_vbus)
6215 port->tcpc->frs_sourcing_vbus(port->tcpc);
6216 tcpm_set_state(port, FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED, 0);
6220 case PORT_RESET_WAIT_OFF:
6222 * State set back to default mode once the timer completes.
6223 * Ignore vbus changes here.
6232 static void _tcpm_pd_vbus_off(struct tcpm_port *port)
6234 tcpm_log_force(port, "VBUS off");
6235 port->vbus_present = false;
6236 port->vbus_never_low = false;
6237 switch (port->state) {
6238 case SNK_HARD_RESET_SINK_OFF:
6239 tcpm_set_state(port, SNK_HARD_RESET_WAIT_VBUS, 0);
6241 case HARD_RESET_SEND:
6244 /* Do nothing, waiting for timeout */
6247 /* Hand over to state machine if needed */
6248 if (tcpm_port_is_source(port))
6249 tcpm_set_state(port, SRC_TRYWAIT_DEBOUNCE, 0);
6251 case SNK_TRY_WAIT_DEBOUNCE:
6252 /* Do nothing, waiting for PD_DEBOUNCE to do be done */
6255 case SNK_TRYWAIT_VBUS:
6256 case SNK_TRYWAIT_DEBOUNCE:
6258 case SNK_ATTACH_WAIT:
6260 /* Do nothing, as TCPM is still waiting for vbus to reach VSAFE5V to connect */
6263 case SNK_NEGOTIATE_CAPABILITIES:
6266 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
6267 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF, 0);
6270 case PR_SWAP_SNK_SRC_SINK_OFF:
6271 /* Do nothing, expected */
6274 case PR_SWAP_SNK_SRC_SOURCE_ON:
6276 * Do nothing when vbus off notification is received.
6277 * TCPM can wait for PD_T_NEWSRC in PR_SWAP_SNK_SRC_SOURCE_ON
6278 * for the vbus source to ramp up.
6282 case PORT_RESET_WAIT_OFF:
6283 tcpm_set_state(port, tcpm_default_state(port), 0);
6287 case SRC_TRY_DEBOUNCE:
6288 /* Do nothing, waiting for sink detection */
6292 case SRC_SEND_CAPABILITIES:
6293 case SRC_SEND_CAPABILITIES_TIMEOUT:
6294 case SRC_NEGOTIATE_CAPABILITIES:
6295 case SRC_TRANSITION_SUPPLY:
6297 case SRC_WAIT_NEW_CAPABILITIES:
6299 * Force to unattached state to re-initiate connection.
6300 * DRP port should move to Unattached.SNK instead of Unattached.SRC if
6301 * sink removed. Although sink removal here is due to source's vbus collapse,
6302 * treat it the same way for consistency.
6304 if (port->port_type == TYPEC_PORT_SRC)
6305 tcpm_set_state(port, SRC_UNATTACHED, tcpm_wait_for_discharge(port));
6307 tcpm_set_state(port, SNK_UNATTACHED, tcpm_wait_for_discharge(port));
6312 * State set back to default mode once the timer completes.
6313 * Ignore vbus changes here.
6318 case FR_SWAP_SEND_TIMEOUT:
6319 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
6320 case FR_SWAP_SNK_SRC_NEW_SINK_READY:
6321 case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED:
6322 /* Do nothing, vbus drop expected */
6325 case SNK_HARD_RESET_WAIT_VBUS:
6326 /* Do nothing, its OK to receive vbus off events */
6330 if (port->pwr_role == TYPEC_SINK && port->attached)
6331 tcpm_set_state(port, SNK_UNATTACHED, tcpm_wait_for_discharge(port));
6336 static void _tcpm_pd_vbus_vsafe0v(struct tcpm_port *port)
6338 tcpm_log_force(port, "VBUS VSAFE0V");
6339 port->vbus_vsafe0v = true;
6340 switch (port->state) {
6341 case SRC_HARD_RESET_VBUS_OFF:
6343 * After establishing the vSafe0V voltage condition on VBUS, the Source Shall wait
6344 * tSrcRecover before re-applying VCONN and restoring VBUS to vSafe5V.
6346 tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SRC_RECOVER);
6348 case SRC_ATTACH_WAIT:
6349 if (tcpm_port_is_source(port))
6350 tcpm_set_state(port, tcpm_try_snk(port) ? SNK_TRY : SRC_ATTACHED,
6351 port->timings.cc_debounce_time);
6354 case SRC_SEND_CAPABILITIES:
6355 case SRC_SEND_CAPABILITIES_TIMEOUT:
6356 case SRC_NEGOTIATE_CAPABILITIES:
6357 case SRC_TRANSITION_SUPPLY:
6359 case SRC_WAIT_NEW_CAPABILITIES:
6360 if (port->auto_vbus_discharge_enabled) {
6361 if (port->port_type == TYPEC_PORT_SRC)
6362 tcpm_set_state(port, SRC_UNATTACHED, 0);
6364 tcpm_set_state(port, SNK_UNATTACHED, 0);
6367 case PR_SWAP_SNK_SRC_SINK_OFF:
6368 case PR_SWAP_SNK_SRC_SOURCE_ON:
6369 /* Do nothing, vsafe0v is expected during transition */
6371 case SNK_ATTACH_WAIT:
6373 /*Do nothing, still waiting for VSAFE5V for connect */
6375 case SNK_HARD_RESET_WAIT_VBUS:
6376 /* Do nothing, its OK to receive vbus off events */
6379 if (port->pwr_role == TYPEC_SINK && port->auto_vbus_discharge_enabled)
6380 tcpm_set_state(port, SNK_UNATTACHED, 0);
6385 static void _tcpm_pd_hard_reset(struct tcpm_port *port)
6387 tcpm_log_force(port, "Received hard reset");
6388 if (port->bist_request == BDO_MODE_TESTDATA && port->tcpc->set_bist_data)
6389 port->tcpc->set_bist_data(port->tcpc, false);
6391 switch (port->state) {
6393 case ERROR_RECOVERY:
6395 case PORT_RESET_WAIT_OFF:
6401 if (port->ams != NONE_AMS)
6402 port->ams = NONE_AMS;
6403 if (port->hard_reset_count < PD_N_HARD_RESET_COUNT)
6404 port->ams = HARD_RESET;
6406 * If we keep receiving hard reset requests, executing the hard reset
6407 * must have failed. Revert to error recovery if that happens.
6409 tcpm_set_state(port,
6410 port->hard_reset_count < PD_N_HARD_RESET_COUNT ?
6411 HARD_RESET_START : ERROR_RECOVERY,
6415 static void tcpm_pd_event_handler(struct kthread_work *work)
6417 struct tcpm_port *port = container_of(work, struct tcpm_port,
6421 mutex_lock(&port->lock);
6423 spin_lock(&port->pd_event_lock);
6424 while (port->pd_events) {
6425 events = port->pd_events;
6426 port->pd_events = 0;
6427 spin_unlock(&port->pd_event_lock);
6428 if (events & TCPM_RESET_EVENT)
6429 _tcpm_pd_hard_reset(port);
6430 if (events & TCPM_VBUS_EVENT) {
6433 vbus = port->tcpc->get_vbus(port->tcpc);
6435 _tcpm_pd_vbus_on(port);
6437 _tcpm_pd_vbus_off(port);
6439 * When TCPC does not support detecting vsafe0v voltage level,
6440 * treat vbus absent as vsafe0v. Else invoke is_vbus_vsafe0v
6441 * to see if vbus has discharge to VSAFE0V.
6443 if (!port->tcpc->is_vbus_vsafe0v ||
6444 port->tcpc->is_vbus_vsafe0v(port->tcpc))
6445 _tcpm_pd_vbus_vsafe0v(port);
6448 if (events & TCPM_CC_EVENT) {
6449 enum typec_cc_status cc1, cc2;
6451 if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0)
6452 _tcpm_cc_change(port, cc1, cc2);
6454 if (events & TCPM_FRS_EVENT) {
6455 if (port->state == SNK_READY) {
6458 port->upcoming_state = FR_SWAP_SEND;
6459 ret = tcpm_ams_start(port, FAST_ROLE_SWAP);
6461 port->upcoming_state = INVALID_STATE;
6463 tcpm_log(port, "Discarding FRS_SIGNAL! Not in sink ready");
6466 if (events & TCPM_SOURCING_VBUS) {
6467 tcpm_log(port, "sourcing vbus");
6469 * In fast role swap case TCPC autonomously sources vbus. Set vbus_source
6470 * true as TCPM wouldn't have called tcpm_set_vbus.
6472 * When vbus is sourced on the command on TCPM i.e. TCPM called
6473 * tcpm_set_vbus to source vbus, vbus_source would already be true.
6475 port->vbus_source = true;
6476 _tcpm_pd_vbus_on(port);
6478 if (events & TCPM_PORT_CLEAN) {
6479 tcpm_log(port, "port clean");
6480 if (port->state == CHECK_CONTAMINANT) {
6481 if (tcpm_start_toggling(port, tcpm_rp_cc(port)))
6482 tcpm_set_state(port, TOGGLING, 0);
6484 tcpm_set_state(port, tcpm_default_state(port), 0);
6487 if (events & TCPM_PORT_ERROR) {
6488 tcpm_log(port, "port triggering error recovery");
6489 tcpm_set_state(port, ERROR_RECOVERY, 0);
6492 spin_lock(&port->pd_event_lock);
6494 spin_unlock(&port->pd_event_lock);
6495 mutex_unlock(&port->lock);
6498 void tcpm_cc_change(struct tcpm_port *port)
6500 spin_lock(&port->pd_event_lock);
6501 port->pd_events |= TCPM_CC_EVENT;
6502 spin_unlock(&port->pd_event_lock);
6503 kthread_queue_work(port->wq, &port->event_work);
6505 EXPORT_SYMBOL_GPL(tcpm_cc_change);
6507 void tcpm_vbus_change(struct tcpm_port *port)
6509 spin_lock(&port->pd_event_lock);
6510 port->pd_events |= TCPM_VBUS_EVENT;
6511 spin_unlock(&port->pd_event_lock);
6512 kthread_queue_work(port->wq, &port->event_work);
6514 EXPORT_SYMBOL_GPL(tcpm_vbus_change);
6516 void tcpm_pd_hard_reset(struct tcpm_port *port)
6518 spin_lock(&port->pd_event_lock);
6519 port->pd_events = TCPM_RESET_EVENT;
6520 spin_unlock(&port->pd_event_lock);
6521 kthread_queue_work(port->wq, &port->event_work);
6523 EXPORT_SYMBOL_GPL(tcpm_pd_hard_reset);
6525 void tcpm_sink_frs(struct tcpm_port *port)
6527 spin_lock(&port->pd_event_lock);
6528 port->pd_events |= TCPM_FRS_EVENT;
6529 spin_unlock(&port->pd_event_lock);
6530 kthread_queue_work(port->wq, &port->event_work);
6532 EXPORT_SYMBOL_GPL(tcpm_sink_frs);
6534 void tcpm_sourcing_vbus(struct tcpm_port *port)
6536 spin_lock(&port->pd_event_lock);
6537 port->pd_events |= TCPM_SOURCING_VBUS;
6538 spin_unlock(&port->pd_event_lock);
6539 kthread_queue_work(port->wq, &port->event_work);
6541 EXPORT_SYMBOL_GPL(tcpm_sourcing_vbus);
6543 void tcpm_port_clean(struct tcpm_port *port)
6545 spin_lock(&port->pd_event_lock);
6546 port->pd_events |= TCPM_PORT_CLEAN;
6547 spin_unlock(&port->pd_event_lock);
6548 kthread_queue_work(port->wq, &port->event_work);
6550 EXPORT_SYMBOL_GPL(tcpm_port_clean);
6552 bool tcpm_port_is_toggling(struct tcpm_port *port)
6554 return port->port_type == TYPEC_PORT_DRP && port->state == TOGGLING;
6556 EXPORT_SYMBOL_GPL(tcpm_port_is_toggling);
6558 void tcpm_port_error_recovery(struct tcpm_port *port)
6560 spin_lock(&port->pd_event_lock);
6561 port->pd_events |= TCPM_PORT_ERROR;
6562 spin_unlock(&port->pd_event_lock);
6563 kthread_queue_work(port->wq, &port->event_work);
6565 EXPORT_SYMBOL_GPL(tcpm_port_error_recovery);
6567 static void tcpm_enable_frs_work(struct kthread_work *work)
6569 struct tcpm_port *port = container_of(work, struct tcpm_port, enable_frs);
6572 mutex_lock(&port->lock);
6573 /* Not FRS capable */
6574 if (!port->connected || port->port_type != TYPEC_PORT_DRP ||
6575 port->pwr_opmode != TYPEC_PWR_MODE_PD ||
6576 !port->tcpc->enable_frs ||
6577 /* Sink caps queried */
6578 port->sink_cap_done || port->negotiated_rev < PD_REV30)
6581 /* Send when the state machine is idle */
6582 if (port->state != SNK_READY || port->vdm_sm_running || port->send_discover ||
6583 port->send_discover_prime)
6586 port->upcoming_state = GET_SINK_CAP;
6587 ret = tcpm_ams_start(port, GET_SINK_CAPABILITIES);
6588 if (ret == -EAGAIN) {
6589 port->upcoming_state = INVALID_STATE;
6591 port->sink_cap_done = true;
6595 mod_enable_frs_delayed_work(port, GET_SINK_CAP_RETRY_MS);
6597 mutex_unlock(&port->lock);
6600 static void tcpm_send_discover_work(struct kthread_work *work)
6602 struct tcpm_port *port = container_of(work, struct tcpm_port, send_discover_work);
6604 mutex_lock(&port->lock);
6605 /* No need to send DISCOVER_IDENTITY anymore */
6606 if (!port->send_discover && !port->send_discover_prime)
6609 if (port->data_role == TYPEC_DEVICE && port->negotiated_rev < PD_REV30) {
6610 port->send_discover = false;
6611 port->send_discover_prime = false;
6615 /* Retry if the port is not idle */
6616 if ((port->state != SRC_READY && port->state != SNK_READY &&
6617 port->state != SRC_VDM_IDENTITY_REQUEST) || port->vdm_sm_running) {
6618 mod_send_discover_delayed_work(port, SEND_DISCOVER_RETRY_MS);
6622 tcpm_send_vdm(port, USB_SID_PD, CMD_DISCOVER_IDENT, NULL, 0, port->tx_sop_type);
6625 mutex_unlock(&port->lock);
6628 static int tcpm_dr_set(struct typec_port *p, enum typec_data_role data)
6630 struct tcpm_port *port = typec_get_drvdata(p);
6633 mutex_lock(&port->swap_lock);
6634 mutex_lock(&port->lock);
6636 if (port->typec_caps.data != TYPEC_PORT_DRD) {
6640 if (port->state != SRC_READY && port->state != SNK_READY) {
6645 if (port->data_role == data) {
6652 * 6.3.9: If an alternate mode is active, a request to swap
6653 * alternate modes shall trigger a port reset.
6654 * Reject data role swap request in this case.
6657 if (!port->pd_capable) {
6659 * If the partner is not PD capable, reset the port to
6660 * trigger a role change. This can only work if a preferred
6661 * role is configured, and if it matches the requested role.
6663 if (port->try_role == TYPEC_NO_PREFERRED_ROLE ||
6664 port->try_role == port->pwr_role) {
6668 port->non_pd_role_swap = true;
6669 tcpm_set_state(port, PORT_RESET, 0);
6671 port->upcoming_state = DR_SWAP_SEND;
6672 ret = tcpm_ams_start(port, DATA_ROLE_SWAP);
6673 if (ret == -EAGAIN) {
6674 port->upcoming_state = INVALID_STATE;
6679 port->swap_status = 0;
6680 port->swap_pending = true;
6681 reinit_completion(&port->swap_complete);
6682 mutex_unlock(&port->lock);
6684 if (!wait_for_completion_timeout(&port->swap_complete,
6685 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT)))
6688 ret = port->swap_status;
6690 port->non_pd_role_swap = false;
6694 mutex_unlock(&port->lock);
6696 mutex_unlock(&port->swap_lock);
6700 static int tcpm_pr_set(struct typec_port *p, enum typec_role role)
6702 struct tcpm_port *port = typec_get_drvdata(p);
6705 mutex_lock(&port->swap_lock);
6706 mutex_lock(&port->lock);
6708 if (port->port_type != TYPEC_PORT_DRP) {
6712 if (port->state != SRC_READY && port->state != SNK_READY) {
6717 if (role == port->pwr_role) {
6722 port->upcoming_state = PR_SWAP_SEND;
6723 ret = tcpm_ams_start(port, POWER_ROLE_SWAP);
6724 if (ret == -EAGAIN) {
6725 port->upcoming_state = INVALID_STATE;
6729 port->swap_status = 0;
6730 port->swap_pending = true;
6731 reinit_completion(&port->swap_complete);
6732 mutex_unlock(&port->lock);
6734 if (!wait_for_completion_timeout(&port->swap_complete,
6735 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT)))
6738 ret = port->swap_status;
6743 mutex_unlock(&port->lock);
6745 mutex_unlock(&port->swap_lock);
6749 static int tcpm_vconn_set(struct typec_port *p, enum typec_role role)
6751 struct tcpm_port *port = typec_get_drvdata(p);
6754 mutex_lock(&port->swap_lock);
6755 mutex_lock(&port->lock);
6757 if (port->state != SRC_READY && port->state != SNK_READY) {
6762 if (role == port->vconn_role) {
6767 port->upcoming_state = VCONN_SWAP_SEND;
6768 ret = tcpm_ams_start(port, VCONN_SWAP);
6769 if (ret == -EAGAIN) {
6770 port->upcoming_state = INVALID_STATE;
6774 port->swap_status = 0;
6775 port->swap_pending = true;
6776 reinit_completion(&port->swap_complete);
6777 mutex_unlock(&port->lock);
6779 if (!wait_for_completion_timeout(&port->swap_complete,
6780 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT)))
6783 ret = port->swap_status;
6788 mutex_unlock(&port->lock);
6790 mutex_unlock(&port->swap_lock);
6794 static int tcpm_try_role(struct typec_port *p, int role)
6796 struct tcpm_port *port = typec_get_drvdata(p);
6797 struct tcpc_dev *tcpc = port->tcpc;
6800 mutex_lock(&port->lock);
6802 ret = tcpc->try_role(tcpc, role);
6804 port->try_role = role;
6805 port->try_src_count = 0;
6806 port->try_snk_count = 0;
6807 mutex_unlock(&port->lock);
6812 static int tcpm_pps_set_op_curr(struct tcpm_port *port, u16 req_op_curr)
6814 unsigned int target_mw;
6817 mutex_lock(&port->swap_lock);
6818 mutex_lock(&port->lock);
6820 if (!port->pps_data.active) {
6825 if (port->state != SNK_READY) {
6830 if (req_op_curr > port->pps_data.max_curr) {
6835 target_mw = (req_op_curr * port->supply_voltage) / 1000;
6836 if (target_mw < port->operating_snk_mw) {
6841 port->upcoming_state = SNK_NEGOTIATE_PPS_CAPABILITIES;
6842 ret = tcpm_ams_start(port, POWER_NEGOTIATION);
6843 if (ret == -EAGAIN) {
6844 port->upcoming_state = INVALID_STATE;
6848 /* Round down operating current to align with PPS valid steps */
6849 req_op_curr = req_op_curr - (req_op_curr % RDO_PROG_CURR_MA_STEP);
6851 reinit_completion(&port->pps_complete);
6852 port->pps_data.req_op_curr = req_op_curr;
6853 port->pps_status = 0;
6854 port->pps_pending = true;
6855 mutex_unlock(&port->lock);
6857 if (!wait_for_completion_timeout(&port->pps_complete,
6858 msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT)))
6861 ret = port->pps_status;
6866 mutex_unlock(&port->lock);
6868 mutex_unlock(&port->swap_lock);
6873 static int tcpm_pps_set_out_volt(struct tcpm_port *port, u16 req_out_volt)
6875 unsigned int target_mw;
6878 mutex_lock(&port->swap_lock);
6879 mutex_lock(&port->lock);
6881 if (!port->pps_data.active) {
6886 if (port->state != SNK_READY) {
6891 target_mw = (port->current_limit * req_out_volt) / 1000;
6892 if (target_mw < port->operating_snk_mw) {
6897 port->upcoming_state = SNK_NEGOTIATE_PPS_CAPABILITIES;
6898 ret = tcpm_ams_start(port, POWER_NEGOTIATION);
6899 if (ret == -EAGAIN) {
6900 port->upcoming_state = INVALID_STATE;
6904 /* Round down output voltage to align with PPS valid steps */
6905 req_out_volt = req_out_volt - (req_out_volt % RDO_PROG_VOLT_MV_STEP);
6907 reinit_completion(&port->pps_complete);
6908 port->pps_data.req_out_volt = req_out_volt;
6909 port->pps_status = 0;
6910 port->pps_pending = true;
6911 mutex_unlock(&port->lock);
6913 if (!wait_for_completion_timeout(&port->pps_complete,
6914 msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT)))
6917 ret = port->pps_status;
6922 mutex_unlock(&port->lock);
6924 mutex_unlock(&port->swap_lock);
6929 static int tcpm_pps_activate(struct tcpm_port *port, bool activate)
6933 mutex_lock(&port->swap_lock);
6934 mutex_lock(&port->lock);
6936 if (!port->pps_data.supported) {
6941 /* Trying to deactivate PPS when already deactivated so just bail */
6942 if (!port->pps_data.active && !activate)
6945 if (port->state != SNK_READY) {
6951 port->upcoming_state = SNK_NEGOTIATE_PPS_CAPABILITIES;
6953 port->upcoming_state = SNK_NEGOTIATE_CAPABILITIES;
6954 ret = tcpm_ams_start(port, POWER_NEGOTIATION);
6955 if (ret == -EAGAIN) {
6956 port->upcoming_state = INVALID_STATE;
6960 reinit_completion(&port->pps_complete);
6961 port->pps_status = 0;
6962 port->pps_pending = true;
6964 /* Trigger PPS request or move back to standard PDO contract */
6966 port->pps_data.req_out_volt = port->supply_voltage;
6967 port->pps_data.req_op_curr = port->current_limit;
6969 mutex_unlock(&port->lock);
6971 if (!wait_for_completion_timeout(&port->pps_complete,
6972 msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT)))
6975 ret = port->pps_status;
6980 mutex_unlock(&port->lock);
6982 mutex_unlock(&port->swap_lock);
6987 static void tcpm_init(struct tcpm_port *port)
6989 enum typec_cc_status cc1, cc2;
6991 port->tcpc->init(port->tcpc);
6993 tcpm_reset_port(port);
6997 * Should possibly wait for VBUS to settle if it was enabled locally
6998 * since tcpm_reset_port() will disable VBUS.
7000 port->vbus_present = port->tcpc->get_vbus(port->tcpc);
7001 if (port->vbus_present)
7002 port->vbus_never_low = true;
7005 * 1. When vbus_present is true, voltage on VBUS is already at VSAFE5V.
7006 * So implicitly vbus_vsafe0v = false.
7008 * 2. When vbus_present is false and TCPC does NOT support querying
7009 * vsafe0v status, then, it's best to assume vbus is at VSAFE0V i.e.
7010 * vbus_vsafe0v is true.
7012 * 3. When vbus_present is false and TCPC does support querying vsafe0v,
7013 * then, query tcpc for vsafe0v status.
7015 if (port->vbus_present)
7016 port->vbus_vsafe0v = false;
7017 else if (!port->tcpc->is_vbus_vsafe0v)
7018 port->vbus_vsafe0v = true;
7020 port->vbus_vsafe0v = port->tcpc->is_vbus_vsafe0v(port->tcpc);
7022 tcpm_set_state(port, tcpm_default_state(port), 0);
7024 if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0)
7025 _tcpm_cc_change(port, cc1, cc2);
7028 * Some adapters need a clean slate at startup, and won't recover
7029 * otherwise. So do not try to be fancy and force a clean disconnect.
7031 tcpm_set_state(port, PORT_RESET, 0);
7034 static int tcpm_port_type_set(struct typec_port *p, enum typec_port_type type)
7036 struct tcpm_port *port = typec_get_drvdata(p);
7038 mutex_lock(&port->lock);
7039 if (type == port->port_type)
7042 port->port_type = type;
7044 if (!port->connected) {
7045 tcpm_set_state(port, PORT_RESET, 0);
7046 } else if (type == TYPEC_PORT_SNK) {
7047 if (!(port->pwr_role == TYPEC_SINK &&
7048 port->data_role == TYPEC_DEVICE))
7049 tcpm_set_state(port, PORT_RESET, 0);
7050 } else if (type == TYPEC_PORT_SRC) {
7051 if (!(port->pwr_role == TYPEC_SOURCE &&
7052 port->data_role == TYPEC_HOST))
7053 tcpm_set_state(port, PORT_RESET, 0);
7057 mutex_unlock(&port->lock);
7061 static struct pd_data *tcpm_find_pd_data(struct tcpm_port *port, struct usb_power_delivery *pd)
7065 for (i = 0; port->pd_list[i]; i++) {
7066 if (port->pd_list[i]->pd == pd)
7067 return port->pd_list[i];
7070 return ERR_PTR(-ENODATA);
7073 static struct usb_power_delivery **tcpm_pd_get(struct typec_port *p)
7075 struct tcpm_port *port = typec_get_drvdata(p);
7080 static int tcpm_pd_set(struct typec_port *p, struct usb_power_delivery *pd)
7082 struct tcpm_port *port = typec_get_drvdata(p);
7083 struct pd_data *data;
7086 mutex_lock(&port->lock);
7088 if (port->selected_pd == pd)
7091 data = tcpm_find_pd_data(port, pd);
7093 ret = PTR_ERR(data);
7097 if (data->sink_desc.pdo[0]) {
7098 for (i = 0; i < PDO_MAX_OBJECTS && data->sink_desc.pdo[i]; i++)
7099 port->snk_pdo[i] = data->sink_desc.pdo[i];
7100 port->nr_snk_pdo = i;
7101 port->operating_snk_mw = data->operating_snk_mw;
7104 if (data->source_desc.pdo[0]) {
7105 for (i = 0; i < PDO_MAX_OBJECTS && data->source_desc.pdo[i]; i++)
7106 port->src_pdo[i] = data->source_desc.pdo[i];
7107 port->nr_src_pdo = i;
7110 switch (port->state) {
7111 case SRC_UNATTACHED:
7112 case SRC_ATTACH_WAIT:
7114 tcpm_set_cc(port, tcpm_rp_cc(port));
7116 case SRC_SEND_CAPABILITIES:
7117 case SRC_SEND_CAPABILITIES_TIMEOUT:
7118 case SRC_NEGOTIATE_CAPABILITIES:
7120 case SRC_WAIT_NEW_CAPABILITIES:
7121 port->caps_count = 0;
7122 port->upcoming_state = SRC_SEND_CAPABILITIES;
7123 ret = tcpm_ams_start(port, POWER_NEGOTIATION);
7124 if (ret == -EAGAIN) {
7125 port->upcoming_state = INVALID_STATE;
7129 case SNK_NEGOTIATE_CAPABILITIES:
7130 case SNK_NEGOTIATE_PPS_CAPABILITIES:
7132 case SNK_TRANSITION_SINK:
7133 case SNK_TRANSITION_SINK_VBUS:
7134 if (port->pps_data.active)
7135 port->upcoming_state = SNK_NEGOTIATE_PPS_CAPABILITIES;
7136 else if (port->pd_capable)
7137 port->upcoming_state = SNK_NEGOTIATE_CAPABILITIES;
7141 port->update_sink_caps = true;
7143 ret = tcpm_ams_start(port, POWER_NEGOTIATION);
7144 if (ret == -EAGAIN) {
7145 port->upcoming_state = INVALID_STATE;
7153 port->port_source_caps = data->source_cap;
7154 port->port_sink_caps = data->sink_cap;
7155 typec_port_set_usb_power_delivery(p, NULL);
7156 port->selected_pd = pd;
7157 typec_port_set_usb_power_delivery(p, port->selected_pd);
7159 mutex_unlock(&port->lock);
7163 static const struct typec_operations tcpm_ops = {
7164 .try_role = tcpm_try_role,
7165 .dr_set = tcpm_dr_set,
7166 .pr_set = tcpm_pr_set,
7167 .vconn_set = tcpm_vconn_set,
7168 .port_type_set = tcpm_port_type_set,
7169 .pd_get = tcpm_pd_get,
7170 .pd_set = tcpm_pd_set
7173 void tcpm_tcpc_reset(struct tcpm_port *port)
7175 mutex_lock(&port->lock);
7176 /* XXX: Maintain PD connection if possible? */
7178 mutex_unlock(&port->lock);
7180 EXPORT_SYMBOL_GPL(tcpm_tcpc_reset);
7182 static void tcpm_port_unregister_pd(struct tcpm_port *port)
7186 port->port_sink_caps = NULL;
7187 port->port_source_caps = NULL;
7188 for (i = 0; i < port->pd_count; i++) {
7189 usb_power_delivery_unregister_capabilities(port->pd_list[i]->sink_cap);
7190 usb_power_delivery_unregister_capabilities(port->pd_list[i]->source_cap);
7191 devm_kfree(port->dev, port->pd_list[i]);
7192 port->pd_list[i] = NULL;
7193 usb_power_delivery_unregister(port->pds[i]);
7194 port->pds[i] = NULL;
7198 static int tcpm_port_register_pd(struct tcpm_port *port)
7200 u16 pd_revision = port->typec_caps.pd_revision;
7201 u16 pd_version = port->pd_rev.ver_major << 8 | port->pd_rev.ver_minor;
7202 struct usb_power_delivery_desc desc = { pd_revision, pd_version };
7203 struct usb_power_delivery_capabilities *cap;
7206 if (!port->nr_src_pdo && !port->nr_snk_pdo)
7209 for (i = 0; i < port->pd_count; i++) {
7210 port->pds[i] = usb_power_delivery_register(port->dev, &desc);
7211 if (IS_ERR(port->pds[i])) {
7212 ret = PTR_ERR(port->pds[i]);
7213 goto err_unregister;
7215 port->pd_list[i]->pd = port->pds[i];
7217 if (port->pd_list[i]->source_desc.pdo[0]) {
7218 cap = usb_power_delivery_register_capabilities(port->pds[i],
7219 &port->pd_list[i]->source_desc);
7222 goto err_unregister;
7224 port->pd_list[i]->source_cap = cap;
7227 if (port->pd_list[i]->sink_desc.pdo[0]) {
7228 cap = usb_power_delivery_register_capabilities(port->pds[i],
7229 &port->pd_list[i]->sink_desc);
7232 goto err_unregister;
7234 port->pd_list[i]->sink_cap = cap;
7238 port->port_source_caps = port->pd_list[0]->source_cap;
7239 port->port_sink_caps = port->pd_list[0]->sink_cap;
7240 port->selected_pd = port->pds[0];
7244 tcpm_port_unregister_pd(port);
7249 static void tcpm_fw_get_timings(struct tcpm_port *port, struct fwnode_handle *fwnode)
7254 ret = fwnode_property_read_u32(fwnode, "sink-wait-cap-time-ms", &val);
7256 port->timings.sink_wait_cap_time = val;
7258 port->timings.sink_wait_cap_time = PD_T_SINK_WAIT_CAP;
7260 ret = fwnode_property_read_u32(fwnode, "ps-source-off-time-ms", &val);
7262 port->timings.ps_src_off_time = val;
7264 port->timings.ps_src_off_time = PD_T_PS_SOURCE_OFF;
7266 ret = fwnode_property_read_u32(fwnode, "cc-debounce-time-ms", &val);
7268 port->timings.cc_debounce_time = val;
7270 port->timings.cc_debounce_time = PD_T_CC_DEBOUNCE;
7272 ret = fwnode_property_read_u32(fwnode, "sink-bc12-completion-time-ms", &val);
7274 port->timings.snk_bc12_cmpletion_time = val;
7277 static int tcpm_fw_get_caps(struct tcpm_port *port, struct fwnode_handle *fwnode)
7279 struct fwnode_handle *capabilities, *caps = NULL;
7280 unsigned int nr_src_pdo, nr_snk_pdo;
7281 const char *opmode_str;
7282 u32 *src_pdo, *snk_pdo;
7283 u32 uw, frs_current;
7291 * This fwnode has a "compatible" property, but is never populated as a
7292 * struct device. Instead we simply parse it to read the properties.
7293 * This it breaks fw_devlink=on. To maintain backward compatibility
7294 * with existing DT files, we work around this by deleting any
7295 * fwnode_links to/from this fwnode.
7297 fw_devlink_purge_absent_suppliers(fwnode);
7299 ret = typec_get_fw_cap(&port->typec_caps, fwnode);
7305 if (fwnode_property_read_bool(fwnode, "accessory-mode-audio"))
7306 port->typec_caps.accessory[mode++] = TYPEC_ACCESSORY_AUDIO;
7308 if (fwnode_property_read_bool(fwnode, "accessory-mode-debug"))
7309 port->typec_caps.accessory[mode++] = TYPEC_ACCESSORY_DEBUG;
7311 port->port_type = port->typec_caps.type;
7312 port->pd_supported = !fwnode_property_read_bool(fwnode, "pd-disable");
7313 port->slow_charger_loop = fwnode_property_read_bool(fwnode, "slow-charger-loop");
7314 port->self_powered = fwnode_property_read_bool(fwnode, "self-powered");
7316 if (!port->pd_supported) {
7317 ret = fwnode_property_read_string(fwnode, "typec-power-opmode", &opmode_str);
7320 ret = typec_find_pwr_opmode(opmode_str);
7323 port->src_rp = tcpm_pwr_opmode_to_rp(ret);
7327 /* The following code are applicable to pd-capable ports, i.e. pd_supported is true. */
7329 /* FRS can only be supported by DRP ports */
7330 if (port->port_type == TYPEC_PORT_DRP) {
7331 ret = fwnode_property_read_u32(fwnode, "new-source-frs-typec-current",
7333 if (!ret && frs_current <= FRS_5V_3A)
7334 port->new_source_frs_current = frs_current;
7340 /* For the backward compatibility, "capabilities" node is optional. */
7341 capabilities = fwnode_get_named_child_node(fwnode, "capabilities");
7342 if (!capabilities) {
7345 port->pd_count = fwnode_get_child_node_count(capabilities);
7346 if (!port->pd_count) {
7348 goto put_capabilities;
7352 port->pds = devm_kcalloc(port->dev, port->pd_count, sizeof(struct usb_power_delivery *),
7356 goto put_capabilities;
7359 port->pd_list = devm_kcalloc(port->dev, port->pd_count, sizeof(struct pd_data *),
7361 if (!port->pd_list) {
7363 goto put_capabilities;
7366 for (i = 0; i < port->pd_count; i++) {
7367 port->pd_list[i] = devm_kzalloc(port->dev, sizeof(struct pd_data), GFP_KERNEL);
7368 if (!port->pd_list[i]) {
7370 goto put_capabilities;
7373 src_pdo = port->pd_list[i]->source_desc.pdo;
7374 port->pd_list[i]->source_desc.role = TYPEC_SOURCE;
7375 snk_pdo = port->pd_list[i]->sink_desc.pdo;
7376 port->pd_list[i]->sink_desc.role = TYPEC_SINK;
7378 /* If "capabilities" is NULL, fall back to single pd cap population. */
7382 caps = fwnode_get_next_child_node(capabilities, caps);
7384 if (port->port_type != TYPEC_PORT_SNK) {
7385 ret = fwnode_property_count_u32(caps, "source-pdos");
7393 nr_src_pdo = min(ret, PDO_MAX_OBJECTS);
7394 ret = fwnode_property_read_u32_array(caps, "source-pdos", src_pdo,
7399 ret = tcpm_validate_caps(port, src_pdo, nr_src_pdo);
7404 port->nr_src_pdo = nr_src_pdo;
7405 memcpy_and_pad(port->src_pdo, sizeof(u32) * PDO_MAX_OBJECTS,
7406 port->pd_list[0]->source_desc.pdo,
7407 sizeof(u32) * nr_src_pdo,
7412 if (port->port_type != TYPEC_PORT_SRC) {
7413 ret = fwnode_property_count_u32(caps, "sink-pdos");
7422 nr_snk_pdo = min(ret, PDO_MAX_OBJECTS);
7423 ret = fwnode_property_read_u32_array(caps, "sink-pdos", snk_pdo,
7428 ret = tcpm_validate_caps(port, snk_pdo, nr_snk_pdo);
7432 if (fwnode_property_read_u32(caps, "op-sink-microwatt", &uw) < 0) {
7437 port->pd_list[i]->operating_snk_mw = uw / 1000;
7440 port->nr_snk_pdo = nr_snk_pdo;
7441 memcpy_and_pad(port->snk_pdo, sizeof(u32) * PDO_MAX_OBJECTS,
7442 port->pd_list[0]->sink_desc.pdo,
7443 sizeof(u32) * nr_snk_pdo,
7445 port->operating_snk_mw = port->pd_list[0]->operating_snk_mw;
7452 fwnode_handle_put(caps);
7454 fwnode_handle_put(capabilities);
7458 static int tcpm_fw_get_snk_vdos(struct tcpm_port *port, struct fwnode_handle *fwnode)
7462 /* sink-vdos is optional */
7463 ret = fwnode_property_count_u32(fwnode, "sink-vdos");
7467 port->nr_snk_vdo = min(ret, VDO_MAX_OBJECTS);
7468 if (port->nr_snk_vdo) {
7469 ret = fwnode_property_read_u32_array(fwnode, "sink-vdos",
7476 /* If sink-vdos is found, sink-vdos-v1 is expected for backward compatibility. */
7477 if (port->nr_snk_vdo) {
7478 ret = fwnode_property_count_u32(fwnode, "sink-vdos-v1");
7484 port->nr_snk_vdo_v1 = min(ret, VDO_MAX_OBJECTS);
7485 ret = fwnode_property_read_u32_array(fwnode, "sink-vdos-v1",
7487 port->nr_snk_vdo_v1);
7495 static void tcpm_fw_get_pd_revision(struct tcpm_port *port, struct fwnode_handle *fwnode)
7500 ret = fwnode_property_count_u8(fwnode, "pd-revision");
7501 if (!ret || ret != 4) {
7502 tcpm_log(port, "Unable to find pd-revision property or incorrect array size");
7506 ret = fwnode_property_read_u8_array(fwnode, "pd-revision", val, 4);
7508 tcpm_log(port, "Failed to parse pd-revision, ret:(%d)", ret);
7512 port->pd_rev.rev_major = val[0];
7513 port->pd_rev.rev_minor = val[1];
7514 port->pd_rev.ver_major = val[2];
7515 port->pd_rev.ver_minor = val[3];
7518 /* Power Supply access to expose source power information */
7519 enum tcpm_psy_online_states {
7520 TCPM_PSY_OFFLINE = 0,
7521 TCPM_PSY_FIXED_ONLINE,
7522 TCPM_PSY_PROG_ONLINE,
7525 static enum power_supply_property tcpm_psy_props[] = {
7526 POWER_SUPPLY_PROP_USB_TYPE,
7527 POWER_SUPPLY_PROP_ONLINE,
7528 POWER_SUPPLY_PROP_VOLTAGE_MIN,
7529 POWER_SUPPLY_PROP_VOLTAGE_MAX,
7530 POWER_SUPPLY_PROP_VOLTAGE_NOW,
7531 POWER_SUPPLY_PROP_CURRENT_MAX,
7532 POWER_SUPPLY_PROP_CURRENT_NOW,
7535 static int tcpm_psy_get_online(struct tcpm_port *port,
7536 union power_supply_propval *val)
7538 if (port->vbus_charge) {
7539 if (port->pps_data.active)
7540 val->intval = TCPM_PSY_PROG_ONLINE;
7542 val->intval = TCPM_PSY_FIXED_ONLINE;
7544 val->intval = TCPM_PSY_OFFLINE;
7550 static int tcpm_psy_get_voltage_min(struct tcpm_port *port,
7551 union power_supply_propval *val)
7553 if (port->pps_data.active)
7554 val->intval = port->pps_data.min_volt * 1000;
7556 val->intval = port->supply_voltage * 1000;
7561 static int tcpm_psy_get_voltage_max(struct tcpm_port *port,
7562 union power_supply_propval *val)
7564 if (port->pps_data.active)
7565 val->intval = port->pps_data.max_volt * 1000;
7567 val->intval = port->supply_voltage * 1000;
7572 static int tcpm_psy_get_voltage_now(struct tcpm_port *port,
7573 union power_supply_propval *val)
7575 val->intval = port->supply_voltage * 1000;
7580 static int tcpm_psy_get_current_max(struct tcpm_port *port,
7581 union power_supply_propval *val)
7583 if (port->pps_data.active)
7584 val->intval = port->pps_data.max_curr * 1000;
7586 val->intval = port->current_limit * 1000;
7591 static int tcpm_psy_get_current_now(struct tcpm_port *port,
7592 union power_supply_propval *val)
7594 val->intval = port->current_limit * 1000;
7599 static int tcpm_psy_get_input_power_limit(struct tcpm_port *port,
7600 union power_supply_propval *val)
7602 unsigned int src_mv, src_ma, max_src_uw = 0;
7603 unsigned int i, tmp;
7605 for (i = 0; i < port->nr_source_caps; i++) {
7606 u32 pdo = port->source_caps[i];
7608 if (pdo_type(pdo) == PDO_TYPE_FIXED) {
7609 src_mv = pdo_fixed_voltage(pdo);
7610 src_ma = pdo_max_current(pdo);
7611 tmp = src_mv * src_ma;
7612 max_src_uw = max(tmp, max_src_uw);
7616 val->intval = max_src_uw;
7620 static int tcpm_psy_get_prop(struct power_supply *psy,
7621 enum power_supply_property psp,
7622 union power_supply_propval *val)
7624 struct tcpm_port *port = power_supply_get_drvdata(psy);
7628 case POWER_SUPPLY_PROP_USB_TYPE:
7629 val->intval = port->usb_type;
7631 case POWER_SUPPLY_PROP_ONLINE:
7632 ret = tcpm_psy_get_online(port, val);
7634 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
7635 ret = tcpm_psy_get_voltage_min(port, val);
7637 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
7638 ret = tcpm_psy_get_voltage_max(port, val);
7640 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
7641 ret = tcpm_psy_get_voltage_now(port, val);
7643 case POWER_SUPPLY_PROP_CURRENT_MAX:
7644 ret = tcpm_psy_get_current_max(port, val);
7646 case POWER_SUPPLY_PROP_CURRENT_NOW:
7647 ret = tcpm_psy_get_current_now(port, val);
7649 case POWER_SUPPLY_PROP_INPUT_POWER_LIMIT:
7650 tcpm_psy_get_input_power_limit(port, val);
7660 static int tcpm_psy_set_online(struct tcpm_port *port,
7661 const union power_supply_propval *val)
7665 switch (val->intval) {
7666 case TCPM_PSY_FIXED_ONLINE:
7667 ret = tcpm_pps_activate(port, false);
7669 case TCPM_PSY_PROG_ONLINE:
7670 ret = tcpm_pps_activate(port, true);
7680 static int tcpm_psy_set_prop(struct power_supply *psy,
7681 enum power_supply_property psp,
7682 const union power_supply_propval *val)
7684 struct tcpm_port *port = power_supply_get_drvdata(psy);
7688 * All the properties below are related to USB PD. The check needs to be
7689 * property specific when a non-pd related property is added.
7691 if (!port->pd_supported)
7695 case POWER_SUPPLY_PROP_ONLINE:
7696 ret = tcpm_psy_set_online(port, val);
7698 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
7699 ret = tcpm_pps_set_out_volt(port, val->intval / 1000);
7701 case POWER_SUPPLY_PROP_CURRENT_NOW:
7702 if (val->intval > port->pps_data.max_curr * 1000)
7705 ret = tcpm_pps_set_op_curr(port, val->intval / 1000);
7711 power_supply_changed(port->psy);
7715 static int tcpm_psy_prop_writeable(struct power_supply *psy,
7716 enum power_supply_property psp)
7719 case POWER_SUPPLY_PROP_ONLINE:
7720 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
7721 case POWER_SUPPLY_PROP_CURRENT_NOW:
7728 static const char *tcpm_psy_name_prefix = "tcpm-source-psy-";
7730 static int devm_tcpm_psy_register(struct tcpm_port *port)
7732 struct power_supply_config psy_cfg = {};
7733 const char *port_dev_name = dev_name(port->dev);
7734 size_t psy_name_len = strlen(tcpm_psy_name_prefix) +
7735 strlen(port_dev_name) + 1;
7738 psy_cfg.drv_data = port;
7739 psy_cfg.fwnode = dev_fwnode(port->dev);
7740 psy_name = devm_kzalloc(port->dev, psy_name_len, GFP_KERNEL);
7744 snprintf(psy_name, psy_name_len, "%s%s", tcpm_psy_name_prefix,
7746 port->psy_desc.name = psy_name;
7747 port->psy_desc.type = POWER_SUPPLY_TYPE_USB;
7748 port->psy_desc.usb_types = BIT(POWER_SUPPLY_USB_TYPE_C) |
7749 BIT(POWER_SUPPLY_USB_TYPE_PD) |
7750 BIT(POWER_SUPPLY_USB_TYPE_PD_PPS);
7751 port->psy_desc.properties = tcpm_psy_props;
7752 port->psy_desc.num_properties = ARRAY_SIZE(tcpm_psy_props);
7753 port->psy_desc.get_property = tcpm_psy_get_prop;
7754 port->psy_desc.set_property = tcpm_psy_set_prop;
7755 port->psy_desc.property_is_writeable = tcpm_psy_prop_writeable;
7757 port->usb_type = POWER_SUPPLY_USB_TYPE_C;
7759 port->psy = devm_power_supply_register(port->dev, &port->psy_desc,
7762 return PTR_ERR_OR_ZERO(port->psy);
7765 static enum hrtimer_restart state_machine_timer_handler(struct hrtimer *timer)
7767 struct tcpm_port *port = container_of(timer, struct tcpm_port, state_machine_timer);
7769 if (port->registered)
7770 kthread_queue_work(port->wq, &port->state_machine);
7771 return HRTIMER_NORESTART;
7774 static enum hrtimer_restart vdm_state_machine_timer_handler(struct hrtimer *timer)
7776 struct tcpm_port *port = container_of(timer, struct tcpm_port, vdm_state_machine_timer);
7778 if (port->registered)
7779 kthread_queue_work(port->wq, &port->vdm_state_machine);
7780 return HRTIMER_NORESTART;
7783 static enum hrtimer_restart enable_frs_timer_handler(struct hrtimer *timer)
7785 struct tcpm_port *port = container_of(timer, struct tcpm_port, enable_frs_timer);
7787 if (port->registered)
7788 kthread_queue_work(port->wq, &port->enable_frs);
7789 return HRTIMER_NORESTART;
7792 static enum hrtimer_restart send_discover_timer_handler(struct hrtimer *timer)
7794 struct tcpm_port *port = container_of(timer, struct tcpm_port, send_discover_timer);
7796 if (port->registered)
7797 kthread_queue_work(port->wq, &port->send_discover_work);
7798 return HRTIMER_NORESTART;
7801 struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
7803 struct tcpm_port *port;
7806 if (!dev || !tcpc ||
7807 !tcpc->get_vbus || !tcpc->set_cc || !tcpc->get_cc ||
7808 !tcpc->set_polarity || !tcpc->set_vconn || !tcpc->set_vbus ||
7809 !tcpc->set_pd_rx || !tcpc->set_roles || !tcpc->pd_transmit)
7810 return ERR_PTR(-EINVAL);
7812 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
7814 return ERR_PTR(-ENOMEM);
7819 mutex_init(&port->lock);
7820 mutex_init(&port->swap_lock);
7822 port->wq = kthread_run_worker(0, dev_name(dev));
7823 if (IS_ERR(port->wq))
7824 return ERR_CAST(port->wq);
7825 sched_set_fifo(port->wq->task);
7827 kthread_init_work(&port->state_machine, tcpm_state_machine_work);
7828 kthread_init_work(&port->vdm_state_machine, vdm_state_machine_work);
7829 kthread_init_work(&port->event_work, tcpm_pd_event_handler);
7830 kthread_init_work(&port->enable_frs, tcpm_enable_frs_work);
7831 kthread_init_work(&port->send_discover_work, tcpm_send_discover_work);
7832 hrtimer_setup(&port->state_machine_timer, state_machine_timer_handler, CLOCK_MONOTONIC,
7834 hrtimer_setup(&port->vdm_state_machine_timer, vdm_state_machine_timer_handler,
7835 CLOCK_MONOTONIC, HRTIMER_MODE_REL);
7836 hrtimer_setup(&port->enable_frs_timer, enable_frs_timer_handler, CLOCK_MONOTONIC,
7838 hrtimer_setup(&port->send_discover_timer, send_discover_timer_handler, CLOCK_MONOTONIC,
7841 spin_lock_init(&port->pd_event_lock);
7843 init_completion(&port->tx_complete);
7844 init_completion(&port->swap_complete);
7845 init_completion(&port->pps_complete);
7846 tcpm_debugfs_init(port);
7848 err = tcpm_fw_get_caps(port, tcpc->fwnode);
7850 goto out_destroy_wq;
7851 err = tcpm_fw_get_snk_vdos(port, tcpc->fwnode);
7853 goto out_destroy_wq;
7855 tcpm_fw_get_timings(port, tcpc->fwnode);
7856 tcpm_fw_get_pd_revision(port, tcpc->fwnode);
7858 port->try_role = port->typec_caps.prefer_role;
7860 port->typec_caps.revision = 0x0120; /* Type-C spec release 1.2 */
7862 if (port->pd_rev.rev_major)
7863 port->typec_caps.pd_revision = port->pd_rev.rev_major << 8 |
7864 port->pd_rev.rev_minor;
7866 port->typec_caps.pd_revision = 0x0300; /* USB-PD spec release 3.0 */
7868 port->typec_caps.svdm_version = SVDM_VER_2_0;
7869 port->typec_caps.driver_data = port;
7870 port->typec_caps.ops = &tcpm_ops;
7871 port->typec_caps.orientation_aware = 1;
7873 port->partner_desc.identity = &port->partner_ident;
7875 port->role_sw = usb_role_switch_get(port->dev);
7877 port->role_sw = fwnode_usb_role_switch_get(tcpc->fwnode);
7878 if (IS_ERR(port->role_sw)) {
7879 err = PTR_ERR(port->role_sw);
7880 goto out_destroy_wq;
7883 err = devm_tcpm_psy_register(port);
7885 goto out_role_sw_put;
7886 power_supply_changed(port->psy);
7888 err = tcpm_port_register_pd(port);
7890 goto out_role_sw_put;
7893 port->typec_caps.pd = port->pds[0];
7895 port->typec_port = typec_register_port(port->dev, &port->typec_caps);
7896 if (IS_ERR(port->typec_port)) {
7897 err = PTR_ERR(port->typec_port);
7898 goto out_unregister_pd;
7901 typec_port_register_altmodes(port->typec_port,
7902 &tcpm_altmode_ops, port,
7903 port->port_altmode, ALTMODE_DISCOVERY_MAX);
7904 typec_port_register_cable_ops(port->port_altmode, ARRAY_SIZE(port->port_altmode),
7906 port->registered = true;
7908 mutex_lock(&port->lock);
7910 mutex_unlock(&port->lock);
7912 tcpm_log(port, "%s: registered", dev_name(dev));
7916 tcpm_port_unregister_pd(port);
7918 usb_role_switch_put(port->role_sw);
7920 tcpm_debugfs_exit(port);
7921 kthread_destroy_worker(port->wq);
7922 return ERR_PTR(err);
7924 EXPORT_SYMBOL_GPL(tcpm_register_port);
7926 void tcpm_unregister_port(struct tcpm_port *port)
7930 port->registered = false;
7931 kthread_destroy_worker(port->wq);
7933 hrtimer_cancel(&port->send_discover_timer);
7934 hrtimer_cancel(&port->enable_frs_timer);
7935 hrtimer_cancel(&port->vdm_state_machine_timer);
7936 hrtimer_cancel(&port->state_machine_timer);
7938 tcpm_reset_port(port);
7940 tcpm_port_unregister_pd(port);
7942 for (i = 0; i < ARRAY_SIZE(port->port_altmode); i++)
7943 typec_unregister_altmode(port->port_altmode[i]);
7944 typec_unregister_port(port->typec_port);
7945 usb_role_switch_put(port->role_sw);
7946 tcpm_debugfs_exit(port);
7948 EXPORT_SYMBOL_GPL(tcpm_unregister_port);
7950 MODULE_AUTHOR("Guenter Roeck <groeck@chromium.org>");
7951 MODULE_DESCRIPTION("USB Type-C Port Manager");
7952 MODULE_LICENSE("GPL");