blob: 36ce3511c733f62193a32fe3b63fd4d195209092 [file] [log] [blame]
Mark Brown31244e32011-07-20 22:56:53 +01001/*
2 * Register map access API - debugfs
3 *
4 * Copyright 2011 Wolfson Microelectronics plc
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/slab.h>
Mark Brown31244e32011-07-20 22:56:53 +010014#include <linux/mutex.h>
15#include <linux/debugfs.h>
16#include <linux/uaccess.h>
Paul Gortmaker51990e82012-01-22 11:23:42 -050017#include <linux/device.h>
Tero Kristoa52eaeb2013-10-24 15:03:41 +030018#include <linux/list.h>
Mark Brown31244e32011-07-20 22:56:53 +010019
20#include "internal.h"
21
Tero Kristoa52eaeb2013-10-24 15:03:41 +030022struct regmap_debugfs_node {
23 struct regmap *map;
24 const char *name;
25 struct list_head link;
26};
27
Mark Brown31244e32011-07-20 22:56:53 +010028static struct dentry *regmap_debugfs_root;
Tero Kristoa52eaeb2013-10-24 15:03:41 +030029static LIST_HEAD(regmap_debugfs_early_list);
30static DEFINE_MUTEX(regmap_debugfs_early_lock);
Mark Brown31244e32011-07-20 22:56:53 +010031
Mark Brown21f55542011-08-10 17:15:31 +090032/* Calculate the length of a fixed format */
Mark Brown9ae31092015-09-19 07:31:47 -070033static size_t regmap_calc_reg_len(int max_val)
Mark Brown21f55542011-08-10 17:15:31 +090034{
Mark Brown176fc2d2015-09-19 07:12:34 -070035 return snprintf(NULL, 0, "%x", max_val);
Mark Brown21f55542011-08-10 17:15:31 +090036}
37
Dimitris Papastamosf0c23192012-02-22 14:20:09 +000038static 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 = {
Stephen Boyd234e3402012-04-05 14:25:11 -070062 .open = simple_open,
Dimitris Papastamosf0c23192012-02-22 14:20:09 +000063 .read = regmap_name_read_file,
64 .llseek = default_llseek,
65};
66
Mark Brown95f971c2013-01-08 13:35:58 +000067static 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
Cristian Birsan359a2f12016-08-08 18:44:22 +030080static 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
Mark Brownafab2f72012-12-09 17:20:10 +090091/*
92 * Work out where the start offset maps into register numbers, bearing
93 * in mind that we suppress hidden registers.
94 */
95static unsigned int regmap_debugfs_get_dump_start(struct regmap *map,
96 unsigned int base,
97 loff_t from,
98 loff_t *pos)
99{
Mark Brown5166b7c2012-12-11 01:24:29 +0900100 struct regmap_debugfs_off_cache *c = NULL;
101 loff_t p = 0;
102 unsigned int i, ret;
Dimitris Papastamosc2c1ee62013-02-08 12:47:14 +0000103 unsigned int fpos_offset;
104 unsigned int reg_offset;
Mark Brownafab2f72012-12-09 17:20:10 +0900105
Mark Brownd6814a72013-05-29 15:54:54 +0100106 /* Suppress the cache if we're using a subrange */
Lars-Peter Clausen26ee4742013-08-28 17:55:07 +0200107 if (base)
108 return base;
Mark Brownd6814a72013-05-29 15:54:54 +0100109
Mark Brown5166b7c2012-12-11 01:24:29 +0900110 /*
111 * If we don't have a cache build one so we don't have to do a
112 * linear scan each time.
113 */
Dimitris Papastamos065b4c52013-02-20 12:15:23 +0000114 mutex_lock(&map->cache_lock);
Dimitris Papastamos480738d2013-02-20 12:15:22 +0000115 i = base;
Mark Brown5166b7c2012-12-11 01:24:29 +0900116 if (list_empty(&map->debugfs_off_cache)) {
Dimitris Papastamos480738d2013-02-20 12:15:22 +0000117 for (; i <= map->max_register; i += map->reg_stride) {
Mark Brown5166b7c2012-12-11 01:24:29 +0900118 /* Skip unprinted registers, closing off cache entry */
Cristian Birsan359a2f12016-08-08 18:44:22 +0300119 if (!regmap_printable(map, i)) {
Mark Brown5166b7c2012-12-11 01:24:29 +0900120 if (c) {
121 c->max = p - 1;
Dimitris Papastamos480738d2013-02-20 12:15:22 +0000122 c->max_reg = i - map->reg_stride;
Mark Brown5166b7c2012-12-11 01:24:29 +0900123 list_add_tail(&c->list,
124 &map->debugfs_off_cache);
125 c = NULL;
126 }
Mark Brownafab2f72012-12-09 17:20:10 +0900127
Mark Brown5166b7c2012-12-11 01:24:29 +0900128 continue;
129 }
Mark Brownafab2f72012-12-09 17:20:10 +0900130
Mark Brown5166b7c2012-12-11 01:24:29 +0900131 /* No cache entry? Start a new one */
132 if (!c) {
133 c = kzalloc(sizeof(*c), GFP_KERNEL);
Mark Brown95f971c2013-01-08 13:35:58 +0000134 if (!c) {
135 regmap_debugfs_free_dump_cache(map);
Dimitris Papastamos065b4c52013-02-20 12:15:23 +0000136 mutex_unlock(&map->cache_lock);
Mark Brown95f971c2013-01-08 13:35:58 +0000137 return base;
138 }
Mark Brown5166b7c2012-12-11 01:24:29 +0900139 c->min = p;
140 c->base_reg = i;
141 }
142
143 p += map->debugfs_tot_len;
Mark Brownafab2f72012-12-09 17:20:10 +0900144 }
Mark Brownafab2f72012-12-09 17:20:10 +0900145 }
146
Mark Browne8d65392013-01-08 18:47:52 +0000147 /* Close the last entry off if we didn't scan beyond it */
148 if (c) {
149 c->max = p - 1;
Dimitris Papastamos480738d2013-02-20 12:15:22 +0000150 c->max_reg = i - map->reg_stride;
Mark Browne8d65392013-01-08 18:47:52 +0000151 list_add_tail(&c->list,
152 &map->debugfs_off_cache);
Mark Browne8d65392013-01-08 18:47:52 +0000153 }
154
Mark Brown5bd9f4b2013-01-08 13:44:50 +0000155 /*
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 */
Russell Kinga3471462013-01-26 11:45:35 +0000160 WARN_ON(list_empty(&map->debugfs_off_cache));
161 ret = base;
Mark Brown5bd9f4b2013-01-08 13:44:50 +0000162
Dimitris Papastamoscf57d602013-02-08 12:47:20 +0000163 /* Find the relevant block:offset */
Mark Brown5166b7c2012-12-11 01:24:29 +0900164 list_for_each_entry(c, &map->debugfs_off_cache, list) {
Mark Brown5a1d6d12013-01-08 20:40:19 +0000165 if (from >= c->min && from <= c->max) {
Dimitris Papastamoscf57d602013-02-08 12:47:20 +0000166 fpos_offset = from - c->min;
167 reg_offset = fpos_offset / map->debugfs_tot_len;
168 *pos = c->min + (reg_offset * map->debugfs_tot_len);
Dimitris Papastamos065b4c52013-02-20 12:15:23 +0000169 mutex_unlock(&map->cache_lock);
Srinivas Kandagatla213fa5d2013-05-14 07:54:23 +0100170 return c->base_reg + (reg_offset * map->reg_stride);
Mark Brown5166b7c2012-12-11 01:24:29 +0900171 }
172
Dimitris Papastamoscf57d602013-02-08 12:47:20 +0000173 *pos = c->max;
174 ret = c->max_reg;
Mark Brown5166b7c2012-12-11 01:24:29 +0900175 }
Dimitris Papastamos065b4c52013-02-20 12:15:23 +0000176 mutex_unlock(&map->cache_lock);
Mark Brown5166b7c2012-12-11 01:24:29 +0900177
178 return ret;
Mark Brownafab2f72012-12-09 17:20:10 +0900179}
180
Dimitris Papastamos4dd7c552013-02-11 10:50:59 +0000181static 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) {
Mark Brown9ae31092015-09-19 07:31:47 -0700186 map->debugfs_reg_len = regmap_calc_reg_len(map->max_register),
Dimitris Papastamos4dd7c552013-02-11 10:50:59 +0000187 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
Mark Brownbd9cc122012-10-03 12:45:37 +0100193static 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)
Mark Brown31244e32011-07-20 22:56:53 +0100196{
Mark Brown31244e32011-07-20 22:56:53 +0100197 size_t buf_pos = 0;
Mark Brownafab2f72012-12-09 17:20:10 +0900198 loff_t p = *ppos;
Mark Brown31244e32011-07-20 22:56:53 +0100199 ssize_t ret;
200 int i;
Mark Brown31244e32011-07-20 22:56:53 +0100201 char *buf;
Mark Brownafab2f72012-12-09 17:20:10 +0900202 unsigned int val, start_reg;
Mark Brown31244e32011-07-20 22:56:53 +0100203
204 if (*ppos < 0 || !count)
205 return -EINVAL;
206
207 buf = kmalloc(count, GFP_KERNEL);
208 if (!buf)
209 return -ENOMEM;
210
Dimitris Papastamos4dd7c552013-02-11 10:50:59 +0000211 regmap_calc_tot_len(map, buf, count);
Mark Brown31244e32011-07-20 22:56:53 +0100212
Mark Brownafab2f72012-12-09 17:20:10 +0900213 /* Work out which register we're starting at */
214 start_reg = regmap_debugfs_get_dump_start(map, from, *ppos, &p);
215
216 for (i = start_reg; i <= to; i += map->reg_stride) {
Cristian Birsan359a2f12016-08-08 18:44:22 +0300217 if (!regmap_readable(map, i) && !regmap_cached(map, i))
Mark Brown31244e32011-07-20 22:56:53 +0100218 continue;
219
Mark Brown8de2f082011-08-10 17:14:41 +0900220 if (regmap_precious(map, i))
Mark Brown2efe1642011-08-08 15:41:46 +0900221 continue;
222
Mark Brown31244e32011-07-20 22:56:53 +0100223 /* If we're in the region the user is trying to read */
224 if (p >= *ppos) {
225 /* ...but not beyond it */
Dimitris Papastamosf3eb8392013-02-07 10:51:56 +0000226 if (buf_pos + map->debugfs_tot_len > count)
Mark Brown31244e32011-07-20 22:56:53 +0100227 break;
228
229 /* Format the register */
230 snprintf(buf + buf_pos, count - buf_pos, "%.*x: ",
Mark Browncbc19382012-12-06 13:29:05 +0900231 map->debugfs_reg_len, i - from);
232 buf_pos += map->debugfs_reg_len + 2;
Mark Brown31244e32011-07-20 22:56:53 +0100233
234 /* Format the value, write all X if we can't read */
235 ret = regmap_read(map, i, &val);
236 if (ret == 0)
237 snprintf(buf + buf_pos, count - buf_pos,
Mark Browncbc19382012-12-06 13:29:05 +0900238 "%.*x", map->debugfs_val_len, val);
Mark Brown31244e32011-07-20 22:56:53 +0100239 else
Mark Browncbc19382012-12-06 13:29:05 +0900240 memset(buf + buf_pos, 'X',
241 map->debugfs_val_len);
Mark Brown31244e32011-07-20 22:56:53 +0100242 buf_pos += 2 * map->format.val_bytes;
243
244 buf[buf_pos++] = '\n';
245 }
Mark Browncbc19382012-12-06 13:29:05 +0900246 p += map->debugfs_tot_len;
Mark Brown31244e32011-07-20 22:56:53 +0100247 }
248
249 ret = buf_pos;
250
251 if (copy_to_user(user_buf, buf, buf_pos)) {
252 ret = -EFAULT;
253 goto out;
254 }
255
256 *ppos += buf_pos;
257
258out:
259 kfree(buf);
260 return ret;
261}
262
Mark Brownbd9cc122012-10-03 12:45:37 +0100263static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf,
264 size_t count, loff_t *ppos)
265{
266 struct regmap *map = file->private_data;
267
268 return regmap_read_debugfs(map, 0, map->max_register, user_buf,
269 count, ppos);
270}
271
Dimitris Papastamos09c6ecd2012-02-22 12:43:50 +0000272#undef REGMAP_ALLOW_WRITE_DEBUGFS
273#ifdef REGMAP_ALLOW_WRITE_DEBUGFS
274/*
275 * This can be dangerous especially when we have clients such as
276 * PMICs, therefore don't provide any real compile time configuration option
277 * for this feature, people who want to use this will need to modify
278 * the source code directly.
279 */
280static ssize_t regmap_map_write_file(struct file *file,
281 const char __user *user_buf,
282 size_t count, loff_t *ppos)
283{
284 char buf[32];
285 size_t buf_size;
286 char *start = buf;
287 unsigned long reg, value;
288 struct regmap *map = file->private_data;
Dimitris Papastamos68e850d2013-05-09 12:46:41 +0100289 int ret;
Dimitris Papastamos09c6ecd2012-02-22 12:43:50 +0000290
291 buf_size = min(count, (sizeof(buf)-1));
292 if (copy_from_user(buf, user_buf, buf_size))
293 return -EFAULT;
294 buf[buf_size] = 0;
295
296 while (*start == ' ')
297 start++;
298 reg = simple_strtoul(start, &start, 16);
299 while (*start == ' ')
300 start++;
Jingoo Han34da5e62013-07-26 13:10:22 +0900301 if (kstrtoul(start, 16, &value))
Dimitris Papastamos09c6ecd2012-02-22 12:43:50 +0000302 return -EINVAL;
303
304 /* Userspace has been fiddling around behind the kernel's back */
Mark Brownf9e464a2013-05-09 14:35:49 +0100305 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
Dimitris Papastamos09c6ecd2012-02-22 12:43:50 +0000306
Dimitris Papastamos68e850d2013-05-09 12:46:41 +0100307 ret = regmap_write(map, reg, value);
308 if (ret < 0)
309 return ret;
Dimitris Papastamos09c6ecd2012-02-22 12:43:50 +0000310 return buf_size;
311}
312#else
313#define regmap_map_write_file NULL
314#endif
315
Mark Brown31244e32011-07-20 22:56:53 +0100316static const struct file_operations regmap_map_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700317 .open = simple_open,
Mark Brown31244e32011-07-20 22:56:53 +0100318 .read = regmap_map_read_file,
Dimitris Papastamos09c6ecd2012-02-22 12:43:50 +0000319 .write = regmap_map_write_file,
Mark Brown31244e32011-07-20 22:56:53 +0100320 .llseek = default_llseek,
321};
322
Mark Brown4b020b32012-10-03 13:13:16 +0100323static ssize_t regmap_range_read_file(struct file *file, char __user *user_buf,
324 size_t count, loff_t *ppos)
325{
326 struct regmap_range_node *range = file->private_data;
327 struct regmap *map = range->map;
328
329 return regmap_read_debugfs(map, range->range_min, range->range_max,
330 user_buf, count, ppos);
331}
332
333static const struct file_operations regmap_range_fops = {
334 .open = simple_open,
335 .read = regmap_range_read_file,
336 .llseek = default_llseek,
337};
338
Dimitris Papastamos065b4c52013-02-20 12:15:23 +0000339static ssize_t regmap_reg_ranges_read_file(struct file *file,
340 char __user *user_buf, size_t count,
341 loff_t *ppos)
342{
343 struct regmap *map = file->private_data;
344 struct regmap_debugfs_off_cache *c;
345 loff_t p = 0;
346 size_t buf_pos = 0;
347 char *buf;
348 char *entry;
349 int ret;
Rasmus Villemoese34dc492015-09-30 20:30:25 +0200350 unsigned entry_len;
Dimitris Papastamos065b4c52013-02-20 12:15:23 +0000351
352 if (*ppos < 0 || !count)
353 return -EINVAL;
354
355 buf = kmalloc(count, GFP_KERNEL);
356 if (!buf)
357 return -ENOMEM;
358
359 entry = kmalloc(PAGE_SIZE, GFP_KERNEL);
360 if (!entry) {
361 kfree(buf);
362 return -ENOMEM;
363 }
364
365 /* While we are at it, build the register dump cache
366 * now so the read() operation on the `registers' file
367 * can benefit from using the cache. We do not care
368 * about the file position information that is contained
369 * in the cache, just about the actual register blocks */
370 regmap_calc_tot_len(map, buf, count);
371 regmap_debugfs_get_dump_start(map, 0, *ppos, &p);
372
373 /* Reset file pointer as the fixed-format of the `registers'
374 * file is not compatible with the `range' file */
375 p = 0;
376 mutex_lock(&map->cache_lock);
377 list_for_each_entry(c, &map->debugfs_off_cache, list) {
Rasmus Villemoesca07e9f2015-09-30 20:30:27 +0200378 entry_len = snprintf(entry, PAGE_SIZE, "%x-%x\n",
Rasmus Villemoese34dc492015-09-30 20:30:25 +0200379 c->base_reg, c->max_reg);
Dimitris Papastamos065b4c52013-02-20 12:15:23 +0000380 if (p >= *ppos) {
Rasmus Villemoesca07e9f2015-09-30 20:30:27 +0200381 if (buf_pos + entry_len > count)
Dimitris Papastamos065b4c52013-02-20 12:15:23 +0000382 break;
Rasmus Villemoes20991cd2015-09-30 20:30:26 +0200383 memcpy(buf + buf_pos, entry, entry_len);
Rasmus Villemoese34dc492015-09-30 20:30:25 +0200384 buf_pos += entry_len;
Dimitris Papastamos065b4c52013-02-20 12:15:23 +0000385 }
Rasmus Villemoesca07e9f2015-09-30 20:30:27 +0200386 p += entry_len;
Dimitris Papastamos065b4c52013-02-20 12:15:23 +0000387 }
388 mutex_unlock(&map->cache_lock);
389
390 kfree(entry);
391 ret = buf_pos;
392
393 if (copy_to_user(user_buf, buf, buf_pos)) {
394 ret = -EFAULT;
395 goto out_buf;
396 }
397
398 *ppos += buf_pos;
399out_buf:
400 kfree(buf);
401 return ret;
402}
403
404static const struct file_operations regmap_reg_ranges_fops = {
405 .open = simple_open,
406 .read = regmap_reg_ranges_read_file,
407 .llseek = default_llseek,
408};
409
Mark Brown8da61f22015-10-20 15:40:59 +0100410static int regmap_access_show(struct seq_file *s, void *ignored)
Mark Brown449e3842011-08-10 17:28:04 +0900411{
Mark Brown8da61f22015-10-20 15:40:59 +0100412 struct regmap *map = s->private;
413 int i, reg_len;
Mark Brown449e3842011-08-10 17:28:04 +0900414
Mark Brown9ae31092015-09-19 07:31:47 -0700415 reg_len = regmap_calc_reg_len(map->max_register);
Mark Brown449e3842011-08-10 17:28:04 +0900416
Stephen Warrenf01ee602012-04-09 13:40:24 -0600417 for (i = 0; i <= map->max_register; i += map->reg_stride) {
Mark Brown449e3842011-08-10 17:28:04 +0900418 /* Ignore registers which are neither readable nor writable */
419 if (!regmap_readable(map, i) && !regmap_writeable(map, i))
420 continue;
421
Mark Brown8da61f22015-10-20 15:40:59 +0100422 /* Format the register */
423 seq_printf(s, "%.*x: %c %c %c %c\n", reg_len, i,
424 regmap_readable(map, i) ? 'y' : 'n',
425 regmap_writeable(map, i) ? 'y' : 'n',
426 regmap_volatile(map, i) ? 'y' : 'n',
427 regmap_precious(map, i) ? 'y' : 'n');
Mark Brown449e3842011-08-10 17:28:04 +0900428 }
429
Mark Brown8da61f22015-10-20 15:40:59 +0100430 return 0;
431}
Mark Brown449e3842011-08-10 17:28:04 +0900432
Mark Brown8da61f22015-10-20 15:40:59 +0100433static int access_open(struct inode *inode, struct file *file)
434{
435 return single_open(file, regmap_access_show, inode->i_private);
Mark Brown449e3842011-08-10 17:28:04 +0900436}
437
438static const struct file_operations regmap_access_fops = {
Mark Brown8da61f22015-10-20 15:40:59 +0100439 .open = access_open,
440 .read = seq_read,
441 .llseek = seq_lseek,
442 .release = single_release,
Mark Brown449e3842011-08-10 17:28:04 +0900443};
Mark Brown31244e32011-07-20 22:56:53 +0100444
Richard Fitzgeraldd3dc5432015-06-23 14:32:55 +0100445static ssize_t regmap_cache_only_write_file(struct file *file,
446 const char __user *user_buf,
447 size_t count, loff_t *ppos)
448{
449 struct regmap *map = container_of(file->private_data,
450 struct regmap, cache_only);
451 ssize_t result;
452 bool was_enabled, require_sync = false;
453 int err;
454
455 map->lock(map->lock_arg);
456
457 was_enabled = map->cache_only;
458
459 result = debugfs_write_file_bool(file, user_buf, count, ppos);
460 if (result < 0) {
461 map->unlock(map->lock_arg);
462 return result;
463 }
464
465 if (map->cache_only && !was_enabled) {
466 dev_warn(map->dev, "debugfs cache_only=Y forced\n");
467 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
468 } else if (!map->cache_only && was_enabled) {
469 dev_warn(map->dev, "debugfs cache_only=N forced: syncing cache\n");
470 require_sync = true;
471 }
472
473 map->unlock(map->lock_arg);
474
475 if (require_sync) {
476 err = regcache_sync(map);
477 if (err)
478 dev_err(map->dev, "Failed to sync cache %d\n", err);
479 }
480
481 return result;
482}
483
484static const struct file_operations regmap_cache_only_fops = {
485 .open = simple_open,
486 .read = debugfs_read_file_bool,
487 .write = regmap_cache_only_write_file,
488};
489
490static ssize_t regmap_cache_bypass_write_file(struct file *file,
491 const char __user *user_buf,
492 size_t count, loff_t *ppos)
493{
494 struct regmap *map = container_of(file->private_data,
495 struct regmap, cache_bypass);
496 ssize_t result;
497 bool was_enabled;
498
499 map->lock(map->lock_arg);
500
501 was_enabled = map->cache_bypass;
502
503 result = debugfs_write_file_bool(file, user_buf, count, ppos);
504 if (result < 0)
505 goto out;
506
507 if (map->cache_bypass && !was_enabled) {
508 dev_warn(map->dev, "debugfs cache_bypass=Y forced\n");
509 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
510 } else if (!map->cache_bypass && was_enabled) {
511 dev_warn(map->dev, "debugfs cache_bypass=N forced\n");
512 }
513
514out:
515 map->unlock(map->lock_arg);
516
517 return result;
518}
519
520static const struct file_operations regmap_cache_bypass_fops = {
521 .open = simple_open,
522 .read = debugfs_read_file_bool,
523 .write = regmap_cache_bypass_write_file,
524};
525
Stephen Warrend3c242e2012-04-04 15:48:29 -0600526void regmap_debugfs_init(struct regmap *map, const char *name)
Mark Brown31244e32011-07-20 22:56:53 +0100527{
Mark Brown4b020b32012-10-03 13:13:16 +0100528 struct rb_node *next;
529 struct regmap_range_node *range_node;
Xiubo Li2c98e0c2014-09-28 11:35:25 +0800530 const char *devname = "dummy";
Mark Brown4b020b32012-10-03 13:13:16 +0100531
Tero Kristoa52eaeb2013-10-24 15:03:41 +0300532 /* If we don't have the debugfs root yet, postpone init */
533 if (!regmap_debugfs_root) {
534 struct regmap_debugfs_node *node;
535 node = kzalloc(sizeof(*node), GFP_KERNEL);
536 if (!node)
537 return;
538 node->map = map;
539 node->name = name;
540 mutex_lock(&regmap_debugfs_early_lock);
541 list_add(&node->link, &regmap_debugfs_early_list);
542 mutex_unlock(&regmap_debugfs_early_lock);
543 return;
544 }
545
Mark Brown5166b7c2012-12-11 01:24:29 +0900546 INIT_LIST_HEAD(&map->debugfs_off_cache);
Dimitris Papastamos065b4c52013-02-20 12:15:23 +0000547 mutex_init(&map->cache_lock);
Mark Brown5166b7c2012-12-11 01:24:29 +0900548
Xiubo Li2c98e0c2014-09-28 11:35:25 +0800549 if (map->dev)
550 devname = dev_name(map->dev);
551
Stephen Warrend3c242e2012-04-04 15:48:29 -0600552 if (name) {
553 map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s",
Xiubo Li2c98e0c2014-09-28 11:35:25 +0800554 devname, name);
Stephen Warrend3c242e2012-04-04 15:48:29 -0600555 name = map->debugfs_name;
556 } else {
Xiubo Li2c98e0c2014-09-28 11:35:25 +0800557 name = devname;
Stephen Warrend3c242e2012-04-04 15:48:29 -0600558 }
559
560 map->debugfs = debugfs_create_dir(name, regmap_debugfs_root);
Mark Brown31244e32011-07-20 22:56:53 +0100561 if (!map->debugfs) {
562 dev_warn(map->dev, "Failed to create debugfs directory\n");
563 return;
564 }
565
Dimitris Papastamosf0c23192012-02-22 14:20:09 +0000566 debugfs_create_file("name", 0400, map->debugfs,
567 map, &regmap_name_fops);
568
Dimitris Papastamos065b4c52013-02-20 12:15:23 +0000569 debugfs_create_file("range", 0400, map->debugfs,
570 map, &regmap_reg_ranges_fops);
571
Pawel Moll676970d2014-01-30 13:26:32 +0000572 if (map->max_register || regmap_readable(map, 0)) {
Markus Pargmannffff7a12a2014-09-08 08:43:37 +0200573 umode_t registers_mode;
574
Axel Lin1635e882015-08-06 10:35:23 +0800575#if defined(REGMAP_ALLOW_WRITE_DEBUGFS)
576 registers_mode = 0600;
577#else
578 registers_mode = 0400;
579#endif
Markus Pargmannffff7a12a2014-09-08 08:43:37 +0200580
581 debugfs_create_file("registers", registers_mode, map->debugfs,
Mark Brown31244e32011-07-20 22:56:53 +0100582 map, &regmap_map_fops);
Mark Brown449e3842011-08-10 17:28:04 +0900583 debugfs_create_file("access", 0400, map->debugfs,
584 map, &regmap_access_fops);
585 }
Mark Brown028a01e2012-02-06 18:02:06 +0000586
587 if (map->cache_type) {
Richard Fitzgeraldd3dc5432015-06-23 14:32:55 +0100588 debugfs_create_file("cache_only", 0600, map->debugfs,
589 &map->cache_only, &regmap_cache_only_fops);
Mark Brown028a01e2012-02-06 18:02:06 +0000590 debugfs_create_bool("cache_dirty", 0400, map->debugfs,
591 &map->cache_dirty);
Richard Fitzgeraldd3dc5432015-06-23 14:32:55 +0100592 debugfs_create_file("cache_bypass", 0600, map->debugfs,
593 &map->cache_bypass,
594 &regmap_cache_bypass_fops);
Mark Brown028a01e2012-02-06 18:02:06 +0000595 }
Mark Brown4b020b32012-10-03 13:13:16 +0100596
597 next = rb_first(&map->range_tree);
598 while (next) {
599 range_node = rb_entry(next, struct regmap_range_node, node);
600
601 if (range_node->name)
602 debugfs_create_file(range_node->name, 0400,
603 map->debugfs, range_node,
604 &regmap_range_fops);
605
606 next = rb_next(&range_node->node);
607 }
Lars-Peter Clausen5e0cbe72014-08-24 15:32:27 +0200608
609 if (map->cache_ops && map->cache_ops->debugfs_init)
610 map->cache_ops->debugfs_init(map);
Mark Brown31244e32011-07-20 22:56:53 +0100611}
612
613void regmap_debugfs_exit(struct regmap *map)
614{
Tero Kristoa52eaeb2013-10-24 15:03:41 +0300615 if (map->debugfs) {
616 debugfs_remove_recursive(map->debugfs);
617 mutex_lock(&map->cache_lock);
618 regmap_debugfs_free_dump_cache(map);
619 mutex_unlock(&map->cache_lock);
620 kfree(map->debugfs_name);
621 } else {
622 struct regmap_debugfs_node *node, *tmp;
623
624 mutex_lock(&regmap_debugfs_early_lock);
625 list_for_each_entry_safe(node, tmp, &regmap_debugfs_early_list,
626 link) {
627 if (node->map == map) {
628 list_del(&node->link);
629 kfree(node);
630 }
631 }
632 mutex_unlock(&regmap_debugfs_early_lock);
633 }
Mark Brown31244e32011-07-20 22:56:53 +0100634}
635
636void regmap_debugfs_initcall(void)
637{
Tero Kristoa52eaeb2013-10-24 15:03:41 +0300638 struct regmap_debugfs_node *node, *tmp;
639
Mark Brown31244e32011-07-20 22:56:53 +0100640 regmap_debugfs_root = debugfs_create_dir("regmap", NULL);
641 if (!regmap_debugfs_root) {
642 pr_warn("regmap: Failed to create debugfs root\n");
643 return;
644 }
Tero Kristoa52eaeb2013-10-24 15:03:41 +0300645
646 mutex_lock(&regmap_debugfs_early_lock);
647 list_for_each_entry_safe(node, tmp, &regmap_debugfs_early_list, link) {
648 regmap_debugfs_init(node->map, node->name);
649 list_del(&node->link);
650 kfree(node);
651 }
652 mutex_unlock(&regmap_debugfs_early_lock);
Mark Brown31244e32011-07-20 22:56:53 +0100653}