Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 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> |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 14 | #include <linux/mutex.h> |
| 15 | #include <linux/debugfs.h> |
| 16 | #include <linux/uaccess.h> |
Paul Gortmaker | 51990e8 | 2012-01-22 11:23:42 -0500 | [diff] [blame] | 17 | #include <linux/device.h> |
Tero Kristo | a52eaeb | 2013-10-24 15:03:41 +0300 | [diff] [blame] | 18 | #include <linux/list.h> |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 19 | |
| 20 | #include "internal.h" |
| 21 | |
Tero Kristo | a52eaeb | 2013-10-24 15:03:41 +0300 | [diff] [blame] | 22 | struct regmap_debugfs_node { |
| 23 | struct regmap *map; |
| 24 | const char *name; |
| 25 | struct list_head link; |
| 26 | }; |
| 27 | |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 28 | static struct dentry *regmap_debugfs_root; |
Tero Kristo | a52eaeb | 2013-10-24 15:03:41 +0300 | [diff] [blame] | 29 | static LIST_HEAD(regmap_debugfs_early_list); |
| 30 | static DEFINE_MUTEX(regmap_debugfs_early_lock); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 31 | |
Mark Brown | 21f5554 | 2011-08-10 17:15:31 +0900 | [diff] [blame] | 32 | /* Calculate the length of a fixed format */ |
Mark Brown | 9ae3109 | 2015-09-19 07:31:47 -0700 | [diff] [blame] | 33 | static size_t regmap_calc_reg_len(int max_val) |
Mark Brown | 21f5554 | 2011-08-10 17:15:31 +0900 | [diff] [blame] | 34 | { |
Mark Brown | 176fc2d | 2015-09-19 07:12:34 -0700 | [diff] [blame] | 35 | return snprintf(NULL, 0, "%x", max_val); |
Mark Brown | 21f5554 | 2011-08-10 17:15:31 +0900 | [diff] [blame] | 36 | } |
| 37 | |
Dimitris Papastamos | f0c2319 | 2012-02-22 14:20:09 +0000 | [diff] [blame] | 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 = { |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 62 | .open = simple_open, |
Dimitris Papastamos | f0c2319 | 2012-02-22 14:20:09 +0000 | [diff] [blame] | 63 | .read = regmap_name_read_file, |
| 64 | .llseek = default_llseek, |
| 65 | }; |
| 66 | |
Mark Brown | 95f971c | 2013-01-08 13:35:58 +0000 | [diff] [blame] | 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 | |
Cristian Birsan | 359a2f1 | 2016-08-08 18:44:22 +0300 | [diff] [blame] | 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 | |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 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 | { |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 100 | struct regmap_debugfs_off_cache *c = NULL; |
| 101 | loff_t p = 0; |
| 102 | unsigned int i, ret; |
Dimitris Papastamos | c2c1ee6 | 2013-02-08 12:47:14 +0000 | [diff] [blame] | 103 | unsigned int fpos_offset; |
| 104 | unsigned int reg_offset; |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 105 | |
Mark Brown | d6814a7 | 2013-05-29 15:54:54 +0100 | [diff] [blame] | 106 | /* Suppress the cache if we're using a subrange */ |
Lars-Peter Clausen | 26ee474 | 2013-08-28 17:55:07 +0200 | [diff] [blame] | 107 | if (base) |
| 108 | return base; |
Mark Brown | d6814a7 | 2013-05-29 15:54:54 +0100 | [diff] [blame] | 109 | |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 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 | */ |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 114 | mutex_lock(&map->cache_lock); |
Dimitris Papastamos | 480738d | 2013-02-20 12:15:22 +0000 | [diff] [blame] | 115 | i = base; |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 116 | if (list_empty(&map->debugfs_off_cache)) { |
Dimitris Papastamos | 480738d | 2013-02-20 12:15:22 +0000 | [diff] [blame] | 117 | for (; i <= map->max_register; i += map->reg_stride) { |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 118 | /* Skip unprinted registers, closing off cache entry */ |
Cristian Birsan | 359a2f1 | 2016-08-08 18:44:22 +0300 | [diff] [blame] | 119 | if (!regmap_printable(map, i)) { |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 120 | if (c) { |
| 121 | c->max = p - 1; |
Dimitris Papastamos | 480738d | 2013-02-20 12:15:22 +0000 | [diff] [blame] | 122 | c->max_reg = i - map->reg_stride; |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 123 | list_add_tail(&c->list, |
| 124 | &map->debugfs_off_cache); |
| 125 | c = NULL; |
| 126 | } |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 127 | |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 128 | continue; |
| 129 | } |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 130 | |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 131 | /* No cache entry? Start a new one */ |
| 132 | if (!c) { |
| 133 | c = kzalloc(sizeof(*c), GFP_KERNEL); |
Mark Brown | 95f971c | 2013-01-08 13:35:58 +0000 | [diff] [blame] | 134 | if (!c) { |
| 135 | regmap_debugfs_free_dump_cache(map); |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 136 | mutex_unlock(&map->cache_lock); |
Mark Brown | 95f971c | 2013-01-08 13:35:58 +0000 | [diff] [blame] | 137 | return base; |
| 138 | } |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 139 | c->min = p; |
| 140 | c->base_reg = i; |
| 141 | } |
| 142 | |
| 143 | p += map->debugfs_tot_len; |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 144 | } |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 145 | } |
| 146 | |
Mark Brown | e8d6539 | 2013-01-08 18:47:52 +0000 | [diff] [blame] | 147 | /* Close the last entry off if we didn't scan beyond it */ |
| 148 | if (c) { |
| 149 | c->max = p - 1; |
Dimitris Papastamos | 480738d | 2013-02-20 12:15:22 +0000 | [diff] [blame] | 150 | c->max_reg = i - map->reg_stride; |
Mark Brown | e8d6539 | 2013-01-08 18:47:52 +0000 | [diff] [blame] | 151 | list_add_tail(&c->list, |
| 152 | &map->debugfs_off_cache); |
Mark Brown | e8d6539 | 2013-01-08 18:47:52 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Mark Brown | 5bd9f4b | 2013-01-08 13:44:50 +0000 | [diff] [blame] | 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 | */ |
Russell King | a347146 | 2013-01-26 11:45:35 +0000 | [diff] [blame] | 160 | WARN_ON(list_empty(&map->debugfs_off_cache)); |
| 161 | ret = base; |
Mark Brown | 5bd9f4b | 2013-01-08 13:44:50 +0000 | [diff] [blame] | 162 | |
Dimitris Papastamos | cf57d60 | 2013-02-08 12:47:20 +0000 | [diff] [blame] | 163 | /* Find the relevant block:offset */ |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 164 | list_for_each_entry(c, &map->debugfs_off_cache, list) { |
Mark Brown | 5a1d6d1 | 2013-01-08 20:40:19 +0000 | [diff] [blame] | 165 | if (from >= c->min && from <= c->max) { |
Dimitris Papastamos | cf57d60 | 2013-02-08 12:47:20 +0000 | [diff] [blame] | 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); |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 169 | mutex_unlock(&map->cache_lock); |
Srinivas Kandagatla | 213fa5d | 2013-05-14 07:54:23 +0100 | [diff] [blame] | 170 | return c->base_reg + (reg_offset * map->reg_stride); |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 171 | } |
| 172 | |
Dimitris Papastamos | cf57d60 | 2013-02-08 12:47:20 +0000 | [diff] [blame] | 173 | *pos = c->max; |
| 174 | ret = c->max_reg; |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 175 | } |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 176 | mutex_unlock(&map->cache_lock); |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 177 | |
| 178 | return ret; |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 179 | } |
| 180 | |
Dimitris Papastamos | 4dd7c55 | 2013-02-11 10:50:59 +0000 | [diff] [blame] | 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) { |
Mark Brown | 9ae3109 | 2015-09-19 07:31:47 -0700 | [diff] [blame] | 186 | map->debugfs_reg_len = regmap_calc_reg_len(map->max_register), |
Dimitris Papastamos | 4dd7c55 | 2013-02-11 10:50:59 +0000 | [diff] [blame] | 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 | |
Mark Brown | bd9cc12 | 2012-10-03 12:45:37 +0100 | [diff] [blame] | 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) |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 196 | { |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 197 | size_t buf_pos = 0; |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 198 | loff_t p = *ppos; |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 199 | ssize_t ret; |
| 200 | int i; |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 201 | char *buf; |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 202 | unsigned int val, start_reg; |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 203 | |
| 204 | if (*ppos < 0 || !count) |
| 205 | return -EINVAL; |
| 206 | |
| 207 | buf = kmalloc(count, GFP_KERNEL); |
| 208 | if (!buf) |
| 209 | return -ENOMEM; |
| 210 | |
Dimitris Papastamos | 4dd7c55 | 2013-02-11 10:50:59 +0000 | [diff] [blame] | 211 | regmap_calc_tot_len(map, buf, count); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 212 | |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 213 | /* 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 Birsan | 359a2f1 | 2016-08-08 18:44:22 +0300 | [diff] [blame] | 217 | if (!regmap_readable(map, i) && !regmap_cached(map, i)) |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 218 | continue; |
| 219 | |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 220 | if (regmap_precious(map, i)) |
Mark Brown | 2efe164 | 2011-08-08 15:41:46 +0900 | [diff] [blame] | 221 | continue; |
| 222 | |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 223 | /* If we're in the region the user is trying to read */ |
| 224 | if (p >= *ppos) { |
| 225 | /* ...but not beyond it */ |
Dimitris Papastamos | f3eb839 | 2013-02-07 10:51:56 +0000 | [diff] [blame] | 226 | if (buf_pos + map->debugfs_tot_len > count) |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 227 | break; |
| 228 | |
| 229 | /* Format the register */ |
| 230 | snprintf(buf + buf_pos, count - buf_pos, "%.*x: ", |
Mark Brown | cbc1938 | 2012-12-06 13:29:05 +0900 | [diff] [blame] | 231 | map->debugfs_reg_len, i - from); |
| 232 | buf_pos += map->debugfs_reg_len + 2; |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 233 | |
| 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 Brown | cbc1938 | 2012-12-06 13:29:05 +0900 | [diff] [blame] | 238 | "%.*x", map->debugfs_val_len, val); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 239 | else |
Mark Brown | cbc1938 | 2012-12-06 13:29:05 +0900 | [diff] [blame] | 240 | memset(buf + buf_pos, 'X', |
| 241 | map->debugfs_val_len); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 242 | buf_pos += 2 * map->format.val_bytes; |
| 243 | |
| 244 | buf[buf_pos++] = '\n'; |
| 245 | } |
Mark Brown | cbc1938 | 2012-12-06 13:29:05 +0900 | [diff] [blame] | 246 | p += map->debugfs_tot_len; |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 247 | } |
| 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 | |
| 258 | out: |
| 259 | kfree(buf); |
| 260 | return ret; |
| 261 | } |
| 262 | |
Mark Brown | bd9cc12 | 2012-10-03 12:45:37 +0100 | [diff] [blame] | 263 | static 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 Papastamos | 09c6ecd | 2012-02-22 12:43:50 +0000 | [diff] [blame] | 272 | #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 | */ |
| 280 | static 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 Papastamos | 68e850d | 2013-05-09 12:46:41 +0100 | [diff] [blame] | 289 | int ret; |
Dimitris Papastamos | 09c6ecd | 2012-02-22 12:43:50 +0000 | [diff] [blame] | 290 | |
| 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 Han | 34da5e6 | 2013-07-26 13:10:22 +0900 | [diff] [blame] | 301 | if (kstrtoul(start, 16, &value)) |
Dimitris Papastamos | 09c6ecd | 2012-02-22 12:43:50 +0000 | [diff] [blame] | 302 | return -EINVAL; |
| 303 | |
| 304 | /* Userspace has been fiddling around behind the kernel's back */ |
Mark Brown | f9e464a | 2013-05-09 14:35:49 +0100 | [diff] [blame] | 305 | add_taint(TAINT_USER, LOCKDEP_STILL_OK); |
Dimitris Papastamos | 09c6ecd | 2012-02-22 12:43:50 +0000 | [diff] [blame] | 306 | |
Dimitris Papastamos | 68e850d | 2013-05-09 12:46:41 +0100 | [diff] [blame] | 307 | ret = regmap_write(map, reg, value); |
| 308 | if (ret < 0) |
| 309 | return ret; |
Dimitris Papastamos | 09c6ecd | 2012-02-22 12:43:50 +0000 | [diff] [blame] | 310 | return buf_size; |
| 311 | } |
| 312 | #else |
| 313 | #define regmap_map_write_file NULL |
| 314 | #endif |
| 315 | |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 316 | static const struct file_operations regmap_map_fops = { |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 317 | .open = simple_open, |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 318 | .read = regmap_map_read_file, |
Dimitris Papastamos | 09c6ecd | 2012-02-22 12:43:50 +0000 | [diff] [blame] | 319 | .write = regmap_map_write_file, |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 320 | .llseek = default_llseek, |
| 321 | }; |
| 322 | |
Mark Brown | 4b020b3 | 2012-10-03 13:13:16 +0100 | [diff] [blame] | 323 | static 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 | |
| 333 | static const struct file_operations regmap_range_fops = { |
| 334 | .open = simple_open, |
| 335 | .read = regmap_range_read_file, |
| 336 | .llseek = default_llseek, |
| 337 | }; |
| 338 | |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 339 | static 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 Villemoes | e34dc49 | 2015-09-30 20:30:25 +0200 | [diff] [blame] | 350 | unsigned entry_len; |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 351 | |
| 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 Villemoes | ca07e9f | 2015-09-30 20:30:27 +0200 | [diff] [blame] | 378 | entry_len = snprintf(entry, PAGE_SIZE, "%x-%x\n", |
Rasmus Villemoes | e34dc49 | 2015-09-30 20:30:25 +0200 | [diff] [blame] | 379 | c->base_reg, c->max_reg); |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 380 | if (p >= *ppos) { |
Rasmus Villemoes | ca07e9f | 2015-09-30 20:30:27 +0200 | [diff] [blame] | 381 | if (buf_pos + entry_len > count) |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 382 | break; |
Rasmus Villemoes | 20991cd | 2015-09-30 20:30:26 +0200 | [diff] [blame] | 383 | memcpy(buf + buf_pos, entry, entry_len); |
Rasmus Villemoes | e34dc49 | 2015-09-30 20:30:25 +0200 | [diff] [blame] | 384 | buf_pos += entry_len; |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 385 | } |
Rasmus Villemoes | ca07e9f | 2015-09-30 20:30:27 +0200 | [diff] [blame] | 386 | p += entry_len; |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 387 | } |
| 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; |
| 399 | out_buf: |
| 400 | kfree(buf); |
| 401 | return ret; |
| 402 | } |
| 403 | |
| 404 | static 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 Brown | 8da61f2 | 2015-10-20 15:40:59 +0100 | [diff] [blame] | 410 | static int regmap_access_show(struct seq_file *s, void *ignored) |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 411 | { |
Mark Brown | 8da61f2 | 2015-10-20 15:40:59 +0100 | [diff] [blame] | 412 | struct regmap *map = s->private; |
| 413 | int i, reg_len; |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 414 | |
Mark Brown | 9ae3109 | 2015-09-19 07:31:47 -0700 | [diff] [blame] | 415 | reg_len = regmap_calc_reg_len(map->max_register); |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 416 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 417 | for (i = 0; i <= map->max_register; i += map->reg_stride) { |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 418 | /* Ignore registers which are neither readable nor writable */ |
| 419 | if (!regmap_readable(map, i) && !regmap_writeable(map, i)) |
| 420 | continue; |
| 421 | |
Mark Brown | 8da61f2 | 2015-10-20 15:40:59 +0100 | [diff] [blame] | 422 | /* 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 Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 428 | } |
| 429 | |
Mark Brown | 8da61f2 | 2015-10-20 15:40:59 +0100 | [diff] [blame] | 430 | return 0; |
| 431 | } |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 432 | |
Mark Brown | 8da61f2 | 2015-10-20 15:40:59 +0100 | [diff] [blame] | 433 | static int access_open(struct inode *inode, struct file *file) |
| 434 | { |
| 435 | return single_open(file, regmap_access_show, inode->i_private); |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | static const struct file_operations regmap_access_fops = { |
Mark Brown | 8da61f2 | 2015-10-20 15:40:59 +0100 | [diff] [blame] | 439 | .open = access_open, |
| 440 | .read = seq_read, |
| 441 | .llseek = seq_lseek, |
| 442 | .release = single_release, |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 443 | }; |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 444 | |
Richard Fitzgerald | d3dc543 | 2015-06-23 14:32:55 +0100 | [diff] [blame] | 445 | static 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 | |
| 484 | static 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 | |
| 490 | static 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 | |
| 514 | out: |
| 515 | map->unlock(map->lock_arg); |
| 516 | |
| 517 | return result; |
| 518 | } |
| 519 | |
| 520 | static 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 Warren | d3c242e | 2012-04-04 15:48:29 -0600 | [diff] [blame] | 526 | void regmap_debugfs_init(struct regmap *map, const char *name) |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 527 | { |
Mark Brown | 4b020b3 | 2012-10-03 13:13:16 +0100 | [diff] [blame] | 528 | struct rb_node *next; |
| 529 | struct regmap_range_node *range_node; |
Xiubo Li | 2c98e0c | 2014-09-28 11:35:25 +0800 | [diff] [blame] | 530 | const char *devname = "dummy"; |
Mark Brown | 4b020b3 | 2012-10-03 13:13:16 +0100 | [diff] [blame] | 531 | |
Tero Kristo | a52eaeb | 2013-10-24 15:03:41 +0300 | [diff] [blame] | 532 | /* 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(®map_debugfs_early_lock); |
| 541 | list_add(&node->link, ®map_debugfs_early_list); |
| 542 | mutex_unlock(®map_debugfs_early_lock); |
| 543 | return; |
| 544 | } |
| 545 | |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 546 | INIT_LIST_HEAD(&map->debugfs_off_cache); |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 547 | mutex_init(&map->cache_lock); |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 548 | |
Xiubo Li | 2c98e0c | 2014-09-28 11:35:25 +0800 | [diff] [blame] | 549 | if (map->dev) |
| 550 | devname = dev_name(map->dev); |
| 551 | |
Stephen Warren | d3c242e | 2012-04-04 15:48:29 -0600 | [diff] [blame] | 552 | if (name) { |
| 553 | map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s", |
Xiubo Li | 2c98e0c | 2014-09-28 11:35:25 +0800 | [diff] [blame] | 554 | devname, name); |
Stephen Warren | d3c242e | 2012-04-04 15:48:29 -0600 | [diff] [blame] | 555 | name = map->debugfs_name; |
| 556 | } else { |
Xiubo Li | 2c98e0c | 2014-09-28 11:35:25 +0800 | [diff] [blame] | 557 | name = devname; |
Stephen Warren | d3c242e | 2012-04-04 15:48:29 -0600 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | map->debugfs = debugfs_create_dir(name, regmap_debugfs_root); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 561 | if (!map->debugfs) { |
| 562 | dev_warn(map->dev, "Failed to create debugfs directory\n"); |
| 563 | return; |
| 564 | } |
| 565 | |
Dimitris Papastamos | f0c2319 | 2012-02-22 14:20:09 +0000 | [diff] [blame] | 566 | debugfs_create_file("name", 0400, map->debugfs, |
| 567 | map, ®map_name_fops); |
| 568 | |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 569 | debugfs_create_file("range", 0400, map->debugfs, |
| 570 | map, ®map_reg_ranges_fops); |
| 571 | |
Pawel Moll | 676970d | 2014-01-30 13:26:32 +0000 | [diff] [blame] | 572 | if (map->max_register || regmap_readable(map, 0)) { |
Markus Pargmann | ffff7a12a | 2014-09-08 08:43:37 +0200 | [diff] [blame] | 573 | umode_t registers_mode; |
| 574 | |
Axel Lin | 1635e88 | 2015-08-06 10:35:23 +0800 | [diff] [blame] | 575 | #if defined(REGMAP_ALLOW_WRITE_DEBUGFS) |
| 576 | registers_mode = 0600; |
| 577 | #else |
| 578 | registers_mode = 0400; |
| 579 | #endif |
Markus Pargmann | ffff7a12a | 2014-09-08 08:43:37 +0200 | [diff] [blame] | 580 | |
| 581 | debugfs_create_file("registers", registers_mode, map->debugfs, |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 582 | map, ®map_map_fops); |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 583 | debugfs_create_file("access", 0400, map->debugfs, |
| 584 | map, ®map_access_fops); |
| 585 | } |
Mark Brown | 028a01e | 2012-02-06 18:02:06 +0000 | [diff] [blame] | 586 | |
| 587 | if (map->cache_type) { |
Richard Fitzgerald | d3dc543 | 2015-06-23 14:32:55 +0100 | [diff] [blame] | 588 | debugfs_create_file("cache_only", 0600, map->debugfs, |
| 589 | &map->cache_only, ®map_cache_only_fops); |
Mark Brown | 028a01e | 2012-02-06 18:02:06 +0000 | [diff] [blame] | 590 | debugfs_create_bool("cache_dirty", 0400, map->debugfs, |
| 591 | &map->cache_dirty); |
Richard Fitzgerald | d3dc543 | 2015-06-23 14:32:55 +0100 | [diff] [blame] | 592 | debugfs_create_file("cache_bypass", 0600, map->debugfs, |
| 593 | &map->cache_bypass, |
| 594 | ®map_cache_bypass_fops); |
Mark Brown | 028a01e | 2012-02-06 18:02:06 +0000 | [diff] [blame] | 595 | } |
Mark Brown | 4b020b3 | 2012-10-03 13:13:16 +0100 | [diff] [blame] | 596 | |
| 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 | ®map_range_fops); |
| 605 | |
| 606 | next = rb_next(&range_node->node); |
| 607 | } |
Lars-Peter Clausen | 5e0cbe7 | 2014-08-24 15:32:27 +0200 | [diff] [blame] | 608 | |
| 609 | if (map->cache_ops && map->cache_ops->debugfs_init) |
| 610 | map->cache_ops->debugfs_init(map); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | void regmap_debugfs_exit(struct regmap *map) |
| 614 | { |
Tero Kristo | a52eaeb | 2013-10-24 15:03:41 +0300 | [diff] [blame] | 615 | 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(®map_debugfs_early_lock); |
| 625 | list_for_each_entry_safe(node, tmp, ®map_debugfs_early_list, |
| 626 | link) { |
| 627 | if (node->map == map) { |
| 628 | list_del(&node->link); |
| 629 | kfree(node); |
| 630 | } |
| 631 | } |
| 632 | mutex_unlock(®map_debugfs_early_lock); |
| 633 | } |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | void regmap_debugfs_initcall(void) |
| 637 | { |
Tero Kristo | a52eaeb | 2013-10-24 15:03:41 +0300 | [diff] [blame] | 638 | struct regmap_debugfs_node *node, *tmp; |
| 639 | |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 640 | 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 Kristo | a52eaeb | 2013-10-24 15:03:41 +0300 | [diff] [blame] | 645 | |
| 646 | mutex_lock(®map_debugfs_early_lock); |
| 647 | list_for_each_entry_safe(node, tmp, ®map_debugfs_early_list, link) { |
| 648 | regmap_debugfs_init(node->map, node->name); |
| 649 | list_del(&node->link); |
| 650 | kfree(node); |
| 651 | } |
| 652 | mutex_unlock(®map_debugfs_early_lock); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 653 | } |