blob: 59764d6c5f418b9ae074cb84145f6421019bca7f [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 Mundt0dae8952009-05-12 06:18:09 +090013 * With clkdev bits:
14 *
15 * Copyright (C) 2008 Russell King.
16 *
Paul Mundt36ddf312006-01-16 22:14:17 -080017 * This file is subject to the terms and conditions of the GNU General Public
18 * License. See the file "COPYING" in the main directory of this archive
19 * for more details.
20 */
21#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/module.h>
Paul Mundt237b98f2006-09-27 17:28:20 +090024#include <linux/mutex.h>
Paul Mundt36ddf312006-01-16 22:14:17 -080025#include <linux/list.h>
Francesco VIRLINZI4a550262009-03-11 07:42:05 +000026#include <linux/kobject.h>
27#include <linux/sysdev.h>
Paul Mundt36ddf312006-01-16 22:14:17 -080028#include <linux/seq_file.h>
29#include <linux/err.h>
Paul Mundt1d118562006-12-01 13:15:14 +090030#include <linux/platform_device.h>
Paul Mundtcedcf332009-05-13 21:51:28 +090031#include <linux/debugfs.h>
Magnus Dammc94a8572009-05-25 08:10:28 +000032#include <linux/cpufreq.h>
Paul Mundt36ddf312006-01-16 22:14:17 -080033#include <asm/clock.h>
Paul Mundt253b0882009-05-13 17:38:11 +090034#include <asm/machvec.h>
Paul Mundt36ddf312006-01-16 22:14:17 -080035
36static LIST_HEAD(clock_list);
37static DEFINE_SPINLOCK(clock_lock);
Paul Mundt237b98f2006-09-27 17:28:20 +090038static DEFINE_MUTEX(clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -080039
Magnus Dammc94a8572009-05-25 08:10:28 +000040void clk_rate_table_build(struct clk *clk,
41 struct cpufreq_frequency_table *freq_table,
42 int nr_freqs,
43 struct clk_div_mult_table *src_table,
44 unsigned long *bitmap)
45{
46 unsigned long mult, div;
47 unsigned long freq;
48 int i;
49
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
74long clk_rate_table_round(struct clk *clk,
75 struct cpufreq_frequency_table *freq_table,
76 unsigned long rate)
77{
78 unsigned long rate_error, rate_error_prev = ~0UL;
79 unsigned long rate_best_fit = rate;
80 unsigned long highest, lowest;
81 int i;
82
83 highest = lowest = 0;
84
85 for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) {
86 unsigned long freq = freq_table[i].frequency;
87
88 if (freq == CPUFREQ_ENTRY_INVALID)
89 continue;
90
91 if (freq > highest)
92 highest = freq;
93 if (freq < lowest)
94 lowest = freq;
95
96 rate_error = abs(freq - rate);
97 if (rate_error < rate_error_prev) {
98 rate_best_fit = freq;
99 rate_error_prev = rate_error;
100 }
101
102 if (rate_error == 0)
103 break;
104 }
105
106 if (rate >= highest)
107 rate_best_fit = highest;
108 if (rate <= lowest)
109 rate_best_fit = lowest;
110
111 return rate_best_fit;
112}
113
Paul Mundta02cb232009-05-12 03:50:44 +0900114/* Used for clocks that always have same value as the parent clock */
115unsigned long followparent_recalc(struct clk *clk)
116{
Paul Mundt549b5e32009-05-14 17:38:46 +0900117 return clk->parent ? clk->parent->rate : 0;
Paul Mundta02cb232009-05-12 03:50:44 +0900118}
119
Paul Mundtaa87aa32009-05-12 05:51:05 +0900120int clk_reparent(struct clk *child, struct clk *parent)
121{
122 list_del_init(&child->sibling);
123 if (parent)
124 list_add(&child->sibling, &parent->children);
125 child->parent = parent;
126
127 /* now do the debugfs renaming to reattach the child
128 to the proper parent */
129
130 return 0;
131}
132
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900133/* Propagate rate to children */
134void propagate_rate(struct clk *tclk)
135{
136 struct clk *clkp;
137
138 list_for_each_entry(clkp, &tclk->children, sibling) {
Paul Mundtd672fef2009-05-13 17:03:09 +0900139 if (clkp->ops && clkp->ops->recalc)
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900140 clkp->rate = clkp->ops->recalc(clkp);
Paul Mundtcc96eac2009-05-13 20:28:15 +0900141 if (clkp->ops && clkp->ops->build_rate_table)
142 clkp->ops->build_rate_table(clkp);
143
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900144 propagate_rate(clkp);
145 }
146}
147
Adrian Bunk4c1cfab2008-06-18 03:36:50 +0300148static void __clk_disable(struct clk *clk)
Paul Mundt36ddf312006-01-16 22:14:17 -0800149{
Paul Mundtae891a42009-05-12 05:30:10 +0900150 if (clk->usecount == 0) {
151 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
152 clk->name);
153 WARN_ON(1);
154 return;
155 }
156
157 if (!(--clk->usecount)) {
dmitry pervushin1929cb32007-04-24 13:39:09 +0900158 if (likely(clk->ops && clk->ops->disable))
159 clk->ops->disable(clk);
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900160 if (likely(clk->parent))
161 __clk_disable(clk->parent);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900162 }
Paul Mundt36ddf312006-01-16 22:14:17 -0800163}
164
165void clk_disable(struct clk *clk)
166{
167 unsigned long flags;
168
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900169 if (!clk)
170 return;
171
Paul Mundt36ddf312006-01-16 22:14:17 -0800172 spin_lock_irqsave(&clock_lock, flags);
173 __clk_disable(clk);
174 spin_unlock_irqrestore(&clock_lock, flags);
175}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900176EXPORT_SYMBOL_GPL(clk_disable);
Paul Mundt36ddf312006-01-16 22:14:17 -0800177
Paul Mundtae891a42009-05-12 05:30:10 +0900178static int __clk_enable(struct clk *clk)
179{
180 int ret = 0;
181
182 if (clk->usecount++ == 0) {
183 if (clk->parent) {
184 ret = __clk_enable(clk->parent);
185 if (unlikely(ret))
186 goto err;
187 }
188
189 if (clk->ops && clk->ops->enable) {
190 ret = clk->ops->enable(clk);
191 if (ret) {
192 if (clk->parent)
193 __clk_disable(clk->parent);
194 goto err;
195 }
196 }
197 }
198
199 return ret;
200err:
201 clk->usecount--;
202 return ret;
203}
204
205int clk_enable(struct clk *clk)
206{
207 unsigned long flags;
208 int ret;
209
210 if (!clk)
211 return -EINVAL;
212
213 spin_lock_irqsave(&clock_lock, flags);
214 ret = __clk_enable(clk);
215 spin_unlock_irqrestore(&clock_lock, flags);
216
217 return ret;
218}
219EXPORT_SYMBOL_GPL(clk_enable);
220
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900221static LIST_HEAD(root_clks);
222
223/**
224 * recalculate_root_clocks - recalculate and propagate all root clocks
225 *
226 * Recalculates all root clocks (clocks with no parent), which if the
227 * clock's .recalc is set correctly, should also propagate their rates.
228 * Called at init.
229 */
230void recalculate_root_clocks(void)
231{
232 struct clk *clkp;
233
234 list_for_each_entry(clkp, &root_clks, sibling) {
Paul Mundtd672fef2009-05-13 17:03:09 +0900235 if (clkp->ops && clkp->ops->recalc)
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900236 clkp->rate = clkp->ops->recalc(clkp);
237 propagate_rate(clkp);
238 }
239}
240
Paul Mundt36ddf312006-01-16 22:14:17 -0800241int clk_register(struct clk *clk)
242{
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900243 if (clk == NULL || IS_ERR(clk))
244 return -EINVAL;
245
246 /*
247 * trap out already registered clocks
248 */
249 if (clk->node.next || clk->node.prev)
250 return 0;
251
Paul Mundt237b98f2006-09-27 17:28:20 +0900252 mutex_lock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800253
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900254 INIT_LIST_HEAD(&clk->children);
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900255 clk->usecount = 0;
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900256
257 if (clk->parent)
258 list_add(&clk->sibling, &clk->parent->children);
259 else
260 list_add(&clk->sibling, &root_clks);
261
Paul Mundt36ddf312006-01-16 22:14:17 -0800262 list_add(&clk->node, &clock_list);
Paul Mundtd672fef2009-05-13 17:03:09 +0900263 if (clk->ops && clk->ops->init)
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900264 clk->ops->init(clk);
Paul Mundt237b98f2006-09-27 17:28:20 +0900265 mutex_unlock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800266
267 return 0;
268}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900269EXPORT_SYMBOL_GPL(clk_register);
Paul Mundt36ddf312006-01-16 22:14:17 -0800270
271void clk_unregister(struct clk *clk)
272{
Paul Mundt237b98f2006-09-27 17:28:20 +0900273 mutex_lock(&clock_list_sem);
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900274 list_del(&clk->sibling);
Paul Mundt36ddf312006-01-16 22:14:17 -0800275 list_del(&clk->node);
Paul Mundt237b98f2006-09-27 17:28:20 +0900276 mutex_unlock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800277}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900278EXPORT_SYMBOL_GPL(clk_unregister);
Paul Mundt36ddf312006-01-16 22:14:17 -0800279
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900280static void clk_enable_init_clocks(void)
281{
282 struct clk *clkp;
283
284 list_for_each_entry(clkp, &clock_list, node)
285 if (clkp->flags & CLK_ENABLE_ON_INIT)
286 clk_enable(clkp);
287}
288
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900289unsigned long clk_get_rate(struct clk *clk)
Paul Mundt36ddf312006-01-16 22:14:17 -0800290{
291 return clk->rate;
292}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900293EXPORT_SYMBOL_GPL(clk_get_rate);
Paul Mundt36ddf312006-01-16 22:14:17 -0800294
295int clk_set_rate(struct clk *clk, unsigned long rate)
296{
dmitry pervushin1929cb32007-04-24 13:39:09 +0900297 return clk_set_rate_ex(clk, rate, 0);
298}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900299EXPORT_SYMBOL_GPL(clk_set_rate);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900300
301int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
302{
Paul Mundt36ddf312006-01-16 22:14:17 -0800303 int ret = -EOPNOTSUPP;
Paul Mundt100890c2009-05-13 17:05:51 +0900304 unsigned long flags;
305
306 spin_lock_irqsave(&clock_lock, flags);
Paul Mundt36ddf312006-01-16 22:14:17 -0800307
308 if (likely(clk->ops && clk->ops->set_rate)) {
dmitry pervushin1929cb32007-04-24 13:39:09 +0900309 ret = clk->ops->set_rate(clk, rate, algo_id);
Paul Mundt100890c2009-05-13 17:05:51 +0900310 if (ret != 0)
311 goto out_unlock;
312 } else {
313 clk->rate = rate;
314 ret = 0;
Paul Mundt36ddf312006-01-16 22:14:17 -0800315 }
316
Paul Mundt100890c2009-05-13 17:05:51 +0900317 if (clk->ops && clk->ops->recalc)
318 clk->rate = clk->ops->recalc(clk);
319
320 propagate_rate(clk);
321
322out_unlock:
323 spin_unlock_irqrestore(&clock_lock, flags);
324
Paul Mundt36ddf312006-01-16 22:14:17 -0800325 return ret;
326}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900327EXPORT_SYMBOL_GPL(clk_set_rate_ex);
Paul Mundt36ddf312006-01-16 22:14:17 -0800328
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000329int clk_set_parent(struct clk *clk, struct clk *parent)
330{
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900331 unsigned long flags;
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000332 int ret = -EINVAL;
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000333
334 if (!parent || !clk)
335 return ret;
Paul Mundtaa87aa32009-05-12 05:51:05 +0900336 if (clk->parent == parent)
337 return 0;
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000338
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900339 spin_lock_irqsave(&clock_lock, flags);
340 if (clk->usecount == 0) {
341 if (clk->ops->set_parent)
342 ret = clk->ops->set_parent(clk, parent);
Paul Mundtaa87aa32009-05-12 05:51:05 +0900343 else
344 ret = clk_reparent(clk, parent);
345
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900346 if (ret == 0) {
Paul Mundtaa87aa32009-05-12 05:51:05 +0900347 pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
348 clk->name, clk->parent->name, clk->rate);
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900349 if (clk->ops->recalc)
350 clk->rate = clk->ops->recalc(clk);
351 propagate_rate(clk);
352 }
353 } else
354 ret = -EBUSY;
355 spin_unlock_irqrestore(&clock_lock, flags);
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000356
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000357 return ret;
358}
359EXPORT_SYMBOL_GPL(clk_set_parent);
360
361struct clk *clk_get_parent(struct clk *clk)
362{
363 return clk->parent;
364}
365EXPORT_SYMBOL_GPL(clk_get_parent);
366
Paul Mundtf6991b02007-07-20 13:29:09 +0900367long clk_round_rate(struct clk *clk, unsigned long rate)
368{
369 if (likely(clk->ops && clk->ops->round_rate)) {
370 unsigned long flags, rounded;
371
372 spin_lock_irqsave(&clock_lock, flags);
373 rounded = clk->ops->round_rate(clk, rate);
374 spin_unlock_irqrestore(&clock_lock, flags);
375
376 return rounded;
377 }
378
379 return clk_get_rate(clk);
380}
381EXPORT_SYMBOL_GPL(clk_round_rate);
382
Paul Mundt1d118562006-12-01 13:15:14 +0900383/*
Paul Mundt0dae8952009-05-12 06:18:09 +0900384 * Find the correct struct clk for the device and connection ID.
385 * We do slightly fuzzy matching here:
386 * An entry with a NULL ID is assumed to be a wildcard.
387 * If an entry has a device ID, it must match
388 * If an entry has a connection ID, it must match
389 * Then we take the most specific entry - with the following
390 * order of precidence: dev+con > dev only > con only.
391 */
392static struct clk *clk_find(const char *dev_id, const char *con_id)
393{
394 struct clk_lookup *p;
395 struct clk *clk = NULL;
396 int match, best = 0;
397
398 list_for_each_entry(p, &clock_list, node) {
399 match = 0;
400 if (p->dev_id) {
401 if (!dev_id || strcmp(p->dev_id, dev_id))
402 continue;
403 match += 2;
404 }
405 if (p->con_id) {
406 if (!con_id || strcmp(p->con_id, con_id))
407 continue;
408 match += 1;
409 }
410 if (match == 0)
411 continue;
412
413 if (match > best) {
414 clk = p->clk;
415 best = match;
416 }
417 }
418 return clk;
419}
420
421struct clk *clk_get_sys(const char *dev_id, const char *con_id)
422{
423 struct clk *clk;
424
425 mutex_lock(&clock_list_sem);
426 clk = clk_find(dev_id, con_id);
427 mutex_unlock(&clock_list_sem);
428
429 return clk ? clk : ERR_PTR(-ENOENT);
430}
431EXPORT_SYMBOL_GPL(clk_get_sys);
432
433/*
Paul Mundt1d118562006-12-01 13:15:14 +0900434 * Returns a clock. Note that we first try to use device id on the bus
435 * and clock name. If this fails, we try to use clock name only.
436 */
437struct clk *clk_get(struct device *dev, const char *id)
Paul Mundt36ddf312006-01-16 22:14:17 -0800438{
Paul Mundt0dae8952009-05-12 06:18:09 +0900439 const char *dev_id = dev ? dev_name(dev) : NULL;
Paul Mundt36ddf312006-01-16 22:14:17 -0800440 struct clk *p, *clk = ERR_PTR(-ENOENT);
Paul Mundt1d118562006-12-01 13:15:14 +0900441 int idno;
442
Paul Mundt0dae8952009-05-12 06:18:09 +0900443 clk = clk_get_sys(dev_id, id);
Paul Mundtf3f82902009-05-12 16:07:40 +0900444 if (clk && !IS_ERR(clk))
Paul Mundt0dae8952009-05-12 06:18:09 +0900445 return clk;
446
Paul Mundt1d118562006-12-01 13:15:14 +0900447 if (dev == NULL || dev->bus != &platform_bus_type)
448 idno = -1;
449 else
450 idno = to_platform_device(dev)->id;
Paul Mundt36ddf312006-01-16 22:14:17 -0800451
Paul Mundt237b98f2006-09-27 17:28:20 +0900452 mutex_lock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800453 list_for_each_entry(p, &clock_list, node) {
Paul Mundt1d118562006-12-01 13:15:14 +0900454 if (p->id == idno &&
455 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
456 clk = p;
457 goto found;
458 }
459 }
460
461 list_for_each_entry(p, &clock_list, node) {
Paul Mundt36ddf312006-01-16 22:14:17 -0800462 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
463 clk = p;
464 break;
465 }
466 }
Paul Mundt1d118562006-12-01 13:15:14 +0900467
468found:
Paul Mundt237b98f2006-09-27 17:28:20 +0900469 mutex_unlock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800470
471 return clk;
472}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900473EXPORT_SYMBOL_GPL(clk_get);
Paul Mundt36ddf312006-01-16 22:14:17 -0800474
475void clk_put(struct clk *clk)
476{
477 if (clk && !IS_ERR(clk))
478 module_put(clk->owner);
479}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900480EXPORT_SYMBOL_GPL(clk_put);
Paul Mundt36ddf312006-01-16 22:14:17 -0800481
Francesco VIRLINZI4a550262009-03-11 07:42:05 +0000482#ifdef CONFIG_PM
483static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
484{
485 static pm_message_t prev_state;
486 struct clk *clkp;
487
488 switch (state.event) {
489 case PM_EVENT_ON:
490 /* Resumeing from hibernation */
Paul Mundtb68d8202009-05-12 03:45:08 +0900491 if (prev_state.event != PM_EVENT_FREEZE)
492 break;
Francesco VIRLINZI50cca712009-03-13 08:08:01 +0000493
Paul Mundtb68d8202009-05-12 03:45:08 +0900494 list_for_each_entry(clkp, &clock_list, node) {
495 if (likely(clkp->ops)) {
496 unsigned long rate = clkp->rate;
497
498 if (likely(clkp->ops->set_parent))
499 clkp->ops->set_parent(clkp,
500 clkp->parent);
501 if (likely(clkp->ops->set_rate))
502 clkp->ops->set_rate(clkp,
503 rate, NO_CHANGE);
504 else if (likely(clkp->ops->recalc))
505 clkp->rate = clkp->ops->recalc(clkp);
506 }
Francesco VIRLINZI4a550262009-03-11 07:42:05 +0000507 }
508 break;
509 case PM_EVENT_FREEZE:
510 break;
511 case PM_EVENT_SUSPEND:
512 break;
513 }
514
515 prev_state = state;
516 return 0;
517}
518
519static int clks_sysdev_resume(struct sys_device *dev)
520{
521 return clks_sysdev_suspend(dev, PMSG_ON);
522}
523
524static struct sysdev_class clks_sysdev_class = {
525 .name = "clks",
526};
527
528static struct sysdev_driver clks_sysdev_driver = {
529 .suspend = clks_sysdev_suspend,
530 .resume = clks_sysdev_resume,
531};
532
533static struct sys_device clks_sysdev_dev = {
534 .cls = &clks_sysdev_class,
535};
536
537static int __init clk_sysdev_init(void)
538{
539 sysdev_class_register(&clks_sysdev_class);
540 sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
541 sysdev_register(&clks_sysdev_dev);
542
543 return 0;
544}
545subsys_initcall(clk_sysdev_init);
546#endif
547
Paul Mundt36ddf312006-01-16 22:14:17 -0800548int __init clk_init(void)
549{
Paul Mundt253b0882009-05-13 17:38:11 +0900550 int ret;
Paul Mundt36ddf312006-01-16 22:14:17 -0800551
Paul Mundt253b0882009-05-13 17:38:11 +0900552 ret = arch_clk_init();
553 if (unlikely(ret)) {
554 pr_err("%s: CPU clock registration failed.\n", __func__);
555 return ret;
Paul Mundt36ddf312006-01-16 22:14:17 -0800556 }
557
Paul Mundt253b0882009-05-13 17:38:11 +0900558 if (sh_mv.mv_clk_init) {
559 ret = sh_mv.mv_clk_init();
560 if (unlikely(ret)) {
561 pr_err("%s: machvec clock initialization failed.\n",
562 __func__);
563 return ret;
564 }
565 }
dmitry pervushindfbbbe92007-05-15 08:42:22 +0900566
Paul Mundt36ddf312006-01-16 22:14:17 -0800567 /* Kick the child clocks.. */
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900568 recalculate_root_clocks();
Paul Mundt36ddf312006-01-16 22:14:17 -0800569
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900570 /* Enable the necessary init clocks */
571 clk_enable_init_clocks();
572
Paul Mundt36ddf312006-01-16 22:14:17 -0800573 return ret;
574}
575
Paul Mundtcedcf332009-05-13 21:51:28 +0900576/*
577 * debugfs support to trace clock tree hierarchy and attributes
578 */
579static struct dentry *clk_debugfs_root;
Paul Mundt36ddf312006-01-16 22:14:17 -0800580
Paul Mundtcedcf332009-05-13 21:51:28 +0900581static int clk_debugfs_register_one(struct clk *c)
582{
583 int err;
584 struct dentry *d, *child;
585 struct clk *pa = c->parent;
586 char s[255];
587 char *p = s;
588
589 p += sprintf(p, "%s", c->name);
Paul Mundt549b5e32009-05-14 17:38:46 +0900590 if (c->id >= 0)
Paul Mundtcedcf332009-05-13 21:51:28 +0900591 sprintf(p, ":%d", c->id);
592 d = debugfs_create_dir(s, pa ? pa->dentry : clk_debugfs_root);
593 if (!d)
594 return -ENOMEM;
595 c->dentry = d;
596
597 d = debugfs_create_u8("usecount", S_IRUGO, c->dentry, (u8 *)&c->usecount);
598 if (!d) {
599 err = -ENOMEM;
600 goto err_out;
601 }
602 d = debugfs_create_u32("rate", S_IRUGO, c->dentry, (u32 *)&c->rate);
603 if (!d) {
604 err = -ENOMEM;
605 goto err_out;
606 }
607 d = debugfs_create_x32("flags", S_IRUGO, c->dentry, (u32 *)&c->flags);
608 if (!d) {
609 err = -ENOMEM;
610 goto err_out;
611 }
612 return 0;
613
614err_out:
615 d = c->dentry;
616 list_for_each_entry(child, &d->d_subdirs, d_u.d_child)
617 debugfs_remove(child);
618 debugfs_remove(c->dentry);
619 return err;
620}
621
622static int clk_debugfs_register(struct clk *c)
623{
624 int err;
625 struct clk *pa = c->parent;
626
627 if (pa && !pa->dentry) {
628 err = clk_debugfs_register(pa);
629 if (err)
630 return err;
631 }
632
633 if (!c->dentry) {
634 err = clk_debugfs_register_one(c);
635 if (err)
636 return err;
637 }
Paul Mundt36ddf312006-01-16 22:14:17 -0800638 return 0;
639}
Paul Mundtcedcf332009-05-13 21:51:28 +0900640
641static int __init clk_debugfs_init(void)
642{
643 struct clk *c;
644 struct dentry *d;
645 int err;
646
647 d = debugfs_create_dir("clock", NULL);
648 if (!d)
649 return -ENOMEM;
650 clk_debugfs_root = d;
651
652 list_for_each_entry(c, &clock_list, node) {
653 err = clk_debugfs_register(c);
654 if (err)
655 goto err_out;
656 }
657 return 0;
658err_out:
659 debugfs_remove(clk_debugfs_root); /* REVISIT: Cleanup correctly */
660 return err;
661}
662late_initcall(clk_debugfs_init);