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