regmap-debugfs.c 6.6 KB

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