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>
18 #include <linux/list.h>
22 struct regmap_debugfs_node {
25 struct list_head link;
28 static struct dentry *regmap_debugfs_root;
29 static LIST_HEAD(regmap_debugfs_early_list);
30 static DEFINE_MUTEX(regmap_debugfs_early_lock);
32 /* Calculate the length of a fixed format */
33 static size_t regmap_calc_reg_len(int max_val, char *buf, size_t buf_size)
35 return snprintf(NULL, 0, "%x", max_val);
38 static ssize_t regmap_name_read_file(struct file *file,
39 char __user *user_buf, size_t count,
42 struct regmap *map = file->private_data;
46 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
50 ret = snprintf(buf, PAGE_SIZE, "%s\n", map->dev->driver->name);
56 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
61 static const struct file_operations regmap_name_fops = {
63 .read = regmap_name_read_file,
64 .llseek = default_llseek,
67 static void regmap_debugfs_free_dump_cache(struct regmap *map)
69 struct regmap_debugfs_off_cache *c;
71 while (!list_empty(&map->debugfs_off_cache)) {
72 c = list_first_entry(&map->debugfs_off_cache,
73 struct regmap_debugfs_off_cache,
81 * Work out where the start offset maps into register numbers, bearing
82 * in mind that we suppress hidden registers.
84 static unsigned int regmap_debugfs_get_dump_start(struct regmap *map,
89 struct regmap_debugfs_off_cache *c = NULL;
92 unsigned int fpos_offset;
93 unsigned int reg_offset;
95 /* Suppress the cache if we're using a subrange */
100 * If we don't have a cache build one so we don't have to do a
101 * linear scan each time.
103 mutex_lock(&map->cache_lock);
105 if (list_empty(&map->debugfs_off_cache)) {
106 for (; i <= map->max_register; i += map->reg_stride) {
107 /* Skip unprinted registers, closing off cache entry */
108 if (!regmap_readable(map, i) ||
109 regmap_precious(map, i)) {
112 c->max_reg = i - map->reg_stride;
113 list_add_tail(&c->list,
114 &map->debugfs_off_cache);
121 /* No cache entry? Start a new one */
123 c = kzalloc(sizeof(*c), GFP_KERNEL);
125 regmap_debugfs_free_dump_cache(map);
126 mutex_unlock(&map->cache_lock);
133 p += map->debugfs_tot_len;
137 /* Close the last entry off if we didn't scan beyond it */
140 c->max_reg = i - map->reg_stride;
141 list_add_tail(&c->list,
142 &map->debugfs_off_cache);
146 * This should never happen; we return above if we fail to
147 * allocate and we should never be in this code if there are
148 * no registers at all.
150 WARN_ON(list_empty(&map->debugfs_off_cache));
153 /* Find the relevant block:offset */
154 list_for_each_entry(c, &map->debugfs_off_cache, list) {
155 if (from >= c->min && from <= c->max) {
156 fpos_offset = from - c->min;
157 reg_offset = fpos_offset / map->debugfs_tot_len;
158 *pos = c->min + (reg_offset * map->debugfs_tot_len);
159 mutex_unlock(&map->cache_lock);
160 return c->base_reg + (reg_offset * map->reg_stride);
166 mutex_unlock(&map->cache_lock);
171 static inline void regmap_calc_tot_len(struct regmap *map,
172 void *buf, size_t count)
174 /* Calculate the length of a fixed format */
175 if (!map->debugfs_tot_len) {
176 map->debugfs_reg_len = regmap_calc_reg_len(map->max_register,
178 map->debugfs_val_len = 2 * map->format.val_bytes;
179 map->debugfs_tot_len = map->debugfs_reg_len +
180 map->debugfs_val_len + 3; /* : \n */
184 static ssize_t regmap_read_debugfs(struct regmap *map, unsigned int from,
185 unsigned int to, char __user *user_buf,
186 size_t count, loff_t *ppos)
193 unsigned int val, start_reg;
195 if (*ppos < 0 || !count)
198 buf = kmalloc(count, GFP_KERNEL);
202 regmap_calc_tot_len(map, buf, count);
204 /* Work out which register we're starting at */
205 start_reg = regmap_debugfs_get_dump_start(map, from, *ppos, &p);
207 for (i = start_reg; i <= to; i += map->reg_stride) {
208 if (!regmap_readable(map, i))
211 if (regmap_precious(map, i))
214 /* If we're in the region the user is trying to read */
216 /* ...but not beyond it */
217 if (buf_pos + map->debugfs_tot_len > count)
220 /* Format the register */
221 snprintf(buf + buf_pos, count - buf_pos, "%.*x: ",
222 map->debugfs_reg_len, i - from);
223 buf_pos += map->debugfs_reg_len + 2;
225 /* Format the value, write all X if we can't read */
226 ret = regmap_read(map, i, &val);
228 snprintf(buf + buf_pos, count - buf_pos,
229 "%.*x", map->debugfs_val_len, val);
231 memset(buf + buf_pos, 'X',
232 map->debugfs_val_len);
233 buf_pos += 2 * map->format.val_bytes;
235 buf[buf_pos++] = '\n';
237 p += map->debugfs_tot_len;
242 if (copy_to_user(user_buf, buf, buf_pos)) {
254 static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf,
255 size_t count, loff_t *ppos)
257 struct regmap *map = file->private_data;
259 return regmap_read_debugfs(map, 0, map->max_register, user_buf,
263 #undef REGMAP_ALLOW_WRITE_DEBUGFS
264 #ifdef REGMAP_ALLOW_WRITE_DEBUGFS
266 * This can be dangerous especially when we have clients such as
267 * PMICs, therefore don't provide any real compile time configuration option
268 * for this feature, people who want to use this will need to modify
269 * the source code directly.
271 static ssize_t regmap_map_write_file(struct file *file,
272 const char __user *user_buf,
273 size_t count, loff_t *ppos)
278 unsigned long reg, value;
279 struct regmap *map = file->private_data;
282 buf_size = min(count, (sizeof(buf)-1));
283 if (copy_from_user(buf, user_buf, buf_size))
287 while (*start == ' ')
289 reg = simple_strtoul(start, &start, 16);
290 while (*start == ' ')
292 if (kstrtoul(start, 16, &value))
295 /* Userspace has been fiddling around behind the kernel's back */
296 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
298 ret = regmap_write(map, reg, value);
304 #define regmap_map_write_file NULL
307 static const struct file_operations regmap_map_fops = {
309 .read = regmap_map_read_file,
310 .write = regmap_map_write_file,
311 .llseek = default_llseek,
314 static ssize_t regmap_range_read_file(struct file *file, char __user *user_buf,
315 size_t count, loff_t *ppos)
317 struct regmap_range_node *range = file->private_data;
318 struct regmap *map = range->map;
320 return regmap_read_debugfs(map, range->range_min, range->range_max,
321 user_buf, count, ppos);
324 static const struct file_operations regmap_range_fops = {
326 .read = regmap_range_read_file,
327 .llseek = default_llseek,
330 static ssize_t regmap_reg_ranges_read_file(struct file *file,
331 char __user *user_buf, size_t count,
334 struct regmap *map = file->private_data;
335 struct regmap_debugfs_off_cache *c;
342 if (*ppos < 0 || !count)
345 buf = kmalloc(count, GFP_KERNEL);
349 entry = kmalloc(PAGE_SIZE, GFP_KERNEL);
355 /* While we are at it, build the register dump cache
356 * now so the read() operation on the `registers' file
357 * can benefit from using the cache. We do not care
358 * about the file position information that is contained
359 * in the cache, just about the actual register blocks */
360 regmap_calc_tot_len(map, buf, count);
361 regmap_debugfs_get_dump_start(map, 0, *ppos, &p);
363 /* Reset file pointer as the fixed-format of the `registers'
364 * file is not compatible with the `range' file */
366 mutex_lock(&map->cache_lock);
367 list_for_each_entry(c, &map->debugfs_off_cache, list) {
368 snprintf(entry, PAGE_SIZE, "%x-%x",
369 c->base_reg, c->max_reg);
371 if (buf_pos + 1 + strlen(entry) > count)
373 snprintf(buf + buf_pos, count - buf_pos,
375 buf_pos += strlen(entry);
379 p += strlen(entry) + 1;
381 mutex_unlock(&map->cache_lock);
386 if (copy_to_user(user_buf, buf, buf_pos)) {
397 static const struct file_operations regmap_reg_ranges_fops = {
399 .read = regmap_reg_ranges_read_file,
400 .llseek = default_llseek,
403 static ssize_t regmap_access_read_file(struct file *file,
404 char __user *user_buf, size_t count,
407 int reg_len, tot_len;
412 struct regmap *map = file->private_data;
415 if (*ppos < 0 || !count)
418 buf = kmalloc(count, GFP_KERNEL);
422 /* Calculate the length of a fixed format */
423 reg_len = regmap_calc_reg_len(map->max_register, buf, count);
424 tot_len = reg_len + 10; /* ': R W V P\n' */
426 for (i = 0; i <= map->max_register; i += map->reg_stride) {
427 /* Ignore registers which are neither readable nor writable */
428 if (!regmap_readable(map, i) && !regmap_writeable(map, i))
431 /* If we're in the region the user is trying to read */
433 /* ...but not beyond it */
434 if (buf_pos + tot_len + 1 >= count)
437 /* Format the register */
438 snprintf(buf + buf_pos, count - buf_pos,
439 "%.*x: %c %c %c %c\n",
441 regmap_readable(map, i) ? 'y' : 'n',
442 regmap_writeable(map, i) ? 'y' : 'n',
443 regmap_volatile(map, i) ? 'y' : 'n',
444 regmap_precious(map, i) ? 'y' : 'n');
453 if (copy_to_user(user_buf, buf, buf_pos)) {
465 static const struct file_operations regmap_access_fops = {
467 .read = regmap_access_read_file,
468 .llseek = default_llseek,
471 static ssize_t regmap_cache_only_write_file(struct file *file,
472 const char __user *user_buf,
473 size_t count, loff_t *ppos)
475 struct regmap *map = container_of(file->private_data,
476 struct regmap, cache_only);
478 bool was_enabled, require_sync = false;
481 map->lock(map->lock_arg);
483 was_enabled = map->cache_only;
485 result = debugfs_write_file_bool(file, user_buf, count, ppos);
487 map->unlock(map->lock_arg);
491 if (map->cache_only && !was_enabled) {
492 dev_warn(map->dev, "debugfs cache_only=Y forced\n");
493 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
494 } else if (!map->cache_only && was_enabled) {
495 dev_warn(map->dev, "debugfs cache_only=N forced: syncing cache\n");
499 map->unlock(map->lock_arg);
502 err = regcache_sync(map);
504 dev_err(map->dev, "Failed to sync cache %d\n", err);
510 static const struct file_operations regmap_cache_only_fops = {
512 .read = debugfs_read_file_bool,
513 .write = regmap_cache_only_write_file,
516 static ssize_t regmap_cache_bypass_write_file(struct file *file,
517 const char __user *user_buf,
518 size_t count, loff_t *ppos)
520 struct regmap *map = container_of(file->private_data,
521 struct regmap, cache_bypass);
525 map->lock(map->lock_arg);
527 was_enabled = map->cache_bypass;
529 result = debugfs_write_file_bool(file, user_buf, count, ppos);
533 if (map->cache_bypass && !was_enabled) {
534 dev_warn(map->dev, "debugfs cache_bypass=Y forced\n");
535 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
536 } else if (!map->cache_bypass && was_enabled) {
537 dev_warn(map->dev, "debugfs cache_bypass=N forced\n");
541 map->unlock(map->lock_arg);
546 static const struct file_operations regmap_cache_bypass_fops = {
548 .read = debugfs_read_file_bool,
549 .write = regmap_cache_bypass_write_file,
552 void regmap_debugfs_init(struct regmap *map, const char *name)
554 struct rb_node *next;
555 struct regmap_range_node *range_node;
556 const char *devname = "dummy";
558 /* If we don't have the debugfs root yet, postpone init */
559 if (!regmap_debugfs_root) {
560 struct regmap_debugfs_node *node;
561 node = kzalloc(sizeof(*node), GFP_KERNEL);
566 mutex_lock(®map_debugfs_early_lock);
567 list_add(&node->link, ®map_debugfs_early_list);
568 mutex_unlock(®map_debugfs_early_lock);
572 INIT_LIST_HEAD(&map->debugfs_off_cache);
573 mutex_init(&map->cache_lock);
576 devname = dev_name(map->dev);
579 map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s",
581 name = map->debugfs_name;
586 map->debugfs = debugfs_create_dir(name, regmap_debugfs_root);
588 dev_warn(map->dev, "Failed to create debugfs directory\n");
592 debugfs_create_file("name", 0400, map->debugfs,
593 map, ®map_name_fops);
595 debugfs_create_file("range", 0400, map->debugfs,
596 map, ®map_reg_ranges_fops);
598 if (map->max_register || regmap_readable(map, 0)) {
599 umode_t registers_mode;
601 #if defined(REGMAP_ALLOW_WRITE_DEBUGFS)
602 registers_mode = 0600;
604 registers_mode = 0400;
607 debugfs_create_file("registers", registers_mode, map->debugfs,
608 map, ®map_map_fops);
609 debugfs_create_file("access", 0400, map->debugfs,
610 map, ®map_access_fops);
613 if (map->cache_type) {
614 debugfs_create_file("cache_only", 0600, map->debugfs,
615 &map->cache_only, ®map_cache_only_fops);
616 debugfs_create_bool("cache_dirty", 0400, map->debugfs,
618 debugfs_create_file("cache_bypass", 0600, map->debugfs,
620 ®map_cache_bypass_fops);
623 next = rb_first(&map->range_tree);
625 range_node = rb_entry(next, struct regmap_range_node, node);
627 if (range_node->name)
628 debugfs_create_file(range_node->name, 0400,
629 map->debugfs, range_node,
632 next = rb_next(&range_node->node);
635 if (map->cache_ops && map->cache_ops->debugfs_init)
636 map->cache_ops->debugfs_init(map);
639 void regmap_debugfs_exit(struct regmap *map)
642 debugfs_remove_recursive(map->debugfs);
643 mutex_lock(&map->cache_lock);
644 regmap_debugfs_free_dump_cache(map);
645 mutex_unlock(&map->cache_lock);
646 kfree(map->debugfs_name);
648 struct regmap_debugfs_node *node, *tmp;
650 mutex_lock(®map_debugfs_early_lock);
651 list_for_each_entry_safe(node, tmp, ®map_debugfs_early_list,
653 if (node->map == map) {
654 list_del(&node->link);
658 mutex_unlock(®map_debugfs_early_lock);
662 void regmap_debugfs_initcall(void)
664 struct regmap_debugfs_node *node, *tmp;
666 regmap_debugfs_root = debugfs_create_dir("regmap", NULL);
667 if (!regmap_debugfs_root) {
668 pr_warn("regmap: Failed to create debugfs root\n");
672 mutex_lock(®map_debugfs_early_lock);
673 list_for_each_entry_safe(node, tmp, ®map_debugfs_early_list, link) {
674 regmap_debugfs_init(node->map, node->name);
675 list_del(&node->link);
678 mutex_unlock(®map_debugfs_early_lock);