2 * Copyright (C) 2010 ST-Ericsson AB
3 * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/clk.h>
25 #include <linux/err.h>
28 #include <linux/platform_device.h>
29 #include <linux/usb/musb-ux500.h>
31 #include "musb_core.h"
33 static struct musb_hdrc_config ux500_musb_hdrc_config
= {
42 struct platform_device
*musb
;
45 #define glue_to_musb(g) platform_get_drvdata(g->musb)
47 static void ux500_musb_set_vbus(struct musb
*musb
, int is_on
)
50 unsigned long timeout
= jiffies
+ msecs_to_jiffies(1000);
51 /* HDRC controls CPEN, but beware current surges during device
52 * connect. They can trigger transient overcurrent conditions
53 * that must be ignored.
56 devctl
= musb_readb(musb
->mregs
, MUSB_DEVCTL
);
59 if (musb
->xceiv
->otg
->state
== OTG_STATE_A_IDLE
) {
60 /* start the session */
61 devctl
|= MUSB_DEVCTL_SESSION
;
62 musb_writeb(musb
->mregs
, MUSB_DEVCTL
, devctl
);
64 * Wait for the musb to set as A device to enable the
67 while (musb_readb(musb
->mregs
, MUSB_DEVCTL
) & 0x80) {
69 if (time_after(jiffies
, timeout
)) {
70 dev_err(musb
->controller
,
71 "configured as A device timeout");
78 musb
->xceiv
->otg
->default_a
= 1;
79 musb
->xceiv
->otg
->state
= OTG_STATE_A_WAIT_VRISE
;
80 devctl
|= MUSB_DEVCTL_SESSION
;
86 /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and jumping
89 musb
->xceiv
->otg
->default_a
= 0;
90 devctl
&= ~MUSB_DEVCTL_SESSION
;
93 musb_writeb(musb
->mregs
, MUSB_DEVCTL
, devctl
);
96 * Devctl values will be updated after vbus goes below
97 * session_valid. The time taken depends on the capacitance
98 * on VBUS line. The max discharge time can be upto 1 sec
99 * as per the spec. Typically on our platform, it is 200ms
104 dev_dbg(musb
->controller
, "VBUS %s, devctl %02x\n",
105 usb_otg_state_string(musb
->xceiv
->otg
->state
),
106 musb_readb(musb
->mregs
, MUSB_DEVCTL
));
109 static int musb_otg_notifications(struct notifier_block
*nb
,
110 unsigned long event
, void *unused
)
112 struct musb
*musb
= container_of(nb
, struct musb
, nb
);
114 dev_dbg(musb
->controller
, "musb_otg_notifications %ld %s\n",
115 event
, usb_otg_state_string(musb
->xceiv
->otg
->state
));
119 dev_dbg(musb
->controller
, "ID GND\n");
120 ux500_musb_set_vbus(musb
, 1);
122 case UX500_MUSB_VBUS
:
123 dev_dbg(musb
->controller
, "VBUS Connect\n");
125 case UX500_MUSB_NONE
:
126 dev_dbg(musb
->controller
, "VBUS Disconnect\n");
127 if (is_host_active(musb
))
128 ux500_musb_set_vbus(musb
, 0);
130 musb
->xceiv
->otg
->state
= OTG_STATE_B_IDLE
;
133 dev_dbg(musb
->controller
, "ID float\n");
139 static irqreturn_t
ux500_musb_interrupt(int irq
, void *__hci
)
142 irqreturn_t retval
= IRQ_NONE
;
143 struct musb
*musb
= __hci
;
145 spin_lock_irqsave(&musb
->lock
, flags
);
147 musb
->int_usb
= musb_readb(musb
->mregs
, MUSB_INTRUSB
);
148 musb
->int_tx
= musb_readw(musb
->mregs
, MUSB_INTRTX
);
149 musb
->int_rx
= musb_readw(musb
->mregs
, MUSB_INTRRX
);
151 if (musb
->int_usb
|| musb
->int_tx
|| musb
->int_rx
)
152 retval
= musb_interrupt(musb
);
154 spin_unlock_irqrestore(&musb
->lock
, flags
);
159 static int ux500_musb_init(struct musb
*musb
)
163 musb
->xceiv
= usb_get_phy(USB_PHY_TYPE_USB2
);
164 if (IS_ERR_OR_NULL(musb
->xceiv
)) {
165 pr_err("HS USB OTG: no transceiver configured\n");
166 return -EPROBE_DEFER
;
169 musb
->nb
.notifier_call
= musb_otg_notifications
;
170 status
= usb_register_notifier(musb
->xceiv
, &musb
->nb
);
172 dev_dbg(musb
->controller
, "notification register failed\n");
176 musb
->isr
= ux500_musb_interrupt
;
181 static int ux500_musb_exit(struct musb
*musb
)
183 usb_unregister_notifier(musb
->xceiv
, &musb
->nb
);
185 usb_put_phy(musb
->xceiv
);
190 static const struct musb_platform_ops ux500_ops
= {
191 .quirks
= MUSB_DMA_UX500
| MUSB_INDEXED_EP
,
192 #ifdef CONFIG_USB_UX500_DMA
193 .dma_init
= ux500_dma_controller_create
,
194 .dma_exit
= ux500_dma_controller_destroy
,
196 .init
= ux500_musb_init
,
197 .exit
= ux500_musb_exit
,
200 .set_vbus
= ux500_musb_set_vbus
,
203 static struct musb_hdrc_platform_data
*
204 ux500_of_probe(struct platform_device
*pdev
, struct device_node
*np
)
206 struct musb_hdrc_platform_data
*pdata
;
210 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
214 mode
= of_get_property(np
, "dr_mode", &strlen
);
216 dev_err(&pdev
->dev
, "No 'dr_mode' property found\n");
221 if (!strcmp(mode
, "host"))
222 pdata
->mode
= MUSB_HOST
;
223 if (!strcmp(mode
, "otg"))
224 pdata
->mode
= MUSB_OTG
;
225 if (!strcmp(mode
, "peripheral"))
226 pdata
->mode
= MUSB_PERIPHERAL
;
232 static int ux500_probe(struct platform_device
*pdev
)
234 struct resource musb_resources
[2];
235 struct musb_hdrc_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
236 struct device_node
*np
= pdev
->dev
.of_node
;
237 struct platform_device
*musb
;
238 struct ux500_glue
*glue
;
244 pdata
= ux500_of_probe(pdev
, np
);
248 pdev
->dev
.platform_data
= pdata
;
250 dev_err(&pdev
->dev
, "no pdata or device tree found\n");
255 glue
= devm_kzalloc(&pdev
->dev
, sizeof(*glue
), GFP_KERNEL
);
259 musb
= platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO
);
261 dev_err(&pdev
->dev
, "failed to allocate musb device\n");
265 clk
= devm_clk_get(&pdev
->dev
, NULL
);
267 dev_err(&pdev
->dev
, "failed to get clock\n");
272 ret
= clk_prepare_enable(clk
);
274 dev_err(&pdev
->dev
, "failed to enable clock\n");
278 musb
->dev
.parent
= &pdev
->dev
;
279 musb
->dev
.dma_mask
= &pdev
->dev
.coherent_dma_mask
;
280 musb
->dev
.coherent_dma_mask
= pdev
->dev
.coherent_dma_mask
;
282 glue
->dev
= &pdev
->dev
;
286 pdata
->platform_ops
= &ux500_ops
;
287 pdata
->config
= &ux500_musb_hdrc_config
;
289 platform_set_drvdata(pdev
, glue
);
291 memset(musb_resources
, 0x00, sizeof(*musb_resources
) *
292 ARRAY_SIZE(musb_resources
));
294 musb_resources
[0].name
= pdev
->resource
[0].name
;
295 musb_resources
[0].start
= pdev
->resource
[0].start
;
296 musb_resources
[0].end
= pdev
->resource
[0].end
;
297 musb_resources
[0].flags
= pdev
->resource
[0].flags
;
299 musb_resources
[1].name
= pdev
->resource
[1].name
;
300 musb_resources
[1].start
= pdev
->resource
[1].start
;
301 musb_resources
[1].end
= pdev
->resource
[1].end
;
302 musb_resources
[1].flags
= pdev
->resource
[1].flags
;
304 ret
= platform_device_add_resources(musb
, musb_resources
,
305 ARRAY_SIZE(musb_resources
));
307 dev_err(&pdev
->dev
, "failed to add resources\n");
311 ret
= platform_device_add_data(musb
, pdata
, sizeof(*pdata
));
313 dev_err(&pdev
->dev
, "failed to add platform_data\n");
317 ret
= platform_device_add(musb
);
319 dev_err(&pdev
->dev
, "failed to register musb device\n");
326 clk_disable_unprepare(clk
);
329 platform_device_put(musb
);
335 static int ux500_remove(struct platform_device
*pdev
)
337 struct ux500_glue
*glue
= platform_get_drvdata(pdev
);
339 platform_device_unregister(glue
->musb
);
340 clk_disable_unprepare(glue
->clk
);
345 #ifdef CONFIG_PM_SLEEP
346 static int ux500_suspend(struct device
*dev
)
348 struct ux500_glue
*glue
= dev_get_drvdata(dev
);
349 struct musb
*musb
= glue_to_musb(glue
);
352 usb_phy_set_suspend(musb
->xceiv
, 1);
354 clk_disable_unprepare(glue
->clk
);
359 static int ux500_resume(struct device
*dev
)
361 struct ux500_glue
*glue
= dev_get_drvdata(dev
);
362 struct musb
*musb
= glue_to_musb(glue
);
365 ret
= clk_prepare_enable(glue
->clk
);
367 dev_err(dev
, "failed to enable clock\n");
372 usb_phy_set_suspend(musb
->xceiv
, 0);
378 static SIMPLE_DEV_PM_OPS(ux500_pm_ops
, ux500_suspend
, ux500_resume
);
380 static const struct of_device_id ux500_match
[] = {
381 { .compatible
= "stericsson,db8500-musb", },
385 MODULE_DEVICE_TABLE(of
, ux500_match
);
387 static struct platform_driver ux500_driver
= {
388 .probe
= ux500_probe
,
389 .remove
= ux500_remove
,
391 .name
= "musb-ux500",
393 .of_match_table
= ux500_match
,
397 MODULE_DESCRIPTION("UX500 MUSB Glue Layer");
398 MODULE_AUTHOR("Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>");
399 MODULE_LICENSE("GPL v2");
400 module_platform_driver(ux500_driver
);