blob: 88795f532370bedd31b04e50d3bf381cbe548519 [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>
28#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
33struct ux500_glue {
34 struct device *dev;
35 struct platform_device *musb;
36 struct clk *clk;
37};
38#define glue_to_musb(g) platform_get_drvdata(g->musb)
39
Fabio Baltieri996a9d22013-03-08 10:27:06 +080040static void ux500_musb_set_vbus(struct musb *musb, int is_on)
41{
42 u8 devctl;
43 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
44 /* HDRC controls CPEN, but beware current surges during device
45 * connect. They can trigger transient overcurrent conditions
46 * that must be ignored.
47 */
48
49 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
50
51 if (is_on) {
52 if (musb->xceiv->state == OTG_STATE_A_IDLE) {
53 /* start the session */
54 devctl |= MUSB_DEVCTL_SESSION;
55 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
56 /*
57 * Wait for the musb to set as A device to enable the
58 * VBUS
59 */
60 while (musb_readb(musb->mregs, MUSB_DEVCTL) & 0x80) {
61
62 if (time_after(jiffies, timeout)) {
63 dev_err(musb->controller,
64 "configured as A device timeout");
65 break;
66 }
67 }
68
69 } else {
70 musb->is_active = 1;
71 musb->xceiv->otg->default_a = 1;
72 musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
73 devctl |= MUSB_DEVCTL_SESSION;
74 MUSB_HST_MODE(musb);
75 }
76 } else {
77 musb->is_active = 0;
78
79 /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and jumping
80 * right to B_IDLE...
81 */
82 musb->xceiv->otg->default_a = 0;
83 devctl &= ~MUSB_DEVCTL_SESSION;
84 MUSB_DEV_MODE(musb);
85 }
86 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
87
88 /*
89 * Devctl values will be updated after vbus goes below
90 * session_valid. The time taken depends on the capacitance
91 * on VBUS line. The max discharge time can be upto 1 sec
92 * as per the spec. Typically on our platform, it is 200ms
93 */
94 if (!is_on)
95 mdelay(200);
96
97 dev_dbg(musb->controller, "VBUS %s, devctl %02x\n",
98 usb_otg_state_string(musb->xceiv->state),
99 musb_readb(musb->mregs, MUSB_DEVCTL));
100}
101
Fabio Baltieri01355222013-03-08 10:27:07 +0800102static int musb_otg_notifications(struct notifier_block *nb,
103 unsigned long event, void *unused)
104{
105 struct musb *musb = container_of(nb, struct musb, nb);
106
107 dev_dbg(musb->controller, "musb_otg_notifications %ld %s\n",
108 event, usb_otg_state_string(musb->xceiv->state));
109
110 switch (event) {
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800111 case UX500_MUSB_ID:
Fabio Baltieri01355222013-03-08 10:27:07 +0800112 dev_dbg(musb->controller, "ID GND\n");
113 ux500_musb_set_vbus(musb, 1);
114 break;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800115 case UX500_MUSB_VBUS:
Fabio Baltieri01355222013-03-08 10:27:07 +0800116 dev_dbg(musb->controller, "VBUS Connect\n");
117 ux500_musb_set_vbus(musb, 0);
118 break;
Fabio Baltieriaf6882b2013-03-08 10:27:09 +0800119 case UX500_MUSB_NONE:
Fabio Baltieri01355222013-03-08 10:27:07 +0800120 dev_dbg(musb->controller, "VBUS Disconnect\n");
121 if (is_host_active(musb))
122 ux500_musb_set_vbus(musb, 0);
123 else
124 musb->xceiv->state = OTG_STATE_B_IDLE;
125 break;
126 default:
127 dev_dbg(musb->controller, "ID float\n");
128 return NOTIFY_DONE;
129 }
130 return NOTIFY_OK;
131}
132
Philippe De Swertbaef6532012-11-06 15:32:13 +0200133static irqreturn_t ux500_musb_interrupt(int irq, void *__hci)
134{
135 unsigned long flags;
136 irqreturn_t retval = IRQ_NONE;
137 struct musb *musb = __hci;
138
139 spin_lock_irqsave(&musb->lock, flags);
140
141 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
142 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
143 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
144
145 if (musb->int_usb || musb->int_tx || musb->int_rx)
146 retval = musb_interrupt(musb);
147
148 spin_unlock_irqrestore(&musb->lock, flags);
149
150 return retval;
151}
152
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100153static int ux500_musb_init(struct musb *musb)
154{
Fabio Baltieri01355222013-03-08 10:27:07 +0800155 int status;
156
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530157 musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530158 if (IS_ERR_OR_NULL(musb->xceiv)) {
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100159 pr_err("HS USB OTG: no transceiver configured\n");
Ming Lei25736e02013-01-04 23:13:58 +0800160 return -EPROBE_DEFER;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100161 }
162
Fabio Baltieri01355222013-03-08 10:27:07 +0800163 musb->nb.notifier_call = musb_otg_notifications;
164 status = usb_register_notifier(musb->xceiv, &musb->nb);
165 if (status < 0) {
166 dev_dbg(musb->controller, "notification register failed\n");
167 return status;
168 }
169
Philippe De Swertbaef6532012-11-06 15:32:13 +0200170 musb->isr = ux500_musb_interrupt;
171
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100172 return 0;
173}
174
175static int ux500_musb_exit(struct musb *musb)
176{
Fabio Baltieri01355222013-03-08 10:27:07 +0800177 usb_unregister_notifier(musb->xceiv, &musb->nb);
178
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530179 usb_put_phy(musb->xceiv);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100180
181 return 0;
182}
183
184static const struct musb_platform_ops ux500_ops = {
185 .init = ux500_musb_init,
186 .exit = ux500_musb_exit,
Fabio Baltieri996a9d22013-03-08 10:27:06 +0800187
188 .set_vbus = ux500_musb_set_vbus,
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100189};
190
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500191static int ux500_probe(struct platform_device *pdev)
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100192{
193 struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
194 struct platform_device *musb;
195 struct ux500_glue *glue;
196 struct clk *clk;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100197 int ret = -ENOMEM;
198
199 glue = kzalloc(sizeof(*glue), GFP_KERNEL);
200 if (!glue) {
201 dev_err(&pdev->dev, "failed to allocate glue context\n");
202 goto err0;
203 }
204
Sebastian Andrzej Siewior2f771162012-10-31 16:12:43 +0100205 musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100206 if (!musb) {
207 dev_err(&pdev->dev, "failed to allocate musb device\n");
Sebastian Andrzej Siewior2f771162012-10-31 16:12:43 +0100208 goto err1;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100209 }
210
211 clk = clk_get(&pdev->dev, "usb");
212 if (IS_ERR(clk)) {
213 dev_err(&pdev->dev, "failed to get clock\n");
214 ret = PTR_ERR(clk);
B, Ravi65b3d522012-08-31 11:09:49 +0000215 goto err3;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100216 }
217
Fabio Baltieri99d17cf2013-01-07 17:47:41 +0100218 ret = clk_prepare_enable(clk);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100219 if (ret) {
220 dev_err(&pdev->dev, "failed to enable clock\n");
B, Ravi65b3d522012-08-31 11:09:49 +0000221 goto err4;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100222 }
223
224 musb->dev.parent = &pdev->dev;
Mian Yousaf Kaukab87266062011-03-15 16:24:29 +0100225 musb->dev.dma_mask = pdev->dev.dma_mask;
226 musb->dev.coherent_dma_mask = pdev->dev.coherent_dma_mask;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100227
228 glue->dev = &pdev->dev;
229 glue->musb = musb;
230 glue->clk = clk;
231
232 pdata->platform_ops = &ux500_ops;
233
234 platform_set_drvdata(pdev, glue);
235
236 ret = platform_device_add_resources(musb, pdev->resource,
237 pdev->num_resources);
238 if (ret) {
239 dev_err(&pdev->dev, "failed to add resources\n");
B, Ravi65b3d522012-08-31 11:09:49 +0000240 goto err5;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100241 }
242
243 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
244 if (ret) {
245 dev_err(&pdev->dev, "failed to add platform_data\n");
B, Ravi65b3d522012-08-31 11:09:49 +0000246 goto err5;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100247 }
248
249 ret = platform_device_add(musb);
250 if (ret) {
251 dev_err(&pdev->dev, "failed to register musb device\n");
B, Ravi65b3d522012-08-31 11:09:49 +0000252 goto err5;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100253 }
254
255 return 0;
256
B, Ravi65b3d522012-08-31 11:09:49 +0000257err5:
Fabio Baltieri99d17cf2013-01-07 17:47:41 +0100258 clk_disable_unprepare(clk);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100259
B, Ravi65b3d522012-08-31 11:09:49 +0000260err4:
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100261 clk_put(clk);
262
B, Ravi65b3d522012-08-31 11:09:49 +0000263err3:
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100264 platform_device_put(musb);
265
266err1:
267 kfree(glue);
268
269err0:
270 return ret;
271}
272
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500273static int ux500_remove(struct platform_device *pdev)
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100274{
275 struct ux500_glue *glue = platform_get_drvdata(pdev);
276
Wei Yongjun4b0de6f2012-10-23 13:36:43 +0800277 platform_device_unregister(glue->musb);
Fabio Baltieri99d17cf2013-01-07 17:47:41 +0100278 clk_disable_unprepare(glue->clk);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100279 clk_put(glue->clk);
280 kfree(glue);
281
282 return 0;
283}
284
285#ifdef CONFIG_PM
286static int ux500_suspend(struct device *dev)
287{
288 struct ux500_glue *glue = dev_get_drvdata(dev);
289 struct musb *musb = glue_to_musb(glue);
290
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200291 usb_phy_set_suspend(musb->xceiv, 1);
Fabio Baltieri99d17cf2013-01-07 17:47:41 +0100292 clk_disable_unprepare(glue->clk);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100293
294 return 0;
295}
296
297static int ux500_resume(struct device *dev)
298{
299 struct ux500_glue *glue = dev_get_drvdata(dev);
300 struct musb *musb = glue_to_musb(glue);
301 int ret;
302
Fabio Baltieri99d17cf2013-01-07 17:47:41 +0100303 ret = clk_prepare_enable(glue->clk);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100304 if (ret) {
305 dev_err(dev, "failed to enable clock\n");
306 return ret;
307 }
308
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200309 usb_phy_set_suspend(musb->xceiv, 0);
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100310
311 return 0;
312}
313
314static const struct dev_pm_ops ux500_pm_ops = {
315 .suspend = ux500_suspend,
316 .resume = ux500_resume,
317};
318
319#define DEV_PM_OPS (&ux500_pm_ops)
320#else
321#define DEV_PM_OPS NULL
322#endif
323
324static struct platform_driver ux500_driver = {
Felipe Balbie9e8c852012-01-26 12:40:23 +0200325 .probe = ux500_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500326 .remove = ux500_remove,
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +0100327 .driver = {
328 .name = "musb-ux500",
329 .pm = DEV_PM_OPS,
330 },
331};
332
333MODULE_DESCRIPTION("UX500 MUSB Glue Layer");
334MODULE_AUTHOR("Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>");
335MODULE_LICENSE("GPL v2");
Srinivas Kandagatla0e7090a2012-10-10 19:37:21 +0100336module_platform_driver(ux500_driver);