1 /* SPDX-License-Identifier: GPL-2.0 */
3 #ifndef __DRIVER_USB_TYPEC_UCSI_H
4 #define __DRIVER_USB_TYPEC_UCSI_H
6 #include <linux/bitops.h>
7 #include <linux/device.h>
8 #include <linux/types.h>
9 #include <linux/usb/typec.h>
11 /* -------------------------------------------------------------------------- */
13 /* Command Status and Connector Change Indication (CCI) data structure */
16 u8 connector_change:7;
20 u16 cancel_complete:1;
28 /* Default fields in CONTROL data structure */
35 /* ACK Command structure */
44 /* Connector Reset Command structure */
52 /* Set USB Operation Mode Command structure */
58 #define UCSI_UOR_ROLE_DFP BIT(0)
59 #define UCSI_UOR_ROLE_UFP BIT(1)
60 #define UCSI_UOR_ROLE_DRP BIT(2)
64 /* Get Alternate Modes Command structure */
65 struct ucsi_altmode_cmd {
69 #define UCSI_RECIPIENT_CON 0
70 #define UCSI_RECIPIENT_SOP 1
71 #define UCSI_RECIPIENT_SOP_P 2
72 #define UCSI_RECIPIENT_SOP_PP 3
81 struct ucsi_command cmd;
82 struct ucsi_uor_cmd uor;
83 struct ucsi_ack_cmd ack;
84 struct ucsi_con_rst con_rst;
85 struct ucsi_altmode_cmd alt;
89 #define __UCSI_CMD(_ctrl_, _cmd_) \
91 (_ctrl_).raw_cmd = 0; \
92 (_ctrl_).cmd.cmd = _cmd_; \
95 /* Helper for preparing ucsi_control for CONNECTOR_RESET command. */
96 #define UCSI_CMD_CONNECTOR_RESET(_ctrl_, _con_, _hard_) \
98 __UCSI_CMD(_ctrl_, UCSI_CONNECTOR_RESET) \
99 (_ctrl_).con_rst.con_num = (_con_)->num; \
100 (_ctrl_).con_rst.hard_reset = _hard_; \
103 /* Helper for preparing ucsi_control for ACK_CC_CI command. */
104 #define UCSI_CMD_ACK(_ctrl_, _ack_) \
106 __UCSI_CMD(_ctrl_, UCSI_ACK_CC_CI) \
107 (_ctrl_).ack.cci_ack = ((_ack_) == UCSI_ACK_EVENT); \
108 (_ctrl_).ack.cmd_ack = ((_ack_) == UCSI_ACK_CMD); \
111 /* Helper for preparing ucsi_control for SET_NOTIFY_ENABLE command. */
112 #define UCSI_CMD_SET_NTFY_ENABLE(_ctrl_, _ntfys_) \
114 __UCSI_CMD(_ctrl_, UCSI_SET_NOTIFICATION_ENABLE) \
115 (_ctrl_).cmd.data = _ntfys_; \
118 /* Helper for preparing ucsi_control for GET_CAPABILITY command. */
119 #define UCSI_CMD_GET_CAPABILITY(_ctrl_) \
121 __UCSI_CMD(_ctrl_, UCSI_GET_CAPABILITY) \
124 /* Helper for preparing ucsi_control for GET_CONNECTOR_CAPABILITY command. */
125 #define UCSI_CMD_GET_CONNECTOR_CAPABILITY(_ctrl_, _con_) \
127 __UCSI_CMD(_ctrl_, UCSI_GET_CONNECTOR_CAPABILITY) \
128 (_ctrl_).cmd.data = _con_; \
131 /* Helper for preparing ucsi_control for GET_ALTERNATE_MODES command. */
132 #define UCSI_CMD_GET_ALTERNATE_MODES(_ctrl_, _r_, _con_num_, _o_, _num_)\
134 __UCSI_CMD((_ctrl_), UCSI_GET_ALTERNATE_MODES) \
135 _ctrl_.alt.recipient = (_r_); \
136 _ctrl_.alt.con_num = (_con_num_); \
137 _ctrl_.alt.offset = (_o_); \
138 _ctrl_.alt.num_altmodes = (_num_) - 1; \
141 /* Helper for preparing ucsi_control for GET_CAM_SUPPORTED command. */
142 #define UCSI_CMD_GET_CAM_SUPPORTED(_ctrl_, _con_) \
144 __UCSI_CMD((_ctrl_), UCSI_GET_CAM_SUPPORTED) \
145 _ctrl_.cmd.data = (_con_); \
148 /* Helper for preparing ucsi_control for GET_CAM_SUPPORTED command. */
149 #define UCSI_CMD_GET_CURRENT_CAM(_ctrl_, _con_) \
151 __UCSI_CMD((_ctrl_), UCSI_GET_CURRENT_CAM) \
152 _ctrl_.cmd.data = (_con_); \
155 /* Helper for preparing ucsi_control for GET_CONNECTOR_STATUS command. */
156 #define UCSI_CMD_GET_CONNECTOR_STATUS(_ctrl_, _con_) \
158 __UCSI_CMD(_ctrl_, UCSI_GET_CONNECTOR_STATUS) \
159 (_ctrl_).cmd.data = _con_; \
162 #define __UCSI_ROLE(_ctrl_, _cmd_, _con_num_) \
164 __UCSI_CMD(_ctrl_, _cmd_) \
165 (_ctrl_).uor.con_num = _con_num_; \
166 (_ctrl_).uor.role = UCSI_UOR_ROLE_DRP; \
169 /* Helper for preparing ucsi_control for SET_UOR command. */
170 #define UCSI_CMD_SET_UOR(_ctrl_, _con_, _role_) \
172 __UCSI_ROLE(_ctrl_, UCSI_SET_UOR, (_con_)->num) \
173 (_ctrl_).uor.role |= (_role_) == TYPEC_HOST ? UCSI_UOR_ROLE_DFP : \
177 /* Helper for preparing ucsi_control for SET_PDR command. */
178 #define UCSI_CMD_SET_PDR(_ctrl_, _con_, _role_) \
180 __UCSI_ROLE(_ctrl_, UCSI_SET_PDR, (_con_)->num) \
181 (_ctrl_).uor.role |= (_role_) == TYPEC_SOURCE ? UCSI_UOR_ROLE_DFP : \
186 #define UCSI_PPM_RESET 0x01
187 #define UCSI_CANCEL 0x02
188 #define UCSI_CONNECTOR_RESET 0x03
189 #define UCSI_ACK_CC_CI 0x04
190 #define UCSI_SET_NOTIFICATION_ENABLE 0x05
191 #define UCSI_GET_CAPABILITY 0x06
192 #define UCSI_GET_CONNECTOR_CAPABILITY 0x07
193 #define UCSI_SET_UOM 0x08
194 #define UCSI_SET_UOR 0x09
195 #define UCSI_SET_PDM 0x0a
196 #define UCSI_SET_PDR 0x0b
197 #define UCSI_GET_ALTERNATE_MODES 0x0c
198 #define UCSI_GET_CAM_SUPPORTED 0x0d
199 #define UCSI_GET_CURRENT_CAM 0x0e
200 #define UCSI_SET_NEW_CAM 0x0f
201 #define UCSI_GET_PDOS 0x10
202 #define UCSI_GET_CABLE_PROPERTY 0x11
203 #define UCSI_GET_CONNECTOR_STATUS 0x12
204 #define UCSI_GET_ERROR_STATUS 0x13
206 /* ACK_CC_CI commands */
207 #define UCSI_ACK_EVENT 1
208 #define UCSI_ACK_CMD 2
210 /* Bits for SET_NOTIFICATION_ENABLE command */
211 #define UCSI_ENABLE_NTFY_CMD_COMPLETE BIT(0)
212 #define UCSI_ENABLE_NTFY_EXT_PWR_SRC_CHANGE BIT(1)
213 #define UCSI_ENABLE_NTFY_PWR_OPMODE_CHANGE BIT(2)
214 #define UCSI_ENABLE_NTFY_CAP_CHANGE BIT(5)
215 #define UCSI_ENABLE_NTFY_PWR_LEVEL_CHANGE BIT(6)
216 #define UCSI_ENABLE_NTFY_PD_RESET_COMPLETE BIT(7)
217 #define UCSI_ENABLE_NTFY_CAM_CHANGE BIT(8)
218 #define UCSI_ENABLE_NTFY_BAT_STATUS_CHANGE BIT(9)
219 #define UCSI_ENABLE_NTFY_PARTNER_CHANGE BIT(11)
220 #define UCSI_ENABLE_NTFY_PWR_DIR_CHANGE BIT(12)
221 #define UCSI_ENABLE_NTFY_CONNECTOR_CHANGE BIT(14)
222 #define UCSI_ENABLE_NTFY_ERROR BIT(15)
223 #define UCSI_ENABLE_NTFY_ALL 0xdbe7
225 /* Error information returned by PPM in response to GET_ERROR_STATUS command. */
226 #define UCSI_ERROR_UNREGONIZED_CMD BIT(0)
227 #define UCSI_ERROR_INVALID_CON_NUM BIT(1)
228 #define UCSI_ERROR_INVALID_CMD_ARGUMENT BIT(2)
229 #define UCSI_ERROR_INCOMPATIBLE_PARTNER BIT(3)
230 #define UCSI_ERROR_CC_COMMUNICATION_ERR BIT(4)
231 #define UCSI_ERROR_DEAD_BATTERY BIT(5)
232 #define UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL BIT(6)
234 /* Data structure filled by PPM in response to GET_CAPABILITY command. */
235 struct ucsi_capability {
237 #define UCSI_CAP_ATTR_DISABLE_STATE BIT(0)
238 #define UCSI_CAP_ATTR_BATTERY_CHARGING BIT(1)
239 #define UCSI_CAP_ATTR_USB_PD BIT(2)
240 #define UCSI_CAP_ATTR_TYPEC_CURRENT BIT(6)
241 #define UCSI_CAP_ATTR_POWER_AC_SUPPLY BIT(8)
242 #define UCSI_CAP_ATTR_POWER_OTHER BIT(10)
243 #define UCSI_CAP_ATTR_POWER_VBUS BIT(14)
244 u32 num_connectors:8;
246 #define UCSI_CAP_SET_UOM BIT(0)
247 #define UCSI_CAP_SET_PDM BIT(1)
248 #define UCSI_CAP_ALT_MODE_DETAILS BIT(2)
249 #define UCSI_CAP_ALT_MODE_OVERRIDE BIT(3)
250 #define UCSI_CAP_PDO_DETAILS BIT(4)
251 #define UCSI_CAP_CABLE_DETAILS BIT(5)
252 #define UCSI_CAP_EXT_SUPPLY_NOTIFICATIONS BIT(6)
253 #define UCSI_CAP_PD_RESET BIT(7)
261 /* Data structure filled by PPM in response to GET_CONNECTOR_CAPABILITY cmd. */
262 struct ucsi_connector_capability {
264 #define UCSI_CONCAP_OPMODE_DFP BIT(0)
265 #define UCSI_CONCAP_OPMODE_UFP BIT(1)
266 #define UCSI_CONCAP_OPMODE_DRP BIT(2)
267 #define UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY BIT(3)
268 #define UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY BIT(4)
269 #define UCSI_CONCAP_OPMODE_USB2 BIT(5)
270 #define UCSI_CONCAP_OPMODE_USB3 BIT(6)
271 #define UCSI_CONCAP_OPMODE_ALT_MODE BIT(7)
277 struct ucsi_altmode {
282 /* Data structure filled by PPM in response to GET_CABLE_PROPERTY command. */
283 struct ucsi_cable_property {
285 u8 current_capability;
290 #define UCSI_CABLE_PROPERTY_PLUG_TYPE_A 0
291 #define UCSI_CABLE_PROPERTY_PLUG_TYPE_B 1
292 #define UCSI_CABLE_PROPERTY_PLUG_TYPE_C 2
293 #define UCSI_CABLE_PROPERTY_PLUG_OTHER 3
300 /* Data structure filled by PPM in response to GET_CONNECTOR_STATUS command. */
301 struct ucsi_connector_status {
303 #define UCSI_CONSTAT_EXT_SUPPLY_CHANGE BIT(1)
304 #define UCSI_CONSTAT_POWER_OPMODE_CHANGE BIT(2)
305 #define UCSI_CONSTAT_PDOS_CHANGE BIT(5)
306 #define UCSI_CONSTAT_POWER_LEVEL_CHANGE BIT(6)
307 #define UCSI_CONSTAT_PD_RESET_COMPLETE BIT(7)
308 #define UCSI_CONSTAT_CAM_CHANGE BIT(8)
309 #define UCSI_CONSTAT_BC_CHANGE BIT(9)
310 #define UCSI_CONSTAT_PARTNER_CHANGE BIT(11)
311 #define UCSI_CONSTAT_POWER_DIR_CHANGE BIT(12)
312 #define UCSI_CONSTAT_CONNECT_CHANGE BIT(14)
313 #define UCSI_CONSTAT_ERROR BIT(15)
315 #define UCSI_CONSTAT_PWR_OPMODE_NONE 0
316 #define UCSI_CONSTAT_PWR_OPMODE_DEFAULT 1
317 #define UCSI_CONSTAT_PWR_OPMODE_BC 2
318 #define UCSI_CONSTAT_PWR_OPMODE_PD 3
319 #define UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5 4
320 #define UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0 5
324 #define UCSI_CONSTAT_PARTNER_FLAG_USB BIT(0)
325 #define UCSI_CONSTAT_PARTNER_FLAG_ALT_MODE BIT(1)
327 #define UCSI_CONSTAT_PARTNER_TYPE_DFP 1
328 #define UCSI_CONSTAT_PARTNER_TYPE_UFP 2
329 #define UCSI_CONSTAT_PARTNER_TYPE_CABLE 3 /* Powered Cable */
330 #define UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP 4 /* Powered Cable */
331 #define UCSI_CONSTAT_PARTNER_TYPE_DEBUG 5
332 #define UCSI_CONSTAT_PARTNER_TYPE_AUDIO 6
333 u32 request_data_obj;
335 #define UCSI_CONSTAT_BC_NOT_CHARGING 0
336 #define UCSI_CONSTAT_BC_NOMINAL_CHARGING 1
337 #define UCSI_CONSTAT_BC_SLOW_CHARGING 2
338 #define UCSI_CONSTAT_BC_TRICKLE_CHARGING 3
339 u8 provider_cap_limit_reason:4;
340 #define UCSI_CONSTAT_CAP_PWR_LOWERED 0
341 #define UCSI_CONSTAT_CAP_PWR_BUDGET_LIMIT 1
345 /* -------------------------------------------------------------------------- */
356 struct ucsi_control ctrl;
362 * struct ucsi_ppm - Interface to UCSI Platform Policy Manager
363 * @data: memory location to the UCSI data structures
364 * @cmd: UCSI command execution routine
365 * @sync: Refresh UCSI mailbox (the data structures)
368 struct ucsi_data *data;
369 int (*cmd)(struct ucsi_ppm *, struct ucsi_control *);
370 int (*sync)(struct ucsi_ppm *);
373 struct ucsi *ucsi_register_ppm(struct device *dev, struct ucsi_ppm *ppm);
374 void ucsi_unregister_ppm(struct ucsi *ucsi);
375 void ucsi_notify(struct ucsi *ucsi);
377 /* -------------------------------------------------------------------------- */
387 struct ucsi_ppm *ppm;
389 enum ucsi_status status;
390 struct completion complete;
391 struct ucsi_capability cap;
392 struct ucsi_connector *connector;
394 struct work_struct work;
396 /* PPM Communication lock */
397 struct mutex ppm_lock;
399 /* PPM communication flags */
401 #define EVENT_PENDING 0
402 #define COMMAND_PENDING 1
403 #define ACK_PENDING 2
406 #define UCSI_MAX_SVID 5
407 #define UCSI_MAX_ALTMODES (UCSI_MAX_SVID * 6)
409 struct ucsi_connector {
413 struct mutex lock; /* port lock */
414 struct work_struct work;
415 struct completion complete;
417 struct typec_port *port;
418 struct typec_partner *partner;
420 struct typec_altmode *port_altmode[UCSI_MAX_ALTMODES];
421 struct typec_altmode *partner_altmode[UCSI_MAX_ALTMODES];
423 struct typec_capability typec_cap;
425 struct ucsi_connector_status status;
426 struct ucsi_connector_capability cap;
429 int ucsi_send_command(struct ucsi *ucsi, struct ucsi_control *ctrl,
430 void *retval, size_t size);
432 void ucsi_altmode_update_active(struct ucsi_connector *con);
433 int ucsi_resume(struct ucsi *ucsi);
435 #if IS_ENABLED(CONFIG_TYPEC_DP_ALTMODE)
436 struct typec_altmode *
437 ucsi_register_displayport(struct ucsi_connector *con,
438 bool override, int offset,
439 struct typec_altmode_desc *desc);
441 void ucsi_displayport_remove_partner(struct typec_altmode *adev);
444 static inline struct typec_altmode *
445 ucsi_register_displayport(struct ucsi_connector *con,
446 bool override, int offset,
447 struct typec_altmode_desc *desc)
453 ucsi_displayport_remove_partner(struct typec_altmode *adev) { }
454 #endif /* CONFIG_TYPEC_DP_ALTMODE */
456 #endif /* __DRIVER_USB_TYPEC_UCSI_H */