blob: 352036b1f9a2111aea21cab3fe144e10aa8783b1 [file] [log] [blame]
Magnus Damm8b5ee112010-05-11 13:29:25 +00001/*
Paul Mundtde9186c2010-10-18 21:32:58 +09002 * SuperH clock framework
Magnus Damm8b5ee112010-05-11 13:29:25 +00003 *
Paul Mundt960bc362010-08-20 19:10:38 +09004 * Copyright (C) 2005 - 2010 Paul Mundt
Magnus Damm8b5ee112010-05-11 13:29:25 +00005 *
6 * This clock framework is derived from the OMAP version by:
7 *
8 * Copyright (C) 2004 - 2008 Nokia Corporation
9 * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
10 *
11 * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
12 *
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.
16 */
Paul Mundt550a1ef2010-10-13 19:24:55 +090017#define pr_fmt(fmt) "clock: " fmt
18
Magnus Damm8b5ee112010-05-11 13:29:25 +000019#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/mutex.h>
23#include <linux/list.h>
Rafael J. Wysockia696b892011-03-22 20:19:28 +000024#include <linux/syscore_ops.h>
Magnus Damm8b5ee112010-05-11 13:29:25 +000025#include <linux/seq_file.h>
26#include <linux/err.h>
Paul Mundt28085bc2010-10-15 16:46:37 +090027#include <linux/io.h>
Magnus Damm8b5ee112010-05-11 13:29:25 +000028#include <linux/debugfs.h>
29#include <linux/cpufreq.h>
30#include <linux/clk.h>
31#include <linux/sh_clk.h>
32
33static LIST_HEAD(clock_list);
34static DEFINE_SPINLOCK(clock_lock);
35static DEFINE_MUTEX(clock_list_sem);
36
Magnus Damm794d78f2011-06-21 07:55:12 +000037/* clock disable operations are not passed on to hardware during boot */
38static int allow_disable;
39
Magnus Damm8b5ee112010-05-11 13:29:25 +000040void clk_rate_table_build(struct clk *clk,
41 struct cpufreq_frequency_table *freq_table,
42 int nr_freqs,
43 struct clk_div_mult_table *src_table,
44 unsigned long *bitmap)
45{
46 unsigned long mult, div;
47 unsigned long freq;
48 int i;
49
Paul Mundtf5869032010-10-15 18:17:35 +090050 clk->nr_freqs = nr_freqs;
51
Magnus Damm8b5ee112010-05-11 13:29:25 +000052 for (i = 0; i < nr_freqs; i++) {
53 div = 1;
54 mult = 1;
55
56 if (src_table->divisors && i < src_table->nr_divisors)
57 div = src_table->divisors[i];
58
59 if (src_table->multipliers && i < src_table->nr_multipliers)
60 mult = src_table->multipliers[i];
61
62 if (!div || !mult || (bitmap && !test_bit(i, bitmap)))
63 freq = CPUFREQ_ENTRY_INVALID;
64 else
65 freq = clk->parent->rate * mult / div;
66
67 freq_table[i].index = i;
68 freq_table[i].frequency = freq;
69 }
70
71 /* Termination entry */
72 freq_table[i].index = i;
73 freq_table[i].frequency = CPUFREQ_TABLE_END;
74}
75
Paul Mundtf5869032010-10-15 18:17:35 +090076struct clk_rate_round_data;
77
78struct clk_rate_round_data {
79 unsigned long rate;
80 unsigned int min, max;
Paul Mundt8e122db2010-10-15 18:33:24 +090081 long (*func)(unsigned int, struct clk_rate_round_data *);
Paul Mundtf5869032010-10-15 18:17:35 +090082 void *arg;
83};
84
85#define for_each_frequency(pos, r, freq) \
Paul Mundte5690e02010-10-16 00:51:05 +090086 for (pos = r->min, freq = r->func(pos, r); \
Kuninori Morimotoc2590f42010-10-18 03:50:29 +000087 pos <= r->max; pos++, freq = r->func(pos, r)) \
Paul Mundtf5869032010-10-15 18:17:35 +090088 if (unlikely(freq == 0)) \
89 ; \
90 else
91
92static long clk_rate_round_helper(struct clk_rate_round_data *rounder)
Magnus Damm8b5ee112010-05-11 13:29:25 +000093{
94 unsigned long rate_error, rate_error_prev = ~0UL;
Paul Mundtf5869032010-10-15 18:17:35 +090095 unsigned long highest, lowest, freq;
Paul Mundt5aefa342010-11-01 15:30:31 -040096 long rate_best_fit = -ENOENT;
Magnus Damm8b5ee112010-05-11 13:29:25 +000097 int i;
98
Paul Mundt960bc362010-08-20 19:10:38 +090099 highest = 0;
100 lowest = ~0UL;
101
Paul Mundtf5869032010-10-15 18:17:35 +0900102 for_each_frequency(i, rounder, freq) {
Paul Mundt960bc362010-08-20 19:10:38 +0900103 if (freq > highest)
104 highest = freq;
105 if (freq < lowest)
106 lowest = freq;
107
Paul Mundtf5869032010-10-15 18:17:35 +0900108 rate_error = abs(freq - rounder->rate);
Magnus Damm8b5ee112010-05-11 13:29:25 +0000109 if (rate_error < rate_error_prev) {
110 rate_best_fit = freq;
111 rate_error_prev = rate_error;
112 }
113
114 if (rate_error == 0)
115 break;
116 }
117
Paul Mundtf5869032010-10-15 18:17:35 +0900118 if (rounder->rate >= highest)
Paul Mundt960bc362010-08-20 19:10:38 +0900119 rate_best_fit = highest;
Paul Mundtf5869032010-10-15 18:17:35 +0900120 if (rounder->rate <= lowest)
Paul Mundt960bc362010-08-20 19:10:38 +0900121 rate_best_fit = lowest;
122
Magnus Damm8b5ee112010-05-11 13:29:25 +0000123 return rate_best_fit;
124}
125
Paul Mundtf5869032010-10-15 18:17:35 +0900126static long clk_rate_table_iter(unsigned int pos,
127 struct clk_rate_round_data *rounder)
128{
129 struct cpufreq_frequency_table *freq_table = rounder->arg;
130 unsigned long freq = freq_table[pos].frequency;
131
132 if (freq == CPUFREQ_ENTRY_INVALID)
133 freq = 0;
134
135 return freq;
136}
137
138long clk_rate_table_round(struct clk *clk,
139 struct cpufreq_frequency_table *freq_table,
140 unsigned long rate)
141{
142 struct clk_rate_round_data table_round = {
143 .min = 0,
Kuninori Morimotoc2590f42010-10-18 03:50:29 +0000144 .max = clk->nr_freqs - 1,
Paul Mundtf5869032010-10-15 18:17:35 +0900145 .func = clk_rate_table_iter,
146 .arg = freq_table,
147 .rate = rate,
148 };
149
Kuninori Morimotoc2590f42010-10-18 03:50:29 +0000150 if (clk->nr_freqs < 1)
Paul Mundt5aefa342010-11-01 15:30:31 -0400151 return -ENOSYS;
Kuninori Morimotoc2590f42010-10-18 03:50:29 +0000152
Paul Mundtf5869032010-10-15 18:17:35 +0900153 return clk_rate_round_helper(&table_round);
154}
155
Paul Mundt8e122db2010-10-15 18:33:24 +0900156static long clk_rate_div_range_iter(unsigned int pos,
157 struct clk_rate_round_data *rounder)
158{
159 return clk_get_rate(rounder->arg) / pos;
160}
161
162long clk_rate_div_range_round(struct clk *clk, unsigned int div_min,
163 unsigned int div_max, unsigned long rate)
164{
165 struct clk_rate_round_data div_range_round = {
166 .min = div_min,
167 .max = div_max,
168 .func = clk_rate_div_range_iter,
169 .arg = clk_get_parent(clk),
170 .rate = rate,
171 };
172
173 return clk_rate_round_helper(&div_range_round);
174}
175
Kuninori Morimotodd2c0ca2011-09-19 18:51:13 -0700176static long clk_rate_mult_range_iter(unsigned int pos,
177 struct clk_rate_round_data *rounder)
178{
179 return clk_get_rate(rounder->arg) * pos;
180}
181
182long clk_rate_mult_range_round(struct clk *clk, unsigned int mult_min,
183 unsigned int mult_max, unsigned long rate)
184{
185 struct clk_rate_round_data mult_range_round = {
186 .min = mult_min,
187 .max = mult_max,
188 .func = clk_rate_mult_range_iter,
189 .arg = clk_get_parent(clk),
190 .rate = rate,
191 };
192
193 return clk_rate_round_helper(&mult_range_round);
194}
195
Magnus Damm8b5ee112010-05-11 13:29:25 +0000196int clk_rate_table_find(struct clk *clk,
197 struct cpufreq_frequency_table *freq_table,
198 unsigned long rate)
199{
200 int i;
201
202 for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
203 unsigned long freq = freq_table[i].frequency;
204
205 if (freq == CPUFREQ_ENTRY_INVALID)
206 continue;
207
208 if (freq == rate)
209 return i;
210 }
211
212 return -ENOENT;
213}
214
215/* Used for clocks that always have same value as the parent clock */
216unsigned long followparent_recalc(struct clk *clk)
217{
218 return clk->parent ? clk->parent->rate : 0;
219}
220
221int clk_reparent(struct clk *child, struct clk *parent)
222{
223 list_del_init(&child->sibling);
224 if (parent)
225 list_add(&child->sibling, &parent->children);
226 child->parent = parent;
227
228 /* now do the debugfs renaming to reattach the child
229 to the proper parent */
230
231 return 0;
232}
233
234/* Propagate rate to children */
235void propagate_rate(struct clk *tclk)
236{
237 struct clk *clkp;
238
239 list_for_each_entry(clkp, &tclk->children, sibling) {
240 if (clkp->ops && clkp->ops->recalc)
241 clkp->rate = clkp->ops->recalc(clkp);
242
243 propagate_rate(clkp);
244 }
245}
246
247static void __clk_disable(struct clk *clk)
248{
Magnus Damm69395392010-10-13 07:44:36 +0000249 if (WARN(!clk->usecount, "Trying to disable clock %p with 0 usecount\n",
250 clk))
Magnus Damm8b5ee112010-05-11 13:29:25 +0000251 return;
Magnus Damm8b5ee112010-05-11 13:29:25 +0000252
253 if (!(--clk->usecount)) {
Magnus Damm794d78f2011-06-21 07:55:12 +0000254 if (likely(allow_disable && clk->ops && clk->ops->disable))
Magnus Damm8b5ee112010-05-11 13:29:25 +0000255 clk->ops->disable(clk);
256 if (likely(clk->parent))
257 __clk_disable(clk->parent);
258 }
259}
260
261void clk_disable(struct clk *clk)
262{
263 unsigned long flags;
264
265 if (!clk)
266 return;
267
268 spin_lock_irqsave(&clock_lock, flags);
269 __clk_disable(clk);
270 spin_unlock_irqrestore(&clock_lock, flags);
271}
272EXPORT_SYMBOL_GPL(clk_disable);
273
274static int __clk_enable(struct clk *clk)
275{
276 int ret = 0;
277
278 if (clk->usecount++ == 0) {
279 if (clk->parent) {
280 ret = __clk_enable(clk->parent);
281 if (unlikely(ret))
282 goto err;
283 }
284
285 if (clk->ops && clk->ops->enable) {
286 ret = clk->ops->enable(clk);
287 if (ret) {
288 if (clk->parent)
289 __clk_disable(clk->parent);
290 goto err;
291 }
292 }
293 }
294
295 return ret;
296err:
297 clk->usecount--;
298 return ret;
299}
300
301int clk_enable(struct clk *clk)
302{
303 unsigned long flags;
304 int ret;
305
306 if (!clk)
307 return -EINVAL;
308
309 spin_lock_irqsave(&clock_lock, flags);
310 ret = __clk_enable(clk);
311 spin_unlock_irqrestore(&clock_lock, flags);
312
313 return ret;
314}
315EXPORT_SYMBOL_GPL(clk_enable);
316
317static LIST_HEAD(root_clks);
318
319/**
320 * recalculate_root_clocks - recalculate and propagate all root clocks
321 *
322 * Recalculates all root clocks (clocks with no parent), which if the
323 * clock's .recalc is set correctly, should also propagate their rates.
324 * Called at init.
325 */
326void recalculate_root_clocks(void)
327{
328 struct clk *clkp;
329
330 list_for_each_entry(clkp, &root_clks, sibling) {
331 if (clkp->ops && clkp->ops->recalc)
332 clkp->rate = clkp->ops->recalc(clkp);
333 propagate_rate(clkp);
334 }
335}
336
Paul Mundt28085bc2010-10-15 16:46:37 +0900337static struct clk_mapping dummy_mapping;
338
339static struct clk *lookup_root_clock(struct clk *clk)
340{
341 while (clk->parent)
342 clk = clk->parent;
343
344 return clk;
345}
346
347static int clk_establish_mapping(struct clk *clk)
348{
349 struct clk_mapping *mapping = clk->mapping;
350
351 /*
352 * Propagate mappings.
353 */
354 if (!mapping) {
355 struct clk *clkp;
356
357 /*
358 * dummy mapping for root clocks with no specified ranges
359 */
360 if (!clk->parent) {
361 clk->mapping = &dummy_mapping;
362 return 0;
363 }
364
365 /*
366 * If we're on a child clock and it provides no mapping of its
367 * own, inherit the mapping from its root clock.
368 */
369 clkp = lookup_root_clock(clk);
370 mapping = clkp->mapping;
371 BUG_ON(!mapping);
372 }
373
374 /*
375 * Establish initial mapping.
376 */
377 if (!mapping->base && mapping->phys) {
378 kref_init(&mapping->ref);
379
380 mapping->base = ioremap_nocache(mapping->phys, mapping->len);
381 if (unlikely(!mapping->base))
382 return -ENXIO;
383 } else if (mapping->base) {
384 /*
385 * Bump the refcount for an existing mapping
386 */
387 kref_get(&mapping->ref);
388 }
389
390 clk->mapping = mapping;
391 return 0;
392}
393
394static void clk_destroy_mapping(struct kref *kref)
395{
396 struct clk_mapping *mapping;
397
398 mapping = container_of(kref, struct clk_mapping, ref);
399
400 iounmap(mapping->base);
401}
402
403static void clk_teardown_mapping(struct clk *clk)
404{
405 struct clk_mapping *mapping = clk->mapping;
406
407 /* Nothing to do */
408 if (mapping == &dummy_mapping)
409 return;
410
411 kref_put(&mapping->ref, clk_destroy_mapping);
412 clk->mapping = NULL;
413}
414
Magnus Damm8b5ee112010-05-11 13:29:25 +0000415int clk_register(struct clk *clk)
416{
Paul Mundt28085bc2010-10-15 16:46:37 +0900417 int ret;
418
Paul Mundt225ca452011-06-24 17:35:40 +0900419 if (IS_ERR_OR_NULL(clk))
Magnus Damm8b5ee112010-05-11 13:29:25 +0000420 return -EINVAL;
421
422 /*
423 * trap out already registered clocks
424 */
425 if (clk->node.next || clk->node.prev)
426 return 0;
427
428 mutex_lock(&clock_list_sem);
429
430 INIT_LIST_HEAD(&clk->children);
431 clk->usecount = 0;
432
Paul Mundt28085bc2010-10-15 16:46:37 +0900433 ret = clk_establish_mapping(clk);
434 if (unlikely(ret))
435 goto out_unlock;
436
Magnus Damm8b5ee112010-05-11 13:29:25 +0000437 if (clk->parent)
438 list_add(&clk->sibling, &clk->parent->children);
439 else
440 list_add(&clk->sibling, &root_clks);
441
442 list_add(&clk->node, &clock_list);
Paul Mundtf278ea82010-11-19 16:40:35 +0900443
444#ifdef CONFIG_SH_CLK_CPG_LEGACY
Magnus Damm8b5ee112010-05-11 13:29:25 +0000445 if (clk->ops && clk->ops->init)
446 clk->ops->init(clk);
Paul Mundtf278ea82010-11-19 16:40:35 +0900447#endif
Paul Mundt28085bc2010-10-15 16:46:37 +0900448
449out_unlock:
Magnus Damm8b5ee112010-05-11 13:29:25 +0000450 mutex_unlock(&clock_list_sem);
451
Paul Mundt28085bc2010-10-15 16:46:37 +0900452 return ret;
Magnus Damm8b5ee112010-05-11 13:29:25 +0000453}
454EXPORT_SYMBOL_GPL(clk_register);
455
456void clk_unregister(struct clk *clk)
457{
458 mutex_lock(&clock_list_sem);
459 list_del(&clk->sibling);
460 list_del(&clk->node);
Paul Mundt28085bc2010-10-15 16:46:37 +0900461 clk_teardown_mapping(clk);
Magnus Damm8b5ee112010-05-11 13:29:25 +0000462 mutex_unlock(&clock_list_sem);
463}
464EXPORT_SYMBOL_GPL(clk_unregister);
465
466void clk_enable_init_clocks(void)
467{
468 struct clk *clkp;
469
470 list_for_each_entry(clkp, &clock_list, node)
471 if (clkp->flags & CLK_ENABLE_ON_INIT)
472 clk_enable(clkp);
473}
474
475unsigned long clk_get_rate(struct clk *clk)
476{
477 return clk->rate;
478}
479EXPORT_SYMBOL_GPL(clk_get_rate);
480
481int clk_set_rate(struct clk *clk, unsigned long rate)
482{
Magnus Damm8b5ee112010-05-11 13:29:25 +0000483 int ret = -EOPNOTSUPP;
484 unsigned long flags;
485
486 spin_lock_irqsave(&clock_lock, flags);
487
488 if (likely(clk->ops && clk->ops->set_rate)) {
Paul Mundt35a96c72010-11-15 18:18:32 +0900489 ret = clk->ops->set_rate(clk, rate);
Magnus Damm8b5ee112010-05-11 13:29:25 +0000490 if (ret != 0)
491 goto out_unlock;
492 } else {
493 clk->rate = rate;
494 ret = 0;
495 }
496
497 if (clk->ops && clk->ops->recalc)
498 clk->rate = clk->ops->recalc(clk);
499
500 propagate_rate(clk);
501
502out_unlock:
503 spin_unlock_irqrestore(&clock_lock, flags);
504
505 return ret;
506}
Paul Mundt9a1683d2010-11-15 18:14:43 +0900507EXPORT_SYMBOL_GPL(clk_set_rate);
Magnus Damm8b5ee112010-05-11 13:29:25 +0000508
509int clk_set_parent(struct clk *clk, struct clk *parent)
510{
511 unsigned long flags;
512 int ret = -EINVAL;
513
514 if (!parent || !clk)
515 return ret;
516 if (clk->parent == parent)
517 return 0;
518
519 spin_lock_irqsave(&clock_lock, flags);
520 if (clk->usecount == 0) {
521 if (clk->ops->set_parent)
522 ret = clk->ops->set_parent(clk, parent);
523 else
524 ret = clk_reparent(clk, parent);
525
526 if (ret == 0) {
Magnus Damm8b5ee112010-05-11 13:29:25 +0000527 if (clk->ops->recalc)
528 clk->rate = clk->ops->recalc(clk);
Paul Mundt550a1ef2010-10-13 19:24:55 +0900529 pr_debug("set parent of %p to %p (new rate %ld)\n",
Magnus Damm69395392010-10-13 07:44:36 +0000530 clk, clk->parent, clk->rate);
Magnus Damm8b5ee112010-05-11 13:29:25 +0000531 propagate_rate(clk);
532 }
533 } else
534 ret = -EBUSY;
535 spin_unlock_irqrestore(&clock_lock, flags);
536
537 return ret;
538}
539EXPORT_SYMBOL_GPL(clk_set_parent);
540
541struct clk *clk_get_parent(struct clk *clk)
542{
543 return clk->parent;
544}
545EXPORT_SYMBOL_GPL(clk_get_parent);
546
547long clk_round_rate(struct clk *clk, unsigned long rate)
548{
549 if (likely(clk->ops && clk->ops->round_rate)) {
550 unsigned long flags, rounded;
551
552 spin_lock_irqsave(&clock_lock, flags);
553 rounded = clk->ops->round_rate(clk, rate);
554 spin_unlock_irqrestore(&clock_lock, flags);
555
556 return rounded;
557 }
558
559 return clk_get_rate(clk);
560}
561EXPORT_SYMBOL_GPL(clk_round_rate);
562
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000563long clk_round_parent(struct clk *clk, unsigned long target,
564 unsigned long *best_freq, unsigned long *parent_freq,
565 unsigned int div_min, unsigned int div_max)
566{
567 struct cpufreq_frequency_table *freq, *best = NULL;
568 unsigned long error = ULONG_MAX, freq_high, freq_low, div;
569 struct clk *parent = clk_get_parent(clk);
570
571 if (!parent) {
572 *parent_freq = 0;
573 *best_freq = clk_round_rate(clk, target);
574 return abs(target - *best_freq);
575 }
576
577 for (freq = parent->freq_table; freq->frequency != CPUFREQ_TABLE_END;
578 freq++) {
579 if (freq->frequency == CPUFREQ_ENTRY_INVALID)
580 continue;
581
582 if (unlikely(freq->frequency / target <= div_min - 1)) {
Paul Mundta766b292010-11-08 09:40:23 +0900583 unsigned long freq_max;
584
585 freq_max = (freq->frequency + div_min / 2) / div_min;
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000586 if (error > target - freq_max) {
587 error = target - freq_max;
588 best = freq;
589 if (best_freq)
590 *best_freq = freq_max;
591 }
Paul Mundta766b292010-11-08 09:40:23 +0900592
Paul Mundted10b492010-11-10 18:02:25 +0900593 pr_debug("too low freq %u, error %lu\n", freq->frequency,
Paul Mundta766b292010-11-08 09:40:23 +0900594 target - freq_max);
595
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000596 if (!error)
597 break;
Paul Mundta766b292010-11-08 09:40:23 +0900598
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000599 continue;
600 }
601
602 if (unlikely(freq->frequency / target >= div_max)) {
Paul Mundta766b292010-11-08 09:40:23 +0900603 unsigned long freq_min;
604
605 freq_min = (freq->frequency + div_max / 2) / div_max;
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000606 if (error > freq_min - target) {
607 error = freq_min - target;
608 best = freq;
609 if (best_freq)
610 *best_freq = freq_min;
611 }
Paul Mundta766b292010-11-08 09:40:23 +0900612
Paul Mundted10b492010-11-10 18:02:25 +0900613 pr_debug("too high freq %u, error %lu\n", freq->frequency,
Paul Mundta766b292010-11-08 09:40:23 +0900614 freq_min - target);
615
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000616 if (!error)
617 break;
Paul Mundta766b292010-11-08 09:40:23 +0900618
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000619 continue;
620 }
621
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000622 div = freq->frequency / target;
623 freq_high = freq->frequency / div;
624 freq_low = freq->frequency / (div + 1);
Paul Mundta766b292010-11-08 09:40:23 +0900625
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000626 if (freq_high - target < error) {
627 error = freq_high - target;
628 best = freq;
629 if (best_freq)
630 *best_freq = freq_high;
631 }
Paul Mundta766b292010-11-08 09:40:23 +0900632
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000633 if (target - freq_low < error) {
634 error = target - freq_low;
635 best = freq;
636 if (best_freq)
637 *best_freq = freq_low;
638 }
Paul Mundta766b292010-11-08 09:40:23 +0900639
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000640 pr_debug("%u / %lu = %lu, / %lu = %lu, best %lu, parent %u\n",
641 freq->frequency, div, freq_high, div + 1, freq_low,
642 *best_freq, best->frequency);
Paul Mundta766b292010-11-08 09:40:23 +0900643
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000644 if (!error)
645 break;
646 }
Paul Mundta766b292010-11-08 09:40:23 +0900647
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000648 if (parent_freq)
649 *parent_freq = best->frequency;
Paul Mundta766b292010-11-08 09:40:23 +0900650
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000651 return error;
652}
653EXPORT_SYMBOL_GPL(clk_round_parent);
654
Magnus Damm8b5ee112010-05-11 13:29:25 +0000655#ifdef CONFIG_PM
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000656static void clks_core_resume(void)
Magnus Damm8b5ee112010-05-11 13:29:25 +0000657{
Magnus Damm8b5ee112010-05-11 13:29:25 +0000658 struct clk *clkp;
659
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000660 list_for_each_entry(clkp, &clock_list, node) {
Magnus Damm583af252011-06-13 04:42:15 +0000661 if (likely(clkp->usecount && clkp->ops)) {
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000662 unsigned long rate = clkp->rate;
Magnus Damm8b5ee112010-05-11 13:29:25 +0000663
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000664 if (likely(clkp->ops->set_parent))
665 clkp->ops->set_parent(clkp,
666 clkp->parent);
667 if (likely(clkp->ops->set_rate))
668 clkp->ops->set_rate(clkp, rate);
669 else if (likely(clkp->ops->recalc))
670 clkp->rate = clkp->ops->recalc(clkp);
Magnus Damm8b5ee112010-05-11 13:29:25 +0000671 }
Magnus Damm8b5ee112010-05-11 13:29:25 +0000672 }
Magnus Damm8b5ee112010-05-11 13:29:25 +0000673}
674
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000675static struct syscore_ops clks_syscore_ops = {
676 .resume = clks_core_resume,
677};
678
679static int __init clk_syscore_init(void)
Magnus Damm8b5ee112010-05-11 13:29:25 +0000680{
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000681 register_syscore_ops(&clks_syscore_ops);
Magnus Damm8b5ee112010-05-11 13:29:25 +0000682
683 return 0;
684}
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000685subsys_initcall(clk_syscore_init);
Magnus Damm8b5ee112010-05-11 13:29:25 +0000686#endif
687
688/*
689 * debugfs support to trace clock tree hierarchy and attributes
690 */
691static struct dentry *clk_debugfs_root;
692
693static int clk_debugfs_register_one(struct clk *c)
694{
695 int err;
Al Viro12520c42011-07-16 12:37:57 -0400696 struct dentry *d;
Magnus Damm8b5ee112010-05-11 13:29:25 +0000697 struct clk *pa = c->parent;
698 char s[255];
699 char *p = s;
700
Magnus Damm69395392010-10-13 07:44:36 +0000701 p += sprintf(p, "%p", c);
Magnus Damm8b5ee112010-05-11 13:29:25 +0000702 d = debugfs_create_dir(s, pa ? pa->dentry : clk_debugfs_root);
703 if (!d)
704 return -ENOMEM;
705 c->dentry = d;
706
707 d = debugfs_create_u8("usecount", S_IRUGO, c->dentry, (u8 *)&c->usecount);
708 if (!d) {
709 err = -ENOMEM;
710 goto err_out;
711 }
712 d = debugfs_create_u32("rate", S_IRUGO, c->dentry, (u32 *)&c->rate);
713 if (!d) {
714 err = -ENOMEM;
715 goto err_out;
716 }
717 d = debugfs_create_x32("flags", S_IRUGO, c->dentry, (u32 *)&c->flags);
718 if (!d) {
719 err = -ENOMEM;
720 goto err_out;
721 }
722 return 0;
723
724err_out:
Al Viro12520c42011-07-16 12:37:57 -0400725 debugfs_remove_recursive(c->dentry);
Magnus Damm8b5ee112010-05-11 13:29:25 +0000726 return err;
727}
728
729static int clk_debugfs_register(struct clk *c)
730{
731 int err;
732 struct clk *pa = c->parent;
733
734 if (pa && !pa->dentry) {
735 err = clk_debugfs_register(pa);
736 if (err)
737 return err;
738 }
739
Magnus Damm69395392010-10-13 07:44:36 +0000740 if (!c->dentry) {
Magnus Damm8b5ee112010-05-11 13:29:25 +0000741 err = clk_debugfs_register_one(c);
742 if (err)
743 return err;
744 }
745 return 0;
746}
747
748static int __init clk_debugfs_init(void)
749{
750 struct clk *c;
751 struct dentry *d;
752 int err;
753
754 d = debugfs_create_dir("clock", NULL);
755 if (!d)
756 return -ENOMEM;
757 clk_debugfs_root = d;
758
759 list_for_each_entry(c, &clock_list, node) {
760 err = clk_debugfs_register(c);
761 if (err)
762 goto err_out;
763 }
764 return 0;
765err_out:
766 debugfs_remove_recursive(clk_debugfs_root);
767 return err;
768}
769late_initcall(clk_debugfs_init);
Magnus Damm794d78f2011-06-21 07:55:12 +0000770
771static int __init clk_late_init(void)
772{
773 unsigned long flags;
774 struct clk *clk;
775
776 /* disable all clocks with zero use count */
777 mutex_lock(&clock_list_sem);
778 spin_lock_irqsave(&clock_lock, flags);
779
780 list_for_each_entry(clk, &clock_list, node)
781 if (!clk->usecount && clk->ops && clk->ops->disable)
782 clk->ops->disable(clk);
783
784 /* from now on allow clock disable operations */
785 allow_disable = 1;
786
787 spin_unlock_irqrestore(&clock_lock, flags);
788 mutex_unlock(&clock_list_sem);
789 return 0;
790}
791late_initcall(clk_late_init);