blob: 00fbd58706b22f1ae54bf33697aa1a730a4e5725 [file] [log] [blame]
Mark Brown31244e32011-07-20 22:56:53 +01001/*
2 * Register map access API - debugfs
3 *
4 * Copyright 2011 Wolfson Microelectronics plc
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
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.
11 */
12
13#include <linux/slab.h>
Mark Brown31244e32011-07-20 22:56:53 +010014#include <linux/mutex.h>
15#include <linux/debugfs.h>
16#include <linux/uaccess.h>
Paul Gortmaker51990e82012-01-22 11:23:42 -050017#include <linux/device.h>
Mark Brown31244e32011-07-20 22:56:53 +010018
19#include "internal.h"
20
21static struct dentry *regmap_debugfs_root;
22
Mark Brown21f55542011-08-10 17:15:31 +090023/* Calculate the length of a fixed format */
24static size_t regmap_calc_reg_len(int max_val, char *buf, size_t buf_size)
25{
26 snprintf(buf, buf_size, "%x", max_val);
27 return strlen(buf);
28}
29
Dimitris Papastamosf0c23192012-02-22 14:20:09 +000030static ssize_t regmap_name_read_file(struct file *file,
31 char __user *user_buf, size_t count,
32 loff_t *ppos)
33{
34 struct regmap *map = file->private_data;
35 int ret;
36 char *buf;
37
38 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
39 if (!buf)
40 return -ENOMEM;
41
42 ret = snprintf(buf, PAGE_SIZE, "%s\n", map->dev->driver->name);
43 if (ret < 0) {
44 kfree(buf);
45 return ret;
46 }
47
48 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
49 kfree(buf);
50 return ret;
51}
52
53static const struct file_operations regmap_name_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -070054 .open = simple_open,
Dimitris Papastamosf0c23192012-02-22 14:20:09 +000055 .read = regmap_name_read_file,
56 .llseek = default_llseek,
57};
58
Mark Brownbd9cc122012-10-03 12:45:37 +010059static 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)
Mark Brown31244e32011-07-20 22:56:53 +010062{
Mark Brown31244e32011-07-20 22:56:53 +010063 size_t buf_pos = 0;
64 loff_t p = 0;
65 ssize_t ret;
66 int i;
Mark Brown31244e32011-07-20 22:56:53 +010067 char *buf;
68 unsigned int val;
69
70 if (*ppos < 0 || !count)
71 return -EINVAL;
72
73 buf = kmalloc(count, GFP_KERNEL);
74 if (!buf)
75 return -ENOMEM;
76
77 /* Calculate the length of a fixed format */
Mark Browncbc19382012-12-06 13:29:05 +090078 if (!map->debugfs_tot_len) {
79 map->debugfs_reg_len = regmap_calc_reg_len(map->max_register,
80 buf, count);
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 */
84 }
Mark Brown31244e32011-07-20 22:56:53 +010085
Mark Brownbd9cc122012-10-03 12:45:37 +010086 for (i = from; i <= to; i += map->reg_stride) {
Mark Brown8de2f082011-08-10 17:14:41 +090087 if (!regmap_readable(map, i))
Mark Brown31244e32011-07-20 22:56:53 +010088 continue;
89
Mark Brown8de2f082011-08-10 17:14:41 +090090 if (regmap_precious(map, i))
Mark Brown2efe1642011-08-08 15:41:46 +090091 continue;
92
Mark Brown31244e32011-07-20 22:56:53 +010093 /* If we're in the region the user is trying to read */
94 if (p >= *ppos) {
95 /* ...but not beyond it */
Mark Browncbc19382012-12-06 13:29:05 +090096 if (buf_pos >= count - 1 - map->debugfs_tot_len)
Mark Brown31244e32011-07-20 22:56:53 +010097 break;
98
99 /* Format the register */
100 snprintf(buf + buf_pos, count - buf_pos, "%.*x: ",
Mark Browncbc19382012-12-06 13:29:05 +0900101 map->debugfs_reg_len, i - from);
102 buf_pos += map->debugfs_reg_len + 2;
Mark Brown31244e32011-07-20 22:56:53 +0100103
104 /* Format the value, write all X if we can't read */
105 ret = regmap_read(map, i, &val);
106 if (ret == 0)
107 snprintf(buf + buf_pos, count - buf_pos,
Mark Browncbc19382012-12-06 13:29:05 +0900108 "%.*x", map->debugfs_val_len, val);
Mark Brown31244e32011-07-20 22:56:53 +0100109 else
Mark Browncbc19382012-12-06 13:29:05 +0900110 memset(buf + buf_pos, 'X',
111 map->debugfs_val_len);
Mark Brown31244e32011-07-20 22:56:53 +0100112 buf_pos += 2 * map->format.val_bytes;
113
114 buf[buf_pos++] = '\n';
115 }
Mark Browncbc19382012-12-06 13:29:05 +0900116 p += map->debugfs_tot_len;
Mark Brown31244e32011-07-20 22:56:53 +0100117 }
118
119 ret = buf_pos;
120
121 if (copy_to_user(user_buf, buf, buf_pos)) {
122 ret = -EFAULT;
123 goto out;
124 }
125
126 *ppos += buf_pos;
127
128out:
129 kfree(buf);
130 return ret;
131}
132
Mark Brownbd9cc122012-10-03 12:45:37 +0100133static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf,
134 size_t count, loff_t *ppos)
135{
136 struct regmap *map = file->private_data;
137
138 return regmap_read_debugfs(map, 0, map->max_register, user_buf,
139 count, ppos);
140}
141
Dimitris Papastamos09c6ecd2012-02-22 12:43:50 +0000142#undef REGMAP_ALLOW_WRITE_DEBUGFS
143#ifdef REGMAP_ALLOW_WRITE_DEBUGFS
144/*
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.
149 */
150static ssize_t regmap_map_write_file(struct file *file,
151 const char __user *user_buf,
152 size_t count, loff_t *ppos)
153{
154 char buf[32];
155 size_t buf_size;
156 char *start = buf;
157 unsigned long reg, value;
158 struct regmap *map = file->private_data;
159
160 buf_size = min(count, (sizeof(buf)-1));
161 if (copy_from_user(buf, user_buf, buf_size))
162 return -EFAULT;
163 buf[buf_size] = 0;
164
165 while (*start == ' ')
166 start++;
167 reg = simple_strtoul(start, &start, 16);
168 while (*start == ' ')
169 start++;
170 if (strict_strtoul(start, 16, &value))
171 return -EINVAL;
172
173 /* Userspace has been fiddling around behind the kernel's back */
174 add_taint(TAINT_USER);
175
176 regmap_write(map, reg, value);
177 return buf_size;
178}
179#else
180#define regmap_map_write_file NULL
181#endif
182
Mark Brown31244e32011-07-20 22:56:53 +0100183static const struct file_operations regmap_map_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700184 .open = simple_open,
Mark Brown31244e32011-07-20 22:56:53 +0100185 .read = regmap_map_read_file,
Dimitris Papastamos09c6ecd2012-02-22 12:43:50 +0000186 .write = regmap_map_write_file,
Mark Brown31244e32011-07-20 22:56:53 +0100187 .llseek = default_llseek,
188};
189
Mark Brown4b020b32012-10-03 13:13:16 +0100190static ssize_t regmap_range_read_file(struct file *file, char __user *user_buf,
191 size_t count, loff_t *ppos)
192{
193 struct regmap_range_node *range = file->private_data;
194 struct regmap *map = range->map;
195
196 return regmap_read_debugfs(map, range->range_min, range->range_max,
197 user_buf, count, ppos);
198}
199
200static const struct file_operations regmap_range_fops = {
201 .open = simple_open,
202 .read = regmap_range_read_file,
203 .llseek = default_llseek,
204};
205
Mark Brown449e3842011-08-10 17:28:04 +0900206static ssize_t regmap_access_read_file(struct file *file,
207 char __user *user_buf, size_t count,
208 loff_t *ppos)
209{
210 int reg_len, tot_len;
211 size_t buf_pos = 0;
212 loff_t p = 0;
213 ssize_t ret;
214 int i;
215 struct regmap *map = file->private_data;
216 char *buf;
217
218 if (*ppos < 0 || !count)
219 return -EINVAL;
220
221 buf = kmalloc(count, GFP_KERNEL);
222 if (!buf)
223 return -ENOMEM;
224
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' */
228
Stephen Warrenf01ee602012-04-09 13:40:24 -0600229 for (i = 0; i <= map->max_register; i += map->reg_stride) {
Mark Brown449e3842011-08-10 17:28:04 +0900230 /* Ignore registers which are neither readable nor writable */
231 if (!regmap_readable(map, i) && !regmap_writeable(map, i))
232 continue;
233
234 /* If we're in the region the user is trying to read */
235 if (p >= *ppos) {
236 /* ...but not beyond it */
237 if (buf_pos >= count - 1 - tot_len)
238 break;
239
240 /* Format the register */
241 snprintf(buf + buf_pos, count - buf_pos,
242 "%.*x: %c %c %c %c\n",
243 reg_len, i,
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');
248
249 buf_pos += tot_len;
250 }
251 p += tot_len;
252 }
253
254 ret = buf_pos;
255
256 if (copy_to_user(user_buf, buf, buf_pos)) {
257 ret = -EFAULT;
258 goto out;
259 }
260
261 *ppos += buf_pos;
262
263out:
264 kfree(buf);
265 return ret;
266}
267
268static const struct file_operations regmap_access_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700269 .open = simple_open,
Mark Brown449e3842011-08-10 17:28:04 +0900270 .read = regmap_access_read_file,
271 .llseek = default_llseek,
272};
Mark Brown31244e32011-07-20 22:56:53 +0100273
Stephen Warrend3c242e2012-04-04 15:48:29 -0600274void regmap_debugfs_init(struct regmap *map, const char *name)
Mark Brown31244e32011-07-20 22:56:53 +0100275{
Mark Brown4b020b32012-10-03 13:13:16 +0100276 struct rb_node *next;
277 struct regmap_range_node *range_node;
278
Stephen Warrend3c242e2012-04-04 15:48:29 -0600279 if (name) {
280 map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s",
281 dev_name(map->dev), name);
282 name = map->debugfs_name;
283 } else {
284 name = dev_name(map->dev);
285 }
286
287 map->debugfs = debugfs_create_dir(name, regmap_debugfs_root);
Mark Brown31244e32011-07-20 22:56:53 +0100288 if (!map->debugfs) {
289 dev_warn(map->dev, "Failed to create debugfs directory\n");
290 return;
291 }
292
Dimitris Papastamosf0c23192012-02-22 14:20:09 +0000293 debugfs_create_file("name", 0400, map->debugfs,
294 map, &regmap_name_fops);
295
Mark Brown449e3842011-08-10 17:28:04 +0900296 if (map->max_register) {
Mark Brown31244e32011-07-20 22:56:53 +0100297 debugfs_create_file("registers", 0400, map->debugfs,
298 map, &regmap_map_fops);
Mark Brown449e3842011-08-10 17:28:04 +0900299 debugfs_create_file("access", 0400, map->debugfs,
300 map, &regmap_access_fops);
301 }
Mark Brown028a01e2012-02-06 18:02:06 +0000302
303 if (map->cache_type) {
304 debugfs_create_bool("cache_only", 0400, map->debugfs,
305 &map->cache_only);
306 debugfs_create_bool("cache_dirty", 0400, map->debugfs,
307 &map->cache_dirty);
308 debugfs_create_bool("cache_bypass", 0400, map->debugfs,
309 &map->cache_bypass);
310 }
Mark Brown4b020b32012-10-03 13:13:16 +0100311
312 next = rb_first(&map->range_tree);
313 while (next) {
314 range_node = rb_entry(next, struct regmap_range_node, node);
315
316 if (range_node->name)
317 debugfs_create_file(range_node->name, 0400,
318 map->debugfs, range_node,
319 &regmap_range_fops);
320
321 next = rb_next(&range_node->node);
322 }
Mark Brown31244e32011-07-20 22:56:53 +0100323}
324
325void regmap_debugfs_exit(struct regmap *map)
326{
327 debugfs_remove_recursive(map->debugfs);
Stephen Warrend3c242e2012-04-04 15:48:29 -0600328 kfree(map->debugfs_name);
Mark Brown31244e32011-07-20 22:56:53 +0100329}
330
331void regmap_debugfs_initcall(void)
332{
333 regmap_debugfs_root = debugfs_create_dir("regmap", NULL);
334 if (!regmap_debugfs_root) {
335 pr_warn("regmap: Failed to create debugfs root\n");
336 return;
337 }
338}