regmap-debugfs.c 7.9 KB

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