Lines Matching +full:clk +full:- +full:phase +full:-
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
4 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
6 * Standard functionality for the common clock API. See Documentation/driver-api/clk.rst
9 #include <linux/clk.h>
10 #include <linux/clk-provider.h>
11 #include <linux/clk/clk-conf.h>
25 #include "clk.h"
83 int phase; member
97 #include <trace/events/clk.h>
99 struct clk { struct
115 if (!core->rpm_enabled) in clk_pm_runtime_get()
118 ret = pm_runtime_get_sync(core->dev); in clk_pm_runtime_get()
120 pm_runtime_put_noidle(core->dev); in clk_pm_runtime_get()
128 if (!core->rpm_enabled) in clk_pm_runtime_put()
131 pm_runtime_put_sync(core->dev); in clk_pm_runtime_put()
155 if (--prepare_refcnt) in clk_prepare_unlock()
195 if (--enable_refcnt) { in clk_enable_unlock()
205 return core->protect_count; in clk_core_rate_is_protected()
216 if (!core->ops->is_prepared) in clk_core_is_prepared()
217 return core->prepare_count; in clk_core_is_prepared()
220 ret = core->ops->is_prepared(core->hw); in clk_core_is_prepared()
235 if (!core->ops->is_enabled) in clk_core_is_enabled()
236 return core->enable_count; in clk_core_is_enabled()
248 if (core->rpm_enabled) { in clk_core_is_enabled()
249 pm_runtime_get_noresume(core->dev); in clk_core_is_enabled()
250 if (!pm_runtime_active(core->dev)) { in clk_core_is_enabled()
256 ret = core->ops->is_enabled(core->hw); in clk_core_is_enabled()
258 if (core->rpm_enabled) in clk_core_is_enabled()
259 pm_runtime_put(core->dev); in clk_core_is_enabled()
266 const char *__clk_get_name(const struct clk *clk) in __clk_get_name() argument
268 return !clk ? NULL : clk->core->name; in __clk_get_name()
274 return hw->core->name; in clk_hw_get_name()
278 struct clk_hw *__clk_get_hw(struct clk *clk) in __clk_get_hw() argument
280 return !clk ? NULL : clk->core->hw; in __clk_get_hw()
286 return hw->core->num_parents; in clk_hw_get_num_parents()
292 return hw->core->parent ? hw->core->parent->hw : NULL; in clk_hw_get_parent()
302 if (!strcmp(core->name, name)) in __clk_lookup_subtree()
305 hlist_for_each_entry(child, &core->children, child_node) { in __clk_lookup_subtree()
322 /* search the 'proper' clk tree first */ in clk_core_lookup()
349 return -ENOENT; in of_parse_clkspec()
354 return ERR_PTR(-ENOENT); in of_clk_get_hw_from_clkspec()
359 * clk_core_get - Find the clk_core parent of a clk
360 * @core: clk to find parent of
363 * This is the preferred method for clk providers to find the parent of a
364 * clk when that parent is external to the clk controller. The parent_names
366 * node's 'clock-names' property or as the 'con_id' matching the device's
367 * dev_name() in a clk_lookup. This allows clk providers to use their own
371 * clock-controller@c001 that has a clk_init_data::parent_data array
373 * clock-controller@f00abcd without needing to get the globally unique name of
374 * the xtal clk.
376 * parent: clock-controller@f00abcd {
378 * #clock-cells = <0>;
381 * clock-controller@c001 {
384 * clock-names = "xtal";
385 * #clock-cells = <1>;
388 * Returns: -ENOENT when the provider can't be found or the clk doesn't
390 * in a clkdev lookup. NULL when the provider knows about the clk but it
392 * A valid clk_core pointer when the clk can be found in the provider.
396 const char *name = core->parents[p_index].fw_name; in clk_core_get()
397 int index = core->parents[p_index].index; in clk_core_get()
398 struct clk_hw *hw = ERR_PTR(-ENOENT); in clk_core_get()
399 struct device *dev = core->dev; in clk_core_get()
401 struct device_node *np = core->of_node; in clk_core_get()
419 return hw->core; in clk_core_get()
424 struct clk_parent_map *entry = &core->parents[index]; in clk_core_fill_parent_index()
425 struct clk_core *parent = ERR_PTR(-ENOENT); in clk_core_fill_parent_index()
427 if (entry->hw) { in clk_core_fill_parent_index()
428 parent = entry->hw->core; in clk_core_fill_parent_index()
435 parent = ERR_PTR(-EPROBE_DEFER); in clk_core_fill_parent_index()
438 if (PTR_ERR(parent) == -ENOENT && entry->name) in clk_core_fill_parent_index()
439 parent = clk_core_lookup(entry->name); in clk_core_fill_parent_index()
444 entry->core = parent; in clk_core_fill_parent_index()
450 if (!core || index >= core->num_parents || !core->parents) in clk_core_get_parent_by_index()
453 if (!core->parents[index].core) in clk_core_get_parent_by_index()
456 return core->parents[index].core; in clk_core_get_parent_by_index()
464 parent = clk_core_get_parent_by_index(hw->core, index); in clk_hw_get_parent_by_index()
466 return !parent ? NULL : parent->hw; in clk_hw_get_parent_by_index()
470 unsigned int __clk_get_enable_count(struct clk *clk) in __clk_get_enable_count() argument
472 return !clk ? 0 : clk->core->enable_count; in __clk_get_enable_count()
480 if (!core->num_parents || core->parent) in clk_core_get_rate_nolock()
481 return core->rate; in clk_core_get_rate_nolock()
484 * Clk must have a parent because num_parents > 0 but the parent isn't in clk_core_get_rate_nolock()
485 * known yet. Best to return 0 as the rate of this clk until we can in clk_core_get_rate_nolock()
493 return clk_core_get_rate_nolock(hw->core); in clk_hw_get_rate()
502 return core->accuracy; in clk_core_get_accuracy_no_lock()
507 return hw->core->flags; in clk_hw_get_flags()
513 return clk_core_is_prepared(hw->core); in clk_hw_is_prepared()
519 return clk_core_rate_is_protected(hw->core); in clk_hw_rate_is_protected()
525 return clk_core_is_enabled(hw->core); in clk_hw_is_enabled()
529 bool __clk_is_enabled(struct clk *clk) in __clk_is_enabled() argument
531 if (!clk) in __clk_is_enabled()
534 return clk_core_is_enabled(clk->core); in __clk_is_enabled()
542 return abs(now - rate) < abs(best - rate); in mux_is_better_rate()
551 struct clk_core *core = hw->core, *parent, *best_parent = NULL; in clk_mux_determine_rate_flags()
557 if (core->flags & CLK_SET_RATE_NO_REPARENT) { in clk_mux_determine_rate_flags()
558 parent = core->parent; in clk_mux_determine_rate_flags()
559 if (core->flags & CLK_SET_RATE_PARENT) { in clk_mux_determine_rate_flags()
560 ret = __clk_determine_rate(parent ? parent->hw : NULL, in clk_mux_determine_rate_flags()
576 num_parents = core->num_parents; in clk_mux_determine_rate_flags()
582 if (core->flags & CLK_SET_RATE_PARENT) { in clk_mux_determine_rate_flags()
584 ret = __clk_determine_rate(parent->hw, &parent_req); in clk_mux_determine_rate_flags()
591 if (mux_is_better_rate(req->rate, parent_req.rate, in clk_mux_determine_rate_flags()
599 return -EINVAL; in clk_mux_determine_rate_flags()
603 req->best_parent_hw = best_parent->hw; in clk_mux_determine_rate_flags()
604 req->best_parent_rate = best; in clk_mux_determine_rate_flags()
605 req->rate = best; in clk_mux_determine_rate_flags()
611 struct clk *__clk_lookup(const char *name) in __clk_lookup()
615 return !core ? NULL : core->hw->clk; in __clk_lookup()
622 struct clk *clk_user; in clk_core_get_boundaries()
626 *min_rate = core->min_rate; in clk_core_get_boundaries()
627 *max_rate = core->max_rate; in clk_core_get_boundaries()
629 hlist_for_each_entry(clk_user, &core->clks, clks_node) in clk_core_get_boundaries()
630 *min_rate = max(*min_rate, clk_user->min_rate); in clk_core_get_boundaries()
632 hlist_for_each_entry(clk_user, &core->clks, clks_node) in clk_core_get_boundaries()
633 *max_rate = min(*max_rate, clk_user->max_rate); in clk_core_get_boundaries()
640 struct clk *user; in clk_core_check_boundaries()
644 if (min_rate > core->max_rate || max_rate < core->min_rate) in clk_core_check_boundaries()
647 hlist_for_each_entry(user, &core->clks, clks_node) in clk_core_check_boundaries()
648 if (min_rate > user->max_rate || max_rate < user->min_rate) in clk_core_check_boundaries()
657 hw->core->min_rate = min_rate; in clk_hw_set_rate_range()
658 hw->core->max_rate = max_rate; in clk_hw_set_rate_range()
663 * __clk_mux_determine_rate - clk_ops::determine_rate implementation for a mux type clk
664 * @hw: mux type clk to determine rate on
671 * Returns: 0 on success, -EERROR value on error
687 /*** clk api ***/
696 if (WARN(core->protect_count == 0, in clk_core_rate_unprotect()
697 "%s already unprotected\n", core->name)) in clk_core_rate_unprotect()
700 if (--core->protect_count > 0) in clk_core_rate_unprotect()
703 clk_core_rate_unprotect(core->parent); in clk_core_rate_unprotect()
713 return -EINVAL; in clk_core_rate_nuke_protect()
715 if (core->protect_count == 0) in clk_core_rate_nuke_protect()
718 ret = core->protect_count; in clk_core_rate_nuke_protect()
719 core->protect_count = 1; in clk_core_rate_nuke_protect()
726 * clk_rate_exclusive_put - release exclusivity over clock rate control
727 * @clk: the clk over which the exclusivity is released
743 void clk_rate_exclusive_put(struct clk *clk) in clk_rate_exclusive_put() argument
745 if (!clk) in clk_rate_exclusive_put()
754 if (WARN_ON(clk->exclusive_count <= 0)) in clk_rate_exclusive_put()
757 clk_core_rate_unprotect(clk->core); in clk_rate_exclusive_put()
758 clk->exclusive_count--; in clk_rate_exclusive_put()
771 if (core->protect_count == 0) in clk_core_rate_protect()
772 clk_core_rate_protect(core->parent); in clk_core_rate_protect()
774 core->protect_count++; in clk_core_rate_protect()
788 core->protect_count = count; in clk_core_rate_restore_protect()
792 * clk_rate_exclusive_get - get exclusivity over the clk rate control
793 * @clk: the clk over which the exclusity of rate control is requested
807 * Returns 0 on success, -EERROR otherwise
809 int clk_rate_exclusive_get(struct clk *clk) in clk_rate_exclusive_get() argument
811 if (!clk) in clk_rate_exclusive_get()
815 clk_core_rate_protect(clk->core); in clk_rate_exclusive_get()
816 clk->exclusive_count++; in clk_rate_exclusive_get()
830 if (WARN(core->prepare_count == 0, in clk_core_unprepare()
831 "%s already unprepared\n", core->name)) in clk_core_unprepare()
834 if (WARN(core->prepare_count == 1 && core->flags & CLK_IS_CRITICAL, in clk_core_unprepare()
835 "Unpreparing critical %s\n", core->name)) in clk_core_unprepare()
838 if (core->flags & CLK_SET_RATE_GATE) in clk_core_unprepare()
841 if (--core->prepare_count > 0) in clk_core_unprepare()
844 WARN(core->enable_count > 0, "Unpreparing enabled %s\n", core->name); in clk_core_unprepare()
848 if (core->ops->unprepare) in clk_core_unprepare()
849 core->ops->unprepare(core->hw); in clk_core_unprepare()
852 clk_core_unprepare(core->parent); in clk_core_unprepare()
864 * clk_unprepare - undo preparation of a clock source
865 * @clk: the clk being unprepared
868 * simple case, clk_unprepare can be used instead of clk_disable to gate a clk
869 * if the operation may sleep. One example is a clk which is accessed over
870 * I2c. In the complex case a clk gate operation may require a fast and a slow
874 void clk_unprepare(struct clk *clk) in clk_unprepare() argument
876 if (IS_ERR_OR_NULL(clk)) in clk_unprepare()
879 clk_core_unprepare_lock(clk->core); in clk_unprepare()
892 if (core->prepare_count == 0) { in clk_core_prepare()
897 ret = clk_core_prepare(core->parent); in clk_core_prepare()
903 if (core->ops->prepare) in clk_core_prepare()
904 ret = core->ops->prepare(core->hw); in clk_core_prepare()
912 core->prepare_count++; in clk_core_prepare()
921 if (core->flags & CLK_SET_RATE_GATE) in clk_core_prepare()
926 clk_core_unprepare(core->parent); in clk_core_prepare()
944 * clk_prepare - prepare a clock source
945 * @clk: the clk being prepared
948 * case, clk_prepare can be used instead of clk_enable to ungate a clk if the
949 * operation may sleep. One example is a clk which is accessed over I2c. In
950 * the complex case a clk ungate operation may require a fast and a slow part.
953 * Returns 0 on success, -EERROR otherwise.
955 int clk_prepare(struct clk *clk) in clk_prepare() argument
957 if (!clk) in clk_prepare()
960 return clk_core_prepare_lock(clk->core); in clk_prepare()
971 if (WARN(core->enable_count == 0, "%s already disabled\n", core->name)) in clk_core_disable()
974 if (WARN(core->enable_count == 1 && core->flags & CLK_IS_CRITICAL, in clk_core_disable()
975 "Disabling critical %s\n", core->name)) in clk_core_disable()
978 if (--core->enable_count > 0) in clk_core_disable()
983 if (core->ops->disable) in clk_core_disable()
984 core->ops->disable(core->hw); in clk_core_disable()
988 clk_core_disable(core->parent); in clk_core_disable()
1001 * clk_disable - gate a clock
1002 * @clk: the clk being gated
1006 * clk if the operation is fast and will never sleep. One example is a
1007 * SoC-internal clk which is controlled via simple register writes. In the
1008 * complex case a clk gate operation may require a fast and a slow part. It is
1012 void clk_disable(struct clk *clk) in clk_disable() argument
1014 if (IS_ERR_OR_NULL(clk)) in clk_disable()
1017 clk_core_disable_lock(clk->core); in clk_disable()
1030 if (WARN(core->prepare_count == 0, in clk_core_enable()
1031 "Enabling unprepared %s\n", core->name)) in clk_core_enable()
1032 return -ESHUTDOWN; in clk_core_enable()
1034 if (core->enable_count == 0) { in clk_core_enable()
1035 ret = clk_core_enable(core->parent); in clk_core_enable()
1042 if (core->ops->enable) in clk_core_enable()
1043 ret = core->ops->enable(core->hw); in clk_core_enable()
1048 clk_core_disable(core->parent); in clk_core_enable()
1053 core->enable_count++; in clk_core_enable()
1070 * clk_gate_restore_context - restore context for poweroff
1081 struct clk_core *core = hw->core; in clk_gate_restore_context()
1083 if (core->enable_count) in clk_gate_restore_context()
1084 core->ops->enable(hw); in clk_gate_restore_context()
1086 core->ops->disable(hw); in clk_gate_restore_context()
1095 hlist_for_each_entry(child, &core->children, child_node) { in clk_core_save_context()
1101 if (core->ops && core->ops->save_context) in clk_core_save_context()
1102 ret = core->ops->save_context(core->hw); in clk_core_save_context()
1111 if (core->ops && core->ops->restore_context) in clk_core_restore_context()
1112 core->ops->restore_context(core->hw); in clk_core_restore_context()
1114 hlist_for_each_entry(child, &core->children, child_node) in clk_core_restore_context()
1119 * clk_save_context - save clock context for poweroff
1127 struct clk_core *clk; in clk_save_context() local
1130 hlist_for_each_entry(clk, &clk_root_list, child_node) { in clk_save_context()
1131 ret = clk_core_save_context(clk); in clk_save_context()
1136 hlist_for_each_entry(clk, &clk_orphan_list, child_node) { in clk_save_context()
1137 ret = clk_core_save_context(clk); in clk_save_context()
1147 * clk_restore_context - restore clock context after poweroff
1165 * clk_enable - ungate a clock
1166 * @clk: the clk being ungated
1169 * simple case, clk_enable can be used instead of clk_prepare to ungate a clk
1170 * if the operation will never sleep. One example is a SoC-internal clk which
1171 * is controlled via simple register writes. In the complex case a clk ungate
1174 * must be called before clk_enable. Returns 0 on success, -EERROR
1177 int clk_enable(struct clk *clk) in clk_enable() argument
1179 if (!clk) in clk_enable()
1182 return clk_core_enable_lock(clk->core); in clk_enable()
1213 hlist_for_each_entry(child, &core->children, child_node) in clk_unprepare_unused_subtree()
1216 if (dev_has_sync_state(core->dev) && in clk_unprepare_unused_subtree()
1217 !(core->flags & CLK_DONT_HOLD_STATE)) in clk_unprepare_unused_subtree()
1220 if (core->prepare_count) in clk_unprepare_unused_subtree()
1223 if (core->flags & CLK_IGNORE_UNUSED) in clk_unprepare_unused_subtree()
1231 if (core->ops->unprepare_unused) in clk_unprepare_unused_subtree()
1232 core->ops->unprepare_unused(core->hw); in clk_unprepare_unused_subtree()
1233 else if (core->ops->unprepare) in clk_unprepare_unused_subtree()
1234 core->ops->unprepare(core->hw); in clk_unprepare_unused_subtree()
1248 hlist_for_each_entry(child, &core->children, child_node) in clk_disable_unused_subtree()
1251 if (dev_has_sync_state(core->dev) && in clk_disable_unused_subtree()
1252 !(core->flags & CLK_DONT_HOLD_STATE)) in clk_disable_unused_subtree()
1255 if (core->flags & CLK_OPS_PARENT_ENABLE) in clk_disable_unused_subtree()
1256 clk_core_prepare_enable(core->parent); in clk_disable_unused_subtree()
1263 if (core->enable_count) in clk_disable_unused_subtree()
1266 if (core->flags & CLK_IGNORE_UNUSED) in clk_disable_unused_subtree()
1270 * some gate clocks have special needs during the disable-unused in clk_disable_unused_subtree()
1276 if (core->ops->disable_unused) in clk_disable_unused_subtree()
1277 core->ops->disable_unused(core->hw); in clk_disable_unused_subtree()
1278 else if (core->ops->disable) in clk_disable_unused_subtree()
1279 core->ops->disable(core->hw); in clk_disable_unused_subtree()
1287 if (core->flags & CLK_OPS_PARENT_ENABLE) in clk_disable_unused_subtree()
1288 clk_core_disable_unprepare(core->parent); in clk_disable_unused_subtree()
1304 pr_warn("clk: Not disabling unused clocks\n"); in clk_disable_unused()
1335 hlist_for_each_entry(child, &core->children, child_node) in clk_unprepare_disable_dev_subtree()
1338 if (core->dev != dev || !core->need_sync) in clk_unprepare_disable_dev_subtree()
1372 * - if the provider is not protected at all in clk_core_determine_round_nolock()
1373 * - if the calling consumer is the only one which has exclusivity in clk_core_determine_round_nolock()
1377 req->rate = core->rate; in clk_core_determine_round_nolock()
1378 } else if (core->ops->determine_rate) { in clk_core_determine_round_nolock()
1379 return core->ops->determine_rate(core->hw, req); in clk_core_determine_round_nolock()
1380 } else if (core->ops->round_rate) { in clk_core_determine_round_nolock()
1381 rate = core->ops->round_rate(core->hw, req->rate, in clk_core_determine_round_nolock()
1382 &req->best_parent_rate); in clk_core_determine_round_nolock()
1386 req->rate = rate; in clk_core_determine_round_nolock()
1388 return -EINVAL; in clk_core_determine_round_nolock()
1402 parent = core->parent; in clk_core_init_rate_req()
1404 req->best_parent_hw = parent->hw; in clk_core_init_rate_req()
1405 req->best_parent_rate = parent->rate; in clk_core_init_rate_req()
1407 req->best_parent_hw = NULL; in clk_core_init_rate_req()
1408 req->best_parent_rate = 0; in clk_core_init_rate_req()
1414 return core->ops->determine_rate || core->ops->round_rate; in clk_core_can_round()
1423 req->rate = 0; in clk_core_round_rate_nolock()
1431 else if (core->flags & CLK_SET_RATE_PARENT) in clk_core_round_rate_nolock()
1432 return clk_core_round_rate_nolock(core->parent, req); in clk_core_round_rate_nolock()
1434 req->rate = core->rate; in clk_core_round_rate_nolock()
1439 * __clk_determine_rate - get the closest rate actually supported by a clock
1448 req->rate = 0; in __clk_determine_rate()
1452 return clk_core_round_rate_nolock(hw->core, req); in __clk_determine_rate()
1457 * clk_hw_round_rate() - round the given rate for a hw clk
1458 * @hw: the hw clk for which we are rounding a rate
1461 * Takes in a rate as input and rounds it to a rate that the clk can actually
1465 * For clk providers to call from within clk_ops such as .round_rate,
1468 * Return: returns rounded rate of hw clk if clk supports round_rate operation
1476 clk_core_get_boundaries(hw->core, &req.min_rate, &req.max_rate); in clk_hw_round_rate()
1479 ret = clk_core_round_rate_nolock(hw->core, &req); in clk_hw_round_rate()
1488 * clk_round_rate - round the given rate for a clk
1489 * @clk: the clk for which we are rounding a rate
1492 * Takes in a rate as input and rounds it to a rate that the clk can actually
1493 * use which is then returned. If clk doesn't support round_rate operation
1496 long clk_round_rate(struct clk *clk, unsigned long rate) in clk_round_rate() argument
1501 if (!clk) in clk_round_rate()
1506 if (clk->exclusive_count) in clk_round_rate()
1507 clk_core_rate_unprotect(clk->core); in clk_round_rate()
1509 clk_core_get_boundaries(clk->core, &req.min_rate, &req.max_rate); in clk_round_rate()
1512 ret = clk_core_round_rate_nolock(clk->core, &req); in clk_round_rate()
1514 if (clk->exclusive_count) in clk_round_rate()
1515 clk_core_rate_protect(clk->core); in clk_round_rate()
1527 * __clk_notify - call clk notifier chain
1528 * @core: clk that is changing rate
1529 * @msg: clk notifier type (see include/linux/clk.h)
1530 * @old_rate: old clk rate
1531 * @new_rate: new clk rate
1533 * Triggers a notifier call chain on the clk rate-change notification
1534 * for 'clk'. Passes a pointer to the struct clk and the previous
1551 if (cn->clk->core == core) { in __clk_notify()
1552 cnd.clk = cn->clk; in __clk_notify()
1553 ret = srcu_notifier_call_chain(&cn->notifier_head, msg, in __clk_notify()
1565 * @core: first clk in the subtree
1567 * Walks the subtree of clks starting with clk and recalculates accuracies as
1568 * it goes. Note that if a clk does not implement the .recalc_accuracy
1579 if (core->parent) in __clk_recalc_accuracies()
1580 parent_accuracy = core->parent->accuracy; in __clk_recalc_accuracies()
1582 if (core->ops->recalc_accuracy) in __clk_recalc_accuracies()
1583 core->accuracy = core->ops->recalc_accuracy(core->hw, in __clk_recalc_accuracies()
1586 core->accuracy = parent_accuracy; in __clk_recalc_accuracies()
1588 hlist_for_each_entry(child, &core->children, child_node) in __clk_recalc_accuracies()
1594 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE)) in clk_core_get_accuracy_recalc()
1601 * clk_get_accuracy - return the accuracy of clk
1602 * @clk: the clk whose accuracy is being returned
1604 * Simply returns the cached accuracy of the clk, unless
1607 * If clk is NULL then returns 0.
1609 long clk_get_accuracy(struct clk *clk) in clk_get_accuracy() argument
1613 if (!clk) in clk_get_accuracy()
1617 accuracy = clk_core_get_accuracy_recalc(clk->core); in clk_get_accuracy()
1629 if (core->ops->recalc_rate && !clk_pm_runtime_get(core)) { in clk_recalc()
1630 rate = core->ops->recalc_rate(core->hw, parent_rate); in clk_recalc()
1638 * @core: first clk in the subtree
1639 * @msg: notification type (see include/linux/clk.h)
1641 * Walks the subtree of clks starting with clk and recalculates rates as it
1642 * goes. Note that if a clk does not implement the .recalc_rate callback then
1656 old_rate = core->rate; in __clk_recalc_rates()
1658 if (core->parent) in __clk_recalc_rates()
1659 parent_rate = core->parent->rate; in __clk_recalc_rates()
1661 core->rate = clk_recalc(core, parent_rate); in __clk_recalc_rates()
1667 if (core->notifier_count && msg) in __clk_recalc_rates()
1668 __clk_notify(core, msg, old_rate, core->rate); in __clk_recalc_rates()
1670 hlist_for_each_entry(child, &core->children, child_node) in __clk_recalc_rates()
1676 if (core && (core->flags & CLK_GET_RATE_NOCACHE)) in clk_core_get_rate_recalc()
1683 * clk_get_rate - return the rate of clk
1684 * @clk: the clk whose rate is being returned
1686 * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
1688 * If clk is NULL then returns 0.
1690 unsigned long clk_get_rate(struct clk *clk) in clk_get_rate() argument
1694 if (!clk) in clk_get_rate()
1698 rate = clk_core_get_rate_recalc(clk->core); in clk_get_rate()
1711 return -EINVAL; in clk_fetch_parent_index()
1713 for (i = 0; i < core->num_parents; i++) { in clk_fetch_parent_index()
1715 if (core->parents[i].core == parent) in clk_fetch_parent_index()
1719 if (core->parents[i].core) in clk_fetch_parent_index()
1723 if (core->parents[i].hw) { in clk_fetch_parent_index()
1724 if (core->parents[i].hw == parent->hw) in clk_fetch_parent_index()
1736 if (core->parents[i].name && in clk_fetch_parent_index()
1737 !strcmp(parent->name, core->parents[i].name)) in clk_fetch_parent_index()
1741 if (i == core->num_parents) in clk_fetch_parent_index()
1742 return -EINVAL; in clk_fetch_parent_index()
1744 core->parents[i].core = parent; in clk_fetch_parent_index()
1749 * clk_hw_get_parent_index - return the index of the parent clock
1750 * @hw: clk_hw associated with the clk being consumed
1752 * Fetches and returns the index of parent clock. Returns -EINVAL if the given
1760 return -EINVAL; in clk_hw_get_parent_index()
1762 return clk_fetch_parent_index(hw->core, parent->core); in clk_hw_get_parent_index()
1768 if (core->need_sync || !core->boot_enabled) in clk_core_hold_state()
1771 if (core->orphan || !dev_has_sync_state(core->dev)) in clk_core_hold_state()
1774 if (core->flags & CLK_DONT_HOLD_STATE) in clk_core_hold_state()
1777 core->need_sync = !clk_core_prepare_enable(core); in clk_core_hold_state()
1784 if (core->orphan) in __clk_core_update_orphan_hold_state()
1789 hlist_for_each_entry(child, &core->children, child_node) in __clk_core_update_orphan_hold_state()
1800 core->orphan = is_orphan; in clk_core_update_orphan_status()
1802 hlist_for_each_entry(child, &core->children, child_node) in clk_core_update_orphan_status()
1808 bool was_orphan = core->orphan; in clk_reparent()
1810 hlist_del(&core->child_node); in clk_reparent()
1813 bool becomes_orphan = new_parent->orphan; in clk_reparent()
1816 if (new_parent->new_child == core) in clk_reparent()
1817 new_parent->new_child = NULL; in clk_reparent()
1819 hlist_add_head(&core->child_node, &new_parent->children); in clk_reparent()
1824 hlist_add_head(&core->child_node, &clk_orphan_list); in clk_reparent()
1829 core->parent = new_parent; in clk_reparent()
1836 struct clk_core *old_parent = core->parent; in __clk_set_parent_before()
1859 if (core->flags & CLK_OPS_PARENT_ENABLE) { in __clk_set_parent_before()
1865 if (core->prepare_count) { in __clk_set_parent_before()
1870 /* update the clk tree topology */ in __clk_set_parent_before()
1886 if (core->prepare_count) { in __clk_set_parent_after()
1891 /* re-balance ref counting if CLK_OPS_PARENT_ENABLE is set */ in __clk_set_parent_after()
1892 if (core->flags & CLK_OPS_PARENT_ENABLE) { in __clk_set_parent_after()
1910 if (parent && core->ops->set_parent) in __clk_set_parent()
1911 ret = core->ops->set_parent(core->hw, p_index); in __clk_set_parent()
1931 * @core: first clk in the subtree
1932 * @parent_rate: the "future" rate of clk's parent
1934 * Walks the subtree of clks starting with clk, speculating rates as it
1938 * pre-rate change notifications and returns early if no clks in the
1939 * subtree have subscribed to the notifications. Note that if a clk does not
1955 if (core->notifier_count) in __clk_speculate_rates()
1956 ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate); in __clk_speculate_rates()
1959 pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n", in __clk_speculate_rates()
1960 __func__, core->name, ret); in __clk_speculate_rates()
1964 hlist_for_each_entry(child, &core->children, child_node) { in __clk_speculate_rates()
1979 core->new_rate = new_rate; in clk_calc_subtree()
1980 core->new_parent = new_parent; in clk_calc_subtree()
1981 core->new_parent_index = p_index; in clk_calc_subtree()
1982 /* include clk in new parent's PRE_RATE_CHANGE notifications */ in clk_calc_subtree()
1983 core->new_child = NULL; in clk_calc_subtree()
1984 if (new_parent && new_parent != core->parent) in clk_calc_subtree()
1985 new_parent->new_child = core; in clk_calc_subtree()
1987 hlist_for_each_entry(child, &core->children, child_node) { in clk_calc_subtree()
1988 child->new_rate = clk_recalc(child, new_rate); in clk_calc_subtree()
1989 clk_calc_subtree(child, child->new_rate, NULL, 0); in clk_calc_subtree()
2014 parent = old_parent = core->parent; in clk_calc_new_rates()
2016 best_parent_rate = parent->rate; in clk_calc_new_rates()
2020 /* find the closest rate and parent clk/rate */ in clk_calc_new_rates()
2036 parent = req.best_parent_hw ? req.best_parent_hw->core : NULL; in clk_calc_new_rates()
2040 } else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) { in clk_calc_new_rates()
2041 /* pass-through clock without adjustable parent */ in clk_calc_new_rates()
2042 core->new_rate = core->rate; in clk_calc_new_rates()
2045 /* pass-through clock with adjustable parent */ in clk_calc_new_rates()
2047 new_rate = parent->new_rate; in clk_calc_new_rates()
2053 (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) { in clk_calc_new_rates()
2055 __func__, core->name); in clk_calc_new_rates()
2060 if (parent && core->num_parents > 1) { in clk_calc_new_rates()
2063 pr_debug("%s: clk %s can not be parent of clk %s\n", in clk_calc_new_rates()
2064 __func__, parent->name, core->name); in clk_calc_new_rates()
2069 if ((core->flags & CLK_SET_RATE_PARENT) && parent && in clk_calc_new_rates()
2070 best_parent_rate != parent->rate) in clk_calc_new_rates()
2090 if (core->rate == core->new_rate) in clk_propagate_rate_change()
2093 if (core->notifier_count) { in clk_propagate_rate_change()
2094 ret = __clk_notify(core, event, core->rate, core->new_rate); in clk_propagate_rate_change()
2099 if (core->ops->pre_rate_change) { in clk_propagate_rate_change()
2100 ret = core->ops->pre_rate_change(core->hw, core->rate, in clk_propagate_rate_change()
2101 core->new_rate); in clk_propagate_rate_change()
2106 hlist_for_each_entry(child, &core->children, child_node) { in clk_propagate_rate_change()
2108 if (child->new_parent && child->new_parent != core) in clk_propagate_rate_change()
2115 /* handle the new child who might not be in core->children yet */ in clk_propagate_rate_change()
2116 if (core->new_child) { in clk_propagate_rate_change()
2117 tmp_clk = clk_propagate_rate_change(core->new_child, event); in clk_propagate_rate_change()
2139 old_rate = core->rate; in clk_change_rate()
2141 if (core->new_parent) { in clk_change_rate()
2142 parent = core->new_parent; in clk_change_rate()
2143 best_parent_rate = core->new_parent->rate; in clk_change_rate()
2144 } else if (core->parent) { in clk_change_rate()
2145 parent = core->parent; in clk_change_rate()
2146 best_parent_rate = core->parent->rate; in clk_change_rate()
2152 if (core->flags & CLK_SET_RATE_UNGATE) { in clk_change_rate()
2161 if (core->new_parent && core->new_parent != core->parent) { in clk_change_rate()
2162 old_parent = __clk_set_parent_before(core, core->new_parent); in clk_change_rate()
2163 trace_clk_set_parent(core, core->new_parent); in clk_change_rate()
2165 if (core->ops->set_rate_and_parent) { in clk_change_rate()
2167 core->ops->set_rate_and_parent(core->hw, core->new_rate, in clk_change_rate()
2169 core->new_parent_index); in clk_change_rate()
2170 } else if (core->ops->set_parent) { in clk_change_rate()
2171 core->ops->set_parent(core->hw, core->new_parent_index); in clk_change_rate()
2174 trace_clk_set_parent_complete(core, core->new_parent); in clk_change_rate()
2175 __clk_set_parent_after(core, core->new_parent, old_parent); in clk_change_rate()
2178 if (core->flags & CLK_OPS_PARENT_ENABLE) in clk_change_rate()
2181 trace_clk_set_rate(core, core->new_rate); in clk_change_rate()
2183 if (!skip_set_rate && core->ops->set_rate) in clk_change_rate()
2184 core->ops->set_rate(core->hw, core->new_rate, best_parent_rate); in clk_change_rate()
2186 trace_clk_set_rate_complete(core, core->new_rate); in clk_change_rate()
2188 core->rate = clk_recalc(core, best_parent_rate); in clk_change_rate()
2190 if (core->flags & CLK_SET_RATE_UNGATE) { in clk_change_rate()
2199 if (core->flags & CLK_OPS_PARENT_ENABLE) in clk_change_rate()
2202 if (core->notifier_count && old_rate != core->rate) in clk_change_rate()
2203 __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate); in clk_change_rate()
2205 if (core->flags & CLK_RECALC_NEW_RATES) in clk_change_rate()
2206 (void)clk_calc_new_rates(core, core->new_rate); in clk_change_rate()
2208 if (core->ops->post_rate_change) in clk_change_rate()
2209 core->ops->post_rate_change(core->hw, old_rate, core->rate); in clk_change_rate()
2215 hlist_for_each_entry_safe(child, tmp, &core->children, child_node) { in clk_change_rate()
2217 if (child->new_parent && child->new_parent != core) in clk_change_rate()
2222 /* handle the new child who might not be in core->children yet */ in clk_change_rate()
2223 if (core->new_child) in clk_change_rate()
2224 clk_change_rate(core->new_child); in clk_change_rate()
2274 return -EBUSY; in clk_core_set_rate_nolock()
2279 return -EINVAL; in clk_core_set_rate_nolock()
2289 fail_clk->name); in clk_core_set_rate_nolock()
2291 ret = -EBUSY; in clk_core_set_rate_nolock()
2298 core->req_rate = req_rate; in clk_core_set_rate_nolock()
2306 * clk_set_rate - specify a new rate for clk
2307 * @clk: the clk whose rate is being changed
2308 * @rate: the new rate for clk
2310 * In the simplest case clk_set_rate will only adjust the rate of clk.
2313 * propagate up to clk's parent; whether or not this happens depends on the
2314 * outcome of clk's .round_rate implementation. If *parent_rate is unchanged
2316 * *parent_rate comes back with a new rate for clk's parent then we propagate
2317 * up to clk's parent and set its rate. Upward propagation will continue
2318 * until either a clk does not support the CLK_SET_RATE_PARENT flag or
2319 * .round_rate stops requesting changes to clk's parent_rate.
2324 * Returns 0 on success, -EERROR otherwise.
2326 int clk_set_rate(struct clk *clk, unsigned long rate) in clk_set_rate() argument
2330 if (!clk) in clk_set_rate()
2336 if (clk->exclusive_count) in clk_set_rate()
2337 clk_core_rate_unprotect(clk->core); in clk_set_rate()
2339 ret = clk_core_set_rate_nolock(clk->core, rate); in clk_set_rate()
2341 if (clk->exclusive_count) in clk_set_rate()
2342 clk_core_rate_protect(clk->core); in clk_set_rate()
2351 * clk_set_rate_exclusive - specify a new rate and get exclusive control
2352 * @clk: the clk whose rate is being changed
2353 * @rate: the new rate for clk
2367 * Returns 0 on success, -EERROR otherwise.
2369 int clk_set_rate_exclusive(struct clk *clk, unsigned long rate) in clk_set_rate_exclusive() argument
2373 if (!clk) in clk_set_rate_exclusive()
2385 ret = clk_core_set_rate_nolock(clk->core, rate); in clk_set_rate_exclusive()
2387 clk_core_rate_protect(clk->core); in clk_set_rate_exclusive()
2388 clk->exclusive_count++; in clk_set_rate_exclusive()
2398 * clk_set_rate_range - set a rate range for a clock source
2399 * @clk: clock source
2405 int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max) in clk_set_rate_range() argument
2410 if (!clk) in clk_set_rate_range()
2414 pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n", in clk_set_rate_range()
2415 __func__, clk->core->name, clk->dev_id, clk->con_id, in clk_set_rate_range()
2417 return -EINVAL; in clk_set_rate_range()
2422 if (clk->exclusive_count) in clk_set_rate_range()
2423 clk_core_rate_unprotect(clk->core); in clk_set_rate_range()
2426 old_min = clk->min_rate; in clk_set_rate_range()
2427 old_max = clk->max_rate; in clk_set_rate_range()
2428 clk->min_rate = min; in clk_set_rate_range()
2429 clk->max_rate = max; in clk_set_rate_range()
2431 if (!clk_core_check_boundaries(clk->core, min, max)) { in clk_set_rate_range()
2432 ret = -EINVAL; in clk_set_rate_range()
2436 rate = clk_core_get_rate_nolock(clk->core); in clk_set_rate_range()
2445 * - round_rate() was not favorable and fell on the wrong in clk_set_rate_range()
2447 * - the determine_rate() callback does not really check for in clk_set_rate_range()
2456 ret = clk_core_set_rate_nolock(clk->core, rate); in clk_set_rate_range()
2459 clk->min_rate = old_min; in clk_set_rate_range()
2460 clk->max_rate = old_max; in clk_set_rate_range()
2465 if (clk->exclusive_count) in clk_set_rate_range()
2466 clk_core_rate_protect(clk->core); in clk_set_rate_range()
2475 * clk_set_min_rate - set a minimum clock rate for a clock source
2476 * @clk: clock source
2481 int clk_set_min_rate(struct clk *clk, unsigned long rate) in clk_set_min_rate() argument
2483 if (!clk) in clk_set_min_rate()
2486 return clk_set_rate_range(clk, rate, clk->max_rate); in clk_set_min_rate()
2491 * clk_set_max_rate - set a maximum clock rate for a clock source
2492 * @clk: clock source
2497 int clk_set_max_rate(struct clk *clk, unsigned long rate) in clk_set_max_rate() argument
2499 if (!clk) in clk_set_max_rate()
2502 return clk_set_rate_range(clk, clk->min_rate, rate); in clk_set_max_rate()
2507 * clk_get_parent - return the parent of a clk
2508 * @clk: the clk whose parent gets returned
2510 * Simply returns clk->parent. Returns NULL if clk is NULL.
2512 struct clk *clk_get_parent(struct clk *clk) in clk_get_parent() argument
2514 struct clk *parent; in clk_get_parent()
2516 if (!clk) in clk_get_parent()
2520 /* TODO: Create a per-user clk and change callers to call clk_put */ in clk_get_parent()
2521 parent = !clk->core->parent ? NULL : clk->core->parent->hw->clk; in clk_get_parent()
2532 if (core->num_parents > 1 && core->ops->get_parent) in __clk_init_parent()
2533 index = core->ops->get_parent(core->hw); in __clk_init_parent()
2551 clk_core_reparent(hw->core, !new_parent ? NULL : new_parent->core); in clk_hw_reparent()
2555 * clk_has_parent - check if a clock is a possible parent for another
2556 * @clk: clock source
2562 * Returns true if @parent is a possible parent for @clk, false otherwise.
2564 bool clk_has_parent(struct clk *clk, struct clk *parent) in clk_has_parent() argument
2570 if (!clk || !parent) in clk_has_parent()
2573 core = clk->core; in clk_has_parent()
2574 parent_core = parent->core; in clk_has_parent()
2577 if (core->parent == parent_core) in clk_has_parent()
2580 for (i = 0; i < core->num_parents; i++) in clk_has_parent()
2581 if (!strcmp(core->parents[i].name, parent_core->name)) in clk_has_parent()
2600 if (core->parent == parent) in clk_core_set_parent_nolock()
2603 /* verify ops for multi-parent clks */ in clk_core_set_parent_nolock()
2604 if (core->num_parents > 1 && !core->ops->set_parent) in clk_core_set_parent_nolock()
2605 return -EPERM; in clk_core_set_parent_nolock()
2607 /* check that we are allowed to re-parent if the clock is in use */ in clk_core_set_parent_nolock()
2608 if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) in clk_core_set_parent_nolock()
2609 return -EBUSY; in clk_core_set_parent_nolock()
2612 return -EBUSY; in clk_core_set_parent_nolock()
2618 pr_debug("%s: clk %s can not be parent of clk %s\n", in clk_core_set_parent_nolock()
2619 __func__, parent->name, core->name); in clk_core_set_parent_nolock()
2622 p_rate = parent->rate; in clk_core_set_parent_nolock()
2636 /* do the re-parent */ in clk_core_set_parent_nolock()
2655 return clk_core_set_parent_nolock(hw->core, parent->core); in clk_hw_set_parent()
2660 * clk_set_parent - switch the parent of a mux clk
2661 * @clk: the mux clk whose input we are switching
2662 * @parent: the new input to clk
2664 * Re-parent clk to use parent as its new input source. If clk is in
2665 * prepared state, the clk will get enabled for the duration of this call. If
2666 * that's not acceptable for a specific clk (Eg: the consumer can't handle
2668 * CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
2670 * After successfully changing clk's parent clk_set_parent will update the
2671 * clk topology, sysfs topology and propagate rate recalculation via
2674 * Returns 0 on success, -EERROR otherwise.
2676 int clk_set_parent(struct clk *clk, struct clk *parent) in clk_set_parent() argument
2680 if (!clk) in clk_set_parent()
2685 if (clk->exclusive_count) in clk_set_parent()
2686 clk_core_rate_unprotect(clk->core); in clk_set_parent()
2688 ret = clk_core_set_parent_nolock(clk->core, in clk_set_parent()
2689 parent ? parent->core : NULL); in clk_set_parent()
2691 if (clk->exclusive_count) in clk_set_parent()
2692 clk_core_rate_protect(clk->core); in clk_set_parent()
2702 int ret = -EINVAL; in clk_core_set_phase_nolock()
2710 return -EBUSY; in clk_core_set_phase_nolock()
2714 if (core->ops->set_phase) { in clk_core_set_phase_nolock()
2715 ret = core->ops->set_phase(core->hw, degrees); in clk_core_set_phase_nolock()
2717 core->phase = degrees; in clk_core_set_phase_nolock()
2726 * clk_set_phase - adjust the phase shift of a clock signal
2727 * @clk: clock signal source
2730 * Shifts the phase of a clock signal by the specified
2731 * degrees. Returns 0 on success, -EERROR otherwise.
2734 * signal that we adjust the clock signal phase against. For example
2735 * phase locked-loop clock signal generators we may shift phase with
2737 * clock phase may be shifted with respect to some other, unspecified
2740 * Additionally the concept of phase shift does not propagate through
2742 * clock accuracy. A parent clock phase attribute does not have an
2743 * impact on the phase attribute of a child clock.
2745 int clk_set_phase(struct clk *clk, int degrees) in clk_set_phase() argument
2749 if (!clk) in clk_set_phase()
2759 if (clk->exclusive_count) in clk_set_phase()
2760 clk_core_rate_unprotect(clk->core); in clk_set_phase()
2762 ret = clk_core_set_phase_nolock(clk->core, degrees); in clk_set_phase()
2764 if (clk->exclusive_count) in clk_set_phase()
2765 clk_core_rate_protect(clk->core); in clk_set_phase()
2778 if (!core->ops->get_phase) in clk_core_get_phase()
2781 /* Always try to update cached phase if possible */ in clk_core_get_phase()
2782 ret = core->ops->get_phase(core->hw); in clk_core_get_phase()
2784 core->phase = ret; in clk_core_get_phase()
2790 * clk_get_phase - return the phase shift of a clock signal
2791 * @clk: clock signal source
2793 * Returns the phase shift of a clock node in degrees, otherwise returns
2794 * -EERROR.
2796 int clk_get_phase(struct clk *clk) in clk_get_phase() argument
2800 if (!clk) in clk_get_phase()
2804 ret = clk_core_get_phase(clk->core); in clk_get_phase()
2814 core->duty.num = 1; in clk_core_reset_duty_cycle_nolock()
2815 core->duty.den = 2; in clk_core_reset_duty_cycle_nolock()
2822 struct clk_duty *duty = &core->duty; in clk_core_update_duty_cycle_nolock()
2825 if (!core->ops->get_duty_cycle) in clk_core_update_duty_cycle_nolock()
2828 ret = core->ops->get_duty_cycle(core->hw, duty); in clk_core_update_duty_cycle_nolock()
2833 if (duty->den == 0 || duty->num > duty->den) { in clk_core_update_duty_cycle_nolock()
2834 ret = -EINVAL; in clk_core_update_duty_cycle_nolock()
2849 if (core->parent && in clk_core_update_duty_cycle_parent_nolock()
2850 core->flags & CLK_DUTY_CYCLE_PARENT) { in clk_core_update_duty_cycle_parent_nolock()
2851 ret = clk_core_update_duty_cycle_nolock(core->parent); in clk_core_update_duty_cycle_parent_nolock()
2852 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty)); in clk_core_update_duty_cycle_parent_nolock()
2871 return -EBUSY; in clk_core_set_duty_cycle_nolock()
2875 if (!core->ops->set_duty_cycle) in clk_core_set_duty_cycle_nolock()
2878 ret = core->ops->set_duty_cycle(core->hw, duty); in clk_core_set_duty_cycle_nolock()
2880 memcpy(&core->duty, duty, sizeof(*duty)); in clk_core_set_duty_cycle_nolock()
2892 if (core->parent && in clk_core_set_duty_cycle_parent_nolock()
2893 core->flags & (CLK_DUTY_CYCLE_PARENT | CLK_SET_RATE_PARENT)) { in clk_core_set_duty_cycle_parent_nolock()
2894 ret = clk_core_set_duty_cycle_nolock(core->parent, duty); in clk_core_set_duty_cycle_parent_nolock()
2895 memcpy(&core->duty, &core->parent->duty, sizeof(core->duty)); in clk_core_set_duty_cycle_parent_nolock()
2902 * clk_set_duty_cycle - adjust the duty cycle ratio of a clock signal
2903 * @clk: clock signal source
2912 int clk_set_duty_cycle(struct clk *clk, unsigned int num, unsigned int den) in clk_set_duty_cycle() argument
2917 if (!clk) in clk_set_duty_cycle()
2922 return -EINVAL; in clk_set_duty_cycle()
2929 if (clk->exclusive_count) in clk_set_duty_cycle()
2930 clk_core_rate_unprotect(clk->core); in clk_set_duty_cycle()
2932 ret = clk_core_set_duty_cycle_nolock(clk->core, &duty); in clk_set_duty_cycle()
2934 if (clk->exclusive_count) in clk_set_duty_cycle()
2935 clk_core_rate_protect(clk->core); in clk_set_duty_cycle()
2946 struct clk_duty *duty = &core->duty; in clk_core_get_scaled_duty_cycle()
2953 ret = mult_frac(scale, duty->num, duty->den); in clk_core_get_scaled_duty_cycle()
2961 * clk_get_scaled_duty_cycle - return the duty cycle ratio of a clock signal
2962 * @clk: clock signal source
2968 int clk_get_scaled_duty_cycle(struct clk *clk, unsigned int scale) in clk_get_scaled_duty_cycle() argument
2970 if (!clk) in clk_get_scaled_duty_cycle()
2973 return clk_core_get_scaled_duty_cycle(clk->core, scale); in clk_get_scaled_duty_cycle()
2978 * clk_is_match - check if two clk's point to the same hardware clock
2979 * @p: clk compared against q
2980 * @q: clk compared against p
2982 * Returns true if the two struct clk pointers both point to the same hardware
2983 * clock node. Put differently, returns true if struct clk *p and struct clk *q
2988 bool clk_is_match(const struct clk *p, const struct clk *q) in clk_is_match()
2990 /* trivial case: identical struct clk's or both NULL */ in clk_is_match()
2994 /* true if clk->core pointers match. Avoid dereferencing garbage */ in clk_is_match()
2996 if (p->core == q->core) in clk_is_match()
3021 int phase; in clk_summary_show_one() local
3023 seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu ", in clk_summary_show_one()
3025 30 - level * 3, c->name, in clk_summary_show_one()
3026 c->enable_count, c->prepare_count, c->protect_count, in clk_summary_show_one()
3030 phase = clk_core_get_phase(c); in clk_summary_show_one()
3031 if (phase >= 0) in clk_summary_show_one()
3032 seq_printf(s, "%5d", phase); in clk_summary_show_one()
3034 seq_puts(s, "-----"); in clk_summary_show_one()
3046 hlist_for_each_entry(child, &c->children, child_node) in clk_summary_show_subtree()
3053 struct hlist_head **lists = (struct hlist_head **)s->private; in clk_summary_show()
3056 …" clock count count count rate accuracy phase cycle\n"); in clk_summary_show()
3057 …seq_puts(s, "-------------------------------------------------------------------------------------… in clk_summary_show()
3073 int phase; in clk_dump_one() local
3079 seq_printf(s, "\"%s\": { ", c->name); in clk_dump_one()
3080 seq_printf(s, "\"enable_count\": %d,", c->enable_count); in clk_dump_one()
3081 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count); in clk_dump_one()
3082 seq_printf(s, "\"protect_count\": %d,", c->protect_count); in clk_dump_one()
3087 phase = clk_core_get_phase(c); in clk_dump_one()
3088 if (phase >= 0) in clk_dump_one()
3089 seq_printf(s, "\"phase\": %d,", phase); in clk_dump_one()
3100 hlist_for_each_entry(child, &c->children, child_node) { in clk_dump_subtree()
3112 struct hlist_head **lists = (struct hlist_head **)s->private; in clk_dump_show()
3164 ret = clk_prepare_enable(core->hw->clk); in clk_prepare_enable_set()
3166 clk_disable_unprepare(core->hw->clk); in clk_prepare_enable_set()
3175 *val = core->enable_count && core->prepare_count; in clk_prepare_enable_get()
3191 *val = core->rate; in clk_rate_get()
3219 struct clk_core *core = s->private; in clk_flags_show()
3220 unsigned long flags = core->flags; in clk_flags_show()
3249 * 4. Fetch parent clock's clock-output-name if DT index was set in possible_parent_show()
3257 seq_puts(s, parent->name); in possible_parent_show()
3258 else if (core->parents[i].name) in possible_parent_show()
3259 seq_puts(s, core->parents[i].name); in possible_parent_show()
3260 else if (core->parents[i].fw_name) in possible_parent_show()
3261 seq_printf(s, "<%s>(fw)", core->parents[i].fw_name); in possible_parent_show()
3262 else if (core->parents[i].index >= 0) in possible_parent_show()
3264 of_clk_get_parent_name(core->of_node, in possible_parent_show()
3265 core->parents[i].index)); in possible_parent_show()
3274 struct clk_core *core = s->private; in possible_parents_show()
3277 for (i = 0; i < core->num_parents - 1; i++) in possible_parents_show()
3288 struct clk_core *core = s->private; in current_parent_show()
3290 if (core->parent) in current_parent_show()
3291 seq_printf(s, "%s\n", core->parent->name); in current_parent_show()
3299 struct clk_core *core = s->private; in clk_duty_cycle_show()
3300 struct clk_duty *duty = &core->duty; in clk_duty_cycle_show()
3302 seq_printf(s, "%u/%u\n", duty->num, duty->den); in clk_duty_cycle_show()
3310 struct clk_core *core = s->private; in clk_min_rate_show()
3324 struct clk_core *core = s->private; in clk_max_rate_show()
3343 root = debugfs_create_dir(core->name, pdentry); in clk_debug_create_one()
3344 core->dentry = root; in clk_debug_create_one()
3350 debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy); in clk_debug_create_one()
3351 debugfs_create_u32("clk_phase", 0444, root, &core->phase); in clk_debug_create_one()
3353 debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count); in clk_debug_create_one()
3354 debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count); in clk_debug_create_one()
3355 debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count); in clk_debug_create_one()
3356 debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count); in clk_debug_create_one()
3364 if (core->num_parents > 0) in clk_debug_create_one()
3368 if (core->num_parents > 1) in clk_debug_create_one()
3372 if (core->ops->debug_init) in clk_debug_create_one()
3373 core->ops->debug_init(core->hw, core->dentry); in clk_debug_create_one()
3377 * clk_debug_register - add a clk node to the debugfs clk directory
3378 * @core: the clk being added to the debugfs clk directory
3380 * Dynamically adds a clk to the debugfs clk directory if debugfs has been
3381 * initialized. Otherwise it bails out early since the debugfs clk directory
3387 hlist_add_head(&core->debug_node, &clk_debug_list); in clk_debug_register()
3394 * clk_debug_unregister - remove a clk node from the debugfs clk directory
3395 * @core: the clk being removed from the debugfs clk directory
3397 * Dynamically removes a clk and all its child nodes from the
3398 * debugfs clk directory if clk->dentry points to debugfs created by
3404 hlist_del_init(&core->debug_node); in clk_debug_unregister()
3405 debugfs_remove_recursive(core->dentry); in clk_debug_unregister()
3406 core->dentry = NULL; in clk_debug_unregister()
3411 * clk_debug_init - lazily populate the debugfs clk directory
3415 * populates the debugfs clk directory once at boot-time when we know that
3416 * debugfs is setup. It should only be called once at boot-time, all other clks
3428 pr_warn("** WRITEABLE clk DebugFS SUPPORT HAS BEEN ENABLED IN THIS KERNEL **\n"); in clk_debug_init()
3430 pr_warn("** This means that this kernel is built to expose clk operations **\n"); in clk_debug_init()
3441 rootdir = debugfs_create_dir("clk", NULL); in clk_debug_init()
3488 /* update the clk tree topology */ in clk_core_reparent_orphans_nolock()
3503 * 'req_rate' is set to something non-zero so that in clk_core_reparent_orphans_nolock()
3506 orphan->req_rate = orphan->rate; in clk_core_reparent_orphans_nolock()
3512 * __clk_core_init - initialize the data structures in a struct clk_core
3523 int phase; in __clk_core_init() local
3526 return -EINVAL; in __clk_core_init()
3531 * Set hw->core after grabbing the prepare_lock to synchronize with in __clk_core_init()
3532 * callers of clk_core_fill_parent_index() where we treat hw->core in __clk_core_init()
3533 * being NULL as the clk not being registered yet. This is crucial so in __clk_core_init()
3536 core->hw->core = core; in __clk_core_init()
3543 if (clk_core_lookup(core->name)) { in __clk_core_init()
3544 pr_debug("%s: clk %s already initialized\n", in __clk_core_init()
3545 __func__, core->name); in __clk_core_init()
3546 ret = -EEXIST; in __clk_core_init()
3550 /* check that clk_ops are sane. See Documentation/driver-api/clk.rst */ in __clk_core_init()
3551 if (core->ops->set_rate && in __clk_core_init()
3552 !((core->ops->round_rate || core->ops->determine_rate) && in __clk_core_init()
3553 core->ops->recalc_rate)) { in __clk_core_init()
3555 __func__, core->name); in __clk_core_init()
3556 ret = -EINVAL; in __clk_core_init()
3560 if (core->ops->set_parent && !core->ops->get_parent) { in __clk_core_init()
3562 __func__, core->name); in __clk_core_init()
3563 ret = -EINVAL; in __clk_core_init()
3567 if (core->num_parents > 1 && !core->ops->get_parent) { in __clk_core_init()
3569 __func__, core->name); in __clk_core_init()
3570 ret = -EINVAL; in __clk_core_init()
3574 if (core->ops->set_rate_and_parent && in __clk_core_init()
3575 !(core->ops->set_parent && core->ops->set_rate)) { in __clk_core_init()
3577 __func__, core->name); in __clk_core_init()
3578 ret = -EINVAL; in __clk_core_init()
3583 * optional platform-specific magic in __clk_core_init()
3596 if (core->ops->init) { in __clk_core_init()
3597 ret = core->ops->init(core->hw); in __clk_core_init()
3602 parent = core->parent = __clk_init_parent(core); in __clk_core_init()
3605 * Populate core->parent if parent has already been clk_core_init'd. If in __clk_core_init()
3606 * parent has not yet been clk_core_init'd then place clk in the orphan in __clk_core_init()
3607 * list. If clk doesn't have any parents then place it in the root in __clk_core_init()
3608 * clk list. in __clk_core_init()
3610 * Every time a new clk is clk_init'd then we walk the list of orphan in __clk_core_init()
3611 * clocks and re-parent any that are children of the clock currently in __clk_core_init()
3615 hlist_add_head(&core->child_node, &parent->children); in __clk_core_init()
3616 core->orphan = parent->orphan; in __clk_core_init()
3617 } else if (!core->num_parents) { in __clk_core_init()
3618 hlist_add_head(&core->child_node, &clk_root_list); in __clk_core_init()
3619 core->orphan = false; in __clk_core_init()
3621 hlist_add_head(&core->child_node, &clk_orphan_list); in __clk_core_init()
3622 core->orphan = true; in __clk_core_init()
3626 * Set clk's accuracy. The preferred method is to use in __clk_core_init()
3632 if (core->ops->recalc_accuracy) in __clk_core_init()
3633 core->accuracy = core->ops->recalc_accuracy(core->hw, in __clk_core_init()
3636 core->accuracy = parent->accuracy; in __clk_core_init()
3638 core->accuracy = 0; in __clk_core_init()
3641 * Set clk's phase by clk_core_get_phase() caching the phase. in __clk_core_init()
3642 * Since a phase is by definition relative to its parent, just in __clk_core_init()
3643 * query the current clock phase, or just assume it's in phase. in __clk_core_init()
3645 phase = clk_core_get_phase(core); in __clk_core_init()
3646 if (phase < 0) { in __clk_core_init()
3647 ret = phase; in __clk_core_init()
3648 pr_warn("%s: Failed to get phase for clk '%s'\n", __func__, in __clk_core_init()
3649 core->name); in __clk_core_init()
3654 * Set clk's duty cycle. in __clk_core_init()
3659 * Set clk's rate. The preferred method is to use .recalc_rate. For in __clk_core_init()
3664 if (core->ops->recalc_rate) in __clk_core_init()
3665 rate = core->ops->recalc_rate(core->hw, in __clk_core_init()
3668 rate = parent->rate; in __clk_core_init()
3671 core->rate = core->req_rate = rate; in __clk_core_init()
3673 core->boot_enabled = clk_core_is_enabled(core); in __clk_core_init()
3680 if (core->flags & CLK_IS_CRITICAL) { in __clk_core_init()
3685 pr_warn("%s: critical clk '%s' failed to prepare\n", in __clk_core_init()
3686 __func__, core->name); in __clk_core_init()
3694 pr_warn("%s: critical clk '%s' failed to enable\n", in __clk_core_init()
3695 __func__, core->name); in __clk_core_init()
3705 kref_init(&core->ref); in __clk_core_init()
3710 hlist_del_init(&core->child_node); in __clk_core_init()
3711 core->hw->core = NULL; in __clk_core_init()
3723 * clk_core_link_consumer - Add a clk consumer to the list of consumers in a clk_core
3724 * @core: clk to add consumer to
3725 * @clk: consumer to link to a clk
3727 static void clk_core_link_consumer(struct clk_core *core, struct clk *clk) in clk_core_link_consumer() argument
3730 hlist_add_head(&clk->clks_node, &core->clks); in clk_core_link_consumer()
3735 * clk_core_unlink_consumer - Remove a clk consumer from the list of consumers in a clk_core
3736 * @clk: consumer to unlink
3738 static void clk_core_unlink_consumer(struct clk *clk) in clk_core_unlink_consumer() argument
3741 hlist_del(&clk->clks_node); in clk_core_unlink_consumer()
3745 * alloc_clk - Allocate a clk consumer, but leave it unlinked to the clk_core
3746 * @core: clk to allocate a consumer for
3750 * Returns: clk consumer left unlinked from the consumer list
3752 static struct clk *alloc_clk(struct clk_core *core, const char *dev_id, in alloc_clk()
3755 struct clk *clk; in alloc_clk() local
3757 clk = kzalloc(sizeof(*clk), GFP_KERNEL); in alloc_clk()
3758 if (!clk) in alloc_clk()
3759 return ERR_PTR(-ENOMEM); in alloc_clk()
3761 clk->core = core; in alloc_clk()
3762 clk->dev_id = dev_id; in alloc_clk()
3763 clk->con_id = kstrdup_const(con_id, GFP_KERNEL); in alloc_clk()
3764 clk->max_rate = ULONG_MAX; in alloc_clk()
3766 return clk; in alloc_clk()
3770 * free_clk - Free a clk consumer
3771 * @clk: clk consumer to free
3773 * Note, this assumes the clk has been unlinked from the clk_core consumer
3776 static void free_clk(struct clk *clk) in free_clk() argument
3778 kfree_const(clk->con_id); in free_clk()
3779 kfree(clk); in free_clk()
3783 * clk_hw_create_clk: Allocate and link a clk consumer to a clk_core given
3785 * @dev: clk consumer device
3786 * @hw: clk_hw associated with the clk being consumed
3790 * This is the main function used to create a clk pointer for use by clk
3792 * used by the framework and clk provider respectively.
3794 struct clk *clk_hw_create_clk(struct device *dev, struct clk_hw *hw, in clk_hw_create_clk()
3797 struct clk *clk; in clk_hw_create_clk() local
3804 core = hw->core; in clk_hw_create_clk()
3805 clk = alloc_clk(core, dev_id, con_id); in clk_hw_create_clk()
3806 if (IS_ERR(clk)) in clk_hw_create_clk()
3807 return clk; in clk_hw_create_clk()
3808 clk->dev = dev; in clk_hw_create_clk()
3810 if (!try_module_get(core->owner)) { in clk_hw_create_clk()
3811 free_clk(clk); in clk_hw_create_clk()
3812 return ERR_PTR(-ENOENT); in clk_hw_create_clk()
3815 kref_get(&core->ref); in clk_hw_create_clk()
3816 clk_core_link_consumer(core, clk); in clk_hw_create_clk()
3818 return clk; in clk_hw_create_clk()
3822 * clk_hw_get_clk - get clk consumer given an clk_hw
3823 * @hw: clk_hw associated with the clk being consumed
3826 * Returns: new clk consumer
3828 * to get a consumer clk and act on the clock element
3831 struct clk *clk_hw_get_clk(struct clk_hw *hw, const char *con_id) in clk_hw_get_clk()
3833 struct device *dev = hw->core->dev; in clk_hw_get_clk()
3846 return -EINVAL; in clk_cpy_name()
3852 return -ENOMEM; in clk_cpy_name()
3860 u8 num_parents = init->num_parents; in clk_core_populate_parent_map()
3861 const char * const *parent_names = init->parent_names; in clk_core_populate_parent_map()
3862 const struct clk_hw **parent_hws = init->parent_hws; in clk_core_populate_parent_map()
3863 const struct clk_parent_data *parent_data = init->parent_data; in clk_core_populate_parent_map()
3871 * Avoid unnecessary string look-ups of clk_core's possible parents by in clk_core_populate_parent_map()
3875 core->parents = parents; in clk_core_populate_parent_map()
3877 return -ENOMEM; in clk_core_populate_parent_map()
3881 parent->index = -1; in clk_core_populate_parent_map()
3886 __func__, core->name); in clk_core_populate_parent_map()
3887 ret = clk_cpy_name(&parent->name, parent_names[i], in clk_core_populate_parent_map()
3890 parent->hw = parent_data[i].hw; in clk_core_populate_parent_map()
3891 parent->index = parent_data[i].index; in clk_core_populate_parent_map()
3892 ret = clk_cpy_name(&parent->fw_name, in clk_core_populate_parent_map()
3895 ret = clk_cpy_name(&parent->name, in clk_core_populate_parent_map()
3899 parent->hw = parent_hws[i]; in clk_core_populate_parent_map()
3901 ret = -EINVAL; in clk_core_populate_parent_map()
3909 } while (--i >= 0); in clk_core_populate_parent_map()
3921 int i = core->num_parents; in clk_core_free_parent_map()
3923 if (!core->num_parents) in clk_core_free_parent_map()
3926 while (--i >= 0) { in clk_core_free_parent_map()
3927 kfree_const(core->parents[i].name); in clk_core_free_parent_map()
3928 kfree_const(core->parents[i].fw_name); in clk_core_free_parent_map()
3931 kfree(core->parents); in clk_core_free_parent_map()
3934 static struct clk *
3939 const struct clk_init_data *init = hw->init; in __clk_register()
3944 * we catch use of hw->init early on in the core. in __clk_register()
3946 hw->init = NULL; in __clk_register()
3950 ret = -ENOMEM; in __clk_register()
3954 core->name = kstrdup_const(init->name, GFP_KERNEL); in __clk_register()
3955 if (!core->name) { in __clk_register()
3956 ret = -ENOMEM; in __clk_register()
3960 if (WARN_ON(!init->ops)) { in __clk_register()
3961 ret = -EINVAL; in __clk_register()
3964 core->ops = init->ops; in __clk_register()
3967 core->rpm_enabled = true; in __clk_register()
3968 core->dev = dev; in __clk_register()
3969 core->of_node = np; in __clk_register()
3970 if (dev && dev->driver) in __clk_register()
3971 core->owner = dev->driver->owner; in __clk_register()
3972 core->hw = hw; in __clk_register()
3973 core->flags = init->flags; in __clk_register()
3974 core->num_parents = init->num_parents; in __clk_register()
3975 core->min_rate = 0; in __clk_register()
3976 core->max_rate = ULONG_MAX; in __clk_register()
3982 INIT_HLIST_HEAD(&core->clks); in __clk_register()
3988 hw->clk = alloc_clk(core, NULL, NULL); in __clk_register()
3989 if (IS_ERR(hw->clk)) { in __clk_register()
3990 ret = PTR_ERR(hw->clk); in __clk_register()
3994 clk_core_link_consumer(core, hw->clk); in __clk_register()
3998 return hw->clk; in __clk_register()
4001 clk_core_unlink_consumer(hw->clk); in __clk_register()
4004 free_clk(hw->clk); in __clk_register()
4005 hw->clk = NULL; in __clk_register()
4011 kfree_const(core->name); in __clk_register()
4019 * dev_or_parent_of_node() - Get device node of @dev or @dev's parent
4023 * @dev->parent if dev doesn't have a device node, or NULL if neither
4024 * @dev or @dev->parent have a device node.
4035 np = dev_of_node(dev->parent); in dev_or_parent_of_node()
4041 * clk_register - allocate a new clock, register it and return an opaque cookie
4043 * @hw: link to hardware-specific clock data
4048 * Returns: a pointer to the newly allocated struct clk which
4053 struct clk *clk_register(struct device *dev, struct clk_hw *hw) in clk_register()
4060 * clk_hw_register - register a clk_hw and return an error code
4062 * @hw: link to hardware-specific clock data
4077 * of_clk_hw_register - register a clk_hw and return an error code
4079 * @hw: link to hardware-specific clock data
4101 kfree_const(core->name); in __clk_release()
4108 * consumer calls clk_put() and the struct clk object is freed.
4112 return -ENXIO; in clk_nodrv_prepare_enable()
4123 return -ENXIO; in clk_nodrv_set_rate()
4128 return -ENXIO; in clk_nodrv_set_parent()
4146 for (i = 0; i < root->num_parents; i++) in clk_core_evict_parent_cache_subtree()
4147 if (root->parents[i].core == target) in clk_core_evict_parent_cache_subtree()
4148 root->parents[i].core = NULL; in clk_core_evict_parent_cache_subtree()
4150 hlist_for_each_entry(child, &root->children, child_node) in clk_core_evict_parent_cache_subtree()
4154 /* Remove this clk from all parent caches */
4169 * clk_unregister - unregister a currently registered clock
4170 * @clk: clock to unregister
4172 void clk_unregister(struct clk *clk) in clk_unregister() argument
4177 if (!clk || WARN_ON_ONCE(IS_ERR(clk))) in clk_unregister()
4180 clk_debug_unregister(clk->core); in clk_unregister()
4184 ops = clk->core->ops; in clk_unregister()
4187 clk->core->name); in clk_unregister()
4195 clk->core->ops = &clk_nodrv_ops; in clk_unregister()
4198 if (ops->terminate) in clk_unregister()
4199 ops->terminate(clk->core->hw); in clk_unregister()
4201 if (!hlist_empty(&clk->core->children)) { in clk_unregister()
4206 hlist_for_each_entry_safe(child, t, &clk->core->children, in clk_unregister()
4211 clk_core_evict_parent_cache(clk->core); in clk_unregister()
4213 hlist_del_init(&clk->core->child_node); in clk_unregister()
4215 if (clk->core->prepare_count) in clk_unregister()
4217 __func__, clk->core->name); in clk_unregister()
4219 if (clk->core->protect_count) in clk_unregister()
4221 __func__, clk->core->name); in clk_unregister()
4223 kref_put(&clk->core->ref, __clk_release); in clk_unregister()
4224 free_clk(clk); in clk_unregister()
4231 * clk_hw_unregister - unregister a currently registered clk_hw
4232 * @hw: hardware-specific clock data to unregister
4236 clk_unregister(hw->clk); in clk_hw_unregister()
4242 clk_unregister(*(struct clk **)res); in devm_clk_unregister_cb()
4251 * devm_clk_register - resource managed clk_register()
4253 * @hw: link to hardware-specific clock data
4260 struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw) in devm_clk_register()
4262 struct clk *clk; in devm_clk_register() local
4263 struct clk **clkp; in devm_clk_register()
4267 return ERR_PTR(-ENOMEM); in devm_clk_register()
4269 clk = clk_register(dev, hw); in devm_clk_register()
4270 if (!IS_ERR(clk)) { in devm_clk_register()
4271 *clkp = clk; in devm_clk_register()
4277 return clk; in devm_clk_register()
4282 * devm_clk_hw_register - resource managed clk_hw_register()
4284 * @hw: link to hardware-specific clock data
4297 return -ENOMEM; in devm_clk_hw_register()
4313 struct clk *c = res; in devm_clk_match()
4329 * devm_clk_unregister - resource managed clk_unregister()
4331 * @clk: clock to unregister
4337 void devm_clk_unregister(struct device *dev, struct clk *clk) in devm_clk_unregister() argument
4339 WARN_ON(devres_release(dev, devm_clk_unregister_cb, devm_clk_match, clk)); in devm_clk_unregister()
4344 * devm_clk_hw_unregister - resource managed clk_hw_unregister()
4345 * @dev: device that is unregistering the hardware-specific clock data
4346 * @hw: link to hardware-specific clock data
4361 clk_put(*(struct clk **)res); in devm_clk_release()
4365 * devm_clk_hw_get_clk - resource managed clk_hw_get_clk()
4367 * @hw: clk_hw associated with the clk being consumed
4374 struct clk *devm_clk_hw_get_clk(struct device *dev, struct clk_hw *hw, in devm_clk_hw_get_clk()
4377 struct clk *clk; in devm_clk_hw_get_clk() local
4378 struct clk **clkp; in devm_clk_hw_get_clk()
4384 WARN_ON_ONCE(dev != hw->core->dev); in devm_clk_hw_get_clk()
4388 return ERR_PTR(-ENOMEM); in devm_clk_hw_get_clk()
4390 clk = clk_hw_get_clk(hw, con_id); in devm_clk_hw_get_clk()
4391 if (!IS_ERR(clk)) { in devm_clk_hw_get_clk()
4392 *clkp = clk; in devm_clk_hw_get_clk()
4398 return clk; in devm_clk_hw_get_clk()
4406 void __clk_put(struct clk *clk) in __clk_put() argument
4410 if (!clk || WARN_ON_ONCE(IS_ERR(clk))) in __clk_put()
4420 if (WARN_ON(clk->exclusive_count)) { in __clk_put()
4422 clk->core->protect_count -= (clk->exclusive_count - 1); in __clk_put()
4423 clk_core_rate_unprotect(clk->core); in __clk_put()
4424 clk->exclusive_count = 0; in __clk_put()
4427 hlist_del(&clk->clks_node); in __clk_put()
4428 if (clk->min_rate > clk->core->req_rate || in __clk_put()
4429 clk->max_rate < clk->core->req_rate) in __clk_put()
4430 clk_core_set_rate_nolock(clk->core, clk->core->req_rate); in __clk_put()
4432 owner = clk->core->owner; in __clk_put()
4433 kref_put(&clk->core->ref, __clk_release); in __clk_put()
4439 free_clk(clk); in __clk_put()
4442 /*** clk rate change notifiers ***/
4445 * clk_notifier_register - add a clk rate change notifier
4446 * @clk: struct clk * to watch
4449 * Request notification when clk's rate changes. This uses an SRCU
4452 * re-enter into the clk framework by calling any top-level clk APIs;
4459 * clk_notifier_register() must be called from non-atomic context.
4460 * Returns -EINVAL if called with null arguments, -ENOMEM upon
4464 int clk_notifier_register(struct clk *clk, struct notifier_block *nb) in clk_notifier_register() argument
4467 int ret = -ENOMEM; in clk_notifier_register()
4469 if (!clk || !nb) in clk_notifier_register()
4470 return -EINVAL; in clk_notifier_register()
4474 /* search the list of notifiers for this clk */ in clk_notifier_register()
4476 if (cn->clk == clk) in clk_notifier_register()
4479 /* if clk wasn't in the notifier list, allocate new clk_notifier */ in clk_notifier_register()
4484 cn->clk = clk; in clk_notifier_register()
4485 srcu_init_notifier_head(&cn->notifier_head); in clk_notifier_register()
4487 list_add(&cn->node, &clk_notifier_list); in clk_notifier_register()
4490 ret = srcu_notifier_chain_register(&cn->notifier_head, nb); in clk_notifier_register()
4492 clk->core->notifier_count++; in clk_notifier_register()
4502 * clk_notifier_unregister - remove a clk rate change notifier
4503 * @clk: struct clk *
4506 * Request no further notification for changes to 'clk' and frees memory
4509 * Returns -EINVAL if called with null arguments; otherwise, passes
4512 int clk_notifier_unregister(struct clk *clk, struct notifier_block *nb) in clk_notifier_unregister() argument
4515 int ret = -ENOENT; in clk_notifier_unregister()
4517 if (!clk || !nb) in clk_notifier_unregister()
4518 return -EINVAL; in clk_notifier_unregister()
4523 if (cn->clk == clk) { in clk_notifier_unregister()
4524 ret = srcu_notifier_chain_unregister(&cn->notifier_head, nb); in clk_notifier_unregister()
4526 clk->core->notifier_count--; in clk_notifier_unregister()
4529 if (!cn->notifier_head.head) { in clk_notifier_unregister()
4530 srcu_cleanup_notifier_head(&cn->notifier_head); in clk_notifier_unregister()
4531 list_del(&cn->node); in clk_notifier_unregister()
4545 struct clk *clk; member
4553 clk_notifier_unregister(devres->clk, devres->nb); in devm_clk_notifier_release()
4556 int devm_clk_notifier_register(struct device *dev, struct clk *clk, in devm_clk_notifier_register() argument
4566 return -ENOMEM; in devm_clk_notifier_register()
4568 ret = clk_notifier_register(clk, nb); in devm_clk_notifier_register()
4570 devres->clk = clk; in devm_clk_notifier_register()
4571 devres->nb = nb; in devm_clk_notifier_register()
4589 * struct of_clk_provider - Clock provider registration structure
4592 * @get: Get clock callback. Returns NULL or a struct clk for the
4602 struct clk *(*get)(struct of_phandle_args *clkspec, void *data);
4614 struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec, in of_clk_src_simple_get()
4627 struct clk *of_clk_src_onecell_get(struct of_phandle_args *clkspec, void *data) in of_clk_src_onecell_get()
4630 unsigned int idx = clkspec->args[0]; in of_clk_src_onecell_get()
4632 if (idx >= clk_data->clk_num) { in of_clk_src_onecell_get()
4634 return ERR_PTR(-EINVAL); in of_clk_src_onecell_get()
4637 return clk_data->clks[idx]; in of_clk_src_onecell_get()
4645 unsigned int idx = clkspec->args[0]; in of_clk_hw_onecell_get()
4647 if (idx >= hw_data->num) { in of_clk_hw_onecell_get()
4649 return ERR_PTR(-EINVAL); in of_clk_hw_onecell_get()
4652 return hw_data->hws[idx]; in of_clk_hw_onecell_get()
4657 * of_clk_add_provider() - Register a clock provider for a node
4665 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec, in of_clk_add_provider()
4677 return -ENOMEM; in of_clk_add_provider()
4679 cp->node = of_node_get(np); in of_clk_add_provider()
4680 cp->data = data; in of_clk_add_provider()
4681 cp->get = clk_src_get; in of_clk_add_provider()
4684 list_add(&cp->link, &of_clk_providers); in of_clk_add_provider()
4694 fwnode_dev_initialized(&np->fwnode, true); in of_clk_add_provider()
4701 * of_clk_add_hw_provider() - Register a clock provider for a node
4719 return -ENOMEM; in of_clk_add_hw_provider()
4721 cp->node = of_node_get(np); in of_clk_add_hw_provider()
4722 cp->data = data; in of_clk_add_hw_provider()
4723 cp->get_hw = get; in of_clk_add_hw_provider()
4726 list_add(&cp->link, &of_clk_providers); in of_clk_add_hw_provider()
4747 * for cases like MFD sub-devices where the child device driver wants to use
4748 * devm_*() APIs but not list the device in DT as a sub-node.
4754 np = dev->of_node; in get_clk_provider_node()
4755 parent_np = dev->parent ? dev->parent->of_node : NULL; in get_clk_provider_node()
4757 if (!of_find_property(np, "#clock-cells", NULL)) in get_clk_provider_node()
4758 if (of_find_property(parent_np, "#clock-cells", NULL)) in get_clk_provider_node()
4765 * devm_of_clk_add_hw_provider() - Managed clk provider node registration
4771 * node or if the device node lacks of clock provider information (#clock-cells)
4773 * has the #clock-cells then it is used in registration. Provider is
4789 return -ENOMEM; in devm_of_clk_add_hw_provider()
4805 * of_clk_del_provider() - Remove a previously registered clock provider
4817 if (cp->node == np) { in of_clk_del_provider()
4818 list_del(&cp->link); in of_clk_del_provider()
4819 fwnode_dev_initialized(&np->fwnode, false); in of_clk_del_provider()
4820 of_node_put(cp->node); in of_clk_del_provider()
4840 * devm_of_clk_del_provider() - Remove clock provider registered using devm
4856 * of_parse_clkspec() - Parse a DT clock specifier for a given device node
4862 * Parses a device node's "clocks" and "clock-names" properties to find the
4865 * parsing error. The @index argument is ignored if @name is non-NULL.
4869 * phandle1: clock-controller@1 {
4870 * #clock-cells = <2>;
4873 * phandle2: clock-controller@2 {
4874 * #clock-cells = <1>;
4877 * clock-consumer@3 {
4879 * clock-names = "name1", "name2";
4882 * To get a device_node for `clock-controller@2' node you may call this
4885 * of_parse_clkspec(clock-consumer@3, -1, "name2", &args);
4886 * of_parse_clkspec(clock-consumer@3, 1, NULL, &args);
4887 * of_parse_clkspec(clock-consumer@3, 1, "name2", &args);
4889 * Return: 0 upon successfully parsing the clock specifier. Otherwise, -ENOENT
4890 * if @name is NULL or -EINVAL if @name is non-NULL and it can't be found in
4891 * the "clock-names" property of @np.
4896 int ret = -ENOENT; in of_parse_clkspec()
4902 * "clock-names" property. If it cannot be found, then index in of_parse_clkspec()
4904 * return -EINVAL. in of_parse_clkspec()
4907 index = of_property_match_string(np, "clock-names", name); in of_parse_clkspec()
4908 ret = of_parse_phandle_with_args(np, "clocks", "#clock-cells", in of_parse_clkspec()
4917 * has a "clock-ranges" property, then we can try one of its in of_parse_clkspec()
4920 np = np->parent; in of_parse_clkspec()
4921 if (np && !of_get_property(np, "clock-ranges", NULL)) in of_parse_clkspec()
4933 struct clk *clk; in __of_clk_get_hw_from_provider() local
4935 if (provider->get_hw) in __of_clk_get_hw_from_provider()
4936 return provider->get_hw(clkspec, provider->data); in __of_clk_get_hw_from_provider()
4938 clk = provider->get(clkspec, provider->data); in __of_clk_get_hw_from_provider()
4939 if (IS_ERR(clk)) in __of_clk_get_hw_from_provider()
4940 return ERR_CAST(clk); in __of_clk_get_hw_from_provider()
4941 return __clk_get_hw(clk); in __of_clk_get_hw_from_provider()
4948 struct clk_hw *hw = ERR_PTR(-EPROBE_DEFER); in of_clk_get_hw_from_clkspec()
4951 return ERR_PTR(-EINVAL); in of_clk_get_hw_from_clkspec()
4955 if (provider->node == clkspec->np) { in of_clk_get_hw_from_clkspec()
4967 * of_clk_get_from_provider() - Lookup a clock from a clock provider
4970 * This function looks up a struct clk from the registered list of clock
4974 struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec) in of_clk_get_from_provider()
4999 static struct clk *__of_clk_get(struct device_node *np, in __of_clk_get()
5008 struct clk *of_clk_get(struct device_node *np, int index) in of_clk_get()
5010 return __of_clk_get(np, index, np->full_name, NULL); in of_clk_get()
5015 * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
5019 * This function parses the clocks and clock-names properties,
5020 * and uses them to look up the struct clk from the registered list of clock
5023 struct clk *of_clk_get_by_name(struct device_node *np, const char *name) in of_clk_get_by_name()
5026 return ERR_PTR(-ENOENT); in of_clk_get_by_name()
5028 return __of_clk_get(np, 0, np->full_name, name); in of_clk_get_by_name()
5033 * of_clk_get_parent_count() - Count the number of clocks a device node has
5042 count = of_count_phandle_with_args(np, "clocks", "#clock-cells"); in of_clk_get_parent_count()
5059 struct clk *clk; in of_clk_get_parent_name() local
5061 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index, in of_clk_get_parent_name()
5070 * specified into an array offset for the clock-output-names property. in of_clk_get_parent_name()
5072 of_property_for_each_u32(clkspec.np, "clock-indices", prop, vp, pv) { in of_clk_get_parent_name()
5079 /* We went off the end of 'clock-indices' without finding it */ in of_clk_get_parent_name()
5083 if (of_property_read_string_index(clkspec.np, "clock-output-names", in of_clk_get_parent_name()
5090 * the clock as long as #clock-cells = 0. in of_clk_get_parent_name()
5092 clk = of_clk_get_from_provider(&clkspec); in of_clk_get_parent_name()
5093 if (IS_ERR(clk)) { in of_clk_get_parent_name()
5095 clk_name = clkspec.np->name; in of_clk_get_parent_name()
5099 clk_name = __clk_get_name(clk); in of_clk_get_parent_name()
5100 clk_put(clk); in of_clk_get_parent_name()
5111 * of_clk_parent_fill() - Fill @parents with names of @np's parents and return
5147 struct clk *clk = of_clk_get(np, i); in parent_ready() local
5150 if (!IS_ERR(clk)) { in parent_ready()
5151 clk_put(clk); in parent_ready()
5157 if (PTR_ERR(clk) == -EPROBE_DEFER) in parent_ready()
5173 * of_clk_detect_critical() - set CLK_IS_CRITICAL flag from Device Tree
5176 * @flags: pointer to top-level framework flags
5178 * Detects if the clock-critical property exists and, if so, sets the
5182 * bindings, such as the one-clock-per-node style that are outdated.
5198 return -EINVAL; in of_clk_detect_critical()
5200 of_property_for_each_u32(np, "clock-critical", prop, cur, idx) in of_clk_detect_critical()
5208 * of_clk_init() - Scan and init clock providers from the DT
5238 list_del(&clk_provider->node); in of_clk_init()
5239 of_node_put(clk_provider->np); in of_clk_init()
5246 parent->clk_init_cb = match->data; in of_clk_init()
5247 parent->np = of_node_get(np); in of_clk_init()
5248 list_add_tail(&parent->node, &clk_provider_list); in of_clk_init()
5255 if (force || parent_ready(clk_provider->np)) { in of_clk_init()
5258 of_node_set_flag(clk_provider->np, in of_clk_init()
5261 clk_provider->clk_init_cb(clk_provider->np); in of_clk_init()
5262 of_clk_set_defaults(clk_provider->np, true); in of_clk_init()
5264 list_del(&clk_provider->node); in of_clk_init()
5265 of_node_put(clk_provider->np); in of_clk_init()
5289 seq_puts(s, "set clk rate:\n"); in clk_rate_show()
5290 seq_puts(s, " echo [clk_name] [rate(Hz)] > /proc/clk/rate\n"); in clk_rate_show()
5308 return -EINVAL; in clk_rate_write()
5311 return -EFAULT; in clk_rate_write()
5317 return -EINVAL; in clk_rate_write()
5322 return -EINVAL; in clk_rate_write()
5346 seq_puts(s, "enable clk:\n"); in clk_enable_show()
5347 seq_puts(s, " echo enable [clk_name] > /proc/clk/enable\n"); in clk_enable_show()
5348 seq_puts(s, "disable clk:\n"); in clk_enable_show()
5349 seq_puts(s, " echo disable [clk_name] > /proc/clk/enable\n"); in clk_enable_show()
5367 return -EINVAL; in clk_enable_write()
5370 return -EFAULT; in clk_enable_write()
5376 return -EINVAL; in clk_enable_write()
5381 return -EINVAL; in clk_enable_write()
5407 seq_puts(s, "echo [clk_name] [parent_name] > /proc/clk/parent\n"); in clk_parent_show()
5426 return -EINVAL; in clk_parent_write()
5429 return -EFAULT; in clk_parent_write()
5435 return -EINVAL; in clk_parent_write()
5440 return -EINVAL; in clk_parent_write()
5445 return -EINVAL; in clk_parent_write()
5451 pr_err("set clk(%s)'s parent(%s) error\n", clk_name, p_name); in clk_parent_write()
5470 seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %5d %6d\n", in clk_proc_summary_show_one()
5472 30 - level * 3, c->name, in clk_proc_summary_show_one()
5473 c->enable_count, c->prepare_count, c->protect_count, in clk_proc_summary_show_one()
5490 hlist_for_each_entry(child, &c->children, child_node) in clk_proc_summary_show_subtree()
5505 …" clock count count count rate accuracy phase cycle\n"); in clk_proc_summary_show()
5506 …seq_puts(s, "-------------------------------------------------------------------------------------… in clk_proc_summary_show()
5524 proc_clk_root = proc_mkdir("clk", NULL); in clk_create_procfs()
5526 return -EINVAL; in clk_create_procfs()
5549 return -EINVAL; in clk_create_procfs()