blob: ae3ffe4fc9cae8c054a227d5075b23bd50df9f50 [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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070038enum usb_otg_event {
39 /* Device is not connected within
40 * TA_WAIT_BCON or not responding.
41 */
42 OTG_EVENT_DEV_CONN_TMOUT,
43 /* B-device returned STALL for
44 * B_HNP_ENABLE feature request.
45 */
46 OTG_EVENT_NO_RESP_FOR_HNP_ENABLE,
47 /* HUB class devices are not
48 * supported.
49 */
50 OTG_EVENT_HUB_NOT_SUPPORTED,
51 /* Device is not supported i.e
52 * not listed in TPL.
53 */
54 OTG_EVENT_DEV_NOT_SUPPORTED,
55 /* HNP failed due to
56 * TA_AIDL_BDIS timeout or
57 * TB_ASE0_BRST timeout
58 */
59 OTG_EVENT_HNP_FAILED,
60 /* B-device did not detect VBUS
61 * within TB_SRP_FAIL time.
62 */
63 OTG_EVENT_NO_RESP_FOR_SRP,
64};
65
Heikki Krogerusde07e182012-02-13 13:24:03 +020066enum usb_phy_events {
Felipe Balbie9a20172009-12-17 13:01:36 +020067 USB_EVENT_NONE, /* no events or cable disconnected */
68 USB_EVENT_VBUS, /* vbus valid event */
69 USB_EVENT_ID, /* id was grounded */
70 USB_EVENT_CHARGER, /* usb dedicated charger */
71 USB_EVENT_ENUMERATED, /* gadget driver enumerated */
72};
73
Heikki Krogerus86753812012-02-13 13:24:02 +020074struct usb_phy;
Daniel Mack91c8a5a2009-10-15 17:09:34 +030075
76/* for transceivers connected thru an ULPI interface, the user must
77 * provide access ops
78 */
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +020079struct usb_phy_io_ops {
Heikki Krogerus86753812012-02-13 13:24:02 +020080 int (*read)(struct usb_phy *x, u32 reg);
81 int (*write)(struct usb_phy *x, u32 val, u32 reg);
Daniel Mack91c8a5a2009-10-15 17:09:34 +030082};
83
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +020084struct usb_otg {
85 u8 default_a;
86
87 struct usb_phy *phy;
88 struct usb_bus *host;
89 struct usb_gadget *gadget;
90
91 /* bind/unbind the host controller */
92 int (*set_host)(struct usb_otg *otg, struct usb_bus *host);
93
94 /* bind/unbind the peripheral controller */
95 int (*set_peripheral)(struct usb_otg *otg,
96 struct usb_gadget *gadget);
97
98 /* effective for A-peripheral, ignored for B devices */
99 int (*set_vbus)(struct usb_otg *otg, bool enabled);
100
101 /* for B devices only: start session with A-Host */
102 int (*start_srp)(struct usb_otg *otg);
103
104 /* start or continue HNP role switch */
105 int (*start_hnp)(struct usb_otg *otg);
106
Steve Mucklef132c6c2012-06-06 18:30:57 -0700107 /* send events to user space */
108 int (*send_event)(struct usb_otg *otg,
109 enum usb_otg_event event);
110
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +0200111};
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113/*
114 * the otg driver needs to interact with both device side and host side
115 * usb controllers. it decides which controller is active at a given
116 * moment, using the transceiver, ID signal, HNP and sometimes static
117 * configuration information (including "board isn't wired for otg").
118 */
Heikki Krogerus86753812012-02-13 13:24:02 +0200119struct usb_phy {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 struct device *dev;
121 const char *label;
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300122 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 enum usb_otg_state state;
Heikki Krogerusde07e182012-02-13 13:24:03 +0200125 enum usb_phy_events last_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +0200127 struct usb_otg *otg;
128
Grazvydas Ignotasbf070bc2012-03-21 16:35:52 +0200129 struct device *io_dev;
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +0200130 struct usb_phy_io_ops *io_ops;
131 void __iomem *io_priv;
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300132
Heikki Krogerusde07e182012-02-13 13:24:03 +0200133 /* for notification of usb_phy_events */
Felipe Balbicccad6d2010-09-29 10:55:49 +0300134 struct atomic_notifier_head notifier;
Felipe Balbie9a20172009-12-17 13:01:36 +0200135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 /* to pass extra port status to the root hub */
137 u16 port_status;
138 u16 port_change;
139
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300140 /* initialize/shutdown the OTG controller */
Heikki Krogerus86753812012-02-13 13:24:02 +0200141 int (*init)(struct usb_phy *x);
142 void (*shutdown)(struct usb_phy *x);
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 /* effective for B devices, ignored for A-peripheral */
Heikki Krogerus86753812012-02-13 13:24:02 +0200145 int (*set_power)(struct usb_phy *x,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 unsigned mA);
147
Juha Yrj?l?4e671852005-10-16 15:47:04 -0700148 /* for non-OTG B devices: set transceiver into suspend mode */
Heikki Krogerus86753812012-02-13 13:24:02 +0200149 int (*set_suspend)(struct usb_phy *x,
Juha Yrj?l?4e671852005-10-16 15:47:04 -0700150 int suspend);
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152};
153
154
155/* for board-specific init logic */
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +0200156extern int usb_set_transceiver(struct usb_phy *);
David Brownellcc835e32009-03-31 12:28:31 -0700157
Guennadi Liakhovetski352a3372010-12-09 22:46:29 +0100158#if defined(CONFIG_NOP_USB_XCEIV) || (defined(CONFIG_NOP_USB_XCEIV_MODULE) && defined(MODULE))
David Brownellcc835e32009-03-31 12:28:31 -0700159/* sometimes transceivers are accessed only through e.g. ULPI */
Ajay Kumar Guptaf6d92a02009-02-06 17:32:35 +0530160extern void usb_nop_xceiv_register(void);
161extern void usb_nop_xceiv_unregister(void);
Maulik Mankad224e1542010-02-17 14:09:29 -0800162#else
163static inline void usb_nop_xceiv_register(void)
164{
165}
166
167static inline void usb_nop_xceiv_unregister(void)
168{
169}
170#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300172/* helpers for direct access thru low-level io interface */
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +0200173static inline int usb_phy_io_read(struct usb_phy *x, u32 reg)
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300174{
Heikki Krogerus86753812012-02-13 13:24:02 +0200175 if (x->io_ops && x->io_ops->read)
176 return x->io_ops->read(x, reg);
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300177
178 return -EINVAL;
179}
180
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +0200181static inline int usb_phy_io_write(struct usb_phy *x, u32 val, u32 reg)
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300182{
Heikki Krogerus86753812012-02-13 13:24:02 +0200183 if (x->io_ops && x->io_ops->write)
184 return x->io_ops->write(x, val, reg);
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300185
186 return -EINVAL;
187}
188
189static inline int
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +0200190usb_phy_init(struct usb_phy *x)
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300191{
Heikki Krogerus86753812012-02-13 13:24:02 +0200192 if (x->init)
193 return x->init(x);
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300194
195 return 0;
196}
197
198static inline void
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +0200199usb_phy_shutdown(struct usb_phy *x)
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300200{
Heikki Krogerus86753812012-02-13 13:24:02 +0200201 if (x->shutdown)
202 x->shutdown(x);
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300203}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700205/* for USB core, host and peripheral controller drivers */
206/* Context: can sleep */
207extern int otg_send_event(enum usb_otg_event event);
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/* for usb host and peripheral controller drivers */
Grazvydas Ignotas748eee02010-09-27 15:17:18 +0300210#ifdef CONFIG_USB_OTG_UTILS
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +0200211extern struct usb_phy *usb_get_transceiver(void);
212extern void usb_put_transceiver(struct usb_phy *);
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200213extern const char *otg_state_string(enum usb_otg_state state);
Grazvydas Ignotas748eee02010-09-27 15:17:18 +0300214#else
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +0200215static inline struct usb_phy *usb_get_transceiver(void)
Grazvydas Ignotas748eee02010-09-27 15:17:18 +0300216{
217 return NULL;
218}
219
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +0200220static inline void usb_put_transceiver(struct usb_phy *x)
Grazvydas Ignotas748eee02010-09-27 15:17:18 +0300221{
222}
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200223
224static inline const char *otg_state_string(enum usb_otg_state state)
225{
226 return NULL;
227}
Grazvydas Ignotas748eee02010-09-27 15:17:18 +0300228#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Robert Jarzmikc2344f12009-01-24 23:54:31 -0800230/* Context: can sleep */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231static inline int
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200232otg_start_hnp(struct usb_otg *otg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200234 if (otg && otg->start_hnp)
235 return otg->start_hnp(otg);
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +0200236
Heikki Krogerus136ced82012-02-13 13:24:19 +0200237 return -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300240/* Context: can sleep */
241static inline int
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200242otg_set_vbus(struct usb_otg *otg, bool enabled)
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300243{
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200244 if (otg && otg->set_vbus)
245 return otg->set_vbus(otg, enabled);
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +0200246
Heikki Krogerus136ced82012-02-13 13:24:19 +0200247 return -ENOTSUPP;
Daniel Mack91c8a5a2009-10-15 17:09:34 +0300248}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250/* for HCDs */
251static inline int
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200252otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200254 if (otg && otg->set_host)
255 return otg->set_host(otg, host);
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +0200256
Heikki Krogerus136ced82012-02-13 13:24:19 +0200257 return -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260/* for usb peripheral controller drivers */
Robert Jarzmikc2344f12009-01-24 23:54:31 -0800261
262/* Context: can sleep */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263static inline int
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200264otg_set_peripheral(struct usb_otg *otg, struct usb_gadget *periph)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200266 if (otg && otg->set_peripheral)
267 return otg->set_peripheral(otg, periph);
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +0200268
Heikki Krogerus136ced82012-02-13 13:24:19 +0200269 return -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272static inline int
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +0200273usb_phy_set_power(struct usb_phy *x, unsigned mA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +0200275 if (x && x->set_power)
276 return x->set_power(x, mA);
277 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Robert Jarzmikc2344f12009-01-24 23:54:31 -0800280/* Context: can sleep */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281static inline int
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +0200282usb_phy_set_suspend(struct usb_phy *x, int suspend)
Juha Yrj?l?4e671852005-10-16 15:47:04 -0700283{
Heikki Krogerus86753812012-02-13 13:24:02 +0200284 if (x->set_suspend != NULL)
285 return x->set_suspend(x, suspend);
Juha Yrj?l?4e671852005-10-16 15:47:04 -0700286 else
287 return 0;
288}
289
290static inline int
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200291otg_start_srp(struct usb_otg *otg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200293 if (otg && otg->start_srp)
294 return otg->start_srp(otg);
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +0200295
Heikki Krogerus136ced82012-02-13 13:24:19 +0200296 return -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Felipe Balbie9a20172009-12-17 13:01:36 +0200299/* notifiers */
300static inline int
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +0200301usb_register_notifier(struct usb_phy *x, struct notifier_block *nb)
Felipe Balbie9a20172009-12-17 13:01:36 +0200302{
Heikki Krogerus86753812012-02-13 13:24:02 +0200303 return atomic_notifier_chain_register(&x->notifier, nb);
Felipe Balbie9a20172009-12-17 13:01:36 +0200304}
305
306static inline void
Heikki Krogerus7a8a3a92012-02-13 13:24:04 +0200307usb_unregister_notifier(struct usb_phy *x, struct notifier_block *nb)
Felipe Balbie9a20172009-12-17 13:01:36 +0200308{
Heikki Krogerus86753812012-02-13 13:24:02 +0200309 atomic_notifier_chain_unregister(&x->notifier, nb);
Felipe Balbie9a20172009-12-17 13:01:36 +0200310}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312/* for OTG controller drivers (and maybe other stuff) */
313extern int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num);
Robert P. J. Daydda43a02008-03-07 13:45:32 -0500314
315#endif /* __LINUX_USB_OTG_H */