regmap-debugfs.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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; 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. void regmap_debugfs_init(struct regmap *map)
  92. {
  93. map->debugfs = debugfs_create_dir(dev_name(map->dev),
  94. regmap_debugfs_root);
  95. if (!map->debugfs) {
  96. dev_warn(map->dev, "Failed to create debugfs directory\n");
  97. return;
  98. }
  99. if (map->max_register)
  100. debugfs_create_file("registers", 0400, map->debugfs,
  101. map, &regmap_map_fops);
  102. }
  103. void regmap_debugfs_exit(struct regmap *map)
  104. {
  105. debugfs_remove_recursive(map->debugfs);
  106. }
  107. void regmap_debugfs_initcall(void)
  108. {
  109. regmap_debugfs_root = debugfs_create_dir("regmap", NULL);
  110. if (!regmap_debugfs_root) {
  111. pr_warn("regmap: Failed to create debugfs root\n");
  112. return;
  113. }
  114. }