blob: 1951b37aa39dcaec61a7c5d57659998845676bcb [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
Richard Zhao15302802012-07-07 22:56:48 +08002/*
3 * Copyright 2012 Freescale Semiconductor, Inc.
4 * Copyright (C) 2012 Marek Vasut <marex@denx.de>
5 * on behalf of DENX Software Engineering GmbH
Richard Zhao15302802012-07-07 22:56:48 +08006 */
7
8#include <linux/module.h>
9#include <linux/of_platform.h>
10#include <linux/of_gpio.h>
11#include <linux/platform_device.h>
12#include <linux/pm_runtime.h>
13#include <linux/dma-mapping.h>
14#include <linux/usb/chipidea.h>
Fabien Lahoudered13631b2016-09-26 13:14:19 +020015#include <linux/usb/of.h>
Richard Zhao15302802012-07-07 22:56:48 +080016#include <linux/clk.h>
Peter Chen7c8e8902018-10-16 09:17:02 +080017#include <linux/pinctrl/consumer.h>
Richard Zhao15302802012-07-07 22:56:48 +080018
19#include "ci.h"
Alexander Shishkin8e229782013-06-24 14:46:36 +030020#include "ci_hdrc_imx.h"
Richard Zhao15302802012-07-07 22:56:48 +080021
Peter Chen10710552014-01-10 13:51:28 +080022struct ci_hdrc_imx_platform_flag {
23 unsigned int flags;
24};
25
Stefan Wahren81345722015-12-29 16:41:08 +000026static const struct ci_hdrc_imx_platform_flag imx23_usb_data = {
27 .flags = CI_HDRC_TURN_VBUS_EARLY_ON |
28 CI_HDRC_DISABLE_STREAMING,
29};
30
Peter Chen10710552014-01-10 13:51:28 +080031static const struct ci_hdrc_imx_platform_flag imx27_usb_data = {
Sebastian Reichel04e16372018-03-29 17:52:48 +080032 .flags = CI_HDRC_DISABLE_STREAMING,
Peter Chen10710552014-01-10 13:51:28 +080033};
34
35static const struct ci_hdrc_imx_platform_flag imx28_usb_data = {
Li Jun6adb9b7b2015-02-11 12:45:01 +080036 .flags = CI_HDRC_IMX28_WRITE_FIX |
Peter Chen0ef877a2014-10-30 11:10:04 +080037 CI_HDRC_TURN_VBUS_EARLY_ON |
38 CI_HDRC_DISABLE_STREAMING,
Peter Chen10710552014-01-10 13:51:28 +080039};
40
Peter Chene14db482015-02-11 12:44:47 +080041static const struct ci_hdrc_imx_platform_flag imx6q_usb_data = {
Li Jun6adb9b7b2015-02-11 12:45:01 +080042 .flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
Peter Chen0ef877a2014-10-30 11:10:04 +080043 CI_HDRC_TURN_VBUS_EARLY_ON |
44 CI_HDRC_DISABLE_STREAMING,
Peter Chene14db482015-02-11 12:44:47 +080045};
46
47static const struct ci_hdrc_imx_platform_flag imx6sl_usb_data = {
Li Jun6adb9b7b2015-02-11 12:45:01 +080048 .flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
Peter Chen0ef877a2014-10-30 11:10:04 +080049 CI_HDRC_TURN_VBUS_EARLY_ON |
50 CI_HDRC_DISABLE_HOST_STREAMING,
Peter Chene14db482015-02-11 12:44:47 +080051};
52
53static const struct ci_hdrc_imx_platform_flag imx6sx_usb_data = {
Li Jun6adb9b7b2015-02-11 12:45:01 +080054 .flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
Peter Chen0ef877a2014-10-30 11:10:04 +080055 CI_HDRC_TURN_VBUS_EARLY_ON |
56 CI_HDRC_DISABLE_HOST_STREAMING,
Peter Chene14db482015-02-11 12:44:47 +080057};
58
Peter Chen52fe5682015-09-16 15:52:32 +080059static const struct ci_hdrc_imx_platform_flag imx6ul_usb_data = {
60 .flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
61 CI_HDRC_TURN_VBUS_EARLY_ON,
62};
63
Peter Chen5cb377c2015-09-09 16:33:02 +080064static const struct ci_hdrc_imx_platform_flag imx7d_usb_data = {
65 .flags = CI_HDRC_SUPPORTS_RUNTIME_PM,
66};
67
Peter Chen10710552014-01-10 13:51:28 +080068static const struct of_device_id ci_hdrc_imx_dt_ids[] = {
Stefan Wahren81345722015-12-29 16:41:08 +000069 { .compatible = "fsl,imx23-usb", .data = &imx23_usb_data},
Peter Chen10710552014-01-10 13:51:28 +080070 { .compatible = "fsl,imx28-usb", .data = &imx28_usb_data},
71 { .compatible = "fsl,imx27-usb", .data = &imx27_usb_data},
Peter Chene14db482015-02-11 12:44:47 +080072 { .compatible = "fsl,imx6q-usb", .data = &imx6q_usb_data},
73 { .compatible = "fsl,imx6sl-usb", .data = &imx6sl_usb_data},
Li Jun8315b772015-09-16 14:46:32 +080074 { .compatible = "fsl,imx6sx-usb", .data = &imx6sx_usb_data},
Peter Chen52fe5682015-09-16 15:52:32 +080075 { .compatible = "fsl,imx6ul-usb", .data = &imx6ul_usb_data},
Peter Chen5cb377c2015-09-09 16:33:02 +080076 { .compatible = "fsl,imx7d-usb", .data = &imx7d_usb_data},
Peter Chen10710552014-01-10 13:51:28 +080077 { /* sentinel */ }
78};
79MODULE_DEVICE_TABLE(of, ci_hdrc_imx_dt_ids);
80
Alexander Shishkin8e229782013-06-24 14:46:36 +030081struct ci_hdrc_imx_data {
Richard Zhao15302802012-07-07 22:56:48 +080082 struct usb_phy *phy;
83 struct platform_device *ci_pdev;
84 struct clk *clk;
Sascha Hauer05986ba2013-08-14 12:44:16 +030085 struct imx_usbmisc_data *usbmisc_data;
Peter Chene14db482015-02-11 12:44:47 +080086 bool supports_runtime_pm;
Sebastian Reichelbe9cae22018-03-29 17:52:49 +080087 bool override_phy_control;
Peter Chene14db482015-02-11 12:44:47 +080088 bool in_lpm;
Peter Chen7c8e8902018-10-16 09:17:02 +080089 struct pinctrl *pinctrl;
90 struct pinctrl_state *pinctrl_hsic_active;
91 struct regulator *hsic_pad_regulator;
Peter Chenae3e57a2015-09-16 09:40:51 +080092 /* SoC before i.mx6 (except imx23/imx28) needs three clks */
93 bool need_three_clks;
94 struct clk *clk_ipg;
95 struct clk *clk_ahb;
96 struct clk *clk_per;
97 /* --------------------------------- */
Richard Zhao15302802012-07-07 22:56:48 +080098};
99
Richard Zhaod142d6b2012-09-12 14:58:05 +0300100/* Common functions shared by usbmisc drivers */
101
Sascha Hauer05986ba2013-08-14 12:44:16 +0300102static struct imx_usbmisc_data *usbmisc_get_init_data(struct device *dev)
Richard Zhaod142d6b2012-09-12 14:58:05 +0300103{
Stefan Agnerf40017e2014-09-22 08:14:15 +0800104 struct platform_device *misc_pdev;
Richard Zhaod142d6b2012-09-12 14:58:05 +0300105 struct device_node *np = dev->of_node;
106 struct of_phandle_args args;
Sascha Hauer05986ba2013-08-14 12:44:16 +0300107 struct imx_usbmisc_data *data;
Richard Zhaod142d6b2012-09-12 14:58:05 +0300108 int ret;
109
Sascha Hauer05986ba2013-08-14 12:44:16 +0300110 /*
111 * In case the fsl,usbmisc property is not present this device doesn't
112 * need usbmisc. Return NULL (which is no error here)
113 */
114 if (!of_get_property(np, "fsl,usbmisc", NULL))
115 return NULL;
116
117 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
118 if (!data)
119 return ERR_PTR(-ENOMEM);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300120
121 ret = of_parse_phandle_with_args(np, "fsl,usbmisc", "#index-cells",
122 0, &args);
123 if (ret) {
124 dev_err(dev, "Failed to parse property fsl,usbmisc, errno %d\n",
125 ret);
Sascha Hauer05986ba2013-08-14 12:44:16 +0300126 return ERR_PTR(ret);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300127 }
Sascha Hauer05986ba2013-08-14 12:44:16 +0300128
129 data->index = args.args[0];
Stefan Agnerf40017e2014-09-22 08:14:15 +0800130
131 misc_pdev = of_find_device_by_node(args.np);
Richard Zhaod142d6b2012-09-12 14:58:05 +0300132 of_node_put(args.np);
133
Tomeu Vizosoef12da92015-08-06 15:09:54 +0200134 if (!misc_pdev || !platform_get_drvdata(misc_pdev))
Stefan Agnerf40017e2014-09-22 08:14:15 +0800135 return ERR_PTR(-EPROBE_DEFER);
136
137 data->dev = &misc_pdev->dev;
138
Uwe Kleine-Königa82bf692018-12-04 09:31:29 +0100139 /*
140 * Check the various over current related properties. If over current
141 * detection is disabled we're not interested in the polarity.
142 */
143 if (of_find_property(np, "disable-over-current", NULL)) {
Sascha Hauer05986ba2013-08-14 12:44:16 +0300144 data->disable_oc = 1;
Uwe Kleine-Königa82bf692018-12-04 09:31:29 +0100145 } else if (of_find_property(np, "over-current-active-high", NULL)) {
146 data->oc_pol_active_low = 0;
147 data->oc_pol_configured = 1;
148 } else if (of_find_property(np, "over-current-active-low", NULL)) {
149 data->oc_pol_active_low = 1;
150 data->oc_pol_configured = 1;
151 }
Li Jun9dba5162016-07-20 16:02:42 +0800152
Michael Grzeschika0685332013-03-30 12:54:01 +0200153 if (of_find_property(np, "external-vbus-divider", NULL))
Sascha Hauer05986ba2013-08-14 12:44:16 +0300154 data->evdo = 1;
Michael Grzeschika0685332013-03-30 12:54:01 +0200155
Fabien Lahoudered13631b2016-09-26 13:14:19 +0200156 if (of_usb_get_phy_mode(np) == USBPHY_INTERFACE_MODE_ULPI)
157 data->ulpi = 1;
158
Sascha Hauer05986ba2013-08-14 12:44:16 +0300159 return data;
Richard Zhaod142d6b2012-09-12 14:58:05 +0300160}
Richard Zhaod142d6b2012-09-12 14:58:05 +0300161
162/* End of common functions shared by usbmisc drivers*/
Peter Chenae3e57a2015-09-16 09:40:51 +0800163static int imx_get_clks(struct device *dev)
164{
165 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
166 int ret = 0;
167
168 data->clk_ipg = devm_clk_get(dev, "ipg");
169 if (IS_ERR(data->clk_ipg)) {
170 /* If the platform only needs one clocks */
171 data->clk = devm_clk_get(dev, NULL);
172 if (IS_ERR(data->clk)) {
173 ret = PTR_ERR(data->clk);
174 dev_err(dev,
175 "Failed to get clks, err=%ld,%ld\n",
176 PTR_ERR(data->clk), PTR_ERR(data->clk_ipg));
177 return ret;
178 }
179 return ret;
180 }
181
182 data->clk_ahb = devm_clk_get(dev, "ahb");
183 if (IS_ERR(data->clk_ahb)) {
184 ret = PTR_ERR(data->clk_ahb);
185 dev_err(dev,
186 "Failed to get ahb clock, err=%d\n", ret);
187 return ret;
188 }
189
190 data->clk_per = devm_clk_get(dev, "per");
191 if (IS_ERR(data->clk_per)) {
192 ret = PTR_ERR(data->clk_per);
193 dev_err(dev,
194 "Failed to get per clock, err=%d\n", ret);
195 return ret;
196 }
197
198 data->need_three_clks = true;
199 return ret;
200}
201
202static int imx_prepare_enable_clks(struct device *dev)
203{
204 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
205 int ret = 0;
206
207 if (data->need_three_clks) {
208 ret = clk_prepare_enable(data->clk_ipg);
209 if (ret) {
210 dev_err(dev,
211 "Failed to prepare/enable ipg clk, err=%d\n",
212 ret);
213 return ret;
214 }
215
216 ret = clk_prepare_enable(data->clk_ahb);
217 if (ret) {
218 dev_err(dev,
219 "Failed to prepare/enable ahb clk, err=%d\n",
220 ret);
221 clk_disable_unprepare(data->clk_ipg);
222 return ret;
223 }
224
225 ret = clk_prepare_enable(data->clk_per);
226 if (ret) {
227 dev_err(dev,
228 "Failed to prepare/enable per clk, err=%d\n",
229 ret);
230 clk_disable_unprepare(data->clk_ahb);
231 clk_disable_unprepare(data->clk_ipg);
232 return ret;
233 }
234 } else {
235 ret = clk_prepare_enable(data->clk);
236 if (ret) {
237 dev_err(dev,
238 "Failed to prepare/enable clk, err=%d\n",
239 ret);
240 return ret;
241 }
242 }
243
244 return ret;
245}
246
247static void imx_disable_unprepare_clks(struct device *dev)
248{
249 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
250
251 if (data->need_three_clks) {
252 clk_disable_unprepare(data->clk_per);
253 clk_disable_unprepare(data->clk_ahb);
254 clk_disable_unprepare(data->clk_ipg);
255 } else {
256 clk_disable_unprepare(data->clk);
257 }
258}
Richard Zhaod142d6b2012-09-12 14:58:05 +0300259
Peter Chen7c8e8902018-10-16 09:17:02 +0800260static int ci_hdrc_imx_notify_event(struct ci_hdrc *ci, unsigned int event)
261{
262 struct device *dev = ci->dev->parent;
263 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
264 int ret = 0;
265
266 switch (event) {
267 case CI_HDRC_IMX_HSIC_ACTIVE_EVENT:
268 ret = pinctrl_select_state(data->pinctrl,
269 data->pinctrl_hsic_active);
270 if (ret)
271 dev_err(dev, "hsic_active select failed, err=%d\n",
272 ret);
273 break;
274 case CI_HDRC_IMX_HSIC_SUSPEND_EVENT:
275 ret = imx_usbmisc_hsic_set_connect(data->usbmisc_data);
276 if (ret)
277 dev_err(dev,
278 "hsic_set_connect failed, err=%d\n", ret);
279 break;
280 default:
281 break;
282 }
283
284 return ret;
285}
286
Alexander Shishkin8e229782013-06-24 14:46:36 +0300287static int ci_hdrc_imx_probe(struct platform_device *pdev)
Richard Zhao15302802012-07-07 22:56:48 +0800288{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300289 struct ci_hdrc_imx_data *data;
290 struct ci_hdrc_platform_data pdata = {
Alexander Shiyanc844d6c2014-03-11 13:47:38 +0800291 .name = dev_name(&pdev->dev),
Michael Grzeschikf6a3b3a2013-06-13 17:59:58 +0300292 .capoffset = DEF_CAPOFFSET,
Peter Chen7c8e8902018-10-16 09:17:02 +0800293 .notify_event = ci_hdrc_imx_notify_event,
Michael Grzeschikf6a3b3a2013-06-13 17:59:58 +0300294 };
Richard Zhao15302802012-07-07 22:56:48 +0800295 int ret;
LABBE Corentin6f51bc32015-11-12 08:43:34 +0100296 const struct of_device_id *of_id;
297 const struct ci_hdrc_imx_platform_flag *imx_platform_flag;
Sebastian Reichelbe9cae22018-03-29 17:52:49 +0800298 struct device_node *np = pdev->dev.of_node;
Peter Chen7c8e8902018-10-16 09:17:02 +0800299 struct device *dev = &pdev->dev;
300 struct pinctrl_state *pinctrl_hsic_idle;
LABBE Corentin6f51bc32015-11-12 08:43:34 +0100301
Peter Chen7c8e8902018-10-16 09:17:02 +0800302 of_id = of_match_device(ci_hdrc_imx_dt_ids, dev);
LABBE Corentin6f51bc32015-11-12 08:43:34 +0100303 if (!of_id)
304 return -ENODEV;
305
306 imx_platform_flag = of_id->data;
Richard Zhao15302802012-07-07 22:56:48 +0800307
308 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
Fabio Estevam735298282014-11-26 13:44:24 +0800309 if (!data)
Richard Zhao15302802012-07-07 22:56:48 +0800310 return -ENOMEM;
Richard Zhao15302802012-07-07 22:56:48 +0800311
Peter Chenae3e57a2015-09-16 09:40:51 +0800312 platform_set_drvdata(pdev, data);
Peter Chen7c8e8902018-10-16 09:17:02 +0800313 data->usbmisc_data = usbmisc_get_init_data(dev);
Sascha Hauer05986ba2013-08-14 12:44:16 +0300314 if (IS_ERR(data->usbmisc_data))
315 return PTR_ERR(data->usbmisc_data);
316
Peter Chen7c8e8902018-10-16 09:17:02 +0800317 if (of_usb_get_phy_mode(dev->of_node) == USBPHY_INTERFACE_MODE_HSIC) {
318 pdata.flags |= CI_HDRC_IMX_IS_HSIC;
319 data->usbmisc_data->hsic = 1;
320 data->pinctrl = devm_pinctrl_get(dev);
321 if (IS_ERR(data->pinctrl)) {
322 dev_err(dev, "pinctrl get failed, err=%ld\n",
323 PTR_ERR(data->pinctrl));
324 return PTR_ERR(data->pinctrl);
325 }
Peter Chenae3e57a2015-09-16 09:40:51 +0800326
Peter Chen7c8e8902018-10-16 09:17:02 +0800327 pinctrl_hsic_idle = pinctrl_lookup_state(data->pinctrl, "idle");
328 if (IS_ERR(pinctrl_hsic_idle)) {
329 dev_err(dev,
330 "pinctrl_hsic_idle lookup failed, err=%ld\n",
331 PTR_ERR(pinctrl_hsic_idle));
332 return PTR_ERR(pinctrl_hsic_idle);
333 }
Richard Zhao15302802012-07-07 22:56:48 +0800334
Peter Chen7c8e8902018-10-16 09:17:02 +0800335 ret = pinctrl_select_state(data->pinctrl, pinctrl_hsic_idle);
336 if (ret) {
337 dev_err(dev, "hsic_idle select failed, err=%d\n", ret);
338 return ret;
339 }
340
341 data->pinctrl_hsic_active = pinctrl_lookup_state(data->pinctrl,
342 "active");
343 if (IS_ERR(data->pinctrl_hsic_active)) {
344 dev_err(dev,
345 "pinctrl_hsic_active lookup failed, err=%ld\n",
346 PTR_ERR(data->pinctrl_hsic_active));
347 return PTR_ERR(data->pinctrl_hsic_active);
348 }
349
350 data->hsic_pad_regulator = devm_regulator_get(dev, "hsic");
351 if (PTR_ERR(data->hsic_pad_regulator) == -EPROBE_DEFER) {
352 return -EPROBE_DEFER;
353 } else if (PTR_ERR(data->hsic_pad_regulator) == -ENODEV) {
354 /* no pad regualator is needed */
355 data->hsic_pad_regulator = NULL;
356 } else if (IS_ERR(data->hsic_pad_regulator)) {
357 dev_err(dev, "Get HSIC pad regulator error: %ld\n",
358 PTR_ERR(data->hsic_pad_regulator));
359 return PTR_ERR(data->hsic_pad_regulator);
360 }
361
362 if (data->hsic_pad_regulator) {
363 ret = regulator_enable(data->hsic_pad_regulator);
364 if (ret) {
365 dev_err(dev,
366 "Failed to enable HSIC pad regulator\n");
367 return ret;
368 }
369 }
370 }
371 ret = imx_get_clks(dev);
372 if (ret)
373 goto disable_hsic_regulator;
374
375 ret = imx_prepare_enable_clks(dev);
376 if (ret)
377 goto disable_hsic_regulator;
378
379 data->phy = devm_usb_get_phy_by_phandle(dev, "fsl,usbphy", 0);
Peter Chenaf59a8b2013-09-24 12:47:54 +0800380 if (IS_ERR(data->phy)) {
381 ret = PTR_ERR(data->phy);
Markus Pargmann16853d72014-07-22 10:09:43 +0800382 /* Return -EINVAL if no usbphy is available */
383 if (ret == -ENODEV)
384 ret = -EINVAL;
Sascha Hauerea1418b2013-06-13 18:00:00 +0300385 goto err_clk;
Richard Zhao15302802012-07-07 22:56:48 +0800386 }
387
Antoine Tenartef44cb42014-10-30 18:41:16 +0100388 pdata.usb_phy = data->phy;
Sebastian Reichelbe9cae22018-03-29 17:52:49 +0800389
Andrey Smirnov03e62752018-05-30 10:34:14 -0700390 if ((of_device_is_compatible(np, "fsl,imx53-usb") ||
391 of_device_is_compatible(np, "fsl,imx51-usb")) && pdata.usb_phy &&
Sebastian Reichelbe9cae22018-03-29 17:52:49 +0800392 of_usb_get_phy_mode(np) == USBPHY_INTERFACE_MODE_ULPI) {
393 pdata.flags |= CI_HDRC_OVERRIDE_PHY_CONTROL;
394 data->override_phy_control = true;
395 usb_phy_init(pdata.usb_phy);
396 }
397
Peter Chen56040082015-02-11 12:44:42 +0800398 pdata.flags |= imx_platform_flag->flags;
Peter Chene14db482015-02-11 12:44:47 +0800399 if (pdata.flags & CI_HDRC_SUPPORTS_RUNTIME_PM)
400 data->supports_runtime_pm = true;
Peter Chen10710552014-01-10 13:51:28 +0800401
Peter Chena4cf1b12015-02-11 12:44:44 +0800402 ret = imx_usbmisc_init(data->usbmisc_data);
403 if (ret) {
Peter Chen7c8e8902018-10-16 09:17:02 +0800404 dev_err(dev, "usbmisc init failed, ret=%d\n", ret);
Peter Chena4cf1b12015-02-11 12:44:44 +0800405 goto err_clk;
Richard Zhaod142d6b2012-09-12 14:58:05 +0300406 }
407
Peter Chen7c8e8902018-10-16 09:17:02 +0800408 data->ci_pdev = ci_hdrc_add_device(dev,
Richard Zhao15302802012-07-07 22:56:48 +0800409 pdev->resource, pdev->num_resources,
Michael Grzeschikf6a3b3a2013-06-13 17:59:58 +0300410 &pdata);
Fabio Estevam770719d2013-06-13 17:59:48 +0300411 if (IS_ERR(data->ci_pdev)) {
412 ret = PTR_ERR(data->ci_pdev);
Stefan Agnerd3d84252016-01-27 16:55:45 -0800413 if (ret != -EPROBE_DEFER)
Peter Chen7c8e8902018-10-16 09:17:02 +0800414 dev_err(dev, "ci_hdrc_add_device failed, err=%d\n",
415 ret);
Peter Chenaf59a8b2013-09-24 12:47:54 +0800416 goto err_clk;
Richard Zhao15302802012-07-07 22:56:48 +0800417 }
418
Peter Chena4cf1b12015-02-11 12:44:44 +0800419 ret = imx_usbmisc_init_post(data->usbmisc_data);
420 if (ret) {
Peter Chen7c8e8902018-10-16 09:17:02 +0800421 dev_err(dev, "usbmisc post failed, ret=%d\n", ret);
Peter Chena4cf1b12015-02-11 12:44:44 +0800422 goto disable_device;
Michael Grzeschika0685332013-03-30 12:54:01 +0200423 }
424
Peter Chene14db482015-02-11 12:44:47 +0800425 if (data->supports_runtime_pm) {
Peter Chen7c8e8902018-10-16 09:17:02 +0800426 pm_runtime_set_active(dev);
427 pm_runtime_enable(dev);
Peter Chene14db482015-02-11 12:44:47 +0800428 }
Richard Zhao15302802012-07-07 22:56:48 +0800429
Peter Chen7c8e8902018-10-16 09:17:02 +0800430 device_set_wakeup_capable(dev, true);
Peter Chen6d653112015-02-11 12:44:49 +0800431
Richard Zhao15302802012-07-07 22:56:48 +0800432 return 0;
433
Fabio Estevam770719d2013-06-13 17:59:48 +0300434disable_device:
Alexander Shishkin8e229782013-06-24 14:46:36 +0300435 ci_hdrc_remove_device(data->ci_pdev);
Sascha Hauerea1418b2013-06-13 18:00:00 +0300436err_clk:
Peter Chen7c8e8902018-10-16 09:17:02 +0800437 imx_disable_unprepare_clks(dev);
438disable_hsic_regulator:
439 if (data->hsic_pad_regulator)
440 ret = regulator_disable(data->hsic_pad_regulator);
Richard Zhao15302802012-07-07 22:56:48 +0800441 return ret;
442}
443
Alexander Shishkin8e229782013-06-24 14:46:36 +0300444static int ci_hdrc_imx_remove(struct platform_device *pdev)
Richard Zhao15302802012-07-07 22:56:48 +0800445{
Alexander Shishkin8e229782013-06-24 14:46:36 +0300446 struct ci_hdrc_imx_data *data = platform_get_drvdata(pdev);
Richard Zhao15302802012-07-07 22:56:48 +0800447
Peter Chene14db482015-02-11 12:44:47 +0800448 if (data->supports_runtime_pm) {
449 pm_runtime_get_sync(&pdev->dev);
450 pm_runtime_disable(&pdev->dev);
451 pm_runtime_put_noidle(&pdev->dev);
452 }
Alexander Shishkin8e229782013-06-24 14:46:36 +0300453 ci_hdrc_remove_device(data->ci_pdev);
Sebastian Reichelbe9cae22018-03-29 17:52:49 +0800454 if (data->override_phy_control)
455 usb_phy_shutdown(data->phy);
Peter Chenae3e57a2015-09-16 09:40:51 +0800456 imx_disable_unprepare_clks(&pdev->dev);
Peter Chen7c8e8902018-10-16 09:17:02 +0800457 if (data->hsic_pad_regulator)
458 regulator_disable(data->hsic_pad_regulator);
Richard Zhao15302802012-07-07 22:56:48 +0800459
Richard Zhao15302802012-07-07 22:56:48 +0800460 return 0;
461}
462
Andreas Fenkartb09b5222015-11-12 14:14:40 +0100463static void ci_hdrc_imx_shutdown(struct platform_device *pdev)
464{
465 ci_hdrc_imx_remove(pdev);
466}
467
Marcus Folkesson9f644a62018-09-02 19:37:04 +0200468static int __maybe_unused imx_controller_suspend(struct device *dev)
Peter Chen2558c1f2014-11-26 13:44:30 +0800469{
470 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
Peter Chen7c8e8902018-10-16 09:17:02 +0800471 int ret = 0;
Peter Chen2558c1f2014-11-26 13:44:30 +0800472
473 dev_dbg(dev, "at %s\n", __func__);
474
Peter Chen7c8e8902018-10-16 09:17:02 +0800475 ret = imx_usbmisc_hsic_set_clk(data->usbmisc_data, false);
476 if (ret) {
477 dev_err(dev, "usbmisc hsic_set_clk failed, ret=%d\n", ret);
478 return ret;
479 }
480
Peter Chenae3e57a2015-09-16 09:40:51 +0800481 imx_disable_unprepare_clks(dev);
Peter Chene14db482015-02-11 12:44:47 +0800482 data->in_lpm = true;
Peter Chen2558c1f2014-11-26 13:44:30 +0800483
484 return 0;
485}
486
Marcus Folkesson9f644a62018-09-02 19:37:04 +0200487static int __maybe_unused imx_controller_resume(struct device *dev)
Peter Chen2558c1f2014-11-26 13:44:30 +0800488{
489 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
Peter Chene14db482015-02-11 12:44:47 +0800490 int ret = 0;
Peter Chen2558c1f2014-11-26 13:44:30 +0800491
492 dev_dbg(dev, "at %s\n", __func__);
493
Peter Chene14db482015-02-11 12:44:47 +0800494 if (!data->in_lpm) {
495 WARN_ON(1);
496 return 0;
497 }
498
Peter Chenae3e57a2015-09-16 09:40:51 +0800499 ret = imx_prepare_enable_clks(dev);
Peter Chene14db482015-02-11 12:44:47 +0800500 if (ret)
501 return ret;
502
503 data->in_lpm = false;
504
505 ret = imx_usbmisc_set_wakeup(data->usbmisc_data, false);
506 if (ret) {
507 dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n", ret);
508 goto clk_disable;
509 }
510
Peter Chen7c8e8902018-10-16 09:17:02 +0800511 ret = imx_usbmisc_hsic_set_clk(data->usbmisc_data, true);
512 if (ret) {
513 dev_err(dev, "usbmisc hsic_set_clk failed, ret=%d\n", ret);
514 goto hsic_set_clk_fail;
515 }
516
Peter Chene14db482015-02-11 12:44:47 +0800517 return 0;
518
Peter Chen7c8e8902018-10-16 09:17:02 +0800519hsic_set_clk_fail:
520 imx_usbmisc_set_wakeup(data->usbmisc_data, true);
Peter Chene14db482015-02-11 12:44:47 +0800521clk_disable:
Peter Chenae3e57a2015-09-16 09:40:51 +0800522 imx_disable_unprepare_clks(dev);
Peter Chene14db482015-02-11 12:44:47 +0800523 return ret;
Peter Chen2558c1f2014-11-26 13:44:30 +0800524}
525
Marcus Folkesson9f644a62018-09-02 19:37:04 +0200526static int __maybe_unused ci_hdrc_imx_suspend(struct device *dev)
Peter Chen2558c1f2014-11-26 13:44:30 +0800527{
Peter Chen6d653112015-02-11 12:44:49 +0800528 int ret;
529
Peter Chene14db482015-02-11 12:44:47 +0800530 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
531
532 if (data->in_lpm)
533 /* The core's suspend doesn't run */
534 return 0;
535
Peter Chen6d653112015-02-11 12:44:49 +0800536 if (device_may_wakeup(dev)) {
537 ret = imx_usbmisc_set_wakeup(data->usbmisc_data, true);
538 if (ret) {
539 dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n",
540 ret);
541 return ret;
542 }
543 }
544
Peter Chen2558c1f2014-11-26 13:44:30 +0800545 return imx_controller_suspend(dev);
546}
547
Marcus Folkesson9f644a62018-09-02 19:37:04 +0200548static int __maybe_unused ci_hdrc_imx_resume(struct device *dev)
Peter Chen2558c1f2014-11-26 13:44:30 +0800549{
Peter Chene14db482015-02-11 12:44:47 +0800550 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
551 int ret;
552
553 ret = imx_controller_resume(dev);
554 if (!ret && data->supports_runtime_pm) {
555 pm_runtime_disable(dev);
556 pm_runtime_set_active(dev);
557 pm_runtime_enable(dev);
558 }
559
560 return ret;
Peter Chen2558c1f2014-11-26 13:44:30 +0800561}
Peter Chen2558c1f2014-11-26 13:44:30 +0800562
Marcus Folkesson9f644a62018-09-02 19:37:04 +0200563static int __maybe_unused ci_hdrc_imx_runtime_suspend(struct device *dev)
Peter Chene14db482015-02-11 12:44:47 +0800564{
565 struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
566 int ret;
567
568 if (data->in_lpm) {
569 WARN_ON(1);
570 return 0;
571 }
572
573 ret = imx_usbmisc_set_wakeup(data->usbmisc_data, true);
574 if (ret) {
575 dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n", ret);
576 return ret;
577 }
578
579 return imx_controller_suspend(dev);
580}
581
Marcus Folkesson9f644a62018-09-02 19:37:04 +0200582static int __maybe_unused ci_hdrc_imx_runtime_resume(struct device *dev)
Peter Chene14db482015-02-11 12:44:47 +0800583{
584 return imx_controller_resume(dev);
585}
586
Peter Chen2558c1f2014-11-26 13:44:30 +0800587static const struct dev_pm_ops ci_hdrc_imx_pm_ops = {
588 SET_SYSTEM_SLEEP_PM_OPS(ci_hdrc_imx_suspend, ci_hdrc_imx_resume)
Peter Chene14db482015-02-11 12:44:47 +0800589 SET_RUNTIME_PM_OPS(ci_hdrc_imx_runtime_suspend,
590 ci_hdrc_imx_runtime_resume, NULL)
Peter Chen2558c1f2014-11-26 13:44:30 +0800591};
Alexander Shishkin8e229782013-06-24 14:46:36 +0300592static struct platform_driver ci_hdrc_imx_driver = {
593 .probe = ci_hdrc_imx_probe,
594 .remove = ci_hdrc_imx_remove,
Andreas Fenkartb09b5222015-11-12 14:14:40 +0100595 .shutdown = ci_hdrc_imx_shutdown,
Richard Zhao15302802012-07-07 22:56:48 +0800596 .driver = {
597 .name = "imx_usb",
Alexander Shishkin8e229782013-06-24 14:46:36 +0300598 .of_match_table = ci_hdrc_imx_dt_ids,
Peter Chen2558c1f2014-11-26 13:44:30 +0800599 .pm = &ci_hdrc_imx_pm_ops,
Richard Zhao15302802012-07-07 22:56:48 +0800600 },
601};
602
Alexander Shishkin8e229782013-06-24 14:46:36 +0300603module_platform_driver(ci_hdrc_imx_driver);
Richard Zhao15302802012-07-07 22:56:48 +0800604
605MODULE_ALIAS("platform:imx-usb");
Marcus Folkesson1f060722018-09-02 19:36:50 +0200606MODULE_LICENSE("GPL");
Alexander Shishkin8e229782013-06-24 14:46:36 +0300607MODULE_DESCRIPTION("CI HDRC i.MX USB binding");
Richard Zhao15302802012-07-07 22:56:48 +0800608MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
609MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");