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/usb.h>
25 #include <linux/usb/pd.h>
26 #include <linux/usb/pd_ado.h>
27 #include <linux/usb/pd_bdo.h>
28 #include <linux/usb/pd_ext_sdb.h>
29 #include <linux/usb/pd_vdo.h>
30 #include <linux/usb/role.h>
31 #include <linux/usb/tcpm.h>
32 #include <linux/usb/typec_altmode.h>
34 #include <uapi/linux/sched/types.h>
36 #define FOREACH_STATE(S) \
39 S(CHECK_CONTAMINANT), \
44 S(SRC_SEND_CAPABILITIES), \
45 S(SRC_SEND_CAPABILITIES_TIMEOUT), \
46 S(SRC_NEGOTIATE_CAPABILITIES), \
47 S(SRC_TRANSITION_SUPPLY), \
49 S(SRC_WAIT_NEW_CAPABILITIES), \
57 S(SNK_DISCOVERY_DEBOUNCE), \
58 S(SNK_DISCOVERY_DEBOUNCE_DONE), \
59 S(SNK_WAIT_CAPABILITIES), \
60 S(SNK_NEGOTIATE_CAPABILITIES), \
61 S(SNK_NEGOTIATE_PPS_CAPABILITIES), \
62 S(SNK_TRANSITION_SINK), \
63 S(SNK_TRANSITION_SINK_VBUS), \
67 S(DEBUG_ACC_ATTACHED), \
68 S(AUDIO_ACC_ATTACHED), \
69 S(AUDIO_ACC_DEBOUNCE), \
72 S(HARD_RESET_START), \
73 S(SRC_HARD_RESET_VBUS_OFF), \
74 S(SRC_HARD_RESET_VBUS_ON), \
75 S(SNK_HARD_RESET_SINK_OFF), \
76 S(SNK_HARD_RESET_WAIT_VBUS), \
77 S(SNK_HARD_RESET_SINK_ON), \
80 S(SRC_SOFT_RESET_WAIT_SNK_TX), \
86 S(DR_SWAP_SEND_TIMEOUT), \
88 S(DR_SWAP_CHANGE_DR), \
92 S(PR_SWAP_SEND_TIMEOUT), \
95 S(PR_SWAP_SRC_SNK_TRANSITION_OFF), \
96 S(PR_SWAP_SRC_SNK_SOURCE_OFF), \
97 S(PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED), \
98 S(PR_SWAP_SRC_SNK_SINK_ON), \
99 S(PR_SWAP_SNK_SRC_SINK_OFF), \
100 S(PR_SWAP_SNK_SRC_SOURCE_ON), \
101 S(PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP), \
103 S(VCONN_SWAP_ACCEPT), \
104 S(VCONN_SWAP_SEND), \
105 S(VCONN_SWAP_SEND_TIMEOUT), \
106 S(VCONN_SWAP_CANCEL), \
107 S(VCONN_SWAP_START), \
108 S(VCONN_SWAP_WAIT_FOR_VCONN), \
109 S(VCONN_SWAP_TURN_ON_VCONN), \
110 S(VCONN_SWAP_TURN_OFF_VCONN), \
113 S(FR_SWAP_SEND_TIMEOUT), \
114 S(FR_SWAP_SNK_SRC_TRANSITION_TO_OFF), \
115 S(FR_SWAP_SNK_SRC_NEW_SINK_READY), \
116 S(FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED), \
121 S(SNK_TRY_WAIT_DEBOUNCE), \
122 S(SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS), \
124 S(SRC_TRYWAIT_DEBOUNCE), \
125 S(SRC_TRYWAIT_UNATTACHED), \
129 S(SRC_TRY_DEBOUNCE), \
131 S(SNK_TRYWAIT_DEBOUNCE), \
132 S(SNK_TRYWAIT_VBUS), \
135 S(GET_STATUS_SEND), \
136 S(GET_STATUS_SEND_TIMEOUT), \
137 S(GET_PPS_STATUS_SEND), \
138 S(GET_PPS_STATUS_SEND_TIMEOUT), \
141 S(GET_SINK_CAP_TIMEOUT), \
145 S(PORT_RESET_WAIT_OFF), \
150 #define FOREACH_AMS(S) \
152 S(POWER_NEGOTIATION), \
157 S(GET_SOURCE_CAPABILITIES), \
158 S(GET_SINK_CAPABILITIES), \
159 S(POWER_ROLE_SWAP), \
164 S(GETTING_SOURCE_EXTENDED_CAPABILITIES),\
165 S(GETTING_SOURCE_SINK_STATUS), \
166 S(GETTING_BATTERY_CAPABILITIES), \
167 S(GETTING_BATTERY_STATUS), \
168 S(GETTING_MANUFACTURER_INFORMATION), \
170 S(FIRMWARE_UPDATE), \
171 S(DISCOVER_IDENTITY), \
172 S(SOURCE_STARTUP_CABLE_PLUG_DISCOVER_IDENTITY), \
175 S(DFP_TO_UFP_ENTER_MODE), \
176 S(DFP_TO_UFP_EXIT_MODE), \
177 S(DFP_TO_CABLE_PLUG_ENTER_MODE), \
178 S(DFP_TO_CABLE_PLUG_EXIT_MODE), \
181 S(UNSTRUCTURED_VDMS), \
182 S(STRUCTURED_VDMS), \
186 #define GENERATE_ENUM(e) e
187 #define GENERATE_STRING(s) #s
190 FOREACH_STATE(GENERATE_ENUM)
193 static const char * const tcpm_states[] = {
194 FOREACH_STATE(GENERATE_STRING)
198 FOREACH_AMS(GENERATE_ENUM)
201 static const char * const tcpm_ams_str[] = {
202 FOREACH_AMS(GENERATE_STRING)
206 VDM_STATE_ERR_BUSY = -3,
207 VDM_STATE_ERR_SEND = -2,
208 VDM_STATE_ERR_TMOUT = -1,
210 /* Anything >0 represents an active state */
213 VDM_STATE_WAIT_RSP_BUSY = 3,
214 VDM_STATE_SEND_MESSAGE = 4,
217 enum pd_msg_request {
221 PD_MSG_CTRL_NOT_SUPP,
222 PD_MSG_DATA_SINK_CAP,
223 PD_MSG_DATA_SOURCE_CAP,
228 ADEV_NOTIFY_USB_AND_QUEUE_VDM,
230 ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL,
235 * Initial current capability of the new source when vSafe5V is applied during PD3.0 Fast Role Swap.
236 * Based on "Table 6-14 Fixed Supply PDO - Sink" of "USB Power Delivery Specification Revision 3.0,
239 enum frs_typec_current {
246 /* Events from low level driver */
248 #define TCPM_CC_EVENT BIT(0)
249 #define TCPM_VBUS_EVENT BIT(1)
250 #define TCPM_RESET_EVENT BIT(2)
251 #define TCPM_FRS_EVENT BIT(3)
252 #define TCPM_SOURCING_VBUS BIT(4)
253 #define TCPM_PORT_CLEAN BIT(5)
255 #define LOG_BUFFER_ENTRIES 1024
256 #define LOG_BUFFER_ENTRY_SIZE 128
258 /* Alternate mode support */
260 #define SVID_DISCOVERY_MAX 16
261 #define ALTMODE_DISCOVERY_MAX (SVID_DISCOVERY_MAX * MODE_DISCOVERY_MAX)
263 #define GET_SINK_CAP_RETRY_MS 100
264 #define SEND_DISCOVER_RETRY_MS 100
266 struct pd_mode_data {
267 int svid_index; /* current SVID index */
269 u16 svids[SVID_DISCOVERY_MAX];
270 int altmodes; /* number of alternate modes */
271 struct typec_altmode_desc altmode_desc[ALTMODE_DISCOVERY_MAX];
275 * @min_volt: Actual min voltage at the local port
276 * @req_min_volt: Requested min voltage to the port partner
277 * @max_volt: Actual max voltage at the local port
278 * @req_max_volt: Requested max voltage to the port partner
279 * @max_curr: Actual max current at the local port
280 * @req_max_curr: Requested max current of the port partner
281 * @req_out_volt: Requested output voltage to the port partner
282 * @req_op_curr: Requested operating current to the port partner
283 * @supported: Parter has at least one APDO hence supports PPS
284 * @active: PPS mode is active
302 struct mutex lock; /* tcpm state machine lock */
303 struct kthread_worker *wq;
305 struct typec_capability typec_caps;
306 struct typec_port *typec_port;
308 struct tcpc_dev *tcpc;
309 struct usb_role_switch *role_sw;
311 enum typec_role vconn_role;
312 enum typec_role pwr_role;
313 enum typec_data_role data_role;
314 enum typec_pwr_opmode pwr_opmode;
316 struct usb_pd_identity partner_ident;
317 struct typec_partner_desc partner_desc;
318 struct typec_partner *partner;
320 enum typec_cc_status cc_req;
321 enum typec_cc_status src_rp; /* work only if pd_supported == false */
323 enum typec_cc_status cc1;
324 enum typec_cc_status cc2;
325 enum typec_cc_polarity polarity;
331 enum typec_port_type port_type;
334 * Set to true when vbus is greater than VSAFE5V min.
335 * Set to false when vbus falls below vSinkDisconnect max threshold.
340 * Set to true when vbus is less than VSAFE0V max.
341 * Set to false when vbus is greater than VSAFE0V max.
349 /* Set to true when Discover_Identity Command is expected to be sent in Ready states. */
357 enum pd_msg_request queued_message;
359 enum tcpm_state enter_state;
360 enum tcpm_state prev_state;
361 enum tcpm_state state;
362 enum tcpm_state delayed_state;
363 ktime_t delayed_runtime;
364 unsigned long delay_ms;
366 spinlock_t pd_event_lock;
369 struct kthread_work event_work;
370 struct hrtimer state_machine_timer;
371 struct kthread_work state_machine;
372 struct hrtimer vdm_state_machine_timer;
373 struct kthread_work vdm_state_machine;
374 struct hrtimer enable_frs_timer;
375 struct kthread_work enable_frs;
376 struct hrtimer send_discover_timer;
377 struct kthread_work send_discover_work;
378 bool state_machine_running;
379 /* Set to true when VDM State Machine has following actions. */
382 struct completion tx_complete;
383 enum tcpm_transmit_status tx_status;
385 struct mutex swap_lock; /* swap command lock */
387 bool non_pd_role_swap;
388 struct completion swap_complete;
391 unsigned int negotiated_rev;
392 unsigned int message_id;
393 unsigned int caps_count;
394 unsigned int hard_reset_count;
396 bool explicit_contract;
397 unsigned int rx_msgid;
400 struct usb_power_delivery *pd;
401 struct usb_power_delivery_capabilities *port_source_caps;
402 struct usb_power_delivery_capabilities *port_sink_caps;
403 struct usb_power_delivery *partner_pd;
404 struct usb_power_delivery_capabilities *partner_source_caps;
405 struct usb_power_delivery_capabilities *partner_sink_caps;
407 /* Partner capabilities/requests */
409 u32 source_caps[PDO_MAX_OBJECTS];
410 unsigned int nr_source_caps;
411 u32 sink_caps[PDO_MAX_OBJECTS];
412 unsigned int nr_sink_caps;
414 /* Local capabilities */
415 u32 src_pdo[PDO_MAX_OBJECTS];
416 unsigned int nr_src_pdo;
417 u32 snk_pdo[PDO_MAX_OBJECTS];
418 unsigned int nr_snk_pdo;
419 u32 snk_vdo_v1[VDO_MAX_OBJECTS];
420 unsigned int nr_snk_vdo_v1;
421 u32 snk_vdo[VDO_MAX_OBJECTS];
422 unsigned int nr_snk_vdo;
424 unsigned int operating_snk_mw;
425 bool update_sink_caps;
427 /* Requested current / voltage to the port partner */
428 u32 req_current_limit;
429 u32 req_supply_voltage;
430 /* Actual current / voltage limit of the local port */
434 /* Used to export TA voltage and current */
435 struct power_supply *psy;
436 struct power_supply_desc psy_desc;
437 enum power_supply_usb_type usb_type;
441 /* PD state for Vendor Defined Messages */
442 enum vdm_states vdm_state;
444 /* next Vendor Defined Message to send */
445 u32 vdo_data[VDO_MAX_SIZE];
447 /* VDO to retry if UFP responder replied busy */
451 struct pd_pps_data pps_data;
452 struct completion pps_complete;
456 /* Alternate mode data */
457 struct pd_mode_data mode_data;
458 struct typec_altmode *partner_altmode[ALTMODE_DISCOVERY_MAX];
459 struct typec_altmode *port_altmode[ALTMODE_DISCOVERY_MAX];
461 /* Deadline in jiffies to exit src_try_wait state */
462 unsigned long max_wait;
464 /* port belongs to a self powered device */
468 enum frs_typec_current new_source_frs_current;
470 /* Sink caps have been queried */
473 /* Collision Avoidance and Atomic Message Sequence */
474 enum tcpm_state upcoming_state;
476 enum tcpm_ams next_ams;
479 /* Auto vbus discharge status */
480 bool auto_vbus_discharge_enabled;
483 * When set, port requests PD_P_SNK_STDBY_MW upon entering SNK_DISCOVERY and
484 * the actual current limit after RX of PD_CTRL_PSRDY for PD link,
485 * SNK_READY for non-pd link.
487 bool slow_charger_loop;
490 * When true indicates that the lower level drivers indicate potential presence
491 * of contaminant in the connector pins based on the tcpm state machine
494 bool potential_contaminant;
495 #ifdef CONFIG_DEBUG_FS
496 struct dentry *dentry;
497 struct mutex logbuffer_lock; /* log buffer access lock */
500 u8 *logbuffer[LOG_BUFFER_ENTRIES];
505 struct kthread_work work;
506 struct tcpm_port *port;
507 struct pd_message msg;
510 static const char * const pd_rev[] = {
516 #define tcpm_cc_is_sink(cc) \
517 ((cc) == TYPEC_CC_RP_DEF || (cc) == TYPEC_CC_RP_1_5 || \
518 (cc) == TYPEC_CC_RP_3_0)
520 #define tcpm_port_is_sink(port) \
521 ((tcpm_cc_is_sink((port)->cc1) && !tcpm_cc_is_sink((port)->cc2)) || \
522 (tcpm_cc_is_sink((port)->cc2) && !tcpm_cc_is_sink((port)->cc1)))
524 #define tcpm_cc_is_source(cc) ((cc) == TYPEC_CC_RD)
525 #define tcpm_cc_is_audio(cc) ((cc) == TYPEC_CC_RA)
526 #define tcpm_cc_is_open(cc) ((cc) == TYPEC_CC_OPEN)
528 #define tcpm_port_is_source(port) \
529 ((tcpm_cc_is_source((port)->cc1) && \
530 !tcpm_cc_is_source((port)->cc2)) || \
531 (tcpm_cc_is_source((port)->cc2) && \
532 !tcpm_cc_is_source((port)->cc1)))
534 #define tcpm_port_is_debug(port) \
535 (tcpm_cc_is_source((port)->cc1) && tcpm_cc_is_source((port)->cc2))
537 #define tcpm_port_is_audio(port) \
538 (tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_audio((port)->cc2))
540 #define tcpm_port_is_audio_detached(port) \
541 ((tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_open((port)->cc2)) || \
542 (tcpm_cc_is_audio((port)->cc2) && tcpm_cc_is_open((port)->cc1)))
544 #define tcpm_try_snk(port) \
545 ((port)->try_snk_count == 0 && (port)->try_role == TYPEC_SINK && \
546 (port)->port_type == TYPEC_PORT_DRP)
548 #define tcpm_try_src(port) \
549 ((port)->try_src_count == 0 && (port)->try_role == TYPEC_SOURCE && \
550 (port)->port_type == TYPEC_PORT_DRP)
552 #define tcpm_data_role_for_source(port) \
553 ((port)->typec_caps.data == TYPEC_PORT_UFP ? \
554 TYPEC_DEVICE : TYPEC_HOST)
556 #define tcpm_data_role_for_sink(port) \
557 ((port)->typec_caps.data == TYPEC_PORT_DFP ? \
558 TYPEC_HOST : TYPEC_DEVICE)
560 #define tcpm_sink_tx_ok(port) \
561 (tcpm_port_is_sink(port) && \
562 ((port)->cc1 == TYPEC_CC_RP_3_0 || (port)->cc2 == TYPEC_CC_RP_3_0))
564 #define tcpm_wait_for_discharge(port) \
565 (((port)->auto_vbus_discharge_enabled && !(port)->vbus_vsafe0v) ? PD_T_SAFE_0V : 0)
567 static enum tcpm_state tcpm_default_state(struct tcpm_port *port)
569 if (port->port_type == TYPEC_PORT_DRP) {
570 if (port->try_role == TYPEC_SINK)
571 return SNK_UNATTACHED;
572 else if (port->try_role == TYPEC_SOURCE)
573 return SRC_UNATTACHED;
574 /* Fall through to return SRC_UNATTACHED */
575 } else if (port->port_type == TYPEC_PORT_SNK) {
576 return SNK_UNATTACHED;
578 return SRC_UNATTACHED;
581 static bool tcpm_port_is_disconnected(struct tcpm_port *port)
583 return (!port->attached && port->cc1 == TYPEC_CC_OPEN &&
584 port->cc2 == TYPEC_CC_OPEN) ||
585 (port->attached && ((port->polarity == TYPEC_POLARITY_CC1 &&
586 port->cc1 == TYPEC_CC_OPEN) ||
587 (port->polarity == TYPEC_POLARITY_CC2 &&
588 port->cc2 == TYPEC_CC_OPEN)));
595 #ifdef CONFIG_DEBUG_FS
597 static bool tcpm_log_full(struct tcpm_port *port)
599 return port->logbuffer_tail ==
600 (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
604 static void _tcpm_log(struct tcpm_port *port, const char *fmt, va_list args)
606 char tmpbuffer[LOG_BUFFER_ENTRY_SIZE];
607 u64 ts_nsec = local_clock();
608 unsigned long rem_nsec;
610 mutex_lock(&port->logbuffer_lock);
611 if (!port->logbuffer[port->logbuffer_head]) {
612 port->logbuffer[port->logbuffer_head] =
613 kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL);
614 if (!port->logbuffer[port->logbuffer_head]) {
615 mutex_unlock(&port->logbuffer_lock);
620 vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args);
622 if (tcpm_log_full(port)) {
623 port->logbuffer_head = max(port->logbuffer_head - 1, 0);
624 strcpy(tmpbuffer, "overflow");
627 if (port->logbuffer_head < 0 ||
628 port->logbuffer_head >= LOG_BUFFER_ENTRIES) {
630 "Bad log buffer index %d\n", port->logbuffer_head);
634 if (!port->logbuffer[port->logbuffer_head]) {
636 "Log buffer index %d is NULL\n", port->logbuffer_head);
640 rem_nsec = do_div(ts_nsec, 1000000000);
641 scnprintf(port->logbuffer[port->logbuffer_head],
642 LOG_BUFFER_ENTRY_SIZE, "[%5lu.%06lu] %s",
643 (unsigned long)ts_nsec, rem_nsec / 1000,
645 port->logbuffer_head = (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
648 mutex_unlock(&port->logbuffer_lock);
652 static void tcpm_log(struct tcpm_port *port, const char *fmt, ...)
656 /* Do not log while disconnected and unattached */
657 if (tcpm_port_is_disconnected(port) &&
658 (port->state == SRC_UNATTACHED || port->state == SNK_UNATTACHED ||
659 port->state == TOGGLING || port->state == CHECK_CONTAMINANT))
663 _tcpm_log(port, fmt, args);
668 static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...)
673 _tcpm_log(port, fmt, args);
677 static void tcpm_log_source_caps(struct tcpm_port *port)
681 for (i = 0; i < port->nr_source_caps; i++) {
682 u32 pdo = port->source_caps[i];
683 enum pd_pdo_type type = pdo_type(pdo);
688 scnprintf(msg, sizeof(msg),
689 "%u mV, %u mA [%s%s%s%s%s%s]",
690 pdo_fixed_voltage(pdo),
691 pdo_max_current(pdo),
692 (pdo & PDO_FIXED_DUAL_ROLE) ?
694 (pdo & PDO_FIXED_SUSPEND) ?
696 (pdo & PDO_FIXED_HIGHER_CAP) ?
698 (pdo & PDO_FIXED_USB_COMM) ?
700 (pdo & PDO_FIXED_DATA_SWAP) ?
702 (pdo & PDO_FIXED_EXTPOWER) ?
706 scnprintf(msg, sizeof(msg),
708 pdo_min_voltage(pdo),
709 pdo_max_voltage(pdo),
710 pdo_max_current(pdo));
713 scnprintf(msg, sizeof(msg),
715 pdo_min_voltage(pdo),
716 pdo_max_voltage(pdo),
720 if (pdo_apdo_type(pdo) == APDO_TYPE_PPS)
721 scnprintf(msg, sizeof(msg),
723 pdo_pps_apdo_min_voltage(pdo),
724 pdo_pps_apdo_max_voltage(pdo),
725 pdo_pps_apdo_max_current(pdo));
727 strcpy(msg, "undefined APDO");
730 strcpy(msg, "undefined");
733 tcpm_log(port, " PDO %d: type %d, %s",
738 static int tcpm_debug_show(struct seq_file *s, void *v)
740 struct tcpm_port *port = s->private;
743 mutex_lock(&port->logbuffer_lock);
744 tail = port->logbuffer_tail;
745 while (tail != port->logbuffer_head) {
746 seq_printf(s, "%s\n", port->logbuffer[tail]);
747 tail = (tail + 1) % LOG_BUFFER_ENTRIES;
749 if (!seq_has_overflowed(s))
750 port->logbuffer_tail = tail;
751 mutex_unlock(&port->logbuffer_lock);
755 DEFINE_SHOW_ATTRIBUTE(tcpm_debug);
757 static void tcpm_debugfs_init(struct tcpm_port *port)
761 mutex_init(&port->logbuffer_lock);
762 snprintf(name, NAME_MAX, "tcpm-%s", dev_name(port->dev));
763 port->dentry = debugfs_create_dir(name, usb_debug_root);
764 debugfs_create_file("log", S_IFREG | 0444, port->dentry, port,
768 static void tcpm_debugfs_exit(struct tcpm_port *port)
772 mutex_lock(&port->logbuffer_lock);
773 for (i = 0; i < LOG_BUFFER_ENTRIES; i++) {
774 kfree(port->logbuffer[i]);
775 port->logbuffer[i] = NULL;
777 mutex_unlock(&port->logbuffer_lock);
779 debugfs_remove(port->dentry);
785 static void tcpm_log(const struct tcpm_port *port, const char *fmt, ...) { }
787 static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...) { }
788 static void tcpm_log_source_caps(struct tcpm_port *port) { }
789 static void tcpm_debugfs_init(const struct tcpm_port *port) { }
790 static void tcpm_debugfs_exit(const struct tcpm_port *port) { }
794 static void tcpm_set_cc(struct tcpm_port *port, enum typec_cc_status cc)
796 tcpm_log(port, "cc:=%d", cc);
798 port->tcpc->set_cc(port->tcpc, cc);
801 static int tcpm_enable_auto_vbus_discharge(struct tcpm_port *port, bool enable)
805 if (port->tcpc->enable_auto_vbus_discharge) {
806 ret = port->tcpc->enable_auto_vbus_discharge(port->tcpc, enable);
807 tcpm_log_force(port, "%s vbus discharge ret:%d", enable ? "enable" : "disable",
810 port->auto_vbus_discharge_enabled = enable;
816 static void tcpm_apply_rc(struct tcpm_port *port)
819 * TCPCI: Move to APPLY_RC state to prevent disconnect during PR_SWAP
820 * when Vbus auto discharge on disconnect is enabled.
822 if (port->tcpc->enable_auto_vbus_discharge && port->tcpc->apply_rc) {
823 tcpm_log(port, "Apply_RC");
824 port->tcpc->apply_rc(port->tcpc, port->cc_req, port->polarity);
825 tcpm_enable_auto_vbus_discharge(port, false);
830 * Determine RP value to set based on maximum current supported
831 * by a port if configured as source.
832 * Returns CC value to report to link partner.
834 static enum typec_cc_status tcpm_rp_cc(struct tcpm_port *port)
836 const u32 *src_pdo = port->src_pdo;
837 int nr_pdo = port->nr_src_pdo;
840 if (!port->pd_supported)
844 * Search for first entry with matching voltage.
845 * It should report the maximum supported current.
847 for (i = 0; i < nr_pdo; i++) {
848 const u32 pdo = src_pdo[i];
850 if (pdo_type(pdo) == PDO_TYPE_FIXED &&
851 pdo_fixed_voltage(pdo) == 5000) {
852 unsigned int curr = pdo_max_current(pdo);
855 return TYPEC_CC_RP_3_0;
856 else if (curr >= 1500)
857 return TYPEC_CC_RP_1_5;
858 return TYPEC_CC_RP_DEF;
862 return TYPEC_CC_RP_DEF;
865 static void tcpm_ams_finish(struct tcpm_port *port)
867 tcpm_log(port, "AMS %s finished", tcpm_ams_str[port->ams]);
869 if (port->pd_capable && port->pwr_role == TYPEC_SOURCE) {
870 if (port->negotiated_rev >= PD_REV30)
871 tcpm_set_cc(port, SINK_TX_OK);
873 tcpm_set_cc(port, SINK_TX_NG);
874 } else if (port->pwr_role == TYPEC_SOURCE) {
875 tcpm_set_cc(port, tcpm_rp_cc(port));
878 port->in_ams = false;
879 port->ams = NONE_AMS;
882 static int tcpm_pd_transmit(struct tcpm_port *port,
883 enum tcpm_transmit_type type,
884 const struct pd_message *msg)
886 unsigned long timeout;
890 tcpm_log(port, "PD TX, header: %#x", le16_to_cpu(msg->header));
892 tcpm_log(port, "PD TX, type: %#x", type);
894 reinit_completion(&port->tx_complete);
895 ret = port->tcpc->pd_transmit(port->tcpc, type, msg, port->negotiated_rev);
899 mutex_unlock(&port->lock);
900 timeout = wait_for_completion_timeout(&port->tx_complete,
901 msecs_to_jiffies(PD_T_TCPC_TX_TIMEOUT));
902 mutex_lock(&port->lock);
906 switch (port->tx_status) {
907 case TCPC_TX_SUCCESS:
908 port->message_id = (port->message_id + 1) & PD_HEADER_ID_MASK;
910 * USB PD rev 2.0, 8.3.2.2.1:
911 * USB PD rev 3.0, 8.3.2.1.3:
912 * "... Note that every AMS is Interruptible until the first
913 * Message in the sequence has been successfully sent (GoodCRC
914 * Message received)."
916 if (port->ams != NONE_AMS)
919 case TCPC_TX_DISCARDED:
928 /* Some AMS don't expect responses. Finish them here. */
929 if (port->ams == ATTENTION || port->ams == SOURCE_ALERT)
930 tcpm_ams_finish(port);
935 void tcpm_pd_transmit_complete(struct tcpm_port *port,
936 enum tcpm_transmit_status status)
938 tcpm_log(port, "PD TX complete, status: %u", status);
939 port->tx_status = status;
940 complete(&port->tx_complete);
942 EXPORT_SYMBOL_GPL(tcpm_pd_transmit_complete);
944 static int tcpm_mux_set(struct tcpm_port *port, int state,
945 enum usb_role usb_role,
946 enum typec_orientation orientation)
950 tcpm_log(port, "Requesting mux state %d, usb-role %d, orientation %d",
951 state, usb_role, orientation);
953 ret = typec_set_orientation(port->typec_port, orientation);
958 ret = usb_role_switch_set_role(port->role_sw, usb_role);
963 return typec_set_mode(port->typec_port, state);
966 static int tcpm_set_polarity(struct tcpm_port *port,
967 enum typec_cc_polarity polarity)
971 tcpm_log(port, "polarity %d", polarity);
973 ret = port->tcpc->set_polarity(port->tcpc, polarity);
977 port->polarity = polarity;
982 static int tcpm_set_vconn(struct tcpm_port *port, bool enable)
986 tcpm_log(port, "vconn:=%d", enable);
988 ret = port->tcpc->set_vconn(port->tcpc, enable);
990 port->vconn_role = enable ? TYPEC_SOURCE : TYPEC_SINK;
991 typec_set_vconn_role(port->typec_port, port->vconn_role);
997 static u32 tcpm_get_current_limit(struct tcpm_port *port)
999 enum typec_cc_status cc;
1002 cc = port->polarity ? port->cc2 : port->cc1;
1004 case TYPEC_CC_RP_1_5:
1007 case TYPEC_CC_RP_3_0:
1010 case TYPEC_CC_RP_DEF:
1012 if (port->tcpc->get_current_limit)
1013 limit = port->tcpc->get_current_limit(port->tcpc);
1022 static int tcpm_set_current_limit(struct tcpm_port *port, u32 max_ma, u32 mv)
1024 int ret = -EOPNOTSUPP;
1026 tcpm_log(port, "Setting voltage/current limit %u mV %u mA", mv, max_ma);
1028 port->supply_voltage = mv;
1029 port->current_limit = max_ma;
1030 power_supply_changed(port->psy);
1032 if (port->tcpc->set_current_limit)
1033 ret = port->tcpc->set_current_limit(port->tcpc, max_ma, mv);
1038 static int tcpm_set_attached_state(struct tcpm_port *port, bool attached)
1040 return port->tcpc->set_roles(port->tcpc, attached, port->pwr_role,
1044 static int tcpm_set_roles(struct tcpm_port *port, bool attached,
1045 enum typec_role role, enum typec_data_role data)
1047 enum typec_orientation orientation;
1048 enum usb_role usb_role;
1051 if (port->polarity == TYPEC_POLARITY_CC1)
1052 orientation = TYPEC_ORIENTATION_NORMAL;
1054 orientation = TYPEC_ORIENTATION_REVERSE;
1056 if (port->typec_caps.data == TYPEC_PORT_DRD) {
1057 if (data == TYPEC_HOST)
1058 usb_role = USB_ROLE_HOST;
1060 usb_role = USB_ROLE_DEVICE;
1061 } else if (port->typec_caps.data == TYPEC_PORT_DFP) {
1062 if (data == TYPEC_HOST) {
1063 if (role == TYPEC_SOURCE)
1064 usb_role = USB_ROLE_HOST;
1066 usb_role = USB_ROLE_NONE;
1071 if (data == TYPEC_DEVICE) {
1072 if (role == TYPEC_SINK)
1073 usb_role = USB_ROLE_DEVICE;
1075 usb_role = USB_ROLE_NONE;
1081 ret = tcpm_mux_set(port, TYPEC_STATE_USB, usb_role, orientation);
1085 ret = port->tcpc->set_roles(port->tcpc, attached, role, data);
1089 port->pwr_role = role;
1090 port->data_role = data;
1091 typec_set_data_role(port->typec_port, data);
1092 typec_set_pwr_role(port->typec_port, role);
1097 static int tcpm_set_pwr_role(struct tcpm_port *port, enum typec_role role)
1101 ret = port->tcpc->set_roles(port->tcpc, true, role,
1106 port->pwr_role = role;
1107 typec_set_pwr_role(port->typec_port, role);
1113 * Transform the PDO to be compliant to PD rev2.0.
1114 * Return 0 if the PDO type is not defined in PD rev2.0.
1115 * Otherwise, return the converted PDO.
1117 static u32 tcpm_forge_legacy_pdo(struct tcpm_port *port, u32 pdo, enum typec_role role)
1119 switch (pdo_type(pdo)) {
1120 case PDO_TYPE_FIXED:
1121 if (role == TYPEC_SINK)
1122 return pdo & ~PDO_FIXED_FRS_CURR_MASK;
1124 return pdo & ~PDO_FIXED_UNCHUNK_EXT;
1134 static int tcpm_pd_send_source_caps(struct tcpm_port *port)
1136 struct pd_message msg;
1138 unsigned int i, nr_pdo = 0;
1140 memset(&msg, 0, sizeof(msg));
1142 for (i = 0; i < port->nr_src_pdo; i++) {
1143 if (port->negotiated_rev >= PD_REV30) {
1144 msg.payload[nr_pdo++] = cpu_to_le32(port->src_pdo[i]);
1146 pdo = tcpm_forge_legacy_pdo(port, port->src_pdo[i], TYPEC_SOURCE);
1148 msg.payload[nr_pdo++] = cpu_to_le32(pdo);
1153 /* No source capabilities defined, sink only */
1154 msg.header = PD_HEADER_LE(PD_CTRL_REJECT,
1157 port->negotiated_rev,
1158 port->message_id, 0);
1160 msg.header = PD_HEADER_LE(PD_DATA_SOURCE_CAP,
1163 port->negotiated_rev,
1168 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
1171 static int tcpm_pd_send_sink_caps(struct tcpm_port *port)
1173 struct pd_message msg;
1175 unsigned int i, nr_pdo = 0;
1177 memset(&msg, 0, sizeof(msg));
1179 for (i = 0; i < port->nr_snk_pdo; i++) {
1180 if (port->negotiated_rev >= PD_REV30) {
1181 msg.payload[nr_pdo++] = cpu_to_le32(port->snk_pdo[i]);
1183 pdo = tcpm_forge_legacy_pdo(port, port->snk_pdo[i], TYPEC_SINK);
1185 msg.payload[nr_pdo++] = cpu_to_le32(pdo);
1190 /* No sink capabilities defined, source only */
1191 msg.header = PD_HEADER_LE(PD_CTRL_REJECT,
1194 port->negotiated_rev,
1195 port->message_id, 0);
1197 msg.header = PD_HEADER_LE(PD_DATA_SINK_CAP,
1200 port->negotiated_rev,
1205 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
1208 static void mod_tcpm_delayed_work(struct tcpm_port *port, unsigned int delay_ms)
1211 hrtimer_start(&port->state_machine_timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL);
1213 hrtimer_cancel(&port->state_machine_timer);
1214 kthread_queue_work(port->wq, &port->state_machine);
1218 static void mod_vdm_delayed_work(struct tcpm_port *port, unsigned int delay_ms)
1221 hrtimer_start(&port->vdm_state_machine_timer, ms_to_ktime(delay_ms),
1224 hrtimer_cancel(&port->vdm_state_machine_timer);
1225 kthread_queue_work(port->wq, &port->vdm_state_machine);
1229 static void mod_enable_frs_delayed_work(struct tcpm_port *port, unsigned int delay_ms)
1232 hrtimer_start(&port->enable_frs_timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL);
1234 hrtimer_cancel(&port->enable_frs_timer);
1235 kthread_queue_work(port->wq, &port->enable_frs);
1239 static void mod_send_discover_delayed_work(struct tcpm_port *port, unsigned int delay_ms)
1242 hrtimer_start(&port->send_discover_timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL);
1244 hrtimer_cancel(&port->send_discover_timer);
1245 kthread_queue_work(port->wq, &port->send_discover_work);
1249 static void tcpm_set_state(struct tcpm_port *port, enum tcpm_state state,
1250 unsigned int delay_ms)
1253 tcpm_log(port, "pending state change %s -> %s @ %u ms [%s %s]",
1254 tcpm_states[port->state], tcpm_states[state], delay_ms,
1255 pd_rev[port->negotiated_rev], tcpm_ams_str[port->ams]);
1256 port->delayed_state = state;
1257 mod_tcpm_delayed_work(port, delay_ms);
1258 port->delayed_runtime = ktime_add(ktime_get(), ms_to_ktime(delay_ms));
1259 port->delay_ms = delay_ms;
1261 tcpm_log(port, "state change %s -> %s [%s %s]",
1262 tcpm_states[port->state], tcpm_states[state],
1263 pd_rev[port->negotiated_rev], tcpm_ams_str[port->ams]);
1264 port->delayed_state = INVALID_STATE;
1265 port->prev_state = port->state;
1266 port->state = state;
1268 * Don't re-queue the state machine work item if we're currently
1269 * in the state machine and we're immediately changing states.
1270 * tcpm_state_machine_work() will continue running the state
1273 if (!port->state_machine_running)
1274 mod_tcpm_delayed_work(port, 0);
1278 static void tcpm_set_state_cond(struct tcpm_port *port, enum tcpm_state state,
1279 unsigned int delay_ms)
1281 if (port->enter_state == port->state)
1282 tcpm_set_state(port, state, delay_ms);
1285 "skipped %sstate change %s -> %s [%u ms], context state %s [%s %s]",
1286 delay_ms ? "delayed " : "",
1287 tcpm_states[port->state], tcpm_states[state],
1288 delay_ms, tcpm_states[port->enter_state],
1289 pd_rev[port->negotiated_rev], tcpm_ams_str[port->ams]);
1292 static void tcpm_queue_message(struct tcpm_port *port,
1293 enum pd_msg_request message)
1295 port->queued_message = message;
1296 mod_tcpm_delayed_work(port, 0);
1299 static bool tcpm_vdm_ams(struct tcpm_port *port)
1301 switch (port->ams) {
1302 case DISCOVER_IDENTITY:
1303 case SOURCE_STARTUP_CABLE_PLUG_DISCOVER_IDENTITY:
1304 case DISCOVER_SVIDS:
1305 case DISCOVER_MODES:
1306 case DFP_TO_UFP_ENTER_MODE:
1307 case DFP_TO_UFP_EXIT_MODE:
1308 case DFP_TO_CABLE_PLUG_ENTER_MODE:
1309 case DFP_TO_CABLE_PLUG_EXIT_MODE:
1311 case UNSTRUCTURED_VDMS:
1312 case STRUCTURED_VDMS:
1321 static bool tcpm_ams_interruptible(struct tcpm_port *port)
1323 switch (port->ams) {
1324 /* Interruptible AMS */
1327 case FIRMWARE_UPDATE:
1328 case DISCOVER_IDENTITY:
1329 case SOURCE_STARTUP_CABLE_PLUG_DISCOVER_IDENTITY:
1330 case DISCOVER_SVIDS:
1331 case DISCOVER_MODES:
1332 case DFP_TO_UFP_ENTER_MODE:
1333 case DFP_TO_UFP_EXIT_MODE:
1334 case DFP_TO_CABLE_PLUG_ENTER_MODE:
1335 case DFP_TO_CABLE_PLUG_EXIT_MODE:
1336 case UNSTRUCTURED_VDMS:
1337 case STRUCTURED_VDMS:
1341 /* Non-Interruptible AMS */
1351 static int tcpm_ams_start(struct tcpm_port *port, enum tcpm_ams ams)
1355 tcpm_log(port, "AMS %s start", tcpm_ams_str[ams]);
1357 if (!tcpm_ams_interruptible(port) &&
1358 !(ams == HARD_RESET || ams == SOFT_RESET_AMS)) {
1359 port->upcoming_state = INVALID_STATE;
1360 tcpm_log(port, "AMS %s not interruptible, aborting",
1361 tcpm_ams_str[port->ams]);
1365 if (port->pwr_role == TYPEC_SOURCE) {
1366 enum typec_cc_status cc_req = port->cc_req;
1370 if (ams == HARD_RESET) {
1371 tcpm_set_cc(port, tcpm_rp_cc(port));
1372 tcpm_pd_transmit(port, TCPC_TX_HARD_RESET, NULL);
1373 tcpm_set_state(port, HARD_RESET_START, 0);
1375 } else if (ams == SOFT_RESET_AMS) {
1376 if (!port->explicit_contract)
1377 tcpm_set_cc(port, tcpm_rp_cc(port));
1378 tcpm_set_state(port, SOFT_RESET_SEND, 0);
1380 } else if (tcpm_vdm_ams(port)) {
1381 /* tSinkTx is enforced in vdm_run_state_machine */
1382 if (port->negotiated_rev >= PD_REV30)
1383 tcpm_set_cc(port, SINK_TX_NG);
1387 if (port->negotiated_rev >= PD_REV30)
1388 tcpm_set_cc(port, SINK_TX_NG);
1390 switch (port->state) {
1393 case SRC_SOFT_RESET_WAIT_SNK_TX:
1395 case SOFT_RESET_SEND:
1396 if (port->negotiated_rev >= PD_REV30)
1397 tcpm_set_state(port, AMS_START,
1398 cc_req == SINK_TX_OK ?
1401 tcpm_set_state(port, AMS_START, 0);
1404 if (port->negotiated_rev >= PD_REV30)
1405 tcpm_set_state(port, SRC_READY,
1406 cc_req == SINK_TX_OK ?
1409 tcpm_set_state(port, SRC_READY, 0);
1413 if (port->negotiated_rev >= PD_REV30 &&
1414 !tcpm_sink_tx_ok(port) &&
1415 ams != SOFT_RESET_AMS &&
1416 ams != HARD_RESET) {
1417 port->upcoming_state = INVALID_STATE;
1418 tcpm_log(port, "Sink TX No Go");
1424 if (ams == HARD_RESET) {
1425 tcpm_pd_transmit(port, TCPC_TX_HARD_RESET, NULL);
1426 tcpm_set_state(port, HARD_RESET_START, 0);
1428 } else if (tcpm_vdm_ams(port)) {
1432 if (port->state == SNK_READY ||
1433 port->state == SNK_SOFT_RESET)
1434 tcpm_set_state(port, AMS_START, 0);
1436 tcpm_set_state(port, SNK_READY, 0);
1443 * VDM/VDO handling functions
1445 static void tcpm_queue_vdm(struct tcpm_port *port, const u32 header,
1446 const u32 *data, int cnt)
1448 u32 vdo_hdr = port->vdo_data[0];
1450 WARN_ON(!mutex_is_locked(&port->lock));
1452 /* If is sending discover_identity, handle received message first */
1453 if (PD_VDO_SVDM(vdo_hdr) && PD_VDO_CMD(vdo_hdr) == CMD_DISCOVER_IDENT) {
1454 port->send_discover = true;
1455 mod_send_discover_delayed_work(port, SEND_DISCOVER_RETRY_MS);
1457 /* Make sure we are not still processing a previous VDM packet */
1458 WARN_ON(port->vdm_state > VDM_STATE_DONE);
1461 port->vdo_count = cnt + 1;
1462 port->vdo_data[0] = header;
1463 memcpy(&port->vdo_data[1], data, sizeof(u32) * cnt);
1464 /* Set ready, vdm state machine will actually send */
1465 port->vdm_retries = 0;
1466 port->vdm_state = VDM_STATE_READY;
1467 port->vdm_sm_running = true;
1469 mod_vdm_delayed_work(port, 0);
1472 static void tcpm_queue_vdm_unlocked(struct tcpm_port *port, const u32 header,
1473 const u32 *data, int cnt)
1475 mutex_lock(&port->lock);
1476 tcpm_queue_vdm(port, header, data, cnt);
1477 mutex_unlock(&port->lock);
1480 static void svdm_consume_identity(struct tcpm_port *port, const u32 *p, int cnt)
1482 u32 vdo = p[VDO_INDEX_IDH];
1483 u32 product = p[VDO_INDEX_PRODUCT];
1485 memset(&port->mode_data, 0, sizeof(port->mode_data));
1487 port->partner_ident.id_header = vdo;
1488 port->partner_ident.cert_stat = p[VDO_INDEX_CSTAT];
1489 port->partner_ident.product = product;
1491 typec_partner_set_identity(port->partner);
1493 tcpm_log(port, "Identity: %04x:%04x.%04x",
1495 PD_PRODUCT_PID(product), product & 0xffff);
1498 static bool svdm_consume_svids(struct tcpm_port *port, const u32 *p, int cnt)
1500 struct pd_mode_data *pmdata = &port->mode_data;
1503 for (i = 1; i < cnt; i++) {
1506 svid = (p[i] >> 16) & 0xffff;
1510 if (pmdata->nsvids >= SVID_DISCOVERY_MAX)
1513 pmdata->svids[pmdata->nsvids++] = svid;
1514 tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid);
1516 svid = p[i] & 0xffff;
1520 if (pmdata->nsvids >= SVID_DISCOVERY_MAX)
1523 pmdata->svids[pmdata->nsvids++] = svid;
1524 tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid);
1528 * PD3.0 Spec 6.4.4.3.2: The SVIDs are returned 2 per VDO (see Table
1529 * 6-43), and can be returned maximum 6 VDOs per response (see Figure
1530 * 6-19). If the Respondersupports 12 or more SVID then the Discover
1531 * SVIDs Command Shall be executed multiple times until a Discover
1532 * SVIDs VDO is returned ending either with a SVID value of 0x0000 in
1533 * the last part of the last VDO or with a VDO containing two SVIDs
1534 * with values of 0x0000.
1536 * However, some odd dockers support SVIDs less than 12 but without
1537 * 0x0000 in the last VDO, so we need to break the Discover SVIDs
1538 * request and return false here.
1542 tcpm_log(port, "SVID_DISCOVERY_MAX(%d) too low!", SVID_DISCOVERY_MAX);
1546 static void svdm_consume_modes(struct tcpm_port *port, const u32 *p, int cnt)
1548 struct pd_mode_data *pmdata = &port->mode_data;
1549 struct typec_altmode_desc *paltmode;
1552 if (pmdata->altmodes >= ARRAY_SIZE(port->partner_altmode)) {
1553 /* Already logged in svdm_consume_svids() */
1557 for (i = 1; i < cnt; i++) {
1558 paltmode = &pmdata->altmode_desc[pmdata->altmodes];
1559 memset(paltmode, 0, sizeof(*paltmode));
1561 paltmode->svid = pmdata->svids[pmdata->svid_index];
1563 paltmode->vdo = p[i];
1565 tcpm_log(port, " Alternate mode %d: SVID 0x%04x, VDO %d: 0x%08x",
1566 pmdata->altmodes, paltmode->svid,
1567 paltmode->mode, paltmode->vdo);
1573 static void tcpm_register_partner_altmodes(struct tcpm_port *port)
1575 struct pd_mode_data *modep = &port->mode_data;
1576 struct typec_altmode *altmode;
1579 for (i = 0; i < modep->altmodes; i++) {
1580 altmode = typec_partner_register_altmode(port->partner,
1581 &modep->altmode_desc[i]);
1582 if (IS_ERR(altmode)) {
1583 tcpm_log(port, "Failed to register partner SVID 0x%04x",
1584 modep->altmode_desc[i].svid);
1587 port->partner_altmode[i] = altmode;
1591 #define supports_modal(port) PD_IDH_MODAL_SUPP((port)->partner_ident.id_header)
1593 static int tcpm_pd_svdm(struct tcpm_port *port, struct typec_altmode *adev,
1594 const u32 *p, int cnt, u32 *response,
1595 enum adev_actions *adev_action)
1597 struct typec_port *typec = port->typec_port;
1598 struct typec_altmode *pdev;
1599 struct pd_mode_data *modep;
1606 cmd_type = PD_VDO_CMDT(p[0]);
1607 cmd = PD_VDO_CMD(p[0]);
1609 tcpm_log(port, "Rx VDM cmd 0x%x type %d cmd %d len %d",
1610 p[0], cmd_type, cmd, cnt);
1612 modep = &port->mode_data;
1614 pdev = typec_match_altmode(port->partner_altmode, ALTMODE_DISCOVERY_MAX,
1615 PD_VDO_VID(p[0]), PD_VDO_OPOS(p[0]));
1617 svdm_version = typec_get_negotiated_svdm_version(typec);
1618 if (svdm_version < 0)
1624 case CMD_DISCOVER_IDENT:
1625 if (PD_VDO_VID(p[0]) != USB_SID_PD)
1628 if (PD_VDO_SVDM_VER(p[0]) < svdm_version) {
1629 typec_partner_set_svdm_version(port->partner,
1630 PD_VDO_SVDM_VER(p[0]));
1631 svdm_version = PD_VDO_SVDM_VER(p[0]);
1634 port->ams = DISCOVER_IDENTITY;
1636 * PD2.0 Spec 6.10.3: respond with NAK as DFP (data host)
1637 * PD3.1 Spec 6.4.4.2.5.1: respond with NAK if "invalid field" or
1638 * "wrong configuation" or "Unrecognized"
1640 if ((port->data_role == TYPEC_DEVICE || svdm_version >= SVDM_VER_2_0) &&
1642 if (svdm_version < SVDM_VER_2_0) {
1643 for (i = 0; i < port->nr_snk_vdo_v1; i++)
1644 response[i + 1] = port->snk_vdo_v1[i];
1645 rlen = port->nr_snk_vdo_v1 + 1;
1648 for (i = 0; i < port->nr_snk_vdo; i++)
1649 response[i + 1] = port->snk_vdo[i];
1650 rlen = port->nr_snk_vdo + 1;
1654 case CMD_DISCOVER_SVID:
1655 port->ams = DISCOVER_SVIDS;
1657 case CMD_DISCOVER_MODES:
1658 port->ams = DISCOVER_MODES;
1660 case CMD_ENTER_MODE:
1661 port->ams = DFP_TO_UFP_ENTER_MODE;
1664 port->ams = DFP_TO_UFP_EXIT_MODE;
1667 /* Attention command does not have response */
1668 *adev_action = ADEV_ATTENTION;
1674 response[0] = p[0] | VDO_CMDT(CMDT_RSP_ACK);
1675 } else if (rlen == 0) {
1676 response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK);
1679 response[0] = p[0] | VDO_CMDT(CMDT_RSP_BUSY);
1682 response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) |
1683 (VDO_SVDM_VERS(typec_get_negotiated_svdm_version(typec)));
1686 /* silently drop message if we are not connected */
1687 if (IS_ERR_OR_NULL(port->partner))
1690 tcpm_ams_finish(port);
1693 case CMD_DISCOVER_IDENT:
1694 if (PD_VDO_SVDM_VER(p[0]) < svdm_version)
1695 typec_partner_set_svdm_version(port->partner,
1696 PD_VDO_SVDM_VER(p[0]));
1698 svdm_consume_identity(port, p, cnt);
1699 response[0] = VDO(USB_SID_PD, 1, typec_get_negotiated_svdm_version(typec),
1703 case CMD_DISCOVER_SVID:
1705 if (svdm_consume_svids(port, p, cnt)) {
1706 response[0] = VDO(USB_SID_PD, 1, svdm_version, CMD_DISCOVER_SVID);
1708 } else if (modep->nsvids && supports_modal(port)) {
1709 response[0] = VDO(modep->svids[0], 1, svdm_version,
1710 CMD_DISCOVER_MODES);
1714 case CMD_DISCOVER_MODES:
1716 svdm_consume_modes(port, p, cnt);
1717 modep->svid_index++;
1718 if (modep->svid_index < modep->nsvids) {
1719 u16 svid = modep->svids[modep->svid_index];
1720 response[0] = VDO(svid, 1, svdm_version, CMD_DISCOVER_MODES);
1723 tcpm_register_partner_altmodes(port);
1726 case CMD_ENTER_MODE:
1728 *adev_action = ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL;
1732 /* Back to USB Operation */
1733 *adev_action = ADEV_NOTIFY_USB_AND_QUEUE_VDM;
1737 case VDO_CMD_VENDOR(0) ... VDO_CMD_VENDOR(15):
1740 /* Unrecognized SVDM */
1741 response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK);
1743 response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) |
1744 (VDO_SVDM_VERS(svdm_version));
1749 tcpm_ams_finish(port);
1751 case CMD_DISCOVER_IDENT:
1752 case CMD_DISCOVER_SVID:
1753 case CMD_DISCOVER_MODES:
1754 case VDO_CMD_VENDOR(0) ... VDO_CMD_VENDOR(15):
1756 case CMD_ENTER_MODE:
1757 /* Back to USB Operation */
1758 *adev_action = ADEV_NOTIFY_USB_AND_QUEUE_VDM;
1761 /* Unrecognized SVDM */
1762 response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK);
1764 response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) |
1765 (VDO_SVDM_VERS(svdm_version));
1770 response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK);
1772 response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) |
1773 (VDO_SVDM_VERS(svdm_version));
1777 /* Informing the alternate mode drivers about everything */
1778 *adev_action = ADEV_QUEUE_VDM;
1782 static void tcpm_pd_handle_msg(struct tcpm_port *port,
1783 enum pd_msg_request message,
1786 static void tcpm_handle_vdm_request(struct tcpm_port *port,
1787 const __le32 *payload, int cnt)
1789 enum adev_actions adev_action = ADEV_NONE;
1790 struct typec_altmode *adev;
1791 u32 p[PD_MAX_PAYLOAD];
1792 u32 response[8] = { };
1795 for (i = 0; i < cnt; i++)
1796 p[i] = le32_to_cpu(payload[i]);
1798 adev = typec_match_altmode(port->port_altmode, ALTMODE_DISCOVERY_MAX,
1799 PD_VDO_VID(p[0]), PD_VDO_OPOS(p[0]));
1801 if (port->vdm_state == VDM_STATE_BUSY) {
1802 /* If UFP responded busy retry after timeout */
1803 if (PD_VDO_CMDT(p[0]) == CMDT_RSP_BUSY) {
1804 port->vdm_state = VDM_STATE_WAIT_RSP_BUSY;
1805 port->vdo_retry = (p[0] & ~VDO_CMDT_MASK) |
1807 mod_vdm_delayed_work(port, PD_T_VDM_BUSY);
1810 port->vdm_state = VDM_STATE_DONE;
1813 if (PD_VDO_SVDM(p[0]) && (adev || tcpm_vdm_ams(port) || port->nr_snk_vdo)) {
1815 * Here a SVDM is received (INIT or RSP or unknown). Set the vdm_sm_running in
1816 * advance because we are dropping the lock but may send VDMs soon.
1817 * For the cases of INIT received:
1818 * - If no response to send, it will be cleared later in this function.
1819 * - If there are responses to send, it will be cleared in the state machine.
1820 * For the cases of RSP received:
1821 * - If no further INIT to send, it will be cleared later in this function.
1822 * - Otherwise, it will be cleared in the state machine if timeout or it will go
1823 * back here until no further INIT to send.
1824 * For the cases of unknown type received:
1825 * - We will send NAK and the flag will be cleared in the state machine.
1827 port->vdm_sm_running = true;
1828 rlen = tcpm_pd_svdm(port, adev, p, cnt, response, &adev_action);
1830 if (port->negotiated_rev >= PD_REV30)
1831 tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS);
1835 * We are done with any state stored in the port struct now, except
1836 * for any port struct changes done by the tcpm_queue_vdm() call
1837 * below, which is a separate operation.
1839 * So we can safely release the lock here; and we MUST release the
1840 * lock here to avoid an AB BA lock inversion:
1842 * If we keep the lock here then the lock ordering in this path is:
1843 * 1. tcpm_pd_rx_handler take the tcpm port lock
1844 * 2. One of the typec_altmode_* calls below takes the alt-mode's lock
1846 * And we also have this ordering:
1847 * 1. alt-mode driver takes the alt-mode's lock
1848 * 2. alt-mode driver calls tcpm_altmode_enter which takes the
1851 * Dropping our lock here avoids this.
1853 mutex_unlock(&port->lock);
1856 switch (adev_action) {
1859 case ADEV_NOTIFY_USB_AND_QUEUE_VDM:
1860 WARN_ON(typec_altmode_notify(adev, TYPEC_STATE_USB, NULL));
1861 typec_altmode_vdm(adev, p[0], &p[1], cnt);
1863 case ADEV_QUEUE_VDM:
1864 typec_altmode_vdm(adev, p[0], &p[1], cnt);
1866 case ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL:
1867 if (typec_altmode_vdm(adev, p[0], &p[1], cnt)) {
1868 int svdm_version = typec_get_negotiated_svdm_version(
1870 if (svdm_version < 0)
1873 response[0] = VDO(adev->svid, 1, svdm_version,
1875 response[0] |= VDO_OPOS(adev->mode);
1879 case ADEV_ATTENTION:
1880 if (typec_altmode_attention(adev, p[1]))
1881 tcpm_log(port, "typec_altmode_attention no port partner altmode");
1887 * We must re-take the lock here to balance the unlock in
1888 * tcpm_pd_rx_handler, note that no changes, other then the
1889 * tcpm_queue_vdm call, are made while the lock is held again.
1890 * All that is done after the call is unwinding the call stack until
1891 * we return to tcpm_pd_rx_handler and do the unlock there.
1893 mutex_lock(&port->lock);
1896 tcpm_queue_vdm(port, response[0], &response[1], rlen - 1);
1898 port->vdm_sm_running = false;
1901 static void tcpm_send_vdm(struct tcpm_port *port, u32 vid, int cmd,
1902 const u32 *data, int count)
1904 int svdm_version = typec_get_negotiated_svdm_version(port->typec_port);
1907 if (svdm_version < 0)
1910 if (WARN_ON(count > VDO_MAX_SIZE - 1))
1911 count = VDO_MAX_SIZE - 1;
1913 /* set VDM header with VID & CMD */
1914 header = VDO(vid, ((vid & USB_SID_PD) == USB_SID_PD) ?
1915 1 : (PD_VDO_CMD(cmd) <= CMD_ATTENTION),
1917 tcpm_queue_vdm(port, header, data, count);
1920 static unsigned int vdm_ready_timeout(u32 vdm_hdr)
1922 unsigned int timeout;
1923 int cmd = PD_VDO_CMD(vdm_hdr);
1925 /* its not a structured VDM command */
1926 if (!PD_VDO_SVDM(vdm_hdr))
1927 return PD_T_VDM_UNSTRUCTURED;
1929 switch (PD_VDO_CMDT(vdm_hdr)) {
1931 if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE)
1932 timeout = PD_T_VDM_WAIT_MODE_E;
1934 timeout = PD_T_VDM_SNDR_RSP;
1937 if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE)
1938 timeout = PD_T_VDM_E_MODE;
1940 timeout = PD_T_VDM_RCVR_RSP;
1946 static void vdm_run_state_machine(struct tcpm_port *port)
1948 struct pd_message msg;
1950 u32 vdo_hdr = port->vdo_data[0];
1952 switch (port->vdm_state) {
1953 case VDM_STATE_READY:
1954 /* Only transmit VDM if attached */
1955 if (!port->attached) {
1956 port->vdm_state = VDM_STATE_ERR_BUSY;
1961 * if there's traffic or we're not in PDO ready state don't send
1964 if (port->state != SRC_READY && port->state != SNK_READY) {
1965 port->vdm_sm_running = false;
1969 /* TODO: AMS operation for Unstructured VDM */
1970 if (PD_VDO_SVDM(vdo_hdr) && PD_VDO_CMDT(vdo_hdr) == CMDT_INIT) {
1971 switch (PD_VDO_CMD(vdo_hdr)) {
1972 case CMD_DISCOVER_IDENT:
1973 res = tcpm_ams_start(port, DISCOVER_IDENTITY);
1975 port->send_discover = false;
1976 } else if (res == -EAGAIN) {
1977 port->vdo_data[0] = 0;
1978 mod_send_discover_delayed_work(port,
1979 SEND_DISCOVER_RETRY_MS);
1982 case CMD_DISCOVER_SVID:
1983 res = tcpm_ams_start(port, DISCOVER_SVIDS);
1985 case CMD_DISCOVER_MODES:
1986 res = tcpm_ams_start(port, DISCOVER_MODES);
1988 case CMD_ENTER_MODE:
1989 res = tcpm_ams_start(port, DFP_TO_UFP_ENTER_MODE);
1992 res = tcpm_ams_start(port, DFP_TO_UFP_EXIT_MODE);
1995 res = tcpm_ams_start(port, ATTENTION);
1997 case VDO_CMD_VENDOR(0) ... VDO_CMD_VENDOR(15):
1998 res = tcpm_ams_start(port, STRUCTURED_VDMS);
2006 port->vdm_state = VDM_STATE_ERR_BUSY;
2011 port->vdm_state = VDM_STATE_SEND_MESSAGE;
2012 mod_vdm_delayed_work(port, (port->negotiated_rev >= PD_REV30 &&
2013 port->pwr_role == TYPEC_SOURCE &&
2014 PD_VDO_SVDM(vdo_hdr) &&
2015 PD_VDO_CMDT(vdo_hdr) == CMDT_INIT) ?
2018 case VDM_STATE_WAIT_RSP_BUSY:
2019 port->vdo_data[0] = port->vdo_retry;
2020 port->vdo_count = 1;
2021 port->vdm_state = VDM_STATE_READY;
2022 tcpm_ams_finish(port);
2024 case VDM_STATE_BUSY:
2025 port->vdm_state = VDM_STATE_ERR_TMOUT;
2026 if (port->ams != NONE_AMS)
2027 tcpm_ams_finish(port);
2029 case VDM_STATE_ERR_SEND:
2031 * A partner which does not support USB PD will not reply,
2032 * so this is not a fatal error. At the same time, some
2033 * devices may not return GoodCRC under some circumstances,
2034 * so we need to retry.
2036 if (port->vdm_retries < 3) {
2037 tcpm_log(port, "VDM Tx error, retry");
2038 port->vdm_retries++;
2039 port->vdm_state = VDM_STATE_READY;
2040 if (PD_VDO_SVDM(vdo_hdr) && PD_VDO_CMDT(vdo_hdr) == CMDT_INIT)
2041 tcpm_ams_finish(port);
2043 tcpm_ams_finish(port);
2046 case VDM_STATE_SEND_MESSAGE:
2047 /* Prepare and send VDM */
2048 memset(&msg, 0, sizeof(msg));
2049 msg.header = PD_HEADER_LE(PD_DATA_VENDOR_DEF,
2052 port->negotiated_rev,
2053 port->message_id, port->vdo_count);
2054 for (i = 0; i < port->vdo_count; i++)
2055 msg.payload[i] = cpu_to_le32(port->vdo_data[i]);
2056 res = tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
2058 port->vdm_state = VDM_STATE_ERR_SEND;
2060 unsigned long timeout;
2062 port->vdm_retries = 0;
2063 port->vdo_data[0] = 0;
2064 port->vdm_state = VDM_STATE_BUSY;
2065 timeout = vdm_ready_timeout(vdo_hdr);
2066 mod_vdm_delayed_work(port, timeout);
2074 static void vdm_state_machine_work(struct kthread_work *work)
2076 struct tcpm_port *port = container_of(work, struct tcpm_port, vdm_state_machine);
2077 enum vdm_states prev_state;
2079 mutex_lock(&port->lock);
2082 * Continue running as long as the port is not busy and there was
2086 prev_state = port->vdm_state;
2087 vdm_run_state_machine(port);
2088 } while (port->vdm_state != prev_state &&
2089 port->vdm_state != VDM_STATE_BUSY &&
2090 port->vdm_state != VDM_STATE_SEND_MESSAGE);
2092 if (port->vdm_state < VDM_STATE_READY)
2093 port->vdm_sm_running = false;
2095 mutex_unlock(&port->lock);
2101 PDO_ERR_VSAFE5V_NOT_FIRST,
2102 PDO_ERR_PDO_TYPE_NOT_IN_ORDER,
2103 PDO_ERR_FIXED_NOT_SORTED,
2104 PDO_ERR_VARIABLE_BATT_NOT_SORTED,
2106 PDO_ERR_PPS_APDO_NOT_SORTED,
2107 PDO_ERR_DUPE_PPS_APDO,
2110 static const char * const pdo_err_msg[] = {
2111 [PDO_ERR_NO_VSAFE5V] =
2112 " err: source/sink caps should at least have vSafe5V",
2113 [PDO_ERR_VSAFE5V_NOT_FIRST] =
2114 " err: vSafe5V Fixed Supply Object Shall always be the first object",
2115 [PDO_ERR_PDO_TYPE_NOT_IN_ORDER] =
2116 " err: PDOs should be in the following order: Fixed; Battery; Variable",
2117 [PDO_ERR_FIXED_NOT_SORTED] =
2118 " err: Fixed supply pdos should be in increasing order of their fixed voltage",
2119 [PDO_ERR_VARIABLE_BATT_NOT_SORTED] =
2120 " err: Variable/Battery supply pdos should be in increasing order of their minimum voltage",
2121 [PDO_ERR_DUPE_PDO] =
2122 " err: Variable/Batt supply pdos cannot have same min/max voltage",
2123 [PDO_ERR_PPS_APDO_NOT_SORTED] =
2124 " err: Programmable power supply apdos should be in increasing order of their maximum voltage",
2125 [PDO_ERR_DUPE_PPS_APDO] =
2126 " err: Programmable power supply apdos cannot have same min/max voltage and max current",
2129 static enum pdo_err tcpm_caps_err(struct tcpm_port *port, const u32 *pdo,
2130 unsigned int nr_pdo)
2134 /* Should at least contain vSafe5v */
2136 return PDO_ERR_NO_VSAFE5V;
2138 /* The vSafe5V Fixed Supply Object Shall always be the first object */
2139 if (pdo_type(pdo[0]) != PDO_TYPE_FIXED ||
2140 pdo_fixed_voltage(pdo[0]) != VSAFE5V)
2141 return PDO_ERR_VSAFE5V_NOT_FIRST;
2143 for (i = 1; i < nr_pdo; i++) {
2144 if (pdo_type(pdo[i]) < pdo_type(pdo[i - 1])) {
2145 return PDO_ERR_PDO_TYPE_NOT_IN_ORDER;
2146 } else if (pdo_type(pdo[i]) == pdo_type(pdo[i - 1])) {
2147 enum pd_pdo_type type = pdo_type(pdo[i]);
2151 * The remaining Fixed Supply Objects, if
2152 * present, shall be sent in voltage order;
2153 * lowest to highest.
2155 case PDO_TYPE_FIXED:
2156 if (pdo_fixed_voltage(pdo[i]) <=
2157 pdo_fixed_voltage(pdo[i - 1]))
2158 return PDO_ERR_FIXED_NOT_SORTED;
2161 * The Battery Supply Objects and Variable
2162 * supply, if present shall be sent in Minimum
2163 * Voltage order; lowest to highest.
2167 if (pdo_min_voltage(pdo[i]) <
2168 pdo_min_voltage(pdo[i - 1]))
2169 return PDO_ERR_VARIABLE_BATT_NOT_SORTED;
2170 else if ((pdo_min_voltage(pdo[i]) ==
2171 pdo_min_voltage(pdo[i - 1])) &&
2172 (pdo_max_voltage(pdo[i]) ==
2173 pdo_max_voltage(pdo[i - 1])))
2174 return PDO_ERR_DUPE_PDO;
2177 * The Programmable Power Supply APDOs, if present,
2178 * shall be sent in Maximum Voltage order;
2179 * lowest to highest.
2182 if (pdo_apdo_type(pdo[i]) != APDO_TYPE_PPS)
2185 if (pdo_pps_apdo_max_voltage(pdo[i]) <
2186 pdo_pps_apdo_max_voltage(pdo[i - 1]))
2187 return PDO_ERR_PPS_APDO_NOT_SORTED;
2188 else if (pdo_pps_apdo_min_voltage(pdo[i]) ==
2189 pdo_pps_apdo_min_voltage(pdo[i - 1]) &&
2190 pdo_pps_apdo_max_voltage(pdo[i]) ==
2191 pdo_pps_apdo_max_voltage(pdo[i - 1]) &&
2192 pdo_pps_apdo_max_current(pdo[i]) ==
2193 pdo_pps_apdo_max_current(pdo[i - 1]))
2194 return PDO_ERR_DUPE_PPS_APDO;
2197 tcpm_log_force(port, " Unknown pdo type");
2205 static int tcpm_validate_caps(struct tcpm_port *port, const u32 *pdo,
2206 unsigned int nr_pdo)
2208 enum pdo_err err_index = tcpm_caps_err(port, pdo, nr_pdo);
2210 if (err_index != PDO_NO_ERR) {
2211 tcpm_log_force(port, " %s", pdo_err_msg[err_index]);
2218 static int tcpm_altmode_enter(struct typec_altmode *altmode, u32 *vdo)
2220 struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
2224 svdm_version = typec_get_negotiated_svdm_version(port->typec_port);
2225 if (svdm_version < 0)
2226 return svdm_version;
2228 header = VDO(altmode->svid, vdo ? 2 : 1, svdm_version, CMD_ENTER_MODE);
2229 header |= VDO_OPOS(altmode->mode);
2231 tcpm_queue_vdm_unlocked(port, header, vdo, vdo ? 1 : 0);
2235 static int tcpm_altmode_exit(struct typec_altmode *altmode)
2237 struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
2241 svdm_version = typec_get_negotiated_svdm_version(port->typec_port);
2242 if (svdm_version < 0)
2243 return svdm_version;
2245 header = VDO(altmode->svid, 1, svdm_version, CMD_EXIT_MODE);
2246 header |= VDO_OPOS(altmode->mode);
2248 tcpm_queue_vdm_unlocked(port, header, NULL, 0);
2252 static int tcpm_altmode_vdm(struct typec_altmode *altmode,
2253 u32 header, const u32 *data, int count)
2255 struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
2257 tcpm_queue_vdm_unlocked(port, header, data, count - 1);
2262 static const struct typec_altmode_ops tcpm_altmode_ops = {
2263 .enter = tcpm_altmode_enter,
2264 .exit = tcpm_altmode_exit,
2265 .vdm = tcpm_altmode_vdm,
2269 * PD (data, control) command handling functions
2271 static inline enum tcpm_state ready_state(struct tcpm_port *port)
2273 if (port->pwr_role == TYPEC_SOURCE)
2279 static int tcpm_pd_send_control(struct tcpm_port *port,
2280 enum pd_ctrl_msg_type type);
2282 static void tcpm_handle_alert(struct tcpm_port *port, const __le32 *payload,
2285 u32 p0 = le32_to_cpu(payload[0]);
2286 unsigned int type = usb_pd_ado_type(p0);
2289 tcpm_log(port, "Alert message received with no type");
2290 tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP);
2294 /* Just handling non-battery alerts for now */
2295 if (!(type & USB_PD_ADO_TYPE_BATT_STATUS_CHANGE)) {
2296 if (port->pwr_role == TYPEC_SOURCE) {
2297 port->upcoming_state = GET_STATUS_SEND;
2298 tcpm_ams_start(port, GETTING_SOURCE_SINK_STATUS);
2301 * Do not check SinkTxOk here in case the Source doesn't set its Rp to
2304 port->ams = GETTING_SOURCE_SINK_STATUS;
2305 tcpm_set_state(port, GET_STATUS_SEND, 0);
2308 tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP);
2312 static int tcpm_set_auto_vbus_discharge_threshold(struct tcpm_port *port,
2313 enum typec_pwr_opmode mode, bool pps_active,
2314 u32 requested_vbus_voltage)
2318 if (!port->tcpc->set_auto_vbus_discharge_threshold)
2321 ret = port->tcpc->set_auto_vbus_discharge_threshold(port->tcpc, mode, pps_active,
2322 requested_vbus_voltage);
2323 tcpm_log_force(port,
2324 "set_auto_vbus_discharge_threshold mode:%d pps_active:%c vbus:%u ret:%d",
2325 mode, pps_active ? 'y' : 'n', requested_vbus_voltage, ret);
2330 static void tcpm_pd_handle_state(struct tcpm_port *port,
2331 enum tcpm_state state,
2333 unsigned int delay_ms)
2335 switch (port->state) {
2339 tcpm_set_state(port, state, delay_ms);
2341 /* 8.3.3.4.1.1 and 6.8.1 power transitioning */
2342 case SNK_TRANSITION_SINK:
2343 case SNK_TRANSITION_SINK_VBUS:
2344 case SRC_TRANSITION_SUPPLY:
2345 tcpm_set_state(port, HARD_RESET_SEND, 0);
2348 if (!tcpm_ams_interruptible(port)) {
2349 tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ?
2350 SRC_SOFT_RESET_WAIT_SNK_TX :
2354 /* process the Message 6.8.1 */
2355 port->upcoming_state = state;
2356 port->next_ams = ams;
2357 tcpm_set_state(port, ready_state(port), delay_ms);
2363 static void tcpm_pd_handle_msg(struct tcpm_port *port,
2364 enum pd_msg_request message,
2367 switch (port->state) {
2371 tcpm_queue_message(port, message);
2373 /* PD 3.0 Spec 8.3.3.4.1.1 and 6.8.1 */
2374 case SNK_TRANSITION_SINK:
2375 case SNK_TRANSITION_SINK_VBUS:
2376 case SRC_TRANSITION_SUPPLY:
2377 tcpm_set_state(port, HARD_RESET_SEND, 0);
2380 if (!tcpm_ams_interruptible(port)) {
2381 tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ?
2382 SRC_SOFT_RESET_WAIT_SNK_TX :
2386 port->next_ams = ams;
2387 tcpm_set_state(port, ready_state(port), 0);
2388 /* 6.8.1 process the Message */
2389 tcpm_queue_message(port, message);
2395 static int tcpm_register_source_caps(struct tcpm_port *port)
2397 struct usb_power_delivery_desc desc = { port->negotiated_rev };
2398 struct usb_power_delivery_capabilities_desc caps = { };
2399 struct usb_power_delivery_capabilities *cap;
2401 if (!port->partner_pd)
2402 port->partner_pd = usb_power_delivery_register(NULL, &desc);
2403 if (IS_ERR(port->partner_pd))
2404 return PTR_ERR(port->partner_pd);
2406 memcpy(caps.pdo, port->source_caps, sizeof(u32) * port->nr_source_caps);
2407 caps.role = TYPEC_SOURCE;
2409 cap = usb_power_delivery_register_capabilities(port->partner_pd, &caps);
2411 return PTR_ERR(cap);
2413 port->partner_source_caps = cap;
2418 static int tcpm_register_sink_caps(struct tcpm_port *port)
2420 struct usb_power_delivery_desc desc = { port->negotiated_rev };
2421 struct usb_power_delivery_capabilities_desc caps = { };
2422 struct usb_power_delivery_capabilities *cap;
2424 if (!port->partner_pd)
2425 port->partner_pd = usb_power_delivery_register(NULL, &desc);
2426 if (IS_ERR(port->partner_pd))
2427 return PTR_ERR(port->partner_pd);
2429 memcpy(caps.pdo, port->sink_caps, sizeof(u32) * port->nr_sink_caps);
2430 caps.role = TYPEC_SINK;
2432 cap = usb_power_delivery_register_capabilities(port->partner_pd, &caps);
2434 return PTR_ERR(cap);
2436 port->partner_sink_caps = cap;
2441 static void tcpm_pd_data_request(struct tcpm_port *port,
2442 const struct pd_message *msg)
2444 enum pd_data_msg_type type = pd_header_type_le(msg->header);
2445 unsigned int cnt = pd_header_cnt_le(msg->header);
2446 unsigned int rev = pd_header_rev_le(msg->header);
2448 enum frs_typec_current partner_frs_current;
2452 if (tcpm_vdm_ams(port) && type != PD_DATA_VENDOR_DEF) {
2453 port->vdm_state = VDM_STATE_ERR_BUSY;
2454 tcpm_ams_finish(port);
2455 mod_vdm_delayed_work(port, 0);
2459 case PD_DATA_SOURCE_CAP:
2460 for (i = 0; i < cnt; i++)
2461 port->source_caps[i] = le32_to_cpu(msg->payload[i]);
2463 port->nr_source_caps = cnt;
2465 tcpm_log_source_caps(port);
2467 tcpm_validate_caps(port, port->source_caps,
2468 port->nr_source_caps);
2470 tcpm_register_source_caps(port);
2473 * Adjust revision in subsequent message headers, as required,
2474 * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't
2475 * support Rev 1.0 so just do nothing in that scenario.
2477 if (rev == PD_REV10) {
2478 if (port->ams == GET_SOURCE_CAPABILITIES)
2479 tcpm_ams_finish(port);
2483 if (rev < PD_MAX_REV)
2484 port->negotiated_rev = rev;
2486 if (port->pwr_role == TYPEC_SOURCE) {
2487 if (port->ams == GET_SOURCE_CAPABILITIES)
2488 tcpm_pd_handle_state(port, SRC_READY, NONE_AMS, 0);
2489 /* Unexpected Source Capabilities */
2491 tcpm_pd_handle_msg(port,
2492 port->negotiated_rev < PD_REV30 ?
2493 PD_MSG_CTRL_REJECT :
2494 PD_MSG_CTRL_NOT_SUPP,
2496 } else if (port->state == SNK_WAIT_CAPABILITIES) {
2498 * This message may be received even if VBUS is not
2499 * present. This is quite unexpected; see USB PD
2500 * specification, sections 8.3.3.6.3.1 and 8.3.3.6.3.2.
2501 * However, at the same time, we must be ready to
2502 * receive this message and respond to it 15ms after
2503 * receiving PS_RDY during power swap operations, no matter
2504 * if VBUS is available or not (USB PD specification,
2506 * So we need to accept the message either way,
2507 * but be prepared to keep waiting for VBUS after it was
2510 port->ams = POWER_NEGOTIATION;
2511 port->in_ams = true;
2512 tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
2514 if (port->ams == GET_SOURCE_CAPABILITIES)
2515 tcpm_ams_finish(port);
2516 tcpm_pd_handle_state(port, SNK_NEGOTIATE_CAPABILITIES,
2517 POWER_NEGOTIATION, 0);
2520 case PD_DATA_REQUEST:
2522 * Adjust revision in subsequent message headers, as required,
2523 * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't
2524 * support Rev 1.0 so just reject in that scenario.
2526 if (rev == PD_REV10) {
2527 tcpm_pd_handle_msg(port,
2528 port->negotiated_rev < PD_REV30 ?
2529 PD_MSG_CTRL_REJECT :
2530 PD_MSG_CTRL_NOT_SUPP,
2535 if (rev < PD_MAX_REV)
2536 port->negotiated_rev = rev;
2538 if (port->pwr_role != TYPEC_SOURCE || cnt != 1) {
2539 tcpm_pd_handle_msg(port,
2540 port->negotiated_rev < PD_REV30 ?
2541 PD_MSG_CTRL_REJECT :
2542 PD_MSG_CTRL_NOT_SUPP,
2547 port->sink_request = le32_to_cpu(msg->payload[0]);
2549 if (port->vdm_sm_running && port->explicit_contract) {
2550 tcpm_pd_handle_msg(port, PD_MSG_CTRL_WAIT, port->ams);
2554 if (port->state == SRC_SEND_CAPABILITIES)
2555 tcpm_set_state(port, SRC_NEGOTIATE_CAPABILITIES, 0);
2557 tcpm_pd_handle_state(port, SRC_NEGOTIATE_CAPABILITIES,
2558 POWER_NEGOTIATION, 0);
2560 case PD_DATA_SINK_CAP:
2561 /* We don't do anything with this at the moment... */
2562 for (i = 0; i < cnt; i++)
2563 port->sink_caps[i] = le32_to_cpu(msg->payload[i]);
2565 partner_frs_current = (port->sink_caps[0] & PDO_FIXED_FRS_CURR_MASK) >>
2566 PDO_FIXED_FRS_CURR_SHIFT;
2567 frs_enable = partner_frs_current && (partner_frs_current <=
2568 port->new_source_frs_current);
2570 "Port partner FRS capable partner_frs_current:%u port_frs_current:%u enable:%c",
2571 partner_frs_current, port->new_source_frs_current, frs_enable ? 'y' : 'n');
2573 ret = port->tcpc->enable_frs(port->tcpc, true);
2574 tcpm_log(port, "Enable FRS %s, ret:%d\n", ret ? "fail" : "success", ret);
2577 port->nr_sink_caps = cnt;
2578 port->sink_cap_done = true;
2579 tcpm_register_sink_caps(port);
2581 if (port->ams == GET_SINK_CAPABILITIES)
2582 tcpm_set_state(port, ready_state(port), 0);
2583 /* Unexpected Sink Capabilities */
2585 tcpm_pd_handle_msg(port,
2586 port->negotiated_rev < PD_REV30 ?
2587 PD_MSG_CTRL_REJECT :
2588 PD_MSG_CTRL_NOT_SUPP,
2591 case PD_DATA_VENDOR_DEF:
2592 tcpm_handle_vdm_request(port, msg->payload, cnt);
2595 port->bist_request = le32_to_cpu(msg->payload[0]);
2596 tcpm_pd_handle_state(port, BIST_RX, BIST, 0);
2599 if (port->state != SRC_READY && port->state != SNK_READY)
2600 tcpm_pd_handle_state(port, port->pwr_role == TYPEC_SOURCE ?
2601 SRC_SOFT_RESET_WAIT_SNK_TX : SNK_SOFT_RESET,
2604 tcpm_handle_alert(port, msg->payload, cnt);
2606 case PD_DATA_BATT_STATUS:
2607 case PD_DATA_GET_COUNTRY_INFO:
2608 /* Currently unsupported */
2609 tcpm_pd_handle_msg(port, port->negotiated_rev < PD_REV30 ?
2610 PD_MSG_CTRL_REJECT :
2611 PD_MSG_CTRL_NOT_SUPP,
2615 tcpm_pd_handle_msg(port, port->negotiated_rev < PD_REV30 ?
2616 PD_MSG_CTRL_REJECT :
2617 PD_MSG_CTRL_NOT_SUPP,
2619 tcpm_log(port, "Unrecognized data message type %#x", type);
2624 static void tcpm_pps_complete(struct tcpm_port *port, int result)
2626 if (port->pps_pending) {
2627 port->pps_status = result;
2628 port->pps_pending = false;
2629 complete(&port->pps_complete);
2633 static void tcpm_pd_ctrl_request(struct tcpm_port *port,
2634 const struct pd_message *msg)
2636 enum pd_ctrl_msg_type type = pd_header_type_le(msg->header);
2637 enum tcpm_state next_state;
2640 * Stop VDM state machine if interrupted by other Messages while NOT_SUPP is allowed in
2641 * VDM AMS if waiting for VDM responses and will be handled later.
2643 if (tcpm_vdm_ams(port) && type != PD_CTRL_NOT_SUPP && type != PD_CTRL_GOOD_CRC) {
2644 port->vdm_state = VDM_STATE_ERR_BUSY;
2645 tcpm_ams_finish(port);
2646 mod_vdm_delayed_work(port, 0);
2650 case PD_CTRL_GOOD_CRC:
2653 case PD_CTRL_GET_SOURCE_CAP:
2654 tcpm_pd_handle_msg(port, PD_MSG_DATA_SOURCE_CAP, GET_SOURCE_CAPABILITIES);
2656 case PD_CTRL_GET_SINK_CAP:
2657 tcpm_pd_handle_msg(port, PD_MSG_DATA_SINK_CAP, GET_SINK_CAPABILITIES);
2659 case PD_CTRL_GOTO_MIN:
2661 case PD_CTRL_PS_RDY:
2662 switch (port->state) {
2663 case SNK_TRANSITION_SINK:
2664 if (port->vbus_present) {
2665 tcpm_set_current_limit(port,
2666 port->req_current_limit,
2667 port->req_supply_voltage);
2668 port->explicit_contract = true;
2669 tcpm_set_auto_vbus_discharge_threshold(port,
2671 port->pps_data.active,
2672 port->supply_voltage);
2673 tcpm_set_state(port, SNK_READY, 0);
2676 * Seen after power swap. Keep waiting for VBUS
2677 * in a transitional state.
2679 tcpm_set_state(port,
2680 SNK_TRANSITION_SINK_VBUS, 0);
2683 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED:
2684 tcpm_set_state(port, PR_SWAP_SRC_SNK_SINK_ON, 0);
2686 case PR_SWAP_SNK_SRC_SINK_OFF:
2687 tcpm_set_state(port, PR_SWAP_SNK_SRC_SOURCE_ON, 0);
2689 case VCONN_SWAP_WAIT_FOR_VCONN:
2690 tcpm_set_state(port, VCONN_SWAP_TURN_OFF_VCONN, 0);
2692 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
2693 tcpm_set_state(port, FR_SWAP_SNK_SRC_NEW_SINK_READY, 0);
2696 tcpm_pd_handle_state(port,
2697 port->pwr_role == TYPEC_SOURCE ?
2698 SRC_SOFT_RESET_WAIT_SNK_TX :
2704 case PD_CTRL_REJECT:
2706 case PD_CTRL_NOT_SUPP:
2707 switch (port->state) {
2708 case SNK_NEGOTIATE_CAPABILITIES:
2709 /* USB PD specification, Figure 8-43 */
2710 if (port->explicit_contract)
2711 next_state = SNK_READY;
2713 next_state = SNK_WAIT_CAPABILITIES;
2715 /* Threshold was relaxed before sending Request. Restore it back. */
2716 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
2717 port->pps_data.active,
2718 port->supply_voltage);
2719 tcpm_set_state(port, next_state, 0);
2721 case SNK_NEGOTIATE_PPS_CAPABILITIES:
2722 /* Revert data back from any requested PPS updates */
2723 port->pps_data.req_out_volt = port->supply_voltage;
2724 port->pps_data.req_op_curr = port->current_limit;
2725 port->pps_status = (type == PD_CTRL_WAIT ?
2726 -EAGAIN : -EOPNOTSUPP);
2728 /* Threshold was relaxed before sending Request. Restore it back. */
2729 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
2730 port->pps_data.active,
2731 port->supply_voltage);
2733 tcpm_set_state(port, SNK_READY, 0);
2736 port->swap_status = (type == PD_CTRL_WAIT ?
2737 -EAGAIN : -EOPNOTSUPP);
2738 tcpm_set_state(port, DR_SWAP_CANCEL, 0);
2741 port->swap_status = (type == PD_CTRL_WAIT ?
2742 -EAGAIN : -EOPNOTSUPP);
2743 tcpm_set_state(port, PR_SWAP_CANCEL, 0);
2745 case VCONN_SWAP_SEND:
2746 port->swap_status = (type == PD_CTRL_WAIT ?
2747 -EAGAIN : -EOPNOTSUPP);
2748 tcpm_set_state(port, VCONN_SWAP_CANCEL, 0);
2751 tcpm_set_state(port, FR_SWAP_CANCEL, 0);
2754 port->sink_cap_done = true;
2755 tcpm_set_state(port, ready_state(port), 0);
2758 * Some port partners do not support GET_STATUS, avoid soft reset the link to
2759 * prevent redundant power re-negotiation
2761 case GET_STATUS_SEND:
2762 tcpm_set_state(port, ready_state(port), 0);
2766 if (port->vdm_state > VDM_STATE_READY) {
2767 port->vdm_state = VDM_STATE_DONE;
2768 if (tcpm_vdm_ams(port))
2769 tcpm_ams_finish(port);
2770 mod_vdm_delayed_work(port, 0);
2775 tcpm_pd_handle_state(port,
2776 port->pwr_role == TYPEC_SOURCE ?
2777 SRC_SOFT_RESET_WAIT_SNK_TX :
2783 case PD_CTRL_ACCEPT:
2784 switch (port->state) {
2785 case SNK_NEGOTIATE_CAPABILITIES:
2786 port->pps_data.active = false;
2787 tcpm_set_state(port, SNK_TRANSITION_SINK, 0);
2789 case SNK_NEGOTIATE_PPS_CAPABILITIES:
2790 port->pps_data.active = true;
2791 port->pps_data.min_volt = port->pps_data.req_min_volt;
2792 port->pps_data.max_volt = port->pps_data.req_max_volt;
2793 port->pps_data.max_curr = port->pps_data.req_max_curr;
2794 port->req_supply_voltage = port->pps_data.req_out_volt;
2795 port->req_current_limit = port->pps_data.req_op_curr;
2796 power_supply_changed(port->psy);
2797 tcpm_set_state(port, SNK_TRANSITION_SINK, 0);
2799 case SOFT_RESET_SEND:
2800 if (port->ams == SOFT_RESET_AMS)
2801 tcpm_ams_finish(port);
2802 if (port->pwr_role == TYPEC_SOURCE) {
2803 port->upcoming_state = SRC_SEND_CAPABILITIES;
2804 tcpm_ams_start(port, POWER_NEGOTIATION);
2806 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
2810 tcpm_set_state(port, DR_SWAP_CHANGE_DR, 0);
2813 tcpm_set_state(port, PR_SWAP_START, 0);
2815 case VCONN_SWAP_SEND:
2816 tcpm_set_state(port, VCONN_SWAP_START, 0);
2819 tcpm_set_state(port, FR_SWAP_SNK_SRC_TRANSITION_TO_OFF, 0);
2822 tcpm_pd_handle_state(port,
2823 port->pwr_role == TYPEC_SOURCE ?
2824 SRC_SOFT_RESET_WAIT_SNK_TX :
2830 case PD_CTRL_SOFT_RESET:
2831 port->ams = SOFT_RESET_AMS;
2832 tcpm_set_state(port, SOFT_RESET, 0);
2834 case PD_CTRL_DR_SWAP:
2837 * 6.3.9: If an alternate mode is active, a request to swap
2838 * alternate modes shall trigger a port reset.
2840 if (port->typec_caps.data != TYPEC_PORT_DRD) {
2841 tcpm_pd_handle_msg(port,
2842 port->negotiated_rev < PD_REV30 ?
2843 PD_MSG_CTRL_REJECT :
2844 PD_MSG_CTRL_NOT_SUPP,
2847 if (port->send_discover) {
2848 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
2852 tcpm_pd_handle_state(port, DR_SWAP_ACCEPT, DATA_ROLE_SWAP, 0);
2855 case PD_CTRL_PR_SWAP:
2856 if (port->port_type != TYPEC_PORT_DRP) {
2857 tcpm_pd_handle_msg(port,
2858 port->negotiated_rev < PD_REV30 ?
2859 PD_MSG_CTRL_REJECT :
2860 PD_MSG_CTRL_NOT_SUPP,
2863 if (port->send_discover) {
2864 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
2868 tcpm_pd_handle_state(port, PR_SWAP_ACCEPT, POWER_ROLE_SWAP, 0);
2871 case PD_CTRL_VCONN_SWAP:
2872 if (port->send_discover) {
2873 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
2877 tcpm_pd_handle_state(port, VCONN_SWAP_ACCEPT, VCONN_SWAP, 0);
2879 case PD_CTRL_GET_SOURCE_CAP_EXT:
2880 case PD_CTRL_GET_STATUS:
2881 case PD_CTRL_FR_SWAP:
2882 case PD_CTRL_GET_PPS_STATUS:
2883 case PD_CTRL_GET_COUNTRY_CODES:
2884 /* Currently not supported */
2885 tcpm_pd_handle_msg(port,
2886 port->negotiated_rev < PD_REV30 ?
2887 PD_MSG_CTRL_REJECT :
2888 PD_MSG_CTRL_NOT_SUPP,
2892 tcpm_pd_handle_msg(port,
2893 port->negotiated_rev < PD_REV30 ?
2894 PD_MSG_CTRL_REJECT :
2895 PD_MSG_CTRL_NOT_SUPP,
2897 tcpm_log(port, "Unrecognized ctrl message type %#x", type);
2902 static void tcpm_pd_ext_msg_request(struct tcpm_port *port,
2903 const struct pd_message *msg)
2905 enum pd_ext_msg_type type = pd_header_type_le(msg->header);
2906 unsigned int data_size = pd_ext_header_data_size_le(msg->ext_msg.header);
2908 /* stopping VDM state machine if interrupted by other Messages */
2909 if (tcpm_vdm_ams(port)) {
2910 port->vdm_state = VDM_STATE_ERR_BUSY;
2911 tcpm_ams_finish(port);
2912 mod_vdm_delayed_work(port, 0);
2915 if (!(le16_to_cpu(msg->ext_msg.header) & PD_EXT_HDR_CHUNKED)) {
2916 tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS);
2917 tcpm_log(port, "Unchunked extended messages unsupported");
2921 if (data_size > PD_EXT_MAX_CHUNK_DATA) {
2922 tcpm_pd_handle_state(port, CHUNK_NOT_SUPP, NONE_AMS, PD_T_CHUNK_NOT_SUPP);
2923 tcpm_log(port, "Chunk handling not yet supported");
2929 case PD_EXT_PPS_STATUS:
2930 if (port->ams == GETTING_SOURCE_SINK_STATUS) {
2931 tcpm_ams_finish(port);
2932 tcpm_set_state(port, ready_state(port), 0);
2934 /* unexpected Status or PPS_Status Message */
2935 tcpm_pd_handle_state(port, port->pwr_role == TYPEC_SOURCE ?
2936 SRC_SOFT_RESET_WAIT_SNK_TX : SNK_SOFT_RESET,
2940 case PD_EXT_SOURCE_CAP_EXT:
2941 case PD_EXT_GET_BATT_CAP:
2942 case PD_EXT_GET_BATT_STATUS:
2943 case PD_EXT_BATT_CAP:
2944 case PD_EXT_GET_MANUFACTURER_INFO:
2945 case PD_EXT_MANUFACTURER_INFO:
2946 case PD_EXT_SECURITY_REQUEST:
2947 case PD_EXT_SECURITY_RESPONSE:
2948 case PD_EXT_FW_UPDATE_REQUEST:
2949 case PD_EXT_FW_UPDATE_RESPONSE:
2950 case PD_EXT_COUNTRY_INFO:
2951 case PD_EXT_COUNTRY_CODES:
2952 tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS);
2955 tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS);
2956 tcpm_log(port, "Unrecognized extended message type %#x", type);
2961 static void tcpm_pd_rx_handler(struct kthread_work *work)
2963 struct pd_rx_event *event = container_of(work,
2964 struct pd_rx_event, work);
2965 const struct pd_message *msg = &event->msg;
2966 unsigned int cnt = pd_header_cnt_le(msg->header);
2967 struct tcpm_port *port = event->port;
2969 mutex_lock(&port->lock);
2971 tcpm_log(port, "PD RX, header: %#x [%d]", le16_to_cpu(msg->header),
2974 if (port->attached) {
2975 enum pd_ctrl_msg_type type = pd_header_type_le(msg->header);
2976 unsigned int msgid = pd_header_msgid_le(msg->header);
2979 * USB PD standard, 6.6.1.2:
2980 * "... if MessageID value in a received Message is the
2981 * same as the stored value, the receiver shall return a
2982 * GoodCRC Message with that MessageID value and drop
2983 * the Message (this is a retry of an already received
2984 * Message). Note: this shall not apply to the Soft_Reset
2985 * Message which always has a MessageID value of zero."
2987 if (msgid == port->rx_msgid && type != PD_CTRL_SOFT_RESET)
2989 port->rx_msgid = msgid;
2992 * If both ends believe to be DFP/host, we have a data role
2995 if (!!(le16_to_cpu(msg->header) & PD_HEADER_DATA_ROLE) ==
2996 (port->data_role == TYPEC_HOST)) {
2998 "Data role mismatch, initiating error recovery");
2999 tcpm_set_state(port, ERROR_RECOVERY, 0);
3001 if (le16_to_cpu(msg->header) & PD_HEADER_EXT_HDR)
3002 tcpm_pd_ext_msg_request(port, msg);
3004 tcpm_pd_data_request(port, msg);
3006 tcpm_pd_ctrl_request(port, msg);
3011 mutex_unlock(&port->lock);
3015 void tcpm_pd_receive(struct tcpm_port *port, const struct pd_message *msg)
3017 struct pd_rx_event *event;
3019 event = kzalloc(sizeof(*event), GFP_ATOMIC);
3023 kthread_init_work(&event->work, tcpm_pd_rx_handler);
3025 memcpy(&event->msg, msg, sizeof(*msg));
3026 kthread_queue_work(port->wq, &event->work);
3028 EXPORT_SYMBOL_GPL(tcpm_pd_receive);
3030 static int tcpm_pd_send_control(struct tcpm_port *port,
3031 enum pd_ctrl_msg_type type)
3033 struct pd_message msg;
3035 memset(&msg, 0, sizeof(msg));
3036 msg.header = PD_HEADER_LE(type, port->pwr_role,
3038 port->negotiated_rev,
3039 port->message_id, 0);
3041 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
3045 * Send queued message without affecting state.
3046 * Return true if state machine should go back to sleep,
3049 static bool tcpm_send_queued_message(struct tcpm_port *port)
3051 enum pd_msg_request queued_message;
3055 queued_message = port->queued_message;
3056 port->queued_message = PD_MSG_NONE;
3058 switch (queued_message) {
3059 case PD_MSG_CTRL_WAIT:
3060 tcpm_pd_send_control(port, PD_CTRL_WAIT);
3062 case PD_MSG_CTRL_REJECT:
3063 tcpm_pd_send_control(port, PD_CTRL_REJECT);
3065 case PD_MSG_CTRL_NOT_SUPP:
3066 tcpm_pd_send_control(port, PD_CTRL_NOT_SUPP);
3068 case PD_MSG_DATA_SINK_CAP:
3069 ret = tcpm_pd_send_sink_caps(port);
3071 tcpm_log(port, "Unable to send snk caps, ret=%d", ret);
3072 tcpm_set_state(port, SNK_SOFT_RESET, 0);
3074 tcpm_ams_finish(port);
3076 case PD_MSG_DATA_SOURCE_CAP:
3077 ret = tcpm_pd_send_source_caps(port);
3080 "Unable to send src caps, ret=%d",
3082 tcpm_set_state(port, SOFT_RESET_SEND, 0);
3083 } else if (port->pwr_role == TYPEC_SOURCE) {
3084 tcpm_ams_finish(port);
3085 tcpm_set_state(port, HARD_RESET_SEND,
3086 PD_T_SENDER_RESPONSE);
3088 tcpm_ams_finish(port);
3094 } while (port->queued_message != PD_MSG_NONE);
3096 if (port->delayed_state != INVALID_STATE) {
3097 if (ktime_after(port->delayed_runtime, ktime_get())) {
3098 mod_tcpm_delayed_work(port, ktime_to_ms(ktime_sub(port->delayed_runtime,
3102 port->delayed_state = INVALID_STATE;
3107 static int tcpm_pd_check_request(struct tcpm_port *port)
3109 u32 pdo, rdo = port->sink_request;
3110 unsigned int max, op, pdo_max, index;
3111 enum pd_pdo_type type;
3113 index = rdo_index(rdo);
3114 if (!index || index > port->nr_src_pdo)
3117 pdo = port->src_pdo[index - 1];
3118 type = pdo_type(pdo);
3120 case PDO_TYPE_FIXED:
3122 max = rdo_max_current(rdo);
3123 op = rdo_op_current(rdo);
3124 pdo_max = pdo_max_current(pdo);
3128 if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH))
3131 if (type == PDO_TYPE_FIXED)
3133 "Requested %u mV, %u mA for %u / %u mA",
3134 pdo_fixed_voltage(pdo), pdo_max, op, max);
3137 "Requested %u -> %u mV, %u mA for %u / %u mA",
3138 pdo_min_voltage(pdo), pdo_max_voltage(pdo),
3142 max = rdo_max_power(rdo);
3143 op = rdo_op_power(rdo);
3144 pdo_max = pdo_max_power(pdo);
3148 if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH))
3151 "Requested %u -> %u mV, %u mW for %u / %u mW",
3152 pdo_min_voltage(pdo), pdo_max_voltage(pdo),
3159 port->op_vsafe5v = index == 1;
3164 #define min_power(x, y) min(pdo_max_power(x), pdo_max_power(y))
3165 #define min_current(x, y) min(pdo_max_current(x), pdo_max_current(y))
3167 static int tcpm_pd_select_pdo(struct tcpm_port *port, int *sink_pdo,
3170 unsigned int i, j, max_src_mv = 0, min_src_mv = 0, max_mw = 0,
3171 max_mv = 0, src_mw = 0, src_ma = 0, max_snk_mv = 0,
3175 port->pps_data.supported = false;
3176 port->usb_type = POWER_SUPPLY_USB_TYPE_PD;
3177 power_supply_changed(port->psy);
3180 * Select the source PDO providing the most power which has a
3183 for (i = 0; i < port->nr_source_caps; i++) {
3184 u32 pdo = port->source_caps[i];
3185 enum pd_pdo_type type = pdo_type(pdo);
3188 case PDO_TYPE_FIXED:
3189 max_src_mv = pdo_fixed_voltage(pdo);
3190 min_src_mv = max_src_mv;
3194 max_src_mv = pdo_max_voltage(pdo);
3195 min_src_mv = pdo_min_voltage(pdo);
3198 if (pdo_apdo_type(pdo) == APDO_TYPE_PPS) {
3199 port->pps_data.supported = true;
3201 POWER_SUPPLY_USB_TYPE_PD_PPS;
3202 power_supply_changed(port->psy);
3206 tcpm_log(port, "Invalid source PDO type, ignoring");
3211 case PDO_TYPE_FIXED:
3213 src_ma = pdo_max_current(pdo);
3214 src_mw = src_ma * min_src_mv / 1000;
3217 src_mw = pdo_max_power(pdo);
3222 tcpm_log(port, "Invalid source PDO type, ignoring");
3226 for (j = 0; j < port->nr_snk_pdo; j++) {
3227 pdo = port->snk_pdo[j];
3229 switch (pdo_type(pdo)) {
3230 case PDO_TYPE_FIXED:
3231 max_snk_mv = pdo_fixed_voltage(pdo);
3232 min_snk_mv = max_snk_mv;
3236 max_snk_mv = pdo_max_voltage(pdo);
3237 min_snk_mv = pdo_min_voltage(pdo);
3242 tcpm_log(port, "Invalid sink PDO type, ignoring");
3246 if (max_src_mv <= max_snk_mv &&
3247 min_src_mv >= min_snk_mv) {
3248 /* Prefer higher voltages if available */
3249 if ((src_mw == max_mw && min_src_mv > max_mv) ||
3254 max_mv = min_src_mv;
3264 static unsigned int tcpm_pd_select_pps_apdo(struct tcpm_port *port)
3266 unsigned int i, src_ma, max_temp_mw = 0, max_op_ma, op_mw;
3267 unsigned int src_pdo = 0;
3270 for (i = 1; i < port->nr_source_caps; ++i) {
3271 pdo = port->source_caps[i];
3273 switch (pdo_type(pdo)) {
3275 if (pdo_apdo_type(pdo) != APDO_TYPE_PPS) {
3276 tcpm_log(port, "Not PPS APDO (source), ignoring");
3280 if (port->pps_data.req_out_volt > pdo_pps_apdo_max_voltage(pdo) ||
3281 port->pps_data.req_out_volt < pdo_pps_apdo_min_voltage(pdo))
3284 src_ma = pdo_pps_apdo_max_current(pdo);
3285 max_op_ma = min(src_ma, port->pps_data.req_op_curr);
3286 op_mw = max_op_ma * port->pps_data.req_out_volt / 1000;
3287 if (op_mw > max_temp_mw) {
3289 max_temp_mw = op_mw;
3293 tcpm_log(port, "Not APDO type (source), ignoring");
3299 src = port->source_caps[src_pdo];
3301 port->pps_data.req_min_volt = pdo_pps_apdo_min_voltage(src);
3302 port->pps_data.req_max_volt = pdo_pps_apdo_max_voltage(src);
3303 port->pps_data.req_max_curr = pdo_pps_apdo_max_current(src);
3304 port->pps_data.req_op_curr = min(port->pps_data.req_max_curr,
3305 port->pps_data.req_op_curr);
3311 static int tcpm_pd_build_request(struct tcpm_port *port, u32 *rdo)
3313 unsigned int mv, ma, mw, flags;
3314 unsigned int max_ma, max_mw;
3315 enum pd_pdo_type type;
3316 u32 pdo, matching_snk_pdo;
3317 int src_pdo_index = 0;
3318 int snk_pdo_index = 0;
3321 ret = tcpm_pd_select_pdo(port, &snk_pdo_index, &src_pdo_index);
3325 pdo = port->source_caps[src_pdo_index];
3326 matching_snk_pdo = port->snk_pdo[snk_pdo_index];
3327 type = pdo_type(pdo);
3330 case PDO_TYPE_FIXED:
3331 mv = pdo_fixed_voltage(pdo);
3335 mv = pdo_min_voltage(pdo);
3338 tcpm_log(port, "Invalid PDO selected!");
3342 /* Select maximum available current within the sink pdo's limit */
3343 if (type == PDO_TYPE_BATT) {
3344 mw = min_power(pdo, matching_snk_pdo);
3345 ma = 1000 * mw / mv;
3347 ma = min_current(pdo, matching_snk_pdo);
3348 mw = ma * mv / 1000;
3351 flags = RDO_USB_COMM | RDO_NO_SUSPEND;
3353 /* Set mismatch bit if offered power is less than operating power */
3356 if (mw < port->operating_snk_mw) {
3357 flags |= RDO_CAP_MISMATCH;
3358 if (type == PDO_TYPE_BATT &&
3359 (pdo_max_power(matching_snk_pdo) > pdo_max_power(pdo)))
3360 max_mw = pdo_max_power(matching_snk_pdo);
3361 else if (pdo_max_current(matching_snk_pdo) >
3362 pdo_max_current(pdo))
3363 max_ma = pdo_max_current(matching_snk_pdo);
3366 tcpm_log(port, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d",
3367 port->cc_req, port->cc1, port->cc2, port->vbus_source,
3368 port->vconn_role == TYPEC_SOURCE ? "source" : "sink",
3371 if (type == PDO_TYPE_BATT) {
3372 *rdo = RDO_BATT(src_pdo_index + 1, mw, max_mw, flags);
3374 tcpm_log(port, "Requesting PDO %d: %u mV, %u mW%s",
3375 src_pdo_index, mv, mw,
3376 flags & RDO_CAP_MISMATCH ? " [mismatch]" : "");
3378 *rdo = RDO_FIXED(src_pdo_index + 1, ma, max_ma, flags);
3380 tcpm_log(port, "Requesting PDO %d: %u mV, %u mA%s",
3381 src_pdo_index, mv, ma,
3382 flags & RDO_CAP_MISMATCH ? " [mismatch]" : "");
3385 port->req_current_limit = ma;
3386 port->req_supply_voltage = mv;
3391 static int tcpm_pd_send_request(struct tcpm_port *port)
3393 struct pd_message msg;
3397 ret = tcpm_pd_build_request(port, &rdo);
3402 * Relax the threshold as voltage will be adjusted after Accept Message plus tSrcTransition.
3403 * It is safer to modify the threshold here.
3405 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0);
3407 memset(&msg, 0, sizeof(msg));
3408 msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
3411 port->negotiated_rev,
3412 port->message_id, 1);
3413 msg.payload[0] = cpu_to_le32(rdo);
3415 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
3418 static int tcpm_pd_build_pps_request(struct tcpm_port *port, u32 *rdo)
3420 unsigned int out_mv, op_ma, op_mw, max_mv, max_ma, flags;
3421 unsigned int src_pdo_index;
3423 src_pdo_index = tcpm_pd_select_pps_apdo(port);
3427 max_mv = port->pps_data.req_max_volt;
3428 max_ma = port->pps_data.req_max_curr;
3429 out_mv = port->pps_data.req_out_volt;
3430 op_ma = port->pps_data.req_op_curr;
3432 flags = RDO_USB_COMM | RDO_NO_SUSPEND;
3434 op_mw = (op_ma * out_mv) / 1000;
3435 if (op_mw < port->operating_snk_mw) {
3437 * Try raising current to meet power needs. If that's not enough
3438 * then try upping the voltage. If that's still not enough
3439 * then we've obviously chosen a PPS APDO which really isn't
3440 * suitable so abandon ship.
3442 op_ma = (port->operating_snk_mw * 1000) / out_mv;
3443 if ((port->operating_snk_mw * 1000) % out_mv)
3445 op_ma += RDO_PROG_CURR_MA_STEP - (op_ma % RDO_PROG_CURR_MA_STEP);
3447 if (op_ma > max_ma) {
3449 out_mv = (port->operating_snk_mw * 1000) / op_ma;
3450 if ((port->operating_snk_mw * 1000) % op_ma)
3452 out_mv += RDO_PROG_VOLT_MV_STEP -
3453 (out_mv % RDO_PROG_VOLT_MV_STEP);
3455 if (out_mv > max_mv) {
3456 tcpm_log(port, "Invalid PPS APDO selected!");
3462 tcpm_log(port, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d",
3463 port->cc_req, port->cc1, port->cc2, port->vbus_source,
3464 port->vconn_role == TYPEC_SOURCE ? "source" : "sink",
3467 *rdo = RDO_PROG(src_pdo_index + 1, out_mv, op_ma, flags);
3469 tcpm_log(port, "Requesting APDO %d: %u mV, %u mA",
3470 src_pdo_index, out_mv, op_ma);
3472 port->pps_data.req_op_curr = op_ma;
3473 port->pps_data.req_out_volt = out_mv;
3478 static int tcpm_pd_send_pps_request(struct tcpm_port *port)
3480 struct pd_message msg;
3484 ret = tcpm_pd_build_pps_request(port, &rdo);
3488 /* Relax the threshold as voltage will be adjusted right after Accept Message. */
3489 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0);
3491 memset(&msg, 0, sizeof(msg));
3492 msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
3495 port->negotiated_rev,
3496 port->message_id, 1);
3497 msg.payload[0] = cpu_to_le32(rdo);
3499 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
3502 static int tcpm_set_vbus(struct tcpm_port *port, bool enable)
3506 if (enable && port->vbus_charge)
3509 tcpm_log(port, "vbus:=%d charge=%d", enable, port->vbus_charge);
3511 ret = port->tcpc->set_vbus(port->tcpc, enable, port->vbus_charge);
3515 port->vbus_source = enable;
3519 static int tcpm_set_charge(struct tcpm_port *port, bool charge)
3523 if (charge && port->vbus_source)
3526 if (charge != port->vbus_charge) {
3527 tcpm_log(port, "vbus=%d charge:=%d", port->vbus_source, charge);
3528 ret = port->tcpc->set_vbus(port->tcpc, port->vbus_source,
3533 port->vbus_charge = charge;
3534 power_supply_changed(port->psy);
3538 static bool tcpm_start_toggling(struct tcpm_port *port, enum typec_cc_status cc)
3542 if (!port->tcpc->start_toggling)
3545 tcpm_log_force(port, "Start toggling");
3546 ret = port->tcpc->start_toggling(port->tcpc, port->port_type, cc);
3550 static int tcpm_init_vbus(struct tcpm_port *port)
3554 ret = port->tcpc->set_vbus(port->tcpc, false, false);
3555 port->vbus_source = false;
3556 port->vbus_charge = false;
3560 static int tcpm_init_vconn(struct tcpm_port *port)
3564 ret = port->tcpc->set_vconn(port->tcpc, false);
3565 port->vconn_role = TYPEC_SINK;
3569 static void tcpm_typec_connect(struct tcpm_port *port)
3571 if (!port->connected) {
3572 /* Make sure we don't report stale identity information */
3573 memset(&port->partner_ident, 0, sizeof(port->partner_ident));
3574 port->partner_desc.usb_pd = port->pd_capable;
3575 if (tcpm_port_is_debug(port))
3576 port->partner_desc.accessory = TYPEC_ACCESSORY_DEBUG;
3577 else if (tcpm_port_is_audio(port))
3578 port->partner_desc.accessory = TYPEC_ACCESSORY_AUDIO;
3580 port->partner_desc.accessory = TYPEC_ACCESSORY_NONE;
3581 port->partner = typec_register_partner(port->typec_port,
3582 &port->partner_desc);
3583 port->connected = true;
3584 typec_partner_set_usb_power_delivery(port->partner, port->partner_pd);
3588 static int tcpm_src_attach(struct tcpm_port *port)
3590 enum typec_cc_polarity polarity =
3591 port->cc2 == TYPEC_CC_RD ? TYPEC_POLARITY_CC2
3592 : TYPEC_POLARITY_CC1;
3598 ret = tcpm_set_polarity(port, polarity);
3602 tcpm_enable_auto_vbus_discharge(port, true);
3604 ret = tcpm_set_roles(port, true, TYPEC_SOURCE, tcpm_data_role_for_source(port));
3608 if (port->pd_supported) {
3609 ret = port->tcpc->set_pd_rx(port->tcpc, true);
3611 goto out_disable_mux;
3615 * USB Type-C specification, version 1.2,
3616 * chapter 4.5.2.2.8.1 (Attached.SRC Requirements)
3617 * Enable VCONN only if the non-RD port is set to RA.
3619 if ((polarity == TYPEC_POLARITY_CC1 && port->cc2 == TYPEC_CC_RA) ||
3620 (polarity == TYPEC_POLARITY_CC2 && port->cc1 == TYPEC_CC_RA)) {
3621 ret = tcpm_set_vconn(port, true);
3623 goto out_disable_pd;
3626 ret = tcpm_set_vbus(port, true);
3628 goto out_disable_vconn;
3630 port->pd_capable = false;
3632 port->partner = NULL;
3634 port->attached = true;
3635 port->send_discover = true;
3640 tcpm_set_vconn(port, false);
3642 if (port->pd_supported)
3643 port->tcpc->set_pd_rx(port->tcpc, false);
3645 tcpm_mux_set(port, TYPEC_STATE_SAFE, USB_ROLE_NONE,
3646 TYPEC_ORIENTATION_NONE);
3650 static void tcpm_typec_disconnect(struct tcpm_port *port)
3652 if (port->connected) {
3653 typec_partner_set_usb_power_delivery(port->partner, NULL);
3654 typec_unregister_partner(port->partner);
3655 port->partner = NULL;
3656 port->connected = false;
3660 static void tcpm_unregister_altmodes(struct tcpm_port *port)
3662 struct pd_mode_data *modep = &port->mode_data;
3665 for (i = 0; i < modep->altmodes; i++) {
3666 typec_unregister_altmode(port->partner_altmode[i]);
3667 port->partner_altmode[i] = NULL;
3670 memset(modep, 0, sizeof(*modep));
3673 static void tcpm_set_partner_usb_comm_capable(struct tcpm_port *port, bool capable)
3675 tcpm_log(port, "Setting usb_comm capable %s", capable ? "true" : "false");
3677 if (port->tcpc->set_partner_usb_comm_capable)
3678 port->tcpc->set_partner_usb_comm_capable(port->tcpc, capable);
3681 static void tcpm_reset_port(struct tcpm_port *port)
3683 tcpm_enable_auto_vbus_discharge(port, false);
3684 port->in_ams = false;
3685 port->ams = NONE_AMS;
3686 port->vdm_sm_running = false;
3687 tcpm_unregister_altmodes(port);
3688 tcpm_typec_disconnect(port);
3689 port->attached = false;
3690 port->pd_capable = false;
3691 port->pps_data.supported = false;
3692 tcpm_set_partner_usb_comm_capable(port, false);
3695 * First Rx ID should be 0; set this to a sentinel of -1 so that
3696 * we can check tcpm_pd_rx_handler() if we had seen it before.
3698 port->rx_msgid = -1;
3700 port->tcpc->set_pd_rx(port->tcpc, false);
3701 tcpm_init_vbus(port); /* also disables charging */
3702 tcpm_init_vconn(port);
3703 tcpm_set_current_limit(port, 0, 0);
3704 tcpm_set_polarity(port, TYPEC_POLARITY_CC1);
3705 tcpm_mux_set(port, TYPEC_STATE_SAFE, USB_ROLE_NONE,
3706 TYPEC_ORIENTATION_NONE);
3707 tcpm_set_attached_state(port, false);
3708 port->try_src_count = 0;
3709 port->try_snk_count = 0;
3710 port->usb_type = POWER_SUPPLY_USB_TYPE_C;
3711 power_supply_changed(port->psy);
3712 port->nr_sink_caps = 0;
3713 port->sink_cap_done = false;
3714 if (port->tcpc->enable_frs)
3715 port->tcpc->enable_frs(port->tcpc, false);
3717 usb_power_delivery_unregister_capabilities(port->partner_sink_caps);
3718 port->partner_sink_caps = NULL;
3719 usb_power_delivery_unregister_capabilities(port->partner_source_caps);
3720 port->partner_source_caps = NULL;
3721 usb_power_delivery_unregister(port->partner_pd);
3722 port->partner_pd = NULL;
3725 static void tcpm_detach(struct tcpm_port *port)
3727 if (tcpm_port_is_disconnected(port))
3728 port->hard_reset_count = 0;
3730 port->try_src_count = 0;
3731 port->try_snk_count = 0;
3733 if (!port->attached)
3736 if (port->tcpc->set_bist_data) {
3737 tcpm_log(port, "disable BIST MODE TESTDATA");
3738 port->tcpc->set_bist_data(port->tcpc, false);
3741 tcpm_reset_port(port);
3744 static void tcpm_src_detach(struct tcpm_port *port)
3749 static int tcpm_snk_attach(struct tcpm_port *port)
3756 ret = tcpm_set_polarity(port, port->cc2 != TYPEC_CC_OPEN ?
3757 TYPEC_POLARITY_CC2 : TYPEC_POLARITY_CC1);
3761 tcpm_enable_auto_vbus_discharge(port, true);
3763 ret = tcpm_set_roles(port, true, TYPEC_SINK, tcpm_data_role_for_sink(port));
3767 port->pd_capable = false;
3769 port->partner = NULL;
3771 port->attached = true;
3772 port->send_discover = true;
3777 static void tcpm_snk_detach(struct tcpm_port *port)
3782 static int tcpm_acc_attach(struct tcpm_port *port)
3789 ret = tcpm_set_roles(port, true, TYPEC_SOURCE,
3790 tcpm_data_role_for_source(port));
3794 port->partner = NULL;
3796 tcpm_typec_connect(port);
3798 port->attached = true;
3803 static void tcpm_acc_detach(struct tcpm_port *port)
3808 static inline enum tcpm_state hard_reset_state(struct tcpm_port *port)
3810 if (port->hard_reset_count < PD_N_HARD_RESET_COUNT)
3811 return HARD_RESET_SEND;
3812 if (port->pd_capable)
3813 return ERROR_RECOVERY;
3814 if (port->pwr_role == TYPEC_SOURCE)
3815 return SRC_UNATTACHED;
3816 if (port->state == SNK_WAIT_CAPABILITIES)
3818 return SNK_UNATTACHED;
3821 static inline enum tcpm_state unattached_state(struct tcpm_port *port)
3823 if (port->port_type == TYPEC_PORT_DRP) {
3824 if (port->pwr_role == TYPEC_SOURCE)
3825 return SRC_UNATTACHED;
3827 return SNK_UNATTACHED;
3828 } else if (port->port_type == TYPEC_PORT_SRC) {
3829 return SRC_UNATTACHED;
3832 return SNK_UNATTACHED;
3835 static void tcpm_swap_complete(struct tcpm_port *port, int result)
3837 if (port->swap_pending) {
3838 port->swap_status = result;
3839 port->swap_pending = false;
3840 port->non_pd_role_swap = false;
3841 complete(&port->swap_complete);
3845 static enum typec_pwr_opmode tcpm_get_pwr_opmode(enum typec_cc_status cc)
3848 case TYPEC_CC_RP_1_5:
3849 return TYPEC_PWR_MODE_1_5A;
3850 case TYPEC_CC_RP_3_0:
3851 return TYPEC_PWR_MODE_3_0A;
3852 case TYPEC_CC_RP_DEF:
3854 return TYPEC_PWR_MODE_USB;
3858 static enum typec_cc_status tcpm_pwr_opmode_to_rp(enum typec_pwr_opmode opmode)
3861 case TYPEC_PWR_MODE_USB:
3862 return TYPEC_CC_RP_DEF;
3863 case TYPEC_PWR_MODE_1_5A:
3864 return TYPEC_CC_RP_1_5;
3865 case TYPEC_PWR_MODE_3_0A:
3866 case TYPEC_PWR_MODE_PD:
3868 return TYPEC_CC_RP_3_0;
3872 static void tcpm_set_initial_svdm_version(struct tcpm_port *port)
3874 switch (port->negotiated_rev) {
3878 * 6.4.4.2.3 Structured VDM Version
3879 * 2.0 states "At this time, there is only one version (1.0) defined.
3880 * This field Shall be set to zero to indicate Version 1.0."
3881 * 3.0 states "This field Shall be set to 01b to indicate Version 2.0."
3882 * To ensure that we follow the Power Delivery revision we are currently
3883 * operating on, downgrade the SVDM version to the highest one supported
3884 * by the Power Delivery revision.
3887 typec_partner_set_svdm_version(port->partner, SVDM_VER_1_0);
3890 typec_partner_set_svdm_version(port->partner, SVDM_VER_1_0);
3895 static void run_state_machine(struct tcpm_port *port)
3898 enum typec_pwr_opmode opmode;
3900 enum tcpm_state upcoming_state;
3902 if (port->tcpc->check_contaminant && port->state != CHECK_CONTAMINANT)
3903 port->potential_contaminant = ((port->enter_state == SRC_ATTACH_WAIT &&
3904 port->state == SRC_UNATTACHED) ||
3905 (port->enter_state == SNK_ATTACH_WAIT &&
3906 port->state == SNK_UNATTACHED) ||
3907 (port->enter_state == SNK_DEBOUNCED &&
3908 port->state == SNK_UNATTACHED));
3910 port->enter_state = port->state;
3911 switch (port->state) {
3914 case CHECK_CONTAMINANT:
3915 port->tcpc->check_contaminant(port->tcpc);
3918 case SRC_UNATTACHED:
3919 if (!port->non_pd_role_swap)
3920 tcpm_swap_complete(port, -ENOTCONN);
3921 tcpm_src_detach(port);
3922 if (port->potential_contaminant) {
3923 tcpm_set_state(port, CHECK_CONTAMINANT, 0);
3926 if (tcpm_start_toggling(port, tcpm_rp_cc(port))) {
3927 tcpm_set_state(port, TOGGLING, 0);
3930 tcpm_set_cc(port, tcpm_rp_cc(port));
3931 if (port->port_type == TYPEC_PORT_DRP)
3932 tcpm_set_state(port, SNK_UNATTACHED, PD_T_DRP_SNK);
3934 case SRC_ATTACH_WAIT:
3935 if (tcpm_port_is_debug(port))
3936 tcpm_set_state(port, DEBUG_ACC_ATTACHED,
3938 else if (tcpm_port_is_audio(port))
3939 tcpm_set_state(port, AUDIO_ACC_ATTACHED,
3941 else if (tcpm_port_is_source(port) && port->vbus_vsafe0v)
3942 tcpm_set_state(port,
3943 tcpm_try_snk(port) ? SNK_TRY
3949 port->try_snk_count++;
3952 * - Do not drive vconn or vbus
3953 * - Terminate CC pins (both) to Rd
3955 * - Wait for tDRPTry (PD_T_DRP_TRY).
3956 * Until then, ignore any state changes.
3958 tcpm_set_cc(port, TYPEC_CC_RD);
3959 tcpm_set_state(port, SNK_TRY_WAIT, PD_T_DRP_TRY);
3962 if (tcpm_port_is_sink(port)) {
3963 tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE, 0);
3965 tcpm_set_state(port, SRC_TRYWAIT, 0);
3969 case SNK_TRY_WAIT_DEBOUNCE:
3970 tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS,
3971 PD_T_TRY_CC_DEBOUNCE);
3973 case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS:
3974 if (port->vbus_present && tcpm_port_is_sink(port))
3975 tcpm_set_state(port, SNK_ATTACHED, 0);
3980 tcpm_set_cc(port, tcpm_rp_cc(port));
3981 if (port->max_wait == 0) {
3982 port->max_wait = jiffies +
3983 msecs_to_jiffies(PD_T_DRP_TRY);
3984 tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED,
3987 if (time_is_after_jiffies(port->max_wait))
3988 tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED,
3989 jiffies_to_msecs(port->max_wait -
3992 tcpm_set_state(port, SNK_UNATTACHED, 0);
3995 case SRC_TRYWAIT_DEBOUNCE:
3996 tcpm_set_state(port, SRC_ATTACHED, PD_T_CC_DEBOUNCE);
3998 case SRC_TRYWAIT_UNATTACHED:
3999 tcpm_set_state(port, SNK_UNATTACHED, 0);
4003 ret = tcpm_src_attach(port);
4004 tcpm_set_state(port, SRC_UNATTACHED,
4005 ret < 0 ? 0 : PD_T_PS_SOURCE_ON);
4008 opmode = tcpm_get_pwr_opmode(tcpm_rp_cc(port));
4009 typec_set_pwr_opmode(port->typec_port, opmode);
4010 port->pwr_opmode = TYPEC_PWR_MODE_USB;
4011 port->caps_count = 0;
4012 port->negotiated_rev = PD_MAX_REV;
4013 port->message_id = 0;
4014 port->rx_msgid = -1;
4015 port->explicit_contract = false;
4016 /* SNK -> SRC POWER/FAST_ROLE_SWAP finished */
4017 if (port->ams == POWER_ROLE_SWAP ||
4018 port->ams == FAST_ROLE_SWAP)
4019 tcpm_ams_finish(port);
4020 if (!port->pd_supported) {
4021 tcpm_set_state(port, SRC_READY, 0);
4024 port->upcoming_state = SRC_SEND_CAPABILITIES;
4025 tcpm_ams_start(port, POWER_NEGOTIATION);
4027 case SRC_SEND_CAPABILITIES:
4029 if (port->caps_count > PD_N_CAPS_COUNT) {
4030 tcpm_set_state(port, SRC_READY, 0);
4033 ret = tcpm_pd_send_source_caps(port);
4035 tcpm_set_state(port, SRC_SEND_CAPABILITIES,
4036 PD_T_SEND_SOURCE_CAP);
4039 * Per standard, we should clear the reset counter here.
4040 * However, that can result in state machine hang-ups.
4041 * Reset it only in READY state to improve stability.
4043 /* port->hard_reset_count = 0; */
4044 port->caps_count = 0;
4045 port->pd_capable = true;
4046 tcpm_set_state_cond(port, SRC_SEND_CAPABILITIES_TIMEOUT,
4047 PD_T_SEND_SOURCE_CAP);
4050 case SRC_SEND_CAPABILITIES_TIMEOUT:
4052 * Error recovery for a PD_DATA_SOURCE_CAP reply timeout.
4054 * PD 2.0 sinks are supposed to accept src-capabilities with a
4055 * 3.0 header and simply ignore any src PDOs which the sink does
4056 * not understand such as PPS but some 2.0 sinks instead ignore
4057 * the entire PD_DATA_SOURCE_CAP message, causing contract
4058 * negotiation to fail.
4060 * After PD_N_HARD_RESET_COUNT hard-reset attempts, we try
4061 * sending src-capabilities with a lower PD revision to
4062 * make these broken sinks work.
4064 if (port->hard_reset_count < PD_N_HARD_RESET_COUNT) {
4065 tcpm_set_state(port, HARD_RESET_SEND, 0);
4066 } else if (port->negotiated_rev > PD_REV20) {
4067 port->negotiated_rev--;
4068 port->hard_reset_count = 0;
4069 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
4071 tcpm_set_state(port, hard_reset_state(port), 0);
4074 case SRC_NEGOTIATE_CAPABILITIES:
4075 ret = tcpm_pd_check_request(port);
4077 tcpm_pd_send_control(port, PD_CTRL_REJECT);
4078 if (!port->explicit_contract) {
4079 tcpm_set_state(port,
4080 SRC_WAIT_NEW_CAPABILITIES, 0);
4082 tcpm_set_state(port, SRC_READY, 0);
4085 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
4086 tcpm_set_partner_usb_comm_capable(port,
4087 !!(port->sink_request & RDO_USB_COMM));
4088 tcpm_set_state(port, SRC_TRANSITION_SUPPLY,
4089 PD_T_SRC_TRANSITION);
4092 case SRC_TRANSITION_SUPPLY:
4093 /* XXX: regulator_set_voltage(vbus, ...) */
4094 tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
4095 port->explicit_contract = true;
4096 typec_set_pwr_opmode(port->typec_port, TYPEC_PWR_MODE_PD);
4097 port->pwr_opmode = TYPEC_PWR_MODE_PD;
4098 tcpm_set_state_cond(port, SRC_READY, 0);
4102 port->hard_reset_count = 0;
4104 port->try_src_count = 0;
4106 tcpm_swap_complete(port, 0);
4107 tcpm_typec_connect(port);
4109 if (port->ams != NONE_AMS)
4110 tcpm_ams_finish(port);
4111 if (port->next_ams != NONE_AMS) {
4112 port->ams = port->next_ams;
4113 port->next_ams = NONE_AMS;
4117 * If previous AMS is interrupted, switch to the upcoming
4120 if (port->upcoming_state != INVALID_STATE) {
4121 upcoming_state = port->upcoming_state;
4122 port->upcoming_state = INVALID_STATE;
4123 tcpm_set_state(port, upcoming_state, 0);
4128 * 6.4.4.3.1 Discover Identity
4129 * "The Discover Identity Command Shall only be sent to SOP when there is an
4130 * Explicit Contract."
4131 * For now, this driver only supports SOP for DISCOVER_IDENTITY, thus using
4132 * port->explicit_contract to decide whether to send the command.
4134 if (port->explicit_contract) {
4135 tcpm_set_initial_svdm_version(port);
4136 mod_send_discover_delayed_work(port, 0);
4138 port->send_discover = false;
4143 * Sending ping messages is not necessary if
4144 * - the source operates at vSafe5V
4146 * - The system is not operating in PD mode
4148 * - Both partners are connected using a Type-C connector
4150 * There is no actual need to send PD messages since the local
4151 * port type-c and the spec does not clearly say whether PD is
4152 * possible when type-c is connected to Type-A/B
4155 case SRC_WAIT_NEW_CAPABILITIES:
4156 /* Nothing to do... */
4160 case SNK_UNATTACHED:
4161 if (!port->non_pd_role_swap)
4162 tcpm_swap_complete(port, -ENOTCONN);
4163 tcpm_pps_complete(port, -ENOTCONN);
4164 tcpm_snk_detach(port);
4165 if (port->potential_contaminant) {
4166 tcpm_set_state(port, CHECK_CONTAMINANT, 0);
4169 if (tcpm_start_toggling(port, TYPEC_CC_RD)) {
4170 tcpm_set_state(port, TOGGLING, 0);
4173 tcpm_set_cc(port, TYPEC_CC_RD);
4174 if (port->port_type == TYPEC_PORT_DRP)
4175 tcpm_set_state(port, SRC_UNATTACHED, PD_T_DRP_SRC);
4177 case SNK_ATTACH_WAIT:
4178 if ((port->cc1 == TYPEC_CC_OPEN &&
4179 port->cc2 != TYPEC_CC_OPEN) ||
4180 (port->cc1 != TYPEC_CC_OPEN &&
4181 port->cc2 == TYPEC_CC_OPEN))
4182 tcpm_set_state(port, SNK_DEBOUNCED,
4184 else if (tcpm_port_is_disconnected(port))
4185 tcpm_set_state(port, SNK_UNATTACHED,
4189 if (tcpm_port_is_disconnected(port))
4190 tcpm_set_state(port, SNK_UNATTACHED,
4192 else if (port->vbus_present)
4193 tcpm_set_state(port,
4194 tcpm_try_src(port) ? SRC_TRY
4199 port->try_src_count++;
4200 tcpm_set_cc(port, tcpm_rp_cc(port));
4202 tcpm_set_state(port, SRC_TRY_WAIT, 0);
4205 if (port->max_wait == 0) {
4206 port->max_wait = jiffies +
4207 msecs_to_jiffies(PD_T_DRP_TRY);
4208 msecs = PD_T_DRP_TRY;
4210 if (time_is_after_jiffies(port->max_wait))
4211 msecs = jiffies_to_msecs(port->max_wait -
4216 tcpm_set_state(port, SNK_TRYWAIT, msecs);
4218 case SRC_TRY_DEBOUNCE:
4219 tcpm_set_state(port, SRC_ATTACHED, PD_T_PD_DEBOUNCE);
4222 tcpm_set_cc(port, TYPEC_CC_RD);
4223 tcpm_set_state(port, SNK_TRYWAIT_VBUS, PD_T_CC_DEBOUNCE);
4225 case SNK_TRYWAIT_VBUS:
4227 * TCPM stays in this state indefinitely until VBUS
4228 * is detected as long as Rp is not detected for
4229 * more than a time period of tPDDebounce.
4231 if (port->vbus_present && tcpm_port_is_sink(port)) {
4232 tcpm_set_state(port, SNK_ATTACHED, 0);
4235 if (!tcpm_port_is_sink(port))
4236 tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, 0);
4238 case SNK_TRYWAIT_DEBOUNCE:
4239 tcpm_set_state(port, SNK_UNATTACHED, PD_T_PD_DEBOUNCE);
4242 ret = tcpm_snk_attach(port);
4244 tcpm_set_state(port, SNK_UNATTACHED, 0);
4246 tcpm_set_state(port, SNK_STARTUP, 0);
4249 opmode = tcpm_get_pwr_opmode(port->polarity ?
4250 port->cc2 : port->cc1);
4251 typec_set_pwr_opmode(port->typec_port, opmode);
4252 port->pwr_opmode = TYPEC_PWR_MODE_USB;
4253 port->negotiated_rev = PD_MAX_REV;
4254 port->message_id = 0;
4255 port->rx_msgid = -1;
4256 port->explicit_contract = false;
4258 if (port->ams == POWER_ROLE_SWAP ||
4259 port->ams == FAST_ROLE_SWAP)
4260 /* SRC -> SNK POWER/FAST_ROLE_SWAP finished */
4261 tcpm_ams_finish(port);
4263 tcpm_set_state(port, SNK_DISCOVERY, 0);
4266 if (port->vbus_present) {
4267 u32 current_lim = tcpm_get_current_limit(port);
4269 if (port->slow_charger_loop && (current_lim > PD_P_SNK_STDBY_MW / 5))
4270 current_lim = PD_P_SNK_STDBY_MW / 5;
4271 tcpm_set_current_limit(port, current_lim, 5000);
4272 /* Not sink vbus if operational current is 0mA */
4273 tcpm_set_charge(port, !!pdo_max_current(port->snk_pdo[0]));
4275 if (!port->pd_supported)
4276 tcpm_set_state(port, SNK_READY, 0);
4278 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
4282 * For DRP, timeouts differ. Also, handling is supposed to be
4283 * different and much more complex (dead battery detection;
4284 * see USB power delivery specification, section 8.3.3.6.1.5.1).
4286 tcpm_set_state(port, hard_reset_state(port),
4287 port->port_type == TYPEC_PORT_DRP ?
4288 PD_T_DB_DETECT : PD_T_NO_RESPONSE);
4290 case SNK_DISCOVERY_DEBOUNCE:
4291 tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE_DONE,
4294 case SNK_DISCOVERY_DEBOUNCE_DONE:
4295 if (!tcpm_port_is_disconnected(port) &&
4296 tcpm_port_is_sink(port) &&
4297 ktime_after(port->delayed_runtime, ktime_get())) {
4298 tcpm_set_state(port, SNK_DISCOVERY,
4299 ktime_to_ms(ktime_sub(port->delayed_runtime, ktime_get())));
4302 tcpm_set_state(port, unattached_state(port), 0);
4304 case SNK_WAIT_CAPABILITIES:
4305 ret = port->tcpc->set_pd_rx(port->tcpc, true);
4307 tcpm_set_state(port, SNK_READY, 0);
4311 * If VBUS has never been low, and we time out waiting
4312 * for source cap, try a soft reset first, in case we
4313 * were already in a stable contract before this boot.
4314 * Do this only once.
4316 if (port->vbus_never_low) {
4317 port->vbus_never_low = false;
4318 tcpm_set_state(port, SNK_SOFT_RESET,
4319 PD_T_SINK_WAIT_CAP);
4321 tcpm_set_state(port, hard_reset_state(port),
4322 PD_T_SINK_WAIT_CAP);
4325 case SNK_NEGOTIATE_CAPABILITIES:
4326 port->pd_capable = true;
4327 tcpm_set_partner_usb_comm_capable(port,
4328 !!(port->source_caps[0] & PDO_FIXED_USB_COMM));
4329 port->hard_reset_count = 0;
4330 ret = tcpm_pd_send_request(port);
4332 /* Restore back to the original state */
4333 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
4334 port->pps_data.active,
4335 port->supply_voltage);
4336 /* Let the Source send capabilities again. */
4337 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
4339 tcpm_set_state_cond(port, hard_reset_state(port),
4340 PD_T_SENDER_RESPONSE);
4343 case SNK_NEGOTIATE_PPS_CAPABILITIES:
4344 ret = tcpm_pd_send_pps_request(port);
4346 /* Restore back to the original state */
4347 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
4348 port->pps_data.active,
4349 port->supply_voltage);
4350 port->pps_status = ret;
4352 * If this was called due to updates to sink
4353 * capabilities, and pps is no longer valid, we should
4354 * safely fall back to a standard PDO.
4356 if (port->update_sink_caps)
4357 tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
4359 tcpm_set_state(port, SNK_READY, 0);
4361 tcpm_set_state_cond(port, hard_reset_state(port),
4362 PD_T_SENDER_RESPONSE);
4365 case SNK_TRANSITION_SINK:
4366 /* From the USB PD spec:
4367 * "The Sink Shall transition to Sink Standby before a positive or
4368 * negative voltage transition of VBUS. During Sink Standby
4369 * the Sink Shall reduce its power draw to pSnkStdby."
4371 * This is not applicable to PPS though as the port can continue
4372 * to draw negotiated power without switching to standby.
4374 if (port->supply_voltage != port->req_supply_voltage && !port->pps_data.active &&
4375 port->current_limit * port->supply_voltage / 1000 > PD_P_SNK_STDBY_MW) {
4376 u32 stdby_ma = PD_P_SNK_STDBY_MW * 1000 / port->supply_voltage;
4378 tcpm_log(port, "Setting standby current %u mV @ %u mA",
4379 port->supply_voltage, stdby_ma);
4380 tcpm_set_current_limit(port, stdby_ma, port->supply_voltage);
4383 case SNK_TRANSITION_SINK_VBUS:
4384 tcpm_set_state(port, hard_reset_state(port),
4385 PD_T_PS_TRANSITION);
4388 port->try_snk_count = 0;
4389 port->update_sink_caps = false;
4390 if (port->explicit_contract) {
4391 typec_set_pwr_opmode(port->typec_port,
4393 port->pwr_opmode = TYPEC_PWR_MODE_PD;
4396 if (!port->pd_capable && port->slow_charger_loop)
4397 tcpm_set_current_limit(port, tcpm_get_current_limit(port), 5000);
4398 tcpm_swap_complete(port, 0);
4399 tcpm_typec_connect(port);
4400 mod_enable_frs_delayed_work(port, 0);
4401 tcpm_pps_complete(port, port->pps_status);
4403 if (port->ams != NONE_AMS)
4404 tcpm_ams_finish(port);
4405 if (port->next_ams != NONE_AMS) {
4406 port->ams = port->next_ams;
4407 port->next_ams = NONE_AMS;
4411 * If previous AMS is interrupted, switch to the upcoming
4414 if (port->upcoming_state != INVALID_STATE) {
4415 upcoming_state = port->upcoming_state;
4416 port->upcoming_state = INVALID_STATE;
4417 tcpm_set_state(port, upcoming_state, 0);
4422 * 6.4.4.3.1 Discover Identity
4423 * "The Discover Identity Command Shall only be sent to SOP when there is an
4424 * Explicit Contract."
4425 * For now, this driver only supports SOP for DISCOVER_IDENTITY, thus using
4426 * port->explicit_contract.
4428 if (port->explicit_contract) {
4429 tcpm_set_initial_svdm_version(port);
4430 mod_send_discover_delayed_work(port, 0);
4432 port->send_discover = false;
4435 power_supply_changed(port->psy);
4438 /* Accessory states */
4439 case ACC_UNATTACHED:
4440 tcpm_acc_detach(port);
4441 tcpm_set_state(port, SRC_UNATTACHED, 0);
4443 case DEBUG_ACC_ATTACHED:
4444 case AUDIO_ACC_ATTACHED:
4445 ret = tcpm_acc_attach(port);
4447 tcpm_set_state(port, ACC_UNATTACHED, 0);
4449 case AUDIO_ACC_DEBOUNCE:
4450 tcpm_set_state(port, ACC_UNATTACHED, PD_T_CC_DEBOUNCE);
4453 /* Hard_Reset states */
4454 case HARD_RESET_SEND:
4455 if (port->ams != NONE_AMS)
4456 tcpm_ams_finish(port);
4458 * State machine will be directed to HARD_RESET_START,
4459 * thus set upcoming_state to INVALID_STATE.
4461 port->upcoming_state = INVALID_STATE;
4462 tcpm_ams_start(port, HARD_RESET);
4464 case HARD_RESET_START:
4465 port->sink_cap_done = false;
4466 if (port->tcpc->enable_frs)
4467 port->tcpc->enable_frs(port->tcpc, false);
4468 port->hard_reset_count++;
4469 port->tcpc->set_pd_rx(port->tcpc, false);
4470 tcpm_unregister_altmodes(port);
4471 port->nr_sink_caps = 0;
4472 port->send_discover = true;
4473 if (port->pwr_role == TYPEC_SOURCE)
4474 tcpm_set_state(port, SRC_HARD_RESET_VBUS_OFF,
4475 PD_T_PS_HARD_RESET);
4477 tcpm_set_state(port, SNK_HARD_RESET_SINK_OFF, 0);
4479 case SRC_HARD_RESET_VBUS_OFF:
4481 * 7.1.5 Response to Hard Resets
4482 * Hard Reset Signaling indicates a communication failure has occurred and the
4483 * Source Shall stop driving VCONN, Shall remove Rp from the VCONN pin and Shall
4484 * drive VBUS to vSafe0V as shown in Figure 7-9.
4486 tcpm_set_vconn(port, false);
4487 tcpm_set_vbus(port, false);
4488 tcpm_set_roles(port, port->self_powered, TYPEC_SOURCE,
4489 tcpm_data_role_for_source(port));
4491 * If tcpc fails to notify vbus off, TCPM will wait for PD_T_SAFE_0V +
4492 * PD_T_SRC_RECOVER before turning vbus back on.
4493 * From Table 7-12 Sequence Description for a Source Initiated Hard Reset:
4494 * 4. Policy Engine waits tPSHardReset after sending Hard Reset Signaling and then
4495 * tells the Device Policy Manager to instruct the power supply to perform a
4496 * Hard Reset. The transition to vSafe0V Shall occur within tSafe0V (t2).
4497 * 5. After tSrcRecover the Source applies power to VBUS in an attempt to
4498 * re-establish communication with the Sink and resume USB Default Operation.
4499 * The transition to vSafe5V Shall occur within tSrcTurnOn(t4).
4501 tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SAFE_0V + PD_T_SRC_RECOVER);
4503 case SRC_HARD_RESET_VBUS_ON:
4504 tcpm_set_vconn(port, true);
4505 tcpm_set_vbus(port, true);
4506 if (port->ams == HARD_RESET)
4507 tcpm_ams_finish(port);
4508 if (port->pd_supported)
4509 port->tcpc->set_pd_rx(port->tcpc, true);
4510 tcpm_set_attached_state(port, true);
4511 tcpm_set_state(port, SRC_UNATTACHED, PD_T_PS_SOURCE_ON);
4513 case SNK_HARD_RESET_SINK_OFF:
4514 /* Do not discharge/disconnect during hard reseet */
4515 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0);
4516 memset(&port->pps_data, 0, sizeof(port->pps_data));
4517 tcpm_set_vconn(port, false);
4518 if (port->pd_capable)
4519 tcpm_set_charge(port, false);
4520 tcpm_set_roles(port, port->self_powered, TYPEC_SINK,
4521 tcpm_data_role_for_sink(port));
4523 * VBUS may or may not toggle, depending on the adapter.
4524 * If it doesn't toggle, transition to SNK_HARD_RESET_SINK_ON
4525 * directly after timeout.
4527 tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, PD_T_SAFE_0V);
4529 case SNK_HARD_RESET_WAIT_VBUS:
4530 if (port->ams == HARD_RESET)
4531 tcpm_ams_finish(port);
4532 /* Assume we're disconnected if VBUS doesn't come back. */
4533 tcpm_set_state(port, SNK_UNATTACHED,
4534 PD_T_SRC_RECOVER_MAX + PD_T_SRC_TURN_ON);
4536 case SNK_HARD_RESET_SINK_ON:
4537 /* Note: There is no guarantee that VBUS is on in this state */
4540 * The specification suggests that dual mode ports in sink
4541 * mode should transition to state PE_SRC_Transition_to_default.
4542 * See USB power delivery specification chapter 8.3.3.6.1.3.
4543 * This would mean to
4544 * - turn off VCONN, reset power supply
4545 * - request hardware reset
4547 * - Transition to state PE_Src_Startup
4548 * SNK only ports shall transition to state Snk_Startup
4549 * (see chapter 8.3.3.3.8).
4550 * Similar, dual-mode ports in source mode should transition
4551 * to PE_SNK_Transition_to_default.
4553 if (port->pd_capable) {
4554 tcpm_set_current_limit(port,
4555 tcpm_get_current_limit(port),
4557 /* Not sink vbus if operational current is 0mA */
4558 tcpm_set_charge(port, !!pdo_max_current(port->snk_pdo[0]));
4560 if (port->ams == HARD_RESET)
4561 tcpm_ams_finish(port);
4562 tcpm_set_attached_state(port, true);
4563 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, VSAFE5V);
4564 tcpm_set_state(port, SNK_STARTUP, 0);
4567 /* Soft_Reset states */
4569 port->message_id = 0;
4570 port->rx_msgid = -1;
4571 /* remove existing capabilities */
4572 usb_power_delivery_unregister_capabilities(port->partner_source_caps);
4573 port->partner_source_caps = NULL;
4574 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
4575 tcpm_ams_finish(port);
4576 if (port->pwr_role == TYPEC_SOURCE) {
4577 port->upcoming_state = SRC_SEND_CAPABILITIES;
4578 tcpm_ams_start(port, POWER_NEGOTIATION);
4580 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
4583 case SRC_SOFT_RESET_WAIT_SNK_TX:
4584 case SNK_SOFT_RESET:
4585 if (port->ams != NONE_AMS)
4586 tcpm_ams_finish(port);
4587 port->upcoming_state = SOFT_RESET_SEND;
4588 tcpm_ams_start(port, SOFT_RESET_AMS);
4590 case SOFT_RESET_SEND:
4591 port->message_id = 0;
4592 port->rx_msgid = -1;
4593 /* remove existing capabilities */
4594 usb_power_delivery_unregister_capabilities(port->partner_source_caps);
4595 port->partner_source_caps = NULL;
4596 if (tcpm_pd_send_control(port, PD_CTRL_SOFT_RESET))
4597 tcpm_set_state_cond(port, hard_reset_state(port), 0);
4599 tcpm_set_state_cond(port, hard_reset_state(port),
4600 PD_T_SENDER_RESPONSE);
4603 /* DR_Swap states */
4605 tcpm_pd_send_control(port, PD_CTRL_DR_SWAP);
4606 if (port->data_role == TYPEC_DEVICE || port->negotiated_rev > PD_REV20)
4607 port->send_discover = true;
4608 tcpm_set_state_cond(port, DR_SWAP_SEND_TIMEOUT,
4609 PD_T_SENDER_RESPONSE);
4611 case DR_SWAP_ACCEPT:
4612 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
4613 if (port->data_role == TYPEC_DEVICE || port->negotiated_rev > PD_REV20)
4614 port->send_discover = true;
4615 tcpm_set_state_cond(port, DR_SWAP_CHANGE_DR, 0);
4617 case DR_SWAP_SEND_TIMEOUT:
4618 tcpm_swap_complete(port, -ETIMEDOUT);
4619 port->send_discover = false;
4620 tcpm_ams_finish(port);
4621 tcpm_set_state(port, ready_state(port), 0);
4623 case DR_SWAP_CHANGE_DR:
4624 tcpm_unregister_altmodes(port);
4625 if (port->data_role == TYPEC_HOST)
4626 tcpm_set_roles(port, true, port->pwr_role,
4629 tcpm_set_roles(port, true, port->pwr_role,
4631 tcpm_ams_finish(port);
4632 tcpm_set_state(port, ready_state(port), 0);
4636 if (tcpm_pd_send_control(port, PD_CTRL_FR_SWAP)) {
4637 tcpm_set_state(port, ERROR_RECOVERY, 0);
4640 tcpm_set_state_cond(port, FR_SWAP_SEND_TIMEOUT, PD_T_SENDER_RESPONSE);
4642 case FR_SWAP_SEND_TIMEOUT:
4643 tcpm_set_state(port, ERROR_RECOVERY, 0);
4645 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
4646 tcpm_set_state(port, ERROR_RECOVERY, PD_T_PS_SOURCE_OFF);
4648 case FR_SWAP_SNK_SRC_NEW_SINK_READY:
4649 if (port->vbus_source)
4650 tcpm_set_state(port, FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED, 0);
4652 tcpm_set_state(port, ERROR_RECOVERY, PD_T_RECEIVER_RESPONSE);
4654 case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED:
4655 tcpm_set_pwr_role(port, TYPEC_SOURCE);
4656 if (tcpm_pd_send_control(port, PD_CTRL_PS_RDY)) {
4657 tcpm_set_state(port, ERROR_RECOVERY, 0);
4660 tcpm_set_cc(port, tcpm_rp_cc(port));
4661 tcpm_set_state(port, SRC_STARTUP, PD_T_SWAP_SRC_START);
4664 /* PR_Swap states */
4665 case PR_SWAP_ACCEPT:
4666 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
4667 tcpm_set_state(port, PR_SWAP_START, 0);
4670 tcpm_pd_send_control(port, PD_CTRL_PR_SWAP);
4671 tcpm_set_state_cond(port, PR_SWAP_SEND_TIMEOUT,
4672 PD_T_SENDER_RESPONSE);
4674 case PR_SWAP_SEND_TIMEOUT:
4675 tcpm_swap_complete(port, -ETIMEDOUT);
4676 tcpm_set_state(port, ready_state(port), 0);
4679 tcpm_apply_rc(port);
4680 if (port->pwr_role == TYPEC_SOURCE)
4681 tcpm_set_state(port, PR_SWAP_SRC_SNK_TRANSITION_OFF,
4682 PD_T_SRC_TRANSITION);
4684 tcpm_set_state(port, PR_SWAP_SNK_SRC_SINK_OFF, 0);
4686 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
4688 * Prevent vbus discharge circuit from turning on during PR_SWAP
4689 * as this is not a disconnect.
4691 tcpm_set_vbus(port, false);
4692 port->explicit_contract = false;
4693 /* allow time for Vbus discharge, must be < tSrcSwapStdby */
4694 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF,
4697 case PR_SWAP_SRC_SNK_SOURCE_OFF:
4698 tcpm_set_cc(port, TYPEC_CC_RD);
4699 /* allow CC debounce */
4700 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED,
4703 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED:
4705 * USB-PD standard, 6.2.1.4, Port Power Role:
4706 * "During the Power Role Swap Sequence, for the initial Source
4707 * Port, the Port Power Role field shall be set to Sink in the
4708 * PS_RDY Message indicating that the initial Source’s power
4709 * supply is turned off"
4711 tcpm_set_pwr_role(port, TYPEC_SINK);
4712 if (tcpm_pd_send_control(port, PD_CTRL_PS_RDY)) {
4713 tcpm_set_state(port, ERROR_RECOVERY, 0);
4716 tcpm_set_state(port, ERROR_RECOVERY, PD_T_PS_SOURCE_ON_PRS);
4718 case PR_SWAP_SRC_SNK_SINK_ON:
4719 tcpm_enable_auto_vbus_discharge(port, true);
4720 /* Set the vbus disconnect threshold for implicit contract */
4721 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, VSAFE5V);
4722 tcpm_set_state(port, SNK_STARTUP, 0);
4724 case PR_SWAP_SNK_SRC_SINK_OFF:
4725 /* will be source, remove existing capabilities */
4726 usb_power_delivery_unregister_capabilities(port->partner_source_caps);
4727 port->partner_source_caps = NULL;
4729 * Prevent vbus discharge circuit from turning on during PR_SWAP
4730 * as this is not a disconnect.
4732 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB,
4733 port->pps_data.active, 0);
4734 tcpm_set_charge(port, false);
4735 tcpm_set_state(port, hard_reset_state(port),
4736 PD_T_PS_SOURCE_OFF);
4738 case PR_SWAP_SNK_SRC_SOURCE_ON:
4739 tcpm_enable_auto_vbus_discharge(port, true);
4740 tcpm_set_cc(port, tcpm_rp_cc(port));
4741 tcpm_set_vbus(port, true);
4743 * allow time VBUS ramp-up, must be < tNewSrc
4744 * Also, this window overlaps with CC debounce as well.
4745 * So, Wait for the max of two which is PD_T_NEWSRC
4747 tcpm_set_state(port, PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP,
4750 case PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP:
4752 * USB PD standard, 6.2.1.4:
4753 * "Subsequent Messages initiated by the Policy Engine,
4754 * such as the PS_RDY Message sent to indicate that Vbus
4755 * is ready, will have the Port Power Role field set to
4758 tcpm_set_pwr_role(port, TYPEC_SOURCE);
4759 tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
4760 tcpm_set_state(port, SRC_STARTUP, PD_T_SWAP_SRC_START);
4763 case VCONN_SWAP_ACCEPT:
4764 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
4765 tcpm_ams_finish(port);
4766 tcpm_set_state(port, VCONN_SWAP_START, 0);
4768 case VCONN_SWAP_SEND:
4769 tcpm_pd_send_control(port, PD_CTRL_VCONN_SWAP);
4770 tcpm_set_state(port, VCONN_SWAP_SEND_TIMEOUT,
4771 PD_T_SENDER_RESPONSE);
4773 case VCONN_SWAP_SEND_TIMEOUT:
4774 tcpm_swap_complete(port, -ETIMEDOUT);
4775 tcpm_set_state(port, ready_state(port), 0);
4777 case VCONN_SWAP_START:
4778 if (port->vconn_role == TYPEC_SOURCE)
4779 tcpm_set_state(port, VCONN_SWAP_WAIT_FOR_VCONN, 0);
4781 tcpm_set_state(port, VCONN_SWAP_TURN_ON_VCONN, 0);
4783 case VCONN_SWAP_WAIT_FOR_VCONN:
4784 tcpm_set_state(port, hard_reset_state(port),
4785 PD_T_VCONN_SOURCE_ON);
4787 case VCONN_SWAP_TURN_ON_VCONN:
4788 tcpm_set_vconn(port, true);
4789 tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
4790 tcpm_set_state(port, ready_state(port), 0);
4792 case VCONN_SWAP_TURN_OFF_VCONN:
4793 tcpm_set_vconn(port, false);
4794 tcpm_set_state(port, ready_state(port), 0);
4797 case DR_SWAP_CANCEL:
4798 case PR_SWAP_CANCEL:
4799 case VCONN_SWAP_CANCEL:
4800 tcpm_swap_complete(port, port->swap_status);
4801 if (port->pwr_role == TYPEC_SOURCE)
4802 tcpm_set_state(port, SRC_READY, 0);
4804 tcpm_set_state(port, SNK_READY, 0);
4806 case FR_SWAP_CANCEL:
4807 if (port->pwr_role == TYPEC_SOURCE)
4808 tcpm_set_state(port, SRC_READY, 0);
4810 tcpm_set_state(port, SNK_READY, 0);
4814 switch (BDO_MODE_MASK(port->bist_request)) {
4815 case BDO_MODE_CARRIER2:
4816 tcpm_pd_transmit(port, TCPC_TX_BIST_MODE_2, NULL);
4817 tcpm_set_state(port, unattached_state(port),
4818 PD_T_BIST_CONT_MODE);
4820 case BDO_MODE_TESTDATA:
4821 if (port->tcpc->set_bist_data) {
4822 tcpm_log(port, "Enable BIST MODE TESTDATA");
4823 port->tcpc->set_bist_data(port->tcpc, true);
4830 case GET_STATUS_SEND:
4831 tcpm_pd_send_control(port, PD_CTRL_GET_STATUS);
4832 tcpm_set_state(port, GET_STATUS_SEND_TIMEOUT,
4833 PD_T_SENDER_RESPONSE);
4835 case GET_STATUS_SEND_TIMEOUT:
4836 tcpm_set_state(port, ready_state(port), 0);
4838 case GET_PPS_STATUS_SEND:
4839 tcpm_pd_send_control(port, PD_CTRL_GET_PPS_STATUS);
4840 tcpm_set_state(port, GET_PPS_STATUS_SEND_TIMEOUT,
4841 PD_T_SENDER_RESPONSE);
4843 case GET_PPS_STATUS_SEND_TIMEOUT:
4844 tcpm_set_state(port, ready_state(port), 0);
4847 tcpm_pd_send_control(port, PD_CTRL_GET_SINK_CAP);
4848 tcpm_set_state(port, GET_SINK_CAP_TIMEOUT, PD_T_SENDER_RESPONSE);
4850 case GET_SINK_CAP_TIMEOUT:
4851 port->sink_cap_done = true;
4852 tcpm_set_state(port, ready_state(port), 0);
4854 case ERROR_RECOVERY:
4855 tcpm_swap_complete(port, -EPROTO);
4856 tcpm_pps_complete(port, -EPROTO);
4857 tcpm_set_state(port, PORT_RESET, 0);
4860 tcpm_reset_port(port);
4861 tcpm_set_cc(port, tcpm_default_state(port) == SNK_UNATTACHED ?
4862 TYPEC_CC_RD : tcpm_rp_cc(port));
4863 tcpm_set_state(port, PORT_RESET_WAIT_OFF,
4864 PD_T_ERROR_RECOVERY);
4866 case PORT_RESET_WAIT_OFF:
4867 tcpm_set_state(port,
4868 tcpm_default_state(port),
4869 port->vbus_present ? PD_T_PS_SOURCE_OFF : 0);
4872 /* AMS intermediate state */
4874 if (port->upcoming_state == INVALID_STATE) {
4875 tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ?
4876 SRC_READY : SNK_READY, 0);
4880 upcoming_state = port->upcoming_state;
4881 port->upcoming_state = INVALID_STATE;
4882 tcpm_set_state(port, upcoming_state, 0);
4886 case CHUNK_NOT_SUPP:
4887 tcpm_pd_send_control(port, PD_CTRL_NOT_SUPP);
4888 tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ? SRC_READY : SNK_READY, 0);
4891 WARN(1, "Unexpected port state %d\n", port->state);
4896 static void tcpm_state_machine_work(struct kthread_work *work)
4898 struct tcpm_port *port = container_of(work, struct tcpm_port, state_machine);
4899 enum tcpm_state prev_state;
4901 mutex_lock(&port->lock);
4902 port->state_machine_running = true;
4904 if (port->queued_message && tcpm_send_queued_message(port))
4907 /* If we were queued due to a delayed state change, update it now */
4908 if (port->delayed_state) {
4909 tcpm_log(port, "state change %s -> %s [delayed %ld ms]",
4910 tcpm_states[port->state],
4911 tcpm_states[port->delayed_state], port->delay_ms);
4912 port->prev_state = port->state;
4913 port->state = port->delayed_state;
4914 port->delayed_state = INVALID_STATE;
4918 * Continue running as long as we have (non-delayed) state changes
4922 prev_state = port->state;
4923 run_state_machine(port);
4924 if (port->queued_message)
4925 tcpm_send_queued_message(port);
4926 } while (port->state != prev_state && !port->delayed_state);
4929 port->state_machine_running = false;
4930 mutex_unlock(&port->lock);
4933 static void _tcpm_cc_change(struct tcpm_port *port, enum typec_cc_status cc1,
4934 enum typec_cc_status cc2)
4936 enum typec_cc_status old_cc1, old_cc2;
4937 enum tcpm_state new_state;
4939 old_cc1 = port->cc1;
4940 old_cc2 = port->cc2;
4944 tcpm_log_force(port,
4945 "CC1: %u -> %u, CC2: %u -> %u [state %s, polarity %d, %s]",
4946 old_cc1, cc1, old_cc2, cc2, tcpm_states[port->state],
4948 tcpm_port_is_disconnected(port) ? "disconnected"
4951 switch (port->state) {
4953 if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) ||
4954 tcpm_port_is_source(port))
4955 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
4956 else if (tcpm_port_is_sink(port))
4957 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
4959 case CHECK_CONTAMINANT:
4960 /* Wait for Toggling to be resumed */
4962 case SRC_UNATTACHED:
4963 case ACC_UNATTACHED:
4964 if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) ||
4965 tcpm_port_is_source(port))
4966 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
4968 case SRC_ATTACH_WAIT:
4969 if (tcpm_port_is_disconnected(port) ||
4970 tcpm_port_is_audio_detached(port))
4971 tcpm_set_state(port, SRC_UNATTACHED, 0);
4972 else if (cc1 != old_cc1 || cc2 != old_cc2)
4973 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
4977 case SRC_SEND_CAPABILITIES:
4979 if (tcpm_port_is_disconnected(port) ||
4980 !tcpm_port_is_source(port)) {
4981 if (port->port_type == TYPEC_PORT_SRC)
4982 tcpm_set_state(port, SRC_UNATTACHED, tcpm_wait_for_discharge(port));
4984 tcpm_set_state(port, SNK_UNATTACHED, tcpm_wait_for_discharge(port));
4987 case SNK_UNATTACHED:
4988 if (tcpm_port_is_sink(port))
4989 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
4991 case SNK_ATTACH_WAIT:
4992 if ((port->cc1 == TYPEC_CC_OPEN &&
4993 port->cc2 != TYPEC_CC_OPEN) ||
4994 (port->cc1 != TYPEC_CC_OPEN &&
4995 port->cc2 == TYPEC_CC_OPEN))
4996 new_state = SNK_DEBOUNCED;
4997 else if (tcpm_port_is_disconnected(port))
4998 new_state = SNK_UNATTACHED;
5001 if (new_state != port->delayed_state)
5002 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
5005 if (tcpm_port_is_disconnected(port))
5006 new_state = SNK_UNATTACHED;
5007 else if (port->vbus_present)
5008 new_state = tcpm_try_src(port) ? SRC_TRY : SNK_ATTACHED;
5010 new_state = SNK_UNATTACHED;
5011 if (new_state != port->delayed_state)
5012 tcpm_set_state(port, SNK_DEBOUNCED, 0);
5016 * EXIT condition is based primarily on vbus disconnect and CC is secondary.
5017 * "A port that has entered into USB PD communications with the Source and
5018 * has seen the CC voltage exceed vRd-USB may monitor the CC pin to detect
5019 * cable disconnect in addition to monitoring VBUS.
5021 * A port that is monitoring the CC voltage for disconnect (but is not in
5022 * the process of a USB PD PR_Swap or USB PD FR_Swap) shall transition to
5023 * Unattached.SNK within tSinkDisconnect after the CC voltage remains below
5024 * vRd-USB for tPDDebounce."
5026 * When set_auto_vbus_discharge_threshold is enabled, CC pins go
5027 * away before vbus decays to disconnect threshold. Allow
5028 * disconnect to be driven by vbus disconnect when auto vbus
5029 * discharge is enabled.
5031 if (!port->auto_vbus_discharge_enabled && tcpm_port_is_disconnected(port))
5032 tcpm_set_state(port, unattached_state(port), 0);
5033 else if (!port->pd_capable &&
5034 (cc1 != old_cc1 || cc2 != old_cc2))
5035 tcpm_set_current_limit(port,
5036 tcpm_get_current_limit(port),
5040 case AUDIO_ACC_ATTACHED:
5041 if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN)
5042 tcpm_set_state(port, AUDIO_ACC_DEBOUNCE, 0);
5044 case AUDIO_ACC_DEBOUNCE:
5045 if (tcpm_port_is_audio(port))
5046 tcpm_set_state(port, AUDIO_ACC_ATTACHED, 0);
5049 case DEBUG_ACC_ATTACHED:
5050 if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN)
5051 tcpm_set_state(port, ACC_UNATTACHED, 0);
5055 /* Do nothing, waiting for timeout */
5059 /* CC line is unstable, wait for debounce */
5060 if (tcpm_port_is_disconnected(port))
5061 tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE, 0);
5063 case SNK_DISCOVERY_DEBOUNCE:
5067 /* Hand over to state machine if needed */
5068 if (!port->vbus_present && tcpm_port_is_source(port))
5069 tcpm_set_state(port, SRC_TRYWAIT_DEBOUNCE, 0);
5071 case SRC_TRYWAIT_DEBOUNCE:
5072 if (port->vbus_present || !tcpm_port_is_source(port))
5073 tcpm_set_state(port, SRC_TRYWAIT, 0);
5075 case SNK_TRY_WAIT_DEBOUNCE:
5076 if (!tcpm_port_is_sink(port)) {
5078 tcpm_set_state(port, SRC_TRYWAIT, 0);
5082 if (tcpm_port_is_source(port))
5083 tcpm_set_state(port, SRC_TRY_DEBOUNCE, 0);
5085 case SRC_TRY_DEBOUNCE:
5086 tcpm_set_state(port, SRC_TRY_WAIT, 0);
5088 case SNK_TRYWAIT_DEBOUNCE:
5089 if (tcpm_port_is_sink(port))
5090 tcpm_set_state(port, SNK_TRYWAIT_VBUS, 0);
5092 case SNK_TRYWAIT_VBUS:
5093 if (!tcpm_port_is_sink(port))
5094 tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, 0);
5096 case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS:
5097 if (!tcpm_port_is_sink(port))
5098 tcpm_set_state(port, SRC_TRYWAIT, PD_T_TRY_CC_DEBOUNCE);
5100 tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS, 0);
5103 /* Do nothing, waiting for tCCDebounce */
5105 case PR_SWAP_SNK_SRC_SINK_OFF:
5106 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
5107 case PR_SWAP_SRC_SNK_SOURCE_OFF:
5108 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED:
5109 case PR_SWAP_SNK_SRC_SOURCE_ON:
5111 * CC state change is expected in PR_SWAP
5116 case FR_SWAP_SEND_TIMEOUT:
5117 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
5118 case FR_SWAP_SNK_SRC_NEW_SINK_READY:
5119 case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED:
5120 /* Do nothing, CC change expected */
5124 case PORT_RESET_WAIT_OFF:
5126 * State set back to default mode once the timer completes.
5127 * Ignore CC changes here.
5132 * While acting as sink and auto vbus discharge is enabled, Allow disconnect
5133 * to be driven by vbus disconnect.
5135 if (tcpm_port_is_disconnected(port) && !(port->pwr_role == TYPEC_SINK &&
5136 port->auto_vbus_discharge_enabled))
5137 tcpm_set_state(port, unattached_state(port), 0);
5142 static void _tcpm_pd_vbus_on(struct tcpm_port *port)
5144 tcpm_log_force(port, "VBUS on");
5145 port->vbus_present = true;
5147 * When vbus_present is true i.e. Voltage at VBUS is greater than VSAFE5V implicitly
5148 * states that vbus is not at VSAFE0V, hence clear the vbus_vsafe0v flag here.
5150 port->vbus_vsafe0v = false;
5152 switch (port->state) {
5153 case SNK_TRANSITION_SINK_VBUS:
5154 port->explicit_contract = true;
5155 tcpm_set_state(port, SNK_READY, 0);
5158 tcpm_set_state(port, SNK_DISCOVERY, 0);
5162 tcpm_set_state(port, tcpm_try_src(port) ? SRC_TRY
5166 case SNK_HARD_RESET_WAIT_VBUS:
5167 tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, 0);
5170 tcpm_set_state(port, SRC_STARTUP, 0);
5172 case SRC_HARD_RESET_VBUS_ON:
5173 tcpm_set_state(port, SRC_STARTUP, 0);
5177 /* Do nothing, waiting for timeout */
5180 /* Do nothing, Waiting for Rd to be detected */
5182 case SRC_TRYWAIT_DEBOUNCE:
5183 tcpm_set_state(port, SRC_TRYWAIT, 0);
5185 case SNK_TRY_WAIT_DEBOUNCE:
5186 /* Do nothing, waiting for PD_DEBOUNCE to do be done */
5189 /* Do nothing, waiting for tCCDebounce */
5191 case SNK_TRYWAIT_VBUS:
5192 if (tcpm_port_is_sink(port))
5193 tcpm_set_state(port, SNK_ATTACHED, 0);
5195 case SNK_TRYWAIT_DEBOUNCE:
5196 /* Do nothing, waiting for Rp */
5198 case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS:
5199 if (port->vbus_present && tcpm_port_is_sink(port))
5200 tcpm_set_state(port, SNK_ATTACHED, 0);
5203 case SRC_TRY_DEBOUNCE:
5204 /* Do nothing, waiting for sink detection */
5207 case FR_SWAP_SEND_TIMEOUT:
5208 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
5209 case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED:
5210 if (port->tcpc->frs_sourcing_vbus)
5211 port->tcpc->frs_sourcing_vbus(port->tcpc);
5213 case FR_SWAP_SNK_SRC_NEW_SINK_READY:
5214 if (port->tcpc->frs_sourcing_vbus)
5215 port->tcpc->frs_sourcing_vbus(port->tcpc);
5216 tcpm_set_state(port, FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED, 0);
5220 case PORT_RESET_WAIT_OFF:
5222 * State set back to default mode once the timer completes.
5223 * Ignore vbus changes here.
5232 static void _tcpm_pd_vbus_off(struct tcpm_port *port)
5234 tcpm_log_force(port, "VBUS off");
5235 port->vbus_present = false;
5236 port->vbus_never_low = false;
5237 switch (port->state) {
5238 case SNK_HARD_RESET_SINK_OFF:
5239 tcpm_set_state(port, SNK_HARD_RESET_WAIT_VBUS, 0);
5241 case HARD_RESET_SEND:
5244 /* Do nothing, waiting for timeout */
5247 /* Hand over to state machine if needed */
5248 if (tcpm_port_is_source(port))
5249 tcpm_set_state(port, SRC_TRYWAIT_DEBOUNCE, 0);
5251 case SNK_TRY_WAIT_DEBOUNCE:
5252 /* Do nothing, waiting for PD_DEBOUNCE to do be done */
5255 case SNK_TRYWAIT_VBUS:
5256 case SNK_TRYWAIT_DEBOUNCE:
5258 case SNK_ATTACH_WAIT:
5260 /* Do nothing, as TCPM is still waiting for vbus to reaach VSAFE5V to connect */
5263 case SNK_NEGOTIATE_CAPABILITIES:
5266 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
5267 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF, 0);
5270 case PR_SWAP_SNK_SRC_SINK_OFF:
5271 /* Do nothing, expected */
5274 case PR_SWAP_SNK_SRC_SOURCE_ON:
5276 * Do nothing when vbus off notification is received.
5277 * TCPM can wait for PD_T_NEWSRC in PR_SWAP_SNK_SRC_SOURCE_ON
5278 * for the vbus source to ramp up.
5282 case PORT_RESET_WAIT_OFF:
5283 tcpm_set_state(port, tcpm_default_state(port), 0);
5287 case SRC_TRY_DEBOUNCE:
5288 /* Do nothing, waiting for sink detection */
5292 case SRC_SEND_CAPABILITIES:
5293 case SRC_SEND_CAPABILITIES_TIMEOUT:
5294 case SRC_NEGOTIATE_CAPABILITIES:
5295 case SRC_TRANSITION_SUPPLY:
5297 case SRC_WAIT_NEW_CAPABILITIES:
5299 * Force to unattached state to re-initiate connection.
5300 * DRP port should move to Unattached.SNK instead of Unattached.SRC if
5301 * sink removed. Although sink removal here is due to source's vbus collapse,
5302 * treat it the same way for consistency.
5304 if (port->port_type == TYPEC_PORT_SRC)
5305 tcpm_set_state(port, SRC_UNATTACHED, tcpm_wait_for_discharge(port));
5307 tcpm_set_state(port, SNK_UNATTACHED, tcpm_wait_for_discharge(port));
5312 * State set back to default mode once the timer completes.
5313 * Ignore vbus changes here.
5318 case FR_SWAP_SEND_TIMEOUT:
5319 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
5320 case FR_SWAP_SNK_SRC_NEW_SINK_READY:
5321 case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED:
5322 /* Do nothing, vbus drop expected */
5325 case SNK_HARD_RESET_WAIT_VBUS:
5326 /* Do nothing, its OK to receive vbus off events */
5330 if (port->pwr_role == TYPEC_SINK && port->attached)
5331 tcpm_set_state(port, SNK_UNATTACHED, tcpm_wait_for_discharge(port));
5336 static void _tcpm_pd_vbus_vsafe0v(struct tcpm_port *port)
5338 tcpm_log_force(port, "VBUS VSAFE0V");
5339 port->vbus_vsafe0v = true;
5340 switch (port->state) {
5341 case SRC_HARD_RESET_VBUS_OFF:
5343 * After establishing the vSafe0V voltage condition on VBUS, the Source Shall wait
5344 * tSrcRecover before re-applying VCONN and restoring VBUS to vSafe5V.
5346 tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SRC_RECOVER);
5348 case SRC_ATTACH_WAIT:
5349 if (tcpm_port_is_source(port))
5350 tcpm_set_state(port, tcpm_try_snk(port) ? SNK_TRY : SRC_ATTACHED,
5354 case SRC_SEND_CAPABILITIES:
5355 case SRC_SEND_CAPABILITIES_TIMEOUT:
5356 case SRC_NEGOTIATE_CAPABILITIES:
5357 case SRC_TRANSITION_SUPPLY:
5359 case SRC_WAIT_NEW_CAPABILITIES:
5360 if (port->auto_vbus_discharge_enabled) {
5361 if (port->port_type == TYPEC_PORT_SRC)
5362 tcpm_set_state(port, SRC_UNATTACHED, 0);
5364 tcpm_set_state(port, SNK_UNATTACHED, 0);
5367 case PR_SWAP_SNK_SRC_SINK_OFF:
5368 case PR_SWAP_SNK_SRC_SOURCE_ON:
5369 /* Do nothing, vsafe0v is expected during transition */
5371 case SNK_ATTACH_WAIT:
5373 /*Do nothing, still waiting for VSAFE5V for connect */
5375 case SNK_HARD_RESET_WAIT_VBUS:
5376 /* Do nothing, its OK to receive vbus off events */
5379 if (port->pwr_role == TYPEC_SINK && port->auto_vbus_discharge_enabled)
5380 tcpm_set_state(port, SNK_UNATTACHED, 0);
5385 static void _tcpm_pd_hard_reset(struct tcpm_port *port)
5387 tcpm_log_force(port, "Received hard reset");
5388 if (port->bist_request == BDO_MODE_TESTDATA && port->tcpc->set_bist_data)
5389 port->tcpc->set_bist_data(port->tcpc, false);
5391 if (port->ams != NONE_AMS)
5392 port->ams = NONE_AMS;
5393 if (port->hard_reset_count < PD_N_HARD_RESET_COUNT)
5394 port->ams = HARD_RESET;
5396 * If we keep receiving hard reset requests, executing the hard reset
5397 * must have failed. Revert to error recovery if that happens.
5399 tcpm_set_state(port,
5400 port->hard_reset_count < PD_N_HARD_RESET_COUNT ?
5401 HARD_RESET_START : ERROR_RECOVERY,
5405 static void tcpm_pd_event_handler(struct kthread_work *work)
5407 struct tcpm_port *port = container_of(work, struct tcpm_port,
5411 mutex_lock(&port->lock);
5413 spin_lock(&port->pd_event_lock);
5414 while (port->pd_events) {
5415 events = port->pd_events;
5416 port->pd_events = 0;
5417 spin_unlock(&port->pd_event_lock);
5418 if (events & TCPM_RESET_EVENT)
5419 _tcpm_pd_hard_reset(port);
5420 if (events & TCPM_VBUS_EVENT) {
5423 vbus = port->tcpc->get_vbus(port->tcpc);
5425 _tcpm_pd_vbus_on(port);
5427 _tcpm_pd_vbus_off(port);
5429 * When TCPC does not support detecting vsafe0v voltage level,
5430 * treat vbus absent as vsafe0v. Else invoke is_vbus_vsafe0v
5431 * to see if vbus has discharge to VSAFE0V.
5433 if (!port->tcpc->is_vbus_vsafe0v ||
5434 port->tcpc->is_vbus_vsafe0v(port->tcpc))
5435 _tcpm_pd_vbus_vsafe0v(port);
5438 if (events & TCPM_CC_EVENT) {
5439 enum typec_cc_status cc1, cc2;
5441 if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0)
5442 _tcpm_cc_change(port, cc1, cc2);
5444 if (events & TCPM_FRS_EVENT) {
5445 if (port->state == SNK_READY) {
5448 port->upcoming_state = FR_SWAP_SEND;
5449 ret = tcpm_ams_start(port, FAST_ROLE_SWAP);
5451 port->upcoming_state = INVALID_STATE;
5453 tcpm_log(port, "Discarding FRS_SIGNAL! Not in sink ready");
5456 if (events & TCPM_SOURCING_VBUS) {
5457 tcpm_log(port, "sourcing vbus");
5459 * In fast role swap case TCPC autonomously sources vbus. Set vbus_source
5460 * true as TCPM wouldn't have called tcpm_set_vbus.
5462 * When vbus is sourced on the command on TCPM i.e. TCPM called
5463 * tcpm_set_vbus to source vbus, vbus_source would already be true.
5465 port->vbus_source = true;
5466 _tcpm_pd_vbus_on(port);
5468 if (events & TCPM_PORT_CLEAN) {
5469 tcpm_log(port, "port clean");
5470 if (port->state == CHECK_CONTAMINANT) {
5471 if (tcpm_start_toggling(port, tcpm_rp_cc(port)))
5472 tcpm_set_state(port, TOGGLING, 0);
5474 tcpm_set_state(port, tcpm_default_state(port), 0);
5478 spin_lock(&port->pd_event_lock);
5480 spin_unlock(&port->pd_event_lock);
5481 mutex_unlock(&port->lock);
5484 void tcpm_cc_change(struct tcpm_port *port)
5486 spin_lock(&port->pd_event_lock);
5487 port->pd_events |= TCPM_CC_EVENT;
5488 spin_unlock(&port->pd_event_lock);
5489 kthread_queue_work(port->wq, &port->event_work);
5491 EXPORT_SYMBOL_GPL(tcpm_cc_change);
5493 void tcpm_vbus_change(struct tcpm_port *port)
5495 spin_lock(&port->pd_event_lock);
5496 port->pd_events |= TCPM_VBUS_EVENT;
5497 spin_unlock(&port->pd_event_lock);
5498 kthread_queue_work(port->wq, &port->event_work);
5500 EXPORT_SYMBOL_GPL(tcpm_vbus_change);
5502 void tcpm_pd_hard_reset(struct tcpm_port *port)
5504 spin_lock(&port->pd_event_lock);
5505 port->pd_events = TCPM_RESET_EVENT;
5506 spin_unlock(&port->pd_event_lock);
5507 kthread_queue_work(port->wq, &port->event_work);
5509 EXPORT_SYMBOL_GPL(tcpm_pd_hard_reset);
5511 void tcpm_sink_frs(struct tcpm_port *port)
5513 spin_lock(&port->pd_event_lock);
5514 port->pd_events |= TCPM_FRS_EVENT;
5515 spin_unlock(&port->pd_event_lock);
5516 kthread_queue_work(port->wq, &port->event_work);
5518 EXPORT_SYMBOL_GPL(tcpm_sink_frs);
5520 void tcpm_sourcing_vbus(struct tcpm_port *port)
5522 spin_lock(&port->pd_event_lock);
5523 port->pd_events |= TCPM_SOURCING_VBUS;
5524 spin_unlock(&port->pd_event_lock);
5525 kthread_queue_work(port->wq, &port->event_work);
5527 EXPORT_SYMBOL_GPL(tcpm_sourcing_vbus);
5529 void tcpm_port_clean(struct tcpm_port *port)
5531 spin_lock(&port->pd_event_lock);
5532 port->pd_events |= TCPM_PORT_CLEAN;
5533 spin_unlock(&port->pd_event_lock);
5534 kthread_queue_work(port->wq, &port->event_work);
5536 EXPORT_SYMBOL_GPL(tcpm_port_clean);
5538 bool tcpm_port_is_toggling(struct tcpm_port *port)
5540 return port->port_type == TYPEC_PORT_DRP && port->state == TOGGLING;
5542 EXPORT_SYMBOL_GPL(tcpm_port_is_toggling);
5544 static void tcpm_enable_frs_work(struct kthread_work *work)
5546 struct tcpm_port *port = container_of(work, struct tcpm_port, enable_frs);
5549 mutex_lock(&port->lock);
5550 /* Not FRS capable */
5551 if (!port->connected || port->port_type != TYPEC_PORT_DRP ||
5552 port->pwr_opmode != TYPEC_PWR_MODE_PD ||
5553 !port->tcpc->enable_frs ||
5554 /* Sink caps queried */
5555 port->sink_cap_done || port->negotiated_rev < PD_REV30)
5558 /* Send when the state machine is idle */
5559 if (port->state != SNK_READY || port->vdm_sm_running || port->send_discover)
5562 port->upcoming_state = GET_SINK_CAP;
5563 ret = tcpm_ams_start(port, GET_SINK_CAPABILITIES);
5564 if (ret == -EAGAIN) {
5565 port->upcoming_state = INVALID_STATE;
5567 port->sink_cap_done = true;
5571 mod_enable_frs_delayed_work(port, GET_SINK_CAP_RETRY_MS);
5573 mutex_unlock(&port->lock);
5576 static void tcpm_send_discover_work(struct kthread_work *work)
5578 struct tcpm_port *port = container_of(work, struct tcpm_port, send_discover_work);
5580 mutex_lock(&port->lock);
5581 /* No need to send DISCOVER_IDENTITY anymore */
5582 if (!port->send_discover)
5585 if (port->data_role == TYPEC_DEVICE && port->negotiated_rev < PD_REV30) {
5586 port->send_discover = false;
5590 /* Retry if the port is not idle */
5591 if ((port->state != SRC_READY && port->state != SNK_READY) || port->vdm_sm_running) {
5592 mod_send_discover_delayed_work(port, SEND_DISCOVER_RETRY_MS);
5596 tcpm_send_vdm(port, USB_SID_PD, CMD_DISCOVER_IDENT, NULL, 0);
5599 mutex_unlock(&port->lock);
5602 static int tcpm_dr_set(struct typec_port *p, enum typec_data_role data)
5604 struct tcpm_port *port = typec_get_drvdata(p);
5607 mutex_lock(&port->swap_lock);
5608 mutex_lock(&port->lock);
5610 if (port->typec_caps.data != TYPEC_PORT_DRD) {
5614 if (port->state != SRC_READY && port->state != SNK_READY) {
5619 if (port->data_role == data) {
5626 * 6.3.9: If an alternate mode is active, a request to swap
5627 * alternate modes shall trigger a port reset.
5628 * Reject data role swap request in this case.
5631 if (!port->pd_capable) {
5633 * If the partner is not PD capable, reset the port to
5634 * trigger a role change. This can only work if a preferred
5635 * role is configured, and if it matches the requested role.
5637 if (port->try_role == TYPEC_NO_PREFERRED_ROLE ||
5638 port->try_role == port->pwr_role) {
5642 port->non_pd_role_swap = true;
5643 tcpm_set_state(port, PORT_RESET, 0);
5645 port->upcoming_state = DR_SWAP_SEND;
5646 ret = tcpm_ams_start(port, DATA_ROLE_SWAP);
5647 if (ret == -EAGAIN) {
5648 port->upcoming_state = INVALID_STATE;
5653 port->swap_status = 0;
5654 port->swap_pending = true;
5655 reinit_completion(&port->swap_complete);
5656 mutex_unlock(&port->lock);
5658 if (!wait_for_completion_timeout(&port->swap_complete,
5659 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT)))
5662 ret = port->swap_status;
5664 port->non_pd_role_swap = false;
5668 mutex_unlock(&port->lock);
5670 mutex_unlock(&port->swap_lock);
5674 static int tcpm_pr_set(struct typec_port *p, enum typec_role role)
5676 struct tcpm_port *port = typec_get_drvdata(p);
5679 mutex_lock(&port->swap_lock);
5680 mutex_lock(&port->lock);
5682 if (port->port_type != TYPEC_PORT_DRP) {
5686 if (port->state != SRC_READY && port->state != SNK_READY) {
5691 if (role == port->pwr_role) {
5696 port->upcoming_state = PR_SWAP_SEND;
5697 ret = tcpm_ams_start(port, POWER_ROLE_SWAP);
5698 if (ret == -EAGAIN) {
5699 port->upcoming_state = INVALID_STATE;
5703 port->swap_status = 0;
5704 port->swap_pending = true;
5705 reinit_completion(&port->swap_complete);
5706 mutex_unlock(&port->lock);
5708 if (!wait_for_completion_timeout(&port->swap_complete,
5709 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT)))
5712 ret = port->swap_status;
5717 mutex_unlock(&port->lock);
5719 mutex_unlock(&port->swap_lock);
5723 static int tcpm_vconn_set(struct typec_port *p, enum typec_role role)
5725 struct tcpm_port *port = typec_get_drvdata(p);
5728 mutex_lock(&port->swap_lock);
5729 mutex_lock(&port->lock);
5731 if (port->state != SRC_READY && port->state != SNK_READY) {
5736 if (role == port->vconn_role) {
5741 port->upcoming_state = VCONN_SWAP_SEND;
5742 ret = tcpm_ams_start(port, VCONN_SWAP);
5743 if (ret == -EAGAIN) {
5744 port->upcoming_state = INVALID_STATE;
5748 port->swap_status = 0;
5749 port->swap_pending = true;
5750 reinit_completion(&port->swap_complete);
5751 mutex_unlock(&port->lock);
5753 if (!wait_for_completion_timeout(&port->swap_complete,
5754 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT)))
5757 ret = port->swap_status;
5762 mutex_unlock(&port->lock);
5764 mutex_unlock(&port->swap_lock);
5768 static int tcpm_try_role(struct typec_port *p, int role)
5770 struct tcpm_port *port = typec_get_drvdata(p);
5771 struct tcpc_dev *tcpc = port->tcpc;
5774 mutex_lock(&port->lock);
5776 ret = tcpc->try_role(tcpc, role);
5778 port->try_role = role;
5779 port->try_src_count = 0;
5780 port->try_snk_count = 0;
5781 mutex_unlock(&port->lock);
5786 static int tcpm_pps_set_op_curr(struct tcpm_port *port, u16 req_op_curr)
5788 unsigned int target_mw;
5791 mutex_lock(&port->swap_lock);
5792 mutex_lock(&port->lock);
5794 if (!port->pps_data.active) {
5799 if (port->state != SNK_READY) {
5804 if (req_op_curr > port->pps_data.max_curr) {
5809 target_mw = (req_op_curr * port->supply_voltage) / 1000;
5810 if (target_mw < port->operating_snk_mw) {
5815 port->upcoming_state = SNK_NEGOTIATE_PPS_CAPABILITIES;
5816 ret = tcpm_ams_start(port, POWER_NEGOTIATION);
5817 if (ret == -EAGAIN) {
5818 port->upcoming_state = INVALID_STATE;
5822 /* Round down operating current to align with PPS valid steps */
5823 req_op_curr = req_op_curr - (req_op_curr % RDO_PROG_CURR_MA_STEP);
5825 reinit_completion(&port->pps_complete);
5826 port->pps_data.req_op_curr = req_op_curr;
5827 port->pps_status = 0;
5828 port->pps_pending = true;
5829 mutex_unlock(&port->lock);
5831 if (!wait_for_completion_timeout(&port->pps_complete,
5832 msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT)))
5835 ret = port->pps_status;
5840 mutex_unlock(&port->lock);
5842 mutex_unlock(&port->swap_lock);
5847 static int tcpm_pps_set_out_volt(struct tcpm_port *port, u16 req_out_volt)
5849 unsigned int target_mw;
5852 mutex_lock(&port->swap_lock);
5853 mutex_lock(&port->lock);
5855 if (!port->pps_data.active) {
5860 if (port->state != SNK_READY) {
5865 target_mw = (port->current_limit * req_out_volt) / 1000;
5866 if (target_mw < port->operating_snk_mw) {
5871 port->upcoming_state = SNK_NEGOTIATE_PPS_CAPABILITIES;
5872 ret = tcpm_ams_start(port, POWER_NEGOTIATION);
5873 if (ret == -EAGAIN) {
5874 port->upcoming_state = INVALID_STATE;
5878 /* Round down output voltage to align with PPS valid steps */
5879 req_out_volt = req_out_volt - (req_out_volt % RDO_PROG_VOLT_MV_STEP);
5881 reinit_completion(&port->pps_complete);
5882 port->pps_data.req_out_volt = req_out_volt;
5883 port->pps_status = 0;
5884 port->pps_pending = true;
5885 mutex_unlock(&port->lock);
5887 if (!wait_for_completion_timeout(&port->pps_complete,
5888 msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT)))
5891 ret = port->pps_status;
5896 mutex_unlock(&port->lock);
5898 mutex_unlock(&port->swap_lock);
5903 static int tcpm_pps_activate(struct tcpm_port *port, bool activate)
5907 mutex_lock(&port->swap_lock);
5908 mutex_lock(&port->lock);
5910 if (!port->pps_data.supported) {
5915 /* Trying to deactivate PPS when already deactivated so just bail */
5916 if (!port->pps_data.active && !activate)
5919 if (port->state != SNK_READY) {
5925 port->upcoming_state = SNK_NEGOTIATE_PPS_CAPABILITIES;
5927 port->upcoming_state = SNK_NEGOTIATE_CAPABILITIES;
5928 ret = tcpm_ams_start(port, POWER_NEGOTIATION);
5929 if (ret == -EAGAIN) {
5930 port->upcoming_state = INVALID_STATE;
5934 reinit_completion(&port->pps_complete);
5935 port->pps_status = 0;
5936 port->pps_pending = true;
5938 /* Trigger PPS request or move back to standard PDO contract */
5940 port->pps_data.req_out_volt = port->supply_voltage;
5941 port->pps_data.req_op_curr = port->current_limit;
5943 mutex_unlock(&port->lock);
5945 if (!wait_for_completion_timeout(&port->pps_complete,
5946 msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT)))
5949 ret = port->pps_status;
5954 mutex_unlock(&port->lock);
5956 mutex_unlock(&port->swap_lock);
5961 static void tcpm_init(struct tcpm_port *port)
5963 enum typec_cc_status cc1, cc2;
5965 port->tcpc->init(port->tcpc);
5967 tcpm_reset_port(port);
5971 * Should possibly wait for VBUS to settle if it was enabled locally
5972 * since tcpm_reset_port() will disable VBUS.
5974 port->vbus_present = port->tcpc->get_vbus(port->tcpc);
5975 if (port->vbus_present)
5976 port->vbus_never_low = true;
5979 * 1. When vbus_present is true, voltage on VBUS is already at VSAFE5V.
5980 * So implicitly vbus_vsafe0v = false.
5982 * 2. When vbus_present is false and TCPC does NOT support querying
5983 * vsafe0v status, then, it's best to assume vbus is at VSAFE0V i.e.
5984 * vbus_vsafe0v is true.
5986 * 3. When vbus_present is false and TCPC does support querying vsafe0v,
5987 * then, query tcpc for vsafe0v status.
5989 if (port->vbus_present)
5990 port->vbus_vsafe0v = false;
5991 else if (!port->tcpc->is_vbus_vsafe0v)
5992 port->vbus_vsafe0v = true;
5994 port->vbus_vsafe0v = port->tcpc->is_vbus_vsafe0v(port->tcpc);
5996 tcpm_set_state(port, tcpm_default_state(port), 0);
5998 if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0)
5999 _tcpm_cc_change(port, cc1, cc2);
6002 * Some adapters need a clean slate at startup, and won't recover
6003 * otherwise. So do not try to be fancy and force a clean disconnect.
6005 tcpm_set_state(port, PORT_RESET, 0);
6008 static int tcpm_port_type_set(struct typec_port *p, enum typec_port_type type)
6010 struct tcpm_port *port = typec_get_drvdata(p);
6012 mutex_lock(&port->lock);
6013 if (type == port->port_type)
6016 port->port_type = type;
6018 if (!port->connected) {
6019 tcpm_set_state(port, PORT_RESET, 0);
6020 } else if (type == TYPEC_PORT_SNK) {
6021 if (!(port->pwr_role == TYPEC_SINK &&
6022 port->data_role == TYPEC_DEVICE))
6023 tcpm_set_state(port, PORT_RESET, 0);
6024 } else if (type == TYPEC_PORT_SRC) {
6025 if (!(port->pwr_role == TYPEC_SOURCE &&
6026 port->data_role == TYPEC_HOST))
6027 tcpm_set_state(port, PORT_RESET, 0);
6031 mutex_unlock(&port->lock);
6035 static const struct typec_operations tcpm_ops = {
6036 .try_role = tcpm_try_role,
6037 .dr_set = tcpm_dr_set,
6038 .pr_set = tcpm_pr_set,
6039 .vconn_set = tcpm_vconn_set,
6040 .port_type_set = tcpm_port_type_set
6043 void tcpm_tcpc_reset(struct tcpm_port *port)
6045 mutex_lock(&port->lock);
6046 /* XXX: Maintain PD connection if possible? */
6048 mutex_unlock(&port->lock);
6050 EXPORT_SYMBOL_GPL(tcpm_tcpc_reset);
6052 static void tcpm_port_unregister_pd(struct tcpm_port *port)
6054 usb_power_delivery_unregister_capabilities(port->port_sink_caps);
6055 port->port_sink_caps = NULL;
6056 usb_power_delivery_unregister_capabilities(port->port_source_caps);
6057 port->port_source_caps = NULL;
6058 usb_power_delivery_unregister(port->pd);
6062 static int tcpm_port_register_pd(struct tcpm_port *port)
6064 struct usb_power_delivery_desc desc = { port->typec_caps.pd_revision };
6065 struct usb_power_delivery_capabilities_desc caps = { };
6066 struct usb_power_delivery_capabilities *cap;
6069 if (!port->nr_src_pdo && !port->nr_snk_pdo)
6072 port->pd = usb_power_delivery_register(port->dev, &desc);
6073 if (IS_ERR(port->pd)) {
6074 ret = PTR_ERR(port->pd);
6075 goto err_unregister;
6078 if (port->nr_src_pdo) {
6079 memcpy_and_pad(caps.pdo, sizeof(caps.pdo), port->src_pdo,
6080 port->nr_src_pdo * sizeof(u32), 0);
6081 caps.role = TYPEC_SOURCE;
6083 cap = usb_power_delivery_register_capabilities(port->pd, &caps);
6086 goto err_unregister;
6089 port->port_source_caps = cap;
6092 if (port->nr_snk_pdo) {
6093 memcpy_and_pad(caps.pdo, sizeof(caps.pdo), port->snk_pdo,
6094 port->nr_snk_pdo * sizeof(u32), 0);
6095 caps.role = TYPEC_SINK;
6097 cap = usb_power_delivery_register_capabilities(port->pd, &caps);
6100 goto err_unregister;
6103 port->port_sink_caps = cap;
6109 tcpm_port_unregister_pd(port);
6114 static int tcpm_fw_get_caps(struct tcpm_port *port,
6115 struct fwnode_handle *fwnode)
6117 const char *opmode_str;
6119 u32 mw, frs_current;
6125 * This fwnode has a "compatible" property, but is never populated as a
6126 * struct device. Instead we simply parse it to read the properties.
6127 * This it breaks fw_devlink=on. To maintain backward compatibility
6128 * with existing DT files, we work around this by deleting any
6129 * fwnode_links to/from this fwnode.
6131 fw_devlink_purge_absent_suppliers(fwnode);
6133 ret = typec_get_fw_cap(&port->typec_caps, fwnode);
6137 port->port_type = port->typec_caps.type;
6138 port->pd_supported = !fwnode_property_read_bool(fwnode, "pd-disable");
6140 port->slow_charger_loop = fwnode_property_read_bool(fwnode, "slow-charger-loop");
6141 if (port->port_type == TYPEC_PORT_SNK)
6144 /* Get Source PDOs for the PD port or Source Rp value for the non-PD port */
6145 if (port->pd_supported) {
6146 ret = fwnode_property_count_u32(fwnode, "source-pdos");
6152 port->nr_src_pdo = min(ret, PDO_MAX_OBJECTS);
6153 ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
6154 port->src_pdo, port->nr_src_pdo);
6157 ret = tcpm_validate_caps(port, port->src_pdo, port->nr_src_pdo);
6161 ret = fwnode_property_read_string(fwnode, "typec-power-opmode", &opmode_str);
6164 ret = typec_find_pwr_opmode(opmode_str);
6167 port->src_rp = tcpm_pwr_opmode_to_rp(ret);
6170 if (port->port_type == TYPEC_PORT_SRC)
6174 port->self_powered = fwnode_property_read_bool(fwnode, "self-powered");
6176 if (!port->pd_supported)
6180 ret = fwnode_property_count_u32(fwnode, "sink-pdos");
6184 port->nr_snk_pdo = min(ret, PDO_MAX_OBJECTS);
6185 ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
6186 port->snk_pdo, port->nr_snk_pdo);
6187 if ((ret < 0) || tcpm_validate_caps(port, port->snk_pdo,
6191 if (fwnode_property_read_u32(fwnode, "op-sink-microwatt", &mw) < 0)
6193 port->operating_snk_mw = mw / 1000;
6195 /* FRS can only be supported by DRP ports */
6196 if (port->port_type == TYPEC_PORT_DRP) {
6197 ret = fwnode_property_read_u32(fwnode, "new-source-frs-typec-current",
6199 if (ret >= 0 && frs_current <= FRS_5V_3A)
6200 port->new_source_frs_current = frs_current;
6203 /* sink-vdos is optional */
6204 ret = fwnode_property_count_u32(fwnode, "sink-vdos");
6208 port->nr_snk_vdo = min(ret, VDO_MAX_OBJECTS);
6209 if (port->nr_snk_vdo) {
6210 ret = fwnode_property_read_u32_array(fwnode, "sink-vdos",
6217 /* If sink-vdos is found, sink-vdos-v1 is expected for backward compatibility. */
6218 if (port->nr_snk_vdo) {
6219 ret = fwnode_property_count_u32(fwnode, "sink-vdos-v1");
6225 port->nr_snk_vdo_v1 = min(ret, VDO_MAX_OBJECTS);
6226 ret = fwnode_property_read_u32_array(fwnode, "sink-vdos-v1",
6228 port->nr_snk_vdo_v1);
6236 /* Power Supply access to expose source power information */
6237 enum tcpm_psy_online_states {
6238 TCPM_PSY_OFFLINE = 0,
6239 TCPM_PSY_FIXED_ONLINE,
6240 TCPM_PSY_PROG_ONLINE,
6243 static enum power_supply_property tcpm_psy_props[] = {
6244 POWER_SUPPLY_PROP_USB_TYPE,
6245 POWER_SUPPLY_PROP_ONLINE,
6246 POWER_SUPPLY_PROP_VOLTAGE_MIN,
6247 POWER_SUPPLY_PROP_VOLTAGE_MAX,
6248 POWER_SUPPLY_PROP_VOLTAGE_NOW,
6249 POWER_SUPPLY_PROP_CURRENT_MAX,
6250 POWER_SUPPLY_PROP_CURRENT_NOW,
6253 static int tcpm_psy_get_online(struct tcpm_port *port,
6254 union power_supply_propval *val)
6256 if (port->vbus_charge) {
6257 if (port->pps_data.active)
6258 val->intval = TCPM_PSY_PROG_ONLINE;
6260 val->intval = TCPM_PSY_FIXED_ONLINE;
6262 val->intval = TCPM_PSY_OFFLINE;
6268 static int tcpm_psy_get_voltage_min(struct tcpm_port *port,
6269 union power_supply_propval *val)
6271 if (port->pps_data.active)
6272 val->intval = port->pps_data.min_volt * 1000;
6274 val->intval = port->supply_voltage * 1000;
6279 static int tcpm_psy_get_voltage_max(struct tcpm_port *port,
6280 union power_supply_propval *val)
6282 if (port->pps_data.active)
6283 val->intval = port->pps_data.max_volt * 1000;
6285 val->intval = port->supply_voltage * 1000;
6290 static int tcpm_psy_get_voltage_now(struct tcpm_port *port,
6291 union power_supply_propval *val)
6293 val->intval = port->supply_voltage * 1000;
6298 static int tcpm_psy_get_current_max(struct tcpm_port *port,
6299 union power_supply_propval *val)
6301 if (port->pps_data.active)
6302 val->intval = port->pps_data.max_curr * 1000;
6304 val->intval = port->current_limit * 1000;
6309 static int tcpm_psy_get_current_now(struct tcpm_port *port,
6310 union power_supply_propval *val)
6312 val->intval = port->current_limit * 1000;
6317 static int tcpm_psy_get_input_power_limit(struct tcpm_port *port,
6318 union power_supply_propval *val)
6320 unsigned int src_mv, src_ma, max_src_uw = 0;
6321 unsigned int i, tmp;
6323 for (i = 0; i < port->nr_source_caps; i++) {
6324 u32 pdo = port->source_caps[i];
6326 if (pdo_type(pdo) == PDO_TYPE_FIXED) {
6327 src_mv = pdo_fixed_voltage(pdo);
6328 src_ma = pdo_max_current(pdo);
6329 tmp = src_mv * src_ma;
6330 max_src_uw = tmp > max_src_uw ? tmp : max_src_uw;
6334 val->intval = max_src_uw;
6338 static int tcpm_psy_get_prop(struct power_supply *psy,
6339 enum power_supply_property psp,
6340 union power_supply_propval *val)
6342 struct tcpm_port *port = power_supply_get_drvdata(psy);
6346 case POWER_SUPPLY_PROP_USB_TYPE:
6347 val->intval = port->usb_type;
6349 case POWER_SUPPLY_PROP_ONLINE:
6350 ret = tcpm_psy_get_online(port, val);
6352 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
6353 ret = tcpm_psy_get_voltage_min(port, val);
6355 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
6356 ret = tcpm_psy_get_voltage_max(port, val);
6358 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
6359 ret = tcpm_psy_get_voltage_now(port, val);
6361 case POWER_SUPPLY_PROP_CURRENT_MAX:
6362 ret = tcpm_psy_get_current_max(port, val);
6364 case POWER_SUPPLY_PROP_CURRENT_NOW:
6365 ret = tcpm_psy_get_current_now(port, val);
6367 case POWER_SUPPLY_PROP_INPUT_POWER_LIMIT:
6368 tcpm_psy_get_input_power_limit(port, val);
6378 static int tcpm_psy_set_online(struct tcpm_port *port,
6379 const union power_supply_propval *val)
6383 switch (val->intval) {
6384 case TCPM_PSY_FIXED_ONLINE:
6385 ret = tcpm_pps_activate(port, false);
6387 case TCPM_PSY_PROG_ONLINE:
6388 ret = tcpm_pps_activate(port, true);
6398 static int tcpm_psy_set_prop(struct power_supply *psy,
6399 enum power_supply_property psp,
6400 const union power_supply_propval *val)
6402 struct tcpm_port *port = power_supply_get_drvdata(psy);
6406 * All the properties below are related to USB PD. The check needs to be
6407 * property specific when a non-pd related property is added.
6409 if (!port->pd_supported)
6413 case POWER_SUPPLY_PROP_ONLINE:
6414 ret = tcpm_psy_set_online(port, val);
6416 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
6417 ret = tcpm_pps_set_out_volt(port, val->intval / 1000);
6419 case POWER_SUPPLY_PROP_CURRENT_NOW:
6420 if (val->intval > port->pps_data.max_curr * 1000)
6423 ret = tcpm_pps_set_op_curr(port, val->intval / 1000);
6429 power_supply_changed(port->psy);
6433 static int tcpm_psy_prop_writeable(struct power_supply *psy,
6434 enum power_supply_property psp)
6437 case POWER_SUPPLY_PROP_ONLINE:
6438 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
6439 case POWER_SUPPLY_PROP_CURRENT_NOW:
6446 static enum power_supply_usb_type tcpm_psy_usb_types[] = {
6447 POWER_SUPPLY_USB_TYPE_C,
6448 POWER_SUPPLY_USB_TYPE_PD,
6449 POWER_SUPPLY_USB_TYPE_PD_PPS,
6452 static const char *tcpm_psy_name_prefix = "tcpm-source-psy-";
6454 static int devm_tcpm_psy_register(struct tcpm_port *port)
6456 struct power_supply_config psy_cfg = {};
6457 const char *port_dev_name = dev_name(port->dev);
6458 size_t psy_name_len = strlen(tcpm_psy_name_prefix) +
6459 strlen(port_dev_name) + 1;
6462 psy_cfg.drv_data = port;
6463 psy_cfg.fwnode = dev_fwnode(port->dev);
6464 psy_name = devm_kzalloc(port->dev, psy_name_len, GFP_KERNEL);
6468 snprintf(psy_name, psy_name_len, "%s%s", tcpm_psy_name_prefix,
6470 port->psy_desc.name = psy_name;
6471 port->psy_desc.type = POWER_SUPPLY_TYPE_USB;
6472 port->psy_desc.usb_types = tcpm_psy_usb_types;
6473 port->psy_desc.num_usb_types = ARRAY_SIZE(tcpm_psy_usb_types);
6474 port->psy_desc.properties = tcpm_psy_props;
6475 port->psy_desc.num_properties = ARRAY_SIZE(tcpm_psy_props);
6476 port->psy_desc.get_property = tcpm_psy_get_prop;
6477 port->psy_desc.set_property = tcpm_psy_set_prop;
6478 port->psy_desc.property_is_writeable = tcpm_psy_prop_writeable;
6480 port->usb_type = POWER_SUPPLY_USB_TYPE_C;
6482 port->psy = devm_power_supply_register(port->dev, &port->psy_desc,
6485 return PTR_ERR_OR_ZERO(port->psy);
6488 static enum hrtimer_restart state_machine_timer_handler(struct hrtimer *timer)
6490 struct tcpm_port *port = container_of(timer, struct tcpm_port, state_machine_timer);
6492 if (port->registered)
6493 kthread_queue_work(port->wq, &port->state_machine);
6494 return HRTIMER_NORESTART;
6497 static enum hrtimer_restart vdm_state_machine_timer_handler(struct hrtimer *timer)
6499 struct tcpm_port *port = container_of(timer, struct tcpm_port, vdm_state_machine_timer);
6501 if (port->registered)
6502 kthread_queue_work(port->wq, &port->vdm_state_machine);
6503 return HRTIMER_NORESTART;
6506 static enum hrtimer_restart enable_frs_timer_handler(struct hrtimer *timer)
6508 struct tcpm_port *port = container_of(timer, struct tcpm_port, enable_frs_timer);
6510 if (port->registered)
6511 kthread_queue_work(port->wq, &port->enable_frs);
6512 return HRTIMER_NORESTART;
6515 static enum hrtimer_restart send_discover_timer_handler(struct hrtimer *timer)
6517 struct tcpm_port *port = container_of(timer, struct tcpm_port, send_discover_timer);
6519 if (port->registered)
6520 kthread_queue_work(port->wq, &port->send_discover_work);
6521 return HRTIMER_NORESTART;
6524 struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
6526 struct tcpm_port *port;
6529 if (!dev || !tcpc ||
6530 !tcpc->get_vbus || !tcpc->set_cc || !tcpc->get_cc ||
6531 !tcpc->set_polarity || !tcpc->set_vconn || !tcpc->set_vbus ||
6532 !tcpc->set_pd_rx || !tcpc->set_roles || !tcpc->pd_transmit)
6533 return ERR_PTR(-EINVAL);
6535 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
6537 return ERR_PTR(-ENOMEM);
6542 mutex_init(&port->lock);
6543 mutex_init(&port->swap_lock);
6545 port->wq = kthread_create_worker(0, dev_name(dev));
6546 if (IS_ERR(port->wq))
6547 return ERR_CAST(port->wq);
6548 sched_set_fifo(port->wq->task);
6550 kthread_init_work(&port->state_machine, tcpm_state_machine_work);
6551 kthread_init_work(&port->vdm_state_machine, vdm_state_machine_work);
6552 kthread_init_work(&port->event_work, tcpm_pd_event_handler);
6553 kthread_init_work(&port->enable_frs, tcpm_enable_frs_work);
6554 kthread_init_work(&port->send_discover_work, tcpm_send_discover_work);
6555 hrtimer_init(&port->state_machine_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6556 port->state_machine_timer.function = state_machine_timer_handler;
6557 hrtimer_init(&port->vdm_state_machine_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6558 port->vdm_state_machine_timer.function = vdm_state_machine_timer_handler;
6559 hrtimer_init(&port->enable_frs_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6560 port->enable_frs_timer.function = enable_frs_timer_handler;
6561 hrtimer_init(&port->send_discover_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6562 port->send_discover_timer.function = send_discover_timer_handler;
6564 spin_lock_init(&port->pd_event_lock);
6566 init_completion(&port->tx_complete);
6567 init_completion(&port->swap_complete);
6568 init_completion(&port->pps_complete);
6569 tcpm_debugfs_init(port);
6571 err = tcpm_fw_get_caps(port, tcpc->fwnode);
6573 goto out_destroy_wq;
6575 port->try_role = port->typec_caps.prefer_role;
6577 port->typec_caps.fwnode = tcpc->fwnode;
6578 port->typec_caps.revision = 0x0120; /* Type-C spec release 1.2 */
6579 port->typec_caps.pd_revision = 0x0300; /* USB-PD spec release 3.0 */
6580 port->typec_caps.svdm_version = SVDM_VER_2_0;
6581 port->typec_caps.driver_data = port;
6582 port->typec_caps.ops = &tcpm_ops;
6583 port->typec_caps.orientation_aware = 1;
6585 port->partner_desc.identity = &port->partner_ident;
6586 port->port_type = port->typec_caps.type;
6588 port->role_sw = usb_role_switch_get(port->dev);
6590 port->role_sw = fwnode_usb_role_switch_get(tcpc->fwnode);
6591 if (IS_ERR(port->role_sw)) {
6592 err = PTR_ERR(port->role_sw);
6593 goto out_destroy_wq;
6596 err = devm_tcpm_psy_register(port);
6598 goto out_role_sw_put;
6599 power_supply_changed(port->psy);
6601 err = tcpm_port_register_pd(port);
6603 goto out_role_sw_put;
6605 port->typec_caps.pd = port->pd;
6607 port->typec_port = typec_register_port(port->dev, &port->typec_caps);
6608 if (IS_ERR(port->typec_port)) {
6609 err = PTR_ERR(port->typec_port);
6610 goto out_unregister_pd;
6613 typec_port_register_altmodes(port->typec_port,
6614 &tcpm_altmode_ops, port,
6615 port->port_altmode, ALTMODE_DISCOVERY_MAX);
6616 port->registered = true;
6618 mutex_lock(&port->lock);
6620 mutex_unlock(&port->lock);
6622 tcpm_log(port, "%s: registered", dev_name(dev));
6626 tcpm_port_unregister_pd(port);
6628 usb_role_switch_put(port->role_sw);
6630 tcpm_debugfs_exit(port);
6631 kthread_destroy_worker(port->wq);
6632 return ERR_PTR(err);
6634 EXPORT_SYMBOL_GPL(tcpm_register_port);
6636 void tcpm_unregister_port(struct tcpm_port *port)
6640 port->registered = false;
6641 kthread_destroy_worker(port->wq);
6643 hrtimer_cancel(&port->send_discover_timer);
6644 hrtimer_cancel(&port->enable_frs_timer);
6645 hrtimer_cancel(&port->vdm_state_machine_timer);
6646 hrtimer_cancel(&port->state_machine_timer);
6648 tcpm_reset_port(port);
6650 tcpm_port_unregister_pd(port);
6652 for (i = 0; i < ARRAY_SIZE(port->port_altmode); i++)
6653 typec_unregister_altmode(port->port_altmode[i]);
6654 typec_unregister_port(port->typec_port);
6655 usb_role_switch_put(port->role_sw);
6656 tcpm_debugfs_exit(port);
6658 EXPORT_SYMBOL_GPL(tcpm_unregister_port);
6660 MODULE_AUTHOR("Guenter Roeck <groeck@chromium.org>");
6661 MODULE_DESCRIPTION("USB Type-C Port Manager");
6662 MODULE_LICENSE("GPL");