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>
24 #include <linux/platform_device.h>
27 #include <asm/semaphore.h>
29 #include <asm/arch/clock.h>
32 static DEFINE_MUTEX(clocks_mutex);
33 DEFINE_SPINLOCK(clockfw_lock);
35 static struct clk_functions *arch_clock;
37 /*-------------------------------------------------------------------------
38 * Standard clock functions defined in include/linux/clk.h
39 *-------------------------------------------------------------------------*/
42 * Returns a clock. Note that we first try to use device id on the bus
43 * and clock name. If this fails, we try to use clock name only.
45 struct clk * clk_get(struct device *dev, const char *id)
47 struct clk *p, *clk = ERR_PTR(-ENOENT);
50 if (dev == NULL || dev->bus != &platform_bus_type)
53 idno = to_platform_device(dev)->id;
55 mutex_lock(&clocks_mutex);
57 list_for_each_entry(p, &clocks, node) {
59 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
65 list_for_each_entry(p, &clocks, node) {
66 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 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;
307 #ifdef CONFIG_PROC_FS
308 #include <linux/proc_fs.h>
309 #include <linux/seq_file.h>
311 static void *omap_ck_start(struct seq_file *m, loff_t *pos)
313 return *pos < 1 ? (void *)1 : NULL;
316 static void *omap_ck_next(struct seq_file *m, void *v, loff_t *pos)
322 static void omap_ck_stop(struct seq_file *m, void *v)
326 int omap_ck_show(struct seq_file *m, void *v)
330 list_for_each_entry(cp, &clocks, node)
331 seq_printf(m,"%s %ld %d\n", cp->name, cp->rate, cp->usecount);
336 static struct seq_operations omap_ck_op = {
337 .start = omap_ck_start,
338 .next = omap_ck_next,
339 .stop = omap_ck_stop,
343 static int omap_ck_open(struct inode *inode, struct file *file)
345 return seq_open(file, &omap_ck_op);
348 static struct file_operations proc_omap_ck_operations = {
349 .open = omap_ck_open,
352 .release = seq_release,
355 int __init omap_ck_init(void)
357 struct proc_dir_entry *entry;
359 entry = create_proc_entry("omap_clocks", 0, NULL);
361 entry->proc_fops = &proc_omap_ck_operations;
365 __initcall(omap_ck_init);
366 #endif /* CONFIG_DEBUG_PROC_FS */