2 * Generic OPP debugfs interface
4 * Copyright (C) 2015-2016 Viresh Kumar <viresh.kumar@linaro.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/debugfs.h>
14 #include <linux/device.h>
15 #include <linux/err.h>
16 #include <linux/init.h>
17 #include <linux/limits.h>
18 #include <linux/slab.h>
22 static struct dentry *rootdir;
24 static void opp_set_dev_name(const struct device *dev, char *name)
27 snprintf(name, NAME_MAX, "%s-%s", dev_name(dev->parent),
30 snprintf(name, NAME_MAX, "%s", dev_name(dev));
33 void opp_debug_remove_one(struct dev_pm_opp *opp)
35 debugfs_remove_recursive(opp->dentry);
38 static bool opp_debug_create_supplies(struct dev_pm_opp *opp,
39 struct opp_table *opp_table,
40 struct dentry *pdentry)
46 for (i = 0; i < opp_table->regulator_count; i++) {
47 name = kasprintf(GFP_KERNEL, "supply-%d", i);
49 /* Create per-opp directory */
50 d = debugfs_create_dir(name, pdentry);
57 if (!debugfs_create_ulong("u_volt_target", S_IRUGO, d,
58 &opp->supplies[i].u_volt))
61 if (!debugfs_create_ulong("u_volt_min", S_IRUGO, d,
62 &opp->supplies[i].u_volt_min))
65 if (!debugfs_create_ulong("u_volt_max", S_IRUGO, d,
66 &opp->supplies[i].u_volt_max))
69 if (!debugfs_create_ulong("u_amp", S_IRUGO, d,
70 &opp->supplies[i].u_amp))
77 int opp_debug_create_one(struct dev_pm_opp *opp, struct opp_table *opp_table)
79 struct dentry *pdentry = opp_table->dentry;
81 char name[25]; /* 20 chars for 64 bit value + 5 (opp:\0) */
83 /* Rate is unique to each OPP, use it to give opp-name */
84 snprintf(name, sizeof(name), "opp:%lu", opp->rate);
86 /* Create per-opp directory */
87 d = debugfs_create_dir(name, pdentry);
91 if (!debugfs_create_bool("available", S_IRUGO, d, &opp->available))
94 if (!debugfs_create_bool("dynamic", S_IRUGO, d, &opp->dynamic))
97 if (!debugfs_create_bool("turbo", S_IRUGO, d, &opp->turbo))
100 if (!debugfs_create_bool("suspend", S_IRUGO, d, &opp->suspend))
103 if (!debugfs_create_ulong("rate_hz", S_IRUGO, d, &opp->rate))
106 if (!opp_debug_create_supplies(opp, opp_table, d))
109 if (!debugfs_create_ulong("clock_latency_ns", S_IRUGO, d,
110 &opp->clock_latency_ns))
117 static int opp_list_debug_create_dir(struct opp_device *opp_dev,
118 struct opp_table *opp_table)
120 const struct device *dev = opp_dev->dev;
123 opp_set_dev_name(dev, opp_table->dentry_name);
125 /* Create device specific directory */
126 d = debugfs_create_dir(opp_table->dentry_name, rootdir);
128 dev_err(dev, "%s: Failed to create debugfs dir\n", __func__);
133 opp_table->dentry = d;
138 static int opp_list_debug_create_link(struct opp_device *opp_dev,
139 struct opp_table *opp_table)
141 const struct device *dev = opp_dev->dev;
145 opp_set_dev_name(opp_dev->dev, name);
147 /* Create device specific directory link */
148 d = debugfs_create_symlink(name, rootdir, opp_table->dentry_name);
150 dev_err(dev, "%s: Failed to create link\n", __func__);
160 * opp_debug_register - add a device opp node to the debugfs 'opp' directory
161 * @opp_dev: opp-dev pointer for device
162 * @opp_table: the device-opp being added
164 * Dynamically adds device specific directory in debugfs 'opp' directory. If the
165 * device-opp is shared with other devices, then links will be created for all
166 * devices except the first.
168 * Return: 0 on success, otherwise negative error.
170 int opp_debug_register(struct opp_device *opp_dev, struct opp_table *opp_table)
173 pr_debug("%s: Uninitialized rootdir\n", __func__);
177 if (opp_table->dentry)
178 return opp_list_debug_create_link(opp_dev, opp_table);
180 return opp_list_debug_create_dir(opp_dev, opp_table);
183 static void opp_migrate_dentry(struct opp_device *opp_dev,
184 struct opp_table *opp_table)
186 struct opp_device *new_dev;
187 const struct device *dev;
188 struct dentry *dentry;
190 /* Look for next opp-dev */
191 list_for_each_entry(new_dev, &opp_table->dev_list, node)
192 if (new_dev != opp_dev)
195 /* new_dev is guaranteed to be valid here */
197 debugfs_remove_recursive(new_dev->dentry);
199 opp_set_dev_name(dev, opp_table->dentry_name);
201 dentry = debugfs_rename(rootdir, opp_dev->dentry, rootdir,
202 opp_table->dentry_name);
204 dev_err(dev, "%s: Failed to rename link from: %s to %s\n",
205 __func__, dev_name(opp_dev->dev), dev_name(dev));
209 new_dev->dentry = dentry;
210 opp_table->dentry = dentry;
214 * opp_debug_unregister - remove a device opp node from debugfs opp directory
215 * @opp_dev: opp-dev pointer for device
216 * @opp_table: the device-opp being removed
218 * Dynamically removes device specific directory from debugfs 'opp' directory.
220 void opp_debug_unregister(struct opp_device *opp_dev,
221 struct opp_table *opp_table)
223 if (opp_dev->dentry == opp_table->dentry) {
224 /* Move the real dentry object under another device */
225 if (!list_is_singular(&opp_table->dev_list)) {
226 opp_migrate_dentry(opp_dev, opp_table);
229 opp_table->dentry = NULL;
232 debugfs_remove_recursive(opp_dev->dentry);
235 opp_dev->dentry = NULL;
238 static int __init opp_debug_init(void)
240 /* Create /sys/kernel/debug/opp directory */
241 rootdir = debugfs_create_dir("opp", NULL);
243 pr_err("%s: Failed to create root directory\n", __func__);
249 core_initcall(opp_debug_init);