1 From 49ff47db168655cac5213cf5dd1844b08fb9823c Mon Sep 17 00:00:00 2001
2 From: Hans de Goede <hdegoede@redhat.com>
3 Date: Sun, 5 Jan 2014 14:42:39 +0100
4 Subject: [PATCH] ohci-platform: Add support for devicetree instantiation
6 Add support for ohci-platform instantiation from devicetree, including
7 optionally getting clks and a phy from devicetree, and enabling / disabling
8 those on power_on / off.
10 This should allow using ohci-platform from devicetree in various cases.
11 Specifically after this commit it can be used for the ohci controller found
12 on Allwinner sunxi SoCs.
14 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
15 Acked-by: Alan Stern <stern@rowland.harvard.edu>
17 Documentation/devicetree/bindings/usb/usb-ohci.txt | 22 +++
18 drivers/usb/host/ohci-platform.c | 162 ++++++++++++++++++---
19 2 files changed, 162 insertions(+), 22 deletions(-)
20 create mode 100644 Documentation/devicetree/bindings/usb/usb-ohci.txt
23 +++ b/Documentation/devicetree/bindings/usb/usb-ohci.txt
28 +- compatible : "generic-ohci"
29 +- reg : ohci controller register range (address and length)
30 +- interrupts : ohci controller interrupt
33 +- big-endian-regs : boolean, set this for hcds with big-endian registers
34 +- big-endian-desc : boolean, set this for hcds with big-endian descriptors
35 +- big-endian : boolean, for hcds with big-endian-regs + big-endian-desc
36 +- clocks : a list of phandle + clock specifier pairs
37 +- phys : phandle + phy specifier pair
42 + ohci0: usb@01c14400 {
43 + compatible = "allwinner,sun4i-a10-ohci", "generic-ohci";
44 + reg = <0x01c14400 0x100>;
46 + clocks = <&usb_clk 6>, <&ahb_gates 2>;
50 --- a/drivers/usb/host/ohci-platform.c
51 +++ b/drivers/usb/host/ohci-platform.c
54 * Copyright 2007 Michael Buesch <m@bues.ch>
55 * Copyright 2011-2012 Hauke Mehrtens <hauke@hauke-m.de>
56 + * Copyright 2014 Hans de Goede <hdegoede@redhat.com>
58 * Derived from the OCHI-SSB driver
59 * Derived from the OHCI-PCI driver
61 * Licensed under the GNU/GPL. See COPYING for details.
64 +#include <linux/clk.h>
65 +#include <linux/dma-mapping.h>
66 #include <linux/hrtimer.h>
68 #include <linux/kernel.h>
69 #include <linux/module.h>
70 #include <linux/err.h>
71 +#include <linux/phy/phy.h>
72 #include <linux/platform_device.h>
73 #include <linux/usb/ohci_pdriver.h>
74 #include <linux/usb.h>
78 #define DRIVER_DESC "OHCI generic platform driver"
79 +#define OHCI_MAX_CLKS 3
80 +#define hcd_to_ohci_priv(h) ((struct ohci_platform_priv *)hcd_to_ohci(h)->priv)
82 +struct ohci_platform_priv {
83 + struct clk *clks[OHCI_MAX_CLKS];
87 static const char hcd_name[] = "ohci-platform";
89 @@ -48,11 +59,67 @@ static int ohci_platform_reset(struct us
90 return ohci_setup(hcd);
93 +static int ohci_platform_power_on(struct platform_device *dev)
95 + struct usb_hcd *hcd = platform_get_drvdata(dev);
96 + struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
99 + for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) {
100 + ret = clk_prepare_enable(priv->clks[clk]);
102 + goto err_disable_clks;
106 + ret = phy_init(priv->phy);
108 + goto err_disable_clks;
110 + ret = phy_power_on(priv->phy);
118 + phy_exit(priv->phy);
121 + clk_disable_unprepare(priv->clks[clk]);
126 +static void ohci_platform_power_off(struct platform_device *dev)
128 + struct usb_hcd *hcd = platform_get_drvdata(dev);
129 + struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
133 + phy_power_off(priv->phy);
134 + phy_exit(priv->phy);
137 + for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
138 + if (priv->clks[clk])
139 + clk_disable_unprepare(priv->clks[clk]);
142 static struct hc_driver __read_mostly ohci_platform_hc_driver;
144 static const struct ohci_driver_overrides platform_overrides __initconst = {
145 - .product_desc = "Generic Platform OHCI controller",
146 - .reset = ohci_platform_reset,
147 + .product_desc = "Generic Platform OHCI controller",
148 + .reset = ohci_platform_reset,
149 + .extra_priv_size = sizeof(struct ohci_platform_priv),
152 +static struct usb_ohci_pdata ohci_platform_defaults = {
153 + .power_on = ohci_platform_power_on,
154 + .power_suspend = ohci_platform_power_off,
155 + .power_off = ohci_platform_power_off,
158 static int ohci_platform_probe(struct platform_device *dev)
159 @@ -60,17 +127,24 @@ static int ohci_platform_probe(struct pl
161 struct resource *res_mem;
162 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
170 + struct ohci_platform_priv *priv;
171 + struct ohci_hcd *ohci;
172 + int err, irq, clk = 0;
178 + * Use reasonable defaults so platforms don't have to provide these
179 + * with DT probing on ARM.
182 + pdata = &ohci_platform_defaults;
184 + err = dma_coerce_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
188 irq = platform_get_irq(dev, 0);
190 dev_err(&dev->dev, "no irq provided");
191 @@ -83,17 +157,66 @@ static int ohci_platform_probe(struct pl
195 + hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
196 + dev_name(&dev->dev));
200 + platform_set_drvdata(dev, hcd);
201 + dev->dev.platform_data = pdata;
202 + priv = hcd_to_ohci_priv(hcd);
203 + ohci = hcd_to_ohci(hcd);
205 + if (pdata == &ohci_platform_defaults && dev->dev.of_node) {
206 + if (of_property_read_bool(dev->dev.of_node, "big-endian-regs"))
207 + ohci->flags |= OHCI_QUIRK_BE_MMIO;
209 + if (of_property_read_bool(dev->dev.of_node, "big-endian-desc"))
210 + ohci->flags |= OHCI_QUIRK_BE_DESC;
212 + if (of_property_read_bool(dev->dev.of_node, "big-endian"))
213 + ohci->flags |= OHCI_QUIRK_BE_MMIO | OHCI_QUIRK_BE_DESC;
215 +#ifndef CONFIG_USB_OHCI_BIG_ENDIAN_MMIO
216 + if (ohci->flags & OHCI_QUIRK_BE_MMIO) {
218 + "Error big-endian-regs not compiled in\n");
223 +#ifndef CONFIG_USB_OHCI_BIG_ENDIAN_DESC
224 + if (ohci->flags & OHCI_QUIRK_BE_DESC) {
226 + "Error big-endian-desc not compiled in\n");
231 + priv->phy = devm_phy_get(&dev->dev, "usb");
232 + if (IS_ERR(priv->phy)) {
233 + err = PTR_ERR(priv->phy);
234 + if (err == -EPROBE_DEFER)
239 + for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
240 + priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
241 + if (IS_ERR(priv->clks[clk])) {
242 + err = PTR_ERR(priv->clks[clk]);
243 + if (err == -EPROBE_DEFER)
245 + priv->clks[clk] = NULL;
251 if (pdata->power_on) {
252 err = pdata->power_on(dev);
257 - hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
258 - dev_name(&dev->dev));
265 hcd->rsrc_start = res_mem->start;
266 @@ -102,11 +225,11 @@ static int ohci_platform_probe(struct pl
267 hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
268 if (IS_ERR(hcd->regs)) {
269 err = PTR_ERR(hcd->regs);
273 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
278 device_wakeup_enable(hcd->self.controller);
280 @@ -114,11 +237,17 @@ static int ohci_platform_probe(struct pl
287 if (pdata->power_off)
288 pdata->power_off(dev);
291 + clk_put(priv->clks[clk]);
293 + if (pdata == &ohci_platform_defaults)
294 + dev->dev.platform_data = NULL;
300 @@ -127,13 +256,22 @@ static int ohci_platform_remove(struct p
302 struct usb_hcd *hcd = platform_get_drvdata(dev);
303 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
304 + struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
310 if (pdata->power_off)
311 pdata->power_off(dev);
313 + for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
314 + clk_put(priv->clks[clk]);
318 + if (pdata == &ohci_platform_defaults)
319 + dev->dev.platform_data = NULL;
324 @@ -180,6 +318,12 @@ static int ohci_platform_resume(struct d
325 #define ohci_platform_resume NULL
326 #endif /* CONFIG_PM */
328 +static const struct of_device_id ohci_platform_ids[] = {
329 + { .compatible = "generic-ohci", },
332 +MODULE_DEVICE_TABLE(of, ohci_platform_ids);
334 static const struct platform_device_id ohci_platform_table[] = {
335 { "ohci-platform", 0 },
337 @@ -200,6 +344,7 @@ static struct platform_driver ohci_platf
338 .owner = THIS_MODULE,
339 .name = "ohci-platform",
340 .pm = &ohci_platform_pm_ops,
341 + .of_match_table = ohci_platform_ids,