blob: 5ebb01e5fa93aab833d04f50d89a404b24f7a18c [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 Arora3bd79162010-12-01 13:51:42 +053057 int enter_hit = 0, verbose = 0, findparent_ncurses = 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
67 * -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 }
138
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 Arorafefe8bf2010-08-05 13:31:20 +0530143
Amit Aroraf4fb8102010-11-30 13:55:50 +0530144 if (findparent && (!clocks || !dump)) {
145 fprintf(stderr, "-p option passed without -c and -d."
146 " Exiting...\n");
147 usage(argv);
148 }
149
Amit Arora6e774cd2010-10-28 11:31:24 +0530150 if (!dump)
151 selectedwindow = REGULATOR;
Amit Arorae9e16b02010-08-03 10:15:20 +0530152
153 init_regulator_ds();
154
Amit Arora47fd9182010-08-24 13:26:06 +0530155 while(1) {
156 int key = 0;
157 struct timeval tval;
158 fd_set readfds;
Amit Arorae9e16b02010-08-03 10:15:20 +0530159
Amit Arora47fd9182010-08-24 13:26:06 +0530160 if (!dump) {
Amit Arorac93e0712010-10-07 13:51:53 +0530161 if(firsttime[0])
Amit Arora47fd9182010-08-24 13:26:06 +0530162 init_curses();
Amit Arora47fd9182010-08-24 13:26:06 +0530163 create_windows();
164 show_header();
165 }
Amit Aroraac4e8652010-11-09 11:16:53 +0530166
Amit Arora47fd9182010-08-24 13:26:06 +0530167
Amit Arorac93e0712010-10-07 13:51:53 +0530168 if (selectedwindow == REGULATOR) {
Amit Arora47fd9182010-08-24 13:26:06 +0530169 read_regulator_info();
Amit Arora728e0c92010-09-14 12:06:09 +0530170 if (!dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530171 create_selectedwindow();
Amit Arora47fd9182010-08-24 13:26:06 +0530172 show_regulator_info(verbose);
Amit Arora6e774cd2010-10-28 11:31:24 +0530173 }
Amit Arora47fd9182010-08-24 13:26:06 +0530174 else
175 print_regulator_info(verbose);
176 }
177
Amit Arora6e774cd2010-10-28 11:31:24 +0530178 if (selectedwindow == CLOCK) {
Amit Arora04f97742010-11-16 11:28:57 +0530179 int ret = 0;
Amit Arora6e774cd2010-10-28 11:31:24 +0530180 if (firsttime[CLOCK]) {
Amit Arora04f97742010-11-16 11:28:57 +0530181 ret = init_clock_details();
182 if (!ret)
183 firsttime[CLOCK] = 0;
Amit Arora3bd79162010-12-01 13:51:42 +0530184 strcpy(clkname_str, "");
Amit Arora6e774cd2010-10-28 11:31:24 +0530185 }
Amit Arora04f97742010-11-16 11:28:57 +0530186 if (!ret && !dump) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530187 int hrow;
Amit Arora0e512722010-10-01 12:24:16 +0530188
Amit Arora6e774cd2010-10-28 11:31:24 +0530189 create_selectedwindow();
Amit Arora3bd79162010-12-01 13:51:42 +0530190 if (!findparent_ncurses) {
191 hrow = read_and_print_clock_info(
192 verbose,
Amit Arora6e774cd2010-10-28 11:31:24 +0530193 highlighted_row,
194 enter_hit);
Amit Arora3bd79162010-12-01 13:51:42 +0530195 highlighted_row = hrow;
196 enter_hit = 0;
197 } else
198 find_parents_for_clock(clkname_str,
199 enter_hit);
Amit Arora04f97742010-11-16 11:28:57 +0530200 }
Amit Aroraf4fb8102010-11-30 13:55:50 +0530201 if (!ret && dump) {
202 if (findparent)
203 read_and_dump_clock_info_one(clkarg);
204 else
205 read_and_dump_clock_info(verbose);
206 }
Amit Arora6e774cd2010-10-28 11:31:24 +0530207 }
Amit Arora47fd9182010-08-24 13:26:06 +0530208
Amit Arorac93e0712010-10-07 13:51:53 +0530209 if (selectedwindow == SENSOR) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530210 if (!dump) {
211 create_selectedwindow();
212 print_sensor_header();
213 } else
214 read_and_print_sensor_info(verbose);
Amit Arora47fd9182010-08-24 13:26:06 +0530215 }
216
217 if (dump)
218 break;
219
220 FD_ZERO(&readfds);
221 FD_SET(0, &readfds);
222 tval.tv_sec = ticktime;
223 tval.tv_usec = (ticktime - tval.tv_sec) * 1000000;
224
225 key = select(1, &readfds, NULL, NULL, &tval);
226
227 if (key) {
228 char keychar;
Amit Arorac93e0712010-10-07 13:51:53 +0530229 int keystroke = getch();
Amit Arora3bd79162010-12-01 13:51:42 +0530230 int oldselectedwin = selectedwindow;
Amit Arora97006e52010-10-28 11:56:08 +0530231
Amit Arora47fd9182010-08-24 13:26:06 +0530232 if (keystroke == EOF)
233 exit(0);
234
Amit Arora6e774cd2010-10-28 11:31:24 +0530235 if (keystroke == KEY_RIGHT || keystroke == 9)
236 selectedwindow++;
Amit Arorac93e0712010-10-07 13:51:53 +0530237
Amit Arora6e774cd2010-10-28 11:31:24 +0530238 if (keystroke == KEY_LEFT || keystroke == 353)
239 selectedwindow--;
Amit Arorac93e0712010-10-07 13:51:53 +0530240
Amit Arora6e774cd2010-10-28 11:31:24 +0530241 if (selectedwindow >= TOTAL_FEATURE_WINS)
242 selectedwindow = 0;
Amit Arorac93e0712010-10-07 13:51:53 +0530243
Amit Arora6e774cd2010-10-28 11:31:24 +0530244 if (selectedwindow < 0)
245 selectedwindow = TOTAL_FEATURE_WINS - 1;
Amit Arorac93e0712010-10-07 13:51:53 +0530246
Amit Arora6e774cd2010-10-28 11:31:24 +0530247 if (selectedwindow == CLOCK) {
248 if (keystroke == KEY_DOWN)
249 highlighted_row++;
250 if (keystroke == KEY_UP && highlighted_row > 0)
251 highlighted_row--;
Amit Arora3bd79162010-12-01 13:51:42 +0530252 if (keystroke == 47)
253 findparent_ncurses = 1;
254
255 if ((keystroke == 27 || oldselectedwin !=
256 selectedwindow) && findparent_ncurses) {
257 findparent_ncurses = 0;
258 clkname_str[0] = '\0';
259 }
260
261 if (findparent_ncurses && keystroke != 13) {
262 int len = strlen(clkname_str);
263 char str[2];
264
265 if (keystroke == 263) {
266 if (len > 0)
267 len--;
268
269 clkname_str[len] = '\0';
270 } else {
271 if (strlen(clkname_str) ||
272 keystroke != '/') {
273 str[0] = keystroke;
274 str[1] = '\0';
275 if (len < 63)
276 strcat(clkname_str,
277 str);
278 }
279 }
280 }
Amit Arora6e774cd2010-10-28 11:31:24 +0530281 }
Amit Arora728e0c92010-09-14 12:06:09 +0530282
Amit Arora47fd9182010-08-24 13:26:06 +0530283 keychar = toupper(keystroke);
Amit Arora3bd79162010-12-01 13:51:42 +0530284//#define DEBUG
285#ifdef DEBUG
286 killall_windows(1); fini_curses();
287 printf("key entered %d:%c\n", keystroke, keychar);
288 exit(1);
289#endif
Amit Arorac93e0712010-10-07 13:51:53 +0530290
Amit Arora6e774cd2010-10-28 11:31:24 +0530291 if (keystroke == 13)
292 enter_hit = 1;
Amit Arora728e0c92010-09-14 12:06:09 +0530293
Amit Arora3bd79162010-12-01 13:51:42 +0530294 if (keychar == 'Q' && !findparent_ncurses)
Amit Arora47fd9182010-08-24 13:26:06 +0530295 exit(0);
296 if (keychar == 'R')
297 ticktime = 3;
298 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530299 }
Amit Arora85fd4952010-08-05 14:04:50 +0530300 exit(0);
Amit Arorae9e16b02010-08-03 10:15:20 +0530301}
Amit Arora17552782010-12-02 12:23:14 +0530302