blob: 723a000146f8e7950971ed6e7acc6f5db7f6c674 [file] [log] [blame]
Greg Kroah-Hartman41dceed2008-01-30 15:21:33 -08001/* USB OTG (On The Go) defines */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Robert P. J. Daydda43a02008-03-07 13:45:32 -05003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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
Robert P. J. Daydda43a02008-03-07 13:45:32 -05009#ifndef __LINUX_USB_OTG_H
10#define __LINUX_USB_OTG_H
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Felipe Balbie9a20172009-12-17 13:01:36 +020012#include <linux/notifier.h>
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/* OTG defines lots of enumeration states before device reset */
15enum usb_otg_state {
16 OTG_STATE_UNDEFINED = 0,
17
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,
22
23 /* extra dual-role default-b states */
24 OTG_STATE_B_WAIT_ACON,
25 OTG_STATE_B_HOST,
26
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,
36};
37
Heikki Krogerusde07e182012-02-13 13:24:03 +020038enum usb_phy_events {
Felipe Balbie9a20172009-12-17 13:01:36 +020039 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 */
44};
45
Heikki Krogerus86753812012-02-13 13:24:02 +020046struct usb_phy;
Daniel Mack91c8a5a2009-10-15 17:09:34 +030047
48/* for transceivers connected thru an ULPI interface, the user must
49 * provide access ops
50 */
51struct otg_io_access_ops {
Heikki Krogerus86753812012-02-13 13:24:02 +020052 int (*read)(struct usb_phy *x, u32 reg);
53 int (*write)(struct usb_phy *x, u32 val, u32 reg);
Daniel Mack91c8a5a2009-10-15 17:09:34 +030054};
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/*
57 * the otg driver needs to interact with both device side and host side
58 * usb controllers. it decides which controller is active at a given
59 * moment, using the transceiver, ID signal, HNP and sometimes static
60 * configuration information (including "board isn't wired for otg").
61 */
Heikki Krogerus86753812012-02-13 13:24:02 +020062struct usb_phy {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 struct device *dev;
64 const char *label;
Daniel Mack91c8a5a2009-10-15 17:09:34 +030065 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 u8 default_a;
68 enum usb_otg_state state;
Heikki Krogerusde07e182012-02-13 13:24:03 +020069 enum usb_phy_events last_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 struct usb_bus *host;
72 struct usb_gadget *gadget;
73
Daniel Mack91c8a5a2009-10-15 17:09:34 +030074 struct otg_io_access_ops *io_ops;
75 void __iomem *io_priv;
76
Heikki Krogerusde07e182012-02-13 13:24:03 +020077 /* for notification of usb_phy_events */
Felipe Balbicccad6d2010-09-29 10:55:49 +030078 struct atomic_notifier_head notifier;
Felipe Balbie9a20172009-12-17 13:01:36 +020079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 /* to pass extra port status to the root hub */
81 u16 port_status;
82 u16 port_change;
83
Daniel Mack91c8a5a2009-10-15 17:09:34 +030084 /* initialize/shutdown the OTG controller */
Heikki Krogerus86753812012-02-13 13:24:02 +020085 int (*init)(struct usb_phy *x);
86 void (*shutdown)(struct usb_phy *x);
Daniel Mack91c8a5a2009-10-15 17:09:34 +030087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 /* bind/unbind the host controller */
Heikki Krogerus86753812012-02-13 13:24:02 +020089 int (*set_host)(struct usb_phy *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 struct usb_bus *host);
91
92 /* bind/unbind the peripheral controller */
Heikki Krogerus86753812012-02-13 13:24:02 +020093 int (*set_peripheral)(struct usb_phy *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 struct usb_gadget *gadget);
95
96 /* effective for B devices, ignored for A-peripheral */
Heikki Krogerus86753812012-02-13 13:24:02 +020097 int (*set_power)(struct usb_phy *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 unsigned mA);
99
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300100 /* effective for A-peripheral, ignored for B devices */
Heikki Krogerus86753812012-02-13 13:24:02 +0200101 int (*set_vbus)(struct usb_phy *x,
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300102 bool enabled);
103
Juha Yrj?l?4e671852005-10-16 15:47:04 -0700104 /* for non-OTG B devices: set transceiver into suspend mode */
Heikki Krogerus86753812012-02-13 13:24:02 +0200105 int (*set_suspend)(struct usb_phy *x,
Juha Yrj?l?4e671852005-10-16 15:47:04 -0700106 int suspend);
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 /* for B devices only: start session with A-Host */
Heikki Krogerus86753812012-02-13 13:24:02 +0200109 int (*start_srp)(struct usb_phy *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 /* start or continue HNP role switch */
Heikki Krogerus86753812012-02-13 13:24:02 +0200112 int (*start_hnp)(struct usb_phy *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114};
115
116
117/* for board-specific init logic */
Heikki Krogerus86753812012-02-13 13:24:02 +0200118extern int otg_set_transceiver(struct usb_phy *);
David Brownellcc835e32009-03-31 12:28:31 -0700119
Guennadi Liakhovetski352a3372010-12-09 22:46:29 +0100120#if defined(CONFIG_NOP_USB_XCEIV) || (defined(CONFIG_NOP_USB_XCEIV_MODULE) && defined(MODULE))
David Brownellcc835e32009-03-31 12:28:31 -0700121/* sometimes transceivers are accessed only through e.g. ULPI */
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530122extern void usb_nop_xceiv_register(void);
123extern void usb_nop_xceiv_unregister(void);
Maulik Mankad224e1542010-02-17 14:09:29 -0800124#else
125static inline void usb_nop_xceiv_register(void)
126{
127}
128
129static inline void usb_nop_xceiv_unregister(void)
130{
131}
132#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300134/* helpers for direct access thru low-level io interface */
Heikki Krogerus86753812012-02-13 13:24:02 +0200135static inline int otg_io_read(struct usb_phy *x, u32 reg)
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300136{
Heikki Krogerus86753812012-02-13 13:24:02 +0200137 if (x->io_ops && x->io_ops->read)
138 return x->io_ops->read(x, reg);
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300139
140 return -EINVAL;
141}
142
Heikki Krogerus86753812012-02-13 13:24:02 +0200143static inline int otg_io_write(struct usb_phy *x, u32 val, u32 reg)
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300144{
Heikki Krogerus86753812012-02-13 13:24:02 +0200145 if (x->io_ops && x->io_ops->write)
146 return x->io_ops->write(x, val, reg);
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300147
148 return -EINVAL;
149}
150
151static inline int
Heikki Krogerus86753812012-02-13 13:24:02 +0200152otg_init(struct usb_phy *x)
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300153{
Heikki Krogerus86753812012-02-13 13:24:02 +0200154 if (x->init)
155 return x->init(x);
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300156
157 return 0;
158}
159
160static inline void
Heikki Krogerus86753812012-02-13 13:24:02 +0200161otg_shutdown(struct usb_phy *x)
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300162{
Heikki Krogerus86753812012-02-13 13:24:02 +0200163 if (x->shutdown)
164 x->shutdown(x);
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300165}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167/* for usb host and peripheral controller drivers */
Grazvydas Ignotas748eee02010-09-27 15:17:18 +0300168#ifdef CONFIG_USB_OTG_UTILS
Heikki Krogerus86753812012-02-13 13:24:02 +0200169extern struct usb_phy *otg_get_transceiver(void);
170extern void otg_put_transceiver(struct usb_phy *);
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200171extern const char *otg_state_string(enum usb_otg_state state);
Grazvydas Ignotas748eee02010-09-27 15:17:18 +0300172#else
Heikki Krogerus86753812012-02-13 13:24:02 +0200173static inline struct usb_phy *otg_get_transceiver(void)
Grazvydas Ignotas748eee02010-09-27 15:17:18 +0300174{
175 return NULL;
176}
177
Heikki Krogerus86753812012-02-13 13:24:02 +0200178static inline void otg_put_transceiver(struct usb_phy *x)
Grazvydas Ignotas748eee02010-09-27 15:17:18 +0300179{
180}
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200181
182static inline const char *otg_state_string(enum usb_otg_state state)
183{
184 return NULL;
185}
Grazvydas Ignotas748eee02010-09-27 15:17:18 +0300186#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Robert Jarzmikc2344f12009-01-24 23:54:31 -0800188/* Context: can sleep */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static inline int
Heikki Krogerus86753812012-02-13 13:24:02 +0200190otg_start_hnp(struct usb_phy *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Heikki Krogerus86753812012-02-13 13:24:02 +0200192 return x->start_hnp(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300195/* Context: can sleep */
196static inline int
Heikki Krogerus86753812012-02-13 13:24:02 +0200197otg_set_vbus(struct usb_phy *x, bool enabled)
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300198{
Heikki Krogerus86753812012-02-13 13:24:02 +0200199 return x->set_vbus(x, enabled);
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300200}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202/* for HCDs */
203static inline int
Heikki Krogerus86753812012-02-13 13:24:02 +0200204otg_set_host(struct usb_phy *x, struct usb_bus *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Heikki Krogerus86753812012-02-13 13:24:02 +0200206 return x->set_host(x, host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/* for usb peripheral controller drivers */
Robert Jarzmikc2344f12009-01-24 23:54:31 -0800210
211/* Context: can sleep */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212static inline int
Heikki Krogerus86753812012-02-13 13:24:02 +0200213otg_set_peripheral(struct usb_phy *x, struct usb_gadget *periph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Heikki Krogerus86753812012-02-13 13:24:02 +0200215 return x->set_peripheral(x, periph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218static inline int
Heikki Krogerus86753812012-02-13 13:24:02 +0200219otg_set_power(struct usb_phy *x, unsigned mA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Heikki Krogerus86753812012-02-13 13:24:02 +0200221 return x->set_power(x, mA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Robert Jarzmikc2344f12009-01-24 23:54:31 -0800224/* Context: can sleep */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static inline int
Heikki Krogerus86753812012-02-13 13:24:02 +0200226otg_set_suspend(struct usb_phy *x, int suspend)
Juha Yrj?l?4e671852005-10-16 15:47:04 -0700227{
Heikki Krogerus86753812012-02-13 13:24:02 +0200228 if (x->set_suspend != NULL)
229 return x->set_suspend(x, suspend);
Juha Yrj?l?4e671852005-10-16 15:47:04 -0700230 else
231 return 0;
232}
233
234static inline int
Heikki Krogerus86753812012-02-13 13:24:02 +0200235otg_start_srp(struct usb_phy *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Heikki Krogerus86753812012-02-13 13:24:02 +0200237 return x->start_srp(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Felipe Balbie9a20172009-12-17 13:01:36 +0200240/* notifiers */
241static inline int
Heikki Krogerus86753812012-02-13 13:24:02 +0200242otg_register_notifier(struct usb_phy *x, struct notifier_block *nb)
Felipe Balbie9a20172009-12-17 13:01:36 +0200243{
Heikki Krogerus86753812012-02-13 13:24:02 +0200244 return atomic_notifier_chain_register(&x->notifier, nb);
Felipe Balbie9a20172009-12-17 13:01:36 +0200245}
246
247static inline void
Heikki Krogerus86753812012-02-13 13:24:02 +0200248otg_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
Felipe Balbie9a20172009-12-17 13:01:36 +0200249{
Heikki Krogerus86753812012-02-13 13:24:02 +0200250 atomic_notifier_chain_unregister(&x->notifier, nb);
Felipe Balbie9a20172009-12-17 13:01:36 +0200251}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253/* for OTG controller drivers (and maybe other stuff) */
254extern int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num);
Robert P. J. Daydda43a02008-03-07 13:45:32 -0500255
256#endif /* __LINUX_USB_OTG_H */