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