Merge tag 'drivers-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/arm...
[linux-2.6.git] / drivers / usb / musb / ux500.c
blobfce71b605936ed228bc104efd2d5d60c0959af2f
1 /*
2 * Copyright (C) 2010 ST-Ericsson AB
3 * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
5 * Based on omap2430.c
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/init.h>
25 #include <linux/clk.h>
26 #include <linux/err.h>
27 #include <linux/io.h>
28 #include <linux/of.h>
29 #include <linux/platform_device.h>
30 #include <linux/usb/musb-ux500.h>
32 #include "musb_core.h"
34 static struct musb_hdrc_config ux500_musb_hdrc_config = {
35 .multipoint = true,
36 .dyn_fifo = true,
37 .num_eps = 16,
38 .ram_bits = 16,
41 struct ux500_glue {
42 struct device *dev;
43 struct platform_device *musb;
44 struct clk *clk;
46 #define glue_to_musb(g) platform_get_drvdata(g->musb)
48 static void ux500_musb_set_vbus(struct musb *musb, int is_on)
50 u8 devctl;
51 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
52 /* HDRC controls CPEN, but beware current surges during device
53 * connect. They can trigger transient overcurrent conditions
54 * that must be ignored.
57 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
59 if (is_on) {
60 if (musb->xceiv->state == OTG_STATE_A_IDLE) {
61 /* start the session */
62 devctl |= MUSB_DEVCTL_SESSION;
63 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
65 * Wait for the musb to set as A device to enable the
66 * VBUS
68 while (musb_readb(musb->mregs, MUSB_DEVCTL) & 0x80) {
70 if (time_after(jiffies, timeout)) {
71 dev_err(musb->controller,
72 "configured as A device timeout");
73 break;
77 } else {
78 musb->is_active = 1;
79 musb->xceiv->otg->default_a = 1;
80 musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
81 devctl |= MUSB_DEVCTL_SESSION;
82 MUSB_HST_MODE(musb);
84 } else {
85 musb->is_active = 0;
87 /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and jumping
88 * right to B_IDLE...
90 musb->xceiv->otg->default_a = 0;
91 devctl &= ~MUSB_DEVCTL_SESSION;
92 MUSB_DEV_MODE(musb);
94 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
97 * Devctl values will be updated after vbus goes below
98 * session_valid. The time taken depends on the capacitance
99 * on VBUS line. The max discharge time can be upto 1 sec
100 * as per the spec. Typically on our platform, it is 200ms
102 if (!is_on)
103 mdelay(200);
105 dev_dbg(musb->controller, "VBUS %s, devctl %02x\n",
106 usb_otg_state_string(musb->xceiv->state),
107 musb_readb(musb->mregs, MUSB_DEVCTL));
110 static int musb_otg_notifications(struct notifier_block *nb,
111 unsigned long event, void *unused)
113 struct musb *musb = container_of(nb, struct musb, nb);
115 dev_dbg(musb->controller, "musb_otg_notifications %ld %s\n",
116 event, usb_otg_state_string(musb->xceiv->state));
118 switch (event) {
119 case UX500_MUSB_ID:
120 dev_dbg(musb->controller, "ID GND\n");
121 ux500_musb_set_vbus(musb, 1);
122 break;
123 case UX500_MUSB_VBUS:
124 dev_dbg(musb->controller, "VBUS Connect\n");
125 break;
126 case UX500_MUSB_NONE:
127 dev_dbg(musb->controller, "VBUS Disconnect\n");
128 if (is_host_active(musb))
129 ux500_musb_set_vbus(musb, 0);
130 else
131 musb->xceiv->state = OTG_STATE_B_IDLE;
132 break;
133 default:
134 dev_dbg(musb->controller, "ID float\n");
135 return NOTIFY_DONE;
137 return NOTIFY_OK;
140 static irqreturn_t ux500_musb_interrupt(int irq, void *__hci)
142 unsigned long flags;
143 irqreturn_t retval = IRQ_NONE;
144 struct musb *musb = __hci;
146 spin_lock_irqsave(&musb->lock, flags);
148 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
149 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
150 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
152 if (musb->int_usb || musb->int_tx || musb->int_rx)
153 retval = musb_interrupt(musb);
155 spin_unlock_irqrestore(&musb->lock, flags);
157 return retval;
160 static int ux500_musb_init(struct musb *musb)
162 int status;
164 musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
165 if (IS_ERR_OR_NULL(musb->xceiv)) {
166 pr_err("HS USB OTG: no transceiver configured\n");
167 return -EPROBE_DEFER;
170 musb->nb.notifier_call = musb_otg_notifications;
171 status = usb_register_notifier(musb->xceiv, &musb->nb);
172 if (status < 0) {
173 dev_dbg(musb->controller, "notification register failed\n");
174 return status;
177 musb->isr = ux500_musb_interrupt;
179 return 0;
182 static int ux500_musb_exit(struct musb *musb)
184 usb_unregister_notifier(musb->xceiv, &musb->nb);
186 usb_put_phy(musb->xceiv);
188 return 0;
191 static const struct musb_platform_ops ux500_ops = {
192 .init = ux500_musb_init,
193 .exit = ux500_musb_exit,
195 .set_vbus = ux500_musb_set_vbus,
198 static struct musb_hdrc_platform_data *
199 ux500_of_probe(struct platform_device *pdev, struct device_node *np)
201 struct musb_hdrc_platform_data *pdata;
202 const char *mode;
203 int strlen;
205 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
206 if (!pdata)
207 return NULL;
209 mode = of_get_property(np, "dr_mode", &strlen);
210 if (!mode) {
211 dev_err(&pdev->dev, "No 'dr_mode' property found\n");
212 return NULL;
215 if (strlen > 0) {
216 if (!strcmp(mode, "host"))
217 pdata->mode = MUSB_HOST;
218 if (!strcmp(mode, "otg"))
219 pdata->mode = MUSB_OTG;
220 if (!strcmp(mode, "peripheral"))
221 pdata->mode = MUSB_PERIPHERAL;
224 return pdata;
227 static int ux500_probe(struct platform_device *pdev)
229 struct resource musb_resources[2];
230 struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
231 struct device_node *np = pdev->dev.of_node;
232 struct platform_device *musb;
233 struct ux500_glue *glue;
234 struct clk *clk;
235 int ret = -ENOMEM;
237 if (!pdata) {
238 if (np) {
239 pdata = ux500_of_probe(pdev, np);
240 if (!pdata)
241 goto err0;
243 pdev->dev.platform_data = pdata;
244 } else {
245 dev_err(&pdev->dev, "no pdata or device tree found\n");
246 goto err0;
250 glue = kzalloc(sizeof(*glue), GFP_KERNEL);
251 if (!glue) {
252 dev_err(&pdev->dev, "failed to allocate glue context\n");
253 goto err0;
256 musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
257 if (!musb) {
258 dev_err(&pdev->dev, "failed to allocate musb device\n");
259 goto err1;
262 clk = clk_get(&pdev->dev, "usb");
263 if (IS_ERR(clk)) {
264 dev_err(&pdev->dev, "failed to get clock\n");
265 ret = PTR_ERR(clk);
266 goto err3;
269 ret = clk_prepare_enable(clk);
270 if (ret) {
271 dev_err(&pdev->dev, "failed to enable clock\n");
272 goto err4;
275 musb->dev.parent = &pdev->dev;
276 musb->dev.dma_mask = &pdev->dev.coherent_dma_mask;
277 musb->dev.coherent_dma_mask = pdev->dev.coherent_dma_mask;
278 musb->dev.of_node = pdev->dev.of_node;
280 glue->dev = &pdev->dev;
281 glue->musb = musb;
282 glue->clk = clk;
284 pdata->platform_ops = &ux500_ops;
285 pdata->config = &ux500_musb_hdrc_config;
287 platform_set_drvdata(pdev, glue);
289 memset(musb_resources, 0x00, sizeof(*musb_resources) *
290 ARRAY_SIZE(musb_resources));
292 musb_resources[0].name = pdev->resource[0].name;
293 musb_resources[0].start = pdev->resource[0].start;
294 musb_resources[0].end = pdev->resource[0].end;
295 musb_resources[0].flags = pdev->resource[0].flags;
297 musb_resources[1].name = pdev->resource[1].name;
298 musb_resources[1].start = pdev->resource[1].start;
299 musb_resources[1].end = pdev->resource[1].end;
300 musb_resources[1].flags = pdev->resource[1].flags;
302 ret = platform_device_add_resources(musb, musb_resources,
303 ARRAY_SIZE(musb_resources));
304 if (ret) {
305 dev_err(&pdev->dev, "failed to add resources\n");
306 goto err5;
309 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
310 if (ret) {
311 dev_err(&pdev->dev, "failed to add platform_data\n");
312 goto err5;
315 ret = platform_device_add(musb);
316 if (ret) {
317 dev_err(&pdev->dev, "failed to register musb device\n");
318 goto err5;
321 return 0;
323 err5:
324 clk_disable_unprepare(clk);
326 err4:
327 clk_put(clk);
329 err3:
330 platform_device_put(musb);
332 err1:
333 kfree(glue);
335 err0:
336 return ret;
339 static int ux500_remove(struct platform_device *pdev)
341 struct ux500_glue *glue = platform_get_drvdata(pdev);
343 platform_device_unregister(glue->musb);
344 clk_disable_unprepare(glue->clk);
345 clk_put(glue->clk);
346 kfree(glue);
348 return 0;
351 #ifdef CONFIG_PM
352 static int ux500_suspend(struct device *dev)
354 struct ux500_glue *glue = dev_get_drvdata(dev);
355 struct musb *musb = glue_to_musb(glue);
357 usb_phy_set_suspend(musb->xceiv, 1);
358 clk_disable_unprepare(glue->clk);
360 return 0;
363 static int ux500_resume(struct device *dev)
365 struct ux500_glue *glue = dev_get_drvdata(dev);
366 struct musb *musb = glue_to_musb(glue);
367 int ret;
369 ret = clk_prepare_enable(glue->clk);
370 if (ret) {
371 dev_err(dev, "failed to enable clock\n");
372 return ret;
375 usb_phy_set_suspend(musb->xceiv, 0);
377 return 0;
380 static const struct dev_pm_ops ux500_pm_ops = {
381 .suspend = ux500_suspend,
382 .resume = ux500_resume,
385 #define DEV_PM_OPS (&ux500_pm_ops)
386 #else
387 #define DEV_PM_OPS NULL
388 #endif
390 static const struct of_device_id ux500_match[] = {
391 { .compatible = "stericsson,db8500-musb", },
395 static struct platform_driver ux500_driver = {
396 .probe = ux500_probe,
397 .remove = ux500_remove,
398 .driver = {
399 .name = "musb-ux500",
400 .pm = DEV_PM_OPS,
401 .of_match_table = ux500_match,
405 MODULE_DESCRIPTION("UX500 MUSB Glue Layer");
406 MODULE_AUTHOR("Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>");
407 MODULE_LICENSE("GPL v2");
408 module_platform_driver(ux500_driver);