blob: 6c0b1c513db72ce9632396223ed99aca20ff3efb [file] [log] [blame]
Venu Byravarasude4217d2012-09-04 14:25:58 +05301/* USB OTG (On The Go) defines */
2/*
3 *
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
12#include <linux/notifier.h>
Peter Chenac965112012-11-09 09:44:44 +080013#include <linux/usb.h>
Venu Byravarasude4217d2012-09-04 14:25:58 +053014
Michael Grzeschik1c9af652013-06-13 17:59:55 +030015enum usb_phy_interface {
16 USBPHY_INTERFACE_MODE_UNKNOWN,
17 USBPHY_INTERFACE_MODE_UTMI,
18 USBPHY_INTERFACE_MODE_UTMIW,
19 USBPHY_INTERFACE_MODE_ULPI,
20 USBPHY_INTERFACE_MODE_SERIAL,
21 USBPHY_INTERFACE_MODE_HSIC,
22};
23
Venu Byravarasude4217d2012-09-04 14:25:58 +053024enum usb_phy_events {
25 USB_EVENT_NONE, /* no events or cable disconnected */
26 USB_EVENT_VBUS, /* vbus valid event */
27 USB_EVENT_ID, /* id was grounded */
28 USB_EVENT_CHARGER, /* usb dedicated charger */
29 USB_EVENT_ENUMERATED, /* gadget driver enumerated */
30};
31
32/* associate a type with PHY */
33enum usb_phy_type {
34 USB_PHY_TYPE_UNDEFINED,
35 USB_PHY_TYPE_USB2,
36 USB_PHY_TYPE_USB3,
37};
38
Venu Byravarasua4c3dde2012-09-06 10:42:15 +053039/* OTG defines lots of enumeration states before device reset */
40enum usb_otg_state {
41 OTG_STATE_UNDEFINED = 0,
42
43 /* single-role peripheral, and dual-role default-b */
44 OTG_STATE_B_IDLE,
45 OTG_STATE_B_SRP_INIT,
46 OTG_STATE_B_PERIPHERAL,
47
48 /* extra dual-role default-b states */
49 OTG_STATE_B_WAIT_ACON,
50 OTG_STATE_B_HOST,
51
52 /* dual-role default-a */
53 OTG_STATE_A_IDLE,
54 OTG_STATE_A_WAIT_VRISE,
55 OTG_STATE_A_WAIT_BCON,
56 OTG_STATE_A_HOST,
57 OTG_STATE_A_SUSPEND,
58 OTG_STATE_A_PERIPHERAL,
59 OTG_STATE_A_WAIT_VFALL,
60 OTG_STATE_A_VBUS_ERR,
61};
62
Venu Byravarasude4217d2012-09-04 14:25:58 +053063struct usb_phy;
64struct usb_otg;
65
66/* for transceivers connected thru an ULPI interface, the user must
67 * provide access ops
68 */
69struct usb_phy_io_ops {
70 int (*read)(struct usb_phy *x, u32 reg);
71 int (*write)(struct usb_phy *x, u32 val, u32 reg);
72};
73
74struct usb_phy {
75 struct device *dev;
76 const char *label;
77 unsigned int flags;
78
79 enum usb_phy_type type;
Venu Byravarasua4c3dde2012-09-06 10:42:15 +053080 enum usb_otg_state state;
Venu Byravarasude4217d2012-09-04 14:25:58 +053081 enum usb_phy_events last_event;
82
83 struct usb_otg *otg;
84
85 struct device *io_dev;
86 struct usb_phy_io_ops *io_ops;
87 void __iomem *io_priv;
88
89 /* for notification of usb_phy_events */
90 struct atomic_notifier_head notifier;
91
92 /* to pass extra port status to the root hub */
93 u16 port_status;
94 u16 port_change;
95
96 /* to support controllers that have multiple transceivers */
97 struct list_head head;
98
99 /* initialize/shutdown the OTG controller */
100 int (*init)(struct usb_phy *x);
101 void (*shutdown)(struct usb_phy *x);
102
Felipe Balbib7742122013-03-08 13:22:58 +0200103 /* enable/disable VBUS */
104 int (*set_vbus)(struct usb_phy *x, int on);
105
Venu Byravarasude4217d2012-09-04 14:25:58 +0530106 /* effective for B devices, ignored for A-peripheral */
107 int (*set_power)(struct usb_phy *x,
108 unsigned mA);
109
110 /* for non-OTG B devices: set transceiver into suspend mode */
111 int (*set_suspend)(struct usb_phy *x,
112 int suspend);
113
114 /* notify phy connect status change */
Peter Chenac965112012-11-09 09:44:44 +0800115 int (*notify_connect)(struct usb_phy *x,
116 enum usb_device_speed speed);
117 int (*notify_disconnect)(struct usb_phy *x,
118 enum usb_device_speed speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530119};
120
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530121/**
122 * struct usb_phy_bind - represent the binding for the phy
123 * @dev_name: the device name of the device that will bind to the phy
124 * @phy_dev_name: the device name of the phy
125 * @index: used if a single controller uses multiple phys
126 * @phy: reference to the phy
127 * @list: to maintain a linked list of the binding information
128 */
129struct usb_phy_bind {
130 const char *dev_name;
131 const char *phy_dev_name;
132 u8 index;
133 struct usb_phy *phy;
134 struct list_head list;
135};
Venu Byravarasude4217d2012-09-04 14:25:58 +0530136
137/* for board-specific init logic */
138extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530139extern int usb_add_phy_dev(struct usb_phy *);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530140extern void usb_remove_phy(struct usb_phy *);
141
142/* helpers for direct access thru low-level io interface */
143static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
144{
Felipe Balbi410aee72013-06-30 15:27:26 +0300145 if (x && x->io_ops && x->io_ops->read)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530146 return x->io_ops->read(x, reg);
147
148 return -EINVAL;
149}
150
151static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
152{
Felipe Balbi410aee72013-06-30 15:27:26 +0300153 if (x && x->io_ops && x->io_ops->write)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530154 return x->io_ops->write(x, val, reg);
155
156 return -EINVAL;
157}
158
159static inline int
160usb_phy_init(struct usb_phy *x)
161{
Felipe Balbi410aee72013-06-30 15:27:26 +0300162 if (x && x->init)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530163 return x->init(x);
164
165 return 0;
166}
167
168static inline void
169usb_phy_shutdown(struct usb_phy *x)
170{
Felipe Balbi410aee72013-06-30 15:27:26 +0300171 if (x && x->shutdown)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530172 x->shutdown(x);
173}
174
Felipe Balbib7742122013-03-08 13:22:58 +0200175static inline int
176usb_phy_vbus_on(struct usb_phy *x)
177{
Felipe Balbi410aee72013-06-30 15:27:26 +0300178 if (!x || !x->set_vbus)
Felipe Balbib7742122013-03-08 13:22:58 +0200179 return 0;
180
181 return x->set_vbus(x, true);
182}
183
184static inline int
185usb_phy_vbus_off(struct usb_phy *x)
186{
Felipe Balbi410aee72013-06-30 15:27:26 +0300187 if (!x || !x->set_vbus)
Felipe Balbib7742122013-03-08 13:22:58 +0200188 return 0;
189
190 return x->set_vbus(x, false);
191}
192
Venu Byravarasude4217d2012-09-04 14:25:58 +0530193/* for usb host and peripheral controller drivers */
Felipe Balbiedc7cb22013-03-07 11:13:43 +0200194#if IS_ENABLED(CONFIG_USB_PHY)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530195extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
196extern struct usb_phy *devm_usb_get_phy(struct device *dev,
197 enum usb_phy_type type);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530198extern struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index);
199extern struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index);
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530200extern struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
201 const char *phandle, u8 index);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530202extern void usb_put_phy(struct usb_phy *);
203extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530204extern int usb_bind_phy(const char *dev_name, u8 index,
205 const char *phy_dev_name);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530206#else
207static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
208{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200209 return ERR_PTR(-ENXIO);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530210}
211
212static inline struct usb_phy *devm_usb_get_phy(struct device *dev,
213 enum usb_phy_type type)
214{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200215 return ERR_PTR(-ENXIO);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530216}
217
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530218static inline struct usb_phy *usb_get_phy_dev(struct device *dev, u8 index)
219{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200220 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530221}
222
223static inline struct usb_phy *devm_usb_get_phy_dev(struct device *dev, u8 index)
224{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200225 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I0fa4fab2013-01-25 08:03:22 +0530226}
227
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530228static inline struct usb_phy *devm_usb_get_phy_by_phandle(struct device *dev,
229 const char *phandle, u8 index)
230{
Felipe Balbib7fa5c22013-03-14 17:59:06 +0200231 return ERR_PTR(-ENXIO);
Kishon Vijay Abraham I5d3c28b2013-01-25 08:03:25 +0530232}
233
Venu Byravarasude4217d2012-09-04 14:25:58 +0530234static inline void usb_put_phy(struct usb_phy *x)
235{
236}
237
238static inline void devm_usb_put_phy(struct device *dev, struct usb_phy *x)
239{
240}
241
Kishon Vijay Abraham Ib4a83e42013-01-25 08:03:21 +0530242static inline int usb_bind_phy(const char *dev_name, u8 index,
243 const char *phy_dev_name)
244{
245 return -EOPNOTSUPP;
246}
Venu Byravarasude4217d2012-09-04 14:25:58 +0530247#endif
248
249static inline int
250usb_phy_set_power(struct usb_phy *x, unsigned mA)
251{
252 if (x && x->set_power)
253 return x->set_power(x, mA);
254 return 0;
255}
256
257/* Context: can sleep */
258static inline int
259usb_phy_set_suspend(struct usb_phy *x, int suspend)
260{
Felipe Balbi410aee72013-06-30 15:27:26 +0300261 if (x && x->set_suspend != NULL)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530262 return x->set_suspend(x, suspend);
263 else
264 return 0;
265}
266
267static inline int
Peter Chenac965112012-11-09 09:44:44 +0800268usb_phy_notify_connect(struct usb_phy *x, enum usb_device_speed speed)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530269{
Felipe Balbi410aee72013-06-30 15:27:26 +0300270 if (x && x->notify_connect)
Peter Chenac965112012-11-09 09:44:44 +0800271 return x->notify_connect(x, speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530272 else
273 return 0;
274}
275
276static inline int
Peter Chenac965112012-11-09 09:44:44 +0800277usb_phy_notify_disconnect(struct usb_phy *x, enum usb_device_speed speed)
Venu Byravarasude4217d2012-09-04 14:25:58 +0530278{
Felipe Balbi410aee72013-06-30 15:27:26 +0300279 if (x && x->notify_disconnect)
Peter Chenac965112012-11-09 09:44:44 +0800280 return x->notify_disconnect(x, speed);
Venu Byravarasude4217d2012-09-04 14:25:58 +0530281 else
282 return 0;
283}
284
285/* notifiers */
286static inline int
287usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
288{
289 return atomic_notifier_chain_register(&x->notifier, nb);
290}
291
292static inline void
293usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
294{
295 atomic_notifier_chain_unregister(&x->notifier, nb);
296}
297
298static inline const char *usb_phy_type_string(enum usb_phy_type type)
299{
300 switch (type) {
301 case USB_PHY_TYPE_USB2:
302 return "USB2 PHY";
303 case USB_PHY_TYPE_USB3:
304 return "USB3 PHY";
305 default:
306 return "UNKNOWN PHY TYPE";
307 }
308}
309#endif /* __LINUX_USB_PHY_H */