regmap-debugfs.c 4.6 KB

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