sunxi: driver refresh for 3.13
[openwrt.git] / target / linux / sunxi / patches-3.13 / 180-1-usb-ohci-platform-dt-instantiation.patch
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
5
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.
9
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.
13
14 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
15 Acked-by: Alan Stern <stern@rowland.harvard.edu>
16 ---
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
21
22 diff --git a/Documentation/devicetree/bindings/usb/usb-ohci.txt b/Documentation/devicetree/bindings/usb/usb-ohci.txt
23 new file mode 100644
24 index 0000000..6ba38d9
25 --- /dev/null
26 +++ b/Documentation/devicetree/bindings/usb/usb-ohci.txt
27 @@ -0,0 +1,22 @@
28 +USB OHCI controllers
29 +
30 +Required properties:
31 +- compatible : "usb-ohci"
32 +- reg : ohci controller register range (address and length)
33 +- interrupts : ohci controller interrupt
34 +
35 +Optional properties:
36 +- clocks : a list of phandle + clock specifier pairs
37 +- phys : phandle + phy specifier pair
38 +- phy-names : "usb"
39 +
40 +Example:
41 +
42 +       ohci0: usb@01c14400 {
43 +               compatible = "allwinner,sun4i-a10-ohci", "usb-ohci";
44 +               reg = <0x01c14400 0x100>;
45 +               interrupts = <64>;
46 +               clocks = <&usb_clk 6>, <&ahb_gates 2>;
47 +               phys = <&usbphy 1>;
48 +               phy-names = "usb";
49 +       };
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
54 @@ -3,6 +3,7 @@
55   *
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>
59   *
60   * Derived from the OCHI-SSB driver
61   * Derived from the OHCI-PCI driver
62 @@ -14,11 +15,14 @@
63   * Licensed under the GNU/GPL. See COPYING for details.
64   */
65  
66 +#include <linux/clk.h>
67 +#include <linux/dma-mapping.h>
68  #include <linux/hrtimer.h>
69  #include <linux/io.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>
77 @@ -27,6 +31,13 @@
78  #include "ohci.h"
79  
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)
83 +
84 +struct ohci_platform_priv {
85 +       struct clk *clks[OHCI_MAX_CLKS];
86 +       struct phy *phy;
87 +};
88  
89  static const char hcd_name[] = "ohci-platform";
90  
91 @@ -48,11 +59,67 @@ static int ohci_platform_reset(struct usb_hcd *hcd)
92         return ohci_setup(hcd);
93  }
94  
95 +static int ohci_platform_power_on(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, ret;
100 +
101 +       for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) {
102 +               ret = clk_prepare_enable(priv->clks[clk]);
103 +               if (ret)
104 +                       goto err_disable_clks;
105 +       }
106 +
107 +       if (priv->phy) {
108 +               ret = phy_init(priv->phy);
109 +               if (ret)
110 +                       goto err_disable_clks;
111 +
112 +               ret = phy_power_on(priv->phy);
113 +               if (ret)
114 +                       goto err_exit_phy;
115 +       }
116 +
117 +       return 0;
118 +
119 +err_exit_phy:
120 +       phy_exit(priv->phy);
121 +err_disable_clks:
122 +       while (--clk >= 0)
123 +               clk_disable_unprepare(priv->clks[clk]);
124 +
125 +       return ret;
126 +}
127 +
128 +static void ohci_platform_power_off(struct platform_device *dev)
129 +{
130 +       struct usb_hcd *hcd = platform_get_drvdata(dev);
131 +       struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
132 +       int clk;
133 +
134 +       if (priv->phy) {
135 +               phy_power_off(priv->phy);
136 +               phy_exit(priv->phy);
137 +       }
138 +
139 +       for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
140 +               if (priv->clks[clk])
141 +                       clk_disable_unprepare(priv->clks[clk]);
142 +}
143 +
144  static struct hc_driver __read_mostly ohci_platform_hc_driver;
145  
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),
152 +};
153 +
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,
158  };
159  
160  static int ohci_platform_probe(struct platform_device *dev)
161 @@ -60,17 +127,23 @@ static int ohci_platform_probe(struct platform_device *dev)
162         struct usb_hcd *hcd;
163         struct resource *res_mem;
164         struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
165 -       int irq;
166 -       int err = -ENOMEM;
167 -
168 -       if (!pdata) {
169 -               WARN_ON(1);
170 -               return -ENODEV;
171 -       }
172 +       struct ohci_platform_priv *priv;
173 +       int err, irq, clk = 0;
174  
175         if (usb_disabled())
176                 return -ENODEV;
177  
178 +       /*
179 +        * Use reasonable defaults so platforms don't have to provide these
180 +        * with DT probing on ARM.
181 +        */
182 +       if (!pdata)
183 +               pdata = &ohci_platform_defaults;
184 +
185 +       err = dma_coerce_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
186 +       if (err)
187 +               return err;
188 +
189         irq = platform_get_irq(dev, 0);
190         if (irq < 0) {
191                 dev_err(&dev->dev, "no irq provided");
192 @@ -83,17 +156,40 @@ static int ohci_platform_probe(struct platform_device *dev)
193                 return -ENXIO;
194         }
195  
196 +       hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
197 +                       dev_name(&dev->dev));
198 +       if (!hcd)
199 +               return -ENOMEM;
200 +
201 +       platform_set_drvdata(dev, hcd);
202 +       dev->dev.platform_data = pdata;
203 +       priv = hcd_to_ohci_priv(hcd);
204 +
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)
210 +                               goto err_put_hcd;
211 +                       priv->phy = NULL;
212 +               }
213 +
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)
219 +                                       goto err_put_clks;
220 +                               priv->clks[clk] = NULL;
221 +                               break;
222 +                       }
223 +               }
224 +       }
225 +
226         if (pdata->power_on) {
227                 err = pdata->power_on(dev);
228                 if (err < 0)
229 -                       return err;
230 -       }
231 -
232 -       hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
233 -                       dev_name(&dev->dev));
234 -       if (!hcd) {
235 -               err = -ENOMEM;
236 -               goto err_power;
237 +                       goto err_put_clks;
238         }
239  
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);
245 -               goto err_put_hcd;
246 +               goto err_power;
247         }
248         err = usb_add_hcd(hcd, irq, IRQF_SHARED);
249         if (err)
250 -               goto err_put_hcd;
251 +               goto err_power;
252  
253         device_wakeup_enable(hcd->self.controller);
254  
255 @@ -114,11 +210,17 @@ static int ohci_platform_probe(struct platform_device *dev)
256  
257         return err;
258  
259 -err_put_hcd:
260 -       usb_put_hcd(hcd);
261  err_power:
262         if (pdata->power_off)
263                 pdata->power_off(dev);
264 +err_put_clks:
265 +       while (--clk >= 0)
266 +               clk_put(priv->clks[clk]);
267 +err_put_hcd:
268 +       if (pdata == &ohci_platform_defaults)
269 +               dev->dev.platform_data = NULL;
270 +
271 +       usb_put_hcd(hcd);
272  
273         return err;
274  }
275 @@ -127,13 +229,22 @@ static int ohci_platform_remove(struct platform_device *dev)
276  {
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);
280 +       int clk;
281  
282         usb_remove_hcd(hcd);
283 -       usb_put_hcd(hcd);
284  
285         if (pdata->power_off)
286                 pdata->power_off(dev);
287  
288 +       for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
289 +               clk_put(priv->clks[clk]);
290 +
291 +       usb_put_hcd(hcd);
292 +
293 +       if (pdata == &ohci_platform_defaults)
294 +               dev->dev.platform_data = NULL;
295 +
296         return 0;
297  }
298  
299 @@ -180,6 +291,12 @@ static int ohci_platform_resume(struct device *dev)
300  #define ohci_platform_resume   NULL
301  #endif /* CONFIG_PM */
302  
303 +static const struct of_device_id ohci_platform_ids[] = {
304 +       { .compatible = "usb-ohci", },
305 +       { }
306 +};
307 +MODULE_DEVICE_TABLE(of, ohci_platform_ids);
308 +
309  static const struct platform_device_id ohci_platform_table[] = {
310         { "ohci-platform", 0 },
311         { }
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,
317         }
318  };
319  
320 -- 
321 1.8.5.5
322