blob: 17f6c078e85172d88cf0b32d0390ca6785156d26 [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
Paul Mundtb68d8202009-05-12 03:45:08 +090078/* Propagate rate to children */
Paul Mundt36ddf312006-01-16 22:14:17 -080079static void propagate_rate(struct clk *clk)
80{
81 struct clk *clkp;
82
83 list_for_each_entry(clkp, &clock_list, node) {
84 if (likely(clkp->parent != clk))
85 continue;
86 if (likely(clkp->ops && clkp->ops->recalc))
Paul Mundtb68d8202009-05-12 03:45:08 +090087 clkp->rate = clkp->ops->recalc(clkp);
Stuart Menefy24eb17e2007-09-28 11:51:52 +090088 if (unlikely(clkp->flags & CLK_RATE_PROPAGATES))
89 propagate_rate(clkp);
Paul Mundt36ddf312006-01-16 22:14:17 -080090 }
91}
92
Paul Mundta02cb232009-05-12 03:50:44 +090093/* Used for clocks that always have same value as the parent clock */
94unsigned long followparent_recalc(struct clk *clk)
95{
96 return clk->parent->rate;
97}
98
Magnus Damm4f5ecaa2009-05-08 08:23:29 +000099static void __clk_init(struct clk *clk)
Paul Mundt36ddf312006-01-16 22:14:17 -0800100{
101 /*
102 * See if this is the first time we're enabling the clock, some
103 * clocks that are always enabled still require "special"
104 * initialization. This is especially true if the clock mode
105 * changes and the clock needs to hunt for the proper set of
106 * divisors to use before it can effectively recalc.
107 */
Magnus Dammb3cacf32009-05-07 10:31:39 +0000108
Magnus Damm4f5ecaa2009-05-08 08:23:29 +0000109 if (clk->flags & CLK_NEEDS_INIT) {
Paul Mundt36ddf312006-01-16 22:14:17 -0800110 if (clk->ops && clk->ops->init)
111 clk->ops->init(clk);
112
Magnus Damm4f5ecaa2009-05-08 08:23:29 +0000113 clk->flags &= ~CLK_NEEDS_INIT;
114 }
115}
dmitry pervushin1929cb32007-04-24 13:39:09 +0900116
Magnus Damm4f5ecaa2009-05-08 08:23:29 +0000117static int __clk_enable(struct clk *clk)
118{
119 if (!clk)
120 return -EINVAL;
121
122 clk->usecount++;
123
124 /* nothing to do if always enabled */
125 if (clk->flags & CLK_ALWAYS_ENABLED)
126 return 0;
127
128 if (clk->usecount == 1) {
129 __clk_init(clk);
130
131 __clk_enable(clk->parent);
132
133 if (clk->ops && clk->ops->enable)
134 clk->ops->enable(clk);
135 }
Paul Mundt36ddf312006-01-16 22:14:17 -0800136
Paul Mundt36ddf312006-01-16 22:14:17 -0800137 return 0;
138}
139
140int clk_enable(struct clk *clk)
141{
142 unsigned long flags;
143 int ret;
144
145 spin_lock_irqsave(&clock_lock, flags);
146 ret = __clk_enable(clk);
147 spin_unlock_irqrestore(&clock_lock, flags);
148
149 return ret;
150}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900151EXPORT_SYMBOL_GPL(clk_enable);
Paul Mundt36ddf312006-01-16 22:14:17 -0800152
Adrian Bunk4c1cfab2008-06-18 03:36:50 +0300153static void __clk_disable(struct clk *clk)
Paul Mundt36ddf312006-01-16 22:14:17 -0800154{
Magnus Damm4f5ecaa2009-05-08 08:23:29 +0000155 if (!clk)
156 return;
157
158 clk->usecount--;
159
160 WARN_ON(clk->usecount < 0);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900161
Paul Mundt36ddf312006-01-16 22:14:17 -0800162 if (clk->flags & CLK_ALWAYS_ENABLED)
163 return;
164
Magnus Damm4f5ecaa2009-05-08 08:23:29 +0000165 if (clk->usecount == 0) {
dmitry pervushin1929cb32007-04-24 13:39:09 +0900166 if (likely(clk->ops && clk->ops->disable))
167 clk->ops->disable(clk);
Magnus Damm4f5ecaa2009-05-08 08:23:29 +0000168
169 __clk_disable(clk->parent);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900170 }
Paul Mundt36ddf312006-01-16 22:14:17 -0800171}
172
173void clk_disable(struct clk *clk)
174{
175 unsigned long flags;
176
177 spin_lock_irqsave(&clock_lock, flags);
178 __clk_disable(clk);
179 spin_unlock_irqrestore(&clock_lock, flags);
180}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900181EXPORT_SYMBOL_GPL(clk_disable);
Paul Mundt36ddf312006-01-16 22:14:17 -0800182
183int clk_register(struct clk *clk)
184{
Paul Mundt237b98f2006-09-27 17:28:20 +0900185 mutex_lock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800186
187 list_add(&clk->node, &clock_list);
Magnus Damm4f5ecaa2009-05-08 08:23:29 +0000188 clk->usecount = 0;
189 clk->flags |= CLK_NEEDS_INIT;
Paul Mundt36ddf312006-01-16 22:14:17 -0800190
Paul Mundt237b98f2006-09-27 17:28:20 +0900191 mutex_unlock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800192
dmitry pervushin1929cb32007-04-24 13:39:09 +0900193 if (clk->flags & CLK_ALWAYS_ENABLED) {
Magnus Damm4f5ecaa2009-05-08 08:23:29 +0000194 __clk_init(clk);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900195 pr_debug( "Clock '%s' is ALWAYS_ENABLED\n", clk->name);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900196 if (clk->ops && clk->ops->enable)
197 clk->ops->enable(clk);
198 pr_debug( "Enabled.");
199 }
200
Paul Mundt36ddf312006-01-16 22:14:17 -0800201 return 0;
202}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900203EXPORT_SYMBOL_GPL(clk_register);
Paul Mundt36ddf312006-01-16 22:14:17 -0800204
205void clk_unregister(struct clk *clk)
206{
Paul Mundt237b98f2006-09-27 17:28:20 +0900207 mutex_lock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800208 list_del(&clk->node);
Paul Mundt237b98f2006-09-27 17:28:20 +0900209 mutex_unlock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800210}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900211EXPORT_SYMBOL_GPL(clk_unregister);
Paul Mundt36ddf312006-01-16 22:14:17 -0800212
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900213unsigned long clk_get_rate(struct clk *clk)
Paul Mundt36ddf312006-01-16 22:14:17 -0800214{
215 return clk->rate;
216}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900217EXPORT_SYMBOL_GPL(clk_get_rate);
Paul Mundt36ddf312006-01-16 22:14:17 -0800218
219int clk_set_rate(struct clk *clk, unsigned long rate)
220{
dmitry pervushin1929cb32007-04-24 13:39:09 +0900221 return clk_set_rate_ex(clk, rate, 0);
222}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900223EXPORT_SYMBOL_GPL(clk_set_rate);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900224
225int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
226{
Paul Mundt36ddf312006-01-16 22:14:17 -0800227 int ret = -EOPNOTSUPP;
228
229 if (likely(clk->ops && clk->ops->set_rate)) {
230 unsigned long flags;
231
232 spin_lock_irqsave(&clock_lock, flags);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900233 ret = clk->ops->set_rate(clk, rate, algo_id);
Paul Mundt36ddf312006-01-16 22:14:17 -0800234 spin_unlock_irqrestore(&clock_lock, flags);
235 }
236
237 if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
238 propagate_rate(clk);
239
240 return ret;
241}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900242EXPORT_SYMBOL_GPL(clk_set_rate_ex);
Paul Mundt36ddf312006-01-16 22:14:17 -0800243
244void clk_recalc_rate(struct clk *clk)
245{
246 if (likely(clk->ops && clk->ops->recalc)) {
247 unsigned long flags;
248
249 spin_lock_irqsave(&clock_lock, flags);
Paul Mundtb68d8202009-05-12 03:45:08 +0900250 clk->rate = clk->ops->recalc(clk);
Paul Mundt36ddf312006-01-16 22:14:17 -0800251 spin_unlock_irqrestore(&clock_lock, flags);
252 }
253
254 if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
255 propagate_rate(clk);
256}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900257EXPORT_SYMBOL_GPL(clk_recalc_rate);
Paul Mundt36ddf312006-01-16 22:14:17 -0800258
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000259int clk_set_parent(struct clk *clk, struct clk *parent)
260{
261 int ret = -EINVAL;
262 struct clk *old;
263
264 if (!parent || !clk)
265 return ret;
266
267 old = clk->parent;
268 if (likely(clk->ops && clk->ops->set_parent)) {
269 unsigned long flags;
270 spin_lock_irqsave(&clock_lock, flags);
271 ret = clk->ops->set_parent(clk, parent);
272 spin_unlock_irqrestore(&clock_lock, flags);
273 clk->parent = (ret ? old : parent);
274 }
275
276 if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
277 propagate_rate(clk);
278 return ret;
279}
280EXPORT_SYMBOL_GPL(clk_set_parent);
281
282struct clk *clk_get_parent(struct clk *clk)
283{
284 return clk->parent;
285}
286EXPORT_SYMBOL_GPL(clk_get_parent);
287
Paul Mundtf6991b02007-07-20 13:29:09 +0900288long clk_round_rate(struct clk *clk, unsigned long rate)
289{
290 if (likely(clk->ops && clk->ops->round_rate)) {
291 unsigned long flags, rounded;
292
293 spin_lock_irqsave(&clock_lock, flags);
294 rounded = clk->ops->round_rate(clk, rate);
295 spin_unlock_irqrestore(&clock_lock, flags);
296
297 return rounded;
298 }
299
300 return clk_get_rate(clk);
301}
302EXPORT_SYMBOL_GPL(clk_round_rate);
303
Paul Mundt1d118562006-12-01 13:15:14 +0900304/*
305 * Returns a clock. Note that we first try to use device id on the bus
306 * and clock name. If this fails, we try to use clock name only.
307 */
308struct clk *clk_get(struct device *dev, const char *id)
Paul Mundt36ddf312006-01-16 22:14:17 -0800309{
310 struct clk *p, *clk = ERR_PTR(-ENOENT);
Paul Mundt1d118562006-12-01 13:15:14 +0900311 int idno;
312
313 if (dev == NULL || dev->bus != &platform_bus_type)
314 idno = -1;
315 else
316 idno = to_platform_device(dev)->id;
Paul Mundt36ddf312006-01-16 22:14:17 -0800317
Paul Mundt237b98f2006-09-27 17:28:20 +0900318 mutex_lock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800319 list_for_each_entry(p, &clock_list, node) {
Paul Mundt1d118562006-12-01 13:15:14 +0900320 if (p->id == idno &&
321 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
322 clk = p;
323 goto found;
324 }
325 }
326
327 list_for_each_entry(p, &clock_list, node) {
Paul Mundt36ddf312006-01-16 22:14:17 -0800328 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
329 clk = p;
330 break;
331 }
332 }
Paul Mundt1d118562006-12-01 13:15:14 +0900333
334found:
Paul Mundt237b98f2006-09-27 17:28:20 +0900335 mutex_unlock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800336
337 return clk;
338}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900339EXPORT_SYMBOL_GPL(clk_get);
Paul Mundt36ddf312006-01-16 22:14:17 -0800340
341void clk_put(struct clk *clk)
342{
343 if (clk && !IS_ERR(clk))
344 module_put(clk->owner);
345}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900346EXPORT_SYMBOL_GPL(clk_put);
Paul Mundt36ddf312006-01-16 22:14:17 -0800347
348void __init __attribute__ ((weak))
349arch_init_clk_ops(struct clk_ops **ops, int type)
350{
351}
352
Paul Mundtfa439722008-09-04 18:53:58 +0900353int __init __attribute__ ((weak))
dmitry pervushindfbbbe92007-05-15 08:42:22 +0900354arch_clk_init(void)
355{
Paul Mundtfa439722008-09-04 18:53:58 +0900356 return 0;
dmitry pervushindfbbbe92007-05-15 08:42:22 +0900357}
358
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900359static int show_clocks(char *buf, char **start, off_t off,
360 int len, int *eof, void *data)
361{
362 struct clk *clk;
363 char *p = buf;
364
365 list_for_each_entry_reverse(clk, &clock_list, node) {
366 unsigned long rate = clk_get_rate(clk);
367
Magnus Damm152fe362008-07-17 19:05:54 +0900368 p += sprintf(p, "%-12s\t: %ld.%02ldMHz\t%s\n", clk->name,
369 rate / 1000000, (rate % 1000000) / 10000,
370 ((clk->flags & CLK_ALWAYS_ENABLED) ||
Magnus Damm4f5ecaa2009-05-08 08:23:29 +0000371 clk->usecount > 0) ?
Magnus Damm152fe362008-07-17 19:05:54 +0900372 "enabled" : "disabled");
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900373 }
374
375 return p - buf;
376}
377
Francesco VIRLINZI4a550262009-03-11 07:42:05 +0000378#ifdef CONFIG_PM
379static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
380{
381 static pm_message_t prev_state;
382 struct clk *clkp;
383
384 switch (state.event) {
385 case PM_EVENT_ON:
386 /* Resumeing from hibernation */
Paul Mundtb68d8202009-05-12 03:45:08 +0900387 if (prev_state.event != PM_EVENT_FREEZE)
388 break;
Francesco VIRLINZI50cca712009-03-13 08:08:01 +0000389
Paul Mundtb68d8202009-05-12 03:45:08 +0900390 list_for_each_entry(clkp, &clock_list, node) {
391 if (likely(clkp->ops)) {
392 unsigned long rate = clkp->rate;
393
394 if (likely(clkp->ops->set_parent))
395 clkp->ops->set_parent(clkp,
396 clkp->parent);
397 if (likely(clkp->ops->set_rate))
398 clkp->ops->set_rate(clkp,
399 rate, NO_CHANGE);
400 else if (likely(clkp->ops->recalc))
401 clkp->rate = clkp->ops->recalc(clkp);
402 }
Francesco VIRLINZI4a550262009-03-11 07:42:05 +0000403 }
404 break;
405 case PM_EVENT_FREEZE:
406 break;
407 case PM_EVENT_SUSPEND:
408 break;
409 }
410
411 prev_state = state;
412 return 0;
413}
414
415static int clks_sysdev_resume(struct sys_device *dev)
416{
417 return clks_sysdev_suspend(dev, PMSG_ON);
418}
419
420static struct sysdev_class clks_sysdev_class = {
421 .name = "clks",
422};
423
424static struct sysdev_driver clks_sysdev_driver = {
425 .suspend = clks_sysdev_suspend,
426 .resume = clks_sysdev_resume,
427};
428
429static struct sys_device clks_sysdev_dev = {
430 .cls = &clks_sysdev_class,
431};
432
433static int __init clk_sysdev_init(void)
434{
435 sysdev_class_register(&clks_sysdev_class);
436 sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
437 sysdev_register(&clks_sysdev_dev);
438
439 return 0;
440}
441subsys_initcall(clk_sysdev_init);
442#endif
443
Paul Mundt36ddf312006-01-16 22:14:17 -0800444int __init clk_init(void)
445{
446 int i, ret = 0;
447
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900448 BUG_ON(!master_clk.rate);
Paul Mundt36ddf312006-01-16 22:14:17 -0800449
450 for (i = 0; i < ARRAY_SIZE(onchip_clocks); i++) {
451 struct clk *clk = onchip_clocks[i];
452
453 arch_init_clk_ops(&clk->ops, i);
454 ret |= clk_register(clk);
Paul Mundt36ddf312006-01-16 22:14:17 -0800455 }
456
Paul Mundtfa439722008-09-04 18:53:58 +0900457 ret |= arch_clk_init();
dmitry pervushindfbbbe92007-05-15 08:42:22 +0900458
Paul Mundt36ddf312006-01-16 22:14:17 -0800459 /* Kick the child clocks.. */
460 propagate_rate(&master_clk);
461 propagate_rate(&bus_clk);
462
463 return ret;
464}
465
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900466static int __init clk_proc_init(void)
Paul Mundt36ddf312006-01-16 22:14:17 -0800467{
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900468 struct proc_dir_entry *p;
469 p = create_proc_read_entry("clocks", S_IRUSR, NULL,
470 show_clocks, NULL);
471 if (unlikely(!p))
472 return -EINVAL;
Paul Mundt36ddf312006-01-16 22:14:17 -0800473
474 return 0;
475}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900476subsys_initcall(clk_proc_init);