regmap-debugfs.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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/module.h>
  14. #include <linux/mutex.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/uaccess.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_map_read_file(struct file *file, char __user *user_buf,
  31. size_t count, loff_t *ppos)
  32. {
  33. int reg_len, val_len, tot_len;
  34. size_t buf_pos = 0;
  35. loff_t p = 0;
  36. ssize_t ret;
  37. int i;
  38. struct regmap *map = file->private_data;
  39. char *buf;
  40. unsigned int val;
  41. if (*ppos < 0 || !count)
  42. return -EINVAL;
  43. buf = kmalloc(count, GFP_KERNEL);
  44. if (!buf)
  45. return -ENOMEM;
  46. /* Calculate the length of a fixed format */
  47. reg_len = regmap_calc_reg_len(map->max_register, buf, count);
  48. val_len = 2 * map->format.val_bytes;
  49. tot_len = reg_len + val_len + 3; /* : \n */
  50. for (i = 0; i < map->max_register + 1; i++) {
  51. if (!regmap_readable(map, i))
  52. continue;
  53. if (regmap_precious(map, i))
  54. continue;
  55. /* If we're in the region the user is trying to read */
  56. if (p >= *ppos) {
  57. /* ...but not beyond it */
  58. if (buf_pos >= count - 1 - tot_len)
  59. break;
  60. /* Format the register */
  61. snprintf(buf + buf_pos, count - buf_pos, "%.*x: ",
  62. reg_len, i);
  63. buf_pos += reg_len + 2;
  64. /* Format the value, write all X if we can't read */
  65. ret = regmap_read(map, i, &val);
  66. if (ret == 0)
  67. snprintf(buf + buf_pos, count - buf_pos,
  68. "%.*x", val_len, val);
  69. else
  70. memset(buf + buf_pos, 'X', val_len);
  71. buf_pos += 2 * map->format.val_bytes;
  72. buf[buf_pos++] = '\n';
  73. }
  74. p += tot_len;
  75. }
  76. ret = buf_pos;
  77. if (copy_to_user(user_buf, buf, buf_pos)) {
  78. ret = -EFAULT;
  79. goto out;
  80. }
  81. *ppos += buf_pos;
  82. out:
  83. kfree(buf);
  84. return ret;
  85. }
  86. static const struct file_operations regmap_map_fops = {
  87. .open = regmap_open_file,
  88. .read = regmap_map_read_file,
  89. .llseek = default_llseek,
  90. };
  91. static ssize_t regmap_access_read_file(struct file *file,
  92. char __user *user_buf, size_t count,
  93. loff_t *ppos)
  94. {
  95. int reg_len, tot_len;
  96. size_t buf_pos = 0;
  97. loff_t p = 0;
  98. ssize_t ret;
  99. int i;
  100. struct regmap *map = file->private_data;
  101. char *buf;
  102. if (*ppos < 0 || !count)
  103. return -EINVAL;
  104. buf = kmalloc(count, GFP_KERNEL);
  105. if (!buf)
  106. return -ENOMEM;
  107. /* Calculate the length of a fixed format */
  108. reg_len = regmap_calc_reg_len(map->max_register, buf, count);
  109. tot_len = reg_len + 10; /* ': R W V P\n' */
  110. for (i = 0; i < map->max_register + 1; i++) {
  111. /* Ignore registers which are neither readable nor writable */
  112. if (!regmap_readable(map, i) && !regmap_writeable(map, i))
  113. continue;
  114. /* If we're in the region the user is trying to read */
  115. if (p >= *ppos) {
  116. /* ...but not beyond it */
  117. if (buf_pos >= count - 1 - tot_len)
  118. break;
  119. /* Format the register */
  120. snprintf(buf + buf_pos, count - buf_pos,
  121. "%.*x: %c %c %c %c\n",
  122. reg_len, i,
  123. regmap_readable(map, i) ? 'y' : 'n',
  124. regmap_writeable(map, i) ? 'y' : 'n',
  125. regmap_volatile(map, i) ? 'y' : 'n',
  126. regmap_precious(map, i) ? 'y' : 'n');
  127. buf_pos += tot_len;
  128. }
  129. p += tot_len;
  130. }
  131. ret = buf_pos;
  132. if (copy_to_user(user_buf, buf, buf_pos)) {
  133. ret = -EFAULT;
  134. goto out;
  135. }
  136. *ppos += buf_pos;
  137. out:
  138. kfree(buf);
  139. return ret;
  140. }
  141. static const struct file_operations regmap_access_fops = {
  142. .open = regmap_open_file,
  143. .read = regmap_access_read_file,
  144. .llseek = default_llseek,
  145. };
  146. void regmap_debugfs_init(struct regmap *map)
  147. {
  148. map->debugfs = debugfs_create_dir(dev_name(map->dev),
  149. regmap_debugfs_root);
  150. if (!map->debugfs) {
  151. dev_warn(map->dev, "Failed to create debugfs directory\n");
  152. return;
  153. }
  154. if (map->max_register) {
  155. debugfs_create_file("registers", 0400, map->debugfs,
  156. map, &regmap_map_fops);
  157. debugfs_create_file("access", 0400, map->debugfs,
  158. map, &regmap_access_fops);
  159. }
  160. }
  161. void regmap_debugfs_exit(struct regmap *map)
  162. {
  163. debugfs_remove_recursive(map->debugfs);
  164. }
  165. void regmap_debugfs_initcall(void)
  166. {
  167. regmap_debugfs_root = debugfs_create_dir("regmap", NULL);
  168. if (!regmap_debugfs_root) {
  169. pr_warn("regmap: Failed to create debugfs root\n");
  170. return;
  171. }
  172. }