GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / usb / host / ohci-exynos.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * SAMSUNG EXYNOS USB HOST OHCI Controller
4  *
5  * Copyright (C) 2011 Samsung Electronics Co.Ltd
6  * Author: Jingoo Han <jg1.han@samsung.com>
7  */
8
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>
19
20 #include "ohci.h"
21
22 #define DRIVER_DESC "OHCI EXYNOS driver"
23
24 static const char hcd_name[] = "ohci-exynos";
25 static struct hc_driver __read_mostly exynos_ohci_hc_driver;
26
27 #define to_exynos_ohci(hcd) (struct exynos_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
28
29 #define PHY_NUMBER 3
30
31 struct exynos_ohci_hcd {
32         struct clk *clk;
33         struct phy *phy[PHY_NUMBER];
34 };
35
36 static int exynos_ohci_get_phy(struct device *dev,
37                                 struct exynos_ohci_hcd *exynos_ohci)
38 {
39         struct device_node *child;
40         struct phy *phy;
41         int phy_number;
42         int ret;
43
44         /* Get PHYs for the controller */
45         for_each_available_child_of_node(dev->of_node, child) {
46                 ret = of_property_read_u32(child, "reg", &phy_number);
47                 if (ret) {
48                         dev_err(dev, "Failed to parse device tree\n");
49                         of_node_put(child);
50                         return ret;
51                 }
52
53                 if (phy_number >= PHY_NUMBER) {
54                         dev_err(dev, "Invalid number of PHYs\n");
55                         of_node_put(child);
56                         return -EINVAL;
57                 }
58
59                 phy = devm_of_phy_get(dev, child, NULL);
60                 exynos_ohci->phy[phy_number] = phy;
61                 if (IS_ERR(phy)) {
62                         ret = PTR_ERR(phy);
63                         if (ret == -EPROBE_DEFER) {
64                                 of_node_put(child);
65                                 return ret;
66                         } else if (ret != -ENOSYS && ret != -ENODEV) {
67                                 dev_err(dev,
68                                         "Error retrieving usb2 phy: %d\n", ret);
69                                 of_node_put(child);
70                                 return ret;
71                         }
72                 }
73         }
74
75         return 0;
76 }
77
78 static int exynos_ohci_phy_enable(struct device *dev)
79 {
80         struct usb_hcd *hcd = dev_get_drvdata(dev);
81         struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
82         int i;
83         int ret = 0;
84
85         for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
86                 if (!IS_ERR(exynos_ohci->phy[i]))
87                         ret = phy_power_on(exynos_ohci->phy[i]);
88         if (ret)
89                 for (i--; i >= 0; i--)
90                         if (!IS_ERR(exynos_ohci->phy[i]))
91                                 phy_power_off(exynos_ohci->phy[i]);
92
93         return ret;
94 }
95
96 static void exynos_ohci_phy_disable(struct device *dev)
97 {
98         struct usb_hcd *hcd = dev_get_drvdata(dev);
99         struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
100         int i;
101
102         for (i = 0; i < PHY_NUMBER; i++)
103                 if (!IS_ERR(exynos_ohci->phy[i]))
104                         phy_power_off(exynos_ohci->phy[i]);
105 }
106
107 static int exynos_ohci_probe(struct platform_device *pdev)
108 {
109         struct exynos_ohci_hcd *exynos_ohci;
110         struct usb_hcd *hcd;
111         struct resource *res;
112         int irq;
113         int err;
114
115         /*
116          * Right now device-tree probed devices don't get dma_mask set.
117          * Since shared usb code relies on it, set it here for now.
118          * Once we move to full device tree support this will vanish off.
119          */
120         err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
121         if (err)
122                 return err;
123
124         hcd = usb_create_hcd(&exynos_ohci_hc_driver,
125                                 &pdev->dev, dev_name(&pdev->dev));
126         if (!hcd) {
127                 dev_err(&pdev->dev, "Unable to create HCD\n");
128                 return -ENOMEM;
129         }
130
131         exynos_ohci = to_exynos_ohci(hcd);
132
133         err = exynos_ohci_get_phy(&pdev->dev, exynos_ohci);
134         if (err)
135                 goto fail_clk;
136
137         exynos_ohci->clk = devm_clk_get(&pdev->dev, "usbhost");
138
139         if (IS_ERR(exynos_ohci->clk)) {
140                 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
141                 err = PTR_ERR(exynos_ohci->clk);
142                 goto fail_clk;
143         }
144
145         err = clk_prepare_enable(exynos_ohci->clk);
146         if (err)
147                 goto fail_clk;
148
149         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
150         hcd->regs = devm_ioremap_resource(&pdev->dev, res);
151         if (IS_ERR(hcd->regs)) {
152                 err = PTR_ERR(hcd->regs);
153                 goto fail_io;
154         }
155         hcd->rsrc_start = res->start;
156         hcd->rsrc_len = resource_size(res);
157
158         irq = platform_get_irq(pdev, 0);
159         if (irq < 0) {
160                 err = irq;
161                 goto fail_io;
162         }
163
164         platform_set_drvdata(pdev, hcd);
165
166         err = exynos_ohci_phy_enable(&pdev->dev);
167         if (err) {
168                 dev_err(&pdev->dev, "Failed to enable USB phy\n");
169                 goto fail_io;
170         }
171
172         err = usb_add_hcd(hcd, irq, IRQF_SHARED);
173         if (err) {
174                 dev_err(&pdev->dev, "Failed to add USB HCD\n");
175                 goto fail_add_hcd;
176         }
177         device_wakeup_enable(hcd->self.controller);
178         return 0;
179
180 fail_add_hcd:
181         exynos_ohci_phy_disable(&pdev->dev);
182 fail_io:
183         clk_disable_unprepare(exynos_ohci->clk);
184 fail_clk:
185         usb_put_hcd(hcd);
186         return err;
187 }
188
189 static int exynos_ohci_remove(struct platform_device *pdev)
190 {
191         struct usb_hcd *hcd = platform_get_drvdata(pdev);
192         struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
193
194         usb_remove_hcd(hcd);
195
196         exynos_ohci_phy_disable(&pdev->dev);
197
198         clk_disable_unprepare(exynos_ohci->clk);
199
200         usb_put_hcd(hcd);
201
202         return 0;
203 }
204
205 static void exynos_ohci_shutdown(struct platform_device *pdev)
206 {
207         struct usb_hcd *hcd = platform_get_drvdata(pdev);
208
209         if (hcd->driver->shutdown)
210                 hcd->driver->shutdown(hcd);
211 }
212
213 #ifdef CONFIG_PM
214 static int exynos_ohci_suspend(struct device *dev)
215 {
216         struct usb_hcd *hcd = dev_get_drvdata(dev);
217         struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
218         bool do_wakeup = device_may_wakeup(dev);
219         int rc = ohci_suspend(hcd, do_wakeup);
220
221         if (rc)
222                 return rc;
223
224         exynos_ohci_phy_disable(dev);
225
226         clk_disable_unprepare(exynos_ohci->clk);
227
228         return 0;
229 }
230
231 static int exynos_ohci_resume(struct device *dev)
232 {
233         struct usb_hcd *hcd                     = dev_get_drvdata(dev);
234         struct exynos_ohci_hcd *exynos_ohci     = to_exynos_ohci(hcd);
235         int ret;
236
237         clk_prepare_enable(exynos_ohci->clk);
238
239         ret = exynos_ohci_phy_enable(dev);
240         if (ret) {
241                 dev_err(dev, "Failed to enable USB phy\n");
242                 clk_disable_unprepare(exynos_ohci->clk);
243                 return ret;
244         }
245
246         ohci_resume(hcd, false);
247
248         return 0;
249 }
250 #else
251 #define exynos_ohci_suspend     NULL
252 #define exynos_ohci_resume      NULL
253 #endif
254
255 static const struct ohci_driver_overrides exynos_overrides __initconst = {
256         .extra_priv_size =      sizeof(struct exynos_ohci_hcd),
257 };
258
259 static const struct dev_pm_ops exynos_ohci_pm_ops = {
260         .suspend        = exynos_ohci_suspend,
261         .resume         = exynos_ohci_resume,
262 };
263
264 #ifdef CONFIG_OF
265 static const struct of_device_id exynos_ohci_match[] = {
266         { .compatible = "samsung,exynos4210-ohci" },
267         {},
268 };
269 MODULE_DEVICE_TABLE(of, exynos_ohci_match);
270 #endif
271
272 static struct platform_driver exynos_ohci_driver = {
273         .probe          = exynos_ohci_probe,
274         .remove         = exynos_ohci_remove,
275         .shutdown       = exynos_ohci_shutdown,
276         .driver = {
277                 .name   = "exynos-ohci",
278                 .pm     = &exynos_ohci_pm_ops,
279                 .of_match_table = of_match_ptr(exynos_ohci_match),
280         }
281 };
282 static int __init ohci_exynos_init(void)
283 {
284         if (usb_disabled())
285                 return -ENODEV;
286
287         pr_info("%s: " DRIVER_DESC "\n", hcd_name);
288         ohci_init_driver(&exynos_ohci_hc_driver, &exynos_overrides);
289         return platform_driver_register(&exynos_ohci_driver);
290 }
291 module_init(ohci_exynos_init);
292
293 static void __exit ohci_exynos_cleanup(void)
294 {
295         platform_driver_unregister(&exynos_ohci_driver);
296 }
297 module_exit(ohci_exynos_cleanup);
298
299 MODULE_ALIAS("platform:exynos-ohci");
300 MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
301 MODULE_LICENSE("GPL v2");