]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/base/regmap/regmap-debugfs.c
Merge tag 'mfd-fixes-4.3' of git://git.kernel.org/pub/scm/linux/kernel/git/lee/mfd
[karo-tx-linux.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, char *buf, size_t buf_size)
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                                                            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
184 static 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)
187 {
188         size_t buf_pos = 0;
189         loff_t p = *ppos;
190         ssize_t ret;
191         int i;
192         char *buf;
193         unsigned int val, start_reg;
194
195         if (*ppos < 0 || !count)
196                 return -EINVAL;
197
198         buf = kmalloc(count, GFP_KERNEL);
199         if (!buf)
200                 return -ENOMEM;
201
202         regmap_calc_tot_len(map, buf, count);
203
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) {
208                 if (!regmap_readable(map, i))
209                         continue;
210
211                 if (regmap_precious(map, i))
212                         continue;
213
214                 /* If we're in the region the user is trying to read */
215                 if (p >= *ppos) {
216                         /* ...but not beyond it */
217                         if (buf_pos + map->debugfs_tot_len > count)
218                                 break;
219
220                         /* Format the register */
221                         snprintf(buf + buf_pos, count - buf_pos, "%.*x: ",
222                                  map->debugfs_reg_len, i - from);
223                         buf_pos += map->debugfs_reg_len + 2;
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,
229                                          "%.*x", map->debugfs_val_len, val);
230                         else
231                                 memset(buf + buf_pos, 'X',
232                                        map->debugfs_val_len);
233                         buf_pos += 2 * map->format.val_bytes;
234
235                         buf[buf_pos++] = '\n';
236                 }
237                 p += map->debugfs_tot_len;
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
249 out:
250         kfree(buf);
251         return ret;
252 }
253
254 static 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
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  */
271 static 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;
280         int ret;
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++;
292         if (kstrtoul(start, 16, &value))
293                 return -EINVAL;
294
295         /* Userspace has been fiddling around behind the kernel's back */
296         add_taint(TAINT_USER, LOCKDEP_STILL_OK);
297
298         ret = regmap_write(map, reg, value);
299         if (ret < 0)
300                 return ret;
301         return buf_size;
302 }
303 #else
304 #define regmap_map_write_file NULL
305 #endif
306
307 static const struct file_operations regmap_map_fops = {
308         .open = simple_open,
309         .read = regmap_map_read_file,
310         .write = regmap_map_write_file,
311         .llseek = default_llseek,
312 };
313
314 static 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
324 static const struct file_operations regmap_range_fops = {
325         .open = simple_open,
326         .read = regmap_range_read_file,
327         .llseek = default_llseek,
328 };
329
330 static 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;
392 out_buf:
393         kfree(buf);
394         return ret;
395 }
396
397 static 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
403 static 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
426         for (i = 0; i <= map->max_register; i += map->reg_stride) {
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 */
434                         if (buf_pos + tot_len + 1 >= count)
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
460 out:
461         kfree(buf);
462         return ret;
463 }
464
465 static const struct file_operations regmap_access_fops = {
466         .open = simple_open,
467         .read = regmap_access_read_file,
468         .llseek = default_llseek,
469 };
470
471 static 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
510 static 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
516 static 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
540 out:
541         map->unlock(map->lock_arg);
542
543         return result;
544 }
545
546 static 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
552 void regmap_debugfs_init(struct regmap *map, const char *name)
553 {
554         struct rb_node *next;
555         struct regmap_range_node *range_node;
556         const char *devname = "dummy";
557
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
572         INIT_LIST_HEAD(&map->debugfs_off_cache);
573         mutex_init(&map->cache_lock);
574
575         if (map->dev)
576                 devname = dev_name(map->dev);
577
578         if (name) {
579                 map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s",
580                                               devname, name);
581                 name = map->debugfs_name;
582         } else {
583                 name = devname;
584         }
585
586         map->debugfs = debugfs_create_dir(name, regmap_debugfs_root);
587         if (!map->debugfs) {
588                 dev_warn(map->dev, "Failed to create debugfs directory\n");
589                 return;
590         }
591
592         debugfs_create_file("name", 0400, map->debugfs,
593                             map, &regmap_name_fops);
594
595         debugfs_create_file("range", 0400, map->debugfs,
596                             map, &regmap_reg_ranges_fops);
597
598         if (map->max_register || regmap_readable(map, 0)) {
599                 umode_t registers_mode;
600
601 #if defined(REGMAP_ALLOW_WRITE_DEBUGFS)
602                 registers_mode = 0600;
603 #else
604                 registers_mode = 0400;
605 #endif
606
607                 debugfs_create_file("registers", registers_mode, map->debugfs,
608                                     map, &regmap_map_fops);
609                 debugfs_create_file("access", 0400, map->debugfs,
610                                     map, &regmap_access_fops);
611         }
612
613         if (map->cache_type) {
614                 debugfs_create_file("cache_only", 0600, map->debugfs,
615                                     &map->cache_only, &regmap_cache_only_fops);
616                 debugfs_create_bool("cache_dirty", 0400, map->debugfs,
617                                     &map->cache_dirty);
618                 debugfs_create_file("cache_bypass", 0600, map->debugfs,
619                                     &map->cache_bypass,
620                                     &regmap_cache_bypass_fops);
621         }
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         }
634
635         if (map->cache_ops && map->cache_ops->debugfs_init)
636                 map->cache_ops->debugfs_init(map);
637 }
638
639 void regmap_debugfs_exit(struct regmap *map)
640 {
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         }
660 }
661
662 void regmap_debugfs_initcall(void)
663 {
664         struct regmap_debugfs_node *node, *tmp;
665
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         }
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);
679 }