Commit | Line | Data |
---|---|---|
956c36c2 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
f0690a25 GR |
2 | /* |
3 | * Copyright 2015-2017 Google, Inc | |
4 | * | |
f0690a25 GR |
5 | * USB Power Delivery protocol stack. |
6 | */ | |
7 | ||
8 | #include <linux/completion.h> | |
9 | #include <linux/debugfs.h> | |
10 | #include <linux/device.h> | |
3ed8e1c2 | 11 | #include <linux/hrtimer.h> |
02d5be46 | 12 | #include <linux/jiffies.h> |
f0690a25 | 13 | #include <linux/kernel.h> |
3ed8e1c2 | 14 | #include <linux/kthread.h> |
f0690a25 GR |
15 | #include <linux/module.h> |
16 | #include <linux/mutex.h> | |
f2a8aa05 | 17 | #include <linux/power_supply.h> |
f0690a25 | 18 | #include <linux/proc_fs.h> |
c97a8cc1 | 19 | #include <linux/property.h> |
f0690a25 GR |
20 | #include <linux/sched/clock.h> |
21 | #include <linux/seq_file.h> | |
22 | #include <linux/slab.h> | |
23 | #include <linux/spinlock.h> | |
9f4bc2ed | 24 | #include <linux/usb.h> |
4b4e02c8 | 25 | #include <linux/usb/pd.h> |
64f7c494 | 26 | #include <linux/usb/pd_ado.h> |
4b4e02c8 | 27 | #include <linux/usb/pd_bdo.h> |
64f7c494 | 28 | #include <linux/usb/pd_ext_sdb.h> |
4b4e02c8 | 29 | #include <linux/usb/pd_vdo.h> |
2000016c | 30 | #include <linux/usb/role.h> |
4b4e02c8 | 31 | #include <linux/usb/tcpm.h> |
e9576fe8 | 32 | #include <linux/usb/typec_altmode.h> |
3ed8e1c2 BJS |
33 | |
34 | #include <uapi/linux/sched/types.h> | |
f0690a25 | 35 | |
f0690a25 GR |
36 | #define FOREACH_STATE(S) \ |
37 | S(INVALID_STATE), \ | |
7893f9e1 | 38 | S(TOGGLING), \ |
f0690a25 GR |
39 | S(SRC_UNATTACHED), \ |
40 | S(SRC_ATTACH_WAIT), \ | |
41 | S(SRC_ATTACHED), \ | |
42 | S(SRC_STARTUP), \ | |
43 | S(SRC_SEND_CAPABILITIES), \ | |
976daf9d | 44 | S(SRC_SEND_CAPABILITIES_TIMEOUT), \ |
f0690a25 GR |
45 | S(SRC_NEGOTIATE_CAPABILITIES), \ |
46 | S(SRC_TRANSITION_SUPPLY), \ | |
47 | S(SRC_READY), \ | |
48 | S(SRC_WAIT_NEW_CAPABILITIES), \ | |
49 | \ | |
50 | S(SNK_UNATTACHED), \ | |
51 | S(SNK_ATTACH_WAIT), \ | |
52 | S(SNK_DEBOUNCED), \ | |
53 | S(SNK_ATTACHED), \ | |
54 | S(SNK_STARTUP), \ | |
55 | S(SNK_DISCOVERY), \ | |
56 | S(SNK_DISCOVERY_DEBOUNCE), \ | |
57 | S(SNK_DISCOVERY_DEBOUNCE_DONE), \ | |
58 | S(SNK_WAIT_CAPABILITIES), \ | |
59 | S(SNK_NEGOTIATE_CAPABILITIES), \ | |
2eadc33f | 60 | S(SNK_NEGOTIATE_PPS_CAPABILITIES), \ |
f0690a25 GR |
61 | S(SNK_TRANSITION_SINK), \ |
62 | S(SNK_TRANSITION_SINK_VBUS), \ | |
63 | S(SNK_READY), \ | |
64 | \ | |
65 | S(ACC_UNATTACHED), \ | |
66 | S(DEBUG_ACC_ATTACHED), \ | |
67 | S(AUDIO_ACC_ATTACHED), \ | |
68 | S(AUDIO_ACC_DEBOUNCE), \ | |
69 | \ | |
70 | S(HARD_RESET_SEND), \ | |
71 | S(HARD_RESET_START), \ | |
72 | S(SRC_HARD_RESET_VBUS_OFF), \ | |
73 | S(SRC_HARD_RESET_VBUS_ON), \ | |
74 | S(SNK_HARD_RESET_SINK_OFF), \ | |
75 | S(SNK_HARD_RESET_WAIT_VBUS), \ | |
76 | S(SNK_HARD_RESET_SINK_ON), \ | |
77 | \ | |
78 | S(SOFT_RESET), \ | |
79 | S(SOFT_RESET_SEND), \ | |
80 | \ | |
81 | S(DR_SWAP_ACCEPT), \ | |
82 | S(DR_SWAP_SEND), \ | |
83 | S(DR_SWAP_SEND_TIMEOUT), \ | |
84 | S(DR_SWAP_CANCEL), \ | |
85 | S(DR_SWAP_CHANGE_DR), \ | |
86 | \ | |
87 | S(PR_SWAP_ACCEPT), \ | |
88 | S(PR_SWAP_SEND), \ | |
89 | S(PR_SWAP_SEND_TIMEOUT), \ | |
90 | S(PR_SWAP_CANCEL), \ | |
91 | S(PR_SWAP_START), \ | |
92 | S(PR_SWAP_SRC_SNK_TRANSITION_OFF), \ | |
93 | S(PR_SWAP_SRC_SNK_SOURCE_OFF), \ | |
b965b631 | 94 | S(PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED), \ |
f0690a25 GR |
95 | S(PR_SWAP_SRC_SNK_SINK_ON), \ |
96 | S(PR_SWAP_SNK_SRC_SINK_OFF), \ | |
97 | S(PR_SWAP_SNK_SRC_SOURCE_ON), \ | |
b965b631 | 98 | S(PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP), \ |
f0690a25 GR |
99 | \ |
100 | S(VCONN_SWAP_ACCEPT), \ | |
101 | S(VCONN_SWAP_SEND), \ | |
102 | S(VCONN_SWAP_SEND_TIMEOUT), \ | |
103 | S(VCONN_SWAP_CANCEL), \ | |
104 | S(VCONN_SWAP_START), \ | |
105 | S(VCONN_SWAP_WAIT_FOR_VCONN), \ | |
106 | S(VCONN_SWAP_TURN_ON_VCONN), \ | |
107 | S(VCONN_SWAP_TURN_OFF_VCONN), \ | |
108 | \ | |
8dc4bd07 BJS |
109 | S(FR_SWAP_SEND), \ |
110 | S(FR_SWAP_SEND_TIMEOUT), \ | |
111 | S(FR_SWAP_SNK_SRC_TRANSITION_TO_OFF), \ | |
112 | S(FR_SWAP_SNK_SRC_NEW_SINK_READY), \ | |
113 | S(FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED), \ | |
114 | S(FR_SWAP_CANCEL), \ | |
115 | \ | |
f0690a25 GR |
116 | S(SNK_TRY), \ |
117 | S(SNK_TRY_WAIT), \ | |
a0a3e04e BJS |
118 | S(SNK_TRY_WAIT_DEBOUNCE), \ |
119 | S(SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS), \ | |
f0690a25 | 120 | S(SRC_TRYWAIT), \ |
02d5be46 | 121 | S(SRC_TRYWAIT_DEBOUNCE), \ |
f0690a25 GR |
122 | S(SRC_TRYWAIT_UNATTACHED), \ |
123 | \ | |
124 | S(SRC_TRY), \ | |
131c7d12 | 125 | S(SRC_TRY_WAIT), \ |
f0690a25 GR |
126 | S(SRC_TRY_DEBOUNCE), \ |
127 | S(SNK_TRYWAIT), \ | |
128 | S(SNK_TRYWAIT_DEBOUNCE), \ | |
129 | S(SNK_TRYWAIT_VBUS), \ | |
130 | S(BIST_RX), \ | |
131 | \ | |
64f7c494 AT |
132 | S(GET_STATUS_SEND), \ |
133 | S(GET_STATUS_SEND_TIMEOUT), \ | |
134 | S(GET_PPS_STATUS_SEND), \ | |
135 | S(GET_PPS_STATUS_SEND_TIMEOUT), \ | |
136 | \ | |
8dc4bd07 BJS |
137 | S(GET_SINK_CAP), \ |
138 | S(GET_SINK_CAP_TIMEOUT), \ | |
139 | \ | |
f0690a25 | 140 | S(ERROR_RECOVERY), \ |
b17dd571 GR |
141 | S(PORT_RESET), \ |
142 | S(PORT_RESET_WAIT_OFF) | |
f0690a25 GR |
143 | |
144 | #define GENERATE_ENUM(e) e | |
145 | #define GENERATE_STRING(s) #s | |
146 | ||
147 | enum tcpm_state { | |
148 | FOREACH_STATE(GENERATE_ENUM) | |
149 | }; | |
150 | ||
151 | static const char * const tcpm_states[] = { | |
152 | FOREACH_STATE(GENERATE_STRING) | |
153 | }; | |
154 | ||
155 | enum vdm_states { | |
156 | VDM_STATE_ERR_BUSY = -3, | |
157 | VDM_STATE_ERR_SEND = -2, | |
158 | VDM_STATE_ERR_TMOUT = -1, | |
159 | VDM_STATE_DONE = 0, | |
160 | /* Anything >0 represents an active state */ | |
161 | VDM_STATE_READY = 1, | |
162 | VDM_STATE_BUSY = 2, | |
163 | VDM_STATE_WAIT_RSP_BUSY = 3, | |
164 | }; | |
165 | ||
166 | enum pd_msg_request { | |
167 | PD_MSG_NONE = 0, | |
168 | PD_MSG_CTRL_REJECT, | |
169 | PD_MSG_CTRL_WAIT, | |
64f7c494 | 170 | PD_MSG_CTRL_NOT_SUPP, |
f0690a25 GR |
171 | PD_MSG_DATA_SINK_CAP, |
172 | PD_MSG_DATA_SOURCE_CAP, | |
173 | }; | |
174 | ||
95b4d51c HG |
175 | enum adev_actions { |
176 | ADEV_NONE = 0, | |
177 | ADEV_NOTIFY_USB_AND_QUEUE_VDM, | |
178 | ADEV_QUEUE_VDM, | |
179 | ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL, | |
180 | ADEV_ATTENTION, | |
181 | }; | |
182 | ||
8dc4bd07 BJS |
183 | /* |
184 | * Initial current capability of the new source when vSafe5V is applied during PD3.0 Fast Role Swap. | |
185 | * Based on "Table 6-14 Fixed Supply PDO - Sink" of "USB Power Delivery Specification Revision 3.0, | |
186 | * Version 1.2" | |
187 | */ | |
188 | enum frs_typec_current { | |
189 | FRS_NOT_SUPPORTED, | |
190 | FRS_DEFAULT_POWER, | |
191 | FRS_5V_1P5A, | |
192 | FRS_5V_3A, | |
193 | }; | |
194 | ||
f0690a25 GR |
195 | /* Events from low level driver */ |
196 | ||
197 | #define TCPM_CC_EVENT BIT(0) | |
198 | #define TCPM_VBUS_EVENT BIT(1) | |
199 | #define TCPM_RESET_EVENT BIT(2) | |
8dc4bd07 BJS |
200 | #define TCPM_FRS_EVENT BIT(3) |
201 | #define TCPM_SOURCING_VBUS BIT(4) | |
f0690a25 GR |
202 | |
203 | #define LOG_BUFFER_ENTRIES 1024 | |
204 | #define LOG_BUFFER_ENTRY_SIZE 128 | |
205 | ||
206 | /* Alternate mode support */ | |
207 | ||
208 | #define SVID_DISCOVERY_MAX 16 | |
e9576fe8 | 209 | #define ALTMODE_DISCOVERY_MAX (SVID_DISCOVERY_MAX * MODE_DISCOVERY_MAX) |
f0690a25 | 210 | |
8dc4bd07 BJS |
211 | #define GET_SINK_CAP_RETRY_MS 100 |
212 | ||
f0690a25 GR |
213 | struct pd_mode_data { |
214 | int svid_index; /* current SVID index */ | |
215 | int nsvids; | |
216 | u16 svids[SVID_DISCOVERY_MAX]; | |
217 | int altmodes; /* number of alternate modes */ | |
e9576fe8 | 218 | struct typec_altmode_desc altmode_desc[ALTMODE_DISCOVERY_MAX]; |
f0690a25 GR |
219 | }; |
220 | ||
2eadc33f AT |
221 | struct pd_pps_data { |
222 | u32 min_volt; | |
223 | u32 max_volt; | |
224 | u32 max_curr; | |
225 | u32 out_volt; | |
226 | u32 op_curr; | |
227 | bool supported; | |
228 | bool active; | |
229 | }; | |
230 | ||
f0690a25 GR |
231 | struct tcpm_port { |
232 | struct device *dev; | |
233 | ||
234 | struct mutex lock; /* tcpm state machine lock */ | |
3ed8e1c2 | 235 | struct kthread_worker *wq; |
f0690a25 GR |
236 | |
237 | struct typec_capability typec_caps; | |
238 | struct typec_port *typec_port; | |
239 | ||
240 | struct tcpc_dev *tcpc; | |
2000016c | 241 | struct usb_role_switch *role_sw; |
f0690a25 GR |
242 | |
243 | enum typec_role vconn_role; | |
244 | enum typec_role pwr_role; | |
245 | enum typec_data_role data_role; | |
246 | enum typec_pwr_opmode pwr_opmode; | |
247 | ||
248 | struct usb_pd_identity partner_ident; | |
249 | struct typec_partner_desc partner_desc; | |
250 | struct typec_partner *partner; | |
251 | ||
252 | enum typec_cc_status cc_req; | |
253 | ||
254 | enum typec_cc_status cc1; | |
255 | enum typec_cc_status cc2; | |
256 | enum typec_cc_polarity polarity; | |
257 | ||
258 | bool attached; | |
259 | bool connected; | |
9b0ae699 | 260 | enum typec_port_type port_type; |
f0690a25 GR |
261 | bool vbus_present; |
262 | bool vbus_never_low; | |
263 | bool vbus_source; | |
264 | bool vbus_charge; | |
265 | ||
266 | bool send_discover; | |
267 | bool op_vsafe5v; | |
268 | ||
269 | int try_role; | |
270 | int try_snk_count; | |
271 | int try_src_count; | |
272 | ||
273 | enum pd_msg_request queued_message; | |
274 | ||
275 | enum tcpm_state enter_state; | |
276 | enum tcpm_state prev_state; | |
277 | enum tcpm_state state; | |
278 | enum tcpm_state delayed_state; | |
3ed8e1c2 | 279 | ktime_t delayed_runtime; |
f0690a25 GR |
280 | unsigned long delay_ms; |
281 | ||
282 | spinlock_t pd_event_lock; | |
283 | u32 pd_events; | |
284 | ||
3ed8e1c2 BJS |
285 | struct kthread_work event_work; |
286 | struct hrtimer state_machine_timer; | |
287 | struct kthread_work state_machine; | |
288 | struct hrtimer vdm_state_machine_timer; | |
289 | struct kthread_work vdm_state_machine; | |
8dc4bd07 BJS |
290 | struct hrtimer enable_frs_timer; |
291 | struct kthread_work enable_frs; | |
f0690a25 GR |
292 | bool state_machine_running; |
293 | ||
294 | struct completion tx_complete; | |
295 | enum tcpm_transmit_status tx_status; | |
296 | ||
297 | struct mutex swap_lock; /* swap command lock */ | |
298 | bool swap_pending; | |
b17dd571 | 299 | bool non_pd_role_swap; |
f0690a25 GR |
300 | struct completion swap_complete; |
301 | int swap_status; | |
302 | ||
2eadc33f | 303 | unsigned int negotiated_rev; |
f0690a25 GR |
304 | unsigned int message_id; |
305 | unsigned int caps_count; | |
306 | unsigned int hard_reset_count; | |
307 | bool pd_capable; | |
308 | bool explicit_contract; | |
5fec4b54 | 309 | unsigned int rx_msgid; |
f0690a25 GR |
310 | |
311 | /* Partner capabilities/requests */ | |
312 | u32 sink_request; | |
313 | u32 source_caps[PDO_MAX_OBJECTS]; | |
314 | unsigned int nr_source_caps; | |
315 | u32 sink_caps[PDO_MAX_OBJECTS]; | |
316 | unsigned int nr_sink_caps; | |
317 | ||
318 | /* Local capabilities */ | |
319 | u32 src_pdo[PDO_MAX_OBJECTS]; | |
320 | unsigned int nr_src_pdo; | |
321 | u32 snk_pdo[PDO_MAX_OBJECTS]; | |
322 | unsigned int nr_snk_pdo; | |
193a6801 GR |
323 | u32 snk_vdo[VDO_MAX_OBJECTS]; |
324 | unsigned int nr_snk_vdo; | |
f0690a25 | 325 | |
f0690a25 | 326 | unsigned int operating_snk_mw; |
2eadc33f | 327 | bool update_sink_caps; |
f0690a25 GR |
328 | |
329 | /* Requested current / voltage */ | |
330 | u32 current_limit; | |
331 | u32 supply_voltage; | |
332 | ||
f2a8aa05 AT |
333 | /* Used to export TA voltage and current */ |
334 | struct power_supply *psy; | |
335 | struct power_supply_desc psy_desc; | |
336 | enum power_supply_usb_type usb_type; | |
337 | ||
f0690a25 GR |
338 | u32 bist_request; |
339 | ||
340 | /* PD state for Vendor Defined Messages */ | |
341 | enum vdm_states vdm_state; | |
342 | u32 vdm_retries; | |
343 | /* next Vendor Defined Message to send */ | |
344 | u32 vdo_data[VDO_MAX_SIZE]; | |
345 | u8 vdo_count; | |
346 | /* VDO to retry if UFP responder replied busy */ | |
347 | u32 vdo_retry; | |
348 | ||
2eadc33f AT |
349 | /* PPS */ |
350 | struct pd_pps_data pps_data; | |
351 | struct completion pps_complete; | |
352 | bool pps_pending; | |
353 | int pps_status; | |
f0690a25 | 354 | |
2eadc33f | 355 | /* Alternate mode data */ |
f0690a25 | 356 | struct pd_mode_data mode_data; |
e9576fe8 HK |
357 | struct typec_altmode *partner_altmode[ALTMODE_DISCOVERY_MAX]; |
358 | struct typec_altmode *port_altmode[ALTMODE_DISCOVERY_MAX]; | |
f0690a25 | 359 | |
02d5be46 BJS |
360 | /* Deadline in jiffies to exit src_try_wait state */ |
361 | unsigned long max_wait; | |
362 | ||
23b5f732 BJS |
363 | /* port belongs to a self powered device */ |
364 | bool self_powered; | |
365 | ||
8115240c BJS |
366 | /* Sink FRS */ |
367 | enum frs_typec_current new_source_frs_current; | |
8dc4bd07 BJS |
368 | |
369 | /* Sink caps have been queried */ | |
370 | bool sink_cap_done; | |
371 | ||
f0690a25 GR |
372 | #ifdef CONFIG_DEBUG_FS |
373 | struct dentry *dentry; | |
374 | struct mutex logbuffer_lock; /* log buffer access lock */ | |
375 | int logbuffer_head; | |
376 | int logbuffer_tail; | |
377 | u8 *logbuffer[LOG_BUFFER_ENTRIES]; | |
378 | #endif | |
379 | }; | |
380 | ||
381 | struct pd_rx_event { | |
3ed8e1c2 | 382 | struct kthread_work work; |
f0690a25 GR |
383 | struct tcpm_port *port; |
384 | struct pd_message msg; | |
385 | }; | |
386 | ||
387 | #define tcpm_cc_is_sink(cc) \ | |
388 | ((cc) == TYPEC_CC_RP_DEF || (cc) == TYPEC_CC_RP_1_5 || \ | |
389 | (cc) == TYPEC_CC_RP_3_0) | |
390 | ||
391 | #define tcpm_port_is_sink(port) \ | |
392 | ((tcpm_cc_is_sink((port)->cc1) && !tcpm_cc_is_sink((port)->cc2)) || \ | |
393 | (tcpm_cc_is_sink((port)->cc2) && !tcpm_cc_is_sink((port)->cc1))) | |
394 | ||
395 | #define tcpm_cc_is_source(cc) ((cc) == TYPEC_CC_RD) | |
396 | #define tcpm_cc_is_audio(cc) ((cc) == TYPEC_CC_RA) | |
397 | #define tcpm_cc_is_open(cc) ((cc) == TYPEC_CC_OPEN) | |
398 | ||
399 | #define tcpm_port_is_source(port) \ | |
400 | ((tcpm_cc_is_source((port)->cc1) && \ | |
401 | !tcpm_cc_is_source((port)->cc2)) || \ | |
402 | (tcpm_cc_is_source((port)->cc2) && \ | |
403 | !tcpm_cc_is_source((port)->cc1))) | |
404 | ||
405 | #define tcpm_port_is_debug(port) \ | |
406 | (tcpm_cc_is_source((port)->cc1) && tcpm_cc_is_source((port)->cc2)) | |
407 | ||
408 | #define tcpm_port_is_audio(port) \ | |
409 | (tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_audio((port)->cc2)) | |
410 | ||
411 | #define tcpm_port_is_audio_detached(port) \ | |
412 | ((tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_open((port)->cc2)) || \ | |
413 | (tcpm_cc_is_audio((port)->cc2) && tcpm_cc_is_open((port)->cc1))) | |
414 | ||
415 | #define tcpm_try_snk(port) \ | |
ff6c8cb1 BJS |
416 | ((port)->try_snk_count == 0 && (port)->try_role == TYPEC_SINK && \ |
417 | (port)->port_type == TYPEC_PORT_DRP) | |
f0690a25 GR |
418 | |
419 | #define tcpm_try_src(port) \ | |
ff6c8cb1 BJS |
420 | ((port)->try_src_count == 0 && (port)->try_role == TYPEC_SOURCE && \ |
421 | (port)->port_type == TYPEC_PORT_DRP) | |
f0690a25 | 422 | |
6ecc632d LJ |
423 | #define tcpm_data_role_for_source(port) \ |
424 | ((port)->typec_caps.data == TYPEC_PORT_UFP ? \ | |
425 | TYPEC_DEVICE : TYPEC_HOST) | |
426 | ||
427 | #define tcpm_data_role_for_sink(port) \ | |
428 | ((port)->typec_caps.data == TYPEC_PORT_DFP ? \ | |
429 | TYPEC_HOST : TYPEC_DEVICE) | |
430 | ||
f0690a25 GR |
431 | static enum tcpm_state tcpm_default_state(struct tcpm_port *port) |
432 | { | |
9b0ae699 | 433 | if (port->port_type == TYPEC_PORT_DRP) { |
b46a9c90 BJS |
434 | if (port->try_role == TYPEC_SINK) |
435 | return SNK_UNATTACHED; | |
436 | else if (port->try_role == TYPEC_SOURCE) | |
437 | return SRC_UNATTACHED; | |
b46a9c90 | 438 | /* Fall through to return SRC_UNATTACHED */ |
ceeb1625 | 439 | } else if (port->port_type == TYPEC_PORT_SNK) { |
f0690a25 | 440 | return SNK_UNATTACHED; |
b46a9c90 | 441 | } |
f0690a25 GR |
442 | return SRC_UNATTACHED; |
443 | } | |
444 | ||
f0690a25 GR |
445 | static bool tcpm_port_is_disconnected(struct tcpm_port *port) |
446 | { | |
447 | return (!port->attached && port->cc1 == TYPEC_CC_OPEN && | |
448 | port->cc2 == TYPEC_CC_OPEN) || | |
449 | (port->attached && ((port->polarity == TYPEC_POLARITY_CC1 && | |
450 | port->cc1 == TYPEC_CC_OPEN) || | |
451 | (port->polarity == TYPEC_POLARITY_CC2 && | |
452 | port->cc2 == TYPEC_CC_OPEN))); | |
453 | } | |
454 | ||
455 | /* | |
456 | * Logging | |
457 | */ | |
458 | ||
459 | #ifdef CONFIG_DEBUG_FS | |
460 | ||
461 | static bool tcpm_log_full(struct tcpm_port *port) | |
462 | { | |
463 | return port->logbuffer_tail == | |
464 | (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES; | |
465 | } | |
466 | ||
e79e0125 | 467 | __printf(2, 0) |
f0690a25 GR |
468 | static void _tcpm_log(struct tcpm_port *port, const char *fmt, va_list args) |
469 | { | |
470 | char tmpbuffer[LOG_BUFFER_ENTRY_SIZE]; | |
471 | u64 ts_nsec = local_clock(); | |
472 | unsigned long rem_nsec; | |
473 | ||
d5a4f935 | 474 | mutex_lock(&port->logbuffer_lock); |
f0690a25 GR |
475 | if (!port->logbuffer[port->logbuffer_head]) { |
476 | port->logbuffer[port->logbuffer_head] = | |
477 | kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL); | |
d5a4f935 PC |
478 | if (!port->logbuffer[port->logbuffer_head]) { |
479 | mutex_unlock(&port->logbuffer_lock); | |
f0690a25 | 480 | return; |
d5a4f935 | 481 | } |
f0690a25 GR |
482 | } |
483 | ||
484 | vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args); | |
485 | ||
f0690a25 GR |
486 | if (tcpm_log_full(port)) { |
487 | port->logbuffer_head = max(port->logbuffer_head - 1, 0); | |
488 | strcpy(tmpbuffer, "overflow"); | |
489 | } | |
490 | ||
491 | if (port->logbuffer_head < 0 || | |
492 | port->logbuffer_head >= LOG_BUFFER_ENTRIES) { | |
493 | dev_warn(port->dev, | |
494 | "Bad log buffer index %d\n", port->logbuffer_head); | |
495 | goto abort; | |
496 | } | |
497 | ||
498 | if (!port->logbuffer[port->logbuffer_head]) { | |
499 | dev_warn(port->dev, | |
500 | "Log buffer index %d is NULL\n", port->logbuffer_head); | |
501 | goto abort; | |
502 | } | |
503 | ||
504 | rem_nsec = do_div(ts_nsec, 1000000000); | |
505 | scnprintf(port->logbuffer[port->logbuffer_head], | |
506 | LOG_BUFFER_ENTRY_SIZE, "[%5lu.%06lu] %s", | |
507 | (unsigned long)ts_nsec, rem_nsec / 1000, | |
508 | tmpbuffer); | |
509 | port->logbuffer_head = (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES; | |
510 | ||
511 | abort: | |
512 | mutex_unlock(&port->logbuffer_lock); | |
513 | } | |
514 | ||
e79e0125 | 515 | __printf(2, 3) |
f0690a25 GR |
516 | static void tcpm_log(struct tcpm_port *port, const char *fmt, ...) |
517 | { | |
518 | va_list args; | |
519 | ||
520 | /* Do not log while disconnected and unattached */ | |
521 | if (tcpm_port_is_disconnected(port) && | |
522 | (port->state == SRC_UNATTACHED || port->state == SNK_UNATTACHED || | |
7893f9e1 | 523 | port->state == TOGGLING)) |
f0690a25 GR |
524 | return; |
525 | ||
526 | va_start(args, fmt); | |
527 | _tcpm_log(port, fmt, args); | |
528 | va_end(args); | |
529 | } | |
530 | ||
e79e0125 | 531 | __printf(2, 3) |
f0690a25 GR |
532 | static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...) |
533 | { | |
534 | va_list args; | |
535 | ||
536 | va_start(args, fmt); | |
537 | _tcpm_log(port, fmt, args); | |
538 | va_end(args); | |
539 | } | |
540 | ||
541 | static void tcpm_log_source_caps(struct tcpm_port *port) | |
542 | { | |
543 | int i; | |
544 | ||
545 | for (i = 0; i < port->nr_source_caps; i++) { | |
546 | u32 pdo = port->source_caps[i]; | |
547 | enum pd_pdo_type type = pdo_type(pdo); | |
548 | char msg[64]; | |
549 | ||
550 | switch (type) { | |
551 | case PDO_TYPE_FIXED: | |
552 | scnprintf(msg, sizeof(msg), | |
553 | "%u mV, %u mA [%s%s%s%s%s%s]", | |
554 | pdo_fixed_voltage(pdo), | |
555 | pdo_max_current(pdo), | |
556 | (pdo & PDO_FIXED_DUAL_ROLE) ? | |
557 | "R" : "", | |
558 | (pdo & PDO_FIXED_SUSPEND) ? | |
559 | "S" : "", | |
560 | (pdo & PDO_FIXED_HIGHER_CAP) ? | |
561 | "H" : "", | |
562 | (pdo & PDO_FIXED_USB_COMM) ? | |
563 | "U" : "", | |
564 | (pdo & PDO_FIXED_DATA_SWAP) ? | |
565 | "D" : "", | |
566 | (pdo & PDO_FIXED_EXTPOWER) ? | |
567 | "E" : ""); | |
568 | break; | |
569 | case PDO_TYPE_VAR: | |
570 | scnprintf(msg, sizeof(msg), | |
571 | "%u-%u mV, %u mA", | |
572 | pdo_min_voltage(pdo), | |
573 | pdo_max_voltage(pdo), | |
574 | pdo_max_current(pdo)); | |
575 | break; | |
576 | case PDO_TYPE_BATT: | |
577 | scnprintf(msg, sizeof(msg), | |
578 | "%u-%u mV, %u mW", | |
579 | pdo_min_voltage(pdo), | |
580 | pdo_max_voltage(pdo), | |
581 | pdo_max_power(pdo)); | |
582 | break; | |
2eadc33f AT |
583 | case PDO_TYPE_APDO: |
584 | if (pdo_apdo_type(pdo) == APDO_TYPE_PPS) | |
585 | scnprintf(msg, sizeof(msg), | |
586 | "%u-%u mV, %u mA", | |
587 | pdo_pps_apdo_min_voltage(pdo), | |
588 | pdo_pps_apdo_max_voltage(pdo), | |
589 | pdo_pps_apdo_max_current(pdo)); | |
590 | else | |
591 | strcpy(msg, "undefined APDO"); | |
592 | break; | |
f0690a25 GR |
593 | default: |
594 | strcpy(msg, "undefined"); | |
595 | break; | |
596 | } | |
597 | tcpm_log(port, " PDO %d: type %d, %s", | |
598 | i, type, msg); | |
599 | } | |
600 | } | |
601 | ||
9e902c59 | 602 | static int tcpm_debug_show(struct seq_file *s, void *v) |
f0690a25 GR |
603 | { |
604 | struct tcpm_port *port = (struct tcpm_port *)s->private; | |
605 | int tail; | |
606 | ||
607 | mutex_lock(&port->logbuffer_lock); | |
608 | tail = port->logbuffer_tail; | |
609 | while (tail != port->logbuffer_head) { | |
610 | seq_printf(s, "%s\n", port->logbuffer[tail]); | |
611 | tail = (tail + 1) % LOG_BUFFER_ENTRIES; | |
612 | } | |
613 | if (!seq_has_overflowed(s)) | |
614 | port->logbuffer_tail = tail; | |
615 | mutex_unlock(&port->logbuffer_lock); | |
616 | ||
617 | return 0; | |
618 | } | |
9e902c59 | 619 | DEFINE_SHOW_ATTRIBUTE(tcpm_debug); |
f0690a25 | 620 | |
a941fc39 | 621 | static void tcpm_debugfs_init(struct tcpm_port *port) |
f0690a25 | 622 | { |
9f4bc2ed | 623 | char name[NAME_MAX]; |
f0690a25 | 624 | |
9f4bc2ed HG |
625 | mutex_init(&port->logbuffer_lock); |
626 | snprintf(name, NAME_MAX, "tcpm-%s", dev_name(port->dev)); | |
627 | port->dentry = debugfs_create_file(name, S_IFREG | 0444, usb_debug_root, | |
9e902c59 | 628 | port, &tcpm_debug_fops); |
f0690a25 GR |
629 | } |
630 | ||
631 | static void tcpm_debugfs_exit(struct tcpm_port *port) | |
632 | { | |
fd5da3e2 LJ |
633 | int i; |
634 | ||
635 | mutex_lock(&port->logbuffer_lock); | |
636 | for (i = 0; i < LOG_BUFFER_ENTRIES; i++) { | |
637 | kfree(port->logbuffer[i]); | |
638 | port->logbuffer[i] = NULL; | |
639 | } | |
640 | mutex_unlock(&port->logbuffer_lock); | |
641 | ||
f0690a25 GR |
642 | debugfs_remove(port->dentry); |
643 | } | |
644 | ||
645 | #else | |
646 | ||
e79e0125 | 647 | __printf(2, 3) |
f0690a25 | 648 | static void tcpm_log(const struct tcpm_port *port, const char *fmt, ...) { } |
e79e0125 | 649 | __printf(2, 3) |
f0690a25 GR |
650 | static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...) { } |
651 | static void tcpm_log_source_caps(struct tcpm_port *port) { } | |
a941fc39 | 652 | static void tcpm_debugfs_init(const struct tcpm_port *port) { } |
f0690a25 GR |
653 | static void tcpm_debugfs_exit(const struct tcpm_port *port) { } |
654 | ||
655 | #endif | |
656 | ||
657 | static int tcpm_pd_transmit(struct tcpm_port *port, | |
658 | enum tcpm_transmit_type type, | |
659 | const struct pd_message *msg) | |
660 | { | |
661 | unsigned long timeout; | |
662 | int ret; | |
663 | ||
664 | if (msg) | |
665 | tcpm_log(port, "PD TX, header: %#x", le16_to_cpu(msg->header)); | |
666 | else | |
667 | tcpm_log(port, "PD TX, type: %#x", type); | |
668 | ||
669 | reinit_completion(&port->tx_complete); | |
670 | ret = port->tcpc->pd_transmit(port->tcpc, type, msg); | |
671 | if (ret < 0) | |
672 | return ret; | |
673 | ||
674 | mutex_unlock(&port->lock); | |
675 | timeout = wait_for_completion_timeout(&port->tx_complete, | |
676 | msecs_to_jiffies(PD_T_TCPC_TX_TIMEOUT)); | |
677 | mutex_lock(&port->lock); | |
678 | if (!timeout) | |
679 | return -ETIMEDOUT; | |
680 | ||
681 | switch (port->tx_status) { | |
682 | case TCPC_TX_SUCCESS: | |
683 | port->message_id = (port->message_id + 1) & PD_HEADER_ID_MASK; | |
684 | return 0; | |
685 | case TCPC_TX_DISCARDED: | |
686 | return -EAGAIN; | |
687 | case TCPC_TX_FAILED: | |
688 | default: | |
689 | return -EIO; | |
690 | } | |
691 | } | |
692 | ||
693 | void tcpm_pd_transmit_complete(struct tcpm_port *port, | |
694 | enum tcpm_transmit_status status) | |
695 | { | |
696 | tcpm_log(port, "PD TX complete, status: %u", status); | |
697 | port->tx_status = status; | |
698 | complete(&port->tx_complete); | |
699 | } | |
700 | EXPORT_SYMBOL_GPL(tcpm_pd_transmit_complete); | |
701 | ||
e9576fe8 | 702 | static int tcpm_mux_set(struct tcpm_port *port, int state, |
2000016c HG |
703 | enum usb_role usb_role, |
704 | enum typec_orientation orientation) | |
f0690a25 | 705 | { |
2000016c | 706 | int ret; |
f0690a25 | 707 | |
e9576fe8 HK |
708 | tcpm_log(port, "Requesting mux state %d, usb-role %d, orientation %d", |
709 | state, usb_role, orientation); | |
f0690a25 | 710 | |
2000016c HG |
711 | ret = typec_set_orientation(port->typec_port, orientation); |
712 | if (ret) | |
713 | return ret; | |
f0690a25 | 714 | |
2000016c HG |
715 | if (port->role_sw) { |
716 | ret = usb_role_switch_set_role(port->role_sw, usb_role); | |
717 | if (ret) | |
718 | return ret; | |
719 | } | |
720 | ||
e9576fe8 | 721 | return typec_set_mode(port->typec_port, state); |
f0690a25 GR |
722 | } |
723 | ||
724 | static int tcpm_set_polarity(struct tcpm_port *port, | |
725 | enum typec_cc_polarity polarity) | |
726 | { | |
727 | int ret; | |
728 | ||
729 | tcpm_log(port, "polarity %d", polarity); | |
730 | ||
731 | ret = port->tcpc->set_polarity(port->tcpc, polarity); | |
732 | if (ret < 0) | |
733 | return ret; | |
734 | ||
735 | port->polarity = polarity; | |
736 | ||
737 | return 0; | |
738 | } | |
739 | ||
740 | static int tcpm_set_vconn(struct tcpm_port *port, bool enable) | |
741 | { | |
742 | int ret; | |
743 | ||
744 | tcpm_log(port, "vconn:=%d", enable); | |
745 | ||
746 | ret = port->tcpc->set_vconn(port->tcpc, enable); | |
747 | if (!ret) { | |
748 | port->vconn_role = enable ? TYPEC_SOURCE : TYPEC_SINK; | |
749 | typec_set_vconn_role(port->typec_port, port->vconn_role); | |
750 | } | |
751 | ||
752 | return ret; | |
753 | } | |
754 | ||
755 | static u32 tcpm_get_current_limit(struct tcpm_port *port) | |
756 | { | |
757 | enum typec_cc_status cc; | |
758 | u32 limit; | |
759 | ||
760 | cc = port->polarity ? port->cc2 : port->cc1; | |
761 | switch (cc) { | |
762 | case TYPEC_CC_RP_1_5: | |
763 | limit = 1500; | |
764 | break; | |
765 | case TYPEC_CC_RP_3_0: | |
766 | limit = 3000; | |
767 | break; | |
768 | case TYPEC_CC_RP_DEF: | |
769 | default: | |
ea62cfc7 HG |
770 | if (port->tcpc->get_current_limit) |
771 | limit = port->tcpc->get_current_limit(port->tcpc); | |
772 | else | |
773 | limit = 0; | |
f0690a25 GR |
774 | break; |
775 | } | |
776 | ||
777 | return limit; | |
778 | } | |
779 | ||
780 | static int tcpm_set_current_limit(struct tcpm_port *port, u32 max_ma, u32 mv) | |
781 | { | |
782 | int ret = -EOPNOTSUPP; | |
783 | ||
784 | tcpm_log(port, "Setting voltage/current limit %u mV %u mA", mv, max_ma); | |
785 | ||
146971e6 HG |
786 | port->supply_voltage = mv; |
787 | port->current_limit = max_ma; | |
788 | ||
f0690a25 GR |
789 | if (port->tcpc->set_current_limit) |
790 | ret = port->tcpc->set_current_limit(port->tcpc, max_ma, mv); | |
791 | ||
792 | return ret; | |
793 | } | |
794 | ||
795 | /* | |
796 | * Determine RP value to set based on maximum current supported | |
797 | * by a port if configured as source. | |
798 | * Returns CC value to report to link partner. | |
799 | */ | |
800 | static enum typec_cc_status tcpm_rp_cc(struct tcpm_port *port) | |
801 | { | |
802 | const u32 *src_pdo = port->src_pdo; | |
803 | int nr_pdo = port->nr_src_pdo; | |
804 | int i; | |
805 | ||
806 | /* | |
807 | * Search for first entry with matching voltage. | |
808 | * It should report the maximum supported current. | |
809 | */ | |
810 | for (i = 0; i < nr_pdo; i++) { | |
811 | const u32 pdo = src_pdo[i]; | |
812 | ||
813 | if (pdo_type(pdo) == PDO_TYPE_FIXED && | |
814 | pdo_fixed_voltage(pdo) == 5000) { | |
815 | unsigned int curr = pdo_max_current(pdo); | |
816 | ||
817 | if (curr >= 3000) | |
818 | return TYPEC_CC_RP_3_0; | |
819 | else if (curr >= 1500) | |
820 | return TYPEC_CC_RP_1_5; | |
821 | return TYPEC_CC_RP_DEF; | |
822 | } | |
823 | } | |
824 | ||
825 | return TYPEC_CC_RP_DEF; | |
826 | } | |
827 | ||
828 | static int tcpm_set_attached_state(struct tcpm_port *port, bool attached) | |
829 | { | |
830 | return port->tcpc->set_roles(port->tcpc, attached, port->pwr_role, | |
831 | port->data_role); | |
832 | } | |
833 | ||
834 | static int tcpm_set_roles(struct tcpm_port *port, bool attached, | |
835 | enum typec_role role, enum typec_data_role data) | |
836 | { | |
2000016c | 837 | enum typec_orientation orientation; |
c6962c29 | 838 | enum usb_role usb_role; |
f0690a25 GR |
839 | int ret; |
840 | ||
2000016c HG |
841 | if (port->polarity == TYPEC_POLARITY_CC1) |
842 | orientation = TYPEC_ORIENTATION_NORMAL; | |
843 | else | |
844 | orientation = TYPEC_ORIENTATION_REVERSE; | |
845 | ||
6ecc632d LJ |
846 | if (port->typec_caps.data == TYPEC_PORT_DRD) { |
847 | if (data == TYPEC_HOST) | |
848 | usb_role = USB_ROLE_HOST; | |
849 | else | |
850 | usb_role = USB_ROLE_DEVICE; | |
851 | } else if (port->typec_caps.data == TYPEC_PORT_DFP) { | |
852 | if (data == TYPEC_HOST) { | |
853 | if (role == TYPEC_SOURCE) | |
854 | usb_role = USB_ROLE_HOST; | |
855 | else | |
856 | usb_role = USB_ROLE_NONE; | |
857 | } else { | |
858 | return -ENOTSUPP; | |
859 | } | |
860 | } else { | |
861 | if (data == TYPEC_DEVICE) { | |
862 | if (role == TYPEC_SINK) | |
863 | usb_role = USB_ROLE_DEVICE; | |
864 | else | |
865 | usb_role = USB_ROLE_NONE; | |
866 | } else { | |
867 | return -ENOTSUPP; | |
868 | } | |
869 | } | |
c6962c29 | 870 | |
e9576fe8 | 871 | ret = tcpm_mux_set(port, TYPEC_STATE_USB, usb_role, orientation); |
f0690a25 GR |
872 | if (ret < 0) |
873 | return ret; | |
874 | ||
875 | ret = port->tcpc->set_roles(port->tcpc, attached, role, data); | |
876 | if (ret < 0) | |
877 | return ret; | |
878 | ||
879 | port->pwr_role = role; | |
880 | port->data_role = data; | |
881 | typec_set_data_role(port->typec_port, data); | |
882 | typec_set_pwr_role(port->typec_port, role); | |
883 | ||
884 | return 0; | |
885 | } | |
886 | ||
887 | static int tcpm_set_pwr_role(struct tcpm_port *port, enum typec_role role) | |
888 | { | |
889 | int ret; | |
890 | ||
891 | ret = port->tcpc->set_roles(port->tcpc, true, role, | |
892 | port->data_role); | |
893 | if (ret < 0) | |
894 | return ret; | |
895 | ||
896 | port->pwr_role = role; | |
897 | typec_set_pwr_role(port->typec_port, role); | |
898 | ||
899 | return 0; | |
900 | } | |
901 | ||
902 | static int tcpm_pd_send_source_caps(struct tcpm_port *port) | |
903 | { | |
904 | struct pd_message msg; | |
905 | int i; | |
906 | ||
907 | memset(&msg, 0, sizeof(msg)); | |
908 | if (!port->nr_src_pdo) { | |
909 | /* No source capabilities defined, sink only */ | |
910 | msg.header = PD_HEADER_LE(PD_CTRL_REJECT, | |
911 | port->pwr_role, | |
912 | port->data_role, | |
2eadc33f | 913 | port->negotiated_rev, |
f0690a25 GR |
914 | port->message_id, 0); |
915 | } else { | |
916 | msg.header = PD_HEADER_LE(PD_DATA_SOURCE_CAP, | |
917 | port->pwr_role, | |
918 | port->data_role, | |
2eadc33f | 919 | port->negotiated_rev, |
f0690a25 GR |
920 | port->message_id, |
921 | port->nr_src_pdo); | |
922 | } | |
923 | for (i = 0; i < port->nr_src_pdo; i++) | |
924 | msg.payload[i] = cpu_to_le32(port->src_pdo[i]); | |
925 | ||
926 | return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg); | |
927 | } | |
928 | ||
929 | static int tcpm_pd_send_sink_caps(struct tcpm_port *port) | |
930 | { | |
931 | struct pd_message msg; | |
932 | int i; | |
933 | ||
934 | memset(&msg, 0, sizeof(msg)); | |
935 | if (!port->nr_snk_pdo) { | |
936 | /* No sink capabilities defined, source only */ | |
937 | msg.header = PD_HEADER_LE(PD_CTRL_REJECT, | |
938 | port->pwr_role, | |
939 | port->data_role, | |
2eadc33f | 940 | port->negotiated_rev, |
f0690a25 GR |
941 | port->message_id, 0); |
942 | } else { | |
943 | msg.header = PD_HEADER_LE(PD_DATA_SINK_CAP, | |
944 | port->pwr_role, | |
945 | port->data_role, | |
2eadc33f | 946 | port->negotiated_rev, |
f0690a25 GR |
947 | port->message_id, |
948 | port->nr_snk_pdo); | |
949 | } | |
950 | for (i = 0; i < port->nr_snk_pdo; i++) | |
951 | msg.payload[i] = cpu_to_le32(port->snk_pdo[i]); | |
952 | ||
953 | return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg); | |
954 | } | |
955 | ||
3ed8e1c2 BJS |
956 | static void mod_tcpm_delayed_work(struct tcpm_port *port, unsigned int delay_ms) |
957 | { | |
958 | if (delay_ms) { | |
959 | hrtimer_start(&port->state_machine_timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL); | |
960 | } else { | |
961 | hrtimer_cancel(&port->state_machine_timer); | |
962 | kthread_queue_work(port->wq, &port->state_machine); | |
963 | } | |
964 | } | |
965 | ||
966 | static void mod_vdm_delayed_work(struct tcpm_port *port, unsigned int delay_ms) | |
967 | { | |
968 | if (delay_ms) { | |
969 | hrtimer_start(&port->vdm_state_machine_timer, ms_to_ktime(delay_ms), | |
970 | HRTIMER_MODE_REL); | |
971 | } else { | |
972 | hrtimer_cancel(&port->vdm_state_machine_timer); | |
973 | kthread_queue_work(port->wq, &port->vdm_state_machine); | |
974 | } | |
975 | } | |
976 | ||
8dc4bd07 BJS |
977 | static void mod_enable_frs_delayed_work(struct tcpm_port *port, unsigned int delay_ms) |
978 | { | |
979 | if (delay_ms) { | |
980 | hrtimer_start(&port->enable_frs_timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL); | |
981 | } else { | |
982 | hrtimer_cancel(&port->enable_frs_timer); | |
983 | kthread_queue_work(port->wq, &port->enable_frs); | |
984 | } | |
985 | } | |
986 | ||
f0690a25 GR |
987 | static void tcpm_set_state(struct tcpm_port *port, enum tcpm_state state, |
988 | unsigned int delay_ms) | |
989 | { | |
990 | if (delay_ms) { | |
991 | tcpm_log(port, "pending state change %s -> %s @ %u ms", | |
992 | tcpm_states[port->state], tcpm_states[state], | |
993 | delay_ms); | |
994 | port->delayed_state = state; | |
3ed8e1c2 BJS |
995 | mod_tcpm_delayed_work(port, delay_ms); |
996 | port->delayed_runtime = ktime_add(ktime_get(), ms_to_ktime(delay_ms)); | |
f0690a25 GR |
997 | port->delay_ms = delay_ms; |
998 | } else { | |
999 | tcpm_log(port, "state change %s -> %s", | |
1000 | tcpm_states[port->state], tcpm_states[state]); | |
1001 | port->delayed_state = INVALID_STATE; | |
1002 | port->prev_state = port->state; | |
1003 | port->state = state; | |
1004 | /* | |
1005 | * Don't re-queue the state machine work item if we're currently | |
1006 | * in the state machine and we're immediately changing states. | |
1007 | * tcpm_state_machine_work() will continue running the state | |
1008 | * machine. | |
1009 | */ | |
1010 | if (!port->state_machine_running) | |
3ed8e1c2 | 1011 | mod_tcpm_delayed_work(port, 0); |
f0690a25 GR |
1012 | } |
1013 | } | |
1014 | ||
1015 | static void tcpm_set_state_cond(struct tcpm_port *port, enum tcpm_state state, | |
1016 | unsigned int delay_ms) | |
1017 | { | |
1018 | if (port->enter_state == port->state) | |
1019 | tcpm_set_state(port, state, delay_ms); | |
1020 | else | |
1021 | tcpm_log(port, | |
1022 | "skipped %sstate change %s -> %s [%u ms], context state %s", | |
1023 | delay_ms ? "delayed " : "", | |
1024 | tcpm_states[port->state], tcpm_states[state], | |
1025 | delay_ms, tcpm_states[port->enter_state]); | |
1026 | } | |
1027 | ||
1028 | static void tcpm_queue_message(struct tcpm_port *port, | |
1029 | enum pd_msg_request message) | |
1030 | { | |
1031 | port->queued_message = message; | |
3ed8e1c2 | 1032 | mod_tcpm_delayed_work(port, 0); |
f0690a25 GR |
1033 | } |
1034 | ||
1035 | /* | |
1036 | * VDM/VDO handling functions | |
1037 | */ | |
1038 | static void tcpm_queue_vdm(struct tcpm_port *port, const u32 header, | |
1039 | const u32 *data, int cnt) | |
1040 | { | |
03eafcfb HG |
1041 | WARN_ON(!mutex_is_locked(&port->lock)); |
1042 | ||
754498c1 HG |
1043 | /* Make sure we are not still processing a previous VDM packet */ |
1044 | WARN_ON(port->vdm_state > VDM_STATE_DONE); | |
1045 | ||
f0690a25 GR |
1046 | port->vdo_count = cnt + 1; |
1047 | port->vdo_data[0] = header; | |
1048 | memcpy(&port->vdo_data[1], data, sizeof(u32) * cnt); | |
1049 | /* Set ready, vdm state machine will actually send */ | |
1050 | port->vdm_retries = 0; | |
1051 | port->vdm_state = VDM_STATE_READY; | |
5f2b8d87 | 1052 | |
3ed8e1c2 | 1053 | mod_vdm_delayed_work(port, 0); |
f0690a25 GR |
1054 | } |
1055 | ||
03eafcfb HG |
1056 | static void tcpm_queue_vdm_unlocked(struct tcpm_port *port, const u32 header, |
1057 | const u32 *data, int cnt) | |
1058 | { | |
1059 | mutex_lock(&port->lock); | |
1060 | tcpm_queue_vdm(port, header, data, cnt); | |
1061 | mutex_unlock(&port->lock); | |
1062 | } | |
1063 | ||
8afe9a35 | 1064 | static void svdm_consume_identity(struct tcpm_port *port, const u32 *p, int cnt) |
f0690a25 | 1065 | { |
8afe9a35 HG |
1066 | u32 vdo = p[VDO_INDEX_IDH]; |
1067 | u32 product = p[VDO_INDEX_PRODUCT]; | |
f0690a25 GR |
1068 | |
1069 | memset(&port->mode_data, 0, sizeof(port->mode_data)); | |
1070 | ||
f0690a25 | 1071 | port->partner_ident.id_header = vdo; |
8afe9a35 | 1072 | port->partner_ident.cert_stat = p[VDO_INDEX_CSTAT]; |
f0690a25 GR |
1073 | port->partner_ident.product = product; |
1074 | ||
1075 | typec_partner_set_identity(port->partner); | |
1076 | ||
1077 | tcpm_log(port, "Identity: %04x:%04x.%04x", | |
1078 | PD_IDH_VID(vdo), | |
1079 | PD_PRODUCT_PID(product), product & 0xffff); | |
1080 | } | |
1081 | ||
8afe9a35 | 1082 | static bool svdm_consume_svids(struct tcpm_port *port, const u32 *p, int cnt) |
f0690a25 GR |
1083 | { |
1084 | struct pd_mode_data *pmdata = &port->mode_data; | |
1085 | int i; | |
1086 | ||
1087 | for (i = 1; i < cnt; i++) { | |
f0690a25 GR |
1088 | u16 svid; |
1089 | ||
8afe9a35 | 1090 | svid = (p[i] >> 16) & 0xffff; |
f0690a25 GR |
1091 | if (!svid) |
1092 | return false; | |
1093 | ||
1094 | if (pmdata->nsvids >= SVID_DISCOVERY_MAX) | |
1095 | goto abort; | |
1096 | ||
1097 | pmdata->svids[pmdata->nsvids++] = svid; | |
1098 | tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid); | |
1099 | ||
8afe9a35 | 1100 | svid = p[i] & 0xffff; |
f0690a25 GR |
1101 | if (!svid) |
1102 | return false; | |
1103 | ||
1104 | if (pmdata->nsvids >= SVID_DISCOVERY_MAX) | |
1105 | goto abort; | |
1106 | ||
1107 | pmdata->svids[pmdata->nsvids++] = svid; | |
1108 | tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid); | |
1109 | } | |
1110 | return true; | |
1111 | abort: | |
1112 | tcpm_log(port, "SVID_DISCOVERY_MAX(%d) too low!", SVID_DISCOVERY_MAX); | |
1113 | return false; | |
1114 | } | |
1115 | ||
8afe9a35 | 1116 | static void svdm_consume_modes(struct tcpm_port *port, const u32 *p, int cnt) |
f0690a25 GR |
1117 | { |
1118 | struct pd_mode_data *pmdata = &port->mode_data; | |
1119 | struct typec_altmode_desc *paltmode; | |
f0690a25 GR |
1120 | int i; |
1121 | ||
1122 | if (pmdata->altmodes >= ARRAY_SIZE(port->partner_altmode)) { | |
1123 | /* Already logged in svdm_consume_svids() */ | |
1124 | return; | |
1125 | } | |
1126 | ||
4ab8c18d HK |
1127 | for (i = 1; i < cnt; i++) { |
1128 | paltmode = &pmdata->altmode_desc[pmdata->altmodes]; | |
1129 | memset(paltmode, 0, sizeof(*paltmode)); | |
1130 | ||
1131 | paltmode->svid = pmdata->svids[pmdata->svid_index]; | |
1132 | paltmode->mode = i; | |
8afe9a35 | 1133 | paltmode->vdo = p[i]; |
4ab8c18d HK |
1134 | |
1135 | tcpm_log(port, " Alternate mode %d: SVID 0x%04x, VDO %d: 0x%08x", | |
1136 | pmdata->altmodes, paltmode->svid, | |
1137 | paltmode->mode, paltmode->vdo); | |
1138 | ||
4ab8c18d | 1139 | pmdata->altmodes++; |
f0690a25 | 1140 | } |
f0690a25 GR |
1141 | } |
1142 | ||
e9576fe8 HK |
1143 | static void tcpm_register_partner_altmodes(struct tcpm_port *port) |
1144 | { | |
1145 | struct pd_mode_data *modep = &port->mode_data; | |
1146 | struct typec_altmode *altmode; | |
1147 | int i; | |
1148 | ||
1149 | for (i = 0; i < modep->altmodes; i++) { | |
1150 | altmode = typec_partner_register_altmode(port->partner, | |
1151 | &modep->altmode_desc[i]); | |
d25d61be | 1152 | if (IS_ERR(altmode)) { |
e9576fe8 HK |
1153 | tcpm_log(port, "Failed to register partner SVID 0x%04x", |
1154 | modep->altmode_desc[i].svid); | |
d25d61be KT |
1155 | altmode = NULL; |
1156 | } | |
e9576fe8 HK |
1157 | port->partner_altmode[i] = altmode; |
1158 | } | |
1159 | } | |
1160 | ||
f0690a25 GR |
1161 | #define supports_modal(port) PD_IDH_MODAL_SUPP((port)->partner_ident.id_header) |
1162 | ||
95b4d51c HG |
1163 | static int tcpm_pd_svdm(struct tcpm_port *port, struct typec_altmode *adev, |
1164 | const u32 *p, int cnt, u32 *response, | |
1165 | enum adev_actions *adev_action) | |
f0690a25 | 1166 | { |
e9576fe8 | 1167 | struct typec_altmode *pdev; |
f0690a25 GR |
1168 | struct pd_mode_data *modep; |
1169 | int rlen = 0; | |
e9576fe8 HK |
1170 | int cmd_type; |
1171 | int cmd; | |
193a6801 | 1172 | int i; |
f0690a25 | 1173 | |
e9576fe8 HK |
1174 | cmd_type = PD_VDO_CMDT(p[0]); |
1175 | cmd = PD_VDO_CMD(p[0]); | |
1176 | ||
f0690a25 | 1177 | tcpm_log(port, "Rx VDM cmd 0x%x type %d cmd %d len %d", |
e9576fe8 | 1178 | p[0], cmd_type, cmd, cnt); |
f0690a25 GR |
1179 | |
1180 | modep = &port->mode_data; | |
1181 | ||
e9576fe8 HK |
1182 | pdev = typec_match_altmode(port->partner_altmode, ALTMODE_DISCOVERY_MAX, |
1183 | PD_VDO_VID(p[0]), PD_VDO_OPOS(p[0])); | |
1184 | ||
f0690a25 GR |
1185 | switch (cmd_type) { |
1186 | case CMDT_INIT: | |
1187 | switch (cmd) { | |
1188 | case CMD_DISCOVER_IDENT: | |
193a6801 GR |
1189 | /* 6.4.4.3.1: Only respond as UFP (device) */ |
1190 | if (port->data_role == TYPEC_DEVICE && | |
1191 | port->nr_snk_vdo) { | |
1192 | for (i = 0; i < port->nr_snk_vdo; i++) | |
cbe5843e | 1193 | response[i + 1] = port->snk_vdo[i]; |
193a6801 GR |
1194 | rlen = port->nr_snk_vdo + 1; |
1195 | } | |
f0690a25 GR |
1196 | break; |
1197 | case CMD_DISCOVER_SVID: | |
1198 | break; | |
1199 | case CMD_DISCOVER_MODES: | |
1200 | break; | |
1201 | case CMD_ENTER_MODE: | |
1202 | break; | |
1203 | case CMD_EXIT_MODE: | |
1204 | break; | |
1205 | case CMD_ATTENTION: | |
e9576fe8 | 1206 | /* Attention command does not have response */ |
95b4d51c | 1207 | *adev_action = ADEV_ATTENTION; |
e9576fe8 | 1208 | return 0; |
f0690a25 GR |
1209 | default: |
1210 | break; | |
1211 | } | |
1212 | if (rlen >= 1) { | |
e9576fe8 | 1213 | response[0] = p[0] | VDO_CMDT(CMDT_RSP_ACK); |
f0690a25 | 1214 | } else if (rlen == 0) { |
e9576fe8 | 1215 | response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK); |
f0690a25 GR |
1216 | rlen = 1; |
1217 | } else { | |
e9576fe8 | 1218 | response[0] = p[0] | VDO_CMDT(CMDT_RSP_BUSY); |
f0690a25 GR |
1219 | rlen = 1; |
1220 | } | |
1221 | break; | |
1222 | case CMDT_RSP_ACK: | |
1223 | /* silently drop message if we are not connected */ | |
cf6e06cd | 1224 | if (IS_ERR_OR_NULL(port->partner)) |
f0690a25 GR |
1225 | break; |
1226 | ||
1227 | switch (cmd) { | |
1228 | case CMD_DISCOVER_IDENT: | |
1229 | /* 6.4.4.3.1 */ | |
8afe9a35 | 1230 | svdm_consume_identity(port, p, cnt); |
f0690a25 GR |
1231 | response[0] = VDO(USB_SID_PD, 1, CMD_DISCOVER_SVID); |
1232 | rlen = 1; | |
1233 | break; | |
1234 | case CMD_DISCOVER_SVID: | |
1235 | /* 6.4.4.3.2 */ | |
8afe9a35 | 1236 | if (svdm_consume_svids(port, p, cnt)) { |
f0690a25 GR |
1237 | response[0] = VDO(USB_SID_PD, 1, |
1238 | CMD_DISCOVER_SVID); | |
1239 | rlen = 1; | |
1240 | } else if (modep->nsvids && supports_modal(port)) { | |
1241 | response[0] = VDO(modep->svids[0], 1, | |
1242 | CMD_DISCOVER_MODES); | |
1243 | rlen = 1; | |
1244 | } | |
1245 | break; | |
1246 | case CMD_DISCOVER_MODES: | |
1247 | /* 6.4.4.3.3 */ | |
8afe9a35 | 1248 | svdm_consume_modes(port, p, cnt); |
f0690a25 GR |
1249 | modep->svid_index++; |
1250 | if (modep->svid_index < modep->nsvids) { | |
e9576fe8 | 1251 | u16 svid = modep->svids[modep->svid_index]; |
f0690a25 GR |
1252 | response[0] = VDO(svid, 1, CMD_DISCOVER_MODES); |
1253 | rlen = 1; | |
1254 | } else { | |
e9576fe8 | 1255 | tcpm_register_partner_altmodes(port); |
f0690a25 GR |
1256 | } |
1257 | break; | |
1258 | case CMD_ENTER_MODE: | |
88d02c9b GR |
1259 | if (adev && pdev) { |
1260 | typec_altmode_update_active(pdev, true); | |
95b4d51c | 1261 | *adev_action = ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL; |
e9576fe8 HK |
1262 | } |
1263 | return 0; | |
1264 | case CMD_EXIT_MODE: | |
88d02c9b GR |
1265 | if (adev && pdev) { |
1266 | typec_altmode_update_active(pdev, false); | |
88d02c9b | 1267 | /* Back to USB Operation */ |
95b4d51c HG |
1268 | *adev_action = ADEV_NOTIFY_USB_AND_QUEUE_VDM; |
1269 | return 0; | |
88d02c9b | 1270 | } |
e9576fe8 HK |
1271 | break; |
1272 | default: | |
1273 | break; | |
1274 | } | |
1275 | break; | |
1276 | case CMDT_RSP_NAK: | |
1277 | switch (cmd) { | |
1278 | case CMD_ENTER_MODE: | |
1279 | /* Back to USB Operation */ | |
95b4d51c HG |
1280 | *adev_action = ADEV_NOTIFY_USB_AND_QUEUE_VDM; |
1281 | return 0; | |
f0690a25 GR |
1282 | default: |
1283 | break; | |
1284 | } | |
1285 | break; | |
1286 | default: | |
1287 | break; | |
1288 | } | |
1289 | ||
e9576fe8 | 1290 | /* Informing the alternate mode drivers about everything */ |
95b4d51c | 1291 | *adev_action = ADEV_QUEUE_VDM; |
f0690a25 GR |
1292 | return rlen; |
1293 | } | |
1294 | ||
1295 | static void tcpm_handle_vdm_request(struct tcpm_port *port, | |
1296 | const __le32 *payload, int cnt) | |
1297 | { | |
95b4d51c HG |
1298 | enum adev_actions adev_action = ADEV_NONE; |
1299 | struct typec_altmode *adev; | |
8afe9a35 | 1300 | u32 p[PD_MAX_PAYLOAD]; |
f0690a25 | 1301 | u32 response[8] = { }; |
8afe9a35 HG |
1302 | int i, rlen = 0; |
1303 | ||
1304 | for (i = 0; i < cnt; i++) | |
1305 | p[i] = le32_to_cpu(payload[i]); | |
f0690a25 | 1306 | |
95b4d51c HG |
1307 | adev = typec_match_altmode(port->port_altmode, ALTMODE_DISCOVERY_MAX, |
1308 | PD_VDO_VID(p[0]), PD_VDO_OPOS(p[0])); | |
1309 | ||
f0690a25 GR |
1310 | if (port->vdm_state == VDM_STATE_BUSY) { |
1311 | /* If UFP responded busy retry after timeout */ | |
8afe9a35 | 1312 | if (PD_VDO_CMDT(p[0]) == CMDT_RSP_BUSY) { |
f0690a25 | 1313 | port->vdm_state = VDM_STATE_WAIT_RSP_BUSY; |
8afe9a35 | 1314 | port->vdo_retry = (p[0] & ~VDO_CMDT_MASK) | |
f0690a25 | 1315 | CMDT_INIT; |
3ed8e1c2 | 1316 | mod_vdm_delayed_work(port, PD_T_VDM_BUSY); |
f0690a25 GR |
1317 | return; |
1318 | } | |
1319 | port->vdm_state = VDM_STATE_DONE; | |
1320 | } | |
1321 | ||
8afe9a35 | 1322 | if (PD_VDO_SVDM(p[0])) |
95b4d51c HG |
1323 | rlen = tcpm_pd_svdm(port, adev, p, cnt, response, &adev_action); |
1324 | ||
a37241d8 HG |
1325 | /* |
1326 | * We are done with any state stored in the port struct now, except | |
1327 | * for any port struct changes done by the tcpm_queue_vdm() call | |
1328 | * below, which is a separate operation. | |
1329 | * | |
1330 | * So we can safely release the lock here; and we MUST release the | |
1331 | * lock here to avoid an AB BA lock inversion: | |
1332 | * | |
1333 | * If we keep the lock here then the lock ordering in this path is: | |
1334 | * 1. tcpm_pd_rx_handler take the tcpm port lock | |
1335 | * 2. One of the typec_altmode_* calls below takes the alt-mode's lock | |
1336 | * | |
1337 | * And we also have this ordering: | |
1338 | * 1. alt-mode driver takes the alt-mode's lock | |
1339 | * 2. alt-mode driver calls tcpm_altmode_enter which takes the | |
1340 | * tcpm port lock | |
1341 | * | |
1342 | * Dropping our lock here avoids this. | |
1343 | */ | |
1344 | mutex_unlock(&port->lock); | |
1345 | ||
95b4d51c HG |
1346 | if (adev) { |
1347 | switch (adev_action) { | |
1348 | case ADEV_NONE: | |
1349 | break; | |
1350 | case ADEV_NOTIFY_USB_AND_QUEUE_VDM: | |
1351 | WARN_ON(typec_altmode_notify(adev, TYPEC_STATE_USB, NULL)); | |
1352 | typec_altmode_vdm(adev, p[0], &p[1], cnt); | |
1353 | break; | |
1354 | case ADEV_QUEUE_VDM: | |
1355 | typec_altmode_vdm(adev, p[0], &p[1], cnt); | |
1356 | break; | |
1357 | case ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL: | |
1358 | if (typec_altmode_vdm(adev, p[0], &p[1], cnt)) { | |
1359 | response[0] = VDO(adev->svid, 1, CMD_EXIT_MODE); | |
1360 | response[0] |= VDO_OPOS(adev->mode); | |
1361 | rlen = 1; | |
1362 | } | |
1363 | break; | |
1364 | case ADEV_ATTENTION: | |
1365 | typec_altmode_attention(adev, p[1]); | |
1366 | break; | |
1367 | } | |
1368 | } | |
f0690a25 | 1369 | |
a37241d8 HG |
1370 | /* |
1371 | * We must re-take the lock here to balance the unlock in | |
1372 | * tcpm_pd_rx_handler, note that no changes, other then the | |
1373 | * tcpm_queue_vdm call, are made while the lock is held again. | |
1374 | * All that is done after the call is unwinding the call stack until | |
1375 | * we return to tcpm_pd_rx_handler and do the unlock there. | |
1376 | */ | |
1377 | mutex_lock(&port->lock); | |
1378 | ||
5f2b8d87 | 1379 | if (rlen > 0) |
f0690a25 | 1380 | tcpm_queue_vdm(port, response[0], &response[1], rlen - 1); |
f0690a25 GR |
1381 | } |
1382 | ||
1383 | static void tcpm_send_vdm(struct tcpm_port *port, u32 vid, int cmd, | |
1384 | const u32 *data, int count) | |
1385 | { | |
1386 | u32 header; | |
1387 | ||
1388 | if (WARN_ON(count > VDO_MAX_SIZE - 1)) | |
1389 | count = VDO_MAX_SIZE - 1; | |
1390 | ||
1391 | /* set VDM header with VID & CMD */ | |
1392 | header = VDO(vid, ((vid & USB_SID_PD) == USB_SID_PD) ? | |
1393 | 1 : (PD_VDO_CMD(cmd) <= CMD_ATTENTION), cmd); | |
1394 | tcpm_queue_vdm(port, header, data, count); | |
f0690a25 GR |
1395 | } |
1396 | ||
1397 | static unsigned int vdm_ready_timeout(u32 vdm_hdr) | |
1398 | { | |
1399 | unsigned int timeout; | |
1400 | int cmd = PD_VDO_CMD(vdm_hdr); | |
1401 | ||
1402 | /* its not a structured VDM command */ | |
1403 | if (!PD_VDO_SVDM(vdm_hdr)) | |
1404 | return PD_T_VDM_UNSTRUCTURED; | |
1405 | ||
1406 | switch (PD_VDO_CMDT(vdm_hdr)) { | |
1407 | case CMDT_INIT: | |
1408 | if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE) | |
1409 | timeout = PD_T_VDM_WAIT_MODE_E; | |
1410 | else | |
1411 | timeout = PD_T_VDM_SNDR_RSP; | |
1412 | break; | |
1413 | default: | |
1414 | if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE) | |
1415 | timeout = PD_T_VDM_E_MODE; | |
1416 | else | |
1417 | timeout = PD_T_VDM_RCVR_RSP; | |
1418 | break; | |
1419 | } | |
1420 | return timeout; | |
1421 | } | |
1422 | ||
1423 | static void vdm_run_state_machine(struct tcpm_port *port) | |
1424 | { | |
1425 | struct pd_message msg; | |
1426 | int i, res; | |
1427 | ||
1428 | switch (port->vdm_state) { | |
1429 | case VDM_STATE_READY: | |
1430 | /* Only transmit VDM if attached */ | |
1431 | if (!port->attached) { | |
1432 | port->vdm_state = VDM_STATE_ERR_BUSY; | |
1433 | break; | |
1434 | } | |
1435 | ||
1436 | /* | |
1437 | * if there's traffic or we're not in PDO ready state don't send | |
1438 | * a VDM. | |
1439 | */ | |
1440 | if (port->state != SRC_READY && port->state != SNK_READY) | |
1441 | break; | |
1442 | ||
1443 | /* Prepare and send VDM */ | |
1444 | memset(&msg, 0, sizeof(msg)); | |
1445 | msg.header = PD_HEADER_LE(PD_DATA_VENDOR_DEF, | |
1446 | port->pwr_role, | |
1447 | port->data_role, | |
2eadc33f | 1448 | port->negotiated_rev, |
f0690a25 GR |
1449 | port->message_id, port->vdo_count); |
1450 | for (i = 0; i < port->vdo_count; i++) | |
1451 | msg.payload[i] = cpu_to_le32(port->vdo_data[i]); | |
1452 | res = tcpm_pd_transmit(port, TCPC_TX_SOP, &msg); | |
1453 | if (res < 0) { | |
1454 | port->vdm_state = VDM_STATE_ERR_SEND; | |
1455 | } else { | |
1456 | unsigned long timeout; | |
1457 | ||
1458 | port->vdm_retries = 0; | |
1459 | port->vdm_state = VDM_STATE_BUSY; | |
1460 | timeout = vdm_ready_timeout(port->vdo_data[0]); | |
3ed8e1c2 | 1461 | mod_vdm_delayed_work(port, timeout); |
f0690a25 GR |
1462 | } |
1463 | break; | |
1464 | case VDM_STATE_WAIT_RSP_BUSY: | |
1465 | port->vdo_data[0] = port->vdo_retry; | |
1466 | port->vdo_count = 1; | |
1467 | port->vdm_state = VDM_STATE_READY; | |
1468 | break; | |
1469 | case VDM_STATE_BUSY: | |
1470 | port->vdm_state = VDM_STATE_ERR_TMOUT; | |
1471 | break; | |
1472 | case VDM_STATE_ERR_SEND: | |
1473 | /* | |
1474 | * A partner which does not support USB PD will not reply, | |
1475 | * so this is not a fatal error. At the same time, some | |
1476 | * devices may not return GoodCRC under some circumstances, | |
1477 | * so we need to retry. | |
1478 | */ | |
1479 | if (port->vdm_retries < 3) { | |
1480 | tcpm_log(port, "VDM Tx error, retry"); | |
1481 | port->vdm_retries++; | |
1482 | port->vdm_state = VDM_STATE_READY; | |
1483 | } | |
1484 | break; | |
1485 | default: | |
1486 | break; | |
1487 | } | |
1488 | } | |
1489 | ||
3ed8e1c2 | 1490 | static void vdm_state_machine_work(struct kthread_work *work) |
f0690a25 | 1491 | { |
3ed8e1c2 | 1492 | struct tcpm_port *port = container_of(work, struct tcpm_port, vdm_state_machine); |
f0690a25 GR |
1493 | enum vdm_states prev_state; |
1494 | ||
1495 | mutex_lock(&port->lock); | |
1496 | ||
1497 | /* | |
1498 | * Continue running as long as the port is not busy and there was | |
1499 | * a state change. | |
1500 | */ | |
1501 | do { | |
1502 | prev_state = port->vdm_state; | |
1503 | vdm_run_state_machine(port); | |
1504 | } while (port->vdm_state != prev_state && | |
1505 | port->vdm_state != VDM_STATE_BUSY); | |
1506 | ||
1507 | mutex_unlock(&port->lock); | |
1508 | } | |
1509 | ||
5007e1b5 BJS |
1510 | enum pdo_err { |
1511 | PDO_NO_ERR, | |
1512 | PDO_ERR_NO_VSAFE5V, | |
1513 | PDO_ERR_VSAFE5V_NOT_FIRST, | |
1514 | PDO_ERR_PDO_TYPE_NOT_IN_ORDER, | |
1515 | PDO_ERR_FIXED_NOT_SORTED, | |
1516 | PDO_ERR_VARIABLE_BATT_NOT_SORTED, | |
1517 | PDO_ERR_DUPE_PDO, | |
2eadc33f AT |
1518 | PDO_ERR_PPS_APDO_NOT_SORTED, |
1519 | PDO_ERR_DUPE_PPS_APDO, | |
5007e1b5 BJS |
1520 | }; |
1521 | ||
1522 | static const char * const pdo_err_msg[] = { | |
1523 | [PDO_ERR_NO_VSAFE5V] = | |
1524 | " err: source/sink caps should atleast have vSafe5V", | |
1525 | [PDO_ERR_VSAFE5V_NOT_FIRST] = | |
1526 | " err: vSafe5V Fixed Supply Object Shall always be the first object", | |
1527 | [PDO_ERR_PDO_TYPE_NOT_IN_ORDER] = | |
1528 | " err: PDOs should be in the following order: Fixed; Battery; Variable", | |
1529 | [PDO_ERR_FIXED_NOT_SORTED] = | |
1530 | " err: Fixed supply pdos should be in increasing order of their fixed voltage", | |
1531 | [PDO_ERR_VARIABLE_BATT_NOT_SORTED] = | |
1532 | " err: Variable/Battery supply pdos should be in increasing order of their minimum voltage", | |
1533 | [PDO_ERR_DUPE_PDO] = | |
1534 | " err: Variable/Batt supply pdos cannot have same min/max voltage", | |
2eadc33f AT |
1535 | [PDO_ERR_PPS_APDO_NOT_SORTED] = |
1536 | " err: Programmable power supply apdos should be in increasing order of their maximum voltage", | |
1537 | [PDO_ERR_DUPE_PPS_APDO] = | |
1538 | " err: Programmable power supply apdos cannot have same min/max voltage and max current", | |
5007e1b5 BJS |
1539 | }; |
1540 | ||
1541 | static enum pdo_err tcpm_caps_err(struct tcpm_port *port, const u32 *pdo, | |
1542 | unsigned int nr_pdo) | |
1543 | { | |
1544 | unsigned int i; | |
1545 | ||
1546 | /* Should at least contain vSafe5v */ | |
1547 | if (nr_pdo < 1) | |
1548 | return PDO_ERR_NO_VSAFE5V; | |
1549 | ||
1550 | /* The vSafe5V Fixed Supply Object Shall always be the first object */ | |
1551 | if (pdo_type(pdo[0]) != PDO_TYPE_FIXED || | |
1552 | pdo_fixed_voltage(pdo[0]) != VSAFE5V) | |
1553 | return PDO_ERR_VSAFE5V_NOT_FIRST; | |
1554 | ||
1555 | for (i = 1; i < nr_pdo; i++) { | |
1556 | if (pdo_type(pdo[i]) < pdo_type(pdo[i - 1])) { | |
1557 | return PDO_ERR_PDO_TYPE_NOT_IN_ORDER; | |
1558 | } else if (pdo_type(pdo[i]) == pdo_type(pdo[i - 1])) { | |
1559 | enum pd_pdo_type type = pdo_type(pdo[i]); | |
1560 | ||
1561 | switch (type) { | |
1562 | /* | |
1563 | * The remaining Fixed Supply Objects, if | |
1564 | * present, shall be sent in voltage order; | |
1565 | * lowest to highest. | |
1566 | */ | |
1567 | case PDO_TYPE_FIXED: | |
1568 | if (pdo_fixed_voltage(pdo[i]) <= | |
1569 | pdo_fixed_voltage(pdo[i - 1])) | |
1570 | return PDO_ERR_FIXED_NOT_SORTED; | |
1571 | break; | |
1572 | /* | |
1573 | * The Battery Supply Objects and Variable | |
1574 | * supply, if present shall be sent in Minimum | |
1575 | * Voltage order; lowest to highest. | |
1576 | */ | |
1577 | case PDO_TYPE_VAR: | |
1578 | case PDO_TYPE_BATT: | |
1579 | if (pdo_min_voltage(pdo[i]) < | |
1580 | pdo_min_voltage(pdo[i - 1])) | |
1581 | return PDO_ERR_VARIABLE_BATT_NOT_SORTED; | |
1582 | else if ((pdo_min_voltage(pdo[i]) == | |
1583 | pdo_min_voltage(pdo[i - 1])) && | |
1584 | (pdo_max_voltage(pdo[i]) == | |
a684d8fd | 1585 | pdo_max_voltage(pdo[i - 1]))) |
5007e1b5 BJS |
1586 | return PDO_ERR_DUPE_PDO; |
1587 | break; | |
2eadc33f AT |
1588 | /* |
1589 | * The Programmable Power Supply APDOs, if present, | |
1590 | * shall be sent in Maximum Voltage order; | |
1591 | * lowest to highest. | |
1592 | */ | |
1593 | case PDO_TYPE_APDO: | |
1594 | if (pdo_apdo_type(pdo[i]) != APDO_TYPE_PPS) | |
1595 | break; | |
1596 | ||
1b6af2f5 AT |
1597 | if (pdo_pps_apdo_max_voltage(pdo[i]) < |
1598 | pdo_pps_apdo_max_voltage(pdo[i - 1])) | |
2eadc33f AT |
1599 | return PDO_ERR_PPS_APDO_NOT_SORTED; |
1600 | else if (pdo_pps_apdo_min_voltage(pdo[i]) == | |
1601 | pdo_pps_apdo_min_voltage(pdo[i - 1]) && | |
1602 | pdo_pps_apdo_max_voltage(pdo[i]) == | |
1603 | pdo_pps_apdo_max_voltage(pdo[i - 1]) && | |
1604 | pdo_pps_apdo_max_current(pdo[i]) == | |
1605 | pdo_pps_apdo_max_current(pdo[i - 1])) | |
1606 | return PDO_ERR_DUPE_PPS_APDO; | |
1607 | break; | |
5007e1b5 BJS |
1608 | default: |
1609 | tcpm_log_force(port, " Unknown pdo type"); | |
1610 | } | |
1611 | } | |
1612 | } | |
1613 | ||
1614 | return PDO_NO_ERR; | |
1615 | } | |
1616 | ||
1617 | static int tcpm_validate_caps(struct tcpm_port *port, const u32 *pdo, | |
1618 | unsigned int nr_pdo) | |
1619 | { | |
1620 | enum pdo_err err_index = tcpm_caps_err(port, pdo, nr_pdo); | |
1621 | ||
1622 | if (err_index != PDO_NO_ERR) { | |
1623 | tcpm_log_force(port, " %s", pdo_err_msg[err_index]); | |
1624 | return -EINVAL; | |
1625 | } | |
1626 | ||
1627 | return 0; | |
1628 | } | |
1629 | ||
8face9aa | 1630 | static int tcpm_altmode_enter(struct typec_altmode *altmode, u32 *vdo) |
e9576fe8 HK |
1631 | { |
1632 | struct tcpm_port *port = typec_altmode_get_drvdata(altmode); | |
1633 | u32 header; | |
1634 | ||
8face9aa | 1635 | header = VDO(altmode->svid, vdo ? 2 : 1, CMD_ENTER_MODE); |
e9576fe8 HK |
1636 | header |= VDO_OPOS(altmode->mode); |
1637 | ||
03eafcfb | 1638 | tcpm_queue_vdm_unlocked(port, header, vdo, vdo ? 1 : 0); |
e9576fe8 HK |
1639 | return 0; |
1640 | } | |
1641 | ||
1642 | static int tcpm_altmode_exit(struct typec_altmode *altmode) | |
1643 | { | |
1644 | struct tcpm_port *port = typec_altmode_get_drvdata(altmode); | |
1645 | u32 header; | |
1646 | ||
e9576fe8 HK |
1647 | header = VDO(altmode->svid, 1, CMD_EXIT_MODE); |
1648 | header |= VDO_OPOS(altmode->mode); | |
1649 | ||
03eafcfb | 1650 | tcpm_queue_vdm_unlocked(port, header, NULL, 0); |
e9576fe8 HK |
1651 | return 0; |
1652 | } | |
1653 | ||
1654 | static int tcpm_altmode_vdm(struct typec_altmode *altmode, | |
1655 | u32 header, const u32 *data, int count) | |
1656 | { | |
1657 | struct tcpm_port *port = typec_altmode_get_drvdata(altmode); | |
1658 | ||
03eafcfb | 1659 | tcpm_queue_vdm_unlocked(port, header, data, count - 1); |
3ed8e1c2 | 1660 | |
e9576fe8 HK |
1661 | return 0; |
1662 | } | |
1663 | ||
1664 | static const struct typec_altmode_ops tcpm_altmode_ops = { | |
1665 | .enter = tcpm_altmode_enter, | |
1666 | .exit = tcpm_altmode_exit, | |
1667 | .vdm = tcpm_altmode_vdm, | |
1668 | }; | |
1669 | ||
f0690a25 GR |
1670 | /* |
1671 | * PD (data, control) command handling functions | |
1672 | */ | |
64f7c494 AT |
1673 | static inline enum tcpm_state ready_state(struct tcpm_port *port) |
1674 | { | |
1675 | if (port->pwr_role == TYPEC_SOURCE) | |
1676 | return SRC_READY; | |
1677 | else | |
1678 | return SNK_READY; | |
1679 | } | |
2eadc33f AT |
1680 | |
1681 | static int tcpm_pd_send_control(struct tcpm_port *port, | |
1682 | enum pd_ctrl_msg_type type); | |
1683 | ||
64f7c494 AT |
1684 | static void tcpm_handle_alert(struct tcpm_port *port, const __le32 *payload, |
1685 | int cnt) | |
1686 | { | |
1687 | u32 p0 = le32_to_cpu(payload[0]); | |
1688 | unsigned int type = usb_pd_ado_type(p0); | |
1689 | ||
1690 | if (!type) { | |
1691 | tcpm_log(port, "Alert message received with no type"); | |
1692 | return; | |
1693 | } | |
1694 | ||
1695 | /* Just handling non-battery alerts for now */ | |
1696 | if (!(type & USB_PD_ADO_TYPE_BATT_STATUS_CHANGE)) { | |
1697 | switch (port->state) { | |
1698 | case SRC_READY: | |
1699 | case SNK_READY: | |
1700 | tcpm_set_state(port, GET_STATUS_SEND, 0); | |
1701 | break; | |
1702 | default: | |
1703 | tcpm_queue_message(port, PD_MSG_CTRL_WAIT); | |
1704 | break; | |
1705 | } | |
1706 | } | |
1707 | } | |
1708 | ||
f321a02c BJS |
1709 | static int tcpm_set_auto_vbus_discharge_threshold(struct tcpm_port *port, |
1710 | enum typec_pwr_opmode mode, bool pps_active, | |
1711 | u32 requested_vbus_voltage) | |
1712 | { | |
1713 | int ret; | |
1714 | ||
1715 | if (!port->tcpc->set_auto_vbus_discharge_threshold) | |
1716 | return 0; | |
1717 | ||
1718 | ret = port->tcpc->set_auto_vbus_discharge_threshold(port->tcpc, mode, pps_active, | |
1719 | requested_vbus_voltage); | |
1720 | tcpm_log_force(port, | |
1721 | "set_auto_vbus_discharge_threshold mode:%d pps_active:%c vbus:%u ret:%d", | |
1722 | mode, pps_active ? 'y' : 'n', requested_vbus_voltage, ret); | |
1723 | ||
1724 | return ret; | |
1725 | } | |
1726 | ||
f0690a25 GR |
1727 | static void tcpm_pd_data_request(struct tcpm_port *port, |
1728 | const struct pd_message *msg) | |
1729 | { | |
1730 | enum pd_data_msg_type type = pd_header_type_le(msg->header); | |
1731 | unsigned int cnt = pd_header_cnt_le(msg->header); | |
2eadc33f | 1732 | unsigned int rev = pd_header_rev_le(msg->header); |
f0690a25 | 1733 | unsigned int i; |
8115240c | 1734 | enum frs_typec_current partner_frs_current; |
8dc4bd07 BJS |
1735 | bool frs_enable; |
1736 | int ret; | |
f0690a25 GR |
1737 | |
1738 | switch (type) { | |
1739 | case PD_DATA_SOURCE_CAP: | |
1740 | if (port->pwr_role != TYPEC_SINK) | |
1741 | break; | |
1742 | ||
1743 | for (i = 0; i < cnt; i++) | |
1744 | port->source_caps[i] = le32_to_cpu(msg->payload[i]); | |
1745 | ||
1746 | port->nr_source_caps = cnt; | |
1747 | ||
1748 | tcpm_log_source_caps(port); | |
1749 | ||
5007e1b5 BJS |
1750 | tcpm_validate_caps(port, port->source_caps, |
1751 | port->nr_source_caps); | |
1752 | ||
2eadc33f AT |
1753 | /* |
1754 | * Adjust revision in subsequent message headers, as required, | |
1755 | * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't | |
1756 | * support Rev 1.0 so just do nothing in that scenario. | |
1757 | */ | |
1758 | if (rev == PD_REV10) | |
1759 | break; | |
1760 | ||
1761 | if (rev < PD_MAX_REV) | |
1762 | port->negotiated_rev = rev; | |
1763 | ||
f0690a25 GR |
1764 | /* |
1765 | * This message may be received even if VBUS is not | |
1766 | * present. This is quite unexpected; see USB PD | |
1767 | * specification, sections 8.3.3.6.3.1 and 8.3.3.6.3.2. | |
1768 | * However, at the same time, we must be ready to | |
1769 | * receive this message and respond to it 15ms after | |
1770 | * receiving PS_RDY during power swap operations, no matter | |
1771 | * if VBUS is available or not (USB PD specification, | |
1772 | * section 6.5.9.2). | |
1773 | * So we need to accept the message either way, | |
1774 | * but be prepared to keep waiting for VBUS after it was | |
1775 | * handled. | |
1776 | */ | |
1777 | tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0); | |
1778 | break; | |
1779 | case PD_DATA_REQUEST: | |
1780 | if (port->pwr_role != TYPEC_SOURCE || | |
1781 | cnt != 1) { | |
1782 | tcpm_queue_message(port, PD_MSG_CTRL_REJECT); | |
1783 | break; | |
1784 | } | |
2eadc33f AT |
1785 | |
1786 | /* | |
1787 | * Adjust revision in subsequent message headers, as required, | |
1788 | * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't | |
1789 | * support Rev 1.0 so just reject in that scenario. | |
1790 | */ | |
1791 | if (rev == PD_REV10) { | |
1792 | tcpm_queue_message(port, PD_MSG_CTRL_REJECT); | |
1793 | break; | |
1794 | } | |
1795 | ||
1796 | if (rev < PD_MAX_REV) | |
1797 | port->negotiated_rev = rev; | |
1798 | ||
f0690a25 GR |
1799 | port->sink_request = le32_to_cpu(msg->payload[0]); |
1800 | tcpm_set_state(port, SRC_NEGOTIATE_CAPABILITIES, 0); | |
1801 | break; | |
1802 | case PD_DATA_SINK_CAP: | |
1803 | /* We don't do anything with this at the moment... */ | |
1804 | for (i = 0; i < cnt; i++) | |
1805 | port->sink_caps[i] = le32_to_cpu(msg->payload[i]); | |
8dc4bd07 | 1806 | |
8115240c | 1807 | partner_frs_current = (port->sink_caps[0] & PDO_FIXED_FRS_CURR_MASK) >> |
8dc4bd07 | 1808 | PDO_FIXED_FRS_CURR_SHIFT; |
8115240c BJS |
1809 | frs_enable = partner_frs_current && (partner_frs_current <= |
1810 | port->new_source_frs_current); | |
8dc4bd07 BJS |
1811 | tcpm_log(port, |
1812 | "Port partner FRS capable partner_frs_current:%u port_frs_current:%u enable:%c", | |
8115240c | 1813 | partner_frs_current, port->new_source_frs_current, frs_enable ? 'y' : 'n'); |
8dc4bd07 BJS |
1814 | if (frs_enable) { |
1815 | ret = port->tcpc->enable_frs(port->tcpc, true); | |
1816 | tcpm_log(port, "Enable FRS %s, ret:%d\n", ret ? "fail" : "success", ret); | |
1817 | } | |
1818 | ||
f0690a25 | 1819 | port->nr_sink_caps = cnt; |
8dc4bd07 BJS |
1820 | port->sink_cap_done = true; |
1821 | tcpm_set_state(port, SNK_READY, 0); | |
f0690a25 GR |
1822 | break; |
1823 | case PD_DATA_VENDOR_DEF: | |
1824 | tcpm_handle_vdm_request(port, msg->payload, cnt); | |
1825 | break; | |
1826 | case PD_DATA_BIST: | |
1827 | if (port->state == SRC_READY || port->state == SNK_READY) { | |
1828 | port->bist_request = le32_to_cpu(msg->payload[0]); | |
1829 | tcpm_set_state(port, BIST_RX, 0); | |
1830 | } | |
1831 | break; | |
64f7c494 AT |
1832 | case PD_DATA_ALERT: |
1833 | tcpm_handle_alert(port, msg->payload, cnt); | |
1834 | break; | |
1835 | case PD_DATA_BATT_STATUS: | |
1836 | case PD_DATA_GET_COUNTRY_INFO: | |
1837 | /* Currently unsupported */ | |
1838 | tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP); | |
1839 | break; | |
f0690a25 GR |
1840 | default: |
1841 | tcpm_log(port, "Unhandled data message type %#x", type); | |
1842 | break; | |
1843 | } | |
1844 | } | |
1845 | ||
2eadc33f AT |
1846 | static void tcpm_pps_complete(struct tcpm_port *port, int result) |
1847 | { | |
1848 | if (port->pps_pending) { | |
1849 | port->pps_status = result; | |
1850 | port->pps_pending = false; | |
1851 | complete(&port->pps_complete); | |
1852 | } | |
1853 | } | |
1854 | ||
f0690a25 GR |
1855 | static void tcpm_pd_ctrl_request(struct tcpm_port *port, |
1856 | const struct pd_message *msg) | |
1857 | { | |
1858 | enum pd_ctrl_msg_type type = pd_header_type_le(msg->header); | |
1859 | enum tcpm_state next_state; | |
1860 | ||
1861 | switch (type) { | |
1862 | case PD_CTRL_GOOD_CRC: | |
1863 | case PD_CTRL_PING: | |
1864 | break; | |
1865 | case PD_CTRL_GET_SOURCE_CAP: | |
1866 | switch (port->state) { | |
1867 | case SRC_READY: | |
1868 | case SNK_READY: | |
1869 | tcpm_queue_message(port, PD_MSG_DATA_SOURCE_CAP); | |
1870 | break; | |
1871 | default: | |
1872 | tcpm_queue_message(port, PD_MSG_CTRL_REJECT); | |
1873 | break; | |
1874 | } | |
1875 | break; | |
1876 | case PD_CTRL_GET_SINK_CAP: | |
1877 | switch (port->state) { | |
1878 | case SRC_READY: | |
1879 | case SNK_READY: | |
1880 | tcpm_queue_message(port, PD_MSG_DATA_SINK_CAP); | |
1881 | break; | |
1882 | default: | |
1883 | tcpm_queue_message(port, PD_MSG_CTRL_REJECT); | |
1884 | break; | |
1885 | } | |
1886 | break; | |
1887 | case PD_CTRL_GOTO_MIN: | |
1888 | break; | |
1889 | case PD_CTRL_PS_RDY: | |
1890 | switch (port->state) { | |
1891 | case SNK_TRANSITION_SINK: | |
1892 | if (port->vbus_present) { | |
1893 | tcpm_set_current_limit(port, | |
1894 | port->current_limit, | |
1895 | port->supply_voltage); | |
8bf05746 | 1896 | port->explicit_contract = true; |
f321a02c BJS |
1897 | tcpm_set_auto_vbus_discharge_threshold(port, |
1898 | TYPEC_PWR_MODE_PD, | |
1899 | port->pps_data.active, | |
1900 | port->supply_voltage); | |
f0690a25 GR |
1901 | tcpm_set_state(port, SNK_READY, 0); |
1902 | } else { | |
1903 | /* | |
1904 | * Seen after power swap. Keep waiting for VBUS | |
1905 | * in a transitional state. | |
1906 | */ | |
1907 | tcpm_set_state(port, | |
1908 | SNK_TRANSITION_SINK_VBUS, 0); | |
1909 | } | |
1910 | break; | |
b965b631 | 1911 | case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED: |
f0690a25 GR |
1912 | tcpm_set_state(port, PR_SWAP_SRC_SNK_SINK_ON, 0); |
1913 | break; | |
1914 | case PR_SWAP_SNK_SRC_SINK_OFF: | |
1915 | tcpm_set_state(port, PR_SWAP_SNK_SRC_SOURCE_ON, 0); | |
1916 | break; | |
1917 | case VCONN_SWAP_WAIT_FOR_VCONN: | |
1918 | tcpm_set_state(port, VCONN_SWAP_TURN_OFF_VCONN, 0); | |
1919 | break; | |
8dc4bd07 BJS |
1920 | case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF: |
1921 | tcpm_set_state(port, FR_SWAP_SNK_SRC_NEW_SINK_READY, 0); | |
1922 | break; | |
f0690a25 GR |
1923 | default: |
1924 | break; | |
1925 | } | |
1926 | break; | |
1927 | case PD_CTRL_REJECT: | |
1928 | case PD_CTRL_WAIT: | |
64f7c494 | 1929 | case PD_CTRL_NOT_SUPP: |
f0690a25 GR |
1930 | switch (port->state) { |
1931 | case SNK_NEGOTIATE_CAPABILITIES: | |
1932 | /* USB PD specification, Figure 8-43 */ | |
1933 | if (port->explicit_contract) | |
1934 | next_state = SNK_READY; | |
1935 | else | |
1936 | next_state = SNK_WAIT_CAPABILITIES; | |
1937 | tcpm_set_state(port, next_state, 0); | |
1938 | break; | |
2eadc33f AT |
1939 | case SNK_NEGOTIATE_PPS_CAPABILITIES: |
1940 | /* Revert data back from any requested PPS updates */ | |
1941 | port->pps_data.out_volt = port->supply_voltage; | |
1942 | port->pps_data.op_curr = port->current_limit; | |
1943 | port->pps_status = (type == PD_CTRL_WAIT ? | |
1944 | -EAGAIN : -EOPNOTSUPP); | |
1945 | tcpm_set_state(port, SNK_READY, 0); | |
1946 | break; | |
f0690a25 GR |
1947 | case DR_SWAP_SEND: |
1948 | port->swap_status = (type == PD_CTRL_WAIT ? | |
1949 | -EAGAIN : -EOPNOTSUPP); | |
1950 | tcpm_set_state(port, DR_SWAP_CANCEL, 0); | |
1951 | break; | |
1952 | case PR_SWAP_SEND: | |
1953 | port->swap_status = (type == PD_CTRL_WAIT ? | |
1954 | -EAGAIN : -EOPNOTSUPP); | |
1955 | tcpm_set_state(port, PR_SWAP_CANCEL, 0); | |
1956 | break; | |
1957 | case VCONN_SWAP_SEND: | |
1958 | port->swap_status = (type == PD_CTRL_WAIT ? | |
1959 | -EAGAIN : -EOPNOTSUPP); | |
1960 | tcpm_set_state(port, VCONN_SWAP_CANCEL, 0); | |
1961 | break; | |
8dc4bd07 BJS |
1962 | case FR_SWAP_SEND: |
1963 | tcpm_set_state(port, FR_SWAP_CANCEL, 0); | |
1964 | break; | |
1965 | case GET_SINK_CAP: | |
1966 | port->sink_cap_done = true; | |
1967 | tcpm_set_state(port, ready_state(port), 0); | |
1968 | break; | |
f0690a25 GR |
1969 | default: |
1970 | break; | |
1971 | } | |
1972 | break; | |
1973 | case PD_CTRL_ACCEPT: | |
1974 | switch (port->state) { | |
1975 | case SNK_NEGOTIATE_CAPABILITIES: | |
2eadc33f AT |
1976 | port->pps_data.active = false; |
1977 | tcpm_set_state(port, SNK_TRANSITION_SINK, 0); | |
1978 | break; | |
1979 | case SNK_NEGOTIATE_PPS_CAPABILITIES: | |
1980 | port->pps_data.active = true; | |
1981 | port->supply_voltage = port->pps_data.out_volt; | |
1982 | port->current_limit = port->pps_data.op_curr; | |
f0690a25 GR |
1983 | tcpm_set_state(port, SNK_TRANSITION_SINK, 0); |
1984 | break; | |
1985 | case SOFT_RESET_SEND: | |
1986 | port->message_id = 0; | |
5fec4b54 | 1987 | port->rx_msgid = -1; |
f0690a25 GR |
1988 | if (port->pwr_role == TYPEC_SOURCE) |
1989 | next_state = SRC_SEND_CAPABILITIES; | |
1990 | else | |
1991 | next_state = SNK_WAIT_CAPABILITIES; | |
1992 | tcpm_set_state(port, next_state, 0); | |
1993 | break; | |
1994 | case DR_SWAP_SEND: | |
1995 | tcpm_set_state(port, DR_SWAP_CHANGE_DR, 0); | |
1996 | break; | |
1997 | case PR_SWAP_SEND: | |
1998 | tcpm_set_state(port, PR_SWAP_START, 0); | |
1999 | break; | |
2000 | case VCONN_SWAP_SEND: | |
2001 | tcpm_set_state(port, VCONN_SWAP_START, 0); | |
2002 | break; | |
8dc4bd07 BJS |
2003 | case FR_SWAP_SEND: |
2004 | tcpm_set_state(port, FR_SWAP_SNK_SRC_TRANSITION_TO_OFF, 0); | |
2005 | break; | |
f0690a25 GR |
2006 | default: |
2007 | break; | |
2008 | } | |
2009 | break; | |
2010 | case PD_CTRL_SOFT_RESET: | |
2011 | tcpm_set_state(port, SOFT_RESET, 0); | |
2012 | break; | |
2013 | case PD_CTRL_DR_SWAP: | |
6ecc632d | 2014 | if (port->typec_caps.data != TYPEC_PORT_DRD) { |
f0690a25 GR |
2015 | tcpm_queue_message(port, PD_MSG_CTRL_REJECT); |
2016 | break; | |
2017 | } | |
2018 | /* | |
2019 | * XXX | |
2020 | * 6.3.9: If an alternate mode is active, a request to swap | |
2021 | * alternate modes shall trigger a port reset. | |
2022 | */ | |
2023 | switch (port->state) { | |
2024 | case SRC_READY: | |
2025 | case SNK_READY: | |
2026 | tcpm_set_state(port, DR_SWAP_ACCEPT, 0); | |
2027 | break; | |
2028 | default: | |
2029 | tcpm_queue_message(port, PD_MSG_CTRL_WAIT); | |
2030 | break; | |
2031 | } | |
2032 | break; | |
2033 | case PD_CTRL_PR_SWAP: | |
9b0ae699 | 2034 | if (port->port_type != TYPEC_PORT_DRP) { |
f0690a25 GR |
2035 | tcpm_queue_message(port, PD_MSG_CTRL_REJECT); |
2036 | break; | |
2037 | } | |
2038 | switch (port->state) { | |
2039 | case SRC_READY: | |
2040 | case SNK_READY: | |
2041 | tcpm_set_state(port, PR_SWAP_ACCEPT, 0); | |
2042 | break; | |
2043 | default: | |
2044 | tcpm_queue_message(port, PD_MSG_CTRL_WAIT); | |
2045 | break; | |
2046 | } | |
2047 | break; | |
2048 | case PD_CTRL_VCONN_SWAP: | |
2049 | switch (port->state) { | |
2050 | case SRC_READY: | |
2051 | case SNK_READY: | |
2052 | tcpm_set_state(port, VCONN_SWAP_ACCEPT, 0); | |
2053 | break; | |
2054 | default: | |
2055 | tcpm_queue_message(port, PD_MSG_CTRL_WAIT); | |
2056 | break; | |
2057 | } | |
2058 | break; | |
64f7c494 AT |
2059 | case PD_CTRL_GET_SOURCE_CAP_EXT: |
2060 | case PD_CTRL_GET_STATUS: | |
2061 | case PD_CTRL_FR_SWAP: | |
2062 | case PD_CTRL_GET_PPS_STATUS: | |
2063 | case PD_CTRL_GET_COUNTRY_CODES: | |
2064 | /* Currently not supported */ | |
2065 | tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP); | |
2066 | break; | |
f0690a25 GR |
2067 | default: |
2068 | tcpm_log(port, "Unhandled ctrl message type %#x", type); | |
2069 | break; | |
2070 | } | |
2071 | } | |
2072 | ||
64f7c494 AT |
2073 | static void tcpm_pd_ext_msg_request(struct tcpm_port *port, |
2074 | const struct pd_message *msg) | |
2075 | { | |
2076 | enum pd_ext_msg_type type = pd_header_type_le(msg->header); | |
2077 | unsigned int data_size = pd_ext_header_data_size_le(msg->ext_msg.header); | |
2078 | ||
25242703 | 2079 | if (!(msg->ext_msg.header & PD_EXT_HDR_CHUNKED)) { |
64f7c494 AT |
2080 | tcpm_log(port, "Unchunked extended messages unsupported"); |
2081 | return; | |
2082 | } | |
2083 | ||
2084 | if (data_size > PD_EXT_MAX_CHUNK_DATA) { | |
2085 | tcpm_log(port, "Chunk handling not yet supported"); | |
2086 | return; | |
2087 | } | |
2088 | ||
2089 | switch (type) { | |
2090 | case PD_EXT_STATUS: | |
2091 | /* | |
2092 | * If PPS related events raised then get PPS status to clear | |
2093 | * (see USB PD 3.0 Spec, 6.5.2.4) | |
2094 | */ | |
2095 | if (msg->ext_msg.data[USB_PD_EXT_SDB_EVENT_FLAGS] & | |
2096 | USB_PD_EXT_SDB_PPS_EVENTS) | |
2097 | tcpm_set_state(port, GET_PPS_STATUS_SEND, 0); | |
2098 | else | |
2099 | tcpm_set_state(port, ready_state(port), 0); | |
2100 | break; | |
2101 | case PD_EXT_PPS_STATUS: | |
2102 | /* | |
2103 | * For now the PPS status message is used to clear events | |
2104 | * and nothing more. | |
2105 | */ | |
2106 | tcpm_set_state(port, ready_state(port), 0); | |
2107 | break; | |
2108 | case PD_EXT_SOURCE_CAP_EXT: | |
2109 | case PD_EXT_GET_BATT_CAP: | |
2110 | case PD_EXT_GET_BATT_STATUS: | |
2111 | case PD_EXT_BATT_CAP: | |
2112 | case PD_EXT_GET_MANUFACTURER_INFO: | |
2113 | case PD_EXT_MANUFACTURER_INFO: | |
2114 | case PD_EXT_SECURITY_REQUEST: | |
2115 | case PD_EXT_SECURITY_RESPONSE: | |
2116 | case PD_EXT_FW_UPDATE_REQUEST: | |
2117 | case PD_EXT_FW_UPDATE_RESPONSE: | |
2118 | case PD_EXT_COUNTRY_INFO: | |
2119 | case PD_EXT_COUNTRY_CODES: | |
2120 | tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP); | |
2121 | break; | |
2122 | default: | |
2123 | tcpm_log(port, "Unhandled extended message type %#x", type); | |
2124 | break; | |
2125 | } | |
2126 | } | |
2127 | ||
3ed8e1c2 | 2128 | static void tcpm_pd_rx_handler(struct kthread_work *work) |
f0690a25 GR |
2129 | { |
2130 | struct pd_rx_event *event = container_of(work, | |
2131 | struct pd_rx_event, work); | |
2132 | const struct pd_message *msg = &event->msg; | |
2133 | unsigned int cnt = pd_header_cnt_le(msg->header); | |
2134 | struct tcpm_port *port = event->port; | |
2135 | ||
2136 | mutex_lock(&port->lock); | |
2137 | ||
2138 | tcpm_log(port, "PD RX, header: %#x [%d]", le16_to_cpu(msg->header), | |
2139 | port->attached); | |
2140 | ||
2141 | if (port->attached) { | |
5fec4b54 GR |
2142 | enum pd_ctrl_msg_type type = pd_header_type_le(msg->header); |
2143 | unsigned int msgid = pd_header_msgid_le(msg->header); | |
2144 | ||
2145 | /* | |
2146 | * USB PD standard, 6.6.1.2: | |
2147 | * "... if MessageID value in a received Message is the | |
2148 | * same as the stored value, the receiver shall return a | |
2149 | * GoodCRC Message with that MessageID value and drop | |
2150 | * the Message (this is a retry of an already received | |
2151 | * Message). Note: this shall not apply to the Soft_Reset | |
2152 | * Message which always has a MessageID value of zero." | |
2153 | */ | |
2154 | if (msgid == port->rx_msgid && type != PD_CTRL_SOFT_RESET) | |
2155 | goto done; | |
2156 | port->rx_msgid = msgid; | |
2157 | ||
f0690a25 GR |
2158 | /* |
2159 | * If both ends believe to be DFP/host, we have a data role | |
2160 | * mismatch. | |
2161 | */ | |
2162 | if (!!(le16_to_cpu(msg->header) & PD_HEADER_DATA_ROLE) == | |
2163 | (port->data_role == TYPEC_HOST)) { | |
2164 | tcpm_log(port, | |
2165 | "Data role mismatch, initiating error recovery"); | |
2166 | tcpm_set_state(port, ERROR_RECOVERY, 0); | |
2167 | } else { | |
64f7c494 AT |
2168 | if (msg->header & PD_HEADER_EXT_HDR) |
2169 | tcpm_pd_ext_msg_request(port, msg); | |
2170 | else if (cnt) | |
f0690a25 GR |
2171 | tcpm_pd_data_request(port, msg); |
2172 | else | |
2173 | tcpm_pd_ctrl_request(port, msg); | |
2174 | } | |
2175 | } | |
2176 | ||
5fec4b54 | 2177 | done: |
f0690a25 GR |
2178 | mutex_unlock(&port->lock); |
2179 | kfree(event); | |
2180 | } | |
2181 | ||
2182 | void tcpm_pd_receive(struct tcpm_port *port, const struct pd_message *msg) | |
2183 | { | |
2184 | struct pd_rx_event *event; | |
2185 | ||
2186 | event = kzalloc(sizeof(*event), GFP_ATOMIC); | |
2187 | if (!event) | |
2188 | return; | |
2189 | ||
3ed8e1c2 | 2190 | kthread_init_work(&event->work, tcpm_pd_rx_handler); |
f0690a25 GR |
2191 | event->port = port; |
2192 | memcpy(&event->msg, msg, sizeof(*msg)); | |
3ed8e1c2 | 2193 | kthread_queue_work(port->wq, &event->work); |
f0690a25 GR |
2194 | } |
2195 | EXPORT_SYMBOL_GPL(tcpm_pd_receive); | |
2196 | ||
2197 | static int tcpm_pd_send_control(struct tcpm_port *port, | |
2198 | enum pd_ctrl_msg_type type) | |
2199 | { | |
2200 | struct pd_message msg; | |
2201 | ||
2202 | memset(&msg, 0, sizeof(msg)); | |
2203 | msg.header = PD_HEADER_LE(type, port->pwr_role, | |
2204 | port->data_role, | |
2eadc33f | 2205 | port->negotiated_rev, |
f0690a25 GR |
2206 | port->message_id, 0); |
2207 | ||
2208 | return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg); | |
2209 | } | |
2210 | ||
2211 | /* | |
2212 | * Send queued message without affecting state. | |
2213 | * Return true if state machine should go back to sleep, | |
2214 | * false otherwise. | |
2215 | */ | |
2216 | static bool tcpm_send_queued_message(struct tcpm_port *port) | |
2217 | { | |
2218 | enum pd_msg_request queued_message; | |
2219 | ||
2220 | do { | |
2221 | queued_message = port->queued_message; | |
2222 | port->queued_message = PD_MSG_NONE; | |
2223 | ||
2224 | switch (queued_message) { | |
2225 | case PD_MSG_CTRL_WAIT: | |
2226 | tcpm_pd_send_control(port, PD_CTRL_WAIT); | |
2227 | break; | |
2228 | case PD_MSG_CTRL_REJECT: | |
2229 | tcpm_pd_send_control(port, PD_CTRL_REJECT); | |
2230 | break; | |
64f7c494 AT |
2231 | case PD_MSG_CTRL_NOT_SUPP: |
2232 | tcpm_pd_send_control(port, PD_CTRL_NOT_SUPP); | |
2233 | break; | |
f0690a25 GR |
2234 | case PD_MSG_DATA_SINK_CAP: |
2235 | tcpm_pd_send_sink_caps(port); | |
2236 | break; | |
2237 | case PD_MSG_DATA_SOURCE_CAP: | |
2238 | tcpm_pd_send_source_caps(port); | |
2239 | break; | |
2240 | default: | |
2241 | break; | |
2242 | } | |
2243 | } while (port->queued_message != PD_MSG_NONE); | |
2244 | ||
2245 | if (port->delayed_state != INVALID_STATE) { | |
3ed8e1c2 BJS |
2246 | if (ktime_after(port->delayed_runtime, ktime_get())) { |
2247 | mod_tcpm_delayed_work(port, ktime_to_ms(ktime_sub(port->delayed_runtime, | |
2248 | ktime_get()))); | |
f0690a25 GR |
2249 | return true; |
2250 | } | |
2251 | port->delayed_state = INVALID_STATE; | |
2252 | } | |
2253 | return false; | |
2254 | } | |
2255 | ||
2256 | static int tcpm_pd_check_request(struct tcpm_port *port) | |
2257 | { | |
2258 | u32 pdo, rdo = port->sink_request; | |
2259 | unsigned int max, op, pdo_max, index; | |
2260 | enum pd_pdo_type type; | |
2261 | ||
2262 | index = rdo_index(rdo); | |
2263 | if (!index || index > port->nr_src_pdo) | |
2264 | return -EINVAL; | |
2265 | ||
2266 | pdo = port->src_pdo[index - 1]; | |
2267 | type = pdo_type(pdo); | |
2268 | switch (type) { | |
2269 | case PDO_TYPE_FIXED: | |
2270 | case PDO_TYPE_VAR: | |
2271 | max = rdo_max_current(rdo); | |
2272 | op = rdo_op_current(rdo); | |
2273 | pdo_max = pdo_max_current(pdo); | |
2274 | ||
2275 | if (op > pdo_max) | |
2276 | return -EINVAL; | |
2277 | if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH)) | |
2278 | return -EINVAL; | |
2279 | ||
2280 | if (type == PDO_TYPE_FIXED) | |
2281 | tcpm_log(port, | |
2282 | "Requested %u mV, %u mA for %u / %u mA", | |
2283 | pdo_fixed_voltage(pdo), pdo_max, op, max); | |
2284 | else | |
2285 | tcpm_log(port, | |
2286 | "Requested %u -> %u mV, %u mA for %u / %u mA", | |
2287 | pdo_min_voltage(pdo), pdo_max_voltage(pdo), | |
2288 | pdo_max, op, max); | |
2289 | break; | |
2290 | case PDO_TYPE_BATT: | |
2291 | max = rdo_max_power(rdo); | |
2292 | op = rdo_op_power(rdo); | |
2293 | pdo_max = pdo_max_power(pdo); | |
2294 | ||
2295 | if (op > pdo_max) | |
2296 | return -EINVAL; | |
2297 | if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH)) | |
2298 | return -EINVAL; | |
2299 | tcpm_log(port, | |
2300 | "Requested %u -> %u mV, %u mW for %u / %u mW", | |
2301 | pdo_min_voltage(pdo), pdo_max_voltage(pdo), | |
2302 | pdo_max, op, max); | |
2303 | break; | |
2304 | default: | |
2305 | return -EINVAL; | |
2306 | } | |
2307 | ||
2308 | port->op_vsafe5v = index == 1; | |
2309 | ||
2310 | return 0; | |
2311 | } | |
2312 | ||
53fe0de9 LJ |
2313 | #define min_power(x, y) min(pdo_max_power(x), pdo_max_power(y)) |
2314 | #define min_current(x, y) min(pdo_max_current(x), pdo_max_current(y)) | |
2315 | ||
2316 | static int tcpm_pd_select_pdo(struct tcpm_port *port, int *sink_pdo, | |
2317 | int *src_pdo) | |
f0690a25 | 2318 | { |
53fe0de9 LJ |
2319 | unsigned int i, j, max_src_mv = 0, min_src_mv = 0, max_mw = 0, |
2320 | max_mv = 0, src_mw = 0, src_ma = 0, max_snk_mv = 0, | |
2321 | min_snk_mv = 0; | |
f0690a25 GR |
2322 | int ret = -EINVAL; |
2323 | ||
2eadc33f | 2324 | port->pps_data.supported = false; |
f2a8aa05 | 2325 | port->usb_type = POWER_SUPPLY_USB_TYPE_PD; |
2eadc33f | 2326 | |
f0690a25 | 2327 | /* |
53fe0de9 LJ |
2328 | * Select the source PDO providing the most power which has a |
2329 | * matchig sink cap. | |
f0690a25 GR |
2330 | */ |
2331 | for (i = 0; i < port->nr_source_caps; i++) { | |
2332 | u32 pdo = port->source_caps[i]; | |
2333 | enum pd_pdo_type type = pdo_type(pdo); | |
f0690a25 | 2334 | |
2eadc33f AT |
2335 | switch (type) { |
2336 | case PDO_TYPE_FIXED: | |
53fe0de9 LJ |
2337 | max_src_mv = pdo_fixed_voltage(pdo); |
2338 | min_src_mv = max_src_mv; | |
2eadc33f AT |
2339 | break; |
2340 | case PDO_TYPE_BATT: | |
2341 | case PDO_TYPE_VAR: | |
53fe0de9 LJ |
2342 | max_src_mv = pdo_max_voltage(pdo); |
2343 | min_src_mv = pdo_min_voltage(pdo); | |
2eadc33f AT |
2344 | break; |
2345 | case PDO_TYPE_APDO: | |
f2a8aa05 | 2346 | if (pdo_apdo_type(pdo) == APDO_TYPE_PPS) { |
2eadc33f | 2347 | port->pps_data.supported = true; |
f2a8aa05 AT |
2348 | port->usb_type = |
2349 | POWER_SUPPLY_USB_TYPE_PD_PPS; | |
2350 | } | |
2eadc33f AT |
2351 | continue; |
2352 | default: | |
2353 | tcpm_log(port, "Invalid source PDO type, ignoring"); | |
2354 | continue; | |
53fe0de9 | 2355 | } |
6f566af3 | 2356 | |
2eadc33f AT |
2357 | switch (type) { |
2358 | case PDO_TYPE_FIXED: | |
2359 | case PDO_TYPE_VAR: | |
53fe0de9 LJ |
2360 | src_ma = pdo_max_current(pdo); |
2361 | src_mw = src_ma * min_src_mv / 1000; | |
2eadc33f AT |
2362 | break; |
2363 | case PDO_TYPE_BATT: | |
2364 | src_mw = pdo_max_power(pdo); | |
2365 | break; | |
2366 | case PDO_TYPE_APDO: | |
2367 | continue; | |
2368 | default: | |
2369 | tcpm_log(port, "Invalid source PDO type, ignoring"); | |
2370 | continue; | |
6f566af3 HG |
2371 | } |
2372 | ||
53fe0de9 LJ |
2373 | for (j = 0; j < port->nr_snk_pdo; j++) { |
2374 | pdo = port->snk_pdo[j]; | |
2375 | ||
2eadc33f AT |
2376 | switch (pdo_type(pdo)) { |
2377 | case PDO_TYPE_FIXED: | |
53fe0de9 | 2378 | max_snk_mv = pdo_fixed_voltage(pdo); |
2eadc33f AT |
2379 | min_snk_mv = max_snk_mv; |
2380 | break; | |
2381 | case PDO_TYPE_BATT: | |
2382 | case PDO_TYPE_VAR: | |
53fe0de9 | 2383 | max_snk_mv = pdo_max_voltage(pdo); |
2eadc33f AT |
2384 | min_snk_mv = pdo_min_voltage(pdo); |
2385 | break; | |
2386 | case PDO_TYPE_APDO: | |
2387 | continue; | |
2388 | default: | |
2389 | tcpm_log(port, "Invalid sink PDO type, ignoring"); | |
2390 | continue; | |
53fe0de9 LJ |
2391 | } |
2392 | ||
2393 | if (max_src_mv <= max_snk_mv && | |
2394 | min_src_mv >= min_snk_mv) { | |
2395 | /* Prefer higher voltages if available */ | |
2396 | if ((src_mw == max_mw && min_src_mv > max_mv) || | |
2397 | src_mw > max_mw) { | |
2398 | *src_pdo = i; | |
2399 | *sink_pdo = j; | |
2400 | max_mw = src_mw; | |
2401 | max_mv = min_src_mv; | |
2402 | ret = 0; | |
2403 | } | |
2404 | } | |
f0690a25 GR |
2405 | } |
2406 | } | |
2407 | ||
2408 | return ret; | |
2409 | } | |
2410 | ||
2eadc33f AT |
2411 | #define min_pps_apdo_current(x, y) \ |
2412 | min(pdo_pps_apdo_max_current(x), pdo_pps_apdo_max_current(y)) | |
2413 | ||
2414 | static unsigned int tcpm_pd_select_pps_apdo(struct tcpm_port *port) | |
2415 | { | |
2416 | unsigned int i, j, max_mw = 0, max_mv = 0; | |
2417 | unsigned int min_src_mv, max_src_mv, src_ma, src_mw; | |
8e4657c6 | 2418 | unsigned int min_snk_mv, max_snk_mv; |
c710d0bb KT |
2419 | unsigned int max_op_mv; |
2420 | u32 pdo, src, snk; | |
2eadc33f AT |
2421 | unsigned int src_pdo = 0, snk_pdo = 0; |
2422 | ||
2423 | /* | |
2424 | * Select the source PPS APDO providing the most power while staying | |
2425 | * within the board's limits. We skip the first PDO as this is always | |
2426 | * 5V 3A. | |
2427 | */ | |
2428 | for (i = 1; i < port->nr_source_caps; ++i) { | |
2429 | pdo = port->source_caps[i]; | |
2430 | ||
2431 | switch (pdo_type(pdo)) { | |
2432 | case PDO_TYPE_APDO: | |
2433 | if (pdo_apdo_type(pdo) != APDO_TYPE_PPS) { | |
2434 | tcpm_log(port, "Not PPS APDO (source), ignoring"); | |
2435 | continue; | |
2436 | } | |
2437 | ||
2438 | min_src_mv = pdo_pps_apdo_min_voltage(pdo); | |
2439 | max_src_mv = pdo_pps_apdo_max_voltage(pdo); | |
2440 | src_ma = pdo_pps_apdo_max_current(pdo); | |
2441 | src_mw = (src_ma * max_src_mv) / 1000; | |
2442 | ||
2443 | /* | |
2444 | * Now search through the sink PDOs to find a matching | |
2445 | * PPS APDO. Again skip the first sink PDO as this will | |
2446 | * always be 5V 3A. | |
2447 | */ | |
c413767e | 2448 | for (j = 1; j < port->nr_snk_pdo; j++) { |
2eadc33f AT |
2449 | pdo = port->snk_pdo[j]; |
2450 | ||
2451 | switch (pdo_type(pdo)) { | |
2452 | case PDO_TYPE_APDO: | |
2453 | if (pdo_apdo_type(pdo) != APDO_TYPE_PPS) { | |
2454 | tcpm_log(port, | |
2455 | "Not PPS APDO (sink), ignoring"); | |
2456 | continue; | |
2457 | } | |
2458 | ||
2459 | min_snk_mv = | |
2460 | pdo_pps_apdo_min_voltage(pdo); | |
2461 | max_snk_mv = | |
2462 | pdo_pps_apdo_max_voltage(pdo); | |
2eadc33f AT |
2463 | break; |
2464 | default: | |
2465 | tcpm_log(port, | |
2466 | "Not APDO type (sink), ignoring"); | |
2467 | continue; | |
2468 | } | |
2469 | ||
c710d0bb KT |
2470 | if (min_src_mv <= max_snk_mv && |
2471 | max_src_mv >= min_snk_mv) { | |
2472 | max_op_mv = min(max_src_mv, max_snk_mv); | |
2473 | src_mw = (max_op_mv * src_ma) / 1000; | |
2eadc33f AT |
2474 | /* Prefer higher voltages if available */ |
2475 | if ((src_mw == max_mw && | |
c710d0bb | 2476 | max_op_mv > max_mv) || |
2eadc33f AT |
2477 | src_mw > max_mw) { |
2478 | src_pdo = i; | |
2479 | snk_pdo = j; | |
2480 | max_mw = src_mw; | |
c710d0bb | 2481 | max_mv = max_op_mv; |
2eadc33f AT |
2482 | } |
2483 | } | |
2484 | } | |
2485 | ||
2486 | break; | |
2487 | default: | |
2488 | tcpm_log(port, "Not APDO type (source), ignoring"); | |
2489 | continue; | |
2490 | } | |
2491 | } | |
2492 | ||
2493 | if (src_pdo) { | |
c710d0bb KT |
2494 | src = port->source_caps[src_pdo]; |
2495 | snk = port->snk_pdo[snk_pdo]; | |
2496 | ||
2497 | port->pps_data.min_volt = max(pdo_pps_apdo_min_voltage(src), | |
2498 | pdo_pps_apdo_min_voltage(snk)); | |
2499 | port->pps_data.max_volt = min(pdo_pps_apdo_max_voltage(src), | |
2500 | pdo_pps_apdo_max_voltage(snk)); | |
2501 | port->pps_data.max_curr = min_pps_apdo_current(src, snk); | |
2502 | port->pps_data.out_volt = min(port->pps_data.max_volt, | |
a07ddce4 KT |
2503 | max(port->pps_data.min_volt, |
2504 | port->pps_data.out_volt)); | |
c710d0bb KT |
2505 | port->pps_data.op_curr = min(port->pps_data.max_curr, |
2506 | port->pps_data.op_curr); | |
2eadc33f AT |
2507 | } |
2508 | ||
2509 | return src_pdo; | |
2510 | } | |
2511 | ||
f0690a25 GR |
2512 | static int tcpm_pd_build_request(struct tcpm_port *port, u32 *rdo) |
2513 | { | |
2514 | unsigned int mv, ma, mw, flags; | |
2515 | unsigned int max_ma, max_mw; | |
2516 | enum pd_pdo_type type; | |
53fe0de9 | 2517 | u32 pdo, matching_snk_pdo; |
0c718676 GKH |
2518 | int src_pdo_index = 0; |
2519 | int snk_pdo_index = 0; | |
53fe0de9 | 2520 | int ret; |
f0690a25 | 2521 | |
53fe0de9 LJ |
2522 | ret = tcpm_pd_select_pdo(port, &snk_pdo_index, &src_pdo_index); |
2523 | if (ret < 0) | |
2524 | return ret; | |
2525 | ||
2526 | pdo = port->source_caps[src_pdo_index]; | |
2527 | matching_snk_pdo = port->snk_pdo[snk_pdo_index]; | |
f0690a25 GR |
2528 | type = pdo_type(pdo); |
2529 | ||
2eadc33f AT |
2530 | switch (type) { |
2531 | case PDO_TYPE_FIXED: | |
f0690a25 | 2532 | mv = pdo_fixed_voltage(pdo); |
2eadc33f AT |
2533 | break; |
2534 | case PDO_TYPE_BATT: | |
2535 | case PDO_TYPE_VAR: | |
f0690a25 | 2536 | mv = pdo_min_voltage(pdo); |
2eadc33f AT |
2537 | break; |
2538 | default: | |
2539 | tcpm_log(port, "Invalid PDO selected!"); | |
2540 | return -EINVAL; | |
2541 | } | |
f0690a25 | 2542 | |
53fe0de9 | 2543 | /* Select maximum available current within the sink pdo's limit */ |
f0690a25 | 2544 | if (type == PDO_TYPE_BATT) { |
53fe0de9 LJ |
2545 | mw = min_power(pdo, matching_snk_pdo); |
2546 | ma = 1000 * mw / mv; | |
f0690a25 | 2547 | } else { |
53fe0de9 LJ |
2548 | ma = min_current(pdo, matching_snk_pdo); |
2549 | mw = ma * mv / 1000; | |
f0690a25 | 2550 | } |
f0690a25 | 2551 | |
931693f9 | 2552 | flags = RDO_USB_COMM | RDO_NO_SUSPEND; |
f0690a25 GR |
2553 | |
2554 | /* Set mismatch bit if offered power is less than operating power */ | |
f0690a25 GR |
2555 | max_ma = ma; |
2556 | max_mw = mw; | |
2557 | if (mw < port->operating_snk_mw) { | |
2558 | flags |= RDO_CAP_MISMATCH; | |
53fe0de9 LJ |
2559 | if (type == PDO_TYPE_BATT && |
2560 | (pdo_max_power(matching_snk_pdo) > pdo_max_power(pdo))) | |
2561 | max_mw = pdo_max_power(matching_snk_pdo); | |
2562 | else if (pdo_max_current(matching_snk_pdo) > | |
2563 | pdo_max_current(pdo)) | |
2564 | max_ma = pdo_max_current(matching_snk_pdo); | |
f0690a25 GR |
2565 | } |
2566 | ||
2567 | tcpm_log(port, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d", | |
2568 | port->cc_req, port->cc1, port->cc2, port->vbus_source, | |
2569 | port->vconn_role == TYPEC_SOURCE ? "source" : "sink", | |
2570 | port->polarity); | |
2571 | ||
2572 | if (type == PDO_TYPE_BATT) { | |
53fe0de9 | 2573 | *rdo = RDO_BATT(src_pdo_index + 1, mw, max_mw, flags); |
f0690a25 GR |
2574 | |
2575 | tcpm_log(port, "Requesting PDO %d: %u mV, %u mW%s", | |
53fe0de9 | 2576 | src_pdo_index, mv, mw, |
f0690a25 GR |
2577 | flags & RDO_CAP_MISMATCH ? " [mismatch]" : ""); |
2578 | } else { | |
53fe0de9 | 2579 | *rdo = RDO_FIXED(src_pdo_index + 1, ma, max_ma, flags); |
f0690a25 GR |
2580 | |
2581 | tcpm_log(port, "Requesting PDO %d: %u mV, %u mA%s", | |
53fe0de9 | 2582 | src_pdo_index, mv, ma, |
f0690a25 GR |
2583 | flags & RDO_CAP_MISMATCH ? " [mismatch]" : ""); |
2584 | } | |
2585 | ||
2586 | port->current_limit = ma; | |
2587 | port->supply_voltage = mv; | |
2588 | ||
2589 | return 0; | |
2590 | } | |
2591 | ||
2592 | static int tcpm_pd_send_request(struct tcpm_port *port) | |
2593 | { | |
2594 | struct pd_message msg; | |
2595 | int ret; | |
2596 | u32 rdo; | |
2597 | ||
2598 | ret = tcpm_pd_build_request(port, &rdo); | |
2599 | if (ret < 0) | |
2600 | return ret; | |
2601 | ||
2602 | memset(&msg, 0, sizeof(msg)); | |
2603 | msg.header = PD_HEADER_LE(PD_DATA_REQUEST, | |
2604 | port->pwr_role, | |
2605 | port->data_role, | |
2eadc33f AT |
2606 | port->negotiated_rev, |
2607 | port->message_id, 1); | |
2608 | msg.payload[0] = cpu_to_le32(rdo); | |
2609 | ||
2610 | return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg); | |
2611 | } | |
2612 | ||
2613 | static int tcpm_pd_build_pps_request(struct tcpm_port *port, u32 *rdo) | |
2614 | { | |
8e4657c6 | 2615 | unsigned int out_mv, op_ma, op_mw, max_mv, max_ma, flags; |
2eadc33f AT |
2616 | enum pd_pdo_type type; |
2617 | unsigned int src_pdo_index; | |
2618 | u32 pdo; | |
2619 | ||
2620 | src_pdo_index = tcpm_pd_select_pps_apdo(port); | |
2621 | if (!src_pdo_index) | |
2622 | return -EOPNOTSUPP; | |
2623 | ||
2624 | pdo = port->source_caps[src_pdo_index]; | |
2625 | type = pdo_type(pdo); | |
2626 | ||
2627 | switch (type) { | |
2628 | case PDO_TYPE_APDO: | |
2629 | if (pdo_apdo_type(pdo) != APDO_TYPE_PPS) { | |
2630 | tcpm_log(port, "Invalid APDO selected!"); | |
2631 | return -EINVAL; | |
2632 | } | |
2eadc33f AT |
2633 | max_mv = port->pps_data.max_volt; |
2634 | max_ma = port->pps_data.max_curr; | |
2635 | out_mv = port->pps_data.out_volt; | |
2636 | op_ma = port->pps_data.op_curr; | |
2637 | break; | |
2638 | default: | |
2639 | tcpm_log(port, "Invalid PDO selected!"); | |
2640 | return -EINVAL; | |
2641 | } | |
2642 | ||
2643 | flags = RDO_USB_COMM | RDO_NO_SUSPEND; | |
2644 | ||
2645 | op_mw = (op_ma * out_mv) / 1000; | |
2646 | if (op_mw < port->operating_snk_mw) { | |
2647 | /* | |
2648 | * Try raising current to meet power needs. If that's not enough | |
2649 | * then try upping the voltage. If that's still not enough | |
2650 | * then we've obviously chosen a PPS APDO which really isn't | |
2651 | * suitable so abandon ship. | |
2652 | */ | |
2653 | op_ma = (port->operating_snk_mw * 1000) / out_mv; | |
2654 | if ((port->operating_snk_mw * 1000) % out_mv) | |
2655 | ++op_ma; | |
2656 | op_ma += RDO_PROG_CURR_MA_STEP - (op_ma % RDO_PROG_CURR_MA_STEP); | |
2657 | ||
2658 | if (op_ma > max_ma) { | |
2659 | op_ma = max_ma; | |
2660 | out_mv = (port->operating_snk_mw * 1000) / op_ma; | |
2661 | if ((port->operating_snk_mw * 1000) % op_ma) | |
2662 | ++out_mv; | |
2663 | out_mv += RDO_PROG_VOLT_MV_STEP - | |
2664 | (out_mv % RDO_PROG_VOLT_MV_STEP); | |
2665 | ||
2666 | if (out_mv > max_mv) { | |
2667 | tcpm_log(port, "Invalid PPS APDO selected!"); | |
2668 | return -EINVAL; | |
2669 | } | |
2670 | } | |
2671 | } | |
2672 | ||
2673 | tcpm_log(port, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d", | |
2674 | port->cc_req, port->cc1, port->cc2, port->vbus_source, | |
2675 | port->vconn_role == TYPEC_SOURCE ? "source" : "sink", | |
2676 | port->polarity); | |
2677 | ||
2678 | *rdo = RDO_PROG(src_pdo_index + 1, out_mv, op_ma, flags); | |
2679 | ||
2680 | tcpm_log(port, "Requesting APDO %d: %u mV, %u mA", | |
2681 | src_pdo_index, out_mv, op_ma); | |
2682 | ||
2683 | port->pps_data.op_curr = op_ma; | |
2684 | port->pps_data.out_volt = out_mv; | |
2685 | ||
2686 | return 0; | |
2687 | } | |
2688 | ||
2689 | static int tcpm_pd_send_pps_request(struct tcpm_port *port) | |
2690 | { | |
2691 | struct pd_message msg; | |
2692 | int ret; | |
2693 | u32 rdo; | |
2694 | ||
2695 | ret = tcpm_pd_build_pps_request(port, &rdo); | |
2696 | if (ret < 0) | |
2697 | return ret; | |
2698 | ||
2699 | memset(&msg, 0, sizeof(msg)); | |
2700 | msg.header = PD_HEADER_LE(PD_DATA_REQUEST, | |
2701 | port->pwr_role, | |
2702 | port->data_role, | |
2703 | port->negotiated_rev, | |
f0690a25 GR |
2704 | port->message_id, 1); |
2705 | msg.payload[0] = cpu_to_le32(rdo); | |
2706 | ||
2707 | return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg); | |
2708 | } | |
2709 | ||
2710 | static int tcpm_set_vbus(struct tcpm_port *port, bool enable) | |
2711 | { | |
2712 | int ret; | |
2713 | ||
2714 | if (enable && port->vbus_charge) | |
2715 | return -EINVAL; | |
2716 | ||
2717 | tcpm_log(port, "vbus:=%d charge=%d", enable, port->vbus_charge); | |
2718 | ||
2719 | ret = port->tcpc->set_vbus(port->tcpc, enable, port->vbus_charge); | |
2720 | if (ret < 0) | |
2721 | return ret; | |
2722 | ||
2723 | port->vbus_source = enable; | |
2724 | return 0; | |
2725 | } | |
2726 | ||
2727 | static int tcpm_set_charge(struct tcpm_port *port, bool charge) | |
2728 | { | |
2729 | int ret; | |
2730 | ||
2731 | if (charge && port->vbus_source) | |
2732 | return -EINVAL; | |
2733 | ||
2734 | if (charge != port->vbus_charge) { | |
2735 | tcpm_log(port, "vbus=%d charge:=%d", port->vbus_source, charge); | |
2736 | ret = port->tcpc->set_vbus(port->tcpc, port->vbus_source, | |
2737 | charge); | |
2738 | if (ret < 0) | |
2739 | return ret; | |
2740 | } | |
2741 | port->vbus_charge = charge; | |
2742 | return 0; | |
2743 | } | |
2744 | ||
7893f9e1 | 2745 | static bool tcpm_start_toggling(struct tcpm_port *port, enum typec_cc_status cc) |
f0690a25 GR |
2746 | { |
2747 | int ret; | |
2748 | ||
7893f9e1 HG |
2749 | if (!port->tcpc->start_toggling) |
2750 | return false; | |
f0690a25 | 2751 | |
7893f9e1 HG |
2752 | tcpm_log_force(port, "Start toggling"); |
2753 | ret = port->tcpc->start_toggling(port->tcpc, port->port_type, cc); | |
2754 | return ret == 0; | |
f0690a25 GR |
2755 | } |
2756 | ||
2757 | static void tcpm_set_cc(struct tcpm_port *port, enum typec_cc_status cc) | |
2758 | { | |
2759 | tcpm_log(port, "cc:=%d", cc); | |
2760 | port->cc_req = cc; | |
2761 | port->tcpc->set_cc(port->tcpc, cc); | |
2762 | } | |
2763 | ||
2764 | static int tcpm_init_vbus(struct tcpm_port *port) | |
2765 | { | |
2766 | int ret; | |
2767 | ||
2768 | ret = port->tcpc->set_vbus(port->tcpc, false, false); | |
2769 | port->vbus_source = false; | |
2770 | port->vbus_charge = false; | |
2771 | return ret; | |
2772 | } | |
2773 | ||
2774 | static int tcpm_init_vconn(struct tcpm_port *port) | |
2775 | { | |
2776 | int ret; | |
2777 | ||
2778 | ret = port->tcpc->set_vconn(port->tcpc, false); | |
2779 | port->vconn_role = TYPEC_SINK; | |
2780 | return ret; | |
2781 | } | |
2782 | ||
2783 | static void tcpm_typec_connect(struct tcpm_port *port) | |
2784 | { | |
2785 | if (!port->connected) { | |
2786 | /* Make sure we don't report stale identity information */ | |
2787 | memset(&port->partner_ident, 0, sizeof(port->partner_ident)); | |
2788 | port->partner_desc.usb_pd = port->pd_capable; | |
2789 | if (tcpm_port_is_debug(port)) | |
2790 | port->partner_desc.accessory = TYPEC_ACCESSORY_DEBUG; | |
2791 | else if (tcpm_port_is_audio(port)) | |
2792 | port->partner_desc.accessory = TYPEC_ACCESSORY_AUDIO; | |
2793 | else | |
2794 | port->partner_desc.accessory = TYPEC_ACCESSORY_NONE; | |
2795 | port->partner = typec_register_partner(port->typec_port, | |
2796 | &port->partner_desc); | |
2797 | port->connected = true; | |
2798 | } | |
2799 | } | |
2800 | ||
2801 | static int tcpm_src_attach(struct tcpm_port *port) | |
2802 | { | |
2803 | enum typec_cc_polarity polarity = | |
2804 | port->cc2 == TYPEC_CC_RD ? TYPEC_POLARITY_CC2 | |
2805 | : TYPEC_POLARITY_CC1; | |
2806 | int ret; | |
2807 | ||
2808 | if (port->attached) | |
2809 | return 0; | |
2810 | ||
2811 | ret = tcpm_set_polarity(port, polarity); | |
2812 | if (ret < 0) | |
2813 | return ret; | |
2814 | ||
f321a02c BJS |
2815 | if (port->tcpc->enable_auto_vbus_discharge) { |
2816 | ret = port->tcpc->enable_auto_vbus_discharge(port->tcpc, true); | |
2817 | tcpm_log_force(port, "enable vbus discharge ret:%d", ret); | |
2818 | } | |
2819 | ||
2820 | ret = tcpm_set_roles(port, true, TYPEC_SOURCE, tcpm_data_role_for_source(port)); | |
f0690a25 GR |
2821 | if (ret < 0) |
2822 | return ret; | |
2823 | ||
2824 | ret = port->tcpc->set_pd_rx(port->tcpc, true); | |
2825 | if (ret < 0) | |
2826 | goto out_disable_mux; | |
2827 | ||
2828 | /* | |
2829 | * USB Type-C specification, version 1.2, | |
2830 | * chapter 4.5.2.2.8.1 (Attached.SRC Requirements) | |
2831 | * Enable VCONN only if the non-RD port is set to RA. | |
2832 | */ | |
2833 | if ((polarity == TYPEC_POLARITY_CC1 && port->cc2 == TYPEC_CC_RA) || | |
2834 | (polarity == TYPEC_POLARITY_CC2 && port->cc1 == TYPEC_CC_RA)) { | |
2835 | ret = tcpm_set_vconn(port, true); | |
2836 | if (ret < 0) | |
2837 | goto out_disable_pd; | |
2838 | } | |
2839 | ||
2840 | ret = tcpm_set_vbus(port, true); | |
2841 | if (ret < 0) | |
2842 | goto out_disable_vconn; | |
2843 | ||
2844 | port->pd_capable = false; | |
2845 | ||
2846 | port->partner = NULL; | |
2847 | ||
2848 | port->attached = true; | |
2849 | port->send_discover = true; | |
2850 | ||
2851 | return 0; | |
2852 | ||
2853 | out_disable_vconn: | |
2854 | tcpm_set_vconn(port, false); | |
2855 | out_disable_pd: | |
2856 | port->tcpc->set_pd_rx(port->tcpc, false); | |
2857 | out_disable_mux: | |
e9576fe8 | 2858 | tcpm_mux_set(port, TYPEC_STATE_SAFE, USB_ROLE_NONE, |
2000016c | 2859 | TYPEC_ORIENTATION_NONE); |
f0690a25 GR |
2860 | return ret; |
2861 | } | |
2862 | ||
2863 | static void tcpm_typec_disconnect(struct tcpm_port *port) | |
2864 | { | |
2865 | if (port->connected) { | |
2866 | typec_unregister_partner(port->partner); | |
2867 | port->partner = NULL; | |
2868 | port->connected = false; | |
2869 | } | |
2870 | } | |
2871 | ||
2872 | static void tcpm_unregister_altmodes(struct tcpm_port *port) | |
2873 | { | |
2874 | struct pd_mode_data *modep = &port->mode_data; | |
2875 | int i; | |
2876 | ||
2877 | for (i = 0; i < modep->altmodes; i++) { | |
2878 | typec_unregister_altmode(port->partner_altmode[i]); | |
2879 | port->partner_altmode[i] = NULL; | |
2880 | } | |
2881 | ||
2882 | memset(modep, 0, sizeof(*modep)); | |
2883 | } | |
2884 | ||
2885 | static void tcpm_reset_port(struct tcpm_port *port) | |
2886 | { | |
f321a02c BJS |
2887 | int ret; |
2888 | ||
2889 | if (port->tcpc->enable_auto_vbus_discharge) { | |
2890 | ret = port->tcpc->enable_auto_vbus_discharge(port->tcpc, false); | |
2891 | tcpm_log_force(port, "Disable vbus discharge ret:%d", ret); | |
2892 | } | |
f0690a25 GR |
2893 | tcpm_unregister_altmodes(port); |
2894 | tcpm_typec_disconnect(port); | |
2895 | port->attached = false; | |
2896 | port->pd_capable = false; | |
2eadc33f | 2897 | port->pps_data.supported = false; |
f0690a25 | 2898 | |
5fec4b54 GR |
2899 | /* |
2900 | * First Rx ID should be 0; set this to a sentinel of -1 so that | |
2901 | * we can check tcpm_pd_rx_handler() if we had seen it before. | |
2902 | */ | |
2903 | port->rx_msgid = -1; | |
2904 | ||
f0690a25 GR |
2905 | port->tcpc->set_pd_rx(port->tcpc, false); |
2906 | tcpm_init_vbus(port); /* also disables charging */ | |
2907 | tcpm_init_vconn(port); | |
2908 | tcpm_set_current_limit(port, 0, 0); | |
2909 | tcpm_set_polarity(port, TYPEC_POLARITY_CC1); | |
e9576fe8 | 2910 | tcpm_mux_set(port, TYPEC_STATE_SAFE, USB_ROLE_NONE, |
2000016c | 2911 | TYPEC_ORIENTATION_NONE); |
f0690a25 GR |
2912 | tcpm_set_attached_state(port, false); |
2913 | port->try_src_count = 0; | |
2914 | port->try_snk_count = 0; | |
f2a8aa05 | 2915 | port->usb_type = POWER_SUPPLY_USB_TYPE_C; |
8dc4bd07 BJS |
2916 | port->nr_sink_caps = 0; |
2917 | port->sink_cap_done = false; | |
2918 | if (port->tcpc->enable_frs) | |
2919 | port->tcpc->enable_frs(port->tcpc, false); | |
f2a8aa05 AT |
2920 | |
2921 | power_supply_changed(port->psy); | |
f0690a25 GR |
2922 | } |
2923 | ||
2924 | static void tcpm_detach(struct tcpm_port *port) | |
2925 | { | |
2d9c6442 LJ |
2926 | if (tcpm_port_is_disconnected(port)) |
2927 | port->hard_reset_count = 0; | |
2928 | ||
f0690a25 GR |
2929 | if (!port->attached) |
2930 | return; | |
2931 | ||
b2dcfefc BJS |
2932 | if (port->tcpc->set_bist_data) { |
2933 | tcpm_log(port, "disable BIST MODE TESTDATA"); | |
2934 | port->tcpc->set_bist_data(port->tcpc, false); | |
2935 | } | |
2936 | ||
f0690a25 GR |
2937 | tcpm_reset_port(port); |
2938 | } | |
2939 | ||
2940 | static void tcpm_src_detach(struct tcpm_port *port) | |
2941 | { | |
2942 | tcpm_detach(port); | |
2943 | } | |
2944 | ||
2945 | static int tcpm_snk_attach(struct tcpm_port *port) | |
2946 | { | |
2947 | int ret; | |
2948 | ||
2949 | if (port->attached) | |
2950 | return 0; | |
2951 | ||
2952 | ret = tcpm_set_polarity(port, port->cc2 != TYPEC_CC_OPEN ? | |
2953 | TYPEC_POLARITY_CC2 : TYPEC_POLARITY_CC1); | |
2954 | if (ret < 0) | |
2955 | return ret; | |
2956 | ||
f321a02c BJS |
2957 | if (port->tcpc->enable_auto_vbus_discharge) { |
2958 | tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, VSAFE5V); | |
2959 | ret = port->tcpc->enable_auto_vbus_discharge(port->tcpc, true); | |
2960 | tcpm_log_force(port, "enable vbus discharge ret:%d", ret); | |
2961 | } | |
2962 | ||
2963 | ret = tcpm_set_roles(port, true, TYPEC_SINK, tcpm_data_role_for_sink(port)); | |
f0690a25 GR |
2964 | if (ret < 0) |
2965 | return ret; | |
2966 | ||
2967 | port->pd_capable = false; | |
2968 | ||
2969 | port->partner = NULL; | |
2970 | ||
2971 | port->attached = true; | |
2972 | port->send_discover = true; | |
2973 | ||
2974 | return 0; | |
2975 | } | |
2976 | ||
2977 | static void tcpm_snk_detach(struct tcpm_port *port) | |
2978 | { | |
2979 | tcpm_detach(port); | |
f0690a25 GR |
2980 | } |
2981 | ||
2982 | static int tcpm_acc_attach(struct tcpm_port *port) | |
2983 | { | |
2984 | int ret; | |
2985 | ||
2986 | if (port->attached) | |
2987 | return 0; | |
2988 | ||
6ecc632d LJ |
2989 | ret = tcpm_set_roles(port, true, TYPEC_SOURCE, |
2990 | tcpm_data_role_for_source(port)); | |
f0690a25 GR |
2991 | if (ret < 0) |
2992 | return ret; | |
2993 | ||
2994 | port->partner = NULL; | |
2995 | ||
2996 | tcpm_typec_connect(port); | |
2997 | ||
2998 | port->attached = true; | |
2999 | ||
3000 | return 0; | |
3001 | } | |
3002 | ||
3003 | static void tcpm_acc_detach(struct tcpm_port *port) | |
3004 | { | |
3005 | tcpm_detach(port); | |
3006 | } | |
3007 | ||
3008 | static inline enum tcpm_state hard_reset_state(struct tcpm_port *port) | |
3009 | { | |
3010 | if (port->hard_reset_count < PD_N_HARD_RESET_COUNT) | |
3011 | return HARD_RESET_SEND; | |
3012 | if (port->pd_capable) | |
3013 | return ERROR_RECOVERY; | |
3014 | if (port->pwr_role == TYPEC_SOURCE) | |
3015 | return SRC_UNATTACHED; | |
3016 | if (port->state == SNK_WAIT_CAPABILITIES) | |
3017 | return SNK_READY; | |
3018 | return SNK_UNATTACHED; | |
3019 | } | |
3020 | ||
f0690a25 GR |
3021 | static inline enum tcpm_state unattached_state(struct tcpm_port *port) |
3022 | { | |
13cb492c BJS |
3023 | if (port->port_type == TYPEC_PORT_DRP) { |
3024 | if (port->pwr_role == TYPEC_SOURCE) | |
3025 | return SRC_UNATTACHED; | |
3026 | else | |
3027 | return SNK_UNATTACHED; | |
ceeb1625 | 3028 | } else if (port->port_type == TYPEC_PORT_SRC) { |
f0690a25 | 3029 | return SRC_UNATTACHED; |
13cb492c BJS |
3030 | } |
3031 | ||
3032 | return SNK_UNATTACHED; | |
f0690a25 GR |
3033 | } |
3034 | ||
3035 | static void tcpm_check_send_discover(struct tcpm_port *port) | |
3036 | { | |
3037 | if (port->data_role == TYPEC_HOST && port->send_discover && | |
3038 | port->pd_capable) { | |
3039 | tcpm_send_vdm(port, USB_SID_PD, CMD_DISCOVER_IDENT, NULL, 0); | |
3040 | port->send_discover = false; | |
3041 | } | |
3042 | } | |
3043 | ||
3044 | static void tcpm_swap_complete(struct tcpm_port *port, int result) | |
3045 | { | |
3046 | if (port->swap_pending) { | |
3047 | port->swap_status = result; | |
3048 | port->swap_pending = false; | |
b17dd571 | 3049 | port->non_pd_role_swap = false; |
f0690a25 GR |
3050 | complete(&port->swap_complete); |
3051 | } | |
3052 | } | |
3053 | ||
53b70e5c | 3054 | static enum typec_pwr_opmode tcpm_get_pwr_opmode(enum typec_cc_status cc) |
fce042f0 BJS |
3055 | { |
3056 | switch (cc) { | |
3057 | case TYPEC_CC_RP_1_5: | |
3058 | return TYPEC_PWR_MODE_1_5A; | |
3059 | case TYPEC_CC_RP_3_0: | |
3060 | return TYPEC_PWR_MODE_3_0A; | |
3061 | case TYPEC_CC_RP_DEF: | |
3062 | default: | |
3063 | return TYPEC_PWR_MODE_USB; | |
3064 | } | |
3065 | } | |
3066 | ||
f0690a25 GR |
3067 | static void run_state_machine(struct tcpm_port *port) |
3068 | { | |
3069 | int ret; | |
fce042f0 | 3070 | enum typec_pwr_opmode opmode; |
131c7d12 | 3071 | unsigned int msecs; |
f0690a25 GR |
3072 | |
3073 | port->enter_state = port->state; | |
3074 | switch (port->state) { | |
7893f9e1 | 3075 | case TOGGLING: |
f0690a25 GR |
3076 | break; |
3077 | /* SRC states */ | |
3078 | case SRC_UNATTACHED: | |
b17dd571 GR |
3079 | if (!port->non_pd_role_swap) |
3080 | tcpm_swap_complete(port, -ENOTCONN); | |
f0690a25 | 3081 | tcpm_src_detach(port); |
7893f9e1 HG |
3082 | if (tcpm_start_toggling(port, tcpm_rp_cc(port))) { |
3083 | tcpm_set_state(port, TOGGLING, 0); | |
f0690a25 GR |
3084 | break; |
3085 | } | |
3086 | tcpm_set_cc(port, tcpm_rp_cc(port)); | |
9b0ae699 | 3087 | if (port->port_type == TYPEC_PORT_DRP) |
f0690a25 GR |
3088 | tcpm_set_state(port, SNK_UNATTACHED, PD_T_DRP_SNK); |
3089 | break; | |
3090 | case SRC_ATTACH_WAIT: | |
3091 | if (tcpm_port_is_debug(port)) | |
3092 | tcpm_set_state(port, DEBUG_ACC_ATTACHED, | |
3093 | PD_T_CC_DEBOUNCE); | |
3094 | else if (tcpm_port_is_audio(port)) | |
3095 | tcpm_set_state(port, AUDIO_ACC_ATTACHED, | |
3096 | PD_T_CC_DEBOUNCE); | |
3097 | else if (tcpm_port_is_source(port)) | |
3098 | tcpm_set_state(port, | |
3099 | tcpm_try_snk(port) ? SNK_TRY | |
3100 | : SRC_ATTACHED, | |
3101 | PD_T_CC_DEBOUNCE); | |
3102 | break; | |
3103 | ||
3104 | case SNK_TRY: | |
3105 | port->try_snk_count++; | |
3106 | /* | |
3107 | * Requirements: | |
3108 | * - Do not drive vconn or vbus | |
3109 | * - Terminate CC pins (both) to Rd | |
3110 | * Action: | |
3111 | * - Wait for tDRPTry (PD_T_DRP_TRY). | |
3112 | * Until then, ignore any state changes. | |
3113 | */ | |
3114 | tcpm_set_cc(port, TYPEC_CC_RD); | |
3115 | tcpm_set_state(port, SNK_TRY_WAIT, PD_T_DRP_TRY); | |
3116 | break; | |
3117 | case SNK_TRY_WAIT: | |
a0a3e04e BJS |
3118 | if (tcpm_port_is_sink(port)) { |
3119 | tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE, 0); | |
3120 | } else { | |
3121 | tcpm_set_state(port, SRC_TRYWAIT, 0); | |
3122 | port->max_wait = 0; | |
3123 | } | |
3124 | break; | |
3125 | case SNK_TRY_WAIT_DEBOUNCE: | |
3126 | tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS, | |
11e5e568 | 3127 | PD_T_TRY_CC_DEBOUNCE); |
a0a3e04e BJS |
3128 | break; |
3129 | case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS: | |
11e5e568 | 3130 | if (port->vbus_present && tcpm_port_is_sink(port)) |
f0690a25 | 3131 | tcpm_set_state(port, SNK_ATTACHED, 0); |
11e5e568 | 3132 | else |
02d5be46 | 3133 | port->max_wait = 0; |
f0690a25 GR |
3134 | break; |
3135 | case SRC_TRYWAIT: | |
3136 | tcpm_set_cc(port, tcpm_rp_cc(port)); | |
02d5be46 BJS |
3137 | if (port->max_wait == 0) { |
3138 | port->max_wait = jiffies + | |
3139 | msecs_to_jiffies(PD_T_DRP_TRY); | |
f0690a25 GR |
3140 | tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED, |
3141 | PD_T_DRP_TRY); | |
02d5be46 BJS |
3142 | } else { |
3143 | if (time_is_after_jiffies(port->max_wait)) | |
3144 | tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED, | |
3145 | jiffies_to_msecs(port->max_wait - | |
3146 | jiffies)); | |
3147 | else | |
3148 | tcpm_set_state(port, SNK_UNATTACHED, 0); | |
3149 | } | |
3150 | break; | |
3151 | case SRC_TRYWAIT_DEBOUNCE: | |
3152 | tcpm_set_state(port, SRC_ATTACHED, PD_T_CC_DEBOUNCE); | |
f0690a25 GR |
3153 | break; |
3154 | case SRC_TRYWAIT_UNATTACHED: | |
3155 | tcpm_set_state(port, SNK_UNATTACHED, 0); | |
3156 | break; | |
3157 | ||
3158 | case SRC_ATTACHED: | |
3159 | ret = tcpm_src_attach(port); | |
3160 | tcpm_set_state(port, SRC_UNATTACHED, | |
3161 | ret < 0 ? 0 : PD_T_PS_SOURCE_ON); | |
3162 | break; | |
3163 | case SRC_STARTUP: | |
fce042f0 BJS |
3164 | opmode = tcpm_get_pwr_opmode(tcpm_rp_cc(port)); |
3165 | typec_set_pwr_opmode(port->typec_port, opmode); | |
f0690a25 GR |
3166 | port->pwr_opmode = TYPEC_PWR_MODE_USB; |
3167 | port->caps_count = 0; | |
2eadc33f | 3168 | port->negotiated_rev = PD_MAX_REV; |
f0690a25 | 3169 | port->message_id = 0; |
5fec4b54 | 3170 | port->rx_msgid = -1; |
f0690a25 GR |
3171 | port->explicit_contract = false; |
3172 | tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0); | |
3173 | break; | |
3174 | case SRC_SEND_CAPABILITIES: | |
3175 | port->caps_count++; | |
3176 | if (port->caps_count > PD_N_CAPS_COUNT) { | |
3177 | tcpm_set_state(port, SRC_READY, 0); | |
3178 | break; | |
3179 | } | |
3180 | ret = tcpm_pd_send_source_caps(port); | |
3181 | if (ret < 0) { | |
3182 | tcpm_set_state(port, SRC_SEND_CAPABILITIES, | |
3183 | PD_T_SEND_SOURCE_CAP); | |
3184 | } else { | |
3185 | /* | |
3186 | * Per standard, we should clear the reset counter here. | |
3187 | * However, that can result in state machine hang-ups. | |
3188 | * Reset it only in READY state to improve stability. | |
3189 | */ | |
3190 | /* port->hard_reset_count = 0; */ | |
3191 | port->caps_count = 0; | |
3192 | port->pd_capable = true; | |
976daf9d | 3193 | tcpm_set_state_cond(port, SRC_SEND_CAPABILITIES_TIMEOUT, |
f0690a25 GR |
3194 | PD_T_SEND_SOURCE_CAP); |
3195 | } | |
3196 | break; | |
976daf9d HG |
3197 | case SRC_SEND_CAPABILITIES_TIMEOUT: |
3198 | /* | |
3199 | * Error recovery for a PD_DATA_SOURCE_CAP reply timeout. | |
3200 | * | |
3201 | * PD 2.0 sinks are supposed to accept src-capabilities with a | |
3202 | * 3.0 header and simply ignore any src PDOs which the sink does | |
3203 | * not understand such as PPS but some 2.0 sinks instead ignore | |
3204 | * the entire PD_DATA_SOURCE_CAP message, causing contract | |
3205 | * negotiation to fail. | |
3206 | * | |
3207 | * After PD_N_HARD_RESET_COUNT hard-reset attempts, we try | |
3208 | * sending src-capabilities with a lower PD revision to | |
3209 | * make these broken sinks work. | |
3210 | */ | |
3211 | if (port->hard_reset_count < PD_N_HARD_RESET_COUNT) { | |
3212 | tcpm_set_state(port, HARD_RESET_SEND, 0); | |
3213 | } else if (port->negotiated_rev > PD_REV20) { | |
3214 | port->negotiated_rev--; | |
3215 | port->hard_reset_count = 0; | |
3216 | tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0); | |
3217 | } else { | |
3218 | tcpm_set_state(port, hard_reset_state(port), 0); | |
3219 | } | |
3220 | break; | |
f0690a25 GR |
3221 | case SRC_NEGOTIATE_CAPABILITIES: |
3222 | ret = tcpm_pd_check_request(port); | |
3223 | if (ret < 0) { | |
3224 | tcpm_pd_send_control(port, PD_CTRL_REJECT); | |
3225 | if (!port->explicit_contract) { | |
3226 | tcpm_set_state(port, | |
3227 | SRC_WAIT_NEW_CAPABILITIES, 0); | |
3228 | } else { | |
3229 | tcpm_set_state(port, SRC_READY, 0); | |
3230 | } | |
3231 | } else { | |
3232 | tcpm_pd_send_control(port, PD_CTRL_ACCEPT); | |
3233 | tcpm_set_state(port, SRC_TRANSITION_SUPPLY, | |
3234 | PD_T_SRC_TRANSITION); | |
3235 | } | |
3236 | break; | |
3237 | case SRC_TRANSITION_SUPPLY: | |
3238 | /* XXX: regulator_set_voltage(vbus, ...) */ | |
3239 | tcpm_pd_send_control(port, PD_CTRL_PS_RDY); | |
3240 | port->explicit_contract = true; | |
3241 | typec_set_pwr_opmode(port->typec_port, TYPEC_PWR_MODE_PD); | |
3242 | port->pwr_opmode = TYPEC_PWR_MODE_PD; | |
3243 | tcpm_set_state_cond(port, SRC_READY, 0); | |
3244 | break; | |
3245 | case SRC_READY: | |
3246 | #if 1 | |
3247 | port->hard_reset_count = 0; | |
3248 | #endif | |
3249 | port->try_src_count = 0; | |
3250 | ||
3113bf1a | 3251 | tcpm_swap_complete(port, 0); |
f0690a25 | 3252 | tcpm_typec_connect(port); |
2eadc33f | 3253 | |
f0690a25 GR |
3254 | tcpm_check_send_discover(port); |
3255 | /* | |
3256 | * 6.3.5 | |
3257 | * Sending ping messages is not necessary if | |
3258 | * - the source operates at vSafe5V | |
3259 | * or | |
3260 | * - The system is not operating in PD mode | |
3261 | * or | |
3262 | * - Both partners are connected using a Type-C connector | |
f451ac9e BJS |
3263 | * |
3264 | * There is no actual need to send PD messages since the local | |
3265 | * port type-c and the spec does not clearly say whether PD is | |
3266 | * possible when type-c is connected to Type-A/B | |
f0690a25 | 3267 | */ |
f0690a25 GR |
3268 | break; |
3269 | case SRC_WAIT_NEW_CAPABILITIES: | |
3270 | /* Nothing to do... */ | |
3271 | break; | |
3272 | ||
3273 | /* SNK states */ | |
3274 | case SNK_UNATTACHED: | |
b17dd571 GR |
3275 | if (!port->non_pd_role_swap) |
3276 | tcpm_swap_complete(port, -ENOTCONN); | |
2eadc33f | 3277 | tcpm_pps_complete(port, -ENOTCONN); |
f0690a25 | 3278 | tcpm_snk_detach(port); |
7893f9e1 HG |
3279 | if (tcpm_start_toggling(port, TYPEC_CC_RD)) { |
3280 | tcpm_set_state(port, TOGGLING, 0); | |
f0690a25 GR |
3281 | break; |
3282 | } | |
3283 | tcpm_set_cc(port, TYPEC_CC_RD); | |
9b0ae699 | 3284 | if (port->port_type == TYPEC_PORT_DRP) |
f0690a25 GR |
3285 | tcpm_set_state(port, SRC_UNATTACHED, PD_T_DRP_SRC); |
3286 | break; | |
3287 | case SNK_ATTACH_WAIT: | |
3288 | if ((port->cc1 == TYPEC_CC_OPEN && | |
3289 | port->cc2 != TYPEC_CC_OPEN) || | |
3290 | (port->cc1 != TYPEC_CC_OPEN && | |
3291 | port->cc2 == TYPEC_CC_OPEN)) | |
3292 | tcpm_set_state(port, SNK_DEBOUNCED, | |
3293 | PD_T_CC_DEBOUNCE); | |
3294 | else if (tcpm_port_is_disconnected(port)) | |
3295 | tcpm_set_state(port, SNK_UNATTACHED, | |
3296 | PD_T_PD_DEBOUNCE); | |
3297 | break; | |
3298 | case SNK_DEBOUNCED: | |
3299 | if (tcpm_port_is_disconnected(port)) | |
3300 | tcpm_set_state(port, SNK_UNATTACHED, | |
3301 | PD_T_PD_DEBOUNCE); | |
3302 | else if (port->vbus_present) | |
3303 | tcpm_set_state(port, | |
3304 | tcpm_try_src(port) ? SRC_TRY | |
3305 | : SNK_ATTACHED, | |
3306 | 0); | |
3307 | else | |
3308 | /* Wait for VBUS, but not forever */ | |
56277035 | 3309 | tcpm_set_state(port, PORT_RESET, PD_T_PS_SOURCE_ON); |
f0690a25 GR |
3310 | break; |
3311 | ||
3312 | case SRC_TRY: | |
3313 | port->try_src_count++; | |
3314 | tcpm_set_cc(port, tcpm_rp_cc(port)); | |
131c7d12 BJS |
3315 | port->max_wait = 0; |
3316 | tcpm_set_state(port, SRC_TRY_WAIT, 0); | |
3317 | break; | |
3318 | case SRC_TRY_WAIT: | |
3319 | if (port->max_wait == 0) { | |
3320 | port->max_wait = jiffies + | |
3321 | msecs_to_jiffies(PD_T_DRP_TRY); | |
3322 | msecs = PD_T_DRP_TRY; | |
3323 | } else { | |
3324 | if (time_is_after_jiffies(port->max_wait)) | |
3325 | msecs = jiffies_to_msecs(port->max_wait - | |
3326 | jiffies); | |
3327 | else | |
3328 | msecs = 0; | |
3329 | } | |
3330 | tcpm_set_state(port, SNK_TRYWAIT, msecs); | |
f0690a25 GR |
3331 | break; |
3332 | case SRC_TRY_DEBOUNCE: | |
3333 | tcpm_set_state(port, SRC_ATTACHED, PD_T_PD_DEBOUNCE); | |
3334 | break; | |
3335 | case SNK_TRYWAIT: | |
3336 | tcpm_set_cc(port, TYPEC_CC_RD); | |
af450ebb | 3337 | tcpm_set_state(port, SNK_TRYWAIT_VBUS, PD_T_CC_DEBOUNCE); |
f0690a25 | 3338 | break; |
af450ebb BJS |
3339 | case SNK_TRYWAIT_VBUS: |
3340 | /* | |
3341 | * TCPM stays in this state indefinitely until VBUS | |
3342 | * is detected as long as Rp is not detected for | |
3343 | * more than a time period of tPDDebounce. | |
3344 | */ | |
3345 | if (port->vbus_present && tcpm_port_is_sink(port)) { | |
f0690a25 GR |
3346 | tcpm_set_state(port, SNK_ATTACHED, 0); |
3347 | break; | |
3348 | } | |
af450ebb BJS |
3349 | if (!tcpm_port_is_sink(port)) |
3350 | tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, 0); | |
f0690a25 | 3351 | break; |
af450ebb BJS |
3352 | case SNK_TRYWAIT_DEBOUNCE: |
3353 | tcpm_set_state(port, SNK_UNATTACHED, PD_T_PD_DEBOUNCE); | |
f0690a25 | 3354 | break; |
f0690a25 GR |
3355 | case SNK_ATTACHED: |
3356 | ret = tcpm_snk_attach(port); | |
3357 | if (ret < 0) | |
3358 | tcpm_set_state(port, SNK_UNATTACHED, 0); | |
3359 | else | |
3360 | tcpm_set_state(port, SNK_STARTUP, 0); | |
3361 | break; | |
3362 | case SNK_STARTUP: | |
fce042f0 BJS |
3363 | opmode = tcpm_get_pwr_opmode(port->polarity ? |
3364 | port->cc2 : port->cc1); | |
3365 | typec_set_pwr_opmode(port->typec_port, opmode); | |
f0690a25 | 3366 | port->pwr_opmode = TYPEC_PWR_MODE_USB; |
2eadc33f | 3367 | port->negotiated_rev = PD_MAX_REV; |
f0690a25 | 3368 | port->message_id = 0; |
5fec4b54 | 3369 | port->rx_msgid = -1; |
f0690a25 GR |
3370 | port->explicit_contract = false; |
3371 | tcpm_set_state(port, SNK_DISCOVERY, 0); | |
3372 | break; | |
3373 | case SNK_DISCOVERY: | |
3374 | if (port->vbus_present) { | |
3375 | tcpm_set_current_limit(port, | |
3376 | tcpm_get_current_limit(port), | |
3377 | 5000); | |
3378 | tcpm_set_charge(port, true); | |
3379 | tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0); | |
3380 | break; | |
3381 | } | |
3382 | /* | |
3383 | * For DRP, timeouts differ. Also, handling is supposed to be | |
3384 | * different and much more complex (dead battery detection; | |
3385 | * see USB power delivery specification, section 8.3.3.6.1.5.1). | |
3386 | */ | |
3387 | tcpm_set_state(port, hard_reset_state(port), | |
9b0ae699 | 3388 | port->port_type == TYPEC_PORT_DRP ? |
f0690a25 GR |
3389 | PD_T_DB_DETECT : PD_T_NO_RESPONSE); |
3390 | break; | |
3391 | case SNK_DISCOVERY_DEBOUNCE: | |
3392 | tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE_DONE, | |
3393 | PD_T_CC_DEBOUNCE); | |
3394 | break; | |
3395 | case SNK_DISCOVERY_DEBOUNCE_DONE: | |
3396 | if (!tcpm_port_is_disconnected(port) && | |
3397 | tcpm_port_is_sink(port) && | |
3ed8e1c2 | 3398 | ktime_after(port->delayed_runtime, ktime_get())) { |
f0690a25 | 3399 | tcpm_set_state(port, SNK_DISCOVERY, |
3ed8e1c2 | 3400 | ktime_to_ms(ktime_sub(port->delayed_runtime, ktime_get()))); |
f0690a25 GR |
3401 | break; |
3402 | } | |
3403 | tcpm_set_state(port, unattached_state(port), 0); | |
3404 | break; | |
3405 | case SNK_WAIT_CAPABILITIES: | |
3406 | ret = port->tcpc->set_pd_rx(port->tcpc, true); | |
3407 | if (ret < 0) { | |
3408 | tcpm_set_state(port, SNK_READY, 0); | |
3409 | break; | |
3410 | } | |
3411 | /* | |
3412 | * If VBUS has never been low, and we time out waiting | |
3413 | * for source cap, try a soft reset first, in case we | |
3414 | * were already in a stable contract before this boot. | |
3415 | * Do this only once. | |
3416 | */ | |
3417 | if (port->vbus_never_low) { | |
3418 | port->vbus_never_low = false; | |
3419 | tcpm_set_state(port, SOFT_RESET_SEND, | |
3420 | PD_T_SINK_WAIT_CAP); | |
3421 | } else { | |
3422 | tcpm_set_state(port, hard_reset_state(port), | |
3423 | PD_T_SINK_WAIT_CAP); | |
3424 | } | |
3425 | break; | |
3426 | case SNK_NEGOTIATE_CAPABILITIES: | |
3427 | port->pd_capable = true; | |
3428 | port->hard_reset_count = 0; | |
3429 | ret = tcpm_pd_send_request(port); | |
3430 | if (ret < 0) { | |
3431 | /* Let the Source send capabilities again. */ | |
3432 | tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0); | |
3433 | } else { | |
3434 | tcpm_set_state_cond(port, hard_reset_state(port), | |
3435 | PD_T_SENDER_RESPONSE); | |
3436 | } | |
3437 | break; | |
2eadc33f AT |
3438 | case SNK_NEGOTIATE_PPS_CAPABILITIES: |
3439 | ret = tcpm_pd_send_pps_request(port); | |
3440 | if (ret < 0) { | |
3441 | port->pps_status = ret; | |
3442 | /* | |
3443 | * If this was called due to updates to sink | |
3444 | * capabilities, and pps is no longer valid, we should | |
3445 | * safely fall back to a standard PDO. | |
3446 | */ | |
3447 | if (port->update_sink_caps) | |
3448 | tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0); | |
3449 | else | |
3450 | tcpm_set_state(port, SNK_READY, 0); | |
3451 | } else { | |
3452 | tcpm_set_state_cond(port, hard_reset_state(port), | |
3453 | PD_T_SENDER_RESPONSE); | |
3454 | } | |
3455 | break; | |
f0690a25 GR |
3456 | case SNK_TRANSITION_SINK: |
3457 | case SNK_TRANSITION_SINK_VBUS: | |
3458 | tcpm_set_state(port, hard_reset_state(port), | |
3459 | PD_T_PS_TRANSITION); | |
3460 | break; | |
3461 | case SNK_READY: | |
3462 | port->try_snk_count = 0; | |
2eadc33f | 3463 | port->update_sink_caps = false; |
8bf05746 BJS |
3464 | if (port->explicit_contract) { |
3465 | typec_set_pwr_opmode(port->typec_port, | |
3466 | TYPEC_PWR_MODE_PD); | |
3467 | port->pwr_opmode = TYPEC_PWR_MODE_PD; | |
3468 | } | |
f0690a25 | 3469 | |
3113bf1a | 3470 | tcpm_swap_complete(port, 0); |
f0690a25 | 3471 | tcpm_typec_connect(port); |
f0690a25 | 3472 | tcpm_check_send_discover(port); |
8dc4bd07 | 3473 | mod_enable_frs_delayed_work(port, 0); |
2eadc33f | 3474 | tcpm_pps_complete(port, port->pps_status); |
f2a8aa05 | 3475 | power_supply_changed(port->psy); |
f0690a25 GR |
3476 | break; |
3477 | ||
3478 | /* Accessory states */ | |
3479 | case ACC_UNATTACHED: | |
3480 | tcpm_acc_detach(port); | |
3481 | tcpm_set_state(port, SRC_UNATTACHED, 0); | |
3482 | break; | |
3483 | case DEBUG_ACC_ATTACHED: | |
3484 | case AUDIO_ACC_ATTACHED: | |
3485 | ret = tcpm_acc_attach(port); | |
3486 | if (ret < 0) | |
3487 | tcpm_set_state(port, ACC_UNATTACHED, 0); | |
3488 | break; | |
3489 | case AUDIO_ACC_DEBOUNCE: | |
3490 | tcpm_set_state(port, ACC_UNATTACHED, PD_T_CC_DEBOUNCE); | |
3491 | break; | |
3492 | ||
3493 | /* Hard_Reset states */ | |
3494 | case HARD_RESET_SEND: | |
3495 | tcpm_pd_transmit(port, TCPC_TX_HARD_RESET, NULL); | |
3496 | tcpm_set_state(port, HARD_RESET_START, 0); | |
3497 | break; | |
3498 | case HARD_RESET_START: | |
8dc4bd07 BJS |
3499 | port->sink_cap_done = false; |
3500 | if (port->tcpc->enable_frs) | |
3501 | port->tcpc->enable_frs(port->tcpc, false); | |
f0690a25 GR |
3502 | port->hard_reset_count++; |
3503 | port->tcpc->set_pd_rx(port->tcpc, false); | |
3504 | tcpm_unregister_altmodes(port); | |
8dc4bd07 | 3505 | port->nr_sink_caps = 0; |
f0690a25 GR |
3506 | port->send_discover = true; |
3507 | if (port->pwr_role == TYPEC_SOURCE) | |
3508 | tcpm_set_state(port, SRC_HARD_RESET_VBUS_OFF, | |
3509 | PD_T_PS_HARD_RESET); | |
3510 | else | |
3511 | tcpm_set_state(port, SNK_HARD_RESET_SINK_OFF, 0); | |
3512 | break; | |
3513 | case SRC_HARD_RESET_VBUS_OFF: | |
23e26d05 BJS |
3514 | /* |
3515 | * 7.1.5 Response to Hard Resets | |
3516 | * Hard Reset Signaling indicates a communication failure has occurred and the | |
3517 | * Source Shall stop driving VCONN, Shall remove Rp from the VCONN pin and Shall | |
3518 | * drive VBUS to vSafe0V as shown in Figure 7-9. | |
3519 | */ | |
3520 | tcpm_set_vconn(port, false); | |
f0690a25 | 3521 | tcpm_set_vbus(port, false); |
23b5f732 | 3522 | tcpm_set_roles(port, port->self_powered, TYPEC_SOURCE, |
6ecc632d | 3523 | tcpm_data_role_for_source(port)); |
23e26d05 BJS |
3524 | /* |
3525 | * If tcpc fails to notify vbus off, TCPM will wait for PD_T_SAFE_0V + | |
3526 | * PD_T_SRC_RECOVER before turning vbus back on. | |
3527 | * From Table 7-12 Sequence Description for a Source Initiated Hard Reset: | |
3528 | * 4. Policy Engine waits tPSHardReset after sending Hard Reset Signaling and then | |
3529 | * tells the Device Policy Manager to instruct the power supply to perform a | |
3530 | * Hard Reset. The transition to vSafe0V Shall occur within tSafe0V (t2). | |
3531 | * 5. After tSrcRecover the Source applies power to VBUS in an attempt to | |
3532 | * re-establish communication with the Sink and resume USB Default Operation. | |
3533 | * The transition to vSafe5V Shall occur within tSrcTurnOn(t4). | |
3534 | */ | |
3535 | tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SAFE_0V + PD_T_SRC_RECOVER); | |
f0690a25 GR |
3536 | break; |
3537 | case SRC_HARD_RESET_VBUS_ON: | |
23e26d05 | 3538 | tcpm_set_vconn(port, true); |
f0690a25 GR |
3539 | tcpm_set_vbus(port, true); |
3540 | port->tcpc->set_pd_rx(port->tcpc, true); | |
3541 | tcpm_set_attached_state(port, true); | |
3542 | tcpm_set_state(port, SRC_UNATTACHED, PD_T_PS_SOURCE_ON); | |
3543 | break; | |
3544 | case SNK_HARD_RESET_SINK_OFF: | |
f321a02c BJS |
3545 | /* Do not discharge/disconnect during hard reseet */ |
3546 | tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0); | |
2eadc33f | 3547 | memset(&port->pps_data, 0, sizeof(port->pps_data)); |
f0690a25 | 3548 | tcpm_set_vconn(port, false); |
157c0f2f BJS |
3549 | if (port->pd_capable) |
3550 | tcpm_set_charge(port, false); | |
23b5f732 | 3551 | tcpm_set_roles(port, port->self_powered, TYPEC_SINK, |
6ecc632d | 3552 | tcpm_data_role_for_sink(port)); |
f0690a25 GR |
3553 | /* |
3554 | * VBUS may or may not toggle, depending on the adapter. | |
3555 | * If it doesn't toggle, transition to SNK_HARD_RESET_SINK_ON | |
3556 | * directly after timeout. | |
3557 | */ | |
3558 | tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, PD_T_SAFE_0V); | |
3559 | break; | |
3560 | case SNK_HARD_RESET_WAIT_VBUS: | |
3561 | /* Assume we're disconnected if VBUS doesn't come back. */ | |
3562 | tcpm_set_state(port, SNK_UNATTACHED, | |
3563 | PD_T_SRC_RECOVER_MAX + PD_T_SRC_TURN_ON); | |
3564 | break; | |
3565 | case SNK_HARD_RESET_SINK_ON: | |
3566 | /* Note: There is no guarantee that VBUS is on in this state */ | |
3567 | /* | |
3568 | * XXX: | |
3569 | * The specification suggests that dual mode ports in sink | |
3570 | * mode should transition to state PE_SRC_Transition_to_default. | |
3571 | * See USB power delivery specification chapter 8.3.3.6.1.3. | |
3572 | * This would mean to to | |
3573 | * - turn off VCONN, reset power supply | |
3574 | * - request hardware reset | |
3575 | * - turn on VCONN | |
3576 | * - Transition to state PE_Src_Startup | |
3577 | * SNK only ports shall transition to state Snk_Startup | |
3578 | * (see chapter 8.3.3.3.8). | |
3579 | * Similar, dual-mode ports in source mode should transition | |
3580 | * to PE_SNK_Transition_to_default. | |
3581 | */ | |
157c0f2f BJS |
3582 | if (port->pd_capable) { |
3583 | tcpm_set_current_limit(port, | |
3584 | tcpm_get_current_limit(port), | |
3585 | 5000); | |
3586 | tcpm_set_charge(port, true); | |
3587 | } | |
f0690a25 | 3588 | tcpm_set_attached_state(port, true); |
f321a02c | 3589 | tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, VSAFE5V); |
f0690a25 GR |
3590 | tcpm_set_state(port, SNK_STARTUP, 0); |
3591 | break; | |
3592 | ||
3593 | /* Soft_Reset states */ | |
3594 | case SOFT_RESET: | |
3595 | port->message_id = 0; | |
5fec4b54 | 3596 | port->rx_msgid = -1; |
f0690a25 GR |
3597 | tcpm_pd_send_control(port, PD_CTRL_ACCEPT); |
3598 | if (port->pwr_role == TYPEC_SOURCE) | |
3599 | tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0); | |
3600 | else | |
3601 | tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0); | |
3602 | break; | |
3603 | case SOFT_RESET_SEND: | |
3604 | port->message_id = 0; | |
5fec4b54 | 3605 | port->rx_msgid = -1; |
f0690a25 GR |
3606 | if (tcpm_pd_send_control(port, PD_CTRL_SOFT_RESET)) |
3607 | tcpm_set_state_cond(port, hard_reset_state(port), 0); | |
3608 | else | |
3609 | tcpm_set_state_cond(port, hard_reset_state(port), | |
3610 | PD_T_SENDER_RESPONSE); | |
3611 | break; | |
3612 | ||
3613 | /* DR_Swap states */ | |
3614 | case DR_SWAP_SEND: | |
3615 | tcpm_pd_send_control(port, PD_CTRL_DR_SWAP); | |
3616 | tcpm_set_state_cond(port, DR_SWAP_SEND_TIMEOUT, | |
3617 | PD_T_SENDER_RESPONSE); | |
3618 | break; | |
3619 | case DR_SWAP_ACCEPT: | |
3620 | tcpm_pd_send_control(port, PD_CTRL_ACCEPT); | |
3621 | tcpm_set_state_cond(port, DR_SWAP_CHANGE_DR, 0); | |
3622 | break; | |
3623 | case DR_SWAP_SEND_TIMEOUT: | |
3624 | tcpm_swap_complete(port, -ETIMEDOUT); | |
3625 | tcpm_set_state(port, ready_state(port), 0); | |
3626 | break; | |
3627 | case DR_SWAP_CHANGE_DR: | |
3628 | if (port->data_role == TYPEC_HOST) { | |
3629 | tcpm_unregister_altmodes(port); | |
3630 | tcpm_set_roles(port, true, port->pwr_role, | |
3631 | TYPEC_DEVICE); | |
3632 | } else { | |
3633 | tcpm_set_roles(port, true, port->pwr_role, | |
3634 | TYPEC_HOST); | |
3635 | port->send_discover = true; | |
3636 | } | |
f0690a25 GR |
3637 | tcpm_set_state(port, ready_state(port), 0); |
3638 | break; | |
3639 | ||
8dc4bd07 BJS |
3640 | case FR_SWAP_SEND: |
3641 | if (tcpm_pd_send_control(port, PD_CTRL_FR_SWAP)) { | |
3642 | tcpm_set_state(port, ERROR_RECOVERY, 0); | |
3643 | break; | |
3644 | } | |
3645 | tcpm_set_state_cond(port, FR_SWAP_SEND_TIMEOUT, PD_T_SENDER_RESPONSE); | |
3646 | break; | |
3647 | case FR_SWAP_SEND_TIMEOUT: | |
3648 | tcpm_set_state(port, ERROR_RECOVERY, 0); | |
3649 | break; | |
3650 | case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF: | |
3651 | tcpm_set_state(port, ERROR_RECOVERY, PD_T_PS_SOURCE_OFF); | |
3652 | break; | |
3653 | case FR_SWAP_SNK_SRC_NEW_SINK_READY: | |
3654 | if (port->vbus_source) | |
3655 | tcpm_set_state(port, FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED, 0); | |
3656 | else | |
3657 | tcpm_set_state(port, ERROR_RECOVERY, PD_T_RECEIVER_RESPONSE); | |
3658 | break; | |
3659 | case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED: | |
3660 | tcpm_set_pwr_role(port, TYPEC_SOURCE); | |
3661 | if (tcpm_pd_send_control(port, PD_CTRL_PS_RDY)) { | |
3662 | tcpm_set_state(port, ERROR_RECOVERY, 0); | |
3663 | break; | |
3664 | } | |
3665 | tcpm_set_cc(port, tcpm_rp_cc(port)); | |
3666 | tcpm_set_state(port, SRC_STARTUP, PD_T_SWAP_SRC_START); | |
3667 | break; | |
3668 | ||
f0690a25 GR |
3669 | /* PR_Swap states */ |
3670 | case PR_SWAP_ACCEPT: | |
3671 | tcpm_pd_send_control(port, PD_CTRL_ACCEPT); | |
3672 | tcpm_set_state(port, PR_SWAP_START, 0); | |
3673 | break; | |
3674 | case PR_SWAP_SEND: | |
3675 | tcpm_pd_send_control(port, PD_CTRL_PR_SWAP); | |
3676 | tcpm_set_state_cond(port, PR_SWAP_SEND_TIMEOUT, | |
3677 | PD_T_SENDER_RESPONSE); | |
3678 | break; | |
3679 | case PR_SWAP_SEND_TIMEOUT: | |
3680 | tcpm_swap_complete(port, -ETIMEDOUT); | |
3681 | tcpm_set_state(port, ready_state(port), 0); | |
3682 | break; | |
3683 | case PR_SWAP_START: | |
3684 | if (port->pwr_role == TYPEC_SOURCE) | |
3685 | tcpm_set_state(port, PR_SWAP_SRC_SNK_TRANSITION_OFF, | |
3686 | PD_T_SRC_TRANSITION); | |
3687 | else | |
3688 | tcpm_set_state(port, PR_SWAP_SNK_SRC_SINK_OFF, 0); | |
3689 | break; | |
3690 | case PR_SWAP_SRC_SNK_TRANSITION_OFF: | |
f321a02c BJS |
3691 | /* |
3692 | * Prevent vbus discharge circuit from turning on during PR_SWAP | |
3693 | * as this is not a disconnect. | |
3694 | */ | |
f0690a25 GR |
3695 | tcpm_set_vbus(port, false); |
3696 | port->explicit_contract = false; | |
b965b631 | 3697 | /* allow time for Vbus discharge, must be < tSrcSwapStdby */ |
f0690a25 | 3698 | tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF, |
b965b631 | 3699 | PD_T_SRCSWAPSTDBY); |
f0690a25 GR |
3700 | break; |
3701 | case PR_SWAP_SRC_SNK_SOURCE_OFF: | |
3702 | tcpm_set_cc(port, TYPEC_CC_RD); | |
b965b631 BJS |
3703 | /* allow CC debounce */ |
3704 | tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED, | |
3705 | PD_T_CC_DEBOUNCE); | |
3706 | break; | |
3707 | case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED: | |
050161ea GR |
3708 | /* |
3709 | * USB-PD standard, 6.2.1.4, Port Power Role: | |
3710 | * "During the Power Role Swap Sequence, for the initial Source | |
3711 | * Port, the Port Power Role field shall be set to Sink in the | |
3712 | * PS_RDY Message indicating that the initial Source’s power | |
3713 | * supply is turned off" | |
3714 | */ | |
3715 | tcpm_set_pwr_role(port, TYPEC_SINK); | |
f0690a25 GR |
3716 | if (tcpm_pd_send_control(port, PD_CTRL_PS_RDY)) { |
3717 | tcpm_set_state(port, ERROR_RECOVERY, 0); | |
3718 | break; | |
3719 | } | |
3720 | tcpm_set_state_cond(port, SNK_UNATTACHED, PD_T_PS_SOURCE_ON); | |
3721 | break; | |
3722 | case PR_SWAP_SRC_SNK_SINK_ON: | |
f321a02c BJS |
3723 | /* Set the vbus disconnect threshold for implicit contract */ |
3724 | tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, VSAFE5V); | |
f0690a25 GR |
3725 | tcpm_set_state(port, SNK_STARTUP, 0); |
3726 | break; | |
3727 | case PR_SWAP_SNK_SRC_SINK_OFF: | |
f321a02c BJS |
3728 | /* |
3729 | * Prevent vbus discharge circuit from turning on during PR_SWAP | |
3730 | * as this is not a disconnect. | |
3731 | */ | |
3732 | tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, | |
3733 | port->pps_data.active, 0); | |
f0690a25 GR |
3734 | tcpm_set_charge(port, false); |
3735 | tcpm_set_state(port, hard_reset_state(port), | |
3736 | PD_T_PS_SOURCE_OFF); | |
3737 | break; | |
3738 | case PR_SWAP_SNK_SRC_SOURCE_ON: | |
3739 | tcpm_set_cc(port, tcpm_rp_cc(port)); | |
3740 | tcpm_set_vbus(port, true); | |
b965b631 BJS |
3741 | /* |
3742 | * allow time VBUS ramp-up, must be < tNewSrc | |
3743 | * Also, this window overlaps with CC debounce as well. | |
3744 | * So, Wait for the max of two which is PD_T_NEWSRC | |
3745 | */ | |
3746 | tcpm_set_state(port, PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP, | |
3747 | PD_T_NEWSRC); | |
3748 | break; | |
3749 | case PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP: | |
050161ea GR |
3750 | /* |
3751 | * USB PD standard, 6.2.1.4: | |
3752 | * "Subsequent Messages initiated by the Policy Engine, | |
3753 | * such as the PS_RDY Message sent to indicate that Vbus | |
3754 | * is ready, will have the Port Power Role field set to | |
3755 | * Source." | |
3756 | */ | |
f0690a25 | 3757 | tcpm_set_pwr_role(port, TYPEC_SOURCE); |
050161ea | 3758 | tcpm_pd_send_control(port, PD_CTRL_PS_RDY); |
6bbe2a90 | 3759 | tcpm_set_state(port, SRC_STARTUP, PD_T_SWAP_SRC_START); |
f0690a25 GR |
3760 | break; |
3761 | ||
3762 | case VCONN_SWAP_ACCEPT: | |
3763 | tcpm_pd_send_control(port, PD_CTRL_ACCEPT); | |
3764 | tcpm_set_state(port, VCONN_SWAP_START, 0); | |
3765 | break; | |
3766 | case VCONN_SWAP_SEND: | |
3767 | tcpm_pd_send_control(port, PD_CTRL_VCONN_SWAP); | |
3768 | tcpm_set_state(port, VCONN_SWAP_SEND_TIMEOUT, | |
3769 | PD_T_SENDER_RESPONSE); | |
3770 | break; | |
3771 | case VCONN_SWAP_SEND_TIMEOUT: | |
3772 | tcpm_swap_complete(port, -ETIMEDOUT); | |
3773 | tcpm_set_state(port, ready_state(port), 0); | |
3774 | break; | |
3775 | case VCONN_SWAP_START: | |
3776 | if (port->vconn_role == TYPEC_SOURCE) | |
3777 | tcpm_set_state(port, VCONN_SWAP_WAIT_FOR_VCONN, 0); | |
3778 | else | |
3779 | tcpm_set_state(port, VCONN_SWAP_TURN_ON_VCONN, 0); | |
3780 | break; | |
3781 | case VCONN_SWAP_WAIT_FOR_VCONN: | |
3782 | tcpm_set_state(port, hard_reset_state(port), | |
3783 | PD_T_VCONN_SOURCE_ON); | |
3784 | break; | |
3785 | case VCONN_SWAP_TURN_ON_VCONN: | |
3786 | tcpm_set_vconn(port, true); | |
3787 | tcpm_pd_send_control(port, PD_CTRL_PS_RDY); | |
f0690a25 GR |
3788 | tcpm_set_state(port, ready_state(port), 0); |
3789 | break; | |
3790 | case VCONN_SWAP_TURN_OFF_VCONN: | |
3791 | tcpm_set_vconn(port, false); | |
f0690a25 GR |
3792 | tcpm_set_state(port, ready_state(port), 0); |
3793 | break; | |
3794 | ||
3795 | case DR_SWAP_CANCEL: | |
3796 | case PR_SWAP_CANCEL: | |
3797 | case VCONN_SWAP_CANCEL: | |
3798 | tcpm_swap_complete(port, port->swap_status); | |
3799 | if (port->pwr_role == TYPEC_SOURCE) | |
3800 | tcpm_set_state(port, SRC_READY, 0); | |
3801 | else | |
3802 | tcpm_set_state(port, SNK_READY, 0); | |
3803 | break; | |
8dc4bd07 BJS |
3804 | case FR_SWAP_CANCEL: |
3805 | if (port->pwr_role == TYPEC_SOURCE) | |
3806 | tcpm_set_state(port, SRC_READY, 0); | |
3807 | else | |
3808 | tcpm_set_state(port, SNK_READY, 0); | |
3809 | break; | |
f0690a25 GR |
3810 | |
3811 | case BIST_RX: | |
3812 | switch (BDO_MODE_MASK(port->bist_request)) { | |
3813 | case BDO_MODE_CARRIER2: | |
3814 | tcpm_pd_transmit(port, TCPC_TX_BIST_MODE_2, NULL); | |
6e1c2241 BJS |
3815 | tcpm_set_state(port, unattached_state(port), |
3816 | PD_T_BIST_CONT_MODE); | |
f0690a25 | 3817 | break; |
b2dcfefc BJS |
3818 | case BDO_MODE_TESTDATA: |
3819 | if (port->tcpc->set_bist_data) { | |
3820 | tcpm_log(port, "Enable BIST MODE TESTDATA"); | |
3821 | port->tcpc->set_bist_data(port->tcpc, true); | |
3822 | } | |
3823 | break; | |
f0690a25 GR |
3824 | default: |
3825 | break; | |
3826 | } | |
f0690a25 | 3827 | break; |
64f7c494 AT |
3828 | case GET_STATUS_SEND: |
3829 | tcpm_pd_send_control(port, PD_CTRL_GET_STATUS); | |
3830 | tcpm_set_state(port, GET_STATUS_SEND_TIMEOUT, | |
3831 | PD_T_SENDER_RESPONSE); | |
3832 | break; | |
3833 | case GET_STATUS_SEND_TIMEOUT: | |
3834 | tcpm_set_state(port, ready_state(port), 0); | |
3835 | break; | |
3836 | case GET_PPS_STATUS_SEND: | |
3837 | tcpm_pd_send_control(port, PD_CTRL_GET_PPS_STATUS); | |
3838 | tcpm_set_state(port, GET_PPS_STATUS_SEND_TIMEOUT, | |
3839 | PD_T_SENDER_RESPONSE); | |
3840 | break; | |
3841 | case GET_PPS_STATUS_SEND_TIMEOUT: | |
3842 | tcpm_set_state(port, ready_state(port), 0); | |
3843 | break; | |
8dc4bd07 BJS |
3844 | case GET_SINK_CAP: |
3845 | tcpm_pd_send_control(port, PD_CTRL_GET_SINK_CAP); | |
3846 | tcpm_set_state(port, GET_SINK_CAP_TIMEOUT, PD_T_SENDER_RESPONSE); | |
3847 | break; | |
3848 | case GET_SINK_CAP_TIMEOUT: | |
3849 | port->sink_cap_done = true; | |
3850 | tcpm_set_state(port, ready_state(port), 0); | |
3851 | break; | |
f0690a25 GR |
3852 | case ERROR_RECOVERY: |
3853 | tcpm_swap_complete(port, -EPROTO); | |
2eadc33f | 3854 | tcpm_pps_complete(port, -EPROTO); |
b17dd571 GR |
3855 | tcpm_set_state(port, PORT_RESET, 0); |
3856 | break; | |
3857 | case PORT_RESET: | |
f0690a25 | 3858 | tcpm_reset_port(port); |
f0690a25 | 3859 | tcpm_set_cc(port, TYPEC_CC_OPEN); |
b17dd571 | 3860 | tcpm_set_state(port, PORT_RESET_WAIT_OFF, |
f0690a25 GR |
3861 | PD_T_ERROR_RECOVERY); |
3862 | break; | |
b17dd571 | 3863 | case PORT_RESET_WAIT_OFF: |
f0690a25 GR |
3864 | tcpm_set_state(port, |
3865 | tcpm_default_state(port), | |
3866 | port->vbus_present ? PD_T_PS_SOURCE_OFF : 0); | |
3867 | break; | |
3868 | default: | |
3869 | WARN(1, "Unexpected port state %d\n", port->state); | |
3870 | break; | |
3871 | } | |
3872 | } | |
3873 | ||
3ed8e1c2 | 3874 | static void tcpm_state_machine_work(struct kthread_work *work) |
f0690a25 | 3875 | { |
3ed8e1c2 | 3876 | struct tcpm_port *port = container_of(work, struct tcpm_port, state_machine); |
f0690a25 GR |
3877 | enum tcpm_state prev_state; |
3878 | ||
3879 | mutex_lock(&port->lock); | |
3880 | port->state_machine_running = true; | |
3881 | ||
3882 | if (port->queued_message && tcpm_send_queued_message(port)) | |
3883 | goto done; | |
3884 | ||
3885 | /* If we were queued due to a delayed state change, update it now */ | |
3886 | if (port->delayed_state) { | |
3887 | tcpm_log(port, "state change %s -> %s [delayed %ld ms]", | |
3888 | tcpm_states[port->state], | |
3889 | tcpm_states[port->delayed_state], port->delay_ms); | |
3890 | port->prev_state = port->state; | |
3891 | port->state = port->delayed_state; | |
3892 | port->delayed_state = INVALID_STATE; | |
3893 | } | |
3894 | ||
3895 | /* | |
3896 | * Continue running as long as we have (non-delayed) state changes | |
3897 | * to make. | |
3898 | */ | |
3899 | do { | |
3900 | prev_state = port->state; | |
3901 | run_state_machine(port); | |
3902 | if (port->queued_message) | |
3903 | tcpm_send_queued_message(port); | |
3904 | } while (port->state != prev_state && !port->delayed_state); | |
3905 | ||
3906 | done: | |
3907 | port->state_machine_running = false; | |
3908 | mutex_unlock(&port->lock); | |
3909 | } | |
3910 | ||
3911 | static void _tcpm_cc_change(struct tcpm_port *port, enum typec_cc_status cc1, | |
3912 | enum typec_cc_status cc2) | |
3913 | { | |
3914 | enum typec_cc_status old_cc1, old_cc2; | |
3915 | enum tcpm_state new_state; | |
3916 | ||
3917 | old_cc1 = port->cc1; | |
3918 | old_cc2 = port->cc2; | |
3919 | port->cc1 = cc1; | |
3920 | port->cc2 = cc2; | |
3921 | ||
3922 | tcpm_log_force(port, | |
3923 | "CC1: %u -> %u, CC2: %u -> %u [state %s, polarity %d, %s]", | |
3924 | old_cc1, cc1, old_cc2, cc2, tcpm_states[port->state], | |
3925 | port->polarity, | |
3926 | tcpm_port_is_disconnected(port) ? "disconnected" | |
3927 | : "connected"); | |
3928 | ||
3929 | switch (port->state) { | |
7893f9e1 | 3930 | case TOGGLING: |
f0690a25 GR |
3931 | if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) || |
3932 | tcpm_port_is_source(port)) | |
3933 | tcpm_set_state(port, SRC_ATTACH_WAIT, 0); | |
3934 | else if (tcpm_port_is_sink(port)) | |
3935 | tcpm_set_state(port, SNK_ATTACH_WAIT, 0); | |
3936 | break; | |
3937 | case SRC_UNATTACHED: | |
3938 | case ACC_UNATTACHED: | |
3939 | if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) || | |
3940 | tcpm_port_is_source(port)) | |
3941 | tcpm_set_state(port, SRC_ATTACH_WAIT, 0); | |
3942 | break; | |
3943 | case SRC_ATTACH_WAIT: | |
3944 | if (tcpm_port_is_disconnected(port) || | |
3945 | tcpm_port_is_audio_detached(port)) | |
3946 | tcpm_set_state(port, SRC_UNATTACHED, 0); | |
3947 | else if (cc1 != old_cc1 || cc2 != old_cc2) | |
3948 | tcpm_set_state(port, SRC_ATTACH_WAIT, 0); | |
3949 | break; | |
3950 | case SRC_ATTACHED: | |
f3b73364 BJS |
3951 | case SRC_SEND_CAPABILITIES: |
3952 | case SRC_READY: | |
3953 | if (tcpm_port_is_disconnected(port) || | |
67540460 LJ |
3954 | !tcpm_port_is_source(port)) { |
3955 | if (port->port_type == TYPEC_PORT_SRC) | |
3956 | tcpm_set_state(port, SRC_UNATTACHED, 0); | |
3957 | else | |
3958 | tcpm_set_state(port, SNK_UNATTACHED, 0); | |
3959 | } | |
f0690a25 | 3960 | break; |
f0690a25 GR |
3961 | case SNK_UNATTACHED: |
3962 | if (tcpm_port_is_sink(port)) | |
3963 | tcpm_set_state(port, SNK_ATTACH_WAIT, 0); | |
3964 | break; | |
3965 | case SNK_ATTACH_WAIT: | |
3966 | if ((port->cc1 == TYPEC_CC_OPEN && | |
3967 | port->cc2 != TYPEC_CC_OPEN) || | |
3968 | (port->cc1 != TYPEC_CC_OPEN && | |
3969 | port->cc2 == TYPEC_CC_OPEN)) | |
3970 | new_state = SNK_DEBOUNCED; | |
3971 | else if (tcpm_port_is_disconnected(port)) | |
3972 | new_state = SNK_UNATTACHED; | |
3973 | else | |
3974 | break; | |
3975 | if (new_state != port->delayed_state) | |
3976 | tcpm_set_state(port, SNK_ATTACH_WAIT, 0); | |
3977 | break; | |
3978 | case SNK_DEBOUNCED: | |
3979 | if (tcpm_port_is_disconnected(port)) | |
3980 | new_state = SNK_UNATTACHED; | |
3981 | else if (port->vbus_present) | |
3982 | new_state = tcpm_try_src(port) ? SRC_TRY : SNK_ATTACHED; | |
3983 | else | |
3984 | new_state = SNK_UNATTACHED; | |
3985 | if (new_state != port->delayed_state) | |
3986 | tcpm_set_state(port, SNK_DEBOUNCED, 0); | |
3987 | break; | |
3988 | case SNK_READY: | |
3989 | if (tcpm_port_is_disconnected(port)) | |
3990 | tcpm_set_state(port, unattached_state(port), 0); | |
3991 | else if (!port->pd_capable && | |
3992 | (cc1 != old_cc1 || cc2 != old_cc2)) | |
3993 | tcpm_set_current_limit(port, | |
3994 | tcpm_get_current_limit(port), | |
3995 | 5000); | |
3996 | break; | |
3997 | ||
3998 | case AUDIO_ACC_ATTACHED: | |
3999 | if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN) | |
4000 | tcpm_set_state(port, AUDIO_ACC_DEBOUNCE, 0); | |
4001 | break; | |
4002 | case AUDIO_ACC_DEBOUNCE: | |
4003 | if (tcpm_port_is_audio(port)) | |
4004 | tcpm_set_state(port, AUDIO_ACC_ATTACHED, 0); | |
4005 | break; | |
4006 | ||
4007 | case DEBUG_ACC_ATTACHED: | |
4008 | if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN) | |
4009 | tcpm_set_state(port, ACC_UNATTACHED, 0); | |
4010 | break; | |
4011 | ||
4012 | case SNK_TRY: | |
4013 | /* Do nothing, waiting for timeout */ | |
4014 | break; | |
4015 | ||
4016 | case SNK_DISCOVERY: | |
4017 | /* CC line is unstable, wait for debounce */ | |
4018 | if (tcpm_port_is_disconnected(port)) | |
4019 | tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE, 0); | |
4020 | break; | |
4021 | case SNK_DISCOVERY_DEBOUNCE: | |
4022 | break; | |
4023 | ||
4024 | case SRC_TRYWAIT: | |
4025 | /* Hand over to state machine if needed */ | |
4026 | if (!port->vbus_present && tcpm_port_is_source(port)) | |
02d5be46 BJS |
4027 | tcpm_set_state(port, SRC_TRYWAIT_DEBOUNCE, 0); |
4028 | break; | |
4029 | case SRC_TRYWAIT_DEBOUNCE: | |
4030 | if (port->vbus_present || !tcpm_port_is_source(port)) | |
f0690a25 GR |
4031 | tcpm_set_state(port, SRC_TRYWAIT, 0); |
4032 | break; | |
a0a3e04e BJS |
4033 | case SNK_TRY_WAIT_DEBOUNCE: |
4034 | if (!tcpm_port_is_sink(port)) { | |
4035 | port->max_wait = 0; | |
4036 | tcpm_set_state(port, SRC_TRYWAIT, 0); | |
f0690a25 | 4037 | } |
f0690a25 | 4038 | break; |
131c7d12 | 4039 | case SRC_TRY_WAIT: |
c79d92bd BJS |
4040 | if (tcpm_port_is_source(port)) |
4041 | tcpm_set_state(port, SRC_TRY_DEBOUNCE, 0); | |
f0690a25 GR |
4042 | break; |
4043 | case SRC_TRY_DEBOUNCE: | |
131c7d12 | 4044 | tcpm_set_state(port, SRC_TRY_WAIT, 0); |
f0690a25 GR |
4045 | break; |
4046 | case SNK_TRYWAIT_DEBOUNCE: | |
af450ebb BJS |
4047 | if (tcpm_port_is_sink(port)) |
4048 | tcpm_set_state(port, SNK_TRYWAIT_VBUS, 0); | |
4049 | break; | |
4050 | case SNK_TRYWAIT_VBUS: | |
4051 | if (!tcpm_port_is_sink(port)) | |
f0690a25 GR |
4052 | tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, 0); |
4053 | break; | |
11e5e568 BJS |
4054 | case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS: |
4055 | if (!tcpm_port_is_sink(port)) | |
4056 | tcpm_set_state(port, SRC_TRYWAIT, PD_T_TRY_CC_DEBOUNCE); | |
4057 | else | |
4058 | tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS, 0); | |
4059 | break; | |
af450ebb BJS |
4060 | case SNK_TRYWAIT: |
4061 | /* Do nothing, waiting for tCCDebounce */ | |
4062 | break; | |
f0690a25 GR |
4063 | case PR_SWAP_SNK_SRC_SINK_OFF: |
4064 | case PR_SWAP_SRC_SNK_TRANSITION_OFF: | |
4065 | case PR_SWAP_SRC_SNK_SOURCE_OFF: | |
b965b631 BJS |
4066 | case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED: |
4067 | case PR_SWAP_SNK_SRC_SOURCE_ON: | |
f0690a25 | 4068 | /* |
b965b631 | 4069 | * CC state change is expected in PR_SWAP |
f0690a25 GR |
4070 | * Ignore it. |
4071 | */ | |
4072 | break; | |
8dc4bd07 BJS |
4073 | case FR_SWAP_SEND: |
4074 | case FR_SWAP_SEND_TIMEOUT: | |
4075 | case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF: | |
4076 | case FR_SWAP_SNK_SRC_NEW_SINK_READY: | |
4077 | case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED: | |
4078 | /* Do nothing, CC change expected */ | |
4079 | break; | |
f0690a25 | 4080 | |
90178974 BJS |
4081 | case PORT_RESET: |
4082 | case PORT_RESET_WAIT_OFF: | |
4083 | /* | |
4084 | * State set back to default mode once the timer completes. | |
4085 | * Ignore CC changes here. | |
4086 | */ | |
4087 | break; | |
4088 | ||
f0690a25 GR |
4089 | default: |
4090 | if (tcpm_port_is_disconnected(port)) | |
4091 | tcpm_set_state(port, unattached_state(port), 0); | |
4092 | break; | |
4093 | } | |
4094 | } | |
4095 | ||
4096 | static void _tcpm_pd_vbus_on(struct tcpm_port *port) | |
4097 | { | |
f0690a25 GR |
4098 | tcpm_log_force(port, "VBUS on"); |
4099 | port->vbus_present = true; | |
4100 | switch (port->state) { | |
4101 | case SNK_TRANSITION_SINK_VBUS: | |
8bf05746 | 4102 | port->explicit_contract = true; |
f0690a25 GR |
4103 | tcpm_set_state(port, SNK_READY, 0); |
4104 | break; | |
4105 | case SNK_DISCOVERY: | |
4106 | tcpm_set_state(port, SNK_DISCOVERY, 0); | |
4107 | break; | |
4108 | ||
4109 | case SNK_DEBOUNCED: | |
4110 | tcpm_set_state(port, tcpm_try_src(port) ? SRC_TRY | |
4111 | : SNK_ATTACHED, | |
4112 | 0); | |
4113 | break; | |
4114 | case SNK_HARD_RESET_WAIT_VBUS: | |
4115 | tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, 0); | |
4116 | break; | |
4117 | case SRC_ATTACHED: | |
4118 | tcpm_set_state(port, SRC_STARTUP, 0); | |
4119 | break; | |
4120 | case SRC_HARD_RESET_VBUS_ON: | |
4121 | tcpm_set_state(port, SRC_STARTUP, 0); | |
4122 | break; | |
4123 | ||
4124 | case SNK_TRY: | |
4125 | /* Do nothing, waiting for timeout */ | |
4126 | break; | |
4127 | case SRC_TRYWAIT: | |
02d5be46 BJS |
4128 | /* Do nothing, Waiting for Rd to be detected */ |
4129 | break; | |
4130 | case SRC_TRYWAIT_DEBOUNCE: | |
4131 | tcpm_set_state(port, SRC_TRYWAIT, 0); | |
f0690a25 | 4132 | break; |
a0a3e04e BJS |
4133 | case SNK_TRY_WAIT_DEBOUNCE: |
4134 | /* Do nothing, waiting for PD_DEBOUNCE to do be done */ | |
f0690a25 GR |
4135 | break; |
4136 | case SNK_TRYWAIT: | |
af450ebb BJS |
4137 | /* Do nothing, waiting for tCCDebounce */ |
4138 | break; | |
4139 | case SNK_TRYWAIT_VBUS: | |
4140 | if (tcpm_port_is_sink(port)) | |
4141 | tcpm_set_state(port, SNK_ATTACHED, 0); | |
4142 | break; | |
4143 | case SNK_TRYWAIT_DEBOUNCE: | |
4144 | /* Do nothing, waiting for Rp */ | |
f0690a25 | 4145 | break; |
11e5e568 BJS |
4146 | case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS: |
4147 | if (port->vbus_present && tcpm_port_is_sink(port)) | |
4148 | tcpm_set_state(port, SNK_ATTACHED, 0); | |
4149 | break; | |
131c7d12 BJS |
4150 | case SRC_TRY_WAIT: |
4151 | case SRC_TRY_DEBOUNCE: | |
4152 | /* Do nothing, waiting for sink detection */ | |
4153 | break; | |
a30a00e3 BJS |
4154 | case FR_SWAP_SEND: |
4155 | case FR_SWAP_SEND_TIMEOUT: | |
4156 | case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF: | |
4157 | case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED: | |
4158 | if (port->tcpc->frs_sourcing_vbus) | |
4159 | port->tcpc->frs_sourcing_vbus(port->tcpc); | |
4160 | break; | |
8dc4bd07 | 4161 | case FR_SWAP_SNK_SRC_NEW_SINK_READY: |
a30a00e3 BJS |
4162 | if (port->tcpc->frs_sourcing_vbus) |
4163 | port->tcpc->frs_sourcing_vbus(port->tcpc); | |
8dc4bd07 BJS |
4164 | tcpm_set_state(port, FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED, 0); |
4165 | break; | |
90178974 BJS |
4166 | |
4167 | case PORT_RESET: | |
4168 | case PORT_RESET_WAIT_OFF: | |
4169 | /* | |
4170 | * State set back to default mode once the timer completes. | |
4171 | * Ignore vbus changes here. | |
4172 | */ | |
4173 | break; | |
4174 | ||
f0690a25 GR |
4175 | default: |
4176 | break; | |
4177 | } | |
4178 | } | |
4179 | ||
4180 | static void _tcpm_pd_vbus_off(struct tcpm_port *port) | |
4181 | { | |
f0690a25 GR |
4182 | tcpm_log_force(port, "VBUS off"); |
4183 | port->vbus_present = false; | |
4184 | port->vbus_never_low = false; | |
4185 | switch (port->state) { | |
4186 | case SNK_HARD_RESET_SINK_OFF: | |
4187 | tcpm_set_state(port, SNK_HARD_RESET_WAIT_VBUS, 0); | |
4188 | break; | |
4189 | case SRC_HARD_RESET_VBUS_OFF: | |
23e26d05 BJS |
4190 | /* |
4191 | * After establishing the vSafe0V voltage condition on VBUS, the Source Shall wait | |
4192 | * tSrcRecover before re-applying VCONN and restoring VBUS to vSafe5V. | |
4193 | */ | |
4194 | tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SRC_RECOVER); | |
f0690a25 GR |
4195 | break; |
4196 | case HARD_RESET_SEND: | |
4197 | break; | |
4198 | ||
4199 | case SNK_TRY: | |
4200 | /* Do nothing, waiting for timeout */ | |
4201 | break; | |
4202 | case SRC_TRYWAIT: | |
4203 | /* Hand over to state machine if needed */ | |
4204 | if (tcpm_port_is_source(port)) | |
02d5be46 | 4205 | tcpm_set_state(port, SRC_TRYWAIT_DEBOUNCE, 0); |
f0690a25 | 4206 | break; |
a0a3e04e BJS |
4207 | case SNK_TRY_WAIT_DEBOUNCE: |
4208 | /* Do nothing, waiting for PD_DEBOUNCE to do be done */ | |
f0690a25 | 4209 | break; |
af450ebb | 4210 | case SNK_TRYWAIT: |
f0690a25 | 4211 | case SNK_TRYWAIT_VBUS: |
af450ebb | 4212 | case SNK_TRYWAIT_DEBOUNCE: |
f0690a25 | 4213 | break; |
f0690a25 GR |
4214 | case SNK_ATTACH_WAIT: |
4215 | tcpm_set_state(port, SNK_UNATTACHED, 0); | |
4216 | break; | |
4217 | ||
4218 | case SNK_NEGOTIATE_CAPABILITIES: | |
4219 | break; | |
4220 | ||
4221 | case PR_SWAP_SRC_SNK_TRANSITION_OFF: | |
4222 | tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF, 0); | |
4223 | break; | |
4224 | ||
4225 | case PR_SWAP_SNK_SRC_SINK_OFF: | |
4226 | /* Do nothing, expected */ | |
4227 | break; | |
4228 | ||
7abc6ca5 BJS |
4229 | case PR_SWAP_SNK_SRC_SOURCE_ON: |
4230 | /* | |
4231 | * Do nothing when vbus off notification is received. | |
4232 | * TCPM can wait for PD_T_NEWSRC in PR_SWAP_SNK_SRC_SOURCE_ON | |
4233 | * for the vbus source to ramp up. | |
4234 | */ | |
4235 | break; | |
4236 | ||
b17dd571 | 4237 | case PORT_RESET_WAIT_OFF: |
c749d4d0 | 4238 | tcpm_set_state(port, tcpm_default_state(port), 0); |
f0690a25 | 4239 | break; |
90178974 | 4240 | |
131c7d12 BJS |
4241 | case SRC_TRY_WAIT: |
4242 | case SRC_TRY_DEBOUNCE: | |
4243 | /* Do nothing, waiting for sink detection */ | |
4244 | break; | |
90178974 BJS |
4245 | |
4246 | case PORT_RESET: | |
4247 | /* | |
4248 | * State set back to default mode once the timer completes. | |
4249 | * Ignore vbus changes here. | |
4250 | */ | |
4251 | break; | |
4252 | ||
8dc4bd07 BJS |
4253 | case FR_SWAP_SEND: |
4254 | case FR_SWAP_SEND_TIMEOUT: | |
4255 | case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF: | |
4256 | case FR_SWAP_SNK_SRC_NEW_SINK_READY: | |
4257 | case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED: | |
4258 | /* Do nothing, vbus drop expected */ | |
4259 | break; | |
4260 | ||
f0690a25 GR |
4261 | default: |
4262 | if (port->pwr_role == TYPEC_SINK && | |
4263 | port->attached) | |
4264 | tcpm_set_state(port, SNK_UNATTACHED, 0); | |
4265 | break; | |
4266 | } | |
4267 | } | |
4268 | ||
4269 | static void _tcpm_pd_hard_reset(struct tcpm_port *port) | |
4270 | { | |
4271 | tcpm_log_force(port, "Received hard reset"); | |
6e1c2241 BJS |
4272 | if (port->bist_request == BDO_MODE_TESTDATA && port->tcpc->set_bist_data) |
4273 | port->tcpc->set_bist_data(port->tcpc, false); | |
4274 | ||
f0690a25 GR |
4275 | /* |
4276 | * If we keep receiving hard reset requests, executing the hard reset | |
4277 | * must have failed. Revert to error recovery if that happens. | |
4278 | */ | |
4279 | tcpm_set_state(port, | |
4280 | port->hard_reset_count < PD_N_HARD_RESET_COUNT ? | |
4281 | HARD_RESET_START : ERROR_RECOVERY, | |
4282 | 0); | |
4283 | } | |
4284 | ||
3ed8e1c2 | 4285 | static void tcpm_pd_event_handler(struct kthread_work *work) |
f0690a25 GR |
4286 | { |
4287 | struct tcpm_port *port = container_of(work, struct tcpm_port, | |
4288 | event_work); | |
4289 | u32 events; | |
4290 | ||
4291 | mutex_lock(&port->lock); | |
4292 | ||
4293 | spin_lock(&port->pd_event_lock); | |
4294 | while (port->pd_events) { | |
4295 | events = port->pd_events; | |
4296 | port->pd_events = 0; | |
4297 | spin_unlock(&port->pd_event_lock); | |
4298 | if (events & TCPM_RESET_EVENT) | |
4299 | _tcpm_pd_hard_reset(port); | |
4300 | if (events & TCPM_VBUS_EVENT) { | |
4301 | bool vbus; | |
4302 | ||
4303 | vbus = port->tcpc->get_vbus(port->tcpc); | |
4304 | if (vbus) | |
4305 | _tcpm_pd_vbus_on(port); | |
4306 | else | |
4307 | _tcpm_pd_vbus_off(port); | |
4308 | } | |
4309 | if (events & TCPM_CC_EVENT) { | |
4310 | enum typec_cc_status cc1, cc2; | |
4311 | ||
4312 | if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0) | |
4313 | _tcpm_cc_change(port, cc1, cc2); | |
4314 | } | |
8dc4bd07 BJS |
4315 | if (events & TCPM_FRS_EVENT) { |
4316 | if (port->state == SNK_READY) | |
4317 | tcpm_set_state(port, FR_SWAP_SEND, 0); | |
4318 | else | |
4319 | tcpm_log(port, "Discarding FRS_SIGNAL! Not in sink ready"); | |
4320 | } | |
4321 | if (events & TCPM_SOURCING_VBUS) { | |
4322 | tcpm_log(port, "sourcing vbus"); | |
4323 | /* | |
4324 | * In fast role swap case TCPC autonomously sources vbus. Set vbus_source | |
4325 | * true as TCPM wouldn't have called tcpm_set_vbus. | |
4326 | * | |
4327 | * When vbus is sourced on the command on TCPM i.e. TCPM called | |
4328 | * tcpm_set_vbus to source vbus, vbus_source would already be true. | |
4329 | */ | |
4330 | port->vbus_source = true; | |
4331 | _tcpm_pd_vbus_on(port); | |
4332 | } | |
4333 | ||
f0690a25 GR |
4334 | spin_lock(&port->pd_event_lock); |
4335 | } | |
4336 | spin_unlock(&port->pd_event_lock); | |
4337 | mutex_unlock(&port->lock); | |
4338 | } | |
4339 | ||
4340 | void tcpm_cc_change(struct tcpm_port *port) | |
4341 | { | |
4342 | spin_lock(&port->pd_event_lock); | |
4343 | port->pd_events |= TCPM_CC_EVENT; | |
4344 | spin_unlock(&port->pd_event_lock); | |
3ed8e1c2 | 4345 | kthread_queue_work(port->wq, &port->event_work); |
f0690a25 GR |
4346 | } |
4347 | EXPORT_SYMBOL_GPL(tcpm_cc_change); | |
4348 | ||
4349 | void tcpm_vbus_change(struct tcpm_port *port) | |
4350 | { | |
4351 | spin_lock(&port->pd_event_lock); | |
4352 | port->pd_events |= TCPM_VBUS_EVENT; | |
4353 | spin_unlock(&port->pd_event_lock); | |
3ed8e1c2 | 4354 | kthread_queue_work(port->wq, &port->event_work); |
f0690a25 GR |
4355 | } |
4356 | EXPORT_SYMBOL_GPL(tcpm_vbus_change); | |
4357 | ||
4358 | void tcpm_pd_hard_reset(struct tcpm_port *port) | |
4359 | { | |
4360 | spin_lock(&port->pd_event_lock); | |
4361 | port->pd_events = TCPM_RESET_EVENT; | |
4362 | spin_unlock(&port->pd_event_lock); | |
3ed8e1c2 | 4363 | kthread_queue_work(port->wq, &port->event_work); |
f0690a25 GR |
4364 | } |
4365 | EXPORT_SYMBOL_GPL(tcpm_pd_hard_reset); | |
4366 | ||
8dc4bd07 BJS |
4367 | void tcpm_sink_frs(struct tcpm_port *port) |
4368 | { | |
4369 | spin_lock(&port->pd_event_lock); | |
4370 | port->pd_events = TCPM_FRS_EVENT; | |
4371 | spin_unlock(&port->pd_event_lock); | |
4372 | kthread_queue_work(port->wq, &port->event_work); | |
4373 | } | |
4374 | EXPORT_SYMBOL_GPL(tcpm_sink_frs); | |
4375 | ||
4376 | void tcpm_sourcing_vbus(struct tcpm_port *port) | |
4377 | { | |
4378 | spin_lock(&port->pd_event_lock); | |
4379 | port->pd_events = TCPM_SOURCING_VBUS; | |
4380 | spin_unlock(&port->pd_event_lock); | |
4381 | kthread_queue_work(port->wq, &port->event_work); | |
4382 | } | |
4383 | EXPORT_SYMBOL_GPL(tcpm_sourcing_vbus); | |
4384 | ||
4385 | static void tcpm_enable_frs_work(struct kthread_work *work) | |
4386 | { | |
4387 | struct tcpm_port *port = container_of(work, struct tcpm_port, enable_frs); | |
4388 | ||
4389 | mutex_lock(&port->lock); | |
4390 | /* Not FRS capable */ | |
4391 | if (!port->connected || port->port_type != TYPEC_PORT_DRP || | |
4392 | port->pwr_opmode != TYPEC_PWR_MODE_PD || | |
4393 | !port->tcpc->enable_frs || | |
4394 | /* Sink caps queried */ | |
4395 | port->sink_cap_done || port->negotiated_rev < PD_REV30) | |
4396 | goto unlock; | |
4397 | ||
4398 | /* Send when the state machine is idle */ | |
4399 | if (port->state != SNK_READY || port->vdm_state != VDM_STATE_DONE || port->send_discover) | |
4400 | goto resched; | |
4401 | ||
4402 | tcpm_set_state(port, GET_SINK_CAP, 0); | |
4403 | port->sink_cap_done = true; | |
4404 | ||
4405 | resched: | |
4406 | mod_enable_frs_delayed_work(port, GET_SINK_CAP_RETRY_MS); | |
4407 | unlock: | |
4408 | mutex_unlock(&port->lock); | |
4409 | } | |
4410 | ||
00ec21e5 | 4411 | static int tcpm_dr_set(struct typec_port *p, enum typec_data_role data) |
f0690a25 | 4412 | { |
00ec21e5 | 4413 | struct tcpm_port *port = typec_get_drvdata(p); |
f0690a25 GR |
4414 | int ret; |
4415 | ||
4416 | mutex_lock(&port->swap_lock); | |
4417 | mutex_lock(&port->lock); | |
4418 | ||
6ecc632d | 4419 | if (port->typec_caps.data != TYPEC_PORT_DRD) { |
f0690a25 GR |
4420 | ret = -EINVAL; |
4421 | goto port_unlock; | |
4422 | } | |
4423 | if (port->state != SRC_READY && port->state != SNK_READY) { | |
4424 | ret = -EAGAIN; | |
4425 | goto port_unlock; | |
4426 | } | |
4427 | ||
4428 | if (port->data_role == data) { | |
4429 | ret = 0; | |
4430 | goto port_unlock; | |
4431 | } | |
4432 | ||
4433 | /* | |
4434 | * XXX | |
4435 | * 6.3.9: If an alternate mode is active, a request to swap | |
4436 | * alternate modes shall trigger a port reset. | |
4437 | * Reject data role swap request in this case. | |
4438 | */ | |
4439 | ||
b17dd571 GR |
4440 | if (!port->pd_capable) { |
4441 | /* | |
4442 | * If the partner is not PD capable, reset the port to | |
4443 | * trigger a role change. This can only work if a preferred | |
4444 | * role is configured, and if it matches the requested role. | |
4445 | */ | |
4446 | if (port->try_role == TYPEC_NO_PREFERRED_ROLE || | |
4447 | port->try_role == port->pwr_role) { | |
4448 | ret = -EINVAL; | |
4449 | goto port_unlock; | |
4450 | } | |
4451 | port->non_pd_role_swap = true; | |
4452 | tcpm_set_state(port, PORT_RESET, 0); | |
4453 | } else { | |
4454 | tcpm_set_state(port, DR_SWAP_SEND, 0); | |
4455 | } | |
4456 | ||
f0690a25 GR |
4457 | port->swap_status = 0; |
4458 | port->swap_pending = true; | |
4459 | reinit_completion(&port->swap_complete); | |
f0690a25 GR |
4460 | mutex_unlock(&port->lock); |
4461 | ||
9adf9f9e GR |
4462 | if (!wait_for_completion_timeout(&port->swap_complete, |
4463 | msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT))) | |
4464 | ret = -ETIMEDOUT; | |
4465 | else | |
4466 | ret = port->swap_status; | |
f0690a25 | 4467 | |
b17dd571 | 4468 | port->non_pd_role_swap = false; |
f0690a25 GR |
4469 | goto swap_unlock; |
4470 | ||
4471 | port_unlock: | |
4472 | mutex_unlock(&port->lock); | |
4473 | swap_unlock: | |
4474 | mutex_unlock(&port->swap_lock); | |
4475 | return ret; | |
4476 | } | |
4477 | ||
00ec21e5 | 4478 | static int tcpm_pr_set(struct typec_port *p, enum typec_role role) |
f0690a25 | 4479 | { |
00ec21e5 | 4480 | struct tcpm_port *port = typec_get_drvdata(p); |
f0690a25 GR |
4481 | int ret; |
4482 | ||
4483 | mutex_lock(&port->swap_lock); | |
4484 | mutex_lock(&port->lock); | |
4485 | ||
9b0ae699 | 4486 | if (port->port_type != TYPEC_PORT_DRP) { |
f0690a25 GR |
4487 | ret = -EINVAL; |
4488 | goto port_unlock; | |
4489 | } | |
4490 | if (port->state != SRC_READY && port->state != SNK_READY) { | |
4491 | ret = -EAGAIN; | |
4492 | goto port_unlock; | |
4493 | } | |
4494 | ||
4495 | if (role == port->pwr_role) { | |
4496 | ret = 0; | |
4497 | goto port_unlock; | |
4498 | } | |
4499 | ||
f0690a25 GR |
4500 | port->swap_status = 0; |
4501 | port->swap_pending = true; | |
4502 | reinit_completion(&port->swap_complete); | |
4503 | tcpm_set_state(port, PR_SWAP_SEND, 0); | |
4504 | mutex_unlock(&port->lock); | |
4505 | ||
9adf9f9e GR |
4506 | if (!wait_for_completion_timeout(&port->swap_complete, |
4507 | msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT))) | |
4508 | ret = -ETIMEDOUT; | |
4509 | else | |
4510 | ret = port->swap_status; | |
f0690a25 | 4511 | |
f0690a25 GR |
4512 | goto swap_unlock; |
4513 | ||
4514 | port_unlock: | |
4515 | mutex_unlock(&port->lock); | |
4516 | swap_unlock: | |
4517 | mutex_unlock(&port->swap_lock); | |
4518 | return ret; | |
4519 | } | |
4520 | ||
00ec21e5 | 4521 | static int tcpm_vconn_set(struct typec_port *p, enum typec_role role) |
f0690a25 | 4522 | { |
00ec21e5 | 4523 | struct tcpm_port *port = typec_get_drvdata(p); |
f0690a25 GR |
4524 | int ret; |
4525 | ||
4526 | mutex_lock(&port->swap_lock); | |
4527 | mutex_lock(&port->lock); | |
4528 | ||
4529 | if (port->state != SRC_READY && port->state != SNK_READY) { | |
4530 | ret = -EAGAIN; | |
4531 | goto port_unlock; | |
4532 | } | |
4533 | ||
4534 | if (role == port->vconn_role) { | |
4535 | ret = 0; | |
4536 | goto port_unlock; | |
4537 | } | |
4538 | ||
4539 | port->swap_status = 0; | |
4540 | port->swap_pending = true; | |
4541 | reinit_completion(&port->swap_complete); | |
4542 | tcpm_set_state(port, VCONN_SWAP_SEND, 0); | |
4543 | mutex_unlock(&port->lock); | |
4544 | ||
9adf9f9e GR |
4545 | if (!wait_for_completion_timeout(&port->swap_complete, |
4546 | msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT))) | |
4547 | ret = -ETIMEDOUT; | |
4548 | else | |
4549 | ret = port->swap_status; | |
f0690a25 | 4550 | |
f0690a25 GR |
4551 | goto swap_unlock; |
4552 | ||
4553 | port_unlock: | |
4554 | mutex_unlock(&port->lock); | |
4555 | swap_unlock: | |
4556 | mutex_unlock(&port->swap_lock); | |
4557 | return ret; | |
4558 | } | |
4559 | ||
00ec21e5 | 4560 | static int tcpm_try_role(struct typec_port *p, int role) |
f0690a25 | 4561 | { |
00ec21e5 | 4562 | struct tcpm_port *port = typec_get_drvdata(p); |
f0690a25 GR |
4563 | struct tcpc_dev *tcpc = port->tcpc; |
4564 | int ret = 0; | |
4565 | ||
4566 | mutex_lock(&port->lock); | |
4567 | if (tcpc->try_role) | |
4568 | ret = tcpc->try_role(tcpc, role); | |
a079973f | 4569 | if (!ret) |
f0690a25 GR |
4570 | port->try_role = role; |
4571 | port->try_src_count = 0; | |
4572 | port->try_snk_count = 0; | |
4573 | mutex_unlock(&port->lock); | |
4574 | ||
4575 | return ret; | |
4576 | } | |
4577 | ||
f2a8aa05 | 4578 | static int tcpm_pps_set_op_curr(struct tcpm_port *port, u16 op_curr) |
2eadc33f AT |
4579 | { |
4580 | unsigned int target_mw; | |
4581 | int ret; | |
4582 | ||
4583 | mutex_lock(&port->swap_lock); | |
4584 | mutex_lock(&port->lock); | |
4585 | ||
4586 | if (!port->pps_data.active) { | |
4587 | ret = -EOPNOTSUPP; | |
4588 | goto port_unlock; | |
4589 | } | |
4590 | ||
4591 | if (port->state != SNK_READY) { | |
4592 | ret = -EAGAIN; | |
4593 | goto port_unlock; | |
4594 | } | |
4595 | ||
4596 | if (op_curr > port->pps_data.max_curr) { | |
4597 | ret = -EINVAL; | |
4598 | goto port_unlock; | |
4599 | } | |
4600 | ||
4601 | target_mw = (op_curr * port->pps_data.out_volt) / 1000; | |
4602 | if (target_mw < port->operating_snk_mw) { | |
4603 | ret = -EINVAL; | |
4604 | goto port_unlock; | |
4605 | } | |
4606 | ||
554fab6d AT |
4607 | /* Round down operating current to align with PPS valid steps */ |
4608 | op_curr = op_curr - (op_curr % RDO_PROG_CURR_MA_STEP); | |
4609 | ||
2eadc33f AT |
4610 | reinit_completion(&port->pps_complete); |
4611 | port->pps_data.op_curr = op_curr; | |
4612 | port->pps_status = 0; | |
4613 | port->pps_pending = true; | |
4614 | tcpm_set_state(port, SNK_NEGOTIATE_PPS_CAPABILITIES, 0); | |
4615 | mutex_unlock(&port->lock); | |
4616 | ||
4617 | if (!wait_for_completion_timeout(&port->pps_complete, | |
4618 | msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT))) | |
4619 | ret = -ETIMEDOUT; | |
4620 | else | |
4621 | ret = port->pps_status; | |
4622 | ||
4623 | goto swap_unlock; | |
4624 | ||
4625 | port_unlock: | |
4626 | mutex_unlock(&port->lock); | |
4627 | swap_unlock: | |
4628 | mutex_unlock(&port->swap_lock); | |
4629 | ||
4630 | return ret; | |
4631 | } | |
4632 | ||
f2a8aa05 | 4633 | static int tcpm_pps_set_out_volt(struct tcpm_port *port, u16 out_volt) |
2eadc33f AT |
4634 | { |
4635 | unsigned int target_mw; | |
4636 | int ret; | |
4637 | ||
4638 | mutex_lock(&port->swap_lock); | |
4639 | mutex_lock(&port->lock); | |
4640 | ||
4641 | if (!port->pps_data.active) { | |
4642 | ret = -EOPNOTSUPP; | |
4643 | goto port_unlock; | |
4644 | } | |
4645 | ||
4646 | if (port->state != SNK_READY) { | |
4647 | ret = -EAGAIN; | |
4648 | goto port_unlock; | |
4649 | } | |
4650 | ||
4651 | if (out_volt < port->pps_data.min_volt || | |
4652 | out_volt > port->pps_data.max_volt) { | |
4653 | ret = -EINVAL; | |
4654 | goto port_unlock; | |
4655 | } | |
4656 | ||
4657 | target_mw = (port->pps_data.op_curr * out_volt) / 1000; | |
4658 | if (target_mw < port->operating_snk_mw) { | |
4659 | ret = -EINVAL; | |
4660 | goto port_unlock; | |
4661 | } | |
4662 | ||
554fab6d AT |
4663 | /* Round down output voltage to align with PPS valid steps */ |
4664 | out_volt = out_volt - (out_volt % RDO_PROG_VOLT_MV_STEP); | |
4665 | ||
2eadc33f AT |
4666 | reinit_completion(&port->pps_complete); |
4667 | port->pps_data.out_volt = out_volt; | |
4668 | port->pps_status = 0; | |
4669 | port->pps_pending = true; | |
4670 | tcpm_set_state(port, SNK_NEGOTIATE_PPS_CAPABILITIES, 0); | |
4671 | mutex_unlock(&port->lock); | |
4672 | ||
4673 | if (!wait_for_completion_timeout(&port->pps_complete, | |
4674 | msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT))) | |
4675 | ret = -ETIMEDOUT; | |
4676 | else | |
4677 | ret = port->pps_status; | |
4678 | ||
4679 | goto swap_unlock; | |
4680 | ||
4681 | port_unlock: | |
4682 | mutex_unlock(&port->lock); | |
4683 | swap_unlock: | |
4684 | mutex_unlock(&port->swap_lock); | |
4685 | ||
4686 | return ret; | |
4687 | } | |
4688 | ||
f2a8aa05 | 4689 | static int tcpm_pps_activate(struct tcpm_port *port, bool activate) |
2eadc33f AT |
4690 | { |
4691 | int ret = 0; | |
4692 | ||
4693 | mutex_lock(&port->swap_lock); | |
4694 | mutex_lock(&port->lock); | |
4695 | ||
4696 | if (!port->pps_data.supported) { | |
4697 | ret = -EOPNOTSUPP; | |
4698 | goto port_unlock; | |
4699 | } | |
4700 | ||
4701 | /* Trying to deactivate PPS when already deactivated so just bail */ | |
4702 | if (!port->pps_data.active && !activate) | |
4703 | goto port_unlock; | |
4704 | ||
4705 | if (port->state != SNK_READY) { | |
4706 | ret = -EAGAIN; | |
4707 | goto port_unlock; | |
4708 | } | |
4709 | ||
4710 | reinit_completion(&port->pps_complete); | |
4711 | port->pps_status = 0; | |
4712 | port->pps_pending = true; | |
4713 | ||
4714 | /* Trigger PPS request or move back to standard PDO contract */ | |
4715 | if (activate) { | |
4716 | port->pps_data.out_volt = port->supply_voltage; | |
4717 | port->pps_data.op_curr = port->current_limit; | |
4718 | tcpm_set_state(port, SNK_NEGOTIATE_PPS_CAPABILITIES, 0); | |
4719 | } else { | |
4720 | tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0); | |
4721 | } | |
4722 | mutex_unlock(&port->lock); | |
4723 | ||
4724 | if (!wait_for_completion_timeout(&port->pps_complete, | |
4725 | msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT))) | |
4726 | ret = -ETIMEDOUT; | |
4727 | else | |
4728 | ret = port->pps_status; | |
4729 | ||
4730 | goto swap_unlock; | |
4731 | ||
4732 | port_unlock: | |
4733 | mutex_unlock(&port->lock); | |
4734 | swap_unlock: | |
4735 | mutex_unlock(&port->swap_lock); | |
4736 | ||
4737 | return ret; | |
4738 | } | |
4739 | ||
f0690a25 GR |
4740 | static void tcpm_init(struct tcpm_port *port) |
4741 | { | |
4742 | enum typec_cc_status cc1, cc2; | |
4743 | ||
4744 | port->tcpc->init(port->tcpc); | |
4745 | ||
4746 | tcpm_reset_port(port); | |
4747 | ||
4748 | /* | |
4749 | * XXX | |
4750 | * Should possibly wait for VBUS to settle if it was enabled locally | |
4751 | * since tcpm_reset_port() will disable VBUS. | |
4752 | */ | |
4753 | port->vbus_present = port->tcpc->get_vbus(port->tcpc); | |
4754 | if (port->vbus_present) | |
4755 | port->vbus_never_low = true; | |
4756 | ||
4757 | tcpm_set_state(port, tcpm_default_state(port), 0); | |
4758 | ||
4759 | if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0) | |
4760 | _tcpm_cc_change(port, cc1, cc2); | |
4761 | ||
4762 | /* | |
4763 | * Some adapters need a clean slate at startup, and won't recover | |
4764 | * otherwise. So do not try to be fancy and force a clean disconnect. | |
4765 | */ | |
b17dd571 | 4766 | tcpm_set_state(port, PORT_RESET, 0); |
f0690a25 GR |
4767 | } |
4768 | ||
00ec21e5 | 4769 | static int tcpm_port_type_set(struct typec_port *p, enum typec_port_type type) |
9b0ae699 | 4770 | { |
00ec21e5 | 4771 | struct tcpm_port *port = typec_get_drvdata(p); |
9b0ae699 BJS |
4772 | |
4773 | mutex_lock(&port->lock); | |
4774 | if (type == port->port_type) | |
4775 | goto port_unlock; | |
4776 | ||
4777 | port->port_type = type; | |
4778 | ||
4779 | if (!port->connected) { | |
4780 | tcpm_set_state(port, PORT_RESET, 0); | |
ceeb1625 | 4781 | } else if (type == TYPEC_PORT_SNK) { |
9b0ae699 BJS |
4782 | if (!(port->pwr_role == TYPEC_SINK && |
4783 | port->data_role == TYPEC_DEVICE)) | |
4784 | tcpm_set_state(port, PORT_RESET, 0); | |
ceeb1625 | 4785 | } else if (type == TYPEC_PORT_SRC) { |
9b0ae699 BJS |
4786 | if (!(port->pwr_role == TYPEC_SOURCE && |
4787 | port->data_role == TYPEC_HOST)) | |
4788 | tcpm_set_state(port, PORT_RESET, 0); | |
4789 | } | |
4790 | ||
4791 | port_unlock: | |
4792 | mutex_unlock(&port->lock); | |
4793 | return 0; | |
4794 | } | |
4795 | ||
00ec21e5 HK |
4796 | static const struct typec_operations tcpm_ops = { |
4797 | .try_role = tcpm_try_role, | |
4798 | .dr_set = tcpm_dr_set, | |
4799 | .pr_set = tcpm_pr_set, | |
4800 | .vconn_set = tcpm_vconn_set, | |
4801 | .port_type_set = tcpm_port_type_set | |
4802 | }; | |
4803 | ||
f0690a25 GR |
4804 | void tcpm_tcpc_reset(struct tcpm_port *port) |
4805 | { | |
4806 | mutex_lock(&port->lock); | |
4807 | /* XXX: Maintain PD connection if possible? */ | |
4808 | tcpm_init(port); | |
4809 | mutex_unlock(&port->lock); | |
4810 | } | |
4811 | EXPORT_SYMBOL_GPL(tcpm_tcpc_reset); | |
4812 | ||
96232cbc LJ |
4813 | static int tcpm_fw_get_caps(struct tcpm_port *port, |
4814 | struct fwnode_handle *fwnode) | |
4815 | { | |
4816 | const char *cap_str; | |
4817 | int ret; | |
8dc4bd07 | 4818 | u32 mw, frs_current; |
96232cbc LJ |
4819 | |
4820 | if (!fwnode) | |
4821 | return -EINVAL; | |
4822 | ||
4823 | /* USB data support is optional */ | |
4824 | ret = fwnode_property_read_string(fwnode, "data-role", &cap_str); | |
4825 | if (ret == 0) { | |
7101949f DC |
4826 | ret = typec_find_port_data_role(cap_str); |
4827 | if (ret < 0) | |
4828 | return ret; | |
4829 | port->typec_caps.data = ret; | |
96232cbc LJ |
4830 | } |
4831 | ||
4832 | ret = fwnode_property_read_string(fwnode, "power-role", &cap_str); | |
4833 | if (ret < 0) | |
4834 | return ret; | |
4835 | ||
7101949f DC |
4836 | ret = typec_find_port_power_role(cap_str); |
4837 | if (ret < 0) | |
4838 | return ret; | |
4839 | port->typec_caps.type = ret; | |
96232cbc LJ |
4840 | port->port_type = port->typec_caps.type; |
4841 | ||
4842 | if (port->port_type == TYPEC_PORT_SNK) | |
4843 | goto sink; | |
4844 | ||
4845 | /* Get source pdos */ | |
bc751492 | 4846 | ret = fwnode_property_count_u32(fwnode, "source-pdos"); |
96232cbc LJ |
4847 | if (ret <= 0) |
4848 | return -EINVAL; | |
4849 | ||
4850 | port->nr_src_pdo = min(ret, PDO_MAX_OBJECTS); | |
4851 | ret = fwnode_property_read_u32_array(fwnode, "source-pdos", | |
4852 | port->src_pdo, port->nr_src_pdo); | |
4853 | if ((ret < 0) || tcpm_validate_caps(port, port->src_pdo, | |
4854 | port->nr_src_pdo)) | |
4855 | return -EINVAL; | |
4856 | ||
4857 | if (port->port_type == TYPEC_PORT_SRC) | |
4858 | return 0; | |
4859 | ||
4860 | /* Get the preferred power role for DRP */ | |
4861 | ret = fwnode_property_read_string(fwnode, "try-power-role", &cap_str); | |
4862 | if (ret < 0) | |
4863 | return ret; | |
4864 | ||
4865 | port->typec_caps.prefer_role = typec_find_power_role(cap_str); | |
4866 | if (port->typec_caps.prefer_role < 0) | |
4867 | return -EINVAL; | |
4868 | sink: | |
4869 | /* Get sink pdos */ | |
bc751492 | 4870 | ret = fwnode_property_count_u32(fwnode, "sink-pdos"); |
96232cbc LJ |
4871 | if (ret <= 0) |
4872 | return -EINVAL; | |
4873 | ||
4874 | port->nr_snk_pdo = min(ret, PDO_MAX_OBJECTS); | |
4875 | ret = fwnode_property_read_u32_array(fwnode, "sink-pdos", | |
4876 | port->snk_pdo, port->nr_snk_pdo); | |
4877 | if ((ret < 0) || tcpm_validate_caps(port, port->snk_pdo, | |
4878 | port->nr_snk_pdo)) | |
4879 | return -EINVAL; | |
4880 | ||
4881 | if (fwnode_property_read_u32(fwnode, "op-sink-microwatt", &mw) < 0) | |
4882 | return -EINVAL; | |
4883 | port->operating_snk_mw = mw / 1000; | |
4884 | ||
23b5f732 BJS |
4885 | port->self_powered = fwnode_property_read_bool(fwnode, "self-powered"); |
4886 | ||
8dc4bd07 BJS |
4887 | /* FRS can only be supported byb DRP ports */ |
4888 | if (port->port_type == TYPEC_PORT_DRP) { | |
8115240c BJS |
4889 | ret = fwnode_property_read_u32(fwnode, "new-source-frs-typec-current", |
4890 | &frs_current); | |
8dc4bd07 | 4891 | if (ret >= 0 && frs_current <= FRS_5V_3A) |
8115240c | 4892 | port->new_source_frs_current = frs_current; |
8dc4bd07 BJS |
4893 | } |
4894 | ||
96232cbc LJ |
4895 | return 0; |
4896 | } | |
4897 | ||
f2a8aa05 AT |
4898 | /* Power Supply access to expose source power information */ |
4899 | enum tcpm_psy_online_states { | |
4900 | TCPM_PSY_OFFLINE = 0, | |
4901 | TCPM_PSY_FIXED_ONLINE, | |
4902 | TCPM_PSY_PROG_ONLINE, | |
4903 | }; | |
4904 | ||
4905 | static enum power_supply_property tcpm_psy_props[] = { | |
4906 | POWER_SUPPLY_PROP_USB_TYPE, | |
4907 | POWER_SUPPLY_PROP_ONLINE, | |
4908 | POWER_SUPPLY_PROP_VOLTAGE_MIN, | |
4909 | POWER_SUPPLY_PROP_VOLTAGE_MAX, | |
4910 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | |
4911 | POWER_SUPPLY_PROP_CURRENT_MAX, | |
4912 | POWER_SUPPLY_PROP_CURRENT_NOW, | |
4913 | }; | |
4914 | ||
4915 | static int tcpm_psy_get_online(struct tcpm_port *port, | |
4916 | union power_supply_propval *val) | |
4917 | { | |
4918 | if (port->vbus_charge) { | |
4919 | if (port->pps_data.active) | |
4920 | val->intval = TCPM_PSY_PROG_ONLINE; | |
4921 | else | |
4922 | val->intval = TCPM_PSY_FIXED_ONLINE; | |
4923 | } else { | |
4924 | val->intval = TCPM_PSY_OFFLINE; | |
4925 | } | |
4926 | ||
4927 | return 0; | |
4928 | } | |
4929 | ||
4930 | static int tcpm_psy_get_voltage_min(struct tcpm_port *port, | |
4931 | union power_supply_propval *val) | |
4932 | { | |
4933 | if (port->pps_data.active) | |
4934 | val->intval = port->pps_data.min_volt * 1000; | |
4935 | else | |
4936 | val->intval = port->supply_voltage * 1000; | |
4937 | ||
4938 | return 0; | |
4939 | } | |
4940 | ||
4941 | static int tcpm_psy_get_voltage_max(struct tcpm_port *port, | |
4942 | union power_supply_propval *val) | |
4943 | { | |
4944 | if (port->pps_data.active) | |
4945 | val->intval = port->pps_data.max_volt * 1000; | |
4946 | else | |
4947 | val->intval = port->supply_voltage * 1000; | |
4948 | ||
4949 | return 0; | |
4950 | } | |
4951 | ||
4952 | static int tcpm_psy_get_voltage_now(struct tcpm_port *port, | |
4953 | union power_supply_propval *val) | |
4954 | { | |
4955 | val->intval = port->supply_voltage * 1000; | |
4956 | ||
4957 | return 0; | |
4958 | } | |
4959 | ||
4960 | static int tcpm_psy_get_current_max(struct tcpm_port *port, | |
4961 | union power_supply_propval *val) | |
4962 | { | |
4963 | if (port->pps_data.active) | |
4964 | val->intval = port->pps_data.max_curr * 1000; | |
4965 | else | |
4966 | val->intval = port->current_limit * 1000; | |
4967 | ||
4968 | return 0; | |
4969 | } | |
4970 | ||
4971 | static int tcpm_psy_get_current_now(struct tcpm_port *port, | |
4972 | union power_supply_propval *val) | |
4973 | { | |
4974 | val->intval = port->current_limit * 1000; | |
4975 | ||
4976 | return 0; | |
4977 | } | |
4978 | ||
4979 | static int tcpm_psy_get_prop(struct power_supply *psy, | |
4980 | enum power_supply_property psp, | |
4981 | union power_supply_propval *val) | |
4982 | { | |
4983 | struct tcpm_port *port = power_supply_get_drvdata(psy); | |
4984 | int ret = 0; | |
4985 | ||
4986 | switch (psp) { | |
4987 | case POWER_SUPPLY_PROP_USB_TYPE: | |
4988 | val->intval = port->usb_type; | |
4989 | break; | |
4990 | case POWER_SUPPLY_PROP_ONLINE: | |
4991 | ret = tcpm_psy_get_online(port, val); | |
4992 | break; | |
4993 | case POWER_SUPPLY_PROP_VOLTAGE_MIN: | |
4994 | ret = tcpm_psy_get_voltage_min(port, val); | |
4995 | break; | |
4996 | case POWER_SUPPLY_PROP_VOLTAGE_MAX: | |
4997 | ret = tcpm_psy_get_voltage_max(port, val); | |
4998 | break; | |
4999 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: | |
5000 | ret = tcpm_psy_get_voltage_now(port, val); | |
5001 | break; | |
5002 | case POWER_SUPPLY_PROP_CURRENT_MAX: | |
5003 | ret = tcpm_psy_get_current_max(port, val); | |
5004 | break; | |
5005 | case POWER_SUPPLY_PROP_CURRENT_NOW: | |
5006 | ret = tcpm_psy_get_current_now(port, val); | |
5007 | break; | |
5008 | default: | |
5009 | ret = -EINVAL; | |
5010 | break; | |
5011 | } | |
5012 | ||
5013 | return ret; | |
5014 | } | |
5015 | ||
5016 | static int tcpm_psy_set_online(struct tcpm_port *port, | |
5017 | const union power_supply_propval *val) | |
5018 | { | |
5019 | int ret; | |
5020 | ||
5021 | switch (val->intval) { | |
5022 | case TCPM_PSY_FIXED_ONLINE: | |
5023 | ret = tcpm_pps_activate(port, false); | |
5024 | break; | |
5025 | case TCPM_PSY_PROG_ONLINE: | |
5026 | ret = tcpm_pps_activate(port, true); | |
5027 | break; | |
5028 | default: | |
5029 | ret = -EINVAL; | |
5030 | break; | |
5031 | } | |
5032 | ||
5033 | return ret; | |
5034 | } | |
5035 | ||
5036 | static int tcpm_psy_set_prop(struct power_supply *psy, | |
5037 | enum power_supply_property psp, | |
5038 | const union power_supply_propval *val) | |
5039 | { | |
5040 | struct tcpm_port *port = power_supply_get_drvdata(psy); | |
5041 | int ret; | |
5042 | ||
5043 | switch (psp) { | |
5044 | case POWER_SUPPLY_PROP_ONLINE: | |
5045 | ret = tcpm_psy_set_online(port, val); | |
5046 | break; | |
5047 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: | |
5048 | if (val->intval < port->pps_data.min_volt * 1000 || | |
5049 | val->intval > port->pps_data.max_volt * 1000) | |
5050 | ret = -EINVAL; | |
5051 | else | |
5052 | ret = tcpm_pps_set_out_volt(port, val->intval / 1000); | |
5053 | break; | |
5054 | case POWER_SUPPLY_PROP_CURRENT_NOW: | |
5055 | if (val->intval > port->pps_data.max_curr * 1000) | |
5056 | ret = -EINVAL; | |
5057 | else | |
5058 | ret = tcpm_pps_set_op_curr(port, val->intval / 1000); | |
5059 | break; | |
5060 | default: | |
5061 | ret = -EINVAL; | |
5062 | break; | |
5063 | } | |
5064 | ||
5065 | return ret; | |
5066 | } | |
5067 | ||
5068 | static int tcpm_psy_prop_writeable(struct power_supply *psy, | |
5069 | enum power_supply_property psp) | |
5070 | { | |
5071 | switch (psp) { | |
5072 | case POWER_SUPPLY_PROP_ONLINE: | |
5073 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: | |
5074 | case POWER_SUPPLY_PROP_CURRENT_NOW: | |
5075 | return 1; | |
5076 | default: | |
5077 | return 0; | |
5078 | } | |
5079 | } | |
5080 | ||
5081 | static enum power_supply_usb_type tcpm_psy_usb_types[] = { | |
5082 | POWER_SUPPLY_USB_TYPE_C, | |
5083 | POWER_SUPPLY_USB_TYPE_PD, | |
5084 | POWER_SUPPLY_USB_TYPE_PD_PPS, | |
5085 | }; | |
5086 | ||
5087 | static const char *tcpm_psy_name_prefix = "tcpm-source-psy-"; | |
5088 | ||
5089 | static int devm_tcpm_psy_register(struct tcpm_port *port) | |
5090 | { | |
5091 | struct power_supply_config psy_cfg = {}; | |
5092 | const char *port_dev_name = dev_name(port->dev); | |
5093 | size_t psy_name_len = strlen(tcpm_psy_name_prefix) + | |
5094 | strlen(port_dev_name) + 1; | |
5095 | char *psy_name; | |
5096 | ||
5097 | psy_cfg.drv_data = port; | |
c97a8cc1 | 5098 | psy_cfg.fwnode = dev_fwnode(port->dev); |
f2a8aa05 AT |
5099 | psy_name = devm_kzalloc(port->dev, psy_name_len, GFP_KERNEL); |
5100 | if (!psy_name) | |
5101 | return -ENOMEM; | |
5102 | ||
5103 | snprintf(psy_name, psy_name_len, "%s%s", tcpm_psy_name_prefix, | |
5104 | port_dev_name); | |
5105 | port->psy_desc.name = psy_name; | |
5106 | port->psy_desc.type = POWER_SUPPLY_TYPE_USB, | |
5107 | port->psy_desc.usb_types = tcpm_psy_usb_types; | |
5108 | port->psy_desc.num_usb_types = ARRAY_SIZE(tcpm_psy_usb_types); | |
5109 | port->psy_desc.properties = tcpm_psy_props, | |
5110 | port->psy_desc.num_properties = ARRAY_SIZE(tcpm_psy_props), | |
5111 | port->psy_desc.get_property = tcpm_psy_get_prop, | |
5112 | port->psy_desc.set_property = tcpm_psy_set_prop, | |
5113 | port->psy_desc.property_is_writeable = tcpm_psy_prop_writeable, | |
5114 | ||
5115 | port->usb_type = POWER_SUPPLY_USB_TYPE_C; | |
5116 | ||
5117 | port->psy = devm_power_supply_register(port->dev, &port->psy_desc, | |
5118 | &psy_cfg); | |
5119 | ||
5120 | return PTR_ERR_OR_ZERO(port->psy); | |
5121 | } | |
5122 | ||
3ed8e1c2 BJS |
5123 | static enum hrtimer_restart state_machine_timer_handler(struct hrtimer *timer) |
5124 | { | |
5125 | struct tcpm_port *port = container_of(timer, struct tcpm_port, state_machine_timer); | |
5126 | ||
5127 | kthread_queue_work(port->wq, &port->state_machine); | |
5128 | return HRTIMER_NORESTART; | |
5129 | } | |
5130 | ||
5131 | static enum hrtimer_restart vdm_state_machine_timer_handler(struct hrtimer *timer) | |
5132 | { | |
5133 | struct tcpm_port *port = container_of(timer, struct tcpm_port, vdm_state_machine_timer); | |
5134 | ||
5135 | kthread_queue_work(port->wq, &port->vdm_state_machine); | |
5136 | return HRTIMER_NORESTART; | |
5137 | } | |
5138 | ||
8dc4bd07 BJS |
5139 | static enum hrtimer_restart enable_frs_timer_handler(struct hrtimer *timer) |
5140 | { | |
5141 | struct tcpm_port *port = container_of(timer, struct tcpm_port, enable_frs_timer); | |
5142 | ||
5143 | kthread_queue_work(port->wq, &port->enable_frs); | |
5144 | return HRTIMER_NORESTART; | |
5145 | } | |
5146 | ||
f0690a25 GR |
5147 | struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc) |
5148 | { | |
5149 | struct tcpm_port *port; | |
a079973f | 5150 | int err; |
f0690a25 | 5151 | |
96232cbc | 5152 | if (!dev || !tcpc || |
f0690a25 GR |
5153 | !tcpc->get_vbus || !tcpc->set_cc || !tcpc->get_cc || |
5154 | !tcpc->set_polarity || !tcpc->set_vconn || !tcpc->set_vbus || | |
5155 | !tcpc->set_pd_rx || !tcpc->set_roles || !tcpc->pd_transmit) | |
5156 | return ERR_PTR(-EINVAL); | |
5157 | ||
5158 | port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL); | |
5159 | if (!port) | |
5160 | return ERR_PTR(-ENOMEM); | |
5161 | ||
5162 | port->dev = dev; | |
5163 | port->tcpc = tcpc; | |
5164 | ||
5165 | mutex_init(&port->lock); | |
5166 | mutex_init(&port->swap_lock); | |
5167 | ||
3ed8e1c2 BJS |
5168 | port->wq = kthread_create_worker(0, dev_name(dev)); |
5169 | if (IS_ERR(port->wq)) | |
5170 | return ERR_CAST(port->wq); | |
5171 | sched_set_fifo(port->wq->task); | |
5172 | ||
5173 | kthread_init_work(&port->state_machine, tcpm_state_machine_work); | |
5174 | kthread_init_work(&port->vdm_state_machine, vdm_state_machine_work); | |
5175 | kthread_init_work(&port->event_work, tcpm_pd_event_handler); | |
8dc4bd07 | 5176 | kthread_init_work(&port->enable_frs, tcpm_enable_frs_work); |
3ed8e1c2 BJS |
5177 | hrtimer_init(&port->state_machine_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
5178 | port->state_machine_timer.function = state_machine_timer_handler; | |
5179 | hrtimer_init(&port->vdm_state_machine_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
5180 | port->vdm_state_machine_timer.function = vdm_state_machine_timer_handler; | |
8dc4bd07 BJS |
5181 | hrtimer_init(&port->enable_frs_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
5182 | port->enable_frs_timer.function = enable_frs_timer_handler; | |
f0690a25 GR |
5183 | |
5184 | spin_lock_init(&port->pd_event_lock); | |
5185 | ||
5186 | init_completion(&port->tx_complete); | |
5187 | init_completion(&port->swap_complete); | |
2eadc33f | 5188 | init_completion(&port->pps_complete); |
5007e1b5 | 5189 | tcpm_debugfs_init(port); |
f0690a25 | 5190 | |
96232cbc | 5191 | err = tcpm_fw_get_caps(port, tcpc->fwnode); |
96232cbc | 5192 | if (err < 0) |
5007e1b5 | 5193 | goto out_destroy_wq; |
96232cbc | 5194 | |
a079973f | 5195 | port->try_role = port->typec_caps.prefer_role; |
f0690a25 | 5196 | |
5e85a04c | 5197 | port->typec_caps.fwnode = tcpc->fwnode; |
f0690a25 | 5198 | port->typec_caps.revision = 0x0120; /* Type-C spec release 1.2 */ |
2eadc33f | 5199 | port->typec_caps.pd_revision = 0x0300; /* USB-PD spec release 3.0 */ |
00ec21e5 HK |
5200 | port->typec_caps.driver_data = port; |
5201 | port->typec_caps.ops = &tcpm_ops; | |
75f81a7f | 5202 | port->typec_caps.orientation_aware = 1; |
f0690a25 GR |
5203 | |
5204 | port->partner_desc.identity = &port->partner_ident; | |
96232cbc | 5205 | port->port_type = port->typec_caps.type; |
f0690a25 | 5206 | |
2000016c HG |
5207 | port->role_sw = usb_role_switch_get(port->dev); |
5208 | if (IS_ERR(port->role_sw)) { | |
5209 | err = PTR_ERR(port->role_sw); | |
5210 | goto out_destroy_wq; | |
5211 | } | |
5212 | ||
f2a8aa05 AT |
5213 | err = devm_tcpm_psy_register(port); |
5214 | if (err) | |
9997ab35 | 5215 | goto out_role_sw_put; |
f2a8aa05 | 5216 | |
f0690a25 | 5217 | port->typec_port = typec_register_port(port->dev, &port->typec_caps); |
cf6e06cd HK |
5218 | if (IS_ERR(port->typec_port)) { |
5219 | err = PTR_ERR(port->typec_port); | |
9997ab35 | 5220 | goto out_role_sw_put; |
f0690a25 GR |
5221 | } |
5222 | ||
f0690a25 GR |
5223 | mutex_lock(&port->lock); |
5224 | tcpm_init(port); | |
5225 | mutex_unlock(&port->lock); | |
5226 | ||
5227 | tcpm_log(port, "%s: registered", dev_name(dev)); | |
5228 | return port; | |
5229 | ||
9997ab35 | 5230 | out_role_sw_put: |
2000016c | 5231 | usb_role_switch_put(port->role_sw); |
9997ab35 JL |
5232 | out_destroy_wq: |
5233 | tcpm_debugfs_exit(port); | |
3ed8e1c2 | 5234 | kthread_destroy_worker(port->wq); |
f0690a25 GR |
5235 | return ERR_PTR(err); |
5236 | } | |
5237 | EXPORT_SYMBOL_GPL(tcpm_register_port); | |
5238 | ||
5239 | void tcpm_unregister_port(struct tcpm_port *port) | |
5240 | { | |
5241 | int i; | |
5242 | ||
a6d5d230 | 5243 | tcpm_reset_port(port); |
f0690a25 GR |
5244 | for (i = 0; i < ARRAY_SIZE(port->port_altmode); i++) |
5245 | typec_unregister_altmode(port->port_altmode[i]); | |
5246 | typec_unregister_port(port->typec_port); | |
5671a460 | 5247 | usb_role_switch_put(port->role_sw); |
f0690a25 | 5248 | tcpm_debugfs_exit(port); |
3ed8e1c2 | 5249 | kthread_destroy_worker(port->wq); |
f0690a25 GR |
5250 | } |
5251 | EXPORT_SYMBOL_GPL(tcpm_unregister_port); | |
5252 | ||
5253 | MODULE_AUTHOR("Guenter Roeck <groeck@chromium.org>"); | |
5254 | MODULE_DESCRIPTION("USB Type-C Port Manager"); | |
5255 | MODULE_LICENSE("GPL"); |