blob: ae0107fe4745fe0a822c45f885f92563fa3f1c11 [file] [log] [blame]
Daniel Lezcano7c15fad2016-02-18 15:57:43 +00001/*
2 * Powerdebug : power debugging tool
Amit Arora17552782010-12-02 12:23:14 +05303 *
Daniel Lezcano7c15fad2016-02-18 15:57:43 +00004 * Copyright (C) 2016, Linaro Limited.
Amit Arora17552782010-12-02 12:23:14 +05305 *
Daniel Lezcano7c15fad2016-02-18 15:57:43 +00006 * Author:
7 * Daniel Lezcano <daniel.lezcano@linaro.org>
Amit Arora17552782010-12-02 12:23:14 +05308 *
Daniel Lezcano7c15fad2016-02-18 15:57:43 +00009 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
Daniel Lezcano2e6ecca2011-03-26 22:05:51 +010023#define SYSFS_REGULATOR "/sys/class/regulator"
Daniel Lezcano15627482011-06-15 15:45:12 +020024#define VALUE_MAX 16
25
Daniel Lezcanob25be4a2011-06-15 15:45:12 +020026#define _GNU_SOURCE
27#include <stdio.h>
28#undef _GNU_SOURCE
29#include <sys/types.h>
30#include <stdbool.h>
31#include <dirent.h>
32#include <string.h>
33#include <stdlib.h>
Sanjay Singh Rawat03fdbc02014-05-27 16:52:27 +053034#include <unistd.h>
35
Daniel Lezcanob301b082011-06-15 15:45:12 +020036#include "display.h"
Daniel Lezcanob25be4a2011-06-15 15:45:12 +020037#include "powerdebug.h"
38#include "tree.h"
39#include "utils.h"
Daniel Lezcano2e6ecca2011-03-26 22:05:51 +010040
Daniel Lezcanob25be4a2011-06-15 15:45:12 +020041struct regulator_info {
42 char name[NAME_MAX];
43 char state[VALUE_MAX];
44 char status[VALUE_MAX];
45 char type[VALUE_MAX];
46 char opmode[VALUE_MAX];
47 int microvolts;
48 int min_microvolts;
49 int max_microvolts;
50 int microamps;
51 int min_microamps;
52 int max_microamps;
53 int requested_microamps;
54 int num_users;
55};
56
57struct regulator_data {
58 const char *name;
59 const char *ifmt;
60 const char *ofmt;
61 bool derefme;
62};
63
64static struct regulator_data regdata[] = {
65 { "name", "%s", "\tname: %s\n" },
66 { "status", "%s", "\tstatus: %s\n" },
67 { "state", "%s", "\tstate: %s\n" },
68 { "type", "%s", "\ttype: %s\n" },
Thara Gopinathce1acda2017-07-14 13:15:47 -040069 { "num_users", "%s", "\tnum_users: %d\n", true },
70 { "microvolts", "%s", "\tmicrovolts: %d\n", true },
71 { "max_microvolts", "%s", "\tmax_microvolts: %d\n", true },
72 { "min_microvolts", "%s", "\tmin_microvolts: %d\n", true },
Daniel Lezcanob25be4a2011-06-15 15:45:12 +020073};
74
75static struct tree *reg_tree;
76
77static struct regulator_info *regulator_alloc(void)
Amit Arora17552782010-12-02 12:23:14 +053078{
Daniel Lezcanob25be4a2011-06-15 15:45:12 +020079 struct regulator_info *regi;
Amit Arora17552782010-12-02 12:23:14 +053080
Daniel Lezcanob25be4a2011-06-15 15:45:12 +020081 regi = malloc(sizeof(*regi));
82 if (regi)
83 memset(regi, 0, sizeof(*regi));
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +010084
Daniel Lezcanob25be4a2011-06-15 15:45:12 +020085 return regi;
Amit Arora17552782010-12-02 12:23:14 +053086}
87
Daniel Lezcanob25be4a2011-06-15 15:45:12 +020088static int regulator_dump_cb(struct tree *tree, void *data)
Amit Arora17552782010-12-02 12:23:14 +053089{
90 int i;
Daniel Lezcanob25be4a2011-06-15 15:45:12 +020091 char buffer[NAME_MAX];
92 size_t nregdata = sizeof(regdata) / sizeof(regdata[0]);
Amit Arora17552782010-12-02 12:23:14 +053093
Daniel Lezcanob25be4a2011-06-15 15:45:12 +020094 if (!strncmp("regulator.", tree->name, strlen("regulator.")))
95 printf("\n%s:\n", tree->name);
96
97 for (i = 0; i < nregdata; i++) {
Sanjay Singh Rawat9fe0c052013-02-22 17:57:18 +053098 int val;
Daniel Lezcanob25be4a2011-06-15 15:45:12 +020099
100 if (file_read_value(tree->path, regdata[i].name,
101 regdata[i].ifmt, buffer))
102 continue;
103
Sanjay Singh Rawat9fe0c052013-02-22 17:57:18 +0530104 if (regdata[i].derefme) {
105 val = atoi(buffer);
106 printf(regdata[i].ofmt, val);
107 } else
108 printf(regdata[i].ofmt, buffer);
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200109 }
110
111 return 0;
112}
113
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200114static int regulator_display_cb(struct tree *t, void *data)
Amit Arora17552782010-12-02 12:23:14 +0530115{
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200116 struct regulator_info *reg = t->private;
117 int *line = data;
118 char *buf;
Amit Arora17552782010-12-02 12:23:14 +0530119
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200120 /* we skip the root node of the tree */
121 if (!t->parent)
122 return 0;
Amit Arora17552782010-12-02 12:23:14 +0530123
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200124 if (!strlen(reg->name))
125 return 0;
Amit Arora17552782010-12-02 12:23:14 +0530126
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200127 if (asprintf(&buf, "%-11s %-11s %-11s %-11s %-11d %-11d %-11d %-12d",
128 reg->name, reg->status, reg->state, reg->type,
129 reg->num_users, reg->microvolts, reg->min_microvolts,
130 reg->max_microvolts) < 0)
131 return -1;
Amit Arora17552782010-12-02 12:23:14 +0530132
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200133 display_print_line(REGULATOR, *line, buf, reg->num_users, t);
134
135 (*line)++;
136
137 free(buf);
138
139 return 0;
Amit Arora17552782010-12-02 12:23:14 +0530140}
141
Daniel Lezcanofa453332011-06-21 00:57:08 +0200142static int regulator_print_header(void)
143{
144 char *buf;
145 int ret;
146
147 if (asprintf(&buf, "%-11s %-11s %-11s %-11s %-11s %-11s %-11s %-12s",
148 "Name", "Status", "State", "Type", "Users", "Microvolts",
149 "Min u-volts", "Max u-volts") < 0)
150 return -1;
151
Daniel Lezcano372ffba2011-06-21 00:57:08 +0200152 ret = display_column_name(buf);
Daniel Lezcanofa453332011-06-21 00:57:08 +0200153
154 free(buf);
155
156 return ret;
157
158}
159
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200160static int regulator_filter_cb(const char *name)
161{
162 /* let's ignore some directories in order to avoid to be
163 * pulled inside the sysfs circular symlinks mess/hell
164 * (choose the word which fit better)
165 */
166 if (!strcmp(name, "device"))
167 return 1;
168
169 if (!strcmp(name, "subsystem"))
170 return 1;
171
172 if (!strcmp(name, "driver"))
173 return 1;
174
175 return 0;
176}
177
178static inline int read_regulator_cb(struct tree *t, void *data)
179{
180 struct regulator_info *reg = t->private;
181
182 file_read_value(t->path, "name", "%s", reg->name);
183 file_read_value(t->path, "state", "%s", reg->state);
184 file_read_value(t->path, "status", "%s", reg->status);
185 file_read_value(t->path, "type", "%s", reg->type);
186 file_read_value(t->path, "opmode", "%s", reg->opmode);
187 file_read_value(t->path, "num_users", "%d", &reg->num_users);
188 file_read_value(t->path, "microvolts", "%d", &reg->microvolts);
189 file_read_value(t->path, "min_microvolts", "%d", &reg->min_microvolts);
190 file_read_value(t->path, "max_microvolts", "%d", &reg->max_microvolts);
191 file_read_value(t->path, "microamps", "%d", &reg->microamps);
192 file_read_value(t->path, "min_microamps", "%d", &reg->min_microamps);
193 file_read_value(t->path, "max_microamps", "%d", &reg->max_microamps);
194
195 return 0;
196}
197
Shaojie Sunc1462e72013-08-20 20:35:15 +0800198static int read_regulator_info(struct tree *tree)
199{
200 return tree_for_each(tree, read_regulator_cb, NULL);
201}
202
203static int regulator_print_info(struct tree *tree)
204{
205 int ret, line = 0;
206
207 display_reset_cursor(REGULATOR);
208
209 regulator_print_header();
210
211 ret = tree_for_each(tree, regulator_display_cb, &line);
212
213 display_refresh_pad(REGULATOR);
214
215 return ret;
216}
217
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200218static int fill_regulator_cb(struct tree *t, void *data)
219{
220 struct regulator_info *reg;
221
222 reg = regulator_alloc();
Sanjay Singh Rawat5fef0052013-04-17 15:02:01 +0530223 if (!reg) {
224 printf("error: unable to allocate memory for regulator\n");
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200225 return -1;
Sanjay Singh Rawat5fef0052013-04-17 15:02:01 +0530226 }
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200227 t->private = reg;
228
229 /* we skip the root node but we set it expanded for its children */
230 if (!t->parent)
231 return 0;
232
233 return read_regulator_cb(t, data);
234}
235
236static int fill_regulator_tree(void)
237{
238 return tree_for_each(reg_tree, fill_regulator_cb, NULL);
239}
240
Daniel Lezcanod42d7ad2016-02-22 15:02:57 +0100241static int regulator_info_load(void)
242{
243 if (reg_tree)
244 return 0;
245
246 reg_tree = tree_load(SYSFS_REGULATOR, regulator_filter_cb, false);
247 if (!reg_tree)
248 return -1;
249
250 if (fill_regulator_tree())
251 return -1;
252
253 return 0;
254}
255
Thara Gopinathd78818c2017-07-14 13:15:45 -0400256int regulator_dump(void)
257{
258
259 if (!reg_tree) {
260 reg_tree = tree_load(SYSFS_REGULATOR, regulator_filter_cb,
261 false);
262 if (!reg_tree) {
263 printf("Failed to load regulator tree\n");
264 return -1;
265 }
266 }
267
268 printf("\nRegulator Information:\n");
269 printf("*********************\n\n");
270
271 return tree_for_each(reg_tree, regulator_dump_cb, NULL);
272}
273
Daniel Lezcanod42d7ad2016-02-22 15:02:57 +0100274static int regulator_display(bool refresh)
275{
276 if (regulator_info_load()) {
277 display_print_error(REGULATOR, 0, "Failed to read regulator info");
278 return 0; /* we don't want this to be a critical error */
279 }
280
281 if (refresh && read_regulator_info(reg_tree))
282 return -1;
283
284 return regulator_print_info(reg_tree);
285}
286
Daniel Lezcanob301b082011-06-15 15:45:12 +0200287static struct display_ops regulator_ops = {
288 .display = regulator_display,
289};
290
Daniel Lezcanob4eec7e2016-02-18 16:44:55 +0000291int regulator_init(struct powerdebug_options *options)
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200292{
Daniel Lezcanob4eec7e2016-02-18 16:44:55 +0000293 if (!(options->flags & REGULATOR_OPTION))
294 return 0;
295
Daniel Lezcano1eedd482016-02-22 11:05:51 +0000296 return display_register(REGULATOR, &regulator_ops);
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200297}