1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2017 Icenowy Zheng <icenowy@aosc.io>
4  */
5 
6 #include <linux/clk.h>
7 #include <linux/clk-provider.h>
8 #include <linux/module.h>
9 #include <linux/of_device.h>
10 #include <linux/platform_device.h>
11 #include <linux/reset.h>
12 
13 #include "ccu_common.h"
14 #include "ccu_div.h"
15 #include "ccu_gate.h"
16 #include "ccu_reset.h"
17 
18 #include "ccu-sun8i-de2.h"
19 
20 static SUNXI_CCU_GATE(bus_mixer0_clk,	"bus-mixer0",	"bus-de",
21 		      0x04, BIT(0), 0);
22 static SUNXI_CCU_GATE(bus_mixer1_clk,	"bus-mixer1",	"bus-de",
23 		      0x04, BIT(1), 0);
24 static SUNXI_CCU_GATE(bus_wb_clk,	"bus-wb",	"bus-de",
25 		      0x04, BIT(2), 0);
26 static SUNXI_CCU_GATE(bus_rot_clk,	"bus-rot",	"bus-de",
27 		      0x04, BIT(3), 0);
28 
29 static SUNXI_CCU_GATE(mixer0_clk,	"mixer0",	"mixer0-div",
30 		      0x00, BIT(0), CLK_SET_RATE_PARENT);
31 static SUNXI_CCU_GATE(mixer1_clk,	"mixer1",	"mixer1-div",
32 		      0x00, BIT(1), CLK_SET_RATE_PARENT);
33 static SUNXI_CCU_GATE(wb_clk,		"wb",		"wb-div",
34 		      0x00, BIT(2), CLK_SET_RATE_PARENT);
35 static SUNXI_CCU_GATE(rot_clk,		"rot",		"rot-div",
36 		      0x00, BIT(3), CLK_SET_RATE_PARENT);
37 
38 static SUNXI_CCU_M(mixer0_div_clk, "mixer0-div", "de", 0x0c, 0, 4,
39 		   CLK_SET_RATE_PARENT);
40 static SUNXI_CCU_M(mixer1_div_clk, "mixer1-div", "de", 0x0c, 4, 4,
41 		   CLK_SET_RATE_PARENT);
42 static SUNXI_CCU_M(wb_div_clk, "wb-div", "de", 0x0c, 8, 4,
43 		   CLK_SET_RATE_PARENT);
44 static SUNXI_CCU_M(rot_div_clk, "rot-div", "de", 0x0c, 0x0c, 4,
45 		   CLK_SET_RATE_PARENT);
46 
47 static SUNXI_CCU_M(mixer0_div_a83_clk, "mixer0-div", "pll-de", 0x0c, 0, 4,
48 		   CLK_SET_RATE_PARENT);
49 static SUNXI_CCU_M(mixer1_div_a83_clk, "mixer1-div", "pll-de", 0x0c, 4, 4,
50 		   CLK_SET_RATE_PARENT);
51 static SUNXI_CCU_M(wb_div_a83_clk, "wb-div", "pll-de", 0x0c, 8, 4,
52 		   CLK_SET_RATE_PARENT);
53 static SUNXI_CCU_M(rot_div_a83_clk, "rot-div", "pll-de", 0x0c, 0x0c, 4,
54 		   CLK_SET_RATE_PARENT);
55 
56 static struct ccu_common *sun8i_de2_ccu_clks[] = {
57 	&mixer0_clk.common,
58 	&mixer1_clk.common,
59 	&wb_clk.common,
60 	&rot_clk.common,
61 
62 	&bus_mixer0_clk.common,
63 	&bus_mixer1_clk.common,
64 	&bus_wb_clk.common,
65 	&bus_rot_clk.common,
66 
67 	&mixer0_div_clk.common,
68 	&mixer1_div_clk.common,
69 	&wb_div_clk.common,
70 	&rot_div_clk.common,
71 
72 	&mixer0_div_a83_clk.common,
73 	&mixer1_div_a83_clk.common,
74 	&wb_div_a83_clk.common,
75 	&rot_div_a83_clk.common,
76 };
77 
78 static struct clk_hw_onecell_data sun8i_a83t_de2_hw_clks = {
79 	.hws	= {
80 		[CLK_MIXER0]		= &mixer0_clk.common.hw,
81 		[CLK_MIXER1]		= &mixer1_clk.common.hw,
82 		[CLK_WB]		= &wb_clk.common.hw,
83 		[CLK_ROT]		= &rot_clk.common.hw,
84 
85 		[CLK_BUS_MIXER0]	= &bus_mixer0_clk.common.hw,
86 		[CLK_BUS_MIXER1]	= &bus_mixer1_clk.common.hw,
87 		[CLK_BUS_WB]		= &bus_wb_clk.common.hw,
88 		[CLK_BUS_ROT]		= &bus_rot_clk.common.hw,
89 
90 		[CLK_MIXER0_DIV]	= &mixer0_div_a83_clk.common.hw,
91 		[CLK_MIXER1_DIV]	= &mixer1_div_a83_clk.common.hw,
92 		[CLK_WB_DIV]		= &wb_div_a83_clk.common.hw,
93 		[CLK_ROT_DIV]		= &rot_div_a83_clk.common.hw,
94 	},
95 	.num	= CLK_NUMBER_WITH_ROT,
96 };
97 
98 static struct clk_hw_onecell_data sun8i_h3_de2_hw_clks = {
99 	.hws	= {
100 		[CLK_MIXER0]		= &mixer0_clk.common.hw,
101 		[CLK_MIXER1]		= &mixer1_clk.common.hw,
102 		[CLK_WB]		= &wb_clk.common.hw,
103 
104 		[CLK_BUS_MIXER0]	= &bus_mixer0_clk.common.hw,
105 		[CLK_BUS_MIXER1]	= &bus_mixer1_clk.common.hw,
106 		[CLK_BUS_WB]		= &bus_wb_clk.common.hw,
107 
108 		[CLK_MIXER0_DIV]	= &mixer0_div_clk.common.hw,
109 		[CLK_MIXER1_DIV]	= &mixer1_div_clk.common.hw,
110 		[CLK_WB_DIV]		= &wb_div_clk.common.hw,
111 	},
112 	.num	= CLK_NUMBER_WITHOUT_ROT,
113 };
114 
115 static struct clk_hw_onecell_data sun8i_v3s_de2_hw_clks = {
116 	.hws	= {
117 		[CLK_MIXER0]		= &mixer0_clk.common.hw,
118 		[CLK_WB]		= &wb_clk.common.hw,
119 
120 		[CLK_BUS_MIXER0]	= &bus_mixer0_clk.common.hw,
121 		[CLK_BUS_WB]		= &bus_wb_clk.common.hw,
122 
123 		[CLK_MIXER0_DIV]	= &mixer0_div_clk.common.hw,
124 		[CLK_WB_DIV]		= &wb_div_clk.common.hw,
125 	},
126 	.num	= CLK_NUMBER_WITHOUT_ROT,
127 };
128 
129 static struct clk_hw_onecell_data sun50i_a64_de2_hw_clks = {
130 	.hws	= {
131 		[CLK_MIXER0]		= &mixer0_clk.common.hw,
132 		[CLK_MIXER1]		= &mixer1_clk.common.hw,
133 		[CLK_WB]		= &wb_clk.common.hw,
134 		[CLK_ROT]		= &rot_clk.common.hw,
135 
136 		[CLK_BUS_MIXER0]	= &bus_mixer0_clk.common.hw,
137 		[CLK_BUS_MIXER1]	= &bus_mixer1_clk.common.hw,
138 		[CLK_BUS_WB]		= &bus_wb_clk.common.hw,
139 		[CLK_BUS_ROT]		= &bus_rot_clk.common.hw,
140 
141 		[CLK_MIXER0_DIV]	= &mixer0_div_clk.common.hw,
142 		[CLK_MIXER1_DIV]	= &mixer1_div_clk.common.hw,
143 		[CLK_WB_DIV]		= &wb_div_clk.common.hw,
144 		[CLK_ROT_DIV]		= &rot_div_clk.common.hw,
145 	},
146 	.num	= CLK_NUMBER_WITH_ROT,
147 };
148 
149 static struct ccu_reset_map sun8i_a83t_de2_resets[] = {
150 	[RST_MIXER0]	= { 0x08, BIT(0) },
151 	/*
152 	 * Mixer1 reset line is shared with wb, so only RST_WB is
153 	 * exported here.
154 	 */
155 	[RST_WB]	= { 0x08, BIT(2) },
156 	[RST_ROT]	= { 0x08, BIT(3) },
157 };
158 
159 static struct ccu_reset_map sun8i_h3_de2_resets[] = {
160 	[RST_MIXER0]	= { 0x08, BIT(0) },
161 	/*
162 	 * Mixer1 reset line is shared with wb, so only RST_WB is
163 	 * exported here.
164 	 * V3s doesn't have mixer1, so it also shares this struct.
165 	 */
166 	[RST_WB]	= { 0x08, BIT(2) },
167 };
168 
169 static struct ccu_reset_map sun50i_a64_de2_resets[] = {
170 	[RST_MIXER0]	= { 0x08, BIT(0) },
171 	[RST_MIXER1]	= { 0x08, BIT(1) },
172 	[RST_WB]	= { 0x08, BIT(2) },
173 	[RST_ROT]	= { 0x08, BIT(3) },
174 };
175 
176 static struct ccu_reset_map sun50i_h5_de2_resets[] = {
177 	[RST_MIXER0]	= { 0x08, BIT(0) },
178 	[RST_MIXER1]	= { 0x08, BIT(1) },
179 	[RST_WB]	= { 0x08, BIT(2) },
180 };
181 
182 static const struct sunxi_ccu_desc sun8i_a83t_de2_clk_desc = {
183 	.ccu_clks	= sun8i_de2_ccu_clks,
184 	.num_ccu_clks	= ARRAY_SIZE(sun8i_de2_ccu_clks),
185 
186 	.hw_clks	= &sun8i_a83t_de2_hw_clks,
187 
188 	.resets		= sun8i_a83t_de2_resets,
189 	.num_resets	= ARRAY_SIZE(sun8i_a83t_de2_resets),
190 };
191 
192 static const struct sunxi_ccu_desc sun8i_h3_de2_clk_desc = {
193 	.ccu_clks	= sun8i_de2_ccu_clks,
194 	.num_ccu_clks	= ARRAY_SIZE(sun8i_de2_ccu_clks),
195 
196 	.hw_clks	= &sun8i_h3_de2_hw_clks,
197 
198 	.resets		= sun8i_h3_de2_resets,
199 	.num_resets	= ARRAY_SIZE(sun8i_h3_de2_resets),
200 };
201 
202 static const struct sunxi_ccu_desc sun8i_r40_de2_clk_desc = {
203 	.ccu_clks	= sun8i_de2_ccu_clks,
204 	.num_ccu_clks	= ARRAY_SIZE(sun8i_de2_ccu_clks),
205 
206 	.hw_clks	= &sun50i_a64_de2_hw_clks,
207 
208 	.resets		= sun8i_a83t_de2_resets,
209 	.num_resets	= ARRAY_SIZE(sun8i_a83t_de2_resets),
210 };
211 
212 static const struct sunxi_ccu_desc sun8i_v3s_de2_clk_desc = {
213 	.ccu_clks	= sun8i_de2_ccu_clks,
214 	.num_ccu_clks	= ARRAY_SIZE(sun8i_de2_ccu_clks),
215 
216 	.hw_clks	= &sun8i_v3s_de2_hw_clks,
217 
218 	.resets		= sun8i_a83t_de2_resets,
219 	.num_resets	= ARRAY_SIZE(sun8i_a83t_de2_resets),
220 };
221 
222 static const struct sunxi_ccu_desc sun50i_a64_de2_clk_desc = {
223 	.ccu_clks	= sun8i_de2_ccu_clks,
224 	.num_ccu_clks	= ARRAY_SIZE(sun8i_de2_ccu_clks),
225 
226 	.hw_clks	= &sun50i_a64_de2_hw_clks,
227 
228 	.resets		= sun50i_a64_de2_resets,
229 	.num_resets	= ARRAY_SIZE(sun50i_a64_de2_resets),
230 };
231 
232 static const struct sunxi_ccu_desc sun50i_h5_de2_clk_desc = {
233 	.ccu_clks	= sun8i_de2_ccu_clks,
234 	.num_ccu_clks	= ARRAY_SIZE(sun8i_de2_ccu_clks),
235 
236 	.hw_clks	= &sun8i_h3_de2_hw_clks,
237 
238 	.resets		= sun50i_h5_de2_resets,
239 	.num_resets	= ARRAY_SIZE(sun50i_h5_de2_resets),
240 };
241 
242 static int sunxi_de2_clk_probe(struct platform_device *pdev)
243 {
244 	struct clk *bus_clk, *mod_clk;
245 	struct reset_control *rstc;
246 	void __iomem *reg;
247 	const struct sunxi_ccu_desc *ccu_desc;
248 	int ret;
249 
250 	ccu_desc = of_device_get_match_data(&pdev->dev);
251 	if (!ccu_desc)
252 		return -EINVAL;
253 
254 	reg = devm_platform_ioremap_resource(pdev, 0);
255 	if (IS_ERR(reg))
256 		return PTR_ERR(reg);
257 
258 	bus_clk = devm_clk_get(&pdev->dev, "bus");
259 	if (IS_ERR(bus_clk)) {
260 		ret = PTR_ERR(bus_clk);
261 		if (ret != -EPROBE_DEFER)
262 			dev_err(&pdev->dev, "Couldn't get bus clk: %d\n", ret);
263 		return ret;
264 	}
265 
266 	mod_clk = devm_clk_get(&pdev->dev, "mod");
267 	if (IS_ERR(mod_clk)) {
268 		ret = PTR_ERR(mod_clk);
269 		if (ret != -EPROBE_DEFER)
270 			dev_err(&pdev->dev, "Couldn't get mod clk: %d\n", ret);
271 		return ret;
272 	}
273 
274 	rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
275 	if (IS_ERR(rstc)) {
276 		ret = PTR_ERR(rstc);
277 		if (ret != -EPROBE_DEFER)
278 			dev_err(&pdev->dev,
279 				"Couldn't get reset control: %d\n", ret);
280 		return ret;
281 	}
282 
283 	/* The clocks need to be enabled for us to access the registers */
284 	ret = clk_prepare_enable(bus_clk);
285 	if (ret) {
286 		dev_err(&pdev->dev, "Couldn't enable bus clk: %d\n", ret);
287 		return ret;
288 	}
289 
290 	ret = clk_prepare_enable(mod_clk);
291 	if (ret) {
292 		dev_err(&pdev->dev, "Couldn't enable mod clk: %d\n", ret);
293 		goto err_disable_bus_clk;
294 	}
295 
296 	/* The reset control needs to be asserted for the controls to work */
297 	ret = reset_control_deassert(rstc);
298 	if (ret) {
299 		dev_err(&pdev->dev,
300 			"Couldn't deassert reset control: %d\n", ret);
301 		goto err_disable_mod_clk;
302 	}
303 
304 	ret = devm_sunxi_ccu_probe(&pdev->dev, reg, ccu_desc);
305 	if (ret)
306 		goto err_assert_reset;
307 
308 	return 0;
309 
310 err_assert_reset:
311 	reset_control_assert(rstc);
312 err_disable_mod_clk:
313 	clk_disable_unprepare(mod_clk);
314 err_disable_bus_clk:
315 	clk_disable_unprepare(bus_clk);
316 	return ret;
317 }
318 
319 static const struct of_device_id sunxi_de2_clk_ids[] = {
320 	{
321 		.compatible = "allwinner,sun8i-a83t-de2-clk",
322 		.data = &sun8i_a83t_de2_clk_desc,
323 	},
324 	{
325 		.compatible = "allwinner,sun8i-h3-de2-clk",
326 		.data = &sun8i_h3_de2_clk_desc,
327 	},
328 	{
329 		.compatible = "allwinner,sun8i-r40-de2-clk",
330 		.data = &sun8i_r40_de2_clk_desc,
331 	},
332 	{
333 		.compatible = "allwinner,sun8i-v3s-de2-clk",
334 		.data = &sun8i_v3s_de2_clk_desc,
335 	},
336 	{
337 		.compatible = "allwinner,sun50i-a64-de2-clk",
338 		.data = &sun50i_a64_de2_clk_desc,
339 	},
340 	{
341 		.compatible = "allwinner,sun50i-h5-de2-clk",
342 		.data = &sun50i_h5_de2_clk_desc,
343 	},
344 	{
345 		.compatible = "allwinner,sun50i-h6-de3-clk",
346 		.data = &sun50i_h5_de2_clk_desc,
347 	},
348 	{ }
349 };
350 
351 static struct platform_driver sunxi_de2_clk_driver = {
352 	.probe	= sunxi_de2_clk_probe,
353 	.driver	= {
354 		.name	= "sunxi-de2-clks",
355 		.of_match_table	= sunxi_de2_clk_ids,
356 	},
357 };
358 module_platform_driver(sunxi_de2_clk_driver);
359 
360 MODULE_IMPORT_NS(SUNXI_CCU);
361 MODULE_LICENSE("GPL");
362