1 // SPDX-License-Identifier: GPL-2.0
3 * dwc3-of-simple.c - OF glue layer for simple integrations
5 * Copyright (c) 2015 Texas Instruments Incorporated - http://www.ti.com
7 * Author: Felipe Balbi <balbi@ti.com>
9 * This is a combination of the old dwc3-qcom.c by Ivan T. Ivanov
10 * <iivanov@mm-sol.com> and the original patch adding support for Xilinx' SoC
11 * by Subbaraya Sundeep Bhatta <subbaraya.sundeep.bhatta@xilinx.com>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/platform_device.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/clk.h>
21 #include <linux/of_platform.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/reset.h>
25 struct dwc3_of_simple
{
29 struct reset_control
*resets
;
34 static int dwc3_of_simple_clk_init(struct dwc3_of_simple
*simple
, int count
)
36 struct device
*dev
= simple
->dev
;
37 struct device_node
*np
= dev
->of_node
;
40 simple
->num_clocks
= count
;
45 simple
->clks
= devm_kcalloc(dev
, simple
->num_clocks
,
46 sizeof(struct clk
*), GFP_KERNEL
);
50 for (i
= 0; i
< simple
->num_clocks
; i
++) {
54 clk
= of_clk_get(np
, i
);
57 clk_disable_unprepare(simple
->clks
[i
]);
58 clk_put(simple
->clks
[i
]);
63 ret
= clk_prepare_enable(clk
);
66 clk_disable_unprepare(simple
->clks
[i
]);
67 clk_put(simple
->clks
[i
]);
74 simple
->clks
[i
] = clk
;
80 static int dwc3_of_simple_probe(struct platform_device
*pdev
)
82 struct dwc3_of_simple
*simple
;
83 struct device
*dev
= &pdev
->dev
;
84 struct device_node
*np
= dev
->of_node
;
88 bool shared_resets
= false;
90 simple
= devm_kzalloc(dev
, sizeof(*simple
), GFP_KERNEL
);
94 platform_set_drvdata(pdev
, simple
);
98 * Some controllers need to toggle the usb3-otg reset before trying to
99 * initialize the PHY, otherwise the PHY times out.
101 if (of_device_is_compatible(np
, "rockchip,rk3399-dwc3"))
102 simple
->need_reset
= true;
104 if (of_device_is_compatible(np
, "amlogic,meson-axg-dwc3") ||
105 of_device_is_compatible(np
, "amlogic,meson-gxl-dwc3")) {
106 shared_resets
= true;
107 simple
->pulse_resets
= true;
110 simple
->resets
= of_reset_control_array_get(np
, shared_resets
, true);
111 if (IS_ERR(simple
->resets
)) {
112 ret
= PTR_ERR(simple
->resets
);
113 dev_err(dev
, "failed to get device resets, err=%d\n", ret
);
117 if (simple
->pulse_resets
) {
118 ret
= reset_control_reset(simple
->resets
);
122 ret
= reset_control_deassert(simple
->resets
);
127 ret
= dwc3_of_simple_clk_init(simple
, of_count_phandle_with_args(np
,
128 "clocks", "#clock-cells"));
130 goto err_resetc_assert
;
132 ret
= of_platform_populate(np
, NULL
, NULL
, dev
);
134 for (i
= 0; i
< simple
->num_clocks
; i
++) {
135 clk_disable_unprepare(simple
->clks
[i
]);
136 clk_put(simple
->clks
[i
]);
139 goto err_resetc_assert
;
142 pm_runtime_set_active(dev
);
143 pm_runtime_enable(dev
);
144 pm_runtime_get_sync(dev
);
149 if (!simple
->pulse_resets
)
150 reset_control_assert(simple
->resets
);
153 reset_control_put(simple
->resets
);
157 static int dwc3_of_simple_remove(struct platform_device
*pdev
)
159 struct dwc3_of_simple
*simple
= platform_get_drvdata(pdev
);
160 struct device
*dev
= &pdev
->dev
;
163 of_platform_depopulate(dev
);
165 for (i
= 0; i
< simple
->num_clocks
; i
++) {
166 clk_disable_unprepare(simple
->clks
[i
]);
167 clk_put(simple
->clks
[i
]);
169 simple
->num_clocks
= 0;
171 if (!simple
->pulse_resets
)
172 reset_control_assert(simple
->resets
);
174 reset_control_put(simple
->resets
);
176 pm_runtime_disable(dev
);
177 pm_runtime_put_noidle(dev
);
178 pm_runtime_set_suspended(dev
);
183 static int __maybe_unused
dwc3_of_simple_runtime_suspend(struct device
*dev
)
185 struct dwc3_of_simple
*simple
= dev_get_drvdata(dev
);
188 for (i
= 0; i
< simple
->num_clocks
; i
++)
189 clk_disable(simple
->clks
[i
]);
194 static int __maybe_unused
dwc3_of_simple_runtime_resume(struct device
*dev
)
196 struct dwc3_of_simple
*simple
= dev_get_drvdata(dev
);
200 for (i
= 0; i
< simple
->num_clocks
; i
++) {
201 ret
= clk_enable(simple
->clks
[i
]);
204 clk_disable(simple
->clks
[i
]);
212 static int __maybe_unused
dwc3_of_simple_suspend(struct device
*dev
)
214 struct dwc3_of_simple
*simple
= dev_get_drvdata(dev
);
216 if (simple
->need_reset
)
217 reset_control_assert(simple
->resets
);
222 static int __maybe_unused
dwc3_of_simple_resume(struct device
*dev
)
224 struct dwc3_of_simple
*simple
= dev_get_drvdata(dev
);
226 if (simple
->need_reset
)
227 reset_control_deassert(simple
->resets
);
232 static const struct dev_pm_ops dwc3_of_simple_dev_pm_ops
= {
233 SET_SYSTEM_SLEEP_PM_OPS(dwc3_of_simple_suspend
, dwc3_of_simple_resume
)
234 SET_RUNTIME_PM_OPS(dwc3_of_simple_runtime_suspend
,
235 dwc3_of_simple_runtime_resume
, NULL
)
238 static const struct of_device_id of_dwc3_simple_match
[] = {
239 { .compatible
= "rockchip,rk3399-dwc3" },
240 { .compatible
= "xlnx,zynqmp-dwc3" },
241 { .compatible
= "cavium,octeon-7130-usb-uctl" },
242 { .compatible
= "sprd,sc9860-dwc3" },
243 { .compatible
= "amlogic,meson-axg-dwc3" },
244 { .compatible
= "amlogic,meson-gxl-dwc3" },
245 { .compatible
= "allwinner,sun50i-h6-dwc3" },
248 MODULE_DEVICE_TABLE(of
, of_dwc3_simple_match
);
250 static struct platform_driver dwc3_of_simple_driver
= {
251 .probe
= dwc3_of_simple_probe
,
252 .remove
= dwc3_of_simple_remove
,
254 .name
= "dwc3-of-simple",
255 .of_match_table
= of_dwc3_simple_match
,
256 .pm
= &dwc3_of_simple_dev_pm_ops
,
260 module_platform_driver(dwc3_of_simple_driver
);
261 MODULE_LICENSE("GPL v2");
262 MODULE_DESCRIPTION("DesignWare USB3 OF Simple Glue Layer");
263 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");