USB: add SPDX identifiers to all remaining files in drivers/usb/
[linux-2.6-block.git] / drivers / usb / musb / ux500.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0+
4bc36fd3
MYK
2/*
3 * Copyright (C) 2010 ST-Ericsson AB
4 * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
5 *
6 * Based on omap2430.c
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/module.h>
24#include <linux/kernel.h>
4bc36fd3 25#include <linux/clk.h>
ded017ee 26#include <linux/err.h>
4bc36fd3 27#include <linux/io.h>
313bdb11 28#include <linux/of.h>
4bc36fd3 29#include <linux/platform_device.h>
af6882be 30#include <linux/usb/musb-ux500.h>
4bc36fd3
MYK
31
32#include "musb_core.h"
33
1e572aa5 34static const struct musb_hdrc_config ux500_musb_hdrc_config = {
a20b1b79
LJ
35 .multipoint = true,
36 .dyn_fifo = true,
37 .num_eps = 16,
38 .ram_bits = 16,
39};
40
4bc36fd3
MYK
41struct 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
996a9d26
FB
48static 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) {
e47d9254 60 if (musb->xceiv->otg->state == OTG_STATE_A_IDLE) {
996a9d26
FB
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;
e47d9254 80 musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
996a9d26
FB
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",
e47d9254 106 usb_otg_state_string(musb->xceiv->otg->state),
996a9d26
FB
107 musb_readb(musb->mregs, MUSB_DEVCTL));
108}
109
0135522c
FB
110static 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",
e47d9254 116 event, usb_otg_state_string(musb->xceiv->otg->state));
0135522c
FB
117
118 switch (event) {
af6882be 119 case UX500_MUSB_ID:
0135522c
FB
120 dev_dbg(musb->controller, "ID GND\n");
121 ux500_musb_set_vbus(musb, 1);
122 break;
af6882be 123 case UX500_MUSB_VBUS:
0135522c 124 dev_dbg(musb->controller, "VBUS Connect\n");
0135522c 125 break;
af6882be 126 case UX500_MUSB_NONE:
0135522c
FB
127 dev_dbg(musb->controller, "VBUS Disconnect\n");
128 if (is_host_active(musb))
129 ux500_musb_set_vbus(musb, 0);
130 else
e47d9254 131 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
0135522c
FB
132 break;
133 default:
134 dev_dbg(musb->controller, "ID float\n");
135 return NOTIFY_DONE;
136 }
137 return NOTIFY_OK;
138}
139
baef653a
PDS
140static 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
4bc36fd3
MYK
160static int ux500_musb_init(struct musb *musb)
161{
0135522c
FB
162 int status;
163
662dca54 164 musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
ded017ee 165 if (IS_ERR_OR_NULL(musb->xceiv)) {
4bc36fd3 166 pr_err("HS USB OTG: no transceiver configured\n");
25736e0c 167 return -EPROBE_DEFER;
4bc36fd3
MYK
168 }
169
0135522c
FB
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;
175 }
176
baef653a
PDS
177 musb->isr = ux500_musb_interrupt;
178
4bc36fd3
MYK
179 return 0;
180}
181
182static int ux500_musb_exit(struct musb *musb)
183{
0135522c
FB
184 usb_unregister_notifier(musb->xceiv, &musb->nb);
185
721002ec 186 usb_put_phy(musb->xceiv);
4bc36fd3
MYK
187
188 return 0;
189}
190
191static const struct musb_platform_ops ux500_ops = {
f8e9f34f 192 .quirks = MUSB_DMA_UX500 | MUSB_INDEXED_EP,
7f6283ed
TL
193#ifdef CONFIG_USB_UX500_DMA
194 .dma_init = ux500_dma_controller_create,
195 .dma_exit = ux500_dma_controller_destroy,
196#endif
4bc36fd3
MYK
197 .init = ux500_musb_init,
198 .exit = ux500_musb_exit,
8a77f05a 199 .fifo_mode = 5,
996a9d26
FB
200
201 .set_vbus = ux500_musb_set_vbus,
4bc36fd3
MYK
202};
203
313bdb11
LJ
204static struct musb_hdrc_platform_data *
205ux500_of_probe(struct platform_device *pdev, struct device_node *np)
206{
207 struct musb_hdrc_platform_data *pdata;
208 const char *mode;
209 int strlen;
210
211 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
212 if (!pdata)
213 return NULL;
214
215 mode = of_get_property(np, "dr_mode", &strlen);
216 if (!mode) {
217 dev_err(&pdev->dev, "No 'dr_mode' property found\n");
218 return NULL;
219 }
220
221 if (strlen > 0) {
222 if (!strcmp(mode, "host"))
223 pdata->mode = MUSB_HOST;
224 if (!strcmp(mode, "otg"))
225 pdata->mode = MUSB_OTG;
226 if (!strcmp(mode, "peripheral"))
227 pdata->mode = MUSB_PERIPHERAL;
228 }
229
230 return pdata;
231}
232
41ac7b3a 233static int ux500_probe(struct platform_device *pdev)
4bc36fd3 234{
09fc7d22 235 struct resource musb_resources[2];
c1a7d67c 236 struct musb_hdrc_platform_data *pdata = dev_get_platdata(&pdev->dev);
313bdb11 237 struct device_node *np = pdev->dev.of_node;
4bc36fd3
MYK
238 struct platform_device *musb;
239 struct ux500_glue *glue;
240 struct clk *clk;
4bc36fd3
MYK
241 int ret = -ENOMEM;
242
313bdb11
LJ
243 if (!pdata) {
244 if (np) {
245 pdata = ux500_of_probe(pdev, np);
246 if (!pdata)
247 goto err0;
248
249 pdev->dev.platform_data = pdata;
250 } else {
251 dev_err(&pdev->dev, "no pdata or device tree found\n");
252 goto err0;
253 }
254 }
255
d7dc5bde 256 glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
24c611b9 257 if (!glue)
4bc36fd3 258 goto err0;
4bc36fd3 259
2f771164 260 musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
4bc36fd3
MYK
261 if (!musb) {
262 dev_err(&pdev->dev, "failed to allocate musb device\n");
d7dc5bde 263 goto err0;
4bc36fd3
MYK
264 }
265
d7dc5bde 266 clk = devm_clk_get(&pdev->dev, NULL);
4bc36fd3
MYK
267 if (IS_ERR(clk)) {
268 dev_err(&pdev->dev, "failed to get clock\n");
269 ret = PTR_ERR(clk);
d7dc5bde 270 goto err1;
4bc36fd3
MYK
271 }
272
99d17cfa 273 ret = clk_prepare_enable(clk);
4bc36fd3
MYK
274 if (ret) {
275 dev_err(&pdev->dev, "failed to enable clock\n");
d7dc5bde 276 goto err1;
4bc36fd3
MYK
277 }
278
279 musb->dev.parent = &pdev->dev;
1e6eebb4 280 musb->dev.dma_mask = &pdev->dev.coherent_dma_mask;
87266064 281 musb->dev.coherent_dma_mask = pdev->dev.coherent_dma_mask;
4bc36fd3
MYK
282
283 glue->dev = &pdev->dev;
284 glue->musb = musb;
285 glue->clk = clk;
286
287 pdata->platform_ops = &ux500_ops;
a20b1b79 288 pdata->config = &ux500_musb_hdrc_config;
4bc36fd3
MYK
289
290 platform_set_drvdata(pdev, glue);
291
09fc7d22
FB
292 memset(musb_resources, 0x00, sizeof(*musb_resources) *
293 ARRAY_SIZE(musb_resources));
294
295 musb_resources[0].name = pdev->resource[0].name;
296 musb_resources[0].start = pdev->resource[0].start;
297 musb_resources[0].end = pdev->resource[0].end;
298 musb_resources[0].flags = pdev->resource[0].flags;
299
300 musb_resources[1].name = pdev->resource[1].name;
301 musb_resources[1].start = pdev->resource[1].start;
302 musb_resources[1].end = pdev->resource[1].end;
303 musb_resources[1].flags = pdev->resource[1].flags;
304
305 ret = platform_device_add_resources(musb, musb_resources,
306 ARRAY_SIZE(musb_resources));
4bc36fd3
MYK
307 if (ret) {
308 dev_err(&pdev->dev, "failed to add resources\n");
d7dc5bde 309 goto err2;
4bc36fd3
MYK
310 }
311
312 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
313 if (ret) {
314 dev_err(&pdev->dev, "failed to add platform_data\n");
d7dc5bde 315 goto err2;
4bc36fd3
MYK
316 }
317
318 ret = platform_device_add(musb);
319 if (ret) {
320 dev_err(&pdev->dev, "failed to register musb device\n");
d7dc5bde 321 goto err2;
4bc36fd3
MYK
322 }
323
324 return 0;
325
d7dc5bde 326err2:
99d17cfa 327 clk_disable_unprepare(clk);
4bc36fd3 328
4bc36fd3 329err1:
d7dc5bde 330 platform_device_put(musb);
4bc36fd3
MYK
331
332err0:
333 return ret;
334}
335
fb4e98ab 336static int ux500_remove(struct platform_device *pdev)
4bc36fd3
MYK
337{
338 struct ux500_glue *glue = platform_get_drvdata(pdev);
339
4b0de6f3 340 platform_device_unregister(glue->musb);
99d17cfa 341 clk_disable_unprepare(glue->clk);
4bc36fd3
MYK
342
343 return 0;
344}
345
30d09223 346#ifdef CONFIG_PM_SLEEP
4bc36fd3
MYK
347static int ux500_suspend(struct device *dev)
348{
349 struct ux500_glue *glue = dev_get_drvdata(dev);
350 struct musb *musb = glue_to_musb(glue);
351
79c5623f
UH
352 if (musb)
353 usb_phy_set_suspend(musb->xceiv, 1);
354
99d17cfa 355 clk_disable_unprepare(glue->clk);
4bc36fd3
MYK
356
357 return 0;
358}
359
360static int ux500_resume(struct device *dev)
361{
362 struct ux500_glue *glue = dev_get_drvdata(dev);
363 struct musb *musb = glue_to_musb(glue);
364 int ret;
365
99d17cfa 366 ret = clk_prepare_enable(glue->clk);
4bc36fd3
MYK
367 if (ret) {
368 dev_err(dev, "failed to enable clock\n");
369 return ret;
370 }
371
79c5623f
UH
372 if (musb)
373 usb_phy_set_suspend(musb->xceiv, 0);
4bc36fd3
MYK
374
375 return 0;
376}
4bc36fd3
MYK
377#endif
378
a89adb09
DM
379static SIMPLE_DEV_PM_OPS(ux500_pm_ops, ux500_suspend, ux500_resume);
380
313bdb11
LJ
381static const struct of_device_id ux500_match[] = {
382 { .compatible = "stericsson,db8500-musb", },
383 {}
384};
385
523d5daf
LB
386MODULE_DEVICE_TABLE(of, ux500_match);
387
4bc36fd3 388static struct platform_driver ux500_driver = {
e9e8c85e 389 .probe = ux500_probe,
7690417d 390 .remove = ux500_remove,
4bc36fd3
MYK
391 .driver = {
392 .name = "musb-ux500",
a89adb09 393 .pm = &ux500_pm_ops,
313bdb11 394 .of_match_table = ux500_match,
4bc36fd3
MYK
395 },
396};
397
398MODULE_DESCRIPTION("UX500 MUSB Glue Layer");
399MODULE_AUTHOR("Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>");
400MODULE_LICENSE("GPL v2");
0e7090a6 401module_platform_driver(ux500_driver);