blob: aa0fd0893585bb545b5ac68d58c83c7740446bff [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
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900142 propagate_rate(clkp);
143 }
144}
145
Adrian Bunk4c1cfab2008-06-18 03:36:50 +0300146static void __clk_disable(struct clk *clk)
Paul Mundt36ddf312006-01-16 22:14:17 -0800147{
Paul Mundtae891a42009-05-12 05:30:10 +0900148 if (clk->usecount == 0) {
149 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
150 clk->name);
151 WARN_ON(1);
152 return;
153 }
154
155 if (!(--clk->usecount)) {
dmitry pervushin1929cb32007-04-24 13:39:09 +0900156 if (likely(clk->ops && clk->ops->disable))
157 clk->ops->disable(clk);
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900158 if (likely(clk->parent))
159 __clk_disable(clk->parent);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900160 }
Paul Mundt36ddf312006-01-16 22:14:17 -0800161}
162
163void clk_disable(struct clk *clk)
164{
165 unsigned long flags;
166
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900167 if (!clk)
168 return;
169
Paul Mundt36ddf312006-01-16 22:14:17 -0800170 spin_lock_irqsave(&clock_lock, flags);
171 __clk_disable(clk);
172 spin_unlock_irqrestore(&clock_lock, flags);
173}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900174EXPORT_SYMBOL_GPL(clk_disable);
Paul Mundt36ddf312006-01-16 22:14:17 -0800175
Paul Mundtae891a42009-05-12 05:30:10 +0900176static int __clk_enable(struct clk *clk)
177{
178 int ret = 0;
179
180 if (clk->usecount++ == 0) {
181 if (clk->parent) {
182 ret = __clk_enable(clk->parent);
183 if (unlikely(ret))
184 goto err;
185 }
186
187 if (clk->ops && clk->ops->enable) {
188 ret = clk->ops->enable(clk);
189 if (ret) {
190 if (clk->parent)
191 __clk_disable(clk->parent);
192 goto err;
193 }
194 }
195 }
196
197 return ret;
198err:
199 clk->usecount--;
200 return ret;
201}
202
203int clk_enable(struct clk *clk)
204{
205 unsigned long flags;
206 int ret;
207
208 if (!clk)
209 return -EINVAL;
210
211 spin_lock_irqsave(&clock_lock, flags);
212 ret = __clk_enable(clk);
213 spin_unlock_irqrestore(&clock_lock, flags);
214
215 return ret;
216}
217EXPORT_SYMBOL_GPL(clk_enable);
218
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900219static LIST_HEAD(root_clks);
220
221/**
222 * recalculate_root_clocks - recalculate and propagate all root clocks
223 *
224 * Recalculates all root clocks (clocks with no parent), which if the
225 * clock's .recalc is set correctly, should also propagate their rates.
226 * Called at init.
227 */
228void recalculate_root_clocks(void)
229{
230 struct clk *clkp;
231
232 list_for_each_entry(clkp, &root_clks, sibling) {
Paul Mundtd672fef2009-05-13 17:03:09 +0900233 if (clkp->ops && clkp->ops->recalc)
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900234 clkp->rate = clkp->ops->recalc(clkp);
235 propagate_rate(clkp);
236 }
237}
238
Paul Mundt36ddf312006-01-16 22:14:17 -0800239int clk_register(struct clk *clk)
240{
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900241 if (clk == NULL || IS_ERR(clk))
242 return -EINVAL;
243
244 /*
245 * trap out already registered clocks
246 */
247 if (clk->node.next || clk->node.prev)
248 return 0;
249
Paul Mundt237b98f2006-09-27 17:28:20 +0900250 mutex_lock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800251
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900252 INIT_LIST_HEAD(&clk->children);
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900253 clk->usecount = 0;
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900254
255 if (clk->parent)
256 list_add(&clk->sibling, &clk->parent->children);
257 else
258 list_add(&clk->sibling, &root_clks);
259
Paul Mundt36ddf312006-01-16 22:14:17 -0800260 list_add(&clk->node, &clock_list);
Paul Mundtd672fef2009-05-13 17:03:09 +0900261 if (clk->ops && clk->ops->init)
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900262 clk->ops->init(clk);
Paul Mundt237b98f2006-09-27 17:28:20 +0900263 mutex_unlock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800264
265 return 0;
266}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900267EXPORT_SYMBOL_GPL(clk_register);
Paul Mundt36ddf312006-01-16 22:14:17 -0800268
269void clk_unregister(struct clk *clk)
270{
Paul Mundt237b98f2006-09-27 17:28:20 +0900271 mutex_lock(&clock_list_sem);
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900272 list_del(&clk->sibling);
Paul Mundt36ddf312006-01-16 22:14:17 -0800273 list_del(&clk->node);
Paul Mundt237b98f2006-09-27 17:28:20 +0900274 mutex_unlock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800275}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900276EXPORT_SYMBOL_GPL(clk_unregister);
Paul Mundt36ddf312006-01-16 22:14:17 -0800277
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900278static void clk_enable_init_clocks(void)
279{
280 struct clk *clkp;
281
282 list_for_each_entry(clkp, &clock_list, node)
283 if (clkp->flags & CLK_ENABLE_ON_INIT)
284 clk_enable(clkp);
285}
286
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900287unsigned long clk_get_rate(struct clk *clk)
Paul Mundt36ddf312006-01-16 22:14:17 -0800288{
289 return clk->rate;
290}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900291EXPORT_SYMBOL_GPL(clk_get_rate);
Paul Mundt36ddf312006-01-16 22:14:17 -0800292
293int clk_set_rate(struct clk *clk, unsigned long rate)
294{
dmitry pervushin1929cb32007-04-24 13:39:09 +0900295 return clk_set_rate_ex(clk, rate, 0);
296}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900297EXPORT_SYMBOL_GPL(clk_set_rate);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900298
299int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
300{
Paul Mundt36ddf312006-01-16 22:14:17 -0800301 int ret = -EOPNOTSUPP;
Paul Mundt100890c2009-05-13 17:05:51 +0900302 unsigned long flags;
303
304 spin_lock_irqsave(&clock_lock, flags);
Paul Mundt36ddf312006-01-16 22:14:17 -0800305
306 if (likely(clk->ops && clk->ops->set_rate)) {
dmitry pervushin1929cb32007-04-24 13:39:09 +0900307 ret = clk->ops->set_rate(clk, rate, algo_id);
Paul Mundt100890c2009-05-13 17:05:51 +0900308 if (ret != 0)
309 goto out_unlock;
310 } else {
311 clk->rate = rate;
312 ret = 0;
Paul Mundt36ddf312006-01-16 22:14:17 -0800313 }
314
Paul Mundt100890c2009-05-13 17:05:51 +0900315 if (clk->ops && clk->ops->recalc)
316 clk->rate = clk->ops->recalc(clk);
317
318 propagate_rate(clk);
319
320out_unlock:
321 spin_unlock_irqrestore(&clock_lock, flags);
322
Paul Mundt36ddf312006-01-16 22:14:17 -0800323 return ret;
324}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900325EXPORT_SYMBOL_GPL(clk_set_rate_ex);
Paul Mundt36ddf312006-01-16 22:14:17 -0800326
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000327int clk_set_parent(struct clk *clk, struct clk *parent)
328{
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900329 unsigned long flags;
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000330 int ret = -EINVAL;
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000331
332 if (!parent || !clk)
333 return ret;
Paul Mundtaa87aa32009-05-12 05:51:05 +0900334 if (clk->parent == parent)
335 return 0;
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000336
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900337 spin_lock_irqsave(&clock_lock, flags);
338 if (clk->usecount == 0) {
339 if (clk->ops->set_parent)
340 ret = clk->ops->set_parent(clk, parent);
Paul Mundtaa87aa32009-05-12 05:51:05 +0900341 else
342 ret = clk_reparent(clk, parent);
343
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900344 if (ret == 0) {
Paul Mundtaa87aa32009-05-12 05:51:05 +0900345 pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
346 clk->name, clk->parent->name, clk->rate);
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900347 if (clk->ops->recalc)
348 clk->rate = clk->ops->recalc(clk);
349 propagate_rate(clk);
350 }
351 } else
352 ret = -EBUSY;
353 spin_unlock_irqrestore(&clock_lock, flags);
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000354
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000355 return ret;
356}
357EXPORT_SYMBOL_GPL(clk_set_parent);
358
359struct clk *clk_get_parent(struct clk *clk)
360{
361 return clk->parent;
362}
363EXPORT_SYMBOL_GPL(clk_get_parent);
364
Paul Mundtf6991b02007-07-20 13:29:09 +0900365long clk_round_rate(struct clk *clk, unsigned long rate)
366{
367 if (likely(clk->ops && clk->ops->round_rate)) {
368 unsigned long flags, rounded;
369
370 spin_lock_irqsave(&clock_lock, flags);
371 rounded = clk->ops->round_rate(clk, rate);
372 spin_unlock_irqrestore(&clock_lock, flags);
373
374 return rounded;
375 }
376
377 return clk_get_rate(clk);
378}
379EXPORT_SYMBOL_GPL(clk_round_rate);
380
Paul Mundt1d118562006-12-01 13:15:14 +0900381/*
Paul Mundt0dae8952009-05-12 06:18:09 +0900382 * Find the correct struct clk for the device and connection ID.
383 * We do slightly fuzzy matching here:
384 * An entry with a NULL ID is assumed to be a wildcard.
385 * If an entry has a device ID, it must match
386 * If an entry has a connection ID, it must match
387 * Then we take the most specific entry - with the following
388 * order of precidence: dev+con > dev only > con only.
389 */
390static struct clk *clk_find(const char *dev_id, const char *con_id)
391{
392 struct clk_lookup *p;
393 struct clk *clk = NULL;
394 int match, best = 0;
395
396 list_for_each_entry(p, &clock_list, node) {
397 match = 0;
398 if (p->dev_id) {
399 if (!dev_id || strcmp(p->dev_id, dev_id))
400 continue;
401 match += 2;
402 }
403 if (p->con_id) {
404 if (!con_id || strcmp(p->con_id, con_id))
405 continue;
406 match += 1;
407 }
408 if (match == 0)
409 continue;
410
411 if (match > best) {
412 clk = p->clk;
413 best = match;
414 }
415 }
416 return clk;
417}
418
419struct clk *clk_get_sys(const char *dev_id, const char *con_id)
420{
421 struct clk *clk;
422
423 mutex_lock(&clock_list_sem);
424 clk = clk_find(dev_id, con_id);
425 mutex_unlock(&clock_list_sem);
426
427 return clk ? clk : ERR_PTR(-ENOENT);
428}
429EXPORT_SYMBOL_GPL(clk_get_sys);
430
431/*
Paul Mundt1d118562006-12-01 13:15:14 +0900432 * Returns a clock. Note that we first try to use device id on the bus
433 * and clock name. If this fails, we try to use clock name only.
434 */
435struct clk *clk_get(struct device *dev, const char *id)
Paul Mundt36ddf312006-01-16 22:14:17 -0800436{
Paul Mundt0dae8952009-05-12 06:18:09 +0900437 const char *dev_id = dev ? dev_name(dev) : NULL;
Paul Mundt36ddf312006-01-16 22:14:17 -0800438 struct clk *p, *clk = ERR_PTR(-ENOENT);
Paul Mundt1d118562006-12-01 13:15:14 +0900439 int idno;
440
Paul Mundt0dae8952009-05-12 06:18:09 +0900441 clk = clk_get_sys(dev_id, id);
Paul Mundtf3f82902009-05-12 16:07:40 +0900442 if (clk && !IS_ERR(clk))
Paul Mundt0dae8952009-05-12 06:18:09 +0900443 return clk;
444
Paul Mundt1d118562006-12-01 13:15:14 +0900445 if (dev == NULL || dev->bus != &platform_bus_type)
446 idno = -1;
447 else
448 idno = to_platform_device(dev)->id;
Paul Mundt36ddf312006-01-16 22:14:17 -0800449
Paul Mundt237b98f2006-09-27 17:28:20 +0900450 mutex_lock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800451 list_for_each_entry(p, &clock_list, node) {
Paul Mundt1d118562006-12-01 13:15:14 +0900452 if (p->id == idno &&
453 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
454 clk = p;
455 goto found;
456 }
457 }
458
459 list_for_each_entry(p, &clock_list, node) {
Paul Mundt36ddf312006-01-16 22:14:17 -0800460 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
461 clk = p;
462 break;
463 }
464 }
Paul Mundt1d118562006-12-01 13:15:14 +0900465
466found:
Paul Mundt237b98f2006-09-27 17:28:20 +0900467 mutex_unlock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800468
469 return clk;
470}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900471EXPORT_SYMBOL_GPL(clk_get);
Paul Mundt36ddf312006-01-16 22:14:17 -0800472
473void clk_put(struct clk *clk)
474{
475 if (clk && !IS_ERR(clk))
476 module_put(clk->owner);
477}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900478EXPORT_SYMBOL_GPL(clk_put);
Paul Mundt36ddf312006-01-16 22:14:17 -0800479
Francesco VIRLINZI4a550262009-03-11 07:42:05 +0000480#ifdef CONFIG_PM
481static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
482{
483 static pm_message_t prev_state;
484 struct clk *clkp;
485
486 switch (state.event) {
487 case PM_EVENT_ON:
488 /* Resumeing from hibernation */
Paul Mundtb68d8202009-05-12 03:45:08 +0900489 if (prev_state.event != PM_EVENT_FREEZE)
490 break;
Francesco VIRLINZI50cca712009-03-13 08:08:01 +0000491
Paul Mundtb68d8202009-05-12 03:45:08 +0900492 list_for_each_entry(clkp, &clock_list, node) {
493 if (likely(clkp->ops)) {
494 unsigned long rate = clkp->rate;
495
496 if (likely(clkp->ops->set_parent))
497 clkp->ops->set_parent(clkp,
498 clkp->parent);
499 if (likely(clkp->ops->set_rate))
500 clkp->ops->set_rate(clkp,
501 rate, NO_CHANGE);
502 else if (likely(clkp->ops->recalc))
503 clkp->rate = clkp->ops->recalc(clkp);
504 }
Francesco VIRLINZI4a550262009-03-11 07:42:05 +0000505 }
506 break;
507 case PM_EVENT_FREEZE:
508 break;
509 case PM_EVENT_SUSPEND:
510 break;
511 }
512
513 prev_state = state;
514 return 0;
515}
516
517static int clks_sysdev_resume(struct sys_device *dev)
518{
519 return clks_sysdev_suspend(dev, PMSG_ON);
520}
521
522static struct sysdev_class clks_sysdev_class = {
523 .name = "clks",
524};
525
526static struct sysdev_driver clks_sysdev_driver = {
527 .suspend = clks_sysdev_suspend,
528 .resume = clks_sysdev_resume,
529};
530
531static struct sys_device clks_sysdev_dev = {
532 .cls = &clks_sysdev_class,
533};
534
535static int __init clk_sysdev_init(void)
536{
537 sysdev_class_register(&clks_sysdev_class);
538 sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
539 sysdev_register(&clks_sysdev_dev);
540
541 return 0;
542}
543subsys_initcall(clk_sysdev_init);
544#endif
545
Paul Mundt36ddf312006-01-16 22:14:17 -0800546int __init clk_init(void)
547{
Paul Mundt253b0882009-05-13 17:38:11 +0900548 int ret;
Paul Mundt36ddf312006-01-16 22:14:17 -0800549
Paul Mundt253b0882009-05-13 17:38:11 +0900550 ret = arch_clk_init();
551 if (unlikely(ret)) {
552 pr_err("%s: CPU clock registration failed.\n", __func__);
553 return ret;
Paul Mundt36ddf312006-01-16 22:14:17 -0800554 }
555
Paul Mundt253b0882009-05-13 17:38:11 +0900556 if (sh_mv.mv_clk_init) {
557 ret = sh_mv.mv_clk_init();
558 if (unlikely(ret)) {
559 pr_err("%s: machvec clock initialization failed.\n",
560 __func__);
561 return ret;
562 }
563 }
dmitry pervushindfbbbe92007-05-15 08:42:22 +0900564
Paul Mundt36ddf312006-01-16 22:14:17 -0800565 /* Kick the child clocks.. */
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900566 recalculate_root_clocks();
Paul Mundt36ddf312006-01-16 22:14:17 -0800567
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900568 /* Enable the necessary init clocks */
569 clk_enable_init_clocks();
570
Paul Mundt36ddf312006-01-16 22:14:17 -0800571 return ret;
572}
573
Paul Mundtcedcf332009-05-13 21:51:28 +0900574/*
575 * debugfs support to trace clock tree hierarchy and attributes
576 */
577static struct dentry *clk_debugfs_root;
Paul Mundt36ddf312006-01-16 22:14:17 -0800578
Paul Mundtcedcf332009-05-13 21:51:28 +0900579static int clk_debugfs_register_one(struct clk *c)
580{
581 int err;
582 struct dentry *d, *child;
583 struct clk *pa = c->parent;
584 char s[255];
585 char *p = s;
586
587 p += sprintf(p, "%s", c->name);
Paul Mundt549b5e32009-05-14 17:38:46 +0900588 if (c->id >= 0)
Paul Mundtcedcf332009-05-13 21:51:28 +0900589 sprintf(p, ":%d", c->id);
590 d = debugfs_create_dir(s, pa ? pa->dentry : clk_debugfs_root);
591 if (!d)
592 return -ENOMEM;
593 c->dentry = d;
594
595 d = debugfs_create_u8("usecount", S_IRUGO, c->dentry, (u8 *)&c->usecount);
596 if (!d) {
597 err = -ENOMEM;
598 goto err_out;
599 }
600 d = debugfs_create_u32("rate", S_IRUGO, c->dentry, (u32 *)&c->rate);
601 if (!d) {
602 err = -ENOMEM;
603 goto err_out;
604 }
605 d = debugfs_create_x32("flags", S_IRUGO, c->dentry, (u32 *)&c->flags);
606 if (!d) {
607 err = -ENOMEM;
608 goto err_out;
609 }
610 return 0;
611
612err_out:
613 d = c->dentry;
614 list_for_each_entry(child, &d->d_subdirs, d_u.d_child)
615 debugfs_remove(child);
616 debugfs_remove(c->dentry);
617 return err;
618}
619
620static int clk_debugfs_register(struct clk *c)
621{
622 int err;
623 struct clk *pa = c->parent;
624
625 if (pa && !pa->dentry) {
626 err = clk_debugfs_register(pa);
627 if (err)
628 return err;
629 }
630
631 if (!c->dentry) {
632 err = clk_debugfs_register_one(c);
633 if (err)
634 return err;
635 }
Paul Mundt36ddf312006-01-16 22:14:17 -0800636 return 0;
637}
Paul Mundtcedcf332009-05-13 21:51:28 +0900638
639static int __init clk_debugfs_init(void)
640{
641 struct clk *c;
642 struct dentry *d;
643 int err;
644
645 d = debugfs_create_dir("clock", NULL);
646 if (!d)
647 return -ENOMEM;
648 clk_debugfs_root = d;
649
650 list_for_each_entry(c, &clock_list, node) {
651 err = clk_debugfs_register(c);
652 if (err)
653 goto err_out;
654 }
655 return 0;
656err_out:
657 debugfs_remove(clk_debugfs_root); /* REVISIT: Cleanup correctly */
658 return err;
659}
660late_initcall(clk_debugfs_init);