blob: ad2ac94fede842ded7f3912ecfa1c7014dce7206 [file] [log] [blame]
Mike Turquetteb24764902012-03-15 23:11:19 -07001/*
2 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
3 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Standard functionality for the common clock API. See Documentation/clk.txt
10 */
11
12#include <linux/clk-private.h>
13#include <linux/module.h>
14#include <linux/mutex.h>
15#include <linux/spinlock.h>
16#include <linux/err.h>
17#include <linux/list.h>
18#include <linux/slab.h>
Grant Likely766e6a42012-04-09 14:50:06 -050019#include <linux/of.h>
Stephen Boyd46c87732012-09-24 13:38:04 -070020#include <linux/device.h>
Mike Turquetteb24764902012-03-15 23:11:19 -070021
22static DEFINE_SPINLOCK(enable_lock);
23static DEFINE_MUTEX(prepare_lock);
24
25static HLIST_HEAD(clk_root_list);
26static HLIST_HEAD(clk_orphan_list);
27static LIST_HEAD(clk_notifier_list);
28
29/*** debugfs support ***/
30
31#ifdef CONFIG_COMMON_CLK_DEBUG
32#include <linux/debugfs.h>
33
34static struct dentry *rootdir;
35static struct dentry *orphandir;
36static int inited = 0;
37
Prashant Gaikwad1af599d2012-12-26 19:16:22 +053038static void clk_summary_show_one(struct seq_file *s, struct clk *c, int level)
39{
40 if (!c)
41 return;
42
43 seq_printf(s, "%*s%-*s %-11d %-12d %-10lu",
44 level * 3 + 1, "",
45 30 - level * 3, c->name,
46 c->enable_count, c->prepare_count, c->rate);
47 seq_printf(s, "\n");
48}
49
50static void clk_summary_show_subtree(struct seq_file *s, struct clk *c,
51 int level)
52{
53 struct clk *child;
54 struct hlist_node *tmp;
55
56 if (!c)
57 return;
58
59 clk_summary_show_one(s, c, level);
60
61 hlist_for_each_entry(child, tmp, &c->children, child_node)
62 clk_summary_show_subtree(s, child, level + 1);
63}
64
65static int clk_summary_show(struct seq_file *s, void *data)
66{
67 struct clk *c;
68 struct hlist_node *tmp;
69
70 seq_printf(s, " clock enable_cnt prepare_cnt rate\n");
71 seq_printf(s, "---------------------------------------------------------------------\n");
72
73 mutex_lock(&prepare_lock);
74
75 hlist_for_each_entry(c, tmp, &clk_root_list, child_node)
76 clk_summary_show_subtree(s, c, 0);
77
78 hlist_for_each_entry(c, tmp, &clk_orphan_list, child_node)
79 clk_summary_show_subtree(s, c, 0);
80
81 mutex_unlock(&prepare_lock);
82
83 return 0;
84}
85
86
87static int clk_summary_open(struct inode *inode, struct file *file)
88{
89 return single_open(file, clk_summary_show, inode->i_private);
90}
91
92static const struct file_operations clk_summary_fops = {
93 .open = clk_summary_open,
94 .read = seq_read,
95 .llseek = seq_lseek,
96 .release = single_release,
97};
98
Prashant Gaikwadbddca892012-12-26 19:16:23 +053099static void clk_dump_one(struct seq_file *s, struct clk *c, int level)
100{
101 if (!c)
102 return;
103
104 seq_printf(s, "\"%s\": { ", c->name);
105 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
106 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
107 seq_printf(s, "\"rate\": %lu", c->rate);
108}
109
110static void clk_dump_subtree(struct seq_file *s, struct clk *c, int level)
111{
112 struct clk *child;
113 struct hlist_node *tmp;
114
115 if (!c)
116 return;
117
118 clk_dump_one(s, c, level);
119
120 hlist_for_each_entry(child, tmp, &c->children, child_node) {
121 seq_printf(s, ",");
122 clk_dump_subtree(s, child, level + 1);
123 }
124
125 seq_printf(s, "}");
126}
127
128static int clk_dump(struct seq_file *s, void *data)
129{
130 struct clk *c;
131 struct hlist_node *tmp;
132 bool first_node = true;
133
134 seq_printf(s, "{");
135
136 mutex_lock(&prepare_lock);
137
138 hlist_for_each_entry(c, tmp, &clk_root_list, child_node) {
139 if (!first_node)
140 seq_printf(s, ",");
141 first_node = false;
142 clk_dump_subtree(s, c, 0);
143 }
144
145 hlist_for_each_entry(c, tmp, &clk_orphan_list, child_node) {
146 seq_printf(s, ",");
147 clk_dump_subtree(s, c, 0);
148 }
149
150 mutex_unlock(&prepare_lock);
151
152 seq_printf(s, "}");
153 return 0;
154}
155
156
157static int clk_dump_open(struct inode *inode, struct file *file)
158{
159 return single_open(file, clk_dump, inode->i_private);
160}
161
162static const struct file_operations clk_dump_fops = {
163 .open = clk_dump_open,
164 .read = seq_read,
165 .llseek = seq_lseek,
166 .release = single_release,
167};
168
Mike Turquetteb24764902012-03-15 23:11:19 -0700169/* caller must hold prepare_lock */
170static int clk_debug_create_one(struct clk *clk, struct dentry *pdentry)
171{
172 struct dentry *d;
173 int ret = -ENOMEM;
174
175 if (!clk || !pdentry) {
176 ret = -EINVAL;
177 goto out;
178 }
179
180 d = debugfs_create_dir(clk->name, pdentry);
181 if (!d)
182 goto out;
183
184 clk->dentry = d;
185
186 d = debugfs_create_u32("clk_rate", S_IRUGO, clk->dentry,
187 (u32 *)&clk->rate);
188 if (!d)
189 goto err_out;
190
191 d = debugfs_create_x32("clk_flags", S_IRUGO, clk->dentry,
192 (u32 *)&clk->flags);
193 if (!d)
194 goto err_out;
195
196 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, clk->dentry,
197 (u32 *)&clk->prepare_count);
198 if (!d)
199 goto err_out;
200
201 d = debugfs_create_u32("clk_enable_count", S_IRUGO, clk->dentry,
202 (u32 *)&clk->enable_count);
203 if (!d)
204 goto err_out;
205
206 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, clk->dentry,
207 (u32 *)&clk->notifier_count);
208 if (!d)
209 goto err_out;
210
211 ret = 0;
212 goto out;
213
214err_out:
215 debugfs_remove(clk->dentry);
216out:
217 return ret;
218}
219
220/* caller must hold prepare_lock */
221static int clk_debug_create_subtree(struct clk *clk, struct dentry *pdentry)
222{
223 struct clk *child;
224 struct hlist_node *tmp;
225 int ret = -EINVAL;;
226
227 if (!clk || !pdentry)
228 goto out;
229
230 ret = clk_debug_create_one(clk, pdentry);
231
232 if (ret)
233 goto out;
234
235 hlist_for_each_entry(child, tmp, &clk->children, child_node)
236 clk_debug_create_subtree(child, clk->dentry);
237
238 ret = 0;
239out:
240 return ret;
241}
242
243/**
244 * clk_debug_register - add a clk node to the debugfs clk tree
245 * @clk: the clk being added to the debugfs clk tree
246 *
247 * Dynamically adds a clk to the debugfs clk tree if debugfs has been
248 * initialized. Otherwise it bails out early since the debugfs clk tree
249 * will be created lazily by clk_debug_init as part of a late_initcall.
250 *
251 * Caller must hold prepare_lock. Only clk_init calls this function (so
252 * far) so this is taken care.
253 */
254static int clk_debug_register(struct clk *clk)
255{
256 struct clk *parent;
257 struct dentry *pdentry;
258 int ret = 0;
259
260 if (!inited)
261 goto out;
262
263 parent = clk->parent;
264
265 /*
266 * Check to see if a clk is a root clk. Also check that it is
267 * safe to add this clk to debugfs
268 */
269 if (!parent)
270 if (clk->flags & CLK_IS_ROOT)
271 pdentry = rootdir;
272 else
273 pdentry = orphandir;
274 else
275 if (parent->dentry)
276 pdentry = parent->dentry;
277 else
278 goto out;
279
280 ret = clk_debug_create_subtree(clk, pdentry);
281
282out:
283 return ret;
284}
285
286/**
287 * clk_debug_init - lazily create the debugfs clk tree visualization
288 *
289 * clks are often initialized very early during boot before memory can
290 * be dynamically allocated and well before debugfs is setup.
291 * clk_debug_init walks the clk tree hierarchy while holding
292 * prepare_lock and creates the topology as part of a late_initcall,
293 * thus insuring that clks initialized very early will still be
294 * represented in the debugfs clk tree. This function should only be
295 * called once at boot-time, and all other clks added dynamically will
296 * be done so with clk_debug_register.
297 */
298static int __init clk_debug_init(void)
299{
300 struct clk *clk;
301 struct hlist_node *tmp;
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530302 struct dentry *d;
Mike Turquetteb24764902012-03-15 23:11:19 -0700303
304 rootdir = debugfs_create_dir("clk", NULL);
305
306 if (!rootdir)
307 return -ENOMEM;
308
Prashant Gaikwad1af599d2012-12-26 19:16:22 +0530309 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, NULL,
310 &clk_summary_fops);
311 if (!d)
312 return -ENOMEM;
313
Prashant Gaikwadbddca892012-12-26 19:16:23 +0530314 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, NULL,
315 &clk_dump_fops);
316 if (!d)
317 return -ENOMEM;
318
Mike Turquetteb24764902012-03-15 23:11:19 -0700319 orphandir = debugfs_create_dir("orphans", rootdir);
320
321 if (!orphandir)
322 return -ENOMEM;
323
324 mutex_lock(&prepare_lock);
325
326 hlist_for_each_entry(clk, tmp, &clk_root_list, child_node)
327 clk_debug_create_subtree(clk, rootdir);
328
329 hlist_for_each_entry(clk, tmp, &clk_orphan_list, child_node)
330 clk_debug_create_subtree(clk, orphandir);
331
332 inited = 1;
333
334 mutex_unlock(&prepare_lock);
335
336 return 0;
337}
338late_initcall(clk_debug_init);
339#else
340static inline int clk_debug_register(struct clk *clk) { return 0; }
Mike Turquette70d347e2012-03-26 11:53:47 -0700341#endif
Mike Turquetteb24764902012-03-15 23:11:19 -0700342
Mike Turquetteb24764902012-03-15 23:11:19 -0700343/* caller must hold prepare_lock */
344static void clk_disable_unused_subtree(struct clk *clk)
345{
346 struct clk *child;
347 struct hlist_node *tmp;
348 unsigned long flags;
349
350 if (!clk)
351 goto out;
352
353 hlist_for_each_entry(child, tmp, &clk->children, child_node)
354 clk_disable_unused_subtree(child);
355
356 spin_lock_irqsave(&enable_lock, flags);
357
358 if (clk->enable_count)
359 goto unlock_out;
360
361 if (clk->flags & CLK_IGNORE_UNUSED)
362 goto unlock_out;
363
Mike Turquette7c045a52012-12-04 11:00:35 -0800364 /*
365 * some gate clocks have special needs during the disable-unused
366 * sequence. call .disable_unused if available, otherwise fall
367 * back to .disable
368 */
369 if (__clk_is_enabled(clk)) {
370 if (clk->ops->disable_unused)
371 clk->ops->disable_unused(clk->hw);
372 else if (clk->ops->disable)
373 clk->ops->disable(clk->hw);
374 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700375
376unlock_out:
377 spin_unlock_irqrestore(&enable_lock, flags);
378
379out:
380 return;
381}
382
383static int clk_disable_unused(void)
384{
385 struct clk *clk;
386 struct hlist_node *tmp;
387
388 mutex_lock(&prepare_lock);
389
390 hlist_for_each_entry(clk, tmp, &clk_root_list, child_node)
391 clk_disable_unused_subtree(clk);
392
393 hlist_for_each_entry(clk, tmp, &clk_orphan_list, child_node)
394 clk_disable_unused_subtree(clk);
395
396 mutex_unlock(&prepare_lock);
397
398 return 0;
399}
400late_initcall(clk_disable_unused);
Mike Turquetteb24764902012-03-15 23:11:19 -0700401
402/*** helper functions ***/
403
Russ Dill65800b22012-11-26 11:20:09 -0800404const char *__clk_get_name(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700405{
406 return !clk ? NULL : clk->name;
407}
Niels de Vos48950842012-12-13 13:12:25 +0100408EXPORT_SYMBOL_GPL(__clk_get_name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700409
Russ Dill65800b22012-11-26 11:20:09 -0800410struct clk_hw *__clk_get_hw(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700411{
412 return !clk ? NULL : clk->hw;
413}
414
Russ Dill65800b22012-11-26 11:20:09 -0800415u8 __clk_get_num_parents(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700416{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700417 return !clk ? 0 : clk->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -0700418}
419
Russ Dill65800b22012-11-26 11:20:09 -0800420struct clk *__clk_get_parent(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700421{
422 return !clk ? NULL : clk->parent;
423}
424
Russ Dill65800b22012-11-26 11:20:09 -0800425unsigned int __clk_get_enable_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700426{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700427 return !clk ? 0 : clk->enable_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700428}
429
Russ Dill65800b22012-11-26 11:20:09 -0800430unsigned int __clk_get_prepare_count(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700431{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700432 return !clk ? 0 : clk->prepare_count;
Mike Turquetteb24764902012-03-15 23:11:19 -0700433}
434
435unsigned long __clk_get_rate(struct clk *clk)
436{
437 unsigned long ret;
438
439 if (!clk) {
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530440 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700441 goto out;
442 }
443
444 ret = clk->rate;
445
446 if (clk->flags & CLK_IS_ROOT)
447 goto out;
448
449 if (!clk->parent)
Rajendra Nayak34e44fe2012-03-26 19:01:48 +0530450 ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700451
452out:
453 return ret;
454}
455
Russ Dill65800b22012-11-26 11:20:09 -0800456unsigned long __clk_get_flags(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700457{
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700458 return !clk ? 0 : clk->flags;
Mike Turquetteb24764902012-03-15 23:11:19 -0700459}
460
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700461bool __clk_is_enabled(struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -0700462{
463 int ret;
464
465 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700466 return false;
Mike Turquetteb24764902012-03-15 23:11:19 -0700467
468 /*
469 * .is_enabled is only mandatory for clocks that gate
470 * fall back to software usage counter if .is_enabled is missing
471 */
472 if (!clk->ops->is_enabled) {
473 ret = clk->enable_count ? 1 : 0;
474 goto out;
475 }
476
477 ret = clk->ops->is_enabled(clk->hw);
478out:
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700479 return !!ret;
Mike Turquetteb24764902012-03-15 23:11:19 -0700480}
481
482static struct clk *__clk_lookup_subtree(const char *name, struct clk *clk)
483{
484 struct clk *child;
485 struct clk *ret;
486 struct hlist_node *tmp;
487
488 if (!strcmp(clk->name, name))
489 return clk;
490
491 hlist_for_each_entry(child, tmp, &clk->children, child_node) {
492 ret = __clk_lookup_subtree(name, child);
493 if (ret)
494 return ret;
495 }
496
497 return NULL;
498}
499
500struct clk *__clk_lookup(const char *name)
501{
502 struct clk *root_clk;
503 struct clk *ret;
504 struct hlist_node *tmp;
505
506 if (!name)
507 return NULL;
508
509 /* search the 'proper' clk tree first */
510 hlist_for_each_entry(root_clk, tmp, &clk_root_list, child_node) {
511 ret = __clk_lookup_subtree(name, root_clk);
512 if (ret)
513 return ret;
514 }
515
516 /* if not found, then search the orphan tree */
517 hlist_for_each_entry(root_clk, tmp, &clk_orphan_list, child_node) {
518 ret = __clk_lookup_subtree(name, root_clk);
519 if (ret)
520 return ret;
521 }
522
523 return NULL;
524}
525
526/*** clk api ***/
527
528void __clk_unprepare(struct clk *clk)
529{
530 if (!clk)
531 return;
532
533 if (WARN_ON(clk->prepare_count == 0))
534 return;
535
536 if (--clk->prepare_count > 0)
537 return;
538
539 WARN_ON(clk->enable_count > 0);
540
541 if (clk->ops->unprepare)
542 clk->ops->unprepare(clk->hw);
543
544 __clk_unprepare(clk->parent);
545}
546
547/**
548 * clk_unprepare - undo preparation of a clock source
549 * @clk: the clk being unprepare
550 *
551 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
552 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
553 * if the operation may sleep. One example is a clk which is accessed over
554 * I2c. In the complex case a clk gate operation may require a fast and a slow
555 * part. It is this reason that clk_unprepare and clk_disable are not mutually
556 * exclusive. In fact clk_disable must be called before clk_unprepare.
557 */
558void clk_unprepare(struct clk *clk)
559{
560 mutex_lock(&prepare_lock);
561 __clk_unprepare(clk);
562 mutex_unlock(&prepare_lock);
563}
564EXPORT_SYMBOL_GPL(clk_unprepare);
565
566int __clk_prepare(struct clk *clk)
567{
568 int ret = 0;
569
570 if (!clk)
571 return 0;
572
573 if (clk->prepare_count == 0) {
574 ret = __clk_prepare(clk->parent);
575 if (ret)
576 return ret;
577
578 if (clk->ops->prepare) {
579 ret = clk->ops->prepare(clk->hw);
580 if (ret) {
581 __clk_unprepare(clk->parent);
582 return ret;
583 }
584 }
585 }
586
587 clk->prepare_count++;
588
589 return 0;
590}
591
592/**
593 * clk_prepare - prepare a clock source
594 * @clk: the clk being prepared
595 *
596 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
597 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
598 * operation may sleep. One example is a clk which is accessed over I2c. In
599 * the complex case a clk ungate operation may require a fast and a slow part.
600 * It is this reason that clk_prepare and clk_enable are not mutually
601 * exclusive. In fact clk_prepare must be called before clk_enable.
602 * Returns 0 on success, -EERROR otherwise.
603 */
604int clk_prepare(struct clk *clk)
605{
606 int ret;
607
608 mutex_lock(&prepare_lock);
609 ret = __clk_prepare(clk);
610 mutex_unlock(&prepare_lock);
611
612 return ret;
613}
614EXPORT_SYMBOL_GPL(clk_prepare);
615
616static void __clk_disable(struct clk *clk)
617{
618 if (!clk)
619 return;
620
Fengguang Wue47c6a32012-07-30 14:39:54 -0700621 if (WARN_ON(IS_ERR(clk)))
622 return;
623
Mike Turquetteb24764902012-03-15 23:11:19 -0700624 if (WARN_ON(clk->enable_count == 0))
625 return;
626
627 if (--clk->enable_count > 0)
628 return;
629
630 if (clk->ops->disable)
631 clk->ops->disable(clk->hw);
632
633 __clk_disable(clk->parent);
634}
635
636/**
637 * clk_disable - gate a clock
638 * @clk: the clk being gated
639 *
640 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
641 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
642 * clk if the operation is fast and will never sleep. One example is a
643 * SoC-internal clk which is controlled via simple register writes. In the
644 * complex case a clk gate operation may require a fast and a slow part. It is
645 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
646 * In fact clk_disable must be called before clk_unprepare.
647 */
648void clk_disable(struct clk *clk)
649{
650 unsigned long flags;
651
652 spin_lock_irqsave(&enable_lock, flags);
653 __clk_disable(clk);
654 spin_unlock_irqrestore(&enable_lock, flags);
655}
656EXPORT_SYMBOL_GPL(clk_disable);
657
658static int __clk_enable(struct clk *clk)
659{
660 int ret = 0;
661
662 if (!clk)
663 return 0;
664
665 if (WARN_ON(clk->prepare_count == 0))
666 return -ESHUTDOWN;
667
668 if (clk->enable_count == 0) {
669 ret = __clk_enable(clk->parent);
670
671 if (ret)
672 return ret;
673
674 if (clk->ops->enable) {
675 ret = clk->ops->enable(clk->hw);
676 if (ret) {
677 __clk_disable(clk->parent);
678 return ret;
679 }
680 }
681 }
682
683 clk->enable_count++;
684 return 0;
685}
686
687/**
688 * clk_enable - ungate a clock
689 * @clk: the clk being ungated
690 *
691 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
692 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
693 * if the operation will never sleep. One example is a SoC-internal clk which
694 * is controlled via simple register writes. In the complex case a clk ungate
695 * operation may require a fast and a slow part. It is this reason that
696 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
697 * must be called before clk_enable. Returns 0 on success, -EERROR
698 * otherwise.
699 */
700int clk_enable(struct clk *clk)
701{
702 unsigned long flags;
703 int ret;
704
705 spin_lock_irqsave(&enable_lock, flags);
706 ret = __clk_enable(clk);
707 spin_unlock_irqrestore(&enable_lock, flags);
708
709 return ret;
710}
711EXPORT_SYMBOL_GPL(clk_enable);
712
713/**
Mike Turquetteb24764902012-03-15 23:11:19 -0700714 * __clk_round_rate - round the given rate for a clk
715 * @clk: round the rate of this clock
716 *
717 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
718 */
719unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
720{
Shawn Guo81536e02012-04-12 20:50:17 +0800721 unsigned long parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700722
723 if (!clk)
Stephen Boyd2ac6b1f2012-10-03 23:38:55 -0700724 return 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700725
Shawn Guof4d8af22012-04-12 20:50:19 +0800726 if (!clk->ops->round_rate) {
727 if (clk->flags & CLK_SET_RATE_PARENT)
728 return __clk_round_rate(clk->parent, rate);
729 else
730 return clk->rate;
731 }
Mike Turquetteb24764902012-03-15 23:11:19 -0700732
Shawn Guo81536e02012-04-12 20:50:17 +0800733 if (clk->parent)
734 parent_rate = clk->parent->rate;
735
736 return clk->ops->round_rate(clk->hw, rate, &parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700737}
738
739/**
740 * clk_round_rate - round the given rate for a clk
741 * @clk: the clk for which we are rounding a rate
742 * @rate: the rate which is to be rounded
743 *
744 * Takes in a rate as input and rounds it to a rate that the clk can actually
745 * use which is then returned. If clk doesn't support round_rate operation
746 * then the parent rate is returned.
747 */
748long clk_round_rate(struct clk *clk, unsigned long rate)
749{
750 unsigned long ret;
751
752 mutex_lock(&prepare_lock);
753 ret = __clk_round_rate(clk, rate);
754 mutex_unlock(&prepare_lock);
755
756 return ret;
757}
758EXPORT_SYMBOL_GPL(clk_round_rate);
759
760/**
761 * __clk_notify - call clk notifier chain
762 * @clk: struct clk * that is changing rate
763 * @msg: clk notifier type (see include/linux/clk.h)
764 * @old_rate: old clk rate
765 * @new_rate: new clk rate
766 *
767 * Triggers a notifier call chain on the clk rate-change notification
768 * for 'clk'. Passes a pointer to the struct clk and the previous
769 * and current rates to the notifier callback. Intended to be called by
770 * internal clock code only. Returns NOTIFY_DONE from the last driver
771 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
772 * a driver returns that.
773 */
774static int __clk_notify(struct clk *clk, unsigned long msg,
775 unsigned long old_rate, unsigned long new_rate)
776{
777 struct clk_notifier *cn;
778 struct clk_notifier_data cnd;
779 int ret = NOTIFY_DONE;
780
781 cnd.clk = clk;
782 cnd.old_rate = old_rate;
783 cnd.new_rate = new_rate;
784
785 list_for_each_entry(cn, &clk_notifier_list, node) {
786 if (cn->clk == clk) {
787 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
788 &cnd);
789 break;
790 }
791 }
792
793 return ret;
794}
795
796/**
797 * __clk_recalc_rates
798 * @clk: first clk in the subtree
799 * @msg: notification type (see include/linux/clk.h)
800 *
801 * Walks the subtree of clks starting with clk and recalculates rates as it
802 * goes. Note that if a clk does not implement the .recalc_rate callback then
803 * it is assumed that the clock will take on the rate of it's parent.
804 *
805 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
806 * if necessary.
807 *
808 * Caller must hold prepare_lock.
809 */
810static void __clk_recalc_rates(struct clk *clk, unsigned long msg)
811{
812 unsigned long old_rate;
813 unsigned long parent_rate = 0;
814 struct hlist_node *tmp;
815 struct clk *child;
816
817 old_rate = clk->rate;
818
819 if (clk->parent)
820 parent_rate = clk->parent->rate;
821
822 if (clk->ops->recalc_rate)
823 clk->rate = clk->ops->recalc_rate(clk->hw, parent_rate);
824 else
825 clk->rate = parent_rate;
826
827 /*
828 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
829 * & ABORT_RATE_CHANGE notifiers
830 */
831 if (clk->notifier_count && msg)
832 __clk_notify(clk, msg, old_rate, clk->rate);
833
834 hlist_for_each_entry(child, tmp, &clk->children, child_node)
835 __clk_recalc_rates(child, msg);
836}
837
838/**
Ulf Hanssona093bde2012-08-31 14:21:28 +0200839 * clk_get_rate - return the rate of clk
840 * @clk: the clk whose rate is being returned
841 *
842 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
843 * is set, which means a recalc_rate will be issued.
844 * If clk is NULL then returns 0.
845 */
846unsigned long clk_get_rate(struct clk *clk)
847{
848 unsigned long rate;
849
850 mutex_lock(&prepare_lock);
851
852 if (clk && (clk->flags & CLK_GET_RATE_NOCACHE))
853 __clk_recalc_rates(clk, 0);
854
855 rate = __clk_get_rate(clk);
856 mutex_unlock(&prepare_lock);
857
858 return rate;
859}
860EXPORT_SYMBOL_GPL(clk_get_rate);
861
862/**
Mike Turquetteb24764902012-03-15 23:11:19 -0700863 * __clk_speculate_rates
864 * @clk: first clk in the subtree
865 * @parent_rate: the "future" rate of clk's parent
866 *
867 * Walks the subtree of clks starting with clk, speculating rates as it
868 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
869 *
870 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
871 * pre-rate change notifications and returns early if no clks in the
872 * subtree have subscribed to the notifications. Note that if a clk does not
873 * implement the .recalc_rate callback then it is assumed that the clock will
874 * take on the rate of it's parent.
875 *
876 * Caller must hold prepare_lock.
877 */
878static int __clk_speculate_rates(struct clk *clk, unsigned long parent_rate)
879{
880 struct hlist_node *tmp;
881 struct clk *child;
882 unsigned long new_rate;
883 int ret = NOTIFY_DONE;
884
885 if (clk->ops->recalc_rate)
886 new_rate = clk->ops->recalc_rate(clk->hw, parent_rate);
887 else
888 new_rate = parent_rate;
889
890 /* abort the rate change if a driver returns NOTIFY_BAD */
891 if (clk->notifier_count)
892 ret = __clk_notify(clk, PRE_RATE_CHANGE, clk->rate, new_rate);
893
894 if (ret == NOTIFY_BAD)
895 goto out;
896
897 hlist_for_each_entry(child, tmp, &clk->children, child_node) {
898 ret = __clk_speculate_rates(child, new_rate);
899 if (ret == NOTIFY_BAD)
900 break;
901 }
902
903out:
904 return ret;
905}
906
907static void clk_calc_subtree(struct clk *clk, unsigned long new_rate)
908{
909 struct clk *child;
910 struct hlist_node *tmp;
911
912 clk->new_rate = new_rate;
913
914 hlist_for_each_entry(child, tmp, &clk->children, child_node) {
915 if (child->ops->recalc_rate)
916 child->new_rate = child->ops->recalc_rate(child->hw, new_rate);
917 else
918 child->new_rate = new_rate;
919 clk_calc_subtree(child, child->new_rate);
920 }
921}
922
923/*
924 * calculate the new rates returning the topmost clock that has to be
925 * changed.
926 */
927static struct clk *clk_calc_new_rates(struct clk *clk, unsigned long rate)
928{
929 struct clk *top = clk;
Shawn Guo81536e02012-04-12 20:50:17 +0800930 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -0700931 unsigned long new_rate;
932
Mike Turquette7452b212012-03-26 14:45:36 -0700933 /* sanity */
934 if (IS_ERR_OR_NULL(clk))
935 return NULL;
936
Mike Turquette63f5c3b2012-05-02 16:23:43 -0700937 /* save parent rate, if it exists */
938 if (clk->parent)
939 best_parent_rate = clk->parent->rate;
940
Mike Turquette7452b212012-03-26 14:45:36 -0700941 /* never propagate up to the parent */
942 if (!(clk->flags & CLK_SET_RATE_PARENT)) {
943 if (!clk->ops->round_rate) {
944 clk->new_rate = clk->rate;
945 return NULL;
Mike Turquette7452b212012-03-26 14:45:36 -0700946 }
Mike Turquette63f5c3b2012-05-02 16:23:43 -0700947 new_rate = clk->ops->round_rate(clk->hw, rate, &best_parent_rate);
948 goto out;
Mike Turquette7452b212012-03-26 14:45:36 -0700949 }
950
951 /* need clk->parent from here on out */
952 if (!clk->parent) {
953 pr_debug("%s: %s has NULL parent\n", __func__, clk->name);
Mike Turquetteb24764902012-03-15 23:11:19 -0700954 return NULL;
955 }
956
Mike Turquette7452b212012-03-26 14:45:36 -0700957 if (!clk->ops->round_rate) {
Mike Turquetteb24764902012-03-15 23:11:19 -0700958 top = clk_calc_new_rates(clk->parent, rate);
Viresh Kumar1b2f9902012-04-17 16:45:38 +0530959 new_rate = clk->parent->new_rate;
Mike Turquetteb24764902012-03-15 23:11:19 -0700960
961 goto out;
962 }
963
Mike Turquette7452b212012-03-26 14:45:36 -0700964 new_rate = clk->ops->round_rate(clk->hw, rate, &best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -0700965
966 if (best_parent_rate != clk->parent->rate) {
967 top = clk_calc_new_rates(clk->parent, best_parent_rate);
968
969 goto out;
970 }
971
972out:
973 clk_calc_subtree(clk, new_rate);
974
975 return top;
976}
977
978/*
979 * Notify about rate changes in a subtree. Always walk down the whole tree
980 * so that in case of an error we can walk down the whole tree again and
981 * abort the change.
982 */
983static struct clk *clk_propagate_rate_change(struct clk *clk, unsigned long event)
984{
985 struct hlist_node *tmp;
986 struct clk *child, *fail_clk = NULL;
987 int ret = NOTIFY_DONE;
988
989 if (clk->rate == clk->new_rate)
990 return 0;
991
992 if (clk->notifier_count) {
993 ret = __clk_notify(clk, event, clk->rate, clk->new_rate);
994 if (ret == NOTIFY_BAD)
995 fail_clk = clk;
996 }
997
998 hlist_for_each_entry(child, tmp, &clk->children, child_node) {
999 clk = clk_propagate_rate_change(child, event);
1000 if (clk)
1001 fail_clk = clk;
1002 }
1003
1004 return fail_clk;
1005}
1006
1007/*
1008 * walk down a subtree and set the new rates notifying the rate
1009 * change on the way
1010 */
1011static void clk_change_rate(struct clk *clk)
1012{
1013 struct clk *child;
1014 unsigned long old_rate;
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001015 unsigned long best_parent_rate = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001016 struct hlist_node *tmp;
1017
1018 old_rate = clk->rate;
1019
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001020 if (clk->parent)
1021 best_parent_rate = clk->parent->rate;
1022
Mike Turquetteb24764902012-03-15 23:11:19 -07001023 if (clk->ops->set_rate)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001024 clk->ops->set_rate(clk->hw, clk->new_rate, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001025
1026 if (clk->ops->recalc_rate)
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001027 clk->rate = clk->ops->recalc_rate(clk->hw, best_parent_rate);
Mike Turquetteb24764902012-03-15 23:11:19 -07001028 else
Pawel Mollbf47b4f2012-06-08 14:04:06 +01001029 clk->rate = best_parent_rate;
Mike Turquetteb24764902012-03-15 23:11:19 -07001030
1031 if (clk->notifier_count && old_rate != clk->rate)
1032 __clk_notify(clk, POST_RATE_CHANGE, old_rate, clk->rate);
1033
1034 hlist_for_each_entry(child, tmp, &clk->children, child_node)
1035 clk_change_rate(child);
1036}
1037
1038/**
1039 * clk_set_rate - specify a new rate for clk
1040 * @clk: the clk whose rate is being changed
1041 * @rate: the new rate for clk
1042 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001043 * In the simplest case clk_set_rate will only adjust the rate of clk.
Mike Turquetteb24764902012-03-15 23:11:19 -07001044 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001045 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1046 * propagate up to clk's parent; whether or not this happens depends on the
1047 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1048 * after calling .round_rate then upstream parent propagation is ignored. If
1049 * *parent_rate comes back with a new rate for clk's parent then we propagate
1050 * up to clk's parent and set it's rate. Upward propagation will continue
1051 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1052 * .round_rate stops requesting changes to clk's parent_rate.
Mike Turquetteb24764902012-03-15 23:11:19 -07001053 *
Mike Turquette5654dc92012-03-26 11:51:34 -07001054 * Rate changes are accomplished via tree traversal that also recalculates the
1055 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
Mike Turquetteb24764902012-03-15 23:11:19 -07001056 *
1057 * Returns 0 on success, -EERROR otherwise.
1058 */
1059int clk_set_rate(struct clk *clk, unsigned long rate)
1060{
1061 struct clk *top, *fail_clk;
1062 int ret = 0;
1063
1064 /* prevent racing with updates to the clock topology */
1065 mutex_lock(&prepare_lock);
1066
1067 /* bail early if nothing to do */
1068 if (rate == clk->rate)
1069 goto out;
1070
Saravana Kannan7e0fa1b2012-05-15 13:43:42 -07001071 if ((clk->flags & CLK_SET_RATE_GATE) && clk->prepare_count) {
Viresh Kumar0e1c0302012-04-11 16:03:42 +05301072 ret = -EBUSY;
1073 goto out;
1074 }
1075
Mike Turquetteb24764902012-03-15 23:11:19 -07001076 /* calculate new rates and get the topmost changed clock */
1077 top = clk_calc_new_rates(clk, rate);
1078 if (!top) {
1079 ret = -EINVAL;
1080 goto out;
1081 }
1082
1083 /* notify that we are about to change rates */
1084 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1085 if (fail_clk) {
1086 pr_warn("%s: failed to set %s rate\n", __func__,
1087 fail_clk->name);
1088 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1089 ret = -EBUSY;
1090 goto out;
1091 }
1092
1093 /* change the rates */
1094 clk_change_rate(top);
1095
Mike Turquetteb24764902012-03-15 23:11:19 -07001096out:
1097 mutex_unlock(&prepare_lock);
1098
1099 return ret;
1100}
1101EXPORT_SYMBOL_GPL(clk_set_rate);
1102
1103/**
1104 * clk_get_parent - return the parent of a clk
1105 * @clk: the clk whose parent gets returned
1106 *
1107 * Simply returns clk->parent. Returns NULL if clk is NULL.
1108 */
1109struct clk *clk_get_parent(struct clk *clk)
1110{
1111 struct clk *parent;
1112
1113 mutex_lock(&prepare_lock);
1114 parent = __clk_get_parent(clk);
1115 mutex_unlock(&prepare_lock);
1116
1117 return parent;
1118}
1119EXPORT_SYMBOL_GPL(clk_get_parent);
1120
1121/*
1122 * .get_parent is mandatory for clocks with multiple possible parents. It is
1123 * optional for single-parent clocks. Always call .get_parent if it is
1124 * available and WARN if it is missing for multi-parent clocks.
1125 *
1126 * For single-parent clocks without .get_parent, first check to see if the
1127 * .parents array exists, and if so use it to avoid an expensive tree
1128 * traversal. If .parents does not exist then walk the tree with __clk_lookup.
1129 */
1130static struct clk *__clk_init_parent(struct clk *clk)
1131{
1132 struct clk *ret = NULL;
1133 u8 index;
1134
1135 /* handle the trivial cases */
1136
1137 if (!clk->num_parents)
1138 goto out;
1139
1140 if (clk->num_parents == 1) {
1141 if (IS_ERR_OR_NULL(clk->parent))
1142 ret = clk->parent = __clk_lookup(clk->parent_names[0]);
1143 ret = clk->parent;
1144 goto out;
1145 }
1146
1147 if (!clk->ops->get_parent) {
1148 WARN(!clk->ops->get_parent,
1149 "%s: multi-parent clocks must implement .get_parent\n",
1150 __func__);
1151 goto out;
1152 };
1153
1154 /*
1155 * Do our best to cache parent clocks in clk->parents. This prevents
1156 * unnecessary and expensive calls to __clk_lookup. We don't set
1157 * clk->parent here; that is done by the calling function
1158 */
1159
1160 index = clk->ops->get_parent(clk->hw);
1161
1162 if (!clk->parents)
1163 clk->parents =
Rajendra Nayak79750592012-06-06 14:41:31 +05301164 kzalloc((sizeof(struct clk*) * clk->num_parents),
Mike Turquetteb24764902012-03-15 23:11:19 -07001165 GFP_KERNEL);
1166
1167 if (!clk->parents)
1168 ret = __clk_lookup(clk->parent_names[index]);
1169 else if (!clk->parents[index])
1170 ret = clk->parents[index] =
1171 __clk_lookup(clk->parent_names[index]);
1172 else
1173 ret = clk->parents[index];
1174
1175out:
1176 return ret;
1177}
1178
1179void __clk_reparent(struct clk *clk, struct clk *new_parent)
1180{
1181#ifdef CONFIG_COMMON_CLK_DEBUG
1182 struct dentry *d;
1183 struct dentry *new_parent_d;
1184#endif
1185
1186 if (!clk || !new_parent)
1187 return;
1188
1189 hlist_del(&clk->child_node);
1190
1191 if (new_parent)
1192 hlist_add_head(&clk->child_node, &new_parent->children);
1193 else
1194 hlist_add_head(&clk->child_node, &clk_orphan_list);
1195
1196#ifdef CONFIG_COMMON_CLK_DEBUG
1197 if (!inited)
1198 goto out;
1199
1200 if (new_parent)
1201 new_parent_d = new_parent->dentry;
1202 else
1203 new_parent_d = orphandir;
1204
1205 d = debugfs_rename(clk->dentry->d_parent, clk->dentry,
1206 new_parent_d, clk->name);
1207 if (d)
1208 clk->dentry = d;
1209 else
1210 pr_debug("%s: failed to rename debugfs entry for %s\n",
1211 __func__, clk->name);
1212out:
1213#endif
1214
1215 clk->parent = new_parent;
1216
1217 __clk_recalc_rates(clk, POST_RATE_CHANGE);
1218}
1219
1220static int __clk_set_parent(struct clk *clk, struct clk *parent)
1221{
1222 struct clk *old_parent;
1223 unsigned long flags;
1224 int ret = -EINVAL;
1225 u8 i;
1226
1227 old_parent = clk->parent;
1228
Rajendra Nayak863b1322012-07-03 12:11:41 +05301229 if (!clk->parents)
Rajendra Nayak79750592012-06-06 14:41:31 +05301230 clk->parents = kzalloc((sizeof(struct clk*) * clk->num_parents),
1231 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07001232
1233 /*
Rajendra Nayak863b1322012-07-03 12:11:41 +05301234 * find index of new parent clock using cached parent ptrs,
1235 * or if not yet cached, use string name comparison and cache
1236 * them now to avoid future calls to __clk_lookup.
Mike Turquetteb24764902012-03-15 23:11:19 -07001237 */
Rajendra Nayak863b1322012-07-03 12:11:41 +05301238 for (i = 0; i < clk->num_parents; i++) {
1239 if (clk->parents && clk->parents[i] == parent)
1240 break;
1241 else if (!strcmp(clk->parent_names[i], parent->name)) {
1242 if (clk->parents)
1243 clk->parents[i] = __clk_lookup(parent->name);
1244 break;
1245 }
1246 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001247
1248 if (i == clk->num_parents) {
1249 pr_debug("%s: clock %s is not a possible parent of clock %s\n",
1250 __func__, parent->name, clk->name);
1251 goto out;
1252 }
1253
1254 /* migrate prepare and enable */
1255 if (clk->prepare_count)
1256 __clk_prepare(parent);
1257
1258 /* FIXME replace with clk_is_enabled(clk) someday */
1259 spin_lock_irqsave(&enable_lock, flags);
1260 if (clk->enable_count)
1261 __clk_enable(parent);
1262 spin_unlock_irqrestore(&enable_lock, flags);
1263
1264 /* change clock input source */
1265 ret = clk->ops->set_parent(clk->hw, i);
1266
1267 /* clean up old prepare and enable */
1268 spin_lock_irqsave(&enable_lock, flags);
1269 if (clk->enable_count)
1270 __clk_disable(old_parent);
1271 spin_unlock_irqrestore(&enable_lock, flags);
1272
1273 if (clk->prepare_count)
1274 __clk_unprepare(old_parent);
1275
1276out:
1277 return ret;
1278}
1279
1280/**
1281 * clk_set_parent - switch the parent of a mux clk
1282 * @clk: the mux clk whose input we are switching
1283 * @parent: the new input to clk
1284 *
1285 * Re-parent clk to use parent as it's new input source. If clk has the
1286 * CLK_SET_PARENT_GATE flag set then clk must be gated for this
1287 * operation to succeed. After successfully changing clk's parent
1288 * clk_set_parent will update the clk topology, sysfs topology and
1289 * propagate rate recalculation via __clk_recalc_rates. Returns 0 on
1290 * success, -EERROR otherwise.
1291 */
1292int clk_set_parent(struct clk *clk, struct clk *parent)
1293{
1294 int ret = 0;
1295
1296 if (!clk || !clk->ops)
1297 return -EINVAL;
1298
1299 if (!clk->ops->set_parent)
1300 return -ENOSYS;
1301
1302 /* prevent racing with updates to the clock topology */
1303 mutex_lock(&prepare_lock);
1304
1305 if (clk->parent == parent)
1306 goto out;
1307
1308 /* propagate PRE_RATE_CHANGE notifications */
1309 if (clk->notifier_count)
1310 ret = __clk_speculate_rates(clk, parent->rate);
1311
1312 /* abort if a driver objects */
1313 if (ret == NOTIFY_STOP)
1314 goto out;
1315
1316 /* only re-parent if the clock is not in use */
1317 if ((clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count)
1318 ret = -EBUSY;
1319 else
1320 ret = __clk_set_parent(clk, parent);
1321
1322 /* propagate ABORT_RATE_CHANGE if .set_parent failed */
1323 if (ret) {
1324 __clk_recalc_rates(clk, ABORT_RATE_CHANGE);
1325 goto out;
1326 }
1327
1328 /* propagate rate recalculation downstream */
1329 __clk_reparent(clk, parent);
1330
1331out:
1332 mutex_unlock(&prepare_lock);
1333
1334 return ret;
1335}
1336EXPORT_SYMBOL_GPL(clk_set_parent);
1337
1338/**
1339 * __clk_init - initialize the data structures in a struct clk
1340 * @dev: device initializing this clk, placeholder for now
1341 * @clk: clk being initialized
1342 *
1343 * Initializes the lists in struct clk, queries the hardware for the
1344 * parent and rate and sets them both.
Mike Turquetteb24764902012-03-15 23:11:19 -07001345 */
Mike Turquetted1302a32012-03-29 14:30:40 -07001346int __clk_init(struct device *dev, struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001347{
Mike Turquetted1302a32012-03-29 14:30:40 -07001348 int i, ret = 0;
Mike Turquetteb24764902012-03-15 23:11:19 -07001349 struct clk *orphan;
1350 struct hlist_node *tmp, *tmp2;
1351
1352 if (!clk)
Mike Turquetted1302a32012-03-29 14:30:40 -07001353 return -EINVAL;
Mike Turquetteb24764902012-03-15 23:11:19 -07001354
1355 mutex_lock(&prepare_lock);
1356
1357 /* check to see if a clock with this name is already registered */
Mike Turquetted1302a32012-03-29 14:30:40 -07001358 if (__clk_lookup(clk->name)) {
1359 pr_debug("%s: clk %s already initialized\n",
1360 __func__, clk->name);
1361 ret = -EEXIST;
Mike Turquetteb24764902012-03-15 23:11:19 -07001362 goto out;
Mike Turquetted1302a32012-03-29 14:30:40 -07001363 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001364
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001365 /* check that clk_ops are sane. See Documentation/clk.txt */
1366 if (clk->ops->set_rate &&
1367 !(clk->ops->round_rate && clk->ops->recalc_rate)) {
1368 pr_warning("%s: %s must implement .round_rate & .recalc_rate\n",
1369 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07001370 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001371 goto out;
1372 }
1373
1374 if (clk->ops->set_parent && !clk->ops->get_parent) {
1375 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
1376 __func__, clk->name);
Mike Turquetted1302a32012-03-29 14:30:40 -07001377 ret = -EINVAL;
Mike Turquetted4d7e3d2012-03-26 16:15:52 -07001378 goto out;
1379 }
1380
Mike Turquetteb24764902012-03-15 23:11:19 -07001381 /* throw a WARN if any entries in parent_names are NULL */
1382 for (i = 0; i < clk->num_parents; i++)
1383 WARN(!clk->parent_names[i],
1384 "%s: invalid NULL in %s's .parent_names\n",
1385 __func__, clk->name);
1386
1387 /*
1388 * Allocate an array of struct clk *'s to avoid unnecessary string
1389 * look-ups of clk's possible parents. This can fail for clocks passed
1390 * in to clk_init during early boot; thus any access to clk->parents[]
1391 * must always check for a NULL pointer and try to populate it if
1392 * necessary.
1393 *
1394 * If clk->parents is not NULL we skip this entire block. This allows
1395 * for clock drivers to statically initialize clk->parents.
1396 */
Rajendra Nayak9ca1c5a2012-06-06 14:41:30 +05301397 if (clk->num_parents > 1 && !clk->parents) {
1398 clk->parents = kzalloc((sizeof(struct clk*) * clk->num_parents),
Mike Turquetteb24764902012-03-15 23:11:19 -07001399 GFP_KERNEL);
1400 /*
1401 * __clk_lookup returns NULL for parents that have not been
1402 * clk_init'd; thus any access to clk->parents[] must check
1403 * for a NULL pointer. We can always perform lazy lookups for
1404 * missing parents later on.
1405 */
1406 if (clk->parents)
1407 for (i = 0; i < clk->num_parents; i++)
1408 clk->parents[i] =
1409 __clk_lookup(clk->parent_names[i]);
1410 }
1411
1412 clk->parent = __clk_init_parent(clk);
1413
1414 /*
1415 * Populate clk->parent if parent has already been __clk_init'd. If
1416 * parent has not yet been __clk_init'd then place clk in the orphan
1417 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
1418 * clk list.
1419 *
1420 * Every time a new clk is clk_init'd then we walk the list of orphan
1421 * clocks and re-parent any that are children of the clock currently
1422 * being clk_init'd.
1423 */
1424 if (clk->parent)
1425 hlist_add_head(&clk->child_node,
1426 &clk->parent->children);
1427 else if (clk->flags & CLK_IS_ROOT)
1428 hlist_add_head(&clk->child_node, &clk_root_list);
1429 else
1430 hlist_add_head(&clk->child_node, &clk_orphan_list);
1431
1432 /*
1433 * Set clk's rate. The preferred method is to use .recalc_rate. For
1434 * simple clocks and lazy developers the default fallback is to use the
1435 * parent's rate. If a clock doesn't have a parent (or is orphaned)
1436 * then rate is set to zero.
1437 */
1438 if (clk->ops->recalc_rate)
1439 clk->rate = clk->ops->recalc_rate(clk->hw,
1440 __clk_get_rate(clk->parent));
1441 else if (clk->parent)
1442 clk->rate = clk->parent->rate;
1443 else
1444 clk->rate = 0;
1445
1446 /*
1447 * walk the list of orphan clocks and reparent any that are children of
1448 * this clock
1449 */
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01001450 hlist_for_each_entry_safe(orphan, tmp, tmp2, &clk_orphan_list, child_node) {
1451 if (orphan->ops->get_parent) {
1452 i = orphan->ops->get_parent(orphan->hw);
1453 if (!strcmp(clk->name, orphan->parent_names[i]))
1454 __clk_reparent(orphan, clk);
1455 continue;
1456 }
1457
Mike Turquetteb24764902012-03-15 23:11:19 -07001458 for (i = 0; i < orphan->num_parents; i++)
1459 if (!strcmp(clk->name, orphan->parent_names[i])) {
1460 __clk_reparent(orphan, clk);
1461 break;
1462 }
Martin Fuzzey1f61e5f2012-11-22 20:15:05 +01001463 }
Mike Turquetteb24764902012-03-15 23:11:19 -07001464
1465 /*
1466 * optional platform-specific magic
1467 *
1468 * The .init callback is not used by any of the basic clock types, but
1469 * exists for weird hardware that must perform initialization magic.
1470 * Please consider other ways of solving initialization problems before
1471 * using this callback, as it's use is discouraged.
1472 */
1473 if (clk->ops->init)
1474 clk->ops->init(clk->hw);
1475
1476 clk_debug_register(clk);
1477
1478out:
1479 mutex_unlock(&prepare_lock);
1480
Mike Turquetted1302a32012-03-29 14:30:40 -07001481 return ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001482}
1483
1484/**
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001485 * __clk_register - register a clock and return a cookie.
1486 *
1487 * Same as clk_register, except that the .clk field inside hw shall point to a
1488 * preallocated (generally statically allocated) struct clk. None of the fields
1489 * of the struct clk need to be initialized.
1490 *
1491 * The data pointed to by .init and .clk field shall NOT be marked as init
1492 * data.
1493 *
1494 * __clk_register is only exposed via clk-private.h and is intended for use with
1495 * very large numbers of clocks that need to be statically initialized. It is
1496 * a layering violation to include clk-private.h from any code which implements
1497 * a clock's .ops; as such any statically initialized clock data MUST be in a
1498 * separate C file from the logic that implements it's operations. Returns 0
1499 * on success, otherwise an error code.
1500 */
1501struct clk *__clk_register(struct device *dev, struct clk_hw *hw)
1502{
1503 int ret;
1504 struct clk *clk;
1505
1506 clk = hw->clk;
1507 clk->name = hw->init->name;
1508 clk->ops = hw->init->ops;
1509 clk->hw = hw;
1510 clk->flags = hw->init->flags;
1511 clk->parent_names = hw->init->parent_names;
1512 clk->num_parents = hw->init->num_parents;
1513
1514 ret = __clk_init(dev, clk);
1515 if (ret)
1516 return ERR_PTR(ret);
1517
1518 return clk;
1519}
1520EXPORT_SYMBOL_GPL(__clk_register);
1521
Stephen Boyd46c87732012-09-24 13:38:04 -07001522static int _clk_register(struct device *dev, struct clk_hw *hw, struct clk *clk)
Mike Turquetteb24764902012-03-15 23:11:19 -07001523{
Mike Turquetted1302a32012-03-29 14:30:40 -07001524 int i, ret;
Mike Turquetteb24764902012-03-15 23:11:19 -07001525
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001526 clk->name = kstrdup(hw->init->name, GFP_KERNEL);
1527 if (!clk->name) {
1528 pr_err("%s: could not allocate clk->name\n", __func__);
1529 ret = -ENOMEM;
1530 goto fail_name;
1531 }
1532 clk->ops = hw->init->ops;
Mike Turquetteb24764902012-03-15 23:11:19 -07001533 clk->hw = hw;
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001534 clk->flags = hw->init->flags;
1535 clk->num_parents = hw->init->num_parents;
Mike Turquetteb24764902012-03-15 23:11:19 -07001536 hw->clk = clk;
1537
Mike Turquetted1302a32012-03-29 14:30:40 -07001538 /* allocate local copy in case parent_names is __initdata */
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001539 clk->parent_names = kzalloc((sizeof(char*) * clk->num_parents),
Mike Turquetted1302a32012-03-29 14:30:40 -07001540 GFP_KERNEL);
Mike Turquetteb24764902012-03-15 23:11:19 -07001541
Mike Turquetted1302a32012-03-29 14:30:40 -07001542 if (!clk->parent_names) {
1543 pr_err("%s: could not allocate clk->parent_names\n", __func__);
1544 ret = -ENOMEM;
1545 goto fail_parent_names;
1546 }
1547
1548
1549 /* copy each string name in case parent_names is __initdata */
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001550 for (i = 0; i < clk->num_parents; i++) {
1551 clk->parent_names[i] = kstrdup(hw->init->parent_names[i],
1552 GFP_KERNEL);
Mike Turquetted1302a32012-03-29 14:30:40 -07001553 if (!clk->parent_names[i]) {
1554 pr_err("%s: could not copy parent_names\n", __func__);
1555 ret = -ENOMEM;
1556 goto fail_parent_names_copy;
1557 }
1558 }
1559
1560 ret = __clk_init(dev, clk);
1561 if (!ret)
Stephen Boyd46c87732012-09-24 13:38:04 -07001562 return 0;
Mike Turquetted1302a32012-03-29 14:30:40 -07001563
1564fail_parent_names_copy:
1565 while (--i >= 0)
1566 kfree(clk->parent_names[i]);
1567 kfree(clk->parent_names);
1568fail_parent_names:
Saravana Kannan0197b3e2012-04-25 22:58:56 -07001569 kfree(clk->name);
1570fail_name:
Stephen Boyd46c87732012-09-24 13:38:04 -07001571 return ret;
1572}
1573
1574/**
1575 * clk_register - allocate a new clock, register it and return an opaque cookie
1576 * @dev: device that is registering this clock
1577 * @hw: link to hardware-specific clock data
1578 *
1579 * clk_register is the primary interface for populating the clock tree with new
1580 * clock nodes. It returns a pointer to the newly allocated struct clk which
1581 * cannot be dereferenced by driver code but may be used in conjuction with the
1582 * rest of the clock API. In the event of an error clk_register will return an
1583 * error code; drivers must test for an error code after calling clk_register.
1584 */
1585struct clk *clk_register(struct device *dev, struct clk_hw *hw)
1586{
1587 int ret;
1588 struct clk *clk;
1589
1590 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
1591 if (!clk) {
1592 pr_err("%s: could not allocate clk\n", __func__);
1593 ret = -ENOMEM;
1594 goto fail_out;
1595 }
1596
1597 ret = _clk_register(dev, hw, clk);
1598 if (!ret)
1599 return clk;
1600
Mike Turquetted1302a32012-03-29 14:30:40 -07001601 kfree(clk);
1602fail_out:
1603 return ERR_PTR(ret);
Mike Turquetteb24764902012-03-15 23:11:19 -07001604}
1605EXPORT_SYMBOL_GPL(clk_register);
1606
Mark Brown1df5c932012-04-18 09:07:12 +01001607/**
1608 * clk_unregister - unregister a currently registered clock
1609 * @clk: clock to unregister
1610 *
1611 * Currently unimplemented.
1612 */
1613void clk_unregister(struct clk *clk) {}
1614EXPORT_SYMBOL_GPL(clk_unregister);
1615
Stephen Boyd46c87732012-09-24 13:38:04 -07001616static void devm_clk_release(struct device *dev, void *res)
1617{
1618 clk_unregister(res);
1619}
1620
1621/**
1622 * devm_clk_register - resource managed clk_register()
1623 * @dev: device that is registering this clock
1624 * @hw: link to hardware-specific clock data
1625 *
1626 * Managed clk_register(). Clocks returned from this function are
1627 * automatically clk_unregister()ed on driver detach. See clk_register() for
1628 * more information.
1629 */
1630struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
1631{
1632 struct clk *clk;
1633 int ret;
1634
1635 clk = devres_alloc(devm_clk_release, sizeof(*clk), GFP_KERNEL);
1636 if (!clk)
1637 return ERR_PTR(-ENOMEM);
1638
1639 ret = _clk_register(dev, hw, clk);
1640 if (!ret) {
1641 devres_add(dev, clk);
1642 } else {
1643 devres_free(clk);
1644 clk = ERR_PTR(ret);
1645 }
1646
1647 return clk;
1648}
1649EXPORT_SYMBOL_GPL(devm_clk_register);
1650
1651static int devm_clk_match(struct device *dev, void *res, void *data)
1652{
1653 struct clk *c = res;
1654 if (WARN_ON(!c))
1655 return 0;
1656 return c == data;
1657}
1658
1659/**
1660 * devm_clk_unregister - resource managed clk_unregister()
1661 * @clk: clock to unregister
1662 *
1663 * Deallocate a clock allocated with devm_clk_register(). Normally
1664 * this function will not need to be called and the resource management
1665 * code will ensure that the resource is freed.
1666 */
1667void devm_clk_unregister(struct device *dev, struct clk *clk)
1668{
1669 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
1670}
1671EXPORT_SYMBOL_GPL(devm_clk_unregister);
1672
Mike Turquetteb24764902012-03-15 23:11:19 -07001673/*** clk rate change notifiers ***/
1674
1675/**
1676 * clk_notifier_register - add a clk rate change notifier
1677 * @clk: struct clk * to watch
1678 * @nb: struct notifier_block * with callback info
1679 *
1680 * Request notification when clk's rate changes. This uses an SRCU
1681 * notifier because we want it to block and notifier unregistrations are
1682 * uncommon. The callbacks associated with the notifier must not
1683 * re-enter into the clk framework by calling any top-level clk APIs;
1684 * this will cause a nested prepare_lock mutex.
1685 *
1686 * Pre-change notifier callbacks will be passed the current, pre-change
1687 * rate of the clk via struct clk_notifier_data.old_rate. The new,
1688 * post-change rate of the clk is passed via struct
1689 * clk_notifier_data.new_rate.
1690 *
1691 * Post-change notifiers will pass the now-current, post-change rate of
1692 * the clk in both struct clk_notifier_data.old_rate and struct
1693 * clk_notifier_data.new_rate.
1694 *
1695 * Abort-change notifiers are effectively the opposite of pre-change
1696 * notifiers: the original pre-change clk rate is passed in via struct
1697 * clk_notifier_data.new_rate and the failed post-change rate is passed
1698 * in via struct clk_notifier_data.old_rate.
1699 *
1700 * clk_notifier_register() must be called from non-atomic context.
1701 * Returns -EINVAL if called with null arguments, -ENOMEM upon
1702 * allocation failure; otherwise, passes along the return value of
1703 * srcu_notifier_chain_register().
1704 */
1705int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
1706{
1707 struct clk_notifier *cn;
1708 int ret = -ENOMEM;
1709
1710 if (!clk || !nb)
1711 return -EINVAL;
1712
1713 mutex_lock(&prepare_lock);
1714
1715 /* search the list of notifiers for this clk */
1716 list_for_each_entry(cn, &clk_notifier_list, node)
1717 if (cn->clk == clk)
1718 break;
1719
1720 /* if clk wasn't in the notifier list, allocate new clk_notifier */
1721 if (cn->clk != clk) {
1722 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
1723 if (!cn)
1724 goto out;
1725
1726 cn->clk = clk;
1727 srcu_init_notifier_head(&cn->notifier_head);
1728
1729 list_add(&cn->node, &clk_notifier_list);
1730 }
1731
1732 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
1733
1734 clk->notifier_count++;
1735
1736out:
1737 mutex_unlock(&prepare_lock);
1738
1739 return ret;
1740}
1741EXPORT_SYMBOL_GPL(clk_notifier_register);
1742
1743/**
1744 * clk_notifier_unregister - remove a clk rate change notifier
1745 * @clk: struct clk *
1746 * @nb: struct notifier_block * with callback info
1747 *
1748 * Request no further notification for changes to 'clk' and frees memory
1749 * allocated in clk_notifier_register.
1750 *
1751 * Returns -EINVAL if called with null arguments; otherwise, passes
1752 * along the return value of srcu_notifier_chain_unregister().
1753 */
1754int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
1755{
1756 struct clk_notifier *cn = NULL;
1757 int ret = -EINVAL;
1758
1759 if (!clk || !nb)
1760 return -EINVAL;
1761
1762 mutex_lock(&prepare_lock);
1763
1764 list_for_each_entry(cn, &clk_notifier_list, node)
1765 if (cn->clk == clk)
1766 break;
1767
1768 if (cn->clk == clk) {
1769 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
1770
1771 clk->notifier_count--;
1772
1773 /* XXX the notifier code should handle this better */
1774 if (!cn->notifier_head.head) {
1775 srcu_cleanup_notifier_head(&cn->notifier_head);
1776 kfree(cn);
1777 }
1778
1779 } else {
1780 ret = -ENOENT;
1781 }
1782
1783 mutex_unlock(&prepare_lock);
1784
1785 return ret;
1786}
1787EXPORT_SYMBOL_GPL(clk_notifier_unregister);
Grant Likely766e6a42012-04-09 14:50:06 -05001788
1789#ifdef CONFIG_OF
1790/**
1791 * struct of_clk_provider - Clock provider registration structure
1792 * @link: Entry in global list of clock providers
1793 * @node: Pointer to device tree node of clock provider
1794 * @get: Get clock callback. Returns NULL or a struct clk for the
1795 * given clock specifier
1796 * @data: context pointer to be passed into @get callback
1797 */
1798struct of_clk_provider {
1799 struct list_head link;
1800
1801 struct device_node *node;
1802 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
1803 void *data;
1804};
1805
1806static LIST_HEAD(of_clk_providers);
1807static DEFINE_MUTEX(of_clk_lock);
1808
1809struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
1810 void *data)
1811{
1812 return data;
1813}
1814EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
1815
Shawn Guo494bfec2012-08-22 21:36:27 +08001816struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
1817{
1818 struct clk_onecell_data *clk_data = data;
1819 unsigned int idx = clkspec->args[0];
1820
1821 if (idx >= clk_data->clk_num) {
1822 pr_err("%s: invalid clock index %d\n", __func__, idx);
1823 return ERR_PTR(-EINVAL);
1824 }
1825
1826 return clk_data->clks[idx];
1827}
1828EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
1829
Grant Likely766e6a42012-04-09 14:50:06 -05001830/**
1831 * of_clk_add_provider() - Register a clock provider for a node
1832 * @np: Device node pointer associated with clock provider
1833 * @clk_src_get: callback for decoding clock
1834 * @data: context pointer for @clk_src_get callback.
1835 */
1836int of_clk_add_provider(struct device_node *np,
1837 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
1838 void *data),
1839 void *data)
1840{
1841 struct of_clk_provider *cp;
1842
1843 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
1844 if (!cp)
1845 return -ENOMEM;
1846
1847 cp->node = of_node_get(np);
1848 cp->data = data;
1849 cp->get = clk_src_get;
1850
1851 mutex_lock(&of_clk_lock);
1852 list_add(&cp->link, &of_clk_providers);
1853 mutex_unlock(&of_clk_lock);
1854 pr_debug("Added clock from %s\n", np->full_name);
1855
1856 return 0;
1857}
1858EXPORT_SYMBOL_GPL(of_clk_add_provider);
1859
1860/**
1861 * of_clk_del_provider() - Remove a previously registered clock provider
1862 * @np: Device node pointer associated with clock provider
1863 */
1864void of_clk_del_provider(struct device_node *np)
1865{
1866 struct of_clk_provider *cp;
1867
1868 mutex_lock(&of_clk_lock);
1869 list_for_each_entry(cp, &of_clk_providers, link) {
1870 if (cp->node == np) {
1871 list_del(&cp->link);
1872 of_node_put(cp->node);
1873 kfree(cp);
1874 break;
1875 }
1876 }
1877 mutex_unlock(&of_clk_lock);
1878}
1879EXPORT_SYMBOL_GPL(of_clk_del_provider);
1880
1881struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
1882{
1883 struct of_clk_provider *provider;
1884 struct clk *clk = ERR_PTR(-ENOENT);
1885
1886 /* Check if we have such a provider in our array */
1887 mutex_lock(&of_clk_lock);
1888 list_for_each_entry(provider, &of_clk_providers, link) {
1889 if (provider->node == clkspec->np)
1890 clk = provider->get(clkspec, provider->data);
1891 if (!IS_ERR(clk))
1892 break;
1893 }
1894 mutex_unlock(&of_clk_lock);
1895
1896 return clk;
1897}
1898
1899const char *of_clk_get_parent_name(struct device_node *np, int index)
1900{
1901 struct of_phandle_args clkspec;
1902 const char *clk_name;
1903 int rc;
1904
1905 if (index < 0)
1906 return NULL;
1907
1908 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
1909 &clkspec);
1910 if (rc)
1911 return NULL;
1912
1913 if (of_property_read_string_index(clkspec.np, "clock-output-names",
1914 clkspec.args_count ? clkspec.args[0] : 0,
1915 &clk_name) < 0)
1916 clk_name = clkspec.np->name;
1917
1918 of_node_put(clkspec.np);
1919 return clk_name;
1920}
1921EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
1922
1923/**
1924 * of_clk_init() - Scan and init clock providers from the DT
1925 * @matches: array of compatible values and init functions for providers.
1926 *
1927 * This function scans the device tree for matching clock providers and
1928 * calls their initialization functions
1929 */
1930void __init of_clk_init(const struct of_device_id *matches)
1931{
1932 struct device_node *np;
1933
1934 for_each_matching_node(np, matches) {
1935 const struct of_device_id *match = of_match_node(matches, np);
1936 of_clk_init_cb_t clk_init_cb = match->data;
1937 clk_init_cb(np);
1938 }
1939}
1940#endif