ARM: 7544/1: Add BUG_ON when hlt counter is wrongly used
[linux-2.6.git] / drivers / regulator / dbx500-prcmu.c
blobf2e5ecdc586424ba1f92947ee5c31f1ce3268cad
1 /*
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
9 */
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>
18 #include "dbx500-prcmu.h"
21 * power state reference count
23 static int power_state_active_cnt; /* will initialize to zero */
24 static DEFINE_SPINLOCK(power_state_active_lock);
26 int power_state_active_get(void)
28 unsigned long flags;
29 int cnt;
31 spin_lock_irqsave(&power_state_active_lock, flags);
32 cnt = power_state_active_cnt;
33 spin_unlock_irqrestore(&power_state_active_lock, flags);
35 return cnt;
38 void power_state_active_enable(void)
40 unsigned long flags;
42 spin_lock_irqsave(&power_state_active_lock, flags);
43 power_state_active_cnt++;
44 spin_unlock_irqrestore(&power_state_active_lock, flags);
47 int power_state_active_disable(void)
49 int ret = 0;
50 unsigned long flags;
52 spin_lock_irqsave(&power_state_active_lock, flags);
53 if (power_state_active_cnt <= 0) {
54 pr_err("power state: unbalanced enable/disable calls\n");
55 ret = -EINVAL;
56 goto out;
59 power_state_active_cnt--;
60 out:
61 spin_unlock_irqrestore(&power_state_active_lock, flags);
62 return ret;
65 #ifdef CONFIG_REGULATOR_DEBUG
67 static struct ux500_regulator_debug {
68 struct dentry *dir;
69 struct dentry *status_file;
70 struct dentry *power_state_cnt_file;
71 struct dbx500_regulator_info *regulator_array;
72 int num_regulators;
73 u8 *state_before_suspend;
74 u8 *state_after_suspend;
75 } rdebug;
77 void ux500_regulator_suspend_debug(void)
79 int i;
80 for (i = 0; i < rdebug.num_regulators; i++)
81 rdebug.state_before_suspend[i] =
82 rdebug.regulator_array[i].is_enabled;
85 void ux500_regulator_resume_debug(void)
87 int i;
88 for (i = 0; i < rdebug.num_regulators; i++)
89 rdebug.state_after_suspend[i] =
90 rdebug.regulator_array[i].is_enabled;
93 static int ux500_regulator_power_state_cnt_print(struct seq_file *s, void *p)
95 struct device *dev = s->private;
96 int err;
98 /* print power state count */
99 err = seq_printf(s, "ux500-regulator power state count: %i\n",
100 power_state_active_get());
101 if (err < 0)
102 dev_err(dev, "seq_printf overflow\n");
104 return 0;
107 static int ux500_regulator_power_state_cnt_open(struct inode *inode,
108 struct file *file)
110 return single_open(file, ux500_regulator_power_state_cnt_print,
111 inode->i_private);
114 static const struct file_operations ux500_regulator_power_state_cnt_fops = {
115 .open = ux500_regulator_power_state_cnt_open,
116 .read = seq_read,
117 .llseek = seq_lseek,
118 .release = single_release,
119 .owner = THIS_MODULE,
122 static int ux500_regulator_status_print(struct seq_file *s, void *p)
124 struct device *dev = s->private;
125 int err;
126 int i;
128 /* print dump header */
129 err = seq_printf(s, "ux500-regulator status:\n");
130 if (err < 0)
131 dev_err(dev, "seq_printf overflow\n");
133 err = seq_printf(s, "%31s : %8s : %8s\n", "current",
134 "before", "after");
135 if (err < 0)
136 dev_err(dev, "seq_printf overflow\n");
138 for (i = 0; i < rdebug.num_regulators; i++) {
139 struct dbx500_regulator_info *info;
140 /* Access per-regulator data */
141 info = &rdebug.regulator_array[i];
143 /* print status */
144 err = seq_printf(s, "%20s : %8s : %8s : %8s\n", info->desc.name,
145 info->is_enabled ? "enabled" : "disabled",
146 rdebug.state_before_suspend[i] ? "enabled" : "disabled",
147 rdebug.state_after_suspend[i] ? "enabled" : "disabled");
148 if (err < 0)
149 dev_err(dev, "seq_printf overflow\n");
152 return 0;
155 static int ux500_regulator_status_open(struct inode *inode, struct file *file)
157 return single_open(file, ux500_regulator_status_print,
158 inode->i_private);
161 static const struct file_operations ux500_regulator_status_fops = {
162 .open = ux500_regulator_status_open,
163 .read = seq_read,
164 .llseek = seq_lseek,
165 .release = single_release,
166 .owner = THIS_MODULE,
169 int __attribute__((weak)) dbx500_regulator_testcase(
170 struct dbx500_regulator_info *regulator_info,
171 int num_regulators)
173 return 0;
176 int __devinit
177 ux500_regulator_debug_init(struct platform_device *pdev,
178 struct dbx500_regulator_info *regulator_info,
179 int num_regulators)
181 /* create directory */
182 rdebug.dir = debugfs_create_dir("ux500-regulator", NULL);
183 if (!rdebug.dir)
184 goto exit_no_debugfs;
186 /* create "status" file */
187 rdebug.status_file = debugfs_create_file("status",
188 S_IRUGO, rdebug.dir, &pdev->dev,
189 &ux500_regulator_status_fops);
190 if (!rdebug.status_file)
191 goto exit_destroy_dir;
193 /* create "power-state-count" file */
194 rdebug.power_state_cnt_file = debugfs_create_file("power-state-count",
195 S_IRUGO, rdebug.dir, &pdev->dev,
196 &ux500_regulator_power_state_cnt_fops);
197 if (!rdebug.power_state_cnt_file)
198 goto exit_destroy_status;
200 rdebug.regulator_array = regulator_info;
201 rdebug.num_regulators = num_regulators;
203 rdebug.state_before_suspend = kzalloc(num_regulators, GFP_KERNEL);
204 if (!rdebug.state_before_suspend) {
205 dev_err(&pdev->dev,
206 "could not allocate memory for saving state\n");
207 goto exit_destroy_power_state;
210 rdebug.state_after_suspend = kzalloc(num_regulators, GFP_KERNEL);
211 if (!rdebug.state_after_suspend) {
212 dev_err(&pdev->dev,
213 "could not allocate memory for saving state\n");
214 goto exit_free;
217 dbx500_regulator_testcase(regulator_info, num_regulators);
218 return 0;
220 exit_free:
221 kfree(rdebug.state_before_suspend);
222 exit_destroy_power_state:
223 debugfs_remove(rdebug.power_state_cnt_file);
224 exit_destroy_status:
225 debugfs_remove(rdebug.status_file);
226 exit_destroy_dir:
227 debugfs_remove(rdebug.dir);
228 exit_no_debugfs:
229 dev_err(&pdev->dev, "failed to create debugfs entries.\n");
230 return -ENOMEM;
233 int __devexit ux500_regulator_debug_exit(void)
235 debugfs_remove_recursive(rdebug.dir);
236 kfree(rdebug.state_after_suspend);
237 kfree(rdebug.state_before_suspend);
239 return 0;
241 #endif