blob: 30ad6f04afb05e6fee5977aaea207ba75c7be2ec [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 */
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/module.h>
20#include <linux/mutex.h>
21#include <linux/list.h>
22#include <linux/kobject.h>
23#include <linux/sysdev.h>
24#include <linux/seq_file.h>
25#include <linux/err.h>
26#include <linux/platform_device.h>
27#include <linux/debugfs.h>
28#include <linux/cpufreq.h>
29#include <linux/clk.h>
30#include <linux/sh_clk.h>
31
32static LIST_HEAD(clock_list);
33static DEFINE_SPINLOCK(clock_lock);
34static DEFINE_MUTEX(clock_list_sem);
35
36void clk_rate_table_build(struct clk *clk,
37 struct cpufreq_frequency_table *freq_table,
38 int nr_freqs,
39 struct clk_div_mult_table *src_table,
40 unsigned long *bitmap)
41{
42 unsigned long mult, div;
43 unsigned long freq;
44 int i;
45
46 for (i = 0; i < nr_freqs; i++) {
47 div = 1;
48 mult = 1;
49
50 if (src_table->divisors && i < src_table->nr_divisors)
51 div = src_table->divisors[i];
52
53 if (src_table->multipliers && i < src_table->nr_multipliers)
54 mult = src_table->multipliers[i];
55
56 if (!div || !mult || (bitmap && !test_bit(i, bitmap)))
57 freq = CPUFREQ_ENTRY_INVALID;
58 else
59 freq = clk->parent->rate * mult / div;
60
61 freq_table[i].index = i;
62 freq_table[i].frequency = freq;
63 }
64
65 /* Termination entry */
66 freq_table[i].index = i;
67 freq_table[i].frequency = CPUFREQ_TABLE_END;
68}
69
70long clk_rate_table_round(struct clk *clk,
71 struct cpufreq_frequency_table *freq_table,
72 unsigned long rate)
73{
74 unsigned long rate_error, rate_error_prev = ~0UL;
75 unsigned long rate_best_fit = rate;
Paul Mundt960bc362010-08-20 19:10:38 +090076 unsigned long highest, lowest;
Magnus Damm8b5ee112010-05-11 13:29:25 +000077 int i;
78
Paul Mundt960bc362010-08-20 19:10:38 +090079 highest = 0;
80 lowest = ~0UL;
81
Magnus Damm8b5ee112010-05-11 13:29:25 +000082 for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
83 unsigned long freq = freq_table[i].frequency;
84
85 if (freq == CPUFREQ_ENTRY_INVALID)
86 continue;
87
Paul Mundt960bc362010-08-20 19:10:38 +090088 if (freq > highest)
89 highest = freq;
90 if (freq < lowest)
91 lowest = freq;
92
Magnus Damm8b5ee112010-05-11 13:29:25 +000093 rate_error = abs(freq - rate);
94 if (rate_error < rate_error_prev) {
95 rate_best_fit = freq;
96 rate_error_prev = rate_error;
97 }
98
99 if (rate_error == 0)
100 break;
101 }
102
Paul Mundt960bc362010-08-20 19:10:38 +0900103 if (rate >= highest)
104 rate_best_fit = highest;
105 if (rate <= lowest)
106 rate_best_fit = lowest;
107
Magnus Damm8b5ee112010-05-11 13:29:25 +0000108 return rate_best_fit;
109}
110
111int clk_rate_table_find(struct clk *clk,
112 struct cpufreq_frequency_table *freq_table,
113 unsigned long rate)
114{
115 int i;
116
117 for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
118 unsigned long freq = freq_table[i].frequency;
119
120 if (freq == CPUFREQ_ENTRY_INVALID)
121 continue;
122
123 if (freq == rate)
124 return i;
125 }
126
127 return -ENOENT;
128}
129
130/* Used for clocks that always have same value as the parent clock */
131unsigned long followparent_recalc(struct clk *clk)
132{
133 return clk->parent ? clk->parent->rate : 0;
134}
135
136int clk_reparent(struct clk *child, struct clk *parent)
137{
138 list_del_init(&child->sibling);
139 if (parent)
140 list_add(&child->sibling, &parent->children);
141 child->parent = parent;
142
143 /* now do the debugfs renaming to reattach the child
144 to the proper parent */
145
146 return 0;
147}
148
149/* Propagate rate to children */
150void propagate_rate(struct clk *tclk)
151{
152 struct clk *clkp;
153
154 list_for_each_entry(clkp, &tclk->children, sibling) {
155 if (clkp->ops && clkp->ops->recalc)
156 clkp->rate = clkp->ops->recalc(clkp);
157
158 propagate_rate(clkp);
159 }
160}
161
162static void __clk_disable(struct clk *clk)
163{
Magnus Damm69395392010-10-13 07:44:36 +0000164 if (WARN(!clk->usecount, "Trying to disable clock %p with 0 usecount\n",
165 clk))
Magnus Damm8b5ee112010-05-11 13:29:25 +0000166 return;
Magnus Damm8b5ee112010-05-11 13:29:25 +0000167
168 if (!(--clk->usecount)) {
169 if (likely(clk->ops && clk->ops->disable))
170 clk->ops->disable(clk);
171 if (likely(clk->parent))
172 __clk_disable(clk->parent);
173 }
174}
175
176void clk_disable(struct clk *clk)
177{
178 unsigned long flags;
179
180 if (!clk)
181 return;
182
183 spin_lock_irqsave(&clock_lock, flags);
184 __clk_disable(clk);
185 spin_unlock_irqrestore(&clock_lock, flags);
186}
187EXPORT_SYMBOL_GPL(clk_disable);
188
189static int __clk_enable(struct clk *clk)
190{
191 int ret = 0;
192
193 if (clk->usecount++ == 0) {
194 if (clk->parent) {
195 ret = __clk_enable(clk->parent);
196 if (unlikely(ret))
197 goto err;
198 }
199
200 if (clk->ops && clk->ops->enable) {
201 ret = clk->ops->enable(clk);
202 if (ret) {
203 if (clk->parent)
204 __clk_disable(clk->parent);
205 goto err;
206 }
207 }
208 }
209
210 return ret;
211err:
212 clk->usecount--;
213 return ret;
214}
215
216int clk_enable(struct clk *clk)
217{
218 unsigned long flags;
219 int ret;
220
221 if (!clk)
222 return -EINVAL;
223
224 spin_lock_irqsave(&clock_lock, flags);
225 ret = __clk_enable(clk);
226 spin_unlock_irqrestore(&clock_lock, flags);
227
228 return ret;
229}
230EXPORT_SYMBOL_GPL(clk_enable);
231
232static LIST_HEAD(root_clks);
233
234/**
235 * recalculate_root_clocks - recalculate and propagate all root clocks
236 *
237 * Recalculates all root clocks (clocks with no parent), which if the
238 * clock's .recalc is set correctly, should also propagate their rates.
239 * Called at init.
240 */
241void recalculate_root_clocks(void)
242{
243 struct clk *clkp;
244
245 list_for_each_entry(clkp, &root_clks, sibling) {
246 if (clkp->ops && clkp->ops->recalc)
247 clkp->rate = clkp->ops->recalc(clkp);
248 propagate_rate(clkp);
249 }
250}
251
252int clk_register(struct clk *clk)
253{
254 if (clk == NULL || IS_ERR(clk))
255 return -EINVAL;
256
257 /*
258 * trap out already registered clocks
259 */
260 if (clk->node.next || clk->node.prev)
261 return 0;
262
263 mutex_lock(&clock_list_sem);
264
265 INIT_LIST_HEAD(&clk->children);
266 clk->usecount = 0;
267
268 if (clk->parent)
269 list_add(&clk->sibling, &clk->parent->children);
270 else
271 list_add(&clk->sibling, &root_clks);
272
273 list_add(&clk->node, &clock_list);
274 if (clk->ops && clk->ops->init)
275 clk->ops->init(clk);
276 mutex_unlock(&clock_list_sem);
277
278 return 0;
279}
280EXPORT_SYMBOL_GPL(clk_register);
281
282void clk_unregister(struct clk *clk)
283{
284 mutex_lock(&clock_list_sem);
285 list_del(&clk->sibling);
286 list_del(&clk->node);
287 mutex_unlock(&clock_list_sem);
288}
289EXPORT_SYMBOL_GPL(clk_unregister);
290
291void clk_enable_init_clocks(void)
292{
293 struct clk *clkp;
294
295 list_for_each_entry(clkp, &clock_list, node)
296 if (clkp->flags & CLK_ENABLE_ON_INIT)
297 clk_enable(clkp);
298}
299
300unsigned long clk_get_rate(struct clk *clk)
301{
302 return clk->rate;
303}
304EXPORT_SYMBOL_GPL(clk_get_rate);
305
306int clk_set_rate(struct clk *clk, unsigned long rate)
307{
308 return clk_set_rate_ex(clk, rate, 0);
309}
310EXPORT_SYMBOL_GPL(clk_set_rate);
311
312int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
313{
314 int ret = -EOPNOTSUPP;
315 unsigned long flags;
316
317 spin_lock_irqsave(&clock_lock, flags);
318
319 if (likely(clk->ops && clk->ops->set_rate)) {
320 ret = clk->ops->set_rate(clk, rate, algo_id);
321 if (ret != 0)
322 goto out_unlock;
323 } else {
324 clk->rate = rate;
325 ret = 0;
326 }
327
328 if (clk->ops && clk->ops->recalc)
329 clk->rate = clk->ops->recalc(clk);
330
331 propagate_rate(clk);
332
333out_unlock:
334 spin_unlock_irqrestore(&clock_lock, flags);
335
336 return ret;
337}
338EXPORT_SYMBOL_GPL(clk_set_rate_ex);
339
340int clk_set_parent(struct clk *clk, struct clk *parent)
341{
342 unsigned long flags;
343 int ret = -EINVAL;
344
345 if (!parent || !clk)
346 return ret;
347 if (clk->parent == parent)
348 return 0;
349
350 spin_lock_irqsave(&clock_lock, flags);
351 if (clk->usecount == 0) {
352 if (clk->ops->set_parent)
353 ret = clk->ops->set_parent(clk, parent);
354 else
355 ret = clk_reparent(clk, parent);
356
357 if (ret == 0) {
Magnus Damm8b5ee112010-05-11 13:29:25 +0000358 if (clk->ops->recalc)
359 clk->rate = clk->ops->recalc(clk);
Magnus Damm69395392010-10-13 07:44:36 +0000360 pr_debug("clock: set parent of %p to %p (new rate %ld)\n",
361 clk, clk->parent, clk->rate);
Magnus Damm8b5ee112010-05-11 13:29:25 +0000362 propagate_rate(clk);
363 }
364 } else
365 ret = -EBUSY;
366 spin_unlock_irqrestore(&clock_lock, flags);
367
368 return ret;
369}
370EXPORT_SYMBOL_GPL(clk_set_parent);
371
372struct clk *clk_get_parent(struct clk *clk)
373{
374 return clk->parent;
375}
376EXPORT_SYMBOL_GPL(clk_get_parent);
377
378long clk_round_rate(struct clk *clk, unsigned long rate)
379{
380 if (likely(clk->ops && clk->ops->round_rate)) {
381 unsigned long flags, rounded;
382
383 spin_lock_irqsave(&clock_lock, flags);
384 rounded = clk->ops->round_rate(clk, rate);
385 spin_unlock_irqrestore(&clock_lock, flags);
386
387 return rounded;
388 }
389
390 return clk_get_rate(clk);
391}
392EXPORT_SYMBOL_GPL(clk_round_rate);
393
394#ifdef CONFIG_PM
395static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
396{
397 static pm_message_t prev_state;
398 struct clk *clkp;
399
400 switch (state.event) {
401 case PM_EVENT_ON:
402 /* Resumeing from hibernation */
403 if (prev_state.event != PM_EVENT_FREEZE)
404 break;
405
406 list_for_each_entry(clkp, &clock_list, node) {
407 if (likely(clkp->ops)) {
408 unsigned long rate = clkp->rate;
409
410 if (likely(clkp->ops->set_parent))
411 clkp->ops->set_parent(clkp,
412 clkp->parent);
413 if (likely(clkp->ops->set_rate))
414 clkp->ops->set_rate(clkp,
415 rate, NO_CHANGE);
416 else if (likely(clkp->ops->recalc))
417 clkp->rate = clkp->ops->recalc(clkp);
418 }
419 }
420 break;
421 case PM_EVENT_FREEZE:
422 break;
423 case PM_EVENT_SUSPEND:
424 break;
425 }
426
427 prev_state = state;
428 return 0;
429}
430
431static int clks_sysdev_resume(struct sys_device *dev)
432{
433 return clks_sysdev_suspend(dev, PMSG_ON);
434}
435
436static struct sysdev_class clks_sysdev_class = {
437 .name = "clks",
438};
439
440static struct sysdev_driver clks_sysdev_driver = {
441 .suspend = clks_sysdev_suspend,
442 .resume = clks_sysdev_resume,
443};
444
445static struct sys_device clks_sysdev_dev = {
446 .cls = &clks_sysdev_class,
447};
448
449static int __init clk_sysdev_init(void)
450{
451 sysdev_class_register(&clks_sysdev_class);
452 sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
453 sysdev_register(&clks_sysdev_dev);
454
455 return 0;
456}
457subsys_initcall(clk_sysdev_init);
458#endif
459
460/*
461 * debugfs support to trace clock tree hierarchy and attributes
462 */
463static struct dentry *clk_debugfs_root;
464
465static int clk_debugfs_register_one(struct clk *c)
466{
467 int err;
468 struct dentry *d, *child, *child_tmp;
469 struct clk *pa = c->parent;
470 char s[255];
471 char *p = s;
472
Magnus Damm69395392010-10-13 07:44:36 +0000473 p += sprintf(p, "%p", c);
Magnus Damm8b5ee112010-05-11 13:29:25 +0000474 d = debugfs_create_dir(s, pa ? pa->dentry : clk_debugfs_root);
475 if (!d)
476 return -ENOMEM;
477 c->dentry = d;
478
479 d = debugfs_create_u8("usecount", S_IRUGO, c->dentry, (u8 *)&c->usecount);
480 if (!d) {
481 err = -ENOMEM;
482 goto err_out;
483 }
484 d = debugfs_create_u32("rate", S_IRUGO, c->dentry, (u32 *)&c->rate);
485 if (!d) {
486 err = -ENOMEM;
487 goto err_out;
488 }
489 d = debugfs_create_x32("flags", S_IRUGO, c->dentry, (u32 *)&c->flags);
490 if (!d) {
491 err = -ENOMEM;
492 goto err_out;
493 }
494 return 0;
495
496err_out:
497 d = c->dentry;
498 list_for_each_entry_safe(child, child_tmp, &d->d_subdirs, d_u.d_child)
499 debugfs_remove(child);
500 debugfs_remove(c->dentry);
501 return err;
502}
503
504static int clk_debugfs_register(struct clk *c)
505{
506 int err;
507 struct clk *pa = c->parent;
508
509 if (pa && !pa->dentry) {
510 err = clk_debugfs_register(pa);
511 if (err)
512 return err;
513 }
514
Magnus Damm69395392010-10-13 07:44:36 +0000515 if (!c->dentry) {
Magnus Damm8b5ee112010-05-11 13:29:25 +0000516 err = clk_debugfs_register_one(c);
517 if (err)
518 return err;
519 }
520 return 0;
521}
522
523static int __init clk_debugfs_init(void)
524{
525 struct clk *c;
526 struct dentry *d;
527 int err;
528
529 d = debugfs_create_dir("clock", NULL);
530 if (!d)
531 return -ENOMEM;
532 clk_debugfs_root = d;
533
534 list_for_each_entry(c, &clock_list, node) {
535 err = clk_debugfs_register(c);
536 if (err)
537 goto err_out;
538 }
539 return 0;
540err_out:
541 debugfs_remove_recursive(clk_debugfs_root);
542 return err;
543}
544late_initcall(clk_debugfs_init);