blob: f40c8053a291f481484a5a47fa6c627c244512db [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>
Felipe Balbi550a7372008-07-24 12:27:36 +030037
Felipe Balbi550a7372008-07-24 12:27:36 +030038#include "musb_core.h"
39#include "omap2430.h"
40
Felipe Balbia3cee122010-12-02 09:27:29 +020041struct omap2430_glue {
42 struct device *dev;
43 struct platform_device *musb;
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +053044 u8 xceiv_event;
45 struct work_struct omap_musb_mailbox_work;
Felipe Balbia3cee122010-12-02 09:27:29 +020046};
Felipe Balbic20aebb2010-12-02 12:44:40 +020047#define glue_to_musb(g) platform_get_drvdata(g->musb)
Felipe Balbia3cee122010-12-02 09:27:29 +020048
Felipe Balbi550a7372008-07-24 12:27:36 +030049static struct timer_list musb_idle_timer;
50
51static void musb_do_idle(unsigned long _musb)
52{
53 struct musb *musb = (void *)_musb;
54 unsigned long flags;
55 u8 power;
Felipe Balbi550a7372008-07-24 12:27:36 +030056 u8 devctl;
57
Felipe Balbi550a7372008-07-24 12:27:36 +030058 spin_lock_irqsave(&musb->lock, flags);
59
David Brownell84e250f2009-03-31 12:30:04 -070060 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +030061 case OTG_STATE_A_WAIT_BCON:
Felipe Balbi550a7372008-07-24 12:27:36 +030062
63 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
64 if (devctl & MUSB_DEVCTL_BDEVICE) {
David Brownell84e250f2009-03-31 12:30:04 -070065 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +030066 MUSB_DEV_MODE(musb);
67 } else {
David Brownell84e250f2009-03-31 12:30:04 -070068 musb->xceiv->state = OTG_STATE_A_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +030069 MUSB_HST_MODE(musb);
70 }
71 break;
Felipe Balbi550a7372008-07-24 12:27:36 +030072 case OTG_STATE_A_SUSPEND:
73 /* finish RESUME signaling? */
74 if (musb->port1_status & MUSB_PORT_STAT_RESUME) {
75 power = musb_readb(musb->mregs, MUSB_POWER);
76 power &= ~MUSB_POWER_RESUME;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +030077 dev_dbg(musb->controller, "root port resume stopped, power %02x\n", power);
Felipe Balbi550a7372008-07-24 12:27:36 +030078 musb_writeb(musb->mregs, MUSB_POWER, power);
79 musb->is_active = 1;
80 musb->port1_status &= ~(USB_PORT_STAT_SUSPEND
81 | MUSB_PORT_STAT_RESUME);
82 musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16;
83 usb_hcd_poll_rh_status(musb_to_hcd(musb));
84 /* NOTE: it might really be A_WAIT_BCON ... */
David Brownell84e250f2009-03-31 12:30:04 -070085 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +030086 }
87 break;
Felipe Balbi550a7372008-07-24 12:27:36 +030088 case OTG_STATE_A_HOST:
89 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
90 if (devctl & MUSB_DEVCTL_BDEVICE)
David Brownell84e250f2009-03-31 12:30:04 -070091 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +030092 else
David Brownell84e250f2009-03-31 12:30:04 -070093 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
Felipe Balbi550a7372008-07-24 12:27:36 +030094 default:
95 break;
96 }
97 spin_unlock_irqrestore(&musb->lock, flags);
98}
99
100
Felipe Balbi743411b2010-12-01 13:22:05 +0200101static void omap2430_musb_try_idle(struct musb *musb, unsigned long timeout)
Felipe Balbi550a7372008-07-24 12:27:36 +0300102{
103 unsigned long default_timeout = jiffies + msecs_to_jiffies(3);
104 static unsigned long last_timer;
105
106 if (timeout == 0)
107 timeout = default_timeout;
108
109 /* Never idle if active, or when VBUS timeout is not set as host */
110 if (musb->is_active || ((musb->a_wait_bcon == 0)
David Brownell84e250f2009-03-31 12:30:04 -0700111 && (musb->xceiv->state == OTG_STATE_A_WAIT_BCON))) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300112 dev_dbg(musb->controller, "%s active, deleting timer\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200113 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300114 del_timer(&musb_idle_timer);
115 last_timer = jiffies;
116 return;
117 }
118
119 if (time_after(last_timer, timeout)) {
120 if (!timer_pending(&musb_idle_timer))
121 last_timer = timeout;
122 else {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300123 dev_dbg(musb->controller, "Longer idle timer already pending, ignoring\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300124 return;
125 }
126 }
127 last_timer = timeout;
128
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300129 dev_dbg(musb->controller, "%s inactive, for idle timer for %lu ms\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200130 otg_state_string(musb->xceiv->state),
Felipe Balbi550a7372008-07-24 12:27:36 +0300131 (unsigned long)jiffies_to_msecs(timeout - jiffies));
132 mod_timer(&musb_idle_timer, timeout);
133}
134
Felipe Balbi743411b2010-12-01 13:22:05 +0200135static void omap2430_musb_set_vbus(struct musb *musb, int is_on)
Felipe Balbi550a7372008-07-24 12:27:36 +0300136{
Heikki Krogerusd445b6d2012-02-13 13:24:15 +0200137 struct usb_otg *otg = musb->xceiv->otg;
Felipe Balbi550a7372008-07-24 12:27:36 +0300138 u8 devctl;
Hema HK594632e2010-12-10 18:10:51 +0530139 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
140 int ret = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +0300141 /* HDRC controls CPEN, but beware current surges during device
142 * connect. They can trigger transient overcurrent conditions
143 * that must be ignored.
144 */
145
146 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
147
148 if (is_on) {
Hema HK594632e2010-12-10 18:10:51 +0530149 if (musb->xceiv->state == OTG_STATE_A_IDLE) {
150 /* start the session */
151 devctl |= MUSB_DEVCTL_SESSION;
152 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
153 /*
154 * Wait for the musb to set as A device to enable the
155 * VBUS
156 */
157 while (musb_readb(musb->mregs, MUSB_DEVCTL) & 0x80) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300158
Hema HK594632e2010-12-10 18:10:51 +0530159 cpu_relax();
160
161 if (time_after(jiffies, timeout)) {
162 dev_err(musb->controller,
163 "configured as A device timeout");
164 ret = -EINVAL;
165 break;
166 }
167 }
168
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200169 if (ret && otg->set_vbus)
170 otg_set_vbus(otg, 1);
Hema HK594632e2010-12-10 18:10:51 +0530171 } else {
172 musb->is_active = 1;
Heikki Krogerusd445b6d2012-02-13 13:24:15 +0200173 otg->default_a = 1;
Hema HK594632e2010-12-10 18:10:51 +0530174 musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
175 devctl |= MUSB_DEVCTL_SESSION;
176 MUSB_HST_MODE(musb);
177 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300178 } else {
179 musb->is_active = 0;
180
181 /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and
182 * jumping right to B_IDLE...
183 */
184
Heikki Krogerusd445b6d2012-02-13 13:24:15 +0200185 otg->default_a = 0;
David Brownell84e250f2009-03-31 12:30:04 -0700186 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300187 devctl &= ~MUSB_DEVCTL_SESSION;
188
189 MUSB_DEV_MODE(musb);
190 }
191 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
192
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300193 dev_dbg(musb->controller, "VBUS %s, devctl %02x "
Felipe Balbi550a7372008-07-24 12:27:36 +0300194 /* otg %3x conf %08x prcm %08x */ "\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200195 otg_state_string(musb->xceiv->state),
Felipe Balbi550a7372008-07-24 12:27:36 +0300196 musb_readb(musb->mregs, MUSB_DEVCTL));
197}
Felipe Balbi550a7372008-07-24 12:27:36 +0300198
Felipe Balbi743411b2010-12-01 13:22:05 +0200199static int omap2430_musb_set_mode(struct musb *musb, u8 musb_mode)
Felipe Balbi550a7372008-07-24 12:27:36 +0300200{
201 u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
202
203 devctl |= MUSB_DEVCTL_SESSION;
204 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
205
David Brownell96a274d2008-11-24 13:06:47 +0200206 return 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300207}
208
Felipe Balbic20aebb2010-12-02 12:44:40 +0200209static inline void omap2430_low_level_exit(struct musb *musb)
210{
211 u32 l;
212
213 /* in any role */
214 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
215 l |= ENABLEFORCE; /* enable MSTANDBY */
216 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
Felipe Balbic20aebb2010-12-02 12:44:40 +0200217}
218
219static inline void omap2430_low_level_init(struct musb *musb)
220{
221 u32 l;
222
Felipe Balbic20aebb2010-12-02 12:44:40 +0200223 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
224 l &= ~ENABLEFORCE; /* disable MSTANDBY */
225 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
226}
227
Hema HK594632e2010-12-10 18:10:51 +0530228static int musb_otg_notifications(struct notifier_block *nb,
229 unsigned long event, void *unused)
230{
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +0530231 struct musb *musb = container_of(nb, struct musb, nb);
232 struct device *dev = musb->controller;
233 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
Vikram Pandita712d8ef2011-08-12 07:38:51 -0700234
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +0530235 glue->xceiv_event = event;
236 schedule_work(&glue->omap_musb_mailbox_work);
Vikram Pandita712d8ef2011-08-12 07:38:51 -0700237
NeilBrownb30b3c62012-01-25 10:02:46 +0200238 return NOTIFY_OK;
Vikram Pandita712d8ef2011-08-12 07:38:51 -0700239}
240
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +0530241static void omap_musb_mailbox_work(struct work_struct *data_notifier_work)
Vikram Pandita712d8ef2011-08-12 07:38:51 -0700242{
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +0530243 struct omap2430_glue *glue = container_of(data_notifier_work,
244 struct omap2430_glue, omap_musb_mailbox_work);
245 struct musb *musb = glue_to_musb(glue);
Hema HK594632e2010-12-10 18:10:51 +0530246 struct device *dev = musb->controller;
247 struct musb_hdrc_platform_data *pdata = dev->platform_data;
248 struct omap_musb_board_data *data = pdata->board_data;
249
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +0530250 switch (glue->xceiv_event) {
Hema HK594632e2010-12-10 18:10:51 +0530251 case USB_EVENT_ID:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300252 dev_dbg(musb->controller, "ID GND\n");
Hema HK594632e2010-12-10 18:10:51 +0530253
Felipe Contrerasb3314d92011-12-19 22:17:49 +0200254 if (!is_otg_enabled(musb) || musb->gadget_driver) {
Hema HK7acc6192011-02-28 14:19:34 +0530255 pm_runtime_get_sync(musb->controller);
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200256 usb_phy_init(musb->xceiv);
Hema HK70045c52011-02-28 15:05:29 +0530257 omap2430_musb_set_vbus(musb, 1);
Hema HK594632e2010-12-10 18:10:51 +0530258 }
259 break;
260
261 case USB_EVENT_VBUS:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300262 dev_dbg(musb->controller, "VBUS Connect\n");
Hema HK594632e2010-12-10 18:10:51 +0530263
Hema HK7acc6192011-02-28 14:19:34 +0530264 if (musb->gadget_driver)
265 pm_runtime_get_sync(musb->controller);
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200266 usb_phy_init(musb->xceiv);
Hema HK594632e2010-12-10 18:10:51 +0530267 break;
268
269 case USB_EVENT_NONE:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300270 dev_dbg(musb->controller, "VBUS Disconnect\n");
Hema HK594632e2010-12-10 18:10:51 +0530271
Jarkko Nikula383cf4e2011-04-27 17:02:37 +0300272 if (is_otg_enabled(musb) || is_peripheral_enabled(musb))
Felipe Balbi62285962011-06-22 17:28:09 +0300273 if (musb->gadget_driver) {
Hema HK7acc6192011-02-28 14:19:34 +0530274 pm_runtime_mark_last_busy(musb->controller);
275 pm_runtime_put_autosuspend(musb->controller);
276 }
277
Hema HK594632e2010-12-10 18:10:51 +0530278 if (data->interface_type == MUSB_INTERFACE_UTMI) {
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200279 if (musb->xceiv->otg->set_vbus)
280 otg_set_vbus(musb->xceiv->otg, 0);
Hema HK594632e2010-12-10 18:10:51 +0530281 }
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200282 usb_phy_shutdown(musb->xceiv);
Hema HK594632e2010-12-10 18:10:51 +0530283 break;
284 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300285 dev_dbg(musb->controller, "ID float\n");
Hema HK594632e2010-12-10 18:10:51 +0530286 }
Hema HK594632e2010-12-10 18:10:51 +0530287}
288
Felipe Balbi743411b2010-12-01 13:22:05 +0200289static int omap2430_musb_init(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300290{
Shubhrajyoti Dad579692012-03-22 12:48:06 +0530291 u32 l;
292 int status = 0;
Hema Kalliguddiea65df52010-09-22 19:27:40 -0500293 struct device *dev = musb->controller;
294 struct musb_hdrc_platform_data *plat = dev->platform_data;
295 struct omap_musb_board_data *data = plat->board_data;
Felipe Balbi550a7372008-07-24 12:27:36 +0300296
David Brownell84e250f2009-03-31 12:30:04 -0700297 /* We require some kind of external transceiver, hooked
298 * up through ULPI. TWL4030-family PMICs include one,
299 * which needs a driver, drivers aren't always needed.
300 */
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530301 musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
David Brownell84e250f2009-03-31 12:30:04 -0700302 if (!musb->xceiv) {
303 pr_err("HS USB OTG: no transceiver configured\n");
304 return -ENODEV;
305 }
306
Hema HK7acc6192011-02-28 14:19:34 +0530307 status = pm_runtime_get_sync(dev);
308 if (status < 0) {
Shubhrajyoti Dad579692012-03-22 12:48:06 +0530309 dev_err(dev, "pm_runtime_get_sync FAILED %d\n", status);
Hema HK7acc6192011-02-28 14:19:34 +0530310 goto err1;
311 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300312
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200313 l = musb_readl(musb->mregs, OTG_INTERFSEL);
Maulik Mankadde2e1b02010-03-12 10:29:07 +0200314
315 if (data->interface_type == MUSB_INTERFACE_UTMI) {
316 /* OMAP4 uses Internal PHY GS70 which uses UTMI interface */
317 l &= ~ULPI_12PIN; /* Disable ULPI */
318 l |= UTMI_8BIT; /* Enable UTMI */
319 } else {
320 l |= ULPI_12PIN;
321 }
322
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200323 musb_writel(musb->mregs, OTG_INTERFSEL, l);
Felipe Balbi550a7372008-07-24 12:27:36 +0300324
325 pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
326 "sysstatus 0x%x, intrfsel 0x%x, simenable 0x%x\n",
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200327 musb_readl(musb->mregs, OTG_REVISION),
328 musb_readl(musb->mregs, OTG_SYSCONFIG),
329 musb_readl(musb->mregs, OTG_SYSSTATUS),
330 musb_readl(musb->mregs, OTG_INTERFSEL),
331 musb_readl(musb->mregs, OTG_SIMENABLE));
Felipe Balbi550a7372008-07-24 12:27:36 +0300332
Hema HK594632e2010-12-10 18:10:51 +0530333 musb->nb.notifier_call = musb_otg_notifications;
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200334 status = usb_register_notifier(musb->xceiv, &musb->nb);
Hema HK594632e2010-12-10 18:10:51 +0530335
336 if (status)
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300337 dev_dbg(musb->controller, "notification register failed\n");
Hema HK594632e2010-12-10 18:10:51 +0530338
Felipe Balbi550a7372008-07-24 12:27:36 +0300339 setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
340
Grazvydas Ignotasc04352a2012-02-04 19:43:51 +0200341 pm_runtime_put_noidle(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +0300342 return 0;
Hema HK7acc6192011-02-28 14:19:34 +0530343
344err1:
Hema HK7acc6192011-02-28 14:19:34 +0530345 return status;
Felipe Balbi550a7372008-07-24 12:27:36 +0300346}
347
Hema HK002eda12011-02-17 12:06:10 +0530348static void omap2430_musb_enable(struct musb *musb)
349{
350 u8 devctl;
351 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
352 struct device *dev = musb->controller;
353 struct musb_hdrc_platform_data *pdata = dev->platform_data;
354 struct omap_musb_board_data *data = pdata->board_data;
355
356 switch (musb->xceiv->last_event) {
357
358 case USB_EVENT_ID:
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200359 usb_phy_init(musb->xceiv);
Felipe Contreras08dec562011-12-19 22:17:50 +0200360 if (data->interface_type != MUSB_INTERFACE_UTMI)
361 break;
362 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
363 /* start the session */
364 devctl |= MUSB_DEVCTL_SESSION;
365 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
366 while (musb_readb(musb->mregs, MUSB_DEVCTL) &
367 MUSB_DEVCTL_BDEVICE) {
368 cpu_relax();
Hema HK002eda12011-02-17 12:06:10 +0530369
Felipe Contreras08dec562011-12-19 22:17:50 +0200370 if (time_after(jiffies, timeout)) {
371 dev_err(dev, "configured as A device timeout");
372 break;
Hema HK002eda12011-02-17 12:06:10 +0530373 }
374 }
375 break;
376
377 case USB_EVENT_VBUS:
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200378 usb_phy_init(musb->xceiv);
Hema HK002eda12011-02-17 12:06:10 +0530379 break;
380
381 default:
382 break;
383 }
384}
385
386static void omap2430_musb_disable(struct musb *musb)
387{
388 if (musb->xceiv->last_event)
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200389 usb_phy_shutdown(musb->xceiv);
Felipe Balbi550a7372008-07-24 12:27:36 +0300390}
391
Felipe Balbi743411b2010-12-01 13:22:05 +0200392static int omap2430_musb_exit(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300393{
Johan Hovold19b9a832011-02-11 16:57:08 +0100394 del_timer_sync(&musb_idle_timer);
Felipe Balbi550a7372008-07-24 12:27:36 +0300395
Felipe Balbic20aebb2010-12-02 12:44:40 +0200396 omap2430_low_level_exit(musb);
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530397 usb_put_phy(musb->xceiv);
Felipe Balbic20aebb2010-12-02 12:44:40 +0200398
Felipe Balbi550a7372008-07-24 12:27:36 +0300399 return 0;
400}
Felipe Balbi743411b2010-12-01 13:22:05 +0200401
Felipe Balbif7ec9432010-12-02 09:48:58 +0200402static const struct musb_platform_ops omap2430_ops = {
Felipe Balbi743411b2010-12-01 13:22:05 +0200403 .init = omap2430_musb_init,
404 .exit = omap2430_musb_exit,
405
Felipe Balbi743411b2010-12-01 13:22:05 +0200406 .set_mode = omap2430_musb_set_mode,
407 .try_idle = omap2430_musb_try_idle,
408
409 .set_vbus = omap2430_musb_set_vbus,
Hema HK002eda12011-02-17 12:06:10 +0530410
411 .enable = omap2430_musb_enable,
412 .disable = omap2430_musb_disable,
Felipe Balbi743411b2010-12-01 13:22:05 +0200413};
Felipe Balbidc098862010-12-01 15:01:11 +0200414
415static u64 omap2430_dmamask = DMA_BIT_MASK(32);
416
Felipe Balbie9e8c852012-01-26 12:40:23 +0200417static int __devinit omap2430_probe(struct platform_device *pdev)
Felipe Balbidc098862010-12-01 15:01:11 +0200418{
419 struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
420 struct platform_device *musb;
Felipe Balbia3cee122010-12-02 09:27:29 +0200421 struct omap2430_glue *glue;
Felipe Balbidc098862010-12-01 15:01:11 +0200422 int ret = -ENOMEM;
423
Felipe Balbia3cee122010-12-02 09:27:29 +0200424 glue = kzalloc(sizeof(*glue), GFP_KERNEL);
425 if (!glue) {
426 dev_err(&pdev->dev, "failed to allocate glue context\n");
427 goto err0;
428 }
429
Felipe Balbidc098862010-12-01 15:01:11 +0200430 musb = platform_device_alloc("musb-hdrc", -1);
431 if (!musb) {
432 dev_err(&pdev->dev, "failed to allocate musb device\n");
Felipe Balbia3cee122010-12-02 09:27:29 +0200433 goto err1;
Felipe Balbidc098862010-12-01 15:01:11 +0200434 }
435
436 musb->dev.parent = &pdev->dev;
437 musb->dev.dma_mask = &omap2430_dmamask;
438 musb->dev.coherent_dma_mask = omap2430_dmamask;
439
Felipe Balbia3cee122010-12-02 09:27:29 +0200440 glue->dev = &pdev->dev;
441 glue->musb = musb;
442
Felipe Balbif7ec9432010-12-02 09:48:58 +0200443 pdata->platform_ops = &omap2430_ops;
444
Felipe Balbia3cee122010-12-02 09:27:29 +0200445 platform_set_drvdata(pdev, glue);
Felipe Balbidc098862010-12-01 15:01:11 +0200446
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +0530447 INIT_WORK(&glue->omap_musb_mailbox_work, omap_musb_mailbox_work);
448
Felipe Balbidc098862010-12-01 15:01:11 +0200449 ret = platform_device_add_resources(musb, pdev->resource,
450 pdev->num_resources);
451 if (ret) {
452 dev_err(&pdev->dev, "failed to add resources\n");
Hema HK207b0e12011-02-17 12:07:22 +0530453 goto err2;
Felipe Balbidc098862010-12-01 15:01:11 +0200454 }
455
456 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
457 if (ret) {
458 dev_err(&pdev->dev, "failed to add platform_data\n");
Hema HK207b0e12011-02-17 12:07:22 +0530459 goto err2;
Felipe Balbidc098862010-12-01 15:01:11 +0200460 }
461
Kishon Vijay Abraham I3006dc82012-03-21 21:30:20 +0530462 pm_runtime_enable(&pdev->dev);
463
Felipe Balbidc098862010-12-01 15:01:11 +0200464 ret = platform_device_add(musb);
465 if (ret) {
466 dev_err(&pdev->dev, "failed to register musb device\n");
Hema HK207b0e12011-02-17 12:07:22 +0530467 goto err2;
Felipe Balbidc098862010-12-01 15:01:11 +0200468 }
469
470 return 0;
471
Felipe Balbia3cee122010-12-02 09:27:29 +0200472err2:
Felipe Balbidc098862010-12-01 15:01:11 +0200473 platform_device_put(musb);
474
Felipe Balbia3cee122010-12-02 09:27:29 +0200475err1:
476 kfree(glue);
477
Felipe Balbidc098862010-12-01 15:01:11 +0200478err0:
479 return ret;
480}
481
Felipe Balbie9e8c852012-01-26 12:40:23 +0200482static int __devexit omap2430_remove(struct platform_device *pdev)
Felipe Balbidc098862010-12-01 15:01:11 +0200483{
Felipe Balbia3cee122010-12-02 09:27:29 +0200484 struct omap2430_glue *glue = platform_get_drvdata(pdev);
Felipe Balbidc098862010-12-01 15:01:11 +0200485
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +0530486 cancel_work_sync(&glue->omap_musb_mailbox_work);
Felipe Balbia3cee122010-12-02 09:27:29 +0200487 platform_device_del(glue->musb);
488 platform_device_put(glue->musb);
489 kfree(glue);
Felipe Balbidc098862010-12-01 15:01:11 +0200490
491 return 0;
492}
493
Felipe Balbic20aebb2010-12-02 12:44:40 +0200494#ifdef CONFIG_PM
Felipe Balbic20aebb2010-12-02 12:44:40 +0200495
Hema HK7acc6192011-02-28 14:19:34 +0530496static int omap2430_runtime_suspend(struct device *dev)
Felipe Balbic20aebb2010-12-02 12:44:40 +0200497{
498 struct omap2430_glue *glue = dev_get_drvdata(dev);
499 struct musb *musb = glue_to_musb(glue);
500
Vladimir Zapolskiyafb76df2011-12-23 18:37:18 +0200501 if (musb) {
502 musb->context.otg_interfsel = musb_readl(musb->mregs,
503 OTG_INTERFSEL);
Hema HKe25bec12011-09-07 09:19:24 -0700504
Vladimir Zapolskiyafb76df2011-12-23 18:37:18 +0200505 omap2430_low_level_exit(musb);
506 usb_phy_set_suspend(musb->xceiv, 1);
507 }
Felipe Balbic20aebb2010-12-02 12:44:40 +0200508
509 return 0;
510}
511
Hema HK7acc6192011-02-28 14:19:34 +0530512static int omap2430_runtime_resume(struct device *dev)
Felipe Balbic20aebb2010-12-02 12:44:40 +0200513{
514 struct omap2430_glue *glue = dev_get_drvdata(dev);
515 struct musb *musb = glue_to_musb(glue);
Felipe Balbic20aebb2010-12-02 12:44:40 +0200516
Vladimir Zapolskiyafb76df2011-12-23 18:37:18 +0200517 if (musb) {
518 omap2430_low_level_init(musb);
519 musb_writel(musb->mregs, OTG_INTERFSEL,
520 musb->context.otg_interfsel);
Hema HKe25bec12011-09-07 09:19:24 -0700521
Vladimir Zapolskiyafb76df2011-12-23 18:37:18 +0200522 usb_phy_set_suspend(musb->xceiv, 0);
523 }
Felipe Balbic20aebb2010-12-02 12:44:40 +0200524
525 return 0;
526}
527
528static struct dev_pm_ops omap2430_pm_ops = {
Hema HK7acc6192011-02-28 14:19:34 +0530529 .runtime_suspend = omap2430_runtime_suspend,
530 .runtime_resume = omap2430_runtime_resume,
Felipe Balbic20aebb2010-12-02 12:44:40 +0200531};
532
533#define DEV_PM_OPS (&omap2430_pm_ops)
534#else
535#define DEV_PM_OPS NULL
536#endif
537
Felipe Balbidc098862010-12-01 15:01:11 +0200538static struct platform_driver omap2430_driver = {
Felipe Balbie9e8c852012-01-26 12:40:23 +0200539 .probe = omap2430_probe,
540 .remove = __devexit_p(omap2430_remove),
Felipe Balbidc098862010-12-01 15:01:11 +0200541 .driver = {
542 .name = "musb-omap2430",
Felipe Balbic20aebb2010-12-02 12:44:40 +0200543 .pm = DEV_PM_OPS,
Felipe Balbidc098862010-12-01 15:01:11 +0200544 },
545};
546
547MODULE_DESCRIPTION("OMAP2PLUS MUSB Glue Layer");
548MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
549MODULE_LICENSE("GPL v2");
550
551static int __init omap2430_init(void)
552{
Felipe Balbie9e8c852012-01-26 12:40:23 +0200553 return platform_driver_register(&omap2430_driver);
Felipe Balbidc098862010-12-01 15:01:11 +0200554}
Felipe Balbie9e8c852012-01-26 12:40:23 +0200555module_init(omap2430_init);
Felipe Balbidc098862010-12-01 15:01:11 +0200556
557static void __exit omap2430_exit(void)
558{
559 platform_driver_unregister(&omap2430_driver);
560}
561module_exit(omap2430_exit);