blob: 02c39a72b27e3bae66fac6bf12dd1ce71f6b8122 [file] [log] [blame]
Felipe Balbi550a7372008-07-24 12:27:36 +03001/*
2 * Copyright (C) 2005-2007 by Texas Instruments
3 * Some code has been taken from tusb6010.c
4 * Copyrights for that are attributable to:
5 * Copyright (C) 2006 Nokia Corporation
Felipe Balbi550a7372008-07-24 12:27:36 +03006 * Tony Lindgren <tony@atomide.com>
7 *
8 * This file is part of the Inventra Controller Driver for Linux.
9 *
10 * The Inventra Controller Driver for Linux is free software; you
11 * can redistribute it and/or modify it under the terms of the GNU
12 * General Public License version 2 as published by the Free Software
13 * Foundation.
14 *
15 * The Inventra Controller Driver for Linux is distributed in
16 * the hope that it will be useful, but WITHOUT ANY WARRANTY;
17 * without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
19 * License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with The Inventra Controller Driver for Linux ; if not,
23 * write to the Free Software Foundation, Inc., 59 Temple Place,
24 * Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27#include <linux/module.h>
28#include <linux/kernel.h>
29#include <linux/sched.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030030#include <linux/init.h>
31#include <linux/list.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030032#include <linux/io.h>
Felipe Balbidc098862010-12-01 15:01:11 +020033#include <linux/platform_device.h>
34#include <linux/dma-mapping.h>
Hema HK207b0e12011-02-17 12:07:22 +053035#include <linux/pm_runtime.h>
36#include <linux/err.h>
NeilBrown12a19b52012-08-13 12:32:58 +100037#include <linux/delay.h>
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +053038#include <linux/usb/musb-omap.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030039
Felipe Balbi550a7372008-07-24 12:27:36 +030040#include "musb_core.h"
41#include "omap2430.h"
42
Felipe Balbia3cee122010-12-02 09:27:29 +020043struct omap2430_glue {
44 struct device *dev;
45 struct platform_device *musb;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +053046 enum omap_musb_vbus_id_status status;
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +053047 struct work_struct omap_musb_mailbox_work;
Felipe Balbia3cee122010-12-02 09:27:29 +020048};
Felipe Balbic20aebb2010-12-02 12:44:40 +020049#define glue_to_musb(g) platform_get_drvdata(g->musb)
Felipe Balbia3cee122010-12-02 09:27:29 +020050
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +053051struct omap2430_glue *_glue;
52
Felipe Balbi550a7372008-07-24 12:27:36 +030053static struct timer_list musb_idle_timer;
54
55static void musb_do_idle(unsigned long _musb)
56{
57 struct musb *musb = (void *)_musb;
58 unsigned long flags;
59 u8 power;
Felipe Balbi550a7372008-07-24 12:27:36 +030060 u8 devctl;
61
Felipe Balbi550a7372008-07-24 12:27:36 +030062 spin_lock_irqsave(&musb->lock, flags);
63
David Brownell84e250f2009-03-31 12:30:04 -070064 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +030065 case OTG_STATE_A_WAIT_BCON:
Felipe Balbi550a7372008-07-24 12:27:36 +030066
67 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
68 if (devctl & MUSB_DEVCTL_BDEVICE) {
David Brownell84e250f2009-03-31 12:30:04 -070069 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +030070 MUSB_DEV_MODE(musb);
71 } else {
David Brownell84e250f2009-03-31 12:30:04 -070072 musb->xceiv->state = OTG_STATE_A_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +030073 MUSB_HST_MODE(musb);
74 }
75 break;
Felipe Balbi550a7372008-07-24 12:27:36 +030076 case OTG_STATE_A_SUSPEND:
77 /* finish RESUME signaling? */
78 if (musb->port1_status & MUSB_PORT_STAT_RESUME) {
79 power = musb_readb(musb->mregs, MUSB_POWER);
80 power &= ~MUSB_POWER_RESUME;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +030081 dev_dbg(musb->controller, "root port resume stopped, power %02x\n", power);
Felipe Balbi550a7372008-07-24 12:27:36 +030082 musb_writeb(musb->mregs, MUSB_POWER, power);
83 musb->is_active = 1;
84 musb->port1_status &= ~(USB_PORT_STAT_SUSPEND
85 | MUSB_PORT_STAT_RESUME);
86 musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16;
87 usb_hcd_poll_rh_status(musb_to_hcd(musb));
88 /* NOTE: it might really be A_WAIT_BCON ... */
David Brownell84e250f2009-03-31 12:30:04 -070089 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +030090 }
91 break;
Felipe Balbi550a7372008-07-24 12:27:36 +030092 case OTG_STATE_A_HOST:
93 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
94 if (devctl & MUSB_DEVCTL_BDEVICE)
David Brownell84e250f2009-03-31 12:30:04 -070095 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +030096 else
David Brownell84e250f2009-03-31 12:30:04 -070097 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
Felipe Balbi550a7372008-07-24 12:27:36 +030098 default:
99 break;
100 }
101 spin_unlock_irqrestore(&musb->lock, flags);
102}
103
104
Felipe Balbi743411b2010-12-01 13:22:05 +0200105static void omap2430_musb_try_idle(struct musb *musb, unsigned long timeout)
Felipe Balbi550a7372008-07-24 12:27:36 +0300106{
107 unsigned long default_timeout = jiffies + msecs_to_jiffies(3);
108 static unsigned long last_timer;
109
110 if (timeout == 0)
111 timeout = default_timeout;
112
113 /* Never idle if active, or when VBUS timeout is not set as host */
114 if (musb->is_active || ((musb->a_wait_bcon == 0)
David Brownell84e250f2009-03-31 12:30:04 -0700115 && (musb->xceiv->state == OTG_STATE_A_WAIT_BCON))) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300116 dev_dbg(musb->controller, "%s active, deleting timer\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200117 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300118 del_timer(&musb_idle_timer);
119 last_timer = jiffies;
120 return;
121 }
122
123 if (time_after(last_timer, timeout)) {
124 if (!timer_pending(&musb_idle_timer))
125 last_timer = timeout;
126 else {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300127 dev_dbg(musb->controller, "Longer idle timer already pending, ignoring\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300128 return;
129 }
130 }
131 last_timer = timeout;
132
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300133 dev_dbg(musb->controller, "%s inactive, for idle timer for %lu ms\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200134 otg_state_string(musb->xceiv->state),
Felipe Balbi550a7372008-07-24 12:27:36 +0300135 (unsigned long)jiffies_to_msecs(timeout - jiffies));
136 mod_timer(&musb_idle_timer, timeout);
137}
138
Felipe Balbi743411b2010-12-01 13:22:05 +0200139static void omap2430_musb_set_vbus(struct musb *musb, int is_on)
Felipe Balbi550a7372008-07-24 12:27:36 +0300140{
Heikki Krogerusd445b6d2012-02-13 13:24:15 +0200141 struct usb_otg *otg = musb->xceiv->otg;
Felipe Balbi550a7372008-07-24 12:27:36 +0300142 u8 devctl;
Hema HK594632e2010-12-10 18:10:51 +0530143 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
Felipe Balbi550a7372008-07-24 12:27:36 +0300144 /* HDRC controls CPEN, but beware current surges during device
145 * connect. They can trigger transient overcurrent conditions
146 * that must be ignored.
147 */
148
149 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
150
151 if (is_on) {
Hema HK594632e2010-12-10 18:10:51 +0530152 if (musb->xceiv->state == OTG_STATE_A_IDLE) {
NeilBrown12a19b52012-08-13 12:32:58 +1000153 int loops = 100;
Hema HK594632e2010-12-10 18:10:51 +0530154 /* start the session */
155 devctl |= MUSB_DEVCTL_SESSION;
156 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
157 /*
158 * Wait for the musb to set as A device to enable the
159 * VBUS
160 */
161 while (musb_readb(musb->mregs, MUSB_DEVCTL) & 0x80) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300162
NeilBrown12a19b52012-08-13 12:32:58 +1000163 mdelay(5);
Hema HK594632e2010-12-10 18:10:51 +0530164 cpu_relax();
165
NeilBrown12a19b52012-08-13 12:32:58 +1000166 if (time_after(jiffies, timeout)
167 || loops-- <= 0) {
Hema HK594632e2010-12-10 18:10:51 +0530168 dev_err(musb->controller,
169 "configured as A device timeout");
Hema HK594632e2010-12-10 18:10:51 +0530170 break;
171 }
172 }
173
Matthias Brugger10770c52012-08-16 13:00:25 +0200174 if (otg->set_vbus)
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200175 otg_set_vbus(otg, 1);
Hema HK594632e2010-12-10 18:10:51 +0530176 } else {
177 musb->is_active = 1;
Heikki Krogerusd445b6d2012-02-13 13:24:15 +0200178 otg->default_a = 1;
Hema HK594632e2010-12-10 18:10:51 +0530179 musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
180 devctl |= MUSB_DEVCTL_SESSION;
181 MUSB_HST_MODE(musb);
182 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300183 } else {
184 musb->is_active = 0;
185
186 /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and
187 * jumping right to B_IDLE...
188 */
189
Heikki Krogerusd445b6d2012-02-13 13:24:15 +0200190 otg->default_a = 0;
David Brownell84e250f2009-03-31 12:30:04 -0700191 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300192 devctl &= ~MUSB_DEVCTL_SESSION;
193
194 MUSB_DEV_MODE(musb);
195 }
196 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
197
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300198 dev_dbg(musb->controller, "VBUS %s, devctl %02x "
Felipe Balbi550a7372008-07-24 12:27:36 +0300199 /* otg %3x conf %08x prcm %08x */ "\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200200 otg_state_string(musb->xceiv->state),
Felipe Balbi550a7372008-07-24 12:27:36 +0300201 musb_readb(musb->mregs, MUSB_DEVCTL));
202}
Felipe Balbi550a7372008-07-24 12:27:36 +0300203
Felipe Balbi743411b2010-12-01 13:22:05 +0200204static int omap2430_musb_set_mode(struct musb *musb, u8 musb_mode)
Felipe Balbi550a7372008-07-24 12:27:36 +0300205{
206 u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
207
208 devctl |= MUSB_DEVCTL_SESSION;
209 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
210
David Brownell96a274d2008-11-24 13:06:47 +0200211 return 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300212}
213
Felipe Balbic20aebb2010-12-02 12:44:40 +0200214static inline void omap2430_low_level_exit(struct musb *musb)
215{
216 u32 l;
217
218 /* in any role */
219 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
220 l |= ENABLEFORCE; /* enable MSTANDBY */
221 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
Felipe Balbic20aebb2010-12-02 12:44:40 +0200222}
223
224static inline void omap2430_low_level_init(struct musb *musb)
225{
226 u32 l;
227
Felipe Balbic20aebb2010-12-02 12:44:40 +0200228 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
229 l &= ~ENABLEFORCE; /* disable MSTANDBY */
230 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
231}
232
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530233void omap_musb_mailbox(enum omap_musb_vbus_id_status status)
Hema HK594632e2010-12-10 18:10:51 +0530234{
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530235 struct omap2430_glue *glue = _glue;
236 struct musb *musb = glue_to_musb(glue);
Vikram Pandita712d8ef2011-08-12 07:38:51 -0700237
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530238 glue->status = status;
239 if (!musb) {
240 dev_err(glue->dev, "musb core is not yet ready\n");
241 return;
242 }
243
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +0530244 schedule_work(&glue->omap_musb_mailbox_work);
Vikram Pandita712d8ef2011-08-12 07:38:51 -0700245}
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530246EXPORT_SYMBOL_GPL(omap_musb_mailbox);
Vikram Pandita712d8ef2011-08-12 07:38:51 -0700247
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530248static void omap_musb_set_mailbox(struct omap2430_glue *glue)
Vikram Pandita712d8ef2011-08-12 07:38:51 -0700249{
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +0530250 struct musb *musb = glue_to_musb(glue);
Hema HK594632e2010-12-10 18:10:51 +0530251 struct device *dev = musb->controller;
252 struct musb_hdrc_platform_data *pdata = dev->platform_data;
253 struct omap_musb_board_data *data = pdata->board_data;
Kishon Vijay Abraham Ic83a8542012-06-22 17:40:53 +0530254 struct usb_otg *otg = musb->xceiv->otg;
Hema HK594632e2010-12-10 18:10:51 +0530255
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530256 switch (glue->status) {
257 case OMAP_MUSB_ID_GROUND:
258 dev_dbg(dev, "ID GND\n");
Hema HK594632e2010-12-10 18:10:51 +0530259
Kishon Vijay Abraham Ic83a8542012-06-22 17:40:53 +0530260 otg->default_a = true;
261 musb->xceiv->state = OTG_STATE_A_IDLE;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530262 musb->xceiv->last_event = USB_EVENT_ID;
Felipe Balbi032ec492011-11-24 15:46:26 +0200263 if (musb->gadget_driver) {
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530264 pm_runtime_get_sync(dev);
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200265 usb_phy_init(musb->xceiv);
Hema HK70045c52011-02-28 15:05:29 +0530266 omap2430_musb_set_vbus(musb, 1);
Hema HK594632e2010-12-10 18:10:51 +0530267 }
268 break;
269
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530270 case OMAP_MUSB_VBUS_VALID:
271 dev_dbg(dev, "VBUS Connect\n");
Hema HK594632e2010-12-10 18:10:51 +0530272
Kishon Vijay Abraham Ic83a8542012-06-22 17:40:53 +0530273 otg->default_a = false;
274 musb->xceiv->state = OTG_STATE_B_IDLE;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530275 musb->xceiv->last_event = USB_EVENT_VBUS;
Hema HK7acc6192011-02-28 14:19:34 +0530276 if (musb->gadget_driver)
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530277 pm_runtime_get_sync(dev);
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200278 usb_phy_init(musb->xceiv);
Hema HK594632e2010-12-10 18:10:51 +0530279 break;
280
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530281 case OMAP_MUSB_ID_FLOAT:
282 case OMAP_MUSB_VBUS_OFF:
283 dev_dbg(dev, "VBUS Disconnect\n");
Hema HK594632e2010-12-10 18:10:51 +0530284
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530285 musb->xceiv->last_event = USB_EVENT_NONE;
Felipe Balbi032ec492011-11-24 15:46:26 +0200286 if (musb->gadget_driver) {
287 pm_runtime_mark_last_busy(dev);
288 pm_runtime_put_autosuspend(dev);
289 }
Hema HK7acc6192011-02-28 14:19:34 +0530290
Hema HK594632e2010-12-10 18:10:51 +0530291 if (data->interface_type == MUSB_INTERFACE_UTMI) {
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200292 if (musb->xceiv->otg->set_vbus)
293 otg_set_vbus(musb->xceiv->otg, 0);
Hema HK594632e2010-12-10 18:10:51 +0530294 }
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200295 usb_phy_shutdown(musb->xceiv);
Hema HK594632e2010-12-10 18:10:51 +0530296 break;
297 default:
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530298 dev_dbg(dev, "ID float\n");
Hema HK594632e2010-12-10 18:10:51 +0530299 }
Hema HK594632e2010-12-10 18:10:51 +0530300}
301
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530302
303static void omap_musb_mailbox_work(struct work_struct *mailbox_work)
304{
305 struct omap2430_glue *glue = container_of(mailbox_work,
306 struct omap2430_glue, omap_musb_mailbox_work);
307 omap_musb_set_mailbox(glue);
308}
309
Felipe Balbi743411b2010-12-01 13:22:05 +0200310static int omap2430_musb_init(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300311{
Shubhrajyoti Dad579692012-03-22 12:48:06 +0530312 u32 l;
313 int status = 0;
Hema Kalliguddiea65df52010-09-22 19:27:40 -0500314 struct device *dev = musb->controller;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530315 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
Hema Kalliguddiea65df52010-09-22 19:27:40 -0500316 struct musb_hdrc_platform_data *plat = dev->platform_data;
317 struct omap_musb_board_data *data = plat->board_data;
Felipe Balbi550a7372008-07-24 12:27:36 +0300318
David Brownell84e250f2009-03-31 12:30:04 -0700319 /* We require some kind of external transceiver, hooked
320 * up through ULPI. TWL4030-family PMICs include one,
321 * which needs a driver, drivers aren't always needed.
322 */
Kishon Vijay Abraham Ib1183c22012-06-22 17:40:54 +0530323 musb->xceiv = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530324 if (IS_ERR_OR_NULL(musb->xceiv)) {
David Brownell84e250f2009-03-31 12:30:04 -0700325 pr_err("HS USB OTG: no transceiver configured\n");
326 return -ENODEV;
327 }
328
Hema HK7acc6192011-02-28 14:19:34 +0530329 status = pm_runtime_get_sync(dev);
330 if (status < 0) {
Shubhrajyoti Dad579692012-03-22 12:48:06 +0530331 dev_err(dev, "pm_runtime_get_sync FAILED %d\n", status);
Hema HK7acc6192011-02-28 14:19:34 +0530332 goto err1;
333 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300334
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200335 l = musb_readl(musb->mregs, OTG_INTERFSEL);
Maulik Mankadde2e1b02010-03-12 10:29:07 +0200336
337 if (data->interface_type == MUSB_INTERFACE_UTMI) {
338 /* OMAP4 uses Internal PHY GS70 which uses UTMI interface */
339 l &= ~ULPI_12PIN; /* Disable ULPI */
340 l |= UTMI_8BIT; /* Enable UTMI */
341 } else {
342 l |= ULPI_12PIN;
343 }
344
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200345 musb_writel(musb->mregs, OTG_INTERFSEL, l);
Felipe Balbi550a7372008-07-24 12:27:36 +0300346
347 pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
348 "sysstatus 0x%x, intrfsel 0x%x, simenable 0x%x\n",
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200349 musb_readl(musb->mregs, OTG_REVISION),
350 musb_readl(musb->mregs, OTG_SYSCONFIG),
351 musb_readl(musb->mregs, OTG_SYSSTATUS),
352 musb_readl(musb->mregs, OTG_INTERFSEL),
353 musb_readl(musb->mregs, OTG_SIMENABLE));
Felipe Balbi550a7372008-07-24 12:27:36 +0300354
Felipe Balbi550a7372008-07-24 12:27:36 +0300355 setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
356
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530357 if (glue->status != OMAP_MUSB_UNKNOWN)
358 omap_musb_set_mailbox(glue);
359
Grazvydas Ignotasc04352a2012-02-04 19:43:51 +0200360 pm_runtime_put_noidle(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +0300361 return 0;
Hema HK7acc6192011-02-28 14:19:34 +0530362
363err1:
Hema HK7acc6192011-02-28 14:19:34 +0530364 return status;
Felipe Balbi550a7372008-07-24 12:27:36 +0300365}
366
Hema HK002eda12011-02-17 12:06:10 +0530367static void omap2430_musb_enable(struct musb *musb)
368{
369 u8 devctl;
370 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
371 struct device *dev = musb->controller;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530372 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
Hema HK002eda12011-02-17 12:06:10 +0530373 struct musb_hdrc_platform_data *pdata = dev->platform_data;
374 struct omap_musb_board_data *data = pdata->board_data;
375
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530376 switch (glue->status) {
Hema HK002eda12011-02-17 12:06:10 +0530377
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530378 case OMAP_MUSB_ID_GROUND:
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200379 usb_phy_init(musb->xceiv);
Felipe Contreras08dec562011-12-19 22:17:50 +0200380 if (data->interface_type != MUSB_INTERFACE_UTMI)
381 break;
382 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
383 /* start the session */
384 devctl |= MUSB_DEVCTL_SESSION;
385 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
386 while (musb_readb(musb->mregs, MUSB_DEVCTL) &
387 MUSB_DEVCTL_BDEVICE) {
388 cpu_relax();
Hema HK002eda12011-02-17 12:06:10 +0530389
Felipe Contreras08dec562011-12-19 22:17:50 +0200390 if (time_after(jiffies, timeout)) {
391 dev_err(dev, "configured as A device timeout");
392 break;
Hema HK002eda12011-02-17 12:06:10 +0530393 }
394 }
395 break;
396
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530397 case OMAP_MUSB_VBUS_VALID:
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200398 usb_phy_init(musb->xceiv);
Hema HK002eda12011-02-17 12:06:10 +0530399 break;
400
401 default:
402 break;
403 }
404}
405
406static void omap2430_musb_disable(struct musb *musb)
407{
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530408 struct device *dev = musb->controller;
409 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
410
411 if (glue->status != OMAP_MUSB_UNKNOWN)
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200412 usb_phy_shutdown(musb->xceiv);
Felipe Balbi550a7372008-07-24 12:27:36 +0300413}
414
Felipe Balbi743411b2010-12-01 13:22:05 +0200415static int omap2430_musb_exit(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300416{
Johan Hovold19b9a832011-02-11 16:57:08 +0100417 del_timer_sync(&musb_idle_timer);
Felipe Balbi550a7372008-07-24 12:27:36 +0300418
Felipe Balbic20aebb2010-12-02 12:44:40 +0200419 omap2430_low_level_exit(musb);
Felipe Balbic20aebb2010-12-02 12:44:40 +0200420
Felipe Balbi550a7372008-07-24 12:27:36 +0300421 return 0;
422}
Felipe Balbi743411b2010-12-01 13:22:05 +0200423
Felipe Balbif7ec9432010-12-02 09:48:58 +0200424static const struct musb_platform_ops omap2430_ops = {
Felipe Balbi743411b2010-12-01 13:22:05 +0200425 .init = omap2430_musb_init,
426 .exit = omap2430_musb_exit,
427
Felipe Balbi743411b2010-12-01 13:22:05 +0200428 .set_mode = omap2430_musb_set_mode,
429 .try_idle = omap2430_musb_try_idle,
430
431 .set_vbus = omap2430_musb_set_vbus,
Hema HK002eda12011-02-17 12:06:10 +0530432
433 .enable = omap2430_musb_enable,
434 .disable = omap2430_musb_disable,
Felipe Balbi743411b2010-12-01 13:22:05 +0200435};
Felipe Balbidc098862010-12-01 15:01:11 +0200436
437static u64 omap2430_dmamask = DMA_BIT_MASK(32);
438
Felipe Balbie9e8c852012-01-26 12:40:23 +0200439static int __devinit omap2430_probe(struct platform_device *pdev)
Felipe Balbidc098862010-12-01 15:01:11 +0200440{
441 struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
442 struct platform_device *musb;
Felipe Balbia3cee122010-12-02 09:27:29 +0200443 struct omap2430_glue *glue;
Felipe Balbidc098862010-12-01 15:01:11 +0200444 int ret = -ENOMEM;
445
Kishon Vijay Abraham Ib1183c22012-06-22 17:40:54 +0530446 glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
Felipe Balbia3cee122010-12-02 09:27:29 +0200447 if (!glue) {
448 dev_err(&pdev->dev, "failed to allocate glue context\n");
449 goto err0;
450 }
451
Felipe Balbidc098862010-12-01 15:01:11 +0200452 musb = platform_device_alloc("musb-hdrc", -1);
453 if (!musb) {
454 dev_err(&pdev->dev, "failed to allocate musb device\n");
Kishon Vijay Abraham Ib1183c22012-06-22 17:40:54 +0530455 goto err0;
Felipe Balbidc098862010-12-01 15:01:11 +0200456 }
457
458 musb->dev.parent = &pdev->dev;
459 musb->dev.dma_mask = &omap2430_dmamask;
460 musb->dev.coherent_dma_mask = omap2430_dmamask;
461
Felipe Balbia3cee122010-12-02 09:27:29 +0200462 glue->dev = &pdev->dev;
463 glue->musb = musb;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530464 glue->status = OMAP_MUSB_UNKNOWN;
Felipe Balbia3cee122010-12-02 09:27:29 +0200465
Felipe Balbif7ec9432010-12-02 09:48:58 +0200466 pdata->platform_ops = &omap2430_ops;
467
Felipe Balbia3cee122010-12-02 09:27:29 +0200468 platform_set_drvdata(pdev, glue);
Felipe Balbidc098862010-12-01 15:01:11 +0200469
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530470 /*
471 * REVISIT if we ever have two instances of the wrapper, we will be
472 * in big trouble
473 */
474 _glue = glue;
475
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +0530476 INIT_WORK(&glue->omap_musb_mailbox_work, omap_musb_mailbox_work);
477
Felipe Balbidc098862010-12-01 15:01:11 +0200478 ret = platform_device_add_resources(musb, pdev->resource,
479 pdev->num_resources);
480 if (ret) {
481 dev_err(&pdev->dev, "failed to add resources\n");
Kishon Vijay Abraham Ib1183c22012-06-22 17:40:54 +0530482 goto err1;
Felipe Balbidc098862010-12-01 15:01:11 +0200483 }
484
485 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
486 if (ret) {
487 dev_err(&pdev->dev, "failed to add platform_data\n");
Kishon Vijay Abraham Ib1183c22012-06-22 17:40:54 +0530488 goto err1;
Felipe Balbidc098862010-12-01 15:01:11 +0200489 }
490
Kishon Vijay Abraham I3006dc82012-03-21 21:30:20 +0530491 pm_runtime_enable(&pdev->dev);
492
Felipe Balbidc098862010-12-01 15:01:11 +0200493 ret = platform_device_add(musb);
494 if (ret) {
495 dev_err(&pdev->dev, "failed to register musb device\n");
Kishon Vijay Abraham Ib1183c22012-06-22 17:40:54 +0530496 goto err1;
Felipe Balbidc098862010-12-01 15:01:11 +0200497 }
498
499 return 0;
500
Felipe Balbia3cee122010-12-02 09:27:29 +0200501err1:
Kishon Vijay Abraham Ib1183c22012-06-22 17:40:54 +0530502 platform_device_put(musb);
Felipe Balbia3cee122010-12-02 09:27:29 +0200503
Felipe Balbidc098862010-12-01 15:01:11 +0200504err0:
505 return ret;
506}
507
Felipe Balbie9e8c852012-01-26 12:40:23 +0200508static int __devexit omap2430_remove(struct platform_device *pdev)
Felipe Balbidc098862010-12-01 15:01:11 +0200509{
Felipe Balbia3cee122010-12-02 09:27:29 +0200510 struct omap2430_glue *glue = platform_get_drvdata(pdev);
Felipe Balbidc098862010-12-01 15:01:11 +0200511
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +0530512 cancel_work_sync(&glue->omap_musb_mailbox_work);
Kishon Vijay Abraham If039df52012-08-02 15:30:07 +0530513 platform_device_unregister(glue->musb);
Felipe Balbidc098862010-12-01 15:01:11 +0200514
515 return 0;
516}
517
Felipe Balbic20aebb2010-12-02 12:44:40 +0200518#ifdef CONFIG_PM
Felipe Balbic20aebb2010-12-02 12:44:40 +0200519
Hema HK7acc6192011-02-28 14:19:34 +0530520static int omap2430_runtime_suspend(struct device *dev)
Felipe Balbic20aebb2010-12-02 12:44:40 +0200521{
522 struct omap2430_glue *glue = dev_get_drvdata(dev);
523 struct musb *musb = glue_to_musb(glue);
524
Vladimir Zapolskiyafb76df2011-12-23 18:37:18 +0200525 if (musb) {
526 musb->context.otg_interfsel = musb_readl(musb->mregs,
527 OTG_INTERFSEL);
Hema HKe25bec12011-09-07 09:19:24 -0700528
Vladimir Zapolskiyafb76df2011-12-23 18:37:18 +0200529 omap2430_low_level_exit(musb);
530 usb_phy_set_suspend(musb->xceiv, 1);
531 }
Felipe Balbic20aebb2010-12-02 12:44:40 +0200532
533 return 0;
534}
535
Hema HK7acc6192011-02-28 14:19:34 +0530536static int omap2430_runtime_resume(struct device *dev)
Felipe Balbic20aebb2010-12-02 12:44:40 +0200537{
538 struct omap2430_glue *glue = dev_get_drvdata(dev);
539 struct musb *musb = glue_to_musb(glue);
Felipe Balbic20aebb2010-12-02 12:44:40 +0200540
Vladimir Zapolskiyafb76df2011-12-23 18:37:18 +0200541 if (musb) {
542 omap2430_low_level_init(musb);
543 musb_writel(musb->mregs, OTG_INTERFSEL,
544 musb->context.otg_interfsel);
Hema HKe25bec12011-09-07 09:19:24 -0700545
Vladimir Zapolskiyafb76df2011-12-23 18:37:18 +0200546 usb_phy_set_suspend(musb->xceiv, 0);
547 }
Felipe Balbic20aebb2010-12-02 12:44:40 +0200548
549 return 0;
550}
551
552static struct dev_pm_ops omap2430_pm_ops = {
Hema HK7acc6192011-02-28 14:19:34 +0530553 .runtime_suspend = omap2430_runtime_suspend,
554 .runtime_resume = omap2430_runtime_resume,
Felipe Balbic20aebb2010-12-02 12:44:40 +0200555};
556
557#define DEV_PM_OPS (&omap2430_pm_ops)
558#else
559#define DEV_PM_OPS NULL
560#endif
561
Felipe Balbidc098862010-12-01 15:01:11 +0200562static struct platform_driver omap2430_driver = {
Felipe Balbie9e8c852012-01-26 12:40:23 +0200563 .probe = omap2430_probe,
564 .remove = __devexit_p(omap2430_remove),
Felipe Balbidc098862010-12-01 15:01:11 +0200565 .driver = {
566 .name = "musb-omap2430",
Felipe Balbic20aebb2010-12-02 12:44:40 +0200567 .pm = DEV_PM_OPS,
Felipe Balbidc098862010-12-01 15:01:11 +0200568 },
569};
570
571MODULE_DESCRIPTION("OMAP2PLUS MUSB Glue Layer");
572MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
573MODULE_LICENSE("GPL v2");
574
575static int __init omap2430_init(void)
576{
Felipe Balbie9e8c852012-01-26 12:40:23 +0200577 return platform_driver_register(&omap2430_driver);
Felipe Balbidc098862010-12-01 15:01:11 +0200578}
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530579subsys_initcall(omap2430_init);
Felipe Balbidc098862010-12-01 15:01:11 +0200580
581static void __exit omap2430_exit(void)
582{
583 platform_driver_unregister(&omap2430_driver);
584}
585module_exit(omap2430_exit);