geneve_core: identify as driver library in modules description
[deliverable/linux.git] / drivers / usb / musb / ux500.c
CommitLineData
4bc36fd3
MYK
1/*
2 * Copyright (C) 2010 ST-Ericsson AB
3 * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
4 *
5 * Based on omap2430.c
6 *
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.
11 *
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.
16 *
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.
20 */
21
22#include <linux/module.h>
23#include <linux/kernel.h>
4bc36fd3 24#include <linux/clk.h>
ded017ee 25#include <linux/err.h>
4bc36fd3 26#include <linux/io.h>
313bdb11 27#include <linux/of.h>
4bc36fd3 28#include <linux/platform_device.h>
af6882be 29#include <linux/usb/musb-ux500.h>
4bc36fd3
MYK
30
31#include "musb_core.h"
32
a20b1b79
LJ
33static struct musb_hdrc_config ux500_musb_hdrc_config = {
34 .multipoint = true,
35 .dyn_fifo = true,
36 .num_eps = 16,
37 .ram_bits = 16,
38};
39
4bc36fd3
MYK
40struct ux500_glue {
41 struct device *dev;
42 struct platform_device *musb;
43 struct clk *clk;
44};
45#define glue_to_musb(g) platform_get_drvdata(g->musb)
46
996a9d26
FB
47static void ux500_musb_set_vbus(struct musb *musb, int is_on)
48{
49 u8 devctl;
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.
54 */
55
56 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
57
58 if (is_on) {
e47d9254 59 if (musb->xceiv->otg->state == OTG_STATE_A_IDLE) {
996a9d26
FB
60 /* start the session */
61 devctl |= MUSB_DEVCTL_SESSION;
62 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
63 /*
64 * Wait for the musb to set as A device to enable the
65 * VBUS
66 */
67 while (musb_readb(musb->mregs, MUSB_DEVCTL) & 0x80) {
68
69 if (time_after(jiffies, timeout)) {
70 dev_err(musb->controller,
71 "configured as A device timeout");
72 break;
73 }
74 }
75
76 } else {
77 musb->is_active = 1;
78 musb->xceiv->otg->default_a = 1;
e47d9254 79 musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
996a9d26
FB
80 devctl |= MUSB_DEVCTL_SESSION;
81 MUSB_HST_MODE(musb);
82 }
83 } else {
84 musb->is_active = 0;
85
86 /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and jumping
87 * right to B_IDLE...
88 */
89 musb->xceiv->otg->default_a = 0;
90 devctl &= ~MUSB_DEVCTL_SESSION;
91 MUSB_DEV_MODE(musb);
92 }
93 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
94
95 /*
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
100 */
101 if (!is_on)
102 mdelay(200);
103
104 dev_dbg(musb->controller, "VBUS %s, devctl %02x\n",
e47d9254 105 usb_otg_state_string(musb->xceiv->otg->state),
996a9d26
FB
106 musb_readb(musb->mregs, MUSB_DEVCTL));
107}
108
0135522c
FB
109static int musb_otg_notifications(struct notifier_block *nb,
110 unsigned long event, void *unused)
111{
112 struct musb *musb = container_of(nb, struct musb, nb);
113
114 dev_dbg(musb->controller, "musb_otg_notifications %ld %s\n",
e47d9254 115 event, usb_otg_state_string(musb->xceiv->otg->state));
0135522c
FB
116
117 switch (event) {
af6882be 118 case UX500_MUSB_ID:
0135522c
FB
119 dev_dbg(musb->controller, "ID GND\n");
120 ux500_musb_set_vbus(musb, 1);
121 break;
af6882be 122 case UX500_MUSB_VBUS:
0135522c 123 dev_dbg(musb->controller, "VBUS Connect\n");
0135522c 124 break;
af6882be 125 case UX500_MUSB_NONE:
0135522c
FB
126 dev_dbg(musb->controller, "VBUS Disconnect\n");
127 if (is_host_active(musb))
128 ux500_musb_set_vbus(musb, 0);
129 else
e47d9254 130 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
0135522c
FB
131 break;
132 default:
133 dev_dbg(musb->controller, "ID float\n");
134 return NOTIFY_DONE;
135 }
136 return NOTIFY_OK;
137}
138
baef653a
PDS
139static irqreturn_t ux500_musb_interrupt(int irq, void *__hci)
140{
141 unsigned long flags;
142 irqreturn_t retval = IRQ_NONE;
143 struct musb *musb = __hci;
144
145 spin_lock_irqsave(&musb->lock, flags);
146
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);
150
151 if (musb->int_usb || musb->int_tx || musb->int_rx)
152 retval = musb_interrupt(musb);
153
154 spin_unlock_irqrestore(&musb->lock, flags);
155
156 return retval;
157}
158
4bc36fd3
MYK
159static int ux500_musb_init(struct musb *musb)
160{
0135522c
FB
161 int status;
162
662dca54 163 musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
ded017ee 164 if (IS_ERR_OR_NULL(musb->xceiv)) {
4bc36fd3 165 pr_err("HS USB OTG: no transceiver configured\n");
25736e0c 166 return -EPROBE_DEFER;
4bc36fd3
MYK
167 }
168
0135522c
FB
169 musb->nb.notifier_call = musb_otg_notifications;
170 status = usb_register_notifier(musb->xceiv, &musb->nb);
171 if (status < 0) {
172 dev_dbg(musb->controller, "notification register failed\n");
173 return status;
174 }
175
baef653a
PDS
176 musb->isr = ux500_musb_interrupt;
177
4bc36fd3
MYK
178 return 0;
179}
180
181static int ux500_musb_exit(struct musb *musb)
182{
0135522c
FB
183 usb_unregister_notifier(musb->xceiv, &musb->nb);
184
721002ec 185 usb_put_phy(musb->xceiv);
4bc36fd3
MYK
186
187 return 0;
188}
189
190static const struct musb_platform_ops ux500_ops = {
d026e9c7 191 .quirks = MUSB_INDEXED_EP,
4bc36fd3
MYK
192 .init = ux500_musb_init,
193 .exit = ux500_musb_exit,
8a77f05a 194 .fifo_mode = 5,
996a9d26
FB
195
196 .set_vbus = ux500_musb_set_vbus,
4bc36fd3
MYK
197};
198
313bdb11
LJ
199static struct musb_hdrc_platform_data *
200ux500_of_probe(struct platform_device *pdev, struct device_node *np)
201{
202 struct musb_hdrc_platform_data *pdata;
203 const char *mode;
204 int strlen;
205
206 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
207 if (!pdata)
208 return NULL;
209
210 mode = of_get_property(np, "dr_mode", &strlen);
211 if (!mode) {
212 dev_err(&pdev->dev, "No 'dr_mode' property found\n");
213 return NULL;
214 }
215
216 if (strlen > 0) {
217 if (!strcmp(mode, "host"))
218 pdata->mode = MUSB_HOST;
219 if (!strcmp(mode, "otg"))
220 pdata->mode = MUSB_OTG;
221 if (!strcmp(mode, "peripheral"))
222 pdata->mode = MUSB_PERIPHERAL;
223 }
224
225 return pdata;
226}
227
41ac7b3a 228static int ux500_probe(struct platform_device *pdev)
4bc36fd3 229{
09fc7d22 230 struct resource musb_resources[2];
c1a7d67c 231 struct musb_hdrc_platform_data *pdata = dev_get_platdata(&pdev->dev);
313bdb11 232 struct device_node *np = pdev->dev.of_node;
4bc36fd3
MYK
233 struct platform_device *musb;
234 struct ux500_glue *glue;
235 struct clk *clk;
4bc36fd3
MYK
236 int ret = -ENOMEM;
237
313bdb11
LJ
238 if (!pdata) {
239 if (np) {
240 pdata = ux500_of_probe(pdev, np);
241 if (!pdata)
242 goto err0;
243
244 pdev->dev.platform_data = pdata;
245 } else {
246 dev_err(&pdev->dev, "no pdata or device tree found\n");
247 goto err0;
248 }
249 }
250
d7dc5bde 251 glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
24c611b9 252 if (!glue)
4bc36fd3 253 goto err0;
4bc36fd3 254
2f771164 255 musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
4bc36fd3
MYK
256 if (!musb) {
257 dev_err(&pdev->dev, "failed to allocate musb device\n");
d7dc5bde 258 goto err0;
4bc36fd3
MYK
259 }
260
d7dc5bde 261 clk = devm_clk_get(&pdev->dev, NULL);
4bc36fd3
MYK
262 if (IS_ERR(clk)) {
263 dev_err(&pdev->dev, "failed to get clock\n");
264 ret = PTR_ERR(clk);
d7dc5bde 265 goto err1;
4bc36fd3
MYK
266 }
267
99d17cfa 268 ret = clk_prepare_enable(clk);
4bc36fd3
MYK
269 if (ret) {
270 dev_err(&pdev->dev, "failed to enable clock\n");
d7dc5bde 271 goto err1;
4bc36fd3
MYK
272 }
273
274 musb->dev.parent = &pdev->dev;
1e6eebb4 275 musb->dev.dma_mask = &pdev->dev.coherent_dma_mask;
87266064 276 musb->dev.coherent_dma_mask = pdev->dev.coherent_dma_mask;
4bc36fd3
MYK
277
278 glue->dev = &pdev->dev;
279 glue->musb = musb;
280 glue->clk = clk;
281
282 pdata->platform_ops = &ux500_ops;
a20b1b79 283 pdata->config = &ux500_musb_hdrc_config;
4bc36fd3
MYK
284
285 platform_set_drvdata(pdev, glue);
286
09fc7d22
FB
287 memset(musb_resources, 0x00, sizeof(*musb_resources) *
288 ARRAY_SIZE(musb_resources));
289
290 musb_resources[0].name = pdev->resource[0].name;
291 musb_resources[0].start = pdev->resource[0].start;
292 musb_resources[0].end = pdev->resource[0].end;
293 musb_resources[0].flags = pdev->resource[0].flags;
294
295 musb_resources[1].name = pdev->resource[1].name;
296 musb_resources[1].start = pdev->resource[1].start;
297 musb_resources[1].end = pdev->resource[1].end;
298 musb_resources[1].flags = pdev->resource[1].flags;
299
300 ret = platform_device_add_resources(musb, musb_resources,
301 ARRAY_SIZE(musb_resources));
4bc36fd3
MYK
302 if (ret) {
303 dev_err(&pdev->dev, "failed to add resources\n");
d7dc5bde 304 goto err2;
4bc36fd3
MYK
305 }
306
307 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
308 if (ret) {
309 dev_err(&pdev->dev, "failed to add platform_data\n");
d7dc5bde 310 goto err2;
4bc36fd3
MYK
311 }
312
313 ret = platform_device_add(musb);
314 if (ret) {
315 dev_err(&pdev->dev, "failed to register musb device\n");
d7dc5bde 316 goto err2;
4bc36fd3
MYK
317 }
318
319 return 0;
320
d7dc5bde 321err2:
99d17cfa 322 clk_disable_unprepare(clk);
4bc36fd3 323
4bc36fd3 324err1:
d7dc5bde 325 platform_device_put(musb);
4bc36fd3
MYK
326
327err0:
328 return ret;
329}
330
fb4e98ab 331static int ux500_remove(struct platform_device *pdev)
4bc36fd3
MYK
332{
333 struct ux500_glue *glue = platform_get_drvdata(pdev);
334
4b0de6f3 335 platform_device_unregister(glue->musb);
99d17cfa 336 clk_disable_unprepare(glue->clk);
4bc36fd3
MYK
337
338 return 0;
339}
340
341#ifdef CONFIG_PM
342static int ux500_suspend(struct device *dev)
343{
344 struct ux500_glue *glue = dev_get_drvdata(dev);
345 struct musb *musb = glue_to_musb(glue);
346
b96d3b08 347 usb_phy_set_suspend(musb->xceiv, 1);
99d17cfa 348 clk_disable_unprepare(glue->clk);
4bc36fd3
MYK
349
350 return 0;
351}
352
353static int ux500_resume(struct device *dev)
354{
355 struct ux500_glue *glue = dev_get_drvdata(dev);
356 struct musb *musb = glue_to_musb(glue);
357 int ret;
358
99d17cfa 359 ret = clk_prepare_enable(glue->clk);
4bc36fd3
MYK
360 if (ret) {
361 dev_err(dev, "failed to enable clock\n");
362 return ret;
363 }
364
b96d3b08 365 usb_phy_set_suspend(musb->xceiv, 0);
4bc36fd3
MYK
366
367 return 0;
368}
4bc36fd3
MYK
369#endif
370
a89adb09
DM
371static SIMPLE_DEV_PM_OPS(ux500_pm_ops, ux500_suspend, ux500_resume);
372
313bdb11
LJ
373static const struct of_device_id ux500_match[] = {
374 { .compatible = "stericsson,db8500-musb", },
375 {}
376};
377
4bc36fd3 378static struct platform_driver ux500_driver = {
e9e8c85e 379 .probe = ux500_probe,
7690417d 380 .remove = ux500_remove,
4bc36fd3
MYK
381 .driver = {
382 .name = "musb-ux500",
a89adb09 383 .pm = &ux500_pm_ops,
313bdb11 384 .of_match_table = ux500_match,
4bc36fd3
MYK
385 },
386};
387
388MODULE_DESCRIPTION("UX500 MUSB Glue Layer");
389MODULE_AUTHOR("Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>");
390MODULE_LICENSE("GPL v2");
0e7090a6 391module_platform_driver(ux500_driver);
This page took 0.406034 seconds and 5 git commands to generate.