1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2014 Chen-Yu Tsai
9 #include <linux/clk-provider.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/of_address.h>
15 #define SUN8I_MBUS_ENABLE 31
16 #define SUN8I_MBUS_MUX_SHIFT 24
17 #define SUN8I_MBUS_MUX_MASK 0x3
18 #define SUN8I_MBUS_DIV_SHIFT 0
19 #define SUN8I_MBUS_DIV_WIDTH 3
20 #define SUN8I_MBUS_MAX_PARENTS 4
22 static DEFINE_SPINLOCK(sun8i_a23_mbus_lock);
24 static void __init sun8i_a23_mbus_setup(struct device_node *node)
26 int num_parents = of_clk_get_parent_count(node);
28 const char *clk_name = node->name;
30 struct clk_divider *div;
31 struct clk_gate *gate;
37 parents = kcalloc(num_parents, sizeof(*parents), GFP_KERNEL);
41 reg = of_io_request_and_map(node, 0, of_node_full_name(node));
43 pr_err("Could not get registers for sun8i-mbus-clk\n");
44 goto err_free_parents;
47 div = kzalloc(sizeof(*div), GFP_KERNEL);
51 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
55 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
59 of_property_read_string(node, "clock-output-names", &clk_name);
60 of_clk_parent_fill(node, parents, num_parents);
63 gate->bit_idx = SUN8I_MBUS_ENABLE;
64 gate->lock = &sun8i_a23_mbus_lock;
67 div->shift = SUN8I_MBUS_DIV_SHIFT;
68 div->width = SUN8I_MBUS_DIV_WIDTH;
69 div->lock = &sun8i_a23_mbus_lock;
72 mux->shift = SUN8I_MBUS_MUX_SHIFT;
73 mux->mask = SUN8I_MBUS_MUX_MASK;
74 mux->lock = &sun8i_a23_mbus_lock;
76 /* The MBUS clocks needs to be always enabled */
77 clk = clk_register_composite(NULL, clk_name, parents, num_parents,
78 &mux->hw, &clk_mux_ops,
79 &div->hw, &clk_divider_ops,
80 &gate->hw, &clk_gate_ops,
85 err = of_clk_add_provider(node, of_clk_src_simple_get, clk);
87 goto err_unregister_clk;
89 kfree(parents); /* parents is deep copied */
94 /* TODO: The composite clock stuff will leak a bit here. */
104 of_address_to_resource(node, 0, &res);
105 release_mem_region(res.start, resource_size(&res));
109 CLK_OF_DECLARE(sun8i_a23_mbus, "allwinner,sun8i-a23-mbus-clk", sun8i_a23_mbus_setup);