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 - https://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 {
27 struct clk_bulk_data *clks;
29 struct reset_control *resets;
33 static int dwc3_of_simple_probe(struct platform_device *pdev)
35 struct dwc3_of_simple *simple;
36 struct device *dev = &pdev->dev;
37 struct device_node *np = dev->of_node;
41 simple = devm_kzalloc(dev, sizeof(*simple), GFP_KERNEL);
45 platform_set_drvdata(pdev, simple);
49 * Some controllers need to toggle the usb3-otg reset before trying to
50 * initialize the PHY, otherwise the PHY times out.
52 if (of_device_is_compatible(np, "rockchip,rk3399-dwc3"))
53 simple->need_reset = true;
55 simple->resets = of_reset_control_array_get_optional_exclusive(np);
56 if (IS_ERR(simple->resets)) {
57 ret = PTR_ERR(simple->resets);
58 dev_err(dev, "failed to get device resets, err=%d\n", ret);
62 ret = reset_control_deassert(simple->resets);
66 ret = clk_bulk_get_all(simple->dev, &simple->clks);
68 goto err_resetc_assert;
70 simple->num_clocks = ret;
71 ret = clk_bulk_prepare_enable(simple->num_clocks, simple->clks);
73 goto err_resetc_assert;
75 ret = of_platform_populate(np, NULL, NULL, dev);
79 pm_runtime_set_active(dev);
80 pm_runtime_enable(dev);
81 pm_runtime_get_sync(dev);
86 clk_bulk_disable_unprepare(simple->num_clocks, simple->clks);
87 clk_bulk_put_all(simple->num_clocks, simple->clks);
90 reset_control_assert(simple->resets);
93 reset_control_put(simple->resets);
97 static void __dwc3_of_simple_teardown(struct dwc3_of_simple *simple)
99 of_platform_depopulate(simple->dev);
101 clk_bulk_disable_unprepare(simple->num_clocks, simple->clks);
102 clk_bulk_put_all(simple->num_clocks, simple->clks);
103 simple->num_clocks = 0;
105 reset_control_assert(simple->resets);
107 reset_control_put(simple->resets);
109 pm_runtime_disable(simple->dev);
110 pm_runtime_put_noidle(simple->dev);
111 pm_runtime_set_suspended(simple->dev);
114 static void dwc3_of_simple_remove(struct platform_device *pdev)
116 struct dwc3_of_simple *simple = platform_get_drvdata(pdev);
118 __dwc3_of_simple_teardown(simple);
121 static void dwc3_of_simple_shutdown(struct platform_device *pdev)
123 struct dwc3_of_simple *simple = platform_get_drvdata(pdev);
125 __dwc3_of_simple_teardown(simple);
128 static int __maybe_unused dwc3_of_simple_runtime_suspend(struct device *dev)
130 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
132 clk_bulk_disable(simple->num_clocks, simple->clks);
137 static int __maybe_unused dwc3_of_simple_runtime_resume(struct device *dev)
139 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
141 return clk_bulk_enable(simple->num_clocks, simple->clks);
144 static int __maybe_unused dwc3_of_simple_suspend(struct device *dev)
146 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
148 if (simple->need_reset)
149 reset_control_assert(simple->resets);
154 static int __maybe_unused dwc3_of_simple_resume(struct device *dev)
156 struct dwc3_of_simple *simple = dev_get_drvdata(dev);
158 if (simple->need_reset)
159 reset_control_deassert(simple->resets);
164 static const struct dev_pm_ops dwc3_of_simple_dev_pm_ops = {
165 SET_SYSTEM_SLEEP_PM_OPS(dwc3_of_simple_suspend, dwc3_of_simple_resume)
166 SET_RUNTIME_PM_OPS(dwc3_of_simple_runtime_suspend,
167 dwc3_of_simple_runtime_resume, NULL)
170 static const struct of_device_id of_dwc3_simple_match[] = {
171 { .compatible = "rockchip,rk3399-dwc3" },
172 { .compatible = "sprd,sc9860-dwc3" },
173 { .compatible = "allwinner,sun50i-h6-dwc3" },
174 { .compatible = "hisilicon,hi3670-dwc3" },
175 { .compatible = "hisilicon,hi3798mv200-dwc3" },
176 { .compatible = "intel,keembay-dwc3" },
179 MODULE_DEVICE_TABLE(of, of_dwc3_simple_match);
181 static struct platform_driver dwc3_of_simple_driver = {
182 .probe = dwc3_of_simple_probe,
183 .remove_new = dwc3_of_simple_remove,
184 .shutdown = dwc3_of_simple_shutdown,
186 .name = "dwc3-of-simple",
187 .of_match_table = of_dwc3_simple_match,
188 .pm = &dwc3_of_simple_dev_pm_ops,
192 module_platform_driver(dwc3_of_simple_driver);
193 MODULE_LICENSE("GPL v2");
194 MODULE_DESCRIPTION("DesignWare USB3 OF Simple Glue Layer");
195 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");