blob: 9ded1bc29260fc029cd3f9f119704f65177c00ee [file] [log] [blame]
Paul Mundt36ddf312006-01-16 22:14:17 -08001/*
2 * arch/sh/kernel/cpu/clock.c - SuperH clock framework
3 *
Paul Mundtb1f6cfe2009-05-12 04:27:43 +09004 * Copyright (C) 2005 - 2009 Paul Mundt
Paul Mundt36ddf312006-01-16 22:14:17 -08005 *
6 * This clock framework is derived from the OMAP version by:
7 *
Paul Mundtb1f6cfe2009-05-12 04:27:43 +09008 * Copyright (C) 2004 - 2008 Nokia Corporation
Paul Mundt36ddf312006-01-16 22:14:17 -08009 * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
10 *
Paul Mundt1d118562006-12-01 13:15:14 +090011 * Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
12 *
Paul Mundt36ddf312006-01-16 22:14:17 -080013 * 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>
Paul Mundt237b98f2006-09-27 17:28:20 +090020#include <linux/mutex.h>
Paul Mundt36ddf312006-01-16 22:14:17 -080021#include <linux/list.h>
Francesco VIRLINZI4a550262009-03-11 07:42:05 +000022#include <linux/kobject.h>
23#include <linux/sysdev.h>
Paul Mundt36ddf312006-01-16 22:14:17 -080024#include <linux/seq_file.h>
25#include <linux/err.h>
Paul Mundt1d118562006-12-01 13:15:14 +090026#include <linux/platform_device.h>
Paul Mundtcedcf332009-05-13 21:51:28 +090027#include <linux/debugfs.h>
Magnus Dammc94a8572009-05-25 08:10:28 +000028#include <linux/cpufreq.h>
Paul Mundt51a50062010-03-08 21:45:19 +090029#include <linux/clk.h>
Paul Mundt36ddf312006-01-16 22:14:17 -080030#include <asm/clock.h>
Paul Mundt253b0882009-05-13 17:38:11 +090031#include <asm/machvec.h>
Paul Mundt36ddf312006-01-16 22:14:17 -080032
33static LIST_HEAD(clock_list);
34static DEFINE_SPINLOCK(clock_lock);
Paul Mundt237b98f2006-09-27 17:28:20 +090035static DEFINE_MUTEX(clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -080036
Magnus Dammc94a8572009-05-25 08:10:28 +000037void 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
47 for (i = 0; i < nr_freqs; i++) {
48 div = 1;
49 mult = 1;
50
51 if (src_table->divisors && i < src_table->nr_divisors)
52 div = src_table->divisors[i];
53
54 if (src_table->multipliers && i < src_table->nr_multipliers)
55 mult = src_table->multipliers[i];
56
57 if (!div || !mult || (bitmap && !test_bit(i, bitmap)))
58 freq = CPUFREQ_ENTRY_INVALID;
59 else
60 freq = clk->parent->rate * mult / div;
61
62 freq_table[i].index = i;
63 freq_table[i].frequency = freq;
64 }
65
66 /* Termination entry */
67 freq_table[i].index = i;
68 freq_table[i].frequency = CPUFREQ_TABLE_END;
69}
70
71long clk_rate_table_round(struct clk *clk,
72 struct cpufreq_frequency_table *freq_table,
73 unsigned long rate)
74{
75 unsigned long rate_error, rate_error_prev = ~0UL;
76 unsigned long rate_best_fit = rate;
77 unsigned long highest, lowest;
78 int i;
79
80 highest = lowest = 0;
81
82 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
88 if (freq > highest)
89 highest = freq;
90 if (freq < lowest)
91 lowest = freq;
92
93 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
103 if (rate >= highest)
104 rate_best_fit = highest;
105 if (rate <= lowest)
106 rate_best_fit = lowest;
107
108 return rate_best_fit;
109}
110
Magnus Damm098dee92009-06-04 05:31:41 +0000111int 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
Paul Mundta02cb232009-05-12 03:50:44 +0900130/* Used for clocks that always have same value as the parent clock */
131unsigned long followparent_recalc(struct clk *clk)
132{
Paul Mundt549b5e32009-05-14 17:38:46 +0900133 return clk->parent ? clk->parent->rate : 0;
Paul Mundta02cb232009-05-12 03:50:44 +0900134}
135
Paul Mundtaa87aa32009-05-12 05:51:05 +0900136int 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
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900149/* 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) {
Paul Mundtd672fef2009-05-13 17:03:09 +0900155 if (clkp->ops && clkp->ops->recalc)
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900156 clkp->rate = clkp->ops->recalc(clkp);
Paul Mundtcc96eac2009-05-13 20:28:15 +0900157
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900158 propagate_rate(clkp);
159 }
160}
161
Adrian Bunk4c1cfab2008-06-18 03:36:50 +0300162static void __clk_disable(struct clk *clk)
Paul Mundt36ddf312006-01-16 22:14:17 -0800163{
Paul Mundtae891a42009-05-12 05:30:10 +0900164 if (clk->usecount == 0) {
165 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
166 clk->name);
167 WARN_ON(1);
168 return;
169 }
170
171 if (!(--clk->usecount)) {
dmitry pervushin1929cb32007-04-24 13:39:09 +0900172 if (likely(clk->ops && clk->ops->disable))
173 clk->ops->disable(clk);
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900174 if (likely(clk->parent))
175 __clk_disable(clk->parent);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900176 }
Paul Mundt36ddf312006-01-16 22:14:17 -0800177}
178
179void clk_disable(struct clk *clk)
180{
181 unsigned long flags;
182
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900183 if (!clk)
184 return;
185
Paul Mundt36ddf312006-01-16 22:14:17 -0800186 spin_lock_irqsave(&clock_lock, flags);
187 __clk_disable(clk);
188 spin_unlock_irqrestore(&clock_lock, flags);
189}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900190EXPORT_SYMBOL_GPL(clk_disable);
Paul Mundt36ddf312006-01-16 22:14:17 -0800191
Paul Mundtae891a42009-05-12 05:30:10 +0900192static int __clk_enable(struct clk *clk)
193{
194 int ret = 0;
195
196 if (clk->usecount++ == 0) {
197 if (clk->parent) {
198 ret = __clk_enable(clk->parent);
199 if (unlikely(ret))
200 goto err;
201 }
202
203 if (clk->ops && clk->ops->enable) {
204 ret = clk->ops->enable(clk);
205 if (ret) {
206 if (clk->parent)
207 __clk_disable(clk->parent);
208 goto err;
209 }
210 }
211 }
212
213 return ret;
214err:
215 clk->usecount--;
216 return ret;
217}
218
219int clk_enable(struct clk *clk)
220{
221 unsigned long flags;
222 int ret;
223
224 if (!clk)
225 return -EINVAL;
226
227 spin_lock_irqsave(&clock_lock, flags);
228 ret = __clk_enable(clk);
229 spin_unlock_irqrestore(&clock_lock, flags);
230
231 return ret;
232}
233EXPORT_SYMBOL_GPL(clk_enable);
234
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900235static LIST_HEAD(root_clks);
236
237/**
238 * recalculate_root_clocks - recalculate and propagate all root clocks
239 *
240 * Recalculates all root clocks (clocks with no parent), which if the
241 * clock's .recalc is set correctly, should also propagate their rates.
242 * Called at init.
243 */
244void recalculate_root_clocks(void)
245{
246 struct clk *clkp;
247
248 list_for_each_entry(clkp, &root_clks, sibling) {
Paul Mundtd672fef2009-05-13 17:03:09 +0900249 if (clkp->ops && clkp->ops->recalc)
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900250 clkp->rate = clkp->ops->recalc(clkp);
251 propagate_rate(clkp);
252 }
253}
254
Paul Mundt36ddf312006-01-16 22:14:17 -0800255int clk_register(struct clk *clk)
256{
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900257 if (clk == NULL || IS_ERR(clk))
258 return -EINVAL;
259
260 /*
261 * trap out already registered clocks
262 */
263 if (clk->node.next || clk->node.prev)
264 return 0;
265
Paul Mundt237b98f2006-09-27 17:28:20 +0900266 mutex_lock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800267
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900268 INIT_LIST_HEAD(&clk->children);
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900269 clk->usecount = 0;
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900270
271 if (clk->parent)
272 list_add(&clk->sibling, &clk->parent->children);
273 else
274 list_add(&clk->sibling, &root_clks);
275
Paul Mundt36ddf312006-01-16 22:14:17 -0800276 list_add(&clk->node, &clock_list);
Paul Mundtd672fef2009-05-13 17:03:09 +0900277 if (clk->ops && clk->ops->init)
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900278 clk->ops->init(clk);
Paul Mundt237b98f2006-09-27 17:28:20 +0900279 mutex_unlock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800280
281 return 0;
282}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900283EXPORT_SYMBOL_GPL(clk_register);
Paul Mundt36ddf312006-01-16 22:14:17 -0800284
285void clk_unregister(struct clk *clk)
286{
Paul Mundt237b98f2006-09-27 17:28:20 +0900287 mutex_lock(&clock_list_sem);
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900288 list_del(&clk->sibling);
Paul Mundt36ddf312006-01-16 22:14:17 -0800289 list_del(&clk->node);
Paul Mundt237b98f2006-09-27 17:28:20 +0900290 mutex_unlock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800291}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900292EXPORT_SYMBOL_GPL(clk_unregister);
Paul Mundt36ddf312006-01-16 22:14:17 -0800293
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900294static void clk_enable_init_clocks(void)
295{
296 struct clk *clkp;
297
298 list_for_each_entry(clkp, &clock_list, node)
299 if (clkp->flags & CLK_ENABLE_ON_INIT)
300 clk_enable(clkp);
301}
302
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900303unsigned long clk_get_rate(struct clk *clk)
Paul Mundt36ddf312006-01-16 22:14:17 -0800304{
305 return clk->rate;
306}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900307EXPORT_SYMBOL_GPL(clk_get_rate);
Paul Mundt36ddf312006-01-16 22:14:17 -0800308
309int clk_set_rate(struct clk *clk, unsigned long rate)
310{
dmitry pervushin1929cb32007-04-24 13:39:09 +0900311 return clk_set_rate_ex(clk, rate, 0);
312}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900313EXPORT_SYMBOL_GPL(clk_set_rate);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900314
315int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
316{
Paul Mundt36ddf312006-01-16 22:14:17 -0800317 int ret = -EOPNOTSUPP;
Paul Mundt100890c2009-05-13 17:05:51 +0900318 unsigned long flags;
319
320 spin_lock_irqsave(&clock_lock, flags);
Paul Mundt36ddf312006-01-16 22:14:17 -0800321
322 if (likely(clk->ops && clk->ops->set_rate)) {
dmitry pervushin1929cb32007-04-24 13:39:09 +0900323 ret = clk->ops->set_rate(clk, rate, algo_id);
Paul Mundt100890c2009-05-13 17:05:51 +0900324 if (ret != 0)
325 goto out_unlock;
326 } else {
327 clk->rate = rate;
328 ret = 0;
Paul Mundt36ddf312006-01-16 22:14:17 -0800329 }
330
Paul Mundt100890c2009-05-13 17:05:51 +0900331 if (clk->ops && clk->ops->recalc)
332 clk->rate = clk->ops->recalc(clk);
333
334 propagate_rate(clk);
335
336out_unlock:
337 spin_unlock_irqrestore(&clock_lock, flags);
338
Paul Mundt36ddf312006-01-16 22:14:17 -0800339 return ret;
340}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900341EXPORT_SYMBOL_GPL(clk_set_rate_ex);
Paul Mundt36ddf312006-01-16 22:14:17 -0800342
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000343int clk_set_parent(struct clk *clk, struct clk *parent)
344{
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900345 unsigned long flags;
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000346 int ret = -EINVAL;
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000347
348 if (!parent || !clk)
349 return ret;
Paul Mundtaa87aa32009-05-12 05:51:05 +0900350 if (clk->parent == parent)
351 return 0;
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000352
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900353 spin_lock_irqsave(&clock_lock, flags);
354 if (clk->usecount == 0) {
355 if (clk->ops->set_parent)
356 ret = clk->ops->set_parent(clk, parent);
Paul Mundtaa87aa32009-05-12 05:51:05 +0900357 else
358 ret = clk_reparent(clk, parent);
359
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900360 if (ret == 0) {
Paul Mundtaa87aa32009-05-12 05:51:05 +0900361 pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
362 clk->name, clk->parent->name, clk->rate);
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900363 if (clk->ops->recalc)
364 clk->rate = clk->ops->recalc(clk);
365 propagate_rate(clk);
366 }
367 } else
368 ret = -EBUSY;
369 spin_unlock_irqrestore(&clock_lock, flags);
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000370
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000371 return ret;
372}
373EXPORT_SYMBOL_GPL(clk_set_parent);
374
375struct clk *clk_get_parent(struct clk *clk)
376{
377 return clk->parent;
378}
379EXPORT_SYMBOL_GPL(clk_get_parent);
380
Paul Mundtf6991b02007-07-20 13:29:09 +0900381long clk_round_rate(struct clk *clk, unsigned long rate)
382{
383 if (likely(clk->ops && clk->ops->round_rate)) {
384 unsigned long flags, rounded;
385
386 spin_lock_irqsave(&clock_lock, flags);
387 rounded = clk->ops->round_rate(clk, rate);
388 spin_unlock_irqrestore(&clock_lock, flags);
389
390 return rounded;
391 }
392
393 return clk_get_rate(clk);
394}
395EXPORT_SYMBOL_GPL(clk_round_rate);
396
Paul Mundt1d118562006-12-01 13:15:14 +0900397/*
398 * Returns a clock. Note that we first try to use device id on the bus
399 * and clock name. If this fails, we try to use clock name only.
400 */
401struct clk *clk_get(struct device *dev, const char *id)
Paul Mundt36ddf312006-01-16 22:14:17 -0800402{
Paul Mundt0dae8952009-05-12 06:18:09 +0900403 const char *dev_id = dev ? dev_name(dev) : NULL;
Paul Mundt36ddf312006-01-16 22:14:17 -0800404 struct clk *p, *clk = ERR_PTR(-ENOENT);
Paul Mundt1d118562006-12-01 13:15:14 +0900405 int idno;
406
Paul Mundt0dae8952009-05-12 06:18:09 +0900407 clk = clk_get_sys(dev_id, id);
Paul Mundtf3f82902009-05-12 16:07:40 +0900408 if (clk && !IS_ERR(clk))
Paul Mundt0dae8952009-05-12 06:18:09 +0900409 return clk;
410
Paul Mundt1d118562006-12-01 13:15:14 +0900411 if (dev == NULL || dev->bus != &platform_bus_type)
412 idno = -1;
413 else
414 idno = to_platform_device(dev)->id;
Paul Mundt36ddf312006-01-16 22:14:17 -0800415
Paul Mundt237b98f2006-09-27 17:28:20 +0900416 mutex_lock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800417 list_for_each_entry(p, &clock_list, node) {
Paul Mundt1d118562006-12-01 13:15:14 +0900418 if (p->id == idno &&
419 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
420 clk = p;
421 goto found;
422 }
423 }
424
425 list_for_each_entry(p, &clock_list, node) {
Paul Mundt36ddf312006-01-16 22:14:17 -0800426 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
427 clk = p;
428 break;
429 }
430 }
Paul Mundt1d118562006-12-01 13:15:14 +0900431
432found:
Paul Mundt237b98f2006-09-27 17:28:20 +0900433 mutex_unlock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800434
435 return clk;
436}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900437EXPORT_SYMBOL_GPL(clk_get);
Paul Mundt36ddf312006-01-16 22:14:17 -0800438
439void clk_put(struct clk *clk)
440{
441 if (clk && !IS_ERR(clk))
442 module_put(clk->owner);
443}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900444EXPORT_SYMBOL_GPL(clk_put);
Paul Mundt36ddf312006-01-16 22:14:17 -0800445
Francesco VIRLINZI4a550262009-03-11 07:42:05 +0000446#ifdef CONFIG_PM
447static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
448{
449 static pm_message_t prev_state;
450 struct clk *clkp;
451
452 switch (state.event) {
453 case PM_EVENT_ON:
454 /* Resumeing from hibernation */
Paul Mundtb68d8202009-05-12 03:45:08 +0900455 if (prev_state.event != PM_EVENT_FREEZE)
456 break;
Francesco VIRLINZI50cca712009-03-13 08:08:01 +0000457
Paul Mundtb68d8202009-05-12 03:45:08 +0900458 list_for_each_entry(clkp, &clock_list, node) {
459 if (likely(clkp->ops)) {
460 unsigned long rate = clkp->rate;
461
462 if (likely(clkp->ops->set_parent))
463 clkp->ops->set_parent(clkp,
464 clkp->parent);
465 if (likely(clkp->ops->set_rate))
466 clkp->ops->set_rate(clkp,
467 rate, NO_CHANGE);
468 else if (likely(clkp->ops->recalc))
469 clkp->rate = clkp->ops->recalc(clkp);
470 }
Francesco VIRLINZI4a550262009-03-11 07:42:05 +0000471 }
472 break;
473 case PM_EVENT_FREEZE:
474 break;
475 case PM_EVENT_SUSPEND:
476 break;
477 }
478
479 prev_state = state;
480 return 0;
481}
482
483static int clks_sysdev_resume(struct sys_device *dev)
484{
485 return clks_sysdev_suspend(dev, PMSG_ON);
486}
487
488static struct sysdev_class clks_sysdev_class = {
489 .name = "clks",
490};
491
492static struct sysdev_driver clks_sysdev_driver = {
493 .suspend = clks_sysdev_suspend,
494 .resume = clks_sysdev_resume,
495};
496
497static struct sys_device clks_sysdev_dev = {
498 .cls = &clks_sysdev_class,
499};
500
501static int __init clk_sysdev_init(void)
502{
503 sysdev_class_register(&clks_sysdev_class);
504 sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
505 sysdev_register(&clks_sysdev_dev);
506
507 return 0;
508}
509subsys_initcall(clk_sysdev_init);
510#endif
511
Paul Mundt36ddf312006-01-16 22:14:17 -0800512int __init clk_init(void)
513{
Paul Mundt253b0882009-05-13 17:38:11 +0900514 int ret;
Paul Mundt36ddf312006-01-16 22:14:17 -0800515
Paul Mundt253b0882009-05-13 17:38:11 +0900516 ret = arch_clk_init();
517 if (unlikely(ret)) {
518 pr_err("%s: CPU clock registration failed.\n", __func__);
519 return ret;
Paul Mundt36ddf312006-01-16 22:14:17 -0800520 }
521
Paul Mundt253b0882009-05-13 17:38:11 +0900522 if (sh_mv.mv_clk_init) {
523 ret = sh_mv.mv_clk_init();
524 if (unlikely(ret)) {
525 pr_err("%s: machvec clock initialization failed.\n",
526 __func__);
527 return ret;
528 }
529 }
dmitry pervushindfbbbe92007-05-15 08:42:22 +0900530
Paul Mundt36ddf312006-01-16 22:14:17 -0800531 /* Kick the child clocks.. */
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900532 recalculate_root_clocks();
Paul Mundt36ddf312006-01-16 22:14:17 -0800533
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900534 /* Enable the necessary init clocks */
535 clk_enable_init_clocks();
536
Paul Mundt36ddf312006-01-16 22:14:17 -0800537 return ret;
538}
539
Paul Mundtcedcf332009-05-13 21:51:28 +0900540/*
541 * debugfs support to trace clock tree hierarchy and attributes
542 */
543static struct dentry *clk_debugfs_root;
Paul Mundt36ddf312006-01-16 22:14:17 -0800544
Paul Mundtcedcf332009-05-13 21:51:28 +0900545static int clk_debugfs_register_one(struct clk *c)
546{
547 int err;
Marek Skuczynskibc10e872010-01-30 22:29:32 +0100548 struct dentry *d, *child, *child_tmp;
Paul Mundtcedcf332009-05-13 21:51:28 +0900549 struct clk *pa = c->parent;
550 char s[255];
551 char *p = s;
552
553 p += sprintf(p, "%s", c->name);
Paul Mundt549b5e32009-05-14 17:38:46 +0900554 if (c->id >= 0)
Paul Mundtcedcf332009-05-13 21:51:28 +0900555 sprintf(p, ":%d", c->id);
556 d = debugfs_create_dir(s, pa ? pa->dentry : clk_debugfs_root);
557 if (!d)
558 return -ENOMEM;
559 c->dentry = d;
560
561 d = debugfs_create_u8("usecount", S_IRUGO, c->dentry, (u8 *)&c->usecount);
562 if (!d) {
563 err = -ENOMEM;
564 goto err_out;
565 }
566 d = debugfs_create_u32("rate", S_IRUGO, c->dentry, (u32 *)&c->rate);
567 if (!d) {
568 err = -ENOMEM;
569 goto err_out;
570 }
571 d = debugfs_create_x32("flags", S_IRUGO, c->dentry, (u32 *)&c->flags);
572 if (!d) {
573 err = -ENOMEM;
574 goto err_out;
575 }
576 return 0;
577
578err_out:
579 d = c->dentry;
Marek Skuczynskibc10e872010-01-30 22:29:32 +0100580 list_for_each_entry_safe(child, child_tmp, &d->d_subdirs, d_u.d_child)
Paul Mundtcedcf332009-05-13 21:51:28 +0900581 debugfs_remove(child);
582 debugfs_remove(c->dentry);
583 return err;
584}
585
586static int clk_debugfs_register(struct clk *c)
587{
588 int err;
589 struct clk *pa = c->parent;
590
591 if (pa && !pa->dentry) {
592 err = clk_debugfs_register(pa);
593 if (err)
594 return err;
595 }
596
597 if (!c->dentry) {
598 err = clk_debugfs_register_one(c);
599 if (err)
600 return err;
601 }
Paul Mundt36ddf312006-01-16 22:14:17 -0800602 return 0;
603}
Paul Mundtcedcf332009-05-13 21:51:28 +0900604
605static int __init clk_debugfs_init(void)
606{
607 struct clk *c;
608 struct dentry *d;
609 int err;
610
611 d = debugfs_create_dir("clock", NULL);
612 if (!d)
613 return -ENOMEM;
614 clk_debugfs_root = d;
615
616 list_for_each_entry(c, &clock_list, node) {
617 err = clk_debugfs_register(c);
618 if (err)
619 goto err_out;
620 }
621 return 0;
622err_out:
623 debugfs_remove(clk_debugfs_root); /* REVISIT: Cleanup correctly */
624 return err;
625}
626late_initcall(clk_debugfs_init);