5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Gated clock implementation
12 #include <linux/clk-provider.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
16 #include <linux/err.h>
17 #include <linux/string.h>
21 * DOC: basic gatable clock which can gate and ungate it's ouput
23 * Traits of this clock:
24 * prepare - clk_(un)prepare only ensures parent is (un)prepared
25 * enable - clk_enable and clk_disable are functional & control gating
26 * rate - inherits rate from parent. No clk_set_rate support
27 * parent - fixed parent. No clk_set_parent support
37 unsigned int *share_count;
40 #define to_clk_gate2(_hw) container_of(_hw, struct clk_gate2, hw)
42 static int clk_gate2_enable(struct clk_hw *hw)
44 struct clk_gate2 *gate = to_clk_gate2(hw);
46 unsigned long flags = 0;
48 spin_lock_irqsave(gate->lock, flags);
50 if (gate->share_count && (*gate->share_count)++ > 0)
53 reg = readl(gate->reg);
54 reg &= ~(3 << gate->bit_idx);
55 reg |= gate->cgr_val << gate->bit_idx;
56 writel(reg, gate->reg);
59 spin_unlock_irqrestore(gate->lock, flags);
64 static void clk_gate2_disable(struct clk_hw *hw)
66 struct clk_gate2 *gate = to_clk_gate2(hw);
68 unsigned long flags = 0;
70 spin_lock_irqsave(gate->lock, flags);
72 if (gate->share_count) {
73 if (WARN_ON(*gate->share_count == 0))
75 else if (--(*gate->share_count) > 0)
79 reg = readl(gate->reg);
80 reg &= ~(3 << gate->bit_idx);
81 writel(reg, gate->reg);
84 spin_unlock_irqrestore(gate->lock, flags);
87 static int clk_gate2_reg_is_enabled(void __iomem *reg, u8 bit_idx)
91 if (((val >> bit_idx) & 1) == 1)
97 static int clk_gate2_is_enabled(struct clk_hw *hw)
99 struct clk_gate2 *gate = to_clk_gate2(hw);
101 return clk_gate2_reg_is_enabled(gate->reg, gate->bit_idx);
104 static void clk_gate2_disable_unused(struct clk_hw *hw)
106 struct clk_gate2 *gate = to_clk_gate2(hw);
107 unsigned long flags = 0;
110 spin_lock_irqsave(gate->lock, flags);
112 if (!gate->share_count || *gate->share_count == 0) {
113 reg = readl(gate->reg);
114 reg &= ~(3 << gate->bit_idx);
115 writel(reg, gate->reg);
118 spin_unlock_irqrestore(gate->lock, flags);
121 static const struct clk_ops clk_gate2_ops = {
122 .enable = clk_gate2_enable,
123 .disable = clk_gate2_disable,
124 .disable_unused = clk_gate2_disable_unused,
125 .is_enabled = clk_gate2_is_enabled,
128 struct clk *clk_register_gate2(struct device *dev, const char *name,
129 const char *parent_name, unsigned long flags,
130 void __iomem *reg, u8 bit_idx, u8 cgr_val,
131 u8 clk_gate2_flags, spinlock_t *lock,
132 unsigned int *share_count)
134 struct clk_gate2 *gate;
136 struct clk_init_data init;
138 gate = kzalloc(sizeof(struct clk_gate2), GFP_KERNEL);
140 return ERR_PTR(-ENOMEM);
142 /* struct clk_gate2 assignments */
144 gate->bit_idx = bit_idx;
145 gate->cgr_val = cgr_val;
146 gate->flags = clk_gate2_flags;
148 gate->share_count = share_count;
151 init.ops = &clk_gate2_ops;
153 init.parent_names = parent_name ? &parent_name : NULL;
154 init.num_parents = parent_name ? 1 : 0;
156 gate->hw.init = &init;
158 clk = clk_register(dev, &gate->hw);