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>
20 * DOC: basic gatable clock which can gate and ungate it's ouput
22 * Traits of this clock:
23 * prepare - clk_(un)prepare only ensures parent is (un)prepared
24 * enable - clk_enable and clk_disable are functional & control gating
25 * rate - inherits rate from parent. No clk_set_rate support
26 * parent - fixed parent. No clk_set_parent support
29 #define to_clk_gate(_hw) container_of(_hw, struct clk_gate, hw)
31 static int clk_gate2_enable(struct clk_hw *hw)
33 struct clk_gate *gate = to_clk_gate(hw);
35 unsigned long flags = 0;
38 spin_lock_irqsave(gate->lock, flags);
40 reg = readl(gate->reg);
41 reg |= 3 << gate->bit_idx;
42 writel(reg, gate->reg);
45 spin_unlock_irqrestore(gate->lock, flags);
50 static void clk_gate2_disable(struct clk_hw *hw)
52 struct clk_gate *gate = to_clk_gate(hw);
54 unsigned long flags = 0;
57 spin_lock_irqsave(gate->lock, flags);
59 reg = readl(gate->reg);
60 reg &= ~(3 << gate->bit_idx);
61 writel(reg, gate->reg);
64 spin_unlock_irqrestore(gate->lock, flags);
67 static int clk_gate2_is_enabled(struct clk_hw *hw)
70 struct clk_gate *gate = to_clk_gate(hw);
72 reg = readl(gate->reg);
74 if (((reg >> gate->bit_idx) & 3) == 3)
80 static struct clk_ops clk_gate2_ops = {
81 .enable = clk_gate2_enable,
82 .disable = clk_gate2_disable,
83 .is_enabled = clk_gate2_is_enabled,
86 struct clk *clk_register_gate2(struct device *dev, const char *name,
87 const char *parent_name, unsigned long flags,
88 void __iomem *reg, u8 bit_idx,
89 u8 clk_gate2_flags, spinlock_t *lock)
91 struct clk_gate *gate;
93 struct clk_init_data init;
95 gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
97 return ERR_PTR(-ENOMEM);
99 /* struct clk_gate assignments */
101 gate->bit_idx = bit_idx;
102 gate->flags = clk_gate2_flags;
106 init.ops = &clk_gate2_ops;
108 init.parent_names = parent_name ? &parent_name : NULL;
109 init.num_parents = parent_name ? 1 : 0;
111 gate->hw.init = &init;
113 clk = clk_register(dev, &gate->hw);