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