1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2012 Freescale Semiconductor, Inc.
4 * Copyright (C) 2012 Marek Vasut <marex@denx.de>
5 * on behalf of DENX Software Engineering GmbH
8 #include <linux/module.h>
9 #include <linux/of_platform.h>
10 #include <linux/platform_device.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/usb/chipidea.h>
13 #include <linux/usb/of.h>
14 #include <linux/clk.h>
15 #include <linux/pinctrl/consumer.h>
16 #include <linux/pm_qos.h>
19 #include "ci_hdrc_imx.h"
21 struct ci_hdrc_imx_platform_flag
{
25 static const struct ci_hdrc_imx_platform_flag imx23_usb_data
= {
26 .flags
= CI_HDRC_TURN_VBUS_EARLY_ON
|
27 CI_HDRC_DISABLE_STREAMING
,
30 static const struct ci_hdrc_imx_platform_flag imx27_usb_data
= {
31 .flags
= CI_HDRC_DISABLE_STREAMING
,
34 static const struct ci_hdrc_imx_platform_flag imx28_usb_data
= {
35 .flags
= CI_HDRC_IMX28_WRITE_FIX
|
36 CI_HDRC_TURN_VBUS_EARLY_ON
|
37 CI_HDRC_DISABLE_STREAMING
,
40 static const struct ci_hdrc_imx_platform_flag imx6q_usb_data
= {
41 .flags
= CI_HDRC_SUPPORTS_RUNTIME_PM
|
42 CI_HDRC_TURN_VBUS_EARLY_ON
|
43 CI_HDRC_DISABLE_STREAMING
,
46 static const struct ci_hdrc_imx_platform_flag imx6sl_usb_data
= {
47 .flags
= CI_HDRC_SUPPORTS_RUNTIME_PM
|
48 CI_HDRC_TURN_VBUS_EARLY_ON
|
49 CI_HDRC_DISABLE_HOST_STREAMING
,
52 static const struct ci_hdrc_imx_platform_flag imx6sx_usb_data
= {
53 .flags
= CI_HDRC_SUPPORTS_RUNTIME_PM
|
54 CI_HDRC_TURN_VBUS_EARLY_ON
|
55 CI_HDRC_DISABLE_HOST_STREAMING
,
58 static const struct ci_hdrc_imx_platform_flag imx6ul_usb_data
= {
59 .flags
= CI_HDRC_SUPPORTS_RUNTIME_PM
|
60 CI_HDRC_TURN_VBUS_EARLY_ON
,
63 static const struct ci_hdrc_imx_platform_flag imx7d_usb_data
= {
64 .flags
= CI_HDRC_SUPPORTS_RUNTIME_PM
,
67 static const struct ci_hdrc_imx_platform_flag imx7ulp_usb_data
= {
68 .flags
= CI_HDRC_SUPPORTS_RUNTIME_PM
|
72 static const struct of_device_id ci_hdrc_imx_dt_ids
[] = {
73 { .compatible
= "fsl,imx23-usb", .data
= &imx23_usb_data
},
74 { .compatible
= "fsl,imx28-usb", .data
= &imx28_usb_data
},
75 { .compatible
= "fsl,imx27-usb", .data
= &imx27_usb_data
},
76 { .compatible
= "fsl,imx6q-usb", .data
= &imx6q_usb_data
},
77 { .compatible
= "fsl,imx6sl-usb", .data
= &imx6sl_usb_data
},
78 { .compatible
= "fsl,imx6sx-usb", .data
= &imx6sx_usb_data
},
79 { .compatible
= "fsl,imx6ul-usb", .data
= &imx6ul_usb_data
},
80 { .compatible
= "fsl,imx7d-usb", .data
= &imx7d_usb_data
},
81 { .compatible
= "fsl,imx7ulp-usb", .data
= &imx7ulp_usb_data
},
84 MODULE_DEVICE_TABLE(of
, ci_hdrc_imx_dt_ids
);
86 struct ci_hdrc_imx_data
{
88 struct platform_device
*ci_pdev
;
90 struct imx_usbmisc_data
*usbmisc_data
;
91 bool supports_runtime_pm
;
92 bool override_phy_control
;
94 struct pinctrl
*pinctrl
;
95 struct pinctrl_state
*pinctrl_hsic_active
;
96 struct regulator
*hsic_pad_regulator
;
97 /* SoC before i.mx6 (except imx23/imx28) needs three clks */
102 /* --------------------------------- */
103 struct pm_qos_request pm_qos_req
;
104 const struct ci_hdrc_imx_platform_flag
*plat_data
;
107 /* Common functions shared by usbmisc drivers */
109 static struct imx_usbmisc_data
*usbmisc_get_init_data(struct device
*dev
)
111 struct platform_device
*misc_pdev
;
112 struct device_node
*np
= dev
->of_node
;
113 struct of_phandle_args args
;
114 struct imx_usbmisc_data
*data
;
118 * In case the fsl,usbmisc property is not present this device doesn't
119 * need usbmisc. Return NULL (which is no error here)
121 if (!of_get_property(np
, "fsl,usbmisc", NULL
))
124 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
126 return ERR_PTR(-ENOMEM
);
128 ret
= of_parse_phandle_with_args(np
, "fsl,usbmisc", "#index-cells",
131 dev_err(dev
, "Failed to parse property fsl,usbmisc, errno %d\n",
136 data
->index
= args
.args
[0];
138 misc_pdev
= of_find_device_by_node(args
.np
);
139 of_node_put(args
.np
);
141 if (!misc_pdev
|| !platform_get_drvdata(misc_pdev
))
142 return ERR_PTR(-EPROBE_DEFER
);
144 data
->dev
= &misc_pdev
->dev
;
147 * Check the various over current related properties. If over current
148 * detection is disabled we're not interested in the polarity.
150 if (of_find_property(np
, "disable-over-current", NULL
)) {
151 data
->disable_oc
= 1;
152 } else if (of_find_property(np
, "over-current-active-high", NULL
)) {
153 data
->oc_pol_active_low
= 0;
154 data
->oc_pol_configured
= 1;
155 } else if (of_find_property(np
, "over-current-active-low", NULL
)) {
156 data
->oc_pol_active_low
= 1;
157 data
->oc_pol_configured
= 1;
159 dev_warn(dev
, "No over current polarity defined\n");
162 data
->pwr_pol
= of_property_read_bool(np
, "power-active-high");
163 data
->evdo
= of_property_read_bool(np
, "external-vbus-divider");
165 if (of_usb_get_phy_mode(np
) == USBPHY_INTERFACE_MODE_ULPI
)
171 /* End of common functions shared by usbmisc drivers*/
172 static int imx_get_clks(struct device
*dev
)
174 struct ci_hdrc_imx_data
*data
= dev_get_drvdata(dev
);
177 data
->clk_ipg
= devm_clk_get(dev
, "ipg");
178 if (IS_ERR(data
->clk_ipg
)) {
179 /* If the platform only needs one clocks */
180 data
->clk
= devm_clk_get(dev
, NULL
);
181 if (IS_ERR(data
->clk
)) {
182 ret
= PTR_ERR(data
->clk
);
184 "Failed to get clks, err=%ld,%ld\n",
185 PTR_ERR(data
->clk
), PTR_ERR(data
->clk_ipg
));
191 data
->clk_ahb
= devm_clk_get(dev
, "ahb");
192 if (IS_ERR(data
->clk_ahb
)) {
193 ret
= PTR_ERR(data
->clk_ahb
);
195 "Failed to get ahb clock, err=%d\n", ret
);
199 data
->clk_per
= devm_clk_get(dev
, "per");
200 if (IS_ERR(data
->clk_per
)) {
201 ret
= PTR_ERR(data
->clk_per
);
203 "Failed to get per clock, err=%d\n", ret
);
207 data
->need_three_clks
= true;
211 static int imx_prepare_enable_clks(struct device
*dev
)
213 struct ci_hdrc_imx_data
*data
= dev_get_drvdata(dev
);
216 if (data
->need_three_clks
) {
217 ret
= clk_prepare_enable(data
->clk_ipg
);
220 "Failed to prepare/enable ipg clk, err=%d\n",
225 ret
= clk_prepare_enable(data
->clk_ahb
);
228 "Failed to prepare/enable ahb clk, err=%d\n",
230 clk_disable_unprepare(data
->clk_ipg
);
234 ret
= clk_prepare_enable(data
->clk_per
);
237 "Failed to prepare/enable per clk, err=%d\n",
239 clk_disable_unprepare(data
->clk_ahb
);
240 clk_disable_unprepare(data
->clk_ipg
);
244 ret
= clk_prepare_enable(data
->clk
);
247 "Failed to prepare/enable clk, err=%d\n",
256 static void imx_disable_unprepare_clks(struct device
*dev
)
258 struct ci_hdrc_imx_data
*data
= dev_get_drvdata(dev
);
260 if (data
->need_three_clks
) {
261 clk_disable_unprepare(data
->clk_per
);
262 clk_disable_unprepare(data
->clk_ahb
);
263 clk_disable_unprepare(data
->clk_ipg
);
265 clk_disable_unprepare(data
->clk
);
269 static int ci_hdrc_imx_notify_event(struct ci_hdrc
*ci
, unsigned int event
)
271 struct device
*dev
= ci
->dev
->parent
;
272 struct ci_hdrc_imx_data
*data
= dev_get_drvdata(dev
);
274 struct imx_usbmisc_data
*mdata
= data
->usbmisc_data
;
277 case CI_HDRC_IMX_HSIC_ACTIVE_EVENT
:
279 ret
= pinctrl_select_state(data
->pinctrl
,
280 data
->pinctrl_hsic_active
);
283 "hsic_active select failed, err=%d\n",
287 case CI_HDRC_IMX_HSIC_SUSPEND_EVENT
:
288 ret
= imx_usbmisc_hsic_set_connect(mdata
);
291 "hsic_set_connect failed, err=%d\n", ret
);
293 case CI_HDRC_CONTROLLER_VBUS_EVENT
:
295 ret
= imx_usbmisc_charger_detection(mdata
, true);
297 ret
= imx_usbmisc_charger_detection(mdata
, false);
299 schedule_work(&ci
->usb_phy
->chg_work
);
308 static int ci_hdrc_imx_probe(struct platform_device
*pdev
)
310 struct ci_hdrc_imx_data
*data
;
311 struct ci_hdrc_platform_data pdata
= {
312 .name
= dev_name(&pdev
->dev
),
313 .capoffset
= DEF_CAPOFFSET
,
314 .notify_event
= ci_hdrc_imx_notify_event
,
317 const struct of_device_id
*of_id
;
318 const struct ci_hdrc_imx_platform_flag
*imx_platform_flag
;
319 struct device_node
*np
= pdev
->dev
.of_node
;
320 struct device
*dev
= &pdev
->dev
;
322 of_id
= of_match_device(ci_hdrc_imx_dt_ids
, dev
);
326 imx_platform_flag
= of_id
->data
;
328 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
332 data
->plat_data
= imx_platform_flag
;
333 pdata
.flags
|= imx_platform_flag
->flags
;
334 platform_set_drvdata(pdev
, data
);
335 data
->usbmisc_data
= usbmisc_get_init_data(dev
);
336 if (IS_ERR(data
->usbmisc_data
))
337 return PTR_ERR(data
->usbmisc_data
);
339 if ((of_usb_get_phy_mode(dev
->of_node
) == USBPHY_INTERFACE_MODE_HSIC
)
340 && data
->usbmisc_data
) {
341 pdata
.flags
|= CI_HDRC_IMX_IS_HSIC
;
342 data
->usbmisc_data
->hsic
= 1;
343 data
->pinctrl
= devm_pinctrl_get(dev
);
344 if (PTR_ERR(data
->pinctrl
) == -ENODEV
)
345 data
->pinctrl
= NULL
;
346 else if (IS_ERR(data
->pinctrl
)) {
347 if (PTR_ERR(data
->pinctrl
) != -EPROBE_DEFER
)
348 dev_err(dev
, "pinctrl get failed, err=%ld\n",
349 PTR_ERR(data
->pinctrl
));
350 return PTR_ERR(data
->pinctrl
);
353 data
->hsic_pad_regulator
=
354 devm_regulator_get_optional(dev
, "hsic");
355 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 if (PTR_ERR(data
->hsic_pad_regulator
) != -EPROBE_DEFER
)
361 "Get HSIC pad regulator error: %ld\n",
362 PTR_ERR(data
->hsic_pad_regulator
));
363 return PTR_ERR(data
->hsic_pad_regulator
);
366 if (data
->hsic_pad_regulator
) {
367 ret
= regulator_enable(data
->hsic_pad_regulator
);
370 "Failed to enable HSIC pad regulator\n");
376 /* HSIC pinctrl handling */
378 struct pinctrl_state
*pinctrl_hsic_idle
;
380 pinctrl_hsic_idle
= pinctrl_lookup_state(data
->pinctrl
, "idle");
381 if (IS_ERR(pinctrl_hsic_idle
)) {
383 "pinctrl_hsic_idle lookup failed, err=%ld\n",
384 PTR_ERR(pinctrl_hsic_idle
));
385 return PTR_ERR(pinctrl_hsic_idle
);
388 ret
= pinctrl_select_state(data
->pinctrl
, pinctrl_hsic_idle
);
390 dev_err(dev
, "hsic_idle select failed, err=%d\n", ret
);
394 data
->pinctrl_hsic_active
= pinctrl_lookup_state(data
->pinctrl
,
396 if (IS_ERR(data
->pinctrl_hsic_active
)) {
398 "pinctrl_hsic_active lookup failed, err=%ld\n",
399 PTR_ERR(data
->pinctrl_hsic_active
));
400 return PTR_ERR(data
->pinctrl_hsic_active
);
404 if (pdata
.flags
& CI_HDRC_PMQOS
)
405 cpu_latency_qos_add_request(&data
->pm_qos_req
, 0);
407 ret
= imx_get_clks(dev
);
409 goto disable_hsic_regulator
;
411 ret
= imx_prepare_enable_clks(dev
);
413 goto disable_hsic_regulator
;
415 data
->phy
= devm_usb_get_phy_by_phandle(dev
, "fsl,usbphy", 0);
416 if (IS_ERR(data
->phy
)) {
417 ret
= PTR_ERR(data
->phy
);
418 /* Return -EINVAL if no usbphy is available */
425 pdata
.usb_phy
= data
->phy
;
426 if (data
->usbmisc_data
)
427 data
->usbmisc_data
->usb_phy
= data
->phy
;
429 if ((of_device_is_compatible(np
, "fsl,imx53-usb") ||
430 of_device_is_compatible(np
, "fsl,imx51-usb")) && pdata
.usb_phy
&&
431 of_usb_get_phy_mode(np
) == USBPHY_INTERFACE_MODE_ULPI
) {
432 pdata
.flags
|= CI_HDRC_OVERRIDE_PHY_CONTROL
;
433 data
->override_phy_control
= true;
434 usb_phy_init(pdata
.usb_phy
);
437 if (pdata
.flags
& CI_HDRC_SUPPORTS_RUNTIME_PM
)
438 data
->supports_runtime_pm
= true;
440 ret
= imx_usbmisc_init(data
->usbmisc_data
);
442 dev_err(dev
, "usbmisc init failed, ret=%d\n", ret
);
446 data
->ci_pdev
= ci_hdrc_add_device(dev
,
447 pdev
->resource
, pdev
->num_resources
,
449 if (IS_ERR(data
->ci_pdev
)) {
450 ret
= PTR_ERR(data
->ci_pdev
);
451 if (ret
!= -EPROBE_DEFER
)
452 dev_err(dev
, "ci_hdrc_add_device failed, err=%d\n",
457 if (data
->usbmisc_data
) {
458 if (!IS_ERR(pdata
.id_extcon
.edev
) ||
459 of_property_read_bool(np
, "usb-role-switch"))
460 data
->usbmisc_data
->ext_id
= 1;
462 if (!IS_ERR(pdata
.vbus_extcon
.edev
) ||
463 of_property_read_bool(np
, "usb-role-switch"))
464 data
->usbmisc_data
->ext_vbus
= 1;
466 /* usbmisc needs to know dr mode to choose wakeup setting */
467 data
->usbmisc_data
->available_role
=
468 ci_hdrc_query_available_role(data
->ci_pdev
);
471 ret
= imx_usbmisc_init_post(data
->usbmisc_data
);
473 dev_err(dev
, "usbmisc post failed, ret=%d\n", ret
);
477 if (data
->supports_runtime_pm
) {
478 pm_runtime_set_active(dev
);
479 pm_runtime_enable(dev
);
482 device_set_wakeup_capable(dev
, true);
487 ci_hdrc_remove_device(data
->ci_pdev
);
489 imx_disable_unprepare_clks(dev
);
490 disable_hsic_regulator
:
491 if (data
->hsic_pad_regulator
)
492 /* don't overwrite original ret (cf. EPROBE_DEFER) */
493 regulator_disable(data
->hsic_pad_regulator
);
494 if (pdata
.flags
& CI_HDRC_PMQOS
)
495 cpu_latency_qos_remove_request(&data
->pm_qos_req
);
496 data
->ci_pdev
= NULL
;
500 static int ci_hdrc_imx_remove(struct platform_device
*pdev
)
502 struct ci_hdrc_imx_data
*data
= platform_get_drvdata(pdev
);
504 if (data
->supports_runtime_pm
) {
505 pm_runtime_get_sync(&pdev
->dev
);
506 pm_runtime_disable(&pdev
->dev
);
507 pm_runtime_put_noidle(&pdev
->dev
);
510 ci_hdrc_remove_device(data
->ci_pdev
);
511 if (data
->override_phy_control
)
512 usb_phy_shutdown(data
->phy
);
514 imx_disable_unprepare_clks(&pdev
->dev
);
515 if (data
->plat_data
->flags
& CI_HDRC_PMQOS
)
516 cpu_latency_qos_remove_request(&data
->pm_qos_req
);
517 if (data
->hsic_pad_regulator
)
518 regulator_disable(data
->hsic_pad_regulator
);
524 static void ci_hdrc_imx_shutdown(struct platform_device
*pdev
)
526 ci_hdrc_imx_remove(pdev
);
529 static int __maybe_unused
imx_controller_suspend(struct device
*dev
)
531 struct ci_hdrc_imx_data
*data
= dev_get_drvdata(dev
);
534 dev_dbg(dev
, "at %s\n", __func__
);
536 ret
= imx_usbmisc_hsic_set_clk(data
->usbmisc_data
, false);
538 dev_err(dev
, "usbmisc hsic_set_clk failed, ret=%d\n", ret
);
542 imx_disable_unprepare_clks(dev
);
543 if (data
->plat_data
->flags
& CI_HDRC_PMQOS
)
544 cpu_latency_qos_remove_request(&data
->pm_qos_req
);
551 static int __maybe_unused
imx_controller_resume(struct device
*dev
)
553 struct ci_hdrc_imx_data
*data
= dev_get_drvdata(dev
);
556 dev_dbg(dev
, "at %s\n", __func__
);
563 if (data
->plat_data
->flags
& CI_HDRC_PMQOS
)
564 cpu_latency_qos_add_request(&data
->pm_qos_req
, 0);
566 ret
= imx_prepare_enable_clks(dev
);
570 data
->in_lpm
= false;
572 ret
= imx_usbmisc_set_wakeup(data
->usbmisc_data
, false);
574 dev_err(dev
, "usbmisc set_wakeup failed, ret=%d\n", ret
);
578 ret
= imx_usbmisc_hsic_set_clk(data
->usbmisc_data
, true);
580 dev_err(dev
, "usbmisc hsic_set_clk failed, ret=%d\n", ret
);
581 goto hsic_set_clk_fail
;
587 imx_usbmisc_set_wakeup(data
->usbmisc_data
, true);
589 imx_disable_unprepare_clks(dev
);
593 static int __maybe_unused
ci_hdrc_imx_suspend(struct device
*dev
)
597 struct ci_hdrc_imx_data
*data
= dev_get_drvdata(dev
);
600 /* The core's suspend doesn't run */
603 if (device_may_wakeup(dev
)) {
604 ret
= imx_usbmisc_set_wakeup(data
->usbmisc_data
, true);
606 dev_err(dev
, "usbmisc set_wakeup failed, ret=%d\n",
612 return imx_controller_suspend(dev
);
615 static int __maybe_unused
ci_hdrc_imx_resume(struct device
*dev
)
617 struct ci_hdrc_imx_data
*data
= dev_get_drvdata(dev
);
620 ret
= imx_controller_resume(dev
);
621 if (!ret
&& data
->supports_runtime_pm
) {
622 pm_runtime_disable(dev
);
623 pm_runtime_set_active(dev
);
624 pm_runtime_enable(dev
);
630 static int __maybe_unused
ci_hdrc_imx_runtime_suspend(struct device
*dev
)
632 struct ci_hdrc_imx_data
*data
= dev_get_drvdata(dev
);
640 ret
= imx_usbmisc_set_wakeup(data
->usbmisc_data
, true);
642 dev_err(dev
, "usbmisc set_wakeup failed, ret=%d\n", ret
);
646 return imx_controller_suspend(dev
);
649 static int __maybe_unused
ci_hdrc_imx_runtime_resume(struct device
*dev
)
651 return imx_controller_resume(dev
);
654 static const struct dev_pm_ops ci_hdrc_imx_pm_ops
= {
655 SET_SYSTEM_SLEEP_PM_OPS(ci_hdrc_imx_suspend
, ci_hdrc_imx_resume
)
656 SET_RUNTIME_PM_OPS(ci_hdrc_imx_runtime_suspend
,
657 ci_hdrc_imx_runtime_resume
, NULL
)
659 static struct platform_driver ci_hdrc_imx_driver
= {
660 .probe
= ci_hdrc_imx_probe
,
661 .remove
= ci_hdrc_imx_remove
,
662 .shutdown
= ci_hdrc_imx_shutdown
,
665 .of_match_table
= ci_hdrc_imx_dt_ids
,
666 .pm
= &ci_hdrc_imx_pm_ops
,
670 module_platform_driver(ci_hdrc_imx_driver
);
672 MODULE_ALIAS("platform:imx-usb");
673 MODULE_LICENSE("GPL");
674 MODULE_DESCRIPTION("CI HDRC i.MX USB binding");
675 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
676 MODULE_AUTHOR("Richard Zhao <richard.zhao@freescale.com>");