1 // SPDX-License-Identifier: GPL-2.0
2 /**
3  * dwc3-of-simple.c - OF glue layer for simple integrations
4  *
5  * Copyright (c) 2015 Texas Instruments Incorporated - http://www.ti.com
6  *
7  * Author: Felipe Balbi <balbi@ti.com>
8  *
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>
12  */
13 
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>
20 #include <linux/of.h>
21 #include <linux/of_platform.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/reset.h>
24 
25 struct dwc3_of_simple {
26 	struct device		*dev;
27 	struct clk_bulk_data	*clks;
28 	int			num_clocks;
29 	struct reset_control	*resets;
30 	bool			pulse_resets;
31 	bool			need_reset;
32 };
33 
34 static int dwc3_of_simple_probe(struct platform_device *pdev)
35 {
36 	struct dwc3_of_simple	*simple;
37 	struct device		*dev = &pdev->dev;
38 	struct device_node	*np = dev->of_node;
39 
40 	int			ret;
41 	bool			shared_resets = false;
42 
43 	simple = devm_kzalloc(dev, sizeof(*simple), GFP_KERNEL);
44 	if (!simple)
45 		return -ENOMEM;
46 
47 	platform_set_drvdata(pdev, simple);
48 	simple->dev = dev;
49 
50 	/*
51 	 * Some controllers need to toggle the usb3-otg reset before trying to
52 	 * initialize the PHY, otherwise the PHY times out.
53 	 */
54 	if (of_device_is_compatible(np, "rockchip,rk3399-dwc3"))
55 		simple->need_reset = true;
56 
57 	if (of_device_is_compatible(np, "amlogic,meson-axg-dwc3") ||
58 	    of_device_is_compatible(np, "amlogic,meson-gxl-dwc3")) {
59 		shared_resets = true;
60 		simple->pulse_resets = true;
61 	}
62 
63 	simple->resets = of_reset_control_array_get(np, shared_resets, true,
64 						    true);
65 	if (IS_ERR(simple->resets)) {
66 		ret = PTR_ERR(simple->resets);
67 		dev_err(dev, "failed to get device resets, err=%d\n", ret);
68 		return ret;
69 	}
70 
71 	if (simple->pulse_resets) {
72 		ret = reset_control_reset(simple->resets);
73 		if (ret)
74 			goto err_resetc_put;
75 	} else {
76 		ret = reset_control_deassert(simple->resets);
77 		if (ret)
78 			goto err_resetc_put;
79 	}
80 
81 	ret = clk_bulk_get_all(simple->dev, &simple->clks);
82 	if (ret < 0)
83 		goto err_resetc_assert;
84 
85 	simple->num_clocks = ret;
86 	ret = clk_bulk_prepare_enable(simple->num_clocks, simple->clks);
87 	if (ret)
88 		goto err_resetc_assert;
89 
90 	ret = of_platform_populate(np, NULL, NULL, dev);
91 	if (ret)
92 		goto err_clk_put;
93 
94 	pm_runtime_set_active(dev);
95 	pm_runtime_enable(dev);
96 	pm_runtime_get_sync(dev);
97 
98 	return 0;
99 
100 err_clk_put:
101 	clk_bulk_disable_unprepare(simple->num_clocks, simple->clks);
102 	clk_bulk_put_all(simple->num_clocks, simple->clks);
103 
104 err_resetc_assert:
105 	if (!simple->pulse_resets)
106 		reset_control_assert(simple->resets);
107 
108 err_resetc_put:
109 	reset_control_put(simple->resets);
110 	return ret;
111 }
112 
113 static void __dwc3_of_simple_teardown(struct dwc3_of_simple *simple)
114 {
115 	of_platform_depopulate(simple->dev);
116 
117 	clk_bulk_disable_unprepare(simple->num_clocks, simple->clks);
118 	clk_bulk_put_all(simple->num_clocks, simple->clks);
119 	simple->num_clocks = 0;
120 
121 	if (!simple->pulse_resets)
122 		reset_control_assert(simple->resets);
123 
124 	reset_control_put(simple->resets);
125 
126 	pm_runtime_disable(simple->dev);
127 	pm_runtime_put_noidle(simple->dev);
128 	pm_runtime_set_suspended(simple->dev);
129 }
130 
131 static int dwc3_of_simple_remove(struct platform_device *pdev)
132 {
133 	struct dwc3_of_simple	*simple = platform_get_drvdata(pdev);
134 
135 	__dwc3_of_simple_teardown(simple);
136 
137 	return 0;
138 }
139 
140 static void dwc3_of_simple_shutdown(struct platform_device *pdev)
141 {
142 	struct dwc3_of_simple	*simple = platform_get_drvdata(pdev);
143 
144 	__dwc3_of_simple_teardown(simple);
145 }
146 
147 static int __maybe_unused dwc3_of_simple_runtime_suspend(struct device *dev)
148 {
149 	struct dwc3_of_simple	*simple = dev_get_drvdata(dev);
150 
151 	clk_bulk_disable(simple->num_clocks, simple->clks);
152 
153 	return 0;
154 }
155 
156 static int __maybe_unused dwc3_of_simple_runtime_resume(struct device *dev)
157 {
158 	struct dwc3_of_simple	*simple = dev_get_drvdata(dev);
159 
160 	return clk_bulk_enable(simple->num_clocks, simple->clks);
161 }
162 
163 static int __maybe_unused dwc3_of_simple_suspend(struct device *dev)
164 {
165 	struct dwc3_of_simple *simple = dev_get_drvdata(dev);
166 
167 	if (simple->need_reset)
168 		reset_control_assert(simple->resets);
169 
170 	return 0;
171 }
172 
173 static int __maybe_unused dwc3_of_simple_resume(struct device *dev)
174 {
175 	struct dwc3_of_simple *simple = dev_get_drvdata(dev);
176 
177 	if (simple->need_reset)
178 		reset_control_deassert(simple->resets);
179 
180 	return 0;
181 }
182 
183 static const struct dev_pm_ops dwc3_of_simple_dev_pm_ops = {
184 	SET_SYSTEM_SLEEP_PM_OPS(dwc3_of_simple_suspend, dwc3_of_simple_resume)
185 	SET_RUNTIME_PM_OPS(dwc3_of_simple_runtime_suspend,
186 			dwc3_of_simple_runtime_resume, NULL)
187 };
188 
189 static const struct of_device_id of_dwc3_simple_match[] = {
190 	{ .compatible = "rockchip,rk3399-dwc3" },
191 	{ .compatible = "xlnx,zynqmp-dwc3" },
192 	{ .compatible = "cavium,octeon-7130-usb-uctl" },
193 	{ .compatible = "sprd,sc9860-dwc3" },
194 	{ .compatible = "amlogic,meson-axg-dwc3" },
195 	{ .compatible = "amlogic,meson-gxl-dwc3" },
196 	{ .compatible = "allwinner,sun50i-h6-dwc3" },
197 	{ /* Sentinel */ }
198 };
199 MODULE_DEVICE_TABLE(of, of_dwc3_simple_match);
200 
201 static struct platform_driver dwc3_of_simple_driver = {
202 	.probe		= dwc3_of_simple_probe,
203 	.remove		= dwc3_of_simple_remove,
204 	.shutdown	= dwc3_of_simple_shutdown,
205 	.driver		= {
206 		.name	= "dwc3-of-simple",
207 		.of_match_table = of_dwc3_simple_match,
208 		.pm	= &dwc3_of_simple_dev_pm_ops,
209 	},
210 };
211 
212 module_platform_driver(dwc3_of_simple_driver);
213 MODULE_LICENSE("GPL v2");
214 MODULE_DESCRIPTION("DesignWare USB3 OF Simple Glue Layer");
215 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
216