]> Pileus Git - ~andy/linux/blob - drivers/clk/clk-gate.c
clk: add "const" for clk_ops of basic clks
[~andy/linux] / drivers / clk / clk-gate.c
1 /*
2  * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
3  * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
4  *
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.
8  *
9  * Gated clock implementation
10  */
11
12 #include <linux/clk-provider.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/io.h>
16 #include <linux/err.h>
17 #include <linux/string.h>
18
19 /**
20  * DOC: basic gatable clock which can gate and ungate it's ouput
21  *
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
27  */
28
29 #define to_clk_gate(_hw) container_of(_hw, struct clk_gate, hw)
30
31 static void clk_gate_set_bit(struct clk_gate *gate)
32 {
33         u32 reg;
34         unsigned long flags = 0;
35
36         if (gate->lock)
37                 spin_lock_irqsave(gate->lock, flags);
38
39         reg = readl(gate->reg);
40         reg |= BIT(gate->bit_idx);
41         writel(reg, gate->reg);
42
43         if (gate->lock)
44                 spin_unlock_irqrestore(gate->lock, flags);
45 }
46
47 static void clk_gate_clear_bit(struct clk_gate *gate)
48 {
49         u32 reg;
50         unsigned long flags = 0;
51
52         if (gate->lock)
53                 spin_lock_irqsave(gate->lock, flags);
54
55         reg = readl(gate->reg);
56         reg &= ~BIT(gate->bit_idx);
57         writel(reg, gate->reg);
58
59         if (gate->lock)
60                 spin_unlock_irqrestore(gate->lock, flags);
61 }
62
63 static int clk_gate_enable(struct clk_hw *hw)
64 {
65         struct clk_gate *gate = to_clk_gate(hw);
66
67         if (gate->flags & CLK_GATE_SET_TO_DISABLE)
68                 clk_gate_clear_bit(gate);
69         else
70                 clk_gate_set_bit(gate);
71
72         return 0;
73 }
74
75 static void clk_gate_disable(struct clk_hw *hw)
76 {
77         struct clk_gate *gate = to_clk_gate(hw);
78
79         if (gate->flags & CLK_GATE_SET_TO_DISABLE)
80                 clk_gate_set_bit(gate);
81         else
82                 clk_gate_clear_bit(gate);
83 }
84
85 static int clk_gate_is_enabled(struct clk_hw *hw)
86 {
87         u32 reg;
88         struct clk_gate *gate = to_clk_gate(hw);
89
90         reg = readl(gate->reg);
91
92         /* if a set bit disables this clk, flip it before masking */
93         if (gate->flags & CLK_GATE_SET_TO_DISABLE)
94                 reg ^= BIT(gate->bit_idx);
95
96         reg &= BIT(gate->bit_idx);
97
98         return reg ? 1 : 0;
99 }
100
101 const struct clk_ops clk_gate_ops = {
102         .enable = clk_gate_enable,
103         .disable = clk_gate_disable,
104         .is_enabled = clk_gate_is_enabled,
105 };
106 EXPORT_SYMBOL_GPL(clk_gate_ops);
107
108 struct clk *clk_register_gate(struct device *dev, const char *name,
109                 const char *parent_name, unsigned long flags,
110                 void __iomem *reg, u8 bit_idx,
111                 u8 clk_gate_flags, spinlock_t *lock)
112 {
113         struct clk_gate *gate;
114         struct clk *clk;
115
116         gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
117
118         if (!gate) {
119                 pr_err("%s: could not allocate gated clk\n", __func__);
120                 return NULL;
121         }
122
123         /* struct clk_gate assignments */
124         gate->reg = reg;
125         gate->bit_idx = bit_idx;
126         gate->flags = clk_gate_flags;
127         gate->lock = lock;
128
129         if (parent_name) {
130                 gate->parent[0] = kstrdup(parent_name, GFP_KERNEL);
131                 if (!gate->parent[0])
132                         goto out;
133         }
134
135         clk = clk_register(dev, name,
136                         &clk_gate_ops, &gate->hw,
137                         gate->parent,
138                         (parent_name ? 1 : 0),
139                         flags);
140         if (clk)
141                 return clk;
142 out:
143         kfree(gate->parent[0]);
144         kfree(gate);
145
146         return NULL;
147 }