1 // SPDX-License-Identifier: GPL-2.0+
3 * SAMSUNG EXYNOS USB HOST OHCI Controller
5 * Copyright (C) 2011 Samsung Electronics Co.Ltd
6 * Author: Jingoo Han <jg1.han@samsung.com>
10 #include <linux/dma-mapping.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/phy/phy.h>
17 #include <linux/usb.h>
18 #include <linux/usb/hcd.h>
22 #define DRIVER_DESC "OHCI EXYNOS driver"
24 static const char hcd_name[] = "ohci-exynos";
25 static struct hc_driver __read_mostly exynos_ohci_hc_driver;
27 #define to_exynos_ohci(hcd) (struct exynos_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
31 struct exynos_ohci_hcd {
33 struct device_node *of_node;
34 struct phy *phy[PHY_NUMBER];
37 static int exynos_ohci_get_phy(struct device *dev,
38 struct exynos_ohci_hcd *exynos_ohci)
40 struct device_node *child;
45 /* Get PHYs for the controller */
46 for_each_available_child_of_node(dev->of_node, child) {
47 ret = of_property_read_u32(child, "reg", &phy_number);
49 dev_err(dev, "Failed to parse device tree\n");
54 if (phy_number >= PHY_NUMBER) {
55 dev_err(dev, "Invalid number of PHYs\n");
60 phy = devm_of_phy_get(dev, child, NULL);
61 exynos_ohci->phy[phy_number] = phy;
64 if (ret == -EPROBE_DEFER) {
67 } else if (ret != -ENOSYS && ret != -ENODEV) {
69 "Error retrieving usb2 phy: %d\n", ret);
79 static int exynos_ohci_phy_enable(struct device *dev)
81 struct usb_hcd *hcd = dev_get_drvdata(dev);
82 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
86 for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
87 if (!IS_ERR(exynos_ohci->phy[i]))
88 ret = phy_power_on(exynos_ohci->phy[i]);
90 for (i--; i >= 0; i--)
91 if (!IS_ERR(exynos_ohci->phy[i]))
92 phy_power_off(exynos_ohci->phy[i]);
97 static void exynos_ohci_phy_disable(struct device *dev)
99 struct usb_hcd *hcd = dev_get_drvdata(dev);
100 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
103 for (i = 0; i < PHY_NUMBER; i++)
104 if (!IS_ERR(exynos_ohci->phy[i]))
105 phy_power_off(exynos_ohci->phy[i]);
108 static int exynos_ohci_probe(struct platform_device *pdev)
110 struct exynos_ohci_hcd *exynos_ohci;
112 struct resource *res;
117 * Right now device-tree probed devices don't get dma_mask set.
118 * Since shared usb code relies on it, set it here for now.
119 * Once we move to full device tree support this will vanish off.
121 err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
125 hcd = usb_create_hcd(&exynos_ohci_hc_driver,
126 &pdev->dev, dev_name(&pdev->dev));
128 dev_err(&pdev->dev, "Unable to create HCD\n");
132 exynos_ohci = to_exynos_ohci(hcd);
134 err = exynos_ohci_get_phy(&pdev->dev, exynos_ohci);
138 exynos_ohci->clk = devm_clk_get(&pdev->dev, "usbhost");
140 if (IS_ERR(exynos_ohci->clk)) {
141 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
142 err = PTR_ERR(exynos_ohci->clk);
146 err = clk_prepare_enable(exynos_ohci->clk);
150 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
151 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
152 if (IS_ERR(hcd->regs)) {
153 err = PTR_ERR(hcd->regs);
156 hcd->rsrc_start = res->start;
157 hcd->rsrc_len = resource_size(res);
159 irq = platform_get_irq(pdev, 0);
161 dev_err(&pdev->dev, "Failed to get IRQ\n");
166 platform_set_drvdata(pdev, hcd);
168 err = exynos_ohci_phy_enable(&pdev->dev);
170 dev_err(&pdev->dev, "Failed to enable USB phy\n");
175 * Workaround: reset of_node pointer to avoid conflict between Exynos
176 * OHCI port subnodes and generic USB device bindings
178 exynos_ohci->of_node = pdev->dev.of_node;
179 pdev->dev.of_node = NULL;
181 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
183 dev_err(&pdev->dev, "Failed to add USB HCD\n");
186 device_wakeup_enable(hcd->self.controller);
190 exynos_ohci_phy_disable(&pdev->dev);
191 pdev->dev.of_node = exynos_ohci->of_node;
193 clk_disable_unprepare(exynos_ohci->clk);
199 static int exynos_ohci_remove(struct platform_device *pdev)
201 struct usb_hcd *hcd = platform_get_drvdata(pdev);
202 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
204 pdev->dev.of_node = exynos_ohci->of_node;
208 exynos_ohci_phy_disable(&pdev->dev);
210 clk_disable_unprepare(exynos_ohci->clk);
217 static void exynos_ohci_shutdown(struct platform_device *pdev)
219 struct usb_hcd *hcd = platform_get_drvdata(pdev);
221 if (hcd->driver->shutdown)
222 hcd->driver->shutdown(hcd);
226 static int exynos_ohci_suspend(struct device *dev)
228 struct usb_hcd *hcd = dev_get_drvdata(dev);
229 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
230 bool do_wakeup = device_may_wakeup(dev);
231 int rc = ohci_suspend(hcd, do_wakeup);
236 exynos_ohci_phy_disable(dev);
238 clk_disable_unprepare(exynos_ohci->clk);
243 static int exynos_ohci_resume(struct device *dev)
245 struct usb_hcd *hcd = dev_get_drvdata(dev);
246 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
249 clk_prepare_enable(exynos_ohci->clk);
251 ret = exynos_ohci_phy_enable(dev);
253 dev_err(dev, "Failed to enable USB phy\n");
254 clk_disable_unprepare(exynos_ohci->clk);
258 ohci_resume(hcd, false);
263 #define exynos_ohci_suspend NULL
264 #define exynos_ohci_resume NULL
267 static const struct ohci_driver_overrides exynos_overrides __initconst = {
268 .extra_priv_size = sizeof(struct exynos_ohci_hcd),
271 static const struct dev_pm_ops exynos_ohci_pm_ops = {
272 .suspend = exynos_ohci_suspend,
273 .resume = exynos_ohci_resume,
277 static const struct of_device_id exynos_ohci_match[] = {
278 { .compatible = "samsung,exynos4210-ohci" },
281 MODULE_DEVICE_TABLE(of, exynos_ohci_match);
284 static struct platform_driver exynos_ohci_driver = {
285 .probe = exynos_ohci_probe,
286 .remove = exynos_ohci_remove,
287 .shutdown = exynos_ohci_shutdown,
289 .name = "exynos-ohci",
290 .pm = &exynos_ohci_pm_ops,
291 .of_match_table = of_match_ptr(exynos_ohci_match),
294 static int __init ohci_exynos_init(void)
299 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
300 ohci_init_driver(&exynos_ohci_hc_driver, &exynos_overrides);
301 return platform_driver_register(&exynos_ohci_driver);
303 module_init(ohci_exynos_init);
305 static void __exit ohci_exynos_cleanup(void)
307 platform_driver_unregister(&exynos_ohci_driver);
309 module_exit(ohci_exynos_cleanup);
311 MODULE_ALIAS("platform:exynos-ohci");
312 MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
313 MODULE_LICENSE("GPL v2");