blob: c94c14b163cdfb7976d05172f0028793546c817e [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;
Amit Arorac93e0712010-10-07 13:51:53 +053023int selectedwindow = -1;
Amit Arora728e0c92010-09-14 12:06:09 +053024double ticktime = 10.0; /* in seconds */
Amit Arorae9e16b02010-08-03 10:15:20 +053025
Amit Arorac93e0712010-10-07 13:51:53 +053026char *win_names[TOTAL_FEATURE_WINS] = {
Amit Arora6e774cd2010-10-28 11:31:24 +053027 "Regulators",
28 "Clocks",
29 "Sensors" };
Amit Arorac93e0712010-10-07 13:51:53 +053030
Amit Arorae9e16b02010-08-03 10:15:20 +053031int init_regulator_ds(void)
32{
33 DIR *regdir;
34 struct dirent *item;
35
36 regdir = opendir("/sys/class/regulator");
37 if (!regdir)
38 return(1);
39 while((item = readdir(regdir))) {
40 if (strncmp(item->d_name, "regulator", 9))
41 continue;
42
43 numregulators++;
44 }
45 closedir(regdir);
46
47 regulators_info = (struct regulator_info *)malloc(numregulators*
48 sizeof(struct regulator_info));
49 if (!regulators_info) {
Amit Arora24ed7d12010-09-14 12:12:58 +053050 fprintf(stderr, "init_regulator_ds: Not enough memory to "
51 "read information for %d regulators!\n", numregulators);
Amit Arorae9e16b02010-08-03 10:15:20 +053052 return(1);
53 }
54
55 return(0);
56}
57
58int read_and_print_sensor_info(int verbose)
59{
60 DIR *dir, *subdir;
Amit Arora1c037892010-08-05 13:46:10 +053061 int len, found = 0;
Amit Arorae9e16b02010-08-03 10:15:20 +053062 char filename[PATH_MAX], devpath[PATH_MAX];
63 char device[PATH_MAX];
64 struct dirent *item, *subitem;
65
Amit Arorae9e16b02010-08-03 10:15:20 +053066 sprintf(filename, "%s", "/sys/class/hwmon");
Amit Arorae9e16b02010-08-03 10:15:20 +053067 dir = opendir(filename);
68 if (!dir)
69 return errno;
70
71 while ((item = readdir(dir))) {
72 if (item->d_name[0] == '.') /* skip the hidden files */
73 continue;
74
Amit Arora1c037892010-08-05 13:46:10 +053075 found = 1;
76
Amit Arorae9e16b02010-08-03 10:15:20 +053077 sprintf(filename, "/sys/class/hwmon/%s", item->d_name);
78 sprintf(devpath, "%s/device", filename);
79
80 len = readlink(devpath, device, PATH_MAX - 1);
81
82 if (len < 0)
83 strcpy(devpath, filename);
84 else
85 device[len] = '\0';
86
87 subdir = opendir(devpath);
88
89 printf("\nSensor Information for %s :\n", item->d_name);
90 fflush(stdin);
Amit Arora97006e52010-10-28 11:56:08 +053091
Amit Arorae9e16b02010-08-03 10:15:20 +053092 while ((subitem = readdir(subdir))) {
93 if (subitem->d_name[0] == '.') /* skip hidden files */
94 continue;
95
96 if(!strncmp(subitem->d_name, "in", 2))
97 get_sensor_info(devpath, subitem->d_name, "in",
98 verbose);
99 else if (!strncmp(subitem->d_name, "temp", 4))
100 get_sensor_info(devpath, subitem->d_name,
101 "temp", verbose);
102 else if (!strncmp(subitem->d_name, "fan", 4))
103 get_sensor_info(devpath, subitem->d_name,
104 "fan", verbose);
105 else if (!strncmp(subitem->d_name, "pwm", 4))
106 get_sensor_info(devpath, subitem->d_name,
107 "pwm", verbose);
108
109 }
110
111 closedir(subdir);
112 }
113 closedir(dir);
114
Amit Arora1c037892010-08-05 13:46:10 +0530115 if(!found && verbose) {
116 printf("Could not find sensor information!");
117 printf(" Looks like /sys/class/hwmon is empty.\n");
118 }
119
Amit Arorae9e16b02010-08-03 10:15:20 +0530120 return 0;
121}
122
Amit Arora47fd9182010-08-24 13:26:06 +0530123void read_info_from_dirent(struct dirent *ritem, char *str, int idx)
124{
125 if (!strcmp(ritem->d_name, "name"))
126 strcpy(regulators_info[idx].name, str);
127 if (!strcmp(ritem->d_name, "state"))
128 strcpy(regulators_info[idx].state, str);
129 if (!strcmp(ritem->d_name, "status"))
130 strcpy(regulators_info[idx].status, str);
Amit Arorae9e16b02010-08-03 10:15:20 +0530131
Amit Arora47fd9182010-08-24 13:26:06 +0530132 if (!strcmp(ritem->d_name, "type"))
133 strcpy(regulators_info[idx].type, str);
134 if (!strcmp(ritem->d_name, "opmode"))
135 strcpy(regulators_info[idx].opmode, str);
136
137 if (!strcmp(ritem->d_name, "microvolts"))
138 regulators_info[idx].microvolts = atoi(str);
139 if (!strcmp(ritem->d_name, "min_microvolts"))
140 regulators_info[idx].min_microvolts = atoi(str);
141 if (!strcmp(ritem->d_name, "max_microvolts"))
142 regulators_info[idx].max_microvolts = atoi(str);
143
144 if (!strcmp(ritem->d_name, "microamps"))
145 regulators_info[idx].microamps = atoi(str);
146 if (!strcmp(ritem->d_name, "min_microamps"))
147 regulators_info[idx].min_microamps = atoi(str);
148 if (!strcmp(ritem->d_name, "max_microamps"))
149 regulators_info[idx].max_microamps = atoi(str);
150 if (!strcmp(ritem->d_name, "requested_microamps"))
151 regulators_info[idx].requested_microamps = atoi(str);
152
153 if (!strcmp(ritem->d_name, "num_users"))
154 regulators_info[idx].num_users = atoi(str);
155}
156
157int read_regulator_info(void)
Amit Arorae9e16b02010-08-03 10:15:20 +0530158{
159 FILE *file = NULL;
160 DIR *regdir, *dir;
161 int len, count = 0, ret = 0;
162 char line[1024], filename[1024], *fptr;
163 struct dirent *item, *ritem;
164
165 regdir = opendir("/sys/class/regulator");
166 if (!regdir)
167 return(1);
168 while((item = readdir(regdir))) {
169 if (strlen(item->d_name) < 3)
170 continue;
171
172 if (strncmp(item->d_name, "regulator", 9))
173 continue;
174
Amit Arora85fd4952010-08-05 14:04:50 +0530175 len = sprintf(filename, "/sys/class/regulator/%s",
176 item->d_name);
Amit Arorae9e16b02010-08-03 10:15:20 +0530177
178 dir = opendir(filename);
Amit Arora85fd4952010-08-05 14:04:50 +0530179 if (!dir)
Amit Arora7ef178c2010-08-03 15:57:21 +0530180 continue;
Amit Arorae9e16b02010-08-03 10:15:20 +0530181 count++;
Amit Arora97006e52010-10-28 11:56:08 +0530182
Amit Arorae9e16b02010-08-03 10:15:20 +0530183 if (count > numregulators) {
184 ret = 1;
Amit Arora85fd4952010-08-05 14:04:50 +0530185 goto exit;
Amit Arorae9e16b02010-08-03 10:15:20 +0530186 }
187
Amit Arorafccac912010-08-03 16:15:09 +0530188 strcpy(regulators_info[count-1].name, item->d_name);
Amit Arorae9e16b02010-08-03 10:15:20 +0530189 while((ritem = readdir(dir))) {
Amit Arorae9e16b02010-08-03 10:15:20 +0530190 if (strlen(ritem->d_name) < 3)
191 continue;
192
193 sprintf(filename + len, "/%s", ritem->d_name);
Amit Arorae9e16b02010-08-03 10:15:20 +0530194 file = fopen(filename, "r");
195 if (!file)
196 continue;
Amit Arorae9e16b02010-08-03 10:15:20 +0530197 memset(line, 0, 1024);
198 fptr = fgets(line, 1024, file);
199 fclose(file);
Amit Arora85fd4952010-08-05 14:04:50 +0530200 if (!fptr)
Amit Arora7ef178c2010-08-03 15:57:21 +0530201 continue;
Amit Arora6e774cd2010-10-28 11:31:24 +0530202 read_info_from_dirent(ritem, fptr, count - 1);
Amit Arorae9e16b02010-08-03 10:15:20 +0530203 }
Amit Arora85fd4952010-08-05 14:04:50 +0530204exit:
Amit Arorae9e16b02010-08-03 10:15:20 +0530205 closedir(dir);
206 if (ret)
207 break;
208 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530209 closedir(regdir);
210
211 return ret;
212}
213
214
215int main(int argc, char **argv)
216{
Amit Arorac93e0712010-10-07 13:51:53 +0530217 int c, i;
218 int firsttime[TOTAL_FEATURE_WINS];
Amit Arora3bd79162010-12-01 13:51:42 +0530219 int enter_hit = 0, verbose = 0, findparent_ncurses = 0;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530220 int regulators = 0, sensors = 0, clocks = 0, findparent = 0;
Amit Arora3bd79162010-12-01 13:51:42 +0530221 char clkarg[64], clkname_str[64];
Amit Arorae9e16b02010-08-03 10:15:20 +0530222
Amit Arora6e774cd2010-10-28 11:31:24 +0530223 for (i = 0; i < TOTAL_FEATURE_WINS; i++)
224 firsttime[i] = 1;
Amit Arorac93e0712010-10-07 13:51:53 +0530225
Amit Arorafefe8bf2010-08-05 13:31:20 +0530226 /*
227 * Options:
Amit Arora6e774cd2010-10-28 11:31:24 +0530228 * -r, --regulator : regulator
229 * -s, --sensor : sensors
230 * -c, --clock : clocks
Amit Aroraf4fb8102010-11-30 13:55:50 +0530231 * -p, --findparents : clockname whose parents have to be found
Amit Arora728e0c92010-09-14 12:06:09 +0530232 * -t, --time : ticktime
Amit Arora47fd9182010-08-24 13:26:06 +0530233 * -d, --dump : dump
Amit Arorafefe8bf2010-08-05 13:31:20 +0530234 * -v, --verbose : verbose
235 * -V, --version : version
236 * -h, --help : help
237 * no option / default : show usage!
Amit Arorae9e16b02010-08-03 10:15:20 +0530238 */
239
Amit Arorafefe8bf2010-08-05 13:31:20 +0530240 while (1) {
241 int optindex = 0;
242 static struct option long_options[] = {
Amit Arora6e774cd2010-10-28 11:31:24 +0530243 {"regulator", 0, 0, 'r'},
244 {"sensor", 0, 0, 's'},
245 {"clock", 0, 0, 'c'},
Amit Arora203f4d42010-12-01 13:52:01 +0530246 {"findparents", 1, 0, 'p'},
247 {"time", 1, 0, 't'},
Amit Arora47fd9182010-08-24 13:26:06 +0530248 {"dump", 0, 0, 'd'},
Amit Arorafefe8bf2010-08-05 13:31:20 +0530249 {"verbose", 0, 0, 'v'},
250 {"version", 0, 0, 'V'},
251 {"help", 0, 0, 'h'},
252 {0, 0, 0, 0}
253 };
254
Amit Aroraf4fb8102010-11-30 13:55:50 +0530255 c = getopt_long(argc, argv, "rscp:t:dvVh", long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +0530256 if (c == -1)
257 break;
258
259 switch (c) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530260 case 'r':
261 regulators = 1;
262 selectedwindow = REGULATOR;
263 break;
264 case 's':
265 sensors = 1;
266 selectedwindow = SENSOR;
267 break;
268 case 'c':
269 clocks = 1;
270 selectedwindow = CLOCK;
271 break;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530272 case 'p':
273 findparent = 1;
274 strcpy(clkarg, optarg);
275 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530276 case 't':
277 ticktime = strtod(optarg, NULL);
278 break;
279 case 'd':
280 dump = 1;
281 break;
282 case 'v':
283 verbose = 1;
284 break;
285 case 'V':
286 version();
287 break;
288 case 'h':
289 usage(argv);
290 break;
291 case '?':
292 fprintf (stderr, "%s: Unknown option %c'.\n",
293 argv[0], optopt);
294 exit(1);
295 default:
296 usage(argv);
297 break;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530298 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530299 }
300
Amit Arora6e774cd2010-10-28 11:31:24 +0530301 if (!dump && (regulators || clocks || sensors)) {
302 fprintf(stderr, "Option supported only in dump mode (-d)\n");
303 usage(argv);
304 }
Amit Arorafefe8bf2010-08-05 13:31:20 +0530305
Amit Aroraf4fb8102010-11-30 13:55:50 +0530306 if (findparent && (!clocks || !dump)) {
307 fprintf(stderr, "-p option passed without -c and -d."
308 " Exiting...\n");
309 usage(argv);
310 }
311
Amit Arora6e774cd2010-10-28 11:31:24 +0530312 if (!dump)
313 selectedwindow = REGULATOR;
Amit Arorae9e16b02010-08-03 10:15:20 +0530314
315 init_regulator_ds();
316
Amit Arora47fd9182010-08-24 13:26:06 +0530317 while(1) {
318 int key = 0;
319 struct timeval tval;
320 fd_set readfds;
Amit Arorae9e16b02010-08-03 10:15:20 +0530321
Amit Arora47fd9182010-08-24 13:26:06 +0530322 if (!dump) {
Amit Arorac93e0712010-10-07 13:51:53 +0530323 if(firsttime[0])
Amit Arora47fd9182010-08-24 13:26:06 +0530324 init_curses();
Amit Arora47fd9182010-08-24 13:26:06 +0530325 create_windows();
326 show_header();
327 }
Amit Aroraac4e8652010-11-09 11:16:53 +0530328
Amit Arora47fd9182010-08-24 13:26:06 +0530329
Amit Arorac93e0712010-10-07 13:51:53 +0530330 if (selectedwindow == REGULATOR) {
Amit Arora47fd9182010-08-24 13:26:06 +0530331 read_regulator_info();
Amit Arora728e0c92010-09-14 12:06:09 +0530332 if (!dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530333 create_selectedwindow();
Amit Arora47fd9182010-08-24 13:26:06 +0530334 show_regulator_info(verbose);
Amit Arora6e774cd2010-10-28 11:31:24 +0530335 }
Amit Arora47fd9182010-08-24 13:26:06 +0530336 else
337 print_regulator_info(verbose);
338 }
339
Amit Arora6e774cd2010-10-28 11:31:24 +0530340 if (selectedwindow == CLOCK) {
Amit Arora04f97742010-11-16 11:28:57 +0530341 int ret = 0;
Amit Arora6e774cd2010-10-28 11:31:24 +0530342 if (firsttime[CLOCK]) {
Amit Arora04f97742010-11-16 11:28:57 +0530343 ret = init_clock_details();
344 if (!ret)
345 firsttime[CLOCK] = 0;
Amit Arora3bd79162010-12-01 13:51:42 +0530346 strcpy(clkname_str, "");
Amit Arora6e774cd2010-10-28 11:31:24 +0530347 }
Amit Arora04f97742010-11-16 11:28:57 +0530348 if (!ret && !dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530349 int hrow;
Amit Arora0e512722010-10-01 12:24:16 +0530350
Amit Arora6e774cd2010-10-28 11:31:24 +0530351 create_selectedwindow();
Amit Arora3bd79162010-12-01 13:51:42 +0530352 if (!findparent_ncurses) {
353 hrow = read_and_print_clock_info(
354 verbose,
Amit Arora6e774cd2010-10-28 11:31:24 +0530355 highlighted_row,
356 enter_hit);
Amit Arora3bd79162010-12-01 13:51:42 +0530357 highlighted_row = hrow;
358 enter_hit = 0;
359 } else
360 find_parents_for_clock(clkname_str,
361 enter_hit);
Amit Arora04f97742010-11-16 11:28:57 +0530362 }
Amit Aroraf4fb8102010-11-30 13:55:50 +0530363 if (!ret && dump) {
364 if (findparent)
365 read_and_dump_clock_info_one(clkarg);
366 else
367 read_and_dump_clock_info(verbose);
368 }
Amit Arora6e774cd2010-10-28 11:31:24 +0530369 }
Amit Arora47fd9182010-08-24 13:26:06 +0530370
Amit Arorac93e0712010-10-07 13:51:53 +0530371 if (selectedwindow == SENSOR) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530372 if (!dump) {
373 create_selectedwindow();
374 print_sensor_header();
375 } else
376 read_and_print_sensor_info(verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530377 }
378
379 if (dump)
380 break;
381
382 FD_ZERO(&readfds);
383 FD_SET(0, &readfds);
384 tval.tv_sec = ticktime;
385 tval.tv_usec = (ticktime - tval.tv_sec) * 1000000;
386
387 key = select(1, &readfds, NULL, NULL, &tval);
388
389 if (key) {
390 char keychar;
Amit Arorac93e0712010-10-07 13:51:53 +0530391 int keystroke = getch();
Amit Arora3bd79162010-12-01 13:51:42 +0530392 int oldselectedwin = selectedwindow;
Amit Arora97006e52010-10-28 11:56:08 +0530393
Amit Arora47fd9182010-08-24 13:26:06 +0530394 if (keystroke == EOF)
395 exit(0);
396
Amit Arora6e774cd2010-10-28 11:31:24 +0530397 if (keystroke == KEY_RIGHT || keystroke == 9)
398 selectedwindow++;
Amit Arorac93e0712010-10-07 13:51:53 +0530399
Amit Arora6e774cd2010-10-28 11:31:24 +0530400 if (keystroke == KEY_LEFT || keystroke == 353)
401 selectedwindow--;
Amit Arorac93e0712010-10-07 13:51:53 +0530402
Amit Arora6e774cd2010-10-28 11:31:24 +0530403 if (selectedwindow >= TOTAL_FEATURE_WINS)
404 selectedwindow = 0;
Amit Arorac93e0712010-10-07 13:51:53 +0530405
Amit Arora6e774cd2010-10-28 11:31:24 +0530406 if (selectedwindow < 0)
407 selectedwindow = TOTAL_FEATURE_WINS - 1;
Amit Arorac93e0712010-10-07 13:51:53 +0530408
Amit Arora6e774cd2010-10-28 11:31:24 +0530409 if (selectedwindow == CLOCK) {
410 if (keystroke == KEY_DOWN)
411 highlighted_row++;
412 if (keystroke == KEY_UP && highlighted_row > 0)
413 highlighted_row--;
Amit Arora3bd79162010-12-01 13:51:42 +0530414 if (keystroke == 47)
415 findparent_ncurses = 1;
416
417 if ((keystroke == 27 || oldselectedwin !=
418 selectedwindow) && findparent_ncurses) {
419 findparent_ncurses = 0;
420 clkname_str[0] = '\0';
421 }
422
423 if (findparent_ncurses && keystroke != 13) {
424 int len = strlen(clkname_str);
425 char str[2];
426
427 if (keystroke == 263) {
428 if (len > 0)
429 len--;
430
431 clkname_str[len] = '\0';
432 } else {
433 if (strlen(clkname_str) ||
434 keystroke != '/') {
435 str[0] = keystroke;
436 str[1] = '\0';
437 if (len < 63)
438 strcat(clkname_str,
439 str);
440 }
441 }
442 }
Amit Arora6e774cd2010-10-28 11:31:24 +0530443 }
Amit Arora728e0c92010-09-14 12:06:09 +0530444
Amit Arora47fd9182010-08-24 13:26:06 +0530445 keychar = toupper(keystroke);
Amit Arora3bd79162010-12-01 13:51:42 +0530446//#define DEBUG
447#ifdef DEBUG
448 killall_windows(1); fini_curses();
449 printf("key entered %d:%c\n", keystroke, keychar);
450 exit(1);
451#endif
Amit Arorac93e0712010-10-07 13:51:53 +0530452
Amit Arora6e774cd2010-10-28 11:31:24 +0530453 if (keystroke == 13)
454 enter_hit = 1;
Amit Arora728e0c92010-09-14 12:06:09 +0530455
Amit Arora3bd79162010-12-01 13:51:42 +0530456 if (keychar == 'Q' && !findparent_ncurses)
Amit Arora47fd9182010-08-24 13:26:06 +0530457 exit(0);
458 if (keychar == 'R')
459 ticktime = 3;
460 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530461 }
Amit Arora85fd4952010-08-05 14:04:50 +0530462 exit(0);
Amit Arorae9e16b02010-08-03 10:15:20 +0530463}