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
9 * This is a combination of the old dwc3-qcom.c by Ivan T. Ivanov
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_put(simple->clks[i]);
59 ret = clk_prepare_enable(clk);
62 clk_disable_unprepare(simple->clks[i]);
63 clk_put(simple->clks[i]);
70 simple->clks[i] = clk;
76 static int dwc3_of_simple_probe(struct platform_device *pdev)
78 struct dwc3_of_simple *simple;
79 struct device *dev = &pdev->dev;
80 struct device_node *np = dev->of_node;
85 simple = devm_kzalloc(dev, sizeof(*simple), GFP_KERNEL);
89 platform_set_drvdata(pdev, simple);
92 simple->resets = of_reset_control_array_get_optional_exclusive(np);
93 if (IS_ERR(simple->resets)) {
94 ret = PTR_ERR(simple->resets);
95 dev_err(dev, "failed to get device resets, err=%d\n", ret);
99 ret = reset_control_deassert(simple->resets);
103 ret = dwc3_of_simple_clk_init(simple, of_count_phandle_with_args(np,
104 "clocks", "#clock-cells"));
106 goto err_resetc_assert;
108 ret = of_platform_populate(np, NULL, NULL, dev);
110 for (i = 0; i < simple->num_clocks; i++) {
111 clk_disable_unprepare(simple->clks[i]);
112 clk_put(simple->clks[i]);
115 goto err_resetc_assert;
118 pm_runtime_set_active(dev);
119 pm_runtime_enable(dev);
120 pm_runtime_get_sync(dev);
125 reset_control_assert(simple->resets);
128 reset_control_put(simple->resets);
132 static int dwc3_of_simple_remove(struct platform_device *pdev)
134 struct dwc3_of_simple *simple = platform_get_drvdata(pdev);
135 struct device *dev = &pdev->dev;
138 of_platform_depopulate(dev);
140 for (i = 0; i < simple->num_clocks; i++) {
141 clk_disable_unprepare(simple->clks[i]);
142 clk_put(simple->clks[i]);
145 reset_control_assert(simple->resets);
146 reset_control_put(simple->resets);
148 pm_runtime_put_sync(dev);
149 pm_runtime_disable(dev);
155 static int dwc3_of_simple_runtime_suspend(struct device *dev)
157 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
160 for (i = 0; i < simple->num_clocks; i++)
161 clk_disable(simple->clks[i]);
166 static int dwc3_of_simple_runtime_resume(struct device *dev)
168 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
172 for (i = 0; i < simple->num_clocks; i++) {
173 ret = clk_enable(simple->clks[i]);
176 clk_disable(simple->clks[i]);
185 static const struct dev_pm_ops dwc3_of_simple_dev_pm_ops = {
186 SET_RUNTIME_PM_OPS(dwc3_of_simple_runtime_suspend,
187 dwc3_of_simple_runtime_resume, NULL)
190 static const struct of_device_id of_dwc3_simple_match[] = {
191 { .compatible = "qcom,dwc3" },
192 { .compatible = "rockchip,rk3399-dwc3" },
193 { .compatible = "xlnx,zynqmp-dwc3" },
194 { .compatible = "cavium,octeon-7130-usb-uctl" },
195 { .compatible = "sprd,sc9860-dwc3" },
198 MODULE_DEVICE_TABLE(of, of_dwc3_simple_match);
200 static struct platform_driver dwc3_of_simple_driver = {
201 .probe = dwc3_of_simple_probe,
202 .remove = dwc3_of_simple_remove,
204 .name = "dwc3-of-simple",
205 .of_match_table = of_dwc3_simple_match,
209 module_platform_driver(dwc3_of_simple_driver);
210 MODULE_LICENSE("GPL v2");
211 MODULE_DESCRIPTION("DesignWare USB3 OF Simple Glue Layer");