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
;
32 static int dwc3_of_simple_clk_init(struct dwc3_of_simple
*simple
, int count
)
34 struct device
*dev
= simple
->dev
;
35 struct device_node
*np
= dev
->of_node
;
38 simple
->num_clocks
= count
;
43 simple
->clks
= devm_kcalloc(dev
, simple
->num_clocks
,
44 sizeof(struct clk
*), GFP_KERNEL
);
48 for (i
= 0; i
< simple
->num_clocks
; i
++) {
52 clk
= of_clk_get(np
, i
);
55 clk_disable_unprepare(simple
->clks
[i
]);
56 clk_put(simple
->clks
[i
]);
61 ret
= clk_prepare_enable(clk
);
64 clk_disable_unprepare(simple
->clks
[i
]);
65 clk_put(simple
->clks
[i
]);
72 simple
->clks
[i
] = clk
;
78 static int dwc3_of_simple_probe(struct platform_device
*pdev
)
80 struct dwc3_of_simple
*simple
;
81 struct device
*dev
= &pdev
->dev
;
82 struct device_node
*np
= dev
->of_node
;
87 simple
= devm_kzalloc(dev
, sizeof(*simple
), GFP_KERNEL
);
91 platform_set_drvdata(pdev
, simple
);
94 simple
->resets
= of_reset_control_array_get_optional_exclusive(np
);
95 if (IS_ERR(simple
->resets
)) {
96 ret
= PTR_ERR(simple
->resets
);
97 dev_err(dev
, "failed to get device resets, err=%d\n", ret
);
101 ret
= reset_control_deassert(simple
->resets
);
105 ret
= dwc3_of_simple_clk_init(simple
, of_count_phandle_with_args(np
,
106 "clocks", "#clock-cells"));
108 goto err_resetc_assert
;
110 ret
= of_platform_populate(np
, NULL
, NULL
, dev
);
112 for (i
= 0; i
< simple
->num_clocks
; i
++) {
113 clk_disable_unprepare(simple
->clks
[i
]);
114 clk_put(simple
->clks
[i
]);
117 goto err_resetc_assert
;
120 pm_runtime_set_active(dev
);
121 pm_runtime_enable(dev
);
122 pm_runtime_get_sync(dev
);
127 reset_control_assert(simple
->resets
);
130 reset_control_put(simple
->resets
);
134 static int dwc3_of_simple_remove(struct platform_device
*pdev
)
136 struct dwc3_of_simple
*simple
= platform_get_drvdata(pdev
);
137 struct device
*dev
= &pdev
->dev
;
140 of_platform_depopulate(dev
);
142 for (i
= 0; i
< simple
->num_clocks
; i
++) {
143 clk_disable_unprepare(simple
->clks
[i
]);
144 clk_put(simple
->clks
[i
]);
146 simple
->num_clocks
= 0;
148 reset_control_assert(simple
->resets
);
149 reset_control_put(simple
->resets
);
151 pm_runtime_put_sync(dev
);
152 pm_runtime_disable(dev
);
158 static int dwc3_of_simple_runtime_suspend(struct device
*dev
)
160 struct dwc3_of_simple
*simple
= dev_get_drvdata(dev
);
163 for (i
= 0; i
< simple
->num_clocks
; i
++)
164 clk_disable(simple
->clks
[i
]);
169 static int dwc3_of_simple_runtime_resume(struct device
*dev
)
171 struct dwc3_of_simple
*simple
= dev_get_drvdata(dev
);
175 for (i
= 0; i
< simple
->num_clocks
; i
++) {
176 ret
= clk_enable(simple
->clks
[i
]);
179 clk_disable(simple
->clks
[i
]);
188 static const struct dev_pm_ops dwc3_of_simple_dev_pm_ops
= {
189 SET_RUNTIME_PM_OPS(dwc3_of_simple_runtime_suspend
,
190 dwc3_of_simple_runtime_resume
, NULL
)
193 static const struct of_device_id of_dwc3_simple_match
[] = {
194 { .compatible
= "qcom,dwc3" },
195 { .compatible
= "rockchip,rk3399-dwc3" },
196 { .compatible
= "xlnx,zynqmp-dwc3" },
197 { .compatible
= "cavium,octeon-7130-usb-uctl" },
198 { .compatible
= "sprd,sc9860-dwc3" },
201 MODULE_DEVICE_TABLE(of
, of_dwc3_simple_match
);
203 static struct platform_driver dwc3_of_simple_driver
= {
204 .probe
= dwc3_of_simple_probe
,
205 .remove
= dwc3_of_simple_remove
,
207 .name
= "dwc3-of-simple",
208 .of_match_table
= of_dwc3_simple_match
,
209 .pm
= &dwc3_of_simple_dev_pm_ops
,
213 module_platform_driver(dwc3_of_simple_driver
);
214 MODULE_LICENSE("GPL v2");
215 MODULE_DESCRIPTION("DesignWare USB3 OF Simple Glue Layer");
216 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");