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
;
84 unsigned int fpos_offset
;
85 unsigned int reg_offset
;
88 * If we don't have a cache build one so we don't have to do a
89 * linear scan each time.
91 mutex_lock(&map
->cache_lock
);
93 if (list_empty(&map
->debugfs_off_cache
)) {
94 for (; i
<= map
->max_register
; i
+= map
->reg_stride
) {
95 /* Skip unprinted registers, closing off cache entry */
96 if (!regmap_readable(map
, i
) ||
97 regmap_precious(map
, i
)) {
100 c
->max_reg
= i
- map
->reg_stride
;
101 list_add_tail(&c
->list
,
102 &map
->debugfs_off_cache
);
109 /* No cache entry? Start a new one */
111 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
113 regmap_debugfs_free_dump_cache(map
);
114 mutex_unlock(&map
->cache_lock
);
121 p
+= map
->debugfs_tot_len
;
125 /* Close the last entry off if we didn't scan beyond it */
128 c
->max_reg
= i
- map
->reg_stride
;
129 list_add_tail(&c
->list
,
130 &map
->debugfs_off_cache
);
134 * This should never happen; we return above if we fail to
135 * allocate and we should never be in this code if there are
136 * no registers at all.
138 WARN_ON(list_empty(&map
->debugfs_off_cache
));
141 /* Find the relevant block:offset */
142 list_for_each_entry(c
, &map
->debugfs_off_cache
, list
) {
143 if (from
>= c
->min
&& from
<= c
->max
) {
144 fpos_offset
= from
- c
->min
;
145 reg_offset
= fpos_offset
/ map
->debugfs_tot_len
;
146 *pos
= c
->min
+ (reg_offset
* map
->debugfs_tot_len
);
147 mutex_unlock(&map
->cache_lock
);
148 return c
->base_reg
+ reg_offset
;
154 mutex_unlock(&map
->cache_lock
);
159 static inline void regmap_calc_tot_len(struct regmap
*map
,
160 void *buf
, size_t count
)
162 /* Calculate the length of a fixed format */
163 if (!map
->debugfs_tot_len
) {
164 map
->debugfs_reg_len
= regmap_calc_reg_len(map
->max_register
,
166 map
->debugfs_val_len
= 2 * map
->format
.val_bytes
;
167 map
->debugfs_tot_len
= map
->debugfs_reg_len
+
168 map
->debugfs_val_len
+ 3; /* : \n */
172 static ssize_t
regmap_read_debugfs(struct regmap
*map
, unsigned int from
,
173 unsigned int to
, char __user
*user_buf
,
174 size_t count
, loff_t
*ppos
)
181 unsigned int val
, start_reg
;
183 if (*ppos
< 0 || !count
)
186 buf
= kmalloc(count
, GFP_KERNEL
);
190 regmap_calc_tot_len(map
, buf
, count
);
192 /* Work out which register we're starting at */
193 start_reg
= regmap_debugfs_get_dump_start(map
, from
, *ppos
, &p
);
195 for (i
= start_reg
; i
<= to
; i
+= map
->reg_stride
) {
196 if (!regmap_readable(map
, i
))
199 if (regmap_precious(map
, i
))
202 /* If we're in the region the user is trying to read */
204 /* ...but not beyond it */
205 if (buf_pos
+ map
->debugfs_tot_len
> count
)
208 /* Format the register */
209 snprintf(buf
+ buf_pos
, count
- buf_pos
, "%.*x: ",
210 map
->debugfs_reg_len
, i
- from
);
211 buf_pos
+= map
->debugfs_reg_len
+ 2;
213 /* Format the value, write all X if we can't read */
214 ret
= regmap_read(map
, i
, &val
);
216 snprintf(buf
+ buf_pos
, count
- buf_pos
,
217 "%.*x", map
->debugfs_val_len
, val
);
219 memset(buf
+ buf_pos
, 'X',
220 map
->debugfs_val_len
);
221 buf_pos
+= 2 * map
->format
.val_bytes
;
223 buf
[buf_pos
++] = '\n';
225 p
+= map
->debugfs_tot_len
;
230 if (copy_to_user(user_buf
, buf
, buf_pos
)) {
242 static ssize_t
regmap_map_read_file(struct file
*file
, char __user
*user_buf
,
243 size_t count
, loff_t
*ppos
)
245 struct regmap
*map
= file
->private_data
;
247 return regmap_read_debugfs(map
, 0, map
->max_register
, user_buf
,
251 #undef REGMAP_ALLOW_WRITE_DEBUGFS
252 #ifdef REGMAP_ALLOW_WRITE_DEBUGFS
254 * This can be dangerous especially when we have clients such as
255 * PMICs, therefore don't provide any real compile time configuration option
256 * for this feature, people who want to use this will need to modify
257 * the source code directly.
259 static ssize_t
regmap_map_write_file(struct file
*file
,
260 const char __user
*user_buf
,
261 size_t count
, loff_t
*ppos
)
266 unsigned long reg
, value
;
267 struct regmap
*map
= file
->private_data
;
269 buf_size
= min(count
, (sizeof(buf
)-1));
270 if (copy_from_user(buf
, user_buf
, buf_size
))
274 while (*start
== ' ')
276 reg
= simple_strtoul(start
, &start
, 16);
277 while (*start
== ' ')
279 if (strict_strtoul(start
, 16, &value
))
282 /* Userspace has been fiddling around behind the kernel's back */
283 add_taint(TAINT_USER
, LOCKDEP_NOW_UNRELIABLE
);
285 regmap_write(map
, reg
, value
);
289 #define regmap_map_write_file NULL
292 static const struct file_operations regmap_map_fops
= {
294 .read
= regmap_map_read_file
,
295 .write
= regmap_map_write_file
,
296 .llseek
= default_llseek
,
299 static ssize_t
regmap_range_read_file(struct file
*file
, char __user
*user_buf
,
300 size_t count
, loff_t
*ppos
)
302 struct regmap_range_node
*range
= file
->private_data
;
303 struct regmap
*map
= range
->map
;
305 return regmap_read_debugfs(map
, range
->range_min
, range
->range_max
,
306 user_buf
, count
, ppos
);
309 static const struct file_operations regmap_range_fops
= {
311 .read
= regmap_range_read_file
,
312 .llseek
= default_llseek
,
315 static ssize_t
regmap_reg_ranges_read_file(struct file
*file
,
316 char __user
*user_buf
, size_t count
,
319 struct regmap
*map
= file
->private_data
;
320 struct regmap_debugfs_off_cache
*c
;
327 if (*ppos
< 0 || !count
)
330 buf
= kmalloc(count
, GFP_KERNEL
);
334 entry
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
340 /* While we are at it, build the register dump cache
341 * now so the read() operation on the `registers' file
342 * can benefit from using the cache. We do not care
343 * about the file position information that is contained
344 * in the cache, just about the actual register blocks */
345 regmap_calc_tot_len(map
, buf
, count
);
346 regmap_debugfs_get_dump_start(map
, 0, *ppos
, &p
);
348 /* Reset file pointer as the fixed-format of the `registers'
349 * file is not compatible with the `range' file */
351 mutex_lock(&map
->cache_lock
);
352 list_for_each_entry(c
, &map
->debugfs_off_cache
, list
) {
353 snprintf(entry
, PAGE_SIZE
, "%x-%x",
354 c
->base_reg
, c
->max_reg
);
356 if (buf_pos
+ 1 + strlen(entry
) > count
)
358 snprintf(buf
+ buf_pos
, count
- buf_pos
,
360 buf_pos
+= strlen(entry
);
364 p
+= strlen(entry
) + 1;
366 mutex_unlock(&map
->cache_lock
);
371 if (copy_to_user(user_buf
, buf
, buf_pos
)) {
382 static const struct file_operations regmap_reg_ranges_fops
= {
384 .read
= regmap_reg_ranges_read_file
,
385 .llseek
= default_llseek
,
388 static ssize_t
regmap_access_read_file(struct file
*file
,
389 char __user
*user_buf
, size_t count
,
392 int reg_len
, tot_len
;
397 struct regmap
*map
= file
->private_data
;
400 if (*ppos
< 0 || !count
)
403 buf
= kmalloc(count
, GFP_KERNEL
);
407 /* Calculate the length of a fixed format */
408 reg_len
= regmap_calc_reg_len(map
->max_register
, buf
, count
);
409 tot_len
= reg_len
+ 10; /* ': R W V P\n' */
411 for (i
= 0; i
<= map
->max_register
; i
+= map
->reg_stride
) {
412 /* Ignore registers which are neither readable nor writable */
413 if (!regmap_readable(map
, i
) && !regmap_writeable(map
, i
))
416 /* If we're in the region the user is trying to read */
418 /* ...but not beyond it */
419 if (buf_pos
>= count
- 1 - tot_len
)
422 /* Format the register */
423 snprintf(buf
+ buf_pos
, count
- buf_pos
,
424 "%.*x: %c %c %c %c\n",
426 regmap_readable(map
, i
) ? 'y' : 'n',
427 regmap_writeable(map
, i
) ? 'y' : 'n',
428 regmap_volatile(map
, i
) ? 'y' : 'n',
429 regmap_precious(map
, i
) ? 'y' : 'n');
438 if (copy_to_user(user_buf
, buf
, buf_pos
)) {
450 static const struct file_operations regmap_access_fops
= {
452 .read
= regmap_access_read_file
,
453 .llseek
= default_llseek
,
456 void regmap_debugfs_init(struct regmap
*map
, const char *name
)
458 struct rb_node
*next
;
459 struct regmap_range_node
*range_node
;
461 INIT_LIST_HEAD(&map
->debugfs_off_cache
);
462 mutex_init(&map
->cache_lock
);
465 map
->debugfs_name
= kasprintf(GFP_KERNEL
, "%s-%s",
466 dev_name(map
->dev
), name
);
467 name
= map
->debugfs_name
;
469 name
= dev_name(map
->dev
);
472 map
->debugfs
= debugfs_create_dir(name
, regmap_debugfs_root
);
474 dev_warn(map
->dev
, "Failed to create debugfs directory\n");
478 debugfs_create_file("name", 0400, map
->debugfs
,
479 map
, ®map_name_fops
);
481 debugfs_create_file("range", 0400, map
->debugfs
,
482 map
, ®map_reg_ranges_fops
);
484 if (map
->max_register
) {
485 debugfs_create_file("registers", 0400, map
->debugfs
,
486 map
, ®map_map_fops
);
487 debugfs_create_file("access", 0400, map
->debugfs
,
488 map
, ®map_access_fops
);
491 if (map
->cache_type
) {
492 debugfs_create_bool("cache_only", 0400, map
->debugfs
,
494 debugfs_create_bool("cache_dirty", 0400, map
->debugfs
,
496 debugfs_create_bool("cache_bypass", 0400, map
->debugfs
,
500 next
= rb_first(&map
->range_tree
);
502 range_node
= rb_entry(next
, struct regmap_range_node
, node
);
504 if (range_node
->name
)
505 debugfs_create_file(range_node
->name
, 0400,
506 map
->debugfs
, range_node
,
509 next
= rb_next(&range_node
->node
);
513 void regmap_debugfs_exit(struct regmap
*map
)
515 debugfs_remove_recursive(map
->debugfs
);
516 mutex_lock(&map
->cache_lock
);
517 regmap_debugfs_free_dump_cache(map
);
518 mutex_unlock(&map
->cache_lock
);
519 kfree(map
->debugfs_name
);
522 void regmap_debugfs_initcall(void)
524 regmap_debugfs_root
= debugfs_create_dir("regmap", NULL
);
525 if (!regmap_debugfs_root
) {
526 pr_warn("regmap: Failed to create debugfs root\n");