blob: e3d1de8a46fd26c1ac13980784028b134064c188 [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 Mundtaa87aa32009-05-12 05:51:05 +090084int clk_reparent(struct clk *child, struct clk *parent)
85{
86 list_del_init(&child->sibling);
87 if (parent)
88 list_add(&child->sibling, &parent->children);
89 child->parent = parent;
90
91 /* now do the debugfs renaming to reattach the child
92 to the proper parent */
93
94 return 0;
95}
96
Paul Mundtb1f6cfe2009-05-12 04:27:43 +090097/* Propagate rate to children */
98void propagate_rate(struct clk *tclk)
99{
100 struct clk *clkp;
101
102 list_for_each_entry(clkp, &tclk->children, sibling) {
103 if (clkp->ops->recalc)
104 clkp->rate = clkp->ops->recalc(clkp);
105 propagate_rate(clkp);
106 }
107}
108
Adrian Bunk4c1cfab2008-06-18 03:36:50 +0300109static void __clk_disable(struct clk *clk)
Paul Mundt36ddf312006-01-16 22:14:17 -0800110{
Paul Mundtae891a42009-05-12 05:30:10 +0900111 if (clk->usecount == 0) {
112 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
113 clk->name);
114 WARN_ON(1);
115 return;
116 }
117
118 if (!(--clk->usecount)) {
dmitry pervushin1929cb32007-04-24 13:39:09 +0900119 if (likely(clk->ops && clk->ops->disable))
120 clk->ops->disable(clk);
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900121 if (likely(clk->parent))
122 __clk_disable(clk->parent);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900123 }
Paul Mundt36ddf312006-01-16 22:14:17 -0800124}
125
126void clk_disable(struct clk *clk)
127{
128 unsigned long flags;
129
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900130 if (!clk)
131 return;
132
Paul Mundt36ddf312006-01-16 22:14:17 -0800133 spin_lock_irqsave(&clock_lock, flags);
134 __clk_disable(clk);
135 spin_unlock_irqrestore(&clock_lock, flags);
136}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900137EXPORT_SYMBOL_GPL(clk_disable);
Paul Mundt36ddf312006-01-16 22:14:17 -0800138
Paul Mundtae891a42009-05-12 05:30:10 +0900139static int __clk_enable(struct clk *clk)
140{
141 int ret = 0;
142
143 if (clk->usecount++ == 0) {
144 if (clk->parent) {
145 ret = __clk_enable(clk->parent);
146 if (unlikely(ret))
147 goto err;
148 }
149
150 if (clk->ops && clk->ops->enable) {
151 ret = clk->ops->enable(clk);
152 if (ret) {
153 if (clk->parent)
154 __clk_disable(clk->parent);
155 goto err;
156 }
157 }
158 }
159
160 return ret;
161err:
162 clk->usecount--;
163 return ret;
164}
165
166int clk_enable(struct clk *clk)
167{
168 unsigned long flags;
169 int ret;
170
171 if (!clk)
172 return -EINVAL;
173
174 spin_lock_irqsave(&clock_lock, flags);
175 ret = __clk_enable(clk);
176 spin_unlock_irqrestore(&clock_lock, flags);
177
178 return ret;
179}
180EXPORT_SYMBOL_GPL(clk_enable);
181
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900182static LIST_HEAD(root_clks);
183
184/**
185 * recalculate_root_clocks - recalculate and propagate all root clocks
186 *
187 * Recalculates all root clocks (clocks with no parent), which if the
188 * clock's .recalc is set correctly, should also propagate their rates.
189 * Called at init.
190 */
191void recalculate_root_clocks(void)
192{
193 struct clk *clkp;
194
195 list_for_each_entry(clkp, &root_clks, sibling) {
196 if (clkp->ops->recalc)
197 clkp->rate = clkp->ops->recalc(clkp);
198 propagate_rate(clkp);
199 }
200}
201
Paul Mundt36ddf312006-01-16 22:14:17 -0800202int clk_register(struct clk *clk)
203{
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900204 if (clk == NULL || IS_ERR(clk))
205 return -EINVAL;
206
207 /*
208 * trap out already registered clocks
209 */
210 if (clk->node.next || clk->node.prev)
211 return 0;
212
Paul Mundt237b98f2006-09-27 17:28:20 +0900213 mutex_lock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800214
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900215 INIT_LIST_HEAD(&clk->children);
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900216 clk->usecount = 0;
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900217
218 if (clk->parent)
219 list_add(&clk->sibling, &clk->parent->children);
220 else
221 list_add(&clk->sibling, &root_clks);
222
Paul Mundt36ddf312006-01-16 22:14:17 -0800223 list_add(&clk->node, &clock_list);
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900224 if (clk->ops->init)
225 clk->ops->init(clk);
Paul Mundt237b98f2006-09-27 17:28:20 +0900226 mutex_unlock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800227
228 return 0;
229}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900230EXPORT_SYMBOL_GPL(clk_register);
Paul Mundt36ddf312006-01-16 22:14:17 -0800231
232void clk_unregister(struct clk *clk)
233{
Paul Mundt237b98f2006-09-27 17:28:20 +0900234 mutex_lock(&clock_list_sem);
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900235 list_del(&clk->sibling);
Paul Mundt36ddf312006-01-16 22:14:17 -0800236 list_del(&clk->node);
Paul Mundt237b98f2006-09-27 17:28:20 +0900237 mutex_unlock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800238}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900239EXPORT_SYMBOL_GPL(clk_unregister);
Paul Mundt36ddf312006-01-16 22:14:17 -0800240
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900241static void clk_enable_init_clocks(void)
242{
243 struct clk *clkp;
244
245 list_for_each_entry(clkp, &clock_list, node)
246 if (clkp->flags & CLK_ENABLE_ON_INIT)
247 clk_enable(clkp);
248}
249
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900250unsigned long clk_get_rate(struct clk *clk)
Paul Mundt36ddf312006-01-16 22:14:17 -0800251{
252 return clk->rate;
253}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900254EXPORT_SYMBOL_GPL(clk_get_rate);
Paul Mundt36ddf312006-01-16 22:14:17 -0800255
256int clk_set_rate(struct clk *clk, unsigned long rate)
257{
dmitry pervushin1929cb32007-04-24 13:39:09 +0900258 return clk_set_rate_ex(clk, rate, 0);
259}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900260EXPORT_SYMBOL_GPL(clk_set_rate);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900261
262int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
263{
Paul Mundt36ddf312006-01-16 22:14:17 -0800264 int ret = -EOPNOTSUPP;
265
266 if (likely(clk->ops && clk->ops->set_rate)) {
267 unsigned long flags;
268
269 spin_lock_irqsave(&clock_lock, flags);
dmitry pervushin1929cb32007-04-24 13:39:09 +0900270 ret = clk->ops->set_rate(clk, rate, algo_id);
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900271 if (ret == 0) {
272 if (clk->ops->recalc)
273 clk->rate = clk->ops->recalc(clk);
274 propagate_rate(clk);
275 }
Paul Mundt36ddf312006-01-16 22:14:17 -0800276 spin_unlock_irqrestore(&clock_lock, flags);
277 }
278
Paul Mundt36ddf312006-01-16 22:14:17 -0800279 return ret;
280}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900281EXPORT_SYMBOL_GPL(clk_set_rate_ex);
Paul Mundt36ddf312006-01-16 22:14:17 -0800282
283void clk_recalc_rate(struct clk *clk)
284{
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900285 unsigned long flags;
Paul Mundt36ddf312006-01-16 22:14:17 -0800286
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900287 if (!clk->ops->recalc)
288 return;
Paul Mundt36ddf312006-01-16 22:14:17 -0800289
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900290 spin_lock_irqsave(&clock_lock, flags);
291 clk->rate = clk->ops->recalc(clk);
292 propagate_rate(clk);
293 spin_unlock_irqrestore(&clock_lock, flags);
Paul Mundt36ddf312006-01-16 22:14:17 -0800294}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900295EXPORT_SYMBOL_GPL(clk_recalc_rate);
Paul Mundt36ddf312006-01-16 22:14:17 -0800296
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000297int clk_set_parent(struct clk *clk, struct clk *parent)
298{
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900299 unsigned long flags;
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000300 int ret = -EINVAL;
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000301
302 if (!parent || !clk)
303 return ret;
Paul Mundtaa87aa32009-05-12 05:51:05 +0900304 if (clk->parent == parent)
305 return 0;
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000306
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900307 spin_lock_irqsave(&clock_lock, flags);
308 if (clk->usecount == 0) {
309 if (clk->ops->set_parent)
310 ret = clk->ops->set_parent(clk, parent);
Paul Mundtaa87aa32009-05-12 05:51:05 +0900311 else
312 ret = clk_reparent(clk, parent);
313
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900314 if (ret == 0) {
Paul Mundtaa87aa32009-05-12 05:51:05 +0900315 pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
316 clk->name, clk->parent->name, clk->rate);
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900317 if (clk->ops->recalc)
318 clk->rate = clk->ops->recalc(clk);
319 propagate_rate(clk);
320 }
321 } else
322 ret = -EBUSY;
323 spin_unlock_irqrestore(&clock_lock, flags);
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000324
Francesco VIRLINZId680c762009-03-11 07:40:54 +0000325 return ret;
326}
327EXPORT_SYMBOL_GPL(clk_set_parent);
328
329struct clk *clk_get_parent(struct clk *clk)
330{
331 return clk->parent;
332}
333EXPORT_SYMBOL_GPL(clk_get_parent);
334
Paul Mundtf6991b02007-07-20 13:29:09 +0900335long clk_round_rate(struct clk *clk, unsigned long rate)
336{
337 if (likely(clk->ops && clk->ops->round_rate)) {
338 unsigned long flags, rounded;
339
340 spin_lock_irqsave(&clock_lock, flags);
341 rounded = clk->ops->round_rate(clk, rate);
342 spin_unlock_irqrestore(&clock_lock, flags);
343
344 return rounded;
345 }
346
347 return clk_get_rate(clk);
348}
349EXPORT_SYMBOL_GPL(clk_round_rate);
350
Paul Mundt1d118562006-12-01 13:15:14 +0900351/*
352 * Returns a clock. Note that we first try to use device id on the bus
353 * and clock name. If this fails, we try to use clock name only.
354 */
355struct clk *clk_get(struct device *dev, const char *id)
Paul Mundt36ddf312006-01-16 22:14:17 -0800356{
357 struct clk *p, *clk = ERR_PTR(-ENOENT);
Paul Mundt1d118562006-12-01 13:15:14 +0900358 int idno;
359
360 if (dev == NULL || dev->bus != &platform_bus_type)
361 idno = -1;
362 else
363 idno = to_platform_device(dev)->id;
Paul Mundt36ddf312006-01-16 22:14:17 -0800364
Paul Mundt237b98f2006-09-27 17:28:20 +0900365 mutex_lock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800366 list_for_each_entry(p, &clock_list, node) {
Paul Mundt1d118562006-12-01 13:15:14 +0900367 if (p->id == idno &&
368 strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
369 clk = p;
370 goto found;
371 }
372 }
373
374 list_for_each_entry(p, &clock_list, node) {
Paul Mundt36ddf312006-01-16 22:14:17 -0800375 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
376 clk = p;
377 break;
378 }
379 }
Paul Mundt1d118562006-12-01 13:15:14 +0900380
381found:
Paul Mundt237b98f2006-09-27 17:28:20 +0900382 mutex_unlock(&clock_list_sem);
Paul Mundt36ddf312006-01-16 22:14:17 -0800383
384 return clk;
385}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900386EXPORT_SYMBOL_GPL(clk_get);
Paul Mundt36ddf312006-01-16 22:14:17 -0800387
388void clk_put(struct clk *clk)
389{
390 if (clk && !IS_ERR(clk))
391 module_put(clk->owner);
392}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900393EXPORT_SYMBOL_GPL(clk_put);
Paul Mundt36ddf312006-01-16 22:14:17 -0800394
395void __init __attribute__ ((weak))
396arch_init_clk_ops(struct clk_ops **ops, int type)
397{
398}
399
Paul Mundtfa439722008-09-04 18:53:58 +0900400int __init __attribute__ ((weak))
dmitry pervushindfbbbe92007-05-15 08:42:22 +0900401arch_clk_init(void)
402{
Paul Mundtfa439722008-09-04 18:53:58 +0900403 return 0;
dmitry pervushindfbbbe92007-05-15 08:42:22 +0900404}
405
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900406static int show_clocks(char *buf, char **start, off_t off,
407 int len, int *eof, void *data)
408{
409 struct clk *clk;
410 char *p = buf;
411
412 list_for_each_entry_reverse(clk, &clock_list, node) {
413 unsigned long rate = clk_get_rate(clk);
414
Magnus Damm152fe362008-07-17 19:05:54 +0900415 p += sprintf(p, "%-12s\t: %ld.%02ldMHz\t%s\n", clk->name,
416 rate / 1000000, (rate % 1000000) / 10000,
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900417 (clk->usecount > 0) ? "enabled" : "disabled");
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900418 }
419
420 return p - buf;
421}
422
Francesco VIRLINZI4a550262009-03-11 07:42:05 +0000423#ifdef CONFIG_PM
424static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
425{
426 static pm_message_t prev_state;
427 struct clk *clkp;
428
429 switch (state.event) {
430 case PM_EVENT_ON:
431 /* Resumeing from hibernation */
Paul Mundtb68d8202009-05-12 03:45:08 +0900432 if (prev_state.event != PM_EVENT_FREEZE)
433 break;
Francesco VIRLINZI50cca712009-03-13 08:08:01 +0000434
Paul Mundtb68d8202009-05-12 03:45:08 +0900435 list_for_each_entry(clkp, &clock_list, node) {
436 if (likely(clkp->ops)) {
437 unsigned long rate = clkp->rate;
438
439 if (likely(clkp->ops->set_parent))
440 clkp->ops->set_parent(clkp,
441 clkp->parent);
442 if (likely(clkp->ops->set_rate))
443 clkp->ops->set_rate(clkp,
444 rate, NO_CHANGE);
445 else if (likely(clkp->ops->recalc))
446 clkp->rate = clkp->ops->recalc(clkp);
447 }
Francesco VIRLINZI4a550262009-03-11 07:42:05 +0000448 }
449 break;
450 case PM_EVENT_FREEZE:
451 break;
452 case PM_EVENT_SUSPEND:
453 break;
454 }
455
456 prev_state = state;
457 return 0;
458}
459
460static int clks_sysdev_resume(struct sys_device *dev)
461{
462 return clks_sysdev_suspend(dev, PMSG_ON);
463}
464
465static struct sysdev_class clks_sysdev_class = {
466 .name = "clks",
467};
468
469static struct sysdev_driver clks_sysdev_driver = {
470 .suspend = clks_sysdev_suspend,
471 .resume = clks_sysdev_resume,
472};
473
474static struct sys_device clks_sysdev_dev = {
475 .cls = &clks_sysdev_class,
476};
477
478static int __init clk_sysdev_init(void)
479{
480 sysdev_class_register(&clks_sysdev_class);
481 sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
482 sysdev_register(&clks_sysdev_dev);
483
484 return 0;
485}
486subsys_initcall(clk_sysdev_init);
487#endif
488
Paul Mundt36ddf312006-01-16 22:14:17 -0800489int __init clk_init(void)
490{
491 int i, ret = 0;
492
Paul Mundte4c2cfe2006-09-27 12:31:01 +0900493 BUG_ON(!master_clk.rate);
Paul Mundt36ddf312006-01-16 22:14:17 -0800494
495 for (i = 0; i < ARRAY_SIZE(onchip_clocks); i++) {
496 struct clk *clk = onchip_clocks[i];
497
498 arch_init_clk_ops(&clk->ops, i);
499 ret |= clk_register(clk);
Paul Mundt36ddf312006-01-16 22:14:17 -0800500 }
501
Paul Mundtfa439722008-09-04 18:53:58 +0900502 ret |= arch_clk_init();
dmitry pervushindfbbbe92007-05-15 08:42:22 +0900503
Paul Mundt36ddf312006-01-16 22:14:17 -0800504 /* Kick the child clocks.. */
Paul Mundtb1f6cfe2009-05-12 04:27:43 +0900505 recalculate_root_clocks();
Paul Mundt36ddf312006-01-16 22:14:17 -0800506
Paul Mundt4ff29ff2009-05-12 05:14:53 +0900507 /* Enable the necessary init clocks */
508 clk_enable_init_clocks();
509
Paul Mundt36ddf312006-01-16 22:14:17 -0800510 return ret;
511}
512
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900513static int __init clk_proc_init(void)
Paul Mundt36ddf312006-01-16 22:14:17 -0800514{
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900515 struct proc_dir_entry *p;
516 p = create_proc_read_entry("clocks", S_IRUSR, NULL,
517 show_clocks, NULL);
518 if (unlikely(!p))
519 return -EINVAL;
Paul Mundt36ddf312006-01-16 22:14:17 -0800520
521 return 0;
522}
Paul Mundtdb62e5b2007-04-26 12:17:20 +0900523subsys_initcall(clk_proc_init);