pm8xxx-debug.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (c) 2011, 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. #define pr_fmt(fmt) "%s: " fmt, __func__
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include <linux/err.h>
  19. #include <linux/mfd/pm8xxx/core.h>
  20. #include <linux/debugfs.h>
  21. #define PM8XXX_DEBUG_DEV_NAME "pm8xxx-debug"
  22. struct pm8xxx_debug_device {
  23. struct mutex debug_mutex;
  24. struct device *parent;
  25. struct dentry *dir;
  26. int addr;
  27. };
  28. static bool pm8xxx_debug_addr_is_valid(int addr)
  29. {
  30. if (addr < 0 || addr > 0x3FF) {
  31. pr_err("PMIC register address is invalid: %d\n", addr);
  32. return false;
  33. }
  34. return true;
  35. }
  36. static int pm8xxx_debug_data_set(void *data, u64 val)
  37. {
  38. struct pm8xxx_debug_device *debugdev = data;
  39. u8 reg = val;
  40. int rc;
  41. mutex_lock(&debugdev->debug_mutex);
  42. if (pm8xxx_debug_addr_is_valid(debugdev->addr)) {
  43. rc = pm8xxx_writeb(debugdev->parent, debugdev->addr, reg);
  44. if (rc)
  45. pr_err("pm8xxx_writeb(0x%03X)=0x%02X failed: rc=%d\n",
  46. debugdev->addr, reg, rc);
  47. }
  48. mutex_unlock(&debugdev->debug_mutex);
  49. return 0;
  50. }
  51. static int pm8xxx_debug_data_get(void *data, u64 *val)
  52. {
  53. struct pm8xxx_debug_device *debugdev = data;
  54. int rc;
  55. u8 reg;
  56. mutex_lock(&debugdev->debug_mutex);
  57. if (pm8xxx_debug_addr_is_valid(debugdev->addr)) {
  58. rc = pm8xxx_readb(debugdev->parent, debugdev->addr, &reg);
  59. if (rc)
  60. pr_err("pm8xxx_readb(0x%03X) failed: rc=%d\n",
  61. debugdev->addr, rc);
  62. else
  63. *val = reg;
  64. }
  65. mutex_unlock(&debugdev->debug_mutex);
  66. return 0;
  67. }
  68. DEFINE_SIMPLE_ATTRIBUTE(debug_data_fops, pm8xxx_debug_data_get,
  69. pm8xxx_debug_data_set, "0x%02llX\n");
  70. static int pm8xxx_debug_addr_set(void *data, u64 val)
  71. {
  72. struct pm8xxx_debug_device *debugdev = data;
  73. if (pm8xxx_debug_addr_is_valid(val)) {
  74. mutex_lock(&debugdev->debug_mutex);
  75. debugdev->addr = val;
  76. mutex_unlock(&debugdev->debug_mutex);
  77. }
  78. return 0;
  79. }
  80. static int pm8xxx_debug_addr_get(void *data, u64 *val)
  81. {
  82. struct pm8xxx_debug_device *debugdev = data;
  83. mutex_lock(&debugdev->debug_mutex);
  84. if (pm8xxx_debug_addr_is_valid(debugdev->addr))
  85. *val = debugdev->addr;
  86. mutex_unlock(&debugdev->debug_mutex);
  87. return 0;
  88. }
  89. DEFINE_SIMPLE_ATTRIBUTE(debug_addr_fops, pm8xxx_debug_addr_get,
  90. pm8xxx_debug_addr_set, "0x%03llX\n");
  91. static int __devinit pm8xxx_debug_probe(struct platform_device *pdev)
  92. {
  93. char *name = pdev->dev.platform_data;
  94. struct pm8xxx_debug_device *debugdev;
  95. struct dentry *dir;
  96. struct dentry *temp;
  97. int rc;
  98. if (name == NULL) {
  99. pr_err("debugfs directory name must be specified in "
  100. "platform_data pointer\n");
  101. return -EINVAL;
  102. }
  103. debugdev = kzalloc(sizeof(struct pm8xxx_debug_device), GFP_KERNEL);
  104. if (debugdev == NULL) {
  105. pr_err("kzalloc failed\n");
  106. return -ENOMEM;
  107. }
  108. debugdev->parent = pdev->dev.parent;
  109. debugdev->addr = -1;
  110. dir = debugfs_create_dir(name, NULL);
  111. if (dir == NULL || IS_ERR(dir)) {
  112. pr_err("debugfs_create_dir failed: rc=%ld\n", PTR_ERR(dir));
  113. rc = PTR_ERR(dir);
  114. goto dir_error;
  115. }
  116. temp = debugfs_create_file("addr", S_IRUSR | S_IWUSR, dir, debugdev,
  117. &debug_addr_fops);
  118. if (temp == NULL || IS_ERR(temp)) {
  119. pr_err("debugfs_create_file failed: rc=%ld\n", PTR_ERR(temp));
  120. rc = PTR_ERR(temp);
  121. goto file_error;
  122. }
  123. temp = debugfs_create_file("data", S_IRUSR | S_IWUSR, dir, debugdev,
  124. &debug_data_fops);
  125. if (temp == NULL || IS_ERR(temp)) {
  126. pr_err("debugfs_create_file failed: rc=%ld\n", PTR_ERR(temp));
  127. rc = PTR_ERR(temp);
  128. goto file_error;
  129. }
  130. mutex_init(&debugdev->debug_mutex);
  131. debugdev->dir = dir;
  132. platform_set_drvdata(pdev, debugdev);
  133. return 0;
  134. file_error:
  135. debugfs_remove_recursive(dir);
  136. dir_error:
  137. kfree(debugdev);
  138. return rc;
  139. }
  140. static int __devexit pm8xxx_debug_remove(struct platform_device *pdev)
  141. {
  142. struct pm8xxx_debug_device *debugdev = platform_get_drvdata(pdev);
  143. if (debugdev) {
  144. debugfs_remove_recursive(debugdev->dir);
  145. mutex_destroy(&debugdev->debug_mutex);
  146. kfree(debugdev);
  147. }
  148. platform_set_drvdata(pdev, NULL);
  149. return 0;
  150. }
  151. static struct platform_driver pm8xxx_debug_driver = {
  152. .probe = pm8xxx_debug_probe,
  153. .remove = __devexit_p(pm8xxx_debug_remove),
  154. .driver = {
  155. .name = PM8XXX_DEBUG_DEV_NAME,
  156. .owner = THIS_MODULE,
  157. },
  158. };
  159. static int __init pm8xxx_debug_init(void)
  160. {
  161. return platform_driver_register(&pm8xxx_debug_driver);
  162. }
  163. subsys_initcall(pm8xxx_debug_init);
  164. static void __exit pm8xxx_debug_exit(void)
  165. {
  166. platform_driver_unregister(&pm8xxx_debug_driver);
  167. }
  168. module_exit(pm8xxx_debug_exit);
  169. MODULE_LICENSE("GPL v2");
  170. MODULE_DESCRIPTION("PM8XXX Debug driver");
  171. MODULE_VERSION("1.0");
  172. MODULE_ALIAS("platform:" PM8XXX_DEBUG_DEV_NAME);