usb: chipidea: i.MX: use devm_usb_get_phy_by_phandle to get phy
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / usb / chipidea / ci13xxx_imx.c
1 /*
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>
22 #include <linux/regulator/consumer.h>
23
24 #include "ci.h"
25 #include "ci13xxx_imx.h"
26
27 #define pdev_to_phy(pdev) \
28         ((struct usb_phy *)platform_get_drvdata(pdev))
29
30 struct ci13xxx_imx_data {
31         struct usb_phy *phy;
32         struct platform_device *ci_pdev;
33         struct clk *clk;
34         struct regulator *reg_vbus;
35 };
36
37 static const struct usbmisc_ops *usbmisc_ops;
38
39 /* Common functions shared by usbmisc drivers */
40
41 int usbmisc_set_ops(const struct usbmisc_ops *ops)
42 {
43         if (usbmisc_ops)
44                 return -EBUSY;
45
46         usbmisc_ops = ops;
47
48         return 0;
49 }
50 EXPORT_SYMBOL_GPL(usbmisc_set_ops);
51
52 void usbmisc_unset_ops(const struct usbmisc_ops *ops)
53 {
54         usbmisc_ops = NULL;
55 }
56 EXPORT_SYMBOL_GPL(usbmisc_unset_ops);
57
58 int usbmisc_get_init_data(struct device *dev, struct usbmisc_usb_device *usbdev)
59 {
60         struct device_node *np = dev->of_node;
61         struct of_phandle_args args;
62         int ret;
63
64         usbdev->dev = dev;
65
66         ret = of_parse_phandle_with_args(np, "fsl,usbmisc", "#index-cells",
67                                         0, &args);
68         if (ret) {
69                 dev_err(dev, "Failed to parse property fsl,usbmisc, errno %d\n",
70                         ret);
71                 memset(usbdev, 0, sizeof(*usbdev));
72                 return ret;
73         }
74         usbdev->index = args.args[0];
75         of_node_put(args.np);
76
77         if (of_find_property(np, "disable-over-current", NULL))
78                 usbdev->disable_oc = 1;
79
80         if (of_find_property(np, "external-vbus-divider", NULL))
81                 usbdev->evdo = 1;
82
83         return 0;
84 }
85 EXPORT_SYMBOL_GPL(usbmisc_get_init_data);
86
87 /* End of common functions shared by usbmisc drivers*/
88
89 static int ci13xxx_imx_probe(struct platform_device *pdev)
90 {
91         struct ci13xxx_imx_data *data;
92         struct ci13xxx_platform_data pdata = {
93                 .name           = "ci13xxx_imx",
94                 .capoffset      = DEF_CAPOFFSET,
95                 .flags          = CI13XXX_REQUIRE_TRANSCEIVER |
96                                   CI13XXX_PULLUP_ON_VBUS |
97                                   CI13XXX_DISABLE_STREAMING,
98         };
99         struct resource *res;
100         int ret;
101         struct usb_phy *phy;
102
103         if (of_find_property(pdev->dev.of_node, "fsl,usbmisc", NULL)
104                 && !usbmisc_ops)
105                 return -EPROBE_DEFER;
106
107         data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
108         if (!data) {
109                 dev_err(&pdev->dev, "Failed to allocate CI13xxx-IMX data!\n");
110                 return -ENOMEM;
111         }
112
113         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
114         if (!res) {
115                 dev_err(&pdev->dev, "Can't get device resources!\n");
116                 return -ENOENT;
117         }
118
119         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
133         phy = devm_usb_get_phy_by_phandle(&pdev->dev, "fsl,usbphy", 0);
134         if (!IS_ERR(phy)) {
135                 ret = usb_phy_init(phy);
136                 if (ret) {
137                         dev_err(&pdev->dev, "unable to init phy: %d\n", ret);
138                         goto err_clk;
139                 }
140         } else if (PTR_ERR(phy) == -EPROBE_DEFER) {
141                 ret = -EPROBE_DEFER;
142                 goto err_clk;
143         }
144
145         /* we only support host now, so enable vbus here */
146         data->reg_vbus = devm_regulator_get(&pdev->dev, "vbus");
147         if (!IS_ERR(data->reg_vbus)) {
148                 ret = regulator_enable(data->reg_vbus);
149                 if (ret) {
150                         dev_err(&pdev->dev,
151                                 "Failed to enable vbus regulator, err=%d\n",
152                                 ret);
153                         goto err_clk;
154                 }
155         } else {
156                 data->reg_vbus = NULL;
157         }
158
159         pdata.phy = data->phy;
160
161         if (!pdev->dev.dma_mask)
162                 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
163         if (!pdev->dev.coherent_dma_mask)
164                 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
165
166         if (usbmisc_ops && usbmisc_ops->init) {
167                 ret = usbmisc_ops->init(&pdev->dev);
168                 if (ret) {
169                         dev_err(&pdev->dev,
170                                 "usbmisc init failed, ret=%d\n", ret);
171                         goto err;
172                 }
173         }
174
175         data->ci_pdev = ci13xxx_add_device(&pdev->dev,
176                                 pdev->resource, pdev->num_resources,
177                                 &pdata);
178         if (IS_ERR(data->ci_pdev)) {
179                 ret = PTR_ERR(data->ci_pdev);
180                 dev_err(&pdev->dev,
181                         "Can't register ci_hdrc platform device, err=%d\n",
182                         ret);
183                 goto err;
184         }
185
186         if (usbmisc_ops && usbmisc_ops->post) {
187                 ret = usbmisc_ops->post(&pdev->dev);
188                 if (ret) {
189                         dev_err(&pdev->dev,
190                                 "usbmisc post failed, ret=%d\n", ret);
191                         goto disable_device;
192                 }
193         }
194
195         platform_set_drvdata(pdev, data);
196
197         pm_runtime_no_callbacks(&pdev->dev);
198         pm_runtime_enable(&pdev->dev);
199
200         return 0;
201
202 disable_device:
203         ci13xxx_remove_device(data->ci_pdev);
204 err:
205         if (data->reg_vbus)
206                 regulator_disable(data->reg_vbus);
207 err_clk:
208         clk_disable_unprepare(data->clk);
209         return ret;
210 }
211
212 static int ci13xxx_imx_remove(struct platform_device *pdev)
213 {
214         struct ci13xxx_imx_data *data = platform_get_drvdata(pdev);
215
216         pm_runtime_disable(&pdev->dev);
217         ci13xxx_remove_device(data->ci_pdev);
218
219         if (data->reg_vbus)
220                 regulator_disable(data->reg_vbus);
221
222         if (data->phy) {
223                 usb_phy_shutdown(data->phy);
224                 module_put(data->phy->dev->driver->owner);
225         }
226
227         clk_disable_unprepare(data->clk);
228
229         return 0;
230 }
231
232 static const struct of_device_id ci13xxx_imx_dt_ids[] = {
233         { .compatible = "fsl,imx27-usb", },
234         { /* sentinel */ }
235 };
236 MODULE_DEVICE_TABLE(of, ci13xxx_imx_dt_ids);
237
238 static struct platform_driver ci13xxx_imx_driver = {
239         .probe = ci13xxx_imx_probe,
240         .remove = ci13xxx_imx_remove,
241         .driver = {
242                 .name = "imx_usb",
243                 .owner = THIS_MODULE,
244                 .of_match_table = ci13xxx_imx_dt_ids,
245          },
246 };
247
248 module_platform_driver(ci13xxx_imx_driver);
249
250 MODULE_ALIAS("platform:imx-usb");
251 MODULE_LICENSE("GPL v2");
252 MODULE_DESCRIPTION("CI13xxx i.MX USB binding");
253 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
254 MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");