Linux 4.19.133
[linux/fpc-iii.git] / drivers / usb / typec / tcpm.c
blob29d72e9b0f017bb51a8a03184170cdc6ed7ed0c0
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright 2015-2017 Google, Inc
5 * USB Power Delivery protocol stack.
6 */
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/pd.h>
23 #include <linux/usb/pd_ado.h>
24 #include <linux/usb/pd_bdo.h>
25 #include <linux/usb/pd_ext_sdb.h>
26 #include <linux/usb/pd_vdo.h>
27 #include <linux/usb/role.h>
28 #include <linux/usb/tcpm.h>
29 #include <linux/usb/typec_altmode.h>
30 #include <linux/workqueue.h>
32 #define FOREACH_STATE(S) \
33 S(INVALID_STATE), \
34 S(TOGGLING), \
35 S(SRC_UNATTACHED), \
36 S(SRC_ATTACH_WAIT), \
37 S(SRC_ATTACHED), \
38 S(SRC_STARTUP), \
39 S(SRC_SEND_CAPABILITIES), \
40 S(SRC_SEND_CAPABILITIES_TIMEOUT), \
41 S(SRC_NEGOTIATE_CAPABILITIES), \
42 S(SRC_TRANSITION_SUPPLY), \
43 S(SRC_READY), \
44 S(SRC_WAIT_NEW_CAPABILITIES), \
46 S(SNK_UNATTACHED), \
47 S(SNK_ATTACH_WAIT), \
48 S(SNK_DEBOUNCED), \
49 S(SNK_ATTACHED), \
50 S(SNK_STARTUP), \
51 S(SNK_DISCOVERY), \
52 S(SNK_DISCOVERY_DEBOUNCE), \
53 S(SNK_DISCOVERY_DEBOUNCE_DONE), \
54 S(SNK_WAIT_CAPABILITIES), \
55 S(SNK_NEGOTIATE_CAPABILITIES), \
56 S(SNK_NEGOTIATE_PPS_CAPABILITIES), \
57 S(SNK_TRANSITION_SINK), \
58 S(SNK_TRANSITION_SINK_VBUS), \
59 S(SNK_READY), \
61 S(ACC_UNATTACHED), \
62 S(DEBUG_ACC_ATTACHED), \
63 S(AUDIO_ACC_ATTACHED), \
64 S(AUDIO_ACC_DEBOUNCE), \
66 S(HARD_RESET_SEND), \
67 S(HARD_RESET_START), \
68 S(SRC_HARD_RESET_VBUS_OFF), \
69 S(SRC_HARD_RESET_VBUS_ON), \
70 S(SNK_HARD_RESET_SINK_OFF), \
71 S(SNK_HARD_RESET_WAIT_VBUS), \
72 S(SNK_HARD_RESET_SINK_ON), \
74 S(SOFT_RESET), \
75 S(SOFT_RESET_SEND), \
77 S(DR_SWAP_ACCEPT), \
78 S(DR_SWAP_SEND), \
79 S(DR_SWAP_SEND_TIMEOUT), \
80 S(DR_SWAP_CANCEL), \
81 S(DR_SWAP_CHANGE_DR), \
83 S(PR_SWAP_ACCEPT), \
84 S(PR_SWAP_SEND), \
85 S(PR_SWAP_SEND_TIMEOUT), \
86 S(PR_SWAP_CANCEL), \
87 S(PR_SWAP_START), \
88 S(PR_SWAP_SRC_SNK_TRANSITION_OFF), \
89 S(PR_SWAP_SRC_SNK_SOURCE_OFF), \
90 S(PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED), \
91 S(PR_SWAP_SRC_SNK_SINK_ON), \
92 S(PR_SWAP_SNK_SRC_SINK_OFF), \
93 S(PR_SWAP_SNK_SRC_SOURCE_ON), \
94 S(PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP), \
96 S(VCONN_SWAP_ACCEPT), \
97 S(VCONN_SWAP_SEND), \
98 S(VCONN_SWAP_SEND_TIMEOUT), \
99 S(VCONN_SWAP_CANCEL), \
100 S(VCONN_SWAP_START), \
101 S(VCONN_SWAP_WAIT_FOR_VCONN), \
102 S(VCONN_SWAP_TURN_ON_VCONN), \
103 S(VCONN_SWAP_TURN_OFF_VCONN), \
105 S(SNK_TRY), \
106 S(SNK_TRY_WAIT), \
107 S(SNK_TRY_WAIT_DEBOUNCE), \
108 S(SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS), \
109 S(SRC_TRYWAIT), \
110 S(SRC_TRYWAIT_DEBOUNCE), \
111 S(SRC_TRYWAIT_UNATTACHED), \
113 S(SRC_TRY), \
114 S(SRC_TRY_WAIT), \
115 S(SRC_TRY_DEBOUNCE), \
116 S(SNK_TRYWAIT), \
117 S(SNK_TRYWAIT_DEBOUNCE), \
118 S(SNK_TRYWAIT_VBUS), \
119 S(BIST_RX), \
121 S(GET_STATUS_SEND), \
122 S(GET_STATUS_SEND_TIMEOUT), \
123 S(GET_PPS_STATUS_SEND), \
124 S(GET_PPS_STATUS_SEND_TIMEOUT), \
126 S(ERROR_RECOVERY), \
127 S(PORT_RESET), \
128 S(PORT_RESET_WAIT_OFF)
130 #define GENERATE_ENUM(e) e
131 #define GENERATE_STRING(s) #s
133 enum tcpm_state {
134 FOREACH_STATE(GENERATE_ENUM)
137 static const char * const tcpm_states[] = {
138 FOREACH_STATE(GENERATE_STRING)
141 enum vdm_states {
142 VDM_STATE_ERR_BUSY = -3,
143 VDM_STATE_ERR_SEND = -2,
144 VDM_STATE_ERR_TMOUT = -1,
145 VDM_STATE_DONE = 0,
146 /* Anything >0 represents an active state */
147 VDM_STATE_READY = 1,
148 VDM_STATE_BUSY = 2,
149 VDM_STATE_WAIT_RSP_BUSY = 3,
152 enum pd_msg_request {
153 PD_MSG_NONE = 0,
154 PD_MSG_CTRL_REJECT,
155 PD_MSG_CTRL_WAIT,
156 PD_MSG_CTRL_NOT_SUPP,
157 PD_MSG_DATA_SINK_CAP,
158 PD_MSG_DATA_SOURCE_CAP,
161 /* Events from low level driver */
163 #define TCPM_CC_EVENT BIT(0)
164 #define TCPM_VBUS_EVENT BIT(1)
165 #define TCPM_RESET_EVENT BIT(2)
167 #define LOG_BUFFER_ENTRIES 1024
168 #define LOG_BUFFER_ENTRY_SIZE 128
170 /* Alternate mode support */
172 #define SVID_DISCOVERY_MAX 16
173 #define ALTMODE_DISCOVERY_MAX (SVID_DISCOVERY_MAX * MODE_DISCOVERY_MAX)
175 struct pd_mode_data {
176 int svid_index; /* current SVID index */
177 int nsvids;
178 u16 svids[SVID_DISCOVERY_MAX];
179 int altmodes; /* number of alternate modes */
180 struct typec_altmode_desc altmode_desc[ALTMODE_DISCOVERY_MAX];
183 struct pd_pps_data {
184 u32 min_volt;
185 u32 max_volt;
186 u32 max_curr;
187 u32 out_volt;
188 u32 op_curr;
189 bool supported;
190 bool active;
193 struct tcpm_port {
194 struct device *dev;
196 struct mutex lock; /* tcpm state machine lock */
197 struct workqueue_struct *wq;
199 struct typec_capability typec_caps;
200 struct typec_port *typec_port;
202 struct tcpc_dev *tcpc;
203 struct usb_role_switch *role_sw;
205 enum typec_role vconn_role;
206 enum typec_role pwr_role;
207 enum typec_data_role data_role;
208 enum typec_pwr_opmode pwr_opmode;
210 struct usb_pd_identity partner_ident;
211 struct typec_partner_desc partner_desc;
212 struct typec_partner *partner;
214 enum typec_cc_status cc_req;
216 enum typec_cc_status cc1;
217 enum typec_cc_status cc2;
218 enum typec_cc_polarity polarity;
220 bool attached;
221 bool connected;
222 enum typec_port_type port_type;
223 bool vbus_present;
224 bool vbus_never_low;
225 bool vbus_source;
226 bool vbus_charge;
228 bool send_discover;
229 bool op_vsafe5v;
231 int try_role;
232 int try_snk_count;
233 int try_src_count;
235 enum pd_msg_request queued_message;
237 enum tcpm_state enter_state;
238 enum tcpm_state prev_state;
239 enum tcpm_state state;
240 enum tcpm_state delayed_state;
241 unsigned long delayed_runtime;
242 unsigned long delay_ms;
244 spinlock_t pd_event_lock;
245 u32 pd_events;
247 struct work_struct event_work;
248 struct delayed_work state_machine;
249 struct delayed_work vdm_state_machine;
250 bool state_machine_running;
252 struct completion tx_complete;
253 enum tcpm_transmit_status tx_status;
255 struct mutex swap_lock; /* swap command lock */
256 bool swap_pending;
257 bool non_pd_role_swap;
258 struct completion swap_complete;
259 int swap_status;
261 unsigned int negotiated_rev;
262 unsigned int message_id;
263 unsigned int caps_count;
264 unsigned int hard_reset_count;
265 bool pd_capable;
266 bool explicit_contract;
267 unsigned int rx_msgid;
269 /* Partner capabilities/requests */
270 u32 sink_request;
271 u32 source_caps[PDO_MAX_OBJECTS];
272 unsigned int nr_source_caps;
273 u32 sink_caps[PDO_MAX_OBJECTS];
274 unsigned int nr_sink_caps;
276 /* Local capabilities */
277 u32 src_pdo[PDO_MAX_OBJECTS];
278 unsigned int nr_src_pdo;
279 u32 snk_pdo[PDO_MAX_OBJECTS];
280 unsigned int nr_snk_pdo;
281 u32 snk_vdo[VDO_MAX_OBJECTS];
282 unsigned int nr_snk_vdo;
284 unsigned int operating_snk_mw;
285 bool update_sink_caps;
287 /* Requested current / voltage */
288 u32 current_limit;
289 u32 supply_voltage;
291 /* Used to export TA voltage and current */
292 struct power_supply *psy;
293 struct power_supply_desc psy_desc;
294 enum power_supply_usb_type usb_type;
296 u32 bist_request;
298 /* PD state for Vendor Defined Messages */
299 enum vdm_states vdm_state;
300 u32 vdm_retries;
301 /* next Vendor Defined Message to send */
302 u32 vdo_data[VDO_MAX_SIZE];
303 u8 vdo_count;
304 /* VDO to retry if UFP responder replied busy */
305 u32 vdo_retry;
307 /* PPS */
308 struct pd_pps_data pps_data;
309 struct completion pps_complete;
310 bool pps_pending;
311 int pps_status;
313 /* Alternate mode data */
314 struct pd_mode_data mode_data;
315 struct typec_altmode *partner_altmode[ALTMODE_DISCOVERY_MAX];
316 struct typec_altmode *port_altmode[ALTMODE_DISCOVERY_MAX];
318 /* Deadline in jiffies to exit src_try_wait state */
319 unsigned long max_wait;
321 /* port belongs to a self powered device */
322 bool self_powered;
324 #ifdef CONFIG_DEBUG_FS
325 struct dentry *dentry;
326 struct mutex logbuffer_lock; /* log buffer access lock */
327 int logbuffer_head;
328 int logbuffer_tail;
329 u8 *logbuffer[LOG_BUFFER_ENTRIES];
330 #endif
333 struct pd_rx_event {
334 struct work_struct work;
335 struct tcpm_port *port;
336 struct pd_message msg;
339 #define tcpm_cc_is_sink(cc) \
340 ((cc) == TYPEC_CC_RP_DEF || (cc) == TYPEC_CC_RP_1_5 || \
341 (cc) == TYPEC_CC_RP_3_0)
343 #define tcpm_port_is_sink(port) \
344 ((tcpm_cc_is_sink((port)->cc1) && !tcpm_cc_is_sink((port)->cc2)) || \
345 (tcpm_cc_is_sink((port)->cc2) && !tcpm_cc_is_sink((port)->cc1)))
347 #define tcpm_cc_is_source(cc) ((cc) == TYPEC_CC_RD)
348 #define tcpm_cc_is_audio(cc) ((cc) == TYPEC_CC_RA)
349 #define tcpm_cc_is_open(cc) ((cc) == TYPEC_CC_OPEN)
351 #define tcpm_port_is_source(port) \
352 ((tcpm_cc_is_source((port)->cc1) && \
353 !tcpm_cc_is_source((port)->cc2)) || \
354 (tcpm_cc_is_source((port)->cc2) && \
355 !tcpm_cc_is_source((port)->cc1)))
357 #define tcpm_port_is_debug(port) \
358 (tcpm_cc_is_source((port)->cc1) && tcpm_cc_is_source((port)->cc2))
360 #define tcpm_port_is_audio(port) \
361 (tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_audio((port)->cc2))
363 #define tcpm_port_is_audio_detached(port) \
364 ((tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_open((port)->cc2)) || \
365 (tcpm_cc_is_audio((port)->cc2) && tcpm_cc_is_open((port)->cc1)))
367 #define tcpm_try_snk(port) \
368 ((port)->try_snk_count == 0 && (port)->try_role == TYPEC_SINK && \
369 (port)->port_type == TYPEC_PORT_DRP)
371 #define tcpm_try_src(port) \
372 ((port)->try_src_count == 0 && (port)->try_role == TYPEC_SOURCE && \
373 (port)->port_type == TYPEC_PORT_DRP)
375 static enum tcpm_state tcpm_default_state(struct tcpm_port *port)
377 if (port->port_type == TYPEC_PORT_DRP) {
378 if (port->try_role == TYPEC_SINK)
379 return SNK_UNATTACHED;
380 else if (port->try_role == TYPEC_SOURCE)
381 return SRC_UNATTACHED;
382 else if (port->tcpc->config &&
383 port->tcpc->config->default_role == TYPEC_SINK)
384 return SNK_UNATTACHED;
385 /* Fall through to return SRC_UNATTACHED */
386 } else if (port->port_type == TYPEC_PORT_SNK) {
387 return SNK_UNATTACHED;
389 return SRC_UNATTACHED;
392 static inline
393 struct tcpm_port *typec_cap_to_tcpm(const struct typec_capability *cap)
395 return container_of(cap, struct tcpm_port, typec_caps);
398 static bool tcpm_port_is_disconnected(struct tcpm_port *port)
400 return (!port->attached && port->cc1 == TYPEC_CC_OPEN &&
401 port->cc2 == TYPEC_CC_OPEN) ||
402 (port->attached && ((port->polarity == TYPEC_POLARITY_CC1 &&
403 port->cc1 == TYPEC_CC_OPEN) ||
404 (port->polarity == TYPEC_POLARITY_CC2 &&
405 port->cc2 == TYPEC_CC_OPEN)));
409 * Logging
412 #ifdef CONFIG_DEBUG_FS
414 static bool tcpm_log_full(struct tcpm_port *port)
416 return port->logbuffer_tail ==
417 (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
420 __printf(2, 0)
421 static void _tcpm_log(struct tcpm_port *port, const char *fmt, va_list args)
423 char tmpbuffer[LOG_BUFFER_ENTRY_SIZE];
424 u64 ts_nsec = local_clock();
425 unsigned long rem_nsec;
427 mutex_lock(&port->logbuffer_lock);
428 if (!port->logbuffer[port->logbuffer_head]) {
429 port->logbuffer[port->logbuffer_head] =
430 kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL);
431 if (!port->logbuffer[port->logbuffer_head]) {
432 mutex_unlock(&port->logbuffer_lock);
433 return;
437 vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args);
439 if (tcpm_log_full(port)) {
440 port->logbuffer_head = max(port->logbuffer_head - 1, 0);
441 strcpy(tmpbuffer, "overflow");
444 if (port->logbuffer_head < 0 ||
445 port->logbuffer_head >= LOG_BUFFER_ENTRIES) {
446 dev_warn(port->dev,
447 "Bad log buffer index %d\n", port->logbuffer_head);
448 goto abort;
451 if (!port->logbuffer[port->logbuffer_head]) {
452 dev_warn(port->dev,
453 "Log buffer index %d is NULL\n", port->logbuffer_head);
454 goto abort;
457 rem_nsec = do_div(ts_nsec, 1000000000);
458 scnprintf(port->logbuffer[port->logbuffer_head],
459 LOG_BUFFER_ENTRY_SIZE, "[%5lu.%06lu] %s",
460 (unsigned long)ts_nsec, rem_nsec / 1000,
461 tmpbuffer);
462 port->logbuffer_head = (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
464 abort:
465 mutex_unlock(&port->logbuffer_lock);
468 __printf(2, 3)
469 static void tcpm_log(struct tcpm_port *port, const char *fmt, ...)
471 va_list args;
473 /* Do not log while disconnected and unattached */
474 if (tcpm_port_is_disconnected(port) &&
475 (port->state == SRC_UNATTACHED || port->state == SNK_UNATTACHED ||
476 port->state == TOGGLING))
477 return;
479 va_start(args, fmt);
480 _tcpm_log(port, fmt, args);
481 va_end(args);
484 __printf(2, 3)
485 static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...)
487 va_list args;
489 va_start(args, fmt);
490 _tcpm_log(port, fmt, args);
491 va_end(args);
494 static void tcpm_log_source_caps(struct tcpm_port *port)
496 int i;
498 for (i = 0; i < port->nr_source_caps; i++) {
499 u32 pdo = port->source_caps[i];
500 enum pd_pdo_type type = pdo_type(pdo);
501 char msg[64];
503 switch (type) {
504 case PDO_TYPE_FIXED:
505 scnprintf(msg, sizeof(msg),
506 "%u mV, %u mA [%s%s%s%s%s%s]",
507 pdo_fixed_voltage(pdo),
508 pdo_max_current(pdo),
509 (pdo & PDO_FIXED_DUAL_ROLE) ?
510 "R" : "",
511 (pdo & PDO_FIXED_SUSPEND) ?
512 "S" : "",
513 (pdo & PDO_FIXED_HIGHER_CAP) ?
514 "H" : "",
515 (pdo & PDO_FIXED_USB_COMM) ?
516 "U" : "",
517 (pdo & PDO_FIXED_DATA_SWAP) ?
518 "D" : "",
519 (pdo & PDO_FIXED_EXTPOWER) ?
520 "E" : "");
521 break;
522 case PDO_TYPE_VAR:
523 scnprintf(msg, sizeof(msg),
524 "%u-%u mV, %u mA",
525 pdo_min_voltage(pdo),
526 pdo_max_voltage(pdo),
527 pdo_max_current(pdo));
528 break;
529 case PDO_TYPE_BATT:
530 scnprintf(msg, sizeof(msg),
531 "%u-%u mV, %u mW",
532 pdo_min_voltage(pdo),
533 pdo_max_voltage(pdo),
534 pdo_max_power(pdo));
535 break;
536 case PDO_TYPE_APDO:
537 if (pdo_apdo_type(pdo) == APDO_TYPE_PPS)
538 scnprintf(msg, sizeof(msg),
539 "%u-%u mV, %u mA",
540 pdo_pps_apdo_min_voltage(pdo),
541 pdo_pps_apdo_max_voltage(pdo),
542 pdo_pps_apdo_max_current(pdo));
543 else
544 strcpy(msg, "undefined APDO");
545 break;
546 default:
547 strcpy(msg, "undefined");
548 break;
550 tcpm_log(port, " PDO %d: type %d, %s",
551 i, type, msg);
555 static int tcpm_debug_show(struct seq_file *s, void *v)
557 struct tcpm_port *port = (struct tcpm_port *)s->private;
558 int tail;
560 mutex_lock(&port->logbuffer_lock);
561 tail = port->logbuffer_tail;
562 while (tail != port->logbuffer_head) {
563 seq_printf(s, "%s\n", port->logbuffer[tail]);
564 tail = (tail + 1) % LOG_BUFFER_ENTRIES;
566 if (!seq_has_overflowed(s))
567 port->logbuffer_tail = tail;
568 mutex_unlock(&port->logbuffer_lock);
570 return 0;
572 DEFINE_SHOW_ATTRIBUTE(tcpm_debug);
574 static struct dentry *rootdir;
576 static void tcpm_debugfs_init(struct tcpm_port *port)
578 mutex_init(&port->logbuffer_lock);
579 /* /sys/kernel/debug/tcpm/usbcX */
580 if (!rootdir)
581 rootdir = debugfs_create_dir("tcpm", NULL);
583 port->dentry = debugfs_create_file(dev_name(port->dev),
584 S_IFREG | 0444, rootdir,
585 port, &tcpm_debug_fops);
588 static void tcpm_debugfs_exit(struct tcpm_port *port)
590 int i;
592 mutex_lock(&port->logbuffer_lock);
593 for (i = 0; i < LOG_BUFFER_ENTRIES; i++) {
594 kfree(port->logbuffer[i]);
595 port->logbuffer[i] = NULL;
597 mutex_unlock(&port->logbuffer_lock);
599 debugfs_remove(port->dentry);
600 if (list_empty(&rootdir->d_subdirs)) {
601 debugfs_remove(rootdir);
602 rootdir = NULL;
606 #else
608 __printf(2, 3)
609 static void tcpm_log(const struct tcpm_port *port, const char *fmt, ...) { }
610 __printf(2, 3)
611 static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...) { }
612 static void tcpm_log_source_caps(struct tcpm_port *port) { }
613 static void tcpm_debugfs_init(const struct tcpm_port *port) { }
614 static void tcpm_debugfs_exit(const struct tcpm_port *port) { }
616 #endif
618 static int tcpm_pd_transmit(struct tcpm_port *port,
619 enum tcpm_transmit_type type,
620 const struct pd_message *msg)
622 unsigned long timeout;
623 int ret;
625 if (msg)
626 tcpm_log(port, "PD TX, header: %#x", le16_to_cpu(msg->header));
627 else
628 tcpm_log(port, "PD TX, type: %#x", type);
630 reinit_completion(&port->tx_complete);
631 ret = port->tcpc->pd_transmit(port->tcpc, type, msg);
632 if (ret < 0)
633 return ret;
635 mutex_unlock(&port->lock);
636 timeout = wait_for_completion_timeout(&port->tx_complete,
637 msecs_to_jiffies(PD_T_TCPC_TX_TIMEOUT));
638 mutex_lock(&port->lock);
639 if (!timeout)
640 return -ETIMEDOUT;
642 switch (port->tx_status) {
643 case TCPC_TX_SUCCESS:
644 port->message_id = (port->message_id + 1) & PD_HEADER_ID_MASK;
645 return 0;
646 case TCPC_TX_DISCARDED:
647 return -EAGAIN;
648 case TCPC_TX_FAILED:
649 default:
650 return -EIO;
654 void tcpm_pd_transmit_complete(struct tcpm_port *port,
655 enum tcpm_transmit_status status)
657 tcpm_log(port, "PD TX complete, status: %u", status);
658 port->tx_status = status;
659 complete(&port->tx_complete);
661 EXPORT_SYMBOL_GPL(tcpm_pd_transmit_complete);
663 static int tcpm_mux_set(struct tcpm_port *port, int state,
664 enum usb_role usb_role,
665 enum typec_orientation orientation)
667 int ret;
669 tcpm_log(port, "Requesting mux state %d, usb-role %d, orientation %d",
670 state, usb_role, orientation);
672 ret = typec_set_orientation(port->typec_port, orientation);
673 if (ret)
674 return ret;
676 if (port->role_sw) {
677 ret = usb_role_switch_set_role(port->role_sw, usb_role);
678 if (ret)
679 return ret;
682 return typec_set_mode(port->typec_port, state);
685 static int tcpm_set_polarity(struct tcpm_port *port,
686 enum typec_cc_polarity polarity)
688 int ret;
690 tcpm_log(port, "polarity %d", polarity);
692 ret = port->tcpc->set_polarity(port->tcpc, polarity);
693 if (ret < 0)
694 return ret;
696 port->polarity = polarity;
698 return 0;
701 static int tcpm_set_vconn(struct tcpm_port *port, bool enable)
703 int ret;
705 tcpm_log(port, "vconn:=%d", enable);
707 ret = port->tcpc->set_vconn(port->tcpc, enable);
708 if (!ret) {
709 port->vconn_role = enable ? TYPEC_SOURCE : TYPEC_SINK;
710 typec_set_vconn_role(port->typec_port, port->vconn_role);
713 return ret;
716 static u32 tcpm_get_current_limit(struct tcpm_port *port)
718 enum typec_cc_status cc;
719 u32 limit;
721 cc = port->polarity ? port->cc2 : port->cc1;
722 switch (cc) {
723 case TYPEC_CC_RP_1_5:
724 limit = 1500;
725 break;
726 case TYPEC_CC_RP_3_0:
727 limit = 3000;
728 break;
729 case TYPEC_CC_RP_DEF:
730 default:
731 if (port->tcpc->get_current_limit)
732 limit = port->tcpc->get_current_limit(port->tcpc);
733 else
734 limit = 0;
735 break;
738 return limit;
741 static int tcpm_set_current_limit(struct tcpm_port *port, u32 max_ma, u32 mv)
743 int ret = -EOPNOTSUPP;
745 tcpm_log(port, "Setting voltage/current limit %u mV %u mA", mv, max_ma);
747 port->supply_voltage = mv;
748 port->current_limit = max_ma;
750 if (port->tcpc->set_current_limit)
751 ret = port->tcpc->set_current_limit(port->tcpc, max_ma, mv);
753 return ret;
757 * Determine RP value to set based on maximum current supported
758 * by a port if configured as source.
759 * Returns CC value to report to link partner.
761 static enum typec_cc_status tcpm_rp_cc(struct tcpm_port *port)
763 const u32 *src_pdo = port->src_pdo;
764 int nr_pdo = port->nr_src_pdo;
765 int i;
768 * Search for first entry with matching voltage.
769 * It should report the maximum supported current.
771 for (i = 0; i < nr_pdo; i++) {
772 const u32 pdo = src_pdo[i];
774 if (pdo_type(pdo) == PDO_TYPE_FIXED &&
775 pdo_fixed_voltage(pdo) == 5000) {
776 unsigned int curr = pdo_max_current(pdo);
778 if (curr >= 3000)
779 return TYPEC_CC_RP_3_0;
780 else if (curr >= 1500)
781 return TYPEC_CC_RP_1_5;
782 return TYPEC_CC_RP_DEF;
786 return TYPEC_CC_RP_DEF;
789 static int tcpm_set_attached_state(struct tcpm_port *port, bool attached)
791 return port->tcpc->set_roles(port->tcpc, attached, port->pwr_role,
792 port->data_role);
795 static int tcpm_set_roles(struct tcpm_port *port, bool attached,
796 enum typec_role role, enum typec_data_role data)
798 enum typec_orientation orientation;
799 enum usb_role usb_role;
800 int ret;
802 if (port->polarity == TYPEC_POLARITY_CC1)
803 orientation = TYPEC_ORIENTATION_NORMAL;
804 else
805 orientation = TYPEC_ORIENTATION_REVERSE;
807 if (data == TYPEC_HOST)
808 usb_role = USB_ROLE_HOST;
809 else
810 usb_role = USB_ROLE_DEVICE;
812 ret = tcpm_mux_set(port, TYPEC_STATE_USB, usb_role, orientation);
813 if (ret < 0)
814 return ret;
816 ret = port->tcpc->set_roles(port->tcpc, attached, role, data);
817 if (ret < 0)
818 return ret;
820 port->pwr_role = role;
821 port->data_role = data;
822 typec_set_data_role(port->typec_port, data);
823 typec_set_pwr_role(port->typec_port, role);
825 return 0;
828 static int tcpm_set_pwr_role(struct tcpm_port *port, enum typec_role role)
830 int ret;
832 ret = port->tcpc->set_roles(port->tcpc, true, role,
833 port->data_role);
834 if (ret < 0)
835 return ret;
837 port->pwr_role = role;
838 typec_set_pwr_role(port->typec_port, role);
840 return 0;
843 static int tcpm_pd_send_source_caps(struct tcpm_port *port)
845 struct pd_message msg;
846 int i;
848 memset(&msg, 0, sizeof(msg));
849 if (!port->nr_src_pdo) {
850 /* No source capabilities defined, sink only */
851 msg.header = PD_HEADER_LE(PD_CTRL_REJECT,
852 port->pwr_role,
853 port->data_role,
854 port->negotiated_rev,
855 port->message_id, 0);
856 } else {
857 msg.header = PD_HEADER_LE(PD_DATA_SOURCE_CAP,
858 port->pwr_role,
859 port->data_role,
860 port->negotiated_rev,
861 port->message_id,
862 port->nr_src_pdo);
864 for (i = 0; i < port->nr_src_pdo; i++)
865 msg.payload[i] = cpu_to_le32(port->src_pdo[i]);
867 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
870 static int tcpm_pd_send_sink_caps(struct tcpm_port *port)
872 struct pd_message msg;
873 int i;
875 memset(&msg, 0, sizeof(msg));
876 if (!port->nr_snk_pdo) {
877 /* No sink capabilities defined, source only */
878 msg.header = PD_HEADER_LE(PD_CTRL_REJECT,
879 port->pwr_role,
880 port->data_role,
881 port->negotiated_rev,
882 port->message_id, 0);
883 } else {
884 msg.header = PD_HEADER_LE(PD_DATA_SINK_CAP,
885 port->pwr_role,
886 port->data_role,
887 port->negotiated_rev,
888 port->message_id,
889 port->nr_snk_pdo);
891 for (i = 0; i < port->nr_snk_pdo; i++)
892 msg.payload[i] = cpu_to_le32(port->snk_pdo[i]);
894 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
897 static void tcpm_set_state(struct tcpm_port *port, enum tcpm_state state,
898 unsigned int delay_ms)
900 if (delay_ms) {
901 tcpm_log(port, "pending state change %s -> %s @ %u ms",
902 tcpm_states[port->state], tcpm_states[state],
903 delay_ms);
904 port->delayed_state = state;
905 mod_delayed_work(port->wq, &port->state_machine,
906 msecs_to_jiffies(delay_ms));
907 port->delayed_runtime = jiffies + msecs_to_jiffies(delay_ms);
908 port->delay_ms = delay_ms;
909 } else {
910 tcpm_log(port, "state change %s -> %s",
911 tcpm_states[port->state], tcpm_states[state]);
912 port->delayed_state = INVALID_STATE;
913 port->prev_state = port->state;
914 port->state = state;
916 * Don't re-queue the state machine work item if we're currently
917 * in the state machine and we're immediately changing states.
918 * tcpm_state_machine_work() will continue running the state
919 * machine.
921 if (!port->state_machine_running)
922 mod_delayed_work(port->wq, &port->state_machine, 0);
926 static void tcpm_set_state_cond(struct tcpm_port *port, enum tcpm_state state,
927 unsigned int delay_ms)
929 if (port->enter_state == port->state)
930 tcpm_set_state(port, state, delay_ms);
931 else
932 tcpm_log(port,
933 "skipped %sstate change %s -> %s [%u ms], context state %s",
934 delay_ms ? "delayed " : "",
935 tcpm_states[port->state], tcpm_states[state],
936 delay_ms, tcpm_states[port->enter_state]);
939 static void tcpm_queue_message(struct tcpm_port *port,
940 enum pd_msg_request message)
942 port->queued_message = message;
943 mod_delayed_work(port->wq, &port->state_machine, 0);
947 * VDM/VDO handling functions
949 static void tcpm_queue_vdm(struct tcpm_port *port, const u32 header,
950 const u32 *data, int cnt)
952 port->vdo_count = cnt + 1;
953 port->vdo_data[0] = header;
954 memcpy(&port->vdo_data[1], data, sizeof(u32) * cnt);
955 /* Set ready, vdm state machine will actually send */
956 port->vdm_retries = 0;
957 port->vdm_state = VDM_STATE_READY;
960 static void svdm_consume_identity(struct tcpm_port *port, const __le32 *payload,
961 int cnt)
963 u32 vdo = le32_to_cpu(payload[VDO_INDEX_IDH]);
964 u32 product = le32_to_cpu(payload[VDO_INDEX_PRODUCT]);
966 memset(&port->mode_data, 0, sizeof(port->mode_data));
968 port->partner_ident.id_header = vdo;
969 port->partner_ident.cert_stat = le32_to_cpu(payload[VDO_INDEX_CSTAT]);
970 port->partner_ident.product = product;
972 typec_partner_set_identity(port->partner);
974 tcpm_log(port, "Identity: %04x:%04x.%04x",
975 PD_IDH_VID(vdo),
976 PD_PRODUCT_PID(product), product & 0xffff);
979 static bool svdm_consume_svids(struct tcpm_port *port, const __le32 *payload,
980 int cnt)
982 struct pd_mode_data *pmdata = &port->mode_data;
983 int i;
985 for (i = 1; i < cnt; i++) {
986 u32 p = le32_to_cpu(payload[i]);
987 u16 svid;
989 svid = (p >> 16) & 0xffff;
990 if (!svid)
991 return false;
993 if (pmdata->nsvids >= SVID_DISCOVERY_MAX)
994 goto abort;
996 pmdata->svids[pmdata->nsvids++] = svid;
997 tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid);
999 svid = p & 0xffff;
1000 if (!svid)
1001 return false;
1003 if (pmdata->nsvids >= SVID_DISCOVERY_MAX)
1004 goto abort;
1006 pmdata->svids[pmdata->nsvids++] = svid;
1007 tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid);
1009 return true;
1010 abort:
1011 tcpm_log(port, "SVID_DISCOVERY_MAX(%d) too low!", SVID_DISCOVERY_MAX);
1012 return false;
1015 static void svdm_consume_modes(struct tcpm_port *port, const __le32 *payload,
1016 int cnt)
1018 struct pd_mode_data *pmdata = &port->mode_data;
1019 struct typec_altmode_desc *paltmode;
1020 int i;
1022 if (pmdata->altmodes >= ARRAY_SIZE(port->partner_altmode)) {
1023 /* Already logged in svdm_consume_svids() */
1024 return;
1027 for (i = 1; i < cnt; i++) {
1028 paltmode = &pmdata->altmode_desc[pmdata->altmodes];
1029 memset(paltmode, 0, sizeof(*paltmode));
1031 paltmode->svid = pmdata->svids[pmdata->svid_index];
1032 paltmode->mode = i;
1033 paltmode->vdo = le32_to_cpu(payload[i]);
1035 tcpm_log(port, " Alternate mode %d: SVID 0x%04x, VDO %d: 0x%08x",
1036 pmdata->altmodes, paltmode->svid,
1037 paltmode->mode, paltmode->vdo);
1039 pmdata->altmodes++;
1043 static void tcpm_register_partner_altmodes(struct tcpm_port *port)
1045 struct pd_mode_data *modep = &port->mode_data;
1046 struct typec_altmode *altmode;
1047 int i;
1049 for (i = 0; i < modep->altmodes; i++) {
1050 altmode = typec_partner_register_altmode(port->partner,
1051 &modep->altmode_desc[i]);
1052 if (!altmode)
1053 tcpm_log(port, "Failed to register partner SVID 0x%04x",
1054 modep->altmode_desc[i].svid);
1055 port->partner_altmode[i] = altmode;
1059 #define supports_modal(port) PD_IDH_MODAL_SUPP((port)->partner_ident.id_header)
1061 static int tcpm_pd_svdm(struct tcpm_port *port, const __le32 *payload, int cnt,
1062 u32 *response)
1064 struct typec_altmode *adev;
1065 struct typec_altmode *pdev;
1066 struct pd_mode_data *modep;
1067 u32 p[PD_MAX_PAYLOAD];
1068 int rlen = 0;
1069 int cmd_type;
1070 int cmd;
1071 int i;
1073 for (i = 0; i < cnt; i++)
1074 p[i] = le32_to_cpu(payload[i]);
1076 cmd_type = PD_VDO_CMDT(p[0]);
1077 cmd = PD_VDO_CMD(p[0]);
1079 tcpm_log(port, "Rx VDM cmd 0x%x type %d cmd %d len %d",
1080 p[0], cmd_type, cmd, cnt);
1082 modep = &port->mode_data;
1084 adev = typec_match_altmode(port->port_altmode, ALTMODE_DISCOVERY_MAX,
1085 PD_VDO_VID(p[0]), PD_VDO_OPOS(p[0]));
1087 pdev = typec_match_altmode(port->partner_altmode, ALTMODE_DISCOVERY_MAX,
1088 PD_VDO_VID(p[0]), PD_VDO_OPOS(p[0]));
1090 switch (cmd_type) {
1091 case CMDT_INIT:
1092 switch (cmd) {
1093 case CMD_DISCOVER_IDENT:
1094 /* 6.4.4.3.1: Only respond as UFP (device) */
1095 if (port->data_role == TYPEC_DEVICE &&
1096 port->nr_snk_vdo) {
1097 for (i = 0; i < port->nr_snk_vdo; i++)
1098 response[i + 1] = port->snk_vdo[i];
1099 rlen = port->nr_snk_vdo + 1;
1101 break;
1102 case CMD_DISCOVER_SVID:
1103 break;
1104 case CMD_DISCOVER_MODES:
1105 break;
1106 case CMD_ENTER_MODE:
1107 break;
1108 case CMD_EXIT_MODE:
1109 break;
1110 case CMD_ATTENTION:
1111 /* Attention command does not have response */
1112 if (adev)
1113 typec_altmode_attention(adev, p[1]);
1114 return 0;
1115 default:
1116 break;
1118 if (rlen >= 1) {
1119 response[0] = p[0] | VDO_CMDT(CMDT_RSP_ACK);
1120 } else if (rlen == 0) {
1121 response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK);
1122 rlen = 1;
1123 } else {
1124 response[0] = p[0] | VDO_CMDT(CMDT_RSP_BUSY);
1125 rlen = 1;
1127 break;
1128 case CMDT_RSP_ACK:
1129 /* silently drop message if we are not connected */
1130 if (IS_ERR_OR_NULL(port->partner))
1131 break;
1133 switch (cmd) {
1134 case CMD_DISCOVER_IDENT:
1135 /* 6.4.4.3.1 */
1136 svdm_consume_identity(port, payload, cnt);
1137 response[0] = VDO(USB_SID_PD, 1, CMD_DISCOVER_SVID);
1138 rlen = 1;
1139 break;
1140 case CMD_DISCOVER_SVID:
1141 /* 6.4.4.3.2 */
1142 if (svdm_consume_svids(port, payload, cnt)) {
1143 response[0] = VDO(USB_SID_PD, 1,
1144 CMD_DISCOVER_SVID);
1145 rlen = 1;
1146 } else if (modep->nsvids && supports_modal(port)) {
1147 response[0] = VDO(modep->svids[0], 1,
1148 CMD_DISCOVER_MODES);
1149 rlen = 1;
1151 break;
1152 case CMD_DISCOVER_MODES:
1153 /* 6.4.4.3.3 */
1154 svdm_consume_modes(port, payload, cnt);
1155 modep->svid_index++;
1156 if (modep->svid_index < modep->nsvids) {
1157 u16 svid = modep->svids[modep->svid_index];
1158 response[0] = VDO(svid, 1, CMD_DISCOVER_MODES);
1159 rlen = 1;
1160 } else {
1161 tcpm_register_partner_altmodes(port);
1163 break;
1164 case CMD_ENTER_MODE:
1165 if (adev && pdev) {
1166 typec_altmode_update_active(pdev, true);
1168 if (typec_altmode_vdm(adev, p[0], &p[1], cnt)) {
1169 response[0] = VDO(adev->svid, 1,
1170 CMD_EXIT_MODE);
1171 response[0] |= VDO_OPOS(adev->mode);
1172 return 1;
1175 return 0;
1176 case CMD_EXIT_MODE:
1177 if (adev && pdev) {
1178 typec_altmode_update_active(pdev, false);
1180 /* Back to USB Operation */
1181 WARN_ON(typec_altmode_notify(adev,
1182 TYPEC_STATE_USB,
1183 NULL));
1185 break;
1186 default:
1187 break;
1189 break;
1190 case CMDT_RSP_NAK:
1191 switch (cmd) {
1192 case CMD_ENTER_MODE:
1193 /* Back to USB Operation */
1194 if (adev)
1195 WARN_ON(typec_altmode_notify(adev,
1196 TYPEC_STATE_USB,
1197 NULL));
1198 break;
1199 default:
1200 break;
1202 break;
1203 default:
1204 break;
1207 /* Informing the alternate mode drivers about everything */
1208 if (adev)
1209 typec_altmode_vdm(adev, p[0], &p[1], cnt);
1211 return rlen;
1214 static void tcpm_handle_vdm_request(struct tcpm_port *port,
1215 const __le32 *payload, int cnt)
1217 int rlen = 0;
1218 u32 response[8] = { };
1219 u32 p0 = le32_to_cpu(payload[0]);
1221 if (port->vdm_state == VDM_STATE_BUSY) {
1222 /* If UFP responded busy retry after timeout */
1223 if (PD_VDO_CMDT(p0) == CMDT_RSP_BUSY) {
1224 port->vdm_state = VDM_STATE_WAIT_RSP_BUSY;
1225 port->vdo_retry = (p0 & ~VDO_CMDT_MASK) |
1226 CMDT_INIT;
1227 mod_delayed_work(port->wq, &port->vdm_state_machine,
1228 msecs_to_jiffies(PD_T_VDM_BUSY));
1229 return;
1231 port->vdm_state = VDM_STATE_DONE;
1234 if (PD_VDO_SVDM(p0))
1235 rlen = tcpm_pd_svdm(port, payload, cnt, response);
1237 if (rlen > 0) {
1238 tcpm_queue_vdm(port, response[0], &response[1], rlen - 1);
1239 mod_delayed_work(port->wq, &port->vdm_state_machine, 0);
1243 static void tcpm_send_vdm(struct tcpm_port *port, u32 vid, int cmd,
1244 const u32 *data, int count)
1246 u32 header;
1248 if (WARN_ON(count > VDO_MAX_SIZE - 1))
1249 count = VDO_MAX_SIZE - 1;
1251 /* set VDM header with VID & CMD */
1252 header = VDO(vid, ((vid & USB_SID_PD) == USB_SID_PD) ?
1253 1 : (PD_VDO_CMD(cmd) <= CMD_ATTENTION), cmd);
1254 tcpm_queue_vdm(port, header, data, count);
1256 mod_delayed_work(port->wq, &port->vdm_state_machine, 0);
1259 static unsigned int vdm_ready_timeout(u32 vdm_hdr)
1261 unsigned int timeout;
1262 int cmd = PD_VDO_CMD(vdm_hdr);
1264 /* its not a structured VDM command */
1265 if (!PD_VDO_SVDM(vdm_hdr))
1266 return PD_T_VDM_UNSTRUCTURED;
1268 switch (PD_VDO_CMDT(vdm_hdr)) {
1269 case CMDT_INIT:
1270 if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE)
1271 timeout = PD_T_VDM_WAIT_MODE_E;
1272 else
1273 timeout = PD_T_VDM_SNDR_RSP;
1274 break;
1275 default:
1276 if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE)
1277 timeout = PD_T_VDM_E_MODE;
1278 else
1279 timeout = PD_T_VDM_RCVR_RSP;
1280 break;
1282 return timeout;
1285 static void vdm_run_state_machine(struct tcpm_port *port)
1287 struct pd_message msg;
1288 int i, res;
1290 switch (port->vdm_state) {
1291 case VDM_STATE_READY:
1292 /* Only transmit VDM if attached */
1293 if (!port->attached) {
1294 port->vdm_state = VDM_STATE_ERR_BUSY;
1295 break;
1299 * if there's traffic or we're not in PDO ready state don't send
1300 * a VDM.
1302 if (port->state != SRC_READY && port->state != SNK_READY)
1303 break;
1305 /* Prepare and send VDM */
1306 memset(&msg, 0, sizeof(msg));
1307 msg.header = PD_HEADER_LE(PD_DATA_VENDOR_DEF,
1308 port->pwr_role,
1309 port->data_role,
1310 port->negotiated_rev,
1311 port->message_id, port->vdo_count);
1312 for (i = 0; i < port->vdo_count; i++)
1313 msg.payload[i] = cpu_to_le32(port->vdo_data[i]);
1314 res = tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
1315 if (res < 0) {
1316 port->vdm_state = VDM_STATE_ERR_SEND;
1317 } else {
1318 unsigned long timeout;
1320 port->vdm_retries = 0;
1321 port->vdm_state = VDM_STATE_BUSY;
1322 timeout = vdm_ready_timeout(port->vdo_data[0]);
1323 mod_delayed_work(port->wq, &port->vdm_state_machine,
1324 timeout);
1326 break;
1327 case VDM_STATE_WAIT_RSP_BUSY:
1328 port->vdo_data[0] = port->vdo_retry;
1329 port->vdo_count = 1;
1330 port->vdm_state = VDM_STATE_READY;
1331 break;
1332 case VDM_STATE_BUSY:
1333 port->vdm_state = VDM_STATE_ERR_TMOUT;
1334 break;
1335 case VDM_STATE_ERR_SEND:
1337 * A partner which does not support USB PD will not reply,
1338 * so this is not a fatal error. At the same time, some
1339 * devices may not return GoodCRC under some circumstances,
1340 * so we need to retry.
1342 if (port->vdm_retries < 3) {
1343 tcpm_log(port, "VDM Tx error, retry");
1344 port->vdm_retries++;
1345 port->vdm_state = VDM_STATE_READY;
1347 break;
1348 default:
1349 break;
1353 static void vdm_state_machine_work(struct work_struct *work)
1355 struct tcpm_port *port = container_of(work, struct tcpm_port,
1356 vdm_state_machine.work);
1357 enum vdm_states prev_state;
1359 mutex_lock(&port->lock);
1362 * Continue running as long as the port is not busy and there was
1363 * a state change.
1365 do {
1366 prev_state = port->vdm_state;
1367 vdm_run_state_machine(port);
1368 } while (port->vdm_state != prev_state &&
1369 port->vdm_state != VDM_STATE_BUSY);
1371 mutex_unlock(&port->lock);
1374 enum pdo_err {
1375 PDO_NO_ERR,
1376 PDO_ERR_NO_VSAFE5V,
1377 PDO_ERR_VSAFE5V_NOT_FIRST,
1378 PDO_ERR_PDO_TYPE_NOT_IN_ORDER,
1379 PDO_ERR_FIXED_NOT_SORTED,
1380 PDO_ERR_VARIABLE_BATT_NOT_SORTED,
1381 PDO_ERR_DUPE_PDO,
1382 PDO_ERR_PPS_APDO_NOT_SORTED,
1383 PDO_ERR_DUPE_PPS_APDO,
1386 static const char * const pdo_err_msg[] = {
1387 [PDO_ERR_NO_VSAFE5V] =
1388 " err: source/sink caps should atleast have vSafe5V",
1389 [PDO_ERR_VSAFE5V_NOT_FIRST] =
1390 " err: vSafe5V Fixed Supply Object Shall always be the first object",
1391 [PDO_ERR_PDO_TYPE_NOT_IN_ORDER] =
1392 " err: PDOs should be in the following order: Fixed; Battery; Variable",
1393 [PDO_ERR_FIXED_NOT_SORTED] =
1394 " err: Fixed supply pdos should be in increasing order of their fixed voltage",
1395 [PDO_ERR_VARIABLE_BATT_NOT_SORTED] =
1396 " err: Variable/Battery supply pdos should be in increasing order of their minimum voltage",
1397 [PDO_ERR_DUPE_PDO] =
1398 " err: Variable/Batt supply pdos cannot have same min/max voltage",
1399 [PDO_ERR_PPS_APDO_NOT_SORTED] =
1400 " err: Programmable power supply apdos should be in increasing order of their maximum voltage",
1401 [PDO_ERR_DUPE_PPS_APDO] =
1402 " err: Programmable power supply apdos cannot have same min/max voltage and max current",
1405 static enum pdo_err tcpm_caps_err(struct tcpm_port *port, const u32 *pdo,
1406 unsigned int nr_pdo)
1408 unsigned int i;
1410 /* Should at least contain vSafe5v */
1411 if (nr_pdo < 1)
1412 return PDO_ERR_NO_VSAFE5V;
1414 /* The vSafe5V Fixed Supply Object Shall always be the first object */
1415 if (pdo_type(pdo[0]) != PDO_TYPE_FIXED ||
1416 pdo_fixed_voltage(pdo[0]) != VSAFE5V)
1417 return PDO_ERR_VSAFE5V_NOT_FIRST;
1419 for (i = 1; i < nr_pdo; i++) {
1420 if (pdo_type(pdo[i]) < pdo_type(pdo[i - 1])) {
1421 return PDO_ERR_PDO_TYPE_NOT_IN_ORDER;
1422 } else if (pdo_type(pdo[i]) == pdo_type(pdo[i - 1])) {
1423 enum pd_pdo_type type = pdo_type(pdo[i]);
1425 switch (type) {
1427 * The remaining Fixed Supply Objects, if
1428 * present, shall be sent in voltage order;
1429 * lowest to highest.
1431 case PDO_TYPE_FIXED:
1432 if (pdo_fixed_voltage(pdo[i]) <=
1433 pdo_fixed_voltage(pdo[i - 1]))
1434 return PDO_ERR_FIXED_NOT_SORTED;
1435 break;
1437 * The Battery Supply Objects and Variable
1438 * supply, if present shall be sent in Minimum
1439 * Voltage order; lowest to highest.
1441 case PDO_TYPE_VAR:
1442 case PDO_TYPE_BATT:
1443 if (pdo_min_voltage(pdo[i]) <
1444 pdo_min_voltage(pdo[i - 1]))
1445 return PDO_ERR_VARIABLE_BATT_NOT_SORTED;
1446 else if ((pdo_min_voltage(pdo[i]) ==
1447 pdo_min_voltage(pdo[i - 1])) &&
1448 (pdo_max_voltage(pdo[i]) ==
1449 pdo_max_voltage(pdo[i - 1])))
1450 return PDO_ERR_DUPE_PDO;
1451 break;
1453 * The Programmable Power Supply APDOs, if present,
1454 * shall be sent in Maximum Voltage order;
1455 * lowest to highest.
1457 case PDO_TYPE_APDO:
1458 if (pdo_apdo_type(pdo[i]) != APDO_TYPE_PPS)
1459 break;
1461 if (pdo_pps_apdo_max_voltage(pdo[i]) <
1462 pdo_pps_apdo_max_voltage(pdo[i - 1]))
1463 return PDO_ERR_PPS_APDO_NOT_SORTED;
1464 else if (pdo_pps_apdo_min_voltage(pdo[i]) ==
1465 pdo_pps_apdo_min_voltage(pdo[i - 1]) &&
1466 pdo_pps_apdo_max_voltage(pdo[i]) ==
1467 pdo_pps_apdo_max_voltage(pdo[i - 1]) &&
1468 pdo_pps_apdo_max_current(pdo[i]) ==
1469 pdo_pps_apdo_max_current(pdo[i - 1]))
1470 return PDO_ERR_DUPE_PPS_APDO;
1471 break;
1472 default:
1473 tcpm_log_force(port, " Unknown pdo type");
1478 return PDO_NO_ERR;
1481 static int tcpm_validate_caps(struct tcpm_port *port, const u32 *pdo,
1482 unsigned int nr_pdo)
1484 enum pdo_err err_index = tcpm_caps_err(port, pdo, nr_pdo);
1486 if (err_index != PDO_NO_ERR) {
1487 tcpm_log_force(port, " %s", pdo_err_msg[err_index]);
1488 return -EINVAL;
1491 return 0;
1494 static int tcpm_altmode_enter(struct typec_altmode *altmode)
1496 struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
1497 u32 header;
1499 mutex_lock(&port->lock);
1500 header = VDO(altmode->svid, 1, CMD_ENTER_MODE);
1501 header |= VDO_OPOS(altmode->mode);
1503 tcpm_queue_vdm(port, header, NULL, 0);
1504 mod_delayed_work(port->wq, &port->vdm_state_machine, 0);
1505 mutex_unlock(&port->lock);
1507 return 0;
1510 static int tcpm_altmode_exit(struct typec_altmode *altmode)
1512 struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
1513 u32 header;
1515 mutex_lock(&port->lock);
1516 header = VDO(altmode->svid, 1, CMD_EXIT_MODE);
1517 header |= VDO_OPOS(altmode->mode);
1519 tcpm_queue_vdm(port, header, NULL, 0);
1520 mod_delayed_work(port->wq, &port->vdm_state_machine, 0);
1521 mutex_unlock(&port->lock);
1523 return 0;
1526 static int tcpm_altmode_vdm(struct typec_altmode *altmode,
1527 u32 header, const u32 *data, int count)
1529 struct tcpm_port *port = typec_altmode_get_drvdata(altmode);
1531 mutex_lock(&port->lock);
1532 tcpm_queue_vdm(port, header, data, count - 1);
1533 mod_delayed_work(port->wq, &port->vdm_state_machine, 0);
1534 mutex_unlock(&port->lock);
1536 return 0;
1539 static const struct typec_altmode_ops tcpm_altmode_ops = {
1540 .enter = tcpm_altmode_enter,
1541 .exit = tcpm_altmode_exit,
1542 .vdm = tcpm_altmode_vdm,
1546 * PD (data, control) command handling functions
1548 static inline enum tcpm_state ready_state(struct tcpm_port *port)
1550 if (port->pwr_role == TYPEC_SOURCE)
1551 return SRC_READY;
1552 else
1553 return SNK_READY;
1556 static int tcpm_pd_send_control(struct tcpm_port *port,
1557 enum pd_ctrl_msg_type type);
1559 static void tcpm_handle_alert(struct tcpm_port *port, const __le32 *payload,
1560 int cnt)
1562 u32 p0 = le32_to_cpu(payload[0]);
1563 unsigned int type = usb_pd_ado_type(p0);
1565 if (!type) {
1566 tcpm_log(port, "Alert message received with no type");
1567 return;
1570 /* Just handling non-battery alerts for now */
1571 if (!(type & USB_PD_ADO_TYPE_BATT_STATUS_CHANGE)) {
1572 switch (port->state) {
1573 case SRC_READY:
1574 case SNK_READY:
1575 tcpm_set_state(port, GET_STATUS_SEND, 0);
1576 break;
1577 default:
1578 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
1579 break;
1584 static void tcpm_pd_data_request(struct tcpm_port *port,
1585 const struct pd_message *msg)
1587 enum pd_data_msg_type type = pd_header_type_le(msg->header);
1588 unsigned int cnt = pd_header_cnt_le(msg->header);
1589 unsigned int rev = pd_header_rev_le(msg->header);
1590 unsigned int i;
1592 switch (type) {
1593 case PD_DATA_SOURCE_CAP:
1594 if (port->pwr_role != TYPEC_SINK)
1595 break;
1597 for (i = 0; i < cnt; i++)
1598 port->source_caps[i] = le32_to_cpu(msg->payload[i]);
1600 port->nr_source_caps = cnt;
1602 tcpm_log_source_caps(port);
1604 tcpm_validate_caps(port, port->source_caps,
1605 port->nr_source_caps);
1608 * Adjust revision in subsequent message headers, as required,
1609 * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't
1610 * support Rev 1.0 so just do nothing in that scenario.
1612 if (rev == PD_REV10)
1613 break;
1615 if (rev < PD_MAX_REV)
1616 port->negotiated_rev = rev;
1619 * This message may be received even if VBUS is not
1620 * present. This is quite unexpected; see USB PD
1621 * specification, sections 8.3.3.6.3.1 and 8.3.3.6.3.2.
1622 * However, at the same time, we must be ready to
1623 * receive this message and respond to it 15ms after
1624 * receiving PS_RDY during power swap operations, no matter
1625 * if VBUS is available or not (USB PD specification,
1626 * section 6.5.9.2).
1627 * So we need to accept the message either way,
1628 * but be prepared to keep waiting for VBUS after it was
1629 * handled.
1631 tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
1632 break;
1633 case PD_DATA_REQUEST:
1634 if (port->pwr_role != TYPEC_SOURCE ||
1635 cnt != 1) {
1636 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1637 break;
1641 * Adjust revision in subsequent message headers, as required,
1642 * to comply with 6.2.1.1.5 of the USB PD 3.0 spec. We don't
1643 * support Rev 1.0 so just reject in that scenario.
1645 if (rev == PD_REV10) {
1646 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1647 break;
1650 if (rev < PD_MAX_REV)
1651 port->negotiated_rev = rev;
1653 port->sink_request = le32_to_cpu(msg->payload[0]);
1654 tcpm_set_state(port, SRC_NEGOTIATE_CAPABILITIES, 0);
1655 break;
1656 case PD_DATA_SINK_CAP:
1657 /* We don't do anything with this at the moment... */
1658 for (i = 0; i < cnt; i++)
1659 port->sink_caps[i] = le32_to_cpu(msg->payload[i]);
1660 port->nr_sink_caps = cnt;
1661 break;
1662 case PD_DATA_VENDOR_DEF:
1663 tcpm_handle_vdm_request(port, msg->payload, cnt);
1664 break;
1665 case PD_DATA_BIST:
1666 if (port->state == SRC_READY || port->state == SNK_READY) {
1667 port->bist_request = le32_to_cpu(msg->payload[0]);
1668 tcpm_set_state(port, BIST_RX, 0);
1670 break;
1671 case PD_DATA_ALERT:
1672 tcpm_handle_alert(port, msg->payload, cnt);
1673 break;
1674 case PD_DATA_BATT_STATUS:
1675 case PD_DATA_GET_COUNTRY_INFO:
1676 /* Currently unsupported */
1677 tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP);
1678 break;
1679 default:
1680 tcpm_log(port, "Unhandled data message type %#x", type);
1681 break;
1685 static void tcpm_pps_complete(struct tcpm_port *port, int result)
1687 if (port->pps_pending) {
1688 port->pps_status = result;
1689 port->pps_pending = false;
1690 complete(&port->pps_complete);
1694 static void tcpm_pd_ctrl_request(struct tcpm_port *port,
1695 const struct pd_message *msg)
1697 enum pd_ctrl_msg_type type = pd_header_type_le(msg->header);
1698 enum tcpm_state next_state;
1700 switch (type) {
1701 case PD_CTRL_GOOD_CRC:
1702 case PD_CTRL_PING:
1703 break;
1704 case PD_CTRL_GET_SOURCE_CAP:
1705 switch (port->state) {
1706 case SRC_READY:
1707 case SNK_READY:
1708 tcpm_queue_message(port, PD_MSG_DATA_SOURCE_CAP);
1709 break;
1710 default:
1711 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1712 break;
1714 break;
1715 case PD_CTRL_GET_SINK_CAP:
1716 switch (port->state) {
1717 case SRC_READY:
1718 case SNK_READY:
1719 tcpm_queue_message(port, PD_MSG_DATA_SINK_CAP);
1720 break;
1721 default:
1722 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1723 break;
1725 break;
1726 case PD_CTRL_GOTO_MIN:
1727 break;
1728 case PD_CTRL_PS_RDY:
1729 switch (port->state) {
1730 case SNK_TRANSITION_SINK:
1731 if (port->vbus_present) {
1732 tcpm_set_current_limit(port,
1733 port->current_limit,
1734 port->supply_voltage);
1735 port->explicit_contract = true;
1736 tcpm_set_state(port, SNK_READY, 0);
1737 } else {
1739 * Seen after power swap. Keep waiting for VBUS
1740 * in a transitional state.
1742 tcpm_set_state(port,
1743 SNK_TRANSITION_SINK_VBUS, 0);
1745 break;
1746 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED:
1747 tcpm_set_state(port, PR_SWAP_SRC_SNK_SINK_ON, 0);
1748 break;
1749 case PR_SWAP_SNK_SRC_SINK_OFF:
1750 tcpm_set_state(port, PR_SWAP_SNK_SRC_SOURCE_ON, 0);
1751 break;
1752 case VCONN_SWAP_WAIT_FOR_VCONN:
1753 tcpm_set_state(port, VCONN_SWAP_TURN_OFF_VCONN, 0);
1754 break;
1755 default:
1756 break;
1758 break;
1759 case PD_CTRL_REJECT:
1760 case PD_CTRL_WAIT:
1761 case PD_CTRL_NOT_SUPP:
1762 switch (port->state) {
1763 case SNK_NEGOTIATE_CAPABILITIES:
1764 /* USB PD specification, Figure 8-43 */
1765 if (port->explicit_contract)
1766 next_state = SNK_READY;
1767 else
1768 next_state = SNK_WAIT_CAPABILITIES;
1769 tcpm_set_state(port, next_state, 0);
1770 break;
1771 case SNK_NEGOTIATE_PPS_CAPABILITIES:
1772 /* Revert data back from any requested PPS updates */
1773 port->pps_data.out_volt = port->supply_voltage;
1774 port->pps_data.op_curr = port->current_limit;
1775 port->pps_status = (type == PD_CTRL_WAIT ?
1776 -EAGAIN : -EOPNOTSUPP);
1777 tcpm_set_state(port, SNK_READY, 0);
1778 break;
1779 case DR_SWAP_SEND:
1780 port->swap_status = (type == PD_CTRL_WAIT ?
1781 -EAGAIN : -EOPNOTSUPP);
1782 tcpm_set_state(port, DR_SWAP_CANCEL, 0);
1783 break;
1784 case PR_SWAP_SEND:
1785 port->swap_status = (type == PD_CTRL_WAIT ?
1786 -EAGAIN : -EOPNOTSUPP);
1787 tcpm_set_state(port, PR_SWAP_CANCEL, 0);
1788 break;
1789 case VCONN_SWAP_SEND:
1790 port->swap_status = (type == PD_CTRL_WAIT ?
1791 -EAGAIN : -EOPNOTSUPP);
1792 tcpm_set_state(port, VCONN_SWAP_CANCEL, 0);
1793 break;
1794 default:
1795 break;
1797 break;
1798 case PD_CTRL_ACCEPT:
1799 switch (port->state) {
1800 case SNK_NEGOTIATE_CAPABILITIES:
1801 port->pps_data.active = false;
1802 tcpm_set_state(port, SNK_TRANSITION_SINK, 0);
1803 break;
1804 case SNK_NEGOTIATE_PPS_CAPABILITIES:
1805 port->pps_data.active = true;
1806 port->supply_voltage = port->pps_data.out_volt;
1807 port->current_limit = port->pps_data.op_curr;
1808 tcpm_set_state(port, SNK_TRANSITION_SINK, 0);
1809 break;
1810 case SOFT_RESET_SEND:
1811 port->message_id = 0;
1812 port->rx_msgid = -1;
1813 if (port->pwr_role == TYPEC_SOURCE)
1814 next_state = SRC_SEND_CAPABILITIES;
1815 else
1816 next_state = SNK_WAIT_CAPABILITIES;
1817 tcpm_set_state(port, next_state, 0);
1818 break;
1819 case DR_SWAP_SEND:
1820 tcpm_set_state(port, DR_SWAP_CHANGE_DR, 0);
1821 break;
1822 case PR_SWAP_SEND:
1823 tcpm_set_state(port, PR_SWAP_START, 0);
1824 break;
1825 case VCONN_SWAP_SEND:
1826 tcpm_set_state(port, VCONN_SWAP_START, 0);
1827 break;
1828 default:
1829 break;
1831 break;
1832 case PD_CTRL_SOFT_RESET:
1833 tcpm_set_state(port, SOFT_RESET, 0);
1834 break;
1835 case PD_CTRL_DR_SWAP:
1836 if (port->port_type != TYPEC_PORT_DRP) {
1837 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1838 break;
1841 * XXX
1842 * 6.3.9: If an alternate mode is active, a request to swap
1843 * alternate modes shall trigger a port reset.
1845 switch (port->state) {
1846 case SRC_READY:
1847 case SNK_READY:
1848 tcpm_set_state(port, DR_SWAP_ACCEPT, 0);
1849 break;
1850 default:
1851 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
1852 break;
1854 break;
1855 case PD_CTRL_PR_SWAP:
1856 if (port->port_type != TYPEC_PORT_DRP) {
1857 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1858 break;
1860 switch (port->state) {
1861 case SRC_READY:
1862 case SNK_READY:
1863 tcpm_set_state(port, PR_SWAP_ACCEPT, 0);
1864 break;
1865 default:
1866 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
1867 break;
1869 break;
1870 case PD_CTRL_VCONN_SWAP:
1871 switch (port->state) {
1872 case SRC_READY:
1873 case SNK_READY:
1874 tcpm_set_state(port, VCONN_SWAP_ACCEPT, 0);
1875 break;
1876 default:
1877 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
1878 break;
1880 break;
1881 case PD_CTRL_GET_SOURCE_CAP_EXT:
1882 case PD_CTRL_GET_STATUS:
1883 case PD_CTRL_FR_SWAP:
1884 case PD_CTRL_GET_PPS_STATUS:
1885 case PD_CTRL_GET_COUNTRY_CODES:
1886 /* Currently not supported */
1887 tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP);
1888 break;
1889 default:
1890 tcpm_log(port, "Unhandled ctrl message type %#x", type);
1891 break;
1895 static void tcpm_pd_ext_msg_request(struct tcpm_port *port,
1896 const struct pd_message *msg)
1898 enum pd_ext_msg_type type = pd_header_type_le(msg->header);
1899 unsigned int data_size = pd_ext_header_data_size_le(msg->ext_msg.header);
1901 if (!(msg->ext_msg.header & PD_EXT_HDR_CHUNKED)) {
1902 tcpm_log(port, "Unchunked extended messages unsupported");
1903 return;
1906 if (data_size > PD_EXT_MAX_CHUNK_DATA) {
1907 tcpm_log(port, "Chunk handling not yet supported");
1908 return;
1911 switch (type) {
1912 case PD_EXT_STATUS:
1914 * If PPS related events raised then get PPS status to clear
1915 * (see USB PD 3.0 Spec, 6.5.2.4)
1917 if (msg->ext_msg.data[USB_PD_EXT_SDB_EVENT_FLAGS] &
1918 USB_PD_EXT_SDB_PPS_EVENTS)
1919 tcpm_set_state(port, GET_PPS_STATUS_SEND, 0);
1920 else
1921 tcpm_set_state(port, ready_state(port), 0);
1922 break;
1923 case PD_EXT_PPS_STATUS:
1925 * For now the PPS status message is used to clear events
1926 * and nothing more.
1928 tcpm_set_state(port, ready_state(port), 0);
1929 break;
1930 case PD_EXT_SOURCE_CAP_EXT:
1931 case PD_EXT_GET_BATT_CAP:
1932 case PD_EXT_GET_BATT_STATUS:
1933 case PD_EXT_BATT_CAP:
1934 case PD_EXT_GET_MANUFACTURER_INFO:
1935 case PD_EXT_MANUFACTURER_INFO:
1936 case PD_EXT_SECURITY_REQUEST:
1937 case PD_EXT_SECURITY_RESPONSE:
1938 case PD_EXT_FW_UPDATE_REQUEST:
1939 case PD_EXT_FW_UPDATE_RESPONSE:
1940 case PD_EXT_COUNTRY_INFO:
1941 case PD_EXT_COUNTRY_CODES:
1942 tcpm_queue_message(port, PD_MSG_CTRL_NOT_SUPP);
1943 break;
1944 default:
1945 tcpm_log(port, "Unhandled extended message type %#x", type);
1946 break;
1950 static void tcpm_pd_rx_handler(struct work_struct *work)
1952 struct pd_rx_event *event = container_of(work,
1953 struct pd_rx_event, work);
1954 const struct pd_message *msg = &event->msg;
1955 unsigned int cnt = pd_header_cnt_le(msg->header);
1956 struct tcpm_port *port = event->port;
1958 mutex_lock(&port->lock);
1960 tcpm_log(port, "PD RX, header: %#x [%d]", le16_to_cpu(msg->header),
1961 port->attached);
1963 if (port->attached) {
1964 enum pd_ctrl_msg_type type = pd_header_type_le(msg->header);
1965 unsigned int msgid = pd_header_msgid_le(msg->header);
1968 * USB PD standard, 6.6.1.2:
1969 * "... if MessageID value in a received Message is the
1970 * same as the stored value, the receiver shall return a
1971 * GoodCRC Message with that MessageID value and drop
1972 * the Message (this is a retry of an already received
1973 * Message). Note: this shall not apply to the Soft_Reset
1974 * Message which always has a MessageID value of zero."
1976 if (msgid == port->rx_msgid && type != PD_CTRL_SOFT_RESET)
1977 goto done;
1978 port->rx_msgid = msgid;
1981 * If both ends believe to be DFP/host, we have a data role
1982 * mismatch.
1984 if (!!(le16_to_cpu(msg->header) & PD_HEADER_DATA_ROLE) ==
1985 (port->data_role == TYPEC_HOST)) {
1986 tcpm_log(port,
1987 "Data role mismatch, initiating error recovery");
1988 tcpm_set_state(port, ERROR_RECOVERY, 0);
1989 } else {
1990 if (msg->header & PD_HEADER_EXT_HDR)
1991 tcpm_pd_ext_msg_request(port, msg);
1992 else if (cnt)
1993 tcpm_pd_data_request(port, msg);
1994 else
1995 tcpm_pd_ctrl_request(port, msg);
1999 done:
2000 mutex_unlock(&port->lock);
2001 kfree(event);
2004 void tcpm_pd_receive(struct tcpm_port *port, const struct pd_message *msg)
2006 struct pd_rx_event *event;
2008 event = kzalloc(sizeof(*event), GFP_ATOMIC);
2009 if (!event)
2010 return;
2012 INIT_WORK(&event->work, tcpm_pd_rx_handler);
2013 event->port = port;
2014 memcpy(&event->msg, msg, sizeof(*msg));
2015 queue_work(port->wq, &event->work);
2017 EXPORT_SYMBOL_GPL(tcpm_pd_receive);
2019 static int tcpm_pd_send_control(struct tcpm_port *port,
2020 enum pd_ctrl_msg_type type)
2022 struct pd_message msg;
2024 memset(&msg, 0, sizeof(msg));
2025 msg.header = PD_HEADER_LE(type, port->pwr_role,
2026 port->data_role,
2027 port->negotiated_rev,
2028 port->message_id, 0);
2030 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
2034 * Send queued message without affecting state.
2035 * Return true if state machine should go back to sleep,
2036 * false otherwise.
2038 static bool tcpm_send_queued_message(struct tcpm_port *port)
2040 enum pd_msg_request queued_message;
2042 do {
2043 queued_message = port->queued_message;
2044 port->queued_message = PD_MSG_NONE;
2046 switch (queued_message) {
2047 case PD_MSG_CTRL_WAIT:
2048 tcpm_pd_send_control(port, PD_CTRL_WAIT);
2049 break;
2050 case PD_MSG_CTRL_REJECT:
2051 tcpm_pd_send_control(port, PD_CTRL_REJECT);
2052 break;
2053 case PD_MSG_CTRL_NOT_SUPP:
2054 tcpm_pd_send_control(port, PD_CTRL_NOT_SUPP);
2055 break;
2056 case PD_MSG_DATA_SINK_CAP:
2057 tcpm_pd_send_sink_caps(port);
2058 break;
2059 case PD_MSG_DATA_SOURCE_CAP:
2060 tcpm_pd_send_source_caps(port);
2061 break;
2062 default:
2063 break;
2065 } while (port->queued_message != PD_MSG_NONE);
2067 if (port->delayed_state != INVALID_STATE) {
2068 if (time_is_after_jiffies(port->delayed_runtime)) {
2069 mod_delayed_work(port->wq, &port->state_machine,
2070 port->delayed_runtime - jiffies);
2071 return true;
2073 port->delayed_state = INVALID_STATE;
2075 return false;
2078 static int tcpm_pd_check_request(struct tcpm_port *port)
2080 u32 pdo, rdo = port->sink_request;
2081 unsigned int max, op, pdo_max, index;
2082 enum pd_pdo_type type;
2084 index = rdo_index(rdo);
2085 if (!index || index > port->nr_src_pdo)
2086 return -EINVAL;
2088 pdo = port->src_pdo[index - 1];
2089 type = pdo_type(pdo);
2090 switch (type) {
2091 case PDO_TYPE_FIXED:
2092 case PDO_TYPE_VAR:
2093 max = rdo_max_current(rdo);
2094 op = rdo_op_current(rdo);
2095 pdo_max = pdo_max_current(pdo);
2097 if (op > pdo_max)
2098 return -EINVAL;
2099 if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH))
2100 return -EINVAL;
2102 if (type == PDO_TYPE_FIXED)
2103 tcpm_log(port,
2104 "Requested %u mV, %u mA for %u / %u mA",
2105 pdo_fixed_voltage(pdo), pdo_max, op, max);
2106 else
2107 tcpm_log(port,
2108 "Requested %u -> %u mV, %u mA for %u / %u mA",
2109 pdo_min_voltage(pdo), pdo_max_voltage(pdo),
2110 pdo_max, op, max);
2111 break;
2112 case PDO_TYPE_BATT:
2113 max = rdo_max_power(rdo);
2114 op = rdo_op_power(rdo);
2115 pdo_max = pdo_max_power(pdo);
2117 if (op > pdo_max)
2118 return -EINVAL;
2119 if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH))
2120 return -EINVAL;
2121 tcpm_log(port,
2122 "Requested %u -> %u mV, %u mW for %u / %u mW",
2123 pdo_min_voltage(pdo), pdo_max_voltage(pdo),
2124 pdo_max, op, max);
2125 break;
2126 default:
2127 return -EINVAL;
2130 port->op_vsafe5v = index == 1;
2132 return 0;
2135 #define min_power(x, y) min(pdo_max_power(x), pdo_max_power(y))
2136 #define min_current(x, y) min(pdo_max_current(x), pdo_max_current(y))
2138 static int tcpm_pd_select_pdo(struct tcpm_port *port, int *sink_pdo,
2139 int *src_pdo)
2141 unsigned int i, j, max_src_mv = 0, min_src_mv = 0, max_mw = 0,
2142 max_mv = 0, src_mw = 0, src_ma = 0, max_snk_mv = 0,
2143 min_snk_mv = 0;
2144 int ret = -EINVAL;
2146 port->pps_data.supported = false;
2147 port->usb_type = POWER_SUPPLY_USB_TYPE_PD;
2150 * Select the source PDO providing the most power which has a
2151 * matchig sink cap.
2153 for (i = 0; i < port->nr_source_caps; i++) {
2154 u32 pdo = port->source_caps[i];
2155 enum pd_pdo_type type = pdo_type(pdo);
2157 switch (type) {
2158 case PDO_TYPE_FIXED:
2159 max_src_mv = pdo_fixed_voltage(pdo);
2160 min_src_mv = max_src_mv;
2161 break;
2162 case PDO_TYPE_BATT:
2163 case PDO_TYPE_VAR:
2164 max_src_mv = pdo_max_voltage(pdo);
2165 min_src_mv = pdo_min_voltage(pdo);
2166 break;
2167 case PDO_TYPE_APDO:
2168 if (pdo_apdo_type(pdo) == APDO_TYPE_PPS) {
2169 port->pps_data.supported = true;
2170 port->usb_type =
2171 POWER_SUPPLY_USB_TYPE_PD_PPS;
2173 continue;
2174 default:
2175 tcpm_log(port, "Invalid source PDO type, ignoring");
2176 continue;
2179 switch (type) {
2180 case PDO_TYPE_FIXED:
2181 case PDO_TYPE_VAR:
2182 src_ma = pdo_max_current(pdo);
2183 src_mw = src_ma * min_src_mv / 1000;
2184 break;
2185 case PDO_TYPE_BATT:
2186 src_mw = pdo_max_power(pdo);
2187 break;
2188 case PDO_TYPE_APDO:
2189 continue;
2190 default:
2191 tcpm_log(port, "Invalid source PDO type, ignoring");
2192 continue;
2195 for (j = 0; j < port->nr_snk_pdo; j++) {
2196 pdo = port->snk_pdo[j];
2198 switch (pdo_type(pdo)) {
2199 case PDO_TYPE_FIXED:
2200 max_snk_mv = pdo_fixed_voltage(pdo);
2201 min_snk_mv = max_snk_mv;
2202 break;
2203 case PDO_TYPE_BATT:
2204 case PDO_TYPE_VAR:
2205 max_snk_mv = pdo_max_voltage(pdo);
2206 min_snk_mv = pdo_min_voltage(pdo);
2207 break;
2208 case PDO_TYPE_APDO:
2209 continue;
2210 default:
2211 tcpm_log(port, "Invalid sink PDO type, ignoring");
2212 continue;
2215 if (max_src_mv <= max_snk_mv &&
2216 min_src_mv >= min_snk_mv) {
2217 /* Prefer higher voltages if available */
2218 if ((src_mw == max_mw && min_src_mv > max_mv) ||
2219 src_mw > max_mw) {
2220 *src_pdo = i;
2221 *sink_pdo = j;
2222 max_mw = src_mw;
2223 max_mv = min_src_mv;
2224 ret = 0;
2230 return ret;
2233 #define min_pps_apdo_current(x, y) \
2234 min(pdo_pps_apdo_max_current(x), pdo_pps_apdo_max_current(y))
2236 static unsigned int tcpm_pd_select_pps_apdo(struct tcpm_port *port)
2238 unsigned int i, j, max_mw = 0, max_mv = 0;
2239 unsigned int min_src_mv, max_src_mv, src_ma, src_mw;
2240 unsigned int min_snk_mv, max_snk_mv, snk_ma;
2241 u32 pdo;
2242 unsigned int src_pdo = 0, snk_pdo = 0;
2245 * Select the source PPS APDO providing the most power while staying
2246 * within the board's limits. We skip the first PDO as this is always
2247 * 5V 3A.
2249 for (i = 1; i < port->nr_source_caps; ++i) {
2250 pdo = port->source_caps[i];
2252 switch (pdo_type(pdo)) {
2253 case PDO_TYPE_APDO:
2254 if (pdo_apdo_type(pdo) != APDO_TYPE_PPS) {
2255 tcpm_log(port, "Not PPS APDO (source), ignoring");
2256 continue;
2259 min_src_mv = pdo_pps_apdo_min_voltage(pdo);
2260 max_src_mv = pdo_pps_apdo_max_voltage(pdo);
2261 src_ma = pdo_pps_apdo_max_current(pdo);
2262 src_mw = (src_ma * max_src_mv) / 1000;
2265 * Now search through the sink PDOs to find a matching
2266 * PPS APDO. Again skip the first sink PDO as this will
2267 * always be 5V 3A.
2269 for (j = 1; j < port->nr_snk_pdo; j++) {
2270 pdo = port->snk_pdo[j];
2272 switch (pdo_type(pdo)) {
2273 case PDO_TYPE_APDO:
2274 if (pdo_apdo_type(pdo) != APDO_TYPE_PPS) {
2275 tcpm_log(port,
2276 "Not PPS APDO (sink), ignoring");
2277 continue;
2280 min_snk_mv =
2281 pdo_pps_apdo_min_voltage(pdo);
2282 max_snk_mv =
2283 pdo_pps_apdo_max_voltage(pdo);
2284 snk_ma =
2285 pdo_pps_apdo_max_current(pdo);
2286 break;
2287 default:
2288 tcpm_log(port,
2289 "Not APDO type (sink), ignoring");
2290 continue;
2293 if (max_src_mv <= max_snk_mv &&
2294 min_src_mv >= min_snk_mv) {
2295 /* Prefer higher voltages if available */
2296 if ((src_mw == max_mw &&
2297 min_src_mv > max_mv) ||
2298 src_mw > max_mw) {
2299 src_pdo = i;
2300 snk_pdo = j;
2301 max_mw = src_mw;
2302 max_mv = max_src_mv;
2307 break;
2308 default:
2309 tcpm_log(port, "Not APDO type (source), ignoring");
2310 continue;
2314 if (src_pdo) {
2315 pdo = port->source_caps[src_pdo];
2317 port->pps_data.min_volt = pdo_pps_apdo_min_voltage(pdo);
2318 port->pps_data.max_volt = pdo_pps_apdo_max_voltage(pdo);
2319 port->pps_data.max_curr =
2320 min_pps_apdo_current(pdo, port->snk_pdo[snk_pdo]);
2321 port->pps_data.out_volt =
2322 min(pdo_pps_apdo_max_voltage(pdo), port->pps_data.out_volt);
2323 port->pps_data.op_curr =
2324 min(port->pps_data.max_curr, port->pps_data.op_curr);
2327 return src_pdo;
2330 static int tcpm_pd_build_request(struct tcpm_port *port, u32 *rdo)
2332 unsigned int mv, ma, mw, flags;
2333 unsigned int max_ma, max_mw;
2334 enum pd_pdo_type type;
2335 u32 pdo, matching_snk_pdo;
2336 int src_pdo_index = 0;
2337 int snk_pdo_index = 0;
2338 int ret;
2340 ret = tcpm_pd_select_pdo(port, &snk_pdo_index, &src_pdo_index);
2341 if (ret < 0)
2342 return ret;
2344 pdo = port->source_caps[src_pdo_index];
2345 matching_snk_pdo = port->snk_pdo[snk_pdo_index];
2346 type = pdo_type(pdo);
2348 switch (type) {
2349 case PDO_TYPE_FIXED:
2350 mv = pdo_fixed_voltage(pdo);
2351 break;
2352 case PDO_TYPE_BATT:
2353 case PDO_TYPE_VAR:
2354 mv = pdo_min_voltage(pdo);
2355 break;
2356 default:
2357 tcpm_log(port, "Invalid PDO selected!");
2358 return -EINVAL;
2361 /* Select maximum available current within the sink pdo's limit */
2362 if (type == PDO_TYPE_BATT) {
2363 mw = min_power(pdo, matching_snk_pdo);
2364 ma = 1000 * mw / mv;
2365 } else {
2366 ma = min_current(pdo, matching_snk_pdo);
2367 mw = ma * mv / 1000;
2370 flags = RDO_USB_COMM | RDO_NO_SUSPEND;
2372 /* Set mismatch bit if offered power is less than operating power */
2373 max_ma = ma;
2374 max_mw = mw;
2375 if (mw < port->operating_snk_mw) {
2376 flags |= RDO_CAP_MISMATCH;
2377 if (type == PDO_TYPE_BATT &&
2378 (pdo_max_power(matching_snk_pdo) > pdo_max_power(pdo)))
2379 max_mw = pdo_max_power(matching_snk_pdo);
2380 else if (pdo_max_current(matching_snk_pdo) >
2381 pdo_max_current(pdo))
2382 max_ma = pdo_max_current(matching_snk_pdo);
2385 tcpm_log(port, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d",
2386 port->cc_req, port->cc1, port->cc2, port->vbus_source,
2387 port->vconn_role == TYPEC_SOURCE ? "source" : "sink",
2388 port->polarity);
2390 if (type == PDO_TYPE_BATT) {
2391 *rdo = RDO_BATT(src_pdo_index + 1, mw, max_mw, flags);
2393 tcpm_log(port, "Requesting PDO %d: %u mV, %u mW%s",
2394 src_pdo_index, mv, mw,
2395 flags & RDO_CAP_MISMATCH ? " [mismatch]" : "");
2396 } else {
2397 *rdo = RDO_FIXED(src_pdo_index + 1, ma, max_ma, flags);
2399 tcpm_log(port, "Requesting PDO %d: %u mV, %u mA%s",
2400 src_pdo_index, mv, ma,
2401 flags & RDO_CAP_MISMATCH ? " [mismatch]" : "");
2404 port->current_limit = ma;
2405 port->supply_voltage = mv;
2407 return 0;
2410 static int tcpm_pd_send_request(struct tcpm_port *port)
2412 struct pd_message msg;
2413 int ret;
2414 u32 rdo;
2416 ret = tcpm_pd_build_request(port, &rdo);
2417 if (ret < 0)
2418 return ret;
2420 memset(&msg, 0, sizeof(msg));
2421 msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
2422 port->pwr_role,
2423 port->data_role,
2424 port->negotiated_rev,
2425 port->message_id, 1);
2426 msg.payload[0] = cpu_to_le32(rdo);
2428 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
2431 static int tcpm_pd_build_pps_request(struct tcpm_port *port, u32 *rdo)
2433 unsigned int out_mv, op_ma, op_mw, min_mv, max_mv, max_ma, flags;
2434 enum pd_pdo_type type;
2435 unsigned int src_pdo_index;
2436 u32 pdo;
2438 src_pdo_index = tcpm_pd_select_pps_apdo(port);
2439 if (!src_pdo_index)
2440 return -EOPNOTSUPP;
2442 pdo = port->source_caps[src_pdo_index];
2443 type = pdo_type(pdo);
2445 switch (type) {
2446 case PDO_TYPE_APDO:
2447 if (pdo_apdo_type(pdo) != APDO_TYPE_PPS) {
2448 tcpm_log(port, "Invalid APDO selected!");
2449 return -EINVAL;
2451 min_mv = port->pps_data.min_volt;
2452 max_mv = port->pps_data.max_volt;
2453 max_ma = port->pps_data.max_curr;
2454 out_mv = port->pps_data.out_volt;
2455 op_ma = port->pps_data.op_curr;
2456 break;
2457 default:
2458 tcpm_log(port, "Invalid PDO selected!");
2459 return -EINVAL;
2462 flags = RDO_USB_COMM | RDO_NO_SUSPEND;
2464 op_mw = (op_ma * out_mv) / 1000;
2465 if (op_mw < port->operating_snk_mw) {
2467 * Try raising current to meet power needs. If that's not enough
2468 * then try upping the voltage. If that's still not enough
2469 * then we've obviously chosen a PPS APDO which really isn't
2470 * suitable so abandon ship.
2472 op_ma = (port->operating_snk_mw * 1000) / out_mv;
2473 if ((port->operating_snk_mw * 1000) % out_mv)
2474 ++op_ma;
2475 op_ma += RDO_PROG_CURR_MA_STEP - (op_ma % RDO_PROG_CURR_MA_STEP);
2477 if (op_ma > max_ma) {
2478 op_ma = max_ma;
2479 out_mv = (port->operating_snk_mw * 1000) / op_ma;
2480 if ((port->operating_snk_mw * 1000) % op_ma)
2481 ++out_mv;
2482 out_mv += RDO_PROG_VOLT_MV_STEP -
2483 (out_mv % RDO_PROG_VOLT_MV_STEP);
2485 if (out_mv > max_mv) {
2486 tcpm_log(port, "Invalid PPS APDO selected!");
2487 return -EINVAL;
2492 tcpm_log(port, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d",
2493 port->cc_req, port->cc1, port->cc2, port->vbus_source,
2494 port->vconn_role == TYPEC_SOURCE ? "source" : "sink",
2495 port->polarity);
2497 *rdo = RDO_PROG(src_pdo_index + 1, out_mv, op_ma, flags);
2499 tcpm_log(port, "Requesting APDO %d: %u mV, %u mA",
2500 src_pdo_index, out_mv, op_ma);
2502 port->pps_data.op_curr = op_ma;
2503 port->pps_data.out_volt = out_mv;
2505 return 0;
2508 static int tcpm_pd_send_pps_request(struct tcpm_port *port)
2510 struct pd_message msg;
2511 int ret;
2512 u32 rdo;
2514 ret = tcpm_pd_build_pps_request(port, &rdo);
2515 if (ret < 0)
2516 return ret;
2518 memset(&msg, 0, sizeof(msg));
2519 msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
2520 port->pwr_role,
2521 port->data_role,
2522 port->negotiated_rev,
2523 port->message_id, 1);
2524 msg.payload[0] = cpu_to_le32(rdo);
2526 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
2529 static int tcpm_set_vbus(struct tcpm_port *port, bool enable)
2531 int ret;
2533 if (enable && port->vbus_charge)
2534 return -EINVAL;
2536 tcpm_log(port, "vbus:=%d charge=%d", enable, port->vbus_charge);
2538 ret = port->tcpc->set_vbus(port->tcpc, enable, port->vbus_charge);
2539 if (ret < 0)
2540 return ret;
2542 port->vbus_source = enable;
2543 return 0;
2546 static int tcpm_set_charge(struct tcpm_port *port, bool charge)
2548 int ret;
2550 if (charge && port->vbus_source)
2551 return -EINVAL;
2553 if (charge != port->vbus_charge) {
2554 tcpm_log(port, "vbus=%d charge:=%d", port->vbus_source, charge);
2555 ret = port->tcpc->set_vbus(port->tcpc, port->vbus_source,
2556 charge);
2557 if (ret < 0)
2558 return ret;
2560 port->vbus_charge = charge;
2561 return 0;
2564 static bool tcpm_start_toggling(struct tcpm_port *port, enum typec_cc_status cc)
2566 int ret;
2568 if (!port->tcpc->start_toggling)
2569 return false;
2571 tcpm_log_force(port, "Start toggling");
2572 ret = port->tcpc->start_toggling(port->tcpc, port->port_type, cc);
2573 return ret == 0;
2576 static void tcpm_set_cc(struct tcpm_port *port, enum typec_cc_status cc)
2578 tcpm_log(port, "cc:=%d", cc);
2579 port->cc_req = cc;
2580 port->tcpc->set_cc(port->tcpc, cc);
2583 static int tcpm_init_vbus(struct tcpm_port *port)
2585 int ret;
2587 ret = port->tcpc->set_vbus(port->tcpc, false, false);
2588 port->vbus_source = false;
2589 port->vbus_charge = false;
2590 return ret;
2593 static int tcpm_init_vconn(struct tcpm_port *port)
2595 int ret;
2597 ret = port->tcpc->set_vconn(port->tcpc, false);
2598 port->vconn_role = TYPEC_SINK;
2599 return ret;
2602 static void tcpm_typec_connect(struct tcpm_port *port)
2604 if (!port->connected) {
2605 /* Make sure we don't report stale identity information */
2606 memset(&port->partner_ident, 0, sizeof(port->partner_ident));
2607 port->partner_desc.usb_pd = port->pd_capable;
2608 if (tcpm_port_is_debug(port))
2609 port->partner_desc.accessory = TYPEC_ACCESSORY_DEBUG;
2610 else if (tcpm_port_is_audio(port))
2611 port->partner_desc.accessory = TYPEC_ACCESSORY_AUDIO;
2612 else
2613 port->partner_desc.accessory = TYPEC_ACCESSORY_NONE;
2614 port->partner = typec_register_partner(port->typec_port,
2615 &port->partner_desc);
2616 port->connected = true;
2620 static int tcpm_src_attach(struct tcpm_port *port)
2622 enum typec_cc_polarity polarity =
2623 port->cc2 == TYPEC_CC_RD ? TYPEC_POLARITY_CC2
2624 : TYPEC_POLARITY_CC1;
2625 int ret;
2627 if (port->attached)
2628 return 0;
2630 ret = tcpm_set_polarity(port, polarity);
2631 if (ret < 0)
2632 return ret;
2634 ret = tcpm_set_roles(port, true, TYPEC_SOURCE, TYPEC_HOST);
2635 if (ret < 0)
2636 return ret;
2638 ret = port->tcpc->set_pd_rx(port->tcpc, true);
2639 if (ret < 0)
2640 goto out_disable_mux;
2643 * USB Type-C specification, version 1.2,
2644 * chapter 4.5.2.2.8.1 (Attached.SRC Requirements)
2645 * Enable VCONN only if the non-RD port is set to RA.
2647 if ((polarity == TYPEC_POLARITY_CC1 && port->cc2 == TYPEC_CC_RA) ||
2648 (polarity == TYPEC_POLARITY_CC2 && port->cc1 == TYPEC_CC_RA)) {
2649 ret = tcpm_set_vconn(port, true);
2650 if (ret < 0)
2651 goto out_disable_pd;
2654 ret = tcpm_set_vbus(port, true);
2655 if (ret < 0)
2656 goto out_disable_vconn;
2658 port->pd_capable = false;
2660 port->partner = NULL;
2662 port->attached = true;
2663 port->send_discover = true;
2665 return 0;
2667 out_disable_vconn:
2668 tcpm_set_vconn(port, false);
2669 out_disable_pd:
2670 port->tcpc->set_pd_rx(port->tcpc, false);
2671 out_disable_mux:
2672 tcpm_mux_set(port, TYPEC_STATE_SAFE, USB_ROLE_NONE,
2673 TYPEC_ORIENTATION_NONE);
2674 return ret;
2677 static void tcpm_typec_disconnect(struct tcpm_port *port)
2679 if (port->connected) {
2680 typec_unregister_partner(port->partner);
2681 port->partner = NULL;
2682 port->connected = false;
2686 static void tcpm_unregister_altmodes(struct tcpm_port *port)
2688 struct pd_mode_data *modep = &port->mode_data;
2689 int i;
2691 for (i = 0; i < modep->altmodes; i++) {
2692 typec_unregister_altmode(port->partner_altmode[i]);
2693 port->partner_altmode[i] = NULL;
2696 memset(modep, 0, sizeof(*modep));
2699 static void tcpm_reset_port(struct tcpm_port *port)
2701 tcpm_unregister_altmodes(port);
2702 tcpm_typec_disconnect(port);
2703 port->attached = false;
2704 port->pd_capable = false;
2705 port->pps_data.supported = false;
2708 * First Rx ID should be 0; set this to a sentinel of -1 so that
2709 * we can check tcpm_pd_rx_handler() if we had seen it before.
2711 port->rx_msgid = -1;
2713 port->tcpc->set_pd_rx(port->tcpc, false);
2714 tcpm_init_vbus(port); /* also disables charging */
2715 tcpm_init_vconn(port);
2716 tcpm_set_current_limit(port, 0, 0);
2717 tcpm_set_polarity(port, TYPEC_POLARITY_CC1);
2718 tcpm_mux_set(port, TYPEC_STATE_SAFE, USB_ROLE_NONE,
2719 TYPEC_ORIENTATION_NONE);
2720 tcpm_set_attached_state(port, false);
2721 port->try_src_count = 0;
2722 port->try_snk_count = 0;
2723 port->usb_type = POWER_SUPPLY_USB_TYPE_C;
2725 power_supply_changed(port->psy);
2728 static void tcpm_detach(struct tcpm_port *port)
2730 if (!port->attached)
2731 return;
2733 if (tcpm_port_is_disconnected(port))
2734 port->hard_reset_count = 0;
2736 tcpm_reset_port(port);
2739 static void tcpm_src_detach(struct tcpm_port *port)
2741 tcpm_detach(port);
2744 static int tcpm_snk_attach(struct tcpm_port *port)
2746 int ret;
2748 if (port->attached)
2749 return 0;
2751 ret = tcpm_set_polarity(port, port->cc2 != TYPEC_CC_OPEN ?
2752 TYPEC_POLARITY_CC2 : TYPEC_POLARITY_CC1);
2753 if (ret < 0)
2754 return ret;
2756 ret = tcpm_set_roles(port, true, TYPEC_SINK, TYPEC_DEVICE);
2757 if (ret < 0)
2758 return ret;
2760 port->pd_capable = false;
2762 port->partner = NULL;
2764 port->attached = true;
2765 port->send_discover = true;
2767 return 0;
2770 static void tcpm_snk_detach(struct tcpm_port *port)
2772 tcpm_detach(port);
2775 static int tcpm_acc_attach(struct tcpm_port *port)
2777 int ret;
2779 if (port->attached)
2780 return 0;
2782 ret = tcpm_set_roles(port, true, TYPEC_SOURCE, TYPEC_HOST);
2783 if (ret < 0)
2784 return ret;
2786 port->partner = NULL;
2788 tcpm_typec_connect(port);
2790 port->attached = true;
2792 return 0;
2795 static void tcpm_acc_detach(struct tcpm_port *port)
2797 tcpm_detach(port);
2800 static inline enum tcpm_state hard_reset_state(struct tcpm_port *port)
2802 if (port->hard_reset_count < PD_N_HARD_RESET_COUNT)
2803 return HARD_RESET_SEND;
2804 if (port->pd_capable)
2805 return ERROR_RECOVERY;
2806 if (port->pwr_role == TYPEC_SOURCE)
2807 return SRC_UNATTACHED;
2808 if (port->state == SNK_WAIT_CAPABILITIES)
2809 return SNK_READY;
2810 return SNK_UNATTACHED;
2813 static inline enum tcpm_state unattached_state(struct tcpm_port *port)
2815 if (port->port_type == TYPEC_PORT_DRP) {
2816 if (port->pwr_role == TYPEC_SOURCE)
2817 return SRC_UNATTACHED;
2818 else
2819 return SNK_UNATTACHED;
2820 } else if (port->port_type == TYPEC_PORT_SRC) {
2821 return SRC_UNATTACHED;
2824 return SNK_UNATTACHED;
2827 static void tcpm_check_send_discover(struct tcpm_port *port)
2829 if (port->data_role == TYPEC_HOST && port->send_discover &&
2830 port->pd_capable) {
2831 tcpm_send_vdm(port, USB_SID_PD, CMD_DISCOVER_IDENT, NULL, 0);
2832 port->send_discover = false;
2836 static void tcpm_swap_complete(struct tcpm_port *port, int result)
2838 if (port->swap_pending) {
2839 port->swap_status = result;
2840 port->swap_pending = false;
2841 port->non_pd_role_swap = false;
2842 complete(&port->swap_complete);
2846 static enum typec_pwr_opmode tcpm_get_pwr_opmode(enum typec_cc_status cc)
2848 switch (cc) {
2849 case TYPEC_CC_RP_1_5:
2850 return TYPEC_PWR_MODE_1_5A;
2851 case TYPEC_CC_RP_3_0:
2852 return TYPEC_PWR_MODE_3_0A;
2853 case TYPEC_CC_RP_DEF:
2854 default:
2855 return TYPEC_PWR_MODE_USB;
2859 static void run_state_machine(struct tcpm_port *port)
2861 int ret;
2862 enum typec_pwr_opmode opmode;
2863 unsigned int msecs;
2865 port->enter_state = port->state;
2866 switch (port->state) {
2867 case TOGGLING:
2868 break;
2869 /* SRC states */
2870 case SRC_UNATTACHED:
2871 if (!port->non_pd_role_swap)
2872 tcpm_swap_complete(port, -ENOTCONN);
2873 tcpm_src_detach(port);
2874 if (tcpm_start_toggling(port, tcpm_rp_cc(port))) {
2875 tcpm_set_state(port, TOGGLING, 0);
2876 break;
2878 tcpm_set_cc(port, tcpm_rp_cc(port));
2879 if (port->port_type == TYPEC_PORT_DRP)
2880 tcpm_set_state(port, SNK_UNATTACHED, PD_T_DRP_SNK);
2881 break;
2882 case SRC_ATTACH_WAIT:
2883 if (tcpm_port_is_debug(port))
2884 tcpm_set_state(port, DEBUG_ACC_ATTACHED,
2885 PD_T_CC_DEBOUNCE);
2886 else if (tcpm_port_is_audio(port))
2887 tcpm_set_state(port, AUDIO_ACC_ATTACHED,
2888 PD_T_CC_DEBOUNCE);
2889 else if (tcpm_port_is_source(port))
2890 tcpm_set_state(port,
2891 tcpm_try_snk(port) ? SNK_TRY
2892 : SRC_ATTACHED,
2893 PD_T_CC_DEBOUNCE);
2894 break;
2896 case SNK_TRY:
2897 port->try_snk_count++;
2899 * Requirements:
2900 * - Do not drive vconn or vbus
2901 * - Terminate CC pins (both) to Rd
2902 * Action:
2903 * - Wait for tDRPTry (PD_T_DRP_TRY).
2904 * Until then, ignore any state changes.
2906 tcpm_set_cc(port, TYPEC_CC_RD);
2907 tcpm_set_state(port, SNK_TRY_WAIT, PD_T_DRP_TRY);
2908 break;
2909 case SNK_TRY_WAIT:
2910 if (tcpm_port_is_sink(port)) {
2911 tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE, 0);
2912 } else {
2913 tcpm_set_state(port, SRC_TRYWAIT, 0);
2914 port->max_wait = 0;
2916 break;
2917 case SNK_TRY_WAIT_DEBOUNCE:
2918 tcpm_set_state(port, SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS,
2919 PD_T_PD_DEBOUNCE);
2920 break;
2921 case SNK_TRY_WAIT_DEBOUNCE_CHECK_VBUS:
2922 if (port->vbus_present && tcpm_port_is_sink(port)) {
2923 tcpm_set_state(port, SNK_ATTACHED, 0);
2924 } else {
2925 tcpm_set_state(port, SRC_TRYWAIT, 0);
2926 port->max_wait = 0;
2928 break;
2929 case SRC_TRYWAIT:
2930 tcpm_set_cc(port, tcpm_rp_cc(port));
2931 if (port->max_wait == 0) {
2932 port->max_wait = jiffies +
2933 msecs_to_jiffies(PD_T_DRP_TRY);
2934 tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED,
2935 PD_T_DRP_TRY);
2936 } else {
2937 if (time_is_after_jiffies(port->max_wait))
2938 tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED,
2939 jiffies_to_msecs(port->max_wait -
2940 jiffies));
2941 else
2942 tcpm_set_state(port, SNK_UNATTACHED, 0);
2944 break;
2945 case SRC_TRYWAIT_DEBOUNCE:
2946 tcpm_set_state(port, SRC_ATTACHED, PD_T_CC_DEBOUNCE);
2947 break;
2948 case SRC_TRYWAIT_UNATTACHED:
2949 tcpm_set_state(port, SNK_UNATTACHED, 0);
2950 break;
2952 case SRC_ATTACHED:
2953 ret = tcpm_src_attach(port);
2954 tcpm_set_state(port, SRC_UNATTACHED,
2955 ret < 0 ? 0 : PD_T_PS_SOURCE_ON);
2956 break;
2957 case SRC_STARTUP:
2958 opmode = tcpm_get_pwr_opmode(tcpm_rp_cc(port));
2959 typec_set_pwr_opmode(port->typec_port, opmode);
2960 port->pwr_opmode = TYPEC_PWR_MODE_USB;
2961 port->caps_count = 0;
2962 port->negotiated_rev = PD_MAX_REV;
2963 port->message_id = 0;
2964 port->rx_msgid = -1;
2965 port->explicit_contract = false;
2966 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
2967 break;
2968 case SRC_SEND_CAPABILITIES:
2969 port->caps_count++;
2970 if (port->caps_count > PD_N_CAPS_COUNT) {
2971 tcpm_set_state(port, SRC_READY, 0);
2972 break;
2974 ret = tcpm_pd_send_source_caps(port);
2975 if (ret < 0) {
2976 tcpm_set_state(port, SRC_SEND_CAPABILITIES,
2977 PD_T_SEND_SOURCE_CAP);
2978 } else {
2980 * Per standard, we should clear the reset counter here.
2981 * However, that can result in state machine hang-ups.
2982 * Reset it only in READY state to improve stability.
2984 /* port->hard_reset_count = 0; */
2985 port->caps_count = 0;
2986 port->pd_capable = true;
2987 tcpm_set_state_cond(port, SRC_SEND_CAPABILITIES_TIMEOUT,
2988 PD_T_SEND_SOURCE_CAP);
2990 break;
2991 case SRC_SEND_CAPABILITIES_TIMEOUT:
2993 * Error recovery for a PD_DATA_SOURCE_CAP reply timeout.
2995 * PD 2.0 sinks are supposed to accept src-capabilities with a
2996 * 3.0 header and simply ignore any src PDOs which the sink does
2997 * not understand such as PPS but some 2.0 sinks instead ignore
2998 * the entire PD_DATA_SOURCE_CAP message, causing contract
2999 * negotiation to fail.
3001 * After PD_N_HARD_RESET_COUNT hard-reset attempts, we try
3002 * sending src-capabilities with a lower PD revision to
3003 * make these broken sinks work.
3005 if (port->hard_reset_count < PD_N_HARD_RESET_COUNT) {
3006 tcpm_set_state(port, HARD_RESET_SEND, 0);
3007 } else if (port->negotiated_rev > PD_REV20) {
3008 port->negotiated_rev--;
3009 port->hard_reset_count = 0;
3010 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
3011 } else {
3012 tcpm_set_state(port, hard_reset_state(port), 0);
3014 break;
3015 case SRC_NEGOTIATE_CAPABILITIES:
3016 ret = tcpm_pd_check_request(port);
3017 if (ret < 0) {
3018 tcpm_pd_send_control(port, PD_CTRL_REJECT);
3019 if (!port->explicit_contract) {
3020 tcpm_set_state(port,
3021 SRC_WAIT_NEW_CAPABILITIES, 0);
3022 } else {
3023 tcpm_set_state(port, SRC_READY, 0);
3025 } else {
3026 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
3027 tcpm_set_state(port, SRC_TRANSITION_SUPPLY,
3028 PD_T_SRC_TRANSITION);
3030 break;
3031 case SRC_TRANSITION_SUPPLY:
3032 /* XXX: regulator_set_voltage(vbus, ...) */
3033 tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
3034 port->explicit_contract = true;
3035 typec_set_pwr_opmode(port->typec_port, TYPEC_PWR_MODE_PD);
3036 port->pwr_opmode = TYPEC_PWR_MODE_PD;
3037 tcpm_set_state_cond(port, SRC_READY, 0);
3038 break;
3039 case SRC_READY:
3040 #if 1
3041 port->hard_reset_count = 0;
3042 #endif
3043 port->try_src_count = 0;
3045 tcpm_swap_complete(port, 0);
3046 tcpm_typec_connect(port);
3048 tcpm_check_send_discover(port);
3050 * 6.3.5
3051 * Sending ping messages is not necessary if
3052 * - the source operates at vSafe5V
3053 * or
3054 * - The system is not operating in PD mode
3055 * or
3056 * - Both partners are connected using a Type-C connector
3058 * There is no actual need to send PD messages since the local
3059 * port type-c and the spec does not clearly say whether PD is
3060 * possible when type-c is connected to Type-A/B
3062 break;
3063 case SRC_WAIT_NEW_CAPABILITIES:
3064 /* Nothing to do... */
3065 break;
3067 /* SNK states */
3068 case SNK_UNATTACHED:
3069 if (!port->non_pd_role_swap)
3070 tcpm_swap_complete(port, -ENOTCONN);
3071 tcpm_pps_complete(port, -ENOTCONN);
3072 tcpm_snk_detach(port);
3073 if (tcpm_start_toggling(port, TYPEC_CC_RD)) {
3074 tcpm_set_state(port, TOGGLING, 0);
3075 break;
3077 tcpm_set_cc(port, TYPEC_CC_RD);
3078 if (port->port_type == TYPEC_PORT_DRP)
3079 tcpm_set_state(port, SRC_UNATTACHED, PD_T_DRP_SRC);
3080 break;
3081 case SNK_ATTACH_WAIT:
3082 if ((port->cc1 == TYPEC_CC_OPEN &&
3083 port->cc2 != TYPEC_CC_OPEN) ||
3084 (port->cc1 != TYPEC_CC_OPEN &&
3085 port->cc2 == TYPEC_CC_OPEN))
3086 tcpm_set_state(port, SNK_DEBOUNCED,
3087 PD_T_CC_DEBOUNCE);
3088 else if (tcpm_port_is_disconnected(port))
3089 tcpm_set_state(port, SNK_UNATTACHED,
3090 PD_T_PD_DEBOUNCE);
3091 break;
3092 case SNK_DEBOUNCED:
3093 if (tcpm_port_is_disconnected(port))
3094 tcpm_set_state(port, SNK_UNATTACHED,
3095 PD_T_PD_DEBOUNCE);
3096 else if (port->vbus_present)
3097 tcpm_set_state(port,
3098 tcpm_try_src(port) ? SRC_TRY
3099 : SNK_ATTACHED,
3101 else
3102 /* Wait for VBUS, but not forever */
3103 tcpm_set_state(port, PORT_RESET, PD_T_PS_SOURCE_ON);
3104 break;
3106 case SRC_TRY:
3107 port->try_src_count++;
3108 tcpm_set_cc(port, tcpm_rp_cc(port));
3109 port->max_wait = 0;
3110 tcpm_set_state(port, SRC_TRY_WAIT, 0);
3111 break;
3112 case SRC_TRY_WAIT:
3113 if (port->max_wait == 0) {
3114 port->max_wait = jiffies +
3115 msecs_to_jiffies(PD_T_DRP_TRY);
3116 msecs = PD_T_DRP_TRY;
3117 } else {
3118 if (time_is_after_jiffies(port->max_wait))
3119 msecs = jiffies_to_msecs(port->max_wait -
3120 jiffies);
3121 else
3122 msecs = 0;
3124 tcpm_set_state(port, SNK_TRYWAIT, msecs);
3125 break;
3126 case SRC_TRY_DEBOUNCE:
3127 tcpm_set_state(port, SRC_ATTACHED, PD_T_PD_DEBOUNCE);
3128 break;
3129 case SNK_TRYWAIT:
3130 tcpm_set_cc(port, TYPEC_CC_RD);
3131 tcpm_set_state(port, SNK_TRYWAIT_VBUS, PD_T_CC_DEBOUNCE);
3132 break;
3133 case SNK_TRYWAIT_VBUS:
3135 * TCPM stays in this state indefinitely until VBUS
3136 * is detected as long as Rp is not detected for
3137 * more than a time period of tPDDebounce.
3139 if (port->vbus_present && tcpm_port_is_sink(port)) {
3140 tcpm_set_state(port, SNK_ATTACHED, 0);
3141 break;
3143 if (!tcpm_port_is_sink(port))
3144 tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, 0);
3145 break;
3146 case SNK_TRYWAIT_DEBOUNCE:
3147 tcpm_set_state(port, SNK_UNATTACHED, PD_T_PD_DEBOUNCE);
3148 break;
3149 case SNK_ATTACHED:
3150 ret = tcpm_snk_attach(port);
3151 if (ret < 0)
3152 tcpm_set_state(port, SNK_UNATTACHED, 0);
3153 else
3154 tcpm_set_state(port, SNK_STARTUP, 0);
3155 break;
3156 case SNK_STARTUP:
3157 opmode = tcpm_get_pwr_opmode(port->polarity ?
3158 port->cc2 : port->cc1);
3159 typec_set_pwr_opmode(port->typec_port, opmode);
3160 port->pwr_opmode = TYPEC_PWR_MODE_USB;
3161 port->negotiated_rev = PD_MAX_REV;
3162 port->message_id = 0;
3163 port->rx_msgid = -1;
3164 port->explicit_contract = false;
3165 tcpm_set_state(port, SNK_DISCOVERY, 0);
3166 break;
3167 case SNK_DISCOVERY:
3168 if (port->vbus_present) {
3169 tcpm_set_current_limit(port,
3170 tcpm_get_current_limit(port),
3171 5000);
3172 tcpm_set_charge(port, true);
3173 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
3174 break;
3177 * For DRP, timeouts differ. Also, handling is supposed to be
3178 * different and much more complex (dead battery detection;
3179 * see USB power delivery specification, section 8.3.3.6.1.5.1).
3181 tcpm_set_state(port, hard_reset_state(port),
3182 port->port_type == TYPEC_PORT_DRP ?
3183 PD_T_DB_DETECT : PD_T_NO_RESPONSE);
3184 break;
3185 case SNK_DISCOVERY_DEBOUNCE:
3186 tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE_DONE,
3187 PD_T_CC_DEBOUNCE);
3188 break;
3189 case SNK_DISCOVERY_DEBOUNCE_DONE:
3190 if (!tcpm_port_is_disconnected(port) &&
3191 tcpm_port_is_sink(port) &&
3192 time_is_after_jiffies(port->delayed_runtime)) {
3193 tcpm_set_state(port, SNK_DISCOVERY,
3194 jiffies_to_msecs(port->delayed_runtime -
3195 jiffies));
3196 break;
3198 tcpm_set_state(port, unattached_state(port), 0);
3199 break;
3200 case SNK_WAIT_CAPABILITIES:
3201 ret = port->tcpc->set_pd_rx(port->tcpc, true);
3202 if (ret < 0) {
3203 tcpm_set_state(port, SNK_READY, 0);
3204 break;
3207 * If VBUS has never been low, and we time out waiting
3208 * for source cap, try a soft reset first, in case we
3209 * were already in a stable contract before this boot.
3210 * Do this only once.
3212 if (port->vbus_never_low) {
3213 port->vbus_never_low = false;
3214 tcpm_set_state(port, SOFT_RESET_SEND,
3215 PD_T_SINK_WAIT_CAP);
3216 } else {
3217 tcpm_set_state(port, hard_reset_state(port),
3218 PD_T_SINK_WAIT_CAP);
3220 break;
3221 case SNK_NEGOTIATE_CAPABILITIES:
3222 port->pd_capable = true;
3223 port->hard_reset_count = 0;
3224 ret = tcpm_pd_send_request(port);
3225 if (ret < 0) {
3226 /* Let the Source send capabilities again. */
3227 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
3228 } else {
3229 tcpm_set_state_cond(port, hard_reset_state(port),
3230 PD_T_SENDER_RESPONSE);
3232 break;
3233 case SNK_NEGOTIATE_PPS_CAPABILITIES:
3234 ret = tcpm_pd_send_pps_request(port);
3235 if (ret < 0) {
3236 port->pps_status = ret;
3238 * If this was called due to updates to sink
3239 * capabilities, and pps is no longer valid, we should
3240 * safely fall back to a standard PDO.
3242 if (port->update_sink_caps)
3243 tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
3244 else
3245 tcpm_set_state(port, SNK_READY, 0);
3246 } else {
3247 tcpm_set_state_cond(port, hard_reset_state(port),
3248 PD_T_SENDER_RESPONSE);
3250 break;
3251 case SNK_TRANSITION_SINK:
3252 case SNK_TRANSITION_SINK_VBUS:
3253 tcpm_set_state(port, hard_reset_state(port),
3254 PD_T_PS_TRANSITION);
3255 break;
3256 case SNK_READY:
3257 port->try_snk_count = 0;
3258 port->update_sink_caps = false;
3259 if (port->explicit_contract) {
3260 typec_set_pwr_opmode(port->typec_port,
3261 TYPEC_PWR_MODE_PD);
3262 port->pwr_opmode = TYPEC_PWR_MODE_PD;
3265 tcpm_swap_complete(port, 0);
3266 tcpm_typec_connect(port);
3267 tcpm_check_send_discover(port);
3268 tcpm_pps_complete(port, port->pps_status);
3270 power_supply_changed(port->psy);
3272 break;
3274 /* Accessory states */
3275 case ACC_UNATTACHED:
3276 tcpm_acc_detach(port);
3277 tcpm_set_state(port, SRC_UNATTACHED, 0);
3278 break;
3279 case DEBUG_ACC_ATTACHED:
3280 case AUDIO_ACC_ATTACHED:
3281 ret = tcpm_acc_attach(port);
3282 if (ret < 0)
3283 tcpm_set_state(port, ACC_UNATTACHED, 0);
3284 break;
3285 case AUDIO_ACC_DEBOUNCE:
3286 tcpm_set_state(port, ACC_UNATTACHED, PD_T_CC_DEBOUNCE);
3287 break;
3289 /* Hard_Reset states */
3290 case HARD_RESET_SEND:
3291 tcpm_pd_transmit(port, TCPC_TX_HARD_RESET, NULL);
3292 tcpm_set_state(port, HARD_RESET_START, 0);
3293 break;
3294 case HARD_RESET_START:
3295 port->hard_reset_count++;
3296 port->tcpc->set_pd_rx(port->tcpc, false);
3297 tcpm_unregister_altmodes(port);
3298 port->send_discover = true;
3299 if (port->pwr_role == TYPEC_SOURCE)
3300 tcpm_set_state(port, SRC_HARD_RESET_VBUS_OFF,
3301 PD_T_PS_HARD_RESET);
3302 else
3303 tcpm_set_state(port, SNK_HARD_RESET_SINK_OFF, 0);
3304 break;
3305 case SRC_HARD_RESET_VBUS_OFF:
3306 tcpm_set_vconn(port, true);
3307 tcpm_set_vbus(port, false);
3308 tcpm_set_roles(port, port->self_powered, TYPEC_SOURCE,
3309 TYPEC_HOST);
3310 tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SRC_RECOVER);
3311 break;
3312 case SRC_HARD_RESET_VBUS_ON:
3313 tcpm_set_vbus(port, true);
3314 port->tcpc->set_pd_rx(port->tcpc, true);
3315 tcpm_set_attached_state(port, true);
3316 tcpm_set_state(port, SRC_UNATTACHED, PD_T_PS_SOURCE_ON);
3317 break;
3318 case SNK_HARD_RESET_SINK_OFF:
3319 memset(&port->pps_data, 0, sizeof(port->pps_data));
3320 tcpm_set_vconn(port, false);
3321 if (port->pd_capable)
3322 tcpm_set_charge(port, false);
3323 tcpm_set_roles(port, port->self_powered, TYPEC_SINK,
3324 TYPEC_DEVICE);
3326 * VBUS may or may not toggle, depending on the adapter.
3327 * If it doesn't toggle, transition to SNK_HARD_RESET_SINK_ON
3328 * directly after timeout.
3330 tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, PD_T_SAFE_0V);
3331 break;
3332 case SNK_HARD_RESET_WAIT_VBUS:
3333 /* Assume we're disconnected if VBUS doesn't come back. */
3334 tcpm_set_state(port, SNK_UNATTACHED,
3335 PD_T_SRC_RECOVER_MAX + PD_T_SRC_TURN_ON);
3336 break;
3337 case SNK_HARD_RESET_SINK_ON:
3338 /* Note: There is no guarantee that VBUS is on in this state */
3340 * XXX:
3341 * The specification suggests that dual mode ports in sink
3342 * mode should transition to state PE_SRC_Transition_to_default.
3343 * See USB power delivery specification chapter 8.3.3.6.1.3.
3344 * This would mean to to
3345 * - turn off VCONN, reset power supply
3346 * - request hardware reset
3347 * - turn on VCONN
3348 * - Transition to state PE_Src_Startup
3349 * SNK only ports shall transition to state Snk_Startup
3350 * (see chapter 8.3.3.3.8).
3351 * Similar, dual-mode ports in source mode should transition
3352 * to PE_SNK_Transition_to_default.
3354 if (port->pd_capable) {
3355 tcpm_set_current_limit(port,
3356 tcpm_get_current_limit(port),
3357 5000);
3358 tcpm_set_charge(port, true);
3360 tcpm_set_attached_state(port, true);
3361 tcpm_set_state(port, SNK_STARTUP, 0);
3362 break;
3364 /* Soft_Reset states */
3365 case SOFT_RESET:
3366 port->message_id = 0;
3367 port->rx_msgid = -1;
3368 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
3369 if (port->pwr_role == TYPEC_SOURCE)
3370 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
3371 else
3372 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
3373 break;
3374 case SOFT_RESET_SEND:
3375 port->message_id = 0;
3376 port->rx_msgid = -1;
3377 if (tcpm_pd_send_control(port, PD_CTRL_SOFT_RESET))
3378 tcpm_set_state_cond(port, hard_reset_state(port), 0);
3379 else
3380 tcpm_set_state_cond(port, hard_reset_state(port),
3381 PD_T_SENDER_RESPONSE);
3382 break;
3384 /* DR_Swap states */
3385 case DR_SWAP_SEND:
3386 tcpm_pd_send_control(port, PD_CTRL_DR_SWAP);
3387 tcpm_set_state_cond(port, DR_SWAP_SEND_TIMEOUT,
3388 PD_T_SENDER_RESPONSE);
3389 break;
3390 case DR_SWAP_ACCEPT:
3391 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
3392 tcpm_set_state_cond(port, DR_SWAP_CHANGE_DR, 0);
3393 break;
3394 case DR_SWAP_SEND_TIMEOUT:
3395 tcpm_swap_complete(port, -ETIMEDOUT);
3396 tcpm_set_state(port, ready_state(port), 0);
3397 break;
3398 case DR_SWAP_CHANGE_DR:
3399 if (port->data_role == TYPEC_HOST) {
3400 tcpm_unregister_altmodes(port);
3401 tcpm_set_roles(port, true, port->pwr_role,
3402 TYPEC_DEVICE);
3403 } else {
3404 tcpm_set_roles(port, true, port->pwr_role,
3405 TYPEC_HOST);
3406 port->send_discover = true;
3408 tcpm_set_state(port, ready_state(port), 0);
3409 break;
3411 /* PR_Swap states */
3412 case PR_SWAP_ACCEPT:
3413 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
3414 tcpm_set_state(port, PR_SWAP_START, 0);
3415 break;
3416 case PR_SWAP_SEND:
3417 tcpm_pd_send_control(port, PD_CTRL_PR_SWAP);
3418 tcpm_set_state_cond(port, PR_SWAP_SEND_TIMEOUT,
3419 PD_T_SENDER_RESPONSE);
3420 break;
3421 case PR_SWAP_SEND_TIMEOUT:
3422 tcpm_swap_complete(port, -ETIMEDOUT);
3423 tcpm_set_state(port, ready_state(port), 0);
3424 break;
3425 case PR_SWAP_START:
3426 if (port->pwr_role == TYPEC_SOURCE)
3427 tcpm_set_state(port, PR_SWAP_SRC_SNK_TRANSITION_OFF,
3428 PD_T_SRC_TRANSITION);
3429 else
3430 tcpm_set_state(port, PR_SWAP_SNK_SRC_SINK_OFF, 0);
3431 break;
3432 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
3433 tcpm_set_vbus(port, false);
3434 port->explicit_contract = false;
3435 /* allow time for Vbus discharge, must be < tSrcSwapStdby */
3436 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF,
3437 PD_T_SRCSWAPSTDBY);
3438 break;
3439 case PR_SWAP_SRC_SNK_SOURCE_OFF:
3440 tcpm_set_cc(port, TYPEC_CC_RD);
3441 /* allow CC debounce */
3442 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED,
3443 PD_T_CC_DEBOUNCE);
3444 break;
3445 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED:
3447 * USB-PD standard, 6.2.1.4, Port Power Role:
3448 * "During the Power Role Swap Sequence, for the initial Source
3449 * Port, the Port Power Role field shall be set to Sink in the
3450 * PS_RDY Message indicating that the initial Source’s power
3451 * supply is turned off"
3453 tcpm_set_pwr_role(port, TYPEC_SINK);
3454 if (tcpm_pd_send_control(port, PD_CTRL_PS_RDY)) {
3455 tcpm_set_state(port, ERROR_RECOVERY, 0);
3456 break;
3458 tcpm_set_state_cond(port, SNK_UNATTACHED, PD_T_PS_SOURCE_ON);
3459 break;
3460 case PR_SWAP_SRC_SNK_SINK_ON:
3461 tcpm_set_state(port, SNK_STARTUP, 0);
3462 break;
3463 case PR_SWAP_SNK_SRC_SINK_OFF:
3464 tcpm_set_charge(port, false);
3465 tcpm_set_state(port, hard_reset_state(port),
3466 PD_T_PS_SOURCE_OFF);
3467 break;
3468 case PR_SWAP_SNK_SRC_SOURCE_ON:
3469 tcpm_set_cc(port, tcpm_rp_cc(port));
3470 tcpm_set_vbus(port, true);
3472 * allow time VBUS ramp-up, must be < tNewSrc
3473 * Also, this window overlaps with CC debounce as well.
3474 * So, Wait for the max of two which is PD_T_NEWSRC
3476 tcpm_set_state(port, PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP,
3477 PD_T_NEWSRC);
3478 break;
3479 case PR_SWAP_SNK_SRC_SOURCE_ON_VBUS_RAMPED_UP:
3481 * USB PD standard, 6.2.1.4:
3482 * "Subsequent Messages initiated by the Policy Engine,
3483 * such as the PS_RDY Message sent to indicate that Vbus
3484 * is ready, will have the Port Power Role field set to
3485 * Source."
3487 tcpm_set_pwr_role(port, TYPEC_SOURCE);
3488 tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
3489 tcpm_set_state(port, SRC_STARTUP, 0);
3490 break;
3492 case VCONN_SWAP_ACCEPT:
3493 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
3494 tcpm_set_state(port, VCONN_SWAP_START, 0);
3495 break;
3496 case VCONN_SWAP_SEND:
3497 tcpm_pd_send_control(port, PD_CTRL_VCONN_SWAP);
3498 tcpm_set_state(port, VCONN_SWAP_SEND_TIMEOUT,
3499 PD_T_SENDER_RESPONSE);
3500 break;
3501 case VCONN_SWAP_SEND_TIMEOUT:
3502 tcpm_swap_complete(port, -ETIMEDOUT);
3503 tcpm_set_state(port, ready_state(port), 0);
3504 break;
3505 case VCONN_SWAP_START:
3506 if (port->vconn_role == TYPEC_SOURCE)
3507 tcpm_set_state(port, VCONN_SWAP_WAIT_FOR_VCONN, 0);
3508 else
3509 tcpm_set_state(port, VCONN_SWAP_TURN_ON_VCONN, 0);
3510 break;
3511 case VCONN_SWAP_WAIT_FOR_VCONN:
3512 tcpm_set_state(port, hard_reset_state(port),
3513 PD_T_VCONN_SOURCE_ON);
3514 break;
3515 case VCONN_SWAP_TURN_ON_VCONN:
3516 tcpm_set_vconn(port, true);
3517 tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
3518 tcpm_set_state(port, ready_state(port), 0);
3519 break;
3520 case VCONN_SWAP_TURN_OFF_VCONN:
3521 tcpm_set_vconn(port, false);
3522 tcpm_set_state(port, ready_state(port), 0);
3523 break;
3525 case DR_SWAP_CANCEL:
3526 case PR_SWAP_CANCEL:
3527 case VCONN_SWAP_CANCEL:
3528 tcpm_swap_complete(port, port->swap_status);
3529 if (port->pwr_role == TYPEC_SOURCE)
3530 tcpm_set_state(port, SRC_READY, 0);
3531 else
3532 tcpm_set_state(port, SNK_READY, 0);
3533 break;
3535 case BIST_RX:
3536 switch (BDO_MODE_MASK(port->bist_request)) {
3537 case BDO_MODE_CARRIER2:
3538 tcpm_pd_transmit(port, TCPC_TX_BIST_MODE_2, NULL);
3539 break;
3540 default:
3541 break;
3543 /* Always switch to unattached state */
3544 tcpm_set_state(port, unattached_state(port), 0);
3545 break;
3546 case GET_STATUS_SEND:
3547 tcpm_pd_send_control(port, PD_CTRL_GET_STATUS);
3548 tcpm_set_state(port, GET_STATUS_SEND_TIMEOUT,
3549 PD_T_SENDER_RESPONSE);
3550 break;
3551 case GET_STATUS_SEND_TIMEOUT:
3552 tcpm_set_state(port, ready_state(port), 0);
3553 break;
3554 case GET_PPS_STATUS_SEND:
3555 tcpm_pd_send_control(port, PD_CTRL_GET_PPS_STATUS);
3556 tcpm_set_state(port, GET_PPS_STATUS_SEND_TIMEOUT,
3557 PD_T_SENDER_RESPONSE);
3558 break;
3559 case GET_PPS_STATUS_SEND_TIMEOUT:
3560 tcpm_set_state(port, ready_state(port), 0);
3561 break;
3562 case ERROR_RECOVERY:
3563 tcpm_swap_complete(port, -EPROTO);
3564 tcpm_pps_complete(port, -EPROTO);
3565 tcpm_set_state(port, PORT_RESET, 0);
3566 break;
3567 case PORT_RESET:
3568 tcpm_reset_port(port);
3569 tcpm_set_cc(port, TYPEC_CC_OPEN);
3570 tcpm_set_state(port, PORT_RESET_WAIT_OFF,
3571 PD_T_ERROR_RECOVERY);
3572 break;
3573 case PORT_RESET_WAIT_OFF:
3574 tcpm_set_state(port,
3575 tcpm_default_state(port),
3576 port->vbus_present ? PD_T_PS_SOURCE_OFF : 0);
3577 break;
3578 default:
3579 WARN(1, "Unexpected port state %d\n", port->state);
3580 break;
3584 static void tcpm_state_machine_work(struct work_struct *work)
3586 struct tcpm_port *port = container_of(work, struct tcpm_port,
3587 state_machine.work);
3588 enum tcpm_state prev_state;
3590 mutex_lock(&port->lock);
3591 port->state_machine_running = true;
3593 if (port->queued_message && tcpm_send_queued_message(port))
3594 goto done;
3596 /* If we were queued due to a delayed state change, update it now */
3597 if (port->delayed_state) {
3598 tcpm_log(port, "state change %s -> %s [delayed %ld ms]",
3599 tcpm_states[port->state],
3600 tcpm_states[port->delayed_state], port->delay_ms);
3601 port->prev_state = port->state;
3602 port->state = port->delayed_state;
3603 port->delayed_state = INVALID_STATE;
3607 * Continue running as long as we have (non-delayed) state changes
3608 * to make.
3610 do {
3611 prev_state = port->state;
3612 run_state_machine(port);
3613 if (port->queued_message)
3614 tcpm_send_queued_message(port);
3615 } while (port->state != prev_state && !port->delayed_state);
3617 done:
3618 port->state_machine_running = false;
3619 mutex_unlock(&port->lock);
3622 static void _tcpm_cc_change(struct tcpm_port *port, enum typec_cc_status cc1,
3623 enum typec_cc_status cc2)
3625 enum typec_cc_status old_cc1, old_cc2;
3626 enum tcpm_state new_state;
3628 old_cc1 = port->cc1;
3629 old_cc2 = port->cc2;
3630 port->cc1 = cc1;
3631 port->cc2 = cc2;
3633 tcpm_log_force(port,
3634 "CC1: %u -> %u, CC2: %u -> %u [state %s, polarity %d, %s]",
3635 old_cc1, cc1, old_cc2, cc2, tcpm_states[port->state],
3636 port->polarity,
3637 tcpm_port_is_disconnected(port) ? "disconnected"
3638 : "connected");
3640 switch (port->state) {
3641 case TOGGLING:
3642 if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) ||
3643 tcpm_port_is_source(port))
3644 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
3645 else if (tcpm_port_is_sink(port))
3646 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
3647 break;
3648 case SRC_UNATTACHED:
3649 case ACC_UNATTACHED:
3650 if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) ||
3651 tcpm_port_is_source(port))
3652 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
3653 break;
3654 case SRC_ATTACH_WAIT:
3655 if (tcpm_port_is_disconnected(port) ||
3656 tcpm_port_is_audio_detached(port))
3657 tcpm_set_state(port, SRC_UNATTACHED, 0);
3658 else if (cc1 != old_cc1 || cc2 != old_cc2)
3659 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
3660 break;
3661 case SRC_ATTACHED:
3662 case SRC_SEND_CAPABILITIES:
3663 case SRC_READY:
3664 if (tcpm_port_is_disconnected(port) ||
3665 !tcpm_port_is_source(port))
3666 tcpm_set_state(port, SRC_UNATTACHED, 0);
3667 break;
3668 case SNK_UNATTACHED:
3669 if (tcpm_port_is_sink(port))
3670 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
3671 break;
3672 case SNK_ATTACH_WAIT:
3673 if ((port->cc1 == TYPEC_CC_OPEN &&
3674 port->cc2 != TYPEC_CC_OPEN) ||
3675 (port->cc1 != TYPEC_CC_OPEN &&
3676 port->cc2 == TYPEC_CC_OPEN))
3677 new_state = SNK_DEBOUNCED;
3678 else if (tcpm_port_is_disconnected(port))
3679 new_state = SNK_UNATTACHED;
3680 else
3681 break;
3682 if (new_state != port->delayed_state)
3683 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
3684 break;
3685 case SNK_DEBOUNCED:
3686 if (tcpm_port_is_disconnected(port))
3687 new_state = SNK_UNATTACHED;
3688 else if (port->vbus_present)
3689 new_state = tcpm_try_src(port) ? SRC_TRY : SNK_ATTACHED;
3690 else
3691 new_state = SNK_UNATTACHED;
3692 if (new_state != port->delayed_state)
3693 tcpm_set_state(port, SNK_DEBOUNCED, 0);
3694 break;
3695 case SNK_READY:
3696 if (tcpm_port_is_disconnected(port))
3697 tcpm_set_state(port, unattached_state(port), 0);
3698 else if (!port->pd_capable &&
3699 (cc1 != old_cc1 || cc2 != old_cc2))
3700 tcpm_set_current_limit(port,
3701 tcpm_get_current_limit(port),
3702 5000);
3703 break;
3705 case AUDIO_ACC_ATTACHED:
3706 if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN)
3707 tcpm_set_state(port, AUDIO_ACC_DEBOUNCE, 0);
3708 break;
3709 case AUDIO_ACC_DEBOUNCE:
3710 if (tcpm_port_is_audio(port))
3711 tcpm_set_state(port, AUDIO_ACC_ATTACHED, 0);
3712 break;
3714 case DEBUG_ACC_ATTACHED:
3715 if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN)
3716 tcpm_set_state(port, ACC_UNATTACHED, 0);
3717 break;
3719 case SNK_TRY:
3720 /* Do nothing, waiting for timeout */
3721 break;
3723 case SNK_DISCOVERY:
3724 /* CC line is unstable, wait for debounce */
3725 if (tcpm_port_is_disconnected(port))
3726 tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE, 0);
3727 break;
3728 case SNK_DISCOVERY_DEBOUNCE:
3729 break;
3731 case SRC_TRYWAIT:
3732 /* Hand over to state machine if needed */
3733 if (!port->vbus_present && tcpm_port_is_source(port))
3734 tcpm_set_state(port, SRC_TRYWAIT_DEBOUNCE, 0);
3735 break;
3736 case SRC_TRYWAIT_DEBOUNCE:
3737 if (port->vbus_present || !tcpm_port_is_source(port))
3738 tcpm_set_state(port, SRC_TRYWAIT, 0);
3739 break;
3740 case SNK_TRY_WAIT_DEBOUNCE:
3741 if (!tcpm_port_is_sink(port)) {
3742 port->max_wait = 0;
3743 tcpm_set_state(port, SRC_TRYWAIT, 0);
3745 break;
3746 case SRC_TRY_WAIT:
3747 if (tcpm_port_is_source(port))
3748 tcpm_set_state(port, SRC_TRY_DEBOUNCE, 0);
3749 break;
3750 case SRC_TRY_DEBOUNCE:
3751 tcpm_set_state(port, SRC_TRY_WAIT, 0);
3752 break;
3753 case SNK_TRYWAIT_DEBOUNCE:
3754 if (tcpm_port_is_sink(port))
3755 tcpm_set_state(port, SNK_TRYWAIT_VBUS, 0);
3756 break;
3757 case SNK_TRYWAIT_VBUS:
3758 if (!tcpm_port_is_sink(port))
3759 tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, 0);
3760 break;
3761 case SNK_TRYWAIT:
3762 /* Do nothing, waiting for tCCDebounce */
3763 break;
3764 case PR_SWAP_SNK_SRC_SINK_OFF:
3765 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
3766 case PR_SWAP_SRC_SNK_SOURCE_OFF:
3767 case PR_SWAP_SRC_SNK_SOURCE_OFF_CC_DEBOUNCED:
3768 case PR_SWAP_SNK_SRC_SOURCE_ON:
3770 * CC state change is expected in PR_SWAP
3771 * Ignore it.
3773 break;
3775 default:
3776 if (tcpm_port_is_disconnected(port))
3777 tcpm_set_state(port, unattached_state(port), 0);
3778 break;
3782 static void _tcpm_pd_vbus_on(struct tcpm_port *port)
3784 tcpm_log_force(port, "VBUS on");
3785 port->vbus_present = true;
3786 switch (port->state) {
3787 case SNK_TRANSITION_SINK_VBUS:
3788 port->explicit_contract = true;
3789 tcpm_set_state(port, SNK_READY, 0);
3790 break;
3791 case SNK_DISCOVERY:
3792 tcpm_set_state(port, SNK_DISCOVERY, 0);
3793 break;
3795 case SNK_DEBOUNCED:
3796 tcpm_set_state(port, tcpm_try_src(port) ? SRC_TRY
3797 : SNK_ATTACHED,
3799 break;
3800 case SNK_HARD_RESET_WAIT_VBUS:
3801 tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, 0);
3802 break;
3803 case SRC_ATTACHED:
3804 tcpm_set_state(port, SRC_STARTUP, 0);
3805 break;
3806 case SRC_HARD_RESET_VBUS_ON:
3807 tcpm_set_state(port, SRC_STARTUP, 0);
3808 break;
3810 case SNK_TRY:
3811 /* Do nothing, waiting for timeout */
3812 break;
3813 case SRC_TRYWAIT:
3814 /* Do nothing, Waiting for Rd to be detected */
3815 break;
3816 case SRC_TRYWAIT_DEBOUNCE:
3817 tcpm_set_state(port, SRC_TRYWAIT, 0);
3818 break;
3819 case SNK_TRY_WAIT_DEBOUNCE:
3820 /* Do nothing, waiting for PD_DEBOUNCE to do be done */
3821 break;
3822 case SNK_TRYWAIT:
3823 /* Do nothing, waiting for tCCDebounce */
3824 break;
3825 case SNK_TRYWAIT_VBUS:
3826 if (tcpm_port_is_sink(port))
3827 tcpm_set_state(port, SNK_ATTACHED, 0);
3828 break;
3829 case SNK_TRYWAIT_DEBOUNCE:
3830 /* Do nothing, waiting for Rp */
3831 break;
3832 case SRC_TRY_WAIT:
3833 case SRC_TRY_DEBOUNCE:
3834 /* Do nothing, waiting for sink detection */
3835 break;
3836 default:
3837 break;
3841 static void _tcpm_pd_vbus_off(struct tcpm_port *port)
3843 tcpm_log_force(port, "VBUS off");
3844 port->vbus_present = false;
3845 port->vbus_never_low = false;
3846 switch (port->state) {
3847 case SNK_HARD_RESET_SINK_OFF:
3848 tcpm_set_state(port, SNK_HARD_RESET_WAIT_VBUS, 0);
3849 break;
3850 case SRC_HARD_RESET_VBUS_OFF:
3851 tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, 0);
3852 break;
3853 case HARD_RESET_SEND:
3854 break;
3856 case SNK_TRY:
3857 /* Do nothing, waiting for timeout */
3858 break;
3859 case SRC_TRYWAIT:
3860 /* Hand over to state machine if needed */
3861 if (tcpm_port_is_source(port))
3862 tcpm_set_state(port, SRC_TRYWAIT_DEBOUNCE, 0);
3863 break;
3864 case SNK_TRY_WAIT_DEBOUNCE:
3865 /* Do nothing, waiting for PD_DEBOUNCE to do be done */
3866 break;
3867 case SNK_TRYWAIT:
3868 case SNK_TRYWAIT_VBUS:
3869 case SNK_TRYWAIT_DEBOUNCE:
3870 break;
3871 case SNK_ATTACH_WAIT:
3872 tcpm_set_state(port, SNK_UNATTACHED, 0);
3873 break;
3875 case SNK_NEGOTIATE_CAPABILITIES:
3876 break;
3878 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
3879 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF, 0);
3880 break;
3882 case PR_SWAP_SNK_SRC_SINK_OFF:
3883 /* Do nothing, expected */
3884 break;
3886 case PORT_RESET_WAIT_OFF:
3887 tcpm_set_state(port, tcpm_default_state(port), 0);
3888 break;
3889 case SRC_TRY_WAIT:
3890 case SRC_TRY_DEBOUNCE:
3891 /* Do nothing, waiting for sink detection */
3892 break;
3893 default:
3894 if (port->pwr_role == TYPEC_SINK &&
3895 port->attached)
3896 tcpm_set_state(port, SNK_UNATTACHED, 0);
3897 break;
3901 static void _tcpm_pd_hard_reset(struct tcpm_port *port)
3903 tcpm_log_force(port, "Received hard reset");
3905 * If we keep receiving hard reset requests, executing the hard reset
3906 * must have failed. Revert to error recovery if that happens.
3908 tcpm_set_state(port,
3909 port->hard_reset_count < PD_N_HARD_RESET_COUNT ?
3910 HARD_RESET_START : ERROR_RECOVERY,
3914 static void tcpm_pd_event_handler(struct work_struct *work)
3916 struct tcpm_port *port = container_of(work, struct tcpm_port,
3917 event_work);
3918 u32 events;
3920 mutex_lock(&port->lock);
3922 spin_lock(&port->pd_event_lock);
3923 while (port->pd_events) {
3924 events = port->pd_events;
3925 port->pd_events = 0;
3926 spin_unlock(&port->pd_event_lock);
3927 if (events & TCPM_RESET_EVENT)
3928 _tcpm_pd_hard_reset(port);
3929 if (events & TCPM_VBUS_EVENT) {
3930 bool vbus;
3932 vbus = port->tcpc->get_vbus(port->tcpc);
3933 if (vbus)
3934 _tcpm_pd_vbus_on(port);
3935 else
3936 _tcpm_pd_vbus_off(port);
3938 if (events & TCPM_CC_EVENT) {
3939 enum typec_cc_status cc1, cc2;
3941 if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0)
3942 _tcpm_cc_change(port, cc1, cc2);
3944 spin_lock(&port->pd_event_lock);
3946 spin_unlock(&port->pd_event_lock);
3947 mutex_unlock(&port->lock);
3950 void tcpm_cc_change(struct tcpm_port *port)
3952 spin_lock(&port->pd_event_lock);
3953 port->pd_events |= TCPM_CC_EVENT;
3954 spin_unlock(&port->pd_event_lock);
3955 queue_work(port->wq, &port->event_work);
3957 EXPORT_SYMBOL_GPL(tcpm_cc_change);
3959 void tcpm_vbus_change(struct tcpm_port *port)
3961 spin_lock(&port->pd_event_lock);
3962 port->pd_events |= TCPM_VBUS_EVENT;
3963 spin_unlock(&port->pd_event_lock);
3964 queue_work(port->wq, &port->event_work);
3966 EXPORT_SYMBOL_GPL(tcpm_vbus_change);
3968 void tcpm_pd_hard_reset(struct tcpm_port *port)
3970 spin_lock(&port->pd_event_lock);
3971 port->pd_events = TCPM_RESET_EVENT;
3972 spin_unlock(&port->pd_event_lock);
3973 queue_work(port->wq, &port->event_work);
3975 EXPORT_SYMBOL_GPL(tcpm_pd_hard_reset);
3977 static int tcpm_dr_set(const struct typec_capability *cap,
3978 enum typec_data_role data)
3980 struct tcpm_port *port = typec_cap_to_tcpm(cap);
3981 int ret;
3983 mutex_lock(&port->swap_lock);
3984 mutex_lock(&port->lock);
3986 if (port->port_type != TYPEC_PORT_DRP) {
3987 ret = -EINVAL;
3988 goto port_unlock;
3990 if (port->state != SRC_READY && port->state != SNK_READY) {
3991 ret = -EAGAIN;
3992 goto port_unlock;
3995 if (port->data_role == data) {
3996 ret = 0;
3997 goto port_unlock;
4001 * XXX
4002 * 6.3.9: If an alternate mode is active, a request to swap
4003 * alternate modes shall trigger a port reset.
4004 * Reject data role swap request in this case.
4007 if (!port->pd_capable) {
4009 * If the partner is not PD capable, reset the port to
4010 * trigger a role change. This can only work if a preferred
4011 * role is configured, and if it matches the requested role.
4013 if (port->try_role == TYPEC_NO_PREFERRED_ROLE ||
4014 port->try_role == port->pwr_role) {
4015 ret = -EINVAL;
4016 goto port_unlock;
4018 port->non_pd_role_swap = true;
4019 tcpm_set_state(port, PORT_RESET, 0);
4020 } else {
4021 tcpm_set_state(port, DR_SWAP_SEND, 0);
4024 port->swap_status = 0;
4025 port->swap_pending = true;
4026 reinit_completion(&port->swap_complete);
4027 mutex_unlock(&port->lock);
4029 if (!wait_for_completion_timeout(&port->swap_complete,
4030 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT)))
4031 ret = -ETIMEDOUT;
4032 else
4033 ret = port->swap_status;
4035 port->non_pd_role_swap = false;
4036 goto swap_unlock;
4038 port_unlock:
4039 mutex_unlock(&port->lock);
4040 swap_unlock:
4041 mutex_unlock(&port->swap_lock);
4042 return ret;
4045 static int tcpm_pr_set(const struct typec_capability *cap,
4046 enum typec_role role)
4048 struct tcpm_port *port = typec_cap_to_tcpm(cap);
4049 int ret;
4051 mutex_lock(&port->swap_lock);
4052 mutex_lock(&port->lock);
4054 if (port->port_type != TYPEC_PORT_DRP) {
4055 ret = -EINVAL;
4056 goto port_unlock;
4058 if (port->state != SRC_READY && port->state != SNK_READY) {
4059 ret = -EAGAIN;
4060 goto port_unlock;
4063 if (role == port->pwr_role) {
4064 ret = 0;
4065 goto port_unlock;
4068 port->swap_status = 0;
4069 port->swap_pending = true;
4070 reinit_completion(&port->swap_complete);
4071 tcpm_set_state(port, PR_SWAP_SEND, 0);
4072 mutex_unlock(&port->lock);
4074 if (!wait_for_completion_timeout(&port->swap_complete,
4075 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT)))
4076 ret = -ETIMEDOUT;
4077 else
4078 ret = port->swap_status;
4080 goto swap_unlock;
4082 port_unlock:
4083 mutex_unlock(&port->lock);
4084 swap_unlock:
4085 mutex_unlock(&port->swap_lock);
4086 return ret;
4089 static int tcpm_vconn_set(const struct typec_capability *cap,
4090 enum typec_role role)
4092 struct tcpm_port *port = typec_cap_to_tcpm(cap);
4093 int ret;
4095 mutex_lock(&port->swap_lock);
4096 mutex_lock(&port->lock);
4098 if (port->state != SRC_READY && port->state != SNK_READY) {
4099 ret = -EAGAIN;
4100 goto port_unlock;
4103 if (role == port->vconn_role) {
4104 ret = 0;
4105 goto port_unlock;
4108 port->swap_status = 0;
4109 port->swap_pending = true;
4110 reinit_completion(&port->swap_complete);
4111 tcpm_set_state(port, VCONN_SWAP_SEND, 0);
4112 mutex_unlock(&port->lock);
4114 if (!wait_for_completion_timeout(&port->swap_complete,
4115 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT)))
4116 ret = -ETIMEDOUT;
4117 else
4118 ret = port->swap_status;
4120 goto swap_unlock;
4122 port_unlock:
4123 mutex_unlock(&port->lock);
4124 swap_unlock:
4125 mutex_unlock(&port->swap_lock);
4126 return ret;
4129 static int tcpm_try_role(const struct typec_capability *cap, int role)
4131 struct tcpm_port *port = typec_cap_to_tcpm(cap);
4132 struct tcpc_dev *tcpc = port->tcpc;
4133 int ret = 0;
4135 mutex_lock(&port->lock);
4136 if (tcpc->try_role)
4137 ret = tcpc->try_role(tcpc, role);
4138 if (!ret && (!tcpc->config || !tcpc->config->try_role_hw))
4139 port->try_role = role;
4140 port->try_src_count = 0;
4141 port->try_snk_count = 0;
4142 mutex_unlock(&port->lock);
4144 return ret;
4147 static int tcpm_pps_set_op_curr(struct tcpm_port *port, u16 op_curr)
4149 unsigned int target_mw;
4150 int ret;
4152 mutex_lock(&port->swap_lock);
4153 mutex_lock(&port->lock);
4155 if (!port->pps_data.active) {
4156 ret = -EOPNOTSUPP;
4157 goto port_unlock;
4160 if (port->state != SNK_READY) {
4161 ret = -EAGAIN;
4162 goto port_unlock;
4165 if (op_curr > port->pps_data.max_curr) {
4166 ret = -EINVAL;
4167 goto port_unlock;
4170 target_mw = (op_curr * port->pps_data.out_volt) / 1000;
4171 if (target_mw < port->operating_snk_mw) {
4172 ret = -EINVAL;
4173 goto port_unlock;
4176 /* Round down operating current to align with PPS valid steps */
4177 op_curr = op_curr - (op_curr % RDO_PROG_CURR_MA_STEP);
4179 reinit_completion(&port->pps_complete);
4180 port->pps_data.op_curr = op_curr;
4181 port->pps_status = 0;
4182 port->pps_pending = true;
4183 tcpm_set_state(port, SNK_NEGOTIATE_PPS_CAPABILITIES, 0);
4184 mutex_unlock(&port->lock);
4186 if (!wait_for_completion_timeout(&port->pps_complete,
4187 msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT)))
4188 ret = -ETIMEDOUT;
4189 else
4190 ret = port->pps_status;
4192 goto swap_unlock;
4194 port_unlock:
4195 mutex_unlock(&port->lock);
4196 swap_unlock:
4197 mutex_unlock(&port->swap_lock);
4199 return ret;
4202 static int tcpm_pps_set_out_volt(struct tcpm_port *port, u16 out_volt)
4204 unsigned int target_mw;
4205 int ret;
4207 mutex_lock(&port->swap_lock);
4208 mutex_lock(&port->lock);
4210 if (!port->pps_data.active) {
4211 ret = -EOPNOTSUPP;
4212 goto port_unlock;
4215 if (port->state != SNK_READY) {
4216 ret = -EAGAIN;
4217 goto port_unlock;
4220 if (out_volt < port->pps_data.min_volt ||
4221 out_volt > port->pps_data.max_volt) {
4222 ret = -EINVAL;
4223 goto port_unlock;
4226 target_mw = (port->pps_data.op_curr * out_volt) / 1000;
4227 if (target_mw < port->operating_snk_mw) {
4228 ret = -EINVAL;
4229 goto port_unlock;
4232 /* Round down output voltage to align with PPS valid steps */
4233 out_volt = out_volt - (out_volt % RDO_PROG_VOLT_MV_STEP);
4235 reinit_completion(&port->pps_complete);
4236 port->pps_data.out_volt = out_volt;
4237 port->pps_status = 0;
4238 port->pps_pending = true;
4239 tcpm_set_state(port, SNK_NEGOTIATE_PPS_CAPABILITIES, 0);
4240 mutex_unlock(&port->lock);
4242 if (!wait_for_completion_timeout(&port->pps_complete,
4243 msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT)))
4244 ret = -ETIMEDOUT;
4245 else
4246 ret = port->pps_status;
4248 goto swap_unlock;
4250 port_unlock:
4251 mutex_unlock(&port->lock);
4252 swap_unlock:
4253 mutex_unlock(&port->swap_lock);
4255 return ret;
4258 static int tcpm_pps_activate(struct tcpm_port *port, bool activate)
4260 int ret = 0;
4262 mutex_lock(&port->swap_lock);
4263 mutex_lock(&port->lock);
4265 if (!port->pps_data.supported) {
4266 ret = -EOPNOTSUPP;
4267 goto port_unlock;
4270 /* Trying to deactivate PPS when already deactivated so just bail */
4271 if (!port->pps_data.active && !activate)
4272 goto port_unlock;
4274 if (port->state != SNK_READY) {
4275 ret = -EAGAIN;
4276 goto port_unlock;
4279 reinit_completion(&port->pps_complete);
4280 port->pps_status = 0;
4281 port->pps_pending = true;
4283 /* Trigger PPS request or move back to standard PDO contract */
4284 if (activate) {
4285 port->pps_data.out_volt = port->supply_voltage;
4286 port->pps_data.op_curr = port->current_limit;
4287 tcpm_set_state(port, SNK_NEGOTIATE_PPS_CAPABILITIES, 0);
4288 } else {
4289 tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
4291 mutex_unlock(&port->lock);
4293 if (!wait_for_completion_timeout(&port->pps_complete,
4294 msecs_to_jiffies(PD_PPS_CTRL_TIMEOUT)))
4295 ret = -ETIMEDOUT;
4296 else
4297 ret = port->pps_status;
4299 goto swap_unlock;
4301 port_unlock:
4302 mutex_unlock(&port->lock);
4303 swap_unlock:
4304 mutex_unlock(&port->swap_lock);
4306 return ret;
4309 static void tcpm_init(struct tcpm_port *port)
4311 enum typec_cc_status cc1, cc2;
4313 port->tcpc->init(port->tcpc);
4315 tcpm_reset_port(port);
4318 * XXX
4319 * Should possibly wait for VBUS to settle if it was enabled locally
4320 * since tcpm_reset_port() will disable VBUS.
4322 port->vbus_present = port->tcpc->get_vbus(port->tcpc);
4323 if (port->vbus_present)
4324 port->vbus_never_low = true;
4326 tcpm_set_state(port, tcpm_default_state(port), 0);
4328 if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0)
4329 _tcpm_cc_change(port, cc1, cc2);
4332 * Some adapters need a clean slate at startup, and won't recover
4333 * otherwise. So do not try to be fancy and force a clean disconnect.
4335 tcpm_set_state(port, PORT_RESET, 0);
4338 static int tcpm_port_type_set(const struct typec_capability *cap,
4339 enum typec_port_type type)
4341 struct tcpm_port *port = typec_cap_to_tcpm(cap);
4343 mutex_lock(&port->lock);
4344 if (type == port->port_type)
4345 goto port_unlock;
4347 port->port_type = type;
4349 if (!port->connected) {
4350 tcpm_set_state(port, PORT_RESET, 0);
4351 } else if (type == TYPEC_PORT_SNK) {
4352 if (!(port->pwr_role == TYPEC_SINK &&
4353 port->data_role == TYPEC_DEVICE))
4354 tcpm_set_state(port, PORT_RESET, 0);
4355 } else if (type == TYPEC_PORT_SRC) {
4356 if (!(port->pwr_role == TYPEC_SOURCE &&
4357 port->data_role == TYPEC_HOST))
4358 tcpm_set_state(port, PORT_RESET, 0);
4361 port_unlock:
4362 mutex_unlock(&port->lock);
4363 return 0;
4366 void tcpm_tcpc_reset(struct tcpm_port *port)
4368 mutex_lock(&port->lock);
4369 /* XXX: Maintain PD connection if possible? */
4370 tcpm_init(port);
4371 mutex_unlock(&port->lock);
4373 EXPORT_SYMBOL_GPL(tcpm_tcpc_reset);
4375 static int tcpm_copy_pdos(u32 *dest_pdo, const u32 *src_pdo,
4376 unsigned int nr_pdo)
4378 unsigned int i;
4380 if (nr_pdo > PDO_MAX_OBJECTS)
4381 nr_pdo = PDO_MAX_OBJECTS;
4383 for (i = 0; i < nr_pdo; i++)
4384 dest_pdo[i] = src_pdo[i];
4386 return nr_pdo;
4389 static int tcpm_copy_vdos(u32 *dest_vdo, const u32 *src_vdo,
4390 unsigned int nr_vdo)
4392 unsigned int i;
4394 if (nr_vdo > VDO_MAX_OBJECTS)
4395 nr_vdo = VDO_MAX_OBJECTS;
4397 for (i = 0; i < nr_vdo; i++)
4398 dest_vdo[i] = src_vdo[i];
4400 return nr_vdo;
4403 static int tcpm_fw_get_caps(struct tcpm_port *port,
4404 struct fwnode_handle *fwnode)
4406 const char *cap_str;
4407 int ret;
4408 u32 mw;
4410 if (!fwnode)
4411 return -EINVAL;
4413 /* USB data support is optional */
4414 ret = fwnode_property_read_string(fwnode, "data-role", &cap_str);
4415 if (ret == 0) {
4416 port->typec_caps.data = typec_find_port_data_role(cap_str);
4417 if (port->typec_caps.data < 0)
4418 return -EINVAL;
4421 ret = fwnode_property_read_string(fwnode, "power-role", &cap_str);
4422 if (ret < 0)
4423 return ret;
4425 port->typec_caps.type = typec_find_port_power_role(cap_str);
4426 if (port->typec_caps.type < 0)
4427 return -EINVAL;
4428 port->port_type = port->typec_caps.type;
4430 if (port->port_type == TYPEC_PORT_SNK)
4431 goto sink;
4433 /* Get source pdos */
4434 ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
4435 NULL, 0);
4436 if (ret <= 0)
4437 return -EINVAL;
4439 port->nr_src_pdo = min(ret, PDO_MAX_OBJECTS);
4440 ret = fwnode_property_read_u32_array(fwnode, "source-pdos",
4441 port->src_pdo, port->nr_src_pdo);
4442 if ((ret < 0) || tcpm_validate_caps(port, port->src_pdo,
4443 port->nr_src_pdo))
4444 return -EINVAL;
4446 if (port->port_type == TYPEC_PORT_SRC)
4447 return 0;
4449 /* Get the preferred power role for DRP */
4450 ret = fwnode_property_read_string(fwnode, "try-power-role", &cap_str);
4451 if (ret < 0)
4452 return ret;
4454 port->typec_caps.prefer_role = typec_find_power_role(cap_str);
4455 if (port->typec_caps.prefer_role < 0)
4456 return -EINVAL;
4457 sink:
4458 /* Get sink pdos */
4459 ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
4460 NULL, 0);
4461 if (ret <= 0)
4462 return -EINVAL;
4464 port->nr_snk_pdo = min(ret, PDO_MAX_OBJECTS);
4465 ret = fwnode_property_read_u32_array(fwnode, "sink-pdos",
4466 port->snk_pdo, port->nr_snk_pdo);
4467 if ((ret < 0) || tcpm_validate_caps(port, port->snk_pdo,
4468 port->nr_snk_pdo))
4469 return -EINVAL;
4471 if (fwnode_property_read_u32(fwnode, "op-sink-microwatt", &mw) < 0)
4472 return -EINVAL;
4473 port->operating_snk_mw = mw / 1000;
4475 port->self_powered = fwnode_property_read_bool(fwnode, "self-powered");
4477 return 0;
4480 int tcpm_update_source_capabilities(struct tcpm_port *port, const u32 *pdo,
4481 unsigned int nr_pdo)
4483 if (tcpm_validate_caps(port, pdo, nr_pdo))
4484 return -EINVAL;
4486 mutex_lock(&port->lock);
4487 port->nr_src_pdo = tcpm_copy_pdos(port->src_pdo, pdo, nr_pdo);
4488 switch (port->state) {
4489 case SRC_UNATTACHED:
4490 case SRC_ATTACH_WAIT:
4491 case SRC_TRYWAIT:
4492 tcpm_set_cc(port, tcpm_rp_cc(port));
4493 break;
4494 case SRC_SEND_CAPABILITIES:
4495 case SRC_NEGOTIATE_CAPABILITIES:
4496 case SRC_READY:
4497 case SRC_WAIT_NEW_CAPABILITIES:
4498 tcpm_set_cc(port, tcpm_rp_cc(port));
4499 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
4500 break;
4501 default:
4502 break;
4504 mutex_unlock(&port->lock);
4505 return 0;
4507 EXPORT_SYMBOL_GPL(tcpm_update_source_capabilities);
4509 int tcpm_update_sink_capabilities(struct tcpm_port *port, const u32 *pdo,
4510 unsigned int nr_pdo,
4511 unsigned int operating_snk_mw)
4513 if (tcpm_validate_caps(port, pdo, nr_pdo))
4514 return -EINVAL;
4516 mutex_lock(&port->lock);
4517 port->nr_snk_pdo = tcpm_copy_pdos(port->snk_pdo, pdo, nr_pdo);
4518 port->operating_snk_mw = operating_snk_mw;
4519 port->update_sink_caps = true;
4521 switch (port->state) {
4522 case SNK_NEGOTIATE_CAPABILITIES:
4523 case SNK_NEGOTIATE_PPS_CAPABILITIES:
4524 case SNK_READY:
4525 case SNK_TRANSITION_SINK:
4526 case SNK_TRANSITION_SINK_VBUS:
4527 if (port->pps_data.active)
4528 tcpm_set_state(port, SNK_NEGOTIATE_PPS_CAPABILITIES, 0);
4529 else
4530 tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
4531 break;
4532 default:
4533 break;
4535 mutex_unlock(&port->lock);
4536 return 0;
4538 EXPORT_SYMBOL_GPL(tcpm_update_sink_capabilities);
4540 /* Power Supply access to expose source power information */
4541 enum tcpm_psy_online_states {
4542 TCPM_PSY_OFFLINE = 0,
4543 TCPM_PSY_FIXED_ONLINE,
4544 TCPM_PSY_PROG_ONLINE,
4547 static enum power_supply_property tcpm_psy_props[] = {
4548 POWER_SUPPLY_PROP_USB_TYPE,
4549 POWER_SUPPLY_PROP_ONLINE,
4550 POWER_SUPPLY_PROP_VOLTAGE_MIN,
4551 POWER_SUPPLY_PROP_VOLTAGE_MAX,
4552 POWER_SUPPLY_PROP_VOLTAGE_NOW,
4553 POWER_SUPPLY_PROP_CURRENT_MAX,
4554 POWER_SUPPLY_PROP_CURRENT_NOW,
4557 static int tcpm_psy_get_online(struct tcpm_port *port,
4558 union power_supply_propval *val)
4560 if (port->vbus_charge) {
4561 if (port->pps_data.active)
4562 val->intval = TCPM_PSY_PROG_ONLINE;
4563 else
4564 val->intval = TCPM_PSY_FIXED_ONLINE;
4565 } else {
4566 val->intval = TCPM_PSY_OFFLINE;
4569 return 0;
4572 static int tcpm_psy_get_voltage_min(struct tcpm_port *port,
4573 union power_supply_propval *val)
4575 if (port->pps_data.active)
4576 val->intval = port->pps_data.min_volt * 1000;
4577 else
4578 val->intval = port->supply_voltage * 1000;
4580 return 0;
4583 static int tcpm_psy_get_voltage_max(struct tcpm_port *port,
4584 union power_supply_propval *val)
4586 if (port->pps_data.active)
4587 val->intval = port->pps_data.max_volt * 1000;
4588 else
4589 val->intval = port->supply_voltage * 1000;
4591 return 0;
4594 static int tcpm_psy_get_voltage_now(struct tcpm_port *port,
4595 union power_supply_propval *val)
4597 val->intval = port->supply_voltage * 1000;
4599 return 0;
4602 static int tcpm_psy_get_current_max(struct tcpm_port *port,
4603 union power_supply_propval *val)
4605 if (port->pps_data.active)
4606 val->intval = port->pps_data.max_curr * 1000;
4607 else
4608 val->intval = port->current_limit * 1000;
4610 return 0;
4613 static int tcpm_psy_get_current_now(struct tcpm_port *port,
4614 union power_supply_propval *val)
4616 val->intval = port->current_limit * 1000;
4618 return 0;
4621 static int tcpm_psy_get_prop(struct power_supply *psy,
4622 enum power_supply_property psp,
4623 union power_supply_propval *val)
4625 struct tcpm_port *port = power_supply_get_drvdata(psy);
4626 int ret = 0;
4628 switch (psp) {
4629 case POWER_SUPPLY_PROP_USB_TYPE:
4630 val->intval = port->usb_type;
4631 break;
4632 case POWER_SUPPLY_PROP_ONLINE:
4633 ret = tcpm_psy_get_online(port, val);
4634 break;
4635 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
4636 ret = tcpm_psy_get_voltage_min(port, val);
4637 break;
4638 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
4639 ret = tcpm_psy_get_voltage_max(port, val);
4640 break;
4641 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
4642 ret = tcpm_psy_get_voltage_now(port, val);
4643 break;
4644 case POWER_SUPPLY_PROP_CURRENT_MAX:
4645 ret = tcpm_psy_get_current_max(port, val);
4646 break;
4647 case POWER_SUPPLY_PROP_CURRENT_NOW:
4648 ret = tcpm_psy_get_current_now(port, val);
4649 break;
4650 default:
4651 ret = -EINVAL;
4652 break;
4655 return ret;
4658 static int tcpm_psy_set_online(struct tcpm_port *port,
4659 const union power_supply_propval *val)
4661 int ret;
4663 switch (val->intval) {
4664 case TCPM_PSY_FIXED_ONLINE:
4665 ret = tcpm_pps_activate(port, false);
4666 break;
4667 case TCPM_PSY_PROG_ONLINE:
4668 ret = tcpm_pps_activate(port, true);
4669 break;
4670 default:
4671 ret = -EINVAL;
4672 break;
4675 return ret;
4678 static int tcpm_psy_set_prop(struct power_supply *psy,
4679 enum power_supply_property psp,
4680 const union power_supply_propval *val)
4682 struct tcpm_port *port = power_supply_get_drvdata(psy);
4683 int ret;
4685 switch (psp) {
4686 case POWER_SUPPLY_PROP_ONLINE:
4687 ret = tcpm_psy_set_online(port, val);
4688 break;
4689 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
4690 if (val->intval < port->pps_data.min_volt * 1000 ||
4691 val->intval > port->pps_data.max_volt * 1000)
4692 ret = -EINVAL;
4693 else
4694 ret = tcpm_pps_set_out_volt(port, val->intval / 1000);
4695 break;
4696 case POWER_SUPPLY_PROP_CURRENT_NOW:
4697 if (val->intval > port->pps_data.max_curr * 1000)
4698 ret = -EINVAL;
4699 else
4700 ret = tcpm_pps_set_op_curr(port, val->intval / 1000);
4701 break;
4702 default:
4703 ret = -EINVAL;
4704 break;
4707 return ret;
4710 static int tcpm_psy_prop_writeable(struct power_supply *psy,
4711 enum power_supply_property psp)
4713 switch (psp) {
4714 case POWER_SUPPLY_PROP_ONLINE:
4715 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
4716 case POWER_SUPPLY_PROP_CURRENT_NOW:
4717 return 1;
4718 default:
4719 return 0;
4723 static enum power_supply_usb_type tcpm_psy_usb_types[] = {
4724 POWER_SUPPLY_USB_TYPE_C,
4725 POWER_SUPPLY_USB_TYPE_PD,
4726 POWER_SUPPLY_USB_TYPE_PD_PPS,
4729 static const char *tcpm_psy_name_prefix = "tcpm-source-psy-";
4731 static int devm_tcpm_psy_register(struct tcpm_port *port)
4733 struct power_supply_config psy_cfg = {};
4734 const char *port_dev_name = dev_name(port->dev);
4735 size_t psy_name_len = strlen(tcpm_psy_name_prefix) +
4736 strlen(port_dev_name) + 1;
4737 char *psy_name;
4739 psy_cfg.drv_data = port;
4740 psy_cfg.fwnode = dev_fwnode(port->dev);
4741 psy_name = devm_kzalloc(port->dev, psy_name_len, GFP_KERNEL);
4742 if (!psy_name)
4743 return -ENOMEM;
4745 snprintf(psy_name, psy_name_len, "%s%s", tcpm_psy_name_prefix,
4746 port_dev_name);
4747 port->psy_desc.name = psy_name;
4748 port->psy_desc.type = POWER_SUPPLY_TYPE_USB,
4749 port->psy_desc.usb_types = tcpm_psy_usb_types;
4750 port->psy_desc.num_usb_types = ARRAY_SIZE(tcpm_psy_usb_types);
4751 port->psy_desc.properties = tcpm_psy_props,
4752 port->psy_desc.num_properties = ARRAY_SIZE(tcpm_psy_props),
4753 port->psy_desc.get_property = tcpm_psy_get_prop,
4754 port->psy_desc.set_property = tcpm_psy_set_prop,
4755 port->psy_desc.property_is_writeable = tcpm_psy_prop_writeable,
4757 port->usb_type = POWER_SUPPLY_USB_TYPE_C;
4759 port->psy = devm_power_supply_register(port->dev, &port->psy_desc,
4760 &psy_cfg);
4762 return PTR_ERR_OR_ZERO(port->psy);
4765 static int tcpm_copy_caps(struct tcpm_port *port,
4766 const struct tcpc_config *tcfg)
4768 if (tcpm_validate_caps(port, tcfg->src_pdo, tcfg->nr_src_pdo) ||
4769 tcpm_validate_caps(port, tcfg->snk_pdo, tcfg->nr_snk_pdo))
4770 return -EINVAL;
4772 port->nr_src_pdo = tcpm_copy_pdos(port->src_pdo, tcfg->src_pdo,
4773 tcfg->nr_src_pdo);
4774 port->nr_snk_pdo = tcpm_copy_pdos(port->snk_pdo, tcfg->snk_pdo,
4775 tcfg->nr_snk_pdo);
4777 port->nr_snk_vdo = tcpm_copy_vdos(port->snk_vdo, tcfg->snk_vdo,
4778 tcfg->nr_snk_vdo);
4780 port->operating_snk_mw = tcfg->operating_snk_mw;
4782 port->typec_caps.prefer_role = tcfg->default_role;
4783 port->typec_caps.type = tcfg->type;
4784 port->typec_caps.data = tcfg->data;
4785 port->self_powered = tcfg->self_powered;
4787 return 0;
4790 struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
4792 struct tcpm_port *port;
4793 int i, err;
4795 if (!dev || !tcpc ||
4796 !tcpc->get_vbus || !tcpc->set_cc || !tcpc->get_cc ||
4797 !tcpc->set_polarity || !tcpc->set_vconn || !tcpc->set_vbus ||
4798 !tcpc->set_pd_rx || !tcpc->set_roles || !tcpc->pd_transmit)
4799 return ERR_PTR(-EINVAL);
4801 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
4802 if (!port)
4803 return ERR_PTR(-ENOMEM);
4805 port->dev = dev;
4806 port->tcpc = tcpc;
4808 mutex_init(&port->lock);
4809 mutex_init(&port->swap_lock);
4811 port->wq = create_singlethread_workqueue(dev_name(dev));
4812 if (!port->wq)
4813 return ERR_PTR(-ENOMEM);
4814 INIT_DELAYED_WORK(&port->state_machine, tcpm_state_machine_work);
4815 INIT_DELAYED_WORK(&port->vdm_state_machine, vdm_state_machine_work);
4816 INIT_WORK(&port->event_work, tcpm_pd_event_handler);
4818 spin_lock_init(&port->pd_event_lock);
4820 init_completion(&port->tx_complete);
4821 init_completion(&port->swap_complete);
4822 init_completion(&port->pps_complete);
4823 tcpm_debugfs_init(port);
4825 err = tcpm_fw_get_caps(port, tcpc->fwnode);
4826 if ((err < 0) && tcpc->config)
4827 err = tcpm_copy_caps(port, tcpc->config);
4828 if (err < 0)
4829 goto out_destroy_wq;
4831 if (!tcpc->config || !tcpc->config->try_role_hw)
4832 port->try_role = port->typec_caps.prefer_role;
4833 else
4834 port->try_role = TYPEC_NO_PREFERRED_ROLE;
4836 port->typec_caps.fwnode = tcpc->fwnode;
4837 port->typec_caps.revision = 0x0120; /* Type-C spec release 1.2 */
4838 port->typec_caps.pd_revision = 0x0300; /* USB-PD spec release 3.0 */
4839 port->typec_caps.dr_set = tcpm_dr_set;
4840 port->typec_caps.pr_set = tcpm_pr_set;
4841 port->typec_caps.vconn_set = tcpm_vconn_set;
4842 port->typec_caps.try_role = tcpm_try_role;
4843 port->typec_caps.port_type_set = tcpm_port_type_set;
4845 port->partner_desc.identity = &port->partner_ident;
4846 port->port_type = port->typec_caps.type;
4848 port->role_sw = usb_role_switch_get(port->dev);
4849 if (IS_ERR(port->role_sw)) {
4850 err = PTR_ERR(port->role_sw);
4851 goto out_destroy_wq;
4854 err = devm_tcpm_psy_register(port);
4855 if (err)
4856 goto out_destroy_wq;
4858 port->typec_port = typec_register_port(port->dev, &port->typec_caps);
4859 if (IS_ERR(port->typec_port)) {
4860 err = PTR_ERR(port->typec_port);
4861 goto out_destroy_wq;
4864 if (tcpc->config && tcpc->config->alt_modes) {
4865 const struct typec_altmode_desc *paltmode = tcpc->config->alt_modes;
4867 i = 0;
4868 while (paltmode->svid && i < ARRAY_SIZE(port->port_altmode)) {
4869 struct typec_altmode *alt;
4871 alt = typec_port_register_altmode(port->typec_port,
4872 paltmode);
4873 if (IS_ERR(alt)) {
4874 tcpm_log(port,
4875 "%s: failed to register port alternate mode 0x%x",
4876 dev_name(dev), paltmode->svid);
4877 break;
4879 typec_altmode_set_drvdata(alt, port);
4880 alt->ops = &tcpm_altmode_ops;
4881 port->port_altmode[i] = alt;
4882 i++;
4883 paltmode++;
4887 mutex_lock(&port->lock);
4888 tcpm_init(port);
4889 mutex_unlock(&port->lock);
4891 tcpm_log(port, "%s: registered", dev_name(dev));
4892 return port;
4894 out_destroy_wq:
4895 usb_role_switch_put(port->role_sw);
4896 destroy_workqueue(port->wq);
4897 return ERR_PTR(err);
4899 EXPORT_SYMBOL_GPL(tcpm_register_port);
4901 void tcpm_unregister_port(struct tcpm_port *port)
4903 int i;
4905 tcpm_reset_port(port);
4906 for (i = 0; i < ARRAY_SIZE(port->port_altmode); i++)
4907 typec_unregister_altmode(port->port_altmode[i]);
4908 typec_unregister_port(port->typec_port);
4909 usb_role_switch_put(port->role_sw);
4910 tcpm_debugfs_exit(port);
4911 destroy_workqueue(port->wq);
4913 EXPORT_SYMBOL_GPL(tcpm_unregister_port);
4915 MODULE_AUTHOR("Guenter Roeck <groeck@chromium.org>");
4916 MODULE_DESCRIPTION("USB Type-C Port Manager");
4917 MODULE_LICENSE("GPL");