• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
13 struct typec_partner;
14 struct typec_cable;
15 struct typec_plug;
16 struct typec_port;
17 
18 struct fwnode_handle;
19 struct device;
20 
21 enum typec_port_type {
22 	TYPEC_PORT_SRC,
23 	TYPEC_PORT_SNK,
24 	TYPEC_PORT_DRP,
25 };
26 
27 enum typec_port_data {
28 	TYPEC_PORT_DFP,
29 	TYPEC_PORT_UFP,
30 	TYPEC_PORT_DRD,
31 };
32 
33 enum typec_plug_type {
34 	USB_PLUG_NONE,
35 	USB_PLUG_TYPE_A,
36 	USB_PLUG_TYPE_B,
37 	USB_PLUG_TYPE_C,
38 	USB_PLUG_CAPTIVE,
39 };
40 
41 enum typec_data_role {
42 	TYPEC_DEVICE,
43 	TYPEC_HOST,
44 };
45 
46 enum typec_role {
47 	TYPEC_SINK,
48 	TYPEC_SOURCE,
49 };
50 
51 enum typec_pwr_opmode {
52 	TYPEC_PWR_MODE_USB,
53 	TYPEC_PWR_MODE_1_5A,
54 	TYPEC_PWR_MODE_3_0A,
55 	TYPEC_PWR_MODE_PD,
56 };
57 
58 enum typec_accessory {
59 	TYPEC_ACCESSORY_NONE,
60 	TYPEC_ACCESSORY_AUDIO,
61 	TYPEC_ACCESSORY_DEBUG,
62 };
63 
64 #define TYPEC_MAX_ACCESSORY	3
65 
66 enum typec_orientation {
67 	TYPEC_ORIENTATION_NONE,
68 	TYPEC_ORIENTATION_NORMAL,
69 	TYPEC_ORIENTATION_REVERSE,
70 };
71 
72 /*
73  * struct usb_pd_identity - USB Power Delivery identity data
74  * @id_header: ID Header VDO
75  * @cert_stat: Cert Stat VDO
76  * @product: Product VDO
77  *
78  * USB power delivery Discover Identity command response data.
79  *
80  * REVISIT: This is USB Power Delivery specific information, so this structure
81  * probable belongs to USB Power Delivery header file once we have them.
82  */
83 struct usb_pd_identity {
84 	u32			id_header;
85 	u32			cert_stat;
86 	u32			product;
87 };
88 
89 int typec_partner_set_identity(struct typec_partner *partner);
90 int typec_cable_set_identity(struct typec_cable *cable);
91 
92 /*
93  * struct typec_altmode_desc - USB Type-C Alternate Mode Descriptor
94  * @svid: Standard or Vendor ID
95  * @mode: Index of the Mode
96  * @vdo: VDO returned by Discover Modes USB PD command
97  * @roles: Only for ports. DRP if the mode is available in both roles
98  *
99  * Description of an Alternate Mode which a connector, cable plug or partner
100  * supports.
101  */
102 struct typec_altmode_desc {
103 	u16			svid;
104 	u8			mode;
105 	u32			vdo;
106 	/* Only used with ports */
107 	enum typec_port_data	roles;
108 };
109 
110 struct typec_altmode
111 *typec_partner_register_altmode(struct typec_partner *partner,
112 				const struct typec_altmode_desc *desc);
113 struct typec_altmode
114 *typec_plug_register_altmode(struct typec_plug *plug,
115 			     const struct typec_altmode_desc *desc);
116 struct typec_altmode
117 *typec_port_register_altmode(struct typec_port *port,
118 			     const struct typec_altmode_desc *desc);
119 void typec_unregister_altmode(struct typec_altmode *altmode);
120 
121 struct typec_port *typec_altmode2port(struct typec_altmode *alt);
122 
123 void typec_altmode_update_active(struct typec_altmode *alt, bool active);
124 
125 enum typec_plug_index {
126 	TYPEC_PLUG_SOP_P,
127 	TYPEC_PLUG_SOP_PP,
128 };
129 
130 /*
131  * struct typec_plug_desc - USB Type-C Cable Plug Descriptor
132  * @index: SOP Prime for the plug connected to DFP and SOP Double Prime for the
133  *         plug connected to UFP
134  *
135  * Represents USB Type-C Cable Plug.
136  */
137 struct typec_plug_desc {
138 	enum typec_plug_index	index;
139 };
140 
141 /*
142  * struct typec_cable_desc - USB Type-C Cable Descriptor
143  * @type: The plug type from USB PD Cable VDO
144  * @active: Is the cable active or passive
145  * @identity: Result of Discover Identity command
146  *
147  * Represents USB Type-C Cable attached to USB Type-C port.
148  */
149 struct typec_cable_desc {
150 	enum typec_plug_type	type;
151 	unsigned int		active:1;
152 	struct usb_pd_identity	*identity;
153 };
154 
155 /*
156  * struct typec_partner_desc - USB Type-C Partner Descriptor
157  * @usb_pd: USB Power Delivery support
158  * @accessory: Audio, Debug or none.
159  * @identity: Discover Identity command data
160  *
161  * Details about a partner that is attached to USB Type-C port. If @identity
162  * member exists when partner is registered, a directory named "identity" is
163  * created to sysfs for the partner device.
164  */
165 struct typec_partner_desc {
166 	unsigned int		usb_pd:1;
167 	enum typec_accessory	accessory;
168 	struct usb_pd_identity	*identity;
169 };
170 
171 /**
172  * struct typec_operations - USB Type-C Port Operations
173  * @try_role: Set data role preference for DRP port
174  * @dr_set: Set Data Role
175  * @pr_set: Set Power Role
176  * @vconn_set: Source VCONN
177  * @port_type_set: Set port type
178  */
179 struct typec_operations {
180 	int (*try_role)(struct typec_port *port, int role);
181 	int (*dr_set)(struct typec_port *port, enum typec_data_role role);
182 	int (*pr_set)(struct typec_port *port, enum typec_role role);
183 	int (*vconn_set)(struct typec_port *port, enum typec_role role);
184 	int (*port_type_set)(struct typec_port *port,
185 			     enum typec_port_type type);
186 };
187 
188 /*
189  * struct typec_capability - USB Type-C Port Capabilities
190  * @type: Supported power role of the port
191  * @data: Supported data role of the port
192  * @revision: USB Type-C Specification release. Binary coded decimal
193  * @pd_revision: USB Power Delivery Specification revision if supported
194  * @prefer_role: Initial role preference (DRP ports).
195  * @accessory: Supported Accessory Modes
196  * @sw: Cable plug orientation switch
197  * @mux: Multiplexer switch for Alternate/Accessory Modes
198  * @fwnode: Optional fwnode of the port
199  * @driver_data: Private pointer for driver specific info
200  * @ops: Port operations vector
201  *
202  * Static capabilities of a single USB Type-C port.
203  */
204 struct typec_capability {
205 	enum typec_port_type	type;
206 	enum typec_port_data	data;
207 	u16			revision; /* 0120H = "1.2" */
208 	u16			pd_revision; /* 0300H = "3.0" */
209 	int			prefer_role;
210 	enum typec_accessory	accessory[TYPEC_MAX_ACCESSORY];
211 
212 	struct typec_switch	*sw;
213 	struct typec_mux	*mux;
214 	struct fwnode_handle	*fwnode;
215 	void			*driver_data;
216 
217 	const struct typec_operations	*ops;
218 };
219 
220 /* Specific to try_role(). Indicates the user want's to clear the preference. */
221 #define TYPEC_NO_PREFERRED_ROLE	(-1)
222 
223 struct typec_port *typec_register_port(struct device *parent,
224 				       const struct typec_capability *cap);
225 void typec_unregister_port(struct typec_port *port);
226 
227 struct typec_partner *typec_register_partner(struct typec_port *port,
228 					     struct typec_partner_desc *desc);
229 void typec_unregister_partner(struct typec_partner *partner);
230 
231 struct typec_cable *typec_register_cable(struct typec_port *port,
232 					 struct typec_cable_desc *desc);
233 void typec_unregister_cable(struct typec_cable *cable);
234 
235 struct typec_plug *typec_register_plug(struct typec_cable *cable,
236 				       struct typec_plug_desc *desc);
237 void typec_unregister_plug(struct typec_plug *plug);
238 
239 void typec_set_data_role(struct typec_port *port, enum typec_data_role role);
240 void typec_set_pwr_role(struct typec_port *port, enum typec_role role);
241 void typec_set_vconn_role(struct typec_port *port, enum typec_role role);
242 void typec_set_pwr_opmode(struct typec_port *port, enum typec_pwr_opmode mode);
243 
244 int typec_set_orientation(struct typec_port *port,
245 			  enum typec_orientation orientation);
246 enum typec_orientation typec_get_orientation(struct typec_port *port);
247 int typec_set_mode(struct typec_port *port, int mode);
248 
249 void *typec_get_drvdata(struct typec_port *port);
250 
251 int typec_find_port_power_role(const char *name);
252 int typec_find_power_role(const char *name);
253 int typec_find_port_data_role(const char *name);
254 #endif /* __LINUX_USB_TYPEC_H */
255