Pull release into acpica branch
[linux-2.6] / arch / arm / plat-omap / clock.c
1 /*
2  *  linux/arch/arm/plat-omap/clock.c
3  *
4  *  Copyright (C) 2004 - 2005 Nokia corporation
5  *  Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
6  *
7  *  Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13 #include <linux/version.h>
14 #include <linux/config.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/list.h>
19 #include <linux/errno.h>
20 #include <linux/err.h>
21 #include <linux/string.h>
22 #include <linux/clk.h>
23 #include <linux/mutex.h>
24
25 #include <asm/io.h>
26 #include <asm/semaphore.h>
27
28 #include <asm/arch/clock.h>
29
30 LIST_HEAD(clocks);
31 static DEFINE_MUTEX(clocks_mutex);
32 DEFINE_SPINLOCK(clockfw_lock);
33
34 static struct clk_functions *arch_clock;
35
36 /*-------------------------------------------------------------------------
37  * Standard clock functions defined in include/linux/clk.h
38  *-------------------------------------------------------------------------*/
39
40 struct clk * clk_get(struct device *dev, const char *id)
41 {
42         struct clk *p, *clk = ERR_PTR(-ENOENT);
43
44         mutex_lock(&clocks_mutex);
45         list_for_each_entry(p, &clocks, node) {
46                 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
47                         clk = p;
48                         break;
49                 }
50         }
51         mutex_unlock(&clocks_mutex);
52
53         return clk;
54 }
55 EXPORT_SYMBOL(clk_get);
56
57 int clk_enable(struct clk *clk)
58 {
59         unsigned long flags;
60         int ret = 0;
61
62         spin_lock_irqsave(&clockfw_lock, flags);
63         if (arch_clock->clk_enable)
64                 ret = arch_clock->clk_enable(clk);
65         spin_unlock_irqrestore(&clockfw_lock, flags);
66
67         return ret;
68 }
69 EXPORT_SYMBOL(clk_enable);
70
71 void clk_disable(struct clk *clk)
72 {
73         unsigned long flags;
74
75         spin_lock_irqsave(&clockfw_lock, flags);
76         if (arch_clock->clk_disable)
77                 arch_clock->clk_disable(clk);
78         spin_unlock_irqrestore(&clockfw_lock, flags);
79 }
80 EXPORT_SYMBOL(clk_disable);
81
82 int clk_get_usecount(struct clk *clk)
83 {
84         unsigned long flags;
85         int ret = 0;
86
87         spin_lock_irqsave(&clockfw_lock, flags);
88         ret = clk->usecount;
89         spin_unlock_irqrestore(&clockfw_lock, flags);
90
91         return ret;
92 }
93 EXPORT_SYMBOL(clk_get_usecount);
94
95 unsigned long clk_get_rate(struct clk *clk)
96 {
97         unsigned long flags;
98         unsigned long ret = 0;
99
100         spin_lock_irqsave(&clockfw_lock, flags);
101         ret = clk->rate;
102         spin_unlock_irqrestore(&clockfw_lock, flags);
103
104         return ret;
105 }
106 EXPORT_SYMBOL(clk_get_rate);
107
108 void clk_put(struct clk *clk)
109 {
110         if (clk && !IS_ERR(clk))
111                 module_put(clk->owner);
112 }
113 EXPORT_SYMBOL(clk_put);
114
115 /*-------------------------------------------------------------------------
116  * Optional clock functions defined in include/linux/clk.h
117  *-------------------------------------------------------------------------*/
118
119 long clk_round_rate(struct clk *clk, unsigned long rate)
120 {
121         unsigned long flags;
122         long ret = 0;
123
124         spin_lock_irqsave(&clockfw_lock, flags);
125         if (arch_clock->clk_round_rate)
126                 ret = arch_clock->clk_round_rate(clk, rate);
127         spin_unlock_irqrestore(&clockfw_lock, flags);
128
129         return ret;
130 }
131 EXPORT_SYMBOL(clk_round_rate);
132
133 int clk_set_rate(struct clk *clk, unsigned long rate)
134 {
135         unsigned long flags;
136         int ret = 0;
137
138         spin_lock_irqsave(&clockfw_lock, flags);
139         if (arch_clock->clk_set_rate)
140                 ret = arch_clock->clk_set_rate(clk, rate);
141         spin_unlock_irqrestore(&clockfw_lock, flags);
142
143         return ret;
144 }
145 EXPORT_SYMBOL(clk_set_rate);
146
147 int clk_set_parent(struct clk *clk, struct clk *parent)
148 {
149         unsigned long flags;
150         int ret = 0;
151
152         spin_lock_irqsave(&clockfw_lock, flags);
153         if (arch_clock->clk_set_parent)
154                 ret =  arch_clock->clk_set_parent(clk, parent);
155         spin_unlock_irqrestore(&clockfw_lock, flags);
156
157         return ret;
158 }
159 EXPORT_SYMBOL(clk_set_parent);
160
161 struct clk *clk_get_parent(struct clk *clk)
162 {
163         unsigned long flags;
164         struct clk * ret = NULL;
165
166         spin_lock_irqsave(&clockfw_lock, flags);
167         if (arch_clock->clk_get_parent)
168                 ret = arch_clock->clk_get_parent(clk);
169         spin_unlock_irqrestore(&clockfw_lock, flags);
170
171         return ret;
172 }
173 EXPORT_SYMBOL(clk_get_parent);
174
175 /*-------------------------------------------------------------------------
176  * OMAP specific clock functions shared between omap1 and omap2
177  *-------------------------------------------------------------------------*/
178
179 unsigned int __initdata mpurate;
180
181 /*
182  * By default we use the rate set by the bootloader.
183  * You can override this with mpurate= cmdline option.
184  */
185 static int __init omap_clk_setup(char *str)
186 {
187         get_option(&str, &mpurate);
188
189         if (!mpurate)
190                 return 1;
191
192         if (mpurate < 1000)
193                 mpurate *= 1000000;
194
195         return 1;
196 }
197 __setup("mpurate=", omap_clk_setup);
198
199 /* Used for clocks that always have same value as the parent clock */
200 void followparent_recalc(struct clk *clk)
201 {
202         clk->rate = clk->parent->rate;
203 }
204
205 /* Propagate rate to children */
206 void propagate_rate(struct clk * tclk)
207 {
208         struct clk *clkp;
209
210         list_for_each_entry(clkp, &clocks, node) {
211                 if (likely(clkp->parent != tclk))
212                         continue;
213                 if (likely((u32)clkp->recalc))
214                         clkp->recalc(clkp);
215         }
216 }
217
218 int clk_register(struct clk *clk)
219 {
220         mutex_lock(&clocks_mutex);
221         list_add(&clk->node, &clocks);
222         if (clk->init)
223                 clk->init(clk);
224         mutex_unlock(&clocks_mutex);
225
226         return 0;
227 }
228 EXPORT_SYMBOL(clk_register);
229
230 void clk_unregister(struct clk *clk)
231 {
232         mutex_lock(&clocks_mutex);
233         list_del(&clk->node);
234         mutex_unlock(&clocks_mutex);
235 }
236 EXPORT_SYMBOL(clk_unregister);
237
238 void clk_deny_idle(struct clk *clk)
239 {
240         unsigned long flags;
241
242         spin_lock_irqsave(&clockfw_lock, flags);
243         if (arch_clock->clk_deny_idle)
244                 arch_clock->clk_deny_idle(clk);
245         spin_unlock_irqrestore(&clockfw_lock, flags);
246 }
247 EXPORT_SYMBOL(clk_deny_idle);
248
249 void clk_allow_idle(struct clk *clk)
250 {
251         unsigned long flags;
252
253         spin_lock_irqsave(&clockfw_lock, flags);
254         if (arch_clock->clk_allow_idle)
255                 arch_clock->clk_allow_idle(clk);
256         spin_unlock_irqrestore(&clockfw_lock, flags);
257 }
258 EXPORT_SYMBOL(clk_allow_idle);
259
260 /*-------------------------------------------------------------------------*/
261
262 int __init clk_init(struct clk_functions * custom_clocks)
263 {
264         if (!custom_clocks) {
265                 printk(KERN_ERR "No custom clock functions registered\n");
266                 BUG();
267         }
268
269         arch_clock = custom_clocks;
270
271         return 0;
272 }