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>
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 #ifdef CONFIG_PM_DEBUG
38 static void print_parents(struct clk *clk)
43 list_for_each_entry(p, &clocks, node) {
44 if (p->parent == clk && p->usecount) {
45 if (!clk->usecount && !printed) {
46 printk("MISMATCH: %s\n", clk->name);
49 printk("\t%-15s\n", p->name);
54 void clk_print_usecounts(void)
59 spin_lock_irqsave(&clockfw_lock, flags);
60 list_for_each_entry(p, &clocks, node) {
62 printk("%-15s: %d\n", p->name, p->usecount);
66 spin_unlock_irqrestore(&clockfw_lock, flags);
71 /*-------------------------------------------------------------------------
72 * Standard clock functions defined in include/linux/clk.h
73 *-------------------------------------------------------------------------*/
76 * Returns a clock. Note that we first try to use device id on the bus
77 * and clock name. If this fails, we try to use clock name only.
79 struct clk * clk_get(struct device *dev, const char *id)
81 struct clk *p, *clk = ERR_PTR(-ENOENT);
84 if (dev == NULL || dev->bus != &platform_bus_type)
87 idno = to_platform_device(dev)->id;
89 mutex_lock(&clocks_mutex);
91 list_for_each_entry(p, &clocks, node) {
93 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
99 list_for_each_entry(p, &clocks, node) {
100 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
107 mutex_unlock(&clocks_mutex);
111 EXPORT_SYMBOL(clk_get);
113 int clk_enable(struct clk *clk)
118 if (clk == NULL || IS_ERR(clk))
121 spin_lock_irqsave(&clockfw_lock, flags);
122 if (arch_clock->clk_enable)
123 ret = arch_clock->clk_enable(clk);
124 spin_unlock_irqrestore(&clockfw_lock, flags);
128 EXPORT_SYMBOL(clk_enable);
130 void clk_disable(struct clk *clk)
134 if (clk == NULL || IS_ERR(clk))
137 spin_lock_irqsave(&clockfw_lock, flags);
138 BUG_ON(clk->usecount == 0);
139 if (arch_clock->clk_disable)
140 arch_clock->clk_disable(clk);
141 spin_unlock_irqrestore(&clockfw_lock, flags);
143 EXPORT_SYMBOL(clk_disable);
145 int clk_get_usecount(struct clk *clk)
150 if (clk == NULL || IS_ERR(clk))
153 spin_lock_irqsave(&clockfw_lock, flags);
155 spin_unlock_irqrestore(&clockfw_lock, flags);
159 EXPORT_SYMBOL(clk_get_usecount);
161 unsigned long clk_get_rate(struct clk *clk)
164 unsigned long ret = 0;
166 if (clk == NULL || IS_ERR(clk))
169 spin_lock_irqsave(&clockfw_lock, flags);
171 spin_unlock_irqrestore(&clockfw_lock, flags);
175 EXPORT_SYMBOL(clk_get_rate);
177 void clk_put(struct clk *clk)
179 if (clk && !IS_ERR(clk))
180 module_put(clk->owner);
182 EXPORT_SYMBOL(clk_put);
184 /*-------------------------------------------------------------------------
185 * Optional clock functions defined in include/linux/clk.h
186 *-------------------------------------------------------------------------*/
188 long clk_round_rate(struct clk *clk, unsigned long rate)
193 if (clk == NULL || IS_ERR(clk))
196 spin_lock_irqsave(&clockfw_lock, flags);
197 if (arch_clock->clk_round_rate)
198 ret = arch_clock->clk_round_rate(clk, rate);
199 spin_unlock_irqrestore(&clockfw_lock, flags);
203 EXPORT_SYMBOL(clk_round_rate);
205 int clk_set_rate(struct clk *clk, unsigned long rate)
210 if (clk == NULL || IS_ERR(clk))
213 spin_lock_irqsave(&clockfw_lock, flags);
214 if (arch_clock->clk_set_rate)
215 ret = arch_clock->clk_set_rate(clk, rate);
216 spin_unlock_irqrestore(&clockfw_lock, flags);
220 EXPORT_SYMBOL(clk_set_rate);
222 int clk_set_parent(struct clk *clk, struct clk *parent)
227 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
230 spin_lock_irqsave(&clockfw_lock, flags);
231 if (arch_clock->clk_set_parent)
232 ret = arch_clock->clk_set_parent(clk, parent);
233 spin_unlock_irqrestore(&clockfw_lock, flags);
237 EXPORT_SYMBOL(clk_set_parent);
239 struct clk *clk_get_parent(struct clk *clk)
242 struct clk * ret = NULL;
244 if (clk == NULL || IS_ERR(clk))
247 spin_lock_irqsave(&clockfw_lock, flags);
248 if (arch_clock->clk_get_parent)
249 ret = arch_clock->clk_get_parent(clk);
250 spin_unlock_irqrestore(&clockfw_lock, flags);
254 EXPORT_SYMBOL(clk_get_parent);
256 /*-------------------------------------------------------------------------
257 * OMAP specific clock functions shared between omap1 and omap2
258 *-------------------------------------------------------------------------*/
260 unsigned int __initdata mpurate;
263 * By default we use the rate set by the bootloader.
264 * You can override this with mpurate= cmdline option.
266 static int __init omap_clk_setup(char *str)
268 get_option(&str, &mpurate);
278 __setup("mpurate=", omap_clk_setup);
280 /* Used for clocks that always have same value as the parent clock */
281 void followparent_recalc(struct clk *clk)
283 if (clk == NULL || IS_ERR(clk))
286 clk->rate = clk->parent->rate;
287 if (unlikely(clk->flags & RATE_PROPAGATES))
291 /* Propagate rate to children */
292 void propagate_rate(struct clk * tclk)
296 if (tclk == NULL || IS_ERR(tclk))
299 list_for_each_entry(clkp, &clocks, node) {
300 if (likely(clkp->parent != tclk))
302 if (likely((u32)clkp->recalc))
308 * recalculate_root_clocks - recalculate and propagate all root clocks
310 * Recalculates all root clocks (clocks with no parent), which if the
311 * clock's .recalc is set correctly, should also propagate their rates.
314 void recalculate_root_clocks(void)
318 list_for_each_entry(clkp, &clocks, node) {
319 if (unlikely(!clkp->parent) && likely((u32)clkp->recalc))
324 int clk_register(struct clk *clk)
326 if (clk == NULL || IS_ERR(clk))
329 mutex_lock(&clocks_mutex);
330 list_add(&clk->node, &clocks);
333 mutex_unlock(&clocks_mutex);
337 EXPORT_SYMBOL(clk_register);
339 void clk_unregister(struct clk *clk)
341 if (clk == NULL || IS_ERR(clk))
344 mutex_lock(&clocks_mutex);
345 list_del(&clk->node);
346 mutex_unlock(&clocks_mutex);
348 EXPORT_SYMBOL(clk_unregister);
350 void clk_deny_idle(struct clk *clk)
354 if (clk == NULL || IS_ERR(clk))
357 spin_lock_irqsave(&clockfw_lock, flags);
358 if (arch_clock->clk_deny_idle)
359 arch_clock->clk_deny_idle(clk);
360 spin_unlock_irqrestore(&clockfw_lock, flags);
362 EXPORT_SYMBOL(clk_deny_idle);
364 void clk_allow_idle(struct clk *clk)
368 if (clk == NULL || IS_ERR(clk))
371 spin_lock_irqsave(&clockfw_lock, flags);
372 if (arch_clock->clk_allow_idle)
373 arch_clock->clk_allow_idle(clk);
374 spin_unlock_irqrestore(&clockfw_lock, flags);
376 EXPORT_SYMBOL(clk_allow_idle);
378 void clk_enable_init_clocks(void)
382 list_for_each_entry(clkp, &clocks, node) {
383 if (clkp->flags & ENABLE_ON_INIT)
387 EXPORT_SYMBOL(clk_enable_init_clocks);
389 #ifdef CONFIG_CPU_FREQ
390 void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
394 spin_lock_irqsave(&clockfw_lock, flags);
395 if (arch_clock->clk_init_cpufreq_table)
396 arch_clock->clk_init_cpufreq_table(table);
397 spin_unlock_irqrestore(&clockfw_lock, flags);
399 EXPORT_SYMBOL(clk_init_cpufreq_table);
402 /*-------------------------------------------------------------------------*/
404 #ifdef CONFIG_OMAP_RESET_CLOCKS
406 * Disable any unused clocks left on by the bootloader
408 static int __init clk_disable_unused(void)
413 list_for_each_entry(ck, &clocks, node) {
414 if (ck->usecount > 0 || (ck->flags & ALWAYS_ENABLED) ||
418 spin_lock_irqsave(&clockfw_lock, flags);
419 if (arch_clock->clk_disable_unused)
420 arch_clock->clk_disable_unused(ck);
421 spin_unlock_irqrestore(&clockfw_lock, flags);
426 late_initcall(clk_disable_unused);
429 int __init clk_init(struct clk_functions * custom_clocks)
431 if (!custom_clocks) {
432 printk(KERN_ERR "No custom clock functions registered\n");
436 arch_clock = custom_clocks;