dbx500-prcmu.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) ST-Ericsson SA 2010
  4. *
  5. * Authors: Sundar Iyer <sundar.iyer@stericsson.com> for ST-Ericsson
  6. * Bengt Jonsson <bengt.g.jonsson@stericsson.com> for ST-Ericsson
  7. *
  8. * UX500 common part of Power domain regulators
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/err.h>
  12. #include <linux/regulator/driver.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/slab.h>
  16. #include <linux/module.h>
  17. #include "dbx500-prcmu.h"
  18. /*
  19. * power state reference count
  20. */
  21. static int power_state_active_cnt; /* will initialize to zero */
  22. static DEFINE_SPINLOCK(power_state_active_lock);
  23. void power_state_active_enable(void)
  24. {
  25. unsigned long flags;
  26. spin_lock_irqsave(&power_state_active_lock, flags);
  27. power_state_active_cnt++;
  28. spin_unlock_irqrestore(&power_state_active_lock, flags);
  29. }
  30. int power_state_active_disable(void)
  31. {
  32. int ret = 0;
  33. unsigned long flags;
  34. spin_lock_irqsave(&power_state_active_lock, flags);
  35. if (power_state_active_cnt <= 0) {
  36. pr_err("power state: unbalanced enable/disable calls\n");
  37. ret = -EINVAL;
  38. goto out;
  39. }
  40. power_state_active_cnt--;
  41. out:
  42. spin_unlock_irqrestore(&power_state_active_lock, flags);
  43. return ret;
  44. }
  45. #ifdef CONFIG_REGULATOR_DEBUG
  46. static int power_state_active_get(void)
  47. {
  48. unsigned long flags;
  49. int cnt;
  50. spin_lock_irqsave(&power_state_active_lock, flags);
  51. cnt = power_state_active_cnt;
  52. spin_unlock_irqrestore(&power_state_active_lock, flags);
  53. return cnt;
  54. }
  55. static struct ux500_regulator_debug {
  56. struct dentry *dir;
  57. struct dentry *status_file;
  58. struct dentry *power_state_cnt_file;
  59. struct dbx500_regulator_info *regulator_array;
  60. int num_regulators;
  61. u8 *state_before_suspend;
  62. u8 *state_after_suspend;
  63. } rdebug;
  64. static int ux500_regulator_power_state_cnt_show(struct seq_file *s, void *p)
  65. {
  66. /* print power state count */
  67. seq_printf(s, "ux500-regulator power state count: %i\n",
  68. power_state_active_get());
  69. return 0;
  70. }
  71. DEFINE_SHOW_ATTRIBUTE(ux500_regulator_power_state_cnt);
  72. static int ux500_regulator_status_show(struct seq_file *s, void *p)
  73. {
  74. int i;
  75. /* print dump header */
  76. seq_puts(s, "ux500-regulator status:\n");
  77. seq_printf(s, "%31s : %8s : %8s\n", "current", "before", "after");
  78. for (i = 0; i < rdebug.num_regulators; i++) {
  79. struct dbx500_regulator_info *info;
  80. /* Access per-regulator data */
  81. info = &rdebug.regulator_array[i];
  82. /* print status */
  83. seq_printf(s, "%20s : %8s : %8s : %8s\n",
  84. info->desc.name,
  85. info->is_enabled ? "enabled" : "disabled",
  86. rdebug.state_before_suspend[i] ? "enabled" : "disabled",
  87. rdebug.state_after_suspend[i] ? "enabled" : "disabled");
  88. }
  89. return 0;
  90. }
  91. DEFINE_SHOW_ATTRIBUTE(ux500_regulator_status);
  92. int __attribute__((weak)) dbx500_regulator_testcase(
  93. struct dbx500_regulator_info *regulator_info,
  94. int num_regulators)
  95. {
  96. return 0;
  97. }
  98. int
  99. ux500_regulator_debug_init(struct platform_device *pdev,
  100. struct dbx500_regulator_info *regulator_info,
  101. int num_regulators)
  102. {
  103. /* create directory */
  104. rdebug.dir = debugfs_create_dir("ux500-regulator", NULL);
  105. if (!rdebug.dir)
  106. goto exit_no_debugfs;
  107. /* create "status" file */
  108. rdebug.status_file = debugfs_create_file("status",
  109. S_IRUGO, rdebug.dir, &pdev->dev,
  110. &ux500_regulator_status_fops);
  111. if (!rdebug.status_file)
  112. goto exit_destroy_dir;
  113. /* create "power-state-count" file */
  114. rdebug.power_state_cnt_file = debugfs_create_file("power-state-count",
  115. S_IRUGO, rdebug.dir, &pdev->dev,
  116. &ux500_regulator_power_state_cnt_fops);
  117. if (!rdebug.power_state_cnt_file)
  118. goto exit_destroy_status;
  119. rdebug.regulator_array = regulator_info;
  120. rdebug.num_regulators = num_regulators;
  121. rdebug.state_before_suspend = kzalloc(num_regulators, GFP_KERNEL);
  122. if (!rdebug.state_before_suspend)
  123. goto exit_destroy_power_state;
  124. rdebug.state_after_suspend = kzalloc(num_regulators, GFP_KERNEL);
  125. if (!rdebug.state_after_suspend)
  126. goto exit_free;
  127. dbx500_regulator_testcase(regulator_info, num_regulators);
  128. return 0;
  129. exit_free:
  130. kfree(rdebug.state_before_suspend);
  131. exit_destroy_power_state:
  132. debugfs_remove(rdebug.power_state_cnt_file);
  133. exit_destroy_status:
  134. debugfs_remove(rdebug.status_file);
  135. exit_destroy_dir:
  136. debugfs_remove(rdebug.dir);
  137. exit_no_debugfs:
  138. dev_err(&pdev->dev, "failed to create debugfs entries.\n");
  139. return -ENOMEM;
  140. }
  141. int ux500_regulator_debug_exit(void)
  142. {
  143. debugfs_remove_recursive(rdebug.dir);
  144. kfree(rdebug.state_after_suspend);
  145. kfree(rdebug.state_before_suspend);
  146. return 0;
  147. }
  148. #endif