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>
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)
31 spin_lock_irqsave(&power_state_active_lock
, flags
);
32 cnt
= power_state_active_cnt
;
33 spin_unlock_irqrestore(&power_state_active_lock
, flags
);
38 void power_state_active_enable(void)
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)
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");
59 power_state_active_cnt
--;
61 spin_unlock_irqrestore(&power_state_active_lock
, flags
);
65 #ifdef CONFIG_REGULATOR_DEBUG
67 static struct ux500_regulator_debug
{
69 struct dentry
*status_file
;
70 struct dentry
*power_state_cnt_file
;
71 struct dbx500_regulator_info
*regulator_array
;
73 u8
*state_before_suspend
;
74 u8
*state_after_suspend
;
77 void ux500_regulator_suspend_debug(void)
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)
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;
98 /* print power state count */
99 err
= seq_printf(s
, "ux500-regulator power state count: %i\n",
100 power_state_active_get());
102 dev_err(dev
, "seq_printf overflow\n");
107 static int ux500_regulator_power_state_cnt_open(struct inode
*inode
,
110 return single_open(file
, ux500_regulator_power_state_cnt_print
,
114 static const struct file_operations ux500_regulator_power_state_cnt_fops
= {
115 .open
= ux500_regulator_power_state_cnt_open
,
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;
128 /* print dump header */
129 err
= seq_printf(s
, "ux500-regulator status:\n");
131 dev_err(dev
, "seq_printf overflow\n");
133 err
= seq_printf(s
, "%31s : %8s : %8s\n", "current",
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
];
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");
149 dev_err(dev
, "seq_printf overflow\n");
155 static int ux500_regulator_status_open(struct inode
*inode
, struct file
*file
)
157 return single_open(file
, ux500_regulator_status_print
,
161 static const struct file_operations ux500_regulator_status_fops
= {
162 .open
= ux500_regulator_status_open
,
165 .release
= single_release
,
166 .owner
= THIS_MODULE
,
169 int __attribute__((weak
)) dbx500_regulator_testcase(
170 struct dbx500_regulator_info
*regulator_info
,
177 ux500_regulator_debug_init(struct platform_device
*pdev
,
178 struct dbx500_regulator_info
*regulator_info
,
181 /* create directory */
182 rdebug
.dir
= debugfs_create_dir("ux500-regulator", NULL
);
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
) {
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
) {
213 "could not allocate memory for saving state\n");
217 dbx500_regulator_testcase(regulator_info
, num_regulators
);
221 kfree(rdebug
.state_before_suspend
);
222 exit_destroy_power_state
:
223 debugfs_remove(rdebug
.power_state_cnt_file
);
225 debugfs_remove(rdebug
.status_file
);
227 debugfs_remove(rdebug
.dir
);
229 dev_err(&pdev
->dev
, "failed to create debugfs entries.\n");
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
);