Merge remote-tracking branches 'asoc/topic/rk3036', 'asoc/topic/rockchip', 'asoc...
[sfrench/cifs-2.6.git] / drivers / usb / typec / ucsi / ucsi.h
1
2 #ifndef __DRIVER_USB_TYPEC_UCSI_H
3 #define __DRIVER_USB_TYPEC_UCSI_H
4
5 #include <linux/bitops.h>
6 #include <linux/device.h>
7 #include <linux/types.h>
8
9 /* -------------------------------------------------------------------------- */
10
11 /* Command Status and Connector Change Indication (CCI) data structure */
12 struct ucsi_cci {
13         u8:1; /* reserved */
14         u8 connector_change:7;
15         u8 data_length;
16         u16:9; /* reserved */
17         u16 not_supported:1;
18         u16 cancel_complete:1;
19         u16 reset_complete:1;
20         u16 busy:1;
21         u16 ack_complete:1;
22         u16 error:1;
23         u16 cmd_complete:1;
24 } __packed;
25
26 /* Default fields in CONTROL data structure */
27 struct ucsi_command {
28         u8 cmd;
29         u8 length;
30         u64 data:48;
31 } __packed;
32
33 /* ACK Command structure */
34 struct ucsi_ack_cmd {
35         u8 cmd;
36         u8 length;
37         u8 cci_ack:1;
38         u8 cmd_ack:1;
39         u8:6; /* reserved */
40 } __packed;
41
42 /* Connector Reset Command structure */
43 struct ucsi_con_rst {
44         u8 cmd;
45         u8 length;
46         u8 con_num:7;
47         u8 hard_reset:1;
48 } __packed;
49
50 /* Set USB Operation Mode Command structure */
51 struct ucsi_uor_cmd {
52         u8 cmd;
53         u8 length;
54         u16 con_num:7;
55         u16 role:3;
56 #define UCSI_UOR_ROLE_DFP                       BIT(0)
57 #define UCSI_UOR_ROLE_UFP                       BIT(1)
58 #define UCSI_UOR_ROLE_DRP                       BIT(2)
59         u16:6; /* reserved */
60 } __packed;
61
62 struct ucsi_control {
63         union {
64                 u64 raw_cmd;
65                 struct ucsi_command cmd;
66                 struct ucsi_uor_cmd uor;
67                 struct ucsi_ack_cmd ack;
68                 struct ucsi_con_rst con_rst;
69         };
70 };
71
72 #define __UCSI_CMD(_ctrl_, _cmd_)                                       \
73 {                                                                       \
74         (_ctrl_).raw_cmd = 0;                                           \
75         (_ctrl_).cmd.cmd = _cmd_;                                       \
76 }
77
78 /* Helper for preparing ucsi_control for CONNECTOR_RESET command. */
79 #define UCSI_CMD_CONNECTOR_RESET(_ctrl_, _con_, _hard_)                 \
80 {                                                                       \
81         __UCSI_CMD(_ctrl_, UCSI_CONNECTOR_RESET)                        \
82         (_ctrl_).con_rst.con_num = (_con_)->num;                        \
83         (_ctrl_).con_rst.hard_reset = _hard_;                           \
84 }
85
86 /* Helper for preparing ucsi_control for ACK_CC_CI command. */
87 #define UCSI_CMD_ACK(_ctrl_, _ack_)                                     \
88 {                                                                       \
89         __UCSI_CMD(_ctrl_, UCSI_ACK_CC_CI)                              \
90         (_ctrl_).ack.cci_ack = ((_ack_) == UCSI_ACK_EVENT);             \
91         (_ctrl_).ack.cmd_ack = ((_ack_) == UCSI_ACK_CMD);               \
92 }
93
94 /* Helper for preparing ucsi_control for SET_NOTIFY_ENABLE command. */
95 #define UCSI_CMD_SET_NTFY_ENABLE(_ctrl_, _ntfys_)                       \
96 {                                                                       \
97         __UCSI_CMD(_ctrl_, UCSI_SET_NOTIFICATION_ENABLE)                \
98         (_ctrl_).cmd.data = _ntfys_;                                    \
99 }
100
101 /* Helper for preparing ucsi_control for GET_CAPABILITY command. */
102 #define UCSI_CMD_GET_CAPABILITY(_ctrl_)                                 \
103 {                                                                       \
104         __UCSI_CMD(_ctrl_, UCSI_GET_CAPABILITY)                         \
105 }
106
107 /* Helper for preparing ucsi_control for GET_CONNECTOR_CAPABILITY command. */
108 #define UCSI_CMD_GET_CONNECTOR_CAPABILITY(_ctrl_, _con_)                \
109 {                                                                       \
110         __UCSI_CMD(_ctrl_, UCSI_GET_CONNECTOR_CAPABILITY)               \
111         (_ctrl_).cmd.data = _con_;                                      \
112 }
113
114 /* Helper for preparing ucsi_control for GET_CONNECTOR_STATUS command. */
115 #define UCSI_CMD_GET_CONNECTOR_STATUS(_ctrl_, _con_)                    \
116 {                                                                       \
117         __UCSI_CMD(_ctrl_, UCSI_GET_CONNECTOR_STATUS)                   \
118         (_ctrl_).cmd.data = _con_;                                      \
119 }
120
121 #define __UCSI_ROLE(_ctrl_, _cmd_, _con_num_)                           \
122 {                                                                       \
123         __UCSI_CMD(_ctrl_, _cmd_)                                       \
124         (_ctrl_).uor.con_num = _con_num_;                               \
125         (_ctrl_).uor.role = UCSI_UOR_ROLE_DRP;                          \
126 }
127
128 /* Helper for preparing ucsi_control for SET_UOR command. */
129 #define UCSI_CMD_SET_UOR(_ctrl_, _con_, _role_)                         \
130 {                                                                       \
131         __UCSI_ROLE(_ctrl_, UCSI_SET_UOR, (_con_)->num)         \
132         (_ctrl_).uor.role |= (_role_) == TYPEC_HOST ? UCSI_UOR_ROLE_DFP : \
133                           UCSI_UOR_ROLE_UFP;                            \
134 }
135
136 /* Helper for preparing ucsi_control for SET_PDR command. */
137 #define UCSI_CMD_SET_PDR(_ctrl_, _con_, _role_)                 \
138 {                                                                       \
139         __UCSI_ROLE(_ctrl_, UCSI_SET_PDR, (_con_)->num)         \
140         (_ctrl_).uor.role |= (_role_) == TYPEC_SOURCE ? UCSI_UOR_ROLE_DFP : \
141                         UCSI_UOR_ROLE_UFP;                              \
142 }
143
144 /* Commands */
145 #define UCSI_PPM_RESET                  0x01
146 #define UCSI_CANCEL                     0x02
147 #define UCSI_CONNECTOR_RESET            0x03
148 #define UCSI_ACK_CC_CI                  0x04
149 #define UCSI_SET_NOTIFICATION_ENABLE    0x05
150 #define UCSI_GET_CAPABILITY             0x06
151 #define UCSI_GET_CONNECTOR_CAPABILITY   0x07
152 #define UCSI_SET_UOM                    0x08
153 #define UCSI_SET_UOR                    0x09
154 #define UCSI_SET_PDM                    0x0a
155 #define UCSI_SET_PDR                    0x0b
156 #define UCSI_GET_ALTERNATE_MODES        0x0c
157 #define UCSI_GET_CAM_SUPPORTED          0x0d
158 #define UCSI_GET_CURRENT_CAM            0x0e
159 #define UCSI_SET_NEW_CAM                0x0f
160 #define UCSI_GET_PDOS                   0x10
161 #define UCSI_GET_CABLE_PROPERTY         0x11
162 #define UCSI_GET_CONNECTOR_STATUS       0x12
163 #define UCSI_GET_ERROR_STATUS           0x13
164
165 /* ACK_CC_CI commands */
166 #define UCSI_ACK_EVENT                  1
167 #define UCSI_ACK_CMD                    2
168
169 /* Bits for SET_NOTIFICATION_ENABLE command */
170 #define UCSI_ENABLE_NTFY_CMD_COMPLETE           BIT(0)
171 #define UCSI_ENABLE_NTFY_EXT_PWR_SRC_CHANGE     BIT(1)
172 #define UCSI_ENABLE_NTFY_PWR_OPMODE_CHANGE      BIT(2)
173 #define UCSI_ENABLE_NTFY_CAP_CHANGE             BIT(5)
174 #define UCSI_ENABLE_NTFY_PWR_LEVEL_CHANGE       BIT(6)
175 #define UCSI_ENABLE_NTFY_PD_RESET_COMPLETE      BIT(7)
176 #define UCSI_ENABLE_NTFY_CAM_CHANGE             BIT(8)
177 #define UCSI_ENABLE_NTFY_BAT_STATUS_CHANGE      BIT(9)
178 #define UCSI_ENABLE_NTFY_PARTNER_CHANGE         BIT(11)
179 #define UCSI_ENABLE_NTFY_PWR_DIR_CHANGE         BIT(12)
180 #define UCSI_ENABLE_NTFY_CONNECTOR_CHANGE       BIT(14)
181 #define UCSI_ENABLE_NTFY_ERROR                  BIT(15)
182 #define UCSI_ENABLE_NTFY_ALL                    0xdbe7
183
184 /* Error information returned by PPM in response to GET_ERROR_STATUS command. */
185 #define UCSI_ERROR_UNREGONIZED_CMD              BIT(0)
186 #define UCSI_ERROR_INVALID_CON_NUM              BIT(1)
187 #define UCSI_ERROR_INVALID_CMD_ARGUMENT         BIT(2)
188 #define UCSI_ERROR_INCOMPATIBLE_PARTNER         BIT(3)
189 #define UCSI_ERROR_CC_COMMUNICATION_ERR         BIT(4)
190 #define UCSI_ERROR_DEAD_BATTERY                 BIT(5)
191 #define UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL    BIT(6)
192
193 /* Data structure filled by PPM in response to GET_CAPABILITY command. */
194 struct ucsi_capability {
195         u32 attributes;
196 #define UCSI_CAP_ATTR_DISABLE_STATE             BIT(0)
197 #define UCSI_CAP_ATTR_BATTERY_CHARGING          BIT(1)
198 #define UCSI_CAP_ATTR_USB_PD                    BIT(2)
199 #define UCSI_CAP_ATTR_TYPEC_CURRENT             BIT(6)
200 #define UCSI_CAP_ATTR_POWER_AC_SUPPLY           BIT(8)
201 #define UCSI_CAP_ATTR_POWER_OTHER               BIT(10)
202 #define UCSI_CAP_ATTR_POWER_VBUS                BIT(14)
203         u32 num_connectors:8;
204         u32 features:24;
205 #define UCSI_CAP_SET_UOM                        BIT(0)
206 #define UCSI_CAP_SET_PDM                        BIT(1)
207 #define UCSI_CAP_ALT_MODE_DETAILS               BIT(2)
208 #define UCSI_CAP_ALT_MODE_OVERRIDE              BIT(3)
209 #define UCSI_CAP_PDO_DETAILS                    BIT(4)
210 #define UCSI_CAP_CABLE_DETAILS                  BIT(5)
211 #define UCSI_CAP_EXT_SUPPLY_NOTIFICATIONS       BIT(6)
212 #define UCSI_CAP_PD_RESET                       BIT(7)
213         u8 num_alt_modes;
214         u8 reserved;
215         u16 bc_version;
216         u16 pd_version;
217         u16 typec_version;
218 } __packed;
219
220 /* Data structure filled by PPM in response to GET_CONNECTOR_CAPABILITY cmd. */
221 struct ucsi_connector_capability {
222         u8 op_mode;
223 #define UCSI_CONCAP_OPMODE_DFP                  BIT(0)
224 #define UCSI_CONCAP_OPMODE_UFP                  BIT(1)
225 #define UCSI_CONCAP_OPMODE_DRP                  BIT(2)
226 #define UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY      BIT(3)
227 #define UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY      BIT(4)
228 #define UCSI_CONCAP_OPMODE_USB2                 BIT(5)
229 #define UCSI_CONCAP_OPMODE_USB3                 BIT(6)
230 #define UCSI_CONCAP_OPMODE_ALT_MODE             BIT(7)
231         u8 provider:1;
232         u8 consumer:1;
233         u8:6; /* reserved */
234 } __packed;
235
236 struct ucsi_altmode {
237         u16 svid;
238         u32 mid;
239 } __packed;
240
241 /* Data structure filled by PPM in response to GET_CABLE_PROPERTY command. */
242 struct ucsi_cable_property {
243         u16 speed_supported;
244         u8 current_capability;
245         u8 vbus_in_cable:1;
246         u8 active_cable:1;
247         u8 directionality:1;
248         u8 plug_type:2;
249 #define UCSI_CABLE_PROPERTY_PLUG_TYPE_A         0
250 #define UCSI_CABLE_PROPERTY_PLUG_TYPE_B         1
251 #define UCSI_CABLE_PROPERTY_PLUG_TYPE_C         2
252 #define UCSI_CABLE_PROPERTY_PLUG_OTHER          3
253         u8 mode_support:1;
254         u8:2; /* reserved */
255         u8 latency:4;
256         u8:4; /* reserved */
257 } __packed;
258
259 /* Data structure filled by PPM in response to GET_CONNECTOR_STATUS command. */
260 struct ucsi_connector_status {
261         u16 change;
262 #define UCSI_CONSTAT_EXT_SUPPLY_CHANGE          BIT(1)
263 #define UCSI_CONSTAT_POWER_OPMODE_CHANGE        BIT(2)
264 #define UCSI_CONSTAT_PDOS_CHANGE                BIT(5)
265 #define UCSI_CONSTAT_POWER_LEVEL_CHANGE         BIT(6)
266 #define UCSI_CONSTAT_PD_RESET_COMPLETE          BIT(7)
267 #define UCSI_CONSTAT_CAM_CHANGE                 BIT(8)
268 #define UCSI_CONSTAT_BC_CHANGE                  BIT(9)
269 #define UCSI_CONSTAT_PARTNER_CHANGE             BIT(11)
270 #define UCSI_CONSTAT_POWER_DIR_CHANGE           BIT(12)
271 #define UCSI_CONSTAT_CONNECT_CHANGE             BIT(14)
272 #define UCSI_CONSTAT_ERROR                      BIT(15)
273         u16 pwr_op_mode:3;
274 #define UCSI_CONSTAT_PWR_OPMODE_NONE            0
275 #define UCSI_CONSTAT_PWR_OPMODE_DEFAULT         1
276 #define UCSI_CONSTAT_PWR_OPMODE_BC              2
277 #define UCSI_CONSTAT_PWR_OPMODE_PD              3
278 #define UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5        4
279 #define UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0        5
280         u16 connected:1;
281         u16 pwr_dir:1;
282         u16 partner_flags:8;
283 #define UCSI_CONSTAT_PARTNER_FLAG_USB           BIT(0)
284 #define UCSI_CONSTAT_PARTNER_FLAG_ALT_MODE      BIT(1)
285         u16 partner_type:3;
286 #define UCSI_CONSTAT_PARTNER_TYPE_DFP           1
287 #define UCSI_CONSTAT_PARTNER_TYPE_UFP           2
288 #define UCSI_CONSTAT_PARTNER_TYPE_CABLE         3 /* Powered Cable */
289 #define UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP 4 /* Powered Cable */
290 #define UCSI_CONSTAT_PARTNER_TYPE_DEBUG         5
291 #define UCSI_CONSTAT_PARTNER_TYPE_AUDIO         6
292         u32 request_data_obj;
293         u8 bc_status:2;
294 #define UCSI_CONSTAT_BC_NOT_CHARGING            0
295 #define UCSI_CONSTAT_BC_NOMINAL_CHARGING        1
296 #define UCSI_CONSTAT_BC_SLOW_CHARGING           2
297 #define UCSI_CONSTAT_BC_TRICKLE_CHARGING        3
298         u8 provider_cap_limit_reason:4;
299 #define UCSI_CONSTAT_CAP_PWR_LOWERED            0
300 #define UCSI_CONSTAT_CAP_PWR_BUDGET_LIMIT       1
301         u8:2; /* reserved */
302 } __packed;
303
304 /* -------------------------------------------------------------------------- */
305
306 struct ucsi;
307
308 struct ucsi_data {
309         u16 version;
310         u16 reserved;
311         union {
312                 u32 raw_cci;
313                 struct ucsi_cci cci;
314         };
315         struct ucsi_control ctrl;
316         u32 message_in[4];
317         u32 message_out[4];
318 } __packed;
319
320 /*
321  * struct ucsi_ppm - Interface to UCSI Platform Policy Manager
322  * @data: memory location to the UCSI data structures
323  * @cmd: UCSI command execution routine
324  * @sync: Refresh UCSI mailbox (the data structures)
325  */
326 struct ucsi_ppm {
327         struct ucsi_data *data;
328         int (*cmd)(struct ucsi_ppm *, struct ucsi_control *);
329         int (*sync)(struct ucsi_ppm *);
330 };
331
332 struct ucsi *ucsi_register_ppm(struct device *dev, struct ucsi_ppm *ppm);
333 void ucsi_unregister_ppm(struct ucsi *ucsi);
334 void ucsi_notify(struct ucsi *ucsi);
335
336 #endif /* __DRIVER_USB_TYPEC_UCSI_H */