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