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