Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / clk / clk.c
blob0f686a9dac3e78212b390ab985e1c571557c65f0
1 /*
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>
21 #include <linux/of.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>
27 #include <linux/stringify.h>
29 #include "clk.h"
31 static DEFINE_SPINLOCK(enable_lock);
32 static DEFINE_MUTEX(prepare_lock);
34 static struct task_struct *prepare_owner;
35 static struct task_struct *enable_owner;
37 static int prepare_refcnt;
38 static int enable_refcnt;
40 static HLIST_HEAD(clk_root_list);
41 static HLIST_HEAD(clk_orphan_list);
42 static LIST_HEAD(clk_notifier_list);
44 /*** private data structures ***/
46 struct clk_core {
47 const char *name;
48 const struct clk_ops *ops;
49 struct clk_hw *hw;
50 struct module *owner;
51 struct device *dev;
52 struct clk_core *parent;
53 const char **parent_names;
54 struct clk_core **parents;
55 u8 num_parents;
56 u8 new_parent_index;
57 unsigned long rate;
58 unsigned long req_rate;
59 unsigned long new_rate;
60 struct clk_core *new_parent;
61 struct clk_core *new_child;
62 unsigned long flags;
63 bool orphan;
64 unsigned int enable_count;
65 unsigned int prepare_count;
66 unsigned int protect_count;
67 unsigned long min_rate;
68 unsigned long max_rate;
69 unsigned long accuracy;
70 int phase;
71 struct hlist_head children;
72 struct hlist_node child_node;
73 struct hlist_head clks;
74 unsigned int notifier_count;
75 #ifdef CONFIG_DEBUG_FS
76 struct dentry *dentry;
77 struct hlist_node debug_node;
78 #endif
79 struct kref ref;
82 #define CREATE_TRACE_POINTS
83 #include <trace/events/clk.h>
85 struct clk {
86 struct clk_core *core;
87 const char *dev_id;
88 const char *con_id;
89 unsigned long min_rate;
90 unsigned long max_rate;
91 unsigned int exclusive_count;
92 struct hlist_node clks_node;
95 /*** runtime pm ***/
96 static int clk_pm_runtime_get(struct clk_core *core)
98 int ret = 0;
100 if (!core->dev)
101 return 0;
103 ret = pm_runtime_get_sync(core->dev);
104 return ret < 0 ? ret : 0;
107 static void clk_pm_runtime_put(struct clk_core *core)
109 if (!core->dev)
110 return;
112 pm_runtime_put_sync(core->dev);
115 /*** locking ***/
116 static void clk_prepare_lock(void)
118 if (!mutex_trylock(&prepare_lock)) {
119 if (prepare_owner == current) {
120 prepare_refcnt++;
121 return;
123 mutex_lock(&prepare_lock);
125 WARN_ON_ONCE(prepare_owner != NULL);
126 WARN_ON_ONCE(prepare_refcnt != 0);
127 prepare_owner = current;
128 prepare_refcnt = 1;
131 static void clk_prepare_unlock(void)
133 WARN_ON_ONCE(prepare_owner != current);
134 WARN_ON_ONCE(prepare_refcnt == 0);
136 if (--prepare_refcnt)
137 return;
138 prepare_owner = NULL;
139 mutex_unlock(&prepare_lock);
142 static unsigned long clk_enable_lock(void)
143 __acquires(enable_lock)
145 unsigned long flags;
148 * On UP systems, spin_trylock_irqsave() always returns true, even if
149 * we already hold the lock. So, in that case, we rely only on
150 * reference counting.
152 if (!IS_ENABLED(CONFIG_SMP) ||
153 !spin_trylock_irqsave(&enable_lock, flags)) {
154 if (enable_owner == current) {
155 enable_refcnt++;
156 __acquire(enable_lock);
157 if (!IS_ENABLED(CONFIG_SMP))
158 local_save_flags(flags);
159 return flags;
161 spin_lock_irqsave(&enable_lock, flags);
163 WARN_ON_ONCE(enable_owner != NULL);
164 WARN_ON_ONCE(enable_refcnt != 0);
165 enable_owner = current;
166 enable_refcnt = 1;
167 return flags;
170 static void clk_enable_unlock(unsigned long flags)
171 __releases(enable_lock)
173 WARN_ON_ONCE(enable_owner != current);
174 WARN_ON_ONCE(enable_refcnt == 0);
176 if (--enable_refcnt) {
177 __release(enable_lock);
178 return;
180 enable_owner = NULL;
181 spin_unlock_irqrestore(&enable_lock, flags);
184 static bool clk_core_rate_is_protected(struct clk_core *core)
186 return core->protect_count;
189 static bool clk_core_is_prepared(struct clk_core *core)
191 bool ret = false;
194 * .is_prepared is optional for clocks that can prepare
195 * fall back to software usage counter if it is missing
197 if (!core->ops->is_prepared)
198 return core->prepare_count;
200 if (!clk_pm_runtime_get(core)) {
201 ret = core->ops->is_prepared(core->hw);
202 clk_pm_runtime_put(core);
205 return ret;
208 static bool clk_core_is_enabled(struct clk_core *core)
210 bool ret = false;
213 * .is_enabled is only mandatory for clocks that gate
214 * fall back to software usage counter if .is_enabled is missing
216 if (!core->ops->is_enabled)
217 return core->enable_count;
220 * Check if clock controller's device is runtime active before
221 * calling .is_enabled callback. If not, assume that clock is
222 * disabled, because we might be called from atomic context, from
223 * which pm_runtime_get() is not allowed.
224 * This function is called mainly from clk_disable_unused_subtree,
225 * which ensures proper runtime pm activation of controller before
226 * taking enable spinlock, but the below check is needed if one tries
227 * to call it from other places.
229 if (core->dev) {
230 pm_runtime_get_noresume(core->dev);
231 if (!pm_runtime_active(core->dev)) {
232 ret = false;
233 goto done;
237 ret = core->ops->is_enabled(core->hw);
238 done:
239 if (core->dev)
240 pm_runtime_put(core->dev);
242 return ret;
245 /*** helper functions ***/
247 const char *__clk_get_name(const struct clk *clk)
249 return !clk ? NULL : clk->core->name;
251 EXPORT_SYMBOL_GPL(__clk_get_name);
253 const char *clk_hw_get_name(const struct clk_hw *hw)
255 return hw->core->name;
257 EXPORT_SYMBOL_GPL(clk_hw_get_name);
259 struct clk_hw *__clk_get_hw(struct clk *clk)
261 return !clk ? NULL : clk->core->hw;
263 EXPORT_SYMBOL_GPL(__clk_get_hw);
265 unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
267 return hw->core->num_parents;
269 EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
271 struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
273 return hw->core->parent ? hw->core->parent->hw : NULL;
275 EXPORT_SYMBOL_GPL(clk_hw_get_parent);
277 static struct clk_core *__clk_lookup_subtree(const char *name,
278 struct clk_core *core)
280 struct clk_core *child;
281 struct clk_core *ret;
283 if (!strcmp(core->name, name))
284 return core;
286 hlist_for_each_entry(child, &core->children, child_node) {
287 ret = __clk_lookup_subtree(name, child);
288 if (ret)
289 return ret;
292 return NULL;
295 static struct clk_core *clk_core_lookup(const char *name)
297 struct clk_core *root_clk;
298 struct clk_core *ret;
300 if (!name)
301 return NULL;
303 /* search the 'proper' clk tree first */
304 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
305 ret = __clk_lookup_subtree(name, root_clk);
306 if (ret)
307 return ret;
310 /* if not found, then search the orphan tree */
311 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
312 ret = __clk_lookup_subtree(name, root_clk);
313 if (ret)
314 return ret;
317 return NULL;
320 static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
321 u8 index)
323 if (!core || index >= core->num_parents)
324 return NULL;
326 if (!core->parents[index])
327 core->parents[index] =
328 clk_core_lookup(core->parent_names[index]);
330 return core->parents[index];
333 struct clk_hw *
334 clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
336 struct clk_core *parent;
338 parent = clk_core_get_parent_by_index(hw->core, index);
340 return !parent ? NULL : parent->hw;
342 EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
344 unsigned int __clk_get_enable_count(struct clk *clk)
346 return !clk ? 0 : clk->core->enable_count;
349 static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
351 unsigned long ret;
353 if (!core) {
354 ret = 0;
355 goto out;
358 ret = core->rate;
360 if (!core->num_parents)
361 goto out;
363 if (!core->parent)
364 ret = 0;
366 out:
367 return ret;
370 unsigned long clk_hw_get_rate(const struct clk_hw *hw)
372 return clk_core_get_rate_nolock(hw->core);
374 EXPORT_SYMBOL_GPL(clk_hw_get_rate);
376 static unsigned long __clk_get_accuracy(struct clk_core *core)
378 if (!core)
379 return 0;
381 return core->accuracy;
384 unsigned long __clk_get_flags(struct clk *clk)
386 return !clk ? 0 : clk->core->flags;
388 EXPORT_SYMBOL_GPL(__clk_get_flags);
390 unsigned long clk_hw_get_flags(const struct clk_hw *hw)
392 return hw->core->flags;
394 EXPORT_SYMBOL_GPL(clk_hw_get_flags);
396 bool clk_hw_is_prepared(const struct clk_hw *hw)
398 return clk_core_is_prepared(hw->core);
401 bool clk_hw_rate_is_protected(const struct clk_hw *hw)
403 return clk_core_rate_is_protected(hw->core);
406 bool clk_hw_is_enabled(const struct clk_hw *hw)
408 return clk_core_is_enabled(hw->core);
411 bool __clk_is_enabled(struct clk *clk)
413 if (!clk)
414 return false;
416 return clk_core_is_enabled(clk->core);
418 EXPORT_SYMBOL_GPL(__clk_is_enabled);
420 static bool mux_is_better_rate(unsigned long rate, unsigned long now,
421 unsigned long best, unsigned long flags)
423 if (flags & CLK_MUX_ROUND_CLOSEST)
424 return abs(now - rate) < abs(best - rate);
426 return now <= rate && now > best;
429 static int
430 clk_mux_determine_rate_flags(struct clk_hw *hw, struct clk_rate_request *req,
431 unsigned long flags)
433 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
434 int i, num_parents, ret;
435 unsigned long best = 0;
436 struct clk_rate_request parent_req = *req;
438 /* if NO_REPARENT flag set, pass through to current parent */
439 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
440 parent = core->parent;
441 if (core->flags & CLK_SET_RATE_PARENT) {
442 ret = __clk_determine_rate(parent ? parent->hw : NULL,
443 &parent_req);
444 if (ret)
445 return ret;
447 best = parent_req.rate;
448 } else if (parent) {
449 best = clk_core_get_rate_nolock(parent);
450 } else {
451 best = clk_core_get_rate_nolock(core);
454 goto out;
457 /* find the parent that can provide the fastest rate <= rate */
458 num_parents = core->num_parents;
459 for (i = 0; i < num_parents; i++) {
460 parent = clk_core_get_parent_by_index(core, i);
461 if (!parent)
462 continue;
464 if (core->flags & CLK_SET_RATE_PARENT) {
465 parent_req = *req;
466 ret = __clk_determine_rate(parent->hw, &parent_req);
467 if (ret)
468 continue;
469 } else {
470 parent_req.rate = clk_core_get_rate_nolock(parent);
473 if (mux_is_better_rate(req->rate, parent_req.rate,
474 best, flags)) {
475 best_parent = parent;
476 best = parent_req.rate;
480 if (!best_parent)
481 return -EINVAL;
483 out:
484 if (best_parent)
485 req->best_parent_hw = best_parent->hw;
486 req->best_parent_rate = best;
487 req->rate = best;
489 return 0;
492 struct clk *__clk_lookup(const char *name)
494 struct clk_core *core = clk_core_lookup(name);
496 return !core ? NULL : core->hw->clk;
499 static void clk_core_get_boundaries(struct clk_core *core,
500 unsigned long *min_rate,
501 unsigned long *max_rate)
503 struct clk *clk_user;
505 *min_rate = core->min_rate;
506 *max_rate = core->max_rate;
508 hlist_for_each_entry(clk_user, &core->clks, clks_node)
509 *min_rate = max(*min_rate, clk_user->min_rate);
511 hlist_for_each_entry(clk_user, &core->clks, clks_node)
512 *max_rate = min(*max_rate, clk_user->max_rate);
515 void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
516 unsigned long max_rate)
518 hw->core->min_rate = min_rate;
519 hw->core->max_rate = max_rate;
521 EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
524 * Helper for finding best parent to provide a given frequency. This can be used
525 * directly as a determine_rate callback (e.g. for a mux), or from a more
526 * complex clock that may combine a mux with other operations.
528 int __clk_mux_determine_rate(struct clk_hw *hw,
529 struct clk_rate_request *req)
531 return clk_mux_determine_rate_flags(hw, req, 0);
533 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
535 int __clk_mux_determine_rate_closest(struct clk_hw *hw,
536 struct clk_rate_request *req)
538 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
540 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
542 /*** clk api ***/
544 static void clk_core_rate_unprotect(struct clk_core *core)
546 lockdep_assert_held(&prepare_lock);
548 if (!core)
549 return;
551 if (WARN_ON(core->protect_count == 0))
552 return;
554 if (--core->protect_count > 0)
555 return;
557 clk_core_rate_unprotect(core->parent);
560 static int clk_core_rate_nuke_protect(struct clk_core *core)
562 int ret;
564 lockdep_assert_held(&prepare_lock);
566 if (!core)
567 return -EINVAL;
569 if (core->protect_count == 0)
570 return 0;
572 ret = core->protect_count;
573 core->protect_count = 1;
574 clk_core_rate_unprotect(core);
576 return ret;
580 * clk_rate_exclusive_put - release exclusivity over clock rate control
581 * @clk: the clk over which the exclusivity is released
583 * clk_rate_exclusive_put() completes a critical section during which a clock
584 * consumer cannot tolerate any other consumer making any operation on the
585 * clock which could result in a rate change or rate glitch. Exclusive clocks
586 * cannot have their rate changed, either directly or indirectly due to changes
587 * further up the parent chain of clocks. As a result, clocks up parent chain
588 * also get under exclusive control of the calling consumer.
590 * If exlusivity is claimed more than once on clock, even by the same consumer,
591 * the rate effectively gets locked as exclusivity can't be preempted.
593 * Calls to clk_rate_exclusive_put() must be balanced with calls to
594 * clk_rate_exclusive_get(). Calls to this function may sleep, and do not return
595 * error status.
597 void clk_rate_exclusive_put(struct clk *clk)
599 if (!clk)
600 return;
602 clk_prepare_lock();
605 * if there is something wrong with this consumer protect count, stop
606 * here before messing with the provider
608 if (WARN_ON(clk->exclusive_count <= 0))
609 goto out;
611 clk_core_rate_unprotect(clk->core);
612 clk->exclusive_count--;
613 out:
614 clk_prepare_unlock();
616 EXPORT_SYMBOL_GPL(clk_rate_exclusive_put);
618 static void clk_core_rate_protect(struct clk_core *core)
620 lockdep_assert_held(&prepare_lock);
622 if (!core)
623 return;
625 if (core->protect_count == 0)
626 clk_core_rate_protect(core->parent);
628 core->protect_count++;
631 static void clk_core_rate_restore_protect(struct clk_core *core, int count)
633 lockdep_assert_held(&prepare_lock);
635 if (!core)
636 return;
638 if (count == 0)
639 return;
641 clk_core_rate_protect(core);
642 core->protect_count = count;
646 * clk_rate_exclusive_get - get exclusivity over the clk rate control
647 * @clk: the clk over which the exclusity of rate control is requested
649 * clk_rate_exlusive_get() begins a critical section during which a clock
650 * consumer cannot tolerate any other consumer making any operation on the
651 * clock which could result in a rate change or rate glitch. Exclusive clocks
652 * cannot have their rate changed, either directly or indirectly due to changes
653 * further up the parent chain of clocks. As a result, clocks up parent chain
654 * also get under exclusive control of the calling consumer.
656 * If exlusivity is claimed more than once on clock, even by the same consumer,
657 * the rate effectively gets locked as exclusivity can't be preempted.
659 * Calls to clk_rate_exclusive_get() should be balanced with calls to
660 * clk_rate_exclusive_put(). Calls to this function may sleep.
661 * Returns 0 on success, -EERROR otherwise
663 int clk_rate_exclusive_get(struct clk *clk)
665 if (!clk)
666 return 0;
668 clk_prepare_lock();
669 clk_core_rate_protect(clk->core);
670 clk->exclusive_count++;
671 clk_prepare_unlock();
673 return 0;
675 EXPORT_SYMBOL_GPL(clk_rate_exclusive_get);
677 static void clk_core_unprepare(struct clk_core *core)
679 lockdep_assert_held(&prepare_lock);
681 if (!core)
682 return;
684 if (WARN_ON(core->prepare_count == 0))
685 return;
687 if (WARN_ON(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL))
688 return;
690 if (--core->prepare_count > 0)
691 return;
693 WARN_ON(core->enable_count > 0);
695 trace_clk_unprepare(core);
697 if (core->ops->unprepare)
698 core->ops->unprepare(core->hw);
700 clk_pm_runtime_put(core);
702 trace_clk_unprepare_complete(core);
703 clk_core_unprepare(core->parent);
706 static void clk_core_unprepare_lock(struct clk_core *core)
708 clk_prepare_lock();
709 clk_core_unprepare(core);
710 clk_prepare_unlock();
714 * clk_unprepare - undo preparation of a clock source
715 * @clk: the clk being unprepared
717 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
718 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
719 * if the operation may sleep. One example is a clk which is accessed over
720 * I2c. In the complex case a clk gate operation may require a fast and a slow
721 * part. It is this reason that clk_unprepare and clk_disable are not mutually
722 * exclusive. In fact clk_disable must be called before clk_unprepare.
724 void clk_unprepare(struct clk *clk)
726 if (IS_ERR_OR_NULL(clk))
727 return;
729 clk_core_unprepare_lock(clk->core);
731 EXPORT_SYMBOL_GPL(clk_unprepare);
733 static int clk_core_prepare(struct clk_core *core)
735 int ret = 0;
737 lockdep_assert_held(&prepare_lock);
739 if (!core)
740 return 0;
742 if (core->prepare_count == 0) {
743 ret = clk_pm_runtime_get(core);
744 if (ret)
745 return ret;
747 ret = clk_core_prepare(core->parent);
748 if (ret)
749 goto runtime_put;
751 trace_clk_prepare(core);
753 if (core->ops->prepare)
754 ret = core->ops->prepare(core->hw);
756 trace_clk_prepare_complete(core);
758 if (ret)
759 goto unprepare;
762 core->prepare_count++;
764 return 0;
765 unprepare:
766 clk_core_unprepare(core->parent);
767 runtime_put:
768 clk_pm_runtime_put(core);
769 return ret;
772 static int clk_core_prepare_lock(struct clk_core *core)
774 int ret;
776 clk_prepare_lock();
777 ret = clk_core_prepare(core);
778 clk_prepare_unlock();
780 return ret;
784 * clk_prepare - prepare a clock source
785 * @clk: the clk being prepared
787 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
788 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
789 * operation may sleep. One example is a clk which is accessed over I2c. In
790 * the complex case a clk ungate operation may require a fast and a slow part.
791 * It is this reason that clk_prepare and clk_enable are not mutually
792 * exclusive. In fact clk_prepare must be called before clk_enable.
793 * Returns 0 on success, -EERROR otherwise.
795 int clk_prepare(struct clk *clk)
797 if (!clk)
798 return 0;
800 return clk_core_prepare_lock(clk->core);
802 EXPORT_SYMBOL_GPL(clk_prepare);
804 static void clk_core_disable(struct clk_core *core)
806 lockdep_assert_held(&enable_lock);
808 if (!core)
809 return;
811 if (WARN_ON(core->enable_count == 0))
812 return;
814 if (WARN_ON(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL))
815 return;
817 if (--core->enable_count > 0)
818 return;
820 trace_clk_disable_rcuidle(core);
822 if (core->ops->disable)
823 core->ops->disable(core->hw);
825 trace_clk_disable_complete_rcuidle(core);
827 clk_core_disable(core->parent);
830 static void clk_core_disable_lock(struct clk_core *core)
832 unsigned long flags;
834 flags = clk_enable_lock();
835 clk_core_disable(core);
836 clk_enable_unlock(flags);
840 * clk_disable - gate a clock
841 * @clk: the clk being gated
843 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
844 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
845 * clk if the operation is fast and will never sleep. One example is a
846 * SoC-internal clk which is controlled via simple register writes. In the
847 * complex case a clk gate operation may require a fast and a slow part. It is
848 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
849 * In fact clk_disable must be called before clk_unprepare.
851 void clk_disable(struct clk *clk)
853 if (IS_ERR_OR_NULL(clk))
854 return;
856 clk_core_disable_lock(clk->core);
858 EXPORT_SYMBOL_GPL(clk_disable);
860 static int clk_core_enable(struct clk_core *core)
862 int ret = 0;
864 lockdep_assert_held(&enable_lock);
866 if (!core)
867 return 0;
869 if (WARN_ON(core->prepare_count == 0))
870 return -ESHUTDOWN;
872 if (core->enable_count == 0) {
873 ret = clk_core_enable(core->parent);
875 if (ret)
876 return ret;
878 trace_clk_enable_rcuidle(core);
880 if (core->ops->enable)
881 ret = core->ops->enable(core->hw);
883 trace_clk_enable_complete_rcuidle(core);
885 if (ret) {
886 clk_core_disable(core->parent);
887 return ret;
891 core->enable_count++;
892 return 0;
895 static int clk_core_enable_lock(struct clk_core *core)
897 unsigned long flags;
898 int ret;
900 flags = clk_enable_lock();
901 ret = clk_core_enable(core);
902 clk_enable_unlock(flags);
904 return ret;
908 * clk_enable - ungate a clock
909 * @clk: the clk being ungated
911 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
912 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
913 * if the operation will never sleep. One example is a SoC-internal clk which
914 * is controlled via simple register writes. In the complex case a clk ungate
915 * operation may require a fast and a slow part. It is this reason that
916 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
917 * must be called before clk_enable. Returns 0 on success, -EERROR
918 * otherwise.
920 int clk_enable(struct clk *clk)
922 if (!clk)
923 return 0;
925 return clk_core_enable_lock(clk->core);
927 EXPORT_SYMBOL_GPL(clk_enable);
929 static int clk_core_prepare_enable(struct clk_core *core)
931 int ret;
933 ret = clk_core_prepare_lock(core);
934 if (ret)
935 return ret;
937 ret = clk_core_enable_lock(core);
938 if (ret)
939 clk_core_unprepare_lock(core);
941 return ret;
944 static void clk_core_disable_unprepare(struct clk_core *core)
946 clk_core_disable_lock(core);
947 clk_core_unprepare_lock(core);
950 static void clk_unprepare_unused_subtree(struct clk_core *core)
952 struct clk_core *child;
954 lockdep_assert_held(&prepare_lock);
956 hlist_for_each_entry(child, &core->children, child_node)
957 clk_unprepare_unused_subtree(child);
959 if (core->prepare_count)
960 return;
962 if (core->flags & CLK_IGNORE_UNUSED)
963 return;
965 if (clk_pm_runtime_get(core))
966 return;
968 if (clk_core_is_prepared(core)) {
969 trace_clk_unprepare(core);
970 if (core->ops->unprepare_unused)
971 core->ops->unprepare_unused(core->hw);
972 else if (core->ops->unprepare)
973 core->ops->unprepare(core->hw);
974 trace_clk_unprepare_complete(core);
977 clk_pm_runtime_put(core);
980 static void clk_disable_unused_subtree(struct clk_core *core)
982 struct clk_core *child;
983 unsigned long flags;
985 lockdep_assert_held(&prepare_lock);
987 hlist_for_each_entry(child, &core->children, child_node)
988 clk_disable_unused_subtree(child);
990 if (core->flags & CLK_OPS_PARENT_ENABLE)
991 clk_core_prepare_enable(core->parent);
993 if (clk_pm_runtime_get(core))
994 goto unprepare_out;
996 flags = clk_enable_lock();
998 if (core->enable_count)
999 goto unlock_out;
1001 if (core->flags & CLK_IGNORE_UNUSED)
1002 goto unlock_out;
1005 * some gate clocks have special needs during the disable-unused
1006 * sequence. call .disable_unused if available, otherwise fall
1007 * back to .disable
1009 if (clk_core_is_enabled(core)) {
1010 trace_clk_disable(core);
1011 if (core->ops->disable_unused)
1012 core->ops->disable_unused(core->hw);
1013 else if (core->ops->disable)
1014 core->ops->disable(core->hw);
1015 trace_clk_disable_complete(core);
1018 unlock_out:
1019 clk_enable_unlock(flags);
1020 clk_pm_runtime_put(core);
1021 unprepare_out:
1022 if (core->flags & CLK_OPS_PARENT_ENABLE)
1023 clk_core_disable_unprepare(core->parent);
1026 static bool clk_ignore_unused;
1027 static int __init clk_ignore_unused_setup(char *__unused)
1029 clk_ignore_unused = true;
1030 return 1;
1032 __setup("clk_ignore_unused", clk_ignore_unused_setup);
1034 static int clk_disable_unused(void)
1036 struct clk_core *core;
1038 if (clk_ignore_unused) {
1039 pr_warn("clk: Not disabling unused clocks\n");
1040 return 0;
1043 clk_prepare_lock();
1045 hlist_for_each_entry(core, &clk_root_list, child_node)
1046 clk_disable_unused_subtree(core);
1048 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1049 clk_disable_unused_subtree(core);
1051 hlist_for_each_entry(core, &clk_root_list, child_node)
1052 clk_unprepare_unused_subtree(core);
1054 hlist_for_each_entry(core, &clk_orphan_list, child_node)
1055 clk_unprepare_unused_subtree(core);
1057 clk_prepare_unlock();
1059 return 0;
1061 late_initcall_sync(clk_disable_unused);
1063 static int clk_core_determine_round_nolock(struct clk_core *core,
1064 struct clk_rate_request *req)
1066 long rate;
1068 lockdep_assert_held(&prepare_lock);
1070 if (!core)
1071 return 0;
1074 * At this point, core protection will be disabled if
1075 * - if the provider is not protected at all
1076 * - if the calling consumer is the only one which has exclusivity
1077 * over the provider
1079 if (clk_core_rate_is_protected(core)) {
1080 req->rate = core->rate;
1081 } else if (core->ops->determine_rate) {
1082 return core->ops->determine_rate(core->hw, req);
1083 } else if (core->ops->round_rate) {
1084 rate = core->ops->round_rate(core->hw, req->rate,
1085 &req->best_parent_rate);
1086 if (rate < 0)
1087 return rate;
1089 req->rate = rate;
1090 } else {
1091 return -EINVAL;
1094 return 0;
1097 static void clk_core_init_rate_req(struct clk_core * const core,
1098 struct clk_rate_request *req)
1100 struct clk_core *parent;
1102 if (WARN_ON(!core || !req))
1103 return;
1105 parent = core->parent;
1106 if (parent) {
1107 req->best_parent_hw = parent->hw;
1108 req->best_parent_rate = parent->rate;
1109 } else {
1110 req->best_parent_hw = NULL;
1111 req->best_parent_rate = 0;
1115 static bool clk_core_can_round(struct clk_core * const core)
1117 if (core->ops->determine_rate || core->ops->round_rate)
1118 return true;
1120 return false;
1123 static int clk_core_round_rate_nolock(struct clk_core *core,
1124 struct clk_rate_request *req)
1126 lockdep_assert_held(&prepare_lock);
1128 if (!core)
1129 return 0;
1131 clk_core_init_rate_req(core, req);
1133 if (clk_core_can_round(core))
1134 return clk_core_determine_round_nolock(core, req);
1135 else if (core->flags & CLK_SET_RATE_PARENT)
1136 return clk_core_round_rate_nolock(core->parent, req);
1138 req->rate = core->rate;
1139 return 0;
1143 * __clk_determine_rate - get the closest rate actually supported by a clock
1144 * @hw: determine the rate of this clock
1145 * @req: target rate request
1147 * Useful for clk_ops such as .set_rate and .determine_rate.
1149 int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
1151 if (!hw) {
1152 req->rate = 0;
1153 return 0;
1156 return clk_core_round_rate_nolock(hw->core, req);
1158 EXPORT_SYMBOL_GPL(__clk_determine_rate);
1160 unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
1162 int ret;
1163 struct clk_rate_request req;
1165 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
1166 req.rate = rate;
1168 ret = clk_core_round_rate_nolock(hw->core, &req);
1169 if (ret)
1170 return 0;
1172 return req.rate;
1174 EXPORT_SYMBOL_GPL(clk_hw_round_rate);
1177 * clk_round_rate - round the given rate for a clk
1178 * @clk: the clk for which we are rounding a rate
1179 * @rate: the rate which is to be rounded
1181 * Takes in a rate as input and rounds it to a rate that the clk can actually
1182 * use which is then returned. If clk doesn't support round_rate operation
1183 * then the parent rate is returned.
1185 long clk_round_rate(struct clk *clk, unsigned long rate)
1187 struct clk_rate_request req;
1188 int ret;
1190 if (!clk)
1191 return 0;
1193 clk_prepare_lock();
1195 if (clk->exclusive_count)
1196 clk_core_rate_unprotect(clk->core);
1198 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
1199 req.rate = rate;
1201 ret = clk_core_round_rate_nolock(clk->core, &req);
1203 if (clk->exclusive_count)
1204 clk_core_rate_protect(clk->core);
1206 clk_prepare_unlock();
1208 if (ret)
1209 return ret;
1211 return req.rate;
1213 EXPORT_SYMBOL_GPL(clk_round_rate);
1216 * __clk_notify - call clk notifier chain
1217 * @core: clk that is changing rate
1218 * @msg: clk notifier type (see include/linux/clk.h)
1219 * @old_rate: old clk rate
1220 * @new_rate: new clk rate
1222 * Triggers a notifier call chain on the clk rate-change notification
1223 * for 'clk'. Passes a pointer to the struct clk and the previous
1224 * and current rates to the notifier callback. Intended to be called by
1225 * internal clock code only. Returns NOTIFY_DONE from the last driver
1226 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
1227 * a driver returns that.
1229 static int __clk_notify(struct clk_core *core, unsigned long msg,
1230 unsigned long old_rate, unsigned long new_rate)
1232 struct clk_notifier *cn;
1233 struct clk_notifier_data cnd;
1234 int ret = NOTIFY_DONE;
1236 cnd.old_rate = old_rate;
1237 cnd.new_rate = new_rate;
1239 list_for_each_entry(cn, &clk_notifier_list, node) {
1240 if (cn->clk->core == core) {
1241 cnd.clk = cn->clk;
1242 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
1243 &cnd);
1244 if (ret & NOTIFY_STOP_MASK)
1245 return ret;
1249 return ret;
1253 * __clk_recalc_accuracies
1254 * @core: first clk in the subtree
1256 * Walks the subtree of clks starting with clk and recalculates accuracies as
1257 * it goes. Note that if a clk does not implement the .recalc_accuracy
1258 * callback then it is assumed that the clock will take on the accuracy of its
1259 * parent.
1261 static void __clk_recalc_accuracies(struct clk_core *core)
1263 unsigned long parent_accuracy = 0;
1264 struct clk_core *child;
1266 lockdep_assert_held(&prepare_lock);
1268 if (core->parent)
1269 parent_accuracy = core->parent->accuracy;
1271 if (core->ops->recalc_accuracy)
1272 core->accuracy = core->ops->recalc_accuracy(core->hw,
1273 parent_accuracy);
1274 else
1275 core->accuracy = parent_accuracy;
1277 hlist_for_each_entry(child, &core->children, child_node)
1278 __clk_recalc_accuracies(child);
1281 static long clk_core_get_accuracy(struct clk_core *core)
1283 unsigned long accuracy;
1285 clk_prepare_lock();
1286 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1287 __clk_recalc_accuracies(core);
1289 accuracy = __clk_get_accuracy(core);
1290 clk_prepare_unlock();
1292 return accuracy;
1296 * clk_get_accuracy - return the accuracy of clk
1297 * @clk: the clk whose accuracy is being returned
1299 * Simply returns the cached accuracy of the clk, unless
1300 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1301 * issued.
1302 * If clk is NULL then returns 0.
1304 long clk_get_accuracy(struct clk *clk)
1306 if (!clk)
1307 return 0;
1309 return clk_core_get_accuracy(clk->core);
1311 EXPORT_SYMBOL_GPL(clk_get_accuracy);
1313 static unsigned long clk_recalc(struct clk_core *core,
1314 unsigned long parent_rate)
1316 unsigned long rate = parent_rate;
1318 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) {
1319 rate = core->ops->recalc_rate(core->hw, parent_rate);
1320 clk_pm_runtime_put(core);
1322 return rate;
1326 * __clk_recalc_rates
1327 * @core: first clk in the subtree
1328 * @msg: notification type (see include/linux/clk.h)
1330 * Walks the subtree of clks starting with clk and recalculates rates as it
1331 * goes. Note that if a clk does not implement the .recalc_rate callback then
1332 * it is assumed that the clock will take on the rate of its parent.
1334 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1335 * if necessary.
1337 static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
1339 unsigned long old_rate;
1340 unsigned long parent_rate = 0;
1341 struct clk_core *child;
1343 lockdep_assert_held(&prepare_lock);
1345 old_rate = core->rate;
1347 if (core->parent)
1348 parent_rate = core->parent->rate;
1350 core->rate = clk_recalc(core, parent_rate);
1353 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1354 * & ABORT_RATE_CHANGE notifiers
1356 if (core->notifier_count && msg)
1357 __clk_notify(core, msg, old_rate, core->rate);
1359 hlist_for_each_entry(child, &core->children, child_node)
1360 __clk_recalc_rates(child, msg);
1363 static unsigned long clk_core_get_rate(struct clk_core *core)
1365 unsigned long rate;
1367 clk_prepare_lock();
1369 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1370 __clk_recalc_rates(core, 0);
1372 rate = clk_core_get_rate_nolock(core);
1373 clk_prepare_unlock();
1375 return rate;
1379 * clk_get_rate - return the rate of clk
1380 * @clk: the clk whose rate is being returned
1382 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1383 * is set, which means a recalc_rate will be issued.
1384 * If clk is NULL then returns 0.
1386 unsigned long clk_get_rate(struct clk *clk)
1388 if (!clk)
1389 return 0;
1391 return clk_core_get_rate(clk->core);
1393 EXPORT_SYMBOL_GPL(clk_get_rate);
1395 static int clk_fetch_parent_index(struct clk_core *core,
1396 struct clk_core *parent)
1398 int i;
1400 if (!parent)
1401 return -EINVAL;
1403 for (i = 0; i < core->num_parents; i++)
1404 if (clk_core_get_parent_by_index(core, i) == parent)
1405 return i;
1407 return -EINVAL;
1411 * Update the orphan status of @core and all its children.
1413 static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1415 struct clk_core *child;
1417 core->orphan = is_orphan;
1419 hlist_for_each_entry(child, &core->children, child_node)
1420 clk_core_update_orphan_status(child, is_orphan);
1423 static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
1425 bool was_orphan = core->orphan;
1427 hlist_del(&core->child_node);
1429 if (new_parent) {
1430 bool becomes_orphan = new_parent->orphan;
1432 /* avoid duplicate POST_RATE_CHANGE notifications */
1433 if (new_parent->new_child == core)
1434 new_parent->new_child = NULL;
1436 hlist_add_head(&core->child_node, &new_parent->children);
1438 if (was_orphan != becomes_orphan)
1439 clk_core_update_orphan_status(core, becomes_orphan);
1440 } else {
1441 hlist_add_head(&core->child_node, &clk_orphan_list);
1442 if (!was_orphan)
1443 clk_core_update_orphan_status(core, true);
1446 core->parent = new_parent;
1449 static struct clk_core *__clk_set_parent_before(struct clk_core *core,
1450 struct clk_core *parent)
1452 unsigned long flags;
1453 struct clk_core *old_parent = core->parent;
1456 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1458 * 2. Migrate prepare state between parents and prevent race with
1459 * clk_enable().
1461 * If the clock is not prepared, then a race with
1462 * clk_enable/disable() is impossible since we already have the
1463 * prepare lock (future calls to clk_enable() need to be preceded by
1464 * a clk_prepare()).
1466 * If the clock is prepared, migrate the prepared state to the new
1467 * parent and also protect against a race with clk_enable() by
1468 * forcing the clock and the new parent on. This ensures that all
1469 * future calls to clk_enable() are practically NOPs with respect to
1470 * hardware and software states.
1472 * See also: Comment for clk_set_parent() below.
1475 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1476 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1477 clk_core_prepare_enable(old_parent);
1478 clk_core_prepare_enable(parent);
1481 /* migrate prepare count if > 0 */
1482 if (core->prepare_count) {
1483 clk_core_prepare_enable(parent);
1484 clk_core_enable_lock(core);
1487 /* update the clk tree topology */
1488 flags = clk_enable_lock();
1489 clk_reparent(core, parent);
1490 clk_enable_unlock(flags);
1492 return old_parent;
1495 static void __clk_set_parent_after(struct clk_core *core,
1496 struct clk_core *parent,
1497 struct clk_core *old_parent)
1500 * Finish the migration of prepare state and undo the changes done
1501 * for preventing a race with clk_enable().
1503 if (core->prepare_count) {
1504 clk_core_disable_lock(core);
1505 clk_core_disable_unprepare(old_parent);
1508 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1509 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1510 clk_core_disable_unprepare(parent);
1511 clk_core_disable_unprepare(old_parent);
1515 static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
1516 u8 p_index)
1518 unsigned long flags;
1519 int ret = 0;
1520 struct clk_core *old_parent;
1522 old_parent = __clk_set_parent_before(core, parent);
1524 trace_clk_set_parent(core, parent);
1526 /* change clock input source */
1527 if (parent && core->ops->set_parent)
1528 ret = core->ops->set_parent(core->hw, p_index);
1530 trace_clk_set_parent_complete(core, parent);
1532 if (ret) {
1533 flags = clk_enable_lock();
1534 clk_reparent(core, old_parent);
1535 clk_enable_unlock(flags);
1536 __clk_set_parent_after(core, old_parent, parent);
1538 return ret;
1541 __clk_set_parent_after(core, parent, old_parent);
1543 return 0;
1547 * __clk_speculate_rates
1548 * @core: first clk in the subtree
1549 * @parent_rate: the "future" rate of clk's parent
1551 * Walks the subtree of clks starting with clk, speculating rates as it
1552 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1554 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1555 * pre-rate change notifications and returns early if no clks in the
1556 * subtree have subscribed to the notifications. Note that if a clk does not
1557 * implement the .recalc_rate callback then it is assumed that the clock will
1558 * take on the rate of its parent.
1560 static int __clk_speculate_rates(struct clk_core *core,
1561 unsigned long parent_rate)
1563 struct clk_core *child;
1564 unsigned long new_rate;
1565 int ret = NOTIFY_DONE;
1567 lockdep_assert_held(&prepare_lock);
1569 new_rate = clk_recalc(core, parent_rate);
1571 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
1572 if (core->notifier_count)
1573 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
1575 if (ret & NOTIFY_STOP_MASK) {
1576 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
1577 __func__, core->name, ret);
1578 goto out;
1581 hlist_for_each_entry(child, &core->children, child_node) {
1582 ret = __clk_speculate_rates(child, new_rate);
1583 if (ret & NOTIFY_STOP_MASK)
1584 break;
1587 out:
1588 return ret;
1591 static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
1592 struct clk_core *new_parent, u8 p_index)
1594 struct clk_core *child;
1596 core->new_rate = new_rate;
1597 core->new_parent = new_parent;
1598 core->new_parent_index = p_index;
1599 /* include clk in new parent's PRE_RATE_CHANGE notifications */
1600 core->new_child = NULL;
1601 if (new_parent && new_parent != core->parent)
1602 new_parent->new_child = core;
1604 hlist_for_each_entry(child, &core->children, child_node) {
1605 child->new_rate = clk_recalc(child, new_rate);
1606 clk_calc_subtree(child, child->new_rate, NULL, 0);
1611 * calculate the new rates returning the topmost clock that has to be
1612 * changed.
1614 static struct clk_core *clk_calc_new_rates(struct clk_core *core,
1615 unsigned long rate)
1617 struct clk_core *top = core;
1618 struct clk_core *old_parent, *parent;
1619 unsigned long best_parent_rate = 0;
1620 unsigned long new_rate;
1621 unsigned long min_rate;
1622 unsigned long max_rate;
1623 int p_index = 0;
1624 long ret;
1626 /* sanity */
1627 if (IS_ERR_OR_NULL(core))
1628 return NULL;
1630 /* save parent rate, if it exists */
1631 parent = old_parent = core->parent;
1632 if (parent)
1633 best_parent_rate = parent->rate;
1635 clk_core_get_boundaries(core, &min_rate, &max_rate);
1637 /* find the closest rate and parent clk/rate */
1638 if (clk_core_can_round(core)) {
1639 struct clk_rate_request req;
1641 req.rate = rate;
1642 req.min_rate = min_rate;
1643 req.max_rate = max_rate;
1645 clk_core_init_rate_req(core, &req);
1647 ret = clk_core_determine_round_nolock(core, &req);
1648 if (ret < 0)
1649 return NULL;
1651 best_parent_rate = req.best_parent_rate;
1652 new_rate = req.rate;
1653 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
1655 if (new_rate < min_rate || new_rate > max_rate)
1656 return NULL;
1657 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
1658 /* pass-through clock without adjustable parent */
1659 core->new_rate = core->rate;
1660 return NULL;
1661 } else {
1662 /* pass-through clock with adjustable parent */
1663 top = clk_calc_new_rates(parent, rate);
1664 new_rate = parent->new_rate;
1665 goto out;
1668 /* some clocks must be gated to change parent */
1669 if (parent != old_parent &&
1670 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
1671 pr_debug("%s: %s not gated but wants to reparent\n",
1672 __func__, core->name);
1673 return NULL;
1676 /* try finding the new parent index */
1677 if (parent && core->num_parents > 1) {
1678 p_index = clk_fetch_parent_index(core, parent);
1679 if (p_index < 0) {
1680 pr_debug("%s: clk %s can not be parent of clk %s\n",
1681 __func__, parent->name, core->name);
1682 return NULL;
1686 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
1687 best_parent_rate != parent->rate)
1688 top = clk_calc_new_rates(parent, best_parent_rate);
1690 out:
1691 clk_calc_subtree(core, new_rate, parent, p_index);
1693 return top;
1697 * Notify about rate changes in a subtree. Always walk down the whole tree
1698 * so that in case of an error we can walk down the whole tree again and
1699 * abort the change.
1701 static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
1702 unsigned long event)
1704 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
1705 int ret = NOTIFY_DONE;
1707 if (core->rate == core->new_rate)
1708 return NULL;
1710 if (core->notifier_count) {
1711 ret = __clk_notify(core, event, core->rate, core->new_rate);
1712 if (ret & NOTIFY_STOP_MASK)
1713 fail_clk = core;
1716 hlist_for_each_entry(child, &core->children, child_node) {
1717 /* Skip children who will be reparented to another clock */
1718 if (child->new_parent && child->new_parent != core)
1719 continue;
1720 tmp_clk = clk_propagate_rate_change(child, event);
1721 if (tmp_clk)
1722 fail_clk = tmp_clk;
1725 /* handle the new child who might not be in core->children yet */
1726 if (core->new_child) {
1727 tmp_clk = clk_propagate_rate_change(core->new_child, event);
1728 if (tmp_clk)
1729 fail_clk = tmp_clk;
1732 return fail_clk;
1736 * walk down a subtree and set the new rates notifying the rate
1737 * change on the way
1739 static void clk_change_rate(struct clk_core *core)
1741 struct clk_core *child;
1742 struct hlist_node *tmp;
1743 unsigned long old_rate;
1744 unsigned long best_parent_rate = 0;
1745 bool skip_set_rate = false;
1746 struct clk_core *old_parent;
1747 struct clk_core *parent = NULL;
1749 old_rate = core->rate;
1751 if (core->new_parent) {
1752 parent = core->new_parent;
1753 best_parent_rate = core->new_parent->rate;
1754 } else if (core->parent) {
1755 parent = core->parent;
1756 best_parent_rate = core->parent->rate;
1759 if (clk_pm_runtime_get(core))
1760 return;
1762 if (core->flags & CLK_SET_RATE_UNGATE) {
1763 unsigned long flags;
1765 clk_core_prepare(core);
1766 flags = clk_enable_lock();
1767 clk_core_enable(core);
1768 clk_enable_unlock(flags);
1771 if (core->new_parent && core->new_parent != core->parent) {
1772 old_parent = __clk_set_parent_before(core, core->new_parent);
1773 trace_clk_set_parent(core, core->new_parent);
1775 if (core->ops->set_rate_and_parent) {
1776 skip_set_rate = true;
1777 core->ops->set_rate_and_parent(core->hw, core->new_rate,
1778 best_parent_rate,
1779 core->new_parent_index);
1780 } else if (core->ops->set_parent) {
1781 core->ops->set_parent(core->hw, core->new_parent_index);
1784 trace_clk_set_parent_complete(core, core->new_parent);
1785 __clk_set_parent_after(core, core->new_parent, old_parent);
1788 if (core->flags & CLK_OPS_PARENT_ENABLE)
1789 clk_core_prepare_enable(parent);
1791 trace_clk_set_rate(core, core->new_rate);
1793 if (!skip_set_rate && core->ops->set_rate)
1794 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
1796 trace_clk_set_rate_complete(core, core->new_rate);
1798 core->rate = clk_recalc(core, best_parent_rate);
1800 if (core->flags & CLK_SET_RATE_UNGATE) {
1801 unsigned long flags;
1803 flags = clk_enable_lock();
1804 clk_core_disable(core);
1805 clk_enable_unlock(flags);
1806 clk_core_unprepare(core);
1809 if (core->flags & CLK_OPS_PARENT_ENABLE)
1810 clk_core_disable_unprepare(parent);
1812 if (core->notifier_count && old_rate != core->rate)
1813 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
1815 if (core->flags & CLK_RECALC_NEW_RATES)
1816 (void)clk_calc_new_rates(core, core->new_rate);
1819 * Use safe iteration, as change_rate can actually swap parents
1820 * for certain clock types.
1822 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
1823 /* Skip children who will be reparented to another clock */
1824 if (child->new_parent && child->new_parent != core)
1825 continue;
1826 clk_change_rate(child);
1829 /* handle the new child who might not be in core->children yet */
1830 if (core->new_child)
1831 clk_change_rate(core->new_child);
1833 clk_pm_runtime_put(core);
1836 static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
1837 unsigned long req_rate)
1839 int ret, cnt;
1840 struct clk_rate_request req;
1842 lockdep_assert_held(&prepare_lock);
1844 if (!core)
1845 return 0;
1847 /* simulate what the rate would be if it could be freely set */
1848 cnt = clk_core_rate_nuke_protect(core);
1849 if (cnt < 0)
1850 return cnt;
1852 clk_core_get_boundaries(core, &req.min_rate, &req.max_rate);
1853 req.rate = req_rate;
1855 ret = clk_core_round_rate_nolock(core, &req);
1857 /* restore the protection */
1858 clk_core_rate_restore_protect(core, cnt);
1860 return ret ? 0 : req.rate;
1863 static int clk_core_set_rate_nolock(struct clk_core *core,
1864 unsigned long req_rate)
1866 struct clk_core *top, *fail_clk;
1867 unsigned long rate;
1868 int ret = 0;
1870 if (!core)
1871 return 0;
1873 rate = clk_core_req_round_rate_nolock(core, req_rate);
1875 /* bail early if nothing to do */
1876 if (rate == clk_core_get_rate_nolock(core))
1877 return 0;
1879 /* fail on a direct rate set of a protected provider */
1880 if (clk_core_rate_is_protected(core))
1881 return -EBUSY;
1883 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
1884 return -EBUSY;
1886 /* calculate new rates and get the topmost changed clock */
1887 top = clk_calc_new_rates(core, req_rate);
1888 if (!top)
1889 return -EINVAL;
1891 ret = clk_pm_runtime_get(core);
1892 if (ret)
1893 return ret;
1895 /* notify that we are about to change rates */
1896 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1897 if (fail_clk) {
1898 pr_debug("%s: failed to set %s rate\n", __func__,
1899 fail_clk->name);
1900 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1901 ret = -EBUSY;
1902 goto err;
1905 /* change the rates */
1906 clk_change_rate(top);
1908 core->req_rate = req_rate;
1909 err:
1910 clk_pm_runtime_put(core);
1912 return ret;
1916 * clk_set_rate - specify a new rate for clk
1917 * @clk: the clk whose rate is being changed
1918 * @rate: the new rate for clk
1920 * In the simplest case clk_set_rate will only adjust the rate of clk.
1922 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1923 * propagate up to clk's parent; whether or not this happens depends on the
1924 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1925 * after calling .round_rate then upstream parent propagation is ignored. If
1926 * *parent_rate comes back with a new rate for clk's parent then we propagate
1927 * up to clk's parent and set its rate. Upward propagation will continue
1928 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1929 * .round_rate stops requesting changes to clk's parent_rate.
1931 * Rate changes are accomplished via tree traversal that also recalculates the
1932 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
1934 * Returns 0 on success, -EERROR otherwise.
1936 int clk_set_rate(struct clk *clk, unsigned long rate)
1938 int ret;
1940 if (!clk)
1941 return 0;
1943 /* prevent racing with updates to the clock topology */
1944 clk_prepare_lock();
1946 if (clk->exclusive_count)
1947 clk_core_rate_unprotect(clk->core);
1949 ret = clk_core_set_rate_nolock(clk->core, rate);
1951 if (clk->exclusive_count)
1952 clk_core_rate_protect(clk->core);
1954 clk_prepare_unlock();
1956 return ret;
1958 EXPORT_SYMBOL_GPL(clk_set_rate);
1961 * clk_set_rate_exclusive - specify a new rate get exclusive control
1962 * @clk: the clk whose rate is being changed
1963 * @rate: the new rate for clk
1965 * This is a combination of clk_set_rate() and clk_rate_exclusive_get()
1966 * within a critical section
1968 * This can be used initially to ensure that at least 1 consumer is
1969 * statisfied when several consumers are competing for exclusivity over the
1970 * same clock provider.
1972 * The exclusivity is not applied if setting the rate failed.
1974 * Calls to clk_rate_exclusive_get() should be balanced with calls to
1975 * clk_rate_exclusive_put().
1977 * Returns 0 on success, -EERROR otherwise.
1979 int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
1981 int ret;
1983 if (!clk)
1984 return 0;
1986 /* prevent racing with updates to the clock topology */
1987 clk_prepare_lock();
1990 * The temporary protection removal is not here, on purpose
1991 * This function is meant to be used instead of clk_rate_protect,
1992 * so before the consumer code path protect the clock provider
1995 ret = clk_core_set_rate_nolock(clk->core, rate);
1996 if (!ret) {
1997 clk_core_rate_protect(clk->core);
1998 clk->exclusive_count++;
2001 clk_prepare_unlock();
2003 return ret;
2005 EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
2008 * clk_set_rate_range - set a rate range for a clock source
2009 * @clk: clock source
2010 * @min: desired minimum clock rate in Hz, inclusive
2011 * @max: desired maximum clock rate in Hz, inclusive
2013 * Returns success (0) or negative errno.
2015 int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
2017 int ret = 0;
2018 unsigned long old_min, old_max, rate;
2020 if (!clk)
2021 return 0;
2023 if (min > max) {
2024 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
2025 __func__, clk->core->name, clk->dev_id, clk->con_id,
2026 min, max);
2027 return -EINVAL;
2030 clk_prepare_lock();
2032 if (clk->exclusive_count)
2033 clk_core_rate_unprotect(clk->core);
2035 /* Save the current values in case we need to rollback the change */
2036 old_min = clk->min_rate;
2037 old_max = clk->max_rate;
2038 clk->min_rate = min;
2039 clk->max_rate = max;
2041 rate = clk_core_get_rate_nolock(clk->core);
2042 if (rate < min || rate > max) {
2044 * FIXME:
2045 * We are in bit of trouble here, current rate is outside the
2046 * the requested range. We are going try to request appropriate
2047 * range boundary but there is a catch. It may fail for the
2048 * usual reason (clock broken, clock protected, etc) but also
2049 * because:
2050 * - round_rate() was not favorable and fell on the wrong
2051 * side of the boundary
2052 * - the determine_rate() callback does not really check for
2053 * this corner case when determining the rate
2056 if (rate < min)
2057 rate = min;
2058 else
2059 rate = max;
2061 ret = clk_core_set_rate_nolock(clk->core, rate);
2062 if (ret) {
2063 /* rollback the changes */
2064 clk->min_rate = old_min;
2065 clk->max_rate = old_max;
2069 if (clk->exclusive_count)
2070 clk_core_rate_protect(clk->core);
2072 clk_prepare_unlock();
2074 return ret;
2076 EXPORT_SYMBOL_GPL(clk_set_rate_range);
2079 * clk_set_min_rate - set a minimum clock rate for a clock source
2080 * @clk: clock source
2081 * @rate: desired minimum clock rate in Hz, inclusive
2083 * Returns success (0) or negative errno.
2085 int clk_set_min_rate(struct clk *clk, unsigned long rate)
2087 if (!clk)
2088 return 0;
2090 return clk_set_rate_range(clk, rate, clk->max_rate);
2092 EXPORT_SYMBOL_GPL(clk_set_min_rate);
2095 * clk_set_max_rate - set a maximum clock rate for a clock source
2096 * @clk: clock source
2097 * @rate: desired maximum clock rate in Hz, inclusive
2099 * Returns success (0) or negative errno.
2101 int clk_set_max_rate(struct clk *clk, unsigned long rate)
2103 if (!clk)
2104 return 0;
2106 return clk_set_rate_range(clk, clk->min_rate, rate);
2108 EXPORT_SYMBOL_GPL(clk_set_max_rate);
2111 * clk_get_parent - return the parent of a clk
2112 * @clk: the clk whose parent gets returned
2114 * Simply returns clk->parent. Returns NULL if clk is NULL.
2116 struct clk *clk_get_parent(struct clk *clk)
2118 struct clk *parent;
2120 if (!clk)
2121 return NULL;
2123 clk_prepare_lock();
2124 /* TODO: Create a per-user clk and change callers to call clk_put */
2125 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
2126 clk_prepare_unlock();
2128 return parent;
2130 EXPORT_SYMBOL_GPL(clk_get_parent);
2132 static struct clk_core *__clk_init_parent(struct clk_core *core)
2134 u8 index = 0;
2136 if (core->num_parents > 1 && core->ops->get_parent)
2137 index = core->ops->get_parent(core->hw);
2139 return clk_core_get_parent_by_index(core, index);
2142 static void clk_core_reparent(struct clk_core *core,
2143 struct clk_core *new_parent)
2145 clk_reparent(core, new_parent);
2146 __clk_recalc_accuracies(core);
2147 __clk_recalc_rates(core, POST_RATE_CHANGE);
2150 void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
2152 if (!hw)
2153 return;
2155 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
2159 * clk_has_parent - check if a clock is a possible parent for another
2160 * @clk: clock source
2161 * @parent: parent clock source
2163 * This function can be used in drivers that need to check that a clock can be
2164 * the parent of another without actually changing the parent.
2166 * Returns true if @parent is a possible parent for @clk, false otherwise.
2168 bool clk_has_parent(struct clk *clk, struct clk *parent)
2170 struct clk_core *core, *parent_core;
2171 unsigned int i;
2173 /* NULL clocks should be nops, so return success if either is NULL. */
2174 if (!clk || !parent)
2175 return true;
2177 core = clk->core;
2178 parent_core = parent->core;
2180 /* Optimize for the case where the parent is already the parent. */
2181 if (core->parent == parent_core)
2182 return true;
2184 for (i = 0; i < core->num_parents; i++)
2185 if (strcmp(core->parent_names[i], parent_core->name) == 0)
2186 return true;
2188 return false;
2190 EXPORT_SYMBOL_GPL(clk_has_parent);
2192 static int clk_core_set_parent_nolock(struct clk_core *core,
2193 struct clk_core *parent)
2195 int ret = 0;
2196 int p_index = 0;
2197 unsigned long p_rate = 0;
2199 lockdep_assert_held(&prepare_lock);
2201 if (!core)
2202 return 0;
2204 if (core->parent == parent)
2205 return 0;
2207 /* verify ops for for multi-parent clks */
2208 if (core->num_parents > 1 && !core->ops->set_parent)
2209 return -EPERM;
2211 /* check that we are allowed to re-parent if the clock is in use */
2212 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count)
2213 return -EBUSY;
2215 if (clk_core_rate_is_protected(core))
2216 return -EBUSY;
2218 /* try finding the new parent index */
2219 if (parent) {
2220 p_index = clk_fetch_parent_index(core, parent);
2221 if (p_index < 0) {
2222 pr_debug("%s: clk %s can not be parent of clk %s\n",
2223 __func__, parent->name, core->name);
2224 return p_index;
2226 p_rate = parent->rate;
2229 ret = clk_pm_runtime_get(core);
2230 if (ret)
2231 return ret;
2233 /* propagate PRE_RATE_CHANGE notifications */
2234 ret = __clk_speculate_rates(core, p_rate);
2236 /* abort if a driver objects */
2237 if (ret & NOTIFY_STOP_MASK)
2238 goto runtime_put;
2240 /* do the re-parent */
2241 ret = __clk_set_parent(core, parent, p_index);
2243 /* propagate rate an accuracy recalculation accordingly */
2244 if (ret) {
2245 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
2246 } else {
2247 __clk_recalc_rates(core, POST_RATE_CHANGE);
2248 __clk_recalc_accuracies(core);
2251 runtime_put:
2252 clk_pm_runtime_put(core);
2254 return ret;
2258 * clk_set_parent - switch the parent of a mux clk
2259 * @clk: the mux clk whose input we are switching
2260 * @parent: the new input to clk
2262 * Re-parent clk to use parent as its new input source. If clk is in
2263 * prepared state, the clk will get enabled for the duration of this call. If
2264 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2265 * that, the reparenting is glitchy in hardware, etc), use the
2266 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2268 * After successfully changing clk's parent clk_set_parent will update the
2269 * clk topology, sysfs topology and propagate rate recalculation via
2270 * __clk_recalc_rates.
2272 * Returns 0 on success, -EERROR otherwise.
2274 int clk_set_parent(struct clk *clk, struct clk *parent)
2276 int ret;
2278 if (!clk)
2279 return 0;
2281 clk_prepare_lock();
2283 if (clk->exclusive_count)
2284 clk_core_rate_unprotect(clk->core);
2286 ret = clk_core_set_parent_nolock(clk->core,
2287 parent ? parent->core : NULL);
2289 if (clk->exclusive_count)
2290 clk_core_rate_protect(clk->core);
2292 clk_prepare_unlock();
2294 return ret;
2296 EXPORT_SYMBOL_GPL(clk_set_parent);
2298 static int clk_core_set_phase_nolock(struct clk_core *core, int degrees)
2300 int ret = -EINVAL;
2302 lockdep_assert_held(&prepare_lock);
2304 if (!core)
2305 return 0;
2307 if (clk_core_rate_is_protected(core))
2308 return -EBUSY;
2310 trace_clk_set_phase(core, degrees);
2312 if (core->ops->set_phase)
2313 ret = core->ops->set_phase(core->hw, degrees);
2315 trace_clk_set_phase_complete(core, degrees);
2317 return ret;
2321 * clk_set_phase - adjust the phase shift of a clock signal
2322 * @clk: clock signal source
2323 * @degrees: number of degrees the signal is shifted
2325 * Shifts the phase of a clock signal by the specified
2326 * degrees. Returns 0 on success, -EERROR otherwise.
2328 * This function makes no distinction about the input or reference
2329 * signal that we adjust the clock signal phase against. For example
2330 * phase locked-loop clock signal generators we may shift phase with
2331 * respect to feedback clock signal input, but for other cases the
2332 * clock phase may be shifted with respect to some other, unspecified
2333 * signal.
2335 * Additionally the concept of phase shift does not propagate through
2336 * the clock tree hierarchy, which sets it apart from clock rates and
2337 * clock accuracy. A parent clock phase attribute does not have an
2338 * impact on the phase attribute of a child clock.
2340 int clk_set_phase(struct clk *clk, int degrees)
2342 int ret;
2344 if (!clk)
2345 return 0;
2347 /* sanity check degrees */
2348 degrees %= 360;
2349 if (degrees < 0)
2350 degrees += 360;
2352 clk_prepare_lock();
2354 if (clk->exclusive_count)
2355 clk_core_rate_unprotect(clk->core);
2357 ret = clk_core_set_phase_nolock(clk->core, degrees);
2359 if (clk->exclusive_count)
2360 clk_core_rate_protect(clk->core);
2362 clk_prepare_unlock();
2364 return ret;
2366 EXPORT_SYMBOL_GPL(clk_set_phase);
2368 static int clk_core_get_phase(struct clk_core *core)
2370 int ret;
2372 clk_prepare_lock();
2373 ret = core->phase;
2374 clk_prepare_unlock();
2376 return ret;
2380 * clk_get_phase - return the phase shift of a clock signal
2381 * @clk: clock signal source
2383 * Returns the phase shift of a clock node in degrees, otherwise returns
2384 * -EERROR.
2386 int clk_get_phase(struct clk *clk)
2388 if (!clk)
2389 return 0;
2391 return clk_core_get_phase(clk->core);
2393 EXPORT_SYMBOL_GPL(clk_get_phase);
2396 * clk_is_match - check if two clk's point to the same hardware clock
2397 * @p: clk compared against q
2398 * @q: clk compared against p
2400 * Returns true if the two struct clk pointers both point to the same hardware
2401 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2402 * share the same struct clk_core object.
2404 * Returns false otherwise. Note that two NULL clks are treated as matching.
2406 bool clk_is_match(const struct clk *p, const struct clk *q)
2408 /* trivial case: identical struct clk's or both NULL */
2409 if (p == q)
2410 return true;
2412 /* true if clk->core pointers match. Avoid dereferencing garbage */
2413 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
2414 if (p->core == q->core)
2415 return true;
2417 return false;
2419 EXPORT_SYMBOL_GPL(clk_is_match);
2421 /*** debugfs support ***/
2423 #ifdef CONFIG_DEBUG_FS
2424 #include <linux/debugfs.h>
2426 static struct dentry *rootdir;
2427 static int inited = 0;
2428 static DEFINE_MUTEX(clk_debug_lock);
2429 static HLIST_HEAD(clk_debug_list);
2431 static struct hlist_head *all_lists[] = {
2432 &clk_root_list,
2433 &clk_orphan_list,
2434 NULL,
2437 static struct hlist_head *orphan_list[] = {
2438 &clk_orphan_list,
2439 NULL,
2442 static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2443 int level)
2445 if (!c)
2446 return;
2448 seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %-3d\n",
2449 level * 3 + 1, "",
2450 30 - level * 3, c->name,
2451 c->enable_count, c->prepare_count, c->protect_count,
2452 clk_core_get_rate(c), clk_core_get_accuracy(c),
2453 clk_core_get_phase(c));
2456 static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2457 int level)
2459 struct clk_core *child;
2461 if (!c)
2462 return;
2464 clk_summary_show_one(s, c, level);
2466 hlist_for_each_entry(child, &c->children, child_node)
2467 clk_summary_show_subtree(s, child, level + 1);
2470 static int clk_summary_show(struct seq_file *s, void *data)
2472 struct clk_core *c;
2473 struct hlist_head **lists = (struct hlist_head **)s->private;
2475 seq_puts(s, " enable prepare protect \n");
2476 seq_puts(s, " clock count count count rate accuracy phase\n");
2477 seq_puts(s, "----------------------------------------------------------------------------------------\n");
2479 clk_prepare_lock();
2481 for (; *lists; lists++)
2482 hlist_for_each_entry(c, *lists, child_node)
2483 clk_summary_show_subtree(s, c, 0);
2485 clk_prepare_unlock();
2487 return 0;
2491 static int clk_summary_open(struct inode *inode, struct file *file)
2493 return single_open(file, clk_summary_show, inode->i_private);
2496 static const struct file_operations clk_summary_fops = {
2497 .open = clk_summary_open,
2498 .read = seq_read,
2499 .llseek = seq_lseek,
2500 .release = single_release,
2503 static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2505 if (!c)
2506 return;
2508 /* This should be JSON format, i.e. elements separated with a comma */
2509 seq_printf(s, "\"%s\": { ", c->name);
2510 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2511 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
2512 seq_printf(s, "\"protect_count\": %d,", c->protect_count);
2513 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2514 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
2515 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
2518 static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2520 struct clk_core *child;
2522 if (!c)
2523 return;
2525 clk_dump_one(s, c, level);
2527 hlist_for_each_entry(child, &c->children, child_node) {
2528 seq_putc(s, ',');
2529 clk_dump_subtree(s, child, level + 1);
2532 seq_putc(s, '}');
2535 static int clk_dump(struct seq_file *s, void *data)
2537 struct clk_core *c;
2538 bool first_node = true;
2539 struct hlist_head **lists = (struct hlist_head **)s->private;
2541 seq_putc(s, '{');
2542 clk_prepare_lock();
2544 for (; *lists; lists++) {
2545 hlist_for_each_entry(c, *lists, child_node) {
2546 if (!first_node)
2547 seq_putc(s, ',');
2548 first_node = false;
2549 clk_dump_subtree(s, c, 0);
2553 clk_prepare_unlock();
2555 seq_puts(s, "}\n");
2556 return 0;
2560 static int clk_dump_open(struct inode *inode, struct file *file)
2562 return single_open(file, clk_dump, inode->i_private);
2565 static const struct file_operations clk_dump_fops = {
2566 .open = clk_dump_open,
2567 .read = seq_read,
2568 .llseek = seq_lseek,
2569 .release = single_release,
2572 static const struct {
2573 unsigned long flag;
2574 const char *name;
2575 } clk_flags[] = {
2576 #define ENTRY(f) { f, __stringify(f) }
2577 ENTRY(CLK_SET_RATE_GATE),
2578 ENTRY(CLK_SET_PARENT_GATE),
2579 ENTRY(CLK_SET_RATE_PARENT),
2580 ENTRY(CLK_IGNORE_UNUSED),
2581 ENTRY(CLK_IS_BASIC),
2582 ENTRY(CLK_GET_RATE_NOCACHE),
2583 ENTRY(CLK_SET_RATE_NO_REPARENT),
2584 ENTRY(CLK_GET_ACCURACY_NOCACHE),
2585 ENTRY(CLK_RECALC_NEW_RATES),
2586 ENTRY(CLK_SET_RATE_UNGATE),
2587 ENTRY(CLK_IS_CRITICAL),
2588 ENTRY(CLK_OPS_PARENT_ENABLE),
2589 #undef ENTRY
2592 static int clk_flags_dump(struct seq_file *s, void *data)
2594 struct clk_core *core = s->private;
2595 unsigned long flags = core->flags;
2596 unsigned int i;
2598 for (i = 0; flags && i < ARRAY_SIZE(clk_flags); i++) {
2599 if (flags & clk_flags[i].flag) {
2600 seq_printf(s, "%s\n", clk_flags[i].name);
2601 flags &= ~clk_flags[i].flag;
2604 if (flags) {
2605 /* Unknown flags */
2606 seq_printf(s, "0x%lx\n", flags);
2609 return 0;
2612 static int clk_flags_open(struct inode *inode, struct file *file)
2614 return single_open(file, clk_flags_dump, inode->i_private);
2617 static const struct file_operations clk_flags_fops = {
2618 .open = clk_flags_open,
2619 .read = seq_read,
2620 .llseek = seq_lseek,
2621 .release = single_release,
2624 static int possible_parents_dump(struct seq_file *s, void *data)
2626 struct clk_core *core = s->private;
2627 int i;
2629 for (i = 0; i < core->num_parents - 1; i++)
2630 seq_printf(s, "%s ", core->parent_names[i]);
2632 seq_printf(s, "%s\n", core->parent_names[i]);
2634 return 0;
2637 static int possible_parents_open(struct inode *inode, struct file *file)
2639 return single_open(file, possible_parents_dump, inode->i_private);
2642 static const struct file_operations possible_parents_fops = {
2643 .open = possible_parents_open,
2644 .read = seq_read,
2645 .llseek = seq_lseek,
2646 .release = single_release,
2649 static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2651 struct dentry *d;
2652 int ret = -ENOMEM;
2654 if (!core || !pdentry) {
2655 ret = -EINVAL;
2656 goto out;
2659 d = debugfs_create_dir(core->name, pdentry);
2660 if (!d)
2661 goto out;
2663 core->dentry = d;
2665 d = debugfs_create_ulong("clk_rate", 0444, core->dentry, &core->rate);
2666 if (!d)
2667 goto err_out;
2669 d = debugfs_create_ulong("clk_accuracy", 0444, core->dentry,
2670 &core->accuracy);
2671 if (!d)
2672 goto err_out;
2674 d = debugfs_create_u32("clk_phase", 0444, core->dentry, &core->phase);
2675 if (!d)
2676 goto err_out;
2678 d = debugfs_create_file("clk_flags", 0444, core->dentry, core,
2679 &clk_flags_fops);
2680 if (!d)
2681 goto err_out;
2683 d = debugfs_create_u32("clk_prepare_count", 0444, core->dentry,
2684 &core->prepare_count);
2685 if (!d)
2686 goto err_out;
2688 d = debugfs_create_u32("clk_enable_count", 0444, core->dentry,
2689 &core->enable_count);
2690 if (!d)
2691 goto err_out;
2693 d = debugfs_create_u32("clk_protect_count", 0444, core->dentry,
2694 &core->protect_count);
2695 if (!d)
2696 goto err_out;
2698 d = debugfs_create_u32("clk_notifier_count", 0444, core->dentry,
2699 &core->notifier_count);
2700 if (!d)
2701 goto err_out;
2703 if (core->num_parents > 1) {
2704 d = debugfs_create_file("clk_possible_parents", 0444,
2705 core->dentry, core, &possible_parents_fops);
2706 if (!d)
2707 goto err_out;
2710 if (core->ops->debug_init) {
2711 ret = core->ops->debug_init(core->hw, core->dentry);
2712 if (ret)
2713 goto err_out;
2716 ret = 0;
2717 goto out;
2719 err_out:
2720 debugfs_remove_recursive(core->dentry);
2721 core->dentry = NULL;
2722 out:
2723 return ret;
2727 * clk_debug_register - add a clk node to the debugfs clk directory
2728 * @core: the clk being added to the debugfs clk directory
2730 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2731 * initialized. Otherwise it bails out early since the debugfs clk directory
2732 * will be created lazily by clk_debug_init as part of a late_initcall.
2734 static int clk_debug_register(struct clk_core *core)
2736 int ret = 0;
2738 mutex_lock(&clk_debug_lock);
2739 hlist_add_head(&core->debug_node, &clk_debug_list);
2740 if (inited)
2741 ret = clk_debug_create_one(core, rootdir);
2742 mutex_unlock(&clk_debug_lock);
2744 return ret;
2748 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2749 * @core: the clk being removed from the debugfs clk directory
2751 * Dynamically removes a clk and all its child nodes from the
2752 * debugfs clk directory if clk->dentry points to debugfs created by
2753 * clk_debug_register in __clk_core_init.
2755 static void clk_debug_unregister(struct clk_core *core)
2757 mutex_lock(&clk_debug_lock);
2758 hlist_del_init(&core->debug_node);
2759 debugfs_remove_recursive(core->dentry);
2760 core->dentry = NULL;
2761 mutex_unlock(&clk_debug_lock);
2764 struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
2765 void *data, const struct file_operations *fops)
2767 struct dentry *d = NULL;
2769 if (hw->core->dentry)
2770 d = debugfs_create_file(name, mode, hw->core->dentry, data,
2771 fops);
2773 return d;
2775 EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
2778 * clk_debug_init - lazily populate the debugfs clk directory
2780 * clks are often initialized very early during boot before memory can be
2781 * dynamically allocated and well before debugfs is setup. This function
2782 * populates the debugfs clk directory once at boot-time when we know that
2783 * debugfs is setup. It should only be called once at boot-time, all other clks
2784 * added dynamically will be done so with clk_debug_register.
2786 static int __init clk_debug_init(void)
2788 struct clk_core *core;
2789 struct dentry *d;
2791 rootdir = debugfs_create_dir("clk", NULL);
2793 if (!rootdir)
2794 return -ENOMEM;
2796 d = debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
2797 &clk_summary_fops);
2798 if (!d)
2799 return -ENOMEM;
2801 d = debugfs_create_file("clk_dump", 0444, rootdir, &all_lists,
2802 &clk_dump_fops);
2803 if (!d)
2804 return -ENOMEM;
2806 d = debugfs_create_file("clk_orphan_summary", 0444, rootdir,
2807 &orphan_list, &clk_summary_fops);
2808 if (!d)
2809 return -ENOMEM;
2811 d = debugfs_create_file("clk_orphan_dump", 0444, rootdir,
2812 &orphan_list, &clk_dump_fops);
2813 if (!d)
2814 return -ENOMEM;
2816 mutex_lock(&clk_debug_lock);
2817 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2818 clk_debug_create_one(core, rootdir);
2820 inited = 1;
2821 mutex_unlock(&clk_debug_lock);
2823 return 0;
2825 late_initcall(clk_debug_init);
2826 #else
2827 static inline int clk_debug_register(struct clk_core *core) { return 0; }
2828 static inline void clk_debug_reparent(struct clk_core *core,
2829 struct clk_core *new_parent)
2832 static inline void clk_debug_unregister(struct clk_core *core)
2835 #endif
2838 * __clk_core_init - initialize the data structures in a struct clk_core
2839 * @core: clk_core being initialized
2841 * Initializes the lists in struct clk_core, queries the hardware for the
2842 * parent and rate and sets them both.
2844 static int __clk_core_init(struct clk_core *core)
2846 int i, ret;
2847 struct clk_core *orphan;
2848 struct hlist_node *tmp2;
2849 unsigned long rate;
2851 if (!core)
2852 return -EINVAL;
2854 clk_prepare_lock();
2856 ret = clk_pm_runtime_get(core);
2857 if (ret)
2858 goto unlock;
2860 /* check to see if a clock with this name is already registered */
2861 if (clk_core_lookup(core->name)) {
2862 pr_debug("%s: clk %s already initialized\n",
2863 __func__, core->name);
2864 ret = -EEXIST;
2865 goto out;
2868 /* check that clk_ops are sane. See Documentation/clk.txt */
2869 if (core->ops->set_rate &&
2870 !((core->ops->round_rate || core->ops->determine_rate) &&
2871 core->ops->recalc_rate)) {
2872 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
2873 __func__, core->name);
2874 ret = -EINVAL;
2875 goto out;
2878 if (core->ops->set_parent && !core->ops->get_parent) {
2879 pr_err("%s: %s must implement .get_parent & .set_parent\n",
2880 __func__, core->name);
2881 ret = -EINVAL;
2882 goto out;
2885 if (core->num_parents > 1 && !core->ops->get_parent) {
2886 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
2887 __func__, core->name);
2888 ret = -EINVAL;
2889 goto out;
2892 if (core->ops->set_rate_and_parent &&
2893 !(core->ops->set_parent && core->ops->set_rate)) {
2894 pr_err("%s: %s must implement .set_parent & .set_rate\n",
2895 __func__, core->name);
2896 ret = -EINVAL;
2897 goto out;
2900 /* throw a WARN if any entries in parent_names are NULL */
2901 for (i = 0; i < core->num_parents; i++)
2902 WARN(!core->parent_names[i],
2903 "%s: invalid NULL in %s's .parent_names\n",
2904 __func__, core->name);
2906 core->parent = __clk_init_parent(core);
2909 * Populate core->parent if parent has already been clk_core_init'd. If
2910 * parent has not yet been clk_core_init'd then place clk in the orphan
2911 * list. If clk doesn't have any parents then place it in the root
2912 * clk list.
2914 * Every time a new clk is clk_init'd then we walk the list of orphan
2915 * clocks and re-parent any that are children of the clock currently
2916 * being clk_init'd.
2918 if (core->parent) {
2919 hlist_add_head(&core->child_node,
2920 &core->parent->children);
2921 core->orphan = core->parent->orphan;
2922 } else if (!core->num_parents) {
2923 hlist_add_head(&core->child_node, &clk_root_list);
2924 core->orphan = false;
2925 } else {
2926 hlist_add_head(&core->child_node, &clk_orphan_list);
2927 core->orphan = true;
2931 * Set clk's accuracy. The preferred method is to use
2932 * .recalc_accuracy. For simple clocks and lazy developers the default
2933 * fallback is to use the parent's accuracy. If a clock doesn't have a
2934 * parent (or is orphaned) then accuracy is set to zero (perfect
2935 * clock).
2937 if (core->ops->recalc_accuracy)
2938 core->accuracy = core->ops->recalc_accuracy(core->hw,
2939 __clk_get_accuracy(core->parent));
2940 else if (core->parent)
2941 core->accuracy = core->parent->accuracy;
2942 else
2943 core->accuracy = 0;
2946 * Set clk's phase.
2947 * Since a phase is by definition relative to its parent, just
2948 * query the current clock phase, or just assume it's in phase.
2950 if (core->ops->get_phase)
2951 core->phase = core->ops->get_phase(core->hw);
2952 else
2953 core->phase = 0;
2956 * Set clk's rate. The preferred method is to use .recalc_rate. For
2957 * simple clocks and lazy developers the default fallback is to use the
2958 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2959 * then rate is set to zero.
2961 if (core->ops->recalc_rate)
2962 rate = core->ops->recalc_rate(core->hw,
2963 clk_core_get_rate_nolock(core->parent));
2964 else if (core->parent)
2965 rate = core->parent->rate;
2966 else
2967 rate = 0;
2968 core->rate = core->req_rate = rate;
2971 * walk the list of orphan clocks and reparent any that newly finds a
2972 * parent.
2974 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
2975 struct clk_core *parent = __clk_init_parent(orphan);
2976 unsigned long flags;
2979 * we could call __clk_set_parent, but that would result in a
2980 * redundant call to the .set_rate op, if it exists
2982 if (parent) {
2983 /* update the clk tree topology */
2984 flags = clk_enable_lock();
2985 clk_reparent(orphan, parent);
2986 clk_enable_unlock(flags);
2987 __clk_recalc_accuracies(orphan);
2988 __clk_recalc_rates(orphan, 0);
2993 * optional platform-specific magic
2995 * The .init callback is not used by any of the basic clock types, but
2996 * exists for weird hardware that must perform initialization magic.
2997 * Please consider other ways of solving initialization problems before
2998 * using this callback, as its use is discouraged.
3000 if (core->ops->init)
3001 core->ops->init(core->hw);
3003 if (core->flags & CLK_IS_CRITICAL) {
3004 unsigned long flags;
3006 clk_core_prepare(core);
3008 flags = clk_enable_lock();
3009 clk_core_enable(core);
3010 clk_enable_unlock(flags);
3013 kref_init(&core->ref);
3014 out:
3015 clk_pm_runtime_put(core);
3016 unlock:
3017 clk_prepare_unlock();
3019 if (!ret)
3020 clk_debug_register(core);
3022 return ret;
3025 struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
3026 const char *con_id)
3028 struct clk *clk;
3030 /* This is to allow this function to be chained to others */
3031 if (IS_ERR_OR_NULL(hw))
3032 return ERR_CAST(hw);
3034 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
3035 if (!clk)
3036 return ERR_PTR(-ENOMEM);
3038 clk->core = hw->core;
3039 clk->dev_id = dev_id;
3040 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
3041 clk->max_rate = ULONG_MAX;
3043 clk_prepare_lock();
3044 hlist_add_head(&clk->clks_node, &hw->core->clks);
3045 clk_prepare_unlock();
3047 return clk;
3050 void __clk_free_clk(struct clk *clk)
3052 clk_prepare_lock();
3053 hlist_del(&clk->clks_node);
3054 clk_prepare_unlock();
3056 kfree_const(clk->con_id);
3057 kfree(clk);
3061 * clk_register - allocate a new clock, register it and return an opaque cookie
3062 * @dev: device that is registering this clock
3063 * @hw: link to hardware-specific clock data
3065 * clk_register is the primary interface for populating the clock tree with new
3066 * clock nodes. It returns a pointer to the newly allocated struct clk which
3067 * cannot be dereferenced by driver code but may be used in conjunction with the
3068 * rest of the clock API. In the event of an error clk_register will return an
3069 * error code; drivers must test for an error code after calling clk_register.
3071 struct clk *clk_register(struct device *dev, struct clk_hw *hw)
3073 int i, ret;
3074 struct clk_core *core;
3076 core = kzalloc(sizeof(*core), GFP_KERNEL);
3077 if (!core) {
3078 ret = -ENOMEM;
3079 goto fail_out;
3082 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
3083 if (!core->name) {
3084 ret = -ENOMEM;
3085 goto fail_name;
3088 if (WARN_ON(!hw->init->ops)) {
3089 ret = -EINVAL;
3090 goto fail_ops;
3092 core->ops = hw->init->ops;
3094 if (dev && pm_runtime_enabled(dev))
3095 core->dev = dev;
3096 if (dev && dev->driver)
3097 core->owner = dev->driver->owner;
3098 core->hw = hw;
3099 core->flags = hw->init->flags;
3100 core->num_parents = hw->init->num_parents;
3101 core->min_rate = 0;
3102 core->max_rate = ULONG_MAX;
3103 hw->core = core;
3105 /* allocate local copy in case parent_names is __initdata */
3106 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
3107 GFP_KERNEL);
3109 if (!core->parent_names) {
3110 ret = -ENOMEM;
3111 goto fail_parent_names;
3115 /* copy each string name in case parent_names is __initdata */
3116 for (i = 0; i < core->num_parents; i++) {
3117 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
3118 GFP_KERNEL);
3119 if (!core->parent_names[i]) {
3120 ret = -ENOMEM;
3121 goto fail_parent_names_copy;
3125 /* avoid unnecessary string look-ups of clk_core's possible parents. */
3126 core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
3127 GFP_KERNEL);
3128 if (!core->parents) {
3129 ret = -ENOMEM;
3130 goto fail_parents;
3133 INIT_HLIST_HEAD(&core->clks);
3135 hw->clk = __clk_create_clk(hw, NULL, NULL);
3136 if (IS_ERR(hw->clk)) {
3137 ret = PTR_ERR(hw->clk);
3138 goto fail_parents;
3141 ret = __clk_core_init(core);
3142 if (!ret)
3143 return hw->clk;
3145 __clk_free_clk(hw->clk);
3146 hw->clk = NULL;
3148 fail_parents:
3149 kfree(core->parents);
3150 fail_parent_names_copy:
3151 while (--i >= 0)
3152 kfree_const(core->parent_names[i]);
3153 kfree(core->parent_names);
3154 fail_parent_names:
3155 fail_ops:
3156 kfree_const(core->name);
3157 fail_name:
3158 kfree(core);
3159 fail_out:
3160 return ERR_PTR(ret);
3162 EXPORT_SYMBOL_GPL(clk_register);
3165 * clk_hw_register - register a clk_hw and return an error code
3166 * @dev: device that is registering this clock
3167 * @hw: link to hardware-specific clock data
3169 * clk_hw_register is the primary interface for populating the clock tree with
3170 * new clock nodes. It returns an integer equal to zero indicating success or
3171 * less than zero indicating failure. Drivers must test for an error code after
3172 * calling clk_hw_register().
3174 int clk_hw_register(struct device *dev, struct clk_hw *hw)
3176 return PTR_ERR_OR_ZERO(clk_register(dev, hw));
3178 EXPORT_SYMBOL_GPL(clk_hw_register);
3180 /* Free memory allocated for a clock. */
3181 static void __clk_release(struct kref *ref)
3183 struct clk_core *core = container_of(ref, struct clk_core, ref);
3184 int i = core->num_parents;
3186 lockdep_assert_held(&prepare_lock);
3188 kfree(core->parents);
3189 while (--i >= 0)
3190 kfree_const(core->parent_names[i]);
3192 kfree(core->parent_names);
3193 kfree_const(core->name);
3194 kfree(core);
3198 * Empty clk_ops for unregistered clocks. These are used temporarily
3199 * after clk_unregister() was called on a clock and until last clock
3200 * consumer calls clk_put() and the struct clk object is freed.
3202 static int clk_nodrv_prepare_enable(struct clk_hw *hw)
3204 return -ENXIO;
3207 static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
3209 WARN_ON_ONCE(1);
3212 static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
3213 unsigned long parent_rate)
3215 return -ENXIO;
3218 static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
3220 return -ENXIO;
3223 static const struct clk_ops clk_nodrv_ops = {
3224 .enable = clk_nodrv_prepare_enable,
3225 .disable = clk_nodrv_disable_unprepare,
3226 .prepare = clk_nodrv_prepare_enable,
3227 .unprepare = clk_nodrv_disable_unprepare,
3228 .set_rate = clk_nodrv_set_rate,
3229 .set_parent = clk_nodrv_set_parent,
3233 * clk_unregister - unregister a currently registered clock
3234 * @clk: clock to unregister
3236 void clk_unregister(struct clk *clk)
3238 unsigned long flags;
3240 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3241 return;
3243 clk_debug_unregister(clk->core);
3245 clk_prepare_lock();
3247 if (clk->core->ops == &clk_nodrv_ops) {
3248 pr_err("%s: unregistered clock: %s\n", __func__,
3249 clk->core->name);
3250 goto unlock;
3253 * Assign empty clock ops for consumers that might still hold
3254 * a reference to this clock.
3256 flags = clk_enable_lock();
3257 clk->core->ops = &clk_nodrv_ops;
3258 clk_enable_unlock(flags);
3260 if (!hlist_empty(&clk->core->children)) {
3261 struct clk_core *child;
3262 struct hlist_node *t;
3264 /* Reparent all children to the orphan list. */
3265 hlist_for_each_entry_safe(child, t, &clk->core->children,
3266 child_node)
3267 clk_core_set_parent_nolock(child, NULL);
3270 hlist_del_init(&clk->core->child_node);
3272 if (clk->core->prepare_count)
3273 pr_warn("%s: unregistering prepared clock: %s\n",
3274 __func__, clk->core->name);
3276 if (clk->core->protect_count)
3277 pr_warn("%s: unregistering protected clock: %s\n",
3278 __func__, clk->core->name);
3280 kref_put(&clk->core->ref, __clk_release);
3281 unlock:
3282 clk_prepare_unlock();
3284 EXPORT_SYMBOL_GPL(clk_unregister);
3287 * clk_hw_unregister - unregister a currently registered clk_hw
3288 * @hw: hardware-specific clock data to unregister
3290 void clk_hw_unregister(struct clk_hw *hw)
3292 clk_unregister(hw->clk);
3294 EXPORT_SYMBOL_GPL(clk_hw_unregister);
3296 static void devm_clk_release(struct device *dev, void *res)
3298 clk_unregister(*(struct clk **)res);
3301 static void devm_clk_hw_release(struct device *dev, void *res)
3303 clk_hw_unregister(*(struct clk_hw **)res);
3307 * devm_clk_register - resource managed clk_register()
3308 * @dev: device that is registering this clock
3309 * @hw: link to hardware-specific clock data
3311 * Managed clk_register(). Clocks returned from this function are
3312 * automatically clk_unregister()ed on driver detach. See clk_register() for
3313 * more information.
3315 struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
3317 struct clk *clk;
3318 struct clk **clkp;
3320 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
3321 if (!clkp)
3322 return ERR_PTR(-ENOMEM);
3324 clk = clk_register(dev, hw);
3325 if (!IS_ERR(clk)) {
3326 *clkp = clk;
3327 devres_add(dev, clkp);
3328 } else {
3329 devres_free(clkp);
3332 return clk;
3334 EXPORT_SYMBOL_GPL(devm_clk_register);
3337 * devm_clk_hw_register - resource managed clk_hw_register()
3338 * @dev: device that is registering this clock
3339 * @hw: link to hardware-specific clock data
3341 * Managed clk_hw_register(). Clocks registered by this function are
3342 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
3343 * for more information.
3345 int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
3347 struct clk_hw **hwp;
3348 int ret;
3350 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
3351 if (!hwp)
3352 return -ENOMEM;
3354 ret = clk_hw_register(dev, hw);
3355 if (!ret) {
3356 *hwp = hw;
3357 devres_add(dev, hwp);
3358 } else {
3359 devres_free(hwp);
3362 return ret;
3364 EXPORT_SYMBOL_GPL(devm_clk_hw_register);
3366 static int devm_clk_match(struct device *dev, void *res, void *data)
3368 struct clk *c = res;
3369 if (WARN_ON(!c))
3370 return 0;
3371 return c == data;
3374 static int devm_clk_hw_match(struct device *dev, void *res, void *data)
3376 struct clk_hw *hw = res;
3378 if (WARN_ON(!hw))
3379 return 0;
3380 return hw == data;
3384 * devm_clk_unregister - resource managed clk_unregister()
3385 * @clk: clock to unregister
3387 * Deallocate a clock allocated with devm_clk_register(). Normally
3388 * this function will not need to be called and the resource management
3389 * code will ensure that the resource is freed.
3391 void devm_clk_unregister(struct device *dev, struct clk *clk)
3393 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
3395 EXPORT_SYMBOL_GPL(devm_clk_unregister);
3398 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
3399 * @dev: device that is unregistering the hardware-specific clock data
3400 * @hw: link to hardware-specific clock data
3402 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
3403 * this function will not need to be called and the resource management
3404 * code will ensure that the resource is freed.
3406 void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
3408 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
3409 hw));
3411 EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
3414 * clkdev helpers
3416 int __clk_get(struct clk *clk)
3418 struct clk_core *core = !clk ? NULL : clk->core;
3420 if (core) {
3421 if (!try_module_get(core->owner))
3422 return 0;
3424 kref_get(&core->ref);
3426 return 1;
3429 void __clk_put(struct clk *clk)
3431 struct module *owner;
3433 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
3434 return;
3436 clk_prepare_lock();
3439 * Before calling clk_put, all calls to clk_rate_exclusive_get() from a
3440 * given user should be balanced with calls to clk_rate_exclusive_put()
3441 * and by that same consumer
3443 if (WARN_ON(clk->exclusive_count)) {
3444 /* We voiced our concern, let's sanitize the situation */
3445 clk->core->protect_count -= (clk->exclusive_count - 1);
3446 clk_core_rate_unprotect(clk->core);
3447 clk->exclusive_count = 0;
3450 hlist_del(&clk->clks_node);
3451 if (clk->min_rate > clk->core->req_rate ||
3452 clk->max_rate < clk->core->req_rate)
3453 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
3455 owner = clk->core->owner;
3456 kref_put(&clk->core->ref, __clk_release);
3458 clk_prepare_unlock();
3460 module_put(owner);
3462 kfree(clk);
3465 /*** clk rate change notifiers ***/
3468 * clk_notifier_register - add a clk rate change notifier
3469 * @clk: struct clk * to watch
3470 * @nb: struct notifier_block * with callback info
3472 * Request notification when clk's rate changes. This uses an SRCU
3473 * notifier because we want it to block and notifier unregistrations are
3474 * uncommon. The callbacks associated with the notifier must not
3475 * re-enter into the clk framework by calling any top-level clk APIs;
3476 * this will cause a nested prepare_lock mutex.
3478 * In all notification cases (pre, post and abort rate change) the original
3479 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
3480 * and the new frequency is passed via struct clk_notifier_data.new_rate.
3482 * clk_notifier_register() must be called from non-atomic context.
3483 * Returns -EINVAL if called with null arguments, -ENOMEM upon
3484 * allocation failure; otherwise, passes along the return value of
3485 * srcu_notifier_chain_register().
3487 int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
3489 struct clk_notifier *cn;
3490 int ret = -ENOMEM;
3492 if (!clk || !nb)
3493 return -EINVAL;
3495 clk_prepare_lock();
3497 /* search the list of notifiers for this clk */
3498 list_for_each_entry(cn, &clk_notifier_list, node)
3499 if (cn->clk == clk)
3500 break;
3502 /* if clk wasn't in the notifier list, allocate new clk_notifier */
3503 if (cn->clk != clk) {
3504 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
3505 if (!cn)
3506 goto out;
3508 cn->clk = clk;
3509 srcu_init_notifier_head(&cn->notifier_head);
3511 list_add(&cn->node, &clk_notifier_list);
3514 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
3516 clk->core->notifier_count++;
3518 out:
3519 clk_prepare_unlock();
3521 return ret;
3523 EXPORT_SYMBOL_GPL(clk_notifier_register);
3526 * clk_notifier_unregister - remove a clk rate change notifier
3527 * @clk: struct clk *
3528 * @nb: struct notifier_block * with callback info
3530 * Request no further notification for changes to 'clk' and frees memory
3531 * allocated in clk_notifier_register.
3533 * Returns -EINVAL if called with null arguments; otherwise, passes
3534 * along the return value of srcu_notifier_chain_unregister().
3536 int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
3538 struct clk_notifier *cn = NULL;
3539 int ret = -EINVAL;
3541 if (!clk || !nb)
3542 return -EINVAL;
3544 clk_prepare_lock();
3546 list_for_each_entry(cn, &clk_notifier_list, node)
3547 if (cn->clk == clk)
3548 break;
3550 if (cn->clk == clk) {
3551 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
3553 clk->core->notifier_count--;
3555 /* XXX the notifier code should handle this better */
3556 if (!cn->notifier_head.head) {
3557 srcu_cleanup_notifier_head(&cn->notifier_head);
3558 list_del(&cn->node);
3559 kfree(cn);
3562 } else {
3563 ret = -ENOENT;
3566 clk_prepare_unlock();
3568 return ret;
3570 EXPORT_SYMBOL_GPL(clk_notifier_unregister);
3572 #ifdef CONFIG_OF
3574 * struct of_clk_provider - Clock provider registration structure
3575 * @link: Entry in global list of clock providers
3576 * @node: Pointer to device tree node of clock provider
3577 * @get: Get clock callback. Returns NULL or a struct clk for the
3578 * given clock specifier
3579 * @data: context pointer to be passed into @get callback
3581 struct of_clk_provider {
3582 struct list_head link;
3584 struct device_node *node;
3585 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
3586 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
3587 void *data;
3590 static const struct of_device_id __clk_of_table_sentinel
3591 __used __section(__clk_of_table_end);
3593 static LIST_HEAD(of_clk_providers);
3594 static DEFINE_MUTEX(of_clk_mutex);
3596 struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
3597 void *data)
3599 return data;
3601 EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
3603 struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
3605 return data;
3607 EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
3609 struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
3611 struct clk_onecell_data *clk_data = data;
3612 unsigned int idx = clkspec->args[0];
3614 if (idx >= clk_data->clk_num) {
3615 pr_err("%s: invalid clock index %u\n", __func__, idx);
3616 return ERR_PTR(-EINVAL);
3619 return clk_data->clks[idx];
3621 EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
3623 struct clk_hw *
3624 of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
3626 struct clk_hw_onecell_data *hw_data = data;
3627 unsigned int idx = clkspec->args[0];
3629 if (idx >= hw_data->num) {
3630 pr_err("%s: invalid index %u\n", __func__, idx);
3631 return ERR_PTR(-EINVAL);
3634 return hw_data->hws[idx];
3636 EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
3639 * of_clk_add_provider() - Register a clock provider for a node
3640 * @np: Device node pointer associated with clock provider
3641 * @clk_src_get: callback for decoding clock
3642 * @data: context pointer for @clk_src_get callback.
3644 int of_clk_add_provider(struct device_node *np,
3645 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
3646 void *data),
3647 void *data)
3649 struct of_clk_provider *cp;
3650 int ret;
3652 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3653 if (!cp)
3654 return -ENOMEM;
3656 cp->node = of_node_get(np);
3657 cp->data = data;
3658 cp->get = clk_src_get;
3660 mutex_lock(&of_clk_mutex);
3661 list_add(&cp->link, &of_clk_providers);
3662 mutex_unlock(&of_clk_mutex);
3663 pr_debug("Added clock from %pOF\n", np);
3665 ret = of_clk_set_defaults(np, true);
3666 if (ret < 0)
3667 of_clk_del_provider(np);
3669 return ret;
3671 EXPORT_SYMBOL_GPL(of_clk_add_provider);
3674 * of_clk_add_hw_provider() - Register a clock provider for a node
3675 * @np: Device node pointer associated with clock provider
3676 * @get: callback for decoding clk_hw
3677 * @data: context pointer for @get callback.
3679 int of_clk_add_hw_provider(struct device_node *np,
3680 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3681 void *data),
3682 void *data)
3684 struct of_clk_provider *cp;
3685 int ret;
3687 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3688 if (!cp)
3689 return -ENOMEM;
3691 cp->node = of_node_get(np);
3692 cp->data = data;
3693 cp->get_hw = get;
3695 mutex_lock(&of_clk_mutex);
3696 list_add(&cp->link, &of_clk_providers);
3697 mutex_unlock(&of_clk_mutex);
3698 pr_debug("Added clk_hw provider from %pOF\n", np);
3700 ret = of_clk_set_defaults(np, true);
3701 if (ret < 0)
3702 of_clk_del_provider(np);
3704 return ret;
3706 EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
3708 static void devm_of_clk_release_provider(struct device *dev, void *res)
3710 of_clk_del_provider(*(struct device_node **)res);
3713 int devm_of_clk_add_hw_provider(struct device *dev,
3714 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3715 void *data),
3716 void *data)
3718 struct device_node **ptr, *np;
3719 int ret;
3721 ptr = devres_alloc(devm_of_clk_release_provider, sizeof(*ptr),
3722 GFP_KERNEL);
3723 if (!ptr)
3724 return -ENOMEM;
3726 np = dev->of_node;
3727 ret = of_clk_add_hw_provider(np, get, data);
3728 if (!ret) {
3729 *ptr = np;
3730 devres_add(dev, ptr);
3731 } else {
3732 devres_free(ptr);
3735 return ret;
3737 EXPORT_SYMBOL_GPL(devm_of_clk_add_hw_provider);
3740 * of_clk_del_provider() - Remove a previously registered clock provider
3741 * @np: Device node pointer associated with clock provider
3743 void of_clk_del_provider(struct device_node *np)
3745 struct of_clk_provider *cp;
3747 mutex_lock(&of_clk_mutex);
3748 list_for_each_entry(cp, &of_clk_providers, link) {
3749 if (cp->node == np) {
3750 list_del(&cp->link);
3751 of_node_put(cp->node);
3752 kfree(cp);
3753 break;
3756 mutex_unlock(&of_clk_mutex);
3758 EXPORT_SYMBOL_GPL(of_clk_del_provider);
3760 static int devm_clk_provider_match(struct device *dev, void *res, void *data)
3762 struct device_node **np = res;
3764 if (WARN_ON(!np || !*np))
3765 return 0;
3767 return *np == data;
3770 void devm_of_clk_del_provider(struct device *dev)
3772 int ret;
3774 ret = devres_release(dev, devm_of_clk_release_provider,
3775 devm_clk_provider_match, dev->of_node);
3777 WARN_ON(ret);
3779 EXPORT_SYMBOL(devm_of_clk_del_provider);
3781 static struct clk_hw *
3782 __of_clk_get_hw_from_provider(struct of_clk_provider *provider,
3783 struct of_phandle_args *clkspec)
3785 struct clk *clk;
3787 if (provider->get_hw)
3788 return provider->get_hw(clkspec, provider->data);
3790 clk = provider->get(clkspec, provider->data);
3791 if (IS_ERR(clk))
3792 return ERR_CAST(clk);
3793 return __clk_get_hw(clk);
3796 struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
3797 const char *dev_id, const char *con_id)
3799 struct of_clk_provider *provider;
3800 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
3801 struct clk_hw *hw;
3803 if (!clkspec)
3804 return ERR_PTR(-EINVAL);
3806 /* Check if we have such a provider in our array */
3807 mutex_lock(&of_clk_mutex);
3808 list_for_each_entry(provider, &of_clk_providers, link) {
3809 if (provider->node == clkspec->np) {
3810 hw = __of_clk_get_hw_from_provider(provider, clkspec);
3811 clk = __clk_create_clk(hw, dev_id, con_id);
3814 if (!IS_ERR(clk)) {
3815 if (!__clk_get(clk)) {
3816 __clk_free_clk(clk);
3817 clk = ERR_PTR(-ENOENT);
3820 break;
3823 mutex_unlock(&of_clk_mutex);
3825 return clk;
3829 * of_clk_get_from_provider() - Lookup a clock from a clock provider
3830 * @clkspec: pointer to a clock specifier data structure
3832 * This function looks up a struct clk from the registered list of clock
3833 * providers, an input is a clock specifier data structure as returned
3834 * from the of_parse_phandle_with_args() function call.
3836 struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
3838 return __of_clk_get_from_provider(clkspec, NULL, __func__);
3840 EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
3843 * of_clk_get_parent_count() - Count the number of clocks a device node has
3844 * @np: device node to count
3846 * Returns: The number of clocks that are possible parents of this node
3848 unsigned int of_clk_get_parent_count(struct device_node *np)
3850 int count;
3852 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
3853 if (count < 0)
3854 return 0;
3856 return count;
3858 EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
3860 const char *of_clk_get_parent_name(struct device_node *np, int index)
3862 struct of_phandle_args clkspec;
3863 struct property *prop;
3864 const char *clk_name;
3865 const __be32 *vp;
3866 u32 pv;
3867 int rc;
3868 int count;
3869 struct clk *clk;
3871 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
3872 &clkspec);
3873 if (rc)
3874 return NULL;
3876 index = clkspec.args_count ? clkspec.args[0] : 0;
3877 count = 0;
3879 /* if there is an indices property, use it to transfer the index
3880 * specified into an array offset for the clock-output-names property.
3882 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
3883 if (index == pv) {
3884 index = count;
3885 break;
3887 count++;
3889 /* We went off the end of 'clock-indices' without finding it */
3890 if (prop && !vp)
3891 return NULL;
3893 if (of_property_read_string_index(clkspec.np, "clock-output-names",
3894 index,
3895 &clk_name) < 0) {
3897 * Best effort to get the name if the clock has been
3898 * registered with the framework. If the clock isn't
3899 * registered, we return the node name as the name of
3900 * the clock as long as #clock-cells = 0.
3902 clk = of_clk_get_from_provider(&clkspec);
3903 if (IS_ERR(clk)) {
3904 if (clkspec.args_count == 0)
3905 clk_name = clkspec.np->name;
3906 else
3907 clk_name = NULL;
3908 } else {
3909 clk_name = __clk_get_name(clk);
3910 clk_put(clk);
3915 of_node_put(clkspec.np);
3916 return clk_name;
3918 EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3921 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
3922 * number of parents
3923 * @np: Device node pointer associated with clock provider
3924 * @parents: pointer to char array that hold the parents' names
3925 * @size: size of the @parents array
3927 * Return: number of parents for the clock node.
3929 int of_clk_parent_fill(struct device_node *np, const char **parents,
3930 unsigned int size)
3932 unsigned int i = 0;
3934 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
3935 i++;
3937 return i;
3939 EXPORT_SYMBOL_GPL(of_clk_parent_fill);
3941 struct clock_provider {
3942 of_clk_init_cb_t clk_init_cb;
3943 struct device_node *np;
3944 struct list_head node;
3948 * This function looks for a parent clock. If there is one, then it
3949 * checks that the provider for this parent clock was initialized, in
3950 * this case the parent clock will be ready.
3952 static int parent_ready(struct device_node *np)
3954 int i = 0;
3956 while (true) {
3957 struct clk *clk = of_clk_get(np, i);
3959 /* this parent is ready we can check the next one */
3960 if (!IS_ERR(clk)) {
3961 clk_put(clk);
3962 i++;
3963 continue;
3966 /* at least one parent is not ready, we exit now */
3967 if (PTR_ERR(clk) == -EPROBE_DEFER)
3968 return 0;
3971 * Here we make assumption that the device tree is
3972 * written correctly. So an error means that there is
3973 * no more parent. As we didn't exit yet, then the
3974 * previous parent are ready. If there is no clock
3975 * parent, no need to wait for them, then we can
3976 * consider their absence as being ready
3978 return 1;
3983 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
3984 * @np: Device node pointer associated with clock provider
3985 * @index: clock index
3986 * @flags: pointer to top-level framework flags
3988 * Detects if the clock-critical property exists and, if so, sets the
3989 * corresponding CLK_IS_CRITICAL flag.
3991 * Do not use this function. It exists only for legacy Device Tree
3992 * bindings, such as the one-clock-per-node style that are outdated.
3993 * Those bindings typically put all clock data into .dts and the Linux
3994 * driver has no clock data, thus making it impossible to set this flag
3995 * correctly from the driver. Only those drivers may call
3996 * of_clk_detect_critical from their setup functions.
3998 * Return: error code or zero on success
4000 int of_clk_detect_critical(struct device_node *np,
4001 int index, unsigned long *flags)
4003 struct property *prop;
4004 const __be32 *cur;
4005 uint32_t idx;
4007 if (!np || !flags)
4008 return -EINVAL;
4010 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
4011 if (index == idx)
4012 *flags |= CLK_IS_CRITICAL;
4014 return 0;
4018 * of_clk_init() - Scan and init clock providers from the DT
4019 * @matches: array of compatible values and init functions for providers.
4021 * This function scans the device tree for matching clock providers
4022 * and calls their initialization functions. It also does it by trying
4023 * to follow the dependencies.
4025 void __init of_clk_init(const struct of_device_id *matches)
4027 const struct of_device_id *match;
4028 struct device_node *np;
4029 struct clock_provider *clk_provider, *next;
4030 bool is_init_done;
4031 bool force = false;
4032 LIST_HEAD(clk_provider_list);
4034 if (!matches)
4035 matches = &__clk_of_table;
4037 /* First prepare the list of the clocks providers */
4038 for_each_matching_node_and_match(np, matches, &match) {
4039 struct clock_provider *parent;
4041 if (!of_device_is_available(np))
4042 continue;
4044 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
4045 if (!parent) {
4046 list_for_each_entry_safe(clk_provider, next,
4047 &clk_provider_list, node) {
4048 list_del(&clk_provider->node);
4049 of_node_put(clk_provider->np);
4050 kfree(clk_provider);
4052 of_node_put(np);
4053 return;
4056 parent->clk_init_cb = match->data;
4057 parent->np = of_node_get(np);
4058 list_add_tail(&parent->node, &clk_provider_list);
4061 while (!list_empty(&clk_provider_list)) {
4062 is_init_done = false;
4063 list_for_each_entry_safe(clk_provider, next,
4064 &clk_provider_list, node) {
4065 if (force || parent_ready(clk_provider->np)) {
4067 /* Don't populate platform devices */
4068 of_node_set_flag(clk_provider->np,
4069 OF_POPULATED);
4071 clk_provider->clk_init_cb(clk_provider->np);
4072 of_clk_set_defaults(clk_provider->np, true);
4074 list_del(&clk_provider->node);
4075 of_node_put(clk_provider->np);
4076 kfree(clk_provider);
4077 is_init_done = true;
4082 * We didn't manage to initialize any of the
4083 * remaining providers during the last loop, so now we
4084 * initialize all the remaining ones unconditionally
4085 * in case the clock parent was not mandatory
4087 if (!is_init_done)
4088 force = true;
4091 #endif