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>
23 #include <linux/pinctrl/consumer.h>
26 #include "ci13xxx_imx.h"
28 #define pdev_to_phy(pdev) \
29 ((struct usb_phy *)platform_get_drvdata(pdev))
31 struct ci13xxx_imx_data {
32 struct device_node *phy_np;
34 struct platform_device *ci_pdev;
36 struct regulator *reg_vbus;
39 static const struct usbmisc_ops *usbmisc_ops;
41 /* Common functions shared by usbmisc drivers */
43 int usbmisc_set_ops(const struct usbmisc_ops *ops)
52 EXPORT_SYMBOL_GPL(usbmisc_set_ops);
54 void usbmisc_unset_ops(const struct usbmisc_ops *ops)
58 EXPORT_SYMBOL_GPL(usbmisc_unset_ops);
60 int usbmisc_get_init_data(struct device *dev, struct usbmisc_usb_device *usbdev)
62 struct device_node *np = dev->of_node;
63 struct of_phandle_args args;
68 ret = of_parse_phandle_with_args(np, "fsl,usbmisc", "#index-cells",
71 dev_err(dev, "Failed to parse property fsl,usbmisc, errno %d\n",
73 memset(usbdev, 0, sizeof(*usbdev));
76 usbdev->index = args.args[0];
79 if (of_find_property(np, "disable-over-current", NULL))
80 usbdev->disable_oc = 1;
82 if (of_find_property(np, "external-vbus-divider", NULL))
87 EXPORT_SYMBOL_GPL(usbmisc_get_init_data);
89 /* End of common functions shared by usbmisc drivers*/
91 static struct ci13xxx_platform_data ci13xxx_imx_platdata = {
92 .name = "ci13xxx_imx",
93 .flags = CI13XXX_REQUIRE_TRANSCEIVER |
94 CI13XXX_PULLUP_ON_VBUS |
95 CI13XXX_DISABLE_STREAMING,
96 .capoffset = DEF_CAPOFFSET,
99 static int ci13xxx_imx_probe(struct platform_device *pdev)
101 struct ci13xxx_imx_data *data;
102 struct platform_device *plat_ci, *phy_pdev;
103 struct device_node *phy_np;
104 struct resource *res;
105 struct regulator *reg_vbus;
106 struct pinctrl *pinctrl;
109 if (of_find_property(pdev->dev.of_node, "fsl,usbmisc", NULL)
111 return -EPROBE_DEFER;
113 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
115 dev_err(&pdev->dev, "Failed to allocate CI13xxx-IMX data!\n");
119 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
121 dev_err(&pdev->dev, "Can't get device resources!\n");
125 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
127 dev_warn(&pdev->dev, "pinctrl get/select failed, err=%ld\n",
130 data->clk = devm_clk_get(&pdev->dev, NULL);
131 if (IS_ERR(data->clk)) {
133 "Failed to get clock, err=%ld\n", PTR_ERR(data->clk));
134 return PTR_ERR(data->clk);
137 ret = clk_prepare_enable(data->clk);
140 "Failed to prepare or enable clock, err=%d\n", ret);
144 phy_np = of_parse_phandle(pdev->dev.of_node, "fsl,usbphy", 0);
146 data->phy_np = phy_np;
147 phy_pdev = of_find_device_by_node(phy_np);
150 phy = pdev_to_phy(phy_pdev);
152 try_module_get(phy_pdev->dev.driver->owner)) {
159 /* we only support host now, so enable vbus here */
160 reg_vbus = devm_regulator_get(&pdev->dev, "vbus");
161 if (!IS_ERR(reg_vbus)) {
162 ret = regulator_enable(reg_vbus);
165 "Failed to enable vbus regulator, err=%d\n",
169 data->reg_vbus = reg_vbus;
174 ci13xxx_imx_platdata.phy = data->phy;
176 if (!pdev->dev.dma_mask)
177 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
178 if (!pdev->dev.coherent_dma_mask)
179 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
181 if (usbmisc_ops && usbmisc_ops->init) {
182 ret = usbmisc_ops->init(&pdev->dev);
185 "usbmisc init failed, ret=%d\n", ret);
190 plat_ci = ci13xxx_add_device(&pdev->dev,
191 pdev->resource, pdev->num_resources,
192 &ci13xxx_imx_platdata);
193 if (IS_ERR(plat_ci)) {
194 ret = PTR_ERR(plat_ci);
196 "Can't register ci_hdrc platform device, err=%d\n",
201 if (usbmisc_ops && usbmisc_ops->post) {
202 ret = usbmisc_ops->post(&pdev->dev);
205 "usbmisc post failed, ret=%d\n", ret);
210 data->ci_pdev = plat_ci;
211 platform_set_drvdata(pdev, data);
213 pm_runtime_no_callbacks(&pdev->dev);
214 pm_runtime_enable(&pdev->dev);
220 regulator_disable(reg_vbus);
224 clk_disable_unprepare(data->clk);
228 static int ci13xxx_imx_remove(struct platform_device *pdev)
230 struct ci13xxx_imx_data *data = platform_get_drvdata(pdev);
232 pm_runtime_disable(&pdev->dev);
233 ci13xxx_remove_device(data->ci_pdev);
236 regulator_disable(data->reg_vbus);
239 usb_phy_shutdown(data->phy);
240 module_put(data->phy->dev->driver->owner);
243 of_node_put(data->phy_np);
245 clk_disable_unprepare(data->clk);
247 platform_set_drvdata(pdev, NULL);
252 static const struct of_device_id ci13xxx_imx_dt_ids[] = {
253 { .compatible = "fsl,imx27-usb", },
256 MODULE_DEVICE_TABLE(of, ci13xxx_imx_dt_ids);
258 static struct platform_driver ci13xxx_imx_driver = {
259 .probe = ci13xxx_imx_probe,
260 .remove = ci13xxx_imx_remove,
263 .owner = THIS_MODULE,
264 .of_match_table = ci13xxx_imx_dt_ids,
268 module_platform_driver(ci13xxx_imx_driver);
270 MODULE_ALIAS("platform:imx-usb");
271 MODULE_LICENSE("GPL v2");
272 MODULE_DESCRIPTION("CI13xxx i.MX USB binding");
273 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
274 MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");