blob: 27d75b66d3ddfb629e4e78a0cc014a6ab96b2a8f [file] [log] [blame]
Amit Arora17552782010-12-02 12:23:14 +05301/*******************************************************************************
Amit Kucheriac0e17fc2011-01-17 09:35:52 +02002 * Copyright (C) 2010, Linaro Limited.
Amit Arora17552782010-12-02 12:23:14 +05303 *
4 * This file is part of PowerDebug.
5 *
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of the Eclipse Public License v1.0
8 * which accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 *
11 * Contributors:
12 * Amit Arora <amit.arora@linaro.org> (IBM Corporation)
13 * - initial API and implementation
Daniel Lezcanob25be4a2011-06-15 15:45:12 +020014 * Daniel Lezcano <daniel.lezcano@linaro.org> (IBM Corporation)
15 * - rewrote code and API based on the tree
Amit Arora17552782010-12-02 12:23:14 +053016 *******************************************************************************/
17
18#include "regulator.h"
19
Daniel Lezcano2e6ecca2011-03-26 22:05:51 +010020#define SYSFS_REGULATOR "/sys/class/regulator"
Daniel Lezcano15627482011-06-15 15:45:12 +020021#define VALUE_MAX 16
22
Daniel Lezcanob25be4a2011-06-15 15:45:12 +020023#define _GNU_SOURCE
24#include <stdio.h>
25#undef _GNU_SOURCE
26#include <sys/types.h>
27#include <stdbool.h>
28#include <dirent.h>
29#include <string.h>
30#include <stdlib.h>
Daniel Lezcanob301b082011-06-15 15:45:12 +020031#include "display.h"
Daniel Lezcanob25be4a2011-06-15 15:45:12 +020032#include "powerdebug.h"
33#include "tree.h"
34#include "utils.h"
Daniel Lezcano2e6ecca2011-03-26 22:05:51 +010035
Daniel Lezcanob25be4a2011-06-15 15:45:12 +020036struct regulator_info {
37 char name[NAME_MAX];
38 char state[VALUE_MAX];
39 char status[VALUE_MAX];
40 char type[VALUE_MAX];
41 char opmode[VALUE_MAX];
42 int microvolts;
43 int min_microvolts;
44 int max_microvolts;
45 int microamps;
46 int min_microamps;
47 int max_microamps;
48 int requested_microamps;
49 int num_users;
50};
51
52struct regulator_data {
53 const char *name;
54 const char *ifmt;
55 const char *ofmt;
56 bool derefme;
57};
58
59static struct regulator_data regdata[] = {
60 { "name", "%s", "\tname: %s\n" },
61 { "status", "%s", "\tstatus: %s\n" },
62 { "state", "%s", "\tstate: %s\n" },
63 { "type", "%s", "\ttype: %s\n" },
64 { "num_users", "%d", "\tnum_users: %d\n", true },
65 { "microvolts", "%d", "\tmicrovolts: %d\n", true },
66 { "max_microvolts", "%d", "\tmax_microvolts: %d\n", true },
67 { "min_microvolts", "%d", "\tmin_microvolts: %d\n", true },
68};
69
70static struct tree *reg_tree;
Sanjay Singh Rawat96f6e052014-05-26 11:35:02 +053071static bool regulator_error = false;
Daniel Lezcanob25be4a2011-06-15 15:45:12 +020072
73static struct regulator_info *regulator_alloc(void)
Amit Arora17552782010-12-02 12:23:14 +053074{
Daniel Lezcanob25be4a2011-06-15 15:45:12 +020075 struct regulator_info *regi;
Amit Arora17552782010-12-02 12:23:14 +053076
Daniel Lezcanob25be4a2011-06-15 15:45:12 +020077 regi = malloc(sizeof(*regi));
78 if (regi)
79 memset(regi, 0, sizeof(*regi));
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +010080
Daniel Lezcanob25be4a2011-06-15 15:45:12 +020081 return regi;
Amit Arora17552782010-12-02 12:23:14 +053082}
83
Daniel Lezcanob25be4a2011-06-15 15:45:12 +020084static int regulator_dump_cb(struct tree *tree, void *data)
Amit Arora17552782010-12-02 12:23:14 +053085{
86 int i;
Daniel Lezcanob25be4a2011-06-15 15:45:12 +020087 char buffer[NAME_MAX];
88 size_t nregdata = sizeof(regdata) / sizeof(regdata[0]);
Amit Arora17552782010-12-02 12:23:14 +053089
Daniel Lezcanob25be4a2011-06-15 15:45:12 +020090 if (!strncmp("regulator.", tree->name, strlen("regulator.")))
91 printf("\n%s:\n", tree->name);
92
93 for (i = 0; i < nregdata; i++) {
Sanjay Singh Rawat9fe0c052013-02-22 17:57:18 +053094 int val;
Daniel Lezcanob25be4a2011-06-15 15:45:12 +020095
96 if (file_read_value(tree->path, regdata[i].name,
97 regdata[i].ifmt, buffer))
98 continue;
99
Sanjay Singh Rawat9fe0c052013-02-22 17:57:18 +0530100 if (regdata[i].derefme) {
101 val = atoi(buffer);
102 printf(regdata[i].ofmt, val);
103 } else
104 printf(regdata[i].ofmt, buffer);
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200105 }
106
107 return 0;
108}
109
110int regulator_dump(void)
111{
Amit Arora422c52f2010-12-02 16:22:29 +0530112 printf("\nRegulator Information:\n");
113 printf("*********************\n\n");
114
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200115 return tree_for_each(reg_tree, regulator_dump_cb, NULL);
Amit Arora17552782010-12-02 12:23:14 +0530116}
117
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200118static int regulator_display_cb(struct tree *t, void *data)
Amit Arora17552782010-12-02 12:23:14 +0530119{
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200120 struct regulator_info *reg = t->private;
121 int *line = data;
122 char *buf;
Amit Arora17552782010-12-02 12:23:14 +0530123
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200124 /* we skip the root node of the tree */
125 if (!t->parent)
126 return 0;
Amit Arora17552782010-12-02 12:23:14 +0530127
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200128 if (!strlen(reg->name))
129 return 0;
Amit Arora17552782010-12-02 12:23:14 +0530130
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200131 if (asprintf(&buf, "%-11s %-11s %-11s %-11s %-11d %-11d %-11d %-12d",
132 reg->name, reg->status, reg->state, reg->type,
133 reg->num_users, reg->microvolts, reg->min_microvolts,
134 reg->max_microvolts) < 0)
135 return -1;
Amit Arora17552782010-12-02 12:23:14 +0530136
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200137 display_print_line(REGULATOR, *line, buf, reg->num_users, t);
138
139 (*line)++;
140
141 free(buf);
142
143 return 0;
Amit Arora17552782010-12-02 12:23:14 +0530144}
145
Daniel Lezcanofa453332011-06-21 00:57:08 +0200146static int regulator_print_header(void)
147{
148 char *buf;
149 int ret;
150
151 if (asprintf(&buf, "%-11s %-11s %-11s %-11s %-11s %-11s %-11s %-12s",
152 "Name", "Status", "State", "Type", "Users", "Microvolts",
153 "Min u-volts", "Max u-volts") < 0)
154 return -1;
155
Daniel Lezcano372ffba2011-06-21 00:57:08 +0200156 ret = display_column_name(buf);
Daniel Lezcanofa453332011-06-21 00:57:08 +0200157
158 free(buf);
159
160 return ret;
161
162}
163
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200164static int regulator_filter_cb(const char *name)
165{
166 /* let's ignore some directories in order to avoid to be
167 * pulled inside the sysfs circular symlinks mess/hell
168 * (choose the word which fit better)
169 */
170 if (!strcmp(name, "device"))
171 return 1;
172
173 if (!strcmp(name, "subsystem"))
174 return 1;
175
176 if (!strcmp(name, "driver"))
177 return 1;
178
179 return 0;
180}
181
182static inline int read_regulator_cb(struct tree *t, void *data)
183{
184 struct regulator_info *reg = t->private;
185
186 file_read_value(t->path, "name", "%s", reg->name);
187 file_read_value(t->path, "state", "%s", reg->state);
188 file_read_value(t->path, "status", "%s", reg->status);
189 file_read_value(t->path, "type", "%s", reg->type);
190 file_read_value(t->path, "opmode", "%s", reg->opmode);
191 file_read_value(t->path, "num_users", "%d", &reg->num_users);
192 file_read_value(t->path, "microvolts", "%d", &reg->microvolts);
193 file_read_value(t->path, "min_microvolts", "%d", &reg->min_microvolts);
194 file_read_value(t->path, "max_microvolts", "%d", &reg->max_microvolts);
195 file_read_value(t->path, "microamps", "%d", &reg->microamps);
196 file_read_value(t->path, "min_microamps", "%d", &reg->min_microamps);
197 file_read_value(t->path, "max_microamps", "%d", &reg->max_microamps);
198
199 return 0;
200}
201
Shaojie Sunc1462e72013-08-20 20:35:15 +0800202static int read_regulator_info(struct tree *tree)
203{
204 return tree_for_each(tree, read_regulator_cb, NULL);
205}
206
207static int regulator_print_info(struct tree *tree)
208{
209 int ret, line = 0;
210
211 display_reset_cursor(REGULATOR);
212
213 regulator_print_header();
214
215 ret = tree_for_each(tree, regulator_display_cb, &line);
216
217 display_refresh_pad(REGULATOR);
218
219 return ret;
220}
221
222static int regulator_display(bool refresh)
223{
Sanjay Singh Rawat96f6e052014-05-26 11:35:02 +0530224 if (regulator_error) {
225 display_message(REGULATOR,
226 "error: path " SYSFS_REGULATOR " not found");
227 return -2;
228 }
229
Shaojie Sunc1462e72013-08-20 20:35:15 +0800230 if (refresh && read_regulator_info(reg_tree))
231 return -1;
232
233 return regulator_print_info(reg_tree);
234}
235
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200236static int fill_regulator_cb(struct tree *t, void *data)
237{
238 struct regulator_info *reg;
239
240 reg = regulator_alloc();
Sanjay Singh Rawat5fef0052013-04-17 15:02:01 +0530241 if (!reg) {
242 printf("error: unable to allocate memory for regulator\n");
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200243 return -1;
Sanjay Singh Rawat5fef0052013-04-17 15:02:01 +0530244 }
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200245 t->private = reg;
246
247 /* we skip the root node but we set it expanded for its children */
248 if (!t->parent)
249 return 0;
250
251 return read_regulator_cb(t, data);
252}
253
254static int fill_regulator_tree(void)
255{
256 return tree_for_each(reg_tree, fill_regulator_cb, NULL);
257}
258
Daniel Lezcanob301b082011-06-15 15:45:12 +0200259static struct display_ops regulator_ops = {
260 .display = regulator_display,
261};
262
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200263int regulator_init(void)
264{
Sanjay Singh Rawat96f6e052014-05-26 11:35:02 +0530265 int ret = 0;
266
267 ret = display_register(REGULATOR, &regulator_ops);
268 if (!ret)
269 printf("error: regulator display register failed");
270
271 if (access(SYSFS_REGULATOR, F_OK))
272 regulator_error = true; /* set the flag */
273
Daniel Lezcano25fc4a32011-08-25 15:46:13 +0200274 reg_tree = tree_load(SYSFS_REGULATOR, regulator_filter_cb, false);
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200275 if (!reg_tree)
276 return -1;
277
Daniel Lezcanocaafece2011-06-27 22:59:17 +0200278 if (fill_regulator_tree())
279 return -1;
280
Sanjay Singh Rawat96f6e052014-05-26 11:35:02 +0530281 return ret;
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200282}