Linux 3.9-rc4
[linux-2.6/cjktty.git] / drivers / clk / clk.c
blobed87b2405806f6a80f24b607be5d23ccc922ce26
1 /*
2 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
3 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
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.
9 * Standard functionality for the common clock API. See Documentation/clk.txt
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>
19 #include <linux/of.h>
20 #include <linux/device.h>
21 #include <linux/init.h>
23 static DEFINE_SPINLOCK(enable_lock);
24 static DEFINE_MUTEX(prepare_lock);
26 static HLIST_HEAD(clk_root_list);
27 static HLIST_HEAD(clk_orphan_list);
28 static LIST_HEAD(clk_notifier_list);
30 /*** debugfs support ***/
32 #ifdef CONFIG_COMMON_CLK_DEBUG
33 #include <linux/debugfs.h>
35 static struct dentry *rootdir;
36 static struct dentry *orphandir;
37 static int inited = 0;
39 static void clk_summary_show_one(struct seq_file *s, struct clk *c, int level)
41 if (!c)
42 return;
44 seq_printf(s, "%*s%-*s %-11d %-12d %-10lu",
45 level * 3 + 1, "",
46 30 - level * 3, c->name,
47 c->enable_count, c->prepare_count, c->rate);
48 seq_printf(s, "\n");
51 static void clk_summary_show_subtree(struct seq_file *s, struct clk *c,
52 int level)
54 struct clk *child;
56 if (!c)
57 return;
59 clk_summary_show_one(s, c, level);
61 hlist_for_each_entry(child, &c->children, child_node)
62 clk_summary_show_subtree(s, child, level + 1);
65 static int clk_summary_show(struct seq_file *s, void *data)
67 struct clk *c;
69 seq_printf(s, " clock enable_cnt prepare_cnt rate\n");
70 seq_printf(s, "---------------------------------------------------------------------\n");
72 mutex_lock(&prepare_lock);
74 hlist_for_each_entry(c, &clk_root_list, child_node)
75 clk_summary_show_subtree(s, c, 0);
77 hlist_for_each_entry(c, &clk_orphan_list, child_node)
78 clk_summary_show_subtree(s, c, 0);
80 mutex_unlock(&prepare_lock);
82 return 0;
86 static int clk_summary_open(struct inode *inode, struct file *file)
88 return single_open(file, clk_summary_show, inode->i_private);
91 static const struct file_operations clk_summary_fops = {
92 .open = clk_summary_open,
93 .read = seq_read,
94 .llseek = seq_lseek,
95 .release = single_release,
98 static void clk_dump_one(struct seq_file *s, struct clk *c, int level)
100 if (!c)
101 return;
103 seq_printf(s, "\"%s\": { ", c->name);
104 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
105 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
106 seq_printf(s, "\"rate\": %lu", c->rate);
109 static void clk_dump_subtree(struct seq_file *s, struct clk *c, int level)
111 struct clk *child;
113 if (!c)
114 return;
116 clk_dump_one(s, c, level);
118 hlist_for_each_entry(child, &c->children, child_node) {
119 seq_printf(s, ",");
120 clk_dump_subtree(s, child, level + 1);
123 seq_printf(s, "}");
126 static int clk_dump(struct seq_file *s, void *data)
128 struct clk *c;
129 bool first_node = true;
131 seq_printf(s, "{");
133 mutex_lock(&prepare_lock);
135 hlist_for_each_entry(c, &clk_root_list, child_node) {
136 if (!first_node)
137 seq_printf(s, ",");
138 first_node = false;
139 clk_dump_subtree(s, c, 0);
142 hlist_for_each_entry(c, &clk_orphan_list, child_node) {
143 seq_printf(s, ",");
144 clk_dump_subtree(s, c, 0);
147 mutex_unlock(&prepare_lock);
149 seq_printf(s, "}");
150 return 0;
154 static int clk_dump_open(struct inode *inode, struct file *file)
156 return single_open(file, clk_dump, inode->i_private);
159 static const struct file_operations clk_dump_fops = {
160 .open = clk_dump_open,
161 .read = seq_read,
162 .llseek = seq_lseek,
163 .release = single_release,
166 /* caller must hold prepare_lock */
167 static int clk_debug_create_one(struct clk *clk, struct dentry *pdentry)
169 struct dentry *d;
170 int ret = -ENOMEM;
172 if (!clk || !pdentry) {
173 ret = -EINVAL;
174 goto out;
177 d = debugfs_create_dir(clk->name, pdentry);
178 if (!d)
179 goto out;
181 clk->dentry = d;
183 d = debugfs_create_u32("clk_rate", S_IRUGO, clk->dentry,
184 (u32 *)&clk->rate);
185 if (!d)
186 goto err_out;
188 d = debugfs_create_x32("clk_flags", S_IRUGO, clk->dentry,
189 (u32 *)&clk->flags);
190 if (!d)
191 goto err_out;
193 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, clk->dentry,
194 (u32 *)&clk->prepare_count);
195 if (!d)
196 goto err_out;
198 d = debugfs_create_u32("clk_enable_count", S_IRUGO, clk->dentry,
199 (u32 *)&clk->enable_count);
200 if (!d)
201 goto err_out;
203 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, clk->dentry,
204 (u32 *)&clk->notifier_count);
205 if (!d)
206 goto err_out;
208 ret = 0;
209 goto out;
211 err_out:
212 debugfs_remove(clk->dentry);
213 out:
214 return ret;
217 /* caller must hold prepare_lock */
218 static int clk_debug_create_subtree(struct clk *clk, struct dentry *pdentry)
220 struct clk *child;
221 int ret = -EINVAL;;
223 if (!clk || !pdentry)
224 goto out;
226 ret = clk_debug_create_one(clk, pdentry);
228 if (ret)
229 goto out;
231 hlist_for_each_entry(child, &clk->children, child_node)
232 clk_debug_create_subtree(child, clk->dentry);
234 ret = 0;
235 out:
236 return ret;
240 * clk_debug_register - add a clk node to the debugfs clk tree
241 * @clk: the clk being added to the debugfs clk tree
243 * Dynamically adds a clk to the debugfs clk tree if debugfs has been
244 * initialized. Otherwise it bails out early since the debugfs clk tree
245 * will be created lazily by clk_debug_init as part of a late_initcall.
247 * Caller must hold prepare_lock. Only clk_init calls this function (so
248 * far) so this is taken care.
250 static int clk_debug_register(struct clk *clk)
252 struct clk *parent;
253 struct dentry *pdentry;
254 int ret = 0;
256 if (!inited)
257 goto out;
259 parent = clk->parent;
262 * Check to see if a clk is a root clk. Also check that it is
263 * safe to add this clk to debugfs
265 if (!parent)
266 if (clk->flags & CLK_IS_ROOT)
267 pdentry = rootdir;
268 else
269 pdentry = orphandir;
270 else
271 if (parent->dentry)
272 pdentry = parent->dentry;
273 else
274 goto out;
276 ret = clk_debug_create_subtree(clk, pdentry);
278 out:
279 return ret;
283 * clk_debug_init - lazily create the debugfs clk tree visualization
285 * clks are often initialized very early during boot before memory can
286 * be dynamically allocated and well before debugfs is setup.
287 * clk_debug_init walks the clk tree hierarchy while holding
288 * prepare_lock and creates the topology as part of a late_initcall,
289 * thus insuring that clks initialized very early will still be
290 * represented in the debugfs clk tree. This function should only be
291 * called once at boot-time, and all other clks added dynamically will
292 * be done so with clk_debug_register.
294 static int __init clk_debug_init(void)
296 struct clk *clk;
297 struct dentry *d;
299 rootdir = debugfs_create_dir("clk", NULL);
301 if (!rootdir)
302 return -ENOMEM;
304 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, NULL,
305 &clk_summary_fops);
306 if (!d)
307 return -ENOMEM;
309 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, NULL,
310 &clk_dump_fops);
311 if (!d)
312 return -ENOMEM;
314 orphandir = debugfs_create_dir("orphans", rootdir);
316 if (!orphandir)
317 return -ENOMEM;
319 mutex_lock(&prepare_lock);
321 hlist_for_each_entry(clk, &clk_root_list, child_node)
322 clk_debug_create_subtree(clk, rootdir);
324 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
325 clk_debug_create_subtree(clk, orphandir);
327 inited = 1;
329 mutex_unlock(&prepare_lock);
331 return 0;
333 late_initcall(clk_debug_init);
334 #else
335 static inline int clk_debug_register(struct clk *clk) { return 0; }
336 #endif
338 /* caller must hold prepare_lock */
339 static void clk_disable_unused_subtree(struct clk *clk)
341 struct clk *child;
342 unsigned long flags;
344 if (!clk)
345 goto out;
347 hlist_for_each_entry(child, &clk->children, child_node)
348 clk_disable_unused_subtree(child);
350 spin_lock_irqsave(&enable_lock, flags);
352 if (clk->enable_count)
353 goto unlock_out;
355 if (clk->flags & CLK_IGNORE_UNUSED)
356 goto unlock_out;
359 * some gate clocks have special needs during the disable-unused
360 * sequence. call .disable_unused if available, otherwise fall
361 * back to .disable
363 if (__clk_is_enabled(clk)) {
364 if (clk->ops->disable_unused)
365 clk->ops->disable_unused(clk->hw);
366 else if (clk->ops->disable)
367 clk->ops->disable(clk->hw);
370 unlock_out:
371 spin_unlock_irqrestore(&enable_lock, flags);
373 out:
374 return;
377 static int clk_disable_unused(void)
379 struct clk *clk;
381 mutex_lock(&prepare_lock);
383 hlist_for_each_entry(clk, &clk_root_list, child_node)
384 clk_disable_unused_subtree(clk);
386 hlist_for_each_entry(clk, &clk_orphan_list, child_node)
387 clk_disable_unused_subtree(clk);
389 mutex_unlock(&prepare_lock);
391 return 0;
393 late_initcall(clk_disable_unused);
395 /*** helper functions ***/
397 const char *__clk_get_name(struct clk *clk)
399 return !clk ? NULL : clk->name;
401 EXPORT_SYMBOL_GPL(__clk_get_name);
403 struct clk_hw *__clk_get_hw(struct clk *clk)
405 return !clk ? NULL : clk->hw;
408 u8 __clk_get_num_parents(struct clk *clk)
410 return !clk ? 0 : clk->num_parents;
413 struct clk *__clk_get_parent(struct clk *clk)
415 return !clk ? NULL : clk->parent;
418 unsigned int __clk_get_enable_count(struct clk *clk)
420 return !clk ? 0 : clk->enable_count;
423 unsigned int __clk_get_prepare_count(struct clk *clk)
425 return !clk ? 0 : clk->prepare_count;
428 unsigned long __clk_get_rate(struct clk *clk)
430 unsigned long ret;
432 if (!clk) {
433 ret = 0;
434 goto out;
437 ret = clk->rate;
439 if (clk->flags & CLK_IS_ROOT)
440 goto out;
442 if (!clk->parent)
443 ret = 0;
445 out:
446 return ret;
449 unsigned long __clk_get_flags(struct clk *clk)
451 return !clk ? 0 : clk->flags;
454 bool __clk_is_enabled(struct clk *clk)
456 int ret;
458 if (!clk)
459 return false;
462 * .is_enabled is only mandatory for clocks that gate
463 * fall back to software usage counter if .is_enabled is missing
465 if (!clk->ops->is_enabled) {
466 ret = clk->enable_count ? 1 : 0;
467 goto out;
470 ret = clk->ops->is_enabled(clk->hw);
471 out:
472 return !!ret;
475 static struct clk *__clk_lookup_subtree(const char *name, struct clk *clk)
477 struct clk *child;
478 struct clk *ret;
480 if (!strcmp(clk->name, name))
481 return clk;
483 hlist_for_each_entry(child, &clk->children, child_node) {
484 ret = __clk_lookup_subtree(name, child);
485 if (ret)
486 return ret;
489 return NULL;
492 struct clk *__clk_lookup(const char *name)
494 struct clk *root_clk;
495 struct clk *ret;
497 if (!name)
498 return NULL;
500 /* search the 'proper' clk tree first */
501 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
502 ret = __clk_lookup_subtree(name, root_clk);
503 if (ret)
504 return ret;
507 /* if not found, then search the orphan tree */
508 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
509 ret = __clk_lookup_subtree(name, root_clk);
510 if (ret)
511 return ret;
514 return NULL;
517 /*** clk api ***/
519 void __clk_unprepare(struct clk *clk)
521 if (!clk)
522 return;
524 if (WARN_ON(clk->prepare_count == 0))
525 return;
527 if (--clk->prepare_count > 0)
528 return;
530 WARN_ON(clk->enable_count > 0);
532 if (clk->ops->unprepare)
533 clk->ops->unprepare(clk->hw);
535 __clk_unprepare(clk->parent);
539 * clk_unprepare - undo preparation of a clock source
540 * @clk: the clk being unprepare
542 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
543 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
544 * if the operation may sleep. One example is a clk which is accessed over
545 * I2c. In the complex case a clk gate operation may require a fast and a slow
546 * part. It is this reason that clk_unprepare and clk_disable are not mutually
547 * exclusive. In fact clk_disable must be called before clk_unprepare.
549 void clk_unprepare(struct clk *clk)
551 mutex_lock(&prepare_lock);
552 __clk_unprepare(clk);
553 mutex_unlock(&prepare_lock);
555 EXPORT_SYMBOL_GPL(clk_unprepare);
557 int __clk_prepare(struct clk *clk)
559 int ret = 0;
561 if (!clk)
562 return 0;
564 if (clk->prepare_count == 0) {
565 ret = __clk_prepare(clk->parent);
566 if (ret)
567 return ret;
569 if (clk->ops->prepare) {
570 ret = clk->ops->prepare(clk->hw);
571 if (ret) {
572 __clk_unprepare(clk->parent);
573 return ret;
578 clk->prepare_count++;
580 return 0;
584 * clk_prepare - prepare a clock source
585 * @clk: the clk being prepared
587 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
588 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
589 * operation may sleep. One example is a clk which is accessed over I2c. In
590 * the complex case a clk ungate operation may require a fast and a slow part.
591 * It is this reason that clk_prepare and clk_enable are not mutually
592 * exclusive. In fact clk_prepare must be called before clk_enable.
593 * Returns 0 on success, -EERROR otherwise.
595 int clk_prepare(struct clk *clk)
597 int ret;
599 mutex_lock(&prepare_lock);
600 ret = __clk_prepare(clk);
601 mutex_unlock(&prepare_lock);
603 return ret;
605 EXPORT_SYMBOL_GPL(clk_prepare);
607 static void __clk_disable(struct clk *clk)
609 if (!clk)
610 return;
612 if (WARN_ON(IS_ERR(clk)))
613 return;
615 if (WARN_ON(clk->enable_count == 0))
616 return;
618 if (--clk->enable_count > 0)
619 return;
621 if (clk->ops->disable)
622 clk->ops->disable(clk->hw);
624 __clk_disable(clk->parent);
628 * clk_disable - gate a clock
629 * @clk: the clk being gated
631 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
632 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
633 * clk if the operation is fast and will never sleep. One example is a
634 * SoC-internal clk which is controlled via simple register writes. In the
635 * complex case a clk gate operation may require a fast and a slow part. It is
636 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
637 * In fact clk_disable must be called before clk_unprepare.
639 void clk_disable(struct clk *clk)
641 unsigned long flags;
643 spin_lock_irqsave(&enable_lock, flags);
644 __clk_disable(clk);
645 spin_unlock_irqrestore(&enable_lock, flags);
647 EXPORT_SYMBOL_GPL(clk_disable);
649 static int __clk_enable(struct clk *clk)
651 int ret = 0;
653 if (!clk)
654 return 0;
656 if (WARN_ON(clk->prepare_count == 0))
657 return -ESHUTDOWN;
659 if (clk->enable_count == 0) {
660 ret = __clk_enable(clk->parent);
662 if (ret)
663 return ret;
665 if (clk->ops->enable) {
666 ret = clk->ops->enable(clk->hw);
667 if (ret) {
668 __clk_disable(clk->parent);
669 return ret;
674 clk->enable_count++;
675 return 0;
679 * clk_enable - ungate a clock
680 * @clk: the clk being ungated
682 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
683 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
684 * if the operation will never sleep. One example is a SoC-internal clk which
685 * is controlled via simple register writes. In the complex case a clk ungate
686 * operation may require a fast and a slow part. It is this reason that
687 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
688 * must be called before clk_enable. Returns 0 on success, -EERROR
689 * otherwise.
691 int clk_enable(struct clk *clk)
693 unsigned long flags;
694 int ret;
696 spin_lock_irqsave(&enable_lock, flags);
697 ret = __clk_enable(clk);
698 spin_unlock_irqrestore(&enable_lock, flags);
700 return ret;
702 EXPORT_SYMBOL_GPL(clk_enable);
705 * __clk_round_rate - round the given rate for a clk
706 * @clk: round the rate of this clock
708 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
710 unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
712 unsigned long parent_rate = 0;
714 if (!clk)
715 return 0;
717 if (!clk->ops->round_rate) {
718 if (clk->flags & CLK_SET_RATE_PARENT)
719 return __clk_round_rate(clk->parent, rate);
720 else
721 return clk->rate;
724 if (clk->parent)
725 parent_rate = clk->parent->rate;
727 return clk->ops->round_rate(clk->hw, rate, &parent_rate);
731 * clk_round_rate - round the given rate for a clk
732 * @clk: the clk for which we are rounding a rate
733 * @rate: the rate which is to be rounded
735 * Takes in a rate as input and rounds it to a rate that the clk can actually
736 * use which is then returned. If clk doesn't support round_rate operation
737 * then the parent rate is returned.
739 long clk_round_rate(struct clk *clk, unsigned long rate)
741 unsigned long ret;
743 mutex_lock(&prepare_lock);
744 ret = __clk_round_rate(clk, rate);
745 mutex_unlock(&prepare_lock);
747 return ret;
749 EXPORT_SYMBOL_GPL(clk_round_rate);
752 * __clk_notify - call clk notifier chain
753 * @clk: struct clk * that is changing rate
754 * @msg: clk notifier type (see include/linux/clk.h)
755 * @old_rate: old clk rate
756 * @new_rate: new clk rate
758 * Triggers a notifier call chain on the clk rate-change notification
759 * for 'clk'. Passes a pointer to the struct clk and the previous
760 * and current rates to the notifier callback. Intended to be called by
761 * internal clock code only. Returns NOTIFY_DONE from the last driver
762 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
763 * a driver returns that.
765 static int __clk_notify(struct clk *clk, unsigned long msg,
766 unsigned long old_rate, unsigned long new_rate)
768 struct clk_notifier *cn;
769 struct clk_notifier_data cnd;
770 int ret = NOTIFY_DONE;
772 cnd.clk = clk;
773 cnd.old_rate = old_rate;
774 cnd.new_rate = new_rate;
776 list_for_each_entry(cn, &clk_notifier_list, node) {
777 if (cn->clk == clk) {
778 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
779 &cnd);
780 break;
784 return ret;
788 * __clk_recalc_rates
789 * @clk: first clk in the subtree
790 * @msg: notification type (see include/linux/clk.h)
792 * Walks the subtree of clks starting with clk and recalculates rates as it
793 * goes. Note that if a clk does not implement the .recalc_rate callback then
794 * it is assumed that the clock will take on the rate of it's parent.
796 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
797 * if necessary.
799 * Caller must hold prepare_lock.
801 static void __clk_recalc_rates(struct clk *clk, unsigned long msg)
803 unsigned long old_rate;
804 unsigned long parent_rate = 0;
805 struct clk *child;
807 old_rate = clk->rate;
809 if (clk->parent)
810 parent_rate = clk->parent->rate;
812 if (clk->ops->recalc_rate)
813 clk->rate = clk->ops->recalc_rate(clk->hw, parent_rate);
814 else
815 clk->rate = parent_rate;
818 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
819 * & ABORT_RATE_CHANGE notifiers
821 if (clk->notifier_count && msg)
822 __clk_notify(clk, msg, old_rate, clk->rate);
824 hlist_for_each_entry(child, &clk->children, child_node)
825 __clk_recalc_rates(child, msg);
829 * clk_get_rate - return the rate of clk
830 * @clk: the clk whose rate is being returned
832 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
833 * is set, which means a recalc_rate will be issued.
834 * If clk is NULL then returns 0.
836 unsigned long clk_get_rate(struct clk *clk)
838 unsigned long rate;
840 mutex_lock(&prepare_lock);
842 if (clk && (clk->flags & CLK_GET_RATE_NOCACHE))
843 __clk_recalc_rates(clk, 0);
845 rate = __clk_get_rate(clk);
846 mutex_unlock(&prepare_lock);
848 return rate;
850 EXPORT_SYMBOL_GPL(clk_get_rate);
853 * __clk_speculate_rates
854 * @clk: first clk in the subtree
855 * @parent_rate: the "future" rate of clk's parent
857 * Walks the subtree of clks starting with clk, speculating rates as it
858 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
860 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
861 * pre-rate change notifications and returns early if no clks in the
862 * subtree have subscribed to the notifications. Note that if a clk does not
863 * implement the .recalc_rate callback then it is assumed that the clock will
864 * take on the rate of it's parent.
866 * Caller must hold prepare_lock.
868 static int __clk_speculate_rates(struct clk *clk, unsigned long parent_rate)
870 struct clk *child;
871 unsigned long new_rate;
872 int ret = NOTIFY_DONE;
874 if (clk->ops->recalc_rate)
875 new_rate = clk->ops->recalc_rate(clk->hw, parent_rate);
876 else
877 new_rate = parent_rate;
879 /* abort the rate change if a driver returns NOTIFY_BAD */
880 if (clk->notifier_count)
881 ret = __clk_notify(clk, PRE_RATE_CHANGE, clk->rate, new_rate);
883 if (ret == NOTIFY_BAD)
884 goto out;
886 hlist_for_each_entry(child, &clk->children, child_node) {
887 ret = __clk_speculate_rates(child, new_rate);
888 if (ret == NOTIFY_BAD)
889 break;
892 out:
893 return ret;
896 static void clk_calc_subtree(struct clk *clk, unsigned long new_rate)
898 struct clk *child;
900 clk->new_rate = new_rate;
902 hlist_for_each_entry(child, &clk->children, child_node) {
903 if (child->ops->recalc_rate)
904 child->new_rate = child->ops->recalc_rate(child->hw, new_rate);
905 else
906 child->new_rate = new_rate;
907 clk_calc_subtree(child, child->new_rate);
912 * calculate the new rates returning the topmost clock that has to be
913 * changed.
915 static struct clk *clk_calc_new_rates(struct clk *clk, unsigned long rate)
917 struct clk *top = clk;
918 unsigned long best_parent_rate = 0;
919 unsigned long new_rate;
921 /* sanity */
922 if (IS_ERR_OR_NULL(clk))
923 return NULL;
925 /* save parent rate, if it exists */
926 if (clk->parent)
927 best_parent_rate = clk->parent->rate;
929 /* never propagate up to the parent */
930 if (!(clk->flags & CLK_SET_RATE_PARENT)) {
931 if (!clk->ops->round_rate) {
932 clk->new_rate = clk->rate;
933 return NULL;
935 new_rate = clk->ops->round_rate(clk->hw, rate, &best_parent_rate);
936 goto out;
939 /* need clk->parent from here on out */
940 if (!clk->parent) {
941 pr_debug("%s: %s has NULL parent\n", __func__, clk->name);
942 return NULL;
945 if (!clk->ops->round_rate) {
946 top = clk_calc_new_rates(clk->parent, rate);
947 new_rate = clk->parent->new_rate;
949 goto out;
952 new_rate = clk->ops->round_rate(clk->hw, rate, &best_parent_rate);
954 if (best_parent_rate != clk->parent->rate) {
955 top = clk_calc_new_rates(clk->parent, best_parent_rate);
957 goto out;
960 out:
961 clk_calc_subtree(clk, new_rate);
963 return top;
967 * Notify about rate changes in a subtree. Always walk down the whole tree
968 * so that in case of an error we can walk down the whole tree again and
969 * abort the change.
971 static struct clk *clk_propagate_rate_change(struct clk *clk, unsigned long event)
973 struct clk *child, *fail_clk = NULL;
974 int ret = NOTIFY_DONE;
976 if (clk->rate == clk->new_rate)
977 return 0;
979 if (clk->notifier_count) {
980 ret = __clk_notify(clk, event, clk->rate, clk->new_rate);
981 if (ret == NOTIFY_BAD)
982 fail_clk = clk;
985 hlist_for_each_entry(child, &clk->children, child_node) {
986 clk = clk_propagate_rate_change(child, event);
987 if (clk)
988 fail_clk = clk;
991 return fail_clk;
995 * walk down a subtree and set the new rates notifying the rate
996 * change on the way
998 static void clk_change_rate(struct clk *clk)
1000 struct clk *child;
1001 unsigned long old_rate;
1002 unsigned long best_parent_rate = 0;
1004 old_rate = clk->rate;
1006 if (clk->parent)
1007 best_parent_rate = clk->parent->rate;
1009 if (clk->ops->set_rate)
1010 clk->ops->set_rate(clk->hw, clk->new_rate, best_parent_rate);
1012 if (clk->ops->recalc_rate)
1013 clk->rate = clk->ops->recalc_rate(clk->hw, best_parent_rate);
1014 else
1015 clk->rate = best_parent_rate;
1017 if (clk->notifier_count && old_rate != clk->rate)
1018 __clk_notify(clk, POST_RATE_CHANGE, old_rate, clk->rate);
1020 hlist_for_each_entry(child, &clk->children, child_node)
1021 clk_change_rate(child);
1025 * clk_set_rate - specify a new rate for clk
1026 * @clk: the clk whose rate is being changed
1027 * @rate: the new rate for clk
1029 * In the simplest case clk_set_rate will only adjust the rate of clk.
1031 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1032 * propagate up to clk's parent; whether or not this happens depends on the
1033 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1034 * after calling .round_rate then upstream parent propagation is ignored. If
1035 * *parent_rate comes back with a new rate for clk's parent then we propagate
1036 * up to clk's parent and set it's rate. Upward propagation will continue
1037 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1038 * .round_rate stops requesting changes to clk's parent_rate.
1040 * Rate changes are accomplished via tree traversal that also recalculates the
1041 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
1043 * Returns 0 on success, -EERROR otherwise.
1045 int clk_set_rate(struct clk *clk, unsigned long rate)
1047 struct clk *top, *fail_clk;
1048 int ret = 0;
1050 /* prevent racing with updates to the clock topology */
1051 mutex_lock(&prepare_lock);
1053 /* bail early if nothing to do */
1054 if (rate == clk->rate)
1055 goto out;
1057 if ((clk->flags & CLK_SET_RATE_GATE) && clk->prepare_count) {
1058 ret = -EBUSY;
1059 goto out;
1062 /* calculate new rates and get the topmost changed clock */
1063 top = clk_calc_new_rates(clk, rate);
1064 if (!top) {
1065 ret = -EINVAL;
1066 goto out;
1069 /* notify that we are about to change rates */
1070 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1071 if (fail_clk) {
1072 pr_warn("%s: failed to set %s rate\n", __func__,
1073 fail_clk->name);
1074 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1075 ret = -EBUSY;
1076 goto out;
1079 /* change the rates */
1080 clk_change_rate(top);
1082 out:
1083 mutex_unlock(&prepare_lock);
1085 return ret;
1087 EXPORT_SYMBOL_GPL(clk_set_rate);
1090 * clk_get_parent - return the parent of a clk
1091 * @clk: the clk whose parent gets returned
1093 * Simply returns clk->parent. Returns NULL if clk is NULL.
1095 struct clk *clk_get_parent(struct clk *clk)
1097 struct clk *parent;
1099 mutex_lock(&prepare_lock);
1100 parent = __clk_get_parent(clk);
1101 mutex_unlock(&prepare_lock);
1103 return parent;
1105 EXPORT_SYMBOL_GPL(clk_get_parent);
1108 * .get_parent is mandatory for clocks with multiple possible parents. It is
1109 * optional for single-parent clocks. Always call .get_parent if it is
1110 * available and WARN if it is missing for multi-parent clocks.
1112 * For single-parent clocks without .get_parent, first check to see if the
1113 * .parents array exists, and if so use it to avoid an expensive tree
1114 * traversal. If .parents does not exist then walk the tree with __clk_lookup.
1116 static struct clk *__clk_init_parent(struct clk *clk)
1118 struct clk *ret = NULL;
1119 u8 index;
1121 /* handle the trivial cases */
1123 if (!clk->num_parents)
1124 goto out;
1126 if (clk->num_parents == 1) {
1127 if (IS_ERR_OR_NULL(clk->parent))
1128 ret = clk->parent = __clk_lookup(clk->parent_names[0]);
1129 ret = clk->parent;
1130 goto out;
1133 if (!clk->ops->get_parent) {
1134 WARN(!clk->ops->get_parent,
1135 "%s: multi-parent clocks must implement .get_parent\n",
1136 __func__);
1137 goto out;
1141 * Do our best to cache parent clocks in clk->parents. This prevents
1142 * unnecessary and expensive calls to __clk_lookup. We don't set
1143 * clk->parent here; that is done by the calling function
1146 index = clk->ops->get_parent(clk->hw);
1148 if (!clk->parents)
1149 clk->parents =
1150 kzalloc((sizeof(struct clk*) * clk->num_parents),
1151 GFP_KERNEL);
1153 if (!clk->parents)
1154 ret = __clk_lookup(clk->parent_names[index]);
1155 else if (!clk->parents[index])
1156 ret = clk->parents[index] =
1157 __clk_lookup(clk->parent_names[index]);
1158 else
1159 ret = clk->parents[index];
1161 out:
1162 return ret;
1165 void __clk_reparent(struct clk *clk, struct clk *new_parent)
1167 #ifdef CONFIG_COMMON_CLK_DEBUG
1168 struct dentry *d;
1169 struct dentry *new_parent_d;
1170 #endif
1172 if (!clk || !new_parent)
1173 return;
1175 hlist_del(&clk->child_node);
1177 if (new_parent)
1178 hlist_add_head(&clk->child_node, &new_parent->children);
1179 else
1180 hlist_add_head(&clk->child_node, &clk_orphan_list);
1182 #ifdef CONFIG_COMMON_CLK_DEBUG
1183 if (!inited)
1184 goto out;
1186 if (new_parent)
1187 new_parent_d = new_parent->dentry;
1188 else
1189 new_parent_d = orphandir;
1191 d = debugfs_rename(clk->dentry->d_parent, clk->dentry,
1192 new_parent_d, clk->name);
1193 if (d)
1194 clk->dentry = d;
1195 else
1196 pr_debug("%s: failed to rename debugfs entry for %s\n",
1197 __func__, clk->name);
1198 out:
1199 #endif
1201 clk->parent = new_parent;
1203 __clk_recalc_rates(clk, POST_RATE_CHANGE);
1206 static int __clk_set_parent(struct clk *clk, struct clk *parent)
1208 struct clk *old_parent;
1209 unsigned long flags;
1210 int ret = -EINVAL;
1211 u8 i;
1213 old_parent = clk->parent;
1215 if (!clk->parents)
1216 clk->parents = kzalloc((sizeof(struct clk*) * clk->num_parents),
1217 GFP_KERNEL);
1220 * find index of new parent clock using cached parent ptrs,
1221 * or if not yet cached, use string name comparison and cache
1222 * them now to avoid future calls to __clk_lookup.
1224 for (i = 0; i < clk->num_parents; i++) {
1225 if (clk->parents && clk->parents[i] == parent)
1226 break;
1227 else if (!strcmp(clk->parent_names[i], parent->name)) {
1228 if (clk->parents)
1229 clk->parents[i] = __clk_lookup(parent->name);
1230 break;
1234 if (i == clk->num_parents) {
1235 pr_debug("%s: clock %s is not a possible parent of clock %s\n",
1236 __func__, parent->name, clk->name);
1237 goto out;
1240 /* migrate prepare and enable */
1241 if (clk->prepare_count)
1242 __clk_prepare(parent);
1244 /* FIXME replace with clk_is_enabled(clk) someday */
1245 spin_lock_irqsave(&enable_lock, flags);
1246 if (clk->enable_count)
1247 __clk_enable(parent);
1248 spin_unlock_irqrestore(&enable_lock, flags);
1250 /* change clock input source */
1251 ret = clk->ops->set_parent(clk->hw, i);
1253 /* clean up old prepare and enable */
1254 spin_lock_irqsave(&enable_lock, flags);
1255 if (clk->enable_count)
1256 __clk_disable(old_parent);
1257 spin_unlock_irqrestore(&enable_lock, flags);
1259 if (clk->prepare_count)
1260 __clk_unprepare(old_parent);
1262 out:
1263 return ret;
1267 * clk_set_parent - switch the parent of a mux clk
1268 * @clk: the mux clk whose input we are switching
1269 * @parent: the new input to clk
1271 * Re-parent clk to use parent as it's new input source. If clk has the
1272 * CLK_SET_PARENT_GATE flag set then clk must be gated for this
1273 * operation to succeed. After successfully changing clk's parent
1274 * clk_set_parent will update the clk topology, sysfs topology and
1275 * propagate rate recalculation via __clk_recalc_rates. Returns 0 on
1276 * success, -EERROR otherwise.
1278 int clk_set_parent(struct clk *clk, struct clk *parent)
1280 int ret = 0;
1282 if (!clk || !clk->ops)
1283 return -EINVAL;
1285 if (!clk->ops->set_parent)
1286 return -ENOSYS;
1288 /* prevent racing with updates to the clock topology */
1289 mutex_lock(&prepare_lock);
1291 if (clk->parent == parent)
1292 goto out;
1294 /* propagate PRE_RATE_CHANGE notifications */
1295 if (clk->notifier_count)
1296 ret = __clk_speculate_rates(clk, parent->rate);
1298 /* abort if a driver objects */
1299 if (ret == NOTIFY_STOP)
1300 goto out;
1302 /* only re-parent if the clock is not in use */
1303 if ((clk->flags & CLK_SET_PARENT_GATE) && clk->prepare_count)
1304 ret = -EBUSY;
1305 else
1306 ret = __clk_set_parent(clk, parent);
1308 /* propagate ABORT_RATE_CHANGE if .set_parent failed */
1309 if (ret) {
1310 __clk_recalc_rates(clk, ABORT_RATE_CHANGE);
1311 goto out;
1314 /* propagate rate recalculation downstream */
1315 __clk_reparent(clk, parent);
1317 out:
1318 mutex_unlock(&prepare_lock);
1320 return ret;
1322 EXPORT_SYMBOL_GPL(clk_set_parent);
1325 * __clk_init - initialize the data structures in a struct clk
1326 * @dev: device initializing this clk, placeholder for now
1327 * @clk: clk being initialized
1329 * Initializes the lists in struct clk, queries the hardware for the
1330 * parent and rate and sets them both.
1332 int __clk_init(struct device *dev, struct clk *clk)
1334 int i, ret = 0;
1335 struct clk *orphan;
1336 struct hlist_node *tmp2;
1338 if (!clk)
1339 return -EINVAL;
1341 mutex_lock(&prepare_lock);
1343 /* check to see if a clock with this name is already registered */
1344 if (__clk_lookup(clk->name)) {
1345 pr_debug("%s: clk %s already initialized\n",
1346 __func__, clk->name);
1347 ret = -EEXIST;
1348 goto out;
1351 /* check that clk_ops are sane. See Documentation/clk.txt */
1352 if (clk->ops->set_rate &&
1353 !(clk->ops->round_rate && clk->ops->recalc_rate)) {
1354 pr_warning("%s: %s must implement .round_rate & .recalc_rate\n",
1355 __func__, clk->name);
1356 ret = -EINVAL;
1357 goto out;
1360 if (clk->ops->set_parent && !clk->ops->get_parent) {
1361 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
1362 __func__, clk->name);
1363 ret = -EINVAL;
1364 goto out;
1367 /* throw a WARN if any entries in parent_names are NULL */
1368 for (i = 0; i < clk->num_parents; i++)
1369 WARN(!clk->parent_names[i],
1370 "%s: invalid NULL in %s's .parent_names\n",
1371 __func__, clk->name);
1374 * Allocate an array of struct clk *'s to avoid unnecessary string
1375 * look-ups of clk's possible parents. This can fail for clocks passed
1376 * in to clk_init during early boot; thus any access to clk->parents[]
1377 * must always check for a NULL pointer and try to populate it if
1378 * necessary.
1380 * If clk->parents is not NULL we skip this entire block. This allows
1381 * for clock drivers to statically initialize clk->parents.
1383 if (clk->num_parents > 1 && !clk->parents) {
1384 clk->parents = kzalloc((sizeof(struct clk*) * clk->num_parents),
1385 GFP_KERNEL);
1387 * __clk_lookup returns NULL for parents that have not been
1388 * clk_init'd; thus any access to clk->parents[] must check
1389 * for a NULL pointer. We can always perform lazy lookups for
1390 * missing parents later on.
1392 if (clk->parents)
1393 for (i = 0; i < clk->num_parents; i++)
1394 clk->parents[i] =
1395 __clk_lookup(clk->parent_names[i]);
1398 clk->parent = __clk_init_parent(clk);
1401 * Populate clk->parent if parent has already been __clk_init'd. If
1402 * parent has not yet been __clk_init'd then place clk in the orphan
1403 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
1404 * clk list.
1406 * Every time a new clk is clk_init'd then we walk the list of orphan
1407 * clocks and re-parent any that are children of the clock currently
1408 * being clk_init'd.
1410 if (clk->parent)
1411 hlist_add_head(&clk->child_node,
1412 &clk->parent->children);
1413 else if (clk->flags & CLK_IS_ROOT)
1414 hlist_add_head(&clk->child_node, &clk_root_list);
1415 else
1416 hlist_add_head(&clk->child_node, &clk_orphan_list);
1419 * Set clk's rate. The preferred method is to use .recalc_rate. For
1420 * simple clocks and lazy developers the default fallback is to use the
1421 * parent's rate. If a clock doesn't have a parent (or is orphaned)
1422 * then rate is set to zero.
1424 if (clk->ops->recalc_rate)
1425 clk->rate = clk->ops->recalc_rate(clk->hw,
1426 __clk_get_rate(clk->parent));
1427 else if (clk->parent)
1428 clk->rate = clk->parent->rate;
1429 else
1430 clk->rate = 0;
1433 * walk the list of orphan clocks and reparent any that are children of
1434 * this clock
1436 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
1437 if (orphan->ops->get_parent) {
1438 i = orphan->ops->get_parent(orphan->hw);
1439 if (!strcmp(clk->name, orphan->parent_names[i]))
1440 __clk_reparent(orphan, clk);
1441 continue;
1444 for (i = 0; i < orphan->num_parents; i++)
1445 if (!strcmp(clk->name, orphan->parent_names[i])) {
1446 __clk_reparent(orphan, clk);
1447 break;
1452 * optional platform-specific magic
1454 * The .init callback is not used by any of the basic clock types, but
1455 * exists for weird hardware that must perform initialization magic.
1456 * Please consider other ways of solving initialization problems before
1457 * using this callback, as it's use is discouraged.
1459 if (clk->ops->init)
1460 clk->ops->init(clk->hw);
1462 clk_debug_register(clk);
1464 out:
1465 mutex_unlock(&prepare_lock);
1467 return ret;
1471 * __clk_register - register a clock and return a cookie.
1473 * Same as clk_register, except that the .clk field inside hw shall point to a
1474 * preallocated (generally statically allocated) struct clk. None of the fields
1475 * of the struct clk need to be initialized.
1477 * The data pointed to by .init and .clk field shall NOT be marked as init
1478 * data.
1480 * __clk_register is only exposed via clk-private.h and is intended for use with
1481 * very large numbers of clocks that need to be statically initialized. It is
1482 * a layering violation to include clk-private.h from any code which implements
1483 * a clock's .ops; as such any statically initialized clock data MUST be in a
1484 * separate C file from the logic that implements it's operations. Returns 0
1485 * on success, otherwise an error code.
1487 struct clk *__clk_register(struct device *dev, struct clk_hw *hw)
1489 int ret;
1490 struct clk *clk;
1492 clk = hw->clk;
1493 clk->name = hw->init->name;
1494 clk->ops = hw->init->ops;
1495 clk->hw = hw;
1496 clk->flags = hw->init->flags;
1497 clk->parent_names = hw->init->parent_names;
1498 clk->num_parents = hw->init->num_parents;
1500 ret = __clk_init(dev, clk);
1501 if (ret)
1502 return ERR_PTR(ret);
1504 return clk;
1506 EXPORT_SYMBOL_GPL(__clk_register);
1508 static int _clk_register(struct device *dev, struct clk_hw *hw, struct clk *clk)
1510 int i, ret;
1512 clk->name = kstrdup(hw->init->name, GFP_KERNEL);
1513 if (!clk->name) {
1514 pr_err("%s: could not allocate clk->name\n", __func__);
1515 ret = -ENOMEM;
1516 goto fail_name;
1518 clk->ops = hw->init->ops;
1519 clk->hw = hw;
1520 clk->flags = hw->init->flags;
1521 clk->num_parents = hw->init->num_parents;
1522 hw->clk = clk;
1524 /* allocate local copy in case parent_names is __initdata */
1525 clk->parent_names = kzalloc((sizeof(char*) * clk->num_parents),
1526 GFP_KERNEL);
1528 if (!clk->parent_names) {
1529 pr_err("%s: could not allocate clk->parent_names\n", __func__);
1530 ret = -ENOMEM;
1531 goto fail_parent_names;
1535 /* copy each string name in case parent_names is __initdata */
1536 for (i = 0; i < clk->num_parents; i++) {
1537 clk->parent_names[i] = kstrdup(hw->init->parent_names[i],
1538 GFP_KERNEL);
1539 if (!clk->parent_names[i]) {
1540 pr_err("%s: could not copy parent_names\n", __func__);
1541 ret = -ENOMEM;
1542 goto fail_parent_names_copy;
1546 ret = __clk_init(dev, clk);
1547 if (!ret)
1548 return 0;
1550 fail_parent_names_copy:
1551 while (--i >= 0)
1552 kfree(clk->parent_names[i]);
1553 kfree(clk->parent_names);
1554 fail_parent_names:
1555 kfree(clk->name);
1556 fail_name:
1557 return ret;
1561 * clk_register - allocate a new clock, register it and return an opaque cookie
1562 * @dev: device that is registering this clock
1563 * @hw: link to hardware-specific clock data
1565 * clk_register is the primary interface for populating the clock tree with new
1566 * clock nodes. It returns a pointer to the newly allocated struct clk which
1567 * cannot be dereferenced by driver code but may be used in conjuction with the
1568 * rest of the clock API. In the event of an error clk_register will return an
1569 * error code; drivers must test for an error code after calling clk_register.
1571 struct clk *clk_register(struct device *dev, struct clk_hw *hw)
1573 int ret;
1574 struct clk *clk;
1576 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
1577 if (!clk) {
1578 pr_err("%s: could not allocate clk\n", __func__);
1579 ret = -ENOMEM;
1580 goto fail_out;
1583 ret = _clk_register(dev, hw, clk);
1584 if (!ret)
1585 return clk;
1587 kfree(clk);
1588 fail_out:
1589 return ERR_PTR(ret);
1591 EXPORT_SYMBOL_GPL(clk_register);
1594 * clk_unregister - unregister a currently registered clock
1595 * @clk: clock to unregister
1597 * Currently unimplemented.
1599 void clk_unregister(struct clk *clk) {}
1600 EXPORT_SYMBOL_GPL(clk_unregister);
1602 static void devm_clk_release(struct device *dev, void *res)
1604 clk_unregister(res);
1608 * devm_clk_register - resource managed clk_register()
1609 * @dev: device that is registering this clock
1610 * @hw: link to hardware-specific clock data
1612 * Managed clk_register(). Clocks returned from this function are
1613 * automatically clk_unregister()ed on driver detach. See clk_register() for
1614 * more information.
1616 struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
1618 struct clk *clk;
1619 int ret;
1621 clk = devres_alloc(devm_clk_release, sizeof(*clk), GFP_KERNEL);
1622 if (!clk)
1623 return ERR_PTR(-ENOMEM);
1625 ret = _clk_register(dev, hw, clk);
1626 if (!ret) {
1627 devres_add(dev, clk);
1628 } else {
1629 devres_free(clk);
1630 clk = ERR_PTR(ret);
1633 return clk;
1635 EXPORT_SYMBOL_GPL(devm_clk_register);
1637 static int devm_clk_match(struct device *dev, void *res, void *data)
1639 struct clk *c = res;
1640 if (WARN_ON(!c))
1641 return 0;
1642 return c == data;
1646 * devm_clk_unregister - resource managed clk_unregister()
1647 * @clk: clock to unregister
1649 * Deallocate a clock allocated with devm_clk_register(). Normally
1650 * this function will not need to be called and the resource management
1651 * code will ensure that the resource is freed.
1653 void devm_clk_unregister(struct device *dev, struct clk *clk)
1655 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
1657 EXPORT_SYMBOL_GPL(devm_clk_unregister);
1659 /*** clk rate change notifiers ***/
1662 * clk_notifier_register - add a clk rate change notifier
1663 * @clk: struct clk * to watch
1664 * @nb: struct notifier_block * with callback info
1666 * Request notification when clk's rate changes. This uses an SRCU
1667 * notifier because we want it to block and notifier unregistrations are
1668 * uncommon. The callbacks associated with the notifier must not
1669 * re-enter into the clk framework by calling any top-level clk APIs;
1670 * this will cause a nested prepare_lock mutex.
1672 * Pre-change notifier callbacks will be passed the current, pre-change
1673 * rate of the clk via struct clk_notifier_data.old_rate. The new,
1674 * post-change rate of the clk is passed via struct
1675 * clk_notifier_data.new_rate.
1677 * Post-change notifiers will pass the now-current, post-change rate of
1678 * the clk in both struct clk_notifier_data.old_rate and struct
1679 * clk_notifier_data.new_rate.
1681 * Abort-change notifiers are effectively the opposite of pre-change
1682 * notifiers: the original pre-change clk rate is passed in via struct
1683 * clk_notifier_data.new_rate and the failed post-change rate is passed
1684 * in via struct clk_notifier_data.old_rate.
1686 * clk_notifier_register() must be called from non-atomic context.
1687 * Returns -EINVAL if called with null arguments, -ENOMEM upon
1688 * allocation failure; otherwise, passes along the return value of
1689 * srcu_notifier_chain_register().
1691 int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
1693 struct clk_notifier *cn;
1694 int ret = -ENOMEM;
1696 if (!clk || !nb)
1697 return -EINVAL;
1699 mutex_lock(&prepare_lock);
1701 /* search the list of notifiers for this clk */
1702 list_for_each_entry(cn, &clk_notifier_list, node)
1703 if (cn->clk == clk)
1704 break;
1706 /* if clk wasn't in the notifier list, allocate new clk_notifier */
1707 if (cn->clk != clk) {
1708 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
1709 if (!cn)
1710 goto out;
1712 cn->clk = clk;
1713 srcu_init_notifier_head(&cn->notifier_head);
1715 list_add(&cn->node, &clk_notifier_list);
1718 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
1720 clk->notifier_count++;
1722 out:
1723 mutex_unlock(&prepare_lock);
1725 return ret;
1727 EXPORT_SYMBOL_GPL(clk_notifier_register);
1730 * clk_notifier_unregister - remove a clk rate change notifier
1731 * @clk: struct clk *
1732 * @nb: struct notifier_block * with callback info
1734 * Request no further notification for changes to 'clk' and frees memory
1735 * allocated in clk_notifier_register.
1737 * Returns -EINVAL if called with null arguments; otherwise, passes
1738 * along the return value of srcu_notifier_chain_unregister().
1740 int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
1742 struct clk_notifier *cn = NULL;
1743 int ret = -EINVAL;
1745 if (!clk || !nb)
1746 return -EINVAL;
1748 mutex_lock(&prepare_lock);
1750 list_for_each_entry(cn, &clk_notifier_list, node)
1751 if (cn->clk == clk)
1752 break;
1754 if (cn->clk == clk) {
1755 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
1757 clk->notifier_count--;
1759 /* XXX the notifier code should handle this better */
1760 if (!cn->notifier_head.head) {
1761 srcu_cleanup_notifier_head(&cn->notifier_head);
1762 kfree(cn);
1765 } else {
1766 ret = -ENOENT;
1769 mutex_unlock(&prepare_lock);
1771 return ret;
1773 EXPORT_SYMBOL_GPL(clk_notifier_unregister);
1775 #ifdef CONFIG_OF
1777 * struct of_clk_provider - Clock provider registration structure
1778 * @link: Entry in global list of clock providers
1779 * @node: Pointer to device tree node of clock provider
1780 * @get: Get clock callback. Returns NULL or a struct clk for the
1781 * given clock specifier
1782 * @data: context pointer to be passed into @get callback
1784 struct of_clk_provider {
1785 struct list_head link;
1787 struct device_node *node;
1788 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
1789 void *data;
1792 extern struct of_device_id __clk_of_table[];
1794 static const struct of_device_id __clk_of_table_sentinel
1795 __used __section(__clk_of_table_end);
1797 static LIST_HEAD(of_clk_providers);
1798 static DEFINE_MUTEX(of_clk_lock);
1800 struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
1801 void *data)
1803 return data;
1805 EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
1807 struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
1809 struct clk_onecell_data *clk_data = data;
1810 unsigned int idx = clkspec->args[0];
1812 if (idx >= clk_data->clk_num) {
1813 pr_err("%s: invalid clock index %d\n", __func__, idx);
1814 return ERR_PTR(-EINVAL);
1817 return clk_data->clks[idx];
1819 EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
1822 * of_clk_add_provider() - Register a clock provider for a node
1823 * @np: Device node pointer associated with clock provider
1824 * @clk_src_get: callback for decoding clock
1825 * @data: context pointer for @clk_src_get callback.
1827 int of_clk_add_provider(struct device_node *np,
1828 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
1829 void *data),
1830 void *data)
1832 struct of_clk_provider *cp;
1834 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
1835 if (!cp)
1836 return -ENOMEM;
1838 cp->node = of_node_get(np);
1839 cp->data = data;
1840 cp->get = clk_src_get;
1842 mutex_lock(&of_clk_lock);
1843 list_add(&cp->link, &of_clk_providers);
1844 mutex_unlock(&of_clk_lock);
1845 pr_debug("Added clock from %s\n", np->full_name);
1847 return 0;
1849 EXPORT_SYMBOL_GPL(of_clk_add_provider);
1852 * of_clk_del_provider() - Remove a previously registered clock provider
1853 * @np: Device node pointer associated with clock provider
1855 void of_clk_del_provider(struct device_node *np)
1857 struct of_clk_provider *cp;
1859 mutex_lock(&of_clk_lock);
1860 list_for_each_entry(cp, &of_clk_providers, link) {
1861 if (cp->node == np) {
1862 list_del(&cp->link);
1863 of_node_put(cp->node);
1864 kfree(cp);
1865 break;
1868 mutex_unlock(&of_clk_lock);
1870 EXPORT_SYMBOL_GPL(of_clk_del_provider);
1872 struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
1874 struct of_clk_provider *provider;
1875 struct clk *clk = ERR_PTR(-ENOENT);
1877 /* Check if we have such a provider in our array */
1878 mutex_lock(&of_clk_lock);
1879 list_for_each_entry(provider, &of_clk_providers, link) {
1880 if (provider->node == clkspec->np)
1881 clk = provider->get(clkspec, provider->data);
1882 if (!IS_ERR(clk))
1883 break;
1885 mutex_unlock(&of_clk_lock);
1887 return clk;
1890 const char *of_clk_get_parent_name(struct device_node *np, int index)
1892 struct of_phandle_args clkspec;
1893 const char *clk_name;
1894 int rc;
1896 if (index < 0)
1897 return NULL;
1899 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
1900 &clkspec);
1901 if (rc)
1902 return NULL;
1904 if (of_property_read_string_index(clkspec.np, "clock-output-names",
1905 clkspec.args_count ? clkspec.args[0] : 0,
1906 &clk_name) < 0)
1907 clk_name = clkspec.np->name;
1909 of_node_put(clkspec.np);
1910 return clk_name;
1912 EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
1915 * of_clk_init() - Scan and init clock providers from the DT
1916 * @matches: array of compatible values and init functions for providers.
1918 * This function scans the device tree for matching clock providers and
1919 * calls their initialization functions
1921 void __init of_clk_init(const struct of_device_id *matches)
1923 struct device_node *np;
1925 if (!matches)
1926 matches = __clk_of_table;
1928 for_each_matching_node(np, matches) {
1929 const struct of_device_id *match = of_match_node(matches, np);
1930 of_clk_init_cb_t clk_init_cb = match->data;
1931 clk_init_cb(np);
1934 #endif