dbx500-prcmu.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2010
  3. *
  4. * License Terms: GNU General Public License v2
  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. void ux500_regulator_suspend_debug(void)
  65. {
  66. int i;
  67. for (i = 0; i < rdebug.num_regulators; i++)
  68. rdebug.state_before_suspend[i] =
  69. rdebug.regulator_array[i].is_enabled;
  70. }
  71. void ux500_regulator_resume_debug(void)
  72. {
  73. int i;
  74. for (i = 0; i < rdebug.num_regulators; i++)
  75. rdebug.state_after_suspend[i] =
  76. rdebug.regulator_array[i].is_enabled;
  77. }
  78. static int ux500_regulator_power_state_cnt_print(struct seq_file *s, void *p)
  79. {
  80. /* print power state count */
  81. seq_printf(s, "ux500-regulator power state count: %i\n",
  82. power_state_active_get());
  83. return 0;
  84. }
  85. static int ux500_regulator_power_state_cnt_open(struct inode *inode,
  86. struct file *file)
  87. {
  88. return single_open(file, ux500_regulator_power_state_cnt_print,
  89. inode->i_private);
  90. }
  91. static const struct file_operations ux500_regulator_power_state_cnt_fops = {
  92. .open = ux500_regulator_power_state_cnt_open,
  93. .read = seq_read,
  94. .llseek = seq_lseek,
  95. .release = single_release,
  96. .owner = THIS_MODULE,
  97. };
  98. static int ux500_regulator_status_print(struct seq_file *s, void *p)
  99. {
  100. int i;
  101. /* print dump header */
  102. seq_puts(s, "ux500-regulator status:\n");
  103. seq_printf(s, "%31s : %8s : %8s\n", "current", "before", "after");
  104. for (i = 0; i < rdebug.num_regulators; i++) {
  105. struct dbx500_regulator_info *info;
  106. /* Access per-regulator data */
  107. info = &rdebug.regulator_array[i];
  108. /* print status */
  109. seq_printf(s, "%20s : %8s : %8s : %8s\n",
  110. info->desc.name,
  111. info->is_enabled ? "enabled" : "disabled",
  112. rdebug.state_before_suspend[i] ? "enabled" : "disabled",
  113. rdebug.state_after_suspend[i] ? "enabled" : "disabled");
  114. }
  115. return 0;
  116. }
  117. static int ux500_regulator_status_open(struct inode *inode, struct file *file)
  118. {
  119. return single_open(file, ux500_regulator_status_print,
  120. inode->i_private);
  121. }
  122. static const struct file_operations ux500_regulator_status_fops = {
  123. .open = ux500_regulator_status_open,
  124. .read = seq_read,
  125. .llseek = seq_lseek,
  126. .release = single_release,
  127. .owner = THIS_MODULE,
  128. };
  129. int __attribute__((weak)) dbx500_regulator_testcase(
  130. struct dbx500_regulator_info *regulator_info,
  131. int num_regulators)
  132. {
  133. return 0;
  134. }
  135. int
  136. ux500_regulator_debug_init(struct platform_device *pdev,
  137. struct dbx500_regulator_info *regulator_info,
  138. int num_regulators)
  139. {
  140. /* create directory */
  141. rdebug.dir = debugfs_create_dir("ux500-regulator", NULL);
  142. if (!rdebug.dir)
  143. goto exit_no_debugfs;
  144. /* create "status" file */
  145. rdebug.status_file = debugfs_create_file("status",
  146. S_IRUGO, rdebug.dir, &pdev->dev,
  147. &ux500_regulator_status_fops);
  148. if (!rdebug.status_file)
  149. goto exit_destroy_dir;
  150. /* create "power-state-count" file */
  151. rdebug.power_state_cnt_file = debugfs_create_file("power-state-count",
  152. S_IRUGO, rdebug.dir, &pdev->dev,
  153. &ux500_regulator_power_state_cnt_fops);
  154. if (!rdebug.power_state_cnt_file)
  155. goto exit_destroy_status;
  156. rdebug.regulator_array = regulator_info;
  157. rdebug.num_regulators = num_regulators;
  158. rdebug.state_before_suspend = kzalloc(num_regulators, GFP_KERNEL);
  159. if (!rdebug.state_before_suspend)
  160. goto exit_destroy_power_state;
  161. rdebug.state_after_suspend = kzalloc(num_regulators, GFP_KERNEL);
  162. if (!rdebug.state_after_suspend)
  163. goto exit_free;
  164. dbx500_regulator_testcase(regulator_info, num_regulators);
  165. return 0;
  166. exit_free:
  167. kfree(rdebug.state_before_suspend);
  168. exit_destroy_power_state:
  169. debugfs_remove(rdebug.power_state_cnt_file);
  170. exit_destroy_status:
  171. debugfs_remove(rdebug.status_file);
  172. exit_destroy_dir:
  173. debugfs_remove(rdebug.dir);
  174. exit_no_debugfs:
  175. dev_err(&pdev->dev, "failed to create debugfs entries.\n");
  176. return -ENOMEM;
  177. }
  178. int ux500_regulator_debug_exit(void)
  179. {
  180. debugfs_remove_recursive(rdebug.dir);
  181. kfree(rdebug.state_after_suspend);
  182. kfree(rdebug.state_before_suspend);
  183. return 0;
  184. }
  185. #endif