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 unsigned int dummy_index;
29 static struct dentry *regmap_debugfs_root;
30 static LIST_HEAD(regmap_debugfs_early_list);
31 static DEFINE_MUTEX(regmap_debugfs_early_lock);
33 /* Calculate the length of a fixed format */
34 static size_t regmap_calc_reg_len(int max_val)
36 return snprintf(NULL, 0, "%x", max_val);
39 static ssize_t regmap_name_read_file(struct file *file,
40 char __user *user_buf, size_t count,
43 struct regmap *map = file->private_data;
44 const char *name = "nodev";
48 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
52 if (map->dev && map->dev->driver)
53 name = map->dev->driver->name;
55 ret = snprintf(buf, PAGE_SIZE, "%s\n", name);
61 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
66 static const struct file_operations regmap_name_fops = {
68 .read = regmap_name_read_file,
69 .llseek = default_llseek,
72 static void regmap_debugfs_free_dump_cache(struct regmap *map)
74 struct regmap_debugfs_off_cache *c;
76 while (!list_empty(&map->debugfs_off_cache)) {
77 c = list_first_entry(&map->debugfs_off_cache,
78 struct regmap_debugfs_off_cache,
85 static bool regmap_printable(struct regmap *map, unsigned int reg)
87 if (regmap_precious(map, reg))
90 if (!regmap_readable(map, reg) && !regmap_cached(map, reg))
97 * Work out where the start offset maps into register numbers, bearing
98 * in mind that we suppress hidden registers.
100 static unsigned int regmap_debugfs_get_dump_start(struct regmap *map,
105 struct regmap_debugfs_off_cache *c = NULL;
108 unsigned int fpos_offset;
109 unsigned int reg_offset;
111 /* Suppress the cache if we're using a subrange */
116 * If we don't have a cache build one so we don't have to do a
117 * linear scan each time.
119 mutex_lock(&map->cache_lock);
121 if (list_empty(&map->debugfs_off_cache)) {
122 for (; i <= map->max_register; i += map->reg_stride) {
123 /* Skip unprinted registers, closing off cache entry */
124 if (!regmap_printable(map, i)) {
127 c->max_reg = i - map->reg_stride;
128 list_add_tail(&c->list,
129 &map->debugfs_off_cache);
136 /* No cache entry? Start a new one */
138 c = kzalloc(sizeof(*c), GFP_KERNEL);
140 regmap_debugfs_free_dump_cache(map);
141 mutex_unlock(&map->cache_lock);
148 p += map->debugfs_tot_len;
152 /* Close the last entry off if we didn't scan beyond it */
155 c->max_reg = i - map->reg_stride;
156 list_add_tail(&c->list,
157 &map->debugfs_off_cache);
161 * This should never happen; we return above if we fail to
162 * allocate and we should never be in this code if there are
163 * no registers at all.
165 WARN_ON(list_empty(&map->debugfs_off_cache));
168 /* Find the relevant block:offset */
169 list_for_each_entry(c, &map->debugfs_off_cache, list) {
170 if (from >= c->min && from <= c->max) {
171 fpos_offset = from - c->min;
172 reg_offset = fpos_offset / map->debugfs_tot_len;
173 *pos = c->min + (reg_offset * map->debugfs_tot_len);
174 mutex_unlock(&map->cache_lock);
175 return c->base_reg + (reg_offset * map->reg_stride);
181 mutex_unlock(&map->cache_lock);
186 static inline void regmap_calc_tot_len(struct regmap *map,
187 void *buf, size_t count)
189 /* Calculate the length of a fixed format */
190 if (!map->debugfs_tot_len) {
191 map->debugfs_reg_len = regmap_calc_reg_len(map->max_register),
192 map->debugfs_val_len = 2 * map->format.val_bytes;
193 map->debugfs_tot_len = map->debugfs_reg_len +
194 map->debugfs_val_len + 3; /* : \n */
198 static ssize_t regmap_read_debugfs(struct regmap *map, unsigned int from,
199 unsigned int to, char __user *user_buf,
200 size_t count, loff_t *ppos)
207 unsigned int val, start_reg;
209 if (*ppos < 0 || !count)
212 if (count > (PAGE_SIZE << (MAX_ORDER - 1)))
213 count = PAGE_SIZE << (MAX_ORDER - 1);
215 buf = kmalloc(count, GFP_KERNEL);
219 regmap_calc_tot_len(map, buf, count);
221 /* Work out which register we're starting at */
222 start_reg = regmap_debugfs_get_dump_start(map, from, *ppos, &p);
224 for (i = start_reg; i <= to; i += map->reg_stride) {
225 if (!regmap_readable(map, i) && !regmap_cached(map, i))
228 if (regmap_precious(map, i))
231 /* If we're in the region the user is trying to read */
233 /* ...but not beyond it */
234 if (buf_pos + map->debugfs_tot_len > count)
237 /* Format the register */
238 snprintf(buf + buf_pos, count - buf_pos, "%.*x: ",
239 map->debugfs_reg_len, i - from);
240 buf_pos += map->debugfs_reg_len + 2;
242 /* Format the value, write all X if we can't read */
243 ret = regmap_read(map, i, &val);
245 snprintf(buf + buf_pos, count - buf_pos,
246 "%.*x", map->debugfs_val_len, val);
248 memset(buf + buf_pos, 'X',
249 map->debugfs_val_len);
250 buf_pos += 2 * map->format.val_bytes;
252 buf[buf_pos++] = '\n';
254 p += map->debugfs_tot_len;
259 if (copy_to_user(user_buf, buf, buf_pos)) {
271 static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf,
272 size_t count, loff_t *ppos)
274 struct regmap *map = file->private_data;
276 return regmap_read_debugfs(map, 0, map->max_register, user_buf,
280 #undef REGMAP_ALLOW_WRITE_DEBUGFS
281 #ifdef REGMAP_ALLOW_WRITE_DEBUGFS
283 * This can be dangerous especially when we have clients such as
284 * PMICs, therefore don't provide any real compile time configuration option
285 * for this feature, people who want to use this will need to modify
286 * the source code directly.
288 static ssize_t regmap_map_write_file(struct file *file,
289 const char __user *user_buf,
290 size_t count, loff_t *ppos)
295 unsigned long reg, value;
296 struct regmap *map = file->private_data;
299 buf_size = min(count, (sizeof(buf)-1));
300 if (copy_from_user(buf, user_buf, buf_size))
304 while (*start == ' ')
306 reg = simple_strtoul(start, &start, 16);
307 while (*start == ' ')
309 if (kstrtoul(start, 16, &value))
312 /* Userspace has been fiddling around behind the kernel's back */
313 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
315 ret = regmap_write(map, reg, value);
321 #define regmap_map_write_file NULL
324 static const struct file_operations regmap_map_fops = {
326 .read = regmap_map_read_file,
327 .write = regmap_map_write_file,
328 .llseek = default_llseek,
331 static ssize_t regmap_range_read_file(struct file *file, char __user *user_buf,
332 size_t count, loff_t *ppos)
334 struct regmap_range_node *range = file->private_data;
335 struct regmap *map = range->map;
337 return regmap_read_debugfs(map, range->range_min, range->range_max,
338 user_buf, count, ppos);
341 static const struct file_operations regmap_range_fops = {
343 .read = regmap_range_read_file,
344 .llseek = default_llseek,
347 static ssize_t regmap_reg_ranges_read_file(struct file *file,
348 char __user *user_buf, size_t count,
351 struct regmap *map = file->private_data;
352 struct regmap_debugfs_off_cache *c;
360 if (*ppos < 0 || !count)
363 if (count > (PAGE_SIZE << (MAX_ORDER - 1)))
364 count = PAGE_SIZE << (MAX_ORDER - 1);
366 buf = kmalloc(count, GFP_KERNEL);
370 entry = kmalloc(PAGE_SIZE, GFP_KERNEL);
376 /* While we are at it, build the register dump cache
377 * now so the read() operation on the `registers' file
378 * can benefit from using the cache. We do not care
379 * about the file position information that is contained
380 * in the cache, just about the actual register blocks */
381 regmap_calc_tot_len(map, buf, count);
382 regmap_debugfs_get_dump_start(map, 0, *ppos, &p);
384 /* Reset file pointer as the fixed-format of the `registers'
385 * file is not compatible with the `range' file */
387 mutex_lock(&map->cache_lock);
388 list_for_each_entry(c, &map->debugfs_off_cache, list) {
389 entry_len = snprintf(entry, PAGE_SIZE, "%x-%x\n",
390 c->base_reg, c->max_reg);
392 if (buf_pos + entry_len > count)
394 memcpy(buf + buf_pos, entry, entry_len);
395 buf_pos += entry_len;
399 mutex_unlock(&map->cache_lock);
404 if (copy_to_user(user_buf, buf, buf_pos)) {
415 static const struct file_operations regmap_reg_ranges_fops = {
417 .read = regmap_reg_ranges_read_file,
418 .llseek = default_llseek,
421 static int regmap_access_show(struct seq_file *s, void *ignored)
423 struct regmap *map = s->private;
426 reg_len = regmap_calc_reg_len(map->max_register);
428 for (i = 0; i <= map->max_register; i += map->reg_stride) {
429 /* Ignore registers which are neither readable nor writable */
430 if (!regmap_readable(map, i) && !regmap_writeable(map, i))
433 /* Format the register */
434 seq_printf(s, "%.*x: %c %c %c %c\n", reg_len, i,
435 regmap_readable(map, i) ? 'y' : 'n',
436 regmap_writeable(map, i) ? 'y' : 'n',
437 regmap_volatile(map, i) ? 'y' : 'n',
438 regmap_precious(map, i) ? 'y' : 'n');
444 static int access_open(struct inode *inode, struct file *file)
446 return single_open(file, regmap_access_show, inode->i_private);
449 static const struct file_operations regmap_access_fops = {
453 .release = single_release,
456 static ssize_t regmap_cache_only_write_file(struct file *file,
457 const char __user *user_buf,
458 size_t count, loff_t *ppos)
460 struct regmap *map = container_of(file->private_data,
461 struct regmap, cache_only);
462 bool new_val, require_sync = false;
465 err = kstrtobool_from_user(user_buf, count, &new_val);
466 /* Ignore malforned data like debugfs_write_file_bool() */
470 err = debugfs_file_get(file->f_path.dentry);
474 map->lock(map->lock_arg);
476 if (new_val && !map->cache_only) {
477 dev_warn(map->dev, "debugfs cache_only=Y forced\n");
478 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
479 } else if (!new_val && map->cache_only) {
480 dev_warn(map->dev, "debugfs cache_only=N forced: syncing cache\n");
483 map->cache_only = new_val;
485 map->unlock(map->lock_arg);
486 debugfs_file_put(file->f_path.dentry);
489 err = regcache_sync(map);
491 dev_err(map->dev, "Failed to sync cache %d\n", err);
497 static const struct file_operations regmap_cache_only_fops = {
499 .read = debugfs_read_file_bool,
500 .write = regmap_cache_only_write_file,
503 static ssize_t regmap_cache_bypass_write_file(struct file *file,
504 const char __user *user_buf,
505 size_t count, loff_t *ppos)
507 struct regmap *map = container_of(file->private_data,
508 struct regmap, cache_bypass);
512 err = kstrtobool_from_user(user_buf, count, &new_val);
513 /* Ignore malforned data like debugfs_write_file_bool() */
517 err = debugfs_file_get(file->f_path.dentry);
521 map->lock(map->lock_arg);
523 if (new_val && !map->cache_bypass) {
524 dev_warn(map->dev, "debugfs cache_bypass=Y forced\n");
525 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
526 } else if (!new_val && map->cache_bypass) {
527 dev_warn(map->dev, "debugfs cache_bypass=N forced\n");
529 map->cache_bypass = new_val;
531 map->unlock(map->lock_arg);
532 debugfs_file_put(file->f_path.dentry);
537 static const struct file_operations regmap_cache_bypass_fops = {
539 .read = debugfs_read_file_bool,
540 .write = regmap_cache_bypass_write_file,
543 void regmap_debugfs_init(struct regmap *map, const char *name)
545 struct rb_node *next;
546 struct regmap_range_node *range_node;
547 const char *devname = "dummy";
550 * Userspace can initiate reads from the hardware over debugfs.
551 * Normally internal regmap structures and buffers are protected with
552 * a mutex or a spinlock, but if the regmap owner decided to disable
553 * all locking mechanisms, this is no longer the case. For safety:
554 * don't create the debugfs entries if locking is disabled.
556 if (map->debugfs_disable) {
557 dev_dbg(map->dev, "regmap locking disabled - not creating debugfs entries\n");
561 /* If we don't have the debugfs root yet, postpone init */
562 if (!regmap_debugfs_root) {
563 struct regmap_debugfs_node *node;
564 node = kzalloc(sizeof(*node), GFP_KERNEL);
569 mutex_lock(®map_debugfs_early_lock);
570 list_add(&node->link, ®map_debugfs_early_list);
571 mutex_unlock(®map_debugfs_early_lock);
575 INIT_LIST_HEAD(&map->debugfs_off_cache);
576 mutex_init(&map->cache_lock);
579 devname = dev_name(map->dev);
582 if (!map->debugfs_name) {
583 map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s",
585 if (!map->debugfs_name)
588 name = map->debugfs_name;
593 if (!strcmp(name, "dummy")) {
594 kfree(map->debugfs_name);
595 map->debugfs_name = kasprintf(GFP_KERNEL, "dummy%d",
597 if (!map->debugfs_name)
599 name = map->debugfs_name;
603 map->debugfs = debugfs_create_dir(name, regmap_debugfs_root);
606 "Failed to create %s debugfs directory\n", name);
608 kfree(map->debugfs_name);
609 map->debugfs_name = NULL;
613 debugfs_create_file("name", 0400, map->debugfs,
614 map, ®map_name_fops);
616 debugfs_create_file("range", 0400, map->debugfs,
617 map, ®map_reg_ranges_fops);
619 if (map->max_register || regmap_readable(map, 0)) {
620 umode_t registers_mode;
622 #if defined(REGMAP_ALLOW_WRITE_DEBUGFS)
623 registers_mode = 0600;
625 registers_mode = 0400;
628 debugfs_create_file("registers", registers_mode, map->debugfs,
629 map, ®map_map_fops);
630 debugfs_create_file("access", 0400, map->debugfs,
631 map, ®map_access_fops);
634 if (map->cache_type) {
635 debugfs_create_file("cache_only", 0600, map->debugfs,
636 &map->cache_only, ®map_cache_only_fops);
637 debugfs_create_bool("cache_dirty", 0400, map->debugfs,
639 debugfs_create_file("cache_bypass", 0600, map->debugfs,
641 ®map_cache_bypass_fops);
644 next = rb_first(&map->range_tree);
646 range_node = rb_entry(next, struct regmap_range_node, node);
648 if (range_node->name)
649 debugfs_create_file(range_node->name, 0400,
650 map->debugfs, range_node,
653 next = rb_next(&range_node->node);
656 if (map->cache_ops && map->cache_ops->debugfs_init)
657 map->cache_ops->debugfs_init(map);
660 void regmap_debugfs_exit(struct regmap *map)
663 debugfs_remove_recursive(map->debugfs);
664 mutex_lock(&map->cache_lock);
665 regmap_debugfs_free_dump_cache(map);
666 mutex_unlock(&map->cache_lock);
667 kfree(map->debugfs_name);
668 map->debugfs_name = NULL;
670 struct regmap_debugfs_node *node, *tmp;
672 mutex_lock(®map_debugfs_early_lock);
673 list_for_each_entry_safe(node, tmp, ®map_debugfs_early_list,
675 if (node->map == map) {
676 list_del(&node->link);
680 mutex_unlock(®map_debugfs_early_lock);
684 void regmap_debugfs_initcall(void)
686 struct regmap_debugfs_node *node, *tmp;
688 regmap_debugfs_root = debugfs_create_dir("regmap", NULL);
689 if (!regmap_debugfs_root) {
690 pr_warn("regmap: Failed to create debugfs root\n");
694 mutex_lock(®map_debugfs_early_lock);
695 list_for_each_entry_safe(node, tmp, ®map_debugfs_early_list, link) {
696 regmap_debugfs_init(node->map, node->name);
697 list_del(&node->link);
700 mutex_unlock(®map_debugfs_early_lock);