blob: 96ac67bbd2db4c3b74545db13e9c2b2f288bfc8d [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
81 return 0;
82}
83EXPORT_SYMBOL_GPL(usbmisc_get_init_data);
84
85/* End of common functions shared by usbmisc drivers*/
86
Richard Zhao15302802012-07-07 22:56:48 +080087static struct ci13xxx_platform_data ci13xxx_imx_platdata __devinitdata = {
88 .name = "ci13xxx_imx",
89 .flags = CI13XXX_REQUIRE_TRANSCEIVER |
90 CI13XXX_PULLUP_ON_VBUS |
91 CI13XXX_DISABLE_STREAMING,
92 .capoffset = DEF_CAPOFFSET,
93};
94
95static int __devinit ci13xxx_imx_probe(struct platform_device *pdev)
96{
97 struct ci13xxx_imx_data *data;
98 struct platform_device *plat_ci, *phy_pdev;
99 struct device_node *phy_np;
100 struct resource *res;
101 struct regulator *reg_vbus;
102 int ret;
103
Richard Zhaod142d6b2012-09-12 14:58:05 +0300104 if (of_find_property(pdev->dev.of_node, "fsl,usbmisc", NULL)
105 && !usbmisc_ops)
106 return -EPROBE_DEFER;
107
Richard Zhao15302802012-07-07 22:56:48 +0800108 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
109 if (!data) {
110 dev_err(&pdev->dev, "Failed to allocate CI13xxx-IMX data!\n");
111 return -ENOMEM;
112 }
113
114 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
115 if (!res) {
116 dev_err(&pdev->dev, "Can't get device resources!\n");
117 return -ENOENT;
118 }
119
120 data->clk = devm_clk_get(&pdev->dev, NULL);
121 if (IS_ERR(data->clk)) {
122 dev_err(&pdev->dev,
123 "Failed to get clock, err=%ld\n", PTR_ERR(data->clk));
124 return PTR_ERR(data->clk);
125 }
126
127 ret = clk_prepare_enable(data->clk);
128 if (ret) {
129 dev_err(&pdev->dev,
130 "Failed to prepare or enable clock, err=%d\n", ret);
131 return ret;
132 }
133
134 phy_np = of_parse_phandle(pdev->dev.of_node, "fsl,usbphy", 0);
135 if (phy_np) {
136 data->phy_np = phy_np;
137 phy_pdev = of_find_device_by_node(phy_np);
138 if (phy_pdev) {
139 struct usb_phy *phy;
140 phy = pdev_to_phy(phy_pdev);
141 if (phy &&
142 try_module_get(phy_pdev->dev.driver->owner)) {
143 usb_phy_init(phy);
144 data->phy = phy;
145 }
146 }
147 }
148
149 /* we only support host now, so enable vbus here */
150 reg_vbus = devm_regulator_get(&pdev->dev, "vbus");
151 if (!IS_ERR(reg_vbus)) {
152 ret = regulator_enable(reg_vbus);
153 if (ret) {
154 dev_err(&pdev->dev,
155 "Failed to enable vbus regulator, err=%d\n",
156 ret);
157 goto put_np;
158 }
159 data->reg_vbus = reg_vbus;
160 } else {
161 reg_vbus = NULL;
162 }
163
164 ci13xxx_imx_platdata.phy = data->phy;
165
166 if (!pdev->dev.dma_mask) {
167 pdev->dev.dma_mask = devm_kzalloc(&pdev->dev,
168 sizeof(*pdev->dev.dma_mask), GFP_KERNEL);
169 if (!pdev->dev.dma_mask) {
170 ret = -ENOMEM;
171 dev_err(&pdev->dev, "Failed to alloc dma_mask!\n");
172 goto err;
173 }
174 *pdev->dev.dma_mask = DMA_BIT_MASK(32);
175 dma_set_coherent_mask(&pdev->dev, *pdev->dev.dma_mask);
176 }
Richard Zhaod142d6b2012-09-12 14:58:05 +0300177
178 if (usbmisc_ops && usbmisc_ops->init) {
179 ret = usbmisc_ops->init(&pdev->dev);
180 if (ret) {
181 dev_err(&pdev->dev,
182 "usbmisc init failed, ret=%d\n", ret);
183 goto err;
184 }
185 }
186
Richard Zhao15302802012-07-07 22:56:48 +0800187 plat_ci = ci13xxx_add_device(&pdev->dev,
188 pdev->resource, pdev->num_resources,
189 &ci13xxx_imx_platdata);
190 if (IS_ERR(plat_ci)) {
191 ret = PTR_ERR(plat_ci);
192 dev_err(&pdev->dev,
193 "Can't register ci_hdrc platform device, err=%d\n",
194 ret);
195 goto err;
196 }
197
198 data->ci_pdev = plat_ci;
199 platform_set_drvdata(pdev, data);
200
201 pm_runtime_no_callbacks(&pdev->dev);
202 pm_runtime_enable(&pdev->dev);
203
204 return 0;
205
206err:
207 if (reg_vbus)
208 regulator_disable(reg_vbus);
209put_np:
210 if (phy_np)
211 of_node_put(phy_np);
212 clk_disable_unprepare(data->clk);
213 return ret;
214}
215
216static int __devexit ci13xxx_imx_remove(struct platform_device *pdev)
217{
218 struct ci13xxx_imx_data *data = platform_get_drvdata(pdev);
219
220 pm_runtime_disable(&pdev->dev);
221 ci13xxx_remove_device(data->ci_pdev);
222
223 if (data->reg_vbus)
224 regulator_disable(data->reg_vbus);
225
226 if (data->phy) {
227 usb_phy_shutdown(data->phy);
228 module_put(data->phy->dev->driver->owner);
229 }
230
231 of_node_put(data->phy_np);
232
233 clk_disable_unprepare(data->clk);
234
235 platform_set_drvdata(pdev, NULL);
236
237 return 0;
238}
239
240static const struct of_device_id ci13xxx_imx_dt_ids[] = {
241 { .compatible = "fsl,imx27-usb", },
242 { /* sentinel */ }
243};
244MODULE_DEVICE_TABLE(of, ci13xxx_imx_dt_ids);
245
246static struct platform_driver ci13xxx_imx_driver = {
247 .probe = ci13xxx_imx_probe,
248 .remove = __devexit_p(ci13xxx_imx_remove),
249 .driver = {
250 .name = "imx_usb",
251 .owner = THIS_MODULE,
252 .of_match_table = ci13xxx_imx_dt_ids,
253 },
254};
255
256module_platform_driver(ci13xxx_imx_driver);
257
258MODULE_ALIAS("platform:imx-usb");
259MODULE_LICENSE("GPL v2");
260MODULE_DESCRIPTION("CI13xxx i.MX USB binding");
261MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
262MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");