blob: 9cecfd517b7820dd0d1715b7deb5ca433d0be4d7 [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 resource *res;
Richard Zhao15302802012-07-07 22:56:48 +0800103 int ret;
104
Richard Zhaod142d6b2012-09-12 14:58:05 +0300105 if (of_find_property(pdev->dev.of_node, "fsl,usbmisc", NULL)
106 && !usbmisc_ops)
107 return -EPROBE_DEFER;
108
Richard Zhao15302802012-07-07 22:56:48 +0800109 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
110 if (!data) {
111 dev_err(&pdev->dev, "Failed to allocate CI13xxx-IMX data!\n");
112 return -ENOMEM;
113 }
114
115 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
116 if (!res) {
117 dev_err(&pdev->dev, "Can't get device resources!\n");
118 return -ENOENT;
119 }
120
121 data->clk = devm_clk_get(&pdev->dev, NULL);
122 if (IS_ERR(data->clk)) {
123 dev_err(&pdev->dev,
124 "Failed to get clock, err=%ld\n", PTR_ERR(data->clk));
125 return PTR_ERR(data->clk);
126 }
127
128 ret = clk_prepare_enable(data->clk);
129 if (ret) {
130 dev_err(&pdev->dev,
131 "Failed to prepare or enable clock, err=%d\n", ret);
132 return ret;
133 }
134
Fabio Estevam05f63532013-06-13 17:59:51 +0300135 data->phy_np = of_parse_phandle(pdev->dev.of_node, "fsl,usbphy", 0);
136 if (data->phy_np) {
137 phy_pdev = of_find_device_by_node(data->phy_np);
Richard Zhao15302802012-07-07 22:56:48 +0800138 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 */
Fabio Estevame56ae542013-06-13 17:59:49 +0300150 data->reg_vbus = devm_regulator_get(&pdev->dev, "vbus");
151 if (!IS_ERR(data->reg_vbus)) {
152 ret = regulator_enable(data->reg_vbus);
Richard Zhao15302802012-07-07 22:56:48 +0800153 if (ret) {
154 dev_err(&pdev->dev,
155 "Failed to enable vbus regulator, err=%d\n",
156 ret);
157 goto put_np;
158 }
Richard Zhao15302802012-07-07 22:56:48 +0800159 } else {
Fabio Estevame56ae542013-06-13 17:59:49 +0300160 data->reg_vbus = NULL;
Richard Zhao15302802012-07-07 22:56:48 +0800161 }
162
163 ci13xxx_imx_platdata.phy = data->phy;
164
Stephen Warren3b9561e2013-05-07 16:53:52 -0600165 if (!pdev->dev.dma_mask)
166 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
167 if (!pdev->dev.coherent_dma_mask)
168 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300169
170 if (usbmisc_ops && usbmisc_ops->init) {
171 ret = usbmisc_ops->init(&pdev->dev);
172 if (ret) {
173 dev_err(&pdev->dev,
174 "usbmisc init failed, ret=%d\n", ret);
175 goto err;
176 }
177 }
178
Fabio Estevam770719d2013-06-13 17:59:48 +0300179 data->ci_pdev = ci13xxx_add_device(&pdev->dev,
Richard Zhao15302802012-07-07 22:56:48 +0800180 pdev->resource, pdev->num_resources,
181 &ci13xxx_imx_platdata);
Fabio Estevam770719d2013-06-13 17:59:48 +0300182 if (IS_ERR(data->ci_pdev)) {
183 ret = PTR_ERR(data->ci_pdev);
Richard Zhao15302802012-07-07 22:56:48 +0800184 dev_err(&pdev->dev,
185 "Can't register ci_hdrc platform device, err=%d\n",
186 ret);
187 goto err;
188 }
189
Michael Grzeschika0685332013-03-30 12:54:01 +0200190 if (usbmisc_ops && usbmisc_ops->post) {
191 ret = usbmisc_ops->post(&pdev->dev);
192 if (ret) {
193 dev_err(&pdev->dev,
194 "usbmisc post failed, ret=%d\n", ret);
Fabio Estevam770719d2013-06-13 17:59:48 +0300195 goto disable_device;
Michael Grzeschika0685332013-03-30 12:54:01 +0200196 }
197 }
198
Richard Zhao15302802012-07-07 22:56:48 +0800199 platform_set_drvdata(pdev, data);
200
201 pm_runtime_no_callbacks(&pdev->dev);
202 pm_runtime_enable(&pdev->dev);
203
204 return 0;
205
Fabio Estevam770719d2013-06-13 17:59:48 +0300206disable_device:
207 ci13xxx_remove_device(data->ci_pdev);
Richard Zhao15302802012-07-07 22:56:48 +0800208err:
Fabio Estevame56ae542013-06-13 17:59:49 +0300209 if (data->reg_vbus)
210 regulator_disable(data->reg_vbus);
Richard Zhao15302802012-07-07 22:56:48 +0800211put_np:
Fabio Estevam05f63532013-06-13 17:59:51 +0300212 if (data->phy_np)
213 of_node_put(data->phy_np);
Richard Zhao15302802012-07-07 22:56:48 +0800214 clk_disable_unprepare(data->clk);
215 return ret;
216}
217
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500218static int ci13xxx_imx_remove(struct platform_device *pdev)
Richard Zhao15302802012-07-07 22:56:48 +0800219{
220 struct ci13xxx_imx_data *data = platform_get_drvdata(pdev);
221
222 pm_runtime_disable(&pdev->dev);
223 ci13xxx_remove_device(data->ci_pdev);
224
225 if (data->reg_vbus)
226 regulator_disable(data->reg_vbus);
227
228 if (data->phy) {
229 usb_phy_shutdown(data->phy);
230 module_put(data->phy->dev->driver->owner);
231 }
232
Fabio Estevam785dabe2013-06-13 17:59:50 +0300233 if (data->phy_np)
234 of_node_put(data->phy_np);
Richard Zhao15302802012-07-07 22:56:48 +0800235
236 clk_disable_unprepare(data->clk);
237
Richard Zhao15302802012-07-07 22:56:48 +0800238 return 0;
239}
240
241static const struct of_device_id ci13xxx_imx_dt_ids[] = {
242 { .compatible = "fsl,imx27-usb", },
243 { /* sentinel */ }
244};
245MODULE_DEVICE_TABLE(of, ci13xxx_imx_dt_ids);
246
247static struct platform_driver ci13xxx_imx_driver = {
248 .probe = ci13xxx_imx_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500249 .remove = ci13xxx_imx_remove,
Richard Zhao15302802012-07-07 22:56:48 +0800250 .driver = {
251 .name = "imx_usb",
252 .owner = THIS_MODULE,
253 .of_match_table = ci13xxx_imx_dt_ids,
254 },
255};
256
257module_platform_driver(ci13xxx_imx_driver);
258
259MODULE_ALIAS("platform:imx-usb");
260MODULE_LICENSE("GPL v2");
261MODULE_DESCRIPTION("CI13xxx i.MX USB binding");
262MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
263MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");