blob: a72ab8fe8cd355ae038be8924b96239d055aab86 [file] [log] [blame]
Jingoo Han62194242011-12-23 11:20:54 +09001/*
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>
Manjunath Goudar50a97e02013-09-21 16:38:38 +053015#include <linux/dma-mapping.h>
16#include <linux/io.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
Vivek Gautamd5138932012-07-16 11:25:36 +053019#include <linux/of.h>
Jingoo Han62194242011-12-23 11:20:54 +090020#include <linux/platform_device.h>
Vivek Gautam7d28e542014-05-05 10:32:57 +053021#include <linux/phy/phy.h>
Vivek Gautamed993bf2013-01-22 18:30:43 +053022#include <linux/usb/phy.h>
Vivek Gautamb506eeb2013-01-22 18:30:40 +053023#include <linux/usb/samsung_usb_phy.h>
Manjunath Goudar50a97e02013-09-21 16:38:38 +053024#include <linux/usb.h>
25#include <linux/usb/hcd.h>
26#include <linux/usb/otg.h>
27
28#include "ohci.h"
29
30#define DRIVER_DESC "OHCI EXYNOS driver"
31
32static const char hcd_name[] = "ohci-exynos";
33static struct hc_driver __read_mostly exynos_ohci_hc_driver;
34
35#define to_exynos_ohci(hcd) (struct exynos_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
Jingoo Han62194242011-12-23 11:20:54 +090036
Vivek Gautam7d28e542014-05-05 10:32:57 +053037#define PHY_NUMBER 3
38
Jingoo Han62194242011-12-23 11:20:54 +090039struct exynos_ohci_hcd {
Jingoo Han62194242011-12-23 11:20:54 +090040 struct clk *clk;
Vivek Gautamed993bf2013-01-22 18:30:43 +053041 struct usb_phy *phy;
42 struct usb_otg *otg;
Vivek Gautam7d28e542014-05-05 10:32:57 +053043 struct phy *phy_g[PHY_NUMBER];
Jingoo Han62194242011-12-23 11:20:54 +090044};
45
Vivek Gautam7d28e542014-05-05 10:32:57 +053046static int exynos_ohci_get_phy(struct device *dev,
47 struct exynos_ohci_hcd *exynos_ohci)
48{
49 struct device_node *child;
50 struct phy *phy;
51 int phy_number;
52 int ret = 0;
53
54 exynos_ohci->phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
55 if (IS_ERR(exynos_ohci->phy)) {
56 ret = PTR_ERR(exynos_ohci->phy);
57 if (ret != -ENXIO && ret != -ENODEV) {
58 dev_err(dev, "no usb2 phy configured\n");
59 return ret;
60 }
61 dev_dbg(dev, "Failed to get usb2 phy\n");
62 } else {
63 exynos_ohci->otg = exynos_ohci->phy->otg;
64 }
65
66 /*
67 * Getting generic phy:
68 * We are keeping both types of phys as a part of transiting OHCI
69 * to generic phy framework, so as to maintain backward compatibilty
70 * with old DTB.
71 * If there are existing devices using DTB files built from them,
72 * to remove the support for old bindings in this driver,
73 * we need to make sure that such devices have their DTBs
74 * updated to ones built from new DTS.
75 */
76 for_each_available_child_of_node(dev->of_node, child) {
77 ret = of_property_read_u32(child, "reg", &phy_number);
78 if (ret) {
79 dev_err(dev, "Failed to parse device tree\n");
80 of_node_put(child);
81 return ret;
82 }
83
84 if (phy_number >= PHY_NUMBER) {
85 dev_err(dev, "Invalid number of PHYs\n");
86 of_node_put(child);
87 return -EINVAL;
88 }
89
Sachin Kamat473e92e2014-06-06 14:13:44 +053090 phy = devm_of_phy_get(dev, child, NULL);
Vivek Gautam7d28e542014-05-05 10:32:57 +053091 of_node_put(child);
92 if (IS_ERR(phy)) {
93 ret = PTR_ERR(phy);
94 if (ret != -ENOSYS && ret != -ENODEV) {
95 dev_err(dev, "no usb2 phy configured\n");
96 return ret;
97 }
98 dev_dbg(dev, "Failed to get usb2 phy\n");
99 }
100 exynos_ohci->phy_g[phy_number] = phy;
101 }
102
103 return ret;
104}
105
106static int exynos_ohci_phy_enable(struct device *dev)
Vivek Gautamed993bf2013-01-22 18:30:43 +0530107{
Vivek Gautam54969ed2014-05-05 10:33:42 +0530108 struct usb_hcd *hcd = dev_get_drvdata(dev);
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530109 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
Vivek Gautam7d28e542014-05-05 10:32:57 +0530110 int i;
111 int ret = 0;
Vivek Gautamed993bf2013-01-22 18:30:43 +0530112
Vivek Gautam7d28e542014-05-05 10:32:57 +0530113 if (!IS_ERR(exynos_ohci->phy))
114 return usb_phy_init(exynos_ohci->phy);
115
116 for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
117 if (!IS_ERR(exynos_ohci->phy_g[i]))
118 ret = phy_power_on(exynos_ohci->phy_g[i]);
119 if (ret)
120 for (i--; i >= 0; i--)
121 if (!IS_ERR(exynos_ohci->phy_g[i]))
122 phy_power_off(exynos_ohci->phy_g[i]);
123
124 return ret;
Vivek Gautamed993bf2013-01-22 18:30:43 +0530125}
126
Vivek Gautam54969ed2014-05-05 10:33:42 +0530127static void exynos_ohci_phy_disable(struct device *dev)
Vivek Gautamed993bf2013-01-22 18:30:43 +0530128{
Vivek Gautam54969ed2014-05-05 10:33:42 +0530129 struct usb_hcd *hcd = dev_get_drvdata(dev);
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530130 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
Vivek Gautam7d28e542014-05-05 10:32:57 +0530131 int i;
Vivek Gautamed993bf2013-01-22 18:30:43 +0530132
Vivek Gautam7d28e542014-05-05 10:32:57 +0530133 if (!IS_ERR(exynos_ohci->phy)) {
Vivek Gautamed993bf2013-01-22 18:30:43 +0530134 usb_phy_shutdown(exynos_ohci->phy);
Vivek Gautam7d28e542014-05-05 10:32:57 +0530135 return;
136 }
137
138 for (i = 0; i < PHY_NUMBER; i++)
139 if (!IS_ERR(exynos_ohci->phy_g[i]))
140 phy_power_off(exynos_ohci->phy_g[i]);
Vivek Gautamed993bf2013-01-22 18:30:43 +0530141}
142
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500143static int exynos_ohci_probe(struct platform_device *pdev)
Jingoo Han62194242011-12-23 11:20:54 +0900144{
Jingoo Han62194242011-12-23 11:20:54 +0900145 struct exynos_ohci_hcd *exynos_ohci;
146 struct usb_hcd *hcd;
Jingoo Han62194242011-12-23 11:20:54 +0900147 struct resource *res;
148 int irq;
149 int err;
150
Vivek Gautamd5138932012-07-16 11:25:36 +0530151 /*
152 * Right now device-tree probed devices don't get dma_mask set.
153 * Since shared usb code relies on it, set it here for now.
154 * Once we move to full device tree support this will vanish off.
155 */
Russell Kinge1fd7342013-06-27 12:36:37 +0100156 err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
Russell King22d9d8e2013-06-10 16:28:49 +0100157 if (err)
158 return err;
Vivek Gautamd5138932012-07-16 11:25:36 +0530159
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530160 hcd = usb_create_hcd(&exynos_ohci_hc_driver,
161 &pdev->dev, dev_name(&pdev->dev));
162 if (!hcd) {
163 dev_err(&pdev->dev, "Unable to create HCD\n");
Jingoo Han62194242011-12-23 11:20:54 +0900164 return -ENOMEM;
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530165 }
166
167 exynos_ohci = to_exynos_ohci(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900168
Thomas Abraham28717822013-04-11 17:12:30 +0530169 if (of_device_is_compatible(pdev->dev.of_node,
170 "samsung,exynos5440-ohci"))
171 goto skip_phy;
172
Vivek Gautam7d28e542014-05-05 10:32:57 +0530173 err = exynos_ohci_get_phy(&pdev->dev, exynos_ohci);
174 if (err)
175 goto fail_clk;
Vivek Gautamed993bf2013-01-22 18:30:43 +0530176
Thomas Abraham28717822013-04-11 17:12:30 +0530177skip_phy:
Jingoo Han60d80ad2012-10-04 16:11:50 +0900178 exynos_ohci->clk = devm_clk_get(&pdev->dev, "usbhost");
Jingoo Han62194242011-12-23 11:20:54 +0900179
180 if (IS_ERR(exynos_ohci->clk)) {
181 dev_err(&pdev->dev, "Failed to get usbhost clock\n");
182 err = PTR_ERR(exynos_ohci->clk);
183 goto fail_clk;
184 }
185
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900186 err = clk_prepare_enable(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900187 if (err)
Jingoo Han60d80ad2012-10-04 16:11:50 +0900188 goto fail_clk;
Jingoo Han62194242011-12-23 11:20:54 +0900189
190 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
191 if (!res) {
192 dev_err(&pdev->dev, "Failed to get I/O memory\n");
193 err = -ENXIO;
194 goto fail_io;
195 }
196
197 hcd->rsrc_start = res->start;
198 hcd->rsrc_len = resource_size(res);
Vivek Gautambae00c12014-05-10 17:30:10 +0530199 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
200 if (IS_ERR(hcd->regs)) {
201 err = PTR_ERR(hcd->regs);
Jingoo Han62194242011-12-23 11:20:54 +0900202 goto fail_io;
203 }
204
205 irq = platform_get_irq(pdev, 0);
206 if (!irq) {
207 dev_err(&pdev->dev, "Failed to get IRQ\n");
208 err = -ENODEV;
Jingoo Han390a0a72012-06-28 16:30:30 +0900209 goto fail_io;
Jingoo Han62194242011-12-23 11:20:54 +0900210 }
211
Vivek Gautamed993bf2013-01-22 18:30:43 +0530212 if (exynos_ohci->otg)
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530213 exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
Vivek Gautamed993bf2013-01-22 18:30:43 +0530214
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530215 platform_set_drvdata(pdev, hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900216
Vivek Gautam7d28e542014-05-05 10:32:57 +0530217 err = exynos_ohci_phy_enable(&pdev->dev);
218 if (err) {
219 dev_err(&pdev->dev, "Failed to enable USB phy\n");
220 goto fail_io;
221 }
Jingoo Han62194242011-12-23 11:20:54 +0900222
223 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
224 if (err) {
225 dev_err(&pdev->dev, "Failed to add USB HCD\n");
Vivek Gautamed993bf2013-01-22 18:30:43 +0530226 goto fail_add_hcd;
Jingoo Han62194242011-12-23 11:20:54 +0900227 }
Peter Chen3c9740a2013-11-05 10:46:02 +0800228 device_wakeup_enable(hcd->self.controller);
Jingoo Han62194242011-12-23 11:20:54 +0900229 return 0;
230
Vivek Gautamed993bf2013-01-22 18:30:43 +0530231fail_add_hcd:
Vivek Gautam54969ed2014-05-05 10:33:42 +0530232 exynos_ohci_phy_disable(&pdev->dev);
Jingoo Han62194242011-12-23 11:20:54 +0900233fail_io:
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900234 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900235fail_clk:
236 usb_put_hcd(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900237 return err;
238}
239
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500240static int exynos_ohci_remove(struct platform_device *pdev)
Jingoo Han62194242011-12-23 11:20:54 +0900241{
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530242 struct usb_hcd *hcd = platform_get_drvdata(pdev);
243 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900244
245 usb_remove_hcd(hcd);
246
Vivek Gautamed993bf2013-01-22 18:30:43 +0530247 if (exynos_ohci->otg)
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530248 exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
Vivek Gautamed993bf2013-01-22 18:30:43 +0530249
Vivek Gautam54969ed2014-05-05 10:33:42 +0530250 exynos_ohci_phy_disable(&pdev->dev);
Jingoo Han62194242011-12-23 11:20:54 +0900251
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900252 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Han62194242011-12-23 11:20:54 +0900253
254 usb_put_hcd(hcd);
Jingoo Han62194242011-12-23 11:20:54 +0900255
256 return 0;
257}
258
259static void exynos_ohci_shutdown(struct platform_device *pdev)
260{
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530261 struct usb_hcd *hcd = platform_get_drvdata(pdev);
Jingoo Han62194242011-12-23 11:20:54 +0900262
263 if (hcd->driver->shutdown)
264 hcd->driver->shutdown(hcd);
265}
266
267#ifdef CONFIG_PM
268static int exynos_ohci_suspend(struct device *dev)
269{
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530270 struct usb_hcd *hcd = dev_get_drvdata(dev);
271 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
Majunath Goudar14982e32013-11-13 17:40:20 +0530272 bool do_wakeup = device_may_wakeup(dev);
Majunath Goudar14982e32013-11-13 17:40:20 +0530273 int rc = ohci_suspend(hcd, do_wakeup);
Jingoo Han62194242011-12-23 11:20:54 +0900274
Majunath Goudar14982e32013-11-13 17:40:20 +0530275 if (rc)
276 return rc;
277
Vivek Gautamed993bf2013-01-22 18:30:43 +0530278 if (exynos_ohci->otg)
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530279 exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
Vivek Gautamed993bf2013-01-22 18:30:43 +0530280
Vivek Gautam54969ed2014-05-05 10:33:42 +0530281 exynos_ohci_phy_disable(dev);
Jingoo Hane864abe2012-06-28 16:49:42 +0900282
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900283 clk_disable_unprepare(exynos_ohci->clk);
Jingoo Hane864abe2012-06-28 16:49:42 +0900284
Majunath Goudar14982e32013-11-13 17:40:20 +0530285 return 0;
Jingoo Han62194242011-12-23 11:20:54 +0900286}
287
288static int exynos_ohci_resume(struct device *dev)
289{
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530290 struct usb_hcd *hcd = dev_get_drvdata(dev);
291 struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
Vivek Gautam7d28e542014-05-05 10:32:57 +0530292 int ret;
Jingoo Han62194242011-12-23 11:20:54 +0900293
Thomas Abrahamc05c9462012-10-03 08:41:37 +0900294 clk_prepare_enable(exynos_ohci->clk);
Jingoo Hane864abe2012-06-28 16:49:42 +0900295
Vivek Gautamed993bf2013-01-22 18:30:43 +0530296 if (exynos_ohci->otg)
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530297 exynos_ohci->otg->set_host(exynos_ohci->otg, &hcd->self);
Vivek Gautamed993bf2013-01-22 18:30:43 +0530298
Vivek Gautam7d28e542014-05-05 10:32:57 +0530299 ret = exynos_ohci_phy_enable(dev);
300 if (ret) {
301 dev_err(dev, "Failed to enable USB phy\n");
302 clk_disable_unprepare(exynos_ohci->clk);
303 return ret;
304 }
Jingoo Han62194242011-12-23 11:20:54 +0900305
Florian Fainellicfa49b42012-10-08 15:11:29 +0200306 ohci_resume(hcd, false);
Jingoo Han62194242011-12-23 11:20:54 +0900307
308 return 0;
309}
310#else
311#define exynos_ohci_suspend NULL
312#define exynos_ohci_resume NULL
313#endif
314
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530315static const struct ohci_driver_overrides exynos_overrides __initconst = {
316 .extra_priv_size = sizeof(struct exynos_ohci_hcd),
317};
318
Jingoo Han62194242011-12-23 11:20:54 +0900319static const struct dev_pm_ops exynos_ohci_pm_ops = {
320 .suspend = exynos_ohci_suspend,
321 .resume = exynos_ohci_resume,
322};
323
Vivek Gautamd5138932012-07-16 11:25:36 +0530324#ifdef CONFIG_OF
325static const struct of_device_id exynos_ohci_match[] = {
Vivek Gautam6e247772013-01-24 19:15:29 +0530326 { .compatible = "samsung,exynos4210-ohci" },
Thomas Abraham28717822013-04-11 17:12:30 +0530327 { .compatible = "samsung,exynos5440-ohci" },
Vivek Gautamd5138932012-07-16 11:25:36 +0530328 {},
329};
330MODULE_DEVICE_TABLE(of, exynos_ohci_match);
331#endif
332
Jingoo Han62194242011-12-23 11:20:54 +0900333static struct platform_driver exynos_ohci_driver = {
334 .probe = exynos_ohci_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500335 .remove = exynos_ohci_remove,
Jingoo Han62194242011-12-23 11:20:54 +0900336 .shutdown = exynos_ohci_shutdown,
337 .driver = {
338 .name = "exynos-ohci",
339 .owner = THIS_MODULE,
340 .pm = &exynos_ohci_pm_ops,
Vivek Gautamd5138932012-07-16 11:25:36 +0530341 .of_match_table = of_match_ptr(exynos_ohci_match),
Jingoo Han62194242011-12-23 11:20:54 +0900342 }
343};
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530344static int __init ohci_exynos_init(void)
345{
346 if (usb_disabled())
347 return -ENODEV;
348
349 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
350 ohci_init_driver(&exynos_ohci_hc_driver, &exynos_overrides);
351 return platform_driver_register(&exynos_ohci_driver);
352}
353module_init(ohci_exynos_init);
354
355static void __exit ohci_exynos_cleanup(void)
356{
357 platform_driver_unregister(&exynos_ohci_driver);
358}
359module_exit(ohci_exynos_cleanup);
Jingoo Han62194242011-12-23 11:20:54 +0900360
361MODULE_ALIAS("platform:exynos-ohci");
362MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
Manjunath Goudar50a97e02013-09-21 16:38:38 +0530363MODULE_LICENSE("GPL v2");