blob: 1417cb2b23e358591213978cf6cb6942f67aac84 [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;
Amit Arora728e0c92010-09-14 12:06:09 +053022int highlighted_row;
23double ticktime = 10.0; /* in seconds */
Amit Arorae9e16b02010-08-03 10:15:20 +053024
25int init_regulator_ds(void)
26{
27 DIR *regdir;
28 struct dirent *item;
29
30 regdir = opendir("/sys/class/regulator");
31 if (!regdir)
32 return(1);
33 while((item = readdir(regdir))) {
34 if (strncmp(item->d_name, "regulator", 9))
35 continue;
36
37 numregulators++;
38 }
39 closedir(regdir);
40
41 regulators_info = (struct regulator_info *)malloc(numregulators*
42 sizeof(struct regulator_info));
43 if (!regulators_info) {
Amit Arora85fd4952010-08-05 14:04:50 +053044 fprintf(stderr, "init_regulator_ds: Not enough memory to ");
45 fprintf(stderr, "read information for %d regulators!\n",
Amit Arorae9e16b02010-08-03 10:15:20 +053046 numregulators);
47 return(1);
48 }
49
50 return(0);
51}
52
53int read_and_print_sensor_info(int verbose)
54{
55 DIR *dir, *subdir;
Amit Arora1c037892010-08-05 13:46:10 +053056 int len, found = 0;
Amit Arorae9e16b02010-08-03 10:15:20 +053057 char filename[PATH_MAX], devpath[PATH_MAX];
58 char device[PATH_MAX];
59 struct dirent *item, *subitem;
60
61/*
62 1. readdir /sys/class/hwmon
63 2. foreach subdir in above, do following
64 a) check if its virtual or physical (check for device)
65 b) for each *type* (in, temp, pwm, fan, curr, power, energy)
66 i> search for "_" (strrchr... check for last)
67 ii> check the *item* depending on type
68 i.e for "in" type items=min, max, input, label
69 for "fan" type items=min,max,input,div,target,label
70 for "pwm" type items=enable, mode, freq
71 for "temp" type items=type, max, min,input,crit,offset,label,lowest,highest,
72 for "curr" type items=max,min,input
73 for "power" type items=average,average_interval,average_min/max, average_highest/lowest, input, input_highest/lowest, accuracy,alarm, cap, cap_min/max
74 for "energy" type items=input
75 for "intrusion" type items=alarm,beep
76*/
77
78 sprintf(filename, "%s", "/sys/class/hwmon");
79
80 dir = opendir(filename);
81 if (!dir)
82 return errno;
83
84 while ((item = readdir(dir))) {
85 if (item->d_name[0] == '.') /* skip the hidden files */
86 continue;
87
Amit Arora1c037892010-08-05 13:46:10 +053088 found = 1;
89
Amit Arorae9e16b02010-08-03 10:15:20 +053090 sprintf(filename, "/sys/class/hwmon/%s", item->d_name);
91 sprintf(devpath, "%s/device", filename);
92
93 len = readlink(devpath, device, PATH_MAX - 1);
94
95 if (len < 0)
96 strcpy(devpath, filename);
97 else
98 device[len] = '\0';
99
100 subdir = opendir(devpath);
101
102 printf("\nSensor Information for %s :\n", item->d_name);
103 fflush(stdin);
104 while ((subitem = readdir(subdir))) {
105 if (subitem->d_name[0] == '.') /* skip hidden files */
106 continue;
107
108 if(!strncmp(subitem->d_name, "in", 2))
109 get_sensor_info(devpath, subitem->d_name, "in",
110 verbose);
111 else if (!strncmp(subitem->d_name, "temp", 4))
112 get_sensor_info(devpath, subitem->d_name,
113 "temp", verbose);
114 else if (!strncmp(subitem->d_name, "fan", 4))
115 get_sensor_info(devpath, subitem->d_name,
116 "fan", verbose);
117 else if (!strncmp(subitem->d_name, "pwm", 4))
118 get_sensor_info(devpath, subitem->d_name,
119 "pwm", verbose);
120
121 }
122
123 closedir(subdir);
124 }
125 closedir(dir);
126
Amit Arora1c037892010-08-05 13:46:10 +0530127 if(!found && verbose) {
128 printf("Could not find sensor information!");
129 printf(" Looks like /sys/class/hwmon is empty.\n");
130 }
131
Amit Arorae9e16b02010-08-03 10:15:20 +0530132 return 0;
133}
134
Amit Arora47fd9182010-08-24 13:26:06 +0530135void read_info_from_dirent(struct dirent *ritem, char *str, int idx)
136{
137 if (!strcmp(ritem->d_name, "name"))
138 strcpy(regulators_info[idx].name, str);
139 if (!strcmp(ritem->d_name, "state"))
140 strcpy(regulators_info[idx].state, str);
141 if (!strcmp(ritem->d_name, "status"))
142 strcpy(regulators_info[idx].status, str);
Amit Arorae9e16b02010-08-03 10:15:20 +0530143
Amit Arora47fd9182010-08-24 13:26:06 +0530144 if (!strcmp(ritem->d_name, "type"))
145 strcpy(regulators_info[idx].type, str);
146 if (!strcmp(ritem->d_name, "opmode"))
147 strcpy(regulators_info[idx].opmode, str);
148
149 if (!strcmp(ritem->d_name, "microvolts"))
150 regulators_info[idx].microvolts = atoi(str);
151 if (!strcmp(ritem->d_name, "min_microvolts"))
152 regulators_info[idx].min_microvolts = atoi(str);
153 if (!strcmp(ritem->d_name, "max_microvolts"))
154 regulators_info[idx].max_microvolts = atoi(str);
155
156 if (!strcmp(ritem->d_name, "microamps"))
157 regulators_info[idx].microamps = atoi(str);
158 if (!strcmp(ritem->d_name, "min_microamps"))
159 regulators_info[idx].min_microamps = atoi(str);
160 if (!strcmp(ritem->d_name, "max_microamps"))
161 regulators_info[idx].max_microamps = atoi(str);
162 if (!strcmp(ritem->d_name, "requested_microamps"))
163 regulators_info[idx].requested_microamps = atoi(str);
164
165 if (!strcmp(ritem->d_name, "num_users"))
166 regulators_info[idx].num_users = atoi(str);
167}
168
169int read_regulator_info(void)
Amit Arorae9e16b02010-08-03 10:15:20 +0530170{
171 FILE *file = NULL;
172 DIR *regdir, *dir;
173 int len, count = 0, ret = 0;
174 char line[1024], filename[1024], *fptr;
175 struct dirent *item, *ritem;
176
177 regdir = opendir("/sys/class/regulator");
178 if (!regdir)
179 return(1);
180 while((item = readdir(regdir))) {
181 if (strlen(item->d_name) < 3)
182 continue;
183
184 if (strncmp(item->d_name, "regulator", 9))
185 continue;
186
Amit Arora85fd4952010-08-05 14:04:50 +0530187 len = sprintf(filename, "/sys/class/regulator/%s",
188 item->d_name);
Amit Arorae9e16b02010-08-03 10:15:20 +0530189
190 dir = opendir(filename);
Amit Arora85fd4952010-08-05 14:04:50 +0530191 if (!dir)
Amit Arora7ef178c2010-08-03 15:57:21 +0530192 continue;
Amit Arorae9e16b02010-08-03 10:15:20 +0530193
194 count++;
195 if (count > numregulators) {
196 ret = 1;
Amit Arora85fd4952010-08-05 14:04:50 +0530197 goto exit;
Amit Arorae9e16b02010-08-03 10:15:20 +0530198 }
199
Amit Arorafccac912010-08-03 16:15:09 +0530200 strcpy(regulators_info[count-1].name, item->d_name);
Amit Arorae9e16b02010-08-03 10:15:20 +0530201 while((ritem = readdir(dir))) {
Amit Arorae9e16b02010-08-03 10:15:20 +0530202 if (strlen(ritem->d_name) < 3)
203 continue;
204
205 sprintf(filename + len, "/%s", ritem->d_name);
Amit Arorae9e16b02010-08-03 10:15:20 +0530206
207 file = fopen(filename, "r");
208 if (!file)
209 continue;
210
211 memset(line, 0, 1024);
212 fptr = fgets(line, 1024, file);
213 fclose(file);
Amit Arora85fd4952010-08-05 14:04:50 +0530214 if (!fptr)
Amit Arora7ef178c2010-08-03 15:57:21 +0530215 continue;
Amit Arorae9e16b02010-08-03 10:15:20 +0530216
Amit Arora47fd9182010-08-24 13:26:06 +0530217 read_info_from_dirent(ritem, fptr, count - 1);
Amit Arorae9e16b02010-08-03 10:15:20 +0530218 }
Amit Arora85fd4952010-08-05 14:04:50 +0530219exit:
Amit Arorae9e16b02010-08-03 10:15:20 +0530220 closedir(dir);
221 if (ret)
222 break;
223 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530224 closedir(regdir);
225
226 return ret;
227}
228
229
230int main(int argc, char **argv)
231{
232 int c;
Amit Arora47fd9182010-08-24 13:26:06 +0530233 int firsttime = 1;
Amit Arora728e0c92010-09-14 12:06:09 +0530234 int regulators = 0, sensors = 0, clocks = 0, verbose = 0;
Amit Arorae9e16b02010-08-03 10:15:20 +0530235
Amit Arorafefe8bf2010-08-05 13:31:20 +0530236 /*
237 * Options:
238 * -r, --regulator : regulator
239 * -s, --sensor : sensors
Amit Arora728e0c92010-09-14 12:06:09 +0530240 * -c, --clock : clocks
241 * -t, --time : ticktime
Amit Arora47fd9182010-08-24 13:26:06 +0530242 * -d, --dump : dump
Amit Arorafefe8bf2010-08-05 13:31:20 +0530243 * -v, --verbose : verbose
244 * -V, --version : version
245 * -h, --help : help
246 * no option / default : show usage!
Amit Arorae9e16b02010-08-03 10:15:20 +0530247 */
248
Amit Arorafefe8bf2010-08-05 13:31:20 +0530249 while (1) {
250 int optindex = 0;
251 static struct option long_options[] = {
252 {"regulator", 0, 0, 'r'},
253 {"sensor", 0, 0, 's'},
Amit Arora728e0c92010-09-14 12:06:09 +0530254 {"clock", 0, 0, 'c'},
255 {"time", 0, 0, 't'},
Amit Arora47fd9182010-08-24 13:26:06 +0530256 {"dump", 0, 0, 'd'},
Amit Arorafefe8bf2010-08-05 13:31:20 +0530257 {"verbose", 0, 0, 'v'},
258 {"version", 0, 0, 'V'},
259 {"help", 0, 0, 'h'},
260 {0, 0, 0, 0}
261 };
262
Amit Arora728e0c92010-09-14 12:06:09 +0530263 c = getopt_long(argc, argv, "rsct:dvVh", long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +0530264 if (c == -1)
265 break;
266
267 switch (c) {
268 case 'r':
269 regulators = 1;
270 break;
271 case 's':
272 sensors = 1;
273 break;
Amit Arora728e0c92010-09-14 12:06:09 +0530274 case 'c':
275 clocks = 1;
276 break;
277 case 't':
278 ticktime = strtod(optarg, NULL);
279 break;
Amit Arora47fd9182010-08-24 13:26:06 +0530280 case 'd':
281 dump = 1;
282 break;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530283 case 'v':
284 verbose = 1;
285 break;
286 case 'V':
287 version();
288 break;
289 case 'h':
290 usage(argv);
291 break;
292 case '?':
293 fprintf (stderr, "%s: Unknown option %c'.\n",
294 argv[0], optopt);
295 exit(1);
296 default:
297 usage(argv);
298 break;
299 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530300 }
301
Amit Arorafefe8bf2010-08-05 13:31:20 +0530302
303 /* Need atleast one option specified */
Amit Arora728e0c92010-09-14 12:06:09 +0530304 if (!regulators && !sensors && !clocks) {
Amit Arorae9e16b02010-08-03 10:15:20 +0530305 usage(argv);
306 }
307
308 init_regulator_ds();
309
Amit Arora47fd9182010-08-24 13:26:06 +0530310 while(1) {
311 int key = 0;
312 struct timeval tval;
313 fd_set readfds;
Amit Arora728e0c92010-09-14 12:06:09 +0530314 int row = 1;
Amit Arorae9e16b02010-08-03 10:15:20 +0530315
Amit Arora47fd9182010-08-24 13:26:06 +0530316 if (!dump) {
317 if(firsttime) {
318 init_curses();
319 firsttime = 0;
320 }
321 create_windows();
322 show_header();
323 }
324
325 if (regulators) {
326 read_regulator_info();
Amit Arora728e0c92010-09-14 12:06:09 +0530327 if (!dump) {
328 row = create_regulator_win(row,
329numregulators+2);
Amit Arora47fd9182010-08-24 13:26:06 +0530330 show_regulator_info(verbose);
Amit Arora728e0c92010-09-14 12:06:09 +0530331 }
Amit Arora47fd9182010-08-24 13:26:06 +0530332 else
333 print_regulator_info(verbose);
334 }
335
Amit Arora728e0c92010-09-14 12:06:09 +0530336 if (clocks && !dump) {
337 int hrow;
338 row = create_clock_win(row, 100);//giv big no.as of now
339 hrow = read_and_print_clock_info(verbose,
340 highlighted_row);
341 highlighted_row = hrow;
342 }
Amit Arora47fd9182010-08-24 13:26:06 +0530343
344 if (sensors) {
Amit Arora728e0c92010-09-14 12:06:09 +0530345 if (!dump) {
346 row = create_sensor_win(row, 100);//big no. as of now
347 print_sensor_header();
348 }
349 else
350 read_and_print_sensor_info(verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530351 }
352
353 if (dump)
354 break;
355
356 FD_ZERO(&readfds);
357 FD_SET(0, &readfds);
358 tval.tv_sec = ticktime;
359 tval.tv_usec = (ticktime - tval.tv_sec) * 1000000;
360
361 key = select(1, &readfds, NULL, NULL, &tval);
362
363 if (key) {
364 char keychar;
365
366 int keystroke = fgetc(stdin);
367 if (keystroke == EOF)
368 exit(0);
369
Amit Arora728e0c92010-09-14 12:06:09 +0530370 if (keystroke == 9)
371 highlighted_row++;
372
Amit Arora47fd9182010-08-24 13:26:06 +0530373 keychar = toupper(keystroke);
Amit Arora728e0c92010-09-14 12:06:09 +0530374#ifdef DEBUG_KEY
375 if (keystroke == 13) {
376 killall_windows();
377 fini_curses();
378 printf("powerdebug: key=%d : char=%c\n", keystroke, keychar);
379 printf("highlighted_row = %d\n", highlighted_row);
380 exit(0);
381 }
382#endif
383
Amit Arora47fd9182010-08-24 13:26:06 +0530384 if (keychar == 'Q')
385 exit(0);
386 if (keychar == 'R')
387 ticktime = 3;
388 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530389 }
390
Amit Arora85fd4952010-08-05 14:04:50 +0530391 exit(0);
Amit Arorae9e16b02010-08-03 10:15:20 +0530392}