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;
242 /* Partner capabilities/requests */
244 u32 source_caps[PDO_MAX_OBJECTS];
245 unsigned int nr_source_caps;
246 u32 sink_caps[PDO_MAX_OBJECTS];
247 unsigned int nr_sink_caps;
249 /* Local capabilities */
250 u32 src_pdo[PDO_MAX_OBJECTS];
251 unsigned int nr_src_pdo;
252 u32 snk_pdo[PDO_MAX_OBJECTS];
253 unsigned int nr_snk_pdo;
255 unsigned int max_snk_mv;
256 unsigned int max_snk_ma;
257 unsigned int max_snk_mw;
258 unsigned int operating_snk_mw;
260 /* Requested current / voltage */
266 /* PD state for Vendor Defined Messages */
267 enum vdm_states vdm_state;
269 /* next Vendor Defined Message to send */
270 u32 vdo_data[VDO_MAX_SIZE];
272 /* VDO to retry if UFP responder replied busy */
275 /* Alternate mode data */
277 struct pd_mode_data mode_data;
278 struct typec_altmode *partner_altmode[SVID_DISCOVERY_MAX];
279 struct typec_altmode *port_altmode[SVID_DISCOVERY_MAX];
281 #ifdef CONFIG_DEBUG_FS
282 struct dentry *dentry;
283 struct mutex logbuffer_lock; /* log buffer access lock */
286 u8 *logbuffer[LOG_BUFFER_ENTRIES];
291 struct work_struct work;
292 struct tcpm_port *port;
293 struct pd_message msg;
296 #define tcpm_cc_is_sink(cc) \
297 ((cc) == TYPEC_CC_RP_DEF || (cc) == TYPEC_CC_RP_1_5 || \
298 (cc) == TYPEC_CC_RP_3_0)
300 #define tcpm_port_is_sink(port) \
301 ((tcpm_cc_is_sink((port)->cc1) && !tcpm_cc_is_sink((port)->cc2)) || \
302 (tcpm_cc_is_sink((port)->cc2) && !tcpm_cc_is_sink((port)->cc1)))
304 #define tcpm_cc_is_source(cc) ((cc) == TYPEC_CC_RD)
305 #define tcpm_cc_is_audio(cc) ((cc) == TYPEC_CC_RA)
306 #define tcpm_cc_is_open(cc) ((cc) == TYPEC_CC_OPEN)
308 #define tcpm_port_is_source(port) \
309 ((tcpm_cc_is_source((port)->cc1) && \
310 !tcpm_cc_is_source((port)->cc2)) || \
311 (tcpm_cc_is_source((port)->cc2) && \
312 !tcpm_cc_is_source((port)->cc1)))
314 #define tcpm_port_is_debug(port) \
315 (tcpm_cc_is_source((port)->cc1) && tcpm_cc_is_source((port)->cc2))
317 #define tcpm_port_is_audio(port) \
318 (tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_audio((port)->cc2))
320 #define tcpm_port_is_audio_detached(port) \
321 ((tcpm_cc_is_audio((port)->cc1) && tcpm_cc_is_open((port)->cc2)) || \
322 (tcpm_cc_is_audio((port)->cc2) && tcpm_cc_is_open((port)->cc1)))
324 #define tcpm_try_snk(port) \
325 ((port)->try_snk_count == 0 && (port)->try_role == TYPEC_SINK)
327 #define tcpm_try_src(port) \
328 ((port)->try_src_count == 0 && (port)->try_role == TYPEC_SOURCE)
330 static enum tcpm_state tcpm_default_state(struct tcpm_port *port)
332 if (port->try_role == TYPEC_SINK)
333 return SNK_UNATTACHED;
334 else if (port->try_role == TYPEC_SOURCE)
335 return SRC_UNATTACHED;
336 else if (port->tcpc->config->default_role == TYPEC_SINK)
337 return SNK_UNATTACHED;
338 return SRC_UNATTACHED;
342 struct tcpm_port *typec_cap_to_tcpm(const struct typec_capability *cap)
344 return container_of(cap, struct tcpm_port, typec_caps);
347 static bool tcpm_port_is_disconnected(struct tcpm_port *port)
349 return (!port->attached && port->cc1 == TYPEC_CC_OPEN &&
350 port->cc2 == TYPEC_CC_OPEN) ||
351 (port->attached && ((port->polarity == TYPEC_POLARITY_CC1 &&
352 port->cc1 == TYPEC_CC_OPEN) ||
353 (port->polarity == TYPEC_POLARITY_CC2 &&
354 port->cc2 == TYPEC_CC_OPEN)));
361 #ifdef CONFIG_DEBUG_FS
363 static bool tcpm_log_full(struct tcpm_port *port)
365 return port->logbuffer_tail ==
366 (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
369 static void _tcpm_log(struct tcpm_port *port, const char *fmt, va_list args)
371 char tmpbuffer[LOG_BUFFER_ENTRY_SIZE];
372 u64 ts_nsec = local_clock();
373 unsigned long rem_nsec;
375 if (!port->logbuffer[port->logbuffer_head]) {
376 port->logbuffer[port->logbuffer_head] =
377 kzalloc(LOG_BUFFER_ENTRY_SIZE, GFP_KERNEL);
378 if (!port->logbuffer[port->logbuffer_head])
382 vsnprintf(tmpbuffer, sizeof(tmpbuffer), fmt, args);
384 mutex_lock(&port->logbuffer_lock);
386 if (tcpm_log_full(port)) {
387 port->logbuffer_head = max(port->logbuffer_head - 1, 0);
388 strcpy(tmpbuffer, "overflow");
391 if (port->logbuffer_head < 0 ||
392 port->logbuffer_head >= LOG_BUFFER_ENTRIES) {
394 "Bad log buffer index %d\n", port->logbuffer_head);
398 if (!port->logbuffer[port->logbuffer_head]) {
400 "Log buffer index %d is NULL\n", port->logbuffer_head);
404 rem_nsec = do_div(ts_nsec, 1000000000);
405 scnprintf(port->logbuffer[port->logbuffer_head],
406 LOG_BUFFER_ENTRY_SIZE, "[%5lu.%06lu] %s",
407 (unsigned long)ts_nsec, rem_nsec / 1000,
409 port->logbuffer_head = (port->logbuffer_head + 1) % LOG_BUFFER_ENTRIES;
412 mutex_unlock(&port->logbuffer_lock);
415 static void tcpm_log(struct tcpm_port *port, const char *fmt, ...)
419 /* Do not log while disconnected and unattached */
420 if (tcpm_port_is_disconnected(port) &&
421 (port->state == SRC_UNATTACHED || port->state == SNK_UNATTACHED ||
422 port->state == DRP_TOGGLING))
426 _tcpm_log(port, fmt, args);
430 static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...)
435 _tcpm_log(port, fmt, args);
439 static void tcpm_log_source_caps(struct tcpm_port *port)
443 for (i = 0; i < port->nr_source_caps; i++) {
444 u32 pdo = port->source_caps[i];
445 enum pd_pdo_type type = pdo_type(pdo);
450 scnprintf(msg, sizeof(msg),
451 "%u mV, %u mA [%s%s%s%s%s%s]",
452 pdo_fixed_voltage(pdo),
453 pdo_max_current(pdo),
454 (pdo & PDO_FIXED_DUAL_ROLE) ?
456 (pdo & PDO_FIXED_SUSPEND) ?
458 (pdo & PDO_FIXED_HIGHER_CAP) ?
460 (pdo & PDO_FIXED_USB_COMM) ?
462 (pdo & PDO_FIXED_DATA_SWAP) ?
464 (pdo & PDO_FIXED_EXTPOWER) ?
468 scnprintf(msg, sizeof(msg),
470 pdo_min_voltage(pdo),
471 pdo_max_voltage(pdo),
472 pdo_max_current(pdo));
475 scnprintf(msg, sizeof(msg),
477 pdo_min_voltage(pdo),
478 pdo_max_voltage(pdo),
482 strcpy(msg, "undefined");
485 tcpm_log(port, " PDO %d: type %d, %s",
490 static int tcpm_seq_show(struct seq_file *s, void *v)
492 struct tcpm_port *port = (struct tcpm_port *)s->private;
495 mutex_lock(&port->logbuffer_lock);
496 tail = port->logbuffer_tail;
497 while (tail != port->logbuffer_head) {
498 seq_printf(s, "%s\n", port->logbuffer[tail]);
499 tail = (tail + 1) % LOG_BUFFER_ENTRIES;
501 if (!seq_has_overflowed(s))
502 port->logbuffer_tail = tail;
503 mutex_unlock(&port->logbuffer_lock);
508 static int tcpm_debug_open(struct inode *inode, struct file *file)
510 return single_open(file, tcpm_seq_show, inode->i_private);
513 static const struct file_operations tcpm_debug_operations = {
514 .open = tcpm_debug_open,
517 .release = single_release,
520 static struct dentry *rootdir;
522 static int tcpm_debugfs_init(struct tcpm_port *port)
524 mutex_init(&port->logbuffer_lock);
525 /* /sys/kernel/debug/tcpm/usbcX */
527 rootdir = debugfs_create_dir("tcpm", NULL);
532 port->dentry = debugfs_create_file(dev_name(port->dev),
533 S_IFREG | 0444, rootdir,
534 port, &tcpm_debug_operations);
539 static void tcpm_debugfs_exit(struct tcpm_port *port)
541 debugfs_remove(port->dentry);
546 static void tcpm_log(const struct tcpm_port *port, const char *fmt, ...) { }
547 static void tcpm_log_force(struct tcpm_port *port, const char *fmt, ...) { }
548 static void tcpm_log_source_caps(struct tcpm_port *port) { }
549 static int tcpm_debugfs_init(const struct tcpm_port *port) { return 0; }
550 static void tcpm_debugfs_exit(const struct tcpm_port *port) { }
554 static int tcpm_pd_transmit(struct tcpm_port *port,
555 enum tcpm_transmit_type type,
556 const struct pd_message *msg)
558 unsigned long timeout;
562 tcpm_log(port, "PD TX, header: %#x", le16_to_cpu(msg->header));
564 tcpm_log(port, "PD TX, type: %#x", type);
566 reinit_completion(&port->tx_complete);
567 ret = port->tcpc->pd_transmit(port->tcpc, type, msg);
571 mutex_unlock(&port->lock);
572 timeout = wait_for_completion_timeout(&port->tx_complete,
573 msecs_to_jiffies(PD_T_TCPC_TX_TIMEOUT));
574 mutex_lock(&port->lock);
578 switch (port->tx_status) {
579 case TCPC_TX_SUCCESS:
580 port->message_id = (port->message_id + 1) & PD_HEADER_ID_MASK;
582 case TCPC_TX_DISCARDED:
590 void tcpm_pd_transmit_complete(struct tcpm_port *port,
591 enum tcpm_transmit_status status)
593 tcpm_log(port, "PD TX complete, status: %u", status);
594 port->tx_status = status;
595 complete(&port->tx_complete);
597 EXPORT_SYMBOL_GPL(tcpm_pd_transmit_complete);
599 static int tcpm_mux_set(struct tcpm_port *port, enum tcpc_mux_mode mode,
600 enum tcpc_usb_switch config)
604 tcpm_log(port, "Requesting mux mode %d, config %d, polarity %d",
605 mode, config, port->polarity);
608 ret = port->tcpc->mux->set(port->tcpc->mux, mode, config,
614 static int tcpm_set_polarity(struct tcpm_port *port,
615 enum typec_cc_polarity polarity)
619 tcpm_log(port, "polarity %d", polarity);
621 ret = port->tcpc->set_polarity(port->tcpc, polarity);
625 port->polarity = polarity;
630 static int tcpm_set_vconn(struct tcpm_port *port, bool enable)
634 tcpm_log(port, "vconn:=%d", enable);
636 ret = port->tcpc->set_vconn(port->tcpc, enable);
638 port->vconn_role = enable ? TYPEC_SOURCE : TYPEC_SINK;
639 typec_set_vconn_role(port->typec_port, port->vconn_role);
645 static u32 tcpm_get_current_limit(struct tcpm_port *port)
647 enum typec_cc_status cc;
650 cc = port->polarity ? port->cc2 : port->cc1;
652 case TYPEC_CC_RP_1_5:
655 case TYPEC_CC_RP_3_0:
658 case TYPEC_CC_RP_DEF:
667 static int tcpm_set_current_limit(struct tcpm_port *port, u32 max_ma, u32 mv)
669 int ret = -EOPNOTSUPP;
671 tcpm_log(port, "Setting voltage/current limit %u mV %u mA", mv, max_ma);
673 if (port->tcpc->set_current_limit)
674 ret = port->tcpc->set_current_limit(port->tcpc, max_ma, mv);
680 * Determine RP value to set based on maximum current supported
681 * by a port if configured as source.
682 * Returns CC value to report to link partner.
684 static enum typec_cc_status tcpm_rp_cc(struct tcpm_port *port)
686 const u32 *src_pdo = port->src_pdo;
687 int nr_pdo = port->nr_src_pdo;
691 * Search for first entry with matching voltage.
692 * It should report the maximum supported current.
694 for (i = 0; i < nr_pdo; i++) {
695 const u32 pdo = src_pdo[i];
697 if (pdo_type(pdo) == PDO_TYPE_FIXED &&
698 pdo_fixed_voltage(pdo) == 5000) {
699 unsigned int curr = pdo_max_current(pdo);
702 return TYPEC_CC_RP_3_0;
703 else if (curr >= 1500)
704 return TYPEC_CC_RP_1_5;
705 return TYPEC_CC_RP_DEF;
709 return TYPEC_CC_RP_DEF;
712 static int tcpm_set_attached_state(struct tcpm_port *port, bool attached)
714 return port->tcpc->set_roles(port->tcpc, attached, port->pwr_role,
718 static int tcpm_set_roles(struct tcpm_port *port, bool attached,
719 enum typec_role role, enum typec_data_role data)
723 if (data == TYPEC_HOST)
724 ret = tcpm_mux_set(port, TYPEC_MUX_USB,
725 TCPC_USB_SWITCH_CONNECT);
727 ret = tcpm_mux_set(port, TYPEC_MUX_NONE,
728 TCPC_USB_SWITCH_DISCONNECT);
732 ret = port->tcpc->set_roles(port->tcpc, attached, role, data);
736 port->pwr_role = role;
737 port->data_role = data;
738 typec_set_data_role(port->typec_port, data);
739 typec_set_pwr_role(port->typec_port, role);
744 static int tcpm_set_pwr_role(struct tcpm_port *port, enum typec_role role)
748 ret = port->tcpc->set_roles(port->tcpc, true, role,
753 port->pwr_role = role;
754 typec_set_pwr_role(port->typec_port, role);
759 static int tcpm_pd_send_source_caps(struct tcpm_port *port)
761 struct pd_message msg;
764 memset(&msg, 0, sizeof(msg));
765 if (!port->nr_src_pdo) {
766 /* No source capabilities defined, sink only */
767 msg.header = PD_HEADER_LE(PD_CTRL_REJECT,
770 port->message_id, 0);
772 msg.header = PD_HEADER_LE(PD_DATA_SOURCE_CAP,
778 for (i = 0; i < port->nr_src_pdo; i++)
779 msg.payload[i] = cpu_to_le32(port->src_pdo[i]);
781 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
784 static int tcpm_pd_send_sink_caps(struct tcpm_port *port)
786 struct pd_message msg;
789 memset(&msg, 0, sizeof(msg));
790 if (!port->nr_snk_pdo) {
791 /* No sink capabilities defined, source only */
792 msg.header = PD_HEADER_LE(PD_CTRL_REJECT,
795 port->message_id, 0);
797 msg.header = PD_HEADER_LE(PD_DATA_SINK_CAP,
803 for (i = 0; i < port->nr_snk_pdo; i++)
804 msg.payload[i] = cpu_to_le32(port->snk_pdo[i]);
806 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
809 static void tcpm_set_state(struct tcpm_port *port, enum tcpm_state state,
810 unsigned int delay_ms)
813 tcpm_log(port, "pending state change %s -> %s @ %u ms",
814 tcpm_states[port->state], tcpm_states[state],
816 port->delayed_state = state;
817 mod_delayed_work(port->wq, &port->state_machine,
818 msecs_to_jiffies(delay_ms));
819 port->delayed_runtime = jiffies + msecs_to_jiffies(delay_ms);
820 port->delay_ms = delay_ms;
822 tcpm_log(port, "state change %s -> %s",
823 tcpm_states[port->state], tcpm_states[state]);
824 port->delayed_state = INVALID_STATE;
825 port->prev_state = port->state;
828 * Don't re-queue the state machine work item if we're currently
829 * in the state machine and we're immediately changing states.
830 * tcpm_state_machine_work() will continue running the state
833 if (!port->state_machine_running)
834 mod_delayed_work(port->wq, &port->state_machine, 0);
838 static void tcpm_set_state_cond(struct tcpm_port *port, enum tcpm_state state,
839 unsigned int delay_ms)
841 if (port->enter_state == port->state)
842 tcpm_set_state(port, state, delay_ms);
845 "skipped %sstate change %s -> %s [%u ms], context state %s",
846 delay_ms ? "delayed " : "",
847 tcpm_states[port->state], tcpm_states[state],
848 delay_ms, tcpm_states[port->enter_state]);
851 static void tcpm_queue_message(struct tcpm_port *port,
852 enum pd_msg_request message)
854 port->queued_message = message;
855 mod_delayed_work(port->wq, &port->state_machine, 0);
859 * VDM/VDO handling functions
861 static void tcpm_queue_vdm(struct tcpm_port *port, const u32 header,
862 const u32 *data, int cnt)
864 port->vdo_count = cnt + 1;
865 port->vdo_data[0] = header;
866 memcpy(&port->vdo_data[1], data, sizeof(u32) * cnt);
867 /* Set ready, vdm state machine will actually send */
868 port->vdm_retries = 0;
869 port->vdm_state = VDM_STATE_READY;
872 static void svdm_consume_identity(struct tcpm_port *port, const __le32 *payload,
875 u32 vdo = le32_to_cpu(payload[VDO_INDEX_IDH]);
876 u32 product = le32_to_cpu(payload[VDO_INDEX_PRODUCT]);
878 memset(&port->mode_data, 0, sizeof(port->mode_data));
880 #if 0 /* Not really a match */
881 switch (PD_IDH_PTYPE(vdo)) {
882 case IDH_PTYPE_UNDEF:
883 port->partner.type = TYPEC_PARTNER_NONE; /* no longer exists */
887 case IDH_PTYPE_PERIPH:
889 case IDH_PTYPE_PCABLE:
891 case IDH_PTYPE_ACABLE:
894 port->partner.type = TYPEC_PARTNER_ALTMODE;
901 port->partner_ident.id_header = vdo;
902 port->partner_ident.cert_stat = le32_to_cpu(payload[VDO_INDEX_CSTAT]);
903 port->partner_ident.product = product;
905 typec_partner_set_identity(port->partner);
907 tcpm_log(port, "Identity: %04x:%04x.%04x",
909 PD_PRODUCT_PID(product), product & 0xffff);
912 static bool svdm_consume_svids(struct tcpm_port *port, const __le32 *payload,
915 struct pd_mode_data *pmdata = &port->mode_data;
918 for (i = 1; i < cnt; i++) {
919 u32 p = le32_to_cpu(payload[i]);
922 svid = (p >> 16) & 0xffff;
926 if (pmdata->nsvids >= SVID_DISCOVERY_MAX)
929 pmdata->svids[pmdata->nsvids++] = svid;
930 tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid);
936 if (pmdata->nsvids >= SVID_DISCOVERY_MAX)
939 pmdata->svids[pmdata->nsvids++] = svid;
940 tcpm_log(port, "SVID %d: 0x%x", pmdata->nsvids, svid);
944 tcpm_log(port, "SVID_DISCOVERY_MAX(%d) too low!", SVID_DISCOVERY_MAX);
948 static void svdm_consume_modes(struct tcpm_port *port, const __le32 *payload,
951 struct pd_mode_data *pmdata = &port->mode_data;
952 struct typec_altmode_desc *paltmode;
953 struct typec_mode_desc *pmode;
956 if (pmdata->altmodes >= ARRAY_SIZE(port->partner_altmode)) {
957 /* Already logged in svdm_consume_svids() */
961 paltmode = &pmdata->altmode_desc[pmdata->altmodes];
962 memset(paltmode, 0, sizeof(*paltmode));
964 paltmode->svid = pmdata->svids[pmdata->svid_index];
966 tcpm_log(port, " Alternate mode %d: SVID 0x%04x",
967 pmdata->altmodes, paltmode->svid);
969 for (i = 1; i < cnt && paltmode->n_modes < ALTMODE_MAX_MODES; i++) {
970 pmode = &paltmode->modes[paltmode->n_modes];
971 memset(pmode, 0, sizeof(*pmode));
972 pmode->vdo = le32_to_cpu(payload[i]);
973 pmode->index = i - 1;
975 tcpm_log(port, " VDO %d: 0x%08x",
976 pmode->index, pmode->vdo);
978 port->partner_altmode[pmdata->altmodes] =
979 typec_partner_register_altmode(port->partner, paltmode);
980 if (port->partner_altmode[pmdata->altmodes] == NULL) {
982 "Failed to register alternate modes for SVID 0x%04x",
989 #define supports_modal(port) PD_IDH_MODAL_SUPP((port)->partner_ident.id_header)
991 static int tcpm_pd_svdm(struct tcpm_port *port, const __le32 *payload, int cnt,
994 u32 p0 = le32_to_cpu(payload[0]);
995 int cmd_type = PD_VDO_CMDT(p0);
996 int cmd = PD_VDO_CMD(p0);
997 struct pd_mode_data *modep;
1001 tcpm_log(port, "Rx VDM cmd 0x%x type %d cmd %d len %d",
1002 p0, cmd_type, cmd, cnt);
1004 modep = &port->mode_data;
1009 case CMD_DISCOVER_IDENT:
1011 case CMD_DISCOVER_SVID:
1013 case CMD_DISCOVER_MODES:
1015 case CMD_ENTER_MODE:
1025 response[0] = p0 | VDO_CMDT(CMDT_RSP_ACK);
1026 } else if (rlen == 0) {
1027 response[0] = p0 | VDO_CMDT(CMDT_RSP_NAK);
1030 response[0] = p0 | VDO_CMDT(CMDT_RSP_BUSY);
1035 /* silently drop message if we are not connected */
1040 case CMD_DISCOVER_IDENT:
1042 svdm_consume_identity(port, payload, cnt);
1043 response[0] = VDO(USB_SID_PD, 1, CMD_DISCOVER_SVID);
1046 case CMD_DISCOVER_SVID:
1048 if (svdm_consume_svids(port, payload, cnt)) {
1049 response[0] = VDO(USB_SID_PD, 1,
1052 } else if (modep->nsvids && supports_modal(port)) {
1053 response[0] = VDO(modep->svids[0], 1,
1054 CMD_DISCOVER_MODES);
1058 case CMD_DISCOVER_MODES:
1060 svdm_consume_modes(port, payload, cnt);
1061 modep->svid_index++;
1062 if (modep->svid_index < modep->nsvids) {
1063 svid = modep->svids[modep->svid_index];
1064 response[0] = VDO(svid, 1, CMD_DISCOVER_MODES);
1068 response[0] = pd_dfp_enter_mode(port, 0, 0);
1074 case CMD_ENTER_MODE:
1087 static void tcpm_handle_vdm_request(struct tcpm_port *port,
1088 const __le32 *payload, int cnt)
1091 u32 response[8] = { };
1092 u32 p0 = le32_to_cpu(payload[0]);
1094 if (port->vdm_state == VDM_STATE_BUSY) {
1095 /* If UFP responded busy retry after timeout */
1096 if (PD_VDO_CMDT(p0) == CMDT_RSP_BUSY) {
1097 port->vdm_state = VDM_STATE_WAIT_RSP_BUSY;
1098 port->vdo_retry = (p0 & ~VDO_CMDT_MASK) |
1100 mod_delayed_work(port->wq, &port->vdm_state_machine,
1101 msecs_to_jiffies(PD_T_VDM_BUSY));
1104 port->vdm_state = VDM_STATE_DONE;
1107 if (PD_VDO_SVDM(p0))
1108 rlen = tcpm_pd_svdm(port, payload, cnt, response);
1111 rlen = tcpm_pd_custom_vdm(port, cnt, payload, response);
1115 tcpm_queue_vdm(port, response[0], &response[1], rlen - 1);
1116 mod_delayed_work(port->wq, &port->vdm_state_machine, 0);
1120 static void tcpm_send_vdm(struct tcpm_port *port, u32 vid, int cmd,
1121 const u32 *data, int count)
1125 if (WARN_ON(count > VDO_MAX_SIZE - 1))
1126 count = VDO_MAX_SIZE - 1;
1128 /* set VDM header with VID & CMD */
1129 header = VDO(vid, ((vid & USB_SID_PD) == USB_SID_PD) ?
1130 1 : (PD_VDO_CMD(cmd) <= CMD_ATTENTION), cmd);
1131 tcpm_queue_vdm(port, header, data, count);
1133 mod_delayed_work(port->wq, &port->vdm_state_machine, 0);
1136 static unsigned int vdm_ready_timeout(u32 vdm_hdr)
1138 unsigned int timeout;
1139 int cmd = PD_VDO_CMD(vdm_hdr);
1141 /* its not a structured VDM command */
1142 if (!PD_VDO_SVDM(vdm_hdr))
1143 return PD_T_VDM_UNSTRUCTURED;
1145 switch (PD_VDO_CMDT(vdm_hdr)) {
1147 if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE)
1148 timeout = PD_T_VDM_WAIT_MODE_E;
1150 timeout = PD_T_VDM_SNDR_RSP;
1153 if (cmd == CMD_ENTER_MODE || cmd == CMD_EXIT_MODE)
1154 timeout = PD_T_VDM_E_MODE;
1156 timeout = PD_T_VDM_RCVR_RSP;
1162 static void vdm_run_state_machine(struct tcpm_port *port)
1164 struct pd_message msg;
1167 switch (port->vdm_state) {
1168 case VDM_STATE_READY:
1169 /* Only transmit VDM if attached */
1170 if (!port->attached) {
1171 port->vdm_state = VDM_STATE_ERR_BUSY;
1176 * if there's traffic or we're not in PDO ready state don't send
1179 if (port->state != SRC_READY && port->state != SNK_READY)
1182 /* Prepare and send VDM */
1183 memset(&msg, 0, sizeof(msg));
1184 msg.header = PD_HEADER_LE(PD_DATA_VENDOR_DEF,
1187 port->message_id, port->vdo_count);
1188 for (i = 0; i < port->vdo_count; i++)
1189 msg.payload[i] = cpu_to_le32(port->vdo_data[i]);
1190 res = tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
1192 port->vdm_state = VDM_STATE_ERR_SEND;
1194 unsigned long timeout;
1196 port->vdm_retries = 0;
1197 port->vdm_state = VDM_STATE_BUSY;
1198 timeout = vdm_ready_timeout(port->vdo_data[0]);
1199 mod_delayed_work(port->wq, &port->vdm_state_machine,
1203 case VDM_STATE_WAIT_RSP_BUSY:
1204 port->vdo_data[0] = port->vdo_retry;
1205 port->vdo_count = 1;
1206 port->vdm_state = VDM_STATE_READY;
1208 case VDM_STATE_BUSY:
1209 port->vdm_state = VDM_STATE_ERR_TMOUT;
1211 case VDM_STATE_ERR_SEND:
1213 * A partner which does not support USB PD will not reply,
1214 * so this is not a fatal error. At the same time, some
1215 * devices may not return GoodCRC under some circumstances,
1216 * so we need to retry.
1218 if (port->vdm_retries < 3) {
1219 tcpm_log(port, "VDM Tx error, retry");
1220 port->vdm_retries++;
1221 port->vdm_state = VDM_STATE_READY;
1229 static void vdm_state_machine_work(struct work_struct *work)
1231 struct tcpm_port *port = container_of(work, struct tcpm_port,
1232 vdm_state_machine.work);
1233 enum vdm_states prev_state;
1235 mutex_lock(&port->lock);
1238 * Continue running as long as the port is not busy and there was
1242 prev_state = port->vdm_state;
1243 vdm_run_state_machine(port);
1244 } while (port->vdm_state != prev_state &&
1245 port->vdm_state != VDM_STATE_BUSY);
1247 mutex_unlock(&port->lock);
1251 * PD (data, control) command handling functions
1253 static void tcpm_pd_data_request(struct tcpm_port *port,
1254 const struct pd_message *msg)
1256 enum pd_data_msg_type type = pd_header_type_le(msg->header);
1257 unsigned int cnt = pd_header_cnt_le(msg->header);
1261 case PD_DATA_SOURCE_CAP:
1262 if (port->pwr_role != TYPEC_SINK)
1265 for (i = 0; i < cnt; i++)
1266 port->source_caps[i] = le32_to_cpu(msg->payload[i]);
1268 port->nr_source_caps = cnt;
1270 tcpm_log_source_caps(port);
1273 * This message may be received even if VBUS is not
1274 * present. This is quite unexpected; see USB PD
1275 * specification, sections 8.3.3.6.3.1 and 8.3.3.6.3.2.
1276 * However, at the same time, we must be ready to
1277 * receive this message and respond to it 15ms after
1278 * receiving PS_RDY during power swap operations, no matter
1279 * if VBUS is available or not (USB PD specification,
1281 * So we need to accept the message either way,
1282 * but be prepared to keep waiting for VBUS after it was
1285 tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
1287 case PD_DATA_REQUEST:
1288 if (port->pwr_role != TYPEC_SOURCE ||
1290 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1293 port->sink_request = le32_to_cpu(msg->payload[0]);
1294 tcpm_set_state(port, SRC_NEGOTIATE_CAPABILITIES, 0);
1296 case PD_DATA_SINK_CAP:
1297 /* We don't do anything with this at the moment... */
1298 for (i = 0; i < cnt; i++)
1299 port->sink_caps[i] = le32_to_cpu(msg->payload[i]);
1300 port->nr_sink_caps = cnt;
1302 case PD_DATA_VENDOR_DEF:
1303 tcpm_handle_vdm_request(port, msg->payload, cnt);
1306 if (port->state == SRC_READY || port->state == SNK_READY) {
1307 port->bist_request = le32_to_cpu(msg->payload[0]);
1308 tcpm_set_state(port, BIST_RX, 0);
1312 tcpm_log(port, "Unhandled data message type %#x", type);
1317 static void tcpm_pd_ctrl_request(struct tcpm_port *port,
1318 const struct pd_message *msg)
1320 enum pd_ctrl_msg_type type = pd_header_type_le(msg->header);
1321 enum tcpm_state next_state;
1324 case PD_CTRL_GOOD_CRC:
1327 case PD_CTRL_GET_SOURCE_CAP:
1328 switch (port->state) {
1331 tcpm_queue_message(port, PD_MSG_DATA_SOURCE_CAP);
1334 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1338 case PD_CTRL_GET_SINK_CAP:
1339 switch (port->state) {
1342 tcpm_queue_message(port, PD_MSG_DATA_SINK_CAP);
1345 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1349 case PD_CTRL_GOTO_MIN:
1351 case PD_CTRL_PS_RDY:
1352 switch (port->state) {
1353 case SNK_TRANSITION_SINK:
1354 if (port->vbus_present) {
1355 tcpm_set_current_limit(port,
1356 port->current_limit,
1357 port->supply_voltage);
1358 tcpm_set_state(port, SNK_READY, 0);
1361 * Seen after power swap. Keep waiting for VBUS
1362 * in a transitional state.
1364 tcpm_set_state(port,
1365 SNK_TRANSITION_SINK_VBUS, 0);
1368 case PR_SWAP_SRC_SNK_SOURCE_OFF:
1369 tcpm_set_state(port, PR_SWAP_SRC_SNK_SINK_ON, 0);
1371 case PR_SWAP_SNK_SRC_SINK_OFF:
1372 tcpm_set_state(port, PR_SWAP_SNK_SRC_SOURCE_ON, 0);
1374 case VCONN_SWAP_WAIT_FOR_VCONN:
1375 tcpm_set_state(port, VCONN_SWAP_TURN_OFF_VCONN, 0);
1381 case PD_CTRL_REJECT:
1383 switch (port->state) {
1384 case SNK_NEGOTIATE_CAPABILITIES:
1385 /* USB PD specification, Figure 8-43 */
1386 if (port->explicit_contract)
1387 next_state = SNK_READY;
1389 next_state = SNK_WAIT_CAPABILITIES;
1390 tcpm_set_state(port, next_state, 0);
1393 port->swap_status = (type == PD_CTRL_WAIT ?
1394 -EAGAIN : -EOPNOTSUPP);
1395 tcpm_set_state(port, DR_SWAP_CANCEL, 0);
1398 port->swap_status = (type == PD_CTRL_WAIT ?
1399 -EAGAIN : -EOPNOTSUPP);
1400 tcpm_set_state(port, PR_SWAP_CANCEL, 0);
1402 case VCONN_SWAP_SEND:
1403 port->swap_status = (type == PD_CTRL_WAIT ?
1404 -EAGAIN : -EOPNOTSUPP);
1405 tcpm_set_state(port, VCONN_SWAP_CANCEL, 0);
1411 case PD_CTRL_ACCEPT:
1412 switch (port->state) {
1413 case SNK_NEGOTIATE_CAPABILITIES:
1414 tcpm_set_state(port, SNK_TRANSITION_SINK, 0);
1416 case SOFT_RESET_SEND:
1417 port->message_id = 0;
1418 if (port->pwr_role == TYPEC_SOURCE)
1419 next_state = SRC_SEND_CAPABILITIES;
1421 next_state = SNK_WAIT_CAPABILITIES;
1422 tcpm_set_state(port, next_state, 0);
1425 tcpm_set_state(port, DR_SWAP_CHANGE_DR, 0);
1428 tcpm_set_state(port, PR_SWAP_START, 0);
1430 case VCONN_SWAP_SEND:
1431 tcpm_set_state(port, VCONN_SWAP_START, 0);
1437 case PD_CTRL_SOFT_RESET:
1438 tcpm_set_state(port, SOFT_RESET, 0);
1440 case PD_CTRL_DR_SWAP:
1441 if (port->typec_caps.type != TYPEC_PORT_DRP) {
1442 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1447 * 6.3.9: If an alternate mode is active, a request to swap
1448 * alternate modes shall trigger a port reset.
1450 switch (port->state) {
1453 tcpm_set_state(port, DR_SWAP_ACCEPT, 0);
1456 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
1460 case PD_CTRL_PR_SWAP:
1461 if (port->typec_caps.type != TYPEC_PORT_DRP) {
1462 tcpm_queue_message(port, PD_MSG_CTRL_REJECT);
1465 switch (port->state) {
1468 tcpm_set_state(port, PR_SWAP_ACCEPT, 0);
1471 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
1475 case PD_CTRL_VCONN_SWAP:
1476 switch (port->state) {
1479 tcpm_set_state(port, VCONN_SWAP_ACCEPT, 0);
1482 tcpm_queue_message(port, PD_MSG_CTRL_WAIT);
1487 tcpm_log(port, "Unhandled ctrl message type %#x", type);
1492 static void tcpm_pd_rx_handler(struct work_struct *work)
1494 struct pd_rx_event *event = container_of(work,
1495 struct pd_rx_event, work);
1496 const struct pd_message *msg = &event->msg;
1497 unsigned int cnt = pd_header_cnt_le(msg->header);
1498 struct tcpm_port *port = event->port;
1500 mutex_lock(&port->lock);
1502 tcpm_log(port, "PD RX, header: %#x [%d]", le16_to_cpu(msg->header),
1505 if (port->attached) {
1507 * If both ends believe to be DFP/host, we have a data role
1510 if (!!(le16_to_cpu(msg->header) & PD_HEADER_DATA_ROLE) ==
1511 (port->data_role == TYPEC_HOST)) {
1513 "Data role mismatch, initiating error recovery");
1514 tcpm_set_state(port, ERROR_RECOVERY, 0);
1517 tcpm_pd_data_request(port, msg);
1519 tcpm_pd_ctrl_request(port, msg);
1523 mutex_unlock(&port->lock);
1527 void tcpm_pd_receive(struct tcpm_port *port, const struct pd_message *msg)
1529 struct pd_rx_event *event;
1531 event = kzalloc(sizeof(*event), GFP_ATOMIC);
1535 INIT_WORK(&event->work, tcpm_pd_rx_handler);
1537 memcpy(&event->msg, msg, sizeof(*msg));
1538 queue_work(port->wq, &event->work);
1540 EXPORT_SYMBOL_GPL(tcpm_pd_receive);
1542 static int tcpm_pd_send_control(struct tcpm_port *port,
1543 enum pd_ctrl_msg_type type)
1545 struct pd_message msg;
1547 memset(&msg, 0, sizeof(msg));
1548 msg.header = PD_HEADER_LE(type, port->pwr_role,
1550 port->message_id, 0);
1552 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
1556 * Send queued message without affecting state.
1557 * Return true if state machine should go back to sleep,
1560 static bool tcpm_send_queued_message(struct tcpm_port *port)
1562 enum pd_msg_request queued_message;
1565 queued_message = port->queued_message;
1566 port->queued_message = PD_MSG_NONE;
1568 switch (queued_message) {
1569 case PD_MSG_CTRL_WAIT:
1570 tcpm_pd_send_control(port, PD_CTRL_WAIT);
1572 case PD_MSG_CTRL_REJECT:
1573 tcpm_pd_send_control(port, PD_CTRL_REJECT);
1575 case PD_MSG_DATA_SINK_CAP:
1576 tcpm_pd_send_sink_caps(port);
1578 case PD_MSG_DATA_SOURCE_CAP:
1579 tcpm_pd_send_source_caps(port);
1584 } while (port->queued_message != PD_MSG_NONE);
1586 if (port->delayed_state != INVALID_STATE) {
1587 if (time_is_after_jiffies(port->delayed_runtime)) {
1588 mod_delayed_work(port->wq, &port->state_machine,
1589 port->delayed_runtime - jiffies);
1592 port->delayed_state = INVALID_STATE;
1597 static int tcpm_pd_check_request(struct tcpm_port *port)
1599 u32 pdo, rdo = port->sink_request;
1600 unsigned int max, op, pdo_max, index;
1601 enum pd_pdo_type type;
1603 index = rdo_index(rdo);
1604 if (!index || index > port->nr_src_pdo)
1607 pdo = port->src_pdo[index - 1];
1608 type = pdo_type(pdo);
1610 case PDO_TYPE_FIXED:
1612 max = rdo_max_current(rdo);
1613 op = rdo_op_current(rdo);
1614 pdo_max = pdo_max_current(pdo);
1618 if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH))
1621 if (type == PDO_TYPE_FIXED)
1623 "Requested %u mV, %u mA for %u / %u mA",
1624 pdo_fixed_voltage(pdo), pdo_max, op, max);
1627 "Requested %u -> %u mV, %u mA for %u / %u mA",
1628 pdo_min_voltage(pdo), pdo_max_voltage(pdo),
1632 max = rdo_max_power(rdo);
1633 op = rdo_op_power(rdo);
1634 pdo_max = pdo_max_power(pdo);
1638 if (max > pdo_max && !(rdo & RDO_CAP_MISMATCH))
1641 "Requested %u -> %u mV, %u mW for %u / %u mW",
1642 pdo_min_voltage(pdo), pdo_max_voltage(pdo),
1649 port->op_vsafe5v = index == 1;
1654 static int tcpm_pd_select_pdo(struct tcpm_port *port)
1656 unsigned int i, max_mw = 0, max_mv = 0;
1660 * Select the source PDO providing the most power while staying within
1661 * the board's voltage limits. Prefer PDO providing exp
1663 for (i = 0; i < port->nr_source_caps; i++) {
1664 u32 pdo = port->source_caps[i];
1665 enum pd_pdo_type type = pdo_type(pdo);
1666 unsigned int mv, ma, mw;
1668 if (type == PDO_TYPE_FIXED)
1669 mv = pdo_fixed_voltage(pdo);
1671 mv = pdo_min_voltage(pdo);
1673 if (type == PDO_TYPE_BATT) {
1674 mw = pdo_max_power(pdo);
1676 ma = min(pdo_max_current(pdo),
1678 mw = ma * mv / 1000;
1681 /* Perfer higher voltages if available */
1682 if ((mw > max_mw || (mw == max_mw && mv > max_mv)) &&
1683 mv <= port->max_snk_mv) {
1693 static int tcpm_pd_build_request(struct tcpm_port *port, u32 *rdo)
1695 unsigned int mv, ma, mw, flags;
1696 unsigned int max_ma, max_mw;
1697 enum pd_pdo_type type;
1701 index = tcpm_pd_select_pdo(port);
1704 pdo = port->source_caps[index];
1705 type = pdo_type(pdo);
1707 if (type == PDO_TYPE_FIXED)
1708 mv = pdo_fixed_voltage(pdo);
1710 mv = pdo_min_voltage(pdo);
1712 /* Select maximum available current within the board's power limit */
1713 if (type == PDO_TYPE_BATT) {
1714 mw = pdo_max_power(pdo);
1715 ma = 1000 * min(mw, port->max_snk_mw) / mv;
1717 ma = min(pdo_max_current(pdo),
1718 1000 * port->max_snk_mw / mv);
1720 ma = min(ma, port->max_snk_ma);
1722 /* XXX: Any other flags need to be set? */
1725 /* Set mismatch bit if offered power is less than operating power */
1726 mw = ma * mv / 1000;
1729 if (mw < port->operating_snk_mw) {
1730 flags |= RDO_CAP_MISMATCH;
1731 max_mw = port->operating_snk_mw;
1732 max_ma = max_mw * 1000 / mv;
1735 tcpm_log(port, "cc=%d cc1=%d cc2=%d vbus=%d vconn=%s polarity=%d",
1736 port->cc_req, port->cc1, port->cc2, port->vbus_source,
1737 port->vconn_role == TYPEC_SOURCE ? "source" : "sink",
1740 if (type == PDO_TYPE_BATT) {
1741 *rdo = RDO_BATT(index + 1, mw, max_mw, flags);
1743 tcpm_log(port, "Requesting PDO %d: %u mV, %u mW%s",
1745 flags & RDO_CAP_MISMATCH ? " [mismatch]" : "");
1747 *rdo = RDO_FIXED(index + 1, ma, max_ma, flags);
1749 tcpm_log(port, "Requesting PDO %d: %u mV, %u mA%s",
1751 flags & RDO_CAP_MISMATCH ? " [mismatch]" : "");
1754 port->current_limit = ma;
1755 port->supply_voltage = mv;
1760 static int tcpm_pd_send_request(struct tcpm_port *port)
1762 struct pd_message msg;
1766 ret = tcpm_pd_build_request(port, &rdo);
1770 memset(&msg, 0, sizeof(msg));
1771 msg.header = PD_HEADER_LE(PD_DATA_REQUEST,
1774 port->message_id, 1);
1775 msg.payload[0] = cpu_to_le32(rdo);
1777 return tcpm_pd_transmit(port, TCPC_TX_SOP, &msg);
1780 static int tcpm_set_vbus(struct tcpm_port *port, bool enable)
1784 if (enable && port->vbus_charge)
1787 tcpm_log(port, "vbus:=%d charge=%d", enable, port->vbus_charge);
1789 ret = port->tcpc->set_vbus(port->tcpc, enable, port->vbus_charge);
1793 port->vbus_source = enable;
1797 static int tcpm_set_charge(struct tcpm_port *port, bool charge)
1801 if (charge && port->vbus_source)
1804 if (charge != port->vbus_charge) {
1805 tcpm_log(port, "vbus=%d charge:=%d", port->vbus_source, charge);
1806 ret = port->tcpc->set_vbus(port->tcpc, port->vbus_source,
1811 port->vbus_charge = charge;
1815 static bool tcpm_start_drp_toggling(struct tcpm_port *port)
1819 if (port->tcpc->start_drp_toggling &&
1820 port->typec_caps.type == TYPEC_PORT_DRP) {
1821 tcpm_log_force(port, "Start DRP toggling");
1822 ret = port->tcpc->start_drp_toggling(port->tcpc,
1831 static void tcpm_set_cc(struct tcpm_port *port, enum typec_cc_status cc)
1833 tcpm_log(port, "cc:=%d", cc);
1835 port->tcpc->set_cc(port->tcpc, cc);
1838 static int tcpm_init_vbus(struct tcpm_port *port)
1842 ret = port->tcpc->set_vbus(port->tcpc, false, false);
1843 port->vbus_source = false;
1844 port->vbus_charge = false;
1848 static int tcpm_init_vconn(struct tcpm_port *port)
1852 ret = port->tcpc->set_vconn(port->tcpc, false);
1853 port->vconn_role = TYPEC_SINK;
1857 static void tcpm_typec_connect(struct tcpm_port *port)
1859 if (!port->connected) {
1860 /* Make sure we don't report stale identity information */
1861 memset(&port->partner_ident, 0, sizeof(port->partner_ident));
1862 port->partner_desc.usb_pd = port->pd_capable;
1863 if (tcpm_port_is_debug(port))
1864 port->partner_desc.accessory = TYPEC_ACCESSORY_DEBUG;
1865 else if (tcpm_port_is_audio(port))
1866 port->partner_desc.accessory = TYPEC_ACCESSORY_AUDIO;
1868 port->partner_desc.accessory = TYPEC_ACCESSORY_NONE;
1869 port->partner = typec_register_partner(port->typec_port,
1870 &port->partner_desc);
1871 port->connected = true;
1875 static int tcpm_src_attach(struct tcpm_port *port)
1877 enum typec_cc_polarity polarity =
1878 port->cc2 == TYPEC_CC_RD ? TYPEC_POLARITY_CC2
1879 : TYPEC_POLARITY_CC1;
1885 ret = tcpm_set_polarity(port, polarity);
1889 ret = tcpm_set_roles(port, true, TYPEC_SOURCE, TYPEC_HOST);
1893 ret = port->tcpc->set_pd_rx(port->tcpc, true);
1895 goto out_disable_mux;
1898 * USB Type-C specification, version 1.2,
1899 * chapter 4.5.2.2.8.1 (Attached.SRC Requirements)
1900 * Enable VCONN only if the non-RD port is set to RA.
1902 if ((polarity == TYPEC_POLARITY_CC1 && port->cc2 == TYPEC_CC_RA) ||
1903 (polarity == TYPEC_POLARITY_CC2 && port->cc1 == TYPEC_CC_RA)) {
1904 ret = tcpm_set_vconn(port, true);
1906 goto out_disable_pd;
1909 ret = tcpm_set_vbus(port, true);
1911 goto out_disable_vconn;
1913 port->pd_capable = false;
1915 port->partner = NULL;
1917 port->attached = true;
1918 port->send_discover = true;
1923 tcpm_set_vconn(port, false);
1925 port->tcpc->set_pd_rx(port->tcpc, false);
1927 tcpm_mux_set(port, TYPEC_MUX_NONE, TCPC_USB_SWITCH_DISCONNECT);
1931 static void tcpm_typec_disconnect(struct tcpm_port *port)
1933 if (port->connected) {
1934 typec_unregister_partner(port->partner);
1935 port->partner = NULL;
1936 port->connected = false;
1940 static void tcpm_unregister_altmodes(struct tcpm_port *port)
1942 struct pd_mode_data *modep = &port->mode_data;
1945 for (i = 0; i < modep->altmodes; i++) {
1946 typec_unregister_altmode(port->partner_altmode[i]);
1947 port->partner_altmode[i] = NULL;
1950 memset(modep, 0, sizeof(*modep));
1953 static void tcpm_reset_port(struct tcpm_port *port)
1955 tcpm_unregister_altmodes(port);
1956 tcpm_typec_disconnect(port);
1957 port->attached = false;
1958 port->pd_capable = false;
1960 port->tcpc->set_pd_rx(port->tcpc, false);
1961 tcpm_init_vbus(port); /* also disables charging */
1962 tcpm_init_vconn(port);
1963 tcpm_set_current_limit(port, 0, 0);
1964 tcpm_set_polarity(port, TYPEC_POLARITY_CC1);
1965 tcpm_set_attached_state(port, false);
1966 port->try_src_count = 0;
1967 port->try_snk_count = 0;
1970 static void tcpm_detach(struct tcpm_port *port)
1972 if (!port->attached)
1975 if (tcpm_port_is_disconnected(port))
1976 port->hard_reset_count = 0;
1978 tcpm_reset_port(port);
1981 static void tcpm_src_detach(struct tcpm_port *port)
1986 static int tcpm_snk_attach(struct tcpm_port *port)
1993 ret = tcpm_set_polarity(port, port->cc2 != TYPEC_CC_OPEN ?
1994 TYPEC_POLARITY_CC2 : TYPEC_POLARITY_CC1);
1998 ret = tcpm_set_roles(port, true, TYPEC_SINK, TYPEC_DEVICE);
2002 port->pd_capable = false;
2004 port->partner = NULL;
2006 port->attached = true;
2007 port->send_discover = true;
2012 static void tcpm_snk_detach(struct tcpm_port *port)
2016 /* XXX: (Dis)connect SuperSpeed mux? */
2019 static int tcpm_acc_attach(struct tcpm_port *port)
2026 ret = tcpm_set_roles(port, true, TYPEC_SOURCE, TYPEC_HOST);
2030 port->partner = NULL;
2032 tcpm_typec_connect(port);
2034 port->attached = true;
2039 static void tcpm_acc_detach(struct tcpm_port *port)
2044 static inline enum tcpm_state hard_reset_state(struct tcpm_port *port)
2046 if (port->hard_reset_count < PD_N_HARD_RESET_COUNT)
2047 return HARD_RESET_SEND;
2048 if (port->pd_capable)
2049 return ERROR_RECOVERY;
2050 if (port->pwr_role == TYPEC_SOURCE)
2051 return SRC_UNATTACHED;
2052 if (port->state == SNK_WAIT_CAPABILITIES)
2054 return SNK_UNATTACHED;
2057 static inline enum tcpm_state ready_state(struct tcpm_port *port)
2059 if (port->pwr_role == TYPEC_SOURCE)
2065 static inline enum tcpm_state unattached_state(struct tcpm_port *port)
2067 if (port->pwr_role == TYPEC_SOURCE)
2068 return SRC_UNATTACHED;
2070 return SNK_UNATTACHED;
2073 static void tcpm_check_send_discover(struct tcpm_port *port)
2075 if (port->data_role == TYPEC_HOST && port->send_discover &&
2077 tcpm_send_vdm(port, USB_SID_PD, CMD_DISCOVER_IDENT, NULL, 0);
2078 port->send_discover = false;
2082 static void tcpm_swap_complete(struct tcpm_port *port, int result)
2084 if (port->swap_pending) {
2085 port->swap_status = result;
2086 port->swap_pending = false;
2087 complete(&port->swap_complete);
2091 static void run_state_machine(struct tcpm_port *port)
2095 port->enter_state = port->state;
2096 switch (port->state) {
2100 case SRC_UNATTACHED:
2101 tcpm_swap_complete(port, -ENOTCONN);
2102 tcpm_src_detach(port);
2103 if (tcpm_start_drp_toggling(port)) {
2104 tcpm_set_state(port, DRP_TOGGLING, 0);
2107 tcpm_set_cc(port, tcpm_rp_cc(port));
2108 if (port->typec_caps.type == TYPEC_PORT_DRP)
2109 tcpm_set_state(port, SNK_UNATTACHED, PD_T_DRP_SNK);
2111 case SRC_ATTACH_WAIT:
2112 if (tcpm_port_is_debug(port))
2113 tcpm_set_state(port, DEBUG_ACC_ATTACHED,
2115 else if (tcpm_port_is_audio(port))
2116 tcpm_set_state(port, AUDIO_ACC_ATTACHED,
2118 else if (tcpm_port_is_source(port))
2119 tcpm_set_state(port,
2120 tcpm_try_snk(port) ? SNK_TRY
2126 port->try_snk_count++;
2129 * - Do not drive vconn or vbus
2130 * - Terminate CC pins (both) to Rd
2132 * - Wait for tDRPTry (PD_T_DRP_TRY).
2133 * Until then, ignore any state changes.
2135 tcpm_set_cc(port, TYPEC_CC_RD);
2136 tcpm_set_state(port, SNK_TRY_WAIT, PD_T_DRP_TRY);
2139 if (port->vbus_present && tcpm_port_is_sink(port)) {
2140 tcpm_set_state(port, SNK_ATTACHED, 0);
2143 if (!tcpm_port_is_sink(port)) {
2144 tcpm_set_state(port, SRC_TRYWAIT,
2148 /* No vbus, cc state is sink or open */
2149 tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED, PD_T_DRP_TRYWAIT);
2152 tcpm_set_cc(port, tcpm_rp_cc(port));
2153 if (!port->vbus_present && tcpm_port_is_source(port))
2154 tcpm_set_state(port, SRC_ATTACHED, PD_T_CC_DEBOUNCE);
2156 tcpm_set_state(port, SRC_TRYWAIT_UNATTACHED,
2159 case SRC_TRYWAIT_UNATTACHED:
2160 tcpm_set_state(port, SNK_UNATTACHED, 0);
2164 ret = tcpm_src_attach(port);
2165 tcpm_set_state(port, SRC_UNATTACHED,
2166 ret < 0 ? 0 : PD_T_PS_SOURCE_ON);
2169 typec_set_pwr_opmode(port->typec_port, TYPEC_PWR_MODE_USB);
2170 port->pwr_opmode = TYPEC_PWR_MODE_USB;
2171 port->caps_count = 0;
2172 port->message_id = 0;
2173 port->explicit_contract = false;
2174 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
2176 case SRC_SEND_CAPABILITIES:
2178 if (port->caps_count > PD_N_CAPS_COUNT) {
2179 tcpm_set_state(port, SRC_READY, 0);
2182 ret = tcpm_pd_send_source_caps(port);
2184 tcpm_set_state(port, SRC_SEND_CAPABILITIES,
2185 PD_T_SEND_SOURCE_CAP);
2188 * Per standard, we should clear the reset counter here.
2189 * However, that can result in state machine hang-ups.
2190 * Reset it only in READY state to improve stability.
2192 /* port->hard_reset_count = 0; */
2193 port->caps_count = 0;
2194 port->pd_capable = true;
2195 tcpm_set_state_cond(port, hard_reset_state(port),
2196 PD_T_SEND_SOURCE_CAP);
2199 case SRC_NEGOTIATE_CAPABILITIES:
2200 ret = tcpm_pd_check_request(port);
2202 tcpm_pd_send_control(port, PD_CTRL_REJECT);
2203 if (!port->explicit_contract) {
2204 tcpm_set_state(port,
2205 SRC_WAIT_NEW_CAPABILITIES, 0);
2207 tcpm_set_state(port, SRC_READY, 0);
2210 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
2211 tcpm_set_state(port, SRC_TRANSITION_SUPPLY,
2212 PD_T_SRC_TRANSITION);
2215 case SRC_TRANSITION_SUPPLY:
2216 /* XXX: regulator_set_voltage(vbus, ...) */
2217 tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
2218 port->explicit_contract = true;
2219 typec_set_pwr_opmode(port->typec_port, TYPEC_PWR_MODE_PD);
2220 port->pwr_opmode = TYPEC_PWR_MODE_PD;
2221 tcpm_set_state_cond(port, SRC_READY, 0);
2225 port->hard_reset_count = 0;
2227 port->try_src_count = 0;
2229 tcpm_typec_connect(port);
2231 tcpm_check_send_discover(port);
2234 * Sending ping messages is not necessary if
2235 * - the source operates at vSafe5V
2237 * - The system is not operating in PD mode
2239 * - Both partners are connected using a Type-C connector
2240 * XXX How do we know that ?
2242 if (port->pwr_opmode == TYPEC_PWR_MODE_PD &&
2243 !port->op_vsafe5v) {
2244 tcpm_pd_send_control(port, PD_CTRL_PING);
2245 tcpm_set_state_cond(port, SRC_READY,
2246 PD_T_SOURCE_ACTIVITY);
2249 case SRC_WAIT_NEW_CAPABILITIES:
2250 /* Nothing to do... */
2254 case SNK_UNATTACHED:
2255 tcpm_swap_complete(port, -ENOTCONN);
2256 tcpm_snk_detach(port);
2257 if (tcpm_start_drp_toggling(port)) {
2258 tcpm_set_state(port, DRP_TOGGLING, 0);
2261 tcpm_set_cc(port, TYPEC_CC_RD);
2262 if (port->typec_caps.type == TYPEC_PORT_DRP)
2263 tcpm_set_state(port, SRC_UNATTACHED, PD_T_DRP_SRC);
2265 case SNK_ATTACH_WAIT:
2266 if ((port->cc1 == TYPEC_CC_OPEN &&
2267 port->cc2 != TYPEC_CC_OPEN) ||
2268 (port->cc1 != TYPEC_CC_OPEN &&
2269 port->cc2 == TYPEC_CC_OPEN))
2270 tcpm_set_state(port, SNK_DEBOUNCED,
2272 else if (tcpm_port_is_disconnected(port))
2273 tcpm_set_state(port, SNK_UNATTACHED,
2277 if (tcpm_port_is_disconnected(port))
2278 tcpm_set_state(port, SNK_UNATTACHED,
2280 else if (port->vbus_present)
2281 tcpm_set_state(port,
2282 tcpm_try_src(port) ? SRC_TRY
2286 /* Wait for VBUS, but not forever */
2287 tcpm_set_state(port, SNK_UNATTACHED, PD_T_PS_SOURCE_ON);
2291 port->try_src_count++;
2292 tcpm_set_cc(port, tcpm_rp_cc(port));
2293 tcpm_set_state(port, SNK_TRYWAIT, PD_T_DRP_TRY);
2295 case SRC_TRY_DEBOUNCE:
2296 tcpm_set_state(port, SRC_ATTACHED, PD_T_PD_DEBOUNCE);
2299 tcpm_set_cc(port, TYPEC_CC_RD);
2300 tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, PD_T_CC_DEBOUNCE);
2302 case SNK_TRYWAIT_DEBOUNCE:
2303 if (port->vbus_present) {
2304 tcpm_set_state(port, SNK_ATTACHED, 0);
2307 if (tcpm_port_is_disconnected(port)) {
2308 tcpm_set_state(port, SNK_UNATTACHED,
2312 if (tcpm_port_is_source(port))
2313 tcpm_set_state(port, SRC_ATTACHED, 0);
2314 /* XXX Are we supposed to stay in this state ? */
2316 case SNK_TRYWAIT_VBUS:
2317 tcpm_set_state(port, SNK_ATTACHED, PD_T_CC_DEBOUNCE);
2321 ret = tcpm_snk_attach(port);
2323 tcpm_set_state(port, SNK_UNATTACHED, 0);
2325 tcpm_set_state(port, SNK_STARTUP, 0);
2328 /* XXX: callback into infrastructure */
2329 typec_set_pwr_opmode(port->typec_port, TYPEC_PWR_MODE_USB);
2330 port->pwr_opmode = TYPEC_PWR_MODE_USB;
2331 port->message_id = 0;
2332 port->explicit_contract = false;
2333 tcpm_set_state(port, SNK_DISCOVERY, 0);
2336 if (port->vbus_present) {
2337 tcpm_set_current_limit(port,
2338 tcpm_get_current_limit(port),
2340 tcpm_set_charge(port, true);
2341 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
2345 * For DRP, timeouts differ. Also, handling is supposed to be
2346 * different and much more complex (dead battery detection;
2347 * see USB power delivery specification, section 8.3.3.6.1.5.1).
2349 tcpm_set_state(port, hard_reset_state(port),
2350 port->typec_caps.type == TYPEC_PORT_DRP ?
2351 PD_T_DB_DETECT : PD_T_NO_RESPONSE);
2353 case SNK_DISCOVERY_DEBOUNCE:
2354 tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE_DONE,
2357 case SNK_DISCOVERY_DEBOUNCE_DONE:
2358 if (!tcpm_port_is_disconnected(port) &&
2359 tcpm_port_is_sink(port) &&
2360 time_is_after_jiffies(port->delayed_runtime)) {
2361 tcpm_set_state(port, SNK_DISCOVERY,
2362 port->delayed_runtime - jiffies);
2365 tcpm_set_state(port, unattached_state(port), 0);
2367 case SNK_WAIT_CAPABILITIES:
2368 ret = port->tcpc->set_pd_rx(port->tcpc, true);
2370 tcpm_set_state(port, SNK_READY, 0);
2374 * If VBUS has never been low, and we time out waiting
2375 * for source cap, try a soft reset first, in case we
2376 * were already in a stable contract before this boot.
2377 * Do this only once.
2379 if (port->vbus_never_low) {
2380 port->vbus_never_low = false;
2381 tcpm_set_state(port, SOFT_RESET_SEND,
2382 PD_T_SINK_WAIT_CAP);
2384 tcpm_set_state(port, hard_reset_state(port),
2385 PD_T_SINK_WAIT_CAP);
2388 case SNK_NEGOTIATE_CAPABILITIES:
2389 port->pd_capable = true;
2390 port->hard_reset_count = 0;
2391 ret = tcpm_pd_send_request(port);
2393 /* Let the Source send capabilities again. */
2394 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
2396 tcpm_set_state_cond(port, hard_reset_state(port),
2397 PD_T_SENDER_RESPONSE);
2400 case SNK_TRANSITION_SINK:
2401 case SNK_TRANSITION_SINK_VBUS:
2402 tcpm_set_state(port, hard_reset_state(port),
2403 PD_T_PS_TRANSITION);
2406 port->try_snk_count = 0;
2407 port->explicit_contract = true;
2408 typec_set_pwr_opmode(port->typec_port, TYPEC_PWR_MODE_PD);
2409 port->pwr_opmode = TYPEC_PWR_MODE_PD;
2411 tcpm_typec_connect(port);
2413 tcpm_check_send_discover(port);
2416 /* Accessory states */
2417 case ACC_UNATTACHED:
2418 tcpm_acc_detach(port);
2419 tcpm_set_state(port, SRC_UNATTACHED, 0);
2421 case DEBUG_ACC_ATTACHED:
2422 case AUDIO_ACC_ATTACHED:
2423 ret = tcpm_acc_attach(port);
2425 tcpm_set_state(port, ACC_UNATTACHED, 0);
2427 case AUDIO_ACC_DEBOUNCE:
2428 tcpm_set_state(port, ACC_UNATTACHED, PD_T_CC_DEBOUNCE);
2431 /* Hard_Reset states */
2432 case HARD_RESET_SEND:
2433 tcpm_pd_transmit(port, TCPC_TX_HARD_RESET, NULL);
2434 tcpm_set_state(port, HARD_RESET_START, 0);
2436 case HARD_RESET_START:
2437 port->hard_reset_count++;
2438 port->tcpc->set_pd_rx(port->tcpc, false);
2439 tcpm_unregister_altmodes(port);
2440 port->send_discover = true;
2441 if (port->pwr_role == TYPEC_SOURCE)
2442 tcpm_set_state(port, SRC_HARD_RESET_VBUS_OFF,
2443 PD_T_PS_HARD_RESET);
2445 tcpm_set_state(port, SNK_HARD_RESET_SINK_OFF, 0);
2447 case SRC_HARD_RESET_VBUS_OFF:
2448 tcpm_set_vconn(port, true);
2449 tcpm_set_vbus(port, false);
2450 tcpm_set_roles(port, false, TYPEC_SOURCE, TYPEC_HOST);
2451 tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, PD_T_SRC_RECOVER);
2453 case SRC_HARD_RESET_VBUS_ON:
2454 tcpm_set_vbus(port, true);
2455 port->tcpc->set_pd_rx(port->tcpc, true);
2456 tcpm_set_attached_state(port, true);
2457 tcpm_set_state(port, SRC_UNATTACHED, PD_T_PS_SOURCE_ON);
2459 case SNK_HARD_RESET_SINK_OFF:
2460 tcpm_set_vconn(port, false);
2461 tcpm_set_charge(port, false);
2462 tcpm_set_roles(port, false, TYPEC_SINK, TYPEC_DEVICE);
2464 * VBUS may or may not toggle, depending on the adapter.
2465 * If it doesn't toggle, transition to SNK_HARD_RESET_SINK_ON
2466 * directly after timeout.
2468 tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, PD_T_SAFE_0V);
2470 case SNK_HARD_RESET_WAIT_VBUS:
2471 /* Assume we're disconnected if VBUS doesn't come back. */
2472 tcpm_set_state(port, SNK_UNATTACHED,
2473 PD_T_SRC_RECOVER_MAX + PD_T_SRC_TURN_ON);
2475 case SNK_HARD_RESET_SINK_ON:
2476 /* Note: There is no guarantee that VBUS is on in this state */
2479 * The specification suggests that dual mode ports in sink
2480 * mode should transition to state PE_SRC_Transition_to_default.
2481 * See USB power delivery specification chapter 8.3.3.6.1.3.
2482 * This would mean to to
2483 * - turn off VCONN, reset power supply
2484 * - request hardware reset
2486 * - Transition to state PE_Src_Startup
2487 * SNK only ports shall transition to state Snk_Startup
2488 * (see chapter 8.3.3.3.8).
2489 * Similar, dual-mode ports in source mode should transition
2490 * to PE_SNK_Transition_to_default.
2492 tcpm_set_attached_state(port, true);
2493 tcpm_set_state(port, SNK_STARTUP, 0);
2496 /* Soft_Reset states */
2498 port->message_id = 0;
2499 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
2500 if (port->pwr_role == TYPEC_SOURCE)
2501 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
2503 tcpm_set_state(port, SNK_WAIT_CAPABILITIES, 0);
2505 case SOFT_RESET_SEND:
2506 port->message_id = 0;
2507 if (tcpm_pd_send_control(port, PD_CTRL_SOFT_RESET))
2508 tcpm_set_state_cond(port, hard_reset_state(port), 0);
2510 tcpm_set_state_cond(port, hard_reset_state(port),
2511 PD_T_SENDER_RESPONSE);
2514 /* DR_Swap states */
2516 tcpm_pd_send_control(port, PD_CTRL_DR_SWAP);
2517 tcpm_set_state_cond(port, DR_SWAP_SEND_TIMEOUT,
2518 PD_T_SENDER_RESPONSE);
2520 case DR_SWAP_ACCEPT:
2521 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
2522 tcpm_set_state_cond(port, DR_SWAP_CHANGE_DR, 0);
2524 case DR_SWAP_SEND_TIMEOUT:
2525 tcpm_swap_complete(port, -ETIMEDOUT);
2526 tcpm_set_state(port, ready_state(port), 0);
2528 case DR_SWAP_CHANGE_DR:
2529 if (port->data_role == TYPEC_HOST) {
2530 tcpm_unregister_altmodes(port);
2531 tcpm_set_roles(port, true, port->pwr_role,
2534 tcpm_set_roles(port, true, port->pwr_role,
2536 port->send_discover = true;
2538 tcpm_swap_complete(port, 0);
2539 tcpm_set_state(port, ready_state(port), 0);
2542 /* PR_Swap states */
2543 case PR_SWAP_ACCEPT:
2544 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
2545 tcpm_set_state(port, PR_SWAP_START, 0);
2548 tcpm_pd_send_control(port, PD_CTRL_PR_SWAP);
2549 tcpm_set_state_cond(port, PR_SWAP_SEND_TIMEOUT,
2550 PD_T_SENDER_RESPONSE);
2552 case PR_SWAP_SEND_TIMEOUT:
2553 tcpm_swap_complete(port, -ETIMEDOUT);
2554 tcpm_set_state(port, ready_state(port), 0);
2557 if (port->pwr_role == TYPEC_SOURCE)
2558 tcpm_set_state(port, PR_SWAP_SRC_SNK_TRANSITION_OFF,
2559 PD_T_SRC_TRANSITION);
2561 tcpm_set_state(port, PR_SWAP_SNK_SRC_SINK_OFF, 0);
2563 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
2564 tcpm_set_vbus(port, false);
2565 port->explicit_contract = false;
2566 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF,
2567 PD_T_PS_SOURCE_OFF);
2569 case PR_SWAP_SRC_SNK_SOURCE_OFF:
2570 tcpm_set_cc(port, TYPEC_CC_RD);
2571 if (tcpm_pd_send_control(port, PD_CTRL_PS_RDY)) {
2572 tcpm_set_state(port, ERROR_RECOVERY, 0);
2575 tcpm_set_state_cond(port, SNK_UNATTACHED, PD_T_PS_SOURCE_ON);
2577 case PR_SWAP_SRC_SNK_SINK_ON:
2578 tcpm_set_pwr_role(port, TYPEC_SINK);
2579 tcpm_swap_complete(port, 0);
2580 tcpm_set_state(port, SNK_STARTUP, 0);
2582 case PR_SWAP_SNK_SRC_SINK_OFF:
2583 tcpm_set_charge(port, false);
2584 tcpm_set_state(port, hard_reset_state(port),
2585 PD_T_PS_SOURCE_OFF);
2587 case PR_SWAP_SNK_SRC_SOURCE_ON:
2588 tcpm_set_cc(port, tcpm_rp_cc(port));
2589 tcpm_set_vbus(port, true);
2590 tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
2591 tcpm_set_pwr_role(port, TYPEC_SOURCE);
2592 tcpm_swap_complete(port, 0);
2593 tcpm_set_state(port, SRC_STARTUP, 0);
2596 case VCONN_SWAP_ACCEPT:
2597 tcpm_pd_send_control(port, PD_CTRL_ACCEPT);
2598 tcpm_set_state(port, VCONN_SWAP_START, 0);
2600 case VCONN_SWAP_SEND:
2601 tcpm_pd_send_control(port, PD_CTRL_VCONN_SWAP);
2602 tcpm_set_state(port, VCONN_SWAP_SEND_TIMEOUT,
2603 PD_T_SENDER_RESPONSE);
2605 case VCONN_SWAP_SEND_TIMEOUT:
2606 tcpm_swap_complete(port, -ETIMEDOUT);
2607 tcpm_set_state(port, ready_state(port), 0);
2609 case VCONN_SWAP_START:
2610 if (port->vconn_role == TYPEC_SOURCE)
2611 tcpm_set_state(port, VCONN_SWAP_WAIT_FOR_VCONN, 0);
2613 tcpm_set_state(port, VCONN_SWAP_TURN_ON_VCONN, 0);
2615 case VCONN_SWAP_WAIT_FOR_VCONN:
2616 tcpm_set_state(port, hard_reset_state(port),
2617 PD_T_VCONN_SOURCE_ON);
2619 case VCONN_SWAP_TURN_ON_VCONN:
2620 tcpm_set_vconn(port, true);
2621 tcpm_pd_send_control(port, PD_CTRL_PS_RDY);
2622 tcpm_swap_complete(port, 0);
2623 tcpm_set_state(port, ready_state(port), 0);
2625 case VCONN_SWAP_TURN_OFF_VCONN:
2626 tcpm_set_vconn(port, false);
2627 tcpm_swap_complete(port, 0);
2628 tcpm_set_state(port, ready_state(port), 0);
2631 case DR_SWAP_CANCEL:
2632 case PR_SWAP_CANCEL:
2633 case VCONN_SWAP_CANCEL:
2634 tcpm_swap_complete(port, port->swap_status);
2635 if (port->pwr_role == TYPEC_SOURCE)
2636 tcpm_set_state(port, SRC_READY, 0);
2638 tcpm_set_state(port, SNK_READY, 0);
2642 switch (BDO_MODE_MASK(port->bist_request)) {
2643 case BDO_MODE_CARRIER2:
2644 tcpm_pd_transmit(port, TCPC_TX_BIST_MODE_2, NULL);
2649 /* Always switch to unattached state */
2650 tcpm_set_state(port, unattached_state(port), 0);
2652 case ERROR_RECOVERY:
2653 tcpm_swap_complete(port, -EPROTO);
2654 tcpm_reset_port(port);
2656 tcpm_set_cc(port, TYPEC_CC_OPEN);
2657 tcpm_set_state(port, ERROR_RECOVERY_WAIT_OFF,
2658 PD_T_ERROR_RECOVERY);
2660 case ERROR_RECOVERY_WAIT_OFF:
2661 tcpm_set_state(port,
2662 tcpm_default_state(port),
2663 port->vbus_present ? PD_T_PS_SOURCE_OFF : 0);
2666 WARN(1, "Unexpected port state %d\n", port->state);
2671 static void tcpm_state_machine_work(struct work_struct *work)
2673 struct tcpm_port *port = container_of(work, struct tcpm_port,
2674 state_machine.work);
2675 enum tcpm_state prev_state;
2677 mutex_lock(&port->lock);
2678 port->state_machine_running = true;
2680 if (port->queued_message && tcpm_send_queued_message(port))
2683 /* If we were queued due to a delayed state change, update it now */
2684 if (port->delayed_state) {
2685 tcpm_log(port, "state change %s -> %s [delayed %ld ms]",
2686 tcpm_states[port->state],
2687 tcpm_states[port->delayed_state], port->delay_ms);
2688 port->prev_state = port->state;
2689 port->state = port->delayed_state;
2690 port->delayed_state = INVALID_STATE;
2694 * Continue running as long as we have (non-delayed) state changes
2698 prev_state = port->state;
2699 run_state_machine(port);
2700 if (port->queued_message)
2701 tcpm_send_queued_message(port);
2702 } while (port->state != prev_state && !port->delayed_state);
2705 port->state_machine_running = false;
2706 mutex_unlock(&port->lock);
2709 static void _tcpm_cc_change(struct tcpm_port *port, enum typec_cc_status cc1,
2710 enum typec_cc_status cc2)
2712 enum typec_cc_status old_cc1, old_cc2;
2713 enum tcpm_state new_state;
2715 old_cc1 = port->cc1;
2716 old_cc2 = port->cc2;
2720 tcpm_log_force(port,
2721 "CC1: %u -> %u, CC2: %u -> %u [state %s, polarity %d, %s]",
2722 old_cc1, cc1, old_cc2, cc2, tcpm_states[port->state],
2724 tcpm_port_is_disconnected(port) ? "disconnected"
2727 switch (port->state) {
2729 if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) ||
2730 tcpm_port_is_source(port))
2731 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
2732 else if (tcpm_port_is_sink(port))
2733 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
2735 case SRC_UNATTACHED:
2736 case ACC_UNATTACHED:
2737 if (tcpm_port_is_debug(port) || tcpm_port_is_audio(port) ||
2738 tcpm_port_is_source(port))
2739 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
2741 case SRC_ATTACH_WAIT:
2742 if (tcpm_port_is_disconnected(port) ||
2743 tcpm_port_is_audio_detached(port))
2744 tcpm_set_state(port, SRC_UNATTACHED, 0);
2745 else if (cc1 != old_cc1 || cc2 != old_cc2)
2746 tcpm_set_state(port, SRC_ATTACH_WAIT, 0);
2749 if (tcpm_port_is_disconnected(port))
2750 tcpm_set_state(port, SRC_UNATTACHED, 0);
2753 case SNK_UNATTACHED:
2754 if (tcpm_port_is_sink(port))
2755 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
2757 case SNK_ATTACH_WAIT:
2758 if ((port->cc1 == TYPEC_CC_OPEN &&
2759 port->cc2 != TYPEC_CC_OPEN) ||
2760 (port->cc1 != TYPEC_CC_OPEN &&
2761 port->cc2 == TYPEC_CC_OPEN))
2762 new_state = SNK_DEBOUNCED;
2763 else if (tcpm_port_is_disconnected(port))
2764 new_state = SNK_UNATTACHED;
2767 if (new_state != port->delayed_state)
2768 tcpm_set_state(port, SNK_ATTACH_WAIT, 0);
2771 if (tcpm_port_is_disconnected(port))
2772 new_state = SNK_UNATTACHED;
2773 else if (port->vbus_present)
2774 new_state = tcpm_try_src(port) ? SRC_TRY : SNK_ATTACHED;
2776 new_state = SNK_UNATTACHED;
2777 if (new_state != port->delayed_state)
2778 tcpm_set_state(port, SNK_DEBOUNCED, 0);
2781 if (tcpm_port_is_disconnected(port))
2782 tcpm_set_state(port, unattached_state(port), 0);
2783 else if (!port->pd_capable &&
2784 (cc1 != old_cc1 || cc2 != old_cc2))
2785 tcpm_set_current_limit(port,
2786 tcpm_get_current_limit(port),
2790 case AUDIO_ACC_ATTACHED:
2791 if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN)
2792 tcpm_set_state(port, AUDIO_ACC_DEBOUNCE, 0);
2794 case AUDIO_ACC_DEBOUNCE:
2795 if (tcpm_port_is_audio(port))
2796 tcpm_set_state(port, AUDIO_ACC_ATTACHED, 0);
2799 case DEBUG_ACC_ATTACHED:
2800 if (cc1 == TYPEC_CC_OPEN || cc2 == TYPEC_CC_OPEN)
2801 tcpm_set_state(port, ACC_UNATTACHED, 0);
2805 /* Do nothing, waiting for timeout */
2809 /* CC line is unstable, wait for debounce */
2810 if (tcpm_port_is_disconnected(port))
2811 tcpm_set_state(port, SNK_DISCOVERY_DEBOUNCE, 0);
2813 case SNK_DISCOVERY_DEBOUNCE:
2817 /* Hand over to state machine if needed */
2818 if (!port->vbus_present && tcpm_port_is_source(port))
2819 new_state = SRC_ATTACHED;
2821 new_state = SRC_TRYWAIT_UNATTACHED;
2823 if (new_state != port->delayed_state)
2824 tcpm_set_state(port, SRC_TRYWAIT, 0);
2827 if (port->vbus_present && tcpm_port_is_sink(port)) {
2828 tcpm_set_state(port, SNK_ATTACHED, 0);
2831 if (!tcpm_port_is_sink(port))
2832 new_state = SRC_TRYWAIT;
2834 new_state = SRC_TRYWAIT_UNATTACHED;
2836 if (new_state != port->delayed_state)
2837 tcpm_set_state(port, SNK_TRY_WAIT, 0);
2841 tcpm_set_state(port, SRC_TRY_DEBOUNCE, 0);
2843 case SRC_TRY_DEBOUNCE:
2844 tcpm_set_state(port, SRC_TRY, 0);
2846 case SNK_TRYWAIT_DEBOUNCE:
2847 if (port->vbus_present) {
2848 tcpm_set_state(port, SNK_ATTACHED, 0);
2851 if (tcpm_port_is_source(port)) {
2852 tcpm_set_state(port, SRC_ATTACHED, 0);
2855 if (tcpm_port_is_disconnected(port) &&
2856 port->delayed_state != SNK_UNATTACHED)
2857 tcpm_set_state(port, SNK_TRYWAIT_DEBOUNCE, 0);
2860 case PR_SWAP_SNK_SRC_SINK_OFF:
2861 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
2862 case PR_SWAP_SRC_SNK_SOURCE_OFF:
2864 * CC state change is expected here; we just turned off power.
2870 if (tcpm_port_is_disconnected(port))
2871 tcpm_set_state(port, unattached_state(port), 0);
2876 static void _tcpm_pd_vbus_on(struct tcpm_port *port)
2878 enum tcpm_state new_state;
2880 tcpm_log_force(port, "VBUS on");
2881 port->vbus_present = true;
2882 switch (port->state) {
2883 case SNK_TRANSITION_SINK_VBUS:
2884 tcpm_set_state(port, SNK_READY, 0);
2887 tcpm_set_state(port, SNK_DISCOVERY, 0);
2891 tcpm_set_state(port, tcpm_try_src(port) ? SRC_TRY
2895 case SNK_HARD_RESET_WAIT_VBUS:
2896 tcpm_set_state(port, SNK_HARD_RESET_SINK_ON, 0);
2899 tcpm_set_state(port, SRC_STARTUP, 0);
2901 case SRC_HARD_RESET_VBUS_ON:
2902 tcpm_set_state(port, SRC_STARTUP, 0);
2906 /* Do nothing, waiting for timeout */
2909 /* Hand over to state machine if needed */
2910 if (port->delayed_state != SRC_TRYWAIT_UNATTACHED)
2911 tcpm_set_state(port, SRC_TRYWAIT, 0);
2914 if (tcpm_port_is_sink(port)) {
2915 tcpm_set_state(port, SNK_ATTACHED, 0);
2918 if (!tcpm_port_is_sink(port))
2919 new_state = SRC_TRYWAIT;
2921 new_state = SRC_TRYWAIT_UNATTACHED;
2923 if (new_state != port->delayed_state)
2924 tcpm_set_state(port, SNK_TRY_WAIT, 0);
2927 tcpm_set_state(port, SNK_TRYWAIT_VBUS, 0);
2935 static void _tcpm_pd_vbus_off(struct tcpm_port *port)
2937 enum tcpm_state new_state;
2939 tcpm_log_force(port, "VBUS off");
2940 port->vbus_present = false;
2941 port->vbus_never_low = false;
2942 switch (port->state) {
2943 case SNK_HARD_RESET_SINK_OFF:
2944 tcpm_set_state(port, SNK_HARD_RESET_WAIT_VBUS, 0);
2946 case SRC_HARD_RESET_VBUS_OFF:
2947 tcpm_set_state(port, SRC_HARD_RESET_VBUS_ON, 0);
2949 case HARD_RESET_SEND:
2953 /* Do nothing, waiting for timeout */
2956 /* Hand over to state machine if needed */
2957 if (tcpm_port_is_source(port))
2958 new_state = SRC_ATTACHED;
2960 new_state = SRC_TRYWAIT_UNATTACHED;
2961 if (new_state != port->delayed_state)
2962 tcpm_set_state(port, SRC_TRYWAIT, 0);
2965 if (!tcpm_port_is_sink(port))
2966 new_state = SRC_TRYWAIT;
2968 new_state = SRC_TRYWAIT_UNATTACHED;
2970 if (new_state != port->delayed_state)
2971 tcpm_set_state(port, SNK_TRY_WAIT, 0);
2973 case SNK_TRYWAIT_VBUS:
2974 tcpm_set_state(port, SNK_TRYWAIT, 0);
2977 case SNK_ATTACH_WAIT:
2978 tcpm_set_state(port, SNK_UNATTACHED, 0);
2981 case SNK_NEGOTIATE_CAPABILITIES:
2984 case PR_SWAP_SRC_SNK_TRANSITION_OFF:
2985 tcpm_set_state(port, PR_SWAP_SRC_SNK_SOURCE_OFF, 0);
2988 case PR_SWAP_SNK_SRC_SINK_OFF:
2989 /* Do nothing, expected */
2992 case ERROR_RECOVERY_WAIT_OFF:
2993 tcpm_set_state(port,
2994 port->pwr_role == TYPEC_SOURCE ?
2995 SRC_UNATTACHED : SNK_UNATTACHED,
3000 if (port->pwr_role == TYPEC_SINK &&
3002 tcpm_set_state(port, SNK_UNATTACHED, 0);
3007 static void _tcpm_pd_hard_reset(struct tcpm_port *port)
3009 tcpm_log_force(port, "Received hard reset");
3011 * If we keep receiving hard reset requests, executing the hard reset
3012 * must have failed. Revert to error recovery if that happens.
3014 tcpm_set_state(port,
3015 port->hard_reset_count < PD_N_HARD_RESET_COUNT ?
3016 HARD_RESET_START : ERROR_RECOVERY,
3020 static void tcpm_pd_event_handler(struct work_struct *work)
3022 struct tcpm_port *port = container_of(work, struct tcpm_port,
3026 mutex_lock(&port->lock);
3028 spin_lock(&port->pd_event_lock);
3029 while (port->pd_events) {
3030 events = port->pd_events;
3031 port->pd_events = 0;
3032 spin_unlock(&port->pd_event_lock);
3033 if (events & TCPM_RESET_EVENT)
3034 _tcpm_pd_hard_reset(port);
3035 if (events & TCPM_VBUS_EVENT) {
3038 vbus = port->tcpc->get_vbus(port->tcpc);
3040 _tcpm_pd_vbus_on(port);
3042 _tcpm_pd_vbus_off(port);
3044 if (events & TCPM_CC_EVENT) {
3045 enum typec_cc_status cc1, cc2;
3047 if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0)
3048 _tcpm_cc_change(port, cc1, cc2);
3050 spin_lock(&port->pd_event_lock);
3052 spin_unlock(&port->pd_event_lock);
3053 mutex_unlock(&port->lock);
3056 void tcpm_cc_change(struct tcpm_port *port)
3058 spin_lock(&port->pd_event_lock);
3059 port->pd_events |= TCPM_CC_EVENT;
3060 spin_unlock(&port->pd_event_lock);
3061 queue_work(port->wq, &port->event_work);
3063 EXPORT_SYMBOL_GPL(tcpm_cc_change);
3065 void tcpm_vbus_change(struct tcpm_port *port)
3067 spin_lock(&port->pd_event_lock);
3068 port->pd_events |= TCPM_VBUS_EVENT;
3069 spin_unlock(&port->pd_event_lock);
3070 queue_work(port->wq, &port->event_work);
3072 EXPORT_SYMBOL_GPL(tcpm_vbus_change);
3074 void tcpm_pd_hard_reset(struct tcpm_port *port)
3076 spin_lock(&port->pd_event_lock);
3077 port->pd_events = TCPM_RESET_EVENT;
3078 spin_unlock(&port->pd_event_lock);
3079 queue_work(port->wq, &port->event_work);
3081 EXPORT_SYMBOL_GPL(tcpm_pd_hard_reset);
3083 static int tcpm_dr_set(const struct typec_capability *cap,
3084 enum typec_data_role data)
3086 struct tcpm_port *port = typec_cap_to_tcpm(cap);
3089 mutex_lock(&port->swap_lock);
3090 mutex_lock(&port->lock);
3092 if (port->typec_caps.type != TYPEC_PORT_DRP || !port->pd_capable) {
3096 if (port->state != SRC_READY && port->state != SNK_READY) {
3101 if (port->data_role == data) {
3108 * 6.3.9: If an alternate mode is active, a request to swap
3109 * alternate modes shall trigger a port reset.
3110 * Reject data role swap request in this case.
3113 port->swap_status = 0;
3114 port->swap_pending = true;
3115 reinit_completion(&port->swap_complete);
3116 tcpm_set_state(port, DR_SWAP_SEND, 0);
3117 mutex_unlock(&port->lock);
3119 wait_for_completion(&port->swap_complete);
3121 ret = port->swap_status;
3125 mutex_unlock(&port->lock);
3127 mutex_unlock(&port->swap_lock);
3131 static int tcpm_pr_set(const struct typec_capability *cap,
3132 enum typec_role role)
3134 struct tcpm_port *port = typec_cap_to_tcpm(cap);
3137 mutex_lock(&port->swap_lock);
3138 mutex_lock(&port->lock);
3140 if (port->typec_caps.type != TYPEC_PORT_DRP) {
3144 if (port->state != SRC_READY && port->state != SNK_READY) {
3149 if (role == port->pwr_role) {
3154 if (!port->pd_capable) {
3156 * If the partner is not PD capable, reset the port to
3157 * trigger a role change. This can only work if a preferred
3158 * role is configured, and if it matches the requested role.
3160 if (port->try_role == TYPEC_NO_PREFERRED_ROLE ||
3161 port->try_role == port->pwr_role) {
3165 tcpm_set_state(port, HARD_RESET_SEND, 0);
3170 port->swap_status = 0;
3171 port->swap_pending = true;
3172 reinit_completion(&port->swap_complete);
3173 tcpm_set_state(port, PR_SWAP_SEND, 0);
3174 mutex_unlock(&port->lock);
3176 wait_for_completion(&port->swap_complete);
3178 ret = port->swap_status;
3182 mutex_unlock(&port->lock);
3184 mutex_unlock(&port->swap_lock);
3188 static int tcpm_vconn_set(const struct typec_capability *cap,
3189 enum typec_role role)
3191 struct tcpm_port *port = typec_cap_to_tcpm(cap);
3194 mutex_lock(&port->swap_lock);
3195 mutex_lock(&port->lock);
3197 if (port->state != SRC_READY && port->state != SNK_READY) {
3202 if (role == port->vconn_role) {
3207 port->swap_status = 0;
3208 port->swap_pending = true;
3209 reinit_completion(&port->swap_complete);
3210 tcpm_set_state(port, VCONN_SWAP_SEND, 0);
3211 mutex_unlock(&port->lock);
3213 wait_for_completion(&port->swap_complete);
3215 ret = port->swap_status;
3219 mutex_unlock(&port->lock);
3221 mutex_unlock(&port->swap_lock);
3225 static int tcpm_try_role(const struct typec_capability *cap, int role)
3227 struct tcpm_port *port = typec_cap_to_tcpm(cap);
3228 struct tcpc_dev *tcpc = port->tcpc;
3231 mutex_lock(&port->lock);
3233 ret = tcpc->try_role(tcpc, role);
3234 if (!ret && !tcpc->config->try_role_hw)
3235 port->try_role = role;
3236 port->try_src_count = 0;
3237 port->try_snk_count = 0;
3238 mutex_unlock(&port->lock);
3243 static void tcpm_init(struct tcpm_port *port)
3245 enum typec_cc_status cc1, cc2;
3247 port->tcpc->init(port->tcpc);
3249 tcpm_reset_port(port);
3253 * Should possibly wait for VBUS to settle if it was enabled locally
3254 * since tcpm_reset_port() will disable VBUS.
3256 port->vbus_present = port->tcpc->get_vbus(port->tcpc);
3257 if (port->vbus_present)
3258 port->vbus_never_low = true;
3260 tcpm_set_state(port, tcpm_default_state(port), 0);
3262 if (port->tcpc->get_cc(port->tcpc, &cc1, &cc2) == 0)
3263 _tcpm_cc_change(port, cc1, cc2);
3266 * Some adapters need a clean slate at startup, and won't recover
3267 * otherwise. So do not try to be fancy and force a clean disconnect.
3269 tcpm_set_state(port, ERROR_RECOVERY, 0);
3272 void tcpm_tcpc_reset(struct tcpm_port *port)
3274 mutex_lock(&port->lock);
3275 /* XXX: Maintain PD connection if possible? */
3277 mutex_unlock(&port->lock);
3279 EXPORT_SYMBOL_GPL(tcpm_tcpc_reset);
3281 static int tcpm_copy_pdos(u32 *dest_pdo, const u32 *src_pdo,
3282 unsigned int nr_pdo)
3286 if (nr_pdo > PDO_MAX_OBJECTS)
3287 nr_pdo = PDO_MAX_OBJECTS;
3289 for (i = 0; i < nr_pdo; i++)
3290 dest_pdo[i] = src_pdo[i];
3295 void tcpm_update_source_capabilities(struct tcpm_port *port, const u32 *pdo,
3296 unsigned int nr_pdo)
3298 mutex_lock(&port->lock);
3299 port->nr_src_pdo = tcpm_copy_pdos(port->src_pdo, pdo, nr_pdo);
3300 switch (port->state) {
3301 case SRC_UNATTACHED:
3302 case SRC_ATTACH_WAIT:
3304 tcpm_set_cc(port, tcpm_rp_cc(port));
3306 case SRC_SEND_CAPABILITIES:
3307 case SRC_NEGOTIATE_CAPABILITIES:
3309 case SRC_WAIT_NEW_CAPABILITIES:
3310 tcpm_set_cc(port, tcpm_rp_cc(port));
3311 tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
3316 mutex_unlock(&port->lock);
3318 EXPORT_SYMBOL_GPL(tcpm_update_source_capabilities);
3320 void tcpm_update_sink_capabilities(struct tcpm_port *port, const u32 *pdo,
3321 unsigned int nr_pdo,
3322 unsigned int max_snk_mv,
3323 unsigned int max_snk_ma,
3324 unsigned int max_snk_mw,
3325 unsigned int operating_snk_mw)
3327 mutex_lock(&port->lock);
3328 port->nr_snk_pdo = tcpm_copy_pdos(port->snk_pdo, pdo, nr_pdo);
3329 port->max_snk_mv = max_snk_mv;
3330 port->max_snk_ma = max_snk_ma;
3331 port->max_snk_mw = max_snk_mw;
3332 port->operating_snk_mw = operating_snk_mw;
3334 switch (port->state) {
3335 case SNK_NEGOTIATE_CAPABILITIES:
3337 case SNK_TRANSITION_SINK:
3338 case SNK_TRANSITION_SINK_VBUS:
3339 tcpm_set_state(port, SNK_NEGOTIATE_CAPABILITIES, 0);
3344 mutex_unlock(&port->lock);
3346 EXPORT_SYMBOL_GPL(tcpm_update_sink_capabilities);
3348 struct tcpm_port *tcpm_register_port(struct device *dev, struct tcpc_dev *tcpc)
3350 struct tcpm_port *port;
3353 if (!dev || !tcpc || !tcpc->config ||
3354 !tcpc->get_vbus || !tcpc->set_cc || !tcpc->get_cc ||
3355 !tcpc->set_polarity || !tcpc->set_vconn || !tcpc->set_vbus ||
3356 !tcpc->set_pd_rx || !tcpc->set_roles || !tcpc->pd_transmit)
3357 return ERR_PTR(-EINVAL);
3359 port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
3361 return ERR_PTR(-ENOMEM);
3366 mutex_init(&port->lock);
3367 mutex_init(&port->swap_lock);
3369 port->wq = create_singlethread_workqueue(dev_name(dev));
3371 return ERR_PTR(-ENOMEM);
3372 INIT_DELAYED_WORK(&port->state_machine, tcpm_state_machine_work);
3373 INIT_DELAYED_WORK(&port->vdm_state_machine, vdm_state_machine_work);
3374 INIT_WORK(&port->event_work, tcpm_pd_event_handler);
3376 spin_lock_init(&port->pd_event_lock);
3378 init_completion(&port->tx_complete);
3379 init_completion(&port->swap_complete);
3381 port->nr_src_pdo = tcpm_copy_pdos(port->src_pdo, tcpc->config->src_pdo,
3382 tcpc->config->nr_src_pdo);
3383 port->nr_snk_pdo = tcpm_copy_pdos(port->snk_pdo, tcpc->config->snk_pdo,
3384 tcpc->config->nr_snk_pdo);
3386 port->max_snk_mv = tcpc->config->max_snk_mv;
3387 port->max_snk_ma = tcpc->config->max_snk_ma;
3388 port->max_snk_mw = tcpc->config->max_snk_mw;
3389 port->operating_snk_mw = tcpc->config->operating_snk_mw;
3390 if (!tcpc->config->try_role_hw)
3391 port->try_role = tcpc->config->default_role;
3393 port->try_role = TYPEC_NO_PREFERRED_ROLE;
3395 port->typec_caps.prefer_role = tcpc->config->default_role;
3396 port->typec_caps.type = tcpc->config->type;
3397 port->typec_caps.revision = 0x0120; /* Type-C spec release 1.2 */
3398 port->typec_caps.pd_revision = 0x0200; /* USB-PD spec release 2.0 */
3399 port->typec_caps.dr_set = tcpm_dr_set;
3400 port->typec_caps.pr_set = tcpm_pr_set;
3401 port->typec_caps.vconn_set = tcpm_vconn_set;
3402 port->typec_caps.try_role = tcpm_try_role;
3404 port->partner_desc.identity = &port->partner_ident;
3408 * - alt_modes, set_alt_mode
3409 * - {debug,audio}_accessory
3412 port->typec_port = typec_register_port(port->dev, &port->typec_caps);
3413 if (!port->typec_port) {
3415 goto out_destroy_wq;
3418 if (tcpc->config->alt_modes) {
3419 struct typec_altmode_desc *paltmode = tcpc->config->alt_modes;
3422 while (paltmode->svid && i < ARRAY_SIZE(port->port_altmode)) {
3423 port->port_altmode[i] =
3424 typec_port_register_altmode(port->typec_port,
3426 if (!port->port_altmode[i]) {
3428 "%s: failed to register port alternate mode 0x%x",
3429 dev_name(dev), paltmode->svid);
3437 tcpm_debugfs_init(port);
3438 mutex_lock(&port->lock);
3440 mutex_unlock(&port->lock);
3442 tcpm_log(port, "%s: registered", dev_name(dev));
3446 destroy_workqueue(port->wq);
3447 return ERR_PTR(err);
3449 EXPORT_SYMBOL_GPL(tcpm_register_port);
3451 void tcpm_unregister_port(struct tcpm_port *port)
3455 for (i = 0; i < ARRAY_SIZE(port->port_altmode); i++)
3456 typec_unregister_altmode(port->port_altmode[i]);
3457 typec_unregister_port(port->typec_port);
3458 tcpm_debugfs_exit(port);
3459 destroy_workqueue(port->wq);
3461 EXPORT_SYMBOL_GPL(tcpm_unregister_port);
3463 MODULE_AUTHOR("Guenter Roeck <groeck@chromium.org>");
3464 MODULE_DESCRIPTION("USB Type-C Port Manager");
3465 MODULE_LICENSE("GPL");