blob: 5fac2a1b53e3f3743be042d33436470afc6e759d [file] [log] [blame]
Richard Zhao15302802012-07-07 22:56:48 +08001/*
2 * Copyright 2012 Freescale Semiconductor, Inc.
3 * Copyright (C) 2012 Marek Vasut <marex@denx.de>
4 * on behalf of DENX Software Engineering GmbH
5 *
6 * The code contained herein is licensed under the GNU General Public
7 * License. You may obtain a copy of the GNU General Public License
8 * Version 2 or later at the following locations:
9 *
10 * http://www.opensource.org/licenses/gpl-license.html
11 * http://www.gnu.org/copyleft/gpl.html
12 */
13
14#include <linux/module.h>
15#include <linux/of_platform.h>
16#include <linux/of_gpio.h>
17#include <linux/platform_device.h>
18#include <linux/pm_runtime.h>
19#include <linux/dma-mapping.h>
20#include <linux/usb/chipidea.h>
21#include <linux/clk.h>
22#include <linux/regulator/consumer.h>
23
24#include "ci.h"
Richard Zhaod142d6b2012-09-12 14:58:05 +030025#include "ci13xxx_imx.h"
Richard Zhao15302802012-07-07 22:56:48 +080026
27#define pdev_to_phy(pdev) \
28 ((struct usb_phy *)platform_get_drvdata(pdev))
29
30struct ci13xxx_imx_data {
31 struct device_node *phy_np;
32 struct usb_phy *phy;
33 struct platform_device *ci_pdev;
34 struct clk *clk;
35 struct regulator *reg_vbus;
36};
37
Richard Zhaod142d6b2012-09-12 14:58:05 +030038static const struct usbmisc_ops *usbmisc_ops;
39
40/* Common functions shared by usbmisc drivers */
41
42int usbmisc_set_ops(const struct usbmisc_ops *ops)
43{
44 if (usbmisc_ops)
45 return -EBUSY;
46
47 usbmisc_ops = ops;
48
49 return 0;
50}
51EXPORT_SYMBOL_GPL(usbmisc_set_ops);
52
53void usbmisc_unset_ops(const struct usbmisc_ops *ops)
54{
55 usbmisc_ops = NULL;
56}
57EXPORT_SYMBOL_GPL(usbmisc_unset_ops);
58
59int usbmisc_get_init_data(struct device *dev, struct usbmisc_usb_device *usbdev)
60{
61 struct device_node *np = dev->of_node;
62 struct of_phandle_args args;
63 int ret;
64
65 usbdev->dev = dev;
66
67 ret = of_parse_phandle_with_args(np, "fsl,usbmisc", "#index-cells",
68 0, &args);
69 if (ret) {
70 dev_err(dev, "Failed to parse property fsl,usbmisc, errno %d\n",
71 ret);
72 memset(usbdev, 0, sizeof(*usbdev));
73 return ret;
74 }
75 usbdev->index = args.args[0];
76 of_node_put(args.np);
77
78 if (of_find_property(np, "disable-over-current", NULL))
79 usbdev->disable_oc = 1;
80
Michael Grzeschika0685332013-03-30 12:54:01 +020081 if (of_find_property(np, "external-vbus-divider", NULL))
82 usbdev->evdo = 1;
83
Richard Zhaod142d6b2012-09-12 14:58:05 +030084 return 0;
85}
86EXPORT_SYMBOL_GPL(usbmisc_get_init_data);
87
88/* End of common functions shared by usbmisc drivers*/
89
Bill Pembertond3608b62012-11-19 13:24:34 -050090static struct ci13xxx_platform_data ci13xxx_imx_platdata = {
Richard Zhao15302802012-07-07 22:56:48 +080091 .name = "ci13xxx_imx",
92 .flags = CI13XXX_REQUIRE_TRANSCEIVER |
93 CI13XXX_PULLUP_ON_VBUS |
94 CI13XXX_DISABLE_STREAMING,
95 .capoffset = DEF_CAPOFFSET,
96};
97
Bill Pemberton41ac7b32012-11-19 13:21:48 -050098static int ci13xxx_imx_probe(struct platform_device *pdev)
Richard Zhao15302802012-07-07 22:56:48 +080099{
100 struct ci13xxx_imx_data *data;
Fabio Estevam770719d2013-06-13 17:59:48 +0300101 struct platform_device *phy_pdev;
Richard Zhao15302802012-07-07 22:56:48 +0800102 struct device_node *phy_np;
103 struct resource *res;
Richard Zhao15302802012-07-07 22:56:48 +0800104 int ret;
105
Richard Zhaod142d6b2012-09-12 14:58:05 +0300106 if (of_find_property(pdev->dev.of_node, "fsl,usbmisc", NULL)
107 && !usbmisc_ops)
108 return -EPROBE_DEFER;
109
Richard Zhao15302802012-07-07 22:56:48 +0800110 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
111 if (!data) {
112 dev_err(&pdev->dev, "Failed to allocate CI13xxx-IMX data!\n");
113 return -ENOMEM;
114 }
115
116 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
117 if (!res) {
118 dev_err(&pdev->dev, "Can't get device resources!\n");
119 return -ENOENT;
120 }
121
122 data->clk = devm_clk_get(&pdev->dev, NULL);
123 if (IS_ERR(data->clk)) {
124 dev_err(&pdev->dev,
125 "Failed to get clock, err=%ld\n", PTR_ERR(data->clk));
126 return PTR_ERR(data->clk);
127 }
128
129 ret = clk_prepare_enable(data->clk);
130 if (ret) {
131 dev_err(&pdev->dev,
132 "Failed to prepare or enable clock, err=%d\n", ret);
133 return ret;
134 }
135
136 phy_np = of_parse_phandle(pdev->dev.of_node, "fsl,usbphy", 0);
137 if (phy_np) {
138 data->phy_np = phy_np;
139 phy_pdev = of_find_device_by_node(phy_np);
140 if (phy_pdev) {
141 struct usb_phy *phy;
142 phy = pdev_to_phy(phy_pdev);
143 if (phy &&
144 try_module_get(phy_pdev->dev.driver->owner)) {
145 usb_phy_init(phy);
146 data->phy = phy;
147 }
148 }
149 }
150
151 /* we only support host now, so enable vbus here */
Fabio Estevame56ae542013-06-13 17:59:49 +0300152 data->reg_vbus = devm_regulator_get(&pdev->dev, "vbus");
153 if (!IS_ERR(data->reg_vbus)) {
154 ret = regulator_enable(data->reg_vbus);
Richard Zhao15302802012-07-07 22:56:48 +0800155 if (ret) {
156 dev_err(&pdev->dev,
157 "Failed to enable vbus regulator, err=%d\n",
158 ret);
159 goto put_np;
160 }
Richard Zhao15302802012-07-07 22:56:48 +0800161 } else {
Fabio Estevame56ae542013-06-13 17:59:49 +0300162 data->reg_vbus = NULL;
Richard Zhao15302802012-07-07 22:56:48 +0800163 }
164
165 ci13xxx_imx_platdata.phy = data->phy;
166
Stephen Warren3b9561e2013-05-07 16:53:52 -0600167 if (!pdev->dev.dma_mask)
168 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
169 if (!pdev->dev.coherent_dma_mask)
170 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300171
172 if (usbmisc_ops && usbmisc_ops->init) {
173 ret = usbmisc_ops->init(&pdev->dev);
174 if (ret) {
175 dev_err(&pdev->dev,
176 "usbmisc init failed, ret=%d\n", ret);
177 goto err;
178 }
179 }
180
Fabio Estevam770719d2013-06-13 17:59:48 +0300181 data->ci_pdev = ci13xxx_add_device(&pdev->dev,
Richard Zhao15302802012-07-07 22:56:48 +0800182 pdev->resource, pdev->num_resources,
183 &ci13xxx_imx_platdata);
Fabio Estevam770719d2013-06-13 17:59:48 +0300184 if (IS_ERR(data->ci_pdev)) {
185 ret = PTR_ERR(data->ci_pdev);
Richard Zhao15302802012-07-07 22:56:48 +0800186 dev_err(&pdev->dev,
187 "Can't register ci_hdrc platform device, err=%d\n",
188 ret);
189 goto err;
190 }
191
Michael Grzeschika0685332013-03-30 12:54:01 +0200192 if (usbmisc_ops && usbmisc_ops->post) {
193 ret = usbmisc_ops->post(&pdev->dev);
194 if (ret) {
195 dev_err(&pdev->dev,
196 "usbmisc post failed, ret=%d\n", ret);
Fabio Estevam770719d2013-06-13 17:59:48 +0300197 goto disable_device;
Michael Grzeschika0685332013-03-30 12:54:01 +0200198 }
199 }
200
Richard Zhao15302802012-07-07 22:56:48 +0800201 platform_set_drvdata(pdev, data);
202
203 pm_runtime_no_callbacks(&pdev->dev);
204 pm_runtime_enable(&pdev->dev);
205
206 return 0;
207
Fabio Estevam770719d2013-06-13 17:59:48 +0300208disable_device:
209 ci13xxx_remove_device(data->ci_pdev);
Richard Zhao15302802012-07-07 22:56:48 +0800210err:
Fabio Estevame56ae542013-06-13 17:59:49 +0300211 if (data->reg_vbus)
212 regulator_disable(data->reg_vbus);
Richard Zhao15302802012-07-07 22:56:48 +0800213put_np:
214 if (phy_np)
215 of_node_put(phy_np);
216 clk_disable_unprepare(data->clk);
217 return ret;
218}
219
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500220static int ci13xxx_imx_remove(struct platform_device *pdev)
Richard Zhao15302802012-07-07 22:56:48 +0800221{
222 struct ci13xxx_imx_data *data = platform_get_drvdata(pdev);
223
224 pm_runtime_disable(&pdev->dev);
225 ci13xxx_remove_device(data->ci_pdev);
226
227 if (data->reg_vbus)
228 regulator_disable(data->reg_vbus);
229
230 if (data->phy) {
231 usb_phy_shutdown(data->phy);
232 module_put(data->phy->dev->driver->owner);
233 }
234
Fabio Estevam785dabe2013-06-13 17:59:50 +0300235 if (data->phy_np)
236 of_node_put(data->phy_np);
Richard Zhao15302802012-07-07 22:56:48 +0800237
238 clk_disable_unprepare(data->clk);
239
Richard Zhao15302802012-07-07 22:56:48 +0800240 return 0;
241}
242
243static const struct of_device_id ci13xxx_imx_dt_ids[] = {
244 { .compatible = "fsl,imx27-usb", },
245 { /* sentinel */ }
246};
247MODULE_DEVICE_TABLE(of, ci13xxx_imx_dt_ids);
248
249static struct platform_driver ci13xxx_imx_driver = {
250 .probe = ci13xxx_imx_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500251 .remove = ci13xxx_imx_remove,
Richard Zhao15302802012-07-07 22:56:48 +0800252 .driver = {
253 .name = "imx_usb",
254 .owner = THIS_MODULE,
255 .of_match_table = ci13xxx_imx_dt_ids,
256 },
257};
258
259module_platform_driver(ci13xxx_imx_driver);
260
261MODULE_ALIAS("platform:imx-usb");
262MODULE_LICENSE("GPL v2");
263MODULE_DESCRIPTION("CI13xxx i.MX USB binding");
264MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
265MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");