dbx500-prcmu.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 dbx500_regulator_info *regulator_array;
  58. int num_regulators;
  59. u8 *state_before_suspend;
  60. u8 *state_after_suspend;
  61. } rdebug;
  62. static int ux500_regulator_power_state_cnt_show(struct seq_file *s, void *p)
  63. {
  64. /* print power state count */
  65. seq_printf(s, "ux500-regulator power state count: %i\n",
  66. power_state_active_get());
  67. return 0;
  68. }
  69. DEFINE_SHOW_ATTRIBUTE(ux500_regulator_power_state_cnt);
  70. static int ux500_regulator_status_show(struct seq_file *s, void *p)
  71. {
  72. int i;
  73. /* print dump header */
  74. seq_puts(s, "ux500-regulator status:\n");
  75. seq_printf(s, "%31s : %8s : %8s\n", "current", "before", "after");
  76. for (i = 0; i < rdebug.num_regulators; i++) {
  77. struct dbx500_regulator_info *info;
  78. /* Access per-regulator data */
  79. info = &rdebug.regulator_array[i];
  80. /* print status */
  81. seq_printf(s, "%20s : %8s : %8s : %8s\n",
  82. info->desc.name,
  83. info->is_enabled ? "enabled" : "disabled",
  84. rdebug.state_before_suspend[i] ? "enabled" : "disabled",
  85. rdebug.state_after_suspend[i] ? "enabled" : "disabled");
  86. }
  87. return 0;
  88. }
  89. DEFINE_SHOW_ATTRIBUTE(ux500_regulator_status);
  90. int
  91. ux500_regulator_debug_init(struct platform_device *pdev,
  92. struct dbx500_regulator_info *regulator_info,
  93. int num_regulators)
  94. {
  95. /* create directory */
  96. rdebug.dir = debugfs_create_dir("ux500-regulator", NULL);
  97. /* create "status" file */
  98. debugfs_create_file("status", S_IRUGO, rdebug.dir, &pdev->dev,
  99. &ux500_regulator_status_fops);
  100. /* create "power-state-count" file */
  101. debugfs_create_file("power-state-count", S_IRUGO, rdebug.dir,
  102. &pdev->dev, &ux500_regulator_power_state_cnt_fops);
  103. rdebug.regulator_array = regulator_info;
  104. rdebug.num_regulators = num_regulators;
  105. rdebug.state_before_suspend = kzalloc(num_regulators, GFP_KERNEL);
  106. if (!rdebug.state_before_suspend)
  107. goto exit_destroy_power_state;
  108. rdebug.state_after_suspend = kzalloc(num_regulators, GFP_KERNEL);
  109. if (!rdebug.state_after_suspend)
  110. goto exit_free;
  111. return 0;
  112. exit_free:
  113. kfree(rdebug.state_before_suspend);
  114. exit_destroy_power_state:
  115. debugfs_remove_recursive(rdebug.dir);
  116. return -ENOMEM;
  117. }
  118. int ux500_regulator_debug_exit(void)
  119. {
  120. debugfs_remove_recursive(rdebug.dir);
  121. kfree(rdebug.state_after_suspend);
  122. kfree(rdebug.state_before_suspend);
  123. return 0;
  124. }
  125. #endif