Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / usb / host / ohci-exynos.c
blobcc5cb09009880a65cb334ba0476300a0dad04f4b
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * SAMSUNG EXYNOS USB HOST OHCI Controller
5 * Copyright (C) 2011 Samsung Electronics Co.Ltd
6 * Author: Jingoo Han <jg1.han@samsung.com>
7 */
9 #include <linux/clk.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/io.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/platform_device.h>
16 #include <linux/phy/phy.h>
17 #include <linux/usb.h>
18 #include <linux/usb/hcd.h>
20 #include "ohci.h"
22 #define DRIVER_DESC "OHCI Exynos driver"
24 static struct hc_driver __read_mostly exynos_ohci_hc_driver;
26 #define to_exynos_ohci(hcd) (struct exynos_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
28 #define PHY_NUMBER 3
30 struct exynos_ohci_hcd {
31 struct clk *clk;
32 struct device_node *of_node;
33 struct phy *phy[PHY_NUMBER];
34 bool legacy_phy;
37 static int exynos_ohci_get_phy(struct device *dev,
38 struct exynos_ohci_hcd *exynos_ohci)
40 struct phy *phy;
41 int phy_number, num_phys;
42 int ret;
44 /* Get PHYs for the controller */
45 num_phys = of_count_phandle_with_args(dev->of_node, "phys",
46 "#phy-cells");
47 for (phy_number = 0; phy_number < num_phys; phy_number++) {
48 phy = devm_of_phy_get_by_index(dev, dev->of_node, phy_number);
49 if (IS_ERR(phy))
50 return PTR_ERR(phy);
51 exynos_ohci->phy[phy_number] = phy;
53 if (num_phys > 0)
54 return 0;
56 /* Get PHYs using legacy bindings */
57 for_each_available_child_of_node_scoped(dev->of_node, child) {
58 ret = of_property_read_u32(child, "reg", &phy_number);
59 if (ret) {
60 dev_err(dev, "Failed to parse device tree\n");
61 return ret;
64 if (phy_number >= PHY_NUMBER) {
65 dev_err(dev, "Invalid number of PHYs\n");
66 return -EINVAL;
69 phy = devm_of_phy_optional_get(dev, child, NULL);
70 exynos_ohci->phy[phy_number] = phy;
71 if (IS_ERR(phy))
72 return PTR_ERR(phy);
75 exynos_ohci->legacy_phy = true;
76 return 0;
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);
83 int i;
84 int ret = 0;
86 for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
87 ret = phy_power_on(exynos_ohci->phy[i]);
88 if (ret)
89 for (i--; i >= 0; i--)
90 phy_power_off(exynos_ohci->phy[i]);
92 return ret;
95 static void exynos_ohci_phy_disable(struct device *dev)
97 struct usb_hcd *hcd = dev_get_drvdata(dev);
98 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
99 int i;
101 for (i = 0; i < PHY_NUMBER; i++)
102 phy_power_off(exynos_ohci->phy[i]);
105 static int exynos_ohci_probe(struct platform_device *pdev)
107 struct exynos_ohci_hcd *exynos_ohci;
108 struct usb_hcd *hcd;
109 struct resource *res;
110 int irq;
111 int err;
114 * Right now device-tree probed devices don't get dma_mask set.
115 * Since shared usb code relies on it, set it here for now.
116 * Once we move to full device tree support this will vanish off.
118 err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
119 if (err)
120 return err;
122 hcd = usb_create_hcd(&exynos_ohci_hc_driver,
123 &pdev->dev, dev_name(&pdev->dev));
124 if (!hcd) {
125 dev_err(&pdev->dev, "Unable to create HCD\n");
126 return -ENOMEM;
129 exynos_ohci = to_exynos_ohci(hcd);
131 err = exynos_ohci_get_phy(&pdev->dev, exynos_ohci);
132 if (err)
133 goto fail_io;
135 exynos_ohci->clk = devm_clk_get_enabled(&pdev->dev, "usbhost");
137 if (IS_ERR(exynos_ohci->clk)) {
138 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
139 err = PTR_ERR(exynos_ohci->clk);
140 goto fail_io;
143 hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
144 if (IS_ERR(hcd->regs)) {
145 err = PTR_ERR(hcd->regs);
146 goto fail_io;
148 hcd->rsrc_start = res->start;
149 hcd->rsrc_len = resource_size(res);
151 irq = platform_get_irq(pdev, 0);
152 if (irq < 0) {
153 err = irq;
154 goto fail_io;
157 platform_set_drvdata(pdev, hcd);
159 err = exynos_ohci_phy_enable(&pdev->dev);
160 if (err) {
161 dev_err(&pdev->dev, "Failed to enable USB phy\n");
162 goto fail_io;
166 * Workaround: reset of_node pointer to avoid conflict between legacy
167 * Exynos OHCI port subnodes and generic USB device bindings
169 exynos_ohci->of_node = pdev->dev.of_node;
170 if (exynos_ohci->legacy_phy)
171 pdev->dev.of_node = NULL;
173 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
174 if (err) {
175 dev_err(&pdev->dev, "Failed to add USB HCD\n");
176 goto fail_add_hcd;
178 device_wakeup_enable(hcd->self.controller);
179 return 0;
181 fail_add_hcd:
182 exynos_ohci_phy_disable(&pdev->dev);
183 pdev->dev.of_node = exynos_ohci->of_node;
184 fail_io:
185 usb_put_hcd(hcd);
186 return err;
189 static void exynos_ohci_remove(struct platform_device *pdev)
191 struct usb_hcd *hcd = platform_get_drvdata(pdev);
192 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
194 pdev->dev.of_node = exynos_ohci->of_node;
196 usb_remove_hcd(hcd);
198 exynos_ohci_phy_disable(&pdev->dev);
200 usb_put_hcd(hcd);
203 static void exynos_ohci_shutdown(struct platform_device *pdev)
205 struct usb_hcd *hcd = platform_get_drvdata(pdev);
207 if (hcd->driver->shutdown)
208 hcd->driver->shutdown(hcd);
211 static int exynos_ohci_suspend(struct device *dev)
213 struct usb_hcd *hcd = dev_get_drvdata(dev);
214 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
215 bool do_wakeup = device_may_wakeup(dev);
216 int rc = ohci_suspend(hcd, do_wakeup);
218 if (rc)
219 return rc;
221 exynos_ohci_phy_disable(dev);
223 clk_disable_unprepare(exynos_ohci->clk);
225 return 0;
228 static int exynos_ohci_resume(struct device *dev)
230 struct usb_hcd *hcd = dev_get_drvdata(dev);
231 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
232 int ret;
234 clk_prepare_enable(exynos_ohci->clk);
236 ret = exynos_ohci_phy_enable(dev);
237 if (ret) {
238 dev_err(dev, "Failed to enable USB phy\n");
239 clk_disable_unprepare(exynos_ohci->clk);
240 return ret;
243 ohci_resume(hcd, false);
245 return 0;
248 static const struct ohci_driver_overrides exynos_overrides __initconst = {
249 .extra_priv_size = sizeof(struct exynos_ohci_hcd),
252 static DEFINE_SIMPLE_DEV_PM_OPS(exynos_ohci_pm_ops,
253 exynos_ohci_suspend, exynos_ohci_resume);
255 #ifdef CONFIG_OF
256 static const struct of_device_id exynos_ohci_match[] = {
257 { .compatible = "samsung,exynos4210-ohci" },
260 MODULE_DEVICE_TABLE(of, exynos_ohci_match);
261 #endif
263 static struct platform_driver exynos_ohci_driver = {
264 .probe = exynos_ohci_probe,
265 .remove = exynos_ohci_remove,
266 .shutdown = exynos_ohci_shutdown,
267 .driver = {
268 .name = "exynos-ohci",
269 .pm = pm_ptr(&exynos_ohci_pm_ops),
270 .of_match_table = of_match_ptr(exynos_ohci_match),
273 static int __init ohci_exynos_init(void)
275 if (usb_disabled())
276 return -ENODEV;
278 ohci_init_driver(&exynos_ohci_hc_driver, &exynos_overrides);
279 return platform_driver_register(&exynos_ohci_driver);
281 module_init(ohci_exynos_init);
283 static void __exit ohci_exynos_cleanup(void)
285 platform_driver_unregister(&exynos_ohci_driver);
287 module_exit(ohci_exynos_cleanup);
289 MODULE_ALIAS("platform:exynos-ohci");
290 MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
291 MODULE_DESCRIPTION("OHCI support for Samsung S5P/Exynos SoC Series");
292 MODULE_LICENSE("GPL v2");