1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
4 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
6 * Standard functionality for the common clock API. See Documentation/driver-api/clk.rst
10 #include <linux/clk-provider.h>
11 #include <linux/clk/clk-conf.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/spinlock.h>
15 #include <linux/err.h>
16 #include <linux/list.h>
17 #include <linux/slab.h>
19 #include <linux/device.h>
20 #include <linux/init.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/sched.h>
23 #include <linux/clkdev.h>
27 static DEFINE_SPINLOCK(enable_lock);
28 static DEFINE_MUTEX(prepare_lock);
30 static struct task_struct *prepare_owner;
31 static struct task_struct *enable_owner;
33 static int prepare_refcnt;
34 static int enable_refcnt;
36 static HLIST_HEAD(clk_root_list);
37 static HLIST_HEAD(clk_orphan_list);
38 static LIST_HEAD(clk_notifier_list);
40 static struct hlist_head *all_lists[] = {
46 /*** private data structures ***/
48 struct clk_parent_map {
49 const struct clk_hw *hw;
50 struct clk_core *core;
58 const struct clk_ops *ops;
62 struct device_node *of_node;
63 struct clk_core *parent;
64 struct clk_parent_map *parents;
68 unsigned long req_rate;
69 unsigned long new_rate;
70 struct clk_core *new_parent;
71 struct clk_core *new_child;
75 unsigned int enable_count;
76 unsigned int prepare_count;
77 unsigned int protect_count;
78 unsigned long min_rate;
79 unsigned long max_rate;
80 unsigned long accuracy;
83 struct hlist_head children;
84 struct hlist_node child_node;
85 struct hlist_head clks;
86 unsigned int notifier_count;
87 #ifdef CONFIG_DEBUG_FS
88 struct dentry *dentry;
89 struct hlist_node debug_node;
94 #define CREATE_TRACE_POINTS
95 #include <trace/events/clk.h>
98 struct clk_core *core;
102 unsigned long min_rate;
103 unsigned long max_rate;
104 unsigned int exclusive_count;
105 struct hlist_node clks_node;
109 static int clk_pm_runtime_get(struct clk_core *core)
113 if (!core->rpm_enabled)
116 ret = pm_runtime_get_sync(core->dev);
118 pm_runtime_put_noidle(core->dev);
124 static void clk_pm_runtime_put(struct clk_core *core)
126 if (!core->rpm_enabled)
129 pm_runtime_put_sync(core->dev);
133 static void clk_prepare_lock(void)
135 if (!mutex_trylock(&prepare_lock)) {
136 if (prepare_owner == current) {
140 mutex_lock(&prepare_lock);
142 WARN_ON_ONCE(prepare_owner != NULL);
143 WARN_ON_ONCE(prepare_refcnt != 0);
144 prepare_owner = current;
148 static void clk_prepare_unlock(void)
150 WARN_ON_ONCE(prepare_owner != current);
151 WARN_ON_ONCE(prepare_refcnt == 0);
153 if (--prepare_refcnt)
155 prepare_owner = NULL;
156 mutex_unlock(&prepare_lock);
159 static unsigned long clk_enable_lock(void)
160 __acquires(enable_lock)
165 * On UP systems, spin_trylock_irqsave() always returns true, even if
166 * we already hold the lock. So, in that case, we rely only on
167 * reference counting.
169 if (!IS_ENABLED(CONFIG_SMP) ||
170 !spin_trylock_irqsave(&enable_lock, flags)) {
171 if (enable_owner == current) {
173 __acquire(enable_lock);
174 if (!IS_ENABLED(CONFIG_SMP))
175 local_save_flags(flags);
178 spin_lock_irqsave(&enable_lock, flags);
180 WARN_ON_ONCE(enable_owner != NULL);
181 WARN_ON_ONCE(enable_refcnt != 0);
182 enable_owner = current;
187 static void clk_enable_unlock(unsigned long flags)
188 __releases(enable_lock)
190 WARN_ON_ONCE(enable_owner != current);
191 WARN_ON_ONCE(enable_refcnt == 0);
193 if (--enable_refcnt) {
194 __release(enable_lock);
198 spin_unlock_irqrestore(&enable_lock, flags);
201 static bool clk_core_rate_is_protected(struct clk_core *core)
203 return core->protect_count;
206 static bool clk_core_is_prepared(struct clk_core *core)
211 * .is_prepared is optional for clocks that can prepare
212 * fall back to software usage counter if it is missing
214 if (!core->ops->is_prepared)
215 return core->prepare_count;
217 if (!clk_pm_runtime_get(core)) {
218 ret = core->ops->is_prepared(core->hw);
219 clk_pm_runtime_put(core);
225 static bool clk_core_is_enabled(struct clk_core *core)
230 * .is_enabled is only mandatory for clocks that gate
231 * fall back to software usage counter if .is_enabled is missing
233 if (!core->ops->is_enabled)
234 return core->enable_count;
237 * Check if clock controller's device is runtime active before
238 * calling .is_enabled callback. If not, assume that clock is
239 * disabled, because we might be called from atomic context, from
240 * which pm_runtime_get() is not allowed.
241 * This function is called mainly from clk_disable_unused_subtree,
242 * which ensures proper runtime pm activation of controller before
243 * taking enable spinlock, but the below check is needed if one tries
244 * to call it from other places.
246 if (core->rpm_enabled) {
247 pm_runtime_get_noresume(core->dev);
248 if (!pm_runtime_active(core->dev)) {
255 * This could be called with the enable lock held, or from atomic
256 * context. If the parent isn't enabled already, we can't do
257 * anything here. We can also assume this clock isn't enabled.
259 if ((core->flags & CLK_OPS_PARENT_ENABLE) && core->parent)
260 if (!clk_core_is_enabled(core->parent)) {
265 ret = core->ops->is_enabled(core->hw);
267 if (core->rpm_enabled)
268 pm_runtime_put(core->dev);
273 /*** helper functions ***/
275 const char *__clk_get_name(const struct clk *clk)
277 return !clk ? NULL : clk->core->name;
279 EXPORT_SYMBOL_GPL(__clk_get_name);
281 const char *clk_hw_get_name(const struct clk_hw *hw)
283 return hw->core->name;
285 EXPORT_SYMBOL_GPL(clk_hw_get_name);
287 struct clk_hw *__clk_get_hw(struct clk *clk)
289 return !clk ? NULL : clk->core->hw;
291 EXPORT_SYMBOL_GPL(__clk_get_hw);
293 unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
295 return hw->core->num_parents;
297 EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
299 struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
301 return hw->core->parent ? hw->core->parent->hw : NULL;
303 EXPORT_SYMBOL_GPL(clk_hw_get_parent);
305 static struct clk_core *__clk_lookup_subtree(const char *name,
306 struct clk_core *core)
308 struct clk_core *child;
309 struct clk_core *ret;
311 if (!strcmp(core->name, name))
314 hlist_for_each_entry(child, &core->children, child_node) {
315 ret = __clk_lookup_subtree(name, child);
323 static struct clk_core *clk_core_lookup(const char *name)
325 struct clk_core *root_clk;
326 struct clk_core *ret;
331 /* search the 'proper' clk tree first */
332 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
333 ret = __clk_lookup_subtree(name, root_clk);
338 /* if not found, then search the orphan tree */
339 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
340 ret = __clk_lookup_subtree(name, root_clk);
349 static int of_parse_clkspec(const struct device_node *np, int index,
350 const char *name, struct of_phandle_args *out_args);
351 static struct clk_hw *
352 of_clk_get_hw_from_clkspec(struct of_phandle_args *clkspec);
354 static inline int of_parse_clkspec(const struct device_node *np, int index,
356 struct of_phandle_args *out_args)
360 static inline struct clk_hw *
361 of_clk_get_hw_from_clkspec(struct of_phandle_args *clkspec)
363 return ERR_PTR(-ENOENT);
368 * clk_core_get - Find the clk_core parent of a clk
369 * @core: clk to find parent of
370 * @p_index: parent index to search for
372 * This is the preferred method for clk providers to find the parent of a
373 * clk when that parent is external to the clk controller. The parent_names
374 * array is indexed and treated as a local name matching a string in the device
375 * node's 'clock-names' property or as the 'con_id' matching the device's
376 * dev_name() in a clk_lookup. This allows clk providers to use their own
377 * namespace instead of looking for a globally unique parent string.
379 * For example the following DT snippet would allow a clock registered by the
380 * clock-controller@c001 that has a clk_init_data::parent_data array
381 * with 'xtal' in the 'name' member to find the clock provided by the
382 * clock-controller@f00abcd without needing to get the globally unique name of
385 * parent: clock-controller@f00abcd {
386 * reg = <0xf00abcd 0xabcd>;
387 * #clock-cells = <0>;
390 * clock-controller@c001 {
391 * reg = <0xc001 0xf00d>;
392 * clocks = <&parent>;
393 * clock-names = "xtal";
394 * #clock-cells = <1>;
397 * Returns: -ENOENT when the provider can't be found or the clk doesn't
398 * exist in the provider or the name can't be found in the DT node or
399 * in a clkdev lookup. NULL when the provider knows about the clk but it
400 * isn't provided on this system.
401 * A valid clk_core pointer when the clk can be found in the provider.
403 static struct clk_core *clk_core_get(struct clk_core *core, u8 p_index)
405 const char *name = core->parents[p_index].fw_name;
406 int index = core->parents[p_index].index;
407 struct clk_hw *hw = ERR_PTR(-ENOENT);
408 struct device *dev = core->dev;
409 const char *dev_id = dev ? dev_name(dev) : NULL;
410 struct device_node *np = core->of_node;
411 struct of_phandle_args clkspec;
413 if (np && (name || index >= 0) &&
414 !of_parse_clkspec(np, index, name, &clkspec)) {
415 hw = of_clk_get_hw_from_clkspec(&clkspec);
416 of_node_put(clkspec.np);
419 * If the DT search above couldn't find the provider fallback to
420 * looking up via clkdev based clk_lookups.
422 hw = clk_find_hw(dev_id, name);
431 static void clk_core_fill_parent_index(struct clk_core *core, u8 index)
433 struct clk_parent_map *entry = &core->parents[index];
434 struct clk_core *parent = ERR_PTR(-ENOENT);
437 parent = entry->hw->core;
439 * We have a direct reference but it isn't registered yet?
440 * Orphan it and let clk_reparent() update the orphan status
441 * when the parent is registered.
444 parent = ERR_PTR(-EPROBE_DEFER);
446 parent = clk_core_get(core, index);
447 if (IS_ERR(parent) && PTR_ERR(parent) == -ENOENT && entry->name)
448 parent = clk_core_lookup(entry->name);
451 /* Only cache it if it's not an error */
453 entry->core = parent;
456 static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
459 if (!core || index >= core->num_parents || !core->parents)
462 if (!core->parents[index].core)
463 clk_core_fill_parent_index(core, index);
465 return core->parents[index].core;
469 clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
471 struct clk_core *parent;
473 parent = clk_core_get_parent_by_index(hw->core, index);
475 return !parent ? NULL : parent->hw;
477 EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
479 unsigned int __clk_get_enable_count(struct clk *clk)
481 return !clk ? 0 : clk->core->enable_count;
484 static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
489 if (!core->num_parents || core->parent)
493 * Clk must have a parent because num_parents > 0 but the parent isn't
494 * known yet. Best to return 0 as the rate of this clk until we can
495 * properly recalc the rate based on the parent's rate.
500 unsigned long clk_hw_get_rate(const struct clk_hw *hw)
502 return clk_core_get_rate_nolock(hw->core);
504 EXPORT_SYMBOL_GPL(clk_hw_get_rate);
506 static unsigned long __clk_get_accuracy(struct clk_core *core)
511 return core->accuracy;
514 unsigned long __clk_get_flags(struct clk *clk)
516 return !clk ? 0 : clk->core->flags;
518 EXPORT_SYMBOL_GPL(__clk_get_flags);
520 unsigned long clk_hw_get_flags(const struct clk_hw *hw)
522 return hw->core->flags;
524 EXPORT_SYMBOL_GPL(clk_hw_get_flags);
526 bool clk_hw_is_prepared(const struct clk_hw *hw)
528 return clk_core_is_prepared(hw->core);
530 EXPORT_SYMBOL_GPL(clk_hw_is_prepared);
532 bool clk_hw_rate_is_protected(const struct clk_hw *hw)
534 return clk_core_rate_is_protected(hw->core);
536 EXPORT_SYMBOL_GPL(clk_hw_rate_is_protected);
538 bool clk_hw_is_enabled(const struct clk_hw *hw)
540 return clk_core_is_enabled(hw->core);
542 EXPORT_SYMBOL_GPL(clk_hw_is_enabled);
544 bool __clk_is_enabled(struct clk *clk)
549 return clk_core_is_enabled(clk->core);
551 EXPORT_SYMBOL_GPL(__clk_is_enabled);
553 static bool mux_is_better_rate(unsigned long rate, unsigned long now,
554 unsigned long best, unsigned long flags)
556 if (flags & CLK_MUX_ROUND_CLOSEST)
557 return abs(now - rate) < abs(best - rate);
559 return now <= rate && now > best;
562 int clk_mux_determine_rate_flags(struct clk_hw *hw,
563 struct clk_rate_request *req,
566 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
567 int i, num_parents, ret;
568 unsigned long best = 0;
569 struct clk_rate_request parent_req = *req;
571 /* if NO_REPARENT flag set, pass through to current parent */
572 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
573 parent = core->parent;
574 if (core->flags & CLK_SET_RATE_PARENT) {
575 ret = __clk_determine_rate(parent ? parent->hw : NULL,
580 best = parent_req.rate;
582 best = clk_core_get_rate_nolock(parent);
584 best = clk_core_get_rate_nolock(core);
590 /* find the parent that can provide the fastest rate <= rate */
591 num_parents = core->num_parents;
592 for (i = 0; i < num_parents; i++) {
593 parent = clk_core_get_parent_by_index(core, i);
597 if (core->flags & CLK_SET_RATE_PARENT) {
599 ret = __clk_determine_rate(parent->hw, &parent_req);
603 parent_req.rate = clk_core_get_rate_nolock(parent);
606 if (mux_is_better_rate(req->rate, parent_req.rate,
608 best_parent = parent;
609 best = parent_req.rate;
618 req->best_parent_hw = best_parent->hw;
619 req->best_parent_rate = best;
624 EXPORT_SYMBOL_GPL(clk_mux_determine_rate_flags);
626 struct clk *__clk_lookup(const char *name)
628 struct clk_core *core = clk_core_lookup(name);
630 return !core ? NULL : core->hw->clk;
633 static void clk_core_get_boundaries(struct clk_core *core,
634 unsigned long *min_rate,
635 unsigned long *max_rate)
637 struct clk *clk_user;
639 lockdep_assert_held(&prepare_lock);
641 *min_rate = core->min_rate;
642 *max_rate = core->max_rate;
644 hlist_for_each_entry(clk_user, &core->clks, clks_node)
645 *min_rate = max(*min_rate, clk_user->min_rate);
647 hlist_for_each_entry(clk_user, &core->clks, clks_node)
648 *max_rate = min(*max_rate, clk_user->max_rate);
651 static bool clk_core_check_boundaries(struct clk_core *core,
652 unsigned long min_rate,
653 unsigned long max_rate)
657 lockdep_assert_held(&prepare_lock);
659 if (min_rate > core->max_rate || max_rate < core->min_rate)
662 hlist_for_each_entry(user, &core->clks, clks_node)
663 if (min_rate > user->max_rate || max_rate < user->min_rate)
669 void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
670 unsigned long max_rate)
672 hw->core->min_rate = min_rate;
673 hw->core->max_rate = max_rate;
675 EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
678 * __clk_mux_determine_rate - clk_ops::determine_rate implementation for a mux type clk
679 * @hw: mux type clk to determine rate on
680 * @req: rate request, also used to return preferred parent and frequencies
682 * Helper for finding best parent to provide a given frequency. This can be used
683 * directly as a determine_rate callback (e.g. for a mux), or from a more
684 * complex clock that may combine a mux with other operations.
686 * Returns: 0 on success, -EERROR value on error
688 int __clk_mux_determine_rate(struct clk_hw *hw,
689 struct clk_rate_request *req)
691 return clk_mux_determine_rate_flags(hw, req, 0);
693 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
695 int __clk_mux_determine_rate_closest(struct clk_hw *hw,
696 struct clk_rate_request *req)
698 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
700 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
704 static void clk_core_rate_unprotect(struct clk_core *core)
706 lockdep_assert_held(&prepare_lock);
711 if (WARN(core->protect_count == 0,
712 "%s already unprotected\n", core->name))
715 if (--core->protect_count > 0)
718 clk_core_rate_unprotect(core->parent);
721 static int clk_core_rate_nuke_protect(struct clk_core *core)
725 lockdep_assert_held(&prepare_lock);
730 if (core->protect_count == 0)
733 ret = core->protect_count;
734 core->protect_count = 1;
735 clk_core_rate_unprotect(core);
741 * clk_rate_exclusive_put - release exclusivity over clock rate control
742 * @clk: the clk over which the exclusivity is released
744 * clk_rate_exclusive_put() completes a critical section during which a clock
745 * consumer cannot tolerate any other consumer making any operation on the
746 * clock which could result in a rate change or rate glitch. Exclusive clocks
747 * cannot have their rate changed, either directly or indirectly due to changes
748 * further up the parent chain of clocks. As a result, clocks up parent chain
749 * also get under exclusive control of the calling consumer.
751 * If exlusivity is claimed more than once on clock, even by the same consumer,
752 * the rate effectively gets locked as exclusivity can't be preempted.
754 * Calls to clk_rate_exclusive_put() must be balanced with calls to
755 * clk_rate_exclusive_get(). Calls to this function may sleep, and do not return
758 void clk_rate_exclusive_put(struct clk *clk)
766 * if there is something wrong with this consumer protect count, stop
767 * here before messing with the provider
769 if (WARN_ON(clk->exclusive_count <= 0))
772 clk_core_rate_unprotect(clk->core);
773 clk->exclusive_count--;
775 clk_prepare_unlock();
777 EXPORT_SYMBOL_GPL(clk_rate_exclusive_put);
779 static void clk_core_rate_protect(struct clk_core *core)
781 lockdep_assert_held(&prepare_lock);
786 if (core->protect_count == 0)
787 clk_core_rate_protect(core->parent);
789 core->protect_count++;
792 static void clk_core_rate_restore_protect(struct clk_core *core, int count)
794 lockdep_assert_held(&prepare_lock);
802 clk_core_rate_protect(core);
803 core->protect_count = count;
807 * clk_rate_exclusive_get - get exclusivity over the clk rate control
808 * @clk: the clk over which the exclusity of rate control is requested
810 * clk_rate_exlusive_get() begins a critical section during which a clock
811 * consumer cannot tolerate any other consumer making any operation on the
812 * clock which could result in a rate change or rate glitch. Exclusive clocks
813 * cannot have their rate changed, either directly or indirectly due to changes
814 * further up the parent chain of clocks. As a result, clocks up parent chain
815 * also get under exclusive control of the calling consumer.
817 * If exlusivity is claimed more than once on clock, even by the same consumer,
818 * the rate effectively gets locked as exclusivity can't be preempted.
820 * Calls to clk_rate_exclusive_get() should be balanced with calls to
821 * clk_rate_exclusive_put(). Calls to this function may sleep.
822 * Returns 0 on success, -EERROR otherwise
824 int clk_rate_exclusive_get(struct clk *clk)
830 clk_core_rate_protect(clk->core);
831 clk->exclusive_count++;
832 clk_prepare_unlock();
836 EXPORT_SYMBOL_GPL(clk_rate_exclusive_get);
838 static void clk_core_unprepare(struct clk_core *core)
840 lockdep_assert_held(&prepare_lock);
845 if (WARN(core->prepare_count == 0,
846 "%s already unprepared\n", core->name))
849 if (WARN(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL,
850 "Unpreparing critical %s\n", core->name))
853 if (core->flags & CLK_SET_RATE_GATE)
854 clk_core_rate_unprotect(core);
856 if (--core->prepare_count > 0)
859 WARN(core->enable_count > 0, "Unpreparing enabled %s\n", core->name);
861 trace_clk_unprepare(core);
863 if (core->ops->unprepare)
864 core->ops->unprepare(core->hw);
866 trace_clk_unprepare_complete(core);
867 clk_core_unprepare(core->parent);
868 clk_pm_runtime_put(core);
871 static void clk_core_unprepare_lock(struct clk_core *core)
874 clk_core_unprepare(core);
875 clk_prepare_unlock();
879 * clk_unprepare - undo preparation of a clock source
880 * @clk: the clk being unprepared
882 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
883 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
884 * if the operation may sleep. One example is a clk which is accessed over
885 * I2c. In the complex case a clk gate operation may require a fast and a slow
886 * part. It is this reason that clk_unprepare and clk_disable are not mutually
887 * exclusive. In fact clk_disable must be called before clk_unprepare.
889 void clk_unprepare(struct clk *clk)
891 if (IS_ERR_OR_NULL(clk))
894 clk_core_unprepare_lock(clk->core);
896 EXPORT_SYMBOL_GPL(clk_unprepare);
898 static int clk_core_prepare(struct clk_core *core)
902 lockdep_assert_held(&prepare_lock);
907 if (core->prepare_count == 0) {
908 ret = clk_pm_runtime_get(core);
912 ret = clk_core_prepare(core->parent);
916 trace_clk_prepare(core);
918 if (core->ops->prepare)
919 ret = core->ops->prepare(core->hw);
921 trace_clk_prepare_complete(core);
927 core->prepare_count++;
930 * CLK_SET_RATE_GATE is a special case of clock protection
931 * Instead of a consumer claiming exclusive rate control, it is
932 * actually the provider which prevents any consumer from making any
933 * operation which could result in a rate change or rate glitch while
934 * the clock is prepared.
936 if (core->flags & CLK_SET_RATE_GATE)
937 clk_core_rate_protect(core);
941 clk_core_unprepare(core->parent);
943 clk_pm_runtime_put(core);
947 static int clk_core_prepare_lock(struct clk_core *core)
952 ret = clk_core_prepare(core);
953 clk_prepare_unlock();
959 * clk_prepare - prepare a clock source
960 * @clk: the clk being prepared
962 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
963 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
964 * operation may sleep. One example is a clk which is accessed over I2c. In
965 * the complex case a clk ungate operation may require a fast and a slow part.
966 * It is this reason that clk_prepare and clk_enable are not mutually
967 * exclusive. In fact clk_prepare must be called before clk_enable.
968 * Returns 0 on success, -EERROR otherwise.
970 int clk_prepare(struct clk *clk)
975 return clk_core_prepare_lock(clk->core);
977 EXPORT_SYMBOL_GPL(clk_prepare);
979 static void clk_core_disable(struct clk_core *core)
981 lockdep_assert_held(&enable_lock);
986 if (WARN(core->enable_count == 0, "%s already disabled\n", core->name))
989 if (WARN(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL,
990 "Disabling critical %s\n", core->name))
993 if (--core->enable_count > 0)
996 trace_clk_disable_rcuidle(core);
998 if (core->ops->disable)
999 core->ops->disable(core->hw);
1001 trace_clk_disable_complete_rcuidle(core);
1003 clk_core_disable(core->parent);
1006 static void clk_core_disable_lock(struct clk_core *core)
1008 unsigned long flags;
1010 flags = clk_enable_lock();
1011 clk_core_disable(core);
1012 clk_enable_unlock(flags);
1016 * clk_disable - gate a clock
1017 * @clk: the clk being gated
1019 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
1020 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
1021 * clk if the operation is fast and will never sleep. One example is a
1022 * SoC-internal clk which is controlled via simple register writes. In the
1023 * complex case a clk gate operation may require a fast and a slow part. It is
1024 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
1025 * In fact clk_disable must be called before clk_unprepare.
1027 void clk_disable(struct clk *clk)
1029 if (IS_ERR_OR_NULL(clk))
1032 clk_core_disable_lock(clk->core);
1034 EXPORT_SYMBOL_GPL(clk_disable);
1036 static int clk_core_enable(struct clk_core *core)
1040 lockdep_assert_held(&enable_lock);
1045 if (WARN(core->prepare_count == 0,
1046 "Enabling unprepared %s\n", core->name))
1049 if (core->enable_count == 0) {
1050 ret = clk_core_enable(core->parent);
1055 trace_clk_enable_rcuidle(core);
1057 if (core->ops->enable)
1058 ret = core->ops->enable(core->hw);
1060 trace_clk_enable_complete_rcuidle(core);
1063 clk_core_disable(core->parent);
1068 core->enable_count++;
1072 static int clk_core_enable_lock(struct clk_core *core)
1074 unsigned long flags;
1077 flags = clk_enable_lock();
1078 ret = clk_core_enable(core);
1079 clk_enable_unlock(flags);
1085 * clk_gate_restore_context - restore context for poweroff
1086 * @hw: the clk_hw pointer of clock whose state is to be restored
1088 * The clock gate restore context function enables or disables
1089 * the gate clocks based on the enable_count. This is done in cases
1090 * where the clock context is lost and based on the enable_count
1091 * the clock either needs to be enabled/disabled. This
1092 * helps restore the state of gate clocks.
1094 void clk_gate_restore_context(struct clk_hw *hw)
1096 struct clk_core *core = hw->core;
1098 if (core->enable_count)
1099 core->ops->enable(hw);
1101 core->ops->disable(hw);
1103 EXPORT_SYMBOL_GPL(clk_gate_restore_context);
1105 static int clk_core_save_context(struct clk_core *core)
1107 struct clk_core *child;
1110 hlist_for_each_entry(child, &core->children, child_node) {
1111 ret = clk_core_save_context(child);
1116 if (core->ops && core->ops->save_context)
1117 ret = core->ops->save_context(core->hw);
1122 static void clk_core_restore_context(struct clk_core *core)
1124 struct clk_core *child;
1126 if (core->ops && core->ops->restore_context)
1127 core->ops->restore_context(core->hw);
1129 hlist_for_each_entry(child, &core->children, child_node)
1130 clk_core_restore_context(child);
1134 * clk_save_context - save clock context for poweroff
1136 * Saves the context of the clock register for powerstates in which the
1137 * contents of the registers will be lost. Occurs deep within the suspend
1138 * code. Returns 0 on success.
1140 int clk_save_context(void)
1142 struct clk_core *clk;
1145 hlist_for_each_entry(clk, &clk_root_list, child_node) {
1146 ret = clk_core_save_context(clk);
1151 hlist_for_each_entry(clk, &clk_orphan_list, child_node) {
1152 ret = clk_core_save_context(clk);
1159 EXPORT_SYMBOL_GPL(clk_save_context);
1162 * clk_restore_context - restore clock context after poweroff
1164 * Restore the saved clock context upon resume.
1167 void clk_restore_context(void)
1169 struct clk_core *core;
1171 hlist_for_each_entry(core, &clk_root_list, child_node)
1172 clk_core_restore_context(core);
1174 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1175 clk_core_restore_context(core);
1177 EXPORT_SYMBOL_GPL(clk_restore_context);
1180 * clk_enable - ungate a clock
1181 * @clk: the clk being ungated
1183 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
1184 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
1185 * if the operation will never sleep. One example is a SoC-internal clk which
1186 * is controlled via simple register writes. In the complex case a clk ungate
1187 * operation may require a fast and a slow part. It is this reason that
1188 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
1189 * must be called before clk_enable. Returns 0 on success, -EERROR
1192 int clk_enable(struct clk *clk)
1197 return clk_core_enable_lock(clk->core);
1199 EXPORT_SYMBOL_GPL(clk_enable);
1201 static int clk_core_prepare_enable(struct clk_core *core)
1205 ret = clk_core_prepare_lock(core);
1209 ret = clk_core_enable_lock(core);
1211 clk_core_unprepare_lock(core);
1216 static void clk_core_disable_unprepare(struct clk_core *core)
1218 clk_core_disable_lock(core);
1219 clk_core_unprepare_lock(core);
1222 static void clk_unprepare_unused_subtree(struct clk_core *core)
1224 struct clk_core *child;
1226 lockdep_assert_held(&prepare_lock);
1228 hlist_for_each_entry(child, &core->children, child_node)
1229 clk_unprepare_unused_subtree(child);
1231 if (core->prepare_count)
1234 if (core->flags & CLK_IGNORE_UNUSED)
1237 if (clk_pm_runtime_get(core))
1240 if (clk_core_is_prepared(core)) {
1241 trace_clk_unprepare(core);
1242 if (core->ops->unprepare_unused)
1243 core->ops->unprepare_unused(core->hw);
1244 else if (core->ops->unprepare)
1245 core->ops->unprepare(core->hw);
1246 trace_clk_unprepare_complete(core);
1249 clk_pm_runtime_put(core);
1252 static void clk_disable_unused_subtree(struct clk_core *core)
1254 struct clk_core *child;
1255 unsigned long flags;
1257 lockdep_assert_held(&prepare_lock);
1259 hlist_for_each_entry(child, &core->children, child_node)
1260 clk_disable_unused_subtree(child);
1262 if (core->flags & CLK_OPS_PARENT_ENABLE)
1263 clk_core_prepare_enable(core->parent);
1265 if (clk_pm_runtime_get(core))
1268 flags = clk_enable_lock();
1270 if (core->enable_count)
1273 if (core->flags & CLK_IGNORE_UNUSED)
1277 * some gate clocks have special needs during the disable-unused
1278 * sequence. call .disable_unused if available, otherwise fall
1281 if (clk_core_is_enabled(core)) {
1282 trace_clk_disable(core);
1283 if (core->ops->disable_unused)
1284 core->ops->disable_unused(core->hw);
1285 else if (core->ops->disable)
1286 core->ops->disable(core->hw);
1287 trace_clk_disable_complete(core);
1291 clk_enable_unlock(flags);
1292 clk_pm_runtime_put(core);
1294 if (core->flags & CLK_OPS_PARENT_ENABLE)
1295 clk_core_disable_unprepare(core->parent);
1298 static bool clk_ignore_unused;
1299 static int __init clk_ignore_unused_setup(char *__unused)
1301 clk_ignore_unused = true;
1304 __setup("clk_ignore_unused", clk_ignore_unused_setup);
1306 static int clk_disable_unused(void)
1308 struct clk_core *core;
1310 if (clk_ignore_unused) {
1311 pr_warn("clk: Not disabling unused clocks\n");
1317 hlist_for_each_entry(core, &clk_root_list, child_node)
1318 clk_disable_unused_subtree(core);
1320 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1321 clk_disable_unused_subtree(core);
1323 hlist_for_each_entry(core, &clk_root_list, child_node)
1324 clk_unprepare_unused_subtree(core);
1326 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1327 clk_unprepare_unused_subtree(core);
1329 clk_prepare_unlock();
1333 late_initcall_sync(clk_disable_unused);
1335 static int clk_core_determine_round_nolock(struct clk_core *core,
1336 struct clk_rate_request *req)
1340 lockdep_assert_held(&prepare_lock);
1346 * At this point, core protection will be disabled if
1347 * - if the provider is not protected at all
1348 * - if the calling consumer is the only one which has exclusivity
1351 if (clk_core_rate_is_protected(core)) {
1352 req->rate = core->rate;
1353 } else if (core->ops->determine_rate) {
1354 return core->ops->determine_rate(core->hw, req);
1355 } else if (core->ops->round_rate) {
1356 rate = core->ops->round_rate(core->hw, req->rate,
1357 &req->best_parent_rate);
1369 static void clk_core_init_rate_req(struct clk_core * const core,
1370 struct clk_rate_request *req)
1372 struct clk_core *parent;
1374 if (WARN_ON(!core || !req))
1377 parent = core->parent;
1379 req->best_parent_hw = parent->hw;
1380 req->best_parent_rate = parent->rate;
1382 req->best_parent_hw = NULL;
1383 req->best_parent_rate = 0;
1387 static bool clk_core_can_round(struct clk_core * const core)
1389 return core->ops->determine_rate || core->ops->round_rate;
1392 static int clk_core_round_rate_nolock(struct clk_core *core,
1393 struct clk_rate_request *req)
1395 lockdep_assert_held(&prepare_lock);
1402 clk_core_init_rate_req(core, req);
1404 if (clk_core_can_round(core))
1405 return clk_core_determine_round_nolock(core, req);
1406 else if (core->flags & CLK_SET_RATE_PARENT)
1407 return clk_core_round_rate_nolock(core->parent, req);
1409 req->rate = core->rate;
1414 * __clk_determine_rate - get the closest rate actually supported by a clock
1415 * @hw: determine the rate of this clock
1416 * @req: target rate request
1418 * Useful for clk_ops such as .set_rate and .determine_rate.
1420 int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
1427 return clk_core_round_rate_nolock(hw->core, req);
1429 EXPORT_SYMBOL_GPL(__clk_determine_rate);
1431 unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
1434 struct clk_rate_request req;
1436 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
1439 ret = clk_core_round_rate_nolock(hw->core, &req);
1445 EXPORT_SYMBOL_GPL(clk_hw_round_rate);
1448 * clk_round_rate - round the given rate for a clk
1449 * @clk: the clk for which we are rounding a rate
1450 * @rate: the rate which is to be rounded
1452 * Takes in a rate as input and rounds it to a rate that the clk can actually
1453 * use which is then returned. If clk doesn't support round_rate operation
1454 * then the parent rate is returned.
1456 long clk_round_rate(struct clk *clk, unsigned long rate)
1458 struct clk_rate_request req;
1466 if (clk->exclusive_count)
1467 clk_core_rate_unprotect(clk->core);
1469 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
1472 ret = clk_core_round_rate_nolock(clk->core, &req);
1474 if (clk->exclusive_count)
1475 clk_core_rate_protect(clk->core);
1477 clk_prepare_unlock();
1484 EXPORT_SYMBOL_GPL(clk_round_rate);
1487 * __clk_notify - call clk notifier chain
1488 * @core: clk that is changing rate
1489 * @msg: clk notifier type (see include/linux/clk.h)
1490 * @old_rate: old clk rate
1491 * @new_rate: new clk rate
1493 * Triggers a notifier call chain on the clk rate-change notification
1494 * for 'clk'. Passes a pointer to the struct clk and the previous
1495 * and current rates to the notifier callback. Intended to be called by
1496 * internal clock code only. Returns NOTIFY_DONE from the last driver
1497 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1498 * a driver returns that.
1500 static int __clk_notify(struct clk_core *core, unsigned long msg,
1501 unsigned long old_rate, unsigned long new_rate)
1503 struct clk_notifier *cn;
1504 struct clk_notifier_data cnd;
1505 int ret = NOTIFY_DONE;
1507 cnd.old_rate = old_rate;
1508 cnd.new_rate = new_rate;
1510 list_for_each_entry(cn, &clk_notifier_list, node) {
1511 if (cn->clk->core == core) {
1513 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1515 if (ret & NOTIFY_STOP_MASK)
1524 * __clk_recalc_accuracies
1525 * @core: first clk in the subtree
1527 * Walks the subtree of clks starting with clk and recalculates accuracies as
1528 * it goes. Note that if a clk does not implement the .recalc_accuracy
1529 * callback then it is assumed that the clock will take on the accuracy of its
1532 static void __clk_recalc_accuracies(struct clk_core *core)
1534 unsigned long parent_accuracy = 0;
1535 struct clk_core *child;
1537 lockdep_assert_held(&prepare_lock);
1540 parent_accuracy = core->parent->accuracy;
1542 if (core->ops->recalc_accuracy)
1543 core->accuracy = core->ops->recalc_accuracy(core->hw,
1546 core->accuracy = parent_accuracy;
1548 hlist_for_each_entry(child, &core->children, child_node)
1549 __clk_recalc_accuracies(child);
1552 static long clk_core_get_accuracy(struct clk_core *core)
1554 unsigned long accuracy;
1557 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1558 __clk_recalc_accuracies(core);
1560 accuracy = __clk_get_accuracy(core);
1561 clk_prepare_unlock();
1567 * clk_get_accuracy - return the accuracy of clk
1568 * @clk: the clk whose accuracy is being returned
1570 * Simply returns the cached accuracy of the clk, unless
1571 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1573 * If clk is NULL then returns 0.
1575 long clk_get_accuracy(struct clk *clk)
1580 return clk_core_get_accuracy(clk->core);
1582 EXPORT_SYMBOL_GPL(clk_get_accuracy);
1584 static unsigned long clk_recalc(struct clk_core *core,
1585 unsigned long parent_rate)
1587 unsigned long rate = parent_rate;
1589 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
1590 rate = core->ops->recalc_rate(core->hw, parent_rate);
1591 clk_pm_runtime_put(core);
1597 * __clk_recalc_rates
1598 * @core: first clk in the subtree
1599 * @msg: notification type (see include/linux/clk.h)
1601 * Walks the subtree of clks starting with clk and recalculates rates as it
1602 * goes. Note that if a clk does not implement the .recalc_rate callback then
1603 * it is assumed that the clock will take on the rate of its parent.
1605 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1608 static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
1610 unsigned long old_rate;
1611 unsigned long parent_rate = 0;
1612 struct clk_core *child;
1614 lockdep_assert_held(&prepare_lock);
1616 old_rate = core->rate;
1619 parent_rate = core->parent->rate;
1621 core->rate = clk_recalc(core, parent_rate);
1624 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1625 * & ABORT_RATE_CHANGE notifiers
1627 if (core->notifier_count && msg)
1628 __clk_notify(core, msg, old_rate, core->rate);
1630 hlist_for_each_entry(child, &core->children, child_node)
1631 __clk_recalc_rates(child, msg);
1634 static unsigned long clk_core_get_rate(struct clk_core *core)
1640 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1641 __clk_recalc_rates(core, 0);
1643 rate = clk_core_get_rate_nolock(core);
1644 clk_prepare_unlock();
1650 * clk_get_rate - return the rate of clk
1651 * @clk: the clk whose rate is being returned
1653 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1654 * is set, which means a recalc_rate will be issued.
1655 * If clk is NULL then returns 0.
1657 unsigned long clk_get_rate(struct clk *clk)
1662 return clk_core_get_rate(clk->core);
1664 EXPORT_SYMBOL_GPL(clk_get_rate);
1666 static int clk_fetch_parent_index(struct clk_core *core,
1667 struct clk_core *parent)
1674 for (i = 0; i < core->num_parents; i++) {
1675 /* Found it first try! */
1676 if (core->parents[i].core == parent)
1679 /* Something else is here, so keep looking */
1680 if (core->parents[i].core)
1683 /* Maybe core hasn't been cached but the hw is all we know? */
1684 if (core->parents[i].hw) {
1685 if (core->parents[i].hw == parent->hw)
1688 /* Didn't match, but we're expecting a clk_hw */
1692 /* Maybe it hasn't been cached (clk_set_parent() path) */
1693 if (parent == clk_core_get(core, i))
1696 /* Fallback to comparing globally unique names */
1697 if (core->parents[i].name &&
1698 !strcmp(parent->name, core->parents[i].name))
1702 if (i == core->num_parents)
1705 core->parents[i].core = parent;
1710 * Update the orphan status of @core and all its children.
1712 static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1714 struct clk_core *child;
1716 core->orphan = is_orphan;
1718 hlist_for_each_entry(child, &core->children, child_node)
1719 clk_core_update_orphan_status(child, is_orphan);
1722 static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
1724 bool was_orphan = core->orphan;
1726 hlist_del(&core->child_node);
1729 bool becomes_orphan = new_parent->orphan;
1731 /* avoid duplicate POST_RATE_CHANGE notifications */
1732 if (new_parent->new_child == core)
1733 new_parent->new_child = NULL;
1735 hlist_add_head(&core->child_node, &new_parent->children);
1737 if (was_orphan != becomes_orphan)
1738 clk_core_update_orphan_status(core, becomes_orphan);
1740 hlist_add_head(&core->child_node, &clk_orphan_list);
1742 clk_core_update_orphan_status(core, true);
1745 core->parent = new_parent;
1748 static struct clk_core *__clk_set_parent_before(struct clk_core *core,
1749 struct clk_core *parent)
1751 unsigned long flags;
1752 struct clk_core *old_parent = core->parent;
1755 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1757 * 2. Migrate prepare state between parents and prevent race with
1760 * If the clock is not prepared, then a race with
1761 * clk_enable/disable() is impossible since we already have the
1762 * prepare lock (future calls to clk_enable() need to be preceded by
1765 * If the clock is prepared, migrate the prepared state to the new
1766 * parent and also protect against a race with clk_enable() by
1767 * forcing the clock and the new parent on. This ensures that all
1768 * future calls to clk_enable() are practically NOPs with respect to
1769 * hardware and software states.
1771 * See also: Comment for clk_set_parent() below.
1774 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1775 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1776 clk_core_prepare_enable(old_parent);
1777 clk_core_prepare_enable(parent);
1780 /* migrate prepare count if > 0 */
1781 if (core->prepare_count) {
1782 clk_core_prepare_enable(parent);
1783 clk_core_enable_lock(core);
1786 /* update the clk tree topology */
1787 flags = clk_enable_lock();
1788 clk_reparent(core, parent);
1789 clk_enable_unlock(flags);
1794 static void __clk_set_parent_after(struct clk_core *core,
1795 struct clk_core *parent,
1796 struct clk_core *old_parent)
1799 * Finish the migration of prepare state and undo the changes done
1800 * for preventing a race with clk_enable().
1802 if (core->prepare_count) {
1803 clk_core_disable_lock(core);
1804 clk_core_disable_unprepare(old_parent);
1807 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1808 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1809 clk_core_disable_unprepare(parent);
1810 clk_core_disable_unprepare(old_parent);
1814 static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
1817 unsigned long flags;
1819 struct clk_core *old_parent;
1821 old_parent = __clk_set_parent_before(core, parent);
1823 trace_clk_set_parent(core, parent);
1825 /* change clock input source */
1826 if (parent && core->ops->set_parent)
1827 ret = core->ops->set_parent(core->hw, p_index);
1829 trace_clk_set_parent_complete(core, parent);
1832 flags = clk_enable_lock();
1833 clk_reparent(core, old_parent);
1834 clk_enable_unlock(flags);
1835 __clk_set_parent_after(core, old_parent, parent);
1840 __clk_set_parent_after(core, parent, old_parent);
1846 * __clk_speculate_rates
1847 * @core: first clk in the subtree
1848 * @parent_rate: the "future" rate of clk's parent
1850 * Walks the subtree of clks starting with clk, speculating rates as it
1851 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1853 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1854 * pre-rate change notifications and returns early if no clks in the
1855 * subtree have subscribed to the notifications. Note that if a clk does not
1856 * implement the .recalc_rate callback then it is assumed that the clock will
1857 * take on the rate of its parent.
1859 static int __clk_speculate_rates(struct clk_core *core,
1860 unsigned long parent_rate)
1862 struct clk_core *child;
1863 unsigned long new_rate;
1864 int ret = NOTIFY_DONE;
1866 lockdep_assert_held(&prepare_lock);
1868 new_rate = clk_recalc(core, parent_rate);
1870 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
1871 if (core->notifier_count)
1872 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
1874 if (ret & NOTIFY_STOP_MASK) {
1875 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
1876 __func__, core->name, ret);
1880 hlist_for_each_entry(child, &core->children, child_node) {
1881 ret = __clk_speculate_rates(child, new_rate);
1882 if (ret & NOTIFY_STOP_MASK)
1890 static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
1891 struct clk_core *new_parent, u8 p_index)
1893 struct clk_core *child;
1895 core->new_rate = new_rate;
1896 core->new_parent = new_parent;
1897 core->new_parent_index = p_index;
1898 /* include clk in new parent's PRE_RATE_CHANGE notifications */
1899 core->new_child = NULL;
1900 if (new_parent && new_parent != core->parent)
1901 new_parent->new_child = core;
1903 hlist_for_each_entry(child, &core->children, child_node) {
1904 child->new_rate = clk_recalc(child, new_rate);
1905 clk_calc_subtree(child, child->new_rate, NULL, 0);
1910 * calculate the new rates returning the topmost clock that has to be
1913 static struct clk_core *clk_calc_new_rates(struct clk_core *core,
1916 struct clk_core *top = core;
1917 struct clk_core *old_parent, *parent;
1918 unsigned long best_parent_rate = 0;
1919 unsigned long new_rate;
1920 unsigned long min_rate;
1921 unsigned long max_rate;
1926 if (IS_ERR_OR_NULL(core))
1929 /* save parent rate, if it exists */
1930 parent = old_parent = core->parent;
1932 best_parent_rate = parent->rate;
1934 clk_core_get_boundaries(core, &min_rate, &max_rate);
1936 /* find the closest rate and parent clk/rate */
1937 if (clk_core_can_round(core)) {
1938 struct clk_rate_request req;
1941 req.min_rate = min_rate;
1942 req.max_rate = max_rate;
1944 clk_core_init_rate_req(core, &req);
1946 ret = clk_core_determine_round_nolock(core, &req);
1950 best_parent_rate = req.best_parent_rate;
1951 new_rate = req.rate;
1952 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
1954 if (new_rate < min_rate || new_rate > max_rate)
1956 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
1957 /* pass-through clock without adjustable parent */
1958 core->new_rate = core->rate;
1961 /* pass-through clock with adjustable parent */
1962 top = clk_calc_new_rates(parent, rate);
1963 new_rate = parent->new_rate;
1967 /* some clocks must be gated to change parent */
1968 if (parent != old_parent &&
1969 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
1970 pr_debug("%s: %s not gated but wants to reparent\n",
1971 __func__, core->name);
1975 /* try finding the new parent index */
1976 if (parent && core->num_parents > 1) {
1977 p_index = clk_fetch_parent_index(core, parent);
1979 pr_debug("%s: clk %s can not be parent of clk %s\n",
1980 __func__, parent->name, core->name);
1985 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
1986 best_parent_rate != parent->rate)
1987 top = clk_calc_new_rates(parent, best_parent_rate);
1990 clk_calc_subtree(core, new_rate, parent, p_index);
1996 * Notify about rate changes in a subtree. Always walk down the whole tree
1997 * so that in case of an error we can walk down the whole tree again and
2000 static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
2001 unsigned long event)
2003 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
2004 int ret = NOTIFY_DONE;
2006 if (core->rate == core->new_rate)
2009 if (core->notifier_count) {
2010 ret = __clk_notify(core, event, core->rate, core->new_rate);
2011 if (ret & NOTIFY_STOP_MASK)
2015 hlist_for_each_entry(child, &core->children, child_node) {
2016 /* Skip children who will be reparented to another clock */
2017 if (child->new_parent && child->new_parent != core)
2019 tmp_clk = clk_propagate_rate_change(child, event);
2024 /* handle the new child who might not be in core->children yet */
2025 if (core->new_child) {
2026 tmp_clk = clk_propagate_rate_change(core->new_child, event);
2035 * walk down a subtree and set the new rates notifying the rate
2038 static void clk_change_rate(struct clk_core *core)
2040 struct clk_core *child;
2041 struct hlist_node *tmp;
2042 unsigned long old_rate;
2043 unsigned long best_parent_rate = 0;
2044 bool skip_set_rate = false;
2045 struct clk_core *old_parent;
2046 struct clk_core *parent = NULL;
2048 old_rate = core->rate;
2050 if (core->new_parent) {
2051 parent = core->new_parent;
2052 best_parent_rate = core->new_parent->rate;
2053 } else if (core->parent) {
2054 parent = core->parent;
2055 best_parent_rate = core->parent->rate;
2058 if (clk_pm_runtime_get(core))
2061 if (core->flags & CLK_SET_RATE_UNGATE) {
2062 unsigned long flags;
2064 clk_core_prepare(core);
2065 flags = clk_enable_lock();
2066 clk_core_enable(core);
2067 clk_enable_unlock(flags);
2070 if (core->new_parent && core->new_parent != core->parent) {
2071 old_parent = __clk_set_parent_before(core, core->new_parent);
2072 trace_clk_set_parent(core, core->new_parent);
2074 if (core->ops->set_rate_and_parent) {
2075 skip_set_rate = true;
2076 core->ops->set_rate_and_parent(core->hw, core->new_rate,
2078 core->new_parent_index);
2079 } else if (core->ops->set_parent) {
2080 core->ops->set_parent(core->hw, core->new_parent_index);
2083 trace_clk_set_parent_complete(core, core->new_parent);
2084 __clk_set_parent_after(core, core->new_parent, old_parent);
2087 if (core->flags & CLK_OPS_PARENT_ENABLE)
2088 clk_core_prepare_enable(parent);
2090 trace_clk_set_rate(core, core->new_rate);
2092 if (!skip_set_rate && core->ops->set_rate)
2093 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
2095 trace_clk_set_rate_complete(core, core->new_rate);
2097 core->rate = clk_recalc(core, best_parent_rate);
2099 if (core->flags & CLK_SET_RATE_UNGATE) {
2100 unsigned long flags;
2102 flags = clk_enable_lock();
2103 clk_core_disable(core);
2104 clk_enable_unlock(flags);
2105 clk_core_unprepare(core);
2108 if (core->flags & CLK_OPS_PARENT_ENABLE)
2109 clk_core_disable_unprepare(parent);
2111 if (core->notifier_count && old_rate != core->rate)
2112 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
2114 if (core->flags & CLK_RECALC_NEW_RATES)
2115 (void)clk_calc_new_rates(core, core->new_rate);
2118 * Use safe iteration, as change_rate can actually swap parents
2119 * for certain clock types.
2121 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
2122 /* Skip children who will be reparented to another clock */
2123 if (child->new_parent && child->new_parent != core)
2125 clk_change_rate(child);
2128 /* handle the new child who might not be in core->children yet */
2129 if (core->new_child)
2130 clk_change_rate(core->new_child);
2132 clk_pm_runtime_put(core);
2135 static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
2136 unsigned long req_rate)
2139 struct clk_rate_request req;
2141 lockdep_assert_held(&prepare_lock);
2146 /* simulate what the rate would be if it could be freely set */
2147 cnt = clk_core_rate_nuke_protect(core);
2151 clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
2152 req.rate = req_rate;
2154 ret = clk_core_round_rate_nolock(core, &req);
2156 /* restore the protection */
2157 clk_core_rate_restore_protect(core, cnt);
2159 return ret ? 0 : req.rate;
2162 static int clk_core_set_rate_nolock(struct clk_core *core,
2163 unsigned long req_rate)
2165 struct clk_core *top, *fail_clk;
2172 rate = clk_core_req_round_rate_nolock(core, req_rate);
2174 /* bail early if nothing to do */
2175 if (rate == clk_core_get_rate_nolock(core))
2178 /* fail on a direct rate set of a protected provider */
2179 if (clk_core_rate_is_protected(core))
2182 /* calculate new rates and get the topmost changed clock */
2183 top = clk_calc_new_rates(core, req_rate);
2187 ret = clk_pm_runtime_get(core);
2191 /* notify that we are about to change rates */
2192 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
2194 pr_debug("%s: failed to set %s rate\n", __func__,
2196 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
2201 /* change the rates */
2202 clk_change_rate(top);
2204 core->req_rate = req_rate;
2206 clk_pm_runtime_put(core);
2212 * clk_set_rate - specify a new rate for clk
2213 * @clk: the clk whose rate is being changed
2214 * @rate: the new rate for clk
2216 * In the simplest case clk_set_rate will only adjust the rate of clk.
2218 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
2219 * propagate up to clk's parent; whether or not this happens depends on the
2220 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
2221 * after calling .round_rate then upstream parent propagation is ignored. If
2222 * *parent_rate comes back with a new rate for clk's parent then we propagate
2223 * up to clk's parent and set its rate. Upward propagation will continue
2224 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
2225 * .round_rate stops requesting changes to clk's parent_rate.
2227 * Rate changes are accomplished via tree traversal that also recalculates the
2228 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
2230 * Returns 0 on success, -EERROR otherwise.
2232 int clk_set_rate(struct clk *clk, unsigned long rate)
2239 /* prevent racing with updates to the clock topology */
2242 if (clk->exclusive_count)
2243 clk_core_rate_unprotect(clk->core);
2245 ret = clk_core_set_rate_nolock(clk->core, rate);
2247 if (clk->exclusive_count)
2248 clk_core_rate_protect(clk->core);
2250 clk_prepare_unlock();
2254 EXPORT_SYMBOL_GPL(clk_set_rate);
2257 * clk_set_rate_exclusive - specify a new rate and get exclusive control
2258 * @clk: the clk whose rate is being changed
2259 * @rate: the new rate for clk
2261 * This is a combination of clk_set_rate() and clk_rate_exclusive_get()
2262 * within a critical section
2264 * This can be used initially to ensure that at least 1 consumer is
2265 * satisfied when several consumers are competing for exclusivity over the
2266 * same clock provider.
2268 * The exclusivity is not applied if setting the rate failed.
2270 * Calls to clk_rate_exclusive_get() should be balanced with calls to
2271 * clk_rate_exclusive_put().
2273 * Returns 0 on success, -EERROR otherwise.
2275 int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
2282 /* prevent racing with updates to the clock topology */
2286 * The temporary protection removal is not here, on purpose
2287 * This function is meant to be used instead of clk_rate_protect,
2288 * so before the consumer code path protect the clock provider
2291 ret = clk_core_set_rate_nolock(clk->core, rate);
2293 clk_core_rate_protect(clk->core);
2294 clk->exclusive_count++;
2297 clk_prepare_unlock();
2301 EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
2304 * clk_set_rate_range - set a rate range for a clock source
2305 * @clk: clock source
2306 * @min: desired minimum clock rate in Hz, inclusive
2307 * @max: desired maximum clock rate in Hz, inclusive
2309 * Returns success (0) or negative errno.
2311 int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
2314 unsigned long old_min, old_max, rate;
2320 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
2321 __func__, clk->core->name, clk->dev_id, clk->con_id,
2328 if (clk->exclusive_count)
2329 clk_core_rate_unprotect(clk->core);
2331 /* Save the current values in case we need to rollback the change */
2332 old_min = clk->min_rate;
2333 old_max = clk->max_rate;
2334 clk->min_rate = min;
2335 clk->max_rate = max;
2337 if (!clk_core_check_boundaries(clk->core, min, max)) {
2342 rate = clk_core_get_rate_nolock(clk->core);
2343 if (rate < min || rate > max) {
2346 * We are in bit of trouble here, current rate is outside the
2347 * the requested range. We are going try to request appropriate
2348 * range boundary but there is a catch. It may fail for the
2349 * usual reason (clock broken, clock protected, etc) but also
2351 * - round_rate() was not favorable and fell on the wrong
2352 * side of the boundary
2353 * - the determine_rate() callback does not really check for
2354 * this corner case when determining the rate
2362 ret = clk_core_set_rate_nolock(clk->core, rate);
2364 /* rollback the changes */
2365 clk->min_rate = old_min;
2366 clk->max_rate = old_max;
2371 if (clk->exclusive_count)
2372 clk_core_rate_protect(clk->core);
2374 clk_prepare_unlock();
2378 EXPORT_SYMBOL_GPL(clk_set_rate_range);
2381 * clk_set_min_rate - set a minimum clock rate for a clock source
2382 * @clk: clock source
2383 * @rate: desired minimum clock rate in Hz, inclusive
2385 * Returns success (0) or negative errno.
2387 int clk_set_min_rate(struct clk *clk, unsigned long rate)
2392 return clk_set_rate_range(clk, rate, clk->max_rate);
2394 EXPORT_SYMBOL_GPL(clk_set_min_rate);
2397 * clk_set_max_rate - set a maximum clock rate for a clock source
2398 * @clk: clock source
2399 * @rate: desired maximum clock rate in Hz, inclusive
2401 * Returns success (0) or negative errno.
2403 int clk_set_max_rate(struct clk *clk, unsigned long rate)
2408 return clk_set_rate_range(clk, clk->min_rate, rate);
2410 EXPORT_SYMBOL_GPL(clk_set_max_rate);
2413 * clk_get_parent - return the parent of a clk
2414 * @clk: the clk whose parent gets returned
2416 * Simply returns clk->parent. Returns NULL if clk is NULL.
2418 struct clk *clk_get_parent(struct clk *clk)
2426 /* TODO: Create a per-user clk and change callers to call clk_put */
2427 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
2428 clk_prepare_unlock();
2432 EXPORT_SYMBOL_GPL(clk_get_parent);
2434 static struct clk_core *__clk_init_parent(struct clk_core *core)
2438 if (core->num_parents > 1 && core->ops->get_parent)
2439 index = core->ops->get_parent(core->hw);
2441 return clk_core_get_parent_by_index(core, index);
2444 static void clk_core_reparent(struct clk_core *core,
2445 struct clk_core *new_parent)
2447 clk_reparent(core, new_parent);
2448 __clk_recalc_accuracies(core);
2449 __clk_recalc_rates(core, POST_RATE_CHANGE);
2452 void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
2457 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2461 * clk_has_parent - check if a clock is a possible parent for another
2462 * @clk: clock source
2463 * @parent: parent clock source
2465 * This function can be used in drivers that need to check that a clock can be
2466 * the parent of another without actually changing the parent.
2468 * Returns true if @parent is a possible parent for @clk, false otherwise.
2470 bool clk_has_parent(struct clk *clk, struct clk *parent)
2472 struct clk_core *core, *parent_core;
2475 /* NULL clocks should be nops, so return success if either is NULL. */
2476 if (!clk || !parent)
2480 parent_core = parent->core;
2482 /* Optimize for the case where the parent is already the parent. */
2483 if (core->parent == parent_core)
2486 for (i = 0; i < core->num_parents; i++)
2487 if (!strcmp(core->parents[i].name, parent_core->name))
2492 EXPORT_SYMBOL_GPL(clk_has_parent);
2494 static int clk_core_set_parent_nolock(struct clk_core *core,
2495 struct clk_core *parent)
2499 unsigned long p_rate = 0;
2501 lockdep_assert_held(&prepare_lock);
2506 if (core->parent == parent)
2509 /* verify ops for multi-parent clks */
2510 if (core->num_parents > 1 && !core->ops->set_parent)
2513 /* check that we are allowed to re-parent if the clock is in use */
2514 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
2517 if (clk_core_rate_is_protected(core))
2520 /* try finding the new parent index */
2522 p_index = clk_fetch_parent_index(core, parent);
2524 pr_debug("%s: clk %s can not be parent of clk %s\n",
2525 __func__, parent->name, core->name);
2528 p_rate = parent->rate;
2531 ret = clk_pm_runtime_get(core);
2535 /* propagate PRE_RATE_CHANGE notifications */
2536 ret = __clk_speculate_rates(core, p_rate);
2538 /* abort if a driver objects */
2539 if (ret & NOTIFY_STOP_MASK)
2542 /* do the re-parent */
2543 ret = __clk_set_parent(core, parent, p_index);
2545 /* propagate rate an accuracy recalculation accordingly */
2547 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
2549 __clk_recalc_rates(core, POST_RATE_CHANGE);
2550 __clk_recalc_accuracies(core);
2554 clk_pm_runtime_put(core);
2559 int clk_hw_set_parent(struct clk_hw *hw, struct clk_hw *parent)
2561 return clk_core_set_parent_nolock(hw->core, parent->core);
2563 EXPORT_SYMBOL_GPL(clk_hw_set_parent);
2566 * clk_set_parent - switch the parent of a mux clk
2567 * @clk: the mux clk whose input we are switching
2568 * @parent: the new input to clk
2570 * Re-parent clk to use parent as its new input source. If clk is in
2571 * prepared state, the clk will get enabled for the duration of this call. If
2572 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2573 * that, the reparenting is glitchy in hardware, etc), use the
2574 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2576 * After successfully changing clk's parent clk_set_parent will update the
2577 * clk topology, sysfs topology and propagate rate recalculation via
2578 * __clk_recalc_rates.
2580 * Returns 0 on success, -EERROR otherwise.
2582 int clk_set_parent(struct clk *clk, struct clk *parent)
2591 if (clk->exclusive_count)
2592 clk_core_rate_unprotect(clk->core);
2594 ret = clk_core_set_parent_nolock(clk->core,
2595 parent ? parent->core : NULL);
2597 if (clk->exclusive_count)
2598 clk_core_rate_protect(clk->core);
2600 clk_prepare_unlock();
2604 EXPORT_SYMBOL_GPL(clk_set_parent);
2606 static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
2610 lockdep_assert_held(&prepare_lock);
2615 if (clk_core_rate_is_protected(core))
2618 trace_clk_set_phase(core, degrees);
2620 if (core->ops->set_phase) {
2621 ret = core->ops->set_phase(core->hw, degrees);
2623 core->phase = degrees;
2626 trace_clk_set_phase_complete(core, degrees);
2632 * clk_set_phase - adjust the phase shift of a clock signal
2633 * @clk: clock signal source
2634 * @degrees: number of degrees the signal is shifted
2636 * Shifts the phase of a clock signal by the specified
2637 * degrees. Returns 0 on success, -EERROR otherwise.
2639 * This function makes no distinction about the input or reference
2640 * signal that we adjust the clock signal phase against. For example
2641 * phase locked-loop clock signal generators we may shift phase with
2642 * respect to feedback clock signal input, but for other cases the
2643 * clock phase may be shifted with respect to some other, unspecified
2646 * Additionally the concept of phase shift does not propagate through
2647 * the clock tree hierarchy, which sets it apart from clock rates and
2648 * clock accuracy. A parent clock phase attribute does not have an
2649 * impact on the phase attribute of a child clock.
2651 int clk_set_phase(struct clk *clk, int degrees)
2658 /* sanity check degrees */
2665 if (clk->exclusive_count)
2666 clk_core_rate_unprotect(clk->core);
2668 ret = clk_core_set_phase_nolock(clk->core, degrees);
2670 if (clk->exclusive_count)
2671 clk_core_rate_protect(clk->core);
2673 clk_prepare_unlock();
2677 EXPORT_SYMBOL_GPL(clk_set_phase);
2679 static int clk_core_get_phase(struct clk_core *core)
2683 lockdep_assert_held(&prepare_lock);
2684 if (!core->ops->get_phase)
2687 /* Always try to update cached phase if possible */
2688 ret = core->ops->get_phase(core->hw);
2696 * clk_get_phase - return the phase shift of a clock signal
2697 * @clk: clock signal source
2699 * Returns the phase shift of a clock node in degrees, otherwise returns
2702 int clk_get_phase(struct clk *clk)
2710 ret = clk_core_get_phase(clk->core);
2711 clk_prepare_unlock();
2715 EXPORT_SYMBOL_GPL(clk_get_phase);
2717 static void clk_core_reset_duty_cycle_nolock(struct clk_core *core)
2719 /* Assume a default value of 50% */
2724 static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core);
2726 static int clk_core_update_duty_cycle_nolock(struct clk_core *core)
2728 struct clk_duty *duty = &core->duty;
2731 if (!core->ops->get_duty_cycle)
2732 return clk_core_update_duty_cycle_parent_nolock(core);
2734 ret = core->ops->get_duty_cycle(core->hw, duty);
2738 /* Don't trust the clock provider too much */
2739 if (duty->den == 0 || duty->num > duty->den) {
2747 clk_core_reset_duty_cycle_nolock(core);
2751 static int clk_core_update_duty_cycle_parent_nolock(struct clk_core *core)
2756 core->flags & CLK_DUTY_CYCLE_PARENT) {
2757 ret = clk_core_update_duty_cycle_nolock(core->parent);
2758 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2760 clk_core_reset_duty_cycle_nolock(core);
2766 static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2767 struct clk_duty *duty);
2769 static int clk_core_set_duty_cycle_nolock(struct clk_core *core,
2770 struct clk_duty *duty)
2774 lockdep_assert_held(&prepare_lock);
2776 if (clk_core_rate_is_protected(core))
2779 trace_clk_set_duty_cycle(core, duty);
2781 if (!core->ops->set_duty_cycle)
2782 return clk_core_set_duty_cycle_parent_nolock(core, duty);
2784 ret = core->ops->set_duty_cycle(core->hw, duty);
2786 memcpy(&core->duty, duty, sizeof(*duty));
2788 trace_clk_set_duty_cycle_complete(core, duty);
2793 static int clk_core_set_duty_cycle_parent_nolock(struct clk_core *core,
2794 struct clk_duty *duty)
2799 core->flags & (CLK_DUTY_CYCLE_PARENT | CLK_SET_RATE_PARENT)) {
2800 ret = clk_core_set_duty_cycle_nolock(core->parent, duty);
2801 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty));
2808 * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
2809 * @clk: clock signal source
2810 * @num: numerator of the duty cycle ratio to be applied
2811 * @den: denominator of the duty cycle ratio to be applied
2813 * Apply the duty cycle ratio if the ratio is valid and the clock can
2814 * perform this operation
2816 * Returns (0) on success, a negative errno otherwise.
2818 int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den)
2821 struct clk_duty duty;
2826 /* sanity check the ratio */
2827 if (den == 0 || num > den)
2835 if (clk->exclusive_count)
2836 clk_core_rate_unprotect(clk->core);
2838 ret = clk_core_set_duty_cycle_nolock(clk->core, &duty);
2840 if (clk->exclusive_count)
2841 clk_core_rate_protect(clk->core);
2843 clk_prepare_unlock();
2847 EXPORT_SYMBOL_GPL(clk_set_duty_cycle);
2849 static int clk_core_get_scaled_duty_cycle(struct clk_core *core,
2852 struct clk_duty *duty = &core->duty;
2857 ret = clk_core_update_duty_cycle_nolock(core);
2859 ret = mult_frac(scale, duty->num, duty->den);
2861 clk_prepare_unlock();
2867 * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal
2868 * @clk: clock signal source
2869 * @scale: scaling factor to be applied to represent the ratio as an integer
2871 * Returns the duty cycle ratio of a clock node multiplied by the provided
2872 * scaling factor, or negative errno on error.
2874 int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale)
2879 return clk_core_get_scaled_duty_cycle(clk->core, scale);
2881 EXPORT_SYMBOL_GPL(clk_get_scaled_duty_cycle);
2884 * clk_is_match - check if two clk's point to the same hardware clock
2885 * @p: clk compared against q
2886 * @q: clk compared against p
2888 * Returns true if the two struct clk pointers both point to the same hardware
2889 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2890 * share the same struct clk_core object.
2892 * Returns false otherwise. Note that two NULL clks are treated as matching.
2894 bool clk_is_match(const struct clk *p, const struct clk *q)
2896 /* trivial case: identical struct clk's or both NULL */
2900 /* true if clk->core pointers match. Avoid dereferencing garbage */
2901 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2902 if (p->core == q->core)
2907 EXPORT_SYMBOL_GPL(clk_is_match);
2909 /*** debugfs support ***/
2911 #ifdef CONFIG_DEBUG_FS
2912 #include <linux/debugfs.h>
2914 static struct dentry *rootdir;
2915 static int inited = 0;
2916 static DEFINE_MUTEX(clk_debug_lock);
2917 static HLIST_HEAD(clk_debug_list);
2919 static struct hlist_head *orphan_list[] = {
2924 static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2929 seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu ",
2931 30 - level * 3, c->name,
2932 c->enable_count, c->prepare_count, c->protect_count,
2933 clk_core_get_rate(c), clk_core_get_accuracy(c));
2935 phase = clk_core_get_phase(c);
2937 seq_printf(s, "%5d", phase);
2939 seq_puts(s, "-----");
2941 seq_printf(s, " %6d\n", clk_core_get_scaled_duty_cycle(c, 100000));
2944 static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2947 struct clk_core *child;
2949 clk_summary_show_one(s, c, level);
2951 hlist_for_each_entry(child, &c->children, child_node)
2952 clk_summary_show_subtree(s, child, level + 1);
2955 static int clk_summary_show(struct seq_file *s, void *data)
2958 struct hlist_head **lists = (struct hlist_head **)s->private;
2960 seq_puts(s, " enable prepare protect duty\n");
2961 seq_puts(s, " clock count count count rate accuracy phase cycle\n");
2962 seq_puts(s, "---------------------------------------------------------------------------------------------\n");
2966 for (; *lists; lists++)
2967 hlist_for_each_entry(c, *lists, child_node)
2968 clk_summary_show_subtree(s, c, 0);
2970 clk_prepare_unlock();
2974 DEFINE_SHOW_ATTRIBUTE(clk_summary);
2976 static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2979 unsigned long min_rate, max_rate;
2981 clk_core_get_boundaries(c, &min_rate, &max_rate);
2983 /* This should be JSON format, i.e. elements separated with a comma */
2984 seq_printf(s, "\"%s\": { ", c->name);
2985 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2986 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
2987 seq_printf(s, "\"protect_count\": %d,", c->protect_count);
2988 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2989 seq_printf(s, "\"min_rate\": %lu,", min_rate);
2990 seq_printf(s, "\"max_rate\": %lu,", max_rate);
2991 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
2992 phase = clk_core_get_phase(c);
2994 seq_printf(s, "\"phase\": %d,", phase);
2995 seq_printf(s, "\"duty_cycle\": %u",
2996 clk_core_get_scaled_duty_cycle(c, 100000));
2999 static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
3001 struct clk_core *child;
3003 clk_dump_one(s, c, level);
3005 hlist_for_each_entry(child, &c->children, child_node) {
3007 clk_dump_subtree(s, child, level + 1);
3013 static int clk_dump_show(struct seq_file *s, void *data)
3016 bool first_node = true;
3017 struct hlist_head **lists = (struct hlist_head **)s->private;
3022 for (; *lists; lists++) {
3023 hlist_for_each_entry(c, *lists, child_node) {
3027 clk_dump_subtree(s, c, 0);
3031 clk_prepare_unlock();
3036 DEFINE_SHOW_ATTRIBUTE(clk_dump);
3038 static const struct {
3042 #define ENTRY(f) { f, #f }
3043 ENTRY(CLK_SET_RATE_GATE),
3044 ENTRY(CLK_SET_PARENT_GATE),
3045 ENTRY(CLK_SET_RATE_PARENT),
3046 ENTRY(CLK_IGNORE_UNUSED),
3047 ENTRY(CLK_GET_RATE_NOCACHE),
3048 ENTRY(CLK_SET_RATE_NO_REPARENT),
3049 ENTRY(CLK_GET_ACCURACY_NOCACHE),
3050 ENTRY(CLK_RECALC_NEW_RATES),
3051 ENTRY(CLK_SET_RATE_UNGATE),
3052 ENTRY(CLK_IS_CRITICAL),
3053 ENTRY(CLK_OPS_PARENT_ENABLE),
3054 ENTRY(CLK_DUTY_CYCLE_PARENT),
3058 static int clk_flags_show(struct seq_file *s, void *data)
3060 struct clk_core *core = s->private;
3061 unsigned long flags = core->flags;
3064 for (i = 0; flags && i < ARRAY_SIZE(clk_flags); i++) {
3065 if (flags & clk_flags[i].flag) {
3066 seq_printf(s, "%s\n", clk_flags[i].name);
3067 flags &= ~clk_flags[i].flag;
3072 seq_printf(s, "0x%lx\n", flags);
3077 DEFINE_SHOW_ATTRIBUTE(clk_flags);
3079 static void possible_parent_show(struct seq_file *s, struct clk_core *core,
3080 unsigned int i, char terminator)
3082 struct clk_core *parent;
3085 * Go through the following options to fetch a parent's name.
3087 * 1. Fetch the registered parent clock and use its name
3088 * 2. Use the global (fallback) name if specified
3089 * 3. Use the local fw_name if provided
3090 * 4. Fetch parent clock's clock-output-name if DT index was set
3092 * This may still fail in some cases, such as when the parent is
3093 * specified directly via a struct clk_hw pointer, but it isn't
3096 parent = clk_core_get_parent_by_index(core, i);
3098 seq_puts(s, parent->name);
3099 else if (core->parents[i].name)
3100 seq_puts(s, core->parents[i].name);
3101 else if (core->parents[i].fw_name)
3102 seq_printf(s, "<%s>(fw)", core->parents[i].fw_name);
3103 else if (core->parents[i].index >= 0)
3105 of_clk_get_parent_name(core->of_node,
3106 core->parents[i].index));
3108 seq_puts(s, "(missing)");
3110 seq_putc(s, terminator);
3113 static int possible_parents_show(struct seq_file *s, void *data)
3115 struct clk_core *core = s->private;
3118 for (i = 0; i < core->num_parents - 1; i++)
3119 possible_parent_show(s, core, i, ' ');
3121 possible_parent_show(s, core, i, '\n');
3125 DEFINE_SHOW_ATTRIBUTE(possible_parents);
3127 static int current_parent_show(struct seq_file *s, void *data)
3129 struct clk_core *core = s->private;
3132 seq_printf(s, "%s\n", core->parent->name);
3136 DEFINE_SHOW_ATTRIBUTE(current_parent);
3138 static int clk_duty_cycle_show(struct seq_file *s, void *data)
3140 struct clk_core *core = s->private;
3141 struct clk_duty *duty = &core->duty;
3143 seq_printf(s, "%u/%u\n", duty->num, duty->den);
3147 DEFINE_SHOW_ATTRIBUTE(clk_duty_cycle);
3149 static int clk_min_rate_show(struct seq_file *s, void *data)
3151 struct clk_core *core = s->private;
3152 unsigned long min_rate, max_rate;
3155 clk_core_get_boundaries(core, &min_rate, &max_rate);
3156 clk_prepare_unlock();
3157 seq_printf(s, "%lu\n", min_rate);
3161 DEFINE_SHOW_ATTRIBUTE(clk_min_rate);
3163 static int clk_max_rate_show(struct seq_file *s, void *data)
3165 struct clk_core *core = s->private;
3166 unsigned long min_rate, max_rate;
3169 clk_core_get_boundaries(core, &min_rate, &max_rate);
3170 clk_prepare_unlock();
3171 seq_printf(s, "%lu\n", max_rate);
3175 DEFINE_SHOW_ATTRIBUTE(clk_max_rate);
3177 static void clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
3179 struct dentry *root;
3181 if (!core || !pdentry)
3184 root = debugfs_create_dir(core->name, pdentry);
3185 core->dentry = root;
3187 debugfs_create_ulong("clk_rate", 0444, root, &core->rate);
3188 debugfs_create_file("clk_min_rate", 0444, root, core, &clk_min_rate_fops);
3189 debugfs_create_file("clk_max_rate", 0444, root, core, &clk_max_rate_fops);
3190 debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy);
3191 debugfs_create_u32("clk_phase", 0444, root, &core->phase);
3192 debugfs_create_file("clk_flags", 0444, root, core, &clk_flags_fops);
3193 debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count);
3194 debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count);
3195 debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count);
3196 debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count);
3197 debugfs_create_file("clk_duty_cycle", 0444, root, core,
3198 &clk_duty_cycle_fops);
3200 if (core->num_parents > 0)
3201 debugfs_create_file("clk_parent", 0444, root, core,
3202 ¤t_parent_fops);
3204 if (core->num_parents > 1)
3205 debugfs_create_file("clk_possible_parents", 0444, root, core,
3206 &possible_parents_fops);
3208 if (core->ops->debug_init)
3209 core->ops->debug_init(core->hw, core->dentry);
3213 * clk_debug_register - add a clk node to the debugfs clk directory
3214 * @core: the clk being added to the debugfs clk directory
3216 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
3217 * initialized. Otherwise it bails out early since the debugfs clk directory
3218 * will be created lazily by clk_debug_init as part of a late_initcall.
3220 static void clk_debug_register(struct clk_core *core)
3222 mutex_lock(&clk_debug_lock);
3223 hlist_add_head(&core->debug_node, &clk_debug_list);
3225 clk_debug_create_one(core, rootdir);
3226 mutex_unlock(&clk_debug_lock);
3230 * clk_debug_unregister - remove a clk node from the debugfs clk directory
3231 * @core: the clk being removed from the debugfs clk directory
3233 * Dynamically removes a clk and all its child nodes from the
3234 * debugfs clk directory if clk->dentry points to debugfs created by
3235 * clk_debug_register in __clk_core_init.
3237 static void clk_debug_unregister(struct clk_core *core)
3239 mutex_lock(&clk_debug_lock);
3240 hlist_del_init(&core->debug_node);
3241 debugfs_remove_recursive(core->dentry);
3242 core->dentry = NULL;
3243 mutex_unlock(&clk_debug_lock);
3247 * clk_debug_init - lazily populate the debugfs clk directory
3249 * clks are often initialized very early during boot before memory can be
3250 * dynamically allocated and well before debugfs is setup. This function
3251 * populates the debugfs clk directory once at boot-time when we know that
3252 * debugfs is setup. It should only be called once at boot-time, all other clks
3253 * added dynamically will be done so with clk_debug_register.
3255 static int __init clk_debug_init(void)
3257 struct clk_core *core;
3259 rootdir = debugfs_create_dir("clk", NULL);
3261 debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
3263 debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
3265 debugfs_create_file("clk_orphan_summary", 0444, rootdir, &orphan_list,
3267 debugfs_create_file("clk_orphan_dump", 0444, rootdir, &orphan_list,
3270 mutex_lock(&clk_debug_lock);
3271 hlist_for_each_entry(core, &clk_debug_list, debug_node)
3272 clk_debug_create_one(core, rootdir);
3275 mutex_unlock(&clk_debug_lock);
3279 late_initcall(clk_debug_init);
3281 static inline void clk_debug_register(struct clk_core *core) { }
3282 static inline void clk_debug_reparent(struct clk_core *core,
3283 struct clk_core *new_parent)
3286 static inline void clk_debug_unregister(struct clk_core *core)
3291 static void clk_core_reparent_orphans_nolock(void)
3293 struct clk_core *orphan;
3294 struct hlist_node *tmp2;
3297 * walk the list of orphan clocks and reparent any that newly finds a
3300 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
3301 struct clk_core *parent = __clk_init_parent(orphan);
3304 * We need to use __clk_set_parent_before() and _after() to
3305 * to properly migrate any prepare/enable count of the orphan
3306 * clock. This is important for CLK_IS_CRITICAL clocks, which
3307 * are enabled during init but might not have a parent yet.
3310 /* update the clk tree topology */
3311 __clk_set_parent_before(orphan, parent);
3312 __clk_set_parent_after(orphan, parent, NULL);
3313 __clk_recalc_accuracies(orphan);
3314 __clk_recalc_rates(orphan, 0);
3317 * __clk_init_parent() will set the initial req_rate to
3318 * 0 if the clock doesn't have clk_ops::recalc_rate and
3319 * is an orphan when it's registered.
3321 * 'req_rate' is used by clk_set_rate_range() and
3322 * clk_put() to trigger a clk_set_rate() call whenever
3323 * the boundaries are modified. Let's make sure
3324 * 'req_rate' is set to something non-zero so that
3325 * clk_set_rate_range() doesn't drop the frequency.
3327 orphan->req_rate = orphan->rate;
3333 * __clk_core_init - initialize the data structures in a struct clk_core
3334 * @core: clk_core being initialized
3336 * Initializes the lists in struct clk_core, queries the hardware for the
3337 * parent and rate and sets them both.
3339 static int __clk_core_init(struct clk_core *core)
3350 * Set hw->core after grabbing the prepare_lock to synchronize with
3351 * callers of clk_core_fill_parent_index() where we treat hw->core
3352 * being NULL as the clk not being registered yet. This is crucial so
3353 * that clks aren't parented until their parent is fully registered.
3355 core->hw->core = core;
3357 ret = clk_pm_runtime_get(core);
3361 /* check to see if a clock with this name is already registered */
3362 if (clk_core_lookup(core->name)) {
3363 pr_debug("%s: clk %s already initialized\n",
3364 __func__, core->name);
3369 /* check that clk_ops are sane. See Documentation/driver-api/clk.rst */
3370 if (core->ops->set_rate &&
3371 !((core->ops->round_rate || core->ops->determine_rate) &&
3372 core->ops->recalc_rate)) {
3373 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
3374 __func__, core->name);
3379 if (core->ops->set_parent && !core->ops->get_parent) {
3380 pr_err("%s: %s must implement .get_parent & .set_parent\n",
3381 __func__, core->name);
3386 if (core->num_parents > 1 && !core->ops->get_parent) {
3387 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
3388 __func__, core->name);
3393 if (core->ops->set_rate_and_parent &&
3394 !(core->ops->set_parent && core->ops->set_rate)) {
3395 pr_err("%s: %s must implement .set_parent & .set_rate\n",
3396 __func__, core->name);
3402 * optional platform-specific magic
3404 * The .init callback is not used by any of the basic clock types, but
3405 * exists for weird hardware that must perform initialization magic.
3406 * Please consider other ways of solving initialization problems before
3407 * using this callback, as its use is discouraged.
3409 * If it exist, this callback should called before any other callback of
3412 if (core->ops->init)
3413 core->ops->init(core->hw);
3416 core->parent = __clk_init_parent(core);
3419 * Populate core->parent if parent has already been clk_core_init'd. If
3420 * parent has not yet been clk_core_init'd then place clk in the orphan
3421 * list. If clk doesn't have any parents then place it in the root
3424 * Every time a new clk is clk_init'd then we walk the list of orphan
3425 * clocks and re-parent any that are children of the clock currently
3429 hlist_add_head(&core->child_node,
3430 &core->parent->children);
3431 core->orphan = core->parent->orphan;
3432 } else if (!core->num_parents) {
3433 hlist_add_head(&core->child_node, &clk_root_list);
3434 core->orphan = false;
3436 hlist_add_head(&core->child_node, &clk_orphan_list);
3437 core->orphan = true;
3441 * Set clk's accuracy. The preferred method is to use
3442 * .recalc_accuracy. For simple clocks and lazy developers the default
3443 * fallback is to use the parent's accuracy. If a clock doesn't have a
3444 * parent (or is orphaned) then accuracy is set to zero (perfect
3447 if (core->ops->recalc_accuracy)
3448 core->accuracy = core->ops->recalc_accuracy(core->hw,
3449 __clk_get_accuracy(core->parent));
3450 else if (core->parent)
3451 core->accuracy = core->parent->accuracy;
3456 * Set clk's phase by clk_core_get_phase() caching the phase.
3457 * Since a phase is by definition relative to its parent, just
3458 * query the current clock phase, or just assume it's in phase.
3460 clk_core_get_phase(core);
3463 * Set clk's duty cycle.
3465 clk_core_update_duty_cycle_nolock(core);
3468 * Set clk's rate. The preferred method is to use .recalc_rate. For
3469 * simple clocks and lazy developers the default fallback is to use the
3470 * parent's rate. If a clock doesn't have a parent (or is orphaned)
3471 * then rate is set to zero.
3473 if (core->ops->recalc_rate)
3474 rate = core->ops->recalc_rate(core->hw,
3475 clk_core_get_rate_nolock(core->parent));
3476 else if (core->parent)
3477 rate = core->parent->rate;
3480 core->rate = core->req_rate = rate;
3483 * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
3484 * don't get accidentally disabled when walking the orphan tree and
3485 * reparenting clocks
3487 if (core->flags & CLK_IS_CRITICAL) {
3488 unsigned long flags;
3490 ret = clk_core_prepare(core);
3494 flags = clk_enable_lock();
3495 ret = clk_core_enable(core);
3496 clk_enable_unlock(flags);
3498 clk_core_unprepare(core);
3503 clk_core_reparent_orphans_nolock();
3506 kref_init(&core->ref);
3508 clk_pm_runtime_put(core);
3511 hlist_del_init(&core->child_node);
3512 core->hw->core = NULL;
3515 clk_prepare_unlock();
3518 clk_debug_register(core);
3524 * clk_core_link_consumer - Add a clk consumer to the list of consumers in a clk_core
3525 * @core: clk to add consumer to
3526 * @clk: consumer to link to a clk
3528 static void clk_core_link_consumer(struct clk_core *core, struct clk *clk)
3531 hlist_add_head(&clk->clks_node, &core->clks);
3532 clk_prepare_unlock();
3536 * clk_core_unlink_consumer - Remove a clk consumer from the list of consumers in a clk_core
3537 * @clk: consumer to unlink
3539 static void clk_core_unlink_consumer(struct clk *clk)
3541 lockdep_assert_held(&prepare_lock);
3542 hlist_del(&clk->clks_node);
3546 * alloc_clk - Allocate a clk consumer, but leave it unlinked to the clk_core
3547 * @core: clk to allocate a consumer for
3548 * @dev_id: string describing device name
3549 * @con_id: connection ID string on device
3551 * Returns: clk consumer left unlinked from the consumer list
3553 static struct clk *alloc_clk(struct clk_core *core, const char *dev_id,
3558 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
3560 return ERR_PTR(-ENOMEM);
3563 clk->dev_id = dev_id;
3564 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
3565 clk->max_rate = ULONG_MAX;
3571 * free_clk - Free a clk consumer
3572 * @clk: clk consumer to free
3574 * Note, this assumes the clk has been unlinked from the clk_core consumer
3577 static void free_clk(struct clk *clk)
3579 kfree_const(clk->con_id);
3584 * clk_hw_create_clk: Allocate and link a clk consumer to a clk_core given
3586 * @dev: clk consumer device
3587 * @hw: clk_hw associated with the clk being consumed
3588 * @dev_id: string describing device name
3589 * @con_id: connection ID string on device
3591 * This is the main function used to create a clk pointer for use by clk
3592 * consumers. It connects a consumer to the clk_core and clk_hw structures
3593 * used by the framework and clk provider respectively.
3595 struct clk *clk_hw_create_clk(struct device *dev, struct clk_hw *hw,
3596 const char *dev_id, const char *con_id)
3599 struct clk_core *core;
3601 /* This is to allow this function to be chained to others */
3602 if (IS_ERR_OR_NULL(hw))
3603 return ERR_CAST(hw);
3606 clk = alloc_clk(core, dev_id, con_id);
3611 if (!try_module_get(core->owner)) {
3613 return ERR_PTR(-ENOENT);
3616 kref_get(&core->ref);
3617 clk_core_link_consumer(core, clk);
3622 static int clk_cpy_name(const char **dst_p, const char *src, bool must_exist)
3632 *dst_p = dst = kstrdup_const(src, GFP_KERNEL);
3639 static int clk_core_populate_parent_map(struct clk_core *core,
3640 const struct clk_init_data *init)
3642 u8 num_parents = init->num_parents;
3643 const char * const *parent_names = init->parent_names;
3644 const struct clk_hw **parent_hws = init->parent_hws;
3645 const struct clk_parent_data *parent_data = init->parent_data;
3647 struct clk_parent_map *parents, *parent;
3653 * Avoid unnecessary string look-ups of clk_core's possible parents by
3654 * having a cache of names/clk_hw pointers to clk_core pointers.
3656 parents = kcalloc(num_parents, sizeof(*parents), GFP_KERNEL);
3657 core->parents = parents;
3661 /* Copy everything over because it might be __initdata */
3662 for (i = 0, parent = parents; i < num_parents; i++, parent++) {
3665 /* throw a WARN if any entries are NULL */
3666 WARN(!parent_names[i],
3667 "%s: invalid NULL in %s's .parent_names\n",
3668 __func__, core->name);
3669 ret = clk_cpy_name(&parent->name, parent_names[i],
3671 } else if (parent_data) {
3672 parent->hw = parent_data[i].hw;
3673 parent->index = parent_data[i].index;
3674 ret = clk_cpy_name(&parent->fw_name,
3675 parent_data[i].fw_name, false);
3677 ret = clk_cpy_name(&parent->name,
3678 parent_data[i].name,
3680 } else if (parent_hws) {
3681 parent->hw = parent_hws[i];
3684 WARN(1, "Must specify parents if num_parents > 0\n");
3689 kfree_const(parents[i].name);
3690 kfree_const(parents[i].fw_name);
3701 static void clk_core_free_parent_map(struct clk_core *core)
3703 int i = core->num_parents;
3705 if (!core->num_parents)
3709 kfree_const(core->parents[i].name);
3710 kfree_const(core->parents[i].fw_name);
3713 kfree(core->parents);
3717 __clk_register(struct device *dev, struct device_node *np, struct clk_hw *hw)
3720 struct clk_core *core;
3721 const struct clk_init_data *init = hw->init;
3724 * The init data is not supposed to be used outside of registration path.
3725 * Set it to NULL so that provider drivers can't use it either and so that
3726 * we catch use of hw->init early on in the core.
3730 core = kzalloc(sizeof(*core), GFP_KERNEL);
3736 core->name = kstrdup_const(init->name, GFP_KERNEL);
3742 if (WARN_ON(!init->ops)) {
3746 core->ops = init->ops;
3748 if (dev && pm_runtime_enabled(dev))
3749 core->rpm_enabled = true;
3752 if (dev && dev->driver)
3753 core->owner = dev->driver->owner;
3755 core->flags = init->flags;
3756 core->num_parents = init->num_parents;
3758 core->max_rate = ULONG_MAX;
3760 ret = clk_core_populate_parent_map(core, init);
3764 INIT_HLIST_HEAD(&core->clks);
3767 * Don't call clk_hw_create_clk() here because that would pin the
3768 * provider module to itself and prevent it from ever being removed.
3770 hw->clk = alloc_clk(core, NULL, NULL);
3771 if (IS_ERR(hw->clk)) {
3772 ret = PTR_ERR(hw->clk);
3773 goto fail_create_clk;
3776 clk_core_link_consumer(core, hw->clk);
3778 ret = __clk_core_init(core);
3783 clk_core_unlink_consumer(hw->clk);
3784 clk_prepare_unlock();
3790 clk_core_free_parent_map(core);
3793 kfree_const(core->name);
3797 return ERR_PTR(ret);
3801 * dev_or_parent_of_node() - Get device node of @dev or @dev's parent
3802 * @dev: Device to get device node of
3804 * Return: device node pointer of @dev, or the device node pointer of
3805 * @dev->parent if dev doesn't have a device node, or NULL if neither
3806 * @dev or @dev->parent have a device node.
3808 static struct device_node *dev_or_parent_of_node(struct device *dev)
3810 struct device_node *np;
3815 np = dev_of_node(dev);
3817 np = dev_of_node(dev->parent);
3823 * clk_register - allocate a new clock, register it and return an opaque cookie
3824 * @dev: device that is registering this clock
3825 * @hw: link to hardware-specific clock data
3827 * clk_register is the *deprecated* interface for populating the clock tree with
3828 * new clock nodes. Use clk_hw_register() instead.
3830 * Returns: a pointer to the newly allocated struct clk which
3831 * cannot be dereferenced by driver code but may be used in conjunction with the
3832 * rest of the clock API. In the event of an error clk_register will return an
3833 * error code; drivers must test for an error code after calling clk_register.
3835 struct clk *clk_register(struct device *dev, struct clk_hw *hw)
3837 return __clk_register(dev, dev_or_parent_of_node(dev), hw);
3839 EXPORT_SYMBOL_GPL(clk_register);
3842 * clk_hw_register - register a clk_hw and return an error code
3843 * @dev: device that is registering this clock
3844 * @hw: link to hardware-specific clock data
3846 * clk_hw_register is the primary interface for populating the clock tree with
3847 * new clock nodes. It returns an integer equal to zero indicating success or
3848 * less than zero indicating failure. Drivers must test for an error code after
3849 * calling clk_hw_register().
3851 int clk_hw_register(struct device *dev, struct clk_hw *hw)
3853 return PTR_ERR_OR_ZERO(__clk_register(dev, dev_or_parent_of_node(dev),
3856 EXPORT_SYMBOL_GPL(clk_hw_register);
3859 * of_clk_hw_register - register a clk_hw and return an error code
3860 * @node: device_node of device that is registering this clock
3861 * @hw: link to hardware-specific clock data
3863 * of_clk_hw_register() is the primary interface for populating the clock tree
3864 * with new clock nodes when a struct device is not available, but a struct
3865 * device_node is. It returns an integer equal to zero indicating success or
3866 * less than zero indicating failure. Drivers must test for an error code after
3867 * calling of_clk_hw_register().
3869 int of_clk_hw_register(struct device_node *node, struct clk_hw *hw)
3871 return PTR_ERR_OR_ZERO(__clk_register(NULL, node, hw));
3873 EXPORT_SYMBOL_GPL(of_clk_hw_register);
3875 /* Free memory allocated for a clock. */
3876 static void __clk_release(struct kref *ref)
3878 struct clk_core *core = container_of(ref, struct clk_core, ref);
3880 lockdep_assert_held(&prepare_lock);
3882 clk_core_free_parent_map(core);
3883 kfree_const(core->name);
3888 * Empty clk_ops for unregistered clocks. These are used temporarily
3889 * after clk_unregister() was called on a clock and until last clock
3890 * consumer calls clk_put() and the struct clk object is freed.
3892 static int clk_nodrv_prepare_enable(struct clk_hw *hw)
3897 static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
3902 static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
3903 unsigned long parent_rate)
3908 static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
3913 static const struct clk_ops clk_nodrv_ops = {
3914 .enable = clk_nodrv_prepare_enable,
3915 .disable = clk_nodrv_disable_unprepare,
3916 .prepare = clk_nodrv_prepare_enable,
3917 .unprepare = clk_nodrv_disable_unprepare,
3918 .set_rate = clk_nodrv_set_rate,
3919 .set_parent = clk_nodrv_set_parent,
3922 static void clk_core_evict_parent_cache_subtree(struct clk_core *root,
3923 struct clk_core *target)
3926 struct clk_core *child;
3928 for (i = 0; i < root->num_parents; i++)
3929 if (root->parents[i].core == target)
3930 root->parents[i].core = NULL;
3932 hlist_for_each_entry(child, &root->children, child_node)
3933 clk_core_evict_parent_cache_subtree(child, target);
3936 /* Remove this clk from all parent caches */
3937 static void clk_core_evict_parent_cache(struct clk_core *core)
3939 struct hlist_head **lists;
3940 struct clk_core *root;
3942 lockdep_assert_held(&prepare_lock);
3944 for (lists = all_lists; *lists; lists++)
3945 hlist_for_each_entry(root, *lists, child_node)
3946 clk_core_evict_parent_cache_subtree(root, core);
3951 * clk_unregister - unregister a currently registered clock
3952 * @clk: clock to unregister
3954 void clk_unregister(struct clk *clk)
3956 unsigned long flags;
3958 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3961 clk_debug_unregister(clk->core);
3965 if (clk->core->ops == &clk_nodrv_ops) {
3966 pr_err("%s: unregistered clock: %s\n", __func__,
3971 * Assign empty clock ops for consumers that might still hold
3972 * a reference to this clock.
3974 flags = clk_enable_lock();
3975 clk->core->ops = &clk_nodrv_ops;
3976 clk_enable_unlock(flags);
3978 if (!hlist_empty(&clk->core->children)) {
3979 struct clk_core *child;
3980 struct hlist_node *t;
3982 /* Reparent all children to the orphan list. */
3983 hlist_for_each_entry_safe(child, t, &clk->core->children,
3985 clk_core_set_parent_nolock(child, NULL);
3988 clk_core_evict_parent_cache(clk->core);
3990 hlist_del_init(&clk->core->child_node);
3992 if (clk->core->prepare_count)
3993 pr_warn("%s: unregistering prepared clock: %s\n",
3994 __func__, clk->core->name);
3996 if (clk->core->protect_count)
3997 pr_warn("%s: unregistering protected clock: %s\n",
3998 __func__, clk->core->name);
4000 kref_put(&clk->core->ref, __clk_release);
4003 clk_prepare_unlock();
4005 EXPORT_SYMBOL_GPL(clk_unregister);
4008 * clk_hw_unregister - unregister a currently registered clk_hw
4009 * @hw: hardware-specific clock data to unregister
4011 void clk_hw_unregister(struct clk_hw *hw)
4013 clk_unregister(hw->clk);
4015 EXPORT_SYMBOL_GPL(clk_hw_unregister);
4017 static void devm_clk_release(struct device *dev, void *res)
4019 clk_unregister(*(struct clk **)res);
4022 static void devm_clk_hw_release(struct device *dev, void *res)
4024 clk_hw_unregister(*(struct clk_hw **)res);
4028 * devm_clk_register - resource managed clk_register()
4029 * @dev: device that is registering this clock
4030 * @hw: link to hardware-specific clock data
4032 * Managed clk_register(). This function is *deprecated*, use devm_clk_hw_register() instead.
4034 * Clocks returned from this function are automatically clk_unregister()ed on
4035 * driver detach. See clk_register() for more information.
4037 struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
4042 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
4044 return ERR_PTR(-ENOMEM);
4046 clk = clk_register(dev, hw);
4049 devres_add(dev, clkp);
4056 EXPORT_SYMBOL_GPL(devm_clk_register);
4059 * devm_clk_hw_register - resource managed clk_hw_register()
4060 * @dev: device that is registering this clock
4061 * @hw: link to hardware-specific clock data
4063 * Managed clk_hw_register(). Clocks registered by this function are
4064 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
4065 * for more information.
4067 int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
4069 struct clk_hw **hwp;
4072 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
4076 ret = clk_hw_register(dev, hw);
4079 devres_add(dev, hwp);
4086 EXPORT_SYMBOL_GPL(devm_clk_hw_register);
4088 static int devm_clk_match(struct device *dev, void *res, void *data)
4090 struct clk *c = res;
4096 static int devm_clk_hw_match(struct device *dev, void *res, void *data)
4098 struct clk_hw *hw = res;
4106 * devm_clk_unregister - resource managed clk_unregister()
4107 * @clk: clock to unregister
4109 * Deallocate a clock allocated with devm_clk_register(). Normally
4110 * this function will not need to be called and the resource management
4111 * code will ensure that the resource is freed.
4113 void devm_clk_unregister(struct device *dev, struct clk *clk)
4115 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
4117 EXPORT_SYMBOL_GPL(devm_clk_unregister);
4120 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
4121 * @dev: device that is unregistering the hardware-specific clock data
4122 * @hw: link to hardware-specific clock data
4124 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
4125 * this function will not need to be called and the resource management
4126 * code will ensure that the resource is freed.
4128 void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
4130 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
4133 EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
4139 void __clk_put(struct clk *clk)
4141 struct module *owner;
4143 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
4149 * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
4150 * given user should be balanced with calls to clk_rate_exclusive_put()
4151 * and by that same consumer
4153 if (WARN_ON(clk->exclusive_count)) {
4154 /* We voiced our concern, let's sanitize the situation */
4155 clk->core->protect_count -= (clk->exclusive_count - 1);
4156 clk_core_rate_unprotect(clk->core);
4157 clk->exclusive_count = 0;
4160 hlist_del(&clk->clks_node);
4161 if (clk->min_rate > clk->core->req_rate ||
4162 clk->max_rate < clk->core->req_rate)
4163 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
4165 owner = clk->core->owner;
4166 kref_put(&clk->core->ref, __clk_release);
4168 clk_prepare_unlock();
4175 /*** clk rate change notifiers ***/
4178 * clk_notifier_register - add a clk rate change notifier
4179 * @clk: struct clk * to watch
4180 * @nb: struct notifier_block * with callback info
4182 * Request notification when clk's rate changes. This uses an SRCU
4183 * notifier because we want it to block and notifier unregistrations are
4184 * uncommon. The callbacks associated with the notifier must not
4185 * re-enter into the clk framework by calling any top-level clk APIs;
4186 * this will cause a nested prepare_lock mutex.
4188 * In all notification cases (pre, post and abort rate change) the original
4189 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
4190 * and the new frequency is passed via struct clk_notifier_data.new_rate.
4192 * clk_notifier_register() must be called from non-atomic context.
4193 * Returns -EINVAL if called with null arguments, -ENOMEM upon
4194 * allocation failure; otherwise, passes along the return value of
4195 * srcu_notifier_chain_register().
4197 int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
4199 struct clk_notifier *cn;
4207 /* search the list of notifiers for this clk */
4208 list_for_each_entry(cn, &clk_notifier_list, node)
4212 /* if clk wasn't in the notifier list, allocate new clk_notifier */
4213 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
4218 srcu_init_notifier_head(&cn->notifier_head);
4220 list_add(&cn->node, &clk_notifier_list);
4223 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
4225 clk->core->notifier_count++;
4228 clk_prepare_unlock();
4232 EXPORT_SYMBOL_GPL(clk_notifier_register);
4235 * clk_notifier_unregister - remove a clk rate change notifier
4236 * @clk: struct clk *
4237 * @nb: struct notifier_block * with callback info
4239 * Request no further notification for changes to 'clk' and frees memory
4240 * allocated in clk_notifier_register.
4242 * Returns -EINVAL if called with null arguments; otherwise, passes
4243 * along the return value of srcu_notifier_chain_unregister().
4245 int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
4247 struct clk_notifier *cn;
4255 list_for_each_entry(cn, &clk_notifier_list, node) {
4256 if (cn->clk == clk) {
4257 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
4259 clk->core->notifier_count--;
4261 /* XXX the notifier code should handle this better */
4262 if (!cn->notifier_head.head) {
4263 srcu_cleanup_notifier_head(&cn->notifier_head);
4264 list_del(&cn->node);
4271 clk_prepare_unlock();
4275 EXPORT_SYMBOL_GPL(clk_notifier_unregister);
4278 static void clk_core_reparent_orphans(void)
4281 clk_core_reparent_orphans_nolock();
4282 clk_prepare_unlock();
4286 * struct of_clk_provider - Clock provider registration structure
4287 * @link: Entry in global list of clock providers
4288 * @node: Pointer to device tree node of clock provider
4289 * @get: Get clock callback. Returns NULL or a struct clk for the
4290 * given clock specifier
4291 * @data: context pointer to be passed into @get callback
4293 struct of_clk_provider {
4294 struct list_head link;
4296 struct device_node *node;
4297 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
4298 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
4302 extern struct of_device_id __clk_of_table;
4303 static const struct of_device_id __clk_of_table_sentinel
4304 __used __section(__clk_of_table_end);
4306 static LIST_HEAD(of_clk_providers);
4307 static DEFINE_MUTEX(of_clk_mutex);
4309 struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
4314 EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
4316 struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
4320 EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
4322 struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
4324 struct clk_onecell_data *clk_data = data;
4325 unsigned int idx = clkspec->args[0];
4327 if (idx >= clk_data->clk_num) {
4328 pr_err("%s: invalid clock index %u\n", __func__, idx);
4329 return ERR_PTR(-EINVAL);
4332 return clk_data->clks[idx];
4334 EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
4337 of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
4339 struct clk_hw_onecell_data *hw_data = data;
4340 unsigned int idx = clkspec->args[0];
4342 if (idx >= hw_data->num) {
4343 pr_err("%s: invalid index %u\n", __func__, idx);
4344 return ERR_PTR(-EINVAL);
4347 return hw_data->hws[idx];
4349 EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
4352 * of_clk_add_provider() - Register a clock provider for a node
4353 * @np: Device node pointer associated with clock provider
4354 * @clk_src_get: callback for decoding clock
4355 * @data: context pointer for @clk_src_get callback.
4357 * This function is *deprecated*. Use of_clk_add_hw_provider() instead.
4359 int of_clk_add_provider(struct device_node *np,
4360 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
4364 struct of_clk_provider *cp;
4367 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
4371 cp->node = of_node_get(np);
4373 cp->get = clk_src_get;
4375 mutex_lock(&of_clk_mutex);
4376 list_add(&cp->link, &of_clk_providers);
4377 mutex_unlock(&of_clk_mutex);
4378 pr_debug("Added clock from %pOF\n", np);
4380 clk_core_reparent_orphans();
4382 ret = of_clk_set_defaults(np, true);
4384 of_clk_del_provider(np);
4388 EXPORT_SYMBOL_GPL(of_clk_add_provider);
4391 * of_clk_add_hw_provider() - Register a clock provider for a node
4392 * @np: Device node pointer associated with clock provider
4393 * @get: callback for decoding clk_hw
4394 * @data: context pointer for @get callback.
4396 int of_clk_add_hw_provider(struct device_node *np,
4397 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
4401 struct of_clk_provider *cp;
4404 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
4408 cp->node = of_node_get(np);
4412 mutex_lock(&of_clk_mutex);
4413 list_add(&cp->link, &of_clk_providers);
4414 mutex_unlock(&of_clk_mutex);
4415 pr_debug("Added clk_hw provider from %pOF\n", np);
4417 clk_core_reparent_orphans();
4419 ret = of_clk_set_defaults(np, true);
4421 of_clk_del_provider(np);
4425 EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
4427 static void devm_of_clk_release_provider(struct device *dev, void *res)
4429 of_clk_del_provider(*(struct device_node **)res);
4433 * We allow a child device to use its parent device as the clock provider node
4434 * for cases like MFD sub-devices where the child device driver wants to use
4435 * devm_*() APIs but not list the device in DT as a sub-node.
4437 static struct device_node *get_clk_provider_node(struct device *dev)
4439 struct device_node *np, *parent_np;
4442 parent_np = dev->parent ? dev->parent->of_node : NULL;
4444 if (!of_find_property(np, "#clock-cells", NULL))
4445 if (of_find_property(parent_np, "#clock-cells", NULL))
4452 * devm_of_clk_add_hw_provider() - Managed clk provider node registration
4453 * @dev: Device acting as the clock provider (used for DT node and lifetime)
4454 * @get: callback for decoding clk_hw
4455 * @data: context pointer for @get callback
4457 * Registers clock provider for given device's node. If the device has no DT
4458 * node or if the device node lacks of clock provider information (#clock-cells)
4459 * then the parent device's node is scanned for this information. If parent node
4460 * has the #clock-cells then it is used in registration. Provider is
4461 * automatically released at device exit.
4463 * Return: 0 on success or an errno on failure.
4465 int devm_of_clk_add_hw_provider(struct device *dev,
4466 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
4470 struct device_node **ptr, *np;
4473 ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
4478 np = get_clk_provider_node(dev);
4479 ret = of_clk_add_hw_provider(np, get, data);
4482 devres_add(dev, ptr);
4489 EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
4492 * of_clk_del_provider() - Remove a previously registered clock provider
4493 * @np: Device node pointer associated with clock provider
4495 void of_clk_del_provider(struct device_node *np)
4497 struct of_clk_provider *cp;
4499 mutex_lock(&of_clk_mutex);
4500 list_for_each_entry(cp, &of_clk_providers, link) {
4501 if (cp->node == np) {
4502 list_del(&cp->link);
4503 of_node_put(cp->node);
4508 mutex_unlock(&of_clk_mutex);
4510 EXPORT_SYMBOL_GPL(of_clk_del_provider);
4512 static int devm_clk_provider_match(struct device *dev, void *res, void *data)
4514 struct device_node **np = res;
4516 if (WARN_ON(!np || !*np))
4523 * devm_of_clk_del_provider() - Remove clock provider registered using devm
4524 * @dev: Device to whose lifetime the clock provider was bound
4526 void devm_of_clk_del_provider(struct device *dev)
4529 struct device_node *np = get_clk_provider_node(dev);
4531 ret = devres_release(dev, devm_of_clk_release_provider,
4532 devm_clk_provider_match, np);
4536 EXPORT_SYMBOL(devm_of_clk_del_provider);
4539 * of_parse_clkspec() - Parse a DT clock specifier for a given device node
4540 * @np: device node to parse clock specifier from
4541 * @index: index of phandle to parse clock out of. If index < 0, @name is used
4542 * @name: clock name to find and parse. If name is NULL, the index is used
4543 * @out_args: Result of parsing the clock specifier
4545 * Parses a device node's "clocks" and "clock-names" properties to find the
4546 * phandle and cells for the index or name that is desired. The resulting clock
4547 * specifier is placed into @out_args, or an errno is returned when there's a
4548 * parsing error. The @index argument is ignored if @name is non-NULL.
4552 * phandle1: clock-controller@1 {
4553 * #clock-cells = <2>;
4556 * phandle2: clock-controller@2 {
4557 * #clock-cells = <1>;
4560 * clock-consumer@3 {
4561 * clocks = <&phandle1 1 2 &phandle2 3>;
4562 * clock-names = "name1", "name2";
4565 * To get a device_node for `clock-controller@2' node you may call this
4566 * function a few different ways:
4568 * of_parse_clkspec(clock-consumer@3, -1, "name2", &args);
4569 * of_parse_clkspec(clock-consumer@3, 1, NULL, &args);
4570 * of_parse_clkspec(clock-consumer@3, 1, "name2", &args);
4572 * Return: 0 upon successfully parsing the clock specifier. Otherwise, -ENOENT
4573 * if @name is NULL or -EINVAL if @name is non-NULL and it can't be found in
4574 * the "clock-names" property of @np.
4576 static int of_parse_clkspec(const struct device_node *np, int index,
4577 const char *name, struct of_phandle_args *out_args)
4581 /* Walk up the tree of devices looking for a clock property that matches */
4584 * For named clocks, first look up the name in the
4585 * "clock-names" property. If it cannot be found, then index
4586 * will be an error code and of_parse_phandle_with_args() will
4590 index = of_property_match_string(np, "clock-names", name);
4591 ret = of_parse_phandle_with_args(np, "clocks", "#clock-cells",
4595 if (name && index >= 0)
4599 * No matching clock found on this node. If the parent node
4600 * has a "clock-ranges" property, then we can try one of its
4604 if (np && !of_get_property(np, "clock-ranges", NULL))
4612 static struct clk_hw *
4613 __of_clk_get_hw_from_provider(struct of_clk_provider *provider,
4614 struct of_phandle_args *clkspec)
4618 if (provider->get_hw)
4619 return provider->get_hw(clkspec, provider->data);
4621 clk = provider->get(clkspec, provider->data);
4623 return ERR_CAST(clk);
4624 return __clk_get_hw(clk);
4627 static struct clk_hw *
4628 of_clk_get_hw_from_clkspec(struct of_phandle_args *clkspec)
4630 struct of_clk_provider *provider;
4631 struct clk_hw *hw = ERR_PTR(-EPROBE_DEFER);
4634 return ERR_PTR(-EINVAL);
4636 mutex_lock(&of_clk_mutex);
4637 list_for_each_entry(provider, &of_clk_providers, link) {
4638 if (provider->node == clkspec->np) {
4639 hw = __of_clk_get_hw_from_provider(provider, clkspec);
4644 mutex_unlock(&of_clk_mutex);
4650 * of_clk_get_from_provider() - Lookup a clock from a clock provider
4651 * @clkspec: pointer to a clock specifier data structure
4653 * This function looks up a struct clk from the registered list of clock
4654 * providers, an input is a clock specifier data structure as returned
4655 * from the of_parse_phandle_with_args() function call.
4657 struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
4659 struct clk_hw *hw = of_clk_get_hw_from_clkspec(clkspec);
4661 return clk_hw_create_clk(NULL, hw, NULL, __func__);
4663 EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
4665 struct clk_hw *of_clk_get_hw(struct device_node *np, int index,
4670 struct of_phandle_args clkspec;
4672 ret = of_parse_clkspec(np, index, con_id, &clkspec);
4674 return ERR_PTR(ret);
4676 hw = of_clk_get_hw_from_clkspec(&clkspec);
4677 of_node_put(clkspec.np);
4682 static struct clk *__of_clk_get(struct device_node *np,
4683 int index, const char *dev_id,
4686 struct clk_hw *hw = of_clk_get_hw(np, index, con_id);
4688 return clk_hw_create_clk(NULL, hw, dev_id, con_id);
4691 struct clk *of_clk_get(struct device_node *np, int index)
4693 return __of_clk_get(np, index, np->full_name, NULL);
4695 EXPORT_SYMBOL(of_clk_get);
4698 * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
4699 * @np: pointer to clock consumer node
4700 * @name: name of consumer's clock input, or NULL for the first clock reference
4702 * This function parses the clocks and clock-names properties,
4703 * and uses them to look up the struct clk from the registered list of clock
4706 struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
4709 return ERR_PTR(-ENOENT);
4711 return __of_clk_get(np, 0, np->full_name, name);
4713 EXPORT_SYMBOL(of_clk_get_by_name);
4716 * of_clk_get_parent_count() - Count the number of clocks a device node has
4717 * @np: device node to count
4719 * Returns: The number of clocks that are possible parents of this node
4721 unsigned int of_clk_get_parent_count(struct device_node *np)
4725 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
4731 EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
4733 const char *of_clk_get_parent_name(struct device_node *np, int index)
4735 struct of_phandle_args clkspec;
4736 struct property *prop;
4737 const char *clk_name;
4744 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
4749 index = clkspec.args_count ? clkspec.args[0] : 0;
4752 /* if there is an indices property, use it to transfer the index
4753 * specified into an array offset for the clock-output-names property.
4755 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
4762 /* We went off the end of 'clock-indices' without finding it */
4766 if (of_property_read_string_index(clkspec.np, "clock-output-names",
4770 * Best effort to get the name if the clock has been
4771 * registered with the framework. If the clock isn't
4772 * registered, we return the node name as the name of
4773 * the clock as long as #clock-cells = 0.
4775 clk = of_clk_get_from_provider(&clkspec);
4777 if (clkspec.args_count == 0)
4778 clk_name = clkspec.np->name;
4782 clk_name = __clk_get_name(clk);
4788 of_node_put(clkspec.np);
4791 EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
4794 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
4796 * @np: Device node pointer associated with clock provider
4797 * @parents: pointer to char array that hold the parents' names
4798 * @size: size of the @parents array
4800 * Return: number of parents for the clock node.
4802 int of_clk_parent_fill(struct device_node *np, const char **parents,
4807 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
4812 EXPORT_SYMBOL_GPL(of_clk_parent_fill);
4814 struct clock_provider {
4815 void (*clk_init_cb)(struct device_node *);
4816 struct device_node *np;
4817 struct list_head node;
4821 * This function looks for a parent clock. If there is one, then it
4822 * checks that the provider for this parent clock was initialized, in
4823 * this case the parent clock will be ready.
4825 static int parent_ready(struct device_node *np)
4830 struct clk *clk = of_clk_get(np, i);
4832 /* this parent is ready we can check the next one */
4839 /* at least one parent is not ready, we exit now */
4840 if (PTR_ERR(clk) == -EPROBE_DEFER)
4844 * Here we make assumption that the device tree is
4845 * written correctly. So an error means that there is
4846 * no more parent. As we didn't exit yet, then the
4847 * previous parent are ready. If there is no clock
4848 * parent, no need to wait for them, then we can
4849 * consider their absence as being ready
4856 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
4857 * @np: Device node pointer associated with clock provider
4858 * @index: clock index
4859 * @flags: pointer to top-level framework flags
4861 * Detects if the clock-critical property exists and, if so, sets the
4862 * corresponding CLK_IS_CRITICAL flag.
4864 * Do not use this function. It exists only for legacy Device Tree
4865 * bindings, such as the one-clock-per-node style that are outdated.
4866 * Those bindings typically put all clock data into .dts and the Linux
4867 * driver has no clock data, thus making it impossible to set this flag
4868 * correctly from the driver. Only those drivers may call
4869 * of_clk_detect_critical from their setup functions.
4871 * Return: error code or zero on success
4873 int of_clk_detect_critical(struct device_node *np,
4874 int index, unsigned long *flags)
4876 struct property *prop;
4883 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
4885 *flags |= CLK_IS_CRITICAL;
4891 * of_clk_init() - Scan and init clock providers from the DT
4892 * @matches: array of compatible values and init functions for providers.
4894 * This function scans the device tree for matching clock providers
4895 * and calls their initialization functions. It also does it by trying
4896 * to follow the dependencies.
4898 void __init of_clk_init(const struct of_device_id *matches)
4900 const struct of_device_id *match;
4901 struct device_node *np;
4902 struct clock_provider *clk_provider, *next;
4905 LIST_HEAD(clk_provider_list);
4908 matches = &__clk_of_table;
4910 /* First prepare the list of the clocks providers */
4911 for_each_matching_node_and_match(np, matches, &match) {
4912 struct clock_provider *parent;
4914 if (!of_device_is_available(np))
4917 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
4919 list_for_each_entry_safe(clk_provider, next,
4920 &clk_provider_list, node) {
4921 list_del(&clk_provider->node);
4922 of_node_put(clk_provider->np);
4923 kfree(clk_provider);
4929 parent->clk_init_cb = match->data;
4930 parent->np = of_node_get(np);
4931 list_add_tail(&parent->node, &clk_provider_list);
4934 while (!list_empty(&clk_provider_list)) {
4935 is_init_done = false;
4936 list_for_each_entry_safe(clk_provider, next,
4937 &clk_provider_list, node) {
4938 if (force || parent_ready(clk_provider->np)) {
4940 /* Don't populate platform devices */
4941 of_node_set_flag(clk_provider->np,
4944 clk_provider->clk_init_cb(clk_provider->np);
4945 of_clk_set_defaults(clk_provider->np, true);
4947 list_del(&clk_provider->node);
4948 of_node_put(clk_provider->np);
4949 kfree(clk_provider);
4950 is_init_done = true;
4955 * We didn't manage to initialize any of the
4956 * remaining providers during the last loop, so now we
4957 * initialize all the remaining ones unconditionally
4958 * in case the clock parent was not mandatory