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