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