ohci-platform: Add support for devicetree instantiation
[pandora-kernel.git] / drivers / usb / host / ohci-platform.c
1 /*
2  * Generic platform ohci driver
3  *
4  * Copyright 2007 Michael Buesch <m@bues.ch>
5  * Copyright 2011-2012 Hauke Mehrtens <hauke@hauke-m.de>
6  * Copyright 2014 Hans de Goede <hdegoede@redhat.com>
7  *
8  * Derived from the OCHI-SSB driver
9  * Derived from the OHCI-PCI driver
10  * Copyright 1999 Roman Weissgaerber
11  * Copyright 2000-2002 David Brownell
12  * Copyright 1999 Linus Torvalds
13  * Copyright 1999 Gregory P. Smith
14  *
15  * Licensed under the GNU/GPL. See COPYING for details.
16  */
17
18 #include <linux/clk.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/hrtimer.h>
21 #include <linux/io.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/err.h>
25 #include <linux/phy/phy.h>
26 #include <linux/platform_device.h>
27 #include <linux/usb/ohci_pdriver.h>
28 #include <linux/usb.h>
29 #include <linux/usb/hcd.h>
30
31 #include "ohci.h"
32
33 #define DRIVER_DESC "OHCI generic platform driver"
34 #define OHCI_MAX_CLKS 3
35 #define hcd_to_ohci_priv(h) ((struct ohci_platform_priv *)hcd_to_ohci(h)->priv)
36
37 struct ohci_platform_priv {
38         struct clk *clks[OHCI_MAX_CLKS];
39         struct phy *phy;
40 };
41
42 static const char hcd_name[] = "ohci-platform";
43
44 static int ohci_platform_reset(struct usb_hcd *hcd)
45 {
46         struct platform_device *pdev = to_platform_device(hcd->self.controller);
47         struct usb_ohci_pdata *pdata = dev_get_platdata(&pdev->dev);
48         struct ohci_hcd *ohci = hcd_to_ohci(hcd);
49
50         if (pdata->big_endian_desc)
51                 ohci->flags |= OHCI_QUIRK_BE_DESC;
52         if (pdata->big_endian_mmio)
53                 ohci->flags |= OHCI_QUIRK_BE_MMIO;
54         if (pdata->no_big_frame_no)
55                 ohci->flags |= OHCI_QUIRK_FRAME_NO;
56         if (pdata->num_ports)
57                 ohci->num_ports = pdata->num_ports;
58
59         return ohci_setup(hcd);
60 }
61
62 static int ohci_platform_power_on(struct platform_device *dev)
63 {
64         struct usb_hcd *hcd = platform_get_drvdata(dev);
65         struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
66         int clk, ret;
67
68         for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) {
69                 ret = clk_prepare_enable(priv->clks[clk]);
70                 if (ret)
71                         goto err_disable_clks;
72         }
73
74         if (priv->phy) {
75                 ret = phy_init(priv->phy);
76                 if (ret)
77                         goto err_disable_clks;
78
79                 ret = phy_power_on(priv->phy);
80                 if (ret)
81                         goto err_exit_phy;
82         }
83
84         return 0;
85
86 err_exit_phy:
87         phy_exit(priv->phy);
88 err_disable_clks:
89         while (--clk >= 0)
90                 clk_disable_unprepare(priv->clks[clk]);
91
92         return ret;
93 }
94
95 static void ohci_platform_power_off(struct platform_device *dev)
96 {
97         struct usb_hcd *hcd = platform_get_drvdata(dev);
98         struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
99         int clk;
100
101         if (priv->phy) {
102                 phy_power_off(priv->phy);
103                 phy_exit(priv->phy);
104         }
105
106         for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
107                 if (priv->clks[clk])
108                         clk_disable_unprepare(priv->clks[clk]);
109 }
110
111 static struct hc_driver __read_mostly ohci_platform_hc_driver;
112
113 static const struct ohci_driver_overrides platform_overrides __initconst = {
114         .product_desc =         "Generic Platform OHCI controller",
115         .reset =                ohci_platform_reset,
116         .extra_priv_size =      sizeof(struct ohci_platform_priv),
117 };
118
119 static struct usb_ohci_pdata ohci_platform_defaults = {
120         .power_on =             ohci_platform_power_on,
121         .power_suspend =        ohci_platform_power_off,
122         .power_off =            ohci_platform_power_off,
123 };
124
125 static int ohci_platform_probe(struct platform_device *dev)
126 {
127         struct usb_hcd *hcd;
128         struct resource *res_mem;
129         struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
130         struct ohci_platform_priv *priv;
131         int err, irq, clk = 0;
132
133         if (usb_disabled())
134                 return -ENODEV;
135
136         /*
137          * Use reasonable defaults so platforms don't have to provide these
138          * with DT probing on ARM.
139          */
140         if (!pdata)
141                 pdata = &ohci_platform_defaults;
142
143         err = dma_coerce_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
144         if (err)
145                 return err;
146
147         irq = platform_get_irq(dev, 0);
148         if (irq < 0) {
149                 dev_err(&dev->dev, "no irq provided");
150                 return irq;
151         }
152
153         res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
154         if (!res_mem) {
155                 dev_err(&dev->dev, "no memory resource provided");
156                 return -ENXIO;
157         }
158
159         hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
160                         dev_name(&dev->dev));
161         if (!hcd)
162                 return -ENOMEM;
163
164         platform_set_drvdata(dev, hcd);
165         dev->dev.platform_data = pdata;
166         priv = hcd_to_ohci_priv(hcd);
167
168         if (pdata == &ohci_platform_defaults && dev->dev.of_node) {
169                 priv->phy = devm_phy_get(&dev->dev, "usb");
170                 if (IS_ERR(priv->phy)) {
171                         err = PTR_ERR(priv->phy);
172                         if (err == -EPROBE_DEFER)
173                                 goto err_put_hcd;
174                         priv->phy = NULL;
175                 }
176
177                 for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
178                         priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
179                         if (IS_ERR(priv->clks[clk])) {
180                                 err = PTR_ERR(priv->clks[clk]);
181                                 if (err == -EPROBE_DEFER)
182                                         goto err_put_clks;
183                                 priv->clks[clk] = NULL;
184                                 break;
185                         }
186                 }
187         }
188
189         if (pdata->power_on) {
190                 err = pdata->power_on(dev);
191                 if (err < 0)
192                         goto err_put_clks;
193         }
194
195         hcd->rsrc_start = res_mem->start;
196         hcd->rsrc_len = resource_size(res_mem);
197
198         hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
199         if (IS_ERR(hcd->regs)) {
200                 err = PTR_ERR(hcd->regs);
201                 goto err_power;
202         }
203         err = usb_add_hcd(hcd, irq, IRQF_SHARED);
204         if (err)
205                 goto err_power;
206
207         device_wakeup_enable(hcd->self.controller);
208
209         platform_set_drvdata(dev, hcd);
210
211         return err;
212
213 err_power:
214         if (pdata->power_off)
215                 pdata->power_off(dev);
216 err_put_clks:
217         while (--clk >= 0)
218                 clk_put(priv->clks[clk]);
219 err_put_hcd:
220         if (pdata == &ohci_platform_defaults)
221                 dev->dev.platform_data = NULL;
222
223         usb_put_hcd(hcd);
224
225         return err;
226 }
227
228 static int ohci_platform_remove(struct platform_device *dev)
229 {
230         struct usb_hcd *hcd = platform_get_drvdata(dev);
231         struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
232         struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
233         int clk;
234
235         usb_remove_hcd(hcd);
236
237         if (pdata->power_off)
238                 pdata->power_off(dev);
239
240         for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
241                 clk_put(priv->clks[clk]);
242
243         usb_put_hcd(hcd);
244
245         if (pdata == &ohci_platform_defaults)
246                 dev->dev.platform_data = NULL;
247
248         return 0;
249 }
250
251 #ifdef CONFIG_PM
252
253 static int ohci_platform_suspend(struct device *dev)
254 {
255         struct usb_hcd *hcd = dev_get_drvdata(dev);
256         struct usb_ohci_pdata *pdata = dev->platform_data;
257         struct platform_device *pdev =
258                 container_of(dev, struct platform_device, dev);
259         bool do_wakeup = device_may_wakeup(dev);
260         int ret;
261
262         ret = ohci_suspend(hcd, do_wakeup);
263         if (ret)
264                 return ret;
265
266         if (pdata->power_suspend)
267                 pdata->power_suspend(pdev);
268
269         return ret;
270 }
271
272 static int ohci_platform_resume(struct device *dev)
273 {
274         struct usb_hcd *hcd = dev_get_drvdata(dev);
275         struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
276         struct platform_device *pdev =
277                 container_of(dev, struct platform_device, dev);
278
279         if (pdata->power_on) {
280                 int err = pdata->power_on(pdev);
281                 if (err < 0)
282                         return err;
283         }
284
285         ohci_resume(hcd, false);
286         return 0;
287 }
288
289 #else /* !CONFIG_PM */
290 #define ohci_platform_suspend   NULL
291 #define ohci_platform_resume    NULL
292 #endif /* CONFIG_PM */
293
294 static const struct of_device_id ohci_platform_ids[] = {
295         { .compatible = "usb-ohci", },
296         { }
297 };
298 MODULE_DEVICE_TABLE(of, ohci_platform_ids);
299
300 static const struct platform_device_id ohci_platform_table[] = {
301         { "ohci-platform", 0 },
302         { }
303 };
304 MODULE_DEVICE_TABLE(platform, ohci_platform_table);
305
306 static const struct dev_pm_ops ohci_platform_pm_ops = {
307         .suspend        = ohci_platform_suspend,
308         .resume         = ohci_platform_resume,
309 };
310
311 static struct platform_driver ohci_platform_driver = {
312         .id_table       = ohci_platform_table,
313         .probe          = ohci_platform_probe,
314         .remove         = ohci_platform_remove,
315         .shutdown       = usb_hcd_platform_shutdown,
316         .driver         = {
317                 .owner  = THIS_MODULE,
318                 .name   = "ohci-platform",
319                 .pm     = &ohci_platform_pm_ops,
320                 .of_match_table = ohci_platform_ids,
321         }
322 };
323
324 static int __init ohci_platform_init(void)
325 {
326         if (usb_disabled())
327                 return -ENODEV;
328
329         pr_info("%s: " DRIVER_DESC "\n", hcd_name);
330
331         ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
332         return platform_driver_register(&ohci_platform_driver);
333 }
334 module_init(ohci_platform_init);
335
336 static void __exit ohci_platform_cleanup(void)
337 {
338         platform_driver_unregister(&ohci_platform_driver);
339 }
340 module_exit(ohci_platform_cleanup);
341
342 MODULE_DESCRIPTION(DRIVER_DESC);
343 MODULE_AUTHOR("Hauke Mehrtens");
344 MODULE_AUTHOR("Alan Stern");
345 MODULE_LICENSE("GPL");