blob: cc53e63c9ce9390b4f86e018b40bf8d7ae1fcedd [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>
Richard Zhao15302802012-07-07 22:56:48 +080022
23#include "ci.h"
Alexander Shishkin8e229782013-06-24 14:46:36 +030024#include "ci_hdrc_imx.h"
Richard Zhao15302802012-07-07 22:56:48 +080025
Alexander Shishkin8e229782013-06-24 14:46:36 +030026struct ci_hdrc_imx_data {
Richard Zhao15302802012-07-07 22:56:48 +080027 struct usb_phy *phy;
28 struct platform_device *ci_pdev;
29 struct clk *clk;
Richard Zhao15302802012-07-07 22:56:48 +080030};
31
Richard Zhaod142d6b2012-09-12 14:58:05 +030032static const struct usbmisc_ops *usbmisc_ops;
33
34/* Common functions shared by usbmisc drivers */
35
36int usbmisc_set_ops(const struct usbmisc_ops *ops)
37{
38 if (usbmisc_ops)
39 return -EBUSY;
40
41 usbmisc_ops = ops;
42
43 return 0;
44}
45EXPORT_SYMBOL_GPL(usbmisc_set_ops);
46
47void usbmisc_unset_ops(const struct usbmisc_ops *ops)
48{
49 usbmisc_ops = NULL;
50}
51EXPORT_SYMBOL_GPL(usbmisc_unset_ops);
52
53int usbmisc_get_init_data(struct device *dev, struct usbmisc_usb_device *usbdev)
54{
55 struct device_node *np = dev->of_node;
56 struct of_phandle_args args;
57 int ret;
58
59 usbdev->dev = dev;
60
61 ret = of_parse_phandle_with_args(np, "fsl,usbmisc", "#index-cells",
62 0, &args);
63 if (ret) {
64 dev_err(dev, "Failed to parse property fsl,usbmisc, errno %d\n",
65 ret);
66 memset(usbdev, 0, sizeof(*usbdev));
67 return ret;
68 }
69 usbdev->index = args.args[0];
70 of_node_put(args.np);
71
72 if (of_find_property(np, "disable-over-current", NULL))
73 usbdev->disable_oc = 1;
74
Michael Grzeschika0685332013-03-30 12:54:01 +020075 if (of_find_property(np, "external-vbus-divider", NULL))
76 usbdev->evdo = 1;
77
Richard Zhaod142d6b2012-09-12 14:58:05 +030078 return 0;
79}
80EXPORT_SYMBOL_GPL(usbmisc_get_init_data);
81
82/* End of common functions shared by usbmisc drivers*/
83
Alexander Shishkin8e229782013-06-24 14:46:36 +030084static int ci_hdrc_imx_probe(struct platform_device *pdev)
Richard Zhao15302802012-07-07 22:56:48 +080085{
Alexander Shishkin8e229782013-06-24 14:46:36 +030086 struct ci_hdrc_imx_data *data;
87 struct ci_hdrc_platform_data pdata = {
88 .name = "ci_hdrc_imx",
Michael Grzeschikf6a3b3a2013-06-13 17:59:58 +030089 .capoffset = DEF_CAPOFFSET,
Alexander Shishkin8e229782013-06-24 14:46:36 +030090 .flags = CI_HDRC_REQUIRE_TRANSCEIVER |
Alexander Shishkin8e229782013-06-24 14:46:36 +030091 CI_HDRC_DISABLE_STREAMING,
Michael Grzeschikf6a3b3a2013-06-13 17:59:58 +030092 };
Richard Zhao15302802012-07-07 22:56:48 +080093 int ret;
94
Richard Zhaod142d6b2012-09-12 14:58:05 +030095 if (of_find_property(pdev->dev.of_node, "fsl,usbmisc", NULL)
96 && !usbmisc_ops)
97 return -EPROBE_DEFER;
98
Richard Zhao15302802012-07-07 22:56:48 +080099 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
100 if (!data) {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300101 dev_err(&pdev->dev, "Failed to allocate ci_hdrc-imx data!\n");
Richard Zhao15302802012-07-07 22:56:48 +0800102 return -ENOMEM;
103 }
104
Richard Zhao15302802012-07-07 22:56:48 +0800105 data->clk = devm_clk_get(&pdev->dev, NULL);
106 if (IS_ERR(data->clk)) {
107 dev_err(&pdev->dev,
108 "Failed to get clock, err=%ld\n", PTR_ERR(data->clk));
109 return PTR_ERR(data->clk);
110 }
111
112 ret = clk_prepare_enable(data->clk);
113 if (ret) {
114 dev_err(&pdev->dev,
115 "Failed to prepare or enable clock, err=%d\n", ret);
116 return ret;
117 }
118
Fabio Estevam046916d2013-06-25 12:58:05 +0300119 data->phy = devm_usb_get_phy_by_phandle(&pdev->dev, "fsl,usbphy", 0);
120 if (!IS_ERR(data->phy)) {
121 ret = usb_phy_init(data->phy);
Sascha Hauerea1418b2013-06-13 18:00:00 +0300122 if (ret) {
123 dev_err(&pdev->dev, "unable to init phy: %d\n", ret);
124 goto err_clk;
Richard Zhao15302802012-07-07 22:56:48 +0800125 }
Fabio Estevam046916d2013-06-25 12:58:05 +0300126 } else if (PTR_ERR(data->phy) == -EPROBE_DEFER) {
Sascha Hauerea1418b2013-06-13 18:00:00 +0300127 ret = -EPROBE_DEFER;
128 goto err_clk;
Richard Zhao15302802012-07-07 22:56:48 +0800129 }
130
Michael Grzeschikf6a3b3a2013-06-13 17:59:58 +0300131 pdata.phy = data->phy;
Richard Zhao15302802012-07-07 22:56:48 +0800132
Stephen Warren3b9561e2013-05-07 16:53:52 -0600133 if (!pdev->dev.dma_mask)
134 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
135 if (!pdev->dev.coherent_dma_mask)
136 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300137
138 if (usbmisc_ops && usbmisc_ops->init) {
139 ret = usbmisc_ops->init(&pdev->dev);
140 if (ret) {
141 dev_err(&pdev->dev,
142 "usbmisc init failed, ret=%d\n", ret);
Peter Chen1542d9c2013-08-14 12:44:03 +0300143 goto err_clk;
Richard Zhaod142d6b2012-09-12 14:58:05 +0300144 }
145 }
146
Alexander Shishkin8e229782013-06-24 14:46:36 +0300147 data->ci_pdev = ci_hdrc_add_device(&pdev->dev,
Richard Zhao15302802012-07-07 22:56:48 +0800148 pdev->resource, pdev->num_resources,
Michael Grzeschikf6a3b3a2013-06-13 17:59:58 +0300149 &pdata);
Fabio Estevam770719d2013-06-13 17:59:48 +0300150 if (IS_ERR(data->ci_pdev)) {
151 ret = PTR_ERR(data->ci_pdev);
Richard Zhao15302802012-07-07 22:56:48 +0800152 dev_err(&pdev->dev,
153 "Can't register ci_hdrc platform device, err=%d\n",
154 ret);
Peter Chen1542d9c2013-08-14 12:44:03 +0300155 goto err_clk;
Richard Zhao15302802012-07-07 22:56:48 +0800156 }
157
Michael Grzeschika0685332013-03-30 12:54:01 +0200158 if (usbmisc_ops && usbmisc_ops->post) {
159 ret = usbmisc_ops->post(&pdev->dev);
160 if (ret) {
161 dev_err(&pdev->dev,
162 "usbmisc post failed, ret=%d\n", ret);
Fabio Estevam770719d2013-06-13 17:59:48 +0300163 goto disable_device;
Michael Grzeschika0685332013-03-30 12:54:01 +0200164 }
165 }
166
Richard Zhao15302802012-07-07 22:56:48 +0800167 platform_set_drvdata(pdev, data);
168
169 pm_runtime_no_callbacks(&pdev->dev);
170 pm_runtime_enable(&pdev->dev);
171
172 return 0;
173
Fabio Estevam770719d2013-06-13 17:59:48 +0300174disable_device:
Alexander Shishkin8e229782013-06-24 14:46:36 +0300175 ci_hdrc_remove_device(data->ci_pdev);
Sascha Hauerea1418b2013-06-13 18:00:00 +0300176err_clk:
Richard Zhao15302802012-07-07 22:56:48 +0800177 clk_disable_unprepare(data->clk);
178 return ret;
179}
180
Alexander Shishkin8e229782013-06-24 14:46:36 +0300181static int ci_hdrc_imx_remove(struct platform_device *pdev)
Richard Zhao15302802012-07-07 22:56:48 +0800182{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300183 struct ci_hdrc_imx_data *data = platform_get_drvdata(pdev);
Richard Zhao15302802012-07-07 22:56:48 +0800184
185 pm_runtime_disable(&pdev->dev);
Alexander Shishkin8e229782013-06-24 14:46:36 +0300186 ci_hdrc_remove_device(data->ci_pdev);
Richard Zhao15302802012-07-07 22:56:48 +0800187
Lothar Waßmann769d92c2013-08-14 12:43:59 +0300188 if (data->phy)
Richard Zhao15302802012-07-07 22:56:48 +0800189 usb_phy_shutdown(data->phy);
Richard Zhao15302802012-07-07 22:56:48 +0800190
Richard Zhao15302802012-07-07 22:56:48 +0800191 clk_disable_unprepare(data->clk);
192
Richard Zhao15302802012-07-07 22:56:48 +0800193 return 0;
194}
195
Alexander Shishkin8e229782013-06-24 14:46:36 +0300196static const struct of_device_id ci_hdrc_imx_dt_ids[] = {
Richard Zhao15302802012-07-07 22:56:48 +0800197 { .compatible = "fsl,imx27-usb", },
198 { /* sentinel */ }
199};
Alexander Shishkin8e229782013-06-24 14:46:36 +0300200MODULE_DEVICE_TABLE(of, ci_hdrc_imx_dt_ids);
Richard Zhao15302802012-07-07 22:56:48 +0800201
Alexander Shishkin8e229782013-06-24 14:46:36 +0300202static struct platform_driver ci_hdrc_imx_driver = {
203 .probe = ci_hdrc_imx_probe,
204 .remove = ci_hdrc_imx_remove,
Richard Zhao15302802012-07-07 22:56:48 +0800205 .driver = {
206 .name = "imx_usb",
207 .owner = THIS_MODULE,
Alexander Shishkin8e229782013-06-24 14:46:36 +0300208 .of_match_table = ci_hdrc_imx_dt_ids,
Richard Zhao15302802012-07-07 22:56:48 +0800209 },
210};
211
Alexander Shishkin8e229782013-06-24 14:46:36 +0300212module_platform_driver(ci_hdrc_imx_driver);
Richard Zhao15302802012-07-07 22:56:48 +0800213
214MODULE_ALIAS("platform:imx-usb");
215MODULE_LICENSE("GPL v2");
Alexander Shishkin8e229782013-06-24 14:46:36 +0300216MODULE_DESCRIPTION("CI HDRC i.MX USB binding");
Richard Zhao15302802012-07-07 22:56:48 +0800217MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
218MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");