1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2017 Icenowy Zheng <icenowy@aosc.io>
7 #include <linux/clk-provider.h>
8 #include <linux/of_address.h>
9 #include <linux/of_platform.h>
10 #include <linux/platform_device.h>
11 #include <linux/reset.h>
13 #include "ccu_common.h"
16 #include "ccu_reset.h"
18 #include "ccu-sun8i-de2.h"
20 static SUNXI_CCU_GATE(bus_mixer0_clk
, "bus-mixer0", "bus-de",
22 static SUNXI_CCU_GATE(bus_mixer1_clk
, "bus-mixer1", "bus-de",
24 static SUNXI_CCU_GATE(bus_wb_clk
, "bus-wb", "bus-de",
26 static SUNXI_CCU_GATE(bus_rot_clk
, "bus-rot", "bus-de",
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
);
38 static SUNXI_CCU_M(mixer0_div_clk
, "mixer0-div", "de", 0x0c, 0, 4,
40 static SUNXI_CCU_M(mixer1_div_clk
, "mixer1-div", "de", 0x0c, 4, 4,
42 static SUNXI_CCU_M(wb_div_clk
, "wb-div", "de", 0x0c, 8, 4,
44 static SUNXI_CCU_M(rot_div_clk
, "rot-div", "de", 0x0c, 0x0c, 4,
47 static SUNXI_CCU_M(mixer0_div_a83_clk
, "mixer0-div", "pll-de", 0x0c, 0, 4,
49 static SUNXI_CCU_M(mixer1_div_a83_clk
, "mixer1-div", "pll-de", 0x0c, 4, 4,
51 static SUNXI_CCU_M(wb_div_a83_clk
, "wb-div", "pll-de", 0x0c, 8, 4,
53 static SUNXI_CCU_M(rot_div_a83_clk
, "rot-div", "pll-de", 0x0c, 0x0c, 4,
56 static struct ccu_common
*sun8i_a83t_de2_clks
[] = {
61 &bus_mixer0_clk
.common
,
62 &bus_mixer1_clk
.common
,
65 &mixer0_div_a83_clk
.common
,
66 &mixer1_div_a83_clk
.common
,
67 &wb_div_a83_clk
.common
,
71 &rot_div_a83_clk
.common
,
74 static struct ccu_common
*sun8i_h3_de2_clks
[] = {
79 &bus_mixer0_clk
.common
,
80 &bus_mixer1_clk
.common
,
83 &mixer0_div_clk
.common
,
84 &mixer1_div_clk
.common
,
88 static struct ccu_common
*sun8i_v3s_de2_clks
[] = {
92 &bus_mixer0_clk
.common
,
95 &mixer0_div_clk
.common
,
99 static struct ccu_common
*sun50i_a64_de2_clks
[] = {
104 &bus_mixer0_clk
.common
,
105 &bus_mixer1_clk
.common
,
108 &mixer0_div_clk
.common
,
109 &mixer1_div_clk
.common
,
117 static struct clk_hw_onecell_data sun8i_a83t_de2_hw_clks
= {
119 [CLK_MIXER0
] = &mixer0_clk
.common
.hw
,
120 [CLK_MIXER1
] = &mixer1_clk
.common
.hw
,
121 [CLK_WB
] = &wb_clk
.common
.hw
,
122 [CLK_ROT
] = &rot_clk
.common
.hw
,
124 [CLK_BUS_MIXER0
] = &bus_mixer0_clk
.common
.hw
,
125 [CLK_BUS_MIXER1
] = &bus_mixer1_clk
.common
.hw
,
126 [CLK_BUS_WB
] = &bus_wb_clk
.common
.hw
,
127 [CLK_BUS_ROT
] = &bus_rot_clk
.common
.hw
,
129 [CLK_MIXER0_DIV
] = &mixer0_div_a83_clk
.common
.hw
,
130 [CLK_MIXER1_DIV
] = &mixer1_div_a83_clk
.common
.hw
,
131 [CLK_WB_DIV
] = &wb_div_a83_clk
.common
.hw
,
132 [CLK_ROT_DIV
] = &rot_div_a83_clk
.common
.hw
,
134 .num
= CLK_NUMBER_WITH_ROT
,
137 static struct clk_hw_onecell_data sun8i_h3_de2_hw_clks
= {
139 [CLK_MIXER0
] = &mixer0_clk
.common
.hw
,
140 [CLK_MIXER1
] = &mixer1_clk
.common
.hw
,
141 [CLK_WB
] = &wb_clk
.common
.hw
,
143 [CLK_BUS_MIXER0
] = &bus_mixer0_clk
.common
.hw
,
144 [CLK_BUS_MIXER1
] = &bus_mixer1_clk
.common
.hw
,
145 [CLK_BUS_WB
] = &bus_wb_clk
.common
.hw
,
147 [CLK_MIXER0_DIV
] = &mixer0_div_clk
.common
.hw
,
148 [CLK_MIXER1_DIV
] = &mixer1_div_clk
.common
.hw
,
149 [CLK_WB_DIV
] = &wb_div_clk
.common
.hw
,
151 .num
= CLK_NUMBER_WITHOUT_ROT
,
154 static struct clk_hw_onecell_data sun8i_v3s_de2_hw_clks
= {
156 [CLK_MIXER0
] = &mixer0_clk
.common
.hw
,
157 [CLK_WB
] = &wb_clk
.common
.hw
,
159 [CLK_BUS_MIXER0
] = &bus_mixer0_clk
.common
.hw
,
160 [CLK_BUS_WB
] = &bus_wb_clk
.common
.hw
,
162 [CLK_MIXER0_DIV
] = &mixer0_div_clk
.common
.hw
,
163 [CLK_WB_DIV
] = &wb_div_clk
.common
.hw
,
165 .num
= CLK_NUMBER_WITHOUT_ROT
,
168 static struct clk_hw_onecell_data sun50i_a64_de2_hw_clks
= {
170 [CLK_MIXER0
] = &mixer0_clk
.common
.hw
,
171 [CLK_MIXER1
] = &mixer1_clk
.common
.hw
,
172 [CLK_WB
] = &wb_clk
.common
.hw
,
173 [CLK_ROT
] = &rot_clk
.common
.hw
,
175 [CLK_BUS_MIXER0
] = &bus_mixer0_clk
.common
.hw
,
176 [CLK_BUS_MIXER1
] = &bus_mixer1_clk
.common
.hw
,
177 [CLK_BUS_WB
] = &bus_wb_clk
.common
.hw
,
178 [CLK_BUS_ROT
] = &bus_rot_clk
.common
.hw
,
180 [CLK_MIXER0_DIV
] = &mixer0_div_clk
.common
.hw
,
181 [CLK_MIXER1_DIV
] = &mixer1_div_clk
.common
.hw
,
182 [CLK_WB_DIV
] = &wb_div_clk
.common
.hw
,
183 [CLK_ROT_DIV
] = &rot_div_clk
.common
.hw
,
185 .num
= CLK_NUMBER_WITH_ROT
,
188 static struct ccu_reset_map sun8i_a83t_de2_resets
[] = {
189 [RST_MIXER0
] = { 0x08, BIT(0) },
191 * Mixer1 reset line is shared with wb, so only RST_WB is
194 [RST_WB
] = { 0x08, BIT(2) },
195 [RST_ROT
] = { 0x08, BIT(3) },
198 static struct ccu_reset_map sun8i_h3_de2_resets
[] = {
199 [RST_MIXER0
] = { 0x08, BIT(0) },
201 * Mixer1 reset line is shared with wb, so only RST_WB is
203 * V3s doesn't have mixer1, so it also shares this struct.
205 [RST_WB
] = { 0x08, BIT(2) },
208 static struct ccu_reset_map sun50i_a64_de2_resets
[] = {
209 [RST_MIXER0
] = { 0x08, BIT(0) },
210 [RST_MIXER1
] = { 0x08, BIT(1) },
211 [RST_WB
] = { 0x08, BIT(2) },
212 [RST_ROT
] = { 0x08, BIT(3) },
215 static struct ccu_reset_map sun50i_h5_de2_resets
[] = {
216 [RST_MIXER0
] = { 0x08, BIT(0) },
217 [RST_MIXER1
] = { 0x08, BIT(1) },
218 [RST_WB
] = { 0x08, BIT(2) },
221 static const struct sunxi_ccu_desc sun8i_a83t_de2_clk_desc
= {
222 .ccu_clks
= sun8i_a83t_de2_clks
,
223 .num_ccu_clks
= ARRAY_SIZE(sun8i_a83t_de2_clks
),
225 .hw_clks
= &sun8i_a83t_de2_hw_clks
,
227 .resets
= sun8i_a83t_de2_resets
,
228 .num_resets
= ARRAY_SIZE(sun8i_a83t_de2_resets
),
231 static const struct sunxi_ccu_desc sun8i_h3_de2_clk_desc
= {
232 .ccu_clks
= sun8i_h3_de2_clks
,
233 .num_ccu_clks
= ARRAY_SIZE(sun8i_h3_de2_clks
),
235 .hw_clks
= &sun8i_h3_de2_hw_clks
,
237 .resets
= sun8i_h3_de2_resets
,
238 .num_resets
= ARRAY_SIZE(sun8i_h3_de2_resets
),
241 static const struct sunxi_ccu_desc sun8i_r40_de2_clk_desc
= {
242 .ccu_clks
= sun50i_a64_de2_clks
,
243 .num_ccu_clks
= ARRAY_SIZE(sun50i_a64_de2_clks
),
245 .hw_clks
= &sun50i_a64_de2_hw_clks
,
247 .resets
= sun8i_a83t_de2_resets
,
248 .num_resets
= ARRAY_SIZE(sun8i_a83t_de2_resets
),
251 static const struct sunxi_ccu_desc sun8i_v3s_de2_clk_desc
= {
252 .ccu_clks
= sun8i_v3s_de2_clks
,
253 .num_ccu_clks
= ARRAY_SIZE(sun8i_v3s_de2_clks
),
255 .hw_clks
= &sun8i_v3s_de2_hw_clks
,
257 .resets
= sun8i_a83t_de2_resets
,
258 .num_resets
= ARRAY_SIZE(sun8i_a83t_de2_resets
),
261 static const struct sunxi_ccu_desc sun50i_a64_de2_clk_desc
= {
262 .ccu_clks
= sun50i_a64_de2_clks
,
263 .num_ccu_clks
= ARRAY_SIZE(sun50i_a64_de2_clks
),
265 .hw_clks
= &sun50i_a64_de2_hw_clks
,
267 .resets
= sun50i_a64_de2_resets
,
268 .num_resets
= ARRAY_SIZE(sun50i_a64_de2_resets
),
271 static const struct sunxi_ccu_desc sun50i_h5_de2_clk_desc
= {
272 .ccu_clks
= sun8i_h3_de2_clks
,
273 .num_ccu_clks
= ARRAY_SIZE(sun8i_h3_de2_clks
),
275 .hw_clks
= &sun8i_h3_de2_hw_clks
,
277 .resets
= sun50i_h5_de2_resets
,
278 .num_resets
= ARRAY_SIZE(sun50i_h5_de2_resets
),
281 static int sunxi_de2_clk_probe(struct platform_device
*pdev
)
283 struct resource
*res
;
284 struct clk
*bus_clk
, *mod_clk
;
285 struct reset_control
*rstc
;
287 const struct sunxi_ccu_desc
*ccu_desc
;
290 ccu_desc
= of_device_get_match_data(&pdev
->dev
);
294 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
295 reg
= devm_ioremap_resource(&pdev
->dev
, res
);
299 bus_clk
= devm_clk_get(&pdev
->dev
, "bus");
300 if (IS_ERR(bus_clk
)) {
301 ret
= PTR_ERR(bus_clk
);
302 if (ret
!= -EPROBE_DEFER
)
303 dev_err(&pdev
->dev
, "Couldn't get bus clk: %d\n", ret
);
307 mod_clk
= devm_clk_get(&pdev
->dev
, "mod");
308 if (IS_ERR(mod_clk
)) {
309 ret
= PTR_ERR(mod_clk
);
310 if (ret
!= -EPROBE_DEFER
)
311 dev_err(&pdev
->dev
, "Couldn't get mod clk: %d\n", ret
);
315 rstc
= devm_reset_control_get_exclusive(&pdev
->dev
, NULL
);
318 if (ret
!= -EPROBE_DEFER
)
320 "Couldn't get reset control: %d\n", ret
);
324 /* The clocks need to be enabled for us to access the registers */
325 ret
= clk_prepare_enable(bus_clk
);
327 dev_err(&pdev
->dev
, "Couldn't enable bus clk: %d\n", ret
);
331 ret
= clk_prepare_enable(mod_clk
);
333 dev_err(&pdev
->dev
, "Couldn't enable mod clk: %d\n", ret
);
334 goto err_disable_bus_clk
;
337 /* The reset control needs to be asserted for the controls to work */
338 ret
= reset_control_deassert(rstc
);
341 "Couldn't deassert reset control: %d\n", ret
);
342 goto err_disable_mod_clk
;
345 ret
= sunxi_ccu_probe(pdev
->dev
.of_node
, reg
, ccu_desc
);
347 goto err_assert_reset
;
352 reset_control_assert(rstc
);
354 clk_disable_unprepare(mod_clk
);
356 clk_disable_unprepare(bus_clk
);
360 static const struct of_device_id sunxi_de2_clk_ids
[] = {
362 .compatible
= "allwinner,sun8i-a83t-de2-clk",
363 .data
= &sun8i_a83t_de2_clk_desc
,
366 .compatible
= "allwinner,sun8i-h3-de2-clk",
367 .data
= &sun8i_h3_de2_clk_desc
,
370 .compatible
= "allwinner,sun8i-r40-de2-clk",
371 .data
= &sun8i_r40_de2_clk_desc
,
374 .compatible
= "allwinner,sun8i-v3s-de2-clk",
375 .data
= &sun8i_v3s_de2_clk_desc
,
378 .compatible
= "allwinner,sun50i-a64-de2-clk",
379 .data
= &sun50i_a64_de2_clk_desc
,
382 .compatible
= "allwinner,sun50i-h5-de2-clk",
383 .data
= &sun50i_h5_de2_clk_desc
,
386 .compatible
= "allwinner,sun50i-h6-de3-clk",
387 .data
= &sun50i_h5_de2_clk_desc
,
392 static struct platform_driver sunxi_de2_clk_driver
= {
393 .probe
= sunxi_de2_clk_probe
,
395 .name
= "sunxi-de2-clks",
396 .of_match_table
= sunxi_de2_clk_ids
,
399 builtin_platform_driver(sunxi_de2_clk_driver
);