regmap-debugfs.c 6.6 KB

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