blob: a45c1e1ac7ef66180fc57ec6d37ae48063771604 [file] [log] [blame]
Thomas Gleixner03761482019-05-28 09:57:24 -07001// SPDX-License-Identifier: GPL-2.0-only
Bengt Jonsson38e96832012-01-13 16:30:31 +01002/*
3 * Copyright (C) ST-Ericsson SA 2010
4 *
Bengt Jonsson38e96832012-01-13 16:30:31 +01005 * 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;
Bengt Jonsson38e96832012-01-13 16:30:31 +010070 struct dbx500_regulator_info *regulator_array;
71 int num_regulators;
72 u8 *state_before_suspend;
73 u8 *state_after_suspend;
74} rdebug;
75
Yangtao Li3e60b4f2018-12-15 03:21:48 -050076static int ux500_regulator_power_state_cnt_show(struct seq_file *s, void *p)
Bengt Jonsson38e96832012-01-13 16:30:31 +010077{
Bengt Jonsson38e96832012-01-13 16:30:31 +010078 /* print power state count */
Joe Perchesaf781142015-02-21 18:53:54 -080079 seq_printf(s, "ux500-regulator power state count: %i\n",
80 power_state_active_get());
Bengt Jonsson38e96832012-01-13 16:30:31 +010081
82 return 0;
83}
Yangtao Li3e60b4f2018-12-15 03:21:48 -050084DEFINE_SHOW_ATTRIBUTE(ux500_regulator_power_state_cnt);
Bengt Jonsson38e96832012-01-13 16:30:31 +010085
Yangtao Li3e60b4f2018-12-15 03:21:48 -050086static int ux500_regulator_status_show(struct seq_file *s, void *p)
Bengt Jonsson38e96832012-01-13 16:30:31 +010087{
Bengt Jonsson38e96832012-01-13 16:30:31 +010088 int i;
89
90 /* print dump header */
Joe Perchesaf781142015-02-21 18:53:54 -080091 seq_puts(s, "ux500-regulator status:\n");
92 seq_printf(s, "%31s : %8s : %8s\n", "current", "before", "after");
Bengt Jonsson38e96832012-01-13 16:30:31 +010093
94 for (i = 0; i < rdebug.num_regulators; i++) {
95 struct dbx500_regulator_info *info;
96 /* Access per-regulator data */
97 info = &rdebug.regulator_array[i];
98
99 /* print status */
Joe Perchesaf781142015-02-21 18:53:54 -0800100 seq_printf(s, "%20s : %8s : %8s : %8s\n",
101 info->desc.name,
102 info->is_enabled ? "enabled" : "disabled",
103 rdebug.state_before_suspend[i] ? "enabled" : "disabled",
104 rdebug.state_after_suspend[i] ? "enabled" : "disabled");
Bengt Jonsson38e96832012-01-13 16:30:31 +0100105 }
106
107 return 0;
108}
Yangtao Li3e60b4f2018-12-15 03:21:48 -0500109DEFINE_SHOW_ATTRIBUTE(ux500_regulator_status);
Bengt Jonsson38e96832012-01-13 16:30:31 +0100110
Bill Pembertona5023572012-11-19 13:22:22 -0500111int
Bengt Jonsson38e96832012-01-13 16:30:31 +0100112ux500_regulator_debug_init(struct platform_device *pdev,
113 struct dbx500_regulator_info *regulator_info,
114 int num_regulators)
115{
116 /* create directory */
117 rdebug.dir = debugfs_create_dir("ux500-regulator", NULL);
Bengt Jonsson38e96832012-01-13 16:30:31 +0100118
119 /* create "status" file */
Jinchao Wang894cda542021-06-26 18:24:54 +0800120 debugfs_create_file("status", 0444, rdebug.dir, &pdev->dev,
Greg Kroah-Hartman8bdaa432020-08-18 15:37:01 +0200121 &ux500_regulator_status_fops);
Bengt Jonsson38e96832012-01-13 16:30:31 +0100122
123 /* create "power-state-count" file */
Jinchao Wang894cda542021-06-26 18:24:54 +0800124 debugfs_create_file("power-state-count", 0444, rdebug.dir,
Greg Kroah-Hartman8bdaa432020-08-18 15:37:01 +0200125 &pdev->dev, &ux500_regulator_power_state_cnt_fops);
Bengt Jonsson38e96832012-01-13 16:30:31 +0100126
127 rdebug.regulator_array = regulator_info;
128 rdebug.num_regulators = num_regulators;
129
130 rdebug.state_before_suspend = kzalloc(num_regulators, GFP_KERNEL);
Sachin Kamatcb487c52014-02-20 14:23:03 +0530131 if (!rdebug.state_before_suspend)
Bengt Jonsson38e96832012-01-13 16:30:31 +0100132 goto exit_destroy_power_state;
Bengt Jonsson38e96832012-01-13 16:30:31 +0100133
134 rdebug.state_after_suspend = kzalloc(num_regulators, GFP_KERNEL);
Sachin Kamatcb487c52014-02-20 14:23:03 +0530135 if (!rdebug.state_after_suspend)
Bengt Jonsson38e96832012-01-13 16:30:31 +0100136 goto exit_free;
Bengt Jonsson38e96832012-01-13 16:30:31 +0100137
Bengt Jonsson38e96832012-01-13 16:30:31 +0100138 return 0;
139
140exit_free:
141 kfree(rdebug.state_before_suspend);
142exit_destroy_power_state:
Greg Kroah-Hartman8bdaa432020-08-18 15:37:01 +0200143 debugfs_remove_recursive(rdebug.dir);
Bengt Jonsson38e96832012-01-13 16:30:31 +0100144 return -ENOMEM;
145}
146
Bill Pemberton8dc995f2012-11-19 13:26:10 -0500147int ux500_regulator_debug_exit(void)
Bengt Jonsson38e96832012-01-13 16:30:31 +0100148{
149 debugfs_remove_recursive(rdebug.dir);
150 kfree(rdebug.state_after_suspend);
151 kfree(rdebug.state_before_suspend);
152
153 return 0;
154}
155#endif