blob: 813d97cdad49ad45cb2320acfec5bbf39489fb7a [file] [log] [blame]
Magnus Damm8b5ee112010-05-11 13:29:25 +00001/*
2 * drivers/sh/clk.c - SuperH clock framework
3 *
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>
24#include <linux/kobject.h>
25#include <linux/sysdev.h>
26#include <linux/seq_file.h>
27#include <linux/err.h>
Paul Mundt28085bc2010-10-15 16:46:37 +090028#include <linux/io.h>
Magnus Damm8b5ee112010-05-11 13:29:25 +000029#include <linux/debugfs.h>
30#include <linux/cpufreq.h>
31#include <linux/clk.h>
32#include <linux/sh_clk.h>
33
34static LIST_HEAD(clock_list);
35static DEFINE_SPINLOCK(clock_lock);
36static DEFINE_MUTEX(clock_list_sem);
37
38void 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
48 for (i = 0; i < nr_freqs; i++) {
49 div = 1;
50 mult = 1;
51
52 if (src_table->divisors && i < src_table->nr_divisors)
53 div = src_table->divisors[i];
54
55 if (src_table->multipliers && i < src_table->nr_multipliers)
56 mult = src_table->multipliers[i];
57
58 if (!div || !mult || (bitmap && !test_bit(i, bitmap)))
59 freq = CPUFREQ_ENTRY_INVALID;
60 else
61 freq = clk->parent->rate * mult / div;
62
63 freq_table[i].index = i;
64 freq_table[i].frequency = freq;
65 }
66
67 /* Termination entry */
68 freq_table[i].index = i;
69 freq_table[i].frequency = CPUFREQ_TABLE_END;
70}
71
72long clk_rate_table_round(struct clk *clk,
73 struct cpufreq_frequency_table *freq_table,
74 unsigned long rate)
75{
76 unsigned long rate_error, rate_error_prev = ~0UL;
77 unsigned long rate_best_fit = rate;
Paul Mundt960bc362010-08-20 19:10:38 +090078 unsigned long highest, lowest;
Magnus Damm8b5ee112010-05-11 13:29:25 +000079 int i;
80
Paul Mundt960bc362010-08-20 19:10:38 +090081 highest = 0;
82 lowest = ~0UL;
83
Magnus Damm8b5ee112010-05-11 13:29:25 +000084 for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
85 unsigned long freq = freq_table[i].frequency;
86
87 if (freq == CPUFREQ_ENTRY_INVALID)
88 continue;
89
Paul Mundt960bc362010-08-20 19:10:38 +090090 if (freq > highest)
91 highest = freq;
92 if (freq < lowest)
93 lowest = freq;
94
Magnus Damm8b5ee112010-05-11 13:29:25 +000095 rate_error = abs(freq - rate);
96 if (rate_error < rate_error_prev) {
97 rate_best_fit = freq;
98 rate_error_prev = rate_error;
99 }
100
101 if (rate_error == 0)
102 break;
103 }
104
Paul Mundt960bc362010-08-20 19:10:38 +0900105 if (rate >= highest)
106 rate_best_fit = highest;
107 if (rate <= lowest)
108 rate_best_fit = lowest;
109
Magnus Damm8b5ee112010-05-11 13:29:25 +0000110 return rate_best_fit;
111}
112
113int clk_rate_table_find(struct clk *clk,
114 struct cpufreq_frequency_table *freq_table,
115 unsigned long rate)
116{
117 int i;
118
119 for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
120 unsigned long freq = freq_table[i].frequency;
121
122 if (freq == CPUFREQ_ENTRY_INVALID)
123 continue;
124
125 if (freq == rate)
126 return i;
127 }
128
129 return -ENOENT;
130}
131
132/* Used for clocks that always have same value as the parent clock */
133unsigned long followparent_recalc(struct clk *clk)
134{
135 return clk->parent ? clk->parent->rate : 0;
136}
137
138int clk_reparent(struct clk *child, struct clk *parent)
139{
140 list_del_init(&child->sibling);
141 if (parent)
142 list_add(&child->sibling, &parent->children);
143 child->parent = parent;
144
145 /* now do the debugfs renaming to reattach the child
146 to the proper parent */
147
148 return 0;
149}
150
151/* Propagate rate to children */
152void propagate_rate(struct clk *tclk)
153{
154 struct clk *clkp;
155
156 list_for_each_entry(clkp, &tclk->children, sibling) {
157 if (clkp->ops && clkp->ops->recalc)
158 clkp->rate = clkp->ops->recalc(clkp);
159
160 propagate_rate(clkp);
161 }
162}
163
164static void __clk_disable(struct clk *clk)
165{
Magnus Damm69395392010-10-13 07:44:36 +0000166 if (WARN(!clk->usecount, "Trying to disable clock %p with 0 usecount\n",
167 clk))
Magnus Damm8b5ee112010-05-11 13:29:25 +0000168 return;
Magnus Damm8b5ee112010-05-11 13:29:25 +0000169
170 if (!(--clk->usecount)) {
171 if (likely(clk->ops && clk->ops->disable))
172 clk->ops->disable(clk);
173 if (likely(clk->parent))
174 __clk_disable(clk->parent);
175 }
176}
177
178void clk_disable(struct clk *clk)
179{
180 unsigned long flags;
181
182 if (!clk)
183 return;
184
185 spin_lock_irqsave(&clock_lock, flags);
186 __clk_disable(clk);
187 spin_unlock_irqrestore(&clock_lock, flags);
188}
189EXPORT_SYMBOL_GPL(clk_disable);
190
191static int __clk_enable(struct clk *clk)
192{
193 int ret = 0;
194
195 if (clk->usecount++ == 0) {
196 if (clk->parent) {
197 ret = __clk_enable(clk->parent);
198 if (unlikely(ret))
199 goto err;
200 }
201
202 if (clk->ops && clk->ops->enable) {
203 ret = clk->ops->enable(clk);
204 if (ret) {
205 if (clk->parent)
206 __clk_disable(clk->parent);
207 goto err;
208 }
209 }
210 }
211
212 return ret;
213err:
214 clk->usecount--;
215 return ret;
216}
217
218int clk_enable(struct clk *clk)
219{
220 unsigned long flags;
221 int ret;
222
223 if (!clk)
224 return -EINVAL;
225
226 spin_lock_irqsave(&clock_lock, flags);
227 ret = __clk_enable(clk);
228 spin_unlock_irqrestore(&clock_lock, flags);
229
230 return ret;
231}
232EXPORT_SYMBOL_GPL(clk_enable);
233
234static LIST_HEAD(root_clks);
235
236/**
237 * recalculate_root_clocks - recalculate and propagate all root clocks
238 *
239 * Recalculates all root clocks (clocks with no parent), which if the
240 * clock's .recalc is set correctly, should also propagate their rates.
241 * Called at init.
242 */
243void recalculate_root_clocks(void)
244{
245 struct clk *clkp;
246
247 list_for_each_entry(clkp, &root_clks, sibling) {
248 if (clkp->ops && clkp->ops->recalc)
249 clkp->rate = clkp->ops->recalc(clkp);
250 propagate_rate(clkp);
251 }
252}
253
Paul Mundt28085bc2010-10-15 16:46:37 +0900254static struct clk_mapping dummy_mapping;
255
256static struct clk *lookup_root_clock(struct clk *clk)
257{
258 while (clk->parent)
259 clk = clk->parent;
260
261 return clk;
262}
263
264static int clk_establish_mapping(struct clk *clk)
265{
266 struct clk_mapping *mapping = clk->mapping;
267
268 /*
269 * Propagate mappings.
270 */
271 if (!mapping) {
272 struct clk *clkp;
273
274 /*
275 * dummy mapping for root clocks with no specified ranges
276 */
277 if (!clk->parent) {
278 clk->mapping = &dummy_mapping;
279 return 0;
280 }
281
282 /*
283 * If we're on a child clock and it provides no mapping of its
284 * own, inherit the mapping from its root clock.
285 */
286 clkp = lookup_root_clock(clk);
287 mapping = clkp->mapping;
288 BUG_ON(!mapping);
289 }
290
291 /*
292 * Establish initial mapping.
293 */
294 if (!mapping->base && mapping->phys) {
295 kref_init(&mapping->ref);
296
297 mapping->base = ioremap_nocache(mapping->phys, mapping->len);
298 if (unlikely(!mapping->base))
299 return -ENXIO;
300 } else if (mapping->base) {
301 /*
302 * Bump the refcount for an existing mapping
303 */
304 kref_get(&mapping->ref);
305 }
306
307 clk->mapping = mapping;
308 return 0;
309}
310
311static void clk_destroy_mapping(struct kref *kref)
312{
313 struct clk_mapping *mapping;
314
315 mapping = container_of(kref, struct clk_mapping, ref);
316
317 iounmap(mapping->base);
318}
319
320static void clk_teardown_mapping(struct clk *clk)
321{
322 struct clk_mapping *mapping = clk->mapping;
323
324 /* Nothing to do */
325 if (mapping == &dummy_mapping)
326 return;
327
328 kref_put(&mapping->ref, clk_destroy_mapping);
329 clk->mapping = NULL;
330}
331
Magnus Damm8b5ee112010-05-11 13:29:25 +0000332int clk_register(struct clk *clk)
333{
Paul Mundt28085bc2010-10-15 16:46:37 +0900334 int ret;
335
Magnus Damm8b5ee112010-05-11 13:29:25 +0000336 if (clk == NULL || IS_ERR(clk))
337 return -EINVAL;
338
339 /*
340 * trap out already registered clocks
341 */
342 if (clk->node.next || clk->node.prev)
343 return 0;
344
345 mutex_lock(&clock_list_sem);
346
347 INIT_LIST_HEAD(&clk->children);
348 clk->usecount = 0;
349
Paul Mundt28085bc2010-10-15 16:46:37 +0900350 ret = clk_establish_mapping(clk);
351 if (unlikely(ret))
352 goto out_unlock;
353
Magnus Damm8b5ee112010-05-11 13:29:25 +0000354 if (clk->parent)
355 list_add(&clk->sibling, &clk->parent->children);
356 else
357 list_add(&clk->sibling, &root_clks);
358
359 list_add(&clk->node, &clock_list);
360 if (clk->ops && clk->ops->init)
361 clk->ops->init(clk);
Paul Mundt28085bc2010-10-15 16:46:37 +0900362
363out_unlock:
Magnus Damm8b5ee112010-05-11 13:29:25 +0000364 mutex_unlock(&clock_list_sem);
365
Paul Mundt28085bc2010-10-15 16:46:37 +0900366 return ret;
Magnus Damm8b5ee112010-05-11 13:29:25 +0000367}
368EXPORT_SYMBOL_GPL(clk_register);
369
370void clk_unregister(struct clk *clk)
371{
372 mutex_lock(&clock_list_sem);
373 list_del(&clk->sibling);
374 list_del(&clk->node);
Paul Mundt28085bc2010-10-15 16:46:37 +0900375 clk_teardown_mapping(clk);
Magnus Damm8b5ee112010-05-11 13:29:25 +0000376 mutex_unlock(&clock_list_sem);
377}
378EXPORT_SYMBOL_GPL(clk_unregister);
379
380void clk_enable_init_clocks(void)
381{
382 struct clk *clkp;
383
384 list_for_each_entry(clkp, &clock_list, node)
385 if (clkp->flags & CLK_ENABLE_ON_INIT)
386 clk_enable(clkp);
387}
388
389unsigned long clk_get_rate(struct clk *clk)
390{
391 return clk->rate;
392}
393EXPORT_SYMBOL_GPL(clk_get_rate);
394
395int clk_set_rate(struct clk *clk, unsigned long rate)
396{
397 return clk_set_rate_ex(clk, rate, 0);
398}
399EXPORT_SYMBOL_GPL(clk_set_rate);
400
401int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
402{
403 int ret = -EOPNOTSUPP;
404 unsigned long flags;
405
406 spin_lock_irqsave(&clock_lock, flags);
407
408 if (likely(clk->ops && clk->ops->set_rate)) {
409 ret = clk->ops->set_rate(clk, rate, algo_id);
410 if (ret != 0)
411 goto out_unlock;
412 } else {
413 clk->rate = rate;
414 ret = 0;
415 }
416
417 if (clk->ops && clk->ops->recalc)
418 clk->rate = clk->ops->recalc(clk);
419
420 propagate_rate(clk);
421
422out_unlock:
423 spin_unlock_irqrestore(&clock_lock, flags);
424
425 return ret;
426}
427EXPORT_SYMBOL_GPL(clk_set_rate_ex);
428
429int clk_set_parent(struct clk *clk, struct clk *parent)
430{
431 unsigned long flags;
432 int ret = -EINVAL;
433
434 if (!parent || !clk)
435 return ret;
436 if (clk->parent == parent)
437 return 0;
438
439 spin_lock_irqsave(&clock_lock, flags);
440 if (clk->usecount == 0) {
441 if (clk->ops->set_parent)
442 ret = clk->ops->set_parent(clk, parent);
443 else
444 ret = clk_reparent(clk, parent);
445
446 if (ret == 0) {
Magnus Damm8b5ee112010-05-11 13:29:25 +0000447 if (clk->ops->recalc)
448 clk->rate = clk->ops->recalc(clk);
Paul Mundt550a1ef2010-10-13 19:24:55 +0900449 pr_debug("set parent of %p to %p (new rate %ld)\n",
Magnus Damm69395392010-10-13 07:44:36 +0000450 clk, clk->parent, clk->rate);
Magnus Damm8b5ee112010-05-11 13:29:25 +0000451 propagate_rate(clk);
452 }
453 } else
454 ret = -EBUSY;
455 spin_unlock_irqrestore(&clock_lock, flags);
456
457 return ret;
458}
459EXPORT_SYMBOL_GPL(clk_set_parent);
460
461struct clk *clk_get_parent(struct clk *clk)
462{
463 return clk->parent;
464}
465EXPORT_SYMBOL_GPL(clk_get_parent);
466
467long clk_round_rate(struct clk *clk, unsigned long rate)
468{
469 if (likely(clk->ops && clk->ops->round_rate)) {
470 unsigned long flags, rounded;
471
472 spin_lock_irqsave(&clock_lock, flags);
473 rounded = clk->ops->round_rate(clk, rate);
474 spin_unlock_irqrestore(&clock_lock, flags);
475
476 return rounded;
477 }
478
479 return clk_get_rate(clk);
480}
481EXPORT_SYMBOL_GPL(clk_round_rate);
482
483#ifdef CONFIG_PM
484static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
485{
486 static pm_message_t prev_state;
487 struct clk *clkp;
488
489 switch (state.event) {
490 case PM_EVENT_ON:
491 /* Resumeing from hibernation */
492 if (prev_state.event != PM_EVENT_FREEZE)
493 break;
494
495 list_for_each_entry(clkp, &clock_list, node) {
496 if (likely(clkp->ops)) {
497 unsigned long rate = clkp->rate;
498
499 if (likely(clkp->ops->set_parent))
500 clkp->ops->set_parent(clkp,
501 clkp->parent);
502 if (likely(clkp->ops->set_rate))
503 clkp->ops->set_rate(clkp,
504 rate, NO_CHANGE);
505 else if (likely(clkp->ops->recalc))
506 clkp->rate = clkp->ops->recalc(clkp);
507 }
508 }
509 break;
510 case PM_EVENT_FREEZE:
511 break;
512 case PM_EVENT_SUSPEND:
513 break;
514 }
515
516 prev_state = state;
517 return 0;
518}
519
520static int clks_sysdev_resume(struct sys_device *dev)
521{
522 return clks_sysdev_suspend(dev, PMSG_ON);
523}
524
525static struct sysdev_class clks_sysdev_class = {
526 .name = "clks",
527};
528
529static struct sysdev_driver clks_sysdev_driver = {
530 .suspend = clks_sysdev_suspend,
531 .resume = clks_sysdev_resume,
532};
533
534static struct sys_device clks_sysdev_dev = {
535 .cls = &clks_sysdev_class,
536};
537
538static int __init clk_sysdev_init(void)
539{
540 sysdev_class_register(&clks_sysdev_class);
541 sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
542 sysdev_register(&clks_sysdev_dev);
543
544 return 0;
545}
546subsys_initcall(clk_sysdev_init);
547#endif
548
549/*
550 * debugfs support to trace clock tree hierarchy and attributes
551 */
552static struct dentry *clk_debugfs_root;
553
554static int clk_debugfs_register_one(struct clk *c)
555{
556 int err;
557 struct dentry *d, *child, *child_tmp;
558 struct clk *pa = c->parent;
559 char s[255];
560 char *p = s;
561
Magnus Damm69395392010-10-13 07:44:36 +0000562 p += sprintf(p, "%p", c);
Magnus Damm8b5ee112010-05-11 13:29:25 +0000563 d = debugfs_create_dir(s, pa ? pa->dentry : clk_debugfs_root);
564 if (!d)
565 return -ENOMEM;
566 c->dentry = d;
567
568 d = debugfs_create_u8("usecount", S_IRUGO, c->dentry, (u8 *)&c->usecount);
569 if (!d) {
570 err = -ENOMEM;
571 goto err_out;
572 }
573 d = debugfs_create_u32("rate", S_IRUGO, c->dentry, (u32 *)&c->rate);
574 if (!d) {
575 err = -ENOMEM;
576 goto err_out;
577 }
578 d = debugfs_create_x32("flags", S_IRUGO, c->dentry, (u32 *)&c->flags);
579 if (!d) {
580 err = -ENOMEM;
581 goto err_out;
582 }
583 return 0;
584
585err_out:
586 d = c->dentry;
587 list_for_each_entry_safe(child, child_tmp, &d->d_subdirs, d_u.d_child)
588 debugfs_remove(child);
589 debugfs_remove(c->dentry);
590 return err;
591}
592
593static int clk_debugfs_register(struct clk *c)
594{
595 int err;
596 struct clk *pa = c->parent;
597
598 if (pa && !pa->dentry) {
599 err = clk_debugfs_register(pa);
600 if (err)
601 return err;
602 }
603
Magnus Damm69395392010-10-13 07:44:36 +0000604 if (!c->dentry) {
Magnus Damm8b5ee112010-05-11 13:29:25 +0000605 err = clk_debugfs_register_one(c);
606 if (err)
607 return err;
608 }
609 return 0;
610}
611
612static int __init clk_debugfs_init(void)
613{
614 struct clk *c;
615 struct dentry *d;
616 int err;
617
618 d = debugfs_create_dir("clock", NULL);
619 if (!d)
620 return -ENOMEM;
621 clk_debugfs_root = d;
622
623 list_for_each_entry(c, &clock_list, node) {
624 err = clk_debugfs_register(c);
625 if (err)
626 goto err_out;
627 }
628 return 0;
629err_out:
630 debugfs_remove_recursive(clk_debugfs_root);
631 return err;
632}
633late_initcall(clk_debugfs_init);