blob: a7b37c2e1a3b6b2ff83bc7688c10256cf759f398 [file] [log] [blame]
Amit Arora39f29542010-09-14 12:03:22 +05301/*******************************************************************************
2 * Copyright (C) 2010, Linaro
3 * Copyright (C) 2010, IBM Corporation
4 *
5 * This file is part of PowerDebug.
6 *
7 * All rights reserved. This program and the accompanying materials
8 * are made available under the terms of the Eclipse Public License v1.0
9 * which accompanies this distribution, and is available at
10 * http://www.eclipse.org/legal/epl-v10.html
11 *
12 * Contributors:
13 * Amit Arora <amit.arora@linaro.org> (IBM Corporation)
14 * - initial API and implementation
15 *******************************************************************************/
16
Amit Arorae9e16b02010-08-03 10:15:20 +053017#include <getopt.h>
Amit Arorae9e16b02010-08-03 10:15:20 +053018#include "powerdebug.h"
19
Amit Arorae9e16b02010-08-03 10:15:20 +053020int numregulators;
Amit Arora47fd9182010-08-24 13:26:06 +053021int dump;
22int ticktime=3; /* in seconds */
Amit Arorae9e16b02010-08-03 10:15:20 +053023
24int init_regulator_ds(void)
25{
26 DIR *regdir;
27 struct dirent *item;
28
29 regdir = opendir("/sys/class/regulator");
30 if (!regdir)
31 return(1);
32 while((item = readdir(regdir))) {
33 if (strncmp(item->d_name, "regulator", 9))
34 continue;
35
36 numregulators++;
37 }
38 closedir(regdir);
39
40 regulators_info = (struct regulator_info *)malloc(numregulators*
41 sizeof(struct regulator_info));
42 if (!regulators_info) {
Amit Arora85fd4952010-08-05 14:04:50 +053043 fprintf(stderr, "init_regulator_ds: Not enough memory to ");
44 fprintf(stderr, "read information for %d regulators!\n",
Amit Arorae9e16b02010-08-03 10:15:20 +053045 numregulators);
46 return(1);
47 }
48
49 return(0);
50}
51
52int read_and_print_sensor_info(int verbose)
53{
54 DIR *dir, *subdir;
Amit Arora1c037892010-08-05 13:46:10 +053055 int len, found = 0;
Amit Arorae9e16b02010-08-03 10:15:20 +053056 char filename[PATH_MAX], devpath[PATH_MAX];
57 char device[PATH_MAX];
58 struct dirent *item, *subitem;
59
60/*
61 1. readdir /sys/class/hwmon
62 2. foreach subdir in above, do following
63 a) check if its virtual or physical (check for device)
64 b) for each *type* (in, temp, pwm, fan, curr, power, energy)
65 i> search for "_" (strrchr... check for last)
66 ii> check the *item* depending on type
67 i.e for "in" type items=min, max, input, label
68 for "fan" type items=min,max,input,div,target,label
69 for "pwm" type items=enable, mode, freq
70 for "temp" type items=type, max, min,input,crit,offset,label,lowest,highest,
71 for "curr" type items=max,min,input
72 for "power" type items=average,average_interval,average_min/max, average_highest/lowest, input, input_highest/lowest, accuracy,alarm, cap, cap_min/max
73 for "energy" type items=input
74 for "intrusion" type items=alarm,beep
75*/
76
77 sprintf(filename, "%s", "/sys/class/hwmon");
78
79 dir = opendir(filename);
80 if (!dir)
81 return errno;
82
83 while ((item = readdir(dir))) {
84 if (item->d_name[0] == '.') /* skip the hidden files */
85 continue;
86
Amit Arora1c037892010-08-05 13:46:10 +053087 found = 1;
88
Amit Arorae9e16b02010-08-03 10:15:20 +053089 sprintf(filename, "/sys/class/hwmon/%s", item->d_name);
90 sprintf(devpath, "%s/device", filename);
91
92 len = readlink(devpath, device, PATH_MAX - 1);
93
94 if (len < 0)
95 strcpy(devpath, filename);
96 else
97 device[len] = '\0';
98
99 subdir = opendir(devpath);
100
101 printf("\nSensor Information for %s :\n", item->d_name);
102 fflush(stdin);
103 while ((subitem = readdir(subdir))) {
104 if (subitem->d_name[0] == '.') /* skip hidden files */
105 continue;
106
107 if(!strncmp(subitem->d_name, "in", 2))
108 get_sensor_info(devpath, subitem->d_name, "in",
109 verbose);
110 else if (!strncmp(subitem->d_name, "temp", 4))
111 get_sensor_info(devpath, subitem->d_name,
112 "temp", verbose);
113 else if (!strncmp(subitem->d_name, "fan", 4))
114 get_sensor_info(devpath, subitem->d_name,
115 "fan", verbose);
116 else if (!strncmp(subitem->d_name, "pwm", 4))
117 get_sensor_info(devpath, subitem->d_name,
118 "pwm", verbose);
119
120 }
121
122 closedir(subdir);
123 }
124 closedir(dir);
125
Amit Arora1c037892010-08-05 13:46:10 +0530126 if(!found && verbose) {
127 printf("Could not find sensor information!");
128 printf(" Looks like /sys/class/hwmon is empty.\n");
129 }
130
Amit Arorae9e16b02010-08-03 10:15:20 +0530131 return 0;
132}
133
Amit Arora47fd9182010-08-24 13:26:06 +0530134void read_info_from_dirent(struct dirent *ritem, char *str, int idx)
135{
136 if (!strcmp(ritem->d_name, "name"))
137 strcpy(regulators_info[idx].name, str);
138 if (!strcmp(ritem->d_name, "state"))
139 strcpy(regulators_info[idx].state, str);
140 if (!strcmp(ritem->d_name, "status"))
141 strcpy(regulators_info[idx].status, str);
Amit Arorae9e16b02010-08-03 10:15:20 +0530142
Amit Arora47fd9182010-08-24 13:26:06 +0530143 if (!strcmp(ritem->d_name, "type"))
144 strcpy(regulators_info[idx].type, str);
145 if (!strcmp(ritem->d_name, "opmode"))
146 strcpy(regulators_info[idx].opmode, str);
147
148 if (!strcmp(ritem->d_name, "microvolts"))
149 regulators_info[idx].microvolts = atoi(str);
150 if (!strcmp(ritem->d_name, "min_microvolts"))
151 regulators_info[idx].min_microvolts = atoi(str);
152 if (!strcmp(ritem->d_name, "max_microvolts"))
153 regulators_info[idx].max_microvolts = atoi(str);
154
155 if (!strcmp(ritem->d_name, "microamps"))
156 regulators_info[idx].microamps = atoi(str);
157 if (!strcmp(ritem->d_name, "min_microamps"))
158 regulators_info[idx].min_microamps = atoi(str);
159 if (!strcmp(ritem->d_name, "max_microamps"))
160 regulators_info[idx].max_microamps = atoi(str);
161 if (!strcmp(ritem->d_name, "requested_microamps"))
162 regulators_info[idx].requested_microamps = atoi(str);
163
164 if (!strcmp(ritem->d_name, "num_users"))
165 regulators_info[idx].num_users = atoi(str);
166}
167
168int read_regulator_info(void)
Amit Arorae9e16b02010-08-03 10:15:20 +0530169{
170 FILE *file = NULL;
171 DIR *regdir, *dir;
172 int len, count = 0, ret = 0;
173 char line[1024], filename[1024], *fptr;
174 struct dirent *item, *ritem;
175
176 regdir = opendir("/sys/class/regulator");
177 if (!regdir)
178 return(1);
179 while((item = readdir(regdir))) {
180 if (strlen(item->d_name) < 3)
181 continue;
182
183 if (strncmp(item->d_name, "regulator", 9))
184 continue;
185
Amit Arora85fd4952010-08-05 14:04:50 +0530186 len = sprintf(filename, "/sys/class/regulator/%s",
187 item->d_name);
Amit Arorae9e16b02010-08-03 10:15:20 +0530188
189 dir = opendir(filename);
Amit Arora85fd4952010-08-05 14:04:50 +0530190 if (!dir)
Amit Arora7ef178c2010-08-03 15:57:21 +0530191 continue;
Amit Arorae9e16b02010-08-03 10:15:20 +0530192
193 count++;
194 if (count > numregulators) {
195 ret = 1;
Amit Arora85fd4952010-08-05 14:04:50 +0530196 goto exit;
Amit Arorae9e16b02010-08-03 10:15:20 +0530197 }
198
Amit Arorafccac912010-08-03 16:15:09 +0530199 strcpy(regulators_info[count-1].name, item->d_name);
Amit Arorae9e16b02010-08-03 10:15:20 +0530200 while((ritem = readdir(dir))) {
Amit Arorae9e16b02010-08-03 10:15:20 +0530201 if (strlen(ritem->d_name) < 3)
202 continue;
203
204 sprintf(filename + len, "/%s", ritem->d_name);
Amit Arorae9e16b02010-08-03 10:15:20 +0530205
206 file = fopen(filename, "r");
207 if (!file)
208 continue;
209
210 memset(line, 0, 1024);
211 fptr = fgets(line, 1024, file);
212 fclose(file);
Amit Arora85fd4952010-08-05 14:04:50 +0530213 if (!fptr)
Amit Arora7ef178c2010-08-03 15:57:21 +0530214 continue;
Amit Arorae9e16b02010-08-03 10:15:20 +0530215
Amit Arora47fd9182010-08-24 13:26:06 +0530216 read_info_from_dirent(ritem, fptr, count - 1);
Amit Arorae9e16b02010-08-03 10:15:20 +0530217 }
Amit Arora85fd4952010-08-05 14:04:50 +0530218exit:
Amit Arorae9e16b02010-08-03 10:15:20 +0530219 closedir(dir);
220 if (ret)
221 break;
222 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530223 closedir(regdir);
224
225 return ret;
226}
227
228
229int main(int argc, char **argv)
230{
231 int c;
Amit Arora47fd9182010-08-24 13:26:06 +0530232 int firsttime = 1;
Amit Arorae9e16b02010-08-03 10:15:20 +0530233 int regulators = 0, sensors = 0, verbose = 0;
234
Amit Arorafefe8bf2010-08-05 13:31:20 +0530235 /*
236 * Options:
237 * -r, --regulator : regulator
238 * -s, --sensor : sensors
Amit Arora47fd9182010-08-24 13:26:06 +0530239 * -d, --dump : dump
Amit Arorafefe8bf2010-08-05 13:31:20 +0530240 * -v, --verbose : verbose
241 * -V, --version : version
242 * -h, --help : help
243 * no option / default : show usage!
Amit Arorae9e16b02010-08-03 10:15:20 +0530244 */
245
Amit Arorafefe8bf2010-08-05 13:31:20 +0530246 while (1) {
247 int optindex = 0;
248 static struct option long_options[] = {
249 {"regulator", 0, 0, 'r'},
250 {"sensor", 0, 0, 's'},
Amit Arora47fd9182010-08-24 13:26:06 +0530251 {"dump", 0, 0, 'd'},
Amit Arorafefe8bf2010-08-05 13:31:20 +0530252 {"verbose", 0, 0, 'v'},
253 {"version", 0, 0, 'V'},
254 {"help", 0, 0, 'h'},
255 {0, 0, 0, 0}
256 };
257
Amit Arora47fd9182010-08-24 13:26:06 +0530258 c = getopt_long(argc, argv, "rsdvVh", long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +0530259 if (c == -1)
260 break;
261
262 switch (c) {
263 case 'r':
264 regulators = 1;
265 break;
266 case 's':
267 sensors = 1;
268 break;
Amit Arora47fd9182010-08-24 13:26:06 +0530269 case 'd':
270 dump = 1;
271 break;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530272 case 'v':
273 verbose = 1;
274 break;
275 case 'V':
276 version();
277 break;
278 case 'h':
279 usage(argv);
280 break;
281 case '?':
282 fprintf (stderr, "%s: Unknown option %c'.\n",
283 argv[0], optopt);
284 exit(1);
285 default:
286 usage(argv);
287 break;
288 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530289 }
290
Amit Arorafefe8bf2010-08-05 13:31:20 +0530291
292 /* Need atleast one option specified */
Amit Arorae9e16b02010-08-03 10:15:20 +0530293 if (!regulators && !sensors) {
Amit Arorae9e16b02010-08-03 10:15:20 +0530294 usage(argv);
295 }
296
297 init_regulator_ds();
298
Amit Arora47fd9182010-08-24 13:26:06 +0530299 while(1) {
300 int key = 0;
301 struct timeval tval;
302 fd_set readfds;
Amit Arorae9e16b02010-08-03 10:15:20 +0530303
Amit Arora47fd9182010-08-24 13:26:06 +0530304 if (!dump) {
305 if(firsttime) {
306 init_curses();
307 firsttime = 0;
308 }
309 create_windows();
310 show_header();
311 }
312
313 if (regulators) {
314 read_regulator_info();
315 if (!dump)
316 show_regulator_info(verbose);
317 else
318 print_regulator_info(verbose);
319 }
320
321
322 if (sensors) {
323 read_and_print_sensor_info(verbose);
324 }
325
326 if (dump)
327 break;
328
329 FD_ZERO(&readfds);
330 FD_SET(0, &readfds);
331 tval.tv_sec = ticktime;
332 tval.tv_usec = (ticktime - tval.tv_sec) * 1000000;
333
334 key = select(1, &readfds, NULL, NULL, &tval);
335
336 if (key) {
337 char keychar;
338
339 int keystroke = fgetc(stdin);
340 if (keystroke == EOF)
341 exit(0);
342
343 keychar = toupper(keystroke);
344 if (keychar == 'Q')
345 exit(0);
346 if (keychar == 'R')
347 ticktime = 3;
348 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530349 }
350
Amit Arora85fd4952010-08-05 14:04:50 +0530351 exit(0);
Amit Arorae9e16b02010-08-03 10:15:20 +0530352}