dbx500-prcmu.c 5.0 KB

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