blob: 3be47e16a92774a513ea21b99efb618ba2ef4161 [file] [log] [blame]
Amit Arora39f29542010-09-14 12:03:22 +05301/*******************************************************************************
2 * Copyright (C) 2010, Linaro
3 * Copyright (C) 2010, IBM Corporation
4 *
5 * This file is part of PowerDebug.
6 *
7 * All rights reserved. This program and the accompanying materials
8 * are made available under the terms of the Eclipse Public License v1.0
9 * which accompanies this distribution, and is available at
10 * http://www.eclipse.org/legal/epl-v10.html
11 *
12 * Contributors:
13 * Amit Arora <amit.arora@linaro.org> (IBM Corporation)
14 * - initial API and implementation
15 *******************************************************************************/
16
Amit Arorae9e16b02010-08-03 10:15:20 +053017#include <getopt.h>
Amit Arorae9e16b02010-08-03 10:15:20 +053018#include "powerdebug.h"
19
Amit Arorae9e16b02010-08-03 10:15:20 +053020int numregulators;
Amit Arora47fd9182010-08-24 13:26:06 +053021int dump;
Amit Arora728e0c92010-09-14 12:06:09 +053022int highlighted_row;
Amit Arorac93e0712010-10-07 13:51:53 +053023int selectedwindow = -1;
Amit Arora728e0c92010-09-14 12:06:09 +053024double ticktime = 10.0; /* in seconds */
Amit Arorae9e16b02010-08-03 10:15:20 +053025
Amit Arorac93e0712010-10-07 13:51:53 +053026char *win_names[TOTAL_FEATURE_WINS] = {
Amit Arora6e774cd2010-10-28 11:31:24 +053027 "Regulators",
28 "Clocks",
29 "Sensors" };
Amit Arorac93e0712010-10-07 13:51:53 +053030
Amit Arora17552782010-12-02 12:23:14 +053031void usage(char **argv)
Amit Arorae9e16b02010-08-03 10:15:20 +053032{
Amit Arora17552782010-12-02 12:23:14 +053033 printf("Usage: %s [OPTIONS]\n", argv[0]);
34 printf(" -r, --regulator Show regulator information\n");
35 printf(" -s, --sensor Show sensor information\n");
36 printf(" -c, --clock Show clock information\n");
37 printf(" -p, --findparents Show all parents for a particular clock\n");
38 printf(" -t, --time Set ticktime in seconds (eg. 10.0)\n");
39 printf(" -d, --dump Dump information once (no refresh)\n");
40 printf(" -v, --verbose Verbose mode (use with -r and/or -s)\n");
41 printf(" -V, --version Show Version\n");
42 printf(" -h, --help Help\n");
Amit Arorae9e16b02010-08-03 10:15:20 +053043
Amit Arora17552782010-12-02 12:23:14 +053044 exit(0);
Amit Arorae9e16b02010-08-03 10:15:20 +053045}
46
Amit Arora17552782010-12-02 12:23:14 +053047void version()
Amit Arorae9e16b02010-08-03 10:15:20 +053048{
Amit Arora17552782010-12-02 12:23:14 +053049 printf("powerdebug version %s\n", VERSION);
50 exit(0);
Amit Arorae9e16b02010-08-03 10:15:20 +053051}
52
Amit Arorae9e16b02010-08-03 10:15:20 +053053int main(int argc, char **argv)
54{
Amit Arorac93e0712010-10-07 13:51:53 +053055 int c, i;
56 int firsttime[TOTAL_FEATURE_WINS];
Amit Aroraa06a7302010-12-02 15:59:37 +053057 int enter_hit = 0, verbose = 0, findparent_ncurses = 0, refreshwin = 0;
Amit Aroraf4fb8102010-11-30 13:55:50 +053058 int regulators = 0, sensors = 0, clocks = 0, findparent = 0;
Amit Arora3bd79162010-12-01 13:51:42 +053059 char clkarg[64], clkname_str[64];
Amit Arorae9e16b02010-08-03 10:15:20 +053060
Amit Arora6e774cd2010-10-28 11:31:24 +053061 for (i = 0; i < TOTAL_FEATURE_WINS; i++)
62 firsttime[i] = 1;
Amit Arorac93e0712010-10-07 13:51:53 +053063
Amit Arorafefe8bf2010-08-05 13:31:20 +053064 /*
65 * Options:
Amit Arora6e774cd2010-10-28 11:31:24 +053066 * -r, --regulator : regulator
Amit Aroraa06a7302010-12-02 15:59:37 +053067 * -s, --sensor : sensors
68 * -c, --clock : clocks
Amit Aroraf4fb8102010-11-30 13:55:50 +053069 * -p, --findparents : clockname whose parents have to be found
Amit Arora728e0c92010-09-14 12:06:09 +053070 * -t, --time : ticktime
Amit Arora47fd9182010-08-24 13:26:06 +053071 * -d, --dump : dump
Amit Arorafefe8bf2010-08-05 13:31:20 +053072 * -v, --verbose : verbose
73 * -V, --version : version
74 * -h, --help : help
75 * no option / default : show usage!
Amit Arorae9e16b02010-08-03 10:15:20 +053076 */
77
Amit Arorafefe8bf2010-08-05 13:31:20 +053078 while (1) {
79 int optindex = 0;
80 static struct option long_options[] = {
Amit Arora6e774cd2010-10-28 11:31:24 +053081 {"regulator", 0, 0, 'r'},
82 {"sensor", 0, 0, 's'},
83 {"clock", 0, 0, 'c'},
Amit Arora203f4d42010-12-01 13:52:01 +053084 {"findparents", 1, 0, 'p'},
85 {"time", 1, 0, 't'},
Amit Arora47fd9182010-08-24 13:26:06 +053086 {"dump", 0, 0, 'd'},
Amit Arorafefe8bf2010-08-05 13:31:20 +053087 {"verbose", 0, 0, 'v'},
88 {"version", 0, 0, 'V'},
89 {"help", 0, 0, 'h'},
90 {0, 0, 0, 0}
91 };
92
Amit Aroraf4fb8102010-11-30 13:55:50 +053093 c = getopt_long(argc, argv, "rscp:t:dvVh", long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +053094 if (c == -1)
95 break;
96
97 switch (c) {
Amit Arora6e774cd2010-10-28 11:31:24 +053098 case 'r':
99 regulators = 1;
100 selectedwindow = REGULATOR;
101 break;
102 case 's':
103 sensors = 1;
104 selectedwindow = SENSOR;
105 break;
106 case 'c':
107 clocks = 1;
108 selectedwindow = CLOCK;
109 break;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530110 case 'p':
111 findparent = 1;
112 strcpy(clkarg, optarg);
113 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530114 case 't':
115 ticktime = strtod(optarg, NULL);
116 break;
117 case 'd':
118 dump = 1;
119 break;
120 case 'v':
121 verbose = 1;
122 break;
123 case 'V':
124 version();
125 break;
126 case 'h':
127 usage(argv);
128 break;
129 case '?':
130 fprintf (stderr, "%s: Unknown option %c'.\n",
131 argv[0], optopt);
132 exit(1);
133 default:
134 usage(argv);
135 break;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530136 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530137 }
Amit Aroraa06a7302010-12-02 15:59:37 +0530138/*
Amit Arora6e774cd2010-10-28 11:31:24 +0530139 if (!dump && (regulators || clocks || sensors)) {
140 fprintf(stderr, "Option supported only in dump mode (-d)\n");
141 usage(argv);
142 }
Amit Aroraa06a7302010-12-02 15:59:37 +0530143*/
144
145 if (dump && !(regulators || clocks || sensors)) {
146 fprintf(stderr, "Dump mode (-d) supported only with -c, -r "
147 "or -s ..\n");
148 usage(argv);
149 }
Amit Arorafefe8bf2010-08-05 13:31:20 +0530150
Amit Aroraf4fb8102010-11-30 13:55:50 +0530151 if (findparent && (!clocks || !dump)) {
152 fprintf(stderr, "-p option passed without -c and -d."
153 " Exiting...\n");
154 usage(argv);
155 }
156
Amit Aroraa06a7302010-12-02 15:59:37 +0530157 if (!dump && selectedwindow == -1)
Amit Arora6e774cd2010-10-28 11:31:24 +0530158 selectedwindow = REGULATOR;
Amit Arorae9e16b02010-08-03 10:15:20 +0530159
160 init_regulator_ds();
161
Amit Arora47fd9182010-08-24 13:26:06 +0530162 while(1) {
163 int key = 0;
164 struct timeval tval;
165 fd_set readfds;
Amit Arorae9e16b02010-08-03 10:15:20 +0530166
Amit Arora47fd9182010-08-24 13:26:06 +0530167 if (!dump) {
Amit Arorac93e0712010-10-07 13:51:53 +0530168 if(firsttime[0])
Amit Arora47fd9182010-08-24 13:26:06 +0530169 init_curses();
Amit Arora47fd9182010-08-24 13:26:06 +0530170 create_windows();
171 show_header();
172 }
Amit Aroraac4e8652010-11-09 11:16:53 +0530173
Amit Arora47fd9182010-08-24 13:26:06 +0530174
Amit Arorac93e0712010-10-07 13:51:53 +0530175 if (selectedwindow == REGULATOR) {
Amit Arora47fd9182010-08-24 13:26:06 +0530176 read_regulator_info();
Amit Arora728e0c92010-09-14 12:06:09 +0530177 if (!dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530178 create_selectedwindow();
Amit Arora47fd9182010-08-24 13:26:06 +0530179 show_regulator_info(verbose);
Amit Arora6e774cd2010-10-28 11:31:24 +0530180 }
Amit Arora47fd9182010-08-24 13:26:06 +0530181 else
182 print_regulator_info(verbose);
183 }
184
Amit Arora6e774cd2010-10-28 11:31:24 +0530185 if (selectedwindow == CLOCK) {
Amit Arora04f97742010-11-16 11:28:57 +0530186 int ret = 0;
Amit Arora6e774cd2010-10-28 11:31:24 +0530187 if (firsttime[CLOCK]) {
Amit Arora04f97742010-11-16 11:28:57 +0530188 ret = init_clock_details();
189 if (!ret)
190 firsttime[CLOCK] = 0;
Amit Arora3bd79162010-12-01 13:51:42 +0530191 strcpy(clkname_str, "");
Amit Arora6e774cd2010-10-28 11:31:24 +0530192 }
Amit Arora04f97742010-11-16 11:28:57 +0530193 if (!ret && !dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530194 int hrow;
Amit Arora0e512722010-10-01 12:24:16 +0530195
Amit Arora6e774cd2010-10-28 11:31:24 +0530196 create_selectedwindow();
Amit Arora3bd79162010-12-01 13:51:42 +0530197 if (!findparent_ncurses) {
Amit Aroraa06a7302010-12-02 15:59:37 +0530198 int command = 0;
199
200 if (enter_hit)
201 command = CLOCK_SELECTED;
202 if (refreshwin)
203 command = REFRESH_WINDOW;
Amit Arora3bd79162010-12-01 13:51:42 +0530204 hrow = read_and_print_clock_info(
205 verbose,
Amit Arora6e774cd2010-10-28 11:31:24 +0530206 highlighted_row,
Amit Aroraa06a7302010-12-02 15:59:37 +0530207 command);
Amit Arora3bd79162010-12-01 13:51:42 +0530208 highlighted_row = hrow;
209 enter_hit = 0;
210 } else
211 find_parents_for_clock(clkname_str,
212 enter_hit);
Amit Arora04f97742010-11-16 11:28:57 +0530213 }
Amit Aroraf4fb8102010-11-30 13:55:50 +0530214 if (!ret && dump) {
215 if (findparent)
216 read_and_dump_clock_info_one(clkarg);
217 else
218 read_and_dump_clock_info(verbose);
219 }
Amit Arora6e774cd2010-10-28 11:31:24 +0530220 }
Amit Arora47fd9182010-08-24 13:26:06 +0530221
Amit Arorac93e0712010-10-07 13:51:53 +0530222 if (selectedwindow == SENSOR) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530223 if (!dump) {
224 create_selectedwindow();
225 print_sensor_header();
226 } else
227 read_and_print_sensor_info(verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530228 }
229
230 if (dump)
231 break;
232
233 FD_ZERO(&readfds);
234 FD_SET(0, &readfds);
235 tval.tv_sec = ticktime;
236 tval.tv_usec = (ticktime - tval.tv_sec) * 1000000;
237
238 key = select(1, &readfds, NULL, NULL, &tval);
239
240 if (key) {
241 char keychar;
Amit Arorac93e0712010-10-07 13:51:53 +0530242 int keystroke = getch();
Amit Arora3bd79162010-12-01 13:51:42 +0530243 int oldselectedwin = selectedwindow;
Amit Arora97006e52010-10-28 11:56:08 +0530244
Amit Arora47fd9182010-08-24 13:26:06 +0530245 if (keystroke == EOF)
246 exit(0);
247
Amit Arora6e774cd2010-10-28 11:31:24 +0530248 if (keystroke == KEY_RIGHT || keystroke == 9)
249 selectedwindow++;
Amit Arorac93e0712010-10-07 13:51:53 +0530250
Amit Arora6e774cd2010-10-28 11:31:24 +0530251 if (keystroke == KEY_LEFT || keystroke == 353)
252 selectedwindow--;
Amit Arorac93e0712010-10-07 13:51:53 +0530253
Amit Arora6e774cd2010-10-28 11:31:24 +0530254 if (selectedwindow >= TOTAL_FEATURE_WINS)
255 selectedwindow = 0;
Amit Arorac93e0712010-10-07 13:51:53 +0530256
Amit Arora6e774cd2010-10-28 11:31:24 +0530257 if (selectedwindow < 0)
258 selectedwindow = TOTAL_FEATURE_WINS - 1;
Amit Arorac93e0712010-10-07 13:51:53 +0530259
Amit Arora6e774cd2010-10-28 11:31:24 +0530260 if (selectedwindow == CLOCK) {
261 if (keystroke == KEY_DOWN)
262 highlighted_row++;
263 if (keystroke == KEY_UP && highlighted_row > 0)
264 highlighted_row--;
Amit Arora3bd79162010-12-01 13:51:42 +0530265 if (keystroke == 47)
266 findparent_ncurses = 1;
267
268 if ((keystroke == 27 || oldselectedwin !=
269 selectedwindow) && findparent_ncurses) {
270 findparent_ncurses = 0;
271 clkname_str[0] = '\0';
272 }
273
274 if (findparent_ncurses && keystroke != 13) {
275 int len = strlen(clkname_str);
276 char str[2];
277
278 if (keystroke == 263) {
279 if (len > 0)
280 len--;
281
282 clkname_str[len] = '\0';
283 } else {
284 if (strlen(clkname_str) ||
285 keystroke != '/') {
286 str[0] = keystroke;
287 str[1] = '\0';
288 if (len < 63)
289 strcat(clkname_str,
290 str);
291 }
292 }
293 }
Amit Arora6e774cd2010-10-28 11:31:24 +0530294 }
Amit Arora728e0c92010-09-14 12:06:09 +0530295
Amit Arora47fd9182010-08-24 13:26:06 +0530296 keychar = toupper(keystroke);
Amit Arora3bd79162010-12-01 13:51:42 +0530297//#define DEBUG
298#ifdef DEBUG
299 killall_windows(1); fini_curses();
300 printf("key entered %d:%c\n", keystroke, keychar);
301 exit(1);
302#endif
Amit Arorac93e0712010-10-07 13:51:53 +0530303
Amit Arora6e774cd2010-10-28 11:31:24 +0530304 if (keystroke == 13)
305 enter_hit = 1;
Amit Arora728e0c92010-09-14 12:06:09 +0530306
Amit Arora3bd79162010-12-01 13:51:42 +0530307 if (keychar == 'Q' && !findparent_ncurses)
Amit Arora47fd9182010-08-24 13:26:06 +0530308 exit(0);
Amit Aroraa06a7302010-12-02 15:59:37 +0530309 if (keychar == 'R') {
310 refreshwin = 1;
Amit Arora47fd9182010-08-24 13:26:06 +0530311 ticktime = 3;
Amit Aroraa06a7302010-12-02 15:59:37 +0530312 } else
313 refreshwin = 0;
Amit Arora47fd9182010-08-24 13:26:06 +0530314 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530315 }
Amit Arora85fd4952010-08-05 14:04:50 +0530316 exit(0);
Amit Arorae9e16b02010-08-03 10:15:20 +0530317}
Amit Arora17552782010-12-02 12:23:14 +0530318