Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 
0003 #ifndef __LINUX_USB_TYPEC_H
0004 #define __LINUX_USB_TYPEC_H
0005 
0006 #include <linux/types.h>
0007 
0008 /* USB Type-C Specification releases */
0009 #define USB_TYPEC_REV_1_0   0x100 /* 1.0 */
0010 #define USB_TYPEC_REV_1_1   0x110 /* 1.1 */
0011 #define USB_TYPEC_REV_1_2   0x120 /* 1.2 */
0012 #define USB_TYPEC_REV_1_3   0x130 /* 1.3 */
0013 #define USB_TYPEC_REV_1_4   0x140 /* 1.4 */
0014 #define USB_TYPEC_REV_2_0   0x200 /* 2.0 */
0015 
0016 struct typec_partner;
0017 struct typec_cable;
0018 struct typec_plug;
0019 struct typec_port;
0020 struct typec_altmode_ops;
0021 
0022 struct fwnode_handle;
0023 struct device;
0024 
0025 struct usb_power_delivery;
0026 
0027 enum typec_port_type {
0028     TYPEC_PORT_SRC,
0029     TYPEC_PORT_SNK,
0030     TYPEC_PORT_DRP,
0031 };
0032 
0033 enum typec_port_data {
0034     TYPEC_PORT_DFP,
0035     TYPEC_PORT_UFP,
0036     TYPEC_PORT_DRD,
0037 };
0038 
0039 enum typec_plug_type {
0040     USB_PLUG_NONE,
0041     USB_PLUG_TYPE_A,
0042     USB_PLUG_TYPE_B,
0043     USB_PLUG_TYPE_C,
0044     USB_PLUG_CAPTIVE,
0045 };
0046 
0047 enum typec_data_role {
0048     TYPEC_DEVICE,
0049     TYPEC_HOST,
0050 };
0051 
0052 enum typec_role {
0053     TYPEC_SINK,
0054     TYPEC_SOURCE,
0055 };
0056 
0057 static inline int is_sink(enum typec_role role)
0058 {
0059     return role == TYPEC_SINK;
0060 }
0061 
0062 static inline int is_source(enum typec_role role)
0063 {
0064     return role == TYPEC_SOURCE;
0065 }
0066 
0067 enum typec_pwr_opmode {
0068     TYPEC_PWR_MODE_USB,
0069     TYPEC_PWR_MODE_1_5A,
0070     TYPEC_PWR_MODE_3_0A,
0071     TYPEC_PWR_MODE_PD,
0072 };
0073 
0074 enum typec_accessory {
0075     TYPEC_ACCESSORY_NONE,
0076     TYPEC_ACCESSORY_AUDIO,
0077     TYPEC_ACCESSORY_DEBUG,
0078 };
0079 
0080 #define TYPEC_MAX_ACCESSORY 3
0081 
0082 enum typec_orientation {
0083     TYPEC_ORIENTATION_NONE,
0084     TYPEC_ORIENTATION_NORMAL,
0085     TYPEC_ORIENTATION_REVERSE,
0086 };
0087 
0088 /*
0089  * struct enter_usb_data - Enter_USB Message details
0090  * @eudo: Enter_USB Data Object
0091  * @active_link_training: Active Cable Plug Link Training
0092  *
0093  * @active_link_training is a flag that should be set with uni-directional SBRX
0094  * communication, and left 0 with passive cables and with bi-directional SBRX
0095  * communication.
0096  */
0097 struct enter_usb_data {
0098     u32         eudo;
0099     unsigned char       active_link_training:1;
0100 };
0101 
0102 /*
0103  * struct usb_pd_identity - USB Power Delivery identity data
0104  * @id_header: ID Header VDO
0105  * @cert_stat: Cert Stat VDO
0106  * @product: Product VDO
0107  * @vdo: Product Type Specific VDOs
0108  *
0109  * USB power delivery Discover Identity command response data.
0110  *
0111  * REVISIT: This is USB Power Delivery specific information, so this structure
0112  * probable belongs to USB Power Delivery header file once we have them.
0113  */
0114 struct usb_pd_identity {
0115     u32         id_header;
0116     u32         cert_stat;
0117     u32         product;
0118     u32         vdo[3];
0119 };
0120 
0121 int typec_partner_set_identity(struct typec_partner *partner);
0122 int typec_cable_set_identity(struct typec_cable *cable);
0123 
0124 /*
0125  * struct typec_altmode_desc - USB Type-C Alternate Mode Descriptor
0126  * @svid: Standard or Vendor ID
0127  * @mode: Index of the Mode
0128  * @vdo: VDO returned by Discover Modes USB PD command
0129  * @roles: Only for ports. DRP if the mode is available in both roles
0130  *
0131  * Description of an Alternate Mode which a connector, cable plug or partner
0132  * supports.
0133  */
0134 struct typec_altmode_desc {
0135     u16         svid;
0136     u8          mode;
0137     u32         vdo;
0138     /* Only used with ports */
0139     enum typec_port_data    roles;
0140 };
0141 
0142 void typec_partner_set_pd_revision(struct typec_partner *partner, u16 pd_revision);
0143 int typec_partner_set_num_altmodes(struct typec_partner *partner, int num_altmodes);
0144 struct typec_altmode
0145 *typec_partner_register_altmode(struct typec_partner *partner,
0146                 const struct typec_altmode_desc *desc);
0147 int typec_plug_set_num_altmodes(struct typec_plug *plug, int num_altmodes);
0148 struct typec_altmode
0149 *typec_plug_register_altmode(struct typec_plug *plug,
0150                  const struct typec_altmode_desc *desc);
0151 struct typec_altmode
0152 *typec_port_register_altmode(struct typec_port *port,
0153                  const struct typec_altmode_desc *desc);
0154 
0155 void typec_port_register_altmodes(struct typec_port *port,
0156     const struct typec_altmode_ops *ops, void *drvdata,
0157     struct typec_altmode **altmodes, size_t n);
0158 
0159 void typec_unregister_altmode(struct typec_altmode *altmode);
0160 
0161 struct typec_port *typec_altmode2port(struct typec_altmode *alt);
0162 
0163 void typec_altmode_update_active(struct typec_altmode *alt, bool active);
0164 
0165 enum typec_plug_index {
0166     TYPEC_PLUG_SOP_P,
0167     TYPEC_PLUG_SOP_PP,
0168 };
0169 
0170 /*
0171  * struct typec_plug_desc - USB Type-C Cable Plug Descriptor
0172  * @index: SOP Prime for the plug connected to DFP and SOP Double Prime for the
0173  *         plug connected to UFP
0174  *
0175  * Represents USB Type-C Cable Plug.
0176  */
0177 struct typec_plug_desc {
0178     enum typec_plug_index   index;
0179 };
0180 
0181 /*
0182  * struct typec_cable_desc - USB Type-C Cable Descriptor
0183  * @type: The plug type from USB PD Cable VDO
0184  * @active: Is the cable active or passive
0185  * @identity: Result of Discover Identity command
0186  * @pd_revision: USB Power Delivery Specification revision if supported
0187  *
0188  * Represents USB Type-C Cable attached to USB Type-C port.
0189  */
0190 struct typec_cable_desc {
0191     enum typec_plug_type    type;
0192     unsigned int        active:1;
0193     struct usb_pd_identity  *identity;
0194     u16         pd_revision; /* 0300H = "3.0" */
0195 
0196 };
0197 
0198 /*
0199  * struct typec_partner_desc - USB Type-C Partner Descriptor
0200  * @usb_pd: USB Power Delivery support
0201  * @accessory: Audio, Debug or none.
0202  * @identity: Discover Identity command data
0203  * @pd_revision: USB Power Delivery Specification Revision if supported
0204  *
0205  * Details about a partner that is attached to USB Type-C port. If @identity
0206  * member exists when partner is registered, a directory named "identity" is
0207  * created to sysfs for the partner device.
0208  *
0209  * @pd_revision is based on the setting of the "Specification Revision" field
0210  * in the message header on the initial "Source Capabilities" message received
0211  * from the partner, or a "Request" message received from the partner, depending
0212  * on whether our port is a Sink or a Source.
0213  */
0214 struct typec_partner_desc {
0215     unsigned int        usb_pd:1;
0216     enum typec_accessory    accessory;
0217     struct usb_pd_identity  *identity;
0218     u16         pd_revision; /* 0300H = "3.0" */
0219 };
0220 
0221 /**
0222  * struct typec_operations - USB Type-C Port Operations
0223  * @try_role: Set data role preference for DRP port
0224  * @dr_set: Set Data Role
0225  * @pr_set: Set Power Role
0226  * @vconn_set: Source VCONN
0227  * @port_type_set: Set port type
0228  * @pd_get: Get available USB Power Delivery Capabilities.
0229  * @pd_set: Set USB Power Delivery Capabilities.
0230  */
0231 struct typec_operations {
0232     int (*try_role)(struct typec_port *port, int role);
0233     int (*dr_set)(struct typec_port *port, enum typec_data_role role);
0234     int (*pr_set)(struct typec_port *port, enum typec_role role);
0235     int (*vconn_set)(struct typec_port *port, enum typec_role role);
0236     int (*port_type_set)(struct typec_port *port,
0237                  enum typec_port_type type);
0238     struct usb_power_delivery **(*pd_get)(struct typec_port *port);
0239     int (*pd_set)(struct typec_port *port, struct usb_power_delivery *pd);
0240 };
0241 
0242 enum usb_pd_svdm_ver {
0243     SVDM_VER_1_0 = 0,
0244     SVDM_VER_2_0 = 1,
0245     SVDM_VER_MAX = SVDM_VER_2_0,
0246 };
0247 
0248 /*
0249  * struct typec_capability - USB Type-C Port Capabilities
0250  * @type: Supported power role of the port
0251  * @data: Supported data role of the port
0252  * @revision: USB Type-C Specification release. Binary coded decimal
0253  * @pd_revision: USB Power Delivery Specification revision if supported
0254  * @svdm_version: USB PD Structured VDM version if supported
0255  * @prefer_role: Initial role preference (DRP ports).
0256  * @accessory: Supported Accessory Modes
0257  * @fwnode: Optional fwnode of the port
0258  * @driver_data: Private pointer for driver specific info
0259  * @pd: Optional USB Power Delivery Support
0260  * @ops: Port operations vector
0261  *
0262  * Static capabilities of a single USB Type-C port.
0263  */
0264 struct typec_capability {
0265     enum typec_port_type    type;
0266     enum typec_port_data    data;
0267     u16         revision; /* 0120H = "1.2" */
0268     u16         pd_revision; /* 0300H = "3.0" */
0269     enum usb_pd_svdm_ver    svdm_version;
0270     int         prefer_role;
0271     enum typec_accessory    accessory[TYPEC_MAX_ACCESSORY];
0272     unsigned int        orientation_aware:1;
0273 
0274     struct fwnode_handle    *fwnode;
0275     void            *driver_data;
0276 
0277     struct usb_power_delivery *pd;
0278 
0279     const struct typec_operations   *ops;
0280 };
0281 
0282 /* Specific to try_role(). Indicates the user want's to clear the preference. */
0283 #define TYPEC_NO_PREFERRED_ROLE (-1)
0284 
0285 struct typec_port *typec_register_port(struct device *parent,
0286                        const struct typec_capability *cap);
0287 void typec_unregister_port(struct typec_port *port);
0288 
0289 struct typec_partner *typec_register_partner(struct typec_port *port,
0290                          struct typec_partner_desc *desc);
0291 void typec_unregister_partner(struct typec_partner *partner);
0292 
0293 struct typec_cable *typec_register_cable(struct typec_port *port,
0294                      struct typec_cable_desc *desc);
0295 void typec_unregister_cable(struct typec_cable *cable);
0296 
0297 struct typec_cable *typec_cable_get(struct typec_port *port);
0298 void typec_cable_put(struct typec_cable *cable);
0299 int typec_cable_is_active(struct typec_cable *cable);
0300 
0301 struct typec_plug *typec_register_plug(struct typec_cable *cable,
0302                        struct typec_plug_desc *desc);
0303 void typec_unregister_plug(struct typec_plug *plug);
0304 
0305 void typec_set_data_role(struct typec_port *port, enum typec_data_role role);
0306 void typec_set_pwr_role(struct typec_port *port, enum typec_role role);
0307 void typec_set_vconn_role(struct typec_port *port, enum typec_role role);
0308 void typec_set_pwr_opmode(struct typec_port *port, enum typec_pwr_opmode mode);
0309 
0310 int typec_set_orientation(struct typec_port *port,
0311               enum typec_orientation orientation);
0312 enum typec_orientation typec_get_orientation(struct typec_port *port);
0313 int typec_set_mode(struct typec_port *port, int mode);
0314 
0315 void *typec_get_drvdata(struct typec_port *port);
0316 
0317 int typec_get_fw_cap(struct typec_capability *cap,
0318              struct fwnode_handle *fwnode);
0319 
0320 int typec_find_pwr_opmode(const char *name);
0321 int typec_find_orientation(const char *name);
0322 int typec_find_port_power_role(const char *name);
0323 int typec_find_power_role(const char *name);
0324 int typec_find_port_data_role(const char *name);
0325 
0326 void typec_partner_set_svdm_version(struct typec_partner *partner,
0327                     enum usb_pd_svdm_ver svdm_version);
0328 int typec_get_negotiated_svdm_version(struct typec_port *port);
0329 
0330 int typec_port_set_usb_power_delivery(struct typec_port *port, struct usb_power_delivery *pd);
0331 int typec_partner_set_usb_power_delivery(struct typec_partner *partner,
0332                      struct usb_power_delivery *pd);
0333 
0334 #endif /* __LINUX_USB_TYPEC_H */