blob: 8faec9dbbb84326d436ffc568451b322eb95a1a3 [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>
Richard Zhaob4dbb252012-09-12 14:58:06 +030023#include <linux/pinctrl/consumer.h>
Richard Zhao15302802012-07-07 22:56:48 +080024
25#include "ci.h"
Richard Zhaod142d6b2012-09-12 14:58:05 +030026#include "ci13xxx_imx.h"
Richard Zhao15302802012-07-07 22:56:48 +080027
28#define pdev_to_phy(pdev) \
29 ((struct usb_phy *)platform_get_drvdata(pdev))
30
31struct ci13xxx_imx_data {
32 struct device_node *phy_np;
33 struct usb_phy *phy;
34 struct platform_device *ci_pdev;
35 struct clk *clk;
36 struct regulator *reg_vbus;
37};
38
Richard Zhaod142d6b2012-09-12 14:58:05 +030039static const struct usbmisc_ops *usbmisc_ops;
40
41/* Common functions shared by usbmisc drivers */
42
43int usbmisc_set_ops(const struct usbmisc_ops *ops)
44{
45 if (usbmisc_ops)
46 return -EBUSY;
47
48 usbmisc_ops = ops;
49
50 return 0;
51}
52EXPORT_SYMBOL_GPL(usbmisc_set_ops);
53
54void usbmisc_unset_ops(const struct usbmisc_ops *ops)
55{
56 usbmisc_ops = NULL;
57}
58EXPORT_SYMBOL_GPL(usbmisc_unset_ops);
59
60int usbmisc_get_init_data(struct device *dev, struct usbmisc_usb_device *usbdev)
61{
62 struct device_node *np = dev->of_node;
63 struct of_phandle_args args;
64 int ret;
65
66 usbdev->dev = dev;
67
68 ret = of_parse_phandle_with_args(np, "fsl,usbmisc", "#index-cells",
69 0, &args);
70 if (ret) {
71 dev_err(dev, "Failed to parse property fsl,usbmisc, errno %d\n",
72 ret);
73 memset(usbdev, 0, sizeof(*usbdev));
74 return ret;
75 }
76 usbdev->index = args.args[0];
77 of_node_put(args.np);
78
79 if (of_find_property(np, "disable-over-current", NULL))
80 usbdev->disable_oc = 1;
81
Michael Grzeschika0685332013-03-30 12:54:01 +020082 if (of_find_property(np, "external-vbus-divider", NULL))
83 usbdev->evdo = 1;
84
Richard Zhaod142d6b2012-09-12 14:58:05 +030085 return 0;
86}
87EXPORT_SYMBOL_GPL(usbmisc_get_init_data);
88
89/* End of common functions shared by usbmisc drivers*/
90
Bill Pembertond3608b62012-11-19 13:24:34 -050091static struct ci13xxx_platform_data ci13xxx_imx_platdata = {
Richard Zhao15302802012-07-07 22:56:48 +080092 .name = "ci13xxx_imx",
93 .flags = CI13XXX_REQUIRE_TRANSCEIVER |
94 CI13XXX_PULLUP_ON_VBUS |
95 CI13XXX_DISABLE_STREAMING,
96 .capoffset = DEF_CAPOFFSET,
97};
98
Bill Pemberton41ac7b32012-11-19 13:21:48 -050099static int ci13xxx_imx_probe(struct platform_device *pdev)
Richard Zhao15302802012-07-07 22:56:48 +0800100{
101 struct ci13xxx_imx_data *data;
102 struct platform_device *plat_ci, *phy_pdev;
103 struct device_node *phy_np;
104 struct resource *res;
105 struct regulator *reg_vbus;
Richard Zhaob4dbb252012-09-12 14:58:06 +0300106 struct pinctrl *pinctrl;
Richard Zhao15302802012-07-07 22:56:48 +0800107 int ret;
108
Richard Zhaod142d6b2012-09-12 14:58:05 +0300109 if (of_find_property(pdev->dev.of_node, "fsl,usbmisc", NULL)
110 && !usbmisc_ops)
111 return -EPROBE_DEFER;
112
Richard Zhao15302802012-07-07 22:56:48 +0800113 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
114 if (!data) {
115 dev_err(&pdev->dev, "Failed to allocate CI13xxx-IMX data!\n");
116 return -ENOMEM;
117 }
118
119 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
120 if (!res) {
121 dev_err(&pdev->dev, "Can't get device resources!\n");
122 return -ENOENT;
123 }
124
Richard Zhaob4dbb252012-09-12 14:58:06 +0300125 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
126 if (IS_ERR(pinctrl))
127 dev_warn(&pdev->dev, "pinctrl get/select failed, err=%ld\n",
128 PTR_ERR(pinctrl));
129
Richard Zhao15302802012-07-07 22:56:48 +0800130 data->clk = devm_clk_get(&pdev->dev, NULL);
131 if (IS_ERR(data->clk)) {
132 dev_err(&pdev->dev,
133 "Failed to get clock, err=%ld\n", PTR_ERR(data->clk));
134 return PTR_ERR(data->clk);
135 }
136
137 ret = clk_prepare_enable(data->clk);
138 if (ret) {
139 dev_err(&pdev->dev,
140 "Failed to prepare or enable clock, err=%d\n", ret);
141 return ret;
142 }
143
144 phy_np = of_parse_phandle(pdev->dev.of_node, "fsl,usbphy", 0);
145 if (phy_np) {
146 data->phy_np = phy_np;
147 phy_pdev = of_find_device_by_node(phy_np);
148 if (phy_pdev) {
149 struct usb_phy *phy;
150 phy = pdev_to_phy(phy_pdev);
151 if (phy &&
152 try_module_get(phy_pdev->dev.driver->owner)) {
153 usb_phy_init(phy);
154 data->phy = phy;
155 }
156 }
157 }
158
159 /* we only support host now, so enable vbus here */
160 reg_vbus = devm_regulator_get(&pdev->dev, "vbus");
161 if (!IS_ERR(reg_vbus)) {
162 ret = regulator_enable(reg_vbus);
163 if (ret) {
164 dev_err(&pdev->dev,
165 "Failed to enable vbus regulator, err=%d\n",
166 ret);
167 goto put_np;
168 }
169 data->reg_vbus = reg_vbus;
170 } else {
171 reg_vbus = NULL;
172 }
173
174 ci13xxx_imx_platdata.phy = data->phy;
175
176 if (!pdev->dev.dma_mask) {
177 pdev->dev.dma_mask = devm_kzalloc(&pdev->dev,
178 sizeof(*pdev->dev.dma_mask), GFP_KERNEL);
179 if (!pdev->dev.dma_mask) {
180 ret = -ENOMEM;
181 dev_err(&pdev->dev, "Failed to alloc dma_mask!\n");
182 goto err;
183 }
184 *pdev->dev.dma_mask = DMA_BIT_MASK(32);
185 dma_set_coherent_mask(&pdev->dev, *pdev->dev.dma_mask);
186 }
Richard Zhaod142d6b2012-09-12 14:58:05 +0300187
188 if (usbmisc_ops && usbmisc_ops->init) {
189 ret = usbmisc_ops->init(&pdev->dev);
190 if (ret) {
191 dev_err(&pdev->dev,
192 "usbmisc init failed, ret=%d\n", ret);
193 goto err;
194 }
195 }
196
Richard Zhao15302802012-07-07 22:56:48 +0800197 plat_ci = ci13xxx_add_device(&pdev->dev,
198 pdev->resource, pdev->num_resources,
199 &ci13xxx_imx_platdata);
200 if (IS_ERR(plat_ci)) {
201 ret = PTR_ERR(plat_ci);
202 dev_err(&pdev->dev,
203 "Can't register ci_hdrc platform device, err=%d\n",
204 ret);
205 goto err;
206 }
207
Michael Grzeschika0685332013-03-30 12:54:01 +0200208 if (usbmisc_ops && usbmisc_ops->post) {
209 ret = usbmisc_ops->post(&pdev->dev);
210 if (ret) {
211 dev_err(&pdev->dev,
212 "usbmisc post failed, ret=%d\n", ret);
213 goto put_np;
214 }
215 }
216
Richard Zhao15302802012-07-07 22:56:48 +0800217 data->ci_pdev = plat_ci;
218 platform_set_drvdata(pdev, data);
219
220 pm_runtime_no_callbacks(&pdev->dev);
221 pm_runtime_enable(&pdev->dev);
222
223 return 0;
224
225err:
226 if (reg_vbus)
227 regulator_disable(reg_vbus);
228put_np:
229 if (phy_np)
230 of_node_put(phy_np);
231 clk_disable_unprepare(data->clk);
232 return ret;
233}
234
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500235static int ci13xxx_imx_remove(struct platform_device *pdev)
Richard Zhao15302802012-07-07 22:56:48 +0800236{
237 struct ci13xxx_imx_data *data = platform_get_drvdata(pdev);
238
239 pm_runtime_disable(&pdev->dev);
240 ci13xxx_remove_device(data->ci_pdev);
241
242 if (data->reg_vbus)
243 regulator_disable(data->reg_vbus);
244
245 if (data->phy) {
246 usb_phy_shutdown(data->phy);
247 module_put(data->phy->dev->driver->owner);
248 }
249
250 of_node_put(data->phy_np);
251
252 clk_disable_unprepare(data->clk);
253
254 platform_set_drvdata(pdev, NULL);
255
256 return 0;
257}
258
259static const struct of_device_id ci13xxx_imx_dt_ids[] = {
260 { .compatible = "fsl,imx27-usb", },
261 { /* sentinel */ }
262};
263MODULE_DEVICE_TABLE(of, ci13xxx_imx_dt_ids);
264
265static struct platform_driver ci13xxx_imx_driver = {
266 .probe = ci13xxx_imx_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500267 .remove = ci13xxx_imx_remove,
Richard Zhao15302802012-07-07 22:56:48 +0800268 .driver = {
269 .name = "imx_usb",
270 .owner = THIS_MODULE,
271 .of_match_table = ci13xxx_imx_dt_ids,
272 },
273};
274
275module_platform_driver(ci13xxx_imx_driver);
276
277MODULE_ALIAS("platform:imx-usb");
278MODULE_LICENSE("GPL v2");
279MODULE_DESCRIPTION("CI13xxx i.MX USB binding");
280MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
281MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");