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/completion.h>
8 #include <linux/device.h>
9 #include <linux/power_supply.h>
10 #include <linux/types.h>
11 #include <linux/usb/typec.h>
12 #include <linux/usb/pd.h>
13 #include <linux/usb/role.h>
14 #include <linux/unaligned.h>
15
16 /* -------------------------------------------------------------------------- */
17
18 struct ucsi;
19 struct ucsi_altmode;
20 struct ucsi_connector;
21 struct dentry;
22
23 /* UCSI offsets (Bytes) */
24 #define UCSI_VERSION 0
25 #define UCSI_CCI 4
26 #define UCSI_CONTROL 8
27 #define UCSI_MESSAGE_IN 16
28 #define UCSI_MESSAGE_OUT 32
29 #define UCSIv2_MESSAGE_OUT 272
30
31 /* UCSI versions */
32 #define UCSI_VERSION_1_1 0x0110
33 #define UCSI_VERSION_1_2 0x0120
34 #define UCSI_VERSION_2_0 0x0200
35 #define UCSI_VERSION_2_1 0x0210
36 #define UCSI_VERSION_3_0 0x0300
37
38 #define UCSI_BCD_GET_MAJOR(_v_) (((_v_) >> 8) & 0xFF)
39 #define UCSI_BCD_GET_MINOR(_v_) (((_v_) >> 4) & 0x0F)
40 #define UCSI_BCD_GET_SUBMINOR(_v_) ((_v_) & 0x0F)
41
42 /*
43 * Per USB PD 3.2, Section 6.2.1.1.5, the spec revision is represented by 2 bits
44 * 0b00 = 1.0, 0b01 = 2.0, 0b10 = 3.0, 0b11 = Reserved, Shall NOT be used.
45 */
46 #define UCSI_SPEC_REVISION_TO_BCD(_v_) (((_v_) + 1) << 8)
47
48 /* Command Status and Connector Change Indication (CCI) bits */
49 #define UCSI_CCI_CONNECTOR(_c_) (((_c_) & GENMASK(7, 1)) >> 1)
50 #define UCSI_CCI_LENGTH(_c_) (((_c_) & GENMASK(15, 8)) >> 8)
51 #define UCSI_CCI_NOT_SUPPORTED BIT(25)
52 #define UCSI_CCI_CANCEL_COMPLETE BIT(26)
53 #define UCSI_CCI_RESET_COMPLETE BIT(27)
54 #define UCSI_CCI_BUSY BIT(28)
55 #define UCSI_CCI_ACK_COMPLETE BIT(29)
56 #define UCSI_CCI_ERROR BIT(30)
57 #define UCSI_CCI_COMMAND_COMPLETE BIT(31)
58
59 /**
60 * struct ucsi_operations - UCSI I/O operations
61 * @read_version: Read implemented UCSI version
62 * @read_cci: Read CCI register
63 * @read_message_in: Read message data from UCSI
64 * @sync_control: Blocking control operation
65 * @async_control: Non-blocking control operation
66 * @update_altmodes: Squashes duplicate DP altmodes
67 * @update_connector: Update connector capabilities before registering
68 * @connector_status: Updates connector status, called holding connector lock
69 *
70 * Read and write routines for UCSI interface. @sync_write must wait for the
71 * Command Completion Event from the PPM before returning, and @async_write must
72 * return immediately after sending the data to the PPM.
73 */
74 struct ucsi_operations {
75 int (*read_version)(struct ucsi *ucsi, u16 *version);
76 int (*read_cci)(struct ucsi *ucsi, u32 *cci);
77 int (*read_message_in)(struct ucsi *ucsi, void *val, size_t val_len);
78 int (*sync_control)(struct ucsi *ucsi, u64 command);
79 int (*async_control)(struct ucsi *ucsi, u64 command);
80 bool (*update_altmodes)(struct ucsi *ucsi, struct ucsi_altmode *orig,
81 struct ucsi_altmode *updated);
82 void (*update_connector)(struct ucsi_connector *con);
83 void (*connector_status)(struct ucsi_connector *con);
84 };
85
86 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops);
87 void ucsi_destroy(struct ucsi *ucsi);
88 int ucsi_register(struct ucsi *ucsi);
89 void ucsi_unregister(struct ucsi *ucsi);
90 void *ucsi_get_drvdata(struct ucsi *ucsi);
91 void ucsi_set_drvdata(struct ucsi *ucsi, void *data);
92
93 void ucsi_connector_change(struct ucsi *ucsi, u8 num);
94
95 /* -------------------------------------------------------------------------- */
96
97 /* Commands */
98 #define UCSI_PPM_RESET 0x01
99 #define UCSI_CANCEL 0x02
100 #define UCSI_CONNECTOR_RESET 0x03
101 #define UCSI_ACK_CC_CI 0x04
102 #define UCSI_SET_NOTIFICATION_ENABLE 0x05
103 #define UCSI_GET_CAPABILITY 0x06
104 #define UCSI_GET_CONNECTOR_CAPABILITY 0x07
105 #define UCSI_SET_UOM 0x08
106 #define UCSI_SET_UOR 0x09
107 #define UCSI_SET_PDM 0x0a
108 #define UCSI_SET_PDR 0x0b
109 #define UCSI_GET_ALTERNATE_MODES 0x0c
110 #define UCSI_GET_CAM_SUPPORTED 0x0d
111 #define UCSI_GET_CURRENT_CAM 0x0e
112 #define UCSI_SET_NEW_CAM 0x0f
113 #define UCSI_GET_PDOS 0x10
114 #define UCSI_GET_CABLE_PROPERTY 0x11
115 #define UCSI_GET_CONNECTOR_STATUS 0x12
116 #define UCSI_GET_ERROR_STATUS 0x13
117 #define UCSI_GET_PD_MESSAGE 0x15
118
119 #define UCSI_CONNECTOR_NUMBER(_num_) ((u64)(_num_) << 16)
120 #define UCSI_COMMAND(_cmd_) ((_cmd_) & 0xff)
121
122 #define UCSI_GET_ALTMODE_GET_CONNECTOR_NUMBER(_cmd_) (((_cmd_) >> 24) & GENMASK(6, 0))
123 #define UCSI_DEFAULT_GET_CONNECTOR_NUMBER(_cmd_) (((_cmd_) >> 16) & GENMASK(6, 0))
124
125 /* CONNECTOR_RESET command bits */
126 #define UCSI_CONNECTOR_RESET_HARD_VER_1_0 BIT(23) /* Deprecated in v1.1 */
127 #define UCSI_CONNECTOR_RESET_DATA_VER_2_0 BIT(23) /* Redefined in v2.0 */
128
129
130 /* ACK_CC_CI bits */
131 #define UCSI_ACK_CONNECTOR_CHANGE BIT(16)
132 #define UCSI_ACK_COMMAND_COMPLETE BIT(17)
133
134 /* SET_NOTIFICATION_ENABLE command bits */
135 #define UCSI_ENABLE_NTFY_CMD_COMPLETE BIT_ULL(16)
136 #define UCSI_ENABLE_NTFY_EXT_PWR_SRC_CHANGE BIT_ULL(17)
137 #define UCSI_ENABLE_NTFY_PWR_OPMODE_CHANGE BIT_ULL(18)
138 #define UCSI_ENABLE_NTFY_ATTENTION BIT_ULL(19)
139 #define UCSI_ENABLE_NTFY_LPM_FW_UPDATE_REQ BIT_ULL(20)
140 #define UCSI_ENABLE_NTFY_CAP_CHANGE BIT_ULL(21)
141 #define UCSI_ENABLE_NTFY_PWR_LEVEL_CHANGE BIT_ULL(22)
142 #define UCSI_ENABLE_NTFY_PD_RESET_COMPLETE BIT_ULL(23)
143 #define UCSI_ENABLE_NTFY_CAM_CHANGE BIT_ULL(24)
144 #define UCSI_ENABLE_NTFY_BAT_STATUS_CHANGE BIT_ULL(25)
145 #define UCSI_ENABLE_NTFY_SECURITY_REQ_PARTNER BIT_ULL(26)
146 #define UCSI_ENABLE_NTFY_PARTNER_CHANGE BIT_ULL(27)
147 #define UCSI_ENABLE_NTFY_PWR_DIR_CHANGE BIT_ULL(28)
148 #define UCSI_ENABLE_NTFY_SET_RETIMER_MODE BIT_ULL(29)
149 #define UCSI_ENABLE_NTFY_CONNECTOR_CHANGE BIT_ULL(30)
150 #define UCSI_ENABLE_NTFY_ERROR BIT_ULL(31)
151 #define UCSI_ENABLE_NTFY_SINK_PATH_STS_CHANGE BIT_ULL(32)
152 #define UCSI_ENABLE_NTFY_ALL 0xdbe70000
153
154 /* SET_UOR command bits */
155 #define UCSI_SET_UOR_ROLE(_r_) (((_r_) == TYPEC_HOST ? 1 : 2) << 23)
156 #define UCSI_SET_UOR_ACCEPT_ROLE_SWAPS BIT(25)
157
158 /* SET_PDF command bits */
159 #define UCSI_SET_PDR_ROLE(_r_) (((_r_) == TYPEC_SOURCE ? 1 : 2) << 23)
160 #define UCSI_SET_PDR_ACCEPT_ROLE_SWAPS BIT(25)
161
162 /* GET_ALTERNATE_MODES command bits */
163 #define UCSI_ALTMODE_RECIPIENT(_r_) (((_r_) >> 16) & 0x7)
164 #define UCSI_GET_ALTMODE_RECIPIENT(_r_) ((u64)(_r_) << 16)
165 #define UCSI_RECIPIENT_CON 0
166 #define UCSI_RECIPIENT_SOP 1
167 #define UCSI_RECIPIENT_SOP_P 2
168 #define UCSI_RECIPIENT_SOP_PP 3
169 #define UCSI_GET_ALTMODE_CONNECTOR_NUMBER(_r_) ((u64)(_r_) << 24)
170 #define UCSI_ALTMODE_OFFSET(_r_) (((_r_) >> 32) & 0xff)
171 #define UCSI_GET_ALTMODE_OFFSET(_r_) ((u64)(_r_) << 32)
172 #define UCSI_GET_ALTMODE_NUM_ALTMODES(_r_) ((u64)(_r_) << 40)
173
174 /* GET_PDOS command bits */
175 #define UCSI_GET_PDOS_PARTNER_PDO(_r_) ((u64)(_r_) << 23)
176 #define UCSI_GET_PDOS_PDO_OFFSET(_r_) ((u64)(_r_) << 24)
177 #define UCSI_GET_PDOS_NUM_PDOS(_r_) ((u64)(_r_) << 32)
178 #define UCSI_MAX_PDOS (4)
179 #define UCSI_GET_PDOS_SRC_PDOS ((u64)1 << 34)
180
181 /* GET_PD_MESSAGE command bits */
182 #define UCSI_GET_PD_MESSAGE_RECIPIENT(_r_) ((u64)(_r_) << 23)
183 #define UCSI_GET_PD_MESSAGE_OFFSET(_r_) ((u64)(_r_) << 26)
184 #define UCSI_GET_PD_MESSAGE_BYTES(_r_) ((u64)(_r_) << 34)
185 #define UCSI_GET_PD_MESSAGE_TYPE(_r_) ((u64)(_r_) << 42)
186 #define UCSI_GET_PD_MESSAGE_TYPE_SNK_CAP_EXT 0
187 #define UCSI_GET_PD_MESSAGE_TYPE_SRC_CAP_EXT 1
188 #define UCSI_GET_PD_MESSAGE_TYPE_BAT_CAP 2
189 #define UCSI_GET_PD_MESSAGE_TYPE_BAT_STAT 3
190 #define UCSI_GET_PD_MESSAGE_TYPE_IDENTITY 4
191 #define UCSI_GET_PD_MESSAGE_TYPE_REVISION 5
192
193 /* -------------------------------------------------------------------------- */
194
195 /* Error information returned by PPM in response to GET_ERROR_STATUS command. */
196 #define UCSI_ERROR_UNREGONIZED_CMD BIT(0)
197 #define UCSI_ERROR_INVALID_CON_NUM BIT(1)
198 #define UCSI_ERROR_INVALID_CMD_ARGUMENT BIT(2)
199 #define UCSI_ERROR_INCOMPATIBLE_PARTNER BIT(3)
200 #define UCSI_ERROR_CC_COMMUNICATION_ERR BIT(4)
201 #define UCSI_ERROR_DEAD_BATTERY BIT(5)
202 #define UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL BIT(6)
203 #define UCSI_ERROR_OVERCURRENT BIT(7)
204 #define UCSI_ERROR_UNDEFINED BIT(8)
205 #define UCSI_ERROR_PARTNER_REJECTED_SWAP BIT(9)
206 #define UCSI_ERROR_HARD_RESET BIT(10)
207 #define UCSI_ERROR_PPM_POLICY_CONFLICT BIT(11)
208 #define UCSI_ERROR_SWAP_REJECTED BIT(12)
209 #define UCSI_ERROR_REVERSE_CURRENT_PROTECTION BIT(13)
210 #define UCSI_ERROR_SET_SINK_PATH_REJECTED BIT(14)
211
212 #define UCSI_SET_NEW_CAM_ENTER(x) (((x) >> 23) & 0x1)
213 #define UCSI_SET_NEW_CAM_GET_AM(x) (((x) >> 24) & 0xff)
214 #define UCSI_SET_NEW_CAM_AM_MASK (0xff << 24)
215 #define UCSI_SET_NEW_CAM_SET_AM(x) (((x) & 0xff) << 24)
216 #define UCSI_CMD_CONNECTOR_MASK (0x7)
217
218 /* Data structure filled by PPM in response to GET_CAPABILITY command. */
219 struct ucsi_capability {
220 u32 attributes;
221 #define UCSI_CAP_ATTR_DISABLE_STATE BIT(0)
222 #define UCSI_CAP_ATTR_BATTERY_CHARGING BIT(1)
223 #define UCSI_CAP_ATTR_USB_PD BIT(2)
224 #define UCSI_CAP_ATTR_TYPEC_CURRENT BIT(6)
225 #define UCSI_CAP_ATTR_POWER_AC_SUPPLY BIT(8)
226 #define UCSI_CAP_ATTR_POWER_OTHER BIT(10)
227 #define UCSI_CAP_ATTR_POWER_VBUS BIT(14)
228 u8 num_connectors;
229 u16 features;
230 #define UCSI_CAP_SET_UOM BIT(0)
231 #define UCSI_CAP_SET_PDM BIT(1)
232 #define UCSI_CAP_ALT_MODE_DETAILS BIT(2)
233 #define UCSI_CAP_ALT_MODE_OVERRIDE BIT(3)
234 #define UCSI_CAP_PDO_DETAILS BIT(4)
235 #define UCSI_CAP_CABLE_DETAILS BIT(5)
236 #define UCSI_CAP_EXT_SUPPLY_NOTIFICATIONS BIT(6)
237 #define UCSI_CAP_PD_RESET BIT(7)
238 #define UCSI_CAP_GET_PD_MESSAGE BIT(8)
239 #define UCSI_CAP_GET_ATTENTION_VDO BIT(9)
240 #define UCSI_CAP_FW_UPDATE_REQUEST BIT(10)
241 #define UCSI_CAP_NEGOTIATED_PWR_LEVEL_CHANGE BIT(11)
242 #define UCSI_CAP_SECURITY_REQUEST BIT(12)
243 #define UCSI_CAP_SET_RETIMER_MODE BIT(13)
244 #define UCSI_CAP_CHUNKING_SUPPORT BIT(14)
245 u8 reserved_1;
246 u8 num_alt_modes;
247 u8 reserved_2;
248 u16 bc_version;
249 u16 pd_version;
250 u16 typec_version;
251 } __packed;
252
253 /* Data structure filled by PPM in response to GET_CONNECTOR_CAPABILITY cmd. */
254 struct ucsi_connector_capability {
255 u8 op_mode;
256 #define UCSI_CONCAP_OPMODE_DFP BIT(0)
257 #define UCSI_CONCAP_OPMODE_UFP BIT(1)
258 #define UCSI_CONCAP_OPMODE_DRP BIT(2)
259 #define UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY BIT(3)
260 #define UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY BIT(4)
261 #define UCSI_CONCAP_OPMODE_USB2 BIT(5)
262 #define UCSI_CONCAP_OPMODE_USB3 BIT(6)
263 #define UCSI_CONCAP_OPMODE_ALT_MODE BIT(7)
264 u32 flags;
265 #define UCSI_CONCAP_FLAG_PROVIDER BIT(0)
266 #define UCSI_CONCAP_FLAG_CONSUMER BIT(1)
267 #define UCSI_CONCAP_FLAG_SWAP_TO_DFP BIT(2)
268 #define UCSI_CONCAP_FLAG_SWAP_TO_UFP BIT(3)
269 #define UCSI_CONCAP_FLAG_SWAP_TO_SRC BIT(4)
270 #define UCSI_CONCAP_FLAG_SWAP_TO_SINK BIT(5)
271 #define UCSI_CONCAP_FLAG_EX_OP_MODE(_f_) \
272 (((_f_) & GENMASK(13, 6)) >> 6)
273 #define UCSI_CONCAP_EX_OP_MODE_USB4_GEN2 BIT(0)
274 #define UCSI_CONCAP_EX_OP_MODE_EPR_SRC BIT(1)
275 #define UCSI_CONCAP_EX_OP_MODE_EPR_SINK BIT(2)
276 #define UCSI_CONCAP_EX_OP_MODE_USB4_GEN3 BIT(3)
277 #define UCSI_CONCAP_EX_OP_MODE_USB4_GEN4 BIT(4)
278 #define UCSI_CONCAP_FLAG_MISC_CAPS(_f_) \
279 (((_f_) & GENMASK(17, 14)) >> 14)
280 #define UCSI_CONCAP_MISC_CAP_FW_UPDATE BIT(0)
281 #define UCSI_CONCAP_MISC_CAP_SECURITY BIT(1)
282 #define UCSI_CONCAP_FLAG_REV_CURR_PROT_SUPPORT BIT(18)
283 #define UCSI_CONCAP_FLAG_PARTNER_PD_MAJOR_REV(_f_) \
284 (((_f_) & GENMASK(20, 19)) >> 19)
285 #define UCSI_CONCAP_FLAG_PARTNER_PD_MAJOR_REV_AS_BCD(_f_) \
286 UCSI_SPEC_REVISION_TO_BCD(UCSI_CONCAP_FLAG_PARTNER_PD_MAJOR_REV(_f_))
287 } __packed;
288
289 struct ucsi_altmode {
290 u16 svid;
291 u32 mid;
292 } __packed;
293
294 /* Data structure filled by PPM in response to GET_CABLE_PROPERTY command. */
295 struct ucsi_cable_property {
296 u16 speed_supported;
297 u8 current_capability;
298 u8 flags;
299 #define UCSI_CABLE_PROP_FLAG_VBUS_IN_CABLE BIT(0)
300 #define UCSI_CABLE_PROP_FLAG_ACTIVE_CABLE BIT(1)
301 #define UCSI_CABLE_PROP_FLAG_DIRECTIONALITY BIT(2)
302 #define UCSI_CABLE_PROP_FLAG_PLUG_TYPE(_f_) (((_f_) & GENMASK(4, 3)) >> 3)
303 #define UCSI_CABLE_PROPERTY_PLUG_TYPE_A 0
304 #define UCSI_CABLE_PROPERTY_PLUG_TYPE_B 1
305 #define UCSI_CABLE_PROPERTY_PLUG_TYPE_C 2
306 #define UCSI_CABLE_PROPERTY_PLUG_OTHER 3
307 #define UCSI_CABLE_PROP_FLAG_MODE_SUPPORT BIT(5)
308 #define UCSI_CABLE_PROP_FLAG_PD_MAJOR_REV(_f_) (((_f_) & GENMASK(7, 6)) >> 6)
309 #define UCSI_CABLE_PROP_FLAG_PD_MAJOR_REV_AS_BCD(_f_) \
310 UCSI_SPEC_REVISION_TO_BCD(UCSI_CABLE_PROP_FLAG_PD_MAJOR_REV(_f_))
311 u8 latency;
312 } __packed;
313
314 /* Data structure filled by PPM in response to GET_CONNECTOR_STATUS command. */
315 struct ucsi_connector_status {
316 u16 change;
317 #define UCSI_CONSTAT_EXT_SUPPLY_CHANGE BIT(1)
318 #define UCSI_CONSTAT_POWER_OPMODE_CHANGE BIT(2)
319 #define UCSI_CONSTAT_PDOS_CHANGE BIT(5)
320 #define UCSI_CONSTAT_POWER_LEVEL_CHANGE BIT(6)
321 #define UCSI_CONSTAT_PD_RESET_COMPLETE BIT(7)
322 #define UCSI_CONSTAT_CAM_CHANGE BIT(8)
323 #define UCSI_CONSTAT_BC_CHANGE BIT(9)
324 #define UCSI_CONSTAT_PARTNER_CHANGE BIT(11)
325 #define UCSI_CONSTAT_POWER_DIR_CHANGE BIT(12)
326 #define UCSI_CONSTAT_CONNECT_CHANGE BIT(14)
327 #define UCSI_CONSTAT_ERROR BIT(15)
328 u16 flags;
329 #define UCSI_CONSTAT_PWR_OPMODE(_f_) ((_f_) & GENMASK(2, 0))
330 #define UCSI_CONSTAT_PWR_OPMODE_NONE 0
331 #define UCSI_CONSTAT_PWR_OPMODE_DEFAULT 1
332 #define UCSI_CONSTAT_PWR_OPMODE_BC 2
333 #define UCSI_CONSTAT_PWR_OPMODE_PD 3
334 #define UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5 4
335 #define UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0 5
336 #define UCSI_CONSTAT_CONNECTED BIT(3)
337 #define UCSI_CONSTAT_PWR_DIR BIT(4)
338 #define UCSI_CONSTAT_PARTNER_FLAGS(_f_) (((_f_) & GENMASK(12, 5)) >> 5)
339 #define UCSI_CONSTAT_PARTNER_FLAG_USB 1
340 #define UCSI_CONSTAT_PARTNER_FLAG_ALT_MODE 2
341 #define UCSI_CONSTAT_PARTNER_TYPE(_f_) (((_f_) & GENMASK(15, 13)) >> 13)
342 #define UCSI_CONSTAT_PARTNER_TYPE_DFP 1
343 #define UCSI_CONSTAT_PARTNER_TYPE_UFP 2
344 #define UCSI_CONSTAT_PARTNER_TYPE_CABLE 3 /* Powered Cable */
345 #define UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP 4 /* Powered Cable */
346 #define UCSI_CONSTAT_PARTNER_TYPE_DEBUG 5
347 #define UCSI_CONSTAT_PARTNER_TYPE_AUDIO 6
348 u32 request_data_obj;
349
350 u8 pwr_status;
351 #define UCSI_CONSTAT_BC_STATUS(_p_) ((_p_) & GENMASK(1, 0))
352 #define UCSI_CONSTAT_BC_NOT_CHARGING 0
353 #define UCSI_CONSTAT_BC_NOMINAL_CHARGING 1
354 #define UCSI_CONSTAT_BC_SLOW_CHARGING 2
355 #define UCSI_CONSTAT_BC_TRICKLE_CHARGING 3
356 } __packed;
357
358 /* -------------------------------------------------------------------------- */
359
360 struct ucsi_debugfs_entry {
361 u64 command;
362 struct ucsi_data {
363 u64 low;
364 u64 high;
365 } response;
366 u32 status;
367 struct dentry *dentry;
368 };
369
370 struct ucsi {
371 u16 version;
372 struct device *dev;
373 void *driver_data;
374
375 const struct ucsi_operations *ops;
376
377 struct ucsi_capability cap;
378 struct ucsi_connector *connector;
379 struct ucsi_debugfs_entry *debugfs;
380
381 struct work_struct resume_work;
382 struct delayed_work work;
383 int work_count;
384 #define UCSI_ROLE_SWITCH_RETRY_PER_HZ 10
385 #define UCSI_ROLE_SWITCH_INTERVAL (HZ / UCSI_ROLE_SWITCH_RETRY_PER_HZ)
386 #define UCSI_ROLE_SWITCH_WAIT_COUNT (10 * UCSI_ROLE_SWITCH_RETRY_PER_HZ)
387
388 /* PPM Communication lock */
389 struct mutex ppm_lock;
390
391 /* The latest "Notification Enable" bits (SET_NOTIFICATION_ENABLE) */
392 u64 ntfy;
393
394 /* PPM communication flags */
395 unsigned long flags;
396 #define EVENT_PENDING 0
397 #define COMMAND_PENDING 1
398 #define ACK_PENDING 2
399 struct completion complete;
400
401 unsigned long quirks;
402 #define UCSI_NO_PARTNER_PDOS BIT(0) /* Don't read partner's PDOs */
403 #define UCSI_DELAY_DEVICE_PDOS BIT(1) /* Reading PDOs fails until the parter is in PD mode */
404 };
405
406 #define UCSI_MAX_DATA_LENGTH(u) (((u)->version < UCSI_VERSION_2_0) ? 0x10 : 0xff)
407
408 #define UCSI_MAX_SVID 5
409 #define UCSI_MAX_ALTMODES (UCSI_MAX_SVID * 6)
410
411 #define UCSI_TYPEC_VSAFE5V 5000
412 #define UCSI_TYPEC_1_5_CURRENT 1500
413 #define UCSI_TYPEC_3_0_CURRENT 3000
414
415 struct ucsi_connector {
416 int num;
417
418 struct ucsi *ucsi;
419 struct mutex lock; /* port lock */
420 struct work_struct work;
421 struct completion complete;
422 struct workqueue_struct *wq;
423 struct list_head partner_tasks;
424
425 struct typec_port *port;
426 struct typec_partner *partner;
427 struct typec_cable *cable;
428 struct typec_plug *plug;
429
430 struct typec_altmode *port_altmode[UCSI_MAX_ALTMODES];
431 struct typec_altmode *partner_altmode[UCSI_MAX_ALTMODES];
432 struct typec_altmode *plug_altmode[UCSI_MAX_ALTMODES];
433
434 struct typec_capability typec_cap;
435
436 struct ucsi_connector_status status;
437 struct ucsi_connector_capability cap;
438 struct power_supply *psy;
439 struct power_supply_desc psy_desc;
440 u32 rdo;
441 u32 src_pdos[PDO_MAX_OBJECTS];
442 int num_pdos;
443
444 /* USB PD objects */
445 struct usb_power_delivery *pd;
446 struct usb_power_delivery_capabilities *port_source_caps;
447 struct usb_power_delivery_capabilities *port_sink_caps;
448 struct usb_power_delivery *partner_pd;
449 struct usb_power_delivery_capabilities *partner_source_caps;
450 struct usb_power_delivery_capabilities *partner_sink_caps;
451
452 struct usb_role_switch *usb_role_sw;
453
454 /* USB PD identity */
455 struct usb_pd_identity partner_identity;
456 struct usb_pd_identity cable_identity;
457 };
458
459 int ucsi_send_command(struct ucsi *ucsi, u64 command,
460 void *retval, size_t size);
461
462 void ucsi_altmode_update_active(struct ucsi_connector *con);
463 int ucsi_resume(struct ucsi *ucsi);
464
465 void ucsi_notify_common(struct ucsi *ucsi, u32 cci);
466 int ucsi_sync_control_common(struct ucsi *ucsi, u64 command);
467
468 #if IS_ENABLED(CONFIG_POWER_SUPPLY)
469 int ucsi_register_port_psy(struct ucsi_connector *con);
470 void ucsi_unregister_port_psy(struct ucsi_connector *con);
471 void ucsi_port_psy_changed(struct ucsi_connector *con);
472 #else
ucsi_register_port_psy(struct ucsi_connector * con)473 static inline int ucsi_register_port_psy(struct ucsi_connector *con) { return 0; }
ucsi_unregister_port_psy(struct ucsi_connector * con)474 static inline void ucsi_unregister_port_psy(struct ucsi_connector *con) { }
ucsi_port_psy_changed(struct ucsi_connector * con)475 static inline void ucsi_port_psy_changed(struct ucsi_connector *con) { }
476 #endif /* CONFIG_POWER_SUPPLY */
477
478 #if IS_ENABLED(CONFIG_TYPEC_DP_ALTMODE)
479 struct typec_altmode *
480 ucsi_register_displayport(struct ucsi_connector *con,
481 bool override, int offset,
482 struct typec_altmode_desc *desc);
483
484 void ucsi_displayport_remove_partner(struct typec_altmode *adev);
485
486 #else
487 static inline struct typec_altmode *
ucsi_register_displayport(struct ucsi_connector * con,bool override,int offset,struct typec_altmode_desc * desc)488 ucsi_register_displayport(struct ucsi_connector *con,
489 bool override, int offset,
490 struct typec_altmode_desc *desc)
491 {
492 return typec_port_register_altmode(con->port, desc);
493 }
494
495 static inline void
ucsi_displayport_remove_partner(struct typec_altmode * adev)496 ucsi_displayport_remove_partner(struct typec_altmode *adev) { }
497 #endif /* CONFIG_TYPEC_DP_ALTMODE */
498
499 #ifdef CONFIG_DEBUG_FS
500 void ucsi_debugfs_init(void);
501 void ucsi_debugfs_exit(void);
502 void ucsi_debugfs_register(struct ucsi *ucsi);
503 void ucsi_debugfs_unregister(struct ucsi *ucsi);
504 #else
ucsi_debugfs_init(void)505 static inline void ucsi_debugfs_init(void) { }
ucsi_debugfs_exit(void)506 static inline void ucsi_debugfs_exit(void) { }
ucsi_debugfs_register(struct ucsi * ucsi)507 static inline void ucsi_debugfs_register(struct ucsi *ucsi) { }
ucsi_debugfs_unregister(struct ucsi * ucsi)508 static inline void ucsi_debugfs_unregister(struct ucsi *ucsi) { }
509 #endif /* CONFIG_DEBUG_FS */
510
511 /*
512 * NVIDIA VirtualLink (svid 0x955) has two altmode. VirtualLink
513 * DP mode with vdo=0x1 and NVIDIA test mode with vdo=0x3
514 */
515 #define USB_TYPEC_NVIDIA_VLINK_DP_VDO 0x1
516 #define USB_TYPEC_NVIDIA_VLINK_DBG_VDO 0x3
517
518 #endif /* __DRIVER_USB_TYPEC_UCSI_H */
519