docbook: fix fatal rapidio yet again (and more to come)
[linux/fpc-iii.git] / arch / arm / plat-omap / clock.c
blob72d34a23a2ec6276073af8906e916075b6808945
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>
27 #include <asm/arch/clock.h>
29 static LIST_HEAD(clocks);
30 static DEFINE_MUTEX(clocks_mutex);
31 static DEFINE_SPINLOCK(clockfw_lock);
33 static struct clk_functions *arch_clock;
35 #ifdef CONFIG_PM_DEBUG
37 static void print_parents(struct clk *clk)
39 struct clk *p;
40 int printed = 0;
42 list_for_each_entry(p, &clocks, node) {
43 if (p->parent == clk && p->usecount) {
44 if (!clk->usecount && !printed) {
45 printk("MISMATCH: %s\n", clk->name);
46 printed = 1;
48 printk("\t%-15s\n", p->name);
53 void clk_print_usecounts(void)
55 unsigned long flags;
56 struct clk *p;
58 spin_lock_irqsave(&clockfw_lock, flags);
59 list_for_each_entry(p, &clocks, node) {
60 if (p->usecount)
61 printk("%-15s: %d\n", p->name, p->usecount);
62 print_parents(p);
65 spin_unlock_irqrestore(&clockfw_lock, flags);
68 #endif
70 /*-------------------------------------------------------------------------
71 * Standard clock functions defined in include/linux/clk.h
72 *-------------------------------------------------------------------------*/
75 * Returns a clock. Note that we first try to use device id on the bus
76 * and clock name. If this fails, we try to use clock name only.
78 struct clk * clk_get(struct device *dev, const char *id)
80 struct clk *p, *clk = ERR_PTR(-ENOENT);
81 int idno;
83 if (dev == NULL || dev->bus != &platform_bus_type)
84 idno = -1;
85 else
86 idno = to_platform_device(dev)->id;
88 mutex_lock(&clocks_mutex);
90 list_for_each_entry(p, &clocks, node) {
91 if (p->id == idno &&
92 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
93 clk = p;
94 goto found;
98 list_for_each_entry(p, &clocks, node) {
99 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
100 clk = p;
101 break;
105 found:
106 mutex_unlock(&clocks_mutex);
108 return clk;
110 EXPORT_SYMBOL(clk_get);
112 int clk_enable(struct clk *clk)
114 unsigned long flags;
115 int ret = 0;
117 if (clk == NULL || IS_ERR(clk))
118 return -EINVAL;
120 spin_lock_irqsave(&clockfw_lock, flags);
121 if (arch_clock->clk_enable)
122 ret = arch_clock->clk_enable(clk);
123 spin_unlock_irqrestore(&clockfw_lock, flags);
125 return ret;
127 EXPORT_SYMBOL(clk_enable);
129 void clk_disable(struct clk *clk)
131 unsigned long flags;
133 if (clk == NULL || IS_ERR(clk))
134 return;
136 spin_lock_irqsave(&clockfw_lock, flags);
137 BUG_ON(clk->usecount == 0);
138 if (arch_clock->clk_disable)
139 arch_clock->clk_disable(clk);
140 spin_unlock_irqrestore(&clockfw_lock, flags);
142 EXPORT_SYMBOL(clk_disable);
144 int clk_get_usecount(struct clk *clk)
146 unsigned long flags;
147 int ret = 0;
149 if (clk == NULL || IS_ERR(clk))
150 return 0;
152 spin_lock_irqsave(&clockfw_lock, flags);
153 ret = clk->usecount;
154 spin_unlock_irqrestore(&clockfw_lock, flags);
156 return ret;
158 EXPORT_SYMBOL(clk_get_usecount);
160 unsigned long clk_get_rate(struct clk *clk)
162 unsigned long flags;
163 unsigned long ret = 0;
165 if (clk == NULL || IS_ERR(clk))
166 return 0;
168 spin_lock_irqsave(&clockfw_lock, flags);
169 ret = clk->rate;
170 spin_unlock_irqrestore(&clockfw_lock, flags);
172 return ret;
174 EXPORT_SYMBOL(clk_get_rate);
176 void clk_put(struct clk *clk)
178 if (clk && !IS_ERR(clk))
179 module_put(clk->owner);
181 EXPORT_SYMBOL(clk_put);
183 /*-------------------------------------------------------------------------
184 * Optional clock functions defined in include/linux/clk.h
185 *-------------------------------------------------------------------------*/
187 long clk_round_rate(struct clk *clk, unsigned long rate)
189 unsigned long flags;
190 long ret = 0;
192 if (clk == NULL || IS_ERR(clk))
193 return ret;
195 spin_lock_irqsave(&clockfw_lock, flags);
196 if (arch_clock->clk_round_rate)
197 ret = arch_clock->clk_round_rate(clk, rate);
198 spin_unlock_irqrestore(&clockfw_lock, flags);
200 return ret;
202 EXPORT_SYMBOL(clk_round_rate);
204 int clk_set_rate(struct clk *clk, unsigned long rate)
206 unsigned long flags;
207 int ret = -EINVAL;
209 if (clk == NULL || IS_ERR(clk))
210 return ret;
212 spin_lock_irqsave(&clockfw_lock, flags);
213 if (arch_clock->clk_set_rate)
214 ret = arch_clock->clk_set_rate(clk, rate);
215 spin_unlock_irqrestore(&clockfw_lock, flags);
217 return ret;
219 EXPORT_SYMBOL(clk_set_rate);
221 int clk_set_parent(struct clk *clk, struct clk *parent)
223 unsigned long flags;
224 int ret = -EINVAL;
226 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
227 return ret;
229 spin_lock_irqsave(&clockfw_lock, flags);
230 if (arch_clock->clk_set_parent)
231 ret = arch_clock->clk_set_parent(clk, parent);
232 spin_unlock_irqrestore(&clockfw_lock, flags);
234 return ret;
236 EXPORT_SYMBOL(clk_set_parent);
238 struct clk *clk_get_parent(struct clk *clk)
240 unsigned long flags;
241 struct clk * ret = NULL;
243 if (clk == NULL || IS_ERR(clk))
244 return ret;
246 spin_lock_irqsave(&clockfw_lock, flags);
247 if (arch_clock->clk_get_parent)
248 ret = arch_clock->clk_get_parent(clk);
249 spin_unlock_irqrestore(&clockfw_lock, flags);
251 return ret;
253 EXPORT_SYMBOL(clk_get_parent);
255 /*-------------------------------------------------------------------------
256 * OMAP specific clock functions shared between omap1 and omap2
257 *-------------------------------------------------------------------------*/
259 unsigned int __initdata mpurate;
262 * By default we use the rate set by the bootloader.
263 * You can override this with mpurate= cmdline option.
265 static int __init omap_clk_setup(char *str)
267 get_option(&str, &mpurate);
269 if (!mpurate)
270 return 1;
272 if (mpurate < 1000)
273 mpurate *= 1000000;
275 return 1;
277 __setup("mpurate=", omap_clk_setup);
279 /* Used for clocks that always have same value as the parent clock */
280 void followparent_recalc(struct clk *clk)
282 if (clk == NULL || IS_ERR(clk))
283 return;
285 clk->rate = clk->parent->rate;
286 if (unlikely(clk->flags & RATE_PROPAGATES))
287 propagate_rate(clk);
290 /* Propagate rate to children */
291 void propagate_rate(struct clk * tclk)
293 struct clk *clkp;
295 if (tclk == NULL || IS_ERR(tclk))
296 return;
298 list_for_each_entry(clkp, &clocks, node) {
299 if (likely(clkp->parent != tclk))
300 continue;
301 if (likely((u32)clkp->recalc))
302 clkp->recalc(clkp);
307 * recalculate_root_clocks - recalculate and propagate all root clocks
309 * Recalculates all root clocks (clocks with no parent), which if the
310 * clock's .recalc is set correctly, should also propagate their rates.
311 * Called at init.
313 void recalculate_root_clocks(void)
315 struct clk *clkp;
317 list_for_each_entry(clkp, &clocks, node) {
318 if (unlikely(!clkp->parent) && likely((u32)clkp->recalc))
319 clkp->recalc(clkp);
323 int clk_register(struct clk *clk)
325 if (clk == NULL || IS_ERR(clk))
326 return -EINVAL;
328 mutex_lock(&clocks_mutex);
329 list_add(&clk->node, &clocks);
330 if (clk->init)
331 clk->init(clk);
332 mutex_unlock(&clocks_mutex);
334 return 0;
336 EXPORT_SYMBOL(clk_register);
338 void clk_unregister(struct clk *clk)
340 if (clk == NULL || IS_ERR(clk))
341 return;
343 mutex_lock(&clocks_mutex);
344 list_del(&clk->node);
345 mutex_unlock(&clocks_mutex);
347 EXPORT_SYMBOL(clk_unregister);
349 void clk_deny_idle(struct clk *clk)
351 unsigned long flags;
353 if (clk == NULL || IS_ERR(clk))
354 return;
356 spin_lock_irqsave(&clockfw_lock, flags);
357 if (arch_clock->clk_deny_idle)
358 arch_clock->clk_deny_idle(clk);
359 spin_unlock_irqrestore(&clockfw_lock, flags);
361 EXPORT_SYMBOL(clk_deny_idle);
363 void clk_allow_idle(struct clk *clk)
365 unsigned long flags;
367 if (clk == NULL || IS_ERR(clk))
368 return;
370 spin_lock_irqsave(&clockfw_lock, flags);
371 if (arch_clock->clk_allow_idle)
372 arch_clock->clk_allow_idle(clk);
373 spin_unlock_irqrestore(&clockfw_lock, flags);
375 EXPORT_SYMBOL(clk_allow_idle);
377 void clk_enable_init_clocks(void)
379 struct clk *clkp;
381 list_for_each_entry(clkp, &clocks, node) {
382 if (clkp->flags & ENABLE_ON_INIT)
383 clk_enable(clkp);
386 EXPORT_SYMBOL(clk_enable_init_clocks);
388 #ifdef CONFIG_CPU_FREQ
389 void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
391 unsigned long flags;
393 spin_lock_irqsave(&clockfw_lock, flags);
394 if (arch_clock->clk_init_cpufreq_table)
395 arch_clock->clk_init_cpufreq_table(table);
396 spin_unlock_irqrestore(&clockfw_lock, flags);
398 EXPORT_SYMBOL(clk_init_cpufreq_table);
399 #endif
401 /*-------------------------------------------------------------------------*/
403 #ifdef CONFIG_OMAP_RESET_CLOCKS
405 * Disable any unused clocks left on by the bootloader
407 static int __init clk_disable_unused(void)
409 struct clk *ck;
410 unsigned long flags;
412 list_for_each_entry(ck, &clocks, node) {
413 if (ck->usecount > 0 || (ck->flags & ALWAYS_ENABLED) ||
414 ck->enable_reg == 0)
415 continue;
417 spin_lock_irqsave(&clockfw_lock, flags);
418 if (arch_clock->clk_disable_unused)
419 arch_clock->clk_disable_unused(ck);
420 spin_unlock_irqrestore(&clockfw_lock, flags);
423 return 0;
425 late_initcall(clk_disable_unused);
426 #endif
428 int __init clk_init(struct clk_functions * custom_clocks)
430 if (!custom_clocks) {
431 printk(KERN_ERR "No custom clock functions registered\n");
432 BUG();
435 arch_clock = custom_clocks;
437 return 0;