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/module.h>
15 #include <linux/mutex.h>
16 #include <linux/debugfs.h>
17 #include <linux/uaccess.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 int regmap_open_file(struct inode
*inode
, struct file
*file
)
32 file
->private_data
= inode
->i_private
;
36 static ssize_t
regmap_map_read_file(struct file
*file
, char __user
*user_buf
,
37 size_t count
, loff_t
*ppos
)
39 int reg_len
, val_len
, tot_len
;
44 struct regmap
*map
= file
->private_data
;
48 if (*ppos
< 0 || !count
)
51 buf
= kmalloc(count
, GFP_KERNEL
);
55 /* Calculate the length of a fixed format */
56 reg_len
= regmap_calc_reg_len(map
->max_register
, buf
, count
);
57 val_len
= 2 * map
->format
.val_bytes
;
58 tot_len
= reg_len
+ val_len
+ 3; /* : \n */
60 for (i
= 0; i
< map
->max_register
+ 1; i
++) {
61 if (!regmap_readable(map
, i
))
64 if (regmap_precious(map
, i
))
67 /* If we're in the region the user is trying to read */
69 /* ...but not beyond it */
70 if (buf_pos
>= count
- 1 - tot_len
)
73 /* Format the register */
74 snprintf(buf
+ buf_pos
, count
- buf_pos
, "%.*x: ",
76 buf_pos
+= reg_len
+ 2;
78 /* Format the value, write all X if we can't read */
79 ret
= regmap_read(map
, i
, &val
);
81 snprintf(buf
+ buf_pos
, count
- buf_pos
,
82 "%.*x", val_len
, val
);
84 memset(buf
+ buf_pos
, 'X', val_len
);
85 buf_pos
+= 2 * map
->format
.val_bytes
;
87 buf
[buf_pos
++] = '\n';
94 if (copy_to_user(user_buf
, buf
, buf_pos
)) {
106 static const struct file_operations regmap_map_fops
= {
107 .open
= regmap_open_file
,
108 .read
= regmap_map_read_file
,
109 .llseek
= default_llseek
,
112 static ssize_t
regmap_access_read_file(struct file
*file
,
113 char __user
*user_buf
, size_t count
,
116 int reg_len
, tot_len
;
121 struct regmap
*map
= file
->private_data
;
124 if (*ppos
< 0 || !count
)
127 buf
= kmalloc(count
, GFP_KERNEL
);
131 /* Calculate the length of a fixed format */
132 reg_len
= regmap_calc_reg_len(map
->max_register
, buf
, count
);
133 tot_len
= reg_len
+ 10; /* ': R W V P\n' */
135 for (i
= 0; i
< map
->max_register
+ 1; i
++) {
136 /* Ignore registers which are neither readable nor writable */
137 if (!regmap_readable(map
, i
) && !regmap_writeable(map
, i
))
140 /* If we're in the region the user is trying to read */
142 /* ...but not beyond it */
143 if (buf_pos
>= count
- 1 - tot_len
)
146 /* Format the register */
147 snprintf(buf
+ buf_pos
, count
- buf_pos
,
148 "%.*x: %c %c %c %c\n",
150 regmap_readable(map
, i
) ? 'y' : 'n',
151 regmap_writeable(map
, i
) ? 'y' : 'n',
152 regmap_volatile(map
, i
) ? 'y' : 'n',
153 regmap_precious(map
, i
) ? 'y' : 'n');
162 if (copy_to_user(user_buf
, buf
, buf_pos
)) {
174 static const struct file_operations regmap_access_fops
= {
175 .open
= regmap_open_file
,
176 .read
= regmap_access_read_file
,
177 .llseek
= default_llseek
,
180 void regmap_debugfs_init(struct regmap
*map
)
182 map
->debugfs
= debugfs_create_dir(dev_name(map
->dev
),
183 regmap_debugfs_root
);
185 dev_warn(map
->dev
, "Failed to create debugfs directory\n");
189 if (map
->max_register
) {
190 debugfs_create_file("registers", 0400, map
->debugfs
,
191 map
, ®map_map_fops
);
192 debugfs_create_file("access", 0400, map
->debugfs
,
193 map
, ®map_access_fops
);
197 void regmap_debugfs_exit(struct regmap
*map
)
199 debugfs_remove_recursive(map
->debugfs
);
202 void regmap_debugfs_initcall(void)
204 regmap_debugfs_root
= debugfs_create_dir("regmap", NULL
);
205 if (!regmap_debugfs_root
) {
206 pr_warn("regmap: Failed to create debugfs root\n");