blob: d705bb98a40bcde6dd718c6704cb99b29c79abef [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"
Amit Arorae9e16b02010-08-03 10:15:20 +053019#include "powerdebug.h"
20
Amit Arora728e0c92010-09-14 12:06:09 +053021int highlighted_row;
Amit Arorae9e16b02010-08-03 10:15:20 +053022
Daniel Lezcano691d5562011-03-26 22:05:57 +010023static struct regulator_info *regulators_info;
24
Amit Arora422c52f2010-12-02 16:22:29 +053025void usage(void)
Amit Arorae9e16b02010-08-03 10:15:20 +053026{
Amit Arora422c52f2010-12-02 16:22:29 +053027 printf("Usage: powerdebug [OPTIONS]\n");
28 printf("\n");
29 printf("powerdebug -d [ -r ] [ -s ] [ -c [ -p <clock-name> ] ] "
30 "[ -v ]\n");
31 printf("powerdebug [ -r | -s | -c ]\n");
Amit Arora17552782010-12-02 12:23:14 +053032 printf(" -r, --regulator Show regulator information\n");
33 printf(" -s, --sensor Show sensor information\n");
34 printf(" -c, --clock Show clock information\n");
Amit Arora422c52f2010-12-02 16:22:29 +053035 printf(" -p, --findparents Show all parents for a particular"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060036 " clock\n");
Amit Arora17552782010-12-02 12:23:14 +053037 printf(" -t, --time Set ticktime in seconds (eg. 10.0)\n");
38 printf(" -d, --dump Dump information once (no refresh)\n");
Amit Arora422c52f2010-12-02 16:22:29 +053039 printf(" -v, --verbose Verbose mode (use with -r and/or"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060040 " -s)\n");
Amit Arora17552782010-12-02 12:23:14 +053041 printf(" -V, --version Show Version\n");
42 printf(" -h, --help Help\n");
Amit Arorae9e16b02010-08-03 10:15:20 +053043}
44
Amit Arora17552782010-12-02 12:23:14 +053045void version()
Amit Arorae9e16b02010-08-03 10:15:20 +053046{
Amit Arora17552782010-12-02 12:23:14 +053047 printf("powerdebug version %s\n", VERSION);
Amit Arorae9e16b02010-08-03 10:15:20 +053048}
49
Daniel Lezcano316bcae2011-03-23 14:37:30 +010050/*
51 * Options:
52 * -r, --regulator : regulator
53 * -s, --sensor : sensors
54 * -c, --clock : clocks
55 * -p, --findparents : clockname whose parents have to be found
56 * -t, --time : ticktime
57 * -d, --dump : dump
58 * -v, --verbose : verbose
59 * -V, --version : version
60 * -h, --help : help
61 * no option / default : show usage!
62 */
63
64static struct option long_options[] = {
65 { "regulator", 0, 0, 'r' },
66 { "sensor", 0, 0, 's' },
67 { "clock", 0, 0, 'c' },
68 { "findparents", 1, 0, 'p' },
69 { "time", 1, 0, 't' },
70 { "dump", 0, 0, 'd' },
71 { "verbose", 0, 0, 'v' },
72 { "version", 0, 0, 'V' },
73 { "help", 0, 0, 'h' },
74 { 0, 0, 0, 0 }
75};
76
77struct powerdebug_options {
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010078 bool verbose;
79 bool findparent;
80 bool regulators;
81 bool sensors;
82 bool clocks;
Daniel Lezcanoa70d9492011-03-23 14:37:40 +010083 bool dump;
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010084 unsigned int ticktime;
Daniel Lezcano558a6d52011-03-23 14:37:41 +010085 int selectedwindow;
Daniel Lezcano9420fde2011-03-23 14:37:31 +010086 char *clkarg;
Daniel Lezcano316bcae2011-03-23 14:37:30 +010087};
88
89int getoptions(int argc, char *argv[], struct powerdebug_options *options)
Amit Arorae9e16b02010-08-03 10:15:20 +053090{
Daniel Lezcano316bcae2011-03-23 14:37:30 +010091 int c;
Amit Arorae9e16b02010-08-03 10:15:20 +053092
Daniel Lezcano316bcae2011-03-23 14:37:30 +010093 memset(options, 0, sizeof(*options));
94 options->ticktime = 10;
Daniel Lezcano558a6d52011-03-23 14:37:41 +010095 options->selectedwindow = -1;
Amit Arorae9e16b02010-08-03 10:15:20 +053096
Amit Arorafefe8bf2010-08-05 13:31:20 +053097 while (1) {
98 int optindex = 0;
Amit Arorafefe8bf2010-08-05 13:31:20 +053099
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100100 c = getopt_long(argc, argv, "rscp:t:dvVh",
101 long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +0530102 if (c == -1)
103 break;
104
105 switch (c) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530106 case 'r':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100107 options->regulators = true;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100108 options->selectedwindow = REGULATOR;
Amit Arora6e774cd2010-10-28 11:31:24 +0530109 break;
110 case 's':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100111 options->sensors = true;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100112 options->selectedwindow = SENSOR;
Amit Arora6e774cd2010-10-28 11:31:24 +0530113 break;
114 case 'c':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100115 options->clocks = true;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100116 options->selectedwindow = CLOCK;
Amit Arora6e774cd2010-10-28 11:31:24 +0530117 break;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530118 case 'p':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100119 options->findparent = true;
Daniel Lezcano9420fde2011-03-23 14:37:31 +0100120 options->clkarg = strdup(optarg);
121 if (!options->clkarg) {
122 fprintf(stderr, "failed to allocate memory");
123 return -1;
124 }
Amit Kucheriaeab558a2011-03-25 09:51:41 +0200125 options->dump = true; /* Assume -dc in case of -p */
126 options->clocks = true;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530127 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530128 case 't':
Daniel Lezcano7f112da2011-03-23 14:37:32 +0100129 options->ticktime = atoi(optarg);
Amit Arora6e774cd2010-10-28 11:31:24 +0530130 break;
131 case 'd':
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100132 options->dump = true;
Amit Arora6e774cd2010-10-28 11:31:24 +0530133 break;
134 case 'v':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100135 options->verbose = true;
Amit Arora6e774cd2010-10-28 11:31:24 +0530136 break;
137 case 'V':
138 version();
139 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530140 case '?':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100141 fprintf(stderr, "%s: Unknown option %c'.\n",
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600142 argv[0], optopt);
Amit Arora6e774cd2010-10-28 11:31:24 +0530143 default:
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100144 return -1;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530145 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530146 }
Amit Aroraa06a7302010-12-02 15:59:37 +0530147
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100148 if (options->dump && !(options->regulators ||
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100149 options->clocks || options->sensors)) {
150 /* By Default lets show everything we have */
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100151 options->regulators = options->clocks = options->sensors = true;
Amit Aroraa06a7302010-12-02 15:59:37 +0530152 }
Amit Arorafefe8bf2010-08-05 13:31:20 +0530153
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100154 if (!options->dump && options->selectedwindow == -1)
155 options->selectedwindow = CLOCK;
Amit Arorae9e16b02010-08-03 10:15:20 +0530156
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100157 return 0;
158}
159
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100160int keystroke_callback(bool *enter_hit, bool *findparent_ncurses,
161 char *clkname_str, bool *refreshwin,
Daniel Lezcano60a41022011-03-23 14:37:35 +0100162 struct powerdebug_options *options)
163{
164 char keychar;
165 int keystroke = getch();
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100166 int oldselectedwin = options->selectedwindow;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100167
168 if (keystroke == EOF)
169 exit(0);
170
171 if (keystroke == KEY_RIGHT || keystroke == 9)
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100172 options->selectedwindow++;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100173
174 if (keystroke == KEY_LEFT || keystroke == 353)
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100175 options->selectedwindow--;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100176
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100177 if (options->selectedwindow >= TOTAL_FEATURE_WINS)
178 options->selectedwindow = 0;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100179
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100180 if (options->selectedwindow < 0)
181 options->selectedwindow = TOTAL_FEATURE_WINS - 1;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100182
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100183 if (options->selectedwindow == CLOCK) {
Daniel Lezcano60a41022011-03-23 14:37:35 +0100184 if (keystroke == KEY_DOWN)
185 highlighted_row++;
186 if (keystroke == KEY_UP && highlighted_row > 0)
187 highlighted_row--;
188 if (keystroke == 47)
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100189 *findparent_ncurses = true;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100190
191 if ((keystroke == 27 || oldselectedwin !=
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100192 options->selectedwindow) && *findparent_ncurses) {
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100193 *findparent_ncurses = false;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100194 clkname_str[0] = '\0';
195 }
196
197 if (*findparent_ncurses && keystroke != 13) {
198 int len = strlen(clkname_str);
199 char str[2];
200
201 if (keystroke == 263) {
202 if (len > 0)
203 len--;
204
205 clkname_str[len] = '\0';
206 } else {
207 if (strlen(clkname_str) ||
208 keystroke != '/') {
209 str[0] = keystroke;
210 str[1] = '\0';
211 if (len < 63)
212 strcat(clkname_str,
213 str);
214 }
215 }
216 }
217 }
218
219 keychar = toupper(keystroke);
220//#define DEBUG
221#ifdef DEBUG
222 killall_windows(1); fini_curses();
223 printf("key entered %d:%c\n", keystroke, keychar);
224 exit(1);
225#endif
226
227 if (keystroke == 13)
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100228 *enter_hit = true;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100229
230 if (keychar == 'Q' && !*findparent_ncurses)
231 return 1;
232 if (keychar == 'R') {
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100233 *refreshwin = true;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100234 options->ticktime = 3;
235 } else
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100236 *refreshwin = false;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100237
238 return 0;
239}
240
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100241int mainloop(struct powerdebug_options *options)
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100242{
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100243 bool findparent_ncurses = false;
244 bool refreshwin = false;
245 bool enter_hit = false;
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100246 int firsttime[TOTAL_FEATURE_WINS];
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100247 int i;
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100248 char clkname_str[64];
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100249
250 for (i = 0; i < TOTAL_FEATURE_WINS; i++)
251 firsttime[i] = 1;
252
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600253 while (1) {
Amit Arora47fd9182010-08-24 13:26:06 +0530254 int key = 0;
255 struct timeval tval;
256 fd_set readfds;
Amit Arorae9e16b02010-08-03 10:15:20 +0530257
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100258 if (!options->dump) {
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600259 if (firsttime[0])
Amit Arora47fd9182010-08-24 13:26:06 +0530260 init_curses();
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100261 create_windows(options->selectedwindow);
262 show_header(options->selectedwindow);
Amit Arora47fd9182010-08-24 13:26:06 +0530263 }
Amit Aroraac4e8652010-11-09 11:16:53 +0530264
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100265 if (options->regulators || options->selectedwindow == REGULATOR) {
Daniel Lezcano408580e2011-03-26 22:05:59 +0100266 regulator_read_info(regulators_info, numregulators);
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100267 if (!options->dump) {
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100268 create_selectedwindow(options->selectedwindow);
Daniel Lezcano3bd64f02011-03-26 22:05:56 +0100269 show_regulator_info(regulators_info, options->verbose);
Amit Arora6e774cd2010-10-28 11:31:24 +0530270 }
Amit Arora47fd9182010-08-24 13:26:06 +0530271 else
Daniel Lezcanofac6aea2011-03-26 22:05:58 +0100272 regulator_print_info(regulators_info, numregulators,
273 options->verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530274 }
275
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100276 if (options->clocks || options->selectedwindow == CLOCK) {
Amit Arora04f97742010-11-16 11:28:57 +0530277 int ret = 0;
Amit Arora6e774cd2010-10-28 11:31:24 +0530278 if (firsttime[CLOCK]) {
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100279 ret = init_clock_details(options->dump,
280 options->selectedwindow);
Amit Arora04f97742010-11-16 11:28:57 +0530281 if (!ret)
282 firsttime[CLOCK] = 0;
Amit Arora3bd79162010-12-01 13:51:42 +0530283 strcpy(clkname_str, "");
Amit Arora6e774cd2010-10-28 11:31:24 +0530284 }
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100285 if (!ret && !options->dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530286 int hrow;
Amit Arora0e512722010-10-01 12:24:16 +0530287
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100288 create_selectedwindow(options->selectedwindow);
Amit Arora3bd79162010-12-01 13:51:42 +0530289 if (!findparent_ncurses) {
Amit Aroraa06a7302010-12-02 15:59:37 +0530290 int command = 0;
291
292 if (enter_hit)
293 command = CLOCK_SELECTED;
294 if (refreshwin)
295 command = REFRESH_WINDOW;
Amit Arora3bd79162010-12-01 13:51:42 +0530296 hrow = read_and_print_clock_info(
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100297 options->verbose,
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600298 highlighted_row,
299 command);
Amit Arora3bd79162010-12-01 13:51:42 +0530300 highlighted_row = hrow;
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100301 enter_hit = false;
Amit Arora3bd79162010-12-01 13:51:42 +0530302 } else
303 find_parents_for_clock(clkname_str,
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100304 enter_hit,
305 options->dump);
Amit Arora04f97742010-11-16 11:28:57 +0530306 }
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100307 if (!ret && options->dump) {
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100308 if (options->findparent)
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100309 read_and_dump_clock_info_one(options->clkarg, options->dump);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530310 else
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100311 read_and_dump_clock_info(options->verbose);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530312 }
Amit Arora6e774cd2010-10-28 11:31:24 +0530313 }
Amit Arora47fd9182010-08-24 13:26:06 +0530314
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100315 if (options->sensors || options->selectedwindow == SENSOR) {
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100316 if (!options->dump) {
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100317 create_selectedwindow(options->selectedwindow);
Amit Arora6e774cd2010-10-28 11:31:24 +0530318 print_sensor_header();
319 } else
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100320 read_and_print_sensor_info(options->verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530321 }
322
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100323 if (options->dump)
Amit Arora47fd9182010-08-24 13:26:06 +0530324 break;
325
326 FD_ZERO(&readfds);
327 FD_SET(0, &readfds);
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100328 tval.tv_sec = options->ticktime;
329 tval.tv_usec = (options->ticktime - tval.tv_sec) * 1000000;
Amit Arora47fd9182010-08-24 13:26:06 +0530330
331 key = select(1, &readfds, NULL, NULL, &tval);
Daniel Lezcano60a41022011-03-23 14:37:35 +0100332 if (!key)
333 continue;
Amit Arora47fd9182010-08-24 13:26:06 +0530334
Daniel Lezcano60a41022011-03-23 14:37:35 +0100335 if (keystroke_callback(&enter_hit, &findparent_ncurses,
336 clkname_str, &refreshwin, options))
337 break;
Amit Arora97006e52010-10-28 11:56:08 +0530338
Amit Arorae9e16b02010-08-03 10:15:20 +0530339 }
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100340
341 return 0;
342}
343
344int main(int argc, char **argv)
345{
346 struct powerdebug_options *options;
347
348 options = malloc(sizeof(*options));
349 if (!options) {
350 fprintf(stderr, "failed to allocated memory\n");
351 return -1;
352 }
353
354 if (getoptions(argc, argv, options)) {
355 usage();
356 return 1;
357 }
358
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +0100359 regulators_info = regulator_init(&numregulators);
360 if (!regulators_info) {
361 printf("not enough memory to allocate regulators info\n");
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100362 return 1;
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +0100363 }
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100364
365 if (mainloop(options))
366 return 1;
367
368 return 0;
Amit Arorae9e16b02010-08-03 10:15:20 +0530369}