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