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
;
21 struct typec_cable_ops
;
27 struct usb_power_delivery
;
28 struct usb_power_delivery_desc
;
30 extern const struct bus_type typec_bus
;
32 enum typec_port_type
{
38 enum typec_port_data
{
44 enum typec_plug_type
{
52 enum typec_data_role
{
62 static inline int is_sink(enum typec_role role
)
64 return role
== TYPEC_SINK
;
67 static inline int is_source(enum typec_role role
)
69 return role
== TYPEC_SOURCE
;
72 enum typec_pwr_opmode
{
79 enum typec_accessory
{
81 TYPEC_ACCESSORY_AUDIO
,
82 TYPEC_ACCESSORY_DEBUG
,
85 #define TYPEC_MAX_ACCESSORY 3
87 enum typec_orientation
{
88 TYPEC_ORIENTATION_NONE
,
89 TYPEC_ORIENTATION_NORMAL
,
90 TYPEC_ORIENTATION_REVERSE
,
100 #define USB_CAPABILITY_USB2 BIT(0)
101 #define USB_CAPABILITY_USB3 BIT(1)
102 #define USB_CAPABILITY_USB4 BIT(2)
105 * struct enter_usb_data - Enter_USB Message details
106 * @eudo: Enter_USB Data Object
107 * @active_link_training: Active Cable Plug Link Training
109 * @active_link_training is a flag that should be set with uni-directional SBRX
110 * communication, and left 0 with passive cables and with bi-directional SBRX
113 struct enter_usb_data
{
115 unsigned char active_link_training
:1;
119 * struct usb_pd_identity - USB Power Delivery identity data
120 * @id_header: ID Header VDO
121 * @cert_stat: Cert Stat VDO
122 * @product: Product VDO
123 * @vdo: Product Type Specific VDOs
125 * USB power delivery Discover Identity command response data.
127 * REVISIT: This is USB Power Delivery specific information, so this structure
128 * probable belongs to USB Power Delivery header file once we have them.
130 struct usb_pd_identity
{
137 int typec_partner_set_identity(struct typec_partner
*partner
);
138 int typec_cable_set_identity(struct typec_cable
*cable
);
141 * struct typec_altmode_desc - USB Type-C Alternate Mode Descriptor
142 * @svid: Standard or Vendor ID
143 * @mode: Index of the Mode
144 * @vdo: VDO returned by Discover Modes USB PD command
145 * @roles: Only for ports. DRP if the mode is available in both roles
146 * @inactive: Only for ports. Make this port inactive (default is active).
148 * Description of an Alternate Mode which a connector, cable plug or partner
151 struct typec_altmode_desc
{
155 /* Only used with ports */
156 enum typec_port_data roles
;
161 void typec_partner_set_pd_revision(struct typec_partner
*partner
, u16 pd_revision
);
162 int typec_partner_set_num_altmodes(struct typec_partner
*partner
, int num_altmodes
);
164 *typec_partner_register_altmode(struct typec_partner
*partner
,
165 const struct typec_altmode_desc
*desc
);
166 int typec_plug_set_num_altmodes(struct typec_plug
*plug
, int num_altmodes
);
168 *typec_plug_register_altmode(struct typec_plug
*plug
,
169 const struct typec_altmode_desc
*desc
);
171 *typec_port_register_altmode(struct typec_port
*port
,
172 const struct typec_altmode_desc
*desc
);
174 void typec_port_register_altmodes(struct typec_port
*port
,
175 const struct typec_altmode_ops
*ops
, void *drvdata
,
176 struct typec_altmode
**altmodes
, size_t n
);
178 void typec_port_register_cable_ops(struct typec_altmode
**altmodes
, int max_altmodes
,
179 const struct typec_cable_ops
*ops
);
181 void typec_unregister_altmode(struct typec_altmode
*altmode
);
183 struct typec_port
*typec_altmode2port(struct typec_altmode
*alt
);
185 void typec_altmode_update_active(struct typec_altmode
*alt
, bool active
);
187 void typec_altmode_set_ops(struct typec_altmode
*alt
,
188 const struct typec_altmode_ops
*ops
);
190 enum typec_plug_index
{
196 * struct typec_plug_desc - USB Type-C Cable Plug Descriptor
197 * @index: SOP Prime for the plug connected to DFP and SOP Double Prime for the
198 * plug connected to UFP
200 * Represents USB Type-C Cable Plug.
202 struct typec_plug_desc
{
203 enum typec_plug_index index
;
207 * struct typec_cable_desc - USB Type-C Cable Descriptor
208 * @type: The plug type from USB PD Cable VDO
209 * @active: Is the cable active or passive
210 * @identity: Result of Discover Identity command
211 * @pd_revision: USB Power Delivery Specification revision if supported
213 * Represents USB Type-C Cable attached to USB Type-C port.
215 struct typec_cable_desc
{
216 enum typec_plug_type type
;
217 unsigned int active
:1;
218 struct usb_pd_identity
*identity
;
219 u16 pd_revision
; /* 0300H = "3.0" */
224 * struct typec_partner_desc - USB Type-C Partner Descriptor
225 * @usb_pd: USB Power Delivery support
226 * @accessory: Audio, Debug or none.
227 * @identity: Discover Identity command data
228 * @pd_revision: USB Power Delivery Specification Revision if supported
229 * @usb_capability: Supported USB Modes
230 * @attach: Notification about attached USB device
231 * @deattach: Notification about removed USB device
233 * Details about a partner that is attached to USB Type-C port. If @identity
234 * member exists when partner is registered, a directory named "identity" is
235 * created to sysfs for the partner device.
237 * @pd_revision is based on the setting of the "Specification Revision" field
238 * in the message header on the initial "Source Capabilities" message received
239 * from the partner, or a "Request" message received from the partner, depending
240 * on whether our port is a Sink or a Source.
242 struct typec_partner_desc
{
243 unsigned int usb_pd
:1;
244 enum typec_accessory accessory
;
245 struct usb_pd_identity
*identity
;
246 u16 pd_revision
; /* 0300H = "3.0" */
249 void (*attach
)(struct typec_partner
*partner
, struct device
*dev
);
250 void (*deattach
)(struct typec_partner
*partner
, struct device
*dev
);
254 * struct typec_operations - USB Type-C Port Operations
255 * @try_role: Set data role preference for DRP port
256 * @dr_set: Set Data Role
257 * @pr_set: Set Power Role
258 * @vconn_set: Source VCONN
259 * @port_type_set: Set port type
260 * @pd_get: Get available USB Power Delivery Capabilities.
261 * @pd_set: Set USB Power Delivery Capabilities.
262 * @default_usb_mode_set: USB Mode to be used by default with Enter_USB Message
263 * @enter_usb_mode: Change the active USB Mode
265 struct typec_operations
{
266 int (*try_role
)(struct typec_port
*port
, int role
);
267 int (*dr_set
)(struct typec_port
*port
, enum typec_data_role role
);
268 int (*pr_set
)(struct typec_port
*port
, enum typec_role role
);
269 int (*vconn_set
)(struct typec_port
*port
, enum typec_role role
);
270 int (*port_type_set
)(struct typec_port
*port
,
271 enum typec_port_type type
);
272 struct usb_power_delivery
**(*pd_get
)(struct typec_port
*port
);
273 int (*pd_set
)(struct typec_port
*port
, struct usb_power_delivery
*pd
);
274 int (*default_usb_mode_set
)(struct typec_port
*port
, enum usb_mode mode
);
275 int (*enter_usb_mode
)(struct typec_port
*port
, enum usb_mode mode
);
278 enum usb_pd_svdm_ver
{
281 SVDM_VER_MAX
= SVDM_VER_2_0
,
285 * struct typec_capability - USB Type-C Port Capabilities
286 * @type: Supported power role of the port
287 * @data: Supported data role of the port
288 * @revision: USB Type-C Specification release. Binary coded decimal
289 * @pd_revision: USB Power Delivery Specification revision if supported
290 * @svdm_version: USB PD Structured VDM version if supported
291 * @prefer_role: Initial role preference (DRP ports).
292 * @accessory: Supported Accessory Modes
293 * @usb_capability: Supported USB Modes
294 * @no_mode_control: Ability to manage Alternate Modes
295 * @fwnode: Optional fwnode of the port
296 * @driver_data: Private pointer for driver specific info
297 * @pd: Optional USB Power Delivery Support
298 * @ops: Port operations vector
300 * Static capabilities of a single USB Type-C port.
302 struct typec_capability
{
303 enum typec_port_type type
;
304 enum typec_port_data data
;
305 u16 revision
; /* 0120H = "1.2" */
306 u16 pd_revision
; /* 0300H = "3.0" */
307 enum usb_pd_svdm_ver svdm_version
;
309 enum typec_accessory accessory
[TYPEC_MAX_ACCESSORY
];
310 unsigned int orientation_aware
:1;
312 bool no_mode_control
;
314 struct fwnode_handle
*fwnode
;
317 struct usb_power_delivery
*pd
;
319 const struct typec_operations
*ops
;
322 /* Specific to try_role(). Indicates the user want's to clear the preference. */
323 #define TYPEC_NO_PREFERRED_ROLE (-1)
325 struct typec_port
*typec_register_port(struct device
*parent
,
326 const struct typec_capability
*cap
);
327 void typec_unregister_port(struct typec_port
*port
);
329 struct typec_partner
*typec_register_partner(struct typec_port
*port
,
330 struct typec_partner_desc
*desc
);
331 void typec_unregister_partner(struct typec_partner
*partner
);
333 struct typec_cable
*typec_register_cable(struct typec_port
*port
,
334 struct typec_cable_desc
*desc
);
335 void typec_unregister_cable(struct typec_cable
*cable
);
337 struct typec_cable
*typec_cable_get(struct typec_port
*port
);
338 void typec_cable_put(struct typec_cable
*cable
);
339 int typec_cable_is_active(struct typec_cable
*cable
);
341 struct typec_plug
*typec_register_plug(struct typec_cable
*cable
,
342 struct typec_plug_desc
*desc
);
343 void typec_unregister_plug(struct typec_plug
*plug
);
345 void typec_set_data_role(struct typec_port
*port
, enum typec_data_role role
);
346 enum typec_data_role
typec_get_data_role(struct typec_port
*port
);
347 void typec_set_pwr_role(struct typec_port
*port
, enum typec_role role
);
348 void typec_set_vconn_role(struct typec_port
*port
, enum typec_role role
);
349 void typec_set_pwr_opmode(struct typec_port
*port
, enum typec_pwr_opmode mode
);
351 int typec_set_orientation(struct typec_port
*port
,
352 enum typec_orientation orientation
);
353 enum typec_orientation
typec_get_orientation(struct typec_port
*port
);
354 int typec_set_mode(struct typec_port
*port
, int mode
);
356 void *typec_get_drvdata(struct typec_port
*port
);
358 int typec_get_fw_cap(struct typec_capability
*cap
,
359 struct fwnode_handle
*fwnode
);
361 int typec_find_pwr_opmode(const char *name
);
362 int typec_find_orientation(const char *name
);
363 int typec_find_port_power_role(const char *name
);
364 int typec_find_power_role(const char *name
);
365 int typec_find_port_data_role(const char *name
);
367 void typec_partner_set_svdm_version(struct typec_partner
*partner
,
368 enum usb_pd_svdm_ver svdm_version
);
369 int typec_get_negotiated_svdm_version(struct typec_port
*port
);
371 int typec_get_cable_svdm_version(struct typec_port
*port
);
372 void typec_cable_set_svdm_version(struct typec_cable
*cable
, enum usb_pd_svdm_ver svdm_version
);
374 struct usb_power_delivery
*typec_partner_usb_power_delivery_register(struct typec_partner
*partner
,
375 struct usb_power_delivery_desc
*desc
);
377 int typec_port_set_usb_power_delivery(struct typec_port
*port
, struct usb_power_delivery
*pd
);
378 int typec_partner_set_usb_power_delivery(struct typec_partner
*partner
,
379 struct usb_power_delivery
*pd
);
381 void typec_partner_set_usb_mode(struct typec_partner
*partner
, enum usb_mode usb_mode
);
382 void typec_port_set_usb_mode(struct typec_port
*port
, enum usb_mode mode
);
385 * struct typec_connector - Representation of Type-C port for external drivers
386 * @attach: notification about device removal
387 * @deattach: notification about device removal
389 * Drivers that control the USB and other ports (DisplayPorts, etc.), that are
390 * connected to the Type-C connectors, can use these callbacks to inform the
391 * Type-C connector class about connections and disconnections. That information
392 * can then be used by the typec-port drivers to power on or off parts that are
393 * needed or not needed - as an example, in USB mode if USB2 device is
394 * enumerated, USB3 components (retimers, phys, and what have you) do not need
397 * The attached (enumerated) devices will be liked with the typec-partner device.
399 struct typec_connector
{
400 void (*attach
)(struct typec_connector
*con
, struct device
*dev
);
401 void (*deattach
)(struct typec_connector
*con
, struct device
*dev
);
404 static inline void typec_attach(struct typec_connector
*con
, struct device
*dev
)
406 if (con
&& con
->attach
)
407 con
->attach(con
, dev
);
410 static inline void typec_deattach(struct typec_connector
*con
, struct device
*dev
)
412 if (con
&& con
->deattach
)
413 con
->deattach(con
, dev
);
416 #endif /* __LINUX_USB_TYPEC_H */