Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 
0003 #ifndef __DRIVER_USB_TYPEC_UCSI_H
0004 #define __DRIVER_USB_TYPEC_UCSI_H
0005 
0006 #include <linux/bitops.h>
0007 #include <linux/device.h>
0008 #include <linux/power_supply.h>
0009 #include <linux/types.h>
0010 #include <linux/usb/typec.h>
0011 #include <linux/usb/pd.h>
0012 #include <linux/usb/role.h>
0013 
0014 /* -------------------------------------------------------------------------- */
0015 
0016 struct ucsi;
0017 struct ucsi_altmode;
0018 
0019 /* UCSI offsets (Bytes) */
0020 #define UCSI_VERSION            0
0021 #define UCSI_CCI            4
0022 #define UCSI_CONTROL            8
0023 #define UCSI_MESSAGE_IN         16
0024 #define UCSI_MESSAGE_OUT        32
0025 
0026 /* Command Status and Connector Change Indication (CCI) bits */
0027 #define UCSI_CCI_CONNECTOR(_c_)     (((_c_) & GENMASK(7, 1)) >> 1)
0028 #define UCSI_CCI_LENGTH(_c_)        (((_c_) & GENMASK(15, 8)) >> 8)
0029 #define UCSI_CCI_NOT_SUPPORTED      BIT(25)
0030 #define UCSI_CCI_CANCEL_COMPLETE    BIT(26)
0031 #define UCSI_CCI_RESET_COMPLETE     BIT(27)
0032 #define UCSI_CCI_BUSY           BIT(28)
0033 #define UCSI_CCI_ACK_COMPLETE       BIT(29)
0034 #define UCSI_CCI_ERROR          BIT(30)
0035 #define UCSI_CCI_COMMAND_COMPLETE   BIT(31)
0036 
0037 /**
0038  * struct ucsi_operations - UCSI I/O operations
0039  * @read: Read operation
0040  * @sync_write: Blocking write operation
0041  * @async_write: Non-blocking write operation
0042  * @update_altmodes: Squashes duplicate DP altmodes
0043  *
0044  * Read and write routines for UCSI interface. @sync_write must wait for the
0045  * Command Completion Event from the PPM before returning, and @async_write must
0046  * return immediately after sending the data to the PPM.
0047  */
0048 struct ucsi_operations {
0049     int (*read)(struct ucsi *ucsi, unsigned int offset,
0050             void *val, size_t val_len);
0051     int (*sync_write)(struct ucsi *ucsi, unsigned int offset,
0052               const void *val, size_t val_len);
0053     int (*async_write)(struct ucsi *ucsi, unsigned int offset,
0054                const void *val, size_t val_len);
0055     bool (*update_altmodes)(struct ucsi *ucsi, struct ucsi_altmode *orig,
0056                 struct ucsi_altmode *updated);
0057 };
0058 
0059 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops);
0060 void ucsi_destroy(struct ucsi *ucsi);
0061 int ucsi_register(struct ucsi *ucsi);
0062 void ucsi_unregister(struct ucsi *ucsi);
0063 void *ucsi_get_drvdata(struct ucsi *ucsi);
0064 void ucsi_set_drvdata(struct ucsi *ucsi, void *data);
0065 
0066 void ucsi_connector_change(struct ucsi *ucsi, u8 num);
0067 
0068 /* -------------------------------------------------------------------------- */
0069 
0070 /* Commands */
0071 #define UCSI_PPM_RESET          0x01
0072 #define UCSI_CANCEL         0x02
0073 #define UCSI_CONNECTOR_RESET        0x03
0074 #define UCSI_ACK_CC_CI          0x04
0075 #define UCSI_SET_NOTIFICATION_ENABLE    0x05
0076 #define UCSI_GET_CAPABILITY     0x06
0077 #define UCSI_GET_CONNECTOR_CAPABILITY   0x07
0078 #define UCSI_SET_UOM            0x08
0079 #define UCSI_SET_UOR            0x09
0080 #define UCSI_SET_PDM            0x0a
0081 #define UCSI_SET_PDR            0x0b
0082 #define UCSI_GET_ALTERNATE_MODES    0x0c
0083 #define UCSI_GET_CAM_SUPPORTED      0x0d
0084 #define UCSI_GET_CURRENT_CAM        0x0e
0085 #define UCSI_SET_NEW_CAM        0x0f
0086 #define UCSI_GET_PDOS           0x10
0087 #define UCSI_GET_CABLE_PROPERTY     0x11
0088 #define UCSI_GET_CONNECTOR_STATUS   0x12
0089 #define UCSI_GET_ERROR_STATUS       0x13
0090 
0091 #define UCSI_CONNECTOR_NUMBER(_num_)        ((u64)(_num_) << 16)
0092 #define UCSI_COMMAND(_cmd_)         ((_cmd_) & 0xff)
0093 
0094 /* CONNECTOR_RESET command bits */
0095 #define UCSI_CONNECTOR_RESET_HARD       BIT(23) /* Deprecated in v1.1 */
0096 
0097 /* ACK_CC_CI bits */
0098 #define UCSI_ACK_CONNECTOR_CHANGE       BIT(16)
0099 #define UCSI_ACK_COMMAND_COMPLETE       BIT(17)
0100 
0101 /* SET_NOTIFICATION_ENABLE command bits */
0102 #define UCSI_ENABLE_NTFY_CMD_COMPLETE       BIT(16)
0103 #define UCSI_ENABLE_NTFY_EXT_PWR_SRC_CHANGE BIT(17)
0104 #define UCSI_ENABLE_NTFY_PWR_OPMODE_CHANGE  BIT(18)
0105 #define UCSI_ENABLE_NTFY_CAP_CHANGE     BIT(21)
0106 #define UCSI_ENABLE_NTFY_PWR_LEVEL_CHANGE   BIT(22)
0107 #define UCSI_ENABLE_NTFY_PD_RESET_COMPLETE  BIT(23)
0108 #define UCSI_ENABLE_NTFY_CAM_CHANGE     BIT(24)
0109 #define UCSI_ENABLE_NTFY_BAT_STATUS_CHANGE  BIT(25)
0110 #define UCSI_ENABLE_NTFY_PARTNER_CHANGE     BIT(27)
0111 #define UCSI_ENABLE_NTFY_PWR_DIR_CHANGE     BIT(28)
0112 #define UCSI_ENABLE_NTFY_CONNECTOR_CHANGE   BIT(30)
0113 #define UCSI_ENABLE_NTFY_ERROR          BIT(31)
0114 #define UCSI_ENABLE_NTFY_ALL            0xdbe70000
0115 
0116 /* SET_UOR command bits */
0117 #define UCSI_SET_UOR_ROLE(_r_)      (((_r_) == TYPEC_HOST ? 1 : 2) << 23)
0118 #define UCSI_SET_UOR_ACCEPT_ROLE_SWAPS      BIT(25)
0119 
0120 /* SET_PDF command bits */
0121 #define UCSI_SET_PDR_ROLE(_r_)      (((_r_) == TYPEC_SOURCE ? 1 : 2) << 23)
0122 #define UCSI_SET_PDR_ACCEPT_ROLE_SWAPS      BIT(25)
0123 
0124 /* GET_ALTERNATE_MODES command bits */
0125 #define UCSI_ALTMODE_RECIPIENT(_r_)     (((_r_) >> 16) & 0x7)
0126 #define UCSI_GET_ALTMODE_RECIPIENT(_r_)     ((u64)(_r_) << 16)
0127 #define   UCSI_RECIPIENT_CON            0
0128 #define   UCSI_RECIPIENT_SOP            1
0129 #define   UCSI_RECIPIENT_SOP_P          2
0130 #define   UCSI_RECIPIENT_SOP_PP         3
0131 #define UCSI_GET_ALTMODE_CONNECTOR_NUMBER(_r_)  ((u64)(_r_) << 24)
0132 #define UCSI_ALTMODE_OFFSET(_r_)        (((_r_) >> 32) & 0xff)
0133 #define UCSI_GET_ALTMODE_OFFSET(_r_)        ((u64)(_r_) << 32)
0134 #define UCSI_GET_ALTMODE_NUM_ALTMODES(_r_)  ((u64)(_r_) << 40)
0135 
0136 /* GET_PDOS command bits */
0137 #define UCSI_GET_PDOS_PARTNER_PDO(_r_)      ((u64)(_r_) << 23)
0138 #define UCSI_GET_PDOS_PDO_OFFSET(_r_)       ((u64)(_r_) << 24)
0139 #define UCSI_GET_PDOS_NUM_PDOS(_r_)     ((u64)(_r_) << 32)
0140 #define UCSI_MAX_PDOS               (4)
0141 #define UCSI_GET_PDOS_SRC_PDOS          ((u64)1 << 34)
0142 
0143 /* -------------------------------------------------------------------------- */
0144 
0145 /* Error information returned by PPM in response to GET_ERROR_STATUS command. */
0146 #define UCSI_ERROR_UNREGONIZED_CMD      BIT(0)
0147 #define UCSI_ERROR_INVALID_CON_NUM      BIT(1)
0148 #define UCSI_ERROR_INVALID_CMD_ARGUMENT     BIT(2)
0149 #define UCSI_ERROR_INCOMPATIBLE_PARTNER     BIT(3)
0150 #define UCSI_ERROR_CC_COMMUNICATION_ERR     BIT(4)
0151 #define UCSI_ERROR_DEAD_BATTERY         BIT(5)
0152 #define UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL    BIT(6)
0153 #define UCSI_ERROR_OVERCURRENT          BIT(7)
0154 #define UCSI_ERROR_UNDEFINED            BIT(8)
0155 #define UCSI_ERROR_PARTNER_REJECTED_SWAP    BIT(9)
0156 #define UCSI_ERROR_HARD_RESET           BIT(10)
0157 #define UCSI_ERROR_PPM_POLICY_CONFLICT      BIT(11)
0158 #define UCSI_ERROR_SWAP_REJECTED        BIT(12)
0159 
0160 #define UCSI_SET_NEW_CAM_ENTER(x)       (((x) >> 23) & 0x1)
0161 #define UCSI_SET_NEW_CAM_GET_AM(x)      (((x) >> 24) & 0xff)
0162 #define UCSI_SET_NEW_CAM_AM_MASK        (0xff << 24)
0163 #define UCSI_SET_NEW_CAM_SET_AM(x)      (((x) & 0xff) << 24)
0164 #define UCSI_CMD_CONNECTOR_MASK         (0x7)
0165 
0166 /* Data structure filled by PPM in response to GET_CAPABILITY command. */
0167 struct ucsi_capability {
0168     u32 attributes;
0169 #define UCSI_CAP_ATTR_DISABLE_STATE     BIT(0)
0170 #define UCSI_CAP_ATTR_BATTERY_CHARGING      BIT(1)
0171 #define UCSI_CAP_ATTR_USB_PD            BIT(2)
0172 #define UCSI_CAP_ATTR_TYPEC_CURRENT     BIT(6)
0173 #define UCSI_CAP_ATTR_POWER_AC_SUPPLY       BIT(8)
0174 #define UCSI_CAP_ATTR_POWER_OTHER       BIT(10)
0175 #define UCSI_CAP_ATTR_POWER_VBUS        BIT(14)
0176     u8 num_connectors;
0177     u8 features;
0178 #define UCSI_CAP_SET_UOM            BIT(0)
0179 #define UCSI_CAP_SET_PDM            BIT(1)
0180 #define UCSI_CAP_ALT_MODE_DETAILS       BIT(2)
0181 #define UCSI_CAP_ALT_MODE_OVERRIDE      BIT(3)
0182 #define UCSI_CAP_PDO_DETAILS            BIT(4)
0183 #define UCSI_CAP_CABLE_DETAILS          BIT(5)
0184 #define UCSI_CAP_EXT_SUPPLY_NOTIFICATIONS   BIT(6)
0185 #define UCSI_CAP_PD_RESET           BIT(7)
0186     u16 reserved_1;
0187     u8 num_alt_modes;
0188     u8 reserved_2;
0189     u16 bc_version;
0190     u16 pd_version;
0191     u16 typec_version;
0192 } __packed;
0193 
0194 /* Data structure filled by PPM in response to GET_CONNECTOR_CAPABILITY cmd. */
0195 struct ucsi_connector_capability {
0196     u8 op_mode;
0197 #define UCSI_CONCAP_OPMODE_DFP          BIT(0)
0198 #define UCSI_CONCAP_OPMODE_UFP          BIT(1)
0199 #define UCSI_CONCAP_OPMODE_DRP          BIT(2)
0200 #define UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY  BIT(3)
0201 #define UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY  BIT(4)
0202 #define UCSI_CONCAP_OPMODE_USB2         BIT(5)
0203 #define UCSI_CONCAP_OPMODE_USB3         BIT(6)
0204 #define UCSI_CONCAP_OPMODE_ALT_MODE     BIT(7)
0205     u8 flags;
0206 #define UCSI_CONCAP_FLAG_PROVIDER       BIT(0)
0207 #define UCSI_CONCAP_FLAG_CONSUMER       BIT(1)
0208 } __packed;
0209 
0210 struct ucsi_altmode {
0211     u16 svid;
0212     u32 mid;
0213 } __packed;
0214 
0215 /* Data structure filled by PPM in response to GET_CABLE_PROPERTY command. */
0216 struct ucsi_cable_property {
0217     u16 speed_supported;
0218     u8 current_capability;
0219     u8 flags;
0220 #define UCSI_CABLE_PROP_FLAG_VBUS_IN_CABLE  BIT(0)
0221 #define UCSI_CABLE_PROP_FLAG_ACTIVE_CABLE   BIT(1)
0222 #define UCSI_CABLE_PROP_FLAG_DIRECTIONALITY BIT(2)
0223 #define UCSI_CABLE_PROP_FLAG_PLUG_TYPE(_f_) ((_f_) & GENMASK(3, 0))
0224 #define   UCSI_CABLE_PROPERTY_PLUG_TYPE_A   0
0225 #define   UCSI_CABLE_PROPERTY_PLUG_TYPE_B   1
0226 #define   UCSI_CABLE_PROPERTY_PLUG_TYPE_C   2
0227 #define   UCSI_CABLE_PROPERTY_PLUG_OTHER    3
0228 #define UCSI_CABLE_PROP_MODE_SUPPORT        BIT(5)
0229     u8 latency;
0230 } __packed;
0231 
0232 /* Data structure filled by PPM in response to GET_CONNECTOR_STATUS command. */
0233 struct ucsi_connector_status {
0234     u16 change;
0235 #define UCSI_CONSTAT_EXT_SUPPLY_CHANGE      BIT(1)
0236 #define UCSI_CONSTAT_POWER_OPMODE_CHANGE    BIT(2)
0237 #define UCSI_CONSTAT_PDOS_CHANGE        BIT(5)
0238 #define UCSI_CONSTAT_POWER_LEVEL_CHANGE     BIT(6)
0239 #define UCSI_CONSTAT_PD_RESET_COMPLETE      BIT(7)
0240 #define UCSI_CONSTAT_CAM_CHANGE         BIT(8)
0241 #define UCSI_CONSTAT_BC_CHANGE          BIT(9)
0242 #define UCSI_CONSTAT_PARTNER_CHANGE     BIT(11)
0243 #define UCSI_CONSTAT_POWER_DIR_CHANGE       BIT(12)
0244 #define UCSI_CONSTAT_CONNECT_CHANGE     BIT(14)
0245 #define UCSI_CONSTAT_ERROR          BIT(15)
0246     u16 flags;
0247 #define UCSI_CONSTAT_PWR_OPMODE(_f_)        ((_f_) & GENMASK(2, 0))
0248 #define   UCSI_CONSTAT_PWR_OPMODE_NONE      0
0249 #define   UCSI_CONSTAT_PWR_OPMODE_DEFAULT   1
0250 #define   UCSI_CONSTAT_PWR_OPMODE_BC        2
0251 #define   UCSI_CONSTAT_PWR_OPMODE_PD        3
0252 #define   UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5  4
0253 #define   UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0  5
0254 #define UCSI_CONSTAT_CONNECTED          BIT(3)
0255 #define UCSI_CONSTAT_PWR_DIR            BIT(4)
0256 #define UCSI_CONSTAT_PARTNER_FLAGS(_f_)     (((_f_) & GENMASK(12, 5)) >> 5)
0257 #define   UCSI_CONSTAT_PARTNER_FLAG_USB     1
0258 #define   UCSI_CONSTAT_PARTNER_FLAG_ALT_MODE    2
0259 #define UCSI_CONSTAT_PARTNER_TYPE(_f_)      (((_f_) & GENMASK(15, 13)) >> 13)
0260 #define   UCSI_CONSTAT_PARTNER_TYPE_DFP     1
0261 #define   UCSI_CONSTAT_PARTNER_TYPE_UFP     2
0262 #define   UCSI_CONSTAT_PARTNER_TYPE_CABLE   3 /* Powered Cable */
0263 #define   UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP   4 /* Powered Cable */
0264 #define   UCSI_CONSTAT_PARTNER_TYPE_DEBUG   5
0265 #define   UCSI_CONSTAT_PARTNER_TYPE_AUDIO   6
0266     u32 request_data_obj;
0267     u8 pwr_status;
0268 #define UCSI_CONSTAT_BC_STATUS(_p_)     ((_p_) & GENMASK(2, 0))
0269 #define   UCSI_CONSTAT_BC_NOT_CHARGING      0
0270 #define   UCSI_CONSTAT_BC_NOMINAL_CHARGING  1
0271 #define   UCSI_CONSTAT_BC_SLOW_CHARGING     2
0272 #define   UCSI_CONSTAT_BC_TRICKLE_CHARGING  3
0273 #define UCSI_CONSTAT_PROVIDER_CAP_LIMIT(_p_)    (((_p_) & GENMASK(6, 3)) >> 3)
0274 #define   UCSI_CONSTAT_CAP_PWR_LOWERED      0
0275 #define   UCSI_CONSTAT_CAP_PWR_BUDGET_LIMIT 1
0276 } __packed;
0277 
0278 /* -------------------------------------------------------------------------- */
0279 
0280 struct ucsi {
0281     u16 version;
0282     struct device *dev;
0283     struct driver_data *driver_data;
0284 
0285     const struct ucsi_operations *ops;
0286 
0287     struct ucsi_capability cap;
0288     struct ucsi_connector *connector;
0289 
0290     struct delayed_work work;
0291     int work_count;
0292 #define UCSI_ROLE_SWITCH_RETRY_PER_HZ   10
0293 #define UCSI_ROLE_SWITCH_INTERVAL   (HZ / UCSI_ROLE_SWITCH_RETRY_PER_HZ)
0294 #define UCSI_ROLE_SWITCH_WAIT_COUNT (10 * UCSI_ROLE_SWITCH_RETRY_PER_HZ)
0295 
0296     /* PPM Communication lock */
0297     struct mutex ppm_lock;
0298 
0299     /* The latest "Notification Enable" bits (SET_NOTIFICATION_ENABLE) */
0300     u64 ntfy;
0301 
0302     /* PPM communication flags */
0303     unsigned long flags;
0304 #define EVENT_PENDING   0
0305 #define COMMAND_PENDING 1
0306 #define ACK_PENDING 2
0307 };
0308 
0309 #define UCSI_MAX_SVID       5
0310 #define UCSI_MAX_ALTMODES   (UCSI_MAX_SVID * 6)
0311 
0312 #define UCSI_TYPEC_VSAFE5V  5000
0313 #define UCSI_TYPEC_1_5_CURRENT  1500
0314 #define UCSI_TYPEC_3_0_CURRENT  3000
0315 
0316 struct ucsi_connector {
0317     int num;
0318 
0319     struct ucsi *ucsi;
0320     struct mutex lock; /* port lock */
0321     struct work_struct work;
0322     struct completion complete;
0323     struct workqueue_struct *wq;
0324 
0325     struct typec_port *port;
0326     struct typec_partner *partner;
0327 
0328     struct typec_altmode *port_altmode[UCSI_MAX_ALTMODES];
0329     struct typec_altmode *partner_altmode[UCSI_MAX_ALTMODES];
0330 
0331     struct typec_capability typec_cap;
0332 
0333     struct ucsi_connector_status status;
0334     struct ucsi_connector_capability cap;
0335     struct power_supply *psy;
0336     struct power_supply_desc psy_desc;
0337     u32 rdo;
0338     u32 src_pdos[PDO_MAX_OBJECTS];
0339     int num_pdos;
0340 
0341     struct usb_role_switch *usb_role_sw;
0342 };
0343 
0344 int ucsi_send_command(struct ucsi *ucsi, u64 command,
0345               void *retval, size_t size);
0346 
0347 void ucsi_altmode_update_active(struct ucsi_connector *con);
0348 int ucsi_resume(struct ucsi *ucsi);
0349 
0350 #if IS_ENABLED(CONFIG_POWER_SUPPLY)
0351 int ucsi_register_port_psy(struct ucsi_connector *con);
0352 void ucsi_unregister_port_psy(struct ucsi_connector *con);
0353 void ucsi_port_psy_changed(struct ucsi_connector *con);
0354 #else
0355 static inline int ucsi_register_port_psy(struct ucsi_connector *con) { return 0; }
0356 static inline void ucsi_unregister_port_psy(struct ucsi_connector *con) { }
0357 static inline void ucsi_port_psy_changed(struct ucsi_connector *con) { }
0358 #endif /* CONFIG_POWER_SUPPLY */
0359 
0360 #if IS_ENABLED(CONFIG_TYPEC_DP_ALTMODE)
0361 struct typec_altmode *
0362 ucsi_register_displayport(struct ucsi_connector *con,
0363               bool override, int offset,
0364               struct typec_altmode_desc *desc);
0365 
0366 void ucsi_displayport_remove_partner(struct typec_altmode *adev);
0367 
0368 #else
0369 static inline struct typec_altmode *
0370 ucsi_register_displayport(struct ucsi_connector *con,
0371               bool override, int offset,
0372               struct typec_altmode_desc *desc)
0373 {
0374     return NULL;
0375 }
0376 
0377 static inline void
0378 ucsi_displayport_remove_partner(struct typec_altmode *adev) { }
0379 #endif /* CONFIG_TYPEC_DP_ALTMODE */
0380 
0381 /*
0382  * NVIDIA VirtualLink (svid 0x955) has two altmode. VirtualLink
0383  * DP mode with vdo=0x1 and NVIDIA test mode with vdo=0x3
0384  */
0385 #define USB_TYPEC_NVIDIA_VLINK_DP_VDO   0x1
0386 #define USB_TYPEC_NVIDIA_VLINK_DBG_VDO  0x3
0387 
0388 #endif /* __DRIVER_USB_TYPEC_UCSI_H */