2 * Copyright 2015-2017 Google, Inc
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * USB Power Delivery protocol stack.
17 #include <linux/completion.h>
18 #include <linux/debugfs.h>
19 #include <linux/device.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/proc_fs.h>
24 #include <linux/sched/clock.h>
25 #include <linux/seq_file.h>
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28 #include <linux/usb/typec.h>
29 #include <linux/workqueue.h>
36 #define FOREACH_STATE(S) \
43 S(SRC_SEND_CAPABILITIES), \
44 S(SRC_NEGOTIATE_CAPABILITIES), \
45 S(SRC_TRANSITION_SUPPLY), \
47 S(SRC_WAIT_NEW_CAPABILITIES), \
55 S(SNK_DISCOVERY_DEBOUNCE), \
56 S(SNK_DISCOVERY_DEBOUNCE_DONE), \
57 S(SNK_WAIT_CAPABILITIES), \
58 S(SNK_NEGOTIATE_CAPABILITIES), \
59 S(SNK_TRANSITION_SINK), \
60 S(SNK_TRANSITION_SINK_VBUS), \
64 S(DEBUG_ACC_ATTACHED), \
65 S(AUDIO_ACC_ATTACHED), \
66 S(AUDIO_ACC_DEBOUNCE), \
69 S(HARD_RESET_START), \
70 S(SRC_HARD_RESET_VBUS_OFF), \
71 S(SRC_HARD_RESET_VBUS_ON), \
72 S(SNK_HARD_RESET_SINK_OFF), \
73 S(SNK_HARD_RESET_WAIT_VBUS), \
74 S(SNK_HARD_RESET_SINK_ON), \
81 S(DR_SWAP_SEND_TIMEOUT), \
83 S(DR_SWAP_CHANGE_DR), \
87 S(PR_SWAP_SEND_TIMEOUT), \
90 S(PR_SWAP_SRC_SNK_TRANSITION_OFF), \
91 S(PR_SWAP_SRC_SNK_SOURCE_OFF), \
92 S(PR_SWAP_SRC_SNK_SINK_ON), \
93 S(PR_SWAP_SNK_SRC_SINK_OFF), \
94 S(PR_SWAP_SNK_SRC_SOURCE_ON), \
96 S(VCONN_SWAP_ACCEPT), \
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), \
108 S(SRC_TRYWAIT_UNATTACHED), \
111 S(SRC_TRY_DEBOUNCE), \
113 S(SNK_TRYWAIT_DEBOUNCE), \
114 S(SNK_TRYWAIT_VBUS), \
118 S(ERROR_RECOVERY_WAIT_OFF)
120 #define GENERATE_ENUM(e) e
121 #define GENERATE_STRING(s) #s
124 FOREACH_STATE(GENERATE_ENUM)
127 static const char * const tcpm_states[] = {
128 FOREACH_STATE(GENERATE_STRING)
132 VDM_STATE_ERR_BUSY = -3,
133 VDM_STATE_ERR_SEND = -2,
134 VDM_STATE_ERR_TMOUT = -1,
136 /* Anything >0 represents an active state */
139 VDM_STATE_WAIT_RSP_BUSY = 3,
142 enum pd_msg_request {
146 PD_MSG_DATA_SINK_CAP,
147 PD_MSG_DATA_SOURCE_CAP,
150 /* Events from low level driver */
152 #define TCPM_CC_EVENT BIT(0)
153 #define TCPM_VBUS_EVENT BIT(1)
154 #define TCPM_RESET_EVENT BIT(2)
156 #define LOG_BUFFER_ENTRIES 1024
157 #define LOG_BUFFER_ENTRY_SIZE 128
159 /* Alternate mode support */
161 #define SVID_DISCOVERY_MAX 16
163 struct pd_mode_data {
164 int svid_index; /* current SVID index */
166 u16 svids[SVID_DISCOVERY_MAX];
167 int altmodes; /* number of alternate modes */
168 struct typec_altmode_desc altmode_desc[SVID_DISCOVERY_MAX];
174 struct mutex lock; /* tcpm state machine lock */
175 struct workqueue_struct *wq;
177 struct typec_capability typec_caps;
178 struct typec_port *typec_port;
180 struct tcpc_dev *tcpc;
182 enum typec_role vconn_role;
183 enum typec_role pwr_role;
184 enum typec_data_role data_role;
185 enum typec_pwr_opmode pwr_opmode;
187 struct usb_pd_identity partner_ident;
188 struct typec_partner_desc partner_desc;
189 struct typec_partner *partner;
191 enum typec_cc_status cc_req;
193 enum typec_cc_status cc1;
194 enum typec_cc_status cc2;
195 enum typec_cc_polarity polarity;
211 enum pd_msg_request queued_message;
213 enum tcpm_state enter_state;
214 enum tcpm_state prev_state;
215 enum tcpm_state state;
216 enum tcpm_state delayed_state;
217 unsigned long delayed_runtime;
218 unsigned long delay_ms;
220 spinlock_t pd_event_lock;
223 struct work_struct event_work;
224 struct delayed_work state_machine;
225 struct delayed_work vdm_state_machine;
226 bool state_machine_running;
228 struct completion tx_complete;
229 enum tcpm_transmit_status tx_status;
231 struct mutex swap_lock; /* swap command lock */
233 struct completion swap_complete;
236 unsigned int message_id;
237 unsigned int caps_count;
238 unsigned int hard_reset_count;
240 bool explicit_contract;
241 unsigned int rx_msgid;
243 /* Partner capabilities/requests */
245 u32 source_caps[PDO_MAX_OBJECTS];
246 unsigned int nr_source_caps;
247 u32 sink_caps[PDO_MAX_OBJECTS];
248 unsigned int nr_sink_caps;
250 /* Local capabilities */
251 u32 src_pdo[PDO_MAX_OBJECTS];
252 unsigned int nr_src_pdo;
253 u32 snk_pdo[PDO_MAX_OBJECTS];
254 unsigned int nr_snk_pdo;
255 u32 snk_vdo[VDO_MAX_OBJECTS];
256 unsigned int nr_snk_vdo;
258 unsigned int max_snk_mv;
259 unsigned int max_snk_ma;
260 unsigned int max_snk_mw;
261 unsigned int operating_snk_mw;
263 /* Requested current / voltage */
269 /* PD state for Vendor Defined Messages */
270 enum vdm_states vdm_state;
272 /* next Vendor Defined Message to send */
273 u32 vdo_data[VDO_MAX_SIZE];
275 /* VDO to retry if UFP responder replied busy */
278 /* Alternate mode data */
280 struct pd_mode_data mode_data;
281 struct typec_altmode *partner_altmode[SVID_DISCOVERY_MAX];
282 struct typec_altmode *port_altmode[SVID_DISCOVERY_MAX];
284 #ifdef CONFIG_DEBUG_FS
285 struct dentry *dentry;
286 struct mutex logbuffer_lock; /* log buffer access lock */
289 u8 *logbuffer[LOG_BUFFER_ENTRIES];
294 struct work_struct work;
295 struct tcpm_port *port;
296 struct pd_message msg;
299 #define tcpm_cc_is_sink(cc) \
300 ((cc) == TYPEC_CC_RP_DEF || (cc) == TYPEC_CC_RP_1_5 || \
301 (cc) == TYPEC_CC_RP_3_0)
303 #define tcpm_port_is_sink(port) \
304 ((tcpm_cc_is_sink((port)->cc1) && !tcpm_cc_is_sink((port)->cc2)) || \
305 (tcpm_cc_is_sink((port)->cc2) && !tcpm_cc_is_sink((port)->cc1)))
307 #define tcpm_cc_is_source(cc) ((cc) == TYPEC_CC_RD)
308 #define tcpm_cc_is_audio(cc) ((cc) == TYPEC_CC_RA)
309 #define tcpm_cc_is_open(cc) ((cc) == TYPEC_CC_OPEN)
311 #define tcpm_port_is_source(port) \
312 ((tcpm_cc_is_source((port)->cc1) && \
313 !tcpm_cc_is_source((port)->cc2)) || \
314 (tcpm_cc_is_source((port)->cc2) && \
315 !tcpm_cc_is_source((port)->cc1)))
317 #define tcpm_port_is_debug(port) \
318 (tcpm_cc_is_source((port)->cc1) && tcpm_cc_is_source((port)->cc2))
320 #define tcpm_port_is_audio(port) \
321 (tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_audio((port)->cc2))
323 #define tcpm_port_is_audio_detached(port) \
324 ((tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_open((port)->cc2)) || \
325 (tcpm_cc_is_audio((port)->cc2) && tcpm_cc_is_open((port)->cc1)))
327 #define tcpm_try_snk(port) \
328 ((port)->try_snk_count == 0 && (port)->try_role == TYPEC_SINK)
330 #define tcpm_try_src(port) \
331 ((port)->try_src_count == 0 && (port)->try_role == TYPEC_SOURCE)
333 static enum tcpm_state tcpm_default_state(struct tcpm_port *port)
335 if (port->typec_caps.type == TYPEC_PORT_DRP) {
336 if (port->try_role == TYPEC_SINK)
337 return SNK_UNATTACHED;
338 else if (port->try_role == TYPEC_SOURCE)
339 return SRC_UNATTACHED;
340 else if (port->tcpc->config->default_role == TYPEC_SINK)
341 return SNK_UNATTACHED;
342 /* Fall through to return SRC_UNATTACHED */
343 } else if (port->typec_caps.type == TYPEC_PORT_UFP) {
344 return SNK_UNATTACHED;
346 return SRC_UNATTACHED;
350 struct tcpm_port *typec_cap_to_tcpm(const struct typec_capability *cap)
352 return container_of(cap, struct tcpm_port, typec_caps);
355 static bool tcpm_port_is_disconnected(struct tcpm_port *port)
357 return (!port->attached && port->cc1 == TYPEC_CC_OPEN &&
358 port->cc2 == TYPEC_CC_OPEN) ||
359 (port->attached && ((port->polarity == TYPEC_POLARITY_CC1 &&
360 port->cc1 == TYPEC_CC_OPEN) ||
361 (port->polarity == TYPEC_POLARITY_CC2 &&
362 port->cc2 == TYPEC_CC_OPEN)));
369 #ifdef CONFIG_DEBUG_FS
371 static bool tcpm_log_full(struct tcpm_port *port)
373 return port->logbuffer_tail ==
374 (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
377 static void _tcpm_log(struct tcpm_port *port, const char *fmt, va_list args)
379 char tmpbuffer[LOG_BUFFER_ENTRY_SIZE];
380 u64 ts_nsec = local_clock();
381 unsigned long rem_nsec;
383 if (!port->logbuffer[port->logbuffer_head]) {
384 port->logbuffer[port->logbuffer_head] =
385 kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL);
386 if (!port->logbuffer[port->logbuffer_head])
390 vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args);
392 mutex_lock(&port->logbuffer_lock);
394 if (tcpm_log_full(port)) {
395 port->logbuffer_head = max(port->logbuffer_head - 1, 0);
396 strcpy(tmpbuffer, "overflow");
399 if (port->logbuffer_head < 0 ||
400 port->logbuffer_head >= LOG_BUFFER_ENTRIES) {
402 "Bad log buffer index %d\n", port->logbuffer_head);
406 if (!port->logbuffer[port->logbuffer_head]) {
408 "Log buffer index %d is NULL\n", port->logbuffer_head);
412 rem_nsec = do_div(ts_nsec, 1000000000);
413 scnprintf(port->logbuffer[port->logbuffer_head],
414 LOG_BUFFER_ENTRY_SIZE, "[%5lu.%06lu] %s",
415 (unsigned long)ts_nsec, rem_nsec / 1000,
417 port->logbuffer_head = (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
420 mutex_unlock(&port->logbuffer_lock);
423 static void tcpm_log(struct tcpm_port *port, const char *fmt, ...)
427 /* Do not log while disconnected and unattached */
428 if (tcpm_port_is_disconnected(port) &&
429 (port->state == SRC_UNATTACHED || port->state == SNK_UNATTACHED ||
430 port->state == DRP_TOGGLING))
434 _tcpm_log(port, fmt, args);
438 static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...)
443 _tcpm_log(port, fmt, args);
447 static void tcpm_log_source_caps(struct tcpm_port *port)
451 for (i = 0; i < port->nr_source_caps; i++) {
452 u32 pdo = port->source_caps[i];
453 enum pd_pdo_type type = pdo_type(pdo);
458 scnprintf(msg, sizeof(msg),
459 "%u mV, %u mA [%s%s%s%s%s%s]",
460 pdo_fixed_voltage(pdo),
461 pdo_max_current(pdo),
462 (pdo & PDO_FIXED_DUAL_ROLE) ?
464 (pdo & PDO_FIXED_SUSPEND) ?
466 (pdo & PDO_FIXED_HIGHER_CAP) ?
468 (pdo & PDO_FIXED_USB_COMM) ?
470 (pdo & PDO_FIXED_DATA_SWAP) ?
472 (pdo & PDO_FIXED_EXTPOWER) ?
476 scnprintf(msg, sizeof(msg),
478 pdo_min_voltage(pdo),
479 pdo_max_voltage(pdo),
480 pdo_max_current(pdo));
483 scnprintf(msg, sizeof(msg),
485 pdo_min_voltage(pdo),
486 pdo_max_voltage(pdo),
490 strcpy(msg, "undefined");
493 tcpm_log(port, " PDO %d: type %d, %s",
498 static int tcpm_seq_show(struct seq_file *s, void *v)
500 struct tcpm_port *port = (struct tcpm_port *)s->private;
503 mutex_lock(&port->logbuffer_lock);
504 tail = port->logbuffer_tail;
505 while (tail != port->logbuffer_head) {
506 seq_printf(s, "%s\n", port->logbuffer[tail]);
507 tail = (tail + 1) % LOG_BUFFER_ENTRIES;
509 if (!seq_has_overflowed(s))
510 port->logbuffer_tail = tail;
511 mutex_unlock(&port->logbuffer_lock);
516 static int tcpm_debug_open(struct inode *inode, struct file *file)
518 return single_open(file, tcpm_seq_show, inode->i_private);
521 static const struct file_operations tcpm_debug_operations = {
522 .open = tcpm_debug_open,
525 .release = single_release,
528 static struct dentry *rootdir;
530 static int tcpm_debugfs_init(struct tcpm_port *port)
532 mutex_init(&port->logbuffer_lock);
533 /* /sys/kernel/debug/tcpm/usbcX */
535 rootdir = debugfs_create_dir("tcpm", NULL);
540 port->dentry = debugfs_create_file(dev_name(port->dev),
541 S_IFREG | 0444, rootdir,
542 port, &tcpm_debug_operations);
547 static void tcpm_debugfs_exit(struct tcpm_port *port)
549 debugfs_remove(port->dentry);
554 static void tcpm_log(const struct tcpm_port *port, const char *fmt, ...) { }
555 static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...) { }
556 static void tcpm_log_source_caps(struct tcpm_port *port) { }
557 static int tcpm_debugfs_init(const struct tcpm_port *port) { return 0; }
558 static void tcpm_debugfs_exit(const struct tcpm_port *port) { }
562 static int tcpm_pd_transmit(struct tcpm_port *port,
563 enum tcpm_transmit_type type,
564 const struct pd_message *msg)
566 unsigned long timeout;
570 tcpm_log(port, "PD TX, header: %#x", le16_to_cpu(msg->header));
572 tcpm_log(port, "PD TX, type: %#x", type);
574 reinit_completion(&port->tx_complete);
575 ret = port->tcpc->pd_transmit(port->tcpc, type, msg);
579 mutex_unlock(&port->lock);
580 timeout = wait_for_completion_timeout(&port->tx_complete,
581 msecs_to_jiffies(PD_T_TCPC_TX_TIMEOUT));
582 mutex_lock(&port->lock);
586 switch (port->tx_status) {
587 case TCPC_TX_SUCCESS:
588 port->message_id = (port->message_id + 1) & PD_HEADER_ID_MASK;
590 case TCPC_TX_DISCARDED:
598 void tcpm_pd_transmit_complete(struct tcpm_port *port,
599 enum tcpm_transmit_status status)
601 tcpm_log(port, "PD TX complete, status: %u", status);
602 port->tx_status = status;
603 complete(&port->tx_complete);
605 EXPORT_SYMBOL_GPL(tcpm_pd_transmit_complete);
607 static int tcpm_mux_set(struct tcpm_port *port, enum tcpc_mux_mode mode,
608 enum tcpc_usb_switch config)
612 tcpm_log(port, "Requesting mux mode %d, config %d, polarity %d",
613 mode, config, port->polarity);
616 ret = port->tcpc->mux->set(port->tcpc->mux, mode, config,
622 static int tcpm_set_polarity(struct tcpm_port *port,
623 enum typec_cc_polarity polarity)
627 tcpm_log(port, "polarity %d", polarity);
629 ret = port->tcpc->set_polarity(port->tcpc, polarity);
633 port->polarity = polarity;
638 static int tcpm_set_vconn(struct tcpm_port *port, bool enable)
642 tcpm_log(port, "vconn:=%d", enable);
644 ret = port->tcpc->set_vconn(port->tcpc, enable);
646 port->vconn_role = enable ? TYPEC_SOURCE : TYPEC_SINK;
647 typec_set_vconn_role(port->typec_port, port->vconn_role);
653 static u32 tcpm_get_current_limit(struct tcpm_port *port)
655 enum typec_cc_status cc;
658 cc = port->polarity ? port->cc2 : port->cc1;
660 case TYPEC_CC_RP_1_5:
663 case TYPEC_CC_RP_3_0:
666 case TYPEC_CC_RP_DEF:
675 static int tcpm_set_current_limit(struct tcpm_port *port, u32 max_ma, u32 mv)
677 int ret = -EOPNOTSUPP;
679 tcpm_log(port, "Setting voltage/current limit %u mV %u mA", mv, max_ma);
681 if (port->tcpc->set_current_limit)
682 ret = port->tcpc->set_current_limit(port->tcpc, max_ma, mv);
688 * Determine RP value to set based on maximum current supported
689 * by a port if configured as source.
690 * Returns CC value to report to link partner.
692 static enum typec_cc_status tcpm_rp_cc(struct tcpm_port *port)
694 const u32 *src_pdo = port->src_pdo;
695 int nr_pdo = port->nr_src_pdo;
699 * Search for first entry with matching voltage.
700 * It should report the maximum supported current.
702 for (i = 0; i < nr_pdo; i++) {
703 const u32 pdo = src_pdo[i];
705 if (pdo_type(pdo) == PDO_TYPE_FIXED &&
706 pdo_fixed_voltage(pdo) == 5000) {
707 unsigned int curr = pdo_max_current(pdo);
710 return TYPEC_CC_RP_3_0;
711 else if (curr >= 1500)
712 return TYPEC_CC_RP_1_5;
713 return TYPEC_CC_RP_DEF;
717 return TYPEC_CC_RP_DEF;
720 static int tcpm_set_attached_state(struct tcpm_port *port, bool attached)
722 return port->tcpc->set_roles(port->tcpc, attached, port->pwr_role,
726 static int tcpm_set_roles(struct tcpm_port *port, bool attached,
727 enum typec_role role, enum typec_data_role data)
731 if (data == TYPEC_HOST)
732 ret = tcpm_mux_set(port, TYPEC_MUX_USB,
733 TCPC_USB_SWITCH_CONNECT);
735 ret = tcpm_mux_set(port, TYPEC_MUX_NONE,
736 TCPC_USB_SWITCH_DISCONNECT);
740 ret = port->tcpc->set_roles(port->tcpc, attached, role, data);
744 port->pwr_role = role;
745 port->data_role = data;
746 typec_set_data_role(port->typec_port, data);
747 typec_set_pwr_role(port->typec_port, role);
752 static int tcpm_set_pwr_role(struct tcpm_port *port, enum typec_role role)
756 ret = port->tcpc->set_roles(port->tcpc, true, role,
761 port->pwr_role = role;
762 typec_set_pwr_role(port->typec_port, role);
767 static int tcpm_pd_send_source_caps(struct tcpm_port *port)
769 struct pd_message msg;
772 memset(&msg, 0, sizeof(msg));
773 if (!port->nr_src_pdo) {
774 /* No source capabilities defined, sink only */
775 msg.header = PD_HEADER_LE(PD_CTRL_REJECT,
778 port->message_id, 0);
780 msg.header = PD_HEADER_LE(PD_DATA_SOURCE_CAP,
786 for (i = 0; i < port->nr_src_pdo; i++)
787 msg.payload[i] = cpu_to_le32(port->src_pdo[i]);
789 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
792 static int tcpm_pd_send_sink_caps(struct tcpm_port *port)
794 struct pd_message msg;
797 memset(&msg, 0, sizeof(msg));
798 if (!port->nr_snk_pdo) {
799 /* No sink capabilities defined, source only */
800 msg.header = PD_HEADER_LE(PD_CTRL_REJECT,
803 port->message_id, 0);
805 msg.header = PD_HEADER_LE(PD_DATA_SINK_CAP,
811 for (i = 0; i < port->nr_snk_pdo; i++)
812 msg.payload[i] = cpu_to_le32(port->snk_pdo[i]);
814 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
817 static void tcpm_set_state(struct tcpm_port *port, enum tcpm_state state,
818 unsigned int delay_ms)
821 tcpm_log(port, "pending state change %s -> %s @ %u ms",
822 tcpm_states[port->state], tcpm_states[state],
824 port->delayed_state = state;
825 mod_delayed_work(port->wq, &port->state_machine,
826 msecs_to_jiffies(delay_ms));
827 port->delayed_runtime = jiffies + msecs_to_jiffies(delay_ms);
828 port->delay_ms = delay_ms;
830 tcpm_log(port, "state change %s -> %s",
831 tcpm_states[port->state], tcpm_states[state]);
832 port->delayed_state = INVALID_STATE;
833 port->prev_state = port->state;
836 * Don't re-queue the state machine work item if we're currently
837 * in the state machine and we're immediately changing states.
838 * tcpm_state_machine_work() will continue running the state
841 if (!port->state_machine_running)
842 mod_delayed_work(port->wq, &port->state_machine, 0);
846 static void tcpm_set_state_cond(struct tcpm_port *port, enum tcpm_state state,
847 unsigned int delay_ms)
849 if (port->enter_state == port->state)
850 tcpm_set_state(port, state, delay_ms);
853 "skipped %sstate change %s -> %s [%u ms], context state %s",
854 delay_ms ? "delayed " : "",
855 tcpm_states[port->state], tcpm_states[state],
856 delay_ms, tcpm_states[port->enter_state]);
859 static void tcpm_queue_message(struct tcpm_port *port,
860 enum pd_msg_request message)
862 port->queued_message = message;
863 mod_delayed_work(port->wq, &port->state_machine, 0);
867 * VDM/VDO handling functions
869 static void tcpm_queue_vdm(struct tcpm_port *port, const u32 header,
870 const u32 *data, int cnt)
872 port->vdo_count = cnt + 1;
873 port->vdo_data[0] = header;
874 memcpy(&port->vdo_data[1], data, sizeof(u32) * cnt);
875 /* Set ready, vdm state machine will actually send */
876 port->vdm_retries = 0;
877 port->vdm_state = VDM_STATE_READY;
880 static void svdm_consume_identity(struct tcpm_port *port, const __le32 *payload,
883 u32 vdo = le32_to_cpu(payload[VDO_INDEX_IDH]);
884 u32 product = le32_to_cpu(payload[VDO_INDEX_PRODUCT]);
886 memset(&port->mode_data, 0, sizeof(port->mode_data));
888 #if 0 /* Not really a match */
889 switch (PD_IDH_PTYPE(vdo)) {
890 case IDH_PTYPE_UNDEF:
891 port->partner.type = TYPEC_PARTNER_NONE; /* no longer exists */
895 case IDH_PTYPE_PERIPH:
897 case IDH_PTYPE_PCABLE:
899 case IDH_PTYPE_ACABLE:
902 port->partner.type = TYPEC_PARTNER_ALTMODE;
909 port->partner_ident.id_header = vdo;
910 port->partner_ident.cert_stat = le32_to_cpu(payload[VDO_INDEX_CSTAT]);
911 port->partner_ident.product = product;
913 typec_partner_set_identity(port->partner);
915 tcpm_log(port, "Identity: %04x:%04x.%04x",
917 PD_PRODUCT_PID(product), product & 0xffff);
920 static bool svdm_consume_svids(struct tcpm_port *port, const __le32 *payload,
923 struct pd_mode_data *pmdata = &port->mode_data;
926 for (i = 1; i < cnt; i++) {
927 u32 p = le32_to_cpu(payload[i]);
930 svid = (p >> 16) & 0xffff;
934 if (pmdata->nsvids >= SVID_DISCOVERY_MAX)
937 pmdata->svids[pmdata->nsvids++] = svid;
938 tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid);
944 if (pmdata->nsvids >= SVID_DISCOVERY_MAX)
947 pmdata->svids[pmdata->nsvids++] = svid;
948 tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid);
952 tcpm_log(port, "SVID_DISCOVERY_MAX(%d) too low!", SVID_DISCOVERY_MAX);
956 static void svdm_consume_modes(struct tcpm_port *port, const __le32 *payload,
959 struct pd_mode_data *pmdata = &port->mode_data;
960 struct typec_altmode_desc *paltmode;
961 struct typec_mode_desc *pmode;
964 if (pmdata->altmodes >= ARRAY_SIZE(port->partner_altmode)) {
965 /* Already logged in svdm_consume_svids() */
969 paltmode = &pmdata->altmode_desc[pmdata->altmodes];
970 memset(paltmode, 0, sizeof(*paltmode));
972 paltmode->svid = pmdata->svids[pmdata->svid_index];
974 tcpm_log(port, " Alternate mode %d: SVID 0x%04x",
975 pmdata->altmodes, paltmode->svid);
977 for (i = 1; i < cnt && paltmode->n_modes < ALTMODE_MAX_MODES; i++) {
978 pmode = &paltmode->modes[paltmode->n_modes];
979 memset(pmode, 0, sizeof(*pmode));
980 pmode->vdo = le32_to_cpu(payload[i]);
981 pmode->index = i - 1;
983 tcpm_log(port, " VDO %d: 0x%08x",
984 pmode->index, pmode->vdo);
986 port->partner_altmode[pmdata->altmodes] =
987 typec_partner_register_altmode(port->partner, paltmode);
988 if (port->partner_altmode[pmdata->altmodes] == NULL) {
990 "Failed to register alternate modes for SVID 0x%04x",
997 #define supports_modal(port) PD_IDH_MODAL_SUPP((port)->partner_ident.id_header)
999 static int tcpm_pd_svdm(struct tcpm_port *port, const __le32 *payload, int cnt,
1002 u32 p0 = le32_to_cpu(payload[0]);
1003 int cmd_type = PD_VDO_CMDT(p0);
1004 int cmd = PD_VDO_CMD(p0);
1005 struct pd_mode_data *modep;
1010 tcpm_log(port, "Rx VDM cmd 0x%x type %d cmd %d len %d",
1011 p0, cmd_type, cmd, cnt);
1013 modep = &port->mode_data;
1018 case CMD_DISCOVER_IDENT:
1019 /* 6.4.4.3.1: Only respond as UFP (device) */
1020 if (port->data_role == TYPEC_DEVICE &&
1022 for (i = 0; i < port->nr_snk_vdo; i++)
1023 response[i + 1] = port->snk_vdo[i];
1024 rlen = port->nr_snk_vdo + 1;
1027 case CMD_DISCOVER_SVID:
1029 case CMD_DISCOVER_MODES:
1031 case CMD_ENTER_MODE:
1041 response[0] = p0 | VDO_CMDT(CMDT_RSP_ACK);
1042 } else if (rlen == 0) {
1043 response[0] = p0 | VDO_CMDT(CMDT_RSP_NAK);
1046 response[0] = p0 | VDO_CMDT(CMDT_RSP_BUSY);
1051 /* silently drop message if we are not connected */
1056 case CMD_DISCOVER_IDENT:
1058 svdm_consume_identity(port, payload, cnt);
1059 response[0] = VDO(USB_SID_PD, 1, CMD_DISCOVER_SVID);
1062 case CMD_DISCOVER_SVID:
1064 if (svdm_consume_svids(port, payload, cnt)) {
1065 response[0] = VDO(USB_SID_PD, 1,
1068 } else if (modep->nsvids && supports_modal(port)) {
1069 response[0] = VDO(modep->svids[0], 1,
1070 CMD_DISCOVER_MODES);
1074 case CMD_DISCOVER_MODES:
1076 svdm_consume_modes(port, payload, cnt);
1077 modep->svid_index++;
1078 if (modep->svid_index < modep->nsvids) {
1079 svid = modep->svids[modep->svid_index];
1080 response[0] = VDO(svid, 1, CMD_DISCOVER_MODES);
1084 response[0] = pd_dfp_enter_mode(port, 0, 0);
1090 case CMD_ENTER_MODE:
1103 static void tcpm_handle_vdm_request(struct tcpm_port *port,
1104 const __le32 *payload, int cnt)
1107 u32 response[8] = { };
1108 u32 p0 = le32_to_cpu(payload[0]);
1110 if (port->vdm_state == VDM_STATE_BUSY) {
1111 /* If UFP responded busy retry after timeout */
1112 if (PD_VDO_CMDT(p0) == CMDT_RSP_BUSY) {
1113 port->vdm_state = VDM_STATE_WAIT_RSP_BUSY;
1114 port->vdo_retry = (p0 & ~VDO_CMDT_MASK) |
1116 mod_delayed_work(port->wq, &port->vdm_state_machine,
1117 msecs_to_jiffies(PD_T_VDM_BUSY));
1120 port->vdm_state = VDM_STATE_DONE;
1123 if (PD_VDO_SVDM(p0))
1124 rlen = tcpm_pd_svdm(port, payload, cnt, response);
1127 rlen = tcpm_pd_custom_vdm(port, cnt, payload, response);
1131 tcpm_queue_vdm(port, response[0], &response[1], rlen - 1);
1132 mod_delayed_work(port->wq, &port->vdm_state_machine, 0);
1136 static void tcpm_send_vdm(struct tcpm_port *port, u32 vid, int cmd,
1137 const u32 *data, int count)
1141 if (WARN_ON(count > VDO_MAX_SIZE - 1))
1142 count = VDO_MAX_SIZE - 1;
1144 /* set VDM header with VID & CMD */
1145 header = VDO(vid, ((vid & USB_SID_PD) == USB_SID_PD) ?
1146 1 : (PD_VDO_CMD(cmd) <= CMD_ATTENTION), cmd);
1147 tcpm_queue_vdm(port, header, data, count);
1149 mod_delayed_work(port->wq, &port->vdm_state_machine, 0);
1152 static unsigned int vdm_ready_timeout(u32 vdm_hdr)
1154 unsigned int timeout;
1155 int cmd = PD_VDO_CMD(vdm_hdr);
1157 /* its not a structured VDM command */
1158 if (!PD_VDO_SVDM(vdm_hdr))
1159 return PD_T_VDM_UNSTRUCTURED;
1161 switch (PD_VDO_CMDT(vdm_hdr)) {
1163 if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE)
1164 timeout = PD_T_VDM_WAIT_MODE_E;
1166 timeout = PD_T_VDM_SNDR_RSP;
1169 if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE)
1170 timeout = PD_T_VDM_E_MODE;
1172 timeout = PD_T_VDM_RCVR_RSP;
1178 static void vdm_run_state_machine(struct tcpm_port *port)
1180 struct pd_message msg;
1183 switch (port->vdm_state) {
1184 case VDM_STATE_READY:
1185 /* Only transmit VDM if attached */
1186 if (!port->attached) {
1187 port->vdm_state = VDM_STATE_ERR_BUSY;
1192 * if there's traffic or we're not in PDO ready state don't send
1195 if (port->state != SRC_READY && port->state != SNK_READY)
1198 /* Prepare and send VDM */
1199 memset(&msg, 0, sizeof(msg));
1200 msg.header = PD_HEADER_LE(PD_DATA_VENDOR_DEF,
1203 port->message_id, port->vdo_count);
1204 for (i = 0; i < port->vdo_count; i++)
1205 msg.payload[i] = cpu_to_le32(port->vdo_data[i]);
1206 res = tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
1208 port->vdm_state = VDM_STATE_ERR_SEND;
1210 unsigned long timeout;
1212 port->vdm_retries = 0;
1213 port->vdm_state = VDM_STATE_BUSY;
1214 timeout = vdm_ready_timeout(port->vdo_data[0]);
1215 mod_delayed_work(port->wq, &port->vdm_state_machine,
1219 case VDM_STATE_WAIT_RSP_BUSY:
1220 port->vdo_data[0] = port->vdo_retry;
1221 port->vdo_count = 1;
1222 port->vdm_state = VDM_STATE_READY;
1224 case VDM_STATE_BUSY:
1225 port->vdm_state = VDM_STATE_ERR_TMOUT;
1227 case VDM_STATE_ERR_SEND:
1229 * A partner which does not support USB PD will not reply,
1230 * so this is not a fatal error. At the same time, some
1231 * devices may not return GoodCRC under some circumstances,
1232 * so we need to retry.
1234 if (port->vdm_retries < 3) {
1235 tcpm_log(port, "VDM Tx error, retry");
1236 port->vdm_retries++;
1237 port->vdm_state = VDM_STATE_READY;
1245 static void vdm_state_machine_work(struct work_struct *work)
1247 struct tcpm_port *port = container_of(work, struct tcpm_port,
1248 vdm_state_machine.work);
1249 enum vdm_states prev_state;
1251 mutex_lock(&port->lock);
1254 * Continue running as long as the port is not busy and there was
1258 prev_state = port->vdm_state;
1259 vdm_run_state_machine(port);
1260 } while (port->vdm_state != prev_state &&
1261 port->vdm_state != VDM_STATE_BUSY);
1263 mutex_unlock(&port->lock);
1267 * PD (data, control) command handling functions
1269 static void tcpm_pd_data_request(struct tcpm_port *port,
1270 const struct pd_message *msg)
1272 enum pd_data_msg_type type = pd_header_type_le(msg->header);
1273 unsigned int cnt = pd_header_cnt_le(msg->header);
1277 case PD_DATA_SOURCE_CAP:
1278 if (port->pwr_role != TYPEC_SINK)
1281 for (i = 0; i < cnt; i++)
1282 port->source_caps[i] = le32_to_cpu(msg->payload[i]);
1284 port->nr_source_caps = cnt;
1286 tcpm_log_source_caps(port);
1289 * This message may be received even if VBUS is not
1290 * present. This is quite unexpected; see USB PD
1291 * specification, sections 8.3.3.6.3.1 and 8.3.3.6.3.2.
1292 * However, at the same time, we must be ready to
1293 * receive this message and respond to it 15ms after
1294 * receiving PS_RDY during power swap operations, no matter
1295 * if VBUS is available or not (USB PD specification,
1297 * So we need to accept the message either way,
1298 * but be prepared to keep waiting for VBUS after it was
1301 tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
1303 case PD_DATA_REQUEST:
1304 if (port->pwr_role != TYPEC_SOURCE ||
1306 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1309 port->sink_request = le32_to_cpu(msg->payload[0]);
1310 tcpm_set_state(port, SRC_NEGOTIATE_CAPABILITIES, 0);
1312 case PD_DATA_SINK_CAP:
1313 /* We don't do anything with this at the moment... */
1314 for (i = 0; i < cnt; i++)
1315 port->sink_caps[i] = le32_to_cpu(msg->payload[i]);
1316 port->nr_sink_caps = cnt;
1318 case PD_DATA_VENDOR_DEF:
1319 tcpm_handle_vdm_request(port, msg->payload, cnt);
1322 if (port->state == SRC_READY || port->state == SNK_READY) {
1323 port->bist_request = le32_to_cpu(msg->payload[0]);
1324 tcpm_set_state(port, BIST_RX, 0);
1328 tcpm_log(port, "Unhandled data message type %#x", type);
1333 static void tcpm_pd_ctrl_request(struct tcpm_port *port,
1334 const struct pd_message *msg)
1336 enum pd_ctrl_msg_type type = pd_header_type_le(msg->header);
1337 enum tcpm_state next_state;
1340 case PD_CTRL_GOOD_CRC:
1343 case PD_CTRL_GET_SOURCE_CAP:
1344 switch (port->state) {
1347 tcpm_queue_message(port, PD_MSG_DATA_SOURCE_CAP);
1350 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1354 case PD_CTRL_GET_SINK_CAP:
1355 switch (port->state) {
1358 tcpm_queue_message(port, PD_MSG_DATA_SINK_CAP);
1361 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1365 case PD_CTRL_GOTO_MIN:
1367 case PD_CTRL_PS_RDY:
1368 switch (port->state) {
1369 case SNK_TRANSITION_SINK:
1370 if (port->vbus_present) {
1371 tcpm_set_current_limit(port,
1372 port->current_limit,
1373 port->supply_voltage);
1374 tcpm_set_state(port, SNK_READY, 0);
1377 * Seen after power swap. Keep waiting for VBUS
1378 * in a transitional state.
1380 tcpm_set_state(port,
1381 SNK_TRANSITION_SINK_VBUS, 0);
1384 case PR_SWAP_SRC_SNK_SOURCE_OFF:
1385 tcpm_set_state(port, PR_SWAP_SRC_SNK_SINK_ON, 0);
1387 case PR_SWAP_SNK_SRC_SINK_OFF:
1388 tcpm_set_state(port, PR_SWAP_SNK_SRC_SOURCE_ON, 0);
1390 case VCONN_SWAP_WAIT_FOR_VCONN:
1391 tcpm_set_state(port, VCONN_SWAP_TURN_OFF_VCONN, 0);
1397 case PD_CTRL_REJECT:
1399 switch (port->state) {
1400 case SNK_NEGOTIATE_CAPABILITIES:
1401 /* USB PD specification, Figure 8-43 */
1402 if (port->explicit_contract)
1403 next_state = SNK_READY;
1405 next_state = SNK_WAIT_CAPABILITIES;
1406 tcpm_set_state(port, next_state, 0);
1409 port->swap_status = (type == PD_CTRL_WAIT ?
1410 -EAGAIN : -EOPNOTSUPP);
1411 tcpm_set_state(port, DR_SWAP_CANCEL, 0);
1414 port->swap_status = (type == PD_CTRL_WAIT ?
1415 -EAGAIN : -EOPNOTSUPP);
1416 tcpm_set_state(port, PR_SWAP_CANCEL, 0);
1418 case VCONN_SWAP_SEND:
1419 port->swap_status = (type == PD_CTRL_WAIT ?
1420 -EAGAIN : -EOPNOTSUPP);
1421 tcpm_set_state(port, VCONN_SWAP_CANCEL, 0);
1427 case PD_CTRL_ACCEPT:
1428 switch (port->state) {
1429 case SNK_NEGOTIATE_CAPABILITIES:
1430 tcpm_set_state(port, SNK_TRANSITION_SINK, 0);
1432 case SOFT_RESET_SEND:
1433 port->message_id = 0;
1434 port->rx_msgid = -1;
1435 if (port->pwr_role == TYPEC_SOURCE)
1436 next_state = SRC_SEND_CAPABILITIES;
1438 next_state = SNK_WAIT_CAPABILITIES;
1439 tcpm_set_state(port, next_state, 0);
1442 tcpm_set_state(port, DR_SWAP_CHANGE_DR, 0);
1445 tcpm_set_state(port, PR_SWAP_START, 0);
1447 case VCONN_SWAP_SEND:
1448 tcpm_set_state(port, VCONN_SWAP_START, 0);
1454 case PD_CTRL_SOFT_RESET:
1455 tcpm_set_state(port, SOFT_RESET, 0);
1457 case PD_CTRL_DR_SWAP:
1458 if (port->typec_caps.type != TYPEC_PORT_DRP) {
1459 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1464 * 6.3.9: If an alternate mode is active, a request to swap
1465 * alternate modes shall trigger a port reset.
1467 switch (port->state) {
1470 tcpm_set_state(port, DR_SWAP_ACCEPT, 0);
1473 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
1477 case PD_CTRL_PR_SWAP:
1478 if (port->typec_caps.type != TYPEC_PORT_DRP) {
1479 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1482 switch (port->state) {
1485 tcpm_set_state(port, PR_SWAP_ACCEPT, 0);
1488 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
1492 case PD_CTRL_VCONN_SWAP:
1493 switch (port->state) {
1496 tcpm_set_state(port, VCONN_SWAP_ACCEPT, 0);
1499 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
1504 tcpm_log(port, "Unhandled ctrl message type %#x", type);
1509 static void tcpm_pd_rx_handler(struct work_struct *work)
1511 struct pd_rx_event *event = container_of(work,
1512 struct pd_rx_event, work);
1513 const struct pd_message *msg = &event->msg;
1514 unsigned int cnt = pd_header_cnt_le(msg->header);
1515 struct tcpm_port *port = event->port;
1517 mutex_lock(&port->lock);
1519 tcpm_log(port, "PD RX, header: %#x [%d]", le16_to_cpu(msg->header),
1522 if (port->attached) {
1523 enum pd_ctrl_msg_type type = pd_header_type_le(msg->header);
1524 unsigned int msgid = pd_header_msgid_le(msg->header);
1527 * USB PD standard, 6.6.1.2:
1528 * "... if MessageID value in a received Message is the
1529 * same as the stored value, the receiver shall return a
1530 * GoodCRC Message with that MessageID value and drop
1531 * the Message (this is a retry of an already received
1532 * Message). Note: this shall not apply to the Soft_Reset
1533 * Message which always has a MessageID value of zero."
1535 if (msgid == port->rx_msgid && type != PD_CTRL_SOFT_RESET)
1537 port->rx_msgid = msgid;
1540 * If both ends believe to be DFP/host, we have a data role
1543 if (!!(le16_to_cpu(msg->header) & PD_HEADER_DATA_ROLE) ==
1544 (port->data_role == TYPEC_HOST)) {
1546 "Data role mismatch, initiating error recovery");
1547 tcpm_set_state(port, ERROR_RECOVERY, 0);
1550 tcpm_pd_data_request(port, msg);
1552 tcpm_pd_ctrl_request(port, msg);
1557 mutex_unlock(&port->lock);
1561 void tcpm_pd_receive(struct tcpm_port *port, const struct pd_message *msg)
1563 struct pd_rx_event *event;
1565 event = kzalloc(sizeof(*event), GFP_ATOMIC);
1569 INIT_WORK(&event->work, tcpm_pd_rx_handler);
1571 memcpy(&event->msg, msg, sizeof(*msg));
1572 queue_work(port->wq, &event->work);
1574 EXPORT_SYMBOL_GPL(tcpm_pd_receive);
1576 static int tcpm_pd_send_control(struct tcpm_port *port,
1577 enum pd_ctrl_msg_type type)
1579 struct pd_message msg;
1581 memset(&msg, 0, sizeof(msg));
1582 msg.header = PD_HEADER_LE(type, port->pwr_role,
1584 port->message_id, 0);
1586 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
1590 * Send queued message without affecting state.
1591 * Return true if state machine should go back to sleep,
1594 static bool tcpm_send_queued_message(struct tcpm_port *port)
1596 enum pd_msg_request queued_message;
1599 queued_message = port->queued_message;
1600 port->queued_message = PD_MSG_NONE;
1602 switch (queued_message) {
1603 case PD_MSG_CTRL_WAIT:
1604 tcpm_pd_send_control(port, PD_CTRL_WAIT);
1606 case PD_MSG_CTRL_REJECT:
1607 tcpm_pd_send_control(port, PD_CTRL_REJECT);
1609 case PD_MSG_DATA_SINK_CAP:
1610 tcpm_pd_send_sink_caps(port);
1612 case PD_MSG_DATA_SOURCE_CAP:
1613 tcpm_pd_send_source_caps(port);
1618 } while (port->queued_message != PD_MSG_NONE);
1620 if (port->delayed_state != INVALID_STATE) {
1621 if (time_is_after_jiffies(port->delayed_runtime)) {
1622 mod_delayed_work(port->wq, &port->state_machine,
1623 port->delayed_runtime - jiffies);
1626 port->delayed_state = INVALID_STATE;
1631 static int tcpm_pd_check_request(struct tcpm_port *port)
1633 u32 pdo, rdo = port->sink_request;
1634 unsigned int max, op, pdo_max, index;
1635 enum pd_pdo_type type;
1637 index = rdo_index(rdo);
1638 if (!index || index > port->nr_src_pdo)
1641 pdo = port->src_pdo[index - 1];
1642 type = pdo_type(pdo);
1644 case PDO_TYPE_FIXED:
1646 max = rdo_max_current(rdo);
1647 op = rdo_op_current(rdo);
1648 pdo_max = pdo_max_current(pdo);
1652 if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH))
1655 if (type == PDO_TYPE_FIXED)
1657 "Requested %u mV, %u mA for %u / %u mA",
1658 pdo_fixed_voltage(pdo), pdo_max, op, max);
1661 "Requested %u -> %u mV, %u mA for %u / %u mA",
1662 pdo_min_voltage(pdo), pdo_max_voltage(pdo),
1666 max = rdo_max_power(rdo);
1667 op = rdo_op_power(rdo);
1668 pdo_max = pdo_max_power(pdo);
1672 if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH))
1675 "Requested %u -> %u mV, %u mW for %u / %u mW",
1676 pdo_min_voltage(pdo), pdo_max_voltage(pdo),
1683 port->op_vsafe5v = index == 1;
1688 static int tcpm_pd_select_pdo(struct tcpm_port *port)
1690 unsigned int i, max_mw = 0, max_mv = 0;
1694 * Select the source PDO providing the most power while staying within
1695 * the board's voltage limits. Prefer PDO providing exp
1697 for (i = 0; i < port->nr_source_caps; i++) {
1698 u32 pdo = port->source_caps[i];
1699 enum pd_pdo_type type = pdo_type(pdo);
1700 unsigned int mv, ma, mw;
1702 if (type == PDO_TYPE_FIXED)
1703 mv = pdo_fixed_voltage(pdo);
1705 mv = pdo_min_voltage(pdo);
1707 if (type == PDO_TYPE_BATT) {
1708 mw = pdo_max_power(pdo);
1710 ma = min(pdo_max_current(pdo),
1712 mw = ma * mv / 1000;
1715 /* Perfer higher voltages if available */
1716 if ((mw > max_mw || (mw == max_mw && mv > max_mv)) &&
1717 mv <= port->max_snk_mv) {
1727 static int tcpm_pd_build_request(struct tcpm_port *port, u32 *rdo)
1729 unsigned int mv, ma, mw, flags;
1730 unsigned int max_ma, max_mw;
1731 enum pd_pdo_type type;
1735 index = tcpm_pd_select_pdo(port);
1738 pdo = port->source_caps[index];
1739 type = pdo_type(pdo);
1741 if (type == PDO_TYPE_FIXED)
1742 mv = pdo_fixed_voltage(pdo);
1744 mv = pdo_min_voltage(pdo);
1746 /* Select maximum available current within the board's power limit */
1747 if (type == PDO_TYPE_BATT) {
1748 mw = pdo_max_power(pdo);
1749 ma = 1000 * min(mw, port->max_snk_mw) / mv;
1751 ma = min(pdo_max_current(pdo),
1752 1000 * port->max_snk_mw / mv);
1754 ma = min(ma, port->max_snk_ma);
1756 flags = RDO_USB_COMM | RDO_NO_SUSPEND;
1758 /* Set mismatch bit if offered power is less than operating power */
1759 mw = ma * mv / 1000;
1762 if (mw < port->operating_snk_mw) {
1763 flags |= RDO_CAP_MISMATCH;
1764 max_mw = port->operating_snk_mw;
1765 max_ma = max_mw * 1000 / mv;
1768 tcpm_log(port, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d",
1769 port->cc_req, port->cc1, port->cc2, port->vbus_source,
1770 port->vconn_role == TYPEC_SOURCE ? "source" : "sink",
1773 if (type == PDO_TYPE_BATT) {
1774 *rdo = RDO_BATT(index + 1, mw, max_mw, flags);
1776 tcpm_log(port, "Requesting PDO %d: %u mV, %u mW%s",
1778 flags & RDO_CAP_MISMATCH ? " [mismatch]" : "");
1780 *rdo = RDO_FIXED(index + 1, ma, max_ma, flags);
1782 tcpm_log(port, "Requesting PDO %d: %u mV, %u mA%s",
1784 flags & RDO_CAP_MISMATCH ? " [mismatch]" : "");
1787 port->current_limit = ma;
1788 port->supply_voltage = mv;
1793 static int tcpm_pd_send_request(struct tcpm_port *port)
1795 struct pd_message msg;
1799 ret = tcpm_pd_build_request(port, &rdo);
1803 memset(&msg, 0, sizeof(msg));
1804 msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
1807 port->message_id, 1);
1808 msg.payload[0] = cpu_to_le32(rdo);
1810 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
1813 static int tcpm_set_vbus(struct tcpm_port *port, bool enable)
1817 if (enable && port->vbus_charge)
1820 tcpm_log(port, "vbus:=%d charge=%d", enable, port->vbus_charge);
1822 ret = port->tcpc->set_vbus(port->tcpc, enable, port->vbus_charge);
1826 port->vbus_source = enable;
1830 static int tcpm_set_charge(struct tcpm_port *port, bool charge)
1834 if (charge && port->vbus_source)
1837 if (charge != port->vbus_charge) {
1838 tcpm_log(port, "vbus=%d charge:=%d", port->vbus_source, charge);
1839 ret = port->tcpc->set_vbus(port->tcpc, port->vbus_source,
1844 port->vbus_charge = charge;
1848 static bool tcpm_start_drp_toggling(struct tcpm_port *port)
1852 if (port->tcpc->start_drp_toggling &&
1853 port->typec_caps.type == TYPEC_PORT_DRP) {
1854 tcpm_log_force(port, "Start DRP toggling");
1855 ret = port->tcpc->start_drp_toggling(port->tcpc,
1864 static void tcpm_set_cc(struct tcpm_port *port, enum typec_cc_status cc)
1866 tcpm_log(port, "cc:=%d", cc);
1868 port->tcpc->set_cc(port->tcpc, cc);
1871 static int tcpm_init_vbus(struct tcpm_port *port)
1875 ret = port->tcpc->set_vbus(port->tcpc, false, false);
1876 port->vbus_source = false;
1877 port->vbus_charge = false;
1881 static int tcpm_init_vconn(struct tcpm_port *port)
1885 ret = port->tcpc->set_vconn(port->tcpc, false);
1886 port->vconn_role = TYPEC_SINK;
1890 static void tcpm_typec_connect(struct tcpm_port *port)
1892 if (!port->connected) {
1893 /* Make sure we don't report stale identity information */
1894 memset(&port->partner_ident, 0, sizeof(port->partner_ident));
1895 port->partner_desc.usb_pd = port->pd_capable;
1896 if (tcpm_port_is_debug(port))
1897 port->partner_desc.accessory = TYPEC_ACCESSORY_DEBUG;
1898 else if (tcpm_port_is_audio(port))
1899 port->partner_desc.accessory = TYPEC_ACCESSORY_AUDIO;
1901 port->partner_desc.accessory = TYPEC_ACCESSORY_NONE;
1902 port->partner = typec_register_partner(port->typec_port,
1903 &port->partner_desc);
1904 port->connected = true;
1908 static int tcpm_src_attach(struct tcpm_port *port)
1910 enum typec_cc_polarity polarity =
1911 port->cc2 == TYPEC_CC_RD ? TYPEC_POLARITY_CC2
1912 : TYPEC_POLARITY_CC1;
1918 ret = tcpm_set_polarity(port, polarity);
1922 ret = tcpm_set_roles(port, true, TYPEC_SOURCE, TYPEC_HOST);
1926 ret = port->tcpc->set_pd_rx(port->tcpc, true);
1928 goto out_disable_mux;
1931 * USB Type-C specification, version 1.2,
1932 * chapter 4.5.2.2.8.1 (Attached.SRC Requirements)
1933 * Enable VCONN only if the non-RD port is set to RA.
1935 if ((polarity == TYPEC_POLARITY_CC1 && port->cc2 == TYPEC_CC_RA) ||
1936 (polarity == TYPEC_POLARITY_CC2 && port->cc1 == TYPEC_CC_RA)) {
1937 ret = tcpm_set_vconn(port, true);
1939 goto out_disable_pd;
1942 ret = tcpm_set_vbus(port, true);
1944 goto out_disable_vconn;
1946 port->pd_capable = false;
1948 port->partner = NULL;
1950 port->attached = true;
1951 port->send_discover = true;
1956 tcpm_set_vconn(port, false);
1958 port->tcpc->set_pd_rx(port->tcpc, false);
1960 tcpm_mux_set(port, TYPEC_MUX_NONE, TCPC_USB_SWITCH_DISCONNECT);
1964 static void tcpm_typec_disconnect(struct tcpm_port *port)
1966 if (port->connected) {
1967 typec_unregister_partner(port->partner);
1968 port->partner = NULL;
1969 port->connected = false;
1973 static void tcpm_unregister_altmodes(struct tcpm_port *port)
1975 struct pd_mode_data *modep = &port->mode_data;
1978 for (i = 0; i < modep->altmodes; i++) {
1979 typec_unregister_altmode(port->partner_altmode[i]);
1980 port->partner_altmode[i] = NULL;
1983 memset(modep, 0, sizeof(*modep));
1986 static void tcpm_reset_port(struct tcpm_port *port)
1988 tcpm_unregister_altmodes(port);
1989 tcpm_typec_disconnect(port);
1990 port->attached = false;
1991 port->pd_capable = false;
1994 * First Rx ID should be 0; set this to a sentinel of -1 so that
1995 * we can check tcpm_pd_rx_handler() if we had seen it before.
1997 port->rx_msgid = -1;
1999 port->tcpc->set_pd_rx(port->tcpc, false);
2000 tcpm_init_vbus(port); /* also disables charging */
2001 tcpm_init_vconn(port);
2002 tcpm_set_current_limit(port, 0, 0);
2003 tcpm_set_polarity(port, TYPEC_POLARITY_CC1);
2004 tcpm_set_attached_state(port, false);
2005 port->try_src_count = 0;
2006 port->try_snk_count = 0;
2009 static void tcpm_detach(struct tcpm_port *port)
2011 if (!port->attached)
2014 if (tcpm_port_is_disconnected(port))
2015 port->hard_reset_count = 0;
2017 tcpm_reset_port(port);
2020 static void tcpm_src_detach(struct tcpm_port *port)
2025 static int tcpm_snk_attach(struct tcpm_port *port)
2032 ret = tcpm_set_polarity(port, port->cc2 != TYPEC_CC_OPEN ?
2033 TYPEC_POLARITY_CC2 : TYPEC_POLARITY_CC1);
2037 ret = tcpm_set_roles(port, true, TYPEC_SINK, TYPEC_DEVICE);
2041 port->pd_capable = false;
2043 port->partner = NULL;
2045 port->attached = true;
2046 port->send_discover = true;
2051 static void tcpm_snk_detach(struct tcpm_port *port)
2055 /* XXX: (Dis)connect SuperSpeed mux? */
2058 static int tcpm_acc_attach(struct tcpm_port *port)
2065 ret = tcpm_set_roles(port, true, TYPEC_SOURCE, TYPEC_HOST);
2069 port->partner = NULL;
2071 tcpm_typec_connect(port);
2073 port->attached = true;
2078 static void tcpm_acc_detach(struct tcpm_port *port)
2083 static inline enum tcpm_state hard_reset_state(struct tcpm_port *port)
2085 if (port->hard_reset_count < PD_N_HARD_RESET_COUNT)
2086 return HARD_RESET_SEND;
2087 if (port->pd_capable)
2088 return ERROR_RECOVERY;
2089 if (port->pwr_role == TYPEC_SOURCE)
2090 return SRC_UNATTACHED;
2091 if (port->state == SNK_WAIT_CAPABILITIES)
2093 return SNK_UNATTACHED;
2096 static inline enum tcpm_state ready_state(struct tcpm_port *port)
2098 if (port->pwr_role == TYPEC_SOURCE)
2104 static inline enum tcpm_state unattached_state(struct tcpm_port *port)
2106 if (port->pwr_role == TYPEC_SOURCE)
2107 return SRC_UNATTACHED;
2109 return SNK_UNATTACHED;
2112 static void tcpm_check_send_discover(struct tcpm_port *port)
2114 if (port->data_role == TYPEC_HOST && port->send_discover &&
2116 tcpm_send_vdm(port, USB_SID_PD, CMD_DISCOVER_IDENT, NULL, 0);
2117 port->send_discover = false;
2121 static void tcpm_swap_complete(struct tcpm_port *port, int result)
2123 if (port->swap_pending) {
2124 port->swap_status = result;
2125 port->swap_pending = false;
2126 complete(&port->swap_complete);
2130 static void run_state_machine(struct tcpm_port *port)
2134 port->enter_state = port->state;
2135 switch (port->state) {
2139 case SRC_UNATTACHED:
2140 tcpm_swap_complete(port, -ENOTCONN);
2141 tcpm_src_detach(port);
2142 if (tcpm_start_drp_toggling(port)) {
2143 tcpm_set_state(port, DRP_TOGGLING, 0);
2146 tcpm_set_cc(port, tcpm_rp_cc(port));
2147 if (port->typec_caps.type == TYPEC_PORT_DRP)
2148 tcpm_set_state(port, SNK_UNATTACHED, PD_T_DRP_SNK);
2150 case SRC_ATTACH_WAIT:
2151 if (tcpm_port_is_debug(port))
2152 tcpm_set_state(port, DEBUG_ACC_ATTACHED,
2154 else if (tcpm_port_is_audio(port))
2155 tcpm_set_state(port, AUDIO_ACC_ATTACHED,
2157 else if (tcpm_port_is_source(port))
2158 tcpm_set_state(port,
2159 tcpm_try_snk(port) ? SNK_TRY
2165 port->try_snk_count++;
2168 * - Do not drive vconn or vbus
2169 * - Terminate CC pins (both) to Rd
2171 * - Wait for tDRPTry (PD_T_DRP_TRY).
2172 * Until then, ignore any state changes.
2174 tcpm_set_cc(port, TYPEC_CC_RD);
2175 tcpm_set_state(port, SNK_TRY_WAIT, PD_T_DRP_TRY);
2178 if (port->vbus_present && tcpm_port_is_sink(port)) {
2179 tcpm_set_state(port, SNK_ATTACHED, 0);
2182 if (!tcpm_port_is_sink(port)) {
2183 tcpm_set_state(port, SRC_TRYWAIT,
2187 /* No vbus, cc state is sink or open */
2188 tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED, PD_T_DRP_TRYWAIT);
2191 tcpm_set_cc(port, tcpm_rp_cc(port));
2192 if (!port->vbus_present && tcpm_port_is_source(port))
2193 tcpm_set_state(port, SRC_ATTACHED, PD_T_CC_DEBOUNCE);
2195 tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED,
2198 case SRC_TRYWAIT_UNATTACHED:
2199 tcpm_set_state(port, SNK_UNATTACHED, 0);
2203 ret = tcpm_src_attach(port);
2204 tcpm_set_state(port, SRC_UNATTACHED,
2205 ret < 0 ? 0 : PD_T_PS_SOURCE_ON);
2208 typec_set_pwr_opmode(port->typec_port, TYPEC_PWR_MODE_USB);
2209 port->pwr_opmode = TYPEC_PWR_MODE_USB;
2210 port->caps_count = 0;
2211 port->message_id = 0;
2212 port->rx_msgid = -1;
2213 port->explicit_contract = false;
2214 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
2216 case SRC_SEND_CAPABILITIES:
2218 if (port->caps_count > PD_N_CAPS_COUNT) {
2219 tcpm_set_state(port, SRC_READY, 0);
2222 ret = tcpm_pd_send_source_caps(port);
2224 tcpm_set_state(port, SRC_SEND_CAPABILITIES,
2225 PD_T_SEND_SOURCE_CAP);
2228 * Per standard, we should clear the reset counter here.
2229 * However, that can result in state machine hang-ups.
2230 * Reset it only in READY state to improve stability.
2232 /* port->hard_reset_count = 0; */
2233 port->caps_count = 0;
2234 port->pd_capable = true;
2235 tcpm_set_state_cond(port, hard_reset_state(port),
2236 PD_T_SEND_SOURCE_CAP);
2239 case SRC_NEGOTIATE_CAPABILITIES:
2240 ret = tcpm_pd_check_request(port);
2242 tcpm_pd_send_control(port, PD_CTRL_REJECT);
2243 if (!port->explicit_contract) {
2244 tcpm_set_state(port,
2245 SRC_WAIT_NEW_CAPABILITIES, 0);
2247 tcpm_set_state(port, SRC_READY, 0);
2250 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
2251 tcpm_set_state(port, SRC_TRANSITION_SUPPLY,
2252 PD_T_SRC_TRANSITION);
2255 case SRC_TRANSITION_SUPPLY:
2256 /* XXX: regulator_set_voltage(vbus, ...) */
2257 tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
2258 port->explicit_contract = true;
2259 typec_set_pwr_opmode(port->typec_port, TYPEC_PWR_MODE_PD);
2260 port->pwr_opmode = TYPEC_PWR_MODE_PD;
2261 tcpm_set_state_cond(port, SRC_READY, 0);
2265 port->hard_reset_count = 0;
2267 port->try_src_count = 0;
2269 tcpm_swap_complete(port, 0);
2270 tcpm_typec_connect(port);
2271 tcpm_check_send_discover(port);
2274 * Sending ping messages is not necessary if
2275 * - the source operates at vSafe5V
2277 * - The system is not operating in PD mode
2279 * - Both partners are connected using a Type-C connector
2280 * XXX How do we know that ?
2282 if (port->pwr_opmode == TYPEC_PWR_MODE_PD &&
2283 !port->op_vsafe5v) {
2284 tcpm_pd_send_control(port, PD_CTRL_PING);
2285 tcpm_set_state_cond(port, SRC_READY,
2286 PD_T_SOURCE_ACTIVITY);
2289 case SRC_WAIT_NEW_CAPABILITIES:
2290 /* Nothing to do... */
2294 case SNK_UNATTACHED:
2295 tcpm_swap_complete(port, -ENOTCONN);
2296 tcpm_snk_detach(port);
2297 if (tcpm_start_drp_toggling(port)) {
2298 tcpm_set_state(port, DRP_TOGGLING, 0);
2301 tcpm_set_cc(port, TYPEC_CC_RD);
2302 if (port->typec_caps.type == TYPEC_PORT_DRP)
2303 tcpm_set_state(port, SRC_UNATTACHED, PD_T_DRP_SRC);
2305 case SNK_ATTACH_WAIT:
2306 if ((port->cc1 == TYPEC_CC_OPEN &&
2307 port->cc2 != TYPEC_CC_OPEN) ||
2308 (port->cc1 != TYPEC_CC_OPEN &&
2309 port->cc2 == TYPEC_CC_OPEN))
2310 tcpm_set_state(port, SNK_DEBOUNCED,
2312 else if (tcpm_port_is_disconnected(port))
2313 tcpm_set_state(port, SNK_UNATTACHED,
2317 if (tcpm_port_is_disconnected(port))
2318 tcpm_set_state(port, SNK_UNATTACHED,
2320 else if (port->vbus_present)
2321 tcpm_set_state(port,
2322 tcpm_try_src(port) ? SRC_TRY
2326 /* Wait for VBUS, but not forever */
2327 tcpm_set_state(port, SNK_UNATTACHED, PD_T_PS_SOURCE_ON);
2331 port->try_src_count++;
2332 tcpm_set_cc(port, tcpm_rp_cc(port));
2333 tcpm_set_state(port, SNK_TRYWAIT, PD_T_DRP_TRY);
2335 case SRC_TRY_DEBOUNCE:
2336 tcpm_set_state(port, SRC_ATTACHED, PD_T_PD_DEBOUNCE);
2339 tcpm_set_cc(port, TYPEC_CC_RD);
2340 tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, PD_T_CC_DEBOUNCE);
2342 case SNK_TRYWAIT_DEBOUNCE:
2343 if (port->vbus_present) {
2344 tcpm_set_state(port, SNK_ATTACHED, 0);
2347 if (tcpm_port_is_disconnected(port)) {
2348 tcpm_set_state(port, SNK_UNATTACHED,
2352 if (tcpm_port_is_source(port))
2353 tcpm_set_state(port, SRC_ATTACHED, 0);
2354 /* XXX Are we supposed to stay in this state ? */
2356 case SNK_TRYWAIT_VBUS:
2357 tcpm_set_state(port, SNK_ATTACHED, PD_T_CC_DEBOUNCE);
2361 ret = tcpm_snk_attach(port);
2363 tcpm_set_state(port, SNK_UNATTACHED, 0);
2365 tcpm_set_state(port, SNK_STARTUP, 0);
2368 /* XXX: callback into infrastructure */
2369 typec_set_pwr_opmode(port->typec_port, TYPEC_PWR_MODE_USB);
2370 port->pwr_opmode = TYPEC_PWR_MODE_USB;
2371 port->message_id = 0;
2372 port->rx_msgid = -1;
2373 port->explicit_contract = false;
2374 tcpm_set_state(port, SNK_DISCOVERY, 0);
2377 if (port->vbus_present) {
2378 tcpm_set_current_limit(port,
2379 tcpm_get_current_limit(port),
2381 tcpm_set_charge(port, true);
2382 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
2386 * For DRP, timeouts differ. Also, handling is supposed to be
2387 * different and much more complex (dead battery detection;
2388 * see USB power delivery specification, section 8.3.3.6.1.5.1).
2390 tcpm_set_state(port, hard_reset_state(port),
2391 port->typec_caps.type == TYPEC_PORT_DRP ?
2392 PD_T_DB_DETECT : PD_T_NO_RESPONSE);
2394 case SNK_DISCOVERY_DEBOUNCE:
2395 tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE_DONE,
2398 case SNK_DISCOVERY_DEBOUNCE_DONE:
2399 if (!tcpm_port_is_disconnected(port) &&
2400 tcpm_port_is_sink(port) &&
2401 time_is_after_jiffies(port->delayed_runtime)) {
2402 tcpm_set_state(port, SNK_DISCOVERY,
2403 port->delayed_runtime - jiffies);
2406 tcpm_set_state(port, unattached_state(port), 0);
2408 case SNK_WAIT_CAPABILITIES:
2409 ret = port->tcpc->set_pd_rx(port->tcpc, true);
2411 tcpm_set_state(port, SNK_READY, 0);
2415 * If VBUS has never been low, and we time out waiting
2416 * for source cap, try a soft reset first, in case we
2417 * were already in a stable contract before this boot.
2418 * Do this only once.
2420 if (port->vbus_never_low) {
2421 port->vbus_never_low = false;
2422 tcpm_set_state(port, SOFT_RESET_SEND,
2423 PD_T_SINK_WAIT_CAP);
2425 tcpm_set_state(port, hard_reset_state(port),
2426 PD_T_SINK_WAIT_CAP);
2429 case SNK_NEGOTIATE_CAPABILITIES:
2430 port->pd_capable = true;
2431 port->hard_reset_count = 0;
2432 ret = tcpm_pd_send_request(port);
2434 /* Let the Source send capabilities again. */
2435 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
2437 tcpm_set_state_cond(port, hard_reset_state(port),
2438 PD_T_SENDER_RESPONSE);
2441 case SNK_TRANSITION_SINK:
2442 case SNK_TRANSITION_SINK_VBUS:
2443 tcpm_set_state(port, hard_reset_state(port),
2444 PD_T_PS_TRANSITION);
2447 port->try_snk_count = 0;
2448 port->explicit_contract = true;
2449 typec_set_pwr_opmode(port->typec_port, TYPEC_PWR_MODE_PD);
2450 port->pwr_opmode = TYPEC_PWR_MODE_PD;
2452 tcpm_swap_complete(port, 0);
2453 tcpm_typec_connect(port);
2454 tcpm_check_send_discover(port);
2457 /* Accessory states */
2458 case ACC_UNATTACHED:
2459 tcpm_acc_detach(port);
2460 tcpm_set_state(port, SRC_UNATTACHED, 0);
2462 case DEBUG_ACC_ATTACHED:
2463 case AUDIO_ACC_ATTACHED:
2464 ret = tcpm_acc_attach(port);
2466 tcpm_set_state(port, ACC_UNATTACHED, 0);
2468 case AUDIO_ACC_DEBOUNCE:
2469 tcpm_set_state(port, ACC_UNATTACHED, PD_T_CC_DEBOUNCE);
2472 /* Hard_Reset states */
2473 case HARD_RESET_SEND:
2474 tcpm_pd_transmit(port, TCPC_TX_HARD_RESET, NULL);
2475 tcpm_set_state(port, HARD_RESET_START, 0);
2477 case HARD_RESET_START:
2478 port->hard_reset_count++;
2479 port->tcpc->set_pd_rx(port->tcpc, false);
2480 tcpm_unregister_altmodes(port);
2481 port->send_discover = true;
2482 if (port->pwr_role == TYPEC_SOURCE)
2483 tcpm_set_state(port, SRC_HARD_RESET_VBUS_OFF,
2484 PD_T_PS_HARD_RESET);
2486 tcpm_set_state(port, SNK_HARD_RESET_SINK_OFF, 0);
2488 case SRC_HARD_RESET_VBUS_OFF:
2489 tcpm_set_vconn(port, true);
2490 tcpm_set_vbus(port, false);
2491 tcpm_set_roles(port, false, TYPEC_SOURCE, TYPEC_HOST);
2492 tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SRC_RECOVER);
2494 case SRC_HARD_RESET_VBUS_ON:
2495 tcpm_set_vbus(port, true);
2496 port->tcpc->set_pd_rx(port->tcpc, true);
2497 tcpm_set_attached_state(port, true);
2498 tcpm_set_state(port, SRC_UNATTACHED, PD_T_PS_SOURCE_ON);
2500 case SNK_HARD_RESET_SINK_OFF:
2501 tcpm_set_vconn(port, false);
2502 tcpm_set_charge(port, false);
2503 tcpm_set_roles(port, false, TYPEC_SINK, TYPEC_DEVICE);
2505 * VBUS may or may not toggle, depending on the adapter.
2506 * If it doesn't toggle, transition to SNK_HARD_RESET_SINK_ON
2507 * directly after timeout.
2509 tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, PD_T_SAFE_0V);
2511 case SNK_HARD_RESET_WAIT_VBUS:
2512 /* Assume we're disconnected if VBUS doesn't come back. */
2513 tcpm_set_state(port, SNK_UNATTACHED,
2514 PD_T_SRC_RECOVER_MAX + PD_T_SRC_TURN_ON);
2516 case SNK_HARD_RESET_SINK_ON:
2517 /* Note: There is no guarantee that VBUS is on in this state */
2520 * The specification suggests that dual mode ports in sink
2521 * mode should transition to state PE_SRC_Transition_to_default.
2522 * See USB power delivery specification chapter 8.3.3.6.1.3.
2523 * This would mean to to
2524 * - turn off VCONN, reset power supply
2525 * - request hardware reset
2527 * - Transition to state PE_Src_Startup
2528 * SNK only ports shall transition to state Snk_Startup
2529 * (see chapter 8.3.3.3.8).
2530 * Similar, dual-mode ports in source mode should transition
2531 * to PE_SNK_Transition_to_default.
2533 tcpm_set_attached_state(port, true);
2534 tcpm_set_state(port, SNK_STARTUP, 0);
2537 /* Soft_Reset states */
2539 port->message_id = 0;
2540 port->rx_msgid = -1;
2541 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
2542 if (port->pwr_role == TYPEC_SOURCE)
2543 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
2545 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
2547 case SOFT_RESET_SEND:
2548 port->message_id = 0;
2549 port->rx_msgid = -1;
2550 if (tcpm_pd_send_control(port, PD_CTRL_SOFT_RESET))
2551 tcpm_set_state_cond(port, hard_reset_state(port), 0);
2553 tcpm_set_state_cond(port, hard_reset_state(port),
2554 PD_T_SENDER_RESPONSE);
2557 /* DR_Swap states */
2559 tcpm_pd_send_control(port, PD_CTRL_DR_SWAP);
2560 tcpm_set_state_cond(port, DR_SWAP_SEND_TIMEOUT,
2561 PD_T_SENDER_RESPONSE);
2563 case DR_SWAP_ACCEPT:
2564 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
2565 tcpm_set_state_cond(port, DR_SWAP_CHANGE_DR, 0);
2567 case DR_SWAP_SEND_TIMEOUT:
2568 tcpm_swap_complete(port, -ETIMEDOUT);
2569 tcpm_set_state(port, ready_state(port), 0);
2571 case DR_SWAP_CHANGE_DR:
2572 if (port->data_role == TYPEC_HOST) {
2573 tcpm_unregister_altmodes(port);
2574 tcpm_set_roles(port, true, port->pwr_role,
2577 tcpm_set_roles(port, true, port->pwr_role,
2579 port->send_discover = true;
2581 tcpm_set_state(port, ready_state(port), 0);
2584 /* PR_Swap states */
2585 case PR_SWAP_ACCEPT:
2586 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
2587 tcpm_set_state(port, PR_SWAP_START, 0);
2590 tcpm_pd_send_control(port, PD_CTRL_PR_SWAP);
2591 tcpm_set_state_cond(port, PR_SWAP_SEND_TIMEOUT,
2592 PD_T_SENDER_RESPONSE);
2594 case PR_SWAP_SEND_TIMEOUT:
2595 tcpm_swap_complete(port, -ETIMEDOUT);
2596 tcpm_set_state(port, ready_state(port), 0);
2599 if (port->pwr_role == TYPEC_SOURCE)
2600 tcpm_set_state(port, PR_SWAP_SRC_SNK_TRANSITION_OFF,
2601 PD_T_SRC_TRANSITION);
2603 tcpm_set_state(port, PR_SWAP_SNK_SRC_SINK_OFF, 0);
2605 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
2606 tcpm_set_vbus(port, false);
2607 port->explicit_contract = false;
2608 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF,
2609 PD_T_PS_SOURCE_OFF);
2611 case PR_SWAP_SRC_SNK_SOURCE_OFF:
2612 tcpm_set_cc(port, TYPEC_CC_RD);
2614 * USB-PD standard, 6.2.1.4, Port Power Role:
2615 * "During the Power Role Swap Sequence, for the initial Source
2616 * Port, the Port Power Role field shall be set to Sink in the
2617 * PS_RDY Message indicating that the initial Source’s power
2618 * supply is turned off"
2620 tcpm_set_pwr_role(port, TYPEC_SINK);
2621 if (tcpm_pd_send_control(port, PD_CTRL_PS_RDY)) {
2622 tcpm_set_state(port, ERROR_RECOVERY, 0);
2625 tcpm_set_state_cond(port, SNK_UNATTACHED, PD_T_PS_SOURCE_ON);
2627 case PR_SWAP_SRC_SNK_SINK_ON:
2628 tcpm_set_state(port, SNK_STARTUP, 0);
2630 case PR_SWAP_SNK_SRC_SINK_OFF:
2631 tcpm_set_charge(port, false);
2632 tcpm_set_state(port, hard_reset_state(port),
2633 PD_T_PS_SOURCE_OFF);
2635 case PR_SWAP_SNK_SRC_SOURCE_ON:
2636 tcpm_set_cc(port, tcpm_rp_cc(port));
2637 tcpm_set_vbus(port, true);
2639 * USB PD standard, 6.2.1.4:
2640 * "Subsequent Messages initiated by the Policy Engine,
2641 * such as the PS_RDY Message sent to indicate that Vbus
2642 * is ready, will have the Port Power Role field set to
2645 tcpm_set_pwr_role(port, TYPEC_SOURCE);
2646 tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
2647 tcpm_set_state(port, SRC_STARTUP, 0);
2650 case VCONN_SWAP_ACCEPT:
2651 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
2652 tcpm_set_state(port, VCONN_SWAP_START, 0);
2654 case VCONN_SWAP_SEND:
2655 tcpm_pd_send_control(port, PD_CTRL_VCONN_SWAP);
2656 tcpm_set_state(port, VCONN_SWAP_SEND_TIMEOUT,
2657 PD_T_SENDER_RESPONSE);
2659 case VCONN_SWAP_SEND_TIMEOUT:
2660 tcpm_swap_complete(port, -ETIMEDOUT);
2661 tcpm_set_state(port, ready_state(port), 0);
2663 case VCONN_SWAP_START:
2664 if (port->vconn_role == TYPEC_SOURCE)
2665 tcpm_set_state(port, VCONN_SWAP_WAIT_FOR_VCONN, 0);
2667 tcpm_set_state(port, VCONN_SWAP_TURN_ON_VCONN, 0);
2669 case VCONN_SWAP_WAIT_FOR_VCONN:
2670 tcpm_set_state(port, hard_reset_state(port),
2671 PD_T_VCONN_SOURCE_ON);
2673 case VCONN_SWAP_TURN_ON_VCONN:
2674 tcpm_set_vconn(port, true);
2675 tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
2676 tcpm_set_state(port, ready_state(port), 0);
2678 case VCONN_SWAP_TURN_OFF_VCONN:
2679 tcpm_set_vconn(port, false);
2680 tcpm_set_state(port, ready_state(port), 0);
2683 case DR_SWAP_CANCEL:
2684 case PR_SWAP_CANCEL:
2685 case VCONN_SWAP_CANCEL:
2686 tcpm_swap_complete(port, port->swap_status);
2687 if (port->pwr_role == TYPEC_SOURCE)
2688 tcpm_set_state(port, SRC_READY, 0);
2690 tcpm_set_state(port, SNK_READY, 0);
2694 switch (BDO_MODE_MASK(port->bist_request)) {
2695 case BDO_MODE_CARRIER2:
2696 tcpm_pd_transmit(port, TCPC_TX_BIST_MODE_2, NULL);
2701 /* Always switch to unattached state */
2702 tcpm_set_state(port, unattached_state(port), 0);
2704 case ERROR_RECOVERY:
2705 tcpm_swap_complete(port, -EPROTO);
2706 tcpm_reset_port(port);
2708 tcpm_set_cc(port, TYPEC_CC_OPEN);
2709 tcpm_set_state(port, ERROR_RECOVERY_WAIT_OFF,
2710 PD_T_ERROR_RECOVERY);
2712 case ERROR_RECOVERY_WAIT_OFF:
2713 tcpm_set_state(port,
2714 tcpm_default_state(port),
2715 port->vbus_present ? PD_T_PS_SOURCE_OFF : 0);
2718 WARN(1, "Unexpected port state %d\n", port->state);
2723 static void tcpm_state_machine_work(struct work_struct *work)
2725 struct tcpm_port *port = container_of(work, struct tcpm_port,
2726 state_machine.work);
2727 enum tcpm_state prev_state;
2729 mutex_lock(&port->lock);
2730 port->state_machine_running = true;
2732 if (port->queued_message && tcpm_send_queued_message(port))
2735 /* If we were queued due to a delayed state change, update it now */
2736 if (port->delayed_state) {
2737 tcpm_log(port, "state change %s -> %s [delayed %ld ms]",
2738 tcpm_states[port->state],
2739 tcpm_states[port->delayed_state], port->delay_ms);
2740 port->prev_state = port->state;
2741 port->state = port->delayed_state;
2742 port->delayed_state = INVALID_STATE;
2746 * Continue running as long as we have (non-delayed) state changes
2750 prev_state = port->state;
2751 run_state_machine(port);
2752 if (port->queued_message)
2753 tcpm_send_queued_message(port);
2754 } while (port->state != prev_state && !port->delayed_state);
2757 port->state_machine_running = false;
2758 mutex_unlock(&port->lock);
2761 static void _tcpm_cc_change(struct tcpm_port *port, enum typec_cc_status cc1,
2762 enum typec_cc_status cc2)
2764 enum typec_cc_status old_cc1, old_cc2;
2765 enum tcpm_state new_state;
2767 old_cc1 = port->cc1;
2768 old_cc2 = port->cc2;
2772 tcpm_log_force(port,
2773 "CC1: %u -> %u, CC2: %u -> %u [state %s, polarity %d, %s]",
2774 old_cc1, cc1, old_cc2, cc2, tcpm_states[port->state],
2776 tcpm_port_is_disconnected(port) ? "disconnected"
2779 switch (port->state) {
2781 if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) ||
2782 tcpm_port_is_source(port))
2783 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
2784 else if (tcpm_port_is_sink(port))
2785 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
2787 case SRC_UNATTACHED:
2788 case ACC_UNATTACHED:
2789 if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) ||
2790 tcpm_port_is_source(port))
2791 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
2793 case SRC_ATTACH_WAIT:
2794 if (tcpm_port_is_disconnected(port) ||
2795 tcpm_port_is_audio_detached(port))
2796 tcpm_set_state(port, SRC_UNATTACHED, 0);
2797 else if (cc1 != old_cc1 || cc2 != old_cc2)
2798 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
2801 if (tcpm_port_is_disconnected(port))
2802 tcpm_set_state(port, SRC_UNATTACHED, 0);
2805 case SNK_UNATTACHED:
2806 if (tcpm_port_is_sink(port))
2807 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
2809 case SNK_ATTACH_WAIT:
2810 if ((port->cc1 == TYPEC_CC_OPEN &&
2811 port->cc2 != TYPEC_CC_OPEN) ||
2812 (port->cc1 != TYPEC_CC_OPEN &&
2813 port->cc2 == TYPEC_CC_OPEN))
2814 new_state = SNK_DEBOUNCED;
2815 else if (tcpm_port_is_disconnected(port))
2816 new_state = SNK_UNATTACHED;
2819 if (new_state != port->delayed_state)
2820 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
2823 if (tcpm_port_is_disconnected(port))
2824 new_state = SNK_UNATTACHED;
2825 else if (port->vbus_present)
2826 new_state = tcpm_try_src(port) ? SRC_TRY : SNK_ATTACHED;
2828 new_state = SNK_UNATTACHED;
2829 if (new_state != port->delayed_state)
2830 tcpm_set_state(port, SNK_DEBOUNCED, 0);
2833 if (tcpm_port_is_disconnected(port))
2834 tcpm_set_state(port, unattached_state(port), 0);
2835 else if (!port->pd_capable &&
2836 (cc1 != old_cc1 || cc2 != old_cc2))
2837 tcpm_set_current_limit(port,
2838 tcpm_get_current_limit(port),
2842 case AUDIO_ACC_ATTACHED:
2843 if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN)
2844 tcpm_set_state(port, AUDIO_ACC_DEBOUNCE, 0);
2846 case AUDIO_ACC_DEBOUNCE:
2847 if (tcpm_port_is_audio(port))
2848 tcpm_set_state(port, AUDIO_ACC_ATTACHED, 0);
2851 case DEBUG_ACC_ATTACHED:
2852 if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN)
2853 tcpm_set_state(port, ACC_UNATTACHED, 0);
2857 /* Do nothing, waiting for timeout */
2861 /* CC line is unstable, wait for debounce */
2862 if (tcpm_port_is_disconnected(port))
2863 tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE, 0);
2865 case SNK_DISCOVERY_DEBOUNCE:
2869 /* Hand over to state machine if needed */
2870 if (!port->vbus_present && tcpm_port_is_source(port))
2871 new_state = SRC_ATTACHED;
2873 new_state = SRC_TRYWAIT_UNATTACHED;
2875 if (new_state != port->delayed_state)
2876 tcpm_set_state(port, SRC_TRYWAIT, 0);
2879 if (port->vbus_present && tcpm_port_is_sink(port)) {
2880 tcpm_set_state(port, SNK_ATTACHED, 0);
2883 if (!tcpm_port_is_sink(port))
2884 new_state = SRC_TRYWAIT;
2886 new_state = SRC_TRYWAIT_UNATTACHED;
2888 if (new_state != port->delayed_state)
2889 tcpm_set_state(port, SNK_TRY_WAIT, 0);
2893 tcpm_set_state(port, SRC_TRY_DEBOUNCE, 0);
2895 case SRC_TRY_DEBOUNCE:
2896 tcpm_set_state(port, SRC_TRY, 0);
2898 case SNK_TRYWAIT_DEBOUNCE:
2899 if (port->vbus_present) {
2900 tcpm_set_state(port, SNK_ATTACHED, 0);
2903 if (tcpm_port_is_source(port)) {
2904 tcpm_set_state(port, SRC_ATTACHED, 0);
2907 if (tcpm_port_is_disconnected(port) &&
2908 port->delayed_state != SNK_UNATTACHED)
2909 tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, 0);
2912 case PR_SWAP_SNK_SRC_SINK_OFF:
2913 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
2914 case PR_SWAP_SRC_SNK_SOURCE_OFF:
2916 * CC state change is expected here; we just turned off power.
2922 if (tcpm_port_is_disconnected(port))
2923 tcpm_set_state(port, unattached_state(port), 0);
2928 static void _tcpm_pd_vbus_on(struct tcpm_port *port)
2930 enum tcpm_state new_state;
2932 tcpm_log_force(port, "VBUS on");
2933 port->vbus_present = true;
2934 switch (port->state) {
2935 case SNK_TRANSITION_SINK_VBUS:
2936 tcpm_set_state(port, SNK_READY, 0);
2939 tcpm_set_state(port, SNK_DISCOVERY, 0);
2943 tcpm_set_state(port, tcpm_try_src(port) ? SRC_TRY
2947 case SNK_HARD_RESET_WAIT_VBUS:
2948 tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, 0);
2951 tcpm_set_state(port, SRC_STARTUP, 0);
2953 case SRC_HARD_RESET_VBUS_ON:
2954 tcpm_set_state(port, SRC_STARTUP, 0);
2958 /* Do nothing, waiting for timeout */
2961 /* Hand over to state machine if needed */
2962 if (port->delayed_state != SRC_TRYWAIT_UNATTACHED)
2963 tcpm_set_state(port, SRC_TRYWAIT, 0);
2966 if (tcpm_port_is_sink(port)) {
2967 tcpm_set_state(port, SNK_ATTACHED, 0);
2970 if (!tcpm_port_is_sink(port))
2971 new_state = SRC_TRYWAIT;
2973 new_state = SRC_TRYWAIT_UNATTACHED;
2975 if (new_state != port->delayed_state)
2976 tcpm_set_state(port, SNK_TRY_WAIT, 0);
2979 tcpm_set_state(port, SNK_TRYWAIT_VBUS, 0);
2987 static void _tcpm_pd_vbus_off(struct tcpm_port *port)
2989 enum tcpm_state new_state;
2991 tcpm_log_force(port, "VBUS off");
2992 port->vbus_present = false;
2993 port->vbus_never_low = false;
2994 switch (port->state) {
2995 case SNK_HARD_RESET_SINK_OFF:
2996 tcpm_set_state(port, SNK_HARD_RESET_WAIT_VBUS, 0);
2998 case SRC_HARD_RESET_VBUS_OFF:
2999 tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, 0);
3001 case HARD_RESET_SEND:
3005 /* Do nothing, waiting for timeout */
3008 /* Hand over to state machine if needed */
3009 if (tcpm_port_is_source(port))
3010 new_state = SRC_ATTACHED;
3012 new_state = SRC_TRYWAIT_UNATTACHED;
3013 if (new_state != port->delayed_state)
3014 tcpm_set_state(port, SRC_TRYWAIT, 0);
3017 if (!tcpm_port_is_sink(port))
3018 new_state = SRC_TRYWAIT;
3020 new_state = SRC_TRYWAIT_UNATTACHED;
3022 if (new_state != port->delayed_state)
3023 tcpm_set_state(port, SNK_TRY_WAIT, 0);
3025 case SNK_TRYWAIT_VBUS:
3026 tcpm_set_state(port, SNK_TRYWAIT, 0);
3029 case SNK_ATTACH_WAIT:
3030 tcpm_set_state(port, SNK_UNATTACHED, 0);
3033 case SNK_NEGOTIATE_CAPABILITIES:
3036 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
3037 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF, 0);
3040 case PR_SWAP_SNK_SRC_SINK_OFF:
3041 /* Do nothing, expected */
3044 case ERROR_RECOVERY_WAIT_OFF:
3045 tcpm_set_state(port, tcpm_default_state(port), 0);
3049 if (port->pwr_role == TYPEC_SINK &&
3051 tcpm_set_state(port, SNK_UNATTACHED, 0);
3056 static void _tcpm_pd_hard_reset(struct tcpm_port *port)
3058 tcpm_log_force(port, "Received hard reset");
3060 * If we keep receiving hard reset requests, executing the hard reset
3061 * must have failed. Revert to error recovery if that happens.
3063 tcpm_set_state(port,
3064 port->hard_reset_count < PD_N_HARD_RESET_COUNT ?
3065 HARD_RESET_START : ERROR_RECOVERY,
3069 static void tcpm_pd_event_handler(struct work_struct *work)
3071 struct tcpm_port *port = container_of(work, struct tcpm_port,
3075 mutex_lock(&port->lock);
3077 spin_lock(&port->pd_event_lock);
3078 while (port->pd_events) {
3079 events = port->pd_events;
3080 port->pd_events = 0;
3081 spin_unlock(&port->pd_event_lock);
3082 if (events & TCPM_RESET_EVENT)
3083 _tcpm_pd_hard_reset(port);
3084 if (events & TCPM_VBUS_EVENT) {
3087 vbus = port->tcpc->get_vbus(port->tcpc);
3089 _tcpm_pd_vbus_on(port);
3091 _tcpm_pd_vbus_off(port);
3093 if (events & TCPM_CC_EVENT) {
3094 enum typec_cc_status cc1, cc2;
3096 if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0)
3097 _tcpm_cc_change(port, cc1, cc2);
3099 spin_lock(&port->pd_event_lock);
3101 spin_unlock(&port->pd_event_lock);
3102 mutex_unlock(&port->lock);
3105 void tcpm_cc_change(struct tcpm_port *port)
3107 spin_lock(&port->pd_event_lock);
3108 port->pd_events |= TCPM_CC_EVENT;
3109 spin_unlock(&port->pd_event_lock);
3110 queue_work(port->wq, &port->event_work);
3112 EXPORT_SYMBOL_GPL(tcpm_cc_change);
3114 void tcpm_vbus_change(struct tcpm_port *port)
3116 spin_lock(&port->pd_event_lock);
3117 port->pd_events |= TCPM_VBUS_EVENT;
3118 spin_unlock(&port->pd_event_lock);
3119 queue_work(port->wq, &port->event_work);
3121 EXPORT_SYMBOL_GPL(tcpm_vbus_change);
3123 void tcpm_pd_hard_reset(struct tcpm_port *port)
3125 spin_lock(&port->pd_event_lock);
3126 port->pd_events = TCPM_RESET_EVENT;
3127 spin_unlock(&port->pd_event_lock);
3128 queue_work(port->wq, &port->event_work);
3130 EXPORT_SYMBOL_GPL(tcpm_pd_hard_reset);
3132 static int tcpm_dr_set(const struct typec_capability *cap,
3133 enum typec_data_role data)
3135 struct tcpm_port *port = typec_cap_to_tcpm(cap);
3138 mutex_lock(&port->swap_lock);
3139 mutex_lock(&port->lock);
3141 if (port->typec_caps.type != TYPEC_PORT_DRP || !port->pd_capable) {
3145 if (port->state != SRC_READY && port->state != SNK_READY) {
3150 if (port->data_role == data) {
3157 * 6.3.9: If an alternate mode is active, a request to swap
3158 * alternate modes shall trigger a port reset.
3159 * Reject data role swap request in this case.
3162 port->swap_status = 0;
3163 port->swap_pending = true;
3164 reinit_completion(&port->swap_complete);
3165 tcpm_set_state(port, DR_SWAP_SEND, 0);
3166 mutex_unlock(&port->lock);
3168 if (!wait_for_completion_timeout(&port->swap_complete,
3169 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT)))
3172 ret = port->swap_status;
3177 mutex_unlock(&port->lock);
3179 mutex_unlock(&port->swap_lock);
3183 static int tcpm_pr_set(const struct typec_capability *cap,
3184 enum typec_role role)
3186 struct tcpm_port *port = typec_cap_to_tcpm(cap);
3189 mutex_lock(&port->swap_lock);
3190 mutex_lock(&port->lock);
3192 if (port->typec_caps.type != TYPEC_PORT_DRP) {
3196 if (port->state != SRC_READY && port->state != SNK_READY) {
3201 if (role == port->pwr_role) {
3206 if (!port->pd_capable) {
3208 * If the partner is not PD capable, reset the port to
3209 * trigger a role change. This can only work if a preferred
3210 * role is configured, and if it matches the requested role.
3212 if (port->try_role == TYPEC_NO_PREFERRED_ROLE ||
3213 port->try_role == port->pwr_role) {
3217 tcpm_set_state(port, HARD_RESET_SEND, 0);
3222 port->swap_status = 0;
3223 port->swap_pending = true;
3224 reinit_completion(&port->swap_complete);
3225 tcpm_set_state(port, PR_SWAP_SEND, 0);
3226 mutex_unlock(&port->lock);
3228 if (!wait_for_completion_timeout(&port->swap_complete,
3229 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT)))
3232 ret = port->swap_status;
3237 mutex_unlock(&port->lock);
3239 mutex_unlock(&port->swap_lock);
3243 static int tcpm_vconn_set(const struct typec_capability *cap,
3244 enum typec_role role)
3246 struct tcpm_port *port = typec_cap_to_tcpm(cap);
3249 mutex_lock(&port->swap_lock);
3250 mutex_lock(&port->lock);
3252 if (port->state != SRC_READY && port->state != SNK_READY) {
3257 if (role == port->vconn_role) {
3262 port->swap_status = 0;
3263 port->swap_pending = true;
3264 reinit_completion(&port->swap_complete);
3265 tcpm_set_state(port, VCONN_SWAP_SEND, 0);
3266 mutex_unlock(&port->lock);
3268 if (!wait_for_completion_timeout(&port->swap_complete,
3269 msecs_to_jiffies(PD_ROLE_SWAP_TIMEOUT)))
3272 ret = port->swap_status;
3277 mutex_unlock(&port->lock);
3279 mutex_unlock(&port->swap_lock);
3283 static int tcpm_try_role(const struct typec_capability *cap, int role)
3285 struct tcpm_port *port = typec_cap_to_tcpm(cap);
3286 struct tcpc_dev *tcpc = port->tcpc;
3289 mutex_lock(&port->lock);
3291 ret = tcpc->try_role(tcpc, role);
3292 if (!ret && !tcpc->config->try_role_hw)
3293 port->try_role = role;
3294 port->try_src_count = 0;
3295 port->try_snk_count = 0;
3296 mutex_unlock(&port->lock);
3301 static void tcpm_init(struct tcpm_port *port)
3303 enum typec_cc_status cc1, cc2;
3305 port->tcpc->init(port->tcpc);
3307 tcpm_reset_port(port);
3311 * Should possibly wait for VBUS to settle if it was enabled locally
3312 * since tcpm_reset_port() will disable VBUS.
3314 port->vbus_present = port->tcpc->get_vbus(port->tcpc);
3315 if (port->vbus_present)
3316 port->vbus_never_low = true;
3318 tcpm_set_state(port, tcpm_default_state(port), 0);
3320 if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0)
3321 _tcpm_cc_change(port, cc1, cc2);
3324 * Some adapters need a clean slate at startup, and won't recover
3325 * otherwise. So do not try to be fancy and force a clean disconnect.
3327 tcpm_set_state(port, ERROR_RECOVERY, 0);
3330 void tcpm_tcpc_reset(struct tcpm_port *port)
3332 mutex_lock(&port->lock);
3333 /* XXX: Maintain PD connection if possible? */
3335 mutex_unlock(&port->lock);
3337 EXPORT_SYMBOL_GPL(tcpm_tcpc_reset);
3339 static int tcpm_copy_pdos(u32 *dest_pdo, const u32 *src_pdo,
3340 unsigned int nr_pdo)
3344 if (nr_pdo > PDO_MAX_OBJECTS)
3345 nr_pdo = PDO_MAX_OBJECTS;
3347 for (i = 0; i < nr_pdo; i++)
3348 dest_pdo[i] = src_pdo[i];
3353 static int tcpm_copy_vdos(u32 *dest_vdo, const u32 *src_vdo,
3354 unsigned int nr_vdo)
3358 if (nr_vdo > VDO_MAX_OBJECTS)
3359 nr_vdo = VDO_MAX_OBJECTS;
3361 for (i = 0; i < nr_vdo; i++)
3362 dest_vdo[i] = src_vdo[i];
3367 void tcpm_update_source_capabilities(struct tcpm_port *port, const u32 *pdo,
3368 unsigned int nr_pdo)
3370 mutex_lock(&port->lock);
3371 port->nr_src_pdo = tcpm_copy_pdos(port->src_pdo, pdo, nr_pdo);
3372 switch (port->state) {
3373 case SRC_UNATTACHED:
3374 case SRC_ATTACH_WAIT:
3376 tcpm_set_cc(port, tcpm_rp_cc(port));
3378 case SRC_SEND_CAPABILITIES:
3379 case SRC_NEGOTIATE_CAPABILITIES:
3381 case SRC_WAIT_NEW_CAPABILITIES:
3382 tcpm_set_cc(port, tcpm_rp_cc(port));
3383 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
3388 mutex_unlock(&port->lock);
3390 EXPORT_SYMBOL_GPL(tcpm_update_source_capabilities);
3392 void tcpm_update_sink_capabilities(struct tcpm_port *port, const u32 *pdo,
3393 unsigned int nr_pdo,
3394 unsigned int max_snk_mv,
3395 unsigned int max_snk_ma,
3396 unsigned int max_snk_mw,
3397 unsigned int operating_snk_mw)
3399 mutex_lock(&port->lock);
3400 port->nr_snk_pdo = tcpm_copy_pdos(port->snk_pdo, pdo, nr_pdo);
3401 port->max_snk_mv = max_snk_mv;
3402 port->max_snk_ma = max_snk_ma;
3403 port->max_snk_mw = max_snk_mw;
3404 port->operating_snk_mw = operating_snk_mw;
3406 switch (port->state) {
3407 case SNK_NEGOTIATE_CAPABILITIES:
3409 case SNK_TRANSITION_SINK:
3410 case SNK_TRANSITION_SINK_VBUS:
3411 tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
3416 mutex_unlock(&port->lock);
3418 EXPORT_SYMBOL_GPL(tcpm_update_sink_capabilities);
3420 struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
3422 struct tcpm_port *port;
3425 if (!dev || !tcpc || !tcpc->config ||
3426 !tcpc->get_vbus || !tcpc->set_cc || !tcpc->get_cc ||
3427 !tcpc->set_polarity || !tcpc->set_vconn || !tcpc->set_vbus ||
3428 !tcpc->set_pd_rx || !tcpc->set_roles || !tcpc->pd_transmit)
3429 return ERR_PTR(-EINVAL);
3431 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
3433 return ERR_PTR(-ENOMEM);
3438 mutex_init(&port->lock);
3439 mutex_init(&port->swap_lock);
3441 port->wq = create_singlethread_workqueue(dev_name(dev));
3443 return ERR_PTR(-ENOMEM);
3444 INIT_DELAYED_WORK(&port->state_machine, tcpm_state_machine_work);
3445 INIT_DELAYED_WORK(&port->vdm_state_machine, vdm_state_machine_work);
3446 INIT_WORK(&port->event_work, tcpm_pd_event_handler);
3448 spin_lock_init(&port->pd_event_lock);
3450 init_completion(&port->tx_complete);
3451 init_completion(&port->swap_complete);
3453 port->nr_src_pdo = tcpm_copy_pdos(port->src_pdo, tcpc->config->src_pdo,
3454 tcpc->config->nr_src_pdo);
3455 port->nr_snk_pdo = tcpm_copy_pdos(port->snk_pdo, tcpc->config->snk_pdo,
3456 tcpc->config->nr_snk_pdo);
3457 port->nr_snk_vdo = tcpm_copy_vdos(port->snk_vdo, tcpc->config->snk_vdo,
3458 tcpc->config->nr_snk_vdo);
3460 port->max_snk_mv = tcpc->config->max_snk_mv;
3461 port->max_snk_ma = tcpc->config->max_snk_ma;
3462 port->max_snk_mw = tcpc->config->max_snk_mw;
3463 port->operating_snk_mw = tcpc->config->operating_snk_mw;
3464 if (!tcpc->config->try_role_hw)
3465 port->try_role = tcpc->config->default_role;
3467 port->try_role = TYPEC_NO_PREFERRED_ROLE;
3469 port->typec_caps.prefer_role = tcpc->config->default_role;
3470 port->typec_caps.type = tcpc->config->type;
3471 port->typec_caps.revision = 0x0120; /* Type-C spec release 1.2 */
3472 port->typec_caps.pd_revision = 0x0200; /* USB-PD spec release 2.0 */
3473 port->typec_caps.dr_set = tcpm_dr_set;
3474 port->typec_caps.pr_set = tcpm_pr_set;
3475 port->typec_caps.vconn_set = tcpm_vconn_set;
3476 port->typec_caps.try_role = tcpm_try_role;
3478 port->partner_desc.identity = &port->partner_ident;
3482 * - alt_modes, set_alt_mode
3483 * - {debug,audio}_accessory
3486 port->typec_port = typec_register_port(port->dev, &port->typec_caps);
3487 if (!port->typec_port) {
3489 goto out_destroy_wq;
3492 if (tcpc->config->alt_modes) {
3493 const struct typec_altmode_desc *paltmode = tcpc->config->alt_modes;
3496 while (paltmode->svid && i < ARRAY_SIZE(port->port_altmode)) {
3497 port->port_altmode[i] =
3498 typec_port_register_altmode(port->typec_port,
3500 if (!port->port_altmode[i]) {
3502 "%s: failed to register port alternate mode 0x%x",
3503 dev_name(dev), paltmode->svid);
3511 tcpm_debugfs_init(port);
3512 mutex_lock(&port->lock);
3514 mutex_unlock(&port->lock);
3516 tcpm_log(port, "%s: registered", dev_name(dev));
3520 destroy_workqueue(port->wq);
3521 return ERR_PTR(err);
3523 EXPORT_SYMBOL_GPL(tcpm_register_port);
3525 void tcpm_unregister_port(struct tcpm_port *port)
3529 for (i = 0; i < ARRAY_SIZE(port->port_altmode); i++)
3530 typec_unregister_altmode(port->port_altmode[i]);
3531 typec_unregister_port(port->typec_port);
3532 tcpm_debugfs_exit(port);
3533 destroy_workqueue(port->wq);
3535 EXPORT_SYMBOL_GPL(tcpm_unregister_port);
3537 MODULE_AUTHOR("Guenter Roeck <groeck@chromium.org>");
3538 MODULE_DESCRIPTION("USB Type-C Port Manager");
3539 MODULE_LICENSE("GPL");