xref: /linux/drivers/usb/chipidea/ci_hdrc_imx.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2012 Freescale Semiconductor, Inc.
4  * Copyright (C) 2012 Marek Vasut <marex@denx.de>
5  * on behalf of DENX Software Engineering GmbH
6  */
7 
8 #include <linux/module.h>
9 #include <linux/of.h>
10 #include <linux/of_platform.h>
11 #include <linux/platform_device.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/usb/chipidea.h>
14 #include <linux/usb/of.h>
15 #include <linux/clk.h>
16 #include <linux/pinctrl/consumer.h>
17 #include <linux/pm_qos.h>
18 
19 #include "ci.h"
20 #include "ci_hdrc_imx.h"
21 
22 struct ci_hdrc_imx_platform_flag {
23 	unsigned int flags;
24 };
25 
26 static 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 
31 static const struct ci_hdrc_imx_platform_flag imx27_usb_data = {
32 	.flags = CI_HDRC_DISABLE_STREAMING,
33 };
34 
35 static const struct ci_hdrc_imx_platform_flag imx28_usb_data = {
36 	.flags = CI_HDRC_IMX28_WRITE_FIX |
37 		CI_HDRC_TURN_VBUS_EARLY_ON |
38 		CI_HDRC_DISABLE_STREAMING,
39 };
40 
41 static const struct ci_hdrc_imx_platform_flag imx6q_usb_data = {
42 	.flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
43 		CI_HDRC_TURN_VBUS_EARLY_ON |
44 		CI_HDRC_DISABLE_STREAMING,
45 };
46 
47 static const struct ci_hdrc_imx_platform_flag imx6sl_usb_data = {
48 	.flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
49 		CI_HDRC_TURN_VBUS_EARLY_ON |
50 		CI_HDRC_DISABLE_HOST_STREAMING,
51 };
52 
53 static const struct ci_hdrc_imx_platform_flag imx6sx_usb_data = {
54 	.flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
55 		CI_HDRC_TURN_VBUS_EARLY_ON |
56 		CI_HDRC_DISABLE_HOST_STREAMING,
57 };
58 
59 static 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 		CI_HDRC_DISABLE_DEVICE_STREAMING,
63 };
64 
65 static const struct ci_hdrc_imx_platform_flag imx7d_usb_data = {
66 	.flags = CI_HDRC_SUPPORTS_RUNTIME_PM,
67 };
68 
69 static const struct ci_hdrc_imx_platform_flag imx7ulp_usb_data = {
70 	.flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
71 		CI_HDRC_HAS_PORTSC_PEC_MISSED |
72 		CI_HDRC_PMQOS,
73 };
74 
75 static const struct ci_hdrc_imx_platform_flag imx8ulp_usb_data = {
76 	.flags = CI_HDRC_SUPPORTS_RUNTIME_PM |
77 		CI_HDRC_HAS_PORTSC_PEC_MISSED,
78 };
79 
80 static const struct of_device_id ci_hdrc_imx_dt_ids[] = {
81 	{ .compatible = "fsl,imx23-usb", .data = &imx23_usb_data},
82 	{ .compatible = "fsl,imx28-usb", .data = &imx28_usb_data},
83 	{ .compatible = "fsl,imx27-usb", .data = &imx27_usb_data},
84 	{ .compatible = "fsl,imx6q-usb", .data = &imx6q_usb_data},
85 	{ .compatible = "fsl,imx6sl-usb", .data = &imx6sl_usb_data},
86 	{ .compatible = "fsl,imx6sx-usb", .data = &imx6sx_usb_data},
87 	{ .compatible = "fsl,imx6ul-usb", .data = &imx6ul_usb_data},
88 	{ .compatible = "fsl,imx7d-usb", .data = &imx7d_usb_data},
89 	{ .compatible = "fsl,imx7ulp-usb", .data = &imx7ulp_usb_data},
90 	{ .compatible = "fsl,imx8ulp-usb", .data = &imx8ulp_usb_data},
91 	{ /* sentinel */ }
92 };
93 MODULE_DEVICE_TABLE(of, ci_hdrc_imx_dt_ids);
94 
95 struct ci_hdrc_imx_data {
96 	struct usb_phy *phy;
97 	struct platform_device *ci_pdev;
98 	struct clk *clk;
99 	struct clk *clk_wakeup;
100 	struct imx_usbmisc_data *usbmisc_data;
101 	bool supports_runtime_pm;
102 	bool override_phy_control;
103 	bool in_lpm;
104 	struct pinctrl *pinctrl;
105 	struct pinctrl_state *pinctrl_hsic_active;
106 	struct regulator *hsic_pad_regulator;
107 	/* SoC before i.mx6 (except imx23/imx28) needs three clks */
108 	bool need_three_clks;
109 	struct clk *clk_ipg;
110 	struct clk *clk_ahb;
111 	struct clk *clk_per;
112 	/* --------------------------------- */
113 	struct pm_qos_request pm_qos_req;
114 	const struct ci_hdrc_imx_platform_flag *plat_data;
115 };
116 
117 /* Common functions shared by usbmisc drivers */
118 
usbmisc_get_init_data(struct device * dev)119 static struct imx_usbmisc_data *usbmisc_get_init_data(struct device *dev)
120 {
121 	struct platform_device *misc_pdev;
122 	struct device_node *np = dev->of_node;
123 	struct of_phandle_args args;
124 	struct imx_usbmisc_data *data;
125 	int ret;
126 
127 	/*
128 	 * In case the fsl,usbmisc property is not present this device doesn't
129 	 * need usbmisc. Return NULL (which is no error here)
130 	 */
131 	if (!of_property_present(np, "fsl,usbmisc"))
132 		return NULL;
133 
134 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
135 	if (!data)
136 		return ERR_PTR(-ENOMEM);
137 
138 	ret = of_parse_phandle_with_args(np, "fsl,usbmisc", "#index-cells",
139 					0, &args);
140 	if (ret) {
141 		dev_err(dev, "Failed to parse property fsl,usbmisc, errno %d\n",
142 			ret);
143 		return ERR_PTR(ret);
144 	}
145 
146 	data->index = args.args[0];
147 
148 	misc_pdev = of_find_device_by_node(args.np);
149 	of_node_put(args.np);
150 
151 	if (!misc_pdev)
152 		return ERR_PTR(-EPROBE_DEFER);
153 
154 	if (!platform_get_drvdata(misc_pdev)) {
155 		put_device(&misc_pdev->dev);
156 		return ERR_PTR(-EPROBE_DEFER);
157 	}
158 	data->dev = &misc_pdev->dev;
159 
160 	/*
161 	 * Check the various over current related properties. If over current
162 	 * detection is disabled we're not interested in the polarity.
163 	 */
164 	if (of_property_read_bool(np, "disable-over-current")) {
165 		data->disable_oc = 1;
166 	} else if (of_property_read_bool(np, "over-current-active-high")) {
167 		data->oc_pol_active_low = 0;
168 		data->oc_pol_configured = 1;
169 	} else if (of_property_read_bool(np, "over-current-active-low")) {
170 		data->oc_pol_active_low = 1;
171 		data->oc_pol_configured = 1;
172 	} else {
173 		dev_warn(dev, "No over current polarity defined\n");
174 	}
175 
176 	data->pwr_pol = of_property_read_bool(np, "power-active-high");
177 	data->evdo = of_property_read_bool(np, "external-vbus-divider");
178 
179 	if (of_usb_get_phy_mode(np) == USBPHY_INTERFACE_MODE_ULPI)
180 		data->ulpi = 1;
181 
182 	if (of_property_read_u32(np, "samsung,picophy-pre-emp-curr-control",
183 			&data->emp_curr_control))
184 		data->emp_curr_control = -1;
185 	if (of_property_read_u32(np, "samsung,picophy-dc-vol-level-adjust",
186 			&data->dc_vol_level_adjust))
187 		data->dc_vol_level_adjust = -1;
188 	if (of_property_read_u32(np, "fsl,picophy-rise-fall-time-adjust",
189 			&data->rise_fall_time_adjust))
190 		data->rise_fall_time_adjust = -1;
191 
192 	return data;
193 }
194 
195 /* End of common functions shared by usbmisc drivers*/
imx_get_clks(struct device * dev)196 static int imx_get_clks(struct device *dev)
197 {
198 	struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
199 	int ret = 0;
200 
201 	data->clk_ipg = devm_clk_get(dev, "ipg");
202 	if (IS_ERR(data->clk_ipg)) {
203 		/* If the platform only needs one primary clock */
204 		data->clk = devm_clk_get(dev, NULL);
205 		if (IS_ERR(data->clk)) {
206 			ret = PTR_ERR(data->clk);
207 			dev_err(dev,
208 				"Failed to get clks, err=%ld,%ld\n",
209 				PTR_ERR(data->clk), PTR_ERR(data->clk_ipg));
210 			return ret;
211 		}
212 		/* Get wakeup clock. Not all of the platforms need to
213 		 * handle this clock. So make it optional.
214 		 */
215 		data->clk_wakeup = devm_clk_get_optional(dev, "usb_wakeup");
216 		if (IS_ERR(data->clk_wakeup))
217 			ret = dev_err_probe(dev, PTR_ERR(data->clk_wakeup),
218 					"Failed to get wakeup clk\n");
219 		return ret;
220 	}
221 
222 	data->clk_ahb = devm_clk_get(dev, "ahb");
223 	if (IS_ERR(data->clk_ahb)) {
224 		ret = PTR_ERR(data->clk_ahb);
225 		dev_err(dev,
226 			"Failed to get ahb clock, err=%d\n", ret);
227 		return ret;
228 	}
229 
230 	data->clk_per = devm_clk_get(dev, "per");
231 	if (IS_ERR(data->clk_per)) {
232 		ret = PTR_ERR(data->clk_per);
233 		dev_err(dev,
234 			"Failed to get per clock, err=%d\n", ret);
235 		return ret;
236 	}
237 
238 	data->need_three_clks = true;
239 	return ret;
240 }
241 
imx_prepare_enable_clks(struct device * dev)242 static int imx_prepare_enable_clks(struct device *dev)
243 {
244 	struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
245 	int ret = 0;
246 
247 	if (data->need_three_clks) {
248 		ret = clk_prepare_enable(data->clk_ipg);
249 		if (ret) {
250 			dev_err(dev,
251 				"Failed to prepare/enable ipg clk, err=%d\n",
252 				ret);
253 			return ret;
254 		}
255 
256 		ret = clk_prepare_enable(data->clk_ahb);
257 		if (ret) {
258 			dev_err(dev,
259 				"Failed to prepare/enable ahb clk, err=%d\n",
260 				ret);
261 			clk_disable_unprepare(data->clk_ipg);
262 			return ret;
263 		}
264 
265 		ret = clk_prepare_enable(data->clk_per);
266 		if (ret) {
267 			dev_err(dev,
268 				"Failed to prepare/enable per clk, err=%d\n",
269 				ret);
270 			clk_disable_unprepare(data->clk_ahb);
271 			clk_disable_unprepare(data->clk_ipg);
272 			return ret;
273 		}
274 	} else {
275 		ret = clk_prepare_enable(data->clk);
276 		if (ret) {
277 			dev_err(dev,
278 				"Failed to prepare/enable clk, err=%d\n",
279 				ret);
280 			return ret;
281 		}
282 	}
283 
284 	return ret;
285 }
286 
imx_disable_unprepare_clks(struct device * dev)287 static void imx_disable_unprepare_clks(struct device *dev)
288 {
289 	struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
290 
291 	if (data->need_three_clks) {
292 		clk_disable_unprepare(data->clk_per);
293 		clk_disable_unprepare(data->clk_ahb);
294 		clk_disable_unprepare(data->clk_ipg);
295 	} else {
296 		clk_disable_unprepare(data->clk);
297 	}
298 }
299 
ci_hdrc_imx_notify_event(struct ci_hdrc * ci,unsigned int event)300 static int ci_hdrc_imx_notify_event(struct ci_hdrc *ci, unsigned int event)
301 {
302 	struct device *dev = ci->dev->parent;
303 	struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
304 	int ret = 0;
305 	struct imx_usbmisc_data *mdata = data->usbmisc_data;
306 
307 	switch (event) {
308 	case CI_HDRC_IMX_HSIC_ACTIVE_EVENT:
309 		if (data->pinctrl) {
310 			ret = pinctrl_select_state(data->pinctrl,
311 					data->pinctrl_hsic_active);
312 			if (ret)
313 				dev_err(dev,
314 					"hsic_active select failed, err=%d\n",
315 					ret);
316 		}
317 		break;
318 	case CI_HDRC_IMX_HSIC_SUSPEND_EVENT:
319 		ret = imx_usbmisc_hsic_set_connect(mdata);
320 		if (ret)
321 			dev_err(dev,
322 				"hsic_set_connect failed, err=%d\n", ret);
323 		break;
324 	case CI_HDRC_CONTROLLER_VBUS_EVENT:
325 		if (ci->vbus_active)
326 			ret = imx_usbmisc_charger_detection(mdata, true);
327 		else
328 			ret = imx_usbmisc_charger_detection(mdata, false);
329 		if (ci->usb_phy)
330 			schedule_work(&ci->usb_phy->chg_work);
331 		break;
332 	default:
333 		break;
334 	}
335 
336 	return ret;
337 }
338 
ci_hdrc_imx_probe(struct platform_device * pdev)339 static int ci_hdrc_imx_probe(struct platform_device *pdev)
340 {
341 	struct ci_hdrc_imx_data *data;
342 	struct ci_hdrc_platform_data pdata = {
343 		.name		= dev_name(&pdev->dev),
344 		.capoffset	= DEF_CAPOFFSET,
345 		.notify_event	= ci_hdrc_imx_notify_event,
346 	};
347 	int ret;
348 	const struct ci_hdrc_imx_platform_flag *imx_platform_flag;
349 	struct device_node *np = pdev->dev.of_node;
350 	struct device *dev = &pdev->dev;
351 
352 	imx_platform_flag = of_device_get_match_data(&pdev->dev);
353 
354 	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
355 	if (!data)
356 		return -ENOMEM;
357 
358 	data->plat_data = imx_platform_flag;
359 	pdata.flags |= imx_platform_flag->flags;
360 	platform_set_drvdata(pdev, data);
361 	data->usbmisc_data = usbmisc_get_init_data(dev);
362 	if (IS_ERR(data->usbmisc_data))
363 		return PTR_ERR(data->usbmisc_data);
364 
365 	if ((of_usb_get_phy_mode(dev->of_node) == USBPHY_INTERFACE_MODE_HSIC)
366 		&& data->usbmisc_data) {
367 		pdata.flags |= CI_HDRC_IMX_IS_HSIC;
368 		data->usbmisc_data->hsic = 1;
369 		data->pinctrl = devm_pinctrl_get(dev);
370 		if (PTR_ERR(data->pinctrl) == -ENODEV)
371 			data->pinctrl = NULL;
372 		else if (IS_ERR(data->pinctrl))
373 			return dev_err_probe(dev, PTR_ERR(data->pinctrl),
374 					     "pinctrl get failed\n");
375 
376 		data->hsic_pad_regulator =
377 				devm_regulator_get_optional(dev, "hsic");
378 		if (PTR_ERR(data->hsic_pad_regulator) == -ENODEV) {
379 			/* no pad regulator is needed */
380 			data->hsic_pad_regulator = NULL;
381 		} else if (IS_ERR(data->hsic_pad_regulator))
382 			return dev_err_probe(dev, PTR_ERR(data->hsic_pad_regulator),
383 					     "Get HSIC pad regulator error\n");
384 
385 		if (data->hsic_pad_regulator) {
386 			ret = regulator_enable(data->hsic_pad_regulator);
387 			if (ret) {
388 				dev_err(dev,
389 					"Failed to enable HSIC pad regulator\n");
390 				return ret;
391 			}
392 		}
393 	}
394 
395 	/* HSIC pinctrl handling */
396 	if (data->pinctrl) {
397 		struct pinctrl_state *pinctrl_hsic_idle;
398 
399 		pinctrl_hsic_idle = pinctrl_lookup_state(data->pinctrl, "idle");
400 		if (IS_ERR(pinctrl_hsic_idle)) {
401 			dev_err(dev,
402 				"pinctrl_hsic_idle lookup failed, err=%ld\n",
403 					PTR_ERR(pinctrl_hsic_idle));
404 			return PTR_ERR(pinctrl_hsic_idle);
405 		}
406 
407 		ret = pinctrl_select_state(data->pinctrl, pinctrl_hsic_idle);
408 		if (ret) {
409 			dev_err(dev, "hsic_idle select failed, err=%d\n", ret);
410 			return ret;
411 		}
412 
413 		data->pinctrl_hsic_active = pinctrl_lookup_state(data->pinctrl,
414 								"active");
415 		if (IS_ERR(data->pinctrl_hsic_active)) {
416 			dev_err(dev,
417 				"pinctrl_hsic_active lookup failed, err=%ld\n",
418 					PTR_ERR(data->pinctrl_hsic_active));
419 			return PTR_ERR(data->pinctrl_hsic_active);
420 		}
421 	}
422 
423 	if (pdata.flags & CI_HDRC_PMQOS)
424 		cpu_latency_qos_add_request(&data->pm_qos_req, 0);
425 
426 	ret = imx_get_clks(dev);
427 	if (ret)
428 		goto disable_hsic_regulator;
429 
430 	ret = imx_prepare_enable_clks(dev);
431 	if (ret)
432 		goto disable_hsic_regulator;
433 
434 	ret = clk_prepare_enable(data->clk_wakeup);
435 	if (ret)
436 		goto err_wakeup_clk;
437 
438 	data->phy = devm_usb_get_phy_by_phandle(dev, "fsl,usbphy", 0);
439 	if (IS_ERR(data->phy)) {
440 		ret = PTR_ERR(data->phy);
441 		if (ret != -ENODEV) {
442 			dev_err_probe(dev, ret, "Failed to parse fsl,usbphy\n");
443 			goto err_clk;
444 		}
445 		data->phy = devm_usb_get_phy_by_phandle(dev, "phys", 0);
446 		if (IS_ERR(data->phy)) {
447 			ret = PTR_ERR(data->phy);
448 			if (ret == -ENODEV) {
449 				data->phy = NULL;
450 			} else {
451 				dev_err_probe(dev, ret, "Failed to parse phys\n");
452 				goto err_clk;
453 			}
454 		}
455 	}
456 
457 	pdata.usb_phy = data->phy;
458 	if (data->usbmisc_data)
459 		data->usbmisc_data->usb_phy = data->phy;
460 
461 	if ((of_device_is_compatible(np, "fsl,imx53-usb") ||
462 	     of_device_is_compatible(np, "fsl,imx51-usb")) && pdata.usb_phy &&
463 	    of_usb_get_phy_mode(np) == USBPHY_INTERFACE_MODE_ULPI) {
464 		pdata.flags |= CI_HDRC_OVERRIDE_PHY_CONTROL;
465 		data->override_phy_control = true;
466 		usb_phy_init(pdata.usb_phy);
467 	}
468 
469 	if (pdata.flags & CI_HDRC_SUPPORTS_RUNTIME_PM)
470 		data->supports_runtime_pm = true;
471 
472 	ret = imx_usbmisc_init(data->usbmisc_data);
473 	if (ret) {
474 		dev_err(dev, "usbmisc init failed, ret=%d\n", ret);
475 		goto err_clk;
476 	}
477 
478 	data->ci_pdev = ci_hdrc_add_device(dev,
479 				pdev->resource, pdev->num_resources,
480 				&pdata);
481 	if (IS_ERR(data->ci_pdev)) {
482 		ret = PTR_ERR(data->ci_pdev);
483 		dev_err_probe(dev, ret, "ci_hdrc_add_device failed\n");
484 		goto err_clk;
485 	}
486 
487 	if (data->usbmisc_data) {
488 		if (!IS_ERR(pdata.id_extcon.edev) ||
489 		    of_property_read_bool(np, "usb-role-switch"))
490 			data->usbmisc_data->ext_id = 1;
491 
492 		if (!IS_ERR(pdata.vbus_extcon.edev) ||
493 		    of_property_read_bool(np, "usb-role-switch"))
494 			data->usbmisc_data->ext_vbus = 1;
495 
496 		/* usbmisc needs to know dr mode to choose wakeup setting */
497 		data->usbmisc_data->available_role =
498 			ci_hdrc_query_available_role(data->ci_pdev);
499 	}
500 
501 	ret = imx_usbmisc_init_post(data->usbmisc_data);
502 	if (ret) {
503 		dev_err(dev, "usbmisc post failed, ret=%d\n", ret);
504 		goto disable_device;
505 	}
506 
507 	if (data->supports_runtime_pm) {
508 		pm_runtime_set_active(dev);
509 		pm_runtime_enable(dev);
510 	}
511 
512 	device_set_wakeup_capable(dev, true);
513 
514 	return 0;
515 
516 disable_device:
517 	ci_hdrc_remove_device(data->ci_pdev);
518 err_clk:
519 	clk_disable_unprepare(data->clk_wakeup);
520 err_wakeup_clk:
521 	imx_disable_unprepare_clks(dev);
522 disable_hsic_regulator:
523 	if (data->hsic_pad_regulator)
524 		/* don't overwrite original ret (cf. EPROBE_DEFER) */
525 		regulator_disable(data->hsic_pad_regulator);
526 	if (pdata.flags & CI_HDRC_PMQOS)
527 		cpu_latency_qos_remove_request(&data->pm_qos_req);
528 	data->ci_pdev = NULL;
529 	return ret;
530 }
531 
ci_hdrc_imx_remove(struct platform_device * pdev)532 static void ci_hdrc_imx_remove(struct platform_device *pdev)
533 {
534 	struct ci_hdrc_imx_data *data = platform_get_drvdata(pdev);
535 
536 	if (data->supports_runtime_pm) {
537 		pm_runtime_get_sync(&pdev->dev);
538 		pm_runtime_disable(&pdev->dev);
539 		pm_runtime_put_noidle(&pdev->dev);
540 	}
541 	if (data->ci_pdev)
542 		ci_hdrc_remove_device(data->ci_pdev);
543 	if (data->override_phy_control)
544 		usb_phy_shutdown(data->phy);
545 	if (data->ci_pdev) {
546 		imx_disable_unprepare_clks(&pdev->dev);
547 		clk_disable_unprepare(data->clk_wakeup);
548 		if (data->plat_data->flags & CI_HDRC_PMQOS)
549 			cpu_latency_qos_remove_request(&data->pm_qos_req);
550 		if (data->hsic_pad_regulator)
551 			regulator_disable(data->hsic_pad_regulator);
552 	}
553 }
554 
ci_hdrc_imx_shutdown(struct platform_device * pdev)555 static void ci_hdrc_imx_shutdown(struct platform_device *pdev)
556 {
557 	ci_hdrc_imx_remove(pdev);
558 }
559 
imx_controller_suspend(struct device * dev,pm_message_t msg)560 static int imx_controller_suspend(struct device *dev,
561 						 pm_message_t msg)
562 {
563 	struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
564 	int ret = 0;
565 
566 	dev_dbg(dev, "at %s\n", __func__);
567 
568 	ret = imx_usbmisc_suspend(data->usbmisc_data,
569 				  PMSG_IS_AUTO(msg) || device_may_wakeup(dev));
570 	if (ret) {
571 		dev_err(dev,
572 			"usbmisc suspend failed, ret=%d\n", ret);
573 		return ret;
574 	}
575 
576 	imx_disable_unprepare_clks(dev);
577 	if (data->plat_data->flags & CI_HDRC_PMQOS)
578 		cpu_latency_qos_remove_request(&data->pm_qos_req);
579 
580 	data->in_lpm = true;
581 
582 	return 0;
583 }
584 
imx_controller_resume(struct device * dev,pm_message_t msg)585 static int imx_controller_resume(struct device *dev,
586 						pm_message_t msg)
587 {
588 	struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
589 	int ret = 0;
590 
591 	dev_dbg(dev, "at %s\n", __func__);
592 
593 	if (!data->in_lpm) {
594 		WARN_ON(1);
595 		return 0;
596 	}
597 
598 	if (data->plat_data->flags & CI_HDRC_PMQOS)
599 		cpu_latency_qos_add_request(&data->pm_qos_req, 0);
600 
601 	ret = imx_prepare_enable_clks(dev);
602 	if (ret)
603 		return ret;
604 
605 	data->in_lpm = false;
606 
607 	ret = imx_usbmisc_resume(data->usbmisc_data,
608 				 PMSG_IS_AUTO(msg) || device_may_wakeup(dev));
609 	if (ret) {
610 		dev_err(dev, "usbmisc resume failed, ret=%d\n", ret);
611 		goto clk_disable;
612 	}
613 
614 	return 0;
615 
616 clk_disable:
617 	imx_disable_unprepare_clks(dev);
618 	return ret;
619 }
620 
ci_hdrc_imx_suspend(struct device * dev)621 static int ci_hdrc_imx_suspend(struct device *dev)
622 {
623 	int ret;
624 
625 	struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
626 
627 	if (data->in_lpm)
628 		/* The core's suspend doesn't run */
629 		return 0;
630 
631 	ret = imx_controller_suspend(dev, PMSG_SUSPEND);
632 	if (ret)
633 		return ret;
634 
635 	pinctrl_pm_select_sleep_state(dev);
636 	return ret;
637 }
638 
ci_hdrc_imx_resume(struct device * dev)639 static int ci_hdrc_imx_resume(struct device *dev)
640 {
641 	struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
642 	int ret;
643 
644 	pinctrl_pm_select_default_state(dev);
645 	ret = imx_controller_resume(dev, PMSG_RESUME);
646 	if (!ret && data->supports_runtime_pm) {
647 		pm_runtime_disable(dev);
648 		pm_runtime_set_active(dev);
649 		pm_runtime_enable(dev);
650 	}
651 
652 	return ret;
653 }
654 
ci_hdrc_imx_runtime_suspend(struct device * dev)655 static int ci_hdrc_imx_runtime_suspend(struct device *dev)
656 {
657 	struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
658 
659 	if (data->in_lpm) {
660 		WARN_ON(1);
661 		return 0;
662 	}
663 
664 	return imx_controller_suspend(dev, PMSG_AUTO_SUSPEND);
665 }
666 
ci_hdrc_imx_runtime_resume(struct device * dev)667 static int ci_hdrc_imx_runtime_resume(struct device *dev)
668 {
669 	return imx_controller_resume(dev, PMSG_AUTO_RESUME);
670 }
671 
672 static const struct dev_pm_ops ci_hdrc_imx_pm_ops = {
673 	SYSTEM_SLEEP_PM_OPS(ci_hdrc_imx_suspend, ci_hdrc_imx_resume)
674 	RUNTIME_PM_OPS(ci_hdrc_imx_runtime_suspend, ci_hdrc_imx_runtime_resume, NULL)
675 };
676 static struct platform_driver ci_hdrc_imx_driver = {
677 	.probe = ci_hdrc_imx_probe,
678 	.remove_new = ci_hdrc_imx_remove,
679 	.shutdown = ci_hdrc_imx_shutdown,
680 	.driver = {
681 		.name = "imx_usb",
682 		.of_match_table = ci_hdrc_imx_dt_ids,
683 		.pm = pm_ptr(&ci_hdrc_imx_pm_ops),
684 	 },
685 };
686 
687 module_platform_driver(ci_hdrc_imx_driver);
688 
689 MODULE_ALIAS("platform:imx-usb");
690 MODULE_LICENSE("GPL");
691 MODULE_DESCRIPTION("CI HDRC i.MX USB binding");
692 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
693 MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");
694