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.h>
13 #include <linux/clk-provider.h>
14 #include <linux/clk/clk-conf.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/spinlock.h>
18 #include <linux/err.h>
19 #include <linux/list.h>
20 #include <linux/slab.h>
22 #include <linux/device.h>
23 #include <linux/init.h>
24 #include <linux/sched.h>
25 #include <linux/clkdev.h>
29 static DEFINE_SPINLOCK(enable_lock);
30 static DEFINE_MUTEX(prepare_lock);
32 static struct task_struct *prepare_owner;
33 static struct task_struct *enable_owner;
35 static int prepare_refcnt;
36 static int enable_refcnt;
38 static HLIST_HEAD(clk_root_list);
39 static HLIST_HEAD(clk_orphan_list);
40 static LIST_HEAD(clk_notifier_list);
42 /*** private data structures ***/
46 const struct clk_ops *ops;
49 struct clk_core *parent;
50 const char **parent_names;
51 struct clk_core **parents;
55 unsigned long req_rate;
56 unsigned long new_rate;
57 struct clk_core *new_parent;
58 struct clk_core *new_child;
61 unsigned int enable_count;
62 unsigned int prepare_count;
63 unsigned long min_rate;
64 unsigned long max_rate;
65 unsigned long accuracy;
67 struct hlist_head children;
68 struct hlist_node child_node;
69 struct hlist_head clks;
70 unsigned int notifier_count;
71 #ifdef CONFIG_DEBUG_FS
72 struct dentry *dentry;
73 struct hlist_node debug_node;
78 #define CREATE_TRACE_POINTS
79 #include <trace/events/clk.h>
82 struct clk_core *core;
85 unsigned long min_rate;
86 unsigned long max_rate;
87 struct hlist_node clks_node;
91 static void clk_prepare_lock(void)
93 if (!mutex_trylock(&prepare_lock)) {
94 if (prepare_owner == current) {
98 mutex_lock(&prepare_lock);
100 WARN_ON_ONCE(prepare_owner != NULL);
101 WARN_ON_ONCE(prepare_refcnt != 0);
102 prepare_owner = current;
106 static void clk_prepare_unlock(void)
108 WARN_ON_ONCE(prepare_owner != current);
109 WARN_ON_ONCE(prepare_refcnt == 0);
111 if (--prepare_refcnt)
113 prepare_owner = NULL;
114 mutex_unlock(&prepare_lock);
117 static unsigned long clk_enable_lock(void)
118 __acquires(enable_lock)
122 if (!spin_trylock_irqsave(&enable_lock, flags)) {
123 if (enable_owner == current) {
125 __acquire(enable_lock);
128 spin_lock_irqsave(&enable_lock, flags);
130 WARN_ON_ONCE(enable_owner != NULL);
131 WARN_ON_ONCE(enable_refcnt != 0);
132 enable_owner = current;
137 static void clk_enable_unlock(unsigned long flags)
138 __releases(enable_lock)
140 WARN_ON_ONCE(enable_owner != current);
141 WARN_ON_ONCE(enable_refcnt == 0);
143 if (--enable_refcnt) {
144 __release(enable_lock);
148 spin_unlock_irqrestore(&enable_lock, flags);
151 static bool clk_core_is_prepared(struct clk_core *core)
154 * .is_prepared is optional for clocks that can prepare
155 * fall back to software usage counter if it is missing
157 if (!core->ops->is_prepared)
158 return core->prepare_count;
160 return core->ops->is_prepared(core->hw);
163 static bool clk_core_is_enabled(struct clk_core *core)
166 * .is_enabled is only mandatory for clocks that gate
167 * fall back to software usage counter if .is_enabled is missing
169 if (!core->ops->is_enabled)
170 return core->enable_count;
172 return core->ops->is_enabled(core->hw);
175 /*** helper functions ***/
177 const char *__clk_get_name(const struct clk *clk)
179 return !clk ? NULL : clk->core->name;
181 EXPORT_SYMBOL_GPL(__clk_get_name);
183 const char *clk_hw_get_name(const struct clk_hw *hw)
185 return hw->core->name;
187 EXPORT_SYMBOL_GPL(clk_hw_get_name);
189 struct clk_hw *__clk_get_hw(struct clk *clk)
191 return !clk ? NULL : clk->core->hw;
193 EXPORT_SYMBOL_GPL(__clk_get_hw);
195 unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
197 return hw->core->num_parents;
199 EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
201 struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
203 return hw->core->parent ? hw->core->parent->hw : NULL;
205 EXPORT_SYMBOL_GPL(clk_hw_get_parent);
207 static struct clk_core *__clk_lookup_subtree(const char *name,
208 struct clk_core *core)
210 struct clk_core *child;
211 struct clk_core *ret;
213 if (!strcmp(core->name, name))
216 hlist_for_each_entry(child, &core->children, child_node) {
217 ret = __clk_lookup_subtree(name, child);
225 static struct clk_core *clk_core_lookup(const char *name)
227 struct clk_core *root_clk;
228 struct clk_core *ret;
233 /* search the 'proper' clk tree first */
234 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
235 ret = __clk_lookup_subtree(name, root_clk);
240 /* if not found, then search the orphan tree */
241 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
242 ret = __clk_lookup_subtree(name, root_clk);
250 static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
253 if (!core || index >= core->num_parents)
256 if (!core->parents[index])
257 core->parents[index] =
258 clk_core_lookup(core->parent_names[index]);
260 return core->parents[index];
264 clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
266 struct clk_core *parent;
268 parent = clk_core_get_parent_by_index(hw->core, index);
270 return !parent ? NULL : parent->hw;
272 EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
274 unsigned int __clk_get_enable_count(struct clk *clk)
276 return !clk ? 0 : clk->core->enable_count;
279 static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
290 if (!core->num_parents)
300 unsigned long clk_hw_get_rate(const struct clk_hw *hw)
302 return clk_core_get_rate_nolock(hw->core);
304 EXPORT_SYMBOL_GPL(clk_hw_get_rate);
306 static unsigned long __clk_get_accuracy(struct clk_core *core)
311 return core->accuracy;
314 unsigned long __clk_get_flags(struct clk *clk)
316 return !clk ? 0 : clk->core->flags;
318 EXPORT_SYMBOL_GPL(__clk_get_flags);
320 unsigned long clk_hw_get_flags(const struct clk_hw *hw)
322 return hw->core->flags;
324 EXPORT_SYMBOL_GPL(clk_hw_get_flags);
326 bool clk_hw_is_prepared(const struct clk_hw *hw)
328 return clk_core_is_prepared(hw->core);
331 bool clk_hw_is_enabled(const struct clk_hw *hw)
333 return clk_core_is_enabled(hw->core);
336 bool __clk_is_enabled(struct clk *clk)
341 return clk_core_is_enabled(clk->core);
343 EXPORT_SYMBOL_GPL(__clk_is_enabled);
345 static bool mux_is_better_rate(unsigned long rate, unsigned long now,
346 unsigned long best, unsigned long flags)
348 if (flags & CLK_MUX_ROUND_CLOSEST)
349 return abs(now - rate) < abs(best - rate);
351 return now <= rate && now > best;
355 clk_mux_determine_rate_flags(struct clk_hw *hw, struct clk_rate_request *req,
358 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
359 int i, num_parents, ret;
360 unsigned long best = 0;
361 struct clk_rate_request parent_req = *req;
363 /* if NO_REPARENT flag set, pass through to current parent */
364 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
365 parent = core->parent;
366 if (core->flags & CLK_SET_RATE_PARENT) {
367 ret = __clk_determine_rate(parent ? parent->hw : NULL,
372 best = parent_req.rate;
374 best = clk_core_get_rate_nolock(parent);
376 best = clk_core_get_rate_nolock(core);
382 /* find the parent that can provide the fastest rate <= rate */
383 num_parents = core->num_parents;
384 for (i = 0; i < num_parents; i++) {
385 parent = clk_core_get_parent_by_index(core, i);
389 if (core->flags & CLK_SET_RATE_PARENT) {
391 ret = __clk_determine_rate(parent->hw, &parent_req);
395 parent_req.rate = clk_core_get_rate_nolock(parent);
398 if (mux_is_better_rate(req->rate, parent_req.rate,
400 best_parent = parent;
401 best = parent_req.rate;
410 req->best_parent_hw = best_parent->hw;
411 req->best_parent_rate = best;
417 struct clk *__clk_lookup(const char *name)
419 struct clk_core *core = clk_core_lookup(name);
421 return !core ? NULL : core->hw->clk;
424 static void clk_core_get_boundaries(struct clk_core *core,
425 unsigned long *min_rate,
426 unsigned long *max_rate)
428 struct clk *clk_user;
430 *min_rate = core->min_rate;
431 *max_rate = core->max_rate;
433 hlist_for_each_entry(clk_user, &core->clks, clks_node)
434 *min_rate = max(*min_rate, clk_user->min_rate);
436 hlist_for_each_entry(clk_user, &core->clks, clks_node)
437 *max_rate = min(*max_rate, clk_user->max_rate);
440 void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
441 unsigned long max_rate)
443 hw->core->min_rate = min_rate;
444 hw->core->max_rate = max_rate;
446 EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
449 * Helper for finding best parent to provide a given frequency. This can be used
450 * directly as a determine_rate callback (e.g. for a mux), or from a more
451 * complex clock that may combine a mux with other operations.
453 int __clk_mux_determine_rate(struct clk_hw *hw,
454 struct clk_rate_request *req)
456 return clk_mux_determine_rate_flags(hw, req, 0);
458 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
460 int __clk_mux_determine_rate_closest(struct clk_hw *hw,
461 struct clk_rate_request *req)
463 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
465 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
469 static void clk_core_unprepare(struct clk_core *core)
471 lockdep_assert_held(&prepare_lock);
476 if (WARN_ON(core->prepare_count == 0))
479 if (WARN_ON(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL))
482 if (--core->prepare_count > 0)
485 WARN_ON(core->enable_count > 0);
487 trace_clk_unprepare(core);
489 if (core->ops->unprepare)
490 core->ops->unprepare(core->hw);
492 trace_clk_unprepare_complete(core);
493 clk_core_unprepare(core->parent);
496 static void clk_core_unprepare_lock(struct clk_core *core)
499 clk_core_unprepare(core);
500 clk_prepare_unlock();
504 * clk_unprepare - undo preparation of a clock source
505 * @clk: the clk being unprepared
507 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
508 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
509 * if the operation may sleep. One example is a clk which is accessed over
510 * I2c. In the complex case a clk gate operation may require a fast and a slow
511 * part. It is this reason that clk_unprepare and clk_disable are not mutually
512 * exclusive. In fact clk_disable must be called before clk_unprepare.
514 void clk_unprepare(struct clk *clk)
516 if (IS_ERR_OR_NULL(clk))
519 clk_core_unprepare_lock(clk->core);
521 EXPORT_SYMBOL_GPL(clk_unprepare);
523 static int clk_core_prepare(struct clk_core *core)
527 lockdep_assert_held(&prepare_lock);
532 if (core->prepare_count == 0) {
533 ret = clk_core_prepare(core->parent);
537 trace_clk_prepare(core);
539 if (core->ops->prepare)
540 ret = core->ops->prepare(core->hw);
542 trace_clk_prepare_complete(core);
545 clk_core_unprepare(core->parent);
550 core->prepare_count++;
555 static int clk_core_prepare_lock(struct clk_core *core)
560 ret = clk_core_prepare(core);
561 clk_prepare_unlock();
567 * clk_prepare - prepare a clock source
568 * @clk: the clk being prepared
570 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
571 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
572 * operation may sleep. One example is a clk which is accessed over I2c. In
573 * the complex case a clk ungate operation may require a fast and a slow part.
574 * It is this reason that clk_prepare and clk_enable are not mutually
575 * exclusive. In fact clk_prepare must be called before clk_enable.
576 * Returns 0 on success, -EERROR otherwise.
578 int clk_prepare(struct clk *clk)
583 return clk_core_prepare_lock(clk->core);
585 EXPORT_SYMBOL_GPL(clk_prepare);
587 static void clk_core_disable(struct clk_core *core)
589 lockdep_assert_held(&enable_lock);
594 if (WARN_ON(core->enable_count == 0))
597 if (WARN_ON(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL))
600 if (--core->enable_count > 0)
603 trace_clk_disable_rcuidle(core);
605 if (core->ops->disable)
606 core->ops->disable(core->hw);
608 trace_clk_disable_complete_rcuidle(core);
610 clk_core_disable(core->parent);
613 static void clk_core_disable_lock(struct clk_core *core)
617 flags = clk_enable_lock();
618 clk_core_disable(core);
619 clk_enable_unlock(flags);
623 * clk_disable - gate a clock
624 * @clk: the clk being gated
626 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
627 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
628 * clk if the operation is fast and will never sleep. One example is a
629 * SoC-internal clk which is controlled via simple register writes. In the
630 * complex case a clk gate operation may require a fast and a slow part. It is
631 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
632 * In fact clk_disable must be called before clk_unprepare.
634 void clk_disable(struct clk *clk)
636 if (IS_ERR_OR_NULL(clk))
639 clk_core_disable_lock(clk->core);
641 EXPORT_SYMBOL_GPL(clk_disable);
643 static int clk_core_enable(struct clk_core *core)
647 lockdep_assert_held(&enable_lock);
652 if (WARN_ON(core->prepare_count == 0))
655 if (core->enable_count == 0) {
656 ret = clk_core_enable(core->parent);
661 trace_clk_enable_rcuidle(core);
663 if (core->ops->enable)
664 ret = core->ops->enable(core->hw);
666 trace_clk_enable_complete_rcuidle(core);
669 clk_core_disable(core->parent);
674 core->enable_count++;
678 static int clk_core_enable_lock(struct clk_core *core)
683 flags = clk_enable_lock();
684 ret = clk_core_enable(core);
685 clk_enable_unlock(flags);
691 * clk_enable - ungate a clock
692 * @clk: the clk being ungated
694 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
695 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
696 * if the operation will never sleep. One example is a SoC-internal clk which
697 * is controlled via simple register writes. In the complex case a clk ungate
698 * operation may require a fast and a slow part. It is this reason that
699 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
700 * must be called before clk_enable. Returns 0 on success, -EERROR
703 int clk_enable(struct clk *clk)
708 return clk_core_enable_lock(clk->core);
710 EXPORT_SYMBOL_GPL(clk_enable);
712 static int clk_core_prepare_enable(struct clk_core *core)
716 ret = clk_core_prepare_lock(core);
720 ret = clk_core_enable_lock(core);
722 clk_core_unprepare_lock(core);
727 static void clk_core_disable_unprepare(struct clk_core *core)
729 clk_core_disable_lock(core);
730 clk_core_unprepare_lock(core);
733 static void clk_unprepare_unused_subtree(struct clk_core *core)
735 struct clk_core *child;
737 lockdep_assert_held(&prepare_lock);
739 hlist_for_each_entry(child, &core->children, child_node)
740 clk_unprepare_unused_subtree(child);
742 if (core->prepare_count)
745 if (core->flags & CLK_IGNORE_UNUSED)
748 if (clk_core_is_prepared(core)) {
749 trace_clk_unprepare(core);
750 if (core->ops->unprepare_unused)
751 core->ops->unprepare_unused(core->hw);
752 else if (core->ops->unprepare)
753 core->ops->unprepare(core->hw);
754 trace_clk_unprepare_complete(core);
758 static void clk_disable_unused_subtree(struct clk_core *core)
760 struct clk_core *child;
763 lockdep_assert_held(&prepare_lock);
765 hlist_for_each_entry(child, &core->children, child_node)
766 clk_disable_unused_subtree(child);
768 flags = clk_enable_lock();
770 if (core->enable_count)
773 if (core->flags & CLK_IGNORE_UNUSED)
777 * some gate clocks have special needs during the disable-unused
778 * sequence. call .disable_unused if available, otherwise fall
781 if (clk_core_is_enabled(core)) {
782 trace_clk_disable(core);
783 if (core->ops->disable_unused)
784 core->ops->disable_unused(core->hw);
785 else if (core->ops->disable)
786 core->ops->disable(core->hw);
787 trace_clk_disable_complete(core);
791 clk_enable_unlock(flags);
794 static bool clk_ignore_unused;
795 static int __init clk_ignore_unused_setup(char *__unused)
797 clk_ignore_unused = true;
800 __setup("clk_ignore_unused", clk_ignore_unused_setup);
802 static int clk_disable_unused(void)
804 struct clk_core *core;
806 if (clk_ignore_unused) {
807 pr_warn("clk: Not disabling unused clocks\n");
813 hlist_for_each_entry(core, &clk_root_list, child_node)
814 clk_disable_unused_subtree(core);
816 hlist_for_each_entry(core, &clk_orphan_list, child_node)
817 clk_disable_unused_subtree(core);
819 hlist_for_each_entry(core, &clk_root_list, child_node)
820 clk_unprepare_unused_subtree(core);
822 hlist_for_each_entry(core, &clk_orphan_list, child_node)
823 clk_unprepare_unused_subtree(core);
825 clk_prepare_unlock();
829 late_initcall_sync(clk_disable_unused);
831 static int clk_core_round_rate_nolock(struct clk_core *core,
832 struct clk_rate_request *req)
834 struct clk_core *parent;
837 lockdep_assert_held(&prepare_lock);
842 parent = core->parent;
844 req->best_parent_hw = parent->hw;
845 req->best_parent_rate = parent->rate;
847 req->best_parent_hw = NULL;
848 req->best_parent_rate = 0;
851 if (core->ops->determine_rate) {
852 return core->ops->determine_rate(core->hw, req);
853 } else if (core->ops->round_rate) {
854 rate = core->ops->round_rate(core->hw, req->rate,
855 &req->best_parent_rate);
860 } else if (core->flags & CLK_SET_RATE_PARENT) {
861 return clk_core_round_rate_nolock(parent, req);
863 req->rate = core->rate;
870 * __clk_determine_rate - get the closest rate actually supported by a clock
871 * @hw: determine the rate of this clock
872 * @req: target rate request
874 * Useful for clk_ops such as .set_rate and .determine_rate.
876 int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
883 return clk_core_round_rate_nolock(hw->core, req);
885 EXPORT_SYMBOL_GPL(__clk_determine_rate);
887 unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
890 struct clk_rate_request req;
892 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
895 ret = clk_core_round_rate_nolock(hw->core, &req);
901 EXPORT_SYMBOL_GPL(clk_hw_round_rate);
904 * clk_round_rate - round the given rate for a clk
905 * @clk: the clk for which we are rounding a rate
906 * @rate: the rate which is to be rounded
908 * Takes in a rate as input and rounds it to a rate that the clk can actually
909 * use which is then returned. If clk doesn't support round_rate operation
910 * then the parent rate is returned.
912 long clk_round_rate(struct clk *clk, unsigned long rate)
914 struct clk_rate_request req;
922 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
925 ret = clk_core_round_rate_nolock(clk->core, &req);
926 clk_prepare_unlock();
933 EXPORT_SYMBOL_GPL(clk_round_rate);
936 * __clk_notify - call clk notifier chain
937 * @core: clk that is changing rate
938 * @msg: clk notifier type (see include/linux/clk.h)
939 * @old_rate: old clk rate
940 * @new_rate: new clk rate
942 * Triggers a notifier call chain on the clk rate-change notification
943 * for 'clk'. Passes a pointer to the struct clk and the previous
944 * and current rates to the notifier callback. Intended to be called by
945 * internal clock code only. Returns NOTIFY_DONE from the last driver
946 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
947 * a driver returns that.
949 static int __clk_notify(struct clk_core *core, unsigned long msg,
950 unsigned long old_rate, unsigned long new_rate)
952 struct clk_notifier *cn;
953 struct clk_notifier_data cnd;
954 int ret = NOTIFY_DONE;
956 cnd.old_rate = old_rate;
957 cnd.new_rate = new_rate;
959 list_for_each_entry(cn, &clk_notifier_list, node) {
960 if (cn->clk->core == core) {
962 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
971 * __clk_recalc_accuracies
972 * @core: first clk in the subtree
974 * Walks the subtree of clks starting with clk and recalculates accuracies as
975 * it goes. Note that if a clk does not implement the .recalc_accuracy
976 * callback then it is assumed that the clock will take on the accuracy of its
979 static void __clk_recalc_accuracies(struct clk_core *core)
981 unsigned long parent_accuracy = 0;
982 struct clk_core *child;
984 lockdep_assert_held(&prepare_lock);
987 parent_accuracy = core->parent->accuracy;
989 if (core->ops->recalc_accuracy)
990 core->accuracy = core->ops->recalc_accuracy(core->hw,
993 core->accuracy = parent_accuracy;
995 hlist_for_each_entry(child, &core->children, child_node)
996 __clk_recalc_accuracies(child);
999 static long clk_core_get_accuracy(struct clk_core *core)
1001 unsigned long accuracy;
1004 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1005 __clk_recalc_accuracies(core);
1007 accuracy = __clk_get_accuracy(core);
1008 clk_prepare_unlock();
1014 * clk_get_accuracy - return the accuracy of clk
1015 * @clk: the clk whose accuracy is being returned
1017 * Simply returns the cached accuracy of the clk, unless
1018 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1020 * If clk is NULL then returns 0.
1022 long clk_get_accuracy(struct clk *clk)
1027 return clk_core_get_accuracy(clk->core);
1029 EXPORT_SYMBOL_GPL(clk_get_accuracy);
1031 static unsigned long clk_recalc(struct clk_core *core,
1032 unsigned long parent_rate)
1034 if (core->ops->recalc_rate)
1035 return core->ops->recalc_rate(core->hw, parent_rate);
1040 * __clk_recalc_rates
1041 * @core: first clk in the subtree
1042 * @msg: notification type (see include/linux/clk.h)
1044 * Walks the subtree of clks starting with clk and recalculates rates as it
1045 * goes. Note that if a clk does not implement the .recalc_rate callback then
1046 * it is assumed that the clock will take on the rate of its parent.
1048 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1051 static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
1053 unsigned long old_rate;
1054 unsigned long parent_rate = 0;
1055 struct clk_core *child;
1057 lockdep_assert_held(&prepare_lock);
1059 old_rate = core->rate;
1062 parent_rate = core->parent->rate;
1064 core->rate = clk_recalc(core, parent_rate);
1067 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1068 * & ABORT_RATE_CHANGE notifiers
1070 if (core->notifier_count && msg)
1071 __clk_notify(core, msg, old_rate, core->rate);
1073 hlist_for_each_entry(child, &core->children, child_node)
1074 __clk_recalc_rates(child, msg);
1077 static unsigned long clk_core_get_rate(struct clk_core *core)
1083 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1084 __clk_recalc_rates(core, 0);
1086 rate = clk_core_get_rate_nolock(core);
1087 clk_prepare_unlock();
1093 * clk_get_rate - return the rate of clk
1094 * @clk: the clk whose rate is being returned
1096 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1097 * is set, which means a recalc_rate will be issued.
1098 * If clk is NULL then returns 0.
1100 unsigned long clk_get_rate(struct clk *clk)
1105 return clk_core_get_rate(clk->core);
1107 EXPORT_SYMBOL_GPL(clk_get_rate);
1109 static int clk_fetch_parent_index(struct clk_core *core,
1110 struct clk_core *parent)
1117 for (i = 0; i < core->num_parents; i++)
1118 if (clk_core_get_parent_by_index(core, i) == parent)
1125 * Update the orphan status of @core and all its children.
1127 static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1129 struct clk_core *child;
1131 core->orphan = is_orphan;
1133 hlist_for_each_entry(child, &core->children, child_node)
1134 clk_core_update_orphan_status(child, is_orphan);
1137 static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
1139 bool was_orphan = core->orphan;
1141 hlist_del(&core->child_node);
1144 bool becomes_orphan = new_parent->orphan;
1146 /* avoid duplicate POST_RATE_CHANGE notifications */
1147 if (new_parent->new_child == core)
1148 new_parent->new_child = NULL;
1150 hlist_add_head(&core->child_node, &new_parent->children);
1152 if (was_orphan != becomes_orphan)
1153 clk_core_update_orphan_status(core, becomes_orphan);
1155 hlist_add_head(&core->child_node, &clk_orphan_list);
1157 clk_core_update_orphan_status(core, true);
1160 core->parent = new_parent;
1163 static struct clk_core *__clk_set_parent_before(struct clk_core *core,
1164 struct clk_core *parent)
1166 unsigned long flags;
1167 struct clk_core *old_parent = core->parent;
1170 * Migrate prepare state between parents and prevent race with
1173 * If the clock is not prepared, then a race with
1174 * clk_enable/disable() is impossible since we already have the
1175 * prepare lock (future calls to clk_enable() need to be preceded by
1178 * If the clock is prepared, migrate the prepared state to the new
1179 * parent and also protect against a race with clk_enable() by
1180 * forcing the clock and the new parent on. This ensures that all
1181 * future calls to clk_enable() are practically NOPs with respect to
1182 * hardware and software states.
1184 * See also: Comment for clk_set_parent() below.
1186 if (core->prepare_count) {
1187 clk_core_prepare(parent);
1188 flags = clk_enable_lock();
1189 clk_core_enable(parent);
1190 clk_core_enable(core);
1191 clk_enable_unlock(flags);
1194 /* update the clk tree topology */
1195 flags = clk_enable_lock();
1196 clk_reparent(core, parent);
1197 clk_enable_unlock(flags);
1202 static void __clk_set_parent_after(struct clk_core *core,
1203 struct clk_core *parent,
1204 struct clk_core *old_parent)
1206 unsigned long flags;
1209 * Finish the migration of prepare state and undo the changes done
1210 * for preventing a race with clk_enable().
1212 if (core->prepare_count) {
1213 flags = clk_enable_lock();
1214 clk_core_disable(core);
1215 clk_core_disable(old_parent);
1216 clk_enable_unlock(flags);
1217 clk_core_unprepare(old_parent);
1221 static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
1224 unsigned long flags;
1226 struct clk_core *old_parent;
1228 old_parent = __clk_set_parent_before(core, parent);
1230 trace_clk_set_parent(core, parent);
1232 /* change clock input source */
1233 if (parent && core->ops->set_parent)
1234 ret = core->ops->set_parent(core->hw, p_index);
1236 trace_clk_set_parent_complete(core, parent);
1239 flags = clk_enable_lock();
1240 clk_reparent(core, old_parent);
1241 clk_enable_unlock(flags);
1242 __clk_set_parent_after(core, old_parent, parent);
1247 __clk_set_parent_after(core, parent, old_parent);
1253 * __clk_speculate_rates
1254 * @core: first clk in the subtree
1255 * @parent_rate: the "future" rate of clk's parent
1257 * Walks the subtree of clks starting with clk, speculating rates as it
1258 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1260 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1261 * pre-rate change notifications and returns early if no clks in the
1262 * subtree have subscribed to the notifications. Note that if a clk does not
1263 * implement the .recalc_rate callback then it is assumed that the clock will
1264 * take on the rate of its parent.
1266 static int __clk_speculate_rates(struct clk_core *core,
1267 unsigned long parent_rate)
1269 struct clk_core *child;
1270 unsigned long new_rate;
1271 int ret = NOTIFY_DONE;
1273 lockdep_assert_held(&prepare_lock);
1275 new_rate = clk_recalc(core, parent_rate);
1277 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
1278 if (core->notifier_count)
1279 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
1281 if (ret & NOTIFY_STOP_MASK) {
1282 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
1283 __func__, core->name, ret);
1287 hlist_for_each_entry(child, &core->children, child_node) {
1288 ret = __clk_speculate_rates(child, new_rate);
1289 if (ret & NOTIFY_STOP_MASK)
1297 static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
1298 struct clk_core *new_parent, u8 p_index)
1300 struct clk_core *child;
1302 core->new_rate = new_rate;
1303 core->new_parent = new_parent;
1304 core->new_parent_index = p_index;
1305 /* include clk in new parent's PRE_RATE_CHANGE notifications */
1306 core->new_child = NULL;
1307 if (new_parent && new_parent != core->parent)
1308 new_parent->new_child = core;
1310 hlist_for_each_entry(child, &core->children, child_node) {
1311 child->new_rate = clk_recalc(child, new_rate);
1312 clk_calc_subtree(child, child->new_rate, NULL, 0);
1317 * calculate the new rates returning the topmost clock that has to be
1320 static struct clk_core *clk_calc_new_rates(struct clk_core *core,
1323 struct clk_core *top = core;
1324 struct clk_core *old_parent, *parent;
1325 unsigned long best_parent_rate = 0;
1326 unsigned long new_rate;
1327 unsigned long min_rate;
1328 unsigned long max_rate;
1333 if (IS_ERR_OR_NULL(core))
1336 /* save parent rate, if it exists */
1337 parent = old_parent = core->parent;
1339 best_parent_rate = parent->rate;
1341 clk_core_get_boundaries(core, &min_rate, &max_rate);
1343 /* find the closest rate and parent clk/rate */
1344 if (core->ops->determine_rate) {
1345 struct clk_rate_request req;
1348 req.min_rate = min_rate;
1349 req.max_rate = max_rate;
1351 req.best_parent_hw = parent->hw;
1352 req.best_parent_rate = parent->rate;
1354 req.best_parent_hw = NULL;
1355 req.best_parent_rate = 0;
1358 ret = core->ops->determine_rate(core->hw, &req);
1362 best_parent_rate = req.best_parent_rate;
1363 new_rate = req.rate;
1364 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
1365 } else if (core->ops->round_rate) {
1366 ret = core->ops->round_rate(core->hw, rate,
1372 if (new_rate < min_rate || new_rate > max_rate)
1374 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
1375 /* pass-through clock without adjustable parent */
1376 core->new_rate = core->rate;
1379 /* pass-through clock with adjustable parent */
1380 top = clk_calc_new_rates(parent, rate);
1381 new_rate = parent->new_rate;
1385 /* some clocks must be gated to change parent */
1386 if (parent != old_parent &&
1387 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
1388 pr_debug("%s: %s not gated but wants to reparent\n",
1389 __func__, core->name);
1393 /* try finding the new parent index */
1394 if (parent && core->num_parents > 1) {
1395 p_index = clk_fetch_parent_index(core, parent);
1397 pr_debug("%s: clk %s can not be parent of clk %s\n",
1398 __func__, parent->name, core->name);
1403 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
1404 best_parent_rate != parent->rate)
1405 top = clk_calc_new_rates(parent, best_parent_rate);
1408 clk_calc_subtree(core, new_rate, parent, p_index);
1414 * Notify about rate changes in a subtree. Always walk down the whole tree
1415 * so that in case of an error we can walk down the whole tree again and
1418 static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
1419 unsigned long event)
1421 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
1422 int ret = NOTIFY_DONE;
1424 if (core->rate == core->new_rate)
1427 if (core->notifier_count) {
1428 ret = __clk_notify(core, event, core->rate, core->new_rate);
1429 if (ret & NOTIFY_STOP_MASK)
1433 hlist_for_each_entry(child, &core->children, child_node) {
1434 /* Skip children who will be reparented to another clock */
1435 if (child->new_parent && child->new_parent != core)
1437 tmp_clk = clk_propagate_rate_change(child, event);
1442 /* handle the new child who might not be in core->children yet */
1443 if (core->new_child) {
1444 tmp_clk = clk_propagate_rate_change(core->new_child, event);
1453 * walk down a subtree and set the new rates notifying the rate
1456 static void clk_change_rate(struct clk_core *core)
1458 struct clk_core *child;
1459 struct hlist_node *tmp;
1460 unsigned long old_rate;
1461 unsigned long best_parent_rate = 0;
1462 bool skip_set_rate = false;
1463 struct clk_core *old_parent;
1465 old_rate = core->rate;
1467 if (core->new_parent)
1468 best_parent_rate = core->new_parent->rate;
1469 else if (core->parent)
1470 best_parent_rate = core->parent->rate;
1472 if (core->flags & CLK_SET_RATE_UNGATE) {
1473 unsigned long flags;
1475 clk_core_prepare(core);
1476 flags = clk_enable_lock();
1477 clk_core_enable(core);
1478 clk_enable_unlock(flags);
1481 if (core->new_parent && core->new_parent != core->parent) {
1482 old_parent = __clk_set_parent_before(core, core->new_parent);
1483 trace_clk_set_parent(core, core->new_parent);
1485 if (core->ops->set_rate_and_parent) {
1486 skip_set_rate = true;
1487 core->ops->set_rate_and_parent(core->hw, core->new_rate,
1489 core->new_parent_index);
1490 } else if (core->ops->set_parent) {
1491 core->ops->set_parent(core->hw, core->new_parent_index);
1494 trace_clk_set_parent_complete(core, core->new_parent);
1495 __clk_set_parent_after(core, core->new_parent, old_parent);
1498 trace_clk_set_rate(core, core->new_rate);
1500 if (!skip_set_rate && core->ops->set_rate)
1501 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
1503 trace_clk_set_rate_complete(core, core->new_rate);
1505 core->rate = clk_recalc(core, best_parent_rate);
1507 if (core->flags & CLK_SET_RATE_UNGATE) {
1508 unsigned long flags;
1510 flags = clk_enable_lock();
1511 clk_core_disable(core);
1512 clk_enable_unlock(flags);
1513 clk_core_unprepare(core);
1516 if (core->notifier_count && old_rate != core->rate)
1517 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
1519 if (core->flags & CLK_RECALC_NEW_RATES)
1520 (void)clk_calc_new_rates(core, core->new_rate);
1523 * Use safe iteration, as change_rate can actually swap parents
1524 * for certain clock types.
1526 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
1527 /* Skip children who will be reparented to another clock */
1528 if (child->new_parent && child->new_parent != core)
1530 clk_change_rate(child);
1533 /* handle the new child who might not be in core->children yet */
1534 if (core->new_child)
1535 clk_change_rate(core->new_child);
1538 static int clk_core_set_rate_nolock(struct clk_core *core,
1539 unsigned long req_rate)
1541 struct clk_core *top, *fail_clk;
1542 unsigned long rate = req_rate;
1547 /* bail early if nothing to do */
1548 if (rate == clk_core_get_rate_nolock(core))
1551 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
1554 /* calculate new rates and get the topmost changed clock */
1555 top = clk_calc_new_rates(core, rate);
1559 /* notify that we are about to change rates */
1560 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1562 pr_debug("%s: failed to set %s rate\n", __func__,
1564 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1568 /* change the rates */
1569 clk_change_rate(top);
1571 core->req_rate = req_rate;
1577 * clk_set_rate - specify a new rate for clk
1578 * @clk: the clk whose rate is being changed
1579 * @rate: the new rate for clk
1581 * In the simplest case clk_set_rate will only adjust the rate of clk.
1583 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1584 * propagate up to clk's parent; whether or not this happens depends on the
1585 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1586 * after calling .round_rate then upstream parent propagation is ignored. If
1587 * *parent_rate comes back with a new rate for clk's parent then we propagate
1588 * up to clk's parent and set its rate. Upward propagation will continue
1589 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1590 * .round_rate stops requesting changes to clk's parent_rate.
1592 * Rate changes are accomplished via tree traversal that also recalculates the
1593 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
1595 * Returns 0 on success, -EERROR otherwise.
1597 int clk_set_rate(struct clk *clk, unsigned long rate)
1604 /* prevent racing with updates to the clock topology */
1607 ret = clk_core_set_rate_nolock(clk->core, rate);
1609 clk_prepare_unlock();
1613 EXPORT_SYMBOL_GPL(clk_set_rate);
1616 * clk_set_rate_range - set a rate range for a clock source
1617 * @clk: clock source
1618 * @min: desired minimum clock rate in Hz, inclusive
1619 * @max: desired maximum clock rate in Hz, inclusive
1621 * Returns success (0) or negative errno.
1623 int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
1631 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
1632 __func__, clk->core->name, clk->dev_id, clk->con_id,
1639 if (min != clk->min_rate || max != clk->max_rate) {
1640 clk->min_rate = min;
1641 clk->max_rate = max;
1642 ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
1645 clk_prepare_unlock();
1649 EXPORT_SYMBOL_GPL(clk_set_rate_range);
1652 * clk_set_min_rate - set a minimum clock rate for a clock source
1653 * @clk: clock source
1654 * @rate: desired minimum clock rate in Hz, inclusive
1656 * Returns success (0) or negative errno.
1658 int clk_set_min_rate(struct clk *clk, unsigned long rate)
1663 return clk_set_rate_range(clk, rate, clk->max_rate);
1665 EXPORT_SYMBOL_GPL(clk_set_min_rate);
1668 * clk_set_max_rate - set a maximum clock rate for a clock source
1669 * @clk: clock source
1670 * @rate: desired maximum clock rate in Hz, inclusive
1672 * Returns success (0) or negative errno.
1674 int clk_set_max_rate(struct clk *clk, unsigned long rate)
1679 return clk_set_rate_range(clk, clk->min_rate, rate);
1681 EXPORT_SYMBOL_GPL(clk_set_max_rate);
1684 * clk_get_parent - return the parent of a clk
1685 * @clk: the clk whose parent gets returned
1687 * Simply returns clk->parent. Returns NULL if clk is NULL.
1689 struct clk *clk_get_parent(struct clk *clk)
1697 /* TODO: Create a per-user clk and change callers to call clk_put */
1698 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
1699 clk_prepare_unlock();
1703 EXPORT_SYMBOL_GPL(clk_get_parent);
1705 static struct clk_core *__clk_init_parent(struct clk_core *core)
1709 if (core->num_parents > 1 && core->ops->get_parent)
1710 index = core->ops->get_parent(core->hw);
1712 return clk_core_get_parent_by_index(core, index);
1715 static void clk_core_reparent(struct clk_core *core,
1716 struct clk_core *new_parent)
1718 clk_reparent(core, new_parent);
1719 __clk_recalc_accuracies(core);
1720 __clk_recalc_rates(core, POST_RATE_CHANGE);
1723 void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
1728 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
1732 * clk_has_parent - check if a clock is a possible parent for another
1733 * @clk: clock source
1734 * @parent: parent clock source
1736 * This function can be used in drivers that need to check that a clock can be
1737 * the parent of another without actually changing the parent.
1739 * Returns true if @parent is a possible parent for @clk, false otherwise.
1741 bool clk_has_parent(struct clk *clk, struct clk *parent)
1743 struct clk_core *core, *parent_core;
1746 /* NULL clocks should be nops, so return success if either is NULL. */
1747 if (!clk || !parent)
1751 parent_core = parent->core;
1753 /* Optimize for the case where the parent is already the parent. */
1754 if (core->parent == parent_core)
1757 for (i = 0; i < core->num_parents; i++)
1758 if (strcmp(core->parent_names[i], parent_core->name) == 0)
1763 EXPORT_SYMBOL_GPL(clk_has_parent);
1765 static int clk_core_set_parent(struct clk_core *core, struct clk_core *parent)
1769 unsigned long p_rate = 0;
1774 /* prevent racing with updates to the clock topology */
1777 if (core->parent == parent)
1780 /* verify ops for for multi-parent clks */
1781 if ((core->num_parents > 1) && (!core->ops->set_parent)) {
1786 /* check that we are allowed to re-parent if the clock is in use */
1787 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
1792 /* try finding the new parent index */
1794 p_index = clk_fetch_parent_index(core, parent);
1796 pr_debug("%s: clk %s can not be parent of clk %s\n",
1797 __func__, parent->name, core->name);
1801 p_rate = parent->rate;
1804 /* propagate PRE_RATE_CHANGE notifications */
1805 ret = __clk_speculate_rates(core, p_rate);
1807 /* abort if a driver objects */
1808 if (ret & NOTIFY_STOP_MASK)
1811 /* do the re-parent */
1812 ret = __clk_set_parent(core, parent, p_index);
1814 /* propagate rate an accuracy recalculation accordingly */
1816 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
1818 __clk_recalc_rates(core, POST_RATE_CHANGE);
1819 __clk_recalc_accuracies(core);
1823 clk_prepare_unlock();
1829 * clk_set_parent - switch the parent of a mux clk
1830 * @clk: the mux clk whose input we are switching
1831 * @parent: the new input to clk
1833 * Re-parent clk to use parent as its new input source. If clk is in
1834 * prepared state, the clk will get enabled for the duration of this call. If
1835 * that's not acceptable for a specific clk (Eg: the consumer can't handle
1836 * that, the reparenting is glitchy in hardware, etc), use the
1837 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
1839 * After successfully changing clk's parent clk_set_parent will update the
1840 * clk topology, sysfs topology and propagate rate recalculation via
1841 * __clk_recalc_rates.
1843 * Returns 0 on success, -EERROR otherwise.
1845 int clk_set_parent(struct clk *clk, struct clk *parent)
1850 return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
1852 EXPORT_SYMBOL_GPL(clk_set_parent);
1855 * clk_set_phase - adjust the phase shift of a clock signal
1856 * @clk: clock signal source
1857 * @degrees: number of degrees the signal is shifted
1859 * Shifts the phase of a clock signal by the specified
1860 * degrees. Returns 0 on success, -EERROR otherwise.
1862 * This function makes no distinction about the input or reference
1863 * signal that we adjust the clock signal phase against. For example
1864 * phase locked-loop clock signal generators we may shift phase with
1865 * respect to feedback clock signal input, but for other cases the
1866 * clock phase may be shifted with respect to some other, unspecified
1869 * Additionally the concept of phase shift does not propagate through
1870 * the clock tree hierarchy, which sets it apart from clock rates and
1871 * clock accuracy. A parent clock phase attribute does not have an
1872 * impact on the phase attribute of a child clock.
1874 int clk_set_phase(struct clk *clk, int degrees)
1881 /* sanity check degrees */
1888 /* bail early if nothing to do */
1889 if (degrees == clk->core->phase)
1892 trace_clk_set_phase(clk->core, degrees);
1894 if (clk->core->ops->set_phase)
1895 ret = clk->core->ops->set_phase(clk->core->hw, degrees);
1897 trace_clk_set_phase_complete(clk->core, degrees);
1900 clk->core->phase = degrees;
1903 clk_prepare_unlock();
1907 EXPORT_SYMBOL_GPL(clk_set_phase);
1909 static int clk_core_get_phase(struct clk_core *core)
1915 clk_prepare_unlock();
1921 * clk_get_phase - return the phase shift of a clock signal
1922 * @clk: clock signal source
1924 * Returns the phase shift of a clock node in degrees, otherwise returns
1927 int clk_get_phase(struct clk *clk)
1932 return clk_core_get_phase(clk->core);
1934 EXPORT_SYMBOL_GPL(clk_get_phase);
1937 * clk_is_match - check if two clk's point to the same hardware clock
1938 * @p: clk compared against q
1939 * @q: clk compared against p
1941 * Returns true if the two struct clk pointers both point to the same hardware
1942 * clock node. Put differently, returns true if struct clk *p and struct clk *q
1943 * share the same struct clk_core object.
1945 * Returns false otherwise. Note that two NULL clks are treated as matching.
1947 bool clk_is_match(const struct clk *p, const struct clk *q)
1949 /* trivial case: identical struct clk's or both NULL */
1953 /* true if clk->core pointers match. Avoid dereferencing garbage */
1954 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
1955 if (p->core == q->core)
1960 EXPORT_SYMBOL_GPL(clk_is_match);
1962 /*** debugfs support ***/
1964 #ifdef CONFIG_DEBUG_FS
1965 #include <linux/debugfs.h>
1967 static struct dentry *rootdir;
1968 static int inited = 0;
1969 static DEFINE_MUTEX(clk_debug_lock);
1970 static HLIST_HEAD(clk_debug_list);
1972 static struct hlist_head *all_lists[] = {
1978 static struct hlist_head *orphan_list[] = {
1983 static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
1989 seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
1991 30 - level * 3, c->name,
1992 c->enable_count, c->prepare_count, clk_core_get_rate(c),
1993 clk_core_get_accuracy(c), clk_core_get_phase(c));
1996 static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
1999 struct clk_core *child;
2004 clk_summary_show_one(s, c, level);
2006 hlist_for_each_entry(child, &c->children, child_node)
2007 clk_summary_show_subtree(s, child, level + 1);
2010 static int clk_summary_show(struct seq_file *s, void *data)
2013 struct hlist_head **lists = (struct hlist_head **)s->private;
2015 seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
2016 seq_puts(s, "----------------------------------------------------------------------------------------\n");
2020 for (; *lists; lists++)
2021 hlist_for_each_entry(c, *lists, child_node)
2022 clk_summary_show_subtree(s, c, 0);
2024 clk_prepare_unlock();
2030 static int clk_summary_open(struct inode *inode, struct file *file)
2032 return single_open(file, clk_summary_show, inode->i_private);
2035 static const struct file_operations clk_summary_fops = {
2036 .open = clk_summary_open,
2038 .llseek = seq_lseek,
2039 .release = single_release,
2042 static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2047 /* This should be JSON format, i.e. elements separated with a comma */
2048 seq_printf(s, "\"%s\": { ", c->name);
2049 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2050 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
2051 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2052 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
2053 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
2056 static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2058 struct clk_core *child;
2063 clk_dump_one(s, c, level);
2065 hlist_for_each_entry(child, &c->children, child_node) {
2067 clk_dump_subtree(s, child, level + 1);
2073 static int clk_dump(struct seq_file *s, void *data)
2076 bool first_node = true;
2077 struct hlist_head **lists = (struct hlist_head **)s->private;
2083 for (; *lists; lists++) {
2084 hlist_for_each_entry(c, *lists, child_node) {
2088 clk_dump_subtree(s, c, 0);
2092 clk_prepare_unlock();
2099 static int clk_dump_open(struct inode *inode, struct file *file)
2101 return single_open(file, clk_dump, inode->i_private);
2104 static const struct file_operations clk_dump_fops = {
2105 .open = clk_dump_open,
2107 .llseek = seq_lseek,
2108 .release = single_release,
2111 static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2116 if (!core || !pdentry) {
2121 d = debugfs_create_dir(core->name, pdentry);
2127 d = debugfs_create_u32("clk_rate", S_IRUGO, core->dentry,
2128 (u32 *)&core->rate);
2132 d = debugfs_create_u32("clk_accuracy", S_IRUGO, core->dentry,
2133 (u32 *)&core->accuracy);
2137 d = debugfs_create_u32("clk_phase", S_IRUGO, core->dentry,
2138 (u32 *)&core->phase);
2142 d = debugfs_create_x32("clk_flags", S_IRUGO, core->dentry,
2143 (u32 *)&core->flags);
2147 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, core->dentry,
2148 (u32 *)&core->prepare_count);
2152 d = debugfs_create_u32("clk_enable_count", S_IRUGO, core->dentry,
2153 (u32 *)&core->enable_count);
2157 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, core->dentry,
2158 (u32 *)&core->notifier_count);
2162 if (core->ops->debug_init) {
2163 ret = core->ops->debug_init(core->hw, core->dentry);
2172 debugfs_remove_recursive(core->dentry);
2173 core->dentry = NULL;
2179 * clk_debug_register - add a clk node to the debugfs clk directory
2180 * @core: the clk being added to the debugfs clk directory
2182 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2183 * initialized. Otherwise it bails out early since the debugfs clk directory
2184 * will be created lazily by clk_debug_init as part of a late_initcall.
2186 static int clk_debug_register(struct clk_core *core)
2190 mutex_lock(&clk_debug_lock);
2191 hlist_add_head(&core->debug_node, &clk_debug_list);
2196 ret = clk_debug_create_one(core, rootdir);
2198 mutex_unlock(&clk_debug_lock);
2204 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2205 * @core: the clk being removed from the debugfs clk directory
2207 * Dynamically removes a clk and all its child nodes from the
2208 * debugfs clk directory if clk->dentry points to debugfs created by
2209 * clk_debug_register in __clk_core_init.
2211 static void clk_debug_unregister(struct clk_core *core)
2213 mutex_lock(&clk_debug_lock);
2214 hlist_del_init(&core->debug_node);
2215 debugfs_remove_recursive(core->dentry);
2216 core->dentry = NULL;
2217 mutex_unlock(&clk_debug_lock);
2220 struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
2221 void *data, const struct file_operations *fops)
2223 struct dentry *d = NULL;
2225 if (hw->core->dentry)
2226 d = debugfs_create_file(name, mode, hw->core->dentry, data,
2231 EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
2234 * clk_debug_init - lazily populate the debugfs clk directory
2236 * clks are often initialized very early during boot before memory can be
2237 * dynamically allocated and well before debugfs is setup. This function
2238 * populates the debugfs clk directory once at boot-time when we know that
2239 * debugfs is setup. It should only be called once at boot-time, all other clks
2240 * added dynamically will be done so with clk_debug_register.
2242 static int __init clk_debug_init(void)
2244 struct clk_core *core;
2247 rootdir = debugfs_create_dir("clk", NULL);
2252 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists,
2257 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists,
2262 d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir,
2263 &orphan_list, &clk_summary_fops);
2267 d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir,
2268 &orphan_list, &clk_dump_fops);
2272 mutex_lock(&clk_debug_lock);
2273 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2274 clk_debug_create_one(core, rootdir);
2277 mutex_unlock(&clk_debug_lock);
2281 late_initcall(clk_debug_init);
2283 static inline int clk_debug_register(struct clk_core *core) { return 0; }
2284 static inline void clk_debug_reparent(struct clk_core *core,
2285 struct clk_core *new_parent)
2288 static inline void clk_debug_unregister(struct clk_core *core)
2294 * __clk_core_init - initialize the data structures in a struct clk_core
2295 * @core: clk_core being initialized
2297 * Initializes the lists in struct clk_core, queries the hardware for the
2298 * parent and rate and sets them both.
2300 static int __clk_core_init(struct clk_core *core)
2303 struct clk_core *orphan;
2304 struct hlist_node *tmp2;
2312 /* check to see if a clock with this name is already registered */
2313 if (clk_core_lookup(core->name)) {
2314 pr_debug("%s: clk %s already initialized\n",
2315 __func__, core->name);
2320 /* check that clk_ops are sane. See Documentation/clk.txt */
2321 if (core->ops->set_rate &&
2322 !((core->ops->round_rate || core->ops->determine_rate) &&
2323 core->ops->recalc_rate)) {
2324 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
2325 __func__, core->name);
2330 if (core->ops->set_parent && !core->ops->get_parent) {
2331 pr_err("%s: %s must implement .get_parent & .set_parent\n",
2332 __func__, core->name);
2337 if (core->num_parents > 1 && !core->ops->get_parent) {
2338 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
2339 __func__, core->name);
2344 if (core->ops->set_rate_and_parent &&
2345 !(core->ops->set_parent && core->ops->set_rate)) {
2346 pr_err("%s: %s must implement .set_parent & .set_rate\n",
2347 __func__, core->name);
2352 /* throw a WARN if any entries in parent_names are NULL */
2353 for (i = 0; i < core->num_parents; i++)
2354 WARN(!core->parent_names[i],
2355 "%s: invalid NULL in %s's .parent_names\n",
2356 __func__, core->name);
2358 core->parent = __clk_init_parent(core);
2361 * Populate core->parent if parent has already been clk_core_init'd. If
2362 * parent has not yet been clk_core_init'd then place clk in the orphan
2363 * list. If clk doesn't have any parents then place it in the root
2366 * Every time a new clk is clk_init'd then we walk the list of orphan
2367 * clocks and re-parent any that are children of the clock currently
2371 hlist_add_head(&core->child_node,
2372 &core->parent->children);
2373 core->orphan = core->parent->orphan;
2374 } else if (!core->num_parents) {
2375 hlist_add_head(&core->child_node, &clk_root_list);
2376 core->orphan = false;
2378 hlist_add_head(&core->child_node, &clk_orphan_list);
2379 core->orphan = true;
2383 * Set clk's accuracy. The preferred method is to use
2384 * .recalc_accuracy. For simple clocks and lazy developers the default
2385 * fallback is to use the parent's accuracy. If a clock doesn't have a
2386 * parent (or is orphaned) then accuracy is set to zero (perfect
2389 if (core->ops->recalc_accuracy)
2390 core->accuracy = core->ops->recalc_accuracy(core->hw,
2391 __clk_get_accuracy(core->parent));
2392 else if (core->parent)
2393 core->accuracy = core->parent->accuracy;
2399 * Since a phase is by definition relative to its parent, just
2400 * query the current clock phase, or just assume it's in phase.
2402 if (core->ops->get_phase)
2403 core->phase = core->ops->get_phase(core->hw);
2408 * Set clk's rate. The preferred method is to use .recalc_rate. For
2409 * simple clocks and lazy developers the default fallback is to use the
2410 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2411 * then rate is set to zero.
2413 if (core->ops->recalc_rate)
2414 rate = core->ops->recalc_rate(core->hw,
2415 clk_core_get_rate_nolock(core->parent));
2416 else if (core->parent)
2417 rate = core->parent->rate;
2420 core->rate = core->req_rate = rate;
2423 * walk the list of orphan clocks and reparent any that newly finds a
2426 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
2427 struct clk_core *parent = __clk_init_parent(orphan);
2430 clk_core_reparent(orphan, parent);
2434 * optional platform-specific magic
2436 * The .init callback is not used by any of the basic clock types, but
2437 * exists for weird hardware that must perform initialization magic.
2438 * Please consider other ways of solving initialization problems before
2439 * using this callback, as its use is discouraged.
2441 if (core->ops->init)
2442 core->ops->init(core->hw);
2444 if (core->flags & CLK_IS_CRITICAL) {
2445 unsigned long flags;
2447 clk_core_prepare(core);
2449 flags = clk_enable_lock();
2450 clk_core_enable(core);
2451 clk_enable_unlock(flags);
2454 kref_init(&core->ref);
2456 clk_prepare_unlock();
2459 clk_debug_register(core);
2464 struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2469 /* This is to allow this function to be chained to others */
2470 if (IS_ERR_OR_NULL(hw))
2471 return (struct clk *) hw;
2473 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2475 return ERR_PTR(-ENOMEM);
2477 clk->core = hw->core;
2478 clk->dev_id = dev_id;
2479 clk->con_id = con_id;
2480 clk->max_rate = ULONG_MAX;
2483 hlist_add_head(&clk->clks_node, &hw->core->clks);
2484 clk_prepare_unlock();
2489 void __clk_free_clk(struct clk *clk)
2492 hlist_del(&clk->clks_node);
2493 clk_prepare_unlock();
2499 * clk_register - allocate a new clock, register it and return an opaque cookie
2500 * @dev: device that is registering this clock
2501 * @hw: link to hardware-specific clock data
2503 * clk_register is the primary interface for populating the clock tree with new
2504 * clock nodes. It returns a pointer to the newly allocated struct clk which
2505 * cannot be dereferenced by driver code but may be used in conjunction with the
2506 * rest of the clock API. In the event of an error clk_register will return an
2507 * error code; drivers must test for an error code after calling clk_register.
2509 struct clk *clk_register(struct device *dev, struct clk_hw *hw)
2512 struct clk_core *core;
2514 core = kzalloc(sizeof(*core), GFP_KERNEL);
2520 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
2525 core->ops = hw->init->ops;
2526 if (dev && dev->driver)
2527 core->owner = dev->driver->owner;
2529 core->flags = hw->init->flags;
2530 core->num_parents = hw->init->num_parents;
2532 core->max_rate = ULONG_MAX;
2535 /* allocate local copy in case parent_names is __initdata */
2536 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
2539 if (!core->parent_names) {
2541 goto fail_parent_names;
2545 /* copy each string name in case parent_names is __initdata */
2546 for (i = 0; i < core->num_parents; i++) {
2547 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
2549 if (!core->parent_names[i]) {
2551 goto fail_parent_names_copy;
2555 /* avoid unnecessary string look-ups of clk_core's possible parents. */
2556 core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
2558 if (!core->parents) {
2563 INIT_HLIST_HEAD(&core->clks);
2565 hw->clk = __clk_create_clk(hw, NULL, NULL);
2566 if (IS_ERR(hw->clk)) {
2567 ret = PTR_ERR(hw->clk);
2571 ret = __clk_core_init(core);
2575 __clk_free_clk(hw->clk);
2579 kfree(core->parents);
2580 fail_parent_names_copy:
2582 kfree_const(core->parent_names[i]);
2583 kfree(core->parent_names);
2585 kfree_const(core->name);
2589 return ERR_PTR(ret);
2591 EXPORT_SYMBOL_GPL(clk_register);
2594 * clk_hw_register - register a clk_hw and return an error code
2595 * @dev: device that is registering this clock
2596 * @hw: link to hardware-specific clock data
2598 * clk_hw_register is the primary interface for populating the clock tree with
2599 * new clock nodes. It returns an integer equal to zero indicating success or
2600 * less than zero indicating failure. Drivers must test for an error code after
2601 * calling clk_hw_register().
2603 int clk_hw_register(struct device *dev, struct clk_hw *hw)
2605 return PTR_ERR_OR_ZERO(clk_register(dev, hw));
2607 EXPORT_SYMBOL_GPL(clk_hw_register);
2609 /* Free memory allocated for a clock. */
2610 static void __clk_release(struct kref *ref)
2612 struct clk_core *core = container_of(ref, struct clk_core, ref);
2613 int i = core->num_parents;
2615 lockdep_assert_held(&prepare_lock);
2617 kfree(core->parents);
2619 kfree_const(core->parent_names[i]);
2621 kfree(core->parent_names);
2622 kfree_const(core->name);
2627 * Empty clk_ops for unregistered clocks. These are used temporarily
2628 * after clk_unregister() was called on a clock and until last clock
2629 * consumer calls clk_put() and the struct clk object is freed.
2631 static int clk_nodrv_prepare_enable(struct clk_hw *hw)
2636 static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
2641 static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
2642 unsigned long parent_rate)
2647 static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
2652 static const struct clk_ops clk_nodrv_ops = {
2653 .enable = clk_nodrv_prepare_enable,
2654 .disable = clk_nodrv_disable_unprepare,
2655 .prepare = clk_nodrv_prepare_enable,
2656 .unprepare = clk_nodrv_disable_unprepare,
2657 .set_rate = clk_nodrv_set_rate,
2658 .set_parent = clk_nodrv_set_parent,
2662 * clk_unregister - unregister a currently registered clock
2663 * @clk: clock to unregister
2665 void clk_unregister(struct clk *clk)
2667 unsigned long flags;
2669 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2672 clk_debug_unregister(clk->core);
2676 if (clk->core->ops == &clk_nodrv_ops) {
2677 pr_err("%s: unregistered clock: %s\n", __func__,
2682 * Assign empty clock ops for consumers that might still hold
2683 * a reference to this clock.
2685 flags = clk_enable_lock();
2686 clk->core->ops = &clk_nodrv_ops;
2687 clk_enable_unlock(flags);
2689 if (!hlist_empty(&clk->core->children)) {
2690 struct clk_core *child;
2691 struct hlist_node *t;
2693 /* Reparent all children to the orphan list. */
2694 hlist_for_each_entry_safe(child, t, &clk->core->children,
2696 clk_core_set_parent(child, NULL);
2699 hlist_del_init(&clk->core->child_node);
2701 if (clk->core->prepare_count)
2702 pr_warn("%s: unregistering prepared clock: %s\n",
2703 __func__, clk->core->name);
2704 kref_put(&clk->core->ref, __clk_release);
2706 clk_prepare_unlock();
2708 EXPORT_SYMBOL_GPL(clk_unregister);
2711 * clk_hw_unregister - unregister a currently registered clk_hw
2712 * @hw: hardware-specific clock data to unregister
2714 void clk_hw_unregister(struct clk_hw *hw)
2716 clk_unregister(hw->clk);
2718 EXPORT_SYMBOL_GPL(clk_hw_unregister);
2720 static void devm_clk_release(struct device *dev, void *res)
2722 clk_unregister(*(struct clk **)res);
2725 static void devm_clk_hw_release(struct device *dev, void *res)
2727 clk_hw_unregister(*(struct clk_hw **)res);
2731 * devm_clk_register - resource managed clk_register()
2732 * @dev: device that is registering this clock
2733 * @hw: link to hardware-specific clock data
2735 * Managed clk_register(). Clocks returned from this function are
2736 * automatically clk_unregister()ed on driver detach. See clk_register() for
2739 struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
2744 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
2746 return ERR_PTR(-ENOMEM);
2748 clk = clk_register(dev, hw);
2751 devres_add(dev, clkp);
2758 EXPORT_SYMBOL_GPL(devm_clk_register);
2761 * devm_clk_hw_register - resource managed clk_hw_register()
2762 * @dev: device that is registering this clock
2763 * @hw: link to hardware-specific clock data
2765 * Managed clk_hw_register(). Clocks registered by this function are
2766 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
2767 * for more information.
2769 int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
2771 struct clk_hw **hwp;
2774 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
2778 ret = clk_hw_register(dev, hw);
2781 devres_add(dev, hwp);
2788 EXPORT_SYMBOL_GPL(devm_clk_hw_register);
2790 static int devm_clk_match(struct device *dev, void *res, void *data)
2792 struct clk *c = res;
2798 static int devm_clk_hw_match(struct device *dev, void *res, void *data)
2800 struct clk_hw *hw = res;
2808 * devm_clk_unregister - resource managed clk_unregister()
2809 * @clk: clock to unregister
2811 * Deallocate a clock allocated with devm_clk_register(). Normally
2812 * this function will not need to be called and the resource management
2813 * code will ensure that the resource is freed.
2815 void devm_clk_unregister(struct device *dev, struct clk *clk)
2817 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
2819 EXPORT_SYMBOL_GPL(devm_clk_unregister);
2822 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
2823 * @dev: device that is unregistering the hardware-specific clock data
2824 * @hw: link to hardware-specific clock data
2826 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
2827 * this function will not need to be called and the resource management
2828 * code will ensure that the resource is freed.
2830 void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
2832 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
2835 EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
2840 int __clk_get(struct clk *clk)
2842 struct clk_core *core = !clk ? NULL : clk->core;
2845 if (!try_module_get(core->owner))
2848 kref_get(&core->ref);
2853 void __clk_put(struct clk *clk)
2855 struct module *owner;
2857 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2862 hlist_del(&clk->clks_node);
2863 if (clk->min_rate > clk->core->req_rate ||
2864 clk->max_rate < clk->core->req_rate)
2865 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
2867 owner = clk->core->owner;
2868 kref_put(&clk->core->ref, __clk_release);
2870 clk_prepare_unlock();
2877 /*** clk rate change notifiers ***/
2880 * clk_notifier_register - add a clk rate change notifier
2881 * @clk: struct clk * to watch
2882 * @nb: struct notifier_block * with callback info
2884 * Request notification when clk's rate changes. This uses an SRCU
2885 * notifier because we want it to block and notifier unregistrations are
2886 * uncommon. The callbacks associated with the notifier must not
2887 * re-enter into the clk framework by calling any top-level clk APIs;
2888 * this will cause a nested prepare_lock mutex.
2890 * In all notification cases (pre, post and abort rate change) the original
2891 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
2892 * and the new frequency is passed via struct clk_notifier_data.new_rate.
2894 * clk_notifier_register() must be called from non-atomic context.
2895 * Returns -EINVAL if called with null arguments, -ENOMEM upon
2896 * allocation failure; otherwise, passes along the return value of
2897 * srcu_notifier_chain_register().
2899 int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
2901 struct clk_notifier *cn;
2909 /* search the list of notifiers for this clk */
2910 list_for_each_entry(cn, &clk_notifier_list, node)
2914 /* if clk wasn't in the notifier list, allocate new clk_notifier */
2915 if (cn->clk != clk) {
2916 cn = kzalloc(sizeof(struct clk_notifier), GFP_KERNEL);
2921 srcu_init_notifier_head(&cn->notifier_head);
2923 list_add(&cn->node, &clk_notifier_list);
2926 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
2928 clk->core->notifier_count++;
2931 clk_prepare_unlock();
2935 EXPORT_SYMBOL_GPL(clk_notifier_register);
2938 * clk_notifier_unregister - remove a clk rate change notifier
2939 * @clk: struct clk *
2940 * @nb: struct notifier_block * with callback info
2942 * Request no further notification for changes to 'clk' and frees memory
2943 * allocated in clk_notifier_register.
2945 * Returns -EINVAL if called with null arguments; otherwise, passes
2946 * along the return value of srcu_notifier_chain_unregister().
2948 int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
2950 struct clk_notifier *cn = NULL;
2958 list_for_each_entry(cn, &clk_notifier_list, node)
2962 if (cn->clk == clk) {
2963 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
2965 clk->core->notifier_count--;
2967 /* XXX the notifier code should handle this better */
2968 if (!cn->notifier_head.head) {
2969 srcu_cleanup_notifier_head(&cn->notifier_head);
2970 list_del(&cn->node);
2978 clk_prepare_unlock();
2982 EXPORT_SYMBOL_GPL(clk_notifier_unregister);
2986 * struct of_clk_provider - Clock provider registration structure
2987 * @link: Entry in global list of clock providers
2988 * @node: Pointer to device tree node of clock provider
2989 * @get: Get clock callback. Returns NULL or a struct clk for the
2990 * given clock specifier
2991 * @data: context pointer to be passed into @get callback
2993 struct of_clk_provider {
2994 struct list_head link;
2996 struct device_node *node;
2997 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
2998 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
3002 static const struct of_device_id __clk_of_table_sentinel
3003 __used __section(__clk_of_table_end);
3005 static LIST_HEAD(of_clk_providers);
3006 static DEFINE_MUTEX(of_clk_mutex);
3008 struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
3013 EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
3015 struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
3019 EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
3021 struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
3023 struct clk_onecell_data *clk_data = data;
3024 unsigned int idx = clkspec->args[0];
3026 if (idx >= clk_data->clk_num) {
3027 pr_err("%s: invalid clock index %u\n", __func__, idx);
3028 return ERR_PTR(-EINVAL);
3031 return clk_data->clks[idx];
3033 EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
3036 of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
3038 struct clk_hw_onecell_data *hw_data = data;
3039 unsigned int idx = clkspec->args[0];
3041 if (idx >= hw_data->num) {
3042 pr_err("%s: invalid index %u\n", __func__, idx);
3043 return ERR_PTR(-EINVAL);
3046 return hw_data->hws[idx];
3048 EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
3051 * of_clk_add_provider() - Register a clock provider for a node
3052 * @np: Device node pointer associated with clock provider
3053 * @clk_src_get: callback for decoding clock
3054 * @data: context pointer for @clk_src_get callback.
3056 int of_clk_add_provider(struct device_node *np,
3057 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
3061 struct of_clk_provider *cp;
3064 cp = kzalloc(sizeof(struct of_clk_provider), GFP_KERNEL);
3068 cp->node = of_node_get(np);
3070 cp->get = clk_src_get;
3072 mutex_lock(&of_clk_mutex);
3073 list_add(&cp->link, &of_clk_providers);
3074 mutex_unlock(&of_clk_mutex);
3075 pr_debug("Added clock from %s\n", np->full_name);
3077 ret = of_clk_set_defaults(np, true);
3079 of_clk_del_provider(np);
3083 EXPORT_SYMBOL_GPL(of_clk_add_provider);
3086 * of_clk_add_hw_provider() - Register a clock provider for a node
3087 * @np: Device node pointer associated with clock provider
3088 * @get: callback for decoding clk_hw
3089 * @data: context pointer for @get callback.
3091 int of_clk_add_hw_provider(struct device_node *np,
3092 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3096 struct of_clk_provider *cp;
3099 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3103 cp->node = of_node_get(np);
3107 mutex_lock(&of_clk_mutex);
3108 list_add(&cp->link, &of_clk_providers);
3109 mutex_unlock(&of_clk_mutex);
3110 pr_debug("Added clk_hw provider from %s\n", np->full_name);
3112 ret = of_clk_set_defaults(np, true);
3114 of_clk_del_provider(np);
3118 EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
3121 * of_clk_del_provider() - Remove a previously registered clock provider
3122 * @np: Device node pointer associated with clock provider
3124 void of_clk_del_provider(struct device_node *np)
3126 struct of_clk_provider *cp;
3128 mutex_lock(&of_clk_mutex);
3129 list_for_each_entry(cp, &of_clk_providers, link) {
3130 if (cp->node == np) {
3131 list_del(&cp->link);
3132 of_node_put(cp->node);
3137 mutex_unlock(&of_clk_mutex);
3139 EXPORT_SYMBOL_GPL(of_clk_del_provider);
3141 static struct clk_hw *
3142 __of_clk_get_hw_from_provider(struct of_clk_provider *provider,
3143 struct of_phandle_args *clkspec)
3146 struct clk_hw *hw = ERR_PTR(-EPROBE_DEFER);
3148 if (provider->get_hw) {
3149 hw = provider->get_hw(clkspec, provider->data);
3150 } else if (provider->get) {
3151 clk = provider->get(clkspec, provider->data);
3153 hw = __clk_get_hw(clk);
3161 struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
3162 const char *dev_id, const char *con_id)
3164 struct of_clk_provider *provider;
3165 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
3166 struct clk_hw *hw = ERR_PTR(-EPROBE_DEFER);
3169 return ERR_PTR(-EINVAL);
3171 /* Check if we have such a provider in our array */
3172 mutex_lock(&of_clk_mutex);
3173 list_for_each_entry(provider, &of_clk_providers, link) {
3174 if (provider->node == clkspec->np)
3175 hw = __of_clk_get_hw_from_provider(provider, clkspec);
3177 clk = __clk_create_clk(hw, dev_id, con_id);
3179 if (!IS_ERR(clk) && !__clk_get(clk)) {
3180 __clk_free_clk(clk);
3181 clk = ERR_PTR(-ENOENT);
3187 mutex_unlock(&of_clk_mutex);
3193 * of_clk_get_from_provider() - Lookup a clock from a clock provider
3194 * @clkspec: pointer to a clock specifier data structure
3196 * This function looks up a struct clk from the registered list of clock
3197 * providers, an input is a clock specifier data structure as returned
3198 * from the of_parse_phandle_with_args() function call.
3200 struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
3202 return __of_clk_get_from_provider(clkspec, NULL, __func__);
3204 EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
3207 * of_clk_get_parent_count() - Count the number of clocks a device node has
3208 * @np: device node to count
3210 * Returns: The number of clocks that are possible parents of this node
3212 unsigned int of_clk_get_parent_count(struct device_node *np)
3216 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
3222 EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
3224 const char *of_clk_get_parent_name(struct device_node *np, int index)
3226 struct of_phandle_args clkspec;
3227 struct property *prop;
3228 const char *clk_name;
3235 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
3240 index = clkspec.args_count ? clkspec.args[0] : 0;
3243 /* if there is an indices property, use it to transfer the index
3244 * specified into an array offset for the clock-output-names property.
3246 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
3253 /* We went off the end of 'clock-indices' without finding it */
3257 if (of_property_read_string_index(clkspec.np, "clock-output-names",
3261 * Best effort to get the name if the clock has been
3262 * registered with the framework. If the clock isn't
3263 * registered, we return the node name as the name of
3264 * the clock as long as #clock-cells = 0.
3266 clk = of_clk_get_from_provider(&clkspec);
3268 if (clkspec.args_count == 0)
3269 clk_name = clkspec.np->name;
3273 clk_name = __clk_get_name(clk);
3279 of_node_put(clkspec.np);
3282 EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3285 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
3287 * @np: Device node pointer associated with clock provider
3288 * @parents: pointer to char array that hold the parents' names
3289 * @size: size of the @parents array
3291 * Return: number of parents for the clock node.
3293 int of_clk_parent_fill(struct device_node *np, const char **parents,
3298 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
3303 EXPORT_SYMBOL_GPL(of_clk_parent_fill);
3305 struct clock_provider {
3306 of_clk_init_cb_t clk_init_cb;
3307 struct device_node *np;
3308 struct list_head node;
3312 * This function looks for a parent clock. If there is one, then it
3313 * checks that the provider for this parent clock was initialized, in
3314 * this case the parent clock will be ready.
3316 static int parent_ready(struct device_node *np)
3321 struct clk *clk = of_clk_get(np, i);
3323 /* this parent is ready we can check the next one */
3330 /* at least one parent is not ready, we exit now */
3331 if (PTR_ERR(clk) == -EPROBE_DEFER)
3335 * Here we make assumption that the device tree is
3336 * written correctly. So an error means that there is
3337 * no more parent. As we didn't exit yet, then the
3338 * previous parent are ready. If there is no clock
3339 * parent, no need to wait for them, then we can
3340 * consider their absence as being ready
3347 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
3348 * @np: Device node pointer associated with clock provider
3349 * @index: clock index
3350 * @flags: pointer to clk_core->flags
3352 * Detects if the clock-critical property exists and, if so, sets the
3353 * corresponding CLK_IS_CRITICAL flag.
3355 * Do not use this function. It exists only for legacy Device Tree
3356 * bindings, such as the one-clock-per-node style that are outdated.
3357 * Those bindings typically put all clock data into .dts and the Linux
3358 * driver has no clock data, thus making it impossible to set this flag
3359 * correctly from the driver. Only those drivers may call
3360 * of_clk_detect_critical from their setup functions.
3362 * Return: error code or zero on success
3364 int of_clk_detect_critical(struct device_node *np,
3365 int index, unsigned long *flags)
3367 struct property *prop;
3374 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
3376 *flags |= CLK_IS_CRITICAL;
3382 * of_clk_init() - Scan and init clock providers from the DT
3383 * @matches: array of compatible values and init functions for providers.
3385 * This function scans the device tree for matching clock providers
3386 * and calls their initialization functions. It also does it by trying
3387 * to follow the dependencies.
3389 void __init of_clk_init(const struct of_device_id *matches)
3391 const struct of_device_id *match;
3392 struct device_node *np;
3393 struct clock_provider *clk_provider, *next;
3396 LIST_HEAD(clk_provider_list);
3399 matches = &__clk_of_table;
3401 /* First prepare the list of the clocks providers */
3402 for_each_matching_node_and_match(np, matches, &match) {
3403 struct clock_provider *parent;
3405 if (!of_device_is_available(np))
3408 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
3410 list_for_each_entry_safe(clk_provider, next,
3411 &clk_provider_list, node) {
3412 list_del(&clk_provider->node);
3413 of_node_put(clk_provider->np);
3414 kfree(clk_provider);
3420 parent->clk_init_cb = match->data;
3421 parent->np = of_node_get(np);
3422 list_add_tail(&parent->node, &clk_provider_list);
3425 while (!list_empty(&clk_provider_list)) {
3426 is_init_done = false;
3427 list_for_each_entry_safe(clk_provider, next,
3428 &clk_provider_list, node) {
3429 if (force || parent_ready(clk_provider->np)) {
3431 clk_provider->clk_init_cb(clk_provider->np);
3432 of_clk_set_defaults(clk_provider->np, true);
3434 list_del(&clk_provider->node);
3435 of_node_put(clk_provider->np);
3436 kfree(clk_provider);
3437 is_init_done = true;
3442 * We didn't manage to initialize any of the
3443 * remaining providers during the last loop, so now we
3444 * initialize all the remaining ones unconditionally
3445 * in case the clock parent was not mandatory