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/power_supply.h>
16 #include <linux/proc_fs.h>
17 #include <linux/property.h>
18 #include <linux/sched/clock.h>
19 #include <linux/seq_file.h>
20 #include <linux/slab.h>
21 #include <linux/spinlock.h>
22 #include <linux/usb/pd.h>
23 #include <linux/usb/pd_ado.h>
24 #include <linux/usb/pd_bdo.h>
25 #include <linux/usb/pd_ext_sdb.h>
26 #include <linux/usb/pd_vdo.h>
27 #include <linux/usb/role.h>
28 #include <linux/usb/tcpm.h>
29 #include <linux/usb/typec_altmode.h>
30 #include <linux/workqueue.h>
32 #define FOREACH_STATE(S) \
39 S(SRC_SEND_CAPABILITIES), \
40 S(SRC_SEND_CAPABILITIES_TIMEOUT), \
41 S(SRC_NEGOTIATE_CAPABILITIES), \
42 S(SRC_TRANSITION_SUPPLY), \
44 S(SRC_WAIT_NEW_CAPABILITIES), \
52 S(SNK_DISCOVERY_DEBOUNCE), \
53 S(SNK_DISCOVERY_DEBOUNCE_DONE), \
54 S(SNK_WAIT_CAPABILITIES), \
55 S(SNK_NEGOTIATE_CAPABILITIES), \
56 S(SNK_NEGOTIATE_PPS_CAPABILITIES), \
57 S(SNK_TRANSITION_SINK), \
58 S(SNK_TRANSITION_SINK_VBUS), \
62 S(DEBUG_ACC_ATTACHED), \
63 S(AUDIO_ACC_ATTACHED), \
64 S(AUDIO_ACC_DEBOUNCE), \
67 S(HARD_RESET_START), \
68 S(SRC_HARD_RESET_VBUS_OFF), \
69 S(SRC_HARD_RESET_VBUS_ON), \
70 S(SNK_HARD_RESET_SINK_OFF), \
71 S(SNK_HARD_RESET_WAIT_VBUS), \
72 S(SNK_HARD_RESET_SINK_ON), \
79 S(DR_SWAP_SEND_TIMEOUT), \
81 S(DR_SWAP_CHANGE_DR), \
85 S(PR_SWAP_SEND_TIMEOUT), \
88 S(PR_SWAP_SRC_SNK_TRANSITION_OFF), \
89 S(PR_SWAP_SRC_SNK_SOURCE_OFF), \
90 S(PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED), \
91 S(PR_SWAP_SRC_SNK_SINK_ON), \
92 S(PR_SWAP_SNK_SRC_SINK_OFF), \
93 S(PR_SWAP_SNK_SRC_SOURCE_ON), \
94 S(PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP), \
96 S(VCONN_SWAP_ACCEPT), \
98 S(VCONN_SWAP_SEND_TIMEOUT), \
99 S(VCONN_SWAP_CANCEL), \
100 S(VCONN_SWAP_START), \
101 S(VCONN_SWAP_WAIT_FOR_VCONN), \
102 S(VCONN_SWAP_TURN_ON_VCONN), \
103 S(VCONN_SWAP_TURN_OFF_VCONN), \
107 S(SNK_TRY_WAIT_DEBOUNCE), \
108 S(SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS), \
110 S(SRC_TRYWAIT_DEBOUNCE), \
111 S(SRC_TRYWAIT_UNATTACHED), \
115 S(SRC_TRY_DEBOUNCE), \
117 S(SNK_TRYWAIT_DEBOUNCE), \
118 S(SNK_TRYWAIT_VBUS), \
121 S(GET_STATUS_SEND), \
122 S(GET_STATUS_SEND_TIMEOUT), \
123 S(GET_PPS_STATUS_SEND), \
124 S(GET_PPS_STATUS_SEND_TIMEOUT), \
128 S(PORT_RESET_WAIT_OFF)
130 #define GENERATE_ENUM(e) e
131 #define GENERATE_STRING(s) #s
134 FOREACH_STATE(GENERATE_ENUM
)
137 static const char * const tcpm_states
[] = {
138 FOREACH_STATE(GENERATE_STRING
)
142 VDM_STATE_ERR_BUSY
= -3,
143 VDM_STATE_ERR_SEND
= -2,
144 VDM_STATE_ERR_TMOUT
= -1,
146 /* Anything >0 represents an active state */
149 VDM_STATE_WAIT_RSP_BUSY
= 3,
152 enum pd_msg_request
{
156 PD_MSG_CTRL_NOT_SUPP
,
157 PD_MSG_DATA_SINK_CAP
,
158 PD_MSG_DATA_SOURCE_CAP
,
161 /* Events from low level driver */
163 #define TCPM_CC_EVENT BIT(0)
164 #define TCPM_VBUS_EVENT BIT(1)
165 #define TCPM_RESET_EVENT BIT(2)
167 #define LOG_BUFFER_ENTRIES 1024
168 #define LOG_BUFFER_ENTRY_SIZE 128
170 /* Alternate mode support */
172 #define SVID_DISCOVERY_MAX 16
173 #define ALTMODE_DISCOVERY_MAX (SVID_DISCOVERY_MAX * MODE_DISCOVERY_MAX)
175 struct pd_mode_data
{
176 int svid_index
; /* current SVID index */
178 u16 svids
[SVID_DISCOVERY_MAX
];
179 int altmodes
; /* number of alternate modes */
180 struct typec_altmode_desc altmode_desc
[ALTMODE_DISCOVERY_MAX
];
196 struct mutex lock
; /* tcpm state machine lock */
197 struct workqueue_struct
*wq
;
199 struct typec_capability typec_caps
;
200 struct typec_port
*typec_port
;
202 struct tcpc_dev
*tcpc
;
203 struct usb_role_switch
*role_sw
;
205 enum typec_role vconn_role
;
206 enum typec_role pwr_role
;
207 enum typec_data_role data_role
;
208 enum typec_pwr_opmode pwr_opmode
;
210 struct usb_pd_identity partner_ident
;
211 struct typec_partner_desc partner_desc
;
212 struct typec_partner
*partner
;
214 enum typec_cc_status cc_req
;
216 enum typec_cc_status cc1
;
217 enum typec_cc_status cc2
;
218 enum typec_cc_polarity polarity
;
222 enum typec_port_type port_type
;
235 enum pd_msg_request queued_message
;
237 enum tcpm_state enter_state
;
238 enum tcpm_state prev_state
;
239 enum tcpm_state state
;
240 enum tcpm_state delayed_state
;
241 unsigned long delayed_runtime
;
242 unsigned long delay_ms
;
244 spinlock_t pd_event_lock
;
247 struct work_struct event_work
;
248 struct delayed_work state_machine
;
249 struct delayed_work vdm_state_machine
;
250 bool state_machine_running
;
252 struct completion tx_complete
;
253 enum tcpm_transmit_status tx_status
;
255 struct mutex swap_lock
; /* swap command lock */
257 bool non_pd_role_swap
;
258 struct completion swap_complete
;
261 unsigned int negotiated_rev
;
262 unsigned int message_id
;
263 unsigned int caps_count
;
264 unsigned int hard_reset_count
;
266 bool explicit_contract
;
267 unsigned int rx_msgid
;
269 /* Partner capabilities/requests */
271 u32 source_caps
[PDO_MAX_OBJECTS
];
272 unsigned int nr_source_caps
;
273 u32 sink_caps
[PDO_MAX_OBJECTS
];
274 unsigned int nr_sink_caps
;
276 /* Local capabilities */
277 u32 src_pdo
[PDO_MAX_OBJECTS
];
278 unsigned int nr_src_pdo
;
279 u32 snk_pdo
[PDO_MAX_OBJECTS
];
280 unsigned int nr_snk_pdo
;
281 u32 snk_vdo
[VDO_MAX_OBJECTS
];
282 unsigned int nr_snk_vdo
;
284 unsigned int operating_snk_mw
;
285 bool update_sink_caps
;
287 /* Requested current / voltage */
291 /* Used to export TA voltage and current */
292 struct power_supply
*psy
;
293 struct power_supply_desc psy_desc
;
294 enum power_supply_usb_type usb_type
;
298 /* PD state for Vendor Defined Messages */
299 enum vdm_states vdm_state
;
301 /* next Vendor Defined Message to send */
302 u32 vdo_data
[VDO_MAX_SIZE
];
304 /* VDO to retry if UFP responder replied busy */
308 struct pd_pps_data pps_data
;
309 struct completion pps_complete
;
313 /* Alternate mode data */
314 struct pd_mode_data mode_data
;
315 struct typec_altmode
*partner_altmode
[ALTMODE_DISCOVERY_MAX
];
316 struct typec_altmode
*port_altmode
[ALTMODE_DISCOVERY_MAX
];
318 /* Deadline in jiffies to exit src_try_wait state */
319 unsigned long max_wait
;
321 /* port belongs to a self powered device */
324 #ifdef CONFIG_DEBUG_FS
325 struct dentry
*dentry
;
326 struct mutex logbuffer_lock
; /* log buffer access lock */
329 u8
*logbuffer
[LOG_BUFFER_ENTRIES
];
334 struct work_struct work
;
335 struct tcpm_port
*port
;
336 struct pd_message msg
;
339 #define tcpm_cc_is_sink(cc) \
340 ((cc) == TYPEC_CC_RP_DEF || (cc) == TYPEC_CC_RP_1_5 || \
341 (cc) == TYPEC_CC_RP_3_0)
343 #define tcpm_port_is_sink(port) \
344 ((tcpm_cc_is_sink((port)->cc1) && !tcpm_cc_is_sink((port)->cc2)) || \
345 (tcpm_cc_is_sink((port)->cc2) && !tcpm_cc_is_sink((port)->cc1)))
347 #define tcpm_cc_is_source(cc) ((cc) == TYPEC_CC_RD)
348 #define tcpm_cc_is_audio(cc) ((cc) == TYPEC_CC_RA)
349 #define tcpm_cc_is_open(cc) ((cc) == TYPEC_CC_OPEN)
351 #define tcpm_port_is_source(port) \
352 ((tcpm_cc_is_source((port)->cc1) && \
353 !tcpm_cc_is_source((port)->cc2)) || \
354 (tcpm_cc_is_source((port)->cc2) && \
355 !tcpm_cc_is_source((port)->cc1)))
357 #define tcpm_port_is_debug(port) \
358 (tcpm_cc_is_source((port)->cc1) && tcpm_cc_is_source((port)->cc2))
360 #define tcpm_port_is_audio(port) \
361 (tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_audio((port)->cc2))
363 #define tcpm_port_is_audio_detached(port) \
364 ((tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_open((port)->cc2)) || \
365 (tcpm_cc_is_audio((port)->cc2) && tcpm_cc_is_open((port)->cc1)))
367 #define tcpm_try_snk(port) \
368 ((port)->try_snk_count == 0 && (port)->try_role == TYPEC_SINK && \
369 (port)->port_type == TYPEC_PORT_DRP)
371 #define tcpm_try_src(port) \
372 ((port)->try_src_count == 0 && (port)->try_role == TYPEC_SOURCE && \
373 (port)->port_type == TYPEC_PORT_DRP)
375 static enum tcpm_state
tcpm_default_state(struct tcpm_port
*port
)
377 if (port
->port_type
== TYPEC_PORT_DRP
) {
378 if (port
->try_role
== TYPEC_SINK
)
379 return SNK_UNATTACHED
;
380 else if (port
->try_role
== TYPEC_SOURCE
)
381 return SRC_UNATTACHED
;
382 else if (port
->tcpc
->config
&&
383 port
->tcpc
->config
->default_role
== TYPEC_SINK
)
384 return SNK_UNATTACHED
;
385 /* Fall through to return SRC_UNATTACHED */
386 } else if (port
->port_type
== TYPEC_PORT_SNK
) {
387 return SNK_UNATTACHED
;
389 return SRC_UNATTACHED
;
393 struct tcpm_port
*typec_cap_to_tcpm(const struct typec_capability
*cap
)
395 return container_of(cap
, struct tcpm_port
, typec_caps
);
398 static bool tcpm_port_is_disconnected(struct tcpm_port
*port
)
400 return (!port
->attached
&& port
->cc1
== TYPEC_CC_OPEN
&&
401 port
->cc2
== TYPEC_CC_OPEN
) ||
402 (port
->attached
&& ((port
->polarity
== TYPEC_POLARITY_CC1
&&
403 port
->cc1
== TYPEC_CC_OPEN
) ||
404 (port
->polarity
== TYPEC_POLARITY_CC2
&&
405 port
->cc2
== TYPEC_CC_OPEN
)));
412 #ifdef CONFIG_DEBUG_FS
414 static bool tcpm_log_full(struct tcpm_port
*port
)
416 return port
->logbuffer_tail
==
417 (port
->logbuffer_head
+ 1) % LOG_BUFFER_ENTRIES
;
421 static void _tcpm_log(struct tcpm_port
*port
, const char *fmt
, va_list args
)
423 char tmpbuffer
[LOG_BUFFER_ENTRY_SIZE
];
424 u64 ts_nsec
= local_clock();
425 unsigned long rem_nsec
;
427 mutex_lock(&port
->logbuffer_lock
);
428 if (!port
->logbuffer
[port
->logbuffer_head
]) {
429 port
->logbuffer
[port
->logbuffer_head
] =
430 kzalloc(LOG_BUFFER_ENTRY_SIZE
, GFP_KERNEL
);
431 if (!port
->logbuffer
[port
->logbuffer_head
]) {
432 mutex_unlock(&port
->logbuffer_lock
);
437 vsnprintf(tmpbuffer
, sizeof(tmpbuffer
), fmt
, args
);
439 if (tcpm_log_full(port
)) {
440 port
->logbuffer_head
= max(port
->logbuffer_head
- 1, 0);
441 strcpy(tmpbuffer
, "overflow");
444 if (port
->logbuffer_head
< 0 ||
445 port
->logbuffer_head
>= LOG_BUFFER_ENTRIES
) {
447 "Bad log buffer index %d\n", port
->logbuffer_head
);
451 if (!port
->logbuffer
[port
->logbuffer_head
]) {
453 "Log buffer index %d is NULL\n", port
->logbuffer_head
);
457 rem_nsec
= do_div(ts_nsec
, 1000000000);
458 scnprintf(port
->logbuffer
[port
->logbuffer_head
],
459 LOG_BUFFER_ENTRY_SIZE
, "[%5lu.%06lu] %s",
460 (unsigned long)ts_nsec
, rem_nsec
/ 1000,
462 port
->logbuffer_head
= (port
->logbuffer_head
+ 1) % LOG_BUFFER_ENTRIES
;
465 mutex_unlock(&port
->logbuffer_lock
);
469 static void tcpm_log(struct tcpm_port
*port
, const char *fmt
, ...)
473 /* Do not log while disconnected and unattached */
474 if (tcpm_port_is_disconnected(port
) &&
475 (port
->state
== SRC_UNATTACHED
|| port
->state
== SNK_UNATTACHED
||
476 port
->state
== TOGGLING
))
480 _tcpm_log(port
, fmt
, args
);
485 static void tcpm_log_force(struct tcpm_port
*port
, const char *fmt
, ...)
490 _tcpm_log(port
, fmt
, args
);
494 static void tcpm_log_source_caps(struct tcpm_port
*port
)
498 for (i
= 0; i
< port
->nr_source_caps
; i
++) {
499 u32 pdo
= port
->source_caps
[i
];
500 enum pd_pdo_type type
= pdo_type(pdo
);
505 scnprintf(msg
, sizeof(msg
),
506 "%u mV, %u mA [%s%s%s%s%s%s]",
507 pdo_fixed_voltage(pdo
),
508 pdo_max_current(pdo
),
509 (pdo
& PDO_FIXED_DUAL_ROLE
) ?
511 (pdo
& PDO_FIXED_SUSPEND
) ?
513 (pdo
& PDO_FIXED_HIGHER_CAP
) ?
515 (pdo
& PDO_FIXED_USB_COMM
) ?
517 (pdo
& PDO_FIXED_DATA_SWAP
) ?
519 (pdo
& PDO_FIXED_EXTPOWER
) ?
523 scnprintf(msg
, sizeof(msg
),
525 pdo_min_voltage(pdo
),
526 pdo_max_voltage(pdo
),
527 pdo_max_current(pdo
));
530 scnprintf(msg
, sizeof(msg
),
532 pdo_min_voltage(pdo
),
533 pdo_max_voltage(pdo
),
537 if (pdo_apdo_type(pdo
) == APDO_TYPE_PPS
)
538 scnprintf(msg
, sizeof(msg
),
540 pdo_pps_apdo_min_voltage(pdo
),
541 pdo_pps_apdo_max_voltage(pdo
),
542 pdo_pps_apdo_max_current(pdo
));
544 strcpy(msg
, "undefined APDO");
547 strcpy(msg
, "undefined");
550 tcpm_log(port
, " PDO %d: type %d, %s",
555 static int tcpm_debug_show(struct seq_file
*s
, void *v
)
557 struct tcpm_port
*port
= (struct tcpm_port
*)s
->private;
560 mutex_lock(&port
->logbuffer_lock
);
561 tail
= port
->logbuffer_tail
;
562 while (tail
!= port
->logbuffer_head
) {
563 seq_printf(s
, "%s\n", port
->logbuffer
[tail
]);
564 tail
= (tail
+ 1) % LOG_BUFFER_ENTRIES
;
566 if (!seq_has_overflowed(s
))
567 port
->logbuffer_tail
= tail
;
568 mutex_unlock(&port
->logbuffer_lock
);
572 DEFINE_SHOW_ATTRIBUTE(tcpm_debug
);
574 static struct dentry
*rootdir
;
576 static void tcpm_debugfs_init(struct tcpm_port
*port
)
578 mutex_init(&port
->logbuffer_lock
);
579 /* /sys/kernel/debug/tcpm/usbcX */
581 rootdir
= debugfs_create_dir("tcpm", NULL
);
583 port
->dentry
= debugfs_create_file(dev_name(port
->dev
),
584 S_IFREG
| 0444, rootdir
,
585 port
, &tcpm_debug_fops
);
588 static void tcpm_debugfs_exit(struct tcpm_port
*port
)
592 mutex_lock(&port
->logbuffer_lock
);
593 for (i
= 0; i
< LOG_BUFFER_ENTRIES
; i
++) {
594 kfree(port
->logbuffer
[i
]);
595 port
->logbuffer
[i
] = NULL
;
597 mutex_unlock(&port
->logbuffer_lock
);
599 debugfs_remove(port
->dentry
);
600 if (list_empty(&rootdir
->d_subdirs
)) {
601 debugfs_remove(rootdir
);
609 static void tcpm_log(const struct tcpm_port
*port
, const char *fmt
, ...) { }
611 static void tcpm_log_force(struct tcpm_port
*port
, const char *fmt
, ...) { }
612 static void tcpm_log_source_caps(struct tcpm_port
*port
) { }
613 static void tcpm_debugfs_init(const struct tcpm_port
*port
) { }
614 static void tcpm_debugfs_exit(const struct tcpm_port
*port
) { }
618 static int tcpm_pd_transmit(struct tcpm_port
*port
,
619 enum tcpm_transmit_type type
,
620 const struct pd_message
*msg
)
622 unsigned long timeout
;
626 tcpm_log(port
, "PD TX, header: %#x", le16_to_cpu(msg
->header
));
628 tcpm_log(port
, "PD TX, type: %#x", type
);
630 reinit_completion(&port
->tx_complete
);
631 ret
= port
->tcpc
->pd_transmit(port
->tcpc
, type
, msg
);
635 mutex_unlock(&port
->lock
);
636 timeout
= wait_for_completion_timeout(&port
->tx_complete
,
637 msecs_to_jiffies(PD_T_TCPC_TX_TIMEOUT
));
638 mutex_lock(&port
->lock
);
642 switch (port
->tx_status
) {
643 case TCPC_TX_SUCCESS
:
644 port
->message_id
= (port
->message_id
+ 1) & PD_HEADER_ID_MASK
;
646 case TCPC_TX_DISCARDED
:
654 void tcpm_pd_transmit_complete(struct tcpm_port
*port
,
655 enum tcpm_transmit_status status
)
657 tcpm_log(port
, "PD TX complete, status: %u", status
);
658 port
->tx_status
= status
;
659 complete(&port
->tx_complete
);
661 EXPORT_SYMBOL_GPL(tcpm_pd_transmit_complete
);
663 static int tcpm_mux_set(struct tcpm_port
*port
, int state
,
664 enum usb_role usb_role
,
665 enum typec_orientation orientation
)
669 tcpm_log(port
, "Requesting mux state %d, usb-role %d, orientation %d",
670 state
, usb_role
, orientation
);
672 ret
= typec_set_orientation(port
->typec_port
, orientation
);
677 ret
= usb_role_switch_set_role(port
->role_sw
, usb_role
);
682 return typec_set_mode(port
->typec_port
, state
);
685 static int tcpm_set_polarity(struct tcpm_port
*port
,
686 enum typec_cc_polarity polarity
)
690 tcpm_log(port
, "polarity %d", polarity
);
692 ret
= port
->tcpc
->set_polarity(port
->tcpc
, polarity
);
696 port
->polarity
= polarity
;
701 static int tcpm_set_vconn(struct tcpm_port
*port
, bool enable
)
705 tcpm_log(port
, "vconn:=%d", enable
);
707 ret
= port
->tcpc
->set_vconn(port
->tcpc
, enable
);
709 port
->vconn_role
= enable
? TYPEC_SOURCE
: TYPEC_SINK
;
710 typec_set_vconn_role(port
->typec_port
, port
->vconn_role
);
716 static u32
tcpm_get_current_limit(struct tcpm_port
*port
)
718 enum typec_cc_status cc
;
721 cc
= port
->polarity
? port
->cc2
: port
->cc1
;
723 case TYPEC_CC_RP_1_5
:
726 case TYPEC_CC_RP_3_0
:
729 case TYPEC_CC_RP_DEF
:
731 if (port
->tcpc
->get_current_limit
)
732 limit
= port
->tcpc
->get_current_limit(port
->tcpc
);
741 static int tcpm_set_current_limit(struct tcpm_port
*port
, u32 max_ma
, u32 mv
)
743 int ret
= -EOPNOTSUPP
;
745 tcpm_log(port
, "Setting voltage/current limit %u mV %u mA", mv
, max_ma
);
747 port
->supply_voltage
= mv
;
748 port
->current_limit
= max_ma
;
750 if (port
->tcpc
->set_current_limit
)
751 ret
= port
->tcpc
->set_current_limit(port
->tcpc
, max_ma
, mv
);
757 * Determine RP value to set based on maximum current supported
758 * by a port if configured as source.
759 * Returns CC value to report to link partner.
761 static enum typec_cc_status
tcpm_rp_cc(struct tcpm_port
*port
)
763 const u32
*src_pdo
= port
->src_pdo
;
764 int nr_pdo
= port
->nr_src_pdo
;
768 * Search for first entry with matching voltage.
769 * It should report the maximum supported current.
771 for (i
= 0; i
< nr_pdo
; i
++) {
772 const u32 pdo
= src_pdo
[i
];
774 if (pdo_type(pdo
) == PDO_TYPE_FIXED
&&
775 pdo_fixed_voltage(pdo
) == 5000) {
776 unsigned int curr
= pdo_max_current(pdo
);
779 return TYPEC_CC_RP_3_0
;
780 else if (curr
>= 1500)
781 return TYPEC_CC_RP_1_5
;
782 return TYPEC_CC_RP_DEF
;
786 return TYPEC_CC_RP_DEF
;
789 static int tcpm_set_attached_state(struct tcpm_port
*port
, bool attached
)
791 return port
->tcpc
->set_roles(port
->tcpc
, attached
, port
->pwr_role
,
795 static int tcpm_set_roles(struct tcpm_port
*port
, bool attached
,
796 enum typec_role role
, enum typec_data_role data
)
798 enum typec_orientation orientation
;
799 enum usb_role usb_role
;
802 if (port
->polarity
== TYPEC_POLARITY_CC1
)
803 orientation
= TYPEC_ORIENTATION_NORMAL
;
805 orientation
= TYPEC_ORIENTATION_REVERSE
;
807 if (data
== TYPEC_HOST
)
808 usb_role
= USB_ROLE_HOST
;
810 usb_role
= USB_ROLE_DEVICE
;
812 ret
= tcpm_mux_set(port
, TYPEC_STATE_USB
, usb_role
, orientation
);
816 ret
= port
->tcpc
->set_roles(port
->tcpc
, attached
, role
, data
);
820 port
->pwr_role
= role
;
821 port
->data_role
= data
;
822 typec_set_data_role(port
->typec_port
, data
);
823 typec_set_pwr_role(port
->typec_port
, role
);
828 static int tcpm_set_pwr_role(struct tcpm_port
*port
, enum typec_role role
)
832 ret
= port
->tcpc
->set_roles(port
->tcpc
, true, role
,
837 port
->pwr_role
= role
;
838 typec_set_pwr_role(port
->typec_port
, role
);
843 static int tcpm_pd_send_source_caps(struct tcpm_port
*port
)
845 struct pd_message msg
;
848 memset(&msg
, 0, sizeof(msg
));
849 if (!port
->nr_src_pdo
) {
850 /* No source capabilities defined, sink only */
851 msg
.header
= PD_HEADER_LE(PD_CTRL_REJECT
,
854 port
->negotiated_rev
,
855 port
->message_id
, 0);
857 msg
.header
= PD_HEADER_LE(PD_DATA_SOURCE_CAP
,
860 port
->negotiated_rev
,
864 for (i
= 0; i
< port
->nr_src_pdo
; i
++)
865 msg
.payload
[i
] = cpu_to_le32(port
->src_pdo
[i
]);
867 return tcpm_pd_transmit(port
, TCPC_TX_SOP
, &msg
);
870 static int tcpm_pd_send_sink_caps(struct tcpm_port
*port
)
872 struct pd_message msg
;
875 memset(&msg
, 0, sizeof(msg
));
876 if (!port
->nr_snk_pdo
) {
877 /* No sink capabilities defined, source only */
878 msg
.header
= PD_HEADER_LE(PD_CTRL_REJECT
,
881 port
->negotiated_rev
,
882 port
->message_id
, 0);
884 msg
.header
= PD_HEADER_LE(PD_DATA_SINK_CAP
,
887 port
->negotiated_rev
,
891 for (i
= 0; i
< port
->nr_snk_pdo
; i
++)
892 msg
.payload
[i
] = cpu_to_le32(port
->snk_pdo
[i
]);
894 return tcpm_pd_transmit(port
, TCPC_TX_SOP
, &msg
);
897 static void tcpm_set_state(struct tcpm_port
*port
, enum tcpm_state state
,
898 unsigned int delay_ms
)
901 tcpm_log(port
, "pending state change %s -> %s @ %u ms",
902 tcpm_states
[port
->state
], tcpm_states
[state
],
904 port
->delayed_state
= state
;
905 mod_delayed_work(port
->wq
, &port
->state_machine
,
906 msecs_to_jiffies(delay_ms
));
907 port
->delayed_runtime
= jiffies
+ msecs_to_jiffies(delay_ms
);
908 port
->delay_ms
= delay_ms
;
910 tcpm_log(port
, "state change %s -> %s",
911 tcpm_states
[port
->state
], tcpm_states
[state
]);
912 port
->delayed_state
= INVALID_STATE
;
913 port
->prev_state
= port
->state
;
916 * Don't re-queue the state machine work item if we're currently
917 * in the state machine and we're immediately changing states.
918 * tcpm_state_machine_work() will continue running the state
921 if (!port
->state_machine_running
)
922 mod_delayed_work(port
->wq
, &port
->state_machine
, 0);
926 static void tcpm_set_state_cond(struct tcpm_port
*port
, enum tcpm_state state
,
927 unsigned int delay_ms
)
929 if (port
->enter_state
== port
->state
)
930 tcpm_set_state(port
, state
, delay_ms
);
933 "skipped %sstate change %s -> %s [%u ms], context state %s",
934 delay_ms
? "delayed " : "",
935 tcpm_states
[port
->state
], tcpm_states
[state
],
936 delay_ms
, tcpm_states
[port
->enter_state
]);
939 static void tcpm_queue_message(struct tcpm_port
*port
,
940 enum pd_msg_request message
)
942 port
->queued_message
= message
;
943 mod_delayed_work(port
->wq
, &port
->state_machine
, 0);
947 * VDM/VDO handling functions
949 static void tcpm_queue_vdm(struct tcpm_port
*port
, const u32 header
,
950 const u32
*data
, int cnt
)
952 port
->vdo_count
= cnt
+ 1;
953 port
->vdo_data
[0] = header
;
954 memcpy(&port
->vdo_data
[1], data
, sizeof(u32
) * cnt
);
955 /* Set ready, vdm state machine will actually send */
956 port
->vdm_retries
= 0;
957 port
->vdm_state
= VDM_STATE_READY
;
960 static void svdm_consume_identity(struct tcpm_port
*port
, const __le32
*payload
,
963 u32 vdo
= le32_to_cpu(payload
[VDO_INDEX_IDH
]);
964 u32 product
= le32_to_cpu(payload
[VDO_INDEX_PRODUCT
]);
966 memset(&port
->mode_data
, 0, sizeof(port
->mode_data
));
968 port
->partner_ident
.id_header
= vdo
;
969 port
->partner_ident
.cert_stat
= le32_to_cpu(payload
[VDO_INDEX_CSTAT
]);
970 port
->partner_ident
.product
= product
;
972 typec_partner_set_identity(port
->partner
);
974 tcpm_log(port
, "Identity: %04x:%04x.%04x",
976 PD_PRODUCT_PID(product
), product
& 0xffff);
979 static bool svdm_consume_svids(struct tcpm_port
*port
, const __le32
*payload
,
982 struct pd_mode_data
*pmdata
= &port
->mode_data
;
985 for (i
= 1; i
< cnt
; i
++) {
986 u32 p
= le32_to_cpu(payload
[i
]);
989 svid
= (p
>> 16) & 0xffff;
993 if (pmdata
->nsvids
>= SVID_DISCOVERY_MAX
)
996 pmdata
->svids
[pmdata
->nsvids
++] = svid
;
997 tcpm_log(port
, "SVID %d: 0x%x", pmdata
->nsvids
, svid
);
1003 if (pmdata
->nsvids
>= SVID_DISCOVERY_MAX
)
1006 pmdata
->svids
[pmdata
->nsvids
++] = svid
;
1007 tcpm_log(port
, "SVID %d: 0x%x", pmdata
->nsvids
, svid
);
1011 tcpm_log(port
, "SVID_DISCOVERY_MAX(%d) too low!", SVID_DISCOVERY_MAX
);
1015 static void svdm_consume_modes(struct tcpm_port
*port
, const __le32
*payload
,
1018 struct pd_mode_data
*pmdata
= &port
->mode_data
;
1019 struct typec_altmode_desc
*paltmode
;
1022 if (pmdata
->altmodes
>= ARRAY_SIZE(port
->partner_altmode
)) {
1023 /* Already logged in svdm_consume_svids() */
1027 for (i
= 1; i
< cnt
; i
++) {
1028 paltmode
= &pmdata
->altmode_desc
[pmdata
->altmodes
];
1029 memset(paltmode
, 0, sizeof(*paltmode
));
1031 paltmode
->svid
= pmdata
->svids
[pmdata
->svid_index
];
1033 paltmode
->vdo
= le32_to_cpu(payload
[i
]);
1035 tcpm_log(port
, " Alternate mode %d: SVID 0x%04x, VDO %d: 0x%08x",
1036 pmdata
->altmodes
, paltmode
->svid
,
1037 paltmode
->mode
, paltmode
->vdo
);
1043 static void tcpm_register_partner_altmodes(struct tcpm_port
*port
)
1045 struct pd_mode_data
*modep
= &port
->mode_data
;
1046 struct typec_altmode
*altmode
;
1049 for (i
= 0; i
< modep
->altmodes
; i
++) {
1050 altmode
= typec_partner_register_altmode(port
->partner
,
1051 &modep
->altmode_desc
[i
]);
1053 tcpm_log(port
, "Failed to register partner SVID 0x%04x",
1054 modep
->altmode_desc
[i
].svid
);
1055 port
->partner_altmode
[i
] = altmode
;
1059 #define supports_modal(port) PD_IDH_MODAL_SUPP((port)->partner_ident.id_header)
1061 static int tcpm_pd_svdm(struct tcpm_port
*port
, const __le32
*payload
, int cnt
,
1064 struct typec_altmode
*adev
;
1065 struct typec_altmode
*pdev
;
1066 struct pd_mode_data
*modep
;
1067 u32 p
[PD_MAX_PAYLOAD
];
1073 for (i
= 0; i
< cnt
; i
++)
1074 p
[i
] = le32_to_cpu(payload
[i
]);
1076 cmd_type
= PD_VDO_CMDT(p
[0]);
1077 cmd
= PD_VDO_CMD(p
[0]);
1079 tcpm_log(port
, "Rx VDM cmd 0x%x type %d cmd %d len %d",
1080 p
[0], cmd_type
, cmd
, cnt
);
1082 modep
= &port
->mode_data
;
1084 adev
= typec_match_altmode(port
->port_altmode
, ALTMODE_DISCOVERY_MAX
,
1085 PD_VDO_VID(p
[0]), PD_VDO_OPOS(p
[0]));
1087 pdev
= typec_match_altmode(port
->partner_altmode
, ALTMODE_DISCOVERY_MAX
,
1088 PD_VDO_VID(p
[0]), PD_VDO_OPOS(p
[0]));
1093 case CMD_DISCOVER_IDENT
:
1094 /* 6.4.4.3.1: Only respond as UFP (device) */
1095 if (port
->data_role
== TYPEC_DEVICE
&&
1097 for (i
= 0; i
< port
->nr_snk_vdo
; i
++)
1098 response
[i
+ 1] = port
->snk_vdo
[i
];
1099 rlen
= port
->nr_snk_vdo
+ 1;
1102 case CMD_DISCOVER_SVID
:
1104 case CMD_DISCOVER_MODES
:
1106 case CMD_ENTER_MODE
:
1111 /* Attention command does not have response */
1113 typec_altmode_attention(adev
, p
[1]);
1119 response
[0] = p
[0] | VDO_CMDT(CMDT_RSP_ACK
);
1120 } else if (rlen
== 0) {
1121 response
[0] = p
[0] | VDO_CMDT(CMDT_RSP_NAK
);
1124 response
[0] = p
[0] | VDO_CMDT(CMDT_RSP_BUSY
);
1129 /* silently drop message if we are not connected */
1130 if (IS_ERR_OR_NULL(port
->partner
))
1134 case CMD_DISCOVER_IDENT
:
1136 svdm_consume_identity(port
, payload
, cnt
);
1137 response
[0] = VDO(USB_SID_PD
, 1, CMD_DISCOVER_SVID
);
1140 case CMD_DISCOVER_SVID
:
1142 if (svdm_consume_svids(port
, payload
, cnt
)) {
1143 response
[0] = VDO(USB_SID_PD
, 1,
1146 } else if (modep
->nsvids
&& supports_modal(port
)) {
1147 response
[0] = VDO(modep
->svids
[0], 1,
1148 CMD_DISCOVER_MODES
);
1152 case CMD_DISCOVER_MODES
:
1154 svdm_consume_modes(port
, payload
, cnt
);
1155 modep
->svid_index
++;
1156 if (modep
->svid_index
< modep
->nsvids
) {
1157 u16 svid
= modep
->svids
[modep
->svid_index
];
1158 response
[0] = VDO(svid
, 1, CMD_DISCOVER_MODES
);
1161 tcpm_register_partner_altmodes(port
);
1164 case CMD_ENTER_MODE
:
1166 typec_altmode_update_active(pdev
, true);
1168 if (typec_altmode_vdm(adev
, p
[0], &p
[1], cnt
)) {
1169 response
[0] = VDO(adev
->svid
, 1,
1171 response
[0] |= VDO_OPOS(adev
->mode
);
1178 typec_altmode_update_active(pdev
, false);
1180 /* Back to USB Operation */
1181 WARN_ON(typec_altmode_notify(adev
,
1192 case CMD_ENTER_MODE
:
1193 /* Back to USB Operation */
1195 WARN_ON(typec_altmode_notify(adev
,
1207 /* Informing the alternate mode drivers about everything */
1209 typec_altmode_vdm(adev
, p
[0], &p
[1], cnt
);
1214 static void tcpm_handle_vdm_request(struct tcpm_port
*port
,
1215 const __le32
*payload
, int cnt
)
1218 u32 response
[8] = { };
1219 u32 p0
= le32_to_cpu(payload
[0]);
1221 if (port
->vdm_state
== VDM_STATE_BUSY
) {
1222 /* If UFP responded busy retry after timeout */
1223 if (PD_VDO_CMDT(p0
) == CMDT_RSP_BUSY
) {
1224 port
->vdm_state
= VDM_STATE_WAIT_RSP_BUSY
;
1225 port
->vdo_retry
= (p0
& ~VDO_CMDT_MASK
) |
1227 mod_delayed_work(port
->wq
, &port
->vdm_state_machine
,
1228 msecs_to_jiffies(PD_T_VDM_BUSY
));
1231 port
->vdm_state
= VDM_STATE_DONE
;
1234 if (PD_VDO_SVDM(p0
))
1235 rlen
= tcpm_pd_svdm(port
, payload
, cnt
, response
);
1238 tcpm_queue_vdm(port
, response
[0], &response
[1], rlen
- 1);
1239 mod_delayed_work(port
->wq
, &port
->vdm_state_machine
, 0);
1243 static void tcpm_send_vdm(struct tcpm_port
*port
, u32 vid
, int cmd
,
1244 const u32
*data
, int count
)
1248 if (WARN_ON(count
> VDO_MAX_SIZE
- 1))
1249 count
= VDO_MAX_SIZE
- 1;
1251 /* set VDM header with VID & CMD */
1252 header
= VDO(vid
, ((vid
& USB_SID_PD
) == USB_SID_PD
) ?
1253 1 : (PD_VDO_CMD(cmd
) <= CMD_ATTENTION
), cmd
);
1254 tcpm_queue_vdm(port
, header
, data
, count
);
1256 mod_delayed_work(port
->wq
, &port
->vdm_state_machine
, 0);
1259 static unsigned int vdm_ready_timeout(u32 vdm_hdr
)
1261 unsigned int timeout
;
1262 int cmd
= PD_VDO_CMD(vdm_hdr
);
1264 /* its not a structured VDM command */
1265 if (!PD_VDO_SVDM(vdm_hdr
))
1266 return PD_T_VDM_UNSTRUCTURED
;
1268 switch (PD_VDO_CMDT(vdm_hdr
)) {
1270 if (cmd
== CMD_ENTER_MODE
|| cmd
== CMD_EXIT_MODE
)
1271 timeout
= PD_T_VDM_WAIT_MODE_E
;
1273 timeout
= PD_T_VDM_SNDR_RSP
;
1276 if (cmd
== CMD_ENTER_MODE
|| cmd
== CMD_EXIT_MODE
)
1277 timeout
= PD_T_VDM_E_MODE
;
1279 timeout
= PD_T_VDM_RCVR_RSP
;
1285 static void vdm_run_state_machine(struct tcpm_port
*port
)
1287 struct pd_message msg
;
1290 switch (port
->vdm_state
) {
1291 case VDM_STATE_READY
:
1292 /* Only transmit VDM if attached */
1293 if (!port
->attached
) {
1294 port
->vdm_state
= VDM_STATE_ERR_BUSY
;
1299 * if there's traffic or we're not in PDO ready state don't send
1302 if (port
->state
!= SRC_READY
&& port
->state
!= SNK_READY
)
1305 /* Prepare and send VDM */
1306 memset(&msg
, 0, sizeof(msg
));
1307 msg
.header
= PD_HEADER_LE(PD_DATA_VENDOR_DEF
,
1310 port
->negotiated_rev
,
1311 port
->message_id
, port
->vdo_count
);
1312 for (i
= 0; i
< port
->vdo_count
; i
++)
1313 msg
.payload
[i
] = cpu_to_le32(port
->vdo_data
[i
]);
1314 res
= tcpm_pd_transmit(port
, TCPC_TX_SOP
, &msg
);
1316 port
->vdm_state
= VDM_STATE_ERR_SEND
;
1318 unsigned long timeout
;
1320 port
->vdm_retries
= 0;
1321 port
->vdm_state
= VDM_STATE_BUSY
;
1322 timeout
= vdm_ready_timeout(port
->vdo_data
[0]);
1323 mod_delayed_work(port
->wq
, &port
->vdm_state_machine
,
1327 case VDM_STATE_WAIT_RSP_BUSY
:
1328 port
->vdo_data
[0] = port
->vdo_retry
;
1329 port
->vdo_count
= 1;
1330 port
->vdm_state
= VDM_STATE_READY
;
1332 case VDM_STATE_BUSY
:
1333 port
->vdm_state
= VDM_STATE_ERR_TMOUT
;
1335 case VDM_STATE_ERR_SEND
:
1337 * A partner which does not support USB PD will not reply,
1338 * so this is not a fatal error. At the same time, some
1339 * devices may not return GoodCRC under some circumstances,
1340 * so we need to retry.
1342 if (port
->vdm_retries
< 3) {
1343 tcpm_log(port
, "VDM Tx error, retry");
1344 port
->vdm_retries
++;
1345 port
->vdm_state
= VDM_STATE_READY
;
1353 static void vdm_state_machine_work(struct work_struct
*work
)
1355 struct tcpm_port
*port
= container_of(work
, struct tcpm_port
,
1356 vdm_state_machine
.work
);
1357 enum vdm_states prev_state
;
1359 mutex_lock(&port
->lock
);
1362 * Continue running as long as the port is not busy and there was
1366 prev_state
= port
->vdm_state
;
1367 vdm_run_state_machine(port
);
1368 } while (port
->vdm_state
!= prev_state
&&
1369 port
->vdm_state
!= VDM_STATE_BUSY
);
1371 mutex_unlock(&port
->lock
);
1377 PDO_ERR_VSAFE5V_NOT_FIRST
,
1378 PDO_ERR_PDO_TYPE_NOT_IN_ORDER
,
1379 PDO_ERR_FIXED_NOT_SORTED
,
1380 PDO_ERR_VARIABLE_BATT_NOT_SORTED
,
1382 PDO_ERR_PPS_APDO_NOT_SORTED
,
1383 PDO_ERR_DUPE_PPS_APDO
,
1386 static const char * const pdo_err_msg
[] = {
1387 [PDO_ERR_NO_VSAFE5V
] =
1388 " err: source/sink caps should atleast have vSafe5V",
1389 [PDO_ERR_VSAFE5V_NOT_FIRST
] =
1390 " err: vSafe5V Fixed Supply Object Shall always be the first object",
1391 [PDO_ERR_PDO_TYPE_NOT_IN_ORDER
] =
1392 " err: PDOs should be in the following order: Fixed; Battery; Variable",
1393 [PDO_ERR_FIXED_NOT_SORTED
] =
1394 " err: Fixed supply pdos should be in increasing order of their fixed voltage",
1395 [PDO_ERR_VARIABLE_BATT_NOT_SORTED
] =
1396 " err: Variable/Battery supply pdos should be in increasing order of their minimum voltage",
1397 [PDO_ERR_DUPE_PDO
] =
1398 " err: Variable/Batt supply pdos cannot have same min/max voltage",
1399 [PDO_ERR_PPS_APDO_NOT_SORTED
] =
1400 " err: Programmable power supply apdos should be in increasing order of their maximum voltage",
1401 [PDO_ERR_DUPE_PPS_APDO
] =
1402 " err: Programmable power supply apdos cannot have same min/max voltage and max current",
1405 static enum pdo_err
tcpm_caps_err(struct tcpm_port
*port
, const u32
*pdo
,
1406 unsigned int nr_pdo
)
1410 /* Should at least contain vSafe5v */
1412 return PDO_ERR_NO_VSAFE5V
;
1414 /* The vSafe5V Fixed Supply Object Shall always be the first object */
1415 if (pdo_type(pdo
[0]) != PDO_TYPE_FIXED
||
1416 pdo_fixed_voltage(pdo
[0]) != VSAFE5V
)
1417 return PDO_ERR_VSAFE5V_NOT_FIRST
;
1419 for (i
= 1; i
< nr_pdo
; i
++) {
1420 if (pdo_type(pdo
[i
]) < pdo_type(pdo
[i
- 1])) {
1421 return PDO_ERR_PDO_TYPE_NOT_IN_ORDER
;
1422 } else if (pdo_type(pdo
[i
]) == pdo_type(pdo
[i
- 1])) {
1423 enum pd_pdo_type type
= pdo_type(pdo
[i
]);
1427 * The remaining Fixed Supply Objects, if
1428 * present, shall be sent in voltage order;
1429 * lowest to highest.
1431 case PDO_TYPE_FIXED
:
1432 if (pdo_fixed_voltage(pdo
[i
]) <=
1433 pdo_fixed_voltage(pdo
[i
- 1]))
1434 return PDO_ERR_FIXED_NOT_SORTED
;
1437 * The Battery Supply Objects and Variable
1438 * supply, if present shall be sent in Minimum
1439 * Voltage order; lowest to highest.
1443 if (pdo_min_voltage(pdo
[i
]) <
1444 pdo_min_voltage(pdo
[i
- 1]))
1445 return PDO_ERR_VARIABLE_BATT_NOT_SORTED
;
1446 else if ((pdo_min_voltage(pdo
[i
]) ==
1447 pdo_min_voltage(pdo
[i
- 1])) &&
1448 (pdo_max_voltage(pdo
[i
]) ==
1449 pdo_max_voltage(pdo
[i
- 1])))
1450 return PDO_ERR_DUPE_PDO
;
1453 * The Programmable Power Supply APDOs, if present,
1454 * shall be sent in Maximum Voltage order;
1455 * lowest to highest.
1458 if (pdo_apdo_type(pdo
[i
]) != APDO_TYPE_PPS
)
1461 if (pdo_pps_apdo_max_voltage(pdo
[i
]) <
1462 pdo_pps_apdo_max_voltage(pdo
[i
- 1]))
1463 return PDO_ERR_PPS_APDO_NOT_SORTED
;
1464 else if (pdo_pps_apdo_min_voltage(pdo
[i
]) ==
1465 pdo_pps_apdo_min_voltage(pdo
[i
- 1]) &&
1466 pdo_pps_apdo_max_voltage(pdo
[i
]) ==
1467 pdo_pps_apdo_max_voltage(pdo
[i
- 1]) &&
1468 pdo_pps_apdo_max_current(pdo
[i
]) ==
1469 pdo_pps_apdo_max_current(pdo
[i
- 1]))
1470 return PDO_ERR_DUPE_PPS_APDO
;
1473 tcpm_log_force(port
, " Unknown pdo type");
1481 static int tcpm_validate_caps(struct tcpm_port
*port
, const u32
*pdo
,
1482 unsigned int nr_pdo
)
1484 enum pdo_err err_index
= tcpm_caps_err(port
, pdo
, nr_pdo
);
1486 if (err_index
!= PDO_NO_ERR
) {
1487 tcpm_log_force(port
, " %s", pdo_err_msg
[err_index
]);
1494 static int tcpm_altmode_enter(struct typec_altmode
*altmode
)
1496 struct tcpm_port
*port
= typec_altmode_get_drvdata(altmode
);
1499 mutex_lock(&port
->lock
);
1500 header
= VDO(altmode
->svid
, 1, CMD_ENTER_MODE
);
1501 header
|= VDO_OPOS(altmode
->mode
);
1503 tcpm_queue_vdm(port
, header
, NULL
, 0);
1504 mod_delayed_work(port
->wq
, &port
->vdm_state_machine
, 0);
1505 mutex_unlock(&port
->lock
);
1510 static int tcpm_altmode_exit(struct typec_altmode
*altmode
)
1512 struct tcpm_port
*port
= typec_altmode_get_drvdata(altmode
);
1515 mutex_lock(&port
->lock
);
1516 header
= VDO(altmode
->svid
, 1, CMD_EXIT_MODE
);
1517 header
|= VDO_OPOS(altmode
->mode
);
1519 tcpm_queue_vdm(port
, header
, NULL
, 0);
1520 mod_delayed_work(port
->wq
, &port
->vdm_state_machine
, 0);
1521 mutex_unlock(&port
->lock
);
1526 static int tcpm_altmode_vdm(struct typec_altmode
*altmode
,
1527 u32 header
, const u32
*data
, int count
)
1529 struct tcpm_port
*port
= typec_altmode_get_drvdata(altmode
);
1531 mutex_lock(&port
->lock
);
1532 tcpm_queue_vdm(port
, header
, data
, count
- 1);
1533 mod_delayed_work(port
->wq
, &port
->vdm_state_machine
, 0);
1534 mutex_unlock(&port
->lock
);
1539 static const struct typec_altmode_ops tcpm_altmode_ops
= {
1540 .enter
= tcpm_altmode_enter
,
1541 .exit
= tcpm_altmode_exit
,
1542 .vdm
= tcpm_altmode_vdm
,
1546 * PD (data, control) command handling functions
1548 static inline enum tcpm_state
ready_state(struct tcpm_port
*port
)
1550 if (port
->pwr_role
== TYPEC_SOURCE
)
1556 static int tcpm_pd_send_control(struct tcpm_port
*port
,
1557 enum pd_ctrl_msg_type type
);
1559 static void tcpm_handle_alert(struct tcpm_port
*port
, const __le32
*payload
,
1562 u32 p0
= le32_to_cpu(payload
[0]);
1563 unsigned int type
= usb_pd_ado_type(p0
);
1566 tcpm_log(port
, "Alert message received with no type");
1570 /* Just handling non-battery alerts for now */
1571 if (!(type
& USB_PD_ADO_TYPE_BATT_STATUS_CHANGE
)) {
1572 switch (port
->state
) {
1575 tcpm_set_state(port
, GET_STATUS_SEND
, 0);
1578 tcpm_queue_message(port
, PD_MSG_CTRL_WAIT
);
1584 static void tcpm_pd_data_request(struct tcpm_port
*port
,
1585 const struct pd_message
*msg
)
1587 enum pd_data_msg_type type
= pd_header_type_le(msg
->header
);
1588 unsigned int cnt
= pd_header_cnt_le(msg
->header
);
1589 unsigned int rev
= pd_header_rev_le(msg
->header
);
1593 case PD_DATA_SOURCE_CAP
:
1594 if (port
->pwr_role
!= TYPEC_SINK
)
1597 for (i
= 0; i
< cnt
; i
++)
1598 port
->source_caps
[i
] = le32_to_cpu(msg
->payload
[i
]);
1600 port
->nr_source_caps
= cnt
;
1602 tcpm_log_source_caps(port
);
1604 tcpm_validate_caps(port
, port
->source_caps
,
1605 port
->nr_source_caps
);
1608 * Adjust revision in subsequent message headers, as required,
1609 * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't
1610 * support Rev 1.0 so just do nothing in that scenario.
1612 if (rev
== PD_REV10
)
1615 if (rev
< PD_MAX_REV
)
1616 port
->negotiated_rev
= rev
;
1619 * This message may be received even if VBUS is not
1620 * present. This is quite unexpected; see USB PD
1621 * specification, sections 8.3.3.6.3.1 and 8.3.3.6.3.2.
1622 * However, at the same time, we must be ready to
1623 * receive this message and respond to it 15ms after
1624 * receiving PS_RDY during power swap operations, no matter
1625 * if VBUS is available or not (USB PD specification,
1627 * So we need to accept the message either way,
1628 * but be prepared to keep waiting for VBUS after it was
1631 tcpm_set_state(port
, SNK_NEGOTIATE_CAPABILITIES
, 0);
1633 case PD_DATA_REQUEST
:
1634 if (port
->pwr_role
!= TYPEC_SOURCE
||
1636 tcpm_queue_message(port
, PD_MSG_CTRL_REJECT
);
1641 * Adjust revision in subsequent message headers, as required,
1642 * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't
1643 * support Rev 1.0 so just reject in that scenario.
1645 if (rev
== PD_REV10
) {
1646 tcpm_queue_message(port
, PD_MSG_CTRL_REJECT
);
1650 if (rev
< PD_MAX_REV
)
1651 port
->negotiated_rev
= rev
;
1653 port
->sink_request
= le32_to_cpu(msg
->payload
[0]);
1654 tcpm_set_state(port
, SRC_NEGOTIATE_CAPABILITIES
, 0);
1656 case PD_DATA_SINK_CAP
:
1657 /* We don't do anything with this at the moment... */
1658 for (i
= 0; i
< cnt
; i
++)
1659 port
->sink_caps
[i
] = le32_to_cpu(msg
->payload
[i
]);
1660 port
->nr_sink_caps
= cnt
;
1662 case PD_DATA_VENDOR_DEF
:
1663 tcpm_handle_vdm_request(port
, msg
->payload
, cnt
);
1666 if (port
->state
== SRC_READY
|| port
->state
== SNK_READY
) {
1667 port
->bist_request
= le32_to_cpu(msg
->payload
[0]);
1668 tcpm_set_state(port
, BIST_RX
, 0);
1672 tcpm_handle_alert(port
, msg
->payload
, cnt
);
1674 case PD_DATA_BATT_STATUS
:
1675 case PD_DATA_GET_COUNTRY_INFO
:
1676 /* Currently unsupported */
1677 tcpm_queue_message(port
, PD_MSG_CTRL_NOT_SUPP
);
1680 tcpm_log(port
, "Unhandled data message type %#x", type
);
1685 static void tcpm_pps_complete(struct tcpm_port
*port
, int result
)
1687 if (port
->pps_pending
) {
1688 port
->pps_status
= result
;
1689 port
->pps_pending
= false;
1690 complete(&port
->pps_complete
);
1694 static void tcpm_pd_ctrl_request(struct tcpm_port
*port
,
1695 const struct pd_message
*msg
)
1697 enum pd_ctrl_msg_type type
= pd_header_type_le(msg
->header
);
1698 enum tcpm_state next_state
;
1701 case PD_CTRL_GOOD_CRC
:
1704 case PD_CTRL_GET_SOURCE_CAP
:
1705 switch (port
->state
) {
1708 tcpm_queue_message(port
, PD_MSG_DATA_SOURCE_CAP
);
1711 tcpm_queue_message(port
, PD_MSG_CTRL_REJECT
);
1715 case PD_CTRL_GET_SINK_CAP
:
1716 switch (port
->state
) {
1719 tcpm_queue_message(port
, PD_MSG_DATA_SINK_CAP
);
1722 tcpm_queue_message(port
, PD_MSG_CTRL_REJECT
);
1726 case PD_CTRL_GOTO_MIN
:
1728 case PD_CTRL_PS_RDY
:
1729 switch (port
->state
) {
1730 case SNK_TRANSITION_SINK
:
1731 if (port
->vbus_present
) {
1732 tcpm_set_current_limit(port
,
1733 port
->current_limit
,
1734 port
->supply_voltage
);
1735 port
->explicit_contract
= true;
1736 tcpm_set_state(port
, SNK_READY
, 0);
1739 * Seen after power swap. Keep waiting for VBUS
1740 * in a transitional state.
1742 tcpm_set_state(port
,
1743 SNK_TRANSITION_SINK_VBUS
, 0);
1746 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED
:
1747 tcpm_set_state(port
, PR_SWAP_SRC_SNK_SINK_ON
, 0);
1749 case PR_SWAP_SNK_SRC_SINK_OFF
:
1750 tcpm_set_state(port
, PR_SWAP_SNK_SRC_SOURCE_ON
, 0);
1752 case VCONN_SWAP_WAIT_FOR_VCONN
:
1753 tcpm_set_state(port
, VCONN_SWAP_TURN_OFF_VCONN
, 0);
1759 case PD_CTRL_REJECT
:
1761 case PD_CTRL_NOT_SUPP
:
1762 switch (port
->state
) {
1763 case SNK_NEGOTIATE_CAPABILITIES
:
1764 /* USB PD specification, Figure 8-43 */
1765 if (port
->explicit_contract
)
1766 next_state
= SNK_READY
;
1768 next_state
= SNK_WAIT_CAPABILITIES
;
1769 tcpm_set_state(port
, next_state
, 0);
1771 case SNK_NEGOTIATE_PPS_CAPABILITIES
:
1772 /* Revert data back from any requested PPS updates */
1773 port
->pps_data
.out_volt
= port
->supply_voltage
;
1774 port
->pps_data
.op_curr
= port
->current_limit
;
1775 port
->pps_status
= (type
== PD_CTRL_WAIT
?
1776 -EAGAIN
: -EOPNOTSUPP
);
1777 tcpm_set_state(port
, SNK_READY
, 0);
1780 port
->swap_status
= (type
== PD_CTRL_WAIT
?
1781 -EAGAIN
: -EOPNOTSUPP
);
1782 tcpm_set_state(port
, DR_SWAP_CANCEL
, 0);
1785 port
->swap_status
= (type
== PD_CTRL_WAIT
?
1786 -EAGAIN
: -EOPNOTSUPP
);
1787 tcpm_set_state(port
, PR_SWAP_CANCEL
, 0);
1789 case VCONN_SWAP_SEND
:
1790 port
->swap_status
= (type
== PD_CTRL_WAIT
?
1791 -EAGAIN
: -EOPNOTSUPP
);
1792 tcpm_set_state(port
, VCONN_SWAP_CANCEL
, 0);
1798 case PD_CTRL_ACCEPT
:
1799 switch (port
->state
) {
1800 case SNK_NEGOTIATE_CAPABILITIES
:
1801 port
->pps_data
.active
= false;
1802 tcpm_set_state(port
, SNK_TRANSITION_SINK
, 0);
1804 case SNK_NEGOTIATE_PPS_CAPABILITIES
:
1805 port
->pps_data
.active
= true;
1806 port
->supply_voltage
= port
->pps_data
.out_volt
;
1807 port
->current_limit
= port
->pps_data
.op_curr
;
1808 tcpm_set_state(port
, SNK_TRANSITION_SINK
, 0);
1810 case SOFT_RESET_SEND
:
1811 port
->message_id
= 0;
1812 port
->rx_msgid
= -1;
1813 if (port
->pwr_role
== TYPEC_SOURCE
)
1814 next_state
= SRC_SEND_CAPABILITIES
;
1816 next_state
= SNK_WAIT_CAPABILITIES
;
1817 tcpm_set_state(port
, next_state
, 0);
1820 tcpm_set_state(port
, DR_SWAP_CHANGE_DR
, 0);
1823 tcpm_set_state(port
, PR_SWAP_START
, 0);
1825 case VCONN_SWAP_SEND
:
1826 tcpm_set_state(port
, VCONN_SWAP_START
, 0);
1832 case PD_CTRL_SOFT_RESET
:
1833 tcpm_set_state(port
, SOFT_RESET
, 0);
1835 case PD_CTRL_DR_SWAP
:
1836 if (port
->port_type
!= TYPEC_PORT_DRP
) {
1837 tcpm_queue_message(port
, PD_MSG_CTRL_REJECT
);
1842 * 6.3.9: If an alternate mode is active, a request to swap
1843 * alternate modes shall trigger a port reset.
1845 switch (port
->state
) {
1848 tcpm_set_state(port
, DR_SWAP_ACCEPT
, 0);
1851 tcpm_queue_message(port
, PD_MSG_CTRL_WAIT
);
1855 case PD_CTRL_PR_SWAP
:
1856 if (port
->port_type
!= TYPEC_PORT_DRP
) {
1857 tcpm_queue_message(port
, PD_MSG_CTRL_REJECT
);
1860 switch (port
->state
) {
1863 tcpm_set_state(port
, PR_SWAP_ACCEPT
, 0);
1866 tcpm_queue_message(port
, PD_MSG_CTRL_WAIT
);
1870 case PD_CTRL_VCONN_SWAP
:
1871 switch (port
->state
) {
1874 tcpm_set_state(port
, VCONN_SWAP_ACCEPT
, 0);
1877 tcpm_queue_message(port
, PD_MSG_CTRL_WAIT
);
1881 case PD_CTRL_GET_SOURCE_CAP_EXT
:
1882 case PD_CTRL_GET_STATUS
:
1883 case PD_CTRL_FR_SWAP
:
1884 case PD_CTRL_GET_PPS_STATUS
:
1885 case PD_CTRL_GET_COUNTRY_CODES
:
1886 /* Currently not supported */
1887 tcpm_queue_message(port
, PD_MSG_CTRL_NOT_SUPP
);
1890 tcpm_log(port
, "Unhandled ctrl message type %#x", type
);
1895 static void tcpm_pd_ext_msg_request(struct tcpm_port
*port
,
1896 const struct pd_message
*msg
)
1898 enum pd_ext_msg_type type
= pd_header_type_le(msg
->header
);
1899 unsigned int data_size
= pd_ext_header_data_size_le(msg
->ext_msg
.header
);
1901 if (!(msg
->ext_msg
.header
& PD_EXT_HDR_CHUNKED
)) {
1902 tcpm_log(port
, "Unchunked extended messages unsupported");
1906 if (data_size
> PD_EXT_MAX_CHUNK_DATA
) {
1907 tcpm_log(port
, "Chunk handling not yet supported");
1914 * If PPS related events raised then get PPS status to clear
1915 * (see USB PD 3.0 Spec, 6.5.2.4)
1917 if (msg
->ext_msg
.data
[USB_PD_EXT_SDB_EVENT_FLAGS
] &
1918 USB_PD_EXT_SDB_PPS_EVENTS
)
1919 tcpm_set_state(port
, GET_PPS_STATUS_SEND
, 0);
1921 tcpm_set_state(port
, ready_state(port
), 0);
1923 case PD_EXT_PPS_STATUS
:
1925 * For now the PPS status message is used to clear events
1928 tcpm_set_state(port
, ready_state(port
), 0);
1930 case PD_EXT_SOURCE_CAP_EXT
:
1931 case PD_EXT_GET_BATT_CAP
:
1932 case PD_EXT_GET_BATT_STATUS
:
1933 case PD_EXT_BATT_CAP
:
1934 case PD_EXT_GET_MANUFACTURER_INFO
:
1935 case PD_EXT_MANUFACTURER_INFO
:
1936 case PD_EXT_SECURITY_REQUEST
:
1937 case PD_EXT_SECURITY_RESPONSE
:
1938 case PD_EXT_FW_UPDATE_REQUEST
:
1939 case PD_EXT_FW_UPDATE_RESPONSE
:
1940 case PD_EXT_COUNTRY_INFO
:
1941 case PD_EXT_COUNTRY_CODES
:
1942 tcpm_queue_message(port
, PD_MSG_CTRL_NOT_SUPP
);
1945 tcpm_log(port
, "Unhandled extended message type %#x", type
);
1950 static void tcpm_pd_rx_handler(struct work_struct
*work
)
1952 struct pd_rx_event
*event
= container_of(work
,
1953 struct pd_rx_event
, work
);
1954 const struct pd_message
*msg
= &event
->msg
;
1955 unsigned int cnt
= pd_header_cnt_le(msg
->header
);
1956 struct tcpm_port
*port
= event
->port
;
1958 mutex_lock(&port
->lock
);
1960 tcpm_log(port
, "PD RX, header: %#x [%d]", le16_to_cpu(msg
->header
),
1963 if (port
->attached
) {
1964 enum pd_ctrl_msg_type type
= pd_header_type_le(msg
->header
);
1965 unsigned int msgid
= pd_header_msgid_le(msg
->header
);
1968 * USB PD standard, 6.6.1.2:
1969 * "... if MessageID value in a received Message is the
1970 * same as the stored value, the receiver shall return a
1971 * GoodCRC Message with that MessageID value and drop
1972 * the Message (this is a retry of an already received
1973 * Message). Note: this shall not apply to the Soft_Reset
1974 * Message which always has a MessageID value of zero."
1976 if (msgid
== port
->rx_msgid
&& type
!= PD_CTRL_SOFT_RESET
)
1978 port
->rx_msgid
= msgid
;
1981 * If both ends believe to be DFP/host, we have a data role
1984 if (!!(le16_to_cpu(msg
->header
) & PD_HEADER_DATA_ROLE
) ==
1985 (port
->data_role
== TYPEC_HOST
)) {
1987 "Data role mismatch, initiating error recovery");
1988 tcpm_set_state(port
, ERROR_RECOVERY
, 0);
1990 if (msg
->header
& PD_HEADER_EXT_HDR
)
1991 tcpm_pd_ext_msg_request(port
, msg
);
1993 tcpm_pd_data_request(port
, msg
);
1995 tcpm_pd_ctrl_request(port
, msg
);
2000 mutex_unlock(&port
->lock
);
2004 void tcpm_pd_receive(struct tcpm_port
*port
, const struct pd_message
*msg
)
2006 struct pd_rx_event
*event
;
2008 event
= kzalloc(sizeof(*event
), GFP_ATOMIC
);
2012 INIT_WORK(&event
->work
, tcpm_pd_rx_handler
);
2014 memcpy(&event
->msg
, msg
, sizeof(*msg
));
2015 queue_work(port
->wq
, &event
->work
);
2017 EXPORT_SYMBOL_GPL(tcpm_pd_receive
);
2019 static int tcpm_pd_send_control(struct tcpm_port
*port
,
2020 enum pd_ctrl_msg_type type
)
2022 struct pd_message msg
;
2024 memset(&msg
, 0, sizeof(msg
));
2025 msg
.header
= PD_HEADER_LE(type
, port
->pwr_role
,
2027 port
->negotiated_rev
,
2028 port
->message_id
, 0);
2030 return tcpm_pd_transmit(port
, TCPC_TX_SOP
, &msg
);
2034 * Send queued message without affecting state.
2035 * Return true if state machine should go back to sleep,
2038 static bool tcpm_send_queued_message(struct tcpm_port
*port
)
2040 enum pd_msg_request queued_message
;
2043 queued_message
= port
->queued_message
;
2044 port
->queued_message
= PD_MSG_NONE
;
2046 switch (queued_message
) {
2047 case PD_MSG_CTRL_WAIT
:
2048 tcpm_pd_send_control(port
, PD_CTRL_WAIT
);
2050 case PD_MSG_CTRL_REJECT
:
2051 tcpm_pd_send_control(port
, PD_CTRL_REJECT
);
2053 case PD_MSG_CTRL_NOT_SUPP
:
2054 tcpm_pd_send_control(port
, PD_CTRL_NOT_SUPP
);
2056 case PD_MSG_DATA_SINK_CAP
:
2057 tcpm_pd_send_sink_caps(port
);
2059 case PD_MSG_DATA_SOURCE_CAP
:
2060 tcpm_pd_send_source_caps(port
);
2065 } while (port
->queued_message
!= PD_MSG_NONE
);
2067 if (port
->delayed_state
!= INVALID_STATE
) {
2068 if (time_is_after_jiffies(port
->delayed_runtime
)) {
2069 mod_delayed_work(port
->wq
, &port
->state_machine
,
2070 port
->delayed_runtime
- jiffies
);
2073 port
->delayed_state
= INVALID_STATE
;
2078 static int tcpm_pd_check_request(struct tcpm_port
*port
)
2080 u32 pdo
, rdo
= port
->sink_request
;
2081 unsigned int max
, op
, pdo_max
, index
;
2082 enum pd_pdo_type type
;
2084 index
= rdo_index(rdo
);
2085 if (!index
|| index
> port
->nr_src_pdo
)
2088 pdo
= port
->src_pdo
[index
- 1];
2089 type
= pdo_type(pdo
);
2091 case PDO_TYPE_FIXED
:
2093 max
= rdo_max_current(rdo
);
2094 op
= rdo_op_current(rdo
);
2095 pdo_max
= pdo_max_current(pdo
);
2099 if (max
> pdo_max
&& !(rdo
& RDO_CAP_MISMATCH
))
2102 if (type
== PDO_TYPE_FIXED
)
2104 "Requested %u mV, %u mA for %u / %u mA",
2105 pdo_fixed_voltage(pdo
), pdo_max
, op
, max
);
2108 "Requested %u -> %u mV, %u mA for %u / %u mA",
2109 pdo_min_voltage(pdo
), pdo_max_voltage(pdo
),
2113 max
= rdo_max_power(rdo
);
2114 op
= rdo_op_power(rdo
);
2115 pdo_max
= pdo_max_power(pdo
);
2119 if (max
> pdo_max
&& !(rdo
& RDO_CAP_MISMATCH
))
2122 "Requested %u -> %u mV, %u mW for %u / %u mW",
2123 pdo_min_voltage(pdo
), pdo_max_voltage(pdo
),
2130 port
->op_vsafe5v
= index
== 1;
2135 #define min_power(x, y) min(pdo_max_power(x), pdo_max_power(y))
2136 #define min_current(x, y) min(pdo_max_current(x), pdo_max_current(y))
2138 static int tcpm_pd_select_pdo(struct tcpm_port
*port
, int *sink_pdo
,
2141 unsigned int i
, j
, max_src_mv
= 0, min_src_mv
= 0, max_mw
= 0,
2142 max_mv
= 0, src_mw
= 0, src_ma
= 0, max_snk_mv
= 0,
2146 port
->pps_data
.supported
= false;
2147 port
->usb_type
= POWER_SUPPLY_USB_TYPE_PD
;
2150 * Select the source PDO providing the most power which has a
2153 for (i
= 0; i
< port
->nr_source_caps
; i
++) {
2154 u32 pdo
= port
->source_caps
[i
];
2155 enum pd_pdo_type type
= pdo_type(pdo
);
2158 case PDO_TYPE_FIXED
:
2159 max_src_mv
= pdo_fixed_voltage(pdo
);
2160 min_src_mv
= max_src_mv
;
2164 max_src_mv
= pdo_max_voltage(pdo
);
2165 min_src_mv
= pdo_min_voltage(pdo
);
2168 if (pdo_apdo_type(pdo
) == APDO_TYPE_PPS
) {
2169 port
->pps_data
.supported
= true;
2171 POWER_SUPPLY_USB_TYPE_PD_PPS
;
2175 tcpm_log(port
, "Invalid source PDO type, ignoring");
2180 case PDO_TYPE_FIXED
:
2182 src_ma
= pdo_max_current(pdo
);
2183 src_mw
= src_ma
* min_src_mv
/ 1000;
2186 src_mw
= pdo_max_power(pdo
);
2191 tcpm_log(port
, "Invalid source PDO type, ignoring");
2195 for (j
= 0; j
< port
->nr_snk_pdo
; j
++) {
2196 pdo
= port
->snk_pdo
[j
];
2198 switch (pdo_type(pdo
)) {
2199 case PDO_TYPE_FIXED
:
2200 max_snk_mv
= pdo_fixed_voltage(pdo
);
2201 min_snk_mv
= max_snk_mv
;
2205 max_snk_mv
= pdo_max_voltage(pdo
);
2206 min_snk_mv
= pdo_min_voltage(pdo
);
2211 tcpm_log(port
, "Invalid sink PDO type, ignoring");
2215 if (max_src_mv
<= max_snk_mv
&&
2216 min_src_mv
>= min_snk_mv
) {
2217 /* Prefer higher voltages if available */
2218 if ((src_mw
== max_mw
&& min_src_mv
> max_mv
) ||
2223 max_mv
= min_src_mv
;
2233 #define min_pps_apdo_current(x, y) \
2234 min(pdo_pps_apdo_max_current(x), pdo_pps_apdo_max_current(y))
2236 static unsigned int tcpm_pd_select_pps_apdo(struct tcpm_port
*port
)
2238 unsigned int i
, j
, max_mw
= 0, max_mv
= 0;
2239 unsigned int min_src_mv
, max_src_mv
, src_ma
, src_mw
;
2240 unsigned int min_snk_mv
, max_snk_mv
, snk_ma
;
2242 unsigned int src_pdo
= 0, snk_pdo
= 0;
2245 * Select the source PPS APDO providing the most power while staying
2246 * within the board's limits. We skip the first PDO as this is always
2249 for (i
= 1; i
< port
->nr_source_caps
; ++i
) {
2250 pdo
= port
->source_caps
[i
];
2252 switch (pdo_type(pdo
)) {
2254 if (pdo_apdo_type(pdo
) != APDO_TYPE_PPS
) {
2255 tcpm_log(port
, "Not PPS APDO (source), ignoring");
2259 min_src_mv
= pdo_pps_apdo_min_voltage(pdo
);
2260 max_src_mv
= pdo_pps_apdo_max_voltage(pdo
);
2261 src_ma
= pdo_pps_apdo_max_current(pdo
);
2262 src_mw
= (src_ma
* max_src_mv
) / 1000;
2265 * Now search through the sink PDOs to find a matching
2266 * PPS APDO. Again skip the first sink PDO as this will
2269 for (j
= 1; j
< port
->nr_snk_pdo
; j
++) {
2270 pdo
= port
->snk_pdo
[j
];
2272 switch (pdo_type(pdo
)) {
2274 if (pdo_apdo_type(pdo
) != APDO_TYPE_PPS
) {
2276 "Not PPS APDO (sink), ignoring");
2281 pdo_pps_apdo_min_voltage(pdo
);
2283 pdo_pps_apdo_max_voltage(pdo
);
2285 pdo_pps_apdo_max_current(pdo
);
2289 "Not APDO type (sink), ignoring");
2293 if (max_src_mv
<= max_snk_mv
&&
2294 min_src_mv
>= min_snk_mv
) {
2295 /* Prefer higher voltages if available */
2296 if ((src_mw
== max_mw
&&
2297 min_src_mv
> max_mv
) ||
2302 max_mv
= max_src_mv
;
2309 tcpm_log(port
, "Not APDO type (source), ignoring");
2315 pdo
= port
->source_caps
[src_pdo
];
2317 port
->pps_data
.min_volt
= pdo_pps_apdo_min_voltage(pdo
);
2318 port
->pps_data
.max_volt
= pdo_pps_apdo_max_voltage(pdo
);
2319 port
->pps_data
.max_curr
=
2320 min_pps_apdo_current(pdo
, port
->snk_pdo
[snk_pdo
]);
2321 port
->pps_data
.out_volt
=
2322 min(pdo_pps_apdo_max_voltage(pdo
), port
->pps_data
.out_volt
);
2323 port
->pps_data
.op_curr
=
2324 min(port
->pps_data
.max_curr
, port
->pps_data
.op_curr
);
2330 static int tcpm_pd_build_request(struct tcpm_port
*port
, u32
*rdo
)
2332 unsigned int mv
, ma
, mw
, flags
;
2333 unsigned int max_ma
, max_mw
;
2334 enum pd_pdo_type type
;
2335 u32 pdo
, matching_snk_pdo
;
2336 int src_pdo_index
= 0;
2337 int snk_pdo_index
= 0;
2340 ret
= tcpm_pd_select_pdo(port
, &snk_pdo_index
, &src_pdo_index
);
2344 pdo
= port
->source_caps
[src_pdo_index
];
2345 matching_snk_pdo
= port
->snk_pdo
[snk_pdo_index
];
2346 type
= pdo_type(pdo
);
2349 case PDO_TYPE_FIXED
:
2350 mv
= pdo_fixed_voltage(pdo
);
2354 mv
= pdo_min_voltage(pdo
);
2357 tcpm_log(port
, "Invalid PDO selected!");
2361 /* Select maximum available current within the sink pdo's limit */
2362 if (type
== PDO_TYPE_BATT
) {
2363 mw
= min_power(pdo
, matching_snk_pdo
);
2364 ma
= 1000 * mw
/ mv
;
2366 ma
= min_current(pdo
, matching_snk_pdo
);
2367 mw
= ma
* mv
/ 1000;
2370 flags
= RDO_USB_COMM
| RDO_NO_SUSPEND
;
2372 /* Set mismatch bit if offered power is less than operating power */
2375 if (mw
< port
->operating_snk_mw
) {
2376 flags
|= RDO_CAP_MISMATCH
;
2377 if (type
== PDO_TYPE_BATT
&&
2378 (pdo_max_power(matching_snk_pdo
) > pdo_max_power(pdo
)))
2379 max_mw
= pdo_max_power(matching_snk_pdo
);
2380 else if (pdo_max_current(matching_snk_pdo
) >
2381 pdo_max_current(pdo
))
2382 max_ma
= pdo_max_current(matching_snk_pdo
);
2385 tcpm_log(port
, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d",
2386 port
->cc_req
, port
->cc1
, port
->cc2
, port
->vbus_source
,
2387 port
->vconn_role
== TYPEC_SOURCE
? "source" : "sink",
2390 if (type
== PDO_TYPE_BATT
) {
2391 *rdo
= RDO_BATT(src_pdo_index
+ 1, mw
, max_mw
, flags
);
2393 tcpm_log(port
, "Requesting PDO %d: %u mV, %u mW%s",
2394 src_pdo_index
, mv
, mw
,
2395 flags
& RDO_CAP_MISMATCH
? " [mismatch]" : "");
2397 *rdo
= RDO_FIXED(src_pdo_index
+ 1, ma
, max_ma
, flags
);
2399 tcpm_log(port
, "Requesting PDO %d: %u mV, %u mA%s",
2400 src_pdo_index
, mv
, ma
,
2401 flags
& RDO_CAP_MISMATCH
? " [mismatch]" : "");
2404 port
->current_limit
= ma
;
2405 port
->supply_voltage
= mv
;
2410 static int tcpm_pd_send_request(struct tcpm_port
*port
)
2412 struct pd_message msg
;
2416 ret
= tcpm_pd_build_request(port
, &rdo
);
2420 memset(&msg
, 0, sizeof(msg
));
2421 msg
.header
= PD_HEADER_LE(PD_DATA_REQUEST
,
2424 port
->negotiated_rev
,
2425 port
->message_id
, 1);
2426 msg
.payload
[0] = cpu_to_le32(rdo
);
2428 return tcpm_pd_transmit(port
, TCPC_TX_SOP
, &msg
);
2431 static int tcpm_pd_build_pps_request(struct tcpm_port
*port
, u32
*rdo
)
2433 unsigned int out_mv
, op_ma
, op_mw
, min_mv
, max_mv
, max_ma
, flags
;
2434 enum pd_pdo_type type
;
2435 unsigned int src_pdo_index
;
2438 src_pdo_index
= tcpm_pd_select_pps_apdo(port
);
2442 pdo
= port
->source_caps
[src_pdo_index
];
2443 type
= pdo_type(pdo
);
2447 if (pdo_apdo_type(pdo
) != APDO_TYPE_PPS
) {
2448 tcpm_log(port
, "Invalid APDO selected!");
2451 min_mv
= port
->pps_data
.min_volt
;
2452 max_mv
= port
->pps_data
.max_volt
;
2453 max_ma
= port
->pps_data
.max_curr
;
2454 out_mv
= port
->pps_data
.out_volt
;
2455 op_ma
= port
->pps_data
.op_curr
;
2458 tcpm_log(port
, "Invalid PDO selected!");
2462 flags
= RDO_USB_COMM
| RDO_NO_SUSPEND
;
2464 op_mw
= (op_ma
* out_mv
) / 1000;
2465 if (op_mw
< port
->operating_snk_mw
) {
2467 * Try raising current to meet power needs. If that's not enough
2468 * then try upping the voltage. If that's still not enough
2469 * then we've obviously chosen a PPS APDO which really isn't
2470 * suitable so abandon ship.
2472 op_ma
= (port
->operating_snk_mw
* 1000) / out_mv
;
2473 if ((port
->operating_snk_mw
* 1000) % out_mv
)
2475 op_ma
+= RDO_PROG_CURR_MA_STEP
- (op_ma
% RDO_PROG_CURR_MA_STEP
);
2477 if (op_ma
> max_ma
) {
2479 out_mv
= (port
->operating_snk_mw
* 1000) / op_ma
;
2480 if ((port
->operating_snk_mw
* 1000) % op_ma
)
2482 out_mv
+= RDO_PROG_VOLT_MV_STEP
-
2483 (out_mv
% RDO_PROG_VOLT_MV_STEP
);
2485 if (out_mv
> max_mv
) {
2486 tcpm_log(port
, "Invalid PPS APDO selected!");
2492 tcpm_log(port
, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d",
2493 port
->cc_req
, port
->cc1
, port
->cc2
, port
->vbus_source
,
2494 port
->vconn_role
== TYPEC_SOURCE
? "source" : "sink",
2497 *rdo
= RDO_PROG(src_pdo_index
+ 1, out_mv
, op_ma
, flags
);
2499 tcpm_log(port
, "Requesting APDO %d: %u mV, %u mA",
2500 src_pdo_index
, out_mv
, op_ma
);
2502 port
->pps_data
.op_curr
= op_ma
;
2503 port
->pps_data
.out_volt
= out_mv
;
2508 static int tcpm_pd_send_pps_request(struct tcpm_port
*port
)
2510 struct pd_message msg
;
2514 ret
= tcpm_pd_build_pps_request(port
, &rdo
);
2518 memset(&msg
, 0, sizeof(msg
));
2519 msg
.header
= PD_HEADER_LE(PD_DATA_REQUEST
,
2522 port
->negotiated_rev
,
2523 port
->message_id
, 1);
2524 msg
.payload
[0] = cpu_to_le32(rdo
);
2526 return tcpm_pd_transmit(port
, TCPC_TX_SOP
, &msg
);
2529 static int tcpm_set_vbus(struct tcpm_port
*port
, bool enable
)
2533 if (enable
&& port
->vbus_charge
)
2536 tcpm_log(port
, "vbus:=%d charge=%d", enable
, port
->vbus_charge
);
2538 ret
= port
->tcpc
->set_vbus(port
->tcpc
, enable
, port
->vbus_charge
);
2542 port
->vbus_source
= enable
;
2546 static int tcpm_set_charge(struct tcpm_port
*port
, bool charge
)
2550 if (charge
&& port
->vbus_source
)
2553 if (charge
!= port
->vbus_charge
) {
2554 tcpm_log(port
, "vbus=%d charge:=%d", port
->vbus_source
, charge
);
2555 ret
= port
->tcpc
->set_vbus(port
->tcpc
, port
->vbus_source
,
2560 port
->vbus_charge
= charge
;
2564 static bool tcpm_start_toggling(struct tcpm_port
*port
, enum typec_cc_status cc
)
2568 if (!port
->tcpc
->start_toggling
)
2571 tcpm_log_force(port
, "Start toggling");
2572 ret
= port
->tcpc
->start_toggling(port
->tcpc
, port
->port_type
, cc
);
2576 static void tcpm_set_cc(struct tcpm_port
*port
, enum typec_cc_status cc
)
2578 tcpm_log(port
, "cc:=%d", cc
);
2580 port
->tcpc
->set_cc(port
->tcpc
, cc
);
2583 static int tcpm_init_vbus(struct tcpm_port
*port
)
2587 ret
= port
->tcpc
->set_vbus(port
->tcpc
, false, false);
2588 port
->vbus_source
= false;
2589 port
->vbus_charge
= false;
2593 static int tcpm_init_vconn(struct tcpm_port
*port
)
2597 ret
= port
->tcpc
->set_vconn(port
->tcpc
, false);
2598 port
->vconn_role
= TYPEC_SINK
;
2602 static void tcpm_typec_connect(struct tcpm_port
*port
)
2604 if (!port
->connected
) {
2605 /* Make sure we don't report stale identity information */
2606 memset(&port
->partner_ident
, 0, sizeof(port
->partner_ident
));
2607 port
->partner_desc
.usb_pd
= port
->pd_capable
;
2608 if (tcpm_port_is_debug(port
))
2609 port
->partner_desc
.accessory
= TYPEC_ACCESSORY_DEBUG
;
2610 else if (tcpm_port_is_audio(port
))
2611 port
->partner_desc
.accessory
= TYPEC_ACCESSORY_AUDIO
;
2613 port
->partner_desc
.accessory
= TYPEC_ACCESSORY_NONE
;
2614 port
->partner
= typec_register_partner(port
->typec_port
,
2615 &port
->partner_desc
);
2616 port
->connected
= true;
2620 static int tcpm_src_attach(struct tcpm_port
*port
)
2622 enum typec_cc_polarity polarity
=
2623 port
->cc2
== TYPEC_CC_RD
? TYPEC_POLARITY_CC2
2624 : TYPEC_POLARITY_CC1
;
2630 ret
= tcpm_set_polarity(port
, polarity
);
2634 ret
= tcpm_set_roles(port
, true, TYPEC_SOURCE
, TYPEC_HOST
);
2638 ret
= port
->tcpc
->set_pd_rx(port
->tcpc
, true);
2640 goto out_disable_mux
;
2643 * USB Type-C specification, version 1.2,
2644 * chapter 4.5.2.2.8.1 (Attached.SRC Requirements)
2645 * Enable VCONN only if the non-RD port is set to RA.
2647 if ((polarity
== TYPEC_POLARITY_CC1
&& port
->cc2
== TYPEC_CC_RA
) ||
2648 (polarity
== TYPEC_POLARITY_CC2
&& port
->cc1
== TYPEC_CC_RA
)) {
2649 ret
= tcpm_set_vconn(port
, true);
2651 goto out_disable_pd
;
2654 ret
= tcpm_set_vbus(port
, true);
2656 goto out_disable_vconn
;
2658 port
->pd_capable
= false;
2660 port
->partner
= NULL
;
2662 port
->attached
= true;
2663 port
->send_discover
= true;
2668 tcpm_set_vconn(port
, false);
2670 port
->tcpc
->set_pd_rx(port
->tcpc
, false);
2672 tcpm_mux_set(port
, TYPEC_STATE_SAFE
, USB_ROLE_NONE
,
2673 TYPEC_ORIENTATION_NONE
);
2677 static void tcpm_typec_disconnect(struct tcpm_port
*port
)
2679 if (port
->connected
) {
2680 typec_unregister_partner(port
->partner
);
2681 port
->partner
= NULL
;
2682 port
->connected
= false;
2686 static void tcpm_unregister_altmodes(struct tcpm_port
*port
)
2688 struct pd_mode_data
*modep
= &port
->mode_data
;
2691 for (i
= 0; i
< modep
->altmodes
; i
++) {
2692 typec_unregister_altmode(port
->partner_altmode
[i
]);
2693 port
->partner_altmode
[i
] = NULL
;
2696 memset(modep
, 0, sizeof(*modep
));
2699 static void tcpm_reset_port(struct tcpm_port
*port
)
2701 tcpm_unregister_altmodes(port
);
2702 tcpm_typec_disconnect(port
);
2703 port
->attached
= false;
2704 port
->pd_capable
= false;
2705 port
->pps_data
.supported
= false;
2708 * First Rx ID should be 0; set this to a sentinel of -1 so that
2709 * we can check tcpm_pd_rx_handler() if we had seen it before.
2711 port
->rx_msgid
= -1;
2713 port
->tcpc
->set_pd_rx(port
->tcpc
, false);
2714 tcpm_init_vbus(port
); /* also disables charging */
2715 tcpm_init_vconn(port
);
2716 tcpm_set_current_limit(port
, 0, 0);
2717 tcpm_set_polarity(port
, TYPEC_POLARITY_CC1
);
2718 tcpm_mux_set(port
, TYPEC_STATE_SAFE
, USB_ROLE_NONE
,
2719 TYPEC_ORIENTATION_NONE
);
2720 tcpm_set_attached_state(port
, false);
2721 port
->try_src_count
= 0;
2722 port
->try_snk_count
= 0;
2723 port
->usb_type
= POWER_SUPPLY_USB_TYPE_C
;
2725 power_supply_changed(port
->psy
);
2728 static void tcpm_detach(struct tcpm_port
*port
)
2730 if (!port
->attached
)
2733 if (tcpm_port_is_disconnected(port
))
2734 port
->hard_reset_count
= 0;
2736 tcpm_reset_port(port
);
2739 static void tcpm_src_detach(struct tcpm_port
*port
)
2744 static int tcpm_snk_attach(struct tcpm_port
*port
)
2751 ret
= tcpm_set_polarity(port
, port
->cc2
!= TYPEC_CC_OPEN
?
2752 TYPEC_POLARITY_CC2
: TYPEC_POLARITY_CC1
);
2756 ret
= tcpm_set_roles(port
, true, TYPEC_SINK
, TYPEC_DEVICE
);
2760 port
->pd_capable
= false;
2762 port
->partner
= NULL
;
2764 port
->attached
= true;
2765 port
->send_discover
= true;
2770 static void tcpm_snk_detach(struct tcpm_port
*port
)
2775 static int tcpm_acc_attach(struct tcpm_port
*port
)
2782 ret
= tcpm_set_roles(port
, true, TYPEC_SOURCE
, TYPEC_HOST
);
2786 port
->partner
= NULL
;
2788 tcpm_typec_connect(port
);
2790 port
->attached
= true;
2795 static void tcpm_acc_detach(struct tcpm_port
*port
)
2800 static inline enum tcpm_state
hard_reset_state(struct tcpm_port
*port
)
2802 if (port
->hard_reset_count
< PD_N_HARD_RESET_COUNT
)
2803 return HARD_RESET_SEND
;
2804 if (port
->pd_capable
)
2805 return ERROR_RECOVERY
;
2806 if (port
->pwr_role
== TYPEC_SOURCE
)
2807 return SRC_UNATTACHED
;
2808 if (port
->state
== SNK_WAIT_CAPABILITIES
)
2810 return SNK_UNATTACHED
;
2813 static inline enum tcpm_state
unattached_state(struct tcpm_port
*port
)
2815 if (port
->port_type
== TYPEC_PORT_DRP
) {
2816 if (port
->pwr_role
== TYPEC_SOURCE
)
2817 return SRC_UNATTACHED
;
2819 return SNK_UNATTACHED
;
2820 } else if (port
->port_type
== TYPEC_PORT_SRC
) {
2821 return SRC_UNATTACHED
;
2824 return SNK_UNATTACHED
;
2827 static void tcpm_check_send_discover(struct tcpm_port
*port
)
2829 if (port
->data_role
== TYPEC_HOST
&& port
->send_discover
&&
2831 tcpm_send_vdm(port
, USB_SID_PD
, CMD_DISCOVER_IDENT
, NULL
, 0);
2832 port
->send_discover
= false;
2836 static void tcpm_swap_complete(struct tcpm_port
*port
, int result
)
2838 if (port
->swap_pending
) {
2839 port
->swap_status
= result
;
2840 port
->swap_pending
= false;
2841 port
->non_pd_role_swap
= false;
2842 complete(&port
->swap_complete
);
2846 static enum typec_pwr_opmode
tcpm_get_pwr_opmode(enum typec_cc_status cc
)
2849 case TYPEC_CC_RP_1_5
:
2850 return TYPEC_PWR_MODE_1_5A
;
2851 case TYPEC_CC_RP_3_0
:
2852 return TYPEC_PWR_MODE_3_0A
;
2853 case TYPEC_CC_RP_DEF
:
2855 return TYPEC_PWR_MODE_USB
;
2859 static void run_state_machine(struct tcpm_port
*port
)
2862 enum typec_pwr_opmode opmode
;
2865 port
->enter_state
= port
->state
;
2866 switch (port
->state
) {
2870 case SRC_UNATTACHED
:
2871 if (!port
->non_pd_role_swap
)
2872 tcpm_swap_complete(port
, -ENOTCONN
);
2873 tcpm_src_detach(port
);
2874 if (tcpm_start_toggling(port
, tcpm_rp_cc(port
))) {
2875 tcpm_set_state(port
, TOGGLING
, 0);
2878 tcpm_set_cc(port
, tcpm_rp_cc(port
));
2879 if (port
->port_type
== TYPEC_PORT_DRP
)
2880 tcpm_set_state(port
, SNK_UNATTACHED
, PD_T_DRP_SNK
);
2882 case SRC_ATTACH_WAIT
:
2883 if (tcpm_port_is_debug(port
))
2884 tcpm_set_state(port
, DEBUG_ACC_ATTACHED
,
2886 else if (tcpm_port_is_audio(port
))
2887 tcpm_set_state(port
, AUDIO_ACC_ATTACHED
,
2889 else if (tcpm_port_is_source(port
))
2890 tcpm_set_state(port
,
2891 tcpm_try_snk(port
) ? SNK_TRY
2897 port
->try_snk_count
++;
2900 * - Do not drive vconn or vbus
2901 * - Terminate CC pins (both) to Rd
2903 * - Wait for tDRPTry (PD_T_DRP_TRY).
2904 * Until then, ignore any state changes.
2906 tcpm_set_cc(port
, TYPEC_CC_RD
);
2907 tcpm_set_state(port
, SNK_TRY_WAIT
, PD_T_DRP_TRY
);
2910 if (tcpm_port_is_sink(port
)) {
2911 tcpm_set_state(port
, SNK_TRY_WAIT_DEBOUNCE
, 0);
2913 tcpm_set_state(port
, SRC_TRYWAIT
, 0);
2917 case SNK_TRY_WAIT_DEBOUNCE
:
2918 tcpm_set_state(port
, SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS
,
2921 case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS
:
2922 if (port
->vbus_present
&& tcpm_port_is_sink(port
)) {
2923 tcpm_set_state(port
, SNK_ATTACHED
, 0);
2925 tcpm_set_state(port
, SRC_TRYWAIT
, 0);
2930 tcpm_set_cc(port
, tcpm_rp_cc(port
));
2931 if (port
->max_wait
== 0) {
2932 port
->max_wait
= jiffies
+
2933 msecs_to_jiffies(PD_T_DRP_TRY
);
2934 tcpm_set_state(port
, SRC_TRYWAIT_UNATTACHED
,
2937 if (time_is_after_jiffies(port
->max_wait
))
2938 tcpm_set_state(port
, SRC_TRYWAIT_UNATTACHED
,
2939 jiffies_to_msecs(port
->max_wait
-
2942 tcpm_set_state(port
, SNK_UNATTACHED
, 0);
2945 case SRC_TRYWAIT_DEBOUNCE
:
2946 tcpm_set_state(port
, SRC_ATTACHED
, PD_T_CC_DEBOUNCE
);
2948 case SRC_TRYWAIT_UNATTACHED
:
2949 tcpm_set_state(port
, SNK_UNATTACHED
, 0);
2953 ret
= tcpm_src_attach(port
);
2954 tcpm_set_state(port
, SRC_UNATTACHED
,
2955 ret
< 0 ? 0 : PD_T_PS_SOURCE_ON
);
2958 opmode
= tcpm_get_pwr_opmode(tcpm_rp_cc(port
));
2959 typec_set_pwr_opmode(port
->typec_port
, opmode
);
2960 port
->pwr_opmode
= TYPEC_PWR_MODE_USB
;
2961 port
->caps_count
= 0;
2962 port
->negotiated_rev
= PD_MAX_REV
;
2963 port
->message_id
= 0;
2964 port
->rx_msgid
= -1;
2965 port
->explicit_contract
= false;
2966 tcpm_set_state(port
, SRC_SEND_CAPABILITIES
, 0);
2968 case SRC_SEND_CAPABILITIES
:
2970 if (port
->caps_count
> PD_N_CAPS_COUNT
) {
2971 tcpm_set_state(port
, SRC_READY
, 0);
2974 ret
= tcpm_pd_send_source_caps(port
);
2976 tcpm_set_state(port
, SRC_SEND_CAPABILITIES
,
2977 PD_T_SEND_SOURCE_CAP
);
2980 * Per standard, we should clear the reset counter here.
2981 * However, that can result in state machine hang-ups.
2982 * Reset it only in READY state to improve stability.
2984 /* port->hard_reset_count = 0; */
2985 port
->caps_count
= 0;
2986 port
->pd_capable
= true;
2987 tcpm_set_state_cond(port
, SRC_SEND_CAPABILITIES_TIMEOUT
,
2988 PD_T_SEND_SOURCE_CAP
);
2991 case SRC_SEND_CAPABILITIES_TIMEOUT
:
2993 * Error recovery for a PD_DATA_SOURCE_CAP reply timeout.
2995 * PD 2.0 sinks are supposed to accept src-capabilities with a
2996 * 3.0 header and simply ignore any src PDOs which the sink does
2997 * not understand such as PPS but some 2.0 sinks instead ignore
2998 * the entire PD_DATA_SOURCE_CAP message, causing contract
2999 * negotiation to fail.
3001 * After PD_N_HARD_RESET_COUNT hard-reset attempts, we try
3002 * sending src-capabilities with a lower PD revision to
3003 * make these broken sinks work.
3005 if (port
->hard_reset_count
< PD_N_HARD_RESET_COUNT
) {
3006 tcpm_set_state(port
, HARD_RESET_SEND
, 0);
3007 } else if (port
->negotiated_rev
> PD_REV20
) {
3008 port
->negotiated_rev
--;
3009 port
->hard_reset_count
= 0;
3010 tcpm_set_state(port
, SRC_SEND_CAPABILITIES
, 0);
3012 tcpm_set_state(port
, hard_reset_state(port
), 0);
3015 case SRC_NEGOTIATE_CAPABILITIES
:
3016 ret
= tcpm_pd_check_request(port
);
3018 tcpm_pd_send_control(port
, PD_CTRL_REJECT
);
3019 if (!port
->explicit_contract
) {
3020 tcpm_set_state(port
,
3021 SRC_WAIT_NEW_CAPABILITIES
, 0);
3023 tcpm_set_state(port
, SRC_READY
, 0);
3026 tcpm_pd_send_control(port
, PD_CTRL_ACCEPT
);
3027 tcpm_set_state(port
, SRC_TRANSITION_SUPPLY
,
3028 PD_T_SRC_TRANSITION
);
3031 case SRC_TRANSITION_SUPPLY
:
3032 /* XXX: regulator_set_voltage(vbus, ...) */
3033 tcpm_pd_send_control(port
, PD_CTRL_PS_RDY
);
3034 port
->explicit_contract
= true;
3035 typec_set_pwr_opmode(port
->typec_port
, TYPEC_PWR_MODE_PD
);
3036 port
->pwr_opmode
= TYPEC_PWR_MODE_PD
;
3037 tcpm_set_state_cond(port
, SRC_READY
, 0);
3041 port
->hard_reset_count
= 0;
3043 port
->try_src_count
= 0;
3045 tcpm_swap_complete(port
, 0);
3046 tcpm_typec_connect(port
);
3048 tcpm_check_send_discover(port
);
3051 * Sending ping messages is not necessary if
3052 * - the source operates at vSafe5V
3054 * - The system is not operating in PD mode
3056 * - Both partners are connected using a Type-C connector
3058 * There is no actual need to send PD messages since the local
3059 * port type-c and the spec does not clearly say whether PD is
3060 * possible when type-c is connected to Type-A/B
3063 case SRC_WAIT_NEW_CAPABILITIES
:
3064 /* Nothing to do... */
3068 case SNK_UNATTACHED
:
3069 if (!port
->non_pd_role_swap
)
3070 tcpm_swap_complete(port
, -ENOTCONN
);
3071 tcpm_pps_complete(port
, -ENOTCONN
);
3072 tcpm_snk_detach(port
);
3073 if (tcpm_start_toggling(port
, TYPEC_CC_RD
)) {
3074 tcpm_set_state(port
, TOGGLING
, 0);
3077 tcpm_set_cc(port
, TYPEC_CC_RD
);
3078 if (port
->port_type
== TYPEC_PORT_DRP
)
3079 tcpm_set_state(port
, SRC_UNATTACHED
, PD_T_DRP_SRC
);
3081 case SNK_ATTACH_WAIT
:
3082 if ((port
->cc1
== TYPEC_CC_OPEN
&&
3083 port
->cc2
!= TYPEC_CC_OPEN
) ||
3084 (port
->cc1
!= TYPEC_CC_OPEN
&&
3085 port
->cc2
== TYPEC_CC_OPEN
))
3086 tcpm_set_state(port
, SNK_DEBOUNCED
,
3088 else if (tcpm_port_is_disconnected(port
))
3089 tcpm_set_state(port
, SNK_UNATTACHED
,
3093 if (tcpm_port_is_disconnected(port
))
3094 tcpm_set_state(port
, SNK_UNATTACHED
,
3096 else if (port
->vbus_present
)
3097 tcpm_set_state(port
,
3098 tcpm_try_src(port
) ? SRC_TRY
3102 /* Wait for VBUS, but not forever */
3103 tcpm_set_state(port
, PORT_RESET
, PD_T_PS_SOURCE_ON
);
3107 port
->try_src_count
++;
3108 tcpm_set_cc(port
, tcpm_rp_cc(port
));
3110 tcpm_set_state(port
, SRC_TRY_WAIT
, 0);
3113 if (port
->max_wait
== 0) {
3114 port
->max_wait
= jiffies
+
3115 msecs_to_jiffies(PD_T_DRP_TRY
);
3116 msecs
= PD_T_DRP_TRY
;
3118 if (time_is_after_jiffies(port
->max_wait
))
3119 msecs
= jiffies_to_msecs(port
->max_wait
-
3124 tcpm_set_state(port
, SNK_TRYWAIT
, msecs
);
3126 case SRC_TRY_DEBOUNCE
:
3127 tcpm_set_state(port
, SRC_ATTACHED
, PD_T_PD_DEBOUNCE
);
3130 tcpm_set_cc(port
, TYPEC_CC_RD
);
3131 tcpm_set_state(port
, SNK_TRYWAIT_VBUS
, PD_T_CC_DEBOUNCE
);
3133 case SNK_TRYWAIT_VBUS
:
3135 * TCPM stays in this state indefinitely until VBUS
3136 * is detected as long as Rp is not detected for
3137 * more than a time period of tPDDebounce.
3139 if (port
->vbus_present
&& tcpm_port_is_sink(port
)) {
3140 tcpm_set_state(port
, SNK_ATTACHED
, 0);
3143 if (!tcpm_port_is_sink(port
))
3144 tcpm_set_state(port
, SNK_TRYWAIT_DEBOUNCE
, 0);
3146 case SNK_TRYWAIT_DEBOUNCE
:
3147 tcpm_set_state(port
, SNK_UNATTACHED
, PD_T_PD_DEBOUNCE
);
3150 ret
= tcpm_snk_attach(port
);
3152 tcpm_set_state(port
, SNK_UNATTACHED
, 0);
3154 tcpm_set_state(port
, SNK_STARTUP
, 0);
3157 opmode
= tcpm_get_pwr_opmode(port
->polarity
?
3158 port
->cc2
: port
->cc1
);
3159 typec_set_pwr_opmode(port
->typec_port
, opmode
);
3160 port
->pwr_opmode
= TYPEC_PWR_MODE_USB
;
3161 port
->negotiated_rev
= PD_MAX_REV
;
3162 port
->message_id
= 0;
3163 port
->rx_msgid
= -1;
3164 port
->explicit_contract
= false;
3165 tcpm_set_state(port
, SNK_DISCOVERY
, 0);
3168 if (port
->vbus_present
) {
3169 tcpm_set_current_limit(port
,
3170 tcpm_get_current_limit(port
),
3172 tcpm_set_charge(port
, true);
3173 tcpm_set_state(port
, SNK_WAIT_CAPABILITIES
, 0);
3177 * For DRP, timeouts differ. Also, handling is supposed to be
3178 * different and much more complex (dead battery detection;
3179 * see USB power delivery specification, section 8.3.3.6.1.5.1).
3181 tcpm_set_state(port
, hard_reset_state(port
),
3182 port
->port_type
== TYPEC_PORT_DRP
?
3183 PD_T_DB_DETECT
: PD_T_NO_RESPONSE
);
3185 case SNK_DISCOVERY_DEBOUNCE
:
3186 tcpm_set_state(port
, SNK_DISCOVERY_DEBOUNCE_DONE
,
3189 case SNK_DISCOVERY_DEBOUNCE_DONE
:
3190 if (!tcpm_port_is_disconnected(port
) &&
3191 tcpm_port_is_sink(port
) &&
3192 time_is_after_jiffies(port
->delayed_runtime
)) {
3193 tcpm_set_state(port
, SNK_DISCOVERY
,
3194 jiffies_to_msecs(port
->delayed_runtime
-
3198 tcpm_set_state(port
, unattached_state(port
), 0);
3200 case SNK_WAIT_CAPABILITIES
:
3201 ret
= port
->tcpc
->set_pd_rx(port
->tcpc
, true);
3203 tcpm_set_state(port
, SNK_READY
, 0);
3207 * If VBUS has never been low, and we time out waiting
3208 * for source cap, try a soft reset first, in case we
3209 * were already in a stable contract before this boot.
3210 * Do this only once.
3212 if (port
->vbus_never_low
) {
3213 port
->vbus_never_low
= false;
3214 tcpm_set_state(port
, SOFT_RESET_SEND
,
3215 PD_T_SINK_WAIT_CAP
);
3217 tcpm_set_state(port
, hard_reset_state(port
),
3218 PD_T_SINK_WAIT_CAP
);
3221 case SNK_NEGOTIATE_CAPABILITIES
:
3222 port
->pd_capable
= true;
3223 port
->hard_reset_count
= 0;
3224 ret
= tcpm_pd_send_request(port
);
3226 /* Let the Source send capabilities again. */
3227 tcpm_set_state(port
, SNK_WAIT_CAPABILITIES
, 0);
3229 tcpm_set_state_cond(port
, hard_reset_state(port
),
3230 PD_T_SENDER_RESPONSE
);
3233 case SNK_NEGOTIATE_PPS_CAPABILITIES
:
3234 ret
= tcpm_pd_send_pps_request(port
);
3236 port
->pps_status
= ret
;
3238 * If this was called due to updates to sink
3239 * capabilities, and pps is no longer valid, we should
3240 * safely fall back to a standard PDO.
3242 if (port
->update_sink_caps
)
3243 tcpm_set_state(port
, SNK_NEGOTIATE_CAPABILITIES
, 0);
3245 tcpm_set_state(port
, SNK_READY
, 0);
3247 tcpm_set_state_cond(port
, hard_reset_state(port
),
3248 PD_T_SENDER_RESPONSE
);
3251 case SNK_TRANSITION_SINK
:
3252 case SNK_TRANSITION_SINK_VBUS
:
3253 tcpm_set_state(port
, hard_reset_state(port
),
3254 PD_T_PS_TRANSITION
);
3257 port
->try_snk_count
= 0;
3258 port
->update_sink_caps
= false;
3259 if (port
->explicit_contract
) {
3260 typec_set_pwr_opmode(port
->typec_port
,
3262 port
->pwr_opmode
= TYPEC_PWR_MODE_PD
;
3265 tcpm_swap_complete(port
, 0);
3266 tcpm_typec_connect(port
);
3267 tcpm_check_send_discover(port
);
3268 tcpm_pps_complete(port
, port
->pps_status
);
3270 power_supply_changed(port
->psy
);
3274 /* Accessory states */
3275 case ACC_UNATTACHED
:
3276 tcpm_acc_detach(port
);
3277 tcpm_set_state(port
, SRC_UNATTACHED
, 0);
3279 case DEBUG_ACC_ATTACHED
:
3280 case AUDIO_ACC_ATTACHED
:
3281 ret
= tcpm_acc_attach(port
);
3283 tcpm_set_state(port
, ACC_UNATTACHED
, 0);
3285 case AUDIO_ACC_DEBOUNCE
:
3286 tcpm_set_state(port
, ACC_UNATTACHED
, PD_T_CC_DEBOUNCE
);
3289 /* Hard_Reset states */
3290 case HARD_RESET_SEND
:
3291 tcpm_pd_transmit(port
, TCPC_TX_HARD_RESET
, NULL
);
3292 tcpm_set_state(port
, HARD_RESET_START
, 0);
3294 case HARD_RESET_START
:
3295 port
->hard_reset_count
++;
3296 port
->tcpc
->set_pd_rx(port
->tcpc
, false);
3297 tcpm_unregister_altmodes(port
);
3298 port
->send_discover
= true;
3299 if (port
->pwr_role
== TYPEC_SOURCE
)
3300 tcpm_set_state(port
, SRC_HARD_RESET_VBUS_OFF
,
3301 PD_T_PS_HARD_RESET
);
3303 tcpm_set_state(port
, SNK_HARD_RESET_SINK_OFF
, 0);
3305 case SRC_HARD_RESET_VBUS_OFF
:
3306 tcpm_set_vconn(port
, true);
3307 tcpm_set_vbus(port
, false);
3308 tcpm_set_roles(port
, port
->self_powered
, TYPEC_SOURCE
,
3310 tcpm_set_state(port
, SRC_HARD_RESET_VBUS_ON
, PD_T_SRC_RECOVER
);
3312 case SRC_HARD_RESET_VBUS_ON
:
3313 tcpm_set_vbus(port
, true);
3314 port
->tcpc
->set_pd_rx(port
->tcpc
, true);
3315 tcpm_set_attached_state(port
, true);
3316 tcpm_set_state(port
, SRC_UNATTACHED
, PD_T_PS_SOURCE_ON
);
3318 case SNK_HARD_RESET_SINK_OFF
:
3319 memset(&port
->pps_data
, 0, sizeof(port
->pps_data
));
3320 tcpm_set_vconn(port
, false);
3321 if (port
->pd_capable
)
3322 tcpm_set_charge(port
, false);
3323 tcpm_set_roles(port
, port
->self_powered
, TYPEC_SINK
,
3326 * VBUS may or may not toggle, depending on the adapter.
3327 * If it doesn't toggle, transition to SNK_HARD_RESET_SINK_ON
3328 * directly after timeout.
3330 tcpm_set_state(port
, SNK_HARD_RESET_SINK_ON
, PD_T_SAFE_0V
);
3332 case SNK_HARD_RESET_WAIT_VBUS
:
3333 /* Assume we're disconnected if VBUS doesn't come back. */
3334 tcpm_set_state(port
, SNK_UNATTACHED
,
3335 PD_T_SRC_RECOVER_MAX
+ PD_T_SRC_TURN_ON
);
3337 case SNK_HARD_RESET_SINK_ON
:
3338 /* Note: There is no guarantee that VBUS is on in this state */
3341 * The specification suggests that dual mode ports in sink
3342 * mode should transition to state PE_SRC_Transition_to_default.
3343 * See USB power delivery specification chapter 8.3.3.6.1.3.
3344 * This would mean to to
3345 * - turn off VCONN, reset power supply
3346 * - request hardware reset
3348 * - Transition to state PE_Src_Startup
3349 * SNK only ports shall transition to state Snk_Startup
3350 * (see chapter 8.3.3.3.8).
3351 * Similar, dual-mode ports in source mode should transition
3352 * to PE_SNK_Transition_to_default.
3354 if (port
->pd_capable
) {
3355 tcpm_set_current_limit(port
,
3356 tcpm_get_current_limit(port
),
3358 tcpm_set_charge(port
, true);
3360 tcpm_set_attached_state(port
, true);
3361 tcpm_set_state(port
, SNK_STARTUP
, 0);
3364 /* Soft_Reset states */
3366 port
->message_id
= 0;
3367 port
->rx_msgid
= -1;
3368 tcpm_pd_send_control(port
, PD_CTRL_ACCEPT
);
3369 if (port
->pwr_role
== TYPEC_SOURCE
)
3370 tcpm_set_state(port
, SRC_SEND_CAPABILITIES
, 0);
3372 tcpm_set_state(port
, SNK_WAIT_CAPABILITIES
, 0);
3374 case SOFT_RESET_SEND
:
3375 port
->message_id
= 0;
3376 port
->rx_msgid
= -1;
3377 if (tcpm_pd_send_control(port
, PD_CTRL_SOFT_RESET
))
3378 tcpm_set_state_cond(port
, hard_reset_state(port
), 0);
3380 tcpm_set_state_cond(port
, hard_reset_state(port
),
3381 PD_T_SENDER_RESPONSE
);
3384 /* DR_Swap states */
3386 tcpm_pd_send_control(port
, PD_CTRL_DR_SWAP
);
3387 tcpm_set_state_cond(port
, DR_SWAP_SEND_TIMEOUT
,
3388 PD_T_SENDER_RESPONSE
);
3390 case DR_SWAP_ACCEPT
:
3391 tcpm_pd_send_control(port
, PD_CTRL_ACCEPT
);
3392 tcpm_set_state_cond(port
, DR_SWAP_CHANGE_DR
, 0);
3394 case DR_SWAP_SEND_TIMEOUT
:
3395 tcpm_swap_complete(port
, -ETIMEDOUT
);
3396 tcpm_set_state(port
, ready_state(port
), 0);
3398 case DR_SWAP_CHANGE_DR
:
3399 if (port
->data_role
== TYPEC_HOST
) {
3400 tcpm_unregister_altmodes(port
);
3401 tcpm_set_roles(port
, true, port
->pwr_role
,
3404 tcpm_set_roles(port
, true, port
->pwr_role
,
3406 port
->send_discover
= true;
3408 tcpm_set_state(port
, ready_state(port
), 0);
3411 /* PR_Swap states */
3412 case PR_SWAP_ACCEPT
:
3413 tcpm_pd_send_control(port
, PD_CTRL_ACCEPT
);
3414 tcpm_set_state(port
, PR_SWAP_START
, 0);
3417 tcpm_pd_send_control(port
, PD_CTRL_PR_SWAP
);
3418 tcpm_set_state_cond(port
, PR_SWAP_SEND_TIMEOUT
,
3419 PD_T_SENDER_RESPONSE
);
3421 case PR_SWAP_SEND_TIMEOUT
:
3422 tcpm_swap_complete(port
, -ETIMEDOUT
);
3423 tcpm_set_state(port
, ready_state(port
), 0);
3426 if (port
->pwr_role
== TYPEC_SOURCE
)
3427 tcpm_set_state(port
, PR_SWAP_SRC_SNK_TRANSITION_OFF
,
3428 PD_T_SRC_TRANSITION
);
3430 tcpm_set_state(port
, PR_SWAP_SNK_SRC_SINK_OFF
, 0);
3432 case PR_SWAP_SRC_SNK_TRANSITION_OFF
:
3433 tcpm_set_vbus(port
, false);
3434 port
->explicit_contract
= false;
3435 /* allow time for Vbus discharge, must be < tSrcSwapStdby */
3436 tcpm_set_state(port
, PR_SWAP_SRC_SNK_SOURCE_OFF
,
3439 case PR_SWAP_SRC_SNK_SOURCE_OFF
:
3440 tcpm_set_cc(port
, TYPEC_CC_RD
);
3441 /* allow CC debounce */
3442 tcpm_set_state(port
, PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED
,
3445 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED
:
3447 * USB-PD standard, 6.2.1.4, Port Power Role:
3448 * "During the Power Role Swap Sequence, for the initial Source
3449 * Port, the Port Power Role field shall be set to Sink in the
3450 * PS_RDY Message indicating that the initial Source’s power
3451 * supply is turned off"
3453 tcpm_set_pwr_role(port
, TYPEC_SINK
);
3454 if (tcpm_pd_send_control(port
, PD_CTRL_PS_RDY
)) {
3455 tcpm_set_state(port
, ERROR_RECOVERY
, 0);
3458 tcpm_set_state_cond(port
, SNK_UNATTACHED
, PD_T_PS_SOURCE_ON
);
3460 case PR_SWAP_SRC_SNK_SINK_ON
:
3461 tcpm_set_state(port
, SNK_STARTUP
, 0);
3463 case PR_SWAP_SNK_SRC_SINK_OFF
:
3464 tcpm_set_charge(port
, false);
3465 tcpm_set_state(port
, hard_reset_state(port
),
3466 PD_T_PS_SOURCE_OFF
);
3468 case PR_SWAP_SNK_SRC_SOURCE_ON
:
3469 tcpm_set_cc(port
, tcpm_rp_cc(port
));
3470 tcpm_set_vbus(port
, true);
3472 * allow time VBUS ramp-up, must be < tNewSrc
3473 * Also, this window overlaps with CC debounce as well.
3474 * So, Wait for the max of two which is PD_T_NEWSRC
3476 tcpm_set_state(port
, PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP
,
3479 case PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP
:
3481 * USB PD standard, 6.2.1.4:
3482 * "Subsequent Messages initiated by the Policy Engine,
3483 * such as the PS_RDY Message sent to indicate that Vbus
3484 * is ready, will have the Port Power Role field set to
3487 tcpm_set_pwr_role(port
, TYPEC_SOURCE
);
3488 tcpm_pd_send_control(port
, PD_CTRL_PS_RDY
);
3489 tcpm_set_state(port
, SRC_STARTUP
, 0);
3492 case VCONN_SWAP_ACCEPT
:
3493 tcpm_pd_send_control(port
, PD_CTRL_ACCEPT
);
3494 tcpm_set_state(port
, VCONN_SWAP_START
, 0);
3496 case VCONN_SWAP_SEND
:
3497 tcpm_pd_send_control(port
, PD_CTRL_VCONN_SWAP
);
3498 tcpm_set_state(port
, VCONN_SWAP_SEND_TIMEOUT
,
3499 PD_T_SENDER_RESPONSE
);
3501 case VCONN_SWAP_SEND_TIMEOUT
:
3502 tcpm_swap_complete(port
, -ETIMEDOUT
);
3503 tcpm_set_state(port
, ready_state(port
), 0);
3505 case VCONN_SWAP_START
:
3506 if (port
->vconn_role
== TYPEC_SOURCE
)
3507 tcpm_set_state(port
, VCONN_SWAP_WAIT_FOR_VCONN
, 0);
3509 tcpm_set_state(port
, VCONN_SWAP_TURN_ON_VCONN
, 0);
3511 case VCONN_SWAP_WAIT_FOR_VCONN
:
3512 tcpm_set_state(port
, hard_reset_state(port
),
3513 PD_T_VCONN_SOURCE_ON
);
3515 case VCONN_SWAP_TURN_ON_VCONN
:
3516 tcpm_set_vconn(port
, true);
3517 tcpm_pd_send_control(port
, PD_CTRL_PS_RDY
);
3518 tcpm_set_state(port
, ready_state(port
), 0);
3520 case VCONN_SWAP_TURN_OFF_VCONN
:
3521 tcpm_set_vconn(port
, false);
3522 tcpm_set_state(port
, ready_state(port
), 0);
3525 case DR_SWAP_CANCEL
:
3526 case PR_SWAP_CANCEL
:
3527 case VCONN_SWAP_CANCEL
:
3528 tcpm_swap_complete(port
, port
->swap_status
);
3529 if (port
->pwr_role
== TYPEC_SOURCE
)
3530 tcpm_set_state(port
, SRC_READY
, 0);
3532 tcpm_set_state(port
, SNK_READY
, 0);
3536 switch (BDO_MODE_MASK(port
->bist_request
)) {
3537 case BDO_MODE_CARRIER2
:
3538 tcpm_pd_transmit(port
, TCPC_TX_BIST_MODE_2
, NULL
);
3543 /* Always switch to unattached state */
3544 tcpm_set_state(port
, unattached_state(port
), 0);
3546 case GET_STATUS_SEND
:
3547 tcpm_pd_send_control(port
, PD_CTRL_GET_STATUS
);
3548 tcpm_set_state(port
, GET_STATUS_SEND_TIMEOUT
,
3549 PD_T_SENDER_RESPONSE
);
3551 case GET_STATUS_SEND_TIMEOUT
:
3552 tcpm_set_state(port
, ready_state(port
), 0);
3554 case GET_PPS_STATUS_SEND
:
3555 tcpm_pd_send_control(port
, PD_CTRL_GET_PPS_STATUS
);
3556 tcpm_set_state(port
, GET_PPS_STATUS_SEND_TIMEOUT
,
3557 PD_T_SENDER_RESPONSE
);
3559 case GET_PPS_STATUS_SEND_TIMEOUT
:
3560 tcpm_set_state(port
, ready_state(port
), 0);
3562 case ERROR_RECOVERY
:
3563 tcpm_swap_complete(port
, -EPROTO
);
3564 tcpm_pps_complete(port
, -EPROTO
);
3565 tcpm_set_state(port
, PORT_RESET
, 0);
3568 tcpm_reset_port(port
);
3569 tcpm_set_cc(port
, TYPEC_CC_OPEN
);
3570 tcpm_set_state(port
, PORT_RESET_WAIT_OFF
,
3571 PD_T_ERROR_RECOVERY
);
3573 case PORT_RESET_WAIT_OFF
:
3574 tcpm_set_state(port
,
3575 tcpm_default_state(port
),
3576 port
->vbus_present
? PD_T_PS_SOURCE_OFF
: 0);
3579 WARN(1, "Unexpected port state %d\n", port
->state
);
3584 static void tcpm_state_machine_work(struct work_struct
*work
)
3586 struct tcpm_port
*port
= container_of(work
, struct tcpm_port
,
3587 state_machine
.work
);
3588 enum tcpm_state prev_state
;
3590 mutex_lock(&port
->lock
);
3591 port
->state_machine_running
= true;
3593 if (port
->queued_message
&& tcpm_send_queued_message(port
))
3596 /* If we were queued due to a delayed state change, update it now */
3597 if (port
->delayed_state
) {
3598 tcpm_log(port
, "state change %s -> %s [delayed %ld ms]",
3599 tcpm_states
[port
->state
],
3600 tcpm_states
[port
->delayed_state
], port
->delay_ms
);
3601 port
->prev_state
= port
->state
;
3602 port
->state
= port
->delayed_state
;
3603 port
->delayed_state
= INVALID_STATE
;
3607 * Continue running as long as we have (non-delayed) state changes
3611 prev_state
= port
->state
;
3612 run_state_machine(port
);
3613 if (port
->queued_message
)
3614 tcpm_send_queued_message(port
);
3615 } while (port
->state
!= prev_state
&& !port
->delayed_state
);
3618 port
->state_machine_running
= false;
3619 mutex_unlock(&port
->lock
);
3622 static void _tcpm_cc_change(struct tcpm_port
*port
, enum typec_cc_status cc1
,
3623 enum typec_cc_status cc2
)
3625 enum typec_cc_status old_cc1
, old_cc2
;
3626 enum tcpm_state new_state
;
3628 old_cc1
= port
->cc1
;
3629 old_cc2
= port
->cc2
;
3633 tcpm_log_force(port
,
3634 "CC1: %u -> %u, CC2: %u -> %u [state %s, polarity %d, %s]",
3635 old_cc1
, cc1
, old_cc2
, cc2
, tcpm_states
[port
->state
],
3637 tcpm_port_is_disconnected(port
) ? "disconnected"
3640 switch (port
->state
) {
3642 if (tcpm_port_is_debug(port
) || tcpm_port_is_audio(port
) ||
3643 tcpm_port_is_source(port
))
3644 tcpm_set_state(port
, SRC_ATTACH_WAIT
, 0);
3645 else if (tcpm_port_is_sink(port
))
3646 tcpm_set_state(port
, SNK_ATTACH_WAIT
, 0);
3648 case SRC_UNATTACHED
:
3649 case ACC_UNATTACHED
:
3650 if (tcpm_port_is_debug(port
) || tcpm_port_is_audio(port
) ||
3651 tcpm_port_is_source(port
))
3652 tcpm_set_state(port
, SRC_ATTACH_WAIT
, 0);
3654 case SRC_ATTACH_WAIT
:
3655 if (tcpm_port_is_disconnected(port
) ||
3656 tcpm_port_is_audio_detached(port
))
3657 tcpm_set_state(port
, SRC_UNATTACHED
, 0);
3658 else if (cc1
!= old_cc1
|| cc2
!= old_cc2
)
3659 tcpm_set_state(port
, SRC_ATTACH_WAIT
, 0);
3662 case SRC_SEND_CAPABILITIES
:
3664 if (tcpm_port_is_disconnected(port
) ||
3665 !tcpm_port_is_source(port
))
3666 tcpm_set_state(port
, SRC_UNATTACHED
, 0);
3668 case SNK_UNATTACHED
:
3669 if (tcpm_port_is_sink(port
))
3670 tcpm_set_state(port
, SNK_ATTACH_WAIT
, 0);
3672 case SNK_ATTACH_WAIT
:
3673 if ((port
->cc1
== TYPEC_CC_OPEN
&&
3674 port
->cc2
!= TYPEC_CC_OPEN
) ||
3675 (port
->cc1
!= TYPEC_CC_OPEN
&&
3676 port
->cc2
== TYPEC_CC_OPEN
))
3677 new_state
= SNK_DEBOUNCED
;
3678 else if (tcpm_port_is_disconnected(port
))
3679 new_state
= SNK_UNATTACHED
;
3682 if (new_state
!= port
->delayed_state
)
3683 tcpm_set_state(port
, SNK_ATTACH_WAIT
, 0);
3686 if (tcpm_port_is_disconnected(port
))
3687 new_state
= SNK_UNATTACHED
;
3688 else if (port
->vbus_present
)
3689 new_state
= tcpm_try_src(port
) ? SRC_TRY
: SNK_ATTACHED
;
3691 new_state
= SNK_UNATTACHED
;
3692 if (new_state
!= port
->delayed_state
)
3693 tcpm_set_state(port
, SNK_DEBOUNCED
, 0);
3696 if (tcpm_port_is_disconnected(port
))
3697 tcpm_set_state(port
, unattached_state(port
), 0);
3698 else if (!port
->pd_capable
&&
3699 (cc1
!= old_cc1
|| cc2
!= old_cc2
))
3700 tcpm_set_current_limit(port
,
3701 tcpm_get_current_limit(port
),
3705 case AUDIO_ACC_ATTACHED
:
3706 if (cc1
== TYPEC_CC_OPEN
|| cc2
== TYPEC_CC_OPEN
)
3707 tcpm_set_state(port
, AUDIO_ACC_DEBOUNCE
, 0);
3709 case AUDIO_ACC_DEBOUNCE
:
3710 if (tcpm_port_is_audio(port
))
3711 tcpm_set_state(port
, AUDIO_ACC_ATTACHED
, 0);
3714 case DEBUG_ACC_ATTACHED
:
3715 if (cc1
== TYPEC_CC_OPEN
|| cc2
== TYPEC_CC_OPEN
)
3716 tcpm_set_state(port
, ACC_UNATTACHED
, 0);
3720 /* Do nothing, waiting for timeout */
3724 /* CC line is unstable, wait for debounce */
3725 if (tcpm_port_is_disconnected(port
))
3726 tcpm_set_state(port
, SNK_DISCOVERY_DEBOUNCE
, 0);
3728 case SNK_DISCOVERY_DEBOUNCE
:
3732 /* Hand over to state machine if needed */
3733 if (!port
->vbus_present
&& tcpm_port_is_source(port
))
3734 tcpm_set_state(port
, SRC_TRYWAIT_DEBOUNCE
, 0);
3736 case SRC_TRYWAIT_DEBOUNCE
:
3737 if (port
->vbus_present
|| !tcpm_port_is_source(port
))
3738 tcpm_set_state(port
, SRC_TRYWAIT
, 0);
3740 case SNK_TRY_WAIT_DEBOUNCE
:
3741 if (!tcpm_port_is_sink(port
)) {
3743 tcpm_set_state(port
, SRC_TRYWAIT
, 0);
3747 if (tcpm_port_is_source(port
))
3748 tcpm_set_state(port
, SRC_TRY_DEBOUNCE
, 0);
3750 case SRC_TRY_DEBOUNCE
:
3751 tcpm_set_state(port
, SRC_TRY_WAIT
, 0);
3753 case SNK_TRYWAIT_DEBOUNCE
:
3754 if (tcpm_port_is_sink(port
))
3755 tcpm_set_state(port
, SNK_TRYWAIT_VBUS
, 0);
3757 case SNK_TRYWAIT_VBUS
:
3758 if (!tcpm_port_is_sink(port
))
3759 tcpm_set_state(port
, SNK_TRYWAIT_DEBOUNCE
, 0);
3762 /* Do nothing, waiting for tCCDebounce */
3764 case PR_SWAP_SNK_SRC_SINK_OFF
:
3765 case PR_SWAP_SRC_SNK_TRANSITION_OFF
:
3766 case PR_SWAP_SRC_SNK_SOURCE_OFF
:
3767 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED
:
3768 case PR_SWAP_SNK_SRC_SOURCE_ON
:
3770 * CC state change is expected in PR_SWAP
3776 if (tcpm_port_is_disconnected(port
))
3777 tcpm_set_state(port
, unattached_state(port
), 0);
3782 static void _tcpm_pd_vbus_on(struct tcpm_port
*port
)
3784 tcpm_log_force(port
, "VBUS on");
3785 port
->vbus_present
= true;
3786 switch (port
->state
) {
3787 case SNK_TRANSITION_SINK_VBUS
:
3788 port
->explicit_contract
= true;
3789 tcpm_set_state(port
, SNK_READY
, 0);
3792 tcpm_set_state(port
, SNK_DISCOVERY
, 0);
3796 tcpm_set_state(port
, tcpm_try_src(port
) ? SRC_TRY
3800 case SNK_HARD_RESET_WAIT_VBUS
:
3801 tcpm_set_state(port
, SNK_HARD_RESET_SINK_ON
, 0);
3804 tcpm_set_state(port
, SRC_STARTUP
, 0);
3806 case SRC_HARD_RESET_VBUS_ON
:
3807 tcpm_set_state(port
, SRC_STARTUP
, 0);
3811 /* Do nothing, waiting for timeout */
3814 /* Do nothing, Waiting for Rd to be detected */
3816 case SRC_TRYWAIT_DEBOUNCE
:
3817 tcpm_set_state(port
, SRC_TRYWAIT
, 0);
3819 case SNK_TRY_WAIT_DEBOUNCE
:
3820 /* Do nothing, waiting for PD_DEBOUNCE to do be done */
3823 /* Do nothing, waiting for tCCDebounce */
3825 case SNK_TRYWAIT_VBUS
:
3826 if (tcpm_port_is_sink(port
))
3827 tcpm_set_state(port
, SNK_ATTACHED
, 0);
3829 case SNK_TRYWAIT_DEBOUNCE
:
3830 /* Do nothing, waiting for Rp */
3833 case SRC_TRY_DEBOUNCE
:
3834 /* Do nothing, waiting for sink detection */
3841 static void _tcpm_pd_vbus_off(struct tcpm_port
*port
)
3843 tcpm_log_force(port
, "VBUS off");
3844 port
->vbus_present
= false;
3845 port
->vbus_never_low
= false;
3846 switch (port
->state
) {
3847 case SNK_HARD_RESET_SINK_OFF
:
3848 tcpm_set_state(port
, SNK_HARD_RESET_WAIT_VBUS
, 0);
3850 case SRC_HARD_RESET_VBUS_OFF
:
3851 tcpm_set_state(port
, SRC_HARD_RESET_VBUS_ON
, 0);
3853 case HARD_RESET_SEND
:
3857 /* Do nothing, waiting for timeout */
3860 /* Hand over to state machine if needed */
3861 if (tcpm_port_is_source(port
))
3862 tcpm_set_state(port
, SRC_TRYWAIT_DEBOUNCE
, 0);
3864 case SNK_TRY_WAIT_DEBOUNCE
:
3865 /* Do nothing, waiting for PD_DEBOUNCE to do be done */
3868 case SNK_TRYWAIT_VBUS
:
3869 case SNK_TRYWAIT_DEBOUNCE
:
3871 case SNK_ATTACH_WAIT
:
3872 tcpm_set_state(port
, SNK_UNATTACHED
, 0);
3875 case SNK_NEGOTIATE_CAPABILITIES
:
3878 case PR_SWAP_SRC_SNK_TRANSITION_OFF
:
3879 tcpm_set_state(port
, PR_SWAP_SRC_SNK_SOURCE_OFF
, 0);
3882 case PR_SWAP_SNK_SRC_SINK_OFF
:
3883 /* Do nothing, expected */
3886 case PORT_RESET_WAIT_OFF
:
3887 tcpm_set_state(port
, tcpm_default_state(port
), 0);
3890 case SRC_TRY_DEBOUNCE
:
3891 /* Do nothing, waiting for sink detection */
3894 if (port
->pwr_role
== TYPEC_SINK
&&
3896 tcpm_set_state(port
, SNK_UNATTACHED
, 0);
3901 static void _tcpm_pd_hard_reset(struct tcpm_port
*port
)
3903 tcpm_log_force(port
, "Received hard reset");
3905 * If we keep receiving hard reset requests, executing the hard reset
3906 * must have failed. Revert to error recovery if that happens.
3908 tcpm_set_state(port
,
3909 port
->hard_reset_count
< PD_N_HARD_RESET_COUNT
?
3910 HARD_RESET_START
: ERROR_RECOVERY
,
3914 static void tcpm_pd_event_handler(struct work_struct
*work
)
3916 struct tcpm_port
*port
= container_of(work
, struct tcpm_port
,
3920 mutex_lock(&port
->lock
);
3922 spin_lock(&port
->pd_event_lock
);
3923 while (port
->pd_events
) {
3924 events
= port
->pd_events
;
3925 port
->pd_events
= 0;
3926 spin_unlock(&port
->pd_event_lock
);
3927 if (events
& TCPM_RESET_EVENT
)
3928 _tcpm_pd_hard_reset(port
);
3929 if (events
& TCPM_VBUS_EVENT
) {
3932 vbus
= port
->tcpc
->get_vbus(port
->tcpc
);
3934 _tcpm_pd_vbus_on(port
);
3936 _tcpm_pd_vbus_off(port
);
3938 if (events
& TCPM_CC_EVENT
) {
3939 enum typec_cc_status cc1
, cc2
;
3941 if (port
->tcpc
->get_cc(port
->tcpc
, &cc1
, &cc2
) == 0)
3942 _tcpm_cc_change(port
, cc1
, cc2
);
3944 spin_lock(&port
->pd_event_lock
);
3946 spin_unlock(&port
->pd_event_lock
);
3947 mutex_unlock(&port
->lock
);
3950 void tcpm_cc_change(struct tcpm_port
*port
)
3952 spin_lock(&port
->pd_event_lock
);
3953 port
->pd_events
|= TCPM_CC_EVENT
;
3954 spin_unlock(&port
->pd_event_lock
);
3955 queue_work(port
->wq
, &port
->event_work
);
3957 EXPORT_SYMBOL_GPL(tcpm_cc_change
);
3959 void tcpm_vbus_change(struct tcpm_port
*port
)
3961 spin_lock(&port
->pd_event_lock
);
3962 port
->pd_events
|= TCPM_VBUS_EVENT
;
3963 spin_unlock(&port
->pd_event_lock
);
3964 queue_work(port
->wq
, &port
->event_work
);
3966 EXPORT_SYMBOL_GPL(tcpm_vbus_change
);
3968 void tcpm_pd_hard_reset(struct tcpm_port
*port
)
3970 spin_lock(&port
->pd_event_lock
);
3971 port
->pd_events
= TCPM_RESET_EVENT
;
3972 spin_unlock(&port
->pd_event_lock
);
3973 queue_work(port
->wq
, &port
->event_work
);
3975 EXPORT_SYMBOL_GPL(tcpm_pd_hard_reset
);
3977 static int tcpm_dr_set(const struct typec_capability
*cap
,
3978 enum typec_data_role data
)
3980 struct tcpm_port
*port
= typec_cap_to_tcpm(cap
);
3983 mutex_lock(&port
->swap_lock
);
3984 mutex_lock(&port
->lock
);
3986 if (port
->port_type
!= TYPEC_PORT_DRP
) {
3990 if (port
->state
!= SRC_READY
&& port
->state
!= SNK_READY
) {
3995 if (port
->data_role
== data
) {
4002 * 6.3.9: If an alternate mode is active, a request to swap
4003 * alternate modes shall trigger a port reset.
4004 * Reject data role swap request in this case.
4007 if (!port
->pd_capable
) {
4009 * If the partner is not PD capable, reset the port to
4010 * trigger a role change. This can only work if a preferred
4011 * role is configured, and if it matches the requested role.
4013 if (port
->try_role
== TYPEC_NO_PREFERRED_ROLE
||
4014 port
->try_role
== port
->pwr_role
) {
4018 port
->non_pd_role_swap
= true;
4019 tcpm_set_state(port
, PORT_RESET
, 0);
4021 tcpm_set_state(port
, DR_SWAP_SEND
, 0);
4024 port
->swap_status
= 0;
4025 port
->swap_pending
= true;
4026 reinit_completion(&port
->swap_complete
);
4027 mutex_unlock(&port
->lock
);
4029 if (!wait_for_completion_timeout(&port
->swap_complete
,
4030 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT
)))
4033 ret
= port
->swap_status
;
4035 port
->non_pd_role_swap
= false;
4039 mutex_unlock(&port
->lock
);
4041 mutex_unlock(&port
->swap_lock
);
4045 static int tcpm_pr_set(const struct typec_capability
*cap
,
4046 enum typec_role role
)
4048 struct tcpm_port
*port
= typec_cap_to_tcpm(cap
);
4051 mutex_lock(&port
->swap_lock
);
4052 mutex_lock(&port
->lock
);
4054 if (port
->port_type
!= TYPEC_PORT_DRP
) {
4058 if (port
->state
!= SRC_READY
&& port
->state
!= SNK_READY
) {
4063 if (role
== port
->pwr_role
) {
4068 port
->swap_status
= 0;
4069 port
->swap_pending
= true;
4070 reinit_completion(&port
->swap_complete
);
4071 tcpm_set_state(port
, PR_SWAP_SEND
, 0);
4072 mutex_unlock(&port
->lock
);
4074 if (!wait_for_completion_timeout(&port
->swap_complete
,
4075 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT
)))
4078 ret
= port
->swap_status
;
4083 mutex_unlock(&port
->lock
);
4085 mutex_unlock(&port
->swap_lock
);
4089 static int tcpm_vconn_set(const struct typec_capability
*cap
,
4090 enum typec_role role
)
4092 struct tcpm_port
*port
= typec_cap_to_tcpm(cap
);
4095 mutex_lock(&port
->swap_lock
);
4096 mutex_lock(&port
->lock
);
4098 if (port
->state
!= SRC_READY
&& port
->state
!= SNK_READY
) {
4103 if (role
== port
->vconn_role
) {
4108 port
->swap_status
= 0;
4109 port
->swap_pending
= true;
4110 reinit_completion(&port
->swap_complete
);
4111 tcpm_set_state(port
, VCONN_SWAP_SEND
, 0);
4112 mutex_unlock(&port
->lock
);
4114 if (!wait_for_completion_timeout(&port
->swap_complete
,
4115 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT
)))
4118 ret
= port
->swap_status
;
4123 mutex_unlock(&port
->lock
);
4125 mutex_unlock(&port
->swap_lock
);
4129 static int tcpm_try_role(const struct typec_capability
*cap
, int role
)
4131 struct tcpm_port
*port
= typec_cap_to_tcpm(cap
);
4132 struct tcpc_dev
*tcpc
= port
->tcpc
;
4135 mutex_lock(&port
->lock
);
4137 ret
= tcpc
->try_role(tcpc
, role
);
4138 if (!ret
&& (!tcpc
->config
|| !tcpc
->config
->try_role_hw
))
4139 port
->try_role
= role
;
4140 port
->try_src_count
= 0;
4141 port
->try_snk_count
= 0;
4142 mutex_unlock(&port
->lock
);
4147 static int tcpm_pps_set_op_curr(struct tcpm_port
*port
, u16 op_curr
)
4149 unsigned int target_mw
;
4152 mutex_lock(&port
->swap_lock
);
4153 mutex_lock(&port
->lock
);
4155 if (!port
->pps_data
.active
) {
4160 if (port
->state
!= SNK_READY
) {
4165 if (op_curr
> port
->pps_data
.max_curr
) {
4170 target_mw
= (op_curr
* port
->pps_data
.out_volt
) / 1000;
4171 if (target_mw
< port
->operating_snk_mw
) {
4176 /* Round down operating current to align with PPS valid steps */
4177 op_curr
= op_curr
- (op_curr
% RDO_PROG_CURR_MA_STEP
);
4179 reinit_completion(&port
->pps_complete
);
4180 port
->pps_data
.op_curr
= op_curr
;
4181 port
->pps_status
= 0;
4182 port
->pps_pending
= true;
4183 tcpm_set_state(port
, SNK_NEGOTIATE_PPS_CAPABILITIES
, 0);
4184 mutex_unlock(&port
->lock
);
4186 if (!wait_for_completion_timeout(&port
->pps_complete
,
4187 msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT
)))
4190 ret
= port
->pps_status
;
4195 mutex_unlock(&port
->lock
);
4197 mutex_unlock(&port
->swap_lock
);
4202 static int tcpm_pps_set_out_volt(struct tcpm_port
*port
, u16 out_volt
)
4204 unsigned int target_mw
;
4207 mutex_lock(&port
->swap_lock
);
4208 mutex_lock(&port
->lock
);
4210 if (!port
->pps_data
.active
) {
4215 if (port
->state
!= SNK_READY
) {
4220 if (out_volt
< port
->pps_data
.min_volt
||
4221 out_volt
> port
->pps_data
.max_volt
) {
4226 target_mw
= (port
->pps_data
.op_curr
* out_volt
) / 1000;
4227 if (target_mw
< port
->operating_snk_mw
) {
4232 /* Round down output voltage to align with PPS valid steps */
4233 out_volt
= out_volt
- (out_volt
% RDO_PROG_VOLT_MV_STEP
);
4235 reinit_completion(&port
->pps_complete
);
4236 port
->pps_data
.out_volt
= out_volt
;
4237 port
->pps_status
= 0;
4238 port
->pps_pending
= true;
4239 tcpm_set_state(port
, SNK_NEGOTIATE_PPS_CAPABILITIES
, 0);
4240 mutex_unlock(&port
->lock
);
4242 if (!wait_for_completion_timeout(&port
->pps_complete
,
4243 msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT
)))
4246 ret
= port
->pps_status
;
4251 mutex_unlock(&port
->lock
);
4253 mutex_unlock(&port
->swap_lock
);
4258 static int tcpm_pps_activate(struct tcpm_port
*port
, bool activate
)
4262 mutex_lock(&port
->swap_lock
);
4263 mutex_lock(&port
->lock
);
4265 if (!port
->pps_data
.supported
) {
4270 /* Trying to deactivate PPS when already deactivated so just bail */
4271 if (!port
->pps_data
.active
&& !activate
)
4274 if (port
->state
!= SNK_READY
) {
4279 reinit_completion(&port
->pps_complete
);
4280 port
->pps_status
= 0;
4281 port
->pps_pending
= true;
4283 /* Trigger PPS request or move back to standard PDO contract */
4285 port
->pps_data
.out_volt
= port
->supply_voltage
;
4286 port
->pps_data
.op_curr
= port
->current_limit
;
4287 tcpm_set_state(port
, SNK_NEGOTIATE_PPS_CAPABILITIES
, 0);
4289 tcpm_set_state(port
, SNK_NEGOTIATE_CAPABILITIES
, 0);
4291 mutex_unlock(&port
->lock
);
4293 if (!wait_for_completion_timeout(&port
->pps_complete
,
4294 msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT
)))
4297 ret
= port
->pps_status
;
4302 mutex_unlock(&port
->lock
);
4304 mutex_unlock(&port
->swap_lock
);
4309 static void tcpm_init(struct tcpm_port
*port
)
4311 enum typec_cc_status cc1
, cc2
;
4313 port
->tcpc
->init(port
->tcpc
);
4315 tcpm_reset_port(port
);
4319 * Should possibly wait for VBUS to settle if it was enabled locally
4320 * since tcpm_reset_port() will disable VBUS.
4322 port
->vbus_present
= port
->tcpc
->get_vbus(port
->tcpc
);
4323 if (port
->vbus_present
)
4324 port
->vbus_never_low
= true;
4326 tcpm_set_state(port
, tcpm_default_state(port
), 0);
4328 if (port
->tcpc
->get_cc(port
->tcpc
, &cc1
, &cc2
) == 0)
4329 _tcpm_cc_change(port
, cc1
, cc2
);
4332 * Some adapters need a clean slate at startup, and won't recover
4333 * otherwise. So do not try to be fancy and force a clean disconnect.
4335 tcpm_set_state(port
, PORT_RESET
, 0);
4338 static int tcpm_port_type_set(const struct typec_capability
*cap
,
4339 enum typec_port_type type
)
4341 struct tcpm_port
*port
= typec_cap_to_tcpm(cap
);
4343 mutex_lock(&port
->lock
);
4344 if (type
== port
->port_type
)
4347 port
->port_type
= type
;
4349 if (!port
->connected
) {
4350 tcpm_set_state(port
, PORT_RESET
, 0);
4351 } else if (type
== TYPEC_PORT_SNK
) {
4352 if (!(port
->pwr_role
== TYPEC_SINK
&&
4353 port
->data_role
== TYPEC_DEVICE
))
4354 tcpm_set_state(port
, PORT_RESET
, 0);
4355 } else if (type
== TYPEC_PORT_SRC
) {
4356 if (!(port
->pwr_role
== TYPEC_SOURCE
&&
4357 port
->data_role
== TYPEC_HOST
))
4358 tcpm_set_state(port
, PORT_RESET
, 0);
4362 mutex_unlock(&port
->lock
);
4366 void tcpm_tcpc_reset(struct tcpm_port
*port
)
4368 mutex_lock(&port
->lock
);
4369 /* XXX: Maintain PD connection if possible? */
4371 mutex_unlock(&port
->lock
);
4373 EXPORT_SYMBOL_GPL(tcpm_tcpc_reset
);
4375 static int tcpm_copy_pdos(u32
*dest_pdo
, const u32
*src_pdo
,
4376 unsigned int nr_pdo
)
4380 if (nr_pdo
> PDO_MAX_OBJECTS
)
4381 nr_pdo
= PDO_MAX_OBJECTS
;
4383 for (i
= 0; i
< nr_pdo
; i
++)
4384 dest_pdo
[i
] = src_pdo
[i
];
4389 static int tcpm_copy_vdos(u32
*dest_vdo
, const u32
*src_vdo
,
4390 unsigned int nr_vdo
)
4394 if (nr_vdo
> VDO_MAX_OBJECTS
)
4395 nr_vdo
= VDO_MAX_OBJECTS
;
4397 for (i
= 0; i
< nr_vdo
; i
++)
4398 dest_vdo
[i
] = src_vdo
[i
];
4403 static int tcpm_fw_get_caps(struct tcpm_port
*port
,
4404 struct fwnode_handle
*fwnode
)
4406 const char *cap_str
;
4413 /* USB data support is optional */
4414 ret
= fwnode_property_read_string(fwnode
, "data-role", &cap_str
);
4416 port
->typec_caps
.data
= typec_find_port_data_role(cap_str
);
4417 if (port
->typec_caps
.data
< 0)
4421 ret
= fwnode_property_read_string(fwnode
, "power-role", &cap_str
);
4425 port
->typec_caps
.type
= typec_find_port_power_role(cap_str
);
4426 if (port
->typec_caps
.type
< 0)
4428 port
->port_type
= port
->typec_caps
.type
;
4430 if (port
->port_type
== TYPEC_PORT_SNK
)
4433 /* Get source pdos */
4434 ret
= fwnode_property_read_u32_array(fwnode
, "source-pdos",
4439 port
->nr_src_pdo
= min(ret
, PDO_MAX_OBJECTS
);
4440 ret
= fwnode_property_read_u32_array(fwnode
, "source-pdos",
4441 port
->src_pdo
, port
->nr_src_pdo
);
4442 if ((ret
< 0) || tcpm_validate_caps(port
, port
->src_pdo
,
4446 if (port
->port_type
== TYPEC_PORT_SRC
)
4449 /* Get the preferred power role for DRP */
4450 ret
= fwnode_property_read_string(fwnode
, "try-power-role", &cap_str
);
4454 port
->typec_caps
.prefer_role
= typec_find_power_role(cap_str
);
4455 if (port
->typec_caps
.prefer_role
< 0)
4459 ret
= fwnode_property_read_u32_array(fwnode
, "sink-pdos",
4464 port
->nr_snk_pdo
= min(ret
, PDO_MAX_OBJECTS
);
4465 ret
= fwnode_property_read_u32_array(fwnode
, "sink-pdos",
4466 port
->snk_pdo
, port
->nr_snk_pdo
);
4467 if ((ret
< 0) || tcpm_validate_caps(port
, port
->snk_pdo
,
4471 if (fwnode_property_read_u32(fwnode
, "op-sink-microwatt", &mw
) < 0)
4473 port
->operating_snk_mw
= mw
/ 1000;
4475 port
->self_powered
= fwnode_property_read_bool(fwnode
, "self-powered");
4480 int tcpm_update_source_capabilities(struct tcpm_port
*port
, const u32
*pdo
,
4481 unsigned int nr_pdo
)
4483 if (tcpm_validate_caps(port
, pdo
, nr_pdo
))
4486 mutex_lock(&port
->lock
);
4487 port
->nr_src_pdo
= tcpm_copy_pdos(port
->src_pdo
, pdo
, nr_pdo
);
4488 switch (port
->state
) {
4489 case SRC_UNATTACHED
:
4490 case SRC_ATTACH_WAIT
:
4492 tcpm_set_cc(port
, tcpm_rp_cc(port
));
4494 case SRC_SEND_CAPABILITIES
:
4495 case SRC_NEGOTIATE_CAPABILITIES
:
4497 case SRC_WAIT_NEW_CAPABILITIES
:
4498 tcpm_set_cc(port
, tcpm_rp_cc(port
));
4499 tcpm_set_state(port
, SRC_SEND_CAPABILITIES
, 0);
4504 mutex_unlock(&port
->lock
);
4507 EXPORT_SYMBOL_GPL(tcpm_update_source_capabilities
);
4509 int tcpm_update_sink_capabilities(struct tcpm_port
*port
, const u32
*pdo
,
4510 unsigned int nr_pdo
,
4511 unsigned int operating_snk_mw
)
4513 if (tcpm_validate_caps(port
, pdo
, nr_pdo
))
4516 mutex_lock(&port
->lock
);
4517 port
->nr_snk_pdo
= tcpm_copy_pdos(port
->snk_pdo
, pdo
, nr_pdo
);
4518 port
->operating_snk_mw
= operating_snk_mw
;
4519 port
->update_sink_caps
= true;
4521 switch (port
->state
) {
4522 case SNK_NEGOTIATE_CAPABILITIES
:
4523 case SNK_NEGOTIATE_PPS_CAPABILITIES
:
4525 case SNK_TRANSITION_SINK
:
4526 case SNK_TRANSITION_SINK_VBUS
:
4527 if (port
->pps_data
.active
)
4528 tcpm_set_state(port
, SNK_NEGOTIATE_PPS_CAPABILITIES
, 0);
4530 tcpm_set_state(port
, SNK_NEGOTIATE_CAPABILITIES
, 0);
4535 mutex_unlock(&port
->lock
);
4538 EXPORT_SYMBOL_GPL(tcpm_update_sink_capabilities
);
4540 /* Power Supply access to expose source power information */
4541 enum tcpm_psy_online_states
{
4542 TCPM_PSY_OFFLINE
= 0,
4543 TCPM_PSY_FIXED_ONLINE
,
4544 TCPM_PSY_PROG_ONLINE
,
4547 static enum power_supply_property tcpm_psy_props
[] = {
4548 POWER_SUPPLY_PROP_USB_TYPE
,
4549 POWER_SUPPLY_PROP_ONLINE
,
4550 POWER_SUPPLY_PROP_VOLTAGE_MIN
,
4551 POWER_SUPPLY_PROP_VOLTAGE_MAX
,
4552 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
4553 POWER_SUPPLY_PROP_CURRENT_MAX
,
4554 POWER_SUPPLY_PROP_CURRENT_NOW
,
4557 static int tcpm_psy_get_online(struct tcpm_port
*port
,
4558 union power_supply_propval
*val
)
4560 if (port
->vbus_charge
) {
4561 if (port
->pps_data
.active
)
4562 val
->intval
= TCPM_PSY_PROG_ONLINE
;
4564 val
->intval
= TCPM_PSY_FIXED_ONLINE
;
4566 val
->intval
= TCPM_PSY_OFFLINE
;
4572 static int tcpm_psy_get_voltage_min(struct tcpm_port
*port
,
4573 union power_supply_propval
*val
)
4575 if (port
->pps_data
.active
)
4576 val
->intval
= port
->pps_data
.min_volt
* 1000;
4578 val
->intval
= port
->supply_voltage
* 1000;
4583 static int tcpm_psy_get_voltage_max(struct tcpm_port
*port
,
4584 union power_supply_propval
*val
)
4586 if (port
->pps_data
.active
)
4587 val
->intval
= port
->pps_data
.max_volt
* 1000;
4589 val
->intval
= port
->supply_voltage
* 1000;
4594 static int tcpm_psy_get_voltage_now(struct tcpm_port
*port
,
4595 union power_supply_propval
*val
)
4597 val
->intval
= port
->supply_voltage
* 1000;
4602 static int tcpm_psy_get_current_max(struct tcpm_port
*port
,
4603 union power_supply_propval
*val
)
4605 if (port
->pps_data
.active
)
4606 val
->intval
= port
->pps_data
.max_curr
* 1000;
4608 val
->intval
= port
->current_limit
* 1000;
4613 static int tcpm_psy_get_current_now(struct tcpm_port
*port
,
4614 union power_supply_propval
*val
)
4616 val
->intval
= port
->current_limit
* 1000;
4621 static int tcpm_psy_get_prop(struct power_supply
*psy
,
4622 enum power_supply_property psp
,
4623 union power_supply_propval
*val
)
4625 struct tcpm_port
*port
= power_supply_get_drvdata(psy
);
4629 case POWER_SUPPLY_PROP_USB_TYPE
:
4630 val
->intval
= port
->usb_type
;
4632 case POWER_SUPPLY_PROP_ONLINE
:
4633 ret
= tcpm_psy_get_online(port
, val
);
4635 case POWER_SUPPLY_PROP_VOLTAGE_MIN
:
4636 ret
= tcpm_psy_get_voltage_min(port
, val
);
4638 case POWER_SUPPLY_PROP_VOLTAGE_MAX
:
4639 ret
= tcpm_psy_get_voltage_max(port
, val
);
4641 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
4642 ret
= tcpm_psy_get_voltage_now(port
, val
);
4644 case POWER_SUPPLY_PROP_CURRENT_MAX
:
4645 ret
= tcpm_psy_get_current_max(port
, val
);
4647 case POWER_SUPPLY_PROP_CURRENT_NOW
:
4648 ret
= tcpm_psy_get_current_now(port
, val
);
4658 static int tcpm_psy_set_online(struct tcpm_port
*port
,
4659 const union power_supply_propval
*val
)
4663 switch (val
->intval
) {
4664 case TCPM_PSY_FIXED_ONLINE
:
4665 ret
= tcpm_pps_activate(port
, false);
4667 case TCPM_PSY_PROG_ONLINE
:
4668 ret
= tcpm_pps_activate(port
, true);
4678 static int tcpm_psy_set_prop(struct power_supply
*psy
,
4679 enum power_supply_property psp
,
4680 const union power_supply_propval
*val
)
4682 struct tcpm_port
*port
= power_supply_get_drvdata(psy
);
4686 case POWER_SUPPLY_PROP_ONLINE
:
4687 ret
= tcpm_psy_set_online(port
, val
);
4689 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
4690 if (val
->intval
< port
->pps_data
.min_volt
* 1000 ||
4691 val
->intval
> port
->pps_data
.max_volt
* 1000)
4694 ret
= tcpm_pps_set_out_volt(port
, val
->intval
/ 1000);
4696 case POWER_SUPPLY_PROP_CURRENT_NOW
:
4697 if (val
->intval
> port
->pps_data
.max_curr
* 1000)
4700 ret
= tcpm_pps_set_op_curr(port
, val
->intval
/ 1000);
4710 static int tcpm_psy_prop_writeable(struct power_supply
*psy
,
4711 enum power_supply_property psp
)
4714 case POWER_SUPPLY_PROP_ONLINE
:
4715 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
4716 case POWER_SUPPLY_PROP_CURRENT_NOW
:
4723 static enum power_supply_usb_type tcpm_psy_usb_types
[] = {
4724 POWER_SUPPLY_USB_TYPE_C
,
4725 POWER_SUPPLY_USB_TYPE_PD
,
4726 POWER_SUPPLY_USB_TYPE_PD_PPS
,
4729 static const char *tcpm_psy_name_prefix
= "tcpm-source-psy-";
4731 static int devm_tcpm_psy_register(struct tcpm_port
*port
)
4733 struct power_supply_config psy_cfg
= {};
4734 const char *port_dev_name
= dev_name(port
->dev
);
4735 size_t psy_name_len
= strlen(tcpm_psy_name_prefix
) +
4736 strlen(port_dev_name
) + 1;
4739 psy_cfg
.drv_data
= port
;
4740 psy_cfg
.fwnode
= dev_fwnode(port
->dev
);
4741 psy_name
= devm_kzalloc(port
->dev
, psy_name_len
, GFP_KERNEL
);
4745 snprintf(psy_name
, psy_name_len
, "%s%s", tcpm_psy_name_prefix
,
4747 port
->psy_desc
.name
= psy_name
;
4748 port
->psy_desc
.type
= POWER_SUPPLY_TYPE_USB
,
4749 port
->psy_desc
.usb_types
= tcpm_psy_usb_types
;
4750 port
->psy_desc
.num_usb_types
= ARRAY_SIZE(tcpm_psy_usb_types
);
4751 port
->psy_desc
.properties
= tcpm_psy_props
,
4752 port
->psy_desc
.num_properties
= ARRAY_SIZE(tcpm_psy_props
),
4753 port
->psy_desc
.get_property
= tcpm_psy_get_prop
,
4754 port
->psy_desc
.set_property
= tcpm_psy_set_prop
,
4755 port
->psy_desc
.property_is_writeable
= tcpm_psy_prop_writeable
,
4757 port
->usb_type
= POWER_SUPPLY_USB_TYPE_C
;
4759 port
->psy
= devm_power_supply_register(port
->dev
, &port
->psy_desc
,
4762 return PTR_ERR_OR_ZERO(port
->psy
);
4765 static int tcpm_copy_caps(struct tcpm_port
*port
,
4766 const struct tcpc_config
*tcfg
)
4768 if (tcpm_validate_caps(port
, tcfg
->src_pdo
, tcfg
->nr_src_pdo
) ||
4769 tcpm_validate_caps(port
, tcfg
->snk_pdo
, tcfg
->nr_snk_pdo
))
4772 port
->nr_src_pdo
= tcpm_copy_pdos(port
->src_pdo
, tcfg
->src_pdo
,
4774 port
->nr_snk_pdo
= tcpm_copy_pdos(port
->snk_pdo
, tcfg
->snk_pdo
,
4777 port
->nr_snk_vdo
= tcpm_copy_vdos(port
->snk_vdo
, tcfg
->snk_vdo
,
4780 port
->operating_snk_mw
= tcfg
->operating_snk_mw
;
4782 port
->typec_caps
.prefer_role
= tcfg
->default_role
;
4783 port
->typec_caps
.type
= tcfg
->type
;
4784 port
->typec_caps
.data
= tcfg
->data
;
4785 port
->self_powered
= tcfg
->self_powered
;
4790 struct tcpm_port
*tcpm_register_port(struct device
*dev
, struct tcpc_dev
*tcpc
)
4792 struct tcpm_port
*port
;
4795 if (!dev
|| !tcpc
||
4796 !tcpc
->get_vbus
|| !tcpc
->set_cc
|| !tcpc
->get_cc
||
4797 !tcpc
->set_polarity
|| !tcpc
->set_vconn
|| !tcpc
->set_vbus
||
4798 !tcpc
->set_pd_rx
|| !tcpc
->set_roles
|| !tcpc
->pd_transmit
)
4799 return ERR_PTR(-EINVAL
);
4801 port
= devm_kzalloc(dev
, sizeof(*port
), GFP_KERNEL
);
4803 return ERR_PTR(-ENOMEM
);
4808 mutex_init(&port
->lock
);
4809 mutex_init(&port
->swap_lock
);
4811 port
->wq
= create_singlethread_workqueue(dev_name(dev
));
4813 return ERR_PTR(-ENOMEM
);
4814 INIT_DELAYED_WORK(&port
->state_machine
, tcpm_state_machine_work
);
4815 INIT_DELAYED_WORK(&port
->vdm_state_machine
, vdm_state_machine_work
);
4816 INIT_WORK(&port
->event_work
, tcpm_pd_event_handler
);
4818 spin_lock_init(&port
->pd_event_lock
);
4820 init_completion(&port
->tx_complete
);
4821 init_completion(&port
->swap_complete
);
4822 init_completion(&port
->pps_complete
);
4823 tcpm_debugfs_init(port
);
4825 err
= tcpm_fw_get_caps(port
, tcpc
->fwnode
);
4826 if ((err
< 0) && tcpc
->config
)
4827 err
= tcpm_copy_caps(port
, tcpc
->config
);
4829 goto out_destroy_wq
;
4831 if (!tcpc
->config
|| !tcpc
->config
->try_role_hw
)
4832 port
->try_role
= port
->typec_caps
.prefer_role
;
4834 port
->try_role
= TYPEC_NO_PREFERRED_ROLE
;
4836 port
->typec_caps
.fwnode
= tcpc
->fwnode
;
4837 port
->typec_caps
.revision
= 0x0120; /* Type-C spec release 1.2 */
4838 port
->typec_caps
.pd_revision
= 0x0300; /* USB-PD spec release 3.0 */
4839 port
->typec_caps
.dr_set
= tcpm_dr_set
;
4840 port
->typec_caps
.pr_set
= tcpm_pr_set
;
4841 port
->typec_caps
.vconn_set
= tcpm_vconn_set
;
4842 port
->typec_caps
.try_role
= tcpm_try_role
;
4843 port
->typec_caps
.port_type_set
= tcpm_port_type_set
;
4845 port
->partner_desc
.identity
= &port
->partner_ident
;
4846 port
->port_type
= port
->typec_caps
.type
;
4848 port
->role_sw
= usb_role_switch_get(port
->dev
);
4849 if (IS_ERR(port
->role_sw
)) {
4850 err
= PTR_ERR(port
->role_sw
);
4851 goto out_destroy_wq
;
4854 err
= devm_tcpm_psy_register(port
);
4856 goto out_destroy_wq
;
4858 port
->typec_port
= typec_register_port(port
->dev
, &port
->typec_caps
);
4859 if (IS_ERR(port
->typec_port
)) {
4860 err
= PTR_ERR(port
->typec_port
);
4861 goto out_destroy_wq
;
4864 if (tcpc
->config
&& tcpc
->config
->alt_modes
) {
4865 const struct typec_altmode_desc
*paltmode
= tcpc
->config
->alt_modes
;
4868 while (paltmode
->svid
&& i
< ARRAY_SIZE(port
->port_altmode
)) {
4869 struct typec_altmode
*alt
;
4871 alt
= typec_port_register_altmode(port
->typec_port
,
4875 "%s: failed to register port alternate mode 0x%x",
4876 dev_name(dev
), paltmode
->svid
);
4879 typec_altmode_set_drvdata(alt
, port
);
4880 alt
->ops
= &tcpm_altmode_ops
;
4881 port
->port_altmode
[i
] = alt
;
4887 mutex_lock(&port
->lock
);
4889 mutex_unlock(&port
->lock
);
4891 tcpm_log(port
, "%s: registered", dev_name(dev
));
4895 usb_role_switch_put(port
->role_sw
);
4896 destroy_workqueue(port
->wq
);
4897 return ERR_PTR(err
);
4899 EXPORT_SYMBOL_GPL(tcpm_register_port
);
4901 void tcpm_unregister_port(struct tcpm_port
*port
)
4905 tcpm_reset_port(port
);
4906 for (i
= 0; i
< ARRAY_SIZE(port
->port_altmode
); i
++)
4907 typec_unregister_altmode(port
->port_altmode
[i
]);
4908 typec_unregister_port(port
->typec_port
);
4909 usb_role_switch_put(port
->role_sw
);
4910 tcpm_debugfs_exit(port
);
4911 destroy_workqueue(port
->wq
);
4913 EXPORT_SYMBOL_GPL(tcpm_unregister_port
);
4915 MODULE_AUTHOR("Guenter Roeck <groeck@chromium.org>");
4916 MODULE_DESCRIPTION("USB Type-C Port Manager");
4917 MODULE_LICENSE("GPL");