blob: c683be5ba8b2b6e930640405b7bd68d9c0cceedb [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 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",
Paul Mundt4ff29ff2009-05-12 05:14:53 +090046 .flags = CLK_ENABLE_ON_INIT,
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,
Paul Mundt4ff29ff2009-05-12 05:14:53 +090053 .flags = CLK_ENABLE_ON_INIT,
Paul Mundt36ddf312006-01-16 22:14:17 -080054};
55
56static struct clk bus_clk = {
57 .name = "bus_clk",
58 .parent = &master_clk,
Paul Mundt4ff29ff2009-05-12 05:14:53 +090059 .flags = CLK_ENABLE_ON_INIT,
Paul Mundt36ddf312006-01-16 22:14:17 -080060};
61
62static struct clk cpu_clk = {
63 .name = "cpu_clk",
64 .parent = &master_clk,
Paul Mundt4ff29ff2009-05-12 05:14:53 +090065 .flags = CLK_ENABLE_ON_INIT,
Paul Mundt36ddf312006-01-16 22:14:17 -080066};
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 Mundta02cb232009-05-12 03:50:44 +090078/* Used for clocks that always have same value as the parent clock */
79unsigned long followparent_recalc(struct clk *clk)
80{
81 return clk->parent->rate;
82}
83
Paul Mundtb1f6cfe2009-05-12 04:27:43 +090084/* Propagate rate to children */
85void propagate_rate(struct clk *tclk)
86{
87 struct clk *clkp;
88
89 list_for_each_entry(clkp, &tclk->children, sibling) {
90 if (clkp->ops->recalc)
91 clkp->rate = clkp->ops->recalc(clkp);
92 propagate_rate(clkp);
93 }
94}
95
Magnus Damm4f5ecaa2009-05-08 08:23:29 +000096static int __clk_enable(struct clk *clk)
97{
Paul Mundt4ff29ff2009-05-12 05:14:53 +090098 if (clk->usecount++ == 0) {
99 if (clk->parent)
100 __clk_enable(clk->parent);
Magnus Damm4f5ecaa2009-05-08 08:23:29 +0000101
102 if (clk->ops && clk->ops->enable)
103 clk->ops->enable(clk);
104 }
Paul Mundt36ddf312006-01-16 22:14:17 -0800105
Paul Mundt36ddf312006-01-16 22:14:17 -0800106 return 0;
107}
108
109int clk_enable(struct clk *clk)
110{
111 unsigned long flags;
112 int ret;
113
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900114 if (!clk)
115 return -EINVAL;
116
Paul Mundt36ddf312006-01-16 22:14:17 -0800117 spin_lock_irqsave(&clock_lock, flags);
118 ret = __clk_enable(clk);
119 spin_unlock_irqrestore(&clock_lock, flags);
120
121 return ret;
122}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900123EXPORT_SYMBOL_GPL(clk_enable);
Paul Mundt36ddf312006-01-16 22:14:17 -0800124
Adrian Bunk4c1cfab2008-06-18 03:36:50 +0300125static void __clk_disable(struct clk *clk)
Paul Mundt36ddf312006-01-16 22:14:17 -0800126{
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900127 if (clk->usecount > 0 && !(--clk->usecount)) {
dmitry pervushin1929cb32007-04-24 13:39:09 +0900128 if (likely(clk->ops && clk->ops->disable))
129 clk->ops->disable(clk);
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900130 if (likely(clk->parent))
131 __clk_disable(clk->parent);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900132 }
Paul Mundt36ddf312006-01-16 22:14:17 -0800133}
134
135void clk_disable(struct clk *clk)
136{
137 unsigned long flags;
138
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900139 if (!clk)
140 return;
141
Paul Mundt36ddf312006-01-16 22:14:17 -0800142 spin_lock_irqsave(&clock_lock, flags);
143 __clk_disable(clk);
144 spin_unlock_irqrestore(&clock_lock, flags);
145}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900146EXPORT_SYMBOL_GPL(clk_disable);
Paul Mundt36ddf312006-01-16 22:14:17 -0800147
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900148static LIST_HEAD(root_clks);
149
150/**
151 * recalculate_root_clocks - recalculate and propagate all root clocks
152 *
153 * Recalculates all root clocks (clocks with no parent), which if the
154 * clock's .recalc is set correctly, should also propagate their rates.
155 * Called at init.
156 */
157void recalculate_root_clocks(void)
158{
159 struct clk *clkp;
160
161 list_for_each_entry(clkp, &root_clks, sibling) {
162 if (clkp->ops->recalc)
163 clkp->rate = clkp->ops->recalc(clkp);
164 propagate_rate(clkp);
165 }
166}
167
Paul Mundt36ddf312006-01-16 22:14:17 -0800168int clk_register(struct clk *clk)
169{
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900170 if (clk == NULL || IS_ERR(clk))
171 return -EINVAL;
172
173 /*
174 * trap out already registered clocks
175 */
176 if (clk->node.next || clk->node.prev)
177 return 0;
178
Paul Mundt237b98f2006-09-27 17:28:20 +0900179 mutex_lock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800180
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900181 INIT_LIST_HEAD(&clk->children);
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900182 clk->usecount = 0;
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900183
184 if (clk->parent)
185 list_add(&clk->sibling, &clk->parent->children);
186 else
187 list_add(&clk->sibling, &root_clks);
188
Paul Mundt36ddf312006-01-16 22:14:17 -0800189 list_add(&clk->node, &clock_list);
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900190 if (clk->ops->init)
191 clk->ops->init(clk);
Paul Mundt237b98f2006-09-27 17:28:20 +0900192 mutex_unlock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800193
194 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 Mundtb1f6cfe2009-05-12 04:27:43 +0900201 list_del(&clk->sibling);
Paul Mundt36ddf312006-01-16 22:14:17 -0800202 list_del(&clk->node);
Paul Mundt237b98f2006-09-27 17:28:20 +0900203 mutex_unlock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800204}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900205EXPORT_SYMBOL_GPL(clk_unregister);
Paul Mundt36ddf312006-01-16 22:14:17 -0800206
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900207static void clk_enable_init_clocks(void)
208{
209 struct clk *clkp;
210
211 list_for_each_entry(clkp, &clock_list, node)
212 if (clkp->flags & CLK_ENABLE_ON_INIT)
213 clk_enable(clkp);
214}
215
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900216unsigned long clk_get_rate(struct clk *clk)
Paul Mundt36ddf312006-01-16 22:14:17 -0800217{
218 return clk->rate;
219}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900220EXPORT_SYMBOL_GPL(clk_get_rate);
Paul Mundt36ddf312006-01-16 22:14:17 -0800221
222int clk_set_rate(struct clk *clk, unsigned long rate)
223{
dmitry pervushin1929cb32007-04-24 13:39:09 +0900224 return clk_set_rate_ex(clk, rate, 0);
225}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900226EXPORT_SYMBOL_GPL(clk_set_rate);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900227
228int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
229{
Paul Mundt36ddf312006-01-16 22:14:17 -0800230 int ret = -EOPNOTSUPP;
231
232 if (likely(clk->ops && clk->ops->set_rate)) {
233 unsigned long flags;
234
235 spin_lock_irqsave(&clock_lock, flags);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900236 ret = clk->ops->set_rate(clk, rate, algo_id);
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900237 if (ret == 0) {
238 if (clk->ops->recalc)
239 clk->rate = clk->ops->recalc(clk);
240 propagate_rate(clk);
241 }
Paul Mundt36ddf312006-01-16 22:14:17 -0800242 spin_unlock_irqrestore(&clock_lock, flags);
243 }
244
Paul Mundt36ddf312006-01-16 22:14:17 -0800245 return ret;
246}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900247EXPORT_SYMBOL_GPL(clk_set_rate_ex);
Paul Mundt36ddf312006-01-16 22:14:17 -0800248
249void clk_recalc_rate(struct clk *clk)
250{
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900251 unsigned long flags;
Paul Mundt36ddf312006-01-16 22:14:17 -0800252
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900253 if (!clk->ops->recalc)
254 return;
Paul Mundt36ddf312006-01-16 22:14:17 -0800255
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900256 spin_lock_irqsave(&clock_lock, flags);
257 clk->rate = clk->ops->recalc(clk);
258 propagate_rate(clk);
259 spin_unlock_irqrestore(&clock_lock, flags);
Paul Mundt36ddf312006-01-16 22:14:17 -0800260}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900261EXPORT_SYMBOL_GPL(clk_recalc_rate);
Paul Mundt36ddf312006-01-16 22:14:17 -0800262
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000263int clk_set_parent(struct clk *clk, struct clk *parent)
264{
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900265 unsigned long flags;
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000266 int ret = -EINVAL;
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000267
268 if (!parent || !clk)
269 return ret;
270
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900271 spin_lock_irqsave(&clock_lock, flags);
272 if (clk->usecount == 0) {
273 if (clk->ops->set_parent)
274 ret = clk->ops->set_parent(clk, parent);
275 if (ret == 0) {
276 if (clk->ops->recalc)
277 clk->rate = clk->ops->recalc(clk);
278 propagate_rate(clk);
279 }
280 } else
281 ret = -EBUSY;
282 spin_unlock_irqrestore(&clock_lock, flags);
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000283
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000284 return ret;
285}
286EXPORT_SYMBOL_GPL(clk_set_parent);
287
288struct clk *clk_get_parent(struct clk *clk)
289{
290 return clk->parent;
291}
292EXPORT_SYMBOL_GPL(clk_get_parent);
293
Paul Mundtf6991b02007-07-20 13:29:09 +0900294long clk_round_rate(struct clk *clk, unsigned long rate)
295{
296 if (likely(clk->ops && clk->ops->round_rate)) {
297 unsigned long flags, rounded;
298
299 spin_lock_irqsave(&clock_lock, flags);
300 rounded = clk->ops->round_rate(clk, rate);
301 spin_unlock_irqrestore(&clock_lock, flags);
302
303 return rounded;
304 }
305
306 return clk_get_rate(clk);
307}
308EXPORT_SYMBOL_GPL(clk_round_rate);
309
Paul Mundt1d118562006-12-01 13:15:14 +0900310/*
311 * Returns a clock. Note that we first try to use device id on the bus
312 * and clock name. If this fails, we try to use clock name only.
313 */
314struct clk *clk_get(struct device *dev, const char *id)
Paul Mundt36ddf312006-01-16 22:14:17 -0800315{
316 struct clk *p, *clk = ERR_PTR(-ENOENT);
Paul Mundt1d118562006-12-01 13:15:14 +0900317 int idno;
318
319 if (dev == NULL || dev->bus != &platform_bus_type)
320 idno = -1;
321 else
322 idno = to_platform_device(dev)->id;
Paul Mundt36ddf312006-01-16 22:14:17 -0800323
Paul Mundt237b98f2006-09-27 17:28:20 +0900324 mutex_lock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800325 list_for_each_entry(p, &clock_list, node) {
Paul Mundt1d118562006-12-01 13:15:14 +0900326 if (p->id == idno &&
327 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
328 clk = p;
329 goto found;
330 }
331 }
332
333 list_for_each_entry(p, &clock_list, node) {
Paul Mundt36ddf312006-01-16 22:14:17 -0800334 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
335 clk = p;
336 break;
337 }
338 }
Paul Mundt1d118562006-12-01 13:15:14 +0900339
340found:
Paul Mundt237b98f2006-09-27 17:28:20 +0900341 mutex_unlock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800342
343 return clk;
344}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900345EXPORT_SYMBOL_GPL(clk_get);
Paul Mundt36ddf312006-01-16 22:14:17 -0800346
347void clk_put(struct clk *clk)
348{
349 if (clk && !IS_ERR(clk))
350 module_put(clk->owner);
351}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900352EXPORT_SYMBOL_GPL(clk_put);
Paul Mundt36ddf312006-01-16 22:14:17 -0800353
354void __init __attribute__ ((weak))
355arch_init_clk_ops(struct clk_ops **ops, int type)
356{
357}
358
Paul Mundtfa439722008-09-04 18:53:58 +0900359int __init __attribute__ ((weak))
dmitry pervushindfbbbe92007-05-15 08:42:22 +0900360arch_clk_init(void)
361{
Paul Mundtfa439722008-09-04 18:53:58 +0900362 return 0;
dmitry pervushindfbbbe92007-05-15 08:42:22 +0900363}
364
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900365static int show_clocks(char *buf, char **start, off_t off,
366 int len, int *eof, void *data)
367{
368 struct clk *clk;
369 char *p = buf;
370
371 list_for_each_entry_reverse(clk, &clock_list, node) {
372 unsigned long rate = clk_get_rate(clk);
373
Magnus Damm152fe362008-07-17 19:05:54 +0900374 p += sprintf(p, "%-12s\t: %ld.%02ldMHz\t%s\n", clk->name,
375 rate / 1000000, (rate % 1000000) / 10000,
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900376 (clk->usecount > 0) ? "enabled" : "disabled");
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900377 }
378
379 return p - buf;
380}
381
Francesco VIRLINZI4a550262009-03-11 07:42:05 +0000382#ifdef CONFIG_PM
383static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
384{
385 static pm_message_t prev_state;
386 struct clk *clkp;
387
388 switch (state.event) {
389 case PM_EVENT_ON:
390 /* Resumeing from hibernation */
Paul Mundtb68d8202009-05-12 03:45:08 +0900391 if (prev_state.event != PM_EVENT_FREEZE)
392 break;
Francesco VIRLINZI50cca712009-03-13 08:08:01 +0000393
Paul Mundtb68d8202009-05-12 03:45:08 +0900394 list_for_each_entry(clkp, &clock_list, node) {
395 if (likely(clkp->ops)) {
396 unsigned long rate = clkp->rate;
397
398 if (likely(clkp->ops->set_parent))
399 clkp->ops->set_parent(clkp,
400 clkp->parent);
401 if (likely(clkp->ops->set_rate))
402 clkp->ops->set_rate(clkp,
403 rate, NO_CHANGE);
404 else if (likely(clkp->ops->recalc))
405 clkp->rate = clkp->ops->recalc(clkp);
406 }
Francesco VIRLINZI4a550262009-03-11 07:42:05 +0000407 }
408 break;
409 case PM_EVENT_FREEZE:
410 break;
411 case PM_EVENT_SUSPEND:
412 break;
413 }
414
415 prev_state = state;
416 return 0;
417}
418
419static int clks_sysdev_resume(struct sys_device *dev)
420{
421 return clks_sysdev_suspend(dev, PMSG_ON);
422}
423
424static struct sysdev_class clks_sysdev_class = {
425 .name = "clks",
426};
427
428static struct sysdev_driver clks_sysdev_driver = {
429 .suspend = clks_sysdev_suspend,
430 .resume = clks_sysdev_resume,
431};
432
433static struct sys_device clks_sysdev_dev = {
434 .cls = &clks_sysdev_class,
435};
436
437static int __init clk_sysdev_init(void)
438{
439 sysdev_class_register(&clks_sysdev_class);
440 sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
441 sysdev_register(&clks_sysdev_dev);
442
443 return 0;
444}
445subsys_initcall(clk_sysdev_init);
446#endif
447
Paul Mundt36ddf312006-01-16 22:14:17 -0800448int __init clk_init(void)
449{
450 int i, ret = 0;
451
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900452 BUG_ON(!master_clk.rate);
Paul Mundt36ddf312006-01-16 22:14:17 -0800453
454 for (i = 0; i < ARRAY_SIZE(onchip_clocks); i++) {
455 struct clk *clk = onchip_clocks[i];
456
457 arch_init_clk_ops(&clk->ops, i);
458 ret |= clk_register(clk);
Paul Mundt36ddf312006-01-16 22:14:17 -0800459 }
460
Paul Mundtfa439722008-09-04 18:53:58 +0900461 ret |= arch_clk_init();
dmitry pervushindfbbbe92007-05-15 08:42:22 +0900462
Paul Mundt36ddf312006-01-16 22:14:17 -0800463 /* Kick the child clocks.. */
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900464 recalculate_root_clocks();
Paul Mundt36ddf312006-01-16 22:14:17 -0800465
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900466 /* Enable the necessary init clocks */
467 clk_enable_init_clocks();
468
Paul Mundt36ddf312006-01-16 22:14:17 -0800469 return ret;
470}
471
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900472static int __init clk_proc_init(void)
Paul Mundt36ddf312006-01-16 22:14:17 -0800473{
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900474 struct proc_dir_entry *p;
475 p = create_proc_read_entry("clocks", S_IRUSR, NULL,
476 show_clocks, NULL);
477 if (unlikely(!p))
478 return -EINVAL;
Paul Mundt36ddf312006-01-16 22:14:17 -0800479
480 return 0;
481}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900482subsys_initcall(clk_proc_init);