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