1 /*
2  * Copyright (c) 2023 The Chromium OS Authors
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_SUBSYS_USBC_TC_COMMON_INTERNAL_H_
8 #define ZEPHYR_SUBSYS_USBC_TC_COMMON_INTERNAL_H_
9 
10 #include <zephyr/kernel.h>
11 #include <zephyr/usb_c/usbc.h>
12 #include <zephyr/smf.h>
13 
14 #include "usbc_timer.h"
15 #include "usbc_stack.h"
16 
17 enum tc_flags {
18 	/**
19 	 * Flag to track Rp resistor change when the sink attached
20 	 * sub-state runs
21 	 */
22 	TC_FLAGS_RP_SUBSTATE_CHANGE = 0,
23 	/** Tracks if VCONN is ON or OFF */
24 	TC_FLAGS_VCONN_ON,
25 };
26 
27 /**
28  * @brief Type-C States
29  */
30 enum tc_state_t {
31 	/** Super state that opens the CC lines */
32 	TC_CC_OPEN_SUPER_STATE,
33 #ifdef CONFIG_USBC_CSM_SINK_ONLY
34 	/** Super state that applies Rd to the CC lines */
35 	TC_CC_RD_SUPER_STATE,
36 	/** Unattached Sink State */
37 	TC_UNATTACHED_SNK_STATE,
38 	/** Attach Wait Sink State */
39 	TC_ATTACH_WAIT_SNK_STATE,
40 	/** Attached Sink State */
41 	TC_ATTACHED_SNK_STATE,
42 #else
43 	/** Super state that applies Rp to the CC lines */
44 	TC_CC_RP_SUPER_STATE,
45 	/** Unattached Source State */
46 	TC_UNATTACHED_SRC_STATE,
47 	/** Unattached Wait Source State */
48 	TC_UNATTACHED_WAIT_SRC_STATE,
49 	/** Attach Wait Source State */
50 	TC_ATTACH_WAIT_SRC_STATE,
51 	/** Attached Source State */
52 	TC_ATTACHED_SRC_STATE,
53 #endif
54 	/** Disabled State */
55 	TC_DISABLED_STATE,
56 	/** Error Recovery State */
57 	TC_ERROR_RECOVERY_STATE,
58 
59 	/** Number of TC States */
60 	TC_STATE_COUNT
61 };
62 
63 /**
64  * @brief TC Layer State Machine Object
65  */
66 struct tc_sm_t {
67 	/** TC layer state machine context */
68 	struct smf_ctx ctx;
69 	/** Port device */
70 	const struct device *dev;
71 	/** TC layer flags */
72 	atomic_t flags;
73 	/** VBUS measurement device */
74 	const struct device *vbus_dev;
75 	/** Port polarity */
76 	enum tc_cc_polarity cc_polarity;
77 	/** The cc state */
78 	enum tc_cc_states cc_state;
79 	/** Voltage on CC pin */
80 	enum tc_cc_voltage_state cc_voltage;
81 	/** Current CC1 value */
82 	enum tc_cc_voltage_state cc1;
83 	/** Current CC2 value */
84 	enum tc_cc_voltage_state cc2;
85 
86 	/* Timers */
87 
88 	/** tCCDebounce timer */
89 	struct usbc_timer_t tc_t_cc_debounce;
90 	/** tRpValueChange timer */
91 	struct usbc_timer_t tc_t_rp_value_change;
92 	/** tErrorRecovery timer */
93 	struct usbc_timer_t tc_t_error_recovery;
94 #ifdef CONFIG_USBC_CSM_SOURCE_ONLY
95 	/** tVconnOff timer */
96 	struct usbc_timer_t tc_t_vconn_off;
97 #endif
98 };
99 
100 /**
101  * @brief Sets a Type-C State
102  *
103  * @param dev Pointer to the device structure for the driver instance
104  * @param state next Type-C State to enter
105  */
106 void tc_set_state(const struct device *dev, const enum tc_state_t state);
107 
108 /**
109  * @brief Get current Type-C State
110  *
111  * @param dev Pointer to the device structure for the driver instance
112  * @return current Type-C state
113  */
114 enum tc_state_t tc_get_state(const struct device *dev);
115 
116 /**
117  * @brief Enable Power Delivery
118  *
119  * @param dev Pointer to the device structure for the driver instance
120  * @param enable set true to enable Power Deliver
121  */
122 void tc_pd_enable(const struct device *dev, const bool enable);
123 
124 /**
125  * @brief This function must only be called in the subsystem init function.
126  *
127  * @param dev Pointer to the device structure for the driver instance.
128  */
129 void tc_subsys_init(const struct device *dev);
130 
131 /**
132  * @brief Run the TC Layer state machine. This is called from the subsystems
133  *        port stack thread.
134  *
135  * @param dev Pointer to the device structure for the driver instance.
136  * @param dpm_request Device Policy Manager request
137  */
138 void tc_run(const struct device *dev, int32_t dpm_request);
139 
140 /**
141  * @brief Checks if the TC Layer is in an Attached state
142  *
143  * @param dev Pointer to the device structure for the driver instance.
144  *
145  * @retval true if TC Layer is in an Attached state, else false
146  */
147 bool tc_is_in_attached_state(const struct device *dev);
148 
149 /**
150  * @brief Sets the Collision Avoidance Rp value
151  *
152  * @param dev Pointer to the device structure for the driver instance.
153  * @param rp Collision Avoidance Rp to set
154  */
155 void tc_select_src_collision_rp(const struct device *dev, enum tc_rp_value rp);
156 
157 #endif /* ZEPHYR_SUBSYS_USBC_TC_COMMON_INTERNAL_H_ */
158