19c92ab61SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2cdb8b80bSIcenowy Zheng /*
3cdb8b80bSIcenowy Zheng * Copyright (c) 2016 Icenowy Zheng <icenowy@aosc.xyz>
4cdb8b80bSIcenowy Zheng */
5cdb8b80bSIcenowy Zheng
6cdb8b80bSIcenowy Zheng #include <linux/clk-provider.h>
77ec03b58SSamuel Holland #include <linux/module.h>
8a96cbb14SRob Herring #include <linux/of.h>
9cdb8b80bSIcenowy Zheng #include <linux/platform_device.h>
10cdb8b80bSIcenowy Zheng
11cdb8b80bSIcenowy Zheng #include "ccu_common.h"
12cdb8b80bSIcenowy Zheng #include "ccu_reset.h"
13cdb8b80bSIcenowy Zheng
14cdb8b80bSIcenowy Zheng #include "ccu_div.h"
15cdb8b80bSIcenowy Zheng #include "ccu_gate.h"
16cdb8b80bSIcenowy Zheng #include "ccu_mp.h"
17cdb8b80bSIcenowy Zheng #include "ccu_nm.h"
18cdb8b80bSIcenowy Zheng
19cdb8b80bSIcenowy Zheng #include "ccu-sun8i-r.h"
20cdb8b80bSIcenowy Zheng
216873d207SChen-Yu Tsai static const struct clk_parent_data ar100_parents[] = {
226873d207SChen-Yu Tsai { .fw_name = "losc" },
236873d207SChen-Yu Tsai { .fw_name = "hosc" },
246873d207SChen-Yu Tsai { .fw_name = "pll-periph" },
256873d207SChen-Yu Tsai { .fw_name = "iosc" },
266873d207SChen-Yu Tsai };
276873d207SChen-Yu Tsai
2813e0dde8SChen-Yu Tsai static const struct ccu_mux_var_prediv ar100_predivs[] = {
2913e0dde8SChen-Yu Tsai { .index = 2, .shift = 8, .width = 5 },
3013e0dde8SChen-Yu Tsai };
31cdb8b80bSIcenowy Zheng
32cdb8b80bSIcenowy Zheng static struct ccu_div ar100_clk = {
33cdb8b80bSIcenowy Zheng .div = _SUNXI_CCU_DIV_FLAGS(4, 2, CLK_DIVIDER_POWER_OF_TWO),
34cdb8b80bSIcenowy Zheng
35cdb8b80bSIcenowy Zheng .mux = {
36cdb8b80bSIcenowy Zheng .shift = 16,
37cdb8b80bSIcenowy Zheng .width = 2,
38cdb8b80bSIcenowy Zheng
3913e0dde8SChen-Yu Tsai .var_predivs = ar100_predivs,
4013e0dde8SChen-Yu Tsai .n_var_predivs = ARRAY_SIZE(ar100_predivs),
41cdb8b80bSIcenowy Zheng },
42cdb8b80bSIcenowy Zheng
43cdb8b80bSIcenowy Zheng .common = {
44cdb8b80bSIcenowy Zheng .reg = 0x00,
45cdb8b80bSIcenowy Zheng .features = CCU_FEATURE_VARIABLE_PREDIV,
466873d207SChen-Yu Tsai .hw.init = CLK_HW_INIT_PARENTS_DATA("ar100",
47cdb8b80bSIcenowy Zheng ar100_parents,
48cdb8b80bSIcenowy Zheng &ccu_div_ops,
49cdb8b80bSIcenowy Zheng 0),
50cdb8b80bSIcenowy Zheng },
51cdb8b80bSIcenowy Zheng };
52cdb8b80bSIcenowy Zheng
5345d0706eSChen-Yu Tsai static CLK_FIXED_FACTOR_HW(ahb0_clk, "ahb0", &ar100_clk.common.hw, 1, 1, 0);
54cdb8b80bSIcenowy Zheng
5547d64fefSSamuel Holland static SUNXI_CCU_M(apb0_clk, "apb0", "ahb0", 0x0c, 0, 2, 0);
565a90c14cSChen-Yu Tsai
5789f27fb2SChen-Yu Tsai /*
5889f27fb2SChen-Yu Tsai * Define the parent as an array that can be reused to save space
5989f27fb2SChen-Yu Tsai * instead of having compound literals for each gate. Also have it
6089f27fb2SChen-Yu Tsai * non-const so we can change it on the A83T.
6189f27fb2SChen-Yu Tsai */
6289f27fb2SChen-Yu Tsai static const struct clk_hw *apb0_gate_parent[] = { &apb0_clk.common.hw };
6389f27fb2SChen-Yu Tsai static SUNXI_CCU_GATE_HWS(apb0_pio_clk, "apb0-pio",
6489f27fb2SChen-Yu Tsai apb0_gate_parent, 0x28, BIT(0), 0);
6589f27fb2SChen-Yu Tsai static SUNXI_CCU_GATE_HWS(apb0_ir_clk, "apb0-ir",
6689f27fb2SChen-Yu Tsai apb0_gate_parent, 0x28, BIT(1), 0);
6789f27fb2SChen-Yu Tsai static SUNXI_CCU_GATE_HWS(apb0_timer_clk, "apb0-timer",
6889f27fb2SChen-Yu Tsai apb0_gate_parent, 0x28, BIT(2), 0);
6989f27fb2SChen-Yu Tsai static SUNXI_CCU_GATE_HWS(apb0_rsb_clk, "apb0-rsb",
7089f27fb2SChen-Yu Tsai apb0_gate_parent, 0x28, BIT(3), 0);
7189f27fb2SChen-Yu Tsai static SUNXI_CCU_GATE_HWS(apb0_uart_clk, "apb0-uart",
7289f27fb2SChen-Yu Tsai apb0_gate_parent, 0x28, BIT(4), 0);
7389f27fb2SChen-Yu Tsai static SUNXI_CCU_GATE_HWS(apb0_i2c_clk, "apb0-i2c",
7489f27fb2SChen-Yu Tsai apb0_gate_parent, 0x28, BIT(6), 0);
7589f27fb2SChen-Yu Tsai static SUNXI_CCU_GATE_HWS(apb0_twd_clk, "apb0-twd",
7689f27fb2SChen-Yu Tsai apb0_gate_parent, 0x28, BIT(7), 0);
77cdb8b80bSIcenowy Zheng
7837cabc74SIcenowy Zheng static const char * const r_mod0_default_parents[] = { "osc32k", "osc24M" };
79cdb8b80bSIcenowy Zheng static SUNXI_CCU_MP_WITH_MUX_GATE(ir_clk, "ir",
80cdb8b80bSIcenowy Zheng r_mod0_default_parents, 0x54,
81cdb8b80bSIcenowy Zheng 0, 4, /* M */
82cdb8b80bSIcenowy Zheng 16, 2, /* P */
83cdb8b80bSIcenowy Zheng 24, 2, /* mux */
84cdb8b80bSIcenowy Zheng BIT(31), /* gate */
85cdb8b80bSIcenowy Zheng 0);
86cdb8b80bSIcenowy Zheng
876873d207SChen-Yu Tsai static const struct clk_parent_data a83t_r_mod0_parents[] = {
886873d207SChen-Yu Tsai { .fw_name = "iosc" },
896873d207SChen-Yu Tsai { .fw_name = "hosc" },
906873d207SChen-Yu Tsai };
915a90c14cSChen-Yu Tsai static const struct ccu_mux_fixed_prediv a83t_ir_predivs[] = {
925a90c14cSChen-Yu Tsai { .index = 0, .div = 16 },
935a90c14cSChen-Yu Tsai };
945a90c14cSChen-Yu Tsai static struct ccu_mp a83t_ir_clk = {
955a90c14cSChen-Yu Tsai .enable = BIT(31),
965a90c14cSChen-Yu Tsai
975a90c14cSChen-Yu Tsai .m = _SUNXI_CCU_DIV(0, 4),
985a90c14cSChen-Yu Tsai .p = _SUNXI_CCU_DIV(16, 2),
995a90c14cSChen-Yu Tsai
1005a90c14cSChen-Yu Tsai .mux = {
1015a90c14cSChen-Yu Tsai .shift = 24,
1025a90c14cSChen-Yu Tsai .width = 2,
1035a90c14cSChen-Yu Tsai .fixed_predivs = a83t_ir_predivs,
1045a90c14cSChen-Yu Tsai .n_predivs = ARRAY_SIZE(a83t_ir_predivs),
1055a90c14cSChen-Yu Tsai },
1065a90c14cSChen-Yu Tsai
1075a90c14cSChen-Yu Tsai .common = {
1085a90c14cSChen-Yu Tsai .reg = 0x54,
1095a90c14cSChen-Yu Tsai .features = CCU_FEATURE_VARIABLE_PREDIV,
1106873d207SChen-Yu Tsai .hw.init = CLK_HW_INIT_PARENTS_DATA("ir",
1115a90c14cSChen-Yu Tsai a83t_r_mod0_parents,
1125a90c14cSChen-Yu Tsai &ccu_mp_ops,
1135a90c14cSChen-Yu Tsai 0),
1145a90c14cSChen-Yu Tsai },
1155a90c14cSChen-Yu Tsai };
1165a90c14cSChen-Yu Tsai
117e1c51d31SSamuel Holland static struct ccu_common *sun8i_r_ccu_clks[] = {
1186873d207SChen-Yu Tsai &ar100_clk.common,
11947d64fefSSamuel Holland &apb0_clk.common,
1205a90c14cSChen-Yu Tsai &apb0_pio_clk.common,
1215a90c14cSChen-Yu Tsai &apb0_ir_clk.common,
1225a90c14cSChen-Yu Tsai &apb0_timer_clk.common,
1235a90c14cSChen-Yu Tsai &apb0_rsb_clk.common,
1245a90c14cSChen-Yu Tsai &apb0_uart_clk.common,
1255a90c14cSChen-Yu Tsai &apb0_i2c_clk.common,
1265a90c14cSChen-Yu Tsai &apb0_twd_clk.common,
127e1c51d31SSamuel Holland &ir_clk.common,
1285a90c14cSChen-Yu Tsai &a83t_ir_clk.common,
1295a90c14cSChen-Yu Tsai };
1305a90c14cSChen-Yu Tsai
1315a90c14cSChen-Yu Tsai static struct clk_hw_onecell_data sun8i_a83t_r_hw_clks = {
1325a90c14cSChen-Yu Tsai .hws = {
1336873d207SChen-Yu Tsai [CLK_AR100] = &ar100_clk.common.hw,
1345a90c14cSChen-Yu Tsai [CLK_AHB0] = &ahb0_clk.hw,
13547d64fefSSamuel Holland [CLK_APB0] = &apb0_clk.common.hw,
1365a90c14cSChen-Yu Tsai [CLK_APB0_PIO] = &apb0_pio_clk.common.hw,
1375a90c14cSChen-Yu Tsai [CLK_APB0_IR] = &apb0_ir_clk.common.hw,
1385a90c14cSChen-Yu Tsai [CLK_APB0_TIMER] = &apb0_timer_clk.common.hw,
1395a90c14cSChen-Yu Tsai [CLK_APB0_RSB] = &apb0_rsb_clk.common.hw,
1405a90c14cSChen-Yu Tsai [CLK_APB0_UART] = &apb0_uart_clk.common.hw,
1415a90c14cSChen-Yu Tsai [CLK_APB0_I2C] = &apb0_i2c_clk.common.hw,
1425a90c14cSChen-Yu Tsai [CLK_APB0_TWD] = &apb0_twd_clk.common.hw,
1435a90c14cSChen-Yu Tsai [CLK_IR] = &a83t_ir_clk.common.hw,
1445a90c14cSChen-Yu Tsai },
1455a90c14cSChen-Yu Tsai .num = CLK_NUMBER,
1465a90c14cSChen-Yu Tsai };
1475a90c14cSChen-Yu Tsai
148cdb8b80bSIcenowy Zheng static struct clk_hw_onecell_data sun8i_h3_r_hw_clks = {
149cdb8b80bSIcenowy Zheng .hws = {
150cdb8b80bSIcenowy Zheng [CLK_AR100] = &ar100_clk.common.hw,
151cdb8b80bSIcenowy Zheng [CLK_AHB0] = &ahb0_clk.hw,
152cdb8b80bSIcenowy Zheng [CLK_APB0] = &apb0_clk.common.hw,
153cdb8b80bSIcenowy Zheng [CLK_APB0_PIO] = &apb0_pio_clk.common.hw,
154cdb8b80bSIcenowy Zheng [CLK_APB0_IR] = &apb0_ir_clk.common.hw,
155cdb8b80bSIcenowy Zheng [CLK_APB0_TIMER] = &apb0_timer_clk.common.hw,
156cdb8b80bSIcenowy Zheng [CLK_APB0_UART] = &apb0_uart_clk.common.hw,
157cdb8b80bSIcenowy Zheng [CLK_APB0_I2C] = &apb0_i2c_clk.common.hw,
158cdb8b80bSIcenowy Zheng [CLK_APB0_TWD] = &apb0_twd_clk.common.hw,
159cdb8b80bSIcenowy Zheng [CLK_IR] = &ir_clk.common.hw,
160cdb8b80bSIcenowy Zheng },
161cdb8b80bSIcenowy Zheng .num = CLK_NUMBER,
162cdb8b80bSIcenowy Zheng };
163cdb8b80bSIcenowy Zheng
164cdb8b80bSIcenowy Zheng static struct clk_hw_onecell_data sun50i_a64_r_hw_clks = {
165cdb8b80bSIcenowy Zheng .hws = {
166cdb8b80bSIcenowy Zheng [CLK_AR100] = &ar100_clk.common.hw,
167cdb8b80bSIcenowy Zheng [CLK_AHB0] = &ahb0_clk.hw,
168cdb8b80bSIcenowy Zheng [CLK_APB0] = &apb0_clk.common.hw,
169cdb8b80bSIcenowy Zheng [CLK_APB0_PIO] = &apb0_pio_clk.common.hw,
170cdb8b80bSIcenowy Zheng [CLK_APB0_IR] = &apb0_ir_clk.common.hw,
171cdb8b80bSIcenowy Zheng [CLK_APB0_TIMER] = &apb0_timer_clk.common.hw,
172cdb8b80bSIcenowy Zheng [CLK_APB0_RSB] = &apb0_rsb_clk.common.hw,
173cdb8b80bSIcenowy Zheng [CLK_APB0_UART] = &apb0_uart_clk.common.hw,
174cdb8b80bSIcenowy Zheng [CLK_APB0_I2C] = &apb0_i2c_clk.common.hw,
175cdb8b80bSIcenowy Zheng [CLK_APB0_TWD] = &apb0_twd_clk.common.hw,
176cdb8b80bSIcenowy Zheng [CLK_IR] = &ir_clk.common.hw,
177cdb8b80bSIcenowy Zheng },
178cdb8b80bSIcenowy Zheng .num = CLK_NUMBER,
179cdb8b80bSIcenowy Zheng };
180cdb8b80bSIcenowy Zheng
1815a90c14cSChen-Yu Tsai static struct ccu_reset_map sun8i_a83t_r_ccu_resets[] = {
1825a90c14cSChen-Yu Tsai [RST_APB0_IR] = { 0xb0, BIT(1) },
1835a90c14cSChen-Yu Tsai [RST_APB0_TIMER] = { 0xb0, BIT(2) },
1845a90c14cSChen-Yu Tsai [RST_APB0_RSB] = { 0xb0, BIT(3) },
1855a90c14cSChen-Yu Tsai [RST_APB0_UART] = { 0xb0, BIT(4) },
1865a90c14cSChen-Yu Tsai [RST_APB0_I2C] = { 0xb0, BIT(6) },
1875a90c14cSChen-Yu Tsai };
1885a90c14cSChen-Yu Tsai
189cdb8b80bSIcenowy Zheng static struct ccu_reset_map sun8i_h3_r_ccu_resets[] = {
190cdb8b80bSIcenowy Zheng [RST_APB0_IR] = { 0xb0, BIT(1) },
191cdb8b80bSIcenowy Zheng [RST_APB0_TIMER] = { 0xb0, BIT(2) },
192cdb8b80bSIcenowy Zheng [RST_APB0_UART] = { 0xb0, BIT(4) },
193cdb8b80bSIcenowy Zheng [RST_APB0_I2C] = { 0xb0, BIT(6) },
194cdb8b80bSIcenowy Zheng };
195cdb8b80bSIcenowy Zheng
196cdb8b80bSIcenowy Zheng static struct ccu_reset_map sun50i_a64_r_ccu_resets[] = {
197cdb8b80bSIcenowy Zheng [RST_APB0_IR] = { 0xb0, BIT(1) },
198cdb8b80bSIcenowy Zheng [RST_APB0_TIMER] = { 0xb0, BIT(2) },
199cdb8b80bSIcenowy Zheng [RST_APB0_RSB] = { 0xb0, BIT(3) },
200cdb8b80bSIcenowy Zheng [RST_APB0_UART] = { 0xb0, BIT(4) },
201cdb8b80bSIcenowy Zheng [RST_APB0_I2C] = { 0xb0, BIT(6) },
202cdb8b80bSIcenowy Zheng };
203cdb8b80bSIcenowy Zheng
2045a90c14cSChen-Yu Tsai static const struct sunxi_ccu_desc sun8i_a83t_r_ccu_desc = {
205e1c51d31SSamuel Holland .ccu_clks = sun8i_r_ccu_clks,
206e1c51d31SSamuel Holland .num_ccu_clks = ARRAY_SIZE(sun8i_r_ccu_clks),
2075a90c14cSChen-Yu Tsai
2085a90c14cSChen-Yu Tsai .hw_clks = &sun8i_a83t_r_hw_clks,
2095a90c14cSChen-Yu Tsai
2105a90c14cSChen-Yu Tsai .resets = sun8i_a83t_r_ccu_resets,
2115a90c14cSChen-Yu Tsai .num_resets = ARRAY_SIZE(sun8i_a83t_r_ccu_resets),
2125a90c14cSChen-Yu Tsai };
2135a90c14cSChen-Yu Tsai
214cdb8b80bSIcenowy Zheng static const struct sunxi_ccu_desc sun8i_h3_r_ccu_desc = {
215e1c51d31SSamuel Holland .ccu_clks = sun8i_r_ccu_clks,
216e1c51d31SSamuel Holland .num_ccu_clks = ARRAY_SIZE(sun8i_r_ccu_clks),
217cdb8b80bSIcenowy Zheng
218cdb8b80bSIcenowy Zheng .hw_clks = &sun8i_h3_r_hw_clks,
219cdb8b80bSIcenowy Zheng
220cdb8b80bSIcenowy Zheng .resets = sun8i_h3_r_ccu_resets,
221cdb8b80bSIcenowy Zheng .num_resets = ARRAY_SIZE(sun8i_h3_r_ccu_resets),
222cdb8b80bSIcenowy Zheng };
223cdb8b80bSIcenowy Zheng
224cdb8b80bSIcenowy Zheng static const struct sunxi_ccu_desc sun50i_a64_r_ccu_desc = {
225e1c51d31SSamuel Holland .ccu_clks = sun8i_r_ccu_clks,
226e1c51d31SSamuel Holland .num_ccu_clks = ARRAY_SIZE(sun8i_r_ccu_clks),
227cdb8b80bSIcenowy Zheng
228cdb8b80bSIcenowy Zheng .hw_clks = &sun50i_a64_r_hw_clks,
229cdb8b80bSIcenowy Zheng
230cdb8b80bSIcenowy Zheng .resets = sun50i_a64_r_ccu_resets,
231cdb8b80bSIcenowy Zheng .num_resets = ARRAY_SIZE(sun50i_a64_r_ccu_resets),
232cdb8b80bSIcenowy Zheng };
233cdb8b80bSIcenowy Zheng
sun8i_r_ccu_probe(struct platform_device * pdev)2347ec03b58SSamuel Holland static int sun8i_r_ccu_probe(struct platform_device *pdev)
235cdb8b80bSIcenowy Zheng {
2367ec03b58SSamuel Holland const struct sunxi_ccu_desc *desc;
237cdb8b80bSIcenowy Zheng void __iomem *reg;
238cdb8b80bSIcenowy Zheng
2397ec03b58SSamuel Holland desc = of_device_get_match_data(&pdev->dev);
2407ec03b58SSamuel Holland if (!desc)
2417ec03b58SSamuel Holland return -EINVAL;
2427ec03b58SSamuel Holland
2437ec03b58SSamuel Holland reg = devm_platform_ioremap_resource(pdev, 0);
2447ec03b58SSamuel Holland if (IS_ERR(reg))
2457ec03b58SSamuel Holland return PTR_ERR(reg);
2467ec03b58SSamuel Holland
2477ec03b58SSamuel Holland return devm_sunxi_ccu_probe(&pdev->dev, reg, desc);
248cdb8b80bSIcenowy Zheng }
249cdb8b80bSIcenowy Zheng
2507ec03b58SSamuel Holland static const struct of_device_id sun8i_r_ccu_ids[] = {
2515a90c14cSChen-Yu Tsai {
2527ec03b58SSamuel Holland .compatible = "allwinner,sun8i-a83t-r-ccu",
2537ec03b58SSamuel Holland .data = &sun8i_a83t_r_ccu_desc,
2547ec03b58SSamuel Holland },
255cdb8b80bSIcenowy Zheng {
2567ec03b58SSamuel Holland .compatible = "allwinner,sun8i-h3-r-ccu",
2577ec03b58SSamuel Holland .data = &sun8i_h3_r_ccu_desc,
2587ec03b58SSamuel Holland },
259cdb8b80bSIcenowy Zheng {
2607ec03b58SSamuel Holland .compatible = "allwinner,sun50i-a64-r-ccu",
2617ec03b58SSamuel Holland .data = &sun50i_a64_r_ccu_desc,
2627ec03b58SSamuel Holland },
2637ec03b58SSamuel Holland { }
2647ec03b58SSamuel Holland };
265c60f6804SKrzysztof Kozlowski MODULE_DEVICE_TABLE(of, sun8i_r_ccu_ids);
2667ec03b58SSamuel Holland
2677ec03b58SSamuel Holland static struct platform_driver sun8i_r_ccu_driver = {
2687ec03b58SSamuel Holland .probe = sun8i_r_ccu_probe,
2697ec03b58SSamuel Holland .driver = {
2707ec03b58SSamuel Holland .name = "sun8i-r-ccu",
2717ec03b58SSamuel Holland .suppress_bind_attrs = true,
2727ec03b58SSamuel Holland .of_match_table = sun8i_r_ccu_ids,
2737ec03b58SSamuel Holland },
2747ec03b58SSamuel Holland };
2757ec03b58SSamuel Holland module_platform_driver(sun8i_r_ccu_driver);
2767ec03b58SSamuel Holland
2777ec03b58SSamuel Holland MODULE_IMPORT_NS(SUNXI_CCU);
278*4e7134faSJeff Johnson MODULE_DESCRIPTION("Support for Allwinner SoCs' PRCM CCUs");
2797ec03b58SSamuel Holland MODULE_LICENSE("GPL");
280