ARM: OMAP: Add sanity check to clk_disable
[linux-2.6/openmoko-kernel.git] / arch / arm / plat-omap / clock.c
blob4abf2714f2d9f9be9de5b9bbe83441b41cdbd2dd
1 /*
2 * linux/arch/arm/plat-omap/clock.c
4 * Copyright (C) 2004 - 2005 Nokia corporation
5 * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
7 * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
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.
13 #include <linux/version.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/list.h>
18 #include <linux/errno.h>
19 #include <linux/err.h>
20 #include <linux/string.h>
21 #include <linux/clk.h>
22 #include <linux/mutex.h>
23 #include <linux/platform_device.h>
25 #include <asm/io.h>
26 #include <asm/semaphore.h>
28 #include <asm/arch/clock.h>
30 static LIST_HEAD(clocks);
31 static DEFINE_MUTEX(clocks_mutex);
32 static DEFINE_SPINLOCK(clockfw_lock);
34 static struct clk_functions *arch_clock;
36 /*-------------------------------------------------------------------------
37 * Standard clock functions defined in include/linux/clk.h
38 *-------------------------------------------------------------------------*/
41 * Returns a clock. Note that we first try to use device id on the bus
42 * and clock name. If this fails, we try to use clock name only.
44 struct clk * clk_get(struct device *dev, const char *id)
46 struct clk *p, *clk = ERR_PTR(-ENOENT);
47 int idno;
49 if (dev == NULL || dev->bus != &platform_bus_type)
50 idno = -1;
51 else
52 idno = to_platform_device(dev)->id;
54 mutex_lock(&clocks_mutex);
56 list_for_each_entry(p, &clocks, node) {
57 if (p->id == idno &&
58 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
59 clk = p;
60 goto found;
64 list_for_each_entry(p, &clocks, node) {
65 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
66 clk = p;
67 break;
71 found:
72 mutex_unlock(&clocks_mutex);
74 return clk;
76 EXPORT_SYMBOL(clk_get);
78 int clk_enable(struct clk *clk)
80 unsigned long flags;
81 int ret = 0;
83 if (clk == NULL || IS_ERR(clk))
84 return -EINVAL;
86 spin_lock_irqsave(&clockfw_lock, flags);
87 if (arch_clock->clk_enable)
88 ret = arch_clock->clk_enable(clk);
89 spin_unlock_irqrestore(&clockfw_lock, flags);
91 return ret;
93 EXPORT_SYMBOL(clk_enable);
95 void clk_disable(struct clk *clk)
97 unsigned long flags;
99 if (clk == NULL || IS_ERR(clk))
100 return;
102 spin_lock_irqsave(&clockfw_lock, flags);
103 BUG_ON(clk->usecount == 0);
104 if (arch_clock->clk_disable)
105 arch_clock->clk_disable(clk);
106 spin_unlock_irqrestore(&clockfw_lock, flags);
108 EXPORT_SYMBOL(clk_disable);
110 int clk_get_usecount(struct clk *clk)
112 unsigned long flags;
113 int ret = 0;
115 if (clk == NULL || IS_ERR(clk))
116 return 0;
118 spin_lock_irqsave(&clockfw_lock, flags);
119 ret = clk->usecount;
120 spin_unlock_irqrestore(&clockfw_lock, flags);
122 return ret;
124 EXPORT_SYMBOL(clk_get_usecount);
126 unsigned long clk_get_rate(struct clk *clk)
128 unsigned long flags;
129 unsigned long ret = 0;
131 if (clk == NULL || IS_ERR(clk))
132 return 0;
134 spin_lock_irqsave(&clockfw_lock, flags);
135 ret = clk->rate;
136 spin_unlock_irqrestore(&clockfw_lock, flags);
138 return ret;
140 EXPORT_SYMBOL(clk_get_rate);
142 void clk_put(struct clk *clk)
144 if (clk && !IS_ERR(clk))
145 module_put(clk->owner);
147 EXPORT_SYMBOL(clk_put);
149 /*-------------------------------------------------------------------------
150 * Optional clock functions defined in include/linux/clk.h
151 *-------------------------------------------------------------------------*/
153 long clk_round_rate(struct clk *clk, unsigned long rate)
155 unsigned long flags;
156 long ret = 0;
158 if (clk == NULL || IS_ERR(clk))
159 return ret;
161 spin_lock_irqsave(&clockfw_lock, flags);
162 if (arch_clock->clk_round_rate)
163 ret = arch_clock->clk_round_rate(clk, rate);
164 spin_unlock_irqrestore(&clockfw_lock, flags);
166 return ret;
168 EXPORT_SYMBOL(clk_round_rate);
170 int clk_set_rate(struct clk *clk, unsigned long rate)
172 unsigned long flags;
173 int ret = -EINVAL;
175 if (clk == NULL || IS_ERR(clk))
176 return ret;
178 spin_lock_irqsave(&clockfw_lock, flags);
179 if (arch_clock->clk_set_rate)
180 ret = arch_clock->clk_set_rate(clk, rate);
181 spin_unlock_irqrestore(&clockfw_lock, flags);
183 return ret;
185 EXPORT_SYMBOL(clk_set_rate);
187 int clk_set_parent(struct clk *clk, struct clk *parent)
189 unsigned long flags;
190 int ret = -EINVAL;
192 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
193 return ret;
195 spin_lock_irqsave(&clockfw_lock, flags);
196 if (arch_clock->clk_set_parent)
197 ret = arch_clock->clk_set_parent(clk, parent);
198 spin_unlock_irqrestore(&clockfw_lock, flags);
200 return ret;
202 EXPORT_SYMBOL(clk_set_parent);
204 struct clk *clk_get_parent(struct clk *clk)
206 unsigned long flags;
207 struct clk * ret = NULL;
209 if (clk == NULL || IS_ERR(clk))
210 return ret;
212 spin_lock_irqsave(&clockfw_lock, flags);
213 if (arch_clock->clk_get_parent)
214 ret = arch_clock->clk_get_parent(clk);
215 spin_unlock_irqrestore(&clockfw_lock, flags);
217 return ret;
219 EXPORT_SYMBOL(clk_get_parent);
221 /*-------------------------------------------------------------------------
222 * OMAP specific clock functions shared between omap1 and omap2
223 *-------------------------------------------------------------------------*/
225 unsigned int __initdata mpurate;
228 * By default we use the rate set by the bootloader.
229 * You can override this with mpurate= cmdline option.
231 static int __init omap_clk_setup(char *str)
233 get_option(&str, &mpurate);
235 if (!mpurate)
236 return 1;
238 if (mpurate < 1000)
239 mpurate *= 1000000;
241 return 1;
243 __setup("mpurate=", omap_clk_setup);
245 /* Used for clocks that always have same value as the parent clock */
246 void followparent_recalc(struct clk *clk)
248 if (clk == NULL || IS_ERR(clk))
249 return;
251 clk->rate = clk->parent->rate;
254 /* Propagate rate to children */
255 void propagate_rate(struct clk * tclk)
257 struct clk *clkp;
259 if (tclk == NULL || IS_ERR(tclk))
260 return;
262 list_for_each_entry(clkp, &clocks, node) {
263 if (likely(clkp->parent != tclk))
264 continue;
265 if (likely((u32)clkp->recalc))
266 clkp->recalc(clkp);
270 int clk_register(struct clk *clk)
272 if (clk == NULL || IS_ERR(clk))
273 return -EINVAL;
275 mutex_lock(&clocks_mutex);
276 list_add(&clk->node, &clocks);
277 if (clk->init)
278 clk->init(clk);
279 mutex_unlock(&clocks_mutex);
281 return 0;
283 EXPORT_SYMBOL(clk_register);
285 void clk_unregister(struct clk *clk)
287 if (clk == NULL || IS_ERR(clk))
288 return;
290 mutex_lock(&clocks_mutex);
291 list_del(&clk->node);
292 mutex_unlock(&clocks_mutex);
294 EXPORT_SYMBOL(clk_unregister);
296 void clk_deny_idle(struct clk *clk)
298 unsigned long flags;
300 if (clk == NULL || IS_ERR(clk))
301 return;
303 spin_lock_irqsave(&clockfw_lock, flags);
304 if (arch_clock->clk_deny_idle)
305 arch_clock->clk_deny_idle(clk);
306 spin_unlock_irqrestore(&clockfw_lock, flags);
308 EXPORT_SYMBOL(clk_deny_idle);
310 void clk_allow_idle(struct clk *clk)
312 unsigned long flags;
314 if (clk == NULL || IS_ERR(clk))
315 return;
317 spin_lock_irqsave(&clockfw_lock, flags);
318 if (arch_clock->clk_allow_idle)
319 arch_clock->clk_allow_idle(clk);
320 spin_unlock_irqrestore(&clockfw_lock, flags);
322 EXPORT_SYMBOL(clk_allow_idle);
324 /*-------------------------------------------------------------------------*/
326 int __init clk_init(struct clk_functions * custom_clocks)
328 if (!custom_clocks) {
329 printk(KERN_ERR "No custom clock functions registered\n");
330 BUG();
333 arch_clock = custom_clocks;
335 return 0;