2 * Generic platform ohci driver
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>
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
15 * Licensed under the GNU/GPL. See COPYING for details.
18 #include <linux/clk.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/hrtimer.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>
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)
37 struct ohci_platform_priv {
38 struct clk *clks[OHCI_MAX_CLKS];
42 static const char hcd_name[] = "ohci-platform";
44 static int ohci_platform_reset(struct usb_hcd *hcd)
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);
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;
57 ohci->num_ports = pdata->num_ports;
59 return ohci_setup(hcd);
62 static int ohci_platform_power_on(struct platform_device *dev)
64 struct usb_hcd *hcd = platform_get_drvdata(dev);
65 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
68 for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++) {
69 ret = clk_prepare_enable(priv->clks[clk]);
71 goto err_disable_clks;
75 ret = phy_init(priv->phy);
77 goto err_disable_clks;
79 ret = phy_power_on(priv->phy);
90 clk_disable_unprepare(priv->clks[clk]);
95 static void ohci_platform_power_off(struct platform_device *dev)
97 struct usb_hcd *hcd = platform_get_drvdata(dev);
98 struct ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
102 phy_power_off(priv->phy);
106 for (clk = OHCI_MAX_CLKS - 1; clk >= 0; clk--)
108 clk_disable_unprepare(priv->clks[clk]);
111 static struct hc_driver __read_mostly ohci_platform_hc_driver;
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),
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,
125 static int ohci_platform_probe(struct platform_device *dev)
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;
137 * Use reasonable defaults so platforms don't have to provide these
138 * with DT probing on ARM.
141 pdata = &ohci_platform_defaults;
143 err = dma_coerce_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
147 irq = platform_get_irq(dev, 0);
149 dev_err(&dev->dev, "no irq provided");
153 res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
155 dev_err(&dev->dev, "no memory resource provided");
159 hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
160 dev_name(&dev->dev));
164 platform_set_drvdata(dev, hcd);
165 dev->dev.platform_data = pdata;
166 priv = hcd_to_ohci_priv(hcd);
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)
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)
183 priv->clks[clk] = NULL;
189 if (pdata->power_on) {
190 err = pdata->power_on(dev);
195 hcd->rsrc_start = res_mem->start;
196 hcd->rsrc_len = resource_size(res_mem);
198 hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
199 if (IS_ERR(hcd->regs)) {
200 err = PTR_ERR(hcd->regs);
203 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
207 device_wakeup_enable(hcd->self.controller);
209 platform_set_drvdata(dev, hcd);
214 if (pdata->power_off)
215 pdata->power_off(dev);
218 clk_put(priv->clks[clk]);
220 if (pdata == &ohci_platform_defaults)
221 dev->dev.platform_data = NULL;
228 static int ohci_platform_remove(struct platform_device *dev)
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);
237 if (pdata->power_off)
238 pdata->power_off(dev);
240 for (clk = 0; clk < OHCI_MAX_CLKS && priv->clks[clk]; clk++)
241 clk_put(priv->clks[clk]);
245 if (pdata == &ohci_platform_defaults)
246 dev->dev.platform_data = NULL;
253 static int ohci_platform_suspend(struct device *dev)
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);
262 ret = ohci_suspend(hcd, do_wakeup);
266 if (pdata->power_suspend)
267 pdata->power_suspend(pdev);
272 static int ohci_platform_resume(struct device *dev)
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);
279 if (pdata->power_on) {
280 int err = pdata->power_on(pdev);
285 ohci_resume(hcd, false);
289 #else /* !CONFIG_PM */
290 #define ohci_platform_suspend NULL
291 #define ohci_platform_resume NULL
292 #endif /* CONFIG_PM */
294 static const struct of_device_id ohci_platform_ids[] = {
295 { .compatible = "usb-ohci", },
298 MODULE_DEVICE_TABLE(of, ohci_platform_ids);
300 static const struct platform_device_id ohci_platform_table[] = {
301 { "ohci-platform", 0 },
304 MODULE_DEVICE_TABLE(platform, ohci_platform_table);
306 static const struct dev_pm_ops ohci_platform_pm_ops = {
307 .suspend = ohci_platform_suspend,
308 .resume = ohci_platform_resume,
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,
317 .owner = THIS_MODULE,
318 .name = "ohci-platform",
319 .pm = &ohci_platform_pm_ops,
320 .of_match_table = ohci_platform_ids,
324 static int __init ohci_platform_init(void)
329 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
331 ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
332 return platform_driver_register(&ohci_platform_driver);
334 module_init(ohci_platform_init);
336 static void __exit ohci_platform_cleanup(void)
338 platform_driver_unregister(&ohci_platform_driver);
340 module_exit(ohci_platform_cleanup);
342 MODULE_DESCRIPTION(DRIVER_DESC);
343 MODULE_AUTHOR("Hauke Mehrtens");
344 MODULE_AUTHOR("Alan Stern");
345 MODULE_LICENSE("GPL");