blob: f0beee7b868e817a7c6d4ca325f8f42917992782 [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>
24#include <linux/init.h>
25#include <linux/clk.h>
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +053026#include <linux/err.h>
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +010027#include <linux/io.h>
Lee Jones313bdb112013-05-15 10:51:48 +010028#include <linux/of.h>
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +010029#include <linux/platform_device.h>
Fabio Baltieriaf6882b2013-03-08 10:27:09 +080030#include <linux/usb/musb-ux500.h>
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +010031
32#include "musb_core.h"
33
Lee Jonesa20b1b72013-05-15 10:51:44 +010034static struct musb_hdrc_config ux500_musb_hdrc_config = {
35 .multipoint = true,
36 .dyn_fifo = true,
37 .num_eps = 16,
38 .ram_bits = 16,
39};
40
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +010041struct ux500_glue {
42 struct device *dev;
43 struct platform_device *musb;
44 struct clk *clk;
45};
46#define glue_to_musb(g) platform_get_drvdata(g->musb)
47
Fabio Baltieri996a9d22013-03-08 10:27:06 +080048static void ux500_musb_set_vbus(struct musb *musb, int is_on)
49{
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.
55 */
56
57 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
58
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);
64 /*
65 * Wait for the musb to set as A device to enable the
66 * VBUS
67 */
68 while (musb_readb(musb->mregs, MUSB_DEVCTL) & 0x80) {
69
70 if (time_after(jiffies, timeout)) {
71 dev_err(musb->controller,
72 "configured as A device timeout");
73 break;
74 }
75 }
76
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);
83 }
84 } else {
85 musb->is_active = 0;
86
87 /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and jumping
88 * right to B_IDLE...
89 */
90 musb->xceiv->otg->default_a = 0;
91 devctl &= ~MUSB_DEVCTL_SESSION;
92 MUSB_DEV_MODE(musb);
93 }
94 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
95
96 /*
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
101 */
102 if (!is_on)
103 mdelay(200);
104
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));
108}
109
Fabio Baltieri01355222013-03-08 10:27:07 +0800110static int musb_otg_notifications(struct notifier_block *nb,
111 unsigned long event, void *unused)
112{
113 struct musb *musb = container_of(nb, struct musb, nb);
114
115 dev_dbg(musb->controller, "musb_otg_notifications %ld %s\n",
116 event, usb_otg_state_string(musb->xceiv->state));
117
118 switch (event) {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800119 case UX500_MUSB_ID:
Fabio Baltieri01355222013-03-08 10:27:07 +0800120 dev_dbg(musb->controller, "ID GND\n");
121 ux500_musb_set_vbus(musb, 1);
122 break;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800123 case UX500_MUSB_VBUS:
Fabio Baltieri01355222013-03-08 10:27:07 +0800124 dev_dbg(musb->controller, "VBUS Connect\n");
Fabio Baltieri01355222013-03-08 10:27:07 +0800125 break;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800126 case UX500_MUSB_NONE:
Fabio Baltieri01355222013-03-08 10:27:07 +0800127 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;
136 }
137 return NOTIFY_OK;
138}
139
Philippe De Swertbaef6532012-11-06 15:32:13 +0200140static irqreturn_t ux500_musb_interrupt(int irq, void *__hci)
141{
142 unsigned long flags;
143 irqreturn_t retval = IRQ_NONE;
144 struct musb *musb = __hci;
145
146 spin_lock_irqsave(&musb->lock, flags);
147
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);
151
152 if (musb->int_usb || musb->int_tx || musb->int_rx)
153 retval = musb_interrupt(musb);
154
155 spin_unlock_irqrestore(&musb->lock, flags);
156
157 return retval;
158}
159
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100160static int ux500_musb_init(struct musb *musb)
161{
Fabio Baltieri01355222013-03-08 10:27:07 +0800162 int status;
163
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530164 musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530165 if (IS_ERR_OR_NULL(musb->xceiv)) {
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100166 pr_err("HS USB OTG: no transceiver configured\n");
Ming Lei25736e02013-01-04 23:13:58 +0800167 return -EPROBE_DEFER;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100168 }
169
Fabio Baltieri01355222013-03-08 10:27:07 +0800170 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;
175 }
176
Philippe De Swertbaef6532012-11-06 15:32:13 +0200177 musb->isr = ux500_musb_interrupt;
178
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100179 return 0;
180}
181
182static int ux500_musb_exit(struct musb *musb)
183{
Fabio Baltieri01355222013-03-08 10:27:07 +0800184 usb_unregister_notifier(musb->xceiv, &musb->nb);
185
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530186 usb_put_phy(musb->xceiv);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100187
188 return 0;
189}
190
191static const struct musb_platform_ops ux500_ops = {
192 .init = ux500_musb_init,
193 .exit = ux500_musb_exit,
Fabio Baltieri996a9d22013-03-08 10:27:06 +0800194
195 .set_vbus = ux500_musb_set_vbus,
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100196};
197
Lee Jones313bdb112013-05-15 10:51:48 +0100198static struct musb_hdrc_platform_data *
199ux500_of_probe(struct platform_device *pdev, struct device_node *np)
200{
201 struct musb_hdrc_platform_data *pdata;
202 const char *mode;
203 int strlen;
204
205 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
206 if (!pdata)
207 return NULL;
208
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;
213 }
214
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;
222 }
223
224 return pdata;
225}
226
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500227static int ux500_probe(struct platform_device *pdev)
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100228{
229 struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
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
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100249 glue = kzalloc(sizeof(*glue), GFP_KERNEL);
250 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");
Sebastian Andrzej Siewior2f771162012-10-31 16:12:43 +0100258 goto err1;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100259 }
260
261 clk = clk_get(&pdev->dev, "usb");
262 if (IS_ERR(clk)) {
263 dev_err(&pdev->dev, "failed to get clock\n");
264 ret = PTR_ERR(clk);
B, Ravi65b3d522012-08-31 11:09:49 +0000265 goto err3;
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");
B, Ravi65b3d522012-08-31 11:09:49 +0000271 goto err4;
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
288 ret = platform_device_add_resources(musb, pdev->resource,
289 pdev->num_resources);
290 if (ret) {
291 dev_err(&pdev->dev, "failed to add resources\n");
B, Ravi65b3d522012-08-31 11:09:49 +0000292 goto err5;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100293 }
294
295 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
296 if (ret) {
297 dev_err(&pdev->dev, "failed to add platform_data\n");
B, Ravi65b3d522012-08-31 11:09:49 +0000298 goto err5;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100299 }
300
301 ret = platform_device_add(musb);
302 if (ret) {
303 dev_err(&pdev->dev, "failed to register musb device\n");
B, Ravi65b3d522012-08-31 11:09:49 +0000304 goto err5;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100305 }
306
307 return 0;
308
B, Ravi65b3d522012-08-31 11:09:49 +0000309err5:
Fabio Baltieri99d17cf2013-01-07 17:47:41 +0100310 clk_disable_unprepare(clk);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100311
B, Ravi65b3d522012-08-31 11:09:49 +0000312err4:
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100313 clk_put(clk);
314
B, Ravi65b3d522012-08-31 11:09:49 +0000315err3:
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100316 platform_device_put(musb);
317
318err1:
319 kfree(glue);
320
321err0:
322 return ret;
323}
324
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500325static int ux500_remove(struct platform_device *pdev)
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100326{
327 struct ux500_glue *glue = platform_get_drvdata(pdev);
328
Wei Yongjun4b0de6f2012-10-23 13:36:43 +0800329 platform_device_unregister(glue->musb);
Fabio Baltieri99d17cf2013-01-07 17:47:41 +0100330 clk_disable_unprepare(glue->clk);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100331 clk_put(glue->clk);
332 kfree(glue);
333
334 return 0;
335}
336
337#ifdef CONFIG_PM
338static int ux500_suspend(struct device *dev)
339{
340 struct ux500_glue *glue = dev_get_drvdata(dev);
341 struct musb *musb = glue_to_musb(glue);
342
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200343 usb_phy_set_suspend(musb->xceiv, 1);
Fabio Baltieri99d17cf2013-01-07 17:47:41 +0100344 clk_disable_unprepare(glue->clk);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100345
346 return 0;
347}
348
349static int ux500_resume(struct device *dev)
350{
351 struct ux500_glue *glue = dev_get_drvdata(dev);
352 struct musb *musb = glue_to_musb(glue);
353 int ret;
354
Fabio Baltieri99d17cf2013-01-07 17:47:41 +0100355 ret = clk_prepare_enable(glue->clk);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100356 if (ret) {
357 dev_err(dev, "failed to enable clock\n");
358 return ret;
359 }
360
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200361 usb_phy_set_suspend(musb->xceiv, 0);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100362
363 return 0;
364}
365
366static const struct dev_pm_ops ux500_pm_ops = {
367 .suspend = ux500_suspend,
368 .resume = ux500_resume,
369};
370
371#define DEV_PM_OPS (&ux500_pm_ops)
372#else
373#define DEV_PM_OPS NULL
374#endif
375
Lee Jones313bdb112013-05-15 10:51:48 +0100376static const struct of_device_id ux500_match[] = {
377 { .compatible = "stericsson,db8500-musb", },
378 {}
379};
380
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100381static struct platform_driver ux500_driver = {
Felipe Balbie9e8c852012-01-26 12:40:23 +0200382 .probe = ux500_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500383 .remove = ux500_remove,
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100384 .driver = {
385 .name = "musb-ux500",
386 .pm = DEV_PM_OPS,
Lee Jones313bdb112013-05-15 10:51:48 +0100387 .of_match_table = ux500_match,
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100388 },
389};
390
391MODULE_DESCRIPTION("UX500 MUSB Glue Layer");
392MODULE_AUTHOR("Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>");
393MODULE_LICENSE("GPL v2");
Srinivas Kandagatla0e7090a2012-10-10 19:37:21 +0100394module_platform_driver(ux500_driver);