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