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