usb: otg: support for multiple transceivers by a single controller
[linux-2.6.git] / include / linux / usb / otg.h
blob1def65fb57d0b2d87d31b59069d85ca4e231fcb0
1 /* USB OTG (On The Go) defines */
2 /*
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 */
9 #ifndef __LINUX_USB_OTG_H
10 #define __LINUX_USB_OTG_H
12 #include <linux/notifier.h>
14 /* OTG defines lots of enumeration states before device reset */
15 enum usb_otg_state {
16 OTG_STATE_UNDEFINED = 0,
18 /* single-role peripheral, and dual-role default-b */
19 OTG_STATE_B_IDLE,
20 OTG_STATE_B_SRP_INIT,
21 OTG_STATE_B_PERIPHERAL,
23 /* extra dual-role default-b states */
24 OTG_STATE_B_WAIT_ACON,
25 OTG_STATE_B_HOST,
27 /* dual-role default-a */
28 OTG_STATE_A_IDLE,
29 OTG_STATE_A_WAIT_VRISE,
30 OTG_STATE_A_WAIT_BCON,
31 OTG_STATE_A_HOST,
32 OTG_STATE_A_SUSPEND,
33 OTG_STATE_A_PERIPHERAL,
34 OTG_STATE_A_WAIT_VFALL,
35 OTG_STATE_A_VBUS_ERR,
38 enum usb_phy_events {
39 USB_EVENT_NONE, /* no events or cable disconnected */
40 USB_EVENT_VBUS, /* vbus valid event */
41 USB_EVENT_ID, /* id was grounded */
42 USB_EVENT_CHARGER, /* usb dedicated charger */
43 USB_EVENT_ENUMERATED, /* gadget driver enumerated */
46 /* associate a type with PHY */
47 enum usb_phy_type {
48 USB_PHY_TYPE_UNDEFINED,
49 USB_PHY_TYPE_USB2,
50 USB_PHY_TYPE_USB3,
53 struct usb_phy;
55 /* for transceivers connected thru an ULPI interface, the user must
56 * provide access ops
58 struct usb_phy_io_ops {
59 int (*read)(struct usb_phy *x, u32 reg);
60 int (*write)(struct usb_phy *x, u32 val, u32 reg);
63 struct usb_otg {
64 u8 default_a;
66 struct usb_phy *phy;
67 struct usb_bus *host;
68 struct usb_gadget *gadget;
70 /* bind/unbind the host controller */
71 int (*set_host)(struct usb_otg *otg, struct usb_bus *host);
73 /* bind/unbind the peripheral controller */
74 int (*set_peripheral)(struct usb_otg *otg,
75 struct usb_gadget *gadget);
77 /* effective for A-peripheral, ignored for B devices */
78 int (*set_vbus)(struct usb_otg *otg, bool enabled);
80 /* for B devices only: start session with A-Host */
81 int (*start_srp)(struct usb_otg *otg);
83 /* start or continue HNP role switch */
84 int (*start_hnp)(struct usb_otg *otg);
89 * the otg driver needs to interact with both device side and host side
90 * usb controllers. it decides which controller is active at a given
91 * moment, using the transceiver, ID signal, HNP and sometimes static
92 * configuration information (including "board isn't wired for otg").
94 struct usb_phy {
95 struct device *dev;
96 const char *label;
97 unsigned int flags;
99 enum usb_phy_type type;
100 enum usb_otg_state state;
101 enum usb_phy_events last_event;
103 struct usb_otg *otg;
105 struct device *io_dev;
106 struct usb_phy_io_ops *io_ops;
107 void __iomem *io_priv;
109 /* for notification of usb_phy_events */
110 struct atomic_notifier_head notifier;
112 /* to pass extra port status to the root hub */
113 u16 port_status;
114 u16 port_change;
116 /* to support controllers that have multiple transceivers */
117 struct list_head head;
119 /* initialize/shutdown the OTG controller */
120 int (*init)(struct usb_phy *x);
121 void (*shutdown)(struct usb_phy *x);
123 /* effective for B devices, ignored for A-peripheral */
124 int (*set_power)(struct usb_phy *x,
125 unsigned mA);
127 /* for non-OTG B devices: set transceiver into suspend mode */
128 int (*set_suspend)(struct usb_phy *x,
129 int suspend);
134 /* for board-specific init logic */
135 extern int usb_add_phy(struct usb_phy *, enum usb_phy_type type);
136 extern void usb_remove_phy(struct usb_phy *);
138 #if defined(CONFIG_NOP_USB_XCEIV) || (defined(CONFIG_NOP_USB_XCEIV_MODULE) && defined(MODULE))
139 /* sometimes transceivers are accessed only through e.g. ULPI */
140 extern void usb_nop_xceiv_register(void);
141 extern void usb_nop_xceiv_unregister(void);
142 #else
143 static inline void usb_nop_xceiv_register(void)
147 static inline void usb_nop_xceiv_unregister(void)
150 #endif
152 /* helpers for direct access thru low-level io interface */
153 static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
155 if (x->io_ops && x->io_ops->read)
156 return x->io_ops->read(x, reg);
158 return -EINVAL;
161 static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
163 if (x->io_ops && x->io_ops->write)
164 return x->io_ops->write(x, val, reg);
166 return -EINVAL;
169 static inline int
170 usb_phy_init(struct usb_phy *x)
172 if (x->init)
173 return x->init(x);
175 return 0;
178 static inline void
179 usb_phy_shutdown(struct usb_phy *x)
181 if (x->shutdown)
182 x->shutdown(x);
185 /* for usb host and peripheral controller drivers */
186 #ifdef CONFIG_USB_OTG_UTILS
187 extern struct usb_phy *usb_get_phy(enum usb_phy_type type);
188 extern void usb_put_phy(struct usb_phy *);
189 extern const char *otg_state_string(enum usb_otg_state state);
190 #else
191 static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
193 return NULL;
196 static inline void usb_put_phy(struct usb_phy *x)
200 static inline const char *otg_state_string(enum usb_otg_state state)
202 return NULL;
204 #endif
206 /* Context: can sleep */
207 static inline int
208 otg_start_hnp(struct usb_otg *otg)
210 if (otg && otg->start_hnp)
211 return otg->start_hnp(otg);
213 return -ENOTSUPP;
216 /* Context: can sleep */
217 static inline int
218 otg_set_vbus(struct usb_otg *otg, bool enabled)
220 if (otg && otg->set_vbus)
221 return otg->set_vbus(otg, enabled);
223 return -ENOTSUPP;
226 /* for HCDs */
227 static inline int
228 otg_set_host(struct usb_otg *otg, struct usb_bus *host)
230 if (otg && otg->set_host)
231 return otg->set_host(otg, host);
233 return -ENOTSUPP;
236 /* for usb peripheral controller drivers */
238 /* Context: can sleep */
239 static inline int
240 otg_set_peripheral(struct usb_otg *otg, struct usb_gadget *periph)
242 if (otg && otg->set_peripheral)
243 return otg->set_peripheral(otg, periph);
245 return -ENOTSUPP;
248 static inline int
249 usb_phy_set_power(struct usb_phy *x, unsigned mA)
251 if (x && x->set_power)
252 return x->set_power(x, mA);
253 return 0;
256 /* Context: can sleep */
257 static inline int
258 usb_phy_set_suspend(struct usb_phy *x, int suspend)
260 if (x->set_suspend != NULL)
261 return x->set_suspend(x, suspend);
262 else
263 return 0;
266 static inline int
267 otg_start_srp(struct usb_otg *otg)
269 if (otg && otg->start_srp)
270 return otg->start_srp(otg);
272 return -ENOTSUPP;
275 /* notifiers */
276 static inline int
277 usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
279 return atomic_notifier_chain_register(&x->notifier, nb);
282 static inline void
283 usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
285 atomic_notifier_chain_unregister(&x->notifier, nb);
288 /* for OTG controller drivers (and maybe other stuff) */
289 extern int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num);
291 static inline const char *usb_phy_type_string(enum usb_phy_type type)
293 switch (type) {
294 case USB_PHY_TYPE_USB2:
295 return "USB2 PHY";
296 case USB_PHY_TYPE_USB3:
297 return "USB3 PHY";
298 default:
299 return "UNKNOWN PHY TYPE";
302 #endif /* __LINUX_USB_OTG_H */