blob: de881b171ba9b59dccf49bdfaa6497955b194aed [file] [log] [blame]
Venu Byravarasude4217d2012-09-04 14:25:58 +05301/*
Peter Chen07673202014-12-30 14:02:28 +08002 * USB PHY defines
Venu Byravarasude4217d2012-09-04 14:25:58 +05303 *
4 * These APIs may be used between USB controllers. USB device drivers
5 * (for either host or peripheral roles) don't use these calls; they
6 * continue to use just usb_device and usb_gadget.
7 */
8
9#ifndef __LINUX_USB_PHY_H
10#define __LINUX_USB_PHY_H
11
Baolin Wang7d21114d2017-05-05 14:12:24 +080012#include <linux/extcon.h>
Venu Byravarasude4217d2012-09-04 14:25:58 +053013#include <linux/notifier.h>
Peter Chenac965112012-11-09 09:44:44 +080014#include <linux/usb.h>
Baolin Wanga9081a02017-08-15 19:07:54 +080015#include <uapi/linux/usb/charger.h>
Venu Byravarasude4217d2012-09-04 14:25:58 +053016
Michael Grzeschik1c9af652013-06-13 17:59:55 +030017enum usb_phy_interface {
18 USBPHY_INTERFACE_MODE_UNKNOWN,
19 USBPHY_INTERFACE_MODE_UTMI,
20 USBPHY_INTERFACE_MODE_UTMIW,
21 USBPHY_INTERFACE_MODE_ULPI,
22 USBPHY_INTERFACE_MODE_SERIAL,
23 USBPHY_INTERFACE_MODE_HSIC,
24};
25
Venu Byravarasude4217d2012-09-04 14:25:58 +053026enum usb_phy_events {
27 USB_EVENT_NONE, /* no events or cable disconnected */
28 USB_EVENT_VBUS, /* vbus valid event */
29 USB_EVENT_ID, /* id was grounded */
30 USB_EVENT_CHARGER, /* usb dedicated charger */
31 USB_EVENT_ENUMERATED, /* gadget driver enumerated */
32};
33
34/* associate a type with PHY */
35enum usb_phy_type {
36 USB_PHY_TYPE_UNDEFINED,
37 USB_PHY_TYPE_USB2,
38 USB_PHY_TYPE_USB3,
39};
40
Venu Byravarasua4c3dde2012-09-06 10:42:15 +053041/* OTG defines lots of enumeration states before device reset */
42enum usb_otg_state {
43 OTG_STATE_UNDEFINED = 0,
44
45 /* single-role peripheral, and dual-role default-b */
46 OTG_STATE_B_IDLE,
47 OTG_STATE_B_SRP_INIT,
48 OTG_STATE_B_PERIPHERAL,
49
50 /* extra dual-role default-b states */
51 OTG_STATE_B_WAIT_ACON,
52 OTG_STATE_B_HOST,
53
54 /* dual-role default-a */
55 OTG_STATE_A_IDLE,
56 OTG_STATE_A_WAIT_VRISE,
57 OTG_STATE_A_WAIT_BCON,
58 OTG_STATE_A_HOST,
59 OTG_STATE_A_SUSPEND,
60 OTG_STATE_A_PERIPHERAL,
61 OTG_STATE_A_WAIT_VFALL,
62 OTG_STATE_A_VBUS_ERR,
63};
64
Venu Byravarasude4217d2012-09-04 14:25:58 +053065struct usb_phy;
66struct usb_otg;
67
Peter Chen58efd4b02015-09-22 15:31:34 +080068/* for phys connected thru an ULPI interface, the user must
Venu Byravarasude4217d2012-09-04 14:25:58 +053069 * provide access ops
70 */
71struct usb_phy_io_ops {
72 int (*read)(struct usb_phy *x, u32 reg);
73 int (*write)(struct usb_phy *x, u32 val, u32 reg);
74};
75
Baolin Wanga9081a02017-08-15 19:07:54 +080076struct usb_charger_current {
77 unsigned int sdp_min;
78 unsigned int sdp_max;
79 unsigned int dcp_min;
80 unsigned int dcp_max;
81 unsigned int cdp_min;
82 unsigned int cdp_max;
83 unsigned int aca_min;
84 unsigned int aca_max;
85};
86
Venu Byravarasude4217d2012-09-04 14:25:58 +053087struct usb_phy {
88 struct device *dev;
89 const char *label;
90 unsigned int flags;
91
92 enum usb_phy_type type;
Venu Byravarasude4217d2012-09-04 14:25:58 +053093 enum usb_phy_events last_event;
94
95 struct usb_otg *otg;
96
97 struct device *io_dev;
98 struct usb_phy_io_ops *io_ops;
99 void __iomem *io_priv;
100
Baolin Wang7d21114d2017-05-05 14:12:24 +0800101 /* to support extcon device */
102 struct extcon_dev *edev;
103 struct extcon_dev *id_edev;
104 struct notifier_block vbus_nb;
105 struct notifier_block id_nb;
Baolin Wanga9081a02017-08-15 19:07:54 +0800106 struct notifier_block type_nb;
107
108 /* Support USB charger */
109 enum usb_charger_type chg_type;
110 enum usb_charger_state chg_state;
111 struct usb_charger_current chg_cur;
112 struct work_struct chg_work;
Baolin Wang7d21114d2017-05-05 14:12:24 +0800113
Venu Byravarasude4217d2012-09-04 14:25:58 +0530114 /* for notification of usb_phy_events */
115 struct atomic_notifier_head notifier;
116
117 /* to pass extra port status to the root hub */
118 u16 port_status;
119 u16 port_change;
120
Peter Chen58efd4b02015-09-22 15:31:34 +0800121 /* to support controllers that have multiple phys */
Venu Byravarasude4217d2012-09-04 14:25:58 +0530122 struct list_head head;
123
Peter Chen58efd4b02015-09-22 15:31:34 +0800124 /* initialize/shutdown the phy */
Venu Byravarasude4217d2012-09-04 14:25:58 +0530125 int (*init)(struct usb_phy *x);
126 void (*shutdown)(struct usb_phy *x);
127
Felipe Balbib7742122013-03-08 13:22:58 +0200128 /* enable/disable VBUS */
129 int (*set_vbus)(struct usb_phy *x, int on);
130
Venu Byravarasude4217d2012-09-04 14:25:58 +0530131 /* effective for B devices, ignored for A-peripheral */
132 int (*set_power)(struct usb_phy *x,
133 unsigned mA);
134
Peter Chen58efd4b02015-09-22 15:31:34 +0800135 /* Set phy into suspend mode */
Venu Byravarasude4217d2012-09-04 14:25:58 +0530136 int (*set_suspend)(struct usb_phy *x,
137 int suspend);
138
Peter Chen57bf9b02014-02-24 10:21:01 +0800139 /*
140 * Set wakeup enable for PHY, in that case, the PHY can be
141 * woken up from suspend status due to external events,
142 * like vbus change, dp/dm change and id.
143 */
144 int (*set_wakeup)(struct usb_phy *x, bool enabled);
145
Venu Byravarasude4217d2012-09-04 14:25:58 +0530146 /* notify phy connect status change */
Peter Chenac965112012-11-09 09:44:44 +0800147 int (*notify_connect)(struct usb_phy *x,
148 enum usb_device_speed speed);
149 int (*notify_disconnect)(struct usb_phy *x,
150 enum usb_device_speed speed);
Baolin Wanga9081a02017-08-15 19:07:54 +0800151
152 /*
153 * Charger detection method can be implemented if you need to
154 * manually detect the charger type.
155 */
156 enum usb_charger_type (*charger_detect)(struct usb_phy *x);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530157};
158
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530159/**
160 * struct usb_phy_bind - represent the binding for the phy
161 * @dev_name: the device name of the device that will bind to the phy
162 * @phy_dev_name: the device name of the phy
163 * @index: used if a single controller uses multiple phys
164 * @phy: reference to the phy
165 * @list: to maintain a linked list of the binding information
166 */
167struct usb_phy_bind {
168 const char *dev_name;
169 const char *phy_dev_name;
170 u8 index;
171 struct usb_phy *phy;
172 struct list_head list;
173};
Venu Byravarasude4217d2012-09-04 14:25:58 +0530174
175/* for board-specific init logic */
176extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530177extern int usb_add_phy_dev(struct usb_phy *);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530178extern void usb_remove_phy(struct usb_phy *);
179
180/* helpers for direct access thru low-level io interface */
181static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
182{
Felipe Balbi410aee72013-06-30 15:27:26 +0300183 if (x && x->io_ops && x->io_ops->read)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530184 return x->io_ops->read(x, reg);
185
186 return -EINVAL;
187}
188
189static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
190{
Felipe Balbi410aee72013-06-30 15:27:26 +0300191 if (x && x->io_ops && x->io_ops->write)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530192 return x->io_ops->write(x, val, reg);
193
194 return -EINVAL;
195}
196
197static inline int
198usb_phy_init(struct usb_phy *x)
199{
Felipe Balbi410aee72013-06-30 15:27:26 +0300200 if (x && x->init)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530201 return x->init(x);
202
203 return 0;
204}
205
206static inline void
207usb_phy_shutdown(struct usb_phy *x)
208{
Felipe Balbi410aee72013-06-30 15:27:26 +0300209 if (x && x->shutdown)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530210 x->shutdown(x);
211}
212
Felipe Balbib7742122013-03-08 13:22:58 +0200213static inline int
214usb_phy_vbus_on(struct usb_phy *x)
215{
Felipe Balbi410aee72013-06-30 15:27:26 +0300216 if (!x || !x->set_vbus)
Felipe Balbib7742122013-03-08 13:22:58 +0200217 return 0;
218
219 return x->set_vbus(x, true);
220}
221
222static inline int
223usb_phy_vbus_off(struct usb_phy *x)
224{
Felipe Balbi410aee72013-06-30 15:27:26 +0300225 if (!x || !x->set_vbus)
Felipe Balbib7742122013-03-08 13:22:58 +0200226 return 0;
227
228 return x->set_vbus(x, false);
229}
230
Venu Byravarasude4217d2012-09-04 14:25:58 +0530231/* for usb host and peripheral controller drivers */
Felipe Balbiedc7cb22013-03-07 11:13:43 +0200232#if IS_ENABLED(CONFIG_USB_PHY)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530233extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
234extern struct usb_phy *devm_usb_get_phy(struct device *dev,
235 enum usb_phy_type type);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530236extern struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index);
237extern struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index);
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530238extern struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
239 const char *phandle, u8 index);
NeilBrowne842b842015-03-23 09:52:48 +1100240extern struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
241 struct device_node *node, struct notifier_block *nb);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530242extern void usb_put_phy(struct usb_phy *);
243extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530244extern int usb_bind_phy(const char *dev_name, u8 index,
245 const char *phy_dev_name);
Kiran Raparthydf9f7b32014-11-21 11:31:20 +0530246extern void usb_phy_set_event(struct usb_phy *x, unsigned long event);
Baolin Wanga9081a02017-08-15 19:07:54 +0800247extern void usb_phy_set_charger_current(struct usb_phy *usb_phy,
248 unsigned int mA);
249extern void usb_phy_get_charger_current(struct usb_phy *usb_phy,
250 unsigned int *min, unsigned int *max);
251extern void usb_phy_set_charger_state(struct usb_phy *usb_phy,
252 enum usb_charger_state state);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530253#else
254static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
255{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200256 return ERR_PTR(-ENXIO);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530257}
258
259static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
260 enum usb_phy_type type)
261{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200262 return ERR_PTR(-ENXIO);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530263}
264
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530265static inline struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
266{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200267 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530268}
269
270static inline struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
271{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200272 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530273}
274
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530275static inline struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
276 const char *phandle, u8 index)
277{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200278 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530279}
280
Arnd Bergmann307c8582015-05-28 16:08:02 +0200281static inline struct usb_phy *devm_usb_get_phy_by_node(struct device *dev,
282 struct device_node *node, struct notifier_block *nb)
283{
284 return ERR_PTR(-ENXIO);
285}
286
Venu Byravarasude4217d2012-09-04 14:25:58 +0530287static inline void usb_put_phy(struct usb_phy *x)
288{
289}
290
291static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
292{
293}
294
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530295static inline int usb_bind_phy(const char *dev_name, u8 index,
296 const char *phy_dev_name)
297{
298 return -EOPNOTSUPP;
299}
Kiran Raparthydf9f7b32014-11-21 11:31:20 +0530300
301static inline void usb_phy_set_event(struct usb_phy *x, unsigned long event)
302{
303}
Baolin Wanga9081a02017-08-15 19:07:54 +0800304
305static inline void usb_phy_set_charger_current(struct usb_phy *usb_phy,
306 unsigned int mA)
307{
308}
309
310static inline void usb_phy_get_charger_current(struct usb_phy *usb_phy,
311 unsigned int *min,
312 unsigned int *max)
313{
314}
315
316static inline void usb_phy_set_charger_state(struct usb_phy *usb_phy,
317 enum usb_charger_state state)
318{
319}
Venu Byravarasude4217d2012-09-04 14:25:58 +0530320#endif
321
322static inline int
323usb_phy_set_power(struct usb_phy *x, unsigned mA)
324{
Baolin Wanga9081a02017-08-15 19:07:54 +0800325 usb_phy_set_charger_current(x, mA);
326
Venu Byravarasude4217d2012-09-04 14:25:58 +0530327 if (x && x->set_power)
328 return x->set_power(x, mA);
329 return 0;
330}
331
332/* Context: can sleep */
333static inline int
334usb_phy_set_suspend(struct usb_phy *x, int suspend)
335{
Felipe Balbi410aee72013-06-30 15:27:26 +0300336 if (x && x->set_suspend != NULL)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530337 return x->set_suspend(x, suspend);
338 else
339 return 0;
340}
341
342static inline int
Peter Chen57bf9b02014-02-24 10:21:01 +0800343usb_phy_set_wakeup(struct usb_phy *x, bool enabled)
344{
345 if (x && x->set_wakeup)
346 return x->set_wakeup(x, enabled);
347 else
348 return 0;
349}
350
351static inline int
Peter Chenac965112012-11-09 09:44:44 +0800352usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530353{
Felipe Balbi410aee72013-06-30 15:27:26 +0300354 if (x && x->notify_connect)
Peter Chenac965112012-11-09 09:44:44 +0800355 return x->notify_connect(x, speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530356 else
357 return 0;
358}
359
360static inline int
Peter Chenac965112012-11-09 09:44:44 +0800361usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530362{
Felipe Balbi410aee72013-06-30 15:27:26 +0300363 if (x && x->notify_disconnect)
Peter Chenac965112012-11-09 09:44:44 +0800364 return x->notify_disconnect(x, speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530365 else
366 return 0;
367}
368
369/* notifiers */
370static inline int
371usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
372{
373 return atomic_notifier_chain_register(&x->notifier, nb);
374}
375
376static inline void
377usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
378{
379 atomic_notifier_chain_unregister(&x->notifier, nb);
380}
381
382static inline const char *usb_phy_type_string(enum usb_phy_type type)
383{
384 switch (type) {
385 case USB_PHY_TYPE_USB2:
386 return "USB2 PHY";
387 case USB_PHY_TYPE_USB3:
388 return "USB3 PHY";
389 default:
390 return "UNKNOWN PHY TYPE";
391 }
392}
393#endif /* __LINUX_USB_PHY_H */