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