blob: e702011c87f9d15b161b3b17fe7dbc3ea58726af [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>
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010017#include <stdbool.h>
Daniel Lezcano2ef06e92011-05-24 15:27:49 +020018#include <math.h>
Daniel Lezcanocac52d92011-03-26 22:05:49 +010019#include "regulator.h"
Daniel Lezcano192c1d22011-03-26 22:06:14 +010020#include "display.h"
Daniel Lezcanof0e06652011-03-26 22:06:19 +010021#include "clocks.h"
Daniel Lezcano597892a2011-06-15 15:45:12 +020022#include "sensor.h"
Amit Arorae9e16b02010-08-03 10:15:20 +053023#include "powerdebug.h"
24
Amit Arora422c52f2010-12-02 16:22:29 +053025void usage(void)
Amit Arorae9e16b02010-08-03 10:15:20 +053026{
Amit Arora422c52f2010-12-02 16:22:29 +053027 printf("Usage: powerdebug [OPTIONS]\n");
28 printf("\n");
29 printf("powerdebug -d [ -r ] [ -s ] [ -c [ -p <clock-name> ] ] "
30 "[ -v ]\n");
31 printf("powerdebug [ -r | -s | -c ]\n");
Amit Arora17552782010-12-02 12:23:14 +053032 printf(" -r, --regulator Show regulator information\n");
33 printf(" -s, --sensor Show sensor information\n");
34 printf(" -c, --clock Show clock information\n");
Amit Arora422c52f2010-12-02 16:22:29 +053035 printf(" -p, --findparents Show all parents for a particular"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060036 " clock\n");
Amit Arora17552782010-12-02 12:23:14 +053037 printf(" -t, --time Set ticktime in seconds (eg. 10.0)\n");
38 printf(" -d, --dump Dump information once (no refresh)\n");
Amit Arora422c52f2010-12-02 16:22:29 +053039 printf(" -v, --verbose Verbose mode (use with -r and/or"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060040 " -s)\n");
Amit Arora17552782010-12-02 12:23:14 +053041 printf(" -V, --version Show Version\n");
42 printf(" -h, --help Help\n");
Amit Arorae9e16b02010-08-03 10:15:20 +053043}
44
Amit Arora17552782010-12-02 12:23:14 +053045void version()
Amit Arorae9e16b02010-08-03 10:15:20 +053046{
Amit Arora17552782010-12-02 12:23:14 +053047 printf("powerdebug version %s\n", VERSION);
Amit Arorae9e16b02010-08-03 10:15:20 +053048}
49
Daniel Lezcano316bcae2011-03-23 14:37:30 +010050/*
51 * Options:
52 * -r, --regulator : regulator
53 * -s, --sensor : sensors
54 * -c, --clock : clocks
55 * -p, --findparents : clockname whose parents have to be found
56 * -t, --time : ticktime
57 * -d, --dump : dump
58 * -v, --verbose : verbose
59 * -V, --version : version
60 * -h, --help : help
61 * no option / default : show usage!
62 */
63
64static struct option long_options[] = {
65 { "regulator", 0, 0, 'r' },
66 { "sensor", 0, 0, 's' },
67 { "clock", 0, 0, 'c' },
68 { "findparents", 1, 0, 'p' },
69 { "time", 1, 0, 't' },
70 { "dump", 0, 0, 'd' },
71 { "verbose", 0, 0, 'v' },
72 { "version", 0, 0, 'V' },
73 { "help", 0, 0, 'h' },
74 { 0, 0, 0, 0 }
75};
76
77struct powerdebug_options {
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010078 bool verbose;
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010079 bool regulators;
80 bool sensors;
81 bool clocks;
Daniel Lezcanoa70d9492011-03-23 14:37:40 +010082 bool dump;
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010083 unsigned int ticktime;
Daniel Lezcano558a6d52011-03-23 14:37:41 +010084 int selectedwindow;
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +010085 char *clkname;
Daniel Lezcano316bcae2011-03-23 14:37:30 +010086};
87
88int getoptions(int argc, char *argv[], struct powerdebug_options *options)
Amit Arorae9e16b02010-08-03 10:15:20 +053089{
Daniel Lezcano316bcae2011-03-23 14:37:30 +010090 int c;
Amit Arorae9e16b02010-08-03 10:15:20 +053091
Daniel Lezcano316bcae2011-03-23 14:37:30 +010092 memset(options, 0, sizeof(*options));
93 options->ticktime = 10;
Daniel Lezcano558a6d52011-03-23 14:37:41 +010094 options->selectedwindow = -1;
Amit Arorae9e16b02010-08-03 10:15:20 +053095
Amit Arorafefe8bf2010-08-05 13:31:20 +053096 while (1) {
97 int optindex = 0;
Amit Arorafefe8bf2010-08-05 13:31:20 +053098
Daniel Lezcano316bcae2011-03-23 14:37:30 +010099 c = getopt_long(argc, argv, "rscp:t:dvVh",
100 long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +0530101 if (c == -1)
102 break;
103
104 switch (c) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530105 case 'r':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100106 options->regulators = true;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100107 options->selectedwindow = REGULATOR;
Amit Arora6e774cd2010-10-28 11:31:24 +0530108 break;
109 case 's':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100110 options->sensors = true;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100111 options->selectedwindow = SENSOR;
Amit Arora6e774cd2010-10-28 11:31:24 +0530112 break;
113 case 'c':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100114 options->clocks = true;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100115 options->selectedwindow = CLOCK;
Amit Arora6e774cd2010-10-28 11:31:24 +0530116 break;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530117 case 'p':
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100118 options->clkname = strdup(optarg);
119 if (!options->clkname) {
Daniel Lezcano9420fde2011-03-23 14:37:31 +0100120 fprintf(stderr, "failed to allocate memory");
121 return -1;
122 }
Amit Kucheriaeab558a2011-03-25 09:51:41 +0200123 options->dump = true; /* Assume -dc in case of -p */
124 options->clocks = true;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530125 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530126 case 't':
Daniel Lezcano7f112da2011-03-23 14:37:32 +0100127 options->ticktime = atoi(optarg);
Amit Arora6e774cd2010-10-28 11:31:24 +0530128 break;
129 case 'd':
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100130 options->dump = true;
Amit Arora6e774cd2010-10-28 11:31:24 +0530131 break;
132 case 'v':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100133 options->verbose = true;
Amit Arora6e774cd2010-10-28 11:31:24 +0530134 break;
135 case 'V':
136 version();
137 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530138 case '?':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100139 fprintf(stderr, "%s: Unknown option %c'.\n",
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600140 argv[0], optopt);
Amit Arora6e774cd2010-10-28 11:31:24 +0530141 default:
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100142 return -1;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530143 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530144 }
Amit Aroraa06a7302010-12-02 15:59:37 +0530145
Daniel Lezcano934fc092011-03-26 22:06:18 +0100146 /* No system specified to be dump, let's default to all */
147 if (!options->regulators && !options->clocks && !options->sensors)
148 options->regulators = options->clocks = options->sensors = true;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530149
Daniel Lezcanoc9c14622011-03-26 22:06:10 +0100150 if (options->selectedwindow == -1)
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100151 options->selectedwindow = CLOCK;
Amit Arorae9e16b02010-08-03 10:15:20 +0530152
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100153 return 0;
154}
155
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100156int keystroke_callback(bool *enter_hit, bool *findparent_ncurses,
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200157 char *clkname_str, bool *refreshwin, bool *cont,
Daniel Lezcano60a41022011-03-23 14:37:35 +0100158 struct powerdebug_options *options)
159{
160 char keychar;
161 int keystroke = getch();
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100162 int oldselectedwin = options->selectedwindow;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100163
164 if (keystroke == EOF)
165 exit(0);
166
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200167 if (keystroke == KEY_RIGHT || keystroke == '\t')
168 options->selectedwindow = display_next_panel();
Daniel Lezcano60a41022011-03-23 14:37:35 +0100169
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200170 if (keystroke == KEY_LEFT || keystroke == KEY_BTAB)
171 options->selectedwindow = display_prev_panel();
Daniel Lezcano60a41022011-03-23 14:37:35 +0100172
Daniel Lezcano2e9df762011-06-15 15:45:12 +0200173 if (keystroke == KEY_DOWN) {
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200174 display_next_line();
Daniel Lezcano2e9df762011-06-15 15:45:12 +0200175 *cont = true;
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200176 }
Daniel Lezcano2e9df762011-06-15 15:45:12 +0200177
178 if (keystroke == KEY_UP) {
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200179 display_prev_line();
Daniel Lezcano2e9df762011-06-15 15:45:12 +0200180 *cont = true;
181 }
182
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100183 if (options->selectedwindow == CLOCK) {
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200184
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200185#if 0
186 /* TODO : fix with a new panel applicable for all subsystems */
Daniel Lezcano3b42b5e2011-05-24 15:27:49 +0200187 if (keystroke == '/')
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100188 *findparent_ncurses = true;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200189#endif
Daniel Lezcano60a41022011-03-23 14:37:35 +0100190
Daniel Lezcano3b42b5e2011-05-24 15:27:49 +0200191 if ((keystroke == '\e' || oldselectedwin !=
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100192 options->selectedwindow) && *findparent_ncurses) {
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100193 *findparent_ncurses = false;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100194 clkname_str[0] = '\0';
195 }
196
Daniel Lezcano3b42b5e2011-05-24 15:27:49 +0200197 if (*findparent_ncurses && keystroke != '\r') {
Daniel Lezcano60a41022011-03-23 14:37:35 +0100198 int len = strlen(clkname_str);
199 char str[2];
200
Daniel Lezcano3b42b5e2011-05-24 15:27:49 +0200201 if (keystroke == KEY_BACKSPACE) {
Daniel Lezcano60a41022011-03-23 14:37:35 +0100202 if (len > 0)
203 len--;
204
205 clkname_str[len] = '\0';
206 } else {
207 if (strlen(clkname_str) ||
208 keystroke != '/') {
209 str[0] = keystroke;
210 str[1] = '\0';
211 if (len < 63)
212 strcat(clkname_str,
213 str);
214 }
215 }
216 }
217 }
218
219 keychar = toupper(keystroke);
220//#define DEBUG
221#ifdef DEBUG
Daniel Lezcano1c25df92011-06-08 23:30:00 +0200222 fini_curses();
Daniel Lezcano60a41022011-03-23 14:37:35 +0100223 printf("key entered %d:%c\n", keystroke, keychar);
224 exit(1);
225#endif
226
Daniel Lezcano3b42b5e2011-05-24 15:27:49 +0200227 if (keystroke == '\r')
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100228 *enter_hit = true;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100229
230 if (keychar == 'Q' && !*findparent_ncurses)
231 return 1;
232 if (keychar == 'R') {
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100233 *refreshwin = true;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100234 options->ticktime = 3;
235 } else
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100236 *refreshwin = false;
Daniel Lezcano60a41022011-03-23 14:37:35 +0100237
238 return 0;
239}
240
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200241int mainloop(struct powerdebug_options *options)
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100242{
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100243 bool findparent_ncurses = false;
244 bool refreshwin = false;
245 bool enter_hit = false;
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200246 bool cont = false;
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100247 char clkname_str[64];
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100248
Daniel Lezcano934fc092011-03-26 22:06:18 +0100249 strcpy(clkname_str, "");
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100250
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600251 while (1) {
Amit Arora47fd9182010-08-24 13:26:06 +0530252 int key = 0;
253 struct timeval tval;
254 fd_set readfds;
Amit Arorae9e16b02010-08-03 10:15:20 +0530255
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200256 if (options->selectedwindow == REGULATOR)
257 regulator_display();
Amit Arora47fd9182010-08-24 13:26:06 +0530258
Daniel Lezcanod96731a2011-06-15 15:45:12 +0200259 if (options->selectedwindow == SENSOR)
260 sensor_display();
261
Daniel Lezcano95b0dac2011-06-08 23:30:00 +0200262 if (options->selectedwindow == CLOCK) {
Daniel Lezcano934fc092011-03-26 22:06:18 +0100263
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200264 if (!cont) {
Amit Aroraa06a7302010-12-02 15:59:37 +0530265
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200266 if (!findparent_ncurses) {
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200267
Daniel Lezcano05916f82011-06-08 23:30:01 +0200268 if (enter_hit)
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200269 clock_toggle_expanded();
Daniel Lezcano05916f82011-06-08 23:30:01 +0200270
Daniel Lezcano597892a2011-06-15 15:45:12 +0200271 clock_display();
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200272 enter_hit = false;
273 } else
274 find_parents_for_clock(clkname_str,
275 enter_hit);
276 } else cont = false;
Amit Arora6e774cd2010-10-28 11:31:24 +0530277 }
Amit Arora47fd9182010-08-24 13:26:06 +0530278
Amit Arora47fd9182010-08-24 13:26:06 +0530279 FD_ZERO(&readfds);
280 FD_SET(0, &readfds);
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100281 tval.tv_sec = options->ticktime;
282 tval.tv_usec = (options->ticktime - tval.tv_sec) * 1000000;
Amit Arora47fd9182010-08-24 13:26:06 +0530283
Daniel Lezcanoe8cf9b82011-06-08 23:30:01 +0200284 again:
Amit Arora47fd9182010-08-24 13:26:06 +0530285 key = select(1, &readfds, NULL, NULL, &tval);
Daniel Lezcano60a41022011-03-23 14:37:35 +0100286 if (!key)
287 continue;
Amit Arora47fd9182010-08-24 13:26:06 +0530288
Daniel Lezcanoe8cf9b82011-06-08 23:30:01 +0200289 if (key < 0) {
290 if (errno == EINTR)
291 goto again;
292 break;
293 }
294
Daniel Lezcano60a41022011-03-23 14:37:35 +0100295 if (keystroke_callback(&enter_hit, &findparent_ncurses,
Daniel Lezcano2adc48d2011-06-08 23:30:01 +0200296 clkname_str, &refreshwin, &cont, options))
Daniel Lezcano60a41022011-03-23 14:37:35 +0100297 break;
Amit Arora97006e52010-10-28 11:56:08 +0530298
Amit Arorae9e16b02010-08-03 10:15:20 +0530299 }
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100300
301 return 0;
302}
303
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200304static int powerdebug_dump(struct powerdebug_options *options)
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100305{
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200306 if (options->regulators)
307 regulator_dump();
Daniel Lezcanob5746712011-03-26 22:06:05 +0100308
Daniel Lezcanoc7891942011-06-08 23:30:00 +0200309 if (options->clocks)
Daniel Lezcano597892a2011-06-15 15:45:12 +0200310 clock_dump(options->clkname);
Daniel Lezcanob5746712011-03-26 22:06:05 +0100311
312 if (options->sensors)
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200313 sensor_dump();
Daniel Lezcanob5746712011-03-26 22:06:05 +0100314
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100315 return 0;
316}
317
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200318static int powerdebug_display(struct powerdebug_options *options)
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100319{
Daniel Lezcano7b3da502011-06-15 15:45:12 +0200320 if (display_init(options->selectedwindow)) {
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100321 printf("failed to initialize display\n");
322 return -1;
323 }
324
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200325 if (mainloop(options))
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100326 return -1;
327
328 return 0;
329}
330
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100331static struct powerdebug_options *powerdebug_init(void)
332{
333 struct powerdebug_options *options;
334
335 options = malloc(sizeof(*options));
336 if (!options)
337 return NULL;
338
339 memset(options, 0, sizeof(*options));
340
341 return options;
342}
343
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100344int main(int argc, char **argv)
345{
346 struct powerdebug_options *options;
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200347 int ret;
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100348
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100349 options = powerdebug_init();
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100350 if (!options) {
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100351 fprintf(stderr, "not enough memory to allocate options\n");
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100352 return 1;
353 }
354
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100355 if (getoptions(argc, argv, options)) {
356 usage();
357 return 1;
358 }
359
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200360 if (regulator_init()) {
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +0100361 printf("not enough memory to allocate regulators info\n");
Daniel Lezcano6e48fa42011-03-26 22:06:23 +0100362 options->regulators = false;
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +0100363 }
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100364
Daniel Lezcanof0e06652011-03-26 22:06:19 +0100365 if (clock_init()) {
366 printf("failed to initialize clock details (check debugfs)\n");
367 options->clocks = false;
368 }
369
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200370 if (sensor_init()) {
371 printf("failed to initialize sensors\n");
372 options->sensors = false;
373 }
374
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200375 ret = options->dump ? powerdebug_dump(options) :
376 powerdebug_display(options);
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100377
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100378 return ret < 0;
Amit Arorae9e16b02010-08-03 10:15:20 +0530379}