17 #define pr_fmt(fmt) "clock: " fmt
19 #include <linux/kernel.h>
21 #include <linux/module.h>
23 #include <linux/list.h>
37 static int allow_disable;
51 for (i = 0; i < nr_freqs; i++) {
55 if (src_table->
divisors && i < src_table->nr_divisors)
58 if (src_table->
multipliers && i < src_table->nr_multipliers)
61 if (!div || !mult || (bitmap && !
test_bit(i, bitmap)))
84 #define for_each_frequency(pos, r, freq) \
85 for (pos = r->min, freq = r->func(pos, r); \
86 pos <= r->max; pos++, freq = r->func(pos, r)) \
87 if (unlikely(freq == 0)) \
93 unsigned long rate_error, rate_error_prev = ~0
UL;
95 long rate_best_fit = -
ENOENT;
107 rate_error =
abs(freq - rounder->
rate);
108 if (rate_error < rate_error_prev) {
109 rate_best_fit =
freq;
110 rate_error_prev = rate_error;
117 if (rounder->
rate >= highest)
119 if (rounder->
rate <= lowest)
120 rate_best_fit = lowest;
122 return rate_best_fit;
125 static long clk_rate_table_iter(
unsigned int pos,
144 .func = clk_rate_table_iter,
152 return clk_rate_round_helper(&table_round);
155 static long clk_rate_div_range_iter(
unsigned int pos,
162 unsigned int div_max,
unsigned long rate)
167 .func = clk_rate_div_range_iter,
172 return clk_rate_round_helper(&div_range_round);
175 static long clk_rate_mult_range_iter(
unsigned int pos,
182 unsigned int mult_max,
unsigned long rate)
187 .func = clk_rate_mult_range_iter,
192 return clk_rate_round_helper(&mult_range_round);
222 list_del_init(&child->
sibling);
236 if (clkp->
ops && clkp->
ops->recalc)
237 clkp->
rate = clkp->
ops->recalc(clkp);
245 if (
WARN(!clk->
usecount,
"Trying to disable clock %p with 0 usecount\n",
250 if (
likely(allow_disable && clk->
ops && clk->
ops->disable))
251 clk->
ops->disable(clk);
266 spin_unlock_irqrestore(&clock_lock, flags);
281 if (clk->
ops && clk->
ops->enable) {
282 ret = clk->
ops->enable(clk);
307 spin_unlock_irqrestore(&clock_lock, flags);
327 if (clkp->
ops && clkp->
ops->recalc)
328 clkp->
rate = clkp->
ops->recalc(clkp);
335 static struct clk *lookup_root_clock(
struct clk *clk)
343 static int clk_establish_mapping(
struct clk *clk)
365 clkp = lookup_root_clock(clk);
373 if (!mapping->
base && mapping->
phys) {
374 kref_init(&mapping->
ref);
379 }
else if (mapping->
base) {
383 kref_get(&mapping->
ref);
393 static void clk_destroy_mapping(
struct kref *
kref)
402 static void clk_teardown_mapping(
struct clk *clk)
407 if (mapping == &dummy_mapping)
410 kref_put(&mapping->
ref, clk_destroy_mapping);
420 if (IS_ERR_OR_NULL(clk))
426 if (clk->
node.next || clk->
node.prev)
434 ret = clk_establish_mapping(clk);
441 list_add(&clk->
sibling, &root_clks);
443 list_add(&clk->
node, &clock_list);
445 #ifdef CONFIG_SH_CLK_CPG_LEGACY
446 if (clk->
ops && clk->
ops->init)
462 clk_teardown_mapping(clk);
490 ret = clk->
ops->set_rate(clk, rate);
498 if (clk->
ops && clk->
ops->recalc)
499 clk->
rate = clk->
ops->recalc(clk);
504 spin_unlock_irqrestore(&clock_lock, flags);
517 if (clk->
parent == parent)
522 if (clk->
ops->set_parent)
523 ret = clk->
ops->set_parent(clk, parent);
528 if (clk->
ops->recalc)
529 clk->
rate = clk->
ops->recalc(clk);
530 pr_debug(
"set parent of %p to %p (new rate %ld)\n",
536 spin_unlock_irqrestore(&clock_lock, flags);
551 unsigned long flags, rounded;
554 rounded = clk->
ops->round_rate(clk, rate);
555 spin_unlock_irqrestore(&clock_lock, flags);
565 unsigned long *best_freq,
unsigned long *parent_freq,
566 unsigned int div_min,
unsigned int div_max)
575 return abs(target - *best_freq);
584 unsigned long freq_max;
586 freq_max = (freq->
frequency + div_min / 2) / div_min;
587 if (error > target - freq_max) {
588 error = target - freq_max;
591 *best_freq = freq_max;
604 unsigned long freq_min;
606 freq_min = (freq->
frequency + div_max / 2) / div_max;
607 if (error > freq_min - target) {
608 error = freq_min -
target;
611 *best_freq = freq_min;
627 if (freq_high - target < error) {
628 error = freq_high -
target;
631 *best_freq = freq_high;
634 if (target - freq_low < error) {
635 error = target - freq_low;
638 *best_freq = freq_low;
641 pr_debug(
"%u / %lu = %lu, / %lu = %lu, best %lu, parent %u\n",
642 freq->
frequency, div, freq_high, div + 1, freq_low,
657 static void clks_core_resume(
void)
666 clkp->
ops->set_parent(clkp,
669 clkp->
ops->set_rate(clkp, rate);
671 clkp->
rate = clkp->
ops->recalc(clkp);
677 .
resume = clks_core_resume,
680 static int __init clk_syscore_init(
void)
689 static int __init clk_late_init(
void)
705 spin_unlock_irqrestore(&clock_lock, flags);