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