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
)
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
,
80 static bool regmap_printable(struct regmap
*map
, unsigned int reg
)
82 if (regmap_precious(map
, reg
))
85 if (!regmap_readable(map
, reg
) && !regmap_cached(map
, reg
))
92 * Work out where the start offset maps into register numbers, bearing
93 * in mind that we suppress hidden registers.
95 static unsigned int regmap_debugfs_get_dump_start(struct regmap
*map
,
100 struct regmap_debugfs_off_cache
*c
= NULL
;
103 unsigned int fpos_offset
;
104 unsigned int reg_offset
;
106 /* Suppress the cache if we're using a subrange */
111 * If we don't have a cache build one so we don't have to do a
112 * linear scan each time.
114 mutex_lock(&map
->cache_lock
);
116 if (list_empty(&map
->debugfs_off_cache
)) {
117 for (; i
<= map
->max_register
; i
+= map
->reg_stride
) {
118 /* Skip unprinted registers, closing off cache entry */
119 if (!regmap_printable(map
, i
)) {
122 c
->max_reg
= i
- map
->reg_stride
;
123 list_add_tail(&c
->list
,
124 &map
->debugfs_off_cache
);
131 /* No cache entry? Start a new one */
133 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
135 regmap_debugfs_free_dump_cache(map
);
136 mutex_unlock(&map
->cache_lock
);
143 p
+= map
->debugfs_tot_len
;
147 /* Close the last entry off if we didn't scan beyond it */
150 c
->max_reg
= i
- map
->reg_stride
;
151 list_add_tail(&c
->list
,
152 &map
->debugfs_off_cache
);
156 * This should never happen; we return above if we fail to
157 * allocate and we should never be in this code if there are
158 * no registers at all.
160 WARN_ON(list_empty(&map
->debugfs_off_cache
));
163 /* Find the relevant block:offset */
164 list_for_each_entry(c
, &map
->debugfs_off_cache
, list
) {
165 if (from
>= c
->min
&& from
<= c
->max
) {
166 fpos_offset
= from
- c
->min
;
167 reg_offset
= fpos_offset
/ map
->debugfs_tot_len
;
168 *pos
= c
->min
+ (reg_offset
* map
->debugfs_tot_len
);
169 mutex_unlock(&map
->cache_lock
);
170 return c
->base_reg
+ (reg_offset
* map
->reg_stride
);
176 mutex_unlock(&map
->cache_lock
);
181 static inline void regmap_calc_tot_len(struct regmap
*map
,
182 void *buf
, size_t count
)
184 /* Calculate the length of a fixed format */
185 if (!map
->debugfs_tot_len
) {
186 map
->debugfs_reg_len
= regmap_calc_reg_len(map
->max_register
),
187 map
->debugfs_val_len
= 2 * map
->format
.val_bytes
;
188 map
->debugfs_tot_len
= map
->debugfs_reg_len
+
189 map
->debugfs_val_len
+ 3; /* : \n */
193 static ssize_t
regmap_read_debugfs(struct regmap
*map
, unsigned int from
,
194 unsigned int to
, char __user
*user_buf
,
195 size_t count
, loff_t
*ppos
)
202 unsigned int val
, start_reg
;
204 if (*ppos
< 0 || !count
)
207 buf
= kmalloc(count
, GFP_KERNEL
);
211 regmap_calc_tot_len(map
, buf
, count
);
213 /* Work out which register we're starting at */
214 start_reg
= regmap_debugfs_get_dump_start(map
, from
, *ppos
, &p
);
216 for (i
= start_reg
; i
<= to
; i
+= map
->reg_stride
) {
217 if (!regmap_readable(map
, i
) && !regmap_cached(map
, i
))
220 if (regmap_precious(map
, i
))
223 /* If we're in the region the user is trying to read */
225 /* ...but not beyond it */
226 if (buf_pos
+ map
->debugfs_tot_len
> count
)
229 /* Format the register */
230 snprintf(buf
+ buf_pos
, count
- buf_pos
, "%.*x: ",
231 map
->debugfs_reg_len
, i
- from
);
232 buf_pos
+= map
->debugfs_reg_len
+ 2;
234 /* Format the value, write all X if we can't read */
235 ret
= regmap_read(map
, i
, &val
);
237 snprintf(buf
+ buf_pos
, count
- buf_pos
,
238 "%.*x", map
->debugfs_val_len
, val
);
240 memset(buf
+ buf_pos
, 'X',
241 map
->debugfs_val_len
);
242 buf_pos
+= 2 * map
->format
.val_bytes
;
244 buf
[buf_pos
++] = '\n';
246 p
+= map
->debugfs_tot_len
;
251 if (copy_to_user(user_buf
, buf
, buf_pos
)) {
263 static ssize_t
regmap_map_read_file(struct file
*file
, char __user
*user_buf
,
264 size_t count
, loff_t
*ppos
)
266 struct regmap
*map
= file
->private_data
;
268 return regmap_read_debugfs(map
, 0, map
->max_register
, user_buf
,
272 #undef REGMAP_ALLOW_WRITE_DEBUGFS
273 #ifdef REGMAP_ALLOW_WRITE_DEBUGFS
275 * This can be dangerous especially when we have clients such as
276 * PMICs, therefore don't provide any real compile time configuration option
277 * for this feature, people who want to use this will need to modify
278 * the source code directly.
280 static ssize_t
regmap_map_write_file(struct file
*file
,
281 const char __user
*user_buf
,
282 size_t count
, loff_t
*ppos
)
287 unsigned long reg
, value
;
288 struct regmap
*map
= file
->private_data
;
291 buf_size
= min(count
, (sizeof(buf
)-1));
292 if (copy_from_user(buf
, user_buf
, buf_size
))
296 while (*start
== ' ')
298 reg
= simple_strtoul(start
, &start
, 16);
299 while (*start
== ' ')
301 if (kstrtoul(start
, 16, &value
))
304 /* Userspace has been fiddling around behind the kernel's back */
305 add_taint(TAINT_USER
, LOCKDEP_STILL_OK
);
307 ret
= regmap_write(map
, reg
, value
);
313 #define regmap_map_write_file NULL
316 static const struct file_operations regmap_map_fops
= {
318 .read
= regmap_map_read_file
,
319 .write
= regmap_map_write_file
,
320 .llseek
= default_llseek
,
323 static ssize_t
regmap_range_read_file(struct file
*file
, char __user
*user_buf
,
324 size_t count
, loff_t
*ppos
)
326 struct regmap_range_node
*range
= file
->private_data
;
327 struct regmap
*map
= range
->map
;
329 return regmap_read_debugfs(map
, range
->range_min
, range
->range_max
,
330 user_buf
, count
, ppos
);
333 static const struct file_operations regmap_range_fops
= {
335 .read
= regmap_range_read_file
,
336 .llseek
= default_llseek
,
339 static ssize_t
regmap_reg_ranges_read_file(struct file
*file
,
340 char __user
*user_buf
, size_t count
,
343 struct regmap
*map
= file
->private_data
;
344 struct regmap_debugfs_off_cache
*c
;
352 if (*ppos
< 0 || !count
)
355 buf
= kmalloc(count
, GFP_KERNEL
);
359 entry
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
365 /* While we are at it, build the register dump cache
366 * now so the read() operation on the `registers' file
367 * can benefit from using the cache. We do not care
368 * about the file position information that is contained
369 * in the cache, just about the actual register blocks */
370 regmap_calc_tot_len(map
, buf
, count
);
371 regmap_debugfs_get_dump_start(map
, 0, *ppos
, &p
);
373 /* Reset file pointer as the fixed-format of the `registers'
374 * file is not compatible with the `range' file */
376 mutex_lock(&map
->cache_lock
);
377 list_for_each_entry(c
, &map
->debugfs_off_cache
, list
) {
378 entry_len
= snprintf(entry
, PAGE_SIZE
, "%x-%x\n",
379 c
->base_reg
, c
->max_reg
);
381 if (buf_pos
+ entry_len
> count
)
383 memcpy(buf
+ buf_pos
, entry
, entry_len
);
384 buf_pos
+= entry_len
;
388 mutex_unlock(&map
->cache_lock
);
393 if (copy_to_user(user_buf
, buf
, buf_pos
)) {
404 static const struct file_operations regmap_reg_ranges_fops
= {
406 .read
= regmap_reg_ranges_read_file
,
407 .llseek
= default_llseek
,
410 static int regmap_access_show(struct seq_file
*s
, void *ignored
)
412 struct regmap
*map
= s
->private;
415 reg_len
= regmap_calc_reg_len(map
->max_register
);
417 for (i
= 0; i
<= map
->max_register
; i
+= map
->reg_stride
) {
418 /* Ignore registers which are neither readable nor writable */
419 if (!regmap_readable(map
, i
) && !regmap_writeable(map
, i
))
422 /* Format the register */
423 seq_printf(s
, "%.*x: %c %c %c %c\n", reg_len
, i
,
424 regmap_readable(map
, i
) ? 'y' : 'n',
425 regmap_writeable(map
, i
) ? 'y' : 'n',
426 regmap_volatile(map
, i
) ? 'y' : 'n',
427 regmap_precious(map
, i
) ? 'y' : 'n');
433 static int access_open(struct inode
*inode
, struct file
*file
)
435 return single_open(file
, regmap_access_show
, inode
->i_private
);
438 static const struct file_operations regmap_access_fops
= {
442 .release
= single_release
,
445 static ssize_t
regmap_cache_only_write_file(struct file
*file
,
446 const char __user
*user_buf
,
447 size_t count
, loff_t
*ppos
)
449 struct regmap
*map
= container_of(file
->private_data
,
450 struct regmap
, cache_only
);
452 bool was_enabled
, require_sync
= false;
455 map
->lock(map
->lock_arg
);
457 was_enabled
= map
->cache_only
;
459 result
= debugfs_write_file_bool(file
, user_buf
, count
, ppos
);
461 map
->unlock(map
->lock_arg
);
465 if (map
->cache_only
&& !was_enabled
) {
466 dev_warn(map
->dev
, "debugfs cache_only=Y forced\n");
467 add_taint(TAINT_USER
, LOCKDEP_STILL_OK
);
468 } else if (!map
->cache_only
&& was_enabled
) {
469 dev_warn(map
->dev
, "debugfs cache_only=N forced: syncing cache\n");
473 map
->unlock(map
->lock_arg
);
476 err
= regcache_sync(map
);
478 dev_err(map
->dev
, "Failed to sync cache %d\n", err
);
484 static const struct file_operations regmap_cache_only_fops
= {
486 .read
= debugfs_read_file_bool
,
487 .write
= regmap_cache_only_write_file
,
490 static ssize_t
regmap_cache_bypass_write_file(struct file
*file
,
491 const char __user
*user_buf
,
492 size_t count
, loff_t
*ppos
)
494 struct regmap
*map
= container_of(file
->private_data
,
495 struct regmap
, cache_bypass
);
499 map
->lock(map
->lock_arg
);
501 was_enabled
= map
->cache_bypass
;
503 result
= debugfs_write_file_bool(file
, user_buf
, count
, ppos
);
507 if (map
->cache_bypass
&& !was_enabled
) {
508 dev_warn(map
->dev
, "debugfs cache_bypass=Y forced\n");
509 add_taint(TAINT_USER
, LOCKDEP_STILL_OK
);
510 } else if (!map
->cache_bypass
&& was_enabled
) {
511 dev_warn(map
->dev
, "debugfs cache_bypass=N forced\n");
515 map
->unlock(map
->lock_arg
);
520 static const struct file_operations regmap_cache_bypass_fops
= {
522 .read
= debugfs_read_file_bool
,
523 .write
= regmap_cache_bypass_write_file
,
526 void regmap_debugfs_init(struct regmap
*map
, const char *name
)
528 struct rb_node
*next
;
529 struct regmap_range_node
*range_node
;
530 const char *devname
= "dummy";
533 * Userspace can initiate reads from the hardware over debugfs.
534 * Normally internal regmap structures and buffers are protected with
535 * a mutex or a spinlock, but if the regmap owner decided to disable
536 * all locking mechanisms, this is no longer the case. For safety:
537 * don't create the debugfs entries if locking is disabled.
539 if (map
->debugfs_disable
) {
540 dev_dbg(map
->dev
, "regmap locking disabled - not creating debugfs entries\n");
544 /* If we don't have the debugfs root yet, postpone init */
545 if (!regmap_debugfs_root
) {
546 struct regmap_debugfs_node
*node
;
547 node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
552 mutex_lock(®map_debugfs_early_lock
);
553 list_add(&node
->link
, ®map_debugfs_early_list
);
554 mutex_unlock(®map_debugfs_early_lock
);
558 INIT_LIST_HEAD(&map
->debugfs_off_cache
);
559 mutex_init(&map
->cache_lock
);
562 devname
= dev_name(map
->dev
);
565 map
->debugfs_name
= kasprintf(GFP_KERNEL
, "%s-%s",
567 name
= map
->debugfs_name
;
572 map
->debugfs
= debugfs_create_dir(name
, regmap_debugfs_root
);
574 dev_warn(map
->dev
, "Failed to create debugfs directory\n");
578 debugfs_create_file("name", 0400, map
->debugfs
,
579 map
, ®map_name_fops
);
581 debugfs_create_file("range", 0400, map
->debugfs
,
582 map
, ®map_reg_ranges_fops
);
584 if (map
->max_register
|| regmap_readable(map
, 0)) {
585 umode_t registers_mode
;
587 #if defined(REGMAP_ALLOW_WRITE_DEBUGFS)
588 registers_mode
= 0600;
590 registers_mode
= 0400;
593 debugfs_create_file("registers", registers_mode
, map
->debugfs
,
594 map
, ®map_map_fops
);
595 debugfs_create_file("access", 0400, map
->debugfs
,
596 map
, ®map_access_fops
);
599 if (map
->cache_type
) {
600 debugfs_create_file("cache_only", 0600, map
->debugfs
,
601 &map
->cache_only
, ®map_cache_only_fops
);
602 debugfs_create_bool("cache_dirty", 0400, map
->debugfs
,
604 debugfs_create_file("cache_bypass", 0600, map
->debugfs
,
606 ®map_cache_bypass_fops
);
609 next
= rb_first(&map
->range_tree
);
611 range_node
= rb_entry(next
, struct regmap_range_node
, node
);
613 if (range_node
->name
)
614 debugfs_create_file(range_node
->name
, 0400,
615 map
->debugfs
, range_node
,
618 next
= rb_next(&range_node
->node
);
621 if (map
->cache_ops
&& map
->cache_ops
->debugfs_init
)
622 map
->cache_ops
->debugfs_init(map
);
625 void regmap_debugfs_exit(struct regmap
*map
)
628 debugfs_remove_recursive(map
->debugfs
);
629 mutex_lock(&map
->cache_lock
);
630 regmap_debugfs_free_dump_cache(map
);
631 mutex_unlock(&map
->cache_lock
);
632 kfree(map
->debugfs_name
);
634 struct regmap_debugfs_node
*node
, *tmp
;
636 mutex_lock(®map_debugfs_early_lock
);
637 list_for_each_entry_safe(node
, tmp
, ®map_debugfs_early_list
,
639 if (node
->map
== map
) {
640 list_del(&node
->link
);
644 mutex_unlock(®map_debugfs_early_lock
);
648 void regmap_debugfs_initcall(void)
650 struct regmap_debugfs_node
*node
, *tmp
;
652 regmap_debugfs_root
= debugfs_create_dir("regmap", NULL
);
653 if (!regmap_debugfs_root
) {
654 pr_warn("regmap: Failed to create debugfs root\n");
658 mutex_lock(®map_debugfs_early_lock
);
659 list_for_each_entry_safe(node
, tmp
, ®map_debugfs_early_list
, link
) {
660 regmap_debugfs_init(node
->map
, node
->name
);
661 list_del(&node
->link
);
664 mutex_unlock(®map_debugfs_early_lock
);