13 #include <linux/module.h>
17 #include <linux/list.h>
18 #include <linux/slab.h>
30 #ifdef CONFIG_COMMON_CLK_DEBUG
33 static struct dentry *rootdir;
34 static struct dentry *orphandir;
35 static int inited = 0;
38 static int clk_debug_create_one(
struct clk *
clk,
struct dentry *pdentry)
43 if (!clk || !pdentry) {
65 (
u32 *)&clk->prepare_count);
70 (
u32 *)&clk->enable_count);
75 (
u32 *)&clk->notifier_count);
89 static int clk_debug_create_subtree(
struct clk *clk,
struct dentry *pdentry)
98 ret = clk_debug_create_one(clk, pdentry);
104 clk_debug_create_subtree(child, clk->
dentry);
122 static
int clk_debug_register(
struct clk *clk)
125 struct dentry *pdentry;
138 if (clk->flags & CLK_IS_ROOT)
144 pdentry = parent->dentry;
148 ret = clk_debug_create_subtree(clk, pdentry);
166 static int __init clk_debug_init(
void)
184 clk_debug_create_subtree(clk, rootdir);
187 clk_debug_create_subtree(clk, orphandir);
197 static inline int clk_debug_register(
struct clk *clk) {
return 0; }
201 static void clk_disable_unused_subtree(
struct clk *clk)
211 clk_disable_unused_subtree(child);
215 if (clk->enable_count)
218 if (clk->flags & CLK_IGNORE_UNUSED)
225 spin_unlock_irqrestore(&enable_lock, flags);
231 static
int clk_disable_unused(
void)
239 clk_disable_unused_subtree(clk);
242 clk_disable_unused_subtree(clk);
254 return !clk ?
NULL : clk->name;
259 return !clk ?
NULL : clk->hw;
264 return !clk ? -
EINVAL : clk->num_parents;
274 return !clk ? -
EINVAL : clk->enable_count;
279 return !clk ? -
EINVAL : clk->prepare_count;
293 if (clk->
flags & CLK_IS_ROOT)
319 if (!clk->
ops->is_enabled) {
320 ret = clk->enable_count ? 1 : 0;
324 ret = clk->
ops->is_enabled(clk->hw);
329 static struct clk *__clk_lookup_subtree(
const char *
name,
struct clk *clk)
339 ret = __clk_lookup_subtree(name, child);
349 struct clk *root_clk;
358 ret = __clk_lookup_subtree(name, root_clk);
365 ret = __clk_lookup_subtree(name, root_clk);
380 if (
WARN_ON(clk->prepare_count == 0))
383 if (--clk->prepare_count > 0)
386 WARN_ON(clk->enable_count > 0);
388 if (clk->
ops->unprepare)
389 clk->
ops->unprepare(clk->hw);
420 if (clk->prepare_count == 0) {
425 if (clk->
ops->prepare) {
426 ret = clk->
ops->prepare(clk->hw);
434 clk->prepare_count++;
471 if (
WARN_ON(clk->enable_count == 0))
474 if (--clk->enable_count > 0)
477 if (clk->
ops->disable)
478 clk->
ops->disable(clk->hw);
501 spin_unlock_irqrestore(&enable_lock, flags);
512 if (
WARN_ON(clk->prepare_count == 0))
515 if (clk->enable_count == 0) {
521 if (clk->
ops->enable) {
522 ret = clk->
ops->enable(clk->hw);
554 spin_unlock_irqrestore(&enable_lock, flags);
568 unsigned long parent_rate = 0;
573 if (!clk->
ops->round_rate) {
574 if (clk->
flags & CLK_SET_RATE_PARENT)
581 parent_rate = clk->
parent->rate;
583 return clk->
ops->round_rate(clk->hw, rate, &parent_rate);
621 static int __clk_notify(
struct clk *clk,
unsigned long msg,
622 unsigned long old_rate,
unsigned long new_rate)
624 struct clk_notifier *cn;
625 struct clk_notifier_data cnd;
626 int ret = NOTIFY_DONE;
629 cnd.old_rate = old_rate;
630 cnd.new_rate = new_rate;
633 if (cn->clk == clk) {
657 static void __clk_recalc_rates(
struct clk *clk,
unsigned long msg)
659 unsigned long old_rate;
660 unsigned long parent_rate = 0;
664 old_rate = clk->
rate;
667 parent_rate = clk->
parent->rate;
669 if (clk->
ops->recalc_rate)
670 clk->
rate = clk->
ops->recalc_rate(clk->hw, parent_rate);
672 clk->
rate = parent_rate;
678 if (clk->notifier_count && msg)
679 __clk_notify(clk, msg, old_rate, clk->
rate);
682 __clk_recalc_rates(child, msg);
699 if (clk && (clk->flags & CLK_GET_RATE_NOCACHE))
700 __clk_recalc_rates(clk, 0);
725 static int __clk_speculate_rates(
struct clk *clk,
unsigned long parent_rate)
729 unsigned long new_rate;
730 int ret = NOTIFY_DONE;
732 if (clk->
ops->recalc_rate)
733 new_rate = clk->
ops->recalc_rate(clk->hw, parent_rate);
735 new_rate = parent_rate;
738 if (clk->notifier_count)
739 ret = __clk_notify(clk, PRE_RATE_CHANGE, clk->
rate, new_rate);
741 if (ret == NOTIFY_BAD)
745 ret = __clk_speculate_rates(child, new_rate);
746 if (ret == NOTIFY_BAD)
754 static void clk_calc_subtree(
struct clk *clk,
unsigned long new_rate)
759 clk->new_rate = new_rate;
762 if (child->
ops->recalc_rate)
763 child->new_rate = child->
ops->recalc_rate(child->hw, new_rate);
765 child->new_rate = new_rate;
766 clk_calc_subtree(child, child->new_rate);
774 static struct clk *clk_calc_new_rates(
struct clk *clk,
unsigned long rate)
776 struct clk *
top = clk;
777 unsigned long best_parent_rate = 0;
778 unsigned long new_rate;
781 if (IS_ERR_OR_NULL(clk))
786 best_parent_rate = clk->
parent->rate;
789 if (!(clk->
flags & CLK_SET_RATE_PARENT)) {
790 if (!clk->
ops->round_rate) {
791 clk->new_rate = clk->
rate;
794 new_rate = clk->
ops->round_rate(clk->hw, rate, &best_parent_rate);
800 pr_debug(
"%s: %s has NULL parent\n", __func__, clk->
name);
804 if (!clk->
ops->round_rate) {
805 top = clk_calc_new_rates(clk->
parent, rate);
806 new_rate = clk->
parent->new_rate;
811 new_rate = clk->
ops->round_rate(clk->hw, rate, &best_parent_rate);
813 if (best_parent_rate != clk->
parent->rate) {
814 top = clk_calc_new_rates(clk->
parent, best_parent_rate);
820 clk_calc_subtree(clk, new_rate);
830 static struct clk *clk_propagate_rate_change(
struct clk *clk,
unsigned long event)
833 struct clk *child, *fail_clk =
NULL;
834 int ret = NOTIFY_DONE;
836 if (clk->
rate == clk->new_rate)
839 if (clk->notifier_count) {
840 ret = __clk_notify(clk, event, clk->
rate, clk->new_rate);
841 if (ret == NOTIFY_BAD)
846 clk = clk_propagate_rate_change(child, event);
858 static void clk_change_rate(
struct clk *clk)
861 unsigned long old_rate;
862 unsigned long best_parent_rate = 0;
865 old_rate = clk->
rate;
868 best_parent_rate = clk->
parent->rate;
870 if (clk->
ops->set_rate)
871 clk->
ops->set_rate(clk->hw, clk->new_rate, best_parent_rate);
873 if (clk->
ops->recalc_rate)
874 clk->
rate = clk->
ops->recalc_rate(clk->hw, best_parent_rate);
876 clk->
rate = best_parent_rate;
878 if (clk->notifier_count && old_rate != clk->
rate)
879 __clk_notify(clk, POST_RATE_CHANGE, old_rate, clk->
rate);
882 clk_change_rate(child);
908 struct clk *
top, *fail_clk;
915 if (rate == clk->rate)
918 if ((clk->flags & CLK_SET_RATE_GATE) && clk->prepare_count) {
924 top = clk_calc_new_rates(clk, rate);
931 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
933 pr_warn(
"%s: failed to set %s rate\n", __func__,
935 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
941 clk_change_rate(top);
980 static struct clk *__clk_init_parent(
struct clk *clk)
982 struct clk *ret =
NULL;
987 if (!clk->num_parents)
990 if (clk->num_parents == 1) {
991 if (IS_ERR_OR_NULL(clk->
parent))
997 if (!clk->
ops->get_parent) {
999 "%s: multi-parent clocks must implement .get_parent\n",
1010 index = clk->
ops->get_parent(clk->hw);
1014 kzalloc((
sizeof(
struct clk*) * clk->num_parents),
1019 else if (!clk->parents[index])
1020 ret = clk->parents[
index] =
1023 ret = clk->parents[
index];
1031 #ifdef CONFIG_COMMON_CLK_DEBUG
1033 struct dentry *new_parent_d;
1036 if (!clk || !new_parent)
1039 hlist_del(&clk->child_node);
1042 hlist_add_head(&clk->child_node, &new_parent->
children);
1044 hlist_add_head(&clk->child_node, &clk_orphan_list);
1046 #ifdef CONFIG_COMMON_CLK_DEBUG
1051 new_parent_d = new_parent->dentry;
1053 new_parent_d = orphandir;
1056 new_parent_d, clk->
name);
1060 pr_debug(
"%s: failed to rename debugfs entry for %s\n",
1061 __func__, clk->
name);
1065 clk->
parent = new_parent;
1067 __clk_recalc_rates(clk, POST_RATE_CHANGE);
1070 static int __clk_set_parent(
struct clk *clk,
struct clk *parent)
1072 struct clk *old_parent;
1073 unsigned long flags;
1077 old_parent = clk->
parent;
1080 clk->parents = kzalloc((
sizeof(
struct clk*) * clk->num_parents),
1088 for (i = 0; i < clk->num_parents; i++) {
1089 if (clk->parents && clk->parents[i] == parent)
1091 else if (!
strcmp(clk->parent_names[i], parent->
name)) {
1098 if (i == clk->num_parents) {
1099 pr_debug(
"%s: clock %s is not a possible parent of clock %s\n",
1100 __func__, parent->
name, clk->
name);
1105 if (clk->prepare_count)
1110 if (clk->enable_count)
1112 spin_unlock_irqrestore(&enable_lock, flags);
1115 ret = clk->
ops->set_parent(clk->hw, i);
1119 if (clk->enable_count)
1121 spin_unlock_irqrestore(&enable_lock, flags);
1123 if (clk->prepare_count)
1146 if (!clk || !clk->
ops)
1149 if (!clk->
ops->set_parent)
1155 if (clk->
parent == parent)
1159 if (clk->notifier_count)
1160 ret = __clk_speculate_rates(clk, parent->
rate);
1163 if (ret == NOTIFY_STOP)
1167 if ((clk->
flags & CLK_SET_PARENT_GATE) && clk->prepare_count)
1170 ret = __clk_set_parent(clk, parent);
1174 __clk_recalc_rates(clk, ABORT_RATE_CHANGE);
1209 pr_debug(
"%s: clk %s already initialized\n",
1210 __func__, clk->
name);
1216 if (clk->
ops->set_rate &&
1217 !(clk->
ops->round_rate && clk->
ops->recalc_rate)) {
1218 pr_warning(
"%s: %s must implement .round_rate & .recalc_rate\n",
1219 __func__, clk->
name);
1224 if (clk->
ops->set_parent && !clk->
ops->get_parent) {
1225 pr_warning(
"%s: %s must implement .get_parent & .set_parent\n",
1226 __func__, clk->
name);
1232 for (i = 0; i < clk->num_parents; i++)
1233 WARN(!clk->parent_names[i],
1234 "%s: invalid NULL in %s's .parent_names\n",
1235 __func__, clk->
name);
1247 if (clk->num_parents > 1 && !clk->parents) {
1248 clk->parents = kzalloc((
sizeof(
struct clk*) * clk->num_parents),
1257 for (i = 0; i < clk->num_parents; i++)
1262 clk->
parent = __clk_init_parent(clk);
1275 hlist_add_head(&clk->child_node,
1277 else if (clk->
flags & CLK_IS_ROOT)
1278 hlist_add_head(&clk->child_node, &clk_root_list);
1280 hlist_add_head(&clk->child_node, &clk_orphan_list);
1288 if (clk->
ops->recalc_rate)
1289 clk->
rate = clk->
ops->recalc_rate(clk->hw,
1301 for (i = 0; i < orphan->num_parents; i++)
1302 if (!
strcmp(clk->
name, orphan->parent_names[i])) {
1316 clk->
ops->init(clk->hw);
1318 clk_debug_register(clk);
1349 clk->
name = hw->init->name;
1350 clk->
ops = hw->init->ops;
1352 clk->
flags = hw->init->flags;
1353 clk->parent_names = hw->
init->parent_names;
1354 clk->num_parents = hw->
init->num_parents;
1358 return ERR_PTR(ret);
1382 pr_err(
"%s: could not allocate clk\n", __func__);
1389 pr_err(
"%s: could not allocate clk->name\n", __func__);
1393 clk->
ops = hw->init->ops;
1395 clk->
flags = hw->init->flags;
1396 clk->num_parents = hw->
init->num_parents;
1400 clk->parent_names = kzalloc((
sizeof(
char*) * clk->num_parents),
1403 if (!clk->parent_names) {
1404 pr_err(
"%s: could not allocate clk->parent_names\n", __func__);
1406 goto fail_parent_names;
1411 for (i = 0; i < clk->num_parents; i++) {
1412 clk->parent_names[
i] =
kstrdup(hw->init->parent_names[i],
1414 if (!clk->parent_names[i]) {
1415 pr_err(
"%s: could not copy parent_names\n", __func__);
1417 goto fail_parent_names_copy;
1425 fail_parent_names_copy:
1427 kfree(clk->parent_names[i]);
1428 kfree(clk->parent_names);
1434 return ERR_PTR(ret);
1481 struct clk_notifier *cn;
1495 if (cn->clk != clk) {
1496 cn = kzalloc(
sizeof(
struct clk_notifier),
GFP_KERNEL);
1503 list_add(&cn->node, &clk_notifier_list);
1508 clk->notifier_count++;
1530 struct clk_notifier *cn =
NULL;
1542 if (cn->clk == clk) {
1545 clk->notifier_count--;
1548 if (!cn->notifier_head.head) {
1572 struct of_clk_provider {
1592 struct clk_onecell_data *clk_data =
data;
1593 unsigned int idx = clkspec->
args[0];
1595 if (idx >= clk_data->clk_num) {
1596 pr_err(
"%s: invalid clock index %d\n", __func__, idx);
1600 return clk_data->clks[
idx];
1615 struct of_clk_provider *
cp;
1617 cp = kzalloc(
sizeof(
struct of_clk_provider),
GFP_KERNEL);
1621 cp->node = of_node_get(np);
1623 cp->get = clk_src_get;
1626 list_add(&cp->link, &of_clk_providers);
1640 struct of_clk_provider *
cp;
1644 if (cp->node == np) {
1646 of_node_put(cp->node);
1657 struct of_clk_provider *provider;
1658 struct clk *clk = ERR_PTR(-
ENOENT);
1663 if (provider->node == clkspec->
np)
1664 clk = provider->get(clkspec, provider->data);
1673 const char *of_clk_get_parent_name(
struct device_node *np,
int index)
1676 const char *clk_name;
1690 clk_name = clkspec.
np->name;
1692 of_node_put(clkspec.
np);
1708 for_each_matching_node(np, matches) {
1710 of_clk_init_cb_t clk_init_cb = match->
data;