This source file includes following definitions.
- FOREACH_STATE
- tcpm_default_state
- typec_cap_to_tcpm
- tcpm_port_is_disconnected
- tcpm_log_full
- __printf
- __printf
- __printf
- tcpm_log_source_caps
- tcpm_debug_show
- tcpm_debugfs_init
- tcpm_debugfs_exit
- __printf
- __printf
- tcpm_log_source_caps
- tcpm_debugfs_init
- tcpm_debugfs_exit
- tcpm_pd_transmit
- tcpm_pd_transmit_complete
- tcpm_mux_set
- tcpm_set_polarity
- tcpm_set_vconn
- tcpm_get_current_limit
- tcpm_set_current_limit
- tcpm_rp_cc
- tcpm_set_attached_state
- tcpm_set_roles
- tcpm_set_pwr_role
- tcpm_pd_send_source_caps
- tcpm_pd_send_sink_caps
- tcpm_set_state
- tcpm_set_state_cond
- tcpm_queue_message
- tcpm_queue_vdm
- svdm_consume_identity
- svdm_consume_svids
- svdm_consume_modes
- tcpm_register_partner_altmodes
- tcpm_pd_svdm
- tcpm_handle_vdm_request
- tcpm_send_vdm
- vdm_ready_timeout
- vdm_run_state_machine
- vdm_state_machine_work
- tcpm_caps_err
- tcpm_validate_caps
- tcpm_altmode_enter
- tcpm_altmode_exit
- tcpm_altmode_vdm
- ready_state
- tcpm_handle_alert
- tcpm_pd_data_request
- tcpm_pps_complete
- tcpm_pd_ctrl_request
- tcpm_pd_ext_msg_request
- tcpm_pd_rx_handler
- tcpm_pd_receive
- tcpm_pd_send_control
- tcpm_send_queued_message
- tcpm_pd_check_request
- tcpm_pd_select_pdo
- tcpm_pd_select_pps_apdo
- tcpm_pd_build_request
- tcpm_pd_send_request
- tcpm_pd_build_pps_request
- tcpm_pd_send_pps_request
- tcpm_set_vbus
- tcpm_set_charge
- tcpm_start_toggling
- tcpm_set_cc
- tcpm_init_vbus
- tcpm_init_vconn
- tcpm_typec_connect
- tcpm_src_attach
- tcpm_typec_disconnect
- tcpm_unregister_altmodes
- tcpm_reset_port
- tcpm_detach
- tcpm_src_detach
- tcpm_snk_attach
- tcpm_snk_detach
- tcpm_acc_attach
- tcpm_acc_detach
- hard_reset_state
- unattached_state
- tcpm_check_send_discover
- tcpm_swap_complete
- tcpm_get_pwr_opmode
- run_state_machine
- tcpm_state_machine_work
- _tcpm_cc_change
- _tcpm_pd_vbus_on
- _tcpm_pd_vbus_off
- _tcpm_pd_hard_reset
- tcpm_pd_event_handler
- tcpm_cc_change
- tcpm_vbus_change
- tcpm_pd_hard_reset
- tcpm_dr_set
- tcpm_pr_set
- tcpm_vconn_set
- tcpm_try_role
- tcpm_pps_set_op_curr
- tcpm_pps_set_out_volt
- tcpm_pps_activate
- tcpm_init
- tcpm_port_type_set
- tcpm_tcpc_reset
- tcpm_copy_pdos
- tcpm_copy_vdos
- tcpm_fw_get_caps
- tcpm_psy_get_online
- tcpm_psy_get_voltage_min
- tcpm_psy_get_voltage_max
- tcpm_psy_get_voltage_now
- tcpm_psy_get_current_max
- tcpm_psy_get_current_now
- tcpm_psy_get_prop
- tcpm_psy_set_online
- tcpm_psy_set_prop
- tcpm_psy_prop_writeable
- devm_tcpm_psy_register
- tcpm_copy_caps
- tcpm_register_port
- tcpm_unregister_port
1
2
3
4
5
6
7
8 #include <linux/completion.h>
9 #include <linux/debugfs.h>
10 #include <linux/device.h>
11 #include <linux/jiffies.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/power_supply.h>
16 #include <linux/proc_fs.h>
17 #include <linux/property.h>
18 #include <linux/sched/clock.h>
19 #include <linux/seq_file.h>
20 #include <linux/slab.h>
21 #include <linux/spinlock.h>
22 #include <linux/usb.h>
23 #include <linux/usb/pd.h>
24 #include <linux/usb/pd_ado.h>
25 #include <linux/usb/pd_bdo.h>
26 #include <linux/usb/pd_ext_sdb.h>
27 #include <linux/usb/pd_vdo.h>
28 #include <linux/usb/role.h>
29 #include <linux/usb/tcpm.h>
30 #include <linux/usb/typec_altmode.h>
31 #include <linux/workqueue.h>
32
33 #define FOREACH_STATE(S) \
34 S(INVALID_STATE), \
35 S(TOGGLING), \
36 S(SRC_UNATTACHED), \
37 S(SRC_ATTACH_WAIT), \
38 S(SRC_ATTACHED), \
39 S(SRC_STARTUP), \
40 S(SRC_SEND_CAPABILITIES), \
41 S(SRC_SEND_CAPABILITIES_TIMEOUT), \
42 S(SRC_NEGOTIATE_CAPABILITIES), \
43 S(SRC_TRANSITION_SUPPLY), \
44 S(SRC_READY), \
45 S(SRC_WAIT_NEW_CAPABILITIES), \
46 \
47 S(SNK_UNATTACHED), \
48 S(SNK_ATTACH_WAIT), \
49 S(SNK_DEBOUNCED), \
50 S(SNK_ATTACHED), \
51 S(SNK_STARTUP), \
52 S(SNK_DISCOVERY), \
53 S(SNK_DISCOVERY_DEBOUNCE), \
54 S(SNK_DISCOVERY_DEBOUNCE_DONE), \
55 S(SNK_WAIT_CAPABILITIES), \
56 S(SNK_NEGOTIATE_CAPABILITIES), \
57 S(SNK_NEGOTIATE_PPS_CAPABILITIES), \
58 S(SNK_TRANSITION_SINK), \
59 S(SNK_TRANSITION_SINK_VBUS), \
60 S(SNK_READY), \
61 \
62 S(ACC_UNATTACHED), \
63 S(DEBUG_ACC_ATTACHED), \
64 S(AUDIO_ACC_ATTACHED), \
65 S(AUDIO_ACC_DEBOUNCE), \
66 \
67 S(HARD_RESET_SEND), \
68 S(HARD_RESET_START), \
69 S(SRC_HARD_RESET_VBUS_OFF), \
70 S(SRC_HARD_RESET_VBUS_ON), \
71 S(SNK_HARD_RESET_SINK_OFF), \
72 S(SNK_HARD_RESET_WAIT_VBUS), \
73 S(SNK_HARD_RESET_SINK_ON), \
74 \
75 S(SOFT_RESET), \
76 S(SOFT_RESET_SEND), \
77 \
78 S(DR_SWAP_ACCEPT), \
79 S(DR_SWAP_SEND), \
80 S(DR_SWAP_SEND_TIMEOUT), \
81 S(DR_SWAP_CANCEL), \
82 S(DR_SWAP_CHANGE_DR), \
83 \
84 S(PR_SWAP_ACCEPT), \
85 S(PR_SWAP_SEND), \
86 S(PR_SWAP_SEND_TIMEOUT), \
87 S(PR_SWAP_CANCEL), \
88 S(PR_SWAP_START), \
89 S(PR_SWAP_SRC_SNK_TRANSITION_OFF), \
90 S(PR_SWAP_SRC_SNK_SOURCE_OFF), \
91 S(PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED), \
92 S(PR_SWAP_SRC_SNK_SINK_ON), \
93 S(PR_SWAP_SNK_SRC_SINK_OFF), \
94 S(PR_SWAP_SNK_SRC_SOURCE_ON), \
95 S(PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP), \
96 \
97 S(VCONN_SWAP_ACCEPT), \
98 S(VCONN_SWAP_SEND), \
99 S(VCONN_SWAP_SEND_TIMEOUT), \
100 S(VCONN_SWAP_CANCEL), \
101 S(VCONN_SWAP_START), \
102 S(VCONN_SWAP_WAIT_FOR_VCONN), \
103 S(VCONN_SWAP_TURN_ON_VCONN), \
104 S(VCONN_SWAP_TURN_OFF_VCONN), \
105 \
106 S(SNK_TRY), \
107 S(SNK_TRY_WAIT), \
108 S(SNK_TRY_WAIT_DEBOUNCE), \
109 S(SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS), \
110 S(SRC_TRYWAIT), \
111 S(SRC_TRYWAIT_DEBOUNCE), \
112 S(SRC_TRYWAIT_UNATTACHED), \
113 \
114 S(SRC_TRY), \
115 S(SRC_TRY_WAIT), \
116 S(SRC_TRY_DEBOUNCE), \
117 S(SNK_TRYWAIT), \
118 S(SNK_TRYWAIT_DEBOUNCE), \
119 S(SNK_TRYWAIT_VBUS), \
120 S(BIST_RX), \
121 \
122 S(GET_STATUS_SEND), \
123 S(GET_STATUS_SEND_TIMEOUT), \
124 S(GET_PPS_STATUS_SEND), \
125 S(GET_PPS_STATUS_SEND_TIMEOUT), \
126 \
127 S(ERROR_RECOVERY), \
128 S(PORT_RESET), \
129 S(PORT_RESET_WAIT_OFF)
130
131 #define GENERATE_ENUM(e) e
132 #define GENERATE_STRING(s) #s
133
134 enum tcpm_state {
135 FOREACH_STATE(GENERATE_ENUM)
136 };
137
138 static const char * const tcpm_states[] = {
139 FOREACH_STATE(GENERATE_STRING)
140 };
141
142 enum vdm_states {
143 VDM_STATE_ERR_BUSY = -3,
144 VDM_STATE_ERR_SEND = -2,
145 VDM_STATE_ERR_TMOUT = -1,
146 VDM_STATE_DONE = 0,
147
148 VDM_STATE_READY = 1,
149 VDM_STATE_BUSY = 2,
150 VDM_STATE_WAIT_RSP_BUSY = 3,
151 };
152
153 enum pd_msg_request {
154 PD_MSG_NONE = 0,
155 PD_MSG_CTRL_REJECT,
156 PD_MSG_CTRL_WAIT,
157 PD_MSG_CTRL_NOT_SUPP,
158 PD_MSG_DATA_SINK_CAP,
159 PD_MSG_DATA_SOURCE_CAP,
160 };
161
162
163
164 #define TCPM_CC_EVENT BIT(0)
165 #define TCPM_VBUS_EVENT BIT(1)
166 #define TCPM_RESET_EVENT BIT(2)
167
168 #define LOG_BUFFER_ENTRIES 1024
169 #define LOG_BUFFER_ENTRY_SIZE 128
170
171
172
173 #define SVID_DISCOVERY_MAX 16
174 #define ALTMODE_DISCOVERY_MAX (SVID_DISCOVERY_MAX * MODE_DISCOVERY_MAX)
175
176 struct pd_mode_data {
177 int svid_index;
178 int nsvids;
179 u16 svids[SVID_DISCOVERY_MAX];
180 int altmodes;
181 struct typec_altmode_desc altmode_desc[ALTMODE_DISCOVERY_MAX];
182 };
183
184 struct pd_pps_data {
185 u32 min_volt;
186 u32 max_volt;
187 u32 max_curr;
188 u32 out_volt;
189 u32 op_curr;
190 bool supported;
191 bool active;
192 };
193
194 struct tcpm_port {
195 struct device *dev;
196
197 struct mutex lock;
198 struct workqueue_struct *wq;
199
200 struct typec_capability typec_caps;
201 struct typec_port *typec_port;
202
203 struct tcpc_dev *tcpc;
204 struct usb_role_switch *role_sw;
205
206 enum typec_role vconn_role;
207 enum typec_role pwr_role;
208 enum typec_data_role data_role;
209 enum typec_pwr_opmode pwr_opmode;
210
211 struct usb_pd_identity partner_ident;
212 struct typec_partner_desc partner_desc;
213 struct typec_partner *partner;
214
215 enum typec_cc_status cc_req;
216
217 enum typec_cc_status cc1;
218 enum typec_cc_status cc2;
219 enum typec_cc_polarity polarity;
220
221 bool attached;
222 bool connected;
223 enum typec_port_type port_type;
224 bool vbus_present;
225 bool vbus_never_low;
226 bool vbus_source;
227 bool vbus_charge;
228
229 bool send_discover;
230 bool op_vsafe5v;
231
232 int try_role;
233 int try_snk_count;
234 int try_src_count;
235
236 enum pd_msg_request queued_message;
237
238 enum tcpm_state enter_state;
239 enum tcpm_state prev_state;
240 enum tcpm_state state;
241 enum tcpm_state delayed_state;
242 unsigned long delayed_runtime;
243 unsigned long delay_ms;
244
245 spinlock_t pd_event_lock;
246 u32 pd_events;
247
248 struct work_struct event_work;
249 struct delayed_work state_machine;
250 struct delayed_work vdm_state_machine;
251 bool state_machine_running;
252
253 struct completion tx_complete;
254 enum tcpm_transmit_status tx_status;
255
256 struct mutex swap_lock;
257 bool swap_pending;
258 bool non_pd_role_swap;
259 struct completion swap_complete;
260 int swap_status;
261
262 unsigned int negotiated_rev;
263 unsigned int message_id;
264 unsigned int caps_count;
265 unsigned int hard_reset_count;
266 bool pd_capable;
267 bool explicit_contract;
268 unsigned int rx_msgid;
269
270
271 u32 sink_request;
272 u32 source_caps[PDO_MAX_OBJECTS];
273 unsigned int nr_source_caps;
274 u32 sink_caps[PDO_MAX_OBJECTS];
275 unsigned int nr_sink_caps;
276
277
278 u32 src_pdo[PDO_MAX_OBJECTS];
279 unsigned int nr_src_pdo;
280 u32 snk_pdo[PDO_MAX_OBJECTS];
281 unsigned int nr_snk_pdo;
282 u32 snk_vdo[VDO_MAX_OBJECTS];
283 unsigned int nr_snk_vdo;
284
285 unsigned int operating_snk_mw;
286 bool update_sink_caps;
287
288
289 u32 current_limit;
290 u32 supply_voltage;
291
292
293 struct power_supply *psy;
294 struct power_supply_desc psy_desc;
295 enum power_supply_usb_type usb_type;
296
297 u32 bist_request;
298
299
300 enum vdm_states vdm_state;
301 u32 vdm_retries;
302
303 u32 vdo_data[VDO_MAX_SIZE];
304 u8 vdo_count;
305
306 u32 vdo_retry;
307
308
309 struct pd_pps_data pps_data;
310 struct completion pps_complete;
311 bool pps_pending;
312 int pps_status;
313
314
315 struct pd_mode_data mode_data;
316 struct typec_altmode *partner_altmode[ALTMODE_DISCOVERY_MAX];
317 struct typec_altmode *port_altmode[ALTMODE_DISCOVERY_MAX];
318
319
320 unsigned long max_wait;
321
322
323 bool self_powered;
324
325 #ifdef CONFIG_DEBUG_FS
326 struct dentry *dentry;
327 struct mutex logbuffer_lock;
328 int logbuffer_head;
329 int logbuffer_tail;
330 u8 *logbuffer[LOG_BUFFER_ENTRIES];
331 #endif
332 };
333
334 struct pd_rx_event {
335 struct work_struct work;
336 struct tcpm_port *port;
337 struct pd_message msg;
338 };
339
340 #define tcpm_cc_is_sink(cc) \
341 ((cc) == TYPEC_CC_RP_DEF || (cc) == TYPEC_CC_RP_1_5 || \
342 (cc) == TYPEC_CC_RP_3_0)
343
344 #define tcpm_port_is_sink(port) \
345 ((tcpm_cc_is_sink((port)->cc1) && !tcpm_cc_is_sink((port)->cc2)) || \
346 (tcpm_cc_is_sink((port)->cc2) && !tcpm_cc_is_sink((port)->cc1)))
347
348 #define tcpm_cc_is_source(cc) ((cc) == TYPEC_CC_RD)
349 #define tcpm_cc_is_audio(cc) ((cc) == TYPEC_CC_RA)
350 #define tcpm_cc_is_open(cc) ((cc) == TYPEC_CC_OPEN)
351
352 #define tcpm_port_is_source(port) \
353 ((tcpm_cc_is_source((port)->cc1) && \
354 !tcpm_cc_is_source((port)->cc2)) || \
355 (tcpm_cc_is_source((port)->cc2) && \
356 !tcpm_cc_is_source((port)->cc1)))
357
358 #define tcpm_port_is_debug(port) \
359 (tcpm_cc_is_source((port)->cc1) && tcpm_cc_is_source((port)->cc2))
360
361 #define tcpm_port_is_audio(port) \
362 (tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_audio((port)->cc2))
363
364 #define tcpm_port_is_audio_detached(port) \
365 ((tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_open((port)->cc2)) || \
366 (tcpm_cc_is_audio((port)->cc2) && tcpm_cc_is_open((port)->cc1)))
367
368 #define tcpm_try_snk(port) \
369 ((port)->try_snk_count == 0 && (port)->try_role == TYPEC_SINK && \
370 (port)->port_type == TYPEC_PORT_DRP)
371
372 #define tcpm_try_src(port) \
373 ((port)->try_src_count == 0 && (port)->try_role == TYPEC_SOURCE && \
374 (port)->port_type == TYPEC_PORT_DRP)
375
376 static enum tcpm_state tcpm_default_state(struct tcpm_port *port)
377 {
378 if (port->port_type == TYPEC_PORT_DRP) {
379 if (port->try_role == TYPEC_SINK)
380 return SNK_UNATTACHED;
381 else if (port->try_role == TYPEC_SOURCE)
382 return SRC_UNATTACHED;
383 else if (port->tcpc->config &&
384 port->tcpc->config->default_role == TYPEC_SINK)
385 return SNK_UNATTACHED;
386
387 } else if (port->port_type == TYPEC_PORT_SNK) {
388 return SNK_UNATTACHED;
389 }
390 return SRC_UNATTACHED;
391 }
392
393 static inline
394 struct tcpm_port *typec_cap_to_tcpm(const struct typec_capability *cap)
395 {
396 return container_of(cap, struct tcpm_port, typec_caps);
397 }
398
399 static bool tcpm_port_is_disconnected(struct tcpm_port *port)
400 {
401 return (!port->attached && port->cc1 == TYPEC_CC_OPEN &&
402 port->cc2 == TYPEC_CC_OPEN) ||
403 (port->attached && ((port->polarity == TYPEC_POLARITY_CC1 &&
404 port->cc1 == TYPEC_CC_OPEN) ||
405 (port->polarity == TYPEC_POLARITY_CC2 &&
406 port->cc2 == TYPEC_CC_OPEN)));
407 }
408
409
410
411
412
413 #ifdef CONFIG_DEBUG_FS
414
415 static bool tcpm_log_full(struct tcpm_port *port)
416 {
417 return port->logbuffer_tail ==
418 (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
419 }
420
421 __printf(2, 0)
422 static void _tcpm_log(struct tcpm_port *port, const char *fmt, va_list args)
423 {
424 char tmpbuffer[LOG_BUFFER_ENTRY_SIZE];
425 u64 ts_nsec = local_clock();
426 unsigned long rem_nsec;
427
428 mutex_lock(&port->logbuffer_lock);
429 if (!port->logbuffer[port->logbuffer_head]) {
430 port->logbuffer[port->logbuffer_head] =
431 kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL);
432 if (!port->logbuffer[port->logbuffer_head]) {
433 mutex_unlock(&port->logbuffer_lock);
434 return;
435 }
436 }
437
438 vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args);
439
440 if (tcpm_log_full(port)) {
441 port->logbuffer_head = max(port->logbuffer_head - 1, 0);
442 strcpy(tmpbuffer, "overflow");
443 }
444
445 if (port->logbuffer_head < 0 ||
446 port->logbuffer_head >= LOG_BUFFER_ENTRIES) {
447 dev_warn(port->dev,
448 "Bad log buffer index %d\n", port->logbuffer_head);
449 goto abort;
450 }
451
452 if (!port->logbuffer[port->logbuffer_head]) {
453 dev_warn(port->dev,
454 "Log buffer index %d is NULL\n", port->logbuffer_head);
455 goto abort;
456 }
457
458 rem_nsec = do_div(ts_nsec, 1000000000);
459 scnprintf(port->logbuffer[port->logbuffer_head],
460 LOG_BUFFER_ENTRY_SIZE, "[%5lu.%06lu] %s",
461 (unsigned long)ts_nsec, rem_nsec / 1000,
462 tmpbuffer);
463 port->logbuffer_head = (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
464
465 abort:
466 mutex_unlock(&port->logbuffer_lock);
467 }
468
469 __printf(2, 3)
470 static void tcpm_log(struct tcpm_port *port, const char *fmt, ...)
471 {
472 va_list args;
473
474
475 if (tcpm_port_is_disconnected(port) &&
476 (port->state == SRC_UNATTACHED || port->state == SNK_UNATTACHED ||
477 port->state == TOGGLING))
478 return;
479
480 va_start(args, fmt);
481 _tcpm_log(port, fmt, args);
482 va_end(args);
483 }
484
485 __printf(2, 3)
486 static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...)
487 {
488 va_list args;
489
490 va_start(args, fmt);
491 _tcpm_log(port, fmt, args);
492 va_end(args);
493 }
494
495 static void tcpm_log_source_caps(struct tcpm_port *port)
496 {
497 int i;
498
499 for (i = 0; i < port->nr_source_caps; i++) {
500 u32 pdo = port->source_caps[i];
501 enum pd_pdo_type type = pdo_type(pdo);
502 char msg[64];
503
504 switch (type) {
505 case PDO_TYPE_FIXED:
506 scnprintf(msg, sizeof(msg),
507 "%u mV, %u mA [%s%s%s%s%s%s]",
508 pdo_fixed_voltage(pdo),
509 pdo_max_current(pdo),
510 (pdo & PDO_FIXED_DUAL_ROLE) ?
511 "R" : "",
512 (pdo & PDO_FIXED_SUSPEND) ?
513 "S" : "",
514 (pdo & PDO_FIXED_HIGHER_CAP) ?
515 "H" : "",
516 (pdo & PDO_FIXED_USB_COMM) ?
517 "U" : "",
518 (pdo & PDO_FIXED_DATA_SWAP) ?
519 "D" : "",
520 (pdo & PDO_FIXED_EXTPOWER) ?
521 "E" : "");
522 break;
523 case PDO_TYPE_VAR:
524 scnprintf(msg, sizeof(msg),
525 "%u-%u mV, %u mA",
526 pdo_min_voltage(pdo),
527 pdo_max_voltage(pdo),
528 pdo_max_current(pdo));
529 break;
530 case PDO_TYPE_BATT:
531 scnprintf(msg, sizeof(msg),
532 "%u-%u mV, %u mW",
533 pdo_min_voltage(pdo),
534 pdo_max_voltage(pdo),
535 pdo_max_power(pdo));
536 break;
537 case PDO_TYPE_APDO:
538 if (pdo_apdo_type(pdo) == APDO_TYPE_PPS)
539 scnprintf(msg, sizeof(msg),
540 "%u-%u mV, %u mA",
541 pdo_pps_apdo_min_voltage(pdo),
542 pdo_pps_apdo_max_voltage(pdo),
543 pdo_pps_apdo_max_current(pdo));
544 else
545 strcpy(msg, "undefined APDO");
546 break;
547 default:
548 strcpy(msg, "undefined");
549 break;
550 }
551 tcpm_log(port, " PDO %d: type %d, %s",
552 i, type, msg);
553 }
554 }
555
556 static int tcpm_debug_show(struct seq_file *s, void *v)
557 {
558 struct tcpm_port *port = (struct tcpm_port *)s->private;
559 int tail;
560
561 mutex_lock(&port->logbuffer_lock);
562 tail = port->logbuffer_tail;
563 while (tail != port->logbuffer_head) {
564 seq_printf(s, "%s\n", port->logbuffer[tail]);
565 tail = (tail + 1) % LOG_BUFFER_ENTRIES;
566 }
567 if (!seq_has_overflowed(s))
568 port->logbuffer_tail = tail;
569 mutex_unlock(&port->logbuffer_lock);
570
571 return 0;
572 }
573 DEFINE_SHOW_ATTRIBUTE(tcpm_debug);
574
575 static void tcpm_debugfs_init(struct tcpm_port *port)
576 {
577 char name[NAME_MAX];
578
579 mutex_init(&port->logbuffer_lock);
580 snprintf(name, NAME_MAX, "tcpm-%s", dev_name(port->dev));
581 port->dentry = debugfs_create_file(name, S_IFREG | 0444, usb_debug_root,
582 port, &tcpm_debug_fops);
583 }
584
585 static void tcpm_debugfs_exit(struct tcpm_port *port)
586 {
587 int i;
588
589 mutex_lock(&port->logbuffer_lock);
590 for (i = 0; i < LOG_BUFFER_ENTRIES; i++) {
591 kfree(port->logbuffer[i]);
592 port->logbuffer[i] = NULL;
593 }
594 mutex_unlock(&port->logbuffer_lock);
595
596 debugfs_remove(port->dentry);
597 }
598
599 #else
600
601 __printf(2, 3)
602 static void tcpm_log(const struct tcpm_port *port, const char *fmt, ...) { }
603 __printf(2, 3)
604 static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...) { }
605 static void tcpm_log_source_caps(struct tcpm_port *port) { }
606 static void tcpm_debugfs_init(const struct tcpm_port *port) { }
607 static void tcpm_debugfs_exit(const struct tcpm_port *port) { }
608
609 #endif
610
611 static int tcpm_pd_transmit(struct tcpm_port *port,
612 enum tcpm_transmit_type type,
613 const struct pd_message *msg)
614 {
615 unsigned long timeout;
616 int ret;
617
618 if (msg)
619 tcpm_log(port, "PD TX, header: %#x", le16_to_cpu(msg->header));
620 else
621 tcpm_log(port, "PD TX, type: %#x", type);
622
623 reinit_completion(&port->tx_complete);
624 ret = port->tcpc->pd_transmit(port->tcpc, type, msg);
625 if (ret < 0)
626 return ret;
627
628 mutex_unlock(&port->lock);
629 timeout = wait_for_completion_timeout(&port->tx_complete,
630 msecs_to_jiffies(PD_T_TCPC_TX_TIMEOUT));
631 mutex_lock(&port->lock);
632 if (!timeout)
633 return -ETIMEDOUT;
634
635 switch (port->tx_status) {
636 case TCPC_TX_SUCCESS:
637 port->message_id = (port->message_id + 1) & PD_HEADER_ID_MASK;
638 return 0;
639 case TCPC_TX_DISCARDED:
640 return -EAGAIN;
641 case TCPC_TX_FAILED:
642 default:
643 return -EIO;
644 }
645 }
646
647 void tcpm_pd_transmit_complete(struct tcpm_port *port,
648 enum tcpm_transmit_status status)
649 {
650 tcpm_log(port, "PD TX complete, status: %u", status);
651 port->tx_status = status;
652 complete(&port->tx_complete);
653 }
654 EXPORT_SYMBOL_GPL(tcpm_pd_transmit_complete);
655
656 static int tcpm_mux_set(struct tcpm_port *port, int state,
657 enum usb_role usb_role,
658 enum typec_orientation orientation)
659 {
660 int ret;
661
662 tcpm_log(port, "Requesting mux state %d, usb-role %d, orientation %d",
663 state, usb_role, orientation);
664
665 ret = typec_set_orientation(port->typec_port, orientation);
666 if (ret)
667 return ret;
668
669 if (port->role_sw) {
670 ret = usb_role_switch_set_role(port->role_sw, usb_role);
671 if (ret)
672 return ret;
673 }
674
675 return typec_set_mode(port->typec_port, state);
676 }
677
678 static int tcpm_set_polarity(struct tcpm_port *port,
679 enum typec_cc_polarity polarity)
680 {
681 int ret;
682
683 tcpm_log(port, "polarity %d", polarity);
684
685 ret = port->tcpc->set_polarity(port->tcpc, polarity);
686 if (ret < 0)
687 return ret;
688
689 port->polarity = polarity;
690
691 return 0;
692 }
693
694 static int tcpm_set_vconn(struct tcpm_port *port, bool enable)
695 {
696 int ret;
697
698 tcpm_log(port, "vconn:=%d", enable);
699
700 ret = port->tcpc->set_vconn(port->tcpc, enable);
701 if (!ret) {
702 port->vconn_role = enable ? TYPEC_SOURCE : TYPEC_SINK;
703 typec_set_vconn_role(port->typec_port, port->vconn_role);
704 }
705
706 return ret;
707 }
708
709 static u32 tcpm_get_current_limit(struct tcpm_port *port)
710 {
711 enum typec_cc_status cc;
712 u32 limit;
713
714 cc = port->polarity ? port->cc2 : port->cc1;
715 switch (cc) {
716 case TYPEC_CC_RP_1_5:
717 limit = 1500;
718 break;
719 case TYPEC_CC_RP_3_0:
720 limit = 3000;
721 break;
722 case TYPEC_CC_RP_DEF:
723 default:
724 if (port->tcpc->get_current_limit)
725 limit = port->tcpc->get_current_limit(port->tcpc);
726 else
727 limit = 0;
728 break;
729 }
730
731 return limit;
732 }
733
734 static int tcpm_set_current_limit(struct tcpm_port *port, u32 max_ma, u32 mv)
735 {
736 int ret = -EOPNOTSUPP;
737
738 tcpm_log(port, "Setting voltage/current limit %u mV %u mA", mv, max_ma);
739
740 port->supply_voltage = mv;
741 port->current_limit = max_ma;
742
743 if (port->tcpc->set_current_limit)
744 ret = port->tcpc->set_current_limit(port->tcpc, max_ma, mv);
745
746 return ret;
747 }
748
749
750
751
752
753
754 static enum typec_cc_status tcpm_rp_cc(struct tcpm_port *port)
755 {
756 const u32 *src_pdo = port->src_pdo;
757 int nr_pdo = port->nr_src_pdo;
758 int i;
759
760
761
762
763
764 for (i = 0; i < nr_pdo; i++) {
765 const u32 pdo = src_pdo[i];
766
767 if (pdo_type(pdo) == PDO_TYPE_FIXED &&
768 pdo_fixed_voltage(pdo) == 5000) {
769 unsigned int curr = pdo_max_current(pdo);
770
771 if (curr >= 3000)
772 return TYPEC_CC_RP_3_0;
773 else if (curr >= 1500)
774 return TYPEC_CC_RP_1_5;
775 return TYPEC_CC_RP_DEF;
776 }
777 }
778
779 return TYPEC_CC_RP_DEF;
780 }
781
782 static int tcpm_set_attached_state(struct tcpm_port *port, bool attached)
783 {
784 return port->tcpc->set_roles(port->tcpc, attached, port->pwr_role,
785 port->data_role);
786 }
787
788 static int tcpm_set_roles(struct tcpm_port *port, bool attached,
789 enum typec_role role, enum typec_data_role data)
790 {
791 enum typec_orientation orientation;
792 enum usb_role usb_role;
793 int ret;
794
795 if (port->polarity == TYPEC_POLARITY_CC1)
796 orientation = TYPEC_ORIENTATION_NORMAL;
797 else
798 orientation = TYPEC_ORIENTATION_REVERSE;
799
800 if (data == TYPEC_HOST)
801 usb_role = USB_ROLE_HOST;
802 else
803 usb_role = USB_ROLE_DEVICE;
804
805 ret = tcpm_mux_set(port, TYPEC_STATE_USB, usb_role, orientation);
806 if (ret < 0)
807 return ret;
808
809 ret = port->tcpc->set_roles(port->tcpc, attached, role, data);
810 if (ret < 0)
811 return ret;
812
813 port->pwr_role = role;
814 port->data_role = data;
815 typec_set_data_role(port->typec_port, data);
816 typec_set_pwr_role(port->typec_port, role);
817
818 return 0;
819 }
820
821 static int tcpm_set_pwr_role(struct tcpm_port *port, enum typec_role role)
822 {
823 int ret;
824
825 ret = port->tcpc->set_roles(port->tcpc, true, role,
826 port->data_role);
827 if (ret < 0)
828 return ret;
829
830 port->pwr_role = role;
831 typec_set_pwr_role(port->typec_port, role);
832
833 return 0;
834 }
835
836 static int tcpm_pd_send_source_caps(struct tcpm_port *port)
837 {
838 struct pd_message msg;
839 int i;
840
841 memset(&msg, 0, sizeof(msg));
842 if (!port->nr_src_pdo) {
843
844 msg.header = PD_HEADER_LE(PD_CTRL_REJECT,
845 port->pwr_role,
846 port->data_role,
847 port->negotiated_rev,
848 port->message_id, 0);
849 } else {
850 msg.header = PD_HEADER_LE(PD_DATA_SOURCE_CAP,
851 port->pwr_role,
852 port->data_role,
853 port->negotiated_rev,
854 port->message_id,
855 port->nr_src_pdo);
856 }
857 for (i = 0; i < port->nr_src_pdo; i++)
858 msg.payload[i] = cpu_to_le32(port->src_pdo[i]);
859
860 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
861 }
862
863 static int tcpm_pd_send_sink_caps(struct tcpm_port *port)
864 {
865 struct pd_message msg;
866 int i;
867
868 memset(&msg, 0, sizeof(msg));
869 if (!port->nr_snk_pdo) {
870
871 msg.header = PD_HEADER_LE(PD_CTRL_REJECT,
872 port->pwr_role,
873 port->data_role,
874 port->negotiated_rev,
875 port->message_id, 0);
876 } else {
877 msg.header = PD_HEADER_LE(PD_DATA_SINK_CAP,
878 port->pwr_role,
879 port->data_role,
880 port->negotiated_rev,
881 port->message_id,
882 port->nr_snk_pdo);
883 }
884 for (i = 0; i < port->nr_snk_pdo; i++)
885 msg.payload[i] = cpu_to_le32(port->snk_pdo[i]);
886
887 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
888 }
889
890 static void tcpm_set_state(struct tcpm_port *port, enum tcpm_state state,
891 unsigned int delay_ms)
892 {
893 if (delay_ms) {
894 tcpm_log(port, "pending state change %s -> %s @ %u ms",
895 tcpm_states[port->state], tcpm_states[state],
896 delay_ms);
897 port->delayed_state = state;
898 mod_delayed_work(port->wq, &port->state_machine,
899 msecs_to_jiffies(delay_ms));
900 port->delayed_runtime = jiffies + msecs_to_jiffies(delay_ms);
901 port->delay_ms = delay_ms;
902 } else {
903 tcpm_log(port, "state change %s -> %s",
904 tcpm_states[port->state], tcpm_states[state]);
905 port->delayed_state = INVALID_STATE;
906 port->prev_state = port->state;
907 port->state = state;
908
909
910
911
912
913
914 if (!port->state_machine_running)
915 mod_delayed_work(port->wq, &port->state_machine, 0);
916 }
917 }
918
919 static void tcpm_set_state_cond(struct tcpm_port *port, enum tcpm_state state,
920 unsigned int delay_ms)
921 {
922 if (port->enter_state == port->state)
923 tcpm_set_state(port, state, delay_ms);
924 else
925 tcpm_log(port,
926 "skipped %sstate change %s -> %s [%u ms], context state %s",
927 delay_ms ? "delayed " : "",
928 tcpm_states[port->state], tcpm_states[state],
929 delay_ms, tcpm_states[port->enter_state]);
930 }
931
932 static void tcpm_queue_message(struct tcpm_port *port,
933 enum pd_msg_request message)
934 {
935 port->queued_message = message;
936 mod_delayed_work(port->wq, &port->state_machine, 0);
937 }
938
939
940
941
942 static void tcpm_queue_vdm(struct tcpm_port *port, const u32 header,
943 const u32 *data, int cnt)
944 {
945 port->vdo_count = cnt + 1;
946 port->vdo_data[0] = header;
947 memcpy(&port->vdo_data[1], data, sizeof(u32) * cnt);
948
949 port->vdm_retries = 0;
950 port->vdm_state = VDM_STATE_READY;
951 }
952
953 static void svdm_consume_identity(struct tcpm_port *port, const __le32 *payload,
954 int cnt)
955 {
956 u32 vdo = le32_to_cpu(payload[VDO_INDEX_IDH]);
957 u32 product = le32_to_cpu(payload[VDO_INDEX_PRODUCT]);
958
959 memset(&port->mode_data, 0, sizeof(port->mode_data));
960
961 port->partner_ident.id_header = vdo;
962 port->partner_ident.cert_stat = le32_to_cpu(payload[VDO_INDEX_CSTAT]);
963 port->partner_ident.product = product;
964
965 typec_partner_set_identity(port->partner);
966
967 tcpm_log(port, "Identity: %04x:%04x.%04x",
968 PD_IDH_VID(vdo),
969 PD_PRODUCT_PID(product), product & 0xffff);
970 }
971
972 static bool svdm_consume_svids(struct tcpm_port *port, const __le32 *payload,
973 int cnt)
974 {
975 struct pd_mode_data *pmdata = &port->mode_data;
976 int i;
977
978 for (i = 1; i < cnt; i++) {
979 u32 p = le32_to_cpu(payload[i]);
980 u16 svid;
981
982 svid = (p >> 16) & 0xffff;
983 if (!svid)
984 return false;
985
986 if (pmdata->nsvids >= SVID_DISCOVERY_MAX)
987 goto abort;
988
989 pmdata->svids[pmdata->nsvids++] = svid;
990 tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid);
991
992 svid = p & 0xffff;
993 if (!svid)
994 return false;
995
996 if (pmdata->nsvids >= SVID_DISCOVERY_MAX)
997 goto abort;
998
999 pmdata->svids[pmdata->nsvids++] = svid;
1000 tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid);
1001 }
1002 return true;
1003 abort:
1004 tcpm_log(port, "SVID_DISCOVERY_MAX(%d) too low!", SVID_DISCOVERY_MAX);
1005 return false;
1006 }
1007
1008 static void svdm_consume_modes(struct tcpm_port *port, const __le32 *payload,
1009 int cnt)
1010 {
1011 struct pd_mode_data *pmdata = &port->mode_data;
1012 struct typec_altmode_desc *paltmode;
1013 int i;
1014
1015 if (pmdata->altmodes >= ARRAY_SIZE(port->partner_altmode)) {
1016
1017 return;
1018 }
1019
1020 for (i = 1; i < cnt; i++) {
1021 paltmode = &pmdata->altmode_desc[pmdata->altmodes];
1022 memset(paltmode, 0, sizeof(*paltmode));
1023
1024 paltmode->svid = pmdata->svids[pmdata->svid_index];
1025 paltmode->mode = i;
1026 paltmode->vdo = le32_to_cpu(payload[i]);
1027
1028 tcpm_log(port, " Alternate mode %d: SVID 0x%04x, VDO %d: 0x%08x",
1029 pmdata->altmodes, paltmode->svid,
1030 paltmode->mode, paltmode->vdo);
1031
1032 pmdata->altmodes++;
1033 }
1034 }
1035
1036 static void tcpm_register_partner_altmodes(struct tcpm_port *port)
1037 {
1038 struct pd_mode_data *modep = &port->mode_data;
1039 struct typec_altmode *altmode;
1040 int i;
1041
1042 for (i = 0; i < modep->altmodes; i++) {
1043 altmode = typec_partner_register_altmode(port->partner,
1044 &modep->altmode_desc[i]);
1045 if (!altmode)
1046 tcpm_log(port, "Failed to register partner SVID 0x%04x",
1047 modep->altmode_desc[i].svid);
1048 port->partner_altmode[i] = altmode;
1049 }
1050 }
1051
1052 #define supports_modal(port) PD_IDH_MODAL_SUPP((port)->partner_ident.id_header)
1053
1054 static int tcpm_pd_svdm(struct tcpm_port *port, const __le32 *payload, int cnt,
1055 u32 *response)
1056 {
1057 struct typec_altmode *adev;
1058 struct typec_altmode *pdev;
1059 struct pd_mode_data *modep;
1060 u32 p[PD_MAX_PAYLOAD];
1061 int rlen = 0;
1062 int cmd_type;
1063 int cmd;
1064 int i;
1065
1066 for (i = 0; i < cnt; i++)
1067 p[i] = le32_to_cpu(payload[i]);
1068
1069 cmd_type = PD_VDO_CMDT(p[0]);
1070 cmd = PD_VDO_CMD(p[0]);
1071
1072 tcpm_log(port, "Rx VDM cmd 0x%x type %d cmd %d len %d",
1073 p[0], cmd_type, cmd, cnt);
1074
1075 modep = &port->mode_data;
1076
1077 adev = typec_match_altmode(port->port_altmode, ALTMODE_DISCOVERY_MAX,
1078 PD_VDO_VID(p[0]), PD_VDO_OPOS(p[0]));
1079
1080 pdev = typec_match_altmode(port->partner_altmode, ALTMODE_DISCOVERY_MAX,
1081 PD_VDO_VID(p[0]), PD_VDO_OPOS(p[0]));
1082
1083 switch (cmd_type) {
1084 case CMDT_INIT:
1085 switch (cmd) {
1086 case CMD_DISCOVER_IDENT:
1087
1088 if (port->data_role == TYPEC_DEVICE &&
1089 port->nr_snk_vdo) {
1090 for (i = 0; i < port->nr_snk_vdo; i++)
1091 response[i + 1] = port->snk_vdo[i];
1092 rlen = port->nr_snk_vdo + 1;
1093 }
1094 break;
1095 case CMD_DISCOVER_SVID:
1096 break;
1097 case CMD_DISCOVER_MODES:
1098 break;
1099 case CMD_ENTER_MODE:
1100 break;
1101 case CMD_EXIT_MODE:
1102 break;
1103 case CMD_ATTENTION:
1104
1105 if (adev)
1106 typec_altmode_attention(adev, p[1]);
1107 return 0;
1108 default:
1109 break;
1110 }
1111 if (rlen >= 1) {
1112 response[0] = p[0] | VDO_CMDT(CMDT_RSP_ACK);
1113 } else if (rlen == 0) {
1114 response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK);
1115 rlen = 1;
1116 } else {
1117 response[0] = p[0] | VDO_CMDT(CMDT_RSP_BUSY);
1118 rlen = 1;
1119 }
1120 break;
1121 case CMDT_RSP_ACK:
1122
1123 if (IS_ERR_OR_NULL(port->partner))
1124 break;
1125
1126 switch (cmd) {
1127 case CMD_DISCOVER_IDENT:
1128
1129 svdm_consume_identity(port, payload, cnt);
1130 response[0] = VDO(USB_SID_PD, 1, CMD_DISCOVER_SVID);
1131 rlen = 1;
1132 break;
1133 case CMD_DISCOVER_SVID:
1134
1135 if (svdm_consume_svids(port, payload, cnt)) {
1136 response[0] = VDO(USB_SID_PD, 1,
1137 CMD_DISCOVER_SVID);
1138 rlen = 1;
1139 } else if (modep->nsvids && supports_modal(port)) {
1140 response[0] = VDO(modep->svids[0], 1,
1141 CMD_DISCOVER_MODES);
1142 rlen = 1;
1143 }
1144 break;
1145 case CMD_DISCOVER_MODES:
1146
1147 svdm_consume_modes(port, payload, cnt);
1148 modep->svid_index++;
1149 if (modep->svid_index < modep->nsvids) {
1150 u16 svid = modep->svids[modep->svid_index];
1151 response[0] = VDO(svid, 1, CMD_DISCOVER_MODES);
1152 rlen = 1;
1153 } else {
1154 tcpm_register_partner_altmodes(port);
1155 }
1156 break;
1157 case CMD_ENTER_MODE:
1158 if (adev && pdev) {
1159 typec_altmode_update_active(pdev, true);
1160
1161 if (typec_altmode_vdm(adev, p[0], &p[1], cnt)) {
1162 response[0] = VDO(adev->svid, 1,
1163 CMD_EXIT_MODE);
1164 response[0] |= VDO_OPOS(adev->mode);
1165 return 1;
1166 }
1167 }
1168 return 0;
1169 case CMD_EXIT_MODE:
1170 if (adev && pdev) {
1171 typec_altmode_update_active(pdev, false);
1172
1173
1174 WARN_ON(typec_altmode_notify(adev,
1175 TYPEC_STATE_USB,
1176 NULL));
1177 }
1178 break;
1179 default:
1180 break;
1181 }
1182 break;
1183 case CMDT_RSP_NAK:
1184 switch (cmd) {
1185 case CMD_ENTER_MODE:
1186
1187 if (adev)
1188 WARN_ON(typec_altmode_notify(adev,
1189 TYPEC_STATE_USB,
1190 NULL));
1191 break;
1192 default:
1193 break;
1194 }
1195 break;
1196 default:
1197 break;
1198 }
1199
1200
1201 if (adev)
1202 typec_altmode_vdm(adev, p[0], &p[1], cnt);
1203
1204 return rlen;
1205 }
1206
1207 static void tcpm_handle_vdm_request(struct tcpm_port *port,
1208 const __le32 *payload, int cnt)
1209 {
1210 int rlen = 0;
1211 u32 response[8] = { };
1212 u32 p0 = le32_to_cpu(payload[0]);
1213
1214 if (port->vdm_state == VDM_STATE_BUSY) {
1215
1216 if (PD_VDO_CMDT(p0) == CMDT_RSP_BUSY) {
1217 port->vdm_state = VDM_STATE_WAIT_RSP_BUSY;
1218 port->vdo_retry = (p0 & ~VDO_CMDT_MASK) |
1219 CMDT_INIT;
1220 mod_delayed_work(port->wq, &port->vdm_state_machine,
1221 msecs_to_jiffies(PD_T_VDM_BUSY));
1222 return;
1223 }
1224 port->vdm_state = VDM_STATE_DONE;
1225 }
1226
1227 if (PD_VDO_SVDM(p0))
1228 rlen = tcpm_pd_svdm(port, payload, cnt, response);
1229
1230 if (rlen > 0) {
1231 tcpm_queue_vdm(port, response[0], &response[1], rlen - 1);
1232 mod_delayed_work(port->wq, &port->vdm_state_machine, 0);
1233 }
1234 }
1235
1236 static void tcpm_send_vdm(struct tcpm_port *port, u32 vid, int cmd,
1237 const u32 *data, int count)
1238 {
1239 u32 header;
1240
1241 if (WARN_ON(count > VDO_MAX_SIZE - 1))
1242 count = VDO_MAX_SIZE - 1;
1243
1244
1245 header = VDO(vid, ((vid & USB_SID_PD) == USB_SID_PD) ?
1246 1 : (PD_VDO_CMD(cmd) <= CMD_ATTENTION), cmd);
1247 tcpm_queue_vdm(port, header, data, count);
1248
1249 mod_delayed_work(port->wq, &port->vdm_state_machine, 0);
1250 }
1251
1252 static unsigned int vdm_ready_timeout(u32 vdm_hdr)
1253 {
1254 unsigned int timeout;
1255 int cmd = PD_VDO_CMD(vdm_hdr);
1256
1257
1258 if (!PD_VDO_SVDM(vdm_hdr))
1259 return PD_T_VDM_UNSTRUCTURED;
1260
1261 switch (PD_VDO_CMDT(vdm_hdr)) {
1262 case CMDT_INIT:
1263 if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE)
1264 timeout = PD_T_VDM_WAIT_MODE_E;
1265 else
1266 timeout = PD_T_VDM_SNDR_RSP;
1267 break;
1268 default:
1269 if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE)
1270 timeout = PD_T_VDM_E_MODE;
1271 else
1272 timeout = PD_T_VDM_RCVR_RSP;
1273 break;
1274 }
1275 return timeout;
1276 }
1277
1278 static void vdm_run_state_machine(struct tcpm_port *port)
1279 {
1280 struct pd_message msg;
1281 int i, res;
1282
1283 switch (port->vdm_state) {
1284 case VDM_STATE_READY:
1285
1286 if (!port->attached) {
1287 port->vdm_state = VDM_STATE_ERR_BUSY;
1288 break;
1289 }
1290
1291
1292
1293
1294
1295 if (port->state != SRC_READY && port->state != SNK_READY)
1296 break;
1297
1298
1299 memset(&msg, 0, sizeof(msg));
1300 msg.header = PD_HEADER_LE(PD_DATA_VENDOR_DEF,
1301 port->pwr_role,
1302 port->data_role,
1303 port->negotiated_rev,
1304 port->message_id, port->vdo_count);
1305 for (i = 0; i < port->vdo_count; i++)
1306 msg.payload[i] = cpu_to_le32(port->vdo_data[i]);
1307 res = tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
1308 if (res < 0) {
1309 port->vdm_state = VDM_STATE_ERR_SEND;
1310 } else {
1311 unsigned long timeout;
1312
1313 port->vdm_retries = 0;
1314 port->vdm_state = VDM_STATE_BUSY;
1315 timeout = vdm_ready_timeout(port->vdo_data[0]);
1316 mod_delayed_work(port->wq, &port->vdm_state_machine,
1317 timeout);
1318 }
1319 break;
1320 case VDM_STATE_WAIT_RSP_BUSY:
1321 port->vdo_data[0] = port->vdo_retry;
1322 port->vdo_count = 1;
1323 port->vdm_state = VDM_STATE_READY;
1324 break;
1325 case VDM_STATE_BUSY:
1326 port->vdm_state = VDM_STATE_ERR_TMOUT;
1327 break;
1328 case VDM_STATE_ERR_SEND:
1329
1330
1331
1332
1333
1334
1335 if (port->vdm_retries < 3) {
1336 tcpm_log(port, "VDM Tx error, retry");
1337 port->vdm_retries++;
1338 port->vdm_state = VDM_STATE_READY;
1339 }
1340 break;
1341 default:
1342 break;
1343 }
1344 }
1345
1346 static void vdm_state_machine_work(struct work_struct *work)
1347 {
1348 struct tcpm_port *port = container_of(work, struct tcpm_port,
1349 vdm_state_machine.work);
1350 enum vdm_states prev_state;
1351
1352 mutex_lock(&port->lock);
1353
1354
1355
1356
1357
1358 do {
1359 prev_state = port->vdm_state;
1360 vdm_run_state_machine(port);
1361 } while (port->vdm_state != prev_state &&
1362 port->vdm_state != VDM_STATE_BUSY);
1363
1364 mutex_unlock(&port->lock);
1365 }
1366
1367 enum pdo_err {
1368 PDO_NO_ERR,
1369 PDO_ERR_NO_VSAFE5V,
1370 PDO_ERR_VSAFE5V_NOT_FIRST,
1371 PDO_ERR_PDO_TYPE_NOT_IN_ORDER,
1372 PDO_ERR_FIXED_NOT_SORTED,
1373 PDO_ERR_VARIABLE_BATT_NOT_SORTED,
1374 PDO_ERR_DUPE_PDO,
1375 PDO_ERR_PPS_APDO_NOT_SORTED,
1376 PDO_ERR_DUPE_PPS_APDO,
1377 };
1378
1379 static const char * const pdo_err_msg[] = {
1380 [PDO_ERR_NO_VSAFE5V] =
1381 " err: source/sink caps should atleast have vSafe5V",
1382 [PDO_ERR_VSAFE5V_NOT_FIRST] =
1383 " err: vSafe5V Fixed Supply Object Shall always be the first object",
1384 [PDO_ERR_PDO_TYPE_NOT_IN_ORDER] =
1385 " err: PDOs should be in the following order: Fixed; Battery; Variable",
1386 [PDO_ERR_FIXED_NOT_SORTED] =
1387 " err: Fixed supply pdos should be in increasing order of their fixed voltage",
1388 [PDO_ERR_VARIABLE_BATT_NOT_SORTED] =
1389 " err: Variable/Battery supply pdos should be in increasing order of their minimum voltage",
1390 [PDO_ERR_DUPE_PDO] =
1391 " err: Variable/Batt supply pdos cannot have same min/max voltage",
1392 [PDO_ERR_PPS_APDO_NOT_SORTED] =
1393 " err: Programmable power supply apdos should be in increasing order of their maximum voltage",
1394 [PDO_ERR_DUPE_PPS_APDO] =
1395 " err: Programmable power supply apdos cannot have same min/max voltage and max current",
1396 };
1397
1398 static enum pdo_err tcpm_caps_err(struct tcpm_port *port, const u32 *pdo,
1399 unsigned int nr_pdo)
1400 {
1401 unsigned int i;
1402
1403
1404 if (nr_pdo < 1)
1405 return PDO_ERR_NO_VSAFE5V;
1406
1407
1408 if (pdo_type(pdo[0]) != PDO_TYPE_FIXED ||
1409 pdo_fixed_voltage(pdo[0]) != VSAFE5V)
1410 return PDO_ERR_VSAFE5V_NOT_FIRST;
1411
1412 for (i = 1; i < nr_pdo; i++) {
1413 if (pdo_type(pdo[i]) < pdo_type(pdo[i - 1])) {
1414 return PDO_ERR_PDO_TYPE_NOT_IN_ORDER;
1415 } else if (pdo_type(pdo[i]) == pdo_type(pdo[i - 1])) {
1416 enum pd_pdo_type type = pdo_type(pdo[i]);
1417
1418 switch (type) {
1419
1420
1421
1422
1423
1424 case PDO_TYPE_FIXED:
1425 if (pdo_fixed_voltage(pdo[i]) <=
1426 pdo_fixed_voltage(pdo[i - 1]))
1427 return PDO_ERR_FIXED_NOT_SORTED;
1428 break;
1429
1430
1431
1432
1433
1434 case PDO_TYPE_VAR:
1435 case PDO_TYPE_BATT:
1436 if (pdo_min_voltage(pdo[i]) <
1437 pdo_min_voltage(pdo[i - 1]))
1438 return PDO_ERR_VARIABLE_BATT_NOT_SORTED;
1439 else if ((pdo_min_voltage(pdo[i]) ==
1440 pdo_min_voltage(pdo[i - 1])) &&
1441 (pdo_max_voltage(pdo[i]) ==
1442 pdo_max_voltage(pdo[i - 1])))
1443 return PDO_ERR_DUPE_PDO;
1444 break;
1445
1446
1447
1448
1449
1450 case PDO_TYPE_APDO:
1451 if (pdo_apdo_type(pdo[i]) != APDO_TYPE_PPS)
1452 break;
1453
1454 if (pdo_pps_apdo_max_voltage(pdo[i]) <
1455 pdo_pps_apdo_max_voltage(pdo[i - 1]))
1456 return PDO_ERR_PPS_APDO_NOT_SORTED;
1457 else if (pdo_pps_apdo_min_voltage(pdo[i]) ==
1458 pdo_pps_apdo_min_voltage(pdo[i - 1]) &&
1459 pdo_pps_apdo_max_voltage(pdo[i]) ==
1460 pdo_pps_apdo_max_voltage(pdo[i - 1]) &&
1461 pdo_pps_apdo_max_current(pdo[i]) ==
1462 pdo_pps_apdo_max_current(pdo[i - 1]))
1463 return PDO_ERR_DUPE_PPS_APDO;
1464 break;
1465 default:
1466 tcpm_log_force(port, " Unknown pdo type");
1467 }
1468 }
1469 }
1470
1471 return PDO_NO_ERR;
1472 }
1473
1474 static int tcpm_validate_caps(struct tcpm_port *port, const u32 *pdo,
1475 unsigned int nr_pdo)
1476 {
1477 enum pdo_err err_index = tcpm_caps_err(port, pdo, nr_pdo);
1478
1479 if (err_index != PDO_NO_ERR) {
1480 tcpm_log_force(port, " %s", pdo_err_msg[err_index]);
1481 return -EINVAL;
1482 }
1483
1484 return 0;
1485 }
1486
1487 static int tcpm_altmode_enter(struct typec_altmode *altmode)
1488 {
1489 struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
1490 u32 header;
1491
1492 mutex_lock(&port->lock);
1493 header = VDO(altmode->svid, 1, CMD_ENTER_MODE);
1494 header |= VDO_OPOS(altmode->mode);
1495
1496 tcpm_queue_vdm(port, header, NULL, 0);
1497 mod_delayed_work(port->wq, &port->vdm_state_machine, 0);
1498 mutex_unlock(&port->lock);
1499
1500 return 0;
1501 }
1502
1503 static int tcpm_altmode_exit(struct typec_altmode *altmode)
1504 {
1505 struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
1506 u32 header;
1507
1508 mutex_lock(&port->lock);
1509 header = VDO(altmode->svid, 1, CMD_EXIT_MODE);
1510 header |= VDO_OPOS(altmode->mode);
1511
1512 tcpm_queue_vdm(port, header, NULL, 0);
1513 mod_delayed_work(port->wq, &port->vdm_state_machine, 0);
1514 mutex_unlock(&port->lock);
1515
1516 return 0;
1517 }
1518
1519 static int tcpm_altmode_vdm(struct typec_altmode *altmode,
1520 u32 header, const u32 *data, int count)
1521 {
1522 struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
1523
1524 mutex_lock(&port->lock);
1525 tcpm_queue_vdm(port, header, data, count - 1);
1526 mod_delayed_work(port->wq, &port->vdm_state_machine, 0);
1527 mutex_unlock(&port->lock);
1528
1529 return 0;
1530 }
1531
1532 static const struct typec_altmode_ops tcpm_altmode_ops = {
1533 .enter = tcpm_altmode_enter,
1534 .exit = tcpm_altmode_exit,
1535 .vdm = tcpm_altmode_vdm,
1536 };
1537
1538
1539
1540
1541 static inline enum tcpm_state ready_state(struct tcpm_port *port)
1542 {
1543 if (port->pwr_role == TYPEC_SOURCE)
1544 return SRC_READY;
1545 else
1546 return SNK_READY;
1547 }
1548
1549 static int tcpm_pd_send_control(struct tcpm_port *port,
1550 enum pd_ctrl_msg_type type);
1551
1552 static void tcpm_handle_alert(struct tcpm_port *port, const __le32 *payload,
1553 int cnt)
1554 {
1555 u32 p0 = le32_to_cpu(payload[0]);
1556 unsigned int type = usb_pd_ado_type(p0);
1557
1558 if (!type) {
1559 tcpm_log(port, "Alert message received with no type");
1560 return;
1561 }
1562
1563
1564 if (!(type & USB_PD_ADO_TYPE_BATT_STATUS_CHANGE)) {
1565 switch (port->state) {
1566 case SRC_READY:
1567 case SNK_READY:
1568 tcpm_set_state(port, GET_STATUS_SEND, 0);
1569 break;
1570 default:
1571 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
1572 break;
1573 }
1574 }
1575 }
1576
1577 static void tcpm_pd_data_request(struct tcpm_port *port,
1578 const struct pd_message *msg)
1579 {
1580 enum pd_data_msg_type type = pd_header_type_le(msg->header);
1581 unsigned int cnt = pd_header_cnt_le(msg->header);
1582 unsigned int rev = pd_header_rev_le(msg->header);
1583 unsigned int i;
1584
1585 switch (type) {
1586 case PD_DATA_SOURCE_CAP:
1587 if (port->pwr_role != TYPEC_SINK)
1588 break;
1589
1590 for (i = 0; i < cnt; i++)
1591 port->source_caps[i] = le32_to_cpu(msg->payload[i]);
1592
1593 port->nr_source_caps = cnt;
1594
1595 tcpm_log_source_caps(port);
1596
1597 tcpm_validate_caps(port, port->source_caps,
1598 port->nr_source_caps);
1599
1600
1601
1602
1603
1604
1605 if (rev == PD_REV10)
1606 break;
1607
1608 if (rev < PD_MAX_REV)
1609 port->negotiated_rev = rev;
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624 tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
1625 break;
1626 case PD_DATA_REQUEST:
1627 if (port->pwr_role != TYPEC_SOURCE ||
1628 cnt != 1) {
1629 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1630 break;
1631 }
1632
1633
1634
1635
1636
1637
1638 if (rev == PD_REV10) {
1639 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1640 break;
1641 }
1642
1643 if (rev < PD_MAX_REV)
1644 port->negotiated_rev = rev;
1645
1646 port->sink_request = le32_to_cpu(msg->payload[0]);
1647 tcpm_set_state(port, SRC_NEGOTIATE_CAPABILITIES, 0);
1648 break;
1649 case PD_DATA_SINK_CAP:
1650
1651 for (i = 0; i < cnt; i++)
1652 port->sink_caps[i] = le32_to_cpu(msg->payload[i]);
1653 port->nr_sink_caps = cnt;
1654 break;
1655 case PD_DATA_VENDOR_DEF:
1656 tcpm_handle_vdm_request(port, msg->payload, cnt);
1657 break;
1658 case PD_DATA_BIST:
1659 if (port->state == SRC_READY || port->state == SNK_READY) {
1660 port->bist_request = le32_to_cpu(msg->payload[0]);
1661 tcpm_set_state(port, BIST_RX, 0);
1662 }
1663 break;
1664 case PD_DATA_ALERT:
1665 tcpm_handle_alert(port, msg->payload, cnt);
1666 break;
1667 case PD_DATA_BATT_STATUS:
1668 case PD_DATA_GET_COUNTRY_INFO:
1669
1670 tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP);
1671 break;
1672 default:
1673 tcpm_log(port, "Unhandled data message type %#x", type);
1674 break;
1675 }
1676 }
1677
1678 static void tcpm_pps_complete(struct tcpm_port *port, int result)
1679 {
1680 if (port->pps_pending) {
1681 port->pps_status = result;
1682 port->pps_pending = false;
1683 complete(&port->pps_complete);
1684 }
1685 }
1686
1687 static void tcpm_pd_ctrl_request(struct tcpm_port *port,
1688 const struct pd_message *msg)
1689 {
1690 enum pd_ctrl_msg_type type = pd_header_type_le(msg->header);
1691 enum tcpm_state next_state;
1692
1693 switch (type) {
1694 case PD_CTRL_GOOD_CRC:
1695 case PD_CTRL_PING:
1696 break;
1697 case PD_CTRL_GET_SOURCE_CAP:
1698 switch (port->state) {
1699 case SRC_READY:
1700 case SNK_READY:
1701 tcpm_queue_message(port, PD_MSG_DATA_SOURCE_CAP);
1702 break;
1703 default:
1704 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1705 break;
1706 }
1707 break;
1708 case PD_CTRL_GET_SINK_CAP:
1709 switch (port->state) {
1710 case SRC_READY:
1711 case SNK_READY:
1712 tcpm_queue_message(port, PD_MSG_DATA_SINK_CAP);
1713 break;
1714 default:
1715 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1716 break;
1717 }
1718 break;
1719 case PD_CTRL_GOTO_MIN:
1720 break;
1721 case PD_CTRL_PS_RDY:
1722 switch (port->state) {
1723 case SNK_TRANSITION_SINK:
1724 if (port->vbus_present) {
1725 tcpm_set_current_limit(port,
1726 port->current_limit,
1727 port->supply_voltage);
1728 port->explicit_contract = true;
1729 tcpm_set_state(port, SNK_READY, 0);
1730 } else {
1731
1732
1733
1734
1735 tcpm_set_state(port,
1736 SNK_TRANSITION_SINK_VBUS, 0);
1737 }
1738 break;
1739 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED:
1740 tcpm_set_state(port, PR_SWAP_SRC_SNK_SINK_ON, 0);
1741 break;
1742 case PR_SWAP_SNK_SRC_SINK_OFF:
1743 tcpm_set_state(port, PR_SWAP_SNK_SRC_SOURCE_ON, 0);
1744 break;
1745 case VCONN_SWAP_WAIT_FOR_VCONN:
1746 tcpm_set_state(port, VCONN_SWAP_TURN_OFF_VCONN, 0);
1747 break;
1748 default:
1749 break;
1750 }
1751 break;
1752 case PD_CTRL_REJECT:
1753 case PD_CTRL_WAIT:
1754 case PD_CTRL_NOT_SUPP:
1755 switch (port->state) {
1756 case SNK_NEGOTIATE_CAPABILITIES:
1757
1758 if (port->explicit_contract)
1759 next_state = SNK_READY;
1760 else
1761 next_state = SNK_WAIT_CAPABILITIES;
1762 tcpm_set_state(port, next_state, 0);
1763 break;
1764 case SNK_NEGOTIATE_PPS_CAPABILITIES:
1765
1766 port->pps_data.out_volt = port->supply_voltage;
1767 port->pps_data.op_curr = port->current_limit;
1768 port->pps_status = (type == PD_CTRL_WAIT ?
1769 -EAGAIN : -EOPNOTSUPP);
1770 tcpm_set_state(port, SNK_READY, 0);
1771 break;
1772 case DR_SWAP_SEND:
1773 port->swap_status = (type == PD_CTRL_WAIT ?
1774 -EAGAIN : -EOPNOTSUPP);
1775 tcpm_set_state(port, DR_SWAP_CANCEL, 0);
1776 break;
1777 case PR_SWAP_SEND:
1778 port->swap_status = (type == PD_CTRL_WAIT ?
1779 -EAGAIN : -EOPNOTSUPP);
1780 tcpm_set_state(port, PR_SWAP_CANCEL, 0);
1781 break;
1782 case VCONN_SWAP_SEND:
1783 port->swap_status = (type == PD_CTRL_WAIT ?
1784 -EAGAIN : -EOPNOTSUPP);
1785 tcpm_set_state(port, VCONN_SWAP_CANCEL, 0);
1786 break;
1787 default:
1788 break;
1789 }
1790 break;
1791 case PD_CTRL_ACCEPT:
1792 switch (port->state) {
1793 case SNK_NEGOTIATE_CAPABILITIES:
1794 port->pps_data.active = false;
1795 tcpm_set_state(port, SNK_TRANSITION_SINK, 0);
1796 break;
1797 case SNK_NEGOTIATE_PPS_CAPABILITIES:
1798 port->pps_data.active = true;
1799 port->supply_voltage = port->pps_data.out_volt;
1800 port->current_limit = port->pps_data.op_curr;
1801 tcpm_set_state(port, SNK_TRANSITION_SINK, 0);
1802 break;
1803 case SOFT_RESET_SEND:
1804 port->message_id = 0;
1805 port->rx_msgid = -1;
1806 if (port->pwr_role == TYPEC_SOURCE)
1807 next_state = SRC_SEND_CAPABILITIES;
1808 else
1809 next_state = SNK_WAIT_CAPABILITIES;
1810 tcpm_set_state(port, next_state, 0);
1811 break;
1812 case DR_SWAP_SEND:
1813 tcpm_set_state(port, DR_SWAP_CHANGE_DR, 0);
1814 break;
1815 case PR_SWAP_SEND:
1816 tcpm_set_state(port, PR_SWAP_START, 0);
1817 break;
1818 case VCONN_SWAP_SEND:
1819 tcpm_set_state(port, VCONN_SWAP_START, 0);
1820 break;
1821 default:
1822 break;
1823 }
1824 break;
1825 case PD_CTRL_SOFT_RESET:
1826 tcpm_set_state(port, SOFT_RESET, 0);
1827 break;
1828 case PD_CTRL_DR_SWAP:
1829 if (port->port_type != TYPEC_PORT_DRP) {
1830 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1831 break;
1832 }
1833
1834
1835
1836
1837
1838 switch (port->state) {
1839 case SRC_READY:
1840 case SNK_READY:
1841 tcpm_set_state(port, DR_SWAP_ACCEPT, 0);
1842 break;
1843 default:
1844 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
1845 break;
1846 }
1847 break;
1848 case PD_CTRL_PR_SWAP:
1849 if (port->port_type != TYPEC_PORT_DRP) {
1850 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1851 break;
1852 }
1853 switch (port->state) {
1854 case SRC_READY:
1855 case SNK_READY:
1856 tcpm_set_state(port, PR_SWAP_ACCEPT, 0);
1857 break;
1858 default:
1859 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
1860 break;
1861 }
1862 break;
1863 case PD_CTRL_VCONN_SWAP:
1864 switch (port->state) {
1865 case SRC_READY:
1866 case SNK_READY:
1867 tcpm_set_state(port, VCONN_SWAP_ACCEPT, 0);
1868 break;
1869 default:
1870 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
1871 break;
1872 }
1873 break;
1874 case PD_CTRL_GET_SOURCE_CAP_EXT:
1875 case PD_CTRL_GET_STATUS:
1876 case PD_CTRL_FR_SWAP:
1877 case PD_CTRL_GET_PPS_STATUS:
1878 case PD_CTRL_GET_COUNTRY_CODES:
1879
1880 tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP);
1881 break;
1882 default:
1883 tcpm_log(port, "Unhandled ctrl message type %#x", type);
1884 break;
1885 }
1886 }
1887
1888 static void tcpm_pd_ext_msg_request(struct tcpm_port *port,
1889 const struct pd_message *msg)
1890 {
1891 enum pd_ext_msg_type type = pd_header_type_le(msg->header);
1892 unsigned int data_size = pd_ext_header_data_size_le(msg->ext_msg.header);
1893
1894 if (!(msg->ext_msg.header & PD_EXT_HDR_CHUNKED)) {
1895 tcpm_log(port, "Unchunked extended messages unsupported");
1896 return;
1897 }
1898
1899 if (data_size > PD_EXT_MAX_CHUNK_DATA) {
1900 tcpm_log(port, "Chunk handling not yet supported");
1901 return;
1902 }
1903
1904 switch (type) {
1905 case PD_EXT_STATUS:
1906
1907
1908
1909
1910 if (msg->ext_msg.data[USB_PD_EXT_SDB_EVENT_FLAGS] &
1911 USB_PD_EXT_SDB_PPS_EVENTS)
1912 tcpm_set_state(port, GET_PPS_STATUS_SEND, 0);
1913 else
1914 tcpm_set_state(port, ready_state(port), 0);
1915 break;
1916 case PD_EXT_PPS_STATUS:
1917
1918
1919
1920
1921 tcpm_set_state(port, ready_state(port), 0);
1922 break;
1923 case PD_EXT_SOURCE_CAP_EXT:
1924 case PD_EXT_GET_BATT_CAP:
1925 case PD_EXT_GET_BATT_STATUS:
1926 case PD_EXT_BATT_CAP:
1927 case PD_EXT_GET_MANUFACTURER_INFO:
1928 case PD_EXT_MANUFACTURER_INFO:
1929 case PD_EXT_SECURITY_REQUEST:
1930 case PD_EXT_SECURITY_RESPONSE:
1931 case PD_EXT_FW_UPDATE_REQUEST:
1932 case PD_EXT_FW_UPDATE_RESPONSE:
1933 case PD_EXT_COUNTRY_INFO:
1934 case PD_EXT_COUNTRY_CODES:
1935 tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP);
1936 break;
1937 default:
1938 tcpm_log(port, "Unhandled extended message type %#x", type);
1939 break;
1940 }
1941 }
1942
1943 static void tcpm_pd_rx_handler(struct work_struct *work)
1944 {
1945 struct pd_rx_event *event = container_of(work,
1946 struct pd_rx_event, work);
1947 const struct pd_message *msg = &event->msg;
1948 unsigned int cnt = pd_header_cnt_le(msg->header);
1949 struct tcpm_port *port = event->port;
1950
1951 mutex_lock(&port->lock);
1952
1953 tcpm_log(port, "PD RX, header: %#x [%d]", le16_to_cpu(msg->header),
1954 port->attached);
1955
1956 if (port->attached) {
1957 enum pd_ctrl_msg_type type = pd_header_type_le(msg->header);
1958 unsigned int msgid = pd_header_msgid_le(msg->header);
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969 if (msgid == port->rx_msgid && type != PD_CTRL_SOFT_RESET)
1970 goto done;
1971 port->rx_msgid = msgid;
1972
1973
1974
1975
1976
1977 if (!!(le16_to_cpu(msg->header) & PD_HEADER_DATA_ROLE) ==
1978 (port->data_role == TYPEC_HOST)) {
1979 tcpm_log(port,
1980 "Data role mismatch, initiating error recovery");
1981 tcpm_set_state(port, ERROR_RECOVERY, 0);
1982 } else {
1983 if (msg->header & PD_HEADER_EXT_HDR)
1984 tcpm_pd_ext_msg_request(port, msg);
1985 else if (cnt)
1986 tcpm_pd_data_request(port, msg);
1987 else
1988 tcpm_pd_ctrl_request(port, msg);
1989 }
1990 }
1991
1992 done:
1993 mutex_unlock(&port->lock);
1994 kfree(event);
1995 }
1996
1997 void tcpm_pd_receive(struct tcpm_port *port, const struct pd_message *msg)
1998 {
1999 struct pd_rx_event *event;
2000
2001 event = kzalloc(sizeof(*event), GFP_ATOMIC);
2002 if (!event)
2003 return;
2004
2005 INIT_WORK(&event->work, tcpm_pd_rx_handler);
2006 event->port = port;
2007 memcpy(&event->msg, msg, sizeof(*msg));
2008 queue_work(port->wq, &event->work);
2009 }
2010 EXPORT_SYMBOL_GPL(tcpm_pd_receive);
2011
2012 static int tcpm_pd_send_control(struct tcpm_port *port,
2013 enum pd_ctrl_msg_type type)
2014 {
2015 struct pd_message msg;
2016
2017 memset(&msg, 0, sizeof(msg));
2018 msg.header = PD_HEADER_LE(type, port->pwr_role,
2019 port->data_role,
2020 port->negotiated_rev,
2021 port->message_id, 0);
2022
2023 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
2024 }
2025
2026
2027
2028
2029
2030
2031 static bool tcpm_send_queued_message(struct tcpm_port *port)
2032 {
2033 enum pd_msg_request queued_message;
2034
2035 do {
2036 queued_message = port->queued_message;
2037 port->queued_message = PD_MSG_NONE;
2038
2039 switch (queued_message) {
2040 case PD_MSG_CTRL_WAIT:
2041 tcpm_pd_send_control(port, PD_CTRL_WAIT);
2042 break;
2043 case PD_MSG_CTRL_REJECT:
2044 tcpm_pd_send_control(port, PD_CTRL_REJECT);
2045 break;
2046 case PD_MSG_CTRL_NOT_SUPP:
2047 tcpm_pd_send_control(port, PD_CTRL_NOT_SUPP);
2048 break;
2049 case PD_MSG_DATA_SINK_CAP:
2050 tcpm_pd_send_sink_caps(port);
2051 break;
2052 case PD_MSG_DATA_SOURCE_CAP:
2053 tcpm_pd_send_source_caps(port);
2054 break;
2055 default:
2056 break;
2057 }
2058 } while (port->queued_message != PD_MSG_NONE);
2059
2060 if (port->delayed_state != INVALID_STATE) {
2061 if (time_is_after_jiffies(port->delayed_runtime)) {
2062 mod_delayed_work(port->wq, &port->state_machine,
2063 port->delayed_runtime - jiffies);
2064 return true;
2065 }
2066 port->delayed_state = INVALID_STATE;
2067 }
2068 return false;
2069 }
2070
2071 static int tcpm_pd_check_request(struct tcpm_port *port)
2072 {
2073 u32 pdo, rdo = port->sink_request;
2074 unsigned int max, op, pdo_max, index;
2075 enum pd_pdo_type type;
2076
2077 index = rdo_index(rdo);
2078 if (!index || index > port->nr_src_pdo)
2079 return -EINVAL;
2080
2081 pdo = port->src_pdo[index - 1];
2082 type = pdo_type(pdo);
2083 switch (type) {
2084 case PDO_TYPE_FIXED:
2085 case PDO_TYPE_VAR:
2086 max = rdo_max_current(rdo);
2087 op = rdo_op_current(rdo);
2088 pdo_max = pdo_max_current(pdo);
2089
2090 if (op > pdo_max)
2091 return -EINVAL;
2092 if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH))
2093 return -EINVAL;
2094
2095 if (type == PDO_TYPE_FIXED)
2096 tcpm_log(port,
2097 "Requested %u mV, %u mA for %u / %u mA",
2098 pdo_fixed_voltage(pdo), pdo_max, op, max);
2099 else
2100 tcpm_log(port,
2101 "Requested %u -> %u mV, %u mA for %u / %u mA",
2102 pdo_min_voltage(pdo), pdo_max_voltage(pdo),
2103 pdo_max, op, max);
2104 break;
2105 case PDO_TYPE_BATT:
2106 max = rdo_max_power(rdo);
2107 op = rdo_op_power(rdo);
2108 pdo_max = pdo_max_power(pdo);
2109
2110 if (op > pdo_max)
2111 return -EINVAL;
2112 if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH))
2113 return -EINVAL;
2114 tcpm_log(port,
2115 "Requested %u -> %u mV, %u mW for %u / %u mW",
2116 pdo_min_voltage(pdo), pdo_max_voltage(pdo),
2117 pdo_max, op, max);
2118 break;
2119 default:
2120 return -EINVAL;
2121 }
2122
2123 port->op_vsafe5v = index == 1;
2124
2125 return 0;
2126 }
2127
2128 #define min_power(x, y) min(pdo_max_power(x), pdo_max_power(y))
2129 #define min_current(x, y) min(pdo_max_current(x), pdo_max_current(y))
2130
2131 static int tcpm_pd_select_pdo(struct tcpm_port *port, int *sink_pdo,
2132 int *src_pdo)
2133 {
2134 unsigned int i, j, max_src_mv = 0, min_src_mv = 0, max_mw = 0,
2135 max_mv = 0, src_mw = 0, src_ma = 0, max_snk_mv = 0,
2136 min_snk_mv = 0;
2137 int ret = -EINVAL;
2138
2139 port->pps_data.supported = false;
2140 port->usb_type = POWER_SUPPLY_USB_TYPE_PD;
2141
2142
2143
2144
2145
2146 for (i = 0; i < port->nr_source_caps; i++) {
2147 u32 pdo = port->source_caps[i];
2148 enum pd_pdo_type type = pdo_type(pdo);
2149
2150 switch (type) {
2151 case PDO_TYPE_FIXED:
2152 max_src_mv = pdo_fixed_voltage(pdo);
2153 min_src_mv = max_src_mv;
2154 break;
2155 case PDO_TYPE_BATT:
2156 case PDO_TYPE_VAR:
2157 max_src_mv = pdo_max_voltage(pdo);
2158 min_src_mv = pdo_min_voltage(pdo);
2159 break;
2160 case PDO_TYPE_APDO:
2161 if (pdo_apdo_type(pdo) == APDO_TYPE_PPS) {
2162 port->pps_data.supported = true;
2163 port->usb_type =
2164 POWER_SUPPLY_USB_TYPE_PD_PPS;
2165 }
2166 continue;
2167 default:
2168 tcpm_log(port, "Invalid source PDO type, ignoring");
2169 continue;
2170 }
2171
2172 switch (type) {
2173 case PDO_TYPE_FIXED:
2174 case PDO_TYPE_VAR:
2175 src_ma = pdo_max_current(pdo);
2176 src_mw = src_ma * min_src_mv / 1000;
2177 break;
2178 case PDO_TYPE_BATT:
2179 src_mw = pdo_max_power(pdo);
2180 break;
2181 case PDO_TYPE_APDO:
2182 continue;
2183 default:
2184 tcpm_log(port, "Invalid source PDO type, ignoring");
2185 continue;
2186 }
2187
2188 for (j = 0; j < port->nr_snk_pdo; j++) {
2189 pdo = port->snk_pdo[j];
2190
2191 switch (pdo_type(pdo)) {
2192 case PDO_TYPE_FIXED:
2193 max_snk_mv = pdo_fixed_voltage(pdo);
2194 min_snk_mv = max_snk_mv;
2195 break;
2196 case PDO_TYPE_BATT:
2197 case PDO_TYPE_VAR:
2198 max_snk_mv = pdo_max_voltage(pdo);
2199 min_snk_mv = pdo_min_voltage(pdo);
2200 break;
2201 case PDO_TYPE_APDO:
2202 continue;
2203 default:
2204 tcpm_log(port, "Invalid sink PDO type, ignoring");
2205 continue;
2206 }
2207
2208 if (max_src_mv <= max_snk_mv &&
2209 min_src_mv >= min_snk_mv) {
2210
2211 if ((src_mw == max_mw && min_src_mv > max_mv) ||
2212 src_mw > max_mw) {
2213 *src_pdo = i;
2214 *sink_pdo = j;
2215 max_mw = src_mw;
2216 max_mv = min_src_mv;
2217 ret = 0;
2218 }
2219 }
2220 }
2221 }
2222
2223 return ret;
2224 }
2225
2226 #define min_pps_apdo_current(x, y) \
2227 min(pdo_pps_apdo_max_current(x), pdo_pps_apdo_max_current(y))
2228
2229 static unsigned int tcpm_pd_select_pps_apdo(struct tcpm_port *port)
2230 {
2231 unsigned int i, j, max_mw = 0, max_mv = 0;
2232 unsigned int min_src_mv, max_src_mv, src_ma, src_mw;
2233 unsigned int min_snk_mv, max_snk_mv;
2234 unsigned int max_op_mv;
2235 u32 pdo, src, snk;
2236 unsigned int src_pdo = 0, snk_pdo = 0;
2237
2238
2239
2240
2241
2242
2243 for (i = 1; i < port->nr_source_caps; ++i) {
2244 pdo = port->source_caps[i];
2245
2246 switch (pdo_type(pdo)) {
2247 case PDO_TYPE_APDO:
2248 if (pdo_apdo_type(pdo) != APDO_TYPE_PPS) {
2249 tcpm_log(port, "Not PPS APDO (source), ignoring");
2250 continue;
2251 }
2252
2253 min_src_mv = pdo_pps_apdo_min_voltage(pdo);
2254 max_src_mv = pdo_pps_apdo_max_voltage(pdo);
2255 src_ma = pdo_pps_apdo_max_current(pdo);
2256 src_mw = (src_ma * max_src_mv) / 1000;
2257
2258
2259
2260
2261
2262
2263 for (j = 1; j < port->nr_snk_pdo; j++) {
2264 pdo = port->snk_pdo[j];
2265
2266 switch (pdo_type(pdo)) {
2267 case PDO_TYPE_APDO:
2268 if (pdo_apdo_type(pdo) != APDO_TYPE_PPS) {
2269 tcpm_log(port,
2270 "Not PPS APDO (sink), ignoring");
2271 continue;
2272 }
2273
2274 min_snk_mv =
2275 pdo_pps_apdo_min_voltage(pdo);
2276 max_snk_mv =
2277 pdo_pps_apdo_max_voltage(pdo);
2278 break;
2279 default:
2280 tcpm_log(port,
2281 "Not APDO type (sink), ignoring");
2282 continue;
2283 }
2284
2285 if (min_src_mv <= max_snk_mv &&
2286 max_src_mv >= min_snk_mv) {
2287 max_op_mv = min(max_src_mv, max_snk_mv);
2288 src_mw = (max_op_mv * src_ma) / 1000;
2289
2290 if ((src_mw == max_mw &&
2291 max_op_mv > max_mv) ||
2292 src_mw > max_mw) {
2293 src_pdo = i;
2294 snk_pdo = j;
2295 max_mw = src_mw;
2296 max_mv = max_op_mv;
2297 }
2298 }
2299 }
2300
2301 break;
2302 default:
2303 tcpm_log(port, "Not APDO type (source), ignoring");
2304 continue;
2305 }
2306 }
2307
2308 if (src_pdo) {
2309 src = port->source_caps[src_pdo];
2310 snk = port->snk_pdo[snk_pdo];
2311
2312 port->pps_data.min_volt = max(pdo_pps_apdo_min_voltage(src),
2313 pdo_pps_apdo_min_voltage(snk));
2314 port->pps_data.max_volt = min(pdo_pps_apdo_max_voltage(src),
2315 pdo_pps_apdo_max_voltage(snk));
2316 port->pps_data.max_curr = min_pps_apdo_current(src, snk);
2317 port->pps_data.out_volt = min(port->pps_data.max_volt,
2318 max(port->pps_data.min_volt,
2319 port->pps_data.out_volt));
2320 port->pps_data.op_curr = min(port->pps_data.max_curr,
2321 port->pps_data.op_curr);
2322 }
2323
2324 return src_pdo;
2325 }
2326
2327 static int tcpm_pd_build_request(struct tcpm_port *port, u32 *rdo)
2328 {
2329 unsigned int mv, ma, mw, flags;
2330 unsigned int max_ma, max_mw;
2331 enum pd_pdo_type type;
2332 u32 pdo, matching_snk_pdo;
2333 int src_pdo_index = 0;
2334 int snk_pdo_index = 0;
2335 int ret;
2336
2337 ret = tcpm_pd_select_pdo(port, &snk_pdo_index, &src_pdo_index);
2338 if (ret < 0)
2339 return ret;
2340
2341 pdo = port->source_caps[src_pdo_index];
2342 matching_snk_pdo = port->snk_pdo[snk_pdo_index];
2343 type = pdo_type(pdo);
2344
2345 switch (type) {
2346 case PDO_TYPE_FIXED:
2347 mv = pdo_fixed_voltage(pdo);
2348 break;
2349 case PDO_TYPE_BATT:
2350 case PDO_TYPE_VAR:
2351 mv = pdo_min_voltage(pdo);
2352 break;
2353 default:
2354 tcpm_log(port, "Invalid PDO selected!");
2355 return -EINVAL;
2356 }
2357
2358
2359 if (type == PDO_TYPE_BATT) {
2360 mw = min_power(pdo, matching_snk_pdo);
2361 ma = 1000 * mw / mv;
2362 } else {
2363 ma = min_current(pdo, matching_snk_pdo);
2364 mw = ma * mv / 1000;
2365 }
2366
2367 flags = RDO_USB_COMM | RDO_NO_SUSPEND;
2368
2369
2370 max_ma = ma;
2371 max_mw = mw;
2372 if (mw < port->operating_snk_mw) {
2373 flags |= RDO_CAP_MISMATCH;
2374 if (type == PDO_TYPE_BATT &&
2375 (pdo_max_power(matching_snk_pdo) > pdo_max_power(pdo)))
2376 max_mw = pdo_max_power(matching_snk_pdo);
2377 else if (pdo_max_current(matching_snk_pdo) >
2378 pdo_max_current(pdo))
2379 max_ma = pdo_max_current(matching_snk_pdo);
2380 }
2381
2382 tcpm_log(port, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d",
2383 port->cc_req, port->cc1, port->cc2, port->vbus_source,
2384 port->vconn_role == TYPEC_SOURCE ? "source" : "sink",
2385 port->polarity);
2386
2387 if (type == PDO_TYPE_BATT) {
2388 *rdo = RDO_BATT(src_pdo_index + 1, mw, max_mw, flags);
2389
2390 tcpm_log(port, "Requesting PDO %d: %u mV, %u mW%s",
2391 src_pdo_index, mv, mw,
2392 flags & RDO_CAP_MISMATCH ? " [mismatch]" : "");
2393 } else {
2394 *rdo = RDO_FIXED(src_pdo_index + 1, ma, max_ma, flags);
2395
2396 tcpm_log(port, "Requesting PDO %d: %u mV, %u mA%s",
2397 src_pdo_index, mv, ma,
2398 flags & RDO_CAP_MISMATCH ? " [mismatch]" : "");
2399 }
2400
2401 port->current_limit = ma;
2402 port->supply_voltage = mv;
2403
2404 return 0;
2405 }
2406
2407 static int tcpm_pd_send_request(struct tcpm_port *port)
2408 {
2409 struct pd_message msg;
2410 int ret;
2411 u32 rdo;
2412
2413 ret = tcpm_pd_build_request(port, &rdo);
2414 if (ret < 0)
2415 return ret;
2416
2417 memset(&msg, 0, sizeof(msg));
2418 msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
2419 port->pwr_role,
2420 port->data_role,
2421 port->negotiated_rev,
2422 port->message_id, 1);
2423 msg.payload[0] = cpu_to_le32(rdo);
2424
2425 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
2426 }
2427
2428 static int tcpm_pd_build_pps_request(struct tcpm_port *port, u32 *rdo)
2429 {
2430 unsigned int out_mv, op_ma, op_mw, max_mv, max_ma, flags;
2431 enum pd_pdo_type type;
2432 unsigned int src_pdo_index;
2433 u32 pdo;
2434
2435 src_pdo_index = tcpm_pd_select_pps_apdo(port);
2436 if (!src_pdo_index)
2437 return -EOPNOTSUPP;
2438
2439 pdo = port->source_caps[src_pdo_index];
2440 type = pdo_type(pdo);
2441
2442 switch (type) {
2443 case PDO_TYPE_APDO:
2444 if (pdo_apdo_type(pdo) != APDO_TYPE_PPS) {
2445 tcpm_log(port, "Invalid APDO selected!");
2446 return -EINVAL;
2447 }
2448 max_mv = port->pps_data.max_volt;
2449 max_ma = port->pps_data.max_curr;
2450 out_mv = port->pps_data.out_volt;
2451 op_ma = port->pps_data.op_curr;
2452 break;
2453 default:
2454 tcpm_log(port, "Invalid PDO selected!");
2455 return -EINVAL;
2456 }
2457
2458 flags = RDO_USB_COMM | RDO_NO_SUSPEND;
2459
2460 op_mw = (op_ma * out_mv) / 1000;
2461 if (op_mw < port->operating_snk_mw) {
2462
2463
2464
2465
2466
2467
2468 op_ma = (port->operating_snk_mw * 1000) / out_mv;
2469 if ((port->operating_snk_mw * 1000) % out_mv)
2470 ++op_ma;
2471 op_ma += RDO_PROG_CURR_MA_STEP - (op_ma % RDO_PROG_CURR_MA_STEP);
2472
2473 if (op_ma > max_ma) {
2474 op_ma = max_ma;
2475 out_mv = (port->operating_snk_mw * 1000) / op_ma;
2476 if ((port->operating_snk_mw * 1000) % op_ma)
2477 ++out_mv;
2478 out_mv += RDO_PROG_VOLT_MV_STEP -
2479 (out_mv % RDO_PROG_VOLT_MV_STEP);
2480
2481 if (out_mv > max_mv) {
2482 tcpm_log(port, "Invalid PPS APDO selected!");
2483 return -EINVAL;
2484 }
2485 }
2486 }
2487
2488 tcpm_log(port, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d",
2489 port->cc_req, port->cc1, port->cc2, port->vbus_source,
2490 port->vconn_role == TYPEC_SOURCE ? "source" : "sink",
2491 port->polarity);
2492
2493 *rdo = RDO_PROG(src_pdo_index + 1, out_mv, op_ma, flags);
2494
2495 tcpm_log(port, "Requesting APDO %d: %u mV, %u mA",
2496 src_pdo_index, out_mv, op_ma);
2497
2498 port->pps_data.op_curr = op_ma;
2499 port->pps_data.out_volt = out_mv;
2500
2501 return 0;
2502 }
2503
2504 static int tcpm_pd_send_pps_request(struct tcpm_port *port)
2505 {
2506 struct pd_message msg;
2507 int ret;
2508 u32 rdo;
2509
2510 ret = tcpm_pd_build_pps_request(port, &rdo);
2511 if (ret < 0)
2512 return ret;
2513
2514 memset(&msg, 0, sizeof(msg));
2515 msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
2516 port->pwr_role,
2517 port->data_role,
2518 port->negotiated_rev,
2519 port->message_id, 1);
2520 msg.payload[0] = cpu_to_le32(rdo);
2521
2522 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
2523 }
2524
2525 static int tcpm_set_vbus(struct tcpm_port *port, bool enable)
2526 {
2527 int ret;
2528
2529 if (enable && port->vbus_charge)
2530 return -EINVAL;
2531
2532 tcpm_log(port, "vbus:=%d charge=%d", enable, port->vbus_charge);
2533
2534 ret = port->tcpc->set_vbus(port->tcpc, enable, port->vbus_charge);
2535 if (ret < 0)
2536 return ret;
2537
2538 port->vbus_source = enable;
2539 return 0;
2540 }
2541
2542 static int tcpm_set_charge(struct tcpm_port *port, bool charge)
2543 {
2544 int ret;
2545
2546 if (charge && port->vbus_source)
2547 return -EINVAL;
2548
2549 if (charge != port->vbus_charge) {
2550 tcpm_log(port, "vbus=%d charge:=%d", port->vbus_source, charge);
2551 ret = port->tcpc->set_vbus(port->tcpc, port->vbus_source,
2552 charge);
2553 if (ret < 0)
2554 return ret;
2555 }
2556 port->vbus_charge = charge;
2557 return 0;
2558 }
2559
2560 static bool tcpm_start_toggling(struct tcpm_port *port, enum typec_cc_status cc)
2561 {
2562 int ret;
2563
2564 if (!port->tcpc->start_toggling)
2565 return false;
2566
2567 tcpm_log_force(port, "Start toggling");
2568 ret = port->tcpc->start_toggling(port->tcpc, port->port_type, cc);
2569 return ret == 0;
2570 }
2571
2572 static void tcpm_set_cc(struct tcpm_port *port, enum typec_cc_status cc)
2573 {
2574 tcpm_log(port, "cc:=%d", cc);
2575 port->cc_req = cc;
2576 port->tcpc->set_cc(port->tcpc, cc);
2577 }
2578
2579 static int tcpm_init_vbus(struct tcpm_port *port)
2580 {
2581 int ret;
2582
2583 ret = port->tcpc->set_vbus(port->tcpc, false, false);
2584 port->vbus_source = false;
2585 port->vbus_charge = false;
2586 return ret;
2587 }
2588
2589 static int tcpm_init_vconn(struct tcpm_port *port)
2590 {
2591 int ret;
2592
2593 ret = port->tcpc->set_vconn(port->tcpc, false);
2594 port->vconn_role = TYPEC_SINK;
2595 return ret;
2596 }
2597
2598 static void tcpm_typec_connect(struct tcpm_port *port)
2599 {
2600 if (!port->connected) {
2601
2602 memset(&port->partner_ident, 0, sizeof(port->partner_ident));
2603 port->partner_desc.usb_pd = port->pd_capable;
2604 if (tcpm_port_is_debug(port))
2605 port->partner_desc.accessory = TYPEC_ACCESSORY_DEBUG;
2606 else if (tcpm_port_is_audio(port))
2607 port->partner_desc.accessory = TYPEC_ACCESSORY_AUDIO;
2608 else
2609 port->partner_desc.accessory = TYPEC_ACCESSORY_NONE;
2610 port->partner = typec_register_partner(port->typec_port,
2611 &port->partner_desc);
2612 port->connected = true;
2613 }
2614 }
2615
2616 static int tcpm_src_attach(struct tcpm_port *port)
2617 {
2618 enum typec_cc_polarity polarity =
2619 port->cc2 == TYPEC_CC_RD ? TYPEC_POLARITY_CC2
2620 : TYPEC_POLARITY_CC1;
2621 int ret;
2622
2623 if (port->attached)
2624 return 0;
2625
2626 ret = tcpm_set_polarity(port, polarity);
2627 if (ret < 0)
2628 return ret;
2629
2630 ret = tcpm_set_roles(port, true, TYPEC_SOURCE, TYPEC_HOST);
2631 if (ret < 0)
2632 return ret;
2633
2634 ret = port->tcpc->set_pd_rx(port->tcpc, true);
2635 if (ret < 0)
2636 goto out_disable_mux;
2637
2638
2639
2640
2641
2642
2643 if ((polarity == TYPEC_POLARITY_CC1 && port->cc2 == TYPEC_CC_RA) ||
2644 (polarity == TYPEC_POLARITY_CC2 && port->cc1 == TYPEC_CC_RA)) {
2645 ret = tcpm_set_vconn(port, true);
2646 if (ret < 0)
2647 goto out_disable_pd;
2648 }
2649
2650 ret = tcpm_set_vbus(port, true);
2651 if (ret < 0)
2652 goto out_disable_vconn;
2653
2654 port->pd_capable = false;
2655
2656 port->partner = NULL;
2657
2658 port->attached = true;
2659 port->send_discover = true;
2660
2661 return 0;
2662
2663 out_disable_vconn:
2664 tcpm_set_vconn(port, false);
2665 out_disable_pd:
2666 port->tcpc->set_pd_rx(port->tcpc, false);
2667 out_disable_mux:
2668 tcpm_mux_set(port, TYPEC_STATE_SAFE, USB_ROLE_NONE,
2669 TYPEC_ORIENTATION_NONE);
2670 return ret;
2671 }
2672
2673 static void tcpm_typec_disconnect(struct tcpm_port *port)
2674 {
2675 if (port->connected) {
2676 typec_unregister_partner(port->partner);
2677 port->partner = NULL;
2678 port->connected = false;
2679 }
2680 }
2681
2682 static void tcpm_unregister_altmodes(struct tcpm_port *port)
2683 {
2684 struct pd_mode_data *modep = &port->mode_data;
2685 int i;
2686
2687 for (i = 0; i < modep->altmodes; i++) {
2688 typec_unregister_altmode(port->partner_altmode[i]);
2689 port->partner_altmode[i] = NULL;
2690 }
2691
2692 memset(modep, 0, sizeof(*modep));
2693 }
2694
2695 static void tcpm_reset_port(struct tcpm_port *port)
2696 {
2697 tcpm_unregister_altmodes(port);
2698 tcpm_typec_disconnect(port);
2699 port->attached = false;
2700 port->pd_capable = false;
2701 port->pps_data.supported = false;
2702
2703
2704
2705
2706
2707 port->rx_msgid = -1;
2708
2709 port->tcpc->set_pd_rx(port->tcpc, false);
2710 tcpm_init_vbus(port);
2711 tcpm_init_vconn(port);
2712 tcpm_set_current_limit(port, 0, 0);
2713 tcpm_set_polarity(port, TYPEC_POLARITY_CC1);
2714 tcpm_mux_set(port, TYPEC_STATE_SAFE, USB_ROLE_NONE,
2715 TYPEC_ORIENTATION_NONE);
2716 tcpm_set_attached_state(port, false);
2717 port->try_src_count = 0;
2718 port->try_snk_count = 0;
2719 port->usb_type = POWER_SUPPLY_USB_TYPE_C;
2720
2721 power_supply_changed(port->psy);
2722 }
2723
2724 static void tcpm_detach(struct tcpm_port *port)
2725 {
2726 if (!port->attached)
2727 return;
2728
2729 if (tcpm_port_is_disconnected(port))
2730 port->hard_reset_count = 0;
2731
2732 tcpm_reset_port(port);
2733 }
2734
2735 static void tcpm_src_detach(struct tcpm_port *port)
2736 {
2737 tcpm_detach(port);
2738 }
2739
2740 static int tcpm_snk_attach(struct tcpm_port *port)
2741 {
2742 int ret;
2743
2744 if (port->attached)
2745 return 0;
2746
2747 ret = tcpm_set_polarity(port, port->cc2 != TYPEC_CC_OPEN ?
2748 TYPEC_POLARITY_CC2 : TYPEC_POLARITY_CC1);
2749 if (ret < 0)
2750 return ret;
2751
2752 ret = tcpm_set_roles(port, true, TYPEC_SINK, TYPEC_DEVICE);
2753 if (ret < 0)
2754 return ret;
2755
2756 port->pd_capable = false;
2757
2758 port->partner = NULL;
2759
2760 port->attached = true;
2761 port->send_discover = true;
2762
2763 return 0;
2764 }
2765
2766 static void tcpm_snk_detach(struct tcpm_port *port)
2767 {
2768 tcpm_detach(port);
2769 }
2770
2771 static int tcpm_acc_attach(struct tcpm_port *port)
2772 {
2773 int ret;
2774
2775 if (port->attached)
2776 return 0;
2777
2778 ret = tcpm_set_roles(port, true, TYPEC_SOURCE, TYPEC_HOST);
2779 if (ret < 0)
2780 return ret;
2781
2782 port->partner = NULL;
2783
2784 tcpm_typec_connect(port);
2785
2786 port->attached = true;
2787
2788 return 0;
2789 }
2790
2791 static void tcpm_acc_detach(struct tcpm_port *port)
2792 {
2793 tcpm_detach(port);
2794 }
2795
2796 static inline enum tcpm_state hard_reset_state(struct tcpm_port *port)
2797 {
2798 if (port->hard_reset_count < PD_N_HARD_RESET_COUNT)
2799 return HARD_RESET_SEND;
2800 if (port->pd_capable)
2801 return ERROR_RECOVERY;
2802 if (port->pwr_role == TYPEC_SOURCE)
2803 return SRC_UNATTACHED;
2804 if (port->state == SNK_WAIT_CAPABILITIES)
2805 return SNK_READY;
2806 return SNK_UNATTACHED;
2807 }
2808
2809 static inline enum tcpm_state unattached_state(struct tcpm_port *port)
2810 {
2811 if (port->port_type == TYPEC_PORT_DRP) {
2812 if (port->pwr_role == TYPEC_SOURCE)
2813 return SRC_UNATTACHED;
2814 else
2815 return SNK_UNATTACHED;
2816 } else if (port->port_type == TYPEC_PORT_SRC) {
2817 return SRC_UNATTACHED;
2818 }
2819
2820 return SNK_UNATTACHED;
2821 }
2822
2823 static void tcpm_check_send_discover(struct tcpm_port *port)
2824 {
2825 if (port->data_role == TYPEC_HOST && port->send_discover &&
2826 port->pd_capable) {
2827 tcpm_send_vdm(port, USB_SID_PD, CMD_DISCOVER_IDENT, NULL, 0);
2828 port->send_discover = false;
2829 }
2830 }
2831
2832 static void tcpm_swap_complete(struct tcpm_port *port, int result)
2833 {
2834 if (port->swap_pending) {
2835 port->swap_status = result;
2836 port->swap_pending = false;
2837 port->non_pd_role_swap = false;
2838 complete(&port->swap_complete);
2839 }
2840 }
2841
2842 static enum typec_pwr_opmode tcpm_get_pwr_opmode(enum typec_cc_status cc)
2843 {
2844 switch (cc) {
2845 case TYPEC_CC_RP_1_5:
2846 return TYPEC_PWR_MODE_1_5A;
2847 case TYPEC_CC_RP_3_0:
2848 return TYPEC_PWR_MODE_3_0A;
2849 case TYPEC_CC_RP_DEF:
2850 default:
2851 return TYPEC_PWR_MODE_USB;
2852 }
2853 }
2854
2855 static void run_state_machine(struct tcpm_port *port)
2856 {
2857 int ret;
2858 enum typec_pwr_opmode opmode;
2859 unsigned int msecs;
2860
2861 port->enter_state = port->state;
2862 switch (port->state) {
2863 case TOGGLING:
2864 break;
2865
2866 case SRC_UNATTACHED:
2867 if (!port->non_pd_role_swap)
2868 tcpm_swap_complete(port, -ENOTCONN);
2869 tcpm_src_detach(port);
2870 if (tcpm_start_toggling(port, tcpm_rp_cc(port))) {
2871 tcpm_set_state(port, TOGGLING, 0);
2872 break;
2873 }
2874 tcpm_set_cc(port, tcpm_rp_cc(port));
2875 if (port->port_type == TYPEC_PORT_DRP)
2876 tcpm_set_state(port, SNK_UNATTACHED, PD_T_DRP_SNK);
2877 break;
2878 case SRC_ATTACH_WAIT:
2879 if (tcpm_port_is_debug(port))
2880 tcpm_set_state(port, DEBUG_ACC_ATTACHED,
2881 PD_T_CC_DEBOUNCE);
2882 else if (tcpm_port_is_audio(port))
2883 tcpm_set_state(port, AUDIO_ACC_ATTACHED,
2884 PD_T_CC_DEBOUNCE);
2885 else if (tcpm_port_is_source(port))
2886 tcpm_set_state(port,
2887 tcpm_try_snk(port) ? SNK_TRY
2888 : SRC_ATTACHED,
2889 PD_T_CC_DEBOUNCE);
2890 break;
2891
2892 case SNK_TRY:
2893 port->try_snk_count++;
2894
2895
2896
2897
2898
2899
2900
2901
2902 tcpm_set_cc(port, TYPEC_CC_RD);
2903 tcpm_set_state(port, SNK_TRY_WAIT, PD_T_DRP_TRY);
2904 break;
2905 case SNK_TRY_WAIT:
2906 if (tcpm_port_is_sink(port)) {
2907 tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE, 0);
2908 } else {
2909 tcpm_set_state(port, SRC_TRYWAIT, 0);
2910 port->max_wait = 0;
2911 }
2912 break;
2913 case SNK_TRY_WAIT_DEBOUNCE:
2914 tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS,
2915 PD_T_PD_DEBOUNCE);
2916 break;
2917 case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS:
2918 if (port->vbus_present && tcpm_port_is_sink(port)) {
2919 tcpm_set_state(port, SNK_ATTACHED, 0);
2920 } else {
2921 tcpm_set_state(port, SRC_TRYWAIT, 0);
2922 port->max_wait = 0;
2923 }
2924 break;
2925 case SRC_TRYWAIT:
2926 tcpm_set_cc(port, tcpm_rp_cc(port));
2927 if (port->max_wait == 0) {
2928 port->max_wait = jiffies +
2929 msecs_to_jiffies(PD_T_DRP_TRY);
2930 tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED,
2931 PD_T_DRP_TRY);
2932 } else {
2933 if (time_is_after_jiffies(port->max_wait))
2934 tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED,
2935 jiffies_to_msecs(port->max_wait -
2936 jiffies));
2937 else
2938 tcpm_set_state(port, SNK_UNATTACHED, 0);
2939 }
2940 break;
2941 case SRC_TRYWAIT_DEBOUNCE:
2942 tcpm_set_state(port, SRC_ATTACHED, PD_T_CC_DEBOUNCE);
2943 break;
2944 case SRC_TRYWAIT_UNATTACHED:
2945 tcpm_set_state(port, SNK_UNATTACHED, 0);
2946 break;
2947
2948 case SRC_ATTACHED:
2949 ret = tcpm_src_attach(port);
2950 tcpm_set_state(port, SRC_UNATTACHED,
2951 ret < 0 ? 0 : PD_T_PS_SOURCE_ON);
2952 break;
2953 case SRC_STARTUP:
2954 opmode = tcpm_get_pwr_opmode(tcpm_rp_cc(port));
2955 typec_set_pwr_opmode(port->typec_port, opmode);
2956 port->pwr_opmode = TYPEC_PWR_MODE_USB;
2957 port->caps_count = 0;
2958 port->negotiated_rev = PD_MAX_REV;
2959 port->message_id = 0;
2960 port->rx_msgid = -1;
2961 port->explicit_contract = false;
2962 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
2963 break;
2964 case SRC_SEND_CAPABILITIES:
2965 port->caps_count++;
2966 if (port->caps_count > PD_N_CAPS_COUNT) {
2967 tcpm_set_state(port, SRC_READY, 0);
2968 break;
2969 }
2970 ret = tcpm_pd_send_source_caps(port);
2971 if (ret < 0) {
2972 tcpm_set_state(port, SRC_SEND_CAPABILITIES,
2973 PD_T_SEND_SOURCE_CAP);
2974 } else {
2975
2976
2977
2978
2979
2980
2981 port->caps_count = 0;
2982 port->pd_capable = true;
2983 tcpm_set_state_cond(port, SRC_SEND_CAPABILITIES_TIMEOUT,
2984 PD_T_SEND_SOURCE_CAP);
2985 }
2986 break;
2987 case SRC_SEND_CAPABILITIES_TIMEOUT:
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001 if (port->hard_reset_count < PD_N_HARD_RESET_COUNT) {
3002 tcpm_set_state(port, HARD_RESET_SEND, 0);
3003 } else if (port->negotiated_rev > PD_REV20) {
3004 port->negotiated_rev--;
3005 port->hard_reset_count = 0;
3006 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
3007 } else {
3008 tcpm_set_state(port, hard_reset_state(port), 0);
3009 }
3010 break;
3011 case SRC_NEGOTIATE_CAPABILITIES:
3012 ret = tcpm_pd_check_request(port);
3013 if (ret < 0) {
3014 tcpm_pd_send_control(port, PD_CTRL_REJECT);
3015 if (!port->explicit_contract) {
3016 tcpm_set_state(port,
3017 SRC_WAIT_NEW_CAPABILITIES, 0);
3018 } else {
3019 tcpm_set_state(port, SRC_READY, 0);
3020 }
3021 } else {
3022 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
3023 tcpm_set_state(port, SRC_TRANSITION_SUPPLY,
3024 PD_T_SRC_TRANSITION);
3025 }
3026 break;
3027 case SRC_TRANSITION_SUPPLY:
3028
3029 tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
3030 port->explicit_contract = true;
3031 typec_set_pwr_opmode(port->typec_port, TYPEC_PWR_MODE_PD);
3032 port->pwr_opmode = TYPEC_PWR_MODE_PD;
3033 tcpm_set_state_cond(port, SRC_READY, 0);
3034 break;
3035 case SRC_READY:
3036 #if 1
3037 port->hard_reset_count = 0;
3038 #endif
3039 port->try_src_count = 0;
3040
3041 tcpm_swap_complete(port, 0);
3042 tcpm_typec_connect(port);
3043
3044 tcpm_check_send_discover(port);
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058 break;
3059 case SRC_WAIT_NEW_CAPABILITIES:
3060
3061 break;
3062
3063
3064 case SNK_UNATTACHED:
3065 if (!port->non_pd_role_swap)
3066 tcpm_swap_complete(port, -ENOTCONN);
3067 tcpm_pps_complete(port, -ENOTCONN);
3068 tcpm_snk_detach(port);
3069 if (tcpm_start_toggling(port, TYPEC_CC_RD)) {
3070 tcpm_set_state(port, TOGGLING, 0);
3071 break;
3072 }
3073 tcpm_set_cc(port, TYPEC_CC_RD);
3074 if (port->port_type == TYPEC_PORT_DRP)
3075 tcpm_set_state(port, SRC_UNATTACHED, PD_T_DRP_SRC);
3076 break;
3077 case SNK_ATTACH_WAIT:
3078 if ((port->cc1 == TYPEC_CC_OPEN &&
3079 port->cc2 != TYPEC_CC_OPEN) ||
3080 (port->cc1 != TYPEC_CC_OPEN &&
3081 port->cc2 == TYPEC_CC_OPEN))
3082 tcpm_set_state(port, SNK_DEBOUNCED,
3083 PD_T_CC_DEBOUNCE);
3084 else if (tcpm_port_is_disconnected(port))
3085 tcpm_set_state(port, SNK_UNATTACHED,
3086 PD_T_PD_DEBOUNCE);
3087 break;
3088 case SNK_DEBOUNCED:
3089 if (tcpm_port_is_disconnected(port))
3090 tcpm_set_state(port, SNK_UNATTACHED,
3091 PD_T_PD_DEBOUNCE);
3092 else if (port->vbus_present)
3093 tcpm_set_state(port,
3094 tcpm_try_src(port) ? SRC_TRY
3095 : SNK_ATTACHED,
3096 0);
3097 else
3098
3099 tcpm_set_state(port, PORT_RESET, PD_T_PS_SOURCE_ON);
3100 break;
3101
3102 case SRC_TRY:
3103 port->try_src_count++;
3104 tcpm_set_cc(port, tcpm_rp_cc(port));
3105 port->max_wait = 0;
3106 tcpm_set_state(port, SRC_TRY_WAIT, 0);
3107 break;
3108 case SRC_TRY_WAIT:
3109 if (port->max_wait == 0) {
3110 port->max_wait = jiffies +
3111 msecs_to_jiffies(PD_T_DRP_TRY);
3112 msecs = PD_T_DRP_TRY;
3113 } else {
3114 if (time_is_after_jiffies(port->max_wait))
3115 msecs = jiffies_to_msecs(port->max_wait -
3116 jiffies);
3117 else
3118 msecs = 0;
3119 }
3120 tcpm_set_state(port, SNK_TRYWAIT, msecs);
3121 break;
3122 case SRC_TRY_DEBOUNCE:
3123 tcpm_set_state(port, SRC_ATTACHED, PD_T_PD_DEBOUNCE);
3124 break;
3125 case SNK_TRYWAIT:
3126 tcpm_set_cc(port, TYPEC_CC_RD);
3127 tcpm_set_state(port, SNK_TRYWAIT_VBUS, PD_T_CC_DEBOUNCE);
3128 break;
3129 case SNK_TRYWAIT_VBUS:
3130
3131
3132
3133
3134
3135 if (port->vbus_present && tcpm_port_is_sink(port)) {
3136 tcpm_set_state(port, SNK_ATTACHED, 0);
3137 break;
3138 }
3139 if (!tcpm_port_is_sink(port))
3140 tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, 0);
3141 break;
3142 case SNK_TRYWAIT_DEBOUNCE:
3143 tcpm_set_state(port, SNK_UNATTACHED, PD_T_PD_DEBOUNCE);
3144 break;
3145 case SNK_ATTACHED:
3146 ret = tcpm_snk_attach(port);
3147 if (ret < 0)
3148 tcpm_set_state(port, SNK_UNATTACHED, 0);
3149 else
3150 tcpm_set_state(port, SNK_STARTUP, 0);
3151 break;
3152 case SNK_STARTUP:
3153 opmode = tcpm_get_pwr_opmode(port->polarity ?
3154 port->cc2 : port->cc1);
3155 typec_set_pwr_opmode(port->typec_port, opmode);
3156 port->pwr_opmode = TYPEC_PWR_MODE_USB;
3157 port->negotiated_rev = PD_MAX_REV;
3158 port->message_id = 0;
3159 port->rx_msgid = -1;
3160 port->explicit_contract = false;
3161 tcpm_set_state(port, SNK_DISCOVERY, 0);
3162 break;
3163 case SNK_DISCOVERY:
3164 if (port->vbus_present) {
3165 tcpm_set_current_limit(port,
3166 tcpm_get_current_limit(port),
3167 5000);
3168 tcpm_set_charge(port, true);
3169 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
3170 break;
3171 }
3172
3173
3174
3175
3176
3177 tcpm_set_state(port, hard_reset_state(port),
3178 port->port_type == TYPEC_PORT_DRP ?
3179 PD_T_DB_DETECT : PD_T_NO_RESPONSE);
3180 break;
3181 case SNK_DISCOVERY_DEBOUNCE:
3182 tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE_DONE,
3183 PD_T_CC_DEBOUNCE);
3184 break;
3185 case SNK_DISCOVERY_DEBOUNCE_DONE:
3186 if (!tcpm_port_is_disconnected(port) &&
3187 tcpm_port_is_sink(port) &&
3188 time_is_after_jiffies(port->delayed_runtime)) {
3189 tcpm_set_state(port, SNK_DISCOVERY,
3190 jiffies_to_msecs(port->delayed_runtime -
3191 jiffies));
3192 break;
3193 }
3194 tcpm_set_state(port, unattached_state(port), 0);
3195 break;
3196 case SNK_WAIT_CAPABILITIES:
3197 ret = port->tcpc->set_pd_rx(port->tcpc, true);
3198 if (ret < 0) {
3199 tcpm_set_state(port, SNK_READY, 0);
3200 break;
3201 }
3202
3203
3204
3205
3206
3207
3208 if (port->vbus_never_low) {
3209 port->vbus_never_low = false;
3210 tcpm_set_state(port, SOFT_RESET_SEND,
3211 PD_T_SINK_WAIT_CAP);
3212 } else {
3213 tcpm_set_state(port, hard_reset_state(port),
3214 PD_T_SINK_WAIT_CAP);
3215 }
3216 break;
3217 case SNK_NEGOTIATE_CAPABILITIES:
3218 port->pd_capable = true;
3219 port->hard_reset_count = 0;
3220 ret = tcpm_pd_send_request(port);
3221 if (ret < 0) {
3222
3223 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
3224 } else {
3225 tcpm_set_state_cond(port, hard_reset_state(port),
3226 PD_T_SENDER_RESPONSE);
3227 }
3228 break;
3229 case SNK_NEGOTIATE_PPS_CAPABILITIES:
3230 ret = tcpm_pd_send_pps_request(port);
3231 if (ret < 0) {
3232 port->pps_status = ret;
3233
3234
3235
3236
3237
3238 if (port->update_sink_caps)
3239 tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
3240 else
3241 tcpm_set_state(port, SNK_READY, 0);
3242 } else {
3243 tcpm_set_state_cond(port, hard_reset_state(port),
3244 PD_T_SENDER_RESPONSE);
3245 }
3246 break;
3247 case SNK_TRANSITION_SINK:
3248 case SNK_TRANSITION_SINK_VBUS:
3249 tcpm_set_state(port, hard_reset_state(port),
3250 PD_T_PS_TRANSITION);
3251 break;
3252 case SNK_READY:
3253 port->try_snk_count = 0;
3254 port->update_sink_caps = false;
3255 if (port->explicit_contract) {
3256 typec_set_pwr_opmode(port->typec_port,
3257 TYPEC_PWR_MODE_PD);
3258 port->pwr_opmode = TYPEC_PWR_MODE_PD;
3259 }
3260
3261 tcpm_swap_complete(port, 0);
3262 tcpm_typec_connect(port);
3263 tcpm_check_send_discover(port);
3264 tcpm_pps_complete(port, port->pps_status);
3265
3266 power_supply_changed(port->psy);
3267
3268 break;
3269
3270
3271 case ACC_UNATTACHED:
3272 tcpm_acc_detach(port);
3273 tcpm_set_state(port, SRC_UNATTACHED, 0);
3274 break;
3275 case DEBUG_ACC_ATTACHED:
3276 case AUDIO_ACC_ATTACHED:
3277 ret = tcpm_acc_attach(port);
3278 if (ret < 0)
3279 tcpm_set_state(port, ACC_UNATTACHED, 0);
3280 break;
3281 case AUDIO_ACC_DEBOUNCE:
3282 tcpm_set_state(port, ACC_UNATTACHED, PD_T_CC_DEBOUNCE);
3283 break;
3284
3285
3286 case HARD_RESET_SEND:
3287 tcpm_pd_transmit(port, TCPC_TX_HARD_RESET, NULL);
3288 tcpm_set_state(port, HARD_RESET_START, 0);
3289 break;
3290 case HARD_RESET_START:
3291 port->hard_reset_count++;
3292 port->tcpc->set_pd_rx(port->tcpc, false);
3293 tcpm_unregister_altmodes(port);
3294 port->send_discover = true;
3295 if (port->pwr_role == TYPEC_SOURCE)
3296 tcpm_set_state(port, SRC_HARD_RESET_VBUS_OFF,
3297 PD_T_PS_HARD_RESET);
3298 else
3299 tcpm_set_state(port, SNK_HARD_RESET_SINK_OFF, 0);
3300 break;
3301 case SRC_HARD_RESET_VBUS_OFF:
3302 tcpm_set_vconn(port, true);
3303 tcpm_set_vbus(port, false);
3304 tcpm_set_roles(port, port->self_powered, TYPEC_SOURCE,
3305 TYPEC_HOST);
3306 tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SRC_RECOVER);
3307 break;
3308 case SRC_HARD_RESET_VBUS_ON:
3309 tcpm_set_vbus(port, true);
3310 port->tcpc->set_pd_rx(port->tcpc, true);
3311 tcpm_set_attached_state(port, true);
3312 tcpm_set_state(port, SRC_UNATTACHED, PD_T_PS_SOURCE_ON);
3313 break;
3314 case SNK_HARD_RESET_SINK_OFF:
3315 memset(&port->pps_data, 0, sizeof(port->pps_data));
3316 tcpm_set_vconn(port, false);
3317 if (port->pd_capable)
3318 tcpm_set_charge(port, false);
3319 tcpm_set_roles(port, port->self_powered, TYPEC_SINK,
3320 TYPEC_DEVICE);
3321
3322
3323
3324
3325
3326 tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, PD_T_SAFE_0V);
3327 break;
3328 case SNK_HARD_RESET_WAIT_VBUS:
3329
3330 tcpm_set_state(port, SNK_UNATTACHED,
3331 PD_T_SRC_RECOVER_MAX + PD_T_SRC_TURN_ON);
3332 break;
3333 case SNK_HARD_RESET_SINK_ON:
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350 if (port->pd_capable) {
3351 tcpm_set_current_limit(port,
3352 tcpm_get_current_limit(port),
3353 5000);
3354 tcpm_set_charge(port, true);
3355 }
3356 tcpm_set_attached_state(port, true);
3357 tcpm_set_state(port, SNK_STARTUP, 0);
3358 break;
3359
3360
3361 case SOFT_RESET:
3362 port->message_id = 0;
3363 port->rx_msgid = -1;
3364 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
3365 if (port->pwr_role == TYPEC_SOURCE)
3366 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
3367 else
3368 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
3369 break;
3370 case SOFT_RESET_SEND:
3371 port->message_id = 0;
3372 port->rx_msgid = -1;
3373 if (tcpm_pd_send_control(port, PD_CTRL_SOFT_RESET))
3374 tcpm_set_state_cond(port, hard_reset_state(port), 0);
3375 else
3376 tcpm_set_state_cond(port, hard_reset_state(port),
3377 PD_T_SENDER_RESPONSE);
3378 break;
3379
3380
3381 case DR_SWAP_SEND:
3382 tcpm_pd_send_control(port, PD_CTRL_DR_SWAP);
3383 tcpm_set_state_cond(port, DR_SWAP_SEND_TIMEOUT,
3384 PD_T_SENDER_RESPONSE);
3385 break;
3386 case DR_SWAP_ACCEPT:
3387 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
3388 tcpm_set_state_cond(port, DR_SWAP_CHANGE_DR, 0);
3389 break;
3390 case DR_SWAP_SEND_TIMEOUT:
3391 tcpm_swap_complete(port, -ETIMEDOUT);
3392 tcpm_set_state(port, ready_state(port), 0);
3393 break;
3394 case DR_SWAP_CHANGE_DR:
3395 if (port->data_role == TYPEC_HOST) {
3396 tcpm_unregister_altmodes(port);
3397 tcpm_set_roles(port, true, port->pwr_role,
3398 TYPEC_DEVICE);
3399 } else {
3400 tcpm_set_roles(port, true, port->pwr_role,
3401 TYPEC_HOST);
3402 port->send_discover = true;
3403 }
3404 tcpm_set_state(port, ready_state(port), 0);
3405 break;
3406
3407
3408 case PR_SWAP_ACCEPT:
3409 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
3410 tcpm_set_state(port, PR_SWAP_START, 0);
3411 break;
3412 case PR_SWAP_SEND:
3413 tcpm_pd_send_control(port, PD_CTRL_PR_SWAP);
3414 tcpm_set_state_cond(port, PR_SWAP_SEND_TIMEOUT,
3415 PD_T_SENDER_RESPONSE);
3416 break;
3417 case PR_SWAP_SEND_TIMEOUT:
3418 tcpm_swap_complete(port, -ETIMEDOUT);
3419 tcpm_set_state(port, ready_state(port), 0);
3420 break;
3421 case PR_SWAP_START:
3422 if (port->pwr_role == TYPEC_SOURCE)
3423 tcpm_set_state(port, PR_SWAP_SRC_SNK_TRANSITION_OFF,
3424 PD_T_SRC_TRANSITION);
3425 else
3426 tcpm_set_state(port, PR_SWAP_SNK_SRC_SINK_OFF, 0);
3427 break;
3428 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
3429 tcpm_set_vbus(port, false);
3430 port->explicit_contract = false;
3431
3432 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF,
3433 PD_T_SRCSWAPSTDBY);
3434 break;
3435 case PR_SWAP_SRC_SNK_SOURCE_OFF:
3436 tcpm_set_cc(port, TYPEC_CC_RD);
3437
3438 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED,
3439 PD_T_CC_DEBOUNCE);
3440 break;
3441 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED:
3442
3443
3444
3445
3446
3447
3448
3449 tcpm_set_pwr_role(port, TYPEC_SINK);
3450 if (tcpm_pd_send_control(port, PD_CTRL_PS_RDY)) {
3451 tcpm_set_state(port, ERROR_RECOVERY, 0);
3452 break;
3453 }
3454 tcpm_set_state_cond(port, SNK_UNATTACHED, PD_T_PS_SOURCE_ON);
3455 break;
3456 case PR_SWAP_SRC_SNK_SINK_ON:
3457 tcpm_set_state(port, SNK_STARTUP, 0);
3458 break;
3459 case PR_SWAP_SNK_SRC_SINK_OFF:
3460 tcpm_set_charge(port, false);
3461 tcpm_set_state(port, hard_reset_state(port),
3462 PD_T_PS_SOURCE_OFF);
3463 break;
3464 case PR_SWAP_SNK_SRC_SOURCE_ON:
3465 tcpm_set_cc(port, tcpm_rp_cc(port));
3466 tcpm_set_vbus(port, true);
3467
3468
3469
3470
3471
3472 tcpm_set_state(port, PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP,
3473 PD_T_NEWSRC);
3474 break;
3475 case PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP:
3476
3477
3478
3479
3480
3481
3482
3483 tcpm_set_pwr_role(port, TYPEC_SOURCE);
3484 tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
3485 tcpm_set_state(port, SRC_STARTUP, 0);
3486 break;
3487
3488 case VCONN_SWAP_ACCEPT:
3489 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
3490 tcpm_set_state(port, VCONN_SWAP_START, 0);
3491 break;
3492 case VCONN_SWAP_SEND:
3493 tcpm_pd_send_control(port, PD_CTRL_VCONN_SWAP);
3494 tcpm_set_state(port, VCONN_SWAP_SEND_TIMEOUT,
3495 PD_T_SENDER_RESPONSE);
3496 break;
3497 case VCONN_SWAP_SEND_TIMEOUT:
3498 tcpm_swap_complete(port, -ETIMEDOUT);
3499 tcpm_set_state(port, ready_state(port), 0);
3500 break;
3501 case VCONN_SWAP_START:
3502 if (port->vconn_role == TYPEC_SOURCE)
3503 tcpm_set_state(port, VCONN_SWAP_WAIT_FOR_VCONN, 0);
3504 else
3505 tcpm_set_state(port, VCONN_SWAP_TURN_ON_VCONN, 0);
3506 break;
3507 case VCONN_SWAP_WAIT_FOR_VCONN:
3508 tcpm_set_state(port, hard_reset_state(port),
3509 PD_T_VCONN_SOURCE_ON);
3510 break;
3511 case VCONN_SWAP_TURN_ON_VCONN:
3512 tcpm_set_vconn(port, true);
3513 tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
3514 tcpm_set_state(port, ready_state(port), 0);
3515 break;
3516 case VCONN_SWAP_TURN_OFF_VCONN:
3517 tcpm_set_vconn(port, false);
3518 tcpm_set_state(port, ready_state(port), 0);
3519 break;
3520
3521 case DR_SWAP_CANCEL:
3522 case PR_SWAP_CANCEL:
3523 case VCONN_SWAP_CANCEL:
3524 tcpm_swap_complete(port, port->swap_status);
3525 if (port->pwr_role == TYPEC_SOURCE)
3526 tcpm_set_state(port, SRC_READY, 0);
3527 else
3528 tcpm_set_state(port, SNK_READY, 0);
3529 break;
3530
3531 case BIST_RX:
3532 switch (BDO_MODE_MASK(port->bist_request)) {
3533 case BDO_MODE_CARRIER2:
3534 tcpm_pd_transmit(port, TCPC_TX_BIST_MODE_2, NULL);
3535 break;
3536 default:
3537 break;
3538 }
3539
3540 tcpm_set_state(port, unattached_state(port), 0);
3541 break;
3542 case GET_STATUS_SEND:
3543 tcpm_pd_send_control(port, PD_CTRL_GET_STATUS);
3544 tcpm_set_state(port, GET_STATUS_SEND_TIMEOUT,
3545 PD_T_SENDER_RESPONSE);
3546 break;
3547 case GET_STATUS_SEND_TIMEOUT:
3548 tcpm_set_state(port, ready_state(port), 0);
3549 break;
3550 case GET_PPS_STATUS_SEND:
3551 tcpm_pd_send_control(port, PD_CTRL_GET_PPS_STATUS);
3552 tcpm_set_state(port, GET_PPS_STATUS_SEND_TIMEOUT,
3553 PD_T_SENDER_RESPONSE);
3554 break;
3555 case GET_PPS_STATUS_SEND_TIMEOUT:
3556 tcpm_set_state(port, ready_state(port), 0);
3557 break;
3558 case ERROR_RECOVERY:
3559 tcpm_swap_complete(port, -EPROTO);
3560 tcpm_pps_complete(port, -EPROTO);
3561 tcpm_set_state(port, PORT_RESET, 0);
3562 break;
3563 case PORT_RESET:
3564 tcpm_reset_port(port);
3565 tcpm_set_cc(port, TYPEC_CC_OPEN);
3566 tcpm_set_state(port, PORT_RESET_WAIT_OFF,
3567 PD_T_ERROR_RECOVERY);
3568 break;
3569 case PORT_RESET_WAIT_OFF:
3570 tcpm_set_state(port,
3571 tcpm_default_state(port),
3572 port->vbus_present ? PD_T_PS_SOURCE_OFF : 0);
3573 break;
3574 default:
3575 WARN(1, "Unexpected port state %d\n", port->state);
3576 break;
3577 }
3578 }
3579
3580 static void tcpm_state_machine_work(struct work_struct *work)
3581 {
3582 struct tcpm_port *port = container_of(work, struct tcpm_port,
3583 state_machine.work);
3584 enum tcpm_state prev_state;
3585
3586 mutex_lock(&port->lock);
3587 port->state_machine_running = true;
3588
3589 if (port->queued_message && tcpm_send_queued_message(port))
3590 goto done;
3591
3592
3593 if (port->delayed_state) {
3594 tcpm_log(port, "state change %s -> %s [delayed %ld ms]",
3595 tcpm_states[port->state],
3596 tcpm_states[port->delayed_state], port->delay_ms);
3597 port->prev_state = port->state;
3598 port->state = port->delayed_state;
3599 port->delayed_state = INVALID_STATE;
3600 }
3601
3602
3603
3604
3605
3606 do {
3607 prev_state = port->state;
3608 run_state_machine(port);
3609 if (port->queued_message)
3610 tcpm_send_queued_message(port);
3611 } while (port->state != prev_state && !port->delayed_state);
3612
3613 done:
3614 port->state_machine_running = false;
3615 mutex_unlock(&port->lock);
3616 }
3617
3618 static void _tcpm_cc_change(struct tcpm_port *port, enum typec_cc_status cc1,
3619 enum typec_cc_status cc2)
3620 {
3621 enum typec_cc_status old_cc1, old_cc2;
3622 enum tcpm_state new_state;
3623
3624 old_cc1 = port->cc1;
3625 old_cc2 = port->cc2;
3626 port->cc1 = cc1;
3627 port->cc2 = cc2;
3628
3629 tcpm_log_force(port,
3630 "CC1: %u -> %u, CC2: %u -> %u [state %s, polarity %d, %s]",
3631 old_cc1, cc1, old_cc2, cc2, tcpm_states[port->state],
3632 port->polarity,
3633 tcpm_port_is_disconnected(port) ? "disconnected"
3634 : "connected");
3635
3636 switch (port->state) {
3637 case TOGGLING:
3638 if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) ||
3639 tcpm_port_is_source(port))
3640 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
3641 else if (tcpm_port_is_sink(port))
3642 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
3643 break;
3644 case SRC_UNATTACHED:
3645 case ACC_UNATTACHED:
3646 if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) ||
3647 tcpm_port_is_source(port))
3648 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
3649 break;
3650 case SRC_ATTACH_WAIT:
3651 if (tcpm_port_is_disconnected(port) ||
3652 tcpm_port_is_audio_detached(port))
3653 tcpm_set_state(port, SRC_UNATTACHED, 0);
3654 else if (cc1 != old_cc1 || cc2 != old_cc2)
3655 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
3656 break;
3657 case SRC_ATTACHED:
3658 case SRC_SEND_CAPABILITIES:
3659 case SRC_READY:
3660 if (tcpm_port_is_disconnected(port) ||
3661 !tcpm_port_is_source(port))
3662 tcpm_set_state(port, SRC_UNATTACHED, 0);
3663 break;
3664 case SNK_UNATTACHED:
3665 if (tcpm_port_is_sink(port))
3666 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
3667 break;
3668 case SNK_ATTACH_WAIT:
3669 if ((port->cc1 == TYPEC_CC_OPEN &&
3670 port->cc2 != TYPEC_CC_OPEN) ||
3671 (port->cc1 != TYPEC_CC_OPEN &&
3672 port->cc2 == TYPEC_CC_OPEN))
3673 new_state = SNK_DEBOUNCED;
3674 else if (tcpm_port_is_disconnected(port))
3675 new_state = SNK_UNATTACHED;
3676 else
3677 break;
3678 if (new_state != port->delayed_state)
3679 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
3680 break;
3681 case SNK_DEBOUNCED:
3682 if (tcpm_port_is_disconnected(port))
3683 new_state = SNK_UNATTACHED;
3684 else if (port->vbus_present)
3685 new_state = tcpm_try_src(port) ? SRC_TRY : SNK_ATTACHED;
3686 else
3687 new_state = SNK_UNATTACHED;
3688 if (new_state != port->delayed_state)
3689 tcpm_set_state(port, SNK_DEBOUNCED, 0);
3690 break;
3691 case SNK_READY:
3692 if (tcpm_port_is_disconnected(port))
3693 tcpm_set_state(port, unattached_state(port), 0);
3694 else if (!port->pd_capable &&
3695 (cc1 != old_cc1 || cc2 != old_cc2))
3696 tcpm_set_current_limit(port,
3697 tcpm_get_current_limit(port),
3698 5000);
3699 break;
3700
3701 case AUDIO_ACC_ATTACHED:
3702 if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN)
3703 tcpm_set_state(port, AUDIO_ACC_DEBOUNCE, 0);
3704 break;
3705 case AUDIO_ACC_DEBOUNCE:
3706 if (tcpm_port_is_audio(port))
3707 tcpm_set_state(port, AUDIO_ACC_ATTACHED, 0);
3708 break;
3709
3710 case DEBUG_ACC_ATTACHED:
3711 if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN)
3712 tcpm_set_state(port, ACC_UNATTACHED, 0);
3713 break;
3714
3715 case SNK_TRY:
3716
3717 break;
3718
3719 case SNK_DISCOVERY:
3720
3721 if (tcpm_port_is_disconnected(port))
3722 tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE, 0);
3723 break;
3724 case SNK_DISCOVERY_DEBOUNCE:
3725 break;
3726
3727 case SRC_TRYWAIT:
3728
3729 if (!port->vbus_present && tcpm_port_is_source(port))
3730 tcpm_set_state(port, SRC_TRYWAIT_DEBOUNCE, 0);
3731 break;
3732 case SRC_TRYWAIT_DEBOUNCE:
3733 if (port->vbus_present || !tcpm_port_is_source(port))
3734 tcpm_set_state(port, SRC_TRYWAIT, 0);
3735 break;
3736 case SNK_TRY_WAIT_DEBOUNCE:
3737 if (!tcpm_port_is_sink(port)) {
3738 port->max_wait = 0;
3739 tcpm_set_state(port, SRC_TRYWAIT, 0);
3740 }
3741 break;
3742 case SRC_TRY_WAIT:
3743 if (tcpm_port_is_source(port))
3744 tcpm_set_state(port, SRC_TRY_DEBOUNCE, 0);
3745 break;
3746 case SRC_TRY_DEBOUNCE:
3747 tcpm_set_state(port, SRC_TRY_WAIT, 0);
3748 break;
3749 case SNK_TRYWAIT_DEBOUNCE:
3750 if (tcpm_port_is_sink(port))
3751 tcpm_set_state(port, SNK_TRYWAIT_VBUS, 0);
3752 break;
3753 case SNK_TRYWAIT_VBUS:
3754 if (!tcpm_port_is_sink(port))
3755 tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, 0);
3756 break;
3757 case SNK_TRYWAIT:
3758
3759 break;
3760 case PR_SWAP_SNK_SRC_SINK_OFF:
3761 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
3762 case PR_SWAP_SRC_SNK_SOURCE_OFF:
3763 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED:
3764 case PR_SWAP_SNK_SRC_SOURCE_ON:
3765
3766
3767
3768
3769 break;
3770
3771 case PORT_RESET:
3772 case PORT_RESET_WAIT_OFF:
3773
3774
3775
3776
3777 break;
3778
3779 default:
3780 if (tcpm_port_is_disconnected(port))
3781 tcpm_set_state(port, unattached_state(port), 0);
3782 break;
3783 }
3784 }
3785
3786 static void _tcpm_pd_vbus_on(struct tcpm_port *port)
3787 {
3788 tcpm_log_force(port, "VBUS on");
3789 port->vbus_present = true;
3790 switch (port->state) {
3791 case SNK_TRANSITION_SINK_VBUS:
3792 port->explicit_contract = true;
3793 tcpm_set_state(port, SNK_READY, 0);
3794 break;
3795 case SNK_DISCOVERY:
3796 tcpm_set_state(port, SNK_DISCOVERY, 0);
3797 break;
3798
3799 case SNK_DEBOUNCED:
3800 tcpm_set_state(port, tcpm_try_src(port) ? SRC_TRY
3801 : SNK_ATTACHED,
3802 0);
3803 break;
3804 case SNK_HARD_RESET_WAIT_VBUS:
3805 tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, 0);
3806 break;
3807 case SRC_ATTACHED:
3808 tcpm_set_state(port, SRC_STARTUP, 0);
3809 break;
3810 case SRC_HARD_RESET_VBUS_ON:
3811 tcpm_set_state(port, SRC_STARTUP, 0);
3812 break;
3813
3814 case SNK_TRY:
3815
3816 break;
3817 case SRC_TRYWAIT:
3818
3819 break;
3820 case SRC_TRYWAIT_DEBOUNCE:
3821 tcpm_set_state(port, SRC_TRYWAIT, 0);
3822 break;
3823 case SNK_TRY_WAIT_DEBOUNCE:
3824
3825 break;
3826 case SNK_TRYWAIT:
3827
3828 break;
3829 case SNK_TRYWAIT_VBUS:
3830 if (tcpm_port_is_sink(port))
3831 tcpm_set_state(port, SNK_ATTACHED, 0);
3832 break;
3833 case SNK_TRYWAIT_DEBOUNCE:
3834
3835 break;
3836 case SRC_TRY_WAIT:
3837 case SRC_TRY_DEBOUNCE:
3838
3839 break;
3840
3841 case PORT_RESET:
3842 case PORT_RESET_WAIT_OFF:
3843
3844
3845
3846
3847 break;
3848
3849 default:
3850 break;
3851 }
3852 }
3853
3854 static void _tcpm_pd_vbus_off(struct tcpm_port *port)
3855 {
3856 tcpm_log_force(port, "VBUS off");
3857 port->vbus_present = false;
3858 port->vbus_never_low = false;
3859 switch (port->state) {
3860 case SNK_HARD_RESET_SINK_OFF:
3861 tcpm_set_state(port, SNK_HARD_RESET_WAIT_VBUS, 0);
3862 break;
3863 case SRC_HARD_RESET_VBUS_OFF:
3864 tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, 0);
3865 break;
3866 case HARD_RESET_SEND:
3867 break;
3868
3869 case SNK_TRY:
3870
3871 break;
3872 case SRC_TRYWAIT:
3873
3874 if (tcpm_port_is_source(port))
3875 tcpm_set_state(port, SRC_TRYWAIT_DEBOUNCE, 0);
3876 break;
3877 case SNK_TRY_WAIT_DEBOUNCE:
3878
3879 break;
3880 case SNK_TRYWAIT:
3881 case SNK_TRYWAIT_VBUS:
3882 case SNK_TRYWAIT_DEBOUNCE:
3883 break;
3884 case SNK_ATTACH_WAIT:
3885 tcpm_set_state(port, SNK_UNATTACHED, 0);
3886 break;
3887
3888 case SNK_NEGOTIATE_CAPABILITIES:
3889 break;
3890
3891 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
3892 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF, 0);
3893 break;
3894
3895 case PR_SWAP_SNK_SRC_SINK_OFF:
3896
3897 break;
3898
3899 case PORT_RESET_WAIT_OFF:
3900 tcpm_set_state(port, tcpm_default_state(port), 0);
3901 break;
3902
3903 case SRC_TRY_WAIT:
3904 case SRC_TRY_DEBOUNCE:
3905
3906 break;
3907
3908 case PORT_RESET:
3909
3910
3911
3912
3913 break;
3914
3915 default:
3916 if (port->pwr_role == TYPEC_SINK &&
3917 port->attached)
3918 tcpm_set_state(port, SNK_UNATTACHED, 0);
3919 break;
3920 }
3921 }
3922
3923 static void _tcpm_pd_hard_reset(struct tcpm_port *port)
3924 {
3925 tcpm_log_force(port, "Received hard reset");
3926
3927
3928
3929
3930 tcpm_set_state(port,
3931 port->hard_reset_count < PD_N_HARD_RESET_COUNT ?
3932 HARD_RESET_START : ERROR_RECOVERY,
3933 0);
3934 }
3935
3936 static void tcpm_pd_event_handler(struct work_struct *work)
3937 {
3938 struct tcpm_port *port = container_of(work, struct tcpm_port,
3939 event_work);
3940 u32 events;
3941
3942 mutex_lock(&port->lock);
3943
3944 spin_lock(&port->pd_event_lock);
3945 while (port->pd_events) {
3946 events = port->pd_events;
3947 port->pd_events = 0;
3948 spin_unlock(&port->pd_event_lock);
3949 if (events & TCPM_RESET_EVENT)
3950 _tcpm_pd_hard_reset(port);
3951 if (events & TCPM_VBUS_EVENT) {
3952 bool vbus;
3953
3954 vbus = port->tcpc->get_vbus(port->tcpc);
3955 if (vbus)
3956 _tcpm_pd_vbus_on(port);
3957 else
3958 _tcpm_pd_vbus_off(port);
3959 }
3960 if (events & TCPM_CC_EVENT) {
3961 enum typec_cc_status cc1, cc2;
3962
3963 if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0)
3964 _tcpm_cc_change(port, cc1, cc2);
3965 }
3966 spin_lock(&port->pd_event_lock);
3967 }
3968 spin_unlock(&port->pd_event_lock);
3969 mutex_unlock(&port->lock);
3970 }
3971
3972 void tcpm_cc_change(struct tcpm_port *port)
3973 {
3974 spin_lock(&port->pd_event_lock);
3975 port->pd_events |= TCPM_CC_EVENT;
3976 spin_unlock(&port->pd_event_lock);
3977 queue_work(port->wq, &port->event_work);
3978 }
3979 EXPORT_SYMBOL_GPL(tcpm_cc_change);
3980
3981 void tcpm_vbus_change(struct tcpm_port *port)
3982 {
3983 spin_lock(&port->pd_event_lock);
3984 port->pd_events |= TCPM_VBUS_EVENT;
3985 spin_unlock(&port->pd_event_lock);
3986 queue_work(port->wq, &port->event_work);
3987 }
3988 EXPORT_SYMBOL_GPL(tcpm_vbus_change);
3989
3990 void tcpm_pd_hard_reset(struct tcpm_port *port)
3991 {
3992 spin_lock(&port->pd_event_lock);
3993 port->pd_events = TCPM_RESET_EVENT;
3994 spin_unlock(&port->pd_event_lock);
3995 queue_work(port->wq, &port->event_work);
3996 }
3997 EXPORT_SYMBOL_GPL(tcpm_pd_hard_reset);
3998
3999 static int tcpm_dr_set(const struct typec_capability *cap,
4000 enum typec_data_role data)
4001 {
4002 struct tcpm_port *port = typec_cap_to_tcpm(cap);
4003 int ret;
4004
4005 mutex_lock(&port->swap_lock);
4006 mutex_lock(&port->lock);
4007
4008 if (port->port_type != TYPEC_PORT_DRP) {
4009 ret = -EINVAL;
4010 goto port_unlock;
4011 }
4012 if (port->state != SRC_READY && port->state != SNK_READY) {
4013 ret = -EAGAIN;
4014 goto port_unlock;
4015 }
4016
4017 if (port->data_role == data) {
4018 ret = 0;
4019 goto port_unlock;
4020 }
4021
4022
4023
4024
4025
4026
4027
4028
4029 if (!port->pd_capable) {
4030
4031
4032
4033
4034
4035 if (port->try_role == TYPEC_NO_PREFERRED_ROLE ||
4036 port->try_role == port->pwr_role) {
4037 ret = -EINVAL;
4038 goto port_unlock;
4039 }
4040 port->non_pd_role_swap = true;
4041 tcpm_set_state(port, PORT_RESET, 0);
4042 } else {
4043 tcpm_set_state(port, DR_SWAP_SEND, 0);
4044 }
4045
4046 port->swap_status = 0;
4047 port->swap_pending = true;
4048 reinit_completion(&port->swap_complete);
4049 mutex_unlock(&port->lock);
4050
4051 if (!wait_for_completion_timeout(&port->swap_complete,
4052 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT)))
4053 ret = -ETIMEDOUT;
4054 else
4055 ret = port->swap_status;
4056
4057 port->non_pd_role_swap = false;
4058 goto swap_unlock;
4059
4060 port_unlock:
4061 mutex_unlock(&port->lock);
4062 swap_unlock:
4063 mutex_unlock(&port->swap_lock);
4064 return ret;
4065 }
4066
4067 static int tcpm_pr_set(const struct typec_capability *cap,
4068 enum typec_role role)
4069 {
4070 struct tcpm_port *port = typec_cap_to_tcpm(cap);
4071 int ret;
4072
4073 mutex_lock(&port->swap_lock);
4074 mutex_lock(&port->lock);
4075
4076 if (port->port_type != TYPEC_PORT_DRP) {
4077 ret = -EINVAL;
4078 goto port_unlock;
4079 }
4080 if (port->state != SRC_READY && port->state != SNK_READY) {
4081 ret = -EAGAIN;
4082 goto port_unlock;
4083 }
4084
4085 if (role == port->pwr_role) {
4086 ret = 0;
4087 goto port_unlock;
4088 }
4089
4090 port->swap_status = 0;
4091 port->swap_pending = true;
4092 reinit_completion(&port->swap_complete);
4093 tcpm_set_state(port, PR_SWAP_SEND, 0);
4094 mutex_unlock(&port->lock);
4095
4096 if (!wait_for_completion_timeout(&port->swap_complete,
4097 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT)))
4098 ret = -ETIMEDOUT;
4099 else
4100 ret = port->swap_status;
4101
4102 goto swap_unlock;
4103
4104 port_unlock:
4105 mutex_unlock(&port->lock);
4106 swap_unlock:
4107 mutex_unlock(&port->swap_lock);
4108 return ret;
4109 }
4110
4111 static int tcpm_vconn_set(const struct typec_capability *cap,
4112 enum typec_role role)
4113 {
4114 struct tcpm_port *port = typec_cap_to_tcpm(cap);
4115 int ret;
4116
4117 mutex_lock(&port->swap_lock);
4118 mutex_lock(&port->lock);
4119
4120 if (port->state != SRC_READY && port->state != SNK_READY) {
4121 ret = -EAGAIN;
4122 goto port_unlock;
4123 }
4124
4125 if (role == port->vconn_role) {
4126 ret = 0;
4127 goto port_unlock;
4128 }
4129
4130 port->swap_status = 0;
4131 port->swap_pending = true;
4132 reinit_completion(&port->swap_complete);
4133 tcpm_set_state(port, VCONN_SWAP_SEND, 0);
4134 mutex_unlock(&port->lock);
4135
4136 if (!wait_for_completion_timeout(&port->swap_complete,
4137 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT)))
4138 ret = -ETIMEDOUT;
4139 else
4140 ret = port->swap_status;
4141
4142 goto swap_unlock;
4143
4144 port_unlock:
4145 mutex_unlock(&port->lock);
4146 swap_unlock:
4147 mutex_unlock(&port->swap_lock);
4148 return ret;
4149 }
4150
4151 static int tcpm_try_role(const struct typec_capability *cap, int role)
4152 {
4153 struct tcpm_port *port = typec_cap_to_tcpm(cap);
4154 struct tcpc_dev *tcpc = port->tcpc;
4155 int ret = 0;
4156
4157 mutex_lock(&port->lock);
4158 if (tcpc->try_role)
4159 ret = tcpc->try_role(tcpc, role);
4160 if (!ret && (!tcpc->config || !tcpc->config->try_role_hw))
4161 port->try_role = role;
4162 port->try_src_count = 0;
4163 port->try_snk_count = 0;
4164 mutex_unlock(&port->lock);
4165
4166 return ret;
4167 }
4168
4169 static int tcpm_pps_set_op_curr(struct tcpm_port *port, u16 op_curr)
4170 {
4171 unsigned int target_mw;
4172 int ret;
4173
4174 mutex_lock(&port->swap_lock);
4175 mutex_lock(&port->lock);
4176
4177 if (!port->pps_data.active) {
4178 ret = -EOPNOTSUPP;
4179 goto port_unlock;
4180 }
4181
4182 if (port->state != SNK_READY) {
4183 ret = -EAGAIN;
4184 goto port_unlock;
4185 }
4186
4187 if (op_curr > port->pps_data.max_curr) {
4188 ret = -EINVAL;
4189 goto port_unlock;
4190 }
4191
4192 target_mw = (op_curr * port->pps_data.out_volt) / 1000;
4193 if (target_mw < port->operating_snk_mw) {
4194 ret = -EINVAL;
4195 goto port_unlock;
4196 }
4197
4198
4199 op_curr = op_curr - (op_curr % RDO_PROG_CURR_MA_STEP);
4200
4201 reinit_completion(&port->pps_complete);
4202 port->pps_data.op_curr = op_curr;
4203 port->pps_status = 0;
4204 port->pps_pending = true;
4205 tcpm_set_state(port, SNK_NEGOTIATE_PPS_CAPABILITIES, 0);
4206 mutex_unlock(&port->lock);
4207
4208 if (!wait_for_completion_timeout(&port->pps_complete,
4209 msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT)))
4210 ret = -ETIMEDOUT;
4211 else
4212 ret = port->pps_status;
4213
4214 goto swap_unlock;
4215
4216 port_unlock:
4217 mutex_unlock(&port->lock);
4218 swap_unlock:
4219 mutex_unlock(&port->swap_lock);
4220
4221 return ret;
4222 }
4223
4224 static int tcpm_pps_set_out_volt(struct tcpm_port *port, u16 out_volt)
4225 {
4226 unsigned int target_mw;
4227 int ret;
4228
4229 mutex_lock(&port->swap_lock);
4230 mutex_lock(&port->lock);
4231
4232 if (!port->pps_data.active) {
4233 ret = -EOPNOTSUPP;
4234 goto port_unlock;
4235 }
4236
4237 if (port->state != SNK_READY) {
4238 ret = -EAGAIN;
4239 goto port_unlock;
4240 }
4241
4242 if (out_volt < port->pps_data.min_volt ||
4243 out_volt > port->pps_data.max_volt) {
4244 ret = -EINVAL;
4245 goto port_unlock;
4246 }
4247
4248 target_mw = (port->pps_data.op_curr * out_volt) / 1000;
4249 if (target_mw < port->operating_snk_mw) {
4250 ret = -EINVAL;
4251 goto port_unlock;
4252 }
4253
4254
4255 out_volt = out_volt - (out_volt % RDO_PROG_VOLT_MV_STEP);
4256
4257 reinit_completion(&port->pps_complete);
4258 port->pps_data.out_volt = out_volt;
4259 port->pps_status = 0;
4260 port->pps_pending = true;
4261 tcpm_set_state(port, SNK_NEGOTIATE_PPS_CAPABILITIES, 0);
4262 mutex_unlock(&port->lock);
4263
4264 if (!wait_for_completion_timeout(&port->pps_complete,
4265 msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT)))
4266 ret = -ETIMEDOUT;
4267 else
4268 ret = port->pps_status;
4269
4270 goto swap_unlock;
4271
4272 port_unlock:
4273 mutex_unlock(&port->lock);
4274 swap_unlock:
4275 mutex_unlock(&port->swap_lock);
4276
4277 return ret;
4278 }
4279
4280 static int tcpm_pps_activate(struct tcpm_port *port, bool activate)
4281 {
4282 int ret = 0;
4283
4284 mutex_lock(&port->swap_lock);
4285 mutex_lock(&port->lock);
4286
4287 if (!port->pps_data.supported) {
4288 ret = -EOPNOTSUPP;
4289 goto port_unlock;
4290 }
4291
4292
4293 if (!port->pps_data.active && !activate)
4294 goto port_unlock;
4295
4296 if (port->state != SNK_READY) {
4297 ret = -EAGAIN;
4298 goto port_unlock;
4299 }
4300
4301 reinit_completion(&port->pps_complete);
4302 port->pps_status = 0;
4303 port->pps_pending = true;
4304
4305
4306 if (activate) {
4307 port->pps_data.out_volt = port->supply_voltage;
4308 port->pps_data.op_curr = port->current_limit;
4309 tcpm_set_state(port, SNK_NEGOTIATE_PPS_CAPABILITIES, 0);
4310 } else {
4311 tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
4312 }
4313 mutex_unlock(&port->lock);
4314
4315 if (!wait_for_completion_timeout(&port->pps_complete,
4316 msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT)))
4317 ret = -ETIMEDOUT;
4318 else
4319 ret = port->pps_status;
4320
4321 goto swap_unlock;
4322
4323 port_unlock:
4324 mutex_unlock(&port->lock);
4325 swap_unlock:
4326 mutex_unlock(&port->swap_lock);
4327
4328 return ret;
4329 }
4330
4331 static void tcpm_init(struct tcpm_port *port)
4332 {
4333 enum typec_cc_status cc1, cc2;
4334
4335 port->tcpc->init(port->tcpc);
4336
4337 tcpm_reset_port(port);
4338
4339
4340
4341
4342
4343
4344 port->vbus_present = port->tcpc->get_vbus(port->tcpc);
4345 if (port->vbus_present)
4346 port->vbus_never_low = true;
4347
4348 tcpm_set_state(port, tcpm_default_state(port), 0);
4349
4350 if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0)
4351 _tcpm_cc_change(port, cc1, cc2);
4352
4353
4354
4355
4356
4357 tcpm_set_state(port, PORT_RESET, 0);
4358 }
4359
4360 static int tcpm_port_type_set(const struct typec_capability *cap,
4361 enum typec_port_type type)
4362 {
4363 struct tcpm_port *port = typec_cap_to_tcpm(cap);
4364
4365 mutex_lock(&port->lock);
4366 if (type == port->port_type)
4367 goto port_unlock;
4368
4369 port->port_type = type;
4370
4371 if (!port->connected) {
4372 tcpm_set_state(port, PORT_RESET, 0);
4373 } else if (type == TYPEC_PORT_SNK) {
4374 if (!(port->pwr_role == TYPEC_SINK &&
4375 port->data_role == TYPEC_DEVICE))
4376 tcpm_set_state(port, PORT_RESET, 0);
4377 } else if (type == TYPEC_PORT_SRC) {
4378 if (!(port->pwr_role == TYPEC_SOURCE &&
4379 port->data_role == TYPEC_HOST))
4380 tcpm_set_state(port, PORT_RESET, 0);
4381 }
4382
4383 port_unlock:
4384 mutex_unlock(&port->lock);
4385 return 0;
4386 }
4387
4388 void tcpm_tcpc_reset(struct tcpm_port *port)
4389 {
4390 mutex_lock(&port->lock);
4391
4392 tcpm_init(port);
4393 mutex_unlock(&port->lock);
4394 }
4395 EXPORT_SYMBOL_GPL(tcpm_tcpc_reset);
4396
4397 static int tcpm_copy_pdos(u32 *dest_pdo, const u32 *src_pdo,
4398 unsigned int nr_pdo)
4399 {
4400 unsigned int i;
4401
4402 if (nr_pdo > PDO_MAX_OBJECTS)
4403 nr_pdo = PDO_MAX_OBJECTS;
4404
4405 for (i = 0; i < nr_pdo; i++)
4406 dest_pdo[i] = src_pdo[i];
4407
4408 return nr_pdo;
4409 }
4410
4411 static int tcpm_copy_vdos(u32 *dest_vdo, const u32 *src_vdo,
4412 unsigned int nr_vdo)
4413 {
4414 unsigned int i;
4415
4416 if (nr_vdo > VDO_MAX_OBJECTS)
4417 nr_vdo = VDO_MAX_OBJECTS;
4418
4419 for (i = 0; i < nr_vdo; i++)
4420 dest_vdo[i] = src_vdo[i];
4421
4422 return nr_vdo;
4423 }
4424
4425 static int tcpm_fw_get_caps(struct tcpm_port *port,
4426 struct fwnode_handle *fwnode)
4427 {
4428 const char *cap_str;
4429 int ret;
4430 u32 mw;
4431
4432 if (!fwnode)
4433 return -EINVAL;
4434
4435
4436 ret = fwnode_property_read_string(fwnode, "data-role", &cap_str);
4437 if (ret == 0) {
4438 ret = typec_find_port_data_role(cap_str);
4439 if (ret < 0)
4440 return ret;
4441 port->typec_caps.data = ret;
4442 }
4443
4444 ret = fwnode_property_read_string(fwnode, "power-role", &cap_str);
4445 if (ret < 0)
4446 return ret;
4447
4448 ret = typec_find_port_power_role(cap_str);
4449 if (ret < 0)
4450 return ret;
4451 port->typec_caps.type = ret;
4452 port->port_type = port->typec_caps.type;
4453
4454 if (port->port_type == TYPEC_PORT_SNK)
4455 goto sink;
4456
4457
4458 ret = fwnode_property_count_u32(fwnode, "source-pdos");
4459 if (ret <= 0)
4460 return -EINVAL;
4461
4462 port->nr_src_pdo = min(ret, PDO_MAX_OBJECTS);
4463 ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
4464 port->src_pdo, port->nr_src_pdo);
4465 if ((ret < 0) || tcpm_validate_caps(port, port->src_pdo,
4466 port->nr_src_pdo))
4467 return -EINVAL;
4468
4469 if (port->port_type == TYPEC_PORT_SRC)
4470 return 0;
4471
4472
4473 ret = fwnode_property_read_string(fwnode, "try-power-role", &cap_str);
4474 if (ret < 0)
4475 return ret;
4476
4477 port->typec_caps.prefer_role = typec_find_power_role(cap_str);
4478 if (port->typec_caps.prefer_role < 0)
4479 return -EINVAL;
4480 sink:
4481
4482 ret = fwnode_property_count_u32(fwnode, "sink-pdos");
4483 if (ret <= 0)
4484 return -EINVAL;
4485
4486 port->nr_snk_pdo = min(ret, PDO_MAX_OBJECTS);
4487 ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
4488 port->snk_pdo, port->nr_snk_pdo);
4489 if ((ret < 0) || tcpm_validate_caps(port, port->snk_pdo,
4490 port->nr_snk_pdo))
4491 return -EINVAL;
4492
4493 if (fwnode_property_read_u32(fwnode, "op-sink-microwatt", &mw) < 0)
4494 return -EINVAL;
4495 port->operating_snk_mw = mw / 1000;
4496
4497 port->self_powered = fwnode_property_read_bool(fwnode, "self-powered");
4498
4499 return 0;
4500 }
4501
4502
4503 enum tcpm_psy_online_states {
4504 TCPM_PSY_OFFLINE = 0,
4505 TCPM_PSY_FIXED_ONLINE,
4506 TCPM_PSY_PROG_ONLINE,
4507 };
4508
4509 static enum power_supply_property tcpm_psy_props[] = {
4510 POWER_SUPPLY_PROP_USB_TYPE,
4511 POWER_SUPPLY_PROP_ONLINE,
4512 POWER_SUPPLY_PROP_VOLTAGE_MIN,
4513 POWER_SUPPLY_PROP_VOLTAGE_MAX,
4514 POWER_SUPPLY_PROP_VOLTAGE_NOW,
4515 POWER_SUPPLY_PROP_CURRENT_MAX,
4516 POWER_SUPPLY_PROP_CURRENT_NOW,
4517 };
4518
4519 static int tcpm_psy_get_online(struct tcpm_port *port,
4520 union power_supply_propval *val)
4521 {
4522 if (port->vbus_charge) {
4523 if (port->pps_data.active)
4524 val->intval = TCPM_PSY_PROG_ONLINE;
4525 else
4526 val->intval = TCPM_PSY_FIXED_ONLINE;
4527 } else {
4528 val->intval = TCPM_PSY_OFFLINE;
4529 }
4530
4531 return 0;
4532 }
4533
4534 static int tcpm_psy_get_voltage_min(struct tcpm_port *port,
4535 union power_supply_propval *val)
4536 {
4537 if (port->pps_data.active)
4538 val->intval = port->pps_data.min_volt * 1000;
4539 else
4540 val->intval = port->supply_voltage * 1000;
4541
4542 return 0;
4543 }
4544
4545 static int tcpm_psy_get_voltage_max(struct tcpm_port *port,
4546 union power_supply_propval *val)
4547 {
4548 if (port->pps_data.active)
4549 val->intval = port->pps_data.max_volt * 1000;
4550 else
4551 val->intval = port->supply_voltage * 1000;
4552
4553 return 0;
4554 }
4555
4556 static int tcpm_psy_get_voltage_now(struct tcpm_port *port,
4557 union power_supply_propval *val)
4558 {
4559 val->intval = port->supply_voltage * 1000;
4560
4561 return 0;
4562 }
4563
4564 static int tcpm_psy_get_current_max(struct tcpm_port *port,
4565 union power_supply_propval *val)
4566 {
4567 if (port->pps_data.active)
4568 val->intval = port->pps_data.max_curr * 1000;
4569 else
4570 val->intval = port->current_limit * 1000;
4571
4572 return 0;
4573 }
4574
4575 static int tcpm_psy_get_current_now(struct tcpm_port *port,
4576 union power_supply_propval *val)
4577 {
4578 val->intval = port->current_limit * 1000;
4579
4580 return 0;
4581 }
4582
4583 static int tcpm_psy_get_prop(struct power_supply *psy,
4584 enum power_supply_property psp,
4585 union power_supply_propval *val)
4586 {
4587 struct tcpm_port *port = power_supply_get_drvdata(psy);
4588 int ret = 0;
4589
4590 switch (psp) {
4591 case POWER_SUPPLY_PROP_USB_TYPE:
4592 val->intval = port->usb_type;
4593 break;
4594 case POWER_SUPPLY_PROP_ONLINE:
4595 ret = tcpm_psy_get_online(port, val);
4596 break;
4597 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
4598 ret = tcpm_psy_get_voltage_min(port, val);
4599 break;
4600 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
4601 ret = tcpm_psy_get_voltage_max(port, val);
4602 break;
4603 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
4604 ret = tcpm_psy_get_voltage_now(port, val);
4605 break;
4606 case POWER_SUPPLY_PROP_CURRENT_MAX:
4607 ret = tcpm_psy_get_current_max(port, val);
4608 break;
4609 case POWER_SUPPLY_PROP_CURRENT_NOW:
4610 ret = tcpm_psy_get_current_now(port, val);
4611 break;
4612 default:
4613 ret = -EINVAL;
4614 break;
4615 }
4616
4617 return ret;
4618 }
4619
4620 static int tcpm_psy_set_online(struct tcpm_port *port,
4621 const union power_supply_propval *val)
4622 {
4623 int ret;
4624
4625 switch (val->intval) {
4626 case TCPM_PSY_FIXED_ONLINE:
4627 ret = tcpm_pps_activate(port, false);
4628 break;
4629 case TCPM_PSY_PROG_ONLINE:
4630 ret = tcpm_pps_activate(port, true);
4631 break;
4632 default:
4633 ret = -EINVAL;
4634 break;
4635 }
4636
4637 return ret;
4638 }
4639
4640 static int tcpm_psy_set_prop(struct power_supply *psy,
4641 enum power_supply_property psp,
4642 const union power_supply_propval *val)
4643 {
4644 struct tcpm_port *port = power_supply_get_drvdata(psy);
4645 int ret;
4646
4647 switch (psp) {
4648 case POWER_SUPPLY_PROP_ONLINE:
4649 ret = tcpm_psy_set_online(port, val);
4650 break;
4651 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
4652 if (val->intval < port->pps_data.min_volt * 1000 ||
4653 val->intval > port->pps_data.max_volt * 1000)
4654 ret = -EINVAL;
4655 else
4656 ret = tcpm_pps_set_out_volt(port, val->intval / 1000);
4657 break;
4658 case POWER_SUPPLY_PROP_CURRENT_NOW:
4659 if (val->intval > port->pps_data.max_curr * 1000)
4660 ret = -EINVAL;
4661 else
4662 ret = tcpm_pps_set_op_curr(port, val->intval / 1000);
4663 break;
4664 default:
4665 ret = -EINVAL;
4666 break;
4667 }
4668
4669 return ret;
4670 }
4671
4672 static int tcpm_psy_prop_writeable(struct power_supply *psy,
4673 enum power_supply_property psp)
4674 {
4675 switch (psp) {
4676 case POWER_SUPPLY_PROP_ONLINE:
4677 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
4678 case POWER_SUPPLY_PROP_CURRENT_NOW:
4679 return 1;
4680 default:
4681 return 0;
4682 }
4683 }
4684
4685 static enum power_supply_usb_type tcpm_psy_usb_types[] = {
4686 POWER_SUPPLY_USB_TYPE_C,
4687 POWER_SUPPLY_USB_TYPE_PD,
4688 POWER_SUPPLY_USB_TYPE_PD_PPS,
4689 };
4690
4691 static const char *tcpm_psy_name_prefix = "tcpm-source-psy-";
4692
4693 static int devm_tcpm_psy_register(struct tcpm_port *port)
4694 {
4695 struct power_supply_config psy_cfg = {};
4696 const char *port_dev_name = dev_name(port->dev);
4697 size_t psy_name_len = strlen(tcpm_psy_name_prefix) +
4698 strlen(port_dev_name) + 1;
4699 char *psy_name;
4700
4701 psy_cfg.drv_data = port;
4702 psy_cfg.fwnode = dev_fwnode(port->dev);
4703 psy_name = devm_kzalloc(port->dev, psy_name_len, GFP_KERNEL);
4704 if (!psy_name)
4705 return -ENOMEM;
4706
4707 snprintf(psy_name, psy_name_len, "%s%s", tcpm_psy_name_prefix,
4708 port_dev_name);
4709 port->psy_desc.name = psy_name;
4710 port->psy_desc.type = POWER_SUPPLY_TYPE_USB,
4711 port->psy_desc.usb_types = tcpm_psy_usb_types;
4712 port->psy_desc.num_usb_types = ARRAY_SIZE(tcpm_psy_usb_types);
4713 port->psy_desc.properties = tcpm_psy_props,
4714 port->psy_desc.num_properties = ARRAY_SIZE(tcpm_psy_props),
4715 port->psy_desc.get_property = tcpm_psy_get_prop,
4716 port->psy_desc.set_property = tcpm_psy_set_prop,
4717 port->psy_desc.property_is_writeable = tcpm_psy_prop_writeable,
4718
4719 port->usb_type = POWER_SUPPLY_USB_TYPE_C;
4720
4721 port->psy = devm_power_supply_register(port->dev, &port->psy_desc,
4722 &psy_cfg);
4723
4724 return PTR_ERR_OR_ZERO(port->psy);
4725 }
4726
4727 static int tcpm_copy_caps(struct tcpm_port *port,
4728 const struct tcpc_config *tcfg)
4729 {
4730 if (tcpm_validate_caps(port, tcfg->src_pdo, tcfg->nr_src_pdo) ||
4731 tcpm_validate_caps(port, tcfg->snk_pdo, tcfg->nr_snk_pdo))
4732 return -EINVAL;
4733
4734 port->nr_src_pdo = tcpm_copy_pdos(port->src_pdo, tcfg->src_pdo,
4735 tcfg->nr_src_pdo);
4736 port->nr_snk_pdo = tcpm_copy_pdos(port->snk_pdo, tcfg->snk_pdo,
4737 tcfg->nr_snk_pdo);
4738
4739 port->nr_snk_vdo = tcpm_copy_vdos(port->snk_vdo, tcfg->snk_vdo,
4740 tcfg->nr_snk_vdo);
4741
4742 port->operating_snk_mw = tcfg->operating_snk_mw;
4743
4744 port->typec_caps.prefer_role = tcfg->default_role;
4745 port->typec_caps.type = tcfg->type;
4746 port->typec_caps.data = tcfg->data;
4747 port->self_powered = tcfg->self_powered;
4748
4749 return 0;
4750 }
4751
4752 struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
4753 {
4754 struct tcpm_port *port;
4755 int i, err;
4756
4757 if (!dev || !tcpc ||
4758 !tcpc->get_vbus || !tcpc->set_cc || !tcpc->get_cc ||
4759 !tcpc->set_polarity || !tcpc->set_vconn || !tcpc->set_vbus ||
4760 !tcpc->set_pd_rx || !tcpc->set_roles || !tcpc->pd_transmit)
4761 return ERR_PTR(-EINVAL);
4762
4763 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
4764 if (!port)
4765 return ERR_PTR(-ENOMEM);
4766
4767 port->dev = dev;
4768 port->tcpc = tcpc;
4769
4770 mutex_init(&port->lock);
4771 mutex_init(&port->swap_lock);
4772
4773 port->wq = create_singlethread_workqueue(dev_name(dev));
4774 if (!port->wq)
4775 return ERR_PTR(-ENOMEM);
4776 INIT_DELAYED_WORK(&port->state_machine, tcpm_state_machine_work);
4777 INIT_DELAYED_WORK(&port->vdm_state_machine, vdm_state_machine_work);
4778 INIT_WORK(&port->event_work, tcpm_pd_event_handler);
4779
4780 spin_lock_init(&port->pd_event_lock);
4781
4782 init_completion(&port->tx_complete);
4783 init_completion(&port->swap_complete);
4784 init_completion(&port->pps_complete);
4785 tcpm_debugfs_init(port);
4786
4787 err = tcpm_fw_get_caps(port, tcpc->fwnode);
4788 if ((err < 0) && tcpc->config)
4789 err = tcpm_copy_caps(port, tcpc->config);
4790 if (err < 0)
4791 goto out_destroy_wq;
4792
4793 if (!tcpc->config || !tcpc->config->try_role_hw)
4794 port->try_role = port->typec_caps.prefer_role;
4795 else
4796 port->try_role = TYPEC_NO_PREFERRED_ROLE;
4797
4798 port->typec_caps.fwnode = tcpc->fwnode;
4799 port->typec_caps.revision = 0x0120;
4800 port->typec_caps.pd_revision = 0x0300;
4801 port->typec_caps.dr_set = tcpm_dr_set;
4802 port->typec_caps.pr_set = tcpm_pr_set;
4803 port->typec_caps.vconn_set = tcpm_vconn_set;
4804 port->typec_caps.try_role = tcpm_try_role;
4805 port->typec_caps.port_type_set = tcpm_port_type_set;
4806
4807 port->partner_desc.identity = &port->partner_ident;
4808 port->port_type = port->typec_caps.type;
4809
4810 port->role_sw = usb_role_switch_get(port->dev);
4811 if (IS_ERR(port->role_sw)) {
4812 err = PTR_ERR(port->role_sw);
4813 goto out_destroy_wq;
4814 }
4815
4816 err = devm_tcpm_psy_register(port);
4817 if (err)
4818 goto out_role_sw_put;
4819
4820 port->typec_port = typec_register_port(port->dev, &port->typec_caps);
4821 if (IS_ERR(port->typec_port)) {
4822 err = PTR_ERR(port->typec_port);
4823 goto out_role_sw_put;
4824 }
4825
4826 if (tcpc->config && tcpc->config->alt_modes) {
4827 const struct typec_altmode_desc *paltmode = tcpc->config->alt_modes;
4828
4829 i = 0;
4830 while (paltmode->svid && i < ARRAY_SIZE(port->port_altmode)) {
4831 struct typec_altmode *alt;
4832
4833 alt = typec_port_register_altmode(port->typec_port,
4834 paltmode);
4835 if (IS_ERR(alt)) {
4836 tcpm_log(port,
4837 "%s: failed to register port alternate mode 0x%x",
4838 dev_name(dev), paltmode->svid);
4839 break;
4840 }
4841 typec_altmode_set_drvdata(alt, port);
4842 alt->ops = &tcpm_altmode_ops;
4843 port->port_altmode[i] = alt;
4844 i++;
4845 paltmode++;
4846 }
4847 }
4848
4849 mutex_lock(&port->lock);
4850 tcpm_init(port);
4851 mutex_unlock(&port->lock);
4852
4853 tcpm_log(port, "%s: registered", dev_name(dev));
4854 return port;
4855
4856 out_role_sw_put:
4857 usb_role_switch_put(port->role_sw);
4858 out_destroy_wq:
4859 tcpm_debugfs_exit(port);
4860 destroy_workqueue(port->wq);
4861 return ERR_PTR(err);
4862 }
4863 EXPORT_SYMBOL_GPL(tcpm_register_port);
4864
4865 void tcpm_unregister_port(struct tcpm_port *port)
4866 {
4867 int i;
4868
4869 tcpm_reset_port(port);
4870 for (i = 0; i < ARRAY_SIZE(port->port_altmode); i++)
4871 typec_unregister_altmode(port->port_altmode[i]);
4872 typec_unregister_port(port->typec_port);
4873 usb_role_switch_put(port->role_sw);
4874 tcpm_debugfs_exit(port);
4875 destroy_workqueue(port->wq);
4876 }
4877 EXPORT_SYMBOL_GPL(tcpm_unregister_port);
4878
4879 MODULE_AUTHOR("Guenter Roeck <groeck@chromium.org>");
4880 MODULE_DESCRIPTION("USB Type-C Port Manager");
4881 MODULE_LICENSE("GPL");