blob: 2d16b9f16de73f5ce42ffb2370bba98c9bccf20b [file] [log] [blame]
Bengt Jonsson38e96832012-01-13 16:30:31 +01001/*
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
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>
Steven Rostedt7f46d0f2013-01-24 10:29:26 -050017#include <linux/module.h>
Bengt Jonsson38e96832012-01-13 16:30:31 +010018
19#include "dbx500-prcmu.h"
20
21/*
22 * power state reference count
23 */
24static int power_state_active_cnt; /* will initialize to zero */
25static DEFINE_SPINLOCK(power_state_active_lock);
26
Bengt Jonsson38e96832012-01-13 16:30:31 +010027void power_state_active_enable(void)
28{
29 unsigned long flags;
30
31 spin_lock_irqsave(&power_state_active_lock, flags);
32 power_state_active_cnt++;
33 spin_unlock_irqrestore(&power_state_active_lock, flags);
34}
35
36int power_state_active_disable(void)
37{
38 int ret = 0;
39 unsigned long flags;
40
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");
44 ret = -EINVAL;
45 goto out;
46 }
47
48 power_state_active_cnt--;
49out:
50 spin_unlock_irqrestore(&power_state_active_lock, flags);
51 return ret;
52}
53
54#ifdef CONFIG_REGULATOR_DEBUG
55
Sachin Kamat3d750952013-05-08 16:35:10 +053056static int power_state_active_get(void)
57{
58 unsigned long flags;
59 int cnt;
60
61 spin_lock_irqsave(&power_state_active_lock, flags);
62 cnt = power_state_active_cnt;
63 spin_unlock_irqrestore(&power_state_active_lock, flags);
64
65 return cnt;
66}
67
Bengt Jonsson38e96832012-01-13 16:30:31 +010068static struct ux500_regulator_debug {
69 struct dentry *dir;
70 struct dentry *status_file;
71 struct dentry *power_state_cnt_file;
72 struct dbx500_regulator_info *regulator_array;
73 int num_regulators;
74 u8 *state_before_suspend;
75 u8 *state_after_suspend;
76} rdebug;
77
78void ux500_regulator_suspend_debug(void)
79{
80 int i;
Sachin Kamate77addc2014-03-18 15:53:01 +053081
Bengt Jonsson38e96832012-01-13 16:30:31 +010082 for (i = 0; i < rdebug.num_regulators; i++)
83 rdebug.state_before_suspend[i] =
84 rdebug.regulator_array[i].is_enabled;
85}
86
87void ux500_regulator_resume_debug(void)
88{
89 int i;
Sachin Kamate77addc2014-03-18 15:53:01 +053090
Bengt Jonsson38e96832012-01-13 16:30:31 +010091 for (i = 0; i < rdebug.num_regulators; i++)
92 rdebug.state_after_suspend[i] =
93 rdebug.regulator_array[i].is_enabled;
94}
95
96static int ux500_regulator_power_state_cnt_print(struct seq_file *s, void *p)
97{
98 struct device *dev = s->private;
99 int err;
100
101 /* print power state count */
102 err = seq_printf(s, "ux500-regulator power state count: %i\n",
103 power_state_active_get());
104 if (err < 0)
105 dev_err(dev, "seq_printf overflow\n");
106
107 return 0;
108}
109
110static int ux500_regulator_power_state_cnt_open(struct inode *inode,
111 struct file *file)
112{
113 return single_open(file, ux500_regulator_power_state_cnt_print,
114 inode->i_private);
115}
116
117static const struct file_operations ux500_regulator_power_state_cnt_fops = {
118 .open = ux500_regulator_power_state_cnt_open,
119 .read = seq_read,
120 .llseek = seq_lseek,
121 .release = single_release,
122 .owner = THIS_MODULE,
123};
124
125static int ux500_regulator_status_print(struct seq_file *s, void *p)
126{
127 struct device *dev = s->private;
128 int err;
129 int i;
130
131 /* print dump header */
Jingoo Han80c48e72014-03-19 09:26:17 +0900132 err = seq_puts(s, "ux500-regulator status:\n");
Bengt Jonsson38e96832012-01-13 16:30:31 +0100133 if (err < 0)
Jingoo Han80c48e72014-03-19 09:26:17 +0900134 dev_err(dev, "seq_puts overflow\n");
Bengt Jonsson38e96832012-01-13 16:30:31 +0100135
136 err = seq_printf(s, "%31s : %8s : %8s\n", "current",
137 "before", "after");
138 if (err < 0)
139 dev_err(dev, "seq_printf overflow\n");
140
141 for (i = 0; i < rdebug.num_regulators; i++) {
142 struct dbx500_regulator_info *info;
143 /* Access per-regulator data */
144 info = &rdebug.regulator_array[i];
145
146 /* print status */
147 err = seq_printf(s, "%20s : %8s : %8s : %8s\n", info->desc.name,
148 info->is_enabled ? "enabled" : "disabled",
149 rdebug.state_before_suspend[i] ? "enabled" : "disabled",
150 rdebug.state_after_suspend[i] ? "enabled" : "disabled");
151 if (err < 0)
152 dev_err(dev, "seq_printf overflow\n");
153 }
154
155 return 0;
156}
157
158static int ux500_regulator_status_open(struct inode *inode, struct file *file)
159{
160 return single_open(file, ux500_regulator_status_print,
161 inode->i_private);
162}
163
164static const struct file_operations ux500_regulator_status_fops = {
165 .open = ux500_regulator_status_open,
166 .read = seq_read,
167 .llseek = seq_lseek,
168 .release = single_release,
169 .owner = THIS_MODULE,
170};
171
172int __attribute__((weak)) dbx500_regulator_testcase(
173 struct dbx500_regulator_info *regulator_info,
174 int num_regulators)
175{
176 return 0;
177}
178
Bill Pembertona5023572012-11-19 13:22:22 -0500179int
Bengt Jonsson38e96832012-01-13 16:30:31 +0100180ux500_regulator_debug_init(struct platform_device *pdev,
181 struct dbx500_regulator_info *regulator_info,
182 int num_regulators)
183{
184 /* create directory */
185 rdebug.dir = debugfs_create_dir("ux500-regulator", NULL);
186 if (!rdebug.dir)
187 goto exit_no_debugfs;
188
189 /* create "status" file */
190 rdebug.status_file = debugfs_create_file("status",
191 S_IRUGO, rdebug.dir, &pdev->dev,
192 &ux500_regulator_status_fops);
193 if (!rdebug.status_file)
194 goto exit_destroy_dir;
195
196 /* create "power-state-count" file */
197 rdebug.power_state_cnt_file = debugfs_create_file("power-state-count",
198 S_IRUGO, rdebug.dir, &pdev->dev,
199 &ux500_regulator_power_state_cnt_fops);
200 if (!rdebug.power_state_cnt_file)
201 goto exit_destroy_status;
202
203 rdebug.regulator_array = regulator_info;
204 rdebug.num_regulators = num_regulators;
205
206 rdebug.state_before_suspend = kzalloc(num_regulators, GFP_KERNEL);
Sachin Kamatcb487c52014-02-20 14:23:03 +0530207 if (!rdebug.state_before_suspend)
Bengt Jonsson38e96832012-01-13 16:30:31 +0100208 goto exit_destroy_power_state;
Bengt Jonsson38e96832012-01-13 16:30:31 +0100209
210 rdebug.state_after_suspend = kzalloc(num_regulators, GFP_KERNEL);
Sachin Kamatcb487c52014-02-20 14:23:03 +0530211 if (!rdebug.state_after_suspend)
Bengt Jonsson38e96832012-01-13 16:30:31 +0100212 goto exit_free;
Bengt Jonsson38e96832012-01-13 16:30:31 +0100213
214 dbx500_regulator_testcase(regulator_info, num_regulators);
215 return 0;
216
217exit_free:
218 kfree(rdebug.state_before_suspend);
219exit_destroy_power_state:
220 debugfs_remove(rdebug.power_state_cnt_file);
221exit_destroy_status:
222 debugfs_remove(rdebug.status_file);
223exit_destroy_dir:
224 debugfs_remove(rdebug.dir);
225exit_no_debugfs:
226 dev_err(&pdev->dev, "failed to create debugfs entries.\n");
227 return -ENOMEM;
228}
229
Bill Pemberton8dc995f2012-11-19 13:26:10 -0500230int ux500_regulator_debug_exit(void)
Bengt Jonsson38e96832012-01-13 16:30:31 +0100231{
232 debugfs_remove_recursive(rdebug.dir);
233 kfree(rdebug.state_after_suspend);
234 kfree(rdebug.state_before_suspend);
235
236 return 0;
237}
238#endif