blob: 7fc1f14cf1c78254573fb352455ba8346e3e0d63 [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 Arora6e774cd2010-10-28 11:31:24 +0530219 int enter_hit = 0;
Amit Arora728e0c92010-09-14 12:06:09 +0530220 int regulators = 0, sensors = 0, clocks = 0, verbose = 0;
Amit Arorae9e16b02010-08-03 10:15:20 +0530221
Amit Arora6e774cd2010-10-28 11:31:24 +0530222 for (i = 0; i < TOTAL_FEATURE_WINS; i++)
223 firsttime[i] = 1;
Amit Arorac93e0712010-10-07 13:51:53 +0530224
Amit Arorafefe8bf2010-08-05 13:31:20 +0530225 /*
226 * Options:
Amit Arora6e774cd2010-10-28 11:31:24 +0530227 * -r, --regulator : regulator
228 * -s, --sensor : sensors
229 * -c, --clock : clocks
Amit Arora728e0c92010-09-14 12:06:09 +0530230 * -t, --time : ticktime
Amit Arora47fd9182010-08-24 13:26:06 +0530231 * -d, --dump : dump
Amit Arorafefe8bf2010-08-05 13:31:20 +0530232 * -v, --verbose : verbose
233 * -V, --version : version
234 * -h, --help : help
235 * no option / default : show usage!
Amit Arorae9e16b02010-08-03 10:15:20 +0530236 */
237
Amit Arorafefe8bf2010-08-05 13:31:20 +0530238 while (1) {
239 int optindex = 0;
240 static struct option long_options[] = {
Amit Arora6e774cd2010-10-28 11:31:24 +0530241 {"regulator", 0, 0, 'r'},
242 {"sensor", 0, 0, 's'},
243 {"clock", 0, 0, 'c'},
Amit Arora728e0c92010-09-14 12:06:09 +0530244 {"time", 0, 0, 't'},
Amit Arora47fd9182010-08-24 13:26:06 +0530245 {"dump", 0, 0, 'd'},
Amit Arorafefe8bf2010-08-05 13:31:20 +0530246 {"verbose", 0, 0, 'v'},
247 {"version", 0, 0, 'V'},
248 {"help", 0, 0, 'h'},
249 {0, 0, 0, 0}
250 };
251
Amit Arora728e0c92010-09-14 12:06:09 +0530252 c = getopt_long(argc, argv, "rsct:dvVh", long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +0530253 if (c == -1)
254 break;
255
256 switch (c) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530257 case 'r':
258 regulators = 1;
259 selectedwindow = REGULATOR;
260 break;
261 case 's':
262 sensors = 1;
263 selectedwindow = SENSOR;
264 break;
265 case 'c':
266 clocks = 1;
267 selectedwindow = CLOCK;
268 break;
269 case 't':
270 ticktime = strtod(optarg, NULL);
271 break;
272 case 'd':
273 dump = 1;
274 break;
275 case 'v':
276 verbose = 1;
277 break;
278 case 'V':
279 version();
280 break;
281 case 'h':
282 usage(argv);
283 break;
284 case '?':
285 fprintf (stderr, "%s: Unknown option %c'.\n",
286 argv[0], optopt);
287 exit(1);
288 default:
289 usage(argv);
290 break;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530291 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530292 }
293
Amit Arora6e774cd2010-10-28 11:31:24 +0530294 if (!dump && (regulators || clocks || sensors)) {
295 fprintf(stderr, "Option supported only in dump mode (-d)\n");
296 usage(argv);
297 }
Amit Arorafefe8bf2010-08-05 13:31:20 +0530298
Amit Arora6e774cd2010-10-28 11:31:24 +0530299 if (!dump)
300 selectedwindow = REGULATOR;
Amit Arorae9e16b02010-08-03 10:15:20 +0530301
302 init_regulator_ds();
303
Amit Arora47fd9182010-08-24 13:26:06 +0530304 while(1) {
305 int key = 0;
306 struct timeval tval;
307 fd_set readfds;
Amit Arorae9e16b02010-08-03 10:15:20 +0530308
Amit Arora47fd9182010-08-24 13:26:06 +0530309 if (!dump) {
Amit Arorac93e0712010-10-07 13:51:53 +0530310 if(firsttime[0])
Amit Arora47fd9182010-08-24 13:26:06 +0530311 init_curses();
Amit Arora47fd9182010-08-24 13:26:06 +0530312 create_windows();
313 show_header();
314 }
Amit Aroraac4e8652010-11-09 11:16:53 +0530315
Amit Arora47fd9182010-08-24 13:26:06 +0530316
Amit Arorac93e0712010-10-07 13:51:53 +0530317 if (selectedwindow == REGULATOR) {
Amit Arora47fd9182010-08-24 13:26:06 +0530318 read_regulator_info();
Amit Arora728e0c92010-09-14 12:06:09 +0530319 if (!dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530320 create_selectedwindow();
Amit Arora47fd9182010-08-24 13:26:06 +0530321 show_regulator_info(verbose);
Amit Arora6e774cd2010-10-28 11:31:24 +0530322 }
Amit Arora47fd9182010-08-24 13:26:06 +0530323 else
324 print_regulator_info(verbose);
325 }
326
Amit Arora6e774cd2010-10-28 11:31:24 +0530327 if (selectedwindow == CLOCK) {
328 if (firsttime[CLOCK]) {
329 init_clock_details();
330 firsttime[CLOCK] = 0;
331 }
332 if (!dump) {
333 int hrow;
Amit Arora0e512722010-10-01 12:24:16 +0530334
Amit Arora6e774cd2010-10-28 11:31:24 +0530335 create_selectedwindow();
336 hrow = read_and_print_clock_info(verbose,
337 highlighted_row,
338 enter_hit);
339 highlighted_row = hrow;
340 enter_hit = 0;
341 } else
Amit Aroraac4e8652010-11-09 11:16:53 +0530342 read_and_dump_clock_info(verbose);
Amit Arora6e774cd2010-10-28 11:31:24 +0530343 }
Amit Arora47fd9182010-08-24 13:26:06 +0530344
Amit Arorac93e0712010-10-07 13:51:53 +0530345 if (selectedwindow == SENSOR) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530346 if (!dump) {
347 create_selectedwindow();
348 print_sensor_header();
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;
Amit Arorac93e0712010-10-07 13:51:53 +0530365 int keystroke = getch();
Amit Arora97006e52010-10-28 11:56:08 +0530366
Amit Arora47fd9182010-08-24 13:26:06 +0530367 if (keystroke == EOF)
368 exit(0);
369
Amit Arora6e774cd2010-10-28 11:31:24 +0530370 if (keystroke == KEY_RIGHT || keystroke == 9)
371 selectedwindow++;
Amit Arorac93e0712010-10-07 13:51:53 +0530372
Amit Arora6e774cd2010-10-28 11:31:24 +0530373 if (keystroke == KEY_LEFT || keystroke == 353)
374 selectedwindow--;
Amit Arorac93e0712010-10-07 13:51:53 +0530375
Amit Arora6e774cd2010-10-28 11:31:24 +0530376 if (selectedwindow >= TOTAL_FEATURE_WINS)
377 selectedwindow = 0;
Amit Arorac93e0712010-10-07 13:51:53 +0530378
Amit Arora6e774cd2010-10-28 11:31:24 +0530379 if (selectedwindow < 0)
380 selectedwindow = TOTAL_FEATURE_WINS - 1;
Amit Arorac93e0712010-10-07 13:51:53 +0530381
Amit Arora6e774cd2010-10-28 11:31:24 +0530382 if (selectedwindow == CLOCK) {
383 if (keystroke == KEY_DOWN)
384 highlighted_row++;
385 if (keystroke == KEY_UP && highlighted_row > 0)
386 highlighted_row--;
387 }
Amit Arora728e0c92010-09-14 12:06:09 +0530388
Amit Arora47fd9182010-08-24 13:26:06 +0530389 keychar = toupper(keystroke);
Amit Arorac93e0712010-10-07 13:51:53 +0530390
Amit Arora6e774cd2010-10-28 11:31:24 +0530391 //killall_windows(1); fini_curses();
392 //printf("key entered %d:%c\n", keystroke, keychar);
393 //exit(1);
Amit Arorac93e0712010-10-07 13:51:53 +0530394
Amit Arora6e774cd2010-10-28 11:31:24 +0530395 if (keystroke == 13)
396 enter_hit = 1;
Amit Arora728e0c92010-09-14 12:06:09 +0530397
Amit Arora47fd9182010-08-24 13:26:06 +0530398 if (keychar == 'Q')
399 exit(0);
400 if (keychar == 'R')
401 ticktime = 3;
402 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530403 }
Amit Arora85fd4952010-08-05 14:04:50 +0530404 exit(0);
Amit Arorae9e16b02010-08-03 10:15:20 +0530405}