blob: 421651060f8dfcb4dbb16b73929fc05de5643fff [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
Peter Chen10710552014-01-10 13:51:28 +080026struct ci_hdrc_imx_platform_flag {
27 unsigned int flags;
28};
29
30static const struct ci_hdrc_imx_platform_flag imx27_usb_data = {
31};
32
33static const struct ci_hdrc_imx_platform_flag imx28_usb_data = {
Peter Chen56040082015-02-11 12:44:42 +080034 .flags = CI_HDRC_IMX28_WRITE_FIX,
Peter Chen10710552014-01-10 13:51:28 +080035};
36
37static const struct of_device_id ci_hdrc_imx_dt_ids[] = {
38 { .compatible = "fsl,imx28-usb", .data = &imx28_usb_data},
39 { .compatible = "fsl,imx27-usb", .data = &imx27_usb_data},
40 { /* sentinel */ }
41};
42MODULE_DEVICE_TABLE(of, ci_hdrc_imx_dt_ids);
43
Alexander Shishkin8e229782013-06-24 14:46:36 +030044struct ci_hdrc_imx_data {
Richard Zhao15302802012-07-07 22:56:48 +080045 struct usb_phy *phy;
46 struct platform_device *ci_pdev;
47 struct clk *clk;
Sascha Hauer05986ba2013-08-14 12:44:16 +030048 struct imx_usbmisc_data *usbmisc_data;
Richard Zhao15302802012-07-07 22:56:48 +080049};
50
Richard Zhaod142d6b2012-09-12 14:58:05 +030051/* Common functions shared by usbmisc drivers */
52
Sascha Hauer05986ba2013-08-14 12:44:16 +030053static struct imx_usbmisc_data *usbmisc_get_init_data(struct device *dev)
Richard Zhaod142d6b2012-09-12 14:58:05 +030054{
Stefan Agnerf40017e2014-09-22 08:14:15 +080055 struct platform_device *misc_pdev;
Richard Zhaod142d6b2012-09-12 14:58:05 +030056 struct device_node *np = dev->of_node;
57 struct of_phandle_args args;
Sascha Hauer05986ba2013-08-14 12:44:16 +030058 struct imx_usbmisc_data *data;
Richard Zhaod142d6b2012-09-12 14:58:05 +030059 int ret;
60
Sascha Hauer05986ba2013-08-14 12:44:16 +030061 /*
62 * In case the fsl,usbmisc property is not present this device doesn't
63 * need usbmisc. Return NULL (which is no error here)
64 */
65 if (!of_get_property(np, "fsl,usbmisc", NULL))
66 return NULL;
67
68 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
69 if (!data)
70 return ERR_PTR(-ENOMEM);
Richard Zhaod142d6b2012-09-12 14:58:05 +030071
72 ret = of_parse_phandle_with_args(np, "fsl,usbmisc", "#index-cells",
73 0, &args);
74 if (ret) {
75 dev_err(dev, "Failed to parse property fsl,usbmisc, errno %d\n",
76 ret);
Sascha Hauer05986ba2013-08-14 12:44:16 +030077 return ERR_PTR(ret);
Richard Zhaod142d6b2012-09-12 14:58:05 +030078 }
Sascha Hauer05986ba2013-08-14 12:44:16 +030079
80 data->index = args.args[0];
Stefan Agnerf40017e2014-09-22 08:14:15 +080081
82 misc_pdev = of_find_device_by_node(args.np);
Richard Zhaod142d6b2012-09-12 14:58:05 +030083 of_node_put(args.np);
84
Stefan Agnerf40017e2014-09-22 08:14:15 +080085 if (!misc_pdev)
86 return ERR_PTR(-EPROBE_DEFER);
87
88 data->dev = &misc_pdev->dev;
89
Richard Zhaod142d6b2012-09-12 14:58:05 +030090 if (of_find_property(np, "disable-over-current", NULL))
Sascha Hauer05986ba2013-08-14 12:44:16 +030091 data->disable_oc = 1;
Richard Zhaod142d6b2012-09-12 14:58:05 +030092
Michael Grzeschika0685332013-03-30 12:54:01 +020093 if (of_find_property(np, "external-vbus-divider", NULL))
Sascha Hauer05986ba2013-08-14 12:44:16 +030094 data->evdo = 1;
Michael Grzeschika0685332013-03-30 12:54:01 +020095
Sascha Hauer05986ba2013-08-14 12:44:16 +030096 return data;
Richard Zhaod142d6b2012-09-12 14:58:05 +030097}
Richard Zhaod142d6b2012-09-12 14:58:05 +030098
99/* End of common functions shared by usbmisc drivers*/
100
Alexander Shishkin8e229782013-06-24 14:46:36 +0300101static int ci_hdrc_imx_probe(struct platform_device *pdev)
Richard Zhao15302802012-07-07 22:56:48 +0800102{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300103 struct ci_hdrc_imx_data *data;
104 struct ci_hdrc_platform_data pdata = {
Alexander Shiyanc844d6c2014-03-11 13:47:38 +0800105 .name = dev_name(&pdev->dev),
Michael Grzeschikf6a3b3a2013-06-13 17:59:58 +0300106 .capoffset = DEF_CAPOFFSET,
Peter Chen947c8852014-11-26 13:44:31 +0800107 .flags = CI_HDRC_DISABLE_STREAMING,
Michael Grzeschikf6a3b3a2013-06-13 17:59:58 +0300108 };
Richard Zhao15302802012-07-07 22:56:48 +0800109 int ret;
Peter Chen10710552014-01-10 13:51:28 +0800110 const struct of_device_id *of_id =
111 of_match_device(ci_hdrc_imx_dt_ids, &pdev->dev);
112 const struct ci_hdrc_imx_platform_flag *imx_platform_flag = of_id->data;
Richard Zhao15302802012-07-07 22:56:48 +0800113
114 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
Fabio Estevam73529822014-11-26 13:44:24 +0800115 if (!data)
Richard Zhao15302802012-07-07 22:56:48 +0800116 return -ENOMEM;
Richard Zhao15302802012-07-07 22:56:48 +0800117
Sascha Hauer05986ba2013-08-14 12:44:16 +0300118 data->usbmisc_data = usbmisc_get_init_data(&pdev->dev);
119 if (IS_ERR(data->usbmisc_data))
120 return PTR_ERR(data->usbmisc_data);
121
Richard Zhao15302802012-07-07 22:56:48 +0800122 data->clk = devm_clk_get(&pdev->dev, NULL);
123 if (IS_ERR(data->clk)) {
124 dev_err(&pdev->dev,
125 "Failed to get clock, err=%ld\n", PTR_ERR(data->clk));
126 return PTR_ERR(data->clk);
127 }
128
129 ret = clk_prepare_enable(data->clk);
130 if (ret) {
131 dev_err(&pdev->dev,
132 "Failed to prepare or enable clock, err=%d\n", ret);
133 return ret;
134 }
135
Fabio Estevam046916d2013-06-25 12:58:05 +0300136 data->phy = devm_usb_get_phy_by_phandle(&pdev->dev, "fsl,usbphy", 0);
Peter Chenaf59a8b2013-09-24 12:47:54 +0800137 if (IS_ERR(data->phy)) {
138 ret = PTR_ERR(data->phy);
Markus Pargmann16853d72014-07-22 10:09:43 +0800139 /* Return -EINVAL if no usbphy is available */
140 if (ret == -ENODEV)
141 ret = -EINVAL;
Sascha Hauerea1418b2013-06-13 18:00:00 +0300142 goto err_clk;
Richard Zhao15302802012-07-07 22:56:48 +0800143 }
144
Antoine Tenartef44cb42014-10-30 18:41:16 +0100145 pdata.usb_phy = data->phy;
Peter Chen56040082015-02-11 12:44:42 +0800146 pdata.flags |= imx_platform_flag->flags;
Peter Chen10710552014-01-10 13:51:28 +0800147
Russell Kinge1fd7342013-06-27 12:36:37 +0100148 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
Russell King22d9d8e2013-06-10 16:28:49 +0100149 if (ret)
150 goto err_clk;
Richard Zhaod142d6b2012-09-12 14:58:05 +0300151
Sascha Hauer05986ba2013-08-14 12:44:16 +0300152 if (data->usbmisc_data) {
153 ret = imx_usbmisc_init(data->usbmisc_data);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300154 if (ret) {
Sascha Hauer05986ba2013-08-14 12:44:16 +0300155 dev_err(&pdev->dev, "usbmisc init failed, ret=%d\n",
156 ret);
Peter Chenaf59a8b2013-09-24 12:47:54 +0800157 goto err_clk;
Richard Zhaod142d6b2012-09-12 14:58:05 +0300158 }
159 }
160
Alexander Shishkin8e229782013-06-24 14:46:36 +0300161 data->ci_pdev = ci_hdrc_add_device(&pdev->dev,
Richard Zhao15302802012-07-07 22:56:48 +0800162 pdev->resource, pdev->num_resources,
Michael Grzeschikf6a3b3a2013-06-13 17:59:58 +0300163 &pdata);
Fabio Estevam770719d2013-06-13 17:59:48 +0300164 if (IS_ERR(data->ci_pdev)) {
165 ret = PTR_ERR(data->ci_pdev);
Richard Zhao15302802012-07-07 22:56:48 +0800166 dev_err(&pdev->dev,
167 "Can't register ci_hdrc platform device, err=%d\n",
168 ret);
Peter Chenaf59a8b2013-09-24 12:47:54 +0800169 goto err_clk;
Richard Zhao15302802012-07-07 22:56:48 +0800170 }
171
Sascha Hauer05986ba2013-08-14 12:44:16 +0300172 if (data->usbmisc_data) {
173 ret = imx_usbmisc_init_post(data->usbmisc_data);
Michael Grzeschika0685332013-03-30 12:54:01 +0200174 if (ret) {
Sascha Hauer05986ba2013-08-14 12:44:16 +0300175 dev_err(&pdev->dev, "usbmisc post failed, ret=%d\n",
176 ret);
Fabio Estevam770719d2013-06-13 17:59:48 +0300177 goto disable_device;
Michael Grzeschika0685332013-03-30 12:54:01 +0200178 }
179 }
180
Richard Zhao15302802012-07-07 22:56:48 +0800181 platform_set_drvdata(pdev, data);
182
183 pm_runtime_no_callbacks(&pdev->dev);
184 pm_runtime_enable(&pdev->dev);
185
186 return 0;
187
Fabio Estevam770719d2013-06-13 17:59:48 +0300188disable_device:
Alexander Shishkin8e229782013-06-24 14:46:36 +0300189 ci_hdrc_remove_device(data->ci_pdev);
Sascha Hauerea1418b2013-06-13 18:00:00 +0300190err_clk:
Richard Zhao15302802012-07-07 22:56:48 +0800191 clk_disable_unprepare(data->clk);
192 return ret;
193}
194
Alexander Shishkin8e229782013-06-24 14:46:36 +0300195static int ci_hdrc_imx_remove(struct platform_device *pdev)
Richard Zhao15302802012-07-07 22:56:48 +0800196{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300197 struct ci_hdrc_imx_data *data = platform_get_drvdata(pdev);
Richard Zhao15302802012-07-07 22:56:48 +0800198
199 pm_runtime_disable(&pdev->dev);
Alexander Shishkin8e229782013-06-24 14:46:36 +0300200 ci_hdrc_remove_device(data->ci_pdev);
Richard Zhao15302802012-07-07 22:56:48 +0800201 clk_disable_unprepare(data->clk);
202
Richard Zhao15302802012-07-07 22:56:48 +0800203 return 0;
204}
205
Peter Chen2558c1f2014-11-26 13:44:30 +0800206#ifdef CONFIG_PM_SLEEP
207static int imx_controller_suspend(struct device *dev)
208{
209 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
210
211 dev_dbg(dev, "at %s\n", __func__);
212
213 clk_disable_unprepare(data->clk);
214
215 return 0;
216}
217
218static int imx_controller_resume(struct device *dev)
219{
220 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
221
222 dev_dbg(dev, "at %s\n", __func__);
223
224 return clk_prepare_enable(data->clk);
225}
226
227static int ci_hdrc_imx_suspend(struct device *dev)
228{
229 return imx_controller_suspend(dev);
230}
231
232static int ci_hdrc_imx_resume(struct device *dev)
233{
234 return imx_controller_resume(dev);
235}
236#endif /* CONFIG_PM_SLEEP */
237
238static const struct dev_pm_ops ci_hdrc_imx_pm_ops = {
239 SET_SYSTEM_SLEEP_PM_OPS(ci_hdrc_imx_suspend, ci_hdrc_imx_resume)
240};
Alexander Shishkin8e229782013-06-24 14:46:36 +0300241static struct platform_driver ci_hdrc_imx_driver = {
242 .probe = ci_hdrc_imx_probe,
243 .remove = ci_hdrc_imx_remove,
Richard Zhao15302802012-07-07 22:56:48 +0800244 .driver = {
245 .name = "imx_usb",
Alexander Shishkin8e229782013-06-24 14:46:36 +0300246 .of_match_table = ci_hdrc_imx_dt_ids,
Peter Chen2558c1f2014-11-26 13:44:30 +0800247 .pm = &ci_hdrc_imx_pm_ops,
Richard Zhao15302802012-07-07 22:56:48 +0800248 },
249};
250
Alexander Shishkin8e229782013-06-24 14:46:36 +0300251module_platform_driver(ci_hdrc_imx_driver);
Richard Zhao15302802012-07-07 22:56:48 +0800252
253MODULE_ALIAS("platform:imx-usb");
254MODULE_LICENSE("GPL v2");
Alexander Shishkin8e229782013-06-24 14:46:36 +0300255MODULE_DESCRIPTION("CI HDRC i.MX USB binding");
Richard Zhao15302802012-07-07 22:56:48 +0800256MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
257MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");