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->try_role == TYPEC_SINK)
336 return SNK_UNATTACHED;
337 else if (port->try_role == TYPEC_SOURCE)
338 return SRC_UNATTACHED;
339 else if (port->tcpc->config->default_role == TYPEC_SINK)
340 return SNK_UNATTACHED;
341 return SRC_UNATTACHED;
345 struct tcpm_port *typec_cap_to_tcpm(const struct typec_capability *cap)
347 return container_of(cap, struct tcpm_port, typec_caps);
350 static bool tcpm_port_is_disconnected(struct tcpm_port *port)
352 return (!port->attached && port->cc1 == TYPEC_CC_OPEN &&
353 port->cc2 == TYPEC_CC_OPEN) ||
354 (port->attached && ((port->polarity == TYPEC_POLARITY_CC1 &&
355 port->cc1 == TYPEC_CC_OPEN) ||
356 (port->polarity == TYPEC_POLARITY_CC2 &&
357 port->cc2 == TYPEC_CC_OPEN)));
364 #ifdef CONFIG_DEBUG_FS
366 static bool tcpm_log_full(struct tcpm_port *port)
368 return port->logbuffer_tail ==
369 (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
372 static void _tcpm_log(struct tcpm_port *port, const char *fmt, va_list args)
374 char tmpbuffer[LOG_BUFFER_ENTRY_SIZE];
375 u64 ts_nsec = local_clock();
376 unsigned long rem_nsec;
378 if (!port->logbuffer[port->logbuffer_head]) {
379 port->logbuffer[port->logbuffer_head] =
380 kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL);
381 if (!port->logbuffer[port->logbuffer_head])
385 vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args);
387 mutex_lock(&port->logbuffer_lock);
389 if (tcpm_log_full(port)) {
390 port->logbuffer_head = max(port->logbuffer_head - 1, 0);
391 strcpy(tmpbuffer, "overflow");
394 if (port->logbuffer_head < 0 ||
395 port->logbuffer_head >= LOG_BUFFER_ENTRIES) {
397 "Bad log buffer index %d\n", port->logbuffer_head);
401 if (!port->logbuffer[port->logbuffer_head]) {
403 "Log buffer index %d is NULL\n", port->logbuffer_head);
407 rem_nsec = do_div(ts_nsec, 1000000000);
408 scnprintf(port->logbuffer[port->logbuffer_head],
409 LOG_BUFFER_ENTRY_SIZE, "[%5lu.%06lu] %s",
410 (unsigned long)ts_nsec, rem_nsec / 1000,
412 port->logbuffer_head = (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
415 mutex_unlock(&port->logbuffer_lock);
418 static void tcpm_log(struct tcpm_port *port, const char *fmt, ...)
422 /* Do not log while disconnected and unattached */
423 if (tcpm_port_is_disconnected(port) &&
424 (port->state == SRC_UNATTACHED || port->state == SNK_UNATTACHED ||
425 port->state == DRP_TOGGLING))
429 _tcpm_log(port, fmt, args);
433 static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...)
438 _tcpm_log(port, fmt, args);
442 static void tcpm_log_source_caps(struct tcpm_port *port)
446 for (i = 0; i < port->nr_source_caps; i++) {
447 u32 pdo = port->source_caps[i];
448 enum pd_pdo_type type = pdo_type(pdo);
453 scnprintf(msg, sizeof(msg),
454 "%u mV, %u mA [%s%s%s%s%s%s]",
455 pdo_fixed_voltage(pdo),
456 pdo_max_current(pdo),
457 (pdo & PDO_FIXED_DUAL_ROLE) ?
459 (pdo & PDO_FIXED_SUSPEND) ?
461 (pdo & PDO_FIXED_HIGHER_CAP) ?
463 (pdo & PDO_FIXED_USB_COMM) ?
465 (pdo & PDO_FIXED_DATA_SWAP) ?
467 (pdo & PDO_FIXED_EXTPOWER) ?
471 scnprintf(msg, sizeof(msg),
473 pdo_min_voltage(pdo),
474 pdo_max_voltage(pdo),
475 pdo_max_current(pdo));
478 scnprintf(msg, sizeof(msg),
480 pdo_min_voltage(pdo),
481 pdo_max_voltage(pdo),
485 strcpy(msg, "undefined");
488 tcpm_log(port, " PDO %d: type %d, %s",
493 static int tcpm_seq_show(struct seq_file *s, void *v)
495 struct tcpm_port *port = (struct tcpm_port *)s->private;
498 mutex_lock(&port->logbuffer_lock);
499 tail = port->logbuffer_tail;
500 while (tail != port->logbuffer_head) {
501 seq_printf(s, "%s\n", port->logbuffer[tail]);
502 tail = (tail + 1) % LOG_BUFFER_ENTRIES;
504 if (!seq_has_overflowed(s))
505 port->logbuffer_tail = tail;
506 mutex_unlock(&port->logbuffer_lock);
511 static int tcpm_debug_open(struct inode *inode, struct file *file)
513 return single_open(file, tcpm_seq_show, inode->i_private);
516 static const struct file_operations tcpm_debug_operations = {
517 .open = tcpm_debug_open,
520 .release = single_release,
523 static struct dentry *rootdir;
525 static int tcpm_debugfs_init(struct tcpm_port *port)
527 mutex_init(&port->logbuffer_lock);
528 /* /sys/kernel/debug/tcpm/usbcX */
530 rootdir = debugfs_create_dir("tcpm", NULL);
535 port->dentry = debugfs_create_file(dev_name(port->dev),
536 S_IFREG | 0444, rootdir,
537 port, &tcpm_debug_operations);
542 static void tcpm_debugfs_exit(struct tcpm_port *port)
544 debugfs_remove(port->dentry);
549 static void tcpm_log(const struct tcpm_port *port, const char *fmt, ...) { }
550 static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...) { }
551 static void tcpm_log_source_caps(struct tcpm_port *port) { }
552 static int tcpm_debugfs_init(const struct tcpm_port *port) { return 0; }
553 static void tcpm_debugfs_exit(const struct tcpm_port *port) { }
557 static int tcpm_pd_transmit(struct tcpm_port *port,
558 enum tcpm_transmit_type type,
559 const struct pd_message *msg)
561 unsigned long timeout;
565 tcpm_log(port, "PD TX, header: %#x", le16_to_cpu(msg->header));
567 tcpm_log(port, "PD TX, type: %#x", type);
569 reinit_completion(&port->tx_complete);
570 ret = port->tcpc->pd_transmit(port->tcpc, type, msg);
574 mutex_unlock(&port->lock);
575 timeout = wait_for_completion_timeout(&port->tx_complete,
576 msecs_to_jiffies(PD_T_TCPC_TX_TIMEOUT));
577 mutex_lock(&port->lock);
581 switch (port->tx_status) {
582 case TCPC_TX_SUCCESS:
583 port->message_id = (port->message_id + 1) & PD_HEADER_ID_MASK;
585 case TCPC_TX_DISCARDED:
593 void tcpm_pd_transmit_complete(struct tcpm_port *port,
594 enum tcpm_transmit_status status)
596 tcpm_log(port, "PD TX complete, status: %u", status);
597 port->tx_status = status;
598 complete(&port->tx_complete);
600 EXPORT_SYMBOL_GPL(tcpm_pd_transmit_complete);
602 static int tcpm_mux_set(struct tcpm_port *port, enum tcpc_mux_mode mode,
603 enum tcpc_usb_switch config)
607 tcpm_log(port, "Requesting mux mode %d, config %d, polarity %d",
608 mode, config, port->polarity);
611 ret = port->tcpc->mux->set(port->tcpc->mux, mode, config,
617 static int tcpm_set_polarity(struct tcpm_port *port,
618 enum typec_cc_polarity polarity)
622 tcpm_log(port, "polarity %d", polarity);
624 ret = port->tcpc->set_polarity(port->tcpc, polarity);
628 port->polarity = polarity;
633 static int tcpm_set_vconn(struct tcpm_port *port, bool enable)
637 tcpm_log(port, "vconn:=%d", enable);
639 ret = port->tcpc->set_vconn(port->tcpc, enable);
641 port->vconn_role = enable ? TYPEC_SOURCE : TYPEC_SINK;
642 typec_set_vconn_role(port->typec_port, port->vconn_role);
648 static u32 tcpm_get_current_limit(struct tcpm_port *port)
650 enum typec_cc_status cc;
653 cc = port->polarity ? port->cc2 : port->cc1;
655 case TYPEC_CC_RP_1_5:
658 case TYPEC_CC_RP_3_0:
661 case TYPEC_CC_RP_DEF:
670 static int tcpm_set_current_limit(struct tcpm_port *port, u32 max_ma, u32 mv)
672 int ret = -EOPNOTSUPP;
674 tcpm_log(port, "Setting voltage/current limit %u mV %u mA", mv, max_ma);
676 if (port->tcpc->set_current_limit)
677 ret = port->tcpc->set_current_limit(port->tcpc, max_ma, mv);
683 * Determine RP value to set based on maximum current supported
684 * by a port if configured as source.
685 * Returns CC value to report to link partner.
687 static enum typec_cc_status tcpm_rp_cc(struct tcpm_port *port)
689 const u32 *src_pdo = port->src_pdo;
690 int nr_pdo = port->nr_src_pdo;
694 * Search for first entry with matching voltage.
695 * It should report the maximum supported current.
697 for (i = 0; i < nr_pdo; i++) {
698 const u32 pdo = src_pdo[i];
700 if (pdo_type(pdo) == PDO_TYPE_FIXED &&
701 pdo_fixed_voltage(pdo) == 5000) {
702 unsigned int curr = pdo_max_current(pdo);
705 return TYPEC_CC_RP_3_0;
706 else if (curr >= 1500)
707 return TYPEC_CC_RP_1_5;
708 return TYPEC_CC_RP_DEF;
712 return TYPEC_CC_RP_DEF;
715 static int tcpm_set_attached_state(struct tcpm_port *port, bool attached)
717 return port->tcpc->set_roles(port->tcpc, attached, port->pwr_role,
721 static int tcpm_set_roles(struct tcpm_port *port, bool attached,
722 enum typec_role role, enum typec_data_role data)
726 if (data == TYPEC_HOST)
727 ret = tcpm_mux_set(port, TYPEC_MUX_USB,
728 TCPC_USB_SWITCH_CONNECT);
730 ret = tcpm_mux_set(port, TYPEC_MUX_NONE,
731 TCPC_USB_SWITCH_DISCONNECT);
735 ret = port->tcpc->set_roles(port->tcpc, attached, role, data);
739 port->pwr_role = role;
740 port->data_role = data;
741 typec_set_data_role(port->typec_port, data);
742 typec_set_pwr_role(port->typec_port, role);
747 static int tcpm_set_pwr_role(struct tcpm_port *port, enum typec_role role)
751 ret = port->tcpc->set_roles(port->tcpc, true, role,
756 port->pwr_role = role;
757 typec_set_pwr_role(port->typec_port, role);
762 static int tcpm_pd_send_source_caps(struct tcpm_port *port)
764 struct pd_message msg;
767 memset(&msg, 0, sizeof(msg));
768 if (!port->nr_src_pdo) {
769 /* No source capabilities defined, sink only */
770 msg.header = PD_HEADER_LE(PD_CTRL_REJECT,
773 port->message_id, 0);
775 msg.header = PD_HEADER_LE(PD_DATA_SOURCE_CAP,
781 for (i = 0; i < port->nr_src_pdo; i++)
782 msg.payload[i] = cpu_to_le32(port->src_pdo[i]);
784 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
787 static int tcpm_pd_send_sink_caps(struct tcpm_port *port)
789 struct pd_message msg;
792 memset(&msg, 0, sizeof(msg));
793 if (!port->nr_snk_pdo) {
794 /* No sink capabilities defined, source only */
795 msg.header = PD_HEADER_LE(PD_CTRL_REJECT,
798 port->message_id, 0);
800 msg.header = PD_HEADER_LE(PD_DATA_SINK_CAP,
806 for (i = 0; i < port->nr_snk_pdo; i++)
807 msg.payload[i] = cpu_to_le32(port->snk_pdo[i]);
809 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
812 static void tcpm_set_state(struct tcpm_port *port, enum tcpm_state state,
813 unsigned int delay_ms)
816 tcpm_log(port, "pending state change %s -> %s @ %u ms",
817 tcpm_states[port->state], tcpm_states[state],
819 port->delayed_state = state;
820 mod_delayed_work(port->wq, &port->state_machine,
821 msecs_to_jiffies(delay_ms));
822 port->delayed_runtime = jiffies + msecs_to_jiffies(delay_ms);
823 port->delay_ms = delay_ms;
825 tcpm_log(port, "state change %s -> %s",
826 tcpm_states[port->state], tcpm_states[state]);
827 port->delayed_state = INVALID_STATE;
828 port->prev_state = port->state;
831 * Don't re-queue the state machine work item if we're currently
832 * in the state machine and we're immediately changing states.
833 * tcpm_state_machine_work() will continue running the state
836 if (!port->state_machine_running)
837 mod_delayed_work(port->wq, &port->state_machine, 0);
841 static void tcpm_set_state_cond(struct tcpm_port *port, enum tcpm_state state,
842 unsigned int delay_ms)
844 if (port->enter_state == port->state)
845 tcpm_set_state(port, state, delay_ms);
848 "skipped %sstate change %s -> %s [%u ms], context state %s",
849 delay_ms ? "delayed " : "",
850 tcpm_states[port->state], tcpm_states[state],
851 delay_ms, tcpm_states[port->enter_state]);
854 static void tcpm_queue_message(struct tcpm_port *port,
855 enum pd_msg_request message)
857 port->queued_message = message;
858 mod_delayed_work(port->wq, &port->state_machine, 0);
862 * VDM/VDO handling functions
864 static void tcpm_queue_vdm(struct tcpm_port *port, const u32 header,
865 const u32 *data, int cnt)
867 port->vdo_count = cnt + 1;
868 port->vdo_data[0] = header;
869 memcpy(&port->vdo_data[1], data, sizeof(u32) * cnt);
870 /* Set ready, vdm state machine will actually send */
871 port->vdm_retries = 0;
872 port->vdm_state = VDM_STATE_READY;
875 static void svdm_consume_identity(struct tcpm_port *port, const __le32 *payload,
878 u32 vdo = le32_to_cpu(payload[VDO_INDEX_IDH]);
879 u32 product = le32_to_cpu(payload[VDO_INDEX_PRODUCT]);
881 memset(&port->mode_data, 0, sizeof(port->mode_data));
883 #if 0 /* Not really a match */
884 switch (PD_IDH_PTYPE(vdo)) {
885 case IDH_PTYPE_UNDEF:
886 port->partner.type = TYPEC_PARTNER_NONE; /* no longer exists */
890 case IDH_PTYPE_PERIPH:
892 case IDH_PTYPE_PCABLE:
894 case IDH_PTYPE_ACABLE:
897 port->partner.type = TYPEC_PARTNER_ALTMODE;
904 port->partner_ident.id_header = vdo;
905 port->partner_ident.cert_stat = le32_to_cpu(payload[VDO_INDEX_CSTAT]);
906 port->partner_ident.product = product;
908 typec_partner_set_identity(port->partner);
910 tcpm_log(port, "Identity: %04x:%04x.%04x",
912 PD_PRODUCT_PID(product), product & 0xffff);
915 static bool svdm_consume_svids(struct tcpm_port *port, const __le32 *payload,
918 struct pd_mode_data *pmdata = &port->mode_data;
921 for (i = 1; i < cnt; i++) {
922 u32 p = le32_to_cpu(payload[i]);
925 svid = (p >> 16) & 0xffff;
929 if (pmdata->nsvids >= SVID_DISCOVERY_MAX)
932 pmdata->svids[pmdata->nsvids++] = svid;
933 tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid);
939 if (pmdata->nsvids >= SVID_DISCOVERY_MAX)
942 pmdata->svids[pmdata->nsvids++] = svid;
943 tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid);
947 tcpm_log(port, "SVID_DISCOVERY_MAX(%d) too low!", SVID_DISCOVERY_MAX);
951 static void svdm_consume_modes(struct tcpm_port *port, const __le32 *payload,
954 struct pd_mode_data *pmdata = &port->mode_data;
955 struct typec_altmode_desc *paltmode;
956 struct typec_mode_desc *pmode;
959 if (pmdata->altmodes >= ARRAY_SIZE(port->partner_altmode)) {
960 /* Already logged in svdm_consume_svids() */
964 paltmode = &pmdata->altmode_desc[pmdata->altmodes];
965 memset(paltmode, 0, sizeof(*paltmode));
967 paltmode->svid = pmdata->svids[pmdata->svid_index];
969 tcpm_log(port, " Alternate mode %d: SVID 0x%04x",
970 pmdata->altmodes, paltmode->svid);
972 for (i = 1; i < cnt && paltmode->n_modes < ALTMODE_MAX_MODES; i++) {
973 pmode = &paltmode->modes[paltmode->n_modes];
974 memset(pmode, 0, sizeof(*pmode));
975 pmode->vdo = le32_to_cpu(payload[i]);
976 pmode->index = i - 1;
978 tcpm_log(port, " VDO %d: 0x%08x",
979 pmode->index, pmode->vdo);
981 port->partner_altmode[pmdata->altmodes] =
982 typec_partner_register_altmode(port->partner, paltmode);
983 if (port->partner_altmode[pmdata->altmodes] == NULL) {
985 "Failed to register alternate modes for SVID 0x%04x",
992 #define supports_modal(port) PD_IDH_MODAL_SUPP((port)->partner_ident.id_header)
994 static int tcpm_pd_svdm(struct tcpm_port *port, const __le32 *payload, int cnt,
997 u32 p0 = le32_to_cpu(payload[0]);
998 int cmd_type = PD_VDO_CMDT(p0);
999 int cmd = PD_VDO_CMD(p0);
1000 struct pd_mode_data *modep;
1005 tcpm_log(port, "Rx VDM cmd 0x%x type %d cmd %d len %d",
1006 p0, cmd_type, cmd, cnt);
1008 modep = &port->mode_data;
1013 case CMD_DISCOVER_IDENT:
1014 /* 6.4.4.3.1: Only respond as UFP (device) */
1015 if (port->data_role == TYPEC_DEVICE &&
1017 for (i = 0; i < port->nr_snk_vdo; i++)
1019 = cpu_to_le32(port->snk_vdo[i]);
1020 rlen = port->nr_snk_vdo + 1;
1023 case CMD_DISCOVER_SVID:
1025 case CMD_DISCOVER_MODES:
1027 case CMD_ENTER_MODE:
1037 response[0] = p0 | VDO_CMDT(CMDT_RSP_ACK);
1038 } else if (rlen == 0) {
1039 response[0] = p0 | VDO_CMDT(CMDT_RSP_NAK);
1042 response[0] = p0 | VDO_CMDT(CMDT_RSP_BUSY);
1047 /* silently drop message if we are not connected */
1052 case CMD_DISCOVER_IDENT:
1054 svdm_consume_identity(port, payload, cnt);
1055 response[0] = VDO(USB_SID_PD, 1, CMD_DISCOVER_SVID);
1058 case CMD_DISCOVER_SVID:
1060 if (svdm_consume_svids(port, payload, cnt)) {
1061 response[0] = VDO(USB_SID_PD, 1,
1064 } else if (modep->nsvids && supports_modal(port)) {
1065 response[0] = VDO(modep->svids[0], 1,
1066 CMD_DISCOVER_MODES);
1070 case CMD_DISCOVER_MODES:
1072 svdm_consume_modes(port, payload, cnt);
1073 modep->svid_index++;
1074 if (modep->svid_index < modep->nsvids) {
1075 svid = modep->svids[modep->svid_index];
1076 response[0] = VDO(svid, 1, CMD_DISCOVER_MODES);
1080 response[0] = pd_dfp_enter_mode(port, 0, 0);
1086 case CMD_ENTER_MODE:
1099 static void tcpm_handle_vdm_request(struct tcpm_port *port,
1100 const __le32 *payload, int cnt)
1103 u32 response[8] = { };
1104 u32 p0 = le32_to_cpu(payload[0]);
1106 if (port->vdm_state == VDM_STATE_BUSY) {
1107 /* If UFP responded busy retry after timeout */
1108 if (PD_VDO_CMDT(p0) == CMDT_RSP_BUSY) {
1109 port->vdm_state = VDM_STATE_WAIT_RSP_BUSY;
1110 port->vdo_retry = (p0 & ~VDO_CMDT_MASK) |
1112 mod_delayed_work(port->wq, &port->vdm_state_machine,
1113 msecs_to_jiffies(PD_T_VDM_BUSY));
1116 port->vdm_state = VDM_STATE_DONE;
1119 if (PD_VDO_SVDM(p0))
1120 rlen = tcpm_pd_svdm(port, payload, cnt, response);
1123 rlen = tcpm_pd_custom_vdm(port, cnt, payload, response);
1127 tcpm_queue_vdm(port, response[0], &response[1], rlen - 1);
1128 mod_delayed_work(port->wq, &port->vdm_state_machine, 0);
1132 static void tcpm_send_vdm(struct tcpm_port *port, u32 vid, int cmd,
1133 const u32 *data, int count)
1137 if (WARN_ON(count > VDO_MAX_SIZE - 1))
1138 count = VDO_MAX_SIZE - 1;
1140 /* set VDM header with VID & CMD */
1141 header = VDO(vid, ((vid & USB_SID_PD) == USB_SID_PD) ?
1142 1 : (PD_VDO_CMD(cmd) <= CMD_ATTENTION), cmd);
1143 tcpm_queue_vdm(port, header, data, count);
1145 mod_delayed_work(port->wq, &port->vdm_state_machine, 0);
1148 static unsigned int vdm_ready_timeout(u32 vdm_hdr)
1150 unsigned int timeout;
1151 int cmd = PD_VDO_CMD(vdm_hdr);
1153 /* its not a structured VDM command */
1154 if (!PD_VDO_SVDM(vdm_hdr))
1155 return PD_T_VDM_UNSTRUCTURED;
1157 switch (PD_VDO_CMDT(vdm_hdr)) {
1159 if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE)
1160 timeout = PD_T_VDM_WAIT_MODE_E;
1162 timeout = PD_T_VDM_SNDR_RSP;
1165 if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE)
1166 timeout = PD_T_VDM_E_MODE;
1168 timeout = PD_T_VDM_RCVR_RSP;
1174 static void vdm_run_state_machine(struct tcpm_port *port)
1176 struct pd_message msg;
1179 switch (port->vdm_state) {
1180 case VDM_STATE_READY:
1181 /* Only transmit VDM if attached */
1182 if (!port->attached) {
1183 port->vdm_state = VDM_STATE_ERR_BUSY;
1188 * if there's traffic or we're not in PDO ready state don't send
1191 if (port->state != SRC_READY && port->state != SNK_READY)
1194 /* Prepare and send VDM */
1195 memset(&msg, 0, sizeof(msg));
1196 msg.header = PD_HEADER_LE(PD_DATA_VENDOR_DEF,
1199 port->message_id, port->vdo_count);
1200 for (i = 0; i < port->vdo_count; i++)
1201 msg.payload[i] = cpu_to_le32(port->vdo_data[i]);
1202 res = tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
1204 port->vdm_state = VDM_STATE_ERR_SEND;
1206 unsigned long timeout;
1208 port->vdm_retries = 0;
1209 port->vdm_state = VDM_STATE_BUSY;
1210 timeout = vdm_ready_timeout(port->vdo_data[0]);
1211 mod_delayed_work(port->wq, &port->vdm_state_machine,
1215 case VDM_STATE_WAIT_RSP_BUSY:
1216 port->vdo_data[0] = port->vdo_retry;
1217 port->vdo_count = 1;
1218 port->vdm_state = VDM_STATE_READY;
1220 case VDM_STATE_BUSY:
1221 port->vdm_state = VDM_STATE_ERR_TMOUT;
1223 case VDM_STATE_ERR_SEND:
1225 * A partner which does not support USB PD will not reply,
1226 * so this is not a fatal error. At the same time, some
1227 * devices may not return GoodCRC under some circumstances,
1228 * so we need to retry.
1230 if (port->vdm_retries < 3) {
1231 tcpm_log(port, "VDM Tx error, retry");
1232 port->vdm_retries++;
1233 port->vdm_state = VDM_STATE_READY;
1241 static void vdm_state_machine_work(struct work_struct *work)
1243 struct tcpm_port *port = container_of(work, struct tcpm_port,
1244 vdm_state_machine.work);
1245 enum vdm_states prev_state;
1247 mutex_lock(&port->lock);
1250 * Continue running as long as the port is not busy and there was
1254 prev_state = port->vdm_state;
1255 vdm_run_state_machine(port);
1256 } while (port->vdm_state != prev_state &&
1257 port->vdm_state != VDM_STATE_BUSY);
1259 mutex_unlock(&port->lock);
1263 * PD (data, control) command handling functions
1265 static void tcpm_pd_data_request(struct tcpm_port *port,
1266 const struct pd_message *msg)
1268 enum pd_data_msg_type type = pd_header_type_le(msg->header);
1269 unsigned int cnt = pd_header_cnt_le(msg->header);
1273 case PD_DATA_SOURCE_CAP:
1274 if (port->pwr_role != TYPEC_SINK)
1277 for (i = 0; i < cnt; i++)
1278 port->source_caps[i] = le32_to_cpu(msg->payload[i]);
1280 port->nr_source_caps = cnt;
1282 tcpm_log_source_caps(port);
1285 * This message may be received even if VBUS is not
1286 * present. This is quite unexpected; see USB PD
1287 * specification, sections 8.3.3.6.3.1 and 8.3.3.6.3.2.
1288 * However, at the same time, we must be ready to
1289 * receive this message and respond to it 15ms after
1290 * receiving PS_RDY during power swap operations, no matter
1291 * if VBUS is available or not (USB PD specification,
1293 * So we need to accept the message either way,
1294 * but be prepared to keep waiting for VBUS after it was
1297 tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
1299 case PD_DATA_REQUEST:
1300 if (port->pwr_role != TYPEC_SOURCE ||
1302 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1305 port->sink_request = le32_to_cpu(msg->payload[0]);
1306 tcpm_set_state(port, SRC_NEGOTIATE_CAPABILITIES, 0);
1308 case PD_DATA_SINK_CAP:
1309 /* We don't do anything with this at the moment... */
1310 for (i = 0; i < cnt; i++)
1311 port->sink_caps[i] = le32_to_cpu(msg->payload[i]);
1312 port->nr_sink_caps = cnt;
1314 case PD_DATA_VENDOR_DEF:
1315 tcpm_handle_vdm_request(port, msg->payload, cnt);
1318 if (port->state == SRC_READY || port->state == SNK_READY) {
1319 port->bist_request = le32_to_cpu(msg->payload[0]);
1320 tcpm_set_state(port, BIST_RX, 0);
1324 tcpm_log(port, "Unhandled data message type %#x", type);
1329 static void tcpm_pd_ctrl_request(struct tcpm_port *port,
1330 const struct pd_message *msg)
1332 enum pd_ctrl_msg_type type = pd_header_type_le(msg->header);
1333 enum tcpm_state next_state;
1336 case PD_CTRL_GOOD_CRC:
1339 case PD_CTRL_GET_SOURCE_CAP:
1340 switch (port->state) {
1343 tcpm_queue_message(port, PD_MSG_DATA_SOURCE_CAP);
1346 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1350 case PD_CTRL_GET_SINK_CAP:
1351 switch (port->state) {
1354 tcpm_queue_message(port, PD_MSG_DATA_SINK_CAP);
1357 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1361 case PD_CTRL_GOTO_MIN:
1363 case PD_CTRL_PS_RDY:
1364 switch (port->state) {
1365 case SNK_TRANSITION_SINK:
1366 if (port->vbus_present) {
1367 tcpm_set_current_limit(port,
1368 port->current_limit,
1369 port->supply_voltage);
1370 tcpm_set_state(port, SNK_READY, 0);
1373 * Seen after power swap. Keep waiting for VBUS
1374 * in a transitional state.
1376 tcpm_set_state(port,
1377 SNK_TRANSITION_SINK_VBUS, 0);
1380 case PR_SWAP_SRC_SNK_SOURCE_OFF:
1381 tcpm_set_state(port, PR_SWAP_SRC_SNK_SINK_ON, 0);
1383 case PR_SWAP_SNK_SRC_SINK_OFF:
1384 tcpm_set_state(port, PR_SWAP_SNK_SRC_SOURCE_ON, 0);
1386 case VCONN_SWAP_WAIT_FOR_VCONN:
1387 tcpm_set_state(port, VCONN_SWAP_TURN_OFF_VCONN, 0);
1393 case PD_CTRL_REJECT:
1395 switch (port->state) {
1396 case SNK_NEGOTIATE_CAPABILITIES:
1397 /* USB PD specification, Figure 8-43 */
1398 if (port->explicit_contract)
1399 next_state = SNK_READY;
1401 next_state = SNK_WAIT_CAPABILITIES;
1402 tcpm_set_state(port, next_state, 0);
1405 port->swap_status = (type == PD_CTRL_WAIT ?
1406 -EAGAIN : -EOPNOTSUPP);
1407 tcpm_set_state(port, DR_SWAP_CANCEL, 0);
1410 port->swap_status = (type == PD_CTRL_WAIT ?
1411 -EAGAIN : -EOPNOTSUPP);
1412 tcpm_set_state(port, PR_SWAP_CANCEL, 0);
1414 case VCONN_SWAP_SEND:
1415 port->swap_status = (type == PD_CTRL_WAIT ?
1416 -EAGAIN : -EOPNOTSUPP);
1417 tcpm_set_state(port, VCONN_SWAP_CANCEL, 0);
1423 case PD_CTRL_ACCEPT:
1424 switch (port->state) {
1425 case SNK_NEGOTIATE_CAPABILITIES:
1426 tcpm_set_state(port, SNK_TRANSITION_SINK, 0);
1428 case SOFT_RESET_SEND:
1429 port->message_id = 0;
1430 port->rx_msgid = -1;
1431 if (port->pwr_role == TYPEC_SOURCE)
1432 next_state = SRC_SEND_CAPABILITIES;
1434 next_state = SNK_WAIT_CAPABILITIES;
1435 tcpm_set_state(port, next_state, 0);
1438 tcpm_set_state(port, DR_SWAP_CHANGE_DR, 0);
1441 tcpm_set_state(port, PR_SWAP_START, 0);
1443 case VCONN_SWAP_SEND:
1444 tcpm_set_state(port, VCONN_SWAP_START, 0);
1450 case PD_CTRL_SOFT_RESET:
1451 tcpm_set_state(port, SOFT_RESET, 0);
1453 case PD_CTRL_DR_SWAP:
1454 if (port->typec_caps.type != TYPEC_PORT_DRP) {
1455 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1460 * 6.3.9: If an alternate mode is active, a request to swap
1461 * alternate modes shall trigger a port reset.
1463 switch (port->state) {
1466 tcpm_set_state(port, DR_SWAP_ACCEPT, 0);
1469 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
1473 case PD_CTRL_PR_SWAP:
1474 if (port->typec_caps.type != TYPEC_PORT_DRP) {
1475 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1478 switch (port->state) {
1481 tcpm_set_state(port, PR_SWAP_ACCEPT, 0);
1484 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
1488 case PD_CTRL_VCONN_SWAP:
1489 switch (port->state) {
1492 tcpm_set_state(port, VCONN_SWAP_ACCEPT, 0);
1495 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
1500 tcpm_log(port, "Unhandled ctrl message type %#x", type);
1505 static void tcpm_pd_rx_handler(struct work_struct *work)
1507 struct pd_rx_event *event = container_of(work,
1508 struct pd_rx_event, work);
1509 const struct pd_message *msg = &event->msg;
1510 unsigned int cnt = pd_header_cnt_le(msg->header);
1511 struct tcpm_port *port = event->port;
1513 mutex_lock(&port->lock);
1515 tcpm_log(port, "PD RX, header: %#x [%d]", le16_to_cpu(msg->header),
1518 if (port->attached) {
1519 enum pd_ctrl_msg_type type = pd_header_type_le(msg->header);
1520 unsigned int msgid = pd_header_msgid_le(msg->header);
1523 * USB PD standard, 6.6.1.2:
1524 * "... if MessageID value in a received Message is the
1525 * same as the stored value, the receiver shall return a
1526 * GoodCRC Message with that MessageID value and drop
1527 * the Message (this is a retry of an already received
1528 * Message). Note: this shall not apply to the Soft_Reset
1529 * Message which always has a MessageID value of zero."
1531 if (msgid == port->rx_msgid && type != PD_CTRL_SOFT_RESET)
1533 port->rx_msgid = msgid;
1536 * If both ends believe to be DFP/host, we have a data role
1539 if (!!(le16_to_cpu(msg->header) & PD_HEADER_DATA_ROLE) ==
1540 (port->data_role == TYPEC_HOST)) {
1542 "Data role mismatch, initiating error recovery");
1543 tcpm_set_state(port, ERROR_RECOVERY, 0);
1546 tcpm_pd_data_request(port, msg);
1548 tcpm_pd_ctrl_request(port, msg);
1553 mutex_unlock(&port->lock);
1557 void tcpm_pd_receive(struct tcpm_port *port, const struct pd_message *msg)
1559 struct pd_rx_event *event;
1561 event = kzalloc(sizeof(*event), GFP_ATOMIC);
1565 INIT_WORK(&event->work, tcpm_pd_rx_handler);
1567 memcpy(&event->msg, msg, sizeof(*msg));
1568 queue_work(port->wq, &event->work);
1570 EXPORT_SYMBOL_GPL(tcpm_pd_receive);
1572 static int tcpm_pd_send_control(struct tcpm_port *port,
1573 enum pd_ctrl_msg_type type)
1575 struct pd_message msg;
1577 memset(&msg, 0, sizeof(msg));
1578 msg.header = PD_HEADER_LE(type, port->pwr_role,
1580 port->message_id, 0);
1582 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
1586 * Send queued message without affecting state.
1587 * Return true if state machine should go back to sleep,
1590 static bool tcpm_send_queued_message(struct tcpm_port *port)
1592 enum pd_msg_request queued_message;
1595 queued_message = port->queued_message;
1596 port->queued_message = PD_MSG_NONE;
1598 switch (queued_message) {
1599 case PD_MSG_CTRL_WAIT:
1600 tcpm_pd_send_control(port, PD_CTRL_WAIT);
1602 case PD_MSG_CTRL_REJECT:
1603 tcpm_pd_send_control(port, PD_CTRL_REJECT);
1605 case PD_MSG_DATA_SINK_CAP:
1606 tcpm_pd_send_sink_caps(port);
1608 case PD_MSG_DATA_SOURCE_CAP:
1609 tcpm_pd_send_source_caps(port);
1614 } while (port->queued_message != PD_MSG_NONE);
1616 if (port->delayed_state != INVALID_STATE) {
1617 if (time_is_after_jiffies(port->delayed_runtime)) {
1618 mod_delayed_work(port->wq, &port->state_machine,
1619 port->delayed_runtime - jiffies);
1622 port->delayed_state = INVALID_STATE;
1627 static int tcpm_pd_check_request(struct tcpm_port *port)
1629 u32 pdo, rdo = port->sink_request;
1630 unsigned int max, op, pdo_max, index;
1631 enum pd_pdo_type type;
1633 index = rdo_index(rdo);
1634 if (!index || index > port->nr_src_pdo)
1637 pdo = port->src_pdo[index - 1];
1638 type = pdo_type(pdo);
1640 case PDO_TYPE_FIXED:
1642 max = rdo_max_current(rdo);
1643 op = rdo_op_current(rdo);
1644 pdo_max = pdo_max_current(pdo);
1648 if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH))
1651 if (type == PDO_TYPE_FIXED)
1653 "Requested %u mV, %u mA for %u / %u mA",
1654 pdo_fixed_voltage(pdo), pdo_max, op, max);
1657 "Requested %u -> %u mV, %u mA for %u / %u mA",
1658 pdo_min_voltage(pdo), pdo_max_voltage(pdo),
1662 max = rdo_max_power(rdo);
1663 op = rdo_op_power(rdo);
1664 pdo_max = pdo_max_power(pdo);
1668 if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH))
1671 "Requested %u -> %u mV, %u mW for %u / %u mW",
1672 pdo_min_voltage(pdo), pdo_max_voltage(pdo),
1679 port->op_vsafe5v = index == 1;
1684 static int tcpm_pd_select_pdo(struct tcpm_port *port)
1686 unsigned int i, max_mw = 0, max_mv = 0;
1690 * Select the source PDO providing the most power while staying within
1691 * the board's voltage limits. Prefer PDO providing exp
1693 for (i = 0; i < port->nr_source_caps; i++) {
1694 u32 pdo = port->source_caps[i];
1695 enum pd_pdo_type type = pdo_type(pdo);
1696 unsigned int mv, ma, mw;
1698 if (type == PDO_TYPE_FIXED)
1699 mv = pdo_fixed_voltage(pdo);
1701 mv = pdo_min_voltage(pdo);
1703 if (type == PDO_TYPE_BATT) {
1704 mw = pdo_max_power(pdo);
1706 ma = min(pdo_max_current(pdo),
1708 mw = ma * mv / 1000;
1711 /* Perfer higher voltages if available */
1712 if ((mw > max_mw || (mw == max_mw && mv > max_mv)) &&
1713 mv <= port->max_snk_mv) {
1723 static int tcpm_pd_build_request(struct tcpm_port *port, u32 *rdo)
1725 unsigned int mv, ma, mw, flags;
1726 unsigned int max_ma, max_mw;
1727 enum pd_pdo_type type;
1731 index = tcpm_pd_select_pdo(port);
1734 pdo = port->source_caps[index];
1735 type = pdo_type(pdo);
1737 if (type == PDO_TYPE_FIXED)
1738 mv = pdo_fixed_voltage(pdo);
1740 mv = pdo_min_voltage(pdo);
1742 /* Select maximum available current within the board's power limit */
1743 if (type == PDO_TYPE_BATT) {
1744 mw = pdo_max_power(pdo);
1745 ma = 1000 * min(mw, port->max_snk_mw) / mv;
1747 ma = min(pdo_max_current(pdo),
1748 1000 * port->max_snk_mw / mv);
1750 ma = min(ma, port->max_snk_ma);
1752 flags = RDO_USB_COMM | RDO_NO_SUSPEND;
1754 /* Set mismatch bit if offered power is less than operating power */
1755 mw = ma * mv / 1000;
1758 if (mw < port->operating_snk_mw) {
1759 flags |= RDO_CAP_MISMATCH;
1760 max_mw = port->operating_snk_mw;
1761 max_ma = max_mw * 1000 / mv;
1764 tcpm_log(port, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d",
1765 port->cc_req, port->cc1, port->cc2, port->vbus_source,
1766 port->vconn_role == TYPEC_SOURCE ? "source" : "sink",
1769 if (type == PDO_TYPE_BATT) {
1770 *rdo = RDO_BATT(index + 1, mw, max_mw, flags);
1772 tcpm_log(port, "Requesting PDO %d: %u mV, %u mW%s",
1774 flags & RDO_CAP_MISMATCH ? " [mismatch]" : "");
1776 *rdo = RDO_FIXED(index + 1, ma, max_ma, flags);
1778 tcpm_log(port, "Requesting PDO %d: %u mV, %u mA%s",
1780 flags & RDO_CAP_MISMATCH ? " [mismatch]" : "");
1783 port->current_limit = ma;
1784 port->supply_voltage = mv;
1789 static int tcpm_pd_send_request(struct tcpm_port *port)
1791 struct pd_message msg;
1795 ret = tcpm_pd_build_request(port, &rdo);
1799 memset(&msg, 0, sizeof(msg));
1800 msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
1803 port->message_id, 1);
1804 msg.payload[0] = cpu_to_le32(rdo);
1806 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
1809 static int tcpm_set_vbus(struct tcpm_port *port, bool enable)
1813 if (enable && port->vbus_charge)
1816 tcpm_log(port, "vbus:=%d charge=%d", enable, port->vbus_charge);
1818 ret = port->tcpc->set_vbus(port->tcpc, enable, port->vbus_charge);
1822 port->vbus_source = enable;
1826 static int tcpm_set_charge(struct tcpm_port *port, bool charge)
1830 if (charge && port->vbus_source)
1833 if (charge != port->vbus_charge) {
1834 tcpm_log(port, "vbus=%d charge:=%d", port->vbus_source, charge);
1835 ret = port->tcpc->set_vbus(port->tcpc, port->vbus_source,
1840 port->vbus_charge = charge;
1844 static bool tcpm_start_drp_toggling(struct tcpm_port *port)
1848 if (port->tcpc->start_drp_toggling &&
1849 port->typec_caps.type == TYPEC_PORT_DRP) {
1850 tcpm_log_force(port, "Start DRP toggling");
1851 ret = port->tcpc->start_drp_toggling(port->tcpc,
1860 static void tcpm_set_cc(struct tcpm_port *port, enum typec_cc_status cc)
1862 tcpm_log(port, "cc:=%d", cc);
1864 port->tcpc->set_cc(port->tcpc, cc);
1867 static int tcpm_init_vbus(struct tcpm_port *port)
1871 ret = port->tcpc->set_vbus(port->tcpc, false, false);
1872 port->vbus_source = false;
1873 port->vbus_charge = false;
1877 static int tcpm_init_vconn(struct tcpm_port *port)
1881 ret = port->tcpc->set_vconn(port->tcpc, false);
1882 port->vconn_role = TYPEC_SINK;
1886 static void tcpm_typec_connect(struct tcpm_port *port)
1888 if (!port->connected) {
1889 /* Make sure we don't report stale identity information */
1890 memset(&port->partner_ident, 0, sizeof(port->partner_ident));
1891 port->partner_desc.usb_pd = port->pd_capable;
1892 if (tcpm_port_is_debug(port))
1893 port->partner_desc.accessory = TYPEC_ACCESSORY_DEBUG;
1894 else if (tcpm_port_is_audio(port))
1895 port->partner_desc.accessory = TYPEC_ACCESSORY_AUDIO;
1897 port->partner_desc.accessory = TYPEC_ACCESSORY_NONE;
1898 port->partner = typec_register_partner(port->typec_port,
1899 &port->partner_desc);
1900 port->connected = true;
1904 static int tcpm_src_attach(struct tcpm_port *port)
1906 enum typec_cc_polarity polarity =
1907 port->cc2 == TYPEC_CC_RD ? TYPEC_POLARITY_CC2
1908 : TYPEC_POLARITY_CC1;
1914 ret = tcpm_set_polarity(port, polarity);
1918 ret = tcpm_set_roles(port, true, TYPEC_SOURCE, TYPEC_HOST);
1922 ret = port->tcpc->set_pd_rx(port->tcpc, true);
1924 goto out_disable_mux;
1927 * USB Type-C specification, version 1.2,
1928 * chapter 4.5.2.2.8.1 (Attached.SRC Requirements)
1929 * Enable VCONN only if the non-RD port is set to RA.
1931 if ((polarity == TYPEC_POLARITY_CC1 && port->cc2 == TYPEC_CC_RA) ||
1932 (polarity == TYPEC_POLARITY_CC2 && port->cc1 == TYPEC_CC_RA)) {
1933 ret = tcpm_set_vconn(port, true);
1935 goto out_disable_pd;
1938 ret = tcpm_set_vbus(port, true);
1940 goto out_disable_vconn;
1942 port->pd_capable = false;
1944 port->partner = NULL;
1946 port->attached = true;
1947 port->send_discover = true;
1952 tcpm_set_vconn(port, false);
1954 port->tcpc->set_pd_rx(port->tcpc, false);
1956 tcpm_mux_set(port, TYPEC_MUX_NONE, TCPC_USB_SWITCH_DISCONNECT);
1960 static void tcpm_typec_disconnect(struct tcpm_port *port)
1962 if (port->connected) {
1963 typec_unregister_partner(port->partner);
1964 port->partner = NULL;
1965 port->connected = false;
1969 static void tcpm_unregister_altmodes(struct tcpm_port *port)
1971 struct pd_mode_data *modep = &port->mode_data;
1974 for (i = 0; i < modep->altmodes; i++) {
1975 typec_unregister_altmode(port->partner_altmode[i]);
1976 port->partner_altmode[i] = NULL;
1979 memset(modep, 0, sizeof(*modep));
1982 static void tcpm_reset_port(struct tcpm_port *port)
1984 tcpm_unregister_altmodes(port);
1985 tcpm_typec_disconnect(port);
1986 port->attached = false;
1987 port->pd_capable = false;
1990 * First Rx ID should be 0; set this to a sentinel of -1 so that
1991 * we can check tcpm_pd_rx_handler() if we had seen it before.
1993 port->rx_msgid = -1;
1995 port->tcpc->set_pd_rx(port->tcpc, false);
1996 tcpm_init_vbus(port); /* also disables charging */
1997 tcpm_init_vconn(port);
1998 tcpm_set_current_limit(port, 0, 0);
1999 tcpm_set_polarity(port, TYPEC_POLARITY_CC1);
2000 tcpm_set_attached_state(port, false);
2001 port->try_src_count = 0;
2002 port->try_snk_count = 0;
2005 static void tcpm_detach(struct tcpm_port *port)
2007 if (!port->attached)
2010 if (tcpm_port_is_disconnected(port))
2011 port->hard_reset_count = 0;
2013 tcpm_reset_port(port);
2016 static void tcpm_src_detach(struct tcpm_port *port)
2021 static int tcpm_snk_attach(struct tcpm_port *port)
2028 ret = tcpm_set_polarity(port, port->cc2 != TYPEC_CC_OPEN ?
2029 TYPEC_POLARITY_CC2 : TYPEC_POLARITY_CC1);
2033 ret = tcpm_set_roles(port, true, TYPEC_SINK, TYPEC_DEVICE);
2037 port->pd_capable = false;
2039 port->partner = NULL;
2041 port->attached = true;
2042 port->send_discover = true;
2047 static void tcpm_snk_detach(struct tcpm_port *port)
2051 /* XXX: (Dis)connect SuperSpeed mux? */
2054 static int tcpm_acc_attach(struct tcpm_port *port)
2061 ret = tcpm_set_roles(port, true, TYPEC_SOURCE, TYPEC_HOST);
2065 port->partner = NULL;
2067 tcpm_typec_connect(port);
2069 port->attached = true;
2074 static void tcpm_acc_detach(struct tcpm_port *port)
2079 static inline enum tcpm_state hard_reset_state(struct tcpm_port *port)
2081 if (port->hard_reset_count < PD_N_HARD_RESET_COUNT)
2082 return HARD_RESET_SEND;
2083 if (port->pd_capable)
2084 return ERROR_RECOVERY;
2085 if (port->pwr_role == TYPEC_SOURCE)
2086 return SRC_UNATTACHED;
2087 if (port->state == SNK_WAIT_CAPABILITIES)
2089 return SNK_UNATTACHED;
2092 static inline enum tcpm_state ready_state(struct tcpm_port *port)
2094 if (port->pwr_role == TYPEC_SOURCE)
2100 static inline enum tcpm_state unattached_state(struct tcpm_port *port)
2102 if (port->pwr_role == TYPEC_SOURCE)
2103 return SRC_UNATTACHED;
2105 return SNK_UNATTACHED;
2108 static void tcpm_check_send_discover(struct tcpm_port *port)
2110 if (port->data_role == TYPEC_HOST && port->send_discover &&
2112 tcpm_send_vdm(port, USB_SID_PD, CMD_DISCOVER_IDENT, NULL, 0);
2113 port->send_discover = false;
2117 static void tcpm_swap_complete(struct tcpm_port *port, int result)
2119 if (port->swap_pending) {
2120 port->swap_status = result;
2121 port->swap_pending = false;
2122 complete(&port->swap_complete);
2126 static void run_state_machine(struct tcpm_port *port)
2130 port->enter_state = port->state;
2131 switch (port->state) {
2135 case SRC_UNATTACHED:
2136 tcpm_swap_complete(port, -ENOTCONN);
2137 tcpm_src_detach(port);
2138 if (tcpm_start_drp_toggling(port)) {
2139 tcpm_set_state(port, DRP_TOGGLING, 0);
2142 tcpm_set_cc(port, tcpm_rp_cc(port));
2143 if (port->typec_caps.type == TYPEC_PORT_DRP)
2144 tcpm_set_state(port, SNK_UNATTACHED, PD_T_DRP_SNK);
2146 case SRC_ATTACH_WAIT:
2147 if (tcpm_port_is_debug(port))
2148 tcpm_set_state(port, DEBUG_ACC_ATTACHED,
2150 else if (tcpm_port_is_audio(port))
2151 tcpm_set_state(port, AUDIO_ACC_ATTACHED,
2153 else if (tcpm_port_is_source(port))
2154 tcpm_set_state(port,
2155 tcpm_try_snk(port) ? SNK_TRY
2161 port->try_snk_count++;
2164 * - Do not drive vconn or vbus
2165 * - Terminate CC pins (both) to Rd
2167 * - Wait for tDRPTry (PD_T_DRP_TRY).
2168 * Until then, ignore any state changes.
2170 tcpm_set_cc(port, TYPEC_CC_RD);
2171 tcpm_set_state(port, SNK_TRY_WAIT, PD_T_DRP_TRY);
2174 if (port->vbus_present && tcpm_port_is_sink(port)) {
2175 tcpm_set_state(port, SNK_ATTACHED, 0);
2178 if (!tcpm_port_is_sink(port)) {
2179 tcpm_set_state(port, SRC_TRYWAIT,
2183 /* No vbus, cc state is sink or open */
2184 tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED, PD_T_DRP_TRYWAIT);
2187 tcpm_set_cc(port, tcpm_rp_cc(port));
2188 if (!port->vbus_present && tcpm_port_is_source(port))
2189 tcpm_set_state(port, SRC_ATTACHED, PD_T_CC_DEBOUNCE);
2191 tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED,
2194 case SRC_TRYWAIT_UNATTACHED:
2195 tcpm_set_state(port, SNK_UNATTACHED, 0);
2199 ret = tcpm_src_attach(port);
2200 tcpm_set_state(port, SRC_UNATTACHED,
2201 ret < 0 ? 0 : PD_T_PS_SOURCE_ON);
2204 typec_set_pwr_opmode(port->typec_port, TYPEC_PWR_MODE_USB);
2205 port->pwr_opmode = TYPEC_PWR_MODE_USB;
2206 port->caps_count = 0;
2207 port->message_id = 0;
2208 port->rx_msgid = -1;
2209 port->explicit_contract = false;
2210 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
2212 case SRC_SEND_CAPABILITIES:
2214 if (port->caps_count > PD_N_CAPS_COUNT) {
2215 tcpm_set_state(port, SRC_READY, 0);
2218 ret = tcpm_pd_send_source_caps(port);
2220 tcpm_set_state(port, SRC_SEND_CAPABILITIES,
2221 PD_T_SEND_SOURCE_CAP);
2224 * Per standard, we should clear the reset counter here.
2225 * However, that can result in state machine hang-ups.
2226 * Reset it only in READY state to improve stability.
2228 /* port->hard_reset_count = 0; */
2229 port->caps_count = 0;
2230 port->pd_capable = true;
2231 tcpm_set_state_cond(port, hard_reset_state(port),
2232 PD_T_SEND_SOURCE_CAP);
2235 case SRC_NEGOTIATE_CAPABILITIES:
2236 ret = tcpm_pd_check_request(port);
2238 tcpm_pd_send_control(port, PD_CTRL_REJECT);
2239 if (!port->explicit_contract) {
2240 tcpm_set_state(port,
2241 SRC_WAIT_NEW_CAPABILITIES, 0);
2243 tcpm_set_state(port, SRC_READY, 0);
2246 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
2247 tcpm_set_state(port, SRC_TRANSITION_SUPPLY,
2248 PD_T_SRC_TRANSITION);
2251 case SRC_TRANSITION_SUPPLY:
2252 /* XXX: regulator_set_voltage(vbus, ...) */
2253 tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
2254 port->explicit_contract = true;
2255 typec_set_pwr_opmode(port->typec_port, TYPEC_PWR_MODE_PD);
2256 port->pwr_opmode = TYPEC_PWR_MODE_PD;
2257 tcpm_set_state_cond(port, SRC_READY, 0);
2261 port->hard_reset_count = 0;
2263 port->try_src_count = 0;
2265 tcpm_typec_connect(port);
2267 tcpm_check_send_discover(port);
2270 * Sending ping messages is not necessary if
2271 * - the source operates at vSafe5V
2273 * - The system is not operating in PD mode
2275 * - Both partners are connected using a Type-C connector
2276 * XXX How do we know that ?
2278 if (port->pwr_opmode == TYPEC_PWR_MODE_PD &&
2279 !port->op_vsafe5v) {
2280 tcpm_pd_send_control(port, PD_CTRL_PING);
2281 tcpm_set_state_cond(port, SRC_READY,
2282 PD_T_SOURCE_ACTIVITY);
2285 case SRC_WAIT_NEW_CAPABILITIES:
2286 /* Nothing to do... */
2290 case SNK_UNATTACHED:
2291 tcpm_swap_complete(port, -ENOTCONN);
2292 tcpm_snk_detach(port);
2293 if (tcpm_start_drp_toggling(port)) {
2294 tcpm_set_state(port, DRP_TOGGLING, 0);
2297 tcpm_set_cc(port, TYPEC_CC_RD);
2298 if (port->typec_caps.type == TYPEC_PORT_DRP)
2299 tcpm_set_state(port, SRC_UNATTACHED, PD_T_DRP_SRC);
2301 case SNK_ATTACH_WAIT:
2302 if ((port->cc1 == TYPEC_CC_OPEN &&
2303 port->cc2 != TYPEC_CC_OPEN) ||
2304 (port->cc1 != TYPEC_CC_OPEN &&
2305 port->cc2 == TYPEC_CC_OPEN))
2306 tcpm_set_state(port, SNK_DEBOUNCED,
2308 else if (tcpm_port_is_disconnected(port))
2309 tcpm_set_state(port, SNK_UNATTACHED,
2313 if (tcpm_port_is_disconnected(port))
2314 tcpm_set_state(port, SNK_UNATTACHED,
2316 else if (port->vbus_present)
2317 tcpm_set_state(port,
2318 tcpm_try_src(port) ? SRC_TRY
2322 /* Wait for VBUS, but not forever */
2323 tcpm_set_state(port, SNK_UNATTACHED, PD_T_PS_SOURCE_ON);
2327 port->try_src_count++;
2328 tcpm_set_cc(port, tcpm_rp_cc(port));
2329 tcpm_set_state(port, SNK_TRYWAIT, PD_T_DRP_TRY);
2331 case SRC_TRY_DEBOUNCE:
2332 tcpm_set_state(port, SRC_ATTACHED, PD_T_PD_DEBOUNCE);
2335 tcpm_set_cc(port, TYPEC_CC_RD);
2336 tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, PD_T_CC_DEBOUNCE);
2338 case SNK_TRYWAIT_DEBOUNCE:
2339 if (port->vbus_present) {
2340 tcpm_set_state(port, SNK_ATTACHED, 0);
2343 if (tcpm_port_is_disconnected(port)) {
2344 tcpm_set_state(port, SNK_UNATTACHED,
2348 if (tcpm_port_is_source(port))
2349 tcpm_set_state(port, SRC_ATTACHED, 0);
2350 /* XXX Are we supposed to stay in this state ? */
2352 case SNK_TRYWAIT_VBUS:
2353 tcpm_set_state(port, SNK_ATTACHED, PD_T_CC_DEBOUNCE);
2357 ret = tcpm_snk_attach(port);
2359 tcpm_set_state(port, SNK_UNATTACHED, 0);
2361 tcpm_set_state(port, SNK_STARTUP, 0);
2364 /* XXX: callback into infrastructure */
2365 typec_set_pwr_opmode(port->typec_port, TYPEC_PWR_MODE_USB);
2366 port->pwr_opmode = TYPEC_PWR_MODE_USB;
2367 port->message_id = 0;
2368 port->rx_msgid = -1;
2369 port->explicit_contract = false;
2370 tcpm_set_state(port, SNK_DISCOVERY, 0);
2373 if (port->vbus_present) {
2374 tcpm_set_current_limit(port,
2375 tcpm_get_current_limit(port),
2377 tcpm_set_charge(port, true);
2378 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
2382 * For DRP, timeouts differ. Also, handling is supposed to be
2383 * different and much more complex (dead battery detection;
2384 * see USB power delivery specification, section 8.3.3.6.1.5.1).
2386 tcpm_set_state(port, hard_reset_state(port),
2387 port->typec_caps.type == TYPEC_PORT_DRP ?
2388 PD_T_DB_DETECT : PD_T_NO_RESPONSE);
2390 case SNK_DISCOVERY_DEBOUNCE:
2391 tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE_DONE,
2394 case SNK_DISCOVERY_DEBOUNCE_DONE:
2395 if (!tcpm_port_is_disconnected(port) &&
2396 tcpm_port_is_sink(port) &&
2397 time_is_after_jiffies(port->delayed_runtime)) {
2398 tcpm_set_state(port, SNK_DISCOVERY,
2399 port->delayed_runtime - jiffies);
2402 tcpm_set_state(port, unattached_state(port), 0);
2404 case SNK_WAIT_CAPABILITIES:
2405 ret = port->tcpc->set_pd_rx(port->tcpc, true);
2407 tcpm_set_state(port, SNK_READY, 0);
2411 * If VBUS has never been low, and we time out waiting
2412 * for source cap, try a soft reset first, in case we
2413 * were already in a stable contract before this boot.
2414 * Do this only once.
2416 if (port->vbus_never_low) {
2417 port->vbus_never_low = false;
2418 tcpm_set_state(port, SOFT_RESET_SEND,
2419 PD_T_SINK_WAIT_CAP);
2421 tcpm_set_state(port, hard_reset_state(port),
2422 PD_T_SINK_WAIT_CAP);
2425 case SNK_NEGOTIATE_CAPABILITIES:
2426 port->pd_capable = true;
2427 port->hard_reset_count = 0;
2428 ret = tcpm_pd_send_request(port);
2430 /* Let the Source send capabilities again. */
2431 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
2433 tcpm_set_state_cond(port, hard_reset_state(port),
2434 PD_T_SENDER_RESPONSE);
2437 case SNK_TRANSITION_SINK:
2438 case SNK_TRANSITION_SINK_VBUS:
2439 tcpm_set_state(port, hard_reset_state(port),
2440 PD_T_PS_TRANSITION);
2443 port->try_snk_count = 0;
2444 port->explicit_contract = true;
2445 typec_set_pwr_opmode(port->typec_port, TYPEC_PWR_MODE_PD);
2446 port->pwr_opmode = TYPEC_PWR_MODE_PD;
2448 tcpm_typec_connect(port);
2450 tcpm_check_send_discover(port);
2453 /* Accessory states */
2454 case ACC_UNATTACHED:
2455 tcpm_acc_detach(port);
2456 tcpm_set_state(port, SRC_UNATTACHED, 0);
2458 case DEBUG_ACC_ATTACHED:
2459 case AUDIO_ACC_ATTACHED:
2460 ret = tcpm_acc_attach(port);
2462 tcpm_set_state(port, ACC_UNATTACHED, 0);
2464 case AUDIO_ACC_DEBOUNCE:
2465 tcpm_set_state(port, ACC_UNATTACHED, PD_T_CC_DEBOUNCE);
2468 /* Hard_Reset states */
2469 case HARD_RESET_SEND:
2470 tcpm_pd_transmit(port, TCPC_TX_HARD_RESET, NULL);
2471 tcpm_set_state(port, HARD_RESET_START, 0);
2473 case HARD_RESET_START:
2474 port->hard_reset_count++;
2475 port->tcpc->set_pd_rx(port->tcpc, false);
2476 tcpm_unregister_altmodes(port);
2477 port->send_discover = true;
2478 if (port->pwr_role == TYPEC_SOURCE)
2479 tcpm_set_state(port, SRC_HARD_RESET_VBUS_OFF,
2480 PD_T_PS_HARD_RESET);
2482 tcpm_set_state(port, SNK_HARD_RESET_SINK_OFF, 0);
2484 case SRC_HARD_RESET_VBUS_OFF:
2485 tcpm_set_vconn(port, true);
2486 tcpm_set_vbus(port, false);
2487 tcpm_set_roles(port, false, TYPEC_SOURCE, TYPEC_HOST);
2488 tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SRC_RECOVER);
2490 case SRC_HARD_RESET_VBUS_ON:
2491 tcpm_set_vbus(port, true);
2492 port->tcpc->set_pd_rx(port->tcpc, true);
2493 tcpm_set_attached_state(port, true);
2494 tcpm_set_state(port, SRC_UNATTACHED, PD_T_PS_SOURCE_ON);
2496 case SNK_HARD_RESET_SINK_OFF:
2497 tcpm_set_vconn(port, false);
2498 tcpm_set_charge(port, false);
2499 tcpm_set_roles(port, false, TYPEC_SINK, TYPEC_DEVICE);
2501 * VBUS may or may not toggle, depending on the adapter.
2502 * If it doesn't toggle, transition to SNK_HARD_RESET_SINK_ON
2503 * directly after timeout.
2505 tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, PD_T_SAFE_0V);
2507 case SNK_HARD_RESET_WAIT_VBUS:
2508 /* Assume we're disconnected if VBUS doesn't come back. */
2509 tcpm_set_state(port, SNK_UNATTACHED,
2510 PD_T_SRC_RECOVER_MAX + PD_T_SRC_TURN_ON);
2512 case SNK_HARD_RESET_SINK_ON:
2513 /* Note: There is no guarantee that VBUS is on in this state */
2516 * The specification suggests that dual mode ports in sink
2517 * mode should transition to state PE_SRC_Transition_to_default.
2518 * See USB power delivery specification chapter 8.3.3.6.1.3.
2519 * This would mean to to
2520 * - turn off VCONN, reset power supply
2521 * - request hardware reset
2523 * - Transition to state PE_Src_Startup
2524 * SNK only ports shall transition to state Snk_Startup
2525 * (see chapter 8.3.3.3.8).
2526 * Similar, dual-mode ports in source mode should transition
2527 * to PE_SNK_Transition_to_default.
2529 tcpm_set_attached_state(port, true);
2530 tcpm_set_state(port, SNK_STARTUP, 0);
2533 /* Soft_Reset states */
2535 port->message_id = 0;
2536 port->rx_msgid = -1;
2537 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
2538 if (port->pwr_role == TYPEC_SOURCE)
2539 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
2541 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
2543 case SOFT_RESET_SEND:
2544 port->message_id = 0;
2545 port->rx_msgid = -1;
2546 if (tcpm_pd_send_control(port, PD_CTRL_SOFT_RESET))
2547 tcpm_set_state_cond(port, hard_reset_state(port), 0);
2549 tcpm_set_state_cond(port, hard_reset_state(port),
2550 PD_T_SENDER_RESPONSE);
2553 /* DR_Swap states */
2555 tcpm_pd_send_control(port, PD_CTRL_DR_SWAP);
2556 tcpm_set_state_cond(port, DR_SWAP_SEND_TIMEOUT,
2557 PD_T_SENDER_RESPONSE);
2559 case DR_SWAP_ACCEPT:
2560 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
2561 tcpm_set_state_cond(port, DR_SWAP_CHANGE_DR, 0);
2563 case DR_SWAP_SEND_TIMEOUT:
2564 tcpm_swap_complete(port, -ETIMEDOUT);
2565 tcpm_set_state(port, ready_state(port), 0);
2567 case DR_SWAP_CHANGE_DR:
2568 if (port->data_role == TYPEC_HOST) {
2569 tcpm_unregister_altmodes(port);
2570 tcpm_set_roles(port, true, port->pwr_role,
2573 tcpm_set_roles(port, true, port->pwr_role,
2575 port->send_discover = true;
2577 tcpm_swap_complete(port, 0);
2578 tcpm_set_state(port, ready_state(port), 0);
2581 /* PR_Swap states */
2582 case PR_SWAP_ACCEPT:
2583 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
2584 tcpm_set_state(port, PR_SWAP_START, 0);
2587 tcpm_pd_send_control(port, PD_CTRL_PR_SWAP);
2588 tcpm_set_state_cond(port, PR_SWAP_SEND_TIMEOUT,
2589 PD_T_SENDER_RESPONSE);
2591 case PR_SWAP_SEND_TIMEOUT:
2592 tcpm_swap_complete(port, -ETIMEDOUT);
2593 tcpm_set_state(port, ready_state(port), 0);
2596 if (port->pwr_role == TYPEC_SOURCE)
2597 tcpm_set_state(port, PR_SWAP_SRC_SNK_TRANSITION_OFF,
2598 PD_T_SRC_TRANSITION);
2600 tcpm_set_state(port, PR_SWAP_SNK_SRC_SINK_OFF, 0);
2602 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
2603 tcpm_set_vbus(port, false);
2604 port->explicit_contract = false;
2605 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF,
2606 PD_T_PS_SOURCE_OFF);
2608 case PR_SWAP_SRC_SNK_SOURCE_OFF:
2609 tcpm_set_cc(port, TYPEC_CC_RD);
2611 * USB-PD standard, 6.2.1.4, Port Power Role:
2612 * "During the Power Role Swap Sequence, for the initial Source
2613 * Port, the Port Power Role field shall be set to Sink in the
2614 * PS_RDY Message indicating that the initial Source’s power
2615 * supply is turned off"
2617 tcpm_set_pwr_role(port, TYPEC_SINK);
2618 if (tcpm_pd_send_control(port, PD_CTRL_PS_RDY)) {
2619 tcpm_set_state(port, ERROR_RECOVERY, 0);
2622 tcpm_set_state_cond(port, SNK_UNATTACHED, PD_T_PS_SOURCE_ON);
2624 case PR_SWAP_SRC_SNK_SINK_ON:
2625 tcpm_swap_complete(port, 0);
2626 tcpm_set_state(port, SNK_STARTUP, 0);
2628 case PR_SWAP_SNK_SRC_SINK_OFF:
2629 tcpm_set_charge(port, false);
2630 tcpm_set_state(port, hard_reset_state(port),
2631 PD_T_PS_SOURCE_OFF);
2633 case PR_SWAP_SNK_SRC_SOURCE_ON:
2634 tcpm_set_cc(port, tcpm_rp_cc(port));
2635 tcpm_set_vbus(port, true);
2637 * USB PD standard, 6.2.1.4:
2638 * "Subsequent Messages initiated by the Policy Engine,
2639 * such as the PS_RDY Message sent to indicate that Vbus
2640 * is ready, will have the Port Power Role field set to
2643 tcpm_set_pwr_role(port, TYPEC_SOURCE);
2644 tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
2645 tcpm_swap_complete(port, 0);
2646 tcpm_set_state(port, SRC_STARTUP, 0);
2649 case VCONN_SWAP_ACCEPT:
2650 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
2651 tcpm_set_state(port, VCONN_SWAP_START, 0);
2653 case VCONN_SWAP_SEND:
2654 tcpm_pd_send_control(port, PD_CTRL_VCONN_SWAP);
2655 tcpm_set_state(port, VCONN_SWAP_SEND_TIMEOUT,
2656 PD_T_SENDER_RESPONSE);
2658 case VCONN_SWAP_SEND_TIMEOUT:
2659 tcpm_swap_complete(port, -ETIMEDOUT);
2660 tcpm_set_state(port, ready_state(port), 0);
2662 case VCONN_SWAP_START:
2663 if (port->vconn_role == TYPEC_SOURCE)
2664 tcpm_set_state(port, VCONN_SWAP_WAIT_FOR_VCONN, 0);
2666 tcpm_set_state(port, VCONN_SWAP_TURN_ON_VCONN, 0);
2668 case VCONN_SWAP_WAIT_FOR_VCONN:
2669 tcpm_set_state(port, hard_reset_state(port),
2670 PD_T_VCONN_SOURCE_ON);
2672 case VCONN_SWAP_TURN_ON_VCONN:
2673 tcpm_set_vconn(port, true);
2674 tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
2675 tcpm_swap_complete(port, 0);
2676 tcpm_set_state(port, ready_state(port), 0);
2678 case VCONN_SWAP_TURN_OFF_VCONN:
2679 tcpm_set_vconn(port, false);
2680 tcpm_swap_complete(port, 0);
2681 tcpm_set_state(port, ready_state(port), 0);
2684 case DR_SWAP_CANCEL:
2685 case PR_SWAP_CANCEL:
2686 case VCONN_SWAP_CANCEL:
2687 tcpm_swap_complete(port, port->swap_status);
2688 if (port->pwr_role == TYPEC_SOURCE)
2689 tcpm_set_state(port, SRC_READY, 0);
2691 tcpm_set_state(port, SNK_READY, 0);
2695 switch (BDO_MODE_MASK(port->bist_request)) {
2696 case BDO_MODE_CARRIER2:
2697 tcpm_pd_transmit(port, TCPC_TX_BIST_MODE_2, NULL);
2702 /* Always switch to unattached state */
2703 tcpm_set_state(port, unattached_state(port), 0);
2705 case ERROR_RECOVERY:
2706 tcpm_swap_complete(port, -EPROTO);
2707 tcpm_reset_port(port);
2709 tcpm_set_cc(port, TYPEC_CC_OPEN);
2710 tcpm_set_state(port, ERROR_RECOVERY_WAIT_OFF,
2711 PD_T_ERROR_RECOVERY);
2713 case ERROR_RECOVERY_WAIT_OFF:
2714 tcpm_set_state(port,
2715 tcpm_default_state(port),
2716 port->vbus_present ? PD_T_PS_SOURCE_OFF : 0);
2719 WARN(1, "Unexpected port state %d\n", port->state);
2724 static void tcpm_state_machine_work(struct work_struct *work)
2726 struct tcpm_port *port = container_of(work, struct tcpm_port,
2727 state_machine.work);
2728 enum tcpm_state prev_state;
2730 mutex_lock(&port->lock);
2731 port->state_machine_running = true;
2733 if (port->queued_message && tcpm_send_queued_message(port))
2736 /* If we were queued due to a delayed state change, update it now */
2737 if (port->delayed_state) {
2738 tcpm_log(port, "state change %s -> %s [delayed %ld ms]",
2739 tcpm_states[port->state],
2740 tcpm_states[port->delayed_state], port->delay_ms);
2741 port->prev_state = port->state;
2742 port->state = port->delayed_state;
2743 port->delayed_state = INVALID_STATE;
2747 * Continue running as long as we have (non-delayed) state changes
2751 prev_state = port->state;
2752 run_state_machine(port);
2753 if (port->queued_message)
2754 tcpm_send_queued_message(port);
2755 } while (port->state != prev_state && !port->delayed_state);
2758 port->state_machine_running = false;
2759 mutex_unlock(&port->lock);
2762 static void _tcpm_cc_change(struct tcpm_port *port, enum typec_cc_status cc1,
2763 enum typec_cc_status cc2)
2765 enum typec_cc_status old_cc1, old_cc2;
2766 enum tcpm_state new_state;
2768 old_cc1 = port->cc1;
2769 old_cc2 = port->cc2;
2773 tcpm_log_force(port,
2774 "CC1: %u -> %u, CC2: %u -> %u [state %s, polarity %d, %s]",
2775 old_cc1, cc1, old_cc2, cc2, tcpm_states[port->state],
2777 tcpm_port_is_disconnected(port) ? "disconnected"
2780 switch (port->state) {
2782 if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) ||
2783 tcpm_port_is_source(port))
2784 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
2785 else if (tcpm_port_is_sink(port))
2786 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
2788 case SRC_UNATTACHED:
2789 case ACC_UNATTACHED:
2790 if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) ||
2791 tcpm_port_is_source(port))
2792 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
2794 case SRC_ATTACH_WAIT:
2795 if (tcpm_port_is_disconnected(port) ||
2796 tcpm_port_is_audio_detached(port))
2797 tcpm_set_state(port, SRC_UNATTACHED, 0);
2798 else if (cc1 != old_cc1 || cc2 != old_cc2)
2799 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
2802 if (tcpm_port_is_disconnected(port))
2803 tcpm_set_state(port, SRC_UNATTACHED, 0);
2806 case SNK_UNATTACHED:
2807 if (tcpm_port_is_sink(port))
2808 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
2810 case SNK_ATTACH_WAIT:
2811 if ((port->cc1 == TYPEC_CC_OPEN &&
2812 port->cc2 != TYPEC_CC_OPEN) ||
2813 (port->cc1 != TYPEC_CC_OPEN &&
2814 port->cc2 == TYPEC_CC_OPEN))
2815 new_state = SNK_DEBOUNCED;
2816 else if (tcpm_port_is_disconnected(port))
2817 new_state = SNK_UNATTACHED;
2820 if (new_state != port->delayed_state)
2821 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
2824 if (tcpm_port_is_disconnected(port))
2825 new_state = SNK_UNATTACHED;
2826 else if (port->vbus_present)
2827 new_state = tcpm_try_src(port) ? SRC_TRY : SNK_ATTACHED;
2829 new_state = SNK_UNATTACHED;
2830 if (new_state != port->delayed_state)
2831 tcpm_set_state(port, SNK_DEBOUNCED, 0);
2834 if (tcpm_port_is_disconnected(port))
2835 tcpm_set_state(port, unattached_state(port), 0);
2836 else if (!port->pd_capable &&
2837 (cc1 != old_cc1 || cc2 != old_cc2))
2838 tcpm_set_current_limit(port,
2839 tcpm_get_current_limit(port),
2843 case AUDIO_ACC_ATTACHED:
2844 if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN)
2845 tcpm_set_state(port, AUDIO_ACC_DEBOUNCE, 0);
2847 case AUDIO_ACC_DEBOUNCE:
2848 if (tcpm_port_is_audio(port))
2849 tcpm_set_state(port, AUDIO_ACC_ATTACHED, 0);
2852 case DEBUG_ACC_ATTACHED:
2853 if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN)
2854 tcpm_set_state(port, ACC_UNATTACHED, 0);
2858 /* Do nothing, waiting for timeout */
2862 /* CC line is unstable, wait for debounce */
2863 if (tcpm_port_is_disconnected(port))
2864 tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE, 0);
2866 case SNK_DISCOVERY_DEBOUNCE:
2870 /* Hand over to state machine if needed */
2871 if (!port->vbus_present && tcpm_port_is_source(port))
2872 new_state = SRC_ATTACHED;
2874 new_state = SRC_TRYWAIT_UNATTACHED;
2876 if (new_state != port->delayed_state)
2877 tcpm_set_state(port, SRC_TRYWAIT, 0);
2880 if (port->vbus_present && tcpm_port_is_sink(port)) {
2881 tcpm_set_state(port, SNK_ATTACHED, 0);
2884 if (!tcpm_port_is_sink(port))
2885 new_state = SRC_TRYWAIT;
2887 new_state = SRC_TRYWAIT_UNATTACHED;
2889 if (new_state != port->delayed_state)
2890 tcpm_set_state(port, SNK_TRY_WAIT, 0);
2894 tcpm_set_state(port, SRC_TRY_DEBOUNCE, 0);
2896 case SRC_TRY_DEBOUNCE:
2897 tcpm_set_state(port, SRC_TRY, 0);
2899 case SNK_TRYWAIT_DEBOUNCE:
2900 if (port->vbus_present) {
2901 tcpm_set_state(port, SNK_ATTACHED, 0);
2904 if (tcpm_port_is_source(port)) {
2905 tcpm_set_state(port, SRC_ATTACHED, 0);
2908 if (tcpm_port_is_disconnected(port) &&
2909 port->delayed_state != SNK_UNATTACHED)
2910 tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, 0);
2913 case PR_SWAP_SNK_SRC_SINK_OFF:
2914 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
2915 case PR_SWAP_SRC_SNK_SOURCE_OFF:
2917 * CC state change is expected here; we just turned off power.
2923 if (tcpm_port_is_disconnected(port))
2924 tcpm_set_state(port, unattached_state(port), 0);
2929 static void _tcpm_pd_vbus_on(struct tcpm_port *port)
2931 enum tcpm_state new_state;
2933 tcpm_log_force(port, "VBUS on");
2934 port->vbus_present = true;
2935 switch (port->state) {
2936 case SNK_TRANSITION_SINK_VBUS:
2937 tcpm_set_state(port, SNK_READY, 0);
2940 tcpm_set_state(port, SNK_DISCOVERY, 0);
2944 tcpm_set_state(port, tcpm_try_src(port) ? SRC_TRY
2948 case SNK_HARD_RESET_WAIT_VBUS:
2949 tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, 0);
2952 tcpm_set_state(port, SRC_STARTUP, 0);
2954 case SRC_HARD_RESET_VBUS_ON:
2955 tcpm_set_state(port, SRC_STARTUP, 0);
2959 /* Do nothing, waiting for timeout */
2962 /* Hand over to state machine if needed */
2963 if (port->delayed_state != SRC_TRYWAIT_UNATTACHED)
2964 tcpm_set_state(port, SRC_TRYWAIT, 0);
2967 if (tcpm_port_is_sink(port)) {
2968 tcpm_set_state(port, SNK_ATTACHED, 0);
2971 if (!tcpm_port_is_sink(port))
2972 new_state = SRC_TRYWAIT;
2974 new_state = SRC_TRYWAIT_UNATTACHED;
2976 if (new_state != port->delayed_state)
2977 tcpm_set_state(port, SNK_TRY_WAIT, 0);
2980 tcpm_set_state(port, SNK_TRYWAIT_VBUS, 0);
2988 static void _tcpm_pd_vbus_off(struct tcpm_port *port)
2990 enum tcpm_state new_state;
2992 tcpm_log_force(port, "VBUS off");
2993 port->vbus_present = false;
2994 port->vbus_never_low = false;
2995 switch (port->state) {
2996 case SNK_HARD_RESET_SINK_OFF:
2997 tcpm_set_state(port, SNK_HARD_RESET_WAIT_VBUS, 0);
2999 case SRC_HARD_RESET_VBUS_OFF:
3000 tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, 0);
3002 case HARD_RESET_SEND:
3006 /* Do nothing, waiting for timeout */
3009 /* Hand over to state machine if needed */
3010 if (tcpm_port_is_source(port))
3011 new_state = SRC_ATTACHED;
3013 new_state = SRC_TRYWAIT_UNATTACHED;
3014 if (new_state != port->delayed_state)
3015 tcpm_set_state(port, SRC_TRYWAIT, 0);
3018 if (!tcpm_port_is_sink(port))
3019 new_state = SRC_TRYWAIT;
3021 new_state = SRC_TRYWAIT_UNATTACHED;
3023 if (new_state != port->delayed_state)
3024 tcpm_set_state(port, SNK_TRY_WAIT, 0);
3026 case SNK_TRYWAIT_VBUS:
3027 tcpm_set_state(port, SNK_TRYWAIT, 0);
3030 case SNK_ATTACH_WAIT:
3031 tcpm_set_state(port, SNK_UNATTACHED, 0);
3034 case SNK_NEGOTIATE_CAPABILITIES:
3037 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
3038 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF, 0);
3041 case PR_SWAP_SNK_SRC_SINK_OFF:
3042 /* Do nothing, expected */
3045 case ERROR_RECOVERY_WAIT_OFF:
3046 tcpm_set_state(port,
3047 port->pwr_role == TYPEC_SOURCE ?
3048 SRC_UNATTACHED : SNK_UNATTACHED,
3053 if (port->pwr_role == TYPEC_SINK &&
3055 tcpm_set_state(port, SNK_UNATTACHED, 0);
3060 static void _tcpm_pd_hard_reset(struct tcpm_port *port)
3062 tcpm_log_force(port, "Received hard reset");
3064 * If we keep receiving hard reset requests, executing the hard reset
3065 * must have failed. Revert to error recovery if that happens.
3067 tcpm_set_state(port,
3068 port->hard_reset_count < PD_N_HARD_RESET_COUNT ?
3069 HARD_RESET_START : ERROR_RECOVERY,
3073 static void tcpm_pd_event_handler(struct work_struct *work)
3075 struct tcpm_port *port = container_of(work, struct tcpm_port,
3079 mutex_lock(&port->lock);
3081 spin_lock(&port->pd_event_lock);
3082 while (port->pd_events) {
3083 events = port->pd_events;
3084 port->pd_events = 0;
3085 spin_unlock(&port->pd_event_lock);
3086 if (events & TCPM_RESET_EVENT)
3087 _tcpm_pd_hard_reset(port);
3088 if (events & TCPM_VBUS_EVENT) {
3091 vbus = port->tcpc->get_vbus(port->tcpc);
3093 _tcpm_pd_vbus_on(port);
3095 _tcpm_pd_vbus_off(port);
3097 if (events & TCPM_CC_EVENT) {
3098 enum typec_cc_status cc1, cc2;
3100 if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0)
3101 _tcpm_cc_change(port, cc1, cc2);
3103 spin_lock(&port->pd_event_lock);
3105 spin_unlock(&port->pd_event_lock);
3106 mutex_unlock(&port->lock);
3109 void tcpm_cc_change(struct tcpm_port *port)
3111 spin_lock(&port->pd_event_lock);
3112 port->pd_events |= TCPM_CC_EVENT;
3113 spin_unlock(&port->pd_event_lock);
3114 queue_work(port->wq, &port->event_work);
3116 EXPORT_SYMBOL_GPL(tcpm_cc_change);
3118 void tcpm_vbus_change(struct tcpm_port *port)
3120 spin_lock(&port->pd_event_lock);
3121 port->pd_events |= TCPM_VBUS_EVENT;
3122 spin_unlock(&port->pd_event_lock);
3123 queue_work(port->wq, &port->event_work);
3125 EXPORT_SYMBOL_GPL(tcpm_vbus_change);
3127 void tcpm_pd_hard_reset(struct tcpm_port *port)
3129 spin_lock(&port->pd_event_lock);
3130 port->pd_events = TCPM_RESET_EVENT;
3131 spin_unlock(&port->pd_event_lock);
3132 queue_work(port->wq, &port->event_work);
3134 EXPORT_SYMBOL_GPL(tcpm_pd_hard_reset);
3136 static int tcpm_dr_set(const struct typec_capability *cap,
3137 enum typec_data_role data)
3139 struct tcpm_port *port = typec_cap_to_tcpm(cap);
3142 mutex_lock(&port->swap_lock);
3143 mutex_lock(&port->lock);
3145 if (port->typec_caps.type != TYPEC_PORT_DRP || !port->pd_capable) {
3149 if (port->state != SRC_READY && port->state != SNK_READY) {
3154 if (port->data_role == data) {
3161 * 6.3.9: If an alternate mode is active, a request to swap
3162 * alternate modes shall trigger a port reset.
3163 * Reject data role swap request in this case.
3166 port->swap_status = 0;
3167 port->swap_pending = true;
3168 reinit_completion(&port->swap_complete);
3169 tcpm_set_state(port, DR_SWAP_SEND, 0);
3170 mutex_unlock(&port->lock);
3172 wait_for_completion(&port->swap_complete);
3174 ret = port->swap_status;
3178 mutex_unlock(&port->lock);
3180 mutex_unlock(&port->swap_lock);
3184 static int tcpm_pr_set(const struct typec_capability *cap,
3185 enum typec_role role)
3187 struct tcpm_port *port = typec_cap_to_tcpm(cap);
3190 mutex_lock(&port->swap_lock);
3191 mutex_lock(&port->lock);
3193 if (port->typec_caps.type != TYPEC_PORT_DRP) {
3197 if (port->state != SRC_READY && port->state != SNK_READY) {
3202 if (role == port->pwr_role) {
3207 if (!port->pd_capable) {
3209 * If the partner is not PD capable, reset the port to
3210 * trigger a role change. This can only work if a preferred
3211 * role is configured, and if it matches the requested role.
3213 if (port->try_role == TYPEC_NO_PREFERRED_ROLE ||
3214 port->try_role == port->pwr_role) {
3218 tcpm_set_state(port, HARD_RESET_SEND, 0);
3223 port->swap_status = 0;
3224 port->swap_pending = true;
3225 reinit_completion(&port->swap_complete);
3226 tcpm_set_state(port, PR_SWAP_SEND, 0);
3227 mutex_unlock(&port->lock);
3229 wait_for_completion(&port->swap_complete);
3231 ret = port->swap_status;
3235 mutex_unlock(&port->lock);
3237 mutex_unlock(&port->swap_lock);
3241 static int tcpm_vconn_set(const struct typec_capability *cap,
3242 enum typec_role role)
3244 struct tcpm_port *port = typec_cap_to_tcpm(cap);
3247 mutex_lock(&port->swap_lock);
3248 mutex_lock(&port->lock);
3250 if (port->state != SRC_READY && port->state != SNK_READY) {
3255 if (role == port->vconn_role) {
3260 port->swap_status = 0;
3261 port->swap_pending = true;
3262 reinit_completion(&port->swap_complete);
3263 tcpm_set_state(port, VCONN_SWAP_SEND, 0);
3264 mutex_unlock(&port->lock);
3266 wait_for_completion(&port->swap_complete);
3268 ret = port->swap_status;
3272 mutex_unlock(&port->lock);
3274 mutex_unlock(&port->swap_lock);
3278 static int tcpm_try_role(const struct typec_capability *cap, int role)
3280 struct tcpm_port *port = typec_cap_to_tcpm(cap);
3281 struct tcpc_dev *tcpc = port->tcpc;
3284 mutex_lock(&port->lock);
3286 ret = tcpc->try_role(tcpc, role);
3287 if (!ret && !tcpc->config->try_role_hw)
3288 port->try_role = role;
3289 port->try_src_count = 0;
3290 port->try_snk_count = 0;
3291 mutex_unlock(&port->lock);
3296 static void tcpm_init(struct tcpm_port *port)
3298 enum typec_cc_status cc1, cc2;
3300 port->tcpc->init(port->tcpc);
3302 tcpm_reset_port(port);
3306 * Should possibly wait for VBUS to settle if it was enabled locally
3307 * since tcpm_reset_port() will disable VBUS.
3309 port->vbus_present = port->tcpc->get_vbus(port->tcpc);
3310 if (port->vbus_present)
3311 port->vbus_never_low = true;
3313 tcpm_set_state(port, tcpm_default_state(port), 0);
3315 if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0)
3316 _tcpm_cc_change(port, cc1, cc2);
3319 * Some adapters need a clean slate at startup, and won't recover
3320 * otherwise. So do not try to be fancy and force a clean disconnect.
3322 tcpm_set_state(port, ERROR_RECOVERY, 0);
3325 void tcpm_tcpc_reset(struct tcpm_port *port)
3327 mutex_lock(&port->lock);
3328 /* XXX: Maintain PD connection if possible? */
3330 mutex_unlock(&port->lock);
3332 EXPORT_SYMBOL_GPL(tcpm_tcpc_reset);
3334 static int tcpm_copy_pdos(u32 *dest_pdo, const u32 *src_pdo,
3335 unsigned int nr_pdo)
3339 if (nr_pdo > PDO_MAX_OBJECTS)
3340 nr_pdo = PDO_MAX_OBJECTS;
3342 for (i = 0; i < nr_pdo; i++)
3343 dest_pdo[i] = src_pdo[i];
3348 static int tcpm_copy_vdos(u32 *dest_vdo, const u32 *src_vdo,
3349 unsigned int nr_vdo)
3353 if (nr_vdo > VDO_MAX_OBJECTS)
3354 nr_vdo = VDO_MAX_OBJECTS;
3356 for (i = 0; i < nr_vdo; i++)
3357 dest_vdo[i] = src_vdo[i];
3362 void tcpm_update_source_capabilities(struct tcpm_port *port, const u32 *pdo,
3363 unsigned int nr_pdo)
3365 mutex_lock(&port->lock);
3366 port->nr_src_pdo = tcpm_copy_pdos(port->src_pdo, pdo, nr_pdo);
3367 switch (port->state) {
3368 case SRC_UNATTACHED:
3369 case SRC_ATTACH_WAIT:
3371 tcpm_set_cc(port, tcpm_rp_cc(port));
3373 case SRC_SEND_CAPABILITIES:
3374 case SRC_NEGOTIATE_CAPABILITIES:
3376 case SRC_WAIT_NEW_CAPABILITIES:
3377 tcpm_set_cc(port, tcpm_rp_cc(port));
3378 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
3383 mutex_unlock(&port->lock);
3385 EXPORT_SYMBOL_GPL(tcpm_update_source_capabilities);
3387 void tcpm_update_sink_capabilities(struct tcpm_port *port, const u32 *pdo,
3388 unsigned int nr_pdo,
3389 unsigned int max_snk_mv,
3390 unsigned int max_snk_ma,
3391 unsigned int max_snk_mw,
3392 unsigned int operating_snk_mw)
3394 mutex_lock(&port->lock);
3395 port->nr_snk_pdo = tcpm_copy_pdos(port->snk_pdo, pdo, nr_pdo);
3396 port->max_snk_mv = max_snk_mv;
3397 port->max_snk_ma = max_snk_ma;
3398 port->max_snk_mw = max_snk_mw;
3399 port->operating_snk_mw = operating_snk_mw;
3401 switch (port->state) {
3402 case SNK_NEGOTIATE_CAPABILITIES:
3404 case SNK_TRANSITION_SINK:
3405 case SNK_TRANSITION_SINK_VBUS:
3406 tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
3411 mutex_unlock(&port->lock);
3413 EXPORT_SYMBOL_GPL(tcpm_update_sink_capabilities);
3415 struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
3417 struct tcpm_port *port;
3420 if (!dev || !tcpc || !tcpc->config ||
3421 !tcpc->get_vbus || !tcpc->set_cc || !tcpc->get_cc ||
3422 !tcpc->set_polarity || !tcpc->set_vconn || !tcpc->set_vbus ||
3423 !tcpc->set_pd_rx || !tcpc->set_roles || !tcpc->pd_transmit)
3424 return ERR_PTR(-EINVAL);
3426 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
3428 return ERR_PTR(-ENOMEM);
3433 mutex_init(&port->lock);
3434 mutex_init(&port->swap_lock);
3436 port->wq = create_singlethread_workqueue(dev_name(dev));
3438 return ERR_PTR(-ENOMEM);
3439 INIT_DELAYED_WORK(&port->state_machine, tcpm_state_machine_work);
3440 INIT_DELAYED_WORK(&port->vdm_state_machine, vdm_state_machine_work);
3441 INIT_WORK(&port->event_work, tcpm_pd_event_handler);
3443 spin_lock_init(&port->pd_event_lock);
3445 init_completion(&port->tx_complete);
3446 init_completion(&port->swap_complete);
3448 port->nr_src_pdo = tcpm_copy_pdos(port->src_pdo, tcpc->config->src_pdo,
3449 tcpc->config->nr_src_pdo);
3450 port->nr_snk_pdo = tcpm_copy_pdos(port->snk_pdo, tcpc->config->snk_pdo,
3451 tcpc->config->nr_snk_pdo);
3452 port->nr_snk_vdo = tcpm_copy_vdos(port->snk_vdo, tcpc->config->snk_vdo,
3453 tcpc->config->nr_snk_vdo);
3455 port->max_snk_mv = tcpc->config->max_snk_mv;
3456 port->max_snk_ma = tcpc->config->max_snk_ma;
3457 port->max_snk_mw = tcpc->config->max_snk_mw;
3458 port->operating_snk_mw = tcpc->config->operating_snk_mw;
3459 if (!tcpc->config->try_role_hw)
3460 port->try_role = tcpc->config->default_role;
3462 port->try_role = TYPEC_NO_PREFERRED_ROLE;
3464 port->typec_caps.prefer_role = tcpc->config->default_role;
3465 port->typec_caps.type = tcpc->config->type;
3466 port->typec_caps.revision = 0x0120; /* Type-C spec release 1.2 */
3467 port->typec_caps.pd_revision = 0x0200; /* USB-PD spec release 2.0 */
3468 port->typec_caps.dr_set = tcpm_dr_set;
3469 port->typec_caps.pr_set = tcpm_pr_set;
3470 port->typec_caps.vconn_set = tcpm_vconn_set;
3471 port->typec_caps.try_role = tcpm_try_role;
3473 port->partner_desc.identity = &port->partner_ident;
3477 * - alt_modes, set_alt_mode
3478 * - {debug,audio}_accessory
3481 port->typec_port = typec_register_port(port->dev, &port->typec_caps);
3482 if (!port->typec_port) {
3484 goto out_destroy_wq;
3487 if (tcpc->config->alt_modes) {
3488 struct typec_altmode_desc *paltmode = tcpc->config->alt_modes;
3491 while (paltmode->svid && i < ARRAY_SIZE(port->port_altmode)) {
3492 port->port_altmode[i] =
3493 typec_port_register_altmode(port->typec_port,
3495 if (!port->port_altmode[i]) {
3497 "%s: failed to register port alternate mode 0x%x",
3498 dev_name(dev), paltmode->svid);
3506 tcpm_debugfs_init(port);
3507 mutex_lock(&port->lock);
3509 mutex_unlock(&port->lock);
3511 tcpm_log(port, "%s: registered", dev_name(dev));
3515 destroy_workqueue(port->wq);
3516 return ERR_PTR(err);
3518 EXPORT_SYMBOL_GPL(tcpm_register_port);
3520 void tcpm_unregister_port(struct tcpm_port *port)
3524 for (i = 0; i < ARRAY_SIZE(port->port_altmode); i++)
3525 typec_unregister_altmode(port->port_altmode[i]);
3526 typec_unregister_port(port->typec_port);
3527 tcpm_debugfs_exit(port);
3528 destroy_workqueue(port->wq);
3530 EXPORT_SYMBOL_GPL(tcpm_unregister_port);
3532 MODULE_AUTHOR("Guenter Roeck <groeck@chromium.org>");
3533 MODULE_DESCRIPTION("USB Type-C Port Manager");
3534 MODULE_LICENSE("GPL");