blob: b3b682a088ee96342829f5fd9a37abe7858768ef [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;
88 char clkarg[64];
89};
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;
121 strcpy(options->clkarg, optarg);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530122 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530123 case 't':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100124 options->ticktime = strtod(optarg, NULL);
Amit Arora6e774cd2010-10-28 11:31:24 +0530125 break;
126 case 'd':
127 dump = 1;
128 break;
129 case 'v':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100130 options->verbose = 1;
Amit Arora6e774cd2010-10-28 11:31:24 +0530131 break;
132 case 'V':
133 version();
134 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530135 case '?':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100136 fprintf(stderr, "%s: Unknown option %c'.\n",
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600137 argv[0], optopt);
Amit Arora6e774cd2010-10-28 11:31:24 +0530138 default:
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100139 return -1;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530140 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530141 }
Amit Aroraa06a7302010-12-02 15:59:37 +0530142
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100143 if (dump && !(options->regulators ||
144 options->clocks || options->sensors)) {
145 /* By Default lets show everything we have */
146 options->regulators = options->clocks = options->sensors = 1;
Amit Aroraa06a7302010-12-02 15:59:37 +0530147 }
Amit Arorafefe8bf2010-08-05 13:31:20 +0530148
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100149 if (options->findparent && (!options->clocks || !dump)) {
Amit Aroraf4fb8102010-11-30 13:55:50 +0530150 fprintf(stderr, "-p option passed without -c and -d."
151 " Exiting...\n");
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100152 return -1;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530153 }
154
Amit Aroraa06a7302010-12-02 15:59:37 +0530155 if (!dump && selectedwindow == -1)
Amit Kucheriaee851a12011-01-12 04:57:48 +0530156 selectedwindow = CLOCK;
Amit Arorae9e16b02010-08-03 10:15:20 +0530157
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100158 return 0;
159}
160
161int main(int argc, char **argv)
162{
163 int i;
164 int findparent_ncurses = 0, refreshwin = 0;
165 int enter_hit = 0;
166 int firsttime[TOTAL_FEATURE_WINS];
167 char clkname_str[64];
168 struct powerdebug_options *options;
169
170 for (i = 0; i < TOTAL_FEATURE_WINS; i++)
171 firsttime[i] = 1;
172
173 options = malloc(sizeof(*options));
174 if (!options) {
175 fprintf(stderr, "failed to allocated memory\n");
176 return -1;
177 }
178
179 if (getoptions(argc, argv, options)) {
180 usage();
181 return 1;
182 }
183
Amit Arorae9e16b02010-08-03 10:15:20 +0530184 init_regulator_ds();
185
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600186 while (1) {
Amit Arora47fd9182010-08-24 13:26:06 +0530187 int key = 0;
188 struct timeval tval;
189 fd_set readfds;
Amit Arorae9e16b02010-08-03 10:15:20 +0530190
Amit Arora47fd9182010-08-24 13:26:06 +0530191 if (!dump) {
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600192 if (firsttime[0])
Amit Arora47fd9182010-08-24 13:26:06 +0530193 init_curses();
Amit Arora47fd9182010-08-24 13:26:06 +0530194 create_windows();
195 show_header();
196 }
Amit Aroraac4e8652010-11-09 11:16:53 +0530197
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100198 if (options->regulators || selectedwindow == REGULATOR) {
Amit Arora47fd9182010-08-24 13:26:06 +0530199 read_regulator_info();
Amit Arora728e0c92010-09-14 12:06:09 +0530200 if (!dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530201 create_selectedwindow();
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100202 show_regulator_info(options->verbose);
Amit Arora6e774cd2010-10-28 11:31:24 +0530203 }
Amit Arora47fd9182010-08-24 13:26:06 +0530204 else
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100205 print_regulator_info(options->verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530206 }
207
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100208 if (options->clocks || selectedwindow == CLOCK) {
Amit Arora04f97742010-11-16 11:28:57 +0530209 int ret = 0;
Amit Arora6e774cd2010-10-28 11:31:24 +0530210 if (firsttime[CLOCK]) {
Amit Arora04f97742010-11-16 11:28:57 +0530211 ret = init_clock_details();
212 if (!ret)
213 firsttime[CLOCK] = 0;
Amit Arora3bd79162010-12-01 13:51:42 +0530214 strcpy(clkname_str, "");
Amit Arora6e774cd2010-10-28 11:31:24 +0530215 }
Amit Arora04f97742010-11-16 11:28:57 +0530216 if (!ret && !dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530217 int hrow;
Amit Arora0e512722010-10-01 12:24:16 +0530218
Amit Arora6e774cd2010-10-28 11:31:24 +0530219 create_selectedwindow();
Amit Arora3bd79162010-12-01 13:51:42 +0530220 if (!findparent_ncurses) {
Amit Aroraa06a7302010-12-02 15:59:37 +0530221 int command = 0;
222
223 if (enter_hit)
224 command = CLOCK_SELECTED;
225 if (refreshwin)
226 command = REFRESH_WINDOW;
Amit Arora3bd79162010-12-01 13:51:42 +0530227 hrow = read_and_print_clock_info(
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100228 options->verbose,
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600229 highlighted_row,
230 command);
Amit Arora3bd79162010-12-01 13:51:42 +0530231 highlighted_row = hrow;
232 enter_hit = 0;
233 } else
234 find_parents_for_clock(clkname_str,
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600235 enter_hit);
Amit Arora04f97742010-11-16 11:28:57 +0530236 }
Amit Aroraf4fb8102010-11-30 13:55:50 +0530237 if (!ret && dump) {
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100238 if (options->findparent)
239 read_and_dump_clock_info_one(options->clkarg);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530240 else
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100241 read_and_dump_clock_info(options->verbose);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530242 }
Amit Arora6e774cd2010-10-28 11:31:24 +0530243 }
Amit Arora47fd9182010-08-24 13:26:06 +0530244
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100245 if (options->sensors || selectedwindow == SENSOR) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530246 if (!dump) {
247 create_selectedwindow();
248 print_sensor_header();
249 } else
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100250 read_and_print_sensor_info(options->verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530251 }
252
253 if (dump)
254 break;
255
256 FD_ZERO(&readfds);
257 FD_SET(0, &readfds);
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100258 tval.tv_sec = options->ticktime;
259 tval.tv_usec = (options->ticktime - tval.tv_sec) * 1000000;
Amit Arora47fd9182010-08-24 13:26:06 +0530260
261 key = select(1, &readfds, NULL, NULL, &tval);
262
263 if (key) {
264 char keychar;
Amit Arorac93e0712010-10-07 13:51:53 +0530265 int keystroke = getch();
Amit Arora3bd79162010-12-01 13:51:42 +0530266 int oldselectedwin = selectedwindow;
Amit Arora97006e52010-10-28 11:56:08 +0530267
Amit Arora47fd9182010-08-24 13:26:06 +0530268 if (keystroke == EOF)
269 exit(0);
270
Amit Arora6e774cd2010-10-28 11:31:24 +0530271 if (keystroke == KEY_RIGHT || keystroke == 9)
272 selectedwindow++;
Amit Arorac93e0712010-10-07 13:51:53 +0530273
Amit Arora6e774cd2010-10-28 11:31:24 +0530274 if (keystroke == KEY_LEFT || keystroke == 353)
275 selectedwindow--;
Amit Arorac93e0712010-10-07 13:51:53 +0530276
Amit Arora6e774cd2010-10-28 11:31:24 +0530277 if (selectedwindow >= TOTAL_FEATURE_WINS)
278 selectedwindow = 0;
Amit Arorac93e0712010-10-07 13:51:53 +0530279
Amit Arora6e774cd2010-10-28 11:31:24 +0530280 if (selectedwindow < 0)
281 selectedwindow = TOTAL_FEATURE_WINS - 1;
Amit Arorac93e0712010-10-07 13:51:53 +0530282
Amit Arora6e774cd2010-10-28 11:31:24 +0530283 if (selectedwindow == CLOCK) {
284 if (keystroke == KEY_DOWN)
285 highlighted_row++;
286 if (keystroke == KEY_UP && highlighted_row > 0)
287 highlighted_row--;
Amit Arora3bd79162010-12-01 13:51:42 +0530288 if (keystroke == 47)
289 findparent_ncurses = 1;
290
291 if ((keystroke == 27 || oldselectedwin !=
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600292 selectedwindow) && findparent_ncurses) {
Amit Arora3bd79162010-12-01 13:51:42 +0530293 findparent_ncurses = 0;
294 clkname_str[0] = '\0';
295 }
296
297 if (findparent_ncurses && keystroke != 13) {
298 int len = strlen(clkname_str);
299 char str[2];
300
301 if (keystroke == 263) {
302 if (len > 0)
303 len--;
304
305 clkname_str[len] = '\0';
306 } else {
307 if (strlen(clkname_str) ||
308 keystroke != '/') {
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600309 str[0] = keystroke;
310 str[1] = '\0';
311 if (len < 63)
312 strcat(clkname_str,
313 str);
Amit Arora3bd79162010-12-01 13:51:42 +0530314 }
315 }
316 }
Amit Arora6e774cd2010-10-28 11:31:24 +0530317 }
Amit Arora728e0c92010-09-14 12:06:09 +0530318
Amit Arora47fd9182010-08-24 13:26:06 +0530319 keychar = toupper(keystroke);
Amit Arora3bd79162010-12-01 13:51:42 +0530320//#define DEBUG
321#ifdef DEBUG
322 killall_windows(1); fini_curses();
323 printf("key entered %d:%c\n", keystroke, keychar);
324 exit(1);
325#endif
Amit Arorac93e0712010-10-07 13:51:53 +0530326
Amit Arora6e774cd2010-10-28 11:31:24 +0530327 if (keystroke == 13)
328 enter_hit = 1;
Amit Arora728e0c92010-09-14 12:06:09 +0530329
Amit Arora3bd79162010-12-01 13:51:42 +0530330 if (keychar == 'Q' && !findparent_ncurses)
Amit Arora47fd9182010-08-24 13:26:06 +0530331 exit(0);
Amit Aroraa06a7302010-12-02 15:59:37 +0530332 if (keychar == 'R') {
333 refreshwin = 1;
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100334 options->ticktime = 3;
Amit Aroraa06a7302010-12-02 15:59:37 +0530335 } else
336 refreshwin = 0;
Amit Arora47fd9182010-08-24 13:26:06 +0530337 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530338 }
Amit Arora85fd4952010-08-05 14:04:50 +0530339 exit(0);
Amit Arorae9e16b02010-08-03 10:15:20 +0530340}