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_map_read_file(struct file *file, char __user *user_buf,
60 size_t count, loff_t *ppos)
62 int reg_len, val_len, tot_len;
67 struct regmap *map = file->private_data;
71 if (*ppos < 0 || !count)
74 buf = kmalloc(count, GFP_KERNEL);
78 /* Calculate the length of a fixed format */
79 reg_len = regmap_calc_reg_len(map->max_register, buf, count);
80 val_len = 2 * map->format.val_bytes;
81 tot_len = reg_len + val_len + 3; /* : \n */
83 for (i = 0; i < map->max_register + 1; i++) {
84 if (!regmap_readable(map, i))
87 if (regmap_precious(map, i))
90 /* If we're in the region the user is trying to read */
92 /* ...but not beyond it */
93 if (buf_pos >= count - 1 - tot_len)
96 /* Format the register */
97 snprintf(buf + buf_pos, count - buf_pos, "%.*x: ",
99 buf_pos += reg_len + 2;
101 /* Format the value, write all X if we can't read */
102 ret = regmap_read(map, i, &val);
104 snprintf(buf + buf_pos, count - buf_pos,
105 "%.*x", val_len, val);
107 memset(buf + buf_pos, 'X', val_len);
108 buf_pos += 2 * map->format.val_bytes;
110 buf[buf_pos++] = '\n';
117 if (copy_to_user(user_buf, buf, buf_pos)) {
129 #undef REGMAP_ALLOW_WRITE_DEBUGFS
130 #ifdef REGMAP_ALLOW_WRITE_DEBUGFS
132 * This can be dangerous especially when we have clients such as
133 * PMICs, therefore don't provide any real compile time configuration option
134 * for this feature, people who want to use this will need to modify
135 * the source code directly.
137 static ssize_t regmap_map_write_file(struct file *file,
138 const char __user *user_buf,
139 size_t count, loff_t *ppos)
144 unsigned long reg, value;
145 struct regmap *map = file->private_data;
147 buf_size = min(count, (sizeof(buf)-1));
148 if (copy_from_user(buf, user_buf, buf_size))
152 while (*start == ' ')
154 reg = simple_strtoul(start, &start, 16);
155 while (*start == ' ')
157 if (strict_strtoul(start, 16, &value))
160 /* Userspace has been fiddling around behind the kernel's back */
161 add_taint(TAINT_USER);
163 regmap_write(map, reg, value);
167 #define regmap_map_write_file NULL
170 static const struct file_operations regmap_map_fops = {
172 .read = regmap_map_read_file,
173 .write = regmap_map_write_file,
174 .llseek = default_llseek,
177 static ssize_t regmap_access_read_file(struct file *file,
178 char __user *user_buf, size_t count,
181 int reg_len, tot_len;
186 struct regmap *map = file->private_data;
189 if (*ppos < 0 || !count)
192 buf = kmalloc(count, GFP_KERNEL);
196 /* Calculate the length of a fixed format */
197 reg_len = regmap_calc_reg_len(map->max_register, buf, count);
198 tot_len = reg_len + 10; /* ': R W V P\n' */
200 for (i = 0; i < map->max_register + 1; i++) {
201 /* Ignore registers which are neither readable nor writable */
202 if (!regmap_readable(map, i) && !regmap_writeable(map, i))
205 /* If we're in the region the user is trying to read */
207 /* ...but not beyond it */
208 if (buf_pos >= count - 1 - tot_len)
211 /* Format the register */
212 snprintf(buf + buf_pos, count - buf_pos,
213 "%.*x: %c %c %c %c\n",
215 regmap_readable(map, i) ? 'y' : 'n',
216 regmap_writeable(map, i) ? 'y' : 'n',
217 regmap_volatile(map, i) ? 'y' : 'n',
218 regmap_precious(map, i) ? 'y' : 'n');
227 if (copy_to_user(user_buf, buf, buf_pos)) {
239 static const struct file_operations regmap_access_fops = {
241 .read = regmap_access_read_file,
242 .llseek = default_llseek,
245 void regmap_debugfs_init(struct regmap *map)
247 map->debugfs = debugfs_create_dir(dev_name(map->dev),
248 regmap_debugfs_root);
250 dev_warn(map->dev, "Failed to create debugfs directory\n");
254 debugfs_create_file("name", 0400, map->debugfs,
255 map, ®map_name_fops);
257 if (map->max_register) {
258 debugfs_create_file("registers", 0400, map->debugfs,
259 map, ®map_map_fops);
260 debugfs_create_file("access", 0400, map->debugfs,
261 map, ®map_access_fops);
264 if (map->cache_type) {
265 debugfs_create_bool("cache_only", 0400, map->debugfs,
267 debugfs_create_bool("cache_dirty", 0400, map->debugfs,
269 debugfs_create_bool("cache_bypass", 0400, map->debugfs,
274 void regmap_debugfs_exit(struct regmap *map)
276 debugfs_remove_recursive(map->debugfs);
279 void regmap_debugfs_initcall(void)
281 regmap_debugfs_root = debugfs_create_dir("regmap", NULL);
282 if (!regmap_debugfs_root) {
283 pr_warn("regmap: Failed to create debugfs root\n");