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>
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)
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);
48 printk("\t%-15s\n", p->name);
53 void clk_print_usecounts(void)
58 spin_lock_irqsave(&clockfw_lock, flags);
59 list_for_each_entry(p, &clocks, node) {
61 printk("%-15s: %d\n", p->name, p->usecount);
65 spin_unlock_irqrestore(&clockfw_lock, flags);
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);
83 if (dev == NULL || dev->bus != &platform_bus_type)
86 idno = to_platform_device(dev)->id;
88 mutex_lock(&clocks_mutex);
90 list_for_each_entry(p, &clocks, node) {
92 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
98 list_for_each_entry(p, &clocks, node) {
99 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
106 mutex_unlock(&clocks_mutex);
110 EXPORT_SYMBOL(clk_get);
112 int clk_enable(struct clk *clk)
117 if (clk == NULL || IS_ERR(clk))
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);
127 EXPORT_SYMBOL(clk_enable);
129 void clk_disable(struct clk *clk)
133 if (clk == NULL || IS_ERR(clk))
136 spin_lock_irqsave(&clockfw_lock, flags);
137 if (clk->usecount == 0) {
138 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
144 if (arch_clock->clk_disable)
145 arch_clock->clk_disable(clk);
148 spin_unlock_irqrestore(&clockfw_lock, flags);
150 EXPORT_SYMBOL(clk_disable);
152 int clk_get_usecount(struct clk *clk)
157 if (clk == NULL || IS_ERR(clk))
160 spin_lock_irqsave(&clockfw_lock, flags);
162 spin_unlock_irqrestore(&clockfw_lock, flags);
166 EXPORT_SYMBOL(clk_get_usecount);
168 unsigned long clk_get_rate(struct clk *clk)
171 unsigned long ret = 0;
173 if (clk == NULL || IS_ERR(clk))
176 spin_lock_irqsave(&clockfw_lock, flags);
178 spin_unlock_irqrestore(&clockfw_lock, flags);
182 EXPORT_SYMBOL(clk_get_rate);
184 void clk_put(struct clk *clk)
186 if (clk && !IS_ERR(clk))
187 module_put(clk->owner);
189 EXPORT_SYMBOL(clk_put);
191 /*-------------------------------------------------------------------------
192 * Optional clock functions defined in include/linux/clk.h
193 *-------------------------------------------------------------------------*/
195 long clk_round_rate(struct clk *clk, unsigned long rate)
200 if (clk == NULL || IS_ERR(clk))
203 spin_lock_irqsave(&clockfw_lock, flags);
204 if (arch_clock->clk_round_rate)
205 ret = arch_clock->clk_round_rate(clk, rate);
206 spin_unlock_irqrestore(&clockfw_lock, flags);
210 EXPORT_SYMBOL(clk_round_rate);
212 int clk_set_rate(struct clk *clk, unsigned long rate)
217 if (clk == NULL || IS_ERR(clk))
220 spin_lock_irqsave(&clockfw_lock, flags);
221 if (arch_clock->clk_set_rate)
222 ret = arch_clock->clk_set_rate(clk, rate);
223 spin_unlock_irqrestore(&clockfw_lock, flags);
227 EXPORT_SYMBOL(clk_set_rate);
229 int clk_set_parent(struct clk *clk, struct clk *parent)
234 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
237 spin_lock_irqsave(&clockfw_lock, flags);
238 if (arch_clock->clk_set_parent)
239 ret = arch_clock->clk_set_parent(clk, parent);
240 spin_unlock_irqrestore(&clockfw_lock, flags);
244 EXPORT_SYMBOL(clk_set_parent);
246 struct clk *clk_get_parent(struct clk *clk)
249 struct clk * ret = NULL;
251 if (clk == NULL || IS_ERR(clk))
254 spin_lock_irqsave(&clockfw_lock, flags);
255 if (arch_clock->clk_get_parent)
256 ret = arch_clock->clk_get_parent(clk);
257 spin_unlock_irqrestore(&clockfw_lock, flags);
261 EXPORT_SYMBOL(clk_get_parent);
263 /*-------------------------------------------------------------------------
264 * OMAP specific clock functions shared between omap1 and omap2
265 *-------------------------------------------------------------------------*/
267 unsigned int __initdata mpurate;
270 * By default we use the rate set by the bootloader.
271 * You can override this with mpurate= cmdline option.
273 static int __init omap_clk_setup(char *str)
275 get_option(&str, &mpurate);
285 __setup("mpurate=", omap_clk_setup);
287 /* Used for clocks that always have same value as the parent clock */
288 void followparent_recalc(struct clk *clk)
290 if (clk == NULL || IS_ERR(clk))
293 clk->rate = clk->parent->rate;
294 if (unlikely(clk->flags & RATE_PROPAGATES))
298 /* Propagate rate to children */
299 void propagate_rate(struct clk * tclk)
303 if (tclk == NULL || IS_ERR(tclk))
306 list_for_each_entry(clkp, &clocks, node) {
307 if (likely(clkp->parent != tclk))
309 if (likely((u32)clkp->recalc))
315 * recalculate_root_clocks - recalculate and propagate all root clocks
317 * Recalculates all root clocks (clocks with no parent), which if the
318 * clock's .recalc is set correctly, should also propagate their rates.
321 void recalculate_root_clocks(void)
325 list_for_each_entry(clkp, &clocks, node) {
326 if (unlikely(!clkp->parent) && likely((u32)clkp->recalc))
331 int clk_register(struct clk *clk)
333 if (clk == NULL || IS_ERR(clk))
336 mutex_lock(&clocks_mutex);
337 list_add(&clk->node, &clocks);
340 mutex_unlock(&clocks_mutex);
344 EXPORT_SYMBOL(clk_register);
346 void clk_unregister(struct clk *clk)
348 if (clk == NULL || IS_ERR(clk))
351 mutex_lock(&clocks_mutex);
352 list_del(&clk->node);
353 mutex_unlock(&clocks_mutex);
355 EXPORT_SYMBOL(clk_unregister);
357 void clk_deny_idle(struct clk *clk)
361 if (clk == NULL || IS_ERR(clk))
364 spin_lock_irqsave(&clockfw_lock, flags);
365 if (arch_clock->clk_deny_idle)
366 arch_clock->clk_deny_idle(clk);
367 spin_unlock_irqrestore(&clockfw_lock, flags);
369 EXPORT_SYMBOL(clk_deny_idle);
371 void clk_allow_idle(struct clk *clk)
375 if (clk == NULL || IS_ERR(clk))
378 spin_lock_irqsave(&clockfw_lock, flags);
379 if (arch_clock->clk_allow_idle)
380 arch_clock->clk_allow_idle(clk);
381 spin_unlock_irqrestore(&clockfw_lock, flags);
383 EXPORT_SYMBOL(clk_allow_idle);
385 void clk_enable_init_clocks(void)
389 list_for_each_entry(clkp, &clocks, node) {
390 if (clkp->flags & ENABLE_ON_INIT)
394 EXPORT_SYMBOL(clk_enable_init_clocks);
396 #ifdef CONFIG_CPU_FREQ
397 void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
401 spin_lock_irqsave(&clockfw_lock, flags);
402 if (arch_clock->clk_init_cpufreq_table)
403 arch_clock->clk_init_cpufreq_table(table);
404 spin_unlock_irqrestore(&clockfw_lock, flags);
406 EXPORT_SYMBOL(clk_init_cpufreq_table);
409 /*-------------------------------------------------------------------------*/
411 #ifdef CONFIG_OMAP_RESET_CLOCKS
413 * Disable any unused clocks left on by the bootloader
415 static int __init clk_disable_unused(void)
420 list_for_each_entry(ck, &clocks, node) {
421 if (ck->usecount > 0 || (ck->flags & ALWAYS_ENABLED) ||
425 spin_lock_irqsave(&clockfw_lock, flags);
426 if (arch_clock->clk_disable_unused)
427 arch_clock->clk_disable_unused(ck);
428 spin_unlock_irqrestore(&clockfw_lock, flags);
433 late_initcall(clk_disable_unused);
436 int __init clk_init(struct clk_functions * custom_clocks)
438 if (!custom_clocks) {
439 printk(KERN_ERR "No custom clock functions registered\n");
443 arch_clock = custom_clocks;