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