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
22 struct fwnode_handle;
23 struct device;
24
25 enum typec_port_type {
26 TYPEC_PORT_SRC,
27 TYPEC_PORT_SNK,
28 TYPEC_PORT_DRP,
29 };
30
31 enum typec_port_data {
32 TYPEC_PORT_DFP,
33 TYPEC_PORT_UFP,
34 TYPEC_PORT_DRD,
35 };
36
37 enum typec_plug_type {
38 USB_PLUG_NONE,
39 USB_PLUG_TYPE_A,
40 USB_PLUG_TYPE_B,
41 USB_PLUG_TYPE_C,
42 USB_PLUG_CAPTIVE,
43 };
44
45 enum typec_data_role {
46 TYPEC_DEVICE,
47 TYPEC_HOST,
48 };
49
50 enum typec_role {
51 TYPEC_SINK,
52 TYPEC_SOURCE,
53 };
54
55 enum typec_pwr_opmode {
56 TYPEC_PWR_MODE_USB,
57 TYPEC_PWR_MODE_1_5A,
58 TYPEC_PWR_MODE_3_0A,
59 TYPEC_PWR_MODE_PD,
60 };
61
62 enum typec_accessory {
63 TYPEC_ACCESSORY_NONE,
64 TYPEC_ACCESSORY_AUDIO,
65 TYPEC_ACCESSORY_DEBUG,
66 };
67
68 #define TYPEC_MAX_ACCESSORY 3
69
70 enum typec_orientation {
71 TYPEC_ORIENTATION_NONE,
72 TYPEC_ORIENTATION_NORMAL,
73 TYPEC_ORIENTATION_REVERSE,
74 };
75
76 /*
77 * struct enter_usb_data - Enter_USB Message details
78 * @eudo: Enter_USB Data Object
79 * @active_link_training: Active Cable Plug Link Training
80 *
81 * @active_link_training is a flag that should be set with uni-directional SBRX
82 * communication, and left 0 with passive cables and with bi-directional SBRX
83 * communication.
84 */
85 struct enter_usb_data {
86 u32 eudo;
87 unsigned char active_link_training:1;
88 };
89
90 /*
91 * struct usb_pd_identity - USB Power Delivery identity data
92 * @id_header: ID Header VDO
93 * @cert_stat: Cert Stat VDO
94 * @product: Product VDO
95 * @vdo: Product Type Specific VDOs
96 *
97 * USB power delivery Discover Identity command response data.
98 *
99 * REVISIT: This is USB Power Delivery specific information, so this structure
100 * probable belongs to USB Power Delivery header file once we have them.
101 */
102 struct usb_pd_identity {
103 u32 id_header;
104 u32 cert_stat;
105 u32 product;
106 u32 vdo[3];
107 };
108
109 int typec_partner_set_identity(struct typec_partner *partner);
110 int typec_cable_set_identity(struct typec_cable *cable);
111
112 /*
113 * struct typec_altmode_desc - USB Type-C Alternate Mode Descriptor
114 * @svid: Standard or Vendor ID
115 * @mode: Index of the Mode
116 * @vdo: VDO returned by Discover Modes USB PD command
117 * @roles: Only for ports. DRP if the mode is available in both roles
118 *
119 * Description of an Alternate Mode which a connector, cable plug or partner
120 * supports.
121 */
122 struct typec_altmode_desc {
123 u16 svid;
124 u8 mode;
125 u32 vdo;
126 /* Only used with ports */
127 enum typec_port_data roles;
128 };
129
130 void typec_partner_set_pd_revision(struct typec_partner *partner, u16 pd_revision);
131 int typec_partner_set_num_altmodes(struct typec_partner *partner, int num_altmodes);
132 struct typec_altmode
133 *typec_partner_register_altmode(struct typec_partner *partner,
134 const struct typec_altmode_desc *desc);
135 int typec_plug_set_num_altmodes(struct typec_plug *plug, int num_altmodes);
136 struct typec_altmode
137 *typec_plug_register_altmode(struct typec_plug *plug,
138 const struct typec_altmode_desc *desc);
139 struct typec_altmode
140 *typec_port_register_altmode(struct typec_port *port,
141 const struct typec_altmode_desc *desc);
142
143 #ifdef CONFIG_NO_GKI
144 void typec_port_register_altmodes(struct typec_port *port,
145 const struct typec_altmode_ops *ops, void *drvdata,
146 struct typec_altmode **altmodes, size_t n);
147 #else
typec_port_register_altmodes(struct typec_port * port,const struct typec_altmode_ops * ops,void * drvdata,struct typec_altmode ** altmodes,size_t n)148 static inline void typec_port_register_altmodes(struct typec_port *port,
149 const struct typec_altmode_ops *ops, void *drvdata,
150 struct typec_altmode **altmodes, size_t n)
151 {
152 }
153 #endif
154
155 void typec_unregister_altmode(struct typec_altmode *altmode);
156
157 struct typec_port *typec_altmode2port(struct typec_altmode *alt);
158
159 void typec_altmode_update_active(struct typec_altmode *alt, bool active);
160
161 enum typec_plug_index {
162 TYPEC_PLUG_SOP_P,
163 TYPEC_PLUG_SOP_PP,
164 };
165
166 /*
167 * struct typec_plug_desc - USB Type-C Cable Plug Descriptor
168 * @index: SOP Prime for the plug connected to DFP and SOP Double Prime for the
169 * plug connected to UFP
170 *
171 * Represents USB Type-C Cable Plug.
172 */
173 struct typec_plug_desc {
174 enum typec_plug_index index;
175 };
176
177 /*
178 * struct typec_cable_desc - USB Type-C Cable Descriptor
179 * @type: The plug type from USB PD Cable VDO
180 * @active: Is the cable active or passive
181 * @identity: Result of Discover Identity command
182 * @pd_revision: USB Power Delivery Specification revision if supported
183 *
184 * Represents USB Type-C Cable attached to USB Type-C port.
185 */
186 struct typec_cable_desc {
187 enum typec_plug_type type;
188 unsigned int active:1;
189 struct usb_pd_identity *identity;
190 u16 pd_revision; /* 0300H = "3.0" */
191
192 };
193
194 /*
195 * struct typec_partner_desc - USB Type-C Partner Descriptor
196 * @usb_pd: USB Power Delivery support
197 * @accessory: Audio, Debug or none.
198 * @identity: Discover Identity command data
199 * @pd_revision: USB Power Delivery Specification Revision if supported
200 *
201 * Details about a partner that is attached to USB Type-C port. If @identity
202 * member exists when partner is registered, a directory named "identity" is
203 * created to sysfs for the partner device.
204 *
205 * @pd_revision is based on the setting of the "Specification Revision" field
206 * in the message header on the initial "Source Capabilities" message received
207 * from the partner, or a "Request" message received from the partner, depending
208 * on whether our port is a Sink or a Source.
209 */
210 struct typec_partner_desc {
211 unsigned int usb_pd:1;
212 enum typec_accessory accessory;
213 struct usb_pd_identity *identity;
214 u16 pd_revision; /* 0300H = "3.0" */
215 };
216
217 /**
218 * struct typec_operations - USB Type-C Port Operations
219 * @try_role: Set data role preference for DRP port
220 * @dr_set: Set Data Role
221 * @pr_set: Set Power Role
222 * @vconn_set: Source VCONN
223 * @port_type_set: Set port type
224 */
225 struct typec_operations {
226 int (*try_role)(struct typec_port *port, int role);
227 int (*dr_set)(struct typec_port *port, enum typec_data_role role);
228 int (*pr_set)(struct typec_port *port, enum typec_role role);
229 int (*vconn_set)(struct typec_port *port, enum typec_role role);
230 int (*port_type_set)(struct typec_port *port,
231 enum typec_port_type type);
232 };
233
234 enum usb_pd_svdm_ver {
235 SVDM_VER_1_0 = 0,
236 SVDM_VER_2_0 = 1,
237 SVDM_VER_MAX = SVDM_VER_2_0,
238 };
239
240 /*
241 * struct typec_capability - USB Type-C Port Capabilities
242 * @type: Supported power role of the port
243 * @data: Supported data role of the port
244 * @revision: USB Type-C Specification release. Binary coded decimal
245 * @pd_revision: USB Power Delivery Specification revision if supported
246 * @svdm_version: USB PD Structured VDM version if supported
247 * @prefer_role: Initial role preference (DRP ports).
248 * @accessory: Supported Accessory Modes
249 * @fwnode: Optional fwnode of the port
250 * @driver_data: Private pointer for driver specific info
251 * @ops: Port operations vector
252 *
253 * Static capabilities of a single USB Type-C port.
254 */
255 struct typec_capability {
256 enum typec_port_type type;
257 enum typec_port_data data;
258 u16 revision; /* 0120H = "1.2" */
259 u16 pd_revision; /* 0300H = "3.0" */
260 enum usb_pd_svdm_ver svdm_version;
261 int prefer_role;
262 enum typec_accessory accessory[TYPEC_MAX_ACCESSORY];
263 unsigned int orientation_aware:1;
264
265 struct fwnode_handle *fwnode;
266 void *driver_data;
267
268 const struct typec_operations *ops;
269 };
270
271 /* Specific to try_role(). Indicates the user want's to clear the preference. */
272 #define TYPEC_NO_PREFERRED_ROLE (-1)
273
274 struct typec_port *typec_register_port(struct device *parent,
275 const struct typec_capability *cap);
276 void typec_unregister_port(struct typec_port *port);
277
278 struct typec_partner *typec_register_partner(struct typec_port *port,
279 struct typec_partner_desc *desc);
280 void typec_unregister_partner(struct typec_partner *partner);
281
282 struct typec_cable *typec_register_cable(struct typec_port *port,
283 struct typec_cable_desc *desc);
284 void typec_unregister_cable(struct typec_cable *cable);
285
286 struct typec_cable *typec_cable_get(struct typec_port *port);
287 void typec_cable_put(struct typec_cable *cable);
288 int typec_cable_is_active(struct typec_cable *cable);
289
290 struct typec_plug *typec_register_plug(struct typec_cable *cable,
291 struct typec_plug_desc *desc);
292 void typec_unregister_plug(struct typec_plug *plug);
293
294 void typec_set_data_role(struct typec_port *port, enum typec_data_role role);
295 void typec_set_pwr_role(struct typec_port *port, enum typec_role role);
296 void typec_set_vconn_role(struct typec_port *port, enum typec_role role);
297 void typec_set_pwr_opmode(struct typec_port *port, enum typec_pwr_opmode mode);
298
299 int typec_set_orientation(struct typec_port *port,
300 enum typec_orientation orientation);
301 enum typec_orientation typec_get_orientation(struct typec_port *port);
302 int typec_set_mode(struct typec_port *port, int mode);
303
304 void *typec_get_drvdata(struct typec_port *port);
305
306 int typec_find_pwr_opmode(const char *name);
307 int typec_find_orientation(const char *name);
308 int typec_find_port_power_role(const char *name);
309 int typec_find_power_role(const char *name);
310 int typec_find_port_data_role(const char *name);
311
312 void typec_partner_set_svdm_version(struct typec_partner *partner,
313 enum usb_pd_svdm_ver svdm_version);
314 int typec_get_negotiated_svdm_version(struct typec_port *port);
315 #endif /* __LINUX_USB_TYPEC_H */
316