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;
54 exynos_ohci->phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
55 if (IS_ERR(exynos_ohci->phy)) {
56 ret = PTR_ERR(exynos_ohci->phy);
57 if (ret != -ENXIO && ret != -ENODEV) {
58 dev_err(dev, "no usb2 phy configured\n");
61 dev_dbg(dev, "Failed to get usb2 phy\n");
63 exynos_ohci->otg = exynos_ohci->phy->otg;
67 * Getting generic phy:
68 * We are keeping both types of phys as a part of transiting OHCI
69 * to generic phy framework, so as to maintain backward compatibilty
71 * If there are existing devices using DTB files built from them,
72 * to remove the support for old bindings in this driver,
73 * we need to make sure that such devices have their DTBs
74 * updated to ones built from new DTS.
76 for_each_available_child_of_node(dev->of_node, child) {
77 ret = of_property_read_u32(child, "reg", &phy_number);
79 dev_err(dev, "Failed to parse device tree\n");
84 if (phy_number >= PHY_NUMBER) {
85 dev_err(dev, "Invalid number of PHYs\n");
90 phy = devm_of_phy_get(dev, child, 0);
94 if (ret != -ENOSYS && ret != -ENODEV) {
95 dev_err(dev, "no usb2 phy configured\n");
98 dev_dbg(dev, "Failed to get usb2 phy\n");
100 exynos_ohci->phy_g[phy_number] = phy;
106 static int exynos_ohci_phy_enable(struct device *dev)
108 struct usb_hcd *hcd = dev_get_drvdata(dev);
109 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
113 if (!IS_ERR(exynos_ohci->phy))
114 return usb_phy_init(exynos_ohci->phy);
116 for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
117 if (!IS_ERR(exynos_ohci->phy_g[i]))
118 ret = phy_power_on(exynos_ohci->phy_g[i]);
120 for (i--; i >= 0; i--)
121 if (!IS_ERR(exynos_ohci->phy_g[i]))
122 phy_power_off(exynos_ohci->phy_g[i]);
127 static void exynos_ohci_phy_disable(struct device *dev)
129 struct usb_hcd *hcd = dev_get_drvdata(dev);
130 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
133 if (!IS_ERR(exynos_ohci->phy)) {
134 usb_phy_shutdown(exynos_ohci->phy);
138 for (i = 0; i < PHY_NUMBER; i++)
139 if (!IS_ERR(exynos_ohci->phy_g[i]))
140 phy_power_off(exynos_ohci->phy_g[i]);
143 static int exynos_ohci_probe(struct platform_device *pdev)
145 struct exynos_ohci_hcd *exynos_ohci;
147 struct resource *res;
152 * Right now device-tree probed devices don't get dma_mask set.
153 * Since shared usb code relies on it, set it here for now.
154 * Once we move to full device tree support this will vanish off.
156 err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
160 hcd = usb_create_hcd(&exynos_ohci_hc_driver,
161 &pdev->dev, dev_name(&pdev->dev));
163 dev_err(&pdev->dev, "Unable to create HCD\n");
167 exynos_ohci = to_exynos_ohci(hcd);
169 if (of_device_is_compatible(pdev->dev.of_node,
170 "samsung,exynos5440-ohci"))
173 err = exynos_ohci_get_phy(&pdev->dev, exynos_ohci);
178 exynos_ohci->clk = devm_clk_get(&pdev->dev, "usbhost");
180 if (IS_ERR(exynos_ohci->clk)) {
181 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
182 err = PTR_ERR(exynos_ohci->clk);
186 err = clk_prepare_enable(exynos_ohci->clk);
190 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
192 dev_err(&pdev->dev, "Failed to get I/O memory\n");
197 hcd->rsrc_start = res->start;
198 hcd->rsrc_len = resource_size(res);
199 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
200 if (IS_ERR(hcd->regs)) {
201 err = PTR_ERR(hcd->regs);
205 irq = platform_get_irq(pdev, 0);
207 dev_err(&pdev->dev, "Failed to get IRQ\n");
212 if (exynos_ohci->otg)
213 exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
215 platform_set_drvdata(pdev, hcd);
217 err = exynos_ohci_phy_enable(&pdev->dev);
219 dev_err(&pdev->dev, "Failed to enable USB phy\n");
223 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
225 dev_err(&pdev->dev, "Failed to add USB HCD\n");
228 device_wakeup_enable(hcd->self.controller);
232 exynos_ohci_phy_disable(&pdev->dev);
234 clk_disable_unprepare(exynos_ohci->clk);
240 static int exynos_ohci_remove(struct platform_device *pdev)
242 struct usb_hcd *hcd = platform_get_drvdata(pdev);
243 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
247 if (exynos_ohci->otg)
248 exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
250 exynos_ohci_phy_disable(&pdev->dev);
252 clk_disable_unprepare(exynos_ohci->clk);
259 static void exynos_ohci_shutdown(struct platform_device *pdev)
261 struct usb_hcd *hcd = platform_get_drvdata(pdev);
263 if (hcd->driver->shutdown)
264 hcd->driver->shutdown(hcd);
268 static int exynos_ohci_suspend(struct device *dev)
270 struct usb_hcd *hcd = dev_get_drvdata(dev);
271 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
272 bool do_wakeup = device_may_wakeup(dev);
273 int rc = ohci_suspend(hcd, do_wakeup);
278 if (exynos_ohci->otg)
279 exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
281 exynos_ohci_phy_disable(dev);
283 clk_disable_unprepare(exynos_ohci->clk);
288 static int exynos_ohci_resume(struct device *dev)
290 struct usb_hcd *hcd = dev_get_drvdata(dev);
291 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
294 clk_prepare_enable(exynos_ohci->clk);
296 if (exynos_ohci->otg)
297 exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
299 ret = exynos_ohci_phy_enable(dev);
301 dev_err(dev, "Failed to enable USB phy\n");
302 clk_disable_unprepare(exynos_ohci->clk);
306 ohci_resume(hcd, false);
311 #define exynos_ohci_suspend NULL
312 #define exynos_ohci_resume NULL
315 static const struct ohci_driver_overrides exynos_overrides __initconst = {
316 .extra_priv_size = sizeof(struct exynos_ohci_hcd),
319 static const struct dev_pm_ops exynos_ohci_pm_ops = {
320 .suspend = exynos_ohci_suspend,
321 .resume = exynos_ohci_resume,
325 static const struct of_device_id exynos_ohci_match[] = {
326 { .compatible = "samsung,exynos4210-ohci" },
327 { .compatible = "samsung,exynos5440-ohci" },
330 MODULE_DEVICE_TABLE(of, exynos_ohci_match);
333 static struct platform_driver exynos_ohci_driver = {
334 .probe = exynos_ohci_probe,
335 .remove = exynos_ohci_remove,
336 .shutdown = exynos_ohci_shutdown,
338 .name = "exynos-ohci",
339 .owner = THIS_MODULE,
340 .pm = &exynos_ohci_pm_ops,
341 .of_match_table = of_match_ptr(exynos_ohci_match),
344 static int __init ohci_exynos_init(void)
349 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
350 ohci_init_driver(&exynos_ohci_hc_driver, &exynos_overrides);
351 return platform_driver_register(&exynos_ohci_driver);
353 module_init(ohci_exynos_init);
355 static void __exit ohci_exynos_cleanup(void)
357 platform_driver_unregister(&exynos_ohci_driver);
359 module_exit(ohci_exynos_cleanup);
361 MODULE_ALIAS("platform:exynos-ohci");
362 MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
363 MODULE_LICENSE("GPL v2");