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-provider.h>
13 #include <linux/clk/clk-conf.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/spinlock.h>
17 #include <linux/err.h>
18 #include <linux/list.h>
19 #include <linux/slab.h>
21 #include <linux/device.h>
22 #include <linux/init.h>
23 #include <linux/sched.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 long clk_core_get_accuracy(struct clk_core
*clk
);
41 static unsigned long clk_core_get_rate(struct clk_core
*clk
);
42 static int clk_core_get_phase(struct clk_core
*clk
);
43 static bool clk_core_is_prepared(struct clk_core
*clk
);
44 static bool clk_core_is_enabled(struct clk_core
*clk
);
45 static struct clk_core
*clk_core_lookup(const char *name
);
47 /*** private data structures ***/
51 const struct clk_ops
*ops
;
54 struct clk_core
*parent
;
55 const char **parent_names
;
56 struct clk_core
**parents
;
60 unsigned long req_rate
;
61 unsigned long new_rate
;
62 struct clk_core
*new_parent
;
63 struct clk_core
*new_child
;
65 unsigned int enable_count
;
66 unsigned int prepare_count
;
67 unsigned long accuracy
;
69 struct hlist_head children
;
70 struct hlist_node child_node
;
71 struct hlist_node debug_node
;
72 struct hlist_head clks
;
73 unsigned int notifier_count
;
74 #ifdef CONFIG_DEBUG_FS
75 struct dentry
*dentry
;
81 struct clk_core
*core
;
84 unsigned long min_rate
;
85 unsigned long max_rate
;
86 struct hlist_node child_node
;
90 static void clk_prepare_lock(void)
92 if (!mutex_trylock(&prepare_lock
)) {
93 if (prepare_owner
== current
) {
97 mutex_lock(&prepare_lock
);
99 WARN_ON_ONCE(prepare_owner
!= NULL
);
100 WARN_ON_ONCE(prepare_refcnt
!= 0);
101 prepare_owner
= current
;
105 static void clk_prepare_unlock(void)
107 WARN_ON_ONCE(prepare_owner
!= current
);
108 WARN_ON_ONCE(prepare_refcnt
== 0);
110 if (--prepare_refcnt
)
112 prepare_owner
= NULL
;
113 mutex_unlock(&prepare_lock
);
116 static unsigned long clk_enable_lock(void)
120 if (!spin_trylock_irqsave(&enable_lock
, flags
)) {
121 if (enable_owner
== current
) {
125 spin_lock_irqsave(&enable_lock
, flags
);
127 WARN_ON_ONCE(enable_owner
!= NULL
);
128 WARN_ON_ONCE(enable_refcnt
!= 0);
129 enable_owner
= current
;
134 static void clk_enable_unlock(unsigned long flags
)
136 WARN_ON_ONCE(enable_owner
!= current
);
137 WARN_ON_ONCE(enable_refcnt
== 0);
142 spin_unlock_irqrestore(&enable_lock
, flags
);
145 /*** debugfs support ***/
147 #ifdef CONFIG_DEBUG_FS
148 #include <linux/debugfs.h>
150 static struct dentry
*rootdir
;
151 static int inited
= 0;
152 static DEFINE_MUTEX(clk_debug_lock
);
153 static HLIST_HEAD(clk_debug_list
);
155 static struct hlist_head
*all_lists
[] = {
161 static struct hlist_head
*orphan_list
[] = {
166 static void clk_summary_show_one(struct seq_file
*s
, struct clk_core
*c
,
172 seq_printf(s
, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
174 30 - level
* 3, c
->name
,
175 c
->enable_count
, c
->prepare_count
, clk_core_get_rate(c
),
176 clk_core_get_accuracy(c
), clk_core_get_phase(c
));
179 static void clk_summary_show_subtree(struct seq_file
*s
, struct clk_core
*c
,
182 struct clk_core
*child
;
187 clk_summary_show_one(s
, c
, level
);
189 hlist_for_each_entry(child
, &c
->children
, child_node
)
190 clk_summary_show_subtree(s
, child
, level
+ 1);
193 static int clk_summary_show(struct seq_file
*s
, void *data
)
196 struct hlist_head
**lists
= (struct hlist_head
**)s
->private;
198 seq_puts(s
, " clock enable_cnt prepare_cnt rate accuracy phase\n");
199 seq_puts(s
, "----------------------------------------------------------------------------------------\n");
203 for (; *lists
; lists
++)
204 hlist_for_each_entry(c
, *lists
, child_node
)
205 clk_summary_show_subtree(s
, c
, 0);
207 clk_prepare_unlock();
213 static int clk_summary_open(struct inode
*inode
, struct file
*file
)
215 return single_open(file
, clk_summary_show
, inode
->i_private
);
218 static const struct file_operations clk_summary_fops
= {
219 .open
= clk_summary_open
,
222 .release
= single_release
,
225 static void clk_dump_one(struct seq_file
*s
, struct clk_core
*c
, int level
)
230 seq_printf(s
, "\"%s\": { ", c
->name
);
231 seq_printf(s
, "\"enable_count\": %d,", c
->enable_count
);
232 seq_printf(s
, "\"prepare_count\": %d,", c
->prepare_count
);
233 seq_printf(s
, "\"rate\": %lu", clk_core_get_rate(c
));
234 seq_printf(s
, "\"accuracy\": %lu", clk_core_get_accuracy(c
));
235 seq_printf(s
, "\"phase\": %d", clk_core_get_phase(c
));
238 static void clk_dump_subtree(struct seq_file
*s
, struct clk_core
*c
, int level
)
240 struct clk_core
*child
;
245 clk_dump_one(s
, c
, level
);
247 hlist_for_each_entry(child
, &c
->children
, child_node
) {
249 clk_dump_subtree(s
, child
, level
+ 1);
255 static int clk_dump(struct seq_file
*s
, void *data
)
258 bool first_node
= true;
259 struct hlist_head
**lists
= (struct hlist_head
**)s
->private;
265 for (; *lists
; lists
++) {
266 hlist_for_each_entry(c
, *lists
, child_node
) {
270 clk_dump_subtree(s
, c
, 0);
274 clk_prepare_unlock();
281 static int clk_dump_open(struct inode
*inode
, struct file
*file
)
283 return single_open(file
, clk_dump
, inode
->i_private
);
286 static const struct file_operations clk_dump_fops
= {
287 .open
= clk_dump_open
,
290 .release
= single_release
,
293 static int clk_debug_create_one(struct clk_core
*clk
, struct dentry
*pdentry
)
298 if (!clk
|| !pdentry
) {
303 d
= debugfs_create_dir(clk
->name
, pdentry
);
309 d
= debugfs_create_u32("clk_rate", S_IRUGO
, clk
->dentry
,
314 d
= debugfs_create_u32("clk_accuracy", S_IRUGO
, clk
->dentry
,
315 (u32
*)&clk
->accuracy
);
319 d
= debugfs_create_u32("clk_phase", S_IRUGO
, clk
->dentry
,
324 d
= debugfs_create_x32("clk_flags", S_IRUGO
, clk
->dentry
,
329 d
= debugfs_create_u32("clk_prepare_count", S_IRUGO
, clk
->dentry
,
330 (u32
*)&clk
->prepare_count
);
334 d
= debugfs_create_u32("clk_enable_count", S_IRUGO
, clk
->dentry
,
335 (u32
*)&clk
->enable_count
);
339 d
= debugfs_create_u32("clk_notifier_count", S_IRUGO
, clk
->dentry
,
340 (u32
*)&clk
->notifier_count
);
344 if (clk
->ops
->debug_init
) {
345 ret
= clk
->ops
->debug_init(clk
->hw
, clk
->dentry
);
354 debugfs_remove_recursive(clk
->dentry
);
361 * clk_debug_register - add a clk node to the debugfs clk tree
362 * @clk: the clk being added to the debugfs clk tree
364 * Dynamically adds a clk to the debugfs clk tree if debugfs has been
365 * initialized. Otherwise it bails out early since the debugfs clk tree
366 * will be created lazily by clk_debug_init as part of a late_initcall.
368 static int clk_debug_register(struct clk_core
*clk
)
372 mutex_lock(&clk_debug_lock
);
373 hlist_add_head(&clk
->debug_node
, &clk_debug_list
);
378 ret
= clk_debug_create_one(clk
, rootdir
);
380 mutex_unlock(&clk_debug_lock
);
386 * clk_debug_unregister - remove a clk node from the debugfs clk tree
387 * @clk: the clk being removed from the debugfs clk tree
389 * Dynamically removes a clk and all it's children clk nodes from the
390 * debugfs clk tree if clk->dentry points to debugfs created by
391 * clk_debug_register in __clk_init.
393 static void clk_debug_unregister(struct clk_core
*clk
)
395 mutex_lock(&clk_debug_lock
);
396 hlist_del_init(&clk
->debug_node
);
397 debugfs_remove_recursive(clk
->dentry
);
399 mutex_unlock(&clk_debug_lock
);
402 struct dentry
*clk_debugfs_add_file(struct clk_hw
*hw
, char *name
, umode_t mode
,
403 void *data
, const struct file_operations
*fops
)
405 struct dentry
*d
= NULL
;
407 if (hw
->core
->dentry
)
408 d
= debugfs_create_file(name
, mode
, hw
->core
->dentry
, data
,
413 EXPORT_SYMBOL_GPL(clk_debugfs_add_file
);
416 * clk_debug_init - lazily create the debugfs clk tree visualization
418 * clks are often initialized very early during boot before memory can
419 * be dynamically allocated and well before debugfs is setup.
420 * clk_debug_init walks the clk tree hierarchy while holding
421 * prepare_lock and creates the topology as part of a late_initcall,
422 * thus insuring that clks initialized very early will still be
423 * represented in the debugfs clk tree. This function should only be
424 * called once at boot-time, and all other clks added dynamically will
425 * be done so with clk_debug_register.
427 static int __init
clk_debug_init(void)
429 struct clk_core
*clk
;
432 rootdir
= debugfs_create_dir("clk", NULL
);
437 d
= debugfs_create_file("clk_summary", S_IRUGO
, rootdir
, &all_lists
,
442 d
= debugfs_create_file("clk_dump", S_IRUGO
, rootdir
, &all_lists
,
447 d
= debugfs_create_file("clk_orphan_summary", S_IRUGO
, rootdir
,
448 &orphan_list
, &clk_summary_fops
);
452 d
= debugfs_create_file("clk_orphan_dump", S_IRUGO
, rootdir
,
453 &orphan_list
, &clk_dump_fops
);
457 mutex_lock(&clk_debug_lock
);
458 hlist_for_each_entry(clk
, &clk_debug_list
, debug_node
)
459 clk_debug_create_one(clk
, rootdir
);
462 mutex_unlock(&clk_debug_lock
);
466 late_initcall(clk_debug_init
);
468 static inline int clk_debug_register(struct clk_core
*clk
) { return 0; }
469 static inline void clk_debug_reparent(struct clk_core
*clk
,
470 struct clk_core
*new_parent
)
473 static inline void clk_debug_unregister(struct clk_core
*clk
)
478 /* caller must hold prepare_lock */
479 static void clk_unprepare_unused_subtree(struct clk_core
*clk
)
481 struct clk_core
*child
;
483 hlist_for_each_entry(child
, &clk
->children
, child_node
)
484 clk_unprepare_unused_subtree(child
);
486 if (clk
->prepare_count
)
489 if (clk
->flags
& CLK_IGNORE_UNUSED
)
492 if (clk_core_is_prepared(clk
)) {
493 if (clk
->ops
->unprepare_unused
)
494 clk
->ops
->unprepare_unused(clk
->hw
);
495 else if (clk
->ops
->unprepare
)
496 clk
->ops
->unprepare(clk
->hw
);
500 /* caller must hold prepare_lock */
501 static void clk_disable_unused_subtree(struct clk_core
*clk
)
503 struct clk_core
*child
;
506 hlist_for_each_entry(child
, &clk
->children
, child_node
)
507 clk_disable_unused_subtree(child
);
509 flags
= clk_enable_lock();
511 if (clk
->enable_count
)
514 if (clk
->flags
& CLK_IGNORE_UNUSED
)
518 * some gate clocks have special needs during the disable-unused
519 * sequence. call .disable_unused if available, otherwise fall
522 if (clk_core_is_enabled(clk
)) {
523 if (clk
->ops
->disable_unused
)
524 clk
->ops
->disable_unused(clk
->hw
);
525 else if (clk
->ops
->disable
)
526 clk
->ops
->disable(clk
->hw
);
530 clk_enable_unlock(flags
);
533 static bool clk_ignore_unused
;
534 static int __init
clk_ignore_unused_setup(char *__unused
)
536 clk_ignore_unused
= true;
539 __setup("clk_ignore_unused", clk_ignore_unused_setup
);
541 static int clk_disable_unused(void)
543 struct clk_core
*clk
;
545 if (clk_ignore_unused
) {
546 pr_warn("clk: Not disabling unused clocks\n");
552 hlist_for_each_entry(clk
, &clk_root_list
, child_node
)
553 clk_disable_unused_subtree(clk
);
555 hlist_for_each_entry(clk
, &clk_orphan_list
, child_node
)
556 clk_disable_unused_subtree(clk
);
558 hlist_for_each_entry(clk
, &clk_root_list
, child_node
)
559 clk_unprepare_unused_subtree(clk
);
561 hlist_for_each_entry(clk
, &clk_orphan_list
, child_node
)
562 clk_unprepare_unused_subtree(clk
);
564 clk_prepare_unlock();
568 late_initcall_sync(clk_disable_unused
);
570 /*** helper functions ***/
572 const char *__clk_get_name(struct clk
*clk
)
574 return !clk
? NULL
: clk
->core
->name
;
576 EXPORT_SYMBOL_GPL(__clk_get_name
);
578 struct clk_hw
*__clk_get_hw(struct clk
*clk
)
580 return !clk
? NULL
: clk
->core
->hw
;
582 EXPORT_SYMBOL_GPL(__clk_get_hw
);
584 u8
__clk_get_num_parents(struct clk
*clk
)
586 return !clk
? 0 : clk
->core
->num_parents
;
588 EXPORT_SYMBOL_GPL(__clk_get_num_parents
);
590 struct clk
*__clk_get_parent(struct clk
*clk
)
595 /* TODO: Create a per-user clk and change callers to call clk_put */
596 return !clk
->core
->parent
? NULL
: clk
->core
->parent
->hw
->clk
;
598 EXPORT_SYMBOL_GPL(__clk_get_parent
);
600 static struct clk_core
*clk_core_get_parent_by_index(struct clk_core
*clk
,
603 if (!clk
|| index
>= clk
->num_parents
)
605 else if (!clk
->parents
)
606 return clk_core_lookup(clk
->parent_names
[index
]);
607 else if (!clk
->parents
[index
])
608 return clk
->parents
[index
] =
609 clk_core_lookup(clk
->parent_names
[index
]);
611 return clk
->parents
[index
];
614 struct clk
*clk_get_parent_by_index(struct clk
*clk
, u8 index
)
616 struct clk_core
*parent
;
621 parent
= clk_core_get_parent_by_index(clk
->core
, index
);
623 return !parent
? NULL
: parent
->hw
->clk
;
625 EXPORT_SYMBOL_GPL(clk_get_parent_by_index
);
627 unsigned int __clk_get_enable_count(struct clk
*clk
)
629 return !clk
? 0 : clk
->core
->enable_count
;
632 static unsigned long clk_core_get_rate_nolock(struct clk_core
*clk
)
643 if (clk
->flags
& CLK_IS_ROOT
)
653 unsigned long __clk_get_rate(struct clk
*clk
)
658 return clk_core_get_rate_nolock(clk
->core
);
660 EXPORT_SYMBOL_GPL(__clk_get_rate
);
662 static unsigned long __clk_get_accuracy(struct clk_core
*clk
)
667 return clk
->accuracy
;
670 unsigned long __clk_get_flags(struct clk
*clk
)
672 return !clk
? 0 : clk
->core
->flags
;
674 EXPORT_SYMBOL_GPL(__clk_get_flags
);
676 static bool clk_core_is_prepared(struct clk_core
*clk
)
684 * .is_prepared is optional for clocks that can prepare
685 * fall back to software usage counter if it is missing
687 if (!clk
->ops
->is_prepared
) {
688 ret
= clk
->prepare_count
? 1 : 0;
692 ret
= clk
->ops
->is_prepared(clk
->hw
);
697 bool __clk_is_prepared(struct clk
*clk
)
702 return clk_core_is_prepared(clk
->core
);
705 static bool clk_core_is_enabled(struct clk_core
*clk
)
713 * .is_enabled is only mandatory for clocks that gate
714 * fall back to software usage counter if .is_enabled is missing
716 if (!clk
->ops
->is_enabled
) {
717 ret
= clk
->enable_count
? 1 : 0;
721 ret
= clk
->ops
->is_enabled(clk
->hw
);
726 bool __clk_is_enabled(struct clk
*clk
)
731 return clk_core_is_enabled(clk
->core
);
733 EXPORT_SYMBOL_GPL(__clk_is_enabled
);
735 static struct clk_core
*__clk_lookup_subtree(const char *name
,
736 struct clk_core
*clk
)
738 struct clk_core
*child
;
739 struct clk_core
*ret
;
741 if (!strcmp(clk
->name
, name
))
744 hlist_for_each_entry(child
, &clk
->children
, child_node
) {
745 ret
= __clk_lookup_subtree(name
, child
);
753 static struct clk_core
*clk_core_lookup(const char *name
)
755 struct clk_core
*root_clk
;
756 struct clk_core
*ret
;
761 /* search the 'proper' clk tree first */
762 hlist_for_each_entry(root_clk
, &clk_root_list
, child_node
) {
763 ret
= __clk_lookup_subtree(name
, root_clk
);
768 /* if not found, then search the orphan tree */
769 hlist_for_each_entry(root_clk
, &clk_orphan_list
, child_node
) {
770 ret
= __clk_lookup_subtree(name
, root_clk
);
778 static bool mux_is_better_rate(unsigned long rate
, unsigned long now
,
779 unsigned long best
, unsigned long flags
)
781 if (flags
& CLK_MUX_ROUND_CLOSEST
)
782 return abs(now
- rate
) < abs(best
- rate
);
784 return now
<= rate
&& now
> best
;
788 clk_mux_determine_rate_flags(struct clk_hw
*hw
, unsigned long rate
,
789 unsigned long min_rate
,
790 unsigned long max_rate
,
791 unsigned long *best_parent_rate
,
792 struct clk_hw
**best_parent_p
,
795 struct clk_core
*core
= hw
->core
, *parent
, *best_parent
= NULL
;
797 unsigned long parent_rate
, best
= 0;
799 /* if NO_REPARENT flag set, pass through to current parent */
800 if (core
->flags
& CLK_SET_RATE_NO_REPARENT
) {
801 parent
= core
->parent
;
802 if (core
->flags
& CLK_SET_RATE_PARENT
)
803 best
= __clk_determine_rate(parent
? parent
->hw
: NULL
,
804 rate
, min_rate
, max_rate
);
806 best
= clk_core_get_rate_nolock(parent
);
808 best
= clk_core_get_rate_nolock(core
);
812 /* find the parent that can provide the fastest rate <= rate */
813 num_parents
= core
->num_parents
;
814 for (i
= 0; i
< num_parents
; i
++) {
815 parent
= clk_core_get_parent_by_index(core
, i
);
818 if (core
->flags
& CLK_SET_RATE_PARENT
)
819 parent_rate
= __clk_determine_rate(parent
->hw
, rate
,
823 parent_rate
= clk_core_get_rate_nolock(parent
);
824 if (mux_is_better_rate(rate
, parent_rate
, best
, flags
)) {
825 best_parent
= parent
;
832 *best_parent_p
= best_parent
->hw
;
833 *best_parent_rate
= best
;
838 struct clk
*__clk_lookup(const char *name
)
840 struct clk_core
*core
= clk_core_lookup(name
);
842 return !core
? NULL
: core
->hw
->clk
;
845 static void clk_core_get_boundaries(struct clk_core
*clk
,
846 unsigned long *min_rate
,
847 unsigned long *max_rate
)
849 struct clk
*clk_user
;
852 *max_rate
= ULONG_MAX
;
854 hlist_for_each_entry(clk_user
, &clk
->clks
, child_node
)
855 *min_rate
= max(*min_rate
, clk_user
->min_rate
);
857 hlist_for_each_entry(clk_user
, &clk
->clks
, child_node
)
858 *max_rate
= min(*max_rate
, clk_user
->max_rate
);
862 * Helper for finding best parent to provide a given frequency. This can be used
863 * directly as a determine_rate callback (e.g. for a mux), or from a more
864 * complex clock that may combine a mux with other operations.
866 long __clk_mux_determine_rate(struct clk_hw
*hw
, unsigned long rate
,
867 unsigned long min_rate
,
868 unsigned long max_rate
,
869 unsigned long *best_parent_rate
,
870 struct clk_hw
**best_parent_p
)
872 return clk_mux_determine_rate_flags(hw
, rate
, min_rate
, max_rate
,
876 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate
);
878 long __clk_mux_determine_rate_closest(struct clk_hw
*hw
, unsigned long rate
,
879 unsigned long min_rate
,
880 unsigned long max_rate
,
881 unsigned long *best_parent_rate
,
882 struct clk_hw
**best_parent_p
)
884 return clk_mux_determine_rate_flags(hw
, rate
, min_rate
, max_rate
,
887 CLK_MUX_ROUND_CLOSEST
);
889 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest
);
893 static void clk_core_unprepare(struct clk_core
*clk
)
898 if (WARN_ON(clk
->prepare_count
== 0))
901 if (--clk
->prepare_count
> 0)
904 WARN_ON(clk
->enable_count
> 0);
906 if (clk
->ops
->unprepare
)
907 clk
->ops
->unprepare(clk
->hw
);
909 clk_core_unprepare(clk
->parent
);
913 * clk_unprepare - undo preparation of a clock source
914 * @clk: the clk being unprepared
916 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
917 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
918 * if the operation may sleep. One example is a clk which is accessed over
919 * I2c. In the complex case a clk gate operation may require a fast and a slow
920 * part. It is this reason that clk_unprepare and clk_disable are not mutually
921 * exclusive. In fact clk_disable must be called before clk_unprepare.
923 void clk_unprepare(struct clk
*clk
)
925 if (IS_ERR_OR_NULL(clk
))
929 clk_core_unprepare(clk
->core
);
930 clk_prepare_unlock();
932 EXPORT_SYMBOL_GPL(clk_unprepare
);
934 static int clk_core_prepare(struct clk_core
*clk
)
941 if (clk
->prepare_count
== 0) {
942 ret
= clk_core_prepare(clk
->parent
);
946 if (clk
->ops
->prepare
) {
947 ret
= clk
->ops
->prepare(clk
->hw
);
949 clk_core_unprepare(clk
->parent
);
955 clk
->prepare_count
++;
961 * clk_prepare - prepare a clock source
962 * @clk: the clk being prepared
964 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
965 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
966 * operation may sleep. One example is a clk which is accessed over I2c. In
967 * the complex case a clk ungate operation may require a fast and a slow part.
968 * It is this reason that clk_prepare and clk_enable are not mutually
969 * exclusive. In fact clk_prepare must be called before clk_enable.
970 * Returns 0 on success, -EERROR otherwise.
972 int clk_prepare(struct clk
*clk
)
980 ret
= clk_core_prepare(clk
->core
);
981 clk_prepare_unlock();
985 EXPORT_SYMBOL_GPL(clk_prepare
);
987 static void clk_core_disable(struct clk_core
*clk
)
992 if (WARN_ON(clk
->enable_count
== 0))
995 if (--clk
->enable_count
> 0)
998 if (clk
->ops
->disable
)
999 clk
->ops
->disable(clk
->hw
);
1001 clk_core_disable(clk
->parent
);
1004 static void __clk_disable(struct clk
*clk
)
1009 clk_core_disable(clk
->core
);
1013 * clk_disable - gate a clock
1014 * @clk: the clk being gated
1016 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
1017 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
1018 * clk if the operation is fast and will never sleep. One example is a
1019 * SoC-internal clk which is controlled via simple register writes. In the
1020 * complex case a clk gate operation may require a fast and a slow part. It is
1021 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
1022 * In fact clk_disable must be called before clk_unprepare.
1024 void clk_disable(struct clk
*clk
)
1026 unsigned long flags
;
1028 if (IS_ERR_OR_NULL(clk
))
1031 flags
= clk_enable_lock();
1033 clk_enable_unlock(flags
);
1035 EXPORT_SYMBOL_GPL(clk_disable
);
1037 static int clk_core_enable(struct clk_core
*clk
)
1044 if (WARN_ON(clk
->prepare_count
== 0))
1047 if (clk
->enable_count
== 0) {
1048 ret
= clk_core_enable(clk
->parent
);
1053 if (clk
->ops
->enable
) {
1054 ret
= clk
->ops
->enable(clk
->hw
);
1056 clk_core_disable(clk
->parent
);
1062 clk
->enable_count
++;
1066 static int __clk_enable(struct clk
*clk
)
1071 return clk_core_enable(clk
->core
);
1075 * clk_enable - ungate a clock
1076 * @clk: the clk being ungated
1078 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
1079 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
1080 * if the operation will never sleep. One example is a SoC-internal clk which
1081 * is controlled via simple register writes. In the complex case a clk ungate
1082 * operation may require a fast and a slow part. It is this reason that
1083 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
1084 * must be called before clk_enable. Returns 0 on success, -EERROR
1087 int clk_enable(struct clk
*clk
)
1089 unsigned long flags
;
1092 flags
= clk_enable_lock();
1093 ret
= __clk_enable(clk
);
1094 clk_enable_unlock(flags
);
1098 EXPORT_SYMBOL_GPL(clk_enable
);
1100 static unsigned long clk_core_round_rate_nolock(struct clk_core
*clk
,
1102 unsigned long min_rate
,
1103 unsigned long max_rate
)
1105 unsigned long parent_rate
= 0;
1106 struct clk_core
*parent
;
1107 struct clk_hw
*parent_hw
;
1112 parent
= clk
->parent
;
1114 parent_rate
= parent
->rate
;
1116 if (clk
->ops
->determine_rate
) {
1117 parent_hw
= parent
? parent
->hw
: NULL
;
1118 return clk
->ops
->determine_rate(clk
->hw
, rate
,
1120 &parent_rate
, &parent_hw
);
1121 } else if (clk
->ops
->round_rate
)
1122 return clk
->ops
->round_rate(clk
->hw
, rate
, &parent_rate
);
1123 else if (clk
->flags
& CLK_SET_RATE_PARENT
)
1124 return clk_core_round_rate_nolock(clk
->parent
, rate
, min_rate
,
1131 * __clk_determine_rate - get the closest rate actually supported by a clock
1132 * @hw: determine the rate of this clock
1133 * @rate: target rate
1134 * @min_rate: returned rate must be greater than this rate
1135 * @max_rate: returned rate must be less than this rate
1137 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate and
1140 unsigned long __clk_determine_rate(struct clk_hw
*hw
,
1142 unsigned long min_rate
,
1143 unsigned long max_rate
)
1148 return clk_core_round_rate_nolock(hw
->core
, rate
, min_rate
, max_rate
);
1150 EXPORT_SYMBOL_GPL(__clk_determine_rate
);
1153 * __clk_round_rate - round the given rate for a clk
1154 * @clk: round the rate of this clock
1155 * @rate: the rate which is to be rounded
1157 * Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
1159 unsigned long __clk_round_rate(struct clk
*clk
, unsigned long rate
)
1161 unsigned long min_rate
;
1162 unsigned long max_rate
;
1167 clk_core_get_boundaries(clk
->core
, &min_rate
, &max_rate
);
1169 return clk_core_round_rate_nolock(clk
->core
, rate
, min_rate
, max_rate
);
1171 EXPORT_SYMBOL_GPL(__clk_round_rate
);
1174 * clk_round_rate - round the given rate for a clk
1175 * @clk: the clk for which we are rounding a rate
1176 * @rate: the rate which is to be rounded
1178 * Takes in a rate as input and rounds it to a rate that the clk can actually
1179 * use which is then returned. If clk doesn't support round_rate operation
1180 * then the parent rate is returned.
1182 long clk_round_rate(struct clk
*clk
, unsigned long rate
)
1190 ret
= __clk_round_rate(clk
, rate
);
1191 clk_prepare_unlock();
1195 EXPORT_SYMBOL_GPL(clk_round_rate
);
1198 * __clk_notify - call clk notifier chain
1199 * @clk: struct clk * that is changing rate
1200 * @msg: clk notifier type (see include/linux/clk.h)
1201 * @old_rate: old clk rate
1202 * @new_rate: new clk rate
1204 * Triggers a notifier call chain on the clk rate-change notification
1205 * for 'clk'. Passes a pointer to the struct clk and the previous
1206 * and current rates to the notifier callback. Intended to be called by
1207 * internal clock code only. Returns NOTIFY_DONE from the last driver
1208 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1209 * a driver returns that.
1211 static int __clk_notify(struct clk_core
*clk
, unsigned long msg
,
1212 unsigned long old_rate
, unsigned long new_rate
)
1214 struct clk_notifier
*cn
;
1215 struct clk_notifier_data cnd
;
1216 int ret
= NOTIFY_DONE
;
1218 cnd
.old_rate
= old_rate
;
1219 cnd
.new_rate
= new_rate
;
1221 list_for_each_entry(cn
, &clk_notifier_list
, node
) {
1222 if (cn
->clk
->core
== clk
) {
1224 ret
= srcu_notifier_call_chain(&cn
->notifier_head
, msg
,
1233 * __clk_recalc_accuracies
1234 * @clk: first clk in the subtree
1236 * Walks the subtree of clks starting with clk and recalculates accuracies as
1237 * it goes. Note that if a clk does not implement the .recalc_accuracy
1238 * callback then it is assumed that the clock will take on the accuracy of it's
1241 * Caller must hold prepare_lock.
1243 static void __clk_recalc_accuracies(struct clk_core
*clk
)
1245 unsigned long parent_accuracy
= 0;
1246 struct clk_core
*child
;
1249 parent_accuracy
= clk
->parent
->accuracy
;
1251 if (clk
->ops
->recalc_accuracy
)
1252 clk
->accuracy
= clk
->ops
->recalc_accuracy(clk
->hw
,
1255 clk
->accuracy
= parent_accuracy
;
1257 hlist_for_each_entry(child
, &clk
->children
, child_node
)
1258 __clk_recalc_accuracies(child
);
1261 static long clk_core_get_accuracy(struct clk_core
*clk
)
1263 unsigned long accuracy
;
1266 if (clk
&& (clk
->flags
& CLK_GET_ACCURACY_NOCACHE
))
1267 __clk_recalc_accuracies(clk
);
1269 accuracy
= __clk_get_accuracy(clk
);
1270 clk_prepare_unlock();
1276 * clk_get_accuracy - return the accuracy of clk
1277 * @clk: the clk whose accuracy is being returned
1279 * Simply returns the cached accuracy of the clk, unless
1280 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1282 * If clk is NULL then returns 0.
1284 long clk_get_accuracy(struct clk
*clk
)
1289 return clk_core_get_accuracy(clk
->core
);
1291 EXPORT_SYMBOL_GPL(clk_get_accuracy
);
1293 static unsigned long clk_recalc(struct clk_core
*clk
,
1294 unsigned long parent_rate
)
1296 if (clk
->ops
->recalc_rate
)
1297 return clk
->ops
->recalc_rate(clk
->hw
, parent_rate
);
1302 * __clk_recalc_rates
1303 * @clk: first clk in the subtree
1304 * @msg: notification type (see include/linux/clk.h)
1306 * Walks the subtree of clks starting with clk and recalculates rates as it
1307 * goes. Note that if a clk does not implement the .recalc_rate callback then
1308 * it is assumed that the clock will take on the rate of its parent.
1310 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1313 * Caller must hold prepare_lock.
1315 static void __clk_recalc_rates(struct clk_core
*clk
, unsigned long msg
)
1317 unsigned long old_rate
;
1318 unsigned long parent_rate
= 0;
1319 struct clk_core
*child
;
1321 old_rate
= clk
->rate
;
1324 parent_rate
= clk
->parent
->rate
;
1326 clk
->rate
= clk_recalc(clk
, parent_rate
);
1329 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1330 * & ABORT_RATE_CHANGE notifiers
1332 if (clk
->notifier_count
&& msg
)
1333 __clk_notify(clk
, msg
, old_rate
, clk
->rate
);
1335 hlist_for_each_entry(child
, &clk
->children
, child_node
)
1336 __clk_recalc_rates(child
, msg
);
1339 static unsigned long clk_core_get_rate(struct clk_core
*clk
)
1345 if (clk
&& (clk
->flags
& CLK_GET_RATE_NOCACHE
))
1346 __clk_recalc_rates(clk
, 0);
1348 rate
= clk_core_get_rate_nolock(clk
);
1349 clk_prepare_unlock();
1353 EXPORT_SYMBOL_GPL(clk_core_get_rate
);
1356 * clk_get_rate - return the rate of clk
1357 * @clk: the clk whose rate is being returned
1359 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1360 * is set, which means a recalc_rate will be issued.
1361 * If clk is NULL then returns 0.
1363 unsigned long clk_get_rate(struct clk
*clk
)
1368 return clk_core_get_rate(clk
->core
);
1370 EXPORT_SYMBOL_GPL(clk_get_rate
);
1372 static int clk_fetch_parent_index(struct clk_core
*clk
,
1373 struct clk_core
*parent
)
1377 if (!clk
->parents
) {
1378 clk
->parents
= kcalloc(clk
->num_parents
,
1379 sizeof(struct clk
*), GFP_KERNEL
);
1385 * find index of new parent clock using cached parent ptrs,
1386 * or if not yet cached, use string name comparison and cache
1387 * them now to avoid future calls to clk_core_lookup.
1389 for (i
= 0; i
< clk
->num_parents
; i
++) {
1390 if (clk
->parents
[i
] == parent
)
1393 if (clk
->parents
[i
])
1396 if (!strcmp(clk
->parent_names
[i
], parent
->name
)) {
1397 clk
->parents
[i
] = clk_core_lookup(parent
->name
);
1405 static void clk_reparent(struct clk_core
*clk
, struct clk_core
*new_parent
)
1407 hlist_del(&clk
->child_node
);
1410 /* avoid duplicate POST_RATE_CHANGE notifications */
1411 if (new_parent
->new_child
== clk
)
1412 new_parent
->new_child
= NULL
;
1414 hlist_add_head(&clk
->child_node
, &new_parent
->children
);
1416 hlist_add_head(&clk
->child_node
, &clk_orphan_list
);
1419 clk
->parent
= new_parent
;
1422 static struct clk_core
*__clk_set_parent_before(struct clk_core
*clk
,
1423 struct clk_core
*parent
)
1425 unsigned long flags
;
1426 struct clk_core
*old_parent
= clk
->parent
;
1429 * Migrate prepare state between parents and prevent race with
1432 * If the clock is not prepared, then a race with
1433 * clk_enable/disable() is impossible since we already have the
1434 * prepare lock (future calls to clk_enable() need to be preceded by
1437 * If the clock is prepared, migrate the prepared state to the new
1438 * parent and also protect against a race with clk_enable() by
1439 * forcing the clock and the new parent on. This ensures that all
1440 * future calls to clk_enable() are practically NOPs with respect to
1441 * hardware and software states.
1443 * See also: Comment for clk_set_parent() below.
1445 if (clk
->prepare_count
) {
1446 clk_core_prepare(parent
);
1447 clk_core_enable(parent
);
1448 clk_core_enable(clk
);
1451 /* update the clk tree topology */
1452 flags
= clk_enable_lock();
1453 clk_reparent(clk
, parent
);
1454 clk_enable_unlock(flags
);
1459 static void __clk_set_parent_after(struct clk_core
*core
,
1460 struct clk_core
*parent
,
1461 struct clk_core
*old_parent
)
1464 * Finish the migration of prepare state and undo the changes done
1465 * for preventing a race with clk_enable().
1467 if (core
->prepare_count
) {
1468 clk_core_disable(core
);
1469 clk_core_disable(old_parent
);
1470 clk_core_unprepare(old_parent
);
1474 static int __clk_set_parent(struct clk_core
*clk
, struct clk_core
*parent
,
1477 unsigned long flags
;
1479 struct clk_core
*old_parent
;
1481 old_parent
= __clk_set_parent_before(clk
, parent
);
1483 /* change clock input source */
1484 if (parent
&& clk
->ops
->set_parent
)
1485 ret
= clk
->ops
->set_parent(clk
->hw
, p_index
);
1488 flags
= clk_enable_lock();
1489 clk_reparent(clk
, old_parent
);
1490 clk_enable_unlock(flags
);
1492 if (clk
->prepare_count
) {
1493 clk_core_disable(clk
);
1494 clk_core_disable(parent
);
1495 clk_core_unprepare(parent
);
1500 __clk_set_parent_after(clk
, parent
, old_parent
);
1506 * __clk_speculate_rates
1507 * @clk: first clk in the subtree
1508 * @parent_rate: the "future" rate of clk's parent
1510 * Walks the subtree of clks starting with clk, speculating rates as it
1511 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1513 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1514 * pre-rate change notifications and returns early if no clks in the
1515 * subtree have subscribed to the notifications. Note that if a clk does not
1516 * implement the .recalc_rate callback then it is assumed that the clock will
1517 * take on the rate of its parent.
1519 * Caller must hold prepare_lock.
1521 static int __clk_speculate_rates(struct clk_core
*clk
,
1522 unsigned long parent_rate
)
1524 struct clk_core
*child
;
1525 unsigned long new_rate
;
1526 int ret
= NOTIFY_DONE
;
1528 new_rate
= clk_recalc(clk
, parent_rate
);
1530 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
1531 if (clk
->notifier_count
)
1532 ret
= __clk_notify(clk
, PRE_RATE_CHANGE
, clk
->rate
, new_rate
);
1534 if (ret
& NOTIFY_STOP_MASK
) {
1535 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
1536 __func__
, clk
->name
, ret
);
1540 hlist_for_each_entry(child
, &clk
->children
, child_node
) {
1541 ret
= __clk_speculate_rates(child
, new_rate
);
1542 if (ret
& NOTIFY_STOP_MASK
)
1550 static void clk_calc_subtree(struct clk_core
*clk
, unsigned long new_rate
,
1551 struct clk_core
*new_parent
, u8 p_index
)
1553 struct clk_core
*child
;
1555 clk
->new_rate
= new_rate
;
1556 clk
->new_parent
= new_parent
;
1557 clk
->new_parent_index
= p_index
;
1558 /* include clk in new parent's PRE_RATE_CHANGE notifications */
1559 clk
->new_child
= NULL
;
1560 if (new_parent
&& new_parent
!= clk
->parent
)
1561 new_parent
->new_child
= clk
;
1563 hlist_for_each_entry(child
, &clk
->children
, child_node
) {
1564 child
->new_rate
= clk_recalc(child
, new_rate
);
1565 clk_calc_subtree(child
, child
->new_rate
, NULL
, 0);
1570 * calculate the new rates returning the topmost clock that has to be
1573 static struct clk_core
*clk_calc_new_rates(struct clk_core
*clk
,
1576 struct clk_core
*top
= clk
;
1577 struct clk_core
*old_parent
, *parent
;
1578 struct clk_hw
*parent_hw
;
1579 unsigned long best_parent_rate
= 0;
1580 unsigned long new_rate
;
1581 unsigned long min_rate
;
1582 unsigned long max_rate
;
1586 if (IS_ERR_OR_NULL(clk
))
1589 /* save parent rate, if it exists */
1590 parent
= old_parent
= clk
->parent
;
1592 best_parent_rate
= parent
->rate
;
1594 clk_core_get_boundaries(clk
, &min_rate
, &max_rate
);
1596 /* find the closest rate and parent clk/rate */
1597 if (clk
->ops
->determine_rate
) {
1598 parent_hw
= parent
? parent
->hw
: NULL
;
1599 new_rate
= clk
->ops
->determine_rate(clk
->hw
, rate
,
1604 parent
= parent_hw
? parent_hw
->core
: NULL
;
1605 } else if (clk
->ops
->round_rate
) {
1606 new_rate
= clk
->ops
->round_rate(clk
->hw
, rate
,
1608 if (new_rate
< min_rate
|| new_rate
> max_rate
)
1610 } else if (!parent
|| !(clk
->flags
& CLK_SET_RATE_PARENT
)) {
1611 /* pass-through clock without adjustable parent */
1612 clk
->new_rate
= clk
->rate
;
1615 /* pass-through clock with adjustable parent */
1616 top
= clk_calc_new_rates(parent
, rate
);
1617 new_rate
= parent
->new_rate
;
1621 /* some clocks must be gated to change parent */
1622 if (parent
!= old_parent
&&
1623 (clk
->flags
& CLK_SET_PARENT_GATE
) && clk
->prepare_count
) {
1624 pr_debug("%s: %s not gated but wants to reparent\n",
1625 __func__
, clk
->name
);
1629 /* try finding the new parent index */
1630 if (parent
&& clk
->num_parents
> 1) {
1631 p_index
= clk_fetch_parent_index(clk
, parent
);
1633 pr_debug("%s: clk %s can not be parent of clk %s\n",
1634 __func__
, parent
->name
, clk
->name
);
1639 if ((clk
->flags
& CLK_SET_RATE_PARENT
) && parent
&&
1640 best_parent_rate
!= parent
->rate
)
1641 top
= clk_calc_new_rates(parent
, best_parent_rate
);
1644 clk_calc_subtree(clk
, new_rate
, parent
, p_index
);
1650 * Notify about rate changes in a subtree. Always walk down the whole tree
1651 * so that in case of an error we can walk down the whole tree again and
1654 static struct clk_core
*clk_propagate_rate_change(struct clk_core
*clk
,
1655 unsigned long event
)
1657 struct clk_core
*child
, *tmp_clk
, *fail_clk
= NULL
;
1658 int ret
= NOTIFY_DONE
;
1660 if (clk
->rate
== clk
->new_rate
)
1663 if (clk
->notifier_count
) {
1664 ret
= __clk_notify(clk
, event
, clk
->rate
, clk
->new_rate
);
1665 if (ret
& NOTIFY_STOP_MASK
)
1669 hlist_for_each_entry(child
, &clk
->children
, child_node
) {
1670 /* Skip children who will be reparented to another clock */
1671 if (child
->new_parent
&& child
->new_parent
!= clk
)
1673 tmp_clk
= clk_propagate_rate_change(child
, event
);
1678 /* handle the new child who might not be in clk->children yet */
1679 if (clk
->new_child
) {
1680 tmp_clk
= clk_propagate_rate_change(clk
->new_child
, event
);
1689 * walk down a subtree and set the new rates notifying the rate
1692 static void clk_change_rate(struct clk_core
*clk
)
1694 struct clk_core
*child
;
1695 struct hlist_node
*tmp
;
1696 unsigned long old_rate
;
1697 unsigned long best_parent_rate
= 0;
1698 bool skip_set_rate
= false;
1699 struct clk_core
*old_parent
;
1701 old_rate
= clk
->rate
;
1703 if (clk
->new_parent
)
1704 best_parent_rate
= clk
->new_parent
->rate
;
1705 else if (clk
->parent
)
1706 best_parent_rate
= clk
->parent
->rate
;
1708 if (clk
->new_parent
&& clk
->new_parent
!= clk
->parent
) {
1709 old_parent
= __clk_set_parent_before(clk
, clk
->new_parent
);
1711 if (clk
->ops
->set_rate_and_parent
) {
1712 skip_set_rate
= true;
1713 clk
->ops
->set_rate_and_parent(clk
->hw
, clk
->new_rate
,
1715 clk
->new_parent_index
);
1716 } else if (clk
->ops
->set_parent
) {
1717 clk
->ops
->set_parent(clk
->hw
, clk
->new_parent_index
);
1720 __clk_set_parent_after(clk
, clk
->new_parent
, old_parent
);
1723 if (!skip_set_rate
&& clk
->ops
->set_rate
)
1724 clk
->ops
->set_rate(clk
->hw
, clk
->new_rate
, best_parent_rate
);
1726 clk
->rate
= clk_recalc(clk
, best_parent_rate
);
1728 if (clk
->notifier_count
&& old_rate
!= clk
->rate
)
1729 __clk_notify(clk
, POST_RATE_CHANGE
, old_rate
, clk
->rate
);
1732 * Use safe iteration, as change_rate can actually swap parents
1733 * for certain clock types.
1735 hlist_for_each_entry_safe(child
, tmp
, &clk
->children
, child_node
) {
1736 /* Skip children who will be reparented to another clock */
1737 if (child
->new_parent
&& child
->new_parent
!= clk
)
1739 clk_change_rate(child
);
1742 /* handle the new child who might not be in clk->children yet */
1744 clk_change_rate(clk
->new_child
);
1747 static int clk_core_set_rate_nolock(struct clk_core
*clk
,
1748 unsigned long req_rate
)
1750 struct clk_core
*top
, *fail_clk
;
1751 unsigned long rate
= req_rate
;
1757 /* bail early if nothing to do */
1758 if (rate
== clk_core_get_rate_nolock(clk
))
1761 if ((clk
->flags
& CLK_SET_RATE_GATE
) && clk
->prepare_count
)
1764 /* calculate new rates and get the topmost changed clock */
1765 top
= clk_calc_new_rates(clk
, rate
);
1769 /* notify that we are about to change rates */
1770 fail_clk
= clk_propagate_rate_change(top
, PRE_RATE_CHANGE
);
1772 pr_debug("%s: failed to set %s rate\n", __func__
,
1774 clk_propagate_rate_change(top
, ABORT_RATE_CHANGE
);
1778 /* change the rates */
1779 clk_change_rate(top
);
1781 clk
->req_rate
= req_rate
;
1787 * clk_set_rate - specify a new rate for clk
1788 * @clk: the clk whose rate is being changed
1789 * @rate: the new rate for clk
1791 * In the simplest case clk_set_rate will only adjust the rate of clk.
1793 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1794 * propagate up to clk's parent; whether or not this happens depends on the
1795 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1796 * after calling .round_rate then upstream parent propagation is ignored. If
1797 * *parent_rate comes back with a new rate for clk's parent then we propagate
1798 * up to clk's parent and set its rate. Upward propagation will continue
1799 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1800 * .round_rate stops requesting changes to clk's parent_rate.
1802 * Rate changes are accomplished via tree traversal that also recalculates the
1803 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
1805 * Returns 0 on success, -EERROR otherwise.
1807 int clk_set_rate(struct clk
*clk
, unsigned long rate
)
1814 /* prevent racing with updates to the clock topology */
1817 ret
= clk_core_set_rate_nolock(clk
->core
, rate
);
1819 clk_prepare_unlock();
1823 EXPORT_SYMBOL_GPL(clk_set_rate
);
1826 * clk_set_rate_range - set a rate range for a clock source
1827 * @clk: clock source
1828 * @min: desired minimum clock rate in Hz, inclusive
1829 * @max: desired maximum clock rate in Hz, inclusive
1831 * Returns success (0) or negative errno.
1833 int clk_set_rate_range(struct clk
*clk
, unsigned long min
, unsigned long max
)
1841 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
1842 __func__
, clk
->core
->name
, clk
->dev_id
, clk
->con_id
,
1849 if (min
!= clk
->min_rate
|| max
!= clk
->max_rate
) {
1850 clk
->min_rate
= min
;
1851 clk
->max_rate
= max
;
1852 ret
= clk_core_set_rate_nolock(clk
->core
, clk
->core
->req_rate
);
1855 clk_prepare_unlock();
1859 EXPORT_SYMBOL_GPL(clk_set_rate_range
);
1862 * clk_set_min_rate - set a minimum clock rate for a clock source
1863 * @clk: clock source
1864 * @rate: desired minimum clock rate in Hz, inclusive
1866 * Returns success (0) or negative errno.
1868 int clk_set_min_rate(struct clk
*clk
, unsigned long rate
)
1873 return clk_set_rate_range(clk
, rate
, clk
->max_rate
);
1875 EXPORT_SYMBOL_GPL(clk_set_min_rate
);
1878 * clk_set_max_rate - set a maximum clock rate for a clock source
1879 * @clk: clock source
1880 * @rate: desired maximum clock rate in Hz, inclusive
1882 * Returns success (0) or negative errno.
1884 int clk_set_max_rate(struct clk
*clk
, unsigned long rate
)
1889 return clk_set_rate_range(clk
, clk
->min_rate
, rate
);
1891 EXPORT_SYMBOL_GPL(clk_set_max_rate
);
1894 * clk_get_parent - return the parent of a clk
1895 * @clk: the clk whose parent gets returned
1897 * Simply returns clk->parent. Returns NULL if clk is NULL.
1899 struct clk
*clk_get_parent(struct clk
*clk
)
1904 parent
= __clk_get_parent(clk
);
1905 clk_prepare_unlock();
1909 EXPORT_SYMBOL_GPL(clk_get_parent
);
1912 * .get_parent is mandatory for clocks with multiple possible parents. It is
1913 * optional for single-parent clocks. Always call .get_parent if it is
1914 * available and WARN if it is missing for multi-parent clocks.
1916 * For single-parent clocks without .get_parent, first check to see if the
1917 * .parents array exists, and if so use it to avoid an expensive tree
1918 * traversal. If .parents does not exist then walk the tree.
1920 static struct clk_core
*__clk_init_parent(struct clk_core
*clk
)
1922 struct clk_core
*ret
= NULL
;
1925 /* handle the trivial cases */
1927 if (!clk
->num_parents
)
1930 if (clk
->num_parents
== 1) {
1931 if (IS_ERR_OR_NULL(clk
->parent
))
1932 clk
->parent
= clk_core_lookup(clk
->parent_names
[0]);
1937 if (!clk
->ops
->get_parent
) {
1938 WARN(!clk
->ops
->get_parent
,
1939 "%s: multi-parent clocks must implement .get_parent\n",
1945 * Do our best to cache parent clocks in clk->parents. This prevents
1946 * unnecessary and expensive lookups. We don't set clk->parent here;
1947 * that is done by the calling function.
1950 index
= clk
->ops
->get_parent(clk
->hw
);
1954 kcalloc(clk
->num_parents
, sizeof(struct clk
*),
1957 ret
= clk_core_get_parent_by_index(clk
, index
);
1963 static void clk_core_reparent(struct clk_core
*clk
,
1964 struct clk_core
*new_parent
)
1966 clk_reparent(clk
, new_parent
);
1967 __clk_recalc_accuracies(clk
);
1968 __clk_recalc_rates(clk
, POST_RATE_CHANGE
);
1972 * clk_has_parent - check if a clock is a possible parent for another
1973 * @clk: clock source
1974 * @parent: parent clock source
1976 * This function can be used in drivers that need to check that a clock can be
1977 * the parent of another without actually changing the parent.
1979 * Returns true if @parent is a possible parent for @clk, false otherwise.
1981 bool clk_has_parent(struct clk
*clk
, struct clk
*parent
)
1983 struct clk_core
*core
, *parent_core
;
1986 /* NULL clocks should be nops, so return success if either is NULL. */
1987 if (!clk
|| !parent
)
1991 parent_core
= parent
->core
;
1993 /* Optimize for the case where the parent is already the parent. */
1994 if (core
->parent
== parent_core
)
1997 for (i
= 0; i
< core
->num_parents
; i
++)
1998 if (strcmp(core
->parent_names
[i
], parent_core
->name
) == 0)
2003 EXPORT_SYMBOL_GPL(clk_has_parent
);
2005 static int clk_core_set_parent(struct clk_core
*clk
, struct clk_core
*parent
)
2009 unsigned long p_rate
= 0;
2014 /* verify ops for for multi-parent clks */
2015 if ((clk
->num_parents
> 1) && (!clk
->ops
->set_parent
))
2018 /* prevent racing with updates to the clock topology */
2021 if (clk
->parent
== parent
)
2024 /* check that we are allowed to re-parent if the clock is in use */
2025 if ((clk
->flags
& CLK_SET_PARENT_GATE
) && clk
->prepare_count
) {
2030 /* try finding the new parent index */
2032 p_index
= clk_fetch_parent_index(clk
, parent
);
2033 p_rate
= parent
->rate
;
2035 pr_debug("%s: clk %s can not be parent of clk %s\n",
2036 __func__
, parent
->name
, clk
->name
);
2042 /* propagate PRE_RATE_CHANGE notifications */
2043 ret
= __clk_speculate_rates(clk
, p_rate
);
2045 /* abort if a driver objects */
2046 if (ret
& NOTIFY_STOP_MASK
)
2049 /* do the re-parent */
2050 ret
= __clk_set_parent(clk
, parent
, p_index
);
2052 /* propagate rate an accuracy recalculation accordingly */
2054 __clk_recalc_rates(clk
, ABORT_RATE_CHANGE
);
2056 __clk_recalc_rates(clk
, POST_RATE_CHANGE
);
2057 __clk_recalc_accuracies(clk
);
2061 clk_prepare_unlock();
2067 * clk_set_parent - switch the parent of a mux clk
2068 * @clk: the mux clk whose input we are switching
2069 * @parent: the new input to clk
2071 * Re-parent clk to use parent as its new input source. If clk is in
2072 * prepared state, the clk will get enabled for the duration of this call. If
2073 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2074 * that, the reparenting is glitchy in hardware, etc), use the
2075 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2077 * After successfully changing clk's parent clk_set_parent will update the
2078 * clk topology, sysfs topology and propagate rate recalculation via
2079 * __clk_recalc_rates.
2081 * Returns 0 on success, -EERROR otherwise.
2083 int clk_set_parent(struct clk
*clk
, struct clk
*parent
)
2088 return clk_core_set_parent(clk
->core
, parent
? parent
->core
: NULL
);
2090 EXPORT_SYMBOL_GPL(clk_set_parent
);
2093 * clk_set_phase - adjust the phase shift of a clock signal
2094 * @clk: clock signal source
2095 * @degrees: number of degrees the signal is shifted
2097 * Shifts the phase of a clock signal by the specified
2098 * degrees. Returns 0 on success, -EERROR otherwise.
2100 * This function makes no distinction about the input or reference
2101 * signal that we adjust the clock signal phase against. For example
2102 * phase locked-loop clock signal generators we may shift phase with
2103 * respect to feedback clock signal input, but for other cases the
2104 * clock phase may be shifted with respect to some other, unspecified
2107 * Additionally the concept of phase shift does not propagate through
2108 * the clock tree hierarchy, which sets it apart from clock rates and
2109 * clock accuracy. A parent clock phase attribute does not have an
2110 * impact on the phase attribute of a child clock.
2112 int clk_set_phase(struct clk
*clk
, int degrees
)
2119 /* sanity check degrees */
2126 if (!clk
->core
->ops
->set_phase
)
2129 ret
= clk
->core
->ops
->set_phase(clk
->core
->hw
, degrees
);
2132 clk
->core
->phase
= degrees
;
2135 clk_prepare_unlock();
2140 EXPORT_SYMBOL_GPL(clk_set_phase
);
2142 static int clk_core_get_phase(struct clk_core
*clk
)
2151 clk_prepare_unlock();
2156 EXPORT_SYMBOL_GPL(clk_get_phase
);
2159 * clk_get_phase - return the phase shift of a clock signal
2160 * @clk: clock signal source
2162 * Returns the phase shift of a clock node in degrees, otherwise returns
2165 int clk_get_phase(struct clk
*clk
)
2170 return clk_core_get_phase(clk
->core
);
2174 * __clk_init - initialize the data structures in a struct clk
2175 * @dev: device initializing this clk, placeholder for now
2176 * @clk: clk being initialized
2178 * Initializes the lists in struct clk_core, queries the hardware for the
2179 * parent and rate and sets them both.
2181 static int __clk_init(struct device
*dev
, struct clk
*clk_user
)
2184 struct clk_core
*orphan
;
2185 struct hlist_node
*tmp2
;
2186 struct clk_core
*clk
;
2192 clk
= clk_user
->core
;
2196 /* check to see if a clock with this name is already registered */
2197 if (clk_core_lookup(clk
->name
)) {
2198 pr_debug("%s: clk %s already initialized\n",
2199 __func__
, clk
->name
);
2204 /* check that clk_ops are sane. See Documentation/clk.txt */
2205 if (clk
->ops
->set_rate
&&
2206 !((clk
->ops
->round_rate
|| clk
->ops
->determine_rate
) &&
2207 clk
->ops
->recalc_rate
)) {
2208 pr_warning("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
2209 __func__
, clk
->name
);
2214 if (clk
->ops
->set_parent
&& !clk
->ops
->get_parent
) {
2215 pr_warning("%s: %s must implement .get_parent & .set_parent\n",
2216 __func__
, clk
->name
);
2221 if (clk
->ops
->set_rate_and_parent
&&
2222 !(clk
->ops
->set_parent
&& clk
->ops
->set_rate
)) {
2223 pr_warn("%s: %s must implement .set_parent & .set_rate\n",
2224 __func__
, clk
->name
);
2229 /* throw a WARN if any entries in parent_names are NULL */
2230 for (i
= 0; i
< clk
->num_parents
; i
++)
2231 WARN(!clk
->parent_names
[i
],
2232 "%s: invalid NULL in %s's .parent_names\n",
2233 __func__
, clk
->name
);
2236 * Allocate an array of struct clk *'s to avoid unnecessary string
2237 * look-ups of clk's possible parents. This can fail for clocks passed
2238 * in to clk_init during early boot; thus any access to clk->parents[]
2239 * must always check for a NULL pointer and try to populate it if
2242 * If clk->parents is not NULL we skip this entire block. This allows
2243 * for clock drivers to statically initialize clk->parents.
2245 if (clk
->num_parents
> 1 && !clk
->parents
) {
2246 clk
->parents
= kcalloc(clk
->num_parents
, sizeof(struct clk
*),
2249 * clk_core_lookup returns NULL for parents that have not been
2250 * clk_init'd; thus any access to clk->parents[] must check
2251 * for a NULL pointer. We can always perform lazy lookups for
2252 * missing parents later on.
2255 for (i
= 0; i
< clk
->num_parents
; i
++)
2257 clk_core_lookup(clk
->parent_names
[i
]);
2260 clk
->parent
= __clk_init_parent(clk
);
2263 * Populate clk->parent if parent has already been __clk_init'd. If
2264 * parent has not yet been __clk_init'd then place clk in the orphan
2265 * list. If clk has set the CLK_IS_ROOT flag then place it in the root
2268 * Every time a new clk is clk_init'd then we walk the list of orphan
2269 * clocks and re-parent any that are children of the clock currently
2273 hlist_add_head(&clk
->child_node
,
2274 &clk
->parent
->children
);
2275 else if (clk
->flags
& CLK_IS_ROOT
)
2276 hlist_add_head(&clk
->child_node
, &clk_root_list
);
2278 hlist_add_head(&clk
->child_node
, &clk_orphan_list
);
2281 * Set clk's accuracy. The preferred method is to use
2282 * .recalc_accuracy. For simple clocks and lazy developers the default
2283 * fallback is to use the parent's accuracy. If a clock doesn't have a
2284 * parent (or is orphaned) then accuracy is set to zero (perfect
2287 if (clk
->ops
->recalc_accuracy
)
2288 clk
->accuracy
= clk
->ops
->recalc_accuracy(clk
->hw
,
2289 __clk_get_accuracy(clk
->parent
));
2290 else if (clk
->parent
)
2291 clk
->accuracy
= clk
->parent
->accuracy
;
2297 * Since a phase is by definition relative to its parent, just
2298 * query the current clock phase, or just assume it's in phase.
2300 if (clk
->ops
->get_phase
)
2301 clk
->phase
= clk
->ops
->get_phase(clk
->hw
);
2306 * Set clk's rate. The preferred method is to use .recalc_rate. For
2307 * simple clocks and lazy developers the default fallback is to use the
2308 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2309 * then rate is set to zero.
2311 if (clk
->ops
->recalc_rate
)
2312 rate
= clk
->ops
->recalc_rate(clk
->hw
,
2313 clk_core_get_rate_nolock(clk
->parent
));
2314 else if (clk
->parent
)
2315 rate
= clk
->parent
->rate
;
2318 clk
->rate
= clk
->req_rate
= rate
;
2321 * walk the list of orphan clocks and reparent any that are children of
2324 hlist_for_each_entry_safe(orphan
, tmp2
, &clk_orphan_list
, child_node
) {
2325 if (orphan
->num_parents
&& orphan
->ops
->get_parent
) {
2326 i
= orphan
->ops
->get_parent(orphan
->hw
);
2327 if (!strcmp(clk
->name
, orphan
->parent_names
[i
]))
2328 clk_core_reparent(orphan
, clk
);
2332 for (i
= 0; i
< orphan
->num_parents
; i
++)
2333 if (!strcmp(clk
->name
, orphan
->parent_names
[i
])) {
2334 clk_core_reparent(orphan
, clk
);
2340 * optional platform-specific magic
2342 * The .init callback is not used by any of the basic clock types, but
2343 * exists for weird hardware that must perform initialization magic.
2344 * Please consider other ways of solving initialization problems before
2345 * using this callback, as its use is discouraged.
2348 clk
->ops
->init(clk
->hw
);
2350 kref_init(&clk
->ref
);
2352 clk_prepare_unlock();
2355 clk_debug_register(clk
);
2360 struct clk
*__clk_create_clk(struct clk_hw
*hw
, const char *dev_id
,
2365 /* This is to allow this function to be chained to others */
2366 if (!hw
|| IS_ERR(hw
))
2367 return (struct clk
*) hw
;
2369 clk
= kzalloc(sizeof(*clk
), GFP_KERNEL
);
2371 return ERR_PTR(-ENOMEM
);
2373 clk
->core
= hw
->core
;
2374 clk
->dev_id
= dev_id
;
2375 clk
->con_id
= con_id
;
2376 clk
->max_rate
= ULONG_MAX
;
2379 hlist_add_head(&clk
->child_node
, &hw
->core
->clks
);
2380 clk_prepare_unlock();
2385 void __clk_free_clk(struct clk
*clk
)
2388 hlist_del(&clk
->child_node
);
2389 clk_prepare_unlock();
2395 * clk_register - allocate a new clock, register it and return an opaque cookie
2396 * @dev: device that is registering this clock
2397 * @hw: link to hardware-specific clock data
2399 * clk_register is the primary interface for populating the clock tree with new
2400 * clock nodes. It returns a pointer to the newly allocated struct clk which
2401 * cannot be dereferenced by driver code but may be used in conjuction with the
2402 * rest of the clock API. In the event of an error clk_register will return an
2403 * error code; drivers must test for an error code after calling clk_register.
2405 struct clk
*clk_register(struct device
*dev
, struct clk_hw
*hw
)
2408 struct clk_core
*clk
;
2410 clk
= kzalloc(sizeof(*clk
), GFP_KERNEL
);
2412 pr_err("%s: could not allocate clk\n", __func__
);
2417 clk
->name
= kstrdup_const(hw
->init
->name
, GFP_KERNEL
);
2419 pr_err("%s: could not allocate clk->name\n", __func__
);
2423 clk
->ops
= hw
->init
->ops
;
2424 if (dev
&& dev
->driver
)
2425 clk
->owner
= dev
->driver
->owner
;
2427 clk
->flags
= hw
->init
->flags
;
2428 clk
->num_parents
= hw
->init
->num_parents
;
2431 /* allocate local copy in case parent_names is __initdata */
2432 clk
->parent_names
= kcalloc(clk
->num_parents
, sizeof(char *),
2435 if (!clk
->parent_names
) {
2436 pr_err("%s: could not allocate clk->parent_names\n", __func__
);
2438 goto fail_parent_names
;
2442 /* copy each string name in case parent_names is __initdata */
2443 for (i
= 0; i
< clk
->num_parents
; i
++) {
2444 clk
->parent_names
[i
] = kstrdup_const(hw
->init
->parent_names
[i
],
2446 if (!clk
->parent_names
[i
]) {
2447 pr_err("%s: could not copy parent_names\n", __func__
);
2449 goto fail_parent_names_copy
;
2453 INIT_HLIST_HEAD(&clk
->clks
);
2455 hw
->clk
= __clk_create_clk(hw
, NULL
, NULL
);
2456 if (IS_ERR(hw
->clk
)) {
2457 pr_err("%s: could not allocate per-user clk\n", __func__
);
2458 ret
= PTR_ERR(hw
->clk
);
2459 goto fail_parent_names_copy
;
2462 ret
= __clk_init(dev
, hw
->clk
);
2466 __clk_free_clk(hw
->clk
);
2469 fail_parent_names_copy
:
2471 kfree_const(clk
->parent_names
[i
]);
2472 kfree(clk
->parent_names
);
2474 kfree_const(clk
->name
);
2478 return ERR_PTR(ret
);
2480 EXPORT_SYMBOL_GPL(clk_register
);
2483 * Free memory allocated for a clock.
2484 * Caller must hold prepare_lock.
2486 static void __clk_release(struct kref
*ref
)
2488 struct clk_core
*clk
= container_of(ref
, struct clk_core
, ref
);
2489 int i
= clk
->num_parents
;
2491 kfree(clk
->parents
);
2493 kfree_const(clk
->parent_names
[i
]);
2495 kfree(clk
->parent_names
);
2496 kfree_const(clk
->name
);
2501 * Empty clk_ops for unregistered clocks. These are used temporarily
2502 * after clk_unregister() was called on a clock and until last clock
2503 * consumer calls clk_put() and the struct clk object is freed.
2505 static int clk_nodrv_prepare_enable(struct clk_hw
*hw
)
2510 static void clk_nodrv_disable_unprepare(struct clk_hw
*hw
)
2515 static int clk_nodrv_set_rate(struct clk_hw
*hw
, unsigned long rate
,
2516 unsigned long parent_rate
)
2521 static int clk_nodrv_set_parent(struct clk_hw
*hw
, u8 index
)
2526 static const struct clk_ops clk_nodrv_ops
= {
2527 .enable
= clk_nodrv_prepare_enable
,
2528 .disable
= clk_nodrv_disable_unprepare
,
2529 .prepare
= clk_nodrv_prepare_enable
,
2530 .unprepare
= clk_nodrv_disable_unprepare
,
2531 .set_rate
= clk_nodrv_set_rate
,
2532 .set_parent
= clk_nodrv_set_parent
,
2536 * clk_unregister - unregister a currently registered clock
2537 * @clk: clock to unregister
2539 void clk_unregister(struct clk
*clk
)
2541 unsigned long flags
;
2543 if (!clk
|| WARN_ON_ONCE(IS_ERR(clk
)))
2546 clk_debug_unregister(clk
->core
);
2550 if (clk
->core
->ops
== &clk_nodrv_ops
) {
2551 pr_err("%s: unregistered clock: %s\n", __func__
,
2556 * Assign empty clock ops for consumers that might still hold
2557 * a reference to this clock.
2559 flags
= clk_enable_lock();
2560 clk
->core
->ops
= &clk_nodrv_ops
;
2561 clk_enable_unlock(flags
);
2563 if (!hlist_empty(&clk
->core
->children
)) {
2564 struct clk_core
*child
;
2565 struct hlist_node
*t
;
2567 /* Reparent all children to the orphan list. */
2568 hlist_for_each_entry_safe(child
, t
, &clk
->core
->children
,
2570 clk_core_set_parent(child
, NULL
);
2573 hlist_del_init(&clk
->core
->child_node
);
2575 if (clk
->core
->prepare_count
)
2576 pr_warn("%s: unregistering prepared clock: %s\n",
2577 __func__
, clk
->core
->name
);
2578 kref_put(&clk
->core
->ref
, __clk_release
);
2580 clk_prepare_unlock();
2582 EXPORT_SYMBOL_GPL(clk_unregister
);
2584 static void devm_clk_release(struct device
*dev
, void *res
)
2586 clk_unregister(*(struct clk
**)res
);
2590 * devm_clk_register - resource managed clk_register()
2591 * @dev: device that is registering this clock
2592 * @hw: link to hardware-specific clock data
2594 * Managed clk_register(). Clocks returned from this function are
2595 * automatically clk_unregister()ed on driver detach. See clk_register() for
2598 struct clk
*devm_clk_register(struct device
*dev
, struct clk_hw
*hw
)
2603 clkp
= devres_alloc(devm_clk_release
, sizeof(*clkp
), GFP_KERNEL
);
2605 return ERR_PTR(-ENOMEM
);
2607 clk
= clk_register(dev
, hw
);
2610 devres_add(dev
, clkp
);
2617 EXPORT_SYMBOL_GPL(devm_clk_register
);
2619 static int devm_clk_match(struct device
*dev
, void *res
, void *data
)
2621 struct clk
*c
= res
;
2628 * devm_clk_unregister - resource managed clk_unregister()
2629 * @clk: clock to unregister
2631 * Deallocate a clock allocated with devm_clk_register(). Normally
2632 * this function will not need to be called and the resource management
2633 * code will ensure that the resource is freed.
2635 void devm_clk_unregister(struct device
*dev
, struct clk
*clk
)
2637 WARN_ON(devres_release(dev
, devm_clk_release
, devm_clk_match
, clk
));
2639 EXPORT_SYMBOL_GPL(devm_clk_unregister
);
2644 int __clk_get(struct clk
*clk
)
2646 struct clk_core
*core
= !clk
? NULL
: clk
->core
;
2649 if (!try_module_get(core
->owner
))
2652 kref_get(&core
->ref
);
2657 void __clk_put(struct clk
*clk
)
2659 struct module
*owner
;
2661 if (!clk
|| WARN_ON_ONCE(IS_ERR(clk
)))
2666 hlist_del(&clk
->child_node
);
2667 if (clk
->min_rate
> clk
->core
->req_rate
||
2668 clk
->max_rate
< clk
->core
->req_rate
)
2669 clk_core_set_rate_nolock(clk
->core
, clk
->core
->req_rate
);
2671 owner
= clk
->core
->owner
;
2672 kref_put(&clk
->core
->ref
, __clk_release
);
2674 clk_prepare_unlock();
2681 /*** clk rate change notifiers ***/
2684 * clk_notifier_register - add a clk rate change notifier
2685 * @clk: struct clk * to watch
2686 * @nb: struct notifier_block * with callback info
2688 * Request notification when clk's rate changes. This uses an SRCU
2689 * notifier because we want it to block and notifier unregistrations are
2690 * uncommon. The callbacks associated with the notifier must not
2691 * re-enter into the clk framework by calling any top-level clk APIs;
2692 * this will cause a nested prepare_lock mutex.
2694 * In all notification cases cases (pre, post and abort rate change) the
2695 * original clock rate is passed to the callback via struct
2696 * clk_notifier_data.old_rate and the new frequency is passed via struct
2697 * clk_notifier_data.new_rate.
2699 * clk_notifier_register() must be called from non-atomic context.
2700 * Returns -EINVAL if called with null arguments, -ENOMEM upon
2701 * allocation failure; otherwise, passes along the return value of
2702 * srcu_notifier_chain_register().
2704 int clk_notifier_register(struct clk
*clk
, struct notifier_block
*nb
)
2706 struct clk_notifier
*cn
;
2714 /* search the list of notifiers for this clk */
2715 list_for_each_entry(cn
, &clk_notifier_list
, node
)
2719 /* if clk wasn't in the notifier list, allocate new clk_notifier */
2720 if (cn
->clk
!= clk
) {
2721 cn
= kzalloc(sizeof(struct clk_notifier
), GFP_KERNEL
);
2726 srcu_init_notifier_head(&cn
->notifier_head
);
2728 list_add(&cn
->node
, &clk_notifier_list
);
2731 ret
= srcu_notifier_chain_register(&cn
->notifier_head
, nb
);
2733 clk
->core
->notifier_count
++;
2736 clk_prepare_unlock();
2740 EXPORT_SYMBOL_GPL(clk_notifier_register
);
2743 * clk_notifier_unregister - remove a clk rate change notifier
2744 * @clk: struct clk *
2745 * @nb: struct notifier_block * with callback info
2747 * Request no further notification for changes to 'clk' and frees memory
2748 * allocated in clk_notifier_register.
2750 * Returns -EINVAL if called with null arguments; otherwise, passes
2751 * along the return value of srcu_notifier_chain_unregister().
2753 int clk_notifier_unregister(struct clk
*clk
, struct notifier_block
*nb
)
2755 struct clk_notifier
*cn
= NULL
;
2763 list_for_each_entry(cn
, &clk_notifier_list
, node
)
2767 if (cn
->clk
== clk
) {
2768 ret
= srcu_notifier_chain_unregister(&cn
->notifier_head
, nb
);
2770 clk
->core
->notifier_count
--;
2772 /* XXX the notifier code should handle this better */
2773 if (!cn
->notifier_head
.head
) {
2774 srcu_cleanup_notifier_head(&cn
->notifier_head
);
2775 list_del(&cn
->node
);
2783 clk_prepare_unlock();
2787 EXPORT_SYMBOL_GPL(clk_notifier_unregister
);
2791 * struct of_clk_provider - Clock provider registration structure
2792 * @link: Entry in global list of clock providers
2793 * @node: Pointer to device tree node of clock provider
2794 * @get: Get clock callback. Returns NULL or a struct clk for the
2795 * given clock specifier
2796 * @data: context pointer to be passed into @get callback
2798 struct of_clk_provider
{
2799 struct list_head link
;
2801 struct device_node
*node
;
2802 struct clk
*(*get
)(struct of_phandle_args
*clkspec
, void *data
);
2806 static const struct of_device_id __clk_of_table_sentinel
2807 __used
__section(__clk_of_table_end
);
2809 static LIST_HEAD(of_clk_providers
);
2810 static DEFINE_MUTEX(of_clk_mutex
);
2812 /* of_clk_provider list locking helpers */
2813 void of_clk_lock(void)
2815 mutex_lock(&of_clk_mutex
);
2818 void of_clk_unlock(void)
2820 mutex_unlock(&of_clk_mutex
);
2823 struct clk
*of_clk_src_simple_get(struct of_phandle_args
*clkspec
,
2828 EXPORT_SYMBOL_GPL(of_clk_src_simple_get
);
2830 struct clk
*of_clk_src_onecell_get(struct of_phandle_args
*clkspec
, void *data
)
2832 struct clk_onecell_data
*clk_data
= data
;
2833 unsigned int idx
= clkspec
->args
[0];
2835 if (idx
>= clk_data
->clk_num
) {
2836 pr_err("%s: invalid clock index %d\n", __func__
, idx
);
2837 return ERR_PTR(-EINVAL
);
2840 return clk_data
->clks
[idx
];
2842 EXPORT_SYMBOL_GPL(of_clk_src_onecell_get
);
2845 * of_clk_add_provider() - Register a clock provider for a node
2846 * @np: Device node pointer associated with clock provider
2847 * @clk_src_get: callback for decoding clock
2848 * @data: context pointer for @clk_src_get callback.
2850 int of_clk_add_provider(struct device_node
*np
,
2851 struct clk
*(*clk_src_get
)(struct of_phandle_args
*clkspec
,
2855 struct of_clk_provider
*cp
;
2858 cp
= kzalloc(sizeof(struct of_clk_provider
), GFP_KERNEL
);
2862 cp
->node
= of_node_get(np
);
2864 cp
->get
= clk_src_get
;
2866 mutex_lock(&of_clk_mutex
);
2867 list_add(&cp
->link
, &of_clk_providers
);
2868 mutex_unlock(&of_clk_mutex
);
2869 pr_debug("Added clock from %s\n", np
->full_name
);
2871 ret
= of_clk_set_defaults(np
, true);
2873 of_clk_del_provider(np
);
2877 EXPORT_SYMBOL_GPL(of_clk_add_provider
);
2880 * of_clk_del_provider() - Remove a previously registered clock provider
2881 * @np: Device node pointer associated with clock provider
2883 void of_clk_del_provider(struct device_node
*np
)
2885 struct of_clk_provider
*cp
;
2887 mutex_lock(&of_clk_mutex
);
2888 list_for_each_entry(cp
, &of_clk_providers
, link
) {
2889 if (cp
->node
== np
) {
2890 list_del(&cp
->link
);
2891 of_node_put(cp
->node
);
2896 mutex_unlock(&of_clk_mutex
);
2898 EXPORT_SYMBOL_GPL(of_clk_del_provider
);
2900 struct clk
*__of_clk_get_from_provider(struct of_phandle_args
*clkspec
,
2901 const char *dev_id
, const char *con_id
)
2903 struct of_clk_provider
*provider
;
2904 struct clk
*clk
= ERR_PTR(-EPROBE_DEFER
);
2906 /* Check if we have such a provider in our array */
2907 list_for_each_entry(provider
, &of_clk_providers
, link
) {
2908 if (provider
->node
== clkspec
->np
)
2909 clk
= provider
->get(clkspec
, provider
->data
);
2911 clk
= __clk_create_clk(__clk_get_hw(clk
), dev_id
,
2914 if (!IS_ERR(clk
) && !__clk_get(clk
)) {
2915 __clk_free_clk(clk
);
2916 clk
= ERR_PTR(-ENOENT
);
2926 struct clk
*of_clk_get_from_provider(struct of_phandle_args
*clkspec
)
2930 mutex_lock(&of_clk_mutex
);
2931 clk
= __of_clk_get_from_provider(clkspec
, NULL
, __func__
);
2932 mutex_unlock(&of_clk_mutex
);
2937 int of_clk_get_parent_count(struct device_node
*np
)
2939 return of_count_phandle_with_args(np
, "clocks", "#clock-cells");
2941 EXPORT_SYMBOL_GPL(of_clk_get_parent_count
);
2943 const char *of_clk_get_parent_name(struct device_node
*np
, int index
)
2945 struct of_phandle_args clkspec
;
2946 struct property
*prop
;
2947 const char *clk_name
;
2956 rc
= of_parse_phandle_with_args(np
, "clocks", "#clock-cells", index
,
2961 index
= clkspec
.args_count
? clkspec
.args
[0] : 0;
2964 /* if there is an indices property, use it to transfer the index
2965 * specified into an array offset for the clock-output-names property.
2967 of_property_for_each_u32(clkspec
.np
, "clock-indices", prop
, vp
, pv
) {
2975 if (of_property_read_string_index(clkspec
.np
, "clock-output-names",
2978 clk_name
= clkspec
.np
->name
;
2980 of_node_put(clkspec
.np
);
2983 EXPORT_SYMBOL_GPL(of_clk_get_parent_name
);
2985 struct clock_provider
{
2986 of_clk_init_cb_t clk_init_cb
;
2987 struct device_node
*np
;
2988 struct list_head node
;
2991 static LIST_HEAD(clk_provider_list
);
2994 * This function looks for a parent clock. If there is one, then it
2995 * checks that the provider for this parent clock was initialized, in
2996 * this case the parent clock will be ready.
2998 static int parent_ready(struct device_node
*np
)
3003 struct clk
*clk
= of_clk_get(np
, i
);
3005 /* this parent is ready we can check the next one */
3012 /* at least one parent is not ready, we exit now */
3013 if (PTR_ERR(clk
) == -EPROBE_DEFER
)
3017 * Here we make assumption that the device tree is
3018 * written correctly. So an error means that there is
3019 * no more parent. As we didn't exit yet, then the
3020 * previous parent are ready. If there is no clock
3021 * parent, no need to wait for them, then we can
3022 * consider their absence as being ready
3029 * of_clk_init() - Scan and init clock providers from the DT
3030 * @matches: array of compatible values and init functions for providers.
3032 * This function scans the device tree for matching clock providers
3033 * and calls their initialization functions. It also does it by trying
3034 * to follow the dependencies.
3036 void __init
of_clk_init(const struct of_device_id
*matches
)
3038 const struct of_device_id
*match
;
3039 struct device_node
*np
;
3040 struct clock_provider
*clk_provider
, *next
;
3045 matches
= &__clk_of_table
;
3047 /* First prepare the list of the clocks providers */
3048 for_each_matching_node_and_match(np
, matches
, &match
) {
3049 struct clock_provider
*parent
=
3050 kzalloc(sizeof(struct clock_provider
), GFP_KERNEL
);
3052 parent
->clk_init_cb
= match
->data
;
3054 list_add_tail(&parent
->node
, &clk_provider_list
);
3057 while (!list_empty(&clk_provider_list
)) {
3058 is_init_done
= false;
3059 list_for_each_entry_safe(clk_provider
, next
,
3060 &clk_provider_list
, node
) {
3061 if (force
|| parent_ready(clk_provider
->np
)) {
3063 clk_provider
->clk_init_cb(clk_provider
->np
);
3064 of_clk_set_defaults(clk_provider
->np
, true);
3066 list_del(&clk_provider
->node
);
3067 kfree(clk_provider
);
3068 is_init_done
= true;
3073 * We didn't manage to initialize any of the
3074 * remaining providers during the last loop, so now we
3075 * initialize all the remaining ones unconditionally
3076 * in case the clock parent was not mandatory