uapi/if_ether.h: move __UAPI_DEF_ETHHDR libc define
[linux/fpc-iii.git] / drivers / clk / clk.c
bloba3f52f6782115abd7038f8f0af9e1b78faebd5db
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/sched.h>
25 #include <linux/clkdev.h>
27 #include "clk.h"
29 static DEFINE_SPINLOCK(enable_lock);
30 static DEFINE_MUTEX(prepare_lock);
32 static struct task_struct *prepare_owner;
33 static struct task_struct *enable_owner;
35 static int prepare_refcnt;
36 static int enable_refcnt;
38 static HLIST_HEAD(clk_root_list);
39 static HLIST_HEAD(clk_orphan_list);
40 static LIST_HEAD(clk_notifier_list);
42 /*** private data structures ***/
44 struct clk_core {
45 const char *name;
46 const struct clk_ops *ops;
47 struct clk_hw *hw;
48 struct module *owner;
49 struct clk_core *parent;
50 const char **parent_names;
51 struct clk_core **parents;
52 u8 num_parents;
53 u8 new_parent_index;
54 unsigned long rate;
55 unsigned long req_rate;
56 unsigned long new_rate;
57 struct clk_core *new_parent;
58 struct clk_core *new_child;
59 unsigned long flags;
60 bool orphan;
61 unsigned int enable_count;
62 unsigned int prepare_count;
63 unsigned long min_rate;
64 unsigned long max_rate;
65 unsigned long accuracy;
66 int phase;
67 struct hlist_head children;
68 struct hlist_node child_node;
69 struct hlist_head clks;
70 unsigned int notifier_count;
71 #ifdef CONFIG_DEBUG_FS
72 struct dentry *dentry;
73 struct hlist_node debug_node;
74 #endif
75 struct kref ref;
78 #define CREATE_TRACE_POINTS
79 #include <trace/events/clk.h>
81 struct clk {
82 struct clk_core *core;
83 const char *dev_id;
84 const char *con_id;
85 unsigned long min_rate;
86 unsigned long max_rate;
87 struct hlist_node clks_node;
90 /*** locking ***/
91 static void clk_prepare_lock(void)
93 if (!mutex_trylock(&prepare_lock)) {
94 if (prepare_owner == current) {
95 prepare_refcnt++;
96 return;
98 mutex_lock(&prepare_lock);
100 WARN_ON_ONCE(prepare_owner != NULL);
101 WARN_ON_ONCE(prepare_refcnt != 0);
102 prepare_owner = current;
103 prepare_refcnt = 1;
106 static void clk_prepare_unlock(void)
108 WARN_ON_ONCE(prepare_owner != current);
109 WARN_ON_ONCE(prepare_refcnt == 0);
111 if (--prepare_refcnt)
112 return;
113 prepare_owner = NULL;
114 mutex_unlock(&prepare_lock);
117 static unsigned long clk_enable_lock(void)
118 __acquires(enable_lock)
120 unsigned long flags;
122 if (!spin_trylock_irqsave(&enable_lock, flags)) {
123 if (enable_owner == current) {
124 enable_refcnt++;
125 __acquire(enable_lock);
126 return flags;
128 spin_lock_irqsave(&enable_lock, flags);
130 WARN_ON_ONCE(enable_owner != NULL);
131 WARN_ON_ONCE(enable_refcnt != 0);
132 enable_owner = current;
133 enable_refcnt = 1;
134 return flags;
137 static void clk_enable_unlock(unsigned long flags)
138 __releases(enable_lock)
140 WARN_ON_ONCE(enable_owner != current);
141 WARN_ON_ONCE(enable_refcnt == 0);
143 if (--enable_refcnt) {
144 __release(enable_lock);
145 return;
147 enable_owner = NULL;
148 spin_unlock_irqrestore(&enable_lock, flags);
151 static bool clk_core_is_prepared(struct clk_core *core)
154 * .is_prepared is optional for clocks that can prepare
155 * fall back to software usage counter if it is missing
157 if (!core->ops->is_prepared)
158 return core->prepare_count;
160 return core->ops->is_prepared(core->hw);
163 static bool clk_core_is_enabled(struct clk_core *core)
166 * .is_enabled is only mandatory for clocks that gate
167 * fall back to software usage counter if .is_enabled is missing
169 if (!core->ops->is_enabled)
170 return core->enable_count;
172 return core->ops->is_enabled(core->hw);
175 /*** helper functions ***/
177 const char *__clk_get_name(const struct clk *clk)
179 return !clk ? NULL : clk->core->name;
181 EXPORT_SYMBOL_GPL(__clk_get_name);
183 const char *clk_hw_get_name(const struct clk_hw *hw)
185 return hw->core->name;
187 EXPORT_SYMBOL_GPL(clk_hw_get_name);
189 struct clk_hw *__clk_get_hw(struct clk *clk)
191 return !clk ? NULL : clk->core->hw;
193 EXPORT_SYMBOL_GPL(__clk_get_hw);
195 unsigned int clk_hw_get_num_parents(const struct clk_hw *hw)
197 return hw->core->num_parents;
199 EXPORT_SYMBOL_GPL(clk_hw_get_num_parents);
201 struct clk_hw *clk_hw_get_parent(const struct clk_hw *hw)
203 return hw->core->parent ? hw->core->parent->hw : NULL;
205 EXPORT_SYMBOL_GPL(clk_hw_get_parent);
207 static struct clk_core *__clk_lookup_subtree(const char *name,
208 struct clk_core *core)
210 struct clk_core *child;
211 struct clk_core *ret;
213 if (!strcmp(core->name, name))
214 return core;
216 hlist_for_each_entry(child, &core->children, child_node) {
217 ret = __clk_lookup_subtree(name, child);
218 if (ret)
219 return ret;
222 return NULL;
225 static struct clk_core *clk_core_lookup(const char *name)
227 struct clk_core *root_clk;
228 struct clk_core *ret;
230 if (!name)
231 return NULL;
233 /* search the 'proper' clk tree first */
234 hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
235 ret = __clk_lookup_subtree(name, root_clk);
236 if (ret)
237 return ret;
240 /* if not found, then search the orphan tree */
241 hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
242 ret = __clk_lookup_subtree(name, root_clk);
243 if (ret)
244 return ret;
247 return NULL;
250 static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
251 u8 index)
253 if (!core || index >= core->num_parents)
254 return NULL;
256 if (!core->parents[index])
257 core->parents[index] =
258 clk_core_lookup(core->parent_names[index]);
260 return core->parents[index];
263 struct clk_hw *
264 clk_hw_get_parent_by_index(const struct clk_hw *hw, unsigned int index)
266 struct clk_core *parent;
268 parent = clk_core_get_parent_by_index(hw->core, index);
270 return !parent ? NULL : parent->hw;
272 EXPORT_SYMBOL_GPL(clk_hw_get_parent_by_index);
274 unsigned int __clk_get_enable_count(struct clk *clk)
276 return !clk ? 0 : clk->core->enable_count;
279 static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
281 unsigned long ret;
283 if (!core) {
284 ret = 0;
285 goto out;
288 ret = core->rate;
290 if (!core->num_parents)
291 goto out;
293 if (!core->parent)
294 ret = 0;
296 out:
297 return ret;
300 unsigned long clk_hw_get_rate(const struct clk_hw *hw)
302 return clk_core_get_rate_nolock(hw->core);
304 EXPORT_SYMBOL_GPL(clk_hw_get_rate);
306 static unsigned long __clk_get_accuracy(struct clk_core *core)
308 if (!core)
309 return 0;
311 return core->accuracy;
314 unsigned long __clk_get_flags(struct clk *clk)
316 return !clk ? 0 : clk->core->flags;
318 EXPORT_SYMBOL_GPL(__clk_get_flags);
320 unsigned long clk_hw_get_flags(const struct clk_hw *hw)
322 return hw->core->flags;
324 EXPORT_SYMBOL_GPL(clk_hw_get_flags);
326 bool clk_hw_is_prepared(const struct clk_hw *hw)
328 return clk_core_is_prepared(hw->core);
331 bool clk_hw_is_enabled(const struct clk_hw *hw)
333 return clk_core_is_enabled(hw->core);
336 bool __clk_is_enabled(struct clk *clk)
338 if (!clk)
339 return false;
341 return clk_core_is_enabled(clk->core);
343 EXPORT_SYMBOL_GPL(__clk_is_enabled);
345 static bool mux_is_better_rate(unsigned long rate, unsigned long now,
346 unsigned long best, unsigned long flags)
348 if (flags & CLK_MUX_ROUND_CLOSEST)
349 return abs(now - rate) < abs(best - rate);
351 return now <= rate && now > best;
354 int clk_mux_determine_rate_flags(struct clk_hw *hw,
355 struct clk_rate_request *req,
356 unsigned long flags)
358 struct clk_core *core = hw->core, *parent, *best_parent = NULL;
359 int i, num_parents, ret;
360 unsigned long best = 0;
361 struct clk_rate_request parent_req = *req;
363 /* if NO_REPARENT flag set, pass through to current parent */
364 if (core->flags & CLK_SET_RATE_NO_REPARENT) {
365 parent = core->parent;
366 if (core->flags & CLK_SET_RATE_PARENT) {
367 ret = __clk_determine_rate(parent ? parent->hw : NULL,
368 &parent_req);
369 if (ret)
370 return ret;
372 best = parent_req.rate;
373 } else if (parent) {
374 best = clk_core_get_rate_nolock(parent);
375 } else {
376 best = clk_core_get_rate_nolock(core);
379 goto out;
382 /* find the parent that can provide the fastest rate <= rate */
383 num_parents = core->num_parents;
384 for (i = 0; i < num_parents; i++) {
385 parent = clk_core_get_parent_by_index(core, i);
386 if (!parent)
387 continue;
389 if (core->flags & CLK_SET_RATE_PARENT) {
390 parent_req = *req;
391 ret = __clk_determine_rate(parent->hw, &parent_req);
392 if (ret)
393 continue;
394 } else {
395 parent_req.rate = clk_core_get_rate_nolock(parent);
398 if (mux_is_better_rate(req->rate, parent_req.rate,
399 best, flags)) {
400 best_parent = parent;
401 best = parent_req.rate;
405 if (!best_parent)
406 return -EINVAL;
408 out:
409 if (best_parent)
410 req->best_parent_hw = best_parent->hw;
411 req->best_parent_rate = best;
412 req->rate = best;
414 return 0;
416 EXPORT_SYMBOL_GPL(clk_mux_determine_rate_flags);
418 struct clk *__clk_lookup(const char *name)
420 struct clk_core *core = clk_core_lookup(name);
422 return !core ? NULL : core->hw->clk;
425 static void clk_core_get_boundaries(struct clk_core *core,
426 unsigned long *min_rate,
427 unsigned long *max_rate)
429 struct clk *clk_user;
431 *min_rate = core->min_rate;
432 *max_rate = core->max_rate;
434 hlist_for_each_entry(clk_user, &core->clks, clks_node)
435 *min_rate = max(*min_rate, clk_user->min_rate);
437 hlist_for_each_entry(clk_user, &core->clks, clks_node)
438 *max_rate = min(*max_rate, clk_user->max_rate);
441 void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
442 unsigned long max_rate)
444 hw->core->min_rate = min_rate;
445 hw->core->max_rate = max_rate;
447 EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
450 * Helper for finding best parent to provide a given frequency. This can be used
451 * directly as a determine_rate callback (e.g. for a mux), or from a more
452 * complex clock that may combine a mux with other operations.
454 int __clk_mux_determine_rate(struct clk_hw *hw,
455 struct clk_rate_request *req)
457 return clk_mux_determine_rate_flags(hw, req, 0);
459 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
461 int __clk_mux_determine_rate_closest(struct clk_hw *hw,
462 struct clk_rate_request *req)
464 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
466 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
468 /*** clk api ***/
470 static void clk_core_unprepare(struct clk_core *core)
472 lockdep_assert_held(&prepare_lock);
474 if (!core)
475 return;
477 if (WARN_ON(core->prepare_count == 0))
478 return;
480 if (WARN_ON(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL))
481 return;
483 if (--core->prepare_count > 0)
484 return;
486 WARN_ON(core->enable_count > 0);
488 trace_clk_unprepare(core);
490 if (core->ops->unprepare)
491 core->ops->unprepare(core->hw);
493 trace_clk_unprepare_complete(core);
494 clk_core_unprepare(core->parent);
497 static void clk_core_unprepare_lock(struct clk_core *core)
499 clk_prepare_lock();
500 clk_core_unprepare(core);
501 clk_prepare_unlock();
505 * clk_unprepare - undo preparation of a clock source
506 * @clk: the clk being unprepared
508 * clk_unprepare may sleep, which differentiates it from clk_disable. In a
509 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
510 * if the operation may sleep. One example is a clk which is accessed over
511 * I2c. In the complex case a clk gate operation may require a fast and a slow
512 * part. It is this reason that clk_unprepare and clk_disable are not mutually
513 * exclusive. In fact clk_disable must be called before clk_unprepare.
515 void clk_unprepare(struct clk *clk)
517 if (IS_ERR_OR_NULL(clk))
518 return;
520 clk_core_unprepare_lock(clk->core);
522 EXPORT_SYMBOL_GPL(clk_unprepare);
524 static int clk_core_prepare(struct clk_core *core)
526 int ret = 0;
528 lockdep_assert_held(&prepare_lock);
530 if (!core)
531 return 0;
533 if (core->prepare_count == 0) {
534 ret = clk_core_prepare(core->parent);
535 if (ret)
536 return ret;
538 trace_clk_prepare(core);
540 if (core->ops->prepare)
541 ret = core->ops->prepare(core->hw);
543 trace_clk_prepare_complete(core);
545 if (ret) {
546 clk_core_unprepare(core->parent);
547 return ret;
551 core->prepare_count++;
553 return 0;
556 static int clk_core_prepare_lock(struct clk_core *core)
558 int ret;
560 clk_prepare_lock();
561 ret = clk_core_prepare(core);
562 clk_prepare_unlock();
564 return ret;
568 * clk_prepare - prepare a clock source
569 * @clk: the clk being prepared
571 * clk_prepare may sleep, which differentiates it from clk_enable. In a simple
572 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
573 * operation may sleep. One example is a clk which is accessed over I2c. In
574 * the complex case a clk ungate operation may require a fast and a slow part.
575 * It is this reason that clk_prepare and clk_enable are not mutually
576 * exclusive. In fact clk_prepare must be called before clk_enable.
577 * Returns 0 on success, -EERROR otherwise.
579 int clk_prepare(struct clk *clk)
581 if (!clk)
582 return 0;
584 return clk_core_prepare_lock(clk->core);
586 EXPORT_SYMBOL_GPL(clk_prepare);
588 static void clk_core_disable(struct clk_core *core)
590 lockdep_assert_held(&enable_lock);
592 if (!core)
593 return;
595 if (WARN_ON(core->enable_count == 0))
596 return;
598 if (WARN_ON(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL))
599 return;
601 if (--core->enable_count > 0)
602 return;
604 trace_clk_disable_rcuidle(core);
606 if (core->ops->disable)
607 core->ops->disable(core->hw);
609 trace_clk_disable_complete_rcuidle(core);
611 clk_core_disable(core->parent);
614 static void clk_core_disable_lock(struct clk_core *core)
616 unsigned long flags;
618 flags = clk_enable_lock();
619 clk_core_disable(core);
620 clk_enable_unlock(flags);
624 * clk_disable - gate a clock
625 * @clk: the clk being gated
627 * clk_disable must not sleep, which differentiates it from clk_unprepare. In
628 * a simple case, clk_disable can be used instead of clk_unprepare to gate a
629 * clk if the operation is fast and will never sleep. One example is a
630 * SoC-internal clk which is controlled via simple register writes. In the
631 * complex case a clk gate operation may require a fast and a slow part. It is
632 * this reason that clk_unprepare and clk_disable are not mutually exclusive.
633 * In fact clk_disable must be called before clk_unprepare.
635 void clk_disable(struct clk *clk)
637 if (IS_ERR_OR_NULL(clk))
638 return;
640 clk_core_disable_lock(clk->core);
642 EXPORT_SYMBOL_GPL(clk_disable);
644 static int clk_core_enable(struct clk_core *core)
646 int ret = 0;
648 lockdep_assert_held(&enable_lock);
650 if (!core)
651 return 0;
653 if (WARN_ON(core->prepare_count == 0))
654 return -ESHUTDOWN;
656 if (core->enable_count == 0) {
657 ret = clk_core_enable(core->parent);
659 if (ret)
660 return ret;
662 trace_clk_enable_rcuidle(core);
664 if (core->ops->enable)
665 ret = core->ops->enable(core->hw);
667 trace_clk_enable_complete_rcuidle(core);
669 if (ret) {
670 clk_core_disable(core->parent);
671 return ret;
675 core->enable_count++;
676 return 0;
679 static int clk_core_enable_lock(struct clk_core *core)
681 unsigned long flags;
682 int ret;
684 flags = clk_enable_lock();
685 ret = clk_core_enable(core);
686 clk_enable_unlock(flags);
688 return ret;
692 * clk_enable - ungate a clock
693 * @clk: the clk being ungated
695 * clk_enable must not sleep, which differentiates it from clk_prepare. In a
696 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
697 * if the operation will never sleep. One example is a SoC-internal clk which
698 * is controlled via simple register writes. In the complex case a clk ungate
699 * operation may require a fast and a slow part. It is this reason that
700 * clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
701 * must be called before clk_enable. Returns 0 on success, -EERROR
702 * otherwise.
704 int clk_enable(struct clk *clk)
706 if (!clk)
707 return 0;
709 return clk_core_enable_lock(clk->core);
711 EXPORT_SYMBOL_GPL(clk_enable);
713 static int clk_core_prepare_enable(struct clk_core *core)
715 int ret;
717 ret = clk_core_prepare_lock(core);
718 if (ret)
719 return ret;
721 ret = clk_core_enable_lock(core);
722 if (ret)
723 clk_core_unprepare_lock(core);
725 return ret;
728 static void clk_core_disable_unprepare(struct clk_core *core)
730 clk_core_disable_lock(core);
731 clk_core_unprepare_lock(core);
734 static void clk_unprepare_unused_subtree(struct clk_core *core)
736 struct clk_core *child;
738 lockdep_assert_held(&prepare_lock);
740 hlist_for_each_entry(child, &core->children, child_node)
741 clk_unprepare_unused_subtree(child);
743 if (core->prepare_count)
744 return;
746 if (core->flags & CLK_IGNORE_UNUSED)
747 return;
749 if (clk_core_is_prepared(core)) {
750 trace_clk_unprepare(core);
751 if (core->ops->unprepare_unused)
752 core->ops->unprepare_unused(core->hw);
753 else if (core->ops->unprepare)
754 core->ops->unprepare(core->hw);
755 trace_clk_unprepare_complete(core);
759 static void clk_disable_unused_subtree(struct clk_core *core)
761 struct clk_core *child;
762 unsigned long flags;
764 lockdep_assert_held(&prepare_lock);
766 hlist_for_each_entry(child, &core->children, child_node)
767 clk_disable_unused_subtree(child);
769 if (core->flags & CLK_OPS_PARENT_ENABLE)
770 clk_core_prepare_enable(core->parent);
772 flags = clk_enable_lock();
774 if (core->enable_count)
775 goto unlock_out;
777 if (core->flags & CLK_IGNORE_UNUSED)
778 goto unlock_out;
781 * some gate clocks have special needs during the disable-unused
782 * sequence. call .disable_unused if available, otherwise fall
783 * back to .disable
785 if (clk_core_is_enabled(core)) {
786 trace_clk_disable(core);
787 if (core->ops->disable_unused)
788 core->ops->disable_unused(core->hw);
789 else if (core->ops->disable)
790 core->ops->disable(core->hw);
791 trace_clk_disable_complete(core);
794 unlock_out:
795 clk_enable_unlock(flags);
796 if (core->flags & CLK_OPS_PARENT_ENABLE)
797 clk_core_disable_unprepare(core->parent);
800 static bool clk_ignore_unused;
801 static int __init clk_ignore_unused_setup(char *__unused)
803 clk_ignore_unused = true;
804 return 1;
806 __setup("clk_ignore_unused", clk_ignore_unused_setup);
808 static int clk_disable_unused(void)
810 struct clk_core *core;
812 if (clk_ignore_unused) {
813 pr_warn("clk: Not disabling unused clocks\n");
814 return 0;
817 clk_prepare_lock();
819 hlist_for_each_entry(core, &clk_root_list, child_node)
820 clk_disable_unused_subtree(core);
822 hlist_for_each_entry(core, &clk_orphan_list, child_node)
823 clk_disable_unused_subtree(core);
825 hlist_for_each_entry(core, &clk_root_list, child_node)
826 clk_unprepare_unused_subtree(core);
828 hlist_for_each_entry(core, &clk_orphan_list, child_node)
829 clk_unprepare_unused_subtree(core);
831 clk_prepare_unlock();
833 return 0;
835 late_initcall_sync(clk_disable_unused);
837 static int clk_core_round_rate_nolock(struct clk_core *core,
838 struct clk_rate_request *req)
840 struct clk_core *parent;
841 long rate;
843 lockdep_assert_held(&prepare_lock);
845 if (!core)
846 return 0;
848 parent = core->parent;
849 if (parent) {
850 req->best_parent_hw = parent->hw;
851 req->best_parent_rate = parent->rate;
852 } else {
853 req->best_parent_hw = NULL;
854 req->best_parent_rate = 0;
857 if (core->ops->determine_rate) {
858 return core->ops->determine_rate(core->hw, req);
859 } else if (core->ops->round_rate) {
860 rate = core->ops->round_rate(core->hw, req->rate,
861 &req->best_parent_rate);
862 if (rate < 0)
863 return rate;
865 req->rate = rate;
866 } else if (core->flags & CLK_SET_RATE_PARENT) {
867 return clk_core_round_rate_nolock(parent, req);
868 } else {
869 req->rate = core->rate;
872 return 0;
876 * __clk_determine_rate - get the closest rate actually supported by a clock
877 * @hw: determine the rate of this clock
878 * @req: target rate request
880 * Useful for clk_ops such as .set_rate and .determine_rate.
882 int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
884 if (!hw) {
885 req->rate = 0;
886 return 0;
889 return clk_core_round_rate_nolock(hw->core, req);
891 EXPORT_SYMBOL_GPL(__clk_determine_rate);
893 unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
895 int ret;
896 struct clk_rate_request req;
898 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate);
899 req.rate = rate;
901 ret = clk_core_round_rate_nolock(hw->core, &req);
902 if (ret)
903 return 0;
905 return req.rate;
907 EXPORT_SYMBOL_GPL(clk_hw_round_rate);
910 * clk_round_rate - round the given rate for a clk
911 * @clk: the clk for which we are rounding a rate
912 * @rate: the rate which is to be rounded
914 * Takes in a rate as input and rounds it to a rate that the clk can actually
915 * use which is then returned. If clk doesn't support round_rate operation
916 * then the parent rate is returned.
918 long clk_round_rate(struct clk *clk, unsigned long rate)
920 struct clk_rate_request req;
921 int ret;
923 if (!clk)
924 return 0;
926 clk_prepare_lock();
928 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate);
929 req.rate = rate;
931 ret = clk_core_round_rate_nolock(clk->core, &req);
932 clk_prepare_unlock();
934 if (ret)
935 return ret;
937 return req.rate;
939 EXPORT_SYMBOL_GPL(clk_round_rate);
942 * __clk_notify - call clk notifier chain
943 * @core: clk that is changing rate
944 * @msg: clk notifier type (see include/linux/clk.h)
945 * @old_rate: old clk rate
946 * @new_rate: new clk rate
948 * Triggers a notifier call chain on the clk rate-change notification
949 * for 'clk'. Passes a pointer to the struct clk and the previous
950 * and current rates to the notifier callback. Intended to be called by
951 * internal clock code only. Returns NOTIFY_DONE from the last driver
952 * called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
953 * a driver returns that.
955 static int __clk_notify(struct clk_core *core, unsigned long msg,
956 unsigned long old_rate, unsigned long new_rate)
958 struct clk_notifier *cn;
959 struct clk_notifier_data cnd;
960 int ret = NOTIFY_DONE;
962 cnd.old_rate = old_rate;
963 cnd.new_rate = new_rate;
965 list_for_each_entry(cn, &clk_notifier_list, node) {
966 if (cn->clk->core == core) {
967 cnd.clk = cn->clk;
968 ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
969 &cnd);
970 if (ret & NOTIFY_STOP_MASK)
971 return ret;
975 return ret;
979 * __clk_recalc_accuracies
980 * @core: first clk in the subtree
982 * Walks the subtree of clks starting with clk and recalculates accuracies as
983 * it goes. Note that if a clk does not implement the .recalc_accuracy
984 * callback then it is assumed that the clock will take on the accuracy of its
985 * parent.
987 static void __clk_recalc_accuracies(struct clk_core *core)
989 unsigned long parent_accuracy = 0;
990 struct clk_core *child;
992 lockdep_assert_held(&prepare_lock);
994 if (core->parent)
995 parent_accuracy = core->parent->accuracy;
997 if (core->ops->recalc_accuracy)
998 core->accuracy = core->ops->recalc_accuracy(core->hw,
999 parent_accuracy);
1000 else
1001 core->accuracy = parent_accuracy;
1003 hlist_for_each_entry(child, &core->children, child_node)
1004 __clk_recalc_accuracies(child);
1007 static long clk_core_get_accuracy(struct clk_core *core)
1009 unsigned long accuracy;
1011 clk_prepare_lock();
1012 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
1013 __clk_recalc_accuracies(core);
1015 accuracy = __clk_get_accuracy(core);
1016 clk_prepare_unlock();
1018 return accuracy;
1022 * clk_get_accuracy - return the accuracy of clk
1023 * @clk: the clk whose accuracy is being returned
1025 * Simply returns the cached accuracy of the clk, unless
1026 * CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
1027 * issued.
1028 * If clk is NULL then returns 0.
1030 long clk_get_accuracy(struct clk *clk)
1032 if (!clk)
1033 return 0;
1035 return clk_core_get_accuracy(clk->core);
1037 EXPORT_SYMBOL_GPL(clk_get_accuracy);
1039 static unsigned long clk_recalc(struct clk_core *core,
1040 unsigned long parent_rate)
1042 if (core->ops->recalc_rate)
1043 return core->ops->recalc_rate(core->hw, parent_rate);
1044 return parent_rate;
1048 * __clk_recalc_rates
1049 * @core: first clk in the subtree
1050 * @msg: notification type (see include/linux/clk.h)
1052 * Walks the subtree of clks starting with clk and recalculates rates as it
1053 * goes. Note that if a clk does not implement the .recalc_rate callback then
1054 * it is assumed that the clock will take on the rate of its parent.
1056 * clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
1057 * if necessary.
1059 static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
1061 unsigned long old_rate;
1062 unsigned long parent_rate = 0;
1063 struct clk_core *child;
1065 lockdep_assert_held(&prepare_lock);
1067 old_rate = core->rate;
1069 if (core->parent)
1070 parent_rate = core->parent->rate;
1072 core->rate = clk_recalc(core, parent_rate);
1075 * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
1076 * & ABORT_RATE_CHANGE notifiers
1078 if (core->notifier_count && msg)
1079 __clk_notify(core, msg, old_rate, core->rate);
1081 hlist_for_each_entry(child, &core->children, child_node)
1082 __clk_recalc_rates(child, msg);
1085 static unsigned long clk_core_get_rate(struct clk_core *core)
1087 unsigned long rate;
1089 clk_prepare_lock();
1091 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
1092 __clk_recalc_rates(core, 0);
1094 rate = clk_core_get_rate_nolock(core);
1095 clk_prepare_unlock();
1097 return rate;
1101 * clk_get_rate - return the rate of clk
1102 * @clk: the clk whose rate is being returned
1104 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1105 * is set, which means a recalc_rate will be issued.
1106 * If clk is NULL then returns 0.
1108 unsigned long clk_get_rate(struct clk *clk)
1110 if (!clk)
1111 return 0;
1113 return clk_core_get_rate(clk->core);
1115 EXPORT_SYMBOL_GPL(clk_get_rate);
1117 static int clk_fetch_parent_index(struct clk_core *core,
1118 struct clk_core *parent)
1120 int i;
1122 if (!parent)
1123 return -EINVAL;
1125 for (i = 0; i < core->num_parents; i++)
1126 if (clk_core_get_parent_by_index(core, i) == parent)
1127 return i;
1129 return -EINVAL;
1133 * Update the orphan status of @core and all its children.
1135 static void clk_core_update_orphan_status(struct clk_core *core, bool is_orphan)
1137 struct clk_core *child;
1139 core->orphan = is_orphan;
1141 hlist_for_each_entry(child, &core->children, child_node)
1142 clk_core_update_orphan_status(child, is_orphan);
1145 static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
1147 bool was_orphan = core->orphan;
1149 hlist_del(&core->child_node);
1151 if (new_parent) {
1152 bool becomes_orphan = new_parent->orphan;
1154 /* avoid duplicate POST_RATE_CHANGE notifications */
1155 if (new_parent->new_child == core)
1156 new_parent->new_child = NULL;
1158 hlist_add_head(&core->child_node, &new_parent->children);
1160 if (was_orphan != becomes_orphan)
1161 clk_core_update_orphan_status(core, becomes_orphan);
1162 } else {
1163 hlist_add_head(&core->child_node, &clk_orphan_list);
1164 if (!was_orphan)
1165 clk_core_update_orphan_status(core, true);
1168 core->parent = new_parent;
1171 static struct clk_core *__clk_set_parent_before(struct clk_core *core,
1172 struct clk_core *parent)
1174 unsigned long flags;
1175 struct clk_core *old_parent = core->parent;
1178 * 1. enable parents for CLK_OPS_PARENT_ENABLE clock
1180 * 2. Migrate prepare state between parents and prevent race with
1181 * clk_enable().
1183 * If the clock is not prepared, then a race with
1184 * clk_enable/disable() is impossible since we already have the
1185 * prepare lock (future calls to clk_enable() need to be preceded by
1186 * a clk_prepare()).
1188 * If the clock is prepared, migrate the prepared state to the new
1189 * parent and also protect against a race with clk_enable() by
1190 * forcing the clock and the new parent on. This ensures that all
1191 * future calls to clk_enable() are practically NOPs with respect to
1192 * hardware and software states.
1194 * See also: Comment for clk_set_parent() below.
1197 /* enable old_parent & parent if CLK_OPS_PARENT_ENABLE is set */
1198 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1199 clk_core_prepare_enable(old_parent);
1200 clk_core_prepare_enable(parent);
1203 /* migrate prepare count if > 0 */
1204 if (core->prepare_count) {
1205 clk_core_prepare_enable(parent);
1206 clk_core_enable_lock(core);
1209 /* update the clk tree topology */
1210 flags = clk_enable_lock();
1211 clk_reparent(core, parent);
1212 clk_enable_unlock(flags);
1214 return old_parent;
1217 static void __clk_set_parent_after(struct clk_core *core,
1218 struct clk_core *parent,
1219 struct clk_core *old_parent)
1222 * Finish the migration of prepare state and undo the changes done
1223 * for preventing a race with clk_enable().
1225 if (core->prepare_count) {
1226 clk_core_disable_lock(core);
1227 clk_core_disable_unprepare(old_parent);
1230 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */
1231 if (core->flags & CLK_OPS_PARENT_ENABLE) {
1232 clk_core_disable_unprepare(parent);
1233 clk_core_disable_unprepare(old_parent);
1237 static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
1238 u8 p_index)
1240 unsigned long flags;
1241 int ret = 0;
1242 struct clk_core *old_parent;
1244 old_parent = __clk_set_parent_before(core, parent);
1246 trace_clk_set_parent(core, parent);
1248 /* change clock input source */
1249 if (parent && core->ops->set_parent)
1250 ret = core->ops->set_parent(core->hw, p_index);
1252 trace_clk_set_parent_complete(core, parent);
1254 if (ret) {
1255 flags = clk_enable_lock();
1256 clk_reparent(core, old_parent);
1257 clk_enable_unlock(flags);
1258 __clk_set_parent_after(core, old_parent, parent);
1260 return ret;
1263 __clk_set_parent_after(core, parent, old_parent);
1265 return 0;
1269 * __clk_speculate_rates
1270 * @core: first clk in the subtree
1271 * @parent_rate: the "future" rate of clk's parent
1273 * Walks the subtree of clks starting with clk, speculating rates as it
1274 * goes and firing off PRE_RATE_CHANGE notifications as necessary.
1276 * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
1277 * pre-rate change notifications and returns early if no clks in the
1278 * subtree have subscribed to the notifications. Note that if a clk does not
1279 * implement the .recalc_rate callback then it is assumed that the clock will
1280 * take on the rate of its parent.
1282 static int __clk_speculate_rates(struct clk_core *core,
1283 unsigned long parent_rate)
1285 struct clk_core *child;
1286 unsigned long new_rate;
1287 int ret = NOTIFY_DONE;
1289 lockdep_assert_held(&prepare_lock);
1291 new_rate = clk_recalc(core, parent_rate);
1293 /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
1294 if (core->notifier_count)
1295 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
1297 if (ret & NOTIFY_STOP_MASK) {
1298 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
1299 __func__, core->name, ret);
1300 goto out;
1303 hlist_for_each_entry(child, &core->children, child_node) {
1304 ret = __clk_speculate_rates(child, new_rate);
1305 if (ret & NOTIFY_STOP_MASK)
1306 break;
1309 out:
1310 return ret;
1313 static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
1314 struct clk_core *new_parent, u8 p_index)
1316 struct clk_core *child;
1318 core->new_rate = new_rate;
1319 core->new_parent = new_parent;
1320 core->new_parent_index = p_index;
1321 /* include clk in new parent's PRE_RATE_CHANGE notifications */
1322 core->new_child = NULL;
1323 if (new_parent && new_parent != core->parent)
1324 new_parent->new_child = core;
1326 hlist_for_each_entry(child, &core->children, child_node) {
1327 child->new_rate = clk_recalc(child, new_rate);
1328 clk_calc_subtree(child, child->new_rate, NULL, 0);
1333 * calculate the new rates returning the topmost clock that has to be
1334 * changed.
1336 static struct clk_core *clk_calc_new_rates(struct clk_core *core,
1337 unsigned long rate)
1339 struct clk_core *top = core;
1340 struct clk_core *old_parent, *parent;
1341 unsigned long best_parent_rate = 0;
1342 unsigned long new_rate;
1343 unsigned long min_rate;
1344 unsigned long max_rate;
1345 int p_index = 0;
1346 long ret;
1348 /* sanity */
1349 if (IS_ERR_OR_NULL(core))
1350 return NULL;
1352 /* save parent rate, if it exists */
1353 parent = old_parent = core->parent;
1354 if (parent)
1355 best_parent_rate = parent->rate;
1357 clk_core_get_boundaries(core, &min_rate, &max_rate);
1359 /* find the closest rate and parent clk/rate */
1360 if (core->ops->determine_rate) {
1361 struct clk_rate_request req;
1363 req.rate = rate;
1364 req.min_rate = min_rate;
1365 req.max_rate = max_rate;
1366 if (parent) {
1367 req.best_parent_hw = parent->hw;
1368 req.best_parent_rate = parent->rate;
1369 } else {
1370 req.best_parent_hw = NULL;
1371 req.best_parent_rate = 0;
1374 ret = core->ops->determine_rate(core->hw, &req);
1375 if (ret < 0)
1376 return NULL;
1378 best_parent_rate = req.best_parent_rate;
1379 new_rate = req.rate;
1380 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL;
1381 } else if (core->ops->round_rate) {
1382 ret = core->ops->round_rate(core->hw, rate,
1383 &best_parent_rate);
1384 if (ret < 0)
1385 return NULL;
1387 new_rate = ret;
1388 if (new_rate < min_rate || new_rate > max_rate)
1389 return NULL;
1390 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
1391 /* pass-through clock without adjustable parent */
1392 core->new_rate = core->rate;
1393 return NULL;
1394 } else {
1395 /* pass-through clock with adjustable parent */
1396 top = clk_calc_new_rates(parent, rate);
1397 new_rate = parent->new_rate;
1398 goto out;
1401 /* some clocks must be gated to change parent */
1402 if (parent != old_parent &&
1403 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
1404 pr_debug("%s: %s not gated but wants to reparent\n",
1405 __func__, core->name);
1406 return NULL;
1409 /* try finding the new parent index */
1410 if (parent && core->num_parents > 1) {
1411 p_index = clk_fetch_parent_index(core, parent);
1412 if (p_index < 0) {
1413 pr_debug("%s: clk %s can not be parent of clk %s\n",
1414 __func__, parent->name, core->name);
1415 return NULL;
1419 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
1420 best_parent_rate != parent->rate)
1421 top = clk_calc_new_rates(parent, best_parent_rate);
1423 out:
1424 clk_calc_subtree(core, new_rate, parent, p_index);
1426 return top;
1430 * Notify about rate changes in a subtree. Always walk down the whole tree
1431 * so that in case of an error we can walk down the whole tree again and
1432 * abort the change.
1434 static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
1435 unsigned long event)
1437 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
1438 int ret = NOTIFY_DONE;
1440 if (core->rate == core->new_rate)
1441 return NULL;
1443 if (core->notifier_count) {
1444 ret = __clk_notify(core, event, core->rate, core->new_rate);
1445 if (ret & NOTIFY_STOP_MASK)
1446 fail_clk = core;
1449 hlist_for_each_entry(child, &core->children, child_node) {
1450 /* Skip children who will be reparented to another clock */
1451 if (child->new_parent && child->new_parent != core)
1452 continue;
1453 tmp_clk = clk_propagate_rate_change(child, event);
1454 if (tmp_clk)
1455 fail_clk = tmp_clk;
1458 /* handle the new child who might not be in core->children yet */
1459 if (core->new_child) {
1460 tmp_clk = clk_propagate_rate_change(core->new_child, event);
1461 if (tmp_clk)
1462 fail_clk = tmp_clk;
1465 return fail_clk;
1469 * walk down a subtree and set the new rates notifying the rate
1470 * change on the way
1472 static void clk_change_rate(struct clk_core *core)
1474 struct clk_core *child;
1475 struct hlist_node *tmp;
1476 unsigned long old_rate;
1477 unsigned long best_parent_rate = 0;
1478 bool skip_set_rate = false;
1479 struct clk_core *old_parent;
1480 struct clk_core *parent = NULL;
1482 old_rate = core->rate;
1484 if (core->new_parent) {
1485 parent = core->new_parent;
1486 best_parent_rate = core->new_parent->rate;
1487 } else if (core->parent) {
1488 parent = core->parent;
1489 best_parent_rate = core->parent->rate;
1492 if (core->flags & CLK_SET_RATE_UNGATE) {
1493 unsigned long flags;
1495 clk_core_prepare(core);
1496 flags = clk_enable_lock();
1497 clk_core_enable(core);
1498 clk_enable_unlock(flags);
1501 if (core->new_parent && core->new_parent != core->parent) {
1502 old_parent = __clk_set_parent_before(core, core->new_parent);
1503 trace_clk_set_parent(core, core->new_parent);
1505 if (core->ops->set_rate_and_parent) {
1506 skip_set_rate = true;
1507 core->ops->set_rate_and_parent(core->hw, core->new_rate,
1508 best_parent_rate,
1509 core->new_parent_index);
1510 } else if (core->ops->set_parent) {
1511 core->ops->set_parent(core->hw, core->new_parent_index);
1514 trace_clk_set_parent_complete(core, core->new_parent);
1515 __clk_set_parent_after(core, core->new_parent, old_parent);
1518 if (core->flags & CLK_OPS_PARENT_ENABLE)
1519 clk_core_prepare_enable(parent);
1521 trace_clk_set_rate(core, core->new_rate);
1523 if (!skip_set_rate && core->ops->set_rate)
1524 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
1526 trace_clk_set_rate_complete(core, core->new_rate);
1528 core->rate = clk_recalc(core, best_parent_rate);
1530 if (core->flags & CLK_SET_RATE_UNGATE) {
1531 unsigned long flags;
1533 flags = clk_enable_lock();
1534 clk_core_disable(core);
1535 clk_enable_unlock(flags);
1536 clk_core_unprepare(core);
1539 if (core->flags & CLK_OPS_PARENT_ENABLE)
1540 clk_core_disable_unprepare(parent);
1542 if (core->notifier_count && old_rate != core->rate)
1543 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
1545 if (core->flags & CLK_RECALC_NEW_RATES)
1546 (void)clk_calc_new_rates(core, core->new_rate);
1549 * Use safe iteration, as change_rate can actually swap parents
1550 * for certain clock types.
1552 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
1553 /* Skip children who will be reparented to another clock */
1554 if (child->new_parent && child->new_parent != core)
1555 continue;
1556 clk_change_rate(child);
1559 /* handle the new child who might not be in core->children yet */
1560 if (core->new_child)
1561 clk_change_rate(core->new_child);
1564 static int clk_core_set_rate_nolock(struct clk_core *core,
1565 unsigned long req_rate)
1567 struct clk_core *top, *fail_clk;
1568 unsigned long rate = req_rate;
1570 if (!core)
1571 return 0;
1573 /* bail early if nothing to do */
1574 if (rate == clk_core_get_rate_nolock(core))
1575 return 0;
1577 if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
1578 return -EBUSY;
1580 /* calculate new rates and get the topmost changed clock */
1581 top = clk_calc_new_rates(core, rate);
1582 if (!top)
1583 return -EINVAL;
1585 /* notify that we are about to change rates */
1586 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
1587 if (fail_clk) {
1588 pr_debug("%s: failed to set %s rate\n", __func__,
1589 fail_clk->name);
1590 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
1591 return -EBUSY;
1594 /* change the rates */
1595 clk_change_rate(top);
1597 core->req_rate = req_rate;
1599 return 0;
1603 * clk_set_rate - specify a new rate for clk
1604 * @clk: the clk whose rate is being changed
1605 * @rate: the new rate for clk
1607 * In the simplest case clk_set_rate will only adjust the rate of clk.
1609 * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
1610 * propagate up to clk's parent; whether or not this happens depends on the
1611 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
1612 * after calling .round_rate then upstream parent propagation is ignored. If
1613 * *parent_rate comes back with a new rate for clk's parent then we propagate
1614 * up to clk's parent and set its rate. Upward propagation will continue
1615 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
1616 * .round_rate stops requesting changes to clk's parent_rate.
1618 * Rate changes are accomplished via tree traversal that also recalculates the
1619 * rates for the clocks and fires off POST_RATE_CHANGE notifiers.
1621 * Returns 0 on success, -EERROR otherwise.
1623 int clk_set_rate(struct clk *clk, unsigned long rate)
1625 int ret;
1627 if (!clk)
1628 return 0;
1630 /* prevent racing with updates to the clock topology */
1631 clk_prepare_lock();
1633 ret = clk_core_set_rate_nolock(clk->core, rate);
1635 clk_prepare_unlock();
1637 return ret;
1639 EXPORT_SYMBOL_GPL(clk_set_rate);
1642 * clk_set_rate_range - set a rate range for a clock source
1643 * @clk: clock source
1644 * @min: desired minimum clock rate in Hz, inclusive
1645 * @max: desired maximum clock rate in Hz, inclusive
1647 * Returns success (0) or negative errno.
1649 int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
1651 int ret = 0;
1653 if (!clk)
1654 return 0;
1656 if (min > max) {
1657 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
1658 __func__, clk->core->name, clk->dev_id, clk->con_id,
1659 min, max);
1660 return -EINVAL;
1663 clk_prepare_lock();
1665 if (min != clk->min_rate || max != clk->max_rate) {
1666 clk->min_rate = min;
1667 clk->max_rate = max;
1668 ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
1671 clk_prepare_unlock();
1673 return ret;
1675 EXPORT_SYMBOL_GPL(clk_set_rate_range);
1678 * clk_set_min_rate - set a minimum clock rate for a clock source
1679 * @clk: clock source
1680 * @rate: desired minimum clock rate in Hz, inclusive
1682 * Returns success (0) or negative errno.
1684 int clk_set_min_rate(struct clk *clk, unsigned long rate)
1686 if (!clk)
1687 return 0;
1689 return clk_set_rate_range(clk, rate, clk->max_rate);
1691 EXPORT_SYMBOL_GPL(clk_set_min_rate);
1694 * clk_set_max_rate - set a maximum clock rate for a clock source
1695 * @clk: clock source
1696 * @rate: desired maximum clock rate in Hz, inclusive
1698 * Returns success (0) or negative errno.
1700 int clk_set_max_rate(struct clk *clk, unsigned long rate)
1702 if (!clk)
1703 return 0;
1705 return clk_set_rate_range(clk, clk->min_rate, rate);
1707 EXPORT_SYMBOL_GPL(clk_set_max_rate);
1710 * clk_get_parent - return the parent of a clk
1711 * @clk: the clk whose parent gets returned
1713 * Simply returns clk->parent. Returns NULL if clk is NULL.
1715 struct clk *clk_get_parent(struct clk *clk)
1717 struct clk *parent;
1719 if (!clk)
1720 return NULL;
1722 clk_prepare_lock();
1723 /* TODO: Create a per-user clk and change callers to call clk_put */
1724 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk;
1725 clk_prepare_unlock();
1727 return parent;
1729 EXPORT_SYMBOL_GPL(clk_get_parent);
1731 static struct clk_core *__clk_init_parent(struct clk_core *core)
1733 u8 index = 0;
1735 if (core->num_parents > 1 && core->ops->get_parent)
1736 index = core->ops->get_parent(core->hw);
1738 return clk_core_get_parent_by_index(core, index);
1741 static void clk_core_reparent(struct clk_core *core,
1742 struct clk_core *new_parent)
1744 clk_reparent(core, new_parent);
1745 __clk_recalc_accuracies(core);
1746 __clk_recalc_rates(core, POST_RATE_CHANGE);
1749 void clk_hw_reparent(struct clk_hw *hw, struct clk_hw *new_parent)
1751 if (!hw)
1752 return;
1754 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core);
1758 * clk_has_parent - check if a clock is a possible parent for another
1759 * @clk: clock source
1760 * @parent: parent clock source
1762 * This function can be used in drivers that need to check that a clock can be
1763 * the parent of another without actually changing the parent.
1765 * Returns true if @parent is a possible parent for @clk, false otherwise.
1767 bool clk_has_parent(struct clk *clk, struct clk *parent)
1769 struct clk_core *core, *parent_core;
1770 unsigned int i;
1772 /* NULL clocks should be nops, so return success if either is NULL. */
1773 if (!clk || !parent)
1774 return true;
1776 core = clk->core;
1777 parent_core = parent->core;
1779 /* Optimize for the case where the parent is already the parent. */
1780 if (core->parent == parent_core)
1781 return true;
1783 for (i = 0; i < core->num_parents; i++)
1784 if (strcmp(core->parent_names[i], parent_core->name) == 0)
1785 return true;
1787 return false;
1789 EXPORT_SYMBOL_GPL(clk_has_parent);
1791 static int clk_core_set_parent(struct clk_core *core, struct clk_core *parent)
1793 int ret = 0;
1794 int p_index = 0;
1795 unsigned long p_rate = 0;
1797 if (!core)
1798 return 0;
1800 /* prevent racing with updates to the clock topology */
1801 clk_prepare_lock();
1803 if (core->parent == parent)
1804 goto out;
1806 /* verify ops for for multi-parent clks */
1807 if ((core->num_parents > 1) && (!core->ops->set_parent)) {
1808 ret = -ENOSYS;
1809 goto out;
1812 /* check that we are allowed to re-parent if the clock is in use */
1813 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
1814 ret = -EBUSY;
1815 goto out;
1818 /* try finding the new parent index */
1819 if (parent) {
1820 p_index = clk_fetch_parent_index(core, parent);
1821 if (p_index < 0) {
1822 pr_debug("%s: clk %s can not be parent of clk %s\n",
1823 __func__, parent->name, core->name);
1824 ret = p_index;
1825 goto out;
1827 p_rate = parent->rate;
1830 /* propagate PRE_RATE_CHANGE notifications */
1831 ret = __clk_speculate_rates(core, p_rate);
1833 /* abort if a driver objects */
1834 if (ret & NOTIFY_STOP_MASK)
1835 goto out;
1837 /* do the re-parent */
1838 ret = __clk_set_parent(core, parent, p_index);
1840 /* propagate rate an accuracy recalculation accordingly */
1841 if (ret) {
1842 __clk_recalc_rates(core, ABORT_RATE_CHANGE);
1843 } else {
1844 __clk_recalc_rates(core, POST_RATE_CHANGE);
1845 __clk_recalc_accuracies(core);
1848 out:
1849 clk_prepare_unlock();
1851 return ret;
1855 * clk_set_parent - switch the parent of a mux clk
1856 * @clk: the mux clk whose input we are switching
1857 * @parent: the new input to clk
1859 * Re-parent clk to use parent as its new input source. If clk is in
1860 * prepared state, the clk will get enabled for the duration of this call. If
1861 * that's not acceptable for a specific clk (Eg: the consumer can't handle
1862 * that, the reparenting is glitchy in hardware, etc), use the
1863 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
1865 * After successfully changing clk's parent clk_set_parent will update the
1866 * clk topology, sysfs topology and propagate rate recalculation via
1867 * __clk_recalc_rates.
1869 * Returns 0 on success, -EERROR otherwise.
1871 int clk_set_parent(struct clk *clk, struct clk *parent)
1873 if (!clk)
1874 return 0;
1876 return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
1878 EXPORT_SYMBOL_GPL(clk_set_parent);
1881 * clk_set_phase - adjust the phase shift of a clock signal
1882 * @clk: clock signal source
1883 * @degrees: number of degrees the signal is shifted
1885 * Shifts the phase of a clock signal by the specified
1886 * degrees. Returns 0 on success, -EERROR otherwise.
1888 * This function makes no distinction about the input or reference
1889 * signal that we adjust the clock signal phase against. For example
1890 * phase locked-loop clock signal generators we may shift phase with
1891 * respect to feedback clock signal input, but for other cases the
1892 * clock phase may be shifted with respect to some other, unspecified
1893 * signal.
1895 * Additionally the concept of phase shift does not propagate through
1896 * the clock tree hierarchy, which sets it apart from clock rates and
1897 * clock accuracy. A parent clock phase attribute does not have an
1898 * impact on the phase attribute of a child clock.
1900 int clk_set_phase(struct clk *clk, int degrees)
1902 int ret = -EINVAL;
1904 if (!clk)
1905 return 0;
1907 /* sanity check degrees */
1908 degrees %= 360;
1909 if (degrees < 0)
1910 degrees += 360;
1912 clk_prepare_lock();
1914 trace_clk_set_phase(clk->core, degrees);
1916 if (clk->core->ops->set_phase)
1917 ret = clk->core->ops->set_phase(clk->core->hw, degrees);
1919 trace_clk_set_phase_complete(clk->core, degrees);
1921 if (!ret)
1922 clk->core->phase = degrees;
1924 clk_prepare_unlock();
1926 return ret;
1928 EXPORT_SYMBOL_GPL(clk_set_phase);
1930 static int clk_core_get_phase(struct clk_core *core)
1932 int ret;
1934 clk_prepare_lock();
1935 /* Always try to update cached phase if possible */
1936 if (core->ops->get_phase)
1937 core->phase = core->ops->get_phase(core->hw);
1938 ret = core->phase;
1939 clk_prepare_unlock();
1941 return ret;
1945 * clk_get_phase - return the phase shift of a clock signal
1946 * @clk: clock signal source
1948 * Returns the phase shift of a clock node in degrees, otherwise returns
1949 * -EERROR.
1951 int clk_get_phase(struct clk *clk)
1953 if (!clk)
1954 return 0;
1956 return clk_core_get_phase(clk->core);
1958 EXPORT_SYMBOL_GPL(clk_get_phase);
1961 * clk_is_match - check if two clk's point to the same hardware clock
1962 * @p: clk compared against q
1963 * @q: clk compared against p
1965 * Returns true if the two struct clk pointers both point to the same hardware
1966 * clock node. Put differently, returns true if struct clk *p and struct clk *q
1967 * share the same struct clk_core object.
1969 * Returns false otherwise. Note that two NULL clks are treated as matching.
1971 bool clk_is_match(const struct clk *p, const struct clk *q)
1973 /* trivial case: identical struct clk's or both NULL */
1974 if (p == q)
1975 return true;
1977 /* true if clk->core pointers match. Avoid dereferencing garbage */
1978 if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
1979 if (p->core == q->core)
1980 return true;
1982 return false;
1984 EXPORT_SYMBOL_GPL(clk_is_match);
1986 /*** debugfs support ***/
1988 #ifdef CONFIG_DEBUG_FS
1989 #include <linux/debugfs.h>
1991 static struct dentry *rootdir;
1992 static int inited = 0;
1993 static DEFINE_MUTEX(clk_debug_lock);
1994 static HLIST_HEAD(clk_debug_list);
1996 static struct hlist_head *all_lists[] = {
1997 &clk_root_list,
1998 &clk_orphan_list,
1999 NULL,
2002 static struct hlist_head *orphan_list[] = {
2003 &clk_orphan_list,
2004 NULL,
2007 static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
2008 int level)
2010 if (!c)
2011 return;
2013 seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
2014 level * 3 + 1, "",
2015 30 - level * 3, c->name,
2016 c->enable_count, c->prepare_count, clk_core_get_rate(c),
2017 clk_core_get_accuracy(c), clk_core_get_phase(c));
2020 static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
2021 int level)
2023 struct clk_core *child;
2025 if (!c)
2026 return;
2028 clk_summary_show_one(s, c, level);
2030 hlist_for_each_entry(child, &c->children, child_node)
2031 clk_summary_show_subtree(s, child, level + 1);
2034 static int clk_summary_show(struct seq_file *s, void *data)
2036 struct clk_core *c;
2037 struct hlist_head **lists = (struct hlist_head **)s->private;
2039 seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
2040 seq_puts(s, "----------------------------------------------------------------------------------------\n");
2042 clk_prepare_lock();
2044 for (; *lists; lists++)
2045 hlist_for_each_entry(c, *lists, child_node)
2046 clk_summary_show_subtree(s, c, 0);
2048 clk_prepare_unlock();
2050 return 0;
2054 static int clk_summary_open(struct inode *inode, struct file *file)
2056 return single_open(file, clk_summary_show, inode->i_private);
2059 static const struct file_operations clk_summary_fops = {
2060 .open = clk_summary_open,
2061 .read = seq_read,
2062 .llseek = seq_lseek,
2063 .release = single_release,
2066 static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
2068 if (!c)
2069 return;
2071 /* This should be JSON format, i.e. elements separated with a comma */
2072 seq_printf(s, "\"%s\": { ", c->name);
2073 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
2074 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
2075 seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
2076 seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
2077 seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
2080 static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
2082 struct clk_core *child;
2084 if (!c)
2085 return;
2087 clk_dump_one(s, c, level);
2089 hlist_for_each_entry(child, &c->children, child_node) {
2090 seq_putc(s, ',');
2091 clk_dump_subtree(s, child, level + 1);
2094 seq_putc(s, '}');
2097 static int clk_dump(struct seq_file *s, void *data)
2099 struct clk_core *c;
2100 bool first_node = true;
2101 struct hlist_head **lists = (struct hlist_head **)s->private;
2103 seq_putc(s, '{');
2104 clk_prepare_lock();
2106 for (; *lists; lists++) {
2107 hlist_for_each_entry(c, *lists, child_node) {
2108 if (!first_node)
2109 seq_putc(s, ',');
2110 first_node = false;
2111 clk_dump_subtree(s, c, 0);
2115 clk_prepare_unlock();
2117 seq_puts(s, "}\n");
2118 return 0;
2122 static int clk_dump_open(struct inode *inode, struct file *file)
2124 return single_open(file, clk_dump, inode->i_private);
2127 static const struct file_operations clk_dump_fops = {
2128 .open = clk_dump_open,
2129 .read = seq_read,
2130 .llseek = seq_lseek,
2131 .release = single_release,
2134 static int possible_parents_dump(struct seq_file *s, void *data)
2136 struct clk_core *core = s->private;
2137 int i;
2139 for (i = 0; i < core->num_parents - 1; i++)
2140 seq_printf(s, "%s ", core->parent_names[i]);
2142 seq_printf(s, "%s\n", core->parent_names[i]);
2144 return 0;
2147 static int possible_parents_open(struct inode *inode, struct file *file)
2149 return single_open(file, possible_parents_dump, inode->i_private);
2152 static const struct file_operations possible_parents_fops = {
2153 .open = possible_parents_open,
2154 .read = seq_read,
2155 .llseek = seq_lseek,
2156 .release = single_release,
2159 static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
2161 struct dentry *d;
2162 int ret = -ENOMEM;
2164 if (!core || !pdentry) {
2165 ret = -EINVAL;
2166 goto out;
2169 d = debugfs_create_dir(core->name, pdentry);
2170 if (!d)
2171 goto out;
2173 core->dentry = d;
2175 d = debugfs_create_u32("clk_rate", S_IRUGO, core->dentry,
2176 (u32 *)&core->rate);
2177 if (!d)
2178 goto err_out;
2180 d = debugfs_create_u32("clk_accuracy", S_IRUGO, core->dentry,
2181 (u32 *)&core->accuracy);
2182 if (!d)
2183 goto err_out;
2185 d = debugfs_create_u32("clk_phase", S_IRUGO, core->dentry,
2186 (u32 *)&core->phase);
2187 if (!d)
2188 goto err_out;
2190 d = debugfs_create_x32("clk_flags", S_IRUGO, core->dentry,
2191 (u32 *)&core->flags);
2192 if (!d)
2193 goto err_out;
2195 d = debugfs_create_u32("clk_prepare_count", S_IRUGO, core->dentry,
2196 (u32 *)&core->prepare_count);
2197 if (!d)
2198 goto err_out;
2200 d = debugfs_create_u32("clk_enable_count", S_IRUGO, core->dentry,
2201 (u32 *)&core->enable_count);
2202 if (!d)
2203 goto err_out;
2205 d = debugfs_create_u32("clk_notifier_count", S_IRUGO, core->dentry,
2206 (u32 *)&core->notifier_count);
2207 if (!d)
2208 goto err_out;
2210 if (core->num_parents > 1) {
2211 d = debugfs_create_file("clk_possible_parents", S_IRUGO,
2212 core->dentry, core, &possible_parents_fops);
2213 if (!d)
2214 goto err_out;
2217 if (core->ops->debug_init) {
2218 ret = core->ops->debug_init(core->hw, core->dentry);
2219 if (ret)
2220 goto err_out;
2223 ret = 0;
2224 goto out;
2226 err_out:
2227 debugfs_remove_recursive(core->dentry);
2228 core->dentry = NULL;
2229 out:
2230 return ret;
2234 * clk_debug_register - add a clk node to the debugfs clk directory
2235 * @core: the clk being added to the debugfs clk directory
2237 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
2238 * initialized. Otherwise it bails out early since the debugfs clk directory
2239 * will be created lazily by clk_debug_init as part of a late_initcall.
2241 static int clk_debug_register(struct clk_core *core)
2243 int ret = 0;
2245 mutex_lock(&clk_debug_lock);
2246 hlist_add_head(&core->debug_node, &clk_debug_list);
2248 if (!inited)
2249 goto unlock;
2251 ret = clk_debug_create_one(core, rootdir);
2252 unlock:
2253 mutex_unlock(&clk_debug_lock);
2255 return ret;
2259 * clk_debug_unregister - remove a clk node from the debugfs clk directory
2260 * @core: the clk being removed from the debugfs clk directory
2262 * Dynamically removes a clk and all its child nodes from the
2263 * debugfs clk directory if clk->dentry points to debugfs created by
2264 * clk_debug_register in __clk_core_init.
2266 static void clk_debug_unregister(struct clk_core *core)
2268 mutex_lock(&clk_debug_lock);
2269 hlist_del_init(&core->debug_node);
2270 debugfs_remove_recursive(core->dentry);
2271 core->dentry = NULL;
2272 mutex_unlock(&clk_debug_lock);
2275 struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
2276 void *data, const struct file_operations *fops)
2278 struct dentry *d = NULL;
2280 if (hw->core->dentry)
2281 d = debugfs_create_file(name, mode, hw->core->dentry, data,
2282 fops);
2284 return d;
2286 EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
2289 * clk_debug_init - lazily populate the debugfs clk directory
2291 * clks are often initialized very early during boot before memory can be
2292 * dynamically allocated and well before debugfs is setup. This function
2293 * populates the debugfs clk directory once at boot-time when we know that
2294 * debugfs is setup. It should only be called once at boot-time, all other clks
2295 * added dynamically will be done so with clk_debug_register.
2297 static int __init clk_debug_init(void)
2299 struct clk_core *core;
2300 struct dentry *d;
2302 rootdir = debugfs_create_dir("clk", NULL);
2304 if (!rootdir)
2305 return -ENOMEM;
2307 d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists,
2308 &clk_summary_fops);
2309 if (!d)
2310 return -ENOMEM;
2312 d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists,
2313 &clk_dump_fops);
2314 if (!d)
2315 return -ENOMEM;
2317 d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir,
2318 &orphan_list, &clk_summary_fops);
2319 if (!d)
2320 return -ENOMEM;
2322 d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir,
2323 &orphan_list, &clk_dump_fops);
2324 if (!d)
2325 return -ENOMEM;
2327 mutex_lock(&clk_debug_lock);
2328 hlist_for_each_entry(core, &clk_debug_list, debug_node)
2329 clk_debug_create_one(core, rootdir);
2331 inited = 1;
2332 mutex_unlock(&clk_debug_lock);
2334 return 0;
2336 late_initcall(clk_debug_init);
2337 #else
2338 static inline int clk_debug_register(struct clk_core *core) { return 0; }
2339 static inline void clk_debug_reparent(struct clk_core *core,
2340 struct clk_core *new_parent)
2343 static inline void clk_debug_unregister(struct clk_core *core)
2346 #endif
2349 * __clk_core_init - initialize the data structures in a struct clk_core
2350 * @core: clk_core being initialized
2352 * Initializes the lists in struct clk_core, queries the hardware for the
2353 * parent and rate and sets them both.
2355 static int __clk_core_init(struct clk_core *core)
2357 int i, ret = 0;
2358 struct clk_core *orphan;
2359 struct hlist_node *tmp2;
2360 unsigned long rate;
2362 if (!core)
2363 return -EINVAL;
2365 clk_prepare_lock();
2367 /* check to see if a clock with this name is already registered */
2368 if (clk_core_lookup(core->name)) {
2369 pr_debug("%s: clk %s already initialized\n",
2370 __func__, core->name);
2371 ret = -EEXIST;
2372 goto out;
2375 /* check that clk_ops are sane. See Documentation/clk.txt */
2376 if (core->ops->set_rate &&
2377 !((core->ops->round_rate || core->ops->determine_rate) &&
2378 core->ops->recalc_rate)) {
2379 pr_err("%s: %s must implement .round_rate or .determine_rate in addition to .recalc_rate\n",
2380 __func__, core->name);
2381 ret = -EINVAL;
2382 goto out;
2385 if (core->ops->set_parent && !core->ops->get_parent) {
2386 pr_err("%s: %s must implement .get_parent & .set_parent\n",
2387 __func__, core->name);
2388 ret = -EINVAL;
2389 goto out;
2392 if (core->num_parents > 1 && !core->ops->get_parent) {
2393 pr_err("%s: %s must implement .get_parent as it has multi parents\n",
2394 __func__, core->name);
2395 ret = -EINVAL;
2396 goto out;
2399 if (core->ops->set_rate_and_parent &&
2400 !(core->ops->set_parent && core->ops->set_rate)) {
2401 pr_err("%s: %s must implement .set_parent & .set_rate\n",
2402 __func__, core->name);
2403 ret = -EINVAL;
2404 goto out;
2407 /* throw a WARN if any entries in parent_names are NULL */
2408 for (i = 0; i < core->num_parents; i++)
2409 WARN(!core->parent_names[i],
2410 "%s: invalid NULL in %s's .parent_names\n",
2411 __func__, core->name);
2413 core->parent = __clk_init_parent(core);
2416 * Populate core->parent if parent has already been clk_core_init'd. If
2417 * parent has not yet been clk_core_init'd then place clk in the orphan
2418 * list. If clk doesn't have any parents then place it in the root
2419 * clk list.
2421 * Every time a new clk is clk_init'd then we walk the list of orphan
2422 * clocks and re-parent any that are children of the clock currently
2423 * being clk_init'd.
2425 if (core->parent) {
2426 hlist_add_head(&core->child_node,
2427 &core->parent->children);
2428 core->orphan = core->parent->orphan;
2429 } else if (!core->num_parents) {
2430 hlist_add_head(&core->child_node, &clk_root_list);
2431 core->orphan = false;
2432 } else {
2433 hlist_add_head(&core->child_node, &clk_orphan_list);
2434 core->orphan = true;
2438 * Set clk's accuracy. The preferred method is to use
2439 * .recalc_accuracy. For simple clocks and lazy developers the default
2440 * fallback is to use the parent's accuracy. If a clock doesn't have a
2441 * parent (or is orphaned) then accuracy is set to zero (perfect
2442 * clock).
2444 if (core->ops->recalc_accuracy)
2445 core->accuracy = core->ops->recalc_accuracy(core->hw,
2446 __clk_get_accuracy(core->parent));
2447 else if (core->parent)
2448 core->accuracy = core->parent->accuracy;
2449 else
2450 core->accuracy = 0;
2453 * Set clk's phase.
2454 * Since a phase is by definition relative to its parent, just
2455 * query the current clock phase, or just assume it's in phase.
2457 if (core->ops->get_phase)
2458 core->phase = core->ops->get_phase(core->hw);
2459 else
2460 core->phase = 0;
2463 * Set clk's rate. The preferred method is to use .recalc_rate. For
2464 * simple clocks and lazy developers the default fallback is to use the
2465 * parent's rate. If a clock doesn't have a parent (or is orphaned)
2466 * then rate is set to zero.
2468 if (core->ops->recalc_rate)
2469 rate = core->ops->recalc_rate(core->hw,
2470 clk_core_get_rate_nolock(core->parent));
2471 else if (core->parent)
2472 rate = core->parent->rate;
2473 else
2474 rate = 0;
2475 core->rate = core->req_rate = rate;
2478 * Enable CLK_IS_CRITICAL clocks so newly added critical clocks
2479 * don't get accidentally disabled when walking the orphan tree and
2480 * reparenting clocks
2482 if (core->flags & CLK_IS_CRITICAL) {
2483 unsigned long flags;
2485 clk_core_prepare(core);
2487 flags = clk_enable_lock();
2488 clk_core_enable(core);
2489 clk_enable_unlock(flags);
2493 * walk the list of orphan clocks and reparent any that newly finds a
2494 * parent.
2496 hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
2497 struct clk_core *parent = __clk_init_parent(orphan);
2500 * We need to use __clk_set_parent_before() and _after() to
2501 * to properly migrate any prepare/enable count of the orphan
2502 * clock. This is important for CLK_IS_CRITICAL clocks, which
2503 * are enabled during init but might not have a parent yet.
2505 if (parent) {
2506 /* update the clk tree topology */
2507 __clk_set_parent_before(orphan, parent);
2508 __clk_set_parent_after(orphan, parent, NULL);
2509 __clk_recalc_accuracies(orphan);
2510 __clk_recalc_rates(orphan, 0);
2515 * optional platform-specific magic
2517 * The .init callback is not used by any of the basic clock types, but
2518 * exists for weird hardware that must perform initialization magic.
2519 * Please consider other ways of solving initialization problems before
2520 * using this callback, as its use is discouraged.
2522 if (core->ops->init)
2523 core->ops->init(core->hw);
2525 kref_init(&core->ref);
2526 out:
2527 clk_prepare_unlock();
2529 if (!ret)
2530 clk_debug_register(core);
2532 return ret;
2535 struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
2536 const char *con_id)
2538 struct clk *clk;
2540 /* This is to allow this function to be chained to others */
2541 if (IS_ERR_OR_NULL(hw))
2542 return ERR_CAST(hw);
2544 clk = kzalloc(sizeof(*clk), GFP_KERNEL);
2545 if (!clk)
2546 return ERR_PTR(-ENOMEM);
2548 clk->core = hw->core;
2549 clk->dev_id = dev_id;
2550 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
2551 clk->max_rate = ULONG_MAX;
2553 clk_prepare_lock();
2554 hlist_add_head(&clk->clks_node, &hw->core->clks);
2555 clk_prepare_unlock();
2557 return clk;
2560 /* keep in sync with __clk_put */
2561 void __clk_free_clk(struct clk *clk)
2563 clk_prepare_lock();
2564 hlist_del(&clk->clks_node);
2565 clk_prepare_unlock();
2567 kfree_const(clk->con_id);
2568 kfree(clk);
2572 * clk_register - allocate a new clock, register it and return an opaque cookie
2573 * @dev: device that is registering this clock
2574 * @hw: link to hardware-specific clock data
2576 * clk_register is the primary interface for populating the clock tree with new
2577 * clock nodes. It returns a pointer to the newly allocated struct clk which
2578 * cannot be dereferenced by driver code but may be used in conjunction with the
2579 * rest of the clock API. In the event of an error clk_register will return an
2580 * error code; drivers must test for an error code after calling clk_register.
2582 struct clk *clk_register(struct device *dev, struct clk_hw *hw)
2584 int i, ret;
2585 struct clk_core *core;
2587 core = kzalloc(sizeof(*core), GFP_KERNEL);
2588 if (!core) {
2589 ret = -ENOMEM;
2590 goto fail_out;
2593 core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
2594 if (!core->name) {
2595 ret = -ENOMEM;
2596 goto fail_name;
2598 core->ops = hw->init->ops;
2599 if (dev && dev->driver)
2600 core->owner = dev->driver->owner;
2601 core->hw = hw;
2602 core->flags = hw->init->flags;
2603 core->num_parents = hw->init->num_parents;
2604 core->min_rate = 0;
2605 core->max_rate = ULONG_MAX;
2606 hw->core = core;
2608 /* allocate local copy in case parent_names is __initdata */
2609 core->parent_names = kcalloc(core->num_parents, sizeof(char *),
2610 GFP_KERNEL);
2612 if (!core->parent_names) {
2613 ret = -ENOMEM;
2614 goto fail_parent_names;
2618 /* copy each string name in case parent_names is __initdata */
2619 for (i = 0; i < core->num_parents; i++) {
2620 core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
2621 GFP_KERNEL);
2622 if (!core->parent_names[i]) {
2623 ret = -ENOMEM;
2624 goto fail_parent_names_copy;
2628 /* avoid unnecessary string look-ups of clk_core's possible parents. */
2629 core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
2630 GFP_KERNEL);
2631 if (!core->parents) {
2632 ret = -ENOMEM;
2633 goto fail_parents;
2636 INIT_HLIST_HEAD(&core->clks);
2638 hw->clk = __clk_create_clk(hw, NULL, NULL);
2639 if (IS_ERR(hw->clk)) {
2640 ret = PTR_ERR(hw->clk);
2641 goto fail_parents;
2644 ret = __clk_core_init(core);
2645 if (!ret)
2646 return hw->clk;
2648 __clk_free_clk(hw->clk);
2649 hw->clk = NULL;
2651 fail_parents:
2652 kfree(core->parents);
2653 fail_parent_names_copy:
2654 while (--i >= 0)
2655 kfree_const(core->parent_names[i]);
2656 kfree(core->parent_names);
2657 fail_parent_names:
2658 kfree_const(core->name);
2659 fail_name:
2660 kfree(core);
2661 fail_out:
2662 return ERR_PTR(ret);
2664 EXPORT_SYMBOL_GPL(clk_register);
2667 * clk_hw_register - register a clk_hw and return an error code
2668 * @dev: device that is registering this clock
2669 * @hw: link to hardware-specific clock data
2671 * clk_hw_register is the primary interface for populating the clock tree with
2672 * new clock nodes. It returns an integer equal to zero indicating success or
2673 * less than zero indicating failure. Drivers must test for an error code after
2674 * calling clk_hw_register().
2676 int clk_hw_register(struct device *dev, struct clk_hw *hw)
2678 return PTR_ERR_OR_ZERO(clk_register(dev, hw));
2680 EXPORT_SYMBOL_GPL(clk_hw_register);
2682 /* Free memory allocated for a clock. */
2683 static void __clk_release(struct kref *ref)
2685 struct clk_core *core = container_of(ref, struct clk_core, ref);
2686 int i = core->num_parents;
2688 lockdep_assert_held(&prepare_lock);
2690 kfree(core->parents);
2691 while (--i >= 0)
2692 kfree_const(core->parent_names[i]);
2694 kfree(core->parent_names);
2695 kfree_const(core->name);
2696 kfree(core);
2700 * Empty clk_ops for unregistered clocks. These are used temporarily
2701 * after clk_unregister() was called on a clock and until last clock
2702 * consumer calls clk_put() and the struct clk object is freed.
2704 static int clk_nodrv_prepare_enable(struct clk_hw *hw)
2706 return -ENXIO;
2709 static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
2711 WARN_ON_ONCE(1);
2714 static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
2715 unsigned long parent_rate)
2717 return -ENXIO;
2720 static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
2722 return -ENXIO;
2725 static const struct clk_ops clk_nodrv_ops = {
2726 .enable = clk_nodrv_prepare_enable,
2727 .disable = clk_nodrv_disable_unprepare,
2728 .prepare = clk_nodrv_prepare_enable,
2729 .unprepare = clk_nodrv_disable_unprepare,
2730 .set_rate = clk_nodrv_set_rate,
2731 .set_parent = clk_nodrv_set_parent,
2735 * clk_unregister - unregister a currently registered clock
2736 * @clk: clock to unregister
2738 void clk_unregister(struct clk *clk)
2740 unsigned long flags;
2742 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2743 return;
2745 clk_debug_unregister(clk->core);
2747 clk_prepare_lock();
2749 if (clk->core->ops == &clk_nodrv_ops) {
2750 pr_err("%s: unregistered clock: %s\n", __func__,
2751 clk->core->name);
2752 goto unlock;
2755 * Assign empty clock ops for consumers that might still hold
2756 * a reference to this clock.
2758 flags = clk_enable_lock();
2759 clk->core->ops = &clk_nodrv_ops;
2760 clk_enable_unlock(flags);
2762 if (!hlist_empty(&clk->core->children)) {
2763 struct clk_core *child;
2764 struct hlist_node *t;
2766 /* Reparent all children to the orphan list. */
2767 hlist_for_each_entry_safe(child, t, &clk->core->children,
2768 child_node)
2769 clk_core_set_parent(child, NULL);
2772 hlist_del_init(&clk->core->child_node);
2774 if (clk->core->prepare_count)
2775 pr_warn("%s: unregistering prepared clock: %s\n",
2776 __func__, clk->core->name);
2777 kref_put(&clk->core->ref, __clk_release);
2778 unlock:
2779 clk_prepare_unlock();
2781 EXPORT_SYMBOL_GPL(clk_unregister);
2784 * clk_hw_unregister - unregister a currently registered clk_hw
2785 * @hw: hardware-specific clock data to unregister
2787 void clk_hw_unregister(struct clk_hw *hw)
2789 clk_unregister(hw->clk);
2791 EXPORT_SYMBOL_GPL(clk_hw_unregister);
2793 static void devm_clk_release(struct device *dev, void *res)
2795 clk_unregister(*(struct clk **)res);
2798 static void devm_clk_hw_release(struct device *dev, void *res)
2800 clk_hw_unregister(*(struct clk_hw **)res);
2804 * devm_clk_register - resource managed clk_register()
2805 * @dev: device that is registering this clock
2806 * @hw: link to hardware-specific clock data
2808 * Managed clk_register(). Clocks returned from this function are
2809 * automatically clk_unregister()ed on driver detach. See clk_register() for
2810 * more information.
2812 struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
2814 struct clk *clk;
2815 struct clk **clkp;
2817 clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
2818 if (!clkp)
2819 return ERR_PTR(-ENOMEM);
2821 clk = clk_register(dev, hw);
2822 if (!IS_ERR(clk)) {
2823 *clkp = clk;
2824 devres_add(dev, clkp);
2825 } else {
2826 devres_free(clkp);
2829 return clk;
2831 EXPORT_SYMBOL_GPL(devm_clk_register);
2834 * devm_clk_hw_register - resource managed clk_hw_register()
2835 * @dev: device that is registering this clock
2836 * @hw: link to hardware-specific clock data
2838 * Managed clk_hw_register(). Clocks registered by this function are
2839 * automatically clk_hw_unregister()ed on driver detach. See clk_hw_register()
2840 * for more information.
2842 int devm_clk_hw_register(struct device *dev, struct clk_hw *hw)
2844 struct clk_hw **hwp;
2845 int ret;
2847 hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
2848 if (!hwp)
2849 return -ENOMEM;
2851 ret = clk_hw_register(dev, hw);
2852 if (!ret) {
2853 *hwp = hw;
2854 devres_add(dev, hwp);
2855 } else {
2856 devres_free(hwp);
2859 return ret;
2861 EXPORT_SYMBOL_GPL(devm_clk_hw_register);
2863 static int devm_clk_match(struct device *dev, void *res, void *data)
2865 struct clk *c = res;
2866 if (WARN_ON(!c))
2867 return 0;
2868 return c == data;
2871 static int devm_clk_hw_match(struct device *dev, void *res, void *data)
2873 struct clk_hw *hw = res;
2875 if (WARN_ON(!hw))
2876 return 0;
2877 return hw == data;
2881 * devm_clk_unregister - resource managed clk_unregister()
2882 * @clk: clock to unregister
2884 * Deallocate a clock allocated with devm_clk_register(). Normally
2885 * this function will not need to be called and the resource management
2886 * code will ensure that the resource is freed.
2888 void devm_clk_unregister(struct device *dev, struct clk *clk)
2890 WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
2892 EXPORT_SYMBOL_GPL(devm_clk_unregister);
2895 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
2896 * @dev: device that is unregistering the hardware-specific clock data
2897 * @hw: link to hardware-specific clock data
2899 * Unregister a clk_hw registered with devm_clk_hw_register(). Normally
2900 * this function will not need to be called and the resource management
2901 * code will ensure that the resource is freed.
2903 void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
2905 WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
2906 hw));
2908 EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
2911 * clkdev helpers
2913 int __clk_get(struct clk *clk)
2915 struct clk_core *core = !clk ? NULL : clk->core;
2917 if (core) {
2918 if (!try_module_get(core->owner))
2919 return 0;
2921 kref_get(&core->ref);
2923 return 1;
2926 /* keep in sync with __clk_free_clk */
2927 void __clk_put(struct clk *clk)
2929 struct module *owner;
2931 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
2932 return;
2934 clk_prepare_lock();
2936 hlist_del(&clk->clks_node);
2937 if (clk->min_rate > clk->core->req_rate ||
2938 clk->max_rate < clk->core->req_rate)
2939 clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
2941 owner = clk->core->owner;
2942 kref_put(&clk->core->ref, __clk_release);
2944 clk_prepare_unlock();
2946 module_put(owner);
2948 kfree_const(clk->con_id);
2949 kfree(clk);
2952 /*** clk rate change notifiers ***/
2955 * clk_notifier_register - add a clk rate change notifier
2956 * @clk: struct clk * to watch
2957 * @nb: struct notifier_block * with callback info
2959 * Request notification when clk's rate changes. This uses an SRCU
2960 * notifier because we want it to block and notifier unregistrations are
2961 * uncommon. The callbacks associated with the notifier must not
2962 * re-enter into the clk framework by calling any top-level clk APIs;
2963 * this will cause a nested prepare_lock mutex.
2965 * In all notification cases (pre, post and abort rate change) the original
2966 * clock rate is passed to the callback via struct clk_notifier_data.old_rate
2967 * and the new frequency is passed via struct clk_notifier_data.new_rate.
2969 * clk_notifier_register() must be called from non-atomic context.
2970 * Returns -EINVAL if called with null arguments, -ENOMEM upon
2971 * allocation failure; otherwise, passes along the return value of
2972 * srcu_notifier_chain_register().
2974 int clk_notifier_register(struct clk *clk, struct notifier_block *nb)
2976 struct clk_notifier *cn;
2977 int ret = -ENOMEM;
2979 if (!clk || !nb)
2980 return -EINVAL;
2982 clk_prepare_lock();
2984 /* search the list of notifiers for this clk */
2985 list_for_each_entry(cn, &clk_notifier_list, node)
2986 if (cn->clk == clk)
2987 break;
2989 /* if clk wasn't in the notifier list, allocate new clk_notifier */
2990 if (cn->clk != clk) {
2991 cn = kzalloc(sizeof(*cn), GFP_KERNEL);
2992 if (!cn)
2993 goto out;
2995 cn->clk = clk;
2996 srcu_init_notifier_head(&cn->notifier_head);
2998 list_add(&cn->node, &clk_notifier_list);
3001 ret = srcu_notifier_chain_register(&cn->notifier_head, nb);
3003 clk->core->notifier_count++;
3005 out:
3006 clk_prepare_unlock();
3008 return ret;
3010 EXPORT_SYMBOL_GPL(clk_notifier_register);
3013 * clk_notifier_unregister - remove a clk rate change notifier
3014 * @clk: struct clk *
3015 * @nb: struct notifier_block * with callback info
3017 * Request no further notification for changes to 'clk' and frees memory
3018 * allocated in clk_notifier_register.
3020 * Returns -EINVAL if called with null arguments; otherwise, passes
3021 * along the return value of srcu_notifier_chain_unregister().
3023 int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb)
3025 struct clk_notifier *cn = NULL;
3026 int ret = -EINVAL;
3028 if (!clk || !nb)
3029 return -EINVAL;
3031 clk_prepare_lock();
3033 list_for_each_entry(cn, &clk_notifier_list, node)
3034 if (cn->clk == clk)
3035 break;
3037 if (cn->clk == clk) {
3038 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb);
3040 clk->core->notifier_count--;
3042 /* XXX the notifier code should handle this better */
3043 if (!cn->notifier_head.head) {
3044 srcu_cleanup_notifier_head(&cn->notifier_head);
3045 list_del(&cn->node);
3046 kfree(cn);
3049 } else {
3050 ret = -ENOENT;
3053 clk_prepare_unlock();
3055 return ret;
3057 EXPORT_SYMBOL_GPL(clk_notifier_unregister);
3059 #ifdef CONFIG_OF
3061 * struct of_clk_provider - Clock provider registration structure
3062 * @link: Entry in global list of clock providers
3063 * @node: Pointer to device tree node of clock provider
3064 * @get: Get clock callback. Returns NULL or a struct clk for the
3065 * given clock specifier
3066 * @data: context pointer to be passed into @get callback
3068 struct of_clk_provider {
3069 struct list_head link;
3071 struct device_node *node;
3072 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
3073 struct clk_hw *(*get_hw)(struct of_phandle_args *clkspec, void *data);
3074 void *data;
3077 static const struct of_device_id __clk_of_table_sentinel
3078 __used __section(__clk_of_table_end);
3080 static LIST_HEAD(of_clk_providers);
3081 static DEFINE_MUTEX(of_clk_mutex);
3083 struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
3084 void *data)
3086 return data;
3088 EXPORT_SYMBOL_GPL(of_clk_src_simple_get);
3090 struct clk_hw *of_clk_hw_simple_get(struct of_phandle_args *clkspec, void *data)
3092 return data;
3094 EXPORT_SYMBOL_GPL(of_clk_hw_simple_get);
3096 struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data)
3098 struct clk_onecell_data *clk_data = data;
3099 unsigned int idx = clkspec->args[0];
3101 if (idx >= clk_data->clk_num) {
3102 pr_err("%s: invalid clock index %u\n", __func__, idx);
3103 return ERR_PTR(-EINVAL);
3106 return clk_data->clks[idx];
3108 EXPORT_SYMBOL_GPL(of_clk_src_onecell_get);
3110 struct clk_hw *
3111 of_clk_hw_onecell_get(struct of_phandle_args *clkspec, void *data)
3113 struct clk_hw_onecell_data *hw_data = data;
3114 unsigned int idx = clkspec->args[0];
3116 if (idx >= hw_data->num) {
3117 pr_err("%s: invalid index %u\n", __func__, idx);
3118 return ERR_PTR(-EINVAL);
3121 return hw_data->hws[idx];
3123 EXPORT_SYMBOL_GPL(of_clk_hw_onecell_get);
3126 * of_clk_add_provider() - Register a clock provider for a node
3127 * @np: Device node pointer associated with clock provider
3128 * @clk_src_get: callback for decoding clock
3129 * @data: context pointer for @clk_src_get callback.
3131 int of_clk_add_provider(struct device_node *np,
3132 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
3133 void *data),
3134 void *data)
3136 struct of_clk_provider *cp;
3137 int ret;
3139 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3140 if (!cp)
3141 return -ENOMEM;
3143 cp->node = of_node_get(np);
3144 cp->data = data;
3145 cp->get = clk_src_get;
3147 mutex_lock(&of_clk_mutex);
3148 list_add(&cp->link, &of_clk_providers);
3149 mutex_unlock(&of_clk_mutex);
3150 pr_debug("Added clock from %pOF\n", np);
3152 ret = of_clk_set_defaults(np, true);
3153 if (ret < 0)
3154 of_clk_del_provider(np);
3156 return ret;
3158 EXPORT_SYMBOL_GPL(of_clk_add_provider);
3161 * of_clk_add_hw_provider() - Register a clock provider for a node
3162 * @np: Device node pointer associated with clock provider
3163 * @get: callback for decoding clk_hw
3164 * @data: context pointer for @get callback.
3166 int of_clk_add_hw_provider(struct device_node *np,
3167 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
3168 void *data),
3169 void *data)
3171 struct of_clk_provider *cp;
3172 int ret;
3174 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
3175 if (!cp)
3176 return -ENOMEM;
3178 cp->node = of_node_get(np);
3179 cp->data = data;
3180 cp->get_hw = get;
3182 mutex_lock(&of_clk_mutex);
3183 list_add(&cp->link, &of_clk_providers);
3184 mutex_unlock(&of_clk_mutex);
3185 pr_debug("Added clk_hw provider from %pOF\n", np);
3187 ret = of_clk_set_defaults(np, true);
3188 if (ret < 0)
3189 of_clk_del_provider(np);
3191 return ret;
3193 EXPORT_SYMBOL_GPL(of_clk_add_hw_provider);
3196 * of_clk_del_provider() - Remove a previously registered clock provider
3197 * @np: Device node pointer associated with clock provider
3199 void of_clk_del_provider(struct device_node *np)
3201 struct of_clk_provider *cp;
3203 mutex_lock(&of_clk_mutex);
3204 list_for_each_entry(cp, &of_clk_providers, link) {
3205 if (cp->node == np) {
3206 list_del(&cp->link);
3207 of_node_put(cp->node);
3208 kfree(cp);
3209 break;
3212 mutex_unlock(&of_clk_mutex);
3214 EXPORT_SYMBOL_GPL(of_clk_del_provider);
3216 static struct clk_hw *
3217 __of_clk_get_hw_from_provider(struct of_clk_provider *provider,
3218 struct of_phandle_args *clkspec)
3220 struct clk *clk;
3222 if (provider->get_hw)
3223 return provider->get_hw(clkspec, provider->data);
3225 clk = provider->get(clkspec, provider->data);
3226 if (IS_ERR(clk))
3227 return ERR_CAST(clk);
3228 return __clk_get_hw(clk);
3231 struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
3232 const char *dev_id, const char *con_id)
3234 struct of_clk_provider *provider;
3235 struct clk *clk = ERR_PTR(-EPROBE_DEFER);
3236 struct clk_hw *hw;
3238 if (!clkspec)
3239 return ERR_PTR(-EINVAL);
3241 /* Check if we have such a provider in our array */
3242 mutex_lock(&of_clk_mutex);
3243 list_for_each_entry(provider, &of_clk_providers, link) {
3244 if (provider->node == clkspec->np) {
3245 hw = __of_clk_get_hw_from_provider(provider, clkspec);
3246 clk = __clk_create_clk(hw, dev_id, con_id);
3249 if (!IS_ERR(clk)) {
3250 if (!__clk_get(clk)) {
3251 __clk_free_clk(clk);
3252 clk = ERR_PTR(-ENOENT);
3255 break;
3258 mutex_unlock(&of_clk_mutex);
3260 return clk;
3264 * of_clk_get_from_provider() - Lookup a clock from a clock provider
3265 * @clkspec: pointer to a clock specifier data structure
3267 * This function looks up a struct clk from the registered list of clock
3268 * providers, an input is a clock specifier data structure as returned
3269 * from the of_parse_phandle_with_args() function call.
3271 struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
3273 return __of_clk_get_from_provider(clkspec, NULL, __func__);
3275 EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
3278 * of_clk_get_parent_count() - Count the number of clocks a device node has
3279 * @np: device node to count
3281 * Returns: The number of clocks that are possible parents of this node
3283 unsigned int of_clk_get_parent_count(struct device_node *np)
3285 int count;
3287 count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
3288 if (count < 0)
3289 return 0;
3291 return count;
3293 EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
3295 const char *of_clk_get_parent_name(struct device_node *np, int index)
3297 struct of_phandle_args clkspec;
3298 struct property *prop;
3299 const char *clk_name;
3300 const __be32 *vp;
3301 u32 pv;
3302 int rc;
3303 int count;
3304 struct clk *clk;
3306 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
3307 &clkspec);
3308 if (rc)
3309 return NULL;
3311 index = clkspec.args_count ? clkspec.args[0] : 0;
3312 count = 0;
3314 /* if there is an indices property, use it to transfer the index
3315 * specified into an array offset for the clock-output-names property.
3317 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) {
3318 if (index == pv) {
3319 index = count;
3320 break;
3322 count++;
3324 /* We went off the end of 'clock-indices' without finding it */
3325 if (prop && !vp)
3326 return NULL;
3328 if (of_property_read_string_index(clkspec.np, "clock-output-names",
3329 index,
3330 &clk_name) < 0) {
3332 * Best effort to get the name if the clock has been
3333 * registered with the framework. If the clock isn't
3334 * registered, we return the node name as the name of
3335 * the clock as long as #clock-cells = 0.
3337 clk = of_clk_get_from_provider(&clkspec);
3338 if (IS_ERR(clk)) {
3339 if (clkspec.args_count == 0)
3340 clk_name = clkspec.np->name;
3341 else
3342 clk_name = NULL;
3343 } else {
3344 clk_name = __clk_get_name(clk);
3345 clk_put(clk);
3350 of_node_put(clkspec.np);
3351 return clk_name;
3353 EXPORT_SYMBOL_GPL(of_clk_get_parent_name);
3356 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
3357 * number of parents
3358 * @np: Device node pointer associated with clock provider
3359 * @parents: pointer to char array that hold the parents' names
3360 * @size: size of the @parents array
3362 * Return: number of parents for the clock node.
3364 int of_clk_parent_fill(struct device_node *np, const char **parents,
3365 unsigned int size)
3367 unsigned int i = 0;
3369 while (i < size && (parents[i] = of_clk_get_parent_name(np, i)) != NULL)
3370 i++;
3372 return i;
3374 EXPORT_SYMBOL_GPL(of_clk_parent_fill);
3376 struct clock_provider {
3377 of_clk_init_cb_t clk_init_cb;
3378 struct device_node *np;
3379 struct list_head node;
3383 * This function looks for a parent clock. If there is one, then it
3384 * checks that the provider for this parent clock was initialized, in
3385 * this case the parent clock will be ready.
3387 static int parent_ready(struct device_node *np)
3389 int i = 0;
3391 while (true) {
3392 struct clk *clk = of_clk_get(np, i);
3394 /* this parent is ready we can check the next one */
3395 if (!IS_ERR(clk)) {
3396 clk_put(clk);
3397 i++;
3398 continue;
3401 /* at least one parent is not ready, we exit now */
3402 if (PTR_ERR(clk) == -EPROBE_DEFER)
3403 return 0;
3406 * Here we make assumption that the device tree is
3407 * written correctly. So an error means that there is
3408 * no more parent. As we didn't exit yet, then the
3409 * previous parent are ready. If there is no clock
3410 * parent, no need to wait for them, then we can
3411 * consider their absence as being ready
3413 return 1;
3418 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
3419 * @np: Device node pointer associated with clock provider
3420 * @index: clock index
3421 * @flags: pointer to clk_core->flags
3423 * Detects if the clock-critical property exists and, if so, sets the
3424 * corresponding CLK_IS_CRITICAL flag.
3426 * Do not use this function. It exists only for legacy Device Tree
3427 * bindings, such as the one-clock-per-node style that are outdated.
3428 * Those bindings typically put all clock data into .dts and the Linux
3429 * driver has no clock data, thus making it impossible to set this flag
3430 * correctly from the driver. Only those drivers may call
3431 * of_clk_detect_critical from their setup functions.
3433 * Return: error code or zero on success
3435 int of_clk_detect_critical(struct device_node *np,
3436 int index, unsigned long *flags)
3438 struct property *prop;
3439 const __be32 *cur;
3440 uint32_t idx;
3442 if (!np || !flags)
3443 return -EINVAL;
3445 of_property_for_each_u32(np, "clock-critical", prop, cur, idx)
3446 if (index == idx)
3447 *flags |= CLK_IS_CRITICAL;
3449 return 0;
3453 * of_clk_init() - Scan and init clock providers from the DT
3454 * @matches: array of compatible values and init functions for providers.
3456 * This function scans the device tree for matching clock providers
3457 * and calls their initialization functions. It also does it by trying
3458 * to follow the dependencies.
3460 void __init of_clk_init(const struct of_device_id *matches)
3462 const struct of_device_id *match;
3463 struct device_node *np;
3464 struct clock_provider *clk_provider, *next;
3465 bool is_init_done;
3466 bool force = false;
3467 LIST_HEAD(clk_provider_list);
3469 if (!matches)
3470 matches = &__clk_of_table;
3472 /* First prepare the list of the clocks providers */
3473 for_each_matching_node_and_match(np, matches, &match) {
3474 struct clock_provider *parent;
3476 if (!of_device_is_available(np))
3477 continue;
3479 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
3480 if (!parent) {
3481 list_for_each_entry_safe(clk_provider, next,
3482 &clk_provider_list, node) {
3483 list_del(&clk_provider->node);
3484 of_node_put(clk_provider->np);
3485 kfree(clk_provider);
3487 of_node_put(np);
3488 return;
3491 parent->clk_init_cb = match->data;
3492 parent->np = of_node_get(np);
3493 list_add_tail(&parent->node, &clk_provider_list);
3496 while (!list_empty(&clk_provider_list)) {
3497 is_init_done = false;
3498 list_for_each_entry_safe(clk_provider, next,
3499 &clk_provider_list, node) {
3500 if (force || parent_ready(clk_provider->np)) {
3502 /* Don't populate platform devices */
3503 of_node_set_flag(clk_provider->np,
3504 OF_POPULATED);
3506 clk_provider->clk_init_cb(clk_provider->np);
3507 of_clk_set_defaults(clk_provider->np, true);
3509 list_del(&clk_provider->node);
3510 of_node_put(clk_provider->np);
3511 kfree(clk_provider);
3512 is_init_done = true;
3517 * We didn't manage to initialize any of the
3518 * remaining providers during the last loop, so now we
3519 * initialize all the remaining ones unconditionally
3520 * in case the clock parent was not mandatory
3522 if (!is_init_done)
3523 force = true;
3526 #endif