]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/usb/musb/omap2430.c
fa9932910969fe1538040fec1efd1e96455f6084
[linux.git] / drivers / usb / musb / omap2430.c
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
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>
30 #include <linux/init.h>
31 #include <linux/list.h>
32 #include <linux/io.h>
33 #include <linux/of.h>
34 #include <linux/platform_device.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/err.h>
38 #include <linux/delay.h>
39 #include <linux/usb/musb.h>
40 #include <linux/phy/omap_control_phy.h>
41 #include <linux/of_platform.h>
42
43 #include "musb_core.h"
44 #include "omap2430.h"
45
46 struct omap2430_glue {
47         struct device           *dev;
48         struct platform_device  *musb;
49         enum musb_vbus_id_status status;
50         struct work_struct      omap_musb_mailbox_work;
51         struct device           *control_otghs;
52         bool                    cable_connected;
53         bool                    enabled;
54         bool                    powered;
55 };
56 #define glue_to_musb(g)         platform_get_drvdata(g->musb)
57
58 static struct omap2430_glue     *_glue;
59
60 static struct timer_list musb_idle_timer;
61
62 static 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
69         spin_lock_irqsave(&musb->lock, flags);
70
71         switch (musb->xceiv->otg->state) {
72         case OTG_STATE_A_WAIT_BCON:
73
74                 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
75                 if (devctl & MUSB_DEVCTL_BDEVICE) {
76                         musb->xceiv->otg->state = OTG_STATE_B_IDLE;
77                         MUSB_DEV_MODE(musb);
78                 } else {
79                         musb->xceiv->otg->state = OTG_STATE_A_IDLE;
80                         MUSB_HST_MODE(musb);
81                 }
82                 break;
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;
88                         dev_dbg(musb->controller, "root port resume stopped, power %02x\n", power);
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;
94                         usb_hcd_poll_rh_status(musb->hcd);
95                         /* NOTE: it might really be A_WAIT_BCON ... */
96                         musb->xceiv->otg->state = OTG_STATE_A_HOST;
97                 }
98                 break;
99         case OTG_STATE_A_HOST:
100                 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
101                 if (devctl &  MUSB_DEVCTL_BDEVICE)
102                         musb->xceiv->otg->state = OTG_STATE_B_IDLE;
103                 else
104                         musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON;
105         default:
106                 break;
107         }
108         spin_unlock_irqrestore(&musb->lock, flags);
109 }
110
111
112 static void omap2430_musb_try_idle(struct musb *musb, unsigned long timeout)
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)
122                         && (musb->xceiv->otg->state == OTG_STATE_A_WAIT_BCON))) {
123                 dev_dbg(musb->controller, "%s active, deleting timer\n",
124                         usb_otg_state_string(musb->xceiv->otg->state));
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 {
134                         dev_dbg(musb->controller, "Longer idle timer already pending, ignoring\n");
135                         return;
136                 }
137         }
138         last_timer = timeout;
139
140         dev_dbg(musb->controller, "%s inactive, for idle timer for %lu ms\n",
141                 usb_otg_state_string(musb->xceiv->otg->state),
142                 (unsigned long)jiffies_to_msecs(timeout - jiffies));
143         mod_timer(&musb_idle_timer, timeout);
144 }
145
146 static void omap2430_musb_set_vbus(struct musb *musb, int is_on)
147 {
148         struct usb_otg  *otg = musb->xceiv->otg;
149         u8              devctl;
150         unsigned long timeout = jiffies + msecs_to_jiffies(1000);
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) {
159                 if (musb->xceiv->otg->state == OTG_STATE_A_IDLE) {
160                         int loops = 100;
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                          */
168                         while (musb_readb(musb->mregs, MUSB_DEVCTL) &
169                                MUSB_DEVCTL_BDEVICE) {
170
171                                 mdelay(5);
172                                 cpu_relax();
173
174                                 if (time_after(jiffies, timeout)
175                                     || loops-- <= 0) {
176                                         dev_err(musb->controller,
177                                         "configured as A device timeout");
178                                         break;
179                                 }
180                         }
181
182                         otg_set_vbus(otg, 1);
183                 } else {
184                         musb->is_active = 1;
185                         otg->default_a = 1;
186                         musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
187                         devctl |= MUSB_DEVCTL_SESSION;
188                         MUSB_HST_MODE(musb);
189                 }
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
197                 otg->default_a = 0;
198                 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
199                 devctl &= ~MUSB_DEVCTL_SESSION;
200
201                 MUSB_DEV_MODE(musb);
202         }
203         musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
204
205         dev_dbg(musb->controller, "VBUS %s, devctl %02x "
206                 /* otg %3x conf %08x prcm %08x */ "\n",
207                 usb_otg_state_string(musb->xceiv->otg->state),
208                 musb_readb(musb->mregs, MUSB_DEVCTL));
209 }
210
211 static int omap2430_musb_set_mode(struct musb *musb, u8 musb_mode)
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
218         return 0;
219 }
220
221 static 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);
229 }
230
231 static inline void omap2430_low_level_init(struct musb *musb)
232 {
233         u32 l;
234
235         l = musb_readl(musb->mregs, OTG_FORCESTDBY);
236         l &= ~ENABLEFORCE;      /* disable MSTANDBY */
237         musb_writel(musb->mregs, OTG_FORCESTDBY, l);
238 }
239
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  */
245 static 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
279 static void omap2430_musb_mailbox(enum musb_vbus_id_status status)
280 {
281         struct omap2430_glue    *glue = _glue;
282
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)) {
290                 pr_err("%s: musb core is not yet ready\n", __func__);
291                 return;
292         }
293
294         schedule_work(&glue->omap_musb_mailbox_work);
295 }
296
297 static void omap_musb_set_mailbox(struct omap2430_glue *glue)
298 {
299         struct musb *musb = glue_to_musb(glue);
300         struct device *dev = musb->controller;
301         struct musb_hdrc_platform_data *pdata = dev_get_platdata(dev);
302         struct omap_musb_board_data *data = pdata->board_data;
303         struct usb_otg *otg = musb->xceiv->otg;
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);
311
312         switch (glue->status) {
313         case MUSB_ID_GROUND:
314                 dev_dbg(dev, "ID GND\n");
315
316                 otg->default_a = true;
317                 musb->xceiv->otg->state = OTG_STATE_A_IDLE;
318                 musb->xceiv->last_event = USB_EVENT_ID;
319                 if (musb->gadget_driver) {
320                         omap_control_usb_set_mode(glue->control_otghs,
321                                 USB_MODE_HOST);
322                         omap2430_musb_set_vbus(musb, 1);
323                 }
324                 break;
325
326         case MUSB_VBUS_VALID:
327                 dev_dbg(dev, "VBUS Connect\n");
328
329                 otg->default_a = false;
330                 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
331                 musb->xceiv->last_event = USB_EVENT_VBUS;
332                 omap_control_usb_set_mode(glue->control_otghs, USB_MODE_DEVICE);
333                 break;
334
335         case MUSB_ID_FLOAT:
336         case MUSB_VBUS_OFF:
337                 dev_dbg(dev, "VBUS Disconnect\n");
338
339                 musb->xceiv->last_event = USB_EVENT_NONE;
340                 if (musb->gadget_driver)
341                         omap2430_musb_set_vbus(musb, 0);
342
343                 if (data->interface_type == MUSB_INTERFACE_UTMI)
344                         otg_set_vbus(musb->xceiv->otg, 0);
345
346                 omap_control_usb_set_mode(glue->control_otghs,
347                         USB_MODE_DISCONNECT);
348                 break;
349         default:
350                 dev_dbg(dev, "ID float\n");
351         }
352
353         if (!cable_connected)
354                 omap2430_set_power(musb, glue->enabled, cable_connected);
355
356         atomic_notifier_call_chain(&musb->xceiv->notifier,
357                         musb->xceiv->last_event, NULL);
358 }
359
360
361 static 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);
365         struct musb *musb = glue_to_musb(glue);
366         struct device *dev = musb->controller;
367
368         pm_runtime_get_sync(dev);
369         omap_musb_set_mailbox(glue);
370         pm_runtime_mark_last_busy(dev);
371         pm_runtime_put_autosuspend(dev);
372 }
373
374 static 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
394 static int omap2430_musb_init(struct musb *musb)
395 {
396         u32 l;
397         int status = 0;
398         struct device *dev = musb->controller;
399         struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
400         struct musb_hdrc_platform_data *plat = dev_get_platdata(dev);
401         struct omap_musb_board_data *data = plat->board_data;
402
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          */
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                  */
416                 musb->xceiv = devm_usb_get_phy_by_phandle(dev->parent,
417                     "usb-phy", 0);
418         } else {
419                 musb->xceiv = devm_usb_get_phy_dev(dev, 0);
420                 musb->phy = devm_phy_get(dev, "usb");
421         }
422
423         if (IS_ERR(musb->xceiv)) {
424                 status = PTR_ERR(musb->xceiv);
425
426                 if (status == -ENXIO)
427                         return status;
428
429                 pr_err("HS USB OTG: no transceiver configured\n");
430                 return -EPROBE_DEFER;
431         }
432
433         if (IS_ERR(musb->phy)) {
434                 pr_err("HS USB OTG: no PHY configured\n");
435                 return PTR_ERR(musb->phy);
436         }
437         musb->isr = omap2430_musb_interrupt;
438
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
449         l = musb_readl(musb->mregs, OTG_INTERFSEL);
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
459         musb_writel(musb->mregs, OTG_INTERFSEL, l);
460
461         pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
462                         "sysstatus 0x%x, intrfsel 0x%x, simenable  0x%x\n",
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));
468
469         setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
470
471         if (glue->status != MUSB_UNKNOWN)
472                 omap_musb_set_mailbox(glue);
473
474         phy_init(musb->phy);
475         phy_power_on(musb->phy);
476         pm_runtime_put(glue->dev);
477         return 0;
478
479 err1:
480         return status;
481 }
482
483 static 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;
488         struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
489         struct musb_hdrc_platform_data *pdata = dev_get_platdata(dev);
490         struct omap_musb_board_data *data = pdata->board_data;
491
492         omap2430_set_power(musb, true, glue->cable_connected);
493
494         switch (glue->status) {
495
496         case MUSB_ID_GROUND:
497                 omap_control_usb_set_mode(glue->control_otghs, USB_MODE_HOST);
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;
511                         }
512                 }
513                 break;
514
515         case MUSB_VBUS_VALID:
516                 omap_control_usb_set_mode(glue->control_otghs, USB_MODE_DEVICE);
517                 break;
518
519         default:
520                 break;
521         }
522 }
523
524 static void omap2430_musb_disable(struct musb *musb)
525 {
526         struct device *dev = musb->controller;
527         struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
528
529         if (glue->status != MUSB_UNKNOWN)
530                 omap_control_usb_set_mode(glue->control_otghs,
531                         USB_MODE_DISCONNECT);
532
533         omap2430_set_power(musb, false, glue->cable_connected);
534 }
535
536 static int omap2430_musb_exit(struct musb *musb)
537 {
538         del_timer_sync(&musb_idle_timer);
539
540         omap2430_low_level_exit(musb);
541         phy_power_off(musb->phy);
542         phy_exit(musb->phy);
543
544         return 0;
545 }
546
547 static const struct musb_platform_ops omap2430_ops = {
548         .quirks         = MUSB_DMA_INVENTRA,
549 #ifdef CONFIG_USB_INVENTRA_DMA
550         .dma_init       = musbhs_dma_controller_create,
551         .dma_exit       = musbhs_dma_controller_destroy,
552 #endif
553         .init           = omap2430_musb_init,
554         .exit           = omap2430_musb_exit,
555
556         .set_mode       = omap2430_musb_set_mode,
557         .try_idle       = omap2430_musb_try_idle,
558
559         .set_vbus       = omap2430_musb_set_vbus,
560
561         .enable         = omap2430_musb_enable,
562         .disable        = omap2430_musb_disable,
563
564         .phy_callback   = omap2430_musb_mailbox,
565 };
566
567 static u64 omap2430_dmamask = DMA_BIT_MASK(32);
568
569 static int omap2430_probe(struct platform_device *pdev)
570 {
571         struct resource                 musb_resources[3];
572         struct musb_hdrc_platform_data  *pdata = dev_get_platdata(&pdev->dev);
573         struct omap_musb_board_data     *data;
574         struct platform_device          *musb;
575         struct omap2430_glue            *glue;
576         struct device_node              *np = pdev->dev.of_node;
577         struct musb_hdrc_config         *config;
578         int                             ret = -ENOMEM, val;
579
580         glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
581         if (!glue)
582                 goto err0;
583
584         musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
585         if (!musb) {
586                 dev_err(&pdev->dev, "failed to allocate musb device\n");
587                 goto err0;
588         }
589
590         musb->dev.parent                = &pdev->dev;
591         musb->dev.dma_mask              = &omap2430_dmamask;
592         musb->dev.coherent_dma_mask     = omap2430_dmamask;
593
594         glue->dev                       = &pdev->dev;
595         glue->musb                      = musb;
596         glue->status                    = MUSB_UNKNOWN;
597         glue->control_otghs = ERR_PTR(-ENODEV);
598
599         if (np) {
600                 struct device_node *control_node;
601                 struct platform_device *control_pdev;
602
603                 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
604                 if (!pdata)
605                         goto err2;
606
607                 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
608                 if (!data)
609                         goto err2;
610
611                 config = devm_kzalloc(&pdev->dev, sizeof(*config), GFP_KERNEL);
612                 if (!config)
613                         goto err2;
614
615                 of_property_read_u32(np, "mode", (u32 *)&pdata->mode);
616                 of_property_read_u32(np, "interface-type",
617                                                 (u32 *)&data->interface_type);
618                 of_property_read_u32(np, "num-eps", (u32 *)&config->num_eps);
619                 of_property_read_u32(np, "ram-bits", (u32 *)&config->ram_bits);
620                 of_property_read_u32(np, "power", (u32 *)&pdata->power);
621
622                 ret = of_property_read_u32(np, "multipoint", &val);
623                 if (!ret && val)
624                         config->multipoint = true;
625
626                 pdata->board_data       = data;
627                 pdata->config           = config;
628
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;
638                 }
639         }
640         pdata->platform_ops             = &omap2430_ops;
641
642         platform_set_drvdata(pdev, glue);
643
644         /*
645          * REVISIT if we ever have two instances of the wrapper, we will be
646          * in big trouble
647          */
648         _glue   = glue;
649
650         INIT_WORK(&glue->omap_musb_mailbox_work, omap_musb_mailbox_work);
651
652         memset(musb_resources, 0x00, sizeof(*musb_resources) *
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
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
670         ret = platform_device_add_resources(musb, musb_resources,
671                         ARRAY_SIZE(musb_resources));
672         if (ret) {
673                 dev_err(&pdev->dev, "failed to add resources\n");
674                 goto err2;
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");
680                 goto err2;
681         }
682
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          */
688
689         ret = platform_device_add(musb);
690         if (ret) {
691                 dev_err(&pdev->dev, "failed to register musb device\n");
692                 goto err2;
693         }
694
695         return 0;
696
697 err2:
698         platform_device_put(musb);
699
700 err0:
701         return ret;
702 }
703
704 static int omap2430_remove(struct platform_device *pdev)
705 {
706         struct omap2430_glue *glue = platform_get_drvdata(pdev);
707         struct musb *musb = glue_to_musb(glue);
708
709         pm_runtime_get_sync(glue->dev);
710         cancel_work_sync(&glue->omap_musb_mailbox_work);
711         platform_device_unregister(glue->musb);
712         omap2430_set_power(musb, false, false);
713         pm_runtime_put_sync(glue->dev);
714         pm_runtime_disable(glue->dev);
715
716         return 0;
717 }
718
719 #ifdef CONFIG_PM
720
721 static int omap2430_runtime_suspend(struct device *dev)
722 {
723         struct omap2430_glue            *glue = dev_get_drvdata(dev);
724         struct musb                     *musb = glue_to_musb(glue);
725
726         if (musb) {
727                 musb->context.otg_interfsel = musb_readl(musb->mregs,
728                                 OTG_INTERFSEL);
729
730                 omap2430_low_level_exit(musb);
731         }
732
733         return 0;
734 }
735
736 static int omap2430_runtime_resume(struct device *dev)
737 {
738         struct omap2430_glue            *glue = dev_get_drvdata(dev);
739         struct musb                     *musb = glue_to_musb(glue);
740
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);
747
748         return 0;
749 }
750
751 static struct dev_pm_ops omap2430_pm_ops = {
752         .runtime_suspend = omap2430_runtime_suspend,
753         .runtime_resume = omap2430_runtime_resume,
754 };
755
756 #define DEV_PM_OPS      (&omap2430_pm_ops)
757 #else
758 #define DEV_PM_OPS      NULL
759 #endif
760
761 #ifdef CONFIG_OF
762 static const struct of_device_id omap2430_id_table[] = {
763         {
764                 .compatible = "ti,omap4-musb"
765         },
766         {
767                 .compatible = "ti,omap3-musb"
768         },
769         {},
770 };
771 MODULE_DEVICE_TABLE(of, omap2430_id_table);
772 #endif
773
774 static struct platform_driver omap2430_driver = {
775         .probe          = omap2430_probe,
776         .remove         = omap2430_remove,
777         .driver         = {
778                 .name   = "musb-omap2430",
779                 .pm     = DEV_PM_OPS,
780                 .of_match_table = of_match_ptr(omap2430_id_table),
781         },
782 };
783
784 MODULE_DESCRIPTION("OMAP2PLUS MUSB Glue Layer");
785 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
786 MODULE_LICENSE("GPL v2");
787
788 static int __init omap2430_init(void)
789 {
790         return platform_driver_register(&omap2430_driver);
791 }
792 subsys_initcall(omap2430_init);
793
794 static void __exit omap2430_exit(void)
795 {
796         platform_driver_unregister(&omap2430_driver);
797 }
798 module_exit(omap2430_exit);