blob: 720e14248167b81c6c5b7032d9fcefbc62b7a9b7 [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 Brown95f971c2013-01-08 13:35:58 +000059static void regmap_debugfs_free_dump_cache(struct regmap *map)
60{
61 struct regmap_debugfs_off_cache *c;
62
63 while (!list_empty(&map->debugfs_off_cache)) {
64 c = list_first_entry(&map->debugfs_off_cache,
65 struct regmap_debugfs_off_cache,
66 list);
67 list_del(&c->list);
68 kfree(c);
69 }
70}
71
Mark Brownafab2f72012-12-09 17:20:10 +090072/*
73 * Work out where the start offset maps into register numbers, bearing
74 * in mind that we suppress hidden registers.
75 */
76static unsigned int regmap_debugfs_get_dump_start(struct regmap *map,
77 unsigned int base,
78 loff_t from,
79 loff_t *pos)
80{
Mark Brown5166b7c2012-12-11 01:24:29 +090081 struct regmap_debugfs_off_cache *c = NULL;
82 loff_t p = 0;
83 unsigned int i, ret;
Mark Brownafab2f72012-12-09 17:20:10 +090084
Mark Brown5166b7c2012-12-11 01:24:29 +090085 /*
86 * If we don't have a cache build one so we don't have to do a
87 * linear scan each time.
88 */
89 if (list_empty(&map->debugfs_off_cache)) {
90 for (i = base; i <= map->max_register; i += map->reg_stride) {
91 /* Skip unprinted registers, closing off cache entry */
92 if (!regmap_readable(map, i) ||
93 regmap_precious(map, i)) {
94 if (c) {
95 c->max = p - 1;
96 list_add_tail(&c->list,
97 &map->debugfs_off_cache);
98 c = NULL;
99 }
Mark Brownafab2f72012-12-09 17:20:10 +0900100
Mark Brown5166b7c2012-12-11 01:24:29 +0900101 continue;
102 }
Mark Brownafab2f72012-12-09 17:20:10 +0900103
Mark Brown5166b7c2012-12-11 01:24:29 +0900104 /* No cache entry? Start a new one */
105 if (!c) {
106 c = kzalloc(sizeof(*c), GFP_KERNEL);
Mark Brown95f971c2013-01-08 13:35:58 +0000107 if (!c) {
108 regmap_debugfs_free_dump_cache(map);
109 return base;
110 }
Mark Brown5166b7c2012-12-11 01:24:29 +0900111 c->min = p;
112 c->base_reg = i;
113 }
114
115 p += map->debugfs_tot_len;
Mark Brownafab2f72012-12-09 17:20:10 +0900116 }
Mark Brownafab2f72012-12-09 17:20:10 +0900117 }
118
Mark Brown5bd9f4b2013-01-08 13:44:50 +0000119 /*
120 * This should never happen; we return above if we fail to
121 * allocate and we should never be in this code if there are
122 * no registers at all.
123 */
124 if (list_empty(&map->debugfs_off_cache)) {
125 WARN_ON(list_empty(&map->debugfs_off_cache));
126 return base;
127 }
128
Mark Brown5166b7c2012-12-11 01:24:29 +0900129 /* Find the relevant block */
130 list_for_each_entry(c, &map->debugfs_off_cache, list) {
Mark Brown5a1d6d12013-01-08 20:40:19 +0000131 if (from >= c->min && from <= c->max) {
Mark Brown5166b7c2012-12-11 01:24:29 +0900132 *pos = c->min;
133 return c->base_reg;
134 }
135
Mark Brown120f8052013-01-02 15:32:00 +0000136 *pos = c->min;
137 ret = c->base_reg;
Mark Brown5166b7c2012-12-11 01:24:29 +0900138 }
139
140 return ret;
Mark Brownafab2f72012-12-09 17:20:10 +0900141}
142
Mark Brownbd9cc122012-10-03 12:45:37 +0100143static ssize_t regmap_read_debugfs(struct regmap *map, unsigned int from,
144 unsigned int to, char __user *user_buf,
145 size_t count, loff_t *ppos)
Mark Brown31244e32011-07-20 22:56:53 +0100146{
Mark Brown31244e32011-07-20 22:56:53 +0100147 size_t buf_pos = 0;
Mark Brownafab2f72012-12-09 17:20:10 +0900148 loff_t p = *ppos;
Mark Brown31244e32011-07-20 22:56:53 +0100149 ssize_t ret;
150 int i;
Mark Brown31244e32011-07-20 22:56:53 +0100151 char *buf;
Mark Brownafab2f72012-12-09 17:20:10 +0900152 unsigned int val, start_reg;
Mark Brown31244e32011-07-20 22:56:53 +0100153
154 if (*ppos < 0 || !count)
155 return -EINVAL;
156
157 buf = kmalloc(count, GFP_KERNEL);
158 if (!buf)
159 return -ENOMEM;
160
161 /* Calculate the length of a fixed format */
Mark Browncbc19382012-12-06 13:29:05 +0900162 if (!map->debugfs_tot_len) {
163 map->debugfs_reg_len = regmap_calc_reg_len(map->max_register,
164 buf, count);
165 map->debugfs_val_len = 2 * map->format.val_bytes;
166 map->debugfs_tot_len = map->debugfs_reg_len +
167 map->debugfs_val_len + 3; /* : \n */
168 }
Mark Brown31244e32011-07-20 22:56:53 +0100169
Mark Brownafab2f72012-12-09 17:20:10 +0900170 /* Work out which register we're starting at */
171 start_reg = regmap_debugfs_get_dump_start(map, from, *ppos, &p);
172
173 for (i = start_reg; i <= to; i += map->reg_stride) {
Mark Brown8de2f082011-08-10 17:14:41 +0900174 if (!regmap_readable(map, i))
Mark Brown31244e32011-07-20 22:56:53 +0100175 continue;
176
Mark Brown8de2f082011-08-10 17:14:41 +0900177 if (regmap_precious(map, i))
Mark Brown2efe1642011-08-08 15:41:46 +0900178 continue;
179
Mark Brown31244e32011-07-20 22:56:53 +0100180 /* If we're in the region the user is trying to read */
181 if (p >= *ppos) {
182 /* ...but not beyond it */
Mark Browndb043282012-12-11 01:14:11 +0900183 if (buf_pos + 1 + map->debugfs_tot_len >= count)
Mark Brown31244e32011-07-20 22:56:53 +0100184 break;
185
186 /* Format the register */
187 snprintf(buf + buf_pos, count - buf_pos, "%.*x: ",
Mark Browncbc19382012-12-06 13:29:05 +0900188 map->debugfs_reg_len, i - from);
189 buf_pos += map->debugfs_reg_len + 2;
Mark Brown31244e32011-07-20 22:56:53 +0100190
191 /* Format the value, write all X if we can't read */
192 ret = regmap_read(map, i, &val);
193 if (ret == 0)
194 snprintf(buf + buf_pos, count - buf_pos,
Mark Browncbc19382012-12-06 13:29:05 +0900195 "%.*x", map->debugfs_val_len, val);
Mark Brown31244e32011-07-20 22:56:53 +0100196 else
Mark Browncbc19382012-12-06 13:29:05 +0900197 memset(buf + buf_pos, 'X',
198 map->debugfs_val_len);
Mark Brown31244e32011-07-20 22:56:53 +0100199 buf_pos += 2 * map->format.val_bytes;
200
201 buf[buf_pos++] = '\n';
202 }
Mark Browncbc19382012-12-06 13:29:05 +0900203 p += map->debugfs_tot_len;
Mark Brown31244e32011-07-20 22:56:53 +0100204 }
205
206 ret = buf_pos;
207
208 if (copy_to_user(user_buf, buf, buf_pos)) {
209 ret = -EFAULT;
210 goto out;
211 }
212
213 *ppos += buf_pos;
214
215out:
216 kfree(buf);
217 return ret;
218}
219
Mark Brownbd9cc122012-10-03 12:45:37 +0100220static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf,
221 size_t count, loff_t *ppos)
222{
223 struct regmap *map = file->private_data;
224
225 return regmap_read_debugfs(map, 0, map->max_register, user_buf,
226 count, ppos);
227}
228
Dimitris Papastamos09c6ecd2012-02-22 12:43:50 +0000229#undef REGMAP_ALLOW_WRITE_DEBUGFS
230#ifdef REGMAP_ALLOW_WRITE_DEBUGFS
231/*
232 * This can be dangerous especially when we have clients such as
233 * PMICs, therefore don't provide any real compile time configuration option
234 * for this feature, people who want to use this will need to modify
235 * the source code directly.
236 */
237static ssize_t regmap_map_write_file(struct file *file,
238 const char __user *user_buf,
239 size_t count, loff_t *ppos)
240{
241 char buf[32];
242 size_t buf_size;
243 char *start = buf;
244 unsigned long reg, value;
245 struct regmap *map = file->private_data;
246
247 buf_size = min(count, (sizeof(buf)-1));
248 if (copy_from_user(buf, user_buf, buf_size))
249 return -EFAULT;
250 buf[buf_size] = 0;
251
252 while (*start == ' ')
253 start++;
254 reg = simple_strtoul(start, &start, 16);
255 while (*start == ' ')
256 start++;
257 if (strict_strtoul(start, 16, &value))
258 return -EINVAL;
259
260 /* Userspace has been fiddling around behind the kernel's back */
261 add_taint(TAINT_USER);
262
263 regmap_write(map, reg, value);
264 return buf_size;
265}
266#else
267#define regmap_map_write_file NULL
268#endif
269
Mark Brown31244e32011-07-20 22:56:53 +0100270static const struct file_operations regmap_map_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700271 .open = simple_open,
Mark Brown31244e32011-07-20 22:56:53 +0100272 .read = regmap_map_read_file,
Dimitris Papastamos09c6ecd2012-02-22 12:43:50 +0000273 .write = regmap_map_write_file,
Mark Brown31244e32011-07-20 22:56:53 +0100274 .llseek = default_llseek,
275};
276
Mark Brown4b020b32012-10-03 13:13:16 +0100277static ssize_t regmap_range_read_file(struct file *file, char __user *user_buf,
278 size_t count, loff_t *ppos)
279{
280 struct regmap_range_node *range = file->private_data;
281 struct regmap *map = range->map;
282
283 return regmap_read_debugfs(map, range->range_min, range->range_max,
284 user_buf, count, ppos);
285}
286
287static const struct file_operations regmap_range_fops = {
288 .open = simple_open,
289 .read = regmap_range_read_file,
290 .llseek = default_llseek,
291};
292
Mark Brown449e3842011-08-10 17:28:04 +0900293static ssize_t regmap_access_read_file(struct file *file,
294 char __user *user_buf, size_t count,
295 loff_t *ppos)
296{
297 int reg_len, tot_len;
298 size_t buf_pos = 0;
299 loff_t p = 0;
300 ssize_t ret;
301 int i;
302 struct regmap *map = file->private_data;
303 char *buf;
304
305 if (*ppos < 0 || !count)
306 return -EINVAL;
307
308 buf = kmalloc(count, GFP_KERNEL);
309 if (!buf)
310 return -ENOMEM;
311
312 /* Calculate the length of a fixed format */
313 reg_len = regmap_calc_reg_len(map->max_register, buf, count);
314 tot_len = reg_len + 10; /* ': R W V P\n' */
315
Stephen Warrenf01ee602012-04-09 13:40:24 -0600316 for (i = 0; i <= map->max_register; i += map->reg_stride) {
Mark Brown449e3842011-08-10 17:28:04 +0900317 /* Ignore registers which are neither readable nor writable */
318 if (!regmap_readable(map, i) && !regmap_writeable(map, i))
319 continue;
320
321 /* If we're in the region the user is trying to read */
322 if (p >= *ppos) {
323 /* ...but not beyond it */
324 if (buf_pos >= count - 1 - tot_len)
325 break;
326
327 /* Format the register */
328 snprintf(buf + buf_pos, count - buf_pos,
329 "%.*x: %c %c %c %c\n",
330 reg_len, i,
331 regmap_readable(map, i) ? 'y' : 'n',
332 regmap_writeable(map, i) ? 'y' : 'n',
333 regmap_volatile(map, i) ? 'y' : 'n',
334 regmap_precious(map, i) ? 'y' : 'n');
335
336 buf_pos += tot_len;
337 }
338 p += tot_len;
339 }
340
341 ret = buf_pos;
342
343 if (copy_to_user(user_buf, buf, buf_pos)) {
344 ret = -EFAULT;
345 goto out;
346 }
347
348 *ppos += buf_pos;
349
350out:
351 kfree(buf);
352 return ret;
353}
354
355static const struct file_operations regmap_access_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700356 .open = simple_open,
Mark Brown449e3842011-08-10 17:28:04 +0900357 .read = regmap_access_read_file,
358 .llseek = default_llseek,
359};
Mark Brown31244e32011-07-20 22:56:53 +0100360
Stephen Warrend3c242e2012-04-04 15:48:29 -0600361void regmap_debugfs_init(struct regmap *map, const char *name)
Mark Brown31244e32011-07-20 22:56:53 +0100362{
Mark Brown4b020b32012-10-03 13:13:16 +0100363 struct rb_node *next;
364 struct regmap_range_node *range_node;
365
Mark Brown5166b7c2012-12-11 01:24:29 +0900366 INIT_LIST_HEAD(&map->debugfs_off_cache);
367
Stephen Warrend3c242e2012-04-04 15:48:29 -0600368 if (name) {
369 map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s",
370 dev_name(map->dev), name);
371 name = map->debugfs_name;
372 } else {
373 name = dev_name(map->dev);
374 }
375
376 map->debugfs = debugfs_create_dir(name, regmap_debugfs_root);
Mark Brown31244e32011-07-20 22:56:53 +0100377 if (!map->debugfs) {
378 dev_warn(map->dev, "Failed to create debugfs directory\n");
379 return;
380 }
381
Dimitris Papastamosf0c23192012-02-22 14:20:09 +0000382 debugfs_create_file("name", 0400, map->debugfs,
383 map, &regmap_name_fops);
384
Mark Brown449e3842011-08-10 17:28:04 +0900385 if (map->max_register) {
Mark Brown31244e32011-07-20 22:56:53 +0100386 debugfs_create_file("registers", 0400, map->debugfs,
387 map, &regmap_map_fops);
Mark Brown449e3842011-08-10 17:28:04 +0900388 debugfs_create_file("access", 0400, map->debugfs,
389 map, &regmap_access_fops);
390 }
Mark Brown028a01e2012-02-06 18:02:06 +0000391
392 if (map->cache_type) {
393 debugfs_create_bool("cache_only", 0400, map->debugfs,
394 &map->cache_only);
395 debugfs_create_bool("cache_dirty", 0400, map->debugfs,
396 &map->cache_dirty);
397 debugfs_create_bool("cache_bypass", 0400, map->debugfs,
398 &map->cache_bypass);
399 }
Mark Brown4b020b32012-10-03 13:13:16 +0100400
401 next = rb_first(&map->range_tree);
402 while (next) {
403 range_node = rb_entry(next, struct regmap_range_node, node);
404
405 if (range_node->name)
406 debugfs_create_file(range_node->name, 0400,
407 map->debugfs, range_node,
408 &regmap_range_fops);
409
410 next = rb_next(&range_node->node);
411 }
Mark Brown31244e32011-07-20 22:56:53 +0100412}
413
414void regmap_debugfs_exit(struct regmap *map)
415{
416 debugfs_remove_recursive(map->debugfs);
Mark Brown95f971c2013-01-08 13:35:58 +0000417 regmap_debugfs_free_dump_cache(map);
Stephen Warrend3c242e2012-04-04 15:48:29 -0600418 kfree(map->debugfs_name);
Mark Brown31244e32011-07-20 22:56:53 +0100419}
420
421void regmap_debugfs_initcall(void)
422{
423 regmap_debugfs_root = debugfs_create_dir("regmap", NULL);
424 if (!regmap_debugfs_root) {
425 pr_warn("regmap: Failed to create debugfs root\n");
426 return;
427 }
428}