]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
de4217d9 | 2 | /* |
07673204 | 3 | * USB PHY defines |
de4217d9 VB |
4 | * |
5 | * These APIs may be used between USB controllers. USB device drivers | |
6 | * (for either host or peripheral roles) don't use these calls; they | |
7 | * continue to use just usb_device and usb_gadget. | |
8 | */ | |
9 | ||
10 | #ifndef __LINUX_USB_PHY_H | |
11 | #define __LINUX_USB_PHY_H | |
12 | ||
7d21114d | 13 | #include <linux/extcon.h> |
de4217d9 | 14 | #include <linux/notifier.h> |
ac96511b | 15 | #include <linux/usb.h> |
a9081a00 | 16 | #include <uapi/linux/usb/charger.h> |
de4217d9 | 17 | |
1c9af653 MG |
18 | enum usb_phy_interface { |
19 | USBPHY_INTERFACE_MODE_UNKNOWN, | |
20 | USBPHY_INTERFACE_MODE_UTMI, | |
21 | USBPHY_INTERFACE_MODE_UTMIW, | |
22 | USBPHY_INTERFACE_MODE_ULPI, | |
23 | USBPHY_INTERFACE_MODE_SERIAL, | |
24 | USBPHY_INTERFACE_MODE_HSIC, | |
25 | }; | |
26 | ||
de4217d9 VB |
27 | enum usb_phy_events { |
28 | USB_EVENT_NONE, /* no events or cable disconnected */ | |
29 | USB_EVENT_VBUS, /* vbus valid event */ | |
30 | USB_EVENT_ID, /* id was grounded */ | |
31 | USB_EVENT_CHARGER, /* usb dedicated charger */ | |
32 | USB_EVENT_ENUMERATED, /* gadget driver enumerated */ | |
33 | }; | |
34 | ||
35 | /* associate a type with PHY */ | |
36 | enum usb_phy_type { | |
37 | USB_PHY_TYPE_UNDEFINED, | |
38 | USB_PHY_TYPE_USB2, | |
39 | USB_PHY_TYPE_USB3, | |
40 | }; | |
41 | ||
a4c3ddec VB |
42 | /* OTG defines lots of enumeration states before device reset */ |
43 | enum usb_otg_state { | |
44 | OTG_STATE_UNDEFINED = 0, | |
45 | ||
46 | /* single-role peripheral, and dual-role default-b */ | |
47 | OTG_STATE_B_IDLE, | |
48 | OTG_STATE_B_SRP_INIT, | |
49 | OTG_STATE_B_PERIPHERAL, | |
50 | ||
51 | /* extra dual-role default-b states */ | |
52 | OTG_STATE_B_WAIT_ACON, | |
53 | OTG_STATE_B_HOST, | |
54 | ||
55 | /* dual-role default-a */ | |
56 | OTG_STATE_A_IDLE, | |
57 | OTG_STATE_A_WAIT_VRISE, | |
58 | OTG_STATE_A_WAIT_BCON, | |
59 | OTG_STATE_A_HOST, | |
60 | OTG_STATE_A_SUSPEND, | |
61 | OTG_STATE_A_PERIPHERAL, | |
62 | OTG_STATE_A_WAIT_VFALL, | |
63 | OTG_STATE_A_VBUS_ERR, | |
64 | }; | |
65 | ||
de4217d9 VB |
66 | struct usb_phy; |
67 | struct usb_otg; | |
68 | ||
58efd4b0 | 69 | /* for phys connected thru an ULPI interface, the user must |
de4217d9 VB |
70 | * provide access ops |
71 | */ | |
72 | struct usb_phy_io_ops { | |
73 | int (*read)(struct usb_phy *x, u32 reg); | |
74 | int (*write)(struct usb_phy *x, u32 val, u32 reg); | |
75 | }; | |
76 | ||
a9081a00 BW |
77 | struct usb_charger_current { |
78 | unsigned int sdp_min; | |
79 | unsigned int sdp_max; | |
80 | unsigned int dcp_min; | |
81 | unsigned int dcp_max; | |
82 | unsigned int cdp_min; | |
83 | unsigned int cdp_max; | |
84 | unsigned int aca_min; | |
85 | unsigned int aca_max; | |
86 | }; | |
87 | ||
de4217d9 VB |
88 | struct usb_phy { |
89 | struct device *dev; | |
90 | const char *label; | |
91 | unsigned int flags; | |
92 | ||
93 | enum usb_phy_type type; | |
94 | enum usb_phy_events last_event; | |
95 | ||
96 | struct usb_otg *otg; | |
97 | ||
98 | struct device *io_dev; | |
99 | struct usb_phy_io_ops *io_ops; | |
100 | void __iomem *io_priv; | |
101 | ||
7d21114d BW |
102 | /* to support extcon device */ |
103 | struct extcon_dev *edev; | |
104 | struct extcon_dev *id_edev; | |
105 | struct notifier_block vbus_nb; | |
106 | struct notifier_block id_nb; | |
a9081a00 BW |
107 | struct notifier_block type_nb; |
108 | ||
109 | /* Support USB charger */ | |
110 | enum usb_charger_type chg_type; | |
111 | enum usb_charger_state chg_state; | |
112 | struct usb_charger_current chg_cur; | |
113 | struct work_struct chg_work; | |
7d21114d | 114 | |
de4217d9 VB |
115 | /* for notification of usb_phy_events */ |
116 | struct atomic_notifier_head notifier; | |
117 | ||
118 | /* to pass extra port status to the root hub */ | |
119 | u16 port_status; | |
120 | u16 port_change; | |
121 | ||
58efd4b0 | 122 | /* to support controllers that have multiple phys */ |
de4217d9 VB |
123 | struct list_head head; |
124 | ||
58efd4b0 | 125 | /* initialize/shutdown the phy */ |
de4217d9 VB |
126 | int (*init)(struct usb_phy *x); |
127 | void (*shutdown)(struct usb_phy *x); | |
128 | ||
b774212e FB |
129 | /* enable/disable VBUS */ |
130 | int (*set_vbus)(struct usb_phy *x, int on); | |
131 | ||
de4217d9 VB |
132 | /* effective for B devices, ignored for A-peripheral */ |
133 | int (*set_power)(struct usb_phy *x, | |
134 | unsigned mA); | |
135 | ||
58efd4b0 | 136 | /* Set phy into suspend mode */ |
de4217d9 VB |
137 | int (*set_suspend)(struct usb_phy *x, |
138 | int suspend); | |
139 | ||
57bf9b09 PC |
140 | /* |
141 | * Set wakeup enable for PHY, in that case, the PHY can be | |
142 | * woken up from suspend status due to external events, | |
143 | * like vbus change, dp/dm change and id. | |
144 | */ | |
145 | int (*set_wakeup)(struct usb_phy *x, bool enabled); | |
146 | ||
de4217d9 | 147 | /* notify phy connect status change */ |
ac96511b PC |
148 | int (*notify_connect)(struct usb_phy *x, |
149 | enum usb_device_speed speed); | |
150 | int (*notify_disconnect)(struct usb_phy *x, | |
151 | enum usb_device_speed speed); | |
a9081a00 BW |
152 | |
153 | /* | |
154 | * Charger detection method can be implemented if you need to | |
155 | * manually detect the charger type. | |
156 | */ | |
157 | enum usb_charger_type (*charger_detect)(struct usb_phy *x); | |
de4217d9 VB |
158 | }; |
159 | ||
b4a83e4d KVA |
160 | /** |
161 | * struct usb_phy_bind - represent the binding for the phy | |
162 | * @dev_name: the device name of the device that will bind to the phy | |
163 | * @phy_dev_name: the device name of the phy | |
164 | * @index: used if a single controller uses multiple phys | |
165 | * @phy: reference to the phy | |
166 | * @list: to maintain a linked list of the binding information | |
167 | */ | |
168 | struct usb_phy_bind { | |
169 | const char *dev_name; | |
170 | const char *phy_dev_name; | |
171 | u8 index; | |
172 | struct usb_phy *phy; | |
173 | struct list_head list; | |
174 | }; | |
de4217d9 VB |
175 | |
176 | /* for board-specific init logic */ | |
177 | extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type); | |
0fa4fab4 | 178 | extern int usb_add_phy_dev(struct usb_phy *); |
de4217d9 VB |
179 | extern void usb_remove_phy(struct usb_phy *); |
180 | ||
181 | /* helpers for direct access thru low-level io interface */ | |
182 | static inline int usb_phy_io_read(struct usb_phy *x, u32 reg) | |
183 | { | |
410aee70 | 184 | if (x && x->io_ops && x->io_ops->read) |
de4217d9 VB |
185 | return x->io_ops->read(x, reg); |
186 | ||
187 | return -EINVAL; | |
188 | } | |
189 | ||
190 | static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg) | |
191 | { | |
410aee70 | 192 | if (x && x->io_ops && x->io_ops->write) |
de4217d9 VB |
193 | return x->io_ops->write(x, val, reg); |
194 | ||
195 | return -EINVAL; | |
196 | } | |
197 | ||
198 | static inline int | |
199 | usb_phy_init(struct usb_phy *x) | |
200 | { | |
410aee70 | 201 | if (x && x->init) |
de4217d9 VB |
202 | return x->init(x); |
203 | ||
204 | return 0; | |
205 | } | |
206 | ||
207 | static inline void | |
208 | usb_phy_shutdown(struct usb_phy *x) | |
209 | { | |
410aee70 | 210 | if (x && x->shutdown) |
de4217d9 VB |
211 | x->shutdown(x); |
212 | } | |
213 | ||
b774212e FB |
214 | static inline int |
215 | usb_phy_vbus_on(struct usb_phy *x) | |
216 | { | |
410aee70 | 217 | if (!x || !x->set_vbus) |
b774212e FB |
218 | return 0; |
219 | ||
220 | return x->set_vbus(x, true); | |
221 | } | |
222 | ||
223 | static inline int | |
224 | usb_phy_vbus_off(struct usb_phy *x) | |
225 | { | |
410aee70 | 226 | if (!x || !x->set_vbus) |
b774212e FB |
227 | return 0; |
228 | ||
229 | return x->set_vbus(x, false); | |
230 | } | |
231 | ||
de4217d9 | 232 | /* for usb host and peripheral controller drivers */ |
edc7cb2e | 233 | #if IS_ENABLED(CONFIG_USB_PHY) |
de4217d9 VB |
234 | extern struct usb_phy *usb_get_phy(enum usb_phy_type type); |
235 | extern struct usb_phy *devm_usb_get_phy(struct device *dev, | |
236 | enum usb_phy_type type); | |
0fa4fab4 KVA |
237 | extern struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index); |
238 | extern struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index); | |
5d3c28b5 KVA |
239 | extern struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev, |
240 | const char *phandle, u8 index); | |
e842b84c N |
241 | extern struct usb_phy *devm_usb_get_phy_by_node(struct device *dev, |
242 | struct device_node *node, struct notifier_block *nb); | |
de4217d9 VB |
243 | extern void usb_put_phy(struct usb_phy *); |
244 | extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x); | |
b4a83e4d KVA |
245 | extern int usb_bind_phy(const char *dev_name, u8 index, |
246 | const char *phy_dev_name); | |
df9f7b31 | 247 | extern void usb_phy_set_event(struct usb_phy *x, unsigned long event); |
a9081a00 BW |
248 | extern void usb_phy_set_charger_current(struct usb_phy *usb_phy, |
249 | unsigned int mA); | |
250 | extern void usb_phy_get_charger_current(struct usb_phy *usb_phy, | |
251 | unsigned int *min, unsigned int *max); | |
252 | extern void usb_phy_set_charger_state(struct usb_phy *usb_phy, | |
253 | enum usb_charger_state state); | |
de4217d9 VB |
254 | #else |
255 | static inline struct usb_phy *usb_get_phy(enum usb_phy_type type) | |
256 | { | |
b7fa5c2a | 257 | return ERR_PTR(-ENXIO); |
de4217d9 VB |
258 | } |
259 | ||
260 | static inline struct usb_phy *devm_usb_get_phy(struct device *dev, | |
261 | enum usb_phy_type type) | |
262 | { | |
b7fa5c2a | 263 | return ERR_PTR(-ENXIO); |
de4217d9 VB |
264 | } |
265 | ||
0fa4fab4 KVA |
266 | static inline struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index) |
267 | { | |
b7fa5c2a | 268 | return ERR_PTR(-ENXIO); |
0fa4fab4 KVA |
269 | } |
270 | ||
271 | static inline struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index) | |
272 | { | |
b7fa5c2a | 273 | return ERR_PTR(-ENXIO); |
0fa4fab4 KVA |
274 | } |
275 | ||
5d3c28b5 KVA |
276 | static inline struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev, |
277 | const char *phandle, u8 index) | |
278 | { | |
b7fa5c2a | 279 | return ERR_PTR(-ENXIO); |
5d3c28b5 KVA |
280 | } |
281 | ||
307c858b AB |
282 | static inline struct usb_phy *devm_usb_get_phy_by_node(struct device *dev, |
283 | struct device_node *node, struct notifier_block *nb) | |
284 | { | |
285 | return ERR_PTR(-ENXIO); | |
286 | } | |
287 | ||
de4217d9 VB |
288 | static inline void usb_put_phy(struct usb_phy *x) |
289 | { | |
290 | } | |
291 | ||
292 | static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x) | |
293 | { | |
294 | } | |
295 | ||
b4a83e4d KVA |
296 | static inline int usb_bind_phy(const char *dev_name, u8 index, |
297 | const char *phy_dev_name) | |
298 | { | |
299 | return -EOPNOTSUPP; | |
300 | } | |
df9f7b31 KR |
301 | |
302 | static inline void usb_phy_set_event(struct usb_phy *x, unsigned long event) | |
303 | { | |
304 | } | |
a9081a00 BW |
305 | |
306 | static inline void usb_phy_set_charger_current(struct usb_phy *usb_phy, | |
307 | unsigned int mA) | |
308 | { | |
309 | } | |
310 | ||
311 | static inline void usb_phy_get_charger_current(struct usb_phy *usb_phy, | |
312 | unsigned int *min, | |
313 | unsigned int *max) | |
314 | { | |
315 | } | |
316 | ||
317 | static inline void usb_phy_set_charger_state(struct usb_phy *usb_phy, | |
318 | enum usb_charger_state state) | |
319 | { | |
320 | } | |
de4217d9 VB |
321 | #endif |
322 | ||
323 | static inline int | |
324 | usb_phy_set_power(struct usb_phy *x, unsigned mA) | |
325 | { | |
eb3c74de BW |
326 | if (!x) |
327 | return 0; | |
328 | ||
a9081a00 BW |
329 | usb_phy_set_charger_current(x, mA); |
330 | ||
eb3c74de | 331 | if (x->set_power) |
de4217d9 VB |
332 | return x->set_power(x, mA); |
333 | return 0; | |
334 | } | |
335 | ||
336 | /* Context: can sleep */ | |
337 | static inline int | |
338 | usb_phy_set_suspend(struct usb_phy *x, int suspend) | |
339 | { | |
410aee70 | 340 | if (x && x->set_suspend != NULL) |
de4217d9 VB |
341 | return x->set_suspend(x, suspend); |
342 | else | |
343 | return 0; | |
344 | } | |
345 | ||
57bf9b09 PC |
346 | static inline int |
347 | usb_phy_set_wakeup(struct usb_phy *x, bool enabled) | |
348 | { | |
349 | if (x && x->set_wakeup) | |
350 | return x->set_wakeup(x, enabled); | |
351 | else | |
352 | return 0; | |
353 | } | |
354 | ||
de4217d9 | 355 | static inline int |
ac96511b | 356 | usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed) |
de4217d9 | 357 | { |
410aee70 | 358 | if (x && x->notify_connect) |
ac96511b | 359 | return x->notify_connect(x, speed); |
de4217d9 VB |
360 | else |
361 | return 0; | |
362 | } | |
363 | ||
364 | static inline int | |
ac96511b | 365 | usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed) |
de4217d9 | 366 | { |
410aee70 | 367 | if (x && x->notify_disconnect) |
ac96511b | 368 | return x->notify_disconnect(x, speed); |
de4217d9 VB |
369 | else |
370 | return 0; | |
371 | } | |
372 | ||
373 | /* notifiers */ | |
374 | static inline int | |
375 | usb_register_notifier(struct usb_phy *x, struct notifier_block *nb) | |
376 | { | |
377 | return atomic_notifier_chain_register(&x->notifier, nb); | |
378 | } | |
379 | ||
380 | static inline void | |
381 | usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb) | |
382 | { | |
383 | atomic_notifier_chain_unregister(&x->notifier, nb); | |
384 | } | |
385 | ||
386 | static inline const char *usb_phy_type_string(enum usb_phy_type type) | |
387 | { | |
388 | switch (type) { | |
389 | case USB_PHY_TYPE_USB2: | |
390 | return "USB2 PHY"; | |
391 | case USB_PHY_TYPE_USB3: | |
392 | return "USB3 PHY"; | |
393 | default: | |
394 | return "UNKNOWN PHY TYPE"; | |
395 | } | |
396 | } | |
397 | #endif /* __LINUX_USB_PHY_H */ |