1 // SPDX-License-Identifier: GPL-2.0-only
6 * Gated clock implementation
9 #include <linux/clk-provider.h>
10 #include <linux/export.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
14 #include <linux/err.h>
15 #include <linux/string.h>
19 * DOC: basic gateable clock which can gate and ungate its output
21 * Traits of this clock:
22 * prepare - clk_(un)prepare only ensures parent is (un)prepared
23 * enable - clk_enable and clk_disable are functional & control gating
24 * rate - inherits rate from parent. No clk_set_rate support
25 * parent - fixed parent. No clk_set_parent support
36 unsigned int *share_count;
39 #define to_clk_gate2(_hw) container_of(_hw, struct clk_gate2, hw)
41 static void clk_gate2_do_shared_clks(struct clk_hw *hw, bool enable)
43 struct clk_gate2 *gate = to_clk_gate2(hw);
46 reg = readl(gate->reg);
47 reg &= ~(gate->cgr_mask << gate->bit_idx);
49 reg |= (gate->cgr_val & gate->cgr_mask) << gate->bit_idx;
50 writel(reg, gate->reg);
53 static int clk_gate2_enable(struct clk_hw *hw)
55 struct clk_gate2 *gate = to_clk_gate2(hw);
58 spin_lock_irqsave(gate->lock, flags);
60 if (gate->share_count && (*gate->share_count)++ > 0)
63 clk_gate2_do_shared_clks(hw, true);
65 spin_unlock_irqrestore(gate->lock, flags);
70 static void clk_gate2_disable(struct clk_hw *hw)
72 struct clk_gate2 *gate = to_clk_gate2(hw);
75 spin_lock_irqsave(gate->lock, flags);
77 if (gate->share_count) {
78 if (WARN_ON(*gate->share_count == 0))
80 else if (--(*gate->share_count) > 0)
84 clk_gate2_do_shared_clks(hw, false);
86 spin_unlock_irqrestore(gate->lock, flags);
89 static int clk_gate2_reg_is_enabled(void __iomem *reg, u8 bit_idx,
90 u8 cgr_val, u8 cgr_mask)
94 if (((val >> bit_idx) & cgr_mask) == cgr_val)
100 static int clk_gate2_is_enabled(struct clk_hw *hw)
102 struct clk_gate2 *gate = to_clk_gate2(hw);
106 spin_lock_irqsave(gate->lock, flags);
108 ret = clk_gate2_reg_is_enabled(gate->reg, gate->bit_idx,
109 gate->cgr_val, gate->cgr_mask);
111 spin_unlock_irqrestore(gate->lock, flags);
116 static void clk_gate2_disable_unused(struct clk_hw *hw)
118 struct clk_gate2 *gate = to_clk_gate2(hw);
121 spin_lock_irqsave(gate->lock, flags);
123 if (!gate->share_count || *gate->share_count == 0)
124 clk_gate2_do_shared_clks(hw, false);
126 spin_unlock_irqrestore(gate->lock, flags);
129 static const struct clk_ops clk_gate2_ops = {
130 .enable = clk_gate2_enable,
131 .disable = clk_gate2_disable,
132 .disable_unused = clk_gate2_disable_unused,
133 .is_enabled = clk_gate2_is_enabled,
136 struct clk_hw *clk_hw_register_gate2(struct device *dev, const char *name,
137 const char *parent_name, unsigned long flags,
138 void __iomem *reg, u8 bit_idx, u8 cgr_val, u8 cgr_mask,
139 u8 clk_gate2_flags, spinlock_t *lock,
140 unsigned int *share_count)
142 struct clk_gate2 *gate;
144 struct clk_init_data init;
147 gate = kzalloc(sizeof(struct clk_gate2), GFP_KERNEL);
149 return ERR_PTR(-ENOMEM);
151 /* struct clk_gate2 assignments */
153 gate->bit_idx = bit_idx;
154 gate->cgr_val = cgr_val;
155 gate->cgr_mask = cgr_mask;
156 gate->flags = clk_gate2_flags;
158 gate->share_count = share_count;
161 init.ops = &clk_gate2_ops;
163 init.parent_names = parent_name ? &parent_name : NULL;
164 init.num_parents = parent_name ? 1 : 0;
166 gate->hw.init = &init;
169 ret = clk_hw_register(dev, hw);
177 EXPORT_SYMBOL_GPL(clk_hw_register_gate2);