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 /*-------------------------------------------------------------------------
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);
49 if (dev == NULL || dev->bus != &platform_bus_type)
52 idno = to_platform_device(dev)->id;
54 mutex_lock(&clocks_mutex);
56 list_for_each_entry(p, &clocks, node) {
58 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
64 list_for_each_entry(p, &clocks, node) {
65 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
72 mutex_unlock(&clocks_mutex);
76 EXPORT_SYMBOL(clk_get);
78 int clk_enable(struct clk *clk)
83 if (clk == NULL || IS_ERR(clk))
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);
93 EXPORT_SYMBOL(clk_enable);
95 void clk_disable(struct clk *clk)
99 if (clk == NULL || IS_ERR(clk))
102 spin_lock_irqsave(&clockfw_lock, flags);
103 if (arch_clock->clk_disable)
104 arch_clock->clk_disable(clk);
105 spin_unlock_irqrestore(&clockfw_lock, flags);
107 EXPORT_SYMBOL(clk_disable);
109 int clk_get_usecount(struct clk *clk)
114 if (clk == NULL || IS_ERR(clk))
117 spin_lock_irqsave(&clockfw_lock, flags);
119 spin_unlock_irqrestore(&clockfw_lock, flags);
123 EXPORT_SYMBOL(clk_get_usecount);
125 unsigned long clk_get_rate(struct clk *clk)
128 unsigned long ret = 0;
130 if (clk == NULL || IS_ERR(clk))
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 include/linux/clk.h
150 *-------------------------------------------------------------------------*/
152 long clk_round_rate(struct clk *clk, unsigned long rate)
157 if (clk == NULL || IS_ERR(clk))
160 spin_lock_irqsave(&clockfw_lock, flags);
161 if (arch_clock->clk_round_rate)
162 ret = arch_clock->clk_round_rate(clk, rate);
163 spin_unlock_irqrestore(&clockfw_lock, flags);
167 EXPORT_SYMBOL(clk_round_rate);
169 int clk_set_rate(struct clk *clk, unsigned long rate)
174 if (clk == NULL || IS_ERR(clk))
177 spin_lock_irqsave(&clockfw_lock, flags);
178 if (arch_clock->clk_set_rate)
179 ret = arch_clock->clk_set_rate(clk, rate);
180 spin_unlock_irqrestore(&clockfw_lock, flags);
184 EXPORT_SYMBOL(clk_set_rate);
186 int clk_set_parent(struct clk *clk, struct clk *parent)
191 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
194 spin_lock_irqsave(&clockfw_lock, flags);
195 if (arch_clock->clk_set_parent)
196 ret = arch_clock->clk_set_parent(clk, parent);
197 spin_unlock_irqrestore(&clockfw_lock, flags);
201 EXPORT_SYMBOL(clk_set_parent);
203 struct clk *clk_get_parent(struct clk *clk)
206 struct clk * ret = NULL;
208 if (clk == NULL || IS_ERR(clk))
211 spin_lock_irqsave(&clockfw_lock, flags);
212 if (arch_clock->clk_get_parent)
213 ret = arch_clock->clk_get_parent(clk);
214 spin_unlock_irqrestore(&clockfw_lock, flags);
218 EXPORT_SYMBOL(clk_get_parent);
220 /*-------------------------------------------------------------------------
221 * OMAP specific clock functions shared between omap1 and omap2
222 *-------------------------------------------------------------------------*/
224 unsigned int __initdata mpurate;
227 * By default we use the rate set by the bootloader.
228 * You can override this with mpurate= cmdline option.
230 static int __init omap_clk_setup(char *str)
232 get_option(&str, &mpurate);
242 __setup("mpurate=", omap_clk_setup);
244 /* Used for clocks that always have same value as the parent clock */
245 void followparent_recalc(struct clk *clk)
247 if (clk == NULL || IS_ERR(clk))
250 clk->rate = clk->parent->rate;
253 /* Propagate rate to children */
254 void propagate_rate(struct clk * tclk)
258 if (tclk == NULL || IS_ERR(tclk))
261 list_for_each_entry(clkp, &clocks, node) {
262 if (likely(clkp->parent != tclk))
264 if (likely((u32)clkp->recalc))
269 int clk_register(struct clk *clk)
271 if (clk == NULL || IS_ERR(clk))
274 mutex_lock(&clocks_mutex);
275 list_add(&clk->node, &clocks);
278 mutex_unlock(&clocks_mutex);
282 EXPORT_SYMBOL(clk_register);
284 void clk_unregister(struct clk *clk)
286 if (clk == NULL || IS_ERR(clk))
289 mutex_lock(&clocks_mutex);
290 list_del(&clk->node);
291 mutex_unlock(&clocks_mutex);
293 EXPORT_SYMBOL(clk_unregister);
295 void clk_deny_idle(struct clk *clk)
299 if (clk == NULL || IS_ERR(clk))
302 spin_lock_irqsave(&clockfw_lock, flags);
303 if (arch_clock->clk_deny_idle)
304 arch_clock->clk_deny_idle(clk);
305 spin_unlock_irqrestore(&clockfw_lock, flags);
307 EXPORT_SYMBOL(clk_deny_idle);
309 void clk_allow_idle(struct clk *clk)
313 if (clk == NULL || IS_ERR(clk))
316 spin_lock_irqsave(&clockfw_lock, flags);
317 if (arch_clock->clk_allow_idle)
318 arch_clock->clk_allow_idle(clk);
319 spin_unlock_irqrestore(&clockfw_lock, flags);
321 EXPORT_SYMBOL(clk_allow_idle);
323 /*-------------------------------------------------------------------------*/
325 int __init clk_init(struct clk_functions * custom_clocks)
327 if (!custom_clocks) {
328 printk(KERN_ERR "No custom clock functions registered\n");
332 arch_clock = custom_clocks;