blob: 817eda2075aa5aaa7e762a91fbb51fc5a6ceab51 [file] [log] [blame]
Greg Kroah-Hartman37613fa2019-04-25 20:06:18 +02001// SPDX-License-Identifier: GPL-2.0
2//
3// Register map access API - debugfs
4//
5// Copyright 2011 Wolfson Microelectronics plc
6//
7// Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
Mark Brown31244e32011-07-20 22:56:53 +01008
9#include <linux/slab.h>
Mark Brown31244e32011-07-20 22:56:53 +010010#include <linux/mutex.h>
11#include <linux/debugfs.h>
12#include <linux/uaccess.h>
Paul Gortmaker51990e82012-01-22 11:23:42 -050013#include <linux/device.h>
Tero Kristoa52eaeb2013-10-24 15:03:41 +030014#include <linux/list.h>
Mark Brown31244e32011-07-20 22:56:53 +010015
16#include "internal.h"
17
Tero Kristoa52eaeb2013-10-24 15:03:41 +030018struct regmap_debugfs_node {
19 struct regmap *map;
Tero Kristoa52eaeb2013-10-24 15:03:41 +030020 struct list_head link;
21};
22
Fabio Estevama430ab22018-03-05 15:52:09 -030023static unsigned int dummy_index;
Mark Brown31244e32011-07-20 22:56:53 +010024static struct dentry *regmap_debugfs_root;
Tero Kristoa52eaeb2013-10-24 15:03:41 +030025static LIST_HEAD(regmap_debugfs_early_list);
26static DEFINE_MUTEX(regmap_debugfs_early_lock);
Mark Brown31244e32011-07-20 22:56:53 +010027
Mark Brown21f55542011-08-10 17:15:31 +090028/* Calculate the length of a fixed format */
Mark Brown9ae31092015-09-19 07:31:47 -070029static size_t regmap_calc_reg_len(int max_val)
Mark Brown21f55542011-08-10 17:15:31 +090030{
Mark Brown176fc2d2015-09-19 07:12:34 -070031 return snprintf(NULL, 0, "%x", max_val);
Mark Brown21f55542011-08-10 17:15:31 +090032}
33
Dimitris Papastamosf0c23192012-02-22 14:20:09 +000034static ssize_t regmap_name_read_file(struct file *file,
35 char __user *user_buf, size_t count,
36 loff_t *ppos)
37{
38 struct regmap *map = file->private_data;
David Lechner12ae3802018-02-19 15:43:01 -060039 const char *name = "nodev";
Dimitris Papastamosf0c23192012-02-22 14:20:09 +000040 int ret;
41 char *buf;
42
43 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
44 if (!buf)
45 return -ENOMEM;
46
David Lechner12ae3802018-02-19 15:43:01 -060047 if (map->dev && map->dev->driver)
48 name = map->dev->driver->name;
49
50 ret = snprintf(buf, PAGE_SIZE, "%s\n", name);
Dimitris Papastamosf0c23192012-02-22 14:20:09 +000051 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) {
Julia Lawall7f4a1222020-09-27 21:12:24 +0200186 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
Lucas Tanurecc6a8d62019-03-19 09:41:33 +0000193static int regmap_next_readable_reg(struct regmap *map, int reg)
194{
195 struct regmap_debugfs_off_cache *c;
196 int ret = -EINVAL;
197
198 if (regmap_printable(map, reg + map->reg_stride)) {
199 ret = reg + map->reg_stride;
200 } else {
201 mutex_lock(&map->cache_lock);
202 list_for_each_entry(c, &map->debugfs_off_cache, list) {
203 if (reg > c->max_reg)
204 continue;
205 if (reg < c->base_reg) {
206 ret = c->base_reg;
207 break;
208 }
209 }
210 mutex_unlock(&map->cache_lock);
211 }
212 return ret;
213}
214
Mark Brownbd9cc122012-10-03 12:45:37 +0100215static ssize_t regmap_read_debugfs(struct regmap *map, unsigned int from,
216 unsigned int to, char __user *user_buf,
217 size_t count, loff_t *ppos)
Mark Brown31244e32011-07-20 22:56:53 +0100218{
Mark Brown31244e32011-07-20 22:56:53 +0100219 size_t buf_pos = 0;
Mark Brownafab2f72012-12-09 17:20:10 +0900220 loff_t p = *ppos;
Mark Brown31244e32011-07-20 22:56:53 +0100221 ssize_t ret;
222 int i;
Mark Brown31244e32011-07-20 22:56:53 +0100223 char *buf;
Mark Brownafab2f72012-12-09 17:20:10 +0900224 unsigned int val, start_reg;
Mark Brown31244e32011-07-20 22:56:53 +0100225
226 if (*ppos < 0 || !count)
227 return -EINVAL;
228
Peng Fan74edd082020-03-13 09:58:07 +0800229 if (count > (PAGE_SIZE << (MAX_ORDER - 1)))
230 count = PAGE_SIZE << (MAX_ORDER - 1);
231
Mark Brown31244e32011-07-20 22:56:53 +0100232 buf = kmalloc(count, GFP_KERNEL);
233 if (!buf)
234 return -ENOMEM;
235
Dimitris Papastamos4dd7c552013-02-11 10:50:59 +0000236 regmap_calc_tot_len(map, buf, count);
Mark Brown31244e32011-07-20 22:56:53 +0100237
Mark Brownafab2f72012-12-09 17:20:10 +0900238 /* Work out which register we're starting at */
239 start_reg = regmap_debugfs_get_dump_start(map, from, *ppos, &p);
240
Lucas Tanurecc6a8d62019-03-19 09:41:33 +0000241 for (i = start_reg; i >= 0 && i <= to;
242 i = regmap_next_readable_reg(map, i)) {
Mark Brown2efe1642011-08-08 15:41:46 +0900243
Mark Brown31244e32011-07-20 22:56:53 +0100244 /* If we're in the region the user is trying to read */
245 if (p >= *ppos) {
246 /* ...but not beyond it */
Dimitris Papastamosf3eb8392013-02-07 10:51:56 +0000247 if (buf_pos + map->debugfs_tot_len > count)
Mark Brown31244e32011-07-20 22:56:53 +0100248 break;
249
250 /* Format the register */
251 snprintf(buf + buf_pos, count - buf_pos, "%.*x: ",
Mark Browncbc19382012-12-06 13:29:05 +0900252 map->debugfs_reg_len, i - from);
253 buf_pos += map->debugfs_reg_len + 2;
Mark Brown31244e32011-07-20 22:56:53 +0100254
255 /* Format the value, write all X if we can't read */
256 ret = regmap_read(map, i, &val);
257 if (ret == 0)
258 snprintf(buf + buf_pos, count - buf_pos,
Mark Browncbc19382012-12-06 13:29:05 +0900259 "%.*x", map->debugfs_val_len, val);
Mark Brown31244e32011-07-20 22:56:53 +0100260 else
Mark Browncbc19382012-12-06 13:29:05 +0900261 memset(buf + buf_pos, 'X',
262 map->debugfs_val_len);
Mark Brown31244e32011-07-20 22:56:53 +0100263 buf_pos += 2 * map->format.val_bytes;
264
265 buf[buf_pos++] = '\n';
266 }
Mark Browncbc19382012-12-06 13:29:05 +0900267 p += map->debugfs_tot_len;
Mark Brown31244e32011-07-20 22:56:53 +0100268 }
269
270 ret = buf_pos;
271
272 if (copy_to_user(user_buf, buf, buf_pos)) {
273 ret = -EFAULT;
274 goto out;
275 }
276
277 *ppos += buf_pos;
278
279out:
280 kfree(buf);
281 return ret;
282}
283
Mark Brownbd9cc122012-10-03 12:45:37 +0100284static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf,
285 size_t count, loff_t *ppos)
286{
287 struct regmap *map = file->private_data;
288
289 return regmap_read_debugfs(map, 0, map->max_register, user_buf,
290 count, ppos);
291}
292
Dimitris Papastamos09c6ecd2012-02-22 12:43:50 +0000293#undef REGMAP_ALLOW_WRITE_DEBUGFS
294#ifdef REGMAP_ALLOW_WRITE_DEBUGFS
295/*
296 * This can be dangerous especially when we have clients such as
297 * PMICs, therefore don't provide any real compile time configuration option
298 * for this feature, people who want to use this will need to modify
299 * the source code directly.
300 */
301static ssize_t regmap_map_write_file(struct file *file,
302 const char __user *user_buf,
303 size_t count, loff_t *ppos)
304{
305 char buf[32];
306 size_t buf_size;
307 char *start = buf;
308 unsigned long reg, value;
309 struct regmap *map = file->private_data;
Dimitris Papastamos68e850d2013-05-09 12:46:41 +0100310 int ret;
Dimitris Papastamos09c6ecd2012-02-22 12:43:50 +0000311
312 buf_size = min(count, (sizeof(buf)-1));
313 if (copy_from_user(buf, user_buf, buf_size))
314 return -EFAULT;
315 buf[buf_size] = 0;
316
317 while (*start == ' ')
318 start++;
319 reg = simple_strtoul(start, &start, 16);
320 while (*start == ' ')
321 start++;
Jingoo Han34da5e62013-07-26 13:10:22 +0900322 if (kstrtoul(start, 16, &value))
Dimitris Papastamos09c6ecd2012-02-22 12:43:50 +0000323 return -EINVAL;
324
325 /* Userspace has been fiddling around behind the kernel's back */
Mark Brownf9e464a2013-05-09 14:35:49 +0100326 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
Dimitris Papastamos09c6ecd2012-02-22 12:43:50 +0000327
Dimitris Papastamos68e850d2013-05-09 12:46:41 +0100328 ret = regmap_write(map, reg, value);
329 if (ret < 0)
330 return ret;
Dimitris Papastamos09c6ecd2012-02-22 12:43:50 +0000331 return buf_size;
332}
333#else
334#define regmap_map_write_file NULL
335#endif
336
Mark Brown31244e32011-07-20 22:56:53 +0100337static const struct file_operations regmap_map_fops = {
Stephen Boyd234e3402012-04-05 14:25:11 -0700338 .open = simple_open,
Mark Brown31244e32011-07-20 22:56:53 +0100339 .read = regmap_map_read_file,
Dimitris Papastamos09c6ecd2012-02-22 12:43:50 +0000340 .write = regmap_map_write_file,
Mark Brown31244e32011-07-20 22:56:53 +0100341 .llseek = default_llseek,
342};
343
Mark Brown4b020b32012-10-03 13:13:16 +0100344static ssize_t regmap_range_read_file(struct file *file, char __user *user_buf,
345 size_t count, loff_t *ppos)
346{
347 struct regmap_range_node *range = file->private_data;
348 struct regmap *map = range->map;
349
350 return regmap_read_debugfs(map, range->range_min, range->range_max,
351 user_buf, count, ppos);
352}
353
354static const struct file_operations regmap_range_fops = {
355 .open = simple_open,
356 .read = regmap_range_read_file,
357 .llseek = default_llseek,
358};
359
Dimitris Papastamos065b4c52013-02-20 12:15:23 +0000360static ssize_t regmap_reg_ranges_read_file(struct file *file,
361 char __user *user_buf, size_t count,
362 loff_t *ppos)
363{
364 struct regmap *map = file->private_data;
365 struct regmap_debugfs_off_cache *c;
366 loff_t p = 0;
367 size_t buf_pos = 0;
368 char *buf;
369 char *entry;
370 int ret;
Jinchao Wangd63aa092021-06-29 01:19:06 +0800371 unsigned int entry_len;
Dimitris Papastamos065b4c52013-02-20 12:15:23 +0000372
373 if (*ppos < 0 || !count)
374 return -EINVAL;
375
Peng Fan74edd082020-03-13 09:58:07 +0800376 if (count > (PAGE_SIZE << (MAX_ORDER - 1)))
377 count = PAGE_SIZE << (MAX_ORDER - 1);
378
Dimitris Papastamos065b4c52013-02-20 12:15:23 +0000379 buf = kmalloc(count, GFP_KERNEL);
380 if (!buf)
381 return -ENOMEM;
382
383 entry = kmalloc(PAGE_SIZE, GFP_KERNEL);
384 if (!entry) {
385 kfree(buf);
386 return -ENOMEM;
387 }
388
389 /* While we are at it, build the register dump cache
390 * now so the read() operation on the `registers' file
391 * can benefit from using the cache. We do not care
392 * about the file position information that is contained
393 * in the cache, just about the actual register blocks */
394 regmap_calc_tot_len(map, buf, count);
395 regmap_debugfs_get_dump_start(map, 0, *ppos, &p);
396
397 /* Reset file pointer as the fixed-format of the `registers'
398 * file is not compatible with the `range' file */
399 p = 0;
400 mutex_lock(&map->cache_lock);
401 list_for_each_entry(c, &map->debugfs_off_cache, list) {
Rasmus Villemoesca07e9f2015-09-30 20:30:27 +0200402 entry_len = snprintf(entry, PAGE_SIZE, "%x-%x\n",
Rasmus Villemoese34dc492015-09-30 20:30:25 +0200403 c->base_reg, c->max_reg);
Dimitris Papastamos065b4c52013-02-20 12:15:23 +0000404 if (p >= *ppos) {
Rasmus Villemoesca07e9f2015-09-30 20:30:27 +0200405 if (buf_pos + entry_len > count)
Dimitris Papastamos065b4c52013-02-20 12:15:23 +0000406 break;
Rasmus Villemoes20991cd2015-09-30 20:30:26 +0200407 memcpy(buf + buf_pos, entry, entry_len);
Rasmus Villemoese34dc492015-09-30 20:30:25 +0200408 buf_pos += entry_len;
Dimitris Papastamos065b4c52013-02-20 12:15:23 +0000409 }
Rasmus Villemoesca07e9f2015-09-30 20:30:27 +0200410 p += entry_len;
Dimitris Papastamos065b4c52013-02-20 12:15:23 +0000411 }
412 mutex_unlock(&map->cache_lock);
413
414 kfree(entry);
415 ret = buf_pos;
416
417 if (copy_to_user(user_buf, buf, buf_pos)) {
418 ret = -EFAULT;
419 goto out_buf;
420 }
421
422 *ppos += buf_pos;
423out_buf:
424 kfree(buf);
425 return ret;
426}
427
428static const struct file_operations regmap_reg_ranges_fops = {
429 .open = simple_open,
430 .read = regmap_reg_ranges_read_file,
431 .llseek = default_llseek,
432};
433
Mark Brown8da61f22015-10-20 15:40:59 +0100434static int regmap_access_show(struct seq_file *s, void *ignored)
Mark Brown449e3842011-08-10 17:28:04 +0900435{
Mark Brown8da61f22015-10-20 15:40:59 +0100436 struct regmap *map = s->private;
437 int i, reg_len;
Mark Brown449e3842011-08-10 17:28:04 +0900438
Mark Brown9ae31092015-09-19 07:31:47 -0700439 reg_len = regmap_calc_reg_len(map->max_register);
Mark Brown449e3842011-08-10 17:28:04 +0900440
Stephen Warrenf01ee602012-04-09 13:40:24 -0600441 for (i = 0; i <= map->max_register; i += map->reg_stride) {
Mark Brown449e3842011-08-10 17:28:04 +0900442 /* Ignore registers which are neither readable nor writable */
443 if (!regmap_readable(map, i) && !regmap_writeable(map, i))
444 continue;
445
Mark Brown8da61f22015-10-20 15:40:59 +0100446 /* Format the register */
447 seq_printf(s, "%.*x: %c %c %c %c\n", reg_len, i,
448 regmap_readable(map, i) ? 'y' : 'n',
449 regmap_writeable(map, i) ? 'y' : 'n',
450 regmap_volatile(map, i) ? 'y' : 'n',
451 regmap_precious(map, i) ? 'y' : 'n');
Mark Brown449e3842011-08-10 17:28:04 +0900452 }
453
Mark Brown8da61f22015-10-20 15:40:59 +0100454 return 0;
455}
Mark Brown449e3842011-08-10 17:28:04 +0900456
Yangtao Li580d4852018-12-15 03:41:19 -0500457DEFINE_SHOW_ATTRIBUTE(regmap_access);
Mark Brown31244e32011-07-20 22:56:53 +0100458
Richard Fitzgeraldd3dc5432015-06-23 14:32:55 +0100459static ssize_t regmap_cache_only_write_file(struct file *file,
460 const char __user *user_buf,
461 size_t count, loff_t *ppos)
462{
463 struct regmap *map = container_of(file->private_data,
464 struct regmap, cache_only);
Douglas Anderson299632e2020-07-15 16:46:15 -0700465 bool new_val, require_sync = false;
Richard Fitzgeraldd3dc5432015-06-23 14:32:55 +0100466 int err;
467
Douglas Anderson299632e2020-07-15 16:46:15 -0700468 err = kstrtobool_from_user(user_buf, count, &new_val);
469 /* Ignore malforned data like debugfs_write_file_bool() */
470 if (err)
471 return count;
472
473 err = debugfs_file_get(file->f_path.dentry);
474 if (err)
475 return err;
476
Richard Fitzgeraldd3dc5432015-06-23 14:32:55 +0100477 map->lock(map->lock_arg);
478
Douglas Anderson299632e2020-07-15 16:46:15 -0700479 if (new_val && !map->cache_only) {
Richard Fitzgeraldd3dc5432015-06-23 14:32:55 +0100480 dev_warn(map->dev, "debugfs cache_only=Y forced\n");
481 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
Douglas Anderson299632e2020-07-15 16:46:15 -0700482 } else if (!new_val && map->cache_only) {
Richard Fitzgeraldd3dc5432015-06-23 14:32:55 +0100483 dev_warn(map->dev, "debugfs cache_only=N forced: syncing cache\n");
484 require_sync = true;
485 }
Douglas Anderson299632e2020-07-15 16:46:15 -0700486 map->cache_only = new_val;
Richard Fitzgeraldd3dc5432015-06-23 14:32:55 +0100487
488 map->unlock(map->lock_arg);
Douglas Anderson299632e2020-07-15 16:46:15 -0700489 debugfs_file_put(file->f_path.dentry);
Richard Fitzgeraldd3dc5432015-06-23 14:32:55 +0100490
491 if (require_sync) {
492 err = regcache_sync(map);
493 if (err)
494 dev_err(map->dev, "Failed to sync cache %d\n", err);
495 }
496
Douglas Anderson299632e2020-07-15 16:46:15 -0700497 return count;
Richard Fitzgeraldd3dc5432015-06-23 14:32:55 +0100498}
499
500static const struct file_operations regmap_cache_only_fops = {
501 .open = simple_open,
502 .read = debugfs_read_file_bool,
503 .write = regmap_cache_only_write_file,
504};
505
506static ssize_t regmap_cache_bypass_write_file(struct file *file,
507 const char __user *user_buf,
508 size_t count, loff_t *ppos)
509{
510 struct regmap *map = container_of(file->private_data,
511 struct regmap, cache_bypass);
Douglas Anderson299632e2020-07-15 16:46:15 -0700512 bool new_val;
513 int err;
514
515 err = kstrtobool_from_user(user_buf, count, &new_val);
516 /* Ignore malforned data like debugfs_write_file_bool() */
517 if (err)
518 return count;
519
520 err = debugfs_file_get(file->f_path.dentry);
521 if (err)
522 return err;
Richard Fitzgeraldd3dc5432015-06-23 14:32:55 +0100523
524 map->lock(map->lock_arg);
525
Douglas Anderson299632e2020-07-15 16:46:15 -0700526 if (new_val && !map->cache_bypass) {
Richard Fitzgeraldd3dc5432015-06-23 14:32:55 +0100527 dev_warn(map->dev, "debugfs cache_bypass=Y forced\n");
528 add_taint(TAINT_USER, LOCKDEP_STILL_OK);
Douglas Anderson299632e2020-07-15 16:46:15 -0700529 } else if (!new_val && map->cache_bypass) {
Richard Fitzgeraldd3dc5432015-06-23 14:32:55 +0100530 dev_warn(map->dev, "debugfs cache_bypass=N forced\n");
531 }
Douglas Anderson299632e2020-07-15 16:46:15 -0700532 map->cache_bypass = new_val;
Richard Fitzgeraldd3dc5432015-06-23 14:32:55 +0100533
Richard Fitzgeraldd3dc5432015-06-23 14:32:55 +0100534 map->unlock(map->lock_arg);
Douglas Anderson299632e2020-07-15 16:46:15 -0700535 debugfs_file_put(file->f_path.dentry);
Richard Fitzgeraldd3dc5432015-06-23 14:32:55 +0100536
Douglas Anderson299632e2020-07-15 16:46:15 -0700537 return count;
Richard Fitzgeraldd3dc5432015-06-23 14:32:55 +0100538}
539
540static const struct file_operations regmap_cache_bypass_fops = {
541 .open = simple_open,
542 .read = debugfs_read_file_bool,
543 .write = regmap_cache_bypass_write_file,
544};
545
Charles Keepax94cc89e2020-09-17 13:08:28 +0100546void regmap_debugfs_init(struct regmap *map)
Mark Brown31244e32011-07-20 22:56:53 +0100547{
Mark Brown4b020b32012-10-03 13:13:16 +0100548 struct rb_node *next;
549 struct regmap_range_node *range_node;
Xiubo Li2c98e0c2014-09-28 11:35:25 +0800550 const char *devname = "dummy";
Charles Keepax94cc89e2020-09-17 13:08:28 +0100551 const char *name = map->name;
Mark Brown4b020b32012-10-03 13:13:16 +0100552
Bartosz Golaszewski078711d2017-12-22 18:42:08 +0100553 /*
554 * Userspace can initiate reads from the hardware over debugfs.
555 * Normally internal regmap structures and buffers are protected with
556 * a mutex or a spinlock, but if the regmap owner decided to disable
557 * all locking mechanisms, this is no longer the case. For safety:
558 * don't create the debugfs entries if locking is disabled.
559 */
Bartosz Golaszewskia5ba91c2017-12-21 12:12:50 +0100560 if (map->debugfs_disable) {
561 dev_dbg(map->dev, "regmap locking disabled - not creating debugfs entries\n");
Mark Brown72465732017-12-12 16:56:43 +0000562 return;
Bartosz Golaszewskia5ba91c2017-12-21 12:12:50 +0100563 }
Mark Brown72465732017-12-12 16:56:43 +0000564
Tero Kristoa52eaeb2013-10-24 15:03:41 +0300565 /* If we don't have the debugfs root yet, postpone init */
566 if (!regmap_debugfs_root) {
567 struct regmap_debugfs_node *node;
568 node = kzalloc(sizeof(*node), GFP_KERNEL);
569 if (!node)
570 return;
571 node->map = map;
Tero Kristoa52eaeb2013-10-24 15:03:41 +0300572 mutex_lock(&regmap_debugfs_early_lock);
573 list_add(&node->link, &regmap_debugfs_early_list);
574 mutex_unlock(&regmap_debugfs_early_lock);
575 return;
576 }
577
Mark Brown5166b7c2012-12-11 01:24:29 +0900578 INIT_LIST_HEAD(&map->debugfs_off_cache);
Dimitris Papastamos065b4c52013-02-20 12:15:23 +0000579 mutex_init(&map->cache_lock);
Mark Brown5166b7c2012-12-11 01:24:29 +0900580
Xiubo Li2c98e0c2014-09-28 11:35:25 +0800581 if (map->dev)
582 devname = dev_name(map->dev);
583
Stephen Warrend3c242e2012-04-04 15:48:29 -0600584 if (name) {
Xiaolei Wangcffa4b22020-12-29 18:50:46 +0800585 if (!map->debugfs_name) {
586 map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s",
Xiubo Li2c98e0c2014-09-28 11:35:25 +0800587 devname, name);
Xiaolei Wangcffa4b22020-12-29 18:50:46 +0800588 if (!map->debugfs_name)
589 return;
590 }
Stephen Warrend3c242e2012-04-04 15:48:29 -0600591 name = map->debugfs_name;
592 } else {
Xiubo Li2c98e0c2014-09-28 11:35:25 +0800593 name = devname;
Stephen Warrend3c242e2012-04-04 15:48:29 -0600594 }
595
Fabio Estevama430ab22018-03-05 15:52:09 -0300596 if (!strcmp(name, "dummy")) {
Dan Carpenterf6bcb4c2021-01-05 14:42:29 +0300597 kfree(map->debugfs_name);
Mark Brown46589e92018-03-05 20:26:51 +0000598 map->debugfs_name = kasprintf(GFP_KERNEL, "dummy%d",
599 dummy_index);
Xiaolei Wangcffa4b22020-12-29 18:50:46 +0800600 if (!map->debugfs_name)
Mark Brownb56a7cb2022-01-07 19:11:45 +0000601 return;
Mark Brown46589e92018-03-05 20:26:51 +0000602 name = map->debugfs_name;
Fabio Estevama430ab22018-03-05 15:52:09 -0300603 dummy_index++;
604 }
605
Stephen Warrend3c242e2012-04-04 15:48:29 -0600606 map->debugfs = debugfs_create_dir(name, regmap_debugfs_root);
Mark Brown31244e32011-07-20 22:56:53 +0100607
Dimitris Papastamosf0c23192012-02-22 14:20:09 +0000608 debugfs_create_file("name", 0400, map->debugfs,
609 map, &regmap_name_fops);
610
Dimitris Papastamos065b4c52013-02-20 12:15:23 +0000611 debugfs_create_file("range", 0400, map->debugfs,
612 map, &regmap_reg_ranges_fops);
613
Pawel Moll676970d2014-01-30 13:26:32 +0000614 if (map->max_register || regmap_readable(map, 0)) {
Markus Pargmannffff7a12a2014-09-08 08:43:37 +0200615 umode_t registers_mode;
616
Axel Lin1635e882015-08-06 10:35:23 +0800617#if defined(REGMAP_ALLOW_WRITE_DEBUGFS)
618 registers_mode = 0600;
619#else
620 registers_mode = 0400;
621#endif
Markus Pargmannffff7a12a2014-09-08 08:43:37 +0200622
623 debugfs_create_file("registers", registers_mode, map->debugfs,
Mark Brown31244e32011-07-20 22:56:53 +0100624 map, &regmap_map_fops);
Mark Brown449e3842011-08-10 17:28:04 +0900625 debugfs_create_file("access", 0400, map->debugfs,
626 map, &regmap_access_fops);
627 }
Mark Brown028a01e2012-02-06 18:02:06 +0000628
629 if (map->cache_type) {
Richard Fitzgeraldd3dc5432015-06-23 14:32:55 +0100630 debugfs_create_file("cache_only", 0600, map->debugfs,
631 &map->cache_only, &regmap_cache_only_fops);
Mark Brown028a01e2012-02-06 18:02:06 +0000632 debugfs_create_bool("cache_dirty", 0400, map->debugfs,
633 &map->cache_dirty);
Richard Fitzgeraldd3dc5432015-06-23 14:32:55 +0100634 debugfs_create_file("cache_bypass", 0600, map->debugfs,
635 &map->cache_bypass,
636 &regmap_cache_bypass_fops);
Mark Brown028a01e2012-02-06 18:02:06 +0000637 }
Mark Brown4b020b32012-10-03 13:13:16 +0100638
639 next = rb_first(&map->range_tree);
640 while (next) {
641 range_node = rb_entry(next, struct regmap_range_node, node);
642
643 if (range_node->name)
644 debugfs_create_file(range_node->name, 0400,
645 map->debugfs, range_node,
646 &regmap_range_fops);
647
648 next = rb_next(&range_node->node);
649 }
Lars-Peter Clausen5e0cbe72014-08-24 15:32:27 +0200650
651 if (map->cache_ops && map->cache_ops->debugfs_init)
652 map->cache_ops->debugfs_init(map);
Mark Brown31244e32011-07-20 22:56:53 +0100653}
654
655void regmap_debugfs_exit(struct regmap *map)
656{
Tero Kristoa52eaeb2013-10-24 15:03:41 +0300657 if (map->debugfs) {
658 debugfs_remove_recursive(map->debugfs);
659 mutex_lock(&map->cache_lock);
660 regmap_debugfs_free_dump_cache(map);
661 mutex_unlock(&map->cache_lock);
662 kfree(map->debugfs_name);
Meng Lie41a9622021-02-26 10:17:37 +0800663 map->debugfs_name = NULL;
Tero Kristoa52eaeb2013-10-24 15:03:41 +0300664 } else {
665 struct regmap_debugfs_node *node, *tmp;
666
667 mutex_lock(&regmap_debugfs_early_lock);
668 list_for_each_entry_safe(node, tmp, &regmap_debugfs_early_list,
669 link) {
670 if (node->map == map) {
671 list_del(&node->link);
672 kfree(node);
673 }
674 }
675 mutex_unlock(&regmap_debugfs_early_lock);
676 }
Mark Brown31244e32011-07-20 22:56:53 +0100677}
678
679void regmap_debugfs_initcall(void)
680{
Tero Kristoa52eaeb2013-10-24 15:03:41 +0300681 struct regmap_debugfs_node *node, *tmp;
682
Mark Brown31244e32011-07-20 22:56:53 +0100683 regmap_debugfs_root = debugfs_create_dir("regmap", NULL);
Tero Kristoa52eaeb2013-10-24 15:03:41 +0300684
685 mutex_lock(&regmap_debugfs_early_lock);
686 list_for_each_entry_safe(node, tmp, &regmap_debugfs_early_list, link) {
Charles Keepax94cc89e2020-09-17 13:08:28 +0100687 regmap_debugfs_init(node->map);
Tero Kristoa52eaeb2013-10-24 15:03:41 +0300688 list_del(&node->link);
689 kfree(node);
690 }
691 mutex_unlock(&regmap_debugfs_early_lock);
Mark Brown31244e32011-07-20 22:56:53 +0100692}