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> 14 #include <linux/mutex.h> 15 #include <linux/debugfs.h> 16 #include <linux/uaccess.h> 17 #include <linux/device.h> 18 19 #include "internal.h" 20 21 static struct dentry *regmap_debugfs_root; 22 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 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 = { 54 .open = simple_open, 55 .read = regmap_name_read_file, 56 .llseek = default_llseek, 57 }; 58 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 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 { 81 struct regmap_debugfs_off_cache *c = NULL; 82 loff_t p = 0; 83 unsigned int i, ret; 84 unsigned int fpos_offset; 85 unsigned int reg_offset; 86 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 */ 91 if (list_empty(&map->debugfs_off_cache)) { 92 for (i = base; i <= map->max_register; i += map->reg_stride) { 93 /* Skip unprinted registers, closing off cache entry */ 94 if (!regmap_readable(map, i) || 95 regmap_precious(map, i)) { 96 if (c) { 97 c->max = p - 1; 98 fpos_offset = c->max - c->min; 99 reg_offset = fpos_offset / map->debugfs_tot_len; 100 c->max_reg = c->base_reg + reg_offset; 101 list_add_tail(&c->list, 102 &map->debugfs_off_cache); 103 c = NULL; 104 } 105 106 continue; 107 } 108 109 /* No cache entry? Start a new one */ 110 if (!c) { 111 c = kzalloc(sizeof(*c), GFP_KERNEL); 112 if (!c) { 113 regmap_debugfs_free_dump_cache(map); 114 return base; 115 } 116 c->min = p; 117 c->base_reg = i; 118 } 119 120 p += map->debugfs_tot_len; 121 } 122 } 123 124 /* Close the last entry off if we didn't scan beyond it */ 125 if (c) { 126 c->max = p - 1; 127 fpos_offset = c->max - c->min; 128 reg_offset = fpos_offset / map->debugfs_tot_len; 129 c->max_reg = c->base_reg + reg_offset; 130 list_add_tail(&c->list, 131 &map->debugfs_off_cache); 132 } 133 134 /* 135 * This should never happen; we return above if we fail to 136 * allocate and we should never be in this code if there are 137 * no registers at all. 138 */ 139 WARN_ON(list_empty(&map->debugfs_off_cache)); 140 ret = base; 141 142 /* Find the relevant block:offset */ 143 list_for_each_entry(c, &map->debugfs_off_cache, list) { 144 if (from >= c->min && from <= c->max) { 145 fpos_offset = from - c->min; 146 reg_offset = fpos_offset / map->debugfs_tot_len; 147 *pos = c->min + (reg_offset * map->debugfs_tot_len); 148 return c->base_reg + reg_offset; 149 } 150 151 *pos = c->max; 152 ret = c->max_reg; 153 } 154 155 return ret; 156 } 157 158 static inline void regmap_calc_tot_len(struct regmap *map, 159 void *buf, size_t count) 160 { 161 /* Calculate the length of a fixed format */ 162 if (!map->debugfs_tot_len) { 163 map->debugfs_reg_len = regmap_calc_reg_len(map->max_register, 164 buf, count); 165 map->debugfs_val_len = 2 * map->format.val_bytes; 166 map->debugfs_tot_len = map->debugfs_reg_len + 167 map->debugfs_val_len + 3; /* : \n */ 168 } 169 } 170 171 static ssize_t regmap_read_debugfs(struct regmap *map, unsigned int from, 172 unsigned int to, char __user *user_buf, 173 size_t count, loff_t *ppos) 174 { 175 size_t buf_pos = 0; 176 loff_t p = *ppos; 177 ssize_t ret; 178 int i; 179 char *buf; 180 unsigned int val, start_reg; 181 182 if (*ppos < 0 || !count) 183 return -EINVAL; 184 185 buf = kmalloc(count, GFP_KERNEL); 186 if (!buf) 187 return -ENOMEM; 188 189 regmap_calc_tot_len(map, buf, count); 190 191 /* Work out which register we're starting at */ 192 start_reg = regmap_debugfs_get_dump_start(map, from, *ppos, &p); 193 194 for (i = start_reg; i <= to; i += map->reg_stride) { 195 if (!regmap_readable(map, i)) 196 continue; 197 198 if (regmap_precious(map, i)) 199 continue; 200 201 /* If we're in the region the user is trying to read */ 202 if (p >= *ppos) { 203 /* ...but not beyond it */ 204 if (buf_pos + map->debugfs_tot_len > count) 205 break; 206 207 /* Format the register */ 208 snprintf(buf + buf_pos, count - buf_pos, "%.*x: ", 209 map->debugfs_reg_len, i - from); 210 buf_pos += map->debugfs_reg_len + 2; 211 212 /* Format the value, write all X if we can't read */ 213 ret = regmap_read(map, i, &val); 214 if (ret == 0) 215 snprintf(buf + buf_pos, count - buf_pos, 216 "%.*x", map->debugfs_val_len, val); 217 else 218 memset(buf + buf_pos, 'X', 219 map->debugfs_val_len); 220 buf_pos += 2 * map->format.val_bytes; 221 222 buf[buf_pos++] = '\n'; 223 } 224 p += map->debugfs_tot_len; 225 } 226 227 ret = buf_pos; 228 229 if (copy_to_user(user_buf, buf, buf_pos)) { 230 ret = -EFAULT; 231 goto out; 232 } 233 234 *ppos += buf_pos; 235 236 out: 237 kfree(buf); 238 return ret; 239 } 240 241 static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf, 242 size_t count, loff_t *ppos) 243 { 244 struct regmap *map = file->private_data; 245 246 return regmap_read_debugfs(map, 0, map->max_register, user_buf, 247 count, ppos); 248 } 249 250 #undef REGMAP_ALLOW_WRITE_DEBUGFS 251 #ifdef REGMAP_ALLOW_WRITE_DEBUGFS 252 /* 253 * This can be dangerous especially when we have clients such as 254 * PMICs, therefore don't provide any real compile time configuration option 255 * for this feature, people who want to use this will need to modify 256 * the source code directly. 257 */ 258 static ssize_t regmap_map_write_file(struct file *file, 259 const char __user *user_buf, 260 size_t count, loff_t *ppos) 261 { 262 char buf[32]; 263 size_t buf_size; 264 char *start = buf; 265 unsigned long reg, value; 266 struct regmap *map = file->private_data; 267 268 buf_size = min(count, (sizeof(buf)-1)); 269 if (copy_from_user(buf, user_buf, buf_size)) 270 return -EFAULT; 271 buf[buf_size] = 0; 272 273 while (*start == ' ') 274 start++; 275 reg = simple_strtoul(start, &start, 16); 276 while (*start == ' ') 277 start++; 278 if (strict_strtoul(start, 16, &value)) 279 return -EINVAL; 280 281 /* Userspace has been fiddling around behind the kernel's back */ 282 add_taint(TAINT_USER, LOCKDEP_NOW_UNRELIABLE); 283 284 regmap_write(map, reg, value); 285 return buf_size; 286 } 287 #else 288 #define regmap_map_write_file NULL 289 #endif 290 291 static const struct file_operations regmap_map_fops = { 292 .open = simple_open, 293 .read = regmap_map_read_file, 294 .write = regmap_map_write_file, 295 .llseek = default_llseek, 296 }; 297 298 static ssize_t regmap_range_read_file(struct file *file, char __user *user_buf, 299 size_t count, loff_t *ppos) 300 { 301 struct regmap_range_node *range = file->private_data; 302 struct regmap *map = range->map; 303 304 return regmap_read_debugfs(map, range->range_min, range->range_max, 305 user_buf, count, ppos); 306 } 307 308 static const struct file_operations regmap_range_fops = { 309 .open = simple_open, 310 .read = regmap_range_read_file, 311 .llseek = default_llseek, 312 }; 313 314 static ssize_t regmap_access_read_file(struct file *file, 315 char __user *user_buf, size_t count, 316 loff_t *ppos) 317 { 318 int reg_len, tot_len; 319 size_t buf_pos = 0; 320 loff_t p = 0; 321 ssize_t ret; 322 int i; 323 struct regmap *map = file->private_data; 324 char *buf; 325 326 if (*ppos < 0 || !count) 327 return -EINVAL; 328 329 buf = kmalloc(count, GFP_KERNEL); 330 if (!buf) 331 return -ENOMEM; 332 333 /* Calculate the length of a fixed format */ 334 reg_len = regmap_calc_reg_len(map->max_register, buf, count); 335 tot_len = reg_len + 10; /* ': R W V P\n' */ 336 337 for (i = 0; i <= map->max_register; i += map->reg_stride) { 338 /* Ignore registers which are neither readable nor writable */ 339 if (!regmap_readable(map, i) && !regmap_writeable(map, i)) 340 continue; 341 342 /* If we're in the region the user is trying to read */ 343 if (p >= *ppos) { 344 /* ...but not beyond it */ 345 if (buf_pos >= count - 1 - tot_len) 346 break; 347 348 /* Format the register */ 349 snprintf(buf + buf_pos, count - buf_pos, 350 "%.*x: %c %c %c %c\n", 351 reg_len, i, 352 regmap_readable(map, i) ? 'y' : 'n', 353 regmap_writeable(map, i) ? 'y' : 'n', 354 regmap_volatile(map, i) ? 'y' : 'n', 355 regmap_precious(map, i) ? 'y' : 'n'); 356 357 buf_pos += tot_len; 358 } 359 p += tot_len; 360 } 361 362 ret = buf_pos; 363 364 if (copy_to_user(user_buf, buf, buf_pos)) { 365 ret = -EFAULT; 366 goto out; 367 } 368 369 *ppos += buf_pos; 370 371 out: 372 kfree(buf); 373 return ret; 374 } 375 376 static const struct file_operations regmap_access_fops = { 377 .open = simple_open, 378 .read = regmap_access_read_file, 379 .llseek = default_llseek, 380 }; 381 382 void regmap_debugfs_init(struct regmap *map, const char *name) 383 { 384 struct rb_node *next; 385 struct regmap_range_node *range_node; 386 387 INIT_LIST_HEAD(&map->debugfs_off_cache); 388 389 if (name) { 390 map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s", 391 dev_name(map->dev), name); 392 name = map->debugfs_name; 393 } else { 394 name = dev_name(map->dev); 395 } 396 397 map->debugfs = debugfs_create_dir(name, regmap_debugfs_root); 398 if (!map->debugfs) { 399 dev_warn(map->dev, "Failed to create debugfs directory\n"); 400 return; 401 } 402 403 debugfs_create_file("name", 0400, map->debugfs, 404 map, ®map_name_fops); 405 406 if (map->max_register) { 407 debugfs_create_file("registers", 0400, map->debugfs, 408 map, ®map_map_fops); 409 debugfs_create_file("access", 0400, map->debugfs, 410 map, ®map_access_fops); 411 } 412 413 if (map->cache_type) { 414 debugfs_create_bool("cache_only", 0400, map->debugfs, 415 &map->cache_only); 416 debugfs_create_bool("cache_dirty", 0400, map->debugfs, 417 &map->cache_dirty); 418 debugfs_create_bool("cache_bypass", 0400, map->debugfs, 419 &map->cache_bypass); 420 } 421 422 next = rb_first(&map->range_tree); 423 while (next) { 424 range_node = rb_entry(next, struct regmap_range_node, node); 425 426 if (range_node->name) 427 debugfs_create_file(range_node->name, 0400, 428 map->debugfs, range_node, 429 ®map_range_fops); 430 431 next = rb_next(&range_node->node); 432 } 433 } 434 435 void regmap_debugfs_exit(struct regmap *map) 436 { 437 debugfs_remove_recursive(map->debugfs); 438 regmap_debugfs_free_dump_cache(map); 439 kfree(map->debugfs_name); 440 } 441 442 void regmap_debugfs_initcall(void) 443 { 444 regmap_debugfs_root = debugfs_create_dir("regmap", NULL); 445 if (!regmap_debugfs_root) { 446 pr_warn("regmap: Failed to create debugfs root\n"); 447 return; 448 } 449 } 450