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_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>
15 #include <linux/usb/of.h>
16 #include <linux/clk.h>
17 #include <linux/pinctrl/consumer.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 };
63 
64 static const struct ci_hdrc_imx_platform_flag imx7d_usb_data = {
65 	.flags = CI_HDRC_SUPPORTS_RUNTIME_PM,
66 };
67 
68 static const struct of_device_id ci_hdrc_imx_dt_ids[] = {
69 	{ .compatible = "fsl,imx23-usb", .data = &imx23_usb_data},
70 	{ .compatible = "fsl,imx28-usb", .data = &imx28_usb_data},
71 	{ .compatible = "fsl,imx27-usb", .data = &imx27_usb_data},
72 	{ .compatible = "fsl,imx6q-usb", .data = &imx6q_usb_data},
73 	{ .compatible = "fsl,imx6sl-usb", .data = &imx6sl_usb_data},
74 	{ .compatible = "fsl,imx6sx-usb", .data = &imx6sx_usb_data},
75 	{ .compatible = "fsl,imx6ul-usb", .data = &imx6ul_usb_data},
76 	{ .compatible = "fsl,imx7d-usb", .data = &imx7d_usb_data},
77 	{ /* sentinel */ }
78 };
79 MODULE_DEVICE_TABLE(of, ci_hdrc_imx_dt_ids);
80 
81 struct ci_hdrc_imx_data {
82 	struct usb_phy *phy;
83 	struct platform_device *ci_pdev;
84 	struct clk *clk;
85 	struct imx_usbmisc_data *usbmisc_data;
86 	bool supports_runtime_pm;
87 	bool override_phy_control;
88 	bool in_lpm;
89 	struct pinctrl *pinctrl;
90 	struct pinctrl_state *pinctrl_hsic_active;
91 	struct regulator *hsic_pad_regulator;
92 	/* 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 	/* --------------------------------- */
98 };
99 
100 /* Common functions shared by usbmisc drivers */
101 
102 static struct imx_usbmisc_data *usbmisc_get_init_data(struct device *dev)
103 {
104 	struct platform_device *misc_pdev;
105 	struct device_node *np = dev->of_node;
106 	struct of_phandle_args args;
107 	struct imx_usbmisc_data *data;
108 	int ret;
109 
110 	/*
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);
120 
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);
126 		return ERR_PTR(ret);
127 	}
128 
129 	data->index = args.args[0];
130 
131 	misc_pdev = of_find_device_by_node(args.np);
132 	of_node_put(args.np);
133 
134 	if (!misc_pdev || !platform_get_drvdata(misc_pdev))
135 		return ERR_PTR(-EPROBE_DEFER);
136 
137 	data->dev = &misc_pdev->dev;
138 
139 	/*
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)) {
144 		data->disable_oc = 1;
145 	} 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 	} else {
152 		dev_warn(dev, "No over current polarity defined\n");
153 	}
154 
155 	if (of_find_property(np, "external-vbus-divider", NULL))
156 		data->evdo = 1;
157 
158 	if (of_usb_get_phy_mode(np) == USBPHY_INTERFACE_MODE_ULPI)
159 		data->ulpi = 1;
160 
161 	return data;
162 }
163 
164 /* End of common functions shared by usbmisc drivers*/
165 static int imx_get_clks(struct device *dev)
166 {
167 	struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
168 	int ret = 0;
169 
170 	data->clk_ipg = devm_clk_get(dev, "ipg");
171 	if (IS_ERR(data->clk_ipg)) {
172 		/* If the platform only needs one clocks */
173 		data->clk = devm_clk_get(dev, NULL);
174 		if (IS_ERR(data->clk)) {
175 			ret = PTR_ERR(data->clk);
176 			dev_err(dev,
177 				"Failed to get clks, err=%ld,%ld\n",
178 				PTR_ERR(data->clk), PTR_ERR(data->clk_ipg));
179 			return ret;
180 		}
181 		return ret;
182 	}
183 
184 	data->clk_ahb = devm_clk_get(dev, "ahb");
185 	if (IS_ERR(data->clk_ahb)) {
186 		ret = PTR_ERR(data->clk_ahb);
187 		dev_err(dev,
188 			"Failed to get ahb clock, err=%d\n", ret);
189 		return ret;
190 	}
191 
192 	data->clk_per = devm_clk_get(dev, "per");
193 	if (IS_ERR(data->clk_per)) {
194 		ret = PTR_ERR(data->clk_per);
195 		dev_err(dev,
196 			"Failed to get per clock, err=%d\n", ret);
197 		return ret;
198 	}
199 
200 	data->need_three_clks = true;
201 	return ret;
202 }
203 
204 static int imx_prepare_enable_clks(struct device *dev)
205 {
206 	struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
207 	int ret = 0;
208 
209 	if (data->need_three_clks) {
210 		ret = clk_prepare_enable(data->clk_ipg);
211 		if (ret) {
212 			dev_err(dev,
213 				"Failed to prepare/enable ipg clk, err=%d\n",
214 				ret);
215 			return ret;
216 		}
217 
218 		ret = clk_prepare_enable(data->clk_ahb);
219 		if (ret) {
220 			dev_err(dev,
221 				"Failed to prepare/enable ahb clk, err=%d\n",
222 				ret);
223 			clk_disable_unprepare(data->clk_ipg);
224 			return ret;
225 		}
226 
227 		ret = clk_prepare_enable(data->clk_per);
228 		if (ret) {
229 			dev_err(dev,
230 				"Failed to prepare/enable per clk, err=%d\n",
231 				ret);
232 			clk_disable_unprepare(data->clk_ahb);
233 			clk_disable_unprepare(data->clk_ipg);
234 			return ret;
235 		}
236 	} else {
237 		ret = clk_prepare_enable(data->clk);
238 		if (ret) {
239 			dev_err(dev,
240 				"Failed to prepare/enable clk, err=%d\n",
241 				ret);
242 			return ret;
243 		}
244 	}
245 
246 	return ret;
247 }
248 
249 static void imx_disable_unprepare_clks(struct device *dev)
250 {
251 	struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
252 
253 	if (data->need_three_clks) {
254 		clk_disable_unprepare(data->clk_per);
255 		clk_disable_unprepare(data->clk_ahb);
256 		clk_disable_unprepare(data->clk_ipg);
257 	} else {
258 		clk_disable_unprepare(data->clk);
259 	}
260 }
261 
262 static int ci_hdrc_imx_notify_event(struct ci_hdrc *ci, unsigned int event)
263 {
264 	struct device *dev = ci->dev->parent;
265 	struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
266 	int ret = 0;
267 
268 	switch (event) {
269 	case CI_HDRC_IMX_HSIC_ACTIVE_EVENT:
270 		ret = pinctrl_select_state(data->pinctrl,
271 				data->pinctrl_hsic_active);
272 		if (ret)
273 			dev_err(dev, "hsic_active select failed, err=%d\n",
274 				ret);
275 		break;
276 	case CI_HDRC_IMX_HSIC_SUSPEND_EVENT:
277 		ret = imx_usbmisc_hsic_set_connect(data->usbmisc_data);
278 		if (ret)
279 			dev_err(dev,
280 				"hsic_set_connect failed, err=%d\n", ret);
281 		break;
282 	default:
283 		break;
284 	}
285 
286 	return ret;
287 }
288 
289 static int ci_hdrc_imx_probe(struct platform_device *pdev)
290 {
291 	struct ci_hdrc_imx_data *data;
292 	struct ci_hdrc_platform_data pdata = {
293 		.name		= dev_name(&pdev->dev),
294 		.capoffset	= DEF_CAPOFFSET,
295 		.notify_event	= ci_hdrc_imx_notify_event,
296 	};
297 	int ret;
298 	const struct of_device_id *of_id;
299 	const struct ci_hdrc_imx_platform_flag *imx_platform_flag;
300 	struct device_node *np = pdev->dev.of_node;
301 	struct device *dev = &pdev->dev;
302 	struct pinctrl_state *pinctrl_hsic_idle;
303 
304 	of_id = of_match_device(ci_hdrc_imx_dt_ids, dev);
305 	if (!of_id)
306 		return -ENODEV;
307 
308 	imx_platform_flag = of_id->data;
309 
310 	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
311 	if (!data)
312 		return -ENOMEM;
313 
314 	platform_set_drvdata(pdev, data);
315 	data->usbmisc_data = usbmisc_get_init_data(dev);
316 	if (IS_ERR(data->usbmisc_data))
317 		return PTR_ERR(data->usbmisc_data);
318 
319 	if (of_usb_get_phy_mode(dev->of_node) == USBPHY_INTERFACE_MODE_HSIC) {
320 		pdata.flags |= CI_HDRC_IMX_IS_HSIC;
321 		data->usbmisc_data->hsic = 1;
322 		data->pinctrl = devm_pinctrl_get(dev);
323 		if (IS_ERR(data->pinctrl)) {
324 			dev_err(dev, "pinctrl get failed, err=%ld\n",
325 					PTR_ERR(data->pinctrl));
326 			return PTR_ERR(data->pinctrl);
327 		}
328 
329 		pinctrl_hsic_idle = pinctrl_lookup_state(data->pinctrl, "idle");
330 		if (IS_ERR(pinctrl_hsic_idle)) {
331 			dev_err(dev,
332 				"pinctrl_hsic_idle lookup failed, err=%ld\n",
333 					PTR_ERR(pinctrl_hsic_idle));
334 			return PTR_ERR(pinctrl_hsic_idle);
335 		}
336 
337 		ret = pinctrl_select_state(data->pinctrl, pinctrl_hsic_idle);
338 		if (ret) {
339 			dev_err(dev, "hsic_idle select failed, err=%d\n", ret);
340 			return ret;
341 		}
342 
343 		data->pinctrl_hsic_active = pinctrl_lookup_state(data->pinctrl,
344 								"active");
345 		if (IS_ERR(data->pinctrl_hsic_active)) {
346 			dev_err(dev,
347 				"pinctrl_hsic_active lookup failed, err=%ld\n",
348 					PTR_ERR(data->pinctrl_hsic_active));
349 			return PTR_ERR(data->pinctrl_hsic_active);
350 		}
351 
352 		data->hsic_pad_regulator = devm_regulator_get(dev, "hsic");
353 		if (PTR_ERR(data->hsic_pad_regulator) == -EPROBE_DEFER) {
354 			return -EPROBE_DEFER;
355 		} else if (PTR_ERR(data->hsic_pad_regulator) == -ENODEV) {
356 			/* no pad regualator is needed */
357 			data->hsic_pad_regulator = NULL;
358 		} else if (IS_ERR(data->hsic_pad_regulator)) {
359 			dev_err(dev, "Get HSIC pad regulator error: %ld\n",
360 					PTR_ERR(data->hsic_pad_regulator));
361 			return PTR_ERR(data->hsic_pad_regulator);
362 		}
363 
364 		if (data->hsic_pad_regulator) {
365 			ret = regulator_enable(data->hsic_pad_regulator);
366 			if (ret) {
367 				dev_err(dev,
368 					"Failed to enable HSIC pad regulator\n");
369 				return ret;
370 			}
371 		}
372 	}
373 	ret = imx_get_clks(dev);
374 	if (ret)
375 		goto disable_hsic_regulator;
376 
377 	ret = imx_prepare_enable_clks(dev);
378 	if (ret)
379 		goto disable_hsic_regulator;
380 
381 	data->phy = devm_usb_get_phy_by_phandle(dev, "fsl,usbphy", 0);
382 	if (IS_ERR(data->phy)) {
383 		ret = PTR_ERR(data->phy);
384 		/* Return -EINVAL if no usbphy is available */
385 		if (ret == -ENODEV)
386 			ret = -EINVAL;
387 		goto err_clk;
388 	}
389 
390 	pdata.usb_phy = data->phy;
391 
392 	if ((of_device_is_compatible(np, "fsl,imx53-usb") ||
393 	     of_device_is_compatible(np, "fsl,imx51-usb")) && pdata.usb_phy &&
394 	    of_usb_get_phy_mode(np) == USBPHY_INTERFACE_MODE_ULPI) {
395 		pdata.flags |= CI_HDRC_OVERRIDE_PHY_CONTROL;
396 		data->override_phy_control = true;
397 		usb_phy_init(pdata.usb_phy);
398 	}
399 
400 	pdata.flags |= imx_platform_flag->flags;
401 	if (pdata.flags & CI_HDRC_SUPPORTS_RUNTIME_PM)
402 		data->supports_runtime_pm = true;
403 
404 	ret = imx_usbmisc_init(data->usbmisc_data);
405 	if (ret) {
406 		dev_err(dev, "usbmisc init failed, ret=%d\n", ret);
407 		goto err_clk;
408 	}
409 
410 	data->ci_pdev = ci_hdrc_add_device(dev,
411 				pdev->resource, pdev->num_resources,
412 				&pdata);
413 	if (IS_ERR(data->ci_pdev)) {
414 		ret = PTR_ERR(data->ci_pdev);
415 		if (ret != -EPROBE_DEFER)
416 			dev_err(dev, "ci_hdrc_add_device failed, err=%d\n",
417 					ret);
418 		goto err_clk;
419 	}
420 
421 	ret = imx_usbmisc_init_post(data->usbmisc_data);
422 	if (ret) {
423 		dev_err(dev, "usbmisc post failed, ret=%d\n", ret);
424 		goto disable_device;
425 	}
426 
427 	if (data->supports_runtime_pm) {
428 		pm_runtime_set_active(dev);
429 		pm_runtime_enable(dev);
430 	}
431 
432 	device_set_wakeup_capable(dev, true);
433 
434 	return 0;
435 
436 disable_device:
437 	ci_hdrc_remove_device(data->ci_pdev);
438 err_clk:
439 	imx_disable_unprepare_clks(dev);
440 disable_hsic_regulator:
441 	if (data->hsic_pad_regulator)
442 		ret = regulator_disable(data->hsic_pad_regulator);
443 	return ret;
444 }
445 
446 static int ci_hdrc_imx_remove(struct platform_device *pdev)
447 {
448 	struct ci_hdrc_imx_data *data = platform_get_drvdata(pdev);
449 
450 	if (data->supports_runtime_pm) {
451 		pm_runtime_get_sync(&pdev->dev);
452 		pm_runtime_disable(&pdev->dev);
453 		pm_runtime_put_noidle(&pdev->dev);
454 	}
455 	ci_hdrc_remove_device(data->ci_pdev);
456 	if (data->override_phy_control)
457 		usb_phy_shutdown(data->phy);
458 	imx_disable_unprepare_clks(&pdev->dev);
459 	if (data->hsic_pad_regulator)
460 		regulator_disable(data->hsic_pad_regulator);
461 
462 	return 0;
463 }
464 
465 static void ci_hdrc_imx_shutdown(struct platform_device *pdev)
466 {
467 	ci_hdrc_imx_remove(pdev);
468 }
469 
470 static int __maybe_unused imx_controller_suspend(struct device *dev)
471 {
472 	struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
473 	int ret = 0;
474 
475 	dev_dbg(dev, "at %s\n", __func__);
476 
477 	ret = imx_usbmisc_hsic_set_clk(data->usbmisc_data, false);
478 	if (ret) {
479 		dev_err(dev, "usbmisc hsic_set_clk failed, ret=%d\n", ret);
480 		return ret;
481 	}
482 
483 	imx_disable_unprepare_clks(dev);
484 	data->in_lpm = true;
485 
486 	return 0;
487 }
488 
489 static int __maybe_unused imx_controller_resume(struct device *dev)
490 {
491 	struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
492 	int ret = 0;
493 
494 	dev_dbg(dev, "at %s\n", __func__);
495 
496 	if (!data->in_lpm) {
497 		WARN_ON(1);
498 		return 0;
499 	}
500 
501 	ret = imx_prepare_enable_clks(dev);
502 	if (ret)
503 		return ret;
504 
505 	data->in_lpm = false;
506 
507 	ret = imx_usbmisc_set_wakeup(data->usbmisc_data, false);
508 	if (ret) {
509 		dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n", ret);
510 		goto clk_disable;
511 	}
512 
513 	ret = imx_usbmisc_hsic_set_clk(data->usbmisc_data, true);
514 	if (ret) {
515 		dev_err(dev, "usbmisc hsic_set_clk failed, ret=%d\n", ret);
516 		goto hsic_set_clk_fail;
517 	}
518 
519 	return 0;
520 
521 hsic_set_clk_fail:
522 	imx_usbmisc_set_wakeup(data->usbmisc_data, true);
523 clk_disable:
524 	imx_disable_unprepare_clks(dev);
525 	return ret;
526 }
527 
528 static int __maybe_unused ci_hdrc_imx_suspend(struct device *dev)
529 {
530 	int ret;
531 
532 	struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
533 
534 	if (data->in_lpm)
535 		/* The core's suspend doesn't run */
536 		return 0;
537 
538 	if (device_may_wakeup(dev)) {
539 		ret = imx_usbmisc_set_wakeup(data->usbmisc_data, true);
540 		if (ret) {
541 			dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n",
542 					ret);
543 			return ret;
544 		}
545 	}
546 
547 	return imx_controller_suspend(dev);
548 }
549 
550 static int __maybe_unused ci_hdrc_imx_resume(struct device *dev)
551 {
552 	struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
553 	int ret;
554 
555 	ret = imx_controller_resume(dev);
556 	if (!ret && data->supports_runtime_pm) {
557 		pm_runtime_disable(dev);
558 		pm_runtime_set_active(dev);
559 		pm_runtime_enable(dev);
560 	}
561 
562 	return ret;
563 }
564 
565 static int __maybe_unused ci_hdrc_imx_runtime_suspend(struct device *dev)
566 {
567 	struct ci_hdrc_imx_data *data = dev_get_drvdata(dev);
568 	int ret;
569 
570 	if (data->in_lpm) {
571 		WARN_ON(1);
572 		return 0;
573 	}
574 
575 	ret = imx_usbmisc_set_wakeup(data->usbmisc_data, true);
576 	if (ret) {
577 		dev_err(dev, "usbmisc set_wakeup failed, ret=%d\n", ret);
578 		return ret;
579 	}
580 
581 	return imx_controller_suspend(dev);
582 }
583 
584 static int __maybe_unused ci_hdrc_imx_runtime_resume(struct device *dev)
585 {
586 	return imx_controller_resume(dev);
587 }
588 
589 static const struct dev_pm_ops ci_hdrc_imx_pm_ops = {
590 	SET_SYSTEM_SLEEP_PM_OPS(ci_hdrc_imx_suspend, ci_hdrc_imx_resume)
591 	SET_RUNTIME_PM_OPS(ci_hdrc_imx_runtime_suspend,
592 			ci_hdrc_imx_runtime_resume, NULL)
593 };
594 static struct platform_driver ci_hdrc_imx_driver = {
595 	.probe = ci_hdrc_imx_probe,
596 	.remove = ci_hdrc_imx_remove,
597 	.shutdown = ci_hdrc_imx_shutdown,
598 	.driver = {
599 		.name = "imx_usb",
600 		.of_match_table = ci_hdrc_imx_dt_ids,
601 		.pm = &ci_hdrc_imx_pm_ops,
602 	 },
603 };
604 
605 module_platform_driver(ci_hdrc_imx_driver);
606 
607 MODULE_ALIAS("platform:imx-usb");
608 MODULE_LICENSE("GPL");
609 MODULE_DESCRIPTION("CI HDRC i.MX USB binding");
610 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
611 MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");
612