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 (IS_ERR_OR_NULL(port->partner))
1631 if (PD_VDO_SVDM_VER(p[0]) < svdm_version) {
1632 typec_partner_set_svdm_version(port->partner,
1633 PD_VDO_SVDM_VER(p[0]));
1634 svdm_version = PD_VDO_SVDM_VER(p[0]);
1637 port->ams = DISCOVER_IDENTITY;
1639 * PD2.0 Spec 6.10.3: respond with NAK as DFP (data host)
1640 * PD3.1 Spec 6.4.4.2.5.1: respond with NAK if "invalid field" or
1641 * "wrong configuation" or "Unrecognized"
1643 if ((port->data_role == TYPEC_DEVICE || svdm_version >= SVDM_VER_2_0) &&
1645 if (svdm_version < SVDM_VER_2_0) {
1646 for (i = 0; i < port->nr_snk_vdo_v1; i++)
1647 response[i + 1] = port->snk_vdo_v1[i];
1648 rlen = port->nr_snk_vdo_v1 + 1;
1651 for (i = 0; i < port->nr_snk_vdo; i++)
1652 response[i + 1] = port->snk_vdo[i];
1653 rlen = port->nr_snk_vdo + 1;
1657 case CMD_DISCOVER_SVID:
1658 port->ams = DISCOVER_SVIDS;
1660 case CMD_DISCOVER_MODES:
1661 port->ams = DISCOVER_MODES;
1663 case CMD_ENTER_MODE:
1664 port->ams = DFP_TO_UFP_ENTER_MODE;
1667 port->ams = DFP_TO_UFP_EXIT_MODE;
1670 /* Attention command does not have response */
1671 *adev_action = ADEV_ATTENTION;
1677 response[0] = p[0] | VDO_CMDT(CMDT_RSP_ACK);
1678 } else if (rlen == 0) {
1679 response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK);
1682 response[0] = p[0] | VDO_CMDT(CMDT_RSP_BUSY);
1685 response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) |
1686 (VDO_SVDM_VERS(typec_get_negotiated_svdm_version(typec)));
1689 /* silently drop message if we are not connected */
1690 if (IS_ERR_OR_NULL(port->partner))
1693 tcpm_ams_finish(port);
1696 case CMD_DISCOVER_IDENT:
1697 if (PD_VDO_SVDM_VER(p[0]) < svdm_version)
1698 typec_partner_set_svdm_version(port->partner,
1699 PD_VDO_SVDM_VER(p[0]));
1701 svdm_consume_identity(port, p, cnt);
1702 response[0] = VDO(USB_SID_PD, 1, typec_get_negotiated_svdm_version(typec),
1706 case CMD_DISCOVER_SVID:
1708 if (svdm_consume_svids(port, p, cnt)) {
1709 response[0] = VDO(USB_SID_PD, 1, svdm_version, CMD_DISCOVER_SVID);
1711 } else if (modep->nsvids && supports_modal(port)) {
1712 response[0] = VDO(modep->svids[0], 1, svdm_version,
1713 CMD_DISCOVER_MODES);
1717 case CMD_DISCOVER_MODES:
1719 svdm_consume_modes(port, p, cnt);
1720 modep->svid_index++;
1721 if (modep->svid_index < modep->nsvids) {
1722 u16 svid = modep->svids[modep->svid_index];
1723 response[0] = VDO(svid, 1, svdm_version, CMD_DISCOVER_MODES);
1726 tcpm_register_partner_altmodes(port);
1729 case CMD_ENTER_MODE:
1731 *adev_action = ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL;
1735 /* Back to USB Operation */
1736 *adev_action = ADEV_NOTIFY_USB_AND_QUEUE_VDM;
1740 case VDO_CMD_VENDOR(0) ... VDO_CMD_VENDOR(15):
1743 /* Unrecognized SVDM */
1744 response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK);
1746 response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) |
1747 (VDO_SVDM_VERS(svdm_version));
1752 tcpm_ams_finish(port);
1754 case CMD_DISCOVER_IDENT:
1755 case CMD_DISCOVER_SVID:
1756 case CMD_DISCOVER_MODES:
1757 case VDO_CMD_VENDOR(0) ... VDO_CMD_VENDOR(15):
1759 case CMD_ENTER_MODE:
1760 /* Back to USB Operation */
1761 *adev_action = ADEV_NOTIFY_USB_AND_QUEUE_VDM;
1764 /* Unrecognized SVDM */
1765 response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK);
1767 response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) |
1768 (VDO_SVDM_VERS(svdm_version));
1773 response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK);
1775 response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) |
1776 (VDO_SVDM_VERS(svdm_version));
1780 /* Informing the alternate mode drivers about everything */
1781 *adev_action = ADEV_QUEUE_VDM;
1785 static void tcpm_pd_handle_msg(struct tcpm_port *port,
1786 enum pd_msg_request message,
1789 static void tcpm_handle_vdm_request(struct tcpm_port *port,
1790 const __le32 *payload, int cnt)
1792 enum adev_actions adev_action = ADEV_NONE;
1793 struct typec_altmode *adev;
1794 u32 p[PD_MAX_PAYLOAD];
1795 u32 response[8] = { };
1798 for (i = 0; i < cnt; i++)
1799 p[i] = le32_to_cpu(payload[i]);
1801 adev = typec_match_altmode(port->port_altmode, ALTMODE_DISCOVERY_MAX,
1802 PD_VDO_VID(p[0]), PD_VDO_OPOS(p[0]));
1804 if (port->vdm_state == VDM_STATE_BUSY) {
1805 /* If UFP responded busy retry after timeout */
1806 if (PD_VDO_CMDT(p[0]) == CMDT_RSP_BUSY) {
1807 port->vdm_state = VDM_STATE_WAIT_RSP_BUSY;
1808 port->vdo_retry = (p[0] & ~VDO_CMDT_MASK) |
1810 mod_vdm_delayed_work(port, PD_T_VDM_BUSY);
1813 port->vdm_state = VDM_STATE_DONE;
1816 if (PD_VDO_SVDM(p[0]) && (adev || tcpm_vdm_ams(port) || port->nr_snk_vdo)) {
1818 * Here a SVDM is received (INIT or RSP or unknown). Set the vdm_sm_running in
1819 * advance because we are dropping the lock but may send VDMs soon.
1820 * For the cases of INIT received:
1821 * - If no response to send, it will be cleared later in this function.
1822 * - If there are responses to send, it will be cleared in the state machine.
1823 * For the cases of RSP received:
1824 * - If no further INIT to send, it will be cleared later in this function.
1825 * - Otherwise, it will be cleared in the state machine if timeout or it will go
1826 * back here until no further INIT to send.
1827 * For the cases of unknown type received:
1828 * - We will send NAK and the flag will be cleared in the state machine.
1830 port->vdm_sm_running = true;
1831 rlen = tcpm_pd_svdm(port, adev, p, cnt, response, &adev_action);
1833 if (port->negotiated_rev >= PD_REV30)
1834 tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS);
1838 * We are done with any state stored in the port struct now, except
1839 * for any port struct changes done by the tcpm_queue_vdm() call
1840 * below, which is a separate operation.
1842 * So we can safely release the lock here; and we MUST release the
1843 * lock here to avoid an AB BA lock inversion:
1845 * If we keep the lock here then the lock ordering in this path is:
1846 * 1. tcpm_pd_rx_handler take the tcpm port lock
1847 * 2. One of the typec_altmode_* calls below takes the alt-mode's lock
1849 * And we also have this ordering:
1850 * 1. alt-mode driver takes the alt-mode's lock
1851 * 2. alt-mode driver calls tcpm_altmode_enter which takes the
1854 * Dropping our lock here avoids this.
1856 mutex_unlock(&port->lock);
1859 switch (adev_action) {
1862 case ADEV_NOTIFY_USB_AND_QUEUE_VDM:
1863 WARN_ON(typec_altmode_notify(adev, TYPEC_STATE_USB, NULL));
1864 typec_altmode_vdm(adev, p[0], &p[1], cnt);
1866 case ADEV_QUEUE_VDM:
1867 typec_altmode_vdm(adev, p[0], &p[1], cnt);
1869 case ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL:
1870 if (typec_altmode_vdm(adev, p[0], &p[1], cnt)) {
1871 int svdm_version = typec_get_negotiated_svdm_version(
1873 if (svdm_version < 0)
1876 response[0] = VDO(adev->svid, 1, svdm_version,
1878 response[0] |= VDO_OPOS(adev->mode);
1882 case ADEV_ATTENTION:
1883 if (typec_altmode_attention(adev, p[1]))
1884 tcpm_log(port, "typec_altmode_attention no port partner altmode");
1890 * We must re-take the lock here to balance the unlock in
1891 * tcpm_pd_rx_handler, note that no changes, other then the
1892 * tcpm_queue_vdm call, are made while the lock is held again.
1893 * All that is done after the call is unwinding the call stack until
1894 * we return to tcpm_pd_rx_handler and do the unlock there.
1896 mutex_lock(&port->lock);
1899 tcpm_queue_vdm(port, response[0], &response[1], rlen - 1);
1901 port->vdm_sm_running = false;
1904 static void tcpm_send_vdm(struct tcpm_port *port, u32 vid, int cmd,
1905 const u32 *data, int count)
1907 int svdm_version = typec_get_negotiated_svdm_version(port->typec_port);
1910 if (svdm_version < 0)
1913 if (WARN_ON(count > VDO_MAX_SIZE - 1))
1914 count = VDO_MAX_SIZE - 1;
1916 /* set VDM header with VID & CMD */
1917 header = VDO(vid, ((vid & USB_SID_PD) == USB_SID_PD) ?
1918 1 : (PD_VDO_CMD(cmd) <= CMD_ATTENTION),
1920 tcpm_queue_vdm(port, header, data, count);
1923 static unsigned int vdm_ready_timeout(u32 vdm_hdr)
1925 unsigned int timeout;
1926 int cmd = PD_VDO_CMD(vdm_hdr);
1928 /* its not a structured VDM command */
1929 if (!PD_VDO_SVDM(vdm_hdr))
1930 return PD_T_VDM_UNSTRUCTURED;
1932 switch (PD_VDO_CMDT(vdm_hdr)) {
1934 if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE)
1935 timeout = PD_T_VDM_WAIT_MODE_E;
1937 timeout = PD_T_VDM_SNDR_RSP;
1940 if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE)
1941 timeout = PD_T_VDM_E_MODE;
1943 timeout = PD_T_VDM_RCVR_RSP;
1949 static void vdm_run_state_machine(struct tcpm_port *port)
1951 struct pd_message msg;
1953 u32 vdo_hdr = port->vdo_data[0];
1955 switch (port->vdm_state) {
1956 case VDM_STATE_READY:
1957 /* Only transmit VDM if attached */
1958 if (!port->attached) {
1959 port->vdm_state = VDM_STATE_ERR_BUSY;
1964 * if there's traffic or we're not in PDO ready state don't send
1967 if (port->state != SRC_READY && port->state != SNK_READY) {
1968 port->vdm_sm_running = false;
1972 /* TODO: AMS operation for Unstructured VDM */
1973 if (PD_VDO_SVDM(vdo_hdr) && PD_VDO_CMDT(vdo_hdr) == CMDT_INIT) {
1974 switch (PD_VDO_CMD(vdo_hdr)) {
1975 case CMD_DISCOVER_IDENT:
1976 res = tcpm_ams_start(port, DISCOVER_IDENTITY);
1978 port->send_discover = false;
1979 } else if (res == -EAGAIN) {
1980 port->vdo_data[0] = 0;
1981 mod_send_discover_delayed_work(port,
1982 SEND_DISCOVER_RETRY_MS);
1985 case CMD_DISCOVER_SVID:
1986 res = tcpm_ams_start(port, DISCOVER_SVIDS);
1988 case CMD_DISCOVER_MODES:
1989 res = tcpm_ams_start(port, DISCOVER_MODES);
1991 case CMD_ENTER_MODE:
1992 res = tcpm_ams_start(port, DFP_TO_UFP_ENTER_MODE);
1995 res = tcpm_ams_start(port, DFP_TO_UFP_EXIT_MODE);
1998 res = tcpm_ams_start(port, ATTENTION);
2000 case VDO_CMD_VENDOR(0) ... VDO_CMD_VENDOR(15):
2001 res = tcpm_ams_start(port, STRUCTURED_VDMS);
2009 port->vdm_state = VDM_STATE_ERR_BUSY;
2014 port->vdm_state = VDM_STATE_SEND_MESSAGE;
2015 mod_vdm_delayed_work(port, (port->negotiated_rev >= PD_REV30 &&
2016 port->pwr_role == TYPEC_SOURCE &&
2017 PD_VDO_SVDM(vdo_hdr) &&
2018 PD_VDO_CMDT(vdo_hdr) == CMDT_INIT) ?
2021 case VDM_STATE_WAIT_RSP_BUSY:
2022 port->vdo_data[0] = port->vdo_retry;
2023 port->vdo_count = 1;
2024 port->vdm_state = VDM_STATE_READY;
2025 tcpm_ams_finish(port);
2027 case VDM_STATE_BUSY:
2028 port->vdm_state = VDM_STATE_ERR_TMOUT;
2029 if (port->ams != NONE_AMS)
2030 tcpm_ams_finish(port);
2032 case VDM_STATE_ERR_SEND:
2034 * A partner which does not support USB PD will not reply,
2035 * so this is not a fatal error. At the same time, some
2036 * devices may not return GoodCRC under some circumstances,
2037 * so we need to retry.
2039 if (port->vdm_retries < 3) {
2040 tcpm_log(port, "VDM Tx error, retry");
2041 port->vdm_retries++;
2042 port->vdm_state = VDM_STATE_READY;
2043 if (PD_VDO_SVDM(vdo_hdr) && PD_VDO_CMDT(vdo_hdr) == CMDT_INIT)
2044 tcpm_ams_finish(port);
2046 tcpm_ams_finish(port);
2049 case VDM_STATE_SEND_MESSAGE:
2050 /* Prepare and send VDM */
2051 memset(&msg, 0, sizeof(msg));
2052 msg.header = PD_HEADER_LE(PD_DATA_VENDOR_DEF,
2055 port->negotiated_rev,
2056 port->message_id, port->vdo_count);
2057 for (i = 0; i < port->vdo_count; i++)
2058 msg.payload[i] = cpu_to_le32(port->vdo_data[i]);
2059 res = tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
2061 port->vdm_state = VDM_STATE_ERR_SEND;
2063 unsigned long timeout;
2065 port->vdm_retries = 0;
2066 port->vdo_data[0] = 0;
2067 port->vdm_state = VDM_STATE_BUSY;
2068 timeout = vdm_ready_timeout(vdo_hdr);
2069 mod_vdm_delayed_work(port, timeout);
2077 static void vdm_state_machine_work(struct kthread_work *work)
2079 struct tcpm_port *port = container_of(work, struct tcpm_port, vdm_state_machine);
2080 enum vdm_states prev_state;
2082 mutex_lock(&port->lock);
2085 * Continue running as long as the port is not busy and there was
2089 prev_state = port->vdm_state;
2090 vdm_run_state_machine(port);
2091 } while (port->vdm_state != prev_state &&
2092 port->vdm_state != VDM_STATE_BUSY &&
2093 port->vdm_state != VDM_STATE_SEND_MESSAGE);
2095 if (port->vdm_state < VDM_STATE_READY)
2096 port->vdm_sm_running = false;
2098 mutex_unlock(&port->lock);
2104 PDO_ERR_VSAFE5V_NOT_FIRST,
2105 PDO_ERR_PDO_TYPE_NOT_IN_ORDER,
2106 PDO_ERR_FIXED_NOT_SORTED,
2107 PDO_ERR_VARIABLE_BATT_NOT_SORTED,
2109 PDO_ERR_PPS_APDO_NOT_SORTED,
2110 PDO_ERR_DUPE_PPS_APDO,
2113 static const char * const pdo_err_msg[] = {
2114 [PDO_ERR_NO_VSAFE5V] =
2115 " err: source/sink caps should at least have vSafe5V",
2116 [PDO_ERR_VSAFE5V_NOT_FIRST] =
2117 " err: vSafe5V Fixed Supply Object Shall always be the first object",
2118 [PDO_ERR_PDO_TYPE_NOT_IN_ORDER] =
2119 " err: PDOs should be in the following order: Fixed; Battery; Variable",
2120 [PDO_ERR_FIXED_NOT_SORTED] =
2121 " err: Fixed supply pdos should be in increasing order of their fixed voltage",
2122 [PDO_ERR_VARIABLE_BATT_NOT_SORTED] =
2123 " err: Variable/Battery supply pdos should be in increasing order of their minimum voltage",
2124 [PDO_ERR_DUPE_PDO] =
2125 " err: Variable/Batt supply pdos cannot have same min/max voltage",
2126 [PDO_ERR_PPS_APDO_NOT_SORTED] =
2127 " err: Programmable power supply apdos should be in increasing order of their maximum voltage",
2128 [PDO_ERR_DUPE_PPS_APDO] =
2129 " err: Programmable power supply apdos cannot have same min/max voltage and max current",
2132 static enum pdo_err tcpm_caps_err(struct tcpm_port *port, const u32 *pdo,
2133 unsigned int nr_pdo)
2137 /* Should at least contain vSafe5v */
2139 return PDO_ERR_NO_VSAFE5V;
2141 /* The vSafe5V Fixed Supply Object Shall always be the first object */
2142 if (pdo_type(pdo[0]) != PDO_TYPE_FIXED ||
2143 pdo_fixed_voltage(pdo[0]) != VSAFE5V)
2144 return PDO_ERR_VSAFE5V_NOT_FIRST;
2146 for (i = 1; i < nr_pdo; i++) {
2147 if (pdo_type(pdo[i]) < pdo_type(pdo[i - 1])) {
2148 return PDO_ERR_PDO_TYPE_NOT_IN_ORDER;
2149 } else if (pdo_type(pdo[i]) == pdo_type(pdo[i - 1])) {
2150 enum pd_pdo_type type = pdo_type(pdo[i]);
2154 * The remaining Fixed Supply Objects, if
2155 * present, shall be sent in voltage order;
2156 * lowest to highest.
2158 case PDO_TYPE_FIXED:
2159 if (pdo_fixed_voltage(pdo[i]) <=
2160 pdo_fixed_voltage(pdo[i - 1]))
2161 return PDO_ERR_FIXED_NOT_SORTED;
2164 * The Battery Supply Objects and Variable
2165 * supply, if present shall be sent in Minimum
2166 * Voltage order; lowest to highest.
2170 if (pdo_min_voltage(pdo[i]) <
2171 pdo_min_voltage(pdo[i - 1]))
2172 return PDO_ERR_VARIABLE_BATT_NOT_SORTED;
2173 else if ((pdo_min_voltage(pdo[i]) ==
2174 pdo_min_voltage(pdo[i - 1])) &&
2175 (pdo_max_voltage(pdo[i]) ==
2176 pdo_max_voltage(pdo[i - 1])))
2177 return PDO_ERR_DUPE_PDO;
2180 * The Programmable Power Supply APDOs, if present,
2181 * shall be sent in Maximum Voltage order;
2182 * lowest to highest.
2185 if (pdo_apdo_type(pdo[i]) != APDO_TYPE_PPS)
2188 if (pdo_pps_apdo_max_voltage(pdo[i]) <
2189 pdo_pps_apdo_max_voltage(pdo[i - 1]))
2190 return PDO_ERR_PPS_APDO_NOT_SORTED;
2191 else if (pdo_pps_apdo_min_voltage(pdo[i]) ==
2192 pdo_pps_apdo_min_voltage(pdo[i - 1]) &&
2193 pdo_pps_apdo_max_voltage(pdo[i]) ==
2194 pdo_pps_apdo_max_voltage(pdo[i - 1]) &&
2195 pdo_pps_apdo_max_current(pdo[i]) ==
2196 pdo_pps_apdo_max_current(pdo[i - 1]))
2197 return PDO_ERR_DUPE_PPS_APDO;
2200 tcpm_log_force(port, " Unknown pdo type");
2208 static int tcpm_validate_caps(struct tcpm_port *port, const u32 *pdo,
2209 unsigned int nr_pdo)
2211 enum pdo_err err_index = tcpm_caps_err(port, pdo, nr_pdo);
2213 if (err_index != PDO_NO_ERR) {
2214 tcpm_log_force(port, " %s", pdo_err_msg[err_index]);
2221 static int tcpm_altmode_enter(struct typec_altmode *altmode, u32 *vdo)
2223 struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
2227 svdm_version = typec_get_negotiated_svdm_version(port->typec_port);
2228 if (svdm_version < 0)
2229 return svdm_version;
2231 header = VDO(altmode->svid, vdo ? 2 : 1, svdm_version, CMD_ENTER_MODE);
2232 header |= VDO_OPOS(altmode->mode);
2234 tcpm_queue_vdm_unlocked(port, header, vdo, vdo ? 1 : 0);
2238 static int tcpm_altmode_exit(struct typec_altmode *altmode)
2240 struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
2244 svdm_version = typec_get_negotiated_svdm_version(port->typec_port);
2245 if (svdm_version < 0)
2246 return svdm_version;
2248 header = VDO(altmode->svid, 1, svdm_version, CMD_EXIT_MODE);
2249 header |= VDO_OPOS(altmode->mode);
2251 tcpm_queue_vdm_unlocked(port, header, NULL, 0);
2255 static int tcpm_altmode_vdm(struct typec_altmode *altmode,
2256 u32 header, const u32 *data, int count)
2258 struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
2260 tcpm_queue_vdm_unlocked(port, header, data, count - 1);
2265 static const struct typec_altmode_ops tcpm_altmode_ops = {
2266 .enter = tcpm_altmode_enter,
2267 .exit = tcpm_altmode_exit,
2268 .vdm = tcpm_altmode_vdm,
2272 * PD (data, control) command handling functions
2274 static inline enum tcpm_state ready_state(struct tcpm_port *port)
2276 if (port->pwr_role == TYPEC_SOURCE)
2282 static int tcpm_pd_send_control(struct tcpm_port *port,
2283 enum pd_ctrl_msg_type type);
2285 static void tcpm_handle_alert(struct tcpm_port *port, const __le32 *payload,
2288 u32 p0 = le32_to_cpu(payload[0]);
2289 unsigned int type = usb_pd_ado_type(p0);
2292 tcpm_log(port, "Alert message received with no type");
2293 tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP);
2297 /* Just handling non-battery alerts for now */
2298 if (!(type & USB_PD_ADO_TYPE_BATT_STATUS_CHANGE)) {
2299 if (port->pwr_role == TYPEC_SOURCE) {
2300 port->upcoming_state = GET_STATUS_SEND;
2301 tcpm_ams_start(port, GETTING_SOURCE_SINK_STATUS);
2304 * Do not check SinkTxOk here in case the Source doesn't set its Rp to
2307 port->ams = GETTING_SOURCE_SINK_STATUS;
2308 tcpm_set_state(port, GET_STATUS_SEND, 0);
2311 tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP);
2315 static int tcpm_set_auto_vbus_discharge_threshold(struct tcpm_port *port,
2316 enum typec_pwr_opmode mode, bool pps_active,
2317 u32 requested_vbus_voltage)
2321 if (!port->tcpc->set_auto_vbus_discharge_threshold)
2324 ret = port->tcpc->set_auto_vbus_discharge_threshold(port->tcpc, mode, pps_active,
2325 requested_vbus_voltage);
2326 tcpm_log_force(port,
2327 "set_auto_vbus_discharge_threshold mode:%d pps_active:%c vbus:%u ret:%d",
2328 mode, pps_active ? 'y' : 'n', requested_vbus_voltage, ret);
2333 static void tcpm_pd_handle_state(struct tcpm_port *port,
2334 enum tcpm_state state,
2336 unsigned int delay_ms)
2338 switch (port->state) {
2342 tcpm_set_state(port, state, delay_ms);
2344 /* 8.3.3.4.1.1 and 6.8.1 power transitioning */
2345 case SNK_TRANSITION_SINK:
2346 case SNK_TRANSITION_SINK_VBUS:
2347 case SRC_TRANSITION_SUPPLY:
2348 tcpm_set_state(port, HARD_RESET_SEND, 0);
2351 if (!tcpm_ams_interruptible(port)) {
2352 tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ?
2353 SRC_SOFT_RESET_WAIT_SNK_TX :
2357 /* process the Message 6.8.1 */
2358 port->upcoming_state = state;
2359 port->next_ams = ams;
2360 tcpm_set_state(port, ready_state(port), delay_ms);
2366 static void tcpm_pd_handle_msg(struct tcpm_port *port,
2367 enum pd_msg_request message,
2370 switch (port->state) {
2374 tcpm_queue_message(port, message);
2376 /* PD 3.0 Spec 8.3.3.4.1.1 and 6.8.1 */
2377 case SNK_TRANSITION_SINK:
2378 case SNK_TRANSITION_SINK_VBUS:
2379 case SRC_TRANSITION_SUPPLY:
2380 tcpm_set_state(port, HARD_RESET_SEND, 0);
2383 if (!tcpm_ams_interruptible(port)) {
2384 tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ?
2385 SRC_SOFT_RESET_WAIT_SNK_TX :
2389 port->next_ams = ams;
2390 tcpm_set_state(port, ready_state(port), 0);
2391 /* 6.8.1 process the Message */
2392 tcpm_queue_message(port, message);
2398 static int tcpm_register_source_caps(struct tcpm_port *port)
2400 struct usb_power_delivery_desc desc = { port->negotiated_rev };
2401 struct usb_power_delivery_capabilities_desc caps = { };
2402 struct usb_power_delivery_capabilities *cap;
2404 if (!port->partner_pd)
2405 port->partner_pd = usb_power_delivery_register(NULL, &desc);
2406 if (IS_ERR(port->partner_pd))
2407 return PTR_ERR(port->partner_pd);
2409 memcpy(caps.pdo, port->source_caps, sizeof(u32) * port->nr_source_caps);
2410 caps.role = TYPEC_SOURCE;
2412 cap = usb_power_delivery_register_capabilities(port->partner_pd, &caps);
2414 return PTR_ERR(cap);
2416 port->partner_source_caps = cap;
2421 static int tcpm_register_sink_caps(struct tcpm_port *port)
2423 struct usb_power_delivery_desc desc = { port->negotiated_rev };
2424 struct usb_power_delivery_capabilities_desc caps = { };
2425 struct usb_power_delivery_capabilities *cap;
2427 if (!port->partner_pd)
2428 port->partner_pd = usb_power_delivery_register(NULL, &desc);
2429 if (IS_ERR(port->partner_pd))
2430 return PTR_ERR(port->partner_pd);
2432 memcpy(caps.pdo, port->sink_caps, sizeof(u32) * port->nr_sink_caps);
2433 caps.role = TYPEC_SINK;
2435 cap = usb_power_delivery_register_capabilities(port->partner_pd, &caps);
2437 return PTR_ERR(cap);
2439 port->partner_sink_caps = cap;
2444 static void tcpm_pd_data_request(struct tcpm_port *port,
2445 const struct pd_message *msg)
2447 enum pd_data_msg_type type = pd_header_type_le(msg->header);
2448 unsigned int cnt = pd_header_cnt_le(msg->header);
2449 unsigned int rev = pd_header_rev_le(msg->header);
2451 enum frs_typec_current partner_frs_current;
2455 if (tcpm_vdm_ams(port) && type != PD_DATA_VENDOR_DEF) {
2456 port->vdm_state = VDM_STATE_ERR_BUSY;
2457 tcpm_ams_finish(port);
2458 mod_vdm_delayed_work(port, 0);
2462 case PD_DATA_SOURCE_CAP:
2463 for (i = 0; i < cnt; i++)
2464 port->source_caps[i] = le32_to_cpu(msg->payload[i]);
2466 port->nr_source_caps = cnt;
2468 tcpm_log_source_caps(port);
2470 tcpm_validate_caps(port, port->source_caps,
2471 port->nr_source_caps);
2473 tcpm_register_source_caps(port);
2476 * Adjust revision in subsequent message headers, as required,
2477 * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't
2478 * support Rev 1.0 so just do nothing in that scenario.
2480 if (rev == PD_REV10) {
2481 if (port->ams == GET_SOURCE_CAPABILITIES)
2482 tcpm_ams_finish(port);
2486 if (rev < PD_MAX_REV)
2487 port->negotiated_rev = rev;
2489 if (port->pwr_role == TYPEC_SOURCE) {
2490 if (port->ams == GET_SOURCE_CAPABILITIES)
2491 tcpm_pd_handle_state(port, SRC_READY, NONE_AMS, 0);
2492 /* Unexpected Source Capabilities */
2494 tcpm_pd_handle_msg(port,
2495 port->negotiated_rev < PD_REV30 ?
2496 PD_MSG_CTRL_REJECT :
2497 PD_MSG_CTRL_NOT_SUPP,
2499 } else if (port->state == SNK_WAIT_CAPABILITIES) {
2501 * This message may be received even if VBUS is not
2502 * present. This is quite unexpected; see USB PD
2503 * specification, sections 8.3.3.6.3.1 and 8.3.3.6.3.2.
2504 * However, at the same time, we must be ready to
2505 * receive this message and respond to it 15ms after
2506 * receiving PS_RDY during power swap operations, no matter
2507 * if VBUS is available or not (USB PD specification,
2509 * So we need to accept the message either way,
2510 * but be prepared to keep waiting for VBUS after it was
2513 port->ams = POWER_NEGOTIATION;
2514 port->in_ams = true;
2515 tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
2517 if (port->ams == GET_SOURCE_CAPABILITIES)
2518 tcpm_ams_finish(port);
2519 tcpm_pd_handle_state(port, SNK_NEGOTIATE_CAPABILITIES,
2520 POWER_NEGOTIATION, 0);
2523 case PD_DATA_REQUEST:
2525 * Adjust revision in subsequent message headers, as required,
2526 * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't
2527 * support Rev 1.0 so just reject in that scenario.
2529 if (rev == PD_REV10) {
2530 tcpm_pd_handle_msg(port,
2531 port->negotiated_rev < PD_REV30 ?
2532 PD_MSG_CTRL_REJECT :
2533 PD_MSG_CTRL_NOT_SUPP,
2538 if (rev < PD_MAX_REV)
2539 port->negotiated_rev = rev;
2541 if (port->pwr_role != TYPEC_SOURCE || cnt != 1) {
2542 tcpm_pd_handle_msg(port,
2543 port->negotiated_rev < PD_REV30 ?
2544 PD_MSG_CTRL_REJECT :
2545 PD_MSG_CTRL_NOT_SUPP,
2550 port->sink_request = le32_to_cpu(msg->payload[0]);
2552 if (port->vdm_sm_running && port->explicit_contract) {
2553 tcpm_pd_handle_msg(port, PD_MSG_CTRL_WAIT, port->ams);
2557 if (port->state == SRC_SEND_CAPABILITIES)
2558 tcpm_set_state(port, SRC_NEGOTIATE_CAPABILITIES, 0);
2560 tcpm_pd_handle_state(port, SRC_NEGOTIATE_CAPABILITIES,
2561 POWER_NEGOTIATION, 0);
2563 case PD_DATA_SINK_CAP:
2564 /* We don't do anything with this at the moment... */
2565 for (i = 0; i < cnt; i++)
2566 port->sink_caps[i] = le32_to_cpu(msg->payload[i]);
2568 partner_frs_current = (port->sink_caps[0] & PDO_FIXED_FRS_CURR_MASK) >>
2569 PDO_FIXED_FRS_CURR_SHIFT;
2570 frs_enable = partner_frs_current && (partner_frs_current <=
2571 port->new_source_frs_current);
2573 "Port partner FRS capable partner_frs_current:%u port_frs_current:%u enable:%c",
2574 partner_frs_current, port->new_source_frs_current, frs_enable ? 'y' : 'n');
2576 ret = port->tcpc->enable_frs(port->tcpc, true);
2577 tcpm_log(port, "Enable FRS %s, ret:%d\n", ret ? "fail" : "success", ret);
2580 port->nr_sink_caps = cnt;
2581 port->sink_cap_done = true;
2582 tcpm_register_sink_caps(port);
2584 if (port->ams == GET_SINK_CAPABILITIES)
2585 tcpm_set_state(port, ready_state(port), 0);
2586 /* Unexpected Sink Capabilities */
2588 tcpm_pd_handle_msg(port,
2589 port->negotiated_rev < PD_REV30 ?
2590 PD_MSG_CTRL_REJECT :
2591 PD_MSG_CTRL_NOT_SUPP,
2594 case PD_DATA_VENDOR_DEF:
2595 tcpm_handle_vdm_request(port, msg->payload, cnt);
2598 port->bist_request = le32_to_cpu(msg->payload[0]);
2599 tcpm_pd_handle_state(port, BIST_RX, BIST, 0);
2602 if (port->state != SRC_READY && port->state != SNK_READY)
2603 tcpm_pd_handle_state(port, port->pwr_role == TYPEC_SOURCE ?
2604 SRC_SOFT_RESET_WAIT_SNK_TX : SNK_SOFT_RESET,
2607 tcpm_handle_alert(port, msg->payload, cnt);
2609 case PD_DATA_BATT_STATUS:
2610 case PD_DATA_GET_COUNTRY_INFO:
2611 /* Currently unsupported */
2612 tcpm_pd_handle_msg(port, port->negotiated_rev < PD_REV30 ?
2613 PD_MSG_CTRL_REJECT :
2614 PD_MSG_CTRL_NOT_SUPP,
2618 tcpm_pd_handle_msg(port, port->negotiated_rev < PD_REV30 ?
2619 PD_MSG_CTRL_REJECT :
2620 PD_MSG_CTRL_NOT_SUPP,
2622 tcpm_log(port, "Unrecognized data message type %#x", type);
2627 static void tcpm_pps_complete(struct tcpm_port *port, int result)
2629 if (port->pps_pending) {
2630 port->pps_status = result;
2631 port->pps_pending = false;
2632 complete(&port->pps_complete);
2636 static void tcpm_pd_ctrl_request(struct tcpm_port *port,
2637 const struct pd_message *msg)
2639 enum pd_ctrl_msg_type type = pd_header_type_le(msg->header);
2640 enum tcpm_state next_state;
2643 * Stop VDM state machine if interrupted by other Messages while NOT_SUPP is allowed in
2644 * VDM AMS if waiting for VDM responses and will be handled later.
2646 if (tcpm_vdm_ams(port) && type != PD_CTRL_NOT_SUPP && type != PD_CTRL_GOOD_CRC) {
2647 port->vdm_state = VDM_STATE_ERR_BUSY;
2648 tcpm_ams_finish(port);
2649 mod_vdm_delayed_work(port, 0);
2653 case PD_CTRL_GOOD_CRC:
2656 case PD_CTRL_GET_SOURCE_CAP:
2657 tcpm_pd_handle_msg(port, PD_MSG_DATA_SOURCE_CAP, GET_SOURCE_CAPABILITIES);
2659 case PD_CTRL_GET_SINK_CAP:
2660 tcpm_pd_handle_msg(port, PD_MSG_DATA_SINK_CAP, GET_SINK_CAPABILITIES);
2662 case PD_CTRL_GOTO_MIN:
2664 case PD_CTRL_PS_RDY:
2665 switch (port->state) {
2666 case SNK_TRANSITION_SINK:
2667 if (port->vbus_present) {
2668 tcpm_set_current_limit(port,
2669 port->req_current_limit,
2670 port->req_supply_voltage);
2671 port->explicit_contract = true;
2672 tcpm_set_auto_vbus_discharge_threshold(port,
2674 port->pps_data.active,
2675 port->supply_voltage);
2676 tcpm_set_state(port, SNK_READY, 0);
2679 * Seen after power swap. Keep waiting for VBUS
2680 * in a transitional state.
2682 tcpm_set_state(port,
2683 SNK_TRANSITION_SINK_VBUS, 0);
2686 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED:
2687 tcpm_set_state(port, PR_SWAP_SRC_SNK_SINK_ON, 0);
2689 case PR_SWAP_SNK_SRC_SINK_OFF:
2690 tcpm_set_state(port, PR_SWAP_SNK_SRC_SOURCE_ON, 0);
2692 case VCONN_SWAP_WAIT_FOR_VCONN:
2693 tcpm_set_state(port, VCONN_SWAP_TURN_OFF_VCONN, 0);
2695 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
2696 tcpm_set_state(port, FR_SWAP_SNK_SRC_NEW_SINK_READY, 0);
2699 tcpm_pd_handle_state(port,
2700 port->pwr_role == TYPEC_SOURCE ?
2701 SRC_SOFT_RESET_WAIT_SNK_TX :
2707 case PD_CTRL_REJECT:
2709 case PD_CTRL_NOT_SUPP:
2710 switch (port->state) {
2711 case SNK_NEGOTIATE_CAPABILITIES:
2712 /* USB PD specification, Figure 8-43 */
2713 if (port->explicit_contract)
2714 next_state = SNK_READY;
2716 next_state = SNK_WAIT_CAPABILITIES;
2718 /* Threshold was relaxed before sending Request. Restore it back. */
2719 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
2720 port->pps_data.active,
2721 port->supply_voltage);
2722 tcpm_set_state(port, next_state, 0);
2724 case SNK_NEGOTIATE_PPS_CAPABILITIES:
2725 /* Revert data back from any requested PPS updates */
2726 port->pps_data.req_out_volt = port->supply_voltage;
2727 port->pps_data.req_op_curr = port->current_limit;
2728 port->pps_status = (type == PD_CTRL_WAIT ?
2729 -EAGAIN : -EOPNOTSUPP);
2731 /* Threshold was relaxed before sending Request. Restore it back. */
2732 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
2733 port->pps_data.active,
2734 port->supply_voltage);
2736 tcpm_set_state(port, SNK_READY, 0);
2739 port->swap_status = (type == PD_CTRL_WAIT ?
2740 -EAGAIN : -EOPNOTSUPP);
2741 tcpm_set_state(port, DR_SWAP_CANCEL, 0);
2744 port->swap_status = (type == PD_CTRL_WAIT ?
2745 -EAGAIN : -EOPNOTSUPP);
2746 tcpm_set_state(port, PR_SWAP_CANCEL, 0);
2748 case VCONN_SWAP_SEND:
2749 port->swap_status = (type == PD_CTRL_WAIT ?
2750 -EAGAIN : -EOPNOTSUPP);
2751 tcpm_set_state(port, VCONN_SWAP_CANCEL, 0);
2754 tcpm_set_state(port, FR_SWAP_CANCEL, 0);
2757 port->sink_cap_done = true;
2758 tcpm_set_state(port, ready_state(port), 0);
2761 * Some port partners do not support GET_STATUS, avoid soft reset the link to
2762 * prevent redundant power re-negotiation
2764 case GET_STATUS_SEND:
2765 tcpm_set_state(port, ready_state(port), 0);
2769 if (port->vdm_state > VDM_STATE_READY) {
2770 port->vdm_state = VDM_STATE_DONE;
2771 if (tcpm_vdm_ams(port))
2772 tcpm_ams_finish(port);
2773 mod_vdm_delayed_work(port, 0);
2778 tcpm_pd_handle_state(port,
2779 port->pwr_role == TYPEC_SOURCE ?
2780 SRC_SOFT_RESET_WAIT_SNK_TX :
2786 case PD_CTRL_ACCEPT:
2787 switch (port->state) {
2788 case SNK_NEGOTIATE_CAPABILITIES:
2789 port->pps_data.active = false;
2790 tcpm_set_state(port, SNK_TRANSITION_SINK, 0);
2792 case SNK_NEGOTIATE_PPS_CAPABILITIES:
2793 port->pps_data.active = true;
2794 port->pps_data.min_volt = port->pps_data.req_min_volt;
2795 port->pps_data.max_volt = port->pps_data.req_max_volt;
2796 port->pps_data.max_curr = port->pps_data.req_max_curr;
2797 port->req_supply_voltage = port->pps_data.req_out_volt;
2798 port->req_current_limit = port->pps_data.req_op_curr;
2799 power_supply_changed(port->psy);
2800 tcpm_set_state(port, SNK_TRANSITION_SINK, 0);
2802 case SOFT_RESET_SEND:
2803 if (port->ams == SOFT_RESET_AMS)
2804 tcpm_ams_finish(port);
2805 if (port->pwr_role == TYPEC_SOURCE) {
2806 port->upcoming_state = SRC_SEND_CAPABILITIES;
2807 tcpm_ams_start(port, POWER_NEGOTIATION);
2809 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
2813 tcpm_set_state(port, DR_SWAP_CHANGE_DR, 0);
2816 tcpm_set_state(port, PR_SWAP_START, 0);
2818 case VCONN_SWAP_SEND:
2819 tcpm_set_state(port, VCONN_SWAP_START, 0);
2822 tcpm_set_state(port, FR_SWAP_SNK_SRC_TRANSITION_TO_OFF, 0);
2825 tcpm_pd_handle_state(port,
2826 port->pwr_role == TYPEC_SOURCE ?
2827 SRC_SOFT_RESET_WAIT_SNK_TX :
2833 case PD_CTRL_SOFT_RESET:
2834 port->ams = SOFT_RESET_AMS;
2835 tcpm_set_state(port, SOFT_RESET, 0);
2837 case PD_CTRL_DR_SWAP:
2840 * 6.3.9: If an alternate mode is active, a request to swap
2841 * alternate modes shall trigger a port reset.
2843 if (port->typec_caps.data != TYPEC_PORT_DRD) {
2844 tcpm_pd_handle_msg(port,
2845 port->negotiated_rev < PD_REV30 ?
2846 PD_MSG_CTRL_REJECT :
2847 PD_MSG_CTRL_NOT_SUPP,
2850 if (port->send_discover) {
2851 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
2855 tcpm_pd_handle_state(port, DR_SWAP_ACCEPT, DATA_ROLE_SWAP, 0);
2858 case PD_CTRL_PR_SWAP:
2859 if (port->port_type != TYPEC_PORT_DRP) {
2860 tcpm_pd_handle_msg(port,
2861 port->negotiated_rev < PD_REV30 ?
2862 PD_MSG_CTRL_REJECT :
2863 PD_MSG_CTRL_NOT_SUPP,
2866 if (port->send_discover) {
2867 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
2871 tcpm_pd_handle_state(port, PR_SWAP_ACCEPT, POWER_ROLE_SWAP, 0);
2874 case PD_CTRL_VCONN_SWAP:
2875 if (port->send_discover) {
2876 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
2880 tcpm_pd_handle_state(port, VCONN_SWAP_ACCEPT, VCONN_SWAP, 0);
2882 case PD_CTRL_GET_SOURCE_CAP_EXT:
2883 case PD_CTRL_GET_STATUS:
2884 case PD_CTRL_FR_SWAP:
2885 case PD_CTRL_GET_PPS_STATUS:
2886 case PD_CTRL_GET_COUNTRY_CODES:
2887 /* Currently not supported */
2888 tcpm_pd_handle_msg(port,
2889 port->negotiated_rev < PD_REV30 ?
2890 PD_MSG_CTRL_REJECT :
2891 PD_MSG_CTRL_NOT_SUPP,
2895 tcpm_pd_handle_msg(port,
2896 port->negotiated_rev < PD_REV30 ?
2897 PD_MSG_CTRL_REJECT :
2898 PD_MSG_CTRL_NOT_SUPP,
2900 tcpm_log(port, "Unrecognized ctrl message type %#x", type);
2905 static void tcpm_pd_ext_msg_request(struct tcpm_port *port,
2906 const struct pd_message *msg)
2908 enum pd_ext_msg_type type = pd_header_type_le(msg->header);
2909 unsigned int data_size = pd_ext_header_data_size_le(msg->ext_msg.header);
2911 /* stopping VDM state machine if interrupted by other Messages */
2912 if (tcpm_vdm_ams(port)) {
2913 port->vdm_state = VDM_STATE_ERR_BUSY;
2914 tcpm_ams_finish(port);
2915 mod_vdm_delayed_work(port, 0);
2918 if (!(le16_to_cpu(msg->ext_msg.header) & PD_EXT_HDR_CHUNKED)) {
2919 tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS);
2920 tcpm_log(port, "Unchunked extended messages unsupported");
2924 if (data_size > PD_EXT_MAX_CHUNK_DATA) {
2925 tcpm_pd_handle_state(port, CHUNK_NOT_SUPP, NONE_AMS, PD_T_CHUNK_NOT_SUPP);
2926 tcpm_log(port, "Chunk handling not yet supported");
2932 case PD_EXT_PPS_STATUS:
2933 if (port->ams == GETTING_SOURCE_SINK_STATUS) {
2934 tcpm_ams_finish(port);
2935 tcpm_set_state(port, ready_state(port), 0);
2937 /* unexpected Status or PPS_Status Message */
2938 tcpm_pd_handle_state(port, port->pwr_role == TYPEC_SOURCE ?
2939 SRC_SOFT_RESET_WAIT_SNK_TX : SNK_SOFT_RESET,
2943 case PD_EXT_SOURCE_CAP_EXT:
2944 case PD_EXT_GET_BATT_CAP:
2945 case PD_EXT_GET_BATT_STATUS:
2946 case PD_EXT_BATT_CAP:
2947 case PD_EXT_GET_MANUFACTURER_INFO:
2948 case PD_EXT_MANUFACTURER_INFO:
2949 case PD_EXT_SECURITY_REQUEST:
2950 case PD_EXT_SECURITY_RESPONSE:
2951 case PD_EXT_FW_UPDATE_REQUEST:
2952 case PD_EXT_FW_UPDATE_RESPONSE:
2953 case PD_EXT_COUNTRY_INFO:
2954 case PD_EXT_COUNTRY_CODES:
2955 tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS);
2958 tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS);
2959 tcpm_log(port, "Unrecognized extended message type %#x", type);
2964 static void tcpm_pd_rx_handler(struct kthread_work *work)
2966 struct pd_rx_event *event = container_of(work,
2967 struct pd_rx_event, work);
2968 const struct pd_message *msg = &event->msg;
2969 unsigned int cnt = pd_header_cnt_le(msg->header);
2970 struct tcpm_port *port = event->port;
2972 mutex_lock(&port->lock);
2974 tcpm_log(port, "PD RX, header: %#x [%d]", le16_to_cpu(msg->header),
2977 if (port->attached) {
2978 enum pd_ctrl_msg_type type = pd_header_type_le(msg->header);
2979 unsigned int msgid = pd_header_msgid_le(msg->header);
2982 * USB PD standard, 6.6.1.2:
2983 * "... if MessageID value in a received Message is the
2984 * same as the stored value, the receiver shall return a
2985 * GoodCRC Message with that MessageID value and drop
2986 * the Message (this is a retry of an already received
2987 * Message). Note: this shall not apply to the Soft_Reset
2988 * Message which always has a MessageID value of zero."
2990 if (msgid == port->rx_msgid && type != PD_CTRL_SOFT_RESET)
2992 port->rx_msgid = msgid;
2995 * If both ends believe to be DFP/host, we have a data role
2998 if (!!(le16_to_cpu(msg->header) & PD_HEADER_DATA_ROLE) ==
2999 (port->data_role == TYPEC_HOST)) {
3001 "Data role mismatch, initiating error recovery");
3002 tcpm_set_state(port, ERROR_RECOVERY, 0);
3004 if (le16_to_cpu(msg->header) & PD_HEADER_EXT_HDR)
3005 tcpm_pd_ext_msg_request(port, msg);
3007 tcpm_pd_data_request(port, msg);
3009 tcpm_pd_ctrl_request(port, msg);
3014 mutex_unlock(&port->lock);
3018 void tcpm_pd_receive(struct tcpm_port *port, const struct pd_message *msg)
3020 struct pd_rx_event *event;
3022 event = kzalloc(sizeof(*event), GFP_ATOMIC);
3026 kthread_init_work(&event->work, tcpm_pd_rx_handler);
3028 memcpy(&event->msg, msg, sizeof(*msg));
3029 kthread_queue_work(port->wq, &event->work);
3031 EXPORT_SYMBOL_GPL(tcpm_pd_receive);
3033 static int tcpm_pd_send_control(struct tcpm_port *port,
3034 enum pd_ctrl_msg_type type)
3036 struct pd_message msg;
3038 memset(&msg, 0, sizeof(msg));
3039 msg.header = PD_HEADER_LE(type, port->pwr_role,
3041 port->negotiated_rev,
3042 port->message_id, 0);
3044 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
3048 * Send queued message without affecting state.
3049 * Return true if state machine should go back to sleep,
3052 static bool tcpm_send_queued_message(struct tcpm_port *port)
3054 enum pd_msg_request queued_message;
3058 queued_message = port->queued_message;
3059 port->queued_message = PD_MSG_NONE;
3061 switch (queued_message) {
3062 case PD_MSG_CTRL_WAIT:
3063 tcpm_pd_send_control(port, PD_CTRL_WAIT);
3065 case PD_MSG_CTRL_REJECT:
3066 tcpm_pd_send_control(port, PD_CTRL_REJECT);
3068 case PD_MSG_CTRL_NOT_SUPP:
3069 tcpm_pd_send_control(port, PD_CTRL_NOT_SUPP);
3071 case PD_MSG_DATA_SINK_CAP:
3072 ret = tcpm_pd_send_sink_caps(port);
3074 tcpm_log(port, "Unable to send snk caps, ret=%d", ret);
3075 tcpm_set_state(port, SNK_SOFT_RESET, 0);
3077 tcpm_ams_finish(port);
3079 case PD_MSG_DATA_SOURCE_CAP:
3080 ret = tcpm_pd_send_source_caps(port);
3083 "Unable to send src caps, ret=%d",
3085 tcpm_set_state(port, SOFT_RESET_SEND, 0);
3086 } else if (port->pwr_role == TYPEC_SOURCE) {
3087 tcpm_ams_finish(port);
3088 tcpm_set_state(port, HARD_RESET_SEND,
3089 PD_T_SENDER_RESPONSE);
3091 tcpm_ams_finish(port);
3097 } while (port->queued_message != PD_MSG_NONE);
3099 if (port->delayed_state != INVALID_STATE) {
3100 if (ktime_after(port->delayed_runtime, ktime_get())) {
3101 mod_tcpm_delayed_work(port, ktime_to_ms(ktime_sub(port->delayed_runtime,
3105 port->delayed_state = INVALID_STATE;
3110 static int tcpm_pd_check_request(struct tcpm_port *port)
3112 u32 pdo, rdo = port->sink_request;
3113 unsigned int max, op, pdo_max, index;
3114 enum pd_pdo_type type;
3116 index = rdo_index(rdo);
3117 if (!index || index > port->nr_src_pdo)
3120 pdo = port->src_pdo[index - 1];
3121 type = pdo_type(pdo);
3123 case PDO_TYPE_FIXED:
3125 max = rdo_max_current(rdo);
3126 op = rdo_op_current(rdo);
3127 pdo_max = pdo_max_current(pdo);
3131 if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH))
3134 if (type == PDO_TYPE_FIXED)
3136 "Requested %u mV, %u mA for %u / %u mA",
3137 pdo_fixed_voltage(pdo), pdo_max, op, max);
3140 "Requested %u -> %u mV, %u mA for %u / %u mA",
3141 pdo_min_voltage(pdo), pdo_max_voltage(pdo),
3145 max = rdo_max_power(rdo);
3146 op = rdo_op_power(rdo);
3147 pdo_max = pdo_max_power(pdo);
3151 if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH))
3154 "Requested %u -> %u mV, %u mW for %u / %u mW",
3155 pdo_min_voltage(pdo), pdo_max_voltage(pdo),
3162 port->op_vsafe5v = index == 1;
3167 #define min_power(x, y) min(pdo_max_power(x), pdo_max_power(y))
3168 #define min_current(x, y) min(pdo_max_current(x), pdo_max_current(y))
3170 static int tcpm_pd_select_pdo(struct tcpm_port *port, int *sink_pdo,
3173 unsigned int i, j, max_src_mv = 0, min_src_mv = 0, max_mw = 0,
3174 max_mv = 0, src_mw = 0, src_ma = 0, max_snk_mv = 0,
3178 port->pps_data.supported = false;
3179 port->usb_type = POWER_SUPPLY_USB_TYPE_PD;
3180 power_supply_changed(port->psy);
3183 * Select the source PDO providing the most power which has a
3186 for (i = 0; i < port->nr_source_caps; i++) {
3187 u32 pdo = port->source_caps[i];
3188 enum pd_pdo_type type = pdo_type(pdo);
3191 case PDO_TYPE_FIXED:
3192 max_src_mv = pdo_fixed_voltage(pdo);
3193 min_src_mv = max_src_mv;
3197 max_src_mv = pdo_max_voltage(pdo);
3198 min_src_mv = pdo_min_voltage(pdo);
3201 if (pdo_apdo_type(pdo) == APDO_TYPE_PPS) {
3202 port->pps_data.supported = true;
3204 POWER_SUPPLY_USB_TYPE_PD_PPS;
3205 power_supply_changed(port->psy);
3209 tcpm_log(port, "Invalid source PDO type, ignoring");
3214 case PDO_TYPE_FIXED:
3216 src_ma = pdo_max_current(pdo);
3217 src_mw = src_ma * min_src_mv / 1000;
3220 src_mw = pdo_max_power(pdo);
3225 tcpm_log(port, "Invalid source PDO type, ignoring");
3229 for (j = 0; j < port->nr_snk_pdo; j++) {
3230 pdo = port->snk_pdo[j];
3232 switch (pdo_type(pdo)) {
3233 case PDO_TYPE_FIXED:
3234 max_snk_mv = pdo_fixed_voltage(pdo);
3235 min_snk_mv = max_snk_mv;
3239 max_snk_mv = pdo_max_voltage(pdo);
3240 min_snk_mv = pdo_min_voltage(pdo);
3245 tcpm_log(port, "Invalid sink PDO type, ignoring");
3249 if (max_src_mv <= max_snk_mv &&
3250 min_src_mv >= min_snk_mv) {
3251 /* Prefer higher voltages if available */
3252 if ((src_mw == max_mw && min_src_mv > max_mv) ||
3257 max_mv = min_src_mv;
3267 static unsigned int tcpm_pd_select_pps_apdo(struct tcpm_port *port)
3269 unsigned int i, src_ma, max_temp_mw = 0, max_op_ma, op_mw;
3270 unsigned int src_pdo = 0;
3273 for (i = 1; i < port->nr_source_caps; ++i) {
3274 pdo = port->source_caps[i];
3276 switch (pdo_type(pdo)) {
3278 if (pdo_apdo_type(pdo) != APDO_TYPE_PPS) {
3279 tcpm_log(port, "Not PPS APDO (source), ignoring");
3283 if (port->pps_data.req_out_volt > pdo_pps_apdo_max_voltage(pdo) ||
3284 port->pps_data.req_out_volt < pdo_pps_apdo_min_voltage(pdo))
3287 src_ma = pdo_pps_apdo_max_current(pdo);
3288 max_op_ma = min(src_ma, port->pps_data.req_op_curr);
3289 op_mw = max_op_ma * port->pps_data.req_out_volt / 1000;
3290 if (op_mw > max_temp_mw) {
3292 max_temp_mw = op_mw;
3296 tcpm_log(port, "Not APDO type (source), ignoring");
3302 src = port->source_caps[src_pdo];
3304 port->pps_data.req_min_volt = pdo_pps_apdo_min_voltage(src);
3305 port->pps_data.req_max_volt = pdo_pps_apdo_max_voltage(src);
3306 port->pps_data.req_max_curr = pdo_pps_apdo_max_current(src);
3307 port->pps_data.req_op_curr = min(port->pps_data.req_max_curr,
3308 port->pps_data.req_op_curr);
3314 static int tcpm_pd_build_request(struct tcpm_port *port, u32 *rdo)
3316 unsigned int mv, ma, mw, flags;
3317 unsigned int max_ma, max_mw;
3318 enum pd_pdo_type type;
3319 u32 pdo, matching_snk_pdo;
3320 int src_pdo_index = 0;
3321 int snk_pdo_index = 0;
3324 ret = tcpm_pd_select_pdo(port, &snk_pdo_index, &src_pdo_index);
3328 pdo = port->source_caps[src_pdo_index];
3329 matching_snk_pdo = port->snk_pdo[snk_pdo_index];
3330 type = pdo_type(pdo);
3333 case PDO_TYPE_FIXED:
3334 mv = pdo_fixed_voltage(pdo);
3338 mv = pdo_min_voltage(pdo);
3341 tcpm_log(port, "Invalid PDO selected!");
3345 /* Select maximum available current within the sink pdo's limit */
3346 if (type == PDO_TYPE_BATT) {
3347 mw = min_power(pdo, matching_snk_pdo);
3348 ma = 1000 * mw / mv;
3350 ma = min_current(pdo, matching_snk_pdo);
3351 mw = ma * mv / 1000;
3354 flags = RDO_USB_COMM | RDO_NO_SUSPEND;
3356 /* Set mismatch bit if offered power is less than operating power */
3359 if (mw < port->operating_snk_mw) {
3360 flags |= RDO_CAP_MISMATCH;
3361 if (type == PDO_TYPE_BATT &&
3362 (pdo_max_power(matching_snk_pdo) > pdo_max_power(pdo)))
3363 max_mw = pdo_max_power(matching_snk_pdo);
3364 else if (pdo_max_current(matching_snk_pdo) >
3365 pdo_max_current(pdo))
3366 max_ma = pdo_max_current(matching_snk_pdo);
3369 tcpm_log(port, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d",
3370 port->cc_req, port->cc1, port->cc2, port->vbus_source,
3371 port->vconn_role == TYPEC_SOURCE ? "source" : "sink",
3374 if (type == PDO_TYPE_BATT) {
3375 *rdo = RDO_BATT(src_pdo_index + 1, mw, max_mw, flags);
3377 tcpm_log(port, "Requesting PDO %d: %u mV, %u mW%s",
3378 src_pdo_index, mv, mw,
3379 flags & RDO_CAP_MISMATCH ? " [mismatch]" : "");
3381 *rdo = RDO_FIXED(src_pdo_index + 1, ma, max_ma, flags);
3383 tcpm_log(port, "Requesting PDO %d: %u mV, %u mA%s",
3384 src_pdo_index, mv, ma,
3385 flags & RDO_CAP_MISMATCH ? " [mismatch]" : "");
3388 port->req_current_limit = ma;
3389 port->req_supply_voltage = mv;
3394 static int tcpm_pd_send_request(struct tcpm_port *port)
3396 struct pd_message msg;
3400 ret = tcpm_pd_build_request(port, &rdo);
3405 * Relax the threshold as voltage will be adjusted after Accept Message plus tSrcTransition.
3406 * It is safer to modify the threshold here.
3408 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0);
3410 memset(&msg, 0, sizeof(msg));
3411 msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
3414 port->negotiated_rev,
3415 port->message_id, 1);
3416 msg.payload[0] = cpu_to_le32(rdo);
3418 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
3421 static int tcpm_pd_build_pps_request(struct tcpm_port *port, u32 *rdo)
3423 unsigned int out_mv, op_ma, op_mw, max_mv, max_ma, flags;
3424 unsigned int src_pdo_index;
3426 src_pdo_index = tcpm_pd_select_pps_apdo(port);
3430 max_mv = port->pps_data.req_max_volt;
3431 max_ma = port->pps_data.req_max_curr;
3432 out_mv = port->pps_data.req_out_volt;
3433 op_ma = port->pps_data.req_op_curr;
3435 flags = RDO_USB_COMM | RDO_NO_SUSPEND;
3437 op_mw = (op_ma * out_mv) / 1000;
3438 if (op_mw < port->operating_snk_mw) {
3440 * Try raising current to meet power needs. If that's not enough
3441 * then try upping the voltage. If that's still not enough
3442 * then we've obviously chosen a PPS APDO which really isn't
3443 * suitable so abandon ship.
3445 op_ma = (port->operating_snk_mw * 1000) / out_mv;
3446 if ((port->operating_snk_mw * 1000) % out_mv)
3448 op_ma += RDO_PROG_CURR_MA_STEP - (op_ma % RDO_PROG_CURR_MA_STEP);
3450 if (op_ma > max_ma) {
3452 out_mv = (port->operating_snk_mw * 1000) / op_ma;
3453 if ((port->operating_snk_mw * 1000) % op_ma)
3455 out_mv += RDO_PROG_VOLT_MV_STEP -
3456 (out_mv % RDO_PROG_VOLT_MV_STEP);
3458 if (out_mv > max_mv) {
3459 tcpm_log(port, "Invalid PPS APDO selected!");
3465 tcpm_log(port, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d",
3466 port->cc_req, port->cc1, port->cc2, port->vbus_source,
3467 port->vconn_role == TYPEC_SOURCE ? "source" : "sink",
3470 *rdo = RDO_PROG(src_pdo_index + 1, out_mv, op_ma, flags);
3472 tcpm_log(port, "Requesting APDO %d: %u mV, %u mA",
3473 src_pdo_index, out_mv, op_ma);
3475 port->pps_data.req_op_curr = op_ma;
3476 port->pps_data.req_out_volt = out_mv;
3481 static int tcpm_pd_send_pps_request(struct tcpm_port *port)
3483 struct pd_message msg;
3487 ret = tcpm_pd_build_pps_request(port, &rdo);
3491 /* Relax the threshold as voltage will be adjusted right after Accept Message. */
3492 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0);
3494 memset(&msg, 0, sizeof(msg));
3495 msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
3498 port->negotiated_rev,
3499 port->message_id, 1);
3500 msg.payload[0] = cpu_to_le32(rdo);
3502 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
3505 static int tcpm_set_vbus(struct tcpm_port *port, bool enable)
3509 if (enable && port->vbus_charge)
3512 tcpm_log(port, "vbus:=%d charge=%d", enable, port->vbus_charge);
3514 ret = port->tcpc->set_vbus(port->tcpc, enable, port->vbus_charge);
3518 port->vbus_source = enable;
3522 static int tcpm_set_charge(struct tcpm_port *port, bool charge)
3526 if (charge && port->vbus_source)
3529 if (charge != port->vbus_charge) {
3530 tcpm_log(port, "vbus=%d charge:=%d", port->vbus_source, charge);
3531 ret = port->tcpc->set_vbus(port->tcpc, port->vbus_source,
3536 port->vbus_charge = charge;
3537 power_supply_changed(port->psy);
3541 static bool tcpm_start_toggling(struct tcpm_port *port, enum typec_cc_status cc)
3545 if (!port->tcpc->start_toggling)
3548 tcpm_log_force(port, "Start toggling");
3549 ret = port->tcpc->start_toggling(port->tcpc, port->port_type, cc);
3553 static int tcpm_init_vbus(struct tcpm_port *port)
3557 ret = port->tcpc->set_vbus(port->tcpc, false, false);
3558 port->vbus_source = false;
3559 port->vbus_charge = false;
3563 static int tcpm_init_vconn(struct tcpm_port *port)
3567 ret = port->tcpc->set_vconn(port->tcpc, false);
3568 port->vconn_role = TYPEC_SINK;
3572 static void tcpm_typec_connect(struct tcpm_port *port)
3574 if (!port->connected) {
3575 /* Make sure we don't report stale identity information */
3576 memset(&port->partner_ident, 0, sizeof(port->partner_ident));
3577 port->partner_desc.usb_pd = port->pd_capable;
3578 if (tcpm_port_is_debug(port))
3579 port->partner_desc.accessory = TYPEC_ACCESSORY_DEBUG;
3580 else if (tcpm_port_is_audio(port))
3581 port->partner_desc.accessory = TYPEC_ACCESSORY_AUDIO;
3583 port->partner_desc.accessory = TYPEC_ACCESSORY_NONE;
3584 port->partner = typec_register_partner(port->typec_port,
3585 &port->partner_desc);
3586 port->connected = true;
3587 typec_partner_set_usb_power_delivery(port->partner, port->partner_pd);
3591 static int tcpm_src_attach(struct tcpm_port *port)
3593 enum typec_cc_polarity polarity =
3594 port->cc2 == TYPEC_CC_RD ? TYPEC_POLARITY_CC2
3595 : TYPEC_POLARITY_CC1;
3601 ret = tcpm_set_polarity(port, polarity);
3605 tcpm_enable_auto_vbus_discharge(port, true);
3607 ret = tcpm_set_roles(port, true, TYPEC_SOURCE, tcpm_data_role_for_source(port));
3611 if (port->pd_supported) {
3612 ret = port->tcpc->set_pd_rx(port->tcpc, true);
3614 goto out_disable_mux;
3618 * USB Type-C specification, version 1.2,
3619 * chapter 4.5.2.2.8.1 (Attached.SRC Requirements)
3620 * Enable VCONN only if the non-RD port is set to RA.
3622 if ((polarity == TYPEC_POLARITY_CC1 && port->cc2 == TYPEC_CC_RA) ||
3623 (polarity == TYPEC_POLARITY_CC2 && port->cc1 == TYPEC_CC_RA)) {
3624 ret = tcpm_set_vconn(port, true);
3626 goto out_disable_pd;
3629 ret = tcpm_set_vbus(port, true);
3631 goto out_disable_vconn;
3633 port->pd_capable = false;
3635 port->partner = NULL;
3637 port->attached = true;
3638 port->send_discover = true;
3643 tcpm_set_vconn(port, false);
3645 if (port->pd_supported)
3646 port->tcpc->set_pd_rx(port->tcpc, false);
3648 tcpm_mux_set(port, TYPEC_STATE_SAFE, USB_ROLE_NONE,
3649 TYPEC_ORIENTATION_NONE);
3653 static void tcpm_typec_disconnect(struct tcpm_port *port)
3655 if (port->connected) {
3656 typec_partner_set_usb_power_delivery(port->partner, NULL);
3657 typec_unregister_partner(port->partner);
3658 port->partner = NULL;
3659 port->connected = false;
3663 static void tcpm_unregister_altmodes(struct tcpm_port *port)
3665 struct pd_mode_data *modep = &port->mode_data;
3668 for (i = 0; i < modep->altmodes; i++) {
3669 typec_unregister_altmode(port->partner_altmode[i]);
3670 port->partner_altmode[i] = NULL;
3673 memset(modep, 0, sizeof(*modep));
3676 static void tcpm_set_partner_usb_comm_capable(struct tcpm_port *port, bool capable)
3678 tcpm_log(port, "Setting usb_comm capable %s", capable ? "true" : "false");
3680 if (port->tcpc->set_partner_usb_comm_capable)
3681 port->tcpc->set_partner_usb_comm_capable(port->tcpc, capable);
3684 static void tcpm_reset_port(struct tcpm_port *port)
3686 tcpm_enable_auto_vbus_discharge(port, false);
3687 port->in_ams = false;
3688 port->ams = NONE_AMS;
3689 port->vdm_sm_running = false;
3690 tcpm_unregister_altmodes(port);
3691 tcpm_typec_disconnect(port);
3692 port->attached = false;
3693 port->pd_capable = false;
3694 port->pps_data.supported = false;
3695 tcpm_set_partner_usb_comm_capable(port, false);
3698 * First Rx ID should be 0; set this to a sentinel of -1 so that
3699 * we can check tcpm_pd_rx_handler() if we had seen it before.
3701 port->rx_msgid = -1;
3703 port->tcpc->set_pd_rx(port->tcpc, false);
3704 tcpm_init_vbus(port); /* also disables charging */
3705 tcpm_init_vconn(port);
3706 tcpm_set_current_limit(port, 0, 0);
3707 tcpm_set_polarity(port, TYPEC_POLARITY_CC1);
3708 tcpm_mux_set(port, TYPEC_STATE_SAFE, USB_ROLE_NONE,
3709 TYPEC_ORIENTATION_NONE);
3710 tcpm_set_attached_state(port, false);
3711 port->try_src_count = 0;
3712 port->try_snk_count = 0;
3713 port->usb_type = POWER_SUPPLY_USB_TYPE_C;
3714 power_supply_changed(port->psy);
3715 port->nr_sink_caps = 0;
3716 port->sink_cap_done = false;
3717 if (port->tcpc->enable_frs)
3718 port->tcpc->enable_frs(port->tcpc, false);
3720 usb_power_delivery_unregister_capabilities(port->partner_sink_caps);
3721 port->partner_sink_caps = NULL;
3722 usb_power_delivery_unregister_capabilities(port->partner_source_caps);
3723 port->partner_source_caps = NULL;
3724 usb_power_delivery_unregister(port->partner_pd);
3725 port->partner_pd = NULL;
3728 static void tcpm_detach(struct tcpm_port *port)
3730 if (tcpm_port_is_disconnected(port))
3731 port->hard_reset_count = 0;
3733 port->try_src_count = 0;
3734 port->try_snk_count = 0;
3736 if (!port->attached)
3739 if (port->tcpc->set_bist_data) {
3740 tcpm_log(port, "disable BIST MODE TESTDATA");
3741 port->tcpc->set_bist_data(port->tcpc, false);
3744 tcpm_reset_port(port);
3747 static void tcpm_src_detach(struct tcpm_port *port)
3752 static int tcpm_snk_attach(struct tcpm_port *port)
3759 ret = tcpm_set_polarity(port, port->cc2 != TYPEC_CC_OPEN ?
3760 TYPEC_POLARITY_CC2 : TYPEC_POLARITY_CC1);
3764 tcpm_enable_auto_vbus_discharge(port, true);
3766 ret = tcpm_set_roles(port, true, TYPEC_SINK, tcpm_data_role_for_sink(port));
3770 port->pd_capable = false;
3772 port->partner = NULL;
3774 port->attached = true;
3775 port->send_discover = true;
3780 static void tcpm_snk_detach(struct tcpm_port *port)
3785 static int tcpm_acc_attach(struct tcpm_port *port)
3792 ret = tcpm_set_roles(port, true, TYPEC_SOURCE,
3793 tcpm_data_role_for_source(port));
3797 port->partner = NULL;
3799 tcpm_typec_connect(port);
3801 port->attached = true;
3806 static void tcpm_acc_detach(struct tcpm_port *port)
3811 static inline enum tcpm_state hard_reset_state(struct tcpm_port *port)
3813 if (port->hard_reset_count < PD_N_HARD_RESET_COUNT)
3814 return HARD_RESET_SEND;
3815 if (port->pd_capable)
3816 return ERROR_RECOVERY;
3817 if (port->pwr_role == TYPEC_SOURCE)
3818 return SRC_UNATTACHED;
3819 if (port->state == SNK_WAIT_CAPABILITIES)
3821 return SNK_UNATTACHED;
3824 static inline enum tcpm_state unattached_state(struct tcpm_port *port)
3826 if (port->port_type == TYPEC_PORT_DRP) {
3827 if (port->pwr_role == TYPEC_SOURCE)
3828 return SRC_UNATTACHED;
3830 return SNK_UNATTACHED;
3831 } else if (port->port_type == TYPEC_PORT_SRC) {
3832 return SRC_UNATTACHED;
3835 return SNK_UNATTACHED;
3838 static void tcpm_swap_complete(struct tcpm_port *port, int result)
3840 if (port->swap_pending) {
3841 port->swap_status = result;
3842 port->swap_pending = false;
3843 port->non_pd_role_swap = false;
3844 complete(&port->swap_complete);
3848 static enum typec_pwr_opmode tcpm_get_pwr_opmode(enum typec_cc_status cc)
3851 case TYPEC_CC_RP_1_5:
3852 return TYPEC_PWR_MODE_1_5A;
3853 case TYPEC_CC_RP_3_0:
3854 return TYPEC_PWR_MODE_3_0A;
3855 case TYPEC_CC_RP_DEF:
3857 return TYPEC_PWR_MODE_USB;
3861 static enum typec_cc_status tcpm_pwr_opmode_to_rp(enum typec_pwr_opmode opmode)
3864 case TYPEC_PWR_MODE_USB:
3865 return TYPEC_CC_RP_DEF;
3866 case TYPEC_PWR_MODE_1_5A:
3867 return TYPEC_CC_RP_1_5;
3868 case TYPEC_PWR_MODE_3_0A:
3869 case TYPEC_PWR_MODE_PD:
3871 return TYPEC_CC_RP_3_0;
3875 static void tcpm_set_initial_svdm_version(struct tcpm_port *port)
3877 switch (port->negotiated_rev) {
3881 * 6.4.4.2.3 Structured VDM Version
3882 * 2.0 states "At this time, there is only one version (1.0) defined.
3883 * This field Shall be set to zero to indicate Version 1.0."
3884 * 3.0 states "This field Shall be set to 01b to indicate Version 2.0."
3885 * To ensure that we follow the Power Delivery revision we are currently
3886 * operating on, downgrade the SVDM version to the highest one supported
3887 * by the Power Delivery revision.
3890 typec_partner_set_svdm_version(port->partner, SVDM_VER_1_0);
3893 typec_partner_set_svdm_version(port->partner, SVDM_VER_1_0);
3898 static void run_state_machine(struct tcpm_port *port)
3901 enum typec_pwr_opmode opmode;
3903 enum tcpm_state upcoming_state;
3905 if (port->tcpc->check_contaminant && port->state != CHECK_CONTAMINANT)
3906 port->potential_contaminant = ((port->enter_state == SRC_ATTACH_WAIT &&
3907 port->state == SRC_UNATTACHED) ||
3908 (port->enter_state == SNK_ATTACH_WAIT &&
3909 port->state == SNK_UNATTACHED) ||
3910 (port->enter_state == SNK_DEBOUNCED &&
3911 port->state == SNK_UNATTACHED));
3913 port->enter_state = port->state;
3914 switch (port->state) {
3917 case CHECK_CONTAMINANT:
3918 port->tcpc->check_contaminant(port->tcpc);
3921 case SRC_UNATTACHED:
3922 if (!port->non_pd_role_swap)
3923 tcpm_swap_complete(port, -ENOTCONN);
3924 tcpm_src_detach(port);
3925 if (port->potential_contaminant) {
3926 tcpm_set_state(port, CHECK_CONTAMINANT, 0);
3929 if (tcpm_start_toggling(port, tcpm_rp_cc(port))) {
3930 tcpm_set_state(port, TOGGLING, 0);
3933 tcpm_set_cc(port, tcpm_rp_cc(port));
3934 if (port->port_type == TYPEC_PORT_DRP)
3935 tcpm_set_state(port, SNK_UNATTACHED, PD_T_DRP_SNK);
3937 case SRC_ATTACH_WAIT:
3938 if (tcpm_port_is_debug(port))
3939 tcpm_set_state(port, DEBUG_ACC_ATTACHED,
3941 else if (tcpm_port_is_audio(port))
3942 tcpm_set_state(port, AUDIO_ACC_ATTACHED,
3944 else if (tcpm_port_is_source(port) && port->vbus_vsafe0v)
3945 tcpm_set_state(port,
3946 tcpm_try_snk(port) ? SNK_TRY
3952 port->try_snk_count++;
3955 * - Do not drive vconn or vbus
3956 * - Terminate CC pins (both) to Rd
3958 * - Wait for tDRPTry (PD_T_DRP_TRY).
3959 * Until then, ignore any state changes.
3961 tcpm_set_cc(port, TYPEC_CC_RD);
3962 tcpm_set_state(port, SNK_TRY_WAIT, PD_T_DRP_TRY);
3965 if (tcpm_port_is_sink(port)) {
3966 tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE, 0);
3968 tcpm_set_state(port, SRC_TRYWAIT, 0);
3972 case SNK_TRY_WAIT_DEBOUNCE:
3973 tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS,
3974 PD_T_TRY_CC_DEBOUNCE);
3976 case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS:
3977 if (port->vbus_present && tcpm_port_is_sink(port))
3978 tcpm_set_state(port, SNK_ATTACHED, 0);
3983 tcpm_set_cc(port, tcpm_rp_cc(port));
3984 if (port->max_wait == 0) {
3985 port->max_wait = jiffies +
3986 msecs_to_jiffies(PD_T_DRP_TRY);
3987 tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED,
3990 if (time_is_after_jiffies(port->max_wait))
3991 tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED,
3992 jiffies_to_msecs(port->max_wait -
3995 tcpm_set_state(port, SNK_UNATTACHED, 0);
3998 case SRC_TRYWAIT_DEBOUNCE:
3999 tcpm_set_state(port, SRC_ATTACHED, PD_T_CC_DEBOUNCE);
4001 case SRC_TRYWAIT_UNATTACHED:
4002 tcpm_set_state(port, SNK_UNATTACHED, 0);
4006 ret = tcpm_src_attach(port);
4007 tcpm_set_state(port, SRC_UNATTACHED,
4008 ret < 0 ? 0 : PD_T_PS_SOURCE_ON);
4011 opmode = tcpm_get_pwr_opmode(tcpm_rp_cc(port));
4012 typec_set_pwr_opmode(port->typec_port, opmode);
4013 port->pwr_opmode = TYPEC_PWR_MODE_USB;
4014 port->caps_count = 0;
4015 port->negotiated_rev = PD_MAX_REV;
4016 port->message_id = 0;
4017 port->rx_msgid = -1;
4018 port->explicit_contract = false;
4019 /* SNK -> SRC POWER/FAST_ROLE_SWAP finished */
4020 if (port->ams == POWER_ROLE_SWAP ||
4021 port->ams == FAST_ROLE_SWAP)
4022 tcpm_ams_finish(port);
4023 if (!port->pd_supported) {
4024 tcpm_set_state(port, SRC_READY, 0);
4027 port->upcoming_state = SRC_SEND_CAPABILITIES;
4028 tcpm_ams_start(port, POWER_NEGOTIATION);
4030 case SRC_SEND_CAPABILITIES:
4032 if (port->caps_count > PD_N_CAPS_COUNT) {
4033 tcpm_set_state(port, SRC_READY, 0);
4036 ret = tcpm_pd_send_source_caps(port);
4038 tcpm_set_state(port, SRC_SEND_CAPABILITIES,
4039 PD_T_SEND_SOURCE_CAP);
4042 * Per standard, we should clear the reset counter here.
4043 * However, that can result in state machine hang-ups.
4044 * Reset it only in READY state to improve stability.
4046 /* port->hard_reset_count = 0; */
4047 port->caps_count = 0;
4048 port->pd_capable = true;
4049 tcpm_set_state_cond(port, SRC_SEND_CAPABILITIES_TIMEOUT,
4050 PD_T_SEND_SOURCE_CAP);
4053 case SRC_SEND_CAPABILITIES_TIMEOUT:
4055 * Error recovery for a PD_DATA_SOURCE_CAP reply timeout.
4057 * PD 2.0 sinks are supposed to accept src-capabilities with a
4058 * 3.0 header and simply ignore any src PDOs which the sink does
4059 * not understand such as PPS but some 2.0 sinks instead ignore
4060 * the entire PD_DATA_SOURCE_CAP message, causing contract
4061 * negotiation to fail.
4063 * After PD_N_HARD_RESET_COUNT hard-reset attempts, we try
4064 * sending src-capabilities with a lower PD revision to
4065 * make these broken sinks work.
4067 if (port->hard_reset_count < PD_N_HARD_RESET_COUNT) {
4068 tcpm_set_state(port, HARD_RESET_SEND, 0);
4069 } else if (port->negotiated_rev > PD_REV20) {
4070 port->negotiated_rev--;
4071 port->hard_reset_count = 0;
4072 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
4074 tcpm_set_state(port, hard_reset_state(port), 0);
4077 case SRC_NEGOTIATE_CAPABILITIES:
4078 ret = tcpm_pd_check_request(port);
4080 tcpm_pd_send_control(port, PD_CTRL_REJECT);
4081 if (!port->explicit_contract) {
4082 tcpm_set_state(port,
4083 SRC_WAIT_NEW_CAPABILITIES, 0);
4085 tcpm_set_state(port, SRC_READY, 0);
4088 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
4089 tcpm_set_partner_usb_comm_capable(port,
4090 !!(port->sink_request & RDO_USB_COMM));
4091 tcpm_set_state(port, SRC_TRANSITION_SUPPLY,
4092 PD_T_SRC_TRANSITION);
4095 case SRC_TRANSITION_SUPPLY:
4096 /* XXX: regulator_set_voltage(vbus, ...) */
4097 tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
4098 port->explicit_contract = true;
4099 typec_set_pwr_opmode(port->typec_port, TYPEC_PWR_MODE_PD);
4100 port->pwr_opmode = TYPEC_PWR_MODE_PD;
4101 tcpm_set_state_cond(port, SRC_READY, 0);
4105 port->hard_reset_count = 0;
4107 port->try_src_count = 0;
4109 tcpm_swap_complete(port, 0);
4110 tcpm_typec_connect(port);
4112 if (port->ams != NONE_AMS)
4113 tcpm_ams_finish(port);
4114 if (port->next_ams != NONE_AMS) {
4115 port->ams = port->next_ams;
4116 port->next_ams = NONE_AMS;
4120 * If previous AMS is interrupted, switch to the upcoming
4123 if (port->upcoming_state != INVALID_STATE) {
4124 upcoming_state = port->upcoming_state;
4125 port->upcoming_state = INVALID_STATE;
4126 tcpm_set_state(port, upcoming_state, 0);
4131 * 6.4.4.3.1 Discover Identity
4132 * "The Discover Identity Command Shall only be sent to SOP when there is an
4133 * Explicit Contract."
4134 * For now, this driver only supports SOP for DISCOVER_IDENTITY, thus using
4135 * port->explicit_contract to decide whether to send the command.
4137 if (port->explicit_contract) {
4138 tcpm_set_initial_svdm_version(port);
4139 mod_send_discover_delayed_work(port, 0);
4141 port->send_discover = false;
4146 * Sending ping messages is not necessary if
4147 * - the source operates at vSafe5V
4149 * - The system is not operating in PD mode
4151 * - Both partners are connected using a Type-C connector
4153 * There is no actual need to send PD messages since the local
4154 * port type-c and the spec does not clearly say whether PD is
4155 * possible when type-c is connected to Type-A/B
4158 case SRC_WAIT_NEW_CAPABILITIES:
4159 /* Nothing to do... */
4163 case SNK_UNATTACHED:
4164 if (!port->non_pd_role_swap)
4165 tcpm_swap_complete(port, -ENOTCONN);
4166 tcpm_pps_complete(port, -ENOTCONN);
4167 tcpm_snk_detach(port);
4168 if (port->potential_contaminant) {
4169 tcpm_set_state(port, CHECK_CONTAMINANT, 0);
4172 if (tcpm_start_toggling(port, TYPEC_CC_RD)) {
4173 tcpm_set_state(port, TOGGLING, 0);
4176 tcpm_set_cc(port, TYPEC_CC_RD);
4177 if (port->port_type == TYPEC_PORT_DRP)
4178 tcpm_set_state(port, SRC_UNATTACHED, PD_T_DRP_SRC);
4180 case SNK_ATTACH_WAIT:
4181 if ((port->cc1 == TYPEC_CC_OPEN &&
4182 port->cc2 != TYPEC_CC_OPEN) ||
4183 (port->cc1 != TYPEC_CC_OPEN &&
4184 port->cc2 == TYPEC_CC_OPEN))
4185 tcpm_set_state(port, SNK_DEBOUNCED,
4187 else if (tcpm_port_is_disconnected(port))
4188 tcpm_set_state(port, SNK_UNATTACHED,
4192 if (tcpm_port_is_disconnected(port))
4193 tcpm_set_state(port, SNK_UNATTACHED,
4195 else if (port->vbus_present)
4196 tcpm_set_state(port,
4197 tcpm_try_src(port) ? SRC_TRY
4202 port->try_src_count++;
4203 tcpm_set_cc(port, tcpm_rp_cc(port));
4205 tcpm_set_state(port, SRC_TRY_WAIT, 0);
4208 if (port->max_wait == 0) {
4209 port->max_wait = jiffies +
4210 msecs_to_jiffies(PD_T_DRP_TRY);
4211 msecs = PD_T_DRP_TRY;
4213 if (time_is_after_jiffies(port->max_wait))
4214 msecs = jiffies_to_msecs(port->max_wait -
4219 tcpm_set_state(port, SNK_TRYWAIT, msecs);
4221 case SRC_TRY_DEBOUNCE:
4222 tcpm_set_state(port, SRC_ATTACHED, PD_T_PD_DEBOUNCE);
4225 tcpm_set_cc(port, TYPEC_CC_RD);
4226 tcpm_set_state(port, SNK_TRYWAIT_VBUS, PD_T_CC_DEBOUNCE);
4228 case SNK_TRYWAIT_VBUS:
4230 * TCPM stays in this state indefinitely until VBUS
4231 * is detected as long as Rp is not detected for
4232 * more than a time period of tPDDebounce.
4234 if (port->vbus_present && tcpm_port_is_sink(port)) {
4235 tcpm_set_state(port, SNK_ATTACHED, 0);
4238 if (!tcpm_port_is_sink(port))
4239 tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, 0);
4241 case SNK_TRYWAIT_DEBOUNCE:
4242 tcpm_set_state(port, SNK_UNATTACHED, PD_T_PD_DEBOUNCE);
4245 ret = tcpm_snk_attach(port);
4247 tcpm_set_state(port, SNK_UNATTACHED, 0);
4249 tcpm_set_state(port, SNK_STARTUP, 0);
4252 opmode = tcpm_get_pwr_opmode(port->polarity ?
4253 port->cc2 : port->cc1);
4254 typec_set_pwr_opmode(port->typec_port, opmode);
4255 port->pwr_opmode = TYPEC_PWR_MODE_USB;
4256 port->negotiated_rev = PD_MAX_REV;
4257 port->message_id = 0;
4258 port->rx_msgid = -1;
4259 port->explicit_contract = false;
4261 if (port->ams == POWER_ROLE_SWAP ||
4262 port->ams == FAST_ROLE_SWAP)
4263 /* SRC -> SNK POWER/FAST_ROLE_SWAP finished */
4264 tcpm_ams_finish(port);
4266 tcpm_set_state(port, SNK_DISCOVERY, 0);
4269 if (port->vbus_present) {
4270 u32 current_lim = tcpm_get_current_limit(port);
4272 if (port->slow_charger_loop && (current_lim > PD_P_SNK_STDBY_MW / 5))
4273 current_lim = PD_P_SNK_STDBY_MW / 5;
4274 tcpm_set_current_limit(port, current_lim, 5000);
4275 /* Not sink vbus if operational current is 0mA */
4276 tcpm_set_charge(port, !port->pd_supported ||
4277 pdo_max_current(port->snk_pdo[0]));
4279 if (!port->pd_supported)
4280 tcpm_set_state(port, SNK_READY, 0);
4282 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
4286 * For DRP, timeouts differ. Also, handling is supposed to be
4287 * different and much more complex (dead battery detection;
4288 * see USB power delivery specification, section 8.3.3.6.1.5.1).
4290 tcpm_set_state(port, hard_reset_state(port),
4291 port->port_type == TYPEC_PORT_DRP ?
4292 PD_T_DB_DETECT : PD_T_NO_RESPONSE);
4294 case SNK_DISCOVERY_DEBOUNCE:
4295 tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE_DONE,
4298 case SNK_DISCOVERY_DEBOUNCE_DONE:
4299 if (!tcpm_port_is_disconnected(port) &&
4300 tcpm_port_is_sink(port) &&
4301 ktime_after(port->delayed_runtime, ktime_get())) {
4302 tcpm_set_state(port, SNK_DISCOVERY,
4303 ktime_to_ms(ktime_sub(port->delayed_runtime, ktime_get())));
4306 tcpm_set_state(port, unattached_state(port), 0);
4308 case SNK_WAIT_CAPABILITIES:
4309 ret = port->tcpc->set_pd_rx(port->tcpc, true);
4311 tcpm_set_state(port, SNK_READY, 0);
4315 * If VBUS has never been low, and we time out waiting
4316 * for source cap, try a soft reset first, in case we
4317 * were already in a stable contract before this boot.
4318 * Do this only once.
4320 if (port->vbus_never_low) {
4321 port->vbus_never_low = false;
4322 tcpm_set_state(port, SNK_SOFT_RESET,
4323 PD_T_SINK_WAIT_CAP);
4325 tcpm_set_state(port, hard_reset_state(port),
4326 PD_T_SINK_WAIT_CAP);
4329 case SNK_NEGOTIATE_CAPABILITIES:
4330 port->pd_capable = true;
4331 tcpm_set_partner_usb_comm_capable(port,
4332 !!(port->source_caps[0] & PDO_FIXED_USB_COMM));
4333 port->hard_reset_count = 0;
4334 ret = tcpm_pd_send_request(port);
4336 /* Restore back to the original state */
4337 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
4338 port->pps_data.active,
4339 port->supply_voltage);
4340 /* Let the Source send capabilities again. */
4341 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
4343 tcpm_set_state_cond(port, hard_reset_state(port),
4344 PD_T_SENDER_RESPONSE);
4347 case SNK_NEGOTIATE_PPS_CAPABILITIES:
4348 ret = tcpm_pd_send_pps_request(port);
4350 /* Restore back to the original state */
4351 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD,
4352 port->pps_data.active,
4353 port->supply_voltage);
4354 port->pps_status = ret;
4356 * If this was called due to updates to sink
4357 * capabilities, and pps is no longer valid, we should
4358 * safely fall back to a standard PDO.
4360 if (port->update_sink_caps)
4361 tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
4363 tcpm_set_state(port, SNK_READY, 0);
4365 tcpm_set_state_cond(port, hard_reset_state(port),
4366 PD_T_SENDER_RESPONSE);
4369 case SNK_TRANSITION_SINK:
4370 /* From the USB PD spec:
4371 * "The Sink Shall transition to Sink Standby before a positive or
4372 * negative voltage transition of VBUS. During Sink Standby
4373 * the Sink Shall reduce its power draw to pSnkStdby."
4375 * This is not applicable to PPS though as the port can continue
4376 * to draw negotiated power without switching to standby.
4378 if (port->supply_voltage != port->req_supply_voltage && !port->pps_data.active &&
4379 port->current_limit * port->supply_voltage / 1000 > PD_P_SNK_STDBY_MW) {
4380 u32 stdby_ma = PD_P_SNK_STDBY_MW * 1000 / port->supply_voltage;
4382 tcpm_log(port, "Setting standby current %u mV @ %u mA",
4383 port->supply_voltage, stdby_ma);
4384 tcpm_set_current_limit(port, stdby_ma, port->supply_voltage);
4387 case SNK_TRANSITION_SINK_VBUS:
4388 tcpm_set_state(port, hard_reset_state(port),
4389 PD_T_PS_TRANSITION);
4392 port->try_snk_count = 0;
4393 port->update_sink_caps = false;
4394 if (port->explicit_contract) {
4395 typec_set_pwr_opmode(port->typec_port,
4397 port->pwr_opmode = TYPEC_PWR_MODE_PD;
4400 if (!port->pd_capable && port->slow_charger_loop)
4401 tcpm_set_current_limit(port, tcpm_get_current_limit(port), 5000);
4402 tcpm_swap_complete(port, 0);
4403 tcpm_typec_connect(port);
4404 mod_enable_frs_delayed_work(port, 0);
4405 tcpm_pps_complete(port, port->pps_status);
4407 if (port->ams != NONE_AMS)
4408 tcpm_ams_finish(port);
4409 if (port->next_ams != NONE_AMS) {
4410 port->ams = port->next_ams;
4411 port->next_ams = NONE_AMS;
4415 * If previous AMS is interrupted, switch to the upcoming
4418 if (port->upcoming_state != INVALID_STATE) {
4419 upcoming_state = port->upcoming_state;
4420 port->upcoming_state = INVALID_STATE;
4421 tcpm_set_state(port, upcoming_state, 0);
4426 * 6.4.4.3.1 Discover Identity
4427 * "The Discover Identity Command Shall only be sent to SOP when there is an
4428 * Explicit Contract."
4429 * For now, this driver only supports SOP for DISCOVER_IDENTITY, thus using
4430 * port->explicit_contract.
4432 if (port->explicit_contract) {
4433 tcpm_set_initial_svdm_version(port);
4434 mod_send_discover_delayed_work(port, 0);
4436 port->send_discover = false;
4439 power_supply_changed(port->psy);
4442 /* Accessory states */
4443 case ACC_UNATTACHED:
4444 tcpm_acc_detach(port);
4445 tcpm_set_state(port, SRC_UNATTACHED, 0);
4447 case DEBUG_ACC_ATTACHED:
4448 case AUDIO_ACC_ATTACHED:
4449 ret = tcpm_acc_attach(port);
4451 tcpm_set_state(port, ACC_UNATTACHED, 0);
4453 case AUDIO_ACC_DEBOUNCE:
4454 tcpm_set_state(port, ACC_UNATTACHED, PD_T_CC_DEBOUNCE);
4457 /* Hard_Reset states */
4458 case HARD_RESET_SEND:
4459 if (port->ams != NONE_AMS)
4460 tcpm_ams_finish(port);
4462 * State machine will be directed to HARD_RESET_START,
4463 * thus set upcoming_state to INVALID_STATE.
4465 port->upcoming_state = INVALID_STATE;
4466 tcpm_ams_start(port, HARD_RESET);
4468 case HARD_RESET_START:
4469 port->sink_cap_done = false;
4470 if (port->tcpc->enable_frs)
4471 port->tcpc->enable_frs(port->tcpc, false);
4472 port->hard_reset_count++;
4473 port->tcpc->set_pd_rx(port->tcpc, false);
4474 tcpm_unregister_altmodes(port);
4475 port->nr_sink_caps = 0;
4476 port->send_discover = true;
4477 if (port->pwr_role == TYPEC_SOURCE)
4478 tcpm_set_state(port, SRC_HARD_RESET_VBUS_OFF,
4479 PD_T_PS_HARD_RESET);
4481 tcpm_set_state(port, SNK_HARD_RESET_SINK_OFF, 0);
4483 case SRC_HARD_RESET_VBUS_OFF:
4485 * 7.1.5 Response to Hard Resets
4486 * Hard Reset Signaling indicates a communication failure has occurred and the
4487 * Source Shall stop driving VCONN, Shall remove Rp from the VCONN pin and Shall
4488 * drive VBUS to vSafe0V as shown in Figure 7-9.
4490 tcpm_set_vconn(port, false);
4491 tcpm_set_vbus(port, false);
4492 tcpm_set_roles(port, port->self_powered, TYPEC_SOURCE,
4493 tcpm_data_role_for_source(port));
4495 * If tcpc fails to notify vbus off, TCPM will wait for PD_T_SAFE_0V +
4496 * PD_T_SRC_RECOVER before turning vbus back on.
4497 * From Table 7-12 Sequence Description for a Source Initiated Hard Reset:
4498 * 4. Policy Engine waits tPSHardReset after sending Hard Reset Signaling and then
4499 * tells the Device Policy Manager to instruct the power supply to perform a
4500 * Hard Reset. The transition to vSafe0V Shall occur within tSafe0V (t2).
4501 * 5. After tSrcRecover the Source applies power to VBUS in an attempt to
4502 * re-establish communication with the Sink and resume USB Default Operation.
4503 * The transition to vSafe5V Shall occur within tSrcTurnOn(t4).
4505 tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SAFE_0V + PD_T_SRC_RECOVER);
4507 case SRC_HARD_RESET_VBUS_ON:
4508 tcpm_set_vconn(port, true);
4509 tcpm_set_vbus(port, true);
4510 if (port->ams == HARD_RESET)
4511 tcpm_ams_finish(port);
4512 if (port->pd_supported)
4513 port->tcpc->set_pd_rx(port->tcpc, true);
4514 tcpm_set_attached_state(port, true);
4515 tcpm_set_state(port, SRC_UNATTACHED, PD_T_PS_SOURCE_ON);
4517 case SNK_HARD_RESET_SINK_OFF:
4518 /* Do not discharge/disconnect during hard reseet */
4519 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0);
4520 memset(&port->pps_data, 0, sizeof(port->pps_data));
4521 tcpm_set_vconn(port, false);
4522 if (port->pd_capable)
4523 tcpm_set_charge(port, false);
4524 tcpm_set_roles(port, port->self_powered, TYPEC_SINK,
4525 tcpm_data_role_for_sink(port));
4527 * VBUS may or may not toggle, depending on the adapter.
4528 * If it doesn't toggle, transition to SNK_HARD_RESET_SINK_ON
4529 * directly after timeout.
4531 tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, PD_T_SAFE_0V);
4533 case SNK_HARD_RESET_WAIT_VBUS:
4534 if (port->ams == HARD_RESET)
4535 tcpm_ams_finish(port);
4536 /* Assume we're disconnected if VBUS doesn't come back. */
4537 tcpm_set_state(port, SNK_UNATTACHED,
4538 PD_T_SRC_RECOVER_MAX + PD_T_SRC_TURN_ON);
4540 case SNK_HARD_RESET_SINK_ON:
4541 /* Note: There is no guarantee that VBUS is on in this state */
4544 * The specification suggests that dual mode ports in sink
4545 * mode should transition to state PE_SRC_Transition_to_default.
4546 * See USB power delivery specification chapter 8.3.3.6.1.3.
4547 * This would mean to
4548 * - turn off VCONN, reset power supply
4549 * - request hardware reset
4551 * - Transition to state PE_Src_Startup
4552 * SNK only ports shall transition to state Snk_Startup
4553 * (see chapter 8.3.3.3.8).
4554 * Similar, dual-mode ports in source mode should transition
4555 * to PE_SNK_Transition_to_default.
4557 if (port->pd_capable) {
4558 tcpm_set_current_limit(port,
4559 tcpm_get_current_limit(port),
4561 /* Not sink vbus if operational current is 0mA */
4562 tcpm_set_charge(port, !!pdo_max_current(port->snk_pdo[0]));
4564 if (port->ams == HARD_RESET)
4565 tcpm_ams_finish(port);
4566 tcpm_set_attached_state(port, true);
4567 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, VSAFE5V);
4568 tcpm_set_state(port, SNK_STARTUP, 0);
4571 /* Soft_Reset states */
4573 port->message_id = 0;
4574 port->rx_msgid = -1;
4575 /* remove existing capabilities */
4576 usb_power_delivery_unregister_capabilities(port->partner_source_caps);
4577 port->partner_source_caps = NULL;
4578 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
4579 tcpm_ams_finish(port);
4580 if (port->pwr_role == TYPEC_SOURCE) {
4581 port->upcoming_state = SRC_SEND_CAPABILITIES;
4582 tcpm_ams_start(port, POWER_NEGOTIATION);
4584 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
4587 case SRC_SOFT_RESET_WAIT_SNK_TX:
4588 case SNK_SOFT_RESET:
4589 if (port->ams != NONE_AMS)
4590 tcpm_ams_finish(port);
4591 port->upcoming_state = SOFT_RESET_SEND;
4592 tcpm_ams_start(port, SOFT_RESET_AMS);
4594 case SOFT_RESET_SEND:
4595 port->message_id = 0;
4596 port->rx_msgid = -1;
4597 /* remove existing capabilities */
4598 usb_power_delivery_unregister_capabilities(port->partner_source_caps);
4599 port->partner_source_caps = NULL;
4600 if (tcpm_pd_send_control(port, PD_CTRL_SOFT_RESET))
4601 tcpm_set_state_cond(port, hard_reset_state(port), 0);
4603 tcpm_set_state_cond(port, hard_reset_state(port),
4604 PD_T_SENDER_RESPONSE);
4607 /* DR_Swap states */
4609 tcpm_pd_send_control(port, PD_CTRL_DR_SWAP);
4610 if (port->data_role == TYPEC_DEVICE || port->negotiated_rev > PD_REV20)
4611 port->send_discover = true;
4612 tcpm_set_state_cond(port, DR_SWAP_SEND_TIMEOUT,
4613 PD_T_SENDER_RESPONSE);
4615 case DR_SWAP_ACCEPT:
4616 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
4617 if (port->data_role == TYPEC_DEVICE || port->negotiated_rev > PD_REV20)
4618 port->send_discover = true;
4619 tcpm_set_state_cond(port, DR_SWAP_CHANGE_DR, 0);
4621 case DR_SWAP_SEND_TIMEOUT:
4622 tcpm_swap_complete(port, -ETIMEDOUT);
4623 port->send_discover = false;
4624 tcpm_ams_finish(port);
4625 tcpm_set_state(port, ready_state(port), 0);
4627 case DR_SWAP_CHANGE_DR:
4628 tcpm_unregister_altmodes(port);
4629 if (port->data_role == TYPEC_HOST)
4630 tcpm_set_roles(port, true, port->pwr_role,
4633 tcpm_set_roles(port, true, port->pwr_role,
4635 tcpm_ams_finish(port);
4636 tcpm_set_state(port, ready_state(port), 0);
4640 if (tcpm_pd_send_control(port, PD_CTRL_FR_SWAP)) {
4641 tcpm_set_state(port, ERROR_RECOVERY, 0);
4644 tcpm_set_state_cond(port, FR_SWAP_SEND_TIMEOUT, PD_T_SENDER_RESPONSE);
4646 case FR_SWAP_SEND_TIMEOUT:
4647 tcpm_set_state(port, ERROR_RECOVERY, 0);
4649 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
4650 tcpm_set_state(port, ERROR_RECOVERY, PD_T_PS_SOURCE_OFF);
4652 case FR_SWAP_SNK_SRC_NEW_SINK_READY:
4653 if (port->vbus_source)
4654 tcpm_set_state(port, FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED, 0);
4656 tcpm_set_state(port, ERROR_RECOVERY, PD_T_RECEIVER_RESPONSE);
4658 case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED:
4659 tcpm_set_pwr_role(port, TYPEC_SOURCE);
4660 if (tcpm_pd_send_control(port, PD_CTRL_PS_RDY)) {
4661 tcpm_set_state(port, ERROR_RECOVERY, 0);
4664 tcpm_set_cc(port, tcpm_rp_cc(port));
4665 tcpm_set_state(port, SRC_STARTUP, PD_T_SWAP_SRC_START);
4668 /* PR_Swap states */
4669 case PR_SWAP_ACCEPT:
4670 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
4671 tcpm_set_state(port, PR_SWAP_START, 0);
4674 tcpm_pd_send_control(port, PD_CTRL_PR_SWAP);
4675 tcpm_set_state_cond(port, PR_SWAP_SEND_TIMEOUT,
4676 PD_T_SENDER_RESPONSE);
4678 case PR_SWAP_SEND_TIMEOUT:
4679 tcpm_swap_complete(port, -ETIMEDOUT);
4680 tcpm_set_state(port, ready_state(port), 0);
4683 tcpm_apply_rc(port);
4684 if (port->pwr_role == TYPEC_SOURCE)
4685 tcpm_set_state(port, PR_SWAP_SRC_SNK_TRANSITION_OFF,
4686 PD_T_SRC_TRANSITION);
4688 tcpm_set_state(port, PR_SWAP_SNK_SRC_SINK_OFF, 0);
4690 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
4692 * Prevent vbus discharge circuit from turning on during PR_SWAP
4693 * as this is not a disconnect.
4695 tcpm_set_vbus(port, false);
4696 port->explicit_contract = false;
4697 /* allow time for Vbus discharge, must be < tSrcSwapStdby */
4698 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF,
4701 case PR_SWAP_SRC_SNK_SOURCE_OFF:
4702 tcpm_set_cc(port, TYPEC_CC_RD);
4703 /* allow CC debounce */
4704 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED,
4707 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED:
4709 * USB-PD standard, 6.2.1.4, Port Power Role:
4710 * "During the Power Role Swap Sequence, for the initial Source
4711 * Port, the Port Power Role field shall be set to Sink in the
4712 * PS_RDY Message indicating that the initial Source’s power
4713 * supply is turned off"
4715 tcpm_set_pwr_role(port, TYPEC_SINK);
4716 if (tcpm_pd_send_control(port, PD_CTRL_PS_RDY)) {
4717 tcpm_set_state(port, ERROR_RECOVERY, 0);
4720 tcpm_set_state(port, ERROR_RECOVERY, PD_T_PS_SOURCE_ON_PRS);
4722 case PR_SWAP_SRC_SNK_SINK_ON:
4723 tcpm_enable_auto_vbus_discharge(port, true);
4724 /* Set the vbus disconnect threshold for implicit contract */
4725 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, VSAFE5V);
4726 tcpm_set_state(port, SNK_STARTUP, 0);
4728 case PR_SWAP_SNK_SRC_SINK_OFF:
4729 /* will be source, remove existing capabilities */
4730 usb_power_delivery_unregister_capabilities(port->partner_source_caps);
4731 port->partner_source_caps = NULL;
4733 * Prevent vbus discharge circuit from turning on during PR_SWAP
4734 * as this is not a disconnect.
4736 tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB,
4737 port->pps_data.active, 0);
4738 tcpm_set_charge(port, false);
4739 tcpm_set_state(port, hard_reset_state(port),
4740 PD_T_PS_SOURCE_OFF);
4742 case PR_SWAP_SNK_SRC_SOURCE_ON:
4743 tcpm_enable_auto_vbus_discharge(port, true);
4744 tcpm_set_cc(port, tcpm_rp_cc(port));
4745 tcpm_set_vbus(port, true);
4747 * allow time VBUS ramp-up, must be < tNewSrc
4748 * Also, this window overlaps with CC debounce as well.
4749 * So, Wait for the max of two which is PD_T_NEWSRC
4751 tcpm_set_state(port, PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP,
4754 case PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP:
4756 * USB PD standard, 6.2.1.4:
4757 * "Subsequent Messages initiated by the Policy Engine,
4758 * such as the PS_RDY Message sent to indicate that Vbus
4759 * is ready, will have the Port Power Role field set to
4762 tcpm_set_pwr_role(port, TYPEC_SOURCE);
4763 tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
4764 tcpm_set_state(port, SRC_STARTUP, PD_T_SWAP_SRC_START);
4767 case VCONN_SWAP_ACCEPT:
4768 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
4769 tcpm_ams_finish(port);
4770 tcpm_set_state(port, VCONN_SWAP_START, 0);
4772 case VCONN_SWAP_SEND:
4773 tcpm_pd_send_control(port, PD_CTRL_VCONN_SWAP);
4774 tcpm_set_state(port, VCONN_SWAP_SEND_TIMEOUT,
4775 PD_T_SENDER_RESPONSE);
4777 case VCONN_SWAP_SEND_TIMEOUT:
4778 tcpm_swap_complete(port, -ETIMEDOUT);
4779 tcpm_set_state(port, ready_state(port), 0);
4781 case VCONN_SWAP_START:
4782 if (port->vconn_role == TYPEC_SOURCE)
4783 tcpm_set_state(port, VCONN_SWAP_WAIT_FOR_VCONN, 0);
4785 tcpm_set_state(port, VCONN_SWAP_TURN_ON_VCONN, 0);
4787 case VCONN_SWAP_WAIT_FOR_VCONN:
4788 tcpm_set_state(port, hard_reset_state(port),
4789 PD_T_VCONN_SOURCE_ON);
4791 case VCONN_SWAP_TURN_ON_VCONN:
4792 tcpm_set_vconn(port, true);
4793 tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
4794 tcpm_set_state(port, ready_state(port), 0);
4796 case VCONN_SWAP_TURN_OFF_VCONN:
4797 tcpm_set_vconn(port, false);
4798 tcpm_set_state(port, ready_state(port), 0);
4801 case DR_SWAP_CANCEL:
4802 case PR_SWAP_CANCEL:
4803 case VCONN_SWAP_CANCEL:
4804 tcpm_swap_complete(port, port->swap_status);
4805 if (port->pwr_role == TYPEC_SOURCE)
4806 tcpm_set_state(port, SRC_READY, 0);
4808 tcpm_set_state(port, SNK_READY, 0);
4810 case FR_SWAP_CANCEL:
4811 if (port->pwr_role == TYPEC_SOURCE)
4812 tcpm_set_state(port, SRC_READY, 0);
4814 tcpm_set_state(port, SNK_READY, 0);
4818 switch (BDO_MODE_MASK(port->bist_request)) {
4819 case BDO_MODE_CARRIER2:
4820 tcpm_pd_transmit(port, TCPC_TX_BIST_MODE_2, NULL);
4821 tcpm_set_state(port, unattached_state(port),
4822 PD_T_BIST_CONT_MODE);
4824 case BDO_MODE_TESTDATA:
4825 if (port->tcpc->set_bist_data) {
4826 tcpm_log(port, "Enable BIST MODE TESTDATA");
4827 port->tcpc->set_bist_data(port->tcpc, true);
4834 case GET_STATUS_SEND:
4835 tcpm_pd_send_control(port, PD_CTRL_GET_STATUS);
4836 tcpm_set_state(port, GET_STATUS_SEND_TIMEOUT,
4837 PD_T_SENDER_RESPONSE);
4839 case GET_STATUS_SEND_TIMEOUT:
4840 tcpm_set_state(port, ready_state(port), 0);
4842 case GET_PPS_STATUS_SEND:
4843 tcpm_pd_send_control(port, PD_CTRL_GET_PPS_STATUS);
4844 tcpm_set_state(port, GET_PPS_STATUS_SEND_TIMEOUT,
4845 PD_T_SENDER_RESPONSE);
4847 case GET_PPS_STATUS_SEND_TIMEOUT:
4848 tcpm_set_state(port, ready_state(port), 0);
4851 tcpm_pd_send_control(port, PD_CTRL_GET_SINK_CAP);
4852 tcpm_set_state(port, GET_SINK_CAP_TIMEOUT, PD_T_SENDER_RESPONSE);
4854 case GET_SINK_CAP_TIMEOUT:
4855 port->sink_cap_done = true;
4856 tcpm_set_state(port, ready_state(port), 0);
4858 case ERROR_RECOVERY:
4859 tcpm_swap_complete(port, -EPROTO);
4860 tcpm_pps_complete(port, -EPROTO);
4861 tcpm_set_state(port, PORT_RESET, 0);
4864 tcpm_reset_port(port);
4865 tcpm_set_cc(port, TYPEC_CC_OPEN);
4866 tcpm_set_state(port, PORT_RESET_WAIT_OFF,
4867 PD_T_ERROR_RECOVERY);
4869 case PORT_RESET_WAIT_OFF:
4870 tcpm_set_state(port,
4871 tcpm_default_state(port),
4872 port->vbus_present ? PD_T_PS_SOURCE_OFF : 0);
4875 /* AMS intermediate state */
4877 if (port->upcoming_state == INVALID_STATE) {
4878 tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ?
4879 SRC_READY : SNK_READY, 0);
4883 upcoming_state = port->upcoming_state;
4884 port->upcoming_state = INVALID_STATE;
4885 tcpm_set_state(port, upcoming_state, 0);
4889 case CHUNK_NOT_SUPP:
4890 tcpm_pd_send_control(port, PD_CTRL_NOT_SUPP);
4891 tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ? SRC_READY : SNK_READY, 0);
4894 WARN(1, "Unexpected port state %d\n", port->state);
4899 static void tcpm_state_machine_work(struct kthread_work *work)
4901 struct tcpm_port *port = container_of(work, struct tcpm_port, state_machine);
4902 enum tcpm_state prev_state;
4904 mutex_lock(&port->lock);
4905 port->state_machine_running = true;
4907 if (port->queued_message && tcpm_send_queued_message(port))
4910 /* If we were queued due to a delayed state change, update it now */
4911 if (port->delayed_state) {
4912 tcpm_log(port, "state change %s -> %s [delayed %ld ms]",
4913 tcpm_states[port->state],
4914 tcpm_states[port->delayed_state], port->delay_ms);
4915 port->prev_state = port->state;
4916 port->state = port->delayed_state;
4917 port->delayed_state = INVALID_STATE;
4921 * Continue running as long as we have (non-delayed) state changes
4925 prev_state = port->state;
4926 run_state_machine(port);
4927 if (port->queued_message)
4928 tcpm_send_queued_message(port);
4929 } while (port->state != prev_state && !port->delayed_state);
4932 port->state_machine_running = false;
4933 mutex_unlock(&port->lock);
4936 static void _tcpm_cc_change(struct tcpm_port *port, enum typec_cc_status cc1,
4937 enum typec_cc_status cc2)
4939 enum typec_cc_status old_cc1, old_cc2;
4940 enum tcpm_state new_state;
4942 old_cc1 = port->cc1;
4943 old_cc2 = port->cc2;
4947 tcpm_log_force(port,
4948 "CC1: %u -> %u, CC2: %u -> %u [state %s, polarity %d, %s]",
4949 old_cc1, cc1, old_cc2, cc2, tcpm_states[port->state],
4951 tcpm_port_is_disconnected(port) ? "disconnected"
4954 switch (port->state) {
4956 if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) ||
4957 tcpm_port_is_source(port))
4958 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
4959 else if (tcpm_port_is_sink(port))
4960 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
4962 case CHECK_CONTAMINANT:
4963 /* Wait for Toggling to be resumed */
4965 case SRC_UNATTACHED:
4966 case ACC_UNATTACHED:
4967 if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) ||
4968 tcpm_port_is_source(port))
4969 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
4971 case SRC_ATTACH_WAIT:
4972 if (tcpm_port_is_disconnected(port) ||
4973 tcpm_port_is_audio_detached(port))
4974 tcpm_set_state(port, SRC_UNATTACHED, 0);
4975 else if (cc1 != old_cc1 || cc2 != old_cc2)
4976 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
4980 case SRC_SEND_CAPABILITIES:
4982 if (tcpm_port_is_disconnected(port) ||
4983 !tcpm_port_is_source(port)) {
4984 if (port->port_type == TYPEC_PORT_SRC)
4985 tcpm_set_state(port, SRC_UNATTACHED, tcpm_wait_for_discharge(port));
4987 tcpm_set_state(port, SNK_UNATTACHED, tcpm_wait_for_discharge(port));
4990 case SNK_UNATTACHED:
4991 if (tcpm_port_is_sink(port))
4992 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
4994 case SNK_ATTACH_WAIT:
4995 if ((port->cc1 == TYPEC_CC_OPEN &&
4996 port->cc2 != TYPEC_CC_OPEN) ||
4997 (port->cc1 != TYPEC_CC_OPEN &&
4998 port->cc2 == TYPEC_CC_OPEN))
4999 new_state = SNK_DEBOUNCED;
5000 else if (tcpm_port_is_disconnected(port))
5001 new_state = SNK_UNATTACHED;
5004 if (new_state != port->delayed_state)
5005 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
5008 if (tcpm_port_is_disconnected(port))
5009 new_state = SNK_UNATTACHED;
5010 else if (port->vbus_present)
5011 new_state = tcpm_try_src(port) ? SRC_TRY : SNK_ATTACHED;
5013 new_state = SNK_UNATTACHED;
5014 if (new_state != port->delayed_state)
5015 tcpm_set_state(port, SNK_DEBOUNCED, 0);
5019 * EXIT condition is based primarily on vbus disconnect and CC is secondary.
5020 * "A port that has entered into USB PD communications with the Source and
5021 * has seen the CC voltage exceed vRd-USB may monitor the CC pin to detect
5022 * cable disconnect in addition to monitoring VBUS.
5024 * A port that is monitoring the CC voltage for disconnect (but is not in
5025 * the process of a USB PD PR_Swap or USB PD FR_Swap) shall transition to
5026 * Unattached.SNK within tSinkDisconnect after the CC voltage remains below
5027 * vRd-USB for tPDDebounce."
5029 * When set_auto_vbus_discharge_threshold is enabled, CC pins go
5030 * away before vbus decays to disconnect threshold. Allow
5031 * disconnect to be driven by vbus disconnect when auto vbus
5032 * discharge is enabled.
5034 if (!port->auto_vbus_discharge_enabled && tcpm_port_is_disconnected(port))
5035 tcpm_set_state(port, unattached_state(port), 0);
5036 else if (!port->pd_capable &&
5037 (cc1 != old_cc1 || cc2 != old_cc2))
5038 tcpm_set_current_limit(port,
5039 tcpm_get_current_limit(port),
5043 case AUDIO_ACC_ATTACHED:
5044 if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN)
5045 tcpm_set_state(port, AUDIO_ACC_DEBOUNCE, 0);
5047 case AUDIO_ACC_DEBOUNCE:
5048 if (tcpm_port_is_audio(port))
5049 tcpm_set_state(port, AUDIO_ACC_ATTACHED, 0);
5052 case DEBUG_ACC_ATTACHED:
5053 if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN)
5054 tcpm_set_state(port, ACC_UNATTACHED, 0);
5058 /* Do nothing, waiting for timeout */
5062 /* CC line is unstable, wait for debounce */
5063 if (tcpm_port_is_disconnected(port))
5064 tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE, 0);
5066 case SNK_DISCOVERY_DEBOUNCE:
5070 /* Hand over to state machine if needed */
5071 if (!port->vbus_present && tcpm_port_is_source(port))
5072 tcpm_set_state(port, SRC_TRYWAIT_DEBOUNCE, 0);
5074 case SRC_TRYWAIT_DEBOUNCE:
5075 if (port->vbus_present || !tcpm_port_is_source(port))
5076 tcpm_set_state(port, SRC_TRYWAIT, 0);
5078 case SNK_TRY_WAIT_DEBOUNCE:
5079 if (!tcpm_port_is_sink(port)) {
5081 tcpm_set_state(port, SRC_TRYWAIT, 0);
5085 if (tcpm_port_is_source(port))
5086 tcpm_set_state(port, SRC_TRY_DEBOUNCE, 0);
5088 case SRC_TRY_DEBOUNCE:
5089 tcpm_set_state(port, SRC_TRY_WAIT, 0);
5091 case SNK_TRYWAIT_DEBOUNCE:
5092 if (tcpm_port_is_sink(port))
5093 tcpm_set_state(port, SNK_TRYWAIT_VBUS, 0);
5095 case SNK_TRYWAIT_VBUS:
5096 if (!tcpm_port_is_sink(port))
5097 tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, 0);
5099 case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS:
5100 if (!tcpm_port_is_sink(port))
5101 tcpm_set_state(port, SRC_TRYWAIT, PD_T_TRY_CC_DEBOUNCE);
5103 tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS, 0);
5106 /* Do nothing, waiting for tCCDebounce */
5108 case PR_SWAP_SNK_SRC_SINK_OFF:
5109 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
5110 case PR_SWAP_SRC_SNK_SOURCE_OFF:
5111 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED:
5112 case PR_SWAP_SNK_SRC_SOURCE_ON:
5114 * CC state change is expected in PR_SWAP
5119 case FR_SWAP_SEND_TIMEOUT:
5120 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
5121 case FR_SWAP_SNK_SRC_NEW_SINK_READY:
5122 case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED:
5123 /* Do nothing, CC change expected */
5127 case PORT_RESET_WAIT_OFF:
5129 * State set back to default mode once the timer completes.
5130 * Ignore CC changes here.
5135 * While acting as sink and auto vbus discharge is enabled, Allow disconnect
5136 * to be driven by vbus disconnect.
5138 if (tcpm_port_is_disconnected(port) && !(port->pwr_role == TYPEC_SINK &&
5139 port->auto_vbus_discharge_enabled))
5140 tcpm_set_state(port, unattached_state(port), 0);
5145 static void _tcpm_pd_vbus_on(struct tcpm_port *port)
5147 tcpm_log_force(port, "VBUS on");
5148 port->vbus_present = true;
5150 * When vbus_present is true i.e. Voltage at VBUS is greater than VSAFE5V implicitly
5151 * states that vbus is not at VSAFE0V, hence clear the vbus_vsafe0v flag here.
5153 port->vbus_vsafe0v = false;
5155 switch (port->state) {
5156 case SNK_TRANSITION_SINK_VBUS:
5157 port->explicit_contract = true;
5158 tcpm_set_state(port, SNK_READY, 0);
5161 tcpm_set_state(port, SNK_DISCOVERY, 0);
5165 tcpm_set_state(port, tcpm_try_src(port) ? SRC_TRY
5169 case SNK_HARD_RESET_WAIT_VBUS:
5170 tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, 0);
5173 tcpm_set_state(port, SRC_STARTUP, 0);
5175 case SRC_HARD_RESET_VBUS_ON:
5176 tcpm_set_state(port, SRC_STARTUP, 0);
5180 /* Do nothing, waiting for timeout */
5183 /* Do nothing, Waiting for Rd to be detected */
5185 case SRC_TRYWAIT_DEBOUNCE:
5186 tcpm_set_state(port, SRC_TRYWAIT, 0);
5188 case SNK_TRY_WAIT_DEBOUNCE:
5189 /* Do nothing, waiting for PD_DEBOUNCE to do be done */
5192 /* Do nothing, waiting for tCCDebounce */
5194 case SNK_TRYWAIT_VBUS:
5195 if (tcpm_port_is_sink(port))
5196 tcpm_set_state(port, SNK_ATTACHED, 0);
5198 case SNK_TRYWAIT_DEBOUNCE:
5199 /* Do nothing, waiting for Rp */
5201 case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS:
5202 if (port->vbus_present && tcpm_port_is_sink(port))
5203 tcpm_set_state(port, SNK_ATTACHED, 0);
5206 case SRC_TRY_DEBOUNCE:
5207 /* Do nothing, waiting for sink detection */
5210 case FR_SWAP_SEND_TIMEOUT:
5211 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
5212 case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED:
5213 if (port->tcpc->frs_sourcing_vbus)
5214 port->tcpc->frs_sourcing_vbus(port->tcpc);
5216 case FR_SWAP_SNK_SRC_NEW_SINK_READY:
5217 if (port->tcpc->frs_sourcing_vbus)
5218 port->tcpc->frs_sourcing_vbus(port->tcpc);
5219 tcpm_set_state(port, FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED, 0);
5223 case PORT_RESET_WAIT_OFF:
5225 * State set back to default mode once the timer completes.
5226 * Ignore vbus changes here.
5235 static void _tcpm_pd_vbus_off(struct tcpm_port *port)
5237 tcpm_log_force(port, "VBUS off");
5238 port->vbus_present = false;
5239 port->vbus_never_low = false;
5240 switch (port->state) {
5241 case SNK_HARD_RESET_SINK_OFF:
5242 tcpm_set_state(port, SNK_HARD_RESET_WAIT_VBUS, 0);
5244 case HARD_RESET_SEND:
5247 /* Do nothing, waiting for timeout */
5250 /* Hand over to state machine if needed */
5251 if (tcpm_port_is_source(port))
5252 tcpm_set_state(port, SRC_TRYWAIT_DEBOUNCE, 0);
5254 case SNK_TRY_WAIT_DEBOUNCE:
5255 /* Do nothing, waiting for PD_DEBOUNCE to do be done */
5258 case SNK_TRYWAIT_VBUS:
5259 case SNK_TRYWAIT_DEBOUNCE:
5261 case SNK_ATTACH_WAIT:
5263 /* Do nothing, as TCPM is still waiting for vbus to reaach VSAFE5V to connect */
5266 case SNK_NEGOTIATE_CAPABILITIES:
5269 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
5270 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF, 0);
5273 case PR_SWAP_SNK_SRC_SINK_OFF:
5274 /* Do nothing, expected */
5277 case PR_SWAP_SNK_SRC_SOURCE_ON:
5279 * Do nothing when vbus off notification is received.
5280 * TCPM can wait for PD_T_NEWSRC in PR_SWAP_SNK_SRC_SOURCE_ON
5281 * for the vbus source to ramp up.
5285 case PORT_RESET_WAIT_OFF:
5286 tcpm_set_state(port, tcpm_default_state(port), 0);
5290 case SRC_TRY_DEBOUNCE:
5291 /* Do nothing, waiting for sink detection */
5295 case SRC_SEND_CAPABILITIES:
5296 case SRC_SEND_CAPABILITIES_TIMEOUT:
5297 case SRC_NEGOTIATE_CAPABILITIES:
5298 case SRC_TRANSITION_SUPPLY:
5300 case SRC_WAIT_NEW_CAPABILITIES:
5302 * Force to unattached state to re-initiate connection.
5303 * DRP port should move to Unattached.SNK instead of Unattached.SRC if
5304 * sink removed. Although sink removal here is due to source's vbus collapse,
5305 * treat it the same way for consistency.
5307 if (port->port_type == TYPEC_PORT_SRC)
5308 tcpm_set_state(port, SRC_UNATTACHED, tcpm_wait_for_discharge(port));
5310 tcpm_set_state(port, SNK_UNATTACHED, tcpm_wait_for_discharge(port));
5315 * State set back to default mode once the timer completes.
5316 * Ignore vbus changes here.
5321 case FR_SWAP_SEND_TIMEOUT:
5322 case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF:
5323 case FR_SWAP_SNK_SRC_NEW_SINK_READY:
5324 case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED:
5325 /* Do nothing, vbus drop expected */
5328 case SNK_HARD_RESET_WAIT_VBUS:
5329 /* Do nothing, its OK to receive vbus off events */
5333 if (port->pwr_role == TYPEC_SINK && port->attached)
5334 tcpm_set_state(port, SNK_UNATTACHED, tcpm_wait_for_discharge(port));
5339 static void _tcpm_pd_vbus_vsafe0v(struct tcpm_port *port)
5341 tcpm_log_force(port, "VBUS VSAFE0V");
5342 port->vbus_vsafe0v = true;
5343 switch (port->state) {
5344 case SRC_HARD_RESET_VBUS_OFF:
5346 * After establishing the vSafe0V voltage condition on VBUS, the Source Shall wait
5347 * tSrcRecover before re-applying VCONN and restoring VBUS to vSafe5V.
5349 tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SRC_RECOVER);
5351 case SRC_ATTACH_WAIT:
5352 if (tcpm_port_is_source(port))
5353 tcpm_set_state(port, tcpm_try_snk(port) ? SNK_TRY : SRC_ATTACHED,
5357 case SRC_SEND_CAPABILITIES:
5358 case SRC_SEND_CAPABILITIES_TIMEOUT:
5359 case SRC_NEGOTIATE_CAPABILITIES:
5360 case SRC_TRANSITION_SUPPLY:
5362 case SRC_WAIT_NEW_CAPABILITIES:
5363 if (port->auto_vbus_discharge_enabled) {
5364 if (port->port_type == TYPEC_PORT_SRC)
5365 tcpm_set_state(port, SRC_UNATTACHED, 0);
5367 tcpm_set_state(port, SNK_UNATTACHED, 0);
5370 case PR_SWAP_SNK_SRC_SINK_OFF:
5371 case PR_SWAP_SNK_SRC_SOURCE_ON:
5372 /* Do nothing, vsafe0v is expected during transition */
5374 case SNK_ATTACH_WAIT:
5376 /*Do nothing, still waiting for VSAFE5V for connect */
5378 case SNK_HARD_RESET_WAIT_VBUS:
5379 /* Do nothing, its OK to receive vbus off events */
5382 if (port->pwr_role == TYPEC_SINK && port->auto_vbus_discharge_enabled)
5383 tcpm_set_state(port, SNK_UNATTACHED, 0);
5388 static void _tcpm_pd_hard_reset(struct tcpm_port *port)
5390 tcpm_log_force(port, "Received hard reset");
5391 if (port->bist_request == BDO_MODE_TESTDATA && port->tcpc->set_bist_data)
5392 port->tcpc->set_bist_data(port->tcpc, false);
5394 switch (port->state) {
5395 case ERROR_RECOVERY:
5397 case PORT_RESET_WAIT_OFF:
5403 if (port->ams != NONE_AMS)
5404 port->ams = NONE_AMS;
5405 if (port->hard_reset_count < PD_N_HARD_RESET_COUNT)
5406 port->ams = HARD_RESET;
5408 * If we keep receiving hard reset requests, executing the hard reset
5409 * must have failed. Revert to error recovery if that happens.
5411 tcpm_set_state(port,
5412 port->hard_reset_count < PD_N_HARD_RESET_COUNT ?
5413 HARD_RESET_START : ERROR_RECOVERY,
5417 static void tcpm_pd_event_handler(struct kthread_work *work)
5419 struct tcpm_port *port = container_of(work, struct tcpm_port,
5423 mutex_lock(&port->lock);
5425 spin_lock(&port->pd_event_lock);
5426 while (port->pd_events) {
5427 events = port->pd_events;
5428 port->pd_events = 0;
5429 spin_unlock(&port->pd_event_lock);
5430 if (events & TCPM_RESET_EVENT)
5431 _tcpm_pd_hard_reset(port);
5432 if (events & TCPM_VBUS_EVENT) {
5435 vbus = port->tcpc->get_vbus(port->tcpc);
5437 _tcpm_pd_vbus_on(port);
5439 _tcpm_pd_vbus_off(port);
5441 * When TCPC does not support detecting vsafe0v voltage level,
5442 * treat vbus absent as vsafe0v. Else invoke is_vbus_vsafe0v
5443 * to see if vbus has discharge to VSAFE0V.
5445 if (!port->tcpc->is_vbus_vsafe0v ||
5446 port->tcpc->is_vbus_vsafe0v(port->tcpc))
5447 _tcpm_pd_vbus_vsafe0v(port);
5450 if (events & TCPM_CC_EVENT) {
5451 enum typec_cc_status cc1, cc2;
5453 if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0)
5454 _tcpm_cc_change(port, cc1, cc2);
5456 if (events & TCPM_FRS_EVENT) {
5457 if (port->state == SNK_READY) {
5460 port->upcoming_state = FR_SWAP_SEND;
5461 ret = tcpm_ams_start(port, FAST_ROLE_SWAP);
5463 port->upcoming_state = INVALID_STATE;
5465 tcpm_log(port, "Discarding FRS_SIGNAL! Not in sink ready");
5468 if (events & TCPM_SOURCING_VBUS) {
5469 tcpm_log(port, "sourcing vbus");
5471 * In fast role swap case TCPC autonomously sources vbus. Set vbus_source
5472 * true as TCPM wouldn't have called tcpm_set_vbus.
5474 * When vbus is sourced on the command on TCPM i.e. TCPM called
5475 * tcpm_set_vbus to source vbus, vbus_source would already be true.
5477 port->vbus_source = true;
5478 _tcpm_pd_vbus_on(port);
5480 if (events & TCPM_PORT_CLEAN) {
5481 tcpm_log(port, "port clean");
5482 if (port->state == CHECK_CONTAMINANT) {
5483 if (tcpm_start_toggling(port, tcpm_rp_cc(port)))
5484 tcpm_set_state(port, TOGGLING, 0);
5486 tcpm_set_state(port, tcpm_default_state(port), 0);
5490 spin_lock(&port->pd_event_lock);
5492 spin_unlock(&port->pd_event_lock);
5493 mutex_unlock(&port->lock);
5496 void tcpm_cc_change(struct tcpm_port *port)
5498 spin_lock(&port->pd_event_lock);
5499 port->pd_events |= TCPM_CC_EVENT;
5500 spin_unlock(&port->pd_event_lock);
5501 kthread_queue_work(port->wq, &port->event_work);
5503 EXPORT_SYMBOL_GPL(tcpm_cc_change);
5505 void tcpm_vbus_change(struct tcpm_port *port)
5507 spin_lock(&port->pd_event_lock);
5508 port->pd_events |= TCPM_VBUS_EVENT;
5509 spin_unlock(&port->pd_event_lock);
5510 kthread_queue_work(port->wq, &port->event_work);
5512 EXPORT_SYMBOL_GPL(tcpm_vbus_change);
5514 void tcpm_pd_hard_reset(struct tcpm_port *port)
5516 spin_lock(&port->pd_event_lock);
5517 port->pd_events = TCPM_RESET_EVENT;
5518 spin_unlock(&port->pd_event_lock);
5519 kthread_queue_work(port->wq, &port->event_work);
5521 EXPORT_SYMBOL_GPL(tcpm_pd_hard_reset);
5523 void tcpm_sink_frs(struct tcpm_port *port)
5525 spin_lock(&port->pd_event_lock);
5526 port->pd_events |= TCPM_FRS_EVENT;
5527 spin_unlock(&port->pd_event_lock);
5528 kthread_queue_work(port->wq, &port->event_work);
5530 EXPORT_SYMBOL_GPL(tcpm_sink_frs);
5532 void tcpm_sourcing_vbus(struct tcpm_port *port)
5534 spin_lock(&port->pd_event_lock);
5535 port->pd_events |= TCPM_SOURCING_VBUS;
5536 spin_unlock(&port->pd_event_lock);
5537 kthread_queue_work(port->wq, &port->event_work);
5539 EXPORT_SYMBOL_GPL(tcpm_sourcing_vbus);
5541 void tcpm_port_clean(struct tcpm_port *port)
5543 spin_lock(&port->pd_event_lock);
5544 port->pd_events |= TCPM_PORT_CLEAN;
5545 spin_unlock(&port->pd_event_lock);
5546 kthread_queue_work(port->wq, &port->event_work);
5548 EXPORT_SYMBOL_GPL(tcpm_port_clean);
5550 bool tcpm_port_is_toggling(struct tcpm_port *port)
5552 return port->port_type == TYPEC_PORT_DRP && port->state == TOGGLING;
5554 EXPORT_SYMBOL_GPL(tcpm_port_is_toggling);
5556 static void tcpm_enable_frs_work(struct kthread_work *work)
5558 struct tcpm_port *port = container_of(work, struct tcpm_port, enable_frs);
5561 mutex_lock(&port->lock);
5562 /* Not FRS capable */
5563 if (!port->connected || port->port_type != TYPEC_PORT_DRP ||
5564 port->pwr_opmode != TYPEC_PWR_MODE_PD ||
5565 !port->tcpc->enable_frs ||
5566 /* Sink caps queried */
5567 port->sink_cap_done || port->negotiated_rev < PD_REV30)
5570 /* Send when the state machine is idle */
5571 if (port->state != SNK_READY || port->vdm_sm_running || port->send_discover)
5574 port->upcoming_state = GET_SINK_CAP;
5575 ret = tcpm_ams_start(port, GET_SINK_CAPABILITIES);
5576 if (ret == -EAGAIN) {
5577 port->upcoming_state = INVALID_STATE;
5579 port->sink_cap_done = true;
5583 mod_enable_frs_delayed_work(port, GET_SINK_CAP_RETRY_MS);
5585 mutex_unlock(&port->lock);
5588 static void tcpm_send_discover_work(struct kthread_work *work)
5590 struct tcpm_port *port = container_of(work, struct tcpm_port, send_discover_work);
5592 mutex_lock(&port->lock);
5593 /* No need to send DISCOVER_IDENTITY anymore */
5594 if (!port->send_discover)
5597 if (port->data_role == TYPEC_DEVICE && port->negotiated_rev < PD_REV30) {
5598 port->send_discover = false;
5602 /* Retry if the port is not idle */
5603 if ((port->state != SRC_READY && port->state != SNK_READY) || port->vdm_sm_running) {
5604 mod_send_discover_delayed_work(port, SEND_DISCOVER_RETRY_MS);
5608 tcpm_send_vdm(port, USB_SID_PD, CMD_DISCOVER_IDENT, NULL, 0);
5611 mutex_unlock(&port->lock);
5614 static int tcpm_dr_set(struct typec_port *p, enum typec_data_role data)
5616 struct tcpm_port *port = typec_get_drvdata(p);
5619 mutex_lock(&port->swap_lock);
5620 mutex_lock(&port->lock);
5622 if (port->typec_caps.data != TYPEC_PORT_DRD) {
5626 if (port->state != SRC_READY && port->state != SNK_READY) {
5631 if (port->data_role == data) {
5638 * 6.3.9: If an alternate mode is active, a request to swap
5639 * alternate modes shall trigger a port reset.
5640 * Reject data role swap request in this case.
5643 if (!port->pd_capable) {
5645 * If the partner is not PD capable, reset the port to
5646 * trigger a role change. This can only work if a preferred
5647 * role is configured, and if it matches the requested role.
5649 if (port->try_role == TYPEC_NO_PREFERRED_ROLE ||
5650 port->try_role == port->pwr_role) {
5654 port->non_pd_role_swap = true;
5655 tcpm_set_state(port, PORT_RESET, 0);
5657 port->upcoming_state = DR_SWAP_SEND;
5658 ret = tcpm_ams_start(port, DATA_ROLE_SWAP);
5659 if (ret == -EAGAIN) {
5660 port->upcoming_state = INVALID_STATE;
5665 port->swap_status = 0;
5666 port->swap_pending = true;
5667 reinit_completion(&port->swap_complete);
5668 mutex_unlock(&port->lock);
5670 if (!wait_for_completion_timeout(&port->swap_complete,
5671 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT)))
5674 ret = port->swap_status;
5676 port->non_pd_role_swap = false;
5680 mutex_unlock(&port->lock);
5682 mutex_unlock(&port->swap_lock);
5686 static int tcpm_pr_set(struct typec_port *p, enum typec_role role)
5688 struct tcpm_port *port = typec_get_drvdata(p);
5691 mutex_lock(&port->swap_lock);
5692 mutex_lock(&port->lock);
5694 if (port->port_type != TYPEC_PORT_DRP) {
5698 if (port->state != SRC_READY && port->state != SNK_READY) {
5703 if (role == port->pwr_role) {
5708 port->upcoming_state = PR_SWAP_SEND;
5709 ret = tcpm_ams_start(port, POWER_ROLE_SWAP);
5710 if (ret == -EAGAIN) {
5711 port->upcoming_state = INVALID_STATE;
5715 port->swap_status = 0;
5716 port->swap_pending = true;
5717 reinit_completion(&port->swap_complete);
5718 mutex_unlock(&port->lock);
5720 if (!wait_for_completion_timeout(&port->swap_complete,
5721 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT)))
5724 ret = port->swap_status;
5729 mutex_unlock(&port->lock);
5731 mutex_unlock(&port->swap_lock);
5735 static int tcpm_vconn_set(struct typec_port *p, enum typec_role role)
5737 struct tcpm_port *port = typec_get_drvdata(p);
5740 mutex_lock(&port->swap_lock);
5741 mutex_lock(&port->lock);
5743 if (port->state != SRC_READY && port->state != SNK_READY) {
5748 if (role == port->vconn_role) {
5753 port->upcoming_state = VCONN_SWAP_SEND;
5754 ret = tcpm_ams_start(port, VCONN_SWAP);
5755 if (ret == -EAGAIN) {
5756 port->upcoming_state = INVALID_STATE;
5760 port->swap_status = 0;
5761 port->swap_pending = true;
5762 reinit_completion(&port->swap_complete);
5763 mutex_unlock(&port->lock);
5765 if (!wait_for_completion_timeout(&port->swap_complete,
5766 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT)))
5769 ret = port->swap_status;
5774 mutex_unlock(&port->lock);
5776 mutex_unlock(&port->swap_lock);
5780 static int tcpm_try_role(struct typec_port *p, int role)
5782 struct tcpm_port *port = typec_get_drvdata(p);
5783 struct tcpc_dev *tcpc = port->tcpc;
5786 mutex_lock(&port->lock);
5788 ret = tcpc->try_role(tcpc, role);
5790 port->try_role = role;
5791 port->try_src_count = 0;
5792 port->try_snk_count = 0;
5793 mutex_unlock(&port->lock);
5798 static int tcpm_pps_set_op_curr(struct tcpm_port *port, u16 req_op_curr)
5800 unsigned int target_mw;
5803 mutex_lock(&port->swap_lock);
5804 mutex_lock(&port->lock);
5806 if (!port->pps_data.active) {
5811 if (port->state != SNK_READY) {
5816 if (req_op_curr > port->pps_data.max_curr) {
5821 target_mw = (req_op_curr * port->supply_voltage) / 1000;
5822 if (target_mw < port->operating_snk_mw) {
5827 port->upcoming_state = SNK_NEGOTIATE_PPS_CAPABILITIES;
5828 ret = tcpm_ams_start(port, POWER_NEGOTIATION);
5829 if (ret == -EAGAIN) {
5830 port->upcoming_state = INVALID_STATE;
5834 /* Round down operating current to align with PPS valid steps */
5835 req_op_curr = req_op_curr - (req_op_curr % RDO_PROG_CURR_MA_STEP);
5837 reinit_completion(&port->pps_complete);
5838 port->pps_data.req_op_curr = req_op_curr;
5839 port->pps_status = 0;
5840 port->pps_pending = true;
5841 mutex_unlock(&port->lock);
5843 if (!wait_for_completion_timeout(&port->pps_complete,
5844 msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT)))
5847 ret = port->pps_status;
5852 mutex_unlock(&port->lock);
5854 mutex_unlock(&port->swap_lock);
5859 static int tcpm_pps_set_out_volt(struct tcpm_port *port, u16 req_out_volt)
5861 unsigned int target_mw;
5864 mutex_lock(&port->swap_lock);
5865 mutex_lock(&port->lock);
5867 if (!port->pps_data.active) {
5872 if (port->state != SNK_READY) {
5877 target_mw = (port->current_limit * req_out_volt) / 1000;
5878 if (target_mw < port->operating_snk_mw) {
5883 port->upcoming_state = SNK_NEGOTIATE_PPS_CAPABILITIES;
5884 ret = tcpm_ams_start(port, POWER_NEGOTIATION);
5885 if (ret == -EAGAIN) {
5886 port->upcoming_state = INVALID_STATE;
5890 /* Round down output voltage to align with PPS valid steps */
5891 req_out_volt = req_out_volt - (req_out_volt % RDO_PROG_VOLT_MV_STEP);
5893 reinit_completion(&port->pps_complete);
5894 port->pps_data.req_out_volt = req_out_volt;
5895 port->pps_status = 0;
5896 port->pps_pending = true;
5897 mutex_unlock(&port->lock);
5899 if (!wait_for_completion_timeout(&port->pps_complete,
5900 msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT)))
5903 ret = port->pps_status;
5908 mutex_unlock(&port->lock);
5910 mutex_unlock(&port->swap_lock);
5915 static int tcpm_pps_activate(struct tcpm_port *port, bool activate)
5919 mutex_lock(&port->swap_lock);
5920 mutex_lock(&port->lock);
5922 if (!port->pps_data.supported) {
5927 /* Trying to deactivate PPS when already deactivated so just bail */
5928 if (!port->pps_data.active && !activate)
5931 if (port->state != SNK_READY) {
5937 port->upcoming_state = SNK_NEGOTIATE_PPS_CAPABILITIES;
5939 port->upcoming_state = SNK_NEGOTIATE_CAPABILITIES;
5940 ret = tcpm_ams_start(port, POWER_NEGOTIATION);
5941 if (ret == -EAGAIN) {
5942 port->upcoming_state = INVALID_STATE;
5946 reinit_completion(&port->pps_complete);
5947 port->pps_status = 0;
5948 port->pps_pending = true;
5950 /* Trigger PPS request or move back to standard PDO contract */
5952 port->pps_data.req_out_volt = port->supply_voltage;
5953 port->pps_data.req_op_curr = port->current_limit;
5955 mutex_unlock(&port->lock);
5957 if (!wait_for_completion_timeout(&port->pps_complete,
5958 msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT)))
5961 ret = port->pps_status;
5966 mutex_unlock(&port->lock);
5968 mutex_unlock(&port->swap_lock);
5973 static void tcpm_init(struct tcpm_port *port)
5975 enum typec_cc_status cc1, cc2;
5977 port->tcpc->init(port->tcpc);
5979 tcpm_reset_port(port);
5983 * Should possibly wait for VBUS to settle if it was enabled locally
5984 * since tcpm_reset_port() will disable VBUS.
5986 port->vbus_present = port->tcpc->get_vbus(port->tcpc);
5987 if (port->vbus_present)
5988 port->vbus_never_low = true;
5991 * 1. When vbus_present is true, voltage on VBUS is already at VSAFE5V.
5992 * So implicitly vbus_vsafe0v = false.
5994 * 2. When vbus_present is false and TCPC does NOT support querying
5995 * vsafe0v status, then, it's best to assume vbus is at VSAFE0V i.e.
5996 * vbus_vsafe0v is true.
5998 * 3. When vbus_present is false and TCPC does support querying vsafe0v,
5999 * then, query tcpc for vsafe0v status.
6001 if (port->vbus_present)
6002 port->vbus_vsafe0v = false;
6003 else if (!port->tcpc->is_vbus_vsafe0v)
6004 port->vbus_vsafe0v = true;
6006 port->vbus_vsafe0v = port->tcpc->is_vbus_vsafe0v(port->tcpc);
6008 tcpm_set_state(port, tcpm_default_state(port), 0);
6010 if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0)
6011 _tcpm_cc_change(port, cc1, cc2);
6014 * Some adapters need a clean slate at startup, and won't recover
6015 * otherwise. So do not try to be fancy and force a clean disconnect.
6017 tcpm_set_state(port, PORT_RESET, 0);
6020 static int tcpm_port_type_set(struct typec_port *p, enum typec_port_type type)
6022 struct tcpm_port *port = typec_get_drvdata(p);
6024 mutex_lock(&port->lock);
6025 if (type == port->port_type)
6028 port->port_type = type;
6030 if (!port->connected) {
6031 tcpm_set_state(port, PORT_RESET, 0);
6032 } else if (type == TYPEC_PORT_SNK) {
6033 if (!(port->pwr_role == TYPEC_SINK &&
6034 port->data_role == TYPEC_DEVICE))
6035 tcpm_set_state(port, PORT_RESET, 0);
6036 } else if (type == TYPEC_PORT_SRC) {
6037 if (!(port->pwr_role == TYPEC_SOURCE &&
6038 port->data_role == TYPEC_HOST))
6039 tcpm_set_state(port, PORT_RESET, 0);
6043 mutex_unlock(&port->lock);
6047 static const struct typec_operations tcpm_ops = {
6048 .try_role = tcpm_try_role,
6049 .dr_set = tcpm_dr_set,
6050 .pr_set = tcpm_pr_set,
6051 .vconn_set = tcpm_vconn_set,
6052 .port_type_set = tcpm_port_type_set
6055 void tcpm_tcpc_reset(struct tcpm_port *port)
6057 mutex_lock(&port->lock);
6058 /* XXX: Maintain PD connection if possible? */
6060 mutex_unlock(&port->lock);
6062 EXPORT_SYMBOL_GPL(tcpm_tcpc_reset);
6064 static void tcpm_port_unregister_pd(struct tcpm_port *port)
6066 usb_power_delivery_unregister_capabilities(port->port_sink_caps);
6067 port->port_sink_caps = NULL;
6068 usb_power_delivery_unregister_capabilities(port->port_source_caps);
6069 port->port_source_caps = NULL;
6070 usb_power_delivery_unregister(port->pd);
6074 static int tcpm_port_register_pd(struct tcpm_port *port)
6076 struct usb_power_delivery_desc desc = { port->typec_caps.pd_revision };
6077 struct usb_power_delivery_capabilities_desc caps = { };
6078 struct usb_power_delivery_capabilities *cap;
6081 if (!port->nr_src_pdo && !port->nr_snk_pdo)
6084 port->pd = usb_power_delivery_register(port->dev, &desc);
6085 if (IS_ERR(port->pd)) {
6086 ret = PTR_ERR(port->pd);
6087 goto err_unregister;
6090 if (port->nr_src_pdo) {
6091 memcpy_and_pad(caps.pdo, sizeof(caps.pdo), port->src_pdo,
6092 port->nr_src_pdo * sizeof(u32), 0);
6093 caps.role = TYPEC_SOURCE;
6095 cap = usb_power_delivery_register_capabilities(port->pd, &caps);
6098 goto err_unregister;
6101 port->port_source_caps = cap;
6104 if (port->nr_snk_pdo) {
6105 memcpy_and_pad(caps.pdo, sizeof(caps.pdo), port->snk_pdo,
6106 port->nr_snk_pdo * sizeof(u32), 0);
6107 caps.role = TYPEC_SINK;
6109 cap = usb_power_delivery_register_capabilities(port->pd, &caps);
6112 goto err_unregister;
6115 port->port_sink_caps = cap;
6121 tcpm_port_unregister_pd(port);
6126 static int tcpm_fw_get_caps(struct tcpm_port *port,
6127 struct fwnode_handle *fwnode)
6129 const char *opmode_str;
6131 u32 mw, frs_current;
6137 * This fwnode has a "compatible" property, but is never populated as a
6138 * struct device. Instead we simply parse it to read the properties.
6139 * This it breaks fw_devlink=on. To maintain backward compatibility
6140 * with existing DT files, we work around this by deleting any
6141 * fwnode_links to/from this fwnode.
6143 fw_devlink_purge_absent_suppliers(fwnode);
6145 ret = typec_get_fw_cap(&port->typec_caps, fwnode);
6149 port->port_type = port->typec_caps.type;
6150 port->pd_supported = !fwnode_property_read_bool(fwnode, "pd-disable");
6152 port->slow_charger_loop = fwnode_property_read_bool(fwnode, "slow-charger-loop");
6153 if (port->port_type == TYPEC_PORT_SNK)
6156 /* Get Source PDOs for the PD port or Source Rp value for the non-PD port */
6157 if (port->pd_supported) {
6158 ret = fwnode_property_count_u32(fwnode, "source-pdos");
6164 port->nr_src_pdo = min(ret, PDO_MAX_OBJECTS);
6165 ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
6166 port->src_pdo, port->nr_src_pdo);
6169 ret = tcpm_validate_caps(port, port->src_pdo, port->nr_src_pdo);
6173 ret = fwnode_property_read_string(fwnode, "typec-power-opmode", &opmode_str);
6176 ret = typec_find_pwr_opmode(opmode_str);
6179 port->src_rp = tcpm_pwr_opmode_to_rp(ret);
6182 if (port->port_type == TYPEC_PORT_SRC)
6186 port->self_powered = fwnode_property_read_bool(fwnode, "self-powered");
6188 if (!port->pd_supported)
6192 ret = fwnode_property_count_u32(fwnode, "sink-pdos");
6196 port->nr_snk_pdo = min(ret, PDO_MAX_OBJECTS);
6197 ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
6198 port->snk_pdo, port->nr_snk_pdo);
6199 if ((ret < 0) || tcpm_validate_caps(port, port->snk_pdo,
6203 if (fwnode_property_read_u32(fwnode, "op-sink-microwatt", &mw) < 0)
6205 port->operating_snk_mw = mw / 1000;
6207 /* FRS can only be supported by DRP ports */
6208 if (port->port_type == TYPEC_PORT_DRP) {
6209 ret = fwnode_property_read_u32(fwnode, "new-source-frs-typec-current",
6211 if (ret >= 0 && frs_current <= FRS_5V_3A)
6212 port->new_source_frs_current = frs_current;
6215 /* sink-vdos is optional */
6216 ret = fwnode_property_count_u32(fwnode, "sink-vdos");
6220 port->nr_snk_vdo = min(ret, VDO_MAX_OBJECTS);
6221 if (port->nr_snk_vdo) {
6222 ret = fwnode_property_read_u32_array(fwnode, "sink-vdos",
6229 /* If sink-vdos is found, sink-vdos-v1 is expected for backward compatibility. */
6230 if (port->nr_snk_vdo) {
6231 ret = fwnode_property_count_u32(fwnode, "sink-vdos-v1");
6237 port->nr_snk_vdo_v1 = min(ret, VDO_MAX_OBJECTS);
6238 ret = fwnode_property_read_u32_array(fwnode, "sink-vdos-v1",
6240 port->nr_snk_vdo_v1);
6248 /* Power Supply access to expose source power information */
6249 enum tcpm_psy_online_states {
6250 TCPM_PSY_OFFLINE = 0,
6251 TCPM_PSY_FIXED_ONLINE,
6252 TCPM_PSY_PROG_ONLINE,
6255 static enum power_supply_property tcpm_psy_props[] = {
6256 POWER_SUPPLY_PROP_USB_TYPE,
6257 POWER_SUPPLY_PROP_ONLINE,
6258 POWER_SUPPLY_PROP_VOLTAGE_MIN,
6259 POWER_SUPPLY_PROP_VOLTAGE_MAX,
6260 POWER_SUPPLY_PROP_VOLTAGE_NOW,
6261 POWER_SUPPLY_PROP_CURRENT_MAX,
6262 POWER_SUPPLY_PROP_CURRENT_NOW,
6265 static int tcpm_psy_get_online(struct tcpm_port *port,
6266 union power_supply_propval *val)
6268 if (port->vbus_charge) {
6269 if (port->pps_data.active)
6270 val->intval = TCPM_PSY_PROG_ONLINE;
6272 val->intval = TCPM_PSY_FIXED_ONLINE;
6274 val->intval = TCPM_PSY_OFFLINE;
6280 static int tcpm_psy_get_voltage_min(struct tcpm_port *port,
6281 union power_supply_propval *val)
6283 if (port->pps_data.active)
6284 val->intval = port->pps_data.min_volt * 1000;
6286 val->intval = port->supply_voltage * 1000;
6291 static int tcpm_psy_get_voltage_max(struct tcpm_port *port,
6292 union power_supply_propval *val)
6294 if (port->pps_data.active)
6295 val->intval = port->pps_data.max_volt * 1000;
6297 val->intval = port->supply_voltage * 1000;
6302 static int tcpm_psy_get_voltage_now(struct tcpm_port *port,
6303 union power_supply_propval *val)
6305 val->intval = port->supply_voltage * 1000;
6310 static int tcpm_psy_get_current_max(struct tcpm_port *port,
6311 union power_supply_propval *val)
6313 if (port->pps_data.active)
6314 val->intval = port->pps_data.max_curr * 1000;
6316 val->intval = port->current_limit * 1000;
6321 static int tcpm_psy_get_current_now(struct tcpm_port *port,
6322 union power_supply_propval *val)
6324 val->intval = port->current_limit * 1000;
6329 static int tcpm_psy_get_input_power_limit(struct tcpm_port *port,
6330 union power_supply_propval *val)
6332 unsigned int src_mv, src_ma, max_src_uw = 0;
6333 unsigned int i, tmp;
6335 for (i = 0; i < port->nr_source_caps; i++) {
6336 u32 pdo = port->source_caps[i];
6338 if (pdo_type(pdo) == PDO_TYPE_FIXED) {
6339 src_mv = pdo_fixed_voltage(pdo);
6340 src_ma = pdo_max_current(pdo);
6341 tmp = src_mv * src_ma;
6342 max_src_uw = tmp > max_src_uw ? tmp : max_src_uw;
6346 val->intval = max_src_uw;
6350 static int tcpm_psy_get_prop(struct power_supply *psy,
6351 enum power_supply_property psp,
6352 union power_supply_propval *val)
6354 struct tcpm_port *port = power_supply_get_drvdata(psy);
6358 case POWER_SUPPLY_PROP_USB_TYPE:
6359 val->intval = port->usb_type;
6361 case POWER_SUPPLY_PROP_ONLINE:
6362 ret = tcpm_psy_get_online(port, val);
6364 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
6365 ret = tcpm_psy_get_voltage_min(port, val);
6367 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
6368 ret = tcpm_psy_get_voltage_max(port, val);
6370 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
6371 ret = tcpm_psy_get_voltage_now(port, val);
6373 case POWER_SUPPLY_PROP_CURRENT_MAX:
6374 ret = tcpm_psy_get_current_max(port, val);
6376 case POWER_SUPPLY_PROP_CURRENT_NOW:
6377 ret = tcpm_psy_get_current_now(port, val);
6379 case POWER_SUPPLY_PROP_INPUT_POWER_LIMIT:
6380 tcpm_psy_get_input_power_limit(port, val);
6390 static int tcpm_psy_set_online(struct tcpm_port *port,
6391 const union power_supply_propval *val)
6395 switch (val->intval) {
6396 case TCPM_PSY_FIXED_ONLINE:
6397 ret = tcpm_pps_activate(port, false);
6399 case TCPM_PSY_PROG_ONLINE:
6400 ret = tcpm_pps_activate(port, true);
6410 static int tcpm_psy_set_prop(struct power_supply *psy,
6411 enum power_supply_property psp,
6412 const union power_supply_propval *val)
6414 struct tcpm_port *port = power_supply_get_drvdata(psy);
6418 * All the properties below are related to USB PD. The check needs to be
6419 * property specific when a non-pd related property is added.
6421 if (!port->pd_supported)
6425 case POWER_SUPPLY_PROP_ONLINE:
6426 ret = tcpm_psy_set_online(port, val);
6428 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
6429 ret = tcpm_pps_set_out_volt(port, val->intval / 1000);
6431 case POWER_SUPPLY_PROP_CURRENT_NOW:
6432 if (val->intval > port->pps_data.max_curr * 1000)
6435 ret = tcpm_pps_set_op_curr(port, val->intval / 1000);
6441 power_supply_changed(port->psy);
6445 static int tcpm_psy_prop_writeable(struct power_supply *psy,
6446 enum power_supply_property psp)
6449 case POWER_SUPPLY_PROP_ONLINE:
6450 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
6451 case POWER_SUPPLY_PROP_CURRENT_NOW:
6458 static enum power_supply_usb_type tcpm_psy_usb_types[] = {
6459 POWER_SUPPLY_USB_TYPE_C,
6460 POWER_SUPPLY_USB_TYPE_PD,
6461 POWER_SUPPLY_USB_TYPE_PD_PPS,
6464 static const char *tcpm_psy_name_prefix = "tcpm-source-psy-";
6466 static int devm_tcpm_psy_register(struct tcpm_port *port)
6468 struct power_supply_config psy_cfg = {};
6469 const char *port_dev_name = dev_name(port->dev);
6470 size_t psy_name_len = strlen(tcpm_psy_name_prefix) +
6471 strlen(port_dev_name) + 1;
6474 psy_cfg.drv_data = port;
6475 psy_cfg.fwnode = dev_fwnode(port->dev);
6476 psy_name = devm_kzalloc(port->dev, psy_name_len, GFP_KERNEL);
6480 snprintf(psy_name, psy_name_len, "%s%s", tcpm_psy_name_prefix,
6482 port->psy_desc.name = psy_name;
6483 port->psy_desc.type = POWER_SUPPLY_TYPE_USB;
6484 port->psy_desc.usb_types = tcpm_psy_usb_types;
6485 port->psy_desc.num_usb_types = ARRAY_SIZE(tcpm_psy_usb_types);
6486 port->psy_desc.properties = tcpm_psy_props;
6487 port->psy_desc.num_properties = ARRAY_SIZE(tcpm_psy_props);
6488 port->psy_desc.get_property = tcpm_psy_get_prop;
6489 port->psy_desc.set_property = tcpm_psy_set_prop;
6490 port->psy_desc.property_is_writeable = tcpm_psy_prop_writeable;
6492 port->usb_type = POWER_SUPPLY_USB_TYPE_C;
6494 port->psy = devm_power_supply_register(port->dev, &port->psy_desc,
6497 return PTR_ERR_OR_ZERO(port->psy);
6500 static enum hrtimer_restart state_machine_timer_handler(struct hrtimer *timer)
6502 struct tcpm_port *port = container_of(timer, struct tcpm_port, state_machine_timer);
6504 if (port->registered)
6505 kthread_queue_work(port->wq, &port->state_machine);
6506 return HRTIMER_NORESTART;
6509 static enum hrtimer_restart vdm_state_machine_timer_handler(struct hrtimer *timer)
6511 struct tcpm_port *port = container_of(timer, struct tcpm_port, vdm_state_machine_timer);
6513 if (port->registered)
6514 kthread_queue_work(port->wq, &port->vdm_state_machine);
6515 return HRTIMER_NORESTART;
6518 static enum hrtimer_restart enable_frs_timer_handler(struct hrtimer *timer)
6520 struct tcpm_port *port = container_of(timer, struct tcpm_port, enable_frs_timer);
6522 if (port->registered)
6523 kthread_queue_work(port->wq, &port->enable_frs);
6524 return HRTIMER_NORESTART;
6527 static enum hrtimer_restart send_discover_timer_handler(struct hrtimer *timer)
6529 struct tcpm_port *port = container_of(timer, struct tcpm_port, send_discover_timer);
6531 if (port->registered)
6532 kthread_queue_work(port->wq, &port->send_discover_work);
6533 return HRTIMER_NORESTART;
6536 struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
6538 struct tcpm_port *port;
6541 if (!dev || !tcpc ||
6542 !tcpc->get_vbus || !tcpc->set_cc || !tcpc->get_cc ||
6543 !tcpc->set_polarity || !tcpc->set_vconn || !tcpc->set_vbus ||
6544 !tcpc->set_pd_rx || !tcpc->set_roles || !tcpc->pd_transmit)
6545 return ERR_PTR(-EINVAL);
6547 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
6549 return ERR_PTR(-ENOMEM);
6554 mutex_init(&port->lock);
6555 mutex_init(&port->swap_lock);
6557 port->wq = kthread_create_worker(0, dev_name(dev));
6558 if (IS_ERR(port->wq))
6559 return ERR_CAST(port->wq);
6560 sched_set_fifo(port->wq->task);
6562 kthread_init_work(&port->state_machine, tcpm_state_machine_work);
6563 kthread_init_work(&port->vdm_state_machine, vdm_state_machine_work);
6564 kthread_init_work(&port->event_work, tcpm_pd_event_handler);
6565 kthread_init_work(&port->enable_frs, tcpm_enable_frs_work);
6566 kthread_init_work(&port->send_discover_work, tcpm_send_discover_work);
6567 hrtimer_init(&port->state_machine_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6568 port->state_machine_timer.function = state_machine_timer_handler;
6569 hrtimer_init(&port->vdm_state_machine_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6570 port->vdm_state_machine_timer.function = vdm_state_machine_timer_handler;
6571 hrtimer_init(&port->enable_frs_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6572 port->enable_frs_timer.function = enable_frs_timer_handler;
6573 hrtimer_init(&port->send_discover_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6574 port->send_discover_timer.function = send_discover_timer_handler;
6576 spin_lock_init(&port->pd_event_lock);
6578 init_completion(&port->tx_complete);
6579 init_completion(&port->swap_complete);
6580 init_completion(&port->pps_complete);
6581 tcpm_debugfs_init(port);
6583 err = tcpm_fw_get_caps(port, tcpc->fwnode);
6585 goto out_destroy_wq;
6587 port->try_role = port->typec_caps.prefer_role;
6589 port->typec_caps.fwnode = tcpc->fwnode;
6590 port->typec_caps.revision = 0x0120; /* Type-C spec release 1.2 */
6591 port->typec_caps.pd_revision = 0x0300; /* USB-PD spec release 3.0 */
6592 port->typec_caps.svdm_version = SVDM_VER_2_0;
6593 port->typec_caps.driver_data = port;
6594 port->typec_caps.ops = &tcpm_ops;
6595 port->typec_caps.orientation_aware = 1;
6597 port->partner_desc.identity = &port->partner_ident;
6598 port->port_type = port->typec_caps.type;
6600 port->role_sw = usb_role_switch_get(port->dev);
6602 port->role_sw = fwnode_usb_role_switch_get(tcpc->fwnode);
6603 if (IS_ERR(port->role_sw)) {
6604 err = PTR_ERR(port->role_sw);
6605 goto out_destroy_wq;
6608 err = devm_tcpm_psy_register(port);
6610 goto out_role_sw_put;
6611 power_supply_changed(port->psy);
6613 err = tcpm_port_register_pd(port);
6615 goto out_role_sw_put;
6617 port->typec_caps.pd = port->pd;
6619 port->typec_port = typec_register_port(port->dev, &port->typec_caps);
6620 if (IS_ERR(port->typec_port)) {
6621 err = PTR_ERR(port->typec_port);
6622 goto out_unregister_pd;
6625 typec_port_register_altmodes(port->typec_port,
6626 &tcpm_altmode_ops, port,
6627 port->port_altmode, ALTMODE_DISCOVERY_MAX);
6628 port->registered = true;
6630 mutex_lock(&port->lock);
6632 mutex_unlock(&port->lock);
6634 tcpm_log(port, "%s: registered", dev_name(dev));
6638 tcpm_port_unregister_pd(port);
6640 usb_role_switch_put(port->role_sw);
6642 tcpm_debugfs_exit(port);
6643 kthread_destroy_worker(port->wq);
6644 return ERR_PTR(err);
6646 EXPORT_SYMBOL_GPL(tcpm_register_port);
6648 void tcpm_unregister_port(struct tcpm_port *port)
6652 port->registered = false;
6653 kthread_destroy_worker(port->wq);
6655 hrtimer_cancel(&port->send_discover_timer);
6656 hrtimer_cancel(&port->enable_frs_timer);
6657 hrtimer_cancel(&port->vdm_state_machine_timer);
6658 hrtimer_cancel(&port->state_machine_timer);
6660 tcpm_reset_port(port);
6662 tcpm_port_unregister_pd(port);
6664 for (i = 0; i < ARRAY_SIZE(port->port_altmode); i++)
6665 typec_unregister_altmode(port->port_altmode[i]);
6666 typec_unregister_port(port->typec_port);
6667 usb_role_switch_put(port->role_sw);
6668 tcpm_debugfs_exit(port);
6670 EXPORT_SYMBOL_GPL(tcpm_unregister_port);
6672 MODULE_AUTHOR("Guenter Roeck <groeck@chromium.org>");
6673 MODULE_DESCRIPTION("USB Type-C Port Manager");
6674 MODULE_LICENSE("GPL");