2 * SAMSUNG EXYNOS USB HOST OHCI Controller
4 * Copyright (C) 2011 Samsung Electronics Co.Ltd
5 * Author: Jingoo Han <jg1.han@samsung.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
14 #include <linux/clk.h>
15 #include <linux/dma-mapping.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/phy/phy.h>
22 #include <linux/usb/phy.h>
23 #include <linux/usb/samsung_usb_phy.h>
24 #include <linux/usb.h>
25 #include <linux/usb/hcd.h>
26 #include <linux/usb/otg.h>
30 #define DRIVER_DESC "OHCI EXYNOS driver"
32 static const char hcd_name[] = "ohci-exynos";
33 static struct hc_driver __read_mostly exynos_ohci_hc_driver;
35 #define to_exynos_ohci(hcd) (struct exynos_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
39 struct exynos_ohci_hcd {
43 struct phy *phy_g[PHY_NUMBER];
46 static int exynos_ohci_get_phy(struct device *dev,
47 struct exynos_ohci_hcd *exynos_ohci)
49 struct device_node *child;
55 * Getting generic phy:
56 * We are keeping both types of phys as a part of transiting OHCI
57 * to generic phy framework, so as to maintain backward compatibilty
59 * We fallback to older USB-PHYs when we fail to get generic PHYs.
61 for_each_available_child_of_node(dev->of_node, child) {
62 ret = of_property_read_u32(child, "reg", &phy_number);
64 dev_err(dev, "Failed to parse device tree\n");
69 if (phy_number >= PHY_NUMBER) {
70 dev_err(dev, "Invalid number of PHYs\n");
75 phy = devm_of_phy_get(dev, child, NULL);
78 /* Lets fallback to older USB-PHYs */
80 exynos_ohci->phy_g[phy_number] = phy;
81 /* Make the older PHYs unavailable */
82 exynos_ohci->phy = ERR_PTR(-ENXIO);
88 exynos_ohci->phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
89 if (IS_ERR(exynos_ohci->phy)) {
90 ret = PTR_ERR(exynos_ohci->phy);
91 if (ret != -ENXIO && ret != -ENODEV) {
92 dev_err(dev, "no usb2 phy configured\n");
95 dev_dbg(dev, "Failed to get usb2 phy\n");
97 exynos_ohci->otg = exynos_ohci->phy->otg;
103 static int exynos_ohci_phy_enable(struct device *dev)
105 struct usb_hcd *hcd = dev_get_drvdata(dev);
106 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
110 if (!IS_ERR(exynos_ohci->phy))
111 return usb_phy_init(exynos_ohci->phy);
113 for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
114 if (!IS_ERR(exynos_ohci->phy_g[i]))
115 ret = phy_power_on(exynos_ohci->phy_g[i]);
117 for (i--; i >= 0; i--)
118 if (!IS_ERR(exynos_ohci->phy_g[i]))
119 phy_power_off(exynos_ohci->phy_g[i]);
124 static void exynos_ohci_phy_disable(struct device *dev)
126 struct usb_hcd *hcd = dev_get_drvdata(dev);
127 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
130 if (!IS_ERR(exynos_ohci->phy)) {
131 usb_phy_shutdown(exynos_ohci->phy);
135 for (i = 0; i < PHY_NUMBER; i++)
136 if (!IS_ERR(exynos_ohci->phy_g[i]))
137 phy_power_off(exynos_ohci->phy_g[i]);
140 static int exynos_ohci_probe(struct platform_device *pdev)
142 struct exynos_ohci_hcd *exynos_ohci;
144 struct resource *res;
149 * Right now device-tree probed devices don't get dma_mask set.
150 * Since shared usb code relies on it, set it here for now.
151 * Once we move to full device tree support this will vanish off.
153 err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
157 hcd = usb_create_hcd(&exynos_ohci_hc_driver,
158 &pdev->dev, dev_name(&pdev->dev));
160 dev_err(&pdev->dev, "Unable to create HCD\n");
164 exynos_ohci = to_exynos_ohci(hcd);
166 if (of_device_is_compatible(pdev->dev.of_node,
167 "samsung,exynos5440-ohci"))
170 err = exynos_ohci_get_phy(&pdev->dev, exynos_ohci);
175 exynos_ohci->clk = devm_clk_get(&pdev->dev, "usbhost");
177 if (IS_ERR(exynos_ohci->clk)) {
178 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
179 err = PTR_ERR(exynos_ohci->clk);
183 err = clk_prepare_enable(exynos_ohci->clk);
187 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
189 dev_err(&pdev->dev, "Failed to get I/O memory\n");
194 hcd->rsrc_start = res->start;
195 hcd->rsrc_len = resource_size(res);
196 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
197 if (IS_ERR(hcd->regs)) {
198 err = PTR_ERR(hcd->regs);
202 irq = platform_get_irq(pdev, 0);
204 dev_err(&pdev->dev, "Failed to get IRQ\n");
209 if (exynos_ohci->otg)
210 exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
212 platform_set_drvdata(pdev, hcd);
214 err = exynos_ohci_phy_enable(&pdev->dev);
216 dev_err(&pdev->dev, "Failed to enable USB phy\n");
220 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
222 dev_err(&pdev->dev, "Failed to add USB HCD\n");
225 device_wakeup_enable(hcd->self.controller);
229 exynos_ohci_phy_disable(&pdev->dev);
231 clk_disable_unprepare(exynos_ohci->clk);
237 static int exynos_ohci_remove(struct platform_device *pdev)
239 struct usb_hcd *hcd = platform_get_drvdata(pdev);
240 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
244 if (exynos_ohci->otg)
245 exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
247 exynos_ohci_phy_disable(&pdev->dev);
249 clk_disable_unprepare(exynos_ohci->clk);
256 static void exynos_ohci_shutdown(struct platform_device *pdev)
258 struct usb_hcd *hcd = platform_get_drvdata(pdev);
260 if (hcd->driver->shutdown)
261 hcd->driver->shutdown(hcd);
265 static int exynos_ohci_suspend(struct device *dev)
267 struct usb_hcd *hcd = dev_get_drvdata(dev);
268 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
269 bool do_wakeup = device_may_wakeup(dev);
270 int rc = ohci_suspend(hcd, do_wakeup);
275 if (exynos_ohci->otg)
276 exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
278 exynos_ohci_phy_disable(dev);
280 clk_disable_unprepare(exynos_ohci->clk);
285 static int exynos_ohci_resume(struct device *dev)
287 struct usb_hcd *hcd = dev_get_drvdata(dev);
288 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
291 clk_prepare_enable(exynos_ohci->clk);
293 if (exynos_ohci->otg)
294 exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
296 ret = exynos_ohci_phy_enable(dev);
298 dev_err(dev, "Failed to enable USB phy\n");
299 clk_disable_unprepare(exynos_ohci->clk);
303 ohci_resume(hcd, false);
308 #define exynos_ohci_suspend NULL
309 #define exynos_ohci_resume NULL
312 static const struct ohci_driver_overrides exynos_overrides __initconst = {
313 .extra_priv_size = sizeof(struct exynos_ohci_hcd),
316 static const struct dev_pm_ops exynos_ohci_pm_ops = {
317 .suspend = exynos_ohci_suspend,
318 .resume = exynos_ohci_resume,
322 static const struct of_device_id exynos_ohci_match[] = {
323 { .compatible = "samsung,exynos4210-ohci" },
324 { .compatible = "samsung,exynos5440-ohci" },
327 MODULE_DEVICE_TABLE(of, exynos_ohci_match);
330 static struct platform_driver exynos_ohci_driver = {
331 .probe = exynos_ohci_probe,
332 .remove = exynos_ohci_remove,
333 .shutdown = exynos_ohci_shutdown,
335 .name = "exynos-ohci",
336 .owner = THIS_MODULE,
337 .pm = &exynos_ohci_pm_ops,
338 .of_match_table = of_match_ptr(exynos_ohci_match),
341 static int __init ohci_exynos_init(void)
346 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
347 ohci_init_driver(&exynos_ohci_hc_driver, &exynos_overrides);
348 return platform_driver_register(&exynos_ohci_driver);
350 module_init(ohci_exynos_init);
352 static void __exit ohci_exynos_cleanup(void)
354 platform_driver_unregister(&exynos_ohci_driver);
356 module_exit(ohci_exynos_cleanup);
358 MODULE_ALIAS("platform:exynos-ohci");
359 MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
360 MODULE_LICENSE("GPL v2");