1 /* SPDX-License-Identifier: GPL-2.0 */
3 #ifndef __LINUX_USB_TYPEC_H
4 #define __LINUX_USB_TYPEC_H
6 #include <linux/types.h>
8 /* USB Type-C Specification releases */
9 #define USB_TYPEC_REV_1_0 0x100 /* 1.0 */
10 #define USB_TYPEC_REV_1_1 0x110 /* 1.1 */
11 #define USB_TYPEC_REV_1_2 0x120 /* 1.2 */
12 #define USB_TYPEC_REV_1_3 0x130 /* 1.3 */
13 #define USB_TYPEC_REV_1_4 0x140 /* 1.4 */
14 #define USB_TYPEC_REV_2_0 0x200 /* 2.0 */
20 struct typec_altmode_ops;
25 struct usb_power_delivery;
27 enum typec_port_type {
33 enum typec_port_data {
39 enum typec_plug_type {
47 enum typec_data_role {
57 static inline int is_sink(enum typec_role role)
59 return role == TYPEC_SINK;
62 static inline int is_source(enum typec_role role)
64 return role == TYPEC_SOURCE;
67 enum typec_pwr_opmode {
74 enum typec_accessory {
76 TYPEC_ACCESSORY_AUDIO,
77 TYPEC_ACCESSORY_DEBUG,
80 #define TYPEC_MAX_ACCESSORY 3
82 enum typec_orientation {
83 TYPEC_ORIENTATION_NONE,
84 TYPEC_ORIENTATION_NORMAL,
85 TYPEC_ORIENTATION_REVERSE,
89 * struct enter_usb_data - Enter_USB Message details
90 * @eudo: Enter_USB Data Object
91 * @active_link_training: Active Cable Plug Link Training
93 * @active_link_training is a flag that should be set with uni-directional SBRX
94 * communication, and left 0 with passive cables and with bi-directional SBRX
97 struct enter_usb_data {
99 unsigned char active_link_training:1;
103 * struct usb_pd_identity - USB Power Delivery identity data
104 * @id_header: ID Header VDO
105 * @cert_stat: Cert Stat VDO
106 * @product: Product VDO
107 * @vdo: Product Type Specific VDOs
109 * USB power delivery Discover Identity command response data.
111 * REVISIT: This is USB Power Delivery specific information, so this structure
112 * probable belongs to USB Power Delivery header file once we have them.
114 struct usb_pd_identity {
121 int typec_partner_set_identity(struct typec_partner *partner);
122 int typec_cable_set_identity(struct typec_cable *cable);
125 * struct typec_altmode_desc - USB Type-C Alternate Mode Descriptor
126 * @svid: Standard or Vendor ID
127 * @mode: Index of the Mode
128 * @vdo: VDO returned by Discover Modes USB PD command
129 * @roles: Only for ports. DRP if the mode is available in both roles
131 * Description of an Alternate Mode which a connector, cable plug or partner
134 struct typec_altmode_desc {
138 /* Only used with ports */
139 enum typec_port_data roles;
142 void typec_partner_set_pd_revision(struct typec_partner *partner, u16 pd_revision);
143 int typec_partner_set_num_altmodes(struct typec_partner *partner, int num_altmodes);
145 *typec_partner_register_altmode(struct typec_partner *partner,
146 const struct typec_altmode_desc *desc);
147 int typec_plug_set_num_altmodes(struct typec_plug *plug, int num_altmodes);
149 *typec_plug_register_altmode(struct typec_plug *plug,
150 const struct typec_altmode_desc *desc);
152 *typec_port_register_altmode(struct typec_port *port,
153 const struct typec_altmode_desc *desc);
155 void typec_port_register_altmodes(struct typec_port *port,
156 const struct typec_altmode_ops *ops, void *drvdata,
157 struct typec_altmode **altmodes, size_t n);
159 void typec_unregister_altmode(struct typec_altmode *altmode);
161 struct typec_port *typec_altmode2port(struct typec_altmode *alt);
163 void typec_altmode_update_active(struct typec_altmode *alt, bool active);
165 enum typec_plug_index {
171 * struct typec_plug_desc - USB Type-C Cable Plug Descriptor
172 * @index: SOP Prime for the plug connected to DFP and SOP Double Prime for the
173 * plug connected to UFP
175 * Represents USB Type-C Cable Plug.
177 struct typec_plug_desc {
178 enum typec_plug_index index;
182 * struct typec_cable_desc - USB Type-C Cable Descriptor
183 * @type: The plug type from USB PD Cable VDO
184 * @active: Is the cable active or passive
185 * @identity: Result of Discover Identity command
186 * @pd_revision: USB Power Delivery Specification revision if supported
188 * Represents USB Type-C Cable attached to USB Type-C port.
190 struct typec_cable_desc {
191 enum typec_plug_type type;
192 unsigned int active:1;
193 struct usb_pd_identity *identity;
194 u16 pd_revision; /* 0300H = "3.0" */
199 * struct typec_partner_desc - USB Type-C Partner Descriptor
200 * @usb_pd: USB Power Delivery support
201 * @accessory: Audio, Debug or none.
202 * @identity: Discover Identity command data
203 * @pd_revision: USB Power Delivery Specification Revision if supported
205 * Details about a partner that is attached to USB Type-C port. If @identity
206 * member exists when partner is registered, a directory named "identity" is
207 * created to sysfs for the partner device.
209 * @pd_revision is based on the setting of the "Specification Revision" field
210 * in the message header on the initial "Source Capabilities" message received
211 * from the partner, or a "Request" message received from the partner, depending
212 * on whether our port is a Sink or a Source.
214 struct typec_partner_desc {
215 unsigned int usb_pd:1;
216 enum typec_accessory accessory;
217 struct usb_pd_identity *identity;
218 u16 pd_revision; /* 0300H = "3.0" */
222 * struct typec_operations - USB Type-C Port Operations
223 * @try_role: Set data role preference for DRP port
224 * @dr_set: Set Data Role
225 * @pr_set: Set Power Role
226 * @vconn_set: Source VCONN
227 * @port_type_set: Set port type
228 * @pd_get: Get available USB Power Delivery Capabilities.
229 * @pd_set: Set USB Power Delivery Capabilities.
231 struct typec_operations {
232 int (*try_role)(struct typec_port *port, int role);
233 int (*dr_set)(struct typec_port *port, enum typec_data_role role);
234 int (*pr_set)(struct typec_port *port, enum typec_role role);
235 int (*vconn_set)(struct typec_port *port, enum typec_role role);
236 int (*port_type_set)(struct typec_port *port,
237 enum typec_port_type type);
238 struct usb_power_delivery **(*pd_get)(struct typec_port *port);
239 int (*pd_set)(struct typec_port *port, struct usb_power_delivery *pd);
242 enum usb_pd_svdm_ver {
245 SVDM_VER_MAX = SVDM_VER_2_0,
249 * struct typec_capability - USB Type-C Port Capabilities
250 * @type: Supported power role of the port
251 * @data: Supported data role of the port
252 * @revision: USB Type-C Specification release. Binary coded decimal
253 * @pd_revision: USB Power Delivery Specification revision if supported
254 * @svdm_version: USB PD Structured VDM version if supported
255 * @prefer_role: Initial role preference (DRP ports).
256 * @accessory: Supported Accessory Modes
257 * @fwnode: Optional fwnode of the port
258 * @driver_data: Private pointer for driver specific info
259 * @pd: Optional USB Power Delivery Support
260 * @ops: Port operations vector
262 * Static capabilities of a single USB Type-C port.
264 struct typec_capability {
265 enum typec_port_type type;
266 enum typec_port_data data;
267 u16 revision; /* 0120H = "1.2" */
268 u16 pd_revision; /* 0300H = "3.0" */
269 enum usb_pd_svdm_ver svdm_version;
271 enum typec_accessory accessory[TYPEC_MAX_ACCESSORY];
272 unsigned int orientation_aware:1;
274 struct fwnode_handle *fwnode;
277 struct usb_power_delivery *pd;
279 const struct typec_operations *ops;
282 /* Specific to try_role(). Indicates the user want's to clear the preference. */
283 #define TYPEC_NO_PREFERRED_ROLE (-1)
285 struct typec_port *typec_register_port(struct device *parent,
286 const struct typec_capability *cap);
287 void typec_unregister_port(struct typec_port *port);
289 struct typec_partner *typec_register_partner(struct typec_port *port,
290 struct typec_partner_desc *desc);
291 void typec_unregister_partner(struct typec_partner *partner);
293 struct typec_cable *typec_register_cable(struct typec_port *port,
294 struct typec_cable_desc *desc);
295 void typec_unregister_cable(struct typec_cable *cable);
297 struct typec_cable *typec_cable_get(struct typec_port *port);
298 void typec_cable_put(struct typec_cable *cable);
299 int typec_cable_is_active(struct typec_cable *cable);
301 struct typec_plug *typec_register_plug(struct typec_cable *cable,
302 struct typec_plug_desc *desc);
303 void typec_unregister_plug(struct typec_plug *plug);
305 void typec_set_data_role(struct typec_port *port, enum typec_data_role role);
306 void typec_set_pwr_role(struct typec_port *port, enum typec_role role);
307 void typec_set_vconn_role(struct typec_port *port, enum typec_role role);
308 void typec_set_pwr_opmode(struct typec_port *port, enum typec_pwr_opmode mode);
310 int typec_set_orientation(struct typec_port *port,
311 enum typec_orientation orientation);
312 enum typec_orientation typec_get_orientation(struct typec_port *port);
313 int typec_set_mode(struct typec_port *port, int mode);
315 void *typec_get_drvdata(struct typec_port *port);
317 int typec_get_fw_cap(struct typec_capability *cap,
318 struct fwnode_handle *fwnode);
320 int typec_find_pwr_opmode(const char *name);
321 int typec_find_orientation(const char *name);
322 int typec_find_port_power_role(const char *name);
323 int typec_find_power_role(const char *name);
324 int typec_find_port_data_role(const char *name);
326 void typec_partner_set_svdm_version(struct typec_partner *partner,
327 enum usb_pd_svdm_ver svdm_version);
328 int typec_get_negotiated_svdm_version(struct typec_port *port);
330 int typec_port_set_usb_power_delivery(struct typec_port *port, struct usb_power_delivery *pd);
331 int typec_partner_set_usb_power_delivery(struct typec_partner *partner,
332 struct usb_power_delivery *pd);
334 #endif /* __LINUX_USB_TYPEC_H */