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