^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright 2013 Emilio López
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Emilio López <emilio@elopez.com.ar>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/clk-provider.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/of_address.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) static DEFINE_SPINLOCK(mod1_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define SUN4I_MOD1_ENABLE 31
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define SUN4I_MOD1_MUX 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define SUN4I_MOD1_MUX_WIDTH 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define SUN4I_MOD1_MAX_PARENTS 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static void __init sun4i_mod1_clk_setup(struct device_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct clk *clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct clk_mux *mux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct clk_gate *gate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) const char *parents[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) const char *clk_name = node->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) void __iomem *reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) reg = of_io_request_and_map(node, 0, of_node_full_name(node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (IS_ERR(reg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) mux = kzalloc(sizeof(*mux), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (!mux)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) goto err_unmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) gate = kzalloc(sizeof(*gate), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (!gate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) goto err_free_mux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) of_property_read_string(node, "clock-output-names", &clk_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) i = of_clk_parent_fill(node, parents, SUN4I_MOD1_MAX_PARENTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) gate->reg = reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) gate->bit_idx = SUN4I_MOD1_ENABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) gate->lock = &mod1_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) mux->reg = reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) mux->shift = SUN4I_MOD1_MUX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) mux->mask = BIT(SUN4I_MOD1_MUX_WIDTH) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) mux->lock = &mod1_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) clk = clk_register_composite(NULL, clk_name, parents, i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) &mux->hw, &clk_mux_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) NULL, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) &gate->hw, &clk_gate_ops, CLK_SET_RATE_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (IS_ERR(clk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) goto err_free_gate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) of_clk_add_provider(node, of_clk_src_simple_get, clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) err_free_gate:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) kfree(gate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) err_free_mux:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) kfree(mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) err_unmap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) iounmap(reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) CLK_OF_DECLARE(sun4i_mod1, "allwinner,sun4i-a10-mod1-clk",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) sun4i_mod1_clk_setup);