sh: clkfwk: Handle NULL clkops for root clocks.
[linux-2.6] / arch / sh / kernel / cpu / clock.c
1 /*
2  * arch/sh/kernel/cpu/clock.c - SuperH clock framework
3  *
4  *  Copyright (C) 2005 - 2009  Paul Mundt
5  *
6  * This clock framework is derived from the OMAP version by:
7  *
8  *      Copyright (C) 2004 - 2008 Nokia Corporation
9  *      Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
10  *
11  *  Modified for omap shared clock framework by Tony Lindgren <tony@atomide.com>
12  *
13  *  With clkdev bits:
14  *
15  *      Copyright (C) 2008 Russell King.
16  *
17  * 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>
24 #include <linux/mutex.h>
25 #include <linux/list.h>
26 #include <linux/kobject.h>
27 #include <linux/sysdev.h>
28 #include <linux/seq_file.h>
29 #include <linux/err.h>
30 #include <linux/platform_device.h>
31 #include <linux/proc_fs.h>
32 #include <asm/clock.h>
33
34 static LIST_HEAD(clock_list);
35 static DEFINE_SPINLOCK(clock_lock);
36 static DEFINE_MUTEX(clock_list_sem);
37
38 /*
39  * Each subtype is expected to define the init routines for these clocks,
40  * as each subtype (or processor family) will have these clocks at the
41  * very least. These are all provided through the CPG, which even some of
42  * the more quirky parts (such as ST40, SH4-202, etc.) still have.
43  *
44  * The processor-specific code is expected to register any additional
45  * clock sources that are of interest.
46  */
47 static struct clk master_clk = {
48         .name           = "master_clk",
49         .flags          = CLK_ENABLE_ON_INIT,
50         .rate           = CONFIG_SH_PCLK_FREQ,
51 };
52
53 static struct clk peripheral_clk = {
54         .name           = "peripheral_clk",
55         .parent         = &master_clk,
56         .flags          = CLK_ENABLE_ON_INIT,
57 };
58
59 static struct clk bus_clk = {
60         .name           = "bus_clk",
61         .parent         = &master_clk,
62         .flags          = CLK_ENABLE_ON_INIT,
63 };
64
65 static struct clk cpu_clk = {
66         .name           = "cpu_clk",
67         .parent         = &master_clk,
68         .flags          = CLK_ENABLE_ON_INIT,
69 };
70
71 /*
72  * The ordering of these clocks matters, do not change it.
73  */
74 static struct clk *onchip_clocks[] = {
75         &master_clk,
76         &peripheral_clk,
77         &bus_clk,
78         &cpu_clk,
79 };
80
81 /* Used for clocks that always have same value as the parent clock */
82 unsigned long followparent_recalc(struct clk *clk)
83 {
84         return clk->parent->rate;
85 }
86
87 int clk_reparent(struct clk *child, struct clk *parent)
88 {
89         list_del_init(&child->sibling);
90         if (parent)
91                 list_add(&child->sibling, &parent->children);
92         child->parent = parent;
93
94         /* now do the debugfs renaming to reattach the child
95            to the proper parent */
96
97         return 0;
98 }
99
100 /* Propagate rate to children */
101 void propagate_rate(struct clk *tclk)
102 {
103         struct clk *clkp;
104
105         list_for_each_entry(clkp, &tclk->children, sibling) {
106                 if (clkp->ops && clkp->ops->recalc)
107                         clkp->rate = clkp->ops->recalc(clkp);
108                 propagate_rate(clkp);
109         }
110 }
111
112 static void __clk_disable(struct clk *clk)
113 {
114         if (clk->usecount == 0) {
115                 printk(KERN_ERR "Trying disable clock %s with 0 usecount\n",
116                        clk->name);
117                 WARN_ON(1);
118                 return;
119         }
120
121         if (!(--clk->usecount)) {
122                 if (likely(clk->ops && clk->ops->disable))
123                         clk->ops->disable(clk);
124                 if (likely(clk->parent))
125                         __clk_disable(clk->parent);
126         }
127 }
128
129 void clk_disable(struct clk *clk)
130 {
131         unsigned long flags;
132
133         if (!clk)
134                 return;
135
136         spin_lock_irqsave(&clock_lock, flags);
137         __clk_disable(clk);
138         spin_unlock_irqrestore(&clock_lock, flags);
139 }
140 EXPORT_SYMBOL_GPL(clk_disable);
141
142 static int __clk_enable(struct clk *clk)
143 {
144         int ret = 0;
145
146         if (clk->usecount++ == 0) {
147                 if (clk->parent) {
148                         ret = __clk_enable(clk->parent);
149                         if (unlikely(ret))
150                                 goto err;
151                 }
152
153                 if (clk->ops && clk->ops->enable) {
154                         ret = clk->ops->enable(clk);
155                         if (ret) {
156                                 if (clk->parent)
157                                         __clk_disable(clk->parent);
158                                 goto err;
159                         }
160                 }
161         }
162
163         return ret;
164 err:
165         clk->usecount--;
166         return ret;
167 }
168
169 int clk_enable(struct clk *clk)
170 {
171         unsigned long flags;
172         int ret;
173
174         if (!clk)
175                 return -EINVAL;
176
177         spin_lock_irqsave(&clock_lock, flags);
178         ret = __clk_enable(clk);
179         spin_unlock_irqrestore(&clock_lock, flags);
180
181         return ret;
182 }
183 EXPORT_SYMBOL_GPL(clk_enable);
184
185 static LIST_HEAD(root_clks);
186
187 /**
188  * recalculate_root_clocks - recalculate and propagate all root clocks
189  *
190  * Recalculates all root clocks (clocks with no parent), which if the
191  * clock's .recalc is set correctly, should also propagate their rates.
192  * Called at init.
193  */
194 void recalculate_root_clocks(void)
195 {
196         struct clk *clkp;
197
198         list_for_each_entry(clkp, &root_clks, sibling) {
199                 if (clkp->ops && clkp->ops->recalc)
200                         clkp->rate = clkp->ops->recalc(clkp);
201                 propagate_rate(clkp);
202         }
203 }
204
205 int clk_register(struct clk *clk)
206 {
207         if (clk == NULL || IS_ERR(clk))
208                 return -EINVAL;
209
210         /*
211          * trap out already registered clocks
212          */
213         if (clk->node.next || clk->node.prev)
214                 return 0;
215
216         mutex_lock(&clock_list_sem);
217
218         INIT_LIST_HEAD(&clk->children);
219         clk->usecount = 0;
220
221         if (clk->parent)
222                 list_add(&clk->sibling, &clk->parent->children);
223         else
224                 list_add(&clk->sibling, &root_clks);
225
226         list_add(&clk->node, &clock_list);
227         if (clk->ops && clk->ops->init)
228                 clk->ops->init(clk);
229         mutex_unlock(&clock_list_sem);
230
231         return 0;
232 }
233 EXPORT_SYMBOL_GPL(clk_register);
234
235 void clk_unregister(struct clk *clk)
236 {
237         mutex_lock(&clock_list_sem);
238         list_del(&clk->sibling);
239         list_del(&clk->node);
240         mutex_unlock(&clock_list_sem);
241 }
242 EXPORT_SYMBOL_GPL(clk_unregister);
243
244 static void clk_enable_init_clocks(void)
245 {
246         struct clk *clkp;
247
248         list_for_each_entry(clkp, &clock_list, node)
249                 if (clkp->flags & CLK_ENABLE_ON_INIT)
250                         clk_enable(clkp);
251 }
252
253 unsigned long clk_get_rate(struct clk *clk)
254 {
255         return clk->rate;
256 }
257 EXPORT_SYMBOL_GPL(clk_get_rate);
258
259 int clk_set_rate(struct clk *clk, unsigned long rate)
260 {
261         return clk_set_rate_ex(clk, rate, 0);
262 }
263 EXPORT_SYMBOL_GPL(clk_set_rate);
264
265 int clk_set_rate_ex(struct clk *clk, unsigned long rate, int algo_id)
266 {
267         int ret = -EOPNOTSUPP;
268
269         if (likely(clk->ops && clk->ops->set_rate)) {
270                 unsigned long flags;
271
272                 spin_lock_irqsave(&clock_lock, flags);
273                 ret = clk->ops->set_rate(clk, rate, algo_id);
274                 if (ret == 0) {
275                         if (clk->ops->recalc)
276                                 clk->rate = clk->ops->recalc(clk);
277                         propagate_rate(clk);
278                 }
279                 spin_unlock_irqrestore(&clock_lock, flags);
280         }
281
282         return ret;
283 }
284 EXPORT_SYMBOL_GPL(clk_set_rate_ex);
285
286 int clk_set_parent(struct clk *clk, struct clk *parent)
287 {
288         unsigned long flags;
289         int ret = -EINVAL;
290
291         if (!parent || !clk)
292                 return ret;
293         if (clk->parent == parent)
294                 return 0;
295
296         spin_lock_irqsave(&clock_lock, flags);
297         if (clk->usecount == 0) {
298                 if (clk->ops->set_parent)
299                         ret = clk->ops->set_parent(clk, parent);
300                 else
301                         ret = clk_reparent(clk, parent);
302
303                 if (ret == 0) {
304                         pr_debug("clock: set parent of %s to %s (new rate %ld)\n",
305                                  clk->name, clk->parent->name, clk->rate);
306                         if (clk->ops->recalc)
307                                 clk->rate = clk->ops->recalc(clk);
308                         propagate_rate(clk);
309                 }
310         } else
311                 ret = -EBUSY;
312         spin_unlock_irqrestore(&clock_lock, flags);
313
314         return ret;
315 }
316 EXPORT_SYMBOL_GPL(clk_set_parent);
317
318 struct clk *clk_get_parent(struct clk *clk)
319 {
320         return clk->parent;
321 }
322 EXPORT_SYMBOL_GPL(clk_get_parent);
323
324 long clk_round_rate(struct clk *clk, unsigned long rate)
325 {
326         if (likely(clk->ops && clk->ops->round_rate)) {
327                 unsigned long flags, rounded;
328
329                 spin_lock_irqsave(&clock_lock, flags);
330                 rounded = clk->ops->round_rate(clk, rate);
331                 spin_unlock_irqrestore(&clock_lock, flags);
332
333                 return rounded;
334         }
335
336         return clk_get_rate(clk);
337 }
338 EXPORT_SYMBOL_GPL(clk_round_rate);
339
340 /*
341  * Find the correct struct clk for the device and connection ID.
342  * We do slightly fuzzy matching here:
343  *  An entry with a NULL ID is assumed to be a wildcard.
344  *  If an entry has a device ID, it must match
345  *  If an entry has a connection ID, it must match
346  * Then we take the most specific entry - with the following
347  * order of precidence: dev+con > dev only > con only.
348  */
349 static struct clk *clk_find(const char *dev_id, const char *con_id)
350 {
351         struct clk_lookup *p;
352         struct clk *clk = NULL;
353         int match, best = 0;
354
355         list_for_each_entry(p, &clock_list, node) {
356                 match = 0;
357                 if (p->dev_id) {
358                         if (!dev_id || strcmp(p->dev_id, dev_id))
359                                 continue;
360                         match += 2;
361                 }
362                 if (p->con_id) {
363                         if (!con_id || strcmp(p->con_id, con_id))
364                                 continue;
365                         match += 1;
366                 }
367                 if (match == 0)
368                         continue;
369
370                 if (match > best) {
371                         clk = p->clk;
372                         best = match;
373                 }
374         }
375         return clk;
376 }
377
378 struct clk *clk_get_sys(const char *dev_id, const char *con_id)
379 {
380         struct clk *clk;
381
382         mutex_lock(&clock_list_sem);
383         clk = clk_find(dev_id, con_id);
384         mutex_unlock(&clock_list_sem);
385
386         return clk ? clk : ERR_PTR(-ENOENT);
387 }
388 EXPORT_SYMBOL_GPL(clk_get_sys);
389
390 /*
391  * Returns a clock. Note that we first try to use device id on the bus
392  * and clock name. If this fails, we try to use clock name only.
393  */
394 struct clk *clk_get(struct device *dev, const char *id)
395 {
396         const char *dev_id = dev ? dev_name(dev) : NULL;
397         struct clk *p, *clk = ERR_PTR(-ENOENT);
398         int idno;
399
400         clk = clk_get_sys(dev_id, id);
401         if (clk && !IS_ERR(clk))
402                 return clk;
403
404         if (dev == NULL || dev->bus != &platform_bus_type)
405                 idno = -1;
406         else
407                 idno = to_platform_device(dev)->id;
408
409         mutex_lock(&clock_list_sem);
410         list_for_each_entry(p, &clock_list, node) {
411                 if (p->id == idno &&
412                     strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
413                         clk = p;
414                         goto found;
415                 }
416         }
417
418         list_for_each_entry(p, &clock_list, node) {
419                 if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
420                         clk = p;
421                         break;
422                 }
423         }
424
425 found:
426         mutex_unlock(&clock_list_sem);
427
428         return clk;
429 }
430 EXPORT_SYMBOL_GPL(clk_get);
431
432 void clk_put(struct clk *clk)
433 {
434         if (clk && !IS_ERR(clk))
435                 module_put(clk->owner);
436 }
437 EXPORT_SYMBOL_GPL(clk_put);
438
439 int __init __weak arch_clk_init(void)
440 {
441         return 0;
442 }
443
444 static int show_clocks(char *buf, char **start, off_t off,
445                        int len, int *eof, void *data)
446 {
447         struct clk *clk;
448         char *p = buf;
449
450         list_for_each_entry_reverse(clk, &clock_list, node) {
451                 unsigned long rate = clk_get_rate(clk);
452
453                 p += sprintf(p, "%-12s\t: %ld.%02ldMHz\t%s\n", clk->name,
454                              rate / 1000000, (rate % 1000000) / 10000,
455                               (clk->usecount > 0) ?  "enabled" : "disabled");
456         }
457
458         return p - buf;
459 }
460
461 #ifdef CONFIG_PM
462 static int clks_sysdev_suspend(struct sys_device *dev, pm_message_t state)
463 {
464         static pm_message_t prev_state;
465         struct clk *clkp;
466
467         switch (state.event) {
468         case PM_EVENT_ON:
469                 /* Resumeing from hibernation */
470                 if (prev_state.event != PM_EVENT_FREEZE)
471                         break;
472
473                 list_for_each_entry(clkp, &clock_list, node) {
474                         if (likely(clkp->ops)) {
475                                 unsigned long rate = clkp->rate;
476
477                                 if (likely(clkp->ops->set_parent))
478                                         clkp->ops->set_parent(clkp,
479                                                 clkp->parent);
480                                 if (likely(clkp->ops->set_rate))
481                                         clkp->ops->set_rate(clkp,
482                                                 rate, NO_CHANGE);
483                                 else if (likely(clkp->ops->recalc))
484                                         clkp->rate = clkp->ops->recalc(clkp);
485                         }
486                 }
487                 break;
488         case PM_EVENT_FREEZE:
489                 break;
490         case PM_EVENT_SUSPEND:
491                 break;
492         }
493
494         prev_state = state;
495         return 0;
496 }
497
498 static int clks_sysdev_resume(struct sys_device *dev)
499 {
500         return clks_sysdev_suspend(dev, PMSG_ON);
501 }
502
503 static struct sysdev_class clks_sysdev_class = {
504         .name = "clks",
505 };
506
507 static struct sysdev_driver clks_sysdev_driver = {
508         .suspend = clks_sysdev_suspend,
509         .resume = clks_sysdev_resume,
510 };
511
512 static struct sys_device clks_sysdev_dev = {
513         .cls = &clks_sysdev_class,
514 };
515
516 static int __init clk_sysdev_init(void)
517 {
518         sysdev_class_register(&clks_sysdev_class);
519         sysdev_driver_register(&clks_sysdev_class, &clks_sysdev_driver);
520         sysdev_register(&clks_sysdev_dev);
521
522         return 0;
523 }
524 subsys_initcall(clk_sysdev_init);
525 #endif
526
527 int __init clk_init(void)
528 {
529         int i, ret = 0;
530
531         BUG_ON(!master_clk.rate);
532
533         for (i = 0; i < ARRAY_SIZE(onchip_clocks); i++) {
534                 struct clk *clk = onchip_clocks[i];
535
536                 arch_init_clk_ops(&clk->ops, i);
537                 ret |= clk_register(clk);
538         }
539
540         ret |= arch_clk_init();
541
542         /* Kick the child clocks.. */
543         recalculate_root_clocks();
544
545         /* Enable the necessary init clocks */
546         clk_enable_init_clocks();
547
548         return ret;
549 }
550
551 static int __init clk_proc_init(void)
552 {
553         struct proc_dir_entry *p;
554         p = create_proc_read_entry("clocks", S_IRUSR, NULL,
555                                    show_clocks, NULL);
556         if (unlikely(!p))
557                 return -EINVAL;
558
559         return 0;
560 }
561 subsys_initcall(clk_proc_init);