regmap-debugfs.c 7.8 KB

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