regmap-debugfs.c 14 KB

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