xref: /linux/drivers/usb/typec/ucsi/ucsi.h (revision ba23adb6533149df33b9a247f31a87227b3c86d5)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #ifndef __DRIVER_USB_TYPEC_UCSI_H
4 #define __DRIVER_USB_TYPEC_UCSI_H
5 
6 #include <linux/bitops.h>
7 #include <linux/bitmap.h>
8 #include <linux/completion.h>
9 #include <linux/device.h>
10 #include <linux/power_supply.h>
11 #include <linux/types.h>
12 #include <linux/usb/typec.h>
13 #include <linux/usb/pd.h>
14 #include <linux/usb/role.h>
15 #include <linux/unaligned.h>
16 
17 /* -------------------------------------------------------------------------- */
18 
19 struct ucsi;
20 struct ucsi_altmode;
21 struct ucsi_connector;
22 struct dentry;
23 
24 /* UCSI offsets (Bytes) */
25 #define UCSI_VERSION			0
26 #define UCSI_CCI			4
27 #define UCSI_CONTROL			8
28 #define UCSI_MESSAGE_IN			16
29 #define UCSI_MESSAGE_OUT		32
30 #define UCSIv2_MESSAGE_OUT		272
31 
32 /* UCSI versions */
33 #define UCSI_VERSION_1_0	0x0100
34 #define UCSI_VERSION_1_1	0x0110
35 #define UCSI_VERSION_1_2	0x0120
36 #define UCSI_VERSION_2_0	0x0200
37 #define UCSI_VERSION_2_1	0x0210
38 #define UCSI_VERSION_3_0	0x0300
39 
40 #define UCSI_BCD_GET_MAJOR(_v_)		(((_v_) >> 8) & 0xFF)
41 #define UCSI_BCD_GET_MINOR(_v_)		(((_v_) >> 4) & 0x0F)
42 #define UCSI_BCD_GET_SUBMINOR(_v_)	((_v_) & 0x0F)
43 
44 /*
45  * Per USB PD 3.2, Section 6.2.1.1.5, the spec revision is represented by 2 bits
46  * 0b00 = 1.0, 0b01 = 2.0, 0b10 = 3.0, 0b11 = Reserved, Shall NOT be used.
47  */
48 #define UCSI_SPEC_REVISION_TO_BCD(_v_)  (((_v_) + 1) << 8)
49 
50 /* Command Status and Connector Change Indication (CCI) bits */
51 #define UCSI_CCI_CONNECTOR(_c_)		(((_c_) & GENMASK(7, 1)) >> 1)
52 #define UCSI_CCI_LENGTH(_c_)		(((_c_) & GENMASK(15, 8)) >> 8)
53 #define UCSI_SET_CCI_LENGTH(_c_)	((_c_) << 8)
54 #define UCSI_CCI_NOT_SUPPORTED		BIT(25)
55 #define UCSI_CCI_CANCEL_COMPLETE	BIT(26)
56 #define UCSI_CCI_RESET_COMPLETE		BIT(27)
57 #define UCSI_CCI_BUSY			BIT(28)
58 #define UCSI_CCI_ACK_COMPLETE		BIT(29)
59 #define UCSI_CCI_ERROR			BIT(30)
60 #define UCSI_CCI_COMMAND_COMPLETE	BIT(31)
61 
62 /**
63  * struct ucsi_operations - UCSI I/O operations
64  * @read_version: Read implemented UCSI version
65  * @read_cci: Read CCI register
66  * @poll_cci: Read CCI register while polling with notifications disabled
67  * @read_message_in: Read message data from UCSI
68  * @sync_control: Blocking control operation
69  * @async_control: Non-blocking control operation
70  * @update_altmodes: Squashes duplicate DP altmodes
71  * @update_connector: Update connector capabilities before registering
72  * @connector_status: Updates connector status, called holding connector lock
73  * @add_partner_altmodes: Start mode selection
74  * @remove_partner_altmodes: Clean mode selection
75  *
76  * Read and write routines for UCSI interface. @sync_write must wait for the
77  * Command Completion Event from the PPM before returning, and @async_write must
78  * return immediately after sending the data to the PPM.
79  */
80 struct ucsi_operations {
81 	int (*read_version)(struct ucsi *ucsi, u16 *version);
82 	int (*read_cci)(struct ucsi *ucsi, u32 *cci);
83 	int (*poll_cci)(struct ucsi *ucsi, u32 *cci);
84 	int (*read_message_in)(struct ucsi *ucsi, void *val, size_t val_len);
85 	int (*sync_control)(struct ucsi *ucsi, u64 command, u32 *cci,
86 			    void *data, size_t size);
87 	int (*async_control)(struct ucsi *ucsi, u64 command);
88 	bool (*update_altmodes)(struct ucsi *ucsi, u8 recipient,
89 				struct ucsi_altmode *orig,
90 				struct ucsi_altmode *updated);
91 	void (*update_connector)(struct ucsi_connector *con);
92 	void (*connector_status)(struct ucsi_connector *con);
93 	void (*add_partner_altmodes)(struct ucsi_connector *con);
94 	void (*remove_partner_altmodes)(struct ucsi_connector *con);
95 };
96 
97 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops);
98 void ucsi_destroy(struct ucsi *ucsi);
99 int ucsi_register(struct ucsi *ucsi);
100 void ucsi_unregister(struct ucsi *ucsi);
101 void *ucsi_get_drvdata(struct ucsi *ucsi);
102 void ucsi_set_drvdata(struct ucsi *ucsi, void *data);
103 bool ucsi_con_mutex_lock(struct ucsi_connector *con);
104 void ucsi_con_mutex_unlock(struct ucsi_connector *con);
105 
106 void ucsi_connector_change(struct ucsi *ucsi, u8 num);
107 
108 /* -------------------------------------------------------------------------- */
109 
110 /* Commands */
111 #define UCSI_PPM_RESET				0x01
112 #define UCSI_CANCEL				0x02
113 #define UCSI_CONNECTOR_RESET			0x03
114 #define UCSI_ACK_CC_CI				0x04
115 #define UCSI_SET_NOTIFICATION_ENABLE		0x05
116 #define UCSI_GET_CAPABILITY			0x06
117 #define UCSI_GET_CAPABILITY_SIZE		128
118 #define UCSI_GET_CONNECTOR_CAPABILITY		0x07
119 #define UCSI_GET_CONNECTOR_CAPABILITY_SIZE	32
120 #define UCSI_SET_CCOM				0x08
121 #define UCSI_SET_UOR				0x09
122 #define UCSI_SET_PDM				0x0a
123 #define UCSI_SET_PDR				0x0b
124 #define UCSI_GET_ALTERNATE_MODES		0x0c
125 #define UCSI_GET_CAM_SUPPORTED			0x0d
126 #define UCSI_GET_CURRENT_CAM			0x0e
127 #define UCSI_SET_NEW_CAM			0x0f
128 #define UCSI_GET_PDOS				0x10
129 #define UCSI_GET_CABLE_PROPERTY			0x11
130 #define UCSI_GET_CABLE_PROPERTY_SIZE		64
131 #define UCSI_GET_CONNECTOR_STATUS		0x12
132 #define UCSI_GET_CONNECTOR_STATUS_SIZE		152
133 #define UCSI_GET_ERROR_STATUS			0x13
134 #define UCSI_SET_POWER_LEVEL			0x14
135 #define UCSI_GET_ATTENTION_VDO			0x16
136 #define UCSI_GET_PD_MESSAGE			0x15
137 #define UCSI_GET_CAM_CS			0x18
138 #define UCSI_SET_SINK_PATH			0x1c
139 #define UCSI_READ_POWER_LEVEL			0x1e
140 #define UCSI_SET_USB				0x21
141 #define UCSI_GET_LPM_PPM_INFO			0x22
142 
143 #define UCSI_CONNECTOR_NUMBER(_num_)		((u64)(_num_) << 16)
144 #define UCSI_COMMAND(_cmd_)			((_cmd_) & 0xff)
145 
146 #define UCSI_GET_ALTMODE_GET_CONNECTOR_NUMBER(_cmd_)	(((_cmd_) >> 24) & GENMASK(6, 0))
147 #define UCSI_DEFAULT_GET_CONNECTOR_NUMBER(_cmd_)	(((_cmd_) >> 16) & GENMASK(6, 0))
148 
149 /* CONNECTOR_RESET command bits */
150 #define UCSI_CONNECTOR_RESET_HARD_VER_1_0	BIT(23) /* Deprecated in v1.1 */
151 #define UCSI_CONNECTOR_RESET_DATA_VER_2_0	BIT(23) /* Redefined in v2.0 */
152 
153 /* ACK_CC_CI bits */
154 #define UCSI_ACK_CONNECTOR_CHANGE		BIT(16)
155 #define UCSI_ACK_COMMAND_COMPLETE		BIT(17)
156 
157 /* SET_NOTIFICATION_ENABLE command bits */
158 #define UCSI_ENABLE_NTFY_CMD_COMPLETE		BIT_ULL(16)
159 #define UCSI_ENABLE_NTFY_EXT_PWR_SRC_CHANGE	BIT_ULL(17)
160 #define UCSI_ENABLE_NTFY_PWR_OPMODE_CHANGE	BIT_ULL(18)
161 #define UCSI_ENABLE_NTFY_ATTENTION		BIT_ULL(19)
162 #define UCSI_ENABLE_NTFY_LPM_FW_UPDATE_REQ	BIT_ULL(20)
163 #define UCSI_ENABLE_NTFY_CAP_CHANGE		BIT_ULL(21)
164 #define UCSI_ENABLE_NTFY_PWR_LEVEL_CHANGE	BIT_ULL(22)
165 #define UCSI_ENABLE_NTFY_PD_RESET_COMPLETE	BIT_ULL(23)
166 #define UCSI_ENABLE_NTFY_CAM_CHANGE		BIT_ULL(24)
167 #define UCSI_ENABLE_NTFY_BAT_STATUS_CHANGE	BIT_ULL(25)
168 #define UCSI_ENABLE_NTFY_SECURITY_REQ_PARTNER	BIT_ULL(26)
169 #define UCSI_ENABLE_NTFY_PARTNER_CHANGE		BIT_ULL(27)
170 #define UCSI_ENABLE_NTFY_PWR_DIR_CHANGE		BIT_ULL(28)
171 #define UCSI_ENABLE_NTFY_SET_RETIMER_MODE	BIT_ULL(29)
172 #define UCSI_ENABLE_NTFY_CONNECTOR_CHANGE	BIT_ULL(30)
173 #define UCSI_ENABLE_NTFY_ERROR			BIT_ULL(31)
174 #define UCSI_ENABLE_NTFY_SINK_PATH_STS_CHANGE	BIT_ULL(32)
175 #define UCSI_ENABLE_NTFY_ALL			0xdbe70000
176 
177 /* SET_UOR command bits */
178 #define UCSI_SET_UOR_ROLE(_r_)		(((_r_) == TYPEC_HOST ? 1 : 2) << 23)
179 #define UCSI_SET_UOR_ACCEPT_ROLE_SWAPS		BIT(25)
180 
181 /* SET_PDF command bits */
182 #define UCSI_SET_PDR_ROLE(_r_)		(((_r_) == TYPEC_SOURCE ? 1 : 2) << 23)
183 #define UCSI_SET_PDR_ACCEPT_ROLE_SWAPS		BIT(25)
184 
185 /* GET_ALTERNATE_MODES command bits */
186 #define UCSI_ALTMODE_RECIPIENT(_r_)		(((_r_) >> 16) & 0x7)
187 #define UCSI_GET_ALTMODE_RECIPIENT(_r_)		((u64)(_r_) << 16)
188 #define   UCSI_RECIPIENT_CON			0
189 #define   UCSI_RECIPIENT_SOP			1
190 #define   UCSI_RECIPIENT_SOP_P			2
191 #define   UCSI_RECIPIENT_SOP_PP			3
192 #define UCSI_GET_ALTMODE_CONNECTOR_NUMBER(_r_)	((u64)(_r_) << 24)
193 #define UCSI_ALTMODE_OFFSET(_r_)		(((_r_) >> 32) & 0xff)
194 #define UCSI_GET_ALTMODE_OFFSET(_r_)		((u64)(_r_) << 32)
195 #define UCSI_GET_ALTMODE_NUM_ALTMODES(_r_)	((u64)(_r_) << 40)
196 
197 /* GET_PDOS command bits */
198 #define UCSI_GET_PDOS_PARTNER_PDO(_r_)		((u64)(_r_) << 23)
199 #define UCSI_GET_PDOS_PDO_OFFSET(_r_)		((u64)(_r_) << 24)
200 #define UCSI_GET_PDOS_NUM_PDOS(_r_)		((u64)(_r_) << 32)
201 #define UCSI_MAX_PDOS				(4)
202 #define UCSI_GET_PDOS_SRC_PDOS			((u64)1 << 34)
203 
204 /* GET_PD_MESSAGE command bits */
205 #define UCSI_GET_PD_MESSAGE_RECIPIENT(_r_)	((u64)(_r_) << 23)
206 #define UCSI_GET_PD_MESSAGE_OFFSET(_r_)		((u64)(_r_) << 26)
207 #define UCSI_GET_PD_MESSAGE_BYTES(_r_)		((u64)(_r_) << 34)
208 #define UCSI_GET_PD_MESSAGE_TYPE(_r_)		((u64)(_r_) << 42)
209 #define   UCSI_GET_PD_MESSAGE_TYPE_SNK_CAP_EXT	0
210 #define   UCSI_GET_PD_MESSAGE_TYPE_SRC_CAP_EXT	1
211 #define   UCSI_GET_PD_MESSAGE_TYPE_BAT_CAP	2
212 #define   UCSI_GET_PD_MESSAGE_TYPE_BAT_STAT	3
213 #define   UCSI_GET_PD_MESSAGE_TYPE_IDENTITY	4
214 #define   UCSI_GET_PD_MESSAGE_TYPE_REVISION	5
215 
216 /* -------------------------------------------------------------------------- */
217 
218 /* Error information returned by PPM in response to GET_ERROR_STATUS command. */
219 #define UCSI_ERROR_UNREGONIZED_CMD		BIT(0)
220 #define UCSI_ERROR_INVALID_CON_NUM		BIT(1)
221 #define UCSI_ERROR_INVALID_CMD_ARGUMENT		BIT(2)
222 #define UCSI_ERROR_INCOMPATIBLE_PARTNER		BIT(3)
223 #define UCSI_ERROR_CC_COMMUNICATION_ERR		BIT(4)
224 #define UCSI_ERROR_DEAD_BATTERY			BIT(5)
225 #define UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL	BIT(6)
226 #define UCSI_ERROR_OVERCURRENT			BIT(7)
227 #define UCSI_ERROR_UNDEFINED			BIT(8)
228 #define UCSI_ERROR_PARTNER_REJECTED_SWAP	BIT(9)
229 #define UCSI_ERROR_HARD_RESET			BIT(10)
230 #define UCSI_ERROR_PPM_POLICY_CONFLICT		BIT(11)
231 #define UCSI_ERROR_SWAP_REJECTED		BIT(12)
232 #define UCSI_ERROR_REVERSE_CURRENT_PROTECTION	BIT(13)
233 #define UCSI_ERROR_SET_SINK_PATH_REJECTED	BIT(14)
234 
235 #define UCSI_SET_NEW_CAM_ENTER(x)		(((x) >> 23) & 0x1)
236 #define UCSI_SET_NEW_CAM_GET_AM(x)		(((x) >> 24) & 0xff)
237 #define UCSI_SET_NEW_CAM_AM_MASK		(0xff << 24)
238 #define UCSI_SET_NEW_CAM_SET_AM(x)		(((x) & 0xff) << 24)
239 #define UCSI_CMD_CONNECTOR_MASK			(0x7)
240 
241 /* Data structure filled by PPM in response to GET_CAPABILITY command. */
242 struct ucsi_capability {
243 	u32 attributes;
244 #define UCSI_CAP_ATTR_DISABLE_STATE		BIT(0)
245 #define UCSI_CAP_ATTR_BATTERY_CHARGING		BIT(1)
246 #define UCSI_CAP_ATTR_USB_PD			BIT(2)
247 #define UCSI_CAP_ATTR_TYPEC_CURRENT		BIT(6)
248 #define UCSI_CAP_ATTR_POWER_AC_SUPPLY		BIT(8)
249 #define UCSI_CAP_ATTR_POWER_OTHER		BIT(10)
250 #define UCSI_CAP_ATTR_POWER_VBUS		BIT(14)
251 	u8 num_connectors;
252 	u16 features;
253 #define UCSI_CAP_SET_UOM			BIT(0)
254 #define UCSI_CAP_SET_PDM			BIT(1)
255 #define UCSI_CAP_ALT_MODE_DETAILS		BIT(2)
256 #define UCSI_CAP_ALT_MODE_OVERRIDE		BIT(3)
257 #define UCSI_CAP_PDO_DETAILS			BIT(4)
258 #define UCSI_CAP_CABLE_DETAILS			BIT(5)
259 #define UCSI_CAP_EXT_SUPPLY_NOTIFICATIONS	BIT(6)
260 #define UCSI_CAP_PD_RESET			BIT(7)
261 #define UCSI_CAP_GET_PD_MESSAGE			BIT(8)
262 #define UCSI_CAP_GET_ATTENTION_VDO		BIT(9)
263 #define UCSI_CAP_FW_UPDATE_REQUEST		BIT(10)
264 #define UCSI_CAP_NEGOTIATED_PWR_LEVEL_CHANGE	BIT(11)
265 #define UCSI_CAP_SECURITY_REQUEST		BIT(12)
266 #define UCSI_CAP_SET_RETIMER_MODE		BIT(13)
267 #define UCSI_CAP_CHUNKING_SUPPORT		BIT(14)
268 	u8 reserved_1;
269 	u8 num_alt_modes;
270 	u8 reserved_2;
271 	u16 bc_version;
272 	u16 pd_version;
273 	u16 typec_version;
274 } __packed;
275 
276 struct ucsi_altmode {
277 	u16 svid;
278 	u32 mid;
279 } __packed;
280 
281 /* Data structure filled by PPM in response to GET_CABLE_PROPERTY command. */
282 struct ucsi_cable_property {
283 	u16 speed_supported;
284 	u8 current_capability;
285 	u8 flags;
286 #define UCSI_CABLE_PROP_FLAG_VBUS_IN_CABLE	BIT(0)
287 #define UCSI_CABLE_PROP_FLAG_ACTIVE_CABLE	BIT(1)
288 #define UCSI_CABLE_PROP_FLAG_DIRECTIONALITY	BIT(2)
289 #define UCSI_CABLE_PROP_FLAG_PLUG_TYPE(_f_)	(((_f_) & GENMASK(4, 3)) >> 3)
290 #define   UCSI_CABLE_PROPERTY_PLUG_TYPE_A	0
291 #define   UCSI_CABLE_PROPERTY_PLUG_TYPE_B	1
292 #define   UCSI_CABLE_PROPERTY_PLUG_TYPE_C	2
293 #define   UCSI_CABLE_PROPERTY_PLUG_OTHER	3
294 #define UCSI_CABLE_PROP_FLAG_MODE_SUPPORT	BIT(5)
295 #define UCSI_CABLE_PROP_FLAG_PD_MAJOR_REV(_f_)	(((_f_) & GENMASK(7, 6)) >> 6)
296 #define UCSI_CABLE_PROP_FLAG_PD_MAJOR_REV_AS_BCD(_f_) \
297 	UCSI_SPEC_REVISION_TO_BCD(UCSI_CABLE_PROP_FLAG_PD_MAJOR_REV(_f_))
298 	u8 latency;
299 } __packed;
300 
301 /* Get Connector Capability Fields. */
302 #define UCSI_CONCAP_OPMODE			UCSI_DECLARE_BITFIELD(0, 0, 8)
303 #define   UCSI_CONCAP_OPMODE_DFP		UCSI_DECLARE_BITFIELD(0, 0, 1)
304 #define   UCSI_CONCAP_OPMODE_UFP		UCSI_DECLARE_BITFIELD(0, 1, 1)
305 #define   UCSI_CONCAP_OPMODE_DRP		UCSI_DECLARE_BITFIELD(0, 2, 1)
306 #define   UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY	UCSI_DECLARE_BITFIELD(0, 3, 1)
307 #define   UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY	UCSI_DECLARE_BITFIELD(0, 4, 1)
308 #define   UCSI_CONCAP_OPMODE_USB2		UCSI_DECLARE_BITFIELD(0, 5, 1)
309 #define   UCSI_CONCAP_OPMODE_USB3		UCSI_DECLARE_BITFIELD(0, 6, 1)
310 #define   UCSI_CONCAP_OPMODE_ALT_MODE		UCSI_DECLARE_BITFIELD(0, 7, 1)
311 #define UCSI_CONCAP_PROVIDER			UCSI_DECLARE_BITFIELD(0, 8, 1)
312 #define UCSI_CONCAP_CONSUMER			UCSI_DECLARE_BITFIELD(0, 9, 1)
313 #define UCSI_CONCAP_SWAP_TO_DFP_V1_1		UCSI_DECLARE_BITFIELD_V1_1(10, 1)
314 #define UCSI_CONCAP_SWAP_TO_UFP_V1_1		UCSI_DECLARE_BITFIELD_V1_1(11, 1)
315 #define UCSI_CONCAP_SWAP_TO_SRC_V1_1		UCSI_DECLARE_BITFIELD_V1_1(12, 1)
316 #define UCSI_CONCAP_SWAP_TO_SNK_V1_1		UCSI_DECLARE_BITFIELD_V1_1(13, 1)
317 #define UCSI_CONCAP_EXT_OPMODE_V2_0		UCSI_DECLARE_BITFIELD_V2_0(14, 8)
318 #define   UCSI_CONCAP_EXT_OPMODE_USB4_GEN2_V2_0	UCSI_DECLARE_BITFIELD_V2_0(14, 1)
319 #define   UCSI_CONCAP_EXT_OPMODE_EPR_SRC_V2_0	UCSI_DECLARE_BITFIELD_V2_0(15, 1)
320 #define   UCSI_CONCAP_EXT_OPMODE_EPR_SINK_V2_0	UCSI_DECLARE_BITFIELD_V2_0(16, 1)
321 #define   UCSI_CONCAP_EXT_OPMODE_USB4_GEN3_V2_0	UCSI_DECLARE_BITFIELD_V2_0(17, 1)
322 #define   UCSI_CONCAP_EXT_OPMODE_USB4_GEN4_V2_0	UCSI_DECLARE_BITFIELD_V2_0(18, 1)
323 #define UCSI_CONCAP_MISC_V2_0			UCSI_DECLARE_BITFIELD_V2_0(22, 4)
324 #define   UCSI_CONCAP_MISC_FW_UPDATE_V2_0	UCSI_DECLARE_BITFIELD_V2_0(22, 1)
325 #define   UCSI_CONCAP_MISC_SECURITY_V2_0	UCSI_DECLARE_BITFIELD_V2_0(23, 1)
326 #define UCSI_CONCAP_REV_CURR_PROT_SUPPORT_V2_0	UCSI_DECLARE_BITFIELD_V2_0(26, 1)
327 #define UCSI_CONCAP_PARTNER_PD_REVISION_V2_1	UCSI_DECLARE_BITFIELD_V2_1(27, 2)
328 
329 /* Helpers for USB capability checks. */
330 #define UCSI_CONCAP_USB2_SUPPORT(_con_) UCSI_CONCAP((_con_), OPMODE_USB2)
331 #define UCSI_CONCAP_USB3_SUPPORT(_con_) UCSI_CONCAP((_con_), OPMODE_USB3)
332 #define UCSI_CONCAP_USB4_SUPPORT(_con_)					\
333 	((_con_)->ucsi->version >= UCSI_VERSION_2_0 &&			\
334 	 (UCSI_CONCAP((_con_), EXT_OPMODE_USB4_GEN2_V2_0) |		\
335 	  UCSI_CONCAP((_con_), EXT_OPMODE_USB4_GEN3_V2_0) |		\
336 	  UCSI_CONCAP((_con_), EXT_OPMODE_USB4_GEN4_V2_0)))
337 
338 /* Get Connector Status Fields. */
339 #define UCSI_CONSTAT_CHANGE			UCSI_DECLARE_BITFIELD(0, 0, 16)
340 #define UCSI_CONSTAT_PWR_OPMODE			UCSI_DECLARE_BITFIELD(0, 16, 3)
341 #define   UCSI_CONSTAT_PWR_OPMODE_NONE		0
342 #define   UCSI_CONSTAT_PWR_OPMODE_DEFAULT	1
343 #define   UCSI_CONSTAT_PWR_OPMODE_BC		2
344 #define   UCSI_CONSTAT_PWR_OPMODE_PD		3
345 #define   UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5	4
346 #define   UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0	5
347 #define UCSI_CONSTAT_CONNECTED			UCSI_DECLARE_BITFIELD(0, 19, 1)
348 #define UCSI_CONSTAT_PWR_DIR			UCSI_DECLARE_BITFIELD(0, 20, 1)
349 #define UCSI_CONSTAT_PARTNER_FLAGS		UCSI_DECLARE_BITFIELD(0, 21, 8)
350 #define   UCSI_CONSTAT_PARTNER_FLAG_USB		UCSI_DECLARE_BITFIELD(0, 21, 1)
351 #define   UCSI_CONSTAT_PARTNER_FLAG_ALT_MODE	UCSI_DECLARE_BITFIELD(0, 22, 1)
352 #define   UCSI_CONSTAT_PARTNER_FLAG_USB4_GEN3	UCSI_DECLARE_BITFIELD(0, 23, 1)
353 #define   UCSI_CONSTAT_PARTNER_FLAG_USB4_GEN4	UCSI_DECLARE_BITFIELD(0, 24, 1)
354 #define UCSI_CONSTAT_PARTNER_TYPE		UCSI_DECLARE_BITFIELD(0, 29, 3)
355 #define   UCSI_CONSTAT_PARTNER_TYPE_DFP		1
356 #define   UCSI_CONSTAT_PARTNER_TYPE_UFP		2
357 #define   UCSI_CONSTAT_PARTNER_TYPE_CABLE	3   /* Powered Cable */
358 #define   UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP 4 /* Powered Cable */
359 #define   UCSI_CONSTAT_PARTNER_TYPE_DEBUG	5
360 #define   UCSI_CONSTAT_PARTNER_TYPE_AUDIO	6
361 #define UCSI_CONSTAT_RDO			UCSI_DECLARE_BITFIELD(0, 32, 32)
362 #define UCSI_CONSTAT_BC_STATUS			UCSI_DECLARE_BITFIELD(0, 64, 2)
363 #define   UCSI_CONSTAT_BC_NOT_CHARGING		0
364 #define   UCSI_CONSTAT_BC_NOMINAL_CHARGING	1
365 #define   UCSI_CONSTAT_BC_SLOW_CHARGING		2
366 #define   UCSI_CONSTAT_BC_TRICKLE_CHARGING	3
367 #define UCSI_CONSTAT_PD_VERSION_V1_2		UCSI_DECLARE_BITFIELD_V1_2(70, 16)
368 #define UCSI_CONSTAT_ORIENTATION		UCSI_DECLARE_BITFIELD_V2_0(86, 1)
369 #define   UCSI_CONSTAT_ORIENTATION_NORMAL	0
370 #define   UCSI_CONSTAT_ORIENTATION_REVERSE	1
371 #define UCSI_CONSTAT_SINK_PATH_STATUS_V2_0	UCSI_DECLARE_BITFIELD_V2_0(87, 1)
372 #define   UCSI_CONSTAT_SINK_PATH_DISABLED   0
373 #define   UCSI_CONSTAT_SINK_PATH_ENABLED    1
374 #define UCSI_CONSTAT_PWR_READING_READY_V2_1	UCSI_DECLARE_BITFIELD_V2_1(89, 1)
375 #define UCSI_CONSTAT_CURRENT_SCALE_V2_1		UCSI_DECLARE_BITFIELD_V2_1(90, 3)
376 #define UCSI_CONSTAT_PEAK_CURRENT_V2_1		UCSI_DECLARE_BITFIELD_V2_1(93, 16)
377 #define UCSI_CONSTAT_AVG_CURRENT_V2_1		UCSI_DECLARE_BITFIELD_V2_1(109, 16)
378 #define UCSI_CONSTAT_VOLTAGE_SCALE_V2_1		UCSI_DECLARE_BITFIELD_V2_1(125, 4)
379 #define UCSI_CONSTAT_VBUS_VOLTAGE_V2_1		UCSI_DECLARE_BITFIELD_V2_1(129, 16)
380 #define UCSI_CONSTAT_CURR_SCALE_MULT		5
381 #define UCSI_CONSTAT_VOLT_SCALE_MULT		5
382 
383 /* Connector Status Change Bits.  */
384 #define UCSI_CONSTAT_EXT_SUPPLY_CHANGE		BIT(1)
385 #define UCSI_CONSTAT_POWER_OPMODE_CHANGE	BIT(2)
386 #define UCSI_CONSTAT_PDOS_CHANGE		BIT(5)
387 #define UCSI_CONSTAT_POWER_LEVEL_CHANGE		BIT(6)
388 #define UCSI_CONSTAT_PD_RESET_COMPLETE		BIT(7)
389 #define UCSI_CONSTAT_CAM_CHANGE			BIT(8)
390 #define UCSI_CONSTAT_BC_CHANGE			BIT(9)
391 #define UCSI_CONSTAT_PARTNER_CHANGE		BIT(11)
392 #define UCSI_CONSTAT_POWER_DIR_CHANGE		BIT(12)
393 #define UCSI_CONSTAT_SINK_PATH_CHANGE		BIT(13)
394 #define UCSI_CONSTAT_CONNECT_CHANGE		BIT(14)
395 #define UCSI_CONSTAT_ERROR			BIT(15)
396 
397 #define UCSI_DECLARE_BITFIELD_V1_1(_offset_, _size_)			\
398 	  UCSI_DECLARE_BITFIELD(UCSI_VERSION_1_1, (_offset_), (_size_))
399 #define UCSI_DECLARE_BITFIELD_V1_2(_offset_, _size_)			\
400 	  UCSI_DECLARE_BITFIELD(UCSI_VERSION_1_2, (_offset_), (_size_))
401 #define UCSI_DECLARE_BITFIELD_V2_0(_offset_, _size_)			\
402 	  UCSI_DECLARE_BITFIELD(UCSI_VERSION_2_0, (_offset_), (_size_))
403 #define UCSI_DECLARE_BITFIELD_V2_1(_offset_, _size_)			\
404 	  UCSI_DECLARE_BITFIELD(UCSI_VERSION_2_1, (_offset_), (_size_))
405 #define UCSI_DECLARE_BITFIELD_V3_0(_offset_, _size_)			\
406 	  UCSI_DECLARE_BITFIELD(UCSI_VERSION_3_0, (_offset_), (_size_))
407 
408 #define UCSI_DECLARE_BITFIELD(_ver_, _offset_, _size_)			\
409 (struct ucsi_bitfield) {						\
410 	.version = _ver_,						\
411 	.offset = _offset_,						\
412 	.size = _size_,							\
413 }
414 
415 struct ucsi_bitfield {
416 	const u16 version;
417 	const u8 offset;
418 	const u8 size;
419 };
420 
421 /**
422  * ucsi_bitfield_read - Read a field from UCSI command response
423  * @_map_: UCSI command response
424  * @_field_: The field offset in the response data structure
425  * @_ver_: UCSI version where the field was introduced
426  *
427  * Reads the fields in the command responses by first checking that the field is
428  * valid with the UCSI interface version that is used in the system.
429  * @_ver_ is the minimum UCSI version for the @_field_. If the UCSI interface is
430  * older than @_ver_, a warning is generated.
431  *
432  * Caveats:
433  * - Removed fields are not checked - @_ver_ is just the minimum UCSI version.
434  *
435  * Returns the value of @_field_, or 0 when the UCSI interface is older than
436  * @_ver_.
437  */
438 #define ucsi_bitfield_read(_map_, _field_, _ver_)			\
439 ({									\
440 	struct ucsi_bitfield f = (_field_);				\
441 	WARN((_ver_) < f.version,					\
442 	"Access to unsupported field at offset 0x%x (need version %04x)", \
443 	     f.offset, f.version) ? 0 :					\
444 	bitmap_read((_map_), f.offset, f.size);				\
445 })
446 
447 /* Helpers to access cached command responses. */
448 #define UCSI_CONCAP(_con_, _field_)					\
449 	ucsi_bitfield_read((_con_)->cap, UCSI_CONCAP_##_field_, (_con_)->ucsi->version)
450 
451 #define UCSI_CONSTAT(_con_, _field_)					\
452 	ucsi_bitfield_read((_con_)->status, UCSI_CONSTAT_##_field_, (_con_)->ucsi->version)
453 
454 /* -------------------------------------------------------------------------- */
455 
456 struct ucsi_debugfs_entry {
457 	u64 command;
458 	struct ucsi_data {
459 		u64 low;
460 		u64 high;
461 	} response;
462 	int status;
463 	struct dentry *dentry;
464 };
465 
466 struct ucsi {
467 	u16 version;
468 	struct device *dev;
469 	void *driver_data;
470 
471 	const struct ucsi_operations *ops;
472 
473 	struct ucsi_capability cap;
474 	struct ucsi_connector *connector;
475 	struct ucsi_debugfs_entry *debugfs;
476 
477 	struct work_struct resume_work;
478 	struct delayed_work work;
479 	int work_count;
480 #define UCSI_ROLE_SWITCH_RETRY_PER_HZ	10
481 #define UCSI_ROLE_SWITCH_INTERVAL	(HZ / UCSI_ROLE_SWITCH_RETRY_PER_HZ)
482 #define UCSI_ROLE_SWITCH_WAIT_COUNT	(10 * UCSI_ROLE_SWITCH_RETRY_PER_HZ)
483 
484 	/* PPM Communication lock */
485 	struct mutex ppm_lock;
486 
487 	/* The latest "Notification Enable" bits (SET_NOTIFICATION_ENABLE) */
488 	u64 ntfy;
489 
490 	/* PPM communication flags */
491 	unsigned long flags;
492 #define EVENT_PENDING	0
493 #define COMMAND_PENDING	1
494 #define ACK_PENDING	2
495 	struct completion complete;
496 
497 	unsigned long quirks;
498 #define UCSI_NO_PARTNER_PDOS	BIT(0)	/* Don't read partner's PDOs */
499 #define UCSI_DELAY_DEVICE_PDOS	BIT(1)	/* Reading PDOs fails until the parter is in PD mode */
500 };
501 
502 #define UCSI_MAX_DATA_LENGTH(u) (((u)->version < UCSI_VERSION_2_0) ? 0x10 : 0xff)
503 
504 #define UCSI_MAX_SVID		5
505 #define UCSI_MAX_ALTMODES	(UCSI_MAX_SVID * 6)
506 
507 #define UCSI_TYPEC_VSAFE5V		5000
508 #define UCSI_TYPEC_DEFAULT_CURRENT	 100
509 #define UCSI_TYPEC_1_5_CURRENT		1500
510 #define UCSI_TYPEC_3_0_CURRENT		3000
511 
512 struct ucsi_connector {
513 	int num;
514 
515 	struct ucsi *ucsi;
516 	struct mutex lock; /* port lock */
517 	struct work_struct work;
518 	struct completion complete;
519 	struct workqueue_struct *wq;
520 	struct list_head partner_tasks;
521 
522 	struct typec_port *port;
523 	struct typec_partner *partner;
524 	struct typec_cable *cable;
525 	struct typec_plug *plug;
526 
527 	struct typec_altmode *port_altmode[UCSI_MAX_ALTMODES];
528 	struct typec_altmode *partner_altmode[UCSI_MAX_ALTMODES];
529 	struct typec_altmode *plug_altmode[UCSI_MAX_ALTMODES];
530 
531 	struct typec_capability typec_cap;
532 
533 	/* Cached command responses. */
534 	DECLARE_BITMAP(cap, UCSI_GET_CONNECTOR_CAPABILITY_SIZE);
535 	DECLARE_BITMAP(status, UCSI_GET_CONNECTOR_STATUS_SIZE);
536 
537 	struct power_supply *psy;
538 	struct power_supply_desc psy_desc;
539 	u32 rdo;
540 	u32 src_pdos[PDO_MAX_OBJECTS];
541 	int num_pdos;
542 
543 	u32 peak_current;
544 	u32 avg_current;
545 	u32 vbus_voltage;
546 
547 	/* USB PD objects */
548 	struct usb_power_delivery *pd;
549 	struct usb_power_delivery_capabilities *port_source_caps;
550 	struct usb_power_delivery_capabilities *port_sink_caps;
551 	struct usb_power_delivery *partner_pd;
552 	struct usb_power_delivery_capabilities *partner_source_caps;
553 	struct usb_power_delivery_capabilities *partner_sink_caps;
554 
555 	struct usb_role_switch *usb_role_sw;
556 
557 	/* USB PD identity */
558 	struct usb_pd_identity partner_identity;
559 	struct usb_pd_identity cable_identity;
560 };
561 
562 int ucsi_send_command(struct ucsi *ucsi, u64 command,
563 		      void *retval, size_t size);
564 
565 void ucsi_altmode_update_active(struct ucsi_connector *con);
566 int ucsi_resume(struct ucsi *ucsi);
567 
568 void ucsi_notify_common(struct ucsi *ucsi, u32 cci);
569 int ucsi_sync_control_common(struct ucsi *ucsi, u64 command, u32 *cci,
570 			     void *data, size_t size);
571 
572 #if IS_ENABLED(CONFIG_POWER_SUPPLY)
573 int ucsi_register_port_psy(struct ucsi_connector *con);
574 void ucsi_unregister_port_psy(struct ucsi_connector *con);
575 void ucsi_port_psy_changed(struct ucsi_connector *con);
576 #else
ucsi_register_port_psy(struct ucsi_connector * con)577 static inline int ucsi_register_port_psy(struct ucsi_connector *con) { return 0; }
ucsi_unregister_port_psy(struct ucsi_connector * con)578 static inline void ucsi_unregister_port_psy(struct ucsi_connector *con) { }
ucsi_port_psy_changed(struct ucsi_connector * con)579 static inline void ucsi_port_psy_changed(struct ucsi_connector *con) { }
580 #endif /* CONFIG_POWER_SUPPLY */
581 
582 #if IS_ENABLED(CONFIG_TYPEC_DP_ALTMODE)
583 struct typec_altmode *
584 ucsi_register_displayport(struct ucsi_connector *con,
585 			  bool override, int offset,
586 			  struct typec_altmode_desc *desc);
587 
588 void ucsi_displayport_remove_partner(struct typec_altmode *adev);
589 
590 #else
591 static inline struct typec_altmode *
ucsi_register_displayport(struct ucsi_connector * con,bool override,int offset,struct typec_altmode_desc * desc)592 ucsi_register_displayport(struct ucsi_connector *con,
593 			  bool override, int offset,
594 			  struct typec_altmode_desc *desc)
595 {
596 	return typec_port_register_altmode(con->port, desc);
597 }
598 
599 static inline void
ucsi_displayport_remove_partner(struct typec_altmode * adev)600 ucsi_displayport_remove_partner(struct typec_altmode *adev) { }
601 #endif /* CONFIG_TYPEC_DP_ALTMODE */
602 
603 #if IS_ENABLED(CONFIG_TYPEC_TBT_ALTMODE)
604 struct typec_altmode *
605 ucsi_register_thunderbolt(struct ucsi_connector *con,
606 			  bool override, int offset,
607 			  struct typec_altmode_desc *desc);
608 
609 void ucsi_thunderbolt_remove_partner(struct typec_altmode *adev);
610 #else
611 static inline struct typec_altmode *
ucsi_register_thunderbolt(struct ucsi_connector * con,bool override,int offset,struct typec_altmode_desc * desc)612 ucsi_register_thunderbolt(struct ucsi_connector *con,
613 			  bool override, int offset,
614 			  struct typec_altmode_desc *desc)
615 {
616 	return typec_port_register_altmode(con->port, desc);
617 }
618 
619 static inline void
ucsi_thunderbolt_remove_partner(struct typec_altmode * adev)620 ucsi_thunderbolt_remove_partner(struct typec_altmode *adev) { }
621 #endif /* CONFIG_TYPEC_TBT_ALTMODE */
622 
623 #ifdef CONFIG_DEBUG_FS
624 void ucsi_debugfs_init(void);
625 void ucsi_debugfs_exit(void);
626 void ucsi_debugfs_register(struct ucsi *ucsi);
627 void ucsi_debugfs_unregister(struct ucsi *ucsi);
628 #else
ucsi_debugfs_init(void)629 static inline void ucsi_debugfs_init(void) { }
ucsi_debugfs_exit(void)630 static inline void ucsi_debugfs_exit(void) { }
ucsi_debugfs_register(struct ucsi * ucsi)631 static inline void ucsi_debugfs_register(struct ucsi *ucsi) { }
ucsi_debugfs_unregister(struct ucsi * ucsi)632 static inline void ucsi_debugfs_unregister(struct ucsi *ucsi) { }
633 #endif /* CONFIG_DEBUG_FS */
634 
635 /*
636  * NVIDIA VirtualLink (svid 0x955) has two altmode. VirtualLink
637  * DP mode with vdo=0x1 and NVIDIA test mode with vdo=0x3
638  */
639 #define USB_TYPEC_NVIDIA_VLINK_DP_VDO	0x1
640 #define USB_TYPEC_NVIDIA_VLINK_DBG_VDO	0x3
641 
642 #endif /* __DRIVER_USB_TYPEC_UCSI_H */
643