FRV: Use generic show_interrupts()
[cris-mirror.git] / drivers / sh / clk / core.c
blob4f64183b27fa6283c80d4fa2d37aa61c61c127fe
1 /*
2 * SuperH clock framework
4 * Copyright (C) 2005 - 2010 Paul Mundt
6 * This clock framework is derived from the OMAP version by:
8 * Copyright (C) 2004 - 2008 Nokia Corporation
9 * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
11 * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
13 * This file is subject to the terms and conditions of the GNU General Public
14 * License. See the file "COPYING" in the main directory of this archive
15 * for more details.
17 #define pr_fmt(fmt) "clock: " fmt
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/list.h>
24 #include <linux/syscore_ops.h>
25 #include <linux/seq_file.h>
26 #include <linux/err.h>
27 #include <linux/io.h>
28 #include <linux/debugfs.h>
29 #include <linux/cpufreq.h>
30 #include <linux/clk.h>
31 #include <linux/sh_clk.h>
33 static LIST_HEAD(clock_list);
34 static DEFINE_SPINLOCK(clock_lock);
35 static DEFINE_MUTEX(clock_list_sem);
37 void clk_rate_table_build(struct clk *clk,
38 struct cpufreq_frequency_table *freq_table,
39 int nr_freqs,
40 struct clk_div_mult_table *src_table,
41 unsigned long *bitmap)
43 unsigned long mult, div;
44 unsigned long freq;
45 int i;
47 clk->nr_freqs = nr_freqs;
49 for (i = 0; i < nr_freqs; i++) {
50 div = 1;
51 mult = 1;
53 if (src_table->divisors && i < src_table->nr_divisors)
54 div = src_table->divisors[i];
56 if (src_table->multipliers && i < src_table->nr_multipliers)
57 mult = src_table->multipliers[i];
59 if (!div || !mult || (bitmap && !test_bit(i, bitmap)))
60 freq = CPUFREQ_ENTRY_INVALID;
61 else
62 freq = clk->parent->rate * mult / div;
64 freq_table[i].index = i;
65 freq_table[i].frequency = freq;
68 /* Termination entry */
69 freq_table[i].index = i;
70 freq_table[i].frequency = CPUFREQ_TABLE_END;
73 struct clk_rate_round_data;
75 struct clk_rate_round_data {
76 unsigned long rate;
77 unsigned int min, max;
78 long (*func)(unsigned int, struct clk_rate_round_data *);
79 void *arg;
82 #define for_each_frequency(pos, r, freq) \
83 for (pos = r->min, freq = r->func(pos, r); \
84 pos <= r->max; pos++, freq = r->func(pos, r)) \
85 if (unlikely(freq == 0)) \
86 ; \
87 else
89 static long clk_rate_round_helper(struct clk_rate_round_data *rounder)
91 unsigned long rate_error, rate_error_prev = ~0UL;
92 unsigned long highest, lowest, freq;
93 long rate_best_fit = -ENOENT;
94 int i;
96 highest = 0;
97 lowest = ~0UL;
99 for_each_frequency(i, rounder, freq) {
100 if (freq > highest)
101 highest = freq;
102 if (freq < lowest)
103 lowest = freq;
105 rate_error = abs(freq - rounder->rate);
106 if (rate_error < rate_error_prev) {
107 rate_best_fit = freq;
108 rate_error_prev = rate_error;
111 if (rate_error == 0)
112 break;
115 if (rounder->rate >= highest)
116 rate_best_fit = highest;
117 if (rounder->rate <= lowest)
118 rate_best_fit = lowest;
120 return rate_best_fit;
123 static long clk_rate_table_iter(unsigned int pos,
124 struct clk_rate_round_data *rounder)
126 struct cpufreq_frequency_table *freq_table = rounder->arg;
127 unsigned long freq = freq_table[pos].frequency;
129 if (freq == CPUFREQ_ENTRY_INVALID)
130 freq = 0;
132 return freq;
135 long clk_rate_table_round(struct clk *clk,
136 struct cpufreq_frequency_table *freq_table,
137 unsigned long rate)
139 struct clk_rate_round_data table_round = {
140 .min = 0,
141 .max = clk->nr_freqs - 1,
142 .func = clk_rate_table_iter,
143 .arg = freq_table,
144 .rate = rate,
147 if (clk->nr_freqs < 1)
148 return -ENOSYS;
150 return clk_rate_round_helper(&table_round);
153 static long clk_rate_div_range_iter(unsigned int pos,
154 struct clk_rate_round_data *rounder)
156 return clk_get_rate(rounder->arg) / pos;
159 long clk_rate_div_range_round(struct clk *clk, unsigned int div_min,
160 unsigned int div_max, unsigned long rate)
162 struct clk_rate_round_data div_range_round = {
163 .min = div_min,
164 .max = div_max,
165 .func = clk_rate_div_range_iter,
166 .arg = clk_get_parent(clk),
167 .rate = rate,
170 return clk_rate_round_helper(&div_range_round);
173 int clk_rate_table_find(struct clk *clk,
174 struct cpufreq_frequency_table *freq_table,
175 unsigned long rate)
177 int i;
179 for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
180 unsigned long freq = freq_table[i].frequency;
182 if (freq == CPUFREQ_ENTRY_INVALID)
183 continue;
185 if (freq == rate)
186 return i;
189 return -ENOENT;
192 /* Used for clocks that always have same value as the parent clock */
193 unsigned long followparent_recalc(struct clk *clk)
195 return clk->parent ? clk->parent->rate : 0;
198 int clk_reparent(struct clk *child, struct clk *parent)
200 list_del_init(&child->sibling);
201 if (parent)
202 list_add(&child->sibling, &parent->children);
203 child->parent = parent;
205 /* now do the debugfs renaming to reattach the child
206 to the proper parent */
208 return 0;
211 /* Propagate rate to children */
212 void propagate_rate(struct clk *tclk)
214 struct clk *clkp;
216 list_for_each_entry(clkp, &tclk->children, sibling) {
217 if (clkp->ops && clkp->ops->recalc)
218 clkp->rate = clkp->ops->recalc(clkp);
220 propagate_rate(clkp);
224 static void __clk_disable(struct clk *clk)
226 if (WARN(!clk->usecount, "Trying to disable clock %p with 0 usecount\n",
227 clk))
228 return;
230 if (!(--clk->usecount)) {
231 if (likely(clk->ops && clk->ops->disable))
232 clk->ops->disable(clk);
233 if (likely(clk->parent))
234 __clk_disable(clk->parent);
238 void clk_disable(struct clk *clk)
240 unsigned long flags;
242 if (!clk)
243 return;
245 spin_lock_irqsave(&clock_lock, flags);
246 __clk_disable(clk);
247 spin_unlock_irqrestore(&clock_lock, flags);
249 EXPORT_SYMBOL_GPL(clk_disable);
251 static int __clk_enable(struct clk *clk)
253 int ret = 0;
255 if (clk->usecount++ == 0) {
256 if (clk->parent) {
257 ret = __clk_enable(clk->parent);
258 if (unlikely(ret))
259 goto err;
262 if (clk->ops && clk->ops->enable) {
263 ret = clk->ops->enable(clk);
264 if (ret) {
265 if (clk->parent)
266 __clk_disable(clk->parent);
267 goto err;
272 return ret;
273 err:
274 clk->usecount--;
275 return ret;
278 int clk_enable(struct clk *clk)
280 unsigned long flags;
281 int ret;
283 if (!clk)
284 return -EINVAL;
286 spin_lock_irqsave(&clock_lock, flags);
287 ret = __clk_enable(clk);
288 spin_unlock_irqrestore(&clock_lock, flags);
290 return ret;
292 EXPORT_SYMBOL_GPL(clk_enable);
294 static LIST_HEAD(root_clks);
297 * recalculate_root_clocks - recalculate and propagate all root clocks
299 * Recalculates all root clocks (clocks with no parent), which if the
300 * clock's .recalc is set correctly, should also propagate their rates.
301 * Called at init.
303 void recalculate_root_clocks(void)
305 struct clk *clkp;
307 list_for_each_entry(clkp, &root_clks, sibling) {
308 if (clkp->ops && clkp->ops->recalc)
309 clkp->rate = clkp->ops->recalc(clkp);
310 propagate_rate(clkp);
314 static struct clk_mapping dummy_mapping;
316 static struct clk *lookup_root_clock(struct clk *clk)
318 while (clk->parent)
319 clk = clk->parent;
321 return clk;
324 static int clk_establish_mapping(struct clk *clk)
326 struct clk_mapping *mapping = clk->mapping;
329 * Propagate mappings.
331 if (!mapping) {
332 struct clk *clkp;
335 * dummy mapping for root clocks with no specified ranges
337 if (!clk->parent) {
338 clk->mapping = &dummy_mapping;
339 return 0;
343 * If we're on a child clock and it provides no mapping of its
344 * own, inherit the mapping from its root clock.
346 clkp = lookup_root_clock(clk);
347 mapping = clkp->mapping;
348 BUG_ON(!mapping);
352 * Establish initial mapping.
354 if (!mapping->base && mapping->phys) {
355 kref_init(&mapping->ref);
357 mapping->base = ioremap_nocache(mapping->phys, mapping->len);
358 if (unlikely(!mapping->base))
359 return -ENXIO;
360 } else if (mapping->base) {
362 * Bump the refcount for an existing mapping
364 kref_get(&mapping->ref);
367 clk->mapping = mapping;
368 return 0;
371 static void clk_destroy_mapping(struct kref *kref)
373 struct clk_mapping *mapping;
375 mapping = container_of(kref, struct clk_mapping, ref);
377 iounmap(mapping->base);
380 static void clk_teardown_mapping(struct clk *clk)
382 struct clk_mapping *mapping = clk->mapping;
384 /* Nothing to do */
385 if (mapping == &dummy_mapping)
386 return;
388 kref_put(&mapping->ref, clk_destroy_mapping);
389 clk->mapping = NULL;
392 int clk_register(struct clk *clk)
394 int ret;
396 if (clk == NULL || IS_ERR(clk))
397 return -EINVAL;
400 * trap out already registered clocks
402 if (clk->node.next || clk->node.prev)
403 return 0;
405 mutex_lock(&clock_list_sem);
407 INIT_LIST_HEAD(&clk->children);
408 clk->usecount = 0;
410 ret = clk_establish_mapping(clk);
411 if (unlikely(ret))
412 goto out_unlock;
414 if (clk->parent)
415 list_add(&clk->sibling, &clk->parent->children);
416 else
417 list_add(&clk->sibling, &root_clks);
419 list_add(&clk->node, &clock_list);
421 #ifdef CONFIG_SH_CLK_CPG_LEGACY
422 if (clk->ops && clk->ops->init)
423 clk->ops->init(clk);
424 #endif
426 out_unlock:
427 mutex_unlock(&clock_list_sem);
429 return ret;
431 EXPORT_SYMBOL_GPL(clk_register);
433 void clk_unregister(struct clk *clk)
435 mutex_lock(&clock_list_sem);
436 list_del(&clk->sibling);
437 list_del(&clk->node);
438 clk_teardown_mapping(clk);
439 mutex_unlock(&clock_list_sem);
441 EXPORT_SYMBOL_GPL(clk_unregister);
443 void clk_enable_init_clocks(void)
445 struct clk *clkp;
447 list_for_each_entry(clkp, &clock_list, node)
448 if (clkp->flags & CLK_ENABLE_ON_INIT)
449 clk_enable(clkp);
452 unsigned long clk_get_rate(struct clk *clk)
454 return clk->rate;
456 EXPORT_SYMBOL_GPL(clk_get_rate);
458 int clk_set_rate(struct clk *clk, unsigned long rate)
460 int ret = -EOPNOTSUPP;
461 unsigned long flags;
463 spin_lock_irqsave(&clock_lock, flags);
465 if (likely(clk->ops && clk->ops->set_rate)) {
466 ret = clk->ops->set_rate(clk, rate);
467 if (ret != 0)
468 goto out_unlock;
469 } else {
470 clk->rate = rate;
471 ret = 0;
474 if (clk->ops && clk->ops->recalc)
475 clk->rate = clk->ops->recalc(clk);
477 propagate_rate(clk);
479 out_unlock:
480 spin_unlock_irqrestore(&clock_lock, flags);
482 return ret;
484 EXPORT_SYMBOL_GPL(clk_set_rate);
486 int clk_set_parent(struct clk *clk, struct clk *parent)
488 unsigned long flags;
489 int ret = -EINVAL;
491 if (!parent || !clk)
492 return ret;
493 if (clk->parent == parent)
494 return 0;
496 spin_lock_irqsave(&clock_lock, flags);
497 if (clk->usecount == 0) {
498 if (clk->ops->set_parent)
499 ret = clk->ops->set_parent(clk, parent);
500 else
501 ret = clk_reparent(clk, parent);
503 if (ret == 0) {
504 if (clk->ops->recalc)
505 clk->rate = clk->ops->recalc(clk);
506 pr_debug("set parent of %p to %p (new rate %ld)\n",
507 clk, clk->parent, clk->rate);
508 propagate_rate(clk);
510 } else
511 ret = -EBUSY;
512 spin_unlock_irqrestore(&clock_lock, flags);
514 return ret;
516 EXPORT_SYMBOL_GPL(clk_set_parent);
518 struct clk *clk_get_parent(struct clk *clk)
520 return clk->parent;
522 EXPORT_SYMBOL_GPL(clk_get_parent);
524 long clk_round_rate(struct clk *clk, unsigned long rate)
526 if (likely(clk->ops && clk->ops->round_rate)) {
527 unsigned long flags, rounded;
529 spin_lock_irqsave(&clock_lock, flags);
530 rounded = clk->ops->round_rate(clk, rate);
531 spin_unlock_irqrestore(&clock_lock, flags);
533 return rounded;
536 return clk_get_rate(clk);
538 EXPORT_SYMBOL_GPL(clk_round_rate);
540 long clk_round_parent(struct clk *clk, unsigned long target,
541 unsigned long *best_freq, unsigned long *parent_freq,
542 unsigned int div_min, unsigned int div_max)
544 struct cpufreq_frequency_table *freq, *best = NULL;
545 unsigned long error = ULONG_MAX, freq_high, freq_low, div;
546 struct clk *parent = clk_get_parent(clk);
548 if (!parent) {
549 *parent_freq = 0;
550 *best_freq = clk_round_rate(clk, target);
551 return abs(target - *best_freq);
554 for (freq = parent->freq_table; freq->frequency != CPUFREQ_TABLE_END;
555 freq++) {
556 if (freq->frequency == CPUFREQ_ENTRY_INVALID)
557 continue;
559 if (unlikely(freq->frequency / target <= div_min - 1)) {
560 unsigned long freq_max;
562 freq_max = (freq->frequency + div_min / 2) / div_min;
563 if (error > target - freq_max) {
564 error = target - freq_max;
565 best = freq;
566 if (best_freq)
567 *best_freq = freq_max;
570 pr_debug("too low freq %u, error %lu\n", freq->frequency,
571 target - freq_max);
573 if (!error)
574 break;
576 continue;
579 if (unlikely(freq->frequency / target >= div_max)) {
580 unsigned long freq_min;
582 freq_min = (freq->frequency + div_max / 2) / div_max;
583 if (error > freq_min - target) {
584 error = freq_min - target;
585 best = freq;
586 if (best_freq)
587 *best_freq = freq_min;
590 pr_debug("too high freq %u, error %lu\n", freq->frequency,
591 freq_min - target);
593 if (!error)
594 break;
596 continue;
599 div = freq->frequency / target;
600 freq_high = freq->frequency / div;
601 freq_low = freq->frequency / (div + 1);
603 if (freq_high - target < error) {
604 error = freq_high - target;
605 best = freq;
606 if (best_freq)
607 *best_freq = freq_high;
610 if (target - freq_low < error) {
611 error = target - freq_low;
612 best = freq;
613 if (best_freq)
614 *best_freq = freq_low;
617 pr_debug("%u / %lu = %lu, / %lu = %lu, best %lu, parent %u\n",
618 freq->frequency, div, freq_high, div + 1, freq_low,
619 *best_freq, best->frequency);
621 if (!error)
622 break;
625 if (parent_freq)
626 *parent_freq = best->frequency;
628 return error;
630 EXPORT_SYMBOL_GPL(clk_round_parent);
632 #ifdef CONFIG_PM
633 static void clks_core_resume(void)
635 struct clk *clkp;
637 list_for_each_entry(clkp, &clock_list, node) {
638 if (likely(clkp->ops)) {
639 unsigned long rate = clkp->rate;
641 if (likely(clkp->ops->set_parent))
642 clkp->ops->set_parent(clkp,
643 clkp->parent);
644 if (likely(clkp->ops->set_rate))
645 clkp->ops->set_rate(clkp, rate);
646 else if (likely(clkp->ops->recalc))
647 clkp->rate = clkp->ops->recalc(clkp);
652 static struct syscore_ops clks_syscore_ops = {
653 .resume = clks_core_resume,
656 static int __init clk_syscore_init(void)
658 register_syscore_ops(&clks_syscore_ops);
660 return 0;
662 subsys_initcall(clk_syscore_init);
663 #endif
666 * debugfs support to trace clock tree hierarchy and attributes
668 static struct dentry *clk_debugfs_root;
670 static int clk_debugfs_register_one(struct clk *c)
672 int err;
673 struct dentry *d, *child, *child_tmp;
674 struct clk *pa = c->parent;
675 char s[255];
676 char *p = s;
678 p += sprintf(p, "%p", c);
679 d = debugfs_create_dir(s, pa ? pa->dentry : clk_debugfs_root);
680 if (!d)
681 return -ENOMEM;
682 c->dentry = d;
684 d = debugfs_create_u8("usecount", S_IRUGO, c->dentry, (u8 *)&c->usecount);
685 if (!d) {
686 err = -ENOMEM;
687 goto err_out;
689 d = debugfs_create_u32("rate", S_IRUGO, c->dentry, (u32 *)&c->rate);
690 if (!d) {
691 err = -ENOMEM;
692 goto err_out;
694 d = debugfs_create_x32("flags", S_IRUGO, c->dentry, (u32 *)&c->flags);
695 if (!d) {
696 err = -ENOMEM;
697 goto err_out;
699 return 0;
701 err_out:
702 d = c->dentry;
703 list_for_each_entry_safe(child, child_tmp, &d->d_subdirs, d_u.d_child)
704 debugfs_remove(child);
705 debugfs_remove(c->dentry);
706 return err;
709 static int clk_debugfs_register(struct clk *c)
711 int err;
712 struct clk *pa = c->parent;
714 if (pa && !pa->dentry) {
715 err = clk_debugfs_register(pa);
716 if (err)
717 return err;
720 if (!c->dentry) {
721 err = clk_debugfs_register_one(c);
722 if (err)
723 return err;
725 return 0;
728 static int __init clk_debugfs_init(void)
730 struct clk *c;
731 struct dentry *d;
732 int err;
734 d = debugfs_create_dir("clock", NULL);
735 if (!d)
736 return -ENOMEM;
737 clk_debugfs_root = d;
739 list_for_each_entry(c, &clock_list, node) {
740 err = clk_debugfs_register(c);
741 if (err)
742 goto err_out;
744 return 0;
745 err_out:
746 debugfs_remove_recursive(clk_debugfs_root);
747 return err;
749 late_initcall(clk_debugfs_init);