blob: 9f36b6346bc798d7163fa353a4acb215913b3868 [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>
Amit Arorae9e16b02010-08-03 10:15:20 +053017#include "powerdebug.h"
18
Amit Arora47fd9182010-08-24 13:26:06 +053019int dump;
Amit Arora728e0c92010-09-14 12:06:09 +053020int highlighted_row;
Amit Arorac93e0712010-10-07 13:51:53 +053021int selectedwindow = -1;
Amit Arorae9e16b02010-08-03 10:15:20 +053022
Amit Arorac93e0712010-10-07 13:51:53 +053023char *win_names[TOTAL_FEATURE_WINS] = {
Amit Arora6e774cd2010-10-28 11:31:24 +053024 "Clocks",
Amit Kucheriaee851a12011-01-12 04:57:48 +053025 "Regulators",
26 "Sensors"
27};
Amit Arorac93e0712010-10-07 13:51:53 +053028
Amit Arora422c52f2010-12-02 16:22:29 +053029void usage(void)
Amit Arorae9e16b02010-08-03 10:15:20 +053030{
Amit Arora422c52f2010-12-02 16:22:29 +053031 printf("Usage: powerdebug [OPTIONS]\n");
32 printf("\n");
33 printf("powerdebug -d [ -r ] [ -s ] [ -c [ -p <clock-name> ] ] "
34 "[ -v ]\n");
35 printf("powerdebug [ -r | -s | -c ]\n");
Amit Arora17552782010-12-02 12:23:14 +053036 printf(" -r, --regulator Show regulator information\n");
37 printf(" -s, --sensor Show sensor information\n");
38 printf(" -c, --clock Show clock information\n");
Amit Arora422c52f2010-12-02 16:22:29 +053039 printf(" -p, --findparents Show all parents for a particular"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060040 " clock\n");
Amit Arora17552782010-12-02 12:23:14 +053041 printf(" -t, --time Set ticktime in seconds (eg. 10.0)\n");
42 printf(" -d, --dump Dump information once (no refresh)\n");
Amit Arora422c52f2010-12-02 16:22:29 +053043 printf(" -v, --verbose Verbose mode (use with -r and/or"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060044 " -s)\n");
Amit Arora17552782010-12-02 12:23:14 +053045 printf(" -V, --version Show Version\n");
46 printf(" -h, --help Help\n");
Amit Arorae9e16b02010-08-03 10:15:20 +053047}
48
Amit Arora17552782010-12-02 12:23:14 +053049void version()
Amit Arorae9e16b02010-08-03 10:15:20 +053050{
Amit Arora17552782010-12-02 12:23:14 +053051 printf("powerdebug version %s\n", VERSION);
Amit Arorae9e16b02010-08-03 10:15:20 +053052}
53
Daniel Lezcano316bcae2011-03-23 14:37:30 +010054/*
55 * Options:
56 * -r, --regulator : regulator
57 * -s, --sensor : sensors
58 * -c, --clock : clocks
59 * -p, --findparents : clockname whose parents have to be found
60 * -t, --time : ticktime
61 * -d, --dump : dump
62 * -v, --verbose : verbose
63 * -V, --version : version
64 * -h, --help : help
65 * no option / default : show usage!
66 */
67
68static struct option long_options[] = {
69 { "regulator", 0, 0, 'r' },
70 { "sensor", 0, 0, 's' },
71 { "clock", 0, 0, 'c' },
72 { "findparents", 1, 0, 'p' },
73 { "time", 1, 0, 't' },
74 { "dump", 0, 0, 'd' },
75 { "verbose", 0, 0, 'v' },
76 { "version", 0, 0, 'V' },
77 { "help", 0, 0, 'h' },
78 { 0, 0, 0, 0 }
79};
80
81struct powerdebug_options {
82 int findparent;
83 int verbose;
84 int regulators;
85 int sensors;
86 int clocks;
87 int ticktime;
Daniel Lezcano9420fde2011-03-23 14:37:31 +010088 char *clkarg;
Daniel Lezcano316bcae2011-03-23 14:37:30 +010089};
90
91int getoptions(int argc, char *argv[], struct powerdebug_options *options)
Amit Arorae9e16b02010-08-03 10:15:20 +053092{
Daniel Lezcano316bcae2011-03-23 14:37:30 +010093 int c;
Amit Arorae9e16b02010-08-03 10:15:20 +053094
Daniel Lezcano316bcae2011-03-23 14:37:30 +010095 memset(options, 0, sizeof(*options));
96 options->ticktime = 10;
Amit Arorae9e16b02010-08-03 10:15:20 +053097
Amit Arorafefe8bf2010-08-05 13:31:20 +053098 while (1) {
99 int optindex = 0;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530100
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100101 c = getopt_long(argc, argv, "rscp:t:dvVh",
102 long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +0530103 if (c == -1)
104 break;
105
106 switch (c) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530107 case 'r':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100108 options->regulators = 1;
Amit Arora6e774cd2010-10-28 11:31:24 +0530109 selectedwindow = REGULATOR;
110 break;
111 case 's':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100112 options->sensors = 1;
Amit Arora6e774cd2010-10-28 11:31:24 +0530113 selectedwindow = SENSOR;
114 break;
115 case 'c':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100116 options->clocks = 1;
Amit Arora6e774cd2010-10-28 11:31:24 +0530117 selectedwindow = CLOCK;
118 break;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530119 case 'p':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100120 options->findparent = 1;
Daniel Lezcano9420fde2011-03-23 14:37:31 +0100121 options->clkarg = strdup(optarg);
122 if (!options->clkarg) {
123 fprintf(stderr, "failed to allocate memory");
124 return -1;
125 }
Amit Aroraf4fb8102010-11-30 13:55:50 +0530126 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530127 case 't':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100128 options->ticktime = strtod(optarg, NULL);
Amit Arora6e774cd2010-10-28 11:31:24 +0530129 break;
130 case 'd':
131 dump = 1;
132 break;
133 case 'v':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100134 options->verbose = 1;
Amit Arora6e774cd2010-10-28 11:31:24 +0530135 break;
136 case 'V':
137 version();
138 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530139 case '?':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100140 fprintf(stderr, "%s: Unknown option %c'.\n",
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600141 argv[0], optopt);
Amit Arora6e774cd2010-10-28 11:31:24 +0530142 default:
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100143 return -1;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530144 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530145 }
Amit Aroraa06a7302010-12-02 15:59:37 +0530146
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100147 if (dump && !(options->regulators ||
148 options->clocks || options->sensors)) {
149 /* By Default lets show everything we have */
150 options->regulators = options->clocks = options->sensors = 1;
Amit Aroraa06a7302010-12-02 15:59:37 +0530151 }
Amit Arorafefe8bf2010-08-05 13:31:20 +0530152
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100153 if (options->findparent && (!options->clocks || !dump)) {
Amit Aroraf4fb8102010-11-30 13:55:50 +0530154 fprintf(stderr, "-p option passed without -c and -d."
155 " Exiting...\n");
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100156 return -1;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530157 }
158
Amit Aroraa06a7302010-12-02 15:59:37 +0530159 if (!dump && selectedwindow == -1)
Amit Kucheriaee851a12011-01-12 04:57:48 +0530160 selectedwindow = CLOCK;
Amit Arorae9e16b02010-08-03 10:15:20 +0530161
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100162 return 0;
163}
164
165int main(int argc, char **argv)
166{
167 int i;
168 int findparent_ncurses = 0, refreshwin = 0;
169 int enter_hit = 0;
170 int firsttime[TOTAL_FEATURE_WINS];
171 char clkname_str[64];
172 struct powerdebug_options *options;
173
174 for (i = 0; i < TOTAL_FEATURE_WINS; i++)
175 firsttime[i] = 1;
176
177 options = malloc(sizeof(*options));
178 if (!options) {
179 fprintf(stderr, "failed to allocated memory\n");
180 return -1;
181 }
182
183 if (getoptions(argc, argv, options)) {
184 usage();
185 return 1;
186 }
187
Amit Arorae9e16b02010-08-03 10:15:20 +0530188 init_regulator_ds();
189
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600190 while (1) {
Amit Arora47fd9182010-08-24 13:26:06 +0530191 int key = 0;
192 struct timeval tval;
193 fd_set readfds;
Amit Arorae9e16b02010-08-03 10:15:20 +0530194
Amit Arora47fd9182010-08-24 13:26:06 +0530195 if (!dump) {
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600196 if (firsttime[0])
Amit Arora47fd9182010-08-24 13:26:06 +0530197 init_curses();
Amit Arora47fd9182010-08-24 13:26:06 +0530198 create_windows();
199 show_header();
200 }
Amit Aroraac4e8652010-11-09 11:16:53 +0530201
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100202 if (options->regulators || selectedwindow == REGULATOR) {
Amit Arora47fd9182010-08-24 13:26:06 +0530203 read_regulator_info();
Amit Arora728e0c92010-09-14 12:06:09 +0530204 if (!dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530205 create_selectedwindow();
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100206 show_regulator_info(options->verbose);
Amit Arora6e774cd2010-10-28 11:31:24 +0530207 }
Amit Arora47fd9182010-08-24 13:26:06 +0530208 else
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100209 print_regulator_info(options->verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530210 }
211
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100212 if (options->clocks || selectedwindow == CLOCK) {
Amit Arora04f97742010-11-16 11:28:57 +0530213 int ret = 0;
Amit Arora6e774cd2010-10-28 11:31:24 +0530214 if (firsttime[CLOCK]) {
Amit Arora04f97742010-11-16 11:28:57 +0530215 ret = init_clock_details();
216 if (!ret)
217 firsttime[CLOCK] = 0;
Amit Arora3bd79162010-12-01 13:51:42 +0530218 strcpy(clkname_str, "");
Amit Arora6e774cd2010-10-28 11:31:24 +0530219 }
Amit Arora04f97742010-11-16 11:28:57 +0530220 if (!ret && !dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530221 int hrow;
Amit Arora0e512722010-10-01 12:24:16 +0530222
Amit Arora6e774cd2010-10-28 11:31:24 +0530223 create_selectedwindow();
Amit Arora3bd79162010-12-01 13:51:42 +0530224 if (!findparent_ncurses) {
Amit Aroraa06a7302010-12-02 15:59:37 +0530225 int command = 0;
226
227 if (enter_hit)
228 command = CLOCK_SELECTED;
229 if (refreshwin)
230 command = REFRESH_WINDOW;
Amit Arora3bd79162010-12-01 13:51:42 +0530231 hrow = read_and_print_clock_info(
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100232 options->verbose,
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600233 highlighted_row,
234 command);
Amit Arora3bd79162010-12-01 13:51:42 +0530235 highlighted_row = hrow;
236 enter_hit = 0;
237 } else
238 find_parents_for_clock(clkname_str,
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600239 enter_hit);
Amit Arora04f97742010-11-16 11:28:57 +0530240 }
Amit Aroraf4fb8102010-11-30 13:55:50 +0530241 if (!ret && dump) {
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100242 if (options->findparent)
243 read_and_dump_clock_info_one(options->clkarg);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530244 else
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100245 read_and_dump_clock_info(options->verbose);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530246 }
Amit Arora6e774cd2010-10-28 11:31:24 +0530247 }
Amit Arora47fd9182010-08-24 13:26:06 +0530248
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100249 if (options->sensors || selectedwindow == SENSOR) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530250 if (!dump) {
251 create_selectedwindow();
252 print_sensor_header();
253 } else
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100254 read_and_print_sensor_info(options->verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530255 }
256
257 if (dump)
258 break;
259
260 FD_ZERO(&readfds);
261 FD_SET(0, &readfds);
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100262 tval.tv_sec = options->ticktime;
263 tval.tv_usec = (options->ticktime - tval.tv_sec) * 1000000;
Amit Arora47fd9182010-08-24 13:26:06 +0530264
265 key = select(1, &readfds, NULL, NULL, &tval);
266
267 if (key) {
268 char keychar;
Amit Arorac93e0712010-10-07 13:51:53 +0530269 int keystroke = getch();
Amit Arora3bd79162010-12-01 13:51:42 +0530270 int oldselectedwin = selectedwindow;
Amit Arora97006e52010-10-28 11:56:08 +0530271
Amit Arora47fd9182010-08-24 13:26:06 +0530272 if (keystroke == EOF)
273 exit(0);
274
Amit Arora6e774cd2010-10-28 11:31:24 +0530275 if (keystroke == KEY_RIGHT || keystroke == 9)
276 selectedwindow++;
Amit Arorac93e0712010-10-07 13:51:53 +0530277
Amit Arora6e774cd2010-10-28 11:31:24 +0530278 if (keystroke == KEY_LEFT || keystroke == 353)
279 selectedwindow--;
Amit Arorac93e0712010-10-07 13:51:53 +0530280
Amit Arora6e774cd2010-10-28 11:31:24 +0530281 if (selectedwindow >= TOTAL_FEATURE_WINS)
282 selectedwindow = 0;
Amit Arorac93e0712010-10-07 13:51:53 +0530283
Amit Arora6e774cd2010-10-28 11:31:24 +0530284 if (selectedwindow < 0)
285 selectedwindow = TOTAL_FEATURE_WINS - 1;
Amit Arorac93e0712010-10-07 13:51:53 +0530286
Amit Arora6e774cd2010-10-28 11:31:24 +0530287 if (selectedwindow == CLOCK) {
288 if (keystroke == KEY_DOWN)
289 highlighted_row++;
290 if (keystroke == KEY_UP && highlighted_row > 0)
291 highlighted_row--;
Amit Arora3bd79162010-12-01 13:51:42 +0530292 if (keystroke == 47)
293 findparent_ncurses = 1;
294
295 if ((keystroke == 27 || oldselectedwin !=
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600296 selectedwindow) && findparent_ncurses) {
Amit Arora3bd79162010-12-01 13:51:42 +0530297 findparent_ncurses = 0;
298 clkname_str[0] = '\0';
299 }
300
301 if (findparent_ncurses && keystroke != 13) {
302 int len = strlen(clkname_str);
303 char str[2];
304
305 if (keystroke == 263) {
306 if (len > 0)
307 len--;
308
309 clkname_str[len] = '\0';
310 } else {
311 if (strlen(clkname_str) ||
312 keystroke != '/') {
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600313 str[0] = keystroke;
314 str[1] = '\0';
315 if (len < 63)
316 strcat(clkname_str,
317 str);
Amit Arora3bd79162010-12-01 13:51:42 +0530318 }
319 }
320 }
Amit Arora6e774cd2010-10-28 11:31:24 +0530321 }
Amit Arora728e0c92010-09-14 12:06:09 +0530322
Amit Arora47fd9182010-08-24 13:26:06 +0530323 keychar = toupper(keystroke);
Amit Arora3bd79162010-12-01 13:51:42 +0530324//#define DEBUG
325#ifdef DEBUG
326 killall_windows(1); fini_curses();
327 printf("key entered %d:%c\n", keystroke, keychar);
328 exit(1);
329#endif
Amit Arorac93e0712010-10-07 13:51:53 +0530330
Amit Arora6e774cd2010-10-28 11:31:24 +0530331 if (keystroke == 13)
332 enter_hit = 1;
Amit Arora728e0c92010-09-14 12:06:09 +0530333
Amit Arora3bd79162010-12-01 13:51:42 +0530334 if (keychar == 'Q' && !findparent_ncurses)
Amit Arora47fd9182010-08-24 13:26:06 +0530335 exit(0);
Amit Aroraa06a7302010-12-02 15:59:37 +0530336 if (keychar == 'R') {
337 refreshwin = 1;
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100338 options->ticktime = 3;
Amit Aroraa06a7302010-12-02 15:59:37 +0530339 } else
340 refreshwin = 0;
Amit Arora47fd9182010-08-24 13:26:06 +0530341 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530342 }
Amit Arora85fd4952010-08-05 14:04:50 +0530343 exit(0);
Amit Arorae9e16b02010-08-03 10:15:20 +0530344}