blob: 197974defbe4f4b0e41ffebf2aae1f2c96dc6c00 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tony Lindgrenb9158552005-07-10 19:58:14 +01002 * linux/arch/arm/plat-omap/clock.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Hiroshi DOYU137b3ee2008-07-03 12:24:41 +03004 * Copyright (C) 2004 - 2008 Nokia corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
6 *
Tony Lindgren1a8bfa12005-11-10 14:26:50 +00007 * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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.
12 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000014#include <linux/init.h>
15#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/list.h>
17#include <linux/errno.h>
18#include <linux/err.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080019#include <linux/string.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000020#include <linux/clk.h>
Arjan van de Ven00431702006-01-12 18:42:23 +000021#include <linux/mutex.h>
Tony Lindgrenb824efa2006-04-02 17:46:20 +010022#include <linux/platform_device.h>
Russell Kingb851cb22008-05-22 16:38:50 +010023#include <linux/cpufreq.h>
Hiroshi DOYU137b3ee2008-07-03 12:24:41 +030024#include <linux/debugfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +010026#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Russell Kinga09e64f2008-08-05 16:14:15 +010028#include <mach/clock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Juha Yrjola7df34502006-06-26 16:16:22 -070030static LIST_HEAD(clocks);
Arjan van de Ven00431702006-01-12 18:42:23 +000031static DEFINE_MUTEX(clocks_mutex);
Juha Yrjola7df34502006-06-26 16:16:22 -070032static DEFINE_SPINLOCK(clockfw_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000034static struct clk_functions *arch_clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000036/*-------------------------------------------------------------------------
Tony Lindgrenf07adc52006-01-17 15:27:09 -080037 * Standard clock functions defined in include/linux/clk.h
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000038 *-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Tony Lindgrenb824efa2006-04-02 17:46:20 +010040/*
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.
43 */
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000044struct clk * clk_get(struct device *dev, const char *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 struct clk *p, *clk = ERR_PTR(-ENOENT);
Tony Lindgrenb824efa2006-04-02 17:46:20 +010047 int idno;
48
49 if (dev == NULL || dev->bus != &platform_bus_type)
50 idno = -1;
51 else
52 idno = to_platform_device(dev)->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Arjan van de Ven00431702006-01-12 18:42:23 +000054 mutex_lock(&clocks_mutex);
Tony Lindgrenb824efa2006-04-02 17:46:20 +010055
56 list_for_each_entry(p, &clocks, node) {
57 if (p->id == idno &&
58 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
59 clk = p;
Tony Lindgren67d4d832006-04-09 22:21:05 +010060 goto found;
Tony Lindgrenb824efa2006-04-02 17:46:20 +010061 }
62 }
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 list_for_each_entry(p, &clocks, node) {
65 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
66 clk = p;
67 break;
68 }
69 }
Tony Lindgrenb824efa2006-04-02 17:46:20 +010070
Tony Lindgren67d4d832006-04-09 22:21:05 +010071found:
Arjan van de Ven00431702006-01-12 18:42:23 +000072 mutex_unlock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 return clk;
75}
76EXPORT_SYMBOL(clk_get);
77
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000078int clk_enable(struct clk *clk)
79{
80 unsigned long flags;
81 int ret = 0;
82
Tony Lindgrenb824efa2006-04-02 17:46:20 +010083 if (clk == NULL || IS_ERR(clk))
84 return -EINVAL;
85
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000086 spin_lock_irqsave(&clockfw_lock, flags);
Tony Lindgrenf07adc52006-01-17 15:27:09 -080087 if (arch_clock->clk_enable)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000088 ret = arch_clock->clk_enable(clk);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +000089 spin_unlock_irqrestore(&clockfw_lock, flags);
90
91 return ret;
92}
93EXPORT_SYMBOL(clk_enable);
94
95void clk_disable(struct clk *clk)
96{
97 unsigned long flags;
98
Tony Lindgrenb824efa2006-04-02 17:46:20 +010099 if (clk == NULL || IS_ERR(clk))
100 return;
101
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000102 spin_lock_irqsave(&clockfw_lock, flags);
Tony Lindgren7cf95772007-08-07 05:20:00 -0700103 if (clk->usecount == 0) {
104 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
105 clk->name);
106 WARN_ON(1);
107 goto out;
108 }
109
Tony Lindgrenf07adc52006-01-17 15:27:09 -0800110 if (arch_clock->clk_disable)
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000111 arch_clock->clk_disable(clk);
Tony Lindgren7cf95772007-08-07 05:20:00 -0700112
113out:
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000114 spin_unlock_irqrestore(&clockfw_lock, flags);
115}
116EXPORT_SYMBOL(clk_disable);
117
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000118int clk_get_usecount(struct clk *clk)
119{
120 unsigned long flags;
121 int ret = 0;
122
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100123 if (clk == NULL || IS_ERR(clk))
124 return 0;
125
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000126 spin_lock_irqsave(&clockfw_lock, flags);
127 ret = clk->usecount;
128 spin_unlock_irqrestore(&clockfw_lock, flags);
129
130 return ret;
131}
132EXPORT_SYMBOL(clk_get_usecount);
133
134unsigned long clk_get_rate(struct clk *clk)
135{
136 unsigned long flags;
137 unsigned long ret = 0;
138
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100139 if (clk == NULL || IS_ERR(clk))
140 return 0;
141
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000142 spin_lock_irqsave(&clockfw_lock, flags);
143 ret = clk->rate;
144 spin_unlock_irqrestore(&clockfw_lock, flags);
145
146 return ret;
147}
148EXPORT_SYMBOL(clk_get_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150void clk_put(struct clk *clk)
151{
152 if (clk && !IS_ERR(clk))
153 module_put(clk->owner);
154}
155EXPORT_SYMBOL(clk_put);
156
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000157/*-------------------------------------------------------------------------
Tony Lindgrenf07adc52006-01-17 15:27:09 -0800158 * Optional clock functions defined in include/linux/clk.h
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000159 *-------------------------------------------------------------------------*/
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161long clk_round_rate(struct clk *clk, unsigned long rate)
162{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000163 unsigned long flags;
164 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100166 if (clk == NULL || IS_ERR(clk))
167 return ret;
168
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000169 spin_lock_irqsave(&clockfw_lock, flags);
170 if (arch_clock->clk_round_rate)
171 ret = arch_clock->clk_round_rate(clk, rate);
172 spin_unlock_irqrestore(&clockfw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000174 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176EXPORT_SYMBOL(clk_round_rate);
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178int clk_set_rate(struct clk *clk, unsigned long rate)
179{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000180 unsigned long flags;
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100181 int ret = -EINVAL;
182
183 if (clk == NULL || IS_ERR(clk))
184 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000186 spin_lock_irqsave(&clockfw_lock, flags);
187 if (arch_clock->clk_set_rate)
188 ret = arch_clock->clk_set_rate(clk, rate);
189 spin_unlock_irqrestore(&clockfw_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 return ret;
192}
193EXPORT_SYMBOL(clk_set_rate);
194
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000195int clk_set_parent(struct clk *clk, struct clk *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000197 unsigned long flags;
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100198 int ret = -EINVAL;
199
200 if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
201 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000203 spin_lock_irqsave(&clockfw_lock, flags);
204 if (arch_clock->clk_set_parent)
205 ret = arch_clock->clk_set_parent(clk, parent);
206 spin_unlock_irqrestore(&clockfw_lock, flags);
207
208 return ret;
209}
210EXPORT_SYMBOL(clk_set_parent);
211
212struct clk *clk_get_parent(struct clk *clk)
213{
214 unsigned long flags;
215 struct clk * ret = NULL;
216
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100217 if (clk == NULL || IS_ERR(clk))
218 return ret;
219
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000220 spin_lock_irqsave(&clockfw_lock, flags);
221 if (arch_clock->clk_get_parent)
222 ret = arch_clock->clk_get_parent(clk);
223 spin_unlock_irqrestore(&clockfw_lock, flags);
224
225 return ret;
226}
227EXPORT_SYMBOL(clk_get_parent);
228
229/*-------------------------------------------------------------------------
230 * OMAP specific clock functions shared between omap1 and omap2
231 *-------------------------------------------------------------------------*/
232
233unsigned int __initdata mpurate;
234
235/*
236 * By default we use the rate set by the bootloader.
237 * You can override this with mpurate= cmdline option.
238 */
239static int __init omap_clk_setup(char *str)
240{
241 get_option(&str, &mpurate);
242
243 if (!mpurate)
244 return 1;
245
246 if (mpurate < 1000)
247 mpurate *= 1000000;
248
249 return 1;
250}
251__setup("mpurate=", omap_clk_setup);
252
253/* Used for clocks that always have same value as the parent clock */
254void followparent_recalc(struct clk *clk)
255{
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100256 if (clk == NULL || IS_ERR(clk))
257 return;
258
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000259 clk->rate = clk->parent->rate;
Imre Deakb1465bf2007-03-06 03:52:01 -0800260 if (unlikely(clk->flags & RATE_PROPAGATES))
261 propagate_rate(clk);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000262}
263
264/* Propagate rate to children */
265void propagate_rate(struct clk * tclk)
266{
267 struct clk *clkp;
268
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100269 if (tclk == NULL || IS_ERR(tclk))
270 return;
271
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000272 list_for_each_entry(clkp, &clocks, node) {
273 if (likely(clkp->parent != tclk))
274 continue;
275 if (likely((u32)clkp->recalc))
276 clkp->recalc(clkp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Paul Walmsley6b8858a2008-03-18 10:35:15 +0200280/**
281 * recalculate_root_clocks - recalculate and propagate all root clocks
282 *
283 * Recalculates all root clocks (clocks with no parent), which if the
284 * clock's .recalc is set correctly, should also propagate their rates.
285 * Called at init.
286 */
287void recalculate_root_clocks(void)
288{
289 struct clk *clkp;
290
291 list_for_each_entry(clkp, &clocks, node) {
292 if (unlikely(!clkp->parent) && likely((u32)clkp->recalc))
293 clkp->recalc(clkp);
294 }
295}
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297int clk_register(struct clk *clk)
298{
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100299 if (clk == NULL || IS_ERR(clk))
300 return -EINVAL;
301
Arjan van de Ven00431702006-01-12 18:42:23 +0000302 mutex_lock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 list_add(&clk->node, &clocks);
304 if (clk->init)
305 clk->init(clk);
Arjan van de Ven00431702006-01-12 18:42:23 +0000306 mutex_unlock(&clocks_mutex);
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return 0;
309}
310EXPORT_SYMBOL(clk_register);
311
312void clk_unregister(struct clk *clk)
313{
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100314 if (clk == NULL || IS_ERR(clk))
315 return;
316
Arjan van de Ven00431702006-01-12 18:42:23 +0000317 mutex_lock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 list_del(&clk->node);
Arjan van de Ven00431702006-01-12 18:42:23 +0000319 mutex_unlock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321EXPORT_SYMBOL(clk_unregister);
322
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000323void clk_deny_idle(struct clk *clk)
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100324{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000325 unsigned long flags;
326
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100327 if (clk == NULL || IS_ERR(clk))
328 return;
329
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000330 spin_lock_irqsave(&clockfw_lock, flags);
331 if (arch_clock->clk_deny_idle)
332 arch_clock->clk_deny_idle(clk);
333 spin_unlock_irqrestore(&clockfw_lock, flags);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100334}
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000335EXPORT_SYMBOL(clk_deny_idle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000337void clk_allow_idle(struct clk *clk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000339 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Tony Lindgrenb824efa2006-04-02 17:46:20 +0100341 if (clk == NULL || IS_ERR(clk))
342 return;
343
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000344 spin_lock_irqsave(&clockfw_lock, flags);
345 if (arch_clock->clk_allow_idle)
346 arch_clock->clk_allow_idle(clk);
347 spin_unlock_irqrestore(&clockfw_lock, flags);
348}
349EXPORT_SYMBOL(clk_allow_idle);
Tony Lindgrenbb13b5f2005-07-10 19:58:18 +0100350
Paul Walmsley6b8858a2008-03-18 10:35:15 +0200351void clk_enable_init_clocks(void)
352{
353 struct clk *clkp;
354
355 list_for_each_entry(clkp, &clocks, node) {
356 if (clkp->flags & ENABLE_ON_INIT)
357 clk_enable(clkp);
358 }
359}
360EXPORT_SYMBOL(clk_enable_init_clocks);
361
362#ifdef CONFIG_CPU_FREQ
363void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
364{
365 unsigned long flags;
366
367 spin_lock_irqsave(&clockfw_lock, flags);
368 if (arch_clock->clk_init_cpufreq_table)
369 arch_clock->clk_init_cpufreq_table(table);
370 spin_unlock_irqrestore(&clockfw_lock, flags);
371}
372EXPORT_SYMBOL(clk_init_cpufreq_table);
373#endif
374
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000375/*-------------------------------------------------------------------------*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Tony Lindgren90afd5c2006-09-25 13:27:20 +0300377#ifdef CONFIG_OMAP_RESET_CLOCKS
378/*
379 * Disable any unused clocks left on by the bootloader
380 */
381static int __init clk_disable_unused(void)
382{
383 struct clk *ck;
384 unsigned long flags;
385
386 list_for_each_entry(ck, &clocks, node) {
387 if (ck->usecount > 0 || (ck->flags & ALWAYS_ENABLED) ||
388 ck->enable_reg == 0)
389 continue;
390
391 spin_lock_irqsave(&clockfw_lock, flags);
392 if (arch_clock->clk_disable_unused)
393 arch_clock->clk_disable_unused(ck);
394 spin_unlock_irqrestore(&clockfw_lock, flags);
395 }
396
397 return 0;
398}
399late_initcall(clk_disable_unused);
400#endif
401
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000402int __init clk_init(struct clk_functions * custom_clocks)
403{
404 if (!custom_clocks) {
405 printk(KERN_ERR "No custom clock functions registered\n");
406 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408
Tony Lindgren1a8bfa12005-11-10 14:26:50 +0000409 arch_clock = custom_clocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 return 0;
412}
Paul Walmsley6b8858a2008-03-18 10:35:15 +0200413
Hiroshi DOYU137b3ee2008-07-03 12:24:41 +0300414#if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
415/*
416 * debugfs support to trace clock tree hierarchy and attributes
417 */
418static struct dentry *clk_debugfs_root;
419
420static int clk_debugfs_register_one(struct clk *c)
421{
422 int err;
423 struct dentry *d, *child;
424 struct clk *pa = c->parent;
425 char s[255];
426 char *p = s;
427
428 p += sprintf(p, "%s", c->name);
429 if (c->id != 0)
430 sprintf(p, ":%d", c->id);
431 d = debugfs_create_dir(s, pa ? pa->dent : clk_debugfs_root);
432 if (IS_ERR(d))
433 return PTR_ERR(d);
434 c->dent = d;
435
436 d = debugfs_create_u8("usecount", S_IRUGO, c->dent, (u8 *)&c->usecount);
437 if (IS_ERR(d)) {
438 err = PTR_ERR(d);
439 goto err_out;
440 }
441 d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
442 if (IS_ERR(d)) {
443 err = PTR_ERR(d);
444 goto err_out;
445 }
446 d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
447 if (IS_ERR(d)) {
448 err = PTR_ERR(d);
449 goto err_out;
450 }
451 return 0;
452
453err_out:
454 d = c->dent;
455 list_for_each_entry(child, &d->d_subdirs, d_u.d_child)
456 debugfs_remove(child);
457 debugfs_remove(c->dent);
458 return err;
459}
460
461static int clk_debugfs_register(struct clk *c)
462{
463 int err;
464 struct clk *pa = c->parent;
465
466 if (pa && !pa->dent) {
467 err = clk_debugfs_register(pa);
468 if (err)
469 return err;
470 }
471
472 if (!c->dent) {
473 err = clk_debugfs_register_one(c);
474 if (err)
475 return err;
476 }
477 return 0;
478}
479
480static int __init clk_debugfs_init(void)
481{
482 struct clk *c;
483 struct dentry *d;
484 int err;
485
486 d = debugfs_create_dir("clock", NULL);
487 if (IS_ERR(d))
488 return PTR_ERR(d);
489 clk_debugfs_root = d;
490
491 list_for_each_entry(c, &clocks, node) {
492 err = clk_debugfs_register(c);
493 if (err)
494 goto err_out;
495 }
496 return 0;
497err_out:
498 debugfs_remove(clk_debugfs_root); /* REVISIT: Cleanup correctly */
499 return err;
500}
501late_initcall(clk_debugfs_init);
502
503#endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */