2 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
3 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Standard functionality for the common clock API. See Documentation/clk.txt
12 #include <linux/clk.h>
13 #include <linux/clk-provider.h>
14 #include <linux/clk/clk-conf.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/spinlock.h>
18 #include <linux/err.h>
19 #include <linux/list.h>
20 #include <linux/slab.h>
22 #include <linux/device.h>
23 #include <linux/init.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/sched.h>
26 #include <linux/clkdev.h>
30 static DEFINE_SPINLOCK(enable_lock
);
31 static DEFINE_MUTEX(prepare_lock
);
33 static struct task_struct
*prepare_owner
;
34 static struct task_struct
*enable_owner
;
36 static int prepare_refcnt
;
37 static int enable_refcnt
;
39 static HLIST_HEAD(clk_root_list
);
40 static HLIST_HEAD(clk_orphan_list
);
41 static LIST_HEAD(clk_notifier_list
);
43 /*** private data structures ***/
47 const struct clk_ops
*ops
;
51 struct clk_core
*parent
;
52 const char **parent_names
;
53 struct clk_core
**parents
;
57 unsigned long req_rate
;
58 unsigned long new_rate
;
59 struct clk_core
*new_parent
;
60 struct clk_core
*new_child
;
63 unsigned int enable_count
;
64 unsigned int prepare_count
;
65 unsigned long min_rate
;
66 unsigned long max_rate
;
67 unsigned long accuracy
;
69 struct hlist_head children
;
70 struct hlist_node child_node
;
71 struct hlist_head clks
;
72 unsigned int notifier_count
;
73 #ifdef CONFIG_DEBUG_FS
74 struct dentry
*dentry
;
75 struct hlist_node debug_node
;
80 #define CREATE_TRACE_POINTS
81 #include <trace/events/clk.h>
84 struct clk_core
*core
;
87 unsigned long min_rate
;
88 unsigned long max_rate
;
89 struct hlist_node clks_node
;
93 static int clk_pm_runtime_get(struct clk_core
*core
)
100 ret
= pm_runtime_get_sync(core
->dev
);
101 return ret
< 0 ? ret
: 0;
104 static void clk_pm_runtime_put(struct clk_core
*core
)
109 pm_runtime_put_sync(core
->dev
);
113 static void clk_prepare_lock(void)
115 if (!mutex_trylock(&prepare_lock
)) {
116 if (prepare_owner
== current
) {
120 mutex_lock(&prepare_lock
);
122 WARN_ON_ONCE(prepare_owner
!= NULL
);
123 WARN_ON_ONCE(prepare_refcnt
!= 0);
124 prepare_owner
= current
;
128 static void clk_prepare_unlock(void)
130 WARN_ON_ONCE(prepare_owner
!= current
);
131 WARN_ON_ONCE(prepare_refcnt
== 0);
133 if (--prepare_refcnt
)
135 prepare_owner
= NULL
;
136 mutex_unlock(&prepare_lock
);
139 static unsigned long clk_enable_lock(void)
140 __acquires(enable_lock
)
144 if (!spin_trylock_irqsave(&enable_lock
, flags
)) {
145 if (enable_owner
== current
) {
147 __acquire(enable_lock
);
150 spin_lock_irqsave(&enable_lock
, flags
);
152 WARN_ON_ONCE(enable_owner
!= NULL
);
153 WARN_ON_ONCE(enable_refcnt
!= 0);
154 enable_owner
= current
;
159 static void clk_enable_unlock(unsigned long flags
)
160 __releases(enable_lock
)
162 WARN_ON_ONCE(enable_owner
!= current
);
163 WARN_ON_ONCE(enable_refcnt
== 0);
165 if (--enable_refcnt
) {
166 __release(enable_lock
);
170 spin_unlock_irqrestore(&enable_lock
, flags
);
173 static bool clk_core_is_prepared(struct clk_core
*core
)
178 * .is_prepared is optional for clocks that can prepare
179 * fall back to software usage counter if it is missing
181 if (!core
->ops
->is_prepared
)
182 return core
->prepare_count
;
184 if (!clk_pm_runtime_get(core
)) {
185 ret
= core
->ops
->is_prepared(core
->hw
);
186 clk_pm_runtime_put(core
);
192 static bool clk_core_is_enabled(struct clk_core
*core
)
197 * .is_enabled is only mandatory for clocks that gate
198 * fall back to software usage counter if .is_enabled is missing
200 if (!core
->ops
->is_enabled
)
201 return core
->enable_count
;
204 * Check if clock controller's device is runtime active before
205 * calling .is_enabled callback. If not, assume that clock is
206 * disabled, because we might be called from atomic context, from
207 * which pm_runtime_get() is not allowed.
208 * This function is called mainly from clk_disable_unused_subtree,
209 * which ensures proper runtime pm activation of controller before
210 * taking enable spinlock, but the below check is needed if one tries
211 * to call it from other places.
214 pm_runtime_get_noresume(core
->dev
);
215 if (!pm_runtime_active(core
->dev
)) {
221 ret
= core
->ops
->is_enabled(core
->hw
);
224 pm_runtime_put(core
->dev
);
229 /*** helper functions ***/
231 const char *__clk_get_name(const struct clk
*clk
)
233 return !clk
? NULL
: clk
->core
->name
;
235 EXPORT_SYMBOL_GPL(__clk_get_name
);
237 const char *clk_hw_get_name(const struct clk_hw
*hw
)
239 return hw
->core
->name
;
241 EXPORT_SYMBOL_GPL(clk_hw_get_name
);
243 struct clk_hw
*__clk_get_hw(struct clk
*clk
)
245 return !clk
? NULL
: clk
->core
->hw
;
247 EXPORT_SYMBOL_GPL(__clk_get_hw
);
249 unsigned int clk_hw_get_num_parents(const struct clk_hw
*hw
)
251 return hw
->core
->num_parents
;
253 EXPORT_SYMBOL_GPL(clk_hw_get_num_parents
);
255 struct clk_hw
*clk_hw_get_parent(const struct clk_hw
*hw
)
257 return hw
->core
->parent
? hw
->core
->parent
->hw
: NULL
;
259 EXPORT_SYMBOL_GPL(clk_hw_get_parent
);
261 static struct clk_core
*__clk_lookup_subtree(const char *name
,
262 struct clk_core
*core
)
264 struct clk_core
*child
;
265 struct clk_core
*ret
;
267 if (!strcmp(core
->name
, name
))
270 hlist_for_each_entry(child
, &core
->children
, child_node
) {
271 ret
= __clk_lookup_subtree(name
, child
);
279 static struct clk_core
*clk_core_lookup(const char *name
)
281 struct clk_core
*root_clk
;
282 struct clk_core
*ret
;
287 /* search the 'proper' clk tree first */
288 hlist_for_each_entry(root_clk
, &clk_root_list
, child_node
) {
289 ret
= __clk_lookup_subtree(name
, root_clk
);
294 /* if not found, then search the orphan tree */
295 hlist_for_each_entry(root_clk
, &clk_orphan_list
, child_node
) {
296 ret
= __clk_lookup_subtree(name
, root_clk
);
304 static struct clk_core
*clk_core_get_parent_by_index(struct clk_core
*core
,
307 if (!core
|| index
>= core
->num_parents
)
310 if (!core
->parents
[index
])
311 core
->parents
[index
] =
312 clk_core_lookup(core
->parent_names
[index
]);
314 return core
->parents
[index
];
318 clk_hw_get_parent_by_index(const struct clk_hw
*hw
, unsigned int index
)
320 struct clk_core
*parent
;
322 parent
= clk_core_get_parent_by_index(hw
->core
, index
);
324 return !parent
? NULL
: parent
->hw
;
326 EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index
);
328 unsigned int __clk_get_enable_count(struct clk
*clk
)
330 return !clk
? 0 : clk
->core
->enable_count
;
333 static unsigned long clk_core_get_rate_nolock(struct clk_core
*core
)
344 if (!core
->num_parents
)
354 unsigned long clk_hw_get_rate(const struct clk_hw
*hw
)
356 return clk_core_get_rate_nolock(hw
->core
);
358 EXPORT_SYMBOL_GPL(clk_hw_get_rate
);
360 static unsigned long __clk_get_accuracy(struct clk_core
*core
)
365 return core
->accuracy
;
368 unsigned long __clk_get_flags(struct clk
*clk
)
370 return !clk
? 0 : clk
->core
->flags
;
372 EXPORT_SYMBOL_GPL(__clk_get_flags
);
374 unsigned long clk_hw_get_flags(const struct clk_hw
*hw
)
376 return hw
->core
->flags
;
378 EXPORT_SYMBOL_GPL(clk_hw_get_flags
);
380 bool clk_hw_is_prepared(const struct clk_hw
*hw
)
382 return clk_core_is_prepared(hw
->core
);
385 bool clk_hw_is_enabled(const struct clk_hw
*hw
)
387 return clk_core_is_enabled(hw
->core
);
390 bool __clk_is_enabled(struct clk
*clk
)
395 return clk_core_is_enabled(clk
->core
);
397 EXPORT_SYMBOL_GPL(__clk_is_enabled
);
399 static bool mux_is_better_rate(unsigned long rate
, unsigned long now
,
400 unsigned long best
, unsigned long flags
)
402 if (flags
& CLK_MUX_ROUND_CLOSEST
)
403 return abs(now
- rate
) < abs(best
- rate
);
405 return now
<= rate
&& now
> best
;
408 int clk_mux_determine_rate_flags(struct clk_hw
*hw
,
409 struct clk_rate_request
*req
,
412 struct clk_core
*core
= hw
->core
, *parent
, *best_parent
= NULL
;
413 int i
, num_parents
, ret
;
414 unsigned long best
= 0;
415 struct clk_rate_request parent_req
= *req
;
417 /* if NO_REPARENT flag set, pass through to current parent */
418 if (core
->flags
& CLK_SET_RATE_NO_REPARENT
) {
419 parent
= core
->parent
;
420 if (core
->flags
& CLK_SET_RATE_PARENT
) {
421 ret
= __clk_determine_rate(parent
? parent
->hw
: NULL
,
426 best
= parent_req
.rate
;
428 best
= clk_core_get_rate_nolock(parent
);
430 best
= clk_core_get_rate_nolock(core
);
436 /* find the parent that can provide the fastest rate <= rate */
437 num_parents
= core
->num_parents
;
438 for (i
= 0; i
< num_parents
; i
++) {
439 parent
= clk_core_get_parent_by_index(core
, i
);
443 if (core
->flags
& CLK_SET_RATE_PARENT
) {
445 ret
= __clk_determine_rate(parent
->hw
, &parent_req
);
449 parent_req
.rate
= clk_core_get_rate_nolock(parent
);
452 if (mux_is_better_rate(req
->rate
, parent_req
.rate
,
454 best_parent
= parent
;
455 best
= parent_req
.rate
;
464 req
->best_parent_hw
= best_parent
->hw
;
465 req
->best_parent_rate
= best
;
470 EXPORT_SYMBOL_GPL(clk_mux_determine_rate_flags
);
472 struct clk
*__clk_lookup(const char *name
)
474 struct clk_core
*core
= clk_core_lookup(name
);
476 return !core
? NULL
: core
->hw
->clk
;
479 static void clk_core_get_boundaries(struct clk_core
*core
,
480 unsigned long *min_rate
,
481 unsigned long *max_rate
)
483 struct clk
*clk_user
;
485 *min_rate
= core
->min_rate
;
486 *max_rate
= core
->max_rate
;
488 hlist_for_each_entry(clk_user
, &core
->clks
, clks_node
)
489 *min_rate
= max(*min_rate
, clk_user
->min_rate
);
491 hlist_for_each_entry(clk_user
, &core
->clks
, clks_node
)
492 *max_rate
= min(*max_rate
, clk_user
->max_rate
);
495 void clk_hw_set_rate_range(struct clk_hw
*hw
, unsigned long min_rate
,
496 unsigned long max_rate
)
498 hw
->core
->min_rate
= min_rate
;
499 hw
->core
->max_rate
= max_rate
;
501 EXPORT_SYMBOL_GPL(clk_hw_set_rate_range
);
504 * Helper for finding best parent to provide a given frequency. This can be used
505 * directly as a determine_rate callback (e.g. for a mux), or from a more
506 * complex clock that may combine a mux with other operations.
508 int __clk_mux_determine_rate(struct clk_hw
*hw
,
509 struct clk_rate_request
*req
)
511 return clk_mux_determine_rate_flags(hw
, req
, 0);
513 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate
);
515 int __clk_mux_determine_rate_closest(struct clk_hw
*hw
,
516 struct clk_rate_request
*req
)
518 return clk_mux_determine_rate_flags(hw
, req
, CLK_MUX_ROUND_CLOSEST
);
520 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest
);
524 static void clk_core_unprepare(struct clk_core
*core
)
526 lockdep_assert_held(&prepare_lock
);
531 if (WARN_ON(core
->prepare_count
== 0))
534 if (WARN_ON(core
->prepare_count
== 1 && core
->flags
& CLK_IS_CRITICAL
))
537 if (--core
->prepare_count
> 0)
540 WARN_ON(core
->enable_count
> 0);
542 trace_clk_unprepare(core
);
544 if (core
->ops
->unprepare
)
545 core
->ops
->unprepare(core
->hw
);
547 clk_pm_runtime_put(core
);
549 trace_clk_unprepare_complete(core
);
550 clk_core_unprepare(core
->parent
);
553 static void clk_core_unprepare_lock(struct clk_core
*core
)
556 clk_core_unprepare(core
);
557 clk_prepare_unlock();
561 * clk_unprepare - undo preparation of a clock source
562 * @clk: the clk being unprepared
564 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
565 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
566 * if the operation may sleep. One example is a clk which is accessed over
567 * I2c. In the complex case a clk gate operation may require a fast and a slow
568 * part. It is this reason that clk_unprepare and clk_disable are not mutually
569 * exclusive. In fact clk_disable must be called before clk_unprepare.
571 void clk_unprepare(struct clk
*clk
)
573 if (IS_ERR_OR_NULL(clk
))
576 clk_core_unprepare_lock(clk
->core
);
578 EXPORT_SYMBOL_GPL(clk_unprepare
);
580 static int clk_core_prepare(struct clk_core
*core
)
584 lockdep_assert_held(&prepare_lock
);
589 if (core
->prepare_count
== 0) {
590 ret
= clk_pm_runtime_get(core
);
594 ret
= clk_core_prepare(core
->parent
);
598 trace_clk_prepare(core
);
600 if (core
->ops
->prepare
)
601 ret
= core
->ops
->prepare(core
->hw
);
603 trace_clk_prepare_complete(core
);
609 core
->prepare_count
++;
613 clk_core_unprepare(core
->parent
);
615 clk_pm_runtime_put(core
);
619 static int clk_core_prepare_lock(struct clk_core
*core
)
624 ret
= clk_core_prepare(core
);
625 clk_prepare_unlock();
631 * clk_prepare - prepare a clock source
632 * @clk: the clk being prepared
634 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
635 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
636 * operation may sleep. One example is a clk which is accessed over I2c. In
637 * the complex case a clk ungate operation may require a fast and a slow part.
638 * It is this reason that clk_prepare and clk_enable are not mutually
639 * exclusive. In fact clk_prepare must be called before clk_enable.
640 * Returns 0 on success, -EERROR otherwise.
642 int clk_prepare(struct clk
*clk
)
647 return clk_core_prepare_lock(clk
->core
);
649 EXPORT_SYMBOL_GPL(clk_prepare
);
651 static void clk_core_disable(struct clk_core
*core
)
653 lockdep_assert_held(&enable_lock
);
658 if (WARN_ON(core
->enable_count
== 0))
661 if (WARN_ON(core
->enable_count
== 1 && core
->flags
& CLK_IS_CRITICAL
))
664 if (--core
->enable_count
> 0)
667 trace_clk_disable_rcuidle(core
);
669 if (core
->ops
->disable
)
670 core
->ops
->disable(core
->hw
);
672 trace_clk_disable_complete_rcuidle(core
);
674 clk_core_disable(core
->parent
);
677 static void clk_core_disable_lock(struct clk_core
*core
)
681 flags
= clk_enable_lock();
682 clk_core_disable(core
);
683 clk_enable_unlock(flags
);
687 * clk_disable - gate a clock
688 * @clk: the clk being gated
690 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
691 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
692 * clk if the operation is fast and will never sleep. One example is a
693 * SoC-internal clk which is controlled via simple register writes. In the
694 * complex case a clk gate operation may require a fast and a slow part. It is
695 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
696 * In fact clk_disable must be called before clk_unprepare.
698 void clk_disable(struct clk
*clk
)
700 if (IS_ERR_OR_NULL(clk
))
703 clk_core_disable_lock(clk
->core
);
705 EXPORT_SYMBOL_GPL(clk_disable
);
707 static int clk_core_enable(struct clk_core
*core
)
711 lockdep_assert_held(&enable_lock
);
716 if (WARN_ON(core
->prepare_count
== 0))
719 if (core
->enable_count
== 0) {
720 ret
= clk_core_enable(core
->parent
);
725 trace_clk_enable_rcuidle(core
);
727 if (core
->ops
->enable
)
728 ret
= core
->ops
->enable(core
->hw
);
730 trace_clk_enable_complete_rcuidle(core
);
733 clk_core_disable(core
->parent
);
738 core
->enable_count
++;
742 static int clk_core_enable_lock(struct clk_core
*core
)
747 flags
= clk_enable_lock();
748 ret
= clk_core_enable(core
);
749 clk_enable_unlock(flags
);
755 * clk_enable - ungate a clock
756 * @clk: the clk being ungated
758 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
759 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
760 * if the operation will never sleep. One example is a SoC-internal clk which
761 * is controlled via simple register writes. In the complex case a clk ungate
762 * operation may require a fast and a slow part. It is this reason that
763 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
764 * must be called before clk_enable. Returns 0 on success, -EERROR
767 int clk_enable(struct clk
*clk
)
772 return clk_core_enable_lock(clk
->core
);
774 EXPORT_SYMBOL_GPL(clk_enable
);
776 static int clk_core_prepare_enable(struct clk_core
*core
)
780 ret
= clk_core_prepare_lock(core
);
784 ret
= clk_core_enable_lock(core
);
786 clk_core_unprepare_lock(core
);
791 static void clk_core_disable_unprepare(struct clk_core
*core
)
793 clk_core_disable_lock(core
);
794 clk_core_unprepare_lock(core
);
797 static void clk_unprepare_unused_subtree(struct clk_core
*core
)
799 struct clk_core
*child
;
801 lockdep_assert_held(&prepare_lock
);
803 hlist_for_each_entry(child
, &core
->children
, child_node
)
804 clk_unprepare_unused_subtree(child
);
806 if (core
->prepare_count
)
809 if (core
->flags
& CLK_IGNORE_UNUSED
)
812 if (clk_pm_runtime_get(core
))
815 if (clk_core_is_prepared(core
)) {
816 trace_clk_unprepare(core
);
817 if (core
->ops
->unprepare_unused
)
818 core
->ops
->unprepare_unused(core
->hw
);
819 else if (core
->ops
->unprepare
)
820 core
->ops
->unprepare(core
->hw
);
821 trace_clk_unprepare_complete(core
);
824 clk_pm_runtime_put(core
);
827 static void clk_disable_unused_subtree(struct clk_core
*core
)
829 struct clk_core
*child
;
832 lockdep_assert_held(&prepare_lock
);
834 hlist_for_each_entry(child
, &core
->children
, child_node
)
835 clk_disable_unused_subtree(child
);
837 if (core
->flags
& CLK_OPS_PARENT_ENABLE
)
838 clk_core_prepare_enable(core
->parent
);
840 if (clk_pm_runtime_get(core
))
843 flags
= clk_enable_lock();
845 if (core
->enable_count
)
848 if (core
->flags
& CLK_IGNORE_UNUSED
)
852 * some gate clocks have special needs during the disable-unused
853 * sequence. call .disable_unused if available, otherwise fall
856 if (clk_core_is_enabled(core
)) {
857 trace_clk_disable(core
);
858 if (core
->ops
->disable_unused
)
859 core
->ops
->disable_unused(core
->hw
);
860 else if (core
->ops
->disable
)
861 core
->ops
->disable(core
->hw
);
862 trace_clk_disable_complete(core
);
866 clk_enable_unlock(flags
);
867 clk_pm_runtime_put(core
);
869 if (core
->flags
& CLK_OPS_PARENT_ENABLE
)
870 clk_core_disable_unprepare(core
->parent
);
873 static bool clk_ignore_unused
;
874 static int __init
clk_ignore_unused_setup(char *__unused
)
876 clk_ignore_unused
= true;
879 __setup("clk_ignore_unused", clk_ignore_unused_setup
);
881 static int clk_disable_unused(void)
883 struct clk_core
*core
;
885 if (clk_ignore_unused
) {
886 pr_warn("clk: Not disabling unused clocks\n");
892 hlist_for_each_entry(core
, &clk_root_list
, child_node
)
893 clk_disable_unused_subtree(core
);
895 hlist_for_each_entry(core
, &clk_orphan_list
, child_node
)
896 clk_disable_unused_subtree(core
);
898 hlist_for_each_entry(core
, &clk_root_list
, child_node
)
899 clk_unprepare_unused_subtree(core
);
901 hlist_for_each_entry(core
, &clk_orphan_list
, child_node
)
902 clk_unprepare_unused_subtree(core
);
904 clk_prepare_unlock();
908 late_initcall_sync(clk_disable_unused
);
910 static int clk_core_round_rate_nolock(struct clk_core
*core
,
911 struct clk_rate_request
*req
)
913 struct clk_core
*parent
;
916 lockdep_assert_held(&prepare_lock
);
921 parent
= core
->parent
;
923 req
->best_parent_hw
= parent
->hw
;
924 req
->best_parent_rate
= parent
->rate
;
926 req
->best_parent_hw
= NULL
;
927 req
->best_parent_rate
= 0;
930 if (core
->ops
->determine_rate
) {
931 return core
->ops
->determine_rate(core
->hw
, req
);
932 } else if (core
->ops
->round_rate
) {
933 rate
= core
->ops
->round_rate(core
->hw
, req
->rate
,
934 &req
->best_parent_rate
);
939 } else if (core
->flags
& CLK_SET_RATE_PARENT
) {
940 return clk_core_round_rate_nolock(parent
, req
);
942 req
->rate
= core
->rate
;
949 * __clk_determine_rate - get the closest rate actually supported by a clock
950 * @hw: determine the rate of this clock
951 * @req: target rate request
953 * Useful for clk_ops such as .set_rate and .determine_rate.
955 int __clk_determine_rate(struct clk_hw
*hw
, struct clk_rate_request
*req
)
962 return clk_core_round_rate_nolock(hw
->core
, req
);
964 EXPORT_SYMBOL_GPL(__clk_determine_rate
);
966 unsigned long clk_hw_round_rate(struct clk_hw
*hw
, unsigned long rate
)
969 struct clk_rate_request req
;
971 clk_core_get_boundaries(hw
->core
, &req
.min_rate
, &req
.max_rate
);
974 ret
= clk_core_round_rate_nolock(hw
->core
, &req
);
980 EXPORT_SYMBOL_GPL(clk_hw_round_rate
);
983 * clk_round_rate - round the given rate for a clk
984 * @clk: the clk for which we are rounding a rate
985 * @rate: the rate which is to be rounded
987 * Takes in a rate as input and rounds it to a rate that the clk can actually
988 * use which is then returned. If clk doesn't support round_rate operation
989 * then the parent rate is returned.
991 long clk_round_rate(struct clk
*clk
, unsigned long rate
)
993 struct clk_rate_request req
;
1001 clk_core_get_boundaries(clk
->core
, &req
.min_rate
, &req
.max_rate
);
1004 ret
= clk_core_round_rate_nolock(clk
->core
, &req
);
1005 clk_prepare_unlock();
1012 EXPORT_SYMBOL_GPL(clk_round_rate
);
1015 * __clk_notify - call clk notifier chain
1016 * @core: clk that is changing rate
1017 * @msg: clk notifier type (see include/linux/clk.h)
1018 * @old_rate: old clk rate
1019 * @new_rate: new clk rate
1021 * Triggers a notifier call chain on the clk rate-change notification
1022 * for 'clk'. Passes a pointer to the struct clk and the previous
1023 * and current rates to the notifier callback. Intended to be called by
1024 * internal clock code only. Returns NOTIFY_DONE from the last driver
1025 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1026 * a driver returns that.
1028 static int __clk_notify(struct clk_core
*core
, unsigned long msg
,
1029 unsigned long old_rate
, unsigned long new_rate
)
1031 struct clk_notifier
*cn
;
1032 struct clk_notifier_data cnd
;
1033 int ret
= NOTIFY_DONE
;
1035 cnd
.old_rate
= old_rate
;
1036 cnd
.new_rate
= new_rate
;
1038 list_for_each_entry(cn
, &clk_notifier_list
, node
) {
1039 if (cn
->clk
->core
== core
) {
1041 ret
= srcu_notifier_call_chain(&cn
->notifier_head
, msg
,
1043 if (ret
& NOTIFY_STOP_MASK
)
1052 * __clk_recalc_accuracies
1053 * @core: first clk in the subtree
1055 * Walks the subtree of clks starting with clk and recalculates accuracies as
1056 * it goes. Note that if a clk does not implement the .recalc_accuracy
1057 * callback then it is assumed that the clock will take on the accuracy of its
1060 static void __clk_recalc_accuracies(struct clk_core
*core
)
1062 unsigned long parent_accuracy
= 0;
1063 struct clk_core
*child
;
1065 lockdep_assert_held(&prepare_lock
);
1068 parent_accuracy
= core
->parent
->accuracy
;
1070 if (core
->ops
->recalc_accuracy
)
1071 core
->accuracy
= core
->ops
->recalc_accuracy(core
->hw
,
1074 core
->accuracy
= parent_accuracy
;
1076 hlist_for_each_entry(child
, &core
->children
, child_node
)
1077 __clk_recalc_accuracies(child
);
1080 static long clk_core_get_accuracy(struct clk_core
*core
)
1082 unsigned long accuracy
;
1085 if (core
&& (core
->flags
& CLK_GET_ACCURACY_NOCACHE
))
1086 __clk_recalc_accuracies(core
);
1088 accuracy
= __clk_get_accuracy(core
);
1089 clk_prepare_unlock();
1095 * clk_get_accuracy - return the accuracy of clk
1096 * @clk: the clk whose accuracy is being returned
1098 * Simply returns the cached accuracy of the clk, unless
1099 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1101 * If clk is NULL then returns 0.
1103 long clk_get_accuracy(struct clk
*clk
)
1108 return clk_core_get_accuracy(clk
->core
);
1110 EXPORT_SYMBOL_GPL(clk_get_accuracy
);
1112 static unsigned long clk_recalc(struct clk_core
*core
,
1113 unsigned long parent_rate
)
1115 unsigned long rate
= parent_rate
;
1117 if (core
->ops
->recalc_rate
&& !clk_pm_runtime_get(core
)) {
1118 rate
= core
->ops
->recalc_rate(core
->hw
, parent_rate
);
1119 clk_pm_runtime_put(core
);
1125 * __clk_recalc_rates
1126 * @core: first clk in the subtree
1127 * @msg: notification type (see include/linux/clk.h)
1129 * Walks the subtree of clks starting with clk and recalculates rates as it
1130 * goes. Note that if a clk does not implement the .recalc_rate callback then
1131 * it is assumed that the clock will take on the rate of its parent.
1133 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1136 static void __clk_recalc_rates(struct clk_core
*core
, unsigned long msg
)
1138 unsigned long old_rate
;
1139 unsigned long parent_rate
= 0;
1140 struct clk_core
*child
;
1142 lockdep_assert_held(&prepare_lock
);
1144 old_rate
= core
->rate
;
1147 parent_rate
= core
->parent
->rate
;
1149 core
->rate
= clk_recalc(core
, parent_rate
);
1152 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1153 * & ABORT_RATE_CHANGE notifiers
1155 if (core
->notifier_count
&& msg
)
1156 __clk_notify(core
, msg
, old_rate
, core
->rate
);
1158 hlist_for_each_entry(child
, &core
->children
, child_node
)
1159 __clk_recalc_rates(child
, msg
);
1162 static unsigned long clk_core_get_rate(struct clk_core
*core
)
1168 if (core
&& (core
->flags
& CLK_GET_RATE_NOCACHE
))
1169 __clk_recalc_rates(core
, 0);
1171 rate
= clk_core_get_rate_nolock(core
);
1172 clk_prepare_unlock();
1178 * clk_get_rate - return the rate of clk
1179 * @clk: the clk whose rate is being returned
1181 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1182 * is set, which means a recalc_rate will be issued.
1183 * If clk is NULL then returns 0.
1185 unsigned long clk_get_rate(struct clk
*clk
)
1190 return clk_core_get_rate(clk
->core
);
1192 EXPORT_SYMBOL_GPL(clk_get_rate
);
1194 static int clk_fetch_parent_index(struct clk_core
*core
,
1195 struct clk_core
*parent
)
1202 for (i
= 0; i
< core
->num_parents
; i
++)
1203 if (clk_core_get_parent_by_index(core
, i
) == parent
)
1210 * Update the orphan status of @core and all its children.
1212 static void clk_core_update_orphan_status(struct clk_core
*core
, bool is_orphan
)
1214 struct clk_core
*child
;
1216 core
->orphan
= is_orphan
;
1218 hlist_for_each_entry(child
, &core
->children
, child_node
)
1219 clk_core_update_orphan_status(child
, is_orphan
);
1222 static void clk_reparent(struct clk_core
*core
, struct clk_core
*new_parent
)
1224 bool was_orphan
= core
->orphan
;
1226 hlist_del(&core
->child_node
);
1229 bool becomes_orphan
= new_parent
->orphan
;
1231 /* avoid duplicate POST_RATE_CHANGE notifications */
1232 if (new_parent
->new_child
== core
)
1233 new_parent
->new_child
= NULL
;
1235 hlist_add_head(&core
->child_node
, &new_parent
->children
);
1237 if (was_orphan
!= becomes_orphan
)
1238 clk_core_update_orphan_status(core
, becomes_orphan
);
1240 hlist_add_head(&core
->child_node
, &clk_orphan_list
);
1242 clk_core_update_orphan_status(core
, true);
1245 core
->parent
= new_parent
;
1248 static struct clk_core
*__clk_set_parent_before(struct clk_core
*core
,
1249 struct clk_core
*parent
)
1251 unsigned long flags
;
1252 struct clk_core
*old_parent
= core
->parent
;
1255 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1257 * 2. Migrate prepare state between parents and prevent race with
1260 * If the clock is not prepared, then a race with
1261 * clk_enable/disable() is impossible since we already have the
1262 * prepare lock (future calls to clk_enable() need to be preceded by
1265 * If the clock is prepared, migrate the prepared state to the new
1266 * parent and also protect against a race with clk_enable() by
1267 * forcing the clock and the new parent on. This ensures that all
1268 * future calls to clk_enable() are practically NOPs with respect to
1269 * hardware and software states.
1271 * See also: Comment for clk_set_parent() below.
1274 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1275 if (core
->flags
& CLK_OPS_PARENT_ENABLE
) {
1276 clk_core_prepare_enable(old_parent
);
1277 clk_core_prepare_enable(parent
);
1280 /* migrate prepare count if > 0 */
1281 if (core
->prepare_count
) {
1282 clk_core_prepare_enable(parent
);
1283 clk_core_enable_lock(core
);
1286 /* update the clk tree topology */
1287 flags
= clk_enable_lock();
1288 clk_reparent(core
, parent
);
1289 clk_enable_unlock(flags
);
1294 static void __clk_set_parent_after(struct clk_core
*core
,
1295 struct clk_core
*parent
,
1296 struct clk_core
*old_parent
)
1299 * Finish the migration of prepare state and undo the changes done
1300 * for preventing a race with clk_enable().
1302 if (core
->prepare_count
) {
1303 clk_core_disable_lock(core
);
1304 clk_core_disable_unprepare(old_parent
);
1307 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1308 if (core
->flags
& CLK_OPS_PARENT_ENABLE
) {
1309 clk_core_disable_unprepare(parent
);
1310 clk_core_disable_unprepare(old_parent
);
1314 static int __clk_set_parent(struct clk_core
*core
, struct clk_core
*parent
,
1317 unsigned long flags
;
1319 struct clk_core
*old_parent
;
1321 old_parent
= __clk_set_parent_before(core
, parent
);
1323 trace_clk_set_parent(core
, parent
);
1325 /* change clock input source */
1326 if (parent
&& core
->ops
->set_parent
)
1327 ret
= core
->ops
->set_parent(core
->hw
, p_index
);
1329 trace_clk_set_parent_complete(core
, parent
);
1332 flags
= clk_enable_lock();
1333 clk_reparent(core
, old_parent
);
1334 clk_enable_unlock(flags
);
1335 __clk_set_parent_after(core
, old_parent
, parent
);
1340 __clk_set_parent_after(core
, parent
, old_parent
);
1346 * __clk_speculate_rates
1347 * @core: first clk in the subtree
1348 * @parent_rate: the "future" rate of clk's parent
1350 * Walks the subtree of clks starting with clk, speculating rates as it
1351 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1353 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1354 * pre-rate change notifications and returns early if no clks in the
1355 * subtree have subscribed to the notifications. Note that if a clk does not
1356 * implement the .recalc_rate callback then it is assumed that the clock will
1357 * take on the rate of its parent.
1359 static int __clk_speculate_rates(struct clk_core
*core
,
1360 unsigned long parent_rate
)
1362 struct clk_core
*child
;
1363 unsigned long new_rate
;
1364 int ret
= NOTIFY_DONE
;
1366 lockdep_assert_held(&prepare_lock
);
1368 new_rate
= clk_recalc(core
, parent_rate
);
1370 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
1371 if (core
->notifier_count
)
1372 ret
= __clk_notify(core
, PRE_RATE_CHANGE
, core
->rate
, new_rate
);
1374 if (ret
& NOTIFY_STOP_MASK
) {
1375 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
1376 __func__
, core
->name
, ret
);
1380 hlist_for_each_entry(child
, &core
->children
, child_node
) {
1381 ret
= __clk_speculate_rates(child
, new_rate
);
1382 if (ret
& NOTIFY_STOP_MASK
)
1390 static void clk_calc_subtree(struct clk_core
*core
, unsigned long new_rate
,
1391 struct clk_core
*new_parent
, u8 p_index
)
1393 struct clk_core
*child
;
1395 core
->new_rate
= new_rate
;
1396 core
->new_parent
= new_parent
;
1397 core
->new_parent_index
= p_index
;
1398 /* include clk in new parent's PRE_RATE_CHANGE notifications */
1399 core
->new_child
= NULL
;
1400 if (new_parent
&& new_parent
!= core
->parent
)
1401 new_parent
->new_child
= core
;
1403 hlist_for_each_entry(child
, &core
->children
, child_node
) {
1404 child
->new_rate
= clk_recalc(child
, new_rate
);
1405 clk_calc_subtree(child
, child
->new_rate
, NULL
, 0);
1410 * calculate the new rates returning the topmost clock that has to be
1413 static struct clk_core
*clk_calc_new_rates(struct clk_core
*core
,
1416 struct clk_core
*top
= core
;
1417 struct clk_core
*old_parent
, *parent
;
1418 unsigned long best_parent_rate
= 0;
1419 unsigned long new_rate
;
1420 unsigned long min_rate
;
1421 unsigned long max_rate
;
1426 if (IS_ERR_OR_NULL(core
))
1429 /* save parent rate, if it exists */
1430 parent
= old_parent
= core
->parent
;
1432 best_parent_rate
= parent
->rate
;
1434 clk_core_get_boundaries(core
, &min_rate
, &max_rate
);
1436 /* find the closest rate and parent clk/rate */
1437 if (core
->ops
->determine_rate
) {
1438 struct clk_rate_request req
;
1441 req
.min_rate
= min_rate
;
1442 req
.max_rate
= max_rate
;
1444 req
.best_parent_hw
= parent
->hw
;
1445 req
.best_parent_rate
= parent
->rate
;
1447 req
.best_parent_hw
= NULL
;
1448 req
.best_parent_rate
= 0;
1451 ret
= core
->ops
->determine_rate(core
->hw
, &req
);
1455 best_parent_rate
= req
.best_parent_rate
;
1456 new_rate
= req
.rate
;
1457 parent
= req
.best_parent_hw
? req
.best_parent_hw
->core
: NULL
;
1458 } else if (core
->ops
->round_rate
) {
1459 ret
= core
->ops
->round_rate(core
->hw
, rate
,
1465 if (new_rate
< min_rate
|| new_rate
> max_rate
)
1467 } else if (!parent
|| !(core
->flags
& CLK_SET_RATE_PARENT
)) {
1468 /* pass-through clock without adjustable parent */
1469 core
->new_rate
= core
->rate
;
1472 /* pass-through clock with adjustable parent */
1473 top
= clk_calc_new_rates(parent
, rate
);
1474 new_rate
= parent
->new_rate
;
1478 /* some clocks must be gated to change parent */
1479 if (parent
!= old_parent
&&
1480 (core
->flags
& CLK_SET_PARENT_GATE
) && core
->prepare_count
) {
1481 pr_debug("%s: %s not gated but wants to reparent\n",
1482 __func__
, core
->name
);
1486 /* try finding the new parent index */
1487 if (parent
&& core
->num_parents
> 1) {
1488 p_index
= clk_fetch_parent_index(core
, parent
);
1490 pr_debug("%s: clk %s can not be parent of clk %s\n",
1491 __func__
, parent
->name
, core
->name
);
1496 if ((core
->flags
& CLK_SET_RATE_PARENT
) && parent
&&
1497 best_parent_rate
!= parent
->rate
)
1498 top
= clk_calc_new_rates(parent
, best_parent_rate
);
1501 clk_calc_subtree(core
, new_rate
, parent
, p_index
);
1507 * Notify about rate changes in a subtree. Always walk down the whole tree
1508 * so that in case of an error we can walk down the whole tree again and
1511 static struct clk_core
*clk_propagate_rate_change(struct clk_core
*core
,
1512 unsigned long event
)
1514 struct clk_core
*child
, *tmp_clk
, *fail_clk
= NULL
;
1515 int ret
= NOTIFY_DONE
;
1517 if (core
->rate
== core
->new_rate
)
1520 if (core
->notifier_count
) {
1521 ret
= __clk_notify(core
, event
, core
->rate
, core
->new_rate
);
1522 if (ret
& NOTIFY_STOP_MASK
)
1526 hlist_for_each_entry(child
, &core
->children
, child_node
) {
1527 /* Skip children who will be reparented to another clock */
1528 if (child
->new_parent
&& child
->new_parent
!= core
)
1530 tmp_clk
= clk_propagate_rate_change(child
, event
);
1535 /* handle the new child who might not be in core->children yet */
1536 if (core
->new_child
) {
1537 tmp_clk
= clk_propagate_rate_change(core
->new_child
, event
);
1546 * walk down a subtree and set the new rates notifying the rate
1549 static void clk_change_rate(struct clk_core
*core
)
1551 struct clk_core
*child
;
1552 struct hlist_node
*tmp
;
1553 unsigned long old_rate
;
1554 unsigned long best_parent_rate
= 0;
1555 bool skip_set_rate
= false;
1556 struct clk_core
*old_parent
;
1557 struct clk_core
*parent
= NULL
;
1559 old_rate
= core
->rate
;
1561 if (core
->new_parent
) {
1562 parent
= core
->new_parent
;
1563 best_parent_rate
= core
->new_parent
->rate
;
1564 } else if (core
->parent
) {
1565 parent
= core
->parent
;
1566 best_parent_rate
= core
->parent
->rate
;
1569 if (clk_pm_runtime_get(core
))
1572 if (core
->flags
& CLK_SET_RATE_UNGATE
) {
1573 unsigned long flags
;
1575 clk_core_prepare(core
);
1576 flags
= clk_enable_lock();
1577 clk_core_enable(core
);
1578 clk_enable_unlock(flags
);
1581 if (core
->new_parent
&& core
->new_parent
!= core
->parent
) {
1582 old_parent
= __clk_set_parent_before(core
, core
->new_parent
);
1583 trace_clk_set_parent(core
, core
->new_parent
);
1585 if (core
->ops
->set_rate_and_parent
) {
1586 skip_set_rate
= true;
1587 core
->ops
->set_rate_and_parent(core
->hw
, core
->new_rate
,
1589 core
->new_parent_index
);
1590 } else if (core
->ops
->set_parent
) {
1591 core
->ops
->set_parent(core
->hw
, core
->new_parent_index
);
1594 trace_clk_set_parent_complete(core
, core
->new_parent
);
1595 __clk_set_parent_after(core
, core
->new_parent
, old_parent
);
1598 if (core
->flags
& CLK_OPS_PARENT_ENABLE
)
1599 clk_core_prepare_enable(parent
);
1601 trace_clk_set_rate(core
, core
->new_rate
);
1603 if (!skip_set_rate
&& core
->ops
->set_rate
)
1604 core
->ops
->set_rate(core
->hw
, core
->new_rate
, best_parent_rate
);
1606 trace_clk_set_rate_complete(core
, core
->new_rate
);
1608 core
->rate
= clk_recalc(core
, best_parent_rate
);
1610 if (core
->flags
& CLK_SET_RATE_UNGATE
) {
1611 unsigned long flags
;
1613 flags
= clk_enable_lock();
1614 clk_core_disable(core
);
1615 clk_enable_unlock(flags
);
1616 clk_core_unprepare(core
);
1619 if (core
->flags
& CLK_OPS_PARENT_ENABLE
)
1620 clk_core_disable_unprepare(parent
);
1622 if (core
->notifier_count
&& old_rate
!= core
->rate
)
1623 __clk_notify(core
, POST_RATE_CHANGE
, old_rate
, core
->rate
);
1625 if (core
->flags
& CLK_RECALC_NEW_RATES
)
1626 (void)clk_calc_new_rates(core
, core
->new_rate
);
1629 * Use safe iteration, as change_rate can actually swap parents
1630 * for certain clock types.
1632 hlist_for_each_entry_safe(child
, tmp
, &core
->children
, child_node
) {
1633 /* Skip children who will be reparented to another clock */
1634 if (child
->new_parent
&& child
->new_parent
!= core
)
1636 clk_change_rate(child
);
1639 /* handle the new child who might not be in core->children yet */
1640 if (core
->new_child
)
1641 clk_change_rate(core
->new_child
);
1643 clk_pm_runtime_put(core
);
1646 static unsigned long clk_core_req_round_rate_nolock(struct clk_core
*core
,
1647 unsigned long req_rate
)
1650 struct clk_rate_request req
;
1652 lockdep_assert_held(&prepare_lock
);
1657 clk_core_get_boundaries(core
, &req
.min_rate
, &req
.max_rate
);
1658 req
.rate
= req_rate
;
1660 ret
= clk_core_round_rate_nolock(core
, &req
);
1662 return ret
? 0 : req
.rate
;
1665 static int clk_core_set_rate_nolock(struct clk_core
*core
,
1666 unsigned long req_rate
)
1668 struct clk_core
*top
, *fail_clk
;
1675 rate
= clk_core_req_round_rate_nolock(core
, req_rate
);
1677 /* bail early if nothing to do */
1678 if (rate
== clk_core_get_rate_nolock(core
))
1681 if ((core
->flags
& CLK_SET_RATE_GATE
) && core
->prepare_count
)
1684 /* calculate new rates and get the topmost changed clock */
1685 top
= clk_calc_new_rates(core
, req_rate
);
1689 ret
= clk_pm_runtime_get(core
);
1693 /* notify that we are about to change rates */
1694 fail_clk
= clk_propagate_rate_change(top
, PRE_RATE_CHANGE
);
1696 pr_debug("%s: failed to set %s rate\n", __func__
,
1698 clk_propagate_rate_change(top
, ABORT_RATE_CHANGE
);
1703 /* change the rates */
1704 clk_change_rate(top
);
1706 core
->req_rate
= req_rate
;
1708 clk_pm_runtime_put(core
);
1714 * clk_set_rate - specify a new rate for clk
1715 * @clk: the clk whose rate is being changed
1716 * @rate: the new rate for clk
1718 * In the simplest case clk_set_rate will only adjust the rate of clk.
1720 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1721 * propagate up to clk's parent; whether or not this happens depends on the
1722 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1723 * after calling .round_rate then upstream parent propagation is ignored. If
1724 * *parent_rate comes back with a new rate for clk's parent then we propagate
1725 * up to clk's parent and set its rate. Upward propagation will continue
1726 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1727 * .round_rate stops requesting changes to clk's parent_rate.
1729 * Rate changes are accomplished via tree traversal that also recalculates the
1730 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
1732 * Returns 0 on success, -EERROR otherwise.
1734 int clk_set_rate(struct clk
*clk
, unsigned long rate
)
1741 /* prevent racing with updates to the clock topology */
1744 ret
= clk_core_set_rate_nolock(clk
->core
, rate
);
1746 clk_prepare_unlock();
1750 EXPORT_SYMBOL_GPL(clk_set_rate
);
1753 * clk_set_rate_range - set a rate range for a clock source
1754 * @clk: clock source
1755 * @min: desired minimum clock rate in Hz, inclusive
1756 * @max: desired maximum clock rate in Hz, inclusive
1758 * Returns success (0) or negative errno.
1760 int clk_set_rate_range(struct clk
*clk
, unsigned long min
, unsigned long max
)
1768 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
1769 __func__
, clk
->core
->name
, clk
->dev_id
, clk
->con_id
,
1776 if (min
!= clk
->min_rate
|| max
!= clk
->max_rate
) {
1777 clk
->min_rate
= min
;
1778 clk
->max_rate
= max
;
1779 ret
= clk_core_set_rate_nolock(clk
->core
, clk
->core
->req_rate
);
1782 clk_prepare_unlock();
1786 EXPORT_SYMBOL_GPL(clk_set_rate_range
);
1789 * clk_set_min_rate - set a minimum clock rate for a clock source
1790 * @clk: clock source
1791 * @rate: desired minimum clock rate in Hz, inclusive
1793 * Returns success (0) or negative errno.
1795 int clk_set_min_rate(struct clk
*clk
, unsigned long rate
)
1800 return clk_set_rate_range(clk
, rate
, clk
->max_rate
);
1802 EXPORT_SYMBOL_GPL(clk_set_min_rate
);
1805 * clk_set_max_rate - set a maximum clock rate for a clock source
1806 * @clk: clock source
1807 * @rate: desired maximum clock rate in Hz, inclusive
1809 * Returns success (0) or negative errno.
1811 int clk_set_max_rate(struct clk
*clk
, unsigned long rate
)
1816 return clk_set_rate_range(clk
, clk
->min_rate
, rate
);
1818 EXPORT_SYMBOL_GPL(clk_set_max_rate
);
1821 * clk_get_parent - return the parent of a clk
1822 * @clk: the clk whose parent gets returned
1824 * Simply returns clk->parent. Returns NULL if clk is NULL.
1826 struct clk
*clk_get_parent(struct clk
*clk
)
1834 /* TODO: Create a per-user clk and change callers to call clk_put */
1835 parent
= !clk
->core
->parent
? NULL
: clk
->core
->parent
->hw
->clk
;
1836 clk_prepare_unlock();
1840 EXPORT_SYMBOL_GPL(clk_get_parent
);
1842 static struct clk_core
*__clk_init_parent(struct clk_core
*core
)
1846 if (core
->num_parents
> 1 && core
->ops
->get_parent
)
1847 index
= core
->ops
->get_parent(core
->hw
);
1849 return clk_core_get_parent_by_index(core
, index
);
1852 static void clk_core_reparent(struct clk_core
*core
,
1853 struct clk_core
*new_parent
)
1855 clk_reparent(core
, new_parent
);
1856 __clk_recalc_accuracies(core
);
1857 __clk_recalc_rates(core
, POST_RATE_CHANGE
);
1860 void clk_hw_reparent(struct clk_hw
*hw
, struct clk_hw
*new_parent
)
1865 clk_core_reparent(hw
->core
, !new_parent
? NULL
: new_parent
->core
);
1869 * clk_has_parent - check if a clock is a possible parent for another
1870 * @clk: clock source
1871 * @parent: parent clock source
1873 * This function can be used in drivers that need to check that a clock can be
1874 * the parent of another without actually changing the parent.
1876 * Returns true if @parent is a possible parent for @clk, false otherwise.
1878 bool clk_has_parent(struct clk
*clk
, struct clk
*parent
)
1880 struct clk_core
*core
, *parent_core
;
1883 /* NULL clocks should be nops, so return success if either is NULL. */
1884 if (!clk
|| !parent
)
1888 parent_core
= parent
->core
;
1890 /* Optimize for the case where the parent is already the parent. */
1891 if (core
->parent
== parent_core
)
1894 for (i
= 0; i
< core
->num_parents
; i
++)
1895 if (strcmp(core
->parent_names
[i
], parent_core
->name
) == 0)
1900 EXPORT_SYMBOL_GPL(clk_has_parent
);
1902 static int clk_core_set_parent(struct clk_core
*core
, struct clk_core
*parent
)
1906 unsigned long p_rate
= 0;
1911 /* prevent racing with updates to the clock topology */
1914 if (core
->parent
== parent
)
1917 /* verify ops for for multi-parent clks */
1918 if ((core
->num_parents
> 1) && (!core
->ops
->set_parent
)) {
1923 /* check that we are allowed to re-parent if the clock is in use */
1924 if ((core
->flags
& CLK_SET_PARENT_GATE
) && core
->prepare_count
) {
1929 /* try finding the new parent index */
1931 p_index
= clk_fetch_parent_index(core
, parent
);
1933 pr_debug("%s: clk %s can not be parent of clk %s\n",
1934 __func__
, parent
->name
, core
->name
);
1938 p_rate
= parent
->rate
;
1941 ret
= clk_pm_runtime_get(core
);
1945 /* propagate PRE_RATE_CHANGE notifications */
1946 ret
= __clk_speculate_rates(core
, p_rate
);
1948 /* abort if a driver objects */
1949 if (ret
& NOTIFY_STOP_MASK
)
1952 /* do the re-parent */
1953 ret
= __clk_set_parent(core
, parent
, p_index
);
1955 /* propagate rate an accuracy recalculation accordingly */
1957 __clk_recalc_rates(core
, ABORT_RATE_CHANGE
);
1959 __clk_recalc_rates(core
, POST_RATE_CHANGE
);
1960 __clk_recalc_accuracies(core
);
1964 clk_pm_runtime_put(core
);
1966 clk_prepare_unlock();
1972 * clk_set_parent - switch the parent of a mux clk
1973 * @clk: the mux clk whose input we are switching
1974 * @parent: the new input to clk
1976 * Re-parent clk to use parent as its new input source. If clk is in
1977 * prepared state, the clk will get enabled for the duration of this call. If
1978 * that's not acceptable for a specific clk (Eg: the consumer can't handle
1979 * that, the reparenting is glitchy in hardware, etc), use the
1980 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
1982 * After successfully changing clk's parent clk_set_parent will update the
1983 * clk topology, sysfs topology and propagate rate recalculation via
1984 * __clk_recalc_rates.
1986 * Returns 0 on success, -EERROR otherwise.
1988 int clk_set_parent(struct clk
*clk
, struct clk
*parent
)
1993 return clk_core_set_parent(clk
->core
, parent
? parent
->core
: NULL
);
1995 EXPORT_SYMBOL_GPL(clk_set_parent
);
1998 * clk_set_phase - adjust the phase shift of a clock signal
1999 * @clk: clock signal source
2000 * @degrees: number of degrees the signal is shifted
2002 * Shifts the phase of a clock signal by the specified
2003 * degrees. Returns 0 on success, -EERROR otherwise.
2005 * This function makes no distinction about the input or reference
2006 * signal that we adjust the clock signal phase against. For example
2007 * phase locked-loop clock signal generators we may shift phase with
2008 * respect to feedback clock signal input, but for other cases the
2009 * clock phase may be shifted with respect to some other, unspecified
2012 * Additionally the concept of phase shift does not propagate through
2013 * the clock tree hierarchy, which sets it apart from clock rates and
2014 * clock accuracy. A parent clock phase attribute does not have an
2015 * impact on the phase attribute of a child clock.
2017 int clk_set_phase(struct clk
*clk
, int degrees
)
2024 /* sanity check degrees */
2031 trace_clk_set_phase(clk
->core
, degrees
);
2033 if (clk
->core
->ops
->set_phase
)
2034 ret
= clk
->core
->ops
->set_phase(clk
->core
->hw
, degrees
);
2036 trace_clk_set_phase_complete(clk
->core
, degrees
);
2039 clk
->core
->phase
= degrees
;
2041 clk_prepare_unlock();
2045 EXPORT_SYMBOL_GPL(clk_set_phase
);
2047 static int clk_core_get_phase(struct clk_core
*core
)
2052 /* Always try to update cached phase if possible */
2053 if (core
->ops
->get_phase
)
2054 core
->phase
= core
->ops
->get_phase(core
->hw
);
2056 clk_prepare_unlock();
2062 * clk_get_phase - return the phase shift of a clock signal
2063 * @clk: clock signal source
2065 * Returns the phase shift of a clock node in degrees, otherwise returns
2068 int clk_get_phase(struct clk
*clk
)
2073 return clk_core_get_phase(clk
->core
);
2075 EXPORT_SYMBOL_GPL(clk_get_phase
);
2078 * clk_is_match - check if two clk's point to the same hardware clock
2079 * @p: clk compared against q
2080 * @q: clk compared against p
2082 * Returns true if the two struct clk pointers both point to the same hardware
2083 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2084 * share the same struct clk_core object.
2086 * Returns false otherwise. Note that two NULL clks are treated as matching.
2088 bool clk_is_match(const struct clk
*p
, const struct clk
*q
)
2090 /* trivial case: identical struct clk's or both NULL */
2094 /* true if clk->core pointers match. Avoid dereferencing garbage */
2095 if (!IS_ERR_OR_NULL(p
) && !IS_ERR_OR_NULL(q
))
2096 if (p
->core
== q
->core
)
2101 EXPORT_SYMBOL_GPL(clk_is_match
);
2103 /*** debugfs support ***/
2105 #ifdef CONFIG_DEBUG_FS
2106 #include <linux/debugfs.h>
2108 static struct dentry
*rootdir
;
2109 static int inited
= 0;
2110 static DEFINE_MUTEX(clk_debug_lock
);
2111 static HLIST_HEAD(clk_debug_list
);
2113 static struct hlist_head
*all_lists
[] = {
2119 static struct hlist_head
*orphan_list
[] = {
2124 static void clk_summary_show_one(struct seq_file
*s
, struct clk_core
*c
,
2130 seq_printf(s
, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
2132 30 - level
* 3, c
->name
,
2133 c
->enable_count
, c
->prepare_count
, clk_core_get_rate(c
),
2134 clk_core_get_accuracy(c
), clk_core_get_phase(c
));
2137 static void clk_summary_show_subtree(struct seq_file
*s
, struct clk_core
*c
,
2140 struct clk_core
*child
;
2145 clk_summary_show_one(s
, c
, level
);
2147 hlist_for_each_entry(child
, &c
->children
, child_node
)
2148 clk_summary_show_subtree(s
, child
, level
+ 1);
2151 static int clk_summary_show(struct seq_file
*s
, void *data
)
2154 struct hlist_head
**lists
= (struct hlist_head
**)s
->private;
2156 seq_puts(s
, " clock enable_cnt prepare_cnt rate accuracy phase\n");
2157 seq_puts(s
, "----------------------------------------------------------------------------------------\n");
2161 for (; *lists
; lists
++)
2162 hlist_for_each_entry(c
, *lists
, child_node
)
2163 clk_summary_show_subtree(s
, c
, 0);
2165 clk_prepare_unlock();
2171 static int clk_summary_open(struct inode
*inode
, struct file
*file
)
2173 return single_open(file
, clk_summary_show
, inode
->i_private
);
2176 static const struct file_operations clk_summary_fops
= {
2177 .open
= clk_summary_open
,
2179 .llseek
= seq_lseek
,
2180 .release
= single_release
,
2183 static void clk_dump_one(struct seq_file
*s
, struct clk_core
*c
, int level
)
2188 /* This should be JSON format, i.e. elements separated with a comma */
2189 seq_printf(s
, "\"%s\": { ", c
->name
);
2190 seq_printf(s
, "\"enable_count\": %d,", c
->enable_count
);
2191 seq_printf(s
, "\"prepare_count\": %d,", c
->prepare_count
);
2192 seq_printf(s
, "\"rate\": %lu,", clk_core_get_rate(c
));
2193 seq_printf(s
, "\"accuracy\": %lu,", clk_core_get_accuracy(c
));
2194 seq_printf(s
, "\"phase\": %d", clk_core_get_phase(c
));
2197 static void clk_dump_subtree(struct seq_file
*s
, struct clk_core
*c
, int level
)
2199 struct clk_core
*child
;
2204 clk_dump_one(s
, c
, level
);
2206 hlist_for_each_entry(child
, &c
->children
, child_node
) {
2208 clk_dump_subtree(s
, child
, level
+ 1);
2214 static int clk_dump(struct seq_file
*s
, void *data
)
2217 bool first_node
= true;
2218 struct hlist_head
**lists
= (struct hlist_head
**)s
->private;
2223 for (; *lists
; lists
++) {
2224 hlist_for_each_entry(c
, *lists
, child_node
) {
2228 clk_dump_subtree(s
, c
, 0);
2232 clk_prepare_unlock();
2239 static int clk_dump_open(struct inode
*inode
, struct file
*file
)
2241 return single_open(file
, clk_dump
, inode
->i_private
);
2244 static const struct file_operations clk_dump_fops
= {
2245 .open
= clk_dump_open
,
2247 .llseek
= seq_lseek
,
2248 .release
= single_release
,
2251 static int possible_parents_dump(struct seq_file
*s
, void *data
)
2253 struct clk_core
*core
= s
->private;
2256 for (i
= 0; i
< core
->num_parents
- 1; i
++)
2257 seq_printf(s
, "%s ", core
->parent_names
[i
]);
2259 seq_printf(s
, "%s\n", core
->parent_names
[i
]);
2264 static int possible_parents_open(struct inode
*inode
, struct file
*file
)
2266 return single_open(file
, possible_parents_dump
, inode
->i_private
);
2269 static const struct file_operations possible_parents_fops
= {
2270 .open
= possible_parents_open
,
2272 .llseek
= seq_lseek
,
2273 .release
= single_release
,
2276 static int clk_debug_create_one(struct clk_core
*core
, struct dentry
*pdentry
)
2281 if (!core
|| !pdentry
) {
2286 d
= debugfs_create_dir(core
->name
, pdentry
);
2292 d
= debugfs_create_u32("clk_rate", S_IRUGO
, core
->dentry
,
2293 (u32
*)&core
->rate
);
2297 d
= debugfs_create_u32("clk_accuracy", S_IRUGO
, core
->dentry
,
2298 (u32
*)&core
->accuracy
);
2302 d
= debugfs_create_u32("clk_phase", S_IRUGO
, core
->dentry
,
2303 (u32
*)&core
->phase
);
2307 d
= debugfs_create_x32("clk_flags", S_IRUGO
, core
->dentry
,
2308 (u32
*)&core
->flags
);
2312 d
= debugfs_create_u32("clk_prepare_count", S_IRUGO
, core
->dentry
,
2313 (u32
*)&core
->prepare_count
);
2317 d
= debugfs_create_u32("clk_enable_count", S_IRUGO
, core
->dentry
,
2318 (u32
*)&core
->enable_count
);
2322 d
= debugfs_create_u32("clk_notifier_count", S_IRUGO
, core
->dentry
,
2323 (u32
*)&core
->notifier_count
);
2327 if (core
->num_parents
> 1) {
2328 d
= debugfs_create_file("clk_possible_parents", S_IRUGO
,
2329 core
->dentry
, core
, &possible_parents_fops
);
2334 if (core
->ops
->debug_init
) {
2335 ret
= core
->ops
->debug_init(core
->hw
, core
->dentry
);
2344 debugfs_remove_recursive(core
->dentry
);
2345 core
->dentry
= NULL
;
2351 * clk_debug_register - add a clk node to the debugfs clk directory
2352 * @core: the clk being added to the debugfs clk directory
2354 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2355 * initialized. Otherwise it bails out early since the debugfs clk directory
2356 * will be created lazily by clk_debug_init as part of a late_initcall.
2358 static int clk_debug_register(struct clk_core
*core
)
2362 mutex_lock(&clk_debug_lock
);
2363 hlist_add_head(&core
->debug_node
, &clk_debug_list
);
2368 ret
= clk_debug_create_one(core
, rootdir
);
2370 mutex_unlock(&clk_debug_lock
);
2376 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2377 * @core: the clk being removed from the debugfs clk directory
2379 * Dynamically removes a clk and all its child nodes from the
2380 * debugfs clk directory if clk->dentry points to debugfs created by
2381 * clk_debug_register in __clk_core_init.
2383 static void clk_debug_unregister(struct clk_core
*core
)
2385 mutex_lock(&clk_debug_lock
);
2386 hlist_del_init(&core
->debug_node
);
2387 debugfs_remove_recursive(core
->dentry
);
2388 core
->dentry
= NULL
;
2389 mutex_unlock(&clk_debug_lock
);
2392 struct dentry
*clk_debugfs_add_file(struct clk_hw
*hw
, char *name
, umode_t mode
,
2393 void *data
, const struct file_operations
*fops
)
2395 struct dentry
*d
= NULL
;
2397 if (hw
->core
->dentry
)
2398 d
= debugfs_create_file(name
, mode
, hw
->core
->dentry
, data
,
2403 EXPORT_SYMBOL_GPL(clk_debugfs_add_file
);
2406 * clk_debug_init - lazily populate the debugfs clk directory
2408 * clks are often initialized very early during boot before memory can be
2409 * dynamically allocated and well before debugfs is setup. This function
2410 * populates the debugfs clk directory once at boot-time when we know that
2411 * debugfs is setup. It should only be called once at boot-time, all other clks
2412 * added dynamically will be done so with clk_debug_register.
2414 static int __init
clk_debug_init(void)
2416 struct clk_core
*core
;
2419 rootdir
= debugfs_create_dir("clk", NULL
);
2424 d
= debugfs_create_file("clk_summary", S_IRUGO
, rootdir
, &all_lists
,
2429 d
= debugfs_create_file("clk_dump", S_IRUGO
, rootdir
, &all_lists
,
2434 d
= debugfs_create_file("clk_orphan_summary", S_IRUGO
, rootdir
,
2435 &orphan_list
, &clk_summary_fops
);
2439 d
= debugfs_create_file("clk_orphan_dump", S_IRUGO
, rootdir
,
2440 &orphan_list
, &clk_dump_fops
);
2444 mutex_lock(&clk_debug_lock
);
2445 hlist_for_each_entry(core
, &clk_debug_list
, debug_node
)
2446 clk_debug_create_one(core
, rootdir
);
2449 mutex_unlock(&clk_debug_lock
);
2453 late_initcall(clk_debug_init
);
2455 static inline int clk_debug_register(struct clk_core
*core
) { return 0; }
2456 static inline void clk_debug_reparent(struct clk_core
*core
,
2457 struct clk_core
*new_parent
)
2460 static inline void clk_debug_unregister(struct clk_core
*core
)
2466 * __clk_core_init - initialize the data structures in a struct clk_core
2467 * @core: clk_core being initialized
2469 * Initializes the lists in struct clk_core, queries the hardware for the
2470 * parent and rate and sets them both.
2472 static int __clk_core_init(struct clk_core
*core
)
2475 struct clk_core
*orphan
;
2476 struct hlist_node
*tmp2
;
2484 ret
= clk_pm_runtime_get(core
);
2488 /* check to see if a clock with this name is already registered */
2489 if (clk_core_lookup(core
->name
)) {
2490 pr_debug("%s: clk %s already initialized\n",
2491 __func__
, core
->name
);
2496 /* check that clk_ops are sane. See Documentation/clk.txt */
2497 if (core
->ops
->set_rate
&&
2498 !((core
->ops
->round_rate
|| core
->ops
->determine_rate
) &&
2499 core
->ops
->recalc_rate
)) {
2500 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
2501 __func__
, core
->name
);
2506 if (core
->ops
->set_parent
&& !core
->ops
->get_parent
) {
2507 pr_err("%s: %s must implement .get_parent & .set_parent\n",
2508 __func__
, core
->name
);
2513 if (core
->num_parents
> 1 && !core
->ops
->get_parent
) {
2514 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
2515 __func__
, core
->name
);
2520 if (core
->ops
->set_rate_and_parent
&&
2521 !(core
->ops
->set_parent
&& core
->ops
->set_rate
)) {
2522 pr_err("%s: %s must implement .set_parent & .set_rate\n",
2523 __func__
, core
->name
);
2528 /* throw a WARN if any entries in parent_names are NULL */
2529 for (i
= 0; i
< core
->num_parents
; i
++)
2530 WARN(!core
->parent_names
[i
],
2531 "%s: invalid NULL in %s's .parent_names\n",
2532 __func__
, core
->name
);
2534 core
->parent
= __clk_init_parent(core
);
2537 * Populate core->parent if parent has already been clk_core_init'd. If
2538 * parent has not yet been clk_core_init'd then place clk in the orphan
2539 * list. If clk doesn't have any parents then place it in the root
2542 * Every time a new clk is clk_init'd then we walk the list of orphan
2543 * clocks and re-parent any that are children of the clock currently
2547 hlist_add_head(&core
->child_node
,
2548 &core
->parent
->children
);
2549 core
->orphan
= core
->parent
->orphan
;
2550 } else if (!core
->num_parents
) {
2551 hlist_add_head(&core
->child_node
, &clk_root_list
);
2552 core
->orphan
= false;
2554 hlist_add_head(&core
->child_node
, &clk_orphan_list
);
2555 core
->orphan
= true;
2559 * Set clk's accuracy. The preferred method is to use
2560 * .recalc_accuracy. For simple clocks and lazy developers the default
2561 * fallback is to use the parent's accuracy. If a clock doesn't have a
2562 * parent (or is orphaned) then accuracy is set to zero (perfect
2565 if (core
->ops
->recalc_accuracy
)
2566 core
->accuracy
= core
->ops
->recalc_accuracy(core
->hw
,
2567 __clk_get_accuracy(core
->parent
));
2568 else if (core
->parent
)
2569 core
->accuracy
= core
->parent
->accuracy
;
2575 * Since a phase is by definition relative to its parent, just
2576 * query the current clock phase, or just assume it's in phase.
2578 if (core
->ops
->get_phase
)
2579 core
->phase
= core
->ops
->get_phase(core
->hw
);
2584 * Set clk's rate. The preferred method is to use .recalc_rate. For
2585 * simple clocks and lazy developers the default fallback is to use the
2586 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2587 * then rate is set to zero.
2589 if (core
->ops
->recalc_rate
)
2590 rate
= core
->ops
->recalc_rate(core
->hw
,
2591 clk_core_get_rate_nolock(core
->parent
));
2592 else if (core
->parent
)
2593 rate
= core
->parent
->rate
;
2596 core
->rate
= core
->req_rate
= rate
;
2599 * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
2600 * don't get accidentally disabled when walking the orphan tree and
2601 * reparenting clocks
2603 if (core
->flags
& CLK_IS_CRITICAL
) {
2604 unsigned long flags
;
2606 clk_core_prepare(core
);
2608 flags
= clk_enable_lock();
2609 clk_core_enable(core
);
2610 clk_enable_unlock(flags
);
2614 * walk the list of orphan clocks and reparent any that newly finds a
2617 hlist_for_each_entry_safe(orphan
, tmp2
, &clk_orphan_list
, child_node
) {
2618 struct clk_core
*parent
= __clk_init_parent(orphan
);
2621 * We need to use __clk_set_parent_before() and _after() to
2622 * to properly migrate any prepare/enable count of the orphan
2623 * clock. This is important for CLK_IS_CRITICAL clocks, which
2624 * are enabled during init but might not have a parent yet.
2627 /* update the clk tree topology */
2628 __clk_set_parent_before(orphan
, parent
);
2629 __clk_set_parent_after(orphan
, parent
, NULL
);
2630 __clk_recalc_accuracies(orphan
);
2631 __clk_recalc_rates(orphan
, 0);
2636 * optional platform-specific magic
2638 * The .init callback is not used by any of the basic clock types, but
2639 * exists for weird hardware that must perform initialization magic.
2640 * Please consider other ways of solving initialization problems before
2641 * using this callback, as its use is discouraged.
2643 if (core
->ops
->init
)
2644 core
->ops
->init(core
->hw
);
2646 kref_init(&core
->ref
);
2648 clk_pm_runtime_put(core
);
2650 clk_prepare_unlock();
2653 clk_debug_register(core
);
2658 struct clk
*__clk_create_clk(struct clk_hw
*hw
, const char *dev_id
,
2663 /* This is to allow this function to be chained to others */
2664 if (IS_ERR_OR_NULL(hw
))
2665 return ERR_CAST(hw
);
2667 clk
= kzalloc(sizeof(*clk
), GFP_KERNEL
);
2669 return ERR_PTR(-ENOMEM
);
2671 clk
->core
= hw
->core
;
2672 clk
->dev_id
= dev_id
;
2673 clk
->con_id
= kstrdup_const(con_id
, GFP_KERNEL
);
2674 clk
->max_rate
= ULONG_MAX
;
2677 hlist_add_head(&clk
->clks_node
, &hw
->core
->clks
);
2678 clk_prepare_unlock();
2683 void __clk_free_clk(struct clk
*clk
)
2686 hlist_del(&clk
->clks_node
);
2687 clk_prepare_unlock();
2689 kfree_const(clk
->con_id
);
2694 * clk_register - allocate a new clock, register it and return an opaque cookie
2695 * @dev: device that is registering this clock
2696 * @hw: link to hardware-specific clock data
2698 * clk_register is the primary interface for populating the clock tree with new
2699 * clock nodes. It returns a pointer to the newly allocated struct clk which
2700 * cannot be dereferenced by driver code but may be used in conjunction with the
2701 * rest of the clock API. In the event of an error clk_register will return an
2702 * error code; drivers must test for an error code after calling clk_register.
2704 struct clk
*clk_register(struct device
*dev
, struct clk_hw
*hw
)
2707 struct clk_core
*core
;
2709 core
= kzalloc(sizeof(*core
), GFP_KERNEL
);
2715 core
->name
= kstrdup_const(hw
->init
->name
, GFP_KERNEL
);
2721 if (WARN_ON(!hw
->init
->ops
)) {
2725 core
->ops
= hw
->init
->ops
;
2727 if (dev
&& pm_runtime_enabled(dev
))
2729 if (dev
&& dev
->driver
)
2730 core
->owner
= dev
->driver
->owner
;
2732 core
->flags
= hw
->init
->flags
;
2733 core
->num_parents
= hw
->init
->num_parents
;
2735 core
->max_rate
= ULONG_MAX
;
2738 /* allocate local copy in case parent_names is __initdata */
2739 core
->parent_names
= kcalloc(core
->num_parents
, sizeof(char *),
2742 if (!core
->parent_names
) {
2744 goto fail_parent_names
;
2748 /* copy each string name in case parent_names is __initdata */
2749 for (i
= 0; i
< core
->num_parents
; i
++) {
2750 core
->parent_names
[i
] = kstrdup_const(hw
->init
->parent_names
[i
],
2752 if (!core
->parent_names
[i
]) {
2754 goto fail_parent_names_copy
;
2758 /* avoid unnecessary string look-ups of clk_core's possible parents. */
2759 core
->parents
= kcalloc(core
->num_parents
, sizeof(*core
->parents
),
2761 if (!core
->parents
) {
2766 INIT_HLIST_HEAD(&core
->clks
);
2768 hw
->clk
= __clk_create_clk(hw
, NULL
, NULL
);
2769 if (IS_ERR(hw
->clk
)) {
2770 ret
= PTR_ERR(hw
->clk
);
2774 ret
= __clk_core_init(core
);
2778 __clk_free_clk(hw
->clk
);
2782 kfree(core
->parents
);
2783 fail_parent_names_copy
:
2785 kfree_const(core
->parent_names
[i
]);
2786 kfree(core
->parent_names
);
2789 kfree_const(core
->name
);
2793 return ERR_PTR(ret
);
2795 EXPORT_SYMBOL_GPL(clk_register
);
2798 * clk_hw_register - register a clk_hw and return an error code
2799 * @dev: device that is registering this clock
2800 * @hw: link to hardware-specific clock data
2802 * clk_hw_register is the primary interface for populating the clock tree with
2803 * new clock nodes. It returns an integer equal to zero indicating success or
2804 * less than zero indicating failure. Drivers must test for an error code after
2805 * calling clk_hw_register().
2807 int clk_hw_register(struct device
*dev
, struct clk_hw
*hw
)
2809 return PTR_ERR_OR_ZERO(clk_register(dev
, hw
));
2811 EXPORT_SYMBOL_GPL(clk_hw_register
);
2813 /* Free memory allocated for a clock. */
2814 static void __clk_release(struct kref
*ref
)
2816 struct clk_core
*core
= container_of(ref
, struct clk_core
, ref
);
2817 int i
= core
->num_parents
;
2819 lockdep_assert_held(&prepare_lock
);
2821 kfree(core
->parents
);
2823 kfree_const(core
->parent_names
[i
]);
2825 kfree(core
->parent_names
);
2826 kfree_const(core
->name
);
2831 * Empty clk_ops for unregistered clocks. These are used temporarily
2832 * after clk_unregister() was called on a clock and until last clock
2833 * consumer calls clk_put() and the struct clk object is freed.
2835 static int clk_nodrv_prepare_enable(struct clk_hw
*hw
)
2840 static void clk_nodrv_disable_unprepare(struct clk_hw
*hw
)
2845 static int clk_nodrv_set_rate(struct clk_hw
*hw
, unsigned long rate
,
2846 unsigned long parent_rate
)
2851 static int clk_nodrv_set_parent(struct clk_hw
*hw
, u8 index
)
2856 static const struct clk_ops clk_nodrv_ops
= {
2857 .enable
= clk_nodrv_prepare_enable
,
2858 .disable
= clk_nodrv_disable_unprepare
,
2859 .prepare
= clk_nodrv_prepare_enable
,
2860 .unprepare
= clk_nodrv_disable_unprepare
,
2861 .set_rate
= clk_nodrv_set_rate
,
2862 .set_parent
= clk_nodrv_set_parent
,
2866 * clk_unregister - unregister a currently registered clock
2867 * @clk: clock to unregister
2869 void clk_unregister(struct clk
*clk
)
2871 unsigned long flags
;
2873 if (!clk
|| WARN_ON_ONCE(IS_ERR(clk
)))
2876 clk_debug_unregister(clk
->core
);
2880 if (clk
->core
->ops
== &clk_nodrv_ops
) {
2881 pr_err("%s: unregistered clock: %s\n", __func__
,
2886 * Assign empty clock ops for consumers that might still hold
2887 * a reference to this clock.
2889 flags
= clk_enable_lock();
2890 clk
->core
->ops
= &clk_nodrv_ops
;
2891 clk_enable_unlock(flags
);
2893 if (!hlist_empty(&clk
->core
->children
)) {
2894 struct clk_core
*child
;
2895 struct hlist_node
*t
;
2897 /* Reparent all children to the orphan list. */
2898 hlist_for_each_entry_safe(child
, t
, &clk
->core
->children
,
2900 clk_core_set_parent(child
, NULL
);
2903 hlist_del_init(&clk
->core
->child_node
);
2905 if (clk
->core
->prepare_count
)
2906 pr_warn("%s: unregistering prepared clock: %s\n",
2907 __func__
, clk
->core
->name
);
2908 kref_put(&clk
->core
->ref
, __clk_release
);
2910 clk_prepare_unlock();
2912 EXPORT_SYMBOL_GPL(clk_unregister
);
2915 * clk_hw_unregister - unregister a currently registered clk_hw
2916 * @hw: hardware-specific clock data to unregister
2918 void clk_hw_unregister(struct clk_hw
*hw
)
2920 clk_unregister(hw
->clk
);
2922 EXPORT_SYMBOL_GPL(clk_hw_unregister
);
2924 static void devm_clk_release(struct device
*dev
, void *res
)
2926 clk_unregister(*(struct clk
**)res
);
2929 static void devm_clk_hw_release(struct device
*dev
, void *res
)
2931 clk_hw_unregister(*(struct clk_hw
**)res
);
2935 * devm_clk_register - resource managed clk_register()
2936 * @dev: device that is registering this clock
2937 * @hw: link to hardware-specific clock data
2939 * Managed clk_register(). Clocks returned from this function are
2940 * automatically clk_unregister()ed on driver detach. See clk_register() for
2943 struct clk
*devm_clk_register(struct device
*dev
, struct clk_hw
*hw
)
2948 clkp
= devres_alloc(devm_clk_release
, sizeof(*clkp
), GFP_KERNEL
);
2950 return ERR_PTR(-ENOMEM
);
2952 clk
= clk_register(dev
, hw
);
2955 devres_add(dev
, clkp
);
2962 EXPORT_SYMBOL_GPL(devm_clk_register
);
2965 * devm_clk_hw_register - resource managed clk_hw_register()
2966 * @dev: device that is registering this clock
2967 * @hw: link to hardware-specific clock data
2969 * Managed clk_hw_register(). Clocks registered by this function are
2970 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
2971 * for more information.
2973 int devm_clk_hw_register(struct device
*dev
, struct clk_hw
*hw
)
2975 struct clk_hw
**hwp
;
2978 hwp
= devres_alloc(devm_clk_hw_release
, sizeof(*hwp
), GFP_KERNEL
);
2982 ret
= clk_hw_register(dev
, hw
);
2985 devres_add(dev
, hwp
);
2992 EXPORT_SYMBOL_GPL(devm_clk_hw_register
);
2994 static int devm_clk_match(struct device
*dev
, void *res
, void *data
)
2996 struct clk
*c
= res
;
3002 static int devm_clk_hw_match(struct device
*dev
, void *res
, void *data
)
3004 struct clk_hw
*hw
= res
;
3012 * devm_clk_unregister - resource managed clk_unregister()
3013 * @clk: clock to unregister
3015 * Deallocate a clock allocated with devm_clk_register(). Normally
3016 * this function will not need to be called and the resource management
3017 * code will ensure that the resource is freed.
3019 void devm_clk_unregister(struct device
*dev
, struct clk
*clk
)
3021 WARN_ON(devres_release(dev
, devm_clk_release
, devm_clk_match
, clk
));
3023 EXPORT_SYMBOL_GPL(devm_clk_unregister
);
3026 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
3027 * @dev: device that is unregistering the hardware-specific clock data
3028 * @hw: link to hardware-specific clock data
3030 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
3031 * this function will not need to be called and the resource management
3032 * code will ensure that the resource is freed.
3034 void devm_clk_hw_unregister(struct device
*dev
, struct clk_hw
*hw
)
3036 WARN_ON(devres_release(dev
, devm_clk_hw_release
, devm_clk_hw_match
,
3039 EXPORT_SYMBOL_GPL(devm_clk_hw_unregister
);
3044 int __clk_get(struct clk
*clk
)
3046 struct clk_core
*core
= !clk
? NULL
: clk
->core
;
3049 if (!try_module_get(core
->owner
))
3052 kref_get(&core
->ref
);
3057 void __clk_put(struct clk
*clk
)
3059 struct module
*owner
;
3061 if (!clk
|| WARN_ON_ONCE(IS_ERR(clk
)))
3066 hlist_del(&clk
->clks_node
);
3067 if (clk
->min_rate
> clk
->core
->req_rate
||
3068 clk
->max_rate
< clk
->core
->req_rate
)
3069 clk_core_set_rate_nolock(clk
->core
, clk
->core
->req_rate
);
3071 owner
= clk
->core
->owner
;
3072 kref_put(&clk
->core
->ref
, __clk_release
);
3074 clk_prepare_unlock();
3081 /*** clk rate change notifiers ***/
3084 * clk_notifier_register - add a clk rate change notifier
3085 * @clk: struct clk * to watch
3086 * @nb: struct notifier_block * with callback info
3088 * Request notification when clk's rate changes. This uses an SRCU
3089 * notifier because we want it to block and notifier unregistrations are
3090 * uncommon. The callbacks associated with the notifier must not
3091 * re-enter into the clk framework by calling any top-level clk APIs;
3092 * this will cause a nested prepare_lock mutex.
3094 * In all notification cases (pre, post and abort rate change) the original
3095 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
3096 * and the new frequency is passed via struct clk_notifier_data.new_rate.
3098 * clk_notifier_register() must be called from non-atomic context.
3099 * Returns -EINVAL if called with null arguments, -ENOMEM upon
3100 * allocation failure; otherwise, passes along the return value of
3101 * srcu_notifier_chain_register().
3103 int clk_notifier_register(struct clk
*clk
, struct notifier_block
*nb
)
3105 struct clk_notifier
*cn
;
3113 /* search the list of notifiers for this clk */
3114 list_for_each_entry(cn
, &clk_notifier_list
, node
)
3118 /* if clk wasn't in the notifier list, allocate new clk_notifier */
3119 if (cn
->clk
!= clk
) {
3120 cn
= kzalloc(sizeof(*cn
), GFP_KERNEL
);
3125 srcu_init_notifier_head(&cn
->notifier_head
);
3127 list_add(&cn
->node
, &clk_notifier_list
);
3130 ret
= srcu_notifier_chain_register(&cn
->notifier_head
, nb
);
3132 clk
->core
->notifier_count
++;
3135 clk_prepare_unlock();
3139 EXPORT_SYMBOL_GPL(clk_notifier_register
);
3142 * clk_notifier_unregister - remove a clk rate change notifier
3143 * @clk: struct clk *
3144 * @nb: struct notifier_block * with callback info
3146 * Request no further notification for changes to 'clk' and frees memory
3147 * allocated in clk_notifier_register.
3149 * Returns -EINVAL if called with null arguments; otherwise, passes
3150 * along the return value of srcu_notifier_chain_unregister().
3152 int clk_notifier_unregister(struct clk
*clk
, struct notifier_block
*nb
)
3154 struct clk_notifier
*cn
= NULL
;
3162 list_for_each_entry(cn
, &clk_notifier_list
, node
)
3166 if (cn
->clk
== clk
) {
3167 ret
= srcu_notifier_chain_unregister(&cn
->notifier_head
, nb
);
3169 clk
->core
->notifier_count
--;
3171 /* XXX the notifier code should handle this better */
3172 if (!cn
->notifier_head
.head
) {
3173 srcu_cleanup_notifier_head(&cn
->notifier_head
);
3174 list_del(&cn
->node
);
3182 clk_prepare_unlock();
3186 EXPORT_SYMBOL_GPL(clk_notifier_unregister
);
3190 * struct of_clk_provider - Clock provider registration structure
3191 * @link: Entry in global list of clock providers
3192 * @node: Pointer to device tree node of clock provider
3193 * @get: Get clock callback. Returns NULL or a struct clk for the
3194 * given clock specifier
3195 * @data: context pointer to be passed into @get callback
3197 struct of_clk_provider
{
3198 struct list_head link
;
3200 struct device_node
*node
;
3201 struct clk
*(*get
)(struct of_phandle_args
*clkspec
, void *data
);
3202 struct clk_hw
*(*get_hw
)(struct of_phandle_args
*clkspec
, void *data
);
3206 static const struct of_device_id __clk_of_table_sentinel
3207 __used
__section(__clk_of_table_end
);
3209 static LIST_HEAD(of_clk_providers
);
3210 static DEFINE_MUTEX(of_clk_mutex
);
3212 struct clk
*of_clk_src_simple_get(struct of_phandle_args
*clkspec
,
3217 EXPORT_SYMBOL_GPL(of_clk_src_simple_get
);
3219 struct clk_hw
*of_clk_hw_simple_get(struct of_phandle_args
*clkspec
, void *data
)
3223 EXPORT_SYMBOL_GPL(of_clk_hw_simple_get
);
3225 struct clk
*of_clk_src_onecell_get(struct of_phandle_args
*clkspec
, void *data
)
3227 struct clk_onecell_data
*clk_data
= data
;
3228 unsigned int idx
= clkspec
->args
[0];
3230 if (idx
>= clk_data
->clk_num
) {
3231 pr_err("%s: invalid clock index %u\n", __func__
, idx
);
3232 return ERR_PTR(-EINVAL
);
3235 return clk_data
->clks
[idx
];
3237 EXPORT_SYMBOL_GPL(of_clk_src_onecell_get
);
3240 of_clk_hw_onecell_get(struct of_phandle_args
*clkspec
, void *data
)
3242 struct clk_hw_onecell_data
*hw_data
= data
;
3243 unsigned int idx
= clkspec
->args
[0];
3245 if (idx
>= hw_data
->num
) {
3246 pr_err("%s: invalid index %u\n", __func__
, idx
);
3247 return ERR_PTR(-EINVAL
);
3250 return hw_data
->hws
[idx
];
3252 EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get
);
3255 * of_clk_add_provider() - Register a clock provider for a node
3256 * @np: Device node pointer associated with clock provider
3257 * @clk_src_get: callback for decoding clock
3258 * @data: context pointer for @clk_src_get callback.
3260 int of_clk_add_provider(struct device_node
*np
,
3261 struct clk
*(*clk_src_get
)(struct of_phandle_args
*clkspec
,
3265 struct of_clk_provider
*cp
;
3268 cp
= kzalloc(sizeof(*cp
), GFP_KERNEL
);
3272 cp
->node
= of_node_get(np
);
3274 cp
->get
= clk_src_get
;
3276 mutex_lock(&of_clk_mutex
);
3277 list_add(&cp
->link
, &of_clk_providers
);
3278 mutex_unlock(&of_clk_mutex
);
3279 pr_debug("Added clock from %pOF\n", np
);
3281 ret
= of_clk_set_defaults(np
, true);
3283 of_clk_del_provider(np
);
3287 EXPORT_SYMBOL_GPL(of_clk_add_provider
);
3290 * of_clk_add_hw_provider() - Register a clock provider for a node
3291 * @np: Device node pointer associated with clock provider
3292 * @get: callback for decoding clk_hw
3293 * @data: context pointer for @get callback.
3295 int of_clk_add_hw_provider(struct device_node
*np
,
3296 struct clk_hw
*(*get
)(struct of_phandle_args
*clkspec
,
3300 struct of_clk_provider
*cp
;
3303 cp
= kzalloc(sizeof(*cp
), GFP_KERNEL
);
3307 cp
->node
= of_node_get(np
);
3311 mutex_lock(&of_clk_mutex
);
3312 list_add(&cp
->link
, &of_clk_providers
);
3313 mutex_unlock(&of_clk_mutex
);
3314 pr_debug("Added clk_hw provider from %pOF\n", np
);
3316 ret
= of_clk_set_defaults(np
, true);
3318 of_clk_del_provider(np
);
3322 EXPORT_SYMBOL_GPL(of_clk_add_hw_provider
);
3324 static void devm_of_clk_release_provider(struct device
*dev
, void *res
)
3326 of_clk_del_provider(*(struct device_node
**)res
);
3329 int devm_of_clk_add_hw_provider(struct device
*dev
,
3330 struct clk_hw
*(*get
)(struct of_phandle_args
*clkspec
,
3334 struct device_node
**ptr
, *np
;
3337 ptr
= devres_alloc(devm_of_clk_release_provider
, sizeof(*ptr
),
3343 ret
= of_clk_add_hw_provider(np
, get
, data
);
3346 devres_add(dev
, ptr
);
3353 EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider
);
3356 * of_clk_del_provider() - Remove a previously registered clock provider
3357 * @np: Device node pointer associated with clock provider
3359 void of_clk_del_provider(struct device_node
*np
)
3361 struct of_clk_provider
*cp
;
3363 mutex_lock(&of_clk_mutex
);
3364 list_for_each_entry(cp
, &of_clk_providers
, link
) {
3365 if (cp
->node
== np
) {
3366 list_del(&cp
->link
);
3367 of_node_put(cp
->node
);
3372 mutex_unlock(&of_clk_mutex
);
3374 EXPORT_SYMBOL_GPL(of_clk_del_provider
);
3376 static int devm_clk_provider_match(struct device
*dev
, void *res
, void *data
)
3378 struct device_node
**np
= res
;
3380 if (WARN_ON(!np
|| !*np
))
3386 void devm_of_clk_del_provider(struct device
*dev
)
3390 ret
= devres_release(dev
, devm_of_clk_release_provider
,
3391 devm_clk_provider_match
, dev
->of_node
);
3395 EXPORT_SYMBOL(devm_of_clk_del_provider
);
3397 static struct clk_hw
*
3398 __of_clk_get_hw_from_provider(struct of_clk_provider
*provider
,
3399 struct of_phandle_args
*clkspec
)
3403 if (provider
->get_hw
)
3404 return provider
->get_hw(clkspec
, provider
->data
);
3406 clk
= provider
->get(clkspec
, provider
->data
);
3408 return ERR_CAST(clk
);
3409 return __clk_get_hw(clk
);
3412 struct clk
*__of_clk_get_from_provider(struct of_phandle_args
*clkspec
,
3413 const char *dev_id
, const char *con_id
)
3415 struct of_clk_provider
*provider
;
3416 struct clk
*clk
= ERR_PTR(-EPROBE_DEFER
);
3420 return ERR_PTR(-EINVAL
);
3422 /* Check if we have such a provider in our array */
3423 mutex_lock(&of_clk_mutex
);
3424 list_for_each_entry(provider
, &of_clk_providers
, link
) {
3425 if (provider
->node
== clkspec
->np
) {
3426 hw
= __of_clk_get_hw_from_provider(provider
, clkspec
);
3427 clk
= __clk_create_clk(hw
, dev_id
, con_id
);
3431 if (!__clk_get(clk
)) {
3432 __clk_free_clk(clk
);
3433 clk
= ERR_PTR(-ENOENT
);
3439 mutex_unlock(&of_clk_mutex
);
3445 * of_clk_get_from_provider() - Lookup a clock from a clock provider
3446 * @clkspec: pointer to a clock specifier data structure
3448 * This function looks up a struct clk from the registered list of clock
3449 * providers, an input is a clock specifier data structure as returned
3450 * from the of_parse_phandle_with_args() function call.
3452 struct clk
*of_clk_get_from_provider(struct of_phandle_args
*clkspec
)
3454 return __of_clk_get_from_provider(clkspec
, NULL
, __func__
);
3456 EXPORT_SYMBOL_GPL(of_clk_get_from_provider
);
3459 * of_clk_get_parent_count() - Count the number of clocks a device node has
3460 * @np: device node to count
3462 * Returns: The number of clocks that are possible parents of this node
3464 unsigned int of_clk_get_parent_count(struct device_node
*np
)
3468 count
= of_count_phandle_with_args(np
, "clocks", "#clock-cells");
3474 EXPORT_SYMBOL_GPL(of_clk_get_parent_count
);
3476 const char *of_clk_get_parent_name(struct device_node
*np
, int index
)
3478 struct of_phandle_args clkspec
;
3479 struct property
*prop
;
3480 const char *clk_name
;
3487 rc
= of_parse_phandle_with_args(np
, "clocks", "#clock-cells", index
,
3492 index
= clkspec
.args_count
? clkspec
.args
[0] : 0;
3495 /* if there is an indices property, use it to transfer the index
3496 * specified into an array offset for the clock-output-names property.
3498 of_property_for_each_u32(clkspec
.np
, "clock-indices", prop
, vp
, pv
) {
3505 /* We went off the end of 'clock-indices' without finding it */
3509 if (of_property_read_string_index(clkspec
.np
, "clock-output-names",
3513 * Best effort to get the name if the clock has been
3514 * registered with the framework. If the clock isn't
3515 * registered, we return the node name as the name of
3516 * the clock as long as #clock-cells = 0.
3518 clk
= of_clk_get_from_provider(&clkspec
);
3520 if (clkspec
.args_count
== 0)
3521 clk_name
= clkspec
.np
->name
;
3525 clk_name
= __clk_get_name(clk
);
3531 of_node_put(clkspec
.np
);
3534 EXPORT_SYMBOL_GPL(of_clk_get_parent_name
);
3537 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
3539 * @np: Device node pointer associated with clock provider
3540 * @parents: pointer to char array that hold the parents' names
3541 * @size: size of the @parents array
3543 * Return: number of parents for the clock node.
3545 int of_clk_parent_fill(struct device_node
*np
, const char **parents
,
3550 while (i
< size
&& (parents
[i
] = of_clk_get_parent_name(np
, i
)) != NULL
)
3555 EXPORT_SYMBOL_GPL(of_clk_parent_fill
);
3557 struct clock_provider
{
3558 of_clk_init_cb_t clk_init_cb
;
3559 struct device_node
*np
;
3560 struct list_head node
;
3564 * This function looks for a parent clock. If there is one, then it
3565 * checks that the provider for this parent clock was initialized, in
3566 * this case the parent clock will be ready.
3568 static int parent_ready(struct device_node
*np
)
3573 struct clk
*clk
= of_clk_get(np
, i
);
3575 /* this parent is ready we can check the next one */
3582 /* at least one parent is not ready, we exit now */
3583 if (PTR_ERR(clk
) == -EPROBE_DEFER
)
3587 * Here we make assumption that the device tree is
3588 * written correctly. So an error means that there is
3589 * no more parent. As we didn't exit yet, then the
3590 * previous parent are ready. If there is no clock
3591 * parent, no need to wait for them, then we can
3592 * consider their absence as being ready
3599 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
3600 * @np: Device node pointer associated with clock provider
3601 * @index: clock index
3602 * @flags: pointer to clk_core->flags
3604 * Detects if the clock-critical property exists and, if so, sets the
3605 * corresponding CLK_IS_CRITICAL flag.
3607 * Do not use this function. It exists only for legacy Device Tree
3608 * bindings, such as the one-clock-per-node style that are outdated.
3609 * Those bindings typically put all clock data into .dts and the Linux
3610 * driver has no clock data, thus making it impossible to set this flag
3611 * correctly from the driver. Only those drivers may call
3612 * of_clk_detect_critical from their setup functions.
3614 * Return: error code or zero on success
3616 int of_clk_detect_critical(struct device_node
*np
,
3617 int index
, unsigned long *flags
)
3619 struct property
*prop
;
3626 of_property_for_each_u32(np
, "clock-critical", prop
, cur
, idx
)
3628 *flags
|= CLK_IS_CRITICAL
;
3634 * of_clk_init() - Scan and init clock providers from the DT
3635 * @matches: array of compatible values and init functions for providers.
3637 * This function scans the device tree for matching clock providers
3638 * and calls their initialization functions. It also does it by trying
3639 * to follow the dependencies.
3641 void __init
of_clk_init(const struct of_device_id
*matches
)
3643 const struct of_device_id
*match
;
3644 struct device_node
*np
;
3645 struct clock_provider
*clk_provider
, *next
;
3648 LIST_HEAD(clk_provider_list
);
3651 matches
= &__clk_of_table
;
3653 /* First prepare the list of the clocks providers */
3654 for_each_matching_node_and_match(np
, matches
, &match
) {
3655 struct clock_provider
*parent
;
3657 if (!of_device_is_available(np
))
3660 parent
= kzalloc(sizeof(*parent
), GFP_KERNEL
);
3662 list_for_each_entry_safe(clk_provider
, next
,
3663 &clk_provider_list
, node
) {
3664 list_del(&clk_provider
->node
);
3665 of_node_put(clk_provider
->np
);
3666 kfree(clk_provider
);
3672 parent
->clk_init_cb
= match
->data
;
3673 parent
->np
= of_node_get(np
);
3674 list_add_tail(&parent
->node
, &clk_provider_list
);
3677 while (!list_empty(&clk_provider_list
)) {
3678 is_init_done
= false;
3679 list_for_each_entry_safe(clk_provider
, next
,
3680 &clk_provider_list
, node
) {
3681 if (force
|| parent_ready(clk_provider
->np
)) {
3683 /* Don't populate platform devices */
3684 of_node_set_flag(clk_provider
->np
,
3687 clk_provider
->clk_init_cb(clk_provider
->np
);
3688 of_clk_set_defaults(clk_provider
->np
, true);
3690 list_del(&clk_provider
->node
);
3691 of_node_put(clk_provider
->np
);
3692 kfree(clk_provider
);
3693 is_init_done
= true;
3698 * We didn't manage to initialize any of the
3699 * remaining providers during the last loop, so now we
3700 * initialize all the remaining ones unconditionally
3701 * in case the clock parent was not mandatory