blob: f67312cf4964729e2f35fd4febbe8918972af633 [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 Lezcano934fc092011-03-26 22:06:18 +0100145 /* No system specified to be dump, let's default to all */
146 if (!options->regulators && !options->clocks && !options->sensors)
147 options->regulators = options->clocks = options->sensors = true;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530148
Daniel Lezcanoc9c14622011-03-26 22:06:10 +0100149 if (options->selectedwindow == -1)
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100150 options->selectedwindow = CLOCK;
Amit Arorae9e16b02010-08-03 10:15:20 +0530151
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100152 return 0;
153}
154
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100155int keystroke_callback(bool *enter_hit, bool *findparent_ncurses,
156 char *clkname_str, bool *refreshwin,
Daniel Lezcano60a41022011-03-23 14:37:35 +0100157 struct powerdebug_options *options)
158{
159 char keychar;
160 int keystroke = getch();
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100161 int oldselectedwin = options->selectedwindow;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100162
163 if (keystroke == EOF)
164 exit(0);
165
166 if (keystroke == KEY_RIGHT || keystroke == 9)
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100167 options->selectedwindow++;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100168
169 if (keystroke == KEY_LEFT || keystroke == 353)
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100170 options->selectedwindow--;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100171
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100172 if (options->selectedwindow >= TOTAL_FEATURE_WINS)
173 options->selectedwindow = 0;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100174
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100175 if (options->selectedwindow < 0)
176 options->selectedwindow = TOTAL_FEATURE_WINS - 1;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100177
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100178 if (options->selectedwindow == CLOCK) {
Daniel Lezcano60a41022011-03-23 14:37:35 +0100179 if (keystroke == KEY_DOWN)
180 highlighted_row++;
181 if (keystroke == KEY_UP && highlighted_row > 0)
182 highlighted_row--;
183 if (keystroke == 47)
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100184 *findparent_ncurses = true;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100185
186 if ((keystroke == 27 || oldselectedwin !=
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100187 options->selectedwindow) && *findparent_ncurses) {
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100188 *findparent_ncurses = false;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100189 clkname_str[0] = '\0';
190 }
191
192 if (*findparent_ncurses && keystroke != 13) {
193 int len = strlen(clkname_str);
194 char str[2];
195
196 if (keystroke == 263) {
197 if (len > 0)
198 len--;
199
200 clkname_str[len] = '\0';
201 } else {
202 if (strlen(clkname_str) ||
203 keystroke != '/') {
204 str[0] = keystroke;
205 str[1] = '\0';
206 if (len < 63)
207 strcat(clkname_str,
208 str);
209 }
210 }
211 }
212 }
213
214 keychar = toupper(keystroke);
215//#define DEBUG
216#ifdef DEBUG
217 killall_windows(1); fini_curses();
218 printf("key entered %d:%c\n", keystroke, keychar);
219 exit(1);
220#endif
221
222 if (keystroke == 13)
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100223 *enter_hit = true;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100224
225 if (keychar == 'Q' && !*findparent_ncurses)
226 return 1;
227 if (keychar == 'R') {
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100228 *refreshwin = true;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100229 options->ticktime = 3;
230 } else
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100231 *refreshwin = false;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100232
233 return 0;
234}
235
Daniel Lezcano08198262011-03-26 22:06:02 +0100236int mainloop(struct powerdebug_options *options,
237 struct regulator_info *reg_info, int nr_reg)
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100238{
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100239 bool findparent_ncurses = false;
240 bool refreshwin = false;
241 bool enter_hit = false;
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100242 char clkname_str[64];
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100243
Daniel Lezcano934fc092011-03-26 22:06:18 +0100244 strcpy(clkname_str, "");
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100245
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600246 while (1) {
Amit Arora47fd9182010-08-24 13:26:06 +0530247 int key = 0;
248 struct timeval tval;
249 fd_set readfds;
Amit Arorae9e16b02010-08-03 10:15:20 +0530250
Daniel Lezcanob5746712011-03-26 22:06:05 +0100251 create_windows(options->selectedwindow);
252 show_header(options->selectedwindow);
Amit Aroraac4e8652010-11-09 11:16:53 +0530253
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100254 if (options->regulators || options->selectedwindow == REGULATOR) {
Daniel Lezcano08198262011-03-26 22:06:02 +0100255 regulator_read_info(reg_info, nr_reg);
Daniel Lezcanob5746712011-03-26 22:06:05 +0100256 create_selectedwindow(options->selectedwindow);
257 show_regulator_info(reg_info, nr_reg,
258 options->verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530259 }
260
Daniel Lezcano934fc092011-03-26 22:06:18 +0100261 if (options->selectedwindow == CLOCK) {
262
263 if (options->clocks) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530264 int hrow;
Amit Arora0e512722010-10-01 12:24:16 +0530265
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100266 create_selectedwindow(options->selectedwindow);
Amit Arora3bd79162010-12-01 13:51:42 +0530267 if (!findparent_ncurses) {
Amit Aroraa06a7302010-12-02 15:59:37 +0530268 int command = 0;
269
270 if (enter_hit)
271 command = CLOCK_SELECTED;
272 if (refreshwin)
273 command = REFRESH_WINDOW;
Amit Arora3bd79162010-12-01 13:51:42 +0530274 hrow = read_and_print_clock_info(
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100275 options->verbose,
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600276 highlighted_row,
277 command);
Amit Arora3bd79162010-12-01 13:51:42 +0530278 highlighted_row = hrow;
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100279 enter_hit = false;
Amit Arora3bd79162010-12-01 13:51:42 +0530280 } else
281 find_parents_for_clock(clkname_str,
Daniel Lezcano897f7332011-03-26 22:06:06 +0100282 enter_hit);
Amit Arora04f97742010-11-16 11:28:57 +0530283 }
Amit Arora6e774cd2010-10-28 11:31:24 +0530284 }
Amit Arora47fd9182010-08-24 13:26:06 +0530285
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100286 if (options->sensors || options->selectedwindow == SENSOR) {
Daniel Lezcanob5746712011-03-26 22:06:05 +0100287 create_selectedwindow(options->selectedwindow);
288 print_sensor_header();
Amit Arora47fd9182010-08-24 13:26:06 +0530289 }
290
Amit Arora47fd9182010-08-24 13:26:06 +0530291 FD_ZERO(&readfds);
292 FD_SET(0, &readfds);
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100293 tval.tv_sec = options->ticktime;
294 tval.tv_usec = (options->ticktime - tval.tv_sec) * 1000000;
Amit Arora47fd9182010-08-24 13:26:06 +0530295
296 key = select(1, &readfds, NULL, NULL, &tval);
Daniel Lezcano60a41022011-03-23 14:37:35 +0100297 if (!key)
298 continue;
Amit Arora47fd9182010-08-24 13:26:06 +0530299
Daniel Lezcano60a41022011-03-23 14:37:35 +0100300 if (keystroke_callback(&enter_hit, &findparent_ncurses,
301 clkname_str, &refreshwin, options))
302 break;
Amit Arora97006e52010-10-28 11:56:08 +0530303
Amit Arorae9e16b02010-08-03 10:15:20 +0530304 }
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100305
306 return 0;
307}
308
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100309static int powerdebug_dump(struct powerdebug_options *options,
310 struct regulator_info *reg_info, int nr_reg)
311{
Daniel Lezcanob5746712011-03-26 22:06:05 +0100312 if (options->regulators) {
313 regulator_read_info(reg_info, nr_reg);
314 regulator_print_info(reg_info, nr_reg, options->verbose);
315 }
316
317 if (options->clocks) {
318 init_clock_details(options->dump, options->selectedwindow);
319
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100320 if (options->clkname)
321 read_and_dump_clock_info_one(options->clkname,
Daniel Lezcanob5746712011-03-26 22:06:05 +0100322 options->dump);
323 else
324 read_and_dump_clock_info(options->verbose);
325 }
326
327 if (options->sensors)
328 read_and_print_sensor_info(options->verbose);
329
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100330 return 0;
331}
332
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100333static struct powerdebug_options *powerdebug_init(void)
334{
335 struct powerdebug_options *options;
336
337 options = malloc(sizeof(*options));
338 if (!options)
339 return NULL;
340
341 memset(options, 0, sizeof(*options));
342
343 return options;
344}
345
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100346int main(int argc, char **argv)
347{
348 struct powerdebug_options *options;
Daniel Lezcano08198262011-03-26 22:06:02 +0100349 struct regulator_info *regulators_info;
350 int numregulators;
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100351
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100352 options = powerdebug_init();
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100353 if (!options) {
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100354 fprintf(stderr, "not enough memory to allocate options\n");
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100355 return 1;
356 }
357
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +0100358 regulators_info = regulator_init(&numregulators);
359 if (!regulators_info) {
360 printf("not enough memory to allocate regulators info\n");
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100361 return 1;
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +0100362 }
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100363
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100364 if (getoptions(argc, argv, options)) {
365 usage();
366 return 1;
367 }
368
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100369 /* we just dump the informations */
370 if (options->dump) {
371 if (powerdebug_dump(options, regulators_info, numregulators))
372 return 1;
373 return 0;
374 }
375
Daniel Lezcanoca17a722011-03-26 22:06:16 +0100376 if (display_init()) {
377 printf("failed to initialize display\n");
378 return 1;
379 }
380
Daniel Lezcano934fc092011-03-26 22:06:18 +0100381 if (init_clock_details(options->dump, options->selectedwindow)) {
382 printf("failed initialize clock details\n");
383 options->clocks = false;
384 }
385
Daniel Lezcano08198262011-03-26 22:06:02 +0100386 if (mainloop(options, regulators_info, numregulators))
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100387 return 1;
388
389 return 0;
Amit Arorae9e16b02010-08-03 10:15:20 +0530390}