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 ssize_t regmap_read_debugfs(struct regmap *map, unsigned int from,
60 unsigned int to, char __user *user_buf,
61 size_t count, loff_t *ppos)
70 if (*ppos < 0 || !count)
73 buf = kmalloc(count, GFP_KERNEL);
77 /* Calculate the length of a fixed format */
78 if (!map->debugfs_tot_len) {
79 map->debugfs_reg_len = regmap_calc_reg_len(map->max_register,
81 map->debugfs_val_len = 2 * map->format.val_bytes;
82 map->debugfs_tot_len = map->debugfs_reg_len +
83 map->debugfs_val_len + 3; /* : \n */
86 for (i = from; i <= to; i += map->reg_stride) {
87 if (!regmap_readable(map, i))
90 if (regmap_precious(map, i))
93 /* If we're in the region the user is trying to read */
95 /* ...but not beyond it */
96 if (buf_pos >= count - 1 - map->debugfs_tot_len)
99 /* Format the register */
100 snprintf(buf + buf_pos, count - buf_pos, "%.*x: ",
101 map->debugfs_reg_len, i - from);
102 buf_pos += map->debugfs_reg_len + 2;
104 /* Format the value, write all X if we can't read */
105 ret = regmap_read(map, i, &val);
107 snprintf(buf + buf_pos, count - buf_pos,
108 "%.*x", map->debugfs_val_len, val);
110 memset(buf + buf_pos, 'X',
111 map->debugfs_val_len);
112 buf_pos += 2 * map->format.val_bytes;
114 buf[buf_pos++] = '\n';
116 p += map->debugfs_tot_len;
121 if (copy_to_user(user_buf, buf, buf_pos)) {
133 static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf,
134 size_t count, loff_t *ppos)
136 struct regmap *map = file->private_data;
138 return regmap_read_debugfs(map, 0, map->max_register, user_buf,
142 #undef REGMAP_ALLOW_WRITE_DEBUGFS
143 #ifdef REGMAP_ALLOW_WRITE_DEBUGFS
145 * This can be dangerous especially when we have clients such as
146 * PMICs, therefore don't provide any real compile time configuration option
147 * for this feature, people who want to use this will need to modify
148 * the source code directly.
150 static ssize_t regmap_map_write_file(struct file *file,
151 const char __user *user_buf,
152 size_t count, loff_t *ppos)
157 unsigned long reg, value;
158 struct regmap *map = file->private_data;
160 buf_size = min(count, (sizeof(buf)-1));
161 if (copy_from_user(buf, user_buf, buf_size))
165 while (*start == ' ')
167 reg = simple_strtoul(start, &start, 16);
168 while (*start == ' ')
170 if (strict_strtoul(start, 16, &value))
173 /* Userspace has been fiddling around behind the kernel's back */
174 add_taint(TAINT_USER);
176 regmap_write(map, reg, value);
180 #define regmap_map_write_file NULL
183 static const struct file_operations regmap_map_fops = {
185 .read = regmap_map_read_file,
186 .write = regmap_map_write_file,
187 .llseek = default_llseek,
190 static ssize_t regmap_range_read_file(struct file *file, char __user *user_buf,
191 size_t count, loff_t *ppos)
193 struct regmap_range_node *range = file->private_data;
194 struct regmap *map = range->map;
196 return regmap_read_debugfs(map, range->range_min, range->range_max,
197 user_buf, count, ppos);
200 static const struct file_operations regmap_range_fops = {
202 .read = regmap_range_read_file,
203 .llseek = default_llseek,
206 static ssize_t regmap_access_read_file(struct file *file,
207 char __user *user_buf, size_t count,
210 int reg_len, tot_len;
215 struct regmap *map = file->private_data;
218 if (*ppos < 0 || !count)
221 buf = kmalloc(count, GFP_KERNEL);
225 /* Calculate the length of a fixed format */
226 reg_len = regmap_calc_reg_len(map->max_register, buf, count);
227 tot_len = reg_len + 10; /* ': R W V P\n' */
229 for (i = 0; i <= map->max_register; i += map->reg_stride) {
230 /* Ignore registers which are neither readable nor writable */
231 if (!regmap_readable(map, i) && !regmap_writeable(map, i))
234 /* If we're in the region the user is trying to read */
236 /* ...but not beyond it */
237 if (buf_pos >= count - 1 - tot_len)
240 /* Format the register */
241 snprintf(buf + buf_pos, count - buf_pos,
242 "%.*x: %c %c %c %c\n",
244 regmap_readable(map, i) ? 'y' : 'n',
245 regmap_writeable(map, i) ? 'y' : 'n',
246 regmap_volatile(map, i) ? 'y' : 'n',
247 regmap_precious(map, i) ? 'y' : 'n');
256 if (copy_to_user(user_buf, buf, buf_pos)) {
268 static const struct file_operations regmap_access_fops = {
270 .read = regmap_access_read_file,
271 .llseek = default_llseek,
274 void regmap_debugfs_init(struct regmap *map, const char *name)
276 struct rb_node *next;
277 struct regmap_range_node *range_node;
280 map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s",
281 dev_name(map->dev), name);
282 name = map->debugfs_name;
284 name = dev_name(map->dev);
287 map->debugfs = debugfs_create_dir(name, regmap_debugfs_root);
289 dev_warn(map->dev, "Failed to create debugfs directory\n");
293 debugfs_create_file("name", 0400, map->debugfs,
294 map, ®map_name_fops);
296 if (map->max_register) {
297 debugfs_create_file("registers", 0400, map->debugfs,
298 map, ®map_map_fops);
299 debugfs_create_file("access", 0400, map->debugfs,
300 map, ®map_access_fops);
303 if (map->cache_type) {
304 debugfs_create_bool("cache_only", 0400, map->debugfs,
306 debugfs_create_bool("cache_dirty", 0400, map->debugfs,
308 debugfs_create_bool("cache_bypass", 0400, map->debugfs,
312 next = rb_first(&map->range_tree);
314 range_node = rb_entry(next, struct regmap_range_node, node);
316 if (range_node->name)
317 debugfs_create_file(range_node->name, 0400,
318 map->debugfs, range_node,
321 next = rb_next(&range_node->node);
325 void regmap_debugfs_exit(struct regmap *map)
327 debugfs_remove_recursive(map->debugfs);
328 kfree(map->debugfs_name);
331 void regmap_debugfs_initcall(void)
333 regmap_debugfs_root = debugfs_create_dir("regmap", NULL);
334 if (!regmap_debugfs_root) {
335 pr_warn("regmap: Failed to create debugfs root\n");