2 * Copyright 2012 Freescale Semiconductor, Inc.
3 * Copyright (C) 2012 Marek Vasut <marex@denx.de>
4 * on behalf of DENX Software Engineering GmbH
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:
10 * http://www.opensource.org/licenses/gpl-license.html
11 * http://www.gnu.org/copyleft/gpl.html
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>
25 #include "ci_hdrc_imx.h"
27 struct ci_hdrc_imx_data {
29 struct platform_device *ci_pdev;
31 struct regulator *reg_vbus;
34 static const struct usbmisc_ops *usbmisc_ops;
36 /* Common functions shared by usbmisc drivers */
38 int usbmisc_set_ops(const struct usbmisc_ops *ops)
47 EXPORT_SYMBOL_GPL(usbmisc_set_ops);
49 void usbmisc_unset_ops(const struct usbmisc_ops *ops)
53 EXPORT_SYMBOL_GPL(usbmisc_unset_ops);
55 int usbmisc_get_init_data(struct device *dev, struct usbmisc_usb_device *usbdev)
57 struct device_node *np = dev->of_node;
58 struct of_phandle_args args;
63 ret = of_parse_phandle_with_args(np, "fsl,usbmisc", "#index-cells",
66 dev_err(dev, "Failed to parse property fsl,usbmisc, errno %d\n",
68 memset(usbdev, 0, sizeof(*usbdev));
71 usbdev->index = args.args[0];
74 if (of_find_property(np, "disable-over-current", NULL))
75 usbdev->disable_oc = 1;
77 if (of_find_property(np, "external-vbus-divider", NULL))
82 EXPORT_SYMBOL_GPL(usbmisc_get_init_data);
84 /* End of common functions shared by usbmisc drivers*/
86 static int ci_hdrc_imx_probe(struct platform_device *pdev)
88 struct ci_hdrc_imx_data *data;
89 struct ci_hdrc_platform_data pdata = {
90 .name = "ci_hdrc_imx",
91 .capoffset = DEF_CAPOFFSET,
92 .flags = CI_HDRC_REQUIRE_TRANSCEIVER |
93 CI_HDRC_PULLUP_ON_VBUS |
94 CI_HDRC_DISABLE_STREAMING,
98 if (of_find_property(pdev->dev.of_node, "fsl,usbmisc", NULL)
100 return -EPROBE_DEFER;
102 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
104 dev_err(&pdev->dev, "Failed to allocate ci_hdrc-imx data!\n");
108 data->clk = devm_clk_get(&pdev->dev, NULL);
109 if (IS_ERR(data->clk)) {
111 "Failed to get clock, err=%ld\n", PTR_ERR(data->clk));
112 return PTR_ERR(data->clk);
115 ret = clk_prepare_enable(data->clk);
118 "Failed to prepare or enable clock, err=%d\n", ret);
122 data->phy = devm_usb_get_phy_by_phandle(&pdev->dev, "fsl,usbphy", 0);
123 if (!IS_ERR(data->phy)) {
124 ret = usb_phy_init(data->phy);
126 dev_err(&pdev->dev, "unable to init phy: %d\n", ret);
129 } else if (PTR_ERR(data->phy) == -EPROBE_DEFER) {
134 /* we only support host now, so enable vbus here */
135 data->reg_vbus = devm_regulator_get(&pdev->dev, "vbus");
136 if (!IS_ERR(data->reg_vbus)) {
137 ret = regulator_enable(data->reg_vbus);
140 "Failed to enable vbus regulator, err=%d\n",
145 data->reg_vbus = NULL;
148 pdata.phy = data->phy;
150 if (!pdev->dev.dma_mask)
151 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
152 if (!pdev->dev.coherent_dma_mask)
153 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
155 if (usbmisc_ops && usbmisc_ops->init) {
156 ret = usbmisc_ops->init(&pdev->dev);
159 "usbmisc init failed, ret=%d\n", ret);
164 data->ci_pdev = ci_hdrc_add_device(&pdev->dev,
165 pdev->resource, pdev->num_resources,
167 if (IS_ERR(data->ci_pdev)) {
168 ret = PTR_ERR(data->ci_pdev);
170 "Can't register ci_hdrc platform device, err=%d\n",
175 if (usbmisc_ops && usbmisc_ops->post) {
176 ret = usbmisc_ops->post(&pdev->dev);
179 "usbmisc post failed, ret=%d\n", ret);
184 platform_set_drvdata(pdev, data);
186 pm_runtime_no_callbacks(&pdev->dev);
187 pm_runtime_enable(&pdev->dev);
192 ci_hdrc_remove_device(data->ci_pdev);
195 regulator_disable(data->reg_vbus);
197 clk_disable_unprepare(data->clk);
201 static int ci_hdrc_imx_remove(struct platform_device *pdev)
203 struct ci_hdrc_imx_data *data = platform_get_drvdata(pdev);
205 pm_runtime_disable(&pdev->dev);
206 ci_hdrc_remove_device(data->ci_pdev);
209 regulator_disable(data->reg_vbus);
212 usb_phy_shutdown(data->phy);
214 clk_disable_unprepare(data->clk);
219 static const struct of_device_id ci_hdrc_imx_dt_ids[] = {
220 { .compatible = "fsl,imx27-usb", },
223 MODULE_DEVICE_TABLE(of, ci_hdrc_imx_dt_ids);
225 static struct platform_driver ci_hdrc_imx_driver = {
226 .probe = ci_hdrc_imx_probe,
227 .remove = ci_hdrc_imx_remove,
230 .owner = THIS_MODULE,
231 .of_match_table = ci_hdrc_imx_dt_ids,
235 module_platform_driver(ci_hdrc_imx_driver);
237 MODULE_ALIAS("platform:imx-usb");
238 MODULE_LICENSE("GPL v2");
239 MODULE_DESCRIPTION("CI HDRC i.MX USB binding");
240 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
241 MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");