]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/base/regmap/regmap-debugfs.c
Merge remote-tracking branches 'regulator/topic/act8865', 'regulator/topic/anatop...
[mirror_ubuntu-zesty-kernel.git] / drivers / base / regmap / regmap-debugfs.c
CommitLineData
31244e39
MB
1/*
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>
31244e39
MB
14#include <linux/mutex.h>
15#include <linux/debugfs.h>
16#include <linux/uaccess.h>
51990e82 17#include <linux/device.h>
a52eaeb1 18#include <linux/list.h>
31244e39
MB
19
20#include "internal.h"
21
a52eaeb1
TK
22struct regmap_debugfs_node {
23 struct regmap *map;
24 const char *name;
25 struct list_head link;
26};
27
31244e39 28static struct dentry *regmap_debugfs_root;
a52eaeb1
TK
29static LIST_HEAD(regmap_debugfs_early_list);
30static DEFINE_MUTEX(regmap_debugfs_early_lock);
31244e39 31
21f55544
MB
32/* Calculate the length of a fixed format */
33static size_t regmap_calc_reg_len(int max_val, char *buf, size_t buf_size)
34{
176fc2d5 35 return snprintf(NULL, 0, "%x", max_val);
21f55544
MB
36}
37
f0c2319f
DP
38static ssize_t regmap_name_read_file(struct file *file,
39 char __user *user_buf, size_t count,
40 loff_t *ppos)
41{
42 struct regmap *map = file->private_data;
43 int ret;
44 char *buf;
45
46 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
47 if (!buf)
48 return -ENOMEM;
49
50 ret = snprintf(buf, PAGE_SIZE, "%s\n", map->dev->driver->name);
51 if (ret < 0) {
52 kfree(buf);
53 return ret;
54 }
55
56 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
57 kfree(buf);
58 return ret;
59}
60
61static const struct file_operations regmap_name_fops = {
234e3405 62 .open = simple_open,
f0c2319f
DP
63 .read = regmap_name_read_file,
64 .llseek = default_llseek,
65};
66
95f971c7
MB
67static void regmap_debugfs_free_dump_cache(struct regmap *map)
68{
69 struct regmap_debugfs_off_cache *c;
70
71 while (!list_empty(&map->debugfs_off_cache)) {
72 c = list_first_entry(&map->debugfs_off_cache,
73 struct regmap_debugfs_off_cache,
74 list);
75 list_del(&c->list);
76 kfree(c);
77 }
78}
79
afab2f7b
MB
80/*
81 * Work out where the start offset maps into register numbers, bearing
82 * in mind that we suppress hidden registers.
83 */
84static unsigned int regmap_debugfs_get_dump_start(struct regmap *map,
85 unsigned int base,
86 loff_t from,
87 loff_t *pos)
88{
5166b7c0
MB
89 struct regmap_debugfs_off_cache *c = NULL;
90 loff_t p = 0;
91 unsigned int i, ret;
c2c1ee66
DP
92 unsigned int fpos_offset;
93 unsigned int reg_offset;
5166b7c0 94
d6814a7d 95 /* Suppress the cache if we're using a subrange */
26ee4741
LPC
96 if (base)
97 return base;
d6814a7d 98
5166b7c0
MB
99 /*
100 * If we don't have a cache build one so we don't have to do a
101 * linear scan each time.
102 */
065b4c58 103 mutex_lock(&map->cache_lock);
480738de 104 i = base;
5166b7c0 105 if (list_empty(&map->debugfs_off_cache)) {
480738de 106 for (; i <= map->max_register; i += map->reg_stride) {
5166b7c0
MB
107 /* Skip unprinted registers, closing off cache entry */
108 if (!regmap_readable(map, i) ||
109 regmap_precious(map, i)) {
110 if (c) {
111 c->max = p - 1;
480738de 112 c->max_reg = i - map->reg_stride;
5166b7c0
MB
113 list_add_tail(&c->list,
114 &map->debugfs_off_cache);
115 c = NULL;
116 }
117
118 continue;
119 }
120
121 /* No cache entry? Start a new one */
122 if (!c) {
123 c = kzalloc(sizeof(*c), GFP_KERNEL);
95f971c7
MB
124 if (!c) {
125 regmap_debugfs_free_dump_cache(map);
065b4c58 126 mutex_unlock(&map->cache_lock);
95f971c7
MB
127 return base;
128 }
5166b7c0
MB
129 c->min = p;
130 c->base_reg = i;
131 }
132
133 p += map->debugfs_tot_len;
134 }
135 }
afab2f7b 136
e8d6539c
MB
137 /* Close the last entry off if we didn't scan beyond it */
138 if (c) {
139 c->max = p - 1;
480738de 140 c->max_reg = i - map->reg_stride;
e8d6539c
MB
141 list_add_tail(&c->list,
142 &map->debugfs_off_cache);
e8d6539c
MB
143 }
144
5bd9f4bb
MB
145 /*
146 * This should never happen; we return above if we fail to
147 * allocate and we should never be in this code if there are
148 * no registers at all.
149 */
a3471469
RK
150 WARN_ON(list_empty(&map->debugfs_off_cache));
151 ret = base;
5bd9f4bb 152
cf57d607 153 /* Find the relevant block:offset */
5166b7c0 154 list_for_each_entry(c, &map->debugfs_off_cache, list) {
5a1d6d17 155 if (from >= c->min && from <= c->max) {
cf57d607
DP
156 fpos_offset = from - c->min;
157 reg_offset = fpos_offset / map->debugfs_tot_len;
158 *pos = c->min + (reg_offset * map->debugfs_tot_len);
065b4c58 159 mutex_unlock(&map->cache_lock);
213fa5d9 160 return c->base_reg + (reg_offset * map->reg_stride);
afab2f7b
MB
161 }
162
cf57d607
DP
163 *pos = c->max;
164 ret = c->max_reg;
afab2f7b 165 }
065b4c58 166 mutex_unlock(&map->cache_lock);
afab2f7b 167
5166b7c0 168 return ret;
afab2f7b
MB
169}
170
4dd7c553
DP
171static inline void regmap_calc_tot_len(struct regmap *map,
172 void *buf, size_t count)
173{
174 /* Calculate the length of a fixed format */
175 if (!map->debugfs_tot_len) {
176 map->debugfs_reg_len = regmap_calc_reg_len(map->max_register,
177 buf, count);
178 map->debugfs_val_len = 2 * map->format.val_bytes;
179 map->debugfs_tot_len = map->debugfs_reg_len +
180 map->debugfs_val_len + 3; /* : \n */
181 }
182}
183
bd9cc12f
MB
184static ssize_t regmap_read_debugfs(struct regmap *map, unsigned int from,
185 unsigned int to, char __user *user_buf,
186 size_t count, loff_t *ppos)
31244e39 187{
31244e39 188 size_t buf_pos = 0;
afab2f7b 189 loff_t p = *ppos;
31244e39
MB
190 ssize_t ret;
191 int i;
31244e39 192 char *buf;
afab2f7b 193 unsigned int val, start_reg;
31244e39
MB
194
195 if (*ppos < 0 || !count)
196 return -EINVAL;
197
198 buf = kmalloc(count, GFP_KERNEL);
199 if (!buf)
200 return -ENOMEM;
201
4dd7c553 202 regmap_calc_tot_len(map, buf, count);
31244e39 203
afab2f7b
MB
204 /* Work out which register we're starting at */
205 start_reg = regmap_debugfs_get_dump_start(map, from, *ppos, &p);
206
207 for (i = start_reg; i <= to; i += map->reg_stride) {
8de2f081 208 if (!regmap_readable(map, i))
31244e39
MB
209 continue;
210
8de2f081 211 if (regmap_precious(map, i))
2efe1642
MB
212 continue;
213
31244e39
MB
214 /* If we're in the region the user is trying to read */
215 if (p >= *ppos) {
216 /* ...but not beyond it */
f3eb8399 217 if (buf_pos + map->debugfs_tot_len > count)
31244e39
MB
218 break;
219
220 /* Format the register */
221 snprintf(buf + buf_pos, count - buf_pos, "%.*x: ",
cbc1938b
MB
222 map->debugfs_reg_len, i - from);
223 buf_pos += map->debugfs_reg_len + 2;
31244e39
MB
224
225 /* Format the value, write all X if we can't read */
226 ret = regmap_read(map, i, &val);
227 if (ret == 0)
228 snprintf(buf + buf_pos, count - buf_pos,
cbc1938b 229 "%.*x", map->debugfs_val_len, val);
31244e39 230 else
cbc1938b
MB
231 memset(buf + buf_pos, 'X',
232 map->debugfs_val_len);
31244e39
MB
233 buf_pos += 2 * map->format.val_bytes;
234
235 buf[buf_pos++] = '\n';
236 }
cbc1938b 237 p += map->debugfs_tot_len;
31244e39
MB
238 }
239
240 ret = buf_pos;
241
242 if (copy_to_user(user_buf, buf, buf_pos)) {
243 ret = -EFAULT;
244 goto out;
245 }
246
247 *ppos += buf_pos;
248
249out:
250 kfree(buf);
251 return ret;
252}
253
bd9cc12f
MB
254static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf,
255 size_t count, loff_t *ppos)
256{
257 struct regmap *map = file->private_data;
258
259 return regmap_read_debugfs(map, 0, map->max_register, user_buf,
260 count, ppos);
261}
262
09c6ecd3
DP
263#undef REGMAP_ALLOW_WRITE_DEBUGFS
264#ifdef REGMAP_ALLOW_WRITE_DEBUGFS
265/*
266 * This can be dangerous especially when we have clients such as
267 * PMICs, therefore don't provide any real compile time configuration option
268 * for this feature, people who want to use this will need to modify
269 * the source code directly.
270 */
271static ssize_t regmap_map_write_file(struct file *file,
272 const char __user *user_buf,
273 size_t count, loff_t *ppos)
274{
275 char buf[32];
276 size_t buf_size;
277 char *start = buf;
278 unsigned long reg, value;
279 struct regmap *map = file->private_data;
68e850d8 280 int ret;
09c6ecd3
DP
281
282 buf_size = min(count, (sizeof(buf)-1));
283 if (copy_from_user(buf, user_buf, buf_size))
284 return -EFAULT;
285 buf[buf_size] = 0;
286
287 while (*start == ' ')
288 start++;
289 reg = simple_strtoul(start, &start, 16);
290 while (*start == ' ')
291 start++;
34da5e67 292 if (kstrtoul(start, 16, &value))
09c6ecd3
DP
293 return -EINVAL;
294
295 /* Userspace has been fiddling around behind the kernel's back */
f9e464a5 296 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
09c6ecd3 297
68e850d8
DP
298 ret = regmap_write(map, reg, value);
299 if (ret < 0)
300 return ret;
09c6ecd3
DP
301 return buf_size;
302}
303#else
304#define regmap_map_write_file NULL
305#endif
306
31244e39 307static const struct file_operations regmap_map_fops = {
234e3405 308 .open = simple_open,
31244e39 309 .read = regmap_map_read_file,
09c6ecd3 310 .write = regmap_map_write_file,
31244e39
MB
311 .llseek = default_llseek,
312};
313
4b020b3f
MB
314static ssize_t regmap_range_read_file(struct file *file, char __user *user_buf,
315 size_t count, loff_t *ppos)
316{
317 struct regmap_range_node *range = file->private_data;
318 struct regmap *map = range->map;
319
320 return regmap_read_debugfs(map, range->range_min, range->range_max,
321 user_buf, count, ppos);
322}
323
324static const struct file_operations regmap_range_fops = {
325 .open = simple_open,
326 .read = regmap_range_read_file,
327 .llseek = default_llseek,
328};
329
065b4c58
DP
330static ssize_t regmap_reg_ranges_read_file(struct file *file,
331 char __user *user_buf, size_t count,
332 loff_t *ppos)
333{
334 struct regmap *map = file->private_data;
335 struct regmap_debugfs_off_cache *c;
336 loff_t p = 0;
337 size_t buf_pos = 0;
338 char *buf;
339 char *entry;
340 int ret;
341
342 if (*ppos < 0 || !count)
343 return -EINVAL;
344
345 buf = kmalloc(count, GFP_KERNEL);
346 if (!buf)
347 return -ENOMEM;
348
349 entry = kmalloc(PAGE_SIZE, GFP_KERNEL);
350 if (!entry) {
351 kfree(buf);
352 return -ENOMEM;
353 }
354
355 /* While we are at it, build the register dump cache
356 * now so the read() operation on the `registers' file
357 * can benefit from using the cache. We do not care
358 * about the file position information that is contained
359 * in the cache, just about the actual register blocks */
360 regmap_calc_tot_len(map, buf, count);
361 regmap_debugfs_get_dump_start(map, 0, *ppos, &p);
362
363 /* Reset file pointer as the fixed-format of the `registers'
364 * file is not compatible with the `range' file */
365 p = 0;
366 mutex_lock(&map->cache_lock);
367 list_for_each_entry(c, &map->debugfs_off_cache, list) {
368 snprintf(entry, PAGE_SIZE, "%x-%x",
369 c->base_reg, c->max_reg);
370 if (p >= *ppos) {
371 if (buf_pos + 1 + strlen(entry) > count)
372 break;
373 snprintf(buf + buf_pos, count - buf_pos,
374 "%s", entry);
375 buf_pos += strlen(entry);
376 buf[buf_pos] = '\n';
377 buf_pos++;
378 }
379 p += strlen(entry) + 1;
380 }
381 mutex_unlock(&map->cache_lock);
382
383 kfree(entry);
384 ret = buf_pos;
385
386 if (copy_to_user(user_buf, buf, buf_pos)) {
387 ret = -EFAULT;
388 goto out_buf;
389 }
390
391 *ppos += buf_pos;
392out_buf:
393 kfree(buf);
394 return ret;
395}
396
397static const struct file_operations regmap_reg_ranges_fops = {
398 .open = simple_open,
399 .read = regmap_reg_ranges_read_file,
400 .llseek = default_llseek,
401};
402
449e3842
MB
403static ssize_t regmap_access_read_file(struct file *file,
404 char __user *user_buf, size_t count,
405 loff_t *ppos)
406{
407 int reg_len, tot_len;
408 size_t buf_pos = 0;
409 loff_t p = 0;
410 ssize_t ret;
411 int i;
412 struct regmap *map = file->private_data;
413 char *buf;
414
415 if (*ppos < 0 || !count)
416 return -EINVAL;
417
418 buf = kmalloc(count, GFP_KERNEL);
419 if (!buf)
420 return -ENOMEM;
421
422 /* Calculate the length of a fixed format */
423 reg_len = regmap_calc_reg_len(map->max_register, buf, count);
424 tot_len = reg_len + 10; /* ': R W V P\n' */
425
f01ee60f 426 for (i = 0; i <= map->max_register; i += map->reg_stride) {
449e3842
MB
427 /* Ignore registers which are neither readable nor writable */
428 if (!regmap_readable(map, i) && !regmap_writeable(map, i))
429 continue;
430
431 /* If we're in the region the user is trying to read */
432 if (p >= *ppos) {
433 /* ...but not beyond it */
b763ec17 434 if (buf_pos + tot_len + 1 >= count)
449e3842
MB
435 break;
436
437 /* Format the register */
438 snprintf(buf + buf_pos, count - buf_pos,
439 "%.*x: %c %c %c %c\n",
440 reg_len, i,
441 regmap_readable(map, i) ? 'y' : 'n',
442 regmap_writeable(map, i) ? 'y' : 'n',
443 regmap_volatile(map, i) ? 'y' : 'n',
444 regmap_precious(map, i) ? 'y' : 'n');
445
446 buf_pos += tot_len;
447 }
448 p += tot_len;
449 }
450
451 ret = buf_pos;
452
453 if (copy_to_user(user_buf, buf, buf_pos)) {
454 ret = -EFAULT;
455 goto out;
456 }
457
458 *ppos += buf_pos;
459
460out:
461 kfree(buf);
462 return ret;
463}
464
465static const struct file_operations regmap_access_fops = {
234e3405 466 .open = simple_open,
449e3842
MB
467 .read = regmap_access_read_file,
468 .llseek = default_llseek,
469};
31244e39 470
d3dc5430
RF
471static ssize_t regmap_cache_only_write_file(struct file *file,
472 const char __user *user_buf,
473 size_t count, loff_t *ppos)
474{
475 struct regmap *map = container_of(file->private_data,
476 struct regmap, cache_only);
477 ssize_t result;
478 bool was_enabled, require_sync = false;
479 int err;
480
481 map->lock(map->lock_arg);
482
483 was_enabled = map->cache_only;
484
485 result = debugfs_write_file_bool(file, user_buf, count, ppos);
486 if (result < 0) {
487 map->unlock(map->lock_arg);
488 return result;
489 }
490
491 if (map->cache_only && !was_enabled) {
492 dev_warn(map->dev, "debugfs cache_only=Y forced\n");
493 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
494 } else if (!map->cache_only && was_enabled) {
495 dev_warn(map->dev, "debugfs cache_only=N forced: syncing cache\n");
496 require_sync = true;
497 }
498
499 map->unlock(map->lock_arg);
500
501 if (require_sync) {
502 err = regcache_sync(map);
503 if (err)
504 dev_err(map->dev, "Failed to sync cache %d\n", err);
505 }
506
507 return result;
508}
509
510static const struct file_operations regmap_cache_only_fops = {
511 .open = simple_open,
512 .read = debugfs_read_file_bool,
513 .write = regmap_cache_only_write_file,
514};
515
516static ssize_t regmap_cache_bypass_write_file(struct file *file,
517 const char __user *user_buf,
518 size_t count, loff_t *ppos)
519{
520 struct regmap *map = container_of(file->private_data,
521 struct regmap, cache_bypass);
522 ssize_t result;
523 bool was_enabled;
524
525 map->lock(map->lock_arg);
526
527 was_enabled = map->cache_bypass;
528
529 result = debugfs_write_file_bool(file, user_buf, count, ppos);
530 if (result < 0)
531 goto out;
532
533 if (map->cache_bypass && !was_enabled) {
534 dev_warn(map->dev, "debugfs cache_bypass=Y forced\n");
535 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
536 } else if (!map->cache_bypass && was_enabled) {
537 dev_warn(map->dev, "debugfs cache_bypass=N forced\n");
538 }
539
540out:
541 map->unlock(map->lock_arg);
542
543 return result;
544}
545
546static const struct file_operations regmap_cache_bypass_fops = {
547 .open = simple_open,
548 .read = debugfs_read_file_bool,
549 .write = regmap_cache_bypass_write_file,
550};
551
d3c242e1 552void regmap_debugfs_init(struct regmap *map, const char *name)
31244e39 553{
4b020b3f
MB
554 struct rb_node *next;
555 struct regmap_range_node *range_node;
2c98e0c1 556 const char *devname = "dummy";
4b020b3f 557
a52eaeb1
TK
558 /* If we don't have the debugfs root yet, postpone init */
559 if (!regmap_debugfs_root) {
560 struct regmap_debugfs_node *node;
561 node = kzalloc(sizeof(*node), GFP_KERNEL);
562 if (!node)
563 return;
564 node->map = map;
565 node->name = name;
566 mutex_lock(&regmap_debugfs_early_lock);
567 list_add(&node->link, &regmap_debugfs_early_list);
568 mutex_unlock(&regmap_debugfs_early_lock);
569 return;
570 }
571
5166b7c0 572 INIT_LIST_HEAD(&map->debugfs_off_cache);
065b4c58 573 mutex_init(&map->cache_lock);
5166b7c0 574
2c98e0c1
XL
575 if (map->dev)
576 devname = dev_name(map->dev);
577
d3c242e1
SW
578 if (name) {
579 map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s",
2c98e0c1 580 devname, name);
d3c242e1
SW
581 name = map->debugfs_name;
582 } else {
2c98e0c1 583 name = devname;
d3c242e1
SW
584 }
585
586 map->debugfs = debugfs_create_dir(name, regmap_debugfs_root);
31244e39
MB
587 if (!map->debugfs) {
588 dev_warn(map->dev, "Failed to create debugfs directory\n");
589 return;
590 }
591
f0c2319f
DP
592 debugfs_create_file("name", 0400, map->debugfs,
593 map, &regmap_name_fops);
594
065b4c58
DP
595 debugfs_create_file("range", 0400, map->debugfs,
596 map, &regmap_reg_ranges_fops);
597
676970da 598 if (map->max_register || regmap_readable(map, 0)) {
ffff7a12
MP
599 umode_t registers_mode;
600
1635e888
AL
601#if defined(REGMAP_ALLOW_WRITE_DEBUGFS)
602 registers_mode = 0600;
603#else
604 registers_mode = 0400;
605#endif
ffff7a12
MP
606
607 debugfs_create_file("registers", registers_mode, map->debugfs,
31244e39 608 map, &regmap_map_fops);
449e3842
MB
609 debugfs_create_file("access", 0400, map->debugfs,
610 map, &regmap_access_fops);
611 }
028a01e6
MB
612
613 if (map->cache_type) {
d3dc5430
RF
614 debugfs_create_file("cache_only", 0600, map->debugfs,
615 &map->cache_only, &regmap_cache_only_fops);
028a01e6
MB
616 debugfs_create_bool("cache_dirty", 0400, map->debugfs,
617 &map->cache_dirty);
d3dc5430
RF
618 debugfs_create_file("cache_bypass", 0600, map->debugfs,
619 &map->cache_bypass,
620 &regmap_cache_bypass_fops);
028a01e6 621 }
4b020b3f
MB
622
623 next = rb_first(&map->range_tree);
624 while (next) {
625 range_node = rb_entry(next, struct regmap_range_node, node);
626
627 if (range_node->name)
628 debugfs_create_file(range_node->name, 0400,
629 map->debugfs, range_node,
630 &regmap_range_fops);
631
632 next = rb_next(&range_node->node);
633 }
5e0cbe78
LPC
634
635 if (map->cache_ops && map->cache_ops->debugfs_init)
636 map->cache_ops->debugfs_init(map);
31244e39
MB
637}
638
639void regmap_debugfs_exit(struct regmap *map)
640{
a52eaeb1
TK
641 if (map->debugfs) {
642 debugfs_remove_recursive(map->debugfs);
643 mutex_lock(&map->cache_lock);
644 regmap_debugfs_free_dump_cache(map);
645 mutex_unlock(&map->cache_lock);
646 kfree(map->debugfs_name);
647 } else {
648 struct regmap_debugfs_node *node, *tmp;
649
650 mutex_lock(&regmap_debugfs_early_lock);
651 list_for_each_entry_safe(node, tmp, &regmap_debugfs_early_list,
652 link) {
653 if (node->map == map) {
654 list_del(&node->link);
655 kfree(node);
656 }
657 }
658 mutex_unlock(&regmap_debugfs_early_lock);
659 }
31244e39
MB
660}
661
662void regmap_debugfs_initcall(void)
663{
a52eaeb1
TK
664 struct regmap_debugfs_node *node, *tmp;
665
31244e39
MB
666 regmap_debugfs_root = debugfs_create_dir("regmap", NULL);
667 if (!regmap_debugfs_root) {
668 pr_warn("regmap: Failed to create debugfs root\n");
669 return;
670 }
a52eaeb1
TK
671
672 mutex_lock(&regmap_debugfs_early_lock);
673 list_for_each_entry_safe(node, tmp, &regmap_debugfs_early_list, link) {
674 regmap_debugfs_init(node->map, node->name);
675 list_del(&node->link);
676 kfree(node);
677 }
678 mutex_unlock(&regmap_debugfs_early_lock);
31244e39 679}