blob: 5fba6f1644007f83b62b18c196984045f09a3711 [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" },
69 { "num_users", "%d", "\tnum_users: %d\n", true },
70 { "microvolts", "%d", "\tmicrovolts: %d\n", true },
71 { "max_microvolts", "%d", "\tmax_microvolts: %d\n", true },
72 { "min_microvolts", "%d", "\tmin_microvolts: %d\n", true },
73};
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
114int regulator_dump(void)
115{
Amit Arora422c52f2010-12-02 16:22:29 +0530116 printf("\nRegulator Information:\n");
117 printf("*********************\n\n");
118
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200119 return tree_for_each(reg_tree, regulator_dump_cb, NULL);
Amit Arora17552782010-12-02 12:23:14 +0530120}
121
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200122static int regulator_display_cb(struct tree *t, void *data)
Amit Arora17552782010-12-02 12:23:14 +0530123{
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200124 struct regulator_info *reg = t->private;
125 int *line = data;
126 char *buf;
Amit Arora17552782010-12-02 12:23:14 +0530127
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200128 /* we skip the root node of the tree */
129 if (!t->parent)
130 return 0;
Amit Arora17552782010-12-02 12:23:14 +0530131
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200132 if (!strlen(reg->name))
133 return 0;
Amit Arora17552782010-12-02 12:23:14 +0530134
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200135 if (asprintf(&buf, "%-11s %-11s %-11s %-11s %-11d %-11d %-11d %-12d",
136 reg->name, reg->status, reg->state, reg->type,
137 reg->num_users, reg->microvolts, reg->min_microvolts,
138 reg->max_microvolts) < 0)
139 return -1;
Amit Arora17552782010-12-02 12:23:14 +0530140
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200141 display_print_line(REGULATOR, *line, buf, reg->num_users, t);
142
143 (*line)++;
144
145 free(buf);
146
147 return 0;
Amit Arora17552782010-12-02 12:23:14 +0530148}
149
Daniel Lezcanofa453332011-06-21 00:57:08 +0200150static int regulator_print_header(void)
151{
152 char *buf;
153 int ret;
154
155 if (asprintf(&buf, "%-11s %-11s %-11s %-11s %-11s %-11s %-11s %-12s",
156 "Name", "Status", "State", "Type", "Users", "Microvolts",
157 "Min u-volts", "Max u-volts") < 0)
158 return -1;
159
Daniel Lezcano372ffba2011-06-21 00:57:08 +0200160 ret = display_column_name(buf);
Daniel Lezcanofa453332011-06-21 00:57:08 +0200161
162 free(buf);
163
164 return ret;
165
166}
167
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200168static int regulator_filter_cb(const char *name)
169{
170 /* let's ignore some directories in order to avoid to be
171 * pulled inside the sysfs circular symlinks mess/hell
172 * (choose the word which fit better)
173 */
174 if (!strcmp(name, "device"))
175 return 1;
176
177 if (!strcmp(name, "subsystem"))
178 return 1;
179
180 if (!strcmp(name, "driver"))
181 return 1;
182
183 return 0;
184}
185
186static inline int read_regulator_cb(struct tree *t, void *data)
187{
188 struct regulator_info *reg = t->private;
189
190 file_read_value(t->path, "name", "%s", reg->name);
191 file_read_value(t->path, "state", "%s", reg->state);
192 file_read_value(t->path, "status", "%s", reg->status);
193 file_read_value(t->path, "type", "%s", reg->type);
194 file_read_value(t->path, "opmode", "%s", reg->opmode);
195 file_read_value(t->path, "num_users", "%d", &reg->num_users);
196 file_read_value(t->path, "microvolts", "%d", &reg->microvolts);
197 file_read_value(t->path, "min_microvolts", "%d", &reg->min_microvolts);
198 file_read_value(t->path, "max_microvolts", "%d", &reg->max_microvolts);
199 file_read_value(t->path, "microamps", "%d", &reg->microamps);
200 file_read_value(t->path, "min_microamps", "%d", &reg->min_microamps);
201 file_read_value(t->path, "max_microamps", "%d", &reg->max_microamps);
202
203 return 0;
204}
205
Shaojie Sunc1462e72013-08-20 20:35:15 +0800206static int read_regulator_info(struct tree *tree)
207{
208 return tree_for_each(tree, read_regulator_cb, NULL);
209}
210
211static int regulator_print_info(struct tree *tree)
212{
213 int ret, line = 0;
214
215 display_reset_cursor(REGULATOR);
216
217 regulator_print_header();
218
219 ret = tree_for_each(tree, regulator_display_cb, &line);
220
221 display_refresh_pad(REGULATOR);
222
223 return ret;
224}
225
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200226static int fill_regulator_cb(struct tree *t, void *data)
227{
228 struct regulator_info *reg;
229
230 reg = regulator_alloc();
Sanjay Singh Rawat5fef0052013-04-17 15:02:01 +0530231 if (!reg) {
232 printf("error: unable to allocate memory for regulator\n");
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200233 return -1;
Sanjay Singh Rawat5fef0052013-04-17 15:02:01 +0530234 }
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200235 t->private = reg;
236
237 /* we skip the root node but we set it expanded for its children */
238 if (!t->parent)
239 return 0;
240
241 return read_regulator_cb(t, data);
242}
243
244static int fill_regulator_tree(void)
245{
246 return tree_for_each(reg_tree, fill_regulator_cb, NULL);
247}
248
Daniel Lezcanod42d7ad2016-02-22 15:02:57 +0100249static int regulator_info_load(void)
250{
251 if (reg_tree)
252 return 0;
253
254 reg_tree = tree_load(SYSFS_REGULATOR, regulator_filter_cb, false);
255 if (!reg_tree)
256 return -1;
257
258 if (fill_regulator_tree())
259 return -1;
260
261 return 0;
262}
263
264static int regulator_display(bool refresh)
265{
266 if (regulator_info_load()) {
267 display_print_error(REGULATOR, 0, "Failed to read regulator info");
268 return 0; /* we don't want this to be a critical error */
269 }
270
271 if (refresh && read_regulator_info(reg_tree))
272 return -1;
273
274 return regulator_print_info(reg_tree);
275}
276
Daniel Lezcanob301b082011-06-15 15:45:12 +0200277static struct display_ops regulator_ops = {
278 .display = regulator_display,
279};
280
Daniel Lezcanob4eec7e2016-02-18 16:44:55 +0000281int regulator_init(struct powerdebug_options *options)
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200282{
Daniel Lezcanob4eec7e2016-02-18 16:44:55 +0000283 if (!(options->flags & REGULATOR_OPTION))
284 return 0;
285
Daniel Lezcano1eedd482016-02-22 11:05:51 +0000286 return display_register(REGULATOR, &regulator_ops);
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200287}