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
35 unsigned int *share_count;
38 #define to_clk_gate2(_hw) container_of(_hw, struct clk_gate2, hw)
40 static int clk_gate2_enable(struct clk_hw *hw)
42 struct clk_gate2 *gate = to_clk_gate2(hw);
47 spin_lock_irqsave(gate->lock, flags);
49 if (gate->share_count && (*gate->share_count)++ > 0)
52 if (gate->flags & IMX_CLK_GATE2_SINGLE_BIT) {
53 ret = clk_gate_ops.enable(hw);
55 reg = readl(gate->reg);
56 reg &= ~(3 << gate->bit_idx);
57 reg |= gate->cgr_val << gate->bit_idx;
58 writel(reg, gate->reg);
62 spin_unlock_irqrestore(gate->lock, flags);
67 static void clk_gate2_disable(struct clk_hw *hw)
69 struct clk_gate2 *gate = to_clk_gate2(hw);
73 spin_lock_irqsave(gate->lock, flags);
75 if (gate->share_count) {
76 if (WARN_ON(*gate->share_count == 0))
78 else if (--(*gate->share_count) > 0)
82 if (gate->flags & IMX_CLK_GATE2_SINGLE_BIT) {
83 clk_gate_ops.disable(hw);
85 reg = readl(gate->reg);
86 reg &= ~(3 << gate->bit_idx);
87 writel(reg, gate->reg);
91 spin_unlock_irqrestore(gate->lock, flags);
94 static int clk_gate2_reg_is_enabled(void __iomem *reg, u8 bit_idx)
98 if (((val >> bit_idx) & 1) == 1)
104 static int clk_gate2_is_enabled(struct clk_hw *hw)
106 struct clk_gate2 *gate = to_clk_gate2(hw);
108 if (gate->flags & IMX_CLK_GATE2_SINGLE_BIT)
109 return clk_gate_ops.is_enabled(hw);
111 return clk_gate2_reg_is_enabled(gate->reg, gate->bit_idx);
114 static void clk_gate2_disable_unused(struct clk_hw *hw)
116 struct clk_gate2 *gate = to_clk_gate2(hw);
120 if (gate->flags & IMX_CLK_GATE2_SINGLE_BIT)
123 spin_lock_irqsave(gate->lock, flags);
125 if (!gate->share_count || *gate->share_count == 0) {
126 reg = readl(gate->reg);
127 reg &= ~(3 << gate->bit_idx);
128 writel(reg, gate->reg);
131 spin_unlock_irqrestore(gate->lock, flags);
134 static const struct clk_ops clk_gate2_ops = {
135 .enable = clk_gate2_enable,
136 .disable = clk_gate2_disable,
137 .disable_unused = clk_gate2_disable_unused,
138 .is_enabled = clk_gate2_is_enabled,
141 struct clk_hw *clk_hw_register_gate2(struct device *dev, const char *name,
142 const char *parent_name, unsigned long flags,
143 void __iomem *reg, u8 bit_idx, u8 cgr_val,
144 u8 clk_gate2_flags, spinlock_t *lock,
145 unsigned int *share_count)
147 struct clk_gate2 *gate;
149 struct clk_init_data init;
152 gate = kzalloc(sizeof(struct clk_gate2), GFP_KERNEL);
154 return ERR_PTR(-ENOMEM);
156 /* struct clk_gate2 assignments */
158 gate->bit_idx = bit_idx;
159 gate->cgr_val = cgr_val;
160 gate->flags = clk_gate2_flags;
162 gate->share_count = share_count;
165 init.ops = &clk_gate2_ops;
167 init.parent_names = parent_name ? &parent_name : NULL;
168 init.num_parents = parent_name ? 1 : 0;
170 gate->hw.init = &init;
173 ret = clk_hw_register(dev, hw);
181 EXPORT_SYMBOL_GPL(clk_hw_register_gate2);