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