2 * Register map access API - debugfs
4 * Copyright 2011 Wolfson Microelectronics plc
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/slab.h>
14 #include <linux/mutex.h>
15 #include <linux/debugfs.h>
16 #include <linux/uaccess.h>
17 #include <linux/device.h>
21 static struct dentry *regmap_debugfs_root;
23 /* Calculate the length of a fixed format */
24 static size_t regmap_calc_reg_len(int max_val, char *buf, size_t buf_size)
26 snprintf(buf, buf_size, "%x", max_val);
30 static ssize_t regmap_name_read_file(struct file *file,
31 char __user *user_buf, size_t count,
34 struct regmap *map = file->private_data;
38 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
42 ret = snprintf(buf, PAGE_SIZE, "%s\n", map->dev->driver->name);
48 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
53 static const struct file_operations regmap_name_fops = {
55 .read = regmap_name_read_file,
56 .llseek = default_llseek,
59 static void regmap_debugfs_free_dump_cache(struct regmap *map)
61 struct regmap_debugfs_off_cache *c;
63 while (!list_empty(&map->debugfs_off_cache)) {
64 c = list_first_entry(&map->debugfs_off_cache,
65 struct regmap_debugfs_off_cache,
73 * Work out where the start offset maps into register numbers, bearing
74 * in mind that we suppress hidden registers.
76 static unsigned int regmap_debugfs_get_dump_start(struct regmap *map,
81 struct regmap_debugfs_off_cache *c = NULL;
86 * If we don't have a cache build one so we don't have to do a
87 * linear scan each time.
89 if (list_empty(&map->debugfs_off_cache)) {
90 for (i = base; i <= map->max_register; i += map->reg_stride) {
91 /* Skip unprinted registers, closing off cache entry */
92 if (!regmap_readable(map, i) ||
93 regmap_precious(map, i)) {
96 list_add_tail(&c->list,
97 &map->debugfs_off_cache);
104 /* No cache entry? Start a new one */
106 c = kzalloc(sizeof(*c), GFP_KERNEL);
108 regmap_debugfs_free_dump_cache(map);
115 p += map->debugfs_tot_len;
119 /* Close the last entry off if we didn't scan beyond it */
122 list_add_tail(&c->list,
123 &map->debugfs_off_cache);
129 * This should never happen; we return above if we fail to
130 * allocate and we should never be in this code if there are
131 * no registers at all.
133 if (list_empty(&map->debugfs_off_cache)) {
134 WARN_ON(list_empty(&map->debugfs_off_cache));
138 /* Find the relevant block */
139 list_for_each_entry(c, &map->debugfs_off_cache, list) {
140 if (from >= c->min && from <= c->max) {
152 static ssize_t regmap_read_debugfs(struct regmap *map, unsigned int from,
153 unsigned int to, char __user *user_buf,
154 size_t count, loff_t *ppos)
161 unsigned int val, start_reg;
163 if (*ppos < 0 || !count)
166 buf = kmalloc(count, GFP_KERNEL);
170 /* Calculate the length of a fixed format */
171 if (!map->debugfs_tot_len) {
172 map->debugfs_reg_len = regmap_calc_reg_len(map->max_register,
174 map->debugfs_val_len = 2 * map->format.val_bytes;
175 map->debugfs_tot_len = map->debugfs_reg_len +
176 map->debugfs_val_len + 3; /* : \n */
179 /* Work out which register we're starting at */
180 start_reg = regmap_debugfs_get_dump_start(map, from, *ppos, &p);
182 for (i = start_reg; i <= to; i += map->reg_stride) {
183 if (!regmap_readable(map, i))
186 if (regmap_precious(map, i))
189 /* If we're in the region the user is trying to read */
191 /* ...but not beyond it */
192 if (buf_pos + 1 + map->debugfs_tot_len >= count)
195 /* Format the register */
196 snprintf(buf + buf_pos, count - buf_pos, "%.*x: ",
197 map->debugfs_reg_len, i - from);
198 buf_pos += map->debugfs_reg_len + 2;
200 /* Format the value, write all X if we can't read */
201 ret = regmap_read(map, i, &val);
203 snprintf(buf + buf_pos, count - buf_pos,
204 "%.*x", map->debugfs_val_len, val);
206 memset(buf + buf_pos, 'X',
207 map->debugfs_val_len);
208 buf_pos += 2 * map->format.val_bytes;
210 buf[buf_pos++] = '\n';
212 p += map->debugfs_tot_len;
217 if (copy_to_user(user_buf, buf, buf_pos)) {
229 static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf,
230 size_t count, loff_t *ppos)
232 struct regmap *map = file->private_data;
234 return regmap_read_debugfs(map, 0, map->max_register, user_buf,
238 #undef REGMAP_ALLOW_WRITE_DEBUGFS
239 #ifdef REGMAP_ALLOW_WRITE_DEBUGFS
241 * This can be dangerous especially when we have clients such as
242 * PMICs, therefore don't provide any real compile time configuration option
243 * for this feature, people who want to use this will need to modify
244 * the source code directly.
246 static ssize_t regmap_map_write_file(struct file *file,
247 const char __user *user_buf,
248 size_t count, loff_t *ppos)
253 unsigned long reg, value;
254 struct regmap *map = file->private_data;
256 buf_size = min(count, (sizeof(buf)-1));
257 if (copy_from_user(buf, user_buf, buf_size))
261 while (*start == ' ')
263 reg = simple_strtoul(start, &start, 16);
264 while (*start == ' ')
266 if (strict_strtoul(start, 16, &value))
269 /* Userspace has been fiddling around behind the kernel's back */
270 add_taint(TAINT_USER);
272 regmap_write(map, reg, value);
276 #define regmap_map_write_file NULL
279 static const struct file_operations regmap_map_fops = {
281 .read = regmap_map_read_file,
282 .write = regmap_map_write_file,
283 .llseek = default_llseek,
286 static ssize_t regmap_range_read_file(struct file *file, char __user *user_buf,
287 size_t count, loff_t *ppos)
289 struct regmap_range_node *range = file->private_data;
290 struct regmap *map = range->map;
292 return regmap_read_debugfs(map, range->range_min, range->range_max,
293 user_buf, count, ppos);
296 static const struct file_operations regmap_range_fops = {
298 .read = regmap_range_read_file,
299 .llseek = default_llseek,
302 static ssize_t regmap_access_read_file(struct file *file,
303 char __user *user_buf, size_t count,
306 int reg_len, tot_len;
311 struct regmap *map = file->private_data;
314 if (*ppos < 0 || !count)
317 buf = kmalloc(count, GFP_KERNEL);
321 /* Calculate the length of a fixed format */
322 reg_len = regmap_calc_reg_len(map->max_register, buf, count);
323 tot_len = reg_len + 10; /* ': R W V P\n' */
325 for (i = 0; i <= map->max_register; i += map->reg_stride) {
326 /* Ignore registers which are neither readable nor writable */
327 if (!regmap_readable(map, i) && !regmap_writeable(map, i))
330 /* If we're in the region the user is trying to read */
332 /* ...but not beyond it */
333 if (buf_pos >= count - 1 - tot_len)
336 /* Format the register */
337 snprintf(buf + buf_pos, count - buf_pos,
338 "%.*x: %c %c %c %c\n",
340 regmap_readable(map, i) ? 'y' : 'n',
341 regmap_writeable(map, i) ? 'y' : 'n',
342 regmap_volatile(map, i) ? 'y' : 'n',
343 regmap_precious(map, i) ? 'y' : 'n');
352 if (copy_to_user(user_buf, buf, buf_pos)) {
364 static const struct file_operations regmap_access_fops = {
366 .read = regmap_access_read_file,
367 .llseek = default_llseek,
370 void regmap_debugfs_init(struct regmap *map, const char *name)
372 struct rb_node *next;
373 struct regmap_range_node *range_node;
375 INIT_LIST_HEAD(&map->debugfs_off_cache);
378 map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s",
379 dev_name(map->dev), name);
380 name = map->debugfs_name;
382 name = dev_name(map->dev);
385 map->debugfs = debugfs_create_dir(name, regmap_debugfs_root);
387 dev_warn(map->dev, "Failed to create debugfs directory\n");
391 debugfs_create_file("name", 0400, map->debugfs,
392 map, ®map_name_fops);
394 if (map->max_register) {
395 debugfs_create_file("registers", 0400, map->debugfs,
396 map, ®map_map_fops);
397 debugfs_create_file("access", 0400, map->debugfs,
398 map, ®map_access_fops);
401 if (map->cache_type) {
402 debugfs_create_bool("cache_only", 0400, map->debugfs,
404 debugfs_create_bool("cache_dirty", 0400, map->debugfs,
406 debugfs_create_bool("cache_bypass", 0400, map->debugfs,
410 next = rb_first(&map->range_tree);
412 range_node = rb_entry(next, struct regmap_range_node, node);
414 if (range_node->name)
415 debugfs_create_file(range_node->name, 0400,
416 map->debugfs, range_node,
419 next = rb_next(&range_node->node);
423 void regmap_debugfs_exit(struct regmap *map)
425 debugfs_remove_recursive(map->debugfs);
426 regmap_debugfs_free_dump_cache(map);
427 kfree(map->debugfs_name);
430 void regmap_debugfs_initcall(void)
432 regmap_debugfs_root = debugfs_create_dir("regmap", NULL);
433 if (!regmap_debugfs_root) {
434 pr_warn("regmap: Failed to create debugfs root\n");