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 static struct hlist_head *all_lists[] = {
48 /*** private data structures ***/
52 const struct clk_ops *ops;
55 struct clk_core *parent;
56 const char **parent_names;
57 struct clk_core **parents;
61 unsigned long req_rate;
62 unsigned long new_rate;
63 struct clk_core *new_parent;
64 struct clk_core *new_child;
67 unsigned int enable_count;
68 unsigned int prepare_count;
69 unsigned long min_rate;
70 unsigned long max_rate;
71 unsigned long accuracy;
73 struct hlist_head children;
74 struct hlist_node child_node;
75 struct hlist_head clks;
76 unsigned int notifier_count;
77 #ifdef CONFIG_DEBUG_FS
78 struct dentry *dentry;
79 struct hlist_node debug_node;
84 #define CREATE_TRACE_POINTS
85 #include <trace/events/clk.h>
88 struct clk_core *core;
91 unsigned long min_rate;
92 unsigned long max_rate;
93 struct hlist_node clks_node;
97 static void clk_prepare_lock(void)
99 if (!mutex_trylock(&prepare_lock)) {
100 if (prepare_owner == current) {
104 mutex_lock(&prepare_lock);
106 WARN_ON_ONCE(prepare_owner != NULL);
107 WARN_ON_ONCE(prepare_refcnt != 0);
108 prepare_owner = current;
112 static void clk_prepare_unlock(void)
114 WARN_ON_ONCE(prepare_owner != current);
115 WARN_ON_ONCE(prepare_refcnt == 0);
117 if (--prepare_refcnt)
119 prepare_owner = NULL;
120 mutex_unlock(&prepare_lock);
123 static unsigned long clk_enable_lock(void)
124 __acquires(enable_lock)
128 if (!spin_trylock_irqsave(&enable_lock, flags)) {
129 if (enable_owner == current) {
131 __acquire(enable_lock);
134 spin_lock_irqsave(&enable_lock, flags);
136 WARN_ON_ONCE(enable_owner != NULL);
137 WARN_ON_ONCE(enable_refcnt != 0);
138 enable_owner = current;
143 static void clk_enable_unlock(unsigned long flags)
144 __releases(enable_lock)
146 WARN_ON_ONCE(enable_owner != current);
147 WARN_ON_ONCE(enable_refcnt == 0);
149 if (--enable_refcnt) {
150 __release(enable_lock);
154 spin_unlock_irqrestore(&enable_lock, flags);
157 static bool clk_core_is_prepared(struct clk_core *core)
160 * .is_prepared is optional for clocks that can prepare
161 * fall back to software usage counter if it is missing
163 if (!core->ops->is_prepared)
164 return core->prepare_count;
166 return core->ops->is_prepared(core->hw);
169 static bool clk_core_is_enabled(struct clk_core *core)
172 * .is_enabled is only mandatory for clocks that gate
173 * fall back to software usage counter if .is_enabled is missing
175 if (!core->ops->is_enabled)
176 return core->enable_count;
178 return core->ops->is_enabled(core->hw);
181 /*** helper functions ***/
183 const char *__clk_get_name(const struct clk *clk)
185 return !clk ? NULL : clk->core->name;
187 EXPORT_SYMBOL_GPL(__clk_get_name);
189 const char *clk_hw_get_name(const struct clk_hw *hw)
191 return hw->core->name;
193 EXPORT_SYMBOL_GPL(clk_hw_get_name);
195 struct clk_hw *__clk_get_hw(struct clk *clk)
197 return !clk ? NULL : clk->core->hw;
199 EXPORT_SYMBOL_GPL(__clk_get_hw);
201 unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
203 return hw->core->num_parents;
205 EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
207 struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
209 return hw->core->parent ? hw->core->parent->hw : NULL;
211 EXPORT_SYMBOL_GPL(clk_hw_get_parent);
213 static struct clk_core *__clk_lookup_subtree(const char *name,
214 struct clk_core *core)
216 struct clk_core *child;
217 struct clk_core *ret;
219 if (!strcmp(core->name, name))
222 hlist_for_each_entry(child, &core->children, child_node) {
223 ret = __clk_lookup_subtree(name, child);
231 static struct clk_core *clk_core_lookup(const char *name)
233 struct clk_core *root_clk;
234 struct clk_core *ret;
239 /* search the 'proper' clk tree first */
240 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
241 ret = __clk_lookup_subtree(name, root_clk);
246 /* if not found, then search the orphan tree */
247 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
248 ret = __clk_lookup_subtree(name, root_clk);
256 static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
259 if (!core || index >= core->num_parents)
262 if (!core->parents[index])
263 core->parents[index] =
264 clk_core_lookup(core->parent_names[index]);
266 return core->parents[index];
270 clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
272 struct clk_core *parent;
274 parent = clk_core_get_parent_by_index(hw->core, index);
276 return !parent ? NULL : parent->hw;
278 EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
280 unsigned int __clk_get_enable_count(struct clk *clk)
282 return !clk ? 0 : clk->core->enable_count;
285 static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
296 if (!core->num_parents)
306 unsigned long clk_hw_get_rate(const struct clk_hw *hw)
308 return clk_core_get_rate_nolock(hw->core);
310 EXPORT_SYMBOL_GPL(clk_hw_get_rate);
312 static unsigned long __clk_get_accuracy(struct clk_core *core)
317 return core->accuracy;
320 unsigned long __clk_get_flags(struct clk *clk)
322 return !clk ? 0 : clk->core->flags;
324 EXPORT_SYMBOL_GPL(__clk_get_flags);
326 unsigned long clk_hw_get_flags(const struct clk_hw *hw)
328 return hw->core->flags;
330 EXPORT_SYMBOL_GPL(clk_hw_get_flags);
332 bool clk_hw_is_prepared(const struct clk_hw *hw)
334 return clk_core_is_prepared(hw->core);
337 bool clk_hw_is_enabled(const struct clk_hw *hw)
339 return clk_core_is_enabled(hw->core);
342 bool __clk_is_enabled(struct clk *clk)
347 return clk_core_is_enabled(clk->core);
349 EXPORT_SYMBOL_GPL(__clk_is_enabled);
351 static bool mux_is_better_rate(unsigned long rate, unsigned long now,
352 unsigned long best, unsigned long flags)
354 if (flags & CLK_MUX_ROUND_CLOSEST)
355 return abs(now - rate) < abs(best - rate);
357 return now <= rate && now > best;
360 int clk_mux_determine_rate_flags(struct clk_hw *hw,
361 struct clk_rate_request *req,
364 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
365 int i, num_parents, ret;
366 unsigned long best = 0;
367 struct clk_rate_request parent_req = *req;
369 /* if NO_REPARENT flag set, pass through to current parent */
370 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
371 parent = core->parent;
372 if (core->flags & CLK_SET_RATE_PARENT) {
373 ret = __clk_determine_rate(parent ? parent->hw : NULL,
378 best = parent_req.rate;
380 best = clk_core_get_rate_nolock(parent);
382 best = clk_core_get_rate_nolock(core);
388 /* find the parent that can provide the fastest rate <= rate */
389 num_parents = core->num_parents;
390 for (i = 0; i < num_parents; i++) {
391 parent = clk_core_get_parent_by_index(core, i);
395 if (core->flags & CLK_SET_RATE_PARENT) {
397 ret = __clk_determine_rate(parent->hw, &parent_req);
401 parent_req.rate = clk_core_get_rate_nolock(parent);
404 if (mux_is_better_rate(req->rate, parent_req.rate,
406 best_parent = parent;
407 best = parent_req.rate;
416 req->best_parent_hw = best_parent->hw;
417 req->best_parent_rate = best;
422 EXPORT_SYMBOL_GPL(clk_mux_determine_rate_flags);
424 struct clk *__clk_lookup(const char *name)
426 struct clk_core *core = clk_core_lookup(name);
428 return !core ? NULL : core->hw->clk;
431 static void clk_core_get_boundaries(struct clk_core *core,
432 unsigned long *min_rate,
433 unsigned long *max_rate)
435 struct clk *clk_user;
437 *min_rate = core->min_rate;
438 *max_rate = core->max_rate;
440 hlist_for_each_entry(clk_user, &core->clks, clks_node)
441 *min_rate = max(*min_rate, clk_user->min_rate);
443 hlist_for_each_entry(clk_user, &core->clks, clks_node)
444 *max_rate = min(*max_rate, clk_user->max_rate);
447 void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
448 unsigned long max_rate)
450 hw->core->min_rate = min_rate;
451 hw->core->max_rate = max_rate;
453 EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
456 * Helper for finding best parent to provide a given frequency. This can be used
457 * directly as a determine_rate callback (e.g. for a mux), or from a more
458 * complex clock that may combine a mux with other operations.
460 int __clk_mux_determine_rate(struct clk_hw *hw,
461 struct clk_rate_request *req)
463 return clk_mux_determine_rate_flags(hw, req, 0);
465 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
467 int __clk_mux_determine_rate_closest(struct clk_hw *hw,
468 struct clk_rate_request *req)
470 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
472 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
476 static void clk_core_unprepare(struct clk_core *core)
478 lockdep_assert_held(&prepare_lock);
483 if (WARN_ON(core->prepare_count == 0))
486 if (WARN_ON(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL))
489 if (--core->prepare_count > 0)
492 WARN_ON(core->enable_count > 0);
494 trace_clk_unprepare(core);
496 if (core->ops->unprepare)
497 core->ops->unprepare(core->hw);
499 trace_clk_unprepare_complete(core);
500 clk_core_unprepare(core->parent);
503 static void clk_core_unprepare_lock(struct clk_core *core)
506 clk_core_unprepare(core);
507 clk_prepare_unlock();
511 * clk_unprepare - undo preparation of a clock source
512 * @clk: the clk being unprepared
514 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
515 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
516 * if the operation may sleep. One example is a clk which is accessed over
517 * I2c. In the complex case a clk gate operation may require a fast and a slow
518 * part. It is this reason that clk_unprepare and clk_disable are not mutually
519 * exclusive. In fact clk_disable must be called before clk_unprepare.
521 void clk_unprepare(struct clk *clk)
523 if (IS_ERR_OR_NULL(clk))
526 clk_core_unprepare_lock(clk->core);
528 EXPORT_SYMBOL_GPL(clk_unprepare);
530 static int clk_core_prepare(struct clk_core *core)
534 lockdep_assert_held(&prepare_lock);
539 if (core->prepare_count == 0) {
540 ret = clk_core_prepare(core->parent);
544 trace_clk_prepare(core);
546 if (core->ops->prepare)
547 ret = core->ops->prepare(core->hw);
549 trace_clk_prepare_complete(core);
552 clk_core_unprepare(core->parent);
557 core->prepare_count++;
562 static int clk_core_prepare_lock(struct clk_core *core)
567 ret = clk_core_prepare(core);
568 clk_prepare_unlock();
574 * clk_prepare - prepare a clock source
575 * @clk: the clk being prepared
577 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
578 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
579 * operation may sleep. One example is a clk which is accessed over I2c. In
580 * the complex case a clk ungate operation may require a fast and a slow part.
581 * It is this reason that clk_prepare and clk_enable are not mutually
582 * exclusive. In fact clk_prepare must be called before clk_enable.
583 * Returns 0 on success, -EERROR otherwise.
585 int clk_prepare(struct clk *clk)
590 return clk_core_prepare_lock(clk->core);
592 EXPORT_SYMBOL_GPL(clk_prepare);
594 static void clk_core_disable(struct clk_core *core)
596 lockdep_assert_held(&enable_lock);
601 if (WARN_ON(core->enable_count == 0))
604 if (WARN_ON(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL))
607 if (--core->enable_count > 0)
610 trace_clk_disable_rcuidle(core);
612 if (core->ops->disable)
613 core->ops->disable(core->hw);
615 trace_clk_disable_complete_rcuidle(core);
617 clk_core_disable(core->parent);
620 static void clk_core_disable_lock(struct clk_core *core)
624 flags = clk_enable_lock();
625 clk_core_disable(core);
626 clk_enable_unlock(flags);
630 * clk_disable - gate a clock
631 * @clk: the clk being gated
633 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
634 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
635 * clk if the operation is fast and will never sleep. One example is a
636 * SoC-internal clk which is controlled via simple register writes. In the
637 * complex case a clk gate operation may require a fast and a slow part. It is
638 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
639 * In fact clk_disable must be called before clk_unprepare.
641 void clk_disable(struct clk *clk)
643 if (IS_ERR_OR_NULL(clk))
646 clk_core_disable_lock(clk->core);
648 EXPORT_SYMBOL_GPL(clk_disable);
650 static int clk_core_enable(struct clk_core *core)
654 lockdep_assert_held(&enable_lock);
659 if (WARN_ON(core->prepare_count == 0))
662 if (core->enable_count == 0) {
663 ret = clk_core_enable(core->parent);
668 trace_clk_enable_rcuidle(core);
670 if (core->ops->enable)
671 ret = core->ops->enable(core->hw);
673 trace_clk_enable_complete_rcuidle(core);
676 clk_core_disable(core->parent);
681 core->enable_count++;
685 static int clk_core_enable_lock(struct clk_core *core)
690 flags = clk_enable_lock();
691 ret = clk_core_enable(core);
692 clk_enable_unlock(flags);
698 * clk_enable - ungate a clock
699 * @clk: the clk being ungated
701 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
702 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
703 * if the operation will never sleep. One example is a SoC-internal clk which
704 * is controlled via simple register writes. In the complex case a clk ungate
705 * operation may require a fast and a slow part. It is this reason that
706 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
707 * must be called before clk_enable. Returns 0 on success, -EERROR
710 int clk_enable(struct clk *clk)
715 return clk_core_enable_lock(clk->core);
717 EXPORT_SYMBOL_GPL(clk_enable);
719 static int clk_core_prepare_enable(struct clk_core *core)
723 ret = clk_core_prepare_lock(core);
727 ret = clk_core_enable_lock(core);
729 clk_core_unprepare_lock(core);
734 static void clk_core_disable_unprepare(struct clk_core *core)
736 clk_core_disable_lock(core);
737 clk_core_unprepare_lock(core);
740 static void clk_unprepare_unused_subtree(struct clk_core *core)
742 struct clk_core *child;
744 lockdep_assert_held(&prepare_lock);
746 hlist_for_each_entry(child, &core->children, child_node)
747 clk_unprepare_unused_subtree(child);
749 if (core->prepare_count)
752 if (core->flags & CLK_IGNORE_UNUSED)
755 if (clk_core_is_prepared(core)) {
756 trace_clk_unprepare(core);
757 if (core->ops->unprepare_unused)
758 core->ops->unprepare_unused(core->hw);
759 else if (core->ops->unprepare)
760 core->ops->unprepare(core->hw);
761 trace_clk_unprepare_complete(core);
765 static void clk_disable_unused_subtree(struct clk_core *core)
767 struct clk_core *child;
770 lockdep_assert_held(&prepare_lock);
772 hlist_for_each_entry(child, &core->children, child_node)
773 clk_disable_unused_subtree(child);
775 if (core->flags & CLK_OPS_PARENT_ENABLE)
776 clk_core_prepare_enable(core->parent);
778 flags = clk_enable_lock();
780 if (core->enable_count)
783 if (core->flags & CLK_IGNORE_UNUSED)
787 * some gate clocks have special needs during the disable-unused
788 * sequence. call .disable_unused if available, otherwise fall
791 if (clk_core_is_enabled(core)) {
792 trace_clk_disable(core);
793 if (core->ops->disable_unused)
794 core->ops->disable_unused(core->hw);
795 else if (core->ops->disable)
796 core->ops->disable(core->hw);
797 trace_clk_disable_complete(core);
801 clk_enable_unlock(flags);
802 if (core->flags & CLK_OPS_PARENT_ENABLE)
803 clk_core_disable_unprepare(core->parent);
806 static bool clk_ignore_unused;
807 static int __init clk_ignore_unused_setup(char *__unused)
809 clk_ignore_unused = true;
812 __setup("clk_ignore_unused", clk_ignore_unused_setup);
814 static int clk_disable_unused(void)
816 struct clk_core *core;
818 if (clk_ignore_unused) {
819 pr_warn("clk: Not disabling unused clocks\n");
825 hlist_for_each_entry(core, &clk_root_list, child_node)
826 clk_disable_unused_subtree(core);
828 hlist_for_each_entry(core, &clk_orphan_list, child_node)
829 clk_disable_unused_subtree(core);
831 hlist_for_each_entry(core, &clk_root_list, child_node)
832 clk_unprepare_unused_subtree(core);
834 hlist_for_each_entry(core, &clk_orphan_list, child_node)
835 clk_unprepare_unused_subtree(core);
837 clk_prepare_unlock();
841 late_initcall_sync(clk_disable_unused);
843 static int clk_core_round_rate_nolock(struct clk_core *core,
844 struct clk_rate_request *req)
846 struct clk_core *parent;
849 lockdep_assert_held(&prepare_lock);
854 parent = core->parent;
856 req->best_parent_hw = parent->hw;
857 req->best_parent_rate = parent->rate;
859 req->best_parent_hw = NULL;
860 req->best_parent_rate = 0;
863 if (core->ops->determine_rate) {
864 return core->ops->determine_rate(core->hw, req);
865 } else if (core->ops->round_rate) {
866 rate = core->ops->round_rate(core->hw, req->rate,
867 &req->best_parent_rate);
872 } else if (core->flags & CLK_SET_RATE_PARENT) {
873 return clk_core_round_rate_nolock(parent, req);
875 req->rate = core->rate;
882 * __clk_determine_rate - get the closest rate actually supported by a clock
883 * @hw: determine the rate of this clock
884 * @req: target rate request
886 * Useful for clk_ops such as .set_rate and .determine_rate.
888 int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
895 return clk_core_round_rate_nolock(hw->core, req);
897 EXPORT_SYMBOL_GPL(__clk_determine_rate);
899 unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
902 struct clk_rate_request req;
904 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
907 ret = clk_core_round_rate_nolock(hw->core, &req);
913 EXPORT_SYMBOL_GPL(clk_hw_round_rate);
916 * clk_round_rate - round the given rate for a clk
917 * @clk: the clk for which we are rounding a rate
918 * @rate: the rate which is to be rounded
920 * Takes in a rate as input and rounds it to a rate that the clk can actually
921 * use which is then returned. If clk doesn't support round_rate operation
922 * then the parent rate is returned.
924 long clk_round_rate(struct clk *clk, unsigned long rate)
926 struct clk_rate_request req;
934 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
937 ret = clk_core_round_rate_nolock(clk->core, &req);
938 clk_prepare_unlock();
945 EXPORT_SYMBOL_GPL(clk_round_rate);
948 * __clk_notify - call clk notifier chain
949 * @core: clk that is changing rate
950 * @msg: clk notifier type (see include/linux/clk.h)
951 * @old_rate: old clk rate
952 * @new_rate: new clk rate
954 * Triggers a notifier call chain on the clk rate-change notification
955 * for 'clk'. Passes a pointer to the struct clk and the previous
956 * and current rates to the notifier callback. Intended to be called by
957 * internal clock code only. Returns NOTIFY_DONE from the last driver
958 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
959 * a driver returns that.
961 static int __clk_notify(struct clk_core *core, unsigned long msg,
962 unsigned long old_rate, unsigned long new_rate)
964 struct clk_notifier *cn;
965 struct clk_notifier_data cnd;
966 int ret = NOTIFY_DONE;
968 cnd.old_rate = old_rate;
969 cnd.new_rate = new_rate;
971 list_for_each_entry(cn, &clk_notifier_list, node) {
972 if (cn->clk->core == core) {
974 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
976 if (ret & NOTIFY_STOP_MASK)
985 * __clk_recalc_accuracies
986 * @core: first clk in the subtree
988 * Walks the subtree of clks starting with clk and recalculates accuracies as
989 * it goes. Note that if a clk does not implement the .recalc_accuracy
990 * callback then it is assumed that the clock will take on the accuracy of its
993 static void __clk_recalc_accuracies(struct clk_core *core)
995 unsigned long parent_accuracy = 0;
996 struct clk_core *child;
998 lockdep_assert_held(&prepare_lock);
1001 parent_accuracy = core->parent->accuracy;
1003 if (core->ops->recalc_accuracy)
1004 core->accuracy = core->ops->recalc_accuracy(core->hw,
1007 core->accuracy = parent_accuracy;
1009 hlist_for_each_entry(child, &core->children, child_node)
1010 __clk_recalc_accuracies(child);
1013 static long clk_core_get_accuracy(struct clk_core *core)
1015 unsigned long accuracy;
1018 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1019 __clk_recalc_accuracies(core);
1021 accuracy = __clk_get_accuracy(core);
1022 clk_prepare_unlock();
1028 * clk_get_accuracy - return the accuracy of clk
1029 * @clk: the clk whose accuracy is being returned
1031 * Simply returns the cached accuracy of the clk, unless
1032 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1034 * If clk is NULL then returns 0.
1036 long clk_get_accuracy(struct clk *clk)
1041 return clk_core_get_accuracy(clk->core);
1043 EXPORT_SYMBOL_GPL(clk_get_accuracy);
1045 static unsigned long clk_recalc(struct clk_core *core,
1046 unsigned long parent_rate)
1048 if (core->ops->recalc_rate)
1049 return core->ops->recalc_rate(core->hw, parent_rate);
1054 * __clk_recalc_rates
1055 * @core: first clk in the subtree
1056 * @msg: notification type (see include/linux/clk.h)
1058 * Walks the subtree of clks starting with clk and recalculates rates as it
1059 * goes. Note that if a clk does not implement the .recalc_rate callback then
1060 * it is assumed that the clock will take on the rate of its parent.
1062 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1065 static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
1067 unsigned long old_rate;
1068 unsigned long parent_rate = 0;
1069 struct clk_core *child;
1071 lockdep_assert_held(&prepare_lock);
1073 old_rate = core->rate;
1076 parent_rate = core->parent->rate;
1078 core->rate = clk_recalc(core, parent_rate);
1081 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1082 * & ABORT_RATE_CHANGE notifiers
1084 if (core->notifier_count && msg)
1085 __clk_notify(core, msg, old_rate, core->rate);
1087 hlist_for_each_entry(child, &core->children, child_node)
1088 __clk_recalc_rates(child, msg);
1091 static unsigned long clk_core_get_rate(struct clk_core *core)
1097 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1098 __clk_recalc_rates(core, 0);
1100 rate = clk_core_get_rate_nolock(core);
1101 clk_prepare_unlock();
1107 * clk_get_rate - return the rate of clk
1108 * @clk: the clk whose rate is being returned
1110 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1111 * is set, which means a recalc_rate will be issued.
1112 * If clk is NULL then returns 0.
1114 unsigned long clk_get_rate(struct clk *clk)
1119 return clk_core_get_rate(clk->core);
1121 EXPORT_SYMBOL_GPL(clk_get_rate);
1123 static int clk_fetch_parent_index(struct clk_core *core,
1124 struct clk_core *parent)
1131 for (i = 0; i < core->num_parents; i++)
1132 if (clk_core_get_parent_by_index(core, i) == parent)
1139 * Update the orphan status of @core and all its children.
1141 static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1143 struct clk_core *child;
1145 core->orphan = is_orphan;
1147 hlist_for_each_entry(child, &core->children, child_node)
1148 clk_core_update_orphan_status(child, is_orphan);
1151 static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
1153 bool was_orphan = core->orphan;
1155 hlist_del(&core->child_node);
1158 bool becomes_orphan = new_parent->orphan;
1160 /* avoid duplicate POST_RATE_CHANGE notifications */
1161 if (new_parent->new_child == core)
1162 new_parent->new_child = NULL;
1164 hlist_add_head(&core->child_node, &new_parent->children);
1166 if (was_orphan != becomes_orphan)
1167 clk_core_update_orphan_status(core, becomes_orphan);
1169 hlist_add_head(&core->child_node, &clk_orphan_list);
1171 clk_core_update_orphan_status(core, true);
1174 core->parent = new_parent;
1177 static struct clk_core *__clk_set_parent_before(struct clk_core *core,
1178 struct clk_core *parent)
1180 unsigned long flags;
1181 struct clk_core *old_parent = core->parent;
1184 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1186 * 2. Migrate prepare state between parents and prevent race with
1189 * If the clock is not prepared, then a race with
1190 * clk_enable/disable() is impossible since we already have the
1191 * prepare lock (future calls to clk_enable() need to be preceded by
1194 * If the clock is prepared, migrate the prepared state to the new
1195 * parent and also protect against a race with clk_enable() by
1196 * forcing the clock and the new parent on. This ensures that all
1197 * future calls to clk_enable() are practically NOPs with respect to
1198 * hardware and software states.
1200 * See also: Comment for clk_set_parent() below.
1203 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1204 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1205 clk_core_prepare_enable(old_parent);
1206 clk_core_prepare_enable(parent);
1209 /* migrate prepare count if > 0 */
1210 if (core->prepare_count) {
1211 clk_core_prepare_enable(parent);
1212 clk_core_enable_lock(core);
1215 /* update the clk tree topology */
1216 flags = clk_enable_lock();
1217 clk_reparent(core, parent);
1218 clk_enable_unlock(flags);
1223 static void __clk_set_parent_after(struct clk_core *core,
1224 struct clk_core *parent,
1225 struct clk_core *old_parent)
1228 * Finish the migration of prepare state and undo the changes done
1229 * for preventing a race with clk_enable().
1231 if (core->prepare_count) {
1232 clk_core_disable_lock(core);
1233 clk_core_disable_unprepare(old_parent);
1236 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1237 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1238 clk_core_disable_unprepare(parent);
1239 clk_core_disable_unprepare(old_parent);
1243 static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
1246 unsigned long flags;
1248 struct clk_core *old_parent;
1250 old_parent = __clk_set_parent_before(core, parent);
1252 trace_clk_set_parent(core, parent);
1254 /* change clock input source */
1255 if (parent && core->ops->set_parent)
1256 ret = core->ops->set_parent(core->hw, p_index);
1258 trace_clk_set_parent_complete(core, parent);
1261 flags = clk_enable_lock();
1262 clk_reparent(core, old_parent);
1263 clk_enable_unlock(flags);
1264 __clk_set_parent_after(core, old_parent, parent);
1269 __clk_set_parent_after(core, parent, old_parent);
1275 * __clk_speculate_rates
1276 * @core: first clk in the subtree
1277 * @parent_rate: the "future" rate of clk's parent
1279 * Walks the subtree of clks starting with clk, speculating rates as it
1280 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1282 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1283 * pre-rate change notifications and returns early if no clks in the
1284 * subtree have subscribed to the notifications. Note that if a clk does not
1285 * implement the .recalc_rate callback then it is assumed that the clock will
1286 * take on the rate of its parent.
1288 static int __clk_speculate_rates(struct clk_core *core,
1289 unsigned long parent_rate)
1291 struct clk_core *child;
1292 unsigned long new_rate;
1293 int ret = NOTIFY_DONE;
1295 lockdep_assert_held(&prepare_lock);
1297 new_rate = clk_recalc(core, parent_rate);
1299 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
1300 if (core->notifier_count)
1301 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
1303 if (ret & NOTIFY_STOP_MASK) {
1304 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
1305 __func__, core->name, ret);
1309 hlist_for_each_entry(child, &core->children, child_node) {
1310 ret = __clk_speculate_rates(child, new_rate);
1311 if (ret & NOTIFY_STOP_MASK)
1319 static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
1320 struct clk_core *new_parent, u8 p_index)
1322 struct clk_core *child;
1324 core->new_rate = new_rate;
1325 core->new_parent = new_parent;
1326 core->new_parent_index = p_index;
1327 /* include clk in new parent's PRE_RATE_CHANGE notifications */
1328 core->new_child = NULL;
1329 if (new_parent && new_parent != core->parent)
1330 new_parent->new_child = core;
1332 hlist_for_each_entry(child, &core->children, child_node) {
1333 child->new_rate = clk_recalc(child, new_rate);
1334 clk_calc_subtree(child, child->new_rate, NULL, 0);
1339 * calculate the new rates returning the topmost clock that has to be
1342 static struct clk_core *clk_calc_new_rates(struct clk_core *core,
1345 struct clk_core *top = core;
1346 struct clk_core *old_parent, *parent;
1347 unsigned long best_parent_rate = 0;
1348 unsigned long new_rate;
1349 unsigned long min_rate;
1350 unsigned long max_rate;
1355 if (IS_ERR_OR_NULL(core))
1358 /* save parent rate, if it exists */
1359 parent = old_parent = core->parent;
1361 best_parent_rate = parent->rate;
1363 clk_core_get_boundaries(core, &min_rate, &max_rate);
1365 /* find the closest rate and parent clk/rate */
1366 if (core->ops->determine_rate) {
1367 struct clk_rate_request req;
1370 req.min_rate = min_rate;
1371 req.max_rate = max_rate;
1373 req.best_parent_hw = parent->hw;
1374 req.best_parent_rate = parent->rate;
1376 req.best_parent_hw = NULL;
1377 req.best_parent_rate = 0;
1380 ret = core->ops->determine_rate(core->hw, &req);
1384 best_parent_rate = req.best_parent_rate;
1385 new_rate = req.rate;
1386 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
1387 } else if (core->ops->round_rate) {
1388 ret = core->ops->round_rate(core->hw, rate,
1394 if (new_rate < min_rate || new_rate > max_rate)
1396 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
1397 /* pass-through clock without adjustable parent */
1398 core->new_rate = core->rate;
1401 /* pass-through clock with adjustable parent */
1402 top = clk_calc_new_rates(parent, rate);
1403 new_rate = parent->new_rate;
1407 /* some clocks must be gated to change parent */
1408 if (parent != old_parent &&
1409 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
1410 pr_debug("%s: %s not gated but wants to reparent\n",
1411 __func__, core->name);
1415 /* try finding the new parent index */
1416 if (parent && core->num_parents > 1) {
1417 p_index = clk_fetch_parent_index(core, parent);
1419 pr_debug("%s: clk %s can not be parent of clk %s\n",
1420 __func__, parent->name, core->name);
1425 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
1426 best_parent_rate != parent->rate)
1427 top = clk_calc_new_rates(parent, best_parent_rate);
1430 clk_calc_subtree(core, new_rate, parent, p_index);
1436 * Notify about rate changes in a subtree. Always walk down the whole tree
1437 * so that in case of an error we can walk down the whole tree again and
1440 static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
1441 unsigned long event)
1443 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
1444 int ret = NOTIFY_DONE;
1446 if (core->rate == core->new_rate)
1449 if (core->notifier_count) {
1450 ret = __clk_notify(core, event, core->rate, core->new_rate);
1451 if (ret & NOTIFY_STOP_MASK)
1455 hlist_for_each_entry(child, &core->children, child_node) {
1456 /* Skip children who will be reparented to another clock */
1457 if (child->new_parent && child->new_parent != core)
1459 tmp_clk = clk_propagate_rate_change(child, event);
1464 /* handle the new child who might not be in core->children yet */
1465 if (core->new_child) {
1466 tmp_clk = clk_propagate_rate_change(core->new_child, event);
1475 * walk down a subtree and set the new rates notifying the rate
1478 static void clk_change_rate(struct clk_core *core)
1480 struct clk_core *child;
1481 struct hlist_node *tmp;
1482 unsigned long old_rate;
1483 unsigned long best_parent_rate = 0;
1484 bool skip_set_rate = false;
1485 struct clk_core *old_parent;
1486 struct clk_core *parent = NULL;
1488 old_rate = core->rate;
1490 if (core->new_parent) {
1491 parent = core->new_parent;
1492 best_parent_rate = core->new_parent->rate;
1493 } else if (core->parent) {
1494 parent = core->parent;
1495 best_parent_rate = core->parent->rate;
1498 if (core->flags & CLK_SET_RATE_UNGATE) {
1499 unsigned long flags;
1501 clk_core_prepare(core);
1502 flags = clk_enable_lock();
1503 clk_core_enable(core);
1504 clk_enable_unlock(flags);
1507 if (core->new_parent && core->new_parent != core->parent) {
1508 old_parent = __clk_set_parent_before(core, core->new_parent);
1509 trace_clk_set_parent(core, core->new_parent);
1511 if (core->ops->set_rate_and_parent) {
1512 skip_set_rate = true;
1513 core->ops->set_rate_and_parent(core->hw, core->new_rate,
1515 core->new_parent_index);
1516 } else if (core->ops->set_parent) {
1517 core->ops->set_parent(core->hw, core->new_parent_index);
1520 trace_clk_set_parent_complete(core, core->new_parent);
1521 __clk_set_parent_after(core, core->new_parent, old_parent);
1524 if (core->flags & CLK_OPS_PARENT_ENABLE)
1525 clk_core_prepare_enable(parent);
1527 trace_clk_set_rate(core, core->new_rate);
1529 if (!skip_set_rate && core->ops->set_rate)
1530 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
1532 trace_clk_set_rate_complete(core, core->new_rate);
1534 core->rate = clk_recalc(core, best_parent_rate);
1536 if (core->flags & CLK_SET_RATE_UNGATE) {
1537 unsigned long flags;
1539 flags = clk_enable_lock();
1540 clk_core_disable(core);
1541 clk_enable_unlock(flags);
1542 clk_core_unprepare(core);
1545 if (core->flags & CLK_OPS_PARENT_ENABLE)
1546 clk_core_disable_unprepare(parent);
1548 if (core->notifier_count && old_rate != core->rate)
1549 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
1551 if (core->flags & CLK_RECALC_NEW_RATES)
1552 (void)clk_calc_new_rates(core, core->new_rate);
1555 * Use safe iteration, as change_rate can actually swap parents
1556 * for certain clock types.
1558 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
1559 /* Skip children who will be reparented to another clock */
1560 if (child->new_parent && child->new_parent != core)
1562 clk_change_rate(child);
1565 /* handle the new child who might not be in core->children yet */
1566 if (core->new_child)
1567 clk_change_rate(core->new_child);
1570 static int clk_core_set_rate_nolock(struct clk_core *core,
1571 unsigned long req_rate)
1573 struct clk_core *top, *fail_clk;
1574 unsigned long rate = req_rate;
1579 /* bail early if nothing to do */
1580 if (rate == clk_core_get_rate_nolock(core))
1583 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
1586 /* calculate new rates and get the topmost changed clock */
1587 top = clk_calc_new_rates(core, rate);
1591 /* notify that we are about to change rates */
1592 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1594 pr_debug("%s: failed to set %s rate\n", __func__,
1596 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1600 /* change the rates */
1601 clk_change_rate(top);
1603 core->req_rate = req_rate;
1609 * clk_set_rate - specify a new rate for clk
1610 * @clk: the clk whose rate is being changed
1611 * @rate: the new rate for clk
1613 * In the simplest case clk_set_rate will only adjust the rate of clk.
1615 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1616 * propagate up to clk's parent; whether or not this happens depends on the
1617 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1618 * after calling .round_rate then upstream parent propagation is ignored. If
1619 * *parent_rate comes back with a new rate for clk's parent then we propagate
1620 * up to clk's parent and set its rate. Upward propagation will continue
1621 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1622 * .round_rate stops requesting changes to clk's parent_rate.
1624 * Rate changes are accomplished via tree traversal that also recalculates the
1625 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
1627 * Returns 0 on success, -EERROR otherwise.
1629 int clk_set_rate(struct clk *clk, unsigned long rate)
1636 /* prevent racing with updates to the clock topology */
1639 ret = clk_core_set_rate_nolock(clk->core, rate);
1641 clk_prepare_unlock();
1645 EXPORT_SYMBOL_GPL(clk_set_rate);
1648 * clk_set_rate_range - set a rate range for a clock source
1649 * @clk: clock source
1650 * @min: desired minimum clock rate in Hz, inclusive
1651 * @max: desired maximum clock rate in Hz, inclusive
1653 * Returns success (0) or negative errno.
1655 int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
1663 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
1664 __func__, clk->core->name, clk->dev_id, clk->con_id,
1671 if (min != clk->min_rate || max != clk->max_rate) {
1672 clk->min_rate = min;
1673 clk->max_rate = max;
1674 ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
1677 clk_prepare_unlock();
1681 EXPORT_SYMBOL_GPL(clk_set_rate_range);
1684 * clk_set_min_rate - set a minimum clock rate for a clock source
1685 * @clk: clock source
1686 * @rate: desired minimum clock rate in Hz, inclusive
1688 * Returns success (0) or negative errno.
1690 int clk_set_min_rate(struct clk *clk, unsigned long rate)
1695 return clk_set_rate_range(clk, rate, clk->max_rate);
1697 EXPORT_SYMBOL_GPL(clk_set_min_rate);
1700 * clk_set_max_rate - set a maximum clock rate for a clock source
1701 * @clk: clock source
1702 * @rate: desired maximum clock rate in Hz, inclusive
1704 * Returns success (0) or negative errno.
1706 int clk_set_max_rate(struct clk *clk, unsigned long rate)
1711 return clk_set_rate_range(clk, clk->min_rate, rate);
1713 EXPORT_SYMBOL_GPL(clk_set_max_rate);
1716 * clk_get_parent - return the parent of a clk
1717 * @clk: the clk whose parent gets returned
1719 * Simply returns clk->parent. Returns NULL if clk is NULL.
1721 struct clk *clk_get_parent(struct clk *clk)
1729 /* TODO: Create a per-user clk and change callers to call clk_put */
1730 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
1731 clk_prepare_unlock();
1735 EXPORT_SYMBOL_GPL(clk_get_parent);
1737 static struct clk_core *__clk_init_parent(struct clk_core *core)
1741 if (core->num_parents > 1 && core->ops->get_parent)
1742 index = core->ops->get_parent(core->hw);
1744 return clk_core_get_parent_by_index(core, index);
1747 static void clk_core_reparent(struct clk_core *core,
1748 struct clk_core *new_parent)
1750 clk_reparent(core, new_parent);
1751 __clk_recalc_accuracies(core);
1752 __clk_recalc_rates(core, POST_RATE_CHANGE);
1755 void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
1760 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
1764 * clk_has_parent - check if a clock is a possible parent for another
1765 * @clk: clock source
1766 * @parent: parent clock source
1768 * This function can be used in drivers that need to check that a clock can be
1769 * the parent of another without actually changing the parent.
1771 * Returns true if @parent is a possible parent for @clk, false otherwise.
1773 bool clk_has_parent(struct clk *clk, struct clk *parent)
1775 struct clk_core *core, *parent_core;
1778 /* NULL clocks should be nops, so return success if either is NULL. */
1779 if (!clk || !parent)
1783 parent_core = parent->core;
1785 /* Optimize for the case where the parent is already the parent. */
1786 if (core->parent == parent_core)
1789 for (i = 0; i < core->num_parents; i++)
1790 if (strcmp(core->parent_names[i], parent_core->name) == 0)
1795 EXPORT_SYMBOL_GPL(clk_has_parent);
1797 static int clk_core_set_parent(struct clk_core *core, struct clk_core *parent)
1801 unsigned long p_rate = 0;
1806 /* prevent racing with updates to the clock topology */
1809 if (core->parent == parent)
1812 /* verify ops for for multi-parent clks */
1813 if ((core->num_parents > 1) && (!core->ops->set_parent)) {
1818 /* check that we are allowed to re-parent if the clock is in use */
1819 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
1824 /* try finding the new parent index */
1826 p_index = clk_fetch_parent_index(core, parent);
1828 pr_debug("%s: clk %s can not be parent of clk %s\n",
1829 __func__, parent->name, core->name);
1833 p_rate = parent->rate;
1836 /* propagate PRE_RATE_CHANGE notifications */
1837 ret = __clk_speculate_rates(core, p_rate);
1839 /* abort if a driver objects */
1840 if (ret & NOTIFY_STOP_MASK)
1843 /* do the re-parent */
1844 ret = __clk_set_parent(core, parent, p_index);
1846 /* propagate rate an accuracy recalculation accordingly */
1848 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
1850 __clk_recalc_rates(core, POST_RATE_CHANGE);
1851 __clk_recalc_accuracies(core);
1855 clk_prepare_unlock();
1861 * clk_set_parent - switch the parent of a mux clk
1862 * @clk: the mux clk whose input we are switching
1863 * @parent: the new input to clk
1865 * Re-parent clk to use parent as its new input source. If clk is in
1866 * prepared state, the clk will get enabled for the duration of this call. If
1867 * that's not acceptable for a specific clk (Eg: the consumer can't handle
1868 * that, the reparenting is glitchy in hardware, etc), use the
1869 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
1871 * After successfully changing clk's parent clk_set_parent will update the
1872 * clk topology, sysfs topology and propagate rate recalculation via
1873 * __clk_recalc_rates.
1875 * Returns 0 on success, -EERROR otherwise.
1877 int clk_set_parent(struct clk *clk, struct clk *parent)
1882 return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
1884 EXPORT_SYMBOL_GPL(clk_set_parent);
1887 * clk_set_phase - adjust the phase shift of a clock signal
1888 * @clk: clock signal source
1889 * @degrees: number of degrees the signal is shifted
1891 * Shifts the phase of a clock signal by the specified
1892 * degrees. Returns 0 on success, -EERROR otherwise.
1894 * This function makes no distinction about the input or reference
1895 * signal that we adjust the clock signal phase against. For example
1896 * phase locked-loop clock signal generators we may shift phase with
1897 * respect to feedback clock signal input, but for other cases the
1898 * clock phase may be shifted with respect to some other, unspecified
1901 * Additionally the concept of phase shift does not propagate through
1902 * the clock tree hierarchy, which sets it apart from clock rates and
1903 * clock accuracy. A parent clock phase attribute does not have an
1904 * impact on the phase attribute of a child clock.
1906 int clk_set_phase(struct clk *clk, int degrees)
1913 /* sanity check degrees */
1920 trace_clk_set_phase(clk->core, degrees);
1922 if (clk->core->ops->set_phase)
1923 ret = clk->core->ops->set_phase(clk->core->hw, degrees);
1925 trace_clk_set_phase_complete(clk->core, degrees);
1928 clk->core->phase = degrees;
1930 clk_prepare_unlock();
1934 EXPORT_SYMBOL_GPL(clk_set_phase);
1936 static int clk_core_get_phase(struct clk_core *core)
1941 /* Always try to update cached phase if possible */
1942 if (core->ops->get_phase)
1943 core->phase = core->ops->get_phase(core->hw);
1945 clk_prepare_unlock();
1951 * clk_get_phase - return the phase shift of a clock signal
1952 * @clk: clock signal source
1954 * Returns the phase shift of a clock node in degrees, otherwise returns
1957 int clk_get_phase(struct clk *clk)
1962 return clk_core_get_phase(clk->core);
1964 EXPORT_SYMBOL_GPL(clk_get_phase);
1967 * clk_is_match - check if two clk's point to the same hardware clock
1968 * @p: clk compared against q
1969 * @q: clk compared against p
1971 * Returns true if the two struct clk pointers both point to the same hardware
1972 * clock node. Put differently, returns true if struct clk *p and struct clk *q
1973 * share the same struct clk_core object.
1975 * Returns false otherwise. Note that two NULL clks are treated as matching.
1977 bool clk_is_match(const struct clk *p, const struct clk *q)
1979 /* trivial case: identical struct clk's or both NULL */
1983 /* true if clk->core pointers match. Avoid dereferencing garbage */
1984 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
1985 if (p->core == q->core)
1990 EXPORT_SYMBOL_GPL(clk_is_match);
1992 /*** debugfs support ***/
1994 #ifdef CONFIG_DEBUG_FS
1995 #include <linux/debugfs.h>
1997 static struct dentry *rootdir;
1998 static int inited = 0;
1999 static DEFINE_MUTEX(clk_debug_lock);
2000 static HLIST_HEAD(clk_debug_list);
2002 static struct hlist_head *orphan_list[] = {
2007 static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2013 seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
2015 30 - level * 3, c->name,
2016 c->enable_count, c->prepare_count, clk_core_get_rate(c),
2017 clk_core_get_accuracy(c), clk_core_get_phase(c));
2020 static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2023 struct clk_core *child;
2028 clk_summary_show_one(s, c, level);
2030 hlist_for_each_entry(child, &c->children, child_node)
2031 clk_summary_show_subtree(s, child, level + 1);
2034 static int clk_summary_show(struct seq_file *s, void *data)
2037 struct hlist_head **lists = (struct hlist_head **)s->private;
2039 seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
2040 seq_puts(s, "----------------------------------------------------------------------------------------\n");
2044 for (; *lists; lists++)
2045 hlist_for_each_entry(c, *lists, child_node)
2046 clk_summary_show_subtree(s, c, 0);
2048 clk_prepare_unlock();
2054 static int clk_summary_open(struct inode *inode, struct file *file)
2056 return single_open(file, clk_summary_show, inode->i_private);
2059 static const struct file_operations clk_summary_fops = {
2060 .open = clk_summary_open,
2062 .llseek = seq_lseek,
2063 .release = single_release,
2066 static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2071 /* This should be JSON format, i.e. elements separated with a comma */
2072 seq_printf(s, "\"%s\": { ", c->name);
2073 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2074 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
2075 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2076 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
2077 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
2080 static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2082 struct clk_core *child;
2087 clk_dump_one(s, c, level);
2089 hlist_for_each_entry(child, &c->children, child_node) {
2091 clk_dump_subtree(s, child, level + 1);
2097 static int clk_dump(struct seq_file *s, void *data)
2100 bool first_node = true;
2101 struct hlist_head **lists = (struct hlist_head **)s->private;
2106 for (; *lists; lists++) {
2107 hlist_for_each_entry(c, *lists, child_node) {
2111 clk_dump_subtree(s, c, 0);
2115 clk_prepare_unlock();
2122 static int clk_dump_open(struct inode *inode, struct file *file)
2124 return single_open(file, clk_dump, inode->i_private);
2127 static const struct file_operations clk_dump_fops = {
2128 .open = clk_dump_open,
2130 .llseek = seq_lseek,
2131 .release = single_release,
2134 static int possible_parents_dump(struct seq_file *s, void *data)
2136 struct clk_core *core = s->private;
2139 for (i = 0; i < core->num_parents - 1; i++)
2140 seq_printf(s, "%s ", core->parent_names[i]);
2142 seq_printf(s, "%s\n", core->parent_names[i]);
2147 static int possible_parents_open(struct inode *inode, struct file *file)
2149 return single_open(file, possible_parents_dump, inode->i_private);
2152 static const struct file_operations possible_parents_fops = {
2153 .open = possible_parents_open,
2155 .llseek = seq_lseek,
2156 .release = single_release,
2159 static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2164 if (!core || !pdentry) {
2169 d = debugfs_create_dir(core->name, pdentry);
2175 d = debugfs_create_ulong("clk_rate", 0444, core->dentry, &core->rate);
2179 d = debugfs_create_ulong("clk_accuracy", 0444, core->dentry,
2184 d = debugfs_create_u32("clk_phase", 0444, core->dentry, &core->phase);
2188 d = debugfs_create_x32("clk_flags", S_IRUGO, core->dentry,
2189 (u32 *)&core->flags);
2193 d = debugfs_create_u32("clk_prepare_count", 0444, core->dentry,
2194 &core->prepare_count);
2198 d = debugfs_create_u32("clk_enable_count", 0444, core->dentry,
2199 &core->enable_count);
2203 d = debugfs_create_u32("clk_notifier_count", 0444, core->dentry,
2204 &core->notifier_count);
2208 if (core->num_parents > 1) {
2209 d = debugfs_create_file("clk_possible_parents", 0444,
2210 core->dentry, core, &possible_parents_fops);
2215 if (core->ops->debug_init) {
2216 ret = core->ops->debug_init(core->hw, core->dentry);
2225 debugfs_remove_recursive(core->dentry);
2226 core->dentry = NULL;
2232 * clk_debug_register - add a clk node to the debugfs clk directory
2233 * @core: the clk being added to the debugfs clk directory
2235 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2236 * initialized. Otherwise it bails out early since the debugfs clk directory
2237 * will be created lazily by clk_debug_init as part of a late_initcall.
2239 static int clk_debug_register(struct clk_core *core)
2243 mutex_lock(&clk_debug_lock);
2244 hlist_add_head(&core->debug_node, &clk_debug_list);
2249 ret = clk_debug_create_one(core, rootdir);
2251 mutex_unlock(&clk_debug_lock);
2257 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2258 * @core: the clk being removed from the debugfs clk directory
2260 * Dynamically removes a clk and all its child nodes from the
2261 * debugfs clk directory if clk->dentry points to debugfs created by
2262 * clk_debug_register in __clk_core_init.
2264 static void clk_debug_unregister(struct clk_core *core)
2266 mutex_lock(&clk_debug_lock);
2267 hlist_del_init(&core->debug_node);
2268 debugfs_remove_recursive(core->dentry);
2269 core->dentry = NULL;
2270 mutex_unlock(&clk_debug_lock);
2273 struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
2274 void *data, const struct file_operations *fops)
2276 struct dentry *d = NULL;
2278 if (hw->core->dentry)
2279 d = debugfs_create_file(name, mode, hw->core->dentry, data,
2284 EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
2287 * clk_debug_init - lazily populate the debugfs clk directory
2289 * clks are often initialized very early during boot before memory can be
2290 * dynamically allocated and well before debugfs is setup. This function
2291 * populates the debugfs clk directory once at boot-time when we know that
2292 * debugfs is setup. It should only be called once at boot-time, all other clks
2293 * added dynamically will be done so with clk_debug_register.
2295 static int __init clk_debug_init(void)
2297 struct clk_core *core;
2300 rootdir = debugfs_create_dir("clk", NULL);
2305 d = debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
2310 d = debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
2315 d = debugfs_create_file("clk_orphan_summary", 0444, rootdir,
2316 &orphan_list, &clk_summary_fops);
2320 d = debugfs_create_file("clk_orphan_dump", 0444, rootdir,
2321 &orphan_list, &clk_dump_fops);
2325 mutex_lock(&clk_debug_lock);
2326 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2327 clk_debug_create_one(core, rootdir);
2330 mutex_unlock(&clk_debug_lock);
2334 late_initcall(clk_debug_init);
2336 static inline int clk_debug_register(struct clk_core *core) { return 0; }
2337 static inline void clk_debug_reparent(struct clk_core *core,
2338 struct clk_core *new_parent)
2341 static inline void clk_debug_unregister(struct clk_core *core)
2347 * __clk_core_init - initialize the data structures in a struct clk_core
2348 * @core: clk_core being initialized
2350 * Initializes the lists in struct clk_core, queries the hardware for the
2351 * parent and rate and sets them both.
2353 static int __clk_core_init(struct clk_core *core)
2356 struct clk_core *orphan;
2357 struct hlist_node *tmp2;
2365 /* check to see if a clock with this name is already registered */
2366 if (clk_core_lookup(core->name)) {
2367 pr_debug("%s: clk %s already initialized\n",
2368 __func__, core->name);
2373 /* check that clk_ops are sane. See Documentation/clk.txt */
2374 if (core->ops->set_rate &&
2375 !((core->ops->round_rate || core->ops->determine_rate) &&
2376 core->ops->recalc_rate)) {
2377 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
2378 __func__, core->name);
2383 if (core->ops->set_parent && !core->ops->get_parent) {
2384 pr_err("%s: %s must implement .get_parent & .set_parent\n",
2385 __func__, core->name);
2390 if (core->num_parents > 1 && !core->ops->get_parent) {
2391 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
2392 __func__, core->name);
2397 if (core->ops->set_rate_and_parent &&
2398 !(core->ops->set_parent && core->ops->set_rate)) {
2399 pr_err("%s: %s must implement .set_parent & .set_rate\n",
2400 __func__, core->name);
2405 /* throw a WARN if any entries in parent_names are NULL */
2406 for (i = 0; i < core->num_parents; i++)
2407 WARN(!core->parent_names[i],
2408 "%s: invalid NULL in %s's .parent_names\n",
2409 __func__, core->name);
2411 core->parent = __clk_init_parent(core);
2414 * Populate core->parent if parent has already been clk_core_init'd. If
2415 * parent has not yet been clk_core_init'd then place clk in the orphan
2416 * list. If clk doesn't have any parents then place it in the root
2419 * Every time a new clk is clk_init'd then we walk the list of orphan
2420 * clocks and re-parent any that are children of the clock currently
2424 hlist_add_head(&core->child_node,
2425 &core->parent->children);
2426 core->orphan = core->parent->orphan;
2427 } else if (!core->num_parents) {
2428 hlist_add_head(&core->child_node, &clk_root_list);
2429 core->orphan = false;
2431 hlist_add_head(&core->child_node, &clk_orphan_list);
2432 core->orphan = true;
2436 * Set clk's accuracy. The preferred method is to use
2437 * .recalc_accuracy. For simple clocks and lazy developers the default
2438 * fallback is to use the parent's accuracy. If a clock doesn't have a
2439 * parent (or is orphaned) then accuracy is set to zero (perfect
2442 if (core->ops->recalc_accuracy)
2443 core->accuracy = core->ops->recalc_accuracy(core->hw,
2444 __clk_get_accuracy(core->parent));
2445 else if (core->parent)
2446 core->accuracy = core->parent->accuracy;
2452 * Since a phase is by definition relative to its parent, just
2453 * query the current clock phase, or just assume it's in phase.
2455 if (core->ops->get_phase)
2456 core->phase = core->ops->get_phase(core->hw);
2461 * Set clk's rate. The preferred method is to use .recalc_rate. For
2462 * simple clocks and lazy developers the default fallback is to use the
2463 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2464 * then rate is set to zero.
2466 if (core->ops->recalc_rate)
2467 rate = core->ops->recalc_rate(core->hw,
2468 clk_core_get_rate_nolock(core->parent));
2469 else if (core->parent)
2470 rate = core->parent->rate;
2473 core->rate = core->req_rate = rate;
2476 * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
2477 * don't get accidentally disabled when walking the orphan tree and
2478 * reparenting clocks
2480 if (core->flags & CLK_IS_CRITICAL) {
2481 unsigned long flags;
2483 ret = clk_core_prepare(core);
2487 flags = clk_enable_lock();
2488 ret = clk_core_enable(core);
2489 clk_enable_unlock(flags);
2491 clk_core_unprepare(core);
2497 * walk the list of orphan clocks and reparent any that newly finds a
2500 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
2501 struct clk_core *parent = __clk_init_parent(orphan);
2504 * We need to use __clk_set_parent_before() and _after() to
2505 * to properly migrate any prepare/enable count of the orphan
2506 * clock. This is important for CLK_IS_CRITICAL clocks, which
2507 * are enabled during init but might not have a parent yet.
2510 /* update the clk tree topology */
2511 __clk_set_parent_before(orphan, parent);
2512 __clk_set_parent_after(orphan, parent, NULL);
2513 __clk_recalc_accuracies(orphan);
2514 __clk_recalc_rates(orphan, 0);
2519 * optional platform-specific magic
2521 * The .init callback is not used by any of the basic clock types, but
2522 * exists for weird hardware that must perform initialization magic.
2523 * Please consider other ways of solving initialization problems before
2524 * using this callback, as its use is discouraged.
2526 if (core->ops->init)
2527 core->ops->init(core->hw);
2529 kref_init(&core->ref);
2531 clk_prepare_unlock();
2534 clk_debug_register(core);
2539 struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2544 /* This is to allow this function to be chained to others */
2545 if (IS_ERR_OR_NULL(hw))
2546 return ERR_CAST(hw);
2548 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2550 return ERR_PTR(-ENOMEM);
2552 clk->core = hw->core;
2553 clk->dev_id = dev_id;
2554 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
2555 clk->max_rate = ULONG_MAX;
2558 hlist_add_head(&clk->clks_node, &hw->core->clks);
2559 clk_prepare_unlock();
2564 /* keep in sync with __clk_put */
2565 void __clk_free_clk(struct clk *clk)
2568 hlist_del(&clk->clks_node);
2569 clk_prepare_unlock();
2571 kfree_const(clk->con_id);
2576 * clk_register - allocate a new clock, register it and return an opaque cookie
2577 * @dev: device that is registering this clock
2578 * @hw: link to hardware-specific clock data
2580 * clk_register is the primary interface for populating the clock tree with new
2581 * clock nodes. It returns a pointer to the newly allocated struct clk which
2582 * cannot be dereferenced by driver code but may be used in conjunction with the
2583 * rest of the clock API. In the event of an error clk_register will return an
2584 * error code; drivers must test for an error code after calling clk_register.
2586 struct clk *clk_register(struct device *dev, struct clk_hw *hw)
2589 struct clk_core *core;
2591 core = kzalloc(sizeof(*core), GFP_KERNEL);
2597 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
2602 core->ops = hw->init->ops;
2603 if (dev && dev->driver)
2604 core->owner = dev->driver->owner;
2606 core->flags = hw->init->flags;
2607 core->num_parents = hw->init->num_parents;
2609 core->max_rate = ULONG_MAX;
2612 /* allocate local copy in case parent_names is __initdata */
2613 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
2616 if (!core->parent_names) {
2618 goto fail_parent_names;
2622 /* copy each string name in case parent_names is __initdata */
2623 for (i = 0; i < core->num_parents; i++) {
2624 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
2626 if (!core->parent_names[i]) {
2628 goto fail_parent_names_copy;
2632 /* avoid unnecessary string look-ups of clk_core's possible parents. */
2633 core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
2635 if (!core->parents) {
2640 INIT_HLIST_HEAD(&core->clks);
2642 hw->clk = __clk_create_clk(hw, NULL, NULL);
2643 if (IS_ERR(hw->clk)) {
2644 ret = PTR_ERR(hw->clk);
2648 ret = __clk_core_init(core);
2652 __clk_free_clk(hw->clk);
2656 kfree(core->parents);
2657 fail_parent_names_copy:
2659 kfree_const(core->parent_names[i]);
2660 kfree(core->parent_names);
2662 kfree_const(core->name);
2666 return ERR_PTR(ret);
2668 EXPORT_SYMBOL_GPL(clk_register);
2671 * clk_hw_register - register a clk_hw and return an error code
2672 * @dev: device that is registering this clock
2673 * @hw: link to hardware-specific clock data
2675 * clk_hw_register is the primary interface for populating the clock tree with
2676 * new clock nodes. It returns an integer equal to zero indicating success or
2677 * less than zero indicating failure. Drivers must test for an error code after
2678 * calling clk_hw_register().
2680 int clk_hw_register(struct device *dev, struct clk_hw *hw)
2682 return PTR_ERR_OR_ZERO(clk_register(dev, hw));
2684 EXPORT_SYMBOL_GPL(clk_hw_register);
2686 /* Free memory allocated for a clock. */
2687 static void __clk_release(struct kref *ref)
2689 struct clk_core *core = container_of(ref, struct clk_core, ref);
2690 int i = core->num_parents;
2692 lockdep_assert_held(&prepare_lock);
2694 kfree(core->parents);
2696 kfree_const(core->parent_names[i]);
2698 kfree(core->parent_names);
2699 kfree_const(core->name);
2704 * Empty clk_ops for unregistered clocks. These are used temporarily
2705 * after clk_unregister() was called on a clock and until last clock
2706 * consumer calls clk_put() and the struct clk object is freed.
2708 static int clk_nodrv_prepare_enable(struct clk_hw *hw)
2713 static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
2718 static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
2719 unsigned long parent_rate)
2724 static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
2729 static const struct clk_ops clk_nodrv_ops = {
2730 .enable = clk_nodrv_prepare_enable,
2731 .disable = clk_nodrv_disable_unprepare,
2732 .prepare = clk_nodrv_prepare_enable,
2733 .unprepare = clk_nodrv_disable_unprepare,
2734 .set_rate = clk_nodrv_set_rate,
2735 .set_parent = clk_nodrv_set_parent,
2738 static void clk_core_evict_parent_cache_subtree(struct clk_core *root,
2739 struct clk_core *target)
2742 struct clk_core *child;
2744 for (i = 0; i < root->num_parents; i++)
2745 if (root->parents[i] == target)
2746 root->parents[i] = NULL;
2748 hlist_for_each_entry(child, &root->children, child_node)
2749 clk_core_evict_parent_cache_subtree(child, target);
2752 /* Remove this clk from all parent caches */
2753 static void clk_core_evict_parent_cache(struct clk_core *core)
2755 struct hlist_head **lists;
2756 struct clk_core *root;
2758 lockdep_assert_held(&prepare_lock);
2760 for (lists = all_lists; *lists; lists++)
2761 hlist_for_each_entry(root, *lists, child_node)
2762 clk_core_evict_parent_cache_subtree(root, core);
2767 * clk_unregister - unregister a currently registered clock
2768 * @clk: clock to unregister
2770 void clk_unregister(struct clk *clk)
2772 unsigned long flags;
2774 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2777 clk_debug_unregister(clk->core);
2781 if (clk->core->ops == &clk_nodrv_ops) {
2782 pr_err("%s: unregistered clock: %s\n", __func__,
2787 * Assign empty clock ops for consumers that might still hold
2788 * a reference to this clock.
2790 flags = clk_enable_lock();
2791 clk->core->ops = &clk_nodrv_ops;
2792 clk_enable_unlock(flags);
2794 if (!hlist_empty(&clk->core->children)) {
2795 struct clk_core *child;
2796 struct hlist_node *t;
2798 /* Reparent all children to the orphan list. */
2799 hlist_for_each_entry_safe(child, t, &clk->core->children,
2801 clk_core_set_parent(child, NULL);
2804 clk_core_evict_parent_cache(clk->core);
2806 hlist_del_init(&clk->core->child_node);
2808 if (clk->core->prepare_count)
2809 pr_warn("%s: unregistering prepared clock: %s\n",
2810 __func__, clk->core->name);
2811 kref_put(&clk->core->ref, __clk_release);
2813 clk_prepare_unlock();
2815 EXPORT_SYMBOL_GPL(clk_unregister);
2818 * clk_hw_unregister - unregister a currently registered clk_hw
2819 * @hw: hardware-specific clock data to unregister
2821 void clk_hw_unregister(struct clk_hw *hw)
2823 clk_unregister(hw->clk);
2825 EXPORT_SYMBOL_GPL(clk_hw_unregister);
2827 static void devm_clk_release(struct device *dev, void *res)
2829 clk_unregister(*(struct clk **)res);
2832 static void devm_clk_hw_release(struct device *dev, void *res)
2834 clk_hw_unregister(*(struct clk_hw **)res);
2838 * devm_clk_register - resource managed clk_register()
2839 * @dev: device that is registering this clock
2840 * @hw: link to hardware-specific clock data
2842 * Managed clk_register(). Clocks returned from this function are
2843 * automatically clk_unregister()ed on driver detach. See clk_register() for
2846 struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
2851 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
2853 return ERR_PTR(-ENOMEM);
2855 clk = clk_register(dev, hw);
2858 devres_add(dev, clkp);
2865 EXPORT_SYMBOL_GPL(devm_clk_register);
2868 * devm_clk_hw_register - resource managed clk_hw_register()
2869 * @dev: device that is registering this clock
2870 * @hw: link to hardware-specific clock data
2872 * Managed clk_hw_register(). Clocks registered by this function are
2873 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
2874 * for more information.
2876 int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
2878 struct clk_hw **hwp;
2881 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
2885 ret = clk_hw_register(dev, hw);
2888 devres_add(dev, hwp);
2895 EXPORT_SYMBOL_GPL(devm_clk_hw_register);
2897 static int devm_clk_match(struct device *dev, void *res, void *data)
2899 struct clk *c = res;
2905 static int devm_clk_hw_match(struct device *dev, void *res, void *data)
2907 struct clk_hw *hw = res;
2915 * devm_clk_unregister - resource managed clk_unregister()
2916 * @clk: clock to unregister
2918 * Deallocate a clock allocated with devm_clk_register(). Normally
2919 * this function will not need to be called and the resource management
2920 * code will ensure that the resource is freed.
2922 void devm_clk_unregister(struct device *dev, struct clk *clk)
2924 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
2926 EXPORT_SYMBOL_GPL(devm_clk_unregister);
2929 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
2930 * @dev: device that is unregistering the hardware-specific clock data
2931 * @hw: link to hardware-specific clock data
2933 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
2934 * this function will not need to be called and the resource management
2935 * code will ensure that the resource is freed.
2937 void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
2939 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
2942 EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
2947 int __clk_get(struct clk *clk)
2949 struct clk_core *core = !clk ? NULL : clk->core;
2952 if (!try_module_get(core->owner))
2955 kref_get(&core->ref);
2960 /* keep in sync with __clk_free_clk */
2961 void __clk_put(struct clk *clk)
2963 struct module *owner;
2965 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2970 hlist_del(&clk->clks_node);
2971 if (clk->min_rate > clk->core->req_rate ||
2972 clk->max_rate < clk->core->req_rate)
2973 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
2975 owner = clk->core->owner;
2976 kref_put(&clk->core->ref, __clk_release);
2978 clk_prepare_unlock();
2982 kfree_const(clk->con_id);
2986 /*** clk rate change notifiers ***/
2989 * clk_notifier_register - add a clk rate change notifier
2990 * @clk: struct clk * to watch
2991 * @nb: struct notifier_block * with callback info
2993 * Request notification when clk's rate changes. This uses an SRCU
2994 * notifier because we want it to block and notifier unregistrations are
2995 * uncommon. The callbacks associated with the notifier must not
2996 * re-enter into the clk framework by calling any top-level clk APIs;
2997 * this will cause a nested prepare_lock mutex.
2999 * In all notification cases (pre, post and abort rate change) the original
3000 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
3001 * and the new frequency is passed via struct clk_notifier_data.new_rate.
3003 * clk_notifier_register() must be called from non-atomic context.
3004 * Returns -EINVAL if called with null arguments, -ENOMEM upon
3005 * allocation failure; otherwise, passes along the return value of
3006 * srcu_notifier_chain_register().
3008 int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
3010 struct clk_notifier *cn;
3018 /* search the list of notifiers for this clk */
3019 list_for_each_entry(cn, &clk_notifier_list, node)
3023 /* if clk wasn't in the notifier list, allocate new clk_notifier */
3024 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
3029 srcu_init_notifier_head(&cn->notifier_head);
3031 list_add(&cn->node, &clk_notifier_list);
3034 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
3036 clk->core->notifier_count++;
3039 clk_prepare_unlock();
3043 EXPORT_SYMBOL_GPL(clk_notifier_register);
3046 * clk_notifier_unregister - remove a clk rate change notifier
3047 * @clk: struct clk *
3048 * @nb: struct notifier_block * with callback info
3050 * Request no further notification for changes to 'clk' and frees memory
3051 * allocated in clk_notifier_register.
3053 * Returns -EINVAL if called with null arguments; otherwise, passes
3054 * along the return value of srcu_notifier_chain_unregister().
3056 int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
3058 struct clk_notifier *cn;
3066 list_for_each_entry(cn, &clk_notifier_list, node) {
3067 if (cn->clk == clk) {
3068 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
3070 clk->core->notifier_count--;
3072 /* XXX the notifier code should handle this better */
3073 if (!cn->notifier_head.head) {
3074 srcu_cleanup_notifier_head(&cn->notifier_head);
3075 list_del(&cn->node);
3082 clk_prepare_unlock();
3086 EXPORT_SYMBOL_GPL(clk_notifier_unregister);
3090 * struct of_clk_provider - Clock provider registration structure
3091 * @link: Entry in global list of clock providers
3092 * @node: Pointer to device tree node of clock provider
3093 * @get: Get clock callback. Returns NULL or a struct clk for the
3094 * given clock specifier
3095 * @data: context pointer to be passed into @get callback
3097 struct of_clk_provider {
3098 struct list_head link;
3100 struct device_node *node;
3101 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
3102 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
3106 static const struct of_device_id __clk_of_table_sentinel
3107 __used __section(__clk_of_table_end);
3109 static LIST_HEAD(of_clk_providers);
3110 static DEFINE_MUTEX(of_clk_mutex);
3112 struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
3117 EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
3119 struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
3123 EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
3125 struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
3127 struct clk_onecell_data *clk_data = data;
3128 unsigned int idx = clkspec->args[0];
3130 if (idx >= clk_data->clk_num) {
3131 pr_err("%s: invalid clock index %u\n", __func__, idx);
3132 return ERR_PTR(-EINVAL);
3135 return clk_data->clks[idx];
3137 EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
3140 of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
3142 struct clk_hw_onecell_data *hw_data = data;
3143 unsigned int idx = clkspec->args[0];
3145 if (idx >= hw_data->num) {
3146 pr_err("%s: invalid index %u\n", __func__, idx);
3147 return ERR_PTR(-EINVAL);
3150 return hw_data->hws[idx];
3152 EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
3155 * of_clk_add_provider() - Register a clock provider for a node
3156 * @np: Device node pointer associated with clock provider
3157 * @clk_src_get: callback for decoding clock
3158 * @data: context pointer for @clk_src_get callback.
3160 int of_clk_add_provider(struct device_node *np,
3161 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
3165 struct of_clk_provider *cp;
3168 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3172 cp->node = of_node_get(np);
3174 cp->get = clk_src_get;
3176 mutex_lock(&of_clk_mutex);
3177 list_add(&cp->link, &of_clk_providers);
3178 mutex_unlock(&of_clk_mutex);
3179 pr_debug("Added clock from %pOF\n", np);
3181 ret = of_clk_set_defaults(np, true);
3183 of_clk_del_provider(np);
3187 EXPORT_SYMBOL_GPL(of_clk_add_provider);
3190 * of_clk_add_hw_provider() - Register a clock provider for a node
3191 * @np: Device node pointer associated with clock provider
3192 * @get: callback for decoding clk_hw
3193 * @data: context pointer for @get callback.
3195 int of_clk_add_hw_provider(struct device_node *np,
3196 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3200 struct of_clk_provider *cp;
3203 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3207 cp->node = of_node_get(np);
3211 mutex_lock(&of_clk_mutex);
3212 list_add(&cp->link, &of_clk_providers);
3213 mutex_unlock(&of_clk_mutex);
3214 pr_debug("Added clk_hw provider from %pOF\n", np);
3216 ret = of_clk_set_defaults(np, true);
3218 of_clk_del_provider(np);
3222 EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
3225 * of_clk_del_provider() - Remove a previously registered clock provider
3226 * @np: Device node pointer associated with clock provider
3228 void of_clk_del_provider(struct device_node *np)
3230 struct of_clk_provider *cp;
3232 mutex_lock(&of_clk_mutex);
3233 list_for_each_entry(cp, &of_clk_providers, link) {
3234 if (cp->node == np) {
3235 list_del(&cp->link);
3236 of_node_put(cp->node);
3241 mutex_unlock(&of_clk_mutex);
3243 EXPORT_SYMBOL_GPL(of_clk_del_provider);
3245 static struct clk_hw *
3246 __of_clk_get_hw_from_provider(struct of_clk_provider *provider,
3247 struct of_phandle_args *clkspec)
3251 if (provider->get_hw)
3252 return provider->get_hw(clkspec, provider->data);
3254 clk = provider->get(clkspec, provider->data);
3256 return ERR_CAST(clk);
3257 return __clk_get_hw(clk);
3260 struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
3261 const char *dev_id, const char *con_id)
3263 struct of_clk_provider *provider;
3264 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
3268 return ERR_PTR(-EINVAL);
3270 /* Check if we have such a provider in our array */
3271 mutex_lock(&of_clk_mutex);
3272 list_for_each_entry(provider, &of_clk_providers, link) {
3273 if (provider->node == clkspec->np) {
3274 hw = __of_clk_get_hw_from_provider(provider, clkspec);
3275 clk = __clk_create_clk(hw, dev_id, con_id);
3279 if (!__clk_get(clk)) {
3280 __clk_free_clk(clk);
3281 clk = ERR_PTR(-ENOENT);
3287 mutex_unlock(&of_clk_mutex);
3293 * of_clk_get_from_provider() - Lookup a clock from a clock provider
3294 * @clkspec: pointer to a clock specifier data structure
3296 * This function looks up a struct clk from the registered list of clock
3297 * providers, an input is a clock specifier data structure as returned
3298 * from the of_parse_phandle_with_args() function call.
3300 struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
3302 return __of_clk_get_from_provider(clkspec, NULL, __func__);
3304 EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
3307 * of_clk_get_parent_count() - Count the number of clocks a device node has
3308 * @np: device node to count
3310 * Returns: The number of clocks that are possible parents of this node
3312 unsigned int of_clk_get_parent_count(struct device_node *np)
3316 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
3322 EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
3324 const char *of_clk_get_parent_name(struct device_node *np, int index)
3326 struct of_phandle_args clkspec;
3327 struct property *prop;
3328 const char *clk_name;
3335 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
3340 index = clkspec.args_count ? clkspec.args[0] : 0;
3343 /* if there is an indices property, use it to transfer the index
3344 * specified into an array offset for the clock-output-names property.
3346 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
3353 /* We went off the end of 'clock-indices' without finding it */
3357 if (of_property_read_string_index(clkspec.np, "clock-output-names",
3361 * Best effort to get the name if the clock has been
3362 * registered with the framework. If the clock isn't
3363 * registered, we return the node name as the name of
3364 * the clock as long as #clock-cells = 0.
3366 clk = of_clk_get_from_provider(&clkspec);
3368 if (clkspec.args_count == 0)
3369 clk_name = clkspec.np->name;
3373 clk_name = __clk_get_name(clk);
3379 of_node_put(clkspec.np);
3382 EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3385 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
3387 * @np: Device node pointer associated with clock provider
3388 * @parents: pointer to char array that hold the parents' names
3389 * @size: size of the @parents array
3391 * Return: number of parents for the clock node.
3393 int of_clk_parent_fill(struct device_node *np, const char **parents,
3398 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
3403 EXPORT_SYMBOL_GPL(of_clk_parent_fill);
3405 struct clock_provider {
3406 of_clk_init_cb_t clk_init_cb;
3407 struct device_node *np;
3408 struct list_head node;
3412 * This function looks for a parent clock. If there is one, then it
3413 * checks that the provider for this parent clock was initialized, in
3414 * this case the parent clock will be ready.
3416 static int parent_ready(struct device_node *np)
3421 struct clk *clk = of_clk_get(np, i);
3423 /* this parent is ready we can check the next one */
3430 /* at least one parent is not ready, we exit now */
3431 if (PTR_ERR(clk) == -EPROBE_DEFER)
3435 * Here we make assumption that the device tree is
3436 * written correctly. So an error means that there is
3437 * no more parent. As we didn't exit yet, then the
3438 * previous parent are ready. If there is no clock
3439 * parent, no need to wait for them, then we can
3440 * consider their absence as being ready
3447 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
3448 * @np: Device node pointer associated with clock provider
3449 * @index: clock index
3450 * @flags: pointer to clk_core->flags
3452 * Detects if the clock-critical property exists and, if so, sets the
3453 * corresponding CLK_IS_CRITICAL flag.
3455 * Do not use this function. It exists only for legacy Device Tree
3456 * bindings, such as the one-clock-per-node style that are outdated.
3457 * Those bindings typically put all clock data into .dts and the Linux
3458 * driver has no clock data, thus making it impossible to set this flag
3459 * correctly from the driver. Only those drivers may call
3460 * of_clk_detect_critical from their setup functions.
3462 * Return: error code or zero on success
3464 int of_clk_detect_critical(struct device_node *np,
3465 int index, unsigned long *flags)
3467 struct property *prop;
3474 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
3476 *flags |= CLK_IS_CRITICAL;
3482 * of_clk_init() - Scan and init clock providers from the DT
3483 * @matches: array of compatible values and init functions for providers.
3485 * This function scans the device tree for matching clock providers
3486 * and calls their initialization functions. It also does it by trying
3487 * to follow the dependencies.
3489 void __init of_clk_init(const struct of_device_id *matches)
3491 const struct of_device_id *match;
3492 struct device_node *np;
3493 struct clock_provider *clk_provider, *next;
3496 LIST_HEAD(clk_provider_list);
3499 matches = &__clk_of_table;
3501 /* First prepare the list of the clocks providers */
3502 for_each_matching_node_and_match(np, matches, &match) {
3503 struct clock_provider *parent;
3505 if (!of_device_is_available(np))
3508 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
3510 list_for_each_entry_safe(clk_provider, next,
3511 &clk_provider_list, node) {
3512 list_del(&clk_provider->node);
3513 of_node_put(clk_provider->np);
3514 kfree(clk_provider);
3520 parent->clk_init_cb = match->data;
3521 parent->np = of_node_get(np);
3522 list_add_tail(&parent->node, &clk_provider_list);
3525 while (!list_empty(&clk_provider_list)) {
3526 is_init_done = false;
3527 list_for_each_entry_safe(clk_provider, next,
3528 &clk_provider_list, node) {
3529 if (force || parent_ready(clk_provider->np)) {
3531 /* Don't populate platform devices */
3532 of_node_set_flag(clk_provider->np,
3535 clk_provider->clk_init_cb(clk_provider->np);
3536 of_clk_set_defaults(clk_provider->np, true);
3538 list_del(&clk_provider->node);
3539 of_node_put(clk_provider->np);
3540 kfree(clk_provider);
3541 is_init_done = true;
3546 * We didn't manage to initialize any of the
3547 * remaining providers during the last loop, so now we
3548 * initialize all the remaining ones unconditionally
3549 * in case the clock parent was not mandatory