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 | |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 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 | { |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 89 | struct regmap_debugfs_off_cache *c = NULL; |
| 90 | loff_t p = 0; |
| 91 | unsigned int i, ret; |
Dimitris Papastamos | c2c1ee6 | 2013-02-08 12:47:14 +0000 | [diff] [blame] | 92 | unsigned int fpos_offset; |
| 93 | unsigned int reg_offset; |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 94 | |
Mark Brown | d6814a7 | 2013-05-29 15:54:54 +0100 | [diff] [blame] | 95 | /* Suppress the cache if we're using a subrange */ |
Lars-Peter Clausen | 26ee474 | 2013-08-28 17:55:07 +0200 | [diff] [blame] | 96 | if (base) |
| 97 | return base; |
Mark Brown | d6814a7 | 2013-05-29 15:54:54 +0100 | [diff] [blame] | 98 | |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 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 | */ |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 103 | mutex_lock(&map->cache_lock); |
Dimitris Papastamos | 480738d | 2013-02-20 12:15:22 +0000 | [diff] [blame] | 104 | i = base; |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 105 | if (list_empty(&map->debugfs_off_cache)) { |
Dimitris Papastamos | 480738d | 2013-02-20 12:15:22 +0000 | [diff] [blame] | 106 | for (; i <= map->max_register; i += map->reg_stride) { |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 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; |
Dimitris Papastamos | 480738d | 2013-02-20 12:15:22 +0000 | [diff] [blame] | 112 | c->max_reg = i - map->reg_stride; |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 113 | list_add_tail(&c->list, |
| 114 | &map->debugfs_off_cache); |
| 115 | c = NULL; |
| 116 | } |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 117 | |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 118 | continue; |
| 119 | } |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 120 | |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 121 | /* No cache entry? Start a new one */ |
| 122 | if (!c) { |
| 123 | c = kzalloc(sizeof(*c), GFP_KERNEL); |
Mark Brown | 95f971c | 2013-01-08 13:35:58 +0000 | [diff] [blame] | 124 | if (!c) { |
| 125 | regmap_debugfs_free_dump_cache(map); |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 126 | mutex_unlock(&map->cache_lock); |
Mark Brown | 95f971c | 2013-01-08 13:35:58 +0000 | [diff] [blame] | 127 | return base; |
| 128 | } |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 129 | c->min = p; |
| 130 | c->base_reg = i; |
| 131 | } |
| 132 | |
| 133 | p += map->debugfs_tot_len; |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 134 | } |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 135 | } |
| 136 | |
Mark Brown | e8d6539 | 2013-01-08 18:47:52 +0000 | [diff] [blame] | 137 | /* Close the last entry off if we didn't scan beyond it */ |
| 138 | if (c) { |
| 139 | c->max = p - 1; |
Dimitris Papastamos | 480738d | 2013-02-20 12:15:22 +0000 | [diff] [blame] | 140 | c->max_reg = i - map->reg_stride; |
Mark Brown | e8d6539 | 2013-01-08 18:47:52 +0000 | [diff] [blame] | 141 | list_add_tail(&c->list, |
| 142 | &map->debugfs_off_cache); |
Mark Brown | e8d6539 | 2013-01-08 18:47:52 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Mark Brown | 5bd9f4b | 2013-01-08 13:44:50 +0000 | [diff] [blame] | 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 | */ |
Russell King | a347146 | 2013-01-26 11:45:35 +0000 | [diff] [blame] | 150 | WARN_ON(list_empty(&map->debugfs_off_cache)); |
| 151 | ret = base; |
Mark Brown | 5bd9f4b | 2013-01-08 13:44:50 +0000 | [diff] [blame] | 152 | |
Dimitris Papastamos | cf57d60 | 2013-02-08 12:47:20 +0000 | [diff] [blame] | 153 | /* Find the relevant block:offset */ |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 154 | list_for_each_entry(c, &map->debugfs_off_cache, list) { |
Mark Brown | 5a1d6d1 | 2013-01-08 20:40:19 +0000 | [diff] [blame] | 155 | if (from >= c->min && from <= c->max) { |
Dimitris Papastamos | cf57d60 | 2013-02-08 12:47:20 +0000 | [diff] [blame] | 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); |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 159 | mutex_unlock(&map->cache_lock); |
Srinivas Kandagatla | 213fa5d | 2013-05-14 07:54:23 +0100 | [diff] [blame] | 160 | return c->base_reg + (reg_offset * map->reg_stride); |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 161 | } |
| 162 | |
Dimitris Papastamos | cf57d60 | 2013-02-08 12:47:20 +0000 | [diff] [blame] | 163 | *pos = c->max; |
| 164 | ret = c->max_reg; |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 165 | } |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 166 | mutex_unlock(&map->cache_lock); |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 167 | |
| 168 | return ret; |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 169 | } |
| 170 | |
Dimitris Papastamos | 4dd7c55 | 2013-02-11 10:50:59 +0000 | [diff] [blame] | 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) { |
Mark Brown | 9ae3109 | 2015-09-19 07:31:47 -0700 | [diff] [blame] | 176 | map->debugfs_reg_len = regmap_calc_reg_len(map->max_register), |
Dimitris Papastamos | 4dd7c55 | 2013-02-11 10:50:59 +0000 | [diff] [blame] | 177 | map->debugfs_val_len = 2 * map->format.val_bytes; |
| 178 | map->debugfs_tot_len = map->debugfs_reg_len + |
| 179 | map->debugfs_val_len + 3; /* : \n */ |
| 180 | } |
| 181 | } |
| 182 | |
Mark Brown | bd9cc12 | 2012-10-03 12:45:37 +0100 | [diff] [blame] | 183 | static ssize_t regmap_read_debugfs(struct regmap *map, unsigned int from, |
| 184 | unsigned int to, char __user *user_buf, |
| 185 | size_t count, loff_t *ppos) |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 186 | { |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 187 | size_t buf_pos = 0; |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 188 | loff_t p = *ppos; |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 189 | ssize_t ret; |
| 190 | int i; |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 191 | char *buf; |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 192 | unsigned int val, start_reg; |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 193 | |
| 194 | if (*ppos < 0 || !count) |
| 195 | return -EINVAL; |
| 196 | |
| 197 | buf = kmalloc(count, GFP_KERNEL); |
| 198 | if (!buf) |
| 199 | return -ENOMEM; |
| 200 | |
Dimitris Papastamos | 4dd7c55 | 2013-02-11 10:50:59 +0000 | [diff] [blame] | 201 | regmap_calc_tot_len(map, buf, count); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 202 | |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 203 | /* Work out which register we're starting at */ |
| 204 | start_reg = regmap_debugfs_get_dump_start(map, from, *ppos, &p); |
| 205 | |
| 206 | for (i = start_reg; i <= to; i += map->reg_stride) { |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 207 | if (!regmap_readable(map, i)) |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 208 | continue; |
| 209 | |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 210 | if (regmap_precious(map, i)) |
Mark Brown | 2efe164 | 2011-08-08 15:41:46 +0900 | [diff] [blame] | 211 | continue; |
| 212 | |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 213 | /* If we're in the region the user is trying to read */ |
| 214 | if (p >= *ppos) { |
| 215 | /* ...but not beyond it */ |
Dimitris Papastamos | f3eb839 | 2013-02-07 10:51:56 +0000 | [diff] [blame] | 216 | if (buf_pos + map->debugfs_tot_len > count) |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 217 | break; |
| 218 | |
| 219 | /* Format the register */ |
| 220 | snprintf(buf + buf_pos, count - buf_pos, "%.*x: ", |
Mark Brown | cbc1938 | 2012-12-06 13:29:05 +0900 | [diff] [blame] | 221 | map->debugfs_reg_len, i - from); |
| 222 | buf_pos += map->debugfs_reg_len + 2; |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 223 | |
| 224 | /* Format the value, write all X if we can't read */ |
| 225 | ret = regmap_read(map, i, &val); |
| 226 | if (ret == 0) |
| 227 | snprintf(buf + buf_pos, count - buf_pos, |
Mark Brown | cbc1938 | 2012-12-06 13:29:05 +0900 | [diff] [blame] | 228 | "%.*x", map->debugfs_val_len, val); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 229 | else |
Mark Brown | cbc1938 | 2012-12-06 13:29:05 +0900 | [diff] [blame] | 230 | memset(buf + buf_pos, 'X', |
| 231 | map->debugfs_val_len); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 232 | buf_pos += 2 * map->format.val_bytes; |
| 233 | |
| 234 | buf[buf_pos++] = '\n'; |
| 235 | } |
Mark Brown | cbc1938 | 2012-12-06 13:29:05 +0900 | [diff] [blame] | 236 | p += map->debugfs_tot_len; |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | ret = buf_pos; |
| 240 | |
| 241 | if (copy_to_user(user_buf, buf, buf_pos)) { |
| 242 | ret = -EFAULT; |
| 243 | goto out; |
| 244 | } |
| 245 | |
| 246 | *ppos += buf_pos; |
| 247 | |
| 248 | out: |
| 249 | kfree(buf); |
| 250 | return ret; |
| 251 | } |
| 252 | |
Mark Brown | bd9cc12 | 2012-10-03 12:45:37 +0100 | [diff] [blame] | 253 | static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf, |
| 254 | size_t count, loff_t *ppos) |
| 255 | { |
| 256 | struct regmap *map = file->private_data; |
| 257 | |
| 258 | return regmap_read_debugfs(map, 0, map->max_register, user_buf, |
| 259 | count, ppos); |
| 260 | } |
| 261 | |
Dimitris Papastamos | 09c6ecd | 2012-02-22 12:43:50 +0000 | [diff] [blame] | 262 | #undef REGMAP_ALLOW_WRITE_DEBUGFS |
| 263 | #ifdef REGMAP_ALLOW_WRITE_DEBUGFS |
| 264 | /* |
| 265 | * This can be dangerous especially when we have clients such as |
| 266 | * PMICs, therefore don't provide any real compile time configuration option |
| 267 | * for this feature, people who want to use this will need to modify |
| 268 | * the source code directly. |
| 269 | */ |
| 270 | static ssize_t regmap_map_write_file(struct file *file, |
| 271 | const char __user *user_buf, |
| 272 | size_t count, loff_t *ppos) |
| 273 | { |
| 274 | char buf[32]; |
| 275 | size_t buf_size; |
| 276 | char *start = buf; |
| 277 | unsigned long reg, value; |
| 278 | struct regmap *map = file->private_data; |
Dimitris Papastamos | 68e850d | 2013-05-09 12:46:41 +0100 | [diff] [blame] | 279 | int ret; |
Dimitris Papastamos | 09c6ecd | 2012-02-22 12:43:50 +0000 | [diff] [blame] | 280 | |
| 281 | buf_size = min(count, (sizeof(buf)-1)); |
| 282 | if (copy_from_user(buf, user_buf, buf_size)) |
| 283 | return -EFAULT; |
| 284 | buf[buf_size] = 0; |
| 285 | |
| 286 | while (*start == ' ') |
| 287 | start++; |
| 288 | reg = simple_strtoul(start, &start, 16); |
| 289 | while (*start == ' ') |
| 290 | start++; |
Jingoo Han | 34da5e6 | 2013-07-26 13:10:22 +0900 | [diff] [blame] | 291 | if (kstrtoul(start, 16, &value)) |
Dimitris Papastamos | 09c6ecd | 2012-02-22 12:43:50 +0000 | [diff] [blame] | 292 | return -EINVAL; |
| 293 | |
| 294 | /* Userspace has been fiddling around behind the kernel's back */ |
Mark Brown | f9e464a | 2013-05-09 14:35:49 +0100 | [diff] [blame] | 295 | add_taint(TAINT_USER, LOCKDEP_STILL_OK); |
Dimitris Papastamos | 09c6ecd | 2012-02-22 12:43:50 +0000 | [diff] [blame] | 296 | |
Dimitris Papastamos | 68e850d | 2013-05-09 12:46:41 +0100 | [diff] [blame] | 297 | ret = regmap_write(map, reg, value); |
| 298 | if (ret < 0) |
| 299 | return ret; |
Dimitris Papastamos | 09c6ecd | 2012-02-22 12:43:50 +0000 | [diff] [blame] | 300 | return buf_size; |
| 301 | } |
| 302 | #else |
| 303 | #define regmap_map_write_file NULL |
| 304 | #endif |
| 305 | |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 306 | static const struct file_operations regmap_map_fops = { |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 307 | .open = simple_open, |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 308 | .read = regmap_map_read_file, |
Dimitris Papastamos | 09c6ecd | 2012-02-22 12:43:50 +0000 | [diff] [blame] | 309 | .write = regmap_map_write_file, |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 310 | .llseek = default_llseek, |
| 311 | }; |
| 312 | |
Mark Brown | 4b020b3 | 2012-10-03 13:13:16 +0100 | [diff] [blame] | 313 | static ssize_t regmap_range_read_file(struct file *file, char __user *user_buf, |
| 314 | size_t count, loff_t *ppos) |
| 315 | { |
| 316 | struct regmap_range_node *range = file->private_data; |
| 317 | struct regmap *map = range->map; |
| 318 | |
| 319 | return regmap_read_debugfs(map, range->range_min, range->range_max, |
| 320 | user_buf, count, ppos); |
| 321 | } |
| 322 | |
| 323 | static const struct file_operations regmap_range_fops = { |
| 324 | .open = simple_open, |
| 325 | .read = regmap_range_read_file, |
| 326 | .llseek = default_llseek, |
| 327 | }; |
| 328 | |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 329 | static ssize_t regmap_reg_ranges_read_file(struct file *file, |
| 330 | char __user *user_buf, size_t count, |
| 331 | loff_t *ppos) |
| 332 | { |
| 333 | struct regmap *map = file->private_data; |
| 334 | struct regmap_debugfs_off_cache *c; |
| 335 | loff_t p = 0; |
| 336 | size_t buf_pos = 0; |
| 337 | char *buf; |
| 338 | char *entry; |
| 339 | int ret; |
Rasmus Villemoes | e34dc49 | 2015-09-30 20:30:25 +0200 | [diff] [blame] | 340 | unsigned entry_len; |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 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) { |
Rasmus Villemoes | ca07e9f | 2015-09-30 20:30:27 +0200 | [diff] [blame] | 368 | entry_len = snprintf(entry, PAGE_SIZE, "%x-%x\n", |
Rasmus Villemoes | e34dc49 | 2015-09-30 20:30:25 +0200 | [diff] [blame] | 369 | c->base_reg, c->max_reg); |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 370 | if (p >= *ppos) { |
Rasmus Villemoes | ca07e9f | 2015-09-30 20:30:27 +0200 | [diff] [blame] | 371 | if (buf_pos + entry_len > count) |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 372 | break; |
Rasmus Villemoes | 20991cd | 2015-09-30 20:30:26 +0200 | [diff] [blame] | 373 | memcpy(buf + buf_pos, entry, entry_len); |
Rasmus Villemoes | e34dc49 | 2015-09-30 20:30:25 +0200 | [diff] [blame] | 374 | buf_pos += entry_len; |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 375 | } |
Rasmus Villemoes | ca07e9f | 2015-09-30 20:30:27 +0200 | [diff] [blame] | 376 | p += entry_len; |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 377 | } |
| 378 | mutex_unlock(&map->cache_lock); |
| 379 | |
| 380 | kfree(entry); |
| 381 | ret = buf_pos; |
| 382 | |
| 383 | if (copy_to_user(user_buf, buf, buf_pos)) { |
| 384 | ret = -EFAULT; |
| 385 | goto out_buf; |
| 386 | } |
| 387 | |
| 388 | *ppos += buf_pos; |
| 389 | out_buf: |
| 390 | kfree(buf); |
| 391 | return ret; |
| 392 | } |
| 393 | |
| 394 | static const struct file_operations regmap_reg_ranges_fops = { |
| 395 | .open = simple_open, |
| 396 | .read = regmap_reg_ranges_read_file, |
| 397 | .llseek = default_llseek, |
| 398 | }; |
| 399 | |
Mark Brown | 8da61f2 | 2015-10-20 15:40:59 +0100 | [diff] [blame] | 400 | static int regmap_access_show(struct seq_file *s, void *ignored) |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 401 | { |
Mark Brown | 8da61f2 | 2015-10-20 15:40:59 +0100 | [diff] [blame] | 402 | struct regmap *map = s->private; |
| 403 | int i, reg_len; |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 404 | |
Mark Brown | 9ae3109 | 2015-09-19 07:31:47 -0700 | [diff] [blame] | 405 | reg_len = regmap_calc_reg_len(map->max_register); |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 406 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 407 | for (i = 0; i <= map->max_register; i += map->reg_stride) { |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 408 | /* Ignore registers which are neither readable nor writable */ |
| 409 | if (!regmap_readable(map, i) && !regmap_writeable(map, i)) |
| 410 | continue; |
| 411 | |
Mark Brown | 8da61f2 | 2015-10-20 15:40:59 +0100 | [diff] [blame] | 412 | /* Format the register */ |
| 413 | seq_printf(s, "%.*x: %c %c %c %c\n", reg_len, i, |
| 414 | regmap_readable(map, i) ? 'y' : 'n', |
| 415 | regmap_writeable(map, i) ? 'y' : 'n', |
| 416 | regmap_volatile(map, i) ? 'y' : 'n', |
| 417 | regmap_precious(map, i) ? 'y' : 'n'); |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 418 | } |
| 419 | |
Mark Brown | 8da61f2 | 2015-10-20 15:40:59 +0100 | [diff] [blame] | 420 | return 0; |
| 421 | } |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 422 | |
Mark Brown | 8da61f2 | 2015-10-20 15:40:59 +0100 | [diff] [blame] | 423 | static int access_open(struct inode *inode, struct file *file) |
| 424 | { |
| 425 | return single_open(file, regmap_access_show, inode->i_private); |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | static const struct file_operations regmap_access_fops = { |
Mark Brown | 8da61f2 | 2015-10-20 15:40:59 +0100 | [diff] [blame] | 429 | .open = access_open, |
| 430 | .read = seq_read, |
| 431 | .llseek = seq_lseek, |
| 432 | .release = single_release, |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 433 | }; |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 434 | |
Richard Fitzgerald | d3dc543 | 2015-06-23 14:32:55 +0100 | [diff] [blame] | 435 | static ssize_t regmap_cache_only_write_file(struct file *file, |
| 436 | const char __user *user_buf, |
| 437 | size_t count, loff_t *ppos) |
| 438 | { |
| 439 | struct regmap *map = container_of(file->private_data, |
| 440 | struct regmap, cache_only); |
| 441 | ssize_t result; |
| 442 | bool was_enabled, require_sync = false; |
| 443 | int err; |
| 444 | |
| 445 | map->lock(map->lock_arg); |
| 446 | |
| 447 | was_enabled = map->cache_only; |
| 448 | |
| 449 | result = debugfs_write_file_bool(file, user_buf, count, ppos); |
| 450 | if (result < 0) { |
| 451 | map->unlock(map->lock_arg); |
| 452 | return result; |
| 453 | } |
| 454 | |
| 455 | if (map->cache_only && !was_enabled) { |
| 456 | dev_warn(map->dev, "debugfs cache_only=Y forced\n"); |
| 457 | add_taint(TAINT_USER, LOCKDEP_STILL_OK); |
| 458 | } else if (!map->cache_only && was_enabled) { |
| 459 | dev_warn(map->dev, "debugfs cache_only=N forced: syncing cache\n"); |
| 460 | require_sync = true; |
| 461 | } |
| 462 | |
| 463 | map->unlock(map->lock_arg); |
| 464 | |
| 465 | if (require_sync) { |
| 466 | err = regcache_sync(map); |
| 467 | if (err) |
| 468 | dev_err(map->dev, "Failed to sync cache %d\n", err); |
| 469 | } |
| 470 | |
| 471 | return result; |
| 472 | } |
| 473 | |
| 474 | static const struct file_operations regmap_cache_only_fops = { |
| 475 | .open = simple_open, |
| 476 | .read = debugfs_read_file_bool, |
| 477 | .write = regmap_cache_only_write_file, |
| 478 | }; |
| 479 | |
| 480 | static ssize_t regmap_cache_bypass_write_file(struct file *file, |
| 481 | const char __user *user_buf, |
| 482 | size_t count, loff_t *ppos) |
| 483 | { |
| 484 | struct regmap *map = container_of(file->private_data, |
| 485 | struct regmap, cache_bypass); |
| 486 | ssize_t result; |
| 487 | bool was_enabled; |
| 488 | |
| 489 | map->lock(map->lock_arg); |
| 490 | |
| 491 | was_enabled = map->cache_bypass; |
| 492 | |
| 493 | result = debugfs_write_file_bool(file, user_buf, count, ppos); |
| 494 | if (result < 0) |
| 495 | goto out; |
| 496 | |
| 497 | if (map->cache_bypass && !was_enabled) { |
| 498 | dev_warn(map->dev, "debugfs cache_bypass=Y forced\n"); |
| 499 | add_taint(TAINT_USER, LOCKDEP_STILL_OK); |
| 500 | } else if (!map->cache_bypass && was_enabled) { |
| 501 | dev_warn(map->dev, "debugfs cache_bypass=N forced\n"); |
| 502 | } |
| 503 | |
| 504 | out: |
| 505 | map->unlock(map->lock_arg); |
| 506 | |
| 507 | return result; |
| 508 | } |
| 509 | |
| 510 | static const struct file_operations regmap_cache_bypass_fops = { |
| 511 | .open = simple_open, |
| 512 | .read = debugfs_read_file_bool, |
| 513 | .write = regmap_cache_bypass_write_file, |
| 514 | }; |
| 515 | |
Stephen Warren | d3c242e | 2012-04-04 15:48:29 -0600 | [diff] [blame] | 516 | void regmap_debugfs_init(struct regmap *map, const char *name) |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 517 | { |
Mark Brown | 4b020b3 | 2012-10-03 13:13:16 +0100 | [diff] [blame] | 518 | struct rb_node *next; |
| 519 | struct regmap_range_node *range_node; |
Xiubo Li | 2c98e0c | 2014-09-28 11:35:25 +0800 | [diff] [blame] | 520 | const char *devname = "dummy"; |
Mark Brown | 4b020b3 | 2012-10-03 13:13:16 +0100 | [diff] [blame] | 521 | |
Tero Kristo | a52eaeb | 2013-10-24 15:03:41 +0300 | [diff] [blame] | 522 | /* If we don't have the debugfs root yet, postpone init */ |
| 523 | if (!regmap_debugfs_root) { |
| 524 | struct regmap_debugfs_node *node; |
| 525 | node = kzalloc(sizeof(*node), GFP_KERNEL); |
| 526 | if (!node) |
| 527 | return; |
| 528 | node->map = map; |
| 529 | node->name = name; |
| 530 | mutex_lock(®map_debugfs_early_lock); |
| 531 | list_add(&node->link, ®map_debugfs_early_list); |
| 532 | mutex_unlock(®map_debugfs_early_lock); |
| 533 | return; |
| 534 | } |
| 535 | |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 536 | INIT_LIST_HEAD(&map->debugfs_off_cache); |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 537 | mutex_init(&map->cache_lock); |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 538 | |
Xiubo Li | 2c98e0c | 2014-09-28 11:35:25 +0800 | [diff] [blame] | 539 | if (map->dev) |
| 540 | devname = dev_name(map->dev); |
| 541 | |
Stephen Warren | d3c242e | 2012-04-04 15:48:29 -0600 | [diff] [blame] | 542 | if (name) { |
| 543 | map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s", |
Xiubo Li | 2c98e0c | 2014-09-28 11:35:25 +0800 | [diff] [blame] | 544 | devname, name); |
Stephen Warren | d3c242e | 2012-04-04 15:48:29 -0600 | [diff] [blame] | 545 | name = map->debugfs_name; |
| 546 | } else { |
Xiubo Li | 2c98e0c | 2014-09-28 11:35:25 +0800 | [diff] [blame] | 547 | name = devname; |
Stephen Warren | d3c242e | 2012-04-04 15:48:29 -0600 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | map->debugfs = debugfs_create_dir(name, regmap_debugfs_root); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 551 | if (!map->debugfs) { |
| 552 | dev_warn(map->dev, "Failed to create debugfs directory\n"); |
| 553 | return; |
| 554 | } |
| 555 | |
Dimitris Papastamos | f0c2319 | 2012-02-22 14:20:09 +0000 | [diff] [blame] | 556 | debugfs_create_file("name", 0400, map->debugfs, |
| 557 | map, ®map_name_fops); |
| 558 | |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 559 | debugfs_create_file("range", 0400, map->debugfs, |
| 560 | map, ®map_reg_ranges_fops); |
| 561 | |
Pawel Moll | 676970d | 2014-01-30 13:26:32 +0000 | [diff] [blame] | 562 | if (map->max_register || regmap_readable(map, 0)) { |
Markus Pargmann | ffff7a12a | 2014-09-08 08:43:37 +0200 | [diff] [blame] | 563 | umode_t registers_mode; |
| 564 | |
Axel Lin | 1635e88 | 2015-08-06 10:35:23 +0800 | [diff] [blame] | 565 | #if defined(REGMAP_ALLOW_WRITE_DEBUGFS) |
| 566 | registers_mode = 0600; |
| 567 | #else |
| 568 | registers_mode = 0400; |
| 569 | #endif |
Markus Pargmann | ffff7a12a | 2014-09-08 08:43:37 +0200 | [diff] [blame] | 570 | |
| 571 | debugfs_create_file("registers", registers_mode, map->debugfs, |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 572 | map, ®map_map_fops); |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 573 | debugfs_create_file("access", 0400, map->debugfs, |
| 574 | map, ®map_access_fops); |
| 575 | } |
Mark Brown | 028a01e | 2012-02-06 18:02:06 +0000 | [diff] [blame] | 576 | |
| 577 | if (map->cache_type) { |
Richard Fitzgerald | d3dc543 | 2015-06-23 14:32:55 +0100 | [diff] [blame] | 578 | debugfs_create_file("cache_only", 0600, map->debugfs, |
| 579 | &map->cache_only, ®map_cache_only_fops); |
Mark Brown | 028a01e | 2012-02-06 18:02:06 +0000 | [diff] [blame] | 580 | debugfs_create_bool("cache_dirty", 0400, map->debugfs, |
| 581 | &map->cache_dirty); |
Richard Fitzgerald | d3dc543 | 2015-06-23 14:32:55 +0100 | [diff] [blame] | 582 | debugfs_create_file("cache_bypass", 0600, map->debugfs, |
| 583 | &map->cache_bypass, |
| 584 | ®map_cache_bypass_fops); |
Mark Brown | 028a01e | 2012-02-06 18:02:06 +0000 | [diff] [blame] | 585 | } |
Mark Brown | 4b020b3 | 2012-10-03 13:13:16 +0100 | [diff] [blame] | 586 | |
| 587 | next = rb_first(&map->range_tree); |
| 588 | while (next) { |
| 589 | range_node = rb_entry(next, struct regmap_range_node, node); |
| 590 | |
| 591 | if (range_node->name) |
| 592 | debugfs_create_file(range_node->name, 0400, |
| 593 | map->debugfs, range_node, |
| 594 | ®map_range_fops); |
| 595 | |
| 596 | next = rb_next(&range_node->node); |
| 597 | } |
Lars-Peter Clausen | 5e0cbe7 | 2014-08-24 15:32:27 +0200 | [diff] [blame] | 598 | |
| 599 | if (map->cache_ops && map->cache_ops->debugfs_init) |
| 600 | map->cache_ops->debugfs_init(map); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | void regmap_debugfs_exit(struct regmap *map) |
| 604 | { |
Tero Kristo | a52eaeb | 2013-10-24 15:03:41 +0300 | [diff] [blame] | 605 | if (map->debugfs) { |
| 606 | debugfs_remove_recursive(map->debugfs); |
| 607 | mutex_lock(&map->cache_lock); |
| 608 | regmap_debugfs_free_dump_cache(map); |
| 609 | mutex_unlock(&map->cache_lock); |
| 610 | kfree(map->debugfs_name); |
| 611 | } else { |
| 612 | struct regmap_debugfs_node *node, *tmp; |
| 613 | |
| 614 | mutex_lock(®map_debugfs_early_lock); |
| 615 | list_for_each_entry_safe(node, tmp, ®map_debugfs_early_list, |
| 616 | link) { |
| 617 | if (node->map == map) { |
| 618 | list_del(&node->link); |
| 619 | kfree(node); |
| 620 | } |
| 621 | } |
| 622 | mutex_unlock(®map_debugfs_early_lock); |
| 623 | } |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | void regmap_debugfs_initcall(void) |
| 627 | { |
Tero Kristo | a52eaeb | 2013-10-24 15:03:41 +0300 | [diff] [blame] | 628 | struct regmap_debugfs_node *node, *tmp; |
| 629 | |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 630 | regmap_debugfs_root = debugfs_create_dir("regmap", NULL); |
| 631 | if (!regmap_debugfs_root) { |
| 632 | pr_warn("regmap: Failed to create debugfs root\n"); |
| 633 | return; |
| 634 | } |
Tero Kristo | a52eaeb | 2013-10-24 15:03:41 +0300 | [diff] [blame] | 635 | |
| 636 | mutex_lock(®map_debugfs_early_lock); |
| 637 | list_for_each_entry_safe(node, tmp, ®map_debugfs_early_list, link) { |
| 638 | regmap_debugfs_init(node->map, node->name); |
| 639 | list_del(&node->link); |
| 640 | kfree(node); |
| 641 | } |
| 642 | mutex_unlock(®map_debugfs_early_lock); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 643 | } |