blob: 073fd424e90fc53dddd1481b2ac0947eee38e0ce [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 Lezcano15627482011-06-15 15:45:12 +020018#include <string.h>
19#include <stdlib.h>
20#include <stdio.h>
21#include <errno.h>
22#include <ncurses.h>
Daniel Lezcanocac52d92011-03-26 22:05:49 +010023#include "regulator.h"
Daniel Lezcano192c1d22011-03-26 22:06:14 +010024#include "display.h"
Daniel Lezcanof0e06652011-03-26 22:06:19 +010025#include "clocks.h"
Daniel Lezcano597892a2011-06-15 15:45:12 +020026#include "sensor.h"
Daniel Lezcano716b23e2011-08-25 15:46:13 +020027#include "gpio.h"
Daniel Lezcanodb145802011-06-21 00:57:08 +020028#include "mainloop.h"
Amit Arorae9e16b02010-08-03 10:15:20 +053029#include "powerdebug.h"
30
Amit Arora422c52f2010-12-02 16:22:29 +053031void usage(void)
Amit Arorae9e16b02010-08-03 10:15:20 +053032{
Amit Arora422c52f2010-12-02 16:22:29 +053033 printf("Usage: powerdebug [OPTIONS]\n");
34 printf("\n");
35 printf("powerdebug -d [ -r ] [ -s ] [ -c [ -p <clock-name> ] ] "
36 "[ -v ]\n");
37 printf("powerdebug [ -r | -s | -c ]\n");
Amit Arora17552782010-12-02 12:23:14 +053038 printf(" -r, --regulator Show regulator information\n");
39 printf(" -s, --sensor Show sensor information\n");
40 printf(" -c, --clock Show clock information\n");
Amit Arora422c52f2010-12-02 16:22:29 +053041 printf(" -p, --findparents Show all parents for a particular"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060042 " clock\n");
Amit Arora17552782010-12-02 12:23:14 +053043 printf(" -t, --time Set ticktime in seconds (eg. 10.0)\n");
44 printf(" -d, --dump Dump information once (no refresh)\n");
Amit Arora422c52f2010-12-02 16:22:29 +053045 printf(" -v, --verbose Verbose mode (use with -r and/or"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060046 " -s)\n");
Amit Arora17552782010-12-02 12:23:14 +053047 printf(" -V, --version Show Version\n");
48 printf(" -h, --help Help\n");
Amit Arorae9e16b02010-08-03 10:15:20 +053049}
50
Amit Arora17552782010-12-02 12:23:14 +053051void version()
Amit Arorae9e16b02010-08-03 10:15:20 +053052{
Amit Arora17552782010-12-02 12:23:14 +053053 printf("powerdebug version %s\n", VERSION);
Amit Arorae9e16b02010-08-03 10:15:20 +053054}
55
Daniel Lezcano316bcae2011-03-23 14:37:30 +010056/*
57 * Options:
Daniel Lezcano716b23e2011-08-25 15:46:13 +020058 * -r, --regulator : regulators
Daniel Lezcano316bcae2011-03-23 14:37:30 +010059 * -s, --sensor : sensors
60 * -c, --clock : clocks
Daniel Lezcano716b23e2011-08-25 15:46:13 +020061 * -g, --gpio : gpios
Daniel Lezcano316bcae2011-03-23 14:37:30 +010062 * -p, --findparents : clockname whose parents have to be found
63 * -t, --time : ticktime
64 * -d, --dump : dump
65 * -v, --verbose : verbose
66 * -V, --version : version
67 * -h, --help : help
68 * no option / default : show usage!
69 */
70
71static struct option long_options[] = {
72 { "regulator", 0, 0, 'r' },
73 { "sensor", 0, 0, 's' },
74 { "clock", 0, 0, 'c' },
Daniel Lezcano716b23e2011-08-25 15:46:13 +020075 { "gpio", 0, 0, 'g' },
Daniel Lezcano316bcae2011-03-23 14:37:30 +010076 { "findparents", 1, 0, 'p' },
77 { "time", 1, 0, 't' },
78 { "dump", 0, 0, 'd' },
79 { "verbose", 0, 0, 'v' },
80 { "version", 0, 0, 'V' },
81 { "help", 0, 0, 'h' },
82 { 0, 0, 0, 0 }
83};
84
85struct powerdebug_options {
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010086 bool verbose;
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010087 bool regulators;
88 bool sensors;
89 bool clocks;
Daniel Lezcano716b23e2011-08-25 15:46:13 +020090 bool gpios;
Daniel Lezcanoa70d9492011-03-23 14:37:40 +010091 bool dump;
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010092 unsigned int ticktime;
Daniel Lezcano558a6d52011-03-23 14:37:41 +010093 int selectedwindow;
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +010094 char *clkname;
Daniel Lezcano316bcae2011-03-23 14:37:30 +010095};
96
97int getoptions(int argc, char *argv[], struct powerdebug_options *options)
Amit Arorae9e16b02010-08-03 10:15:20 +053098{
Daniel Lezcano316bcae2011-03-23 14:37:30 +010099 int c;
Amit Arorae9e16b02010-08-03 10:15:20 +0530100
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100101 memset(options, 0, sizeof(*options));
102 options->ticktime = 10;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100103 options->selectedwindow = -1;
Amit Arorae9e16b02010-08-03 10:15:20 +0530104
Amit Arorafefe8bf2010-08-05 13:31:20 +0530105 while (1) {
106 int optindex = 0;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530107
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200108 c = getopt_long(argc, argv, "rscgp:t:dvVh",
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100109 long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +0530110 if (c == -1)
111 break;
112
113 switch (c) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530114 case 'r':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100115 options->regulators = true;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100116 options->selectedwindow = REGULATOR;
Amit Arora6e774cd2010-10-28 11:31:24 +0530117 break;
118 case 's':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100119 options->sensors = true;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100120 options->selectedwindow = SENSOR;
Amit Arora6e774cd2010-10-28 11:31:24 +0530121 break;
122 case 'c':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100123 options->clocks = true;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100124 options->selectedwindow = CLOCK;
Amit Arora6e774cd2010-10-28 11:31:24 +0530125 break;
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200126 case 'g':
127 options->gpios = true;
128 options->selectedwindow = GPIO;
129 break;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530130 case 'p':
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100131 options->clkname = strdup(optarg);
132 if (!options->clkname) {
Daniel Lezcano9420fde2011-03-23 14:37:31 +0100133 fprintf(stderr, "failed to allocate memory");
134 return -1;
135 }
Amit Kucheriaeab558a2011-03-25 09:51:41 +0200136 options->dump = true; /* Assume -dc in case of -p */
137 options->clocks = true;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530138 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530139 case 't':
Daniel Lezcano7f112da2011-03-23 14:37:32 +0100140 options->ticktime = atoi(optarg);
Amit Arora6e774cd2010-10-28 11:31:24 +0530141 break;
142 case 'd':
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100143 options->dump = true;
Amit Arora6e774cd2010-10-28 11:31:24 +0530144 break;
145 case 'v':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100146 options->verbose = true;
Amit Arora6e774cd2010-10-28 11:31:24 +0530147 break;
148 case 'V':
149 version();
150 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530151 case '?':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100152 fprintf(stderr, "%s: Unknown option %c'.\n",
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600153 argv[0], optopt);
Amit Arora6e774cd2010-10-28 11:31:24 +0530154 default:
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100155 return -1;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530156 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530157 }
Amit Aroraa06a7302010-12-02 15:59:37 +0530158
Daniel Lezcano934fc092011-03-26 22:06:18 +0100159 /* No system specified to be dump, let's default to all */
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200160 if (!options->regulators && !options->clocks &&
161 !options->sensors && !options->gpios)
162 options->regulators = options->clocks =
163 options->sensors = options->gpios = true;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530164
Daniel Lezcanoc9c14622011-03-26 22:06:10 +0100165 if (options->selectedwindow == -1)
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100166 options->selectedwindow = CLOCK;
Amit Arorae9e16b02010-08-03 10:15:20 +0530167
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100168 return 0;
169}
170
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200171static int powerdebug_dump(struct powerdebug_options *options)
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100172{
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200173 if (options->regulators)
174 regulator_dump();
Daniel Lezcanob5746712011-03-26 22:06:05 +0100175
Daniel Lezcanoc7891942011-06-08 23:30:00 +0200176 if (options->clocks)
Daniel Lezcano597892a2011-06-15 15:45:12 +0200177 clock_dump(options->clkname);
Daniel Lezcanob5746712011-03-26 22:06:05 +0100178
179 if (options->sensors)
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200180 sensor_dump();
Daniel Lezcanob5746712011-03-26 22:06:05 +0100181
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200182 if (options->gpios)
183 gpio_dump();
184
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100185 return 0;
186}
187
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200188static int powerdebug_display(struct powerdebug_options *options)
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100189{
Daniel Lezcano7b3da502011-06-15 15:45:12 +0200190 if (display_init(options->selectedwindow)) {
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100191 printf("failed to initialize display\n");
192 return -1;
193 }
194
Daniel Lezcanodb145802011-06-21 00:57:08 +0200195 if (mainloop(options->ticktime * 1000))
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100196 return -1;
197
198 return 0;
199}
200
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100201static struct powerdebug_options *powerdebug_init(void)
202{
203 struct powerdebug_options *options;
204
205 options = malloc(sizeof(*options));
206 if (!options)
207 return NULL;
208
209 memset(options, 0, sizeof(*options));
210
211 return options;
212}
213
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100214int main(int argc, char **argv)
215{
216 struct powerdebug_options *options;
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200217 int ret;
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100218
Sanjay Singh Rawat9fe0c052013-02-22 17:57:18 +0530219#ifdef __ANDROID__
220 if (setenv("TERM", "xterm", 1) < 0) {
221 fprintf(stderr, "setenv failure");
222 return 1;
223 }
224 if (setenv("TERMINFO", "/system/etc/terminfo", 1) < 0) {
225 fprintf(stderr, "setenv failure");
226 return 1;
227 }
228#endif
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100229 options = powerdebug_init();
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100230 if (!options) {
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100231 fprintf(stderr, "not enough memory to allocate options\n");
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100232 return 1;
233 }
234
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100235 if (getoptions(argc, argv, options)) {
236 usage();
237 return 1;
238 }
239
Daniel Lezcanodb145802011-06-21 00:57:08 +0200240 if (mainloop_init()) {
241 fprintf(stderr, "failed to initialize the mainloop\n");
242 return 1;
243 }
244
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200245 if (regulator_init()) {
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +0100246 printf("not enough memory to allocate regulators info\n");
Daniel Lezcano6e48fa42011-03-26 22:06:23 +0100247 options->regulators = false;
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +0100248 }
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100249
Daniel Lezcanof0e06652011-03-26 22:06:19 +0100250 if (clock_init()) {
251 printf("failed to initialize clock details (check debugfs)\n");
252 options->clocks = false;
253 }
254
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200255 if (sensor_init()) {
256 printf("failed to initialize sensors\n");
257 options->sensors = false;
258 }
259
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200260 if (gpio_init()) {
261 printf("failed to initialize gpios\n");
262 options->gpios = false;
263 }
264
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200265 ret = options->dump ? powerdebug_dump(options) :
266 powerdebug_display(options);
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100267
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100268 return ret < 0;
Amit Arorae9e16b02010-08-03 10:15:20 +0530269}