blob: 24f46e1f043adac56d4553f536dc128cfcbf30c6 [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;
104 struct regulator *reg_vbus;
105 int ret;
106
Richard Zhaod142d6b2012-09-12 14:58:05 +0300107 if (of_find_property(pdev->dev.of_node, "fsl,usbmisc", NULL)
108 && !usbmisc_ops)
109 return -EPROBE_DEFER;
110
Richard Zhao15302802012-07-07 22:56:48 +0800111 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
112 if (!data) {
113 dev_err(&pdev->dev, "Failed to allocate CI13xxx-IMX data!\n");
114 return -ENOMEM;
115 }
116
117 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
118 if (!res) {
119 dev_err(&pdev->dev, "Can't get device resources!\n");
120 return -ENOENT;
121 }
122
123 data->clk = devm_clk_get(&pdev->dev, NULL);
124 if (IS_ERR(data->clk)) {
125 dev_err(&pdev->dev,
126 "Failed to get clock, err=%ld\n", PTR_ERR(data->clk));
127 return PTR_ERR(data->clk);
128 }
129
130 ret = clk_prepare_enable(data->clk);
131 if (ret) {
132 dev_err(&pdev->dev,
133 "Failed to prepare or enable clock, err=%d\n", ret);
134 return ret;
135 }
136
137 phy_np = of_parse_phandle(pdev->dev.of_node, "fsl,usbphy", 0);
138 if (phy_np) {
139 data->phy_np = phy_np;
140 phy_pdev = of_find_device_by_node(phy_np);
141 if (phy_pdev) {
142 struct usb_phy *phy;
143 phy = pdev_to_phy(phy_pdev);
144 if (phy &&
145 try_module_get(phy_pdev->dev.driver->owner)) {
146 usb_phy_init(phy);
147 data->phy = phy;
148 }
149 }
150 }
151
152 /* we only support host now, so enable vbus here */
153 reg_vbus = devm_regulator_get(&pdev->dev, "vbus");
154 if (!IS_ERR(reg_vbus)) {
155 ret = regulator_enable(reg_vbus);
156 if (ret) {
157 dev_err(&pdev->dev,
158 "Failed to enable vbus regulator, err=%d\n",
159 ret);
160 goto put_np;
161 }
162 data->reg_vbus = reg_vbus;
163 } else {
164 reg_vbus = NULL;
165 }
166
167 ci13xxx_imx_platdata.phy = data->phy;
168
Stephen Warren3b9561e2013-05-07 16:53:52 -0600169 if (!pdev->dev.dma_mask)
170 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
171 if (!pdev->dev.coherent_dma_mask)
172 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300173
174 if (usbmisc_ops && usbmisc_ops->init) {
175 ret = usbmisc_ops->init(&pdev->dev);
176 if (ret) {
177 dev_err(&pdev->dev,
178 "usbmisc init failed, ret=%d\n", ret);
179 goto err;
180 }
181 }
182
Fabio Estevam770719d2013-06-13 17:59:48 +0300183 data->ci_pdev = ci13xxx_add_device(&pdev->dev,
Richard Zhao15302802012-07-07 22:56:48 +0800184 pdev->resource, pdev->num_resources,
185 &ci13xxx_imx_platdata);
Fabio Estevam770719d2013-06-13 17:59:48 +0300186 if (IS_ERR(data->ci_pdev)) {
187 ret = PTR_ERR(data->ci_pdev);
Richard Zhao15302802012-07-07 22:56:48 +0800188 dev_err(&pdev->dev,
189 "Can't register ci_hdrc platform device, err=%d\n",
190 ret);
191 goto err;
192 }
193
Michael Grzeschika0685332013-03-30 12:54:01 +0200194 if (usbmisc_ops && usbmisc_ops->post) {
195 ret = usbmisc_ops->post(&pdev->dev);
196 if (ret) {
197 dev_err(&pdev->dev,
198 "usbmisc post failed, ret=%d\n", ret);
Fabio Estevam770719d2013-06-13 17:59:48 +0300199 goto disable_device;
Michael Grzeschika0685332013-03-30 12:54:01 +0200200 }
201 }
202
Richard Zhao15302802012-07-07 22:56:48 +0800203 platform_set_drvdata(pdev, data);
204
205 pm_runtime_no_callbacks(&pdev->dev);
206 pm_runtime_enable(&pdev->dev);
207
208 return 0;
209
Fabio Estevam770719d2013-06-13 17:59:48 +0300210disable_device:
211 ci13xxx_remove_device(data->ci_pdev);
Richard Zhao15302802012-07-07 22:56:48 +0800212err:
213 if (reg_vbus)
214 regulator_disable(reg_vbus);
215put_np:
216 if (phy_np)
217 of_node_put(phy_np);
218 clk_disable_unprepare(data->clk);
219 return ret;
220}
221
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500222static int ci13xxx_imx_remove(struct platform_device *pdev)
Richard Zhao15302802012-07-07 22:56:48 +0800223{
224 struct ci13xxx_imx_data *data = platform_get_drvdata(pdev);
225
226 pm_runtime_disable(&pdev->dev);
227 ci13xxx_remove_device(data->ci_pdev);
228
229 if (data->reg_vbus)
230 regulator_disable(data->reg_vbus);
231
232 if (data->phy) {
233 usb_phy_shutdown(data->phy);
234 module_put(data->phy->dev->driver->owner);
235 }
236
237 of_node_put(data->phy_np);
238
239 clk_disable_unprepare(data->clk);
240
Richard Zhao15302802012-07-07 22:56:48 +0800241 return 0;
242}
243
244static const struct of_device_id ci13xxx_imx_dt_ids[] = {
245 { .compatible = "fsl,imx27-usb", },
246 { /* sentinel */ }
247};
248MODULE_DEVICE_TABLE(of, ci13xxx_imx_dt_ids);
249
250static struct platform_driver ci13xxx_imx_driver = {
251 .probe = ci13xxx_imx_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500252 .remove = ci13xxx_imx_remove,
Richard Zhao15302802012-07-07 22:56:48 +0800253 .driver = {
254 .name = "imx_usb",
255 .owner = THIS_MODULE,
256 .of_match_table = ci13xxx_imx_dt_ids,
257 },
258};
259
260module_platform_driver(ci13xxx_imx_driver);
261
262MODULE_ALIAS("platform:imx-usb");
263MODULE_LICENSE("GPL v2");
264MODULE_DESCRIPTION("CI13xxx i.MX USB binding");
265MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
266MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");