blob: 3f4d60c5f32d238bb70cedeac5fe9a1bba367af1 [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 Arora728e0c92010-09-14 12:06:09 +053022double ticktime = 10.0; /* in seconds */
Amit Arorae9e16b02010-08-03 10:15:20 +053023
Amit Arorac93e0712010-10-07 13:51:53 +053024char *win_names[TOTAL_FEATURE_WINS] = {
Amit Arora6e774cd2010-10-28 11:31:24 +053025 "Clocks",
Amit Kucheriaee851a12011-01-12 04:57:48 +053026 "Regulators",
27 "Sensors"
28};
Amit Arorac93e0712010-10-07 13:51:53 +053029
Amit Arora422c52f2010-12-02 16:22:29 +053030void usage(void)
Amit Arorae9e16b02010-08-03 10:15:20 +053031{
Amit Arora422c52f2010-12-02 16:22:29 +053032 printf("Usage: powerdebug [OPTIONS]\n");
33 printf("\n");
34 printf("powerdebug -d [ -r ] [ -s ] [ -c [ -p <clock-name> ] ] "
35 "[ -v ]\n");
36 printf("powerdebug [ -r | -s | -c ]\n");
Amit Arora17552782010-12-02 12:23:14 +053037 printf(" -r, --regulator Show regulator information\n");
38 printf(" -s, --sensor Show sensor information\n");
39 printf(" -c, --clock Show clock information\n");
Amit Arora422c52f2010-12-02 16:22:29 +053040 printf(" -p, --findparents Show all parents for a particular"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060041 " clock\n");
Amit Arora17552782010-12-02 12:23:14 +053042 printf(" -t, --time Set ticktime in seconds (eg. 10.0)\n");
43 printf(" -d, --dump Dump information once (no refresh)\n");
Amit Arora422c52f2010-12-02 16:22:29 +053044 printf(" -v, --verbose Verbose mode (use with -r and/or"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060045 " -s)\n");
Amit Arora17552782010-12-02 12:23:14 +053046 printf(" -V, --version Show Version\n");
47 printf(" -h, --help Help\n");
Amit Arorae9e16b02010-08-03 10:15:20 +053048
Amit Arora17552782010-12-02 12:23:14 +053049 exit(0);
Amit Arorae9e16b02010-08-03 10:15:20 +053050}
51
Amit Arora17552782010-12-02 12:23:14 +053052void version()
Amit Arorae9e16b02010-08-03 10:15:20 +053053{
Amit Arora17552782010-12-02 12:23:14 +053054 printf("powerdebug version %s\n", VERSION);
55 exit(0);
Amit Arorae9e16b02010-08-03 10:15:20 +053056}
57
Amit Arorae9e16b02010-08-03 10:15:20 +053058int main(int argc, char **argv)
59{
Amit Arorac93e0712010-10-07 13:51:53 +053060 int c, i;
61 int firsttime[TOTAL_FEATURE_WINS];
Amit Aroraa06a7302010-12-02 15:59:37 +053062 int enter_hit = 0, verbose = 0, findparent_ncurses = 0, refreshwin = 0;
Amit Aroraf4fb8102010-11-30 13:55:50 +053063 int regulators = 0, sensors = 0, clocks = 0, findparent = 0;
Amit Arora3bd79162010-12-01 13:51:42 +053064 char clkarg[64], clkname_str[64];
Amit Arorae9e16b02010-08-03 10:15:20 +053065
Amit Arora6e774cd2010-10-28 11:31:24 +053066 for (i = 0; i < TOTAL_FEATURE_WINS; i++)
67 firsttime[i] = 1;
Amit Arorac93e0712010-10-07 13:51:53 +053068
Amit Arorafefe8bf2010-08-05 13:31:20 +053069 /*
70 * Options:
Amit Arora6e774cd2010-10-28 11:31:24 +053071 * -r, --regulator : regulator
Amit Aroraa06a7302010-12-02 15:59:37 +053072 * -s, --sensor : sensors
73 * -c, --clock : clocks
Amit Aroraf4fb8102010-11-30 13:55:50 +053074 * -p, --findparents : clockname whose parents have to be found
Amit Arora728e0c92010-09-14 12:06:09 +053075 * -t, --time : ticktime
Amit Arora47fd9182010-08-24 13:26:06 +053076 * -d, --dump : dump
Amit Arorafefe8bf2010-08-05 13:31:20 +053077 * -v, --verbose : verbose
78 * -V, --version : version
79 * -h, --help : help
80 * no option / default : show usage!
Amit Arorae9e16b02010-08-03 10:15:20 +053081 */
82
Amit Arorafefe8bf2010-08-05 13:31:20 +053083 while (1) {
84 int optindex = 0;
85 static struct option long_options[] = {
Amit Arora6e774cd2010-10-28 11:31:24 +053086 {"regulator", 0, 0, 'r'},
87 {"sensor", 0, 0, 's'},
88 {"clock", 0, 0, 'c'},
Amit Arora203f4d42010-12-01 13:52:01 +053089 {"findparents", 1, 0, 'p'},
90 {"time", 1, 0, 't'},
Amit Arora47fd9182010-08-24 13:26:06 +053091 {"dump", 0, 0, 'd'},
Amit Arorafefe8bf2010-08-05 13:31:20 +053092 {"verbose", 0, 0, 'v'},
93 {"version", 0, 0, 'V'},
94 {"help", 0, 0, 'h'},
95 {0, 0, 0, 0}
96 };
97
Amit Aroraf4fb8102010-11-30 13:55:50 +053098 c = getopt_long(argc, argv, "rscp:t:dvVh", long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +053099 if (c == -1)
100 break;
101
102 switch (c) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530103 case 'r':
104 regulators = 1;
105 selectedwindow = REGULATOR;
106 break;
107 case 's':
108 sensors = 1;
109 selectedwindow = SENSOR;
110 break;
111 case 'c':
112 clocks = 1;
113 selectedwindow = CLOCK;
114 break;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530115 case 'p':
116 findparent = 1;
117 strcpy(clkarg, optarg);
118 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530119 case 't':
120 ticktime = strtod(optarg, NULL);
121 break;
122 case 'd':
123 dump = 1;
124 break;
125 case 'v':
126 verbose = 1;
127 break;
128 case 'V':
129 version();
130 break;
131 case 'h':
Amit Arora422c52f2010-12-02 16:22:29 +0530132 usage();
Amit Arora6e774cd2010-10-28 11:31:24 +0530133 break;
134 case '?':
135 fprintf (stderr, "%s: Unknown option %c'.\n",
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600136 argv[0], optopt);
Amit Arora6e774cd2010-10-28 11:31:24 +0530137 exit(1);
138 default:
Amit Arora422c52f2010-12-02 16:22:29 +0530139 usage();
Amit Arora6e774cd2010-10-28 11:31:24 +0530140 break;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530141 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530142 }
Amit Aroraa06a7302010-12-02 15:59:37 +0530143
144 if (dump && !(regulators || clocks || sensors)) {
Amit Arora422c52f2010-12-02 16:22:29 +0530145 //fprintf(stderr, "Dump mode (-d) supported only with -c, -r "
146 // "or -s ..\n");
147 //usage();
148 // By Default lets show everything we have!
149 regulators = clocks = sensors = 1;
Amit Aroraa06a7302010-12-02 15:59:37 +0530150 }
Amit Arorafefe8bf2010-08-05 13:31:20 +0530151
Amit Aroraf4fb8102010-11-30 13:55:50 +0530152 if (findparent && (!clocks || !dump)) {
153 fprintf(stderr, "-p option passed without -c and -d."
154 " Exiting...\n");
Amit Arora422c52f2010-12-02 16:22:29 +0530155 usage();
Amit Aroraf4fb8102010-11-30 13:55:50 +0530156 }
157
Amit Aroraa06a7302010-12-02 15:59:37 +0530158 if (!dump && selectedwindow == -1)
Amit Kucheriaee851a12011-01-12 04:57:48 +0530159 selectedwindow = CLOCK;
Amit Arorae9e16b02010-08-03 10:15:20 +0530160
161 init_regulator_ds();
162
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600163 while (1) {
Amit Arora47fd9182010-08-24 13:26:06 +0530164 int key = 0;
165 struct timeval tval;
166 fd_set readfds;
Amit Arorae9e16b02010-08-03 10:15:20 +0530167
Amit Arora47fd9182010-08-24 13:26:06 +0530168 if (!dump) {
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600169 if (firsttime[0])
Amit Arora47fd9182010-08-24 13:26:06 +0530170 init_curses();
Amit Arora47fd9182010-08-24 13:26:06 +0530171 create_windows();
172 show_header();
173 }
Amit Aroraac4e8652010-11-09 11:16:53 +0530174
Amit Arora422c52f2010-12-02 16:22:29 +0530175 if (regulators || 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 Arora422c52f2010-12-02 16:22:29 +0530185 if (clocks || 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(
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600205 verbose,
206 highlighted_row,
207 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,
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600212 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 Arora422c52f2010-12-02 16:22:29 +0530222 if (sensors || 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 !=
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600269 selectedwindow) && findparent_ncurses) {
Amit Arora3bd79162010-12-01 13:51:42 +0530270 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 != '/') {
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600286 str[0] = keystroke;
287 str[1] = '\0';
288 if (len < 63)
289 strcat(clkname_str,
290 str);
Amit Arora3bd79162010-12-01 13:51:42 +0530291 }
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}