xref: /linux/drivers/usb/host/ohci-exynos.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
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 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)
27 
28 #define PHY_NUMBER 3
29 
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;
35 };
36 
37 static int exynos_ohci_get_phy(struct device *dev,
38 				struct exynos_ohci_hcd *exynos_ohci)
39 {
40 	struct phy *phy;
41 	int phy_number, num_phys;
42 	int ret;
43 
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;
52 	}
53 	if (num_phys > 0)
54 		return 0;
55 
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;
62 		}
63 
64 		if (phy_number >= PHY_NUMBER) {
65 			dev_err(dev, "Invalid number of PHYs\n");
66 			return -EINVAL;
67 		}
68 
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);
73 	}
74 
75 	exynos_ohci->legacy_phy = true;
76 	return 0;
77 }
78 
79 static int exynos_ohci_phy_enable(struct device *dev)
80 {
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;
85 
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]);
91 
92 	return ret;
93 }
94 
95 static void exynos_ohci_phy_disable(struct device *dev)
96 {
97 	struct usb_hcd *hcd = dev_get_drvdata(dev);
98 	struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
99 	int i;
100 
101 	for (i = 0; i < PHY_NUMBER; i++)
102 		phy_power_off(exynos_ohci->phy[i]);
103 }
104 
105 static int exynos_ohci_probe(struct platform_device *pdev)
106 {
107 	struct exynos_ohci_hcd *exynos_ohci;
108 	struct usb_hcd *hcd;
109 	struct resource *res;
110 	int irq;
111 	int err;
112 
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 	 */
118 	err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
119 	if (err)
120 		return err;
121 
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;
127 	}
128 
129 	exynos_ohci = to_exynos_ohci(hcd);
130 
131 	err = exynos_ohci_get_phy(&pdev->dev, exynos_ohci);
132 	if (err)
133 		goto fail_io;
134 
135 	exynos_ohci->clk = devm_clk_get_enabled(&pdev->dev, "usbhost");
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);
140 		goto fail_io;
141 	}
142 
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;
147 	}
148 	hcd->rsrc_start = res->start;
149 	hcd->rsrc_len = resource_size(res);
150 
151 	irq = platform_get_irq(pdev, 0);
152 	if (irq < 0) {
153 		err = irq;
154 		goto fail_io;
155 	}
156 
157 	platform_set_drvdata(pdev, hcd);
158 
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 	}
164 
165 	/*
166 	 * Workaround: reset of_node pointer to avoid conflict between legacy
167 	 * Exynos OHCI port subnodes and generic USB device bindings
168 	 */
169 	exynos_ohci->of_node = pdev->dev.of_node;
170 	if (exynos_ohci->legacy_phy)
171 		pdev->dev.of_node = NULL;
172 
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;
177 	}
178 	device_wakeup_enable(hcd->self.controller);
179 	return 0;
180 
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;
187 }
188 
189 static void 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 	pdev->dev.of_node = exynos_ohci->of_node;
195 
196 	usb_remove_hcd(hcd);
197 
198 	exynos_ohci_phy_disable(&pdev->dev);
199 
200 	usb_put_hcd(hcd);
201 }
202 
203 static void exynos_ohci_shutdown(struct platform_device *pdev)
204 {
205 	struct usb_hcd *hcd = platform_get_drvdata(pdev);
206 
207 	if (hcd->driver->shutdown)
208 		hcd->driver->shutdown(hcd);
209 }
210 
211 static int exynos_ohci_suspend(struct device *dev)
212 {
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);
217 
218 	if (rc)
219 		return rc;
220 
221 	exynos_ohci_phy_disable(dev);
222 
223 	clk_disable_unprepare(exynos_ohci->clk);
224 
225 	return 0;
226 }
227 
228 static int exynos_ohci_resume(struct device *dev)
229 {
230 	struct usb_hcd *hcd			= dev_get_drvdata(dev);
231 	struct exynos_ohci_hcd *exynos_ohci	= to_exynos_ohci(hcd);
232 	int ret;
233 
234 	clk_prepare_enable(exynos_ohci->clk);
235 
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 	}
242 
243 	ohci_resume(hcd, false);
244 
245 	return 0;
246 }
247 
248 static const struct ohci_driver_overrides exynos_overrides __initconst = {
249 	.extra_priv_size =	sizeof(struct exynos_ohci_hcd),
250 };
251 
252 static DEFINE_SIMPLE_DEV_PM_OPS(exynos_ohci_pm_ops,
253 				exynos_ohci_suspend, exynos_ohci_resume);
254 
255 #ifdef CONFIG_OF
256 static const struct of_device_id exynos_ohci_match[] = {
257 	{ .compatible = "samsung,exynos4210-ohci" },
258 	{},
259 };
260 MODULE_DEVICE_TABLE(of, exynos_ohci_match);
261 #endif
262 
263 static struct platform_driver exynos_ohci_driver = {
264 	.probe		= exynos_ohci_probe,
265 	.remove_new	= 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),
271 	}
272 };
273 static int __init ohci_exynos_init(void)
274 {
275 	if (usb_disabled())
276 		return -ENODEV;
277 
278 	ohci_init_driver(&exynos_ohci_hc_driver, &exynos_overrides);
279 	return platform_driver_register(&exynos_ohci_driver);
280 }
281 module_init(ohci_exynos_init);
282 
283 static void __exit ohci_exynos_cleanup(void)
284 {
285 	platform_driver_unregister(&exynos_ohci_driver);
286 }
287 module_exit(ohci_exynos_cleanup);
288 
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");
293