1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) ST-Ericsson SA 2010
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Authors: Sundar Iyer <sundar.iyer@stericsson.com> for ST-Ericsson
6*4882a593Smuzhiyun * Bengt Jonsson <bengt.g.jonsson@stericsson.com> for ST-Ericsson
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * UX500 common part of Power domain regulators
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/kernel.h>
12*4882a593Smuzhiyun #include <linux/err.h>
13*4882a593Smuzhiyun #include <linux/regulator/driver.h>
14*4882a593Smuzhiyun #include <linux/debugfs.h>
15*4882a593Smuzhiyun #include <linux/seq_file.h>
16*4882a593Smuzhiyun #include <linux/slab.h>
17*4882a593Smuzhiyun #include <linux/module.h>
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #include "dbx500-prcmu.h"
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun /*
22*4882a593Smuzhiyun * power state reference count
23*4882a593Smuzhiyun */
24*4882a593Smuzhiyun static int power_state_active_cnt; /* will initialize to zero */
25*4882a593Smuzhiyun static DEFINE_SPINLOCK(power_state_active_lock);
26*4882a593Smuzhiyun
power_state_active_enable(void)27*4882a593Smuzhiyun void power_state_active_enable(void)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun unsigned long flags;
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun spin_lock_irqsave(&power_state_active_lock, flags);
32*4882a593Smuzhiyun power_state_active_cnt++;
33*4882a593Smuzhiyun spin_unlock_irqrestore(&power_state_active_lock, flags);
34*4882a593Smuzhiyun }
35*4882a593Smuzhiyun
power_state_active_disable(void)36*4882a593Smuzhiyun int power_state_active_disable(void)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun int ret = 0;
39*4882a593Smuzhiyun unsigned long flags;
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun spin_lock_irqsave(&power_state_active_lock, flags);
42*4882a593Smuzhiyun if (power_state_active_cnt <= 0) {
43*4882a593Smuzhiyun pr_err("power state: unbalanced enable/disable calls\n");
44*4882a593Smuzhiyun ret = -EINVAL;
45*4882a593Smuzhiyun goto out;
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun power_state_active_cnt--;
49*4882a593Smuzhiyun out:
50*4882a593Smuzhiyun spin_unlock_irqrestore(&power_state_active_lock, flags);
51*4882a593Smuzhiyun return ret;
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun #ifdef CONFIG_REGULATOR_DEBUG
55*4882a593Smuzhiyun
power_state_active_get(void)56*4882a593Smuzhiyun static int power_state_active_get(void)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun unsigned long flags;
59*4882a593Smuzhiyun int cnt;
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun spin_lock_irqsave(&power_state_active_lock, flags);
62*4882a593Smuzhiyun cnt = power_state_active_cnt;
63*4882a593Smuzhiyun spin_unlock_irqrestore(&power_state_active_lock, flags);
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun return cnt;
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun static struct ux500_regulator_debug {
69*4882a593Smuzhiyun struct dentry *dir;
70*4882a593Smuzhiyun struct dbx500_regulator_info *regulator_array;
71*4882a593Smuzhiyun int num_regulators;
72*4882a593Smuzhiyun u8 *state_before_suspend;
73*4882a593Smuzhiyun u8 *state_after_suspend;
74*4882a593Smuzhiyun } rdebug;
75*4882a593Smuzhiyun
ux500_regulator_power_state_cnt_show(struct seq_file * s,void * p)76*4882a593Smuzhiyun static int ux500_regulator_power_state_cnt_show(struct seq_file *s, void *p)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun /* print power state count */
79*4882a593Smuzhiyun seq_printf(s, "ux500-regulator power state count: %i\n",
80*4882a593Smuzhiyun power_state_active_get());
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun return 0;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun DEFINE_SHOW_ATTRIBUTE(ux500_regulator_power_state_cnt);
85*4882a593Smuzhiyun
ux500_regulator_status_show(struct seq_file * s,void * p)86*4882a593Smuzhiyun static int ux500_regulator_status_show(struct seq_file *s, void *p)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun int i;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun /* print dump header */
91*4882a593Smuzhiyun seq_puts(s, "ux500-regulator status:\n");
92*4882a593Smuzhiyun seq_printf(s, "%31s : %8s : %8s\n", "current", "before", "after");
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun for (i = 0; i < rdebug.num_regulators; i++) {
95*4882a593Smuzhiyun struct dbx500_regulator_info *info;
96*4882a593Smuzhiyun /* Access per-regulator data */
97*4882a593Smuzhiyun info = &rdebug.regulator_array[i];
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun /* print status */
100*4882a593Smuzhiyun seq_printf(s, "%20s : %8s : %8s : %8s\n",
101*4882a593Smuzhiyun info->desc.name,
102*4882a593Smuzhiyun info->is_enabled ? "enabled" : "disabled",
103*4882a593Smuzhiyun rdebug.state_before_suspend[i] ? "enabled" : "disabled",
104*4882a593Smuzhiyun rdebug.state_after_suspend[i] ? "enabled" : "disabled");
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun return 0;
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun DEFINE_SHOW_ATTRIBUTE(ux500_regulator_status);
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun int
ux500_regulator_debug_init(struct platform_device * pdev,struct dbx500_regulator_info * regulator_info,int num_regulators)112*4882a593Smuzhiyun ux500_regulator_debug_init(struct platform_device *pdev,
113*4882a593Smuzhiyun struct dbx500_regulator_info *regulator_info,
114*4882a593Smuzhiyun int num_regulators)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun /* create directory */
117*4882a593Smuzhiyun rdebug.dir = debugfs_create_dir("ux500-regulator", NULL);
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /* create "status" file */
120*4882a593Smuzhiyun debugfs_create_file("status", S_IRUGO, rdebug.dir, &pdev->dev,
121*4882a593Smuzhiyun &ux500_regulator_status_fops);
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun /* create "power-state-count" file */
124*4882a593Smuzhiyun debugfs_create_file("power-state-count", S_IRUGO, rdebug.dir,
125*4882a593Smuzhiyun &pdev->dev, &ux500_regulator_power_state_cnt_fops);
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun rdebug.regulator_array = regulator_info;
128*4882a593Smuzhiyun rdebug.num_regulators = num_regulators;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun rdebug.state_before_suspend = kzalloc(num_regulators, GFP_KERNEL);
131*4882a593Smuzhiyun if (!rdebug.state_before_suspend)
132*4882a593Smuzhiyun goto exit_destroy_power_state;
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun rdebug.state_after_suspend = kzalloc(num_regulators, GFP_KERNEL);
135*4882a593Smuzhiyun if (!rdebug.state_after_suspend)
136*4882a593Smuzhiyun goto exit_free;
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun return 0;
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun exit_free:
141*4882a593Smuzhiyun kfree(rdebug.state_before_suspend);
142*4882a593Smuzhiyun exit_destroy_power_state:
143*4882a593Smuzhiyun debugfs_remove_recursive(rdebug.dir);
144*4882a593Smuzhiyun return -ENOMEM;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun
ux500_regulator_debug_exit(void)147*4882a593Smuzhiyun int ux500_regulator_debug_exit(void)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun debugfs_remove_recursive(rdebug.dir);
150*4882a593Smuzhiyun kfree(rdebug.state_after_suspend);
151*4882a593Smuzhiyun kfree(rdebug.state_before_suspend);
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun return 0;
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun #endif
156