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.h>
30 #include <linux/workqueue.h>
32 #define FOREACH_STATE(S) \
39 S(SRC_SEND_CAPABILITIES), \
40 S(SRC_NEGOTIATE_CAPABILITIES), \
41 S(SRC_TRANSITION_SUPPLY), \
43 S(SRC_WAIT_NEW_CAPABILITIES), \
51 S(SNK_DISCOVERY_DEBOUNCE), \
52 S(SNK_DISCOVERY_DEBOUNCE_DONE), \
53 S(SNK_WAIT_CAPABILITIES), \
54 S(SNK_NEGOTIATE_CAPABILITIES), \
55 S(SNK_NEGOTIATE_PPS_CAPABILITIES), \
56 S(SNK_TRANSITION_SINK), \
57 S(SNK_TRANSITION_SINK_VBUS), \
61 S(DEBUG_ACC_ATTACHED), \
62 S(AUDIO_ACC_ATTACHED), \
63 S(AUDIO_ACC_DEBOUNCE), \
66 S(HARD_RESET_START), \
67 S(SRC_HARD_RESET_VBUS_OFF), \
68 S(SRC_HARD_RESET_VBUS_ON), \
69 S(SNK_HARD_RESET_SINK_OFF), \
70 S(SNK_HARD_RESET_WAIT_VBUS), \
71 S(SNK_HARD_RESET_SINK_ON), \
78 S(DR_SWAP_SEND_TIMEOUT), \
80 S(DR_SWAP_CHANGE_DR), \
84 S(PR_SWAP_SEND_TIMEOUT), \
87 S(PR_SWAP_SRC_SNK_TRANSITION_OFF), \
88 S(PR_SWAP_SRC_SNK_SOURCE_OFF), \
89 S(PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED), \
90 S(PR_SWAP_SRC_SNK_SINK_ON), \
91 S(PR_SWAP_SNK_SRC_SINK_OFF), \
92 S(PR_SWAP_SNK_SRC_SOURCE_ON), \
93 S(PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP), \
95 S(VCONN_SWAP_ACCEPT), \
97 S(VCONN_SWAP_SEND_TIMEOUT), \
98 S(VCONN_SWAP_CANCEL), \
99 S(VCONN_SWAP_START), \
100 S(VCONN_SWAP_WAIT_FOR_VCONN), \
101 S(VCONN_SWAP_TURN_ON_VCONN), \
102 S(VCONN_SWAP_TURN_OFF_VCONN), \
106 S(SNK_TRY_WAIT_DEBOUNCE), \
107 S(SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS), \
109 S(SRC_TRYWAIT_DEBOUNCE), \
110 S(SRC_TRYWAIT_UNATTACHED), \
114 S(SRC_TRY_DEBOUNCE), \
116 S(SNK_TRYWAIT_DEBOUNCE), \
117 S(SNK_TRYWAIT_VBUS), \
120 S(GET_STATUS_SEND), \
121 S(GET_STATUS_SEND_TIMEOUT), \
122 S(GET_PPS_STATUS_SEND), \
123 S(GET_PPS_STATUS_SEND_TIMEOUT), \
127 S(PORT_RESET_WAIT_OFF)
129 #define GENERATE_ENUM(e) e
130 #define GENERATE_STRING(s) #s
133 FOREACH_STATE(GENERATE_ENUM
)
136 static const char * const tcpm_states
[] = {
137 FOREACH_STATE(GENERATE_STRING
)
141 VDM_STATE_ERR_BUSY
= -3,
142 VDM_STATE_ERR_SEND
= -2,
143 VDM_STATE_ERR_TMOUT
= -1,
145 /* Anything >0 represents an active state */
148 VDM_STATE_WAIT_RSP_BUSY
= 3,
151 enum pd_msg_request
{
155 PD_MSG_CTRL_NOT_SUPP
,
156 PD_MSG_DATA_SINK_CAP
,
157 PD_MSG_DATA_SOURCE_CAP
,
160 /* Events from low level driver */
162 #define TCPM_CC_EVENT BIT(0)
163 #define TCPM_VBUS_EVENT BIT(1)
164 #define TCPM_RESET_EVENT BIT(2)
166 #define LOG_BUFFER_ENTRIES 1024
167 #define LOG_BUFFER_ENTRY_SIZE 128
169 /* Alternate mode support */
171 #define SVID_DISCOVERY_MAX 16
173 struct pd_mode_data
{
174 int svid_index
; /* current SVID index */
176 u16 svids
[SVID_DISCOVERY_MAX
];
177 int altmodes
; /* number of alternate modes */
178 struct typec_altmode_desc altmode_desc
[SVID_DISCOVERY_MAX
];
194 struct mutex lock
; /* tcpm state machine lock */
195 struct workqueue_struct
*wq
;
197 struct typec_capability typec_caps
;
198 struct typec_port
*typec_port
;
200 struct tcpc_dev
*tcpc
;
201 struct usb_role_switch
*role_sw
;
203 enum typec_role vconn_role
;
204 enum typec_role pwr_role
;
205 enum typec_data_role data_role
;
206 enum typec_pwr_opmode pwr_opmode
;
208 struct usb_pd_identity partner_ident
;
209 struct typec_partner_desc partner_desc
;
210 struct typec_partner
*partner
;
212 enum typec_cc_status cc_req
;
214 enum typec_cc_status cc1
;
215 enum typec_cc_status cc2
;
216 enum typec_cc_polarity polarity
;
220 enum typec_port_type port_type
;
233 enum pd_msg_request queued_message
;
235 enum tcpm_state enter_state
;
236 enum tcpm_state prev_state
;
237 enum tcpm_state state
;
238 enum tcpm_state delayed_state
;
239 unsigned long delayed_runtime
;
240 unsigned long delay_ms
;
242 spinlock_t pd_event_lock
;
245 struct work_struct event_work
;
246 struct delayed_work state_machine
;
247 struct delayed_work vdm_state_machine
;
248 bool state_machine_running
;
250 struct completion tx_complete
;
251 enum tcpm_transmit_status tx_status
;
253 struct mutex swap_lock
; /* swap command lock */
255 bool non_pd_role_swap
;
256 struct completion swap_complete
;
259 unsigned int negotiated_rev
;
260 unsigned int message_id
;
261 unsigned int caps_count
;
262 unsigned int hard_reset_count
;
264 bool explicit_contract
;
265 unsigned int rx_msgid
;
267 /* Partner capabilities/requests */
269 u32 source_caps
[PDO_MAX_OBJECTS
];
270 unsigned int nr_source_caps
;
271 u32 sink_caps
[PDO_MAX_OBJECTS
];
272 unsigned int nr_sink_caps
;
274 /* Local capabilities */
275 u32 src_pdo
[PDO_MAX_OBJECTS
];
276 unsigned int nr_src_pdo
;
277 u32 snk_pdo
[PDO_MAX_OBJECTS
];
278 unsigned int nr_snk_pdo
;
279 u32 snk_vdo
[VDO_MAX_OBJECTS
];
280 unsigned int nr_snk_vdo
;
282 unsigned int operating_snk_mw
;
283 bool update_sink_caps
;
285 /* Requested current / voltage */
289 /* Used to export TA voltage and current */
290 struct power_supply
*psy
;
291 struct power_supply_desc psy_desc
;
292 enum power_supply_usb_type usb_type
;
296 /* PD state for Vendor Defined Messages */
297 enum vdm_states vdm_state
;
299 /* next Vendor Defined Message to send */
300 u32 vdo_data
[VDO_MAX_SIZE
];
302 /* VDO to retry if UFP responder replied busy */
306 struct pd_pps_data pps_data
;
307 struct completion pps_complete
;
311 /* Alternate mode data */
312 struct pd_mode_data mode_data
;
313 struct typec_altmode
*partner_altmode
[SVID_DISCOVERY_MAX
];
314 struct typec_altmode
*port_altmode
[SVID_DISCOVERY_MAX
];
316 /* Deadline in jiffies to exit src_try_wait state */
317 unsigned long max_wait
;
319 #ifdef CONFIG_DEBUG_FS
320 struct dentry
*dentry
;
321 struct mutex logbuffer_lock
; /* log buffer access lock */
324 u8
*logbuffer
[LOG_BUFFER_ENTRIES
];
329 struct work_struct work
;
330 struct tcpm_port
*port
;
331 struct pd_message msg
;
334 #define tcpm_cc_is_sink(cc) \
335 ((cc) == TYPEC_CC_RP_DEF || (cc) == TYPEC_CC_RP_1_5 || \
336 (cc) == TYPEC_CC_RP_3_0)
338 #define tcpm_port_is_sink(port) \
339 ((tcpm_cc_is_sink((port)->cc1) && !tcpm_cc_is_sink((port)->cc2)) || \
340 (tcpm_cc_is_sink((port)->cc2) && !tcpm_cc_is_sink((port)->cc1)))
342 #define tcpm_cc_is_source(cc) ((cc) == TYPEC_CC_RD)
343 #define tcpm_cc_is_audio(cc) ((cc) == TYPEC_CC_RA)
344 #define tcpm_cc_is_open(cc) ((cc) == TYPEC_CC_OPEN)
346 #define tcpm_port_is_source(port) \
347 ((tcpm_cc_is_source((port)->cc1) && \
348 !tcpm_cc_is_source((port)->cc2)) || \
349 (tcpm_cc_is_source((port)->cc2) && \
350 !tcpm_cc_is_source((port)->cc1)))
352 #define tcpm_port_is_debug(port) \
353 (tcpm_cc_is_source((port)->cc1) && tcpm_cc_is_source((port)->cc2))
355 #define tcpm_port_is_audio(port) \
356 (tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_audio((port)->cc2))
358 #define tcpm_port_is_audio_detached(port) \
359 ((tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_open((port)->cc2)) || \
360 (tcpm_cc_is_audio((port)->cc2) && tcpm_cc_is_open((port)->cc1)))
362 #define tcpm_try_snk(port) \
363 ((port)->try_snk_count == 0 && (port)->try_role == TYPEC_SINK && \
364 (port)->port_type == TYPEC_PORT_DRP)
366 #define tcpm_try_src(port) \
367 ((port)->try_src_count == 0 && (port)->try_role == TYPEC_SOURCE && \
368 (port)->port_type == TYPEC_PORT_DRP)
370 static enum tcpm_state
tcpm_default_state(struct tcpm_port
*port
)
372 if (port
->port_type
== TYPEC_PORT_DRP
) {
373 if (port
->try_role
== TYPEC_SINK
)
374 return SNK_UNATTACHED
;
375 else if (port
->try_role
== TYPEC_SOURCE
)
376 return SRC_UNATTACHED
;
377 else if (port
->tcpc
->config
->default_role
== TYPEC_SINK
)
378 return SNK_UNATTACHED
;
379 /* Fall through to return SRC_UNATTACHED */
380 } else if (port
->port_type
== TYPEC_PORT_SNK
) {
381 return SNK_UNATTACHED
;
383 return SRC_UNATTACHED
;
387 struct tcpm_port
*typec_cap_to_tcpm(const struct typec_capability
*cap
)
389 return container_of(cap
, struct tcpm_port
, typec_caps
);
392 static bool tcpm_port_is_disconnected(struct tcpm_port
*port
)
394 return (!port
->attached
&& port
->cc1
== TYPEC_CC_OPEN
&&
395 port
->cc2
== TYPEC_CC_OPEN
) ||
396 (port
->attached
&& ((port
->polarity
== TYPEC_POLARITY_CC1
&&
397 port
->cc1
== TYPEC_CC_OPEN
) ||
398 (port
->polarity
== TYPEC_POLARITY_CC2
&&
399 port
->cc2
== TYPEC_CC_OPEN
)));
406 #ifdef CONFIG_DEBUG_FS
408 static bool tcpm_log_full(struct tcpm_port
*port
)
410 return port
->logbuffer_tail
==
411 (port
->logbuffer_head
+ 1) % LOG_BUFFER_ENTRIES
;
415 static void _tcpm_log(struct tcpm_port
*port
, const char *fmt
, va_list args
)
417 char tmpbuffer
[LOG_BUFFER_ENTRY_SIZE
];
418 u64 ts_nsec
= local_clock();
419 unsigned long rem_nsec
;
421 mutex_lock(&port
->logbuffer_lock
);
422 if (!port
->logbuffer
[port
->logbuffer_head
]) {
423 port
->logbuffer
[port
->logbuffer_head
] =
424 kzalloc(LOG_BUFFER_ENTRY_SIZE
, GFP_KERNEL
);
425 if (!port
->logbuffer
[port
->logbuffer_head
]) {
426 mutex_unlock(&port
->logbuffer_lock
);
431 vsnprintf(tmpbuffer
, sizeof(tmpbuffer
), fmt
, args
);
433 if (tcpm_log_full(port
)) {
434 port
->logbuffer_head
= max(port
->logbuffer_head
- 1, 0);
435 strcpy(tmpbuffer
, "overflow");
438 if (port
->logbuffer_head
< 0 ||
439 port
->logbuffer_head
>= LOG_BUFFER_ENTRIES
) {
441 "Bad log buffer index %d\n", port
->logbuffer_head
);
445 if (!port
->logbuffer
[port
->logbuffer_head
]) {
447 "Log buffer index %d is NULL\n", port
->logbuffer_head
);
451 rem_nsec
= do_div(ts_nsec
, 1000000000);
452 scnprintf(port
->logbuffer
[port
->logbuffer_head
],
453 LOG_BUFFER_ENTRY_SIZE
, "[%5lu.%06lu] %s",
454 (unsigned long)ts_nsec
, rem_nsec
/ 1000,
456 port
->logbuffer_head
= (port
->logbuffer_head
+ 1) % LOG_BUFFER_ENTRIES
;
459 mutex_unlock(&port
->logbuffer_lock
);
463 static void tcpm_log(struct tcpm_port
*port
, const char *fmt
, ...)
467 /* Do not log while disconnected and unattached */
468 if (tcpm_port_is_disconnected(port
) &&
469 (port
->state
== SRC_UNATTACHED
|| port
->state
== SNK_UNATTACHED
||
470 port
->state
== DRP_TOGGLING
))
474 _tcpm_log(port
, fmt
, args
);
479 static void tcpm_log_force(struct tcpm_port
*port
, const char *fmt
, ...)
484 _tcpm_log(port
, fmt
, args
);
488 static void tcpm_log_source_caps(struct tcpm_port
*port
)
492 for (i
= 0; i
< port
->nr_source_caps
; i
++) {
493 u32 pdo
= port
->source_caps
[i
];
494 enum pd_pdo_type type
= pdo_type(pdo
);
499 scnprintf(msg
, sizeof(msg
),
500 "%u mV, %u mA [%s%s%s%s%s%s]",
501 pdo_fixed_voltage(pdo
),
502 pdo_max_current(pdo
),
503 (pdo
& PDO_FIXED_DUAL_ROLE
) ?
505 (pdo
& PDO_FIXED_SUSPEND
) ?
507 (pdo
& PDO_FIXED_HIGHER_CAP
) ?
509 (pdo
& PDO_FIXED_USB_COMM
) ?
511 (pdo
& PDO_FIXED_DATA_SWAP
) ?
513 (pdo
& PDO_FIXED_EXTPOWER
) ?
517 scnprintf(msg
, sizeof(msg
),
519 pdo_min_voltage(pdo
),
520 pdo_max_voltage(pdo
),
521 pdo_max_current(pdo
));
524 scnprintf(msg
, sizeof(msg
),
526 pdo_min_voltage(pdo
),
527 pdo_max_voltage(pdo
),
531 if (pdo_apdo_type(pdo
) == APDO_TYPE_PPS
)
532 scnprintf(msg
, sizeof(msg
),
534 pdo_pps_apdo_min_voltage(pdo
),
535 pdo_pps_apdo_max_voltage(pdo
),
536 pdo_pps_apdo_max_current(pdo
));
538 strcpy(msg
, "undefined APDO");
541 strcpy(msg
, "undefined");
544 tcpm_log(port
, " PDO %d: type %d, %s",
549 static int tcpm_debug_show(struct seq_file
*s
, void *v
)
551 struct tcpm_port
*port
= (struct tcpm_port
*)s
->private;
554 mutex_lock(&port
->logbuffer_lock
);
555 tail
= port
->logbuffer_tail
;
556 while (tail
!= port
->logbuffer_head
) {
557 seq_printf(s
, "%s\n", port
->logbuffer
[tail
]);
558 tail
= (tail
+ 1) % LOG_BUFFER_ENTRIES
;
560 if (!seq_has_overflowed(s
))
561 port
->logbuffer_tail
= tail
;
562 mutex_unlock(&port
->logbuffer_lock
);
566 DEFINE_SHOW_ATTRIBUTE(tcpm_debug
);
568 static struct dentry
*rootdir
;
570 static void tcpm_debugfs_init(struct tcpm_port
*port
)
572 mutex_init(&port
->logbuffer_lock
);
573 /* /sys/kernel/debug/tcpm/usbcX */
575 rootdir
= debugfs_create_dir("tcpm", NULL
);
577 port
->dentry
= debugfs_create_file(dev_name(port
->dev
),
578 S_IFREG
| 0444, rootdir
,
579 port
, &tcpm_debug_fops
);
582 static void tcpm_debugfs_exit(struct tcpm_port
*port
)
584 debugfs_remove(port
->dentry
);
590 static void tcpm_log(const struct tcpm_port
*port
, const char *fmt
, ...) { }
592 static void tcpm_log_force(struct tcpm_port
*port
, const char *fmt
, ...) { }
593 static void tcpm_log_source_caps(struct tcpm_port
*port
) { }
594 static void tcpm_debugfs_init(const struct tcpm_port
*port
) { }
595 static void tcpm_debugfs_exit(const struct tcpm_port
*port
) { }
599 static int tcpm_pd_transmit(struct tcpm_port
*port
,
600 enum tcpm_transmit_type type
,
601 const struct pd_message
*msg
)
603 unsigned long timeout
;
607 tcpm_log(port
, "PD TX, header: %#x", le16_to_cpu(msg
->header
));
609 tcpm_log(port
, "PD TX, type: %#x", type
);
611 reinit_completion(&port
->tx_complete
);
612 ret
= port
->tcpc
->pd_transmit(port
->tcpc
, type
, msg
);
616 mutex_unlock(&port
->lock
);
617 timeout
= wait_for_completion_timeout(&port
->tx_complete
,
618 msecs_to_jiffies(PD_T_TCPC_TX_TIMEOUT
));
619 mutex_lock(&port
->lock
);
623 switch (port
->tx_status
) {
624 case TCPC_TX_SUCCESS
:
625 port
->message_id
= (port
->message_id
+ 1) & PD_HEADER_ID_MASK
;
627 case TCPC_TX_DISCARDED
:
635 void tcpm_pd_transmit_complete(struct tcpm_port
*port
,
636 enum tcpm_transmit_status status
)
638 tcpm_log(port
, "PD TX complete, status: %u", status
);
639 port
->tx_status
= status
;
640 complete(&port
->tx_complete
);
642 EXPORT_SYMBOL_GPL(tcpm_pd_transmit_complete
);
644 static int tcpm_mux_set(struct tcpm_port
*port
, enum tcpc_mux_mode mode
,
645 enum usb_role usb_role
,
646 enum typec_orientation orientation
)
650 tcpm_log(port
, "Requesting mux mode %d, usb-role %d, orientation %d",
651 mode
, usb_role
, orientation
);
653 ret
= typec_set_orientation(port
->typec_port
, orientation
);
658 ret
= usb_role_switch_set_role(port
->role_sw
, usb_role
);
663 return typec_set_mode(port
->typec_port
, mode
);
666 static int tcpm_set_polarity(struct tcpm_port
*port
,
667 enum typec_cc_polarity polarity
)
671 tcpm_log(port
, "polarity %d", polarity
);
673 ret
= port
->tcpc
->set_polarity(port
->tcpc
, polarity
);
677 port
->polarity
= polarity
;
682 static int tcpm_set_vconn(struct tcpm_port
*port
, bool enable
)
686 tcpm_log(port
, "vconn:=%d", enable
);
688 ret
= port
->tcpc
->set_vconn(port
->tcpc
, enable
);
690 port
->vconn_role
= enable
? TYPEC_SOURCE
: TYPEC_SINK
;
691 typec_set_vconn_role(port
->typec_port
, port
->vconn_role
);
697 static u32
tcpm_get_current_limit(struct tcpm_port
*port
)
699 enum typec_cc_status cc
;
702 cc
= port
->polarity
? port
->cc2
: port
->cc1
;
704 case TYPEC_CC_RP_1_5
:
707 case TYPEC_CC_RP_3_0
:
710 case TYPEC_CC_RP_DEF
:
712 if (port
->tcpc
->get_current_limit
)
713 limit
= port
->tcpc
->get_current_limit(port
->tcpc
);
722 static int tcpm_set_current_limit(struct tcpm_port
*port
, u32 max_ma
, u32 mv
)
724 int ret
= -EOPNOTSUPP
;
726 tcpm_log(port
, "Setting voltage/current limit %u mV %u mA", mv
, max_ma
);
728 port
->supply_voltage
= mv
;
729 port
->current_limit
= max_ma
;
731 if (port
->tcpc
->set_current_limit
)
732 ret
= port
->tcpc
->set_current_limit(port
->tcpc
, max_ma
, mv
);
738 * Determine RP value to set based on maximum current supported
739 * by a port if configured as source.
740 * Returns CC value to report to link partner.
742 static enum typec_cc_status
tcpm_rp_cc(struct tcpm_port
*port
)
744 const u32
*src_pdo
= port
->src_pdo
;
745 int nr_pdo
= port
->nr_src_pdo
;
749 * Search for first entry with matching voltage.
750 * It should report the maximum supported current.
752 for (i
= 0; i
< nr_pdo
; i
++) {
753 const u32 pdo
= src_pdo
[i
];
755 if (pdo_type(pdo
) == PDO_TYPE_FIXED
&&
756 pdo_fixed_voltage(pdo
) == 5000) {
757 unsigned int curr
= pdo_max_current(pdo
);
760 return TYPEC_CC_RP_3_0
;
761 else if (curr
>= 1500)
762 return TYPEC_CC_RP_1_5
;
763 return TYPEC_CC_RP_DEF
;
767 return TYPEC_CC_RP_DEF
;
770 static int tcpm_set_attached_state(struct tcpm_port
*port
, bool attached
)
772 return port
->tcpc
->set_roles(port
->tcpc
, attached
, port
->pwr_role
,
776 static int tcpm_set_roles(struct tcpm_port
*port
, bool attached
,
777 enum typec_role role
, enum typec_data_role data
)
779 enum typec_orientation orientation
;
780 enum usb_role usb_role
;
783 if (port
->polarity
== TYPEC_POLARITY_CC1
)
784 orientation
= TYPEC_ORIENTATION_NORMAL
;
786 orientation
= TYPEC_ORIENTATION_REVERSE
;
788 if (data
== TYPEC_HOST
)
789 usb_role
= USB_ROLE_HOST
;
791 usb_role
= USB_ROLE_DEVICE
;
793 ret
= tcpm_mux_set(port
, TYPEC_MUX_USB
, usb_role
, orientation
);
797 ret
= port
->tcpc
->set_roles(port
->tcpc
, attached
, role
, data
);
801 port
->pwr_role
= role
;
802 port
->data_role
= data
;
803 typec_set_data_role(port
->typec_port
, data
);
804 typec_set_pwr_role(port
->typec_port
, role
);
809 static int tcpm_set_pwr_role(struct tcpm_port
*port
, enum typec_role role
)
813 ret
= port
->tcpc
->set_roles(port
->tcpc
, true, role
,
818 port
->pwr_role
= role
;
819 typec_set_pwr_role(port
->typec_port
, role
);
824 static int tcpm_pd_send_source_caps(struct tcpm_port
*port
)
826 struct pd_message msg
;
829 memset(&msg
, 0, sizeof(msg
));
830 if (!port
->nr_src_pdo
) {
831 /* No source capabilities defined, sink only */
832 msg
.header
= PD_HEADER_LE(PD_CTRL_REJECT
,
835 port
->negotiated_rev
,
836 port
->message_id
, 0);
838 msg
.header
= PD_HEADER_LE(PD_DATA_SOURCE_CAP
,
841 port
->negotiated_rev
,
845 for (i
= 0; i
< port
->nr_src_pdo
; i
++)
846 msg
.payload
[i
] = cpu_to_le32(port
->src_pdo
[i
]);
848 return tcpm_pd_transmit(port
, TCPC_TX_SOP
, &msg
);
851 static int tcpm_pd_send_sink_caps(struct tcpm_port
*port
)
853 struct pd_message msg
;
856 memset(&msg
, 0, sizeof(msg
));
857 if (!port
->nr_snk_pdo
) {
858 /* No sink capabilities defined, source only */
859 msg
.header
= PD_HEADER_LE(PD_CTRL_REJECT
,
862 port
->negotiated_rev
,
863 port
->message_id
, 0);
865 msg
.header
= PD_HEADER_LE(PD_DATA_SINK_CAP
,
868 port
->negotiated_rev
,
872 for (i
= 0; i
< port
->nr_snk_pdo
; i
++)
873 msg
.payload
[i
] = cpu_to_le32(port
->snk_pdo
[i
]);
875 return tcpm_pd_transmit(port
, TCPC_TX_SOP
, &msg
);
878 static void tcpm_set_state(struct tcpm_port
*port
, enum tcpm_state state
,
879 unsigned int delay_ms
)
882 tcpm_log(port
, "pending state change %s -> %s @ %u ms",
883 tcpm_states
[port
->state
], tcpm_states
[state
],
885 port
->delayed_state
= state
;
886 mod_delayed_work(port
->wq
, &port
->state_machine
,
887 msecs_to_jiffies(delay_ms
));
888 port
->delayed_runtime
= jiffies
+ msecs_to_jiffies(delay_ms
);
889 port
->delay_ms
= delay_ms
;
891 tcpm_log(port
, "state change %s -> %s",
892 tcpm_states
[port
->state
], tcpm_states
[state
]);
893 port
->delayed_state
= INVALID_STATE
;
894 port
->prev_state
= port
->state
;
897 * Don't re-queue the state machine work item if we're currently
898 * in the state machine and we're immediately changing states.
899 * tcpm_state_machine_work() will continue running the state
902 if (!port
->state_machine_running
)
903 mod_delayed_work(port
->wq
, &port
->state_machine
, 0);
907 static void tcpm_set_state_cond(struct tcpm_port
*port
, enum tcpm_state state
,
908 unsigned int delay_ms
)
910 if (port
->enter_state
== port
->state
)
911 tcpm_set_state(port
, state
, delay_ms
);
914 "skipped %sstate change %s -> %s [%u ms], context state %s",
915 delay_ms
? "delayed " : "",
916 tcpm_states
[port
->state
], tcpm_states
[state
],
917 delay_ms
, tcpm_states
[port
->enter_state
]);
920 static void tcpm_queue_message(struct tcpm_port
*port
,
921 enum pd_msg_request message
)
923 port
->queued_message
= message
;
924 mod_delayed_work(port
->wq
, &port
->state_machine
, 0);
928 * VDM/VDO handling functions
930 static void tcpm_queue_vdm(struct tcpm_port
*port
, const u32 header
,
931 const u32
*data
, int cnt
)
933 port
->vdo_count
= cnt
+ 1;
934 port
->vdo_data
[0] = header
;
935 memcpy(&port
->vdo_data
[1], data
, sizeof(u32
) * cnt
);
936 /* Set ready, vdm state machine will actually send */
937 port
->vdm_retries
= 0;
938 port
->vdm_state
= VDM_STATE_READY
;
941 static void svdm_consume_identity(struct tcpm_port
*port
, const __le32
*payload
,
944 u32 vdo
= le32_to_cpu(payload
[VDO_INDEX_IDH
]);
945 u32 product
= le32_to_cpu(payload
[VDO_INDEX_PRODUCT
]);
947 memset(&port
->mode_data
, 0, sizeof(port
->mode_data
));
949 port
->partner_ident
.id_header
= vdo
;
950 port
->partner_ident
.cert_stat
= le32_to_cpu(payload
[VDO_INDEX_CSTAT
]);
951 port
->partner_ident
.product
= product
;
953 typec_partner_set_identity(port
->partner
);
955 tcpm_log(port
, "Identity: %04x:%04x.%04x",
957 PD_PRODUCT_PID(product
), product
& 0xffff);
960 static bool svdm_consume_svids(struct tcpm_port
*port
, const __le32
*payload
,
963 struct pd_mode_data
*pmdata
= &port
->mode_data
;
966 for (i
= 1; i
< cnt
; i
++) {
967 u32 p
= le32_to_cpu(payload
[i
]);
970 svid
= (p
>> 16) & 0xffff;
974 if (pmdata
->nsvids
>= SVID_DISCOVERY_MAX
)
977 pmdata
->svids
[pmdata
->nsvids
++] = svid
;
978 tcpm_log(port
, "SVID %d: 0x%x", pmdata
->nsvids
, svid
);
984 if (pmdata
->nsvids
>= SVID_DISCOVERY_MAX
)
987 pmdata
->svids
[pmdata
->nsvids
++] = svid
;
988 tcpm_log(port
, "SVID %d: 0x%x", pmdata
->nsvids
, svid
);
992 tcpm_log(port
, "SVID_DISCOVERY_MAX(%d) too low!", SVID_DISCOVERY_MAX
);
996 static void svdm_consume_modes(struct tcpm_port
*port
, const __le32
*payload
,
999 struct pd_mode_data
*pmdata
= &port
->mode_data
;
1000 struct typec_altmode_desc
*paltmode
;
1001 struct typec_mode_desc
*pmode
;
1004 if (pmdata
->altmodes
>= ARRAY_SIZE(port
->partner_altmode
)) {
1005 /* Already logged in svdm_consume_svids() */
1009 paltmode
= &pmdata
->altmode_desc
[pmdata
->altmodes
];
1010 memset(paltmode
, 0, sizeof(*paltmode
));
1012 paltmode
->svid
= pmdata
->svids
[pmdata
->svid_index
];
1014 tcpm_log(port
, " Alternate mode %d: SVID 0x%04x",
1015 pmdata
->altmodes
, paltmode
->svid
);
1017 for (i
= 1; i
< cnt
&& paltmode
->n_modes
< ALTMODE_MAX_MODES
; i
++) {
1018 pmode
= &paltmode
->modes
[paltmode
->n_modes
];
1019 memset(pmode
, 0, sizeof(*pmode
));
1020 pmode
->vdo
= le32_to_cpu(payload
[i
]);
1021 pmode
->index
= i
- 1;
1022 paltmode
->n_modes
++;
1023 tcpm_log(port
, " VDO %d: 0x%08x",
1024 pmode
->index
, pmode
->vdo
);
1026 port
->partner_altmode
[pmdata
->altmodes
] =
1027 typec_partner_register_altmode(port
->partner
, paltmode
);
1028 if (!port
->partner_altmode
[pmdata
->altmodes
]) {
1030 "Failed to register alternate modes for SVID 0x%04x",
1037 #define supports_modal(port) PD_IDH_MODAL_SUPP((port)->partner_ident.id_header)
1039 static int tcpm_pd_svdm(struct tcpm_port
*port
, const __le32
*payload
, int cnt
,
1042 u32 p0
= le32_to_cpu(payload
[0]);
1043 int cmd_type
= PD_VDO_CMDT(p0
);
1044 int cmd
= PD_VDO_CMD(p0
);
1045 struct pd_mode_data
*modep
;
1050 tcpm_log(port
, "Rx VDM cmd 0x%x type %d cmd %d len %d",
1051 p0
, cmd_type
, cmd
, cnt
);
1053 modep
= &port
->mode_data
;
1058 case CMD_DISCOVER_IDENT
:
1059 /* 6.4.4.3.1: Only respond as UFP (device) */
1060 if (port
->data_role
== TYPEC_DEVICE
&&
1062 for (i
= 0; i
< port
->nr_snk_vdo
; i
++)
1063 response
[i
+ 1] = port
->snk_vdo
[i
];
1064 rlen
= port
->nr_snk_vdo
+ 1;
1067 case CMD_DISCOVER_SVID
:
1069 case CMD_DISCOVER_MODES
:
1071 case CMD_ENTER_MODE
:
1081 response
[0] = p0
| VDO_CMDT(CMDT_RSP_ACK
);
1082 } else if (rlen
== 0) {
1083 response
[0] = p0
| VDO_CMDT(CMDT_RSP_NAK
);
1086 response
[0] = p0
| VDO_CMDT(CMDT_RSP_BUSY
);
1091 /* silently drop message if we are not connected */
1092 if (IS_ERR_OR_NULL(port
->partner
))
1096 case CMD_DISCOVER_IDENT
:
1098 svdm_consume_identity(port
, payload
, cnt
);
1099 response
[0] = VDO(USB_SID_PD
, 1, CMD_DISCOVER_SVID
);
1102 case CMD_DISCOVER_SVID
:
1104 if (svdm_consume_svids(port
, payload
, cnt
)) {
1105 response
[0] = VDO(USB_SID_PD
, 1,
1108 } else if (modep
->nsvids
&& supports_modal(port
)) {
1109 response
[0] = VDO(modep
->svids
[0], 1,
1110 CMD_DISCOVER_MODES
);
1114 case CMD_DISCOVER_MODES
:
1116 svdm_consume_modes(port
, payload
, cnt
);
1117 modep
->svid_index
++;
1118 if (modep
->svid_index
< modep
->nsvids
) {
1119 svid
= modep
->svids
[modep
->svid_index
];
1120 response
[0] = VDO(svid
, 1, CMD_DISCOVER_MODES
);
1123 /* enter alternate mode if/when implemented */
1126 case CMD_ENTER_MODE
:
1139 static void tcpm_handle_vdm_request(struct tcpm_port
*port
,
1140 const __le32
*payload
, int cnt
)
1143 u32 response
[8] = { };
1144 u32 p0
= le32_to_cpu(payload
[0]);
1146 if (port
->vdm_state
== VDM_STATE_BUSY
) {
1147 /* If UFP responded busy retry after timeout */
1148 if (PD_VDO_CMDT(p0
) == CMDT_RSP_BUSY
) {
1149 port
->vdm_state
= VDM_STATE_WAIT_RSP_BUSY
;
1150 port
->vdo_retry
= (p0
& ~VDO_CMDT_MASK
) |
1152 mod_delayed_work(port
->wq
, &port
->vdm_state_machine
,
1153 msecs_to_jiffies(PD_T_VDM_BUSY
));
1156 port
->vdm_state
= VDM_STATE_DONE
;
1159 if (PD_VDO_SVDM(p0
))
1160 rlen
= tcpm_pd_svdm(port
, payload
, cnt
, response
);
1163 tcpm_queue_vdm(port
, response
[0], &response
[1], rlen
- 1);
1164 mod_delayed_work(port
->wq
, &port
->vdm_state_machine
, 0);
1168 static void tcpm_send_vdm(struct tcpm_port
*port
, u32 vid
, int cmd
,
1169 const u32
*data
, int count
)
1173 if (WARN_ON(count
> VDO_MAX_SIZE
- 1))
1174 count
= VDO_MAX_SIZE
- 1;
1176 /* set VDM header with VID & CMD */
1177 header
= VDO(vid
, ((vid
& USB_SID_PD
) == USB_SID_PD
) ?
1178 1 : (PD_VDO_CMD(cmd
) <= CMD_ATTENTION
), cmd
);
1179 tcpm_queue_vdm(port
, header
, data
, count
);
1181 mod_delayed_work(port
->wq
, &port
->vdm_state_machine
, 0);
1184 static unsigned int vdm_ready_timeout(u32 vdm_hdr
)
1186 unsigned int timeout
;
1187 int cmd
= PD_VDO_CMD(vdm_hdr
);
1189 /* its not a structured VDM command */
1190 if (!PD_VDO_SVDM(vdm_hdr
))
1191 return PD_T_VDM_UNSTRUCTURED
;
1193 switch (PD_VDO_CMDT(vdm_hdr
)) {
1195 if (cmd
== CMD_ENTER_MODE
|| cmd
== CMD_EXIT_MODE
)
1196 timeout
= PD_T_VDM_WAIT_MODE_E
;
1198 timeout
= PD_T_VDM_SNDR_RSP
;
1201 if (cmd
== CMD_ENTER_MODE
|| cmd
== CMD_EXIT_MODE
)
1202 timeout
= PD_T_VDM_E_MODE
;
1204 timeout
= PD_T_VDM_RCVR_RSP
;
1210 static void vdm_run_state_machine(struct tcpm_port
*port
)
1212 struct pd_message msg
;
1215 switch (port
->vdm_state
) {
1216 case VDM_STATE_READY
:
1217 /* Only transmit VDM if attached */
1218 if (!port
->attached
) {
1219 port
->vdm_state
= VDM_STATE_ERR_BUSY
;
1224 * if there's traffic or we're not in PDO ready state don't send
1227 if (port
->state
!= SRC_READY
&& port
->state
!= SNK_READY
)
1230 /* Prepare and send VDM */
1231 memset(&msg
, 0, sizeof(msg
));
1232 msg
.header
= PD_HEADER_LE(PD_DATA_VENDOR_DEF
,
1235 port
->negotiated_rev
,
1236 port
->message_id
, port
->vdo_count
);
1237 for (i
= 0; i
< port
->vdo_count
; i
++)
1238 msg
.payload
[i
] = cpu_to_le32(port
->vdo_data
[i
]);
1239 res
= tcpm_pd_transmit(port
, TCPC_TX_SOP
, &msg
);
1241 port
->vdm_state
= VDM_STATE_ERR_SEND
;
1243 unsigned long timeout
;
1245 port
->vdm_retries
= 0;
1246 port
->vdm_state
= VDM_STATE_BUSY
;
1247 timeout
= vdm_ready_timeout(port
->vdo_data
[0]);
1248 mod_delayed_work(port
->wq
, &port
->vdm_state_machine
,
1252 case VDM_STATE_WAIT_RSP_BUSY
:
1253 port
->vdo_data
[0] = port
->vdo_retry
;
1254 port
->vdo_count
= 1;
1255 port
->vdm_state
= VDM_STATE_READY
;
1257 case VDM_STATE_BUSY
:
1258 port
->vdm_state
= VDM_STATE_ERR_TMOUT
;
1260 case VDM_STATE_ERR_SEND
:
1262 * A partner which does not support USB PD will not reply,
1263 * so this is not a fatal error. At the same time, some
1264 * devices may not return GoodCRC under some circumstances,
1265 * so we need to retry.
1267 if (port
->vdm_retries
< 3) {
1268 tcpm_log(port
, "VDM Tx error, retry");
1269 port
->vdm_retries
++;
1270 port
->vdm_state
= VDM_STATE_READY
;
1278 static void vdm_state_machine_work(struct work_struct
*work
)
1280 struct tcpm_port
*port
= container_of(work
, struct tcpm_port
,
1281 vdm_state_machine
.work
);
1282 enum vdm_states prev_state
;
1284 mutex_lock(&port
->lock
);
1287 * Continue running as long as the port is not busy and there was
1291 prev_state
= port
->vdm_state
;
1292 vdm_run_state_machine(port
);
1293 } while (port
->vdm_state
!= prev_state
&&
1294 port
->vdm_state
!= VDM_STATE_BUSY
);
1296 mutex_unlock(&port
->lock
);
1302 PDO_ERR_VSAFE5V_NOT_FIRST
,
1303 PDO_ERR_PDO_TYPE_NOT_IN_ORDER
,
1304 PDO_ERR_FIXED_NOT_SORTED
,
1305 PDO_ERR_VARIABLE_BATT_NOT_SORTED
,
1307 PDO_ERR_PPS_APDO_NOT_SORTED
,
1308 PDO_ERR_DUPE_PPS_APDO
,
1311 static const char * const pdo_err_msg
[] = {
1312 [PDO_ERR_NO_VSAFE5V
] =
1313 " err: source/sink caps should atleast have vSafe5V",
1314 [PDO_ERR_VSAFE5V_NOT_FIRST
] =
1315 " err: vSafe5V Fixed Supply Object Shall always be the first object",
1316 [PDO_ERR_PDO_TYPE_NOT_IN_ORDER
] =
1317 " err: PDOs should be in the following order: Fixed; Battery; Variable",
1318 [PDO_ERR_FIXED_NOT_SORTED
] =
1319 " err: Fixed supply pdos should be in increasing order of their fixed voltage",
1320 [PDO_ERR_VARIABLE_BATT_NOT_SORTED
] =
1321 " err: Variable/Battery supply pdos should be in increasing order of their minimum voltage",
1322 [PDO_ERR_DUPE_PDO
] =
1323 " err: Variable/Batt supply pdos cannot have same min/max voltage",
1324 [PDO_ERR_PPS_APDO_NOT_SORTED
] =
1325 " err: Programmable power supply apdos should be in increasing order of their maximum voltage",
1326 [PDO_ERR_DUPE_PPS_APDO
] =
1327 " err: Programmable power supply apdos cannot have same min/max voltage and max current",
1330 static enum pdo_err
tcpm_caps_err(struct tcpm_port
*port
, const u32
*pdo
,
1331 unsigned int nr_pdo
)
1335 /* Should at least contain vSafe5v */
1337 return PDO_ERR_NO_VSAFE5V
;
1339 /* The vSafe5V Fixed Supply Object Shall always be the first object */
1340 if (pdo_type(pdo
[0]) != PDO_TYPE_FIXED
||
1341 pdo_fixed_voltage(pdo
[0]) != VSAFE5V
)
1342 return PDO_ERR_VSAFE5V_NOT_FIRST
;
1344 for (i
= 1; i
< nr_pdo
; i
++) {
1345 if (pdo_type(pdo
[i
]) < pdo_type(pdo
[i
- 1])) {
1346 return PDO_ERR_PDO_TYPE_NOT_IN_ORDER
;
1347 } else if (pdo_type(pdo
[i
]) == pdo_type(pdo
[i
- 1])) {
1348 enum pd_pdo_type type
= pdo_type(pdo
[i
]);
1352 * The remaining Fixed Supply Objects, if
1353 * present, shall be sent in voltage order;
1354 * lowest to highest.
1356 case PDO_TYPE_FIXED
:
1357 if (pdo_fixed_voltage(pdo
[i
]) <=
1358 pdo_fixed_voltage(pdo
[i
- 1]))
1359 return PDO_ERR_FIXED_NOT_SORTED
;
1362 * The Battery Supply Objects and Variable
1363 * supply, if present shall be sent in Minimum
1364 * Voltage order; lowest to highest.
1368 if (pdo_min_voltage(pdo
[i
]) <
1369 pdo_min_voltage(pdo
[i
- 1]))
1370 return PDO_ERR_VARIABLE_BATT_NOT_SORTED
;
1371 else if ((pdo_min_voltage(pdo
[i
]) ==
1372 pdo_min_voltage(pdo
[i
- 1])) &&
1373 (pdo_max_voltage(pdo
[i
]) ==
1374 pdo_min_voltage(pdo
[i
- 1])))
1375 return PDO_ERR_DUPE_PDO
;
1378 * The Programmable Power Supply APDOs, if present,
1379 * shall be sent in Maximum Voltage order;
1380 * lowest to highest.
1383 if (pdo_apdo_type(pdo
[i
]) != APDO_TYPE_PPS
)
1386 if (pdo_pps_apdo_max_current(pdo
[i
]) <
1387 pdo_pps_apdo_max_current(pdo
[i
- 1]))
1388 return PDO_ERR_PPS_APDO_NOT_SORTED
;
1389 else if (pdo_pps_apdo_min_voltage(pdo
[i
]) ==
1390 pdo_pps_apdo_min_voltage(pdo
[i
- 1]) &&
1391 pdo_pps_apdo_max_voltage(pdo
[i
]) ==
1392 pdo_pps_apdo_max_voltage(pdo
[i
- 1]) &&
1393 pdo_pps_apdo_max_current(pdo
[i
]) ==
1394 pdo_pps_apdo_max_current(pdo
[i
- 1]))
1395 return PDO_ERR_DUPE_PPS_APDO
;
1398 tcpm_log_force(port
, " Unknown pdo type");
1406 static int tcpm_validate_caps(struct tcpm_port
*port
, const u32
*pdo
,
1407 unsigned int nr_pdo
)
1409 enum pdo_err err_index
= tcpm_caps_err(port
, pdo
, nr_pdo
);
1411 if (err_index
!= PDO_NO_ERR
) {
1412 tcpm_log_force(port
, " %s", pdo_err_msg
[err_index
]);
1420 * PD (data, control) command handling functions
1422 static inline enum tcpm_state
ready_state(struct tcpm_port
*port
)
1424 if (port
->pwr_role
== TYPEC_SOURCE
)
1430 static int tcpm_pd_send_control(struct tcpm_port
*port
,
1431 enum pd_ctrl_msg_type type
);
1433 static void tcpm_handle_alert(struct tcpm_port
*port
, const __le32
*payload
,
1436 u32 p0
= le32_to_cpu(payload
[0]);
1437 unsigned int type
= usb_pd_ado_type(p0
);
1440 tcpm_log(port
, "Alert message received with no type");
1444 /* Just handling non-battery alerts for now */
1445 if (!(type
& USB_PD_ADO_TYPE_BATT_STATUS_CHANGE
)) {
1446 switch (port
->state
) {
1449 tcpm_set_state(port
, GET_STATUS_SEND
, 0);
1452 tcpm_queue_message(port
, PD_MSG_CTRL_WAIT
);
1458 static void tcpm_pd_data_request(struct tcpm_port
*port
,
1459 const struct pd_message
*msg
)
1461 enum pd_data_msg_type type
= pd_header_type_le(msg
->header
);
1462 unsigned int cnt
= pd_header_cnt_le(msg
->header
);
1463 unsigned int rev
= pd_header_rev_le(msg
->header
);
1467 case PD_DATA_SOURCE_CAP
:
1468 if (port
->pwr_role
!= TYPEC_SINK
)
1471 for (i
= 0; i
< cnt
; i
++)
1472 port
->source_caps
[i
] = le32_to_cpu(msg
->payload
[i
]);
1474 port
->nr_source_caps
= cnt
;
1476 tcpm_log_source_caps(port
);
1478 tcpm_validate_caps(port
, port
->source_caps
,
1479 port
->nr_source_caps
);
1482 * Adjust revision in subsequent message headers, as required,
1483 * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't
1484 * support Rev 1.0 so just do nothing in that scenario.
1486 if (rev
== PD_REV10
)
1489 if (rev
< PD_MAX_REV
)
1490 port
->negotiated_rev
= rev
;
1493 * This message may be received even if VBUS is not
1494 * present. This is quite unexpected; see USB PD
1495 * specification, sections 8.3.3.6.3.1 and 8.3.3.6.3.2.
1496 * However, at the same time, we must be ready to
1497 * receive this message and respond to it 15ms after
1498 * receiving PS_RDY during power swap operations, no matter
1499 * if VBUS is available or not (USB PD specification,
1501 * So we need to accept the message either way,
1502 * but be prepared to keep waiting for VBUS after it was
1505 tcpm_set_state(port
, SNK_NEGOTIATE_CAPABILITIES
, 0);
1507 case PD_DATA_REQUEST
:
1508 if (port
->pwr_role
!= TYPEC_SOURCE
||
1510 tcpm_queue_message(port
, PD_MSG_CTRL_REJECT
);
1515 * Adjust revision in subsequent message headers, as required,
1516 * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't
1517 * support Rev 1.0 so just reject in that scenario.
1519 if (rev
== PD_REV10
) {
1520 tcpm_queue_message(port
, PD_MSG_CTRL_REJECT
);
1524 if (rev
< PD_MAX_REV
)
1525 port
->negotiated_rev
= rev
;
1527 port
->sink_request
= le32_to_cpu(msg
->payload
[0]);
1528 tcpm_set_state(port
, SRC_NEGOTIATE_CAPABILITIES
, 0);
1530 case PD_DATA_SINK_CAP
:
1531 /* We don't do anything with this at the moment... */
1532 for (i
= 0; i
< cnt
; i
++)
1533 port
->sink_caps
[i
] = le32_to_cpu(msg
->payload
[i
]);
1534 port
->nr_sink_caps
= cnt
;
1536 case PD_DATA_VENDOR_DEF
:
1537 tcpm_handle_vdm_request(port
, msg
->payload
, cnt
);
1540 if (port
->state
== SRC_READY
|| port
->state
== SNK_READY
) {
1541 port
->bist_request
= le32_to_cpu(msg
->payload
[0]);
1542 tcpm_set_state(port
, BIST_RX
, 0);
1546 tcpm_handle_alert(port
, msg
->payload
, cnt
);
1548 case PD_DATA_BATT_STATUS
:
1549 case PD_DATA_GET_COUNTRY_INFO
:
1550 /* Currently unsupported */
1551 tcpm_queue_message(port
, PD_MSG_CTRL_NOT_SUPP
);
1554 tcpm_log(port
, "Unhandled data message type %#x", type
);
1559 static void tcpm_pps_complete(struct tcpm_port
*port
, int result
)
1561 if (port
->pps_pending
) {
1562 port
->pps_status
= result
;
1563 port
->pps_pending
= false;
1564 complete(&port
->pps_complete
);
1568 static void tcpm_pd_ctrl_request(struct tcpm_port
*port
,
1569 const struct pd_message
*msg
)
1571 enum pd_ctrl_msg_type type
= pd_header_type_le(msg
->header
);
1572 enum tcpm_state next_state
;
1575 case PD_CTRL_GOOD_CRC
:
1578 case PD_CTRL_GET_SOURCE_CAP
:
1579 switch (port
->state
) {
1582 tcpm_queue_message(port
, PD_MSG_DATA_SOURCE_CAP
);
1585 tcpm_queue_message(port
, PD_MSG_CTRL_REJECT
);
1589 case PD_CTRL_GET_SINK_CAP
:
1590 switch (port
->state
) {
1593 tcpm_queue_message(port
, PD_MSG_DATA_SINK_CAP
);
1596 tcpm_queue_message(port
, PD_MSG_CTRL_REJECT
);
1600 case PD_CTRL_GOTO_MIN
:
1602 case PD_CTRL_PS_RDY
:
1603 switch (port
->state
) {
1604 case SNK_TRANSITION_SINK
:
1605 if (port
->vbus_present
) {
1606 tcpm_set_current_limit(port
,
1607 port
->current_limit
,
1608 port
->supply_voltage
);
1609 port
->explicit_contract
= true;
1610 tcpm_set_state(port
, SNK_READY
, 0);
1613 * Seen after power swap. Keep waiting for VBUS
1614 * in a transitional state.
1616 tcpm_set_state(port
,
1617 SNK_TRANSITION_SINK_VBUS
, 0);
1620 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED
:
1621 tcpm_set_state(port
, PR_SWAP_SRC_SNK_SINK_ON
, 0);
1623 case PR_SWAP_SNK_SRC_SINK_OFF
:
1624 tcpm_set_state(port
, PR_SWAP_SNK_SRC_SOURCE_ON
, 0);
1626 case VCONN_SWAP_WAIT_FOR_VCONN
:
1627 tcpm_set_state(port
, VCONN_SWAP_TURN_OFF_VCONN
, 0);
1633 case PD_CTRL_REJECT
:
1635 case PD_CTRL_NOT_SUPP
:
1636 switch (port
->state
) {
1637 case SNK_NEGOTIATE_CAPABILITIES
:
1638 /* USB PD specification, Figure 8-43 */
1639 if (port
->explicit_contract
)
1640 next_state
= SNK_READY
;
1642 next_state
= SNK_WAIT_CAPABILITIES
;
1643 tcpm_set_state(port
, next_state
, 0);
1645 case SNK_NEGOTIATE_PPS_CAPABILITIES
:
1646 /* Revert data back from any requested PPS updates */
1647 port
->pps_data
.out_volt
= port
->supply_voltage
;
1648 port
->pps_data
.op_curr
= port
->current_limit
;
1649 port
->pps_status
= (type
== PD_CTRL_WAIT
?
1650 -EAGAIN
: -EOPNOTSUPP
);
1651 tcpm_set_state(port
, SNK_READY
, 0);
1654 port
->swap_status
= (type
== PD_CTRL_WAIT
?
1655 -EAGAIN
: -EOPNOTSUPP
);
1656 tcpm_set_state(port
, DR_SWAP_CANCEL
, 0);
1659 port
->swap_status
= (type
== PD_CTRL_WAIT
?
1660 -EAGAIN
: -EOPNOTSUPP
);
1661 tcpm_set_state(port
, PR_SWAP_CANCEL
, 0);
1663 case VCONN_SWAP_SEND
:
1664 port
->swap_status
= (type
== PD_CTRL_WAIT
?
1665 -EAGAIN
: -EOPNOTSUPP
);
1666 tcpm_set_state(port
, VCONN_SWAP_CANCEL
, 0);
1672 case PD_CTRL_ACCEPT
:
1673 switch (port
->state
) {
1674 case SNK_NEGOTIATE_CAPABILITIES
:
1675 port
->pps_data
.active
= false;
1676 tcpm_set_state(port
, SNK_TRANSITION_SINK
, 0);
1678 case SNK_NEGOTIATE_PPS_CAPABILITIES
:
1679 port
->pps_data
.active
= true;
1680 port
->supply_voltage
= port
->pps_data
.out_volt
;
1681 port
->current_limit
= port
->pps_data
.op_curr
;
1682 tcpm_set_state(port
, SNK_TRANSITION_SINK
, 0);
1684 case SOFT_RESET_SEND
:
1685 port
->message_id
= 0;
1686 port
->rx_msgid
= -1;
1687 if (port
->pwr_role
== TYPEC_SOURCE
)
1688 next_state
= SRC_SEND_CAPABILITIES
;
1690 next_state
= SNK_WAIT_CAPABILITIES
;
1691 tcpm_set_state(port
, next_state
, 0);
1694 tcpm_set_state(port
, DR_SWAP_CHANGE_DR
, 0);
1697 tcpm_set_state(port
, PR_SWAP_START
, 0);
1699 case VCONN_SWAP_SEND
:
1700 tcpm_set_state(port
, VCONN_SWAP_START
, 0);
1706 case PD_CTRL_SOFT_RESET
:
1707 tcpm_set_state(port
, SOFT_RESET
, 0);
1709 case PD_CTRL_DR_SWAP
:
1710 if (port
->port_type
!= TYPEC_PORT_DRP
) {
1711 tcpm_queue_message(port
, PD_MSG_CTRL_REJECT
);
1716 * 6.3.9: If an alternate mode is active, a request to swap
1717 * alternate modes shall trigger a port reset.
1719 switch (port
->state
) {
1722 tcpm_set_state(port
, DR_SWAP_ACCEPT
, 0);
1725 tcpm_queue_message(port
, PD_MSG_CTRL_WAIT
);
1729 case PD_CTRL_PR_SWAP
:
1730 if (port
->port_type
!= TYPEC_PORT_DRP
) {
1731 tcpm_queue_message(port
, PD_MSG_CTRL_REJECT
);
1734 switch (port
->state
) {
1737 tcpm_set_state(port
, PR_SWAP_ACCEPT
, 0);
1740 tcpm_queue_message(port
, PD_MSG_CTRL_WAIT
);
1744 case PD_CTRL_VCONN_SWAP
:
1745 switch (port
->state
) {
1748 tcpm_set_state(port
, VCONN_SWAP_ACCEPT
, 0);
1751 tcpm_queue_message(port
, PD_MSG_CTRL_WAIT
);
1755 case PD_CTRL_GET_SOURCE_CAP_EXT
:
1756 case PD_CTRL_GET_STATUS
:
1757 case PD_CTRL_FR_SWAP
:
1758 case PD_CTRL_GET_PPS_STATUS
:
1759 case PD_CTRL_GET_COUNTRY_CODES
:
1760 /* Currently not supported */
1761 tcpm_queue_message(port
, PD_MSG_CTRL_NOT_SUPP
);
1764 tcpm_log(port
, "Unhandled ctrl message type %#x", type
);
1769 static void tcpm_pd_ext_msg_request(struct tcpm_port
*port
,
1770 const struct pd_message
*msg
)
1772 enum pd_ext_msg_type type
= pd_header_type_le(msg
->header
);
1773 unsigned int data_size
= pd_ext_header_data_size_le(msg
->ext_msg
.header
);
1775 if (!(msg
->ext_msg
.header
& PD_EXT_HDR_CHUNKED
)) {
1776 tcpm_log(port
, "Unchunked extended messages unsupported");
1780 if (data_size
> PD_EXT_MAX_CHUNK_DATA
) {
1781 tcpm_log(port
, "Chunk handling not yet supported");
1788 * If PPS related events raised then get PPS status to clear
1789 * (see USB PD 3.0 Spec, 6.5.2.4)
1791 if (msg
->ext_msg
.data
[USB_PD_EXT_SDB_EVENT_FLAGS
] &
1792 USB_PD_EXT_SDB_PPS_EVENTS
)
1793 tcpm_set_state(port
, GET_PPS_STATUS_SEND
, 0);
1795 tcpm_set_state(port
, ready_state(port
), 0);
1797 case PD_EXT_PPS_STATUS
:
1799 * For now the PPS status message is used to clear events
1802 tcpm_set_state(port
, ready_state(port
), 0);
1804 case PD_EXT_SOURCE_CAP_EXT
:
1805 case PD_EXT_GET_BATT_CAP
:
1806 case PD_EXT_GET_BATT_STATUS
:
1807 case PD_EXT_BATT_CAP
:
1808 case PD_EXT_GET_MANUFACTURER_INFO
:
1809 case PD_EXT_MANUFACTURER_INFO
:
1810 case PD_EXT_SECURITY_REQUEST
:
1811 case PD_EXT_SECURITY_RESPONSE
:
1812 case PD_EXT_FW_UPDATE_REQUEST
:
1813 case PD_EXT_FW_UPDATE_RESPONSE
:
1814 case PD_EXT_COUNTRY_INFO
:
1815 case PD_EXT_COUNTRY_CODES
:
1816 tcpm_queue_message(port
, PD_MSG_CTRL_NOT_SUPP
);
1819 tcpm_log(port
, "Unhandled extended message type %#x", type
);
1824 static void tcpm_pd_rx_handler(struct work_struct
*work
)
1826 struct pd_rx_event
*event
= container_of(work
,
1827 struct pd_rx_event
, work
);
1828 const struct pd_message
*msg
= &event
->msg
;
1829 unsigned int cnt
= pd_header_cnt_le(msg
->header
);
1830 struct tcpm_port
*port
= event
->port
;
1832 mutex_lock(&port
->lock
);
1834 tcpm_log(port
, "PD RX, header: %#x [%d]", le16_to_cpu(msg
->header
),
1837 if (port
->attached
) {
1838 enum pd_ctrl_msg_type type
= pd_header_type_le(msg
->header
);
1839 unsigned int msgid
= pd_header_msgid_le(msg
->header
);
1842 * USB PD standard, 6.6.1.2:
1843 * "... if MessageID value in a received Message is the
1844 * same as the stored value, the receiver shall return a
1845 * GoodCRC Message with that MessageID value and drop
1846 * the Message (this is a retry of an already received
1847 * Message). Note: this shall not apply to the Soft_Reset
1848 * Message which always has a MessageID value of zero."
1850 if (msgid
== port
->rx_msgid
&& type
!= PD_CTRL_SOFT_RESET
)
1852 port
->rx_msgid
= msgid
;
1855 * If both ends believe to be DFP/host, we have a data role
1858 if (!!(le16_to_cpu(msg
->header
) & PD_HEADER_DATA_ROLE
) ==
1859 (port
->data_role
== TYPEC_HOST
)) {
1861 "Data role mismatch, initiating error recovery");
1862 tcpm_set_state(port
, ERROR_RECOVERY
, 0);
1864 if (msg
->header
& PD_HEADER_EXT_HDR
)
1865 tcpm_pd_ext_msg_request(port
, msg
);
1867 tcpm_pd_data_request(port
, msg
);
1869 tcpm_pd_ctrl_request(port
, msg
);
1874 mutex_unlock(&port
->lock
);
1878 void tcpm_pd_receive(struct tcpm_port
*port
, const struct pd_message
*msg
)
1880 struct pd_rx_event
*event
;
1882 event
= kzalloc(sizeof(*event
), GFP_ATOMIC
);
1886 INIT_WORK(&event
->work
, tcpm_pd_rx_handler
);
1888 memcpy(&event
->msg
, msg
, sizeof(*msg
));
1889 queue_work(port
->wq
, &event
->work
);
1891 EXPORT_SYMBOL_GPL(tcpm_pd_receive
);
1893 static int tcpm_pd_send_control(struct tcpm_port
*port
,
1894 enum pd_ctrl_msg_type type
)
1896 struct pd_message msg
;
1898 memset(&msg
, 0, sizeof(msg
));
1899 msg
.header
= PD_HEADER_LE(type
, port
->pwr_role
,
1901 port
->negotiated_rev
,
1902 port
->message_id
, 0);
1904 return tcpm_pd_transmit(port
, TCPC_TX_SOP
, &msg
);
1908 * Send queued message without affecting state.
1909 * Return true if state machine should go back to sleep,
1912 static bool tcpm_send_queued_message(struct tcpm_port
*port
)
1914 enum pd_msg_request queued_message
;
1917 queued_message
= port
->queued_message
;
1918 port
->queued_message
= PD_MSG_NONE
;
1920 switch (queued_message
) {
1921 case PD_MSG_CTRL_WAIT
:
1922 tcpm_pd_send_control(port
, PD_CTRL_WAIT
);
1924 case PD_MSG_CTRL_REJECT
:
1925 tcpm_pd_send_control(port
, PD_CTRL_REJECT
);
1927 case PD_MSG_CTRL_NOT_SUPP
:
1928 tcpm_pd_send_control(port
, PD_CTRL_NOT_SUPP
);
1930 case PD_MSG_DATA_SINK_CAP
:
1931 tcpm_pd_send_sink_caps(port
);
1933 case PD_MSG_DATA_SOURCE_CAP
:
1934 tcpm_pd_send_source_caps(port
);
1939 } while (port
->queued_message
!= PD_MSG_NONE
);
1941 if (port
->delayed_state
!= INVALID_STATE
) {
1942 if (time_is_after_jiffies(port
->delayed_runtime
)) {
1943 mod_delayed_work(port
->wq
, &port
->state_machine
,
1944 port
->delayed_runtime
- jiffies
);
1947 port
->delayed_state
= INVALID_STATE
;
1952 static int tcpm_pd_check_request(struct tcpm_port
*port
)
1954 u32 pdo
, rdo
= port
->sink_request
;
1955 unsigned int max
, op
, pdo_max
, index
;
1956 enum pd_pdo_type type
;
1958 index
= rdo_index(rdo
);
1959 if (!index
|| index
> port
->nr_src_pdo
)
1962 pdo
= port
->src_pdo
[index
- 1];
1963 type
= pdo_type(pdo
);
1965 case PDO_TYPE_FIXED
:
1967 max
= rdo_max_current(rdo
);
1968 op
= rdo_op_current(rdo
);
1969 pdo_max
= pdo_max_current(pdo
);
1973 if (max
> pdo_max
&& !(rdo
& RDO_CAP_MISMATCH
))
1976 if (type
== PDO_TYPE_FIXED
)
1978 "Requested %u mV, %u mA for %u / %u mA",
1979 pdo_fixed_voltage(pdo
), pdo_max
, op
, max
);
1982 "Requested %u -> %u mV, %u mA for %u / %u mA",
1983 pdo_min_voltage(pdo
), pdo_max_voltage(pdo
),
1987 max
= rdo_max_power(rdo
);
1988 op
= rdo_op_power(rdo
);
1989 pdo_max
= pdo_max_power(pdo
);
1993 if (max
> pdo_max
&& !(rdo
& RDO_CAP_MISMATCH
))
1996 "Requested %u -> %u mV, %u mW for %u / %u mW",
1997 pdo_min_voltage(pdo
), pdo_max_voltage(pdo
),
2004 port
->op_vsafe5v
= index
== 1;
2009 #define min_power(x, y) min(pdo_max_power(x), pdo_max_power(y))
2010 #define min_current(x, y) min(pdo_max_current(x), pdo_max_current(y))
2012 static int tcpm_pd_select_pdo(struct tcpm_port
*port
, int *sink_pdo
,
2015 unsigned int i
, j
, max_src_mv
= 0, min_src_mv
= 0, max_mw
= 0,
2016 max_mv
= 0, src_mw
= 0, src_ma
= 0, max_snk_mv
= 0,
2020 port
->pps_data
.supported
= false;
2021 port
->usb_type
= POWER_SUPPLY_USB_TYPE_PD
;
2024 * Select the source PDO providing the most power which has a
2027 for (i
= 0; i
< port
->nr_source_caps
; i
++) {
2028 u32 pdo
= port
->source_caps
[i
];
2029 enum pd_pdo_type type
= pdo_type(pdo
);
2032 case PDO_TYPE_FIXED
:
2033 max_src_mv
= pdo_fixed_voltage(pdo
);
2034 min_src_mv
= max_src_mv
;
2038 max_src_mv
= pdo_max_voltage(pdo
);
2039 min_src_mv
= pdo_min_voltage(pdo
);
2042 if (pdo_apdo_type(pdo
) == APDO_TYPE_PPS
) {
2043 port
->pps_data
.supported
= true;
2045 POWER_SUPPLY_USB_TYPE_PD_PPS
;
2049 tcpm_log(port
, "Invalid source PDO type, ignoring");
2054 case PDO_TYPE_FIXED
:
2056 src_ma
= pdo_max_current(pdo
);
2057 src_mw
= src_ma
* min_src_mv
/ 1000;
2060 src_mw
= pdo_max_power(pdo
);
2065 tcpm_log(port
, "Invalid source PDO type, ignoring");
2069 for (j
= 0; j
< port
->nr_snk_pdo
; j
++) {
2070 pdo
= port
->snk_pdo
[j
];
2072 switch (pdo_type(pdo
)) {
2073 case PDO_TYPE_FIXED
:
2074 max_snk_mv
= pdo_fixed_voltage(pdo
);
2075 min_snk_mv
= max_snk_mv
;
2079 max_snk_mv
= pdo_max_voltage(pdo
);
2080 min_snk_mv
= pdo_min_voltage(pdo
);
2085 tcpm_log(port
, "Invalid sink PDO type, ignoring");
2089 if (max_src_mv
<= max_snk_mv
&&
2090 min_src_mv
>= min_snk_mv
) {
2091 /* Prefer higher voltages if available */
2092 if ((src_mw
== max_mw
&& min_src_mv
> max_mv
) ||
2097 max_mv
= min_src_mv
;
2107 #define min_pps_apdo_current(x, y) \
2108 min(pdo_pps_apdo_max_current(x), pdo_pps_apdo_max_current(y))
2110 static unsigned int tcpm_pd_select_pps_apdo(struct tcpm_port
*port
)
2112 unsigned int i
, j
, max_mw
= 0, max_mv
= 0;
2113 unsigned int min_src_mv
, max_src_mv
, src_ma
, src_mw
;
2114 unsigned int min_snk_mv
, max_snk_mv
, snk_ma
;
2116 unsigned int src_pdo
= 0, snk_pdo
= 0;
2119 * Select the source PPS APDO providing the most power while staying
2120 * within the board's limits. We skip the first PDO as this is always
2123 for (i
= 1; i
< port
->nr_source_caps
; ++i
) {
2124 pdo
= port
->source_caps
[i
];
2126 switch (pdo_type(pdo
)) {
2128 if (pdo_apdo_type(pdo
) != APDO_TYPE_PPS
) {
2129 tcpm_log(port
, "Not PPS APDO (source), ignoring");
2133 min_src_mv
= pdo_pps_apdo_min_voltage(pdo
);
2134 max_src_mv
= pdo_pps_apdo_max_voltage(pdo
);
2135 src_ma
= pdo_pps_apdo_max_current(pdo
);
2136 src_mw
= (src_ma
* max_src_mv
) / 1000;
2139 * Now search through the sink PDOs to find a matching
2140 * PPS APDO. Again skip the first sink PDO as this will
2143 for (j
= 1; j
< port
->nr_snk_pdo
; j
++) {
2144 pdo
= port
->snk_pdo
[j
];
2146 switch (pdo_type(pdo
)) {
2148 if (pdo_apdo_type(pdo
) != APDO_TYPE_PPS
) {
2150 "Not PPS APDO (sink), ignoring");
2155 pdo_pps_apdo_min_voltage(pdo
);
2157 pdo_pps_apdo_max_voltage(pdo
);
2159 pdo_pps_apdo_max_current(pdo
);
2163 "Not APDO type (sink), ignoring");
2167 if (max_src_mv
<= max_snk_mv
&&
2168 min_src_mv
>= min_snk_mv
) {
2169 /* Prefer higher voltages if available */
2170 if ((src_mw
== max_mw
&&
2171 min_src_mv
> max_mv
) ||
2176 max_mv
= max_src_mv
;
2183 tcpm_log(port
, "Not APDO type (source), ignoring");
2189 pdo
= port
->source_caps
[src_pdo
];
2191 port
->pps_data
.min_volt
= pdo_pps_apdo_min_voltage(pdo
);
2192 port
->pps_data
.max_volt
= pdo_pps_apdo_max_voltage(pdo
);
2193 port
->pps_data
.max_curr
=
2194 min_pps_apdo_current(pdo
, port
->snk_pdo
[snk_pdo
]);
2195 port
->pps_data
.out_volt
=
2196 min(pdo_pps_apdo_max_voltage(pdo
), port
->pps_data
.out_volt
);
2197 port
->pps_data
.op_curr
=
2198 min(port
->pps_data
.max_curr
, port
->pps_data
.op_curr
);
2204 static int tcpm_pd_build_request(struct tcpm_port
*port
, u32
*rdo
)
2206 unsigned int mv
, ma
, mw
, flags
;
2207 unsigned int max_ma
, max_mw
;
2208 enum pd_pdo_type type
;
2209 u32 pdo
, matching_snk_pdo
;
2210 int src_pdo_index
= 0;
2211 int snk_pdo_index
= 0;
2214 ret
= tcpm_pd_select_pdo(port
, &snk_pdo_index
, &src_pdo_index
);
2218 pdo
= port
->source_caps
[src_pdo_index
];
2219 matching_snk_pdo
= port
->snk_pdo
[snk_pdo_index
];
2220 type
= pdo_type(pdo
);
2223 case PDO_TYPE_FIXED
:
2224 mv
= pdo_fixed_voltage(pdo
);
2228 mv
= pdo_min_voltage(pdo
);
2231 tcpm_log(port
, "Invalid PDO selected!");
2235 /* Select maximum available current within the sink pdo's limit */
2236 if (type
== PDO_TYPE_BATT
) {
2237 mw
= min_power(pdo
, matching_snk_pdo
);
2238 ma
= 1000 * mw
/ mv
;
2240 ma
= min_current(pdo
, matching_snk_pdo
);
2241 mw
= ma
* mv
/ 1000;
2244 flags
= RDO_USB_COMM
| RDO_NO_SUSPEND
;
2246 /* Set mismatch bit if offered power is less than operating power */
2249 if (mw
< port
->operating_snk_mw
) {
2250 flags
|= RDO_CAP_MISMATCH
;
2251 if (type
== PDO_TYPE_BATT
&&
2252 (pdo_max_power(matching_snk_pdo
) > pdo_max_power(pdo
)))
2253 max_mw
= pdo_max_power(matching_snk_pdo
);
2254 else if (pdo_max_current(matching_snk_pdo
) >
2255 pdo_max_current(pdo
))
2256 max_ma
= pdo_max_current(matching_snk_pdo
);
2259 tcpm_log(port
, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d",
2260 port
->cc_req
, port
->cc1
, port
->cc2
, port
->vbus_source
,
2261 port
->vconn_role
== TYPEC_SOURCE
? "source" : "sink",
2264 if (type
== PDO_TYPE_BATT
) {
2265 *rdo
= RDO_BATT(src_pdo_index
+ 1, mw
, max_mw
, flags
);
2267 tcpm_log(port
, "Requesting PDO %d: %u mV, %u mW%s",
2268 src_pdo_index
, mv
, mw
,
2269 flags
& RDO_CAP_MISMATCH
? " [mismatch]" : "");
2271 *rdo
= RDO_FIXED(src_pdo_index
+ 1, ma
, max_ma
, flags
);
2273 tcpm_log(port
, "Requesting PDO %d: %u mV, %u mA%s",
2274 src_pdo_index
, mv
, ma
,
2275 flags
& RDO_CAP_MISMATCH
? " [mismatch]" : "");
2278 port
->current_limit
= ma
;
2279 port
->supply_voltage
= mv
;
2284 static int tcpm_pd_send_request(struct tcpm_port
*port
)
2286 struct pd_message msg
;
2290 ret
= tcpm_pd_build_request(port
, &rdo
);
2294 memset(&msg
, 0, sizeof(msg
));
2295 msg
.header
= PD_HEADER_LE(PD_DATA_REQUEST
,
2298 port
->negotiated_rev
,
2299 port
->message_id
, 1);
2300 msg
.payload
[0] = cpu_to_le32(rdo
);
2302 return tcpm_pd_transmit(port
, TCPC_TX_SOP
, &msg
);
2305 static int tcpm_pd_build_pps_request(struct tcpm_port
*port
, u32
*rdo
)
2307 unsigned int out_mv
, op_ma
, op_mw
, min_mv
, max_mv
, max_ma
, flags
;
2308 enum pd_pdo_type type
;
2309 unsigned int src_pdo_index
;
2312 src_pdo_index
= tcpm_pd_select_pps_apdo(port
);
2316 pdo
= port
->source_caps
[src_pdo_index
];
2317 type
= pdo_type(pdo
);
2321 if (pdo_apdo_type(pdo
) != APDO_TYPE_PPS
) {
2322 tcpm_log(port
, "Invalid APDO selected!");
2325 min_mv
= port
->pps_data
.min_volt
;
2326 max_mv
= port
->pps_data
.max_volt
;
2327 max_ma
= port
->pps_data
.max_curr
;
2328 out_mv
= port
->pps_data
.out_volt
;
2329 op_ma
= port
->pps_data
.op_curr
;
2332 tcpm_log(port
, "Invalid PDO selected!");
2336 flags
= RDO_USB_COMM
| RDO_NO_SUSPEND
;
2338 op_mw
= (op_ma
* out_mv
) / 1000;
2339 if (op_mw
< port
->operating_snk_mw
) {
2341 * Try raising current to meet power needs. If that's not enough
2342 * then try upping the voltage. If that's still not enough
2343 * then we've obviously chosen a PPS APDO which really isn't
2344 * suitable so abandon ship.
2346 op_ma
= (port
->operating_snk_mw
* 1000) / out_mv
;
2347 if ((port
->operating_snk_mw
* 1000) % out_mv
)
2349 op_ma
+= RDO_PROG_CURR_MA_STEP
- (op_ma
% RDO_PROG_CURR_MA_STEP
);
2351 if (op_ma
> max_ma
) {
2353 out_mv
= (port
->operating_snk_mw
* 1000) / op_ma
;
2354 if ((port
->operating_snk_mw
* 1000) % op_ma
)
2356 out_mv
+= RDO_PROG_VOLT_MV_STEP
-
2357 (out_mv
% RDO_PROG_VOLT_MV_STEP
);
2359 if (out_mv
> max_mv
) {
2360 tcpm_log(port
, "Invalid PPS APDO selected!");
2366 tcpm_log(port
, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d",
2367 port
->cc_req
, port
->cc1
, port
->cc2
, port
->vbus_source
,
2368 port
->vconn_role
== TYPEC_SOURCE
? "source" : "sink",
2371 *rdo
= RDO_PROG(src_pdo_index
+ 1, out_mv
, op_ma
, flags
);
2373 tcpm_log(port
, "Requesting APDO %d: %u mV, %u mA",
2374 src_pdo_index
, out_mv
, op_ma
);
2376 port
->pps_data
.op_curr
= op_ma
;
2377 port
->pps_data
.out_volt
= out_mv
;
2382 static int tcpm_pd_send_pps_request(struct tcpm_port
*port
)
2384 struct pd_message msg
;
2388 ret
= tcpm_pd_build_pps_request(port
, &rdo
);
2392 memset(&msg
, 0, sizeof(msg
));
2393 msg
.header
= PD_HEADER_LE(PD_DATA_REQUEST
,
2396 port
->negotiated_rev
,
2397 port
->message_id
, 1);
2398 msg
.payload
[0] = cpu_to_le32(rdo
);
2400 return tcpm_pd_transmit(port
, TCPC_TX_SOP
, &msg
);
2403 static int tcpm_set_vbus(struct tcpm_port
*port
, bool enable
)
2407 if (enable
&& port
->vbus_charge
)
2410 tcpm_log(port
, "vbus:=%d charge=%d", enable
, port
->vbus_charge
);
2412 ret
= port
->tcpc
->set_vbus(port
->tcpc
, enable
, port
->vbus_charge
);
2416 port
->vbus_source
= enable
;
2420 static int tcpm_set_charge(struct tcpm_port
*port
, bool charge
)
2424 if (charge
&& port
->vbus_source
)
2427 if (charge
!= port
->vbus_charge
) {
2428 tcpm_log(port
, "vbus=%d charge:=%d", port
->vbus_source
, charge
);
2429 ret
= port
->tcpc
->set_vbus(port
->tcpc
, port
->vbus_source
,
2434 port
->vbus_charge
= charge
;
2438 static bool tcpm_start_drp_toggling(struct tcpm_port
*port
)
2442 if (port
->tcpc
->start_drp_toggling
&&
2443 port
->port_type
== TYPEC_PORT_DRP
) {
2444 tcpm_log_force(port
, "Start DRP toggling");
2445 ret
= port
->tcpc
->start_drp_toggling(port
->tcpc
,
2454 static void tcpm_set_cc(struct tcpm_port
*port
, enum typec_cc_status cc
)
2456 tcpm_log(port
, "cc:=%d", cc
);
2458 port
->tcpc
->set_cc(port
->tcpc
, cc
);
2461 static int tcpm_init_vbus(struct tcpm_port
*port
)
2465 ret
= port
->tcpc
->set_vbus(port
->tcpc
, false, false);
2466 port
->vbus_source
= false;
2467 port
->vbus_charge
= false;
2471 static int tcpm_init_vconn(struct tcpm_port
*port
)
2475 ret
= port
->tcpc
->set_vconn(port
->tcpc
, false);
2476 port
->vconn_role
= TYPEC_SINK
;
2480 static void tcpm_typec_connect(struct tcpm_port
*port
)
2482 if (!port
->connected
) {
2483 /* Make sure we don't report stale identity information */
2484 memset(&port
->partner_ident
, 0, sizeof(port
->partner_ident
));
2485 port
->partner_desc
.usb_pd
= port
->pd_capable
;
2486 if (tcpm_port_is_debug(port
))
2487 port
->partner_desc
.accessory
= TYPEC_ACCESSORY_DEBUG
;
2488 else if (tcpm_port_is_audio(port
))
2489 port
->partner_desc
.accessory
= TYPEC_ACCESSORY_AUDIO
;
2491 port
->partner_desc
.accessory
= TYPEC_ACCESSORY_NONE
;
2492 port
->partner
= typec_register_partner(port
->typec_port
,
2493 &port
->partner_desc
);
2494 port
->connected
= true;
2498 static int tcpm_src_attach(struct tcpm_port
*port
)
2500 enum typec_cc_polarity polarity
=
2501 port
->cc2
== TYPEC_CC_RD
? TYPEC_POLARITY_CC2
2502 : TYPEC_POLARITY_CC1
;
2508 ret
= tcpm_set_polarity(port
, polarity
);
2512 ret
= tcpm_set_roles(port
, true, TYPEC_SOURCE
, TYPEC_HOST
);
2516 ret
= port
->tcpc
->set_pd_rx(port
->tcpc
, true);
2518 goto out_disable_mux
;
2521 * USB Type-C specification, version 1.2,
2522 * chapter 4.5.2.2.8.1 (Attached.SRC Requirements)
2523 * Enable VCONN only if the non-RD port is set to RA.
2525 if ((polarity
== TYPEC_POLARITY_CC1
&& port
->cc2
== TYPEC_CC_RA
) ||
2526 (polarity
== TYPEC_POLARITY_CC2
&& port
->cc1
== TYPEC_CC_RA
)) {
2527 ret
= tcpm_set_vconn(port
, true);
2529 goto out_disable_pd
;
2532 ret
= tcpm_set_vbus(port
, true);
2534 goto out_disable_vconn
;
2536 port
->pd_capable
= false;
2538 port
->partner
= NULL
;
2540 port
->attached
= true;
2541 port
->send_discover
= true;
2546 tcpm_set_vconn(port
, false);
2548 port
->tcpc
->set_pd_rx(port
->tcpc
, false);
2550 tcpm_mux_set(port
, TYPEC_MUX_NONE
, USB_ROLE_NONE
,
2551 TYPEC_ORIENTATION_NONE
);
2555 static void tcpm_typec_disconnect(struct tcpm_port
*port
)
2557 if (port
->connected
) {
2558 typec_unregister_partner(port
->partner
);
2559 port
->partner
= NULL
;
2560 port
->connected
= false;
2564 static void tcpm_unregister_altmodes(struct tcpm_port
*port
)
2566 struct pd_mode_data
*modep
= &port
->mode_data
;
2569 for (i
= 0; i
< modep
->altmodes
; i
++) {
2570 typec_unregister_altmode(port
->partner_altmode
[i
]);
2571 port
->partner_altmode
[i
] = NULL
;
2574 memset(modep
, 0, sizeof(*modep
));
2577 static void tcpm_reset_port(struct tcpm_port
*port
)
2579 tcpm_unregister_altmodes(port
);
2580 tcpm_typec_disconnect(port
);
2581 port
->attached
= false;
2582 port
->pd_capable
= false;
2583 port
->pps_data
.supported
= false;
2586 * First Rx ID should be 0; set this to a sentinel of -1 so that
2587 * we can check tcpm_pd_rx_handler() if we had seen it before.
2589 port
->rx_msgid
= -1;
2591 port
->tcpc
->set_pd_rx(port
->tcpc
, false);
2592 tcpm_init_vbus(port
); /* also disables charging */
2593 tcpm_init_vconn(port
);
2594 tcpm_set_current_limit(port
, 0, 0);
2595 tcpm_set_polarity(port
, TYPEC_POLARITY_CC1
);
2596 tcpm_mux_set(port
, TYPEC_MUX_NONE
, USB_ROLE_NONE
,
2597 TYPEC_ORIENTATION_NONE
);
2598 tcpm_set_attached_state(port
, false);
2599 port
->try_src_count
= 0;
2600 port
->try_snk_count
= 0;
2601 port
->usb_type
= POWER_SUPPLY_USB_TYPE_C
;
2603 power_supply_changed(port
->psy
);
2606 static void tcpm_detach(struct tcpm_port
*port
)
2608 if (!port
->attached
)
2611 if (tcpm_port_is_disconnected(port
))
2612 port
->hard_reset_count
= 0;
2614 tcpm_reset_port(port
);
2617 static void tcpm_src_detach(struct tcpm_port
*port
)
2622 static int tcpm_snk_attach(struct tcpm_port
*port
)
2629 ret
= tcpm_set_polarity(port
, port
->cc2
!= TYPEC_CC_OPEN
?
2630 TYPEC_POLARITY_CC2
: TYPEC_POLARITY_CC1
);
2634 ret
= tcpm_set_roles(port
, true, TYPEC_SINK
, TYPEC_DEVICE
);
2638 port
->pd_capable
= false;
2640 port
->partner
= NULL
;
2642 port
->attached
= true;
2643 port
->send_discover
= true;
2648 static void tcpm_snk_detach(struct tcpm_port
*port
)
2653 static int tcpm_acc_attach(struct tcpm_port
*port
)
2660 ret
= tcpm_set_roles(port
, true, TYPEC_SOURCE
, TYPEC_HOST
);
2664 port
->partner
= NULL
;
2666 tcpm_typec_connect(port
);
2668 port
->attached
= true;
2673 static void tcpm_acc_detach(struct tcpm_port
*port
)
2678 static inline enum tcpm_state
hard_reset_state(struct tcpm_port
*port
)
2680 if (port
->hard_reset_count
< PD_N_HARD_RESET_COUNT
)
2681 return HARD_RESET_SEND
;
2682 if (port
->pd_capable
)
2683 return ERROR_RECOVERY
;
2684 if (port
->pwr_role
== TYPEC_SOURCE
)
2685 return SRC_UNATTACHED
;
2686 if (port
->state
== SNK_WAIT_CAPABILITIES
)
2688 return SNK_UNATTACHED
;
2691 static inline enum tcpm_state
unattached_state(struct tcpm_port
*port
)
2693 if (port
->port_type
== TYPEC_PORT_DRP
) {
2694 if (port
->pwr_role
== TYPEC_SOURCE
)
2695 return SRC_UNATTACHED
;
2697 return SNK_UNATTACHED
;
2698 } else if (port
->port_type
== TYPEC_PORT_SRC
) {
2699 return SRC_UNATTACHED
;
2702 return SNK_UNATTACHED
;
2705 static void tcpm_check_send_discover(struct tcpm_port
*port
)
2707 if (port
->data_role
== TYPEC_HOST
&& port
->send_discover
&&
2709 tcpm_send_vdm(port
, USB_SID_PD
, CMD_DISCOVER_IDENT
, NULL
, 0);
2710 port
->send_discover
= false;
2714 static void tcpm_swap_complete(struct tcpm_port
*port
, int result
)
2716 if (port
->swap_pending
) {
2717 port
->swap_status
= result
;
2718 port
->swap_pending
= false;
2719 port
->non_pd_role_swap
= false;
2720 complete(&port
->swap_complete
);
2724 static enum typec_pwr_opmode
tcpm_get_pwr_opmode(enum typec_cc_status cc
)
2727 case TYPEC_CC_RP_1_5
:
2728 return TYPEC_PWR_MODE_1_5A
;
2729 case TYPEC_CC_RP_3_0
:
2730 return TYPEC_PWR_MODE_3_0A
;
2731 case TYPEC_CC_RP_DEF
:
2733 return TYPEC_PWR_MODE_USB
;
2737 static void run_state_machine(struct tcpm_port
*port
)
2740 enum typec_pwr_opmode opmode
;
2743 port
->enter_state
= port
->state
;
2744 switch (port
->state
) {
2748 case SRC_UNATTACHED
:
2749 if (!port
->non_pd_role_swap
)
2750 tcpm_swap_complete(port
, -ENOTCONN
);
2751 tcpm_src_detach(port
);
2752 if (tcpm_start_drp_toggling(port
)) {
2753 tcpm_set_state(port
, DRP_TOGGLING
, 0);
2756 tcpm_set_cc(port
, tcpm_rp_cc(port
));
2757 if (port
->port_type
== TYPEC_PORT_DRP
)
2758 tcpm_set_state(port
, SNK_UNATTACHED
, PD_T_DRP_SNK
);
2760 case SRC_ATTACH_WAIT
:
2761 if (tcpm_port_is_debug(port
))
2762 tcpm_set_state(port
, DEBUG_ACC_ATTACHED
,
2764 else if (tcpm_port_is_audio(port
))
2765 tcpm_set_state(port
, AUDIO_ACC_ATTACHED
,
2767 else if (tcpm_port_is_source(port
))
2768 tcpm_set_state(port
,
2769 tcpm_try_snk(port
) ? SNK_TRY
2775 port
->try_snk_count
++;
2778 * - Do not drive vconn or vbus
2779 * - Terminate CC pins (both) to Rd
2781 * - Wait for tDRPTry (PD_T_DRP_TRY).
2782 * Until then, ignore any state changes.
2784 tcpm_set_cc(port
, TYPEC_CC_RD
);
2785 tcpm_set_state(port
, SNK_TRY_WAIT
, PD_T_DRP_TRY
);
2788 if (tcpm_port_is_sink(port
)) {
2789 tcpm_set_state(port
, SNK_TRY_WAIT_DEBOUNCE
, 0);
2791 tcpm_set_state(port
, SRC_TRYWAIT
, 0);
2795 case SNK_TRY_WAIT_DEBOUNCE
:
2796 tcpm_set_state(port
, SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS
,
2799 case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS
:
2800 if (port
->vbus_present
&& tcpm_port_is_sink(port
)) {
2801 tcpm_set_state(port
, SNK_ATTACHED
, 0);
2803 tcpm_set_state(port
, SRC_TRYWAIT
, 0);
2808 tcpm_set_cc(port
, tcpm_rp_cc(port
));
2809 if (port
->max_wait
== 0) {
2810 port
->max_wait
= jiffies
+
2811 msecs_to_jiffies(PD_T_DRP_TRY
);
2812 tcpm_set_state(port
, SRC_TRYWAIT_UNATTACHED
,
2815 if (time_is_after_jiffies(port
->max_wait
))
2816 tcpm_set_state(port
, SRC_TRYWAIT_UNATTACHED
,
2817 jiffies_to_msecs(port
->max_wait
-
2820 tcpm_set_state(port
, SNK_UNATTACHED
, 0);
2823 case SRC_TRYWAIT_DEBOUNCE
:
2824 tcpm_set_state(port
, SRC_ATTACHED
, PD_T_CC_DEBOUNCE
);
2826 case SRC_TRYWAIT_UNATTACHED
:
2827 tcpm_set_state(port
, SNK_UNATTACHED
, 0);
2831 ret
= tcpm_src_attach(port
);
2832 tcpm_set_state(port
, SRC_UNATTACHED
,
2833 ret
< 0 ? 0 : PD_T_PS_SOURCE_ON
);
2836 opmode
= tcpm_get_pwr_opmode(tcpm_rp_cc(port
));
2837 typec_set_pwr_opmode(port
->typec_port
, opmode
);
2838 port
->pwr_opmode
= TYPEC_PWR_MODE_USB
;
2839 port
->caps_count
= 0;
2840 port
->negotiated_rev
= PD_MAX_REV
;
2841 port
->message_id
= 0;
2842 port
->rx_msgid
= -1;
2843 port
->explicit_contract
= false;
2844 tcpm_set_state(port
, SRC_SEND_CAPABILITIES
, 0);
2846 case SRC_SEND_CAPABILITIES
:
2848 if (port
->caps_count
> PD_N_CAPS_COUNT
) {
2849 tcpm_set_state(port
, SRC_READY
, 0);
2852 ret
= tcpm_pd_send_source_caps(port
);
2854 tcpm_set_state(port
, SRC_SEND_CAPABILITIES
,
2855 PD_T_SEND_SOURCE_CAP
);
2858 * Per standard, we should clear the reset counter here.
2859 * However, that can result in state machine hang-ups.
2860 * Reset it only in READY state to improve stability.
2862 /* port->hard_reset_count = 0; */
2863 port
->caps_count
= 0;
2864 port
->pd_capable
= true;
2865 tcpm_set_state_cond(port
, hard_reset_state(port
),
2866 PD_T_SEND_SOURCE_CAP
);
2869 case SRC_NEGOTIATE_CAPABILITIES
:
2870 ret
= tcpm_pd_check_request(port
);
2872 tcpm_pd_send_control(port
, PD_CTRL_REJECT
);
2873 if (!port
->explicit_contract
) {
2874 tcpm_set_state(port
,
2875 SRC_WAIT_NEW_CAPABILITIES
, 0);
2877 tcpm_set_state(port
, SRC_READY
, 0);
2880 tcpm_pd_send_control(port
, PD_CTRL_ACCEPT
);
2881 tcpm_set_state(port
, SRC_TRANSITION_SUPPLY
,
2882 PD_T_SRC_TRANSITION
);
2885 case SRC_TRANSITION_SUPPLY
:
2886 /* XXX: regulator_set_voltage(vbus, ...) */
2887 tcpm_pd_send_control(port
, PD_CTRL_PS_RDY
);
2888 port
->explicit_contract
= true;
2889 typec_set_pwr_opmode(port
->typec_port
, TYPEC_PWR_MODE_PD
);
2890 port
->pwr_opmode
= TYPEC_PWR_MODE_PD
;
2891 tcpm_set_state_cond(port
, SRC_READY
, 0);
2895 port
->hard_reset_count
= 0;
2897 port
->try_src_count
= 0;
2899 tcpm_swap_complete(port
, 0);
2900 tcpm_typec_connect(port
);
2902 tcpm_check_send_discover(port
);
2905 * Sending ping messages is not necessary if
2906 * - the source operates at vSafe5V
2908 * - The system is not operating in PD mode
2910 * - Both partners are connected using a Type-C connector
2912 * There is no actual need to send PD messages since the local
2913 * port type-c and the spec does not clearly say whether PD is
2914 * possible when type-c is connected to Type-A/B
2917 case SRC_WAIT_NEW_CAPABILITIES
:
2918 /* Nothing to do... */
2922 case SNK_UNATTACHED
:
2923 if (!port
->non_pd_role_swap
)
2924 tcpm_swap_complete(port
, -ENOTCONN
);
2925 tcpm_pps_complete(port
, -ENOTCONN
);
2926 tcpm_snk_detach(port
);
2927 if (tcpm_start_drp_toggling(port
)) {
2928 tcpm_set_state(port
, DRP_TOGGLING
, 0);
2931 tcpm_set_cc(port
, TYPEC_CC_RD
);
2932 if (port
->port_type
== TYPEC_PORT_DRP
)
2933 tcpm_set_state(port
, SRC_UNATTACHED
, PD_T_DRP_SRC
);
2935 case SNK_ATTACH_WAIT
:
2936 if ((port
->cc1
== TYPEC_CC_OPEN
&&
2937 port
->cc2
!= TYPEC_CC_OPEN
) ||
2938 (port
->cc1
!= TYPEC_CC_OPEN
&&
2939 port
->cc2
== TYPEC_CC_OPEN
))
2940 tcpm_set_state(port
, SNK_DEBOUNCED
,
2942 else if (tcpm_port_is_disconnected(port
))
2943 tcpm_set_state(port
, SNK_UNATTACHED
,
2947 if (tcpm_port_is_disconnected(port
))
2948 tcpm_set_state(port
, SNK_UNATTACHED
,
2950 else if (port
->vbus_present
)
2951 tcpm_set_state(port
,
2952 tcpm_try_src(port
) ? SRC_TRY
2956 /* Wait for VBUS, but not forever */
2957 tcpm_set_state(port
, PORT_RESET
, PD_T_PS_SOURCE_ON
);
2961 port
->try_src_count
++;
2962 tcpm_set_cc(port
, tcpm_rp_cc(port
));
2964 tcpm_set_state(port
, SRC_TRY_WAIT
, 0);
2967 if (port
->max_wait
== 0) {
2968 port
->max_wait
= jiffies
+
2969 msecs_to_jiffies(PD_T_DRP_TRY
);
2970 msecs
= PD_T_DRP_TRY
;
2972 if (time_is_after_jiffies(port
->max_wait
))
2973 msecs
= jiffies_to_msecs(port
->max_wait
-
2978 tcpm_set_state(port
, SNK_TRYWAIT
, msecs
);
2980 case SRC_TRY_DEBOUNCE
:
2981 tcpm_set_state(port
, SRC_ATTACHED
, PD_T_PD_DEBOUNCE
);
2984 tcpm_set_cc(port
, TYPEC_CC_RD
);
2985 tcpm_set_state(port
, SNK_TRYWAIT_VBUS
, PD_T_CC_DEBOUNCE
);
2987 case SNK_TRYWAIT_VBUS
:
2989 * TCPM stays in this state indefinitely until VBUS
2990 * is detected as long as Rp is not detected for
2991 * more than a time period of tPDDebounce.
2993 if (port
->vbus_present
&& tcpm_port_is_sink(port
)) {
2994 tcpm_set_state(port
, SNK_ATTACHED
, 0);
2997 if (!tcpm_port_is_sink(port
))
2998 tcpm_set_state(port
, SNK_TRYWAIT_DEBOUNCE
, 0);
3000 case SNK_TRYWAIT_DEBOUNCE
:
3001 tcpm_set_state(port
, SNK_UNATTACHED
, PD_T_PD_DEBOUNCE
);
3004 ret
= tcpm_snk_attach(port
);
3006 tcpm_set_state(port
, SNK_UNATTACHED
, 0);
3008 tcpm_set_state(port
, SNK_STARTUP
, 0);
3011 opmode
= tcpm_get_pwr_opmode(port
->polarity
?
3012 port
->cc2
: port
->cc1
);
3013 typec_set_pwr_opmode(port
->typec_port
, opmode
);
3014 port
->pwr_opmode
= TYPEC_PWR_MODE_USB
;
3015 port
->negotiated_rev
= PD_MAX_REV
;
3016 port
->message_id
= 0;
3017 port
->rx_msgid
= -1;
3018 port
->explicit_contract
= false;
3019 tcpm_set_state(port
, SNK_DISCOVERY
, 0);
3022 if (port
->vbus_present
) {
3023 tcpm_set_current_limit(port
,
3024 tcpm_get_current_limit(port
),
3026 tcpm_set_charge(port
, true);
3027 tcpm_set_state(port
, SNK_WAIT_CAPABILITIES
, 0);
3031 * For DRP, timeouts differ. Also, handling is supposed to be
3032 * different and much more complex (dead battery detection;
3033 * see USB power delivery specification, section 8.3.3.6.1.5.1).
3035 tcpm_set_state(port
, hard_reset_state(port
),
3036 port
->port_type
== TYPEC_PORT_DRP
?
3037 PD_T_DB_DETECT
: PD_T_NO_RESPONSE
);
3039 case SNK_DISCOVERY_DEBOUNCE
:
3040 tcpm_set_state(port
, SNK_DISCOVERY_DEBOUNCE_DONE
,
3043 case SNK_DISCOVERY_DEBOUNCE_DONE
:
3044 if (!tcpm_port_is_disconnected(port
) &&
3045 tcpm_port_is_sink(port
) &&
3046 time_is_after_jiffies(port
->delayed_runtime
)) {
3047 tcpm_set_state(port
, SNK_DISCOVERY
,
3048 jiffies_to_msecs(port
->delayed_runtime
-
3052 tcpm_set_state(port
, unattached_state(port
), 0);
3054 case SNK_WAIT_CAPABILITIES
:
3055 ret
= port
->tcpc
->set_pd_rx(port
->tcpc
, true);
3057 tcpm_set_state(port
, SNK_READY
, 0);
3061 * If VBUS has never been low, and we time out waiting
3062 * for source cap, try a soft reset first, in case we
3063 * were already in a stable contract before this boot.
3064 * Do this only once.
3066 if (port
->vbus_never_low
) {
3067 port
->vbus_never_low
= false;
3068 tcpm_set_state(port
, SOFT_RESET_SEND
,
3069 PD_T_SINK_WAIT_CAP
);
3071 tcpm_set_state(port
, hard_reset_state(port
),
3072 PD_T_SINK_WAIT_CAP
);
3075 case SNK_NEGOTIATE_CAPABILITIES
:
3076 port
->pd_capable
= true;
3077 port
->hard_reset_count
= 0;
3078 ret
= tcpm_pd_send_request(port
);
3080 /* Let the Source send capabilities again. */
3081 tcpm_set_state(port
, SNK_WAIT_CAPABILITIES
, 0);
3083 tcpm_set_state_cond(port
, hard_reset_state(port
),
3084 PD_T_SENDER_RESPONSE
);
3087 case SNK_NEGOTIATE_PPS_CAPABILITIES
:
3088 ret
= tcpm_pd_send_pps_request(port
);
3090 port
->pps_status
= ret
;
3092 * If this was called due to updates to sink
3093 * capabilities, and pps is no longer valid, we should
3094 * safely fall back to a standard PDO.
3096 if (port
->update_sink_caps
)
3097 tcpm_set_state(port
, SNK_NEGOTIATE_CAPABILITIES
, 0);
3099 tcpm_set_state(port
, SNK_READY
, 0);
3101 tcpm_set_state_cond(port
, hard_reset_state(port
),
3102 PD_T_SENDER_RESPONSE
);
3105 case SNK_TRANSITION_SINK
:
3106 case SNK_TRANSITION_SINK_VBUS
:
3107 tcpm_set_state(port
, hard_reset_state(port
),
3108 PD_T_PS_TRANSITION
);
3111 port
->try_snk_count
= 0;
3112 port
->update_sink_caps
= false;
3113 if (port
->explicit_contract
) {
3114 typec_set_pwr_opmode(port
->typec_port
,
3116 port
->pwr_opmode
= TYPEC_PWR_MODE_PD
;
3119 tcpm_swap_complete(port
, 0);
3120 tcpm_typec_connect(port
);
3121 tcpm_check_send_discover(port
);
3122 tcpm_pps_complete(port
, port
->pps_status
);
3124 power_supply_changed(port
->psy
);
3128 /* Accessory states */
3129 case ACC_UNATTACHED
:
3130 tcpm_acc_detach(port
);
3131 tcpm_set_state(port
, SRC_UNATTACHED
, 0);
3133 case DEBUG_ACC_ATTACHED
:
3134 case AUDIO_ACC_ATTACHED
:
3135 ret
= tcpm_acc_attach(port
);
3137 tcpm_set_state(port
, ACC_UNATTACHED
, 0);
3139 case AUDIO_ACC_DEBOUNCE
:
3140 tcpm_set_state(port
, ACC_UNATTACHED
, PD_T_CC_DEBOUNCE
);
3143 /* Hard_Reset states */
3144 case HARD_RESET_SEND
:
3145 tcpm_pd_transmit(port
, TCPC_TX_HARD_RESET
, NULL
);
3146 tcpm_set_state(port
, HARD_RESET_START
, 0);
3148 case HARD_RESET_START
:
3149 port
->hard_reset_count
++;
3150 port
->tcpc
->set_pd_rx(port
->tcpc
, false);
3151 tcpm_unregister_altmodes(port
);
3152 port
->send_discover
= true;
3153 if (port
->pwr_role
== TYPEC_SOURCE
)
3154 tcpm_set_state(port
, SRC_HARD_RESET_VBUS_OFF
,
3155 PD_T_PS_HARD_RESET
);
3157 tcpm_set_state(port
, SNK_HARD_RESET_SINK_OFF
, 0);
3159 case SRC_HARD_RESET_VBUS_OFF
:
3160 tcpm_set_vconn(port
, true);
3161 tcpm_set_vbus(port
, false);
3162 tcpm_set_roles(port
, false, TYPEC_SOURCE
, TYPEC_HOST
);
3163 tcpm_set_state(port
, SRC_HARD_RESET_VBUS_ON
, PD_T_SRC_RECOVER
);
3165 case SRC_HARD_RESET_VBUS_ON
:
3166 tcpm_set_vbus(port
, true);
3167 port
->tcpc
->set_pd_rx(port
->tcpc
, true);
3168 tcpm_set_attached_state(port
, true);
3169 tcpm_set_state(port
, SRC_UNATTACHED
, PD_T_PS_SOURCE_ON
);
3171 case SNK_HARD_RESET_SINK_OFF
:
3172 memset(&port
->pps_data
, 0, sizeof(port
->pps_data
));
3173 tcpm_set_vconn(port
, false);
3174 tcpm_set_charge(port
, false);
3175 tcpm_set_roles(port
, false, TYPEC_SINK
, TYPEC_DEVICE
);
3177 * VBUS may or may not toggle, depending on the adapter.
3178 * If it doesn't toggle, transition to SNK_HARD_RESET_SINK_ON
3179 * directly after timeout.
3181 tcpm_set_state(port
, SNK_HARD_RESET_SINK_ON
, PD_T_SAFE_0V
);
3183 case SNK_HARD_RESET_WAIT_VBUS
:
3184 /* Assume we're disconnected if VBUS doesn't come back. */
3185 tcpm_set_state(port
, SNK_UNATTACHED
,
3186 PD_T_SRC_RECOVER_MAX
+ PD_T_SRC_TURN_ON
);
3188 case SNK_HARD_RESET_SINK_ON
:
3189 /* Note: There is no guarantee that VBUS is on in this state */
3192 * The specification suggests that dual mode ports in sink
3193 * mode should transition to state PE_SRC_Transition_to_default.
3194 * See USB power delivery specification chapter 8.3.3.6.1.3.
3195 * This would mean to to
3196 * - turn off VCONN, reset power supply
3197 * - request hardware reset
3199 * - Transition to state PE_Src_Startup
3200 * SNK only ports shall transition to state Snk_Startup
3201 * (see chapter 8.3.3.3.8).
3202 * Similar, dual-mode ports in source mode should transition
3203 * to PE_SNK_Transition_to_default.
3205 tcpm_set_attached_state(port
, true);
3206 tcpm_set_state(port
, SNK_STARTUP
, 0);
3209 /* Soft_Reset states */
3211 port
->message_id
= 0;
3212 port
->rx_msgid
= -1;
3213 tcpm_pd_send_control(port
, PD_CTRL_ACCEPT
);
3214 if (port
->pwr_role
== TYPEC_SOURCE
)
3215 tcpm_set_state(port
, SRC_SEND_CAPABILITIES
, 0);
3217 tcpm_set_state(port
, SNK_WAIT_CAPABILITIES
, 0);
3219 case SOFT_RESET_SEND
:
3220 port
->message_id
= 0;
3221 port
->rx_msgid
= -1;
3222 if (tcpm_pd_send_control(port
, PD_CTRL_SOFT_RESET
))
3223 tcpm_set_state_cond(port
, hard_reset_state(port
), 0);
3225 tcpm_set_state_cond(port
, hard_reset_state(port
),
3226 PD_T_SENDER_RESPONSE
);
3229 /* DR_Swap states */
3231 tcpm_pd_send_control(port
, PD_CTRL_DR_SWAP
);
3232 tcpm_set_state_cond(port
, DR_SWAP_SEND_TIMEOUT
,
3233 PD_T_SENDER_RESPONSE
);
3235 case DR_SWAP_ACCEPT
:
3236 tcpm_pd_send_control(port
, PD_CTRL_ACCEPT
);
3237 tcpm_set_state_cond(port
, DR_SWAP_CHANGE_DR
, 0);
3239 case DR_SWAP_SEND_TIMEOUT
:
3240 tcpm_swap_complete(port
, -ETIMEDOUT
);
3241 tcpm_set_state(port
, ready_state(port
), 0);
3243 case DR_SWAP_CHANGE_DR
:
3244 if (port
->data_role
== TYPEC_HOST
) {
3245 tcpm_unregister_altmodes(port
);
3246 tcpm_set_roles(port
, true, port
->pwr_role
,
3249 tcpm_set_roles(port
, true, port
->pwr_role
,
3251 port
->send_discover
= true;
3253 tcpm_set_state(port
, ready_state(port
), 0);
3256 /* PR_Swap states */
3257 case PR_SWAP_ACCEPT
:
3258 tcpm_pd_send_control(port
, PD_CTRL_ACCEPT
);
3259 tcpm_set_state(port
, PR_SWAP_START
, 0);
3262 tcpm_pd_send_control(port
, PD_CTRL_PR_SWAP
);
3263 tcpm_set_state_cond(port
, PR_SWAP_SEND_TIMEOUT
,
3264 PD_T_SENDER_RESPONSE
);
3266 case PR_SWAP_SEND_TIMEOUT
:
3267 tcpm_swap_complete(port
, -ETIMEDOUT
);
3268 tcpm_set_state(port
, ready_state(port
), 0);
3271 if (port
->pwr_role
== TYPEC_SOURCE
)
3272 tcpm_set_state(port
, PR_SWAP_SRC_SNK_TRANSITION_OFF
,
3273 PD_T_SRC_TRANSITION
);
3275 tcpm_set_state(port
, PR_SWAP_SNK_SRC_SINK_OFF
, 0);
3277 case PR_SWAP_SRC_SNK_TRANSITION_OFF
:
3278 tcpm_set_vbus(port
, false);
3279 port
->explicit_contract
= false;
3280 /* allow time for Vbus discharge, must be < tSrcSwapStdby */
3281 tcpm_set_state(port
, PR_SWAP_SRC_SNK_SOURCE_OFF
,
3284 case PR_SWAP_SRC_SNK_SOURCE_OFF
:
3285 tcpm_set_cc(port
, TYPEC_CC_RD
);
3286 /* allow CC debounce */
3287 tcpm_set_state(port
, PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED
,
3290 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED
:
3292 * USB-PD standard, 6.2.1.4, Port Power Role:
3293 * "During the Power Role Swap Sequence, for the initial Source
3294 * Port, the Port Power Role field shall be set to Sink in the
3295 * PS_RDY Message indicating that the initial Source’s power
3296 * supply is turned off"
3298 tcpm_set_pwr_role(port
, TYPEC_SINK
);
3299 if (tcpm_pd_send_control(port
, PD_CTRL_PS_RDY
)) {
3300 tcpm_set_state(port
, ERROR_RECOVERY
, 0);
3303 tcpm_set_state_cond(port
, SNK_UNATTACHED
, PD_T_PS_SOURCE_ON
);
3305 case PR_SWAP_SRC_SNK_SINK_ON
:
3306 tcpm_set_state(port
, SNK_STARTUP
, 0);
3308 case PR_SWAP_SNK_SRC_SINK_OFF
:
3309 tcpm_set_charge(port
, false);
3310 tcpm_set_state(port
, hard_reset_state(port
),
3311 PD_T_PS_SOURCE_OFF
);
3313 case PR_SWAP_SNK_SRC_SOURCE_ON
:
3314 tcpm_set_cc(port
, tcpm_rp_cc(port
));
3315 tcpm_set_vbus(port
, true);
3317 * allow time VBUS ramp-up, must be < tNewSrc
3318 * Also, this window overlaps with CC debounce as well.
3319 * So, Wait for the max of two which is PD_T_NEWSRC
3321 tcpm_set_state(port
, PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP
,
3324 case PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP
:
3326 * USB PD standard, 6.2.1.4:
3327 * "Subsequent Messages initiated by the Policy Engine,
3328 * such as the PS_RDY Message sent to indicate that Vbus
3329 * is ready, will have the Port Power Role field set to
3332 tcpm_set_pwr_role(port
, TYPEC_SOURCE
);
3333 tcpm_pd_send_control(port
, PD_CTRL_PS_RDY
);
3334 tcpm_set_state(port
, SRC_STARTUP
, 0);
3337 case VCONN_SWAP_ACCEPT
:
3338 tcpm_pd_send_control(port
, PD_CTRL_ACCEPT
);
3339 tcpm_set_state(port
, VCONN_SWAP_START
, 0);
3341 case VCONN_SWAP_SEND
:
3342 tcpm_pd_send_control(port
, PD_CTRL_VCONN_SWAP
);
3343 tcpm_set_state(port
, VCONN_SWAP_SEND_TIMEOUT
,
3344 PD_T_SENDER_RESPONSE
);
3346 case VCONN_SWAP_SEND_TIMEOUT
:
3347 tcpm_swap_complete(port
, -ETIMEDOUT
);
3348 tcpm_set_state(port
, ready_state(port
), 0);
3350 case VCONN_SWAP_START
:
3351 if (port
->vconn_role
== TYPEC_SOURCE
)
3352 tcpm_set_state(port
, VCONN_SWAP_WAIT_FOR_VCONN
, 0);
3354 tcpm_set_state(port
, VCONN_SWAP_TURN_ON_VCONN
, 0);
3356 case VCONN_SWAP_WAIT_FOR_VCONN
:
3357 tcpm_set_state(port
, hard_reset_state(port
),
3358 PD_T_VCONN_SOURCE_ON
);
3360 case VCONN_SWAP_TURN_ON_VCONN
:
3361 tcpm_set_vconn(port
, true);
3362 tcpm_pd_send_control(port
, PD_CTRL_PS_RDY
);
3363 tcpm_set_state(port
, ready_state(port
), 0);
3365 case VCONN_SWAP_TURN_OFF_VCONN
:
3366 tcpm_set_vconn(port
, false);
3367 tcpm_set_state(port
, ready_state(port
), 0);
3370 case DR_SWAP_CANCEL
:
3371 case PR_SWAP_CANCEL
:
3372 case VCONN_SWAP_CANCEL
:
3373 tcpm_swap_complete(port
, port
->swap_status
);
3374 if (port
->pwr_role
== TYPEC_SOURCE
)
3375 tcpm_set_state(port
, SRC_READY
, 0);
3377 tcpm_set_state(port
, SNK_READY
, 0);
3381 switch (BDO_MODE_MASK(port
->bist_request
)) {
3382 case BDO_MODE_CARRIER2
:
3383 tcpm_pd_transmit(port
, TCPC_TX_BIST_MODE_2
, NULL
);
3388 /* Always switch to unattached state */
3389 tcpm_set_state(port
, unattached_state(port
), 0);
3391 case GET_STATUS_SEND
:
3392 tcpm_pd_send_control(port
, PD_CTRL_GET_STATUS
);
3393 tcpm_set_state(port
, GET_STATUS_SEND_TIMEOUT
,
3394 PD_T_SENDER_RESPONSE
);
3396 case GET_STATUS_SEND_TIMEOUT
:
3397 tcpm_set_state(port
, ready_state(port
), 0);
3399 case GET_PPS_STATUS_SEND
:
3400 tcpm_pd_send_control(port
, PD_CTRL_GET_PPS_STATUS
);
3401 tcpm_set_state(port
, GET_PPS_STATUS_SEND_TIMEOUT
,
3402 PD_T_SENDER_RESPONSE
);
3404 case GET_PPS_STATUS_SEND_TIMEOUT
:
3405 tcpm_set_state(port
, ready_state(port
), 0);
3407 case ERROR_RECOVERY
:
3408 tcpm_swap_complete(port
, -EPROTO
);
3409 tcpm_pps_complete(port
, -EPROTO
);
3410 tcpm_set_state(port
, PORT_RESET
, 0);
3413 tcpm_reset_port(port
);
3414 tcpm_set_cc(port
, TYPEC_CC_OPEN
);
3415 tcpm_set_state(port
, PORT_RESET_WAIT_OFF
,
3416 PD_T_ERROR_RECOVERY
);
3418 case PORT_RESET_WAIT_OFF
:
3419 tcpm_set_state(port
,
3420 tcpm_default_state(port
),
3421 port
->vbus_present
? PD_T_PS_SOURCE_OFF
: 0);
3424 WARN(1, "Unexpected port state %d\n", port
->state
);
3429 static void tcpm_state_machine_work(struct work_struct
*work
)
3431 struct tcpm_port
*port
= container_of(work
, struct tcpm_port
,
3432 state_machine
.work
);
3433 enum tcpm_state prev_state
;
3435 mutex_lock(&port
->lock
);
3436 port
->state_machine_running
= true;
3438 if (port
->queued_message
&& tcpm_send_queued_message(port
))
3441 /* If we were queued due to a delayed state change, update it now */
3442 if (port
->delayed_state
) {
3443 tcpm_log(port
, "state change %s -> %s [delayed %ld ms]",
3444 tcpm_states
[port
->state
],
3445 tcpm_states
[port
->delayed_state
], port
->delay_ms
);
3446 port
->prev_state
= port
->state
;
3447 port
->state
= port
->delayed_state
;
3448 port
->delayed_state
= INVALID_STATE
;
3452 * Continue running as long as we have (non-delayed) state changes
3456 prev_state
= port
->state
;
3457 run_state_machine(port
);
3458 if (port
->queued_message
)
3459 tcpm_send_queued_message(port
);
3460 } while (port
->state
!= prev_state
&& !port
->delayed_state
);
3463 port
->state_machine_running
= false;
3464 mutex_unlock(&port
->lock
);
3467 static void _tcpm_cc_change(struct tcpm_port
*port
, enum typec_cc_status cc1
,
3468 enum typec_cc_status cc2
)
3470 enum typec_cc_status old_cc1
, old_cc2
;
3471 enum tcpm_state new_state
;
3473 old_cc1
= port
->cc1
;
3474 old_cc2
= port
->cc2
;
3478 tcpm_log_force(port
,
3479 "CC1: %u -> %u, CC2: %u -> %u [state %s, polarity %d, %s]",
3480 old_cc1
, cc1
, old_cc2
, cc2
, tcpm_states
[port
->state
],
3482 tcpm_port_is_disconnected(port
) ? "disconnected"
3485 switch (port
->state
) {
3487 if (tcpm_port_is_debug(port
) || tcpm_port_is_audio(port
) ||
3488 tcpm_port_is_source(port
))
3489 tcpm_set_state(port
, SRC_ATTACH_WAIT
, 0);
3490 else if (tcpm_port_is_sink(port
))
3491 tcpm_set_state(port
, SNK_ATTACH_WAIT
, 0);
3493 case SRC_UNATTACHED
:
3494 case ACC_UNATTACHED
:
3495 if (tcpm_port_is_debug(port
) || tcpm_port_is_audio(port
) ||
3496 tcpm_port_is_source(port
))
3497 tcpm_set_state(port
, SRC_ATTACH_WAIT
, 0);
3499 case SRC_ATTACH_WAIT
:
3500 if (tcpm_port_is_disconnected(port
) ||
3501 tcpm_port_is_audio_detached(port
))
3502 tcpm_set_state(port
, SRC_UNATTACHED
, 0);
3503 else if (cc1
!= old_cc1
|| cc2
!= old_cc2
)
3504 tcpm_set_state(port
, SRC_ATTACH_WAIT
, 0);
3507 case SRC_SEND_CAPABILITIES
:
3509 if (tcpm_port_is_disconnected(port
) ||
3510 !tcpm_port_is_source(port
))
3511 tcpm_set_state(port
, SRC_UNATTACHED
, 0);
3513 case SNK_UNATTACHED
:
3514 if (tcpm_port_is_sink(port
))
3515 tcpm_set_state(port
, SNK_ATTACH_WAIT
, 0);
3517 case SNK_ATTACH_WAIT
:
3518 if ((port
->cc1
== TYPEC_CC_OPEN
&&
3519 port
->cc2
!= TYPEC_CC_OPEN
) ||
3520 (port
->cc1
!= TYPEC_CC_OPEN
&&
3521 port
->cc2
== TYPEC_CC_OPEN
))
3522 new_state
= SNK_DEBOUNCED
;
3523 else if (tcpm_port_is_disconnected(port
))
3524 new_state
= SNK_UNATTACHED
;
3527 if (new_state
!= port
->delayed_state
)
3528 tcpm_set_state(port
, SNK_ATTACH_WAIT
, 0);
3531 if (tcpm_port_is_disconnected(port
))
3532 new_state
= SNK_UNATTACHED
;
3533 else if (port
->vbus_present
)
3534 new_state
= tcpm_try_src(port
) ? SRC_TRY
: SNK_ATTACHED
;
3536 new_state
= SNK_UNATTACHED
;
3537 if (new_state
!= port
->delayed_state
)
3538 tcpm_set_state(port
, SNK_DEBOUNCED
, 0);
3541 if (tcpm_port_is_disconnected(port
))
3542 tcpm_set_state(port
, unattached_state(port
), 0);
3543 else if (!port
->pd_capable
&&
3544 (cc1
!= old_cc1
|| cc2
!= old_cc2
))
3545 tcpm_set_current_limit(port
,
3546 tcpm_get_current_limit(port
),
3550 case AUDIO_ACC_ATTACHED
:
3551 if (cc1
== TYPEC_CC_OPEN
|| cc2
== TYPEC_CC_OPEN
)
3552 tcpm_set_state(port
, AUDIO_ACC_DEBOUNCE
, 0);
3554 case AUDIO_ACC_DEBOUNCE
:
3555 if (tcpm_port_is_audio(port
))
3556 tcpm_set_state(port
, AUDIO_ACC_ATTACHED
, 0);
3559 case DEBUG_ACC_ATTACHED
:
3560 if (cc1
== TYPEC_CC_OPEN
|| cc2
== TYPEC_CC_OPEN
)
3561 tcpm_set_state(port
, ACC_UNATTACHED
, 0);
3565 /* Do nothing, waiting for timeout */
3569 /* CC line is unstable, wait for debounce */
3570 if (tcpm_port_is_disconnected(port
))
3571 tcpm_set_state(port
, SNK_DISCOVERY_DEBOUNCE
, 0);
3573 case SNK_DISCOVERY_DEBOUNCE
:
3577 /* Hand over to state machine if needed */
3578 if (!port
->vbus_present
&& tcpm_port_is_source(port
))
3579 tcpm_set_state(port
, SRC_TRYWAIT_DEBOUNCE
, 0);
3581 case SRC_TRYWAIT_DEBOUNCE
:
3582 if (port
->vbus_present
|| !tcpm_port_is_source(port
))
3583 tcpm_set_state(port
, SRC_TRYWAIT
, 0);
3585 case SNK_TRY_WAIT_DEBOUNCE
:
3586 if (!tcpm_port_is_sink(port
)) {
3588 tcpm_set_state(port
, SRC_TRYWAIT
, 0);
3592 if (tcpm_port_is_source(port
))
3593 tcpm_set_state(port
, SRC_TRY_DEBOUNCE
, 0);
3595 case SRC_TRY_DEBOUNCE
:
3596 tcpm_set_state(port
, SRC_TRY_WAIT
, 0);
3598 case SNK_TRYWAIT_DEBOUNCE
:
3599 if (tcpm_port_is_sink(port
))
3600 tcpm_set_state(port
, SNK_TRYWAIT_VBUS
, 0);
3602 case SNK_TRYWAIT_VBUS
:
3603 if (!tcpm_port_is_sink(port
))
3604 tcpm_set_state(port
, SNK_TRYWAIT_DEBOUNCE
, 0);
3607 /* Do nothing, waiting for tCCDebounce */
3609 case PR_SWAP_SNK_SRC_SINK_OFF
:
3610 case PR_SWAP_SRC_SNK_TRANSITION_OFF
:
3611 case PR_SWAP_SRC_SNK_SOURCE_OFF
:
3612 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED
:
3613 case PR_SWAP_SNK_SRC_SOURCE_ON
:
3615 * CC state change is expected in PR_SWAP
3621 if (tcpm_port_is_disconnected(port
))
3622 tcpm_set_state(port
, unattached_state(port
), 0);
3627 static void _tcpm_pd_vbus_on(struct tcpm_port
*port
)
3629 tcpm_log_force(port
, "VBUS on");
3630 port
->vbus_present
= true;
3631 switch (port
->state
) {
3632 case SNK_TRANSITION_SINK_VBUS
:
3633 port
->explicit_contract
= true;
3634 tcpm_set_state(port
, SNK_READY
, 0);
3637 tcpm_set_state(port
, SNK_DISCOVERY
, 0);
3641 tcpm_set_state(port
, tcpm_try_src(port
) ? SRC_TRY
3645 case SNK_HARD_RESET_WAIT_VBUS
:
3646 tcpm_set_state(port
, SNK_HARD_RESET_SINK_ON
, 0);
3649 tcpm_set_state(port
, SRC_STARTUP
, 0);
3651 case SRC_HARD_RESET_VBUS_ON
:
3652 tcpm_set_state(port
, SRC_STARTUP
, 0);
3656 /* Do nothing, waiting for timeout */
3659 /* Do nothing, Waiting for Rd to be detected */
3661 case SRC_TRYWAIT_DEBOUNCE
:
3662 tcpm_set_state(port
, SRC_TRYWAIT
, 0);
3664 case SNK_TRY_WAIT_DEBOUNCE
:
3665 /* Do nothing, waiting for PD_DEBOUNCE to do be done */
3668 /* Do nothing, waiting for tCCDebounce */
3670 case SNK_TRYWAIT_VBUS
:
3671 if (tcpm_port_is_sink(port
))
3672 tcpm_set_state(port
, SNK_ATTACHED
, 0);
3674 case SNK_TRYWAIT_DEBOUNCE
:
3675 /* Do nothing, waiting for Rp */
3678 case SRC_TRY_DEBOUNCE
:
3679 /* Do nothing, waiting for sink detection */
3686 static void _tcpm_pd_vbus_off(struct tcpm_port
*port
)
3688 tcpm_log_force(port
, "VBUS off");
3689 port
->vbus_present
= false;
3690 port
->vbus_never_low
= false;
3691 switch (port
->state
) {
3692 case SNK_HARD_RESET_SINK_OFF
:
3693 tcpm_set_state(port
, SNK_HARD_RESET_WAIT_VBUS
, 0);
3695 case SRC_HARD_RESET_VBUS_OFF
:
3696 tcpm_set_state(port
, SRC_HARD_RESET_VBUS_ON
, 0);
3698 case HARD_RESET_SEND
:
3702 /* Do nothing, waiting for timeout */
3705 /* Hand over to state machine if needed */
3706 if (tcpm_port_is_source(port
))
3707 tcpm_set_state(port
, SRC_TRYWAIT_DEBOUNCE
, 0);
3709 case SNK_TRY_WAIT_DEBOUNCE
:
3710 /* Do nothing, waiting for PD_DEBOUNCE to do be done */
3713 case SNK_TRYWAIT_VBUS
:
3714 case SNK_TRYWAIT_DEBOUNCE
:
3716 case SNK_ATTACH_WAIT
:
3717 tcpm_set_state(port
, SNK_UNATTACHED
, 0);
3720 case SNK_NEGOTIATE_CAPABILITIES
:
3723 case PR_SWAP_SRC_SNK_TRANSITION_OFF
:
3724 tcpm_set_state(port
, PR_SWAP_SRC_SNK_SOURCE_OFF
, 0);
3727 case PR_SWAP_SNK_SRC_SINK_OFF
:
3728 /* Do nothing, expected */
3731 case PORT_RESET_WAIT_OFF
:
3732 tcpm_set_state(port
, tcpm_default_state(port
), 0);
3735 case SRC_TRY_DEBOUNCE
:
3736 /* Do nothing, waiting for sink detection */
3739 if (port
->pwr_role
== TYPEC_SINK
&&
3741 tcpm_set_state(port
, SNK_UNATTACHED
, 0);
3746 static void _tcpm_pd_hard_reset(struct tcpm_port
*port
)
3748 tcpm_log_force(port
, "Received hard reset");
3750 * If we keep receiving hard reset requests, executing the hard reset
3751 * must have failed. Revert to error recovery if that happens.
3753 tcpm_set_state(port
,
3754 port
->hard_reset_count
< PD_N_HARD_RESET_COUNT
?
3755 HARD_RESET_START
: ERROR_RECOVERY
,
3759 static void tcpm_pd_event_handler(struct work_struct
*work
)
3761 struct tcpm_port
*port
= container_of(work
, struct tcpm_port
,
3765 mutex_lock(&port
->lock
);
3767 spin_lock(&port
->pd_event_lock
);
3768 while (port
->pd_events
) {
3769 events
= port
->pd_events
;
3770 port
->pd_events
= 0;
3771 spin_unlock(&port
->pd_event_lock
);
3772 if (events
& TCPM_RESET_EVENT
)
3773 _tcpm_pd_hard_reset(port
);
3774 if (events
& TCPM_VBUS_EVENT
) {
3777 vbus
= port
->tcpc
->get_vbus(port
->tcpc
);
3779 _tcpm_pd_vbus_on(port
);
3781 _tcpm_pd_vbus_off(port
);
3783 if (events
& TCPM_CC_EVENT
) {
3784 enum typec_cc_status cc1
, cc2
;
3786 if (port
->tcpc
->get_cc(port
->tcpc
, &cc1
, &cc2
) == 0)
3787 _tcpm_cc_change(port
, cc1
, cc2
);
3789 spin_lock(&port
->pd_event_lock
);
3791 spin_unlock(&port
->pd_event_lock
);
3792 mutex_unlock(&port
->lock
);
3795 void tcpm_cc_change(struct tcpm_port
*port
)
3797 spin_lock(&port
->pd_event_lock
);
3798 port
->pd_events
|= TCPM_CC_EVENT
;
3799 spin_unlock(&port
->pd_event_lock
);
3800 queue_work(port
->wq
, &port
->event_work
);
3802 EXPORT_SYMBOL_GPL(tcpm_cc_change
);
3804 void tcpm_vbus_change(struct tcpm_port
*port
)
3806 spin_lock(&port
->pd_event_lock
);
3807 port
->pd_events
|= TCPM_VBUS_EVENT
;
3808 spin_unlock(&port
->pd_event_lock
);
3809 queue_work(port
->wq
, &port
->event_work
);
3811 EXPORT_SYMBOL_GPL(tcpm_vbus_change
);
3813 void tcpm_pd_hard_reset(struct tcpm_port
*port
)
3815 spin_lock(&port
->pd_event_lock
);
3816 port
->pd_events
= TCPM_RESET_EVENT
;
3817 spin_unlock(&port
->pd_event_lock
);
3818 queue_work(port
->wq
, &port
->event_work
);
3820 EXPORT_SYMBOL_GPL(tcpm_pd_hard_reset
);
3822 static int tcpm_dr_set(const struct typec_capability
*cap
,
3823 enum typec_data_role data
)
3825 struct tcpm_port
*port
= typec_cap_to_tcpm(cap
);
3828 mutex_lock(&port
->swap_lock
);
3829 mutex_lock(&port
->lock
);
3831 if (port
->port_type
!= TYPEC_PORT_DRP
) {
3835 if (port
->state
!= SRC_READY
&& port
->state
!= SNK_READY
) {
3840 if (port
->data_role
== data
) {
3847 * 6.3.9: If an alternate mode is active, a request to swap
3848 * alternate modes shall trigger a port reset.
3849 * Reject data role swap request in this case.
3852 if (!port
->pd_capable
) {
3854 * If the partner is not PD capable, reset the port to
3855 * trigger a role change. This can only work if a preferred
3856 * role is configured, and if it matches the requested role.
3858 if (port
->try_role
== TYPEC_NO_PREFERRED_ROLE
||
3859 port
->try_role
== port
->pwr_role
) {
3863 port
->non_pd_role_swap
= true;
3864 tcpm_set_state(port
, PORT_RESET
, 0);
3866 tcpm_set_state(port
, DR_SWAP_SEND
, 0);
3869 port
->swap_status
= 0;
3870 port
->swap_pending
= true;
3871 reinit_completion(&port
->swap_complete
);
3872 mutex_unlock(&port
->lock
);
3874 if (!wait_for_completion_timeout(&port
->swap_complete
,
3875 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT
)))
3878 ret
= port
->swap_status
;
3880 port
->non_pd_role_swap
= false;
3884 mutex_unlock(&port
->lock
);
3886 mutex_unlock(&port
->swap_lock
);
3890 static int tcpm_pr_set(const struct typec_capability
*cap
,
3891 enum typec_role role
)
3893 struct tcpm_port
*port
= typec_cap_to_tcpm(cap
);
3896 mutex_lock(&port
->swap_lock
);
3897 mutex_lock(&port
->lock
);
3899 if (port
->port_type
!= TYPEC_PORT_DRP
) {
3903 if (port
->state
!= SRC_READY
&& port
->state
!= SNK_READY
) {
3908 if (role
== port
->pwr_role
) {
3913 port
->swap_status
= 0;
3914 port
->swap_pending
= true;
3915 reinit_completion(&port
->swap_complete
);
3916 tcpm_set_state(port
, PR_SWAP_SEND
, 0);
3917 mutex_unlock(&port
->lock
);
3919 if (!wait_for_completion_timeout(&port
->swap_complete
,
3920 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT
)))
3923 ret
= port
->swap_status
;
3928 mutex_unlock(&port
->lock
);
3930 mutex_unlock(&port
->swap_lock
);
3934 static int tcpm_vconn_set(const struct typec_capability
*cap
,
3935 enum typec_role role
)
3937 struct tcpm_port
*port
= typec_cap_to_tcpm(cap
);
3940 mutex_lock(&port
->swap_lock
);
3941 mutex_lock(&port
->lock
);
3943 if (port
->state
!= SRC_READY
&& port
->state
!= SNK_READY
) {
3948 if (role
== port
->vconn_role
) {
3953 port
->swap_status
= 0;
3954 port
->swap_pending
= true;
3955 reinit_completion(&port
->swap_complete
);
3956 tcpm_set_state(port
, VCONN_SWAP_SEND
, 0);
3957 mutex_unlock(&port
->lock
);
3959 if (!wait_for_completion_timeout(&port
->swap_complete
,
3960 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT
)))
3963 ret
= port
->swap_status
;
3968 mutex_unlock(&port
->lock
);
3970 mutex_unlock(&port
->swap_lock
);
3974 static int tcpm_try_role(const struct typec_capability
*cap
, int role
)
3976 struct tcpm_port
*port
= typec_cap_to_tcpm(cap
);
3977 struct tcpc_dev
*tcpc
= port
->tcpc
;
3980 mutex_lock(&port
->lock
);
3982 ret
= tcpc
->try_role(tcpc
, role
);
3983 if (!ret
&& !tcpc
->config
->try_role_hw
)
3984 port
->try_role
= role
;
3985 port
->try_src_count
= 0;
3986 port
->try_snk_count
= 0;
3987 mutex_unlock(&port
->lock
);
3992 static int tcpm_pps_set_op_curr(struct tcpm_port
*port
, u16 op_curr
)
3994 unsigned int target_mw
;
3997 mutex_lock(&port
->swap_lock
);
3998 mutex_lock(&port
->lock
);
4000 if (!port
->pps_data
.active
) {
4005 if (port
->state
!= SNK_READY
) {
4010 if (op_curr
> port
->pps_data
.max_curr
) {
4015 target_mw
= (op_curr
* port
->pps_data
.out_volt
) / 1000;
4016 if (target_mw
< port
->operating_snk_mw
) {
4021 reinit_completion(&port
->pps_complete
);
4022 port
->pps_data
.op_curr
= op_curr
;
4023 port
->pps_status
= 0;
4024 port
->pps_pending
= true;
4025 tcpm_set_state(port
, SNK_NEGOTIATE_PPS_CAPABILITIES
, 0);
4026 mutex_unlock(&port
->lock
);
4028 if (!wait_for_completion_timeout(&port
->pps_complete
,
4029 msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT
)))
4032 ret
= port
->pps_status
;
4037 mutex_unlock(&port
->lock
);
4039 mutex_unlock(&port
->swap_lock
);
4044 static int tcpm_pps_set_out_volt(struct tcpm_port
*port
, u16 out_volt
)
4046 unsigned int target_mw
;
4049 mutex_lock(&port
->swap_lock
);
4050 mutex_lock(&port
->lock
);
4052 if (!port
->pps_data
.active
) {
4057 if (port
->state
!= SNK_READY
) {
4062 if (out_volt
< port
->pps_data
.min_volt
||
4063 out_volt
> port
->pps_data
.max_volt
) {
4068 target_mw
= (port
->pps_data
.op_curr
* out_volt
) / 1000;
4069 if (target_mw
< port
->operating_snk_mw
) {
4074 reinit_completion(&port
->pps_complete
);
4075 port
->pps_data
.out_volt
= out_volt
;
4076 port
->pps_status
= 0;
4077 port
->pps_pending
= true;
4078 tcpm_set_state(port
, SNK_NEGOTIATE_PPS_CAPABILITIES
, 0);
4079 mutex_unlock(&port
->lock
);
4081 if (!wait_for_completion_timeout(&port
->pps_complete
,
4082 msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT
)))
4085 ret
= port
->pps_status
;
4090 mutex_unlock(&port
->lock
);
4092 mutex_unlock(&port
->swap_lock
);
4097 static int tcpm_pps_activate(struct tcpm_port
*port
, bool activate
)
4101 mutex_lock(&port
->swap_lock
);
4102 mutex_lock(&port
->lock
);
4104 if (!port
->pps_data
.supported
) {
4109 /* Trying to deactivate PPS when already deactivated so just bail */
4110 if (!port
->pps_data
.active
&& !activate
)
4113 if (port
->state
!= SNK_READY
) {
4118 reinit_completion(&port
->pps_complete
);
4119 port
->pps_status
= 0;
4120 port
->pps_pending
= true;
4122 /* Trigger PPS request or move back to standard PDO contract */
4124 port
->pps_data
.out_volt
= port
->supply_voltage
;
4125 port
->pps_data
.op_curr
= port
->current_limit
;
4126 tcpm_set_state(port
, SNK_NEGOTIATE_PPS_CAPABILITIES
, 0);
4128 tcpm_set_state(port
, SNK_NEGOTIATE_CAPABILITIES
, 0);
4130 mutex_unlock(&port
->lock
);
4132 if (!wait_for_completion_timeout(&port
->pps_complete
,
4133 msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT
)))
4136 ret
= port
->pps_status
;
4141 mutex_unlock(&port
->lock
);
4143 mutex_unlock(&port
->swap_lock
);
4148 static void tcpm_init(struct tcpm_port
*port
)
4150 enum typec_cc_status cc1
, cc2
;
4152 port
->tcpc
->init(port
->tcpc
);
4154 tcpm_reset_port(port
);
4158 * Should possibly wait for VBUS to settle if it was enabled locally
4159 * since tcpm_reset_port() will disable VBUS.
4161 port
->vbus_present
= port
->tcpc
->get_vbus(port
->tcpc
);
4162 if (port
->vbus_present
)
4163 port
->vbus_never_low
= true;
4165 tcpm_set_state(port
, tcpm_default_state(port
), 0);
4167 if (port
->tcpc
->get_cc(port
->tcpc
, &cc1
, &cc2
) == 0)
4168 _tcpm_cc_change(port
, cc1
, cc2
);
4171 * Some adapters need a clean slate at startup, and won't recover
4172 * otherwise. So do not try to be fancy and force a clean disconnect.
4174 tcpm_set_state(port
, PORT_RESET
, 0);
4177 static int tcpm_port_type_set(const struct typec_capability
*cap
,
4178 enum typec_port_type type
)
4180 struct tcpm_port
*port
= typec_cap_to_tcpm(cap
);
4182 mutex_lock(&port
->lock
);
4183 if (type
== port
->port_type
)
4186 port
->port_type
= type
;
4188 if (!port
->connected
) {
4189 tcpm_set_state(port
, PORT_RESET
, 0);
4190 } else if (type
== TYPEC_PORT_SNK
) {
4191 if (!(port
->pwr_role
== TYPEC_SINK
&&
4192 port
->data_role
== TYPEC_DEVICE
))
4193 tcpm_set_state(port
, PORT_RESET
, 0);
4194 } else if (type
== TYPEC_PORT_SRC
) {
4195 if (!(port
->pwr_role
== TYPEC_SOURCE
&&
4196 port
->data_role
== TYPEC_HOST
))
4197 tcpm_set_state(port
, PORT_RESET
, 0);
4201 mutex_unlock(&port
->lock
);
4205 void tcpm_tcpc_reset(struct tcpm_port
*port
)
4207 mutex_lock(&port
->lock
);
4208 /* XXX: Maintain PD connection if possible? */
4210 mutex_unlock(&port
->lock
);
4212 EXPORT_SYMBOL_GPL(tcpm_tcpc_reset
);
4214 static int tcpm_copy_pdos(u32
*dest_pdo
, const u32
*src_pdo
,
4215 unsigned int nr_pdo
)
4219 if (nr_pdo
> PDO_MAX_OBJECTS
)
4220 nr_pdo
= PDO_MAX_OBJECTS
;
4222 for (i
= 0; i
< nr_pdo
; i
++)
4223 dest_pdo
[i
] = src_pdo
[i
];
4228 static int tcpm_copy_vdos(u32
*dest_vdo
, const u32
*src_vdo
,
4229 unsigned int nr_vdo
)
4233 if (nr_vdo
> VDO_MAX_OBJECTS
)
4234 nr_vdo
= VDO_MAX_OBJECTS
;
4236 for (i
= 0; i
< nr_vdo
; i
++)
4237 dest_vdo
[i
] = src_vdo
[i
];
4242 int tcpm_update_source_capabilities(struct tcpm_port
*port
, const u32
*pdo
,
4243 unsigned int nr_pdo
)
4245 if (tcpm_validate_caps(port
, pdo
, nr_pdo
))
4248 mutex_lock(&port
->lock
);
4249 port
->nr_src_pdo
= tcpm_copy_pdos(port
->src_pdo
, pdo
, nr_pdo
);
4250 switch (port
->state
) {
4251 case SRC_UNATTACHED
:
4252 case SRC_ATTACH_WAIT
:
4254 tcpm_set_cc(port
, tcpm_rp_cc(port
));
4256 case SRC_SEND_CAPABILITIES
:
4257 case SRC_NEGOTIATE_CAPABILITIES
:
4259 case SRC_WAIT_NEW_CAPABILITIES
:
4260 tcpm_set_cc(port
, tcpm_rp_cc(port
));
4261 tcpm_set_state(port
, SRC_SEND_CAPABILITIES
, 0);
4266 mutex_unlock(&port
->lock
);
4269 EXPORT_SYMBOL_GPL(tcpm_update_source_capabilities
);
4271 int tcpm_update_sink_capabilities(struct tcpm_port
*port
, const u32
*pdo
,
4272 unsigned int nr_pdo
,
4273 unsigned int operating_snk_mw
)
4275 if (tcpm_validate_caps(port
, pdo
, nr_pdo
))
4278 mutex_lock(&port
->lock
);
4279 port
->nr_snk_pdo
= tcpm_copy_pdos(port
->snk_pdo
, pdo
, nr_pdo
);
4280 port
->operating_snk_mw
= operating_snk_mw
;
4281 port
->update_sink_caps
= true;
4283 switch (port
->state
) {
4284 case SNK_NEGOTIATE_CAPABILITIES
:
4285 case SNK_NEGOTIATE_PPS_CAPABILITIES
:
4287 case SNK_TRANSITION_SINK
:
4288 case SNK_TRANSITION_SINK_VBUS
:
4289 if (port
->pps_data
.active
)
4290 tcpm_set_state(port
, SNK_NEGOTIATE_PPS_CAPABILITIES
, 0);
4292 tcpm_set_state(port
, SNK_NEGOTIATE_CAPABILITIES
, 0);
4297 mutex_unlock(&port
->lock
);
4300 EXPORT_SYMBOL_GPL(tcpm_update_sink_capabilities
);
4302 /* Power Supply access to expose source power information */
4303 enum tcpm_psy_online_states
{
4304 TCPM_PSY_OFFLINE
= 0,
4305 TCPM_PSY_FIXED_ONLINE
,
4306 TCPM_PSY_PROG_ONLINE
,
4309 static enum power_supply_property tcpm_psy_props
[] = {
4310 POWER_SUPPLY_PROP_USB_TYPE
,
4311 POWER_SUPPLY_PROP_ONLINE
,
4312 POWER_SUPPLY_PROP_VOLTAGE_MIN
,
4313 POWER_SUPPLY_PROP_VOLTAGE_MAX
,
4314 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
4315 POWER_SUPPLY_PROP_CURRENT_MAX
,
4316 POWER_SUPPLY_PROP_CURRENT_NOW
,
4319 static int tcpm_psy_get_online(struct tcpm_port
*port
,
4320 union power_supply_propval
*val
)
4322 if (port
->vbus_charge
) {
4323 if (port
->pps_data
.active
)
4324 val
->intval
= TCPM_PSY_PROG_ONLINE
;
4326 val
->intval
= TCPM_PSY_FIXED_ONLINE
;
4328 val
->intval
= TCPM_PSY_OFFLINE
;
4334 static int tcpm_psy_get_voltage_min(struct tcpm_port
*port
,
4335 union power_supply_propval
*val
)
4337 if (port
->pps_data
.active
)
4338 val
->intval
= port
->pps_data
.min_volt
* 1000;
4340 val
->intval
= port
->supply_voltage
* 1000;
4345 static int tcpm_psy_get_voltage_max(struct tcpm_port
*port
,
4346 union power_supply_propval
*val
)
4348 if (port
->pps_data
.active
)
4349 val
->intval
= port
->pps_data
.max_volt
* 1000;
4351 val
->intval
= port
->supply_voltage
* 1000;
4356 static int tcpm_psy_get_voltage_now(struct tcpm_port
*port
,
4357 union power_supply_propval
*val
)
4359 val
->intval
= port
->supply_voltage
* 1000;
4364 static int tcpm_psy_get_current_max(struct tcpm_port
*port
,
4365 union power_supply_propval
*val
)
4367 if (port
->pps_data
.active
)
4368 val
->intval
= port
->pps_data
.max_curr
* 1000;
4370 val
->intval
= port
->current_limit
* 1000;
4375 static int tcpm_psy_get_current_now(struct tcpm_port
*port
,
4376 union power_supply_propval
*val
)
4378 val
->intval
= port
->current_limit
* 1000;
4383 static int tcpm_psy_get_prop(struct power_supply
*psy
,
4384 enum power_supply_property psp
,
4385 union power_supply_propval
*val
)
4387 struct tcpm_port
*port
= power_supply_get_drvdata(psy
);
4391 case POWER_SUPPLY_PROP_USB_TYPE
:
4392 val
->intval
= port
->usb_type
;
4394 case POWER_SUPPLY_PROP_ONLINE
:
4395 ret
= tcpm_psy_get_online(port
, val
);
4397 case POWER_SUPPLY_PROP_VOLTAGE_MIN
:
4398 ret
= tcpm_psy_get_voltage_min(port
, val
);
4400 case POWER_SUPPLY_PROP_VOLTAGE_MAX
:
4401 ret
= tcpm_psy_get_voltage_max(port
, val
);
4403 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
4404 ret
= tcpm_psy_get_voltage_now(port
, val
);
4406 case POWER_SUPPLY_PROP_CURRENT_MAX
:
4407 ret
= tcpm_psy_get_current_max(port
, val
);
4409 case POWER_SUPPLY_PROP_CURRENT_NOW
:
4410 ret
= tcpm_psy_get_current_now(port
, val
);
4420 static int tcpm_psy_set_online(struct tcpm_port
*port
,
4421 const union power_supply_propval
*val
)
4425 switch (val
->intval
) {
4426 case TCPM_PSY_FIXED_ONLINE
:
4427 ret
= tcpm_pps_activate(port
, false);
4429 case TCPM_PSY_PROG_ONLINE
:
4430 ret
= tcpm_pps_activate(port
, true);
4440 static int tcpm_psy_set_prop(struct power_supply
*psy
,
4441 enum power_supply_property psp
,
4442 const union power_supply_propval
*val
)
4444 struct tcpm_port
*port
= power_supply_get_drvdata(psy
);
4448 case POWER_SUPPLY_PROP_ONLINE
:
4449 ret
= tcpm_psy_set_online(port
, val
);
4451 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
4452 if (val
->intval
< port
->pps_data
.min_volt
* 1000 ||
4453 val
->intval
> port
->pps_data
.max_volt
* 1000)
4456 ret
= tcpm_pps_set_out_volt(port
, val
->intval
/ 1000);
4458 case POWER_SUPPLY_PROP_CURRENT_NOW
:
4459 if (val
->intval
> port
->pps_data
.max_curr
* 1000)
4462 ret
= tcpm_pps_set_op_curr(port
, val
->intval
/ 1000);
4472 static int tcpm_psy_prop_writeable(struct power_supply
*psy
,
4473 enum power_supply_property psp
)
4476 case POWER_SUPPLY_PROP_ONLINE
:
4477 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
4478 case POWER_SUPPLY_PROP_CURRENT_NOW
:
4485 static enum power_supply_usb_type tcpm_psy_usb_types
[] = {
4486 POWER_SUPPLY_USB_TYPE_C
,
4487 POWER_SUPPLY_USB_TYPE_PD
,
4488 POWER_SUPPLY_USB_TYPE_PD_PPS
,
4491 static const char *tcpm_psy_name_prefix
= "tcpm-source-psy-";
4493 static int devm_tcpm_psy_register(struct tcpm_port
*port
)
4495 struct power_supply_config psy_cfg
= {};
4496 const char *port_dev_name
= dev_name(port
->dev
);
4497 size_t psy_name_len
= strlen(tcpm_psy_name_prefix
) +
4498 strlen(port_dev_name
) + 1;
4501 psy_cfg
.drv_data
= port
;
4502 psy_cfg
.fwnode
= dev_fwnode(port
->dev
);
4503 psy_name
= devm_kzalloc(port
->dev
, psy_name_len
, GFP_KERNEL
);
4507 snprintf(psy_name
, psy_name_len
, "%s%s", tcpm_psy_name_prefix
,
4509 port
->psy_desc
.name
= psy_name
;
4510 port
->psy_desc
.type
= POWER_SUPPLY_TYPE_USB
,
4511 port
->psy_desc
.usb_types
= tcpm_psy_usb_types
;
4512 port
->psy_desc
.num_usb_types
= ARRAY_SIZE(tcpm_psy_usb_types
);
4513 port
->psy_desc
.properties
= tcpm_psy_props
,
4514 port
->psy_desc
.num_properties
= ARRAY_SIZE(tcpm_psy_props
),
4515 port
->psy_desc
.get_property
= tcpm_psy_get_prop
,
4516 port
->psy_desc
.set_property
= tcpm_psy_set_prop
,
4517 port
->psy_desc
.property_is_writeable
= tcpm_psy_prop_writeable
,
4519 port
->usb_type
= POWER_SUPPLY_USB_TYPE_C
;
4521 port
->psy
= devm_power_supply_register(port
->dev
, &port
->psy_desc
,
4524 return PTR_ERR_OR_ZERO(port
->psy
);
4527 struct tcpm_port
*tcpm_register_port(struct device
*dev
, struct tcpc_dev
*tcpc
)
4529 struct tcpm_port
*port
;
4532 if (!dev
|| !tcpc
|| !tcpc
->config
||
4533 !tcpc
->get_vbus
|| !tcpc
->set_cc
|| !tcpc
->get_cc
||
4534 !tcpc
->set_polarity
|| !tcpc
->set_vconn
|| !tcpc
->set_vbus
||
4535 !tcpc
->set_pd_rx
|| !tcpc
->set_roles
|| !tcpc
->pd_transmit
)
4536 return ERR_PTR(-EINVAL
);
4538 port
= devm_kzalloc(dev
, sizeof(*port
), GFP_KERNEL
);
4540 return ERR_PTR(-ENOMEM
);
4545 mutex_init(&port
->lock
);
4546 mutex_init(&port
->swap_lock
);
4548 port
->wq
= create_singlethread_workqueue(dev_name(dev
));
4550 return ERR_PTR(-ENOMEM
);
4551 INIT_DELAYED_WORK(&port
->state_machine
, tcpm_state_machine_work
);
4552 INIT_DELAYED_WORK(&port
->vdm_state_machine
, vdm_state_machine_work
);
4553 INIT_WORK(&port
->event_work
, tcpm_pd_event_handler
);
4555 spin_lock_init(&port
->pd_event_lock
);
4557 init_completion(&port
->tx_complete
);
4558 init_completion(&port
->swap_complete
);
4559 init_completion(&port
->pps_complete
);
4560 tcpm_debugfs_init(port
);
4562 if (tcpm_validate_caps(port
, tcpc
->config
->src_pdo
,
4563 tcpc
->config
->nr_src_pdo
) ||
4564 tcpm_validate_caps(port
, tcpc
->config
->snk_pdo
,
4565 tcpc
->config
->nr_snk_pdo
)) {
4567 goto out_destroy_wq
;
4569 port
->nr_src_pdo
= tcpm_copy_pdos(port
->src_pdo
, tcpc
->config
->src_pdo
,
4570 tcpc
->config
->nr_src_pdo
);
4571 port
->nr_snk_pdo
= tcpm_copy_pdos(port
->snk_pdo
, tcpc
->config
->snk_pdo
,
4572 tcpc
->config
->nr_snk_pdo
);
4573 port
->nr_snk_vdo
= tcpm_copy_vdos(port
->snk_vdo
, tcpc
->config
->snk_vdo
,
4574 tcpc
->config
->nr_snk_vdo
);
4576 port
->operating_snk_mw
= tcpc
->config
->operating_snk_mw
;
4577 if (!tcpc
->config
->try_role_hw
)
4578 port
->try_role
= tcpc
->config
->default_role
;
4580 port
->try_role
= TYPEC_NO_PREFERRED_ROLE
;
4582 port
->typec_caps
.prefer_role
= tcpc
->config
->default_role
;
4583 port
->typec_caps
.type
= tcpc
->config
->type
;
4584 port
->typec_caps
.data
= tcpc
->config
->data
;
4585 port
->typec_caps
.revision
= 0x0120; /* Type-C spec release 1.2 */
4586 port
->typec_caps
.pd_revision
= 0x0300; /* USB-PD spec release 3.0 */
4587 port
->typec_caps
.dr_set
= tcpm_dr_set
;
4588 port
->typec_caps
.pr_set
= tcpm_pr_set
;
4589 port
->typec_caps
.vconn_set
= tcpm_vconn_set
;
4590 port
->typec_caps
.try_role
= tcpm_try_role
;
4591 port
->typec_caps
.port_type_set
= tcpm_port_type_set
;
4593 port
->partner_desc
.identity
= &port
->partner_ident
;
4594 port
->port_type
= tcpc
->config
->type
;
4596 port
->role_sw
= usb_role_switch_get(port
->dev
);
4597 if (IS_ERR(port
->role_sw
)) {
4598 err
= PTR_ERR(port
->role_sw
);
4599 goto out_destroy_wq
;
4602 err
= devm_tcpm_psy_register(port
);
4604 goto out_destroy_wq
;
4606 port
->typec_port
= typec_register_port(port
->dev
, &port
->typec_caps
);
4607 if (IS_ERR(port
->typec_port
)) {
4608 err
= PTR_ERR(port
->typec_port
);
4609 goto out_destroy_wq
;
4612 if (tcpc
->config
->alt_modes
) {
4613 const struct typec_altmode_desc
*paltmode
= tcpc
->config
->alt_modes
;
4616 while (paltmode
->svid
&& i
< ARRAY_SIZE(port
->port_altmode
)) {
4617 struct typec_altmode
*alt
;
4619 alt
= typec_port_register_altmode(port
->typec_port
,
4623 "%s: failed to register port alternate mode 0x%x",
4624 dev_name(dev
), paltmode
->svid
);
4627 port
->port_altmode
[i
] = alt
;
4633 mutex_lock(&port
->lock
);
4635 mutex_unlock(&port
->lock
);
4637 tcpm_log(port
, "%s: registered", dev_name(dev
));
4641 usb_role_switch_put(port
->role_sw
);
4642 destroy_workqueue(port
->wq
);
4643 return ERR_PTR(err
);
4645 EXPORT_SYMBOL_GPL(tcpm_register_port
);
4647 void tcpm_unregister_port(struct tcpm_port
*port
)
4651 tcpm_reset_port(port
);
4652 for (i
= 0; i
< ARRAY_SIZE(port
->port_altmode
); i
++)
4653 typec_unregister_altmode(port
->port_altmode
[i
]);
4654 typec_unregister_port(port
->typec_port
);
4655 usb_role_switch_put(port
->role_sw
);
4656 tcpm_debugfs_exit(port
);
4657 destroy_workqueue(port
->wq
);
4659 EXPORT_SYMBOL_GPL(tcpm_unregister_port
);
4661 MODULE_AUTHOR("Guenter Roeck <groeck@chromium.org>");
4662 MODULE_DESCRIPTION("USB Type-C Port Manager");
4663 MODULE_LICENSE("GPL");