regmap-debugfs.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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_map_read_file(struct file *file, char __user *user_buf,
  50. size_t count, loff_t *ppos)
  51. {
  52. int reg_len, val_len, tot_len;
  53. size_t buf_pos = 0;
  54. loff_t p = 0;
  55. ssize_t ret;
  56. int i;
  57. struct regmap *map = file->private_data;
  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 = 0; i <= map->max_register; 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);
  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. #undef REGMAP_ALLOW_WRITE_DEBUGFS
  106. #ifdef REGMAP_ALLOW_WRITE_DEBUGFS
  107. /*
  108. * This can be dangerous especially when we have clients such as
  109. * PMICs, therefore don't provide any real compile time configuration option
  110. * for this feature, people who want to use this will need to modify
  111. * the source code directly.
  112. */
  113. static ssize_t regmap_map_write_file(struct file *file,
  114. const char __user *user_buf,
  115. size_t count, loff_t *ppos)
  116. {
  117. char buf[32];
  118. size_t buf_size;
  119. char *start = buf;
  120. unsigned long reg, value;
  121. struct regmap *map = file->private_data;
  122. buf_size = min(count, (sizeof(buf)-1));
  123. if (copy_from_user(buf, user_buf, buf_size))
  124. return -EFAULT;
  125. buf[buf_size] = 0;
  126. while (*start == ' ')
  127. start++;
  128. reg = simple_strtoul(start, &start, 16);
  129. while (*start == ' ')
  130. start++;
  131. if (strict_strtoul(start, 16, &value))
  132. return -EINVAL;
  133. /* Userspace has been fiddling around behind the kernel's back */
  134. add_taint(TAINT_USER);
  135. regmap_write(map, reg, value);
  136. return buf_size;
  137. }
  138. #else
  139. #define regmap_map_write_file NULL
  140. #endif
  141. static const struct file_operations regmap_map_fops = {
  142. .open = simple_open,
  143. .read = regmap_map_read_file,
  144. .write = regmap_map_write_file,
  145. .llseek = default_llseek,
  146. };
  147. static ssize_t regmap_access_read_file(struct file *file,
  148. char __user *user_buf, size_t count,
  149. loff_t *ppos)
  150. {
  151. int reg_len, tot_len;
  152. size_t buf_pos = 0;
  153. loff_t p = 0;
  154. ssize_t ret;
  155. int i;
  156. struct regmap *map = file->private_data;
  157. char *buf;
  158. if (*ppos < 0 || !count)
  159. return -EINVAL;
  160. buf = kmalloc(count, GFP_KERNEL);
  161. if (!buf)
  162. return -ENOMEM;
  163. /* Calculate the length of a fixed format */
  164. reg_len = regmap_calc_reg_len(map->max_register, buf, count);
  165. tot_len = reg_len + 10; /* ': R W V P\n' */
  166. for (i = 0; i <= map->max_register; i += map->reg_stride) {
  167. /* Ignore registers which are neither readable nor writable */
  168. if (!regmap_readable(map, i) && !regmap_writeable(map, i))
  169. continue;
  170. /* If we're in the region the user is trying to read */
  171. if (p >= *ppos) {
  172. /* ...but not beyond it */
  173. if (buf_pos >= count - 1 - tot_len)
  174. break;
  175. /* Format the register */
  176. snprintf(buf + buf_pos, count - buf_pos,
  177. "%.*x: %c %c %c %c\n",
  178. reg_len, i,
  179. regmap_readable(map, i) ? 'y' : 'n',
  180. regmap_writeable(map, i) ? 'y' : 'n',
  181. regmap_volatile(map, i) ? 'y' : 'n',
  182. regmap_precious(map, i) ? 'y' : 'n');
  183. buf_pos += tot_len;
  184. }
  185. p += tot_len;
  186. }
  187. ret = buf_pos;
  188. if (copy_to_user(user_buf, buf, buf_pos)) {
  189. ret = -EFAULT;
  190. goto out;
  191. }
  192. *ppos += buf_pos;
  193. out:
  194. kfree(buf);
  195. return ret;
  196. }
  197. static const struct file_operations regmap_access_fops = {
  198. .open = simple_open,
  199. .read = regmap_access_read_file,
  200. .llseek = default_llseek,
  201. };
  202. void regmap_debugfs_init(struct regmap *map, const char *name)
  203. {
  204. if (name) {
  205. map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s",
  206. dev_name(map->dev), name);
  207. name = map->debugfs_name;
  208. } else {
  209. name = dev_name(map->dev);
  210. }
  211. map->debugfs = debugfs_create_dir(name, regmap_debugfs_root);
  212. if (!map->debugfs) {
  213. dev_warn(map->dev, "Failed to create debugfs directory\n");
  214. return;
  215. }
  216. debugfs_create_file("name", 0400, map->debugfs,
  217. map, &regmap_name_fops);
  218. if (map->max_register) {
  219. debugfs_create_file("registers", 0400, map->debugfs,
  220. map, &regmap_map_fops);
  221. debugfs_create_file("access", 0400, map->debugfs,
  222. map, &regmap_access_fops);
  223. }
  224. if (map->cache_type) {
  225. debugfs_create_bool("cache_only", 0400, map->debugfs,
  226. &map->cache_only);
  227. debugfs_create_bool("cache_dirty", 0400, map->debugfs,
  228. &map->cache_dirty);
  229. debugfs_create_bool("cache_bypass", 0400, map->debugfs,
  230. &map->cache_bypass);
  231. }
  232. }
  233. void regmap_debugfs_exit(struct regmap *map)
  234. {
  235. debugfs_remove_recursive(map->debugfs);
  236. kfree(map->debugfs_name);
  237. }
  238. void regmap_debugfs_initcall(void)
  239. {
  240. regmap_debugfs_root = debugfs_create_dir("regmap", NULL);
  241. if (!regmap_debugfs_root) {
  242. pr_warn("regmap: Failed to create debugfs root\n");
  243. return;
  244. }
  245. }