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> |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 18 | |
| 19 | #include "internal.h" |
| 20 | |
| 21 | static struct dentry *regmap_debugfs_root; |
| 22 | |
Mark Brown | 21f5554 | 2011-08-10 17:15:31 +0900 | [diff] [blame] | 23 | /* Calculate the length of a fixed format */ |
| 24 | static size_t regmap_calc_reg_len(int max_val, char *buf, size_t buf_size) |
| 25 | { |
| 26 | snprintf(buf, buf_size, "%x", max_val); |
| 27 | return strlen(buf); |
| 28 | } |
| 29 | |
Dimitris Papastamos | f0c2319 | 2012-02-22 14:20:09 +0000 | [diff] [blame] | 30 | static ssize_t regmap_name_read_file(struct file *file, |
| 31 | char __user *user_buf, size_t count, |
| 32 | loff_t *ppos) |
| 33 | { |
| 34 | struct regmap *map = file->private_data; |
| 35 | int ret; |
| 36 | char *buf; |
| 37 | |
| 38 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 39 | if (!buf) |
| 40 | return -ENOMEM; |
| 41 | |
| 42 | ret = snprintf(buf, PAGE_SIZE, "%s\n", map->dev->driver->name); |
| 43 | if (ret < 0) { |
| 44 | kfree(buf); |
| 45 | return ret; |
| 46 | } |
| 47 | |
| 48 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); |
| 49 | kfree(buf); |
| 50 | return ret; |
| 51 | } |
| 52 | |
| 53 | static const struct file_operations regmap_name_fops = { |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 54 | .open = simple_open, |
Dimitris Papastamos | f0c2319 | 2012-02-22 14:20:09 +0000 | [diff] [blame] | 55 | .read = regmap_name_read_file, |
| 56 | .llseek = default_llseek, |
| 57 | }; |
| 58 | |
Mark Brown | 95f971c | 2013-01-08 13:35:58 +0000 | [diff] [blame] | 59 | static void regmap_debugfs_free_dump_cache(struct regmap *map) |
| 60 | { |
| 61 | struct regmap_debugfs_off_cache *c; |
| 62 | |
| 63 | while (!list_empty(&map->debugfs_off_cache)) { |
| 64 | c = list_first_entry(&map->debugfs_off_cache, |
| 65 | struct regmap_debugfs_off_cache, |
| 66 | list); |
| 67 | list_del(&c->list); |
| 68 | kfree(c); |
| 69 | } |
| 70 | } |
| 71 | |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 72 | /* |
| 73 | * Work out where the start offset maps into register numbers, bearing |
| 74 | * in mind that we suppress hidden registers. |
| 75 | */ |
| 76 | static unsigned int regmap_debugfs_get_dump_start(struct regmap *map, |
| 77 | unsigned int base, |
| 78 | loff_t from, |
| 79 | loff_t *pos) |
| 80 | { |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 81 | struct regmap_debugfs_off_cache *c = NULL; |
| 82 | loff_t p = 0; |
| 83 | unsigned int i, ret; |
Dimitris Papastamos | c2c1ee6 | 2013-02-08 12:47:14 +0000 | [diff] [blame] | 84 | unsigned int fpos_offset; |
| 85 | unsigned int reg_offset; |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 86 | |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 87 | /* |
| 88 | * If we don't have a cache build one so we don't have to do a |
| 89 | * linear scan each time. |
| 90 | */ |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 91 | mutex_lock(&map->cache_lock); |
Dimitris Papastamos | 480738d | 2013-02-20 12:15:22 +0000 | [diff] [blame] | 92 | i = base; |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 93 | if (list_empty(&map->debugfs_off_cache)) { |
Dimitris Papastamos | 480738d | 2013-02-20 12:15:22 +0000 | [diff] [blame] | 94 | for (; i <= map->max_register; i += map->reg_stride) { |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 95 | /* Skip unprinted registers, closing off cache entry */ |
| 96 | if (!regmap_readable(map, i) || |
| 97 | regmap_precious(map, i)) { |
| 98 | if (c) { |
| 99 | c->max = p - 1; |
Dimitris Papastamos | 480738d | 2013-02-20 12:15:22 +0000 | [diff] [blame] | 100 | c->max_reg = i - map->reg_stride; |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 101 | list_add_tail(&c->list, |
| 102 | &map->debugfs_off_cache); |
| 103 | c = NULL; |
| 104 | } |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 105 | |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 106 | continue; |
| 107 | } |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 108 | |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 109 | /* No cache entry? Start a new one */ |
| 110 | if (!c) { |
| 111 | c = kzalloc(sizeof(*c), GFP_KERNEL); |
Mark Brown | 95f971c | 2013-01-08 13:35:58 +0000 | [diff] [blame] | 112 | if (!c) { |
| 113 | regmap_debugfs_free_dump_cache(map); |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 114 | mutex_unlock(&map->cache_lock); |
Mark Brown | 95f971c | 2013-01-08 13:35:58 +0000 | [diff] [blame] | 115 | return base; |
| 116 | } |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 117 | c->min = p; |
| 118 | c->base_reg = i; |
| 119 | } |
| 120 | |
| 121 | p += map->debugfs_tot_len; |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 122 | } |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 123 | } |
| 124 | |
Mark Brown | e8d6539 | 2013-01-08 18:47:52 +0000 | [diff] [blame] | 125 | /* Close the last entry off if we didn't scan beyond it */ |
| 126 | if (c) { |
| 127 | c->max = p - 1; |
Dimitris Papastamos | 480738d | 2013-02-20 12:15:22 +0000 | [diff] [blame] | 128 | c->max_reg = i - map->reg_stride; |
Mark Brown | e8d6539 | 2013-01-08 18:47:52 +0000 | [diff] [blame] | 129 | list_add_tail(&c->list, |
| 130 | &map->debugfs_off_cache); |
Mark Brown | e8d6539 | 2013-01-08 18:47:52 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Mark Brown | 5bd9f4b | 2013-01-08 13:44:50 +0000 | [diff] [blame] | 133 | /* |
| 134 | * This should never happen; we return above if we fail to |
| 135 | * allocate and we should never be in this code if there are |
| 136 | * no registers at all. |
| 137 | */ |
Russell King | a347146 | 2013-01-26 11:45:35 +0000 | [diff] [blame] | 138 | WARN_ON(list_empty(&map->debugfs_off_cache)); |
| 139 | ret = base; |
Mark Brown | 5bd9f4b | 2013-01-08 13:44:50 +0000 | [diff] [blame] | 140 | |
Dimitris Papastamos | cf57d60 | 2013-02-08 12:47:20 +0000 | [diff] [blame] | 141 | /* Find the relevant block:offset */ |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 142 | list_for_each_entry(c, &map->debugfs_off_cache, list) { |
Mark Brown | 5a1d6d1 | 2013-01-08 20:40:19 +0000 | [diff] [blame] | 143 | if (from >= c->min && from <= c->max) { |
Dimitris Papastamos | cf57d60 | 2013-02-08 12:47:20 +0000 | [diff] [blame] | 144 | fpos_offset = from - c->min; |
| 145 | reg_offset = fpos_offset / map->debugfs_tot_len; |
| 146 | *pos = c->min + (reg_offset * map->debugfs_tot_len); |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 147 | mutex_unlock(&map->cache_lock); |
Dimitris Papastamos | cf57d60 | 2013-02-08 12:47:20 +0000 | [diff] [blame] | 148 | return c->base_reg + reg_offset; |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 149 | } |
| 150 | |
Dimitris Papastamos | cf57d60 | 2013-02-08 12:47:20 +0000 | [diff] [blame] | 151 | *pos = c->max; |
| 152 | ret = c->max_reg; |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 153 | } |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 154 | mutex_unlock(&map->cache_lock); |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 155 | |
| 156 | return ret; |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 157 | } |
| 158 | |
Dimitris Papastamos | 4dd7c55 | 2013-02-11 10:50:59 +0000 | [diff] [blame] | 159 | static inline void regmap_calc_tot_len(struct regmap *map, |
| 160 | void *buf, size_t count) |
| 161 | { |
| 162 | /* Calculate the length of a fixed format */ |
| 163 | if (!map->debugfs_tot_len) { |
| 164 | map->debugfs_reg_len = regmap_calc_reg_len(map->max_register, |
| 165 | buf, count); |
| 166 | map->debugfs_val_len = 2 * map->format.val_bytes; |
| 167 | map->debugfs_tot_len = map->debugfs_reg_len + |
| 168 | map->debugfs_val_len + 3; /* : \n */ |
| 169 | } |
| 170 | } |
| 171 | |
Mark Brown | bd9cc12 | 2012-10-03 12:45:37 +0100 | [diff] [blame] | 172 | static ssize_t regmap_read_debugfs(struct regmap *map, unsigned int from, |
| 173 | unsigned int to, char __user *user_buf, |
| 174 | size_t count, loff_t *ppos) |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 175 | { |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 176 | size_t buf_pos = 0; |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 177 | loff_t p = *ppos; |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 178 | ssize_t ret; |
| 179 | int i; |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 180 | char *buf; |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 181 | unsigned int val, start_reg; |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 182 | |
| 183 | if (*ppos < 0 || !count) |
| 184 | return -EINVAL; |
| 185 | |
| 186 | buf = kmalloc(count, GFP_KERNEL); |
| 187 | if (!buf) |
| 188 | return -ENOMEM; |
| 189 | |
Dimitris Papastamos | 4dd7c55 | 2013-02-11 10:50:59 +0000 | [diff] [blame] | 190 | regmap_calc_tot_len(map, buf, count); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 191 | |
Mark Brown | afab2f7 | 2012-12-09 17:20:10 +0900 | [diff] [blame] | 192 | /* Work out which register we're starting at */ |
| 193 | start_reg = regmap_debugfs_get_dump_start(map, from, *ppos, &p); |
| 194 | |
| 195 | for (i = start_reg; i <= to; i += map->reg_stride) { |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 196 | if (!regmap_readable(map, i)) |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 197 | continue; |
| 198 | |
Mark Brown | 8de2f08 | 2011-08-10 17:14:41 +0900 | [diff] [blame] | 199 | if (regmap_precious(map, i)) |
Mark Brown | 2efe164 | 2011-08-08 15:41:46 +0900 | [diff] [blame] | 200 | continue; |
| 201 | |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 202 | /* If we're in the region the user is trying to read */ |
| 203 | if (p >= *ppos) { |
| 204 | /* ...but not beyond it */ |
Dimitris Papastamos | f3eb839 | 2013-02-07 10:51:56 +0000 | [diff] [blame] | 205 | if (buf_pos + map->debugfs_tot_len > count) |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 206 | break; |
| 207 | |
| 208 | /* Format the register */ |
| 209 | snprintf(buf + buf_pos, count - buf_pos, "%.*x: ", |
Mark Brown | cbc1938 | 2012-12-06 13:29:05 +0900 | [diff] [blame] | 210 | map->debugfs_reg_len, i - from); |
| 211 | buf_pos += map->debugfs_reg_len + 2; |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 212 | |
| 213 | /* Format the value, write all X if we can't read */ |
| 214 | ret = regmap_read(map, i, &val); |
| 215 | if (ret == 0) |
| 216 | snprintf(buf + buf_pos, count - buf_pos, |
Mark Brown | cbc1938 | 2012-12-06 13:29:05 +0900 | [diff] [blame] | 217 | "%.*x", map->debugfs_val_len, val); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 218 | else |
Mark Brown | cbc1938 | 2012-12-06 13:29:05 +0900 | [diff] [blame] | 219 | memset(buf + buf_pos, 'X', |
| 220 | map->debugfs_val_len); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 221 | buf_pos += 2 * map->format.val_bytes; |
| 222 | |
| 223 | buf[buf_pos++] = '\n'; |
| 224 | } |
Mark Brown | cbc1938 | 2012-12-06 13:29:05 +0900 | [diff] [blame] | 225 | p += map->debugfs_tot_len; |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | ret = buf_pos; |
| 229 | |
| 230 | if (copy_to_user(user_buf, buf, buf_pos)) { |
| 231 | ret = -EFAULT; |
| 232 | goto out; |
| 233 | } |
| 234 | |
| 235 | *ppos += buf_pos; |
| 236 | |
| 237 | out: |
| 238 | kfree(buf); |
| 239 | return ret; |
| 240 | } |
| 241 | |
Mark Brown | bd9cc12 | 2012-10-03 12:45:37 +0100 | [diff] [blame] | 242 | static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf, |
| 243 | size_t count, loff_t *ppos) |
| 244 | { |
| 245 | struct regmap *map = file->private_data; |
| 246 | |
| 247 | return regmap_read_debugfs(map, 0, map->max_register, user_buf, |
| 248 | count, ppos); |
| 249 | } |
| 250 | |
Dimitris Papastamos | 09c6ecd | 2012-02-22 12:43:50 +0000 | [diff] [blame] | 251 | #undef REGMAP_ALLOW_WRITE_DEBUGFS |
| 252 | #ifdef REGMAP_ALLOW_WRITE_DEBUGFS |
| 253 | /* |
| 254 | * This can be dangerous especially when we have clients such as |
| 255 | * PMICs, therefore don't provide any real compile time configuration option |
| 256 | * for this feature, people who want to use this will need to modify |
| 257 | * the source code directly. |
| 258 | */ |
| 259 | static ssize_t regmap_map_write_file(struct file *file, |
| 260 | const char __user *user_buf, |
| 261 | size_t count, loff_t *ppos) |
| 262 | { |
| 263 | char buf[32]; |
| 264 | size_t buf_size; |
| 265 | char *start = buf; |
| 266 | unsigned long reg, value; |
| 267 | struct regmap *map = file->private_data; |
Dimitris Papastamos | 68e850d | 2013-05-09 12:46:41 +0100 | [diff] [blame] | 268 | int ret; |
Dimitris Papastamos | 09c6ecd | 2012-02-22 12:43:50 +0000 | [diff] [blame] | 269 | |
| 270 | buf_size = min(count, (sizeof(buf)-1)); |
| 271 | if (copy_from_user(buf, user_buf, buf_size)) |
| 272 | return -EFAULT; |
| 273 | buf[buf_size] = 0; |
| 274 | |
| 275 | while (*start == ' ') |
| 276 | start++; |
| 277 | reg = simple_strtoul(start, &start, 16); |
| 278 | while (*start == ' ') |
| 279 | start++; |
| 280 | if (strict_strtoul(start, 16, &value)) |
| 281 | return -EINVAL; |
| 282 | |
| 283 | /* Userspace has been fiddling around behind the kernel's back */ |
Rusty Russell | 373d4d0 | 2013-01-21 17:17:39 +1030 | [diff] [blame] | 284 | add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE); |
Dimitris Papastamos | 09c6ecd | 2012-02-22 12:43:50 +0000 | [diff] [blame] | 285 | |
Dimitris Papastamos | 68e850d | 2013-05-09 12:46:41 +0100 | [diff] [blame] | 286 | ret = regmap_write(map, reg, value); |
| 287 | if (ret < 0) |
| 288 | return ret; |
Dimitris Papastamos | 09c6ecd | 2012-02-22 12:43:50 +0000 | [diff] [blame] | 289 | return buf_size; |
| 290 | } |
| 291 | #else |
| 292 | #define regmap_map_write_file NULL |
| 293 | #endif |
| 294 | |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 295 | static const struct file_operations regmap_map_fops = { |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 296 | .open = simple_open, |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 297 | .read = regmap_map_read_file, |
Dimitris Papastamos | 09c6ecd | 2012-02-22 12:43:50 +0000 | [diff] [blame] | 298 | .write = regmap_map_write_file, |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 299 | .llseek = default_llseek, |
| 300 | }; |
| 301 | |
Mark Brown | 4b020b3 | 2012-10-03 13:13:16 +0100 | [diff] [blame] | 302 | static ssize_t regmap_range_read_file(struct file *file, char __user *user_buf, |
| 303 | size_t count, loff_t *ppos) |
| 304 | { |
| 305 | struct regmap_range_node *range = file->private_data; |
| 306 | struct regmap *map = range->map; |
| 307 | |
| 308 | return regmap_read_debugfs(map, range->range_min, range->range_max, |
| 309 | user_buf, count, ppos); |
| 310 | } |
| 311 | |
| 312 | static const struct file_operations regmap_range_fops = { |
| 313 | .open = simple_open, |
| 314 | .read = regmap_range_read_file, |
| 315 | .llseek = default_llseek, |
| 316 | }; |
| 317 | |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 318 | static ssize_t regmap_reg_ranges_read_file(struct file *file, |
| 319 | char __user *user_buf, size_t count, |
| 320 | loff_t *ppos) |
| 321 | { |
| 322 | struct regmap *map = file->private_data; |
| 323 | struct regmap_debugfs_off_cache *c; |
| 324 | loff_t p = 0; |
| 325 | size_t buf_pos = 0; |
| 326 | char *buf; |
| 327 | char *entry; |
| 328 | int ret; |
| 329 | |
| 330 | if (*ppos < 0 || !count) |
| 331 | return -EINVAL; |
| 332 | |
| 333 | buf = kmalloc(count, GFP_KERNEL); |
| 334 | if (!buf) |
| 335 | return -ENOMEM; |
| 336 | |
| 337 | entry = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 338 | if (!entry) { |
| 339 | kfree(buf); |
| 340 | return -ENOMEM; |
| 341 | } |
| 342 | |
| 343 | /* While we are at it, build the register dump cache |
| 344 | * now so the read() operation on the `registers' file |
| 345 | * can benefit from using the cache. We do not care |
| 346 | * about the file position information that is contained |
| 347 | * in the cache, just about the actual register blocks */ |
| 348 | regmap_calc_tot_len(map, buf, count); |
| 349 | regmap_debugfs_get_dump_start(map, 0, *ppos, &p); |
| 350 | |
| 351 | /* Reset file pointer as the fixed-format of the `registers' |
| 352 | * file is not compatible with the `range' file */ |
| 353 | p = 0; |
| 354 | mutex_lock(&map->cache_lock); |
| 355 | list_for_each_entry(c, &map->debugfs_off_cache, list) { |
| 356 | snprintf(entry, PAGE_SIZE, "%x-%x", |
| 357 | c->base_reg, c->max_reg); |
| 358 | if (p >= *ppos) { |
| 359 | if (buf_pos + 1 + strlen(entry) > count) |
| 360 | break; |
| 361 | snprintf(buf + buf_pos, count - buf_pos, |
| 362 | "%s", entry); |
| 363 | buf_pos += strlen(entry); |
| 364 | buf[buf_pos] = '\n'; |
| 365 | buf_pos++; |
| 366 | } |
| 367 | p += strlen(entry) + 1; |
| 368 | } |
| 369 | mutex_unlock(&map->cache_lock); |
| 370 | |
| 371 | kfree(entry); |
| 372 | ret = buf_pos; |
| 373 | |
| 374 | if (copy_to_user(user_buf, buf, buf_pos)) { |
| 375 | ret = -EFAULT; |
| 376 | goto out_buf; |
| 377 | } |
| 378 | |
| 379 | *ppos += buf_pos; |
| 380 | out_buf: |
| 381 | kfree(buf); |
| 382 | return ret; |
| 383 | } |
| 384 | |
| 385 | static const struct file_operations regmap_reg_ranges_fops = { |
| 386 | .open = simple_open, |
| 387 | .read = regmap_reg_ranges_read_file, |
| 388 | .llseek = default_llseek, |
| 389 | }; |
| 390 | |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 391 | static ssize_t regmap_access_read_file(struct file *file, |
| 392 | char __user *user_buf, size_t count, |
| 393 | loff_t *ppos) |
| 394 | { |
| 395 | int reg_len, tot_len; |
| 396 | size_t buf_pos = 0; |
| 397 | loff_t p = 0; |
| 398 | ssize_t ret; |
| 399 | int i; |
| 400 | struct regmap *map = file->private_data; |
| 401 | char *buf; |
| 402 | |
| 403 | if (*ppos < 0 || !count) |
| 404 | return -EINVAL; |
| 405 | |
| 406 | buf = kmalloc(count, GFP_KERNEL); |
| 407 | if (!buf) |
| 408 | return -ENOMEM; |
| 409 | |
| 410 | /* Calculate the length of a fixed format */ |
| 411 | reg_len = regmap_calc_reg_len(map->max_register, buf, count); |
| 412 | tot_len = reg_len + 10; /* ': R W V P\n' */ |
| 413 | |
Stephen Warren | f01ee60 | 2012-04-09 13:40:24 -0600 | [diff] [blame] | 414 | for (i = 0; i <= map->max_register; i += map->reg_stride) { |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 415 | /* Ignore registers which are neither readable nor writable */ |
| 416 | if (!regmap_readable(map, i) && !regmap_writeable(map, i)) |
| 417 | continue; |
| 418 | |
| 419 | /* If we're in the region the user is trying to read */ |
| 420 | if (p >= *ppos) { |
| 421 | /* ...but not beyond it */ |
| 422 | if (buf_pos >= count - 1 - tot_len) |
| 423 | break; |
| 424 | |
| 425 | /* Format the register */ |
| 426 | snprintf(buf + buf_pos, count - buf_pos, |
| 427 | "%.*x: %c %c %c %c\n", |
| 428 | reg_len, i, |
| 429 | regmap_readable(map, i) ? 'y' : 'n', |
| 430 | regmap_writeable(map, i) ? 'y' : 'n', |
| 431 | regmap_volatile(map, i) ? 'y' : 'n', |
| 432 | regmap_precious(map, i) ? 'y' : 'n'); |
| 433 | |
| 434 | buf_pos += tot_len; |
| 435 | } |
| 436 | p += tot_len; |
| 437 | } |
| 438 | |
| 439 | ret = buf_pos; |
| 440 | |
| 441 | if (copy_to_user(user_buf, buf, buf_pos)) { |
| 442 | ret = -EFAULT; |
| 443 | goto out; |
| 444 | } |
| 445 | |
| 446 | *ppos += buf_pos; |
| 447 | |
| 448 | out: |
| 449 | kfree(buf); |
| 450 | return ret; |
| 451 | } |
| 452 | |
| 453 | static const struct file_operations regmap_access_fops = { |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 454 | .open = simple_open, |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 455 | .read = regmap_access_read_file, |
| 456 | .llseek = default_llseek, |
| 457 | }; |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 458 | |
Stephen Warren | d3c242e | 2012-04-04 15:48:29 -0600 | [diff] [blame] | 459 | void regmap_debugfs_init(struct regmap *map, const char *name) |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 460 | { |
Mark Brown | 4b020b3 | 2012-10-03 13:13:16 +0100 | [diff] [blame] | 461 | struct rb_node *next; |
| 462 | struct regmap_range_node *range_node; |
| 463 | |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 464 | INIT_LIST_HEAD(&map->debugfs_off_cache); |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 465 | mutex_init(&map->cache_lock); |
Mark Brown | 5166b7c | 2012-12-11 01:24:29 +0900 | [diff] [blame] | 466 | |
Stephen Warren | d3c242e | 2012-04-04 15:48:29 -0600 | [diff] [blame] | 467 | if (name) { |
| 468 | map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s", |
| 469 | dev_name(map->dev), name); |
| 470 | name = map->debugfs_name; |
| 471 | } else { |
| 472 | name = dev_name(map->dev); |
| 473 | } |
| 474 | |
| 475 | map->debugfs = debugfs_create_dir(name, regmap_debugfs_root); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 476 | if (!map->debugfs) { |
| 477 | dev_warn(map->dev, "Failed to create debugfs directory\n"); |
| 478 | return; |
| 479 | } |
| 480 | |
Dimitris Papastamos | f0c2319 | 2012-02-22 14:20:09 +0000 | [diff] [blame] | 481 | debugfs_create_file("name", 0400, map->debugfs, |
| 482 | map, ®map_name_fops); |
| 483 | |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 484 | debugfs_create_file("range", 0400, map->debugfs, |
| 485 | map, ®map_reg_ranges_fops); |
| 486 | |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 487 | if (map->max_register) { |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 488 | debugfs_create_file("registers", 0400, map->debugfs, |
| 489 | map, ®map_map_fops); |
Mark Brown | 449e384 | 2011-08-10 17:28:04 +0900 | [diff] [blame] | 490 | debugfs_create_file("access", 0400, map->debugfs, |
| 491 | map, ®map_access_fops); |
| 492 | } |
Mark Brown | 028a01e | 2012-02-06 18:02:06 +0000 | [diff] [blame] | 493 | |
| 494 | if (map->cache_type) { |
| 495 | debugfs_create_bool("cache_only", 0400, map->debugfs, |
| 496 | &map->cache_only); |
| 497 | debugfs_create_bool("cache_dirty", 0400, map->debugfs, |
| 498 | &map->cache_dirty); |
| 499 | debugfs_create_bool("cache_bypass", 0400, map->debugfs, |
| 500 | &map->cache_bypass); |
| 501 | } |
Mark Brown | 4b020b3 | 2012-10-03 13:13:16 +0100 | [diff] [blame] | 502 | |
| 503 | next = rb_first(&map->range_tree); |
| 504 | while (next) { |
| 505 | range_node = rb_entry(next, struct regmap_range_node, node); |
| 506 | |
| 507 | if (range_node->name) |
| 508 | debugfs_create_file(range_node->name, 0400, |
| 509 | map->debugfs, range_node, |
| 510 | ®map_range_fops); |
| 511 | |
| 512 | next = rb_next(&range_node->node); |
| 513 | } |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | void regmap_debugfs_exit(struct regmap *map) |
| 517 | { |
| 518 | debugfs_remove_recursive(map->debugfs); |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 519 | mutex_lock(&map->cache_lock); |
Mark Brown | 95f971c | 2013-01-08 13:35:58 +0000 | [diff] [blame] | 520 | regmap_debugfs_free_dump_cache(map); |
Dimitris Papastamos | 065b4c5 | 2013-02-20 12:15:23 +0000 | [diff] [blame] | 521 | mutex_unlock(&map->cache_lock); |
Stephen Warren | d3c242e | 2012-04-04 15:48:29 -0600 | [diff] [blame] | 522 | kfree(map->debugfs_name); |
Mark Brown | 31244e3 | 2011-07-20 22:56:53 +0100 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | void regmap_debugfs_initcall(void) |
| 526 | { |
| 527 | regmap_debugfs_root = debugfs_create_dir("regmap", NULL); |
| 528 | if (!regmap_debugfs_root) { |
| 529 | pr_warn("regmap: Failed to create debugfs root\n"); |
| 530 | return; |
| 531 | } |
| 532 | } |