msm_cpr-debug.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2012, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #define pr_fmt(fmt) "%s: " fmt, __func__
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include <linux/err.h>
  19. #include <linux/debugfs.h>
  20. #include <linux/module.h>
  21. struct msm_cpr_debug_device {
  22. struct mutex debug_mutex;
  23. struct dentry *dir;
  24. int addr_offset;
  25. void __iomem *base;
  26. };
  27. static inline
  28. void write_reg(struct msm_cpr_debug_device *cpr, u32 value)
  29. {
  30. writel_relaxed(value, cpr->base + cpr->addr_offset);
  31. }
  32. static inline u32 read_reg(struct msm_cpr_debug_device *cpr)
  33. {
  34. return readl_relaxed(cpr->base + cpr->addr_offset);
  35. }
  36. static bool msm_cpr_debug_addr_is_valid(int addr)
  37. {
  38. if (addr < 0 || addr > 0x15C) {
  39. pr_err("CPR register address is invalid: %d\n", addr);
  40. return false;
  41. }
  42. return true;
  43. }
  44. static int msm_cpr_debug_data_set(void *data, u64 val)
  45. {
  46. struct msm_cpr_debug_device *debugdev = data;
  47. uint32_t reg = val;
  48. mutex_lock(&debugdev->debug_mutex);
  49. if (msm_cpr_debug_addr_is_valid(debugdev->addr_offset))
  50. write_reg(debugdev, reg);
  51. mutex_unlock(&debugdev->debug_mutex);
  52. return 0;
  53. }
  54. static int msm_cpr_debug_data_get(void *data, u64 *val)
  55. {
  56. struct msm_cpr_debug_device *debugdev = data;
  57. uint32_t reg;
  58. mutex_lock(&debugdev->debug_mutex);
  59. if (msm_cpr_debug_addr_is_valid(debugdev->addr_offset)) {
  60. reg = read_reg(debugdev);
  61. *val = reg;
  62. }
  63. mutex_unlock(&debugdev->debug_mutex);
  64. return 0;
  65. }
  66. DEFINE_SIMPLE_ATTRIBUTE(debug_data_fops, msm_cpr_debug_data_get,
  67. msm_cpr_debug_data_set, "0x%02llX\n");
  68. static int msm_cpr_debug_addr_set(void *data, u64 val)
  69. {
  70. struct msm_cpr_debug_device *debugdev = data;
  71. if (msm_cpr_debug_addr_is_valid(val)) {
  72. mutex_lock(&debugdev->debug_mutex);
  73. debugdev->addr_offset = val;
  74. mutex_unlock(&debugdev->debug_mutex);
  75. }
  76. return 0;
  77. }
  78. static int msm_cpr_debug_addr_get(void *data, u64 *val)
  79. {
  80. struct msm_cpr_debug_device *debugdev = data;
  81. mutex_lock(&debugdev->debug_mutex);
  82. if (msm_cpr_debug_addr_is_valid(debugdev->addr_offset))
  83. *val = debugdev->addr_offset;
  84. mutex_unlock(&debugdev->debug_mutex);
  85. return 0;
  86. }
  87. DEFINE_SIMPLE_ATTRIBUTE(debug_addr_fops, msm_cpr_debug_addr_get,
  88. msm_cpr_debug_addr_set, "0x%03llX\n");
  89. int msm_cpr_debug_init(void *data)
  90. {
  91. char *name = "cpr-debug";
  92. struct msm_cpr_debug_device *debugdev;
  93. struct dentry *dir;
  94. struct dentry *temp;
  95. int rc;
  96. debugdev = kzalloc(sizeof(struct msm_cpr_debug_device), GFP_KERNEL);
  97. if (debugdev == NULL) {
  98. pr_err("kzalloc failed\n");
  99. return -ENOMEM;
  100. }
  101. dir = debugfs_create_dir(name, NULL);
  102. if (dir == NULL || IS_ERR(dir)) {
  103. pr_err("debugfs_create_dir failed: rc=%ld\n", PTR_ERR(dir));
  104. rc = PTR_ERR(dir);
  105. goto dir_error;
  106. }
  107. temp = debugfs_create_file("address", S_IRUGO | S_IWUSR, dir, debugdev,
  108. &debug_addr_fops);
  109. if (temp == NULL || IS_ERR(temp)) {
  110. pr_err("debugfs_create_file failed: rc=%ld\n", PTR_ERR(temp));
  111. rc = PTR_ERR(temp);
  112. goto file_error;
  113. }
  114. temp = debugfs_create_file("data", S_IRUGO | S_IWUSR, dir, debugdev,
  115. &debug_data_fops);
  116. if (temp == NULL || IS_ERR(temp)) {
  117. pr_err("debugfs_create_file failed: rc=%ld\n", PTR_ERR(temp));
  118. rc = PTR_ERR(temp);
  119. goto file_error;
  120. }
  121. debugdev->base = data;
  122. debugdev->addr_offset = -1;
  123. debugdev->dir = dir;
  124. mutex_init(&debugdev->debug_mutex);
  125. return 0;
  126. file_error:
  127. debugfs_remove_recursive(dir);
  128. dir_error:
  129. kfree(debugdev);
  130. return rc;
  131. }