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/jiffies.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/proc_fs.h>
16 #include <linux/sched/clock.h>
17 #include <linux/seq_file.h>
18 #include <linux/slab.h>
19 #include <linux/spinlock.h>
20 #include <linux/usb/pd.h>
21 #include <linux/usb/pd_bdo.h>
22 #include <linux/usb/pd_vdo.h>
23 #include <linux/usb/tcpm.h>
24 #include <linux/usb/typec.h>
25 #include <linux/workqueue.h>
27 #define FOREACH_STATE(S) \
34 S(SRC_SEND_CAPABILITIES), \
35 S(SRC_NEGOTIATE_CAPABILITIES), \
36 S(SRC_TRANSITION_SUPPLY), \
38 S(SRC_WAIT_NEW_CAPABILITIES), \
46 S(SNK_DISCOVERY_DEBOUNCE), \
47 S(SNK_DISCOVERY_DEBOUNCE_DONE), \
48 S(SNK_WAIT_CAPABILITIES), \
49 S(SNK_NEGOTIATE_CAPABILITIES), \
50 S(SNK_TRANSITION_SINK), \
51 S(SNK_TRANSITION_SINK_VBUS), \
55 S(DEBUG_ACC_ATTACHED), \
56 S(AUDIO_ACC_ATTACHED), \
57 S(AUDIO_ACC_DEBOUNCE), \
60 S(HARD_RESET_START), \
61 S(SRC_HARD_RESET_VBUS_OFF), \
62 S(SRC_HARD_RESET_VBUS_ON), \
63 S(SNK_HARD_RESET_SINK_OFF), \
64 S(SNK_HARD_RESET_WAIT_VBUS), \
65 S(SNK_HARD_RESET_SINK_ON), \
72 S(DR_SWAP_SEND_TIMEOUT), \
74 S(DR_SWAP_CHANGE_DR), \
78 S(PR_SWAP_SEND_TIMEOUT), \
81 S(PR_SWAP_SRC_SNK_TRANSITION_OFF), \
82 S(PR_SWAP_SRC_SNK_SOURCE_OFF), \
83 S(PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED), \
84 S(PR_SWAP_SRC_SNK_SINK_ON), \
85 S(PR_SWAP_SNK_SRC_SINK_OFF), \
86 S(PR_SWAP_SNK_SRC_SOURCE_ON), \
87 S(PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP), \
89 S(VCONN_SWAP_ACCEPT), \
91 S(VCONN_SWAP_SEND_TIMEOUT), \
92 S(VCONN_SWAP_CANCEL), \
93 S(VCONN_SWAP_START), \
94 S(VCONN_SWAP_WAIT_FOR_VCONN), \
95 S(VCONN_SWAP_TURN_ON_VCONN), \
96 S(VCONN_SWAP_TURN_OFF_VCONN), \
100 S(SNK_TRY_WAIT_DEBOUNCE), \
101 S(SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS), \
103 S(SRC_TRYWAIT_DEBOUNCE), \
104 S(SRC_TRYWAIT_UNATTACHED), \
108 S(SRC_TRY_DEBOUNCE), \
110 S(SNK_TRYWAIT_DEBOUNCE), \
111 S(SNK_TRYWAIT_VBUS), \
116 S(PORT_RESET_WAIT_OFF)
118 #define GENERATE_ENUM(e) e
119 #define GENERATE_STRING(s) #s
122 FOREACH_STATE(GENERATE_ENUM
)
125 static const char * const tcpm_states
[] = {
126 FOREACH_STATE(GENERATE_STRING
)
130 VDM_STATE_ERR_BUSY
= -3,
131 VDM_STATE_ERR_SEND
= -2,
132 VDM_STATE_ERR_TMOUT
= -1,
134 /* Anything >0 represents an active state */
137 VDM_STATE_WAIT_RSP_BUSY
= 3,
140 enum pd_msg_request
{
144 PD_MSG_DATA_SINK_CAP
,
145 PD_MSG_DATA_SOURCE_CAP
,
148 /* Events from low level driver */
150 #define TCPM_CC_EVENT BIT(0)
151 #define TCPM_VBUS_EVENT BIT(1)
152 #define TCPM_RESET_EVENT BIT(2)
154 #define LOG_BUFFER_ENTRIES 1024
155 #define LOG_BUFFER_ENTRY_SIZE 128
157 /* Alternate mode support */
159 #define SVID_DISCOVERY_MAX 16
161 struct pd_mode_data
{
162 int svid_index
; /* current SVID index */
164 u16 svids
[SVID_DISCOVERY_MAX
];
165 int altmodes
; /* number of alternate modes */
166 struct typec_altmode_desc altmode_desc
[SVID_DISCOVERY_MAX
];
172 struct mutex lock
; /* tcpm state machine lock */
173 struct workqueue_struct
*wq
;
175 struct typec_capability typec_caps
;
176 struct typec_port
*typec_port
;
178 struct tcpc_dev
*tcpc
;
180 enum typec_role vconn_role
;
181 enum typec_role pwr_role
;
182 enum typec_data_role data_role
;
183 enum typec_pwr_opmode pwr_opmode
;
185 struct usb_pd_identity partner_ident
;
186 struct typec_partner_desc partner_desc
;
187 struct typec_partner
*partner
;
189 enum typec_cc_status cc_req
;
191 enum typec_cc_status cc1
;
192 enum typec_cc_status cc2
;
193 enum typec_cc_polarity polarity
;
197 enum typec_port_type port_type
;
210 enum pd_msg_request queued_message
;
212 enum tcpm_state enter_state
;
213 enum tcpm_state prev_state
;
214 enum tcpm_state state
;
215 enum tcpm_state delayed_state
;
216 unsigned long delayed_runtime
;
217 unsigned long delay_ms
;
219 spinlock_t pd_event_lock
;
222 struct work_struct event_work
;
223 struct delayed_work state_machine
;
224 struct delayed_work vdm_state_machine
;
225 bool state_machine_running
;
227 struct completion tx_complete
;
228 enum tcpm_transmit_status tx_status
;
230 struct mutex swap_lock
; /* swap command lock */
232 bool non_pd_role_swap
;
233 struct completion swap_complete
;
236 unsigned int message_id
;
237 unsigned int caps_count
;
238 unsigned int hard_reset_count
;
240 bool explicit_contract
;
241 unsigned int rx_msgid
;
243 /* Partner capabilities/requests */
245 u32 source_caps
[PDO_MAX_OBJECTS
];
246 unsigned int nr_source_caps
;
247 u32 sink_caps
[PDO_MAX_OBJECTS
];
248 unsigned int nr_sink_caps
;
250 /* Local capabilities */
251 u32 src_pdo
[PDO_MAX_OBJECTS
];
252 unsigned int nr_src_pdo
;
253 u32 snk_pdo
[PDO_MAX_OBJECTS
];
254 unsigned int nr_snk_pdo
;
255 unsigned int nr_fixed
; /* number of fixed sink PDOs */
256 unsigned int nr_var
; /* number of variable sink PDOs */
257 unsigned int nr_batt
; /* number of battery sink PDOs */
258 u32 snk_vdo
[VDO_MAX_OBJECTS
];
259 unsigned int nr_snk_vdo
;
261 unsigned int max_snk_mv
;
262 unsigned int max_snk_ma
;
263 unsigned int max_snk_mw
;
264 unsigned int operating_snk_mw
;
266 /* Requested current / voltage */
272 /* PD state for Vendor Defined Messages */
273 enum vdm_states vdm_state
;
275 /* next Vendor Defined Message to send */
276 u32 vdo_data
[VDO_MAX_SIZE
];
278 /* VDO to retry if UFP responder replied busy */
281 /* Alternate mode data */
283 struct pd_mode_data mode_data
;
284 struct typec_altmode
*partner_altmode
[SVID_DISCOVERY_MAX
];
285 struct typec_altmode
*port_altmode
[SVID_DISCOVERY_MAX
];
287 /* Deadline in jiffies to exit src_try_wait state */
288 unsigned long max_wait
;
290 #ifdef CONFIG_DEBUG_FS
291 struct dentry
*dentry
;
292 struct mutex logbuffer_lock
; /* log buffer access lock */
295 u8
*logbuffer
[LOG_BUFFER_ENTRIES
];
300 struct work_struct work
;
301 struct tcpm_port
*port
;
302 struct pd_message msg
;
305 #define tcpm_cc_is_sink(cc) \
306 ((cc) == TYPEC_CC_RP_DEF || (cc) == TYPEC_CC_RP_1_5 || \
307 (cc) == TYPEC_CC_RP_3_0)
309 #define tcpm_port_is_sink(port) \
310 ((tcpm_cc_is_sink((port)->cc1) && !tcpm_cc_is_sink((port)->cc2)) || \
311 (tcpm_cc_is_sink((port)->cc2) && !tcpm_cc_is_sink((port)->cc1)))
313 #define tcpm_cc_is_source(cc) ((cc) == TYPEC_CC_RD)
314 #define tcpm_cc_is_audio(cc) ((cc) == TYPEC_CC_RA)
315 #define tcpm_cc_is_open(cc) ((cc) == TYPEC_CC_OPEN)
317 #define tcpm_port_is_source(port) \
318 ((tcpm_cc_is_source((port)->cc1) && \
319 !tcpm_cc_is_source((port)->cc2)) || \
320 (tcpm_cc_is_source((port)->cc2) && \
321 !tcpm_cc_is_source((port)->cc1)))
323 #define tcpm_port_is_debug(port) \
324 (tcpm_cc_is_source((port)->cc1) && tcpm_cc_is_source((port)->cc2))
326 #define tcpm_port_is_audio(port) \
327 (tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_audio((port)->cc2))
329 #define tcpm_port_is_audio_detached(port) \
330 ((tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_open((port)->cc2)) || \
331 (tcpm_cc_is_audio((port)->cc2) && tcpm_cc_is_open((port)->cc1)))
333 #define tcpm_try_snk(port) \
334 ((port)->try_snk_count == 0 && (port)->try_role == TYPEC_SINK && \
335 (port)->port_type == TYPEC_PORT_DRP)
337 #define tcpm_try_src(port) \
338 ((port)->try_src_count == 0 && (port)->try_role == TYPEC_SOURCE && \
339 (port)->port_type == TYPEC_PORT_DRP)
341 static enum tcpm_state
tcpm_default_state(struct tcpm_port
*port
)
343 if (port
->port_type
== TYPEC_PORT_DRP
) {
344 if (port
->try_role
== TYPEC_SINK
)
345 return SNK_UNATTACHED
;
346 else if (port
->try_role
== TYPEC_SOURCE
)
347 return SRC_UNATTACHED
;
348 else if (port
->tcpc
->config
->default_role
== TYPEC_SINK
)
349 return SNK_UNATTACHED
;
350 /* Fall through to return SRC_UNATTACHED */
351 } else if (port
->port_type
== TYPEC_PORT_UFP
) {
352 return SNK_UNATTACHED
;
354 return SRC_UNATTACHED
;
358 struct tcpm_port
*typec_cap_to_tcpm(const struct typec_capability
*cap
)
360 return container_of(cap
, struct tcpm_port
, typec_caps
);
363 static bool tcpm_port_is_disconnected(struct tcpm_port
*port
)
365 return (!port
->attached
&& port
->cc1
== TYPEC_CC_OPEN
&&
366 port
->cc2
== TYPEC_CC_OPEN
) ||
367 (port
->attached
&& ((port
->polarity
== TYPEC_POLARITY_CC1
&&
368 port
->cc1
== TYPEC_CC_OPEN
) ||
369 (port
->polarity
== TYPEC_POLARITY_CC2
&&
370 port
->cc2
== TYPEC_CC_OPEN
)));
377 #ifdef CONFIG_DEBUG_FS
379 static bool tcpm_log_full(struct tcpm_port
*port
)
381 return port
->logbuffer_tail
==
382 (port
->logbuffer_head
+ 1) % LOG_BUFFER_ENTRIES
;
386 static void _tcpm_log(struct tcpm_port
*port
, const char *fmt
, va_list args
)
388 char tmpbuffer
[LOG_BUFFER_ENTRY_SIZE
];
389 u64 ts_nsec
= local_clock();
390 unsigned long rem_nsec
;
392 if (!port
->logbuffer
[port
->logbuffer_head
]) {
393 port
->logbuffer
[port
->logbuffer_head
] =
394 kzalloc(LOG_BUFFER_ENTRY_SIZE
, GFP_KERNEL
);
395 if (!port
->logbuffer
[port
->logbuffer_head
])
399 vsnprintf(tmpbuffer
, sizeof(tmpbuffer
), fmt
, args
);
401 mutex_lock(&port
->logbuffer_lock
);
403 if (tcpm_log_full(port
)) {
404 port
->logbuffer_head
= max(port
->logbuffer_head
- 1, 0);
405 strcpy(tmpbuffer
, "overflow");
408 if (port
->logbuffer_head
< 0 ||
409 port
->logbuffer_head
>= LOG_BUFFER_ENTRIES
) {
411 "Bad log buffer index %d\n", port
->logbuffer_head
);
415 if (!port
->logbuffer
[port
->logbuffer_head
]) {
417 "Log buffer index %d is NULL\n", port
->logbuffer_head
);
421 rem_nsec
= do_div(ts_nsec
, 1000000000);
422 scnprintf(port
->logbuffer
[port
->logbuffer_head
],
423 LOG_BUFFER_ENTRY_SIZE
, "[%5lu.%06lu] %s",
424 (unsigned long)ts_nsec
, rem_nsec
/ 1000,
426 port
->logbuffer_head
= (port
->logbuffer_head
+ 1) % LOG_BUFFER_ENTRIES
;
429 mutex_unlock(&port
->logbuffer_lock
);
433 static void tcpm_log(struct tcpm_port
*port
, const char *fmt
, ...)
437 /* Do not log while disconnected and unattached */
438 if (tcpm_port_is_disconnected(port
) &&
439 (port
->state
== SRC_UNATTACHED
|| port
->state
== SNK_UNATTACHED
||
440 port
->state
== DRP_TOGGLING
))
444 _tcpm_log(port
, fmt
, args
);
449 static void tcpm_log_force(struct tcpm_port
*port
, const char *fmt
, ...)
454 _tcpm_log(port
, fmt
, args
);
458 static void tcpm_log_source_caps(struct tcpm_port
*port
)
462 for (i
= 0; i
< port
->nr_source_caps
; i
++) {
463 u32 pdo
= port
->source_caps
[i
];
464 enum pd_pdo_type type
= pdo_type(pdo
);
469 scnprintf(msg
, sizeof(msg
),
470 "%u mV, %u mA [%s%s%s%s%s%s]",
471 pdo_fixed_voltage(pdo
),
472 pdo_max_current(pdo
),
473 (pdo
& PDO_FIXED_DUAL_ROLE
) ?
475 (pdo
& PDO_FIXED_SUSPEND
) ?
477 (pdo
& PDO_FIXED_HIGHER_CAP
) ?
479 (pdo
& PDO_FIXED_USB_COMM
) ?
481 (pdo
& PDO_FIXED_DATA_SWAP
) ?
483 (pdo
& PDO_FIXED_EXTPOWER
) ?
487 scnprintf(msg
, sizeof(msg
),
489 pdo_min_voltage(pdo
),
490 pdo_max_voltage(pdo
),
491 pdo_max_current(pdo
));
494 scnprintf(msg
, sizeof(msg
),
496 pdo_min_voltage(pdo
),
497 pdo_max_voltage(pdo
),
501 strcpy(msg
, "undefined");
504 tcpm_log(port
, " PDO %d: type %d, %s",
509 static int tcpm_seq_show(struct seq_file
*s
, void *v
)
511 struct tcpm_port
*port
= (struct tcpm_port
*)s
->private;
514 mutex_lock(&port
->logbuffer_lock
);
515 tail
= port
->logbuffer_tail
;
516 while (tail
!= port
->logbuffer_head
) {
517 seq_printf(s
, "%s\n", port
->logbuffer
[tail
]);
518 tail
= (tail
+ 1) % LOG_BUFFER_ENTRIES
;
520 if (!seq_has_overflowed(s
))
521 port
->logbuffer_tail
= tail
;
522 mutex_unlock(&port
->logbuffer_lock
);
527 static int tcpm_debug_open(struct inode
*inode
, struct file
*file
)
529 return single_open(file
, tcpm_seq_show
, inode
->i_private
);
532 static const struct file_operations tcpm_debug_operations
= {
533 .open
= tcpm_debug_open
,
536 .release
= single_release
,
539 static struct dentry
*rootdir
;
541 static int tcpm_debugfs_init(struct tcpm_port
*port
)
543 mutex_init(&port
->logbuffer_lock
);
544 /* /sys/kernel/debug/tcpm/usbcX */
546 rootdir
= debugfs_create_dir("tcpm", NULL
);
551 port
->dentry
= debugfs_create_file(dev_name(port
->dev
),
552 S_IFREG
| 0444, rootdir
,
553 port
, &tcpm_debug_operations
);
558 static void tcpm_debugfs_exit(struct tcpm_port
*port
)
560 debugfs_remove(port
->dentry
);
566 static void tcpm_log(const struct tcpm_port
*port
, const char *fmt
, ...) { }
568 static void tcpm_log_force(struct tcpm_port
*port
, const char *fmt
, ...) { }
569 static void tcpm_log_source_caps(struct tcpm_port
*port
) { }
570 static int tcpm_debugfs_init(const struct tcpm_port
*port
) { return 0; }
571 static void tcpm_debugfs_exit(const struct tcpm_port
*port
) { }
575 static int tcpm_pd_transmit(struct tcpm_port
*port
,
576 enum tcpm_transmit_type type
,
577 const struct pd_message
*msg
)
579 unsigned long timeout
;
583 tcpm_log(port
, "PD TX, header: %#x", le16_to_cpu(msg
->header
));
585 tcpm_log(port
, "PD TX, type: %#x", type
);
587 reinit_completion(&port
->tx_complete
);
588 ret
= port
->tcpc
->pd_transmit(port
->tcpc
, type
, msg
);
592 mutex_unlock(&port
->lock
);
593 timeout
= wait_for_completion_timeout(&port
->tx_complete
,
594 msecs_to_jiffies(PD_T_TCPC_TX_TIMEOUT
));
595 mutex_lock(&port
->lock
);
599 switch (port
->tx_status
) {
600 case TCPC_TX_SUCCESS
:
601 port
->message_id
= (port
->message_id
+ 1) & PD_HEADER_ID_MASK
;
603 case TCPC_TX_DISCARDED
:
611 void tcpm_pd_transmit_complete(struct tcpm_port
*port
,
612 enum tcpm_transmit_status status
)
614 tcpm_log(port
, "PD TX complete, status: %u", status
);
615 port
->tx_status
= status
;
616 complete(&port
->tx_complete
);
618 EXPORT_SYMBOL_GPL(tcpm_pd_transmit_complete
);
620 static int tcpm_mux_set(struct tcpm_port
*port
, enum tcpc_mux_mode mode
,
621 enum tcpc_usb_switch config
)
625 tcpm_log(port
, "Requesting mux mode %d, config %d, polarity %d",
626 mode
, config
, port
->polarity
);
629 ret
= port
->tcpc
->mux
->set(port
->tcpc
->mux
, mode
, config
,
635 static int tcpm_set_polarity(struct tcpm_port
*port
,
636 enum typec_cc_polarity polarity
)
640 tcpm_log(port
, "polarity %d", polarity
);
642 ret
= port
->tcpc
->set_polarity(port
->tcpc
, polarity
);
646 port
->polarity
= polarity
;
651 static int tcpm_set_vconn(struct tcpm_port
*port
, bool enable
)
655 tcpm_log(port
, "vconn:=%d", enable
);
657 ret
= port
->tcpc
->set_vconn(port
->tcpc
, enable
);
659 port
->vconn_role
= enable
? TYPEC_SOURCE
: TYPEC_SINK
;
660 typec_set_vconn_role(port
->typec_port
, port
->vconn_role
);
666 static u32
tcpm_get_current_limit(struct tcpm_port
*port
)
668 enum typec_cc_status cc
;
671 cc
= port
->polarity
? port
->cc2
: port
->cc1
;
673 case TYPEC_CC_RP_1_5
:
676 case TYPEC_CC_RP_3_0
:
679 case TYPEC_CC_RP_DEF
:
681 if (port
->tcpc
->get_current_limit
)
682 limit
= port
->tcpc
->get_current_limit(port
->tcpc
);
691 static int tcpm_set_current_limit(struct tcpm_port
*port
, u32 max_ma
, u32 mv
)
693 int ret
= -EOPNOTSUPP
;
695 tcpm_log(port
, "Setting voltage/current limit %u mV %u mA", mv
, max_ma
);
697 if (port
->tcpc
->set_current_limit
)
698 ret
= port
->tcpc
->set_current_limit(port
->tcpc
, max_ma
, mv
);
704 * Determine RP value to set based on maximum current supported
705 * by a port if configured as source.
706 * Returns CC value to report to link partner.
708 static enum typec_cc_status
tcpm_rp_cc(struct tcpm_port
*port
)
710 const u32
*src_pdo
= port
->src_pdo
;
711 int nr_pdo
= port
->nr_src_pdo
;
715 * Search for first entry with matching voltage.
716 * It should report the maximum supported current.
718 for (i
= 0; i
< nr_pdo
; i
++) {
719 const u32 pdo
= src_pdo
[i
];
721 if (pdo_type(pdo
) == PDO_TYPE_FIXED
&&
722 pdo_fixed_voltage(pdo
) == 5000) {
723 unsigned int curr
= pdo_max_current(pdo
);
726 return TYPEC_CC_RP_3_0
;
727 else if (curr
>= 1500)
728 return TYPEC_CC_RP_1_5
;
729 return TYPEC_CC_RP_DEF
;
733 return TYPEC_CC_RP_DEF
;
736 static int tcpm_set_attached_state(struct tcpm_port
*port
, bool attached
)
738 return port
->tcpc
->set_roles(port
->tcpc
, attached
, port
->pwr_role
,
742 static int tcpm_set_roles(struct tcpm_port
*port
, bool attached
,
743 enum typec_role role
, enum typec_data_role data
)
747 if (data
== TYPEC_HOST
)
748 ret
= tcpm_mux_set(port
, TYPEC_MUX_USB
,
749 TCPC_USB_SWITCH_CONNECT
);
751 ret
= tcpm_mux_set(port
, TYPEC_MUX_NONE
,
752 TCPC_USB_SWITCH_DISCONNECT
);
756 ret
= port
->tcpc
->set_roles(port
->tcpc
, attached
, role
, data
);
760 port
->pwr_role
= role
;
761 port
->data_role
= data
;
762 typec_set_data_role(port
->typec_port
, data
);
763 typec_set_pwr_role(port
->typec_port
, role
);
768 static int tcpm_set_pwr_role(struct tcpm_port
*port
, enum typec_role role
)
772 ret
= port
->tcpc
->set_roles(port
->tcpc
, true, role
,
777 port
->pwr_role
= role
;
778 typec_set_pwr_role(port
->typec_port
, role
);
783 static int tcpm_pd_send_source_caps(struct tcpm_port
*port
)
785 struct pd_message msg
;
788 memset(&msg
, 0, sizeof(msg
));
789 if (!port
->nr_src_pdo
) {
790 /* No source capabilities defined, sink only */
791 msg
.header
= PD_HEADER_LE(PD_CTRL_REJECT
,
794 port
->message_id
, 0);
796 msg
.header
= PD_HEADER_LE(PD_DATA_SOURCE_CAP
,
802 for (i
= 0; i
< port
->nr_src_pdo
; i
++)
803 msg
.payload
[i
] = cpu_to_le32(port
->src_pdo
[i
]);
805 return tcpm_pd_transmit(port
, TCPC_TX_SOP
, &msg
);
808 static int tcpm_pd_send_sink_caps(struct tcpm_port
*port
)
810 struct pd_message msg
;
813 memset(&msg
, 0, sizeof(msg
));
814 if (!port
->nr_snk_pdo
) {
815 /* No sink capabilities defined, source only */
816 msg
.header
= PD_HEADER_LE(PD_CTRL_REJECT
,
819 port
->message_id
, 0);
821 msg
.header
= PD_HEADER_LE(PD_DATA_SINK_CAP
,
827 for (i
= 0; i
< port
->nr_snk_pdo
; i
++)
828 msg
.payload
[i
] = cpu_to_le32(port
->snk_pdo
[i
]);
830 return tcpm_pd_transmit(port
, TCPC_TX_SOP
, &msg
);
833 static void tcpm_set_state(struct tcpm_port
*port
, enum tcpm_state state
,
834 unsigned int delay_ms
)
837 tcpm_log(port
, "pending state change %s -> %s @ %u ms",
838 tcpm_states
[port
->state
], tcpm_states
[state
],
840 port
->delayed_state
= state
;
841 mod_delayed_work(port
->wq
, &port
->state_machine
,
842 msecs_to_jiffies(delay_ms
));
843 port
->delayed_runtime
= jiffies
+ msecs_to_jiffies(delay_ms
);
844 port
->delay_ms
= delay_ms
;
846 tcpm_log(port
, "state change %s -> %s",
847 tcpm_states
[port
->state
], tcpm_states
[state
]);
848 port
->delayed_state
= INVALID_STATE
;
849 port
->prev_state
= port
->state
;
852 * Don't re-queue the state machine work item if we're currently
853 * in the state machine and we're immediately changing states.
854 * tcpm_state_machine_work() will continue running the state
857 if (!port
->state_machine_running
)
858 mod_delayed_work(port
->wq
, &port
->state_machine
, 0);
862 static void tcpm_set_state_cond(struct tcpm_port
*port
, enum tcpm_state state
,
863 unsigned int delay_ms
)
865 if (port
->enter_state
== port
->state
)
866 tcpm_set_state(port
, state
, delay_ms
);
869 "skipped %sstate change %s -> %s [%u ms], context state %s",
870 delay_ms
? "delayed " : "",
871 tcpm_states
[port
->state
], tcpm_states
[state
],
872 delay_ms
, tcpm_states
[port
->enter_state
]);
875 static void tcpm_queue_message(struct tcpm_port
*port
,
876 enum pd_msg_request message
)
878 port
->queued_message
= message
;
879 mod_delayed_work(port
->wq
, &port
->state_machine
, 0);
883 * VDM/VDO handling functions
885 static void tcpm_queue_vdm(struct tcpm_port
*port
, const u32 header
,
886 const u32
*data
, int cnt
)
888 port
->vdo_count
= cnt
+ 1;
889 port
->vdo_data
[0] = header
;
890 memcpy(&port
->vdo_data
[1], data
, sizeof(u32
) * cnt
);
891 /* Set ready, vdm state machine will actually send */
892 port
->vdm_retries
= 0;
893 port
->vdm_state
= VDM_STATE_READY
;
896 static void svdm_consume_identity(struct tcpm_port
*port
, const __le32
*payload
,
899 u32 vdo
= le32_to_cpu(payload
[VDO_INDEX_IDH
]);
900 u32 product
= le32_to_cpu(payload
[VDO_INDEX_PRODUCT
]);
902 memset(&port
->mode_data
, 0, sizeof(port
->mode_data
));
904 port
->partner_ident
.id_header
= vdo
;
905 port
->partner_ident
.cert_stat
= le32_to_cpu(payload
[VDO_INDEX_CSTAT
]);
906 port
->partner_ident
.product
= product
;
908 typec_partner_set_identity(port
->partner
);
910 tcpm_log(port
, "Identity: %04x:%04x.%04x",
912 PD_PRODUCT_PID(product
), product
& 0xffff);
915 static bool svdm_consume_svids(struct tcpm_port
*port
, const __le32
*payload
,
918 struct pd_mode_data
*pmdata
= &port
->mode_data
;
921 for (i
= 1; i
< cnt
; i
++) {
922 u32 p
= le32_to_cpu(payload
[i
]);
925 svid
= (p
>> 16) & 0xffff;
929 if (pmdata
->nsvids
>= SVID_DISCOVERY_MAX
)
932 pmdata
->svids
[pmdata
->nsvids
++] = svid
;
933 tcpm_log(port
, "SVID %d: 0x%x", pmdata
->nsvids
, svid
);
939 if (pmdata
->nsvids
>= SVID_DISCOVERY_MAX
)
942 pmdata
->svids
[pmdata
->nsvids
++] = svid
;
943 tcpm_log(port
, "SVID %d: 0x%x", pmdata
->nsvids
, svid
);
947 tcpm_log(port
, "SVID_DISCOVERY_MAX(%d) too low!", SVID_DISCOVERY_MAX
);
951 static void svdm_consume_modes(struct tcpm_port
*port
, const __le32
*payload
,
954 struct pd_mode_data
*pmdata
= &port
->mode_data
;
955 struct typec_altmode_desc
*paltmode
;
956 struct typec_mode_desc
*pmode
;
959 if (pmdata
->altmodes
>= ARRAY_SIZE(port
->partner_altmode
)) {
960 /* Already logged in svdm_consume_svids() */
964 paltmode
= &pmdata
->altmode_desc
[pmdata
->altmodes
];
965 memset(paltmode
, 0, sizeof(*paltmode
));
967 paltmode
->svid
= pmdata
->svids
[pmdata
->svid_index
];
969 tcpm_log(port
, " Alternate mode %d: SVID 0x%04x",
970 pmdata
->altmodes
, paltmode
->svid
);
972 for (i
= 1; i
< cnt
&& paltmode
->n_modes
< ALTMODE_MAX_MODES
; i
++) {
973 pmode
= &paltmode
->modes
[paltmode
->n_modes
];
974 memset(pmode
, 0, sizeof(*pmode
));
975 pmode
->vdo
= le32_to_cpu(payload
[i
]);
976 pmode
->index
= i
- 1;
978 tcpm_log(port
, " VDO %d: 0x%08x",
979 pmode
->index
, pmode
->vdo
);
981 port
->partner_altmode
[pmdata
->altmodes
] =
982 typec_partner_register_altmode(port
->partner
, paltmode
);
983 if (!port
->partner_altmode
[pmdata
->altmodes
]) {
985 "Failed to register alternate modes for SVID 0x%04x",
992 #define supports_modal(port) PD_IDH_MODAL_SUPP((port)->partner_ident.id_header)
994 static int tcpm_pd_svdm(struct tcpm_port
*port
, const __le32
*payload
, int cnt
,
997 u32 p0
= le32_to_cpu(payload
[0]);
998 int cmd_type
= PD_VDO_CMDT(p0
);
999 int cmd
= PD_VDO_CMD(p0
);
1000 struct pd_mode_data
*modep
;
1005 tcpm_log(port
, "Rx VDM cmd 0x%x type %d cmd %d len %d",
1006 p0
, cmd_type
, cmd
, cnt
);
1008 modep
= &port
->mode_data
;
1013 case CMD_DISCOVER_IDENT
:
1014 /* 6.4.4.3.1: Only respond as UFP (device) */
1015 if (port
->data_role
== TYPEC_DEVICE
&&
1017 for (i
= 0; i
< port
->nr_snk_vdo
; i
++)
1018 response
[i
+ 1] = port
->snk_vdo
[i
];
1019 rlen
= port
->nr_snk_vdo
+ 1;
1022 case CMD_DISCOVER_SVID
:
1024 case CMD_DISCOVER_MODES
:
1026 case CMD_ENTER_MODE
:
1036 response
[0] = p0
| VDO_CMDT(CMDT_RSP_ACK
);
1037 } else if (rlen
== 0) {
1038 response
[0] = p0
| VDO_CMDT(CMDT_RSP_NAK
);
1041 response
[0] = p0
| VDO_CMDT(CMDT_RSP_BUSY
);
1046 /* silently drop message if we are not connected */
1051 case CMD_DISCOVER_IDENT
:
1053 svdm_consume_identity(port
, payload
, cnt
);
1054 response
[0] = VDO(USB_SID_PD
, 1, CMD_DISCOVER_SVID
);
1057 case CMD_DISCOVER_SVID
:
1059 if (svdm_consume_svids(port
, payload
, cnt
)) {
1060 response
[0] = VDO(USB_SID_PD
, 1,
1063 } else if (modep
->nsvids
&& supports_modal(port
)) {
1064 response
[0] = VDO(modep
->svids
[0], 1,
1065 CMD_DISCOVER_MODES
);
1069 case CMD_DISCOVER_MODES
:
1071 svdm_consume_modes(port
, payload
, cnt
);
1072 modep
->svid_index
++;
1073 if (modep
->svid_index
< modep
->nsvids
) {
1074 svid
= modep
->svids
[modep
->svid_index
];
1075 response
[0] = VDO(svid
, 1, CMD_DISCOVER_MODES
);
1078 /* enter alternate mode if/when implemented */
1081 case CMD_ENTER_MODE
:
1094 static void tcpm_handle_vdm_request(struct tcpm_port
*port
,
1095 const __le32
*payload
, int cnt
)
1098 u32 response
[8] = { };
1099 u32 p0
= le32_to_cpu(payload
[0]);
1101 if (port
->vdm_state
== VDM_STATE_BUSY
) {
1102 /* If UFP responded busy retry after timeout */
1103 if (PD_VDO_CMDT(p0
) == CMDT_RSP_BUSY
) {
1104 port
->vdm_state
= VDM_STATE_WAIT_RSP_BUSY
;
1105 port
->vdo_retry
= (p0
& ~VDO_CMDT_MASK
) |
1107 mod_delayed_work(port
->wq
, &port
->vdm_state_machine
,
1108 msecs_to_jiffies(PD_T_VDM_BUSY
));
1111 port
->vdm_state
= VDM_STATE_DONE
;
1114 if (PD_VDO_SVDM(p0
))
1115 rlen
= tcpm_pd_svdm(port
, payload
, cnt
, response
);
1118 tcpm_queue_vdm(port
, response
[0], &response
[1], rlen
- 1);
1119 mod_delayed_work(port
->wq
, &port
->vdm_state_machine
, 0);
1123 static void tcpm_send_vdm(struct tcpm_port
*port
, u32 vid
, int cmd
,
1124 const u32
*data
, int count
)
1128 if (WARN_ON(count
> VDO_MAX_SIZE
- 1))
1129 count
= VDO_MAX_SIZE
- 1;
1131 /* set VDM header with VID & CMD */
1132 header
= VDO(vid
, ((vid
& USB_SID_PD
) == USB_SID_PD
) ?
1133 1 : (PD_VDO_CMD(cmd
) <= CMD_ATTENTION
), cmd
);
1134 tcpm_queue_vdm(port
, header
, data
, count
);
1136 mod_delayed_work(port
->wq
, &port
->vdm_state_machine
, 0);
1139 static unsigned int vdm_ready_timeout(u32 vdm_hdr
)
1141 unsigned int timeout
;
1142 int cmd
= PD_VDO_CMD(vdm_hdr
);
1144 /* its not a structured VDM command */
1145 if (!PD_VDO_SVDM(vdm_hdr
))
1146 return PD_T_VDM_UNSTRUCTURED
;
1148 switch (PD_VDO_CMDT(vdm_hdr
)) {
1150 if (cmd
== CMD_ENTER_MODE
|| cmd
== CMD_EXIT_MODE
)
1151 timeout
= PD_T_VDM_WAIT_MODE_E
;
1153 timeout
= PD_T_VDM_SNDR_RSP
;
1156 if (cmd
== CMD_ENTER_MODE
|| cmd
== CMD_EXIT_MODE
)
1157 timeout
= PD_T_VDM_E_MODE
;
1159 timeout
= PD_T_VDM_RCVR_RSP
;
1165 static void vdm_run_state_machine(struct tcpm_port
*port
)
1167 struct pd_message msg
;
1170 switch (port
->vdm_state
) {
1171 case VDM_STATE_READY
:
1172 /* Only transmit VDM if attached */
1173 if (!port
->attached
) {
1174 port
->vdm_state
= VDM_STATE_ERR_BUSY
;
1179 * if there's traffic or we're not in PDO ready state don't send
1182 if (port
->state
!= SRC_READY
&& port
->state
!= SNK_READY
)
1185 /* Prepare and send VDM */
1186 memset(&msg
, 0, sizeof(msg
));
1187 msg
.header
= PD_HEADER_LE(PD_DATA_VENDOR_DEF
,
1190 port
->message_id
, port
->vdo_count
);
1191 for (i
= 0; i
< port
->vdo_count
; i
++)
1192 msg
.payload
[i
] = cpu_to_le32(port
->vdo_data
[i
]);
1193 res
= tcpm_pd_transmit(port
, TCPC_TX_SOP
, &msg
);
1195 port
->vdm_state
= VDM_STATE_ERR_SEND
;
1197 unsigned long timeout
;
1199 port
->vdm_retries
= 0;
1200 port
->vdm_state
= VDM_STATE_BUSY
;
1201 timeout
= vdm_ready_timeout(port
->vdo_data
[0]);
1202 mod_delayed_work(port
->wq
, &port
->vdm_state_machine
,
1206 case VDM_STATE_WAIT_RSP_BUSY
:
1207 port
->vdo_data
[0] = port
->vdo_retry
;
1208 port
->vdo_count
= 1;
1209 port
->vdm_state
= VDM_STATE_READY
;
1211 case VDM_STATE_BUSY
:
1212 port
->vdm_state
= VDM_STATE_ERR_TMOUT
;
1214 case VDM_STATE_ERR_SEND
:
1216 * A partner which does not support USB PD will not reply,
1217 * so this is not a fatal error. At the same time, some
1218 * devices may not return GoodCRC under some circumstances,
1219 * so we need to retry.
1221 if (port
->vdm_retries
< 3) {
1222 tcpm_log(port
, "VDM Tx error, retry");
1223 port
->vdm_retries
++;
1224 port
->vdm_state
= VDM_STATE_READY
;
1232 static void vdm_state_machine_work(struct work_struct
*work
)
1234 struct tcpm_port
*port
= container_of(work
, struct tcpm_port
,
1235 vdm_state_machine
.work
);
1236 enum vdm_states prev_state
;
1238 mutex_lock(&port
->lock
);
1241 * Continue running as long as the port is not busy and there was
1245 prev_state
= port
->vdm_state
;
1246 vdm_run_state_machine(port
);
1247 } while (port
->vdm_state
!= prev_state
&&
1248 port
->vdm_state
!= VDM_STATE_BUSY
);
1250 mutex_unlock(&port
->lock
);
1256 PDO_ERR_VSAFE5V_NOT_FIRST
,
1257 PDO_ERR_PDO_TYPE_NOT_IN_ORDER
,
1258 PDO_ERR_FIXED_NOT_SORTED
,
1259 PDO_ERR_VARIABLE_BATT_NOT_SORTED
,
1263 static const char * const pdo_err_msg
[] = {
1264 [PDO_ERR_NO_VSAFE5V
] =
1265 " err: source/sink caps should atleast have vSafe5V",
1266 [PDO_ERR_VSAFE5V_NOT_FIRST
] =
1267 " err: vSafe5V Fixed Supply Object Shall always be the first object",
1268 [PDO_ERR_PDO_TYPE_NOT_IN_ORDER
] =
1269 " err: PDOs should be in the following order: Fixed; Battery; Variable",
1270 [PDO_ERR_FIXED_NOT_SORTED
] =
1271 " err: Fixed supply pdos should be in increasing order of their fixed voltage",
1272 [PDO_ERR_VARIABLE_BATT_NOT_SORTED
] =
1273 " err: Variable/Battery supply pdos should be in increasing order of their minimum voltage",
1274 [PDO_ERR_DUPE_PDO
] =
1275 " err: Variable/Batt supply pdos cannot have same min/max voltage",
1278 static enum pdo_err
tcpm_caps_err(struct tcpm_port
*port
, const u32
*pdo
,
1279 unsigned int nr_pdo
)
1283 /* Should at least contain vSafe5v */
1285 return PDO_ERR_NO_VSAFE5V
;
1287 /* The vSafe5V Fixed Supply Object Shall always be the first object */
1288 if (pdo_type(pdo
[0]) != PDO_TYPE_FIXED
||
1289 pdo_fixed_voltage(pdo
[0]) != VSAFE5V
)
1290 return PDO_ERR_VSAFE5V_NOT_FIRST
;
1292 for (i
= 1; i
< nr_pdo
; i
++) {
1293 if (pdo_type(pdo
[i
]) < pdo_type(pdo
[i
- 1])) {
1294 return PDO_ERR_PDO_TYPE_NOT_IN_ORDER
;
1295 } else if (pdo_type(pdo
[i
]) == pdo_type(pdo
[i
- 1])) {
1296 enum pd_pdo_type type
= pdo_type(pdo
[i
]);
1300 * The remaining Fixed Supply Objects, if
1301 * present, shall be sent in voltage order;
1302 * lowest to highest.
1304 case PDO_TYPE_FIXED
:
1305 if (pdo_fixed_voltage(pdo
[i
]) <=
1306 pdo_fixed_voltage(pdo
[i
- 1]))
1307 return PDO_ERR_FIXED_NOT_SORTED
;
1310 * The Battery Supply Objects and Variable
1311 * supply, if present shall be sent in Minimum
1312 * Voltage order; lowest to highest.
1316 if (pdo_min_voltage(pdo
[i
]) <
1317 pdo_min_voltage(pdo
[i
- 1]))
1318 return PDO_ERR_VARIABLE_BATT_NOT_SORTED
;
1319 else if ((pdo_min_voltage(pdo
[i
]) ==
1320 pdo_min_voltage(pdo
[i
- 1])) &&
1321 (pdo_max_voltage(pdo
[i
]) ==
1322 pdo_min_voltage(pdo
[i
- 1])))
1323 return PDO_ERR_DUPE_PDO
;
1326 tcpm_log_force(port
, " Unknown pdo type");
1334 static int tcpm_validate_caps(struct tcpm_port
*port
, const u32
*pdo
,
1335 unsigned int nr_pdo
)
1337 enum pdo_err err_index
= tcpm_caps_err(port
, pdo
, nr_pdo
);
1339 if (err_index
!= PDO_NO_ERR
) {
1340 tcpm_log_force(port
, " %s", pdo_err_msg
[err_index
]);
1348 * PD (data, control) command handling functions
1350 static void tcpm_pd_data_request(struct tcpm_port
*port
,
1351 const struct pd_message
*msg
)
1353 enum pd_data_msg_type type
= pd_header_type_le(msg
->header
);
1354 unsigned int cnt
= pd_header_cnt_le(msg
->header
);
1358 case PD_DATA_SOURCE_CAP
:
1359 if (port
->pwr_role
!= TYPEC_SINK
)
1362 for (i
= 0; i
< cnt
; i
++)
1363 port
->source_caps
[i
] = le32_to_cpu(msg
->payload
[i
]);
1365 port
->nr_source_caps
= cnt
;
1367 tcpm_log_source_caps(port
);
1369 tcpm_validate_caps(port
, port
->source_caps
,
1370 port
->nr_source_caps
);
1373 * This message may be received even if VBUS is not
1374 * present. This is quite unexpected; see USB PD
1375 * specification, sections 8.3.3.6.3.1 and 8.3.3.6.3.2.
1376 * However, at the same time, we must be ready to
1377 * receive this message and respond to it 15ms after
1378 * receiving PS_RDY during power swap operations, no matter
1379 * if VBUS is available or not (USB PD specification,
1381 * So we need to accept the message either way,
1382 * but be prepared to keep waiting for VBUS after it was
1385 tcpm_set_state(port
, SNK_NEGOTIATE_CAPABILITIES
, 0);
1387 case PD_DATA_REQUEST
:
1388 if (port
->pwr_role
!= TYPEC_SOURCE
||
1390 tcpm_queue_message(port
, PD_MSG_CTRL_REJECT
);
1393 port
->sink_request
= le32_to_cpu(msg
->payload
[0]);
1394 tcpm_set_state(port
, SRC_NEGOTIATE_CAPABILITIES
, 0);
1396 case PD_DATA_SINK_CAP
:
1397 /* We don't do anything with this at the moment... */
1398 for (i
= 0; i
< cnt
; i
++)
1399 port
->sink_caps
[i
] = le32_to_cpu(msg
->payload
[i
]);
1400 port
->nr_sink_caps
= cnt
;
1402 case PD_DATA_VENDOR_DEF
:
1403 tcpm_handle_vdm_request(port
, msg
->payload
, cnt
);
1406 if (port
->state
== SRC_READY
|| port
->state
== SNK_READY
) {
1407 port
->bist_request
= le32_to_cpu(msg
->payload
[0]);
1408 tcpm_set_state(port
, BIST_RX
, 0);
1412 tcpm_log(port
, "Unhandled data message type %#x", type
);
1417 static void tcpm_pd_ctrl_request(struct tcpm_port
*port
,
1418 const struct pd_message
*msg
)
1420 enum pd_ctrl_msg_type type
= pd_header_type_le(msg
->header
);
1421 enum tcpm_state next_state
;
1424 case PD_CTRL_GOOD_CRC
:
1427 case PD_CTRL_GET_SOURCE_CAP
:
1428 switch (port
->state
) {
1431 tcpm_queue_message(port
, PD_MSG_DATA_SOURCE_CAP
);
1434 tcpm_queue_message(port
, PD_MSG_CTRL_REJECT
);
1438 case PD_CTRL_GET_SINK_CAP
:
1439 switch (port
->state
) {
1442 tcpm_queue_message(port
, PD_MSG_DATA_SINK_CAP
);
1445 tcpm_queue_message(port
, PD_MSG_CTRL_REJECT
);
1449 case PD_CTRL_GOTO_MIN
:
1451 case PD_CTRL_PS_RDY
:
1452 switch (port
->state
) {
1453 case SNK_TRANSITION_SINK
:
1454 if (port
->vbus_present
) {
1455 tcpm_set_current_limit(port
,
1456 port
->current_limit
,
1457 port
->supply_voltage
);
1458 port
->explicit_contract
= true;
1459 tcpm_set_state(port
, SNK_READY
, 0);
1462 * Seen after power swap. Keep waiting for VBUS
1463 * in a transitional state.
1465 tcpm_set_state(port
,
1466 SNK_TRANSITION_SINK_VBUS
, 0);
1469 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED
:
1470 tcpm_set_state(port
, PR_SWAP_SRC_SNK_SINK_ON
, 0);
1472 case PR_SWAP_SNK_SRC_SINK_OFF
:
1473 tcpm_set_state(port
, PR_SWAP_SNK_SRC_SOURCE_ON
, 0);
1475 case VCONN_SWAP_WAIT_FOR_VCONN
:
1476 tcpm_set_state(port
, VCONN_SWAP_TURN_OFF_VCONN
, 0);
1482 case PD_CTRL_REJECT
:
1484 switch (port
->state
) {
1485 case SNK_NEGOTIATE_CAPABILITIES
:
1486 /* USB PD specification, Figure 8-43 */
1487 if (port
->explicit_contract
)
1488 next_state
= SNK_READY
;
1490 next_state
= SNK_WAIT_CAPABILITIES
;
1491 tcpm_set_state(port
, next_state
, 0);
1494 port
->swap_status
= (type
== PD_CTRL_WAIT
?
1495 -EAGAIN
: -EOPNOTSUPP
);
1496 tcpm_set_state(port
, DR_SWAP_CANCEL
, 0);
1499 port
->swap_status
= (type
== PD_CTRL_WAIT
?
1500 -EAGAIN
: -EOPNOTSUPP
);
1501 tcpm_set_state(port
, PR_SWAP_CANCEL
, 0);
1503 case VCONN_SWAP_SEND
:
1504 port
->swap_status
= (type
== PD_CTRL_WAIT
?
1505 -EAGAIN
: -EOPNOTSUPP
);
1506 tcpm_set_state(port
, VCONN_SWAP_CANCEL
, 0);
1512 case PD_CTRL_ACCEPT
:
1513 switch (port
->state
) {
1514 case SNK_NEGOTIATE_CAPABILITIES
:
1515 tcpm_set_state(port
, SNK_TRANSITION_SINK
, 0);
1517 case SOFT_RESET_SEND
:
1518 port
->message_id
= 0;
1519 port
->rx_msgid
= -1;
1520 if (port
->pwr_role
== TYPEC_SOURCE
)
1521 next_state
= SRC_SEND_CAPABILITIES
;
1523 next_state
= SNK_WAIT_CAPABILITIES
;
1524 tcpm_set_state(port
, next_state
, 0);
1527 tcpm_set_state(port
, DR_SWAP_CHANGE_DR
, 0);
1530 tcpm_set_state(port
, PR_SWAP_START
, 0);
1532 case VCONN_SWAP_SEND
:
1533 tcpm_set_state(port
, VCONN_SWAP_START
, 0);
1539 case PD_CTRL_SOFT_RESET
:
1540 tcpm_set_state(port
, SOFT_RESET
, 0);
1542 case PD_CTRL_DR_SWAP
:
1543 if (port
->port_type
!= TYPEC_PORT_DRP
) {
1544 tcpm_queue_message(port
, PD_MSG_CTRL_REJECT
);
1549 * 6.3.9: If an alternate mode is active, a request to swap
1550 * alternate modes shall trigger a port reset.
1552 switch (port
->state
) {
1555 tcpm_set_state(port
, DR_SWAP_ACCEPT
, 0);
1558 tcpm_queue_message(port
, PD_MSG_CTRL_WAIT
);
1562 case PD_CTRL_PR_SWAP
:
1563 if (port
->port_type
!= TYPEC_PORT_DRP
) {
1564 tcpm_queue_message(port
, PD_MSG_CTRL_REJECT
);
1567 switch (port
->state
) {
1570 tcpm_set_state(port
, PR_SWAP_ACCEPT
, 0);
1573 tcpm_queue_message(port
, PD_MSG_CTRL_WAIT
);
1577 case PD_CTRL_VCONN_SWAP
:
1578 switch (port
->state
) {
1581 tcpm_set_state(port
, VCONN_SWAP_ACCEPT
, 0);
1584 tcpm_queue_message(port
, PD_MSG_CTRL_WAIT
);
1589 tcpm_log(port
, "Unhandled ctrl message type %#x", type
);
1594 static void tcpm_pd_rx_handler(struct work_struct
*work
)
1596 struct pd_rx_event
*event
= container_of(work
,
1597 struct pd_rx_event
, work
);
1598 const struct pd_message
*msg
= &event
->msg
;
1599 unsigned int cnt
= pd_header_cnt_le(msg
->header
);
1600 struct tcpm_port
*port
= event
->port
;
1602 mutex_lock(&port
->lock
);
1604 tcpm_log(port
, "PD RX, header: %#x [%d]", le16_to_cpu(msg
->header
),
1607 if (port
->attached
) {
1608 enum pd_ctrl_msg_type type
= pd_header_type_le(msg
->header
);
1609 unsigned int msgid
= pd_header_msgid_le(msg
->header
);
1612 * USB PD standard, 6.6.1.2:
1613 * "... if MessageID value in a received Message is the
1614 * same as the stored value, the receiver shall return a
1615 * GoodCRC Message with that MessageID value and drop
1616 * the Message (this is a retry of an already received
1617 * Message). Note: this shall not apply to the Soft_Reset
1618 * Message which always has a MessageID value of zero."
1620 if (msgid
== port
->rx_msgid
&& type
!= PD_CTRL_SOFT_RESET
)
1622 port
->rx_msgid
= msgid
;
1625 * If both ends believe to be DFP/host, we have a data role
1628 if (!!(le16_to_cpu(msg
->header
) & PD_HEADER_DATA_ROLE
) ==
1629 (port
->data_role
== TYPEC_HOST
)) {
1631 "Data role mismatch, initiating error recovery");
1632 tcpm_set_state(port
, ERROR_RECOVERY
, 0);
1635 tcpm_pd_data_request(port
, msg
);
1637 tcpm_pd_ctrl_request(port
, msg
);
1642 mutex_unlock(&port
->lock
);
1646 void tcpm_pd_receive(struct tcpm_port
*port
, const struct pd_message
*msg
)
1648 struct pd_rx_event
*event
;
1650 event
= kzalloc(sizeof(*event
), GFP_ATOMIC
);
1654 INIT_WORK(&event
->work
, tcpm_pd_rx_handler
);
1656 memcpy(&event
->msg
, msg
, sizeof(*msg
));
1657 queue_work(port
->wq
, &event
->work
);
1659 EXPORT_SYMBOL_GPL(tcpm_pd_receive
);
1661 static int tcpm_pd_send_control(struct tcpm_port
*port
,
1662 enum pd_ctrl_msg_type type
)
1664 struct pd_message msg
;
1666 memset(&msg
, 0, sizeof(msg
));
1667 msg
.header
= PD_HEADER_LE(type
, port
->pwr_role
,
1669 port
->message_id
, 0);
1671 return tcpm_pd_transmit(port
, TCPC_TX_SOP
, &msg
);
1675 * Send queued message without affecting state.
1676 * Return true if state machine should go back to sleep,
1679 static bool tcpm_send_queued_message(struct tcpm_port
*port
)
1681 enum pd_msg_request queued_message
;
1684 queued_message
= port
->queued_message
;
1685 port
->queued_message
= PD_MSG_NONE
;
1687 switch (queued_message
) {
1688 case PD_MSG_CTRL_WAIT
:
1689 tcpm_pd_send_control(port
, PD_CTRL_WAIT
);
1691 case PD_MSG_CTRL_REJECT
:
1692 tcpm_pd_send_control(port
, PD_CTRL_REJECT
);
1694 case PD_MSG_DATA_SINK_CAP
:
1695 tcpm_pd_send_sink_caps(port
);
1697 case PD_MSG_DATA_SOURCE_CAP
:
1698 tcpm_pd_send_source_caps(port
);
1703 } while (port
->queued_message
!= PD_MSG_NONE
);
1705 if (port
->delayed_state
!= INVALID_STATE
) {
1706 if (time_is_after_jiffies(port
->delayed_runtime
)) {
1707 mod_delayed_work(port
->wq
, &port
->state_machine
,
1708 port
->delayed_runtime
- jiffies
);
1711 port
->delayed_state
= INVALID_STATE
;
1716 static int tcpm_pd_check_request(struct tcpm_port
*port
)
1718 u32 pdo
, rdo
= port
->sink_request
;
1719 unsigned int max
, op
, pdo_max
, index
;
1720 enum pd_pdo_type type
;
1722 index
= rdo_index(rdo
);
1723 if (!index
|| index
> port
->nr_src_pdo
)
1726 pdo
= port
->src_pdo
[index
- 1];
1727 type
= pdo_type(pdo
);
1729 case PDO_TYPE_FIXED
:
1731 max
= rdo_max_current(rdo
);
1732 op
= rdo_op_current(rdo
);
1733 pdo_max
= pdo_max_current(pdo
);
1737 if (max
> pdo_max
&& !(rdo
& RDO_CAP_MISMATCH
))
1740 if (type
== PDO_TYPE_FIXED
)
1742 "Requested %u mV, %u mA for %u / %u mA",
1743 pdo_fixed_voltage(pdo
), pdo_max
, op
, max
);
1746 "Requested %u -> %u mV, %u mA for %u / %u mA",
1747 pdo_min_voltage(pdo
), pdo_max_voltage(pdo
),
1751 max
= rdo_max_power(rdo
);
1752 op
= rdo_op_power(rdo
);
1753 pdo_max
= pdo_max_power(pdo
);
1757 if (max
> pdo_max
&& !(rdo
& RDO_CAP_MISMATCH
))
1760 "Requested %u -> %u mV, %u mW for %u / %u mW",
1761 pdo_min_voltage(pdo
), pdo_max_voltage(pdo
),
1768 port
->op_vsafe5v
= index
== 1;
1773 #define min_power(x, y) min(pdo_max_power(x), pdo_max_power(y))
1774 #define min_current(x, y) min(pdo_max_current(x), pdo_max_current(y))
1776 static int tcpm_pd_select_pdo(struct tcpm_port
*port
, int *sink_pdo
,
1779 unsigned int i
, j
, max_mw
= 0, max_mv
= 0, mw
= 0, mv
= 0, ma
= 0;
1783 * Select the source PDO providing the most power which has a
1786 for (i
= 0; i
< port
->nr_source_caps
; i
++) {
1787 u32 pdo
= port
->source_caps
[i
];
1788 enum pd_pdo_type type
= pdo_type(pdo
);
1790 if (type
== PDO_TYPE_FIXED
) {
1791 for (j
= 0; j
< port
->nr_fixed
; j
++) {
1792 if (pdo_fixed_voltage(pdo
) ==
1793 pdo_fixed_voltage(port
->snk_pdo
[j
])) {
1794 ma
= min_current(pdo
, port
->snk_pdo
[j
]);
1795 mv
= pdo_fixed_voltage(pdo
);
1796 mw
= ma
* mv
/ 1000;
1798 (mw
== max_mw
&& mv
> max_mv
)) {
1805 /* There could only be one fixed pdo
1806 * at a specific voltage level.
1812 } else if (type
== PDO_TYPE_BATT
) {
1813 for (j
= port
->nr_fixed
;
1814 j
< port
->nr_fixed
+
1817 if (pdo_min_voltage(pdo
) >=
1818 pdo_min_voltage(port
->snk_pdo
[j
]) &&
1819 pdo_max_voltage(pdo
) <=
1820 pdo_max_voltage(port
->snk_pdo
[j
])) {
1821 mw
= min_power(pdo
, port
->snk_pdo
[j
]);
1822 mv
= pdo_min_voltage(pdo
);
1824 (mw
== max_mw
&& mv
> max_mv
)) {
1833 } else if (type
== PDO_TYPE_VAR
) {
1834 for (j
= port
->nr_fixed
+
1836 j
< port
->nr_fixed
+
1840 if (pdo_min_voltage(pdo
) >=
1841 pdo_min_voltage(port
->snk_pdo
[j
]) &&
1842 pdo_max_voltage(pdo
) <=
1843 pdo_max_voltage(port
->snk_pdo
[j
])) {
1844 ma
= min_current(pdo
, port
->snk_pdo
[j
]);
1845 mv
= pdo_min_voltage(pdo
);
1846 mw
= ma
* mv
/ 1000;
1848 (mw
== max_mw
&& mv
> max_mv
)) {
1863 static int tcpm_pd_build_request(struct tcpm_port
*port
, u32
*rdo
)
1865 unsigned int mv
, ma
, mw
, flags
;
1866 unsigned int max_ma
, max_mw
;
1867 enum pd_pdo_type type
;
1868 int src_pdo_index
, snk_pdo_index
;
1869 u32 pdo
, matching_snk_pdo
;
1871 if (tcpm_pd_select_pdo(port
, &snk_pdo_index
, &src_pdo_index
) < 0)
1874 pdo
= port
->source_caps
[src_pdo_index
];
1875 matching_snk_pdo
= port
->snk_pdo
[snk_pdo_index
];
1876 type
= pdo_type(pdo
);
1878 if (type
== PDO_TYPE_FIXED
)
1879 mv
= pdo_fixed_voltage(pdo
);
1881 mv
= pdo_min_voltage(pdo
);
1883 /* Select maximum available current within the sink pdo's limit */
1884 if (type
== PDO_TYPE_BATT
) {
1885 mw
= min_power(pdo
, matching_snk_pdo
);
1886 ma
= 1000 * mw
/ mv
;
1888 ma
= min_current(pdo
, matching_snk_pdo
);
1889 mw
= ma
* mv
/ 1000;
1892 flags
= RDO_USB_COMM
| RDO_NO_SUSPEND
;
1894 /* Set mismatch bit if offered power is less than operating power */
1897 if (mw
< port
->operating_snk_mw
) {
1898 flags
|= RDO_CAP_MISMATCH
;
1899 if (type
== PDO_TYPE_BATT
&&
1900 (pdo_max_power(matching_snk_pdo
) > pdo_max_power(pdo
)))
1901 max_mw
= pdo_max_power(matching_snk_pdo
);
1902 else if (pdo_max_current(matching_snk_pdo
) >
1903 pdo_max_current(pdo
))
1904 max_ma
= pdo_max_current(matching_snk_pdo
);
1907 tcpm_log(port
, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d",
1908 port
->cc_req
, port
->cc1
, port
->cc2
, port
->vbus_source
,
1909 port
->vconn_role
== TYPEC_SOURCE
? "source" : "sink",
1912 if (type
== PDO_TYPE_BATT
) {
1913 *rdo
= RDO_BATT(src_pdo_index
+ 1, mw
, max_mw
, flags
);
1915 tcpm_log(port
, "Requesting PDO %d: %u mV, %u mW%s",
1916 src_pdo_index
, mv
, mw
,
1917 flags
& RDO_CAP_MISMATCH
? " [mismatch]" : "");
1919 *rdo
= RDO_FIXED(src_pdo_index
+ 1, ma
, max_ma
, flags
);
1921 tcpm_log(port
, "Requesting PDO %d: %u mV, %u mA%s",
1922 src_pdo_index
, mv
, ma
,
1923 flags
& RDO_CAP_MISMATCH
? " [mismatch]" : "");
1926 port
->current_limit
= ma
;
1927 port
->supply_voltage
= mv
;
1932 static int tcpm_pd_send_request(struct tcpm_port
*port
)
1934 struct pd_message msg
;
1938 ret
= tcpm_pd_build_request(port
, &rdo
);
1942 memset(&msg
, 0, sizeof(msg
));
1943 msg
.header
= PD_HEADER_LE(PD_DATA_REQUEST
,
1946 port
->message_id
, 1);
1947 msg
.payload
[0] = cpu_to_le32(rdo
);
1949 return tcpm_pd_transmit(port
, TCPC_TX_SOP
, &msg
);
1952 static int tcpm_set_vbus(struct tcpm_port
*port
, bool enable
)
1956 if (enable
&& port
->vbus_charge
)
1959 tcpm_log(port
, "vbus:=%d charge=%d", enable
, port
->vbus_charge
);
1961 ret
= port
->tcpc
->set_vbus(port
->tcpc
, enable
, port
->vbus_charge
);
1965 port
->vbus_source
= enable
;
1969 static int tcpm_set_charge(struct tcpm_port
*port
, bool charge
)
1973 if (charge
&& port
->vbus_source
)
1976 if (charge
!= port
->vbus_charge
) {
1977 tcpm_log(port
, "vbus=%d charge:=%d", port
->vbus_source
, charge
);
1978 ret
= port
->tcpc
->set_vbus(port
->tcpc
, port
->vbus_source
,
1983 port
->vbus_charge
= charge
;
1987 static bool tcpm_start_drp_toggling(struct tcpm_port
*port
)
1991 if (port
->tcpc
->start_drp_toggling
&&
1992 port
->port_type
== TYPEC_PORT_DRP
) {
1993 tcpm_log_force(port
, "Start DRP toggling");
1994 ret
= port
->tcpc
->start_drp_toggling(port
->tcpc
,
2003 static void tcpm_set_cc(struct tcpm_port
*port
, enum typec_cc_status cc
)
2005 tcpm_log(port
, "cc:=%d", cc
);
2007 port
->tcpc
->set_cc(port
->tcpc
, cc
);
2010 static int tcpm_init_vbus(struct tcpm_port
*port
)
2014 ret
= port
->tcpc
->set_vbus(port
->tcpc
, false, false);
2015 port
->vbus_source
= false;
2016 port
->vbus_charge
= false;
2020 static int tcpm_init_vconn(struct tcpm_port
*port
)
2024 ret
= port
->tcpc
->set_vconn(port
->tcpc
, false);
2025 port
->vconn_role
= TYPEC_SINK
;
2029 static void tcpm_typec_connect(struct tcpm_port
*port
)
2031 if (!port
->connected
) {
2032 /* Make sure we don't report stale identity information */
2033 memset(&port
->partner_ident
, 0, sizeof(port
->partner_ident
));
2034 port
->partner_desc
.usb_pd
= port
->pd_capable
;
2035 if (tcpm_port_is_debug(port
))
2036 port
->partner_desc
.accessory
= TYPEC_ACCESSORY_DEBUG
;
2037 else if (tcpm_port_is_audio(port
))
2038 port
->partner_desc
.accessory
= TYPEC_ACCESSORY_AUDIO
;
2040 port
->partner_desc
.accessory
= TYPEC_ACCESSORY_NONE
;
2041 port
->partner
= typec_register_partner(port
->typec_port
,
2042 &port
->partner_desc
);
2043 port
->connected
= true;
2047 static int tcpm_src_attach(struct tcpm_port
*port
)
2049 enum typec_cc_polarity polarity
=
2050 port
->cc2
== TYPEC_CC_RD
? TYPEC_POLARITY_CC2
2051 : TYPEC_POLARITY_CC1
;
2057 ret
= tcpm_set_polarity(port
, polarity
);
2061 ret
= tcpm_set_roles(port
, true, TYPEC_SOURCE
, TYPEC_HOST
);
2065 ret
= port
->tcpc
->set_pd_rx(port
->tcpc
, true);
2067 goto out_disable_mux
;
2070 * USB Type-C specification, version 1.2,
2071 * chapter 4.5.2.2.8.1 (Attached.SRC Requirements)
2072 * Enable VCONN only if the non-RD port is set to RA.
2074 if ((polarity
== TYPEC_POLARITY_CC1
&& port
->cc2
== TYPEC_CC_RA
) ||
2075 (polarity
== TYPEC_POLARITY_CC2
&& port
->cc1
== TYPEC_CC_RA
)) {
2076 ret
= tcpm_set_vconn(port
, true);
2078 goto out_disable_pd
;
2081 ret
= tcpm_set_vbus(port
, true);
2083 goto out_disable_vconn
;
2085 port
->pd_capable
= false;
2087 port
->partner
= NULL
;
2089 port
->attached
= true;
2090 port
->send_discover
= true;
2095 tcpm_set_vconn(port
, false);
2097 port
->tcpc
->set_pd_rx(port
->tcpc
, false);
2099 tcpm_mux_set(port
, TYPEC_MUX_NONE
, TCPC_USB_SWITCH_DISCONNECT
);
2103 static void tcpm_typec_disconnect(struct tcpm_port
*port
)
2105 if (port
->connected
) {
2106 typec_unregister_partner(port
->partner
);
2107 port
->partner
= NULL
;
2108 port
->connected
= false;
2112 static void tcpm_unregister_altmodes(struct tcpm_port
*port
)
2114 struct pd_mode_data
*modep
= &port
->mode_data
;
2117 for (i
= 0; i
< modep
->altmodes
; i
++) {
2118 typec_unregister_altmode(port
->partner_altmode
[i
]);
2119 port
->partner_altmode
[i
] = NULL
;
2122 memset(modep
, 0, sizeof(*modep
));
2125 static void tcpm_reset_port(struct tcpm_port
*port
)
2127 tcpm_unregister_altmodes(port
);
2128 tcpm_typec_disconnect(port
);
2129 port
->attached
= false;
2130 port
->pd_capable
= false;
2133 * First Rx ID should be 0; set this to a sentinel of -1 so that
2134 * we can check tcpm_pd_rx_handler() if we had seen it before.
2136 port
->rx_msgid
= -1;
2138 port
->tcpc
->set_pd_rx(port
->tcpc
, false);
2139 tcpm_init_vbus(port
); /* also disables charging */
2140 tcpm_init_vconn(port
);
2141 tcpm_set_current_limit(port
, 0, 0);
2142 tcpm_set_polarity(port
, TYPEC_POLARITY_CC1
);
2143 tcpm_set_attached_state(port
, false);
2144 port
->try_src_count
= 0;
2145 port
->try_snk_count
= 0;
2148 static void tcpm_detach(struct tcpm_port
*port
)
2150 if (!port
->attached
)
2153 if (tcpm_port_is_disconnected(port
))
2154 port
->hard_reset_count
= 0;
2156 tcpm_reset_port(port
);
2159 static void tcpm_src_detach(struct tcpm_port
*port
)
2164 static int tcpm_snk_attach(struct tcpm_port
*port
)
2171 ret
= tcpm_set_polarity(port
, port
->cc2
!= TYPEC_CC_OPEN
?
2172 TYPEC_POLARITY_CC2
: TYPEC_POLARITY_CC1
);
2176 ret
= tcpm_set_roles(port
, true, TYPEC_SINK
, TYPEC_DEVICE
);
2180 port
->pd_capable
= false;
2182 port
->partner
= NULL
;
2184 port
->attached
= true;
2185 port
->send_discover
= true;
2190 static void tcpm_snk_detach(struct tcpm_port
*port
)
2194 /* XXX: (Dis)connect SuperSpeed mux? */
2197 static int tcpm_acc_attach(struct tcpm_port
*port
)
2204 ret
= tcpm_set_roles(port
, true, TYPEC_SOURCE
, TYPEC_HOST
);
2208 port
->partner
= NULL
;
2210 tcpm_typec_connect(port
);
2212 port
->attached
= true;
2217 static void tcpm_acc_detach(struct tcpm_port
*port
)
2222 static inline enum tcpm_state
hard_reset_state(struct tcpm_port
*port
)
2224 if (port
->hard_reset_count
< PD_N_HARD_RESET_COUNT
)
2225 return HARD_RESET_SEND
;
2226 if (port
->pd_capable
)
2227 return ERROR_RECOVERY
;
2228 if (port
->pwr_role
== TYPEC_SOURCE
)
2229 return SRC_UNATTACHED
;
2230 if (port
->state
== SNK_WAIT_CAPABILITIES
)
2232 return SNK_UNATTACHED
;
2235 static inline enum tcpm_state
ready_state(struct tcpm_port
*port
)
2237 if (port
->pwr_role
== TYPEC_SOURCE
)
2243 static inline enum tcpm_state
unattached_state(struct tcpm_port
*port
)
2245 if (port
->port_type
== TYPEC_PORT_DRP
) {
2246 if (port
->pwr_role
== TYPEC_SOURCE
)
2247 return SRC_UNATTACHED
;
2249 return SNK_UNATTACHED
;
2250 } else if (port
->port_type
== TYPEC_PORT_DFP
) {
2251 return SRC_UNATTACHED
;
2254 return SNK_UNATTACHED
;
2257 static void tcpm_check_send_discover(struct tcpm_port
*port
)
2259 if (port
->data_role
== TYPEC_HOST
&& port
->send_discover
&&
2261 tcpm_send_vdm(port
, USB_SID_PD
, CMD_DISCOVER_IDENT
, NULL
, 0);
2262 port
->send_discover
= false;
2266 static void tcpm_swap_complete(struct tcpm_port
*port
, int result
)
2268 if (port
->swap_pending
) {
2269 port
->swap_status
= result
;
2270 port
->swap_pending
= false;
2271 port
->non_pd_role_swap
= false;
2272 complete(&port
->swap_complete
);
2276 static enum typec_pwr_opmode
tcpm_get_pwr_opmode(enum typec_cc_status cc
)
2279 case TYPEC_CC_RP_1_5
:
2280 return TYPEC_PWR_MODE_1_5A
;
2281 case TYPEC_CC_RP_3_0
:
2282 return TYPEC_PWR_MODE_3_0A
;
2283 case TYPEC_CC_RP_DEF
:
2285 return TYPEC_PWR_MODE_USB
;
2289 static void run_state_machine(struct tcpm_port
*port
)
2292 enum typec_pwr_opmode opmode
;
2295 port
->enter_state
= port
->state
;
2296 switch (port
->state
) {
2300 case SRC_UNATTACHED
:
2301 if (!port
->non_pd_role_swap
)
2302 tcpm_swap_complete(port
, -ENOTCONN
);
2303 tcpm_src_detach(port
);
2304 if (tcpm_start_drp_toggling(port
)) {
2305 tcpm_set_state(port
, DRP_TOGGLING
, 0);
2308 tcpm_set_cc(port
, tcpm_rp_cc(port
));
2309 if (port
->port_type
== TYPEC_PORT_DRP
)
2310 tcpm_set_state(port
, SNK_UNATTACHED
, PD_T_DRP_SNK
);
2312 case SRC_ATTACH_WAIT
:
2313 if (tcpm_port_is_debug(port
))
2314 tcpm_set_state(port
, DEBUG_ACC_ATTACHED
,
2316 else if (tcpm_port_is_audio(port
))
2317 tcpm_set_state(port
, AUDIO_ACC_ATTACHED
,
2319 else if (tcpm_port_is_source(port
))
2320 tcpm_set_state(port
,
2321 tcpm_try_snk(port
) ? SNK_TRY
2327 port
->try_snk_count
++;
2330 * - Do not drive vconn or vbus
2331 * - Terminate CC pins (both) to Rd
2333 * - Wait for tDRPTry (PD_T_DRP_TRY).
2334 * Until then, ignore any state changes.
2336 tcpm_set_cc(port
, TYPEC_CC_RD
);
2337 tcpm_set_state(port
, SNK_TRY_WAIT
, PD_T_DRP_TRY
);
2340 if (tcpm_port_is_sink(port
)) {
2341 tcpm_set_state(port
, SNK_TRY_WAIT_DEBOUNCE
, 0);
2343 tcpm_set_state(port
, SRC_TRYWAIT
, 0);
2347 case SNK_TRY_WAIT_DEBOUNCE
:
2348 tcpm_set_state(port
, SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS
,
2351 case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS
:
2352 if (port
->vbus_present
&& tcpm_port_is_sink(port
)) {
2353 tcpm_set_state(port
, SNK_ATTACHED
, 0);
2355 tcpm_set_state(port
, SRC_TRYWAIT
, 0);
2360 tcpm_set_cc(port
, tcpm_rp_cc(port
));
2361 if (port
->max_wait
== 0) {
2362 port
->max_wait
= jiffies
+
2363 msecs_to_jiffies(PD_T_DRP_TRY
);
2364 tcpm_set_state(port
, SRC_TRYWAIT_UNATTACHED
,
2367 if (time_is_after_jiffies(port
->max_wait
))
2368 tcpm_set_state(port
, SRC_TRYWAIT_UNATTACHED
,
2369 jiffies_to_msecs(port
->max_wait
-
2372 tcpm_set_state(port
, SNK_UNATTACHED
, 0);
2375 case SRC_TRYWAIT_DEBOUNCE
:
2376 tcpm_set_state(port
, SRC_ATTACHED
, PD_T_CC_DEBOUNCE
);
2378 case SRC_TRYWAIT_UNATTACHED
:
2379 tcpm_set_state(port
, SNK_UNATTACHED
, 0);
2383 ret
= tcpm_src_attach(port
);
2384 tcpm_set_state(port
, SRC_UNATTACHED
,
2385 ret
< 0 ? 0 : PD_T_PS_SOURCE_ON
);
2388 opmode
= tcpm_get_pwr_opmode(tcpm_rp_cc(port
));
2389 typec_set_pwr_opmode(port
->typec_port
, opmode
);
2390 port
->pwr_opmode
= TYPEC_PWR_MODE_USB
;
2391 port
->caps_count
= 0;
2392 port
->message_id
= 0;
2393 port
->rx_msgid
= -1;
2394 port
->explicit_contract
= false;
2395 tcpm_set_state(port
, SRC_SEND_CAPABILITIES
, 0);
2397 case SRC_SEND_CAPABILITIES
:
2399 if (port
->caps_count
> PD_N_CAPS_COUNT
) {
2400 tcpm_set_state(port
, SRC_READY
, 0);
2403 ret
= tcpm_pd_send_source_caps(port
);
2405 tcpm_set_state(port
, SRC_SEND_CAPABILITIES
,
2406 PD_T_SEND_SOURCE_CAP
);
2409 * Per standard, we should clear the reset counter here.
2410 * However, that can result in state machine hang-ups.
2411 * Reset it only in READY state to improve stability.
2413 /* port->hard_reset_count = 0; */
2414 port
->caps_count
= 0;
2415 port
->pd_capable
= true;
2416 tcpm_set_state_cond(port
, hard_reset_state(port
),
2417 PD_T_SEND_SOURCE_CAP
);
2420 case SRC_NEGOTIATE_CAPABILITIES
:
2421 ret
= tcpm_pd_check_request(port
);
2423 tcpm_pd_send_control(port
, PD_CTRL_REJECT
);
2424 if (!port
->explicit_contract
) {
2425 tcpm_set_state(port
,
2426 SRC_WAIT_NEW_CAPABILITIES
, 0);
2428 tcpm_set_state(port
, SRC_READY
, 0);
2431 tcpm_pd_send_control(port
, PD_CTRL_ACCEPT
);
2432 tcpm_set_state(port
, SRC_TRANSITION_SUPPLY
,
2433 PD_T_SRC_TRANSITION
);
2436 case SRC_TRANSITION_SUPPLY
:
2437 /* XXX: regulator_set_voltage(vbus, ...) */
2438 tcpm_pd_send_control(port
, PD_CTRL_PS_RDY
);
2439 port
->explicit_contract
= true;
2440 typec_set_pwr_opmode(port
->typec_port
, TYPEC_PWR_MODE_PD
);
2441 port
->pwr_opmode
= TYPEC_PWR_MODE_PD
;
2442 tcpm_set_state_cond(port
, SRC_READY
, 0);
2446 port
->hard_reset_count
= 0;
2448 port
->try_src_count
= 0;
2450 tcpm_swap_complete(port
, 0);
2451 tcpm_typec_connect(port
);
2452 tcpm_check_send_discover(port
);
2455 * Sending ping messages is not necessary if
2456 * - the source operates at vSafe5V
2458 * - The system is not operating in PD mode
2460 * - Both partners are connected using a Type-C connector
2462 * There is no actual need to send PD messages since the local
2463 * port type-c and the spec does not clearly say whether PD is
2464 * possible when type-c is connected to Type-A/B
2467 case SRC_WAIT_NEW_CAPABILITIES
:
2468 /* Nothing to do... */
2472 case SNK_UNATTACHED
:
2473 if (!port
->non_pd_role_swap
)
2474 tcpm_swap_complete(port
, -ENOTCONN
);
2475 tcpm_snk_detach(port
);
2476 if (tcpm_start_drp_toggling(port
)) {
2477 tcpm_set_state(port
, DRP_TOGGLING
, 0);
2480 tcpm_set_cc(port
, TYPEC_CC_RD
);
2481 if (port
->port_type
== TYPEC_PORT_DRP
)
2482 tcpm_set_state(port
, SRC_UNATTACHED
, PD_T_DRP_SRC
);
2484 case SNK_ATTACH_WAIT
:
2485 if ((port
->cc1
== TYPEC_CC_OPEN
&&
2486 port
->cc2
!= TYPEC_CC_OPEN
) ||
2487 (port
->cc1
!= TYPEC_CC_OPEN
&&
2488 port
->cc2
== TYPEC_CC_OPEN
))
2489 tcpm_set_state(port
, SNK_DEBOUNCED
,
2491 else if (tcpm_port_is_disconnected(port
))
2492 tcpm_set_state(port
, SNK_UNATTACHED
,
2496 if (tcpm_port_is_disconnected(port
))
2497 tcpm_set_state(port
, SNK_UNATTACHED
,
2499 else if (port
->vbus_present
)
2500 tcpm_set_state(port
,
2501 tcpm_try_src(port
) ? SRC_TRY
2505 /* Wait for VBUS, but not forever */
2506 tcpm_set_state(port
, PORT_RESET
, PD_T_PS_SOURCE_ON
);
2510 port
->try_src_count
++;
2511 tcpm_set_cc(port
, tcpm_rp_cc(port
));
2513 tcpm_set_state(port
, SRC_TRY_WAIT
, 0);
2516 if (port
->max_wait
== 0) {
2517 port
->max_wait
= jiffies
+
2518 msecs_to_jiffies(PD_T_DRP_TRY
);
2519 msecs
= PD_T_DRP_TRY
;
2521 if (time_is_after_jiffies(port
->max_wait
))
2522 msecs
= jiffies_to_msecs(port
->max_wait
-
2527 tcpm_set_state(port
, SNK_TRYWAIT
, msecs
);
2529 case SRC_TRY_DEBOUNCE
:
2530 tcpm_set_state(port
, SRC_ATTACHED
, PD_T_PD_DEBOUNCE
);
2533 tcpm_set_cc(port
, TYPEC_CC_RD
);
2534 tcpm_set_state(port
, SNK_TRYWAIT_VBUS
, PD_T_CC_DEBOUNCE
);
2536 case SNK_TRYWAIT_VBUS
:
2538 * TCPM stays in this state indefinitely until VBUS
2539 * is detected as long as Rp is not detected for
2540 * more than a time period of tPDDebounce.
2542 if (port
->vbus_present
&& tcpm_port_is_sink(port
)) {
2543 tcpm_set_state(port
, SNK_ATTACHED
, 0);
2546 if (!tcpm_port_is_sink(port
))
2547 tcpm_set_state(port
, SNK_TRYWAIT_DEBOUNCE
, 0);
2549 case SNK_TRYWAIT_DEBOUNCE
:
2550 tcpm_set_state(port
, SNK_UNATTACHED
, PD_T_PD_DEBOUNCE
);
2553 ret
= tcpm_snk_attach(port
);
2555 tcpm_set_state(port
, SNK_UNATTACHED
, 0);
2557 tcpm_set_state(port
, SNK_STARTUP
, 0);
2560 opmode
= tcpm_get_pwr_opmode(port
->polarity
?
2561 port
->cc2
: port
->cc1
);
2562 typec_set_pwr_opmode(port
->typec_port
, opmode
);
2563 port
->pwr_opmode
= TYPEC_PWR_MODE_USB
;
2564 port
->message_id
= 0;
2565 port
->rx_msgid
= -1;
2566 port
->explicit_contract
= false;
2567 tcpm_set_state(port
, SNK_DISCOVERY
, 0);
2570 if (port
->vbus_present
) {
2571 tcpm_set_current_limit(port
,
2572 tcpm_get_current_limit(port
),
2574 tcpm_set_charge(port
, true);
2575 tcpm_set_state(port
, SNK_WAIT_CAPABILITIES
, 0);
2579 * For DRP, timeouts differ. Also, handling is supposed to be
2580 * different and much more complex (dead battery detection;
2581 * see USB power delivery specification, section 8.3.3.6.1.5.1).
2583 tcpm_set_state(port
, hard_reset_state(port
),
2584 port
->port_type
== TYPEC_PORT_DRP
?
2585 PD_T_DB_DETECT
: PD_T_NO_RESPONSE
);
2587 case SNK_DISCOVERY_DEBOUNCE
:
2588 tcpm_set_state(port
, SNK_DISCOVERY_DEBOUNCE_DONE
,
2591 case SNK_DISCOVERY_DEBOUNCE_DONE
:
2592 if (!tcpm_port_is_disconnected(port
) &&
2593 tcpm_port_is_sink(port
) &&
2594 time_is_after_jiffies(port
->delayed_runtime
)) {
2595 tcpm_set_state(port
, SNK_DISCOVERY
,
2596 port
->delayed_runtime
- jiffies
);
2599 tcpm_set_state(port
, unattached_state(port
), 0);
2601 case SNK_WAIT_CAPABILITIES
:
2602 ret
= port
->tcpc
->set_pd_rx(port
->tcpc
, true);
2604 tcpm_set_state(port
, SNK_READY
, 0);
2608 * If VBUS has never been low, and we time out waiting
2609 * for source cap, try a soft reset first, in case we
2610 * were already in a stable contract before this boot.
2611 * Do this only once.
2613 if (port
->vbus_never_low
) {
2614 port
->vbus_never_low
= false;
2615 tcpm_set_state(port
, SOFT_RESET_SEND
,
2616 PD_T_SINK_WAIT_CAP
);
2618 tcpm_set_state(port
, hard_reset_state(port
),
2619 PD_T_SINK_WAIT_CAP
);
2622 case SNK_NEGOTIATE_CAPABILITIES
:
2623 port
->pd_capable
= true;
2624 port
->hard_reset_count
= 0;
2625 ret
= tcpm_pd_send_request(port
);
2627 /* Let the Source send capabilities again. */
2628 tcpm_set_state(port
, SNK_WAIT_CAPABILITIES
, 0);
2630 tcpm_set_state_cond(port
, hard_reset_state(port
),
2631 PD_T_SENDER_RESPONSE
);
2634 case SNK_TRANSITION_SINK
:
2635 case SNK_TRANSITION_SINK_VBUS
:
2636 tcpm_set_state(port
, hard_reset_state(port
),
2637 PD_T_PS_TRANSITION
);
2640 port
->try_snk_count
= 0;
2641 if (port
->explicit_contract
) {
2642 typec_set_pwr_opmode(port
->typec_port
,
2644 port
->pwr_opmode
= TYPEC_PWR_MODE_PD
;
2647 tcpm_swap_complete(port
, 0);
2648 tcpm_typec_connect(port
);
2649 tcpm_check_send_discover(port
);
2652 /* Accessory states */
2653 case ACC_UNATTACHED
:
2654 tcpm_acc_detach(port
);
2655 tcpm_set_state(port
, SRC_UNATTACHED
, 0);
2657 case DEBUG_ACC_ATTACHED
:
2658 case AUDIO_ACC_ATTACHED
:
2659 ret
= tcpm_acc_attach(port
);
2661 tcpm_set_state(port
, ACC_UNATTACHED
, 0);
2663 case AUDIO_ACC_DEBOUNCE
:
2664 tcpm_set_state(port
, ACC_UNATTACHED
, PD_T_CC_DEBOUNCE
);
2667 /* Hard_Reset states */
2668 case HARD_RESET_SEND
:
2669 tcpm_pd_transmit(port
, TCPC_TX_HARD_RESET
, NULL
);
2670 tcpm_set_state(port
, HARD_RESET_START
, 0);
2672 case HARD_RESET_START
:
2673 port
->hard_reset_count
++;
2674 port
->tcpc
->set_pd_rx(port
->tcpc
, false);
2675 tcpm_unregister_altmodes(port
);
2676 port
->send_discover
= true;
2677 if (port
->pwr_role
== TYPEC_SOURCE
)
2678 tcpm_set_state(port
, SRC_HARD_RESET_VBUS_OFF
,
2679 PD_T_PS_HARD_RESET
);
2681 tcpm_set_state(port
, SNK_HARD_RESET_SINK_OFF
, 0);
2683 case SRC_HARD_RESET_VBUS_OFF
:
2684 tcpm_set_vconn(port
, true);
2685 tcpm_set_vbus(port
, false);
2686 tcpm_set_roles(port
, false, TYPEC_SOURCE
, TYPEC_HOST
);
2687 tcpm_set_state(port
, SRC_HARD_RESET_VBUS_ON
, PD_T_SRC_RECOVER
);
2689 case SRC_HARD_RESET_VBUS_ON
:
2690 tcpm_set_vbus(port
, true);
2691 port
->tcpc
->set_pd_rx(port
->tcpc
, true);
2692 tcpm_set_attached_state(port
, true);
2693 tcpm_set_state(port
, SRC_UNATTACHED
, PD_T_PS_SOURCE_ON
);
2695 case SNK_HARD_RESET_SINK_OFF
:
2696 tcpm_set_vconn(port
, false);
2697 tcpm_set_charge(port
, false);
2698 tcpm_set_roles(port
, false, TYPEC_SINK
, TYPEC_DEVICE
);
2700 * VBUS may or may not toggle, depending on the adapter.
2701 * If it doesn't toggle, transition to SNK_HARD_RESET_SINK_ON
2702 * directly after timeout.
2704 tcpm_set_state(port
, SNK_HARD_RESET_SINK_ON
, PD_T_SAFE_0V
);
2706 case SNK_HARD_RESET_WAIT_VBUS
:
2707 /* Assume we're disconnected if VBUS doesn't come back. */
2708 tcpm_set_state(port
, SNK_UNATTACHED
,
2709 PD_T_SRC_RECOVER_MAX
+ PD_T_SRC_TURN_ON
);
2711 case SNK_HARD_RESET_SINK_ON
:
2712 /* Note: There is no guarantee that VBUS is on in this state */
2715 * The specification suggests that dual mode ports in sink
2716 * mode should transition to state PE_SRC_Transition_to_default.
2717 * See USB power delivery specification chapter 8.3.3.6.1.3.
2718 * This would mean to to
2719 * - turn off VCONN, reset power supply
2720 * - request hardware reset
2722 * - Transition to state PE_Src_Startup
2723 * SNK only ports shall transition to state Snk_Startup
2724 * (see chapter 8.3.3.3.8).
2725 * Similar, dual-mode ports in source mode should transition
2726 * to PE_SNK_Transition_to_default.
2728 tcpm_set_attached_state(port
, true);
2729 tcpm_set_state(port
, SNK_STARTUP
, 0);
2732 /* Soft_Reset states */
2734 port
->message_id
= 0;
2735 port
->rx_msgid
= -1;
2736 tcpm_pd_send_control(port
, PD_CTRL_ACCEPT
);
2737 if (port
->pwr_role
== TYPEC_SOURCE
)
2738 tcpm_set_state(port
, SRC_SEND_CAPABILITIES
, 0);
2740 tcpm_set_state(port
, SNK_WAIT_CAPABILITIES
, 0);
2742 case SOFT_RESET_SEND
:
2743 port
->message_id
= 0;
2744 port
->rx_msgid
= -1;
2745 if (tcpm_pd_send_control(port
, PD_CTRL_SOFT_RESET
))
2746 tcpm_set_state_cond(port
, hard_reset_state(port
), 0);
2748 tcpm_set_state_cond(port
, hard_reset_state(port
),
2749 PD_T_SENDER_RESPONSE
);
2752 /* DR_Swap states */
2754 tcpm_pd_send_control(port
, PD_CTRL_DR_SWAP
);
2755 tcpm_set_state_cond(port
, DR_SWAP_SEND_TIMEOUT
,
2756 PD_T_SENDER_RESPONSE
);
2758 case DR_SWAP_ACCEPT
:
2759 tcpm_pd_send_control(port
, PD_CTRL_ACCEPT
);
2760 tcpm_set_state_cond(port
, DR_SWAP_CHANGE_DR
, 0);
2762 case DR_SWAP_SEND_TIMEOUT
:
2763 tcpm_swap_complete(port
, -ETIMEDOUT
);
2764 tcpm_set_state(port
, ready_state(port
), 0);
2766 case DR_SWAP_CHANGE_DR
:
2767 if (port
->data_role
== TYPEC_HOST
) {
2768 tcpm_unregister_altmodes(port
);
2769 tcpm_set_roles(port
, true, port
->pwr_role
,
2772 tcpm_set_roles(port
, true, port
->pwr_role
,
2774 port
->send_discover
= true;
2776 tcpm_set_state(port
, ready_state(port
), 0);
2779 /* PR_Swap states */
2780 case PR_SWAP_ACCEPT
:
2781 tcpm_pd_send_control(port
, PD_CTRL_ACCEPT
);
2782 tcpm_set_state(port
, PR_SWAP_START
, 0);
2785 tcpm_pd_send_control(port
, PD_CTRL_PR_SWAP
);
2786 tcpm_set_state_cond(port
, PR_SWAP_SEND_TIMEOUT
,
2787 PD_T_SENDER_RESPONSE
);
2789 case PR_SWAP_SEND_TIMEOUT
:
2790 tcpm_swap_complete(port
, -ETIMEDOUT
);
2791 tcpm_set_state(port
, ready_state(port
), 0);
2794 if (port
->pwr_role
== TYPEC_SOURCE
)
2795 tcpm_set_state(port
, PR_SWAP_SRC_SNK_TRANSITION_OFF
,
2796 PD_T_SRC_TRANSITION
);
2798 tcpm_set_state(port
, PR_SWAP_SNK_SRC_SINK_OFF
, 0);
2800 case PR_SWAP_SRC_SNK_TRANSITION_OFF
:
2801 tcpm_set_vbus(port
, false);
2802 port
->explicit_contract
= false;
2803 /* allow time for Vbus discharge, must be < tSrcSwapStdby */
2804 tcpm_set_state(port
, PR_SWAP_SRC_SNK_SOURCE_OFF
,
2807 case PR_SWAP_SRC_SNK_SOURCE_OFF
:
2808 tcpm_set_cc(port
, TYPEC_CC_RD
);
2809 /* allow CC debounce */
2810 tcpm_set_state(port
, PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED
,
2813 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED
:
2815 * USB-PD standard, 6.2.1.4, Port Power Role:
2816 * "During the Power Role Swap Sequence, for the initial Source
2817 * Port, the Port Power Role field shall be set to Sink in the
2818 * PS_RDY Message indicating that the initial Source’s power
2819 * supply is turned off"
2821 tcpm_set_pwr_role(port
, TYPEC_SINK
);
2822 if (tcpm_pd_send_control(port
, PD_CTRL_PS_RDY
)) {
2823 tcpm_set_state(port
, ERROR_RECOVERY
, 0);
2826 tcpm_set_state_cond(port
, SNK_UNATTACHED
, PD_T_PS_SOURCE_ON
);
2828 case PR_SWAP_SRC_SNK_SINK_ON
:
2829 tcpm_set_state(port
, SNK_STARTUP
, 0);
2831 case PR_SWAP_SNK_SRC_SINK_OFF
:
2832 tcpm_set_charge(port
, false);
2833 tcpm_set_state(port
, hard_reset_state(port
),
2834 PD_T_PS_SOURCE_OFF
);
2836 case PR_SWAP_SNK_SRC_SOURCE_ON
:
2837 tcpm_set_cc(port
, tcpm_rp_cc(port
));
2838 tcpm_set_vbus(port
, true);
2840 * allow time VBUS ramp-up, must be < tNewSrc
2841 * Also, this window overlaps with CC debounce as well.
2842 * So, Wait for the max of two which is PD_T_NEWSRC
2844 tcpm_set_state(port
, PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP
,
2847 case PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP
:
2849 * USB PD standard, 6.2.1.4:
2850 * "Subsequent Messages initiated by the Policy Engine,
2851 * such as the PS_RDY Message sent to indicate that Vbus
2852 * is ready, will have the Port Power Role field set to
2855 tcpm_set_pwr_role(port
, TYPEC_SOURCE
);
2856 tcpm_pd_send_control(port
, PD_CTRL_PS_RDY
);
2857 tcpm_set_state(port
, SRC_STARTUP
, 0);
2860 case VCONN_SWAP_ACCEPT
:
2861 tcpm_pd_send_control(port
, PD_CTRL_ACCEPT
);
2862 tcpm_set_state(port
, VCONN_SWAP_START
, 0);
2864 case VCONN_SWAP_SEND
:
2865 tcpm_pd_send_control(port
, PD_CTRL_VCONN_SWAP
);
2866 tcpm_set_state(port
, VCONN_SWAP_SEND_TIMEOUT
,
2867 PD_T_SENDER_RESPONSE
);
2869 case VCONN_SWAP_SEND_TIMEOUT
:
2870 tcpm_swap_complete(port
, -ETIMEDOUT
);
2871 tcpm_set_state(port
, ready_state(port
), 0);
2873 case VCONN_SWAP_START
:
2874 if (port
->vconn_role
== TYPEC_SOURCE
)
2875 tcpm_set_state(port
, VCONN_SWAP_WAIT_FOR_VCONN
, 0);
2877 tcpm_set_state(port
, VCONN_SWAP_TURN_ON_VCONN
, 0);
2879 case VCONN_SWAP_WAIT_FOR_VCONN
:
2880 tcpm_set_state(port
, hard_reset_state(port
),
2881 PD_T_VCONN_SOURCE_ON
);
2883 case VCONN_SWAP_TURN_ON_VCONN
:
2884 tcpm_set_vconn(port
, true);
2885 tcpm_pd_send_control(port
, PD_CTRL_PS_RDY
);
2886 tcpm_set_state(port
, ready_state(port
), 0);
2888 case VCONN_SWAP_TURN_OFF_VCONN
:
2889 tcpm_set_vconn(port
, false);
2890 tcpm_set_state(port
, ready_state(port
), 0);
2893 case DR_SWAP_CANCEL
:
2894 case PR_SWAP_CANCEL
:
2895 case VCONN_SWAP_CANCEL
:
2896 tcpm_swap_complete(port
, port
->swap_status
);
2897 if (port
->pwr_role
== TYPEC_SOURCE
)
2898 tcpm_set_state(port
, SRC_READY
, 0);
2900 tcpm_set_state(port
, SNK_READY
, 0);
2904 switch (BDO_MODE_MASK(port
->bist_request
)) {
2905 case BDO_MODE_CARRIER2
:
2906 tcpm_pd_transmit(port
, TCPC_TX_BIST_MODE_2
, NULL
);
2911 /* Always switch to unattached state */
2912 tcpm_set_state(port
, unattached_state(port
), 0);
2914 case ERROR_RECOVERY
:
2915 tcpm_swap_complete(port
, -EPROTO
);
2916 tcpm_set_state(port
, PORT_RESET
, 0);
2919 tcpm_reset_port(port
);
2920 tcpm_set_cc(port
, TYPEC_CC_OPEN
);
2921 tcpm_set_state(port
, PORT_RESET_WAIT_OFF
,
2922 PD_T_ERROR_RECOVERY
);
2924 case PORT_RESET_WAIT_OFF
:
2925 tcpm_set_state(port
,
2926 tcpm_default_state(port
),
2927 port
->vbus_present
? PD_T_PS_SOURCE_OFF
: 0);
2930 WARN(1, "Unexpected port state %d\n", port
->state
);
2935 static void tcpm_state_machine_work(struct work_struct
*work
)
2937 struct tcpm_port
*port
= container_of(work
, struct tcpm_port
,
2938 state_machine
.work
);
2939 enum tcpm_state prev_state
;
2941 mutex_lock(&port
->lock
);
2942 port
->state_machine_running
= true;
2944 if (port
->queued_message
&& tcpm_send_queued_message(port
))
2947 /* If we were queued due to a delayed state change, update it now */
2948 if (port
->delayed_state
) {
2949 tcpm_log(port
, "state change %s -> %s [delayed %ld ms]",
2950 tcpm_states
[port
->state
],
2951 tcpm_states
[port
->delayed_state
], port
->delay_ms
);
2952 port
->prev_state
= port
->state
;
2953 port
->state
= port
->delayed_state
;
2954 port
->delayed_state
= INVALID_STATE
;
2958 * Continue running as long as we have (non-delayed) state changes
2962 prev_state
= port
->state
;
2963 run_state_machine(port
);
2964 if (port
->queued_message
)
2965 tcpm_send_queued_message(port
);
2966 } while (port
->state
!= prev_state
&& !port
->delayed_state
);
2969 port
->state_machine_running
= false;
2970 mutex_unlock(&port
->lock
);
2973 static void _tcpm_cc_change(struct tcpm_port
*port
, enum typec_cc_status cc1
,
2974 enum typec_cc_status cc2
)
2976 enum typec_cc_status old_cc1
, old_cc2
;
2977 enum tcpm_state new_state
;
2979 old_cc1
= port
->cc1
;
2980 old_cc2
= port
->cc2
;
2984 tcpm_log_force(port
,
2985 "CC1: %u -> %u, CC2: %u -> %u [state %s, polarity %d, %s]",
2986 old_cc1
, cc1
, old_cc2
, cc2
, tcpm_states
[port
->state
],
2988 tcpm_port_is_disconnected(port
) ? "disconnected"
2991 switch (port
->state
) {
2993 if (tcpm_port_is_debug(port
) || tcpm_port_is_audio(port
) ||
2994 tcpm_port_is_source(port
))
2995 tcpm_set_state(port
, SRC_ATTACH_WAIT
, 0);
2996 else if (tcpm_port_is_sink(port
))
2997 tcpm_set_state(port
, SNK_ATTACH_WAIT
, 0);
2999 case SRC_UNATTACHED
:
3000 case ACC_UNATTACHED
:
3001 if (tcpm_port_is_debug(port
) || tcpm_port_is_audio(port
) ||
3002 tcpm_port_is_source(port
))
3003 tcpm_set_state(port
, SRC_ATTACH_WAIT
, 0);
3005 case SRC_ATTACH_WAIT
:
3006 if (tcpm_port_is_disconnected(port
) ||
3007 tcpm_port_is_audio_detached(port
))
3008 tcpm_set_state(port
, SRC_UNATTACHED
, 0);
3009 else if (cc1
!= old_cc1
|| cc2
!= old_cc2
)
3010 tcpm_set_state(port
, SRC_ATTACH_WAIT
, 0);
3013 case SRC_SEND_CAPABILITIES
:
3015 if (tcpm_port_is_disconnected(port
) ||
3016 !tcpm_port_is_source(port
))
3017 tcpm_set_state(port
, SRC_UNATTACHED
, 0);
3019 case SNK_UNATTACHED
:
3020 if (tcpm_port_is_sink(port
))
3021 tcpm_set_state(port
, SNK_ATTACH_WAIT
, 0);
3023 case SNK_ATTACH_WAIT
:
3024 if ((port
->cc1
== TYPEC_CC_OPEN
&&
3025 port
->cc2
!= TYPEC_CC_OPEN
) ||
3026 (port
->cc1
!= TYPEC_CC_OPEN
&&
3027 port
->cc2
== TYPEC_CC_OPEN
))
3028 new_state
= SNK_DEBOUNCED
;
3029 else if (tcpm_port_is_disconnected(port
))
3030 new_state
= SNK_UNATTACHED
;
3033 if (new_state
!= port
->delayed_state
)
3034 tcpm_set_state(port
, SNK_ATTACH_WAIT
, 0);
3037 if (tcpm_port_is_disconnected(port
))
3038 new_state
= SNK_UNATTACHED
;
3039 else if (port
->vbus_present
)
3040 new_state
= tcpm_try_src(port
) ? SRC_TRY
: SNK_ATTACHED
;
3042 new_state
= SNK_UNATTACHED
;
3043 if (new_state
!= port
->delayed_state
)
3044 tcpm_set_state(port
, SNK_DEBOUNCED
, 0);
3047 if (tcpm_port_is_disconnected(port
))
3048 tcpm_set_state(port
, unattached_state(port
), 0);
3049 else if (!port
->pd_capable
&&
3050 (cc1
!= old_cc1
|| cc2
!= old_cc2
))
3051 tcpm_set_current_limit(port
,
3052 tcpm_get_current_limit(port
),
3056 case AUDIO_ACC_ATTACHED
:
3057 if (cc1
== TYPEC_CC_OPEN
|| cc2
== TYPEC_CC_OPEN
)
3058 tcpm_set_state(port
, AUDIO_ACC_DEBOUNCE
, 0);
3060 case AUDIO_ACC_DEBOUNCE
:
3061 if (tcpm_port_is_audio(port
))
3062 tcpm_set_state(port
, AUDIO_ACC_ATTACHED
, 0);
3065 case DEBUG_ACC_ATTACHED
:
3066 if (cc1
== TYPEC_CC_OPEN
|| cc2
== TYPEC_CC_OPEN
)
3067 tcpm_set_state(port
, ACC_UNATTACHED
, 0);
3071 /* Do nothing, waiting for timeout */
3075 /* CC line is unstable, wait for debounce */
3076 if (tcpm_port_is_disconnected(port
))
3077 tcpm_set_state(port
, SNK_DISCOVERY_DEBOUNCE
, 0);
3079 case SNK_DISCOVERY_DEBOUNCE
:
3083 /* Hand over to state machine if needed */
3084 if (!port
->vbus_present
&& tcpm_port_is_source(port
))
3085 tcpm_set_state(port
, SRC_TRYWAIT_DEBOUNCE
, 0);
3087 case SRC_TRYWAIT_DEBOUNCE
:
3088 if (port
->vbus_present
|| !tcpm_port_is_source(port
))
3089 tcpm_set_state(port
, SRC_TRYWAIT
, 0);
3091 case SNK_TRY_WAIT_DEBOUNCE
:
3092 if (!tcpm_port_is_sink(port
)) {
3094 tcpm_set_state(port
, SRC_TRYWAIT
, 0);
3098 if (tcpm_port_is_source(port
))
3099 tcpm_set_state(port
, SRC_TRY_DEBOUNCE
, 0);
3101 case SRC_TRY_DEBOUNCE
:
3102 tcpm_set_state(port
, SRC_TRY_WAIT
, 0);
3104 case SNK_TRYWAIT_DEBOUNCE
:
3105 if (tcpm_port_is_sink(port
))
3106 tcpm_set_state(port
, SNK_TRYWAIT_VBUS
, 0);
3108 case SNK_TRYWAIT_VBUS
:
3109 if (!tcpm_port_is_sink(port
))
3110 tcpm_set_state(port
, SNK_TRYWAIT_DEBOUNCE
, 0);
3113 /* Do nothing, waiting for tCCDebounce */
3115 case PR_SWAP_SNK_SRC_SINK_OFF
:
3116 case PR_SWAP_SRC_SNK_TRANSITION_OFF
:
3117 case PR_SWAP_SRC_SNK_SOURCE_OFF
:
3118 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED
:
3119 case PR_SWAP_SNK_SRC_SOURCE_ON
:
3121 * CC state change is expected in PR_SWAP
3127 if (tcpm_port_is_disconnected(port
))
3128 tcpm_set_state(port
, unattached_state(port
), 0);
3133 static void _tcpm_pd_vbus_on(struct tcpm_port
*port
)
3135 tcpm_log_force(port
, "VBUS on");
3136 port
->vbus_present
= true;
3137 switch (port
->state
) {
3138 case SNK_TRANSITION_SINK_VBUS
:
3139 port
->explicit_contract
= true;
3140 tcpm_set_state(port
, SNK_READY
, 0);
3143 tcpm_set_state(port
, SNK_DISCOVERY
, 0);
3147 tcpm_set_state(port
, tcpm_try_src(port
) ? SRC_TRY
3151 case SNK_HARD_RESET_WAIT_VBUS
:
3152 tcpm_set_state(port
, SNK_HARD_RESET_SINK_ON
, 0);
3155 tcpm_set_state(port
, SRC_STARTUP
, 0);
3157 case SRC_HARD_RESET_VBUS_ON
:
3158 tcpm_set_state(port
, SRC_STARTUP
, 0);
3162 /* Do nothing, waiting for timeout */
3165 /* Do nothing, Waiting for Rd to be detected */
3167 case SRC_TRYWAIT_DEBOUNCE
:
3168 tcpm_set_state(port
, SRC_TRYWAIT
, 0);
3170 case SNK_TRY_WAIT_DEBOUNCE
:
3171 /* Do nothing, waiting for PD_DEBOUNCE to do be done */
3174 /* Do nothing, waiting for tCCDebounce */
3176 case SNK_TRYWAIT_VBUS
:
3177 if (tcpm_port_is_sink(port
))
3178 tcpm_set_state(port
, SNK_ATTACHED
, 0);
3180 case SNK_TRYWAIT_DEBOUNCE
:
3181 /* Do nothing, waiting for Rp */
3184 case SRC_TRY_DEBOUNCE
:
3185 /* Do nothing, waiting for sink detection */
3192 static void _tcpm_pd_vbus_off(struct tcpm_port
*port
)
3194 tcpm_log_force(port
, "VBUS off");
3195 port
->vbus_present
= false;
3196 port
->vbus_never_low
= false;
3197 switch (port
->state
) {
3198 case SNK_HARD_RESET_SINK_OFF
:
3199 tcpm_set_state(port
, SNK_HARD_RESET_WAIT_VBUS
, 0);
3201 case SRC_HARD_RESET_VBUS_OFF
:
3202 tcpm_set_state(port
, SRC_HARD_RESET_VBUS_ON
, 0);
3204 case HARD_RESET_SEND
:
3208 /* Do nothing, waiting for timeout */
3211 /* Hand over to state machine if needed */
3212 if (tcpm_port_is_source(port
))
3213 tcpm_set_state(port
, SRC_TRYWAIT_DEBOUNCE
, 0);
3215 case SNK_TRY_WAIT_DEBOUNCE
:
3216 /* Do nothing, waiting for PD_DEBOUNCE to do be done */
3219 case SNK_TRYWAIT_VBUS
:
3220 case SNK_TRYWAIT_DEBOUNCE
:
3222 case SNK_ATTACH_WAIT
:
3223 tcpm_set_state(port
, SNK_UNATTACHED
, 0);
3226 case SNK_NEGOTIATE_CAPABILITIES
:
3229 case PR_SWAP_SRC_SNK_TRANSITION_OFF
:
3230 tcpm_set_state(port
, PR_SWAP_SRC_SNK_SOURCE_OFF
, 0);
3233 case PR_SWAP_SNK_SRC_SINK_OFF
:
3234 /* Do nothing, expected */
3237 case PORT_RESET_WAIT_OFF
:
3238 tcpm_set_state(port
, tcpm_default_state(port
), 0);
3241 case SRC_TRY_DEBOUNCE
:
3242 /* Do nothing, waiting for sink detection */
3245 if (port
->pwr_role
== TYPEC_SINK
&&
3247 tcpm_set_state(port
, SNK_UNATTACHED
, 0);
3252 static void _tcpm_pd_hard_reset(struct tcpm_port
*port
)
3254 tcpm_log_force(port
, "Received hard reset");
3256 * If we keep receiving hard reset requests, executing the hard reset
3257 * must have failed. Revert to error recovery if that happens.
3259 tcpm_set_state(port
,
3260 port
->hard_reset_count
< PD_N_HARD_RESET_COUNT
?
3261 HARD_RESET_START
: ERROR_RECOVERY
,
3265 static void tcpm_pd_event_handler(struct work_struct
*work
)
3267 struct tcpm_port
*port
= container_of(work
, struct tcpm_port
,
3271 mutex_lock(&port
->lock
);
3273 spin_lock(&port
->pd_event_lock
);
3274 while (port
->pd_events
) {
3275 events
= port
->pd_events
;
3276 port
->pd_events
= 0;
3277 spin_unlock(&port
->pd_event_lock
);
3278 if (events
& TCPM_RESET_EVENT
)
3279 _tcpm_pd_hard_reset(port
);
3280 if (events
& TCPM_VBUS_EVENT
) {
3283 vbus
= port
->tcpc
->get_vbus(port
->tcpc
);
3285 _tcpm_pd_vbus_on(port
);
3287 _tcpm_pd_vbus_off(port
);
3289 if (events
& TCPM_CC_EVENT
) {
3290 enum typec_cc_status cc1
, cc2
;
3292 if (port
->tcpc
->get_cc(port
->tcpc
, &cc1
, &cc2
) == 0)
3293 _tcpm_cc_change(port
, cc1
, cc2
);
3295 spin_lock(&port
->pd_event_lock
);
3297 spin_unlock(&port
->pd_event_lock
);
3298 mutex_unlock(&port
->lock
);
3301 void tcpm_cc_change(struct tcpm_port
*port
)
3303 spin_lock(&port
->pd_event_lock
);
3304 port
->pd_events
|= TCPM_CC_EVENT
;
3305 spin_unlock(&port
->pd_event_lock
);
3306 queue_work(port
->wq
, &port
->event_work
);
3308 EXPORT_SYMBOL_GPL(tcpm_cc_change
);
3310 void tcpm_vbus_change(struct tcpm_port
*port
)
3312 spin_lock(&port
->pd_event_lock
);
3313 port
->pd_events
|= TCPM_VBUS_EVENT
;
3314 spin_unlock(&port
->pd_event_lock
);
3315 queue_work(port
->wq
, &port
->event_work
);
3317 EXPORT_SYMBOL_GPL(tcpm_vbus_change
);
3319 void tcpm_pd_hard_reset(struct tcpm_port
*port
)
3321 spin_lock(&port
->pd_event_lock
);
3322 port
->pd_events
= TCPM_RESET_EVENT
;
3323 spin_unlock(&port
->pd_event_lock
);
3324 queue_work(port
->wq
, &port
->event_work
);
3326 EXPORT_SYMBOL_GPL(tcpm_pd_hard_reset
);
3328 static int tcpm_dr_set(const struct typec_capability
*cap
,
3329 enum typec_data_role data
)
3331 struct tcpm_port
*port
= typec_cap_to_tcpm(cap
);
3334 mutex_lock(&port
->swap_lock
);
3335 mutex_lock(&port
->lock
);
3337 if (port
->port_type
!= TYPEC_PORT_DRP
) {
3341 if (port
->state
!= SRC_READY
&& port
->state
!= SNK_READY
) {
3346 if (port
->data_role
== data
) {
3353 * 6.3.9: If an alternate mode is active, a request to swap
3354 * alternate modes shall trigger a port reset.
3355 * Reject data role swap request in this case.
3358 if (!port
->pd_capable
) {
3360 * If the partner is not PD capable, reset the port to
3361 * trigger a role change. This can only work if a preferred
3362 * role is configured, and if it matches the requested role.
3364 if (port
->try_role
== TYPEC_NO_PREFERRED_ROLE
||
3365 port
->try_role
== port
->pwr_role
) {
3369 port
->non_pd_role_swap
= true;
3370 tcpm_set_state(port
, PORT_RESET
, 0);
3372 tcpm_set_state(port
, DR_SWAP_SEND
, 0);
3375 port
->swap_status
= 0;
3376 port
->swap_pending
= true;
3377 reinit_completion(&port
->swap_complete
);
3378 mutex_unlock(&port
->lock
);
3380 if (!wait_for_completion_timeout(&port
->swap_complete
,
3381 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT
)))
3384 ret
= port
->swap_status
;
3386 port
->non_pd_role_swap
= false;
3390 mutex_unlock(&port
->lock
);
3392 mutex_unlock(&port
->swap_lock
);
3396 static int tcpm_pr_set(const struct typec_capability
*cap
,
3397 enum typec_role role
)
3399 struct tcpm_port
*port
= typec_cap_to_tcpm(cap
);
3402 mutex_lock(&port
->swap_lock
);
3403 mutex_lock(&port
->lock
);
3405 if (port
->port_type
!= TYPEC_PORT_DRP
) {
3409 if (port
->state
!= SRC_READY
&& port
->state
!= SNK_READY
) {
3414 if (role
== port
->pwr_role
) {
3419 port
->swap_status
= 0;
3420 port
->swap_pending
= true;
3421 reinit_completion(&port
->swap_complete
);
3422 tcpm_set_state(port
, PR_SWAP_SEND
, 0);
3423 mutex_unlock(&port
->lock
);
3425 if (!wait_for_completion_timeout(&port
->swap_complete
,
3426 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT
)))
3429 ret
= port
->swap_status
;
3434 mutex_unlock(&port
->lock
);
3436 mutex_unlock(&port
->swap_lock
);
3440 static int tcpm_vconn_set(const struct typec_capability
*cap
,
3441 enum typec_role role
)
3443 struct tcpm_port
*port
= typec_cap_to_tcpm(cap
);
3446 mutex_lock(&port
->swap_lock
);
3447 mutex_lock(&port
->lock
);
3449 if (port
->state
!= SRC_READY
&& port
->state
!= SNK_READY
) {
3454 if (role
== port
->vconn_role
) {
3459 port
->swap_status
= 0;
3460 port
->swap_pending
= true;
3461 reinit_completion(&port
->swap_complete
);
3462 tcpm_set_state(port
, VCONN_SWAP_SEND
, 0);
3463 mutex_unlock(&port
->lock
);
3465 if (!wait_for_completion_timeout(&port
->swap_complete
,
3466 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT
)))
3469 ret
= port
->swap_status
;
3474 mutex_unlock(&port
->lock
);
3476 mutex_unlock(&port
->swap_lock
);
3480 static int tcpm_try_role(const struct typec_capability
*cap
, int role
)
3482 struct tcpm_port
*port
= typec_cap_to_tcpm(cap
);
3483 struct tcpc_dev
*tcpc
= port
->tcpc
;
3486 mutex_lock(&port
->lock
);
3488 ret
= tcpc
->try_role(tcpc
, role
);
3489 if (!ret
&& !tcpc
->config
->try_role_hw
)
3490 port
->try_role
= role
;
3491 port
->try_src_count
= 0;
3492 port
->try_snk_count
= 0;
3493 mutex_unlock(&port
->lock
);
3498 static void tcpm_init(struct tcpm_port
*port
)
3500 enum typec_cc_status cc1
, cc2
;
3502 port
->tcpc
->init(port
->tcpc
);
3504 tcpm_reset_port(port
);
3508 * Should possibly wait for VBUS to settle if it was enabled locally
3509 * since tcpm_reset_port() will disable VBUS.
3511 port
->vbus_present
= port
->tcpc
->get_vbus(port
->tcpc
);
3512 if (port
->vbus_present
)
3513 port
->vbus_never_low
= true;
3515 tcpm_set_state(port
, tcpm_default_state(port
), 0);
3517 if (port
->tcpc
->get_cc(port
->tcpc
, &cc1
, &cc2
) == 0)
3518 _tcpm_cc_change(port
, cc1
, cc2
);
3521 * Some adapters need a clean slate at startup, and won't recover
3522 * otherwise. So do not try to be fancy and force a clean disconnect.
3524 tcpm_set_state(port
, PORT_RESET
, 0);
3527 static int tcpm_port_type_set(const struct typec_capability
*cap
,
3528 enum typec_port_type type
)
3530 struct tcpm_port
*port
= typec_cap_to_tcpm(cap
);
3532 mutex_lock(&port
->lock
);
3533 if (type
== port
->port_type
)
3536 port
->port_type
= type
;
3538 if (!port
->connected
) {
3539 tcpm_set_state(port
, PORT_RESET
, 0);
3540 } else if (type
== TYPEC_PORT_UFP
) {
3541 if (!(port
->pwr_role
== TYPEC_SINK
&&
3542 port
->data_role
== TYPEC_DEVICE
))
3543 tcpm_set_state(port
, PORT_RESET
, 0);
3544 } else if (type
== TYPEC_PORT_DFP
) {
3545 if (!(port
->pwr_role
== TYPEC_SOURCE
&&
3546 port
->data_role
== TYPEC_HOST
))
3547 tcpm_set_state(port
, PORT_RESET
, 0);
3551 mutex_unlock(&port
->lock
);
3555 void tcpm_tcpc_reset(struct tcpm_port
*port
)
3557 mutex_lock(&port
->lock
);
3558 /* XXX: Maintain PD connection if possible? */
3560 mutex_unlock(&port
->lock
);
3562 EXPORT_SYMBOL_GPL(tcpm_tcpc_reset
);
3564 static int tcpm_copy_pdos(u32
*dest_pdo
, const u32
*src_pdo
,
3565 unsigned int nr_pdo
)
3569 if (nr_pdo
> PDO_MAX_OBJECTS
)
3570 nr_pdo
= PDO_MAX_OBJECTS
;
3572 for (i
= 0; i
< nr_pdo
; i
++)
3573 dest_pdo
[i
] = src_pdo
[i
];
3578 static int tcpm_copy_vdos(u32
*dest_vdo
, const u32
*src_vdo
,
3579 unsigned int nr_vdo
)
3583 if (nr_vdo
> VDO_MAX_OBJECTS
)
3584 nr_vdo
= VDO_MAX_OBJECTS
;
3586 for (i
= 0; i
< nr_vdo
; i
++)
3587 dest_vdo
[i
] = src_vdo
[i
];
3592 int tcpm_update_source_capabilities(struct tcpm_port
*port
, const u32
*pdo
,
3593 unsigned int nr_pdo
)
3595 if (tcpm_validate_caps(port
, pdo
, nr_pdo
))
3598 mutex_lock(&port
->lock
);
3599 port
->nr_src_pdo
= tcpm_copy_pdos(port
->src_pdo
, pdo
, nr_pdo
);
3600 switch (port
->state
) {
3601 case SRC_UNATTACHED
:
3602 case SRC_ATTACH_WAIT
:
3604 tcpm_set_cc(port
, tcpm_rp_cc(port
));
3606 case SRC_SEND_CAPABILITIES
:
3607 case SRC_NEGOTIATE_CAPABILITIES
:
3609 case SRC_WAIT_NEW_CAPABILITIES
:
3610 tcpm_set_cc(port
, tcpm_rp_cc(port
));
3611 tcpm_set_state(port
, SRC_SEND_CAPABILITIES
, 0);
3616 mutex_unlock(&port
->lock
);
3619 EXPORT_SYMBOL_GPL(tcpm_update_source_capabilities
);
3621 int tcpm_update_sink_capabilities(struct tcpm_port
*port
, const u32
*pdo
,
3622 unsigned int nr_pdo
,
3623 unsigned int max_snk_mv
,
3624 unsigned int max_snk_ma
,
3625 unsigned int max_snk_mw
,
3626 unsigned int operating_snk_mw
)
3628 if (tcpm_validate_caps(port
, pdo
, nr_pdo
))
3631 mutex_lock(&port
->lock
);
3632 port
->nr_snk_pdo
= tcpm_copy_pdos(port
->snk_pdo
, pdo
, nr_pdo
);
3633 port
->max_snk_mv
= max_snk_mv
;
3634 port
->max_snk_ma
= max_snk_ma
;
3635 port
->max_snk_mw
= max_snk_mw
;
3636 port
->operating_snk_mw
= operating_snk_mw
;
3638 switch (port
->state
) {
3639 case SNK_NEGOTIATE_CAPABILITIES
:
3641 case SNK_TRANSITION_SINK
:
3642 case SNK_TRANSITION_SINK_VBUS
:
3643 tcpm_set_state(port
, SNK_NEGOTIATE_CAPABILITIES
, 0);
3648 mutex_unlock(&port
->lock
);
3651 EXPORT_SYMBOL_GPL(tcpm_update_sink_capabilities
);
3653 static int nr_type_pdos(const u32
*pdo
, unsigned int nr_pdo
,
3654 enum pd_pdo_type type
)
3659 for (i
= 0; i
< nr_pdo
; i
++) {
3660 if (pdo_type(pdo
[i
]) == type
)
3666 struct tcpm_port
*tcpm_register_port(struct device
*dev
, struct tcpc_dev
*tcpc
)
3668 struct tcpm_port
*port
;
3671 if (!dev
|| !tcpc
|| !tcpc
->config
||
3672 !tcpc
->get_vbus
|| !tcpc
->set_cc
|| !tcpc
->get_cc
||
3673 !tcpc
->set_polarity
|| !tcpc
->set_vconn
|| !tcpc
->set_vbus
||
3674 !tcpc
->set_pd_rx
|| !tcpc
->set_roles
|| !tcpc
->pd_transmit
)
3675 return ERR_PTR(-EINVAL
);
3677 port
= devm_kzalloc(dev
, sizeof(*port
), GFP_KERNEL
);
3679 return ERR_PTR(-ENOMEM
);
3684 mutex_init(&port
->lock
);
3685 mutex_init(&port
->swap_lock
);
3687 port
->wq
= create_singlethread_workqueue(dev_name(dev
));
3689 return ERR_PTR(-ENOMEM
);
3690 INIT_DELAYED_WORK(&port
->state_machine
, tcpm_state_machine_work
);
3691 INIT_DELAYED_WORK(&port
->vdm_state_machine
, vdm_state_machine_work
);
3692 INIT_WORK(&port
->event_work
, tcpm_pd_event_handler
);
3694 spin_lock_init(&port
->pd_event_lock
);
3696 init_completion(&port
->tx_complete
);
3697 init_completion(&port
->swap_complete
);
3698 tcpm_debugfs_init(port
);
3700 if (tcpm_validate_caps(port
, tcpc
->config
->src_pdo
,
3701 tcpc
->config
->nr_src_pdo
) ||
3702 tcpm_validate_caps(port
, tcpc
->config
->snk_pdo
,
3703 tcpc
->config
->nr_snk_pdo
)) {
3705 goto out_destroy_wq
;
3707 port
->nr_src_pdo
= tcpm_copy_pdos(port
->src_pdo
, tcpc
->config
->src_pdo
,
3708 tcpc
->config
->nr_src_pdo
);
3709 port
->nr_snk_pdo
= tcpm_copy_pdos(port
->snk_pdo
, tcpc
->config
->snk_pdo
,
3710 tcpc
->config
->nr_snk_pdo
);
3711 port
->nr_fixed
= nr_type_pdos(port
->snk_pdo
,
3714 port
->nr_var
= nr_type_pdos(port
->snk_pdo
,
3717 port
->nr_batt
= nr_type_pdos(port
->snk_pdo
,
3720 port
->nr_snk_vdo
= tcpm_copy_vdos(port
->snk_vdo
, tcpc
->config
->snk_vdo
,
3721 tcpc
->config
->nr_snk_vdo
);
3723 port
->max_snk_mv
= tcpc
->config
->max_snk_mv
;
3724 port
->max_snk_ma
= tcpc
->config
->max_snk_ma
;
3725 port
->max_snk_mw
= tcpc
->config
->max_snk_mw
;
3726 port
->operating_snk_mw
= tcpc
->config
->operating_snk_mw
;
3727 if (!tcpc
->config
->try_role_hw
)
3728 port
->try_role
= tcpc
->config
->default_role
;
3730 port
->try_role
= TYPEC_NO_PREFERRED_ROLE
;
3732 port
->typec_caps
.prefer_role
= tcpc
->config
->default_role
;
3733 port
->typec_caps
.type
= tcpc
->config
->type
;
3734 port
->typec_caps
.revision
= 0x0120; /* Type-C spec release 1.2 */
3735 port
->typec_caps
.pd_revision
= 0x0200; /* USB-PD spec release 2.0 */
3736 port
->typec_caps
.dr_set
= tcpm_dr_set
;
3737 port
->typec_caps
.pr_set
= tcpm_pr_set
;
3738 port
->typec_caps
.vconn_set
= tcpm_vconn_set
;
3739 port
->typec_caps
.try_role
= tcpm_try_role
;
3740 port
->typec_caps
.port_type_set
= tcpm_port_type_set
;
3742 port
->partner_desc
.identity
= &port
->partner_ident
;
3743 port
->port_type
= tcpc
->config
->type
;
3745 port
->typec_port
= typec_register_port(port
->dev
, &port
->typec_caps
);
3746 if (!port
->typec_port
) {
3748 goto out_destroy_wq
;
3751 if (tcpc
->config
->alt_modes
) {
3752 const struct typec_altmode_desc
*paltmode
= tcpc
->config
->alt_modes
;
3755 while (paltmode
->svid
&& i
< ARRAY_SIZE(port
->port_altmode
)) {
3756 port
->port_altmode
[i
] =
3757 typec_port_register_altmode(port
->typec_port
,
3759 if (!port
->port_altmode
[i
]) {
3761 "%s: failed to register port alternate mode 0x%x",
3762 dev_name(dev
), paltmode
->svid
);
3770 mutex_lock(&port
->lock
);
3772 mutex_unlock(&port
->lock
);
3774 tcpm_log(port
, "%s: registered", dev_name(dev
));
3778 destroy_workqueue(port
->wq
);
3779 return ERR_PTR(err
);
3781 EXPORT_SYMBOL_GPL(tcpm_register_port
);
3783 void tcpm_unregister_port(struct tcpm_port
*port
)
3787 tcpm_reset_port(port
);
3788 for (i
= 0; i
< ARRAY_SIZE(port
->port_altmode
); i
++)
3789 typec_unregister_altmode(port
->port_altmode
[i
]);
3790 typec_unregister_port(port
->typec_port
);
3791 tcpm_debugfs_exit(port
);
3792 destroy_workqueue(port
->wq
);
3794 EXPORT_SYMBOL_GPL(tcpm_unregister_port
);
3796 MODULE_AUTHOR("Guenter Roeck <groeck@chromium.org>");
3797 MODULE_DESCRIPTION("USB Type-C Port Manager");
3798 MODULE_LICENSE("GPL");