blob: f18c03795ed55f131b2636aaf2e581be80fcbb73 [file] [log] [blame]
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +01001/*
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>
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +010024#include <linux/clk.h>
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +053025#include <linux/err.h>
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +010026#include <linux/io.h>
Lee Jones313bdb112013-05-15 10:51:48 +010027#include <linux/of.h>
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +010028#include <linux/platform_device.h>
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080029#include <linux/usb/musb-ux500.h>
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +010030
31#include "musb_core.h"
32
Lee Jonesa20b1b72013-05-15 10:51:44 +010033static 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
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +010040struct 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
Fabio Baltieri996a9d22013-03-08 10:27:06 +080047static 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) {
59 if (musb->xceiv->state == OTG_STATE_A_IDLE) {
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;
79 musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
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",
105 usb_otg_state_string(musb->xceiv->state),
106 musb_readb(musb->mregs, MUSB_DEVCTL));
107}
108
Fabio Baltieri01355222013-03-08 10:27:07 +0800109static 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",
115 event, usb_otg_state_string(musb->xceiv->state));
116
117 switch (event) {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800118 case UX500_MUSB_ID:
Fabio Baltieri01355222013-03-08 10:27:07 +0800119 dev_dbg(musb->controller, "ID GND\n");
120 ux500_musb_set_vbus(musb, 1);
121 break;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800122 case UX500_MUSB_VBUS:
Fabio Baltieri01355222013-03-08 10:27:07 +0800123 dev_dbg(musb->controller, "VBUS Connect\n");
Fabio Baltieri01355222013-03-08 10:27:07 +0800124 break;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800125 case UX500_MUSB_NONE:
Fabio Baltieri01355222013-03-08 10:27:07 +0800126 dev_dbg(musb->controller, "VBUS Disconnect\n");
127 if (is_host_active(musb))
128 ux500_musb_set_vbus(musb, 0);
129 else
130 musb->xceiv->state = OTG_STATE_B_IDLE;
131 break;
132 default:
133 dev_dbg(musb->controller, "ID float\n");
134 return NOTIFY_DONE;
135 }
136 return NOTIFY_OK;
137}
138
Philippe De Swertbaef6532012-11-06 15:32:13 +0200139static 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
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100159static int ux500_musb_init(struct musb *musb)
160{
Fabio Baltieri01355222013-03-08 10:27:07 +0800161 int status;
162
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530163 musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530164 if (IS_ERR_OR_NULL(musb->xceiv)) {
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100165 pr_err("HS USB OTG: no transceiver configured\n");
Ming Lei25736e02013-01-04 23:13:58 +0800166 return -EPROBE_DEFER;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100167 }
168
Fabio Baltieri01355222013-03-08 10:27:07 +0800169 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
Philippe De Swertbaef6532012-11-06 15:32:13 +0200176 musb->isr = ux500_musb_interrupt;
177
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100178 return 0;
179}
180
181static int ux500_musb_exit(struct musb *musb)
182{
Fabio Baltieri01355222013-03-08 10:27:07 +0800183 usb_unregister_notifier(musb->xceiv, &musb->nb);
184
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530185 usb_put_phy(musb->xceiv);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100186
187 return 0;
188}
189
190static const struct musb_platform_ops ux500_ops = {
191 .init = ux500_musb_init,
192 .exit = ux500_musb_exit,
Fabio Baltieri996a9d22013-03-08 10:27:06 +0800193
194 .set_vbus = ux500_musb_set_vbus,
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100195};
196
Lee Jones313bdb112013-05-15 10:51:48 +0100197static struct musb_hdrc_platform_data *
198ux500_of_probe(struct platform_device *pdev, struct device_node *np)
199{
200 struct musb_hdrc_platform_data *pdata;
201 const char *mode;
202 int strlen;
203
204 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
205 if (!pdata)
206 return NULL;
207
208 mode = of_get_property(np, "dr_mode", &strlen);
209 if (!mode) {
210 dev_err(&pdev->dev, "No 'dr_mode' property found\n");
211 return NULL;
212 }
213
214 if (strlen > 0) {
215 if (!strcmp(mode, "host"))
216 pdata->mode = MUSB_HOST;
217 if (!strcmp(mode, "otg"))
218 pdata->mode = MUSB_OTG;
219 if (!strcmp(mode, "peripheral"))
220 pdata->mode = MUSB_PERIPHERAL;
221 }
222
223 return pdata;
224}
225
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500226static int ux500_probe(struct platform_device *pdev)
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100227{
Felipe Balbi09fc7d22013-04-24 17:21:42 +0300228 struct resource musb_resources[2];
Jingoo Hanc1a7d672013-07-30 17:03:12 +0900229 struct musb_hdrc_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lee Jones313bdb112013-05-15 10:51:48 +0100230 struct device_node *np = pdev->dev.of_node;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100231 struct platform_device *musb;
232 struct ux500_glue *glue;
233 struct clk *clk;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100234 int ret = -ENOMEM;
235
Lee Jones313bdb112013-05-15 10:51:48 +0100236 if (!pdata) {
237 if (np) {
238 pdata = ux500_of_probe(pdev, np);
239 if (!pdata)
240 goto err0;
241
242 pdev->dev.platform_data = pdata;
243 } else {
244 dev_err(&pdev->dev, "no pdata or device tree found\n");
245 goto err0;
246 }
247 }
248
Himangi Saraogid7dc5bd2014-06-21 02:10:25 +0530249 glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100250 if (!glue) {
251 dev_err(&pdev->dev, "failed to allocate glue context\n");
252 goto err0;
253 }
254
Sebastian Andrzej Siewior2f771162012-10-31 16:12:43 +0100255 musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100256 if (!musb) {
257 dev_err(&pdev->dev, "failed to allocate musb device\n");
Himangi Saraogid7dc5bd2014-06-21 02:10:25 +0530258 goto err0;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100259 }
260
Himangi Saraogid7dc5bd2014-06-21 02:10:25 +0530261 clk = devm_clk_get(&pdev->dev, NULL);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100262 if (IS_ERR(clk)) {
263 dev_err(&pdev->dev, "failed to get clock\n");
264 ret = PTR_ERR(clk);
Himangi Saraogid7dc5bd2014-06-21 02:10:25 +0530265 goto err1;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100266 }
267
Fabio Baltieri99d17cf2013-01-07 17:47:41 +0100268 ret = clk_prepare_enable(clk);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100269 if (ret) {
270 dev_err(&pdev->dev, "failed to enable clock\n");
Himangi Saraogid7dc5bd2014-06-21 02:10:25 +0530271 goto err1;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100272 }
273
274 musb->dev.parent = &pdev->dev;
Lee Jones1e6eebb2013-05-15 10:51:45 +0100275 musb->dev.dma_mask = &pdev->dev.coherent_dma_mask;
Mian Yousaf Kaukab87266062011-03-15 16:24:29 +0100276 musb->dev.coherent_dma_mask = pdev->dev.coherent_dma_mask;
Lee Jones313bdb112013-05-15 10:51:48 +0100277 musb->dev.of_node = pdev->dev.of_node;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100278
279 glue->dev = &pdev->dev;
280 glue->musb = musb;
281 glue->clk = clk;
282
283 pdata->platform_ops = &ux500_ops;
Lee Jonesa20b1b72013-05-15 10:51:44 +0100284 pdata->config = &ux500_musb_hdrc_config;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100285
286 platform_set_drvdata(pdev, glue);
287
Felipe Balbi09fc7d22013-04-24 17:21:42 +0300288 memset(musb_resources, 0x00, sizeof(*musb_resources) *
289 ARRAY_SIZE(musb_resources));
290
291 musb_resources[0].name = pdev->resource[0].name;
292 musb_resources[0].start = pdev->resource[0].start;
293 musb_resources[0].end = pdev->resource[0].end;
294 musb_resources[0].flags = pdev->resource[0].flags;
295
296 musb_resources[1].name = pdev->resource[1].name;
297 musb_resources[1].start = pdev->resource[1].start;
298 musb_resources[1].end = pdev->resource[1].end;
299 musb_resources[1].flags = pdev->resource[1].flags;
300
301 ret = platform_device_add_resources(musb, musb_resources,
302 ARRAY_SIZE(musb_resources));
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100303 if (ret) {
304 dev_err(&pdev->dev, "failed to add resources\n");
Himangi Saraogid7dc5bd2014-06-21 02:10:25 +0530305 goto err2;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100306 }
307
308 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
309 if (ret) {
310 dev_err(&pdev->dev, "failed to add platform_data\n");
Himangi Saraogid7dc5bd2014-06-21 02:10:25 +0530311 goto err2;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100312 }
313
314 ret = platform_device_add(musb);
315 if (ret) {
316 dev_err(&pdev->dev, "failed to register musb device\n");
Himangi Saraogid7dc5bd2014-06-21 02:10:25 +0530317 goto err2;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100318 }
319
320 return 0;
321
Himangi Saraogid7dc5bd2014-06-21 02:10:25 +0530322err2:
Fabio Baltieri99d17cf2013-01-07 17:47:41 +0100323 clk_disable_unprepare(clk);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100324
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100325err1:
Himangi Saraogid7dc5bd2014-06-21 02:10:25 +0530326 platform_device_put(musb);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100327
328err0:
329 return ret;
330}
331
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500332static int ux500_remove(struct platform_device *pdev)
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100333{
334 struct ux500_glue *glue = platform_get_drvdata(pdev);
335
Wei Yongjun4b0de6f2012-10-23 13:36:43 +0800336 platform_device_unregister(glue->musb);
Fabio Baltieri99d17cf2013-01-07 17:47:41 +0100337 clk_disable_unprepare(glue->clk);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100338
339 return 0;
340}
341
342#ifdef CONFIG_PM
343static int ux500_suspend(struct device *dev)
344{
345 struct ux500_glue *glue = dev_get_drvdata(dev);
346 struct musb *musb = glue_to_musb(glue);
347
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200348 usb_phy_set_suspend(musb->xceiv, 1);
Fabio Baltieri99d17cf2013-01-07 17:47:41 +0100349 clk_disable_unprepare(glue->clk);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100350
351 return 0;
352}
353
354static int ux500_resume(struct device *dev)
355{
356 struct ux500_glue *glue = dev_get_drvdata(dev);
357 struct musb *musb = glue_to_musb(glue);
358 int ret;
359
Fabio Baltieri99d17cf2013-01-07 17:47:41 +0100360 ret = clk_prepare_enable(glue->clk);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100361 if (ret) {
362 dev_err(dev, "failed to enable clock\n");
363 return ret;
364 }
365
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200366 usb_phy_set_suspend(musb->xceiv, 0);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100367
368 return 0;
369}
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100370#endif
371
Daniel Macka89adb02013-09-30 21:02:09 +0200372static SIMPLE_DEV_PM_OPS(ux500_pm_ops, ux500_suspend, ux500_resume);
373
Lee Jones313bdb112013-05-15 10:51:48 +0100374static const struct of_device_id ux500_match[] = {
375 { .compatible = "stericsson,db8500-musb", },
376 {}
377};
378
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100379static struct platform_driver ux500_driver = {
Felipe Balbie9e8c852012-01-26 12:40:23 +0200380 .probe = ux500_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500381 .remove = ux500_remove,
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100382 .driver = {
383 .name = "musb-ux500",
Daniel Macka89adb02013-09-30 21:02:09 +0200384 .pm = &ux500_pm_ops,
Lee Jones313bdb112013-05-15 10:51:48 +0100385 .of_match_table = ux500_match,
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100386 },
387};
388
389MODULE_DESCRIPTION("UX500 MUSB Glue Layer");
390MODULE_AUTHOR("Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>");
391MODULE_LICENSE("GPL v2");
Srinivas Kandagatla0e7090a2012-10-10 19:37:21 +0100392module_platform_driver(ux500_driver);