Merge tag 'mm-hotfixes-stable-2025-07-11-16-16' of git://git.kernel.org/pub/scm/linux...
[linux-2.6-block.git] / drivers / usb / host / ohci-exynos.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0+
62194244
JH
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>
62194244
JH
7 */
8
9#include <linux/clk.h>
50a97e05
MG
10#include <linux/dma-mapping.h>
11#include <linux/io.h>
12#include <linux/kernel.h>
13#include <linux/module.h>
d5138930 14#include <linux/of.h>
62194244 15#include <linux/platform_device.h>
7d28e54b 16#include <linux/phy/phy.h>
50a97e05
MG
17#include <linux/usb.h>
18#include <linux/usb/hcd.h>
50a97e05
MG
19
20#include "ohci.h"
21
dea7b202 22#define DRIVER_DESC "OHCI Exynos driver"
50a97e05 23
50a97e05
MG
24static struct hc_driver __read_mostly exynos_ohci_hc_driver;
25
26#define to_exynos_ohci(hcd) (struct exynos_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
62194244 27
7d28e54b
VG
28#define PHY_NUMBER 3
29
62194244 30struct exynos_ohci_hcd {
62194244 31 struct clk *clk;
01d40714 32 struct device_node *of_node;
2db94162 33 struct phy *phy[PHY_NUMBER];
214b606e 34 bool legacy_phy;
62194244
JH
35};
36
7d28e54b
VG
37static int exynos_ohci_get_phy(struct device *dev,
38 struct exynos_ohci_hcd *exynos_ohci)
39{
7d28e54b 40 struct phy *phy;
214b606e 41 int phy_number, num_phys;
2db94162 42 int ret;
7d28e54b 43
2db94162 44 /* Get PHYs for the controller */
214b606e
MS
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;
52 }
53 if (num_phys > 0)
54 return 0;
55
56 /* Get PHYs using legacy bindings */
e24ed5e2 57 for_each_available_child_of_node_scoped(dev->of_node, child) {
7d28e54b
VG
58 ret = of_property_read_u32(child, "reg", &phy_number);
59 if (ret) {
60 dev_err(dev, "Failed to parse device tree\n");
7d28e54b
VG
61 return ret;
62 }
63
64 if (phy_number >= PHY_NUMBER) {
65 dev_err(dev, "Invalid number of PHYs\n");
7d28e54b
VG
66 return -EINVAL;
67 }
68
41a435e3 69 phy = devm_of_phy_optional_get(dev, child, NULL);
2db94162 70 exynos_ohci->phy[phy_number] = phy;
e24ed5e2 71 if (IS_ERR(phy))
41a435e3 72 return PTR_ERR(phy);
7d28e54b
VG
73 }
74
214b606e 75 exynos_ohci->legacy_phy = true;
2db94162 76 return 0;
7d28e54b
VG
77}
78
79static int exynos_ohci_phy_enable(struct device *dev)
ed993bf1 80{
54969ed6 81 struct usb_hcd *hcd = dev_get_drvdata(dev);
50a97e05 82 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
7d28e54b
VG
83 int i;
84 int ret = 0;
85
7d28e54b 86 for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
41a435e3 87 ret = phy_power_on(exynos_ohci->phy[i]);
7d28e54b
VG
88 if (ret)
89 for (i--; i >= 0; i--)
41a435e3 90 phy_power_off(exynos_ohci->phy[i]);
7d28e54b
VG
91
92 return ret;
ed993bf1
VG
93}
94
54969ed6 95static void exynos_ohci_phy_disable(struct device *dev)
ed993bf1 96{
54969ed6 97 struct usb_hcd *hcd = dev_get_drvdata(dev);
50a97e05 98 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
7d28e54b 99 int i;
ed993bf1 100
7d28e54b 101 for (i = 0; i < PHY_NUMBER; i++)
41a435e3 102 phy_power_off(exynos_ohci->phy[i]);
ed993bf1
VG
103}
104
41ac7b3a 105static int exynos_ohci_probe(struct platform_device *pdev)
62194244 106{
62194244
JH
107 struct exynos_ohci_hcd *exynos_ohci;
108 struct usb_hcd *hcd;
62194244
JH
109 struct resource *res;
110 int irq;
111 int err;
112
d5138930
VG
113 /*
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.
117 */
e1fd7341 118 err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
22d9d8e8
RK
119 if (err)
120 return err;
d5138930 121
50a97e05
MG
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");
62194244 126 return -ENOMEM;
50a97e05
MG
127 }
128
129 exynos_ohci = to_exynos_ohci(hcd);
62194244 130
7d28e54b
VG
131 err = exynos_ohci_get_phy(&pdev->dev, exynos_ohci);
132 if (err)
3d284c95 133 goto fail_io;
ed993bf1 134
3d284c95 135 exynos_ohci->clk = devm_clk_get_enabled(&pdev->dev, "usbhost");
62194244
JH
136
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);
3d284c95 140 goto fail_io;
62194244
JH
141 }
142
31a7b792 143 hcd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
bae00c1a
VG
144 if (IS_ERR(hcd->regs)) {
145 err = PTR_ERR(hcd->regs);
62194244
JH
146 goto fail_io;
147 }
b87a9c52
VB
148 hcd->rsrc_start = res->start;
149 hcd->rsrc_len = resource_size(res);
62194244
JH
150
151 irq = platform_get_irq(pdev, 0);
1d416983
TB
152 if (irq < 0) {
153 err = irq;
390a0a78 154 goto fail_io;
62194244
JH
155 }
156
50a97e05 157 platform_set_drvdata(pdev, hcd);
62194244 158
7d28e54b
VG
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;
163 }
62194244 164
01d40714 165 /*
214b606e
MS
166 * Workaround: reset of_node pointer to avoid conflict between legacy
167 * Exynos OHCI port subnodes and generic USB device bindings
01d40714
MS
168 */
169 exynos_ohci->of_node = pdev->dev.of_node;
214b606e
MS
170 if (exynos_ohci->legacy_phy)
171 pdev->dev.of_node = NULL;
01d40714 172
62194244
JH
173 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
174 if (err) {
175 dev_err(&pdev->dev, "Failed to add USB HCD\n");
ed993bf1 176 goto fail_add_hcd;
62194244 177 }
3c9740a1 178 device_wakeup_enable(hcd->self.controller);
62194244
JH
179 return 0;
180
ed993bf1 181fail_add_hcd:
54969ed6 182 exynos_ohci_phy_disable(&pdev->dev);
01d40714 183 pdev->dev.of_node = exynos_ohci->of_node;
62194244 184fail_io:
62194244 185 usb_put_hcd(hcd);
62194244
JH
186 return err;
187}
188
16fe06cd 189static void exynos_ohci_remove(struct platform_device *pdev)
62194244 190{
50a97e05
MG
191 struct usb_hcd *hcd = platform_get_drvdata(pdev);
192 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
62194244 193
01d40714
MS
194 pdev->dev.of_node = exynos_ohci->of_node;
195
62194244
JH
196 usb_remove_hcd(hcd);
197
54969ed6 198 exynos_ohci_phy_disable(&pdev->dev);
62194244 199
62194244 200 usb_put_hcd(hcd);
62194244
JH
201}
202
203static void exynos_ohci_shutdown(struct platform_device *pdev)
204{
50a97e05 205 struct usb_hcd *hcd = platform_get_drvdata(pdev);
62194244
JH
206
207 if (hcd->driver->shutdown)
208 hcd->driver->shutdown(hcd);
209}
210
62194244
JH
211static int exynos_ohci_suspend(struct device *dev)
212{
50a97e05
MG
213 struct usb_hcd *hcd = dev_get_drvdata(dev);
214 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
14982e31 215 bool do_wakeup = device_may_wakeup(dev);
14982e31 216 int rc = ohci_suspend(hcd, do_wakeup);
62194244 217
14982e31
MG
218 if (rc)
219 return rc;
066ba8e0 220
54969ed6 221 exynos_ohci_phy_disable(dev);
e864abed 222
c05c946c 223 clk_disable_unprepare(exynos_ohci->clk);
e864abed 224
14982e31 225 return 0;
62194244
JH
226}
227
228static int exynos_ohci_resume(struct device *dev)
229{
50a97e05
MG
230 struct usb_hcd *hcd = dev_get_drvdata(dev);
231 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
7d28e54b 232 int ret;
62194244 233
c05c946c 234 clk_prepare_enable(exynos_ohci->clk);
e864abed 235
7d28e54b
VG
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;
241 }
62194244 242
cfa49b4b 243 ohci_resume(hcd, false);
62194244
JH
244
245 return 0;
246}
62194244 247
50a97e05
MG
248static const struct ohci_driver_overrides exynos_overrides __initconst = {
249 .extra_priv_size = sizeof(struct exynos_ohci_hcd),
250};
251
a6a243b6
AM
252static DEFINE_SIMPLE_DEV_PM_OPS(exynos_ohci_pm_ops,
253 exynos_ohci_suspend, exynos_ohci_resume);
62194244 254
d5138930
VG
255#ifdef CONFIG_OF
256static const struct of_device_id exynos_ohci_match[] = {
6e247777 257 { .compatible = "samsung,exynos4210-ohci" },
d5138930
VG
258 {},
259};
260MODULE_DEVICE_TABLE(of, exynos_ohci_match);
261#endif
262
62194244
JH
263static struct platform_driver exynos_ohci_driver = {
264 .probe = exynos_ohci_probe,
9a0749d6 265 .remove = exynos_ohci_remove,
62194244
JH
266 .shutdown = exynos_ohci_shutdown,
267 .driver = {
268 .name = "exynos-ohci",
a6a243b6 269 .pm = pm_ptr(&exynos_ohci_pm_ops),
d5138930 270 .of_match_table = of_match_ptr(exynos_ohci_match),
62194244
JH
271 }
272};
50a97e05
MG
273static int __init ohci_exynos_init(void)
274{
275 if (usb_disabled())
276 return -ENODEV;
277
50a97e05
MG
278 ohci_init_driver(&exynos_ohci_hc_driver, &exynos_overrides);
279 return platform_driver_register(&exynos_ohci_driver);
280}
281module_init(ohci_exynos_init);
282
283static void __exit ohci_exynos_cleanup(void)
284{
285 platform_driver_unregister(&exynos_ohci_driver);
286}
287module_exit(ohci_exynos_cleanup);
62194244
JH
288
289MODULE_ALIAS("platform:exynos-ohci");
290MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
9fdce69f 291MODULE_DESCRIPTION("OHCI support for Samsung S5P/Exynos SoC Series");
50a97e05 292MODULE_LICENSE("GPL v2");