[PATCH v2] clk: Add clk_set_parent debugfs node

From: Sam Protsenko
Date: Thu Oct 07 2021 - 09:50:23 EST


Useful for testing mux clocks. One can write the index of the parent to
set into clk_parent node, starting from 0. Example

# cd /sys/kernel/debug/clk/mout_peri_bus
# cat clk_possible_parrents
dout_shared0_div4 dout_shared1_div4
# cat clk_parent
dout_shared0_div4
# echo 1 > clk_parent
# cat clk_parent
dout_shared1_div4

One needs to define CLOCK_ALLOW_WRITE_DEBUGFS in drivers/clk/clk.c in
order to use this feature.

Signed-off-by: Sam Protsenko <semen.protsenko@xxxxxxxxxx>
---
Changes in v2:
- Merged write() function into existing 'clk_parent' file
- Removed 'if (val >= core->num_parents)' check
- Removed incorrect usage of IS_ERR_OR_NULL()

drivers/clk/clk.c | 56 +++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 54 insertions(+), 2 deletions(-)

diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index 806c55f0991b..5776efab4739 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -3224,6 +3224,46 @@ static int current_parent_show(struct seq_file *s, void *data)
}
DEFINE_SHOW_ATTRIBUTE(current_parent);

+#ifdef CLOCK_ALLOW_WRITE_DEBUGFS
+static ssize_t current_parent_write(struct file *file, const char __user *ubuf,
+ size_t count, loff_t *ppos)
+{
+ struct seq_file *s = file->private_data;
+ struct clk_core *core = s->private;
+ struct clk_core *parent;
+ char buf[4] = { 0 };
+ u8 idx;
+ int err;
+
+ if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
+ return -EFAULT;
+
+ err = kstrtou8(buf, 0, &idx);
+ if (err)
+ return err;
+
+ parent = clk_core_get_parent_by_index(core, idx);
+ if (!parent)
+ return -ENOENT;
+
+ clk_prepare_lock();
+ err = clk_core_set_parent_nolock(core, parent);
+ clk_prepare_unlock();
+ if (err)
+ return err;
+
+ return count;
+}
+
+static const struct file_operations current_parent_rw_fops = {
+ .open = current_parent_open,
+ .write = current_parent_write,
+ .read = seq_read,
+ .llseek = seq_lseek,
+ .release = single_release,
+};
+#endif
+
static int clk_duty_cycle_show(struct seq_file *s, void *data)
{
struct clk_core *core = s->private;
@@ -3291,13 +3331,25 @@ static void clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
&clk_prepare_enable_fops);
#endif

- if (core->num_parents > 0)
+ if (core->num_parents > 0) {
+#ifdef CLOCK_ALLOW_WRITE_DEBUGFS
+ if (core->num_parents > 1) {
+ debugfs_create_file("clk_parent", 0644, root, core,
+ &current_parent_rw_fops);
+ } else {
+ debugfs_create_file("clk_parent", 0444, root, core,
+ &current_parent_fops);
+ }
+#else
debugfs_create_file("clk_parent", 0444, root, core,
&current_parent_fops);
+#endif
+ }

- if (core->num_parents > 1)
+ if (core->num_parents > 1) {
debugfs_create_file("clk_possible_parents", 0444, root, core,
&possible_parents_fops);
+ }

if (core->ops->debug_init)
core->ops->debug_init(core->hw, core->dentry);
--
2.30.2