regmap-debugfs.c 9.5 KB

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