regmap-debugfs.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. static int regmap_map_open_file(struct inode *inode, struct file *file)
  20. {
  21. file->private_data = inode->i_private;
  22. return 0;
  23. }
  24. static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf,
  25. size_t count, loff_t *ppos)
  26. {
  27. int reg_len, val_len, tot_len;
  28. size_t buf_pos = 0;
  29. loff_t p = 0;
  30. ssize_t ret;
  31. int i;
  32. struct regmap *map = file->private_data;
  33. char *buf;
  34. unsigned int val;
  35. if (*ppos < 0 || !count)
  36. return -EINVAL;
  37. buf = kmalloc(count, GFP_KERNEL);
  38. if (!buf)
  39. return -ENOMEM;
  40. /* Calculate the length of a fixed format */
  41. snprintf(buf, count, "%x", map->max_register);
  42. reg_len = strlen(buf);
  43. val_len = 2 * map->format.val_bytes;
  44. tot_len = reg_len + val_len + 3; /* : \n */
  45. for (i = 0; i < map->max_register; i++) {
  46. if (map->readable_reg &&
  47. !map->readable_reg(map->dev, i))
  48. continue;
  49. if (map->precious_reg &&
  50. map->precious_reg(map->dev, i))
  51. continue;
  52. /* If we're in the region the user is trying to read */
  53. if (p >= *ppos) {
  54. /* ...but not beyond it */
  55. if (buf_pos >= count - 1 - tot_len)
  56. break;
  57. /* Format the register */
  58. snprintf(buf + buf_pos, count - buf_pos, "%.*x: ",
  59. reg_len, i);
  60. buf_pos += reg_len + 2;
  61. /* Format the value, write all X if we can't read */
  62. ret = regmap_read(map, i, &val);
  63. if (ret == 0)
  64. snprintf(buf + buf_pos, count - buf_pos,
  65. "%.*x", val_len, val);
  66. else
  67. memset(buf + buf_pos, 'X', val_len);
  68. buf_pos += 2 * map->format.val_bytes;
  69. buf[buf_pos++] = '\n';
  70. }
  71. p += tot_len;
  72. }
  73. ret = buf_pos;
  74. if (copy_to_user(user_buf, buf, buf_pos)) {
  75. ret = -EFAULT;
  76. goto out;
  77. }
  78. *ppos += buf_pos;
  79. out:
  80. kfree(buf);
  81. return ret;
  82. }
  83. static const struct file_operations regmap_map_fops = {
  84. .open = regmap_map_open_file,
  85. .read = regmap_map_read_file,
  86. .llseek = default_llseek,
  87. };
  88. void regmap_debugfs_init(struct regmap *map)
  89. {
  90. map->debugfs = debugfs_create_dir(dev_name(map->dev),
  91. regmap_debugfs_root);
  92. if (!map->debugfs) {
  93. dev_warn(map->dev, "Failed to create debugfs directory\n");
  94. return;
  95. }
  96. if (map->max_register)
  97. debugfs_create_file("registers", 0400, map->debugfs,
  98. map, &regmap_map_fops);
  99. }
  100. void regmap_debugfs_exit(struct regmap *map)
  101. {
  102. debugfs_remove_recursive(map->debugfs);
  103. }
  104. void regmap_debugfs_initcall(void)
  105. {
  106. regmap_debugfs_root = debugfs_create_dir("regmap", NULL);
  107. if (!regmap_debugfs_root) {
  108. pr_warn("regmap: Failed to create debugfs root\n");
  109. return;
  110. }
  111. }