提交 4dff95dc 编写于 作者: S Stephen Boyd

clk: Remove forward declared function prototypes

Move the code around so that we don't need to declare function
prototypes at the start of the file. Simplify
clk_core_is_prepared() and clk_core_is_enabled() too to make the
diff easier to read.
Signed-off-by: NStephen Boyd <sboyd@codeaurora.org>
上级 1f3e1983
...@@ -37,13 +37,6 @@ static HLIST_HEAD(clk_root_list); ...@@ -37,13 +37,6 @@ static HLIST_HEAD(clk_root_list);
static HLIST_HEAD(clk_orphan_list); static HLIST_HEAD(clk_orphan_list);
static LIST_HEAD(clk_notifier_list); static LIST_HEAD(clk_notifier_list);
static long clk_core_get_accuracy(struct clk_core *core);
static unsigned long clk_core_get_rate(struct clk_core *core);
static int clk_core_get_phase(struct clk_core *core);
static bool clk_core_is_prepared(struct clk_core *core);
static bool clk_core_is_enabled(struct clk_core *core);
static struct clk_core *clk_core_lookup(const char *name);
/*** private data structures ***/ /*** private data structures ***/
struct clk_core { struct clk_core {
...@@ -145,2093 +138,2075 @@ static void clk_enable_unlock(unsigned long flags) ...@@ -145,2093 +138,2075 @@ static void clk_enable_unlock(unsigned long flags)
spin_unlock_irqrestore(&enable_lock, flags); spin_unlock_irqrestore(&enable_lock, flags);
} }
/*** debugfs support ***/ static bool clk_core_is_prepared(struct clk_core *core)
{
#ifdef CONFIG_DEBUG_FS /*
#include <linux/debugfs.h> * .is_prepared is optional for clocks that can prepare
* fall back to software usage counter if it is missing
*/
if (!core->ops->is_prepared)
return core->prepare_count;
static struct dentry *rootdir; return core->ops->is_prepared(core->hw);
static int inited = 0; }
static DEFINE_MUTEX(clk_debug_lock);
static HLIST_HEAD(clk_debug_list);
static struct hlist_head *all_lists[] = { static bool clk_core_is_enabled(struct clk_core *core)
&clk_root_list, {
&clk_orphan_list, /*
NULL, * .is_enabled is only mandatory for clocks that gate
}; * fall back to software usage counter if .is_enabled is missing
*/
if (!core->ops->is_enabled)
return core->enable_count;
static struct hlist_head *orphan_list[] = { return core->ops->is_enabled(core->hw);
&clk_orphan_list, }
NULL,
};
static void clk_summary_show_one(struct seq_file *s, struct clk_core *c, /* caller must hold prepare_lock */
int level) static void clk_unprepare_unused_subtree(struct clk_core *core)
{ {
if (!c) struct clk_core *child;
lockdep_assert_held(&prepare_lock);
hlist_for_each_entry(child, &core->children, child_node)
clk_unprepare_unused_subtree(child);
if (core->prepare_count)
return; return;
seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n", if (core->flags & CLK_IGNORE_UNUSED)
level * 3 + 1, "", return;
30 - level * 3, c->name,
c->enable_count, c->prepare_count, clk_core_get_rate(c), if (clk_core_is_prepared(core)) {
clk_core_get_accuracy(c), clk_core_get_phase(c)); trace_clk_unprepare(core);
if (core->ops->unprepare_unused)
core->ops->unprepare_unused(core->hw);
else if (core->ops->unprepare)
core->ops->unprepare(core->hw);
trace_clk_unprepare_complete(core);
}
} }
static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c, /* caller must hold prepare_lock */
int level) static void clk_disable_unused_subtree(struct clk_core *core)
{ {
struct clk_core *child; struct clk_core *child;
unsigned long flags;
if (!c) lockdep_assert_held(&prepare_lock);
return;
clk_summary_show_one(s, c, level); hlist_for_each_entry(child, &core->children, child_node)
clk_disable_unused_subtree(child);
hlist_for_each_entry(child, &c->children, child_node) flags = clk_enable_lock();
clk_summary_show_subtree(s, child, level + 1);
if (core->enable_count)
goto unlock_out;
if (core->flags & CLK_IGNORE_UNUSED)
goto unlock_out;
/*
* some gate clocks have special needs during the disable-unused
* sequence. call .disable_unused if available, otherwise fall
* back to .disable
*/
if (clk_core_is_enabled(core)) {
trace_clk_disable(core);
if (core->ops->disable_unused)
core->ops->disable_unused(core->hw);
else if (core->ops->disable)
core->ops->disable(core->hw);
trace_clk_disable_complete(core);
}
unlock_out:
clk_enable_unlock(flags);
} }
static int clk_summary_show(struct seq_file *s, void *data) static bool clk_ignore_unused;
static int __init clk_ignore_unused_setup(char *__unused)
{ {
struct clk_core *c; clk_ignore_unused = true;
struct hlist_head **lists = (struct hlist_head **)s->private; return 1;
}
__setup("clk_ignore_unused", clk_ignore_unused_setup);
seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n"); static int clk_disable_unused(void)
seq_puts(s, "----------------------------------------------------------------------------------------\n"); {
struct clk_core *core;
if (clk_ignore_unused) {
pr_warn("clk: Not disabling unused clocks\n");
return 0;
}
clk_prepare_lock(); clk_prepare_lock();
for (; *lists; lists++) hlist_for_each_entry(core, &clk_root_list, child_node)
hlist_for_each_entry(c, *lists, child_node) clk_disable_unused_subtree(core);
clk_summary_show_subtree(s, c, 0);
hlist_for_each_entry(core, &clk_orphan_list, child_node)
clk_disable_unused_subtree(core);
hlist_for_each_entry(core, &clk_root_list, child_node)
clk_unprepare_unused_subtree(core);
hlist_for_each_entry(core, &clk_orphan_list, child_node)
clk_unprepare_unused_subtree(core);
clk_prepare_unlock(); clk_prepare_unlock();
return 0; return 0;
} }
late_initcall_sync(clk_disable_unused);
/*** helper functions ***/
static int clk_summary_open(struct inode *inode, struct file *file) const char *__clk_get_name(struct clk *clk)
{ {
return single_open(file, clk_summary_show, inode->i_private); return !clk ? NULL : clk->core->name;
} }
EXPORT_SYMBOL_GPL(__clk_get_name);
static const struct file_operations clk_summary_fops = { struct clk_hw *__clk_get_hw(struct clk *clk)
.open = clk_summary_open, {
.read = seq_read, return !clk ? NULL : clk->core->hw;
.llseek = seq_lseek, }
.release = single_release, EXPORT_SYMBOL_GPL(__clk_get_hw);
};
static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level) u8 __clk_get_num_parents(struct clk *clk)
{ {
if (!c) return !clk ? 0 : clk->core->num_parents;
return; }
EXPORT_SYMBOL_GPL(__clk_get_num_parents);
seq_printf(s, "\"%s\": { ", c->name); struct clk *__clk_get_parent(struct clk *clk)
seq_printf(s, "\"enable_count\": %d,", c->enable_count); {
seq_printf(s, "\"prepare_count\": %d,", c->prepare_count); if (!clk)
seq_printf(s, "\"rate\": %lu", clk_core_get_rate(c)); return NULL;
seq_printf(s, "\"accuracy\": %lu", clk_core_get_accuracy(c));
seq_printf(s, "\"phase\": %d", clk_core_get_phase(c)); /* TODO: Create a per-user clk and change callers to call clk_put */
return !clk->core->parent ? NULL : clk->core->parent->hw->clk;
} }
EXPORT_SYMBOL_GPL(__clk_get_parent);
static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level) static struct clk_core *__clk_lookup_subtree(const char *name,
struct clk_core *core)
{ {
struct clk_core *child; struct clk_core *child;
struct clk_core *ret;
if (!c) if (!strcmp(core->name, name))
return; return core;
clk_dump_one(s, c, level);
hlist_for_each_entry(child, &c->children, child_node) { hlist_for_each_entry(child, &core->children, child_node) {
seq_printf(s, ","); ret = __clk_lookup_subtree(name, child);
clk_dump_subtree(s, child, level + 1); if (ret)
return ret;
} }
seq_printf(s, "}"); return NULL;
} }
static int clk_dump(struct seq_file *s, void *data) static struct clk_core *clk_core_lookup(const char *name)
{ {
struct clk_core *c; struct clk_core *root_clk;
bool first_node = true; struct clk_core *ret;
struct hlist_head **lists = (struct hlist_head **)s->private;
seq_printf(s, "{");
clk_prepare_lock(); if (!name)
return NULL;
for (; *lists; lists++) { /* search the 'proper' clk tree first */
hlist_for_each_entry(c, *lists, child_node) { hlist_for_each_entry(root_clk, &clk_root_list, child_node) {
if (!first_node) ret = __clk_lookup_subtree(name, root_clk);
seq_puts(s, ","); if (ret)
first_node = false; return ret;
clk_dump_subtree(s, c, 0);
}
} }
clk_prepare_unlock(); /* if not found, then search the orphan tree */
hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) {
ret = __clk_lookup_subtree(name, root_clk);
if (ret)
return ret;
}
seq_printf(s, "}"); return NULL;
return 0;
} }
static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
static int clk_dump_open(struct inode *inode, struct file *file) u8 index)
{ {
return single_open(file, clk_dump, inode->i_private); if (!core || index >= core->num_parents)
return NULL;
else if (!core->parents)
return clk_core_lookup(core->parent_names[index]);
else if (!core->parents[index])
return core->parents[index] =
clk_core_lookup(core->parent_names[index]);
else
return core->parents[index];
} }
static const struct file_operations clk_dump_fops = { struct clk *clk_get_parent_by_index(struct clk *clk, u8 index)
.open = clk_dump_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
{ {
struct dentry *d; struct clk_core *parent;
int ret = -ENOMEM;
if (!core || !pdentry) { if (!clk)
ret = -EINVAL; return NULL;
goto out;
}
d = debugfs_create_dir(core->name, pdentry); parent = clk_core_get_parent_by_index(clk->core, index);
if (!d)
goto out;
core->dentry = d;
d = debugfs_create_u32("clk_rate", S_IRUGO, core->dentry,
(u32 *)&core->rate);
if (!d)
goto err_out;
d = debugfs_create_u32("clk_accuracy", S_IRUGO, core->dentry,
(u32 *)&core->accuracy);
if (!d)
goto err_out;
d = debugfs_create_u32("clk_phase", S_IRUGO, core->dentry, return !parent ? NULL : parent->hw->clk;
(u32 *)&core->phase); }
if (!d) EXPORT_SYMBOL_GPL(clk_get_parent_by_index);
goto err_out;
d = debugfs_create_x32("clk_flags", S_IRUGO, core->dentry, unsigned int __clk_get_enable_count(struct clk *clk)
(u32 *)&core->flags); {
if (!d) return !clk ? 0 : clk->core->enable_count;
goto err_out; }
d = debugfs_create_u32("clk_prepare_count", S_IRUGO, core->dentry, static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
(u32 *)&core->prepare_count); {
if (!d) unsigned long ret;
goto err_out;
d = debugfs_create_u32("clk_enable_count", S_IRUGO, core->dentry, if (!core) {
(u32 *)&core->enable_count); ret = 0;
if (!d) goto out;
goto err_out; }
d = debugfs_create_u32("clk_notifier_count", S_IRUGO, core->dentry, ret = core->rate;
(u32 *)&core->notifier_count);
if (!d)
goto err_out;
if (core->ops->debug_init) { if (core->flags & CLK_IS_ROOT)
ret = core->ops->debug_init(core->hw, core->dentry); goto out;
if (ret)
goto err_out;
}
ret = 0; if (!core->parent)
goto out; ret = 0;
err_out:
debugfs_remove_recursive(core->dentry);
core->dentry = NULL;
out: out:
return ret; return ret;
} }
/** unsigned long __clk_get_rate(struct clk *clk)
* clk_debug_register - add a clk node to the debugfs clk tree
* @core: the clk being added to the debugfs clk tree
*
* Dynamically adds a clk to the debugfs clk tree if debugfs has been
* initialized. Otherwise it bails out early since the debugfs clk tree
* will be created lazily by clk_debug_init as part of a late_initcall.
*/
static int clk_debug_register(struct clk_core *core)
{ {
int ret = 0; if (!clk)
return 0;
mutex_lock(&clk_debug_lock);
hlist_add_head(&core->debug_node, &clk_debug_list);
if (!inited) return clk_core_get_rate_nolock(clk->core);
goto unlock; }
EXPORT_SYMBOL_GPL(__clk_get_rate);
ret = clk_debug_create_one(core, rootdir); static unsigned long __clk_get_accuracy(struct clk_core *core)
unlock: {
mutex_unlock(&clk_debug_lock); if (!core)
return 0;
return ret; return core->accuracy;
} }
/** unsigned long __clk_get_flags(struct clk *clk)
* clk_debug_unregister - remove a clk node from the debugfs clk tree
* @core: the clk being removed from the debugfs clk tree
*
* Dynamically removes a clk and all it's children clk nodes from the
* debugfs clk tree if clk->dentry points to debugfs created by
* clk_debug_register in __clk_init.
*/
static void clk_debug_unregister(struct clk_core *core)
{ {
mutex_lock(&clk_debug_lock); return !clk ? 0 : clk->core->flags;
hlist_del_init(&core->debug_node);
debugfs_remove_recursive(core->dentry);
core->dentry = NULL;
mutex_unlock(&clk_debug_lock);
} }
EXPORT_SYMBOL_GPL(__clk_get_flags);
struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode, bool __clk_is_prepared(struct clk *clk)
void *data, const struct file_operations *fops)
{ {
struct dentry *d = NULL; if (!clk)
return false;
if (hw->core->dentry)
d = debugfs_create_file(name, mode, hw->core->dentry, data,
fops);
return d; return clk_core_is_prepared(clk->core);
} }
EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
/** bool __clk_is_enabled(struct clk *clk)
* clk_debug_init - lazily create the debugfs clk tree visualization
*
* clks are often initialized very early during boot before memory can
* be dynamically allocated and well before debugfs is setup.
* clk_debug_init walks the clk tree hierarchy while holding
* prepare_lock and creates the topology as part of a late_initcall,
* thus insuring that clks initialized very early will still be
* represented in the debugfs clk tree. This function should only be
* called once at boot-time, and all other clks added dynamically will
* be done so with clk_debug_register.
*/
static int __init clk_debug_init(void)
{ {
struct clk_core *core; if (!clk)
struct dentry *d; return false;
rootdir = debugfs_create_dir("clk", NULL);
if (!rootdir) return clk_core_is_enabled(clk->core);
return -ENOMEM; }
EXPORT_SYMBOL_GPL(__clk_is_enabled);
d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists, static bool mux_is_better_rate(unsigned long rate, unsigned long now,
&clk_summary_fops); unsigned long best, unsigned long flags)
if (!d) {
return -ENOMEM; if (flags & CLK_MUX_ROUND_CLOSEST)
return abs(now - rate) < abs(best - rate);
d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists, return now <= rate && now > best;
&clk_dump_fops); }
if (!d)
return -ENOMEM;
d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir, static long
&orphan_list, &clk_summary_fops); clk_mux_determine_rate_flags(struct clk_hw *hw, unsigned long rate,
if (!d) unsigned long min_rate,
return -ENOMEM; unsigned long max_rate,
unsigned long *best_parent_rate,
struct clk_hw **best_parent_p,
unsigned long flags)
{
struct clk_core *core = hw->core, *parent, *best_parent = NULL;
int i, num_parents;
unsigned long parent_rate, best = 0;
d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir, /* if NO_REPARENT flag set, pass through to current parent */
&orphan_list, &clk_dump_fops); if (core->flags & CLK_SET_RATE_NO_REPARENT) {
if (!d) parent = core->parent;
return -ENOMEM; if (core->flags & CLK_SET_RATE_PARENT)
best = __clk_determine_rate(parent ? parent->hw : NULL,
rate, min_rate, max_rate);
else if (parent)
best = clk_core_get_rate_nolock(parent);
else
best = clk_core_get_rate_nolock(core);
goto out;
}
mutex_lock(&clk_debug_lock); /* find the parent that can provide the fastest rate <= rate */
hlist_for_each_entry(core, &clk_debug_list, debug_node) num_parents = core->num_parents;
clk_debug_create_one(core, rootdir); for (i = 0; i < num_parents; i++) {
parent = clk_core_get_parent_by_index(core, i);
if (!parent)
continue;
if (core->flags & CLK_SET_RATE_PARENT)
parent_rate = __clk_determine_rate(parent->hw, rate,
min_rate,
max_rate);
else
parent_rate = clk_core_get_rate_nolock(parent);
if (mux_is_better_rate(rate, parent_rate, best, flags)) {
best_parent = parent;
best = parent_rate;
}
}
inited = 1; out:
mutex_unlock(&clk_debug_lock); if (best_parent)
*best_parent_p = best_parent->hw;
*best_parent_rate = best;
return 0; return best;
}
late_initcall(clk_debug_init);
#else
static inline int clk_debug_register(struct clk_core *core) { return 0; }
static inline void clk_debug_reparent(struct clk_core *core,
struct clk_core *new_parent)
{
} }
static inline void clk_debug_unregister(struct clk_core *core)
struct clk *__clk_lookup(const char *name)
{ {
struct clk_core *core = clk_core_lookup(name);
return !core ? NULL : core->hw->clk;
} }
#endif
/* caller must hold prepare_lock */ static void clk_core_get_boundaries(struct clk_core *core,
static void clk_unprepare_unused_subtree(struct clk_core *core) unsigned long *min_rate,
unsigned long *max_rate)
{ {
struct clk_core *child; struct clk *clk_user;
lockdep_assert_held(&prepare_lock); *min_rate = 0;
*max_rate = ULONG_MAX;
hlist_for_each_entry(child, &core->children, child_node) hlist_for_each_entry(clk_user, &core->clks, clks_node)
clk_unprepare_unused_subtree(child); *min_rate = max(*min_rate, clk_user->min_rate);
if (core->prepare_count) hlist_for_each_entry(clk_user, &core->clks, clks_node)
return; *max_rate = min(*max_rate, clk_user->max_rate);
}
if (core->flags & CLK_IGNORE_UNUSED) /*
return; * Helper for finding best parent to provide a given frequency. This can be used
* directly as a determine_rate callback (e.g. for a mux), or from a more
if (clk_core_is_prepared(core)) { * complex clock that may combine a mux with other operations.
trace_clk_unprepare(core); */
if (core->ops->unprepare_unused) long __clk_mux_determine_rate(struct clk_hw *hw, unsigned long rate,
core->ops->unprepare_unused(core->hw); unsigned long min_rate,
else if (core->ops->unprepare) unsigned long max_rate,
core->ops->unprepare(core->hw); unsigned long *best_parent_rate,
trace_clk_unprepare_complete(core); struct clk_hw **best_parent_p)
} {
return clk_mux_determine_rate_flags(hw, rate, min_rate, max_rate,
best_parent_rate,
best_parent_p, 0);
} }
EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
/* caller must hold prepare_lock */ long __clk_mux_determine_rate_closest(struct clk_hw *hw, unsigned long rate,
static void clk_disable_unused_subtree(struct clk_core *core) unsigned long min_rate,
unsigned long max_rate,
unsigned long *best_parent_rate,
struct clk_hw **best_parent_p)
{ {
struct clk_core *child; return clk_mux_determine_rate_flags(hw, rate, min_rate, max_rate,
unsigned long flags; best_parent_rate,
best_parent_p,
CLK_MUX_ROUND_CLOSEST);
}
EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
lockdep_assert_held(&prepare_lock); /*** clk api ***/
hlist_for_each_entry(child, &core->children, child_node) static void clk_core_unprepare(struct clk_core *core)
clk_disable_unused_subtree(child); {
if (!core)
return;
flags = clk_enable_lock(); if (WARN_ON(core->prepare_count == 0))
return;
if (core->enable_count) if (--core->prepare_count > 0)
goto unlock_out; return;
if (core->flags & CLK_IGNORE_UNUSED) WARN_ON(core->enable_count > 0);
goto unlock_out;
/* trace_clk_unprepare(core);
* some gate clocks have special needs during the disable-unused
* sequence. call .disable_unused if available, otherwise fall
* back to .disable
*/
if (clk_core_is_enabled(core)) {
trace_clk_disable(core);
if (core->ops->disable_unused)
core->ops->disable_unused(core->hw);
else if (core->ops->disable)
core->ops->disable(core->hw);
trace_clk_disable_complete(core);
}
unlock_out: if (core->ops->unprepare)
clk_enable_unlock(flags); core->ops->unprepare(core->hw);
trace_clk_unprepare_complete(core);
clk_core_unprepare(core->parent);
} }
static bool clk_ignore_unused; /**
static int __init clk_ignore_unused_setup(char *__unused) * clk_unprepare - undo preparation of a clock source
* @clk: the clk being unprepared
*
* clk_unprepare may sleep, which differentiates it from clk_disable. In a
* simple case, clk_unprepare can be used instead of clk_disable to gate a clk
* if the operation may sleep. One example is a clk which is accessed over
* I2c. In the complex case a clk gate operation may require a fast and a slow
* part. It is this reason that clk_unprepare and clk_disable are not mutually
* exclusive. In fact clk_disable must be called before clk_unprepare.
*/
void clk_unprepare(struct clk *clk)
{ {
clk_ignore_unused = true; if (IS_ERR_OR_NULL(clk))
return 1; return;
clk_prepare_lock();
clk_core_unprepare(clk->core);
clk_prepare_unlock();
} }
__setup("clk_ignore_unused", clk_ignore_unused_setup); EXPORT_SYMBOL_GPL(clk_unprepare);
static int clk_disable_unused(void) static int clk_core_prepare(struct clk_core *core)
{ {
struct clk_core *core; int ret = 0;
if (clk_ignore_unused) { if (!core)
pr_warn("clk: Not disabling unused clocks\n");
return 0; return 0;
}
clk_prepare_lock(); if (core->prepare_count == 0) {
ret = clk_core_prepare(core->parent);
if (ret)
return ret;
hlist_for_each_entry(core, &clk_root_list, child_node) trace_clk_prepare(core);
clk_disable_unused_subtree(core);
hlist_for_each_entry(core, &clk_orphan_list, child_node) if (core->ops->prepare)
clk_disable_unused_subtree(core); ret = core->ops->prepare(core->hw);
hlist_for_each_entry(core, &clk_root_list, child_node) trace_clk_prepare_complete(core);
clk_unprepare_unused_subtree(core);
hlist_for_each_entry(core, &clk_orphan_list, child_node) if (ret) {
clk_unprepare_unused_subtree(core); clk_core_unprepare(core->parent);
return ret;
}
}
clk_prepare_unlock(); core->prepare_count++;
return 0; return 0;
} }
late_initcall_sync(clk_disable_unused);
/*** helper functions ***/
const char *__clk_get_name(struct clk *clk) /**
* clk_prepare - prepare a clock source
* @clk: the clk being prepared
*
* clk_prepare may sleep, which differentiates it from clk_enable. In a simple
* case, clk_prepare can be used instead of clk_enable to ungate a clk if the
* operation may sleep. One example is a clk which is accessed over I2c. In
* the complex case a clk ungate operation may require a fast and a slow part.
* It is this reason that clk_prepare and clk_enable are not mutually
* exclusive. In fact clk_prepare must be called before clk_enable.
* Returns 0 on success, -EERROR otherwise.
*/
int clk_prepare(struct clk *clk)
{ {
return !clk ? NULL : clk->core->name; int ret;
}
EXPORT_SYMBOL_GPL(__clk_get_name);
struct clk_hw *__clk_get_hw(struct clk *clk) if (!clk)
{ return 0;
return !clk ? NULL : clk->core->hw;
}
EXPORT_SYMBOL_GPL(__clk_get_hw);
u8 __clk_get_num_parents(struct clk *clk) clk_prepare_lock();
{ ret = clk_core_prepare(clk->core);
return !clk ? 0 : clk->core->num_parents; clk_prepare_unlock();
return ret;
} }
EXPORT_SYMBOL_GPL(__clk_get_num_parents); EXPORT_SYMBOL_GPL(clk_prepare);
struct clk *__clk_get_parent(struct clk *clk) static void clk_core_disable(struct clk_core *core)
{ {
if (!clk) if (!core)
return NULL; return;
/* TODO: Create a per-user clk and change callers to call clk_put */ if (WARN_ON(core->enable_count == 0))
return !clk->core->parent ? NULL : clk->core->parent->hw->clk; return;
}
EXPORT_SYMBOL_GPL(__clk_get_parent);
static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core, if (--core->enable_count > 0)
u8 index) return;
{
if (!core || index >= core->num_parents)
return NULL;
else if (!core->parents)
return clk_core_lookup(core->parent_names[index]);
else if (!core->parents[index])
return core->parents[index] =
clk_core_lookup(core->parent_names[index]);
else
return core->parents[index];
}
struct clk *clk_get_parent_by_index(struct clk *clk, u8 index) trace_clk_disable(core);
{
struct clk_core *parent;
if (!clk) if (core->ops->disable)
return NULL; core->ops->disable(core->hw);
parent = clk_core_get_parent_by_index(clk->core, index); trace_clk_disable_complete(core);
return !parent ? NULL : parent->hw->clk; clk_core_disable(core->parent);
} }
EXPORT_SYMBOL_GPL(clk_get_parent_by_index);
unsigned int __clk_get_enable_count(struct clk *clk) /**
* clk_disable - gate a clock
* @clk: the clk being gated
*
* clk_disable must not sleep, which differentiates it from clk_unprepare. In
* a simple case, clk_disable can be used instead of clk_unprepare to gate a
* clk if the operation is fast and will never sleep. One example is a
* SoC-internal clk which is controlled via simple register writes. In the
* complex case a clk gate operation may require a fast and a slow part. It is
* this reason that clk_unprepare and clk_disable are not mutually exclusive.
* In fact clk_disable must be called before clk_unprepare.
*/
void clk_disable(struct clk *clk)
{ {
return !clk ? 0 : clk->core->enable_count; unsigned long flags;
if (IS_ERR_OR_NULL(clk))
return;
flags = clk_enable_lock();
clk_core_disable(clk->core);
clk_enable_unlock(flags);
} }
EXPORT_SYMBOL_GPL(clk_disable);
static unsigned long clk_core_get_rate_nolock(struct clk_core *core) static int clk_core_enable(struct clk_core *core)
{ {
unsigned long ret; int ret = 0;
if (!core) { if (!core)
ret = 0; return 0;
goto out;
}
ret = core->rate; if (WARN_ON(core->prepare_count == 0))
return -ESHUTDOWN;
if (core->flags & CLK_IS_ROOT) if (core->enable_count == 0) {
goto out; ret = clk_core_enable(core->parent);
if (!core->parent) if (ret)
ret = 0; return ret;
out: trace_clk_enable(core);
return ret;
}
unsigned long __clk_get_rate(struct clk *clk) if (core->ops->enable)
{ ret = core->ops->enable(core->hw);
if (!clk)
return 0;
return clk_core_get_rate_nolock(clk->core); trace_clk_enable_complete(core);
if (ret) {
clk_core_disable(core->parent);
return ret;
}
}
core->enable_count++;
return 0;
} }
EXPORT_SYMBOL_GPL(__clk_get_rate);
static unsigned long __clk_get_accuracy(struct clk_core *core) /**
* clk_enable - ungate a clock
* @clk: the clk being ungated
*
* clk_enable must not sleep, which differentiates it from clk_prepare. In a
* simple case, clk_enable can be used instead of clk_prepare to ungate a clk
* if the operation will never sleep. One example is a SoC-internal clk which
* is controlled via simple register writes. In the complex case a clk ungate
* operation may require a fast and a slow part. It is this reason that
* clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare
* must be called before clk_enable. Returns 0 on success, -EERROR
* otherwise.
*/
int clk_enable(struct clk *clk)
{ {
if (!core) unsigned long flags;
int ret;
if (!clk)
return 0; return 0;
return core->accuracy; flags = clk_enable_lock();
} ret = clk_core_enable(clk->core);
clk_enable_unlock(flags);
unsigned long __clk_get_flags(struct clk *clk) return ret;
{
return !clk ? 0 : clk->core->flags;
} }
EXPORT_SYMBOL_GPL(__clk_get_flags); EXPORT_SYMBOL_GPL(clk_enable);
static bool clk_core_is_prepared(struct clk_core *core) static unsigned long clk_core_round_rate_nolock(struct clk_core *core,
unsigned long rate,
unsigned long min_rate,
unsigned long max_rate)
{ {
int ret; unsigned long parent_rate = 0;
struct clk_core *parent;
struct clk_hw *parent_hw;
lockdep_assert_held(&prepare_lock);
if (!core) if (!core)
return false; return 0;
/* parent = core->parent;
* .is_prepared is optional for clocks that can prepare if (parent)
* fall back to software usage counter if it is missing parent_rate = parent->rate;
*/
if (!core->ops->is_prepared) {
ret = core->prepare_count ? 1 : 0;
goto out;
}
ret = core->ops->is_prepared(core->hw); if (core->ops->determine_rate) {
out: parent_hw = parent ? parent->hw : NULL;
return !!ret; return core->ops->determine_rate(core->hw, rate,
min_rate, max_rate,
&parent_rate, &parent_hw);
} else if (core->ops->round_rate)
return core->ops->round_rate(core->hw, rate, &parent_rate);
else if (core->flags & CLK_SET_RATE_PARENT)
return clk_core_round_rate_nolock(core->parent, rate, min_rate,
max_rate);
else
return core->rate;
} }
bool __clk_is_prepared(struct clk *clk) /**
* __clk_determine_rate - get the closest rate actually supported by a clock
* @hw: determine the rate of this clock
* @rate: target rate
* @min_rate: returned rate must be greater than this rate
* @max_rate: returned rate must be less than this rate
*
* Caller must hold prepare_lock. Useful for clk_ops such as .set_rate and
* .determine_rate.
*/
unsigned long __clk_determine_rate(struct clk_hw *hw,
unsigned long rate,
unsigned long min_rate,
unsigned long max_rate)
{ {
if (!clk) if (!hw)
return false; return 0;
return clk_core_is_prepared(clk->core); return clk_core_round_rate_nolock(hw->core, rate, min_rate, max_rate);
} }
EXPORT_SYMBOL_GPL(__clk_determine_rate);
static bool clk_core_is_enabled(struct clk_core *core) /**
* __clk_round_rate - round the given rate for a clk
* @clk: round the rate of this clock
* @rate: the rate which is to be rounded
*
* Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
*/
unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
{ {
int ret; unsigned long min_rate;
unsigned long max_rate;
if (!core) if (!clk)
return false; return 0;
/* clk_core_get_boundaries(clk->core, &min_rate, &max_rate);
* .is_enabled is only mandatory for clocks that gate
* fall back to software usage counter if .is_enabled is missing
*/
if (!core->ops->is_enabled) {
ret = core->enable_count ? 1 : 0;
goto out;
}
ret = core->ops->is_enabled(core->hw); return clk_core_round_rate_nolock(clk->core, rate, min_rate, max_rate);
out:
return !!ret;
} }
EXPORT_SYMBOL_GPL(__clk_round_rate);
bool __clk_is_enabled(struct clk *clk) /**
* clk_round_rate - round the given rate for a clk
* @clk: the clk for which we are rounding a rate
* @rate: the rate which is to be rounded
*
* Takes in a rate as input and rounds it to a rate that the clk can actually
* use which is then returned. If clk doesn't support round_rate operation
* then the parent rate is returned.
*/
long clk_round_rate(struct clk *clk, unsigned long rate)
{ {
unsigned long ret;
if (!clk) if (!clk)
return false; return 0;
return clk_core_is_enabled(clk->core); clk_prepare_lock();
ret = __clk_round_rate(clk, rate);
clk_prepare_unlock();
return ret;
} }
EXPORT_SYMBOL_GPL(__clk_is_enabled); EXPORT_SYMBOL_GPL(clk_round_rate);
static struct clk_core *__clk_lookup_subtree(const char *name, /**
struct clk_core *core) * __clk_notify - call clk notifier chain
* @core: clk that is changing rate
* @msg: clk notifier type (see include/linux/clk.h)
* @old_rate: old clk rate
* @new_rate: new clk rate
*
* Triggers a notifier call chain on the clk rate-change notification
* for 'clk'. Passes a pointer to the struct clk and the previous
* and current rates to the notifier callback. Intended to be called by
* internal clock code only. Returns NOTIFY_DONE from the last driver
* called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
* a driver returns that.
*/
static int __clk_notify(struct clk_core *core, unsigned long msg,
unsigned long old_rate, unsigned long new_rate)
{ {
struct clk_core *child; struct clk_notifier *cn;
struct clk_core *ret; struct clk_notifier_data cnd;
int ret = NOTIFY_DONE;
if (!strcmp(core->name, name)) cnd.old_rate = old_rate;
return core; cnd.new_rate = new_rate;
hlist_for_each_entry(child, &core->children, child_node) { list_for_each_entry(cn, &clk_notifier_list, node) {
ret = __clk_lookup_subtree(name, child); if (cn->clk->core == core) {
if (ret) cnd.clk = cn->clk;
return ret; ret = srcu_notifier_call_chain(&cn->notifier_head, msg,
&cnd);
}
} }
return NULL; return ret;
} }
static struct clk_core *clk_core_lookup(const char *name) /**
* __clk_recalc_accuracies
* @core: first clk in the subtree
*
* Walks the subtree of clks starting with clk and recalculates accuracies as
* it goes. Note that if a clk does not implement the .recalc_accuracy
* callback then it is assumed that the clock will take on the accuracy of it's
* parent.
*
* Caller must hold prepare_lock.
*/
static void __clk_recalc_accuracies(struct clk_core *core)
{ {
struct clk_core *root_clk; unsigned long parent_accuracy = 0;
struct clk_core *ret; struct clk_core *child;
if (!name) lockdep_assert_held(&prepare_lock);
return NULL;
/* search the 'proper' clk tree first */ if (core->parent)
hlist_for_each_entry(root_clk, &clk_root_list, child_node) { parent_accuracy = core->parent->accuracy;
ret = __clk_lookup_subtree(name, root_clk);
if (ret)
return ret;
}
/* if not found, then search the orphan tree */ if (core->ops->recalc_accuracy)
hlist_for_each_entry(root_clk, &clk_orphan_list, child_node) { core->accuracy = core->ops->recalc_accuracy(core->hw,
ret = __clk_lookup_subtree(name, root_clk); parent_accuracy);
if (ret) else
return ret; core->accuracy = parent_accuracy;
}
return NULL; hlist_for_each_entry(child, &core->children, child_node)
__clk_recalc_accuracies(child);
} }
static bool mux_is_better_rate(unsigned long rate, unsigned long now, static long clk_core_get_accuracy(struct clk_core *core)
unsigned long best, unsigned long flags)
{ {
if (flags & CLK_MUX_ROUND_CLOSEST) unsigned long accuracy;
return abs(now - rate) < abs(best - rate);
return now <= rate && now > best; clk_prepare_lock();
} if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
__clk_recalc_accuracies(core);
static long accuracy = __clk_get_accuracy(core);
clk_mux_determine_rate_flags(struct clk_hw *hw, unsigned long rate, clk_prepare_unlock();
unsigned long min_rate,
unsigned long max_rate,
unsigned long *best_parent_rate,
struct clk_hw **best_parent_p,
unsigned long flags)
{
struct clk_core *core = hw->core, *parent, *best_parent = NULL;
int i, num_parents;
unsigned long parent_rate, best = 0;
/* if NO_REPARENT flag set, pass through to current parent */
if (core->flags & CLK_SET_RATE_NO_REPARENT) {
parent = core->parent;
if (core->flags & CLK_SET_RATE_PARENT)
best = __clk_determine_rate(parent ? parent->hw : NULL,
rate, min_rate, max_rate);
else if (parent)
best = clk_core_get_rate_nolock(parent);
else
best = clk_core_get_rate_nolock(core);
goto out;
}
/* find the parent that can provide the fastest rate <= rate */
num_parents = core->num_parents;
for (i = 0; i < num_parents; i++) {
parent = clk_core_get_parent_by_index(core, i);
if (!parent)
continue;
if (core->flags & CLK_SET_RATE_PARENT)
parent_rate = __clk_determine_rate(parent->hw, rate,
min_rate,
max_rate);
else
parent_rate = clk_core_get_rate_nolock(parent);
if (mux_is_better_rate(rate, parent_rate, best, flags)) {
best_parent = parent;
best = parent_rate;
}
}
out:
if (best_parent)
*best_parent_p = best_parent->hw;
*best_parent_rate = best;
return best; return accuracy;
} }
struct clk *__clk_lookup(const char *name) /**
* clk_get_accuracy - return the accuracy of clk
* @clk: the clk whose accuracy is being returned
*
* Simply returns the cached accuracy of the clk, unless
* CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
* issued.
* If clk is NULL then returns 0.
*/
long clk_get_accuracy(struct clk *clk)
{ {
struct clk_core *core = clk_core_lookup(name); if (!clk)
return 0;
return !core ? NULL : core->hw->clk; return clk_core_get_accuracy(clk->core);
} }
EXPORT_SYMBOL_GPL(clk_get_accuracy);
static void clk_core_get_boundaries(struct clk_core *core, static unsigned long clk_recalc(struct clk_core *core,
unsigned long *min_rate, unsigned long parent_rate)
unsigned long *max_rate)
{ {
struct clk *clk_user; if (core->ops->recalc_rate)
return core->ops->recalc_rate(core->hw, parent_rate);
*min_rate = 0; return parent_rate;
*max_rate = ULONG_MAX;
hlist_for_each_entry(clk_user, &core->clks, clks_node)
*min_rate = max(*min_rate, clk_user->min_rate);
hlist_for_each_entry(clk_user, &core->clks, clks_node)
*max_rate = min(*max_rate, clk_user->max_rate);
} }
/* /**
* Helper for finding best parent to provide a given frequency. This can be used * __clk_recalc_rates
* directly as a determine_rate callback (e.g. for a mux), or from a more * @core: first clk in the subtree
* complex clock that may combine a mux with other operations. * @msg: notification type (see include/linux/clk.h)
*
* Walks the subtree of clks starting with clk and recalculates rates as it
* goes. Note that if a clk does not implement the .recalc_rate callback then
* it is assumed that the clock will take on the rate of its parent.
*
* clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
* if necessary.
*
* Caller must hold prepare_lock.
*/ */
long __clk_mux_determine_rate(struct clk_hw *hw, unsigned long rate, static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
unsigned long min_rate,
unsigned long max_rate,
unsigned long *best_parent_rate,
struct clk_hw **best_parent_p)
{ {
return clk_mux_determine_rate_flags(hw, rate, min_rate, max_rate, unsigned long old_rate;
best_parent_rate, unsigned long parent_rate = 0;
best_parent_p, 0); struct clk_core *child;
}
EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
long __clk_mux_determine_rate_closest(struct clk_hw *hw, unsigned long rate, lockdep_assert_held(&prepare_lock);
unsigned long min_rate,
unsigned long max_rate,
unsigned long *best_parent_rate,
struct clk_hw **best_parent_p)
{
return clk_mux_determine_rate_flags(hw, rate, min_rate, max_rate,
best_parent_rate,
best_parent_p,
CLK_MUX_ROUND_CLOSEST);
}
EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
/*** clk api ***/ old_rate = core->rate;
static void clk_core_unprepare(struct clk_core *core) if (core->parent)
{ parent_rate = core->parent->rate;
if (!core)
return;
if (WARN_ON(core->prepare_count == 0)) core->rate = clk_recalc(core, parent_rate);
return;
if (--core->prepare_count > 0) /*
return; * ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE
* & ABORT_RATE_CHANGE notifiers
*/
if (core->notifier_count && msg)
__clk_notify(core, msg, old_rate, core->rate);
WARN_ON(core->enable_count > 0); hlist_for_each_entry(child, &core->children, child_node)
__clk_recalc_rates(child, msg);
}
trace_clk_unprepare(core); static unsigned long clk_core_get_rate(struct clk_core *core)
{
unsigned long rate;
if (core->ops->unprepare) clk_prepare_lock();
core->ops->unprepare(core->hw);
trace_clk_unprepare_complete(core); if (core && (core->flags & CLK_GET_RATE_NOCACHE))
clk_core_unprepare(core->parent); __clk_recalc_rates(core, 0);
rate = clk_core_get_rate_nolock(core);
clk_prepare_unlock();
return rate;
} }
/** /**
* clk_unprepare - undo preparation of a clock source * clk_get_rate - return the rate of clk
* @clk: the clk being unprepared * @clk: the clk whose rate is being returned
* *
* clk_unprepare may sleep, which differentiates it from clk_disable. In a * Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag
* simple case, clk_unprepare can be used instead of clk_disable to gate a clk * is set, which means a recalc_rate will be issued.
* if the operation may sleep. One example is a clk which is accessed over * If clk is NULL then returns 0.
* I2c. In the complex case a clk gate operation may require a fast and a slow
* part. It is this reason that clk_unprepare and clk_disable are not mutually
* exclusive. In fact clk_disable must be called before clk_unprepare.
*/ */
void clk_unprepare(struct clk *clk) unsigned long clk_get_rate(struct clk *clk)
{ {
if (IS_ERR_OR_NULL(clk)) if (!clk)
return; return 0;
clk_prepare_lock(); return clk_core_get_rate(clk->core);
clk_core_unprepare(clk->core);
clk_prepare_unlock();
} }
EXPORT_SYMBOL_GPL(clk_unprepare); EXPORT_SYMBOL_GPL(clk_get_rate);
static int clk_core_prepare(struct clk_core *core) static int clk_fetch_parent_index(struct clk_core *core,
struct clk_core *parent)
{ {
int ret = 0; int i;
if (!core)
return 0;
if (core->prepare_count == 0) {
ret = clk_core_prepare(core->parent);
if (ret)
return ret;
trace_clk_prepare(core); if (!core->parents) {
core->parents = kcalloc(core->num_parents,
sizeof(struct clk *), GFP_KERNEL);
if (!core->parents)
return -ENOMEM;
}
if (core->ops->prepare) /*
ret = core->ops->prepare(core->hw); * find index of new parent clock using cached parent ptrs,
* or if not yet cached, use string name comparison and cache
* them now to avoid future calls to clk_core_lookup.
*/
for (i = 0; i < core->num_parents; i++) {
if (core->parents[i] == parent)
return i;
trace_clk_prepare_complete(core); if (core->parents[i])
continue;
if (ret) { if (!strcmp(core->parent_names[i], parent->name)) {
clk_core_unprepare(core->parent); core->parents[i] = clk_core_lookup(parent->name);
return ret; return i;
} }
} }
core->prepare_count++; return -EINVAL;
return 0;
} }
/** static void clk_reparent(struct clk_core *core, struct clk_core *new_parent)
* clk_prepare - prepare a clock source
* @clk: the clk being prepared
*
* clk_prepare may sleep, which differentiates it from clk_enable. In a simple
* case, clk_prepare can be used instead of clk_enable to ungate a clk if the
* operation may sleep. One example is a clk which is accessed over I2c. In
* the complex case a clk ungate operation may require a fast and a slow part.
* It is this reason that clk_prepare and clk_enable are not mutually
* exclusive. In fact clk_prepare must be called before clk_enable.
* Returns 0 on success, -EERROR otherwise.
*/
int clk_prepare(struct clk *clk)
{ {
int ret; hlist_del(&core->child_node);
if (!clk)
return 0;
clk_prepare_lock(); if (new_parent) {
ret = clk_core_prepare(clk->core); /* avoid duplicate POST_RATE_CHANGE notifications */
clk_prepare_unlock(); if (new_parent->new_child == core)
new_parent->new_child = NULL;
return ret;
}
EXPORT_SYMBOL_GPL(clk_prepare);
static void clk_core_disable(struct clk_core *core)
{
if (!core)
return;
if (WARN_ON(core->enable_count == 0))
return;
if (--core->enable_count > 0)
return;
trace_clk_disable(core);
if (core->ops->disable)
core->ops->disable(core->hw);
trace_clk_disable_complete(core); hlist_add_head(&core->child_node, &new_parent->children);
} else {
hlist_add_head(&core->child_node, &clk_orphan_list);
}
clk_core_disable(core->parent); core->parent = new_parent;
} }
/** static struct clk_core *__clk_set_parent_before(struct clk_core *core,
* clk_disable - gate a clock struct clk_core *parent)
* @clk: the clk being gated
*
* clk_disable must not sleep, which differentiates it from clk_unprepare. In
* a simple case, clk_disable can be used instead of clk_unprepare to gate a
* clk if the operation is fast and will never sleep. One example is a
* SoC-internal clk which is controlled via simple register writes. In the
* complex case a clk gate operation may require a fast and a slow part. It is
* this reason that clk_unprepare and clk_disable are not mutually exclusive.
* In fact clk_disable must be called before clk_unprepare.
*/
void clk_disable(struct clk *clk)
{ {
unsigned long flags; unsigned long flags;
struct clk_core *old_parent = core->parent;
if (IS_ERR_OR_NULL(clk)) /*
return; * Migrate prepare state between parents and prevent race with
* clk_enable().
*
* If the clock is not prepared, then a race with
* clk_enable/disable() is impossible since we already have the
* prepare lock (future calls to clk_enable() need to be preceded by
* a clk_prepare()).
*
* If the clock is prepared, migrate the prepared state to the new
* parent and also protect against a race with clk_enable() by
* forcing the clock and the new parent on. This ensures that all
* future calls to clk_enable() are practically NOPs with respect to
* hardware and software states.
*
* See also: Comment for clk_set_parent() below.
*/
if (core->prepare_count) {
clk_core_prepare(parent);
clk_core_enable(parent);
clk_core_enable(core);
}
/* update the clk tree topology */
flags = clk_enable_lock(); flags = clk_enable_lock();
clk_core_disable(clk->core); clk_reparent(core, parent);
clk_enable_unlock(flags); clk_enable_unlock(flags);
return old_parent;
} }
EXPORT_SYMBOL_GPL(clk_disable);
static int clk_core_enable(struct clk_core *core) static void __clk_set_parent_after(struct clk_core *core,
struct clk_core *parent,
struct clk_core *old_parent)
{ {
int ret = 0; /*
* Finish the migration of prepare state and undo the changes done
if (!core) * for preventing a race with clk_enable().
return 0; */
if (core->prepare_count) {
clk_core_disable(core);
clk_core_disable(old_parent);
clk_core_unprepare(old_parent);
}
}
if (WARN_ON(core->prepare_count == 0)) static int __clk_set_parent(struct clk_core *core, struct clk_core *parent,
return -ESHUTDOWN; u8 p_index)
{
unsigned long flags;
int ret = 0;
struct clk_core *old_parent;
if (core->enable_count == 0) { old_parent = __clk_set_parent_before(core, parent);
ret = clk_core_enable(core->parent);
if (ret) trace_clk_set_parent(core, parent);
return ret;
trace_clk_enable(core); /* change clock input source */
if (parent && core->ops->set_parent)
ret = core->ops->set_parent(core->hw, p_index);
if (core->ops->enable) trace_clk_set_parent_complete(core, parent);
ret = core->ops->enable(core->hw);
trace_clk_enable_complete(core); if (ret) {
flags = clk_enable_lock();
clk_reparent(core, old_parent);
clk_enable_unlock(flags);
if (ret) { if (core->prepare_count) {
clk_core_disable(core->parent); clk_core_disable(core);
return ret; clk_core_disable(parent);
clk_core_unprepare(parent);
} }
return ret;
} }
core->enable_count++; __clk_set_parent_after(core, parent, old_parent);
return 0; return 0;
} }
/** /**
* clk_enable - ungate a clock * __clk_speculate_rates
* @clk: the clk being ungated * @core: first clk in the subtree
* @parent_rate: the "future" rate of clk's parent
* *
* clk_enable must not sleep, which differentiates it from clk_prepare. In a * Walks the subtree of clks starting with clk, speculating rates as it
* simple case, clk_enable can be used instead of clk_prepare to ungate a clk * goes and firing off PRE_RATE_CHANGE notifications as necessary.
* if the operation will never sleep. One example is a SoC-internal clk which *
* is controlled via simple register writes. In the complex case a clk ungate * Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
* operation may require a fast and a slow part. It is this reason that * pre-rate change notifications and returns early if no clks in the
* clk_enable and clk_prepare are not mutually exclusive. In fact clk_prepare * subtree have subscribed to the notifications. Note that if a clk does not
* must be called before clk_enable. Returns 0 on success, -EERROR * implement the .recalc_rate callback then it is assumed that the clock will
* otherwise. * take on the rate of its parent.
*
* Caller must hold prepare_lock.
*/ */
int clk_enable(struct clk *clk) static int __clk_speculate_rates(struct clk_core *core,
unsigned long parent_rate)
{ {
unsigned long flags; struct clk_core *child;
int ret; unsigned long new_rate;
int ret = NOTIFY_DONE;
if (!clk) lockdep_assert_held(&prepare_lock);
return 0;
flags = clk_enable_lock(); new_rate = clk_recalc(core, parent_rate);
ret = clk_core_enable(clk->core);
clk_enable_unlock(flags); /* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */
if (core->notifier_count)
ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate);
if (ret & NOTIFY_STOP_MASK) {
pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
__func__, core->name, ret);
goto out;
}
hlist_for_each_entry(child, &core->children, child_node) {
ret = __clk_speculate_rates(child, new_rate);
if (ret & NOTIFY_STOP_MASK)
break;
}
out:
return ret; return ret;
} }
EXPORT_SYMBOL_GPL(clk_enable);
static unsigned long clk_core_round_rate_nolock(struct clk_core *core, static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
unsigned long rate, struct clk_core *new_parent, u8 p_index)
unsigned long min_rate,
unsigned long max_rate)
{ {
unsigned long parent_rate = 0; struct clk_core *child;
struct clk_core *parent;
struct clk_hw *parent_hw;
lockdep_assert_held(&prepare_lock); core->new_rate = new_rate;
core->new_parent = new_parent;
core->new_parent_index = p_index;
/* include clk in new parent's PRE_RATE_CHANGE notifications */
core->new_child = NULL;
if (new_parent && new_parent != core->parent)
new_parent->new_child = core;
if (!core) hlist_for_each_entry(child, &core->children, child_node) {
return 0; child->new_rate = clk_recalc(child, new_rate);
clk_calc_subtree(child, child->new_rate, NULL, 0);
}
}
parent = core->parent; /*
* calculate the new rates returning the topmost clock that has to be
* changed.
*/
static struct clk_core *clk_calc_new_rates(struct clk_core *core,
unsigned long rate)
{
struct clk_core *top = core;
struct clk_core *old_parent, *parent;
struct clk_hw *parent_hw;
unsigned long best_parent_rate = 0;
unsigned long new_rate;
unsigned long min_rate;
unsigned long max_rate;
int p_index = 0;
long ret;
/* sanity */
if (IS_ERR_OR_NULL(core))
return NULL;
/* save parent rate, if it exists */
parent = old_parent = core->parent;
if (parent) if (parent)
parent_rate = parent->rate; best_parent_rate = parent->rate;
clk_core_get_boundaries(core, &min_rate, &max_rate);
/* find the closest rate and parent clk/rate */
if (core->ops->determine_rate) { if (core->ops->determine_rate) {
parent_hw = parent ? parent->hw : NULL; parent_hw = parent ? parent->hw : NULL;
return core->ops->determine_rate(core->hw, rate, ret = core->ops->determine_rate(core->hw, rate,
min_rate, max_rate, min_rate,
&parent_rate, &parent_hw); max_rate,
} else if (core->ops->round_rate) &best_parent_rate,
return core->ops->round_rate(core->hw, rate, &parent_rate); &parent_hw);
else if (core->flags & CLK_SET_RATE_PARENT) if (ret < 0)
return clk_core_round_rate_nolock(core->parent, rate, min_rate, return NULL;
max_rate);
else
return core->rate;
}
/**
* __clk_determine_rate - get the closest rate actually supported by a clock
* @hw: determine the rate of this clock
* @rate: target rate
* @min_rate: returned rate must be greater than this rate
* @max_rate: returned rate must be less than this rate
*
* Caller must hold prepare_lock. Useful for clk_ops such as .set_rate and
* .determine_rate.
*/
unsigned long __clk_determine_rate(struct clk_hw *hw,
unsigned long rate,
unsigned long min_rate,
unsigned long max_rate)
{
if (!hw)
return 0;
return clk_core_round_rate_nolock(hw->core, rate, min_rate, max_rate);
}
EXPORT_SYMBOL_GPL(__clk_determine_rate);
/**
* __clk_round_rate - round the given rate for a clk
* @clk: round the rate of this clock
* @rate: the rate which is to be rounded
*
* Caller must hold prepare_lock. Useful for clk_ops such as .set_rate
*/
unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
{
unsigned long min_rate;
unsigned long max_rate;
if (!clk) new_rate = ret;
return 0; parent = parent_hw ? parent_hw->core : NULL;
} else if (core->ops->round_rate) {
ret = core->ops->round_rate(core->hw, rate,
&best_parent_rate);
if (ret < 0)
return NULL;
clk_core_get_boundaries(clk->core, &min_rate, &max_rate); new_rate = ret;
if (new_rate < min_rate || new_rate > max_rate)
return NULL;
} else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) {
/* pass-through clock without adjustable parent */
core->new_rate = core->rate;
return NULL;
} else {
/* pass-through clock with adjustable parent */
top = clk_calc_new_rates(parent, rate);
new_rate = parent->new_rate;
goto out;
}
return clk_core_round_rate_nolock(clk->core, rate, min_rate, max_rate); /* some clocks must be gated to change parent */
} if (parent != old_parent &&
EXPORT_SYMBOL_GPL(__clk_round_rate); (core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
pr_debug("%s: %s not gated but wants to reparent\n",
__func__, core->name);
return NULL;
}
/** /* try finding the new parent index */
* clk_round_rate - round the given rate for a clk if (parent && core->num_parents > 1) {
* @clk: the clk for which we are rounding a rate p_index = clk_fetch_parent_index(core, parent);
* @rate: the rate which is to be rounded if (p_index < 0) {
* pr_debug("%s: clk %s can not be parent of clk %s\n",
* Takes in a rate as input and rounds it to a rate that the clk can actually __func__, parent->name, core->name);
* use which is then returned. If clk doesn't support round_rate operation return NULL;
* then the parent rate is returned. }
*/ }
long clk_round_rate(struct clk *clk, unsigned long rate)
{
unsigned long ret;
if (!clk) if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
return 0; best_parent_rate != parent->rate)
top = clk_calc_new_rates(parent, best_parent_rate);
clk_prepare_lock(); out:
ret = __clk_round_rate(clk, rate); clk_calc_subtree(core, new_rate, parent, p_index);
clk_prepare_unlock();
return ret; return top;
} }
EXPORT_SYMBOL_GPL(clk_round_rate);
/** /*
* __clk_notify - call clk notifier chain * Notify about rate changes in a subtree. Always walk down the whole tree
* @core: clk that is changing rate * so that in case of an error we can walk down the whole tree again and
* @msg: clk notifier type (see include/linux/clk.h) * abort the change.
* @old_rate: old clk rate
* @new_rate: new clk rate
*
* Triggers a notifier call chain on the clk rate-change notification
* for 'clk'. Passes a pointer to the struct clk and the previous
* and current rates to the notifier callback. Intended to be called by
* internal clock code only. Returns NOTIFY_DONE from the last driver
* called if all went well, or NOTIFY_STOP or NOTIFY_BAD immediately if
* a driver returns that.
*/ */
static int __clk_notify(struct clk_core *core, unsigned long msg, static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
unsigned long old_rate, unsigned long new_rate) unsigned long event)
{ {
struct clk_notifier *cn; struct clk_core *child, *tmp_clk, *fail_clk = NULL;
struct clk_notifier_data cnd;
int ret = NOTIFY_DONE; int ret = NOTIFY_DONE;
cnd.old_rate = old_rate; if (core->rate == core->new_rate)
cnd.new_rate = new_rate; return NULL;
list_for_each_entry(cn, &clk_notifier_list, node) { if (core->notifier_count) {
if (cn->clk->core == core) { ret = __clk_notify(core, event, core->rate, core->new_rate);
cnd.clk = cn->clk; if (ret & NOTIFY_STOP_MASK)
ret = srcu_notifier_call_chain(&cn->notifier_head, msg, fail_clk = core;
&cnd);
}
} }
return ret; hlist_for_each_entry(child, &core->children, child_node) {
} /* Skip children who will be reparented to another clock */
if (child->new_parent && child->new_parent != core)
/** continue;
* __clk_recalc_accuracies tmp_clk = clk_propagate_rate_change(child, event);
* @core: first clk in the subtree if (tmp_clk)
* fail_clk = tmp_clk;
* Walks the subtree of clks starting with clk and recalculates accuracies as }
* it goes. Note that if a clk does not implement the .recalc_accuracy
* callback then it is assumed that the clock will take on the accuracy of it's
* parent.
*
* Caller must hold prepare_lock.
*/
static void __clk_recalc_accuracies(struct clk_core *core)
{
unsigned long parent_accuracy = 0;
struct clk_core *child;
lockdep_assert_held(&prepare_lock);
if (core->parent)
parent_accuracy = core->parent->accuracy;
if (core->ops->recalc_accuracy) /* handle the new child who might not be in core->children yet */
core->accuracy = core->ops->recalc_accuracy(core->hw, if (core->new_child) {
parent_accuracy); tmp_clk = clk_propagate_rate_change(core->new_child, event);
else if (tmp_clk)
core->accuracy = parent_accuracy; fail_clk = tmp_clk;
}
hlist_for_each_entry(child, &core->children, child_node) return fail_clk;
__clk_recalc_accuracies(child);
} }
static long clk_core_get_accuracy(struct clk_core *core) /*
* walk down a subtree and set the new rates notifying the rate
* change on the way
*/
static void clk_change_rate(struct clk_core *core)
{ {
unsigned long accuracy; struct clk_core *child;
struct hlist_node *tmp;
clk_prepare_lock(); unsigned long old_rate;
if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE)) unsigned long best_parent_rate = 0;
__clk_recalc_accuracies(core); bool skip_set_rate = false;
struct clk_core *old_parent;
accuracy = __clk_get_accuracy(core); old_rate = core->rate;
clk_prepare_unlock();
return accuracy; if (core->new_parent)
} best_parent_rate = core->new_parent->rate;
else if (core->parent)
best_parent_rate = core->parent->rate;
/** if (core->new_parent && core->new_parent != core->parent) {
* clk_get_accuracy - return the accuracy of clk old_parent = __clk_set_parent_before(core, core->new_parent);
* @clk: the clk whose accuracy is being returned trace_clk_set_parent(core, core->new_parent);
*
* Simply returns the cached accuracy of the clk, unless
* CLK_GET_ACCURACY_NOCACHE flag is set, which means a recalc_rate will be
* issued.
* If clk is NULL then returns 0.
*/
long clk_get_accuracy(struct clk *clk)
{
if (!clk)
return 0;
return clk_core_get_accuracy(clk->core); if (core->ops->set_rate_and_parent) {
} skip_set_rate = true;
EXPORT_SYMBOL_GPL(clk_get_accuracy); core->ops->set_rate_and_parent(core->hw, core->new_rate,
best_parent_rate,
core->new_parent_index);
} else if (core->ops->set_parent) {
core->ops->set_parent(core->hw, core->new_parent_index);
}
static unsigned long clk_recalc(struct clk_core *core, trace_clk_set_parent_complete(core, core->new_parent);
unsigned long parent_rate) __clk_set_parent_after(core, core->new_parent, old_parent);
{ }
if (core->ops->recalc_rate)
return core->ops->recalc_rate(core->hw, parent_rate);
return parent_rate;
}
/** trace_clk_set_rate(core, core->new_rate);
* __clk_recalc_rates
* @core: first clk in the subtree
* @msg: notification type (see include/linux/clk.h)
*
* Walks the subtree of clks starting with clk and recalculates rates as it
* goes. Note that if a clk does not implement the .recalc_rate callback then
* it is assumed that the clock will take on the rate of its parent.
*
* clk_recalc_rates also propagates the POST_RATE_CHANGE notification,
* if necessary.
*
* Caller must hold prepare_lock.
*/
static void __clk_recalc_rates(struct clk_core *core, unsigned long msg)
{
unsigned long old_rate;
unsigned long parent_rate = 0;
struct clk_core *child;
lockdep_assert_held(&prepare_lock); if (!skip_set_rate && core->ops->set_rate)
core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
old_rate = core->rate; trace_clk_set_rate_complete(core, core->new_rate);
if (core->parent) core->rate = clk_recalc(core, best_parent_rate);
parent_rate = core->parent->rate;
core->rate = clk_recalc(core, parent_rate); if (core->notifier_count && old_rate != core->rate)
__clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
/* /*
* ignore NOTIFY_STOP and NOTIFY_BAD return values for POST_RATE_CHANGE * Use safe iteration, as change_rate can actually swap parents
* & ABORT_RATE_CHANGE notifiers * for certain clock types.
*/ */
if (core->notifier_count && msg) hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
__clk_notify(core, msg, old_rate, core->rate); /* Skip children who will be reparented to another clock */
if (child->new_parent && child->new_parent != core)
continue;
clk_change_rate(child);
}
hlist_for_each_entry(child, &core->children, child_node) /* handle the new child who might not be in core->children yet */
__clk_recalc_rates(child, msg); if (core->new_child)
clk_change_rate(core->new_child);
} }
static unsigned long clk_core_get_rate(struct clk_core *core) static int clk_core_set_rate_nolock(struct clk_core *core,
unsigned long req_rate)
{ {
unsigned long rate; struct clk_core *top, *fail_clk;
unsigned long rate = req_rate;
int ret = 0;
clk_prepare_lock(); if (!core)
return 0;
if (core && (core->flags & CLK_GET_RATE_NOCACHE)) /* bail early if nothing to do */
__clk_recalc_rates(core, 0); if (rate == clk_core_get_rate_nolock(core))
return 0;
rate = clk_core_get_rate_nolock(core); if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count)
clk_prepare_unlock(); return -EBUSY;
return rate; /* calculate new rates and get the topmost changed clock */
top = clk_calc_new_rates(core, rate);
if (!top)
return -EINVAL;
/* notify that we are about to change rates */
fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
if (fail_clk) {
pr_debug("%s: failed to set %s rate\n", __func__,
fail_clk->name);
clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
return -EBUSY;
}
/* change the rates */
clk_change_rate(top);
core->req_rate = req_rate;
return ret;
} }
/** /**
* clk_get_rate - return the rate of clk * clk_set_rate - specify a new rate for clk
* @clk: the clk whose rate is being returned * @clk: the clk whose rate is being changed
* @rate: the new rate for clk
* *
* Simply returns the cached rate of the clk, unless CLK_GET_RATE_NOCACHE flag * In the simplest case clk_set_rate will only adjust the rate of clk.
* is set, which means a recalc_rate will be issued. *
* If clk is NULL then returns 0. * Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to
* propagate up to clk's parent; whether or not this happens depends on the
* outcome of clk's .round_rate implementation. If *parent_rate is unchanged
* after calling .round_rate then upstream parent propagation is ignored. If
* *parent_rate comes back with a new rate for clk's parent then we propagate
* up to clk's parent and set its rate. Upward propagation will continue
* until either a clk does not support the CLK_SET_RATE_PARENT flag or
* .round_rate stops requesting changes to clk's parent_rate.
*
* Rate changes are accomplished via tree traversal that also recalculates the
* rates for the clocks and fires off POST_RATE_CHANGE notifiers.
*
* Returns 0 on success, -EERROR otherwise.
*/ */
unsigned long clk_get_rate(struct clk *clk) int clk_set_rate(struct clk *clk, unsigned long rate)
{ {
int ret;
if (!clk) if (!clk)
return 0; return 0;
return clk_core_get_rate(clk->core); /* prevent racing with updates to the clock topology */
} clk_prepare_lock();
EXPORT_SYMBOL_GPL(clk_get_rate);
static int clk_fetch_parent_index(struct clk_core *core,
struct clk_core *parent)
{
int i;
if (!core->parents) {
core->parents = kcalloc(core->num_parents,
sizeof(struct clk *), GFP_KERNEL);
if (!core->parents)
return -ENOMEM;
}
/*
* find index of new parent clock using cached parent ptrs,
* or if not yet cached, use string name comparison and cache
* them now to avoid future calls to clk_core_lookup.
*/
for (i = 0; i < core->num_parents; i++) {
if (core->parents[i] == parent)
return i;
if (core->parents[i]) ret = clk_core_set_rate_nolock(clk->core, rate);
continue;
if (!strcmp(core->parent_names[i], parent->name)) { clk_prepare_unlock();
core->parents[i] = clk_core_lookup(parent->name);
return i;
}
}
return -EINVAL; return ret;
} }
EXPORT_SYMBOL_GPL(clk_set_rate);
static void clk_reparent(struct clk_core *core, struct clk_core *new_parent) /**
* clk_set_rate_range - set a rate range for a clock source
* @clk: clock source
* @min: desired minimum clock rate in Hz, inclusive
* @max: desired maximum clock rate in Hz, inclusive
*
* Returns success (0) or negative errno.
*/
int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
{ {
hlist_del(&core->child_node); int ret = 0;
if (new_parent) { if (!clk)
/* avoid duplicate POST_RATE_CHANGE notifications */ return 0;
if (new_parent->new_child == core)
new_parent->new_child = NULL;
hlist_add_head(&core->child_node, &new_parent->children); if (min > max) {
} else { pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n",
hlist_add_head(&core->child_node, &clk_orphan_list); __func__, clk->core->name, clk->dev_id, clk->con_id,
min, max);
return -EINVAL;
} }
core->parent = new_parent; clk_prepare_lock();
}
static struct clk_core *__clk_set_parent_before(struct clk_core *core,
struct clk_core *parent)
{
unsigned long flags;
struct clk_core *old_parent = core->parent;
/* if (min != clk->min_rate || max != clk->max_rate) {
* Migrate prepare state between parents and prevent race with clk->min_rate = min;
* clk_enable(). clk->max_rate = max;
* ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
* If the clock is not prepared, then a race with
* clk_enable/disable() is impossible since we already have the
* prepare lock (future calls to clk_enable() need to be preceded by
* a clk_prepare()).
*
* If the clock is prepared, migrate the prepared state to the new
* parent and also protect against a race with clk_enable() by
* forcing the clock and the new parent on. This ensures that all
* future calls to clk_enable() are practically NOPs with respect to
* hardware and software states.
*
* See also: Comment for clk_set_parent() below.
*/
if (core->prepare_count) {
clk_core_prepare(parent);
clk_core_enable(parent);
clk_core_enable(core);
} }
/* update the clk tree topology */ clk_prepare_unlock();
flags = clk_enable_lock();
clk_reparent(core, parent);
clk_enable_unlock(flags);
return old_parent; return ret;
} }
EXPORT_SYMBOL_GPL(clk_set_rate_range);
static void __clk_set_parent_after(struct clk_core *core, /**
struct clk_core *parent, * clk_set_min_rate - set a minimum clock rate for a clock source
struct clk_core *old_parent) * @clk: clock source
* @rate: desired minimum clock rate in Hz, inclusive
*
* Returns success (0) or negative errno.
*/
int clk_set_min_rate(struct clk *clk, unsigned long rate)
{ {
/* if (!clk)
* Finish the migration of prepare state and undo the changes done return 0;
* for preventing a race with clk_enable().
*/ return clk_set_rate_range(clk, rate, clk->max_rate);
if (core->prepare_count) {
clk_core_disable(core);
clk_core_disable(old_parent);
clk_core_unprepare(old_parent);
}
} }
EXPORT_SYMBOL_GPL(clk_set_min_rate);
static int __clk_set_parent(struct clk_core *core, struct clk_core *parent, /**
u8 p_index) * clk_set_max_rate - set a maximum clock rate for a clock source
* @clk: clock source
* @rate: desired maximum clock rate in Hz, inclusive
*
* Returns success (0) or negative errno.
*/
int clk_set_max_rate(struct clk *clk, unsigned long rate)
{ {
unsigned long flags; if (!clk)
int ret = 0; return 0;
struct clk_core *old_parent;
old_parent = __clk_set_parent_before(core, parent);
trace_clk_set_parent(core, parent);
/* change clock input source */
if (parent && core->ops->set_parent)
ret = core->ops->set_parent(core->hw, p_index);
trace_clk_set_parent_complete(core, parent);
if (ret) {
flags = clk_enable_lock();
clk_reparent(core, old_parent);
clk_enable_unlock(flags);
if (core->prepare_count) {
clk_core_disable(core);
clk_core_disable(parent);
clk_core_unprepare(parent);
}
return ret;
}
__clk_set_parent_after(core, parent, old_parent);
return 0; return clk_set_rate_range(clk, clk->min_rate, rate);
} }
EXPORT_SYMBOL_GPL(clk_set_max_rate);
/** /**
* __clk_speculate_rates * clk_get_parent - return the parent of a clk
* @core: first clk in the subtree * @clk: the clk whose parent gets returned
* @parent_rate: the "future" rate of clk's parent
*
* Walks the subtree of clks starting with clk, speculating rates as it
* goes and firing off PRE_RATE_CHANGE notifications as necessary.
*
* Unlike clk_recalc_rates, clk_speculate_rates exists only for sending
* pre-rate change notifications and returns early if no clks in the
* subtree have subscribed to the notifications. Note that if a clk does not
* implement the .recalc_rate callback then it is assumed that the clock will
* take on the rate of its parent.
* *
* Caller must hold prepare_lock. * Simply returns clk->parent. Returns NULL if clk is NULL.
*/ */
static int __clk_speculate_rates(struct clk_core *core, struct clk *clk_get_parent(struct clk *clk)
unsigned long parent_rate)
{ {
struct clk_core *child; struct clk *parent;
unsigned long new_rate;
int ret = NOTIFY_DONE;
lockdep_assert_held(&prepare_lock); clk_prepare_lock();
parent = __clk_get_parent(clk);
clk_prepare_unlock();
new_rate = clk_recalc(core, parent_rate); return parent;
}
EXPORT_SYMBOL_GPL(clk_get_parent);
/* abort rate change if a driver returns NOTIFY_BAD or NOTIFY_STOP */ /*
if (core->notifier_count) * .get_parent is mandatory for clocks with multiple possible parents. It is
ret = __clk_notify(core, PRE_RATE_CHANGE, core->rate, new_rate); * optional for single-parent clocks. Always call .get_parent if it is
* available and WARN if it is missing for multi-parent clocks.
*
* For single-parent clocks without .get_parent, first check to see if the
* .parents array exists, and if so use it to avoid an expensive tree
* traversal. If .parents does not exist then walk the tree.
*/
static struct clk_core *__clk_init_parent(struct clk_core *core)
{
struct clk_core *ret = NULL;
u8 index;
if (ret & NOTIFY_STOP_MASK) { /* handle the trivial cases */
pr_debug("%s: clk notifier callback for clock %s aborted with error %d\n",
__func__, core->name, ret); if (!core->num_parents)
goto out; goto out;
}
hlist_for_each_entry(child, &core->children, child_node) { if (core->num_parents == 1) {
ret = __clk_speculate_rates(child, new_rate); if (IS_ERR_OR_NULL(core->parent))
if (ret & NOTIFY_STOP_MASK) core->parent = clk_core_lookup(core->parent_names[0]);
break; ret = core->parent;
goto out;
} }
if (!core->ops->get_parent) {
WARN(!core->ops->get_parent,
"%s: multi-parent clocks must implement .get_parent\n",
__func__);
goto out;
};
/*
* Do our best to cache parent clocks in core->parents. This prevents
* unnecessary and expensive lookups. We don't set core->parent here;
* that is done by the calling function.
*/
index = core->ops->get_parent(core->hw);
if (!core->parents)
core->parents =
kcalloc(core->num_parents, sizeof(struct clk *),
GFP_KERNEL);
ret = clk_core_get_parent_by_index(core, index);
out: out:
return ret; return ret;
} }
static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate, static void clk_core_reparent(struct clk_core *core,
struct clk_core *new_parent, u8 p_index) struct clk_core *new_parent)
{ {
struct clk_core *child; clk_reparent(core, new_parent);
__clk_recalc_accuracies(core);
core->new_rate = new_rate; __clk_recalc_rates(core, POST_RATE_CHANGE);
core->new_parent = new_parent;
core->new_parent_index = p_index;
/* include clk in new parent's PRE_RATE_CHANGE notifications */
core->new_child = NULL;
if (new_parent && new_parent != core->parent)
new_parent->new_child = core;
hlist_for_each_entry(child, &core->children, child_node) {
child->new_rate = clk_recalc(child, new_rate);
clk_calc_subtree(child, child->new_rate, NULL, 0);
}
} }
/* /**
* calculate the new rates returning the topmost clock that has to be * clk_has_parent - check if a clock is a possible parent for another
* changed. * @clk: clock source
* @parent: parent clock source
*
* This function can be used in drivers that need to check that a clock can be
* the parent of another without actually changing the parent.
*
* Returns true if @parent is a possible parent for @clk, false otherwise.
*/ */
static struct clk_core *clk_calc_new_rates(struct clk_core *core, bool clk_has_parent(struct clk *clk, struct clk *parent)
unsigned long rate)
{ {
struct clk_core *top = core; struct clk_core *core, *parent_core;
struct clk_core *old_parent, *parent; unsigned int i;
struct clk_hw *parent_hw;
unsigned long best_parent_rate = 0;
unsigned long new_rate;
unsigned long min_rate;
unsigned long max_rate;
int p_index = 0;
long ret;
/* sanity */ /* NULL clocks should be nops, so return success if either is NULL. */
if (IS_ERR_OR_NULL(core)) if (!clk || !parent)
return NULL; return true;
/* save parent rate, if it exists */ core = clk->core;
parent = old_parent = core->parent; parent_core = parent->core;
if (parent)
best_parent_rate = parent->rate;
clk_core_get_boundaries(core, &min_rate, &max_rate); /* Optimize for the case where the parent is already the parent. */
if (core->parent == parent_core)
return true;
/* find the closest rate and parent clk/rate */ for (i = 0; i < core->num_parents; i++)
if (core->ops->determine_rate) { if (strcmp(core->parent_names[i], parent_core->name) == 0)
parent_hw = parent ? parent->hw : NULL; return true;
ret = core->ops->determine_rate(core->hw, rate,
min_rate,
max_rate,
&best_parent_rate,
&parent_hw);
if (ret < 0)
return NULL;
new_rate = ret; return false;
parent = parent_hw ? parent_hw->core : NULL; }
} else if (core->ops->round_rate) { EXPORT_SYMBOL_GPL(clk_has_parent);
ret = core->ops->round_rate(core->hw, rate,
&best_parent_rate);
if (ret < 0)
return NULL;
new_rate = ret; static int clk_core_set_parent(struct clk_core *core, struct clk_core *parent)
if (new_rate < min_rate || new_rate > max_rate) {
return NULL; int ret = 0;
} else if (!parent || !(core->flags & CLK_SET_RATE_PARENT)) { int p_index = 0;
/* pass-through clock without adjustable parent */ unsigned long p_rate = 0;
core->new_rate = core->rate;
return NULL; if (!core)
} else { return 0;
/* pass-through clock with adjustable parent */
top = clk_calc_new_rates(parent, rate); /* prevent racing with updates to the clock topology */
new_rate = parent->new_rate; clk_prepare_lock();
if (core->parent == parent)
goto out;
/* verify ops for for multi-parent clks */
if ((core->num_parents > 1) && (!core->ops->set_parent)) {
ret = -ENOSYS;
goto out; goto out;
} }
/* some clocks must be gated to change parent */ /* check that we are allowed to re-parent if the clock is in use */
if (parent != old_parent && if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
(core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) { ret = -EBUSY;
pr_debug("%s: %s not gated but wants to reparent\n", goto out;
__func__, core->name);
return NULL;
} }
/* try finding the new parent index */ /* try finding the new parent index */
if (parent && core->num_parents > 1) { if (parent) {
p_index = clk_fetch_parent_index(core, parent); p_index = clk_fetch_parent_index(core, parent);
p_rate = parent->rate;
if (p_index < 0) { if (p_index < 0) {
pr_debug("%s: clk %s can not be parent of clk %s\n", pr_debug("%s: clk %s can not be parent of clk %s\n",
__func__, parent->name, core->name); __func__, parent->name, core->name);
return NULL; ret = p_index;
goto out;
} }
} }
if ((core->flags & CLK_SET_RATE_PARENT) && parent && /* propagate PRE_RATE_CHANGE notifications */
best_parent_rate != parent->rate) ret = __clk_speculate_rates(core, p_rate);
top = clk_calc_new_rates(parent, best_parent_rate);
out:
clk_calc_subtree(core, new_rate, parent, p_index);
return top;
}
/* /* abort if a driver objects */
* Notify about rate changes in a subtree. Always walk down the whole tree if (ret & NOTIFY_STOP_MASK)
* so that in case of an error we can walk down the whole tree again and goto out;
* abort the change.
*/
static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
unsigned long event)
{
struct clk_core *child, *tmp_clk, *fail_clk = NULL;
int ret = NOTIFY_DONE;
if (core->rate == core->new_rate) /* do the re-parent */
return NULL; ret = __clk_set_parent(core, parent, p_index);
if (core->notifier_count) { /* propagate rate an accuracy recalculation accordingly */
ret = __clk_notify(core, event, core->rate, core->new_rate); if (ret) {
if (ret & NOTIFY_STOP_MASK) __clk_recalc_rates(core, ABORT_RATE_CHANGE);
fail_clk = core; } else {
__clk_recalc_rates(core, POST_RATE_CHANGE);
__clk_recalc_accuracies(core);
} }
hlist_for_each_entry(child, &core->children, child_node) { out:
/* Skip children who will be reparented to another clock */ clk_prepare_unlock();
if (child->new_parent && child->new_parent != core)
continue;
tmp_clk = clk_propagate_rate_change(child, event);
if (tmp_clk)
fail_clk = tmp_clk;
}
/* handle the new child who might not be in core->children yet */ return ret;
if (core->new_child) { }
tmp_clk = clk_propagate_rate_change(core->new_child, event);
if (tmp_clk)
fail_clk = tmp_clk;
}
return fail_clk; /**
* clk_set_parent - switch the parent of a mux clk
* @clk: the mux clk whose input we are switching
* @parent: the new input to clk
*
* Re-parent clk to use parent as its new input source. If clk is in
* prepared state, the clk will get enabled for the duration of this call. If
* that's not acceptable for a specific clk (Eg: the consumer can't handle
* that, the reparenting is glitchy in hardware, etc), use the
* CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
*
* After successfully changing clk's parent clk_set_parent will update the
* clk topology, sysfs topology and propagate rate recalculation via
* __clk_recalc_rates.
*
* Returns 0 on success, -EERROR otherwise.
*/
int clk_set_parent(struct clk *clk, struct clk *parent)
{
if (!clk)
return 0;
return clk_core_set_parent(clk->core, parent ? parent->core : NULL);
} }
EXPORT_SYMBOL_GPL(clk_set_parent);
/* /**
* walk down a subtree and set the new rates notifying the rate * clk_set_phase - adjust the phase shift of a clock signal
* change on the way * @clk: clock signal source
* @degrees: number of degrees the signal is shifted
*
* Shifts the phase of a clock signal by the specified
* degrees. Returns 0 on success, -EERROR otherwise.
*
* This function makes no distinction about the input or reference
* signal that we adjust the clock signal phase against. For example
* phase locked-loop clock signal generators we may shift phase with
* respect to feedback clock signal input, but for other cases the
* clock phase may be shifted with respect to some other, unspecified
* signal.
*
* Additionally the concept of phase shift does not propagate through
* the clock tree hierarchy, which sets it apart from clock rates and
* clock accuracy. A parent clock phase attribute does not have an
* impact on the phase attribute of a child clock.
*/ */
static void clk_change_rate(struct clk_core *core) int clk_set_phase(struct clk *clk, int degrees)
{ {
struct clk_core *child; int ret = -EINVAL;
struct hlist_node *tmp;
unsigned long old_rate;
unsigned long best_parent_rate = 0;
bool skip_set_rate = false;
struct clk_core *old_parent;
old_rate = core->rate; if (!clk)
return 0;
if (core->new_parent) /* sanity check degrees */
best_parent_rate = core->new_parent->rate; degrees %= 360;
else if (core->parent) if (degrees < 0)
best_parent_rate = core->parent->rate; degrees += 360;
if (core->new_parent && core->new_parent != core->parent) { clk_prepare_lock();
old_parent = __clk_set_parent_before(core, core->new_parent);
trace_clk_set_parent(core, core->new_parent);
if (core->ops->set_rate_and_parent) { trace_clk_set_phase(clk->core, degrees);
skip_set_rate = true;
core->ops->set_rate_and_parent(core->hw, core->new_rate,
best_parent_rate,
core->new_parent_index);
} else if (core->ops->set_parent) {
core->ops->set_parent(core->hw, core->new_parent_index);
}
trace_clk_set_parent_complete(core, core->new_parent); if (clk->core->ops->set_phase)
__clk_set_parent_after(core, core->new_parent, old_parent); ret = clk->core->ops->set_phase(clk->core->hw, degrees);
}
trace_clk_set_rate(core, core->new_rate); trace_clk_set_phase_complete(clk->core, degrees);
if (!skip_set_rate && core->ops->set_rate) if (!ret)
core->ops->set_rate(core->hw, core->new_rate, best_parent_rate); clk->core->phase = degrees;
trace_clk_set_rate_complete(core, core->new_rate); clk_prepare_unlock();
core->rate = clk_recalc(core, best_parent_rate); return ret;
}
EXPORT_SYMBOL_GPL(clk_set_phase);
if (core->notifier_count && old_rate != core->rate) static int clk_core_get_phase(struct clk_core *core)
__clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate); {
int ret;
/* clk_prepare_lock();
* Use safe iteration, as change_rate can actually swap parents ret = core->phase;
* for certain clock types. clk_prepare_unlock();
*/
hlist_for_each_entry_safe(child, tmp, &core->children, child_node) {
/* Skip children who will be reparented to another clock */
if (child->new_parent && child->new_parent != core)
continue;
clk_change_rate(child);
}
/* handle the new child who might not be in core->children yet */ return ret;
if (core->new_child)
clk_change_rate(core->new_child);
} }
static int clk_core_set_rate_nolock(struct clk_core *core, /**
unsigned long req_rate) * clk_get_phase - return the phase shift of a clock signal
* @clk: clock signal source
*
* Returns the phase shift of a clock node in degrees, otherwise returns
* -EERROR.
*/
int clk_get_phase(struct clk *clk)
{ {
struct clk_core *top, *fail_clk; if (!clk)
unsigned long rate = req_rate;
int ret = 0;
if (!core)
return 0; return 0;
/* bail early if nothing to do */ return clk_core_get_phase(clk->core);
if (rate == clk_core_get_rate_nolock(core)) }
return 0; EXPORT_SYMBOL_GPL(clk_get_phase);
if ((core->flags & CLK_SET_RATE_GATE) && core->prepare_count) /**
return -EBUSY; * clk_is_match - check if two clk's point to the same hardware clock
* @p: clk compared against q
* @q: clk compared against p
*
* Returns true if the two struct clk pointers both point to the same hardware
* clock node. Put differently, returns true if struct clk *p and struct clk *q
* share the same struct clk_core object.
*
* Returns false otherwise. Note that two NULL clks are treated as matching.
*/
bool clk_is_match(const struct clk *p, const struct clk *q)
{
/* trivial case: identical struct clk's or both NULL */
if (p == q)
return true;
/* calculate new rates and get the topmost changed clock */ /* true if clk->core pointers match. Avoid derefing garbage */
top = clk_calc_new_rates(core, rate); if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
if (!top) if (p->core == q->core)
return -EINVAL; return true;
/* notify that we are about to change rates */ return false;
fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE); }
if (fail_clk) { EXPORT_SYMBOL_GPL(clk_is_match);
pr_debug("%s: failed to set %s rate\n", __func__,
fail_clk->name);
clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
return -EBUSY;
}
/* change the rates */ /*** debugfs support ***/
clk_change_rate(top);
core->req_rate = req_rate; #ifdef CONFIG_DEBUG_FS
#include <linux/debugfs.h>
return ret; static struct dentry *rootdir;
} static int inited = 0;
static DEFINE_MUTEX(clk_debug_lock);
static HLIST_HEAD(clk_debug_list);
/** static struct hlist_head *all_lists[] = {
* clk_set_rate - specify a new rate for clk &clk_root_list,
* @clk: the clk whose rate is being changed &clk_orphan_list,
* @rate: the new rate for clk NULL,
* };
* In the simplest case clk_set_rate will only adjust the rate of clk.
* static struct hlist_head *orphan_list[] = {
* Setting the CLK_SET_RATE_PARENT flag allows the rate change operation to &clk_orphan_list,
* propagate up to clk's parent; whether or not this happens depends on the NULL,
* outcome of clk's .round_rate implementation. If *parent_rate is unchanged };
* after calling .round_rate then upstream parent propagation is ignored. If
* *parent_rate comes back with a new rate for clk's parent then we propagate static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
* up to clk's parent and set its rate. Upward propagation will continue int level)
* until either a clk does not support the CLK_SET_RATE_PARENT flag or
* .round_rate stops requesting changes to clk's parent_rate.
*
* Rate changes are accomplished via tree traversal that also recalculates the
* rates for the clocks and fires off POST_RATE_CHANGE notifiers.
*
* Returns 0 on success, -EERROR otherwise.
*/
int clk_set_rate(struct clk *clk, unsigned long rate)
{ {
int ret; if (!c)
return;
if (!clk) seq_printf(s, "%*s%-*s %11d %12d %11lu %10lu %-3d\n",
return 0; level * 3 + 1, "",
30 - level * 3, c->name,
c->enable_count, c->prepare_count, clk_core_get_rate(c),
clk_core_get_accuracy(c), clk_core_get_phase(c));
}
/* prevent racing with updates to the clock topology */ static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
clk_prepare_lock(); int level)
{
struct clk_core *child;
ret = clk_core_set_rate_nolock(clk->core, rate); if (!c)
return;
clk_prepare_unlock(); clk_summary_show_one(s, c, level);
return ret; hlist_for_each_entry(child, &c->children, child_node)
clk_summary_show_subtree(s, child, level + 1);
} }
EXPORT_SYMBOL_GPL(clk_set_rate);
/** static int clk_summary_show(struct seq_file *s, void *data)
* clk_set_rate_range - set a rate range for a clock source
* @clk: clock source
* @min: desired minimum clock rate in Hz, inclusive
* @max: desired maximum clock rate in Hz, inclusive
*
* Returns success (0) or negative errno.
*/
int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
{ {
int ret = 0; struct clk_core *c;
struct hlist_head **lists = (struct hlist_head **)s->private;
if (!clk)
return 0;
if (min > max) { seq_puts(s, " clock enable_cnt prepare_cnt rate accuracy phase\n");
pr_err("%s: clk %s dev %s con %s: invalid range [%lu, %lu]\n", seq_puts(s, "----------------------------------------------------------------------------------------\n");
__func__, clk->core->name, clk->dev_id, clk->con_id,
min, max);
return -EINVAL;
}
clk_prepare_lock(); clk_prepare_lock();
if (min != clk->min_rate || max != clk->max_rate) { for (; *lists; lists++)
clk->min_rate = min; hlist_for_each_entry(c, *lists, child_node)
clk->max_rate = max; clk_summary_show_subtree(s, c, 0);
ret = clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
}
clk_prepare_unlock(); clk_prepare_unlock();
return ret; return 0;
} }
EXPORT_SYMBOL_GPL(clk_set_rate_range);
/**
* clk_set_min_rate - set a minimum clock rate for a clock source
* @clk: clock source
* @rate: desired minimum clock rate in Hz, inclusive
*
* Returns success (0) or negative errno.
*/
int clk_set_min_rate(struct clk *clk, unsigned long rate)
{
if (!clk)
return 0;
return clk_set_rate_range(clk, rate, clk->max_rate);
}
EXPORT_SYMBOL_GPL(clk_set_min_rate);
/** static int clk_summary_open(struct inode *inode, struct file *file)
* clk_set_max_rate - set a maximum clock rate for a clock source
* @clk: clock source
* @rate: desired maximum clock rate in Hz, inclusive
*
* Returns success (0) or negative errno.
*/
int clk_set_max_rate(struct clk *clk, unsigned long rate)
{ {
if (!clk) return single_open(file, clk_summary_show, inode->i_private);
return 0;
return clk_set_rate_range(clk, clk->min_rate, rate);
} }
EXPORT_SYMBOL_GPL(clk_set_max_rate);
/** static const struct file_operations clk_summary_fops = {
* clk_get_parent - return the parent of a clk .open = clk_summary_open,
* @clk: the clk whose parent gets returned .read = seq_read,
* .llseek = seq_lseek,
* Simply returns clk->parent. Returns NULL if clk is NULL. .release = single_release,
*/ };
struct clk *clk_get_parent(struct clk *clk)
{
struct clk *parent;
clk_prepare_lock(); static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
parent = __clk_get_parent(clk); {
clk_prepare_unlock(); if (!c)
return;
return parent; seq_printf(s, "\"%s\": { ", c->name);
seq_printf(s, "\"enable_count\": %d,", c->enable_count);
seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
seq_printf(s, "\"rate\": %lu", clk_core_get_rate(c));
seq_printf(s, "\"accuracy\": %lu", clk_core_get_accuracy(c));
seq_printf(s, "\"phase\": %d", clk_core_get_phase(c));
} }
EXPORT_SYMBOL_GPL(clk_get_parent);
/* static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
* .get_parent is mandatory for clocks with multiple possible parents. It is
* optional for single-parent clocks. Always call .get_parent if it is
* available and WARN if it is missing for multi-parent clocks.
*
* For single-parent clocks without .get_parent, first check to see if the
* .parents array exists, and if so use it to avoid an expensive tree
* traversal. If .parents does not exist then walk the tree.
*/
static struct clk_core *__clk_init_parent(struct clk_core *core)
{ {
struct clk_core *ret = NULL; struct clk_core *child;
u8 index;
/* handle the trivial cases */ if (!c)
return;
if (!core->num_parents) clk_dump_one(s, c, level);
goto out;
if (core->num_parents == 1) { hlist_for_each_entry(child, &c->children, child_node) {
if (IS_ERR_OR_NULL(core->parent)) seq_printf(s, ",");
core->parent = clk_core_lookup(core->parent_names[0]); clk_dump_subtree(s, child, level + 1);
ret = core->parent;
goto out;
} }
if (!core->ops->get_parent) { seq_printf(s, "}");
WARN(!core->ops->get_parent,
"%s: multi-parent clocks must implement .get_parent\n",
__func__);
goto out;
};
/*
* Do our best to cache parent clocks in core->parents. This prevents
* unnecessary and expensive lookups. We don't set core->parent here;
* that is done by the calling function.
*/
index = core->ops->get_parent(core->hw);
if (!core->parents)
core->parents =
kcalloc(core->num_parents, sizeof(struct clk *),
GFP_KERNEL);
ret = clk_core_get_parent_by_index(core, index);
out:
return ret;
}
static void clk_core_reparent(struct clk_core *core,
struct clk_core *new_parent)
{
clk_reparent(core, new_parent);
__clk_recalc_accuracies(core);
__clk_recalc_rates(core, POST_RATE_CHANGE);
} }
/** static int clk_dump(struct seq_file *s, void *data)
* clk_has_parent - check if a clock is a possible parent for another
* @clk: clock source
* @parent: parent clock source
*
* This function can be used in drivers that need to check that a clock can be
* the parent of another without actually changing the parent.
*
* Returns true if @parent is a possible parent for @clk, false otherwise.
*/
bool clk_has_parent(struct clk *clk, struct clk *parent)
{ {
struct clk_core *core, *parent_core; struct clk_core *c;
unsigned int i; bool first_node = true;
struct hlist_head **lists = (struct hlist_head **)s->private;
/* NULL clocks should be nops, so return success if either is NULL. */ seq_printf(s, "{");
if (!clk || !parent)
return true;
core = clk->core; clk_prepare_lock();
parent_core = parent->core;
/* Optimize for the case where the parent is already the parent. */ for (; *lists; lists++) {
if (core->parent == parent_core) hlist_for_each_entry(c, *lists, child_node) {
return true; if (!first_node)
seq_puts(s, ",");
first_node = false;
clk_dump_subtree(s, c, 0);
}
}
for (i = 0; i < core->num_parents; i++) clk_prepare_unlock();
if (strcmp(core->parent_names[i], parent_core->name) == 0)
return true;
return false; seq_printf(s, "}");
return 0;
} }
EXPORT_SYMBOL_GPL(clk_has_parent);
static int clk_core_set_parent(struct clk_core *core, struct clk_core *parent)
static int clk_dump_open(struct inode *inode, struct file *file)
{ {
int ret = 0; return single_open(file, clk_dump, inode->i_private);
int p_index = 0; }
unsigned long p_rate = 0;
if (!core) static const struct file_operations clk_dump_fops = {
return 0; .open = clk_dump_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
/* prevent racing with updates to the clock topology */ static int clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
clk_prepare_lock(); {
struct dentry *d;
int ret = -ENOMEM;
if (core->parent == parent) if (!core || !pdentry) {
ret = -EINVAL;
goto out; goto out;
}
/* verify ops for for multi-parent clks */ d = debugfs_create_dir(core->name, pdentry);
if ((core->num_parents > 1) && (!core->ops->set_parent)) { if (!d)
ret = -ENOSYS;
goto out; goto out;
}
/* check that we are allowed to re-parent if the clock is in use */ core->dentry = d;
if ((core->flags & CLK_SET_PARENT_GATE) && core->prepare_count) {
ret = -EBUSY; d = debugfs_create_u32("clk_rate", S_IRUGO, core->dentry,
goto out; (u32 *)&core->rate);
} if (!d)
goto err_out;
d = debugfs_create_u32("clk_accuracy", S_IRUGO, core->dentry,
(u32 *)&core->accuracy);
if (!d)
goto err_out;
d = debugfs_create_u32("clk_phase", S_IRUGO, core->dentry,
(u32 *)&core->phase);
if (!d)
goto err_out;
/* try finding the new parent index */ d = debugfs_create_x32("clk_flags", S_IRUGO, core->dentry,
if (parent) { (u32 *)&core->flags);
p_index = clk_fetch_parent_index(core, parent); if (!d)
p_rate = parent->rate; goto err_out;
if (p_index < 0) {
pr_debug("%s: clk %s can not be parent of clk %s\n",
__func__, parent->name, core->name);
ret = p_index;
goto out;
}
}
/* propagate PRE_RATE_CHANGE notifications */ d = debugfs_create_u32("clk_prepare_count", S_IRUGO, core->dentry,
ret = __clk_speculate_rates(core, p_rate); (u32 *)&core->prepare_count);
if (!d)
goto err_out;
/* abort if a driver objects */ d = debugfs_create_u32("clk_enable_count", S_IRUGO, core->dentry,
if (ret & NOTIFY_STOP_MASK) (u32 *)&core->enable_count);
goto out; if (!d)
goto err_out;
/* do the re-parent */ d = debugfs_create_u32("clk_notifier_count", S_IRUGO, core->dentry,
ret = __clk_set_parent(core, parent, p_index); (u32 *)&core->notifier_count);
if (!d)
goto err_out;
/* propagate rate an accuracy recalculation accordingly */ if (core->ops->debug_init) {
if (ret) { ret = core->ops->debug_init(core->hw, core->dentry);
__clk_recalc_rates(core, ABORT_RATE_CHANGE); if (ret)
} else { goto err_out;
__clk_recalc_rates(core, POST_RATE_CHANGE);
__clk_recalc_accuracies(core);
} }
out: ret = 0;
clk_prepare_unlock(); goto out;
err_out:
debugfs_remove_recursive(core->dentry);
core->dentry = NULL;
out:
return ret; return ret;
} }
/** /**
* clk_set_parent - switch the parent of a mux clk * clk_debug_register - add a clk node to the debugfs clk tree
* @clk: the mux clk whose input we are switching * @core: the clk being added to the debugfs clk tree
* @parent: the new input to clk
*
* Re-parent clk to use parent as its new input source. If clk is in
* prepared state, the clk will get enabled for the duration of this call. If
* that's not acceptable for a specific clk (Eg: the consumer can't handle
* that, the reparenting is glitchy in hardware, etc), use the
* CLK_SET_PARENT_GATE flag to allow reparenting only when clk is unprepared.
*
* After successfully changing clk's parent clk_set_parent will update the
* clk topology, sysfs topology and propagate rate recalculation via
* __clk_recalc_rates.
* *
* Returns 0 on success, -EERROR otherwise. * Dynamically adds a clk to the debugfs clk tree if debugfs has been
* initialized. Otherwise it bails out early since the debugfs clk tree
* will be created lazily by clk_debug_init as part of a late_initcall.
*/ */
int clk_set_parent(struct clk *clk, struct clk *parent) static int clk_debug_register(struct clk_core *core)
{ {
if (!clk) int ret = 0;
return 0;
return clk_core_set_parent(clk->core, parent ? parent->core : NULL); mutex_lock(&clk_debug_lock);
hlist_add_head(&core->debug_node, &clk_debug_list);
if (!inited)
goto unlock;
ret = clk_debug_create_one(core, rootdir);
unlock:
mutex_unlock(&clk_debug_lock);
return ret;
} }
EXPORT_SYMBOL_GPL(clk_set_parent);
/** /**
* clk_set_phase - adjust the phase shift of a clock signal * clk_debug_unregister - remove a clk node from the debugfs clk tree
* @clk: clock signal source * @core: the clk being removed from the debugfs clk tree
* @degrees: number of degrees the signal is shifted
*
* Shifts the phase of a clock signal by the specified
* degrees. Returns 0 on success, -EERROR otherwise.
*
* This function makes no distinction about the input or reference
* signal that we adjust the clock signal phase against. For example
* phase locked-loop clock signal generators we may shift phase with
* respect to feedback clock signal input, but for other cases the
* clock phase may be shifted with respect to some other, unspecified
* signal.
* *
* Additionally the concept of phase shift does not propagate through * Dynamically removes a clk and all it's children clk nodes from the
* the clock tree hierarchy, which sets it apart from clock rates and * debugfs clk tree if clk->dentry points to debugfs created by
* clock accuracy. A parent clock phase attribute does not have an * clk_debug_register in __clk_init.
* impact on the phase attribute of a child clock.
*/ */
int clk_set_phase(struct clk *clk, int degrees) static void clk_debug_unregister(struct clk_core *core)
{ {
int ret = -EINVAL; mutex_lock(&clk_debug_lock);
hlist_del_init(&core->debug_node);
debugfs_remove_recursive(core->dentry);
core->dentry = NULL;
mutex_unlock(&clk_debug_lock);
}
if (!clk) struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
return 0; void *data, const struct file_operations *fops)
{
struct dentry *d = NULL;
/* sanity check degrees */ if (hw->core->dentry)
degrees %= 360; d = debugfs_create_file(name, mode, hw->core->dentry, data,
if (degrees < 0) fops);
degrees += 360;
clk_prepare_lock(); return d;
}
EXPORT_SYMBOL_GPL(clk_debugfs_add_file);
trace_clk_set_phase(clk->core, degrees); /**
* clk_debug_init - lazily create the debugfs clk tree visualization
*
* clks are often initialized very early during boot before memory can
* be dynamically allocated and well before debugfs is setup.
* clk_debug_init walks the clk tree hierarchy while holding
* prepare_lock and creates the topology as part of a late_initcall,
* thus insuring that clks initialized very early will still be
* represented in the debugfs clk tree. This function should only be
* called once at boot-time, and all other clks added dynamically will
* be done so with clk_debug_register.
*/
static int __init clk_debug_init(void)
{
struct clk_core *core;
struct dentry *d;
if (clk->core->ops->set_phase) rootdir = debugfs_create_dir("clk", NULL);
ret = clk->core->ops->set_phase(clk->core->hw, degrees);
trace_clk_set_phase_complete(clk->core, degrees); if (!rootdir)
return -ENOMEM;
if (!ret) d = debugfs_create_file("clk_summary", S_IRUGO, rootdir, &all_lists,
clk->core->phase = degrees; &clk_summary_fops);
if (!d)
return -ENOMEM;
clk_prepare_unlock(); d = debugfs_create_file("clk_dump", S_IRUGO, rootdir, &all_lists,
&clk_dump_fops);
if (!d)
return -ENOMEM;
return ret; d = debugfs_create_file("clk_orphan_summary", S_IRUGO, rootdir,
} &orphan_list, &clk_summary_fops);
EXPORT_SYMBOL_GPL(clk_set_phase); if (!d)
return -ENOMEM;
static int clk_core_get_phase(struct clk_core *core) d = debugfs_create_file("clk_orphan_dump", S_IRUGO, rootdir,
{ &orphan_list, &clk_dump_fops);
int ret; if (!d)
return -ENOMEM;
clk_prepare_lock(); mutex_lock(&clk_debug_lock);
ret = core->phase; hlist_for_each_entry(core, &clk_debug_list, debug_node)
clk_prepare_unlock(); clk_debug_create_one(core, rootdir);
return ret; inited = 1;
} mutex_unlock(&clk_debug_lock);
EXPORT_SYMBOL_GPL(clk_get_phase);
/** return 0;
* clk_get_phase - return the phase shift of a clock signal }
* @clk: clock signal source late_initcall(clk_debug_init);
* #else
* Returns the phase shift of a clock node in degrees, otherwise returns static inline int clk_debug_register(struct clk_core *core) { return 0; }
* -EERROR. static inline void clk_debug_reparent(struct clk_core *core,
*/ struct clk_core *new_parent)
int clk_get_phase(struct clk *clk)
{ {
if (!clk)
return 0;
return clk_core_get_phase(clk->core);
} }
static inline void clk_debug_unregister(struct clk_core *core)
/**
* clk_is_match - check if two clk's point to the same hardware clock
* @p: clk compared against q
* @q: clk compared against p
*
* Returns true if the two struct clk pointers both point to the same hardware
* clock node. Put differently, returns true if struct clk *p and struct clk *q
* share the same struct clk_core object.
*
* Returns false otherwise. Note that two NULL clks are treated as matching.
*/
bool clk_is_match(const struct clk *p, const struct clk *q)
{ {
/* trivial case: identical struct clk's or both NULL */
if (p == q)
return true;
/* true if clk->core pointers match. Avoid derefing garbage */
if (!IS_ERR_OR_NULL(p) && !IS_ERR_OR_NULL(q))
if (p->core == q->core)
return true;
return false;
} }
EXPORT_SYMBOL_GPL(clk_is_match); #endif
/** /**
* __clk_init - initialize the data structures in a struct clk * __clk_init - initialize the data structures in a struct clk
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册