]>
Commit | Line | Data |
---|---|---|
8b5ee113 | 1 | /* |
de9186c2 | 2 | * SuperH clock framework |
8b5ee113 | 3 | * |
960bc368 | 4 | * Copyright (C) 2005 - 2010 Paul Mundt |
8b5ee113 MD |
5 | * |
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 | */ | |
550a1ef1 PM |
17 | #define pr_fmt(fmt) "clock: " fmt |
18 | ||
8b5ee113 MD |
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/kobject.h> | |
25 | #include <linux/sysdev.h> | |
26 | #include <linux/seq_file.h> | |
27 | #include <linux/err.h> | |
28085bc5 | 28 | #include <linux/io.h> |
8b5ee113 MD |
29 | #include <linux/debugfs.h> |
30 | #include <linux/cpufreq.h> | |
31 | #include <linux/clk.h> | |
32 | #include <linux/sh_clk.h> | |
33 | ||
34 | static LIST_HEAD(clock_list); | |
35 | static DEFINE_SPINLOCK(clock_lock); | |
36 | static DEFINE_MUTEX(clock_list_sem); | |
37 | ||
38 | void clk_rate_table_build(struct clk *clk, | |
39 | struct cpufreq_frequency_table *freq_table, | |
40 | int nr_freqs, | |
41 | struct clk_div_mult_table *src_table, | |
42 | unsigned long *bitmap) | |
43 | { | |
44 | unsigned long mult, div; | |
45 | unsigned long freq; | |
46 | int i; | |
47 | ||
f586903d PM |
48 | clk->nr_freqs = nr_freqs; |
49 | ||
8b5ee113 MD |
50 | for (i = 0; i < nr_freqs; i++) { |
51 | div = 1; | |
52 | mult = 1; | |
53 | ||
54 | if (src_table->divisors && i < src_table->nr_divisors) | |
55 | div = src_table->divisors[i]; | |
56 | ||
57 | if (src_table->multipliers && i < src_table->nr_multipliers) | |
58 | mult = src_table->multipliers[i]; | |
59 | ||
60 | if (!div || !mult || (bitmap && !test_bit(i, bitmap))) | |
61 | freq = CPUFREQ_ENTRY_INVALID; | |
62 | else | |
63 | freq = clk->parent->rate * mult / div; | |
64 | ||
65 | freq_table[i].index = i; | |
66 | freq_table[i].frequency = freq; | |
67 | } | |
68 | ||
69 | /* Termination entry */ | |
70 | freq_table[i].index = i; | |
71 | freq_table[i].frequency = CPUFREQ_TABLE_END; | |
72 | } | |
73 | ||
f586903d PM |
74 | struct clk_rate_round_data; |
75 | ||
76 | struct clk_rate_round_data { | |
77 | unsigned long rate; | |
78 | unsigned int min, max; | |
8e122db6 | 79 | long (*func)(unsigned int, struct clk_rate_round_data *); |
f586903d PM |
80 | void *arg; |
81 | }; | |
82 | ||
83 | #define for_each_frequency(pos, r, freq) \ | |
e5690e0d | 84 | for (pos = r->min, freq = r->func(pos, r); \ |
c2590f4a | 85 | pos <= r->max; pos++, freq = r->func(pos, r)) \ |
f586903d PM |
86 | if (unlikely(freq == 0)) \ |
87 | ; \ | |
88 | else | |
89 | ||
90 | static long clk_rate_round_helper(struct clk_rate_round_data *rounder) | |
8b5ee113 MD |
91 | { |
92 | unsigned long rate_error, rate_error_prev = ~0UL; | |
f586903d | 93 | unsigned long highest, lowest, freq; |
5aefa34f | 94 | long rate_best_fit = -ENOENT; |
8b5ee113 MD |
95 | int i; |
96 | ||
960bc368 PM |
97 | highest = 0; |
98 | lowest = ~0UL; | |
99 | ||
f586903d | 100 | for_each_frequency(i, rounder, freq) { |
960bc368 PM |
101 | if (freq > highest) |
102 | highest = freq; | |
103 | if (freq < lowest) | |
104 | lowest = freq; | |
105 | ||
f586903d | 106 | rate_error = abs(freq - rounder->rate); |
8b5ee113 MD |
107 | if (rate_error < rate_error_prev) { |
108 | rate_best_fit = freq; | |
109 | rate_error_prev = rate_error; | |
110 | } | |
111 | ||
112 | if (rate_error == 0) | |
113 | break; | |
114 | } | |
115 | ||
f586903d | 116 | if (rounder->rate >= highest) |
960bc368 | 117 | rate_best_fit = highest; |
f586903d | 118 | if (rounder->rate <= lowest) |
960bc368 PM |
119 | rate_best_fit = lowest; |
120 | ||
8b5ee113 MD |
121 | return rate_best_fit; |
122 | } | |
123 | ||
f586903d PM |
124 | static long clk_rate_table_iter(unsigned int pos, |
125 | struct clk_rate_round_data *rounder) | |
126 | { | |
127 | struct cpufreq_frequency_table *freq_table = rounder->arg; | |
128 | unsigned long freq = freq_table[pos].frequency; | |
129 | ||
130 | if (freq == CPUFREQ_ENTRY_INVALID) | |
131 | freq = 0; | |
132 | ||
133 | return freq; | |
134 | } | |
135 | ||
136 | long clk_rate_table_round(struct clk *clk, | |
137 | struct cpufreq_frequency_table *freq_table, | |
138 | unsigned long rate) | |
139 | { | |
140 | struct clk_rate_round_data table_round = { | |
141 | .min = 0, | |
c2590f4a | 142 | .max = clk->nr_freqs - 1, |
f586903d PM |
143 | .func = clk_rate_table_iter, |
144 | .arg = freq_table, | |
145 | .rate = rate, | |
146 | }; | |
147 | ||
c2590f4a | 148 | if (clk->nr_freqs < 1) |
5aefa34f | 149 | return -ENOSYS; |
c2590f4a | 150 | |
f586903d PM |
151 | return clk_rate_round_helper(&table_round); |
152 | } | |
153 | ||
8e122db6 PM |
154 | static long clk_rate_div_range_iter(unsigned int pos, |
155 | struct clk_rate_round_data *rounder) | |
156 | { | |
157 | return clk_get_rate(rounder->arg) / pos; | |
158 | } | |
159 | ||
160 | long clk_rate_div_range_round(struct clk *clk, unsigned int div_min, | |
161 | unsigned int div_max, unsigned long rate) | |
162 | { | |
163 | struct clk_rate_round_data div_range_round = { | |
164 | .min = div_min, | |
165 | .max = div_max, | |
166 | .func = clk_rate_div_range_iter, | |
167 | .arg = clk_get_parent(clk), | |
168 | .rate = rate, | |
169 | }; | |
170 | ||
171 | return clk_rate_round_helper(&div_range_round); | |
172 | } | |
173 | ||
8b5ee113 MD |
174 | int clk_rate_table_find(struct clk *clk, |
175 | struct cpufreq_frequency_table *freq_table, | |
176 | unsigned long rate) | |
177 | { | |
178 | int i; | |
179 | ||
180 | for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) { | |
181 | unsigned long freq = freq_table[i].frequency; | |
182 | ||
183 | if (freq == CPUFREQ_ENTRY_INVALID) | |
184 | continue; | |
185 | ||
186 | if (freq == rate) | |
187 | return i; | |
188 | } | |
189 | ||
190 | return -ENOENT; | |
191 | } | |
192 | ||
193 | /* Used for clocks that always have same value as the parent clock */ | |
194 | unsigned long followparent_recalc(struct clk *clk) | |
195 | { | |
196 | return clk->parent ? clk->parent->rate : 0; | |
197 | } | |
198 | ||
199 | int clk_reparent(struct clk *child, struct clk *parent) | |
200 | { | |
201 | list_del_init(&child->sibling); | |
202 | if (parent) | |
203 | list_add(&child->sibling, &parent->children); | |
204 | child->parent = parent; | |
205 | ||
206 | /* now do the debugfs renaming to reattach the child | |
207 | to the proper parent */ | |
208 | ||
209 | return 0; | |
210 | } | |
211 | ||
212 | /* Propagate rate to children */ | |
213 | void propagate_rate(struct clk *tclk) | |
214 | { | |
215 | struct clk *clkp; | |
216 | ||
217 | list_for_each_entry(clkp, &tclk->children, sibling) { | |
218 | if (clkp->ops && clkp->ops->recalc) | |
219 | clkp->rate = clkp->ops->recalc(clkp); | |
220 | ||
221 | propagate_rate(clkp); | |
222 | } | |
223 | } | |
224 | ||
225 | static void __clk_disable(struct clk *clk) | |
226 | { | |
69395396 MD |
227 | if (WARN(!clk->usecount, "Trying to disable clock %p with 0 usecount\n", |
228 | clk)) | |
8b5ee113 | 229 | return; |
8b5ee113 MD |
230 | |
231 | if (!(--clk->usecount)) { | |
232 | if (likely(clk->ops && clk->ops->disable)) | |
233 | clk->ops->disable(clk); | |
234 | if (likely(clk->parent)) | |
235 | __clk_disable(clk->parent); | |
236 | } | |
237 | } | |
238 | ||
239 | void clk_disable(struct clk *clk) | |
240 | { | |
241 | unsigned long flags; | |
242 | ||
243 | if (!clk) | |
244 | return; | |
245 | ||
246 | spin_lock_irqsave(&clock_lock, flags); | |
247 | __clk_disable(clk); | |
248 | spin_unlock_irqrestore(&clock_lock, flags); | |
249 | } | |
250 | EXPORT_SYMBOL_GPL(clk_disable); | |
251 | ||
252 | static int __clk_enable(struct clk *clk) | |
253 | { | |
254 | int ret = 0; | |
255 | ||
256 | if (clk->usecount++ == 0) { | |
257 | if (clk->parent) { | |
258 | ret = __clk_enable(clk->parent); | |
259 | if (unlikely(ret)) | |
260 | goto err; | |
261 | } | |
262 | ||
263 | if (clk->ops && clk->ops->enable) { | |
264 | ret = clk->ops->enable(clk); | |
265 | if (ret) { | |
266 | if (clk->parent) | |
267 | __clk_disable(clk->parent); | |
268 | goto err; | |
269 | } | |
270 | } | |
271 | } | |
272 | ||
273 | return ret; | |
274 | err: | |
275 | clk->usecount--; | |
276 | return ret; | |
277 | } | |
278 | ||
279 | int clk_enable(struct clk *clk) | |
280 | { | |
281 | unsigned long flags; | |
282 | int ret; | |
283 | ||
284 | if (!clk) | |
285 | return -EINVAL; | |
286 | ||
287 | spin_lock_irqsave(&clock_lock, flags); | |
288 | ret = __clk_enable(clk); | |
289 | spin_unlock_irqrestore(&clock_lock, flags); | |
290 | ||
291 | return ret; | |
292 | } | |
293 | EXPORT_SYMBOL_GPL(clk_enable); | |
294 | ||
295 | static LIST_HEAD(root_clks); | |
296 | ||
297 | /** | |
298 | * recalculate_root_clocks - recalculate and propagate all root clocks | |
299 | * | |
300 | * Recalculates all root clocks (clocks with no parent), which if the | |
301 | * clock's .recalc is set correctly, should also propagate their rates. | |
302 | * Called at init. | |
303 | */ | |
304 | void recalculate_root_clocks(void) | |
305 | { | |
306 | struct clk *clkp; | |
307 | ||
308 | list_for_each_entry(clkp, &root_clks, sibling) { | |
309 | if (clkp->ops && clkp->ops->recalc) | |
310 | clkp->rate = clkp->ops->recalc(clkp); | |
311 | propagate_rate(clkp); | |
312 | } | |
313 | } | |
314 | ||
28085bc5 PM |
315 | static struct clk_mapping dummy_mapping; |
316 | ||
317 | static struct clk *lookup_root_clock(struct clk *clk) | |
318 | { | |
319 | while (clk->parent) | |
320 | clk = clk->parent; | |
321 | ||
322 | return clk; | |
323 | } | |
324 | ||
325 | static int clk_establish_mapping(struct clk *clk) | |
326 | { | |
327 | struct clk_mapping *mapping = clk->mapping; | |
328 | ||
329 | /* | |
330 | * Propagate mappings. | |
331 | */ | |
332 | if (!mapping) { | |
333 | struct clk *clkp; | |
334 | ||
335 | /* | |
336 | * dummy mapping for root clocks with no specified ranges | |
337 | */ | |
338 | if (!clk->parent) { | |
339 | clk->mapping = &dummy_mapping; | |
340 | return 0; | |
341 | } | |
342 | ||
343 | /* | |
344 | * If we're on a child clock and it provides no mapping of its | |
345 | * own, inherit the mapping from its root clock. | |
346 | */ | |
347 | clkp = lookup_root_clock(clk); | |
348 | mapping = clkp->mapping; | |
349 | BUG_ON(!mapping); | |
350 | } | |
351 | ||
352 | /* | |
353 | * Establish initial mapping. | |
354 | */ | |
355 | if (!mapping->base && mapping->phys) { | |
356 | kref_init(&mapping->ref); | |
357 | ||
358 | mapping->base = ioremap_nocache(mapping->phys, mapping->len); | |
359 | if (unlikely(!mapping->base)) | |
360 | return -ENXIO; | |
361 | } else if (mapping->base) { | |
362 | /* | |
363 | * Bump the refcount for an existing mapping | |
364 | */ | |
365 | kref_get(&mapping->ref); | |
366 | } | |
367 | ||
368 | clk->mapping = mapping; | |
369 | return 0; | |
370 | } | |
371 | ||
372 | static void clk_destroy_mapping(struct kref *kref) | |
373 | { | |
374 | struct clk_mapping *mapping; | |
375 | ||
376 | mapping = container_of(kref, struct clk_mapping, ref); | |
377 | ||
378 | iounmap(mapping->base); | |
379 | } | |
380 | ||
381 | static void clk_teardown_mapping(struct clk *clk) | |
382 | { | |
383 | struct clk_mapping *mapping = clk->mapping; | |
384 | ||
385 | /* Nothing to do */ | |
386 | if (mapping == &dummy_mapping) | |
387 | return; | |
388 | ||
389 | kref_put(&mapping->ref, clk_destroy_mapping); | |
390 | clk->mapping = NULL; | |
391 | } | |
392 | ||
8b5ee113 MD |
393 | int clk_register(struct clk *clk) |
394 | { | |
28085bc5 PM |
395 | int ret; |
396 | ||
8b5ee113 MD |
397 | if (clk == NULL || IS_ERR(clk)) |
398 | return -EINVAL; | |
399 | ||
400 | /* | |
401 | * trap out already registered clocks | |
402 | */ | |
403 | if (clk->node.next || clk->node.prev) | |
404 | return 0; | |
405 | ||
406 | mutex_lock(&clock_list_sem); | |
407 | ||
408 | INIT_LIST_HEAD(&clk->children); | |
409 | clk->usecount = 0; | |
410 | ||
28085bc5 PM |
411 | ret = clk_establish_mapping(clk); |
412 | if (unlikely(ret)) | |
413 | goto out_unlock; | |
414 | ||
8b5ee113 MD |
415 | if (clk->parent) |
416 | list_add(&clk->sibling, &clk->parent->children); | |
417 | else | |
418 | list_add(&clk->sibling, &root_clks); | |
419 | ||
420 | list_add(&clk->node, &clock_list); | |
421 | if (clk->ops && clk->ops->init) | |
422 | clk->ops->init(clk); | |
28085bc5 PM |
423 | |
424 | out_unlock: | |
8b5ee113 MD |
425 | mutex_unlock(&clock_list_sem); |
426 | ||
28085bc5 | 427 | return ret; |
8b5ee113 MD |
428 | } |
429 | EXPORT_SYMBOL_GPL(clk_register); | |
430 | ||
431 | void clk_unregister(struct clk *clk) | |
432 | { | |
433 | mutex_lock(&clock_list_sem); | |
434 | list_del(&clk->sibling); | |
435 | list_del(&clk->node); | |
28085bc5 | 436 | clk_teardown_mapping(clk); |
8b5ee113 MD |
437 | mutex_unlock(&clock_list_sem); |
438 | } | |
439 | EXPORT_SYMBOL_GPL(clk_unregister); | |
440 | ||
441 | void clk_enable_init_clocks(void) | |
442 | { | |
443 | struct clk *clkp; | |
444 | ||
445 | list_for_each_entry(clkp, &clock_list, node) | |
446 | if (clkp->flags & CLK_ENABLE_ON_INIT) | |
447 | clk_enable(clkp); | |
448 | } | |
449 | ||
450 | unsigned long clk_get_rate(struct clk *clk) | |
451 | { | |
452 | return clk->rate; | |
453 | } | |
454 | EXPORT_SYMBOL_GPL(clk_get_rate); | |
455 | ||
456 | int clk_set_rate(struct clk *clk, unsigned long rate) | |
8b5ee113 MD |
457 | { |
458 | int ret = -EOPNOTSUPP; | |
459 | unsigned long flags; | |
460 | ||
461 | spin_lock_irqsave(&clock_lock, flags); | |
462 | ||
463 | if (likely(clk->ops && clk->ops->set_rate)) { | |
35a96c73 | 464 | ret = clk->ops->set_rate(clk, rate); |
8b5ee113 MD |
465 | if (ret != 0) |
466 | goto out_unlock; | |
467 | } else { | |
468 | clk->rate = rate; | |
469 | ret = 0; | |
470 | } | |
471 | ||
472 | if (clk->ops && clk->ops->recalc) | |
473 | clk->rate = clk->ops->recalc(clk); | |
474 | ||
475 | propagate_rate(clk); | |
476 | ||
477 | out_unlock: | |
478 | spin_unlock_irqrestore(&clock_lock, flags); | |
479 | ||
480 | return ret; | |
481 | } | |
9a1683d1 | 482 | EXPORT_SYMBOL_GPL(clk_set_rate); |
8b5ee113 MD |
483 | |
484 | int clk_set_parent(struct clk *clk, struct clk *parent) | |
485 | { | |
486 | unsigned long flags; | |
487 | int ret = -EINVAL; | |
488 | ||
489 | if (!parent || !clk) | |
490 | return ret; | |
491 | if (clk->parent == parent) | |
492 | return 0; | |
493 | ||
494 | spin_lock_irqsave(&clock_lock, flags); | |
495 | if (clk->usecount == 0) { | |
496 | if (clk->ops->set_parent) | |
497 | ret = clk->ops->set_parent(clk, parent); | |
498 | else | |
499 | ret = clk_reparent(clk, parent); | |
500 | ||
501 | if (ret == 0) { | |
8b5ee113 MD |
502 | if (clk->ops->recalc) |
503 | clk->rate = clk->ops->recalc(clk); | |
550a1ef1 | 504 | pr_debug("set parent of %p to %p (new rate %ld)\n", |
69395396 | 505 | clk, clk->parent, clk->rate); |
8b5ee113 MD |
506 | propagate_rate(clk); |
507 | } | |
508 | } else | |
509 | ret = -EBUSY; | |
510 | spin_unlock_irqrestore(&clock_lock, flags); | |
511 | ||
512 | return ret; | |
513 | } | |
514 | EXPORT_SYMBOL_GPL(clk_set_parent); | |
515 | ||
516 | struct clk *clk_get_parent(struct clk *clk) | |
517 | { | |
518 | return clk->parent; | |
519 | } | |
520 | EXPORT_SYMBOL_GPL(clk_get_parent); | |
521 | ||
522 | long clk_round_rate(struct clk *clk, unsigned long rate) | |
523 | { | |
524 | if (likely(clk->ops && clk->ops->round_rate)) { | |
525 | unsigned long flags, rounded; | |
526 | ||
527 | spin_lock_irqsave(&clock_lock, flags); | |
528 | rounded = clk->ops->round_rate(clk, rate); | |
529 | spin_unlock_irqrestore(&clock_lock, flags); | |
530 | ||
531 | return rounded; | |
532 | } | |
533 | ||
534 | return clk_get_rate(clk); | |
535 | } | |
536 | EXPORT_SYMBOL_GPL(clk_round_rate); | |
537 | ||
6af26c6c GL |
538 | long clk_round_parent(struct clk *clk, unsigned long target, |
539 | unsigned long *best_freq, unsigned long *parent_freq, | |
540 | unsigned int div_min, unsigned int div_max) | |
541 | { | |
542 | struct cpufreq_frequency_table *freq, *best = NULL; | |
543 | unsigned long error = ULONG_MAX, freq_high, freq_low, div; | |
544 | struct clk *parent = clk_get_parent(clk); | |
545 | ||
546 | if (!parent) { | |
547 | *parent_freq = 0; | |
548 | *best_freq = clk_round_rate(clk, target); | |
549 | return abs(target - *best_freq); | |
550 | } | |
551 | ||
552 | for (freq = parent->freq_table; freq->frequency != CPUFREQ_TABLE_END; | |
553 | freq++) { | |
554 | if (freq->frequency == CPUFREQ_ENTRY_INVALID) | |
555 | continue; | |
556 | ||
557 | if (unlikely(freq->frequency / target <= div_min - 1)) { | |
a766b297 PM |
558 | unsigned long freq_max; |
559 | ||
560 | freq_max = (freq->frequency + div_min / 2) / div_min; | |
6af26c6c GL |
561 | if (error > target - freq_max) { |
562 | error = target - freq_max; | |
563 | best = freq; | |
564 | if (best_freq) | |
565 | *best_freq = freq_max; | |
566 | } | |
a766b297 | 567 | |
ed10b490 | 568 | pr_debug("too low freq %u, error %lu\n", freq->frequency, |
a766b297 PM |
569 | target - freq_max); |
570 | ||
6af26c6c GL |
571 | if (!error) |
572 | break; | |
a766b297 | 573 | |
6af26c6c GL |
574 | continue; |
575 | } | |
576 | ||
577 | if (unlikely(freq->frequency / target >= div_max)) { | |
a766b297 PM |
578 | unsigned long freq_min; |
579 | ||
580 | freq_min = (freq->frequency + div_max / 2) / div_max; | |
6af26c6c GL |
581 | if (error > freq_min - target) { |
582 | error = freq_min - target; | |
583 | best = freq; | |
584 | if (best_freq) | |
585 | *best_freq = freq_min; | |
586 | } | |
a766b297 | 587 | |
ed10b490 | 588 | pr_debug("too high freq %u, error %lu\n", freq->frequency, |
a766b297 PM |
589 | freq_min - target); |
590 | ||
6af26c6c GL |
591 | if (!error) |
592 | break; | |
a766b297 | 593 | |
6af26c6c GL |
594 | continue; |
595 | } | |
596 | ||
6af26c6c GL |
597 | div = freq->frequency / target; |
598 | freq_high = freq->frequency / div; | |
599 | freq_low = freq->frequency / (div + 1); | |
a766b297 | 600 | |
6af26c6c GL |
601 | if (freq_high - target < error) { |
602 | error = freq_high - target; | |
603 | best = freq; | |
604 | if (best_freq) | |
605 | *best_freq = freq_high; | |
606 | } | |
a766b297 | 607 | |
6af26c6c GL |
608 | if (target - freq_low < error) { |
609 | error = target - freq_low; | |
610 | best = freq; | |
611 | if (best_freq) | |
612 | *best_freq = freq_low; | |
613 | } | |
a766b297 | 614 | |
6af26c6c GL |
615 | pr_debug("%u / %lu = %lu, / %lu = %lu, best %lu, parent %u\n", |
616 | freq->frequency, div, freq_high, div + 1, freq_low, | |
617 | *best_freq, best->frequency); | |
a766b297 | 618 | |
6af26c6c GL |
619 | if (!error) |
620 | break; | |
621 | } | |
a766b297 | 622 | |
6af26c6c GL |
623 | if (parent_freq) |
624 | *parent_freq = best->frequency; | |
a766b297 | 625 | |
6af26c6c GL |
626 | return error; |
627 | } | |
628 | EXPORT_SYMBOL_GPL(clk_round_parent); | |
629 | ||
8b5ee113 MD |
630 | #ifdef CONFIG_PM |
631 | static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state) | |
632 | { | |
633 | static pm_message_t prev_state; | |
634 | struct clk *clkp; | |
635 | ||
636 | switch (state.event) { | |
637 | case PM_EVENT_ON: | |
638 | /* Resumeing from hibernation */ | |
639 | if (prev_state.event != PM_EVENT_FREEZE) | |
640 | break; | |
641 | ||
642 | list_for_each_entry(clkp, &clock_list, node) { | |
643 | if (likely(clkp->ops)) { | |
644 | unsigned long rate = clkp->rate; | |
645 | ||
646 | if (likely(clkp->ops->set_parent)) | |
647 | clkp->ops->set_parent(clkp, | |
648 | clkp->parent); | |
649 | if (likely(clkp->ops->set_rate)) | |
35a96c73 | 650 | clkp->ops->set_rate(clkp, rate); |
8b5ee113 MD |
651 | else if (likely(clkp->ops->recalc)) |
652 | clkp->rate = clkp->ops->recalc(clkp); | |
653 | } | |
654 | } | |
655 | break; | |
656 | case PM_EVENT_FREEZE: | |
657 | break; | |
658 | case PM_EVENT_SUSPEND: | |
659 | break; | |
660 | } | |
661 | ||
662 | prev_state = state; | |
663 | return 0; | |
664 | } | |
665 | ||
666 | static int clks_sysdev_resume(struct sys_device *dev) | |
667 | { | |
668 | return clks_sysdev_suspend(dev, PMSG_ON); | |
669 | } | |
670 | ||
671 | static struct sysdev_class clks_sysdev_class = { | |
672 | .name = "clks", | |
673 | }; | |
674 | ||
675 | static struct sysdev_driver clks_sysdev_driver = { | |
676 | .suspend = clks_sysdev_suspend, | |
677 | .resume = clks_sysdev_resume, | |
678 | }; | |
679 | ||
680 | static struct sys_device clks_sysdev_dev = { | |
681 | .cls = &clks_sysdev_class, | |
682 | }; | |
683 | ||
684 | static int __init clk_sysdev_init(void) | |
685 | { | |
686 | sysdev_class_register(&clks_sysdev_class); | |
687 | sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver); | |
688 | sysdev_register(&clks_sysdev_dev); | |
689 | ||
690 | return 0; | |
691 | } | |
692 | subsys_initcall(clk_sysdev_init); | |
693 | #endif | |
694 | ||
695 | /* | |
696 | * debugfs support to trace clock tree hierarchy and attributes | |
697 | */ | |
698 | static struct dentry *clk_debugfs_root; | |
699 | ||
700 | static int clk_debugfs_register_one(struct clk *c) | |
701 | { | |
702 | int err; | |
703 | struct dentry *d, *child, *child_tmp; | |
704 | struct clk *pa = c->parent; | |
705 | char s[255]; | |
706 | char *p = s; | |
707 | ||
69395396 | 708 | p += sprintf(p, "%p", c); |
8b5ee113 MD |
709 | d = debugfs_create_dir(s, pa ? pa->dentry : clk_debugfs_root); |
710 | if (!d) | |
711 | return -ENOMEM; | |
712 | c->dentry = d; | |
713 | ||
714 | d = debugfs_create_u8("usecount", S_IRUGO, c->dentry, (u8 *)&c->usecount); | |
715 | if (!d) { | |
716 | err = -ENOMEM; | |
717 | goto err_out; | |
718 | } | |
719 | d = debugfs_create_u32("rate", S_IRUGO, c->dentry, (u32 *)&c->rate); | |
720 | if (!d) { | |
721 | err = -ENOMEM; | |
722 | goto err_out; | |
723 | } | |
724 | d = debugfs_create_x32("flags", S_IRUGO, c->dentry, (u32 *)&c->flags); | |
725 | if (!d) { | |
726 | err = -ENOMEM; | |
727 | goto err_out; | |
728 | } | |
729 | return 0; | |
730 | ||
731 | err_out: | |
732 | d = c->dentry; | |
733 | list_for_each_entry_safe(child, child_tmp, &d->d_subdirs, d_u.d_child) | |
734 | debugfs_remove(child); | |
735 | debugfs_remove(c->dentry); | |
736 | return err; | |
737 | } | |
738 | ||
739 | static int clk_debugfs_register(struct clk *c) | |
740 | { | |
741 | int err; | |
742 | struct clk *pa = c->parent; | |
743 | ||
744 | if (pa && !pa->dentry) { | |
745 | err = clk_debugfs_register(pa); | |
746 | if (err) | |
747 | return err; | |
748 | } | |
749 | ||
69395396 | 750 | if (!c->dentry) { |
8b5ee113 MD |
751 | err = clk_debugfs_register_one(c); |
752 | if (err) | |
753 | return err; | |
754 | } | |
755 | return 0; | |
756 | } | |
757 | ||
758 | static int __init clk_debugfs_init(void) | |
759 | { | |
760 | struct clk *c; | |
761 | struct dentry *d; | |
762 | int err; | |
763 | ||
764 | d = debugfs_create_dir("clock", NULL); | |
765 | if (!d) | |
766 | return -ENOMEM; | |
767 | clk_debugfs_root = d; | |
768 | ||
769 | list_for_each_entry(c, &clock_list, node) { | |
770 | err = clk_debugfs_register(c); | |
771 | if (err) | |
772 | goto err_out; | |
773 | } | |
774 | return 0; | |
775 | err_out: | |
776 | debugfs_remove_recursive(clk_debugfs_root); | |
777 | return err; | |
778 | } | |
779 | late_initcall(clk_debugfs_init); |