2 * Copyright (C) ST-Ericsson SA 2010
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
8 * UX500 common part of Power domain regulators
11 #include <linux/kernel.h>
12 #include <linux/err.h>
13 #include <linux/regulator/driver.h>
14 #include <linux/debugfs.h>
15 #include <linux/seq_file.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
19 #include "dbx500-prcmu.h"
22 * power state reference count
24 static int power_state_active_cnt; /* will initialize to zero */
25 static DEFINE_SPINLOCK(power_state_active_lock);
27 void power_state_active_enable(void)
31 spin_lock_irqsave(&power_state_active_lock, flags);
32 power_state_active_cnt++;
33 spin_unlock_irqrestore(&power_state_active_lock, flags);
36 int power_state_active_disable(void)
41 spin_lock_irqsave(&power_state_active_lock, flags);
42 if (power_state_active_cnt <= 0) {
43 pr_err("power state: unbalanced enable/disable calls\n");
48 power_state_active_cnt--;
50 spin_unlock_irqrestore(&power_state_active_lock, flags);
54 #ifdef CONFIG_REGULATOR_DEBUG
56 static int power_state_active_get(void)
61 spin_lock_irqsave(&power_state_active_lock, flags);
62 cnt = power_state_active_cnt;
63 spin_unlock_irqrestore(&power_state_active_lock, flags);
68 static struct ux500_regulator_debug {
70 struct dentry *status_file;
71 struct dentry *power_state_cnt_file;
72 struct dbx500_regulator_info *regulator_array;
74 u8 *state_before_suspend;
75 u8 *state_after_suspend;
78 void ux500_regulator_suspend_debug(void)
81 for (i = 0; i < rdebug.num_regulators; i++)
82 rdebug.state_before_suspend[i] =
83 rdebug.regulator_array[i].is_enabled;
86 void ux500_regulator_resume_debug(void)
89 for (i = 0; i < rdebug.num_regulators; i++)
90 rdebug.state_after_suspend[i] =
91 rdebug.regulator_array[i].is_enabled;
94 static int ux500_regulator_power_state_cnt_print(struct seq_file *s, void *p)
96 struct device *dev = s->private;
99 /* print power state count */
100 err = seq_printf(s, "ux500-regulator power state count: %i\n",
101 power_state_active_get());
103 dev_err(dev, "seq_printf overflow\n");
108 static int ux500_regulator_power_state_cnt_open(struct inode *inode,
111 return single_open(file, ux500_regulator_power_state_cnt_print,
115 static const struct file_operations ux500_regulator_power_state_cnt_fops = {
116 .open = ux500_regulator_power_state_cnt_open,
119 .release = single_release,
120 .owner = THIS_MODULE,
123 static int ux500_regulator_status_print(struct seq_file *s, void *p)
125 struct device *dev = s->private;
129 /* print dump header */
130 err = seq_printf(s, "ux500-regulator status:\n");
132 dev_err(dev, "seq_printf overflow\n");
134 err = seq_printf(s, "%31s : %8s : %8s\n", "current",
137 dev_err(dev, "seq_printf overflow\n");
139 for (i = 0; i < rdebug.num_regulators; i++) {
140 struct dbx500_regulator_info *info;
141 /* Access per-regulator data */
142 info = &rdebug.regulator_array[i];
145 err = seq_printf(s, "%20s : %8s : %8s : %8s\n", info->desc.name,
146 info->is_enabled ? "enabled" : "disabled",
147 rdebug.state_before_suspend[i] ? "enabled" : "disabled",
148 rdebug.state_after_suspend[i] ? "enabled" : "disabled");
150 dev_err(dev, "seq_printf overflow\n");
156 static int ux500_regulator_status_open(struct inode *inode, struct file *file)
158 return single_open(file, ux500_regulator_status_print,
162 static const struct file_operations ux500_regulator_status_fops = {
163 .open = ux500_regulator_status_open,
166 .release = single_release,
167 .owner = THIS_MODULE,
170 int __attribute__((weak)) dbx500_regulator_testcase(
171 struct dbx500_regulator_info *regulator_info,
178 ux500_regulator_debug_init(struct platform_device *pdev,
179 struct dbx500_regulator_info *regulator_info,
182 /* create directory */
183 rdebug.dir = debugfs_create_dir("ux500-regulator", NULL);
185 goto exit_no_debugfs;
187 /* create "status" file */
188 rdebug.status_file = debugfs_create_file("status",
189 S_IRUGO, rdebug.dir, &pdev->dev,
190 &ux500_regulator_status_fops);
191 if (!rdebug.status_file)
192 goto exit_destroy_dir;
194 /* create "power-state-count" file */
195 rdebug.power_state_cnt_file = debugfs_create_file("power-state-count",
196 S_IRUGO, rdebug.dir, &pdev->dev,
197 &ux500_regulator_power_state_cnt_fops);
198 if (!rdebug.power_state_cnt_file)
199 goto exit_destroy_status;
201 rdebug.regulator_array = regulator_info;
202 rdebug.num_regulators = num_regulators;
204 rdebug.state_before_suspend = kzalloc(num_regulators, GFP_KERNEL);
205 if (!rdebug.state_before_suspend) {
207 "could not allocate memory for saving state\n");
208 goto exit_destroy_power_state;
211 rdebug.state_after_suspend = kzalloc(num_regulators, GFP_KERNEL);
212 if (!rdebug.state_after_suspend) {
214 "could not allocate memory for saving state\n");
218 dbx500_regulator_testcase(regulator_info, num_regulators);
222 kfree(rdebug.state_before_suspend);
223 exit_destroy_power_state:
224 debugfs_remove(rdebug.power_state_cnt_file);
226 debugfs_remove(rdebug.status_file);
228 debugfs_remove(rdebug.dir);
230 dev_err(&pdev->dev, "failed to create debugfs entries.\n");
234 int ux500_regulator_debug_exit(void)
236 debugfs_remove_recursive(rdebug.dir);
237 kfree(rdebug.state_after_suspend);
238 kfree(rdebug.state_before_suspend);