blob: 7e9c39951ecb87c086c9ab439df893a5c0980752 [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
37void 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)
42{
43 unsigned long mult, div;
44 unsigned long freq;
45 int i;
46
Paul Mundtf5869032010-10-15 18:17:35 +090047 clk->nr_freqs = nr_freqs;
48
Magnus Damm8b5ee112010-05-11 13:29:25 +000049 for (i = 0; i < nr_freqs; i++) {
50 div = 1;
51 mult = 1;
52
53 if (src_table->divisors && i < src_table->nr_divisors)
54 div = src_table->divisors[i];
55
56 if (src_table->multipliers && i < src_table->nr_multipliers)
57 mult = src_table->multipliers[i];
58
59 if (!div || !mult || (bitmap && !test_bit(i, bitmap)))
60 freq = CPUFREQ_ENTRY_INVALID;
61 else
62 freq = clk->parent->rate * mult / div;
63
64 freq_table[i].index = i;
65 freq_table[i].frequency = freq;
66 }
67
68 /* Termination entry */
69 freq_table[i].index = i;
70 freq_table[i].frequency = CPUFREQ_TABLE_END;
71}
72
Paul Mundtf5869032010-10-15 18:17:35 +090073struct clk_rate_round_data;
74
75struct clk_rate_round_data {
76 unsigned long rate;
77 unsigned int min, max;
Paul Mundt8e122db2010-10-15 18:33:24 +090078 long (*func)(unsigned int, struct clk_rate_round_data *);
Paul Mundtf5869032010-10-15 18:17:35 +090079 void *arg;
80};
81
82#define for_each_frequency(pos, r, freq) \
Paul Mundte5690e02010-10-16 00:51:05 +090083 for (pos = r->min, freq = r->func(pos, r); \
Kuninori Morimotoc2590f42010-10-18 03:50:29 +000084 pos <= r->max; pos++, freq = r->func(pos, r)) \
Paul Mundtf5869032010-10-15 18:17:35 +090085 if (unlikely(freq == 0)) \
86 ; \
87 else
88
89static long clk_rate_round_helper(struct clk_rate_round_data *rounder)
Magnus Damm8b5ee112010-05-11 13:29:25 +000090{
91 unsigned long rate_error, rate_error_prev = ~0UL;
Paul Mundtf5869032010-10-15 18:17:35 +090092 unsigned long highest, lowest, freq;
Paul Mundt5aefa342010-11-01 15:30:31 -040093 long rate_best_fit = -ENOENT;
Magnus Damm8b5ee112010-05-11 13:29:25 +000094 int i;
95
Paul Mundt960bc362010-08-20 19:10:38 +090096 highest = 0;
97 lowest = ~0UL;
98
Paul Mundtf5869032010-10-15 18:17:35 +090099 for_each_frequency(i, rounder, freq) {
Paul Mundt960bc362010-08-20 19:10:38 +0900100 if (freq > highest)
101 highest = freq;
102 if (freq < lowest)
103 lowest = freq;
104
Paul Mundtf5869032010-10-15 18:17:35 +0900105 rate_error = abs(freq - rounder->rate);
Magnus Damm8b5ee112010-05-11 13:29:25 +0000106 if (rate_error < rate_error_prev) {
107 rate_best_fit = freq;
108 rate_error_prev = rate_error;
109 }
110
111 if (rate_error == 0)
112 break;
113 }
114
Paul Mundtf5869032010-10-15 18:17:35 +0900115 if (rounder->rate >= highest)
Paul Mundt960bc362010-08-20 19:10:38 +0900116 rate_best_fit = highest;
Paul Mundtf5869032010-10-15 18:17:35 +0900117 if (rounder->rate <= lowest)
Paul Mundt960bc362010-08-20 19:10:38 +0900118 rate_best_fit = lowest;
119
Magnus Damm8b5ee112010-05-11 13:29:25 +0000120 return rate_best_fit;
121}
122
Paul Mundtf5869032010-10-15 18:17:35 +0900123static long clk_rate_table_iter(unsigned int pos,
124 struct clk_rate_round_data *rounder)
125{
126 struct cpufreq_frequency_table *freq_table = rounder->arg;
127 unsigned long freq = freq_table[pos].frequency;
128
129 if (freq == CPUFREQ_ENTRY_INVALID)
130 freq = 0;
131
132 return freq;
133}
134
135long clk_rate_table_round(struct clk *clk,
136 struct cpufreq_frequency_table *freq_table,
137 unsigned long rate)
138{
139 struct clk_rate_round_data table_round = {
140 .min = 0,
Kuninori Morimotoc2590f42010-10-18 03:50:29 +0000141 .max = clk->nr_freqs - 1,
Paul Mundtf5869032010-10-15 18:17:35 +0900142 .func = clk_rate_table_iter,
143 .arg = freq_table,
144 .rate = rate,
145 };
146
Kuninori Morimotoc2590f42010-10-18 03:50:29 +0000147 if (clk->nr_freqs < 1)
Paul Mundt5aefa342010-11-01 15:30:31 -0400148 return -ENOSYS;
Kuninori Morimotoc2590f42010-10-18 03:50:29 +0000149
Paul Mundtf5869032010-10-15 18:17:35 +0900150 return clk_rate_round_helper(&table_round);
151}
152
Paul Mundt8e122db2010-10-15 18:33:24 +0900153static long clk_rate_div_range_iter(unsigned int pos,
154 struct clk_rate_round_data *rounder)
155{
156 return clk_get_rate(rounder->arg) / pos;
157}
158
159long clk_rate_div_range_round(struct clk *clk, unsigned int div_min,
160 unsigned int div_max, unsigned long rate)
161{
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,
168 };
169
170 return clk_rate_round_helper(&div_range_round);
171}
172
Magnus Damm8b5ee112010-05-11 13:29:25 +0000173int clk_rate_table_find(struct clk *clk,
174 struct cpufreq_frequency_table *freq_table,
175 unsigned long rate)
176{
177 int i;
178
179 for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
180 unsigned long freq = freq_table[i].frequency;
181
182 if (freq == CPUFREQ_ENTRY_INVALID)
183 continue;
184
185 if (freq == rate)
186 return i;
187 }
188
189 return -ENOENT;
190}
191
192/* Used for clocks that always have same value as the parent clock */
193unsigned long followparent_recalc(struct clk *clk)
194{
195 return clk->parent ? clk->parent->rate : 0;
196}
197
198int clk_reparent(struct clk *child, struct clk *parent)
199{
200 list_del_init(&child->sibling);
201 if (parent)
202 list_add(&child->sibling, &parent->children);
203 child->parent = parent;
204
205 /* now do the debugfs renaming to reattach the child
206 to the proper parent */
207
208 return 0;
209}
210
211/* Propagate rate to children */
212void propagate_rate(struct clk *tclk)
213{
214 struct clk *clkp;
215
216 list_for_each_entry(clkp, &tclk->children, sibling) {
217 if (clkp->ops && clkp->ops->recalc)
218 clkp->rate = clkp->ops->recalc(clkp);
219
220 propagate_rate(clkp);
221 }
222}
223
224static void __clk_disable(struct clk *clk)
225{
Magnus Damm69395392010-10-13 07:44:36 +0000226 if (WARN(!clk->usecount, "Trying to disable clock %p with 0 usecount\n",
227 clk))
Magnus Damm8b5ee112010-05-11 13:29:25 +0000228 return;
Magnus Damm8b5ee112010-05-11 13:29:25 +0000229
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);
235 }
236}
237
238void clk_disable(struct clk *clk)
239{
240 unsigned long flags;
241
242 if (!clk)
243 return;
244
245 spin_lock_irqsave(&clock_lock, flags);
246 __clk_disable(clk);
247 spin_unlock_irqrestore(&clock_lock, flags);
248}
249EXPORT_SYMBOL_GPL(clk_disable);
250
251static int __clk_enable(struct clk *clk)
252{
253 int ret = 0;
254
255 if (clk->usecount++ == 0) {
256 if (clk->parent) {
257 ret = __clk_enable(clk->parent);
258 if (unlikely(ret))
259 goto err;
260 }
261
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;
268 }
269 }
270 }
271
272 return ret;
273err:
274 clk->usecount--;
275 return ret;
276}
277
278int clk_enable(struct clk *clk)
279{
280 unsigned long flags;
281 int ret;
282
283 if (!clk)
284 return -EINVAL;
285
286 spin_lock_irqsave(&clock_lock, flags);
287 ret = __clk_enable(clk);
288 spin_unlock_irqrestore(&clock_lock, flags);
289
290 return ret;
291}
292EXPORT_SYMBOL_GPL(clk_enable);
293
294static LIST_HEAD(root_clks);
295
296/**
297 * recalculate_root_clocks - recalculate and propagate all root clocks
298 *
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.
302 */
303void recalculate_root_clocks(void)
304{
305 struct clk *clkp;
306
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);
311 }
312}
313
Paul Mundt28085bc2010-10-15 16:46:37 +0900314static struct clk_mapping dummy_mapping;
315
316static struct clk *lookup_root_clock(struct clk *clk)
317{
318 while (clk->parent)
319 clk = clk->parent;
320
321 return clk;
322}
323
324static int clk_establish_mapping(struct clk *clk)
325{
326 struct clk_mapping *mapping = clk->mapping;
327
328 /*
329 * Propagate mappings.
330 */
331 if (!mapping) {
332 struct clk *clkp;
333
334 /*
335 * dummy mapping for root clocks with no specified ranges
336 */
337 if (!clk->parent) {
338 clk->mapping = &dummy_mapping;
339 return 0;
340 }
341
342 /*
343 * If we're on a child clock and it provides no mapping of its
344 * own, inherit the mapping from its root clock.
345 */
346 clkp = lookup_root_clock(clk);
347 mapping = clkp->mapping;
348 BUG_ON(!mapping);
349 }
350
351 /*
352 * Establish initial mapping.
353 */
354 if (!mapping->base && mapping->phys) {
355 kref_init(&mapping->ref);
356
357 mapping->base = ioremap_nocache(mapping->phys, mapping->len);
358 if (unlikely(!mapping->base))
359 return -ENXIO;
360 } else if (mapping->base) {
361 /*
362 * Bump the refcount for an existing mapping
363 */
364 kref_get(&mapping->ref);
365 }
366
367 clk->mapping = mapping;
368 return 0;
369}
370
371static void clk_destroy_mapping(struct kref *kref)
372{
373 struct clk_mapping *mapping;
374
375 mapping = container_of(kref, struct clk_mapping, ref);
376
377 iounmap(mapping->base);
378}
379
380static void clk_teardown_mapping(struct clk *clk)
381{
382 struct clk_mapping *mapping = clk->mapping;
383
384 /* Nothing to do */
385 if (mapping == &dummy_mapping)
386 return;
387
388 kref_put(&mapping->ref, clk_destroy_mapping);
389 clk->mapping = NULL;
390}
391
Magnus Damm8b5ee112010-05-11 13:29:25 +0000392int clk_register(struct clk *clk)
393{
Paul Mundt28085bc2010-10-15 16:46:37 +0900394 int ret;
395
Magnus Damm8b5ee112010-05-11 13:29:25 +0000396 if (clk == NULL || IS_ERR(clk))
397 return -EINVAL;
398
399 /*
400 * trap out already registered clocks
401 */
402 if (clk->node.next || clk->node.prev)
403 return 0;
404
405 mutex_lock(&clock_list_sem);
406
407 INIT_LIST_HEAD(&clk->children);
408 clk->usecount = 0;
409
Paul Mundt28085bc2010-10-15 16:46:37 +0900410 ret = clk_establish_mapping(clk);
411 if (unlikely(ret))
412 goto out_unlock;
413
Magnus Damm8b5ee112010-05-11 13:29:25 +0000414 if (clk->parent)
415 list_add(&clk->sibling, &clk->parent->children);
416 else
417 list_add(&clk->sibling, &root_clks);
418
419 list_add(&clk->node, &clock_list);
Paul Mundtf278ea82010-11-19 16:40:35 +0900420
421#ifdef CONFIG_SH_CLK_CPG_LEGACY
Magnus Damm8b5ee112010-05-11 13:29:25 +0000422 if (clk->ops && clk->ops->init)
423 clk->ops->init(clk);
Paul Mundtf278ea82010-11-19 16:40:35 +0900424#endif
Paul Mundt28085bc2010-10-15 16:46:37 +0900425
426out_unlock:
Magnus Damm8b5ee112010-05-11 13:29:25 +0000427 mutex_unlock(&clock_list_sem);
428
Paul Mundt28085bc2010-10-15 16:46:37 +0900429 return ret;
Magnus Damm8b5ee112010-05-11 13:29:25 +0000430}
431EXPORT_SYMBOL_GPL(clk_register);
432
433void clk_unregister(struct clk *clk)
434{
435 mutex_lock(&clock_list_sem);
436 list_del(&clk->sibling);
437 list_del(&clk->node);
Paul Mundt28085bc2010-10-15 16:46:37 +0900438 clk_teardown_mapping(clk);
Magnus Damm8b5ee112010-05-11 13:29:25 +0000439 mutex_unlock(&clock_list_sem);
440}
441EXPORT_SYMBOL_GPL(clk_unregister);
442
443void clk_enable_init_clocks(void)
444{
445 struct clk *clkp;
446
447 list_for_each_entry(clkp, &clock_list, node)
448 if (clkp->flags & CLK_ENABLE_ON_INIT)
449 clk_enable(clkp);
450}
451
452unsigned long clk_get_rate(struct clk *clk)
453{
454 return clk->rate;
455}
456EXPORT_SYMBOL_GPL(clk_get_rate);
457
458int clk_set_rate(struct clk *clk, unsigned long rate)
459{
Magnus Damm8b5ee112010-05-11 13:29:25 +0000460 int ret = -EOPNOTSUPP;
461 unsigned long flags;
462
463 spin_lock_irqsave(&clock_lock, flags);
464
465 if (likely(clk->ops && clk->ops->set_rate)) {
Paul Mundt35a96c72010-11-15 18:18:32 +0900466 ret = clk->ops->set_rate(clk, rate);
Magnus Damm8b5ee112010-05-11 13:29:25 +0000467 if (ret != 0)
468 goto out_unlock;
469 } else {
470 clk->rate = rate;
471 ret = 0;
472 }
473
474 if (clk->ops && clk->ops->recalc)
475 clk->rate = clk->ops->recalc(clk);
476
477 propagate_rate(clk);
478
479out_unlock:
480 spin_unlock_irqrestore(&clock_lock, flags);
481
482 return ret;
483}
Paul Mundt9a1683d2010-11-15 18:14:43 +0900484EXPORT_SYMBOL_GPL(clk_set_rate);
Magnus Damm8b5ee112010-05-11 13:29:25 +0000485
486int clk_set_parent(struct clk *clk, struct clk *parent)
487{
488 unsigned long flags;
489 int ret = -EINVAL;
490
491 if (!parent || !clk)
492 return ret;
493 if (clk->parent == parent)
494 return 0;
495
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);
502
503 if (ret == 0) {
Magnus Damm8b5ee112010-05-11 13:29:25 +0000504 if (clk->ops->recalc)
505 clk->rate = clk->ops->recalc(clk);
Paul Mundt550a1ef2010-10-13 19:24:55 +0900506 pr_debug("set parent of %p to %p (new rate %ld)\n",
Magnus Damm69395392010-10-13 07:44:36 +0000507 clk, clk->parent, clk->rate);
Magnus Damm8b5ee112010-05-11 13:29:25 +0000508 propagate_rate(clk);
509 }
510 } else
511 ret = -EBUSY;
512 spin_unlock_irqrestore(&clock_lock, flags);
513
514 return ret;
515}
516EXPORT_SYMBOL_GPL(clk_set_parent);
517
518struct clk *clk_get_parent(struct clk *clk)
519{
520 return clk->parent;
521}
522EXPORT_SYMBOL_GPL(clk_get_parent);
523
524long clk_round_rate(struct clk *clk, unsigned long rate)
525{
526 if (likely(clk->ops && clk->ops->round_rate)) {
527 unsigned long flags, rounded;
528
529 spin_lock_irqsave(&clock_lock, flags);
530 rounded = clk->ops->round_rate(clk, rate);
531 spin_unlock_irqrestore(&clock_lock, flags);
532
533 return rounded;
534 }
535
536 return clk_get_rate(clk);
537}
538EXPORT_SYMBOL_GPL(clk_round_rate);
539
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000540long 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)
543{
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);
547
548 if (!parent) {
549 *parent_freq = 0;
550 *best_freq = clk_round_rate(clk, target);
551 return abs(target - *best_freq);
552 }
553
554 for (freq = parent->freq_table; freq->frequency != CPUFREQ_TABLE_END;
555 freq++) {
556 if (freq->frequency == CPUFREQ_ENTRY_INVALID)
557 continue;
558
559 if (unlikely(freq->frequency / target <= div_min - 1)) {
Paul Mundta766b292010-11-08 09:40:23 +0900560 unsigned long freq_max;
561
562 freq_max = (freq->frequency + div_min / 2) / div_min;
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000563 if (error > target - freq_max) {
564 error = target - freq_max;
565 best = freq;
566 if (best_freq)
567 *best_freq = freq_max;
568 }
Paul Mundta766b292010-11-08 09:40:23 +0900569
Paul Mundted10b492010-11-10 18:02:25 +0900570 pr_debug("too low freq %u, error %lu\n", freq->frequency,
Paul Mundta766b292010-11-08 09:40:23 +0900571 target - freq_max);
572
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000573 if (!error)
574 break;
Paul Mundta766b292010-11-08 09:40:23 +0900575
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000576 continue;
577 }
578
579 if (unlikely(freq->frequency / target >= div_max)) {
Paul Mundta766b292010-11-08 09:40:23 +0900580 unsigned long freq_min;
581
582 freq_min = (freq->frequency + div_max / 2) / div_max;
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000583 if (error > freq_min - target) {
584 error = freq_min - target;
585 best = freq;
586 if (best_freq)
587 *best_freq = freq_min;
588 }
Paul Mundta766b292010-11-08 09:40:23 +0900589
Paul Mundted10b492010-11-10 18:02:25 +0900590 pr_debug("too high freq %u, error %lu\n", freq->frequency,
Paul Mundta766b292010-11-08 09:40:23 +0900591 freq_min - target);
592
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000593 if (!error)
594 break;
Paul Mundta766b292010-11-08 09:40:23 +0900595
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000596 continue;
597 }
598
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000599 div = freq->frequency / target;
600 freq_high = freq->frequency / div;
601 freq_low = freq->frequency / (div + 1);
Paul Mundta766b292010-11-08 09:40:23 +0900602
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000603 if (freq_high - target < error) {
604 error = freq_high - target;
605 best = freq;
606 if (best_freq)
607 *best_freq = freq_high;
608 }
Paul Mundta766b292010-11-08 09:40:23 +0900609
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000610 if (target - freq_low < error) {
611 error = target - freq_low;
612 best = freq;
613 if (best_freq)
614 *best_freq = freq_low;
615 }
Paul Mundta766b292010-11-08 09:40:23 +0900616
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000617 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);
Paul Mundta766b292010-11-08 09:40:23 +0900620
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000621 if (!error)
622 break;
623 }
Paul Mundta766b292010-11-08 09:40:23 +0900624
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000625 if (parent_freq)
626 *parent_freq = best->frequency;
Paul Mundta766b292010-11-08 09:40:23 +0900627
Guennadi Liakhovetski6af26c62010-11-02 11:27:24 +0000628 return error;
629}
630EXPORT_SYMBOL_GPL(clk_round_parent);
631
Magnus Damm8b5ee112010-05-11 13:29:25 +0000632#ifdef CONFIG_PM
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000633static void clks_core_resume(void)
Magnus Damm8b5ee112010-05-11 13:29:25 +0000634{
Magnus Damm8b5ee112010-05-11 13:29:25 +0000635 struct clk *clkp;
636
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000637 list_for_each_entry(clkp, &clock_list, node) {
Magnus Damm583af252011-06-13 04:42:15 +0000638 if (likely(clkp->usecount && clkp->ops)) {
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000639 unsigned long rate = clkp->rate;
Magnus Damm8b5ee112010-05-11 13:29:25 +0000640
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000641 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);
Magnus Damm8b5ee112010-05-11 13:29:25 +0000648 }
Magnus Damm8b5ee112010-05-11 13:29:25 +0000649 }
Magnus Damm8b5ee112010-05-11 13:29:25 +0000650}
651
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000652static struct syscore_ops clks_syscore_ops = {
653 .resume = clks_core_resume,
654};
655
656static int __init clk_syscore_init(void)
Magnus Damm8b5ee112010-05-11 13:29:25 +0000657{
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000658 register_syscore_ops(&clks_syscore_ops);
Magnus Damm8b5ee112010-05-11 13:29:25 +0000659
660 return 0;
661}
Rafael J. Wysockia696b892011-03-22 20:19:28 +0000662subsys_initcall(clk_syscore_init);
Magnus Damm8b5ee112010-05-11 13:29:25 +0000663#endif
664
665/*
666 * debugfs support to trace clock tree hierarchy and attributes
667 */
668static struct dentry *clk_debugfs_root;
669
670static int clk_debugfs_register_one(struct clk *c)
671{
672 int err;
673 struct dentry *d, *child, *child_tmp;
674 struct clk *pa = c->parent;
675 char s[255];
676 char *p = s;
677
Magnus Damm69395392010-10-13 07:44:36 +0000678 p += sprintf(p, "%p", c);
Magnus Damm8b5ee112010-05-11 13:29:25 +0000679 d = debugfs_create_dir(s, pa ? pa->dentry : clk_debugfs_root);
680 if (!d)
681 return -ENOMEM;
682 c->dentry = d;
683
684 d = debugfs_create_u8("usecount", S_IRUGO, c->dentry, (u8 *)&c->usecount);
685 if (!d) {
686 err = -ENOMEM;
687 goto err_out;
688 }
689 d = debugfs_create_u32("rate", S_IRUGO, c->dentry, (u32 *)&c->rate);
690 if (!d) {
691 err = -ENOMEM;
692 goto err_out;
693 }
694 d = debugfs_create_x32("flags", S_IRUGO, c->dentry, (u32 *)&c->flags);
695 if (!d) {
696 err = -ENOMEM;
697 goto err_out;
698 }
699 return 0;
700
701err_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;
707}
708
709static int clk_debugfs_register(struct clk *c)
710{
711 int err;
712 struct clk *pa = c->parent;
713
714 if (pa && !pa->dentry) {
715 err = clk_debugfs_register(pa);
716 if (err)
717 return err;
718 }
719
Magnus Damm69395392010-10-13 07:44:36 +0000720 if (!c->dentry) {
Magnus Damm8b5ee112010-05-11 13:29:25 +0000721 err = clk_debugfs_register_one(c);
722 if (err)
723 return err;
724 }
725 return 0;
726}
727
728static int __init clk_debugfs_init(void)
729{
730 struct clk *c;
731 struct dentry *d;
732 int err;
733
734 d = debugfs_create_dir("clock", NULL);
735 if (!d)
736 return -ENOMEM;
737 clk_debugfs_root = d;
738
739 list_for_each_entry(c, &clock_list, node) {
740 err = clk_debugfs_register(c);
741 if (err)
742 goto err_out;
743 }
744 return 0;
745err_out:
746 debugfs_remove_recursive(clk_debugfs_root);
747 return err;
748}
749late_initcall(clk_debugfs_init);