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), \ | |
0908c5ac KT |
79 | S(SRC_SOFT_RESET_WAIT_SNK_TX), \ |
80 | S(SNK_SOFT_RESET), \ | |
f0690a25 GR |
81 | S(SOFT_RESET_SEND), \ |
82 | \ | |
83 | S(DR_SWAP_ACCEPT), \ | |
84 | S(DR_SWAP_SEND), \ | |
85 | S(DR_SWAP_SEND_TIMEOUT), \ | |
86 | S(DR_SWAP_CANCEL), \ | |
87 | S(DR_SWAP_CHANGE_DR), \ | |
88 | \ | |
89 | S(PR_SWAP_ACCEPT), \ | |
90 | S(PR_SWAP_SEND), \ | |
91 | S(PR_SWAP_SEND_TIMEOUT), \ | |
92 | S(PR_SWAP_CANCEL), \ | |
93 | S(PR_SWAP_START), \ | |
94 | S(PR_SWAP_SRC_SNK_TRANSITION_OFF), \ | |
95 | S(PR_SWAP_SRC_SNK_SOURCE_OFF), \ | |
b965b631 | 96 | S(PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED), \ |
f0690a25 GR |
97 | S(PR_SWAP_SRC_SNK_SINK_ON), \ |
98 | S(PR_SWAP_SNK_SRC_SINK_OFF), \ | |
99 | S(PR_SWAP_SNK_SRC_SOURCE_ON), \ | |
b965b631 | 100 | S(PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP), \ |
f0690a25 GR |
101 | \ |
102 | S(VCONN_SWAP_ACCEPT), \ | |
103 | S(VCONN_SWAP_SEND), \ | |
104 | S(VCONN_SWAP_SEND_TIMEOUT), \ | |
105 | S(VCONN_SWAP_CANCEL), \ | |
106 | S(VCONN_SWAP_START), \ | |
107 | S(VCONN_SWAP_WAIT_FOR_VCONN), \ | |
108 | S(VCONN_SWAP_TURN_ON_VCONN), \ | |
109 | S(VCONN_SWAP_TURN_OFF_VCONN), \ | |
110 | \ | |
8dc4bd07 BJS |
111 | S(FR_SWAP_SEND), \ |
112 | S(FR_SWAP_SEND_TIMEOUT), \ | |
113 | S(FR_SWAP_SNK_SRC_TRANSITION_TO_OFF), \ | |
114 | S(FR_SWAP_SNK_SRC_NEW_SINK_READY), \ | |
115 | S(FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED), \ | |
116 | S(FR_SWAP_CANCEL), \ | |
117 | \ | |
f0690a25 GR |
118 | S(SNK_TRY), \ |
119 | S(SNK_TRY_WAIT), \ | |
a0a3e04e BJS |
120 | S(SNK_TRY_WAIT_DEBOUNCE), \ |
121 | S(SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS), \ | |
f0690a25 | 122 | S(SRC_TRYWAIT), \ |
02d5be46 | 123 | S(SRC_TRYWAIT_DEBOUNCE), \ |
f0690a25 GR |
124 | S(SRC_TRYWAIT_UNATTACHED), \ |
125 | \ | |
126 | S(SRC_TRY), \ | |
131c7d12 | 127 | S(SRC_TRY_WAIT), \ |
f0690a25 GR |
128 | S(SRC_TRY_DEBOUNCE), \ |
129 | S(SNK_TRYWAIT), \ | |
130 | S(SNK_TRYWAIT_DEBOUNCE), \ | |
131 | S(SNK_TRYWAIT_VBUS), \ | |
132 | S(BIST_RX), \ | |
133 | \ | |
64f7c494 AT |
134 | S(GET_STATUS_SEND), \ |
135 | S(GET_STATUS_SEND_TIMEOUT), \ | |
136 | S(GET_PPS_STATUS_SEND), \ | |
137 | S(GET_PPS_STATUS_SEND_TIMEOUT), \ | |
138 | \ | |
8dc4bd07 BJS |
139 | S(GET_SINK_CAP), \ |
140 | S(GET_SINK_CAP_TIMEOUT), \ | |
141 | \ | |
f0690a25 | 142 | S(ERROR_RECOVERY), \ |
b17dd571 | 143 | S(PORT_RESET), \ |
0908c5ac KT |
144 | S(PORT_RESET_WAIT_OFF), \ |
145 | \ | |
8dea75e1 KT |
146 | S(AMS_START), \ |
147 | S(CHUNK_NOT_SUPP) | |
0908c5ac KT |
148 | |
149 | #define FOREACH_AMS(S) \ | |
150 | S(NONE_AMS), \ | |
151 | S(POWER_NEGOTIATION), \ | |
152 | S(GOTOMIN), \ | |
153 | S(SOFT_RESET_AMS), \ | |
154 | S(HARD_RESET), \ | |
155 | S(CABLE_RESET), \ | |
156 | S(GET_SOURCE_CAPABILITIES), \ | |
157 | S(GET_SINK_CAPABILITIES), \ | |
158 | S(POWER_ROLE_SWAP), \ | |
159 | S(FAST_ROLE_SWAP), \ | |
160 | S(DATA_ROLE_SWAP), \ | |
161 | S(VCONN_SWAP), \ | |
162 | S(SOURCE_ALERT), \ | |
163 | S(GETTING_SOURCE_EXTENDED_CAPABILITIES),\ | |
164 | S(GETTING_SOURCE_SINK_STATUS), \ | |
165 | S(GETTING_BATTERY_CAPABILITIES), \ | |
166 | S(GETTING_BATTERY_STATUS), \ | |
167 | S(GETTING_MANUFACTURER_INFORMATION), \ | |
168 | S(SECURITY), \ | |
169 | S(FIRMWARE_UPDATE), \ | |
170 | S(DISCOVER_IDENTITY), \ | |
171 | S(SOURCE_STARTUP_CABLE_PLUG_DISCOVER_IDENTITY), \ | |
172 | S(DISCOVER_SVIDS), \ | |
173 | S(DISCOVER_MODES), \ | |
174 | S(DFP_TO_UFP_ENTER_MODE), \ | |
175 | S(DFP_TO_UFP_EXIT_MODE), \ | |
176 | S(DFP_TO_CABLE_PLUG_ENTER_MODE), \ | |
177 | S(DFP_TO_CABLE_PLUG_EXIT_MODE), \ | |
178 | S(ATTENTION), \ | |
179 | S(BIST), \ | |
180 | S(UNSTRUCTURED_VDMS), \ | |
181 | S(STRUCTURED_VDMS), \ | |
182 | S(COUNTRY_INFO), \ | |
183 | S(COUNTRY_CODES) | |
f0690a25 GR |
184 | |
185 | #define GENERATE_ENUM(e) e | |
186 | #define GENERATE_STRING(s) #s | |
187 | ||
188 | enum tcpm_state { | |
189 | FOREACH_STATE(GENERATE_ENUM) | |
190 | }; | |
191 | ||
192 | static const char * const tcpm_states[] = { | |
193 | FOREACH_STATE(GENERATE_STRING) | |
194 | }; | |
195 | ||
0908c5ac KT |
196 | enum tcpm_ams { |
197 | FOREACH_AMS(GENERATE_ENUM) | |
198 | }; | |
199 | ||
200 | static const char * const tcpm_ams_str[] = { | |
201 | FOREACH_AMS(GENERATE_STRING) | |
202 | }; | |
203 | ||
f0690a25 GR |
204 | enum vdm_states { |
205 | VDM_STATE_ERR_BUSY = -3, | |
206 | VDM_STATE_ERR_SEND = -2, | |
207 | VDM_STATE_ERR_TMOUT = -1, | |
208 | VDM_STATE_DONE = 0, | |
209 | /* Anything >0 represents an active state */ | |
210 | VDM_STATE_READY = 1, | |
211 | VDM_STATE_BUSY = 2, | |
212 | VDM_STATE_WAIT_RSP_BUSY = 3, | |
0908c5ac | 213 | VDM_STATE_SEND_MESSAGE = 4, |
f0690a25 GR |
214 | }; |
215 | ||
216 | enum pd_msg_request { | |
217 | PD_MSG_NONE = 0, | |
218 | PD_MSG_CTRL_REJECT, | |
219 | PD_MSG_CTRL_WAIT, | |
64f7c494 | 220 | PD_MSG_CTRL_NOT_SUPP, |
f0690a25 GR |
221 | PD_MSG_DATA_SINK_CAP, |
222 | PD_MSG_DATA_SOURCE_CAP, | |
223 | }; | |
224 | ||
95b4d51c HG |
225 | enum adev_actions { |
226 | ADEV_NONE = 0, | |
227 | ADEV_NOTIFY_USB_AND_QUEUE_VDM, | |
228 | ADEV_QUEUE_VDM, | |
229 | ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL, | |
230 | ADEV_ATTENTION, | |
231 | }; | |
232 | ||
8dc4bd07 BJS |
233 | /* |
234 | * Initial current capability of the new source when vSafe5V is applied during PD3.0 Fast Role Swap. | |
235 | * Based on "Table 6-14 Fixed Supply PDO - Sink" of "USB Power Delivery Specification Revision 3.0, | |
236 | * Version 1.2" | |
237 | */ | |
238 | enum frs_typec_current { | |
239 | FRS_NOT_SUPPORTED, | |
240 | FRS_DEFAULT_POWER, | |
241 | FRS_5V_1P5A, | |
242 | FRS_5V_3A, | |
243 | }; | |
244 | ||
f0690a25 GR |
245 | /* Events from low level driver */ |
246 | ||
247 | #define TCPM_CC_EVENT BIT(0) | |
248 | #define TCPM_VBUS_EVENT BIT(1) | |
249 | #define TCPM_RESET_EVENT BIT(2) | |
8dc4bd07 BJS |
250 | #define TCPM_FRS_EVENT BIT(3) |
251 | #define TCPM_SOURCING_VBUS BIT(4) | |
f0690a25 GR |
252 | |
253 | #define LOG_BUFFER_ENTRIES 1024 | |
254 | #define LOG_BUFFER_ENTRY_SIZE 128 | |
255 | ||
256 | /* Alternate mode support */ | |
257 | ||
258 | #define SVID_DISCOVERY_MAX 16 | |
e9576fe8 | 259 | #define ALTMODE_DISCOVERY_MAX (SVID_DISCOVERY_MAX * MODE_DISCOVERY_MAX) |
f0690a25 | 260 | |
8dc4bd07 | 261 | #define GET_SINK_CAP_RETRY_MS 100 |
c34e85fa | 262 | #define SEND_DISCOVER_RETRY_MS 100 |
8dc4bd07 | 263 | |
f0690a25 GR |
264 | struct pd_mode_data { |
265 | int svid_index; /* current SVID index */ | |
266 | int nsvids; | |
267 | u16 svids[SVID_DISCOVERY_MAX]; | |
268 | int altmodes; /* number of alternate modes */ | |
e9576fe8 | 269 | struct typec_altmode_desc altmode_desc[ALTMODE_DISCOVERY_MAX]; |
f0690a25 GR |
270 | }; |
271 | ||
e3a07202 BJS |
272 | /* |
273 | * @min_volt: Actual min voltage at the local port | |
274 | * @req_min_volt: Requested min voltage to the port partner | |
275 | * @max_volt: Actual max voltage at the local port | |
276 | * @req_max_volt: Requested max voltage to the port partner | |
277 | * @max_curr: Actual max current at the local port | |
278 | * @req_max_curr: Requested max current of the port partner | |
279 | * @req_out_volt: Requested output voltage to the port partner | |
280 | * @req_op_curr: Requested operating current to the port partner | |
f73800a9 | 281 | * @supported: Parter has at least one APDO hence supports PPS |
e3a07202 BJS |
282 | * @active: PPS mode is active |
283 | */ | |
2eadc33f AT |
284 | struct pd_pps_data { |
285 | u32 min_volt; | |
e3a07202 | 286 | u32 req_min_volt; |
2eadc33f | 287 | u32 max_volt; |
e3a07202 | 288 | u32 req_max_volt; |
2eadc33f | 289 | u32 max_curr; |
e3a07202 BJS |
290 | u32 req_max_curr; |
291 | u32 req_out_volt; | |
292 | u32 req_op_curr; | |
2eadc33f AT |
293 | bool supported; |
294 | bool active; | |
295 | }; | |
296 | ||
f0690a25 GR |
297 | struct tcpm_port { |
298 | struct device *dev; | |
299 | ||
300 | struct mutex lock; /* tcpm state machine lock */ | |
3ed8e1c2 | 301 | struct kthread_worker *wq; |
f0690a25 GR |
302 | |
303 | struct typec_capability typec_caps; | |
304 | struct typec_port *typec_port; | |
305 | ||
306 | struct tcpc_dev *tcpc; | |
2000016c | 307 | struct usb_role_switch *role_sw; |
f0690a25 GR |
308 | |
309 | enum typec_role vconn_role; | |
310 | enum typec_role pwr_role; | |
311 | enum typec_data_role data_role; | |
312 | enum typec_pwr_opmode pwr_opmode; | |
313 | ||
314 | struct usb_pd_identity partner_ident; | |
315 | struct typec_partner_desc partner_desc; | |
316 | struct typec_partner *partner; | |
317 | ||
318 | enum typec_cc_status cc_req; | |
e9e6e164 | 319 | enum typec_cc_status src_rp; /* work only if pd_supported == false */ |
f0690a25 GR |
320 | |
321 | enum typec_cc_status cc1; | |
322 | enum typec_cc_status cc2; | |
323 | enum typec_cc_polarity polarity; | |
324 | ||
325 | bool attached; | |
326 | bool connected; | |
ca4d8344 | 327 | bool registered; |
e9e6e164 | 328 | bool pd_supported; |
9b0ae699 | 329 | enum typec_port_type port_type; |
28b43d3d BJS |
330 | |
331 | /* | |
332 | * Set to true when vbus is greater than VSAFE5V min. | |
333 | * Set to false when vbus falls below vSinkDisconnect max threshold. | |
334 | */ | |
f0690a25 | 335 | bool vbus_present; |
28b43d3d BJS |
336 | |
337 | /* | |
338 | * Set to true when vbus is less than VSAFE0V max. | |
339 | * Set to false when vbus is greater than VSAFE0V max. | |
340 | */ | |
341 | bool vbus_vsafe0v; | |
342 | ||
f0690a25 GR |
343 | bool vbus_never_low; |
344 | bool vbus_source; | |
345 | bool vbus_charge; | |
346 | ||
ef52b4a9 | 347 | /* Set to true when Discover_Identity Command is expected to be sent in Ready states. */ |
f0690a25 GR |
348 | bool send_discover; |
349 | bool op_vsafe5v; | |
350 | ||
351 | int try_role; | |
352 | int try_snk_count; | |
353 | int try_src_count; | |
354 | ||
355 | enum pd_msg_request queued_message; | |
356 | ||
357 | enum tcpm_state enter_state; | |
358 | enum tcpm_state prev_state; | |
359 | enum tcpm_state state; | |
360 | enum tcpm_state delayed_state; | |
3ed8e1c2 | 361 | ktime_t delayed_runtime; |
f0690a25 GR |
362 | unsigned long delay_ms; |
363 | ||
364 | spinlock_t pd_event_lock; | |
365 | u32 pd_events; | |
366 | ||
3ed8e1c2 BJS |
367 | struct kthread_work event_work; |
368 | struct hrtimer state_machine_timer; | |
369 | struct kthread_work state_machine; | |
370 | struct hrtimer vdm_state_machine_timer; | |
371 | struct kthread_work vdm_state_machine; | |
8dc4bd07 BJS |
372 | struct hrtimer enable_frs_timer; |
373 | struct kthread_work enable_frs; | |
c34e85fa KT |
374 | struct hrtimer send_discover_timer; |
375 | struct kthread_work send_discover_work; | |
f0690a25 | 376 | bool state_machine_running; |
ef52b4a9 | 377 | /* Set to true when VDM State Machine has following actions. */ |
8d3a0578 | 378 | bool vdm_sm_running; |
f0690a25 GR |
379 | |
380 | struct completion tx_complete; | |
381 | enum tcpm_transmit_status tx_status; | |
382 | ||
383 | struct mutex swap_lock; /* swap command lock */ | |
384 | bool swap_pending; | |
b17dd571 | 385 | bool non_pd_role_swap; |
f0690a25 GR |
386 | struct completion swap_complete; |
387 | int swap_status; | |
388 | ||
2eadc33f | 389 | unsigned int negotiated_rev; |
f0690a25 GR |
390 | unsigned int message_id; |
391 | unsigned int caps_count; | |
392 | unsigned int hard_reset_count; | |
393 | bool pd_capable; | |
394 | bool explicit_contract; | |
5fec4b54 | 395 | unsigned int rx_msgid; |
f0690a25 | 396 | |
8203d269 HK |
397 | /* USB PD objects */ |
398 | struct usb_power_delivery *pd; | |
399 | struct usb_power_delivery_capabilities *port_source_caps; | |
400 | struct usb_power_delivery_capabilities *port_sink_caps; | |
401 | struct usb_power_delivery *partner_pd; | |
402 | struct usb_power_delivery_capabilities *partner_source_caps; | |
403 | struct usb_power_delivery_capabilities *partner_sink_caps; | |
404 | ||
f0690a25 GR |
405 | /* Partner capabilities/requests */ |
406 | u32 sink_request; | |
407 | u32 source_caps[PDO_MAX_OBJECTS]; | |
408 | unsigned int nr_source_caps; | |
409 | u32 sink_caps[PDO_MAX_OBJECTS]; | |
410 | unsigned int nr_sink_caps; | |
411 | ||
412 | /* Local capabilities */ | |
413 | u32 src_pdo[PDO_MAX_OBJECTS]; | |
414 | unsigned int nr_src_pdo; | |
415 | u32 snk_pdo[PDO_MAX_OBJECTS]; | |
416 | unsigned int nr_snk_pdo; | |
7ac50510 KT |
417 | u32 snk_vdo_v1[VDO_MAX_OBJECTS]; |
418 | unsigned int nr_snk_vdo_v1; | |
193a6801 GR |
419 | u32 snk_vdo[VDO_MAX_OBJECTS]; |
420 | unsigned int nr_snk_vdo; | |
f0690a25 | 421 | |
f0690a25 | 422 | unsigned int operating_snk_mw; |
2eadc33f | 423 | bool update_sink_caps; |
f0690a25 | 424 | |
f3dedafb BJS |
425 | /* Requested current / voltage to the port partner */ |
426 | u32 req_current_limit; | |
427 | u32 req_supply_voltage; | |
428 | /* Actual current / voltage limit of the local port */ | |
f0690a25 GR |
429 | u32 current_limit; |
430 | u32 supply_voltage; | |
431 | ||
f2a8aa05 AT |
432 | /* Used to export TA voltage and current */ |
433 | struct power_supply *psy; | |
434 | struct power_supply_desc psy_desc; | |
435 | enum power_supply_usb_type usb_type; | |
436 | ||
f0690a25 GR |
437 | u32 bist_request; |
438 | ||
439 | /* PD state for Vendor Defined Messages */ | |
440 | enum vdm_states vdm_state; | |
441 | u32 vdm_retries; | |
442 | /* next Vendor Defined Message to send */ | |
443 | u32 vdo_data[VDO_MAX_SIZE]; | |
444 | u8 vdo_count; | |
445 | /* VDO to retry if UFP responder replied busy */ | |
446 | u32 vdo_retry; | |
447 | ||
2eadc33f AT |
448 | /* PPS */ |
449 | struct pd_pps_data pps_data; | |
450 | struct completion pps_complete; | |
451 | bool pps_pending; | |
452 | int pps_status; | |
f0690a25 | 453 | |
2eadc33f | 454 | /* Alternate mode data */ |
f0690a25 | 455 | struct pd_mode_data mode_data; |
e9576fe8 HK |
456 | struct typec_altmode *partner_altmode[ALTMODE_DISCOVERY_MAX]; |
457 | struct typec_altmode *port_altmode[ALTMODE_DISCOVERY_MAX]; | |
f0690a25 | 458 | |
02d5be46 BJS |
459 | /* Deadline in jiffies to exit src_try_wait state */ |
460 | unsigned long max_wait; | |
461 | ||
23b5f732 BJS |
462 | /* port belongs to a self powered device */ |
463 | bool self_powered; | |
464 | ||
8115240c BJS |
465 | /* Sink FRS */ |
466 | enum frs_typec_current new_source_frs_current; | |
8dc4bd07 BJS |
467 | |
468 | /* Sink caps have been queried */ | |
469 | bool sink_cap_done; | |
470 | ||
0908c5ac KT |
471 | /* Collision Avoidance and Atomic Message Sequence */ |
472 | enum tcpm_state upcoming_state; | |
473 | enum tcpm_ams ams; | |
8dea75e1 | 474 | enum tcpm_ams next_ams; |
0908c5ac KT |
475 | bool in_ams; |
476 | ||
3287f58b BJS |
477 | /* Auto vbus discharge status */ |
478 | bool auto_vbus_discharge_enabled; | |
479 | ||
1373fefc BJS |
480 | /* |
481 | * When set, port requests PD_P_SNK_STDBY_MW upon entering SNK_DISCOVERY and | |
4e27465a | 482 | * the actual current limit after RX of PD_CTRL_PSRDY for PD link, |
1373fefc BJS |
483 | * SNK_READY for non-pd link. |
484 | */ | |
485 | bool slow_charger_loop; | |
f0690a25 GR |
486 | #ifdef CONFIG_DEBUG_FS |
487 | struct dentry *dentry; | |
488 | struct mutex logbuffer_lock; /* log buffer access lock */ | |
489 | int logbuffer_head; | |
490 | int logbuffer_tail; | |
491 | u8 *logbuffer[LOG_BUFFER_ENTRIES]; | |
492 | #endif | |
493 | }; | |
494 | ||
495 | struct pd_rx_event { | |
3ed8e1c2 | 496 | struct kthread_work work; |
f0690a25 GR |
497 | struct tcpm_port *port; |
498 | struct pd_message msg; | |
499 | }; | |
500 | ||
0908c5ac KT |
501 | static const char * const pd_rev[] = { |
502 | [PD_REV10] = "rev1", | |
503 | [PD_REV20] = "rev2", | |
504 | [PD_REV30] = "rev3", | |
505 | }; | |
506 | ||
f0690a25 GR |
507 | #define tcpm_cc_is_sink(cc) \ |
508 | ((cc) == TYPEC_CC_RP_DEF || (cc) == TYPEC_CC_RP_1_5 || \ | |
509 | (cc) == TYPEC_CC_RP_3_0) | |
510 | ||
511 | #define tcpm_port_is_sink(port) \ | |
512 | ((tcpm_cc_is_sink((port)->cc1) && !tcpm_cc_is_sink((port)->cc2)) || \ | |
513 | (tcpm_cc_is_sink((port)->cc2) && !tcpm_cc_is_sink((port)->cc1))) | |
514 | ||
515 | #define tcpm_cc_is_source(cc) ((cc) == TYPEC_CC_RD) | |
516 | #define tcpm_cc_is_audio(cc) ((cc) == TYPEC_CC_RA) | |
517 | #define tcpm_cc_is_open(cc) ((cc) == TYPEC_CC_OPEN) | |
518 | ||
519 | #define tcpm_port_is_source(port) \ | |
520 | ((tcpm_cc_is_source((port)->cc1) && \ | |
521 | !tcpm_cc_is_source((port)->cc2)) || \ | |
522 | (tcpm_cc_is_source((port)->cc2) && \ | |
523 | !tcpm_cc_is_source((port)->cc1))) | |
524 | ||
525 | #define tcpm_port_is_debug(port) \ | |
526 | (tcpm_cc_is_source((port)->cc1) && tcpm_cc_is_source((port)->cc2)) | |
527 | ||
528 | #define tcpm_port_is_audio(port) \ | |
529 | (tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_audio((port)->cc2)) | |
530 | ||
531 | #define tcpm_port_is_audio_detached(port) \ | |
532 | ((tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_open((port)->cc2)) || \ | |
533 | (tcpm_cc_is_audio((port)->cc2) && tcpm_cc_is_open((port)->cc1))) | |
534 | ||
535 | #define tcpm_try_snk(port) \ | |
ff6c8cb1 BJS |
536 | ((port)->try_snk_count == 0 && (port)->try_role == TYPEC_SINK && \ |
537 | (port)->port_type == TYPEC_PORT_DRP) | |
f0690a25 GR |
538 | |
539 | #define tcpm_try_src(port) \ | |
ff6c8cb1 BJS |
540 | ((port)->try_src_count == 0 && (port)->try_role == TYPEC_SOURCE && \ |
541 | (port)->port_type == TYPEC_PORT_DRP) | |
f0690a25 | 542 | |
6ecc632d LJ |
543 | #define tcpm_data_role_for_source(port) \ |
544 | ((port)->typec_caps.data == TYPEC_PORT_UFP ? \ | |
545 | TYPEC_DEVICE : TYPEC_HOST) | |
546 | ||
547 | #define tcpm_data_role_for_sink(port) \ | |
548 | ((port)->typec_caps.data == TYPEC_PORT_DFP ? \ | |
549 | TYPEC_HOST : TYPEC_DEVICE) | |
550 | ||
0908c5ac KT |
551 | #define tcpm_sink_tx_ok(port) \ |
552 | (tcpm_port_is_sink(port) && \ | |
553 | ((port)->cc1 == TYPEC_CC_RP_3_0 || (port)->cc2 == TYPEC_CC_RP_3_0)) | |
554 | ||
3287f58b BJS |
555 | #define tcpm_wait_for_discharge(port) \ |
556 | (((port)->auto_vbus_discharge_enabled && !(port)->vbus_vsafe0v) ? PD_T_SAFE_0V : 0) | |
557 | ||
f0690a25 GR |
558 | static enum tcpm_state tcpm_default_state(struct tcpm_port *port) |
559 | { | |
9b0ae699 | 560 | if (port->port_type == TYPEC_PORT_DRP) { |
b46a9c90 BJS |
561 | if (port->try_role == TYPEC_SINK) |
562 | return SNK_UNATTACHED; | |
563 | else if (port->try_role == TYPEC_SOURCE) | |
564 | return SRC_UNATTACHED; | |
b46a9c90 | 565 | /* Fall through to return SRC_UNATTACHED */ |
ceeb1625 | 566 | } else if (port->port_type == TYPEC_PORT_SNK) { |
f0690a25 | 567 | return SNK_UNATTACHED; |
b46a9c90 | 568 | } |
f0690a25 GR |
569 | return SRC_UNATTACHED; |
570 | } | |
571 | ||
f0690a25 GR |
572 | static bool tcpm_port_is_disconnected(struct tcpm_port *port) |
573 | { | |
574 | return (!port->attached && port->cc1 == TYPEC_CC_OPEN && | |
575 | port->cc2 == TYPEC_CC_OPEN) || | |
576 | (port->attached && ((port->polarity == TYPEC_POLARITY_CC1 && | |
577 | port->cc1 == TYPEC_CC_OPEN) || | |
578 | (port->polarity == TYPEC_POLARITY_CC2 && | |
579 | port->cc2 == TYPEC_CC_OPEN))); | |
580 | } | |
581 | ||
582 | /* | |
583 | * Logging | |
584 | */ | |
585 | ||
586 | #ifdef CONFIG_DEBUG_FS | |
587 | ||
588 | static bool tcpm_log_full(struct tcpm_port *port) | |
589 | { | |
590 | return port->logbuffer_tail == | |
591 | (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES; | |
592 | } | |
593 | ||
e79e0125 | 594 | __printf(2, 0) |
f0690a25 GR |
595 | static void _tcpm_log(struct tcpm_port *port, const char *fmt, va_list args) |
596 | { | |
597 | char tmpbuffer[LOG_BUFFER_ENTRY_SIZE]; | |
598 | u64 ts_nsec = local_clock(); | |
599 | unsigned long rem_nsec; | |
600 | ||
d5a4f935 | 601 | mutex_lock(&port->logbuffer_lock); |
f0690a25 GR |
602 | if (!port->logbuffer[port->logbuffer_head]) { |
603 | port->logbuffer[port->logbuffer_head] = | |
604 | kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL); | |
d5a4f935 PC |
605 | if (!port->logbuffer[port->logbuffer_head]) { |
606 | mutex_unlock(&port->logbuffer_lock); | |
f0690a25 | 607 | return; |
d5a4f935 | 608 | } |
f0690a25 GR |
609 | } |
610 | ||
611 | vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args); | |
612 | ||
f0690a25 GR |
613 | if (tcpm_log_full(port)) { |
614 | port->logbuffer_head = max(port->logbuffer_head - 1, 0); | |
615 | strcpy(tmpbuffer, "overflow"); | |
616 | } | |
617 | ||
618 | if (port->logbuffer_head < 0 || | |
619 | port->logbuffer_head >= LOG_BUFFER_ENTRIES) { | |
620 | dev_warn(port->dev, | |
621 | "Bad log buffer index %d\n", port->logbuffer_head); | |
622 | goto abort; | |
623 | } | |
624 | ||
625 | if (!port->logbuffer[port->logbuffer_head]) { | |
626 | dev_warn(port->dev, | |
627 | "Log buffer index %d is NULL\n", port->logbuffer_head); | |
628 | goto abort; | |
629 | } | |
630 | ||
631 | rem_nsec = do_div(ts_nsec, 1000000000); | |
632 | scnprintf(port->logbuffer[port->logbuffer_head], | |
633 | LOG_BUFFER_ENTRY_SIZE, "[%5lu.%06lu] %s", | |
634 | (unsigned long)ts_nsec, rem_nsec / 1000, | |
635 | tmpbuffer); | |
636 | port->logbuffer_head = (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES; | |
637 | ||
638 | abort: | |
639 | mutex_unlock(&port->logbuffer_lock); | |
640 | } | |
641 | ||
e79e0125 | 642 | __printf(2, 3) |
f0690a25 GR |
643 | static void tcpm_log(struct tcpm_port *port, const char *fmt, ...) |
644 | { | |
645 | va_list args; | |
646 | ||
647 | /* Do not log while disconnected and unattached */ | |
648 | if (tcpm_port_is_disconnected(port) && | |
649 | (port->state == SRC_UNATTACHED || port->state == SNK_UNATTACHED || | |
7893f9e1 | 650 | port->state == TOGGLING)) |
f0690a25 GR |
651 | return; |
652 | ||
653 | va_start(args, fmt); | |
654 | _tcpm_log(port, fmt, args); | |
655 | va_end(args); | |
656 | } | |
657 | ||
e79e0125 | 658 | __printf(2, 3) |
f0690a25 GR |
659 | static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...) |
660 | { | |
661 | va_list args; | |
662 | ||
663 | va_start(args, fmt); | |
664 | _tcpm_log(port, fmt, args); | |
665 | va_end(args); | |
666 | } | |
667 | ||
668 | static void tcpm_log_source_caps(struct tcpm_port *port) | |
669 | { | |
670 | int i; | |
671 | ||
672 | for (i = 0; i < port->nr_source_caps; i++) { | |
673 | u32 pdo = port->source_caps[i]; | |
674 | enum pd_pdo_type type = pdo_type(pdo); | |
675 | char msg[64]; | |
676 | ||
677 | switch (type) { | |
678 | case PDO_TYPE_FIXED: | |
679 | scnprintf(msg, sizeof(msg), | |
680 | "%u mV, %u mA [%s%s%s%s%s%s]", | |
681 | pdo_fixed_voltage(pdo), | |
682 | pdo_max_current(pdo), | |
683 | (pdo & PDO_FIXED_DUAL_ROLE) ? | |
684 | "R" : "", | |
685 | (pdo & PDO_FIXED_SUSPEND) ? | |
686 | "S" : "", | |
687 | (pdo & PDO_FIXED_HIGHER_CAP) ? | |
688 | "H" : "", | |
689 | (pdo & PDO_FIXED_USB_COMM) ? | |
690 | "U" : "", | |
691 | (pdo & PDO_FIXED_DATA_SWAP) ? | |
692 | "D" : "", | |
693 | (pdo & PDO_FIXED_EXTPOWER) ? | |
694 | "E" : ""); | |
695 | break; | |
696 | case PDO_TYPE_VAR: | |
697 | scnprintf(msg, sizeof(msg), | |
698 | "%u-%u mV, %u mA", | |
699 | pdo_min_voltage(pdo), | |
700 | pdo_max_voltage(pdo), | |
701 | pdo_max_current(pdo)); | |
702 | break; | |
703 | case PDO_TYPE_BATT: | |
704 | scnprintf(msg, sizeof(msg), | |
705 | "%u-%u mV, %u mW", | |
706 | pdo_min_voltage(pdo), | |
707 | pdo_max_voltage(pdo), | |
708 | pdo_max_power(pdo)); | |
709 | break; | |
2eadc33f AT |
710 | case PDO_TYPE_APDO: |
711 | if (pdo_apdo_type(pdo) == APDO_TYPE_PPS) | |
712 | scnprintf(msg, sizeof(msg), | |
713 | "%u-%u mV, %u mA", | |
714 | pdo_pps_apdo_min_voltage(pdo), | |
715 | pdo_pps_apdo_max_voltage(pdo), | |
716 | pdo_pps_apdo_max_current(pdo)); | |
717 | else | |
718 | strcpy(msg, "undefined APDO"); | |
719 | break; | |
f0690a25 GR |
720 | default: |
721 | strcpy(msg, "undefined"); | |
722 | break; | |
723 | } | |
724 | tcpm_log(port, " PDO %d: type %d, %s", | |
725 | i, type, msg); | |
726 | } | |
727 | } | |
728 | ||
9e902c59 | 729 | static int tcpm_debug_show(struct seq_file *s, void *v) |
f0690a25 GR |
730 | { |
731 | struct tcpm_port *port = (struct tcpm_port *)s->private; | |
732 | int tail; | |
733 | ||
734 | mutex_lock(&port->logbuffer_lock); | |
735 | tail = port->logbuffer_tail; | |
736 | while (tail != port->logbuffer_head) { | |
737 | seq_printf(s, "%s\n", port->logbuffer[tail]); | |
738 | tail = (tail + 1) % LOG_BUFFER_ENTRIES; | |
739 | } | |
740 | if (!seq_has_overflowed(s)) | |
741 | port->logbuffer_tail = tail; | |
742 | mutex_unlock(&port->logbuffer_lock); | |
743 | ||
744 | return 0; | |
745 | } | |
9e902c59 | 746 | DEFINE_SHOW_ATTRIBUTE(tcpm_debug); |
f0690a25 | 747 | |
a941fc39 | 748 | static void tcpm_debugfs_init(struct tcpm_port *port) |
f0690a25 | 749 | { |
9f4bc2ed | 750 | char name[NAME_MAX]; |
f0690a25 | 751 | |
9f4bc2ed HG |
752 | mutex_init(&port->logbuffer_lock); |
753 | snprintf(name, NAME_MAX, "tcpm-%s", dev_name(port->dev)); | |
153e140d GKH |
754 | port->dentry = debugfs_create_dir(name, usb_debug_root); |
755 | debugfs_create_file("log", S_IFREG | 0444, port->dentry, port, | |
756 | &tcpm_debug_fops); | |
f0690a25 GR |
757 | } |
758 | ||
759 | static void tcpm_debugfs_exit(struct tcpm_port *port) | |
760 | { | |
fd5da3e2 LJ |
761 | int i; |
762 | ||
763 | mutex_lock(&port->logbuffer_lock); | |
764 | for (i = 0; i < LOG_BUFFER_ENTRIES; i++) { | |
765 | kfree(port->logbuffer[i]); | |
766 | port->logbuffer[i] = NULL; | |
767 | } | |
768 | mutex_unlock(&port->logbuffer_lock); | |
769 | ||
f0690a25 GR |
770 | debugfs_remove(port->dentry); |
771 | } | |
772 | ||
773 | #else | |
774 | ||
e79e0125 | 775 | __printf(2, 3) |
f0690a25 | 776 | static void tcpm_log(const struct tcpm_port *port, const char *fmt, ...) { } |
e79e0125 | 777 | __printf(2, 3) |
f0690a25 GR |
778 | static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...) { } |
779 | static void tcpm_log_source_caps(struct tcpm_port *port) { } | |
a941fc39 | 780 | static void tcpm_debugfs_init(const struct tcpm_port *port) { } |
f0690a25 GR |
781 | static void tcpm_debugfs_exit(const struct tcpm_port *port) { } |
782 | ||
783 | #endif | |
784 | ||
0908c5ac KT |
785 | static void tcpm_set_cc(struct tcpm_port *port, enum typec_cc_status cc) |
786 | { | |
787 | tcpm_log(port, "cc:=%d", cc); | |
788 | port->cc_req = cc; | |
789 | port->tcpc->set_cc(port->tcpc, cc); | |
790 | } | |
791 | ||
dea6f87e BJS |
792 | static int tcpm_enable_auto_vbus_discharge(struct tcpm_port *port, bool enable) |
793 | { | |
794 | int ret = 0; | |
795 | ||
796 | if (port->tcpc->enable_auto_vbus_discharge) { | |
797 | ret = port->tcpc->enable_auto_vbus_discharge(port->tcpc, enable); | |
798 | tcpm_log_force(port, "%s vbus discharge ret:%d", enable ? "enable" : "disable", | |
799 | ret); | |
800 | if (!ret) | |
801 | port->auto_vbus_discharge_enabled = enable; | |
802 | } | |
803 | ||
804 | return ret; | |
805 | } | |
806 | ||
59d4d06c BJS |
807 | static void tcpm_apply_rc(struct tcpm_port *port) |
808 | { | |
809 | /* | |
810 | * TCPCI: Move to APPLY_RC state to prevent disconnect during PR_SWAP | |
811 | * when Vbus auto discharge on disconnect is enabled. | |
812 | */ | |
813 | if (port->tcpc->enable_auto_vbus_discharge && port->tcpc->apply_rc) { | |
814 | tcpm_log(port, "Apply_RC"); | |
815 | port->tcpc->apply_rc(port->tcpc, port->cc_req, port->polarity); | |
816 | tcpm_enable_auto_vbus_discharge(port, false); | |
817 | } | |
818 | } | |
819 | ||
0908c5ac KT |
820 | /* |
821 | * Determine RP value to set based on maximum current supported | |
822 | * by a port if configured as source. | |
823 | * Returns CC value to report to link partner. | |
824 | */ | |
825 | static enum typec_cc_status tcpm_rp_cc(struct tcpm_port *port) | |
826 | { | |
827 | const u32 *src_pdo = port->src_pdo; | |
828 | int nr_pdo = port->nr_src_pdo; | |
829 | int i; | |
830 | ||
e9e6e164 KT |
831 | if (!port->pd_supported) |
832 | return port->src_rp; | |
833 | ||
0908c5ac KT |
834 | /* |
835 | * Search for first entry with matching voltage. | |
836 | * It should report the maximum supported current. | |
837 | */ | |
838 | for (i = 0; i < nr_pdo; i++) { | |
839 | const u32 pdo = src_pdo[i]; | |
840 | ||
841 | if (pdo_type(pdo) == PDO_TYPE_FIXED && | |
842 | pdo_fixed_voltage(pdo) == 5000) { | |
843 | unsigned int curr = pdo_max_current(pdo); | |
844 | ||
845 | if (curr >= 3000) | |
846 | return TYPEC_CC_RP_3_0; | |
847 | else if (curr >= 1500) | |
848 | return TYPEC_CC_RP_1_5; | |
849 | return TYPEC_CC_RP_DEF; | |
850 | } | |
851 | } | |
852 | ||
853 | return TYPEC_CC_RP_DEF; | |
854 | } | |
855 | ||
493d0856 | 856 | static void tcpm_ams_finish(struct tcpm_port *port) |
0908c5ac | 857 | { |
0908c5ac KT |
858 | tcpm_log(port, "AMS %s finished", tcpm_ams_str[port->ams]); |
859 | ||
860 | if (port->pd_capable && port->pwr_role == TYPEC_SOURCE) { | |
861 | if (port->negotiated_rev >= PD_REV30) | |
862 | tcpm_set_cc(port, SINK_TX_OK); | |
863 | else | |
864 | tcpm_set_cc(port, SINK_TX_NG); | |
865 | } else if (port->pwr_role == TYPEC_SOURCE) { | |
866 | tcpm_set_cc(port, tcpm_rp_cc(port)); | |
867 | } | |
868 | ||
869 | port->in_ams = false; | |
870 | port->ams = NONE_AMS; | |
0908c5ac KT |
871 | } |
872 | ||
f0690a25 GR |
873 | static int tcpm_pd_transmit(struct tcpm_port *port, |
874 | enum tcpm_transmit_type type, | |
875 | const struct pd_message *msg) | |
876 | { | |
877 | unsigned long timeout; | |
878 | int ret; | |
879 | ||
880 | if (msg) | |
881 | tcpm_log(port, "PD TX, header: %#x", le16_to_cpu(msg->header)); | |
882 | else | |
883 | tcpm_log(port, "PD TX, type: %#x", type); | |
884 | ||
885 | reinit_completion(&port->tx_complete); | |
e4a93780 | 886 | ret = port->tcpc->pd_transmit(port->tcpc, type, msg, port->negotiated_rev); |
f0690a25 GR |
887 | if (ret < 0) |
888 | return ret; | |
889 | ||
890 | mutex_unlock(&port->lock); | |
891 | timeout = wait_for_completion_timeout(&port->tx_complete, | |
892 | msecs_to_jiffies(PD_T_TCPC_TX_TIMEOUT)); | |
893 | mutex_lock(&port->lock); | |
894 | if (!timeout) | |
895 | return -ETIMEDOUT; | |
896 | ||
897 | switch (port->tx_status) { | |
898 | case TCPC_TX_SUCCESS: | |
899 | port->message_id = (port->message_id + 1) & PD_HEADER_ID_MASK; | |
0908c5ac KT |
900 | /* |
901 | * USB PD rev 2.0, 8.3.2.2.1: | |
902 | * USB PD rev 3.0, 8.3.2.1.3: | |
903 | * "... Note that every AMS is Interruptible until the first | |
904 | * Message in the sequence has been successfully sent (GoodCRC | |
905 | * Message received)." | |
906 | */ | |
907 | if (port->ams != NONE_AMS) | |
908 | port->in_ams = true; | |
909 | break; | |
f0690a25 | 910 | case TCPC_TX_DISCARDED: |
0908c5ac KT |
911 | ret = -EAGAIN; |
912 | break; | |
f0690a25 GR |
913 | case TCPC_TX_FAILED: |
914 | default: | |
0908c5ac KT |
915 | ret = -EIO; |
916 | break; | |
f0690a25 | 917 | } |
0908c5ac KT |
918 | |
919 | /* Some AMS don't expect responses. Finish them here. */ | |
920 | if (port->ams == ATTENTION || port->ams == SOURCE_ALERT) | |
921 | tcpm_ams_finish(port); | |
922 | ||
923 | return ret; | |
f0690a25 GR |
924 | } |
925 | ||
926 | void tcpm_pd_transmit_complete(struct tcpm_port *port, | |
927 | enum tcpm_transmit_status status) | |
928 | { | |
929 | tcpm_log(port, "PD TX complete, status: %u", status); | |
930 | port->tx_status = status; | |
931 | complete(&port->tx_complete); | |
932 | } | |
933 | EXPORT_SYMBOL_GPL(tcpm_pd_transmit_complete); | |
934 | ||
e9576fe8 | 935 | static int tcpm_mux_set(struct tcpm_port *port, int state, |
2000016c HG |
936 | enum usb_role usb_role, |
937 | enum typec_orientation orientation) | |
f0690a25 | 938 | { |
2000016c | 939 | int ret; |
f0690a25 | 940 | |
e9576fe8 HK |
941 | tcpm_log(port, "Requesting mux state %d, usb-role %d, orientation %d", |
942 | state, usb_role, orientation); | |
f0690a25 | 943 | |
2000016c HG |
944 | ret = typec_set_orientation(port->typec_port, orientation); |
945 | if (ret) | |
946 | return ret; | |
f0690a25 | 947 | |
2000016c HG |
948 | if (port->role_sw) { |
949 | ret = usb_role_switch_set_role(port->role_sw, usb_role); | |
950 | if (ret) | |
951 | return ret; | |
952 | } | |
953 | ||
e9576fe8 | 954 | return typec_set_mode(port->typec_port, state); |
f0690a25 GR |
955 | } |
956 | ||
957 | static int tcpm_set_polarity(struct tcpm_port *port, | |
958 | enum typec_cc_polarity polarity) | |
959 | { | |
960 | int ret; | |
961 | ||
962 | tcpm_log(port, "polarity %d", polarity); | |
963 | ||
964 | ret = port->tcpc->set_polarity(port->tcpc, polarity); | |
965 | if (ret < 0) | |
966 | return ret; | |
967 | ||
968 | port->polarity = polarity; | |
969 | ||
970 | return 0; | |
971 | } | |
972 | ||
973 | static int tcpm_set_vconn(struct tcpm_port *port, bool enable) | |
974 | { | |
975 | int ret; | |
976 | ||
977 | tcpm_log(port, "vconn:=%d", enable); | |
978 | ||
979 | ret = port->tcpc->set_vconn(port->tcpc, enable); | |
980 | if (!ret) { | |
981 | port->vconn_role = enable ? TYPEC_SOURCE : TYPEC_SINK; | |
982 | typec_set_vconn_role(port->typec_port, port->vconn_role); | |
983 | } | |
984 | ||
985 | return ret; | |
986 | } | |
987 | ||
988 | static u32 tcpm_get_current_limit(struct tcpm_port *port) | |
989 | { | |
990 | enum typec_cc_status cc; | |
991 | u32 limit; | |
992 | ||
993 | cc = port->polarity ? port->cc2 : port->cc1; | |
994 | switch (cc) { | |
995 | case TYPEC_CC_RP_1_5: | |
996 | limit = 1500; | |
997 | break; | |
998 | case TYPEC_CC_RP_3_0: | |
999 | limit = 3000; | |
1000 | break; | |
1001 | case TYPEC_CC_RP_DEF: | |
1002 | default: | |
ea62cfc7 HG |
1003 | if (port->tcpc->get_current_limit) |
1004 | limit = port->tcpc->get_current_limit(port->tcpc); | |
1005 | else | |
1006 | limit = 0; | |
f0690a25 GR |
1007 | break; |
1008 | } | |
1009 | ||
1010 | return limit; | |
1011 | } | |
1012 | ||
1013 | static int tcpm_set_current_limit(struct tcpm_port *port, u32 max_ma, u32 mv) | |
1014 | { | |
1015 | int ret = -EOPNOTSUPP; | |
1016 | ||
1017 | tcpm_log(port, "Setting voltage/current limit %u mV %u mA", mv, max_ma); | |
1018 | ||
146971e6 HG |
1019 | port->supply_voltage = mv; |
1020 | port->current_limit = max_ma; | |
86629e09 | 1021 | power_supply_changed(port->psy); |
146971e6 | 1022 | |
f0690a25 GR |
1023 | if (port->tcpc->set_current_limit) |
1024 | ret = port->tcpc->set_current_limit(port->tcpc, max_ma, mv); | |
1025 | ||
1026 | return ret; | |
1027 | } | |
1028 | ||
f0690a25 GR |
1029 | static int tcpm_set_attached_state(struct tcpm_port *port, bool attached) |
1030 | { | |
1031 | return port->tcpc->set_roles(port->tcpc, attached, port->pwr_role, | |
1032 | port->data_role); | |
1033 | } | |
1034 | ||
1035 | static int tcpm_set_roles(struct tcpm_port *port, bool attached, | |
1036 | enum typec_role role, enum typec_data_role data) | |
1037 | { | |
2000016c | 1038 | enum typec_orientation orientation; |
c6962c29 | 1039 | enum usb_role usb_role; |
f0690a25 GR |
1040 | int ret; |
1041 | ||
2000016c HG |
1042 | if (port->polarity == TYPEC_POLARITY_CC1) |
1043 | orientation = TYPEC_ORIENTATION_NORMAL; | |
1044 | else | |
1045 | orientation = TYPEC_ORIENTATION_REVERSE; | |
1046 | ||
6ecc632d LJ |
1047 | if (port->typec_caps.data == TYPEC_PORT_DRD) { |
1048 | if (data == TYPEC_HOST) | |
1049 | usb_role = USB_ROLE_HOST; | |
1050 | else | |
1051 | usb_role = USB_ROLE_DEVICE; | |
1052 | } else if (port->typec_caps.data == TYPEC_PORT_DFP) { | |
1053 | if (data == TYPEC_HOST) { | |
1054 | if (role == TYPEC_SOURCE) | |
1055 | usb_role = USB_ROLE_HOST; | |
1056 | else | |
1057 | usb_role = USB_ROLE_NONE; | |
1058 | } else { | |
1059 | return -ENOTSUPP; | |
1060 | } | |
1061 | } else { | |
1062 | if (data == TYPEC_DEVICE) { | |
1063 | if (role == TYPEC_SINK) | |
1064 | usb_role = USB_ROLE_DEVICE; | |
1065 | else | |
1066 | usb_role = USB_ROLE_NONE; | |
1067 | } else { | |
1068 | return -ENOTSUPP; | |
1069 | } | |
1070 | } | |
c6962c29 | 1071 | |
e9576fe8 | 1072 | ret = tcpm_mux_set(port, TYPEC_STATE_USB, usb_role, orientation); |
f0690a25 GR |
1073 | if (ret < 0) |
1074 | return ret; | |
1075 | ||
1076 | ret = port->tcpc->set_roles(port->tcpc, attached, role, data); | |
1077 | if (ret < 0) | |
1078 | return ret; | |
1079 | ||
1080 | port->pwr_role = role; | |
1081 | port->data_role = data; | |
1082 | typec_set_data_role(port->typec_port, data); | |
1083 | typec_set_pwr_role(port->typec_port, role); | |
1084 | ||
1085 | return 0; | |
1086 | } | |
1087 | ||
1088 | static int tcpm_set_pwr_role(struct tcpm_port *port, enum typec_role role) | |
1089 | { | |
1090 | int ret; | |
1091 | ||
1092 | ret = port->tcpc->set_roles(port->tcpc, true, role, | |
1093 | port->data_role); | |
1094 | if (ret < 0) | |
1095 | return ret; | |
1096 | ||
1097 | port->pwr_role = role; | |
1098 | typec_set_pwr_role(port->typec_port, role); | |
1099 | ||
1100 | return 0; | |
1101 | } | |
1102 | ||
f75a1025 KT |
1103 | /* |
1104 | * Transform the PDO to be compliant to PD rev2.0. | |
1105 | * Return 0 if the PDO type is not defined in PD rev2.0. | |
1106 | * Otherwise, return the converted PDO. | |
1107 | */ | |
1108 | static u32 tcpm_forge_legacy_pdo(struct tcpm_port *port, u32 pdo, enum typec_role role) | |
1109 | { | |
1110 | switch (pdo_type(pdo)) { | |
1111 | case PDO_TYPE_FIXED: | |
1112 | if (role == TYPEC_SINK) | |
1113 | return pdo & ~PDO_FIXED_FRS_CURR_MASK; | |
1114 | else | |
1115 | return pdo & ~PDO_FIXED_UNCHUNK_EXT; | |
1116 | case PDO_TYPE_VAR: | |
1117 | case PDO_TYPE_BATT: | |
1118 | return pdo; | |
1119 | case PDO_TYPE_APDO: | |
1120 | default: | |
1121 | return 0; | |
1122 | } | |
1123 | } | |
1124 | ||
f0690a25 GR |
1125 | static int tcpm_pd_send_source_caps(struct tcpm_port *port) |
1126 | { | |
1127 | struct pd_message msg; | |
f75a1025 KT |
1128 | u32 pdo; |
1129 | unsigned int i, nr_pdo = 0; | |
f0690a25 GR |
1130 | |
1131 | memset(&msg, 0, sizeof(msg)); | |
f75a1025 KT |
1132 | |
1133 | for (i = 0; i < port->nr_src_pdo; i++) { | |
1134 | if (port->negotiated_rev >= PD_REV30) { | |
1135 | msg.payload[nr_pdo++] = cpu_to_le32(port->src_pdo[i]); | |
1136 | } else { | |
1137 | pdo = tcpm_forge_legacy_pdo(port, port->src_pdo[i], TYPEC_SOURCE); | |
1138 | if (pdo) | |
1139 | msg.payload[nr_pdo++] = cpu_to_le32(pdo); | |
1140 | } | |
1141 | } | |
1142 | ||
1143 | if (!nr_pdo) { | |
f0690a25 GR |
1144 | /* No source capabilities defined, sink only */ |
1145 | msg.header = PD_HEADER_LE(PD_CTRL_REJECT, | |
1146 | port->pwr_role, | |
1147 | port->data_role, | |
2eadc33f | 1148 | port->negotiated_rev, |
f0690a25 GR |
1149 | port->message_id, 0); |
1150 | } else { | |
1151 | msg.header = PD_HEADER_LE(PD_DATA_SOURCE_CAP, | |
1152 | port->pwr_role, | |
1153 | port->data_role, | |
2eadc33f | 1154 | port->negotiated_rev, |
f0690a25 | 1155 | port->message_id, |
f75a1025 | 1156 | nr_pdo); |
f0690a25 | 1157 | } |
f0690a25 GR |
1158 | |
1159 | return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg); | |
1160 | } | |
1161 | ||
1162 | static int tcpm_pd_send_sink_caps(struct tcpm_port *port) | |
1163 | { | |
1164 | struct pd_message msg; | |
f75a1025 KT |
1165 | u32 pdo; |
1166 | unsigned int i, nr_pdo = 0; | |
f0690a25 GR |
1167 | |
1168 | memset(&msg, 0, sizeof(msg)); | |
f75a1025 KT |
1169 | |
1170 | for (i = 0; i < port->nr_snk_pdo; i++) { | |
1171 | if (port->negotiated_rev >= PD_REV30) { | |
1172 | msg.payload[nr_pdo++] = cpu_to_le32(port->snk_pdo[i]); | |
1173 | } else { | |
1174 | pdo = tcpm_forge_legacy_pdo(port, port->snk_pdo[i], TYPEC_SINK); | |
1175 | if (pdo) | |
1176 | msg.payload[nr_pdo++] = cpu_to_le32(pdo); | |
1177 | } | |
1178 | } | |
1179 | ||
1180 | if (!nr_pdo) { | |
f0690a25 GR |
1181 | /* No sink capabilities defined, source only */ |
1182 | msg.header = PD_HEADER_LE(PD_CTRL_REJECT, | |
1183 | port->pwr_role, | |
1184 | port->data_role, | |
2eadc33f | 1185 | port->negotiated_rev, |
f0690a25 GR |
1186 | port->message_id, 0); |
1187 | } else { | |
1188 | msg.header = PD_HEADER_LE(PD_DATA_SINK_CAP, | |
1189 | port->pwr_role, | |
1190 | port->data_role, | |
2eadc33f | 1191 | port->negotiated_rev, |
f0690a25 | 1192 | port->message_id, |
f75a1025 | 1193 | nr_pdo); |
f0690a25 | 1194 | } |
f0690a25 GR |
1195 | |
1196 | return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg); | |
1197 | } | |
1198 | ||
3ed8e1c2 BJS |
1199 | static void mod_tcpm_delayed_work(struct tcpm_port *port, unsigned int delay_ms) |
1200 | { | |
1201 | if (delay_ms) { | |
1202 | hrtimer_start(&port->state_machine_timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL); | |
1203 | } else { | |
1204 | hrtimer_cancel(&port->state_machine_timer); | |
1205 | kthread_queue_work(port->wq, &port->state_machine); | |
1206 | } | |
1207 | } | |
1208 | ||
1209 | static void mod_vdm_delayed_work(struct tcpm_port *port, unsigned int delay_ms) | |
1210 | { | |
1211 | if (delay_ms) { | |
1212 | hrtimer_start(&port->vdm_state_machine_timer, ms_to_ktime(delay_ms), | |
1213 | HRTIMER_MODE_REL); | |
1214 | } else { | |
1215 | hrtimer_cancel(&port->vdm_state_machine_timer); | |
1216 | kthread_queue_work(port->wq, &port->vdm_state_machine); | |
1217 | } | |
1218 | } | |
1219 | ||
8dc4bd07 BJS |
1220 | static void mod_enable_frs_delayed_work(struct tcpm_port *port, unsigned int delay_ms) |
1221 | { | |
1222 | if (delay_ms) { | |
1223 | hrtimer_start(&port->enable_frs_timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL); | |
1224 | } else { | |
1225 | hrtimer_cancel(&port->enable_frs_timer); | |
1226 | kthread_queue_work(port->wq, &port->enable_frs); | |
1227 | } | |
1228 | } | |
1229 | ||
c34e85fa KT |
1230 | static void mod_send_discover_delayed_work(struct tcpm_port *port, unsigned int delay_ms) |
1231 | { | |
1232 | if (delay_ms) { | |
1233 | hrtimer_start(&port->send_discover_timer, ms_to_ktime(delay_ms), HRTIMER_MODE_REL); | |
1234 | } else { | |
1235 | hrtimer_cancel(&port->send_discover_timer); | |
1236 | kthread_queue_work(port->wq, &port->send_discover_work); | |
1237 | } | |
1238 | } | |
1239 | ||
f0690a25 GR |
1240 | static void tcpm_set_state(struct tcpm_port *port, enum tcpm_state state, |
1241 | unsigned int delay_ms) | |
1242 | { | |
1243 | if (delay_ms) { | |
0908c5ac KT |
1244 | tcpm_log(port, "pending state change %s -> %s @ %u ms [%s %s]", |
1245 | tcpm_states[port->state], tcpm_states[state], delay_ms, | |
1246 | pd_rev[port->negotiated_rev], tcpm_ams_str[port->ams]); | |
f0690a25 | 1247 | port->delayed_state = state; |
3ed8e1c2 BJS |
1248 | mod_tcpm_delayed_work(port, delay_ms); |
1249 | port->delayed_runtime = ktime_add(ktime_get(), ms_to_ktime(delay_ms)); | |
f0690a25 GR |
1250 | port->delay_ms = delay_ms; |
1251 | } else { | |
0908c5ac KT |
1252 | tcpm_log(port, "state change %s -> %s [%s %s]", |
1253 | tcpm_states[port->state], tcpm_states[state], | |
1254 | pd_rev[port->negotiated_rev], tcpm_ams_str[port->ams]); | |
f0690a25 GR |
1255 | port->delayed_state = INVALID_STATE; |
1256 | port->prev_state = port->state; | |
1257 | port->state = state; | |
1258 | /* | |
1259 | * Don't re-queue the state machine work item if we're currently | |
1260 | * in the state machine and we're immediately changing states. | |
1261 | * tcpm_state_machine_work() will continue running the state | |
1262 | * machine. | |
1263 | */ | |
1264 | if (!port->state_machine_running) | |
3ed8e1c2 | 1265 | mod_tcpm_delayed_work(port, 0); |
f0690a25 GR |
1266 | } |
1267 | } | |
1268 | ||
1269 | static void tcpm_set_state_cond(struct tcpm_port *port, enum tcpm_state state, | |
1270 | unsigned int delay_ms) | |
1271 | { | |
1272 | if (port->enter_state == port->state) | |
1273 | tcpm_set_state(port, state, delay_ms); | |
1274 | else | |
1275 | tcpm_log(port, | |
0908c5ac | 1276 | "skipped %sstate change %s -> %s [%u ms], context state %s [%s %s]", |
f0690a25 GR |
1277 | delay_ms ? "delayed " : "", |
1278 | tcpm_states[port->state], tcpm_states[state], | |
0908c5ac KT |
1279 | delay_ms, tcpm_states[port->enter_state], |
1280 | pd_rev[port->negotiated_rev], tcpm_ams_str[port->ams]); | |
f0690a25 GR |
1281 | } |
1282 | ||
1283 | static void tcpm_queue_message(struct tcpm_port *port, | |
1284 | enum pd_msg_request message) | |
1285 | { | |
1286 | port->queued_message = message; | |
3ed8e1c2 | 1287 | mod_tcpm_delayed_work(port, 0); |
f0690a25 GR |
1288 | } |
1289 | ||
0908c5ac KT |
1290 | static bool tcpm_vdm_ams(struct tcpm_port *port) |
1291 | { | |
1292 | switch (port->ams) { | |
1293 | case DISCOVER_IDENTITY: | |
1294 | case SOURCE_STARTUP_CABLE_PLUG_DISCOVER_IDENTITY: | |
1295 | case DISCOVER_SVIDS: | |
1296 | case DISCOVER_MODES: | |
1297 | case DFP_TO_UFP_ENTER_MODE: | |
1298 | case DFP_TO_UFP_EXIT_MODE: | |
1299 | case DFP_TO_CABLE_PLUG_ENTER_MODE: | |
1300 | case DFP_TO_CABLE_PLUG_EXIT_MODE: | |
1301 | case ATTENTION: | |
1302 | case UNSTRUCTURED_VDMS: | |
1303 | case STRUCTURED_VDMS: | |
1304 | break; | |
1305 | default: | |
1306 | return false; | |
1307 | } | |
1308 | ||
1309 | return true; | |
1310 | } | |
1311 | ||
1312 | static bool tcpm_ams_interruptible(struct tcpm_port *port) | |
1313 | { | |
1314 | switch (port->ams) { | |
1315 | /* Interruptible AMS */ | |
1316 | case NONE_AMS: | |
1317 | case SECURITY: | |
1318 | case FIRMWARE_UPDATE: | |
1319 | case DISCOVER_IDENTITY: | |
1320 | case SOURCE_STARTUP_CABLE_PLUG_DISCOVER_IDENTITY: | |
1321 | case DISCOVER_SVIDS: | |
1322 | case DISCOVER_MODES: | |
1323 | case DFP_TO_UFP_ENTER_MODE: | |
1324 | case DFP_TO_UFP_EXIT_MODE: | |
1325 | case DFP_TO_CABLE_PLUG_ENTER_MODE: | |
1326 | case DFP_TO_CABLE_PLUG_EXIT_MODE: | |
1327 | case UNSTRUCTURED_VDMS: | |
1328 | case STRUCTURED_VDMS: | |
1329 | case COUNTRY_INFO: | |
1330 | case COUNTRY_CODES: | |
1331 | break; | |
1332 | /* Non-Interruptible AMS */ | |
1333 | default: | |
1334 | if (port->in_ams) | |
1335 | return false; | |
1336 | break; | |
1337 | } | |
1338 | ||
1339 | return true; | |
1340 | } | |
1341 | ||
1342 | static int tcpm_ams_start(struct tcpm_port *port, enum tcpm_ams ams) | |
1343 | { | |
1344 | int ret = 0; | |
1345 | ||
1346 | tcpm_log(port, "AMS %s start", tcpm_ams_str[ams]); | |
1347 | ||
8dea75e1 KT |
1348 | if (!tcpm_ams_interruptible(port) && |
1349 | !(ams == HARD_RESET || ams == SOFT_RESET_AMS)) { | |
0908c5ac KT |
1350 | port->upcoming_state = INVALID_STATE; |
1351 | tcpm_log(port, "AMS %s not interruptible, aborting", | |
1352 | tcpm_ams_str[port->ams]); | |
1353 | return -EAGAIN; | |
1354 | } | |
1355 | ||
1356 | if (port->pwr_role == TYPEC_SOURCE) { | |
1357 | enum typec_cc_status cc_req = port->cc_req; | |
1358 | ||
1359 | port->ams = ams; | |
1360 | ||
1361 | if (ams == HARD_RESET) { | |
1362 | tcpm_set_cc(port, tcpm_rp_cc(port)); | |
1363 | tcpm_pd_transmit(port, TCPC_TX_HARD_RESET, NULL); | |
1364 | tcpm_set_state(port, HARD_RESET_START, 0); | |
1365 | return ret; | |
1366 | } else if (ams == SOFT_RESET_AMS) { | |
8dea75e1 | 1367 | if (!port->explicit_contract) |
0908c5ac | 1368 | tcpm_set_cc(port, tcpm_rp_cc(port)); |
8dea75e1 KT |
1369 | tcpm_set_state(port, SOFT_RESET_SEND, 0); |
1370 | return ret; | |
0908c5ac KT |
1371 | } else if (tcpm_vdm_ams(port)) { |
1372 | /* tSinkTx is enforced in vdm_run_state_machine */ | |
1373 | if (port->negotiated_rev >= PD_REV30) | |
1374 | tcpm_set_cc(port, SINK_TX_NG); | |
1375 | return ret; | |
1376 | } | |
1377 | ||
1378 | if (port->negotiated_rev >= PD_REV30) | |
1379 | tcpm_set_cc(port, SINK_TX_NG); | |
1380 | ||
1381 | switch (port->state) { | |
1382 | case SRC_READY: | |
1383 | case SRC_STARTUP: | |
1384 | case SRC_SOFT_RESET_WAIT_SNK_TX: | |
1385 | case SOFT_RESET: | |
1386 | case SOFT_RESET_SEND: | |
1387 | if (port->negotiated_rev >= PD_REV30) | |
1388 | tcpm_set_state(port, AMS_START, | |
1389 | cc_req == SINK_TX_OK ? | |
1390 | PD_T_SINK_TX : 0); | |
1391 | else | |
1392 | tcpm_set_state(port, AMS_START, 0); | |
1393 | break; | |
1394 | default: | |
1395 | if (port->negotiated_rev >= PD_REV30) | |
1396 | tcpm_set_state(port, SRC_READY, | |
1397 | cc_req == SINK_TX_OK ? | |
1398 | PD_T_SINK_TX : 0); | |
1399 | else | |
1400 | tcpm_set_state(port, SRC_READY, 0); | |
1401 | break; | |
1402 | } | |
1403 | } else { | |
1404 | if (port->negotiated_rev >= PD_REV30 && | |
1405 | !tcpm_sink_tx_ok(port) && | |
1406 | ams != SOFT_RESET_AMS && | |
1407 | ams != HARD_RESET) { | |
1408 | port->upcoming_state = INVALID_STATE; | |
1409 | tcpm_log(port, "Sink TX No Go"); | |
1410 | return -EAGAIN; | |
1411 | } | |
1412 | ||
1413 | port->ams = ams; | |
1414 | ||
1415 | if (ams == HARD_RESET) { | |
1416 | tcpm_pd_transmit(port, TCPC_TX_HARD_RESET, NULL); | |
1417 | tcpm_set_state(port, HARD_RESET_START, 0); | |
1418 | return ret; | |
1419 | } else if (tcpm_vdm_ams(port)) { | |
1420 | return ret; | |
1421 | } | |
1422 | ||
1423 | if (port->state == SNK_READY || | |
1424 | port->state == SNK_SOFT_RESET) | |
1425 | tcpm_set_state(port, AMS_START, 0); | |
1426 | else | |
1427 | tcpm_set_state(port, SNK_READY, 0); | |
1428 | } | |
1429 | ||
1430 | return ret; | |
1431 | } | |
1432 | ||
f0690a25 GR |
1433 | /* |
1434 | * VDM/VDO handling functions | |
1435 | */ | |
1436 | static void tcpm_queue_vdm(struct tcpm_port *port, const u32 header, | |
1437 | const u32 *data, int cnt) | |
1438 | { | |
03eafcfb HG |
1439 | WARN_ON(!mutex_is_locked(&port->lock)); |
1440 | ||
754498c1 HG |
1441 | /* Make sure we are not still processing a previous VDM packet */ |
1442 | WARN_ON(port->vdm_state > VDM_STATE_DONE); | |
1443 | ||
f0690a25 GR |
1444 | port->vdo_count = cnt + 1; |
1445 | port->vdo_data[0] = header; | |
1446 | memcpy(&port->vdo_data[1], data, sizeof(u32) * cnt); | |
1447 | /* Set ready, vdm state machine will actually send */ | |
1448 | port->vdm_retries = 0; | |
1449 | port->vdm_state = VDM_STATE_READY; | |
ef52b4a9 | 1450 | port->vdm_sm_running = true; |
5f2b8d87 | 1451 | |
3ed8e1c2 | 1452 | mod_vdm_delayed_work(port, 0); |
f0690a25 GR |
1453 | } |
1454 | ||
03eafcfb HG |
1455 | static void tcpm_queue_vdm_unlocked(struct tcpm_port *port, const u32 header, |
1456 | const u32 *data, int cnt) | |
1457 | { | |
1458 | mutex_lock(&port->lock); | |
1459 | tcpm_queue_vdm(port, header, data, cnt); | |
1460 | mutex_unlock(&port->lock); | |
1461 | } | |
1462 | ||
8afe9a35 | 1463 | static void svdm_consume_identity(struct tcpm_port *port, const u32 *p, int cnt) |
f0690a25 | 1464 | { |
8afe9a35 HG |
1465 | u32 vdo = p[VDO_INDEX_IDH]; |
1466 | u32 product = p[VDO_INDEX_PRODUCT]; | |
f0690a25 GR |
1467 | |
1468 | memset(&port->mode_data, 0, sizeof(port->mode_data)); | |
1469 | ||
f0690a25 | 1470 | port->partner_ident.id_header = vdo; |
8afe9a35 | 1471 | port->partner_ident.cert_stat = p[VDO_INDEX_CSTAT]; |
f0690a25 GR |
1472 | port->partner_ident.product = product; |
1473 | ||
1474 | typec_partner_set_identity(port->partner); | |
1475 | ||
1476 | tcpm_log(port, "Identity: %04x:%04x.%04x", | |
1477 | PD_IDH_VID(vdo), | |
1478 | PD_PRODUCT_PID(product), product & 0xffff); | |
1479 | } | |
1480 | ||
8afe9a35 | 1481 | static bool svdm_consume_svids(struct tcpm_port *port, const u32 *p, int cnt) |
f0690a25 GR |
1482 | { |
1483 | struct pd_mode_data *pmdata = &port->mode_data; | |
1484 | int i; | |
1485 | ||
1486 | for (i = 1; i < cnt; i++) { | |
f0690a25 GR |
1487 | u16 svid; |
1488 | ||
8afe9a35 | 1489 | svid = (p[i] >> 16) & 0xffff; |
f0690a25 GR |
1490 | if (!svid) |
1491 | return false; | |
1492 | ||
1493 | if (pmdata->nsvids >= SVID_DISCOVERY_MAX) | |
1494 | goto abort; | |
1495 | ||
1496 | pmdata->svids[pmdata->nsvids++] = svid; | |
1497 | tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid); | |
1498 | ||
8afe9a35 | 1499 | svid = p[i] & 0xffff; |
f0690a25 GR |
1500 | if (!svid) |
1501 | return false; | |
1502 | ||
1503 | if (pmdata->nsvids >= SVID_DISCOVERY_MAX) | |
1504 | goto abort; | |
1505 | ||
1506 | pmdata->svids[pmdata->nsvids++] = svid; | |
1507 | tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid); | |
1508 | } | |
1509 | return true; | |
1510 | abort: | |
1511 | tcpm_log(port, "SVID_DISCOVERY_MAX(%d) too low!", SVID_DISCOVERY_MAX); | |
1512 | return false; | |
1513 | } | |
1514 | ||
8afe9a35 | 1515 | static void svdm_consume_modes(struct tcpm_port *port, const u32 *p, int cnt) |
f0690a25 GR |
1516 | { |
1517 | struct pd_mode_data *pmdata = &port->mode_data; | |
1518 | struct typec_altmode_desc *paltmode; | |
f0690a25 GR |
1519 | int i; |
1520 | ||
1521 | if (pmdata->altmodes >= ARRAY_SIZE(port->partner_altmode)) { | |
1522 | /* Already logged in svdm_consume_svids() */ | |
1523 | return; | |
1524 | } | |
1525 | ||
4ab8c18d HK |
1526 | for (i = 1; i < cnt; i++) { |
1527 | paltmode = &pmdata->altmode_desc[pmdata->altmodes]; | |
1528 | memset(paltmode, 0, sizeof(*paltmode)); | |
1529 | ||
1530 | paltmode->svid = pmdata->svids[pmdata->svid_index]; | |
1531 | paltmode->mode = i; | |
8afe9a35 | 1532 | paltmode->vdo = p[i]; |
4ab8c18d HK |
1533 | |
1534 | tcpm_log(port, " Alternate mode %d: SVID 0x%04x, VDO %d: 0x%08x", | |
1535 | pmdata->altmodes, paltmode->svid, | |
1536 | paltmode->mode, paltmode->vdo); | |
1537 | ||
4ab8c18d | 1538 | pmdata->altmodes++; |
f0690a25 | 1539 | } |
f0690a25 GR |
1540 | } |
1541 | ||
e9576fe8 HK |
1542 | static void tcpm_register_partner_altmodes(struct tcpm_port *port) |
1543 | { | |
1544 | struct pd_mode_data *modep = &port->mode_data; | |
1545 | struct typec_altmode *altmode; | |
1546 | int i; | |
1547 | ||
1548 | for (i = 0; i < modep->altmodes; i++) { | |
1549 | altmode = typec_partner_register_altmode(port->partner, | |
1550 | &modep->altmode_desc[i]); | |
d25d61be | 1551 | if (IS_ERR(altmode)) { |
e9576fe8 HK |
1552 | tcpm_log(port, "Failed to register partner SVID 0x%04x", |
1553 | modep->altmode_desc[i].svid); | |
d25d61be KT |
1554 | altmode = NULL; |
1555 | } | |
e9576fe8 HK |
1556 | port->partner_altmode[i] = altmode; |
1557 | } | |
1558 | } | |
1559 | ||
f0690a25 GR |
1560 | #define supports_modal(port) PD_IDH_MODAL_SUPP((port)->partner_ident.id_header) |
1561 | ||
95b4d51c HG |
1562 | static int tcpm_pd_svdm(struct tcpm_port *port, struct typec_altmode *adev, |
1563 | const u32 *p, int cnt, u32 *response, | |
1564 | enum adev_actions *adev_action) | |
f0690a25 | 1565 | { |
5e1d4c49 | 1566 | struct typec_port *typec = port->typec_port; |
e9576fe8 | 1567 | struct typec_altmode *pdev; |
f0690a25 | 1568 | struct pd_mode_data *modep; |
5e1d4c49 | 1569 | int svdm_version; |
f0690a25 | 1570 | int rlen = 0; |
e9576fe8 HK |
1571 | int cmd_type; |
1572 | int cmd; | |
193a6801 | 1573 | int i; |
f0690a25 | 1574 | |
e9576fe8 HK |
1575 | cmd_type = PD_VDO_CMDT(p[0]); |
1576 | cmd = PD_VDO_CMD(p[0]); | |
1577 | ||
f0690a25 | 1578 | tcpm_log(port, "Rx VDM cmd 0x%x type %d cmd %d len %d", |
e9576fe8 | 1579 | p[0], cmd_type, cmd, cnt); |
f0690a25 GR |
1580 | |
1581 | modep = &port->mode_data; | |
1582 | ||
e9576fe8 HK |
1583 | pdev = typec_match_altmode(port->partner_altmode, ALTMODE_DISCOVERY_MAX, |
1584 | PD_VDO_VID(p[0]), PD_VDO_OPOS(p[0])); | |
1585 | ||
5e1d4c49 KT |
1586 | svdm_version = typec_get_negotiated_svdm_version(typec); |
1587 | if (svdm_version < 0) | |
1588 | return 0; | |
1589 | ||
f0690a25 GR |
1590 | switch (cmd_type) { |
1591 | case CMDT_INIT: | |
1592 | switch (cmd) { | |
1593 | case CMD_DISCOVER_IDENT: | |
8dea75e1 KT |
1594 | if (PD_VDO_VID(p[0]) != USB_SID_PD) |
1595 | break; | |
1596 | ||
f41bfc7e | 1597 | if (PD_VDO_SVDM_VER(p[0]) < svdm_version) { |
5e1d4c49 KT |
1598 | typec_partner_set_svdm_version(port->partner, |
1599 | PD_VDO_SVDM_VER(p[0])); | |
f41bfc7e KT |
1600 | svdm_version = PD_VDO_SVDM_VER(p[0]); |
1601 | } | |
0bc3ee92 | 1602 | |
80137c18 | 1603 | port->ams = DISCOVER_IDENTITY; |
f41bfc7e KT |
1604 | /* |
1605 | * PD2.0 Spec 6.10.3: respond with NAK as DFP (data host) | |
1606 | * PD3.1 Spec 6.4.4.2.5.1: respond with NAK if "invalid field" or | |
1607 | * "wrong configuation" or "Unrecognized" | |
1608 | */ | |
1609 | if ((port->data_role == TYPEC_DEVICE || svdm_version >= SVDM_VER_2_0) && | |
193a6801 | 1610 | port->nr_snk_vdo) { |
7ac50510 KT |
1611 | if (svdm_version < SVDM_VER_2_0) { |
1612 | for (i = 0; i < port->nr_snk_vdo_v1; i++) | |
1613 | response[i + 1] = port->snk_vdo_v1[i]; | |
1614 | rlen = port->nr_snk_vdo_v1 + 1; | |
1615 | ||
1616 | } else { | |
1617 | for (i = 0; i < port->nr_snk_vdo; i++) | |
1618 | response[i + 1] = port->snk_vdo[i]; | |
1619 | rlen = port->nr_snk_vdo + 1; | |
1620 | } | |
193a6801 | 1621 | } |
f0690a25 GR |
1622 | break; |
1623 | case CMD_DISCOVER_SVID: | |
80137c18 | 1624 | port->ams = DISCOVER_SVIDS; |
f0690a25 GR |
1625 | break; |
1626 | case CMD_DISCOVER_MODES: | |
80137c18 | 1627 | port->ams = DISCOVER_MODES; |
f0690a25 GR |
1628 | break; |
1629 | case CMD_ENTER_MODE: | |
80137c18 | 1630 | port->ams = DFP_TO_UFP_ENTER_MODE; |
f0690a25 GR |
1631 | break; |
1632 | case CMD_EXIT_MODE: | |
80137c18 | 1633 | port->ams = DFP_TO_UFP_EXIT_MODE; |
f0690a25 GR |
1634 | break; |
1635 | case CMD_ATTENTION: | |
e9576fe8 | 1636 | /* Attention command does not have response */ |
95b4d51c | 1637 | *adev_action = ADEV_ATTENTION; |
e9576fe8 | 1638 | return 0; |
f0690a25 GR |
1639 | default: |
1640 | break; | |
1641 | } | |
1642 | if (rlen >= 1) { | |
e9576fe8 | 1643 | response[0] = p[0] | VDO_CMDT(CMDT_RSP_ACK); |
f0690a25 | 1644 | } else if (rlen == 0) { |
e9576fe8 | 1645 | response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK); |
f0690a25 GR |
1646 | rlen = 1; |
1647 | } else { | |
e9576fe8 | 1648 | response[0] = p[0] | VDO_CMDT(CMDT_RSP_BUSY); |
f0690a25 GR |
1649 | rlen = 1; |
1650 | } | |
5e1d4c49 KT |
1651 | response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) | |
1652 | (VDO_SVDM_VERS(typec_get_negotiated_svdm_version(typec))); | |
f0690a25 GR |
1653 | break; |
1654 | case CMDT_RSP_ACK: | |
1655 | /* silently drop message if we are not connected */ | |
cf6e06cd | 1656 | if (IS_ERR_OR_NULL(port->partner)) |
f0690a25 GR |
1657 | break; |
1658 | ||
0908c5ac KT |
1659 | tcpm_ams_finish(port); |
1660 | ||
f0690a25 GR |
1661 | switch (cmd) { |
1662 | case CMD_DISCOVER_IDENT: | |
5e1d4c49 KT |
1663 | if (PD_VDO_SVDM_VER(p[0]) < svdm_version) |
1664 | typec_partner_set_svdm_version(port->partner, | |
1665 | PD_VDO_SVDM_VER(p[0])); | |
f0690a25 | 1666 | /* 6.4.4.3.1 */ |
8afe9a35 | 1667 | svdm_consume_identity(port, p, cnt); |
5e1d4c49 KT |
1668 | response[0] = VDO(USB_SID_PD, 1, typec_get_negotiated_svdm_version(typec), |
1669 | CMD_DISCOVER_SVID); | |
f0690a25 GR |
1670 | rlen = 1; |
1671 | break; | |
1672 | case CMD_DISCOVER_SVID: | |
1673 | /* 6.4.4.3.2 */ | |
8afe9a35 | 1674 | if (svdm_consume_svids(port, p, cnt)) { |
5e1d4c49 | 1675 | response[0] = VDO(USB_SID_PD, 1, svdm_version, CMD_DISCOVER_SVID); |
f0690a25 GR |
1676 | rlen = 1; |
1677 | } else if (modep->nsvids && supports_modal(port)) { | |
5e1d4c49 | 1678 | response[0] = VDO(modep->svids[0], 1, svdm_version, |
f0690a25 GR |
1679 | CMD_DISCOVER_MODES); |
1680 | rlen = 1; | |
1681 | } | |
1682 | break; | |
1683 | case CMD_DISCOVER_MODES: | |
1684 | /* 6.4.4.3.3 */ | |
8afe9a35 | 1685 | svdm_consume_modes(port, p, cnt); |
f0690a25 GR |
1686 | modep->svid_index++; |
1687 | if (modep->svid_index < modep->nsvids) { | |
e9576fe8 | 1688 | u16 svid = modep->svids[modep->svid_index]; |
5e1d4c49 | 1689 | response[0] = VDO(svid, 1, svdm_version, CMD_DISCOVER_MODES); |
f0690a25 GR |
1690 | rlen = 1; |
1691 | } else { | |
e9576fe8 | 1692 | tcpm_register_partner_altmodes(port); |
f0690a25 GR |
1693 | } |
1694 | break; | |
1695 | case CMD_ENTER_MODE: | |
88d02c9b GR |
1696 | if (adev && pdev) { |
1697 | typec_altmode_update_active(pdev, true); | |
95b4d51c | 1698 | *adev_action = ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL; |
e9576fe8 HK |
1699 | } |
1700 | return 0; | |
1701 | case CMD_EXIT_MODE: | |
88d02c9b GR |
1702 | if (adev && pdev) { |
1703 | typec_altmode_update_active(pdev, false); | |
88d02c9b | 1704 | /* Back to USB Operation */ |
95b4d51c HG |
1705 | *adev_action = ADEV_NOTIFY_USB_AND_QUEUE_VDM; |
1706 | return 0; | |
88d02c9b | 1707 | } |
e9576fe8 | 1708 | break; |
8dea75e1 KT |
1709 | case VDO_CMD_VENDOR(0) ... VDO_CMD_VENDOR(15): |
1710 | break; | |
e9576fe8 | 1711 | default: |
8dea75e1 KT |
1712 | /* Unrecognized SVDM */ |
1713 | response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK); | |
1714 | rlen = 1; | |
5e1d4c49 KT |
1715 | response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) | |
1716 | (VDO_SVDM_VERS(svdm_version)); | |
e9576fe8 HK |
1717 | break; |
1718 | } | |
1719 | break; | |
1720 | case CMDT_RSP_NAK: | |
0908c5ac | 1721 | tcpm_ams_finish(port); |
e9576fe8 | 1722 | switch (cmd) { |
8dea75e1 KT |
1723 | case CMD_DISCOVER_IDENT: |
1724 | case CMD_DISCOVER_SVID: | |
1725 | case CMD_DISCOVER_MODES: | |
1726 | case VDO_CMD_VENDOR(0) ... VDO_CMD_VENDOR(15): | |
1727 | break; | |
e9576fe8 HK |
1728 | case CMD_ENTER_MODE: |
1729 | /* Back to USB Operation */ | |
95b4d51c HG |
1730 | *adev_action = ADEV_NOTIFY_USB_AND_QUEUE_VDM; |
1731 | return 0; | |
f0690a25 | 1732 | default: |
8dea75e1 KT |
1733 | /* Unrecognized SVDM */ |
1734 | response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK); | |
1735 | rlen = 1; | |
5e1d4c49 KT |
1736 | response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) | |
1737 | (VDO_SVDM_VERS(svdm_version)); | |
f0690a25 GR |
1738 | break; |
1739 | } | |
1740 | break; | |
1741 | default: | |
8dea75e1 KT |
1742 | response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK); |
1743 | rlen = 1; | |
5e1d4c49 KT |
1744 | response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) | |
1745 | (VDO_SVDM_VERS(svdm_version)); | |
f0690a25 GR |
1746 | break; |
1747 | } | |
1748 | ||
e9576fe8 | 1749 | /* Informing the alternate mode drivers about everything */ |
95b4d51c | 1750 | *adev_action = ADEV_QUEUE_VDM; |
f0690a25 GR |
1751 | return rlen; |
1752 | } | |
1753 | ||
5571ea31 HG |
1754 | static void tcpm_pd_handle_msg(struct tcpm_port *port, |
1755 | enum pd_msg_request message, | |
1756 | enum tcpm_ams ams); | |
1757 | ||
f0690a25 GR |
1758 | static void tcpm_handle_vdm_request(struct tcpm_port *port, |
1759 | const __le32 *payload, int cnt) | |
1760 | { | |
95b4d51c HG |
1761 | enum adev_actions adev_action = ADEV_NONE; |
1762 | struct typec_altmode *adev; | |
8afe9a35 | 1763 | u32 p[PD_MAX_PAYLOAD]; |
f0690a25 | 1764 | u32 response[8] = { }; |
8afe9a35 HG |
1765 | int i, rlen = 0; |
1766 | ||
1767 | for (i = 0; i < cnt; i++) | |
1768 | p[i] = le32_to_cpu(payload[i]); | |
f0690a25 | 1769 | |
95b4d51c HG |
1770 | adev = typec_match_altmode(port->port_altmode, ALTMODE_DISCOVERY_MAX, |
1771 | PD_VDO_VID(p[0]), PD_VDO_OPOS(p[0])); | |
1772 | ||
f0690a25 GR |
1773 | if (port->vdm_state == VDM_STATE_BUSY) { |
1774 | /* If UFP responded busy retry after timeout */ | |
8afe9a35 | 1775 | if (PD_VDO_CMDT(p[0]) == CMDT_RSP_BUSY) { |
f0690a25 | 1776 | port->vdm_state = VDM_STATE_WAIT_RSP_BUSY; |
8afe9a35 | 1777 | port->vdo_retry = (p[0] & ~VDO_CMDT_MASK) | |
f0690a25 | 1778 | CMDT_INIT; |
3ed8e1c2 | 1779 | mod_vdm_delayed_work(port, PD_T_VDM_BUSY); |
f0690a25 GR |
1780 | return; |
1781 | } | |
1782 | port->vdm_state = VDM_STATE_DONE; | |
1783 | } | |
1784 | ||
5571ea31 | 1785 | if (PD_VDO_SVDM(p[0]) && (adev || tcpm_vdm_ams(port) || port->nr_snk_vdo)) { |
ef52b4a9 KT |
1786 | /* |
1787 | * Here a SVDM is received (INIT or RSP or unknown). Set the vdm_sm_running in | |
1788 | * advance because we are dropping the lock but may send VDMs soon. | |
1789 | * For the cases of INIT received: | |
1790 | * - If no response to send, it will be cleared later in this function. | |
1791 | * - If there are responses to send, it will be cleared in the state machine. | |
1792 | * For the cases of RSP received: | |
1793 | * - If no further INIT to send, it will be cleared later in this function. | |
1794 | * - Otherwise, it will be cleared in the state machine if timeout or it will go | |
1795 | * back here until no further INIT to send. | |
1796 | * For the cases of unknown type received: | |
1797 | * - We will send NAK and the flag will be cleared in the state machine. | |
1798 | */ | |
1799 | port->vdm_sm_running = true; | |
95b4d51c | 1800 | rlen = tcpm_pd_svdm(port, adev, p, cnt, response, &adev_action); |
8dea75e1 KT |
1801 | } else { |
1802 | if (port->negotiated_rev >= PD_REV30) | |
5571ea31 | 1803 | tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS); |
8dea75e1 | 1804 | } |
95b4d51c | 1805 | |
a37241d8 HG |
1806 | /* |
1807 | * We are done with any state stored in the port struct now, except | |
1808 | * for any port struct changes done by the tcpm_queue_vdm() call | |
1809 | * below, which is a separate operation. | |
1810 | * | |
1811 | * So we can safely release the lock here; and we MUST release the | |
1812 | * lock here to avoid an AB BA lock inversion: | |
1813 | * | |
1814 | * If we keep the lock here then the lock ordering in this path is: | |
1815 | * 1. tcpm_pd_rx_handler take the tcpm port lock | |
1816 | * 2. One of the typec_altmode_* calls below takes the alt-mode's lock | |
1817 | * | |
1818 | * And we also have this ordering: | |
1819 | * 1. alt-mode driver takes the alt-mode's lock | |
1820 | * 2. alt-mode driver calls tcpm_altmode_enter which takes the | |
1821 | * tcpm port lock | |
1822 | * | |
1823 | * Dropping our lock here avoids this. | |
1824 | */ | |
1825 | mutex_unlock(&port->lock); | |
1826 | ||
95b4d51c HG |
1827 | if (adev) { |
1828 | switch (adev_action) { | |
1829 | case ADEV_NONE: | |
1830 | break; | |
1831 | case ADEV_NOTIFY_USB_AND_QUEUE_VDM: | |
1832 | WARN_ON(typec_altmode_notify(adev, TYPEC_STATE_USB, NULL)); | |
1833 | typec_altmode_vdm(adev, p[0], &p[1], cnt); | |
1834 | break; | |
1835 | case ADEV_QUEUE_VDM: | |
1836 | typec_altmode_vdm(adev, p[0], &p[1], cnt); | |
1837 | break; | |
1838 | case ADEV_QUEUE_VDM_SEND_EXIT_MODE_ON_FAIL: | |
1839 | if (typec_altmode_vdm(adev, p[0], &p[1], cnt)) { | |
5e1d4c49 KT |
1840 | int svdm_version = typec_get_negotiated_svdm_version( |
1841 | port->typec_port); | |
1842 | if (svdm_version < 0) | |
1843 | break; | |
1844 | ||
1845 | response[0] = VDO(adev->svid, 1, svdm_version, | |
1846 | CMD_EXIT_MODE); | |
95b4d51c HG |
1847 | response[0] |= VDO_OPOS(adev->mode); |
1848 | rlen = 1; | |
1849 | } | |
1850 | break; | |
1851 | case ADEV_ATTENTION: | |
1852 | typec_altmode_attention(adev, p[1]); | |
1853 | break; | |
1854 | } | |
1855 | } | |
f0690a25 | 1856 | |
a37241d8 HG |
1857 | /* |
1858 | * We must re-take the lock here to balance the unlock in | |
1859 | * tcpm_pd_rx_handler, note that no changes, other then the | |
1860 | * tcpm_queue_vdm call, are made while the lock is held again. | |
1861 | * All that is done after the call is unwinding the call stack until | |
1862 | * we return to tcpm_pd_rx_handler and do the unlock there. | |
1863 | */ | |
1864 | mutex_lock(&port->lock); | |
1865 | ||
5f2b8d87 | 1866 | if (rlen > 0) |
f0690a25 | 1867 | tcpm_queue_vdm(port, response[0], &response[1], rlen - 1); |
ef52b4a9 KT |
1868 | else |
1869 | port->vdm_sm_running = false; | |
f0690a25 GR |
1870 | } |
1871 | ||
1872 | static void tcpm_send_vdm(struct tcpm_port *port, u32 vid, int cmd, | |
1873 | const u32 *data, int count) | |
1874 | { | |
5e1d4c49 | 1875 | int svdm_version = typec_get_negotiated_svdm_version(port->typec_port); |
f0690a25 GR |
1876 | u32 header; |
1877 | ||
5e1d4c49 KT |
1878 | if (svdm_version < 0) |
1879 | return; | |
1880 | ||
f0690a25 GR |
1881 | if (WARN_ON(count > VDO_MAX_SIZE - 1)) |
1882 | count = VDO_MAX_SIZE - 1; | |
1883 | ||
1884 | /* set VDM header with VID & CMD */ | |
1885 | header = VDO(vid, ((vid & USB_SID_PD) == USB_SID_PD) ? | |
5e1d4c49 KT |
1886 | 1 : (PD_VDO_CMD(cmd) <= CMD_ATTENTION), |
1887 | svdm_version, cmd); | |
f0690a25 | 1888 | tcpm_queue_vdm(port, header, data, count); |
f0690a25 GR |
1889 | } |
1890 | ||
1891 | static unsigned int vdm_ready_timeout(u32 vdm_hdr) | |
1892 | { | |
1893 | unsigned int timeout; | |
1894 | int cmd = PD_VDO_CMD(vdm_hdr); | |
1895 | ||
1896 | /* its not a structured VDM command */ | |
1897 | if (!PD_VDO_SVDM(vdm_hdr)) | |
1898 | return PD_T_VDM_UNSTRUCTURED; | |
1899 | ||
1900 | switch (PD_VDO_CMDT(vdm_hdr)) { | |
1901 | case CMDT_INIT: | |
1902 | if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE) | |
1903 | timeout = PD_T_VDM_WAIT_MODE_E; | |
1904 | else | |
1905 | timeout = PD_T_VDM_SNDR_RSP; | |
1906 | break; | |
1907 | default: | |
1908 | if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE) | |
1909 | timeout = PD_T_VDM_E_MODE; | |
1910 | else | |
1911 | timeout = PD_T_VDM_RCVR_RSP; | |
1912 | break; | |
1913 | } | |
1914 | return timeout; | |
1915 | } | |
1916 | ||
1917 | static void vdm_run_state_machine(struct tcpm_port *port) | |
1918 | { | |
1919 | struct pd_message msg; | |
0908c5ac KT |
1920 | int i, res = 0; |
1921 | u32 vdo_hdr = port->vdo_data[0]; | |
f0690a25 GR |
1922 | |
1923 | switch (port->vdm_state) { | |
1924 | case VDM_STATE_READY: | |
1925 | /* Only transmit VDM if attached */ | |
1926 | if (!port->attached) { | |
1927 | port->vdm_state = VDM_STATE_ERR_BUSY; | |
1928 | break; | |
1929 | } | |
1930 | ||
1931 | /* | |
1932 | * if there's traffic or we're not in PDO ready state don't send | |
1933 | * a VDM. | |
1934 | */ | |
ef52b4a9 KT |
1935 | if (port->state != SRC_READY && port->state != SNK_READY) { |
1936 | port->vdm_sm_running = false; | |
f0690a25 | 1937 | break; |
ef52b4a9 | 1938 | } |
f0690a25 | 1939 | |
0908c5ac KT |
1940 | /* TODO: AMS operation for Unstructured VDM */ |
1941 | if (PD_VDO_SVDM(vdo_hdr) && PD_VDO_CMDT(vdo_hdr) == CMDT_INIT) { | |
1942 | switch (PD_VDO_CMD(vdo_hdr)) { | |
1943 | case CMD_DISCOVER_IDENT: | |
1944 | res = tcpm_ams_start(port, DISCOVER_IDENTITY); | |
8d3a0578 KT |
1945 | if (res == 0) |
1946 | port->send_discover = false; | |
c34e85fa KT |
1947 | else if (res == -EAGAIN) |
1948 | mod_send_discover_delayed_work(port, | |
1949 | SEND_DISCOVER_RETRY_MS); | |
0908c5ac KT |
1950 | break; |
1951 | case CMD_DISCOVER_SVID: | |
1952 | res = tcpm_ams_start(port, DISCOVER_SVIDS); | |
1953 | break; | |
1954 | case CMD_DISCOVER_MODES: | |
1955 | res = tcpm_ams_start(port, DISCOVER_MODES); | |
1956 | break; | |
1957 | case CMD_ENTER_MODE: | |
1958 | res = tcpm_ams_start(port, DFP_TO_UFP_ENTER_MODE); | |
1959 | break; | |
1960 | case CMD_EXIT_MODE: | |
1961 | res = tcpm_ams_start(port, DFP_TO_UFP_EXIT_MODE); | |
1962 | break; | |
1963 | case CMD_ATTENTION: | |
1964 | res = tcpm_ams_start(port, ATTENTION); | |
1965 | break; | |
1966 | case VDO_CMD_VENDOR(0) ... VDO_CMD_VENDOR(15): | |
1967 | res = tcpm_ams_start(port, STRUCTURED_VDMS); | |
1968 | break; | |
1969 | default: | |
1970 | res = -EOPNOTSUPP; | |
1971 | break; | |
1972 | } | |
f0690a25 | 1973 | |
8d3a0578 | 1974 | if (res < 0) { |
c34e85fa | 1975 | port->vdm_state = VDM_STATE_ERR_BUSY; |
0908c5ac | 1976 | return; |
8d3a0578 | 1977 | } |
f0690a25 | 1978 | } |
0908c5ac KT |
1979 | |
1980 | port->vdm_state = VDM_STATE_SEND_MESSAGE; | |
1981 | mod_vdm_delayed_work(port, (port->negotiated_rev >= PD_REV30 && | |
1982 | port->pwr_role == TYPEC_SOURCE && | |
1983 | PD_VDO_SVDM(vdo_hdr) && | |
1984 | PD_VDO_CMDT(vdo_hdr) == CMDT_INIT) ? | |
1985 | PD_T_SINK_TX : 0); | |
f0690a25 GR |
1986 | break; |
1987 | case VDM_STATE_WAIT_RSP_BUSY: | |
1988 | port->vdo_data[0] = port->vdo_retry; | |
1989 | port->vdo_count = 1; | |
1990 | port->vdm_state = VDM_STATE_READY; | |
f1fbd950 | 1991 | tcpm_ams_finish(port); |
f0690a25 GR |
1992 | break; |
1993 | case VDM_STATE_BUSY: | |
1994 | port->vdm_state = VDM_STATE_ERR_TMOUT; | |
0908c5ac KT |
1995 | if (port->ams != NONE_AMS) |
1996 | tcpm_ams_finish(port); | |
f0690a25 GR |
1997 | break; |
1998 | case VDM_STATE_ERR_SEND: | |
1999 | /* | |
2000 | * A partner which does not support USB PD will not reply, | |
2001 | * so this is not a fatal error. At the same time, some | |
2002 | * devices may not return GoodCRC under some circumstances, | |
2003 | * so we need to retry. | |
2004 | */ | |
2005 | if (port->vdm_retries < 3) { | |
2006 | tcpm_log(port, "VDM Tx error, retry"); | |
2007 | port->vdm_retries++; | |
2008 | port->vdm_state = VDM_STATE_READY; | |
5ab14ab1 KT |
2009 | if (PD_VDO_SVDM(vdo_hdr) && PD_VDO_CMDT(vdo_hdr) == CMDT_INIT) |
2010 | tcpm_ams_finish(port); | |
2011 | } else { | |
0908c5ac KT |
2012 | tcpm_ams_finish(port); |
2013 | } | |
2014 | break; | |
2015 | case VDM_STATE_SEND_MESSAGE: | |
2016 | /* Prepare and send VDM */ | |
2017 | memset(&msg, 0, sizeof(msg)); | |
2018 | msg.header = PD_HEADER_LE(PD_DATA_VENDOR_DEF, | |
2019 | port->pwr_role, | |
2020 | port->data_role, | |
2021 | port->negotiated_rev, | |
2022 | port->message_id, port->vdo_count); | |
2023 | for (i = 0; i < port->vdo_count; i++) | |
2024 | msg.payload[i] = cpu_to_le32(port->vdo_data[i]); | |
2025 | res = tcpm_pd_transmit(port, TCPC_TX_SOP, &msg); | |
2026 | if (res < 0) { | |
2027 | port->vdm_state = VDM_STATE_ERR_SEND; | |
2028 | } else { | |
2029 | unsigned long timeout; | |
2030 | ||
2031 | port->vdm_retries = 0; | |
2032 | port->vdm_state = VDM_STATE_BUSY; | |
2033 | timeout = vdm_ready_timeout(vdo_hdr); | |
2034 | mod_vdm_delayed_work(port, timeout); | |
f0690a25 GR |
2035 | } |
2036 | break; | |
2037 | default: | |
2038 | break; | |
2039 | } | |
2040 | } | |
2041 | ||
3ed8e1c2 | 2042 | static void vdm_state_machine_work(struct kthread_work *work) |
f0690a25 | 2043 | { |
3ed8e1c2 | 2044 | struct tcpm_port *port = container_of(work, struct tcpm_port, vdm_state_machine); |
f0690a25 GR |
2045 | enum vdm_states prev_state; |
2046 | ||
2047 | mutex_lock(&port->lock); | |
2048 | ||
2049 | /* | |
2050 | * Continue running as long as the port is not busy and there was | |
2051 | * a state change. | |
2052 | */ | |
2053 | do { | |
2054 | prev_state = port->vdm_state; | |
2055 | vdm_run_state_machine(port); | |
2056 | } while (port->vdm_state != prev_state && | |
0908c5ac KT |
2057 | port->vdm_state != VDM_STATE_BUSY && |
2058 | port->vdm_state != VDM_STATE_SEND_MESSAGE); | |
f0690a25 | 2059 | |
f1fbd950 | 2060 | if (port->vdm_state < VDM_STATE_READY) |
8d3a0578 KT |
2061 | port->vdm_sm_running = false; |
2062 | ||
f0690a25 GR |
2063 | mutex_unlock(&port->lock); |
2064 | } | |
2065 | ||
5007e1b5 BJS |
2066 | enum pdo_err { |
2067 | PDO_NO_ERR, | |
2068 | PDO_ERR_NO_VSAFE5V, | |
2069 | PDO_ERR_VSAFE5V_NOT_FIRST, | |
2070 | PDO_ERR_PDO_TYPE_NOT_IN_ORDER, | |
2071 | PDO_ERR_FIXED_NOT_SORTED, | |
2072 | PDO_ERR_VARIABLE_BATT_NOT_SORTED, | |
2073 | PDO_ERR_DUPE_PDO, | |
2eadc33f AT |
2074 | PDO_ERR_PPS_APDO_NOT_SORTED, |
2075 | PDO_ERR_DUPE_PPS_APDO, | |
5007e1b5 BJS |
2076 | }; |
2077 | ||
2078 | static const char * const pdo_err_msg[] = { | |
2079 | [PDO_ERR_NO_VSAFE5V] = | |
f73800a9 | 2080 | " err: source/sink caps should at least have vSafe5V", |
5007e1b5 BJS |
2081 | [PDO_ERR_VSAFE5V_NOT_FIRST] = |
2082 | " err: vSafe5V Fixed Supply Object Shall always be the first object", | |
2083 | [PDO_ERR_PDO_TYPE_NOT_IN_ORDER] = | |
2084 | " err: PDOs should be in the following order: Fixed; Battery; Variable", | |
2085 | [PDO_ERR_FIXED_NOT_SORTED] = | |
2086 | " err: Fixed supply pdos should be in increasing order of their fixed voltage", | |
2087 | [PDO_ERR_VARIABLE_BATT_NOT_SORTED] = | |
2088 | " err: Variable/Battery supply pdos should be in increasing order of their minimum voltage", | |
2089 | [PDO_ERR_DUPE_PDO] = | |
2090 | " err: Variable/Batt supply pdos cannot have same min/max voltage", | |
2eadc33f AT |
2091 | [PDO_ERR_PPS_APDO_NOT_SORTED] = |
2092 | " err: Programmable power supply apdos should be in increasing order of their maximum voltage", | |
2093 | [PDO_ERR_DUPE_PPS_APDO] = | |
2094 | " err: Programmable power supply apdos cannot have same min/max voltage and max current", | |
5007e1b5 BJS |
2095 | }; |
2096 | ||
2097 | static enum pdo_err tcpm_caps_err(struct tcpm_port *port, const u32 *pdo, | |
2098 | unsigned int nr_pdo) | |
2099 | { | |
2100 | unsigned int i; | |
2101 | ||
2102 | /* Should at least contain vSafe5v */ | |
2103 | if (nr_pdo < 1) | |
2104 | return PDO_ERR_NO_VSAFE5V; | |
2105 | ||
2106 | /* The vSafe5V Fixed Supply Object Shall always be the first object */ | |
2107 | if (pdo_type(pdo[0]) != PDO_TYPE_FIXED || | |
2108 | pdo_fixed_voltage(pdo[0]) != VSAFE5V) | |
2109 | return PDO_ERR_VSAFE5V_NOT_FIRST; | |
2110 | ||
2111 | for (i = 1; i < nr_pdo; i++) { | |
2112 | if (pdo_type(pdo[i]) < pdo_type(pdo[i - 1])) { | |
2113 | return PDO_ERR_PDO_TYPE_NOT_IN_ORDER; | |
2114 | } else if (pdo_type(pdo[i]) == pdo_type(pdo[i - 1])) { | |
2115 | enum pd_pdo_type type = pdo_type(pdo[i]); | |
2116 | ||
2117 | switch (type) { | |
2118 | /* | |
2119 | * The remaining Fixed Supply Objects, if | |
2120 | * present, shall be sent in voltage order; | |
2121 | * lowest to highest. | |
2122 | */ | |
2123 | case PDO_TYPE_FIXED: | |
2124 | if (pdo_fixed_voltage(pdo[i]) <= | |
2125 | pdo_fixed_voltage(pdo[i - 1])) | |
2126 | return PDO_ERR_FIXED_NOT_SORTED; | |
2127 | break; | |
2128 | /* | |
2129 | * The Battery Supply Objects and Variable | |
2130 | * supply, if present shall be sent in Minimum | |
2131 | * Voltage order; lowest to highest. | |
2132 | */ | |
2133 | case PDO_TYPE_VAR: | |
2134 | case PDO_TYPE_BATT: | |
2135 | if (pdo_min_voltage(pdo[i]) < | |
2136 | pdo_min_voltage(pdo[i - 1])) | |
2137 | return PDO_ERR_VARIABLE_BATT_NOT_SORTED; | |
2138 | else if ((pdo_min_voltage(pdo[i]) == | |
2139 | pdo_min_voltage(pdo[i - 1])) && | |
2140 | (pdo_max_voltage(pdo[i]) == | |
a684d8fd | 2141 | pdo_max_voltage(pdo[i - 1]))) |
5007e1b5 BJS |
2142 | return PDO_ERR_DUPE_PDO; |
2143 | break; | |
2eadc33f AT |
2144 | /* |
2145 | * The Programmable Power Supply APDOs, if present, | |
2146 | * shall be sent in Maximum Voltage order; | |
2147 | * lowest to highest. | |
2148 | */ | |
2149 | case PDO_TYPE_APDO: | |
2150 | if (pdo_apdo_type(pdo[i]) != APDO_TYPE_PPS) | |
2151 | break; | |
2152 | ||
1b6af2f5 AT |
2153 | if (pdo_pps_apdo_max_voltage(pdo[i]) < |
2154 | pdo_pps_apdo_max_voltage(pdo[i - 1])) | |
2eadc33f AT |
2155 | return PDO_ERR_PPS_APDO_NOT_SORTED; |
2156 | else if (pdo_pps_apdo_min_voltage(pdo[i]) == | |
2157 | pdo_pps_apdo_min_voltage(pdo[i - 1]) && | |
2158 | pdo_pps_apdo_max_voltage(pdo[i]) == | |
2159 | pdo_pps_apdo_max_voltage(pdo[i - 1]) && | |
2160 | pdo_pps_apdo_max_current(pdo[i]) == | |
2161 | pdo_pps_apdo_max_current(pdo[i - 1])) | |
2162 | return PDO_ERR_DUPE_PPS_APDO; | |
2163 | break; | |
5007e1b5 BJS |
2164 | default: |
2165 | tcpm_log_force(port, " Unknown pdo type"); | |
2166 | } | |
2167 | } | |
2168 | } | |
2169 | ||
2170 | return PDO_NO_ERR; | |
2171 | } | |
2172 | ||
2173 | static int tcpm_validate_caps(struct tcpm_port *port, const u32 *pdo, | |
2174 | unsigned int nr_pdo) | |
2175 | { | |
2176 | enum pdo_err err_index = tcpm_caps_err(port, pdo, nr_pdo); | |
2177 | ||
2178 | if (err_index != PDO_NO_ERR) { | |
2179 | tcpm_log_force(port, " %s", pdo_err_msg[err_index]); | |
2180 | return -EINVAL; | |
2181 | } | |
2182 | ||
2183 | return 0; | |
2184 | } | |
2185 | ||
8face9aa | 2186 | static int tcpm_altmode_enter(struct typec_altmode *altmode, u32 *vdo) |
e9576fe8 HK |
2187 | { |
2188 | struct tcpm_port *port = typec_altmode_get_drvdata(altmode); | |
5e1d4c49 | 2189 | int svdm_version; |
e9576fe8 HK |
2190 | u32 header; |
2191 | ||
5e1d4c49 KT |
2192 | svdm_version = typec_get_negotiated_svdm_version(port->typec_port); |
2193 | if (svdm_version < 0) | |
2194 | return svdm_version; | |
2195 | ||
2196 | header = VDO(altmode->svid, vdo ? 2 : 1, svdm_version, CMD_ENTER_MODE); | |
e9576fe8 HK |
2197 | header |= VDO_OPOS(altmode->mode); |
2198 | ||
03eafcfb | 2199 | tcpm_queue_vdm_unlocked(port, header, vdo, vdo ? 1 : 0); |
e9576fe8 HK |
2200 | return 0; |
2201 | } | |
2202 | ||
2203 | static int tcpm_altmode_exit(struct typec_altmode *altmode) | |
2204 | { | |
2205 | struct tcpm_port *port = typec_altmode_get_drvdata(altmode); | |
5e1d4c49 | 2206 | int svdm_version; |
e9576fe8 HK |
2207 | u32 header; |
2208 | ||
5e1d4c49 KT |
2209 | svdm_version = typec_get_negotiated_svdm_version(port->typec_port); |
2210 | if (svdm_version < 0) | |
2211 | return svdm_version; | |
2212 | ||
2213 | header = VDO(altmode->svid, 1, svdm_version, CMD_EXIT_MODE); | |
e9576fe8 HK |
2214 | header |= VDO_OPOS(altmode->mode); |
2215 | ||
03eafcfb | 2216 | tcpm_queue_vdm_unlocked(port, header, NULL, 0); |
e9576fe8 HK |
2217 | return 0; |
2218 | } | |
2219 | ||
2220 | static int tcpm_altmode_vdm(struct typec_altmode *altmode, | |
2221 | u32 header, const u32 *data, int count) | |
2222 | { | |
2223 | struct tcpm_port *port = typec_altmode_get_drvdata(altmode); | |
2224 | ||
03eafcfb | 2225 | tcpm_queue_vdm_unlocked(port, header, data, count - 1); |
3ed8e1c2 | 2226 | |
e9576fe8 HK |
2227 | return 0; |
2228 | } | |
2229 | ||
2230 | static const struct typec_altmode_ops tcpm_altmode_ops = { | |
2231 | .enter = tcpm_altmode_enter, | |
2232 | .exit = tcpm_altmode_exit, | |
2233 | .vdm = tcpm_altmode_vdm, | |
2234 | }; | |
2235 | ||
f0690a25 GR |
2236 | /* |
2237 | * PD (data, control) command handling functions | |
2238 | */ | |
64f7c494 AT |
2239 | static inline enum tcpm_state ready_state(struct tcpm_port *port) |
2240 | { | |
2241 | if (port->pwr_role == TYPEC_SOURCE) | |
2242 | return SRC_READY; | |
2243 | else | |
2244 | return SNK_READY; | |
2245 | } | |
2eadc33f AT |
2246 | |
2247 | static int tcpm_pd_send_control(struct tcpm_port *port, | |
2248 | enum pd_ctrl_msg_type type); | |
2249 | ||
64f7c494 AT |
2250 | static void tcpm_handle_alert(struct tcpm_port *port, const __le32 *payload, |
2251 | int cnt) | |
2252 | { | |
2253 | u32 p0 = le32_to_cpu(payload[0]); | |
2254 | unsigned int type = usb_pd_ado_type(p0); | |
2255 | ||
2256 | if (!type) { | |
2257 | tcpm_log(port, "Alert message received with no type"); | |
063933f4 | 2258 | tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP); |
64f7c494 AT |
2259 | return; |
2260 | } | |
2261 | ||
2262 | /* Just handling non-battery alerts for now */ | |
2263 | if (!(type & USB_PD_ADO_TYPE_BATT_STATUS_CHANGE)) { | |
063933f4 KT |
2264 | if (port->pwr_role == TYPEC_SOURCE) { |
2265 | port->upcoming_state = GET_STATUS_SEND; | |
2266 | tcpm_ams_start(port, GETTING_SOURCE_SINK_STATUS); | |
2267 | } else { | |
2268 | /* | |
2269 | * Do not check SinkTxOk here in case the Source doesn't set its Rp to | |
2270 | * SinkTxOk in time. | |
2271 | */ | |
2272 | port->ams = GETTING_SOURCE_SINK_STATUS; | |
64f7c494 | 2273 | tcpm_set_state(port, GET_STATUS_SEND, 0); |
64f7c494 | 2274 | } |
063933f4 KT |
2275 | } else { |
2276 | tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP); | |
64f7c494 AT |
2277 | } |
2278 | } | |
2279 | ||
f321a02c BJS |
2280 | static int tcpm_set_auto_vbus_discharge_threshold(struct tcpm_port *port, |
2281 | enum typec_pwr_opmode mode, bool pps_active, | |
2282 | u32 requested_vbus_voltage) | |
2283 | { | |
2284 | int ret; | |
2285 | ||
2286 | if (!port->tcpc->set_auto_vbus_discharge_threshold) | |
2287 | return 0; | |
2288 | ||
2289 | ret = port->tcpc->set_auto_vbus_discharge_threshold(port->tcpc, mode, pps_active, | |
2290 | requested_vbus_voltage); | |
2291 | tcpm_log_force(port, | |
2292 | "set_auto_vbus_discharge_threshold mode:%d pps_active:%c vbus:%u ret:%d", | |
2293 | mode, pps_active ? 'y' : 'n', requested_vbus_voltage, ret); | |
2294 | ||
2295 | return ret; | |
2296 | } | |
2297 | ||
8dea75e1 KT |
2298 | static void tcpm_pd_handle_state(struct tcpm_port *port, |
2299 | enum tcpm_state state, | |
2300 | enum tcpm_ams ams, | |
2301 | unsigned int delay_ms) | |
2302 | { | |
2303 | switch (port->state) { | |
2304 | case SRC_READY: | |
2305 | case SNK_READY: | |
2306 | port->ams = ams; | |
2307 | tcpm_set_state(port, state, delay_ms); | |
2308 | break; | |
2309 | /* 8.3.3.4.1.1 and 6.8.1 power transitioning */ | |
2310 | case SNK_TRANSITION_SINK: | |
2311 | case SNK_TRANSITION_SINK_VBUS: | |
2312 | case SRC_TRANSITION_SUPPLY: | |
2313 | tcpm_set_state(port, HARD_RESET_SEND, 0); | |
2314 | break; | |
2315 | default: | |
2316 | if (!tcpm_ams_interruptible(port)) { | |
2317 | tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ? | |
2318 | SRC_SOFT_RESET_WAIT_SNK_TX : | |
2319 | SNK_SOFT_RESET, | |
2320 | 0); | |
2321 | } else { | |
2322 | /* process the Message 6.8.1 */ | |
2323 | port->upcoming_state = state; | |
2324 | port->next_ams = ams; | |
2325 | tcpm_set_state(port, ready_state(port), delay_ms); | |
2326 | } | |
2327 | break; | |
2328 | } | |
2329 | } | |
2330 | ||
2331 | static void tcpm_pd_handle_msg(struct tcpm_port *port, | |
2332 | enum pd_msg_request message, | |
2333 | enum tcpm_ams ams) | |
2334 | { | |
2335 | switch (port->state) { | |
2336 | case SRC_READY: | |
2337 | case SNK_READY: | |
2338 | port->ams = ams; | |
2339 | tcpm_queue_message(port, message); | |
2340 | break; | |
2341 | /* PD 3.0 Spec 8.3.3.4.1.1 and 6.8.1 */ | |
2342 | case SNK_TRANSITION_SINK: | |
2343 | case SNK_TRANSITION_SINK_VBUS: | |
2344 | case SRC_TRANSITION_SUPPLY: | |
2345 | tcpm_set_state(port, HARD_RESET_SEND, 0); | |
2346 | break; | |
2347 | default: | |
2348 | if (!tcpm_ams_interruptible(port)) { | |
2349 | tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ? | |
2350 | SRC_SOFT_RESET_WAIT_SNK_TX : | |
2351 | SNK_SOFT_RESET, | |
2352 | 0); | |
2353 | } else { | |
2354 | port->next_ams = ams; | |
2355 | tcpm_set_state(port, ready_state(port), 0); | |
2356 | /* 6.8.1 process the Message */ | |
2357 | tcpm_queue_message(port, message); | |
2358 | } | |
2359 | break; | |
2360 | } | |
2361 | } | |
2362 | ||
8203d269 HK |
2363 | static int tcpm_register_source_caps(struct tcpm_port *port) |
2364 | { | |
2365 | struct usb_power_delivery_desc desc = { port->negotiated_rev }; | |
2366 | struct usb_power_delivery_capabilities_desc caps = { }; | |
2367 | struct usb_power_delivery_capabilities *cap; | |
2368 | ||
2369 | if (!port->partner_pd) | |
2370 | port->partner_pd = usb_power_delivery_register(NULL, &desc); | |
2371 | if (IS_ERR(port->partner_pd)) | |
2372 | return PTR_ERR(port->partner_pd); | |
2373 | ||
2374 | memcpy(caps.pdo, port->source_caps, sizeof(u32) * port->nr_source_caps); | |
2375 | caps.role = TYPEC_SOURCE; | |
2376 | ||
2377 | cap = usb_power_delivery_register_capabilities(port->partner_pd, &caps); | |
2378 | if (IS_ERR(cap)) | |
2379 | return PTR_ERR(cap); | |
2380 | ||
2381 | port->partner_source_caps = cap; | |
2382 | ||
2383 | return 0; | |
2384 | } | |
2385 | ||
2386 | static int tcpm_register_sink_caps(struct tcpm_port *port) | |
2387 | { | |
2388 | struct usb_power_delivery_desc desc = { port->negotiated_rev }; | |
2389 | struct usb_power_delivery_capabilities_desc caps = { }; | |
2390 | struct usb_power_delivery_capabilities *cap; | |
2391 | ||
2392 | if (!port->partner_pd) | |
2393 | port->partner_pd = usb_power_delivery_register(NULL, &desc); | |
2394 | if (IS_ERR(port->partner_pd)) | |
2395 | return PTR_ERR(port->partner_pd); | |
2396 | ||
2397 | memcpy(caps.pdo, port->sink_caps, sizeof(u32) * port->nr_sink_caps); | |
2398 | caps.role = TYPEC_SINK; | |
2399 | ||
2400 | cap = usb_power_delivery_register_capabilities(port->partner_pd, &caps); | |
2401 | if (IS_ERR(cap)) | |
2402 | return PTR_ERR(cap); | |
2403 | ||
2404 | port->partner_sink_caps = cap; | |
2405 | ||
2406 | return 0; | |
2407 | } | |
2408 | ||
f0690a25 GR |
2409 | static void tcpm_pd_data_request(struct tcpm_port *port, |
2410 | const struct pd_message *msg) | |
2411 | { | |
2412 | enum pd_data_msg_type type = pd_header_type_le(msg->header); | |
2413 | unsigned int cnt = pd_header_cnt_le(msg->header); | |
2eadc33f | 2414 | unsigned int rev = pd_header_rev_le(msg->header); |
f0690a25 | 2415 | unsigned int i; |
8115240c | 2416 | enum frs_typec_current partner_frs_current; |
8dc4bd07 BJS |
2417 | bool frs_enable; |
2418 | int ret; | |
f0690a25 | 2419 | |
0bc3ee92 KT |
2420 | if (tcpm_vdm_ams(port) && type != PD_DATA_VENDOR_DEF) { |
2421 | port->vdm_state = VDM_STATE_ERR_BUSY; | |
2422 | tcpm_ams_finish(port); | |
2423 | mod_vdm_delayed_work(port, 0); | |
2424 | } | |
2425 | ||
f0690a25 GR |
2426 | switch (type) { |
2427 | case PD_DATA_SOURCE_CAP: | |
f0690a25 GR |
2428 | for (i = 0; i < cnt; i++) |
2429 | port->source_caps[i] = le32_to_cpu(msg->payload[i]); | |
2430 | ||
2431 | port->nr_source_caps = cnt; | |
2432 | ||
2433 | tcpm_log_source_caps(port); | |
2434 | ||
5007e1b5 BJS |
2435 | tcpm_validate_caps(port, port->source_caps, |
2436 | port->nr_source_caps); | |
2437 | ||
8203d269 HK |
2438 | tcpm_register_source_caps(port); |
2439 | ||
2eadc33f AT |
2440 | /* |
2441 | * Adjust revision in subsequent message headers, as required, | |
2442 | * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't | |
2443 | * support Rev 1.0 so just do nothing in that scenario. | |
2444 | */ | |
8dea75e1 KT |
2445 | if (rev == PD_REV10) { |
2446 | if (port->ams == GET_SOURCE_CAPABILITIES) | |
2447 | tcpm_ams_finish(port); | |
2eadc33f | 2448 | break; |
8dea75e1 | 2449 | } |
2eadc33f AT |
2450 | |
2451 | if (rev < PD_MAX_REV) | |
2452 | port->negotiated_rev = rev; | |
2453 | ||
8dea75e1 KT |
2454 | if (port->pwr_role == TYPEC_SOURCE) { |
2455 | if (port->ams == GET_SOURCE_CAPABILITIES) | |
2456 | tcpm_pd_handle_state(port, SRC_READY, NONE_AMS, 0); | |
2457 | /* Unexpected Source Capabilities */ | |
2458 | else | |
2459 | tcpm_pd_handle_msg(port, | |
2460 | port->negotiated_rev < PD_REV30 ? | |
2461 | PD_MSG_CTRL_REJECT : | |
2462 | PD_MSG_CTRL_NOT_SUPP, | |
2463 | NONE_AMS); | |
2464 | } else if (port->state == SNK_WAIT_CAPABILITIES) { | |
f0690a25 GR |
2465 | /* |
2466 | * This message may be received even if VBUS is not | |
2467 | * present. This is quite unexpected; see USB PD | |
2468 | * specification, sections 8.3.3.6.3.1 and 8.3.3.6.3.2. | |
2469 | * However, at the same time, we must be ready to | |
2470 | * receive this message and respond to it 15ms after | |
2471 | * receiving PS_RDY during power swap operations, no matter | |
2472 | * if VBUS is available or not (USB PD specification, | |
2473 | * section 6.5.9.2). | |
2474 | * So we need to accept the message either way, | |
2475 | * but be prepared to keep waiting for VBUS after it was | |
2476 | * handled. | |
2477 | */ | |
8dea75e1 | 2478 | port->ams = POWER_NEGOTIATION; |
2b8ff93f | 2479 | port->in_ams = true; |
8dea75e1 KT |
2480 | tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0); |
2481 | } else { | |
2482 | if (port->ams == GET_SOURCE_CAPABILITIES) | |
2483 | tcpm_ams_finish(port); | |
2484 | tcpm_pd_handle_state(port, SNK_NEGOTIATE_CAPABILITIES, | |
2485 | POWER_NEGOTIATION, 0); | |
2486 | } | |
f0690a25 GR |
2487 | break; |
2488 | case PD_DATA_REQUEST: | |
2eadc33f AT |
2489 | /* |
2490 | * Adjust revision in subsequent message headers, as required, | |
2491 | * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't | |
2492 | * support Rev 1.0 so just reject in that scenario. | |
2493 | */ | |
2494 | if (rev == PD_REV10) { | |
8dea75e1 KT |
2495 | tcpm_pd_handle_msg(port, |
2496 | port->negotiated_rev < PD_REV30 ? | |
2497 | PD_MSG_CTRL_REJECT : | |
2498 | PD_MSG_CTRL_NOT_SUPP, | |
2499 | NONE_AMS); | |
2eadc33f AT |
2500 | break; |
2501 | } | |
2502 | ||
2503 | if (rev < PD_MAX_REV) | |
2504 | port->negotiated_rev = rev; | |
2505 | ||
8dea75e1 KT |
2506 | if (port->pwr_role != TYPEC_SOURCE || cnt != 1) { |
2507 | tcpm_pd_handle_msg(port, | |
2508 | port->negotiated_rev < PD_REV30 ? | |
2509 | PD_MSG_CTRL_REJECT : | |
2510 | PD_MSG_CTRL_NOT_SUPP, | |
2511 | NONE_AMS); | |
2512 | break; | |
2513 | } | |
2514 | ||
f0690a25 | 2515 | port->sink_request = le32_to_cpu(msg->payload[0]); |
8d3a0578 KT |
2516 | |
2517 | if (port->vdm_sm_running && port->explicit_contract) { | |
2518 | tcpm_pd_handle_msg(port, PD_MSG_CTRL_WAIT, port->ams); | |
2519 | break; | |
2520 | } | |
2521 | ||
8dea75e1 KT |
2522 | if (port->state == SRC_SEND_CAPABILITIES) |
2523 | tcpm_set_state(port, SRC_NEGOTIATE_CAPABILITIES, 0); | |
2524 | else | |
2525 | tcpm_pd_handle_state(port, SRC_NEGOTIATE_CAPABILITIES, | |
2526 | POWER_NEGOTIATION, 0); | |
f0690a25 GR |
2527 | break; |
2528 | case PD_DATA_SINK_CAP: | |
2529 | /* We don't do anything with this at the moment... */ | |
2530 | for (i = 0; i < cnt; i++) | |
2531 | port->sink_caps[i] = le32_to_cpu(msg->payload[i]); | |
8dc4bd07 | 2532 | |
8115240c | 2533 | partner_frs_current = (port->sink_caps[0] & PDO_FIXED_FRS_CURR_MASK) >> |
8dc4bd07 | 2534 | PDO_FIXED_FRS_CURR_SHIFT; |
8115240c BJS |
2535 | frs_enable = partner_frs_current && (partner_frs_current <= |
2536 | port->new_source_frs_current); | |
8dc4bd07 BJS |
2537 | tcpm_log(port, |
2538 | "Port partner FRS capable partner_frs_current:%u port_frs_current:%u enable:%c", | |
8115240c | 2539 | partner_frs_current, port->new_source_frs_current, frs_enable ? 'y' : 'n'); |
8dc4bd07 BJS |
2540 | if (frs_enable) { |
2541 | ret = port->tcpc->enable_frs(port->tcpc, true); | |
2542 | tcpm_log(port, "Enable FRS %s, ret:%d\n", ret ? "fail" : "success", ret); | |
2543 | } | |
2544 | ||
f0690a25 | 2545 | port->nr_sink_caps = cnt; |
8dc4bd07 | 2546 | port->sink_cap_done = true; |
8203d269 HK |
2547 | tcpm_register_sink_caps(port); |
2548 | ||
8dea75e1 | 2549 | if (port->ams == GET_SINK_CAPABILITIES) |
2e2b8d15 | 2550 | tcpm_set_state(port, ready_state(port), 0); |
8dea75e1 KT |
2551 | /* Unexpected Sink Capabilities */ |
2552 | else | |
2553 | tcpm_pd_handle_msg(port, | |
2554 | port->negotiated_rev < PD_REV30 ? | |
2555 | PD_MSG_CTRL_REJECT : | |
2556 | PD_MSG_CTRL_NOT_SUPP, | |
2557 | NONE_AMS); | |
f0690a25 GR |
2558 | break; |
2559 | case PD_DATA_VENDOR_DEF: | |
5571ea31 | 2560 | tcpm_handle_vdm_request(port, msg->payload, cnt); |
f0690a25 GR |
2561 | break; |
2562 | case PD_DATA_BIST: | |
8dea75e1 KT |
2563 | port->bist_request = le32_to_cpu(msg->payload[0]); |
2564 | tcpm_pd_handle_state(port, BIST_RX, BIST, 0); | |
f0690a25 | 2565 | break; |
64f7c494 | 2566 | case PD_DATA_ALERT: |
063933f4 KT |
2567 | if (port->state != SRC_READY && port->state != SNK_READY) |
2568 | tcpm_pd_handle_state(port, port->pwr_role == TYPEC_SOURCE ? | |
2569 | SRC_SOFT_RESET_WAIT_SNK_TX : SNK_SOFT_RESET, | |
2570 | NONE_AMS, 0); | |
2571 | else | |
2572 | tcpm_handle_alert(port, msg->payload, cnt); | |
64f7c494 AT |
2573 | break; |
2574 | case PD_DATA_BATT_STATUS: | |
2575 | case PD_DATA_GET_COUNTRY_INFO: | |
2576 | /* Currently unsupported */ | |
8dea75e1 KT |
2577 | tcpm_pd_handle_msg(port, port->negotiated_rev < PD_REV30 ? |
2578 | PD_MSG_CTRL_REJECT : | |
2579 | PD_MSG_CTRL_NOT_SUPP, | |
2580 | NONE_AMS); | |
64f7c494 | 2581 | break; |
f0690a25 | 2582 | default: |
8dea75e1 KT |
2583 | tcpm_pd_handle_msg(port, port->negotiated_rev < PD_REV30 ? |
2584 | PD_MSG_CTRL_REJECT : | |
2585 | PD_MSG_CTRL_NOT_SUPP, | |
2586 | NONE_AMS); | |
2587 | tcpm_log(port, "Unrecognized data message type %#x", type); | |
f0690a25 GR |
2588 | break; |
2589 | } | |
2590 | } | |
2591 | ||
2eadc33f AT |
2592 | static void tcpm_pps_complete(struct tcpm_port *port, int result) |
2593 | { | |
2594 | if (port->pps_pending) { | |
2595 | port->pps_status = result; | |
2596 | port->pps_pending = false; | |
2597 | complete(&port->pps_complete); | |
2598 | } | |
2599 | } | |
2600 | ||
f0690a25 GR |
2601 | static void tcpm_pd_ctrl_request(struct tcpm_port *port, |
2602 | const struct pd_message *msg) | |
2603 | { | |
2604 | enum pd_ctrl_msg_type type = pd_header_type_le(msg->header); | |
2605 | enum tcpm_state next_state; | |
2606 | ||
0bc3ee92 KT |
2607 | /* |
2608 | * Stop VDM state machine if interrupted by other Messages while NOT_SUPP is allowed in | |
2609 | * VDM AMS if waiting for VDM responses and will be handled later. | |
2610 | */ | |
2611 | if (tcpm_vdm_ams(port) && type != PD_CTRL_NOT_SUPP && type != PD_CTRL_GOOD_CRC) { | |
2612 | port->vdm_state = VDM_STATE_ERR_BUSY; | |
2613 | tcpm_ams_finish(port); | |
2614 | mod_vdm_delayed_work(port, 0); | |
2615 | } | |
2616 | ||
f0690a25 GR |
2617 | switch (type) { |
2618 | case PD_CTRL_GOOD_CRC: | |
2619 | case PD_CTRL_PING: | |
2620 | break; | |
2621 | case PD_CTRL_GET_SOURCE_CAP: | |
8dea75e1 | 2622 | tcpm_pd_handle_msg(port, PD_MSG_DATA_SOURCE_CAP, GET_SOURCE_CAPABILITIES); |
f0690a25 GR |
2623 | break; |
2624 | case PD_CTRL_GET_SINK_CAP: | |
8dea75e1 | 2625 | tcpm_pd_handle_msg(port, PD_MSG_DATA_SINK_CAP, GET_SINK_CAPABILITIES); |
f0690a25 GR |
2626 | break; |
2627 | case PD_CTRL_GOTO_MIN: | |
2628 | break; | |
2629 | case PD_CTRL_PS_RDY: | |
2630 | switch (port->state) { | |
2631 | case SNK_TRANSITION_SINK: | |
2632 | if (port->vbus_present) { | |
2633 | tcpm_set_current_limit(port, | |
f3dedafb BJS |
2634 | port->req_current_limit, |
2635 | port->req_supply_voltage); | |
8bf05746 | 2636 | port->explicit_contract = true; |
f321a02c BJS |
2637 | tcpm_set_auto_vbus_discharge_threshold(port, |
2638 | TYPEC_PWR_MODE_PD, | |
2639 | port->pps_data.active, | |
2640 | port->supply_voltage); | |
f0690a25 GR |
2641 | tcpm_set_state(port, SNK_READY, 0); |
2642 | } else { | |
2643 | /* | |
2644 | * Seen after power swap. Keep waiting for VBUS | |
2645 | * in a transitional state. | |
2646 | */ | |
2647 | tcpm_set_state(port, | |
2648 | SNK_TRANSITION_SINK_VBUS, 0); | |
2649 | } | |
2650 | break; | |
b965b631 | 2651 | case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED: |
f0690a25 GR |
2652 | tcpm_set_state(port, PR_SWAP_SRC_SNK_SINK_ON, 0); |
2653 | break; | |
2654 | case PR_SWAP_SNK_SRC_SINK_OFF: | |
2655 | tcpm_set_state(port, PR_SWAP_SNK_SRC_SOURCE_ON, 0); | |
2656 | break; | |
2657 | case VCONN_SWAP_WAIT_FOR_VCONN: | |
2658 | tcpm_set_state(port, VCONN_SWAP_TURN_OFF_VCONN, 0); | |
2659 | break; | |
8dc4bd07 BJS |
2660 | case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF: |
2661 | tcpm_set_state(port, FR_SWAP_SNK_SRC_NEW_SINK_READY, 0); | |
2662 | break; | |
f0690a25 | 2663 | default: |
8dea75e1 KT |
2664 | tcpm_pd_handle_state(port, |
2665 | port->pwr_role == TYPEC_SOURCE ? | |
2666 | SRC_SOFT_RESET_WAIT_SNK_TX : | |
2667 | SNK_SOFT_RESET, | |
2668 | NONE_AMS, 0); | |
f0690a25 GR |
2669 | break; |
2670 | } | |
2671 | break; | |
2672 | case PD_CTRL_REJECT: | |
2673 | case PD_CTRL_WAIT: | |
64f7c494 | 2674 | case PD_CTRL_NOT_SUPP: |
f0690a25 GR |
2675 | switch (port->state) { |
2676 | case SNK_NEGOTIATE_CAPABILITIES: | |
2677 | /* USB PD specification, Figure 8-43 */ | |
ef52b4a9 | 2678 | if (port->explicit_contract) |
f0690a25 | 2679 | next_state = SNK_READY; |
ef52b4a9 | 2680 | else |
f0690a25 | 2681 | next_state = SNK_WAIT_CAPABILITIES; |
2b537cf8 KT |
2682 | |
2683 | /* Threshold was relaxed before sending Request. Restore it back. */ | |
2684 | tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD, | |
2685 | port->pps_data.active, | |
2686 | port->supply_voltage); | |
f0690a25 GR |
2687 | tcpm_set_state(port, next_state, 0); |
2688 | break; | |
2eadc33f AT |
2689 | case SNK_NEGOTIATE_PPS_CAPABILITIES: |
2690 | /* Revert data back from any requested PPS updates */ | |
e3a07202 BJS |
2691 | port->pps_data.req_out_volt = port->supply_voltage; |
2692 | port->pps_data.req_op_curr = port->current_limit; | |
2eadc33f AT |
2693 | port->pps_status = (type == PD_CTRL_WAIT ? |
2694 | -EAGAIN : -EOPNOTSUPP); | |
8d3a0578 | 2695 | |
2b537cf8 KT |
2696 | /* Threshold was relaxed before sending Request. Restore it back. */ |
2697 | tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD, | |
2698 | port->pps_data.active, | |
2699 | port->supply_voltage); | |
2700 | ||
2eadc33f AT |
2701 | tcpm_set_state(port, SNK_READY, 0); |
2702 | break; | |
f0690a25 GR |
2703 | case DR_SWAP_SEND: |
2704 | port->swap_status = (type == PD_CTRL_WAIT ? | |
2705 | -EAGAIN : -EOPNOTSUPP); | |
2706 | tcpm_set_state(port, DR_SWAP_CANCEL, 0); | |
2707 | break; | |
2708 | case PR_SWAP_SEND: | |
2709 | port->swap_status = (type == PD_CTRL_WAIT ? | |
2710 | -EAGAIN : -EOPNOTSUPP); | |
2711 | tcpm_set_state(port, PR_SWAP_CANCEL, 0); | |
2712 | break; | |
2713 | case VCONN_SWAP_SEND: | |
2714 | port->swap_status = (type == PD_CTRL_WAIT ? | |
2715 | -EAGAIN : -EOPNOTSUPP); | |
2716 | tcpm_set_state(port, VCONN_SWAP_CANCEL, 0); | |
2717 | break; | |
8dc4bd07 BJS |
2718 | case FR_SWAP_SEND: |
2719 | tcpm_set_state(port, FR_SWAP_CANCEL, 0); | |
2720 | break; | |
2721 | case GET_SINK_CAP: | |
2722 | port->sink_cap_done = true; | |
2723 | tcpm_set_state(port, ready_state(port), 0); | |
2724 | break; | |
f1fbd950 KT |
2725 | case SRC_READY: |
2726 | case SNK_READY: | |
2727 | if (port->vdm_state > VDM_STATE_READY) { | |
2728 | port->vdm_state = VDM_STATE_DONE; | |
2729 | if (tcpm_vdm_ams(port)) | |
2730 | tcpm_ams_finish(port); | |
2731 | mod_vdm_delayed_work(port, 0); | |
2732 | break; | |
2733 | } | |
2734 | fallthrough; | |
f0690a25 | 2735 | default: |
8dea75e1 KT |
2736 | tcpm_pd_handle_state(port, |
2737 | port->pwr_role == TYPEC_SOURCE ? | |
2738 | SRC_SOFT_RESET_WAIT_SNK_TX : | |
2739 | SNK_SOFT_RESET, | |
2740 | NONE_AMS, 0); | |
f0690a25 GR |
2741 | break; |
2742 | } | |
2743 | break; | |
2744 | case PD_CTRL_ACCEPT: | |
2745 | switch (port->state) { | |
2746 | case SNK_NEGOTIATE_CAPABILITIES: | |
2eadc33f AT |
2747 | port->pps_data.active = false; |
2748 | tcpm_set_state(port, SNK_TRANSITION_SINK, 0); | |
2749 | break; | |
2750 | case SNK_NEGOTIATE_PPS_CAPABILITIES: | |
2751 | port->pps_data.active = true; | |
e3a07202 BJS |
2752 | port->pps_data.min_volt = port->pps_data.req_min_volt; |
2753 | port->pps_data.max_volt = port->pps_data.req_max_volt; | |
2754 | port->pps_data.max_curr = port->pps_data.req_max_curr; | |
2755 | port->req_supply_voltage = port->pps_data.req_out_volt; | |
2756 | port->req_current_limit = port->pps_data.req_op_curr; | |
4050f268 | 2757 | power_supply_changed(port->psy); |
f0690a25 GR |
2758 | tcpm_set_state(port, SNK_TRANSITION_SINK, 0); |
2759 | break; | |
2760 | case SOFT_RESET_SEND: | |
0908c5ac KT |
2761 | if (port->ams == SOFT_RESET_AMS) |
2762 | tcpm_ams_finish(port); | |
2763 | if (port->pwr_role == TYPEC_SOURCE) { | |
2764 | port->upcoming_state = SRC_SEND_CAPABILITIES; | |
2765 | tcpm_ams_start(port, POWER_NEGOTIATION); | |
2766 | } else { | |
2767 | tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0); | |
2768 | } | |
f0690a25 GR |
2769 | break; |
2770 | case DR_SWAP_SEND: | |
2771 | tcpm_set_state(port, DR_SWAP_CHANGE_DR, 0); | |
2772 | break; | |
2773 | case PR_SWAP_SEND: | |
2774 | tcpm_set_state(port, PR_SWAP_START, 0); | |
2775 | break; | |
2776 | case VCONN_SWAP_SEND: | |
2777 | tcpm_set_state(port, VCONN_SWAP_START, 0); | |
2778 | break; | |
8dc4bd07 BJS |
2779 | case FR_SWAP_SEND: |
2780 | tcpm_set_state(port, FR_SWAP_SNK_SRC_TRANSITION_TO_OFF, 0); | |
2781 | break; | |
f0690a25 | 2782 | default: |
8dea75e1 KT |
2783 | tcpm_pd_handle_state(port, |
2784 | port->pwr_role == TYPEC_SOURCE ? | |
2785 | SRC_SOFT_RESET_WAIT_SNK_TX : | |
2786 | SNK_SOFT_RESET, | |
2787 | NONE_AMS, 0); | |
f0690a25 GR |
2788 | break; |
2789 | } | |
2790 | break; | |
2791 | case PD_CTRL_SOFT_RESET: | |
8dea75e1 | 2792 | port->ams = SOFT_RESET_AMS; |
f0690a25 GR |
2793 | tcpm_set_state(port, SOFT_RESET, 0); |
2794 | break; | |
2795 | case PD_CTRL_DR_SWAP: | |
f0690a25 GR |
2796 | /* |
2797 | * XXX | |
2798 | * 6.3.9: If an alternate mode is active, a request to swap | |
2799 | * alternate modes shall trigger a port reset. | |
2800 | */ | |
8d3a0578 | 2801 | if (port->typec_caps.data != TYPEC_PORT_DRD) { |
8dea75e1 KT |
2802 | tcpm_pd_handle_msg(port, |
2803 | port->negotiated_rev < PD_REV30 ? | |
2804 | PD_MSG_CTRL_REJECT : | |
2805 | PD_MSG_CTRL_NOT_SUPP, | |
2806 | NONE_AMS); | |
8d3a0578 | 2807 | } else { |
ef52b4a9 | 2808 | if (port->send_discover) { |
8d3a0578 KT |
2809 | tcpm_queue_message(port, PD_MSG_CTRL_WAIT); |
2810 | break; | |
2811 | } | |
2812 | ||
8dea75e1 | 2813 | tcpm_pd_handle_state(port, DR_SWAP_ACCEPT, DATA_ROLE_SWAP, 0); |
8d3a0578 | 2814 | } |
f0690a25 GR |
2815 | break; |
2816 | case PD_CTRL_PR_SWAP: | |
8d3a0578 | 2817 | if (port->port_type != TYPEC_PORT_DRP) { |
8dea75e1 KT |
2818 | tcpm_pd_handle_msg(port, |
2819 | port->negotiated_rev < PD_REV30 ? | |
2820 | PD_MSG_CTRL_REJECT : | |
2821 | PD_MSG_CTRL_NOT_SUPP, | |
2822 | NONE_AMS); | |
8d3a0578 | 2823 | } else { |
ef52b4a9 | 2824 | if (port->send_discover) { |
8d3a0578 KT |
2825 | tcpm_queue_message(port, PD_MSG_CTRL_WAIT); |
2826 | break; | |
2827 | } | |
2828 | ||
8dea75e1 | 2829 | tcpm_pd_handle_state(port, PR_SWAP_ACCEPT, POWER_ROLE_SWAP, 0); |
8d3a0578 | 2830 | } |
f0690a25 GR |
2831 | break; |
2832 | case PD_CTRL_VCONN_SWAP: | |
ef52b4a9 | 2833 | if (port->send_discover) { |
8d3a0578 KT |
2834 | tcpm_queue_message(port, PD_MSG_CTRL_WAIT); |
2835 | break; | |
2836 | } | |
2837 | ||
8dea75e1 | 2838 | tcpm_pd_handle_state(port, VCONN_SWAP_ACCEPT, VCONN_SWAP, 0); |
f0690a25 | 2839 | break; |
64f7c494 AT |
2840 | case PD_CTRL_GET_SOURCE_CAP_EXT: |
2841 | case PD_CTRL_GET_STATUS: | |
2842 | case PD_CTRL_FR_SWAP: | |
2843 | case PD_CTRL_GET_PPS_STATUS: | |
2844 | case PD_CTRL_GET_COUNTRY_CODES: | |
2845 | /* Currently not supported */ | |
8dea75e1 KT |
2846 | tcpm_pd_handle_msg(port, |
2847 | port->negotiated_rev < PD_REV30 ? | |
2848 | PD_MSG_CTRL_REJECT : | |
2849 | PD_MSG_CTRL_NOT_SUPP, | |
2850 | NONE_AMS); | |
64f7c494 | 2851 | break; |
f0690a25 | 2852 | default: |
8dea75e1 KT |
2853 | tcpm_pd_handle_msg(port, |
2854 | port->negotiated_rev < PD_REV30 ? | |
2855 | PD_MSG_CTRL_REJECT : | |
2856 | PD_MSG_CTRL_NOT_SUPP, | |
2857 | NONE_AMS); | |
2858 | tcpm_log(port, "Unrecognized ctrl message type %#x", type); | |
f0690a25 GR |
2859 | break; |
2860 | } | |
2861 | } | |
2862 | ||
64f7c494 AT |
2863 | static void tcpm_pd_ext_msg_request(struct tcpm_port *port, |
2864 | const struct pd_message *msg) | |
2865 | { | |
2866 | enum pd_ext_msg_type type = pd_header_type_le(msg->header); | |
2867 | unsigned int data_size = pd_ext_header_data_size_le(msg->ext_msg.header); | |
2868 | ||
0bc3ee92 KT |
2869 | /* stopping VDM state machine if interrupted by other Messages */ |
2870 | if (tcpm_vdm_ams(port)) { | |
2871 | port->vdm_state = VDM_STATE_ERR_BUSY; | |
2872 | tcpm_ams_finish(port); | |
2873 | mod_vdm_delayed_work(port, 0); | |
2874 | } | |
2875 | ||
c58bbe34 | 2876 | if (!(le16_to_cpu(msg->ext_msg.header) & PD_EXT_HDR_CHUNKED)) { |
8dea75e1 | 2877 | tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS); |
64f7c494 AT |
2878 | tcpm_log(port, "Unchunked extended messages unsupported"); |
2879 | return; | |
2880 | } | |
2881 | ||
2882 | if (data_size > PD_EXT_MAX_CHUNK_DATA) { | |
8dea75e1 | 2883 | tcpm_pd_handle_state(port, CHUNK_NOT_SUPP, NONE_AMS, PD_T_CHUNK_NOT_SUPP); |
64f7c494 AT |
2884 | tcpm_log(port, "Chunk handling not yet supported"); |
2885 | return; | |
2886 | } | |
2887 | ||
2888 | switch (type) { | |
2889 | case PD_EXT_STATUS: | |
64f7c494 | 2890 | case PD_EXT_PPS_STATUS: |
063933f4 KT |
2891 | if (port->ams == GETTING_SOURCE_SINK_STATUS) { |
2892 | tcpm_ams_finish(port); | |
2893 | tcpm_set_state(port, ready_state(port), 0); | |
2894 | } else { | |
2895 | /* unexpected Status or PPS_Status Message */ | |
2896 | tcpm_pd_handle_state(port, port->pwr_role == TYPEC_SOURCE ? | |
2897 | SRC_SOFT_RESET_WAIT_SNK_TX : SNK_SOFT_RESET, | |
2898 | NONE_AMS, 0); | |
2899 | } | |
64f7c494 AT |
2900 | break; |
2901 | case PD_EXT_SOURCE_CAP_EXT: | |
2902 | case PD_EXT_GET_BATT_CAP: | |
2903 | case PD_EXT_GET_BATT_STATUS: | |
2904 | case PD_EXT_BATT_CAP: | |
2905 | case PD_EXT_GET_MANUFACTURER_INFO: | |
2906 | case PD_EXT_MANUFACTURER_INFO: | |
2907 | case PD_EXT_SECURITY_REQUEST: | |
2908 | case PD_EXT_SECURITY_RESPONSE: | |
2909 | case PD_EXT_FW_UPDATE_REQUEST: | |
2910 | case PD_EXT_FW_UPDATE_RESPONSE: | |
2911 | case PD_EXT_COUNTRY_INFO: | |
2912 | case PD_EXT_COUNTRY_CODES: | |
8dea75e1 | 2913 | tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS); |
64f7c494 AT |
2914 | break; |
2915 | default: | |
8dea75e1 KT |
2916 | tcpm_pd_handle_msg(port, PD_MSG_CTRL_NOT_SUPP, NONE_AMS); |
2917 | tcpm_log(port, "Unrecognized extended message type %#x", type); | |
64f7c494 AT |
2918 | break; |
2919 | } | |
2920 | } | |
2921 | ||
3ed8e1c2 | 2922 | static void tcpm_pd_rx_handler(struct kthread_work *work) |
f0690a25 GR |
2923 | { |
2924 | struct pd_rx_event *event = container_of(work, | |
2925 | struct pd_rx_event, work); | |
2926 | const struct pd_message *msg = &event->msg; | |
2927 | unsigned int cnt = pd_header_cnt_le(msg->header); | |
2928 | struct tcpm_port *port = event->port; | |
2929 | ||
2930 | mutex_lock(&port->lock); | |
2931 | ||
2932 | tcpm_log(port, "PD RX, header: %#x [%d]", le16_to_cpu(msg->header), | |
2933 | port->attached); | |
2934 | ||
2935 | if (port->attached) { | |
5fec4b54 GR |
2936 | enum pd_ctrl_msg_type type = pd_header_type_le(msg->header); |
2937 | unsigned int msgid = pd_header_msgid_le(msg->header); | |
2938 | ||
2939 | /* | |
2940 | * USB PD standard, 6.6.1.2: | |
2941 | * "... if MessageID value in a received Message is the | |
2942 | * same as the stored value, the receiver shall return a | |
2943 | * GoodCRC Message with that MessageID value and drop | |
2944 | * the Message (this is a retry of an already received | |
2945 | * Message). Note: this shall not apply to the Soft_Reset | |
2946 | * Message which always has a MessageID value of zero." | |
2947 | */ | |
2948 | if (msgid == port->rx_msgid && type != PD_CTRL_SOFT_RESET) | |
2949 | goto done; | |
2950 | port->rx_msgid = msgid; | |
2951 | ||
f0690a25 GR |
2952 | /* |
2953 | * If both ends believe to be DFP/host, we have a data role | |
2954 | * mismatch. | |
2955 | */ | |
2956 | if (!!(le16_to_cpu(msg->header) & PD_HEADER_DATA_ROLE) == | |
2957 | (port->data_role == TYPEC_HOST)) { | |
2958 | tcpm_log(port, | |
2959 | "Data role mismatch, initiating error recovery"); | |
2960 | tcpm_set_state(port, ERROR_RECOVERY, 0); | |
2961 | } else { | |
c58bbe34 | 2962 | if (le16_to_cpu(msg->header) & PD_HEADER_EXT_HDR) |
64f7c494 AT |
2963 | tcpm_pd_ext_msg_request(port, msg); |
2964 | else if (cnt) | |
f0690a25 GR |
2965 | tcpm_pd_data_request(port, msg); |
2966 | else | |
2967 | tcpm_pd_ctrl_request(port, msg); | |
2968 | } | |
2969 | } | |
2970 | ||
5fec4b54 | 2971 | done: |
f0690a25 GR |
2972 | mutex_unlock(&port->lock); |
2973 | kfree(event); | |
2974 | } | |
2975 | ||
2976 | void tcpm_pd_receive(struct tcpm_port *port, const struct pd_message *msg) | |
2977 | { | |
2978 | struct pd_rx_event *event; | |
2979 | ||
2980 | event = kzalloc(sizeof(*event), GFP_ATOMIC); | |
2981 | if (!event) | |
2982 | return; | |
2983 | ||
3ed8e1c2 | 2984 | kthread_init_work(&event->work, tcpm_pd_rx_handler); |
f0690a25 GR |
2985 | event->port = port; |
2986 | memcpy(&event->msg, msg, sizeof(*msg)); | |
3ed8e1c2 | 2987 | kthread_queue_work(port->wq, &event->work); |
f0690a25 GR |
2988 | } |
2989 | EXPORT_SYMBOL_GPL(tcpm_pd_receive); | |
2990 | ||
2991 | static int tcpm_pd_send_control(struct tcpm_port *port, | |
2992 | enum pd_ctrl_msg_type type) | |
2993 | { | |
2994 | struct pd_message msg; | |
2995 | ||
2996 | memset(&msg, 0, sizeof(msg)); | |
2997 | msg.header = PD_HEADER_LE(type, port->pwr_role, | |
2998 | port->data_role, | |
2eadc33f | 2999 | port->negotiated_rev, |
f0690a25 GR |
3000 | port->message_id, 0); |
3001 | ||
3002 | return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg); | |
3003 | } | |
3004 | ||
3005 | /* | |
3006 | * Send queued message without affecting state. | |
3007 | * Return true if state machine should go back to sleep, | |
3008 | * false otherwise. | |
3009 | */ | |
3010 | static bool tcpm_send_queued_message(struct tcpm_port *port) | |
3011 | { | |
3012 | enum pd_msg_request queued_message; | |
60e998d1 | 3013 | int ret; |
f0690a25 GR |
3014 | |
3015 | do { | |
3016 | queued_message = port->queued_message; | |
3017 | port->queued_message = PD_MSG_NONE; | |
3018 | ||
3019 | switch (queued_message) { | |
3020 | case PD_MSG_CTRL_WAIT: | |
3021 | tcpm_pd_send_control(port, PD_CTRL_WAIT); | |
3022 | break; | |
3023 | case PD_MSG_CTRL_REJECT: | |
3024 | tcpm_pd_send_control(port, PD_CTRL_REJECT); | |
3025 | break; | |
64f7c494 AT |
3026 | case PD_MSG_CTRL_NOT_SUPP: |
3027 | tcpm_pd_send_control(port, PD_CTRL_NOT_SUPP); | |
3028 | break; | |
f0690a25 | 3029 | case PD_MSG_DATA_SINK_CAP: |
8dea75e1 KT |
3030 | ret = tcpm_pd_send_sink_caps(port); |
3031 | if (ret < 0) { | |
3032 | tcpm_log(port, "Unable to send snk caps, ret=%d", ret); | |
3033 | tcpm_set_state(port, SNK_SOFT_RESET, 0); | |
3034 | } | |
3035 | tcpm_ams_finish(port); | |
f0690a25 GR |
3036 | break; |
3037 | case PD_MSG_DATA_SOURCE_CAP: | |
60e998d1 | 3038 | ret = tcpm_pd_send_source_caps(port); |
3039 | if (ret < 0) { | |
3040 | tcpm_log(port, | |
3041 | "Unable to send src caps, ret=%d", | |
3042 | ret); | |
3043 | tcpm_set_state(port, SOFT_RESET_SEND, 0); | |
3044 | } else if (port->pwr_role == TYPEC_SOURCE) { | |
8dea75e1 | 3045 | tcpm_ams_finish(port); |
60e998d1 | 3046 | tcpm_set_state(port, HARD_RESET_SEND, |
3047 | PD_T_SENDER_RESPONSE); | |
8dea75e1 KT |
3048 | } else { |
3049 | tcpm_ams_finish(port); | |
60e998d1 | 3050 | } |
f0690a25 GR |
3051 | break; |
3052 | default: | |
3053 | break; | |
3054 | } | |
3055 | } while (port->queued_message != PD_MSG_NONE); | |
3056 | ||
3057 | if (port->delayed_state != INVALID_STATE) { | |
3ed8e1c2 BJS |
3058 | if (ktime_after(port->delayed_runtime, ktime_get())) { |
3059 | mod_tcpm_delayed_work(port, ktime_to_ms(ktime_sub(port->delayed_runtime, | |
3060 | ktime_get()))); | |
f0690a25 GR |
3061 | return true; |
3062 | } | |
3063 | port->delayed_state = INVALID_STATE; | |
3064 | } | |
3065 | return false; | |
3066 | } | |
3067 | ||
3068 | static int tcpm_pd_check_request(struct tcpm_port *port) | |
3069 | { | |
3070 | u32 pdo, rdo = port->sink_request; | |
3071 | unsigned int max, op, pdo_max, index; | |
3072 | enum pd_pdo_type type; | |
3073 | ||
3074 | index = rdo_index(rdo); | |
3075 | if (!index || index > port->nr_src_pdo) | |
3076 | return -EINVAL; | |
3077 | ||
3078 | pdo = port->src_pdo[index - 1]; | |
3079 | type = pdo_type(pdo); | |
3080 | switch (type) { | |
3081 | case PDO_TYPE_FIXED: | |
3082 | case PDO_TYPE_VAR: | |
3083 | max = rdo_max_current(rdo); | |
3084 | op = rdo_op_current(rdo); | |
3085 | pdo_max = pdo_max_current(pdo); | |
3086 | ||
3087 | if (op > pdo_max) | |
3088 | return -EINVAL; | |
3089 | if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH)) | |
3090 | return -EINVAL; | |
3091 | ||
3092 | if (type == PDO_TYPE_FIXED) | |
3093 | tcpm_log(port, | |
3094 | "Requested %u mV, %u mA for %u / %u mA", | |
3095 | pdo_fixed_voltage(pdo), pdo_max, op, max); | |
3096 | else | |
3097 | tcpm_log(port, | |
3098 | "Requested %u -> %u mV, %u mA for %u / %u mA", | |
3099 | pdo_min_voltage(pdo), pdo_max_voltage(pdo), | |
3100 | pdo_max, op, max); | |
3101 | break; | |
3102 | case PDO_TYPE_BATT: | |
3103 | max = rdo_max_power(rdo); | |
3104 | op = rdo_op_power(rdo); | |
3105 | pdo_max = pdo_max_power(pdo); | |
3106 | ||
3107 | if (op > pdo_max) | |
3108 | return -EINVAL; | |
3109 | if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH)) | |
3110 | return -EINVAL; | |
3111 | tcpm_log(port, | |
3112 | "Requested %u -> %u mV, %u mW for %u / %u mW", | |
3113 | pdo_min_voltage(pdo), pdo_max_voltage(pdo), | |
3114 | pdo_max, op, max); | |
3115 | break; | |
3116 | default: | |
3117 | return -EINVAL; | |
3118 | } | |
3119 | ||
3120 | port->op_vsafe5v = index == 1; | |
3121 | ||
3122 | return 0; | |
3123 | } | |
3124 | ||
53fe0de9 LJ |
3125 | #define min_power(x, y) min(pdo_max_power(x), pdo_max_power(y)) |
3126 | #define min_current(x, y) min(pdo_max_current(x), pdo_max_current(y)) | |
3127 | ||
3128 | static int tcpm_pd_select_pdo(struct tcpm_port *port, int *sink_pdo, | |
3129 | int *src_pdo) | |
f0690a25 | 3130 | { |
53fe0de9 LJ |
3131 | unsigned int i, j, max_src_mv = 0, min_src_mv = 0, max_mw = 0, |
3132 | max_mv = 0, src_mw = 0, src_ma = 0, max_snk_mv = 0, | |
3133 | min_snk_mv = 0; | |
f0690a25 GR |
3134 | int ret = -EINVAL; |
3135 | ||
2eadc33f | 3136 | port->pps_data.supported = false; |
f2a8aa05 | 3137 | port->usb_type = POWER_SUPPLY_USB_TYPE_PD; |
86629e09 | 3138 | power_supply_changed(port->psy); |
2eadc33f | 3139 | |
f0690a25 | 3140 | /* |
53fe0de9 LJ |
3141 | * Select the source PDO providing the most power which has a |
3142 | * matchig sink cap. | |
f0690a25 GR |
3143 | */ |
3144 | for (i = 0; i < port->nr_source_caps; i++) { | |
3145 | u32 pdo = port->source_caps[i]; | |
3146 | enum pd_pdo_type type = pdo_type(pdo); | |
f0690a25 | 3147 | |
2eadc33f AT |
3148 | switch (type) { |
3149 | case PDO_TYPE_FIXED: | |
53fe0de9 LJ |
3150 | max_src_mv = pdo_fixed_voltage(pdo); |
3151 | min_src_mv = max_src_mv; | |
2eadc33f AT |
3152 | break; |
3153 | case PDO_TYPE_BATT: | |
3154 | case PDO_TYPE_VAR: | |
53fe0de9 LJ |
3155 | max_src_mv = pdo_max_voltage(pdo); |
3156 | min_src_mv = pdo_min_voltage(pdo); | |
2eadc33f AT |
3157 | break; |
3158 | case PDO_TYPE_APDO: | |
f2a8aa05 | 3159 | if (pdo_apdo_type(pdo) == APDO_TYPE_PPS) { |
2eadc33f | 3160 | port->pps_data.supported = true; |
f2a8aa05 AT |
3161 | port->usb_type = |
3162 | POWER_SUPPLY_USB_TYPE_PD_PPS; | |
86629e09 | 3163 | power_supply_changed(port->psy); |
f2a8aa05 | 3164 | } |
2eadc33f AT |
3165 | continue; |
3166 | default: | |
3167 | tcpm_log(port, "Invalid source PDO type, ignoring"); | |
3168 | continue; | |
53fe0de9 | 3169 | } |
6f566af3 | 3170 | |
2eadc33f AT |
3171 | switch (type) { |
3172 | case PDO_TYPE_FIXED: | |
3173 | case PDO_TYPE_VAR: | |
53fe0de9 LJ |
3174 | src_ma = pdo_max_current(pdo); |
3175 | src_mw = src_ma * min_src_mv / 1000; | |
2eadc33f AT |
3176 | break; |
3177 | case PDO_TYPE_BATT: | |
3178 | src_mw = pdo_max_power(pdo); | |
3179 | break; | |
3180 | case PDO_TYPE_APDO: | |
3181 | continue; | |
3182 | default: | |
3183 | tcpm_log(port, "Invalid source PDO type, ignoring"); | |
3184 | continue; | |
6f566af3 HG |
3185 | } |
3186 | ||
53fe0de9 LJ |
3187 | for (j = 0; j < port->nr_snk_pdo; j++) { |
3188 | pdo = port->snk_pdo[j]; | |
3189 | ||
2eadc33f AT |
3190 | switch (pdo_type(pdo)) { |
3191 | case PDO_TYPE_FIXED: | |
53fe0de9 | 3192 | max_snk_mv = pdo_fixed_voltage(pdo); |
2eadc33f AT |
3193 | min_snk_mv = max_snk_mv; |
3194 | break; | |
3195 | case PDO_TYPE_BATT: | |
3196 | case PDO_TYPE_VAR: | |
53fe0de9 | 3197 | max_snk_mv = pdo_max_voltage(pdo); |
2eadc33f AT |
3198 | min_snk_mv = pdo_min_voltage(pdo); |
3199 | break; | |
3200 | case PDO_TYPE_APDO: | |
3201 | continue; | |
3202 | default: | |
3203 | tcpm_log(port, "Invalid sink PDO type, ignoring"); | |
3204 | continue; | |
53fe0de9 LJ |
3205 | } |
3206 | ||
3207 | if (max_src_mv <= max_snk_mv && | |
3208 | min_src_mv >= min_snk_mv) { | |
3209 | /* Prefer higher voltages if available */ | |
3210 | if ((src_mw == max_mw && min_src_mv > max_mv) || | |
3211 | src_mw > max_mw) { | |
3212 | *src_pdo = i; | |
3213 | *sink_pdo = j; | |
3214 | max_mw = src_mw; | |
3215 | max_mv = min_src_mv; | |
3216 | ret = 0; | |
3217 | } | |
3218 | } | |
f0690a25 GR |
3219 | } |
3220 | } | |
3221 | ||
3222 | return ret; | |
3223 | } | |
3224 | ||
2eadc33f AT |
3225 | #define min_pps_apdo_current(x, y) \ |
3226 | min(pdo_pps_apdo_max_current(x), pdo_pps_apdo_max_current(y)) | |
3227 | ||
3228 | static unsigned int tcpm_pd_select_pps_apdo(struct tcpm_port *port) | |
3229 | { | |
3230 | unsigned int i, j, max_mw = 0, max_mv = 0; | |
3231 | unsigned int min_src_mv, max_src_mv, src_ma, src_mw; | |
8e4657c6 | 3232 | unsigned int min_snk_mv, max_snk_mv; |
c710d0bb KT |
3233 | unsigned int max_op_mv; |
3234 | u32 pdo, src, snk; | |
2eadc33f AT |
3235 | unsigned int src_pdo = 0, snk_pdo = 0; |
3236 | ||
3237 | /* | |
3238 | * Select the source PPS APDO providing the most power while staying | |
3239 | * within the board's limits. We skip the first PDO as this is always | |
3240 | * 5V 3A. | |
3241 | */ | |
3242 | for (i = 1; i < port->nr_source_caps; ++i) { | |
3243 | pdo = port->source_caps[i]; | |
3244 | ||
3245 | switch (pdo_type(pdo)) { | |
3246 | case PDO_TYPE_APDO: | |
3247 | if (pdo_apdo_type(pdo) != APDO_TYPE_PPS) { | |
3248 | tcpm_log(port, "Not PPS APDO (source), ignoring"); | |
3249 | continue; | |
3250 | } | |
3251 | ||
3252 | min_src_mv = pdo_pps_apdo_min_voltage(pdo); | |
3253 | max_src_mv = pdo_pps_apdo_max_voltage(pdo); | |
3254 | src_ma = pdo_pps_apdo_max_current(pdo); | |
3255 | src_mw = (src_ma * max_src_mv) / 1000; | |
3256 | ||
3257 | /* | |
3258 | * Now search through the sink PDOs to find a matching | |
3259 | * PPS APDO. Again skip the first sink PDO as this will | |
3260 | * always be 5V 3A. | |
3261 | */ | |
c413767e | 3262 | for (j = 1; j < port->nr_snk_pdo; j++) { |
2eadc33f AT |
3263 | pdo = port->snk_pdo[j]; |
3264 | ||
3265 | switch (pdo_type(pdo)) { | |
3266 | case PDO_TYPE_APDO: | |
3267 | if (pdo_apdo_type(pdo) != APDO_TYPE_PPS) { | |
3268 | tcpm_log(port, | |
3269 | "Not PPS APDO (sink), ignoring"); | |
3270 | continue; | |
3271 | } | |
3272 | ||
3273 | min_snk_mv = | |
3274 | pdo_pps_apdo_min_voltage(pdo); | |
3275 | max_snk_mv = | |
3276 | pdo_pps_apdo_max_voltage(pdo); | |
2eadc33f AT |
3277 | break; |
3278 | default: | |
3279 | tcpm_log(port, | |
3280 | "Not APDO type (sink), ignoring"); | |
3281 | continue; | |
3282 | } | |
3283 | ||
c710d0bb KT |
3284 | if (min_src_mv <= max_snk_mv && |
3285 | max_src_mv >= min_snk_mv) { | |
3286 | max_op_mv = min(max_src_mv, max_snk_mv); | |
3287 | src_mw = (max_op_mv * src_ma) / 1000; | |
2eadc33f AT |
3288 | /* Prefer higher voltages if available */ |
3289 | if ((src_mw == max_mw && | |
c710d0bb | 3290 | max_op_mv > max_mv) || |
2eadc33f AT |
3291 | src_mw > max_mw) { |
3292 | src_pdo = i; | |
3293 | snk_pdo = j; | |
3294 | max_mw = src_mw; | |
c710d0bb | 3295 | max_mv = max_op_mv; |
2eadc33f AT |
3296 | } |
3297 | } | |
3298 | } | |
3299 | ||
3300 | break; | |
3301 | default: | |
3302 | tcpm_log(port, "Not APDO type (source), ignoring"); | |
3303 | continue; | |
3304 | } | |
3305 | } | |
3306 | ||
3307 | if (src_pdo) { | |
c710d0bb KT |
3308 | src = port->source_caps[src_pdo]; |
3309 | snk = port->snk_pdo[snk_pdo]; | |
3310 | ||
e3a07202 BJS |
3311 | port->pps_data.req_min_volt = max(pdo_pps_apdo_min_voltage(src), |
3312 | pdo_pps_apdo_min_voltage(snk)); | |
3313 | port->pps_data.req_max_volt = min(pdo_pps_apdo_max_voltage(src), | |
3314 | pdo_pps_apdo_max_voltage(snk)); | |
3315 | port->pps_data.req_max_curr = min_pps_apdo_current(src, snk); | |
374157ff BJS |
3316 | port->pps_data.req_out_volt = min(port->pps_data.req_max_volt, |
3317 | max(port->pps_data.req_min_volt, | |
e3a07202 | 3318 | port->pps_data.req_out_volt)); |
374157ff | 3319 | port->pps_data.req_op_curr = min(port->pps_data.req_max_curr, |
e3a07202 | 3320 | port->pps_data.req_op_curr); |
2eadc33f AT |
3321 | } |
3322 | ||
3323 | return src_pdo; | |
3324 | } | |
3325 | ||
f0690a25 GR |
3326 | static int tcpm_pd_build_request(struct tcpm_port *port, u32 *rdo) |
3327 | { | |
3328 | unsigned int mv, ma, mw, flags; | |
3329 | unsigned int max_ma, max_mw; | |
3330 | enum pd_pdo_type type; | |
53fe0de9 | 3331 | u32 pdo, matching_snk_pdo; |
0c718676 GKH |
3332 | int src_pdo_index = 0; |
3333 | int snk_pdo_index = 0; | |
53fe0de9 | 3334 | int ret; |
f0690a25 | 3335 | |
53fe0de9 LJ |
3336 | ret = tcpm_pd_select_pdo(port, &snk_pdo_index, &src_pdo_index); |
3337 | if (ret < 0) | |
3338 | return ret; | |
3339 | ||
3340 | pdo = port->source_caps[src_pdo_index]; | |
3341 | matching_snk_pdo = port->snk_pdo[snk_pdo_index]; | |
f0690a25 GR |
3342 | type = pdo_type(pdo); |
3343 | ||
2eadc33f AT |
3344 | switch (type) { |
3345 | case PDO_TYPE_FIXED: | |
f0690a25 | 3346 | mv = pdo_fixed_voltage(pdo); |
2eadc33f AT |
3347 | break; |
3348 | case PDO_TYPE_BATT: | |
3349 | case PDO_TYPE_VAR: | |
f0690a25 | 3350 | mv = pdo_min_voltage(pdo); |
2eadc33f AT |
3351 | break; |
3352 | default: | |
3353 | tcpm_log(port, "Invalid PDO selected!"); | |
3354 | return -EINVAL; | |
3355 | } | |
f0690a25 | 3356 | |
53fe0de9 | 3357 | /* Select maximum available current within the sink pdo's limit */ |
f0690a25 | 3358 | if (type == PDO_TYPE_BATT) { |
53fe0de9 LJ |
3359 | mw = min_power(pdo, matching_snk_pdo); |
3360 | ma = 1000 * mw / mv; | |
f0690a25 | 3361 | } else { |
53fe0de9 LJ |
3362 | ma = min_current(pdo, matching_snk_pdo); |
3363 | mw = ma * mv / 1000; | |
f0690a25 | 3364 | } |
f0690a25 | 3365 | |
931693f9 | 3366 | flags = RDO_USB_COMM | RDO_NO_SUSPEND; |
f0690a25 GR |
3367 | |
3368 | /* Set mismatch bit if offered power is less than operating power */ | |
f0690a25 GR |
3369 | max_ma = ma; |
3370 | max_mw = mw; | |
3371 | if (mw < port->operating_snk_mw) { | |
3372 | flags |= RDO_CAP_MISMATCH; | |
53fe0de9 LJ |
3373 | if (type == PDO_TYPE_BATT && |
3374 | (pdo_max_power(matching_snk_pdo) > pdo_max_power(pdo))) | |
3375 | max_mw = pdo_max_power(matching_snk_pdo); | |
3376 | else if (pdo_max_current(matching_snk_pdo) > | |
3377 | pdo_max_current(pdo)) | |
3378 | max_ma = pdo_max_current(matching_snk_pdo); | |
f0690a25 GR |
3379 | } |
3380 | ||
3381 | tcpm_log(port, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d", | |
3382 | port->cc_req, port->cc1, port->cc2, port->vbus_source, | |
3383 | port->vconn_role == TYPEC_SOURCE ? "source" : "sink", | |
3384 | port->polarity); | |
3385 | ||
3386 | if (type == PDO_TYPE_BATT) { | |
53fe0de9 | 3387 | *rdo = RDO_BATT(src_pdo_index + 1, mw, max_mw, flags); |
f0690a25 GR |
3388 | |
3389 | tcpm_log(port, "Requesting PDO %d: %u mV, %u mW%s", | |
53fe0de9 | 3390 | src_pdo_index, mv, mw, |
f0690a25 GR |
3391 | flags & RDO_CAP_MISMATCH ? " [mismatch]" : ""); |
3392 | } else { | |
53fe0de9 | 3393 | *rdo = RDO_FIXED(src_pdo_index + 1, ma, max_ma, flags); |
f0690a25 GR |
3394 | |
3395 | tcpm_log(port, "Requesting PDO %d: %u mV, %u mA%s", | |
53fe0de9 | 3396 | src_pdo_index, mv, ma, |
f0690a25 GR |
3397 | flags & RDO_CAP_MISMATCH ? " [mismatch]" : ""); |
3398 | } | |
3399 | ||
f3dedafb BJS |
3400 | port->req_current_limit = ma; |
3401 | port->req_supply_voltage = mv; | |
f0690a25 GR |
3402 | |
3403 | return 0; | |
3404 | } | |
3405 | ||
3406 | static int tcpm_pd_send_request(struct tcpm_port *port) | |
3407 | { | |
3408 | struct pd_message msg; | |
3409 | int ret; | |
3410 | u32 rdo; | |
3411 | ||
3412 | ret = tcpm_pd_build_request(port, &rdo); | |
3413 | if (ret < 0) | |
3414 | return ret; | |
3415 | ||
2b537cf8 KT |
3416 | /* |
3417 | * Relax the threshold as voltage will be adjusted after Accept Message plus tSrcTransition. | |
3418 | * It is safer to modify the threshold here. | |
3419 | */ | |
3420 | tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0); | |
3421 | ||
f0690a25 GR |
3422 | memset(&msg, 0, sizeof(msg)); |
3423 | msg.header = PD_HEADER_LE(PD_DATA_REQUEST, | |
3424 | port->pwr_role, | |
3425 | port->data_role, | |
2eadc33f AT |
3426 | port->negotiated_rev, |
3427 | port->message_id, 1); | |
3428 | msg.payload[0] = cpu_to_le32(rdo); | |
3429 | ||
3430 | return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg); | |
3431 | } | |
3432 | ||
3433 | static int tcpm_pd_build_pps_request(struct tcpm_port *port, u32 *rdo) | |
3434 | { | |
8e4657c6 | 3435 | unsigned int out_mv, op_ma, op_mw, max_mv, max_ma, flags; |
2eadc33f AT |
3436 | enum pd_pdo_type type; |
3437 | unsigned int src_pdo_index; | |
3438 | u32 pdo; | |
3439 | ||
3440 | src_pdo_index = tcpm_pd_select_pps_apdo(port); | |
3441 | if (!src_pdo_index) | |
3442 | return -EOPNOTSUPP; | |
3443 | ||
3444 | pdo = port->source_caps[src_pdo_index]; | |
3445 | type = pdo_type(pdo); | |
3446 | ||
3447 | switch (type) { | |
3448 | case PDO_TYPE_APDO: | |
3449 | if (pdo_apdo_type(pdo) != APDO_TYPE_PPS) { | |
3450 | tcpm_log(port, "Invalid APDO selected!"); | |
3451 | return -EINVAL; | |
3452 | } | |
e3a07202 BJS |
3453 | max_mv = port->pps_data.req_max_volt; |
3454 | max_ma = port->pps_data.req_max_curr; | |
3455 | out_mv = port->pps_data.req_out_volt; | |
3456 | op_ma = port->pps_data.req_op_curr; | |
2eadc33f AT |
3457 | break; |
3458 | default: | |
3459 | tcpm_log(port, "Invalid PDO selected!"); | |
3460 | return -EINVAL; | |
3461 | } | |
3462 | ||
3463 | flags = RDO_USB_COMM | RDO_NO_SUSPEND; | |
3464 | ||
3465 | op_mw = (op_ma * out_mv) / 1000; | |
3466 | if (op_mw < port->operating_snk_mw) { | |
3467 | /* | |
3468 | * Try raising current to meet power needs. If that's not enough | |
3469 | * then try upping the voltage. If that's still not enough | |
3470 | * then we've obviously chosen a PPS APDO which really isn't | |
3471 | * suitable so abandon ship. | |
3472 | */ | |
3473 | op_ma = (port->operating_snk_mw * 1000) / out_mv; | |
3474 | if ((port->operating_snk_mw * 1000) % out_mv) | |
3475 | ++op_ma; | |
3476 | op_ma += RDO_PROG_CURR_MA_STEP - (op_ma % RDO_PROG_CURR_MA_STEP); | |
3477 | ||
3478 | if (op_ma > max_ma) { | |
3479 | op_ma = max_ma; | |
3480 | out_mv = (port->operating_snk_mw * 1000) / op_ma; | |
3481 | if ((port->operating_snk_mw * 1000) % op_ma) | |
3482 | ++out_mv; | |
3483 | out_mv += RDO_PROG_VOLT_MV_STEP - | |
3484 | (out_mv % RDO_PROG_VOLT_MV_STEP); | |
3485 | ||
3486 | if (out_mv > max_mv) { | |
3487 | tcpm_log(port, "Invalid PPS APDO selected!"); | |
3488 | return -EINVAL; | |
3489 | } | |
3490 | } | |
3491 | } | |
3492 | ||
3493 | tcpm_log(port, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d", | |
3494 | port->cc_req, port->cc1, port->cc2, port->vbus_source, | |
3495 | port->vconn_role == TYPEC_SOURCE ? "source" : "sink", | |
3496 | port->polarity); | |
3497 | ||
3498 | *rdo = RDO_PROG(src_pdo_index + 1, out_mv, op_ma, flags); | |
3499 | ||
3500 | tcpm_log(port, "Requesting APDO %d: %u mV, %u mA", | |
3501 | src_pdo_index, out_mv, op_ma); | |
3502 | ||
e3a07202 BJS |
3503 | port->pps_data.req_op_curr = op_ma; |
3504 | port->pps_data.req_out_volt = out_mv; | |
2eadc33f AT |
3505 | |
3506 | return 0; | |
3507 | } | |
3508 | ||
3509 | static int tcpm_pd_send_pps_request(struct tcpm_port *port) | |
3510 | { | |
3511 | struct pd_message msg; | |
3512 | int ret; | |
3513 | u32 rdo; | |
3514 | ||
3515 | ret = tcpm_pd_build_pps_request(port, &rdo); | |
3516 | if (ret < 0) | |
3517 | return ret; | |
3518 | ||
2b537cf8 KT |
3519 | /* Relax the threshold as voltage will be adjusted right after Accept Message. */ |
3520 | tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0); | |
3521 | ||
2eadc33f AT |
3522 | memset(&msg, 0, sizeof(msg)); |
3523 | msg.header = PD_HEADER_LE(PD_DATA_REQUEST, | |
3524 | port->pwr_role, | |
3525 | port->data_role, | |
3526 | port->negotiated_rev, | |
f0690a25 GR |
3527 | port->message_id, 1); |
3528 | msg.payload[0] = cpu_to_le32(rdo); | |
3529 | ||
3530 | return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg); | |
3531 | } | |
3532 | ||
3533 | static int tcpm_set_vbus(struct tcpm_port *port, bool enable) | |
3534 | { | |
3535 | int ret; | |
3536 | ||
3537 | if (enable && port->vbus_charge) | |
3538 | return -EINVAL; | |
3539 | ||
3540 | tcpm_log(port, "vbus:=%d charge=%d", enable, port->vbus_charge); | |
3541 | ||
3542 | ret = port->tcpc->set_vbus(port->tcpc, enable, port->vbus_charge); | |
3543 | if (ret < 0) | |
3544 | return ret; | |
3545 | ||
3546 | port->vbus_source = enable; | |
3547 | return 0; | |
3548 | } | |
3549 | ||
3550 | static int tcpm_set_charge(struct tcpm_port *port, bool charge) | |
3551 | { | |
3552 | int ret; | |
3553 | ||
3554 | if (charge && port->vbus_source) | |
3555 | return -EINVAL; | |
3556 | ||
3557 | if (charge != port->vbus_charge) { | |
3558 | tcpm_log(port, "vbus=%d charge:=%d", port->vbus_source, charge); | |
3559 | ret = port->tcpc->set_vbus(port->tcpc, port->vbus_source, | |
3560 | charge); | |
3561 | if (ret < 0) | |
3562 | return ret; | |
3563 | } | |
3564 | port->vbus_charge = charge; | |
86629e09 | 3565 | power_supply_changed(port->psy); |
f0690a25 GR |
3566 | return 0; |
3567 | } | |
3568 | ||
7893f9e1 | 3569 | static bool tcpm_start_toggling(struct tcpm_port *port, enum typec_cc_status cc) |
f0690a25 GR |
3570 | { |
3571 | int ret; | |
3572 | ||
7893f9e1 HG |
3573 | if (!port->tcpc->start_toggling) |
3574 | return false; | |
f0690a25 | 3575 | |
7893f9e1 HG |
3576 | tcpm_log_force(port, "Start toggling"); |
3577 | ret = port->tcpc->start_toggling(port->tcpc, port->port_type, cc); | |
3578 | return ret == 0; | |
f0690a25 GR |
3579 | } |
3580 | ||
f0690a25 GR |
3581 | static int tcpm_init_vbus(struct tcpm_port *port) |
3582 | { | |
3583 | int ret; | |
3584 | ||
3585 | ret = port->tcpc->set_vbus(port->tcpc, false, false); | |
3586 | port->vbus_source = false; | |
3587 | port->vbus_charge = false; | |
3588 | return ret; | |
3589 | } | |
3590 | ||
3591 | static int tcpm_init_vconn(struct tcpm_port *port) | |
3592 | { | |
3593 | int ret; | |
3594 | ||
3595 | ret = port->tcpc->set_vconn(port->tcpc, false); | |
3596 | port->vconn_role = TYPEC_SINK; | |
3597 | return ret; | |
3598 | } | |
3599 | ||
3600 | static void tcpm_typec_connect(struct tcpm_port *port) | |
3601 | { | |
3602 | if (!port->connected) { | |
3603 | /* Make sure we don't report stale identity information */ | |
3604 | memset(&port->partner_ident, 0, sizeof(port->partner_ident)); | |
3605 | port->partner_desc.usb_pd = port->pd_capable; | |
3606 | if (tcpm_port_is_debug(port)) | |
3607 | port->partner_desc.accessory = TYPEC_ACCESSORY_DEBUG; | |
3608 | else if (tcpm_port_is_audio(port)) | |
3609 | port->partner_desc.accessory = TYPEC_ACCESSORY_AUDIO; | |
3610 | else | |
3611 | port->partner_desc.accessory = TYPEC_ACCESSORY_NONE; | |
3612 | port->partner = typec_register_partner(port->typec_port, | |
3613 | &port->partner_desc); | |
3614 | port->connected = true; | |
8203d269 | 3615 | typec_partner_set_usb_power_delivery(port->partner, port->partner_pd); |
f0690a25 GR |
3616 | } |
3617 | } | |
3618 | ||
3619 | static int tcpm_src_attach(struct tcpm_port *port) | |
3620 | { | |
3621 | enum typec_cc_polarity polarity = | |
3622 | port->cc2 == TYPEC_CC_RD ? TYPEC_POLARITY_CC2 | |
3623 | : TYPEC_POLARITY_CC1; | |
3624 | int ret; | |
3625 | ||
3626 | if (port->attached) | |
3627 | return 0; | |
3628 | ||
3629 | ret = tcpm_set_polarity(port, polarity); | |
3630 | if (ret < 0) | |
3631 | return ret; | |
3632 | ||
dea6f87e | 3633 | tcpm_enable_auto_vbus_discharge(port, true); |
f321a02c BJS |
3634 | |
3635 | ret = tcpm_set_roles(port, true, TYPEC_SOURCE, tcpm_data_role_for_source(port)); | |
f0690a25 GR |
3636 | if (ret < 0) |
3637 | return ret; | |
3638 | ||
e9e6e164 KT |
3639 | if (port->pd_supported) { |
3640 | ret = port->tcpc->set_pd_rx(port->tcpc, true); | |
3641 | if (ret < 0) | |
3642 | goto out_disable_mux; | |
3643 | } | |
f0690a25 GR |
3644 | |
3645 | /* | |
3646 | * USB Type-C specification, version 1.2, | |
3647 | * chapter 4.5.2.2.8.1 (Attached.SRC Requirements) | |
3648 | * Enable VCONN only if the non-RD port is set to RA. | |
3649 | */ | |
3650 | if ((polarity == TYPEC_POLARITY_CC1 && port->cc2 == TYPEC_CC_RA) || | |
3651 | (polarity == TYPEC_POLARITY_CC2 && port->cc1 == TYPEC_CC_RA)) { | |
3652 | ret = tcpm_set_vconn(port, true); | |
3653 | if (ret < 0) | |
3654 | goto out_disable_pd; | |
3655 | } | |
3656 | ||
3657 | ret = tcpm_set_vbus(port, true); | |
3658 | if (ret < 0) | |
3659 | goto out_disable_vconn; | |
3660 | ||
3661 | port->pd_capable = false; | |
3662 | ||
3663 | port->partner = NULL; | |
3664 | ||
3665 | port->attached = true; | |
3666 | port->send_discover = true; | |
3667 | ||
3668 | return 0; | |
3669 | ||
3670 | out_disable_vconn: | |
3671 | tcpm_set_vconn(port, false); | |
3672 | out_disable_pd: | |
e9e6e164 KT |
3673 | if (port->pd_supported) |
3674 | port->tcpc->set_pd_rx(port->tcpc, false); | |
f0690a25 | 3675 | out_disable_mux: |
e9576fe8 | 3676 | tcpm_mux_set(port, TYPEC_STATE_SAFE, USB_ROLE_NONE, |
2000016c | 3677 | TYPEC_ORIENTATION_NONE); |
f0690a25 GR |
3678 | return ret; |
3679 | } | |
3680 | ||
3681 | static void tcpm_typec_disconnect(struct tcpm_port *port) | |
3682 | { | |
3683 | if (port->connected) { | |
8203d269 | 3684 | typec_partner_set_usb_power_delivery(port->partner, NULL); |
f0690a25 GR |
3685 | typec_unregister_partner(port->partner); |
3686 | port->partner = NULL; | |
3687 | port->connected = false; | |
3688 | } | |
3689 | } | |
3690 | ||
3691 | static void tcpm_unregister_altmodes(struct tcpm_port *port) | |
3692 | { | |
3693 | struct pd_mode_data *modep = &port->mode_data; | |
3694 | int i; | |
3695 | ||
3696 | for (i = 0; i < modep->altmodes; i++) { | |
3697 | typec_unregister_altmode(port->partner_altmode[i]); | |
3698 | port->partner_altmode[i] = NULL; | |
3699 | } | |
3700 | ||
3701 | memset(modep, 0, sizeof(*modep)); | |
3702 | } | |
3703 | ||
a69bdb28 BJS |
3704 | static void tcpm_set_partner_usb_comm_capable(struct tcpm_port *port, bool capable) |
3705 | { | |
3706 | tcpm_log(port, "Setting usb_comm capable %s", capable ? "true" : "false"); | |
3707 | ||
3708 | if (port->tcpc->set_partner_usb_comm_capable) | |
3709 | port->tcpc->set_partner_usb_comm_capable(port->tcpc, capable); | |
3710 | } | |
3711 | ||
f0690a25 GR |
3712 | static void tcpm_reset_port(struct tcpm_port *port) |
3713 | { | |
dea6f87e | 3714 | tcpm_enable_auto_vbus_discharge(port, false); |
0908c5ac KT |
3715 | port->in_ams = false; |
3716 | port->ams = NONE_AMS; | |
8d3a0578 | 3717 | port->vdm_sm_running = false; |
f0690a25 GR |
3718 | tcpm_unregister_altmodes(port); |
3719 | tcpm_typec_disconnect(port); | |
3720 | port->attached = false; | |
3721 | port->pd_capable = false; | |
2eadc33f | 3722 | port->pps_data.supported = false; |
a69bdb28 | 3723 | tcpm_set_partner_usb_comm_capable(port, false); |
f0690a25 | 3724 | |
5fec4b54 GR |
3725 | /* |
3726 | * First Rx ID should be 0; set this to a sentinel of -1 so that | |
3727 | * we can check tcpm_pd_rx_handler() if we had seen it before. | |
3728 | */ | |
3729 | port->rx_msgid = -1; | |
3730 | ||
f0690a25 GR |
3731 | port->tcpc->set_pd_rx(port->tcpc, false); |
3732 | tcpm_init_vbus(port); /* also disables charging */ | |
3733 | tcpm_init_vconn(port); | |
3734 | tcpm_set_current_limit(port, 0, 0); | |
3735 | tcpm_set_polarity(port, TYPEC_POLARITY_CC1); | |
e9576fe8 | 3736 | tcpm_mux_set(port, TYPEC_STATE_SAFE, USB_ROLE_NONE, |
2000016c | 3737 | TYPEC_ORIENTATION_NONE); |
f0690a25 GR |
3738 | tcpm_set_attached_state(port, false); |
3739 | port->try_src_count = 0; | |
3740 | port->try_snk_count = 0; | |
f2a8aa05 | 3741 | port->usb_type = POWER_SUPPLY_USB_TYPE_C; |
86629e09 | 3742 | power_supply_changed(port->psy); |
8dc4bd07 BJS |
3743 | port->nr_sink_caps = 0; |
3744 | port->sink_cap_done = false; | |
3745 | if (port->tcpc->enable_frs) | |
3746 | port->tcpc->enable_frs(port->tcpc, false); | |
8203d269 HK |
3747 | |
3748 | usb_power_delivery_unregister_capabilities(port->partner_sink_caps); | |
3749 | port->partner_sink_caps = NULL; | |
3750 | usb_power_delivery_unregister_capabilities(port->partner_source_caps); | |
3751 | port->partner_source_caps = NULL; | |
3752 | usb_power_delivery_unregister(port->partner_pd); | |
3753 | port->partner_pd = NULL; | |
f0690a25 GR |
3754 | } |
3755 | ||
3756 | static void tcpm_detach(struct tcpm_port *port) | |
3757 | { | |
2d9c6442 LJ |
3758 | if (tcpm_port_is_disconnected(port)) |
3759 | port->hard_reset_count = 0; | |
3760 | ||
f0690a25 GR |
3761 | if (!port->attached) |
3762 | return; | |
3763 | ||
b2dcfefc BJS |
3764 | if (port->tcpc->set_bist_data) { |
3765 | tcpm_log(port, "disable BIST MODE TESTDATA"); | |
3766 | port->tcpc->set_bist_data(port->tcpc, false); | |
3767 | } | |
3768 | ||
f0690a25 GR |
3769 | tcpm_reset_port(port); |
3770 | } | |
3771 | ||
3772 | static void tcpm_src_detach(struct tcpm_port *port) | |
3773 | { | |
3774 | tcpm_detach(port); | |
3775 | } | |
3776 | ||
3777 | static int tcpm_snk_attach(struct tcpm_port *port) | |
3778 | { | |
3779 | int ret; | |
3780 | ||
3781 | if (port->attached) | |
3782 | return 0; | |
3783 | ||
3784 | ret = tcpm_set_polarity(port, port->cc2 != TYPEC_CC_OPEN ? | |
3785 | TYPEC_POLARITY_CC2 : TYPEC_POLARITY_CC1); | |
3786 | if (ret < 0) | |
3787 | return ret; | |
3788 | ||
dea6f87e | 3789 | tcpm_enable_auto_vbus_discharge(port, true); |
f321a02c BJS |
3790 | |
3791 | ret = tcpm_set_roles(port, true, TYPEC_SINK, tcpm_data_role_for_sink(port)); | |
f0690a25 GR |
3792 | if (ret < 0) |
3793 | return ret; | |
3794 | ||
3795 | port->pd_capable = false; | |
3796 | ||
3797 | port->partner = NULL; | |
3798 | ||
3799 | port->attached = true; | |
3800 | port->send_discover = true; | |
3801 | ||
3802 | return 0; | |
3803 | } | |
3804 | ||
3805 | static void tcpm_snk_detach(struct tcpm_port *port) | |
3806 | { | |
3807 | tcpm_detach(port); | |
f0690a25 GR |
3808 | } |
3809 | ||
3810 | static int tcpm_acc_attach(struct tcpm_port *port) | |
3811 | { | |
3812 | int ret; | |
3813 | ||
3814 | if (port->attached) | |
3815 | return 0; | |
3816 | ||
6ecc632d LJ |
3817 | ret = tcpm_set_roles(port, true, TYPEC_SOURCE, |
3818 | tcpm_data_role_for_source(port)); | |
f0690a25 GR |
3819 | if (ret < 0) |
3820 | return ret; | |
3821 | ||
3822 | port->partner = NULL; | |
3823 | ||
3824 | tcpm_typec_connect(port); | |
3825 | ||
3826 | port->attached = true; | |
3827 | ||
3828 | return 0; | |
3829 | } | |
3830 | ||
3831 | static void tcpm_acc_detach(struct tcpm_port *port) | |
3832 | { | |
3833 | tcpm_detach(port); | |
3834 | } | |
3835 | ||
3836 | static inline enum tcpm_state hard_reset_state(struct tcpm_port *port) | |
3837 | { | |
3838 | if (port->hard_reset_count < PD_N_HARD_RESET_COUNT) | |
3839 | return HARD_RESET_SEND; | |
3840 | if (port->pd_capable) | |
3841 | return ERROR_RECOVERY; | |
3842 | if (port->pwr_role == TYPEC_SOURCE) | |
3843 | return SRC_UNATTACHED; | |
3844 | if (port->state == SNK_WAIT_CAPABILITIES) | |
3845 | return SNK_READY; | |
3846 | return SNK_UNATTACHED; | |
3847 | } | |
3848 | ||
f0690a25 GR |
3849 | static inline enum tcpm_state unattached_state(struct tcpm_port *port) |
3850 | { | |
13cb492c BJS |
3851 | if (port->port_type == TYPEC_PORT_DRP) { |
3852 | if (port->pwr_role == TYPEC_SOURCE) | |
3853 | return SRC_UNATTACHED; | |
3854 | else | |
3855 | return SNK_UNATTACHED; | |
ceeb1625 | 3856 | } else if (port->port_type == TYPEC_PORT_SRC) { |
f0690a25 | 3857 | return SRC_UNATTACHED; |
13cb492c BJS |
3858 | } |
3859 | ||
3860 | return SNK_UNATTACHED; | |
f0690a25 GR |
3861 | } |
3862 | ||
f0690a25 GR |
3863 | static void tcpm_swap_complete(struct tcpm_port *port, int result) |
3864 | { | |
3865 | if (port->swap_pending) { | |
3866 | port->swap_status = result; | |
3867 | port->swap_pending = false; | |
b17dd571 | 3868 | port->non_pd_role_swap = false; |
f0690a25 GR |
3869 | complete(&port->swap_complete); |
3870 | } | |
3871 | } | |
3872 | ||
53b70e5c | 3873 | static enum typec_pwr_opmode tcpm_get_pwr_opmode(enum typec_cc_status cc) |
fce042f0 BJS |
3874 | { |
3875 | switch (cc) { | |
3876 | case TYPEC_CC_RP_1_5: | |
3877 | return TYPEC_PWR_MODE_1_5A; | |
3878 | case TYPEC_CC_RP_3_0: | |
3879 | return TYPEC_PWR_MODE_3_0A; | |
3880 | case TYPEC_CC_RP_DEF: | |
3881 | default: | |
3882 | return TYPEC_PWR_MODE_USB; | |
3883 | } | |
3884 | } | |
3885 | ||
e9e6e164 KT |
3886 | static enum typec_cc_status tcpm_pwr_opmode_to_rp(enum typec_pwr_opmode opmode) |
3887 | { | |
3888 | switch (opmode) { | |
3889 | case TYPEC_PWR_MODE_USB: | |
3890 | return TYPEC_CC_RP_DEF; | |
3891 | case TYPEC_PWR_MODE_1_5A: | |
3892 | return TYPEC_CC_RP_1_5; | |
3893 | case TYPEC_PWR_MODE_3_0A: | |
3894 | case TYPEC_PWR_MODE_PD: | |
3895 | default: | |
3896 | return TYPEC_CC_RP_3_0; | |
3897 | } | |
3898 | } | |
3899 | ||
f0690a25 GR |
3900 | static void run_state_machine(struct tcpm_port *port) |
3901 | { | |
3902 | int ret; | |
fce042f0 | 3903 | enum typec_pwr_opmode opmode; |
131c7d12 | 3904 | unsigned int msecs; |
0908c5ac | 3905 | enum tcpm_state upcoming_state; |
f0690a25 GR |
3906 | |
3907 | port->enter_state = port->state; | |
3908 | switch (port->state) { | |
7893f9e1 | 3909 | case TOGGLING: |
f0690a25 GR |
3910 | break; |
3911 | /* SRC states */ | |
3912 | case SRC_UNATTACHED: | |
b17dd571 GR |
3913 | if (!port->non_pd_role_swap) |
3914 | tcpm_swap_complete(port, -ENOTCONN); | |
f0690a25 | 3915 | tcpm_src_detach(port); |
7893f9e1 HG |
3916 | if (tcpm_start_toggling(port, tcpm_rp_cc(port))) { |
3917 | tcpm_set_state(port, TOGGLING, 0); | |
f0690a25 GR |
3918 | break; |
3919 | } | |
3920 | tcpm_set_cc(port, tcpm_rp_cc(port)); | |
9b0ae699 | 3921 | if (port->port_type == TYPEC_PORT_DRP) |
f0690a25 GR |
3922 | tcpm_set_state(port, SNK_UNATTACHED, PD_T_DRP_SNK); |
3923 | break; | |
3924 | case SRC_ATTACH_WAIT: | |
3925 | if (tcpm_port_is_debug(port)) | |
3926 | tcpm_set_state(port, DEBUG_ACC_ATTACHED, | |
3927 | PD_T_CC_DEBOUNCE); | |
3928 | else if (tcpm_port_is_audio(port)) | |
3929 | tcpm_set_state(port, AUDIO_ACC_ATTACHED, | |
3930 | PD_T_CC_DEBOUNCE); | |
28b43d3d | 3931 | else if (tcpm_port_is_source(port) && port->vbus_vsafe0v) |
f0690a25 GR |
3932 | tcpm_set_state(port, |
3933 | tcpm_try_snk(port) ? SNK_TRY | |
3934 | : SRC_ATTACHED, | |
3935 | PD_T_CC_DEBOUNCE); | |
3936 | break; | |
3937 | ||
3938 | case SNK_TRY: | |
3939 | port->try_snk_count++; | |
3940 | /* | |
3941 | * Requirements: | |
3942 | * - Do not drive vconn or vbus | |
3943 | * - Terminate CC pins (both) to Rd | |
3944 | * Action: | |
3945 | * - Wait for tDRPTry (PD_T_DRP_TRY). | |
3946 | * Until then, ignore any state changes. | |
3947 | */ | |
3948 | tcpm_set_cc(port, TYPEC_CC_RD); | |
3949 | tcpm_set_state(port, SNK_TRY_WAIT, PD_T_DRP_TRY); | |
3950 | break; | |
3951 | case SNK_TRY_WAIT: | |
a0a3e04e BJS |
3952 | if (tcpm_port_is_sink(port)) { |
3953 | tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE, 0); | |
3954 | } else { | |
3955 | tcpm_set_state(port, SRC_TRYWAIT, 0); | |
3956 | port->max_wait = 0; | |
3957 | } | |
3958 | break; | |
3959 | case SNK_TRY_WAIT_DEBOUNCE: | |
3960 | tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS, | |
11e5e568 | 3961 | PD_T_TRY_CC_DEBOUNCE); |
a0a3e04e BJS |
3962 | break; |
3963 | case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS: | |
11e5e568 | 3964 | if (port->vbus_present && tcpm_port_is_sink(port)) |
f0690a25 | 3965 | tcpm_set_state(port, SNK_ATTACHED, 0); |
11e5e568 | 3966 | else |
02d5be46 | 3967 | port->max_wait = 0; |
f0690a25 GR |
3968 | break; |
3969 | case SRC_TRYWAIT: | |
3970 | tcpm_set_cc(port, tcpm_rp_cc(port)); | |
02d5be46 BJS |
3971 | if (port->max_wait == 0) { |
3972 | port->max_wait = jiffies + | |
3973 | msecs_to_jiffies(PD_T_DRP_TRY); | |
f0690a25 GR |
3974 | tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED, |
3975 | PD_T_DRP_TRY); | |
02d5be46 BJS |
3976 | } else { |
3977 | if (time_is_after_jiffies(port->max_wait)) | |
3978 | tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED, | |
3979 | jiffies_to_msecs(port->max_wait - | |
3980 | jiffies)); | |
3981 | else | |
3982 | tcpm_set_state(port, SNK_UNATTACHED, 0); | |
3983 | } | |
3984 | break; | |
3985 | case SRC_TRYWAIT_DEBOUNCE: | |
3986 | tcpm_set_state(port, SRC_ATTACHED, PD_T_CC_DEBOUNCE); | |
f0690a25 GR |
3987 | break; |
3988 | case SRC_TRYWAIT_UNATTACHED: | |
3989 | tcpm_set_state(port, SNK_UNATTACHED, 0); | |
3990 | break; | |
3991 | ||
3992 | case SRC_ATTACHED: | |
3993 | ret = tcpm_src_attach(port); | |
3994 | tcpm_set_state(port, SRC_UNATTACHED, | |
3995 | ret < 0 ? 0 : PD_T_PS_SOURCE_ON); | |
3996 | break; | |
3997 | case SRC_STARTUP: | |
fce042f0 BJS |
3998 | opmode = tcpm_get_pwr_opmode(tcpm_rp_cc(port)); |
3999 | typec_set_pwr_opmode(port->typec_port, opmode); | |
f0690a25 GR |
4000 | port->pwr_opmode = TYPEC_PWR_MODE_USB; |
4001 | port->caps_count = 0; | |
2eadc33f | 4002 | port->negotiated_rev = PD_MAX_REV; |
f0690a25 | 4003 | port->message_id = 0; |
5fec4b54 | 4004 | port->rx_msgid = -1; |
f0690a25 | 4005 | port->explicit_contract = false; |
0908c5ac KT |
4006 | /* SNK -> SRC POWER/FAST_ROLE_SWAP finished */ |
4007 | if (port->ams == POWER_ROLE_SWAP || | |
4008 | port->ams == FAST_ROLE_SWAP) | |
4009 | tcpm_ams_finish(port); | |
e9e6e164 KT |
4010 | if (!port->pd_supported) { |
4011 | tcpm_set_state(port, SRC_READY, 0); | |
4012 | break; | |
4013 | } | |
0908c5ac KT |
4014 | port->upcoming_state = SRC_SEND_CAPABILITIES; |
4015 | tcpm_ams_start(port, POWER_NEGOTIATION); | |
f0690a25 GR |
4016 | break; |
4017 | case SRC_SEND_CAPABILITIES: | |
4018 | port->caps_count++; | |
4019 | if (port->caps_count > PD_N_CAPS_COUNT) { | |
4020 | tcpm_set_state(port, SRC_READY, 0); | |
4021 | break; | |
4022 | } | |
4023 | ret = tcpm_pd_send_source_caps(port); | |
4024 | if (ret < 0) { | |
4025 | tcpm_set_state(port, SRC_SEND_CAPABILITIES, | |
4026 | PD_T_SEND_SOURCE_CAP); | |
4027 | } else { | |
4028 | /* | |
4029 | * Per standard, we should clear the reset counter here. | |
4030 | * However, that can result in state machine hang-ups. | |
4031 | * Reset it only in READY state to improve stability. | |
4032 | */ | |
4033 | /* port->hard_reset_count = 0; */ | |
4034 | port->caps_count = 0; | |
4035 | port->pd_capable = true; | |
976daf9d | 4036 | tcpm_set_state_cond(port, SRC_SEND_CAPABILITIES_TIMEOUT, |
f0690a25 GR |
4037 | PD_T_SEND_SOURCE_CAP); |
4038 | } | |
4039 | break; | |
976daf9d HG |
4040 | case SRC_SEND_CAPABILITIES_TIMEOUT: |
4041 | /* | |
4042 | * Error recovery for a PD_DATA_SOURCE_CAP reply timeout. | |
4043 | * | |
4044 | * PD 2.0 sinks are supposed to accept src-capabilities with a | |
4045 | * 3.0 header and simply ignore any src PDOs which the sink does | |
4046 | * not understand such as PPS but some 2.0 sinks instead ignore | |
4047 | * the entire PD_DATA_SOURCE_CAP message, causing contract | |
4048 | * negotiation to fail. | |
4049 | * | |
4050 | * After PD_N_HARD_RESET_COUNT hard-reset attempts, we try | |
4051 | * sending src-capabilities with a lower PD revision to | |
4052 | * make these broken sinks work. | |
4053 | */ | |
4054 | if (port->hard_reset_count < PD_N_HARD_RESET_COUNT) { | |
4055 | tcpm_set_state(port, HARD_RESET_SEND, 0); | |
4056 | } else if (port->negotiated_rev > PD_REV20) { | |
4057 | port->negotiated_rev--; | |
4058 | port->hard_reset_count = 0; | |
4059 | tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0); | |
4060 | } else { | |
4061 | tcpm_set_state(port, hard_reset_state(port), 0); | |
4062 | } | |
4063 | break; | |
f0690a25 GR |
4064 | case SRC_NEGOTIATE_CAPABILITIES: |
4065 | ret = tcpm_pd_check_request(port); | |
4066 | if (ret < 0) { | |
4067 | tcpm_pd_send_control(port, PD_CTRL_REJECT); | |
4068 | if (!port->explicit_contract) { | |
4069 | tcpm_set_state(port, | |
4070 | SRC_WAIT_NEW_CAPABILITIES, 0); | |
4071 | } else { | |
4072 | tcpm_set_state(port, SRC_READY, 0); | |
4073 | } | |
4074 | } else { | |
4075 | tcpm_pd_send_control(port, PD_CTRL_ACCEPT); | |
a69bdb28 BJS |
4076 | tcpm_set_partner_usb_comm_capable(port, |
4077 | !!(port->sink_request & RDO_USB_COMM)); | |
f0690a25 GR |
4078 | tcpm_set_state(port, SRC_TRANSITION_SUPPLY, |
4079 | PD_T_SRC_TRANSITION); | |
4080 | } | |
4081 | break; | |
4082 | case SRC_TRANSITION_SUPPLY: | |
4083 | /* XXX: regulator_set_voltage(vbus, ...) */ | |
4084 | tcpm_pd_send_control(port, PD_CTRL_PS_RDY); | |
4085 | port->explicit_contract = true; | |
4086 | typec_set_pwr_opmode(port->typec_port, TYPEC_PWR_MODE_PD); | |
4087 | port->pwr_opmode = TYPEC_PWR_MODE_PD; | |
4088 | tcpm_set_state_cond(port, SRC_READY, 0); | |
4089 | break; | |
4090 | case SRC_READY: | |
4091 | #if 1 | |
4092 | port->hard_reset_count = 0; | |
4093 | #endif | |
4094 | port->try_src_count = 0; | |
4095 | ||
3113bf1a | 4096 | tcpm_swap_complete(port, 0); |
f0690a25 | 4097 | tcpm_typec_connect(port); |
2eadc33f | 4098 | |
0908c5ac KT |
4099 | if (port->ams != NONE_AMS) |
4100 | tcpm_ams_finish(port); | |
8dea75e1 KT |
4101 | if (port->next_ams != NONE_AMS) { |
4102 | port->ams = port->next_ams; | |
4103 | port->next_ams = NONE_AMS; | |
4104 | } | |
4105 | ||
0908c5ac KT |
4106 | /* |
4107 | * If previous AMS is interrupted, switch to the upcoming | |
4108 | * state. | |
4109 | */ | |
4110 | if (port->upcoming_state != INVALID_STATE) { | |
4111 | upcoming_state = port->upcoming_state; | |
4112 | port->upcoming_state = INVALID_STATE; | |
4113 | tcpm_set_state(port, upcoming_state, 0); | |
4114 | break; | |
4115 | } | |
4116 | ||
c34e85fa KT |
4117 | /* |
4118 | * 6.4.4.3.1 Discover Identity | |
4119 | * "The Discover Identity Command Shall only be sent to SOP when there is an | |
4120 | * Explicit Contract." | |
4121 | * For now, this driver only supports SOP for DISCOVER_IDENTITY, thus using | |
4122 | * port->explicit_contract to decide whether to send the command. | |
4123 | */ | |
4124 | if (port->explicit_contract) | |
4125 | mod_send_discover_delayed_work(port, 0); | |
4126 | else | |
4127 | port->send_discover = false; | |
4128 | ||
f0690a25 GR |
4129 | /* |
4130 | * 6.3.5 | |
4131 | * Sending ping messages is not necessary if | |
4132 | * - the source operates at vSafe5V | |
4133 | * or | |
4134 | * - The system is not operating in PD mode | |
4135 | * or | |
4136 | * - Both partners are connected using a Type-C connector | |
f451ac9e BJS |
4137 | * |
4138 | * There is no actual need to send PD messages since the local | |
4139 | * port type-c and the spec does not clearly say whether PD is | |
4140 | * possible when type-c is connected to Type-A/B | |
f0690a25 | 4141 | */ |
f0690a25 GR |
4142 | break; |
4143 | case SRC_WAIT_NEW_CAPABILITIES: | |
4144 | /* Nothing to do... */ | |
4145 | break; | |
4146 | ||
4147 | /* SNK states */ | |
4148 | case SNK_UNATTACHED: | |
b17dd571 GR |
4149 | if (!port->non_pd_role_swap) |
4150 | tcpm_swap_complete(port, -ENOTCONN); | |
2eadc33f | 4151 | tcpm_pps_complete(port, -ENOTCONN); |
f0690a25 | 4152 | tcpm_snk_detach(port); |
7893f9e1 HG |
4153 | if (tcpm_start_toggling(port, TYPEC_CC_RD)) { |
4154 | tcpm_set_state(port, TOGGLING, 0); | |
f0690a25 GR |
4155 | break; |
4156 | } | |
4157 | tcpm_set_cc(port, TYPEC_CC_RD); | |
9b0ae699 | 4158 | if (port->port_type == TYPEC_PORT_DRP) |
f0690a25 GR |
4159 | tcpm_set_state(port, SRC_UNATTACHED, PD_T_DRP_SRC); |
4160 | break; | |
4161 | case SNK_ATTACH_WAIT: | |
4162 | if ((port->cc1 == TYPEC_CC_OPEN && | |
4163 | port->cc2 != TYPEC_CC_OPEN) || | |
4164 | (port->cc1 != TYPEC_CC_OPEN && | |
4165 | port->cc2 == TYPEC_CC_OPEN)) | |
4166 | tcpm_set_state(port, SNK_DEBOUNCED, | |
4167 | PD_T_CC_DEBOUNCE); | |
4168 | else if (tcpm_port_is_disconnected(port)) | |
4169 | tcpm_set_state(port, SNK_UNATTACHED, | |
4170 | PD_T_PD_DEBOUNCE); | |
4171 | break; | |
4172 | case SNK_DEBOUNCED: | |
4173 | if (tcpm_port_is_disconnected(port)) | |
4174 | tcpm_set_state(port, SNK_UNATTACHED, | |
4175 | PD_T_PD_DEBOUNCE); | |
4176 | else if (port->vbus_present) | |
4177 | tcpm_set_state(port, | |
4178 | tcpm_try_src(port) ? SRC_TRY | |
4179 | : SNK_ATTACHED, | |
4180 | 0); | |
f0690a25 | 4181 | break; |
f0690a25 GR |
4182 | case SRC_TRY: |
4183 | port->try_src_count++; | |
4184 | tcpm_set_cc(port, tcpm_rp_cc(port)); | |
131c7d12 BJS |
4185 | port->max_wait = 0; |
4186 | tcpm_set_state(port, SRC_TRY_WAIT, 0); | |
4187 | break; | |
4188 | case SRC_TRY_WAIT: | |
4189 | if (port->max_wait == 0) { | |
4190 | port->max_wait = jiffies + | |
4191 | msecs_to_jiffies(PD_T_DRP_TRY); | |
4192 | msecs = PD_T_DRP_TRY; | |
4193 | } else { | |
4194 | if (time_is_after_jiffies(port->max_wait)) | |
4195 | msecs = jiffies_to_msecs(port->max_wait - | |
4196 | jiffies); | |
4197 | else | |
4198 | msecs = 0; | |
4199 | } | |
4200 | tcpm_set_state(port, SNK_TRYWAIT, msecs); | |
f0690a25 GR |
4201 | break; |
4202 | case SRC_TRY_DEBOUNCE: | |
4203 | tcpm_set_state(port, SRC_ATTACHED, PD_T_PD_DEBOUNCE); | |
4204 | break; | |
4205 | case SNK_TRYWAIT: | |
4206 | tcpm_set_cc(port, TYPEC_CC_RD); | |
af450ebb | 4207 | tcpm_set_state(port, SNK_TRYWAIT_VBUS, PD_T_CC_DEBOUNCE); |
f0690a25 | 4208 | break; |
af450ebb BJS |
4209 | case SNK_TRYWAIT_VBUS: |
4210 | /* | |
4211 | * TCPM stays in this state indefinitely until VBUS | |
4212 | * is detected as long as Rp is not detected for | |
4213 | * more than a time period of tPDDebounce. | |
4214 | */ | |
4215 | if (port->vbus_present && tcpm_port_is_sink(port)) { | |
f0690a25 GR |
4216 | tcpm_set_state(port, SNK_ATTACHED, 0); |
4217 | break; | |
4218 | } | |
af450ebb BJS |
4219 | if (!tcpm_port_is_sink(port)) |
4220 | tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, 0); | |
f0690a25 | 4221 | break; |
af450ebb BJS |
4222 | case SNK_TRYWAIT_DEBOUNCE: |
4223 | tcpm_set_state(port, SNK_UNATTACHED, PD_T_PD_DEBOUNCE); | |
f0690a25 | 4224 | break; |
f0690a25 GR |
4225 | case SNK_ATTACHED: |
4226 | ret = tcpm_snk_attach(port); | |
4227 | if (ret < 0) | |
4228 | tcpm_set_state(port, SNK_UNATTACHED, 0); | |
4229 | else | |
4230 | tcpm_set_state(port, SNK_STARTUP, 0); | |
4231 | break; | |
4232 | case SNK_STARTUP: | |
fce042f0 BJS |
4233 | opmode = tcpm_get_pwr_opmode(port->polarity ? |
4234 | port->cc2 : port->cc1); | |
4235 | typec_set_pwr_opmode(port->typec_port, opmode); | |
f0690a25 | 4236 | port->pwr_opmode = TYPEC_PWR_MODE_USB; |
2eadc33f | 4237 | port->negotiated_rev = PD_MAX_REV; |
f0690a25 | 4238 | port->message_id = 0; |
5fec4b54 | 4239 | port->rx_msgid = -1; |
f0690a25 | 4240 | port->explicit_contract = false; |
0908c5ac KT |
4241 | |
4242 | if (port->ams == POWER_ROLE_SWAP || | |
4243 | port->ams == FAST_ROLE_SWAP) | |
4244 | /* SRC -> SNK POWER/FAST_ROLE_SWAP finished */ | |
4245 | tcpm_ams_finish(port); | |
4246 | ||
f0690a25 GR |
4247 | tcpm_set_state(port, SNK_DISCOVERY, 0); |
4248 | break; | |
4249 | case SNK_DISCOVERY: | |
4250 | if (port->vbus_present) { | |
1373fefc BJS |
4251 | u32 current_lim = tcpm_get_current_limit(port); |
4252 | ||
12701ce5 | 4253 | if (port->slow_charger_loop && (current_lim > PD_P_SNK_STDBY_MW / 5)) |
1373fefc BJS |
4254 | current_lim = PD_P_SNK_STDBY_MW / 5; |
4255 | tcpm_set_current_limit(port, current_lim, 5000); | |
f0690a25 | 4256 | tcpm_set_charge(port, true); |
e9e6e164 KT |
4257 | if (!port->pd_supported) |
4258 | tcpm_set_state(port, SNK_READY, 0); | |
4259 | else | |
4260 | tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0); | |
f0690a25 GR |
4261 | break; |
4262 | } | |
4263 | /* | |
4264 | * For DRP, timeouts differ. Also, handling is supposed to be | |
4265 | * different and much more complex (dead battery detection; | |
4266 | * see USB power delivery specification, section 8.3.3.6.1.5.1). | |
4267 | */ | |
4268 | tcpm_set_state(port, hard_reset_state(port), | |
9b0ae699 | 4269 | port->port_type == TYPEC_PORT_DRP ? |
f0690a25 GR |
4270 | PD_T_DB_DETECT : PD_T_NO_RESPONSE); |
4271 | break; | |
4272 | case SNK_DISCOVERY_DEBOUNCE: | |
4273 | tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE_DONE, | |
4274 | PD_T_CC_DEBOUNCE); | |
4275 | break; | |
4276 | case SNK_DISCOVERY_DEBOUNCE_DONE: | |
4277 | if (!tcpm_port_is_disconnected(port) && | |
4278 | tcpm_port_is_sink(port) && | |
3ed8e1c2 | 4279 | ktime_after(port->delayed_runtime, ktime_get())) { |
f0690a25 | 4280 | tcpm_set_state(port, SNK_DISCOVERY, |
3ed8e1c2 | 4281 | ktime_to_ms(ktime_sub(port->delayed_runtime, ktime_get()))); |
f0690a25 GR |
4282 | break; |
4283 | } | |
4284 | tcpm_set_state(port, unattached_state(port), 0); | |
4285 | break; | |
4286 | case SNK_WAIT_CAPABILITIES: | |
4287 | ret = port->tcpc->set_pd_rx(port->tcpc, true); | |
4288 | if (ret < 0) { | |
4289 | tcpm_set_state(port, SNK_READY, 0); | |
4290 | break; | |
4291 | } | |
4292 | /* | |
4293 | * If VBUS has never been low, and we time out waiting | |
4294 | * for source cap, try a soft reset first, in case we | |
4295 | * were already in a stable contract before this boot. | |
4296 | * Do this only once. | |
4297 | */ | |
4298 | if (port->vbus_never_low) { | |
4299 | port->vbus_never_low = false; | |
0908c5ac | 4300 | tcpm_set_state(port, SNK_SOFT_RESET, |
f0690a25 GR |
4301 | PD_T_SINK_WAIT_CAP); |
4302 | } else { | |
4303 | tcpm_set_state(port, hard_reset_state(port), | |
4304 | PD_T_SINK_WAIT_CAP); | |
4305 | } | |
4306 | break; | |
4307 | case SNK_NEGOTIATE_CAPABILITIES: | |
4308 | port->pd_capable = true; | |
a69bdb28 BJS |
4309 | tcpm_set_partner_usb_comm_capable(port, |
4310 | !!(port->source_caps[0] & PDO_FIXED_USB_COMM)); | |
f0690a25 GR |
4311 | port->hard_reset_count = 0; |
4312 | ret = tcpm_pd_send_request(port); | |
4313 | if (ret < 0) { | |
2b537cf8 KT |
4314 | /* Restore back to the original state */ |
4315 | tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD, | |
4316 | port->pps_data.active, | |
4317 | port->supply_voltage); | |
f0690a25 GR |
4318 | /* Let the Source send capabilities again. */ |
4319 | tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0); | |
4320 | } else { | |
4321 | tcpm_set_state_cond(port, hard_reset_state(port), | |
4322 | PD_T_SENDER_RESPONSE); | |
4323 | } | |
4324 | break; | |
2eadc33f AT |
4325 | case SNK_NEGOTIATE_PPS_CAPABILITIES: |
4326 | ret = tcpm_pd_send_pps_request(port); | |
4327 | if (ret < 0) { | |
2b537cf8 KT |
4328 | /* Restore back to the original state */ |
4329 | tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_PD, | |
4330 | port->pps_data.active, | |
4331 | port->supply_voltage); | |
2eadc33f AT |
4332 | port->pps_status = ret; |
4333 | /* | |
4334 | * If this was called due to updates to sink | |
4335 | * capabilities, and pps is no longer valid, we should | |
4336 | * safely fall back to a standard PDO. | |
4337 | */ | |
4338 | if (port->update_sink_caps) | |
4339 | tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0); | |
4340 | else | |
4341 | tcpm_set_state(port, SNK_READY, 0); | |
4342 | } else { | |
4343 | tcpm_set_state_cond(port, hard_reset_state(port), | |
4344 | PD_T_SENDER_RESPONSE); | |
4345 | } | |
4346 | break; | |
f0690a25 | 4347 | case SNK_TRANSITION_SINK: |
12308684 BJS |
4348 | /* From the USB PD spec: |
4349 | * "The Sink Shall transition to Sink Standby before a positive or | |
4350 | * negative voltage transition of VBUS. During Sink Standby | |
4351 | * the Sink Shall reduce its power draw to pSnkStdby." | |
4352 | * | |
4353 | * This is not applicable to PPS though as the port can continue | |
4354 | * to draw negotiated power without switching to standby. | |
4355 | */ | |
4356 | if (port->supply_voltage != port->req_supply_voltage && !port->pps_data.active && | |
4357 | port->current_limit * port->supply_voltage / 1000 > PD_P_SNK_STDBY_MW) { | |
4358 | u32 stdby_ma = PD_P_SNK_STDBY_MW * 1000 / port->supply_voltage; | |
4359 | ||
4360 | tcpm_log(port, "Setting standby current %u mV @ %u mA", | |
4361 | port->supply_voltage, stdby_ma); | |
4362 | tcpm_set_current_limit(port, stdby_ma, port->supply_voltage); | |
4363 | } | |
4364 | fallthrough; | |
f0690a25 GR |
4365 | case SNK_TRANSITION_SINK_VBUS: |
4366 | tcpm_set_state(port, hard_reset_state(port), | |
4367 | PD_T_PS_TRANSITION); | |
4368 | break; | |
4369 | case SNK_READY: | |
4370 | port->try_snk_count = 0; | |
2eadc33f | 4371 | port->update_sink_caps = false; |
8bf05746 BJS |
4372 | if (port->explicit_contract) { |
4373 | typec_set_pwr_opmode(port->typec_port, | |
4374 | TYPEC_PWR_MODE_PD); | |
4375 | port->pwr_opmode = TYPEC_PWR_MODE_PD; | |
4376 | } | |
f0690a25 | 4377 | |
1373fefc BJS |
4378 | if (!port->pd_capable && port->slow_charger_loop) |
4379 | tcpm_set_current_limit(port, tcpm_get_current_limit(port), 5000); | |
3113bf1a | 4380 | tcpm_swap_complete(port, 0); |
f0690a25 | 4381 | tcpm_typec_connect(port); |
8dc4bd07 | 4382 | mod_enable_frs_delayed_work(port, 0); |
2eadc33f | 4383 | tcpm_pps_complete(port, port->pps_status); |
0908c5ac KT |
4384 | |
4385 | if (port->ams != NONE_AMS) | |
4386 | tcpm_ams_finish(port); | |
8dea75e1 KT |
4387 | if (port->next_ams != NONE_AMS) { |
4388 | port->ams = port->next_ams; | |
4389 | port->next_ams = NONE_AMS; | |
4390 | } | |
4391 | ||
0908c5ac KT |
4392 | /* |
4393 | * If previous AMS is interrupted, switch to the upcoming | |
4394 | * state. | |
4395 | */ | |
4396 | if (port->upcoming_state != INVALID_STATE) { | |
4397 | upcoming_state = port->upcoming_state; | |
4398 | port->upcoming_state = INVALID_STATE; | |
4399 | tcpm_set_state(port, upcoming_state, 0); | |
4400 | break; | |
4401 | } | |
4402 | ||
c34e85fa KT |
4403 | /* |
4404 | * 6.4.4.3.1 Discover Identity | |
4405 | * "The Discover Identity Command Shall only be sent to SOP when there is an | |
4406 | * Explicit Contract." | |
4407 | * For now, this driver only supports SOP for DISCOVER_IDENTITY, thus using | |
4408 | * port->explicit_contract. | |
4409 | */ | |
4410 | if (port->explicit_contract) | |
4411 | mod_send_discover_delayed_work(port, 0); | |
4412 | else | |
4413 | port->send_discover = false; | |
4414 | ||
f2a8aa05 | 4415 | power_supply_changed(port->psy); |
f0690a25 GR |
4416 | break; |
4417 | ||
4418 | /* Accessory states */ | |
4419 | case ACC_UNATTACHED: | |
4420 | tcpm_acc_detach(port); | |
4421 | tcpm_set_state(port, SRC_UNATTACHED, 0); | |
4422 | break; | |
4423 | case DEBUG_ACC_ATTACHED: | |
4424 | case AUDIO_ACC_ATTACHED: | |
4425 | ret = tcpm_acc_attach(port); | |
4426 | if (ret < 0) | |
4427 | tcpm_set_state(port, ACC_UNATTACHED, 0); | |
4428 | break; | |
4429 | case AUDIO_ACC_DEBOUNCE: | |
4430 | tcpm_set_state(port, ACC_UNATTACHED, PD_T_CC_DEBOUNCE); | |
4431 | break; | |
4432 | ||
4433 | /* Hard_Reset states */ | |
4434 | case HARD_RESET_SEND: | |
0908c5ac KT |
4435 | if (port->ams != NONE_AMS) |
4436 | tcpm_ams_finish(port); | |
4437 | /* | |
4438 | * State machine will be directed to HARD_RESET_START, | |
4439 | * thus set upcoming_state to INVALID_STATE. | |
4440 | */ | |
4441 | port->upcoming_state = INVALID_STATE; | |
4442 | tcpm_ams_start(port, HARD_RESET); | |
f0690a25 GR |
4443 | break; |
4444 | case HARD_RESET_START: | |
8dc4bd07 BJS |
4445 | port->sink_cap_done = false; |
4446 | if (port->tcpc->enable_frs) | |
4447 | port->tcpc->enable_frs(port->tcpc, false); | |
f0690a25 GR |
4448 | port->hard_reset_count++; |
4449 | port->tcpc->set_pd_rx(port->tcpc, false); | |
4450 | tcpm_unregister_altmodes(port); | |
8dc4bd07 | 4451 | port->nr_sink_caps = 0; |
f0690a25 GR |
4452 | port->send_discover = true; |
4453 | if (port->pwr_role == TYPEC_SOURCE) | |
4454 | tcpm_set_state(port, SRC_HARD_RESET_VBUS_OFF, | |
4455 | PD_T_PS_HARD_RESET); | |
4456 | else | |
4457 | tcpm_set_state(port, SNK_HARD_RESET_SINK_OFF, 0); | |
4458 | break; | |
4459 | case SRC_HARD_RESET_VBUS_OFF: | |
23e26d05 BJS |
4460 | /* |
4461 | * 7.1.5 Response to Hard Resets | |
4462 | * Hard Reset Signaling indicates a communication failure has occurred and the | |
4463 | * Source Shall stop driving VCONN, Shall remove Rp from the VCONN pin and Shall | |
4464 | * drive VBUS to vSafe0V as shown in Figure 7-9. | |
4465 | */ | |
4466 | tcpm_set_vconn(port, false); | |
f0690a25 | 4467 | tcpm_set_vbus(port, false); |
23b5f732 | 4468 | tcpm_set_roles(port, port->self_powered, TYPEC_SOURCE, |
6ecc632d | 4469 | tcpm_data_role_for_source(port)); |
23e26d05 BJS |
4470 | /* |
4471 | * If tcpc fails to notify vbus off, TCPM will wait for PD_T_SAFE_0V + | |
4472 | * PD_T_SRC_RECOVER before turning vbus back on. | |
4473 | * From Table 7-12 Sequence Description for a Source Initiated Hard Reset: | |
4474 | * 4. Policy Engine waits tPSHardReset after sending Hard Reset Signaling and then | |
4475 | * tells the Device Policy Manager to instruct the power supply to perform a | |
4476 | * Hard Reset. The transition to vSafe0V Shall occur within tSafe0V (t2). | |
4477 | * 5. After tSrcRecover the Source applies power to VBUS in an attempt to | |
4478 | * re-establish communication with the Sink and resume USB Default Operation. | |
4479 | * The transition to vSafe5V Shall occur within tSrcTurnOn(t4). | |
4480 | */ | |
4481 | tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SAFE_0V + PD_T_SRC_RECOVER); | |
f0690a25 GR |
4482 | break; |
4483 | case SRC_HARD_RESET_VBUS_ON: | |
23e26d05 | 4484 | tcpm_set_vconn(port, true); |
f0690a25 | 4485 | tcpm_set_vbus(port, true); |
0908c5ac KT |
4486 | if (port->ams == HARD_RESET) |
4487 | tcpm_ams_finish(port); | |
e9e6e164 KT |
4488 | if (port->pd_supported) |
4489 | port->tcpc->set_pd_rx(port->tcpc, true); | |
f0690a25 GR |
4490 | tcpm_set_attached_state(port, true); |
4491 | tcpm_set_state(port, SRC_UNATTACHED, PD_T_PS_SOURCE_ON); | |
4492 | break; | |
4493 | case SNK_HARD_RESET_SINK_OFF: | |
f321a02c BJS |
4494 | /* Do not discharge/disconnect during hard reseet */ |
4495 | tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, 0); | |
2eadc33f | 4496 | memset(&port->pps_data, 0, sizeof(port->pps_data)); |
f0690a25 | 4497 | tcpm_set_vconn(port, false); |
157c0f2f BJS |
4498 | if (port->pd_capable) |
4499 | tcpm_set_charge(port, false); | |
23b5f732 | 4500 | tcpm_set_roles(port, port->self_powered, TYPEC_SINK, |
6ecc632d | 4501 | tcpm_data_role_for_sink(port)); |
f0690a25 GR |
4502 | /* |
4503 | * VBUS may or may not toggle, depending on the adapter. | |
4504 | * If it doesn't toggle, transition to SNK_HARD_RESET_SINK_ON | |
4505 | * directly after timeout. | |
4506 | */ | |
4507 | tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, PD_T_SAFE_0V); | |
4508 | break; | |
4509 | case SNK_HARD_RESET_WAIT_VBUS: | |
0908c5ac KT |
4510 | if (port->ams == HARD_RESET) |
4511 | tcpm_ams_finish(port); | |
f0690a25 GR |
4512 | /* Assume we're disconnected if VBUS doesn't come back. */ |
4513 | tcpm_set_state(port, SNK_UNATTACHED, | |
4514 | PD_T_SRC_RECOVER_MAX + PD_T_SRC_TURN_ON); | |
4515 | break; | |
4516 | case SNK_HARD_RESET_SINK_ON: | |
4517 | /* Note: There is no guarantee that VBUS is on in this state */ | |
4518 | /* | |
4519 | * XXX: | |
4520 | * The specification suggests that dual mode ports in sink | |
4521 | * mode should transition to state PE_SRC_Transition_to_default. | |
4522 | * See USB power delivery specification chapter 8.3.3.6.1.3. | |
908d34aa | 4523 | * This would mean to |
f0690a25 GR |
4524 | * - turn off VCONN, reset power supply |
4525 | * - request hardware reset | |
4526 | * - turn on VCONN | |
4527 | * - Transition to state PE_Src_Startup | |
4528 | * SNK only ports shall transition to state Snk_Startup | |
4529 | * (see chapter 8.3.3.3.8). | |
4530 | * Similar, dual-mode ports in source mode should transition | |
4531 | * to PE_SNK_Transition_to_default. | |
4532 | */ | |
157c0f2f BJS |
4533 | if (port->pd_capable) { |
4534 | tcpm_set_current_limit(port, | |
4535 | tcpm_get_current_limit(port), | |
4536 | 5000); | |
4537 | tcpm_set_charge(port, true); | |
4538 | } | |
0908c5ac KT |
4539 | if (port->ams == HARD_RESET) |
4540 | tcpm_ams_finish(port); | |
f0690a25 | 4541 | tcpm_set_attached_state(port, true); |
f321a02c | 4542 | tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, VSAFE5V); |
f0690a25 GR |
4543 | tcpm_set_state(port, SNK_STARTUP, 0); |
4544 | break; | |
4545 | ||
4546 | /* Soft_Reset states */ | |
4547 | case SOFT_RESET: | |
4548 | port->message_id = 0; | |
5fec4b54 | 4549 | port->rx_msgid = -1; |
f0690a25 | 4550 | tcpm_pd_send_control(port, PD_CTRL_ACCEPT); |
8dea75e1 | 4551 | tcpm_ams_finish(port); |
0908c5ac KT |
4552 | if (port->pwr_role == TYPEC_SOURCE) { |
4553 | port->upcoming_state = SRC_SEND_CAPABILITIES; | |
4554 | tcpm_ams_start(port, POWER_NEGOTIATION); | |
4555 | } else { | |
f0690a25 | 4556 | tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0); |
0908c5ac KT |
4557 | } |
4558 | break; | |
4559 | case SRC_SOFT_RESET_WAIT_SNK_TX: | |
4560 | case SNK_SOFT_RESET: | |
4561 | if (port->ams != NONE_AMS) | |
4562 | tcpm_ams_finish(port); | |
4563 | port->upcoming_state = SOFT_RESET_SEND; | |
4564 | tcpm_ams_start(port, SOFT_RESET_AMS); | |
f0690a25 GR |
4565 | break; |
4566 | case SOFT_RESET_SEND: | |
4567 | port->message_id = 0; | |
5fec4b54 | 4568 | port->rx_msgid = -1; |
f0690a25 GR |
4569 | if (tcpm_pd_send_control(port, PD_CTRL_SOFT_RESET)) |
4570 | tcpm_set_state_cond(port, hard_reset_state(port), 0); | |
4571 | else | |
4572 | tcpm_set_state_cond(port, hard_reset_state(port), | |
4573 | PD_T_SENDER_RESPONSE); | |
4574 | break; | |
4575 | ||
4576 | /* DR_Swap states */ | |
4577 | case DR_SWAP_SEND: | |
4578 | tcpm_pd_send_control(port, PD_CTRL_DR_SWAP); | |
ef52b4a9 KT |
4579 | if (port->data_role == TYPEC_DEVICE || port->negotiated_rev > PD_REV20) |
4580 | port->send_discover = true; | |
f0690a25 GR |
4581 | tcpm_set_state_cond(port, DR_SWAP_SEND_TIMEOUT, |
4582 | PD_T_SENDER_RESPONSE); | |
4583 | break; | |
4584 | case DR_SWAP_ACCEPT: | |
4585 | tcpm_pd_send_control(port, PD_CTRL_ACCEPT); | |
ef52b4a9 KT |
4586 | if (port->data_role == TYPEC_DEVICE || port->negotiated_rev > PD_REV20) |
4587 | port->send_discover = true; | |
f0690a25 GR |
4588 | tcpm_set_state_cond(port, DR_SWAP_CHANGE_DR, 0); |
4589 | break; | |
4590 | case DR_SWAP_SEND_TIMEOUT: | |
4591 | tcpm_swap_complete(port, -ETIMEDOUT); | |
ef52b4a9 | 4592 | port->send_discover = false; |
8dea75e1 | 4593 | tcpm_ams_finish(port); |
f0690a25 GR |
4594 | tcpm_set_state(port, ready_state(port), 0); |
4595 | break; | |
4596 | case DR_SWAP_CHANGE_DR: | |
4597 | if (port->data_role == TYPEC_HOST) { | |
4598 | tcpm_unregister_altmodes(port); | |
4599 | tcpm_set_roles(port, true, port->pwr_role, | |
4600 | TYPEC_DEVICE); | |
4601 | } else { | |
4602 | tcpm_set_roles(port, true, port->pwr_role, | |
4603 | TYPEC_HOST); | |
f0690a25 | 4604 | } |
8dea75e1 | 4605 | tcpm_ams_finish(port); |
f0690a25 GR |
4606 | tcpm_set_state(port, ready_state(port), 0); |
4607 | break; | |
4608 | ||
8dc4bd07 BJS |
4609 | case FR_SWAP_SEND: |
4610 | if (tcpm_pd_send_control(port, PD_CTRL_FR_SWAP)) { | |
4611 | tcpm_set_state(port, ERROR_RECOVERY, 0); | |
4612 | break; | |
4613 | } | |
4614 | tcpm_set_state_cond(port, FR_SWAP_SEND_TIMEOUT, PD_T_SENDER_RESPONSE); | |
4615 | break; | |
4616 | case FR_SWAP_SEND_TIMEOUT: | |
4617 | tcpm_set_state(port, ERROR_RECOVERY, 0); | |
4618 | break; | |
4619 | case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF: | |
4620 | tcpm_set_state(port, ERROR_RECOVERY, PD_T_PS_SOURCE_OFF); | |
4621 | break; | |
4622 | case FR_SWAP_SNK_SRC_NEW_SINK_READY: | |
4623 | if (port->vbus_source) | |
4624 | tcpm_set_state(port, FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED, 0); | |
4625 | else | |
4626 | tcpm_set_state(port, ERROR_RECOVERY, PD_T_RECEIVER_RESPONSE); | |
4627 | break; | |
4628 | case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED: | |
4629 | tcpm_set_pwr_role(port, TYPEC_SOURCE); | |
4630 | if (tcpm_pd_send_control(port, PD_CTRL_PS_RDY)) { | |
4631 | tcpm_set_state(port, ERROR_RECOVERY, 0); | |
4632 | break; | |
4633 | } | |
4634 | tcpm_set_cc(port, tcpm_rp_cc(port)); | |
4635 | tcpm_set_state(port, SRC_STARTUP, PD_T_SWAP_SRC_START); | |
4636 | break; | |
4637 | ||
f0690a25 GR |
4638 | /* PR_Swap states */ |
4639 | case PR_SWAP_ACCEPT: | |
4640 | tcpm_pd_send_control(port, PD_CTRL_ACCEPT); | |
4641 | tcpm_set_state(port, PR_SWAP_START, 0); | |
4642 | break; | |
4643 | case PR_SWAP_SEND: | |
4644 | tcpm_pd_send_control(port, PD_CTRL_PR_SWAP); | |
4645 | tcpm_set_state_cond(port, PR_SWAP_SEND_TIMEOUT, | |
4646 | PD_T_SENDER_RESPONSE); | |
4647 | break; | |
4648 | case PR_SWAP_SEND_TIMEOUT: | |
4649 | tcpm_swap_complete(port, -ETIMEDOUT); | |
4650 | tcpm_set_state(port, ready_state(port), 0); | |
4651 | break; | |
4652 | case PR_SWAP_START: | |
59d4d06c | 4653 | tcpm_apply_rc(port); |
f0690a25 GR |
4654 | if (port->pwr_role == TYPEC_SOURCE) |
4655 | tcpm_set_state(port, PR_SWAP_SRC_SNK_TRANSITION_OFF, | |
4656 | PD_T_SRC_TRANSITION); | |
4657 | else | |
4658 | tcpm_set_state(port, PR_SWAP_SNK_SRC_SINK_OFF, 0); | |
4659 | break; | |
4660 | case PR_SWAP_SRC_SNK_TRANSITION_OFF: | |
f321a02c BJS |
4661 | /* |
4662 | * Prevent vbus discharge circuit from turning on during PR_SWAP | |
4663 | * as this is not a disconnect. | |
4664 | */ | |
f0690a25 GR |
4665 | tcpm_set_vbus(port, false); |
4666 | port->explicit_contract = false; | |
b965b631 | 4667 | /* allow time for Vbus discharge, must be < tSrcSwapStdby */ |
f0690a25 | 4668 | tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF, |
b965b631 | 4669 | PD_T_SRCSWAPSTDBY); |
f0690a25 GR |
4670 | break; |
4671 | case PR_SWAP_SRC_SNK_SOURCE_OFF: | |
4672 | tcpm_set_cc(port, TYPEC_CC_RD); | |
b965b631 BJS |
4673 | /* allow CC debounce */ |
4674 | tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED, | |
4675 | PD_T_CC_DEBOUNCE); | |
4676 | break; | |
4677 | case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED: | |
050161ea GR |
4678 | /* |
4679 | * USB-PD standard, 6.2.1.4, Port Power Role: | |
4680 | * "During the Power Role Swap Sequence, for the initial Source | |
4681 | * Port, the Port Power Role field shall be set to Sink in the | |
4682 | * PS_RDY Message indicating that the initial Source’s power | |
4683 | * supply is turned off" | |
4684 | */ | |
4685 | tcpm_set_pwr_role(port, TYPEC_SINK); | |
f0690a25 GR |
4686 | if (tcpm_pd_send_control(port, PD_CTRL_PS_RDY)) { |
4687 | tcpm_set_state(port, ERROR_RECOVERY, 0); | |
4688 | break; | |
4689 | } | |
fe79d5de | 4690 | tcpm_set_state(port, ERROR_RECOVERY, PD_T_PS_SOURCE_ON_PRS); |
f0690a25 GR |
4691 | break; |
4692 | case PR_SWAP_SRC_SNK_SINK_ON: | |
59d4d06c | 4693 | tcpm_enable_auto_vbus_discharge(port, true); |
f321a02c BJS |
4694 | /* Set the vbus disconnect threshold for implicit contract */ |
4695 | tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, false, VSAFE5V); | |
f0690a25 GR |
4696 | tcpm_set_state(port, SNK_STARTUP, 0); |
4697 | break; | |
4698 | case PR_SWAP_SNK_SRC_SINK_OFF: | |
f321a02c BJS |
4699 | /* |
4700 | * Prevent vbus discharge circuit from turning on during PR_SWAP | |
4701 | * as this is not a disconnect. | |
4702 | */ | |
4703 | tcpm_set_auto_vbus_discharge_threshold(port, TYPEC_PWR_MODE_USB, | |
4704 | port->pps_data.active, 0); | |
f0690a25 GR |
4705 | tcpm_set_charge(port, false); |
4706 | tcpm_set_state(port, hard_reset_state(port), | |
4707 | PD_T_PS_SOURCE_OFF); | |
4708 | break; | |
4709 | case PR_SWAP_SNK_SRC_SOURCE_ON: | |
59d4d06c | 4710 | tcpm_enable_auto_vbus_discharge(port, true); |
f0690a25 GR |
4711 | tcpm_set_cc(port, tcpm_rp_cc(port)); |
4712 | tcpm_set_vbus(port, true); | |
b965b631 BJS |
4713 | /* |
4714 | * allow time VBUS ramp-up, must be < tNewSrc | |
4715 | * Also, this window overlaps with CC debounce as well. | |
4716 | * So, Wait for the max of two which is PD_T_NEWSRC | |
4717 | */ | |
4718 | tcpm_set_state(port, PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP, | |
4719 | PD_T_NEWSRC); | |
4720 | break; | |
4721 | case PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP: | |
050161ea GR |
4722 | /* |
4723 | * USB PD standard, 6.2.1.4: | |
4724 | * "Subsequent Messages initiated by the Policy Engine, | |
4725 | * such as the PS_RDY Message sent to indicate that Vbus | |
4726 | * is ready, will have the Port Power Role field set to | |
4727 | * Source." | |
4728 | */ | |
f0690a25 | 4729 | tcpm_set_pwr_role(port, TYPEC_SOURCE); |
050161ea | 4730 | tcpm_pd_send_control(port, PD_CTRL_PS_RDY); |
6bbe2a90 | 4731 | tcpm_set_state(port, SRC_STARTUP, PD_T_SWAP_SRC_START); |
f0690a25 GR |
4732 | break; |
4733 | ||
4734 | case VCONN_SWAP_ACCEPT: | |
4735 | tcpm_pd_send_control(port, PD_CTRL_ACCEPT); | |
8dea75e1 | 4736 | tcpm_ams_finish(port); |
f0690a25 GR |
4737 | tcpm_set_state(port, VCONN_SWAP_START, 0); |
4738 | break; | |
4739 | case VCONN_SWAP_SEND: | |
4740 | tcpm_pd_send_control(port, PD_CTRL_VCONN_SWAP); | |
4741 | tcpm_set_state(port, VCONN_SWAP_SEND_TIMEOUT, | |
4742 | PD_T_SENDER_RESPONSE); | |
4743 | break; | |
4744 | case VCONN_SWAP_SEND_TIMEOUT: | |
4745 | tcpm_swap_complete(port, -ETIMEDOUT); | |
4746 | tcpm_set_state(port, ready_state(port), 0); | |
4747 | break; | |
4748 | case VCONN_SWAP_START: | |
4749 | if (port->vconn_role == TYPEC_SOURCE) | |
4750 | tcpm_set_state(port, VCONN_SWAP_WAIT_FOR_VCONN, 0); | |
4751 | else | |
4752 | tcpm_set_state(port, VCONN_SWAP_TURN_ON_VCONN, 0); | |
4753 | break; | |
4754 | case VCONN_SWAP_WAIT_FOR_VCONN: | |
4755 | tcpm_set_state(port, hard_reset_state(port), | |
4756 | PD_T_VCONN_SOURCE_ON); | |
4757 | break; | |
4758 | case VCONN_SWAP_TURN_ON_VCONN: | |
4759 | tcpm_set_vconn(port, true); | |
4760 | tcpm_pd_send_control(port, PD_CTRL_PS_RDY); | |
f0690a25 GR |
4761 | tcpm_set_state(port, ready_state(port), 0); |
4762 | break; | |
4763 | case VCONN_SWAP_TURN_OFF_VCONN: | |
4764 | tcpm_set_vconn(port, false); | |
f0690a25 GR |
4765 | tcpm_set_state(port, ready_state(port), 0); |
4766 | break; | |
4767 | ||
4768 | case DR_SWAP_CANCEL: | |
4769 | case PR_SWAP_CANCEL: | |
4770 | case VCONN_SWAP_CANCEL: | |
4771 | tcpm_swap_complete(port, port->swap_status); | |
4772 | if (port->pwr_role == TYPEC_SOURCE) | |
4773 | tcpm_set_state(port, SRC_READY, 0); | |
4774 | else | |
4775 | tcpm_set_state(port, SNK_READY, 0); | |
4776 | break; | |
8dc4bd07 BJS |
4777 | case FR_SWAP_CANCEL: |
4778 | if (port->pwr_role == TYPEC_SOURCE) | |
4779 | tcpm_set_state(port, SRC_READY, 0); | |
4780 | else | |
4781 | tcpm_set_state(port, SNK_READY, 0); | |
4782 | break; | |
f0690a25 GR |
4783 | |
4784 | case BIST_RX: | |
4785 | switch (BDO_MODE_MASK(port->bist_request)) { | |
4786 | case BDO_MODE_CARRIER2: | |
4787 | tcpm_pd_transmit(port, TCPC_TX_BIST_MODE_2, NULL); | |
6e1c2241 BJS |
4788 | tcpm_set_state(port, unattached_state(port), |
4789 | PD_T_BIST_CONT_MODE); | |
f0690a25 | 4790 | break; |
b2dcfefc BJS |
4791 | case BDO_MODE_TESTDATA: |
4792 | if (port->tcpc->set_bist_data) { | |
4793 | tcpm_log(port, "Enable BIST MODE TESTDATA"); | |
4794 | port->tcpc->set_bist_data(port->tcpc, true); | |
4795 | } | |
4796 | break; | |
f0690a25 GR |
4797 | default: |
4798 | break; | |
4799 | } | |
f0690a25 | 4800 | break; |
64f7c494 AT |
4801 | case GET_STATUS_SEND: |
4802 | tcpm_pd_send_control(port, PD_CTRL_GET_STATUS); | |
4803 | tcpm_set_state(port, GET_STATUS_SEND_TIMEOUT, | |
4804 | PD_T_SENDER_RESPONSE); | |
4805 | break; | |
4806 | case GET_STATUS_SEND_TIMEOUT: | |
4807 | tcpm_set_state(port, ready_state(port), 0); | |
4808 | break; | |
4809 | case GET_PPS_STATUS_SEND: | |
4810 | tcpm_pd_send_control(port, PD_CTRL_GET_PPS_STATUS); | |
4811 | tcpm_set_state(port, GET_PPS_STATUS_SEND_TIMEOUT, | |
4812 | PD_T_SENDER_RESPONSE); | |
4813 | break; | |
4814 | case GET_PPS_STATUS_SEND_TIMEOUT: | |
4815 | tcpm_set_state(port, ready_state(port), 0); | |
4816 | break; | |
8dc4bd07 BJS |
4817 | case GET_SINK_CAP: |
4818 | tcpm_pd_send_control(port, PD_CTRL_GET_SINK_CAP); | |
4819 | tcpm_set_state(port, GET_SINK_CAP_TIMEOUT, PD_T_SENDER_RESPONSE); | |
4820 | break; | |
4821 | case GET_SINK_CAP_TIMEOUT: | |
4822 | port->sink_cap_done = true; | |
4823 | tcpm_set_state(port, ready_state(port), 0); | |
4824 | break; | |
f0690a25 GR |
4825 | case ERROR_RECOVERY: |
4826 | tcpm_swap_complete(port, -EPROTO); | |
2eadc33f | 4827 | tcpm_pps_complete(port, -EPROTO); |
b17dd571 GR |
4828 | tcpm_set_state(port, PORT_RESET, 0); |
4829 | break; | |
4830 | case PORT_RESET: | |
f0690a25 | 4831 | tcpm_reset_port(port); |
f0690a25 | 4832 | tcpm_set_cc(port, TYPEC_CC_OPEN); |
b17dd571 | 4833 | tcpm_set_state(port, PORT_RESET_WAIT_OFF, |
f0690a25 GR |
4834 | PD_T_ERROR_RECOVERY); |
4835 | break; | |
b17dd571 | 4836 | case PORT_RESET_WAIT_OFF: |
f0690a25 GR |
4837 | tcpm_set_state(port, |
4838 | tcpm_default_state(port), | |
4839 | port->vbus_present ? PD_T_PS_SOURCE_OFF : 0); | |
4840 | break; | |
0908c5ac KT |
4841 | |
4842 | /* AMS intermediate state */ | |
4843 | case AMS_START: | |
4844 | if (port->upcoming_state == INVALID_STATE) { | |
4845 | tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ? | |
4846 | SRC_READY : SNK_READY, 0); | |
4847 | break; | |
4848 | } | |
4849 | ||
4850 | upcoming_state = port->upcoming_state; | |
4851 | port->upcoming_state = INVALID_STATE; | |
4852 | tcpm_set_state(port, upcoming_state, 0); | |
4853 | break; | |
8dea75e1 KT |
4854 | |
4855 | /* Chunk state */ | |
4856 | case CHUNK_NOT_SUPP: | |
4857 | tcpm_pd_send_control(port, PD_CTRL_NOT_SUPP); | |
4858 | tcpm_set_state(port, port->pwr_role == TYPEC_SOURCE ? SRC_READY : SNK_READY, 0); | |
4859 | break; | |
f0690a25 GR |
4860 | default: |
4861 | WARN(1, "Unexpected port state %d\n", port->state); | |
4862 | break; | |
4863 | } | |
4864 | } | |
4865 | ||
3ed8e1c2 | 4866 | static void tcpm_state_machine_work(struct kthread_work *work) |
f0690a25 | 4867 | { |
3ed8e1c2 | 4868 | struct tcpm_port *port = container_of(work, struct tcpm_port, state_machine); |
f0690a25 GR |
4869 | enum tcpm_state prev_state; |
4870 | ||
4871 | mutex_lock(&port->lock); | |
4872 | port->state_machine_running = true; | |
4873 | ||
4874 | if (port->queued_message && tcpm_send_queued_message(port)) | |
4875 | goto done; | |
4876 | ||
4877 | /* If we were queued due to a delayed state change, update it now */ | |
4878 | if (port->delayed_state) { | |
4879 | tcpm_log(port, "state change %s -> %s [delayed %ld ms]", | |
4880 | tcpm_states[port->state], | |
4881 | tcpm_states[port->delayed_state], port->delay_ms); | |
4882 | port->prev_state = port->state; | |
4883 | port->state = port->delayed_state; | |
4884 | port->delayed_state = INVALID_STATE; | |
4885 | } | |
4886 | ||
4887 | /* | |
4888 | * Continue running as long as we have (non-delayed) state changes | |
4889 | * to make. | |
4890 | */ | |
4891 | do { | |
4892 | prev_state = port->state; | |
4893 | run_state_machine(port); | |
4894 | if (port->queued_message) | |
4895 | tcpm_send_queued_message(port); | |
4896 | } while (port->state != prev_state && !port->delayed_state); | |
4897 | ||
4898 | done: | |
4899 | port->state_machine_running = false; | |
4900 | mutex_unlock(&port->lock); | |
4901 | } | |
4902 | ||
4903 | static void _tcpm_cc_change(struct tcpm_port *port, enum typec_cc_status cc1, | |
4904 | enum typec_cc_status cc2) | |
4905 | { | |
4906 | enum typec_cc_status old_cc1, old_cc2; | |
4907 | enum tcpm_state new_state; | |
4908 | ||
4909 | old_cc1 = port->cc1; | |
4910 | old_cc2 = port->cc2; | |
4911 | port->cc1 = cc1; | |
4912 | port->cc2 = cc2; | |
4913 | ||
4914 | tcpm_log_force(port, | |
4915 | "CC1: %u -> %u, CC2: %u -> %u [state %s, polarity %d, %s]", | |
4916 | old_cc1, cc1, old_cc2, cc2, tcpm_states[port->state], | |
4917 | port->polarity, | |
4918 | tcpm_port_is_disconnected(port) ? "disconnected" | |
4919 | : "connected"); | |
4920 | ||
4921 | switch (port->state) { | |
7893f9e1 | 4922 | case TOGGLING: |
f0690a25 GR |
4923 | if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) || |
4924 | tcpm_port_is_source(port)) | |
4925 | tcpm_set_state(port, SRC_ATTACH_WAIT, 0); | |
4926 | else if (tcpm_port_is_sink(port)) | |
4927 | tcpm_set_state(port, SNK_ATTACH_WAIT, 0); | |
4928 | break; | |
4929 | case SRC_UNATTACHED: | |
4930 | case ACC_UNATTACHED: | |
4931 | if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) || | |
4932 | tcpm_port_is_source(port)) | |
4933 | tcpm_set_state(port, SRC_ATTACH_WAIT, 0); | |
4934 | break; | |
4935 | case SRC_ATTACH_WAIT: | |
4936 | if (tcpm_port_is_disconnected(port) || | |
4937 | tcpm_port_is_audio_detached(port)) | |
4938 | tcpm_set_state(port, SRC_UNATTACHED, 0); | |
4939 | else if (cc1 != old_cc1 || cc2 != old_cc2) | |
4940 | tcpm_set_state(port, SRC_ATTACH_WAIT, 0); | |
4941 | break; | |
4942 | case SRC_ATTACHED: | |
6d91017a | 4943 | case SRC_STARTUP: |
f3b73364 BJS |
4944 | case SRC_SEND_CAPABILITIES: |
4945 | case SRC_READY: | |
4946 | if (tcpm_port_is_disconnected(port) || | |
67540460 LJ |
4947 | !tcpm_port_is_source(port)) { |
4948 | if (port->port_type == TYPEC_PORT_SRC) | |
3287f58b | 4949 | tcpm_set_state(port, SRC_UNATTACHED, tcpm_wait_for_discharge(port)); |
67540460 | 4950 | else |
3287f58b | 4951 | tcpm_set_state(port, SNK_UNATTACHED, tcpm_wait_for_discharge(port)); |
67540460 | 4952 | } |
f0690a25 | 4953 | break; |
f0690a25 GR |
4954 | case SNK_UNATTACHED: |
4955 | if (tcpm_port_is_sink(port)) | |
4956 | tcpm_set_state(port, SNK_ATTACH_WAIT, 0); | |
4957 | break; | |
4958 | case SNK_ATTACH_WAIT: | |
4959 | if ((port->cc1 == TYPEC_CC_OPEN && | |
4960 | port->cc2 != TYPEC_CC_OPEN) || | |
4961 | (port->cc1 != TYPEC_CC_OPEN && | |
4962 | port->cc2 == TYPEC_CC_OPEN)) | |
4963 | new_state = SNK_DEBOUNCED; | |
4964 | else if (tcpm_port_is_disconnected(port)) | |
4965 | new_state = SNK_UNATTACHED; | |
4966 | else | |
4967 | break; | |
4968 | if (new_state != port->delayed_state) | |
4969 | tcpm_set_state(port, SNK_ATTACH_WAIT, 0); | |
4970 | break; | |
4971 | case SNK_DEBOUNCED: | |
4972 | if (tcpm_port_is_disconnected(port)) | |
4973 | new_state = SNK_UNATTACHED; | |
4974 | else if (port->vbus_present) | |
4975 | new_state = tcpm_try_src(port) ? SRC_TRY : SNK_ATTACHED; | |
4976 | else | |
4977 | new_state = SNK_UNATTACHED; | |
4978 | if (new_state != port->delayed_state) | |
4979 | tcpm_set_state(port, SNK_DEBOUNCED, 0); | |
4980 | break; | |
4981 | case SNK_READY: | |
3287f58b BJS |
4982 | /* |
4983 | * EXIT condition is based primarily on vbus disconnect and CC is secondary. | |
4984 | * "A port that has entered into USB PD communications with the Source and | |
4985 | * has seen the CC voltage exceed vRd-USB may monitor the CC pin to detect | |
4986 | * cable disconnect in addition to monitoring VBUS. | |
4987 | * | |
4988 | * A port that is monitoring the CC voltage for disconnect (but is not in | |
4989 | * the process of a USB PD PR_Swap or USB PD FR_Swap) shall transition to | |
4990 | * Unattached.SNK within tSinkDisconnect after the CC voltage remains below | |
4991 | * vRd-USB for tPDDebounce." | |
4992 | * | |
4993 | * When set_auto_vbus_discharge_threshold is enabled, CC pins go | |
4994 | * away before vbus decays to disconnect threshold. Allow | |
4995 | * disconnect to be driven by vbus disconnect when auto vbus | |
4996 | * discharge is enabled. | |
4997 | */ | |
4998 | if (!port->auto_vbus_discharge_enabled && tcpm_port_is_disconnected(port)) | |
f0690a25 GR |
4999 | tcpm_set_state(port, unattached_state(port), 0); |
5000 | else if (!port->pd_capable && | |
5001 | (cc1 != old_cc1 || cc2 != old_cc2)) | |
5002 | tcpm_set_current_limit(port, | |
5003 | tcpm_get_current_limit(port), | |
5004 | 5000); | |
5005 | break; | |
5006 | ||
5007 | case AUDIO_ACC_ATTACHED: | |
5008 | if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN) | |
5009 | tcpm_set_state(port, AUDIO_ACC_DEBOUNCE, 0); | |
5010 | break; | |
5011 | case AUDIO_ACC_DEBOUNCE: | |
5012 | if (tcpm_port_is_audio(port)) | |
5013 | tcpm_set_state(port, AUDIO_ACC_ATTACHED, 0); | |
5014 | break; | |
5015 | ||
5016 | case DEBUG_ACC_ATTACHED: | |
5017 | if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN) | |
5018 | tcpm_set_state(port, ACC_UNATTACHED, 0); | |
5019 | break; | |
5020 | ||
5021 | case SNK_TRY: | |
5022 | /* Do nothing, waiting for timeout */ | |
5023 | break; | |
5024 | ||
5025 | case SNK_DISCOVERY: | |
5026 | /* CC line is unstable, wait for debounce */ | |
5027 | if (tcpm_port_is_disconnected(port)) | |
5028 | tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE, 0); | |
5029 | break; | |
5030 | case SNK_DISCOVERY_DEBOUNCE: | |
5031 | break; | |
5032 | ||
5033 | case SRC_TRYWAIT: | |
5034 | /* Hand over to state machine if needed */ | |
5035 | if (!port->vbus_present && tcpm_port_is_source(port)) | |
02d5be46 BJS |
5036 | tcpm_set_state(port, SRC_TRYWAIT_DEBOUNCE, 0); |
5037 | break; | |
5038 | case SRC_TRYWAIT_DEBOUNCE: | |
5039 | if (port->vbus_present || !tcpm_port_is_source(port)) | |
f0690a25 GR |
5040 | tcpm_set_state(port, SRC_TRYWAIT, 0); |
5041 | break; | |
a0a3e04e BJS |
5042 | case SNK_TRY_WAIT_DEBOUNCE: |
5043 | if (!tcpm_port_is_sink(port)) { | |
5044 | port->max_wait = 0; | |
5045 | tcpm_set_state(port, SRC_TRYWAIT, 0); | |
f0690a25 | 5046 | } |
f0690a25 | 5047 | break; |
131c7d12 | 5048 | case SRC_TRY_WAIT: |
c79d92bd BJS |
5049 | if (tcpm_port_is_source(port)) |
5050 | tcpm_set_state(port, SRC_TRY_DEBOUNCE, 0); | |
f0690a25 GR |
5051 | break; |
5052 | case SRC_TRY_DEBOUNCE: | |
131c7d12 | 5053 | tcpm_set_state(port, SRC_TRY_WAIT, 0); |
f0690a25 GR |
5054 | break; |
5055 | case SNK_TRYWAIT_DEBOUNCE: | |
af450ebb BJS |
5056 | if (tcpm_port_is_sink(port)) |
5057 | tcpm_set_state(port, SNK_TRYWAIT_VBUS, 0); | |
5058 | break; | |
5059 | case SNK_TRYWAIT_VBUS: | |
5060 | if (!tcpm_port_is_sink(port)) | |
f0690a25 GR |
5061 | tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, 0); |
5062 | break; | |
11e5e568 BJS |
5063 | case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS: |
5064 | if (!tcpm_port_is_sink(port)) | |
5065 | tcpm_set_state(port, SRC_TRYWAIT, PD_T_TRY_CC_DEBOUNCE); | |
5066 | else | |
5067 | tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS, 0); | |
5068 | break; | |
af450ebb BJS |
5069 | case SNK_TRYWAIT: |
5070 | /* Do nothing, waiting for tCCDebounce */ | |
5071 | break; | |
f0690a25 GR |
5072 | case PR_SWAP_SNK_SRC_SINK_OFF: |
5073 | case PR_SWAP_SRC_SNK_TRANSITION_OFF: | |
5074 | case PR_SWAP_SRC_SNK_SOURCE_OFF: | |
b965b631 BJS |
5075 | case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED: |
5076 | case PR_SWAP_SNK_SRC_SOURCE_ON: | |
f0690a25 | 5077 | /* |
b965b631 | 5078 | * CC state change is expected in PR_SWAP |
f0690a25 GR |
5079 | * Ignore it. |
5080 | */ | |
5081 | break; | |
8dc4bd07 BJS |
5082 | case FR_SWAP_SEND: |
5083 | case FR_SWAP_SEND_TIMEOUT: | |
5084 | case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF: | |
5085 | case FR_SWAP_SNK_SRC_NEW_SINK_READY: | |
5086 | case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED: | |
5087 | /* Do nothing, CC change expected */ | |
5088 | break; | |
f0690a25 | 5089 | |
90178974 BJS |
5090 | case PORT_RESET: |
5091 | case PORT_RESET_WAIT_OFF: | |
5092 | /* | |
5093 | * State set back to default mode once the timer completes. | |
5094 | * Ignore CC changes here. | |
5095 | */ | |
5096 | break; | |
f0690a25 | 5097 | default: |
3287f58b BJS |
5098 | /* |
5099 | * While acting as sink and auto vbus discharge is enabled, Allow disconnect | |
5100 | * to be driven by vbus disconnect. | |
5101 | */ | |
5102 | if (tcpm_port_is_disconnected(port) && !(port->pwr_role == TYPEC_SINK && | |
5103 | port->auto_vbus_discharge_enabled)) | |
f0690a25 GR |
5104 | tcpm_set_state(port, unattached_state(port), 0); |
5105 | break; | |
5106 | } | |
5107 | } | |
5108 | ||
5109 | static void _tcpm_pd_vbus_on(struct tcpm_port *port) | |
5110 | { | |
f0690a25 GR |
5111 | tcpm_log_force(port, "VBUS on"); |
5112 | port->vbus_present = true; | |
28b43d3d BJS |
5113 | /* |
5114 | * When vbus_present is true i.e. Voltage at VBUS is greater than VSAFE5V implicitly | |
5115 | * states that vbus is not at VSAFE0V, hence clear the vbus_vsafe0v flag here. | |
5116 | */ | |
5117 | port->vbus_vsafe0v = false; | |
5118 | ||
f0690a25 GR |
5119 | switch (port->state) { |
5120 | case SNK_TRANSITION_SINK_VBUS: | |
8bf05746 | 5121 | port->explicit_contract = true; |
f0690a25 GR |
5122 | tcpm_set_state(port, SNK_READY, 0); |
5123 | break; | |
5124 | case SNK_DISCOVERY: | |
5125 | tcpm_set_state(port, SNK_DISCOVERY, 0); | |
5126 | break; | |
5127 | ||
5128 | case SNK_DEBOUNCED: | |
5129 | tcpm_set_state(port, tcpm_try_src(port) ? SRC_TRY | |
5130 | : SNK_ATTACHED, | |
5131 | 0); | |
5132 | break; | |
5133 | case SNK_HARD_RESET_WAIT_VBUS: | |
5134 | tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, 0); | |
5135 | break; | |
5136 | case SRC_ATTACHED: | |
5137 | tcpm_set_state(port, SRC_STARTUP, 0); | |
5138 | break; | |
5139 | case SRC_HARD_RESET_VBUS_ON: | |
5140 | tcpm_set_state(port, SRC_STARTUP, 0); | |
5141 | break; | |
5142 | ||
5143 | case SNK_TRY: | |
5144 | /* Do nothing, waiting for timeout */ | |
5145 | break; | |
5146 | case SRC_TRYWAIT: | |
02d5be46 BJS |
5147 | /* Do nothing, Waiting for Rd to be detected */ |
5148 | break; | |
5149 | case SRC_TRYWAIT_DEBOUNCE: | |
5150 | tcpm_set_state(port, SRC_TRYWAIT, 0); | |
f0690a25 | 5151 | break; |
a0a3e04e BJS |
5152 | case SNK_TRY_WAIT_DEBOUNCE: |
5153 | /* Do nothing, waiting for PD_DEBOUNCE to do be done */ | |
f0690a25 GR |
5154 | break; |
5155 | case SNK_TRYWAIT: | |
af450ebb BJS |
5156 | /* Do nothing, waiting for tCCDebounce */ |
5157 | break; | |
5158 | case SNK_TRYWAIT_VBUS: | |
5159 | if (tcpm_port_is_sink(port)) | |
5160 | tcpm_set_state(port, SNK_ATTACHED, 0); | |
5161 | break; | |
5162 | case SNK_TRYWAIT_DEBOUNCE: | |
5163 | /* Do nothing, waiting for Rp */ | |
f0690a25 | 5164 | break; |
11e5e568 BJS |
5165 | case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS: |
5166 | if (port->vbus_present && tcpm_port_is_sink(port)) | |
5167 | tcpm_set_state(port, SNK_ATTACHED, 0); | |
5168 | break; | |
131c7d12 BJS |
5169 | case SRC_TRY_WAIT: |
5170 | case SRC_TRY_DEBOUNCE: | |
5171 | /* Do nothing, waiting for sink detection */ | |
5172 | break; | |
a30a00e3 BJS |
5173 | case FR_SWAP_SEND: |
5174 | case FR_SWAP_SEND_TIMEOUT: | |
5175 | case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF: | |
5176 | case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED: | |
5177 | if (port->tcpc->frs_sourcing_vbus) | |
5178 | port->tcpc->frs_sourcing_vbus(port->tcpc); | |
5179 | break; | |
8dc4bd07 | 5180 | case FR_SWAP_SNK_SRC_NEW_SINK_READY: |
a30a00e3 BJS |
5181 | if (port->tcpc->frs_sourcing_vbus) |
5182 | port->tcpc->frs_sourcing_vbus(port->tcpc); | |
8dc4bd07 BJS |
5183 | tcpm_set_state(port, FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED, 0); |
5184 | break; | |
90178974 BJS |
5185 | |
5186 | case PORT_RESET: | |
5187 | case PORT_RESET_WAIT_OFF: | |
5188 | /* | |
5189 | * State set back to default mode once the timer completes. | |
5190 | * Ignore vbus changes here. | |
5191 | */ | |
5192 | break; | |
5193 | ||
f0690a25 GR |
5194 | default: |
5195 | break; | |
5196 | } | |
5197 | } | |
5198 | ||
5199 | static void _tcpm_pd_vbus_off(struct tcpm_port *port) | |
5200 | { | |
f0690a25 GR |
5201 | tcpm_log_force(port, "VBUS off"); |
5202 | port->vbus_present = false; | |
5203 | port->vbus_never_low = false; | |
5204 | switch (port->state) { | |
5205 | case SNK_HARD_RESET_SINK_OFF: | |
5206 | tcpm_set_state(port, SNK_HARD_RESET_WAIT_VBUS, 0); | |
5207 | break; | |
f0690a25 GR |
5208 | case HARD_RESET_SEND: |
5209 | break; | |
f0690a25 GR |
5210 | case SNK_TRY: |
5211 | /* Do nothing, waiting for timeout */ | |
5212 | break; | |
5213 | case SRC_TRYWAIT: | |
5214 | /* Hand over to state machine if needed */ | |
5215 | if (tcpm_port_is_source(port)) | |
02d5be46 | 5216 | tcpm_set_state(port, SRC_TRYWAIT_DEBOUNCE, 0); |
f0690a25 | 5217 | break; |
a0a3e04e BJS |
5218 | case SNK_TRY_WAIT_DEBOUNCE: |
5219 | /* Do nothing, waiting for PD_DEBOUNCE to do be done */ | |
f0690a25 | 5220 | break; |
af450ebb | 5221 | case SNK_TRYWAIT: |
f0690a25 | 5222 | case SNK_TRYWAIT_VBUS: |
af450ebb | 5223 | case SNK_TRYWAIT_DEBOUNCE: |
f0690a25 | 5224 | break; |
f0690a25 | 5225 | case SNK_ATTACH_WAIT: |
90b8aa9f BJS |
5226 | case SNK_DEBOUNCED: |
5227 | /* Do nothing, as TCPM is still waiting for vbus to reaach VSAFE5V to connect */ | |
f0690a25 GR |
5228 | break; |
5229 | ||
5230 | case SNK_NEGOTIATE_CAPABILITIES: | |
5231 | break; | |
5232 | ||
5233 | case PR_SWAP_SRC_SNK_TRANSITION_OFF: | |
5234 | tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF, 0); | |
5235 | break; | |
5236 | ||
5237 | case PR_SWAP_SNK_SRC_SINK_OFF: | |
5238 | /* Do nothing, expected */ | |
5239 | break; | |
5240 | ||
7abc6ca5 BJS |
5241 | case PR_SWAP_SNK_SRC_SOURCE_ON: |
5242 | /* | |
5243 | * Do nothing when vbus off notification is received. | |
5244 | * TCPM can wait for PD_T_NEWSRC in PR_SWAP_SNK_SRC_SOURCE_ON | |
5245 | * for the vbus source to ramp up. | |
5246 | */ | |
5247 | break; | |
5248 | ||
b17dd571 | 5249 | case PORT_RESET_WAIT_OFF: |
c749d4d0 | 5250 | tcpm_set_state(port, tcpm_default_state(port), 0); |
f0690a25 | 5251 | break; |
90178974 | 5252 | |
131c7d12 BJS |
5253 | case SRC_TRY_WAIT: |
5254 | case SRC_TRY_DEBOUNCE: | |
5255 | /* Do nothing, waiting for sink detection */ | |
5256 | break; | |
90178974 | 5257 | |
7771bcc7 BJS |
5258 | case SRC_STARTUP: |
5259 | case SRC_SEND_CAPABILITIES: | |
5260 | case SRC_SEND_CAPABILITIES_TIMEOUT: | |
5261 | case SRC_NEGOTIATE_CAPABILITIES: | |
5262 | case SRC_TRANSITION_SUPPLY: | |
5263 | case SRC_READY: | |
5264 | case SRC_WAIT_NEW_CAPABILITIES: | |
3287f58b BJS |
5265 | /* |
5266 | * Force to unattached state to re-initiate connection. | |
5267 | * DRP port should move to Unattached.SNK instead of Unattached.SRC if | |
5268 | * sink removed. Although sink removal here is due to source's vbus collapse, | |
5269 | * treat it the same way for consistency. | |
5270 | */ | |
5271 | if (port->port_type == TYPEC_PORT_SRC) | |
5272 | tcpm_set_state(port, SRC_UNATTACHED, tcpm_wait_for_discharge(port)); | |
5273 | else | |
5274 | tcpm_set_state(port, SNK_UNATTACHED, tcpm_wait_for_discharge(port)); | |
7771bcc7 BJS |
5275 | break; |
5276 | ||
90178974 BJS |
5277 | case PORT_RESET: |
5278 | /* | |
5279 | * State set back to default mode once the timer completes. | |
5280 | * Ignore vbus changes here. | |
5281 | */ | |
5282 | break; | |
5283 | ||
8dc4bd07 BJS |
5284 | case FR_SWAP_SEND: |
5285 | case FR_SWAP_SEND_TIMEOUT: | |
5286 | case FR_SWAP_SNK_SRC_TRANSITION_TO_OFF: | |
5287 | case FR_SWAP_SNK_SRC_NEW_SINK_READY: | |
5288 | case FR_SWAP_SNK_SRC_SOURCE_VBUS_APPLIED: | |
5289 | /* Do nothing, vbus drop expected */ | |
5290 | break; | |
5291 | ||
f0690a25 | 5292 | default: |
3287f58b BJS |
5293 | if (port->pwr_role == TYPEC_SINK && port->attached) |
5294 | tcpm_set_state(port, SNK_UNATTACHED, tcpm_wait_for_discharge(port)); | |
f0690a25 GR |
5295 | break; |
5296 | } | |
5297 | } | |
5298 | ||
28b43d3d BJS |
5299 | static void _tcpm_pd_vbus_vsafe0v(struct tcpm_port *port) |
5300 | { | |
5301 | tcpm_log_force(port, "VBUS VSAFE0V"); | |
5302 | port->vbus_vsafe0v = true; | |
5303 | switch (port->state) { | |
5304 | case SRC_HARD_RESET_VBUS_OFF: | |
5305 | /* | |
5306 | * After establishing the vSafe0V voltage condition on VBUS, the Source Shall wait | |
5307 | * tSrcRecover before re-applying VCONN and restoring VBUS to vSafe5V. | |
5308 | */ | |
5309 | tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SRC_RECOVER); | |
5310 | break; | |
5311 | case SRC_ATTACH_WAIT: | |
5312 | if (tcpm_port_is_source(port)) | |
5313 | tcpm_set_state(port, tcpm_try_snk(port) ? SNK_TRY : SRC_ATTACHED, | |
5314 | PD_T_CC_DEBOUNCE); | |
5315 | break; | |
3287f58b BJS |
5316 | case SRC_STARTUP: |
5317 | case SRC_SEND_CAPABILITIES: | |
5318 | case SRC_SEND_CAPABILITIES_TIMEOUT: | |
5319 | case SRC_NEGOTIATE_CAPABILITIES: | |
5320 | case SRC_TRANSITION_SUPPLY: | |
5321 | case SRC_READY: | |
5322 | case SRC_WAIT_NEW_CAPABILITIES: | |
5323 | if (port->auto_vbus_discharge_enabled) { | |
5324 | if (port->port_type == TYPEC_PORT_SRC) | |
5325 | tcpm_set_state(port, SRC_UNATTACHED, 0); | |
5326 | else | |
5327 | tcpm_set_state(port, SNK_UNATTACHED, 0); | |
5328 | } | |
5329 | break; | |
d112efbe | 5330 | case PR_SWAP_SNK_SRC_SINK_OFF: |
fed09e0b | 5331 | case PR_SWAP_SNK_SRC_SOURCE_ON: |
d112efbe BJS |
5332 | /* Do nothing, vsafe0v is expected during transition */ |
5333 | break; | |
746f96e7 BJS |
5334 | case SNK_ATTACH_WAIT: |
5335 | case SNK_DEBOUNCED: | |
5336 | /*Do nothing, still waiting for VSAFE5V for connect */ | |
5337 | break; | |
28b43d3d | 5338 | default: |
3287f58b BJS |
5339 | if (port->pwr_role == TYPEC_SINK && port->auto_vbus_discharge_enabled) |
5340 | tcpm_set_state(port, SNK_UNATTACHED, 0); | |
28b43d3d BJS |
5341 | break; |
5342 | } | |
5343 | } | |
5344 | ||
f0690a25 GR |
5345 | static void _tcpm_pd_hard_reset(struct tcpm_port *port) |
5346 | { | |
5347 | tcpm_log_force(port, "Received hard reset"); | |
6e1c2241 BJS |
5348 | if (port->bist_request == BDO_MODE_TESTDATA && port->tcpc->set_bist_data) |
5349 | port->tcpc->set_bist_data(port->tcpc, false); | |
5350 | ||
0908c5ac KT |
5351 | if (port->ams != NONE_AMS) |
5352 | port->ams = NONE_AMS; | |
8dea75e1 KT |
5353 | if (port->hard_reset_count < PD_N_HARD_RESET_COUNT) |
5354 | port->ams = HARD_RESET; | |
f0690a25 GR |
5355 | /* |
5356 | * If we keep receiving hard reset requests, executing the hard reset | |
5357 | * must have failed. Revert to error recovery if that happens. | |
5358 | */ | |
5359 | tcpm_set_state(port, | |
5360 | port->hard_reset_count < PD_N_HARD_RESET_COUNT ? | |
5361 | HARD_RESET_START : ERROR_RECOVERY, | |
5362 | 0); | |
5363 | } | |
5364 | ||
3ed8e1c2 | 5365 | static void tcpm_pd_event_handler(struct kthread_work *work) |
f0690a25 GR |
5366 | { |
5367 | struct tcpm_port *port = container_of(work, struct tcpm_port, | |
5368 | event_work); | |
5369 | u32 events; | |
5370 | ||
5371 | mutex_lock(&port->lock); | |
5372 | ||
5373 | spin_lock(&port->pd_event_lock); | |
5374 | while (port->pd_events) { | |
5375 | events = port->pd_events; | |
5376 | port->pd_events = 0; | |
5377 | spin_unlock(&port->pd_event_lock); | |
5378 | if (events & TCPM_RESET_EVENT) | |
5379 | _tcpm_pd_hard_reset(port); | |
5380 | if (events & TCPM_VBUS_EVENT) { | |
5381 | bool vbus; | |
5382 | ||
5383 | vbus = port->tcpc->get_vbus(port->tcpc); | |
28b43d3d | 5384 | if (vbus) { |
f0690a25 | 5385 | _tcpm_pd_vbus_on(port); |
28b43d3d | 5386 | } else { |
f0690a25 | 5387 | _tcpm_pd_vbus_off(port); |
28b43d3d BJS |
5388 | /* |
5389 | * When TCPC does not support detecting vsafe0v voltage level, | |
5390 | * treat vbus absent as vsafe0v. Else invoke is_vbus_vsafe0v | |
5391 | * to see if vbus has discharge to VSAFE0V. | |
5392 | */ | |
5393 | if (!port->tcpc->is_vbus_vsafe0v || | |
5394 | port->tcpc->is_vbus_vsafe0v(port->tcpc)) | |
5395 | _tcpm_pd_vbus_vsafe0v(port); | |
5396 | } | |
f0690a25 GR |
5397 | } |
5398 | if (events & TCPM_CC_EVENT) { | |
5399 | enum typec_cc_status cc1, cc2; | |
5400 | ||
5401 | if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0) | |
5402 | _tcpm_cc_change(port, cc1, cc2); | |
5403 | } | |
8dc4bd07 | 5404 | if (events & TCPM_FRS_EVENT) { |
0908c5ac KT |
5405 | if (port->state == SNK_READY) { |
5406 | int ret; | |
5407 | ||
5408 | port->upcoming_state = FR_SWAP_SEND; | |
5409 | ret = tcpm_ams_start(port, FAST_ROLE_SWAP); | |
5410 | if (ret == -EAGAIN) | |
5411 | port->upcoming_state = INVALID_STATE; | |
5412 | } else { | |
8dc4bd07 | 5413 | tcpm_log(port, "Discarding FRS_SIGNAL! Not in sink ready"); |
0908c5ac | 5414 | } |
8dc4bd07 BJS |
5415 | } |
5416 | if (events & TCPM_SOURCING_VBUS) { | |
5417 | tcpm_log(port, "sourcing vbus"); | |
5418 | /* | |
5419 | * In fast role swap case TCPC autonomously sources vbus. Set vbus_source | |
5420 | * true as TCPM wouldn't have called tcpm_set_vbus. | |
5421 | * | |
5422 | * When vbus is sourced on the command on TCPM i.e. TCPM called | |
5423 | * tcpm_set_vbus to source vbus, vbus_source would already be true. | |
5424 | */ | |
5425 | port->vbus_source = true; | |
5426 | _tcpm_pd_vbus_on(port); | |
5427 | } | |
5428 | ||
f0690a25 GR |
5429 | spin_lock(&port->pd_event_lock); |
5430 | } | |
5431 | spin_unlock(&port->pd_event_lock); | |
5432 | mutex_unlock(&port->lock); | |
5433 | } | |
5434 | ||
5435 | void tcpm_cc_change(struct tcpm_port *port) | |
5436 | { | |
5437 | spin_lock(&port->pd_event_lock); | |
5438 | port->pd_events |= TCPM_CC_EVENT; | |
5439 | spin_unlock(&port->pd_event_lock); | |
3ed8e1c2 | 5440 | kthread_queue_work(port->wq, &port->event_work); |
f0690a25 GR |
5441 | } |
5442 | EXPORT_SYMBOL_GPL(tcpm_cc_change); | |
5443 | ||
5444 | void tcpm_vbus_change(struct tcpm_port *port) | |
5445 | { | |
5446 | spin_lock(&port->pd_event_lock); | |
5447 | port->pd_events |= TCPM_VBUS_EVENT; | |
5448 | spin_unlock(&port->pd_event_lock); | |
3ed8e1c2 | 5449 | kthread_queue_work(port->wq, &port->event_work); |
f0690a25 GR |
5450 | } |
5451 | EXPORT_SYMBOL_GPL(tcpm_vbus_change); | |
5452 | ||
5453 | void tcpm_pd_hard_reset(struct tcpm_port *port) | |
5454 | { | |
5455 | spin_lock(&port->pd_event_lock); | |
5456 | port->pd_events = TCPM_RESET_EVENT; | |
5457 | spin_unlock(&port->pd_event_lock); | |
3ed8e1c2 | 5458 | kthread_queue_work(port->wq, &port->event_work); |
f0690a25 GR |
5459 | } |
5460 | EXPORT_SYMBOL_GPL(tcpm_pd_hard_reset); | |
5461 | ||
8dc4bd07 BJS |
5462 | void tcpm_sink_frs(struct tcpm_port *port) |
5463 | { | |
5464 | spin_lock(&port->pd_event_lock); | |
43ad944c | 5465 | port->pd_events |= TCPM_FRS_EVENT; |
8dc4bd07 BJS |
5466 | spin_unlock(&port->pd_event_lock); |
5467 | kthread_queue_work(port->wq, &port->event_work); | |
5468 | } | |
5469 | EXPORT_SYMBOL_GPL(tcpm_sink_frs); | |
5470 | ||
5471 | void tcpm_sourcing_vbus(struct tcpm_port *port) | |
5472 | { | |
5473 | spin_lock(&port->pd_event_lock); | |
43ad944c | 5474 | port->pd_events |= TCPM_SOURCING_VBUS; |
8dc4bd07 BJS |
5475 | spin_unlock(&port->pd_event_lock); |
5476 | kthread_queue_work(port->wq, &port->event_work); | |
5477 | } | |
5478 | EXPORT_SYMBOL_GPL(tcpm_sourcing_vbus); | |
5479 | ||
5480 | static void tcpm_enable_frs_work(struct kthread_work *work) | |
5481 | { | |
5482 | struct tcpm_port *port = container_of(work, struct tcpm_port, enable_frs); | |
0908c5ac | 5483 | int ret; |
8dc4bd07 BJS |
5484 | |
5485 | mutex_lock(&port->lock); | |
5486 | /* Not FRS capable */ | |
5487 | if (!port->connected || port->port_type != TYPEC_PORT_DRP || | |
5488 | port->pwr_opmode != TYPEC_PWR_MODE_PD || | |
5489 | !port->tcpc->enable_frs || | |
5490 | /* Sink caps queried */ | |
5491 | port->sink_cap_done || port->negotiated_rev < PD_REV30) | |
5492 | goto unlock; | |
5493 | ||
5494 | /* Send when the state machine is idle */ | |
2b8c956e | 5495 | if (port->state != SNK_READY || port->vdm_sm_running || port->send_discover) |
8dc4bd07 BJS |
5496 | goto resched; |
5497 | ||
0908c5ac KT |
5498 | port->upcoming_state = GET_SINK_CAP; |
5499 | ret = tcpm_ams_start(port, GET_SINK_CAPABILITIES); | |
5500 | if (ret == -EAGAIN) { | |
5501 | port->upcoming_state = INVALID_STATE; | |
5502 | } else { | |
5503 | port->sink_cap_done = true; | |
5504 | goto unlock; | |
5505 | } | |
8dc4bd07 BJS |
5506 | resched: |
5507 | mod_enable_frs_delayed_work(port, GET_SINK_CAP_RETRY_MS); | |
5508 | unlock: | |
5509 | mutex_unlock(&port->lock); | |
5510 | } | |
5511 | ||
c34e85fa KT |
5512 | static void tcpm_send_discover_work(struct kthread_work *work) |
5513 | { | |
5514 | struct tcpm_port *port = container_of(work, struct tcpm_port, send_discover_work); | |
5515 | ||
5516 | mutex_lock(&port->lock); | |
5517 | /* No need to send DISCOVER_IDENTITY anymore */ | |
5518 | if (!port->send_discover) | |
5519 | goto unlock; | |
5520 | ||
ef52b4a9 KT |
5521 | if (port->data_role == TYPEC_DEVICE && port->negotiated_rev < PD_REV30) { |
5522 | port->send_discover = false; | |
5523 | goto unlock; | |
5524 | } | |
5525 | ||
c34e85fa KT |
5526 | /* Retry if the port is not idle */ |
5527 | if ((port->state != SRC_READY && port->state != SNK_READY) || port->vdm_sm_running) { | |
5528 | mod_send_discover_delayed_work(port, SEND_DISCOVER_RETRY_MS); | |
5529 | goto unlock; | |
5530 | } | |
5531 | ||
ef52b4a9 | 5532 | tcpm_send_vdm(port, USB_SID_PD, CMD_DISCOVER_IDENT, NULL, 0); |
c34e85fa KT |
5533 | |
5534 | unlock: | |
5535 | mutex_unlock(&port->lock); | |
5536 | } | |
5537 | ||
00ec21e5 | 5538 | static int tcpm_dr_set(struct typec_port *p, enum typec_data_role data) |
f0690a25 | 5539 | { |
00ec21e5 | 5540 | struct tcpm_port *port = typec_get_drvdata(p); |
f0690a25 GR |
5541 | int ret; |
5542 | ||
5543 | mutex_lock(&port->swap_lock); | |
5544 | mutex_lock(&port->lock); | |
5545 | ||
6ecc632d | 5546 | if (port->typec_caps.data != TYPEC_PORT_DRD) { |
f0690a25 GR |
5547 | ret = -EINVAL; |
5548 | goto port_unlock; | |
5549 | } | |
5550 | if (port->state != SRC_READY && port->state != SNK_READY) { | |
5551 | ret = -EAGAIN; | |
5552 | goto port_unlock; | |
5553 | } | |
5554 | ||
5555 | if (port->data_role == data) { | |
5556 | ret = 0; | |
5557 | goto port_unlock; | |
5558 | } | |
5559 | ||
5560 | /* | |
5561 | * XXX | |
5562 | * 6.3.9: If an alternate mode is active, a request to swap | |
5563 | * alternate modes shall trigger a port reset. | |
5564 | * Reject data role swap request in this case. | |
5565 | */ | |
5566 | ||
b17dd571 GR |
5567 | if (!port->pd_capable) { |
5568 | /* | |
5569 | * If the partner is not PD capable, reset the port to | |
5570 | * trigger a role change. This can only work if a preferred | |
5571 | * role is configured, and if it matches the requested role. | |
5572 | */ | |
5573 | if (port->try_role == TYPEC_NO_PREFERRED_ROLE || | |
5574 | port->try_role == port->pwr_role) { | |
5575 | ret = -EINVAL; | |
5576 | goto port_unlock; | |
5577 | } | |
5578 | port->non_pd_role_swap = true; | |
5579 | tcpm_set_state(port, PORT_RESET, 0); | |
5580 | } else { | |
0908c5ac KT |
5581 | port->upcoming_state = DR_SWAP_SEND; |
5582 | ret = tcpm_ams_start(port, DATA_ROLE_SWAP); | |
5583 | if (ret == -EAGAIN) { | |
5584 | port->upcoming_state = INVALID_STATE; | |
5585 | goto port_unlock; | |
5586 | } | |
b17dd571 GR |
5587 | } |
5588 | ||
f0690a25 GR |
5589 | port->swap_status = 0; |
5590 | port->swap_pending = true; | |
5591 | reinit_completion(&port->swap_complete); | |
f0690a25 GR |
5592 | mutex_unlock(&port->lock); |
5593 | ||
9adf9f9e GR |
5594 | if (!wait_for_completion_timeout(&port->swap_complete, |
5595 | msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT))) | |
5596 | ret = -ETIMEDOUT; | |
5597 | else | |
5598 | ret = port->swap_status; | |
f0690a25 | 5599 | |
b17dd571 | 5600 | port->non_pd_role_swap = false; |
f0690a25 GR |
5601 | goto swap_unlock; |
5602 | ||
5603 | port_unlock: | |
5604 | mutex_unlock(&port->lock); | |
5605 | swap_unlock: | |
5606 | mutex_unlock(&port->swap_lock); | |
5607 | return ret; | |
5608 | } | |
5609 | ||
00ec21e5 | 5610 | static int tcpm_pr_set(struct typec_port *p, enum typec_role role) |
f0690a25 | 5611 | { |
00ec21e5 | 5612 | struct tcpm_port *port = typec_get_drvdata(p); |
f0690a25 GR |
5613 | int ret; |
5614 | ||
5615 | mutex_lock(&port->swap_lock); | |
5616 | mutex_lock(&port->lock); | |
5617 | ||
9b0ae699 | 5618 | if (port->port_type != TYPEC_PORT_DRP) { |
f0690a25 GR |
5619 | ret = -EINVAL; |
5620 | goto port_unlock; | |
5621 | } | |
5622 | if (port->state != SRC_READY && port->state != SNK_READY) { | |
5623 | ret = -EAGAIN; | |
5624 | goto port_unlock; | |
5625 | } | |
5626 | ||
5627 | if (role == port->pwr_role) { | |
5628 | ret = 0; | |
5629 | goto port_unlock; | |
5630 | } | |
5631 | ||
0908c5ac KT |
5632 | port->upcoming_state = PR_SWAP_SEND; |
5633 | ret = tcpm_ams_start(port, POWER_ROLE_SWAP); | |
5634 | if (ret == -EAGAIN) { | |
5635 | port->upcoming_state = INVALID_STATE; | |
5636 | goto port_unlock; | |
5637 | } | |
5638 | ||
f0690a25 GR |
5639 | port->swap_status = 0; |
5640 | port->swap_pending = true; | |
5641 | reinit_completion(&port->swap_complete); | |
f0690a25 GR |
5642 | mutex_unlock(&port->lock); |
5643 | ||
9adf9f9e GR |
5644 | if (!wait_for_completion_timeout(&port->swap_complete, |
5645 | msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT))) | |
5646 | ret = -ETIMEDOUT; | |
5647 | else | |
5648 | ret = port->swap_status; | |
f0690a25 | 5649 | |
f0690a25 GR |
5650 | goto swap_unlock; |
5651 | ||
5652 | port_unlock: | |
5653 | mutex_unlock(&port->lock); | |
5654 | swap_unlock: | |
5655 | mutex_unlock(&port->swap_lock); | |
5656 | return ret; | |
5657 | } | |
5658 | ||
00ec21e5 | 5659 | static int tcpm_vconn_set(struct typec_port *p, enum typec_role role) |
f0690a25 | 5660 | { |
00ec21e5 | 5661 | struct tcpm_port *port = typec_get_drvdata(p); |
f0690a25 GR |
5662 | int ret; |
5663 | ||
5664 | mutex_lock(&port->swap_lock); | |
5665 | mutex_lock(&port->lock); | |
5666 | ||
5667 | if (port->state != SRC_READY && port->state != SNK_READY) { | |
5668 | ret = -EAGAIN; | |
5669 | goto port_unlock; | |
5670 | } | |
5671 | ||
5672 | if (role == port->vconn_role) { | |
5673 | ret = 0; | |
5674 | goto port_unlock; | |
5675 | } | |
5676 | ||
0908c5ac KT |
5677 | port->upcoming_state = VCONN_SWAP_SEND; |
5678 | ret = tcpm_ams_start(port, VCONN_SWAP); | |
5679 | if (ret == -EAGAIN) { | |
5680 | port->upcoming_state = INVALID_STATE; | |
5681 | goto port_unlock; | |
5682 | } | |
5683 | ||
f0690a25 GR |
5684 | port->swap_status = 0; |
5685 | port->swap_pending = true; | |
5686 | reinit_completion(&port->swap_complete); | |
f0690a25 GR |
5687 | mutex_unlock(&port->lock); |
5688 | ||
9adf9f9e GR |
5689 | if (!wait_for_completion_timeout(&port->swap_complete, |
5690 | msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT))) | |
5691 | ret = -ETIMEDOUT; | |
5692 | else | |
5693 | ret = port->swap_status; | |
f0690a25 | 5694 | |
f0690a25 GR |
5695 | goto swap_unlock; |
5696 | ||
5697 | port_unlock: | |
5698 | mutex_unlock(&port->lock); | |
5699 | swap_unlock: | |
5700 | mutex_unlock(&port->swap_lock); | |
5701 | return ret; | |
5702 | } | |
5703 | ||
00ec21e5 | 5704 | static int tcpm_try_role(struct typec_port *p, int role) |
f0690a25 | 5705 | { |
00ec21e5 | 5706 | struct tcpm_port *port = typec_get_drvdata(p); |
f0690a25 GR |
5707 | struct tcpc_dev *tcpc = port->tcpc; |
5708 | int ret = 0; | |
5709 | ||
5710 | mutex_lock(&port->lock); | |
5711 | if (tcpc->try_role) | |
5712 | ret = tcpc->try_role(tcpc, role); | |
a079973f | 5713 | if (!ret) |
f0690a25 GR |
5714 | port->try_role = role; |
5715 | port->try_src_count = 0; | |
5716 | port->try_snk_count = 0; | |
5717 | mutex_unlock(&port->lock); | |
5718 | ||
5719 | return ret; | |
5720 | } | |
5721 | ||
e3a07202 | 5722 | static int tcpm_pps_set_op_curr(struct tcpm_port *port, u16 req_op_curr) |
2eadc33f AT |
5723 | { |
5724 | unsigned int target_mw; | |
5725 | int ret; | |
5726 | ||
5727 | mutex_lock(&port->swap_lock); | |
5728 | mutex_lock(&port->lock); | |
5729 | ||
5730 | if (!port->pps_data.active) { | |
5731 | ret = -EOPNOTSUPP; | |
5732 | goto port_unlock; | |
5733 | } | |
5734 | ||
5735 | if (port->state != SNK_READY) { | |
5736 | ret = -EAGAIN; | |
5737 | goto port_unlock; | |
5738 | } | |
5739 | ||
e3a07202 | 5740 | if (req_op_curr > port->pps_data.max_curr) { |
2eadc33f AT |
5741 | ret = -EINVAL; |
5742 | goto port_unlock; | |
5743 | } | |
5744 | ||
e3a07202 | 5745 | target_mw = (req_op_curr * port->supply_voltage) / 1000; |
2eadc33f AT |
5746 | if (target_mw < port->operating_snk_mw) { |
5747 | ret = -EINVAL; | |
5748 | goto port_unlock; | |
5749 | } | |
5750 | ||
0908c5ac KT |
5751 | port->upcoming_state = SNK_NEGOTIATE_PPS_CAPABILITIES; |
5752 | ret = tcpm_ams_start(port, POWER_NEGOTIATION); | |
5753 | if (ret == -EAGAIN) { | |
5754 | port->upcoming_state = INVALID_STATE; | |
5755 | goto port_unlock; | |
5756 | } | |
5757 | ||
554fab6d | 5758 | /* Round down operating current to align with PPS valid steps */ |
e3a07202 | 5759 | req_op_curr = req_op_curr - (req_op_curr % RDO_PROG_CURR_MA_STEP); |
554fab6d | 5760 | |
2eadc33f | 5761 | reinit_completion(&port->pps_complete); |
e3a07202 | 5762 | port->pps_data.req_op_curr = req_op_curr; |
2eadc33f AT |
5763 | port->pps_status = 0; |
5764 | port->pps_pending = true; | |
2eadc33f AT |
5765 | mutex_unlock(&port->lock); |
5766 | ||
5767 | if (!wait_for_completion_timeout(&port->pps_complete, | |
5768 | msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT))) | |
5769 | ret = -ETIMEDOUT; | |
5770 | else | |
5771 | ret = port->pps_status; | |
5772 | ||
5773 | goto swap_unlock; | |
5774 | ||
5775 | port_unlock: | |
5776 | mutex_unlock(&port->lock); | |
5777 | swap_unlock: | |
5778 | mutex_unlock(&port->swap_lock); | |
5779 | ||
5780 | return ret; | |
5781 | } | |
5782 | ||
e3a07202 | 5783 | static int tcpm_pps_set_out_volt(struct tcpm_port *port, u16 req_out_volt) |
2eadc33f AT |
5784 | { |
5785 | unsigned int target_mw; | |
5786 | int ret; | |
5787 | ||
5788 | mutex_lock(&port->swap_lock); | |
5789 | mutex_lock(&port->lock); | |
5790 | ||
5791 | if (!port->pps_data.active) { | |
5792 | ret = -EOPNOTSUPP; | |
5793 | goto port_unlock; | |
5794 | } | |
5795 | ||
5796 | if (port->state != SNK_READY) { | |
5797 | ret = -EAGAIN; | |
5798 | goto port_unlock; | |
5799 | } | |
5800 | ||
e3a07202 BJS |
5801 | if (req_out_volt < port->pps_data.min_volt || |
5802 | req_out_volt > port->pps_data.max_volt) { | |
2eadc33f AT |
5803 | ret = -EINVAL; |
5804 | goto port_unlock; | |
5805 | } | |
5806 | ||
e3a07202 | 5807 | target_mw = (port->current_limit * req_out_volt) / 1000; |
2eadc33f AT |
5808 | if (target_mw < port->operating_snk_mw) { |
5809 | ret = -EINVAL; | |
5810 | goto port_unlock; | |
5811 | } | |
5812 | ||
0908c5ac KT |
5813 | port->upcoming_state = SNK_NEGOTIATE_PPS_CAPABILITIES; |
5814 | ret = tcpm_ams_start(port, POWER_NEGOTIATION); | |
5815 | if (ret == -EAGAIN) { | |
5816 | port->upcoming_state = INVALID_STATE; | |
5817 | goto port_unlock; | |
5818 | } | |
5819 | ||
554fab6d | 5820 | /* Round down output voltage to align with PPS valid steps */ |
e3a07202 | 5821 | req_out_volt = req_out_volt - (req_out_volt % RDO_PROG_VOLT_MV_STEP); |
554fab6d | 5822 | |
2eadc33f | 5823 | reinit_completion(&port->pps_complete); |
e3a07202 | 5824 | port->pps_data.req_out_volt = req_out_volt; |
2eadc33f AT |
5825 | port->pps_status = 0; |
5826 | port->pps_pending = true; | |
2eadc33f AT |
5827 | mutex_unlock(&port->lock); |
5828 | ||
5829 | if (!wait_for_completion_timeout(&port->pps_complete, | |
5830 | msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT))) | |
5831 | ret = -ETIMEDOUT; | |
5832 | else | |
5833 | ret = port->pps_status; | |
5834 | ||
5835 | goto swap_unlock; | |
5836 | ||
5837 | port_unlock: | |
5838 | mutex_unlock(&port->lock); | |
5839 | swap_unlock: | |
5840 | mutex_unlock(&port->swap_lock); | |
5841 | ||
5842 | return ret; | |
5843 | } | |
5844 | ||
f2a8aa05 | 5845 | static int tcpm_pps_activate(struct tcpm_port *port, bool activate) |
2eadc33f AT |
5846 | { |
5847 | int ret = 0; | |
5848 | ||
5849 | mutex_lock(&port->swap_lock); | |
5850 | mutex_lock(&port->lock); | |
5851 | ||
5852 | if (!port->pps_data.supported) { | |
5853 | ret = -EOPNOTSUPP; | |
5854 | goto port_unlock; | |
5855 | } | |
5856 | ||
5857 | /* Trying to deactivate PPS when already deactivated so just bail */ | |
5858 | if (!port->pps_data.active && !activate) | |
5859 | goto port_unlock; | |
5860 | ||
5861 | if (port->state != SNK_READY) { | |
5862 | ret = -EAGAIN; | |
5863 | goto port_unlock; | |
5864 | } | |
5865 | ||
0908c5ac KT |
5866 | if (activate) |
5867 | port->upcoming_state = SNK_NEGOTIATE_PPS_CAPABILITIES; | |
5868 | else | |
5869 | port->upcoming_state = SNK_NEGOTIATE_CAPABILITIES; | |
5870 | ret = tcpm_ams_start(port, POWER_NEGOTIATION); | |
5871 | if (ret == -EAGAIN) { | |
5872 | port->upcoming_state = INVALID_STATE; | |
5873 | goto port_unlock; | |
5874 | } | |
5875 | ||
2eadc33f AT |
5876 | reinit_completion(&port->pps_complete); |
5877 | port->pps_status = 0; | |
5878 | port->pps_pending = true; | |
5879 | ||
5880 | /* Trigger PPS request or move back to standard PDO contract */ | |
5881 | if (activate) { | |
e3a07202 BJS |
5882 | port->pps_data.req_out_volt = port->supply_voltage; |
5883 | port->pps_data.req_op_curr = port->current_limit; | |
2eadc33f AT |
5884 | } |
5885 | mutex_unlock(&port->lock); | |
5886 | ||
5887 | if (!wait_for_completion_timeout(&port->pps_complete, | |
5888 | msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT))) | |
5889 | ret = -ETIMEDOUT; | |
5890 | else | |
5891 | ret = port->pps_status; | |
5892 | ||
5893 | goto swap_unlock; | |
5894 | ||
5895 | port_unlock: | |
5896 | mutex_unlock(&port->lock); | |
5897 | swap_unlock: | |
5898 | mutex_unlock(&port->swap_lock); | |
5899 | ||
5900 | return ret; | |
5901 | } | |
5902 | ||
f0690a25 GR |
5903 | static void tcpm_init(struct tcpm_port *port) |
5904 | { | |
5905 | enum typec_cc_status cc1, cc2; | |
5906 | ||
5907 | port->tcpc->init(port->tcpc); | |
5908 | ||
5909 | tcpm_reset_port(port); | |
5910 | ||
5911 | /* | |
5912 | * XXX | |
5913 | * Should possibly wait for VBUS to settle if it was enabled locally | |
5914 | * since tcpm_reset_port() will disable VBUS. | |
5915 | */ | |
5916 | port->vbus_present = port->tcpc->get_vbus(port->tcpc); | |
5917 | if (port->vbus_present) | |
5918 | port->vbus_never_low = true; | |
5919 | ||
3db4c21c BJS |
5920 | /* |
5921 | * 1. When vbus_present is true, voltage on VBUS is already at VSAFE5V. | |
5922 | * So implicitly vbus_vsafe0v = false. | |
5923 | * | |
5924 | * 2. When vbus_present is false and TCPC does NOT support querying | |
5925 | * vsafe0v status, then, it's best to assume vbus is at VSAFE0V i.e. | |
5926 | * vbus_vsafe0v is true. | |
5927 | * | |
5928 | * 3. When vbus_present is false and TCPC does support querying vsafe0v, | |
5929 | * then, query tcpc for vsafe0v status. | |
5930 | */ | |
5931 | if (port->vbus_present) | |
5932 | port->vbus_vsafe0v = false; | |
5933 | else if (!port->tcpc->is_vbus_vsafe0v) | |
5934 | port->vbus_vsafe0v = true; | |
5935 | else | |
5936 | port->vbus_vsafe0v = port->tcpc->is_vbus_vsafe0v(port->tcpc); | |
5937 | ||
f0690a25 GR |
5938 | tcpm_set_state(port, tcpm_default_state(port), 0); |
5939 | ||
5940 | if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0) | |
5941 | _tcpm_cc_change(port, cc1, cc2); | |
5942 | ||
5943 | /* | |
5944 | * Some adapters need a clean slate at startup, and won't recover | |
5945 | * otherwise. So do not try to be fancy and force a clean disconnect. | |
5946 | */ | |
b17dd571 | 5947 | tcpm_set_state(port, PORT_RESET, 0); |
f0690a25 GR |
5948 | } |
5949 | ||
00ec21e5 | 5950 | static int tcpm_port_type_set(struct typec_port *p, enum typec_port_type type) |
9b0ae699 | 5951 | { |
00ec21e5 | 5952 | struct tcpm_port *port = typec_get_drvdata(p); |
9b0ae699 BJS |
5953 | |
5954 | mutex_lock(&port->lock); | |
5955 | if (type == port->port_type) | |
5956 | goto port_unlock; | |
5957 | ||
5958 | port->port_type = type; | |
5959 | ||
5960 | if (!port->connected) { | |
5961 | tcpm_set_state(port, PORT_RESET, 0); | |
ceeb1625 | 5962 | } else if (type == TYPEC_PORT_SNK) { |
9b0ae699 BJS |
5963 | if (!(port->pwr_role == TYPEC_SINK && |
5964 | port->data_role == TYPEC_DEVICE)) | |
5965 | tcpm_set_state(port, PORT_RESET, 0); | |
ceeb1625 | 5966 | } else if (type == TYPEC_PORT_SRC) { |
9b0ae699 BJS |
5967 | if (!(port->pwr_role == TYPEC_SOURCE && |
5968 | port->data_role == TYPEC_HOST)) | |
5969 | tcpm_set_state(port, PORT_RESET, 0); | |
5970 | } | |
5971 | ||
5972 | port_unlock: | |
5973 | mutex_unlock(&port->lock); | |
5974 | return 0; | |
5975 | } | |
5976 | ||
00ec21e5 HK |
5977 | static const struct typec_operations tcpm_ops = { |
5978 | .try_role = tcpm_try_role, | |
5979 | .dr_set = tcpm_dr_set, | |
5980 | .pr_set = tcpm_pr_set, | |
5981 | .vconn_set = tcpm_vconn_set, | |
5982 | .port_type_set = tcpm_port_type_set | |
5983 | }; | |
5984 | ||
f0690a25 GR |
5985 | void tcpm_tcpc_reset(struct tcpm_port *port) |
5986 | { | |
5987 | mutex_lock(&port->lock); | |
5988 | /* XXX: Maintain PD connection if possible? */ | |
5989 | tcpm_init(port); | |
5990 | mutex_unlock(&port->lock); | |
5991 | } | |
5992 | EXPORT_SYMBOL_GPL(tcpm_tcpc_reset); | |
5993 | ||
8203d269 HK |
5994 | static void tcpm_port_unregister_pd(struct tcpm_port *port) |
5995 | { | |
5996 | usb_power_delivery_unregister_capabilities(port->port_sink_caps); | |
5997 | port->port_sink_caps = NULL; | |
5998 | usb_power_delivery_unregister_capabilities(port->port_source_caps); | |
5999 | port->port_source_caps = NULL; | |
6000 | usb_power_delivery_unregister(port->pd); | |
6001 | port->pd = NULL; | |
6002 | } | |
6003 | ||
6004 | static int tcpm_port_register_pd(struct tcpm_port *port) | |
6005 | { | |
6006 | struct usb_power_delivery_desc desc = { port->typec_caps.pd_revision }; | |
6007 | struct usb_power_delivery_capabilities_desc caps = { }; | |
6008 | struct usb_power_delivery_capabilities *cap; | |
6009 | int ret; | |
6010 | ||
6011 | if (!port->nr_src_pdo && !port->nr_snk_pdo) | |
6012 | return 0; | |
6013 | ||
6014 | port->pd = usb_power_delivery_register(port->dev, &desc); | |
6015 | if (IS_ERR(port->pd)) { | |
6016 | ret = PTR_ERR(port->pd); | |
6017 | goto err_unregister; | |
6018 | } | |
6019 | ||
6020 | if (port->nr_src_pdo) { | |
6021 | memcpy_and_pad(caps.pdo, sizeof(caps.pdo), port->src_pdo, | |
6022 | port->nr_src_pdo * sizeof(u32), 0); | |
6023 | caps.role = TYPEC_SOURCE; | |
6024 | ||
6025 | cap = usb_power_delivery_register_capabilities(port->pd, &caps); | |
6026 | if (IS_ERR(cap)) { | |
6027 | ret = PTR_ERR(cap); | |
6028 | goto err_unregister; | |
6029 | } | |
6030 | ||
6031 | port->port_source_caps = cap; | |
6032 | } | |
6033 | ||
6034 | if (port->nr_snk_pdo) { | |
6035 | memcpy_and_pad(caps.pdo, sizeof(caps.pdo), port->snk_pdo, | |
6036 | port->nr_snk_pdo * sizeof(u32), 0); | |
6037 | caps.role = TYPEC_SINK; | |
6038 | ||
6039 | cap = usb_power_delivery_register_capabilities(port->pd, &caps); | |
6040 | if (IS_ERR(cap)) { | |
6041 | ret = PTR_ERR(cap); | |
6042 | goto err_unregister; | |
6043 | } | |
6044 | ||
6045 | port->port_sink_caps = cap; | |
6046 | } | |
6047 | ||
6048 | return 0; | |
6049 | ||
6050 | err_unregister: | |
6051 | tcpm_port_unregister_pd(port); | |
6052 | ||
6053 | return ret; | |
6054 | } | |
6055 | ||
96232cbc LJ |
6056 | static int tcpm_fw_get_caps(struct tcpm_port *port, |
6057 | struct fwnode_handle *fwnode) | |
6058 | { | |
e9e6e164 | 6059 | const char *opmode_str; |
96232cbc | 6060 | int ret; |
8dc4bd07 | 6061 | u32 mw, frs_current; |
96232cbc LJ |
6062 | |
6063 | if (!fwnode) | |
6064 | return -EINVAL; | |
6065 | ||
28ec344b SK |
6066 | /* |
6067 | * This fwnode has a "compatible" property, but is never populated as a | |
6068 | * struct device. Instead we simply parse it to read the properties. | |
6069 | * This it breaks fw_devlink=on. To maintain backward compatibility | |
6070 | * with existing DT files, we work around this by deleting any | |
6071 | * fwnode_links to/from this fwnode. | |
6072 | */ | |
6073 | fw_devlink_purge_absent_suppliers(fwnode); | |
6074 | ||
2e7dfb0e | 6075 | ret = typec_get_fw_cap(&port->typec_caps, fwnode); |
96232cbc LJ |
6076 | if (ret < 0) |
6077 | return ret; | |
6078 | ||
96232cbc | 6079 | port->port_type = port->typec_caps.type; |
e9e6e164 | 6080 | port->pd_supported = !fwnode_property_read_bool(fwnode, "pd-disable"); |
96232cbc | 6081 | |
1373fefc | 6082 | port->slow_charger_loop = fwnode_property_read_bool(fwnode, "slow-charger-loop"); |
96232cbc LJ |
6083 | if (port->port_type == TYPEC_PORT_SNK) |
6084 | goto sink; | |
6085 | ||
e9e6e164 KT |
6086 | /* Get Source PDOs for the PD port or Source Rp value for the non-PD port */ |
6087 | if (port->pd_supported) { | |
6088 | ret = fwnode_property_count_u32(fwnode, "source-pdos"); | |
6089 | if (ret == 0) | |
6090 | return -EINVAL; | |
6091 | else if (ret < 0) | |
6092 | return ret; | |
96232cbc | 6093 | |
e9e6e164 KT |
6094 | port->nr_src_pdo = min(ret, PDO_MAX_OBJECTS); |
6095 | ret = fwnode_property_read_u32_array(fwnode, "source-pdos", | |
6096 | port->src_pdo, port->nr_src_pdo); | |
6097 | if (ret) | |
6098 | return ret; | |
6099 | ret = tcpm_validate_caps(port, port->src_pdo, port->nr_src_pdo); | |
6100 | if (ret) | |
6101 | return ret; | |
6102 | } else { | |
6103 | ret = fwnode_property_read_string(fwnode, "typec-power-opmode", &opmode_str); | |
6104 | if (ret) | |
6105 | return ret; | |
6106 | ret = typec_find_pwr_opmode(opmode_str); | |
6107 | if (ret < 0) | |
6108 | return ret; | |
6109 | port->src_rp = tcpm_pwr_opmode_to_rp(ret); | |
6110 | } | |
96232cbc LJ |
6111 | |
6112 | if (port->port_type == TYPEC_PORT_SRC) | |
6113 | return 0; | |
6114 | ||
96232cbc | 6115 | sink: |
e9e6e164 KT |
6116 | port->self_powered = fwnode_property_read_bool(fwnode, "self-powered"); |
6117 | ||
6118 | if (!port->pd_supported) | |
6119 | return 0; | |
6120 | ||
96232cbc | 6121 | /* Get sink pdos */ |
bc751492 | 6122 | ret = fwnode_property_count_u32(fwnode, "sink-pdos"); |
96232cbc LJ |
6123 | if (ret <= 0) |
6124 | return -EINVAL; | |
6125 | ||
6126 | port->nr_snk_pdo = min(ret, PDO_MAX_OBJECTS); | |
6127 | ret = fwnode_property_read_u32_array(fwnode, "sink-pdos", | |
6128 | port->snk_pdo, port->nr_snk_pdo); | |
6129 | if ((ret < 0) || tcpm_validate_caps(port, port->snk_pdo, | |
6130 | port->nr_snk_pdo)) | |
6131 | return -EINVAL; | |
6132 | ||
6133 | if (fwnode_property_read_u32(fwnode, "op-sink-microwatt", &mw) < 0) | |
6134 | return -EINVAL; | |
6135 | port->operating_snk_mw = mw / 1000; | |
6136 | ||
e9e6e164 | 6137 | /* FRS can only be supported by DRP ports */ |
8dc4bd07 | 6138 | if (port->port_type == TYPEC_PORT_DRP) { |
8115240c BJS |
6139 | ret = fwnode_property_read_u32(fwnode, "new-source-frs-typec-current", |
6140 | &frs_current); | |
8dc4bd07 | 6141 | if (ret >= 0 && frs_current <= FRS_5V_3A) |
8115240c | 6142 | port->new_source_frs_current = frs_current; |
8dc4bd07 BJS |
6143 | } |
6144 | ||
f5d1d63e KT |
6145 | /* sink-vdos is optional */ |
6146 | ret = fwnode_property_count_u32(fwnode, "sink-vdos"); | |
6147 | if (ret < 0) | |
6148 | ret = 0; | |
6149 | ||
6150 | port->nr_snk_vdo = min(ret, VDO_MAX_OBJECTS); | |
6151 | if (port->nr_snk_vdo) { | |
6152 | ret = fwnode_property_read_u32_array(fwnode, "sink-vdos", | |
6153 | port->snk_vdo, | |
6154 | port->nr_snk_vdo); | |
6155 | if (ret < 0) | |
6156 | return ret; | |
6157 | } | |
6158 | ||
7ac50510 KT |
6159 | /* If sink-vdos is found, sink-vdos-v1 is expected for backward compatibility. */ |
6160 | if (port->nr_snk_vdo) { | |
6161 | ret = fwnode_property_count_u32(fwnode, "sink-vdos-v1"); | |
6162 | if (ret < 0) | |
6163 | return ret; | |
6164 | else if (ret == 0) | |
6165 | return -ENODATA; | |
6166 | ||
6167 | port->nr_snk_vdo_v1 = min(ret, VDO_MAX_OBJECTS); | |
6168 | ret = fwnode_property_read_u32_array(fwnode, "sink-vdos-v1", | |
6169 | port->snk_vdo_v1, | |
6170 | port->nr_snk_vdo_v1); | |
6171 | if (ret < 0) | |
6172 | return ret; | |
6173 | } | |
f5d1d63e | 6174 | |
96232cbc LJ |
6175 | return 0; |
6176 | } | |
6177 | ||
f2a8aa05 AT |
6178 | /* Power Supply access to expose source power information */ |
6179 | enum tcpm_psy_online_states { | |
6180 | TCPM_PSY_OFFLINE = 0, | |
6181 | TCPM_PSY_FIXED_ONLINE, | |
6182 | TCPM_PSY_PROG_ONLINE, | |
6183 | }; | |
6184 | ||
6185 | static enum power_supply_property tcpm_psy_props[] = { | |
6186 | POWER_SUPPLY_PROP_USB_TYPE, | |
6187 | POWER_SUPPLY_PROP_ONLINE, | |
6188 | POWER_SUPPLY_PROP_VOLTAGE_MIN, | |
6189 | POWER_SUPPLY_PROP_VOLTAGE_MAX, | |
6190 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | |
6191 | POWER_SUPPLY_PROP_CURRENT_MAX, | |
6192 | POWER_SUPPLY_PROP_CURRENT_NOW, | |
6193 | }; | |
6194 | ||
6195 | static int tcpm_psy_get_online(struct tcpm_port *port, | |
6196 | union power_supply_propval *val) | |
6197 | { | |
6198 | if (port->vbus_charge) { | |
6199 | if (port->pps_data.active) | |
6200 | val->intval = TCPM_PSY_PROG_ONLINE; | |
6201 | else | |
6202 | val->intval = TCPM_PSY_FIXED_ONLINE; | |
6203 | } else { | |
6204 | val->intval = TCPM_PSY_OFFLINE; | |
6205 | } | |
6206 | ||
6207 | return 0; | |
6208 | } | |
6209 | ||
6210 | static int tcpm_psy_get_voltage_min(struct tcpm_port *port, | |
6211 | union power_supply_propval *val) | |
6212 | { | |
6213 | if (port->pps_data.active) | |
6214 | val->intval = port->pps_data.min_volt * 1000; | |
6215 | else | |
6216 | val->intval = port->supply_voltage * 1000; | |
6217 | ||
6218 | return 0; | |
6219 | } | |
6220 | ||
6221 | static int tcpm_psy_get_voltage_max(struct tcpm_port *port, | |
6222 | union power_supply_propval *val) | |
6223 | { | |
6224 | if (port->pps_data.active) | |
6225 | val->intval = port->pps_data.max_volt * 1000; | |
6226 | else | |
6227 | val->intval = port->supply_voltage * 1000; | |
6228 | ||
6229 | return 0; | |
6230 | } | |
6231 | ||
6232 | static int tcpm_psy_get_voltage_now(struct tcpm_port *port, | |
6233 | union power_supply_propval *val) | |
6234 | { | |
6235 | val->intval = port->supply_voltage * 1000; | |
6236 | ||
6237 | return 0; | |
6238 | } | |
6239 | ||
6240 | static int tcpm_psy_get_current_max(struct tcpm_port *port, | |
6241 | union power_supply_propval *val) | |
6242 | { | |
6243 | if (port->pps_data.active) | |
6244 | val->intval = port->pps_data.max_curr * 1000; | |
6245 | else | |
6246 | val->intval = port->current_limit * 1000; | |
6247 | ||
6248 | return 0; | |
6249 | } | |
6250 | ||
6251 | static int tcpm_psy_get_current_now(struct tcpm_port *port, | |
6252 | union power_supply_propval *val) | |
6253 | { | |
6254 | val->intval = port->current_limit * 1000; | |
6255 | ||
6256 | return 0; | |
6257 | } | |
6258 | ||
6259 | static int tcpm_psy_get_prop(struct power_supply *psy, | |
6260 | enum power_supply_property psp, | |
6261 | union power_supply_propval *val) | |
6262 | { | |
6263 | struct tcpm_port *port = power_supply_get_drvdata(psy); | |
6264 | int ret = 0; | |
6265 | ||
6266 | switch (psp) { | |
6267 | case POWER_SUPPLY_PROP_USB_TYPE: | |
6268 | val->intval = port->usb_type; | |
6269 | break; | |
6270 | case POWER_SUPPLY_PROP_ONLINE: | |
6271 | ret = tcpm_psy_get_online(port, val); | |
6272 | break; | |
6273 | case POWER_SUPPLY_PROP_VOLTAGE_MIN: | |
6274 | ret = tcpm_psy_get_voltage_min(port, val); | |
6275 | break; | |
6276 | case POWER_SUPPLY_PROP_VOLTAGE_MAX: | |
6277 | ret = tcpm_psy_get_voltage_max(port, val); | |
6278 | break; | |
6279 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: | |
6280 | ret = tcpm_psy_get_voltage_now(port, val); | |
6281 | break; | |
6282 | case POWER_SUPPLY_PROP_CURRENT_MAX: | |
6283 | ret = tcpm_psy_get_current_max(port, val); | |
6284 | break; | |
6285 | case POWER_SUPPLY_PROP_CURRENT_NOW: | |
6286 | ret = tcpm_psy_get_current_now(port, val); | |
6287 | break; | |
6288 | default: | |
6289 | ret = -EINVAL; | |
6290 | break; | |
6291 | } | |
6292 | ||
6293 | return ret; | |
6294 | } | |
6295 | ||
6296 | static int tcpm_psy_set_online(struct tcpm_port *port, | |
6297 | const union power_supply_propval *val) | |
6298 | { | |
6299 | int ret; | |
6300 | ||
6301 | switch (val->intval) { | |
6302 | case TCPM_PSY_FIXED_ONLINE: | |
6303 | ret = tcpm_pps_activate(port, false); | |
6304 | break; | |
6305 | case TCPM_PSY_PROG_ONLINE: | |
6306 | ret = tcpm_pps_activate(port, true); | |
6307 | break; | |
6308 | default: | |
6309 | ret = -EINVAL; | |
6310 | break; | |
6311 | } | |
6312 | ||
6313 | return ret; | |
6314 | } | |
6315 | ||
6316 | static int tcpm_psy_set_prop(struct power_supply *psy, | |
6317 | enum power_supply_property psp, | |
6318 | const union power_supply_propval *val) | |
6319 | { | |
6320 | struct tcpm_port *port = power_supply_get_drvdata(psy); | |
6321 | int ret; | |
6322 | ||
f2d38edc BJS |
6323 | /* |
6324 | * All the properties below are related to USB PD. The check needs to be | |
6325 | * property specific when a non-pd related property is added. | |
6326 | */ | |
6327 | if (!port->pd_supported) | |
6328 | return -EOPNOTSUPP; | |
6329 | ||
f2a8aa05 AT |
6330 | switch (psp) { |
6331 | case POWER_SUPPLY_PROP_ONLINE: | |
6332 | ret = tcpm_psy_set_online(port, val); | |
6333 | break; | |
6334 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: | |
6335 | if (val->intval < port->pps_data.min_volt * 1000 || | |
6336 | val->intval > port->pps_data.max_volt * 1000) | |
6337 | ret = -EINVAL; | |
6338 | else | |
6339 | ret = tcpm_pps_set_out_volt(port, val->intval / 1000); | |
6340 | break; | |
6341 | case POWER_SUPPLY_PROP_CURRENT_NOW: | |
6342 | if (val->intval > port->pps_data.max_curr * 1000) | |
6343 | ret = -EINVAL; | |
6344 | else | |
6345 | ret = tcpm_pps_set_op_curr(port, val->intval / 1000); | |
6346 | break; | |
6347 | default: | |
6348 | ret = -EINVAL; | |
6349 | break; | |
6350 | } | |
86629e09 | 6351 | power_supply_changed(port->psy); |
f2a8aa05 AT |
6352 | return ret; |
6353 | } | |
6354 | ||
6355 | static int tcpm_psy_prop_writeable(struct power_supply *psy, | |
6356 | enum power_supply_property psp) | |
6357 | { | |
6358 | switch (psp) { | |
6359 | case POWER_SUPPLY_PROP_ONLINE: | |
6360 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: | |
6361 | case POWER_SUPPLY_PROP_CURRENT_NOW: | |
6362 | return 1; | |
6363 | default: | |
6364 | return 0; | |
6365 | } | |
6366 | } | |
6367 | ||
6368 | static enum power_supply_usb_type tcpm_psy_usb_types[] = { | |
6369 | POWER_SUPPLY_USB_TYPE_C, | |
6370 | POWER_SUPPLY_USB_TYPE_PD, | |
6371 | POWER_SUPPLY_USB_TYPE_PD_PPS, | |
6372 | }; | |
6373 | ||
6374 | static const char *tcpm_psy_name_prefix = "tcpm-source-psy-"; | |
6375 | ||
6376 | static int devm_tcpm_psy_register(struct tcpm_port *port) | |
6377 | { | |
6378 | struct power_supply_config psy_cfg = {}; | |
6379 | const char *port_dev_name = dev_name(port->dev); | |
6380 | size_t psy_name_len = strlen(tcpm_psy_name_prefix) + | |
6381 | strlen(port_dev_name) + 1; | |
6382 | char *psy_name; | |
6383 | ||
6384 | psy_cfg.drv_data = port; | |
c97a8cc1 | 6385 | psy_cfg.fwnode = dev_fwnode(port->dev); |
f2a8aa05 AT |
6386 | psy_name = devm_kzalloc(port->dev, psy_name_len, GFP_KERNEL); |
6387 | if (!psy_name) | |
6388 | return -ENOMEM; | |
6389 | ||
6390 | snprintf(psy_name, psy_name_len, "%s%s", tcpm_psy_name_prefix, | |
6391 | port_dev_name); | |
6392 | port->psy_desc.name = psy_name; | |
b5206275 | 6393 | port->psy_desc.type = POWER_SUPPLY_TYPE_USB; |
f2a8aa05 AT |
6394 | port->psy_desc.usb_types = tcpm_psy_usb_types; |
6395 | port->psy_desc.num_usb_types = ARRAY_SIZE(tcpm_psy_usb_types); | |
b5206275 ZY |
6396 | port->psy_desc.properties = tcpm_psy_props; |
6397 | port->psy_desc.num_properties = ARRAY_SIZE(tcpm_psy_props); | |
6398 | port->psy_desc.get_property = tcpm_psy_get_prop; | |
6399 | port->psy_desc.set_property = tcpm_psy_set_prop; | |
6400 | port->psy_desc.property_is_writeable = tcpm_psy_prop_writeable; | |
f2a8aa05 AT |
6401 | |
6402 | port->usb_type = POWER_SUPPLY_USB_TYPE_C; | |
6403 | ||
6404 | port->psy = devm_power_supply_register(port->dev, &port->psy_desc, | |
6405 | &psy_cfg); | |
6406 | ||
6407 | return PTR_ERR_OR_ZERO(port->psy); | |
6408 | } | |
6409 | ||
3ed8e1c2 BJS |
6410 | static enum hrtimer_restart state_machine_timer_handler(struct hrtimer *timer) |
6411 | { | |
6412 | struct tcpm_port *port = container_of(timer, struct tcpm_port, state_machine_timer); | |
6413 | ||
ca4d8344 XY |
6414 | if (port->registered) |
6415 | kthread_queue_work(port->wq, &port->state_machine); | |
3ed8e1c2 BJS |
6416 | return HRTIMER_NORESTART; |
6417 | } | |
6418 | ||
6419 | static enum hrtimer_restart vdm_state_machine_timer_handler(struct hrtimer *timer) | |
6420 | { | |
6421 | struct tcpm_port *port = container_of(timer, struct tcpm_port, vdm_state_machine_timer); | |
6422 | ||
ca4d8344 XY |
6423 | if (port->registered) |
6424 | kthread_queue_work(port->wq, &port->vdm_state_machine); | |
3ed8e1c2 BJS |
6425 | return HRTIMER_NORESTART; |
6426 | } | |
6427 | ||
8dc4bd07 BJS |
6428 | static enum hrtimer_restart enable_frs_timer_handler(struct hrtimer *timer) |
6429 | { | |
6430 | struct tcpm_port *port = container_of(timer, struct tcpm_port, enable_frs_timer); | |
6431 | ||
ca4d8344 XY |
6432 | if (port->registered) |
6433 | kthread_queue_work(port->wq, &port->enable_frs); | |
8dc4bd07 BJS |
6434 | return HRTIMER_NORESTART; |
6435 | } | |
6436 | ||
c34e85fa KT |
6437 | static enum hrtimer_restart send_discover_timer_handler(struct hrtimer *timer) |
6438 | { | |
6439 | struct tcpm_port *port = container_of(timer, struct tcpm_port, send_discover_timer); | |
6440 | ||
ca4d8344 XY |
6441 | if (port->registered) |
6442 | kthread_queue_work(port->wq, &port->send_discover_work); | |
c34e85fa KT |
6443 | return HRTIMER_NORESTART; |
6444 | } | |
6445 | ||
f0690a25 GR |
6446 | struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc) |
6447 | { | |
6448 | struct tcpm_port *port; | |
a079973f | 6449 | int err; |
f0690a25 | 6450 | |
96232cbc | 6451 | if (!dev || !tcpc || |
f0690a25 GR |
6452 | !tcpc->get_vbus || !tcpc->set_cc || !tcpc->get_cc || |
6453 | !tcpc->set_polarity || !tcpc->set_vconn || !tcpc->set_vbus || | |
6454 | !tcpc->set_pd_rx || !tcpc->set_roles || !tcpc->pd_transmit) | |
6455 | return ERR_PTR(-EINVAL); | |
6456 | ||
6457 | port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL); | |
6458 | if (!port) | |
6459 | return ERR_PTR(-ENOMEM); | |
6460 | ||
6461 | port->dev = dev; | |
6462 | port->tcpc = tcpc; | |
6463 | ||
6464 | mutex_init(&port->lock); | |
6465 | mutex_init(&port->swap_lock); | |
6466 | ||
3ed8e1c2 BJS |
6467 | port->wq = kthread_create_worker(0, dev_name(dev)); |
6468 | if (IS_ERR(port->wq)) | |
6469 | return ERR_CAST(port->wq); | |
6470 | sched_set_fifo(port->wq->task); | |
6471 | ||
6472 | kthread_init_work(&port->state_machine, tcpm_state_machine_work); | |
6473 | kthread_init_work(&port->vdm_state_machine, vdm_state_machine_work); | |
6474 | kthread_init_work(&port->event_work, tcpm_pd_event_handler); | |
8dc4bd07 | 6475 | kthread_init_work(&port->enable_frs, tcpm_enable_frs_work); |
c34e85fa | 6476 | kthread_init_work(&port->send_discover_work, tcpm_send_discover_work); |
3ed8e1c2 BJS |
6477 | hrtimer_init(&port->state_machine_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
6478 | port->state_machine_timer.function = state_machine_timer_handler; | |
6479 | hrtimer_init(&port->vdm_state_machine_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); | |
6480 | port->vdm_state_machine_timer.function = vdm_state_machine_timer_handler; | |
8dc4bd07 BJS |
6481 | hrtimer_init(&port->enable_frs_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
6482 | port->enable_frs_timer.function = enable_frs_timer_handler; | |
c34e85fa KT |
6483 | hrtimer_init(&port->send_discover_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
6484 | port->send_discover_timer.function = send_discover_timer_handler; | |
f0690a25 GR |
6485 | |
6486 | spin_lock_init(&port->pd_event_lock); | |
6487 | ||
6488 | init_completion(&port->tx_complete); | |
6489 | init_completion(&port->swap_complete); | |
2eadc33f | 6490 | init_completion(&port->pps_complete); |
5007e1b5 | 6491 | tcpm_debugfs_init(port); |
f0690a25 | 6492 | |
96232cbc | 6493 | err = tcpm_fw_get_caps(port, tcpc->fwnode); |
96232cbc | 6494 | if (err < 0) |
5007e1b5 | 6495 | goto out_destroy_wq; |
96232cbc | 6496 | |
a079973f | 6497 | port->try_role = port->typec_caps.prefer_role; |
f0690a25 | 6498 | |
5e85a04c | 6499 | port->typec_caps.fwnode = tcpc->fwnode; |
f0690a25 | 6500 | port->typec_caps.revision = 0x0120; /* Type-C spec release 1.2 */ |
2eadc33f | 6501 | port->typec_caps.pd_revision = 0x0300; /* USB-PD spec release 3.0 */ |
5e1d4c49 | 6502 | port->typec_caps.svdm_version = SVDM_VER_2_0; |
00ec21e5 HK |
6503 | port->typec_caps.driver_data = port; |
6504 | port->typec_caps.ops = &tcpm_ops; | |
75f81a7f | 6505 | port->typec_caps.orientation_aware = 1; |
f0690a25 GR |
6506 | |
6507 | port->partner_desc.identity = &port->partner_ident; | |
96232cbc | 6508 | port->port_type = port->typec_caps.type; |
f0690a25 | 6509 | |
2000016c HG |
6510 | port->role_sw = usb_role_switch_get(port->dev); |
6511 | if (IS_ERR(port->role_sw)) { | |
6512 | err = PTR_ERR(port->role_sw); | |
6513 | goto out_destroy_wq; | |
6514 | } | |
6515 | ||
f2a8aa05 AT |
6516 | err = devm_tcpm_psy_register(port); |
6517 | if (err) | |
9997ab35 | 6518 | goto out_role_sw_put; |
86629e09 | 6519 | power_supply_changed(port->psy); |
f2a8aa05 | 6520 | |
8203d269 HK |
6521 | err = tcpm_port_register_pd(port); |
6522 | if (err) | |
6523 | goto out_role_sw_put; | |
6524 | ||
6525 | port->typec_caps.pd = port->pd; | |
6526 | ||
f0690a25 | 6527 | port->typec_port = typec_register_port(port->dev, &port->typec_caps); |
cf6e06cd HK |
6528 | if (IS_ERR(port->typec_port)) { |
6529 | err = PTR_ERR(port->typec_port); | |
8203d269 | 6530 | goto out_unregister_pd; |
f0690a25 GR |
6531 | } |
6532 | ||
55d8b347 HG |
6533 | typec_port_register_altmodes(port->typec_port, |
6534 | &tcpm_altmode_ops, port, | |
6535 | port->port_altmode, ALTMODE_DISCOVERY_MAX); | |
ca4d8344 | 6536 | port->registered = true; |
55d8b347 | 6537 | |
f0690a25 GR |
6538 | mutex_lock(&port->lock); |
6539 | tcpm_init(port); | |
6540 | mutex_unlock(&port->lock); | |
6541 | ||
6542 | tcpm_log(port, "%s: registered", dev_name(dev)); | |
6543 | return port; | |
6544 | ||
8203d269 HK |
6545 | out_unregister_pd: |
6546 | tcpm_port_unregister_pd(port); | |
9997ab35 | 6547 | out_role_sw_put: |
2000016c | 6548 | usb_role_switch_put(port->role_sw); |
9997ab35 JL |
6549 | out_destroy_wq: |
6550 | tcpm_debugfs_exit(port); | |
3ed8e1c2 | 6551 | kthread_destroy_worker(port->wq); |
f0690a25 GR |
6552 | return ERR_PTR(err); |
6553 | } | |
6554 | EXPORT_SYMBOL_GPL(tcpm_register_port); | |
6555 | ||
6556 | void tcpm_unregister_port(struct tcpm_port *port) | |
6557 | { | |
6558 | int i; | |
6559 | ||
ca4d8344 XY |
6560 | port->registered = false; |
6561 | kthread_destroy_worker(port->wq); | |
6562 | ||
024236ab | 6563 | hrtimer_cancel(&port->send_discover_timer); |
7ade4805 | 6564 | hrtimer_cancel(&port->enable_frs_timer); |
3a13ff7e LJ |
6565 | hrtimer_cancel(&port->vdm_state_machine_timer); |
6566 | hrtimer_cancel(&port->state_machine_timer); | |
6567 | ||
a6d5d230 | 6568 | tcpm_reset_port(port); |
8203d269 HK |
6569 | |
6570 | tcpm_port_unregister_pd(port); | |
6571 | ||
f0690a25 GR |
6572 | for (i = 0; i < ARRAY_SIZE(port->port_altmode); i++) |
6573 | typec_unregister_altmode(port->port_altmode[i]); | |
6574 | typec_unregister_port(port->typec_port); | |
5671a460 | 6575 | usb_role_switch_put(port->role_sw); |
f0690a25 | 6576 | tcpm_debugfs_exit(port); |
f0690a25 GR |
6577 | } |
6578 | EXPORT_SYMBOL_GPL(tcpm_unregister_port); | |
6579 | ||
6580 | MODULE_AUTHOR("Guenter Roeck <groeck@chromium.org>"); | |
6581 | MODULE_DESCRIPTION("USB Type-C Port Manager"); | |
6582 | MODULE_LICENSE("GPL"); |