blob: 46a213a596e29ec5ae142477e40c563744f49ede [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 Browne8d65392013-01-08 18:47:52 +0000119 /* Close the last entry off if we didn't scan beyond it */
120 if (c) {
121 c->max = p - 1;
122 list_add_tail(&c->list,
123 &map->debugfs_off_cache);
124 } else {
125 return base;
126 }
127
Mark Brown5bd9f4b2013-01-08 13:44:50 +0000128 /*
129 * This should never happen; we return above if we fail to
130 * allocate and we should never be in this code if there are
131 * no registers at all.
132 */
133 if (list_empty(&map->debugfs_off_cache)) {
134 WARN_ON(list_empty(&map->debugfs_off_cache));
135 return base;
136 }
137
Mark Brown5166b7c2012-12-11 01:24:29 +0900138 /* Find the relevant block */
139 list_for_each_entry(c, &map->debugfs_off_cache, list) {
Mark Brown5a1d6d12013-01-08 20:40:19 +0000140 if (from >= c->min && from <= c->max) {
Mark Brown5166b7c2012-12-11 01:24:29 +0900141 *pos = c->min;
142 return c->base_reg;
143 }
144
Mark Brown120f8052013-01-02 15:32:00 +0000145 *pos = c->min;
146 ret = c->base_reg;
Mark Brown5166b7c2012-12-11 01:24:29 +0900147 }
148
149 return ret;
Mark Brownafab2f72012-12-09 17:20:10 +0900150}
151
Mark Brownbd9cc122012-10-03 12:45:37 +0100152static ssize_t regmap_read_debugfs(struct regmap *map, unsigned int from,
153 unsigned int to, char __user *user_buf,
154 size_t count, loff_t *ppos)
Mark Brown31244e32011-07-20 22:56:53 +0100155{
Mark Brown31244e32011-07-20 22:56:53 +0100156 size_t buf_pos = 0;
Mark Brownafab2f72012-12-09 17:20:10 +0900157 loff_t p = *ppos;
Mark Brown31244e32011-07-20 22:56:53 +0100158 ssize_t ret;
159 int i;
Mark Brown31244e32011-07-20 22:56:53 +0100160 char *buf;
Mark Brownafab2f72012-12-09 17:20:10 +0900161 unsigned int val, start_reg;
Mark Brown31244e32011-07-20 22:56:53 +0100162
163 if (*ppos < 0 || !count)
164 return -EINVAL;
165
166 buf = kmalloc(count, GFP_KERNEL);
167 if (!buf)
168 return -ENOMEM;
169
170 /* Calculate the length of a fixed format */
Mark Browncbc19382012-12-06 13:29:05 +0900171 if (!map->debugfs_tot_len) {
172 map->debugfs_reg_len = regmap_calc_reg_len(map->max_register,
173 buf, count);
174 map->debugfs_val_len = 2 * map->format.val_bytes;
175 map->debugfs_tot_len = map->debugfs_reg_len +
176 map->debugfs_val_len + 3; /* : \n */
177 }
Mark Brown31244e32011-07-20 22:56:53 +0100178
Mark Brownafab2f72012-12-09 17:20:10 +0900179 /* Work out which register we're starting at */
180 start_reg = regmap_debugfs_get_dump_start(map, from, *ppos, &p);
181
182 for (i = start_reg; i <= to; i += map->reg_stride) {
Mark Brown8de2f082011-08-10 17:14:41 +0900183 if (!regmap_readable(map, i))
Mark Brown31244e32011-07-20 22:56:53 +0100184 continue;
185
Mark Brown8de2f082011-08-10 17:14:41 +0900186 if (regmap_precious(map, i))
Mark Brown2efe1642011-08-08 15:41:46 +0900187 continue;
188
Mark Brown31244e32011-07-20 22:56:53 +0100189 /* If we're in the region the user is trying to read */
190 if (p >= *ppos) {
191 /* ...but not beyond it */
Mark Browndb043282012-12-11 01:14:11 +0900192 if (buf_pos + 1 + map->debugfs_tot_len >= count)
Mark Brown31244e32011-07-20 22:56:53 +0100193 break;
194
195 /* Format the register */
196 snprintf(buf + buf_pos, count - buf_pos, "%.*x: ",
Mark Browncbc19382012-12-06 13:29:05 +0900197 map->debugfs_reg_len, i - from);
198 buf_pos += map->debugfs_reg_len + 2;
Mark Brown31244e32011-07-20 22:56:53 +0100199
200 /* Format the value, write all X if we can't read */
201 ret = regmap_read(map, i, &val);
202 if (ret == 0)
203 snprintf(buf + buf_pos, count - buf_pos,
Mark Browncbc19382012-12-06 13:29:05 +0900204 "%.*x", map->debugfs_val_len, val);
Mark Brown31244e32011-07-20 22:56:53 +0100205 else
Mark Browncbc19382012-12-06 13:29:05 +0900206 memset(buf + buf_pos, 'X',
207 map->debugfs_val_len);
Mark Brown31244e32011-07-20 22:56:53 +0100208 buf_pos += 2 * map->format.val_bytes;
209
210 buf[buf_pos++] = '\n';
211 }
Mark Browncbc19382012-12-06 13:29:05 +0900212 p += map->debugfs_tot_len;
Mark Brown31244e32011-07-20 22:56:53 +0100213 }
214
215 ret = buf_pos;
216
217 if (copy_to_user(user_buf, buf, buf_pos)) {
218 ret = -EFAULT;
219 goto out;
220 }
221
222 *ppos += buf_pos;
223
224out:
225 kfree(buf);
226 return ret;
227}
228
Mark Brownbd9cc122012-10-03 12:45:37 +0100229static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf,
230 size_t count, loff_t *ppos)
231{
232 struct regmap *map = file->private_data;
233
234 return regmap_read_debugfs(map, 0, map->max_register, user_buf,
235 count, ppos);
236}
237
Dimitris Papastamos09c6ecd2012-02-22 12:43:50 +0000238#undef REGMAP_ALLOW_WRITE_DEBUGFS
239#ifdef REGMAP_ALLOW_WRITE_DEBUGFS
240/*
241 * This can be dangerous especially when we have clients such as
242 * PMICs, therefore don't provide any real compile time configuration option
243 * for this feature, people who want to use this will need to modify
244 * the source code directly.
245 */
246static ssize_t regmap_map_write_file(struct file *file,
247 const char __user *user_buf,
248 size_t count, loff_t *ppos)
249{
250 char buf[32];
251 size_t buf_size;
252 char *start = buf;
253 unsigned long reg, value;
254 struct regmap *map = file->private_data;
255
256 buf_size = min(count, (sizeof(buf)-1));
257 if (copy_from_user(buf, user_buf, buf_size))
258 return -EFAULT;
259 buf[buf_size] = 0;
260
261 while (*start == ' ')
262 start++;
263 reg = simple_strtoul(start, &start, 16);
264 while (*start == ' ')
265 start++;
266 if (strict_strtoul(start, 16, &value))
267 return -EINVAL;
268
269 /* Userspace has been fiddling around behind the kernel's back */
270 add_taint(TAINT_USER);
271
272 regmap_write(map, reg, value);
273 return buf_size;
274}
275#else
276#define regmap_map_write_file NULL
277#endif
278
Mark Brown31244e32011-07-20 22:56:53 +0100279static const struct file_operations regmap_map_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700280 .open = simple_open,
Mark Brown31244e32011-07-20 22:56:53 +0100281 .read = regmap_map_read_file,
Dimitris Papastamos09c6ecd2012-02-22 12:43:50 +0000282 .write = regmap_map_write_file,
Mark Brown31244e32011-07-20 22:56:53 +0100283 .llseek = default_llseek,
284};
285
Mark Brown4b020b32012-10-03 13:13:16 +0100286static ssize_t regmap_range_read_file(struct file *file, char __user *user_buf,
287 size_t count, loff_t *ppos)
288{
289 struct regmap_range_node *range = file->private_data;
290 struct regmap *map = range->map;
291
292 return regmap_read_debugfs(map, range->range_min, range->range_max,
293 user_buf, count, ppos);
294}
295
296static const struct file_operations regmap_range_fops = {
297 .open = simple_open,
298 .read = regmap_range_read_file,
299 .llseek = default_llseek,
300};
301
Mark Brown449e3842011-08-10 17:28:04 +0900302static ssize_t regmap_access_read_file(struct file *file,
303 char __user *user_buf, size_t count,
304 loff_t *ppos)
305{
306 int reg_len, tot_len;
307 size_t buf_pos = 0;
308 loff_t p = 0;
309 ssize_t ret;
310 int i;
311 struct regmap *map = file->private_data;
312 char *buf;
313
314 if (*ppos < 0 || !count)
315 return -EINVAL;
316
317 buf = kmalloc(count, GFP_KERNEL);
318 if (!buf)
319 return -ENOMEM;
320
321 /* Calculate the length of a fixed format */
322 reg_len = regmap_calc_reg_len(map->max_register, buf, count);
323 tot_len = reg_len + 10; /* ': R W V P\n' */
324
Stephen Warrenf01ee602012-04-09 13:40:24 -0600325 for (i = 0; i <= map->max_register; i += map->reg_stride) {
Mark Brown449e3842011-08-10 17:28:04 +0900326 /* Ignore registers which are neither readable nor writable */
327 if (!regmap_readable(map, i) && !regmap_writeable(map, i))
328 continue;
329
330 /* If we're in the region the user is trying to read */
331 if (p >= *ppos) {
332 /* ...but not beyond it */
333 if (buf_pos >= count - 1 - tot_len)
334 break;
335
336 /* Format the register */
337 snprintf(buf + buf_pos, count - buf_pos,
338 "%.*x: %c %c %c %c\n",
339 reg_len, i,
340 regmap_readable(map, i) ? 'y' : 'n',
341 regmap_writeable(map, i) ? 'y' : 'n',
342 regmap_volatile(map, i) ? 'y' : 'n',
343 regmap_precious(map, i) ? 'y' : 'n');
344
345 buf_pos += tot_len;
346 }
347 p += tot_len;
348 }
349
350 ret = buf_pos;
351
352 if (copy_to_user(user_buf, buf, buf_pos)) {
353 ret = -EFAULT;
354 goto out;
355 }
356
357 *ppos += buf_pos;
358
359out:
360 kfree(buf);
361 return ret;
362}
363
364static const struct file_operations regmap_access_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700365 .open = simple_open,
Mark Brown449e3842011-08-10 17:28:04 +0900366 .read = regmap_access_read_file,
367 .llseek = default_llseek,
368};
Mark Brown31244e32011-07-20 22:56:53 +0100369
Stephen Warrend3c242e2012-04-04 15:48:29 -0600370void regmap_debugfs_init(struct regmap *map, const char *name)
Mark Brown31244e32011-07-20 22:56:53 +0100371{
Mark Brown4b020b32012-10-03 13:13:16 +0100372 struct rb_node *next;
373 struct regmap_range_node *range_node;
374
Mark Brown5166b7c2012-12-11 01:24:29 +0900375 INIT_LIST_HEAD(&map->debugfs_off_cache);
376
Stephen Warrend3c242e2012-04-04 15:48:29 -0600377 if (name) {
378 map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s",
379 dev_name(map->dev), name);
380 name = map->debugfs_name;
381 } else {
382 name = dev_name(map->dev);
383 }
384
385 map->debugfs = debugfs_create_dir(name, regmap_debugfs_root);
Mark Brown31244e32011-07-20 22:56:53 +0100386 if (!map->debugfs) {
387 dev_warn(map->dev, "Failed to create debugfs directory\n");
388 return;
389 }
390
Dimitris Papastamosf0c23192012-02-22 14:20:09 +0000391 debugfs_create_file("name", 0400, map->debugfs,
392 map, &regmap_name_fops);
393
Mark Brown449e3842011-08-10 17:28:04 +0900394 if (map->max_register) {
Mark Brown31244e32011-07-20 22:56:53 +0100395 debugfs_create_file("registers", 0400, map->debugfs,
396 map, &regmap_map_fops);
Mark Brown449e3842011-08-10 17:28:04 +0900397 debugfs_create_file("access", 0400, map->debugfs,
398 map, &regmap_access_fops);
399 }
Mark Brown028a01e2012-02-06 18:02:06 +0000400
401 if (map->cache_type) {
402 debugfs_create_bool("cache_only", 0400, map->debugfs,
403 &map->cache_only);
404 debugfs_create_bool("cache_dirty", 0400, map->debugfs,
405 &map->cache_dirty);
406 debugfs_create_bool("cache_bypass", 0400, map->debugfs,
407 &map->cache_bypass);
408 }
Mark Brown4b020b32012-10-03 13:13:16 +0100409
410 next = rb_first(&map->range_tree);
411 while (next) {
412 range_node = rb_entry(next, struct regmap_range_node, node);
413
414 if (range_node->name)
415 debugfs_create_file(range_node->name, 0400,
416 map->debugfs, range_node,
417 &regmap_range_fops);
418
419 next = rb_next(&range_node->node);
420 }
Mark Brown31244e32011-07-20 22:56:53 +0100421}
422
423void regmap_debugfs_exit(struct regmap *map)
424{
425 debugfs_remove_recursive(map->debugfs);
Mark Brown95f971c2013-01-08 13:35:58 +0000426 regmap_debugfs_free_dump_cache(map);
Stephen Warrend3c242e2012-04-04 15:48:29 -0600427 kfree(map->debugfs_name);
Mark Brown31244e32011-07-20 22:56:53 +0100428}
429
430void regmap_debugfs_initcall(void)
431{
432 regmap_debugfs_root = debugfs_create_dir("regmap", NULL);
433 if (!regmap_debugfs_root) {
434 pr_warn("regmap: Failed to create debugfs root\n");
435 return;
436 }
437}