1 /*
2  * Generic OPP debugfs interface
3  *
4  * Copyright (C) 2015-2016 Viresh Kumar <viresh.kumar@linaro.org>
5  *
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.
9  */
10 
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 
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>
19 
20 #include "opp.h"
21 
22 static struct dentry *rootdir;
23 
opp_set_dev_name(const struct device * dev,char * name)24 static void opp_set_dev_name(const struct device *dev, char *name)
25 {
26 	if (dev->parent)
27 		snprintf(name, NAME_MAX, "%s-%s", dev_name(dev->parent),
28 			 dev_name(dev));
29 	else
30 		snprintf(name, NAME_MAX, "%s", dev_name(dev));
31 }
32 
opp_debug_remove_one(struct dev_pm_opp * opp)33 void opp_debug_remove_one(struct dev_pm_opp *opp)
34 {
35 	debugfs_remove_recursive(opp->dentry);
36 }
37 
opp_debug_create_supplies(struct dev_pm_opp * opp,struct opp_table * opp_table,struct dentry * pdentry)38 static bool opp_debug_create_supplies(struct dev_pm_opp *opp,
39 				      struct opp_table *opp_table,
40 				      struct dentry *pdentry)
41 {
42 	struct dentry *d;
43 	int i;
44 
45 	for (i = 0; i < opp_table->regulator_count; i++) {
46 		char name[15];
47 
48 		snprintf(name, sizeof(name), "supply-%d", i);
49 
50 		/* Create per-opp directory */
51 		d = debugfs_create_dir(name, pdentry);
52 
53 		if (!d)
54 			return false;
55 
56 		if (!debugfs_create_ulong("u_volt_target", S_IRUGO, d,
57 					  &opp->supplies[i].u_volt))
58 			return false;
59 
60 		if (!debugfs_create_ulong("u_volt_min", S_IRUGO, d,
61 					  &opp->supplies[i].u_volt_min))
62 			return false;
63 
64 		if (!debugfs_create_ulong("u_volt_max", S_IRUGO, d,
65 					  &opp->supplies[i].u_volt_max))
66 			return false;
67 
68 		if (!debugfs_create_ulong("u_amp", S_IRUGO, d,
69 					  &opp->supplies[i].u_amp))
70 			return false;
71 	}
72 
73 	return true;
74 }
75 
opp_debug_create_one(struct dev_pm_opp * opp,struct opp_table * opp_table)76 int opp_debug_create_one(struct dev_pm_opp *opp, struct opp_table *opp_table)
77 {
78 	struct dentry *pdentry = opp_table->dentry;
79 	struct dentry *d;
80 	unsigned long id;
81 	char name[25];	/* 20 chars for 64 bit value + 5 (opp:\0) */
82 
83 	/*
84 	 * Get directory name for OPP.
85 	 *
86 	 * - Normally rate is unique to each OPP, use it to get unique opp-name.
87 	 * - For some devices rate isn't available, use index instead.
88 	 */
89 	if (likely(opp->rate))
90 		id = opp->rate;
91 	else
92 		id = _get_opp_count(opp_table);
93 
94 	snprintf(name, sizeof(name), "opp:%lu", id);
95 
96 	/* Create per-opp directory */
97 	d = debugfs_create_dir(name, pdentry);
98 	if (!d)
99 		return -ENOMEM;
100 
101 	if (!debugfs_create_bool("available", S_IRUGO, d, &opp->available))
102 		return -ENOMEM;
103 
104 	if (!debugfs_create_bool("dynamic", S_IRUGO, d, &opp->dynamic))
105 		return -ENOMEM;
106 
107 	if (!debugfs_create_bool("turbo", S_IRUGO, d, &opp->turbo))
108 		return -ENOMEM;
109 
110 	if (!debugfs_create_bool("suspend", S_IRUGO, d, &opp->suspend))
111 		return -ENOMEM;
112 
113 	if (!debugfs_create_u32("performance_state", S_IRUGO, d, &opp->pstate))
114 		return -ENOMEM;
115 
116 	if (!debugfs_create_ulong("rate_hz", S_IRUGO, d, &opp->rate))
117 		return -ENOMEM;
118 
119 	if (!opp_debug_create_supplies(opp, opp_table, d))
120 		return -ENOMEM;
121 
122 	if (!debugfs_create_ulong("clock_latency_ns", S_IRUGO, d,
123 				  &opp->clock_latency_ns))
124 		return -ENOMEM;
125 
126 	opp->dentry = d;
127 	return 0;
128 }
129 
opp_list_debug_create_dir(struct opp_device * opp_dev,struct opp_table * opp_table)130 static int opp_list_debug_create_dir(struct opp_device *opp_dev,
131 				     struct opp_table *opp_table)
132 {
133 	const struct device *dev = opp_dev->dev;
134 	struct dentry *d;
135 
136 	opp_set_dev_name(dev, opp_table->dentry_name);
137 
138 	/* Create device specific directory */
139 	d = debugfs_create_dir(opp_table->dentry_name, rootdir);
140 	if (!d) {
141 		dev_err(dev, "%s: Failed to create debugfs dir\n", __func__);
142 		return -ENOMEM;
143 	}
144 
145 	opp_dev->dentry = d;
146 	opp_table->dentry = d;
147 
148 	return 0;
149 }
150 
opp_list_debug_create_link(struct opp_device * opp_dev,struct opp_table * opp_table)151 static int opp_list_debug_create_link(struct opp_device *opp_dev,
152 				      struct opp_table *opp_table)
153 {
154 	const struct device *dev = opp_dev->dev;
155 	char name[NAME_MAX];
156 	struct dentry *d;
157 
158 	opp_set_dev_name(opp_dev->dev, name);
159 
160 	/* Create device specific directory link */
161 	d = debugfs_create_symlink(name, rootdir, opp_table->dentry_name);
162 	if (!d) {
163 		dev_err(dev, "%s: Failed to create link\n", __func__);
164 		return -ENOMEM;
165 	}
166 
167 	opp_dev->dentry = d;
168 
169 	return 0;
170 }
171 
172 /**
173  * opp_debug_register - add a device opp node to the debugfs 'opp' directory
174  * @opp_dev: opp-dev pointer for device
175  * @opp_table: the device-opp being added
176  *
177  * Dynamically adds device specific directory in debugfs 'opp' directory. If the
178  * device-opp is shared with other devices, then links will be created for all
179  * devices except the first.
180  *
181  * Return: 0 on success, otherwise negative error.
182  */
opp_debug_register(struct opp_device * opp_dev,struct opp_table * opp_table)183 int opp_debug_register(struct opp_device *opp_dev, struct opp_table *opp_table)
184 {
185 	if (!rootdir) {
186 		pr_debug("%s: Uninitialized rootdir\n", __func__);
187 		return -EINVAL;
188 	}
189 
190 	if (opp_table->dentry)
191 		return opp_list_debug_create_link(opp_dev, opp_table);
192 
193 	return opp_list_debug_create_dir(opp_dev, opp_table);
194 }
195 
opp_migrate_dentry(struct opp_device * opp_dev,struct opp_table * opp_table)196 static void opp_migrate_dentry(struct opp_device *opp_dev,
197 			       struct opp_table *opp_table)
198 {
199 	struct opp_device *new_dev;
200 	const struct device *dev;
201 	struct dentry *dentry;
202 
203 	/* Look for next opp-dev */
204 	list_for_each_entry(new_dev, &opp_table->dev_list, node)
205 		if (new_dev != opp_dev)
206 			break;
207 
208 	/* new_dev is guaranteed to be valid here */
209 	dev = new_dev->dev;
210 	debugfs_remove_recursive(new_dev->dentry);
211 
212 	opp_set_dev_name(dev, opp_table->dentry_name);
213 
214 	dentry = debugfs_rename(rootdir, opp_dev->dentry, rootdir,
215 				opp_table->dentry_name);
216 	if (!dentry) {
217 		dev_err(dev, "%s: Failed to rename link from: %s to %s\n",
218 			__func__, dev_name(opp_dev->dev), dev_name(dev));
219 		return;
220 	}
221 
222 	new_dev->dentry = dentry;
223 	opp_table->dentry = dentry;
224 }
225 
226 /**
227  * opp_debug_unregister - remove a device opp node from debugfs opp directory
228  * @opp_dev: opp-dev pointer for device
229  * @opp_table: the device-opp being removed
230  *
231  * Dynamically removes device specific directory from debugfs 'opp' directory.
232  */
opp_debug_unregister(struct opp_device * opp_dev,struct opp_table * opp_table)233 void opp_debug_unregister(struct opp_device *opp_dev,
234 			  struct opp_table *opp_table)
235 {
236 	if (opp_dev->dentry == opp_table->dentry) {
237 		/* Move the real dentry object under another device */
238 		if (!list_is_singular(&opp_table->dev_list)) {
239 			opp_migrate_dentry(opp_dev, opp_table);
240 			goto out;
241 		}
242 		opp_table->dentry = NULL;
243 	}
244 
245 	debugfs_remove_recursive(opp_dev->dentry);
246 
247 out:
248 	opp_dev->dentry = NULL;
249 }
250 
opp_debug_init(void)251 static int __init opp_debug_init(void)
252 {
253 	/* Create /sys/kernel/debug/opp directory */
254 	rootdir = debugfs_create_dir("opp", NULL);
255 	if (!rootdir) {
256 		pr_err("%s: Failed to create root directory\n", __func__);
257 		return -ENOMEM;
258 	}
259 
260 	return 0;
261 }
262 core_initcall(opp_debug_init);
263