1 From 6b63b9b2093258541dd79a3ed311cd1d0412b846 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
22 diff --git a/Documentation/devicetree/bindings/usb/usb-ohci.txt b/Documentation/devicetree/bindings/usb/usb-ohci.txt
24 index 0000000..6ba38d9
26 +++ b/Documentation/devicetree/bindings/usb/usb-ohci.txt
31 +- compatible : "usb-ohci"
32 +- reg : ohci controller register range (address and length)
33 +- interrupts : ohci controller interrupt
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", "usb-ohci";
44 + reg = <0x01c14400 0x100>;
46 + clocks = <&usb_clk 6>, <&ahb_gates 2>;
50 diff --git a/drivers/usb/host/ohci-platform.c b/drivers/usb/host/ohci-platform.c
51 index 68f674c..49304dd 100644
52 --- a/drivers/usb/host/ohci-platform.c
53 +++ b/drivers/usb/host/ohci-platform.c
56 * Copyright 2007 Michael Buesch <m@bues.ch>
57 * Copyright 2011-2012 Hauke Mehrtens <hauke@hauke-m.de>
58 + * Copyright 2014 Hans de Goede <hdegoede@redhat.com>
60 * Derived from the OCHI-SSB driver
61 * Derived from the OHCI-PCI driver
63 * Licensed under the GNU/GPL. See COPYING for details.
66 +#include <linux/clk.h>
67 +#include <linux/dma-mapping.h>
68 #include <linux/hrtimer.h>
70 #include <linux/kernel.h>
71 #include <linux/module.h>
72 #include <linux/err.h>
73 +#include <linux/phy/phy.h>
74 #include <linux/platform_device.h>
75 #include <linux/usb/ohci_pdriver.h>
76 #include <linux/usb.h>
80 #define DRIVER_DESC "OHCI generic platform driver"
81 +#define OHCI_MAX_CLKS 3
82 +#define hcd_to_ohci_priv(h) ((struct ohci_platform_priv *)hcd_to_ohci(h)->priv)
84 +struct ohci_platform_priv {
85 + struct clk *clks[OHCI_MAX_CLKS];
89 static const char hcd_name[] = "ohci-platform";
91 @@ -48,11 +59,67 @@ static int ohci_platform_reset(struct usb_hcd *hcd)
92 return ohci_setup(hcd);
95 +static int ohci_platform_power_on(struct platform_device *dev)
97 + struct usb_hcd *hcd = platform_get_drvdata(dev);
98 + struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
101 + for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) {
102 + ret = clk_prepare_enable(priv->clks[clk]);
104 + goto err_disable_clks;
108 + ret = phy_init(priv->phy);
110 + goto err_disable_clks;
112 + ret = phy_power_on(priv->phy);
120 + phy_exit(priv->phy);
123 + clk_disable_unprepare(priv->clks[clk]);
128 +static void ohci_platform_power_off(struct platform_device *dev)
130 + struct usb_hcd *hcd = platform_get_drvdata(dev);
131 + struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
135 + phy_power_off(priv->phy);
136 + phy_exit(priv->phy);
139 + for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
140 + if (priv->clks[clk])
141 + clk_disable_unprepare(priv->clks[clk]);
144 static struct hc_driver __read_mostly ohci_platform_hc_driver;
146 static const struct ohci_driver_overrides platform_overrides __initconst = {
147 - .product_desc = "Generic Platform OHCI controller",
148 - .reset = ohci_platform_reset,
149 + .product_desc = "Generic Platform OHCI controller",
150 + .reset = ohci_platform_reset,
151 + .extra_priv_size = sizeof(struct ohci_platform_priv),
154 +static struct usb_ohci_pdata ohci_platform_defaults = {
155 + .power_on = ohci_platform_power_on,
156 + .power_suspend = ohci_platform_power_off,
157 + .power_off = ohci_platform_power_off,
160 static int ohci_platform_probe(struct platform_device *dev)
161 @@ -60,17 +127,23 @@ static int ohci_platform_probe(struct platform_device *dev)
163 struct resource *res_mem;
164 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
172 + struct ohci_platform_priv *priv;
173 + int err, irq, clk = 0;
179 + * Use reasonable defaults so platforms don't have to provide these
180 + * with DT probing on ARM.
183 + pdata = &ohci_platform_defaults;
185 + err = dma_coerce_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
189 irq = platform_get_irq(dev, 0);
191 dev_err(&dev->dev, "no irq provided");
192 @@ -83,17 +156,40 @@ static int ohci_platform_probe(struct platform_device *dev)
196 + hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
197 + dev_name(&dev->dev));
201 + platform_set_drvdata(dev, hcd);
202 + dev->dev.platform_data = pdata;
203 + priv = hcd_to_ohci_priv(hcd);
205 + if (pdata == &ohci_platform_defaults && dev->dev.of_node) {
206 + priv->phy = devm_phy_get(&dev->dev, "usb");
207 + if (IS_ERR(priv->phy)) {
208 + err = PTR_ERR(priv->phy);
209 + if (err == -EPROBE_DEFER)
214 + for (clk = 0; clk < OHCI_MAX_CLKS; clk++) {
215 + priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
216 + if (IS_ERR(priv->clks[clk])) {
217 + err = PTR_ERR(priv->clks[clk]);
218 + if (err == -EPROBE_DEFER)
220 + priv->clks[clk] = NULL;
226 if (pdata->power_on) {
227 err = pdata->power_on(dev);
232 - hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
233 - dev_name(&dev->dev));
240 hcd->rsrc_start = res_mem->start;
241 @@ -102,11 +198,11 @@ static int ohci_platform_probe(struct platform_device *dev)
242 hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
243 if (IS_ERR(hcd->regs)) {
244 err = PTR_ERR(hcd->regs);
248 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
253 device_wakeup_enable(hcd->self.controller);
255 @@ -114,11 +210,17 @@ static int ohci_platform_probe(struct platform_device *dev)
262 if (pdata->power_off)
263 pdata->power_off(dev);
266 + clk_put(priv->clks[clk]);
268 + if (pdata == &ohci_platform_defaults)
269 + dev->dev.platform_data = NULL;
275 @@ -127,13 +229,22 @@ static int ohci_platform_remove(struct platform_device *dev)
277 struct usb_hcd *hcd = platform_get_drvdata(dev);
278 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
279 + struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
285 if (pdata->power_off)
286 pdata->power_off(dev);
288 + for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
289 + clk_put(priv->clks[clk]);
293 + if (pdata == &ohci_platform_defaults)
294 + dev->dev.platform_data = NULL;
299 @@ -180,6 +291,12 @@ static int ohci_platform_resume(struct device *dev)
300 #define ohci_platform_resume NULL
301 #endif /* CONFIG_PM */
303 +static const struct of_device_id ohci_platform_ids[] = {
304 + { .compatible = "usb-ohci", },
307 +MODULE_DEVICE_TABLE(of, ohci_platform_ids);
309 static const struct platform_device_id ohci_platform_table[] = {
310 { "ohci-platform", 0 },
312 @@ -200,6 +317,7 @@ static int ohci_platform_resume(struct device *dev)
313 .owner = THIS_MODULE,
314 .name = "ohci-platform",
315 .pm = &ohci_platform_pm_ops,
316 + .of_match_table = ohci_platform_ids,