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