Commit | Line | Data |
---|---|---|
15302800 RZ |
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> | |
15302800 RZ |
22 | |
23 | #include "ci.h" | |
8e22978c | 24 | #include "ci_hdrc_imx.h" |
15302800 | 25 | |
1071055e PC |
26 | struct ci_hdrc_imx_platform_flag { |
27 | unsigned int flags; | |
e14db48d | 28 | bool runtime_pm; |
1071055e PC |
29 | }; |
30 | ||
31 | static const struct ci_hdrc_imx_platform_flag imx27_usb_data = { | |
32 | }; | |
33 | ||
34 | static const struct ci_hdrc_imx_platform_flag imx28_usb_data = { | |
56040087 | 35 | .flags = CI_HDRC_IMX28_WRITE_FIX, |
1071055e PC |
36 | }; |
37 | ||
e14db48d PC |
38 | static const struct ci_hdrc_imx_platform_flag imx6q_usb_data = { |
39 | .flags = CI_HDRC_SUPPORTS_RUNTIME_PM, | |
40 | }; | |
41 | ||
42 | static const struct ci_hdrc_imx_platform_flag imx6sl_usb_data = { | |
43 | .flags = CI_HDRC_SUPPORTS_RUNTIME_PM, | |
44 | }; | |
45 | ||
46 | static const struct ci_hdrc_imx_platform_flag imx6sx_usb_data = { | |
47 | .flags = CI_HDRC_SUPPORTS_RUNTIME_PM, | |
48 | }; | |
49 | ||
1071055e PC |
50 | static const struct of_device_id ci_hdrc_imx_dt_ids[] = { |
51 | { .compatible = "fsl,imx28-usb", .data = &imx28_usb_data}, | |
52 | { .compatible = "fsl,imx27-usb", .data = &imx27_usb_data}, | |
e14db48d PC |
53 | { .compatible = "fsl,imx6q-usb", .data = &imx6q_usb_data}, |
54 | { .compatible = "fsl,imx6sl-usb", .data = &imx6sl_usb_data}, | |
55 | { .compatible = "fsl,imx6sx-usb", .data = &imx6sl_usb_data}, | |
1071055e PC |
56 | { /* sentinel */ } |
57 | }; | |
58 | MODULE_DEVICE_TABLE(of, ci_hdrc_imx_dt_ids); | |
59 | ||
8e22978c | 60 | struct ci_hdrc_imx_data { |
15302800 RZ |
61 | struct usb_phy *phy; |
62 | struct platform_device *ci_pdev; | |
63 | struct clk *clk; | |
05986ba9 | 64 | struct imx_usbmisc_data *usbmisc_data; |
e14db48d PC |
65 | bool supports_runtime_pm; |
66 | bool in_lpm; | |
15302800 RZ |
67 | }; |
68 | ||
d142d6be RZ |
69 | /* Common functions shared by usbmisc drivers */ |
70 | ||
05986ba9 | 71 | static struct imx_usbmisc_data *usbmisc_get_init_data(struct device *dev) |
d142d6be | 72 | { |
f40017e0 | 73 | struct platform_device *misc_pdev; |
d142d6be RZ |
74 | struct device_node *np = dev->of_node; |
75 | struct of_phandle_args args; | |
05986ba9 | 76 | struct imx_usbmisc_data *data; |
d142d6be RZ |
77 | int ret; |
78 | ||
05986ba9 SH |
79 | /* |
80 | * In case the fsl,usbmisc property is not present this device doesn't | |
81 | * need usbmisc. Return NULL (which is no error here) | |
82 | */ | |
83 | if (!of_get_property(np, "fsl,usbmisc", NULL)) | |
84 | return NULL; | |
85 | ||
86 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); | |
87 | if (!data) | |
88 | return ERR_PTR(-ENOMEM); | |
d142d6be RZ |
89 | |
90 | ret = of_parse_phandle_with_args(np, "fsl,usbmisc", "#index-cells", | |
91 | 0, &args); | |
92 | if (ret) { | |
93 | dev_err(dev, "Failed to parse property fsl,usbmisc, errno %d\n", | |
94 | ret); | |
05986ba9 | 95 | return ERR_PTR(ret); |
d142d6be | 96 | } |
05986ba9 SH |
97 | |
98 | data->index = args.args[0]; | |
f40017e0 SA |
99 | |
100 | misc_pdev = of_find_device_by_node(args.np); | |
d142d6be RZ |
101 | of_node_put(args.np); |
102 | ||
f40017e0 SA |
103 | if (!misc_pdev) |
104 | return ERR_PTR(-EPROBE_DEFER); | |
105 | ||
106 | data->dev = &misc_pdev->dev; | |
107 | ||
d142d6be | 108 | if (of_find_property(np, "disable-over-current", NULL)) |
05986ba9 | 109 | data->disable_oc = 1; |
d142d6be | 110 | |
a0685330 | 111 | if (of_find_property(np, "external-vbus-divider", NULL)) |
05986ba9 | 112 | data->evdo = 1; |
a0685330 | 113 | |
05986ba9 | 114 | return data; |
d142d6be | 115 | } |
d142d6be RZ |
116 | |
117 | /* End of common functions shared by usbmisc drivers*/ | |
118 | ||
8e22978c | 119 | static int ci_hdrc_imx_probe(struct platform_device *pdev) |
15302800 | 120 | { |
8e22978c AS |
121 | struct ci_hdrc_imx_data *data; |
122 | struct ci_hdrc_platform_data pdata = { | |
c844d6c8 | 123 | .name = dev_name(&pdev->dev), |
f6a3b3a3 | 124 | .capoffset = DEF_CAPOFFSET, |
947c8859 | 125 | .flags = CI_HDRC_DISABLE_STREAMING, |
f6a3b3a3 | 126 | }; |
15302800 | 127 | int ret; |
1071055e PC |
128 | const struct of_device_id *of_id = |
129 | of_match_device(ci_hdrc_imx_dt_ids, &pdev->dev); | |
130 | const struct ci_hdrc_imx_platform_flag *imx_platform_flag = of_id->data; | |
15302800 RZ |
131 | |
132 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); | |
73529828 | 133 | if (!data) |
15302800 | 134 | return -ENOMEM; |
15302800 | 135 | |
05986ba9 SH |
136 | data->usbmisc_data = usbmisc_get_init_data(&pdev->dev); |
137 | if (IS_ERR(data->usbmisc_data)) | |
138 | return PTR_ERR(data->usbmisc_data); | |
139 | ||
15302800 RZ |
140 | data->clk = devm_clk_get(&pdev->dev, NULL); |
141 | if (IS_ERR(data->clk)) { | |
142 | dev_err(&pdev->dev, | |
143 | "Failed to get clock, err=%ld\n", PTR_ERR(data->clk)); | |
144 | return PTR_ERR(data->clk); | |
145 | } | |
146 | ||
147 | ret = clk_prepare_enable(data->clk); | |
148 | if (ret) { | |
149 | dev_err(&pdev->dev, | |
150 | "Failed to prepare or enable clock, err=%d\n", ret); | |
151 | return ret; | |
152 | } | |
153 | ||
046916de | 154 | data->phy = devm_usb_get_phy_by_phandle(&pdev->dev, "fsl,usbphy", 0); |
af59a8b1 PC |
155 | if (IS_ERR(data->phy)) { |
156 | ret = PTR_ERR(data->phy); | |
16853d7b MP |
157 | /* Return -EINVAL if no usbphy is available */ |
158 | if (ret == -ENODEV) | |
159 | ret = -EINVAL; | |
ea1418b5 | 160 | goto err_clk; |
15302800 RZ |
161 | } |
162 | ||
ef44cb42 | 163 | pdata.usb_phy = data->phy; |
56040087 | 164 | pdata.flags |= imx_platform_flag->flags; |
e14db48d PC |
165 | if (pdata.flags & CI_HDRC_SUPPORTS_RUNTIME_PM) |
166 | data->supports_runtime_pm = true; | |
1071055e | 167 | |
e1fd7341 | 168 | ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)); |
22d9d8e8 RK |
169 | if (ret) |
170 | goto err_clk; | |
d142d6be | 171 | |
a4cf1b14 PC |
172 | ret = imx_usbmisc_init(data->usbmisc_data); |
173 | if (ret) { | |
174 | dev_err(&pdev->dev, "usbmisc init failed, ret=%d\n", ret); | |
175 | goto err_clk; | |
d142d6be RZ |
176 | } |
177 | ||
8e22978c | 178 | data->ci_pdev = ci_hdrc_add_device(&pdev->dev, |
15302800 | 179 | pdev->resource, pdev->num_resources, |
f6a3b3a3 | 180 | &pdata); |
770719df FE |
181 | if (IS_ERR(data->ci_pdev)) { |
182 | ret = PTR_ERR(data->ci_pdev); | |
15302800 RZ |
183 | dev_err(&pdev->dev, |
184 | "Can't register ci_hdrc platform device, err=%d\n", | |
185 | ret); | |
af59a8b1 | 186 | goto err_clk; |
15302800 RZ |
187 | } |
188 | ||
a4cf1b14 PC |
189 | ret = imx_usbmisc_init_post(data->usbmisc_data); |
190 | if (ret) { | |
191 | dev_err(&pdev->dev, "usbmisc post failed, ret=%d\n", ret); | |
192 | goto disable_device; | |
a0685330 MG |
193 | } |
194 | ||
15302800 RZ |
195 | platform_set_drvdata(pdev, data); |
196 | ||
e14db48d PC |
197 | if (data->supports_runtime_pm) { |
198 | pm_runtime_set_active(&pdev->dev); | |
199 | pm_runtime_enable(&pdev->dev); | |
200 | } | |
15302800 | 201 | |
6d653110 PC |
202 | device_set_wakeup_capable(&pdev->dev, true); |
203 | ||
15302800 RZ |
204 | return 0; |
205 | ||
770719df | 206 | disable_device: |
8e22978c | 207 | ci_hdrc_remove_device(data->ci_pdev); |
ea1418b5 | 208 | err_clk: |
15302800 RZ |
209 | clk_disable_unprepare(data->clk); |
210 | return ret; | |
211 | } | |
212 | ||
8e22978c | 213 | static int ci_hdrc_imx_remove(struct platform_device *pdev) |
15302800 | 214 | { |
8e22978c | 215 | struct ci_hdrc_imx_data *data = platform_get_drvdata(pdev); |
15302800 | 216 | |
e14db48d PC |
217 | if (data->supports_runtime_pm) { |
218 | pm_runtime_get_sync(&pdev->dev); | |
219 | pm_runtime_disable(&pdev->dev); | |
220 | pm_runtime_put_noidle(&pdev->dev); | |
221 | } | |
8e22978c | 222 | ci_hdrc_remove_device(data->ci_pdev); |
15302800 RZ |
223 | clk_disable_unprepare(data->clk); |
224 | ||
15302800 RZ |
225 | return 0; |
226 | } | |
227 | ||
e14db48d | 228 | #ifdef CONFIG_PM |
2558c1f5 PC |
229 | static int imx_controller_suspend(struct device *dev) |
230 | { | |
231 | struct ci_hdrc_imx_data *data = dev_get_drvdata(dev); | |
232 | ||
233 | dev_dbg(dev, "at %s\n", __func__); | |
234 | ||
235 | clk_disable_unprepare(data->clk); | |
e14db48d | 236 | data->in_lpm = true; |
2558c1f5 PC |
237 | |
238 | return 0; | |
239 | } | |
240 | ||
241 | static int imx_controller_resume(struct device *dev) | |
242 | { | |
243 | struct ci_hdrc_imx_data *data = dev_get_drvdata(dev); | |
e14db48d | 244 | int ret = 0; |
2558c1f5 PC |
245 | |
246 | dev_dbg(dev, "at %s\n", __func__); | |
247 | ||
e14db48d PC |
248 | if (!data->in_lpm) { |
249 | WARN_ON(1); | |
250 | return 0; | |
251 | } | |
252 | ||
253 | ret = clk_prepare_enable(data->clk); | |
254 | if (ret) | |
255 | return ret; | |
256 | ||
257 | data->in_lpm = false; | |
258 | ||
259 | ret = imx_usbmisc_set_wakeup(data->usbmisc_data, false); | |
260 | if (ret) { | |
261 | dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n", ret); | |
262 | goto clk_disable; | |
263 | } | |
264 | ||
265 | return 0; | |
266 | ||
267 | clk_disable: | |
268 | clk_disable_unprepare(data->clk); | |
269 | return ret; | |
2558c1f5 PC |
270 | } |
271 | ||
e14db48d | 272 | #ifdef CONFIG_PM_SLEEP |
2558c1f5 PC |
273 | static int ci_hdrc_imx_suspend(struct device *dev) |
274 | { | |
6d653110 PC |
275 | int ret; |
276 | ||
e14db48d PC |
277 | struct ci_hdrc_imx_data *data = dev_get_drvdata(dev); |
278 | ||
279 | if (data->in_lpm) | |
280 | /* The core's suspend doesn't run */ | |
281 | return 0; | |
282 | ||
6d653110 PC |
283 | if (device_may_wakeup(dev)) { |
284 | ret = imx_usbmisc_set_wakeup(data->usbmisc_data, true); | |
285 | if (ret) { | |
286 | dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n", | |
287 | ret); | |
288 | return ret; | |
289 | } | |
290 | } | |
291 | ||
2558c1f5 PC |
292 | return imx_controller_suspend(dev); |
293 | } | |
294 | ||
295 | static int ci_hdrc_imx_resume(struct device *dev) | |
296 | { | |
e14db48d PC |
297 | struct ci_hdrc_imx_data *data = dev_get_drvdata(dev); |
298 | int ret; | |
299 | ||
300 | ret = imx_controller_resume(dev); | |
301 | if (!ret && data->supports_runtime_pm) { | |
302 | pm_runtime_disable(dev); | |
303 | pm_runtime_set_active(dev); | |
304 | pm_runtime_enable(dev); | |
305 | } | |
306 | ||
307 | return ret; | |
2558c1f5 PC |
308 | } |
309 | #endif /* CONFIG_PM_SLEEP */ | |
310 | ||
e14db48d PC |
311 | static int ci_hdrc_imx_runtime_suspend(struct device *dev) |
312 | { | |
313 | struct ci_hdrc_imx_data *data = dev_get_drvdata(dev); | |
314 | int ret; | |
315 | ||
316 | if (data->in_lpm) { | |
317 | WARN_ON(1); | |
318 | return 0; | |
319 | } | |
320 | ||
321 | ret = imx_usbmisc_set_wakeup(data->usbmisc_data, true); | |
322 | if (ret) { | |
323 | dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n", ret); | |
324 | return ret; | |
325 | } | |
326 | ||
327 | return imx_controller_suspend(dev); | |
328 | } | |
329 | ||
330 | static int ci_hdrc_imx_runtime_resume(struct device *dev) | |
331 | { | |
332 | return imx_controller_resume(dev); | |
333 | } | |
334 | ||
335 | #endif /* CONFIG_PM */ | |
336 | ||
2558c1f5 PC |
337 | static const struct dev_pm_ops ci_hdrc_imx_pm_ops = { |
338 | SET_SYSTEM_SLEEP_PM_OPS(ci_hdrc_imx_suspend, ci_hdrc_imx_resume) | |
e14db48d PC |
339 | SET_RUNTIME_PM_OPS(ci_hdrc_imx_runtime_suspend, |
340 | ci_hdrc_imx_runtime_resume, NULL) | |
2558c1f5 | 341 | }; |
8e22978c AS |
342 | static struct platform_driver ci_hdrc_imx_driver = { |
343 | .probe = ci_hdrc_imx_probe, | |
344 | .remove = ci_hdrc_imx_remove, | |
15302800 RZ |
345 | .driver = { |
346 | .name = "imx_usb", | |
8e22978c | 347 | .of_match_table = ci_hdrc_imx_dt_ids, |
2558c1f5 | 348 | .pm = &ci_hdrc_imx_pm_ops, |
15302800 RZ |
349 | }, |
350 | }; | |
351 | ||
8e22978c | 352 | module_platform_driver(ci_hdrc_imx_driver); |
15302800 RZ |
353 | |
354 | MODULE_ALIAS("platform:imx-usb"); | |
355 | MODULE_LICENSE("GPL v2"); | |
8e22978c | 356 | MODULE_DESCRIPTION("CI HDRC i.MX USB binding"); |
15302800 RZ |
357 | MODULE_AUTHOR("Marek Vasut <marex@denx.de>"); |
358 | MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>"); |