blob: 215b0fe118e1a9e82815a5d950ca880932e9a987 [file] [log] [blame]
Amit Arora39f29542010-09-14 12:03:22 +05301/*******************************************************************************
Amit Kucheriac0e17fc2011-01-17 09:35:52 +02002 * Copyright (C) 2010, Linaro Limited.
Amit Arora39f29542010-09-14 12:03:22 +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
14 *******************************************************************************/
15
Amit Arorae9e16b02010-08-03 10:15:20 +053016#include <getopt.h>
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010017#include <stdbool.h>
Daniel Lezcanocac52d92011-03-26 22:05:49 +010018#include "regulator.h"
Daniel Lezcano192c1d22011-03-26 22:06:14 +010019#include "display.h"
Amit Arorae9e16b02010-08-03 10:15:20 +053020#include "powerdebug.h"
21
Daniel Lezcano20be6782011-03-26 22:06:03 +010022static int highlighted_row;
Amit Arorae9e16b02010-08-03 10:15:20 +053023
Amit Arora422c52f2010-12-02 16:22:29 +053024void usage(void)
Amit Arorae9e16b02010-08-03 10:15:20 +053025{
Amit Arora422c52f2010-12-02 16:22:29 +053026 printf("Usage: powerdebug [OPTIONS]\n");
27 printf("\n");
28 printf("powerdebug -d [ -r ] [ -s ] [ -c [ -p <clock-name> ] ] "
29 "[ -v ]\n");
30 printf("powerdebug [ -r | -s | -c ]\n");
Amit Arora17552782010-12-02 12:23:14 +053031 printf(" -r, --regulator Show regulator information\n");
32 printf(" -s, --sensor Show sensor information\n");
33 printf(" -c, --clock Show clock information\n");
Amit Arora422c52f2010-12-02 16:22:29 +053034 printf(" -p, --findparents Show all parents for a particular"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060035 " clock\n");
Amit Arora17552782010-12-02 12:23:14 +053036 printf(" -t, --time Set ticktime in seconds (eg. 10.0)\n");
37 printf(" -d, --dump Dump information once (no refresh)\n");
Amit Arora422c52f2010-12-02 16:22:29 +053038 printf(" -v, --verbose Verbose mode (use with -r and/or"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060039 " -s)\n");
Amit Arora17552782010-12-02 12:23:14 +053040 printf(" -V, --version Show Version\n");
41 printf(" -h, --help Help\n");
Amit Arorae9e16b02010-08-03 10:15:20 +053042}
43
Amit Arora17552782010-12-02 12:23:14 +053044void version()
Amit Arorae9e16b02010-08-03 10:15:20 +053045{
Amit Arora17552782010-12-02 12:23:14 +053046 printf("powerdebug version %s\n", VERSION);
Amit Arorae9e16b02010-08-03 10:15:20 +053047}
48
Daniel Lezcano316bcae2011-03-23 14:37:30 +010049/*
50 * Options:
51 * -r, --regulator : regulator
52 * -s, --sensor : sensors
53 * -c, --clock : clocks
54 * -p, --findparents : clockname whose parents have to be found
55 * -t, --time : ticktime
56 * -d, --dump : dump
57 * -v, --verbose : verbose
58 * -V, --version : version
59 * -h, --help : help
60 * no option / default : show usage!
61 */
62
63static struct option long_options[] = {
64 { "regulator", 0, 0, 'r' },
65 { "sensor", 0, 0, 's' },
66 { "clock", 0, 0, 'c' },
67 { "findparents", 1, 0, 'p' },
68 { "time", 1, 0, 't' },
69 { "dump", 0, 0, 'd' },
70 { "verbose", 0, 0, 'v' },
71 { "version", 0, 0, 'V' },
72 { "help", 0, 0, 'h' },
73 { 0, 0, 0, 0 }
74};
75
76struct powerdebug_options {
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010077 bool verbose;
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010078 bool regulators;
79 bool sensors;
80 bool clocks;
Daniel Lezcanoa70d9492011-03-23 14:37:40 +010081 bool dump;
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010082 unsigned int ticktime;
Daniel Lezcano558a6d52011-03-23 14:37:41 +010083 int selectedwindow;
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +010084 char *clkname;
Daniel Lezcano316bcae2011-03-23 14:37:30 +010085};
86
87int getoptions(int argc, char *argv[], struct powerdebug_options *options)
Amit Arorae9e16b02010-08-03 10:15:20 +053088{
Daniel Lezcano316bcae2011-03-23 14:37:30 +010089 int c;
Amit Arorae9e16b02010-08-03 10:15:20 +053090
Daniel Lezcano316bcae2011-03-23 14:37:30 +010091 memset(options, 0, sizeof(*options));
92 options->ticktime = 10;
Daniel Lezcano558a6d52011-03-23 14:37:41 +010093 options->selectedwindow = -1;
Amit Arorae9e16b02010-08-03 10:15:20 +053094
Amit Arorafefe8bf2010-08-05 13:31:20 +053095 while (1) {
96 int optindex = 0;
Amit Arorafefe8bf2010-08-05 13:31:20 +053097
Daniel Lezcano316bcae2011-03-23 14:37:30 +010098 c = getopt_long(argc, argv, "rscp:t:dvVh",
99 long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +0530100 if (c == -1)
101 break;
102
103 switch (c) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530104 case 'r':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100105 options->regulators = true;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100106 options->selectedwindow = REGULATOR;
Amit Arora6e774cd2010-10-28 11:31:24 +0530107 break;
108 case 's':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100109 options->sensors = true;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100110 options->selectedwindow = SENSOR;
Amit Arora6e774cd2010-10-28 11:31:24 +0530111 break;
112 case 'c':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100113 options->clocks = true;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100114 options->selectedwindow = CLOCK;
Amit Arora6e774cd2010-10-28 11:31:24 +0530115 break;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530116 case 'p':
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100117 options->clkname = strdup(optarg);
118 if (!options->clkname) {
Daniel Lezcano9420fde2011-03-23 14:37:31 +0100119 fprintf(stderr, "failed to allocate memory");
120 return -1;
121 }
Amit Kucheriaeab558a2011-03-25 09:51:41 +0200122 options->dump = true; /* Assume -dc in case of -p */
123 options->clocks = true;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530124 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530125 case 't':
Daniel Lezcano7f112da2011-03-23 14:37:32 +0100126 options->ticktime = atoi(optarg);
Amit Arora6e774cd2010-10-28 11:31:24 +0530127 break;
128 case 'd':
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100129 options->dump = true;
Amit Arora6e774cd2010-10-28 11:31:24 +0530130 break;
131 case 'v':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100132 options->verbose = true;
Amit Arora6e774cd2010-10-28 11:31:24 +0530133 break;
134 case 'V':
135 version();
136 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530137 case '?':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100138 fprintf(stderr, "%s: Unknown option %c'.\n",
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600139 argv[0], optopt);
Amit Arora6e774cd2010-10-28 11:31:24 +0530140 default:
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100141 return -1;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530142 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530143 }
Amit Aroraa06a7302010-12-02 15:59:37 +0530144
Daniel Lezcanoc9c14622011-03-26 22:06:10 +0100145 if (options->dump) {
146
147 /* No system specified to be dump, let's default to all */
148 if (!options->regulators &&
149 !options->clocks &&
150 !options->sensors) {
151 options->regulators = options->clocks =
152 options->sensors = true;
153
154 return 0;
155 }
156
Amit Aroraa06a7302010-12-02 15:59:37 +0530157 }
Amit Arorafefe8bf2010-08-05 13:31:20 +0530158
Daniel Lezcanoc9c14622011-03-26 22:06:10 +0100159 if (options->selectedwindow == -1)
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100160 options->selectedwindow = CLOCK;
Amit Arorae9e16b02010-08-03 10:15:20 +0530161
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100162 return 0;
163}
164
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100165int keystroke_callback(bool *enter_hit, bool *findparent_ncurses,
166 char *clkname_str, bool *refreshwin,
Daniel Lezcano60a41022011-03-23 14:37:35 +0100167 struct powerdebug_options *options)
168{
169 char keychar;
170 int keystroke = getch();
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100171 int oldselectedwin = options->selectedwindow;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100172
173 if (keystroke == EOF)
174 exit(0);
175
176 if (keystroke == KEY_RIGHT || keystroke == 9)
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100177 options->selectedwindow++;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100178
179 if (keystroke == KEY_LEFT || keystroke == 353)
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100180 options->selectedwindow--;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100181
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100182 if (options->selectedwindow >= TOTAL_FEATURE_WINS)
183 options->selectedwindow = 0;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100184
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100185 if (options->selectedwindow < 0)
186 options->selectedwindow = TOTAL_FEATURE_WINS - 1;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100187
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100188 if (options->selectedwindow == CLOCK) {
Daniel Lezcano60a41022011-03-23 14:37:35 +0100189 if (keystroke == KEY_DOWN)
190 highlighted_row++;
191 if (keystroke == KEY_UP && highlighted_row > 0)
192 highlighted_row--;
193 if (keystroke == 47)
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100194 *findparent_ncurses = true;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100195
196 if ((keystroke == 27 || oldselectedwin !=
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100197 options->selectedwindow) && *findparent_ncurses) {
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100198 *findparent_ncurses = false;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100199 clkname_str[0] = '\0';
200 }
201
202 if (*findparent_ncurses && keystroke != 13) {
203 int len = strlen(clkname_str);
204 char str[2];
205
206 if (keystroke == 263) {
207 if (len > 0)
208 len--;
209
210 clkname_str[len] = '\0';
211 } else {
212 if (strlen(clkname_str) ||
213 keystroke != '/') {
214 str[0] = keystroke;
215 str[1] = '\0';
216 if (len < 63)
217 strcat(clkname_str,
218 str);
219 }
220 }
221 }
222 }
223
224 keychar = toupper(keystroke);
225//#define DEBUG
226#ifdef DEBUG
227 killall_windows(1); fini_curses();
228 printf("key entered %d:%c\n", keystroke, keychar);
229 exit(1);
230#endif
231
232 if (keystroke == 13)
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100233 *enter_hit = true;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100234
235 if (keychar == 'Q' && !*findparent_ncurses)
236 return 1;
237 if (keychar == 'R') {
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100238 *refreshwin = true;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100239 options->ticktime = 3;
240 } else
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100241 *refreshwin = false;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100242
243 return 0;
244}
245
Daniel Lezcano08198262011-03-26 22:06:02 +0100246int mainloop(struct powerdebug_options *options,
247 struct regulator_info *reg_info, int nr_reg)
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100248{
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100249 bool findparent_ncurses = false;
250 bool refreshwin = false;
251 bool enter_hit = false;
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100252 int firsttime[TOTAL_FEATURE_WINS];
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100253 int i;
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100254 char clkname_str[64];
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100255
256 for (i = 0; i < TOTAL_FEATURE_WINS; i++)
257 firsttime[i] = 1;
258
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600259 while (1) {
Amit Arora47fd9182010-08-24 13:26:06 +0530260 int key = 0;
261 struct timeval tval;
262 fd_set readfds;
Amit Arorae9e16b02010-08-03 10:15:20 +0530263
Daniel Lezcanoca17a722011-03-26 22:06:16 +0100264 if (firsttime[0] && display_init())
265 return -1;
266
Daniel Lezcanob5746712011-03-26 22:06:05 +0100267 create_windows(options->selectedwindow);
268 show_header(options->selectedwindow);
Amit Aroraac4e8652010-11-09 11:16:53 +0530269
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100270 if (options->regulators || options->selectedwindow == REGULATOR) {
Daniel Lezcano08198262011-03-26 22:06:02 +0100271 regulator_read_info(reg_info, nr_reg);
Daniel Lezcanob5746712011-03-26 22:06:05 +0100272 create_selectedwindow(options->selectedwindow);
273 show_regulator_info(reg_info, nr_reg,
274 options->verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530275 }
276
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100277 if (options->clocks || options->selectedwindow == CLOCK) {
Amit Arora04f97742010-11-16 11:28:57 +0530278 int ret = 0;
Amit Arora6e774cd2010-10-28 11:31:24 +0530279 if (firsttime[CLOCK]) {
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100280 ret = init_clock_details(options->dump,
281 options->selectedwindow);
Amit Arora04f97742010-11-16 11:28:57 +0530282 if (!ret)
283 firsttime[CLOCK] = 0;
Amit Arora3bd79162010-12-01 13:51:42 +0530284 strcpy(clkname_str, "");
Amit Arora6e774cd2010-10-28 11:31:24 +0530285 }
Daniel Lezcanob5746712011-03-26 22:06:05 +0100286 if (!ret) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530287 int hrow;
Amit Arora0e512722010-10-01 12:24:16 +0530288
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100289 create_selectedwindow(options->selectedwindow);
Amit Arora3bd79162010-12-01 13:51:42 +0530290 if (!findparent_ncurses) {
Amit Aroraa06a7302010-12-02 15:59:37 +0530291 int command = 0;
292
293 if (enter_hit)
294 command = CLOCK_SELECTED;
295 if (refreshwin)
296 command = REFRESH_WINDOW;
Amit Arora3bd79162010-12-01 13:51:42 +0530297 hrow = read_and_print_clock_info(
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100298 options->verbose,
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600299 highlighted_row,
300 command);
Amit Arora3bd79162010-12-01 13:51:42 +0530301 highlighted_row = hrow;
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100302 enter_hit = false;
Amit Arora3bd79162010-12-01 13:51:42 +0530303 } else
304 find_parents_for_clock(clkname_str,
Daniel Lezcano897f7332011-03-26 22:06:06 +0100305 enter_hit);
Amit Arora04f97742010-11-16 11:28:57 +0530306 }
Amit Arora6e774cd2010-10-28 11:31:24 +0530307 }
Amit Arora47fd9182010-08-24 13:26:06 +0530308
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100309 if (options->sensors || options->selectedwindow == SENSOR) {
Daniel Lezcanob5746712011-03-26 22:06:05 +0100310 create_selectedwindow(options->selectedwindow);
311 print_sensor_header();
Amit Arora47fd9182010-08-24 13:26:06 +0530312 }
313
Amit Arora47fd9182010-08-24 13:26:06 +0530314 FD_ZERO(&readfds);
315 FD_SET(0, &readfds);
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100316 tval.tv_sec = options->ticktime;
317 tval.tv_usec = (options->ticktime - tval.tv_sec) * 1000000;
Amit Arora47fd9182010-08-24 13:26:06 +0530318
319 key = select(1, &readfds, NULL, NULL, &tval);
Daniel Lezcano60a41022011-03-23 14:37:35 +0100320 if (!key)
321 continue;
Amit Arora47fd9182010-08-24 13:26:06 +0530322
Daniel Lezcano60a41022011-03-23 14:37:35 +0100323 if (keystroke_callback(&enter_hit, &findparent_ncurses,
324 clkname_str, &refreshwin, options))
325 break;
Amit Arora97006e52010-10-28 11:56:08 +0530326
Amit Arorae9e16b02010-08-03 10:15:20 +0530327 }
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100328
329 return 0;
330}
331
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100332static int powerdebug_dump(struct powerdebug_options *options,
333 struct regulator_info *reg_info, int nr_reg)
334{
Daniel Lezcanob5746712011-03-26 22:06:05 +0100335 if (options->regulators) {
336 regulator_read_info(reg_info, nr_reg);
337 regulator_print_info(reg_info, nr_reg, options->verbose);
338 }
339
340 if (options->clocks) {
341 init_clock_details(options->dump, options->selectedwindow);
342
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100343 if (options->clkname)
344 read_and_dump_clock_info_one(options->clkname,
Daniel Lezcanob5746712011-03-26 22:06:05 +0100345 options->dump);
346 else
347 read_and_dump_clock_info(options->verbose);
348 }
349
350 if (options->sensors)
351 read_and_print_sensor_info(options->verbose);
352
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100353 return 0;
354}
355
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100356static struct powerdebug_options *powerdebug_init(void)
357{
358 struct powerdebug_options *options;
359
360 options = malloc(sizeof(*options));
361 if (!options)
362 return NULL;
363
364 memset(options, 0, sizeof(*options));
365
366 return options;
367}
368
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100369int main(int argc, char **argv)
370{
371 struct powerdebug_options *options;
Daniel Lezcano08198262011-03-26 22:06:02 +0100372 struct regulator_info *regulators_info;
373 int numregulators;
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100374
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100375 options = powerdebug_init();
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100376 if (!options) {
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100377 fprintf(stderr, "not enough memory to allocate options\n");
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100378 return 1;
379 }
380
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +0100381 regulators_info = regulator_init(&numregulators);
382 if (!regulators_info) {
383 printf("not enough memory to allocate regulators info\n");
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100384 return 1;
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +0100385 }
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100386
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100387 if (getoptions(argc, argv, options)) {
388 usage();
389 return 1;
390 }
391
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100392 /* we just dump the informations */
393 if (options->dump) {
394 if (powerdebug_dump(options, regulators_info, numregulators))
395 return 1;
396 return 0;
397 }
398
Daniel Lezcanoca17a722011-03-26 22:06:16 +0100399 if (display_init()) {
400 printf("failed to initialize display\n");
401 return 1;
402 }
403
Daniel Lezcano08198262011-03-26 22:06:02 +0100404 if (mainloop(options, regulators_info, numregulators))
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100405 return 1;
406
407 return 0;
Amit Arorae9e16b02010-08-03 10:15:20 +0530408}