regmap-debugfs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 "internal.h"
  18. static struct dentry *regmap_debugfs_root;
  19. /* Calculate the length of a fixed format */
  20. static size_t regmap_calc_reg_len(int max_val, char *buf, size_t buf_size)
  21. {
  22. snprintf(buf, buf_size, "%x", max_val);
  23. return strlen(buf);
  24. }
  25. static ssize_t regmap_name_read_file(struct file *file,
  26. char __user *user_buf, size_t count,
  27. loff_t *ppos)
  28. {
  29. struct regmap *map = file->private_data;
  30. int ret;
  31. char *buf;
  32. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  33. if (!buf)
  34. return -ENOMEM;
  35. ret = snprintf(buf, PAGE_SIZE, "%s\n", map->dev->driver->name);
  36. if (ret < 0) {
  37. kfree(buf);
  38. return ret;
  39. }
  40. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  41. kfree(buf);
  42. return ret;
  43. }
  44. static const struct file_operations regmap_name_fops = {
  45. .open = simple_open,
  46. .read = regmap_name_read_file,
  47. .llseek = default_llseek,
  48. };
  49. static void regmap_debugfs_free_dump_cache(struct regmap *map)
  50. {
  51. struct regmap_debugfs_off_cache *c;
  52. while (!list_empty(&map->debugfs_off_cache)) {
  53. c = list_first_entry(&map->debugfs_off_cache,
  54. struct regmap_debugfs_off_cache,
  55. list);
  56. list_del(&c->list);
  57. kfree(c);
  58. }
  59. }
  60. /*
  61. * Work out where the start offset maps into register numbers, bearing
  62. * in mind that we suppress hidden registers.
  63. */
  64. static unsigned int regmap_debugfs_get_dump_start(struct regmap *map,
  65. unsigned int base,
  66. loff_t from,
  67. loff_t *pos)
  68. {
  69. struct regmap_debugfs_off_cache *c = NULL;
  70. loff_t p = 0;
  71. unsigned int i, ret;
  72. /*
  73. * If we don't have a cache build one so we don't have to do a
  74. * linear scan each time.
  75. */
  76. if (list_empty(&map->debugfs_off_cache)) {
  77. for (i = base; i <= map->max_register; i += map->reg_stride) {
  78. /* Skip unprinted registers, closing off cache entry */
  79. if (!regmap_readable(map, i) ||
  80. regmap_precious(map, i)) {
  81. if (c) {
  82. c->max = p - 1;
  83. list_add_tail(&c->list,
  84. &map->debugfs_off_cache);
  85. c = NULL;
  86. }
  87. continue;
  88. }
  89. /* No cache entry? Start a new one */
  90. if (!c) {
  91. c = kzalloc(sizeof(*c), GFP_KERNEL);
  92. if (!c) {
  93. regmap_debugfs_free_dump_cache(map);
  94. return base;
  95. }
  96. c->min = p;
  97. c->base_reg = i;
  98. }
  99. p += map->debugfs_tot_len;
  100. }
  101. }
  102. /* Close the last entry off if we didn't scan beyond it */
  103. if (c) {
  104. c->max = p - 1;
  105. list_add_tail(&c->list,
  106. &map->debugfs_off_cache);
  107. }
  108. /*
  109. * This should never happen; we return above if we fail to
  110. * allocate and we should never be in this code if there are
  111. * no registers at all.
  112. */
  113. if (list_empty(&map->debugfs_off_cache)) {
  114. WARN_ON(list_empty(&map->debugfs_off_cache));
  115. return base;
  116. }
  117. /* Find the relevant block */
  118. list_for_each_entry(c, &map->debugfs_off_cache, list) {
  119. if (from >= c->min && from <= c->max) {
  120. *pos = c->min;
  121. return c->base_reg;
  122. }
  123. *pos = c->min;
  124. ret = c->base_reg;
  125. }
  126. return ret;
  127. }
  128. static ssize_t regmap_read_debugfs(struct regmap *map, unsigned int from,
  129. unsigned int to, char __user *user_buf,
  130. size_t count, loff_t *ppos)
  131. {
  132. size_t buf_pos = 0;
  133. loff_t p = *ppos;
  134. ssize_t ret;
  135. int i;
  136. char *buf;
  137. unsigned int val, start_reg;
  138. if (*ppos < 0 || !count)
  139. return -EINVAL;
  140. buf = kmalloc(count, GFP_KERNEL);
  141. if (!buf)
  142. return -ENOMEM;
  143. /* Calculate the length of a fixed format */
  144. if (!map->debugfs_tot_len) {
  145. map->debugfs_reg_len = regmap_calc_reg_len(map->max_register,
  146. buf, count);
  147. map->debugfs_val_len = 2 * map->format.val_bytes;
  148. map->debugfs_tot_len = map->debugfs_reg_len +
  149. map->debugfs_val_len + 3; /* : \n */
  150. }
  151. /* Work out which register we're starting at */
  152. start_reg = regmap_debugfs_get_dump_start(map, from, *ppos, &p);
  153. for (i = start_reg; i <= to; i += map->reg_stride) {
  154. if (!regmap_readable(map, i))
  155. continue;
  156. if (regmap_precious(map, i))
  157. continue;
  158. /* If we're in the region the user is trying to read */
  159. if (p >= *ppos) {
  160. /* ...but not beyond it */
  161. if (buf_pos + 1 + map->debugfs_tot_len >= count)
  162. break;
  163. /* Format the register */
  164. snprintf(buf + buf_pos, count - buf_pos, "%.*x: ",
  165. map->debugfs_reg_len, i - from);
  166. buf_pos += map->debugfs_reg_len + 2;
  167. /* Format the value, write all X if we can't read */
  168. ret = regmap_read(map, i, &val);
  169. if (ret == 0)
  170. snprintf(buf + buf_pos, count - buf_pos,
  171. "%.*x", map->debugfs_val_len, val);
  172. else
  173. memset(buf + buf_pos, 'X',
  174. map->debugfs_val_len);
  175. buf_pos += 2 * map->format.val_bytes;
  176. buf[buf_pos++] = '\n';
  177. }
  178. p += map->debugfs_tot_len;
  179. }
  180. ret = buf_pos;
  181. if (copy_to_user(user_buf, buf, buf_pos)) {
  182. ret = -EFAULT;
  183. goto out;
  184. }
  185. *ppos += buf_pos;
  186. out:
  187. kfree(buf);
  188. return ret;
  189. }
  190. static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf,
  191. size_t count, loff_t *ppos)
  192. {
  193. struct regmap *map = file->private_data;
  194. return regmap_read_debugfs(map, 0, map->max_register, user_buf,
  195. count, ppos);
  196. }
  197. #undef REGMAP_ALLOW_WRITE_DEBUGFS
  198. #ifdef REGMAP_ALLOW_WRITE_DEBUGFS
  199. /*
  200. * This can be dangerous especially when we have clients such as
  201. * PMICs, therefore don't provide any real compile time configuration option
  202. * for this feature, people who want to use this will need to modify
  203. * the source code directly.
  204. */
  205. static ssize_t regmap_map_write_file(struct file *file,
  206. const char __user *user_buf,
  207. size_t count, loff_t *ppos)
  208. {
  209. char buf[32];
  210. size_t buf_size;
  211. char *start = buf;
  212. unsigned long reg, value;
  213. struct regmap *map = file->private_data;
  214. buf_size = min(count, (sizeof(buf)-1));
  215. if (copy_from_user(buf, user_buf, buf_size))
  216. return -EFAULT;
  217. buf[buf_size] = 0;
  218. while (*start == ' ')
  219. start++;
  220. reg = simple_strtoul(start, &start, 16);
  221. while (*start == ' ')
  222. start++;
  223. if (strict_strtoul(start, 16, &value))
  224. return -EINVAL;
  225. /* Userspace has been fiddling around behind the kernel's back */
  226. add_taint(TAINT_USER);
  227. regmap_write(map, reg, value);
  228. return buf_size;
  229. }
  230. #else
  231. #define regmap_map_write_file NULL
  232. #endif
  233. static const struct file_operations regmap_map_fops = {
  234. .open = simple_open,
  235. .read = regmap_map_read_file,
  236. .write = regmap_map_write_file,
  237. .llseek = default_llseek,
  238. };
  239. static ssize_t regmap_range_read_file(struct file *file, char __user *user_buf,
  240. size_t count, loff_t *ppos)
  241. {
  242. struct regmap_range_node *range = file->private_data;
  243. struct regmap *map = range->map;
  244. return regmap_read_debugfs(map, range->range_min, range->range_max,
  245. user_buf, count, ppos);
  246. }
  247. static const struct file_operations regmap_range_fops = {
  248. .open = simple_open,
  249. .read = regmap_range_read_file,
  250. .llseek = default_llseek,
  251. };
  252. static ssize_t regmap_access_read_file(struct file *file,
  253. char __user *user_buf, size_t count,
  254. loff_t *ppos)
  255. {
  256. int reg_len, tot_len;
  257. size_t buf_pos = 0;
  258. loff_t p = 0;
  259. ssize_t ret;
  260. int i;
  261. struct regmap *map = file->private_data;
  262. char *buf;
  263. if (*ppos < 0 || !count)
  264. return -EINVAL;
  265. buf = kmalloc(count, GFP_KERNEL);
  266. if (!buf)
  267. return -ENOMEM;
  268. /* Calculate the length of a fixed format */
  269. reg_len = regmap_calc_reg_len(map->max_register, buf, count);
  270. tot_len = reg_len + 10; /* ': R W V P\n' */
  271. for (i = 0; i <= map->max_register; i += map->reg_stride) {
  272. /* Ignore registers which are neither readable nor writable */
  273. if (!regmap_readable(map, i) && !regmap_writeable(map, i))
  274. continue;
  275. /* If we're in the region the user is trying to read */
  276. if (p >= *ppos) {
  277. /* ...but not beyond it */
  278. if (buf_pos >= count - 1 - tot_len)
  279. break;
  280. /* Format the register */
  281. snprintf(buf + buf_pos, count - buf_pos,
  282. "%.*x: %c %c %c %c\n",
  283. reg_len, i,
  284. regmap_readable(map, i) ? 'y' : 'n',
  285. regmap_writeable(map, i) ? 'y' : 'n',
  286. regmap_volatile(map, i) ? 'y' : 'n',
  287. regmap_precious(map, i) ? 'y' : 'n');
  288. buf_pos += tot_len;
  289. }
  290. p += tot_len;
  291. }
  292. ret = buf_pos;
  293. if (copy_to_user(user_buf, buf, buf_pos)) {
  294. ret = -EFAULT;
  295. goto out;
  296. }
  297. *ppos += buf_pos;
  298. out:
  299. kfree(buf);
  300. return ret;
  301. }
  302. static const struct file_operations regmap_access_fops = {
  303. .open = simple_open,
  304. .read = regmap_access_read_file,
  305. .llseek = default_llseek,
  306. };
  307. void regmap_debugfs_init(struct regmap *map, const char *name)
  308. {
  309. struct rb_node *next;
  310. struct regmap_range_node *range_node;
  311. INIT_LIST_HEAD(&map->debugfs_off_cache);
  312. if (name) {
  313. map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s",
  314. dev_name(map->dev), name);
  315. name = map->debugfs_name;
  316. } else {
  317. name = dev_name(map->dev);
  318. }
  319. map->debugfs = debugfs_create_dir(name, regmap_debugfs_root);
  320. if (!map->debugfs) {
  321. dev_warn(map->dev, "Failed to create debugfs directory\n");
  322. return;
  323. }
  324. debugfs_create_file("name", 0400, map->debugfs,
  325. map, &regmap_name_fops);
  326. if (map->max_register) {
  327. debugfs_create_file("registers", 0400, map->debugfs,
  328. map, &regmap_map_fops);
  329. debugfs_create_file("access", 0400, map->debugfs,
  330. map, &regmap_access_fops);
  331. }
  332. if (map->cache_type) {
  333. debugfs_create_bool("cache_only", 0400, map->debugfs,
  334. &map->cache_only);
  335. debugfs_create_bool("cache_dirty", 0400, map->debugfs,
  336. &map->cache_dirty);
  337. debugfs_create_bool("cache_bypass", 0400, map->debugfs,
  338. &map->cache_bypass);
  339. }
  340. next = rb_first(&map->range_tree);
  341. while (next) {
  342. range_node = rb_entry(next, struct regmap_range_node, node);
  343. if (range_node->name)
  344. debugfs_create_file(range_node->name, 0400,
  345. map->debugfs, range_node,
  346. &regmap_range_fops);
  347. next = rb_next(&range_node->node);
  348. }
  349. }
  350. void regmap_debugfs_exit(struct regmap *map)
  351. {
  352. debugfs_remove_recursive(map->debugfs);
  353. regmap_debugfs_free_dump_cache(map);
  354. kfree(map->debugfs_name);
  355. }
  356. void regmap_debugfs_initcall(void)
  357. {
  358. regmap_debugfs_root = debugfs_create_dir("regmap", NULL);
  359. if (!regmap_debugfs_root) {
  360. pr_warn("regmap: Failed to create debugfs root\n");
  361. return;
  362. }
  363. }