blob: 3a9c0ec7abf93a0bac137b60539f873cf7c94e73 [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 Lezcano7f112da2011-03-23 14:37:32 +0100128 options->ticktime = atoi(optarg);
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
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100165int mainloop(struct powerdebug_options *options)
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100166{
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100167 int findparent_ncurses = 0, refreshwin = 0;
168 int enter_hit = 0;
169 int firsttime[TOTAL_FEATURE_WINS];
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100170 int i;
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100171 char clkname_str[64];
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100172
173 for (i = 0; i < TOTAL_FEATURE_WINS; i++)
174 firsttime[i] = 1;
175
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600176 while (1) {
Amit Arora47fd9182010-08-24 13:26:06 +0530177 int key = 0;
178 struct timeval tval;
179 fd_set readfds;
Amit Arorae9e16b02010-08-03 10:15:20 +0530180
Amit Arora47fd9182010-08-24 13:26:06 +0530181 if (!dump) {
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600182 if (firsttime[0])
Amit Arora47fd9182010-08-24 13:26:06 +0530183 init_curses();
Amit Arora47fd9182010-08-24 13:26:06 +0530184 create_windows();
185 show_header();
186 }
Amit Aroraac4e8652010-11-09 11:16:53 +0530187
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100188 if (options->regulators || selectedwindow == REGULATOR) {
Amit Arora47fd9182010-08-24 13:26:06 +0530189 read_regulator_info();
Amit Arora728e0c92010-09-14 12:06:09 +0530190 if (!dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530191 create_selectedwindow();
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100192 show_regulator_info(options->verbose);
Amit Arora6e774cd2010-10-28 11:31:24 +0530193 }
Amit Arora47fd9182010-08-24 13:26:06 +0530194 else
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100195 print_regulator_info(options->verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530196 }
197
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100198 if (options->clocks || selectedwindow == CLOCK) {
Amit Arora04f97742010-11-16 11:28:57 +0530199 int ret = 0;
Amit Arora6e774cd2010-10-28 11:31:24 +0530200 if (firsttime[CLOCK]) {
Amit Arora04f97742010-11-16 11:28:57 +0530201 ret = init_clock_details();
202 if (!ret)
203 firsttime[CLOCK] = 0;
Amit Arora3bd79162010-12-01 13:51:42 +0530204 strcpy(clkname_str, "");
Amit Arora6e774cd2010-10-28 11:31:24 +0530205 }
Amit Arora04f97742010-11-16 11:28:57 +0530206 if (!ret && !dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530207 int hrow;
Amit Arora0e512722010-10-01 12:24:16 +0530208
Amit Arora6e774cd2010-10-28 11:31:24 +0530209 create_selectedwindow();
Amit Arora3bd79162010-12-01 13:51:42 +0530210 if (!findparent_ncurses) {
Amit Aroraa06a7302010-12-02 15:59:37 +0530211 int command = 0;
212
213 if (enter_hit)
214 command = CLOCK_SELECTED;
215 if (refreshwin)
216 command = REFRESH_WINDOW;
Amit Arora3bd79162010-12-01 13:51:42 +0530217 hrow = read_and_print_clock_info(
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100218 options->verbose,
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600219 highlighted_row,
220 command);
Amit Arora3bd79162010-12-01 13:51:42 +0530221 highlighted_row = hrow;
222 enter_hit = 0;
223 } else
224 find_parents_for_clock(clkname_str,
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600225 enter_hit);
Amit Arora04f97742010-11-16 11:28:57 +0530226 }
Amit Aroraf4fb8102010-11-30 13:55:50 +0530227 if (!ret && dump) {
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100228 if (options->findparent)
229 read_and_dump_clock_info_one(options->clkarg);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530230 else
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100231 read_and_dump_clock_info(options->verbose);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530232 }
Amit Arora6e774cd2010-10-28 11:31:24 +0530233 }
Amit Arora47fd9182010-08-24 13:26:06 +0530234
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100235 if (options->sensors || selectedwindow == SENSOR) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530236 if (!dump) {
237 create_selectedwindow();
238 print_sensor_header();
239 } else
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100240 read_and_print_sensor_info(options->verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530241 }
242
243 if (dump)
244 break;
245
246 FD_ZERO(&readfds);
247 FD_SET(0, &readfds);
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100248 tval.tv_sec = options->ticktime;
249 tval.tv_usec = (options->ticktime - tval.tv_sec) * 1000000;
Amit Arora47fd9182010-08-24 13:26:06 +0530250
251 key = select(1, &readfds, NULL, NULL, &tval);
252
253 if (key) {
254 char keychar;
Amit Arorac93e0712010-10-07 13:51:53 +0530255 int keystroke = getch();
Amit Arora3bd79162010-12-01 13:51:42 +0530256 int oldselectedwin = selectedwindow;
Amit Arora97006e52010-10-28 11:56:08 +0530257
Amit Arora47fd9182010-08-24 13:26:06 +0530258 if (keystroke == EOF)
259 exit(0);
260
Amit Arora6e774cd2010-10-28 11:31:24 +0530261 if (keystroke == KEY_RIGHT || keystroke == 9)
262 selectedwindow++;
Amit Arorac93e0712010-10-07 13:51:53 +0530263
Amit Arora6e774cd2010-10-28 11:31:24 +0530264 if (keystroke == KEY_LEFT || keystroke == 353)
265 selectedwindow--;
Amit Arorac93e0712010-10-07 13:51:53 +0530266
Amit Arora6e774cd2010-10-28 11:31:24 +0530267 if (selectedwindow >= TOTAL_FEATURE_WINS)
268 selectedwindow = 0;
Amit Arorac93e0712010-10-07 13:51:53 +0530269
Amit Arora6e774cd2010-10-28 11:31:24 +0530270 if (selectedwindow < 0)
271 selectedwindow = TOTAL_FEATURE_WINS - 1;
Amit Arorac93e0712010-10-07 13:51:53 +0530272
Amit Arora6e774cd2010-10-28 11:31:24 +0530273 if (selectedwindow == CLOCK) {
274 if (keystroke == KEY_DOWN)
275 highlighted_row++;
276 if (keystroke == KEY_UP && highlighted_row > 0)
277 highlighted_row--;
Amit Arora3bd79162010-12-01 13:51:42 +0530278 if (keystroke == 47)
279 findparent_ncurses = 1;
280
281 if ((keystroke == 27 || oldselectedwin !=
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600282 selectedwindow) && findparent_ncurses) {
Amit Arora3bd79162010-12-01 13:51:42 +0530283 findparent_ncurses = 0;
284 clkname_str[0] = '\0';
285 }
286
287 if (findparent_ncurses && keystroke != 13) {
288 int len = strlen(clkname_str);
289 char str[2];
290
291 if (keystroke == 263) {
292 if (len > 0)
293 len--;
294
295 clkname_str[len] = '\0';
296 } else {
297 if (strlen(clkname_str) ||
298 keystroke != '/') {
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600299 str[0] = keystroke;
300 str[1] = '\0';
301 if (len < 63)
302 strcat(clkname_str,
303 str);
Amit Arora3bd79162010-12-01 13:51:42 +0530304 }
305 }
306 }
Amit Arora6e774cd2010-10-28 11:31:24 +0530307 }
Amit Arora728e0c92010-09-14 12:06:09 +0530308
Amit Arora47fd9182010-08-24 13:26:06 +0530309 keychar = toupper(keystroke);
Amit Arora3bd79162010-12-01 13:51:42 +0530310//#define DEBUG
311#ifdef DEBUG
312 killall_windows(1); fini_curses();
313 printf("key entered %d:%c\n", keystroke, keychar);
314 exit(1);
315#endif
Amit Arorac93e0712010-10-07 13:51:53 +0530316
Amit Arora6e774cd2010-10-28 11:31:24 +0530317 if (keystroke == 13)
318 enter_hit = 1;
Amit Arora728e0c92010-09-14 12:06:09 +0530319
Amit Arora3bd79162010-12-01 13:51:42 +0530320 if (keychar == 'Q' && !findparent_ncurses)
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100321 break;
Amit Aroraa06a7302010-12-02 15:59:37 +0530322 if (keychar == 'R') {
323 refreshwin = 1;
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100324 options->ticktime = 3;
Amit Aroraa06a7302010-12-02 15:59:37 +0530325 } else
326 refreshwin = 0;
Amit Arora47fd9182010-08-24 13:26:06 +0530327 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530328 }
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100329
330 return 0;
331}
332
333int main(int argc, char **argv)
334{
335 struct powerdebug_options *options;
336
337 options = malloc(sizeof(*options));
338 if (!options) {
339 fprintf(stderr, "failed to allocated memory\n");
340 return -1;
341 }
342
343 if (getoptions(argc, argv, options)) {
344 usage();
345 return 1;
346 }
347
348 if (init_regulator_ds())
349 return 1;
350
351 if (mainloop(options))
352 return 1;
353
354 return 0;
Amit Arorae9e16b02010-08-03 10:15:20 +0530355}