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