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