blob: 9099cd33fbcb87f1364575e2ade64bf307e34ccc [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 Brown5166b7c2012-12-11 01:24:29 +0900119 /* Find the relevant block */
120 list_for_each_entry(c, &map->debugfs_off_cache, list) {
Mark Brown5a1d6d12013-01-08 20:40:19 +0000121 if (from >= c->min && from <= c->max) {
Mark Brown5166b7c2012-12-11 01:24:29 +0900122 *pos = c->min;
123 return c->base_reg;
124 }
125
Mark Brown120f8052013-01-02 15:32:00 +0000126 *pos = c->min;
127 ret = c->base_reg;
Mark Brown5166b7c2012-12-11 01:24:29 +0900128 }
129
130 return ret;
Mark Brownafab2f72012-12-09 17:20:10 +0900131}
132
Mark Brownbd9cc122012-10-03 12:45:37 +0100133static ssize_t regmap_read_debugfs(struct regmap *map, unsigned int from,
134 unsigned int to, char __user *user_buf,
135 size_t count, loff_t *ppos)
Mark Brown31244e32011-07-20 22:56:53 +0100136{
Mark Brown31244e32011-07-20 22:56:53 +0100137 size_t buf_pos = 0;
Mark Brownafab2f72012-12-09 17:20:10 +0900138 loff_t p = *ppos;
Mark Brown31244e32011-07-20 22:56:53 +0100139 ssize_t ret;
140 int i;
Mark Brown31244e32011-07-20 22:56:53 +0100141 char *buf;
Mark Brownafab2f72012-12-09 17:20:10 +0900142 unsigned int val, start_reg;
Mark Brown31244e32011-07-20 22:56:53 +0100143
144 if (*ppos < 0 || !count)
145 return -EINVAL;
146
147 buf = kmalloc(count, GFP_KERNEL);
148 if (!buf)
149 return -ENOMEM;
150
151 /* Calculate the length of a fixed format */
Mark Browncbc19382012-12-06 13:29:05 +0900152 if (!map->debugfs_tot_len) {
153 map->debugfs_reg_len = regmap_calc_reg_len(map->max_register,
154 buf, count);
155 map->debugfs_val_len = 2 * map->format.val_bytes;
156 map->debugfs_tot_len = map->debugfs_reg_len +
157 map->debugfs_val_len + 3; /* : \n */
158 }
Mark Brown31244e32011-07-20 22:56:53 +0100159
Mark Brownafab2f72012-12-09 17:20:10 +0900160 /* Work out which register we're starting at */
161 start_reg = regmap_debugfs_get_dump_start(map, from, *ppos, &p);
162
163 for (i = start_reg; i <= to; i += map->reg_stride) {
Mark Brown8de2f082011-08-10 17:14:41 +0900164 if (!regmap_readable(map, i))
Mark Brown31244e32011-07-20 22:56:53 +0100165 continue;
166
Mark Brown8de2f082011-08-10 17:14:41 +0900167 if (regmap_precious(map, i))
Mark Brown2efe1642011-08-08 15:41:46 +0900168 continue;
169
Mark Brown31244e32011-07-20 22:56:53 +0100170 /* If we're in the region the user is trying to read */
171 if (p >= *ppos) {
172 /* ...but not beyond it */
Mark Browndb043282012-12-11 01:14:11 +0900173 if (buf_pos + 1 + map->debugfs_tot_len >= count)
Mark Brown31244e32011-07-20 22:56:53 +0100174 break;
175
176 /* Format the register */
177 snprintf(buf + buf_pos, count - buf_pos, "%.*x: ",
Mark Browncbc19382012-12-06 13:29:05 +0900178 map->debugfs_reg_len, i - from);
179 buf_pos += map->debugfs_reg_len + 2;
Mark Brown31244e32011-07-20 22:56:53 +0100180
181 /* Format the value, write all X if we can't read */
182 ret = regmap_read(map, i, &val);
183 if (ret == 0)
184 snprintf(buf + buf_pos, count - buf_pos,
Mark Browncbc19382012-12-06 13:29:05 +0900185 "%.*x", map->debugfs_val_len, val);
Mark Brown31244e32011-07-20 22:56:53 +0100186 else
Mark Browncbc19382012-12-06 13:29:05 +0900187 memset(buf + buf_pos, 'X',
188 map->debugfs_val_len);
Mark Brown31244e32011-07-20 22:56:53 +0100189 buf_pos += 2 * map->format.val_bytes;
190
191 buf[buf_pos++] = '\n';
192 }
Mark Browncbc19382012-12-06 13:29:05 +0900193 p += map->debugfs_tot_len;
Mark Brown31244e32011-07-20 22:56:53 +0100194 }
195
196 ret = buf_pos;
197
198 if (copy_to_user(user_buf, buf, buf_pos)) {
199 ret = -EFAULT;
200 goto out;
201 }
202
203 *ppos += buf_pos;
204
205out:
206 kfree(buf);
207 return ret;
208}
209
Mark Brownbd9cc122012-10-03 12:45:37 +0100210static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf,
211 size_t count, loff_t *ppos)
212{
213 struct regmap *map = file->private_data;
214
215 return regmap_read_debugfs(map, 0, map->max_register, user_buf,
216 count, ppos);
217}
218
Dimitris Papastamos09c6ecd2012-02-22 12:43:50 +0000219#undef REGMAP_ALLOW_WRITE_DEBUGFS
220#ifdef REGMAP_ALLOW_WRITE_DEBUGFS
221/*
222 * This can be dangerous especially when we have clients such as
223 * PMICs, therefore don't provide any real compile time configuration option
224 * for this feature, people who want to use this will need to modify
225 * the source code directly.
226 */
227static ssize_t regmap_map_write_file(struct file *file,
228 const char __user *user_buf,
229 size_t count, loff_t *ppos)
230{
231 char buf[32];
232 size_t buf_size;
233 char *start = buf;
234 unsigned long reg, value;
235 struct regmap *map = file->private_data;
236
237 buf_size = min(count, (sizeof(buf)-1));
238 if (copy_from_user(buf, user_buf, buf_size))
239 return -EFAULT;
240 buf[buf_size] = 0;
241
242 while (*start == ' ')
243 start++;
244 reg = simple_strtoul(start, &start, 16);
245 while (*start == ' ')
246 start++;
247 if (strict_strtoul(start, 16, &value))
248 return -EINVAL;
249
250 /* Userspace has been fiddling around behind the kernel's back */
251 add_taint(TAINT_USER);
252
253 regmap_write(map, reg, value);
254 return buf_size;
255}
256#else
257#define regmap_map_write_file NULL
258#endif
259
Mark Brown31244e32011-07-20 22:56:53 +0100260static const struct file_operations regmap_map_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700261 .open = simple_open,
Mark Brown31244e32011-07-20 22:56:53 +0100262 .read = regmap_map_read_file,
Dimitris Papastamos09c6ecd2012-02-22 12:43:50 +0000263 .write = regmap_map_write_file,
Mark Brown31244e32011-07-20 22:56:53 +0100264 .llseek = default_llseek,
265};
266
Mark Brown4b020b32012-10-03 13:13:16 +0100267static ssize_t regmap_range_read_file(struct file *file, char __user *user_buf,
268 size_t count, loff_t *ppos)
269{
270 struct regmap_range_node *range = file->private_data;
271 struct regmap *map = range->map;
272
273 return regmap_read_debugfs(map, range->range_min, range->range_max,
274 user_buf, count, ppos);
275}
276
277static const struct file_operations regmap_range_fops = {
278 .open = simple_open,
279 .read = regmap_range_read_file,
280 .llseek = default_llseek,
281};
282
Mark Brown449e3842011-08-10 17:28:04 +0900283static ssize_t regmap_access_read_file(struct file *file,
284 char __user *user_buf, size_t count,
285 loff_t *ppos)
286{
287 int reg_len, tot_len;
288 size_t buf_pos = 0;
289 loff_t p = 0;
290 ssize_t ret;
291 int i;
292 struct regmap *map = file->private_data;
293 char *buf;
294
295 if (*ppos < 0 || !count)
296 return -EINVAL;
297
298 buf = kmalloc(count, GFP_KERNEL);
299 if (!buf)
300 return -ENOMEM;
301
302 /* Calculate the length of a fixed format */
303 reg_len = regmap_calc_reg_len(map->max_register, buf, count);
304 tot_len = reg_len + 10; /* ': R W V P\n' */
305
Stephen Warrenf01ee602012-04-09 13:40:24 -0600306 for (i = 0; i <= map->max_register; i += map->reg_stride) {
Mark Brown449e3842011-08-10 17:28:04 +0900307 /* Ignore registers which are neither readable nor writable */
308 if (!regmap_readable(map, i) && !regmap_writeable(map, i))
309 continue;
310
311 /* If we're in the region the user is trying to read */
312 if (p >= *ppos) {
313 /* ...but not beyond it */
314 if (buf_pos >= count - 1 - tot_len)
315 break;
316
317 /* Format the register */
318 snprintf(buf + buf_pos, count - buf_pos,
319 "%.*x: %c %c %c %c\n",
320 reg_len, i,
321 regmap_readable(map, i) ? 'y' : 'n',
322 regmap_writeable(map, i) ? 'y' : 'n',
323 regmap_volatile(map, i) ? 'y' : 'n',
324 regmap_precious(map, i) ? 'y' : 'n');
325
326 buf_pos += tot_len;
327 }
328 p += tot_len;
329 }
330
331 ret = buf_pos;
332
333 if (copy_to_user(user_buf, buf, buf_pos)) {
334 ret = -EFAULT;
335 goto out;
336 }
337
338 *ppos += buf_pos;
339
340out:
341 kfree(buf);
342 return ret;
343}
344
345static const struct file_operations regmap_access_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700346 .open = simple_open,
Mark Brown449e3842011-08-10 17:28:04 +0900347 .read = regmap_access_read_file,
348 .llseek = default_llseek,
349};
Mark Brown31244e32011-07-20 22:56:53 +0100350
Stephen Warrend3c242e2012-04-04 15:48:29 -0600351void regmap_debugfs_init(struct regmap *map, const char *name)
Mark Brown31244e32011-07-20 22:56:53 +0100352{
Mark Brown4b020b32012-10-03 13:13:16 +0100353 struct rb_node *next;
354 struct regmap_range_node *range_node;
355
Mark Brown5166b7c2012-12-11 01:24:29 +0900356 INIT_LIST_HEAD(&map->debugfs_off_cache);
357
Stephen Warrend3c242e2012-04-04 15:48:29 -0600358 if (name) {
359 map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s",
360 dev_name(map->dev), name);
361 name = map->debugfs_name;
362 } else {
363 name = dev_name(map->dev);
364 }
365
366 map->debugfs = debugfs_create_dir(name, regmap_debugfs_root);
Mark Brown31244e32011-07-20 22:56:53 +0100367 if (!map->debugfs) {
368 dev_warn(map->dev, "Failed to create debugfs directory\n");
369 return;
370 }
371
Dimitris Papastamosf0c23192012-02-22 14:20:09 +0000372 debugfs_create_file("name", 0400, map->debugfs,
373 map, &regmap_name_fops);
374
Mark Brown449e3842011-08-10 17:28:04 +0900375 if (map->max_register) {
Mark Brown31244e32011-07-20 22:56:53 +0100376 debugfs_create_file("registers", 0400, map->debugfs,
377 map, &regmap_map_fops);
Mark Brown449e3842011-08-10 17:28:04 +0900378 debugfs_create_file("access", 0400, map->debugfs,
379 map, &regmap_access_fops);
380 }
Mark Brown028a01e2012-02-06 18:02:06 +0000381
382 if (map->cache_type) {
383 debugfs_create_bool("cache_only", 0400, map->debugfs,
384 &map->cache_only);
385 debugfs_create_bool("cache_dirty", 0400, map->debugfs,
386 &map->cache_dirty);
387 debugfs_create_bool("cache_bypass", 0400, map->debugfs,
388 &map->cache_bypass);
389 }
Mark Brown4b020b32012-10-03 13:13:16 +0100390
391 next = rb_first(&map->range_tree);
392 while (next) {
393 range_node = rb_entry(next, struct regmap_range_node, node);
394
395 if (range_node->name)
396 debugfs_create_file(range_node->name, 0400,
397 map->debugfs, range_node,
398 &regmap_range_fops);
399
400 next = rb_next(&range_node->node);
401 }
Mark Brown31244e32011-07-20 22:56:53 +0100402}
403
404void regmap_debugfs_exit(struct regmap *map)
405{
406 debugfs_remove_recursive(map->debugfs);
Mark Brown95f971c2013-01-08 13:35:58 +0000407 regmap_debugfs_free_dump_cache(map);
Stephen Warrend3c242e2012-04-04 15:48:29 -0600408 kfree(map->debugfs_name);
Mark Brown31244e32011-07-20 22:56:53 +0100409}
410
411void regmap_debugfs_initcall(void)
412{
413 regmap_debugfs_root = debugfs_create_dir("regmap", NULL);
414 if (!regmap_debugfs_root) {
415 pr_warn("regmap: Failed to create debugfs root\n");
416 return;
417 }
418}