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