]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/usb/musb/omap2430.c
usb: musb: Handle cable status better for 2430 glue layer
[mirror_ubuntu-bionic-kernel.git] / drivers / usb / musb / omap2430.c
CommitLineData
550a7375
FB
1/*
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
550a7375
FB
6 * 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>
550a7375
FB
30#include <linux/init.h>
31#include <linux/list.h>
550a7375 32#include <linux/io.h>
00a0b1d5 33#include <linux/of.h>
dc09886b
FB
34#include <linux/platform_device.h>
35#include <linux/dma-mapping.h>
207b0e1f
HH
36#include <linux/pm_runtime.h>
37#include <linux/err.h>
12a19b5f 38#include <linux/delay.h>
8055555f 39#include <linux/usb/musb.h>
14da699b 40#include <linux/phy/omap_control_phy.h>
8934d3e4 41#include <linux/of_platform.h>
550a7375 42
550a7375
FB
43#include "musb_core.h"
44#include "omap2430.h"
45
a3cee12a
FB
46struct omap2430_glue {
47 struct device *dev;
48 struct platform_device *musb;
8055555f 49 enum musb_vbus_id_status status;
1e5acb8d 50 struct work_struct omap_musb_mailbox_work;
ca784be3 51 struct device *control_otghs;
21f77bee
TL
52 bool cable_connected;
53 bool enabled;
54 bool powered;
a3cee12a 55};
c20aebb9 56#define glue_to_musb(g) platform_get_drvdata(g->musb)
a3cee12a 57
4b58ed11 58static struct omap2430_glue *_glue;
c9721438 59
550a7375
FB
60static struct timer_list musb_idle_timer;
61
62static void musb_do_idle(unsigned long _musb)
63{
64 struct musb *musb = (void *)_musb;
65 unsigned long flags;
66 u8 power;
67 u8 devctl;
68
550a7375
FB
69 spin_lock_irqsave(&musb->lock, flags);
70
e47d9254 71 switch (musb->xceiv->otg->state) {
550a7375 72 case OTG_STATE_A_WAIT_BCON:
550a7375
FB
73
74 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
75 if (devctl & MUSB_DEVCTL_BDEVICE) {
e47d9254 76 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
550a7375
FB
77 MUSB_DEV_MODE(musb);
78 } else {
e47d9254 79 musb->xceiv->otg->state = OTG_STATE_A_IDLE;
550a7375
FB
80 MUSB_HST_MODE(musb);
81 }
82 break;
550a7375
FB
83 case OTG_STATE_A_SUSPEND:
84 /* finish RESUME signaling? */
85 if (musb->port1_status & MUSB_PORT_STAT_RESUME) {
86 power = musb_readb(musb->mregs, MUSB_POWER);
87 power &= ~MUSB_POWER_RESUME;
5c8a86e1 88 dev_dbg(musb->controller, "root port resume stopped, power %02x\n", power);
550a7375
FB
89 musb_writeb(musb->mregs, MUSB_POWER, power);
90 musb->is_active = 1;
91 musb->port1_status &= ~(USB_PORT_STAT_SUSPEND
92 | MUSB_PORT_STAT_RESUME);
93 musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16;
8b125df5 94 usb_hcd_poll_rh_status(musb->hcd);
550a7375 95 /* NOTE: it might really be A_WAIT_BCON ... */
e47d9254 96 musb->xceiv->otg->state = OTG_STATE_A_HOST;
550a7375
FB
97 }
98 break;
550a7375
FB
99 case OTG_STATE_A_HOST:
100 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
101 if (devctl & MUSB_DEVCTL_BDEVICE)
e47d9254 102 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
550a7375 103 else
e47d9254 104 musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON;
550a7375
FB
105 default:
106 break;
107 }
108 spin_unlock_irqrestore(&musb->lock, flags);
109}
110
111
743411b3 112static void omap2430_musb_try_idle(struct musb *musb, unsigned long timeout)
550a7375
FB
113{
114 unsigned long default_timeout = jiffies + msecs_to_jiffies(3);
115 static unsigned long last_timer;
116
117 if (timeout == 0)
118 timeout = default_timeout;
119
120 /* Never idle if active, or when VBUS timeout is not set as host */
121 if (musb->is_active || ((musb->a_wait_bcon == 0)
e47d9254 122 && (musb->xceiv->otg->state == OTG_STATE_A_WAIT_BCON))) {
5c8a86e1 123 dev_dbg(musb->controller, "%s active, deleting timer\n",
e47d9254 124 usb_otg_state_string(musb->xceiv->otg->state));
550a7375
FB
125 del_timer(&musb_idle_timer);
126 last_timer = jiffies;
127 return;
128 }
129
130 if (time_after(last_timer, timeout)) {
131 if (!timer_pending(&musb_idle_timer))
132 last_timer = timeout;
133 else {
5c8a86e1 134 dev_dbg(musb->controller, "Longer idle timer already pending, ignoring\n");
550a7375
FB
135 return;
136 }
137 }
138 last_timer = timeout;
139
5c8a86e1 140 dev_dbg(musb->controller, "%s inactive, for idle timer for %lu ms\n",
e47d9254 141 usb_otg_state_string(musb->xceiv->otg->state),
550a7375
FB
142 (unsigned long)jiffies_to_msecs(timeout - jiffies));
143 mod_timer(&musb_idle_timer, timeout);
144}
145
743411b3 146static void omap2430_musb_set_vbus(struct musb *musb, int is_on)
550a7375 147{
d445b6da 148 struct usb_otg *otg = musb->xceiv->otg;
550a7375 149 u8 devctl;
594632ef 150 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
550a7375
FB
151 /* HDRC controls CPEN, but beware current surges during device
152 * connect. They can trigger transient overcurrent conditions
153 * that must be ignored.
154 */
155
156 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
157
158 if (is_on) {
e47d9254 159 if (musb->xceiv->otg->state == OTG_STATE_A_IDLE) {
12a19b5f 160 int loops = 100;
594632ef
HH
161 /* start the session */
162 devctl |= MUSB_DEVCTL_SESSION;
163 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
164 /*
165 * Wait for the musb to set as A device to enable the
166 * VBUS
167 */
5a805309
SS
168 while (musb_readb(musb->mregs, MUSB_DEVCTL) &
169 MUSB_DEVCTL_BDEVICE) {
594632ef 170
12a19b5f 171 mdelay(5);
594632ef
HH
172 cpu_relax();
173
12a19b5f
N
174 if (time_after(jiffies, timeout)
175 || loops-- <= 0) {
594632ef
HH
176 dev_err(musb->controller,
177 "configured as A device timeout");
594632ef
HH
178 break;
179 }
180 }
181
bb467cf5 182 otg_set_vbus(otg, 1);
594632ef
HH
183 } else {
184 musb->is_active = 1;
d445b6da 185 otg->default_a = 1;
e47d9254 186 musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
594632ef
HH
187 devctl |= MUSB_DEVCTL_SESSION;
188 MUSB_HST_MODE(musb);
189 }
550a7375
FB
190 } else {
191 musb->is_active = 0;
192
193 /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and
194 * jumping right to B_IDLE...
195 */
196
d445b6da 197 otg->default_a = 0;
e47d9254 198 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
550a7375
FB
199 devctl &= ~MUSB_DEVCTL_SESSION;
200
201 MUSB_DEV_MODE(musb);
202 }
203 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
204
5c8a86e1 205 dev_dbg(musb->controller, "VBUS %s, devctl %02x "
550a7375 206 /* otg %3x conf %08x prcm %08x */ "\n",
e47d9254 207 usb_otg_state_string(musb->xceiv->otg->state),
550a7375
FB
208 musb_readb(musb->mregs, MUSB_DEVCTL));
209}
550a7375 210
743411b3 211static int omap2430_musb_set_mode(struct musb *musb, u8 musb_mode)
550a7375
FB
212{
213 u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
214
215 devctl |= MUSB_DEVCTL_SESSION;
216 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
217
96a274d1 218 return 0;
550a7375
FB
219}
220
c20aebb9
FB
221static inline void omap2430_low_level_exit(struct musb *musb)
222{
223 u32 l;
224
225 /* in any role */
226 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
227 l |= ENABLEFORCE; /* enable MSTANDBY */
228 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
c20aebb9
FB
229}
230
231static inline void omap2430_low_level_init(struct musb *musb)
232{
233 u32 l;
234
c20aebb9
FB
235 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
236 l &= ~ENABLEFORCE; /* disable MSTANDBY */
237 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
238}
239
21f77bee
TL
240/*
241 * We can get multiple cable events so we need to keep track
242 * of the power state. Only keep power enabled if USB cable is
243 * connected and a gadget is started.
244 */
245static void omap2430_set_power(struct musb *musb, bool enabled, bool cable)
246{
247 struct device *dev = musb->controller;
248 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
249 bool power_up;
250 int res;
251
252 if (glue->enabled != enabled)
253 glue->enabled = enabled;
254
255 if (glue->cable_connected != cable)
256 glue->cable_connected = cable;
257
258 power_up = glue->enabled && glue->cable_connected;
259 if (power_up == glue->powered) {
260 dev_warn(musb->controller, "power state already %i\n",
261 power_up);
262 return;
263 }
264
265 glue->powered = power_up;
266
267 if (power_up) {
268 res = pm_runtime_get_sync(musb->controller);
269 if (res < 0) {
270 dev_err(musb->controller, "could not enable: %i", res);
271 glue->powered = false;
272 }
273 } else {
274 pm_runtime_mark_last_busy(musb->controller);
275 pm_runtime_put_autosuspend(musb->controller);
276 }
277}
278
8055555f 279static void omap2430_musb_mailbox(enum musb_vbus_id_status status)
594632ef 280{
c9721438 281 struct omap2430_glue *glue = _glue;
712d8efa 282
f8c4b0e7
AK
283 if (!glue) {
284 pr_err("%s: musb core is not yet initialized\n", __func__);
285 return;
286 }
287 glue->status = status;
288
289 if (!glue_to_musb(glue)) {
80ab72e1 290 pr_err("%s: musb core is not yet ready\n", __func__);
c9721438
KVA
291 return;
292 }
712d8efa 293
c9721438 294 schedule_work(&glue->omap_musb_mailbox_work);
712d8efa
VP
295}
296
c9721438 297static void omap_musb_set_mailbox(struct omap2430_glue *glue)
712d8efa 298{
1e5acb8d 299 struct musb *musb = glue_to_musb(glue);
594632ef 300 struct device *dev = musb->controller;
c1a7d67c 301 struct musb_hdrc_platform_data *pdata = dev_get_platdata(dev);
594632ef 302 struct omap_musb_board_data *data = pdata->board_data;
c83a8542 303 struct usb_otg *otg = musb->xceiv->otg;
21f77bee
TL
304 bool cable_connected;
305
306 cable_connected = ((glue->status == MUSB_ID_GROUND) ||
307 (glue->status == MUSB_VBUS_VALID));
308
309 if (cable_connected)
310 omap2430_set_power(musb, glue->enabled, cable_connected);
594632ef 311
c9721438 312 switch (glue->status) {
8055555f 313 case MUSB_ID_GROUND:
c9721438 314 dev_dbg(dev, "ID GND\n");
594632ef 315
c83a8542 316 otg->default_a = true;
e47d9254 317 musb->xceiv->otg->state = OTG_STATE_A_IDLE;
c9721438 318 musb->xceiv->last_event = USB_EVENT_ID;
032ec49f 319 if (musb->gadget_driver) {
ca784be3
KVA
320 omap_control_usb_set_mode(glue->control_otghs,
321 USB_MODE_HOST);
70045c57 322 omap2430_musb_set_vbus(musb, 1);
594632ef
HH
323 }
324 break;
325
8055555f 326 case MUSB_VBUS_VALID:
c9721438 327 dev_dbg(dev, "VBUS Connect\n");
594632ef 328
c83a8542 329 otg->default_a = false;
e47d9254 330 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
c9721438 331 musb->xceiv->last_event = USB_EVENT_VBUS;
ca784be3 332 omap_control_usb_set_mode(glue->control_otghs, USB_MODE_DEVICE);
594632ef
HH
333 break;
334
8055555f
TL
335 case MUSB_ID_FLOAT:
336 case MUSB_VBUS_OFF:
c9721438 337 dev_dbg(dev, "VBUS Disconnect\n");
594632ef 338
c9721438 339 musb->xceiv->last_event = USB_EVENT_NONE;
21f77bee 340 if (musb->gadget_driver)
2c1fe89d 341 omap2430_musb_set_vbus(musb, 0);
7acc6197 342
bb467cf5
KVA
343 if (data->interface_type == MUSB_INTERFACE_UTMI)
344 otg_set_vbus(musb->xceiv->otg, 0);
345
ca784be3
KVA
346 omap_control_usb_set_mode(glue->control_otghs,
347 USB_MODE_DISCONNECT);
594632ef
HH
348 break;
349 default:
c9721438 350 dev_dbg(dev, "ID float\n");
594632ef 351 }
6fa7178c 352
21f77bee
TL
353 if (!cable_connected)
354 omap2430_set_power(musb, glue->enabled, cable_connected);
355
6fa7178c
PR
356 atomic_notifier_call_chain(&musb->xceiv->notifier,
357 musb->xceiv->last_event, NULL);
594632ef
HH
358}
359
c9721438
KVA
360
361static void omap_musb_mailbox_work(struct work_struct *mailbox_work)
362{
363 struct omap2430_glue *glue = container_of(mailbox_work,
364 struct omap2430_glue, omap_musb_mailbox_work);
8b2bc2c9
FB
365 struct musb *musb = glue_to_musb(glue);
366 struct device *dev = musb->controller;
367
368 pm_runtime_get_sync(dev);
c9721438 369 omap_musb_set_mailbox(glue);
8b2bc2c9
FB
370 pm_runtime_mark_last_busy(dev);
371 pm_runtime_put_autosuspend(dev);
c9721438
KVA
372}
373
baef653a
PDS
374static irqreturn_t omap2430_musb_interrupt(int irq, void *__hci)
375{
376 unsigned long flags;
377 irqreturn_t retval = IRQ_NONE;
378 struct musb *musb = __hci;
379
380 spin_lock_irqsave(&musb->lock, flags);
381
382 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
383 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
384 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
385
386 if (musb->int_usb || musb->int_tx || musb->int_rx)
387 retval = musb_interrupt(musb);
388
389 spin_unlock_irqrestore(&musb->lock, flags);
390
391 return retval;
392}
393
743411b3 394static int omap2430_musb_init(struct musb *musb)
550a7375 395{
ad579699
S
396 u32 l;
397 int status = 0;
ea65df57 398 struct device *dev = musb->controller;
c9721438 399 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
c1a7d67c 400 struct musb_hdrc_platform_data *plat = dev_get_platdata(dev);
ea65df57 401 struct omap_musb_board_data *data = plat->board_data;
550a7375 402
84e250ff
DB
403 /* We require some kind of external transceiver, hooked
404 * up through ULPI. TWL4030-family PMICs include one,
405 * which needs a driver, drivers aren't always needed.
406 */
3e3101d5
KVA
407 if (dev->parent->of_node) {
408 musb->phy = devm_phy_get(dev->parent, "usb2-phy");
409
410 /* We can't totally remove musb->xceiv as of now because
411 * musb core uses xceiv.state and xceiv.otg. Once we have
412 * a separate state machine to handle otg, these can be moved
413 * out of xceiv and then we can start using the generic PHY
414 * framework
415 */
b16604f2
KVA
416 musb->xceiv = devm_usb_get_phy_by_phandle(dev->parent,
417 "usb-phy", 0);
3e3101d5 418 } else {
b16604f2 419 musb->xceiv = devm_usb_get_phy_dev(dev, 0);
3e3101d5
KVA
420 musb->phy = devm_phy_get(dev, "usb");
421 }
b16604f2 422
a90199bb
FB
423 if (IS_ERR(musb->xceiv)) {
424 status = PTR_ERR(musb->xceiv);
425
426 if (status == -ENXIO)
427 return status;
428
84e250ff 429 pr_err("HS USB OTG: no transceiver configured\n");
25736e0c 430 return -EPROBE_DEFER;
84e250ff
DB
431 }
432
3e3101d5
KVA
433 if (IS_ERR(musb->phy)) {
434 pr_err("HS USB OTG: no PHY configured\n");
435 return PTR_ERR(musb->phy);
436 }
baef653a
PDS
437 musb->isr = omap2430_musb_interrupt;
438
8f2279d5
TL
439 /*
440 * Enable runtime PM for musb parent (this driver). We can't
441 * do it earlier as struct musb is not yet allocated and we
442 * need to touch the musb registers for runtime PM.
443 */
444 pm_runtime_enable(glue->dev);
445 status = pm_runtime_get_sync(glue->dev);
446 if (status < 0)
447 goto err1;
448
8573e6a6 449 l = musb_readl(musb->mregs, OTG_INTERFSEL);
de2e1b0c
MM
450
451 if (data->interface_type == MUSB_INTERFACE_UTMI) {
452 /* OMAP4 uses Internal PHY GS70 which uses UTMI interface */
453 l &= ~ULPI_12PIN; /* Disable ULPI */
454 l |= UTMI_8BIT; /* Enable UTMI */
455 } else {
456 l |= ULPI_12PIN;
457 }
458
8573e6a6 459 musb_writel(musb->mregs, OTG_INTERFSEL, l);
550a7375
FB
460
461 pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
462 "sysstatus 0x%x, intrfsel 0x%x, simenable 0x%x\n",
8573e6a6
FB
463 musb_readl(musb->mregs, OTG_REVISION),
464 musb_readl(musb->mregs, OTG_SYSCONFIG),
465 musb_readl(musb->mregs, OTG_SYSSTATUS),
466 musb_readl(musb->mregs, OTG_INTERFSEL),
467 musb_readl(musb->mregs, OTG_SIMENABLE));
550a7375 468
550a7375
FB
469 setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
470
8055555f 471 if (glue->status != MUSB_UNKNOWN)
c9721438
KVA
472 omap_musb_set_mailbox(glue);
473
3e3101d5 474 phy_init(musb->phy);
3063a12b 475 phy_power_on(musb->phy);
7099dbc5 476 pm_runtime_put(glue->dev);
550a7375 477 return 0;
7acc6197
HH
478
479err1:
7acc6197 480 return status;
550a7375
FB
481}
482
002eda13
HH
483static void omap2430_musb_enable(struct musb *musb)
484{
485 u8 devctl;
486 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
487 struct device *dev = musb->controller;
c9721438 488 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
c1a7d67c 489 struct musb_hdrc_platform_data *pdata = dev_get_platdata(dev);
002eda13
HH
490 struct omap_musb_board_data *data = pdata->board_data;
491
21f77bee
TL
492 omap2430_set_power(musb, true, glue->cable_connected);
493
c9721438 494 switch (glue->status) {
002eda13 495
8055555f 496 case MUSB_ID_GROUND:
ca784be3 497 omap_control_usb_set_mode(glue->control_otghs, USB_MODE_HOST);
08dec56e
FC
498 if (data->interface_type != MUSB_INTERFACE_UTMI)
499 break;
500 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
501 /* start the session */
502 devctl |= MUSB_DEVCTL_SESSION;
503 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
504 while (musb_readb(musb->mregs, MUSB_DEVCTL) &
505 MUSB_DEVCTL_BDEVICE) {
506 cpu_relax();
507
508 if (time_after(jiffies, timeout)) {
509 dev_err(dev, "configured as A device timeout");
510 break;
002eda13
HH
511 }
512 }
513 break;
514
8055555f 515 case MUSB_VBUS_VALID:
ca784be3 516 omap_control_usb_set_mode(glue->control_otghs, USB_MODE_DEVICE);
002eda13
HH
517 break;
518
519 default:
520 break;
521 }
522}
523
524static void omap2430_musb_disable(struct musb *musb)
525{
c9721438
KVA
526 struct device *dev = musb->controller;
527 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
528
8055555f 529 if (glue->status != MUSB_UNKNOWN)
ca784be3
KVA
530 omap_control_usb_set_mode(glue->control_otghs,
531 USB_MODE_DISCONNECT);
21f77bee
TL
532
533 omap2430_set_power(musb, false, glue->cable_connected);
550a7375
FB
534}
535
743411b3 536static int omap2430_musb_exit(struct musb *musb)
550a7375 537{
19b9a83e 538 del_timer_sync(&musb_idle_timer);
550a7375 539
c20aebb9 540 omap2430_low_level_exit(musb);
3063a12b 541 phy_power_off(musb->phy);
3e3101d5 542 phy_exit(musb->phy);
c20aebb9 543
550a7375
FB
544 return 0;
545}
743411b3 546
f7ec9437 547static const struct musb_platform_ops omap2430_ops = {
f8e9f34f 548 .quirks = MUSB_DMA_INVENTRA,
7f6283ed
TL
549#ifdef CONFIG_USB_INVENTRA_DMA
550 .dma_init = musbhs_dma_controller_create,
551 .dma_exit = musbhs_dma_controller_destroy,
552#endif
743411b3
FB
553 .init = omap2430_musb_init,
554 .exit = omap2430_musb_exit,
555
743411b3
FB
556 .set_mode = omap2430_musb_set_mode,
557 .try_idle = omap2430_musb_try_idle,
558
559 .set_vbus = omap2430_musb_set_vbus,
002eda13
HH
560
561 .enable = omap2430_musb_enable,
562 .disable = omap2430_musb_disable,
8055555f
TL
563
564 .phy_callback = omap2430_musb_mailbox,
743411b3 565};
dc09886b
FB
566
567static u64 omap2430_dmamask = DMA_BIT_MASK(32);
568
41ac7b3a 569static int omap2430_probe(struct platform_device *pdev)
dc09886b 570{
c1f01be4 571 struct resource musb_resources[3];
c1a7d67c 572 struct musb_hdrc_platform_data *pdata = dev_get_platdata(&pdev->dev);
00a0b1d5 573 struct omap_musb_board_data *data;
dc09886b 574 struct platform_device *musb;
a3cee12a 575 struct omap2430_glue *glue;
00a0b1d5
KVA
576 struct device_node *np = pdev->dev.of_node;
577 struct musb_hdrc_config *config;
606bf4d5 578 int ret = -ENOMEM, val;
dc09886b 579
b1183c24 580 glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
af1bdfc9 581 if (!glue)
a3cee12a 582 goto err0;
a3cee12a 583
2f771164 584 musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
dc09886b
FB
585 if (!musb) {
586 dev_err(&pdev->dev, "failed to allocate musb device\n");
2f771164 587 goto err0;
dc09886b
FB
588 }
589
590 musb->dev.parent = &pdev->dev;
591 musb->dev.dma_mask = &omap2430_dmamask;
592 musb->dev.coherent_dma_mask = omap2430_dmamask;
593
a3cee12a
FB
594 glue->dev = &pdev->dev;
595 glue->musb = musb;
8055555f 596 glue->status = MUSB_UNKNOWN;
8934d3e4 597 glue->control_otghs = ERR_PTR(-ENODEV);
a3cee12a 598
00a0b1d5 599 if (np) {
8934d3e4
RQ
600 struct device_node *control_node;
601 struct platform_device *control_pdev;
602
00a0b1d5 603 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
af1bdfc9 604 if (!pdata)
2f771164 605 goto err2;
00a0b1d5
KVA
606
607 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
af1bdfc9 608 if (!data)
2f771164 609 goto err2;
00a0b1d5
KVA
610
611 config = devm_kzalloc(&pdev->dev, sizeof(*config), GFP_KERNEL);
af1bdfc9 612 if (!config)
2f771164 613 goto err2;
00a0b1d5
KVA
614
615 of_property_read_u32(np, "mode", (u32 *)&pdata->mode);
eee44da0 616 of_property_read_u32(np, "interface-type",
00a0b1d5 617 (u32 *)&data->interface_type);
eee44da0
KVA
618 of_property_read_u32(np, "num-eps", (u32 *)&config->num_eps);
619 of_property_read_u32(np, "ram-bits", (u32 *)&config->ram_bits);
00a0b1d5 620 of_property_read_u32(np, "power", (u32 *)&pdata->power);
606bf4d5
TL
621
622 ret = of_property_read_u32(np, "multipoint", &val);
623 if (!ret && val)
624 config->multipoint = true;
00a0b1d5
KVA
625
626 pdata->board_data = data;
627 pdata->config = config;
00a0b1d5 628
8934d3e4
RQ
629 control_node = of_parse_phandle(np, "ctrl-module", 0);
630 if (control_node) {
631 control_pdev = of_find_device_by_node(control_node);
632 if (!control_pdev) {
633 dev_err(&pdev->dev, "Failed to get control device\n");
634 ret = -EINVAL;
635 goto err2;
636 }
637 glue->control_otghs = &control_pdev->dev;
ca784be3 638 }
ca784be3 639 }
f7ec9437
FB
640 pdata->platform_ops = &omap2430_ops;
641
a3cee12a 642 platform_set_drvdata(pdev, glue);
dc09886b 643
c9721438
KVA
644 /*
645 * REVISIT if we ever have two instances of the wrapper, we will be
646 * in big trouble
647 */
648 _glue = glue;
649
1e5acb8d
KVA
650 INIT_WORK(&glue->omap_musb_mailbox_work, omap_musb_mailbox_work);
651
3a0ddc71 652 memset(musb_resources, 0x00, sizeof(*musb_resources) *
09fc7d22
FB
653 ARRAY_SIZE(musb_resources));
654
655 musb_resources[0].name = pdev->resource[0].name;
656 musb_resources[0].start = pdev->resource[0].start;
657 musb_resources[0].end = pdev->resource[0].end;
658 musb_resources[0].flags = pdev->resource[0].flags;
659
660 musb_resources[1].name = pdev->resource[1].name;
661 musb_resources[1].start = pdev->resource[1].start;
662 musb_resources[1].end = pdev->resource[1].end;
663 musb_resources[1].flags = pdev->resource[1].flags;
664
c1f01be4
KVA
665 musb_resources[2].name = pdev->resource[2].name;
666 musb_resources[2].start = pdev->resource[2].start;
667 musb_resources[2].end = pdev->resource[2].end;
668 musb_resources[2].flags = pdev->resource[2].flags;
669
09fc7d22
FB
670 ret = platform_device_add_resources(musb, musb_resources,
671 ARRAY_SIZE(musb_resources));
dc09886b
FB
672 if (ret) {
673 dev_err(&pdev->dev, "failed to add resources\n");
65b3d52d 674 goto err2;
dc09886b
FB
675 }
676
677 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
678 if (ret) {
679 dev_err(&pdev->dev, "failed to add platform_data\n");
65b3d52d 680 goto err2;
dc09886b
FB
681 }
682
8f2279d5
TL
683 /*
684 * Note that we cannot enable PM runtime yet for this
685 * driver as we need struct musb initialized first.
686 * See omap2430_musb_init above.
687 */
3006dc8c 688
dc09886b
FB
689 ret = platform_device_add(musb);
690 if (ret) {
691 dev_err(&pdev->dev, "failed to register musb device\n");
65b3d52d 692 goto err2;
dc09886b
FB
693 }
694
207b0e1f 695 return 0;
03491761 696
65b3d52d 697err2:
b1183c24 698 platform_device_put(musb);
a3cee12a 699
dc09886b
FB
700err0:
701 return ret;
702}
703
fb4e98ab 704static int omap2430_remove(struct platform_device *pdev)
dc09886b 705{
21f77bee
TL
706 struct omap2430_glue *glue = platform_get_drvdata(pdev);
707 struct musb *musb = glue_to_musb(glue);
dc09886b 708
03e43528 709 pm_runtime_get_sync(glue->dev);
1e5acb8d 710 cancel_work_sync(&glue->omap_musb_mailbox_work);
f039df58 711 platform_device_unregister(glue->musb);
21f77bee 712 omap2430_set_power(musb, false, false);
03e43528
TL
713 pm_runtime_put_sync(glue->dev);
714 pm_runtime_disable(glue->dev);
dc09886b
FB
715
716 return 0;
717}
718
c20aebb9 719#ifdef CONFIG_PM
c20aebb9 720
7acc6197 721static int omap2430_runtime_suspend(struct device *dev)
c20aebb9
FB
722{
723 struct omap2430_glue *glue = dev_get_drvdata(dev);
724 struct musb *musb = glue_to_musb(glue);
725
afb76df1
VZ
726 if (musb) {
727 musb->context.otg_interfsel = musb_readl(musb->mregs,
728 OTG_INTERFSEL);
e25bec16 729
afb76df1 730 omap2430_low_level_exit(musb);
afb76df1 731 }
c20aebb9
FB
732
733 return 0;
734}
735
7acc6197 736static int omap2430_runtime_resume(struct device *dev)
c20aebb9
FB
737{
738 struct omap2430_glue *glue = dev_get_drvdata(dev);
739 struct musb *musb = glue_to_musb(glue);
c20aebb9 740
8f2279d5
TL
741 if (!musb)
742 return -EPROBE_DEFER;
743
744 omap2430_low_level_init(musb);
745 musb_writel(musb->mregs, OTG_INTERFSEL,
746 musb->context.otg_interfsel);
c20aebb9
FB
747
748 return 0;
749}
750
751static struct dev_pm_ops omap2430_pm_ops = {
7acc6197
HH
752 .runtime_suspend = omap2430_runtime_suspend,
753 .runtime_resume = omap2430_runtime_resume,
c20aebb9
FB
754};
755
756#define DEV_PM_OPS (&omap2430_pm_ops)
757#else
758#define DEV_PM_OPS NULL
759#endif
760
00a0b1d5
KVA
761#ifdef CONFIG_OF
762static const struct of_device_id omap2430_id_table[] = {
763 {
764 .compatible = "ti,omap4-musb"
765 },
766 {
767 .compatible = "ti,omap3-musb"
768 },
769 {},
770};
771MODULE_DEVICE_TABLE(of, omap2430_id_table);
772#endif
773
dc09886b 774static struct platform_driver omap2430_driver = {
e9e8c85e 775 .probe = omap2430_probe,
7690417d 776 .remove = omap2430_remove,
dc09886b
FB
777 .driver = {
778 .name = "musb-omap2430",
c20aebb9 779 .pm = DEV_PM_OPS,
00a0b1d5 780 .of_match_table = of_match_ptr(omap2430_id_table),
dc09886b
FB
781 },
782};
783
784MODULE_DESCRIPTION("OMAP2PLUS MUSB Glue Layer");
785MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
786MODULE_LICENSE("GPL v2");
787
788static int __init omap2430_init(void)
789{
e9e8c85e 790 return platform_driver_register(&omap2430_driver);
dc09886b 791}
c9721438 792subsys_initcall(omap2430_init);
dc09886b
FB
793
794static void __exit omap2430_exit(void)
795{
796 platform_driver_unregister(&omap2430_driver);
797}
798module_exit(omap2430_exit);