1 // SPDX-License-Identifier: GPL-2.0
6 * Gated clock implementation
9 #include <linux/clk-provider.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
13 #include <linux/err.h>
14 #include <linux/string.h>
17 * DOC: basic gatable clock which can gate and ungate it's ouput
19 * Traits of this clock:
20 * prepare - clk_(un)prepare only ensures parent is (un)prepared
21 * enable - clk_enable and clk_disable are functional & control gating
22 * rate - inherits rate from parent. No clk_set_rate support
23 * parent - fixed parent. No clk_set_parent support
27 * It works on following logic:
29 * For enabling clock, enable = 1
30 * set2dis = 1 -> clear bit -> set = 0
31 * set2dis = 0 -> set bit -> set = 1
33 * For disabling clock, enable = 0
34 * set2dis = 1 -> set bit -> set = 1
35 * set2dis = 0 -> clear bit -> set = 0
37 * So, result is always: enable xor set2dis.
39 static void clk_gate_endisable(struct clk_hw *hw, int enable)
41 struct clk_gate *gate = to_clk_gate(hw);
42 int set = gate->flags & CLK_GATE_SET_TO_DISABLE ? 1 : 0;
43 unsigned long uninitialized_var(flags);
49 spin_lock_irqsave(gate->lock, flags);
51 __acquire(gate->lock);
53 if (gate->flags & CLK_GATE_HIWORD_MASK) {
54 reg = BIT(gate->bit_idx + 16);
56 reg |= BIT(gate->bit_idx);
58 reg = clk_readl(gate->reg);
61 reg |= BIT(gate->bit_idx);
63 reg &= ~BIT(gate->bit_idx);
66 clk_writel(reg, gate->reg);
69 spin_unlock_irqrestore(gate->lock, flags);
71 __release(gate->lock);
74 static int clk_gate_enable(struct clk_hw *hw)
76 clk_gate_endisable(hw, 1);
81 static void clk_gate_disable(struct clk_hw *hw)
83 clk_gate_endisable(hw, 0);
86 int clk_gate_is_enabled(struct clk_hw *hw)
89 struct clk_gate *gate = to_clk_gate(hw);
91 reg = clk_readl(gate->reg);
93 /* if a set bit disables this clk, flip it before masking */
94 if (gate->flags & CLK_GATE_SET_TO_DISABLE)
95 reg ^= BIT(gate->bit_idx);
97 reg &= BIT(gate->bit_idx);
101 EXPORT_SYMBOL_GPL(clk_gate_is_enabled);
103 const struct clk_ops clk_gate_ops = {
104 .enable = clk_gate_enable,
105 .disable = clk_gate_disable,
106 .is_enabled = clk_gate_is_enabled,
108 EXPORT_SYMBOL_GPL(clk_gate_ops);
111 * clk_hw_register_gate - register a gate clock with the clock framework
112 * @dev: device that is registering this clock
113 * @name: name of this clock
114 * @parent_name: name of this clock's parent
115 * @flags: framework-specific flags for this clock
116 * @reg: register address to control gating of this clock
117 * @bit_idx: which bit in the register controls gating of this clock
118 * @clk_gate_flags: gate-specific flags for this clock
119 * @lock: shared register lock for this clock
121 struct clk_hw *clk_hw_register_gate(struct device *dev, const char *name,
122 const char *parent_name, unsigned long flags,
123 void __iomem *reg, u8 bit_idx,
124 u8 clk_gate_flags, spinlock_t *lock)
126 struct clk_gate *gate;
128 struct clk_init_data init;
131 if (clk_gate_flags & CLK_GATE_HIWORD_MASK) {
133 pr_err("gate bit exceeds LOWORD field\n");
134 return ERR_PTR(-EINVAL);
138 /* allocate the gate */
139 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
141 return ERR_PTR(-ENOMEM);
144 init.ops = &clk_gate_ops;
145 init.flags = flags | CLK_IS_BASIC;
146 init.parent_names = parent_name ? &parent_name : NULL;
147 init.num_parents = parent_name ? 1 : 0;
149 /* struct clk_gate assignments */
151 gate->bit_idx = bit_idx;
152 gate->flags = clk_gate_flags;
154 gate->hw.init = &init;
157 ret = clk_hw_register(dev, hw);
165 EXPORT_SYMBOL_GPL(clk_hw_register_gate);
167 struct clk *clk_register_gate(struct device *dev, const char *name,
168 const char *parent_name, unsigned long flags,
169 void __iomem *reg, u8 bit_idx,
170 u8 clk_gate_flags, spinlock_t *lock)
174 hw = clk_hw_register_gate(dev, name, parent_name, flags, reg,
175 bit_idx, clk_gate_flags, lock);
180 EXPORT_SYMBOL_GPL(clk_register_gate);
182 void clk_unregister_gate(struct clk *clk)
184 struct clk_gate *gate;
187 hw = __clk_get_hw(clk);
191 gate = to_clk_gate(hw);
196 EXPORT_SYMBOL_GPL(clk_unregister_gate);
198 void clk_hw_unregister_gate(struct clk_hw *hw)
200 struct clk_gate *gate;
202 gate = to_clk_gate(hw);
204 clk_hw_unregister(hw);
207 EXPORT_SYMBOL_GPL(clk_hw_unregister_gate);