xref: /linux-6.15/include/linux/usb/typec.h (revision 8060bcb1)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #ifndef __LINUX_USB_TYPEC_H
4 #define __LINUX_USB_TYPEC_H
5 
6 #include <linux/types.h>
7 
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 */
15 
16 struct typec_partner;
17 struct typec_cable;
18 struct typec_plug;
19 struct typec_port;
20 struct typec_altmode_ops;
21 struct typec_cable_ops;
22 
23 struct fwnode_handle;
24 struct device;
25 
26 struct usb_power_delivery;
27 struct usb_power_delivery_desc;
28 
29 enum typec_port_type {
30 	TYPEC_PORT_SRC,
31 	TYPEC_PORT_SNK,
32 	TYPEC_PORT_DRP,
33 };
34 
35 enum typec_port_data {
36 	TYPEC_PORT_DFP,
37 	TYPEC_PORT_UFP,
38 	TYPEC_PORT_DRD,
39 };
40 
41 enum typec_plug_type {
42 	USB_PLUG_NONE,
43 	USB_PLUG_TYPE_A,
44 	USB_PLUG_TYPE_B,
45 	USB_PLUG_TYPE_C,
46 	USB_PLUG_CAPTIVE,
47 };
48 
49 enum typec_data_role {
50 	TYPEC_DEVICE,
51 	TYPEC_HOST,
52 };
53 
54 enum typec_role {
55 	TYPEC_SINK,
56 	TYPEC_SOURCE,
57 };
58 
59 static inline int is_sink(enum typec_role role)
60 {
61 	return role == TYPEC_SINK;
62 }
63 
64 static inline int is_source(enum typec_role role)
65 {
66 	return role == TYPEC_SOURCE;
67 }
68 
69 enum typec_pwr_opmode {
70 	TYPEC_PWR_MODE_USB,
71 	TYPEC_PWR_MODE_1_5A,
72 	TYPEC_PWR_MODE_3_0A,
73 	TYPEC_PWR_MODE_PD,
74 };
75 
76 enum typec_accessory {
77 	TYPEC_ACCESSORY_NONE,
78 	TYPEC_ACCESSORY_AUDIO,
79 	TYPEC_ACCESSORY_DEBUG,
80 };
81 
82 #define TYPEC_MAX_ACCESSORY	3
83 
84 enum typec_orientation {
85 	TYPEC_ORIENTATION_NONE,
86 	TYPEC_ORIENTATION_NORMAL,
87 	TYPEC_ORIENTATION_REVERSE,
88 };
89 
90 enum usb_mode {
91 	USB_MODE_NONE,
92 	USB_MODE_USB2,
93 	USB_MODE_USB3,
94 	USB_MODE_USB4
95 };
96 
97 #define USB_CAPABILITY_USB2	BIT(0)
98 #define USB_CAPABILITY_USB3	BIT(1)
99 #define USB_CAPABILITY_USB4	BIT(2)
100 
101 /*
102  * struct enter_usb_data - Enter_USB Message details
103  * @eudo: Enter_USB Data Object
104  * @active_link_training: Active Cable Plug Link Training
105  *
106  * @active_link_training is a flag that should be set with uni-directional SBRX
107  * communication, and left 0 with passive cables and with bi-directional SBRX
108  * communication.
109  */
110 struct enter_usb_data {
111 	u32			eudo;
112 	unsigned char		active_link_training:1;
113 };
114 
115 /*
116  * struct usb_pd_identity - USB Power Delivery identity data
117  * @id_header: ID Header VDO
118  * @cert_stat: Cert Stat VDO
119  * @product: Product VDO
120  * @vdo: Product Type Specific VDOs
121  *
122  * USB power delivery Discover Identity command response data.
123  *
124  * REVISIT: This is USB Power Delivery specific information, so this structure
125  * probable belongs to USB Power Delivery header file once we have them.
126  */
127 struct usb_pd_identity {
128 	u32			id_header;
129 	u32			cert_stat;
130 	u32			product;
131 	u32			vdo[3];
132 };
133 
134 int typec_partner_set_identity(struct typec_partner *partner);
135 int typec_cable_set_identity(struct typec_cable *cable);
136 
137 /*
138  * struct typec_altmode_desc - USB Type-C Alternate Mode Descriptor
139  * @svid: Standard or Vendor ID
140  * @mode: Index of the Mode
141  * @vdo: VDO returned by Discover Modes USB PD command
142  * @roles: Only for ports. DRP if the mode is available in both roles
143  *
144  * Description of an Alternate Mode which a connector, cable plug or partner
145  * supports.
146  */
147 struct typec_altmode_desc {
148 	u16			svid;
149 	u8			mode;
150 	u32			vdo;
151 	/* Only used with ports */
152 	enum typec_port_data	roles;
153 };
154 
155 void typec_partner_set_pd_revision(struct typec_partner *partner, u16 pd_revision);
156 int typec_partner_set_num_altmodes(struct typec_partner *partner, int num_altmodes);
157 struct typec_altmode
158 *typec_partner_register_altmode(struct typec_partner *partner,
159 				const struct typec_altmode_desc *desc);
160 int typec_plug_set_num_altmodes(struct typec_plug *plug, int num_altmodes);
161 struct typec_altmode
162 *typec_plug_register_altmode(struct typec_plug *plug,
163 			     const struct typec_altmode_desc *desc);
164 struct typec_altmode
165 *typec_port_register_altmode(struct typec_port *port,
166 			     const struct typec_altmode_desc *desc);
167 
168 void typec_port_register_altmodes(struct typec_port *port,
169 	const struct typec_altmode_ops *ops, void *drvdata,
170 	struct typec_altmode **altmodes, size_t n);
171 
172 void typec_port_register_cable_ops(struct typec_altmode **altmodes, int max_altmodes,
173 				   const struct typec_cable_ops *ops);
174 
175 void typec_unregister_altmode(struct typec_altmode *altmode);
176 
177 struct typec_port *typec_altmode2port(struct typec_altmode *alt);
178 
179 void typec_altmode_update_active(struct typec_altmode *alt, bool active);
180 
181 void typec_altmode_set_ops(struct typec_altmode *alt,
182 			   const struct typec_altmode_ops *ops);
183 
184 enum typec_plug_index {
185 	TYPEC_PLUG_SOP_P,
186 	TYPEC_PLUG_SOP_PP,
187 };
188 
189 /*
190  * struct typec_plug_desc - USB Type-C Cable Plug Descriptor
191  * @index: SOP Prime for the plug connected to DFP and SOP Double Prime for the
192  *         plug connected to UFP
193  *
194  * Represents USB Type-C Cable Plug.
195  */
196 struct typec_plug_desc {
197 	enum typec_plug_index	index;
198 };
199 
200 /*
201  * struct typec_cable_desc - USB Type-C Cable Descriptor
202  * @type: The plug type from USB PD Cable VDO
203  * @active: Is the cable active or passive
204  * @identity: Result of Discover Identity command
205  * @pd_revision: USB Power Delivery Specification revision if supported
206  *
207  * Represents USB Type-C Cable attached to USB Type-C port.
208  */
209 struct typec_cable_desc {
210 	enum typec_plug_type	type;
211 	unsigned int		active:1;
212 	struct usb_pd_identity	*identity;
213 	u16			pd_revision; /* 0300H = "3.0" */
214 
215 };
216 
217 /*
218  * struct typec_partner_desc - USB Type-C Partner Descriptor
219  * @usb_pd: USB Power Delivery support
220  * @accessory: Audio, Debug or none.
221  * @identity: Discover Identity command data
222  * @pd_revision: USB Power Delivery Specification Revision if supported
223  * @attach: Notification about attached USB device
224  * @deattach: Notification about removed USB device
225  *
226  * Details about a partner that is attached to USB Type-C port. If @identity
227  * member exists when partner is registered, a directory named "identity" is
228  * created to sysfs for the partner device.
229  *
230  * @pd_revision is based on the setting of the "Specification Revision" field
231  * in the message header on the initial "Source Capabilities" message received
232  * from the partner, or a "Request" message received from the partner, depending
233  * on whether our port is a Sink or a Source.
234  */
235 struct typec_partner_desc {
236 	unsigned int		usb_pd:1;
237 	enum typec_accessory	accessory;
238 	struct usb_pd_identity	*identity;
239 	u16			pd_revision; /* 0300H = "3.0" */
240 
241 	void (*attach)(struct typec_partner *partner, struct device *dev);
242 	void (*deattach)(struct typec_partner *partner, struct device *dev);
243 };
244 
245 /**
246  * struct typec_operations - USB Type-C Port Operations
247  * @try_role: Set data role preference for DRP port
248  * @dr_set: Set Data Role
249  * @pr_set: Set Power Role
250  * @vconn_set: Source VCONN
251  * @port_type_set: Set port type
252  * @pd_get: Get available USB Power Delivery Capabilities.
253  * @pd_set: Set USB Power Delivery Capabilities.
254  * @default_usb_mode_set: USB Mode to be used by default with Enter_USB Message
255  */
256 struct typec_operations {
257 	int (*try_role)(struct typec_port *port, int role);
258 	int (*dr_set)(struct typec_port *port, enum typec_data_role role);
259 	int (*pr_set)(struct typec_port *port, enum typec_role role);
260 	int (*vconn_set)(struct typec_port *port, enum typec_role role);
261 	int (*port_type_set)(struct typec_port *port,
262 			     enum typec_port_type type);
263 	struct usb_power_delivery **(*pd_get)(struct typec_port *port);
264 	int (*pd_set)(struct typec_port *port, struct usb_power_delivery *pd);
265 	int (*default_usb_mode_set)(struct typec_port *port, enum usb_mode mode);
266 };
267 
268 enum usb_pd_svdm_ver {
269 	SVDM_VER_1_0 = 0,
270 	SVDM_VER_2_0 = 1,
271 	SVDM_VER_MAX = SVDM_VER_2_0,
272 };
273 
274 /*
275  * struct typec_capability - USB Type-C Port Capabilities
276  * @type: Supported power role of the port
277  * @data: Supported data role of the port
278  * @revision: USB Type-C Specification release. Binary coded decimal
279  * @pd_revision: USB Power Delivery Specification revision if supported
280  * @svdm_version: USB PD Structured VDM version if supported
281  * @prefer_role: Initial role preference (DRP ports).
282  * @accessory: Supported Accessory Modes
283  * @usb_capability: Supported USB Modes
284  * @fwnode: Optional fwnode of the port
285  * @driver_data: Private pointer for driver specific info
286  * @pd: Optional USB Power Delivery Support
287  * @ops: Port operations vector
288  *
289  * Static capabilities of a single USB Type-C port.
290  */
291 struct typec_capability {
292 	enum typec_port_type	type;
293 	enum typec_port_data	data;
294 	u16			revision; /* 0120H = "1.2" */
295 	u16			pd_revision; /* 0300H = "3.0" */
296 	enum usb_pd_svdm_ver	svdm_version;
297 	int			prefer_role;
298 	enum typec_accessory	accessory[TYPEC_MAX_ACCESSORY];
299 	unsigned int		orientation_aware:1;
300 	u8			usb_capability;
301 
302 	struct fwnode_handle	*fwnode;
303 	void			*driver_data;
304 
305 	struct usb_power_delivery *pd;
306 
307 	const struct typec_operations	*ops;
308 };
309 
310 /* Specific to try_role(). Indicates the user want's to clear the preference. */
311 #define TYPEC_NO_PREFERRED_ROLE	(-1)
312 
313 struct typec_port *typec_register_port(struct device *parent,
314 				       const struct typec_capability *cap);
315 void typec_unregister_port(struct typec_port *port);
316 
317 struct typec_partner *typec_register_partner(struct typec_port *port,
318 					     struct typec_partner_desc *desc);
319 void typec_unregister_partner(struct typec_partner *partner);
320 
321 struct typec_cable *typec_register_cable(struct typec_port *port,
322 					 struct typec_cable_desc *desc);
323 void typec_unregister_cable(struct typec_cable *cable);
324 
325 struct typec_cable *typec_cable_get(struct typec_port *port);
326 void typec_cable_put(struct typec_cable *cable);
327 int typec_cable_is_active(struct typec_cable *cable);
328 
329 struct typec_plug *typec_register_plug(struct typec_cable *cable,
330 				       struct typec_plug_desc *desc);
331 void typec_unregister_plug(struct typec_plug *plug);
332 
333 void typec_set_data_role(struct typec_port *port, enum typec_data_role role);
334 void typec_set_pwr_role(struct typec_port *port, enum typec_role role);
335 void typec_set_vconn_role(struct typec_port *port, enum typec_role role);
336 void typec_set_pwr_opmode(struct typec_port *port, enum typec_pwr_opmode mode);
337 
338 int typec_set_orientation(struct typec_port *port,
339 			  enum typec_orientation orientation);
340 enum typec_orientation typec_get_orientation(struct typec_port *port);
341 int typec_set_mode(struct typec_port *port, int mode);
342 
343 void *typec_get_drvdata(struct typec_port *port);
344 
345 int typec_get_fw_cap(struct typec_capability *cap,
346 		     struct fwnode_handle *fwnode);
347 
348 int typec_find_pwr_opmode(const char *name);
349 int typec_find_orientation(const char *name);
350 int typec_find_port_power_role(const char *name);
351 int typec_find_power_role(const char *name);
352 int typec_find_port_data_role(const char *name);
353 
354 void typec_partner_set_svdm_version(struct typec_partner *partner,
355 				    enum usb_pd_svdm_ver svdm_version);
356 int typec_get_negotiated_svdm_version(struct typec_port *port);
357 
358 int typec_get_cable_svdm_version(struct typec_port *port);
359 void typec_cable_set_svdm_version(struct typec_cable *cable, enum usb_pd_svdm_ver svdm_version);
360 
361 struct usb_power_delivery *typec_partner_usb_power_delivery_register(struct typec_partner *partner,
362 							struct usb_power_delivery_desc *desc);
363 
364 int typec_port_set_usb_power_delivery(struct typec_port *port, struct usb_power_delivery *pd);
365 int typec_partner_set_usb_power_delivery(struct typec_partner *partner,
366 					 struct usb_power_delivery *pd);
367 
368 void typec_port_set_usb_mode(struct typec_port *port, enum usb_mode mode);
369 
370 /**
371  * struct typec_connector - Representation of Type-C port for external drivers
372  * @attach: notification about device removal
373  * @deattach: notification about device removal
374  *
375  * Drivers that control the USB and other ports (DisplayPorts, etc.), that are
376  * connected to the Type-C connectors, can use these callbacks to inform the
377  * Type-C connector class about connections and disconnections. That information
378  * can then be used by the typec-port drivers to power on or off parts that are
379  * needed or not needed - as an example, in USB mode if USB2 device is
380  * enumerated, USB3 components (retimers, phys, and what have you) do not need
381  * to be powered on.
382  *
383  * The attached (enumerated) devices will be liked with the typec-partner device.
384  */
385 struct typec_connector {
386 	void (*attach)(struct typec_connector *con, struct device *dev);
387 	void (*deattach)(struct typec_connector *con, struct device *dev);
388 };
389 
390 static inline void typec_attach(struct typec_connector *con, struct device *dev)
391 {
392 	if (con && con->attach)
393 		con->attach(con, dev);
394 }
395 
396 static inline void typec_deattach(struct typec_connector *con, struct device *dev)
397 {
398 	if (con && con->deattach)
399 		con->deattach(con, dev);
400 }
401 
402 #endif /* __LINUX_USB_TYPEC_H */
403