1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27813dd6fSViresh Kumar /*
37813dd6fSViresh Kumar * Generic OPP debugfs interface
47813dd6fSViresh Kumar *
57813dd6fSViresh Kumar * Copyright (C) 2015-2016 Viresh Kumar <viresh.kumar@linaro.org>
67813dd6fSViresh Kumar */
77813dd6fSViresh Kumar
87813dd6fSViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
97813dd6fSViresh Kumar
107813dd6fSViresh Kumar #include <linux/debugfs.h>
117813dd6fSViresh Kumar #include <linux/device.h>
127813dd6fSViresh Kumar #include <linux/err.h>
13021dbecaSViresh Kumar #include <linux/of.h>
147813dd6fSViresh Kumar #include <linux/init.h>
157813dd6fSViresh Kumar #include <linux/limits.h>
167813dd6fSViresh Kumar #include <linux/slab.h>
177813dd6fSViresh Kumar
187813dd6fSViresh Kumar #include "opp.h"
197813dd6fSViresh Kumar
207813dd6fSViresh Kumar static struct dentry *rootdir;
217813dd6fSViresh Kumar
opp_set_dev_name(const struct device * dev,char * name)227813dd6fSViresh Kumar static void opp_set_dev_name(const struct device *dev, char *name)
237813dd6fSViresh Kumar {
247813dd6fSViresh Kumar if (dev->parent)
257813dd6fSViresh Kumar snprintf(name, NAME_MAX, "%s-%s", dev_name(dev->parent),
267813dd6fSViresh Kumar dev_name(dev));
277813dd6fSViresh Kumar else
287813dd6fSViresh Kumar snprintf(name, NAME_MAX, "%s", dev_name(dev));
297813dd6fSViresh Kumar }
307813dd6fSViresh Kumar
opp_debug_remove_one(struct dev_pm_opp * opp)317813dd6fSViresh Kumar void opp_debug_remove_one(struct dev_pm_opp *opp)
327813dd6fSViresh Kumar {
337813dd6fSViresh Kumar debugfs_remove_recursive(opp->dentry);
347813dd6fSViresh Kumar }
357813dd6fSViresh Kumar
bw_name_read(struct file * fp,char __user * userbuf,size_t count,loff_t * ppos)360430b1d5SViresh Kumar static ssize_t bw_name_read(struct file *fp, char __user *userbuf,
370430b1d5SViresh Kumar size_t count, loff_t *ppos)
380430b1d5SViresh Kumar {
390430b1d5SViresh Kumar struct icc_path *path = fp->private_data;
40*b64a1b4fSViresh Kumar const char *name = icc_get_name(path);
410430b1d5SViresh Kumar char buf[64];
42*b64a1b4fSViresh Kumar int i = 0;
430430b1d5SViresh Kumar
44*b64a1b4fSViresh Kumar if (name)
45*b64a1b4fSViresh Kumar i = scnprintf(buf, sizeof(buf), "%.62s\n", name);
460430b1d5SViresh Kumar
470430b1d5SViresh Kumar return simple_read_from_buffer(userbuf, count, ppos, buf, i);
480430b1d5SViresh Kumar }
490430b1d5SViresh Kumar
500430b1d5SViresh Kumar static const struct file_operations bw_name_fops = {
510430b1d5SViresh Kumar .open = simple_open,
520430b1d5SViresh Kumar .read = bw_name_read,
530430b1d5SViresh Kumar .llseek = default_llseek,
540430b1d5SViresh Kumar };
550430b1d5SViresh Kumar
opp_debug_create_bw(struct dev_pm_opp * opp,struct opp_table * opp_table,struct dentry * pdentry)560430b1d5SViresh Kumar static void opp_debug_create_bw(struct dev_pm_opp *opp,
570430b1d5SViresh Kumar struct opp_table *opp_table,
580430b1d5SViresh Kumar struct dentry *pdentry)
590430b1d5SViresh Kumar {
600430b1d5SViresh Kumar struct dentry *d;
610430b1d5SViresh Kumar char name[11];
620430b1d5SViresh Kumar int i;
630430b1d5SViresh Kumar
640430b1d5SViresh Kumar for (i = 0; i < opp_table->path_count; i++) {
650430b1d5SViresh Kumar snprintf(name, sizeof(name), "icc-path-%.1d", i);
660430b1d5SViresh Kumar
670430b1d5SViresh Kumar /* Create per-path directory */
680430b1d5SViresh Kumar d = debugfs_create_dir(name, pdentry);
690430b1d5SViresh Kumar
700430b1d5SViresh Kumar debugfs_create_file("name", S_IRUGO, d, opp_table->paths[i],
710430b1d5SViresh Kumar &bw_name_fops);
720430b1d5SViresh Kumar debugfs_create_u32("peak_bw", S_IRUGO, d,
730430b1d5SViresh Kumar &opp->bandwidth[i].peak);
740430b1d5SViresh Kumar debugfs_create_u32("avg_bw", S_IRUGO, d,
750430b1d5SViresh Kumar &opp->bandwidth[i].avg);
760430b1d5SViresh Kumar }
770430b1d5SViresh Kumar }
780430b1d5SViresh Kumar
opp_debug_create_clks(struct dev_pm_opp * opp,struct opp_table * opp_table,struct dentry * pdentry)792083da24SViresh Kumar static void opp_debug_create_clks(struct dev_pm_opp *opp,
802083da24SViresh Kumar struct opp_table *opp_table,
812083da24SViresh Kumar struct dentry *pdentry)
822083da24SViresh Kumar {
832083da24SViresh Kumar char name[12];
842083da24SViresh Kumar int i;
852083da24SViresh Kumar
862083da24SViresh Kumar if (opp_table->clk_count == 1) {
872083da24SViresh Kumar debugfs_create_ulong("rate_hz", S_IRUGO, pdentry, &opp->rates[0]);
882083da24SViresh Kumar return;
892083da24SViresh Kumar }
902083da24SViresh Kumar
912083da24SViresh Kumar for (i = 0; i < opp_table->clk_count; i++) {
922083da24SViresh Kumar snprintf(name, sizeof(name), "rate_hz_%d", i);
932083da24SViresh Kumar debugfs_create_ulong(name, S_IRUGO, pdentry, &opp->rates[i]);
942083da24SViresh Kumar }
952083da24SViresh Kumar }
962083da24SViresh Kumar
opp_debug_create_supplies(struct dev_pm_opp * opp,struct opp_table * opp_table,struct dentry * pdentry)97a2dea4cbSGreg Kroah-Hartman static void opp_debug_create_supplies(struct dev_pm_opp *opp,
987813dd6fSViresh Kumar struct opp_table *opp_table,
997813dd6fSViresh Kumar struct dentry *pdentry)
1007813dd6fSViresh Kumar {
1017813dd6fSViresh Kumar struct dentry *d;
1027813dd6fSViresh Kumar int i;
1037813dd6fSViresh Kumar
1047813dd6fSViresh Kumar for (i = 0; i < opp_table->regulator_count; i++) {
105d741029aSArvind Yadav char name[15];
106d741029aSArvind Yadav
107d741029aSArvind Yadav snprintf(name, sizeof(name), "supply-%d", i);
1087813dd6fSViresh Kumar
1097813dd6fSViresh Kumar /* Create per-opp directory */
1107813dd6fSViresh Kumar d = debugfs_create_dir(name, pdentry);
1117813dd6fSViresh Kumar
112a2dea4cbSGreg Kroah-Hartman debugfs_create_ulong("u_volt_target", S_IRUGO, d,
113a2dea4cbSGreg Kroah-Hartman &opp->supplies[i].u_volt);
1147813dd6fSViresh Kumar
115a2dea4cbSGreg Kroah-Hartman debugfs_create_ulong("u_volt_min", S_IRUGO, d,
116a2dea4cbSGreg Kroah-Hartman &opp->supplies[i].u_volt_min);
1177813dd6fSViresh Kumar
118a2dea4cbSGreg Kroah-Hartman debugfs_create_ulong("u_volt_max", S_IRUGO, d,
119a2dea4cbSGreg Kroah-Hartman &opp->supplies[i].u_volt_max);
1207813dd6fSViresh Kumar
121a2dea4cbSGreg Kroah-Hartman debugfs_create_ulong("u_amp", S_IRUGO, d,
122a2dea4cbSGreg Kroah-Hartman &opp->supplies[i].u_amp);
1234f9a7a1dSLukasz Luba
1244f9a7a1dSLukasz Luba debugfs_create_ulong("u_watt", S_IRUGO, d,
1254f9a7a1dSLukasz Luba &opp->supplies[i].u_watt);
126a2dea4cbSGreg Kroah-Hartman }
1277813dd6fSViresh Kumar }
1287813dd6fSViresh Kumar
opp_debug_create_one(struct dev_pm_opp * opp,struct opp_table * opp_table)129a2dea4cbSGreg Kroah-Hartman void opp_debug_create_one(struct dev_pm_opp *opp, struct opp_table *opp_table)
1307813dd6fSViresh Kumar {
1317813dd6fSViresh Kumar struct dentry *pdentry = opp_table->dentry;
1327813dd6fSViresh Kumar struct dentry *d;
133a1e8c136SViresh Kumar unsigned long id;
1347813dd6fSViresh Kumar char name[25]; /* 20 chars for 64 bit value + 5 (opp:\0) */
1357813dd6fSViresh Kumar
136a1e8c136SViresh Kumar /*
137a1e8c136SViresh Kumar * Get directory name for OPP.
138a1e8c136SViresh Kumar *
139a1e8c136SViresh Kumar * - Normally rate is unique to each OPP, use it to get unique opp-name.
1402083da24SViresh Kumar * - For some devices rate isn't available or there are multiple, use
1412083da24SViresh Kumar * index instead for them.
142a1e8c136SViresh Kumar */
1432083da24SViresh Kumar if (likely(opp_table->clk_count == 1 && opp->rates[0]))
1442083da24SViresh Kumar id = opp->rates[0];
145a1e8c136SViresh Kumar else
146a1e8c136SViresh Kumar id = _get_opp_count(opp_table);
147a1e8c136SViresh Kumar
148a1e8c136SViresh Kumar snprintf(name, sizeof(name), "opp:%lu", id);
1497813dd6fSViresh Kumar
1507813dd6fSViresh Kumar /* Create per-opp directory */
1517813dd6fSViresh Kumar d = debugfs_create_dir(name, pdentry);
1527813dd6fSViresh Kumar
153a2dea4cbSGreg Kroah-Hartman debugfs_create_bool("available", S_IRUGO, d, &opp->available);
154a2dea4cbSGreg Kroah-Hartman debugfs_create_bool("dynamic", S_IRUGO, d, &opp->dynamic);
155a2dea4cbSGreg Kroah-Hartman debugfs_create_bool("turbo", S_IRUGO, d, &opp->turbo);
156a2dea4cbSGreg Kroah-Hartman debugfs_create_bool("suspend", S_IRUGO, d, &opp->suspend);
157021dbecaSViresh Kumar debugfs_create_u32("level", S_IRUGO, d, &opp->level);
158a2dea4cbSGreg Kroah-Hartman debugfs_create_ulong("clock_latency_ns", S_IRUGO, d,
159a2dea4cbSGreg Kroah-Hartman &opp->clock_latency_ns);
1607813dd6fSViresh Kumar
161021dbecaSViresh Kumar opp->of_name = of_node_full_name(opp->np);
162021dbecaSViresh Kumar debugfs_create_str("of_name", S_IRUGO, d, (char **)&opp->of_name);
163021dbecaSViresh Kumar
1642083da24SViresh Kumar opp_debug_create_clks(opp, opp_table, d);
165a2dea4cbSGreg Kroah-Hartman opp_debug_create_supplies(opp, opp_table, d);
1660430b1d5SViresh Kumar opp_debug_create_bw(opp, opp_table, d);
1677813dd6fSViresh Kumar
1687813dd6fSViresh Kumar opp->dentry = d;
1697813dd6fSViresh Kumar }
1707813dd6fSViresh Kumar
opp_list_debug_create_dir(struct opp_device * opp_dev,struct opp_table * opp_table)171a2dea4cbSGreg Kroah-Hartman static void opp_list_debug_create_dir(struct opp_device *opp_dev,
1727813dd6fSViresh Kumar struct opp_table *opp_table)
1737813dd6fSViresh Kumar {
1747813dd6fSViresh Kumar const struct device *dev = opp_dev->dev;
1757813dd6fSViresh Kumar struct dentry *d;
1767813dd6fSViresh Kumar
1777813dd6fSViresh Kumar opp_set_dev_name(dev, opp_table->dentry_name);
1787813dd6fSViresh Kumar
1797813dd6fSViresh Kumar /* Create device specific directory */
1807813dd6fSViresh Kumar d = debugfs_create_dir(opp_table->dentry_name, rootdir);
1817813dd6fSViresh Kumar
1827813dd6fSViresh Kumar opp_dev->dentry = d;
1837813dd6fSViresh Kumar opp_table->dentry = d;
1847813dd6fSViresh Kumar }
1857813dd6fSViresh Kumar
opp_list_debug_create_link(struct opp_device * opp_dev,struct opp_table * opp_table)186a2dea4cbSGreg Kroah-Hartman static void opp_list_debug_create_link(struct opp_device *opp_dev,
1877813dd6fSViresh Kumar struct opp_table *opp_table)
1887813dd6fSViresh Kumar {
1897813dd6fSViresh Kumar char name[NAME_MAX];
1907813dd6fSViresh Kumar
1917813dd6fSViresh Kumar opp_set_dev_name(opp_dev->dev, name);
1927813dd6fSViresh Kumar
1937813dd6fSViresh Kumar /* Create device specific directory link */
194a2dea4cbSGreg Kroah-Hartman opp_dev->dentry = debugfs_create_symlink(name, rootdir,
195a2dea4cbSGreg Kroah-Hartman opp_table->dentry_name);
1967813dd6fSViresh Kumar }
1977813dd6fSViresh Kumar
1987813dd6fSViresh Kumar /**
1997813dd6fSViresh Kumar * opp_debug_register - add a device opp node to the debugfs 'opp' directory
2007813dd6fSViresh Kumar * @opp_dev: opp-dev pointer for device
2017813dd6fSViresh Kumar * @opp_table: the device-opp being added
2027813dd6fSViresh Kumar *
2037813dd6fSViresh Kumar * Dynamically adds device specific directory in debugfs 'opp' directory. If the
2047813dd6fSViresh Kumar * device-opp is shared with other devices, then links will be created for all
2057813dd6fSViresh Kumar * devices except the first.
2067813dd6fSViresh Kumar */
opp_debug_register(struct opp_device * opp_dev,struct opp_table * opp_table)207a2dea4cbSGreg Kroah-Hartman void opp_debug_register(struct opp_device *opp_dev, struct opp_table *opp_table)
2087813dd6fSViresh Kumar {
2097813dd6fSViresh Kumar if (opp_table->dentry)
210a2dea4cbSGreg Kroah-Hartman opp_list_debug_create_link(opp_dev, opp_table);
211a2dea4cbSGreg Kroah-Hartman else
212a2dea4cbSGreg Kroah-Hartman opp_list_debug_create_dir(opp_dev, opp_table);
2137813dd6fSViresh Kumar }
2147813dd6fSViresh Kumar
opp_migrate_dentry(struct opp_device * opp_dev,struct opp_table * opp_table)2157813dd6fSViresh Kumar static void opp_migrate_dentry(struct opp_device *opp_dev,
2167813dd6fSViresh Kumar struct opp_table *opp_table)
2177813dd6fSViresh Kumar {
218c14faabfSXiaomeng Tong struct opp_device *new_dev = NULL, *iter;
2197813dd6fSViresh Kumar const struct device *dev;
2207813dd6fSViresh Kumar struct dentry *dentry;
2217813dd6fSViresh Kumar
2227813dd6fSViresh Kumar /* Look for next opp-dev */
223c14faabfSXiaomeng Tong list_for_each_entry(iter, &opp_table->dev_list, node)
224c14faabfSXiaomeng Tong if (iter != opp_dev) {
225c14faabfSXiaomeng Tong new_dev = iter;
2267813dd6fSViresh Kumar break;
227c14faabfSXiaomeng Tong }
228c14faabfSXiaomeng Tong
229c14faabfSXiaomeng Tong BUG_ON(!new_dev);
2307813dd6fSViresh Kumar
2317813dd6fSViresh Kumar /* new_dev is guaranteed to be valid here */
2327813dd6fSViresh Kumar dev = new_dev->dev;
2337813dd6fSViresh Kumar debugfs_remove_recursive(new_dev->dentry);
2347813dd6fSViresh Kumar
2357813dd6fSViresh Kumar opp_set_dev_name(dev, opp_table->dentry_name);
2367813dd6fSViresh Kumar
2377813dd6fSViresh Kumar dentry = debugfs_rename(rootdir, opp_dev->dentry, rootdir,
2387813dd6fSViresh Kumar opp_table->dentry_name);
239eca4c0eeSQi Zheng if (IS_ERR(dentry)) {
2407813dd6fSViresh Kumar dev_err(dev, "%s: Failed to rename link from: %s to %s\n",
2417813dd6fSViresh Kumar __func__, dev_name(opp_dev->dev), dev_name(dev));
2427813dd6fSViresh Kumar return;
2437813dd6fSViresh Kumar }
2447813dd6fSViresh Kumar
2457813dd6fSViresh Kumar new_dev->dentry = dentry;
2467813dd6fSViresh Kumar opp_table->dentry = dentry;
2477813dd6fSViresh Kumar }
2487813dd6fSViresh Kumar
2497813dd6fSViresh Kumar /**
2507813dd6fSViresh Kumar * opp_debug_unregister - remove a device opp node from debugfs opp directory
2517813dd6fSViresh Kumar * @opp_dev: opp-dev pointer for device
2527813dd6fSViresh Kumar * @opp_table: the device-opp being removed
2537813dd6fSViresh Kumar *
2547813dd6fSViresh Kumar * Dynamically removes device specific directory from debugfs 'opp' directory.
2557813dd6fSViresh Kumar */
opp_debug_unregister(struct opp_device * opp_dev,struct opp_table * opp_table)2567813dd6fSViresh Kumar void opp_debug_unregister(struct opp_device *opp_dev,
2577813dd6fSViresh Kumar struct opp_table *opp_table)
2587813dd6fSViresh Kumar {
2597813dd6fSViresh Kumar if (opp_dev->dentry == opp_table->dentry) {
2607813dd6fSViresh Kumar /* Move the real dentry object under another device */
2617813dd6fSViresh Kumar if (!list_is_singular(&opp_table->dev_list)) {
2627813dd6fSViresh Kumar opp_migrate_dentry(opp_dev, opp_table);
2637813dd6fSViresh Kumar goto out;
2647813dd6fSViresh Kumar }
2657813dd6fSViresh Kumar opp_table->dentry = NULL;
2667813dd6fSViresh Kumar }
2677813dd6fSViresh Kumar
2687813dd6fSViresh Kumar debugfs_remove_recursive(opp_dev->dentry);
2697813dd6fSViresh Kumar
2707813dd6fSViresh Kumar out:
2717813dd6fSViresh Kumar opp_dev->dentry = NULL;
2727813dd6fSViresh Kumar }
2737813dd6fSViresh Kumar
opp_debug_init(void)2747813dd6fSViresh Kumar static int __init opp_debug_init(void)
2757813dd6fSViresh Kumar {
2767813dd6fSViresh Kumar /* Create /sys/kernel/debug/opp directory */
2777813dd6fSViresh Kumar rootdir = debugfs_create_dir("opp", NULL);
2787813dd6fSViresh Kumar
2797813dd6fSViresh Kumar return 0;
2807813dd6fSViresh Kumar }
2817813dd6fSViresh Kumar core_initcall(opp_debug_init);
282