blob: c00f77257d3651ff3e99858b23e1396e2df921b3 [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 +080026#define CI_HDRC_IMX_IMX28_WRITE_FIX BIT(0)
27
28struct ci_hdrc_imx_platform_flag {
29 unsigned int flags;
30};
31
32static const struct ci_hdrc_imx_platform_flag imx27_usb_data = {
33};
34
35static const struct ci_hdrc_imx_platform_flag imx28_usb_data = {
36 .flags = CI_HDRC_IMX_IMX28_WRITE_FIX,
37};
38
39static const struct of_device_id ci_hdrc_imx_dt_ids[] = {
40 { .compatible = "fsl,imx28-usb", .data = &imx28_usb_data},
41 { .compatible = "fsl,imx27-usb", .data = &imx27_usb_data},
42 { /* sentinel */ }
43};
44MODULE_DEVICE_TABLE(of, ci_hdrc_imx_dt_ids);
45
Alexander Shishkin8e229782013-06-24 14:46:36 +030046struct ci_hdrc_imx_data {
Richard Zhao15302802012-07-07 22:56:48 +080047 struct usb_phy *phy;
48 struct platform_device *ci_pdev;
49 struct clk *clk;
Sascha Hauer05986ba2013-08-14 12:44:16 +030050 struct imx_usbmisc_data *usbmisc_data;
Richard Zhao15302802012-07-07 22:56:48 +080051};
52
Richard Zhaod142d6b2012-09-12 14:58:05 +030053/* Common functions shared by usbmisc drivers */
54
Sascha Hauer05986ba2013-08-14 12:44:16 +030055static struct imx_usbmisc_data *usbmisc_get_init_data(struct device *dev)
Richard Zhaod142d6b2012-09-12 14:58:05 +030056{
57 struct device_node *np = dev->of_node;
58 struct of_phandle_args args;
Sascha Hauer05986ba2013-08-14 12:44:16 +030059 struct imx_usbmisc_data *data;
Richard Zhaod142d6b2012-09-12 14:58:05 +030060 int ret;
61
Sascha Hauer05986ba2013-08-14 12:44:16 +030062 /*
63 * In case the fsl,usbmisc property is not present this device doesn't
64 * need usbmisc. Return NULL (which is no error here)
65 */
66 if (!of_get_property(np, "fsl,usbmisc", NULL))
67 return NULL;
68
69 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
70 if (!data)
71 return ERR_PTR(-ENOMEM);
Richard Zhaod142d6b2012-09-12 14:58:05 +030072
73 ret = of_parse_phandle_with_args(np, "fsl,usbmisc", "#index-cells",
74 0, &args);
75 if (ret) {
76 dev_err(dev, "Failed to parse property fsl,usbmisc, errno %d\n",
77 ret);
Sascha Hauer05986ba2013-08-14 12:44:16 +030078 return ERR_PTR(ret);
Richard Zhaod142d6b2012-09-12 14:58:05 +030079 }
Sascha Hauer05986ba2013-08-14 12:44:16 +030080
81 data->index = args.args[0];
Richard Zhaod142d6b2012-09-12 14:58:05 +030082 of_node_put(args.np);
83
84 if (of_find_property(np, "disable-over-current", NULL))
Sascha Hauer05986ba2013-08-14 12:44:16 +030085 data->disable_oc = 1;
Richard Zhaod142d6b2012-09-12 14:58:05 +030086
Michael Grzeschika0685332013-03-30 12:54:01 +020087 if (of_find_property(np, "external-vbus-divider", NULL))
Sascha Hauer05986ba2013-08-14 12:44:16 +030088 data->evdo = 1;
Michael Grzeschika0685332013-03-30 12:54:01 +020089
Sascha Hauer05986ba2013-08-14 12:44:16 +030090 return data;
Richard Zhaod142d6b2012-09-12 14:58:05 +030091}
Richard Zhaod142d6b2012-09-12 14:58:05 +030092
93/* End of common functions shared by usbmisc drivers*/
94
Alexander Shishkin8e229782013-06-24 14:46:36 +030095static int ci_hdrc_imx_probe(struct platform_device *pdev)
Richard Zhao15302802012-07-07 22:56:48 +080096{
Alexander Shishkin8e229782013-06-24 14:46:36 +030097 struct ci_hdrc_imx_data *data;
98 struct ci_hdrc_platform_data pdata = {
99 .name = "ci_hdrc_imx",
Michael Grzeschikf6a3b3a2013-06-13 17:59:58 +0300100 .capoffset = DEF_CAPOFFSET,
Alexander Shishkin8e229782013-06-24 14:46:36 +0300101 .flags = CI_HDRC_REQUIRE_TRANSCEIVER |
Alexander Shishkin8e229782013-06-24 14:46:36 +0300102 CI_HDRC_DISABLE_STREAMING,
Michael Grzeschikf6a3b3a2013-06-13 17:59:58 +0300103 };
Richard Zhao15302802012-07-07 22:56:48 +0800104 int ret;
Peter Chen10710552014-01-10 13:51:28 +0800105 const struct of_device_id *of_id =
106 of_match_device(ci_hdrc_imx_dt_ids, &pdev->dev);
107 const struct ci_hdrc_imx_platform_flag *imx_platform_flag = of_id->data;
Richard Zhao15302802012-07-07 22:56:48 +0800108
109 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
110 if (!data) {
Alexander Shishkin8e229782013-06-24 14:46:36 +0300111 dev_err(&pdev->dev, "Failed to allocate ci_hdrc-imx data!\n");
Richard Zhao15302802012-07-07 22:56:48 +0800112 return -ENOMEM;
113 }
114
Sascha Hauer05986ba2013-08-14 12:44:16 +0300115 data->usbmisc_data = usbmisc_get_init_data(&pdev->dev);
116 if (IS_ERR(data->usbmisc_data))
117 return PTR_ERR(data->usbmisc_data);
118
Richard Zhao15302802012-07-07 22:56:48 +0800119 data->clk = devm_clk_get(&pdev->dev, NULL);
120 if (IS_ERR(data->clk)) {
121 dev_err(&pdev->dev,
122 "Failed to get clock, err=%ld\n", PTR_ERR(data->clk));
123 return PTR_ERR(data->clk);
124 }
125
126 ret = clk_prepare_enable(data->clk);
127 if (ret) {
128 dev_err(&pdev->dev,
129 "Failed to prepare or enable clock, err=%d\n", ret);
130 return ret;
131 }
132
Fabio Estevam046916d2013-06-25 12:58:05 +0300133 data->phy = devm_usb_get_phy_by_phandle(&pdev->dev, "fsl,usbphy", 0);
Peter Chenaf59a8b2013-09-24 12:47:54 +0800134 if (IS_ERR(data->phy)) {
135 ret = PTR_ERR(data->phy);
Sascha Hauerea1418b2013-06-13 18:00:00 +0300136 goto err_clk;
Richard Zhao15302802012-07-07 22:56:48 +0800137 }
138
Michael Grzeschikf6a3b3a2013-06-13 17:59:58 +0300139 pdata.phy = data->phy;
Richard Zhao15302802012-07-07 22:56:48 +0800140
Peter Chen10710552014-01-10 13:51:28 +0800141 if (imx_platform_flag->flags & CI_HDRC_IMX_IMX28_WRITE_FIX)
142 pdata.flags |= CI_HDRC_IMX28_WRITE_FIX;
143
Russell Kinge1fd7342013-06-27 12:36:37 +0100144 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
Russell King22d9d8e2013-06-10 16:28:49 +0100145 if (ret)
146 goto err_clk;
Richard Zhaod142d6b2012-09-12 14:58:05 +0300147
Sascha Hauer05986ba2013-08-14 12:44:16 +0300148 if (data->usbmisc_data) {
149 ret = imx_usbmisc_init(data->usbmisc_data);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300150 if (ret) {
Sascha Hauer05986ba2013-08-14 12:44:16 +0300151 dev_err(&pdev->dev, "usbmisc init failed, ret=%d\n",
152 ret);
Peter Chenaf59a8b2013-09-24 12:47:54 +0800153 goto err_clk;
Richard Zhaod142d6b2012-09-12 14:58:05 +0300154 }
155 }
156
Alexander Shishkin8e229782013-06-24 14:46:36 +0300157 data->ci_pdev = ci_hdrc_add_device(&pdev->dev,
Richard Zhao15302802012-07-07 22:56:48 +0800158 pdev->resource, pdev->num_resources,
Michael Grzeschikf6a3b3a2013-06-13 17:59:58 +0300159 &pdata);
Fabio Estevam770719d2013-06-13 17:59:48 +0300160 if (IS_ERR(data->ci_pdev)) {
161 ret = PTR_ERR(data->ci_pdev);
Richard Zhao15302802012-07-07 22:56:48 +0800162 dev_err(&pdev->dev,
163 "Can't register ci_hdrc platform device, err=%d\n",
164 ret);
Peter Chenaf59a8b2013-09-24 12:47:54 +0800165 goto err_clk;
Richard Zhao15302802012-07-07 22:56:48 +0800166 }
167
Sascha Hauer05986ba2013-08-14 12:44:16 +0300168 if (data->usbmisc_data) {
169 ret = imx_usbmisc_init_post(data->usbmisc_data);
Michael Grzeschika0685332013-03-30 12:54:01 +0200170 if (ret) {
Sascha Hauer05986ba2013-08-14 12:44:16 +0300171 dev_err(&pdev->dev, "usbmisc post failed, ret=%d\n",
172 ret);
Fabio Estevam770719d2013-06-13 17:59:48 +0300173 goto disable_device;
Michael Grzeschika0685332013-03-30 12:54:01 +0200174 }
175 }
176
Richard Zhao15302802012-07-07 22:56:48 +0800177 platform_set_drvdata(pdev, data);
178
179 pm_runtime_no_callbacks(&pdev->dev);
180 pm_runtime_enable(&pdev->dev);
181
182 return 0;
183
Fabio Estevam770719d2013-06-13 17:59:48 +0300184disable_device:
Alexander Shishkin8e229782013-06-24 14:46:36 +0300185 ci_hdrc_remove_device(data->ci_pdev);
Sascha Hauerea1418b2013-06-13 18:00:00 +0300186err_clk:
Richard Zhao15302802012-07-07 22:56:48 +0800187 clk_disable_unprepare(data->clk);
188 return ret;
189}
190
Alexander Shishkin8e229782013-06-24 14:46:36 +0300191static int ci_hdrc_imx_remove(struct platform_device *pdev)
Richard Zhao15302802012-07-07 22:56:48 +0800192{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300193 struct ci_hdrc_imx_data *data = platform_get_drvdata(pdev);
Richard Zhao15302802012-07-07 22:56:48 +0800194
195 pm_runtime_disable(&pdev->dev);
Alexander Shishkin8e229782013-06-24 14:46:36 +0300196 ci_hdrc_remove_device(data->ci_pdev);
Richard Zhao15302802012-07-07 22:56:48 +0800197 clk_disable_unprepare(data->clk);
198
Richard Zhao15302802012-07-07 22:56:48 +0800199 return 0;
200}
201
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>");