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/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>
26 #include <asm/semaphore.h>
28 #include <asm/arch/clock.h>
31 static DEFINE_MUTEX(clocks_mutex);
32 DEFINE_SPINLOCK(clockfw_lock);
34 static struct clk_functions *arch_clock;
36 /*-------------------------------------------------------------------------
37 * Standard clock functions defined in asm/hardware/clock.h
38 *-------------------------------------------------------------------------*/
40 struct clk * clk_get(struct device *dev, const char *id)
42 struct clk *p, *clk = ERR_PTR(-ENOENT);
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)) {
51 mutex_unlock(&clocks_mutex);
55 EXPORT_SYMBOL(clk_get);
57 int clk_enable(struct clk *clk)
62 spin_lock_irqsave(&clockfw_lock, flags);
64 ret = clk->enable(clk);
65 else if (arch_clock->clk_enable)
66 ret = arch_clock->clk_enable(clk);
68 printk(KERN_ERR "Could not enable clock %s\n", clk->name);
69 spin_unlock_irqrestore(&clockfw_lock, flags);
73 EXPORT_SYMBOL(clk_enable);
75 void clk_disable(struct clk *clk)
79 spin_lock_irqsave(&clockfw_lock, flags);
82 else if (arch_clock->clk_disable)
83 arch_clock->clk_disable(clk);
85 printk(KERN_ERR "Could not disable clock %s\n", clk->name);
86 spin_unlock_irqrestore(&clockfw_lock, flags);
88 EXPORT_SYMBOL(clk_disable);
90 int clk_use(struct clk *clk)
95 spin_lock_irqsave(&clockfw_lock, flags);
96 if (arch_clock->clk_use)
97 ret = arch_clock->clk_use(clk);
98 spin_unlock_irqrestore(&clockfw_lock, flags);
102 EXPORT_SYMBOL(clk_use);
104 void clk_unuse(struct clk *clk)
108 spin_lock_irqsave(&clockfw_lock, flags);
109 if (arch_clock->clk_unuse)
110 arch_clock->clk_unuse(clk);
111 spin_unlock_irqrestore(&clockfw_lock, flags);
113 EXPORT_SYMBOL(clk_unuse);
115 int clk_get_usecount(struct clk *clk)
120 spin_lock_irqsave(&clockfw_lock, flags);
122 spin_unlock_irqrestore(&clockfw_lock, flags);
126 EXPORT_SYMBOL(clk_get_usecount);
128 unsigned long clk_get_rate(struct clk *clk)
131 unsigned long ret = 0;
133 spin_lock_irqsave(&clockfw_lock, flags);
135 spin_unlock_irqrestore(&clockfw_lock, flags);
139 EXPORT_SYMBOL(clk_get_rate);
141 void clk_put(struct clk *clk)
143 if (clk && !IS_ERR(clk))
144 module_put(clk->owner);
146 EXPORT_SYMBOL(clk_put);
148 /*-------------------------------------------------------------------------
149 * Optional clock functions defined in asm/hardware/clock.h
150 *-------------------------------------------------------------------------*/
152 long clk_round_rate(struct clk *clk, unsigned long rate)
157 spin_lock_irqsave(&clockfw_lock, flags);
158 if (arch_clock->clk_round_rate)
159 ret = arch_clock->clk_round_rate(clk, rate);
160 spin_unlock_irqrestore(&clockfw_lock, flags);
164 EXPORT_SYMBOL(clk_round_rate);
166 int clk_set_rate(struct clk *clk, unsigned long rate)
171 spin_lock_irqsave(&clockfw_lock, flags);
172 if (arch_clock->clk_set_rate)
173 ret = arch_clock->clk_set_rate(clk, rate);
174 spin_unlock_irqrestore(&clockfw_lock, flags);
178 EXPORT_SYMBOL(clk_set_rate);
180 int clk_set_parent(struct clk *clk, struct clk *parent)
185 spin_lock_irqsave(&clockfw_lock, flags);
186 if (arch_clock->clk_set_parent)
187 ret = arch_clock->clk_set_parent(clk, parent);
188 spin_unlock_irqrestore(&clockfw_lock, flags);
192 EXPORT_SYMBOL(clk_set_parent);
194 struct clk *clk_get_parent(struct clk *clk)
197 struct clk * ret = NULL;
199 spin_lock_irqsave(&clockfw_lock, flags);
200 if (arch_clock->clk_get_parent)
201 ret = arch_clock->clk_get_parent(clk);
202 spin_unlock_irqrestore(&clockfw_lock, flags);
206 EXPORT_SYMBOL(clk_get_parent);
208 /*-------------------------------------------------------------------------
209 * OMAP specific clock functions shared between omap1 and omap2
210 *-------------------------------------------------------------------------*/
212 unsigned int __initdata mpurate;
215 * By default we use the rate set by the bootloader.
216 * You can override this with mpurate= cmdline option.
218 static int __init omap_clk_setup(char *str)
220 get_option(&str, &mpurate);
230 __setup("mpurate=", omap_clk_setup);
232 /* Used for clocks that always have same value as the parent clock */
233 void followparent_recalc(struct clk *clk)
235 clk->rate = clk->parent->rate;
238 /* Propagate rate to children */
239 void propagate_rate(struct clk * tclk)
243 list_for_each_entry(clkp, &clocks, node) {
244 if (likely(clkp->parent != tclk))
246 if (likely((u32)clkp->recalc))
251 int clk_register(struct clk *clk)
253 mutex_lock(&clocks_mutex);
254 list_add(&clk->node, &clocks);
257 mutex_unlock(&clocks_mutex);
261 EXPORT_SYMBOL(clk_register);
263 void clk_unregister(struct clk *clk)
265 mutex_lock(&clocks_mutex);
266 list_del(&clk->node);
267 mutex_unlock(&clocks_mutex);
269 EXPORT_SYMBOL(clk_unregister);
271 void clk_deny_idle(struct clk *clk)
275 spin_lock_irqsave(&clockfw_lock, flags);
276 if (arch_clock->clk_deny_idle)
277 arch_clock->clk_deny_idle(clk);
278 spin_unlock_irqrestore(&clockfw_lock, flags);
280 EXPORT_SYMBOL(clk_deny_idle);
282 void clk_allow_idle(struct clk *clk)
286 spin_lock_irqsave(&clockfw_lock, flags);
287 if (arch_clock->clk_allow_idle)
288 arch_clock->clk_allow_idle(clk);
289 spin_unlock_irqrestore(&clockfw_lock, flags);
291 EXPORT_SYMBOL(clk_allow_idle);
293 /*-------------------------------------------------------------------------*/
295 int __init clk_init(struct clk_functions * custom_clocks)
297 if (!custom_clocks) {
298 printk(KERN_ERR "No custom clock functions registered\n");
302 arch_clock = custom_clocks;