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