blob: 133dbe4033412e7c017a4dd6b2b1a5ea096645e8 [file] [log] [blame]
Paul Mundt36ddf312006-01-16 22:14:17 -08001/*
2 * arch/sh/kernel/cpu/clock.c - SuperH clock framework
3 *
Paul Mundtdb62e5b2007-04-26 12:17:20 +09004 * Copyright (C) 2005, 2006, 2007 Paul Mundt
Paul Mundt36ddf312006-01-16 22:14:17 -08005 *
6 * This clock framework is derived from the OMAP version by:
7 *
Paul Mundt1d118562006-12-01 13:15:14 +09008 * Copyright (C) 2004 - 2005 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 Mundtdb62e5b2007-04-26 12:17:20 +090027#include <linux/proc_fs.h>
Paul Mundt36ddf312006-01-16 22:14:17 -080028#include <asm/clock.h>
29#include <asm/timer.h>
30
31static LIST_HEAD(clock_list);
32static DEFINE_SPINLOCK(clock_lock);
Paul Mundt237b98f2006-09-27 17:28:20 +090033static DEFINE_MUTEX(clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -080034
35/*
36 * Each subtype is expected to define the init routines for these clocks,
37 * as each subtype (or processor family) will have these clocks at the
38 * very least. These are all provided through the CPG, which even some of
39 * the more quirky parts (such as ST40, SH4-202, etc.) still have.
40 *
41 * The processor-specific code is expected to register any additional
42 * clock sources that are of interest.
43 */
44static struct clk master_clk = {
45 .name = "master_clk",
46 .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
Paul Mundt36ddf312006-01-16 22:14:17 -080047 .rate = CONFIG_SH_PCLK_FREQ,
Paul Mundt36ddf312006-01-16 22:14:17 -080048};
49
50static struct clk module_clk = {
51 .name = "module_clk",
52 .parent = &master_clk,
53 .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
54};
55
56static struct clk bus_clk = {
57 .name = "bus_clk",
58 .parent = &master_clk,
59 .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
60};
61
62static struct clk cpu_clk = {
63 .name = "cpu_clk",
64 .parent = &master_clk,
65 .flags = CLK_ALWAYS_ENABLED,
66};
67
68/*
69 * The ordering of these clocks matters, do not change it.
70 */
71static struct clk *onchip_clocks[] = {
72 &master_clk,
73 &module_clk,
74 &bus_clk,
75 &cpu_clk,
76};
77
78static void propagate_rate(struct clk *clk)
79{
80 struct clk *clkp;
81
82 list_for_each_entry(clkp, &clock_list, node) {
83 if (likely(clkp->parent != clk))
84 continue;
85 if (likely(clkp->ops && clkp->ops->recalc))
86 clkp->ops->recalc(clkp);
Stuart Menefy24eb17e2007-09-28 11:51:52 +090087 if (unlikely(clkp->flags & CLK_RATE_PROPAGATES))
88 propagate_rate(clkp);
Paul Mundt36ddf312006-01-16 22:14:17 -080089 }
90}
91
Magnus Damm4f5ecaa2009-05-08 08:23:29 +000092static void __clk_init(struct clk *clk)
Paul Mundt36ddf312006-01-16 22:14:17 -080093{
94 /*
95 * See if this is the first time we're enabling the clock, some
96 * clocks that are always enabled still require "special"
97 * initialization. This is especially true if the clock mode
98 * changes and the clock needs to hunt for the proper set of
99 * divisors to use before it can effectively recalc.
100 */
Magnus Dammb3cacf32009-05-07 10:31:39 +0000101
Magnus Damm4f5ecaa2009-05-08 08:23:29 +0000102 if (clk->flags & CLK_NEEDS_INIT) {
Paul Mundt36ddf312006-01-16 22:14:17 -0800103 if (clk->ops && clk->ops->init)
104 clk->ops->init(clk);
105
Magnus Damm4f5ecaa2009-05-08 08:23:29 +0000106 clk->flags &= ~CLK_NEEDS_INIT;
107 }
108}
dmitry pervushin1929cb32007-04-24 13:39:09 +0900109
Magnus Damm4f5ecaa2009-05-08 08:23:29 +0000110static int __clk_enable(struct clk *clk)
111{
112 if (!clk)
113 return -EINVAL;
114
115 clk->usecount++;
116
117 /* nothing to do if always enabled */
118 if (clk->flags & CLK_ALWAYS_ENABLED)
119 return 0;
120
121 if (clk->usecount == 1) {
122 __clk_init(clk);
123
124 __clk_enable(clk->parent);
125
126 if (clk->ops && clk->ops->enable)
127 clk->ops->enable(clk);
128 }
Paul Mundt36ddf312006-01-16 22:14:17 -0800129
Paul Mundt36ddf312006-01-16 22:14:17 -0800130 return 0;
131}
132
133int clk_enable(struct clk *clk)
134{
135 unsigned long flags;
136 int ret;
137
138 spin_lock_irqsave(&clock_lock, flags);
139 ret = __clk_enable(clk);
140 spin_unlock_irqrestore(&clock_lock, flags);
141
142 return ret;
143}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900144EXPORT_SYMBOL_GPL(clk_enable);
Paul Mundt36ddf312006-01-16 22:14:17 -0800145
Adrian Bunk4c1cfab2008-06-18 03:36:50 +0300146static void __clk_disable(struct clk *clk)
Paul Mundt36ddf312006-01-16 22:14:17 -0800147{
Magnus Damm4f5ecaa2009-05-08 08:23:29 +0000148 if (!clk)
149 return;
150
151 clk->usecount--;
152
153 WARN_ON(clk->usecount < 0);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900154
Paul Mundt36ddf312006-01-16 22:14:17 -0800155 if (clk->flags & CLK_ALWAYS_ENABLED)
156 return;
157
Magnus Damm4f5ecaa2009-05-08 08:23:29 +0000158 if (clk->usecount == 0) {
dmitry pervushin1929cb32007-04-24 13:39:09 +0900159 if (likely(clk->ops && clk->ops->disable))
160 clk->ops->disable(clk);
Magnus Damm4f5ecaa2009-05-08 08:23:29 +0000161
162 __clk_disable(clk->parent);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900163 }
Paul Mundt36ddf312006-01-16 22:14:17 -0800164}
165
166void clk_disable(struct clk *clk)
167{
168 unsigned long flags;
169
170 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
176int clk_register(struct clk *clk)
177{
Paul Mundt237b98f2006-09-27 17:28:20 +0900178 mutex_lock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800179
180 list_add(&clk->node, &clock_list);
Magnus Damm4f5ecaa2009-05-08 08:23:29 +0000181 clk->usecount = 0;
182 clk->flags |= CLK_NEEDS_INIT;
Paul Mundt36ddf312006-01-16 22:14:17 -0800183
Paul Mundt237b98f2006-09-27 17:28:20 +0900184 mutex_unlock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800185
dmitry pervushin1929cb32007-04-24 13:39:09 +0900186 if (clk->flags & CLK_ALWAYS_ENABLED) {
Magnus Damm4f5ecaa2009-05-08 08:23:29 +0000187 __clk_init(clk);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900188 pr_debug( "Clock '%s' is ALWAYS_ENABLED\n", clk->name);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900189 if (clk->ops && clk->ops->enable)
190 clk->ops->enable(clk);
191 pr_debug( "Enabled.");
192 }
193
Paul Mundt36ddf312006-01-16 22:14:17 -0800194 return 0;
195}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900196EXPORT_SYMBOL_GPL(clk_register);
Paul Mundt36ddf312006-01-16 22:14:17 -0800197
198void clk_unregister(struct clk *clk)
199{
Paul Mundt237b98f2006-09-27 17:28:20 +0900200 mutex_lock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800201 list_del(&clk->node);
Paul Mundt237b98f2006-09-27 17:28:20 +0900202 mutex_unlock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800203}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900204EXPORT_SYMBOL_GPL(clk_unregister);
Paul Mundt36ddf312006-01-16 22:14:17 -0800205
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900206unsigned long clk_get_rate(struct clk *clk)
Paul Mundt36ddf312006-01-16 22:14:17 -0800207{
208 return clk->rate;
209}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900210EXPORT_SYMBOL_GPL(clk_get_rate);
Paul Mundt36ddf312006-01-16 22:14:17 -0800211
212int clk_set_rate(struct clk *clk, unsigned long rate)
213{
dmitry pervushin1929cb32007-04-24 13:39:09 +0900214 return clk_set_rate_ex(clk, rate, 0);
215}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900216EXPORT_SYMBOL_GPL(clk_set_rate);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900217
218int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
219{
Paul Mundt36ddf312006-01-16 22:14:17 -0800220 int ret = -EOPNOTSUPP;
221
222 if (likely(clk->ops && clk->ops->set_rate)) {
223 unsigned long flags;
224
225 spin_lock_irqsave(&clock_lock, flags);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900226 ret = clk->ops->set_rate(clk, rate, algo_id);
Paul Mundt36ddf312006-01-16 22:14:17 -0800227 spin_unlock_irqrestore(&clock_lock, flags);
228 }
229
230 if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
231 propagate_rate(clk);
232
233 return ret;
234}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900235EXPORT_SYMBOL_GPL(clk_set_rate_ex);
Paul Mundt36ddf312006-01-16 22:14:17 -0800236
237void clk_recalc_rate(struct clk *clk)
238{
239 if (likely(clk->ops && clk->ops->recalc)) {
240 unsigned long flags;
241
242 spin_lock_irqsave(&clock_lock, flags);
243 clk->ops->recalc(clk);
244 spin_unlock_irqrestore(&clock_lock, flags);
245 }
246
247 if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
248 propagate_rate(clk);
249}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900250EXPORT_SYMBOL_GPL(clk_recalc_rate);
Paul Mundt36ddf312006-01-16 22:14:17 -0800251
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000252int clk_set_parent(struct clk *clk, struct clk *parent)
253{
254 int ret = -EINVAL;
255 struct clk *old;
256
257 if (!parent || !clk)
258 return ret;
259
260 old = clk->parent;
261 if (likely(clk->ops && clk->ops->set_parent)) {
262 unsigned long flags;
263 spin_lock_irqsave(&clock_lock, flags);
264 ret = clk->ops->set_parent(clk, parent);
265 spin_unlock_irqrestore(&clock_lock, flags);
266 clk->parent = (ret ? old : parent);
267 }
268
269 if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
270 propagate_rate(clk);
271 return ret;
272}
273EXPORT_SYMBOL_GPL(clk_set_parent);
274
275struct clk *clk_get_parent(struct clk *clk)
276{
277 return clk->parent;
278}
279EXPORT_SYMBOL_GPL(clk_get_parent);
280
Paul Mundtf6991b02007-07-20 13:29:09 +0900281long clk_round_rate(struct clk *clk, unsigned long rate)
282{
283 if (likely(clk->ops && clk->ops->round_rate)) {
284 unsigned long flags, rounded;
285
286 spin_lock_irqsave(&clock_lock, flags);
287 rounded = clk->ops->round_rate(clk, rate);
288 spin_unlock_irqrestore(&clock_lock, flags);
289
290 return rounded;
291 }
292
293 return clk_get_rate(clk);
294}
295EXPORT_SYMBOL_GPL(clk_round_rate);
296
Paul Mundt1d118562006-12-01 13:15:14 +0900297/*
298 * Returns a clock. Note that we first try to use device id on the bus
299 * and clock name. If this fails, we try to use clock name only.
300 */
301struct clk *clk_get(struct device *dev, const char *id)
Paul Mundt36ddf312006-01-16 22:14:17 -0800302{
303 struct clk *p, *clk = ERR_PTR(-ENOENT);
Paul Mundt1d118562006-12-01 13:15:14 +0900304 int idno;
305
306 if (dev == NULL || dev->bus != &platform_bus_type)
307 idno = -1;
308 else
309 idno = to_platform_device(dev)->id;
Paul Mundt36ddf312006-01-16 22:14:17 -0800310
Paul Mundt237b98f2006-09-27 17:28:20 +0900311 mutex_lock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800312 list_for_each_entry(p, &clock_list, node) {
Paul Mundt1d118562006-12-01 13:15:14 +0900313 if (p->id == idno &&
314 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
315 clk = p;
316 goto found;
317 }
318 }
319
320 list_for_each_entry(p, &clock_list, node) {
Paul Mundt36ddf312006-01-16 22:14:17 -0800321 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
322 clk = p;
323 break;
324 }
325 }
Paul Mundt1d118562006-12-01 13:15:14 +0900326
327found:
Paul Mundt237b98f2006-09-27 17:28:20 +0900328 mutex_unlock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800329
330 return clk;
331}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900332EXPORT_SYMBOL_GPL(clk_get);
Paul Mundt36ddf312006-01-16 22:14:17 -0800333
334void clk_put(struct clk *clk)
335{
336 if (clk && !IS_ERR(clk))
337 module_put(clk->owner);
338}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900339EXPORT_SYMBOL_GPL(clk_put);
Paul Mundt36ddf312006-01-16 22:14:17 -0800340
341void __init __attribute__ ((weak))
342arch_init_clk_ops(struct clk_ops **ops, int type)
343{
344}
345
Paul Mundtfa439722008-09-04 18:53:58 +0900346int __init __attribute__ ((weak))
dmitry pervushindfbbbe92007-05-15 08:42:22 +0900347arch_clk_init(void)
348{
Paul Mundtfa439722008-09-04 18:53:58 +0900349 return 0;
dmitry pervushindfbbbe92007-05-15 08:42:22 +0900350}
351
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900352static int show_clocks(char *buf, char **start, off_t off,
353 int len, int *eof, void *data)
354{
355 struct clk *clk;
356 char *p = buf;
357
358 list_for_each_entry_reverse(clk, &clock_list, node) {
359 unsigned long rate = clk_get_rate(clk);
360
Magnus Damm152fe362008-07-17 19:05:54 +0900361 p += sprintf(p, "%-12s\t: %ld.%02ldMHz\t%s\n", clk->name,
362 rate / 1000000, (rate % 1000000) / 10000,
363 ((clk->flags & CLK_ALWAYS_ENABLED) ||
Magnus Damm4f5ecaa2009-05-08 08:23:29 +0000364 clk->usecount > 0) ?
Magnus Damm152fe362008-07-17 19:05:54 +0900365 "enabled" : "disabled");
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900366 }
367
368 return p - buf;
369}
370
Francesco VIRLINZI4a550262009-03-11 07:42:05 +0000371#ifdef CONFIG_PM
372static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
373{
374 static pm_message_t prev_state;
375 struct clk *clkp;
376
377 switch (state.event) {
378 case PM_EVENT_ON:
379 /* Resumeing from hibernation */
380 if (prev_state.event == PM_EVENT_FREEZE) {
381 list_for_each_entry(clkp, &clock_list, node)
382 if (likely(clkp->ops)) {
Francesco VIRLINZI50cca712009-03-13 08:08:01 +0000383 unsigned long rate = clkp->rate;
384
Francesco VIRLINZI4a550262009-03-11 07:42:05 +0000385 if (likely(clkp->ops->set_parent))
386 clkp->ops->set_parent(clkp,
387 clkp->parent);
388 if (likely(clkp->ops->set_rate))
389 clkp->ops->set_rate(clkp,
Francesco VIRLINZI50cca712009-03-13 08:08:01 +0000390 rate, NO_CHANGE);
Francesco VIRLINZI4a550262009-03-11 07:42:05 +0000391 else if (likely(clkp->ops->recalc))
392 clkp->ops->recalc(clkp);
393 }
394 }
395 break;
396 case PM_EVENT_FREEZE:
397 break;
398 case PM_EVENT_SUSPEND:
399 break;
400 }
401
402 prev_state = state;
403 return 0;
404}
405
406static int clks_sysdev_resume(struct sys_device *dev)
407{
408 return clks_sysdev_suspend(dev, PMSG_ON);
409}
410
411static struct sysdev_class clks_sysdev_class = {
412 .name = "clks",
413};
414
415static struct sysdev_driver clks_sysdev_driver = {
416 .suspend = clks_sysdev_suspend,
417 .resume = clks_sysdev_resume,
418};
419
420static struct sys_device clks_sysdev_dev = {
421 .cls = &clks_sysdev_class,
422};
423
424static int __init clk_sysdev_init(void)
425{
426 sysdev_class_register(&clks_sysdev_class);
427 sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
428 sysdev_register(&clks_sysdev_dev);
429
430 return 0;
431}
432subsys_initcall(clk_sysdev_init);
433#endif
434
Paul Mundt36ddf312006-01-16 22:14:17 -0800435int __init clk_init(void)
436{
437 int i, ret = 0;
438
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900439 BUG_ON(!master_clk.rate);
Paul Mundt36ddf312006-01-16 22:14:17 -0800440
441 for (i = 0; i < ARRAY_SIZE(onchip_clocks); i++) {
442 struct clk *clk = onchip_clocks[i];
443
444 arch_init_clk_ops(&clk->ops, i);
445 ret |= clk_register(clk);
Paul Mundt36ddf312006-01-16 22:14:17 -0800446 }
447
Paul Mundtfa439722008-09-04 18:53:58 +0900448 ret |= arch_clk_init();
dmitry pervushindfbbbe92007-05-15 08:42:22 +0900449
Paul Mundt36ddf312006-01-16 22:14:17 -0800450 /* Kick the child clocks.. */
451 propagate_rate(&master_clk);
452 propagate_rate(&bus_clk);
453
454 return ret;
455}
456
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900457static int __init clk_proc_init(void)
Paul Mundt36ddf312006-01-16 22:14:17 -0800458{
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900459 struct proc_dir_entry *p;
460 p = create_proc_read_entry("clocks", S_IRUSR, NULL,
461 show_clocks, NULL);
462 if (unlikely(!p))
463 return -EINVAL;
Paul Mundt36ddf312006-01-16 22:14:17 -0800464
465 return 0;
466}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900467subsys_initcall(clk_proc_init);