blob: 14362c00db3f3e29bfdf24b3ff8bcd029e95b749 [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"
Alexander Shishkin8e229782013-06-24 14:46:36 +030025#include "ci_hdrc_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
Alexander Shishkin8e229782013-06-24 14:46:36 +030030struct ci_hdrc_imx_data {
Richard Zhao15302802012-07-07 22:56:48 +080031 struct usb_phy *phy;
32 struct platform_device *ci_pdev;
33 struct clk *clk;
34 struct regulator *reg_vbus;
35};
36
Richard Zhaod142d6b2012-09-12 14:58:05 +030037static const struct usbmisc_ops *usbmisc_ops;
38
39/* Common functions shared by usbmisc drivers */
40
41int usbmisc_set_ops(const struct usbmisc_ops *ops)
42{
43 if (usbmisc_ops)
44 return -EBUSY;
45
46 usbmisc_ops = ops;
47
48 return 0;
49}
50EXPORT_SYMBOL_GPL(usbmisc_set_ops);
51
52void usbmisc_unset_ops(const struct usbmisc_ops *ops)
53{
54 usbmisc_ops = NULL;
55}
56EXPORT_SYMBOL_GPL(usbmisc_unset_ops);
57
58int usbmisc_get_init_data(struct device *dev, struct usbmisc_usb_device *usbdev)
59{
60 struct device_node *np = dev->of_node;
61 struct of_phandle_args args;
62 int ret;
63
64 usbdev->dev = dev;
65
66 ret = of_parse_phandle_with_args(np, "fsl,usbmisc", "#index-cells",
67 0, &args);
68 if (ret) {
69 dev_err(dev, "Failed to parse property fsl,usbmisc, errno %d\n",
70 ret);
71 memset(usbdev, 0, sizeof(*usbdev));
72 return ret;
73 }
74 usbdev->index = args.args[0];
75 of_node_put(args.np);
76
77 if (of_find_property(np, "disable-over-current", NULL))
78 usbdev->disable_oc = 1;
79
Michael Grzeschika0685332013-03-30 12:54:01 +020080 if (of_find_property(np, "external-vbus-divider", NULL))
81 usbdev->evdo = 1;
82
Richard Zhaod142d6b2012-09-12 14:58:05 +030083 return 0;
84}
85EXPORT_SYMBOL_GPL(usbmisc_get_init_data);
86
87/* End of common functions shared by usbmisc drivers*/
88
Alexander Shishkin8e229782013-06-24 14:46:36 +030089static int ci_hdrc_imx_probe(struct platform_device *pdev)
Richard Zhao15302802012-07-07 22:56:48 +080090{
Alexander Shishkin8e229782013-06-24 14:46:36 +030091 struct ci_hdrc_imx_data *data;
92 struct ci_hdrc_platform_data pdata = {
93 .name = "ci_hdrc_imx",
Michael Grzeschikf6a3b3a2013-06-13 17:59:58 +030094 .capoffset = DEF_CAPOFFSET,
Alexander Shishkin8e229782013-06-24 14:46:36 +030095 .flags = CI_HDRC_REQUIRE_TRANSCEIVER |
96 CI_HDRC_PULLUP_ON_VBUS |
97 CI_HDRC_DISABLE_STREAMING,
Michael Grzeschikf6a3b3a2013-06-13 17:59:58 +030098 };
Richard Zhao15302802012-07-07 22:56:48 +080099 struct resource *res;
Richard Zhao15302802012-07-07 22:56:48 +0800100 int ret;
101
Richard Zhaod142d6b2012-09-12 14:58:05 +0300102 if (of_find_property(pdev->dev.of_node, "fsl,usbmisc", NULL)
103 && !usbmisc_ops)
104 return -EPROBE_DEFER;
105
Richard Zhao15302802012-07-07 22:56:48 +0800106 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
107 if (!data) {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300108 dev_err(&pdev->dev, "Failed to allocate ci_hdrc-imx data!\n");
Richard Zhao15302802012-07-07 22:56:48 +0800109 return -ENOMEM;
110 }
111
112 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
113 if (!res) {
114 dev_err(&pdev->dev, "Can't get device resources!\n");
115 return -ENOENT;
116 }
117
118 data->clk = devm_clk_get(&pdev->dev, NULL);
119 if (IS_ERR(data->clk)) {
120 dev_err(&pdev->dev,
121 "Failed to get clock, err=%ld\n", PTR_ERR(data->clk));
122 return PTR_ERR(data->clk);
123 }
124
125 ret = clk_prepare_enable(data->clk);
126 if (ret) {
127 dev_err(&pdev->dev,
128 "Failed to prepare or enable clock, err=%d\n", ret);
129 return ret;
130 }
131
Fabio Estevam046916d2013-06-25 12:58:05 +0300132 data->phy = devm_usb_get_phy_by_phandle(&pdev->dev, "fsl,usbphy", 0);
133 if (!IS_ERR(data->phy)) {
134 ret = usb_phy_init(data->phy);
Sascha Hauerea1418b2013-06-13 18:00:00 +0300135 if (ret) {
136 dev_err(&pdev->dev, "unable to init phy: %d\n", ret);
137 goto err_clk;
Richard Zhao15302802012-07-07 22:56:48 +0800138 }
Fabio Estevam046916d2013-06-25 12:58:05 +0300139 } else if (PTR_ERR(data->phy) == -EPROBE_DEFER) {
Sascha Hauerea1418b2013-06-13 18:00:00 +0300140 ret = -EPROBE_DEFER;
141 goto err_clk;
Richard Zhao15302802012-07-07 22:56:48 +0800142 }
143
144 /* we only support host now, so enable vbus here */
Fabio Estevame56ae542013-06-13 17:59:49 +0300145 data->reg_vbus = devm_regulator_get(&pdev->dev, "vbus");
146 if (!IS_ERR(data->reg_vbus)) {
147 ret = regulator_enable(data->reg_vbus);
Richard Zhao15302802012-07-07 22:56:48 +0800148 if (ret) {
149 dev_err(&pdev->dev,
150 "Failed to enable vbus regulator, err=%d\n",
151 ret);
Sascha Hauerea1418b2013-06-13 18:00:00 +0300152 goto err_clk;
Richard Zhao15302802012-07-07 22:56:48 +0800153 }
Richard Zhao15302802012-07-07 22:56:48 +0800154 } else {
Fabio Estevame56ae542013-06-13 17:59:49 +0300155 data->reg_vbus = NULL;
Richard Zhao15302802012-07-07 22:56:48 +0800156 }
157
Michael Grzeschikf6a3b3a2013-06-13 17:59:58 +0300158 pdata.phy = data->phy;
Richard Zhao15302802012-07-07 22:56:48 +0800159
Stephen Warren3b9561e2013-05-07 16:53:52 -0600160 if (!pdev->dev.dma_mask)
161 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
162 if (!pdev->dev.coherent_dma_mask)
163 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300164
165 if (usbmisc_ops && usbmisc_ops->init) {
166 ret = usbmisc_ops->init(&pdev->dev);
167 if (ret) {
168 dev_err(&pdev->dev,
169 "usbmisc init failed, ret=%d\n", ret);
170 goto err;
171 }
172 }
173
Alexander Shishkin8e229782013-06-24 14:46:36 +0300174 data->ci_pdev = ci_hdrc_add_device(&pdev->dev,
Richard Zhao15302802012-07-07 22:56:48 +0800175 pdev->resource, pdev->num_resources,
Michael Grzeschikf6a3b3a2013-06-13 17:59:58 +0300176 &pdata);
Fabio Estevam770719d2013-06-13 17:59:48 +0300177 if (IS_ERR(data->ci_pdev)) {
178 ret = PTR_ERR(data->ci_pdev);
Richard Zhao15302802012-07-07 22:56:48 +0800179 dev_err(&pdev->dev,
180 "Can't register ci_hdrc platform device, err=%d\n",
181 ret);
182 goto err;
183 }
184
Michael Grzeschika0685332013-03-30 12:54:01 +0200185 if (usbmisc_ops && usbmisc_ops->post) {
186 ret = usbmisc_ops->post(&pdev->dev);
187 if (ret) {
188 dev_err(&pdev->dev,
189 "usbmisc post failed, ret=%d\n", ret);
Fabio Estevam770719d2013-06-13 17:59:48 +0300190 goto disable_device;
Michael Grzeschika0685332013-03-30 12:54:01 +0200191 }
192 }
193
Richard Zhao15302802012-07-07 22:56:48 +0800194 platform_set_drvdata(pdev, data);
195
196 pm_runtime_no_callbacks(&pdev->dev);
197 pm_runtime_enable(&pdev->dev);
198
199 return 0;
200
Fabio Estevam770719d2013-06-13 17:59:48 +0300201disable_device:
Alexander Shishkin8e229782013-06-24 14:46:36 +0300202 ci_hdrc_remove_device(data->ci_pdev);
Richard Zhao15302802012-07-07 22:56:48 +0800203err:
Fabio Estevame56ae542013-06-13 17:59:49 +0300204 if (data->reg_vbus)
205 regulator_disable(data->reg_vbus);
Sascha Hauerea1418b2013-06-13 18:00:00 +0300206err_clk:
Richard Zhao15302802012-07-07 22:56:48 +0800207 clk_disable_unprepare(data->clk);
208 return ret;
209}
210
Alexander Shishkin8e229782013-06-24 14:46:36 +0300211static int ci_hdrc_imx_remove(struct platform_device *pdev)
Richard Zhao15302802012-07-07 22:56:48 +0800212{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300213 struct ci_hdrc_imx_data *data = platform_get_drvdata(pdev);
Richard Zhao15302802012-07-07 22:56:48 +0800214
215 pm_runtime_disable(&pdev->dev);
Alexander Shishkin8e229782013-06-24 14:46:36 +0300216 ci_hdrc_remove_device(data->ci_pdev);
Richard Zhao15302802012-07-07 22:56:48 +0800217
218 if (data->reg_vbus)
219 regulator_disable(data->reg_vbus);
220
221 if (data->phy) {
222 usb_phy_shutdown(data->phy);
223 module_put(data->phy->dev->driver->owner);
224 }
225
Richard Zhao15302802012-07-07 22:56:48 +0800226 clk_disable_unprepare(data->clk);
227
Richard Zhao15302802012-07-07 22:56:48 +0800228 return 0;
229}
230
Alexander Shishkin8e229782013-06-24 14:46:36 +0300231static const struct of_device_id ci_hdrc_imx_dt_ids[] = {
Richard Zhao15302802012-07-07 22:56:48 +0800232 { .compatible = "fsl,imx27-usb", },
233 { /* sentinel */ }
234};
Alexander Shishkin8e229782013-06-24 14:46:36 +0300235MODULE_DEVICE_TABLE(of, ci_hdrc_imx_dt_ids);
Richard Zhao15302802012-07-07 22:56:48 +0800236
Alexander Shishkin8e229782013-06-24 14:46:36 +0300237static struct platform_driver ci_hdrc_imx_driver = {
238 .probe = ci_hdrc_imx_probe,
239 .remove = ci_hdrc_imx_remove,
Richard Zhao15302802012-07-07 22:56:48 +0800240 .driver = {
241 .name = "imx_usb",
242 .owner = THIS_MODULE,
Alexander Shishkin8e229782013-06-24 14:46:36 +0300243 .of_match_table = ci_hdrc_imx_dt_ids,
Richard Zhao15302802012-07-07 22:56:48 +0800244 },
245};
246
Alexander Shishkin8e229782013-06-24 14:46:36 +0300247module_platform_driver(ci_hdrc_imx_driver);
Richard Zhao15302802012-07-07 22:56:48 +0800248
249MODULE_ALIAS("platform:imx-usb");
250MODULE_LICENSE("GPL v2");
Alexander Shishkin8e229782013-06-24 14:46:36 +0300251MODULE_DESCRIPTION("CI HDRC i.MX USB binding");
Richard Zhao15302802012-07-07 22:56:48 +0800252MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
253MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");