1b7c7b050SIcenowy Zheng // SPDX-License-Identifier: GPL-2.0
2b7c7b050SIcenowy Zheng /*
3b7c7b050SIcenowy Zheng  * Copyright (c) 2017 Icenowy Zheng <icenowy@aosc.xyz>
4b7c7b050SIcenowy Zheng  */
5b7c7b050SIcenowy Zheng 
6b7c7b050SIcenowy Zheng #include <linux/clk-provider.h>
7*7ec03b58SSamuel Holland #include <linux/module.h>
8*7ec03b58SSamuel Holland #include <linux/of_device.h>
9b7c7b050SIcenowy Zheng #include <linux/platform_device.h>
10b7c7b050SIcenowy Zheng 
11b7c7b050SIcenowy Zheng #include "ccu_common.h"
12b7c7b050SIcenowy Zheng #include "ccu_reset.h"
13b7c7b050SIcenowy Zheng 
14b7c7b050SIcenowy Zheng #include "ccu_div.h"
15b7c7b050SIcenowy Zheng #include "ccu_gate.h"
16b7c7b050SIcenowy Zheng #include "ccu_mp.h"
17b7c7b050SIcenowy Zheng #include "ccu_nm.h"
18b7c7b050SIcenowy Zheng 
19b7c7b050SIcenowy Zheng #include "ccu-sun50i-h6-r.h"
20b7c7b050SIcenowy Zheng 
21b7c7b050SIcenowy Zheng /*
22b7c7b050SIcenowy Zheng  * Information about AR100 and AHB/APB clocks in R_CCU are gathered from
23b7c7b050SIcenowy Zheng  * clock definitions in the BSP source code.
24b7c7b050SIcenowy Zheng  */
25b7c7b050SIcenowy Zheng 
26b7c7b050SIcenowy Zheng static const char * const ar100_r_apb2_parents[] = { "osc24M", "osc32k",
270c545240SSamuel Holland 						     "iosc", "pll-periph0" };
28b7c7b050SIcenowy Zheng static const struct ccu_mux_var_prediv ar100_r_apb2_predivs[] = {
290c545240SSamuel Holland 	{ .index = 3, .shift = 0, .width = 5 },
30b7c7b050SIcenowy Zheng };
31b7c7b050SIcenowy Zheng 
32b7c7b050SIcenowy Zheng static struct ccu_div ar100_clk = {
33b7c7b050SIcenowy Zheng 	.div		= _SUNXI_CCU_DIV_FLAGS(8, 2, CLK_DIVIDER_POWER_OF_TWO),
34b7c7b050SIcenowy Zheng 
35b7c7b050SIcenowy Zheng 	.mux		= {
36b7c7b050SIcenowy Zheng 		.shift	= 24,
37b7c7b050SIcenowy Zheng 		.width	= 2,
38b7c7b050SIcenowy Zheng 
39b7c7b050SIcenowy Zheng 		.var_predivs	= ar100_r_apb2_predivs,
40b7c7b050SIcenowy Zheng 		.n_var_predivs	= ARRAY_SIZE(ar100_r_apb2_predivs),
41b7c7b050SIcenowy Zheng 	},
42b7c7b050SIcenowy Zheng 
43b7c7b050SIcenowy Zheng 	.common		= {
44b7c7b050SIcenowy Zheng 		.reg		= 0x000,
45b7c7b050SIcenowy Zheng 		.features	= CCU_FEATURE_VARIABLE_PREDIV,
46b7c7b050SIcenowy Zheng 		.hw.init	= CLK_HW_INIT_PARENTS("ar100",
47b7c7b050SIcenowy Zheng 						      ar100_r_apb2_parents,
48b7c7b050SIcenowy Zheng 						      &ccu_div_ops,
49b7c7b050SIcenowy Zheng 						      0),
50b7c7b050SIcenowy Zheng 	},
51b7c7b050SIcenowy Zheng };
52b7c7b050SIcenowy Zheng 
5322ce173fSChen-Yu Tsai static CLK_FIXED_FACTOR_HW(r_ahb_clk, "r-ahb", &ar100_clk.common.hw, 1, 1, 0);
54b7c7b050SIcenowy Zheng 
55675a6d46SSamuel Holland static SUNXI_CCU_M(r_apb1_clk, "r-apb1", "r-ahb", 0x00c, 0, 2, 0);
56b7c7b050SIcenowy Zheng 
57b7c7b050SIcenowy Zheng static struct ccu_div r_apb2_clk = {
58b7c7b050SIcenowy Zheng 	.div		= _SUNXI_CCU_DIV_FLAGS(8, 2, CLK_DIVIDER_POWER_OF_TWO),
59b7c7b050SIcenowy Zheng 
60b7c7b050SIcenowy Zheng 	.mux		= {
61b7c7b050SIcenowy Zheng 		.shift	= 24,
62b7c7b050SIcenowy Zheng 		.width	= 2,
63b7c7b050SIcenowy Zheng 
64b7c7b050SIcenowy Zheng 		.var_predivs	= ar100_r_apb2_predivs,
65b7c7b050SIcenowy Zheng 		.n_var_predivs	= ARRAY_SIZE(ar100_r_apb2_predivs),
66b7c7b050SIcenowy Zheng 	},
67b7c7b050SIcenowy Zheng 
68b7c7b050SIcenowy Zheng 	.common		= {
69b7c7b050SIcenowy Zheng 		.reg		= 0x010,
70b7c7b050SIcenowy Zheng 		.features	= CCU_FEATURE_VARIABLE_PREDIV,
71b7c7b050SIcenowy Zheng 		.hw.init	= CLK_HW_INIT_PARENTS("r-apb2",
72b7c7b050SIcenowy Zheng 						      ar100_r_apb2_parents,
73b7c7b050SIcenowy Zheng 						      &ccu_div_ops,
74b7c7b050SIcenowy Zheng 						      0),
75b7c7b050SIcenowy Zheng 	},
76b7c7b050SIcenowy Zheng };
77b7c7b050SIcenowy Zheng 
78b7c7b050SIcenowy Zheng /*
79b7c7b050SIcenowy Zheng  * Information about the gate/resets are gathered from the clock header file
80b7c7b050SIcenowy Zheng  * in the BSP source code, although most of them are unused. The existence
81b7c7b050SIcenowy Zheng  * of the hardware block is verified with "3.1 Memory Mapping" chapter in
82b7c7b050SIcenowy Zheng  * "Allwinner H6 V200 User Manual V1.1"; and the parent APB buses are verified
83b7c7b050SIcenowy Zheng  * with "3.3.2.1 System Bus Tree" chapter inthe same document.
84b7c7b050SIcenowy Zheng  */
85b7c7b050SIcenowy Zheng static SUNXI_CCU_GATE(r_apb1_timer_clk,	"r-apb1-timer",	"r-apb1",
86b7c7b050SIcenowy Zheng 		      0x11c, BIT(0), 0);
87b7c7b050SIcenowy Zheng static SUNXI_CCU_GATE(r_apb1_twd_clk,	"r-apb1-twd",	"r-apb1",
88b7c7b050SIcenowy Zheng 		      0x12c, BIT(0), 0);
89b7c7b050SIcenowy Zheng static SUNXI_CCU_GATE(r_apb1_pwm_clk,	"r-apb1-pwm",	"r-apb1",
90b7c7b050SIcenowy Zheng 		      0x13c, BIT(0), 0);
91b7c7b050SIcenowy Zheng static SUNXI_CCU_GATE(r_apb2_uart_clk,	"r-apb2-uart",	"r-apb2",
92b7c7b050SIcenowy Zheng 		      0x18c, BIT(0), 0);
93b7c7b050SIcenowy Zheng static SUNXI_CCU_GATE(r_apb2_i2c_clk,	"r-apb2-i2c",	"r-apb2",
94b7c7b050SIcenowy Zheng 		      0x19c, BIT(0), 0);
950482a4e6SSamuel Holland static SUNXI_CCU_GATE(r_apb2_rsb_clk,	"r-apb2-rsb",	"r-apb2",
960482a4e6SSamuel Holland 		      0x1bc, BIT(0), 0);
97b7c7b050SIcenowy Zheng static SUNXI_CCU_GATE(r_apb1_ir_clk,	"r-apb1-ir",	"r-apb1",
98b7c7b050SIcenowy Zheng 		      0x1cc, BIT(0), 0);
99b7c7b050SIcenowy Zheng static SUNXI_CCU_GATE(r_apb1_w1_clk,	"r-apb1-w1",	"r-apb1",
100f1676754SOndrej Jirman 		      0x1ec, BIT(0), 0);
101b7c7b050SIcenowy Zheng 
102b7c7b050SIcenowy Zheng /* Information of IR(RX) mod clock is gathered from BSP source code */
103b7c7b050SIcenowy Zheng static const char * const r_mod0_default_parents[] = { "osc32k", "osc24M" };
104b7c7b050SIcenowy Zheng static SUNXI_CCU_MP_WITH_MUX_GATE(ir_clk, "ir",
105b7c7b050SIcenowy Zheng 				  r_mod0_default_parents, 0x1c0,
106b7c7b050SIcenowy Zheng 				  0, 5,		/* M */
107b7c7b050SIcenowy Zheng 				  8, 2,		/* P */
108b7c7b050SIcenowy Zheng 				  24, 1,	/* mux */
109b7c7b050SIcenowy Zheng 				  BIT(31),	/* gate */
110b7c7b050SIcenowy Zheng 				  0);
111b7c7b050SIcenowy Zheng 
112b7c7b050SIcenowy Zheng /*
113b7c7b050SIcenowy Zheng  * BSP didn't use the 1-wire function at all now, and the information about
114b7c7b050SIcenowy Zheng  * this mod clock is guessed from the IR mod clock above. The existence of
115b7c7b050SIcenowy Zheng  * this mod clock is proven by BSP clock header, and the dividers are verified
116b7c7b050SIcenowy Zheng  * by contents in the 1-wire related chapter of the User Manual.
117b7c7b050SIcenowy Zheng  */
118b7c7b050SIcenowy Zheng 
119b7c7b050SIcenowy Zheng static SUNXI_CCU_MP_WITH_MUX_GATE(w1_clk, "w1",
120b7c7b050SIcenowy Zheng 				  r_mod0_default_parents, 0x1e0,
121b7c7b050SIcenowy Zheng 				  0, 5,		/* M */
122b7c7b050SIcenowy Zheng 				  8, 2,		/* P */
123b7c7b050SIcenowy Zheng 				  24, 1,	/* mux */
124b7c7b050SIcenowy Zheng 				  BIT(31),	/* gate */
125b7c7b050SIcenowy Zheng 				  0);
126b7c7b050SIcenowy Zheng 
127b7c7b050SIcenowy Zheng static struct ccu_common *sun50i_h6_r_ccu_clks[] = {
128b7c7b050SIcenowy Zheng 	&ar100_clk.common,
129b7c7b050SIcenowy Zheng 	&r_apb1_clk.common,
130b7c7b050SIcenowy Zheng 	&r_apb2_clk.common,
131b7c7b050SIcenowy Zheng 	&r_apb1_timer_clk.common,
132b7c7b050SIcenowy Zheng 	&r_apb1_twd_clk.common,
133b7c7b050SIcenowy Zheng 	&r_apb1_pwm_clk.common,
134b7c7b050SIcenowy Zheng 	&r_apb2_uart_clk.common,
135b7c7b050SIcenowy Zheng 	&r_apb2_i2c_clk.common,
1360482a4e6SSamuel Holland 	&r_apb2_rsb_clk.common,
137b7c7b050SIcenowy Zheng 	&r_apb1_ir_clk.common,
138b7c7b050SIcenowy Zheng 	&r_apb1_w1_clk.common,
139b7c7b050SIcenowy Zheng 	&ir_clk.common,
140b7c7b050SIcenowy Zheng 	&w1_clk.common,
141b7c7b050SIcenowy Zheng };
142b7c7b050SIcenowy Zheng 
143394a36ddSAndre Przywara static struct ccu_common *sun50i_h616_r_ccu_clks[] = {
144394a36ddSAndre Przywara 	&r_apb1_clk.common,
145394a36ddSAndre Przywara 	&r_apb2_clk.common,
146394a36ddSAndre Przywara 	&r_apb1_twd_clk.common,
147394a36ddSAndre Przywara 	&r_apb2_i2c_clk.common,
148394a36ddSAndre Przywara 	&r_apb2_rsb_clk.common,
149394a36ddSAndre Przywara 	&r_apb1_ir_clk.common,
150394a36ddSAndre Przywara 	&ir_clk.common,
151394a36ddSAndre Przywara };
152394a36ddSAndre Przywara 
153b7c7b050SIcenowy Zheng static struct clk_hw_onecell_data sun50i_h6_r_hw_clks = {
154b7c7b050SIcenowy Zheng 	.hws	= {
155b7c7b050SIcenowy Zheng 		[CLK_AR100]		= &ar100_clk.common.hw,
156b7c7b050SIcenowy Zheng 		[CLK_R_AHB]		= &r_ahb_clk.hw,
157b7c7b050SIcenowy Zheng 		[CLK_R_APB1]		= &r_apb1_clk.common.hw,
158b7c7b050SIcenowy Zheng 		[CLK_R_APB2]		= &r_apb2_clk.common.hw,
159b7c7b050SIcenowy Zheng 		[CLK_R_APB1_TIMER]	= &r_apb1_timer_clk.common.hw,
160b7c7b050SIcenowy Zheng 		[CLK_R_APB1_TWD]	= &r_apb1_twd_clk.common.hw,
161b7c7b050SIcenowy Zheng 		[CLK_R_APB1_PWM]	= &r_apb1_pwm_clk.common.hw,
162b7c7b050SIcenowy Zheng 		[CLK_R_APB2_UART]	= &r_apb2_uart_clk.common.hw,
163b7c7b050SIcenowy Zheng 		[CLK_R_APB2_I2C]	= &r_apb2_i2c_clk.common.hw,
1640482a4e6SSamuel Holland 		[CLK_R_APB2_RSB]	= &r_apb2_rsb_clk.common.hw,
165b7c7b050SIcenowy Zheng 		[CLK_R_APB1_IR]		= &r_apb1_ir_clk.common.hw,
166b7c7b050SIcenowy Zheng 		[CLK_R_APB1_W1]		= &r_apb1_w1_clk.common.hw,
167b7c7b050SIcenowy Zheng 		[CLK_IR]		= &ir_clk.common.hw,
168b7c7b050SIcenowy Zheng 		[CLK_W1]		= &w1_clk.common.hw,
169b7c7b050SIcenowy Zheng 	},
170b7c7b050SIcenowy Zheng 	.num	= CLK_NUMBER,
171b7c7b050SIcenowy Zheng };
172b7c7b050SIcenowy Zheng 
173394a36ddSAndre Przywara static struct clk_hw_onecell_data sun50i_h616_r_hw_clks = {
174394a36ddSAndre Przywara 	.hws	= {
175394a36ddSAndre Przywara 		[CLK_R_AHB]		= &r_ahb_clk.hw,
176394a36ddSAndre Przywara 		[CLK_R_APB1]		= &r_apb1_clk.common.hw,
177394a36ddSAndre Przywara 		[CLK_R_APB2]		= &r_apb2_clk.common.hw,
178394a36ddSAndre Przywara 		[CLK_R_APB1_TWD]	= &r_apb1_twd_clk.common.hw,
179394a36ddSAndre Przywara 		[CLK_R_APB2_I2C]	= &r_apb2_i2c_clk.common.hw,
180394a36ddSAndre Przywara 		[CLK_R_APB2_RSB]	= &r_apb2_rsb_clk.common.hw,
181394a36ddSAndre Przywara 		[CLK_R_APB1_IR]		= &r_apb1_ir_clk.common.hw,
182394a36ddSAndre Przywara 		[CLK_IR]		= &ir_clk.common.hw,
183394a36ddSAndre Przywara 	},
184394a36ddSAndre Przywara 	.num	= CLK_NUMBER,
185394a36ddSAndre Przywara };
186394a36ddSAndre Przywara 
187b7c7b050SIcenowy Zheng static struct ccu_reset_map sun50i_h6_r_ccu_resets[] = {
188b7c7b050SIcenowy Zheng 	[RST_R_APB1_TIMER]	=  { 0x11c, BIT(16) },
189b7c7b050SIcenowy Zheng 	[RST_R_APB1_TWD]	=  { 0x12c, BIT(16) },
190b7c7b050SIcenowy Zheng 	[RST_R_APB1_PWM]	=  { 0x13c, BIT(16) },
191b7c7b050SIcenowy Zheng 	[RST_R_APB2_UART]	=  { 0x18c, BIT(16) },
192b7c7b050SIcenowy Zheng 	[RST_R_APB2_I2C]	=  { 0x19c, BIT(16) },
1930482a4e6SSamuel Holland 	[RST_R_APB2_RSB]	=  { 0x1bc, BIT(16) },
194b7c7b050SIcenowy Zheng 	[RST_R_APB1_IR]		=  { 0x1cc, BIT(16) },
195b7c7b050SIcenowy Zheng 	[RST_R_APB1_W1]		=  { 0x1ec, BIT(16) },
196b7c7b050SIcenowy Zheng };
197b7c7b050SIcenowy Zheng 
198394a36ddSAndre Przywara static struct ccu_reset_map sun50i_h616_r_ccu_resets[] = {
199394a36ddSAndre Przywara 	[RST_R_APB1_TWD]	=  { 0x12c, BIT(16) },
200394a36ddSAndre Przywara 	[RST_R_APB2_I2C]	=  { 0x19c, BIT(16) },
201394a36ddSAndre Przywara 	[RST_R_APB2_RSB]	=  { 0x1bc, BIT(16) },
202394a36ddSAndre Przywara 	[RST_R_APB1_IR]		=  { 0x1cc, BIT(16) },
203394a36ddSAndre Przywara };
204394a36ddSAndre Przywara 
205b7c7b050SIcenowy Zheng static const struct sunxi_ccu_desc sun50i_h6_r_ccu_desc = {
206b7c7b050SIcenowy Zheng 	.ccu_clks	= sun50i_h6_r_ccu_clks,
207b7c7b050SIcenowy Zheng 	.num_ccu_clks	= ARRAY_SIZE(sun50i_h6_r_ccu_clks),
208b7c7b050SIcenowy Zheng 
209b7c7b050SIcenowy Zheng 	.hw_clks	= &sun50i_h6_r_hw_clks,
210b7c7b050SIcenowy Zheng 
211b7c7b050SIcenowy Zheng 	.resets		= sun50i_h6_r_ccu_resets,
212b7c7b050SIcenowy Zheng 	.num_resets	= ARRAY_SIZE(sun50i_h6_r_ccu_resets),
213b7c7b050SIcenowy Zheng };
214b7c7b050SIcenowy Zheng 
215394a36ddSAndre Przywara static const struct sunxi_ccu_desc sun50i_h616_r_ccu_desc = {
216394a36ddSAndre Przywara 	.ccu_clks	= sun50i_h616_r_ccu_clks,
217394a36ddSAndre Przywara 	.num_ccu_clks	= ARRAY_SIZE(sun50i_h616_r_ccu_clks),
218394a36ddSAndre Przywara 
219394a36ddSAndre Przywara 	.hw_clks	= &sun50i_h616_r_hw_clks,
220394a36ddSAndre Przywara 
221394a36ddSAndre Przywara 	.resets		= sun50i_h616_r_ccu_resets,
222394a36ddSAndre Przywara 	.num_resets	= ARRAY_SIZE(sun50i_h616_r_ccu_resets),
223394a36ddSAndre Przywara };
224394a36ddSAndre Przywara 
225*7ec03b58SSamuel Holland static int sun50i_h6_r_ccu_probe(struct platform_device *pdev)
226b7c7b050SIcenowy Zheng {
227*7ec03b58SSamuel Holland 	const struct sunxi_ccu_desc *desc;
228b7c7b050SIcenowy Zheng 	void __iomem *reg;
229b7c7b050SIcenowy Zheng 
230*7ec03b58SSamuel Holland 	desc = of_device_get_match_data(&pdev->dev);
231*7ec03b58SSamuel Holland 	if (!desc)
232*7ec03b58SSamuel Holland 		return -EINVAL;
233*7ec03b58SSamuel Holland 
234*7ec03b58SSamuel Holland 	reg = devm_platform_ioremap_resource(pdev, 0);
235*7ec03b58SSamuel Holland 	if (IS_ERR(reg))
236*7ec03b58SSamuel Holland 		return PTR_ERR(reg);
237*7ec03b58SSamuel Holland 
238*7ec03b58SSamuel Holland 	return devm_sunxi_ccu_probe(&pdev->dev, reg, desc);
239b7c7b050SIcenowy Zheng }
240b7c7b050SIcenowy Zheng 
241*7ec03b58SSamuel Holland static const struct of_device_id sun50i_h6_r_ccu_ids[] = {
242b7c7b050SIcenowy Zheng 	{
243*7ec03b58SSamuel Holland 		.compatible = "allwinner,sun50i-h6-r-ccu",
244*7ec03b58SSamuel Holland 		.data = &sun50i_h6_r_ccu_desc,
245*7ec03b58SSamuel Holland 	},
246394a36ddSAndre Przywara 	{
247*7ec03b58SSamuel Holland 		.compatible = "allwinner,sun50i-h616-r-ccu",
248*7ec03b58SSamuel Holland 		.data = &sun50i_h616_r_ccu_desc,
249*7ec03b58SSamuel Holland 	},
250*7ec03b58SSamuel Holland 	{ }
251*7ec03b58SSamuel Holland };
252*7ec03b58SSamuel Holland 
253*7ec03b58SSamuel Holland static struct platform_driver sun50i_h6_r_ccu_driver = {
254*7ec03b58SSamuel Holland 	.probe	= sun50i_h6_r_ccu_probe,
255*7ec03b58SSamuel Holland 	.driver	= {
256*7ec03b58SSamuel Holland 		.name			= "sun50i-h6-r-ccu",
257*7ec03b58SSamuel Holland 		.suppress_bind_attrs	= true,
258*7ec03b58SSamuel Holland 		.of_match_table		= sun50i_h6_r_ccu_ids,
259*7ec03b58SSamuel Holland 	},
260*7ec03b58SSamuel Holland };
261*7ec03b58SSamuel Holland module_platform_driver(sun50i_h6_r_ccu_driver);
262*7ec03b58SSamuel Holland 
263*7ec03b58SSamuel Holland MODULE_IMPORT_NS(SUNXI_CCU);
264*7ec03b58SSamuel Holland MODULE_LICENSE("GPL");
265