blob: 0f5c98a84b5cf961243e471b6b8f240f5e97f3a9 [file] [log] [blame]
Daniel Lezcano7c15fad2016-02-18 15:57:43 +00001/*
2 * Power debug tool (powerdebug)
Amit Arora39f29542010-09-14 12:03:22 +05303 *
Daniel Lezcano7c15fad2016-02-18 15:57:43 +00004 * Copyright (C) 2016, Linaro Limited.
Amit Arora39f29542010-09-14 12:03:22 +05305 *
Daniel Lezcano7c15fad2016-02-18 15:57:43 +00006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
Amit Arora39f29542010-09-14 12:03:22 +053010 *
Daniel Lezcano7c15fad2016-02-18 15:57:43 +000011 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 */
Amit Arora39f29542010-09-14 12:03:22 +053021
Amit Arorae9e16b02010-08-03 10:15:20 +053022#include <getopt.h>
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010023#include <stdbool.h>
Daniel Lezcano15627482011-06-15 15:45:12 +020024#include <string.h>
25#include <stdlib.h>
26#include <stdio.h>
27#include <errno.h>
28#include <ncurses.h>
Sanjay Singh Rawat83b37c32013-04-17 14:57:07 +053029#include <signal.h>
Daniel Lezcano192c1d22011-03-26 22:06:14 +010030#include "display.h"
Daniel Lezcanodb145802011-06-21 00:57:08 +020031#include "mainloop.h"
Amit Arorae9e16b02010-08-03 10:15:20 +053032#include "powerdebug.h"
33
Sanjay Singh Rawat83b37c32013-04-17 14:57:07 +053034extern void sigwinch_handler(int);
35
Daniel Lezcano9db095c2016-02-18 13:05:01 +000036#define REGULATOR_OPTION 0x01
37#define SENSOR_OPTION 0x02
38#define CLOCK_OPTION 0x04
39#define GPIO_OPTION 0x08
40
41#define VERBOSE_OPTION 0x10
42#define DUMP_OPTION 0x20
43
44#define DEFAULT_OPTION (REGULATOR_OPTION | SENSOR_OPTION | \
45 CLOCK_OPTION | GPIO_OPTION)
46
Amit Arora422c52f2010-12-02 16:22:29 +053047void usage(void)
Amit Arorae9e16b02010-08-03 10:15:20 +053048{
Amit Arora422c52f2010-12-02 16:22:29 +053049 printf("Usage: powerdebug [OPTIONS]\n");
50 printf("\n");
51 printf("powerdebug -d [ -r ] [ -s ] [ -c [ -p <clock-name> ] ] "
52 "[ -v ]\n");
53 printf("powerdebug [ -r | -s | -c ]\n");
Amit Arora17552782010-12-02 12:23:14 +053054 printf(" -r, --regulator Show regulator information\n");
55 printf(" -s, --sensor Show sensor information\n");
56 printf(" -c, --clock Show clock information\n");
Amit Arora422c52f2010-12-02 16:22:29 +053057 printf(" -p, --findparents Show all parents for a particular"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060058 " clock\n");
Amit Arora17552782010-12-02 12:23:14 +053059 printf(" -t, --time Set ticktime in seconds (eg. 10.0)\n");
60 printf(" -d, --dump Dump information once (no refresh)\n");
Amit Arora422c52f2010-12-02 16:22:29 +053061 printf(" -v, --verbose Verbose mode (use with -r and/or"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060062 " -s)\n");
Amit Arora17552782010-12-02 12:23:14 +053063 printf(" -V, --version Show Version\n");
64 printf(" -h, --help Help\n");
Amit Arorae9e16b02010-08-03 10:15:20 +053065}
66
Amit Arora17552782010-12-02 12:23:14 +053067void version()
Amit Arorae9e16b02010-08-03 10:15:20 +053068{
Amit Arora17552782010-12-02 12:23:14 +053069 printf("powerdebug version %s\n", VERSION);
Amit Arorae9e16b02010-08-03 10:15:20 +053070}
71
Daniel Lezcano316bcae2011-03-23 14:37:30 +010072/*
73 * Options:
Daniel Lezcano716b23e2011-08-25 15:46:13 +020074 * -r, --regulator : regulators
Daniel Lezcano316bcae2011-03-23 14:37:30 +010075 * -s, --sensor : sensors
76 * -c, --clock : clocks
Daniel Lezcano716b23e2011-08-25 15:46:13 +020077 * -g, --gpio : gpios
Daniel Lezcano316bcae2011-03-23 14:37:30 +010078 * -p, --findparents : clockname whose parents have to be found
79 * -t, --time : ticktime
80 * -d, --dump : dump
81 * -v, --verbose : verbose
82 * -V, --version : version
83 * -h, --help : help
84 * no option / default : show usage!
85 */
86
87static struct option long_options[] = {
88 { "regulator", 0, 0, 'r' },
89 { "sensor", 0, 0, 's' },
90 { "clock", 0, 0, 'c' },
Daniel Lezcano716b23e2011-08-25 15:46:13 +020091 { "gpio", 0, 0, 'g' },
Daniel Lezcano316bcae2011-03-23 14:37:30 +010092 { "findparents", 1, 0, 'p' },
93 { "time", 1, 0, 't' },
94 { "dump", 0, 0, 'd' },
95 { "verbose", 0, 0, 'v' },
96 { "version", 0, 0, 'V' },
97 { "help", 0, 0, 'h' },
98 { 0, 0, 0, 0 }
99};
100
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100101int getoptions(int argc, char *argv[], struct powerdebug_options *options)
Amit Arorae9e16b02010-08-03 10:15:20 +0530102{
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100103 int c;
Amit Arorae9e16b02010-08-03 10:15:20 +0530104
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100105 memset(options, 0, sizeof(*options));
106 options->ticktime = 10;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100107 options->selectedwindow = -1;
Amit Arorae9e16b02010-08-03 10:15:20 +0530108
Amit Arorafefe8bf2010-08-05 13:31:20 +0530109 while (1) {
110 int optindex = 0;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530111
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200112 c = getopt_long(argc, argv, "rscgp:t:dvVh",
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100113 long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +0530114 if (c == -1)
115 break;
116
117 switch (c) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530118 case 'r':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000119 options->flags |= REGULATOR_OPTION;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100120 options->selectedwindow = REGULATOR;
Amit Arora6e774cd2010-10-28 11:31:24 +0530121 break;
122 case 's':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000123 options->flags |= SENSOR_OPTION;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100124 options->selectedwindow = SENSOR;
Amit Arora6e774cd2010-10-28 11:31:24 +0530125 break;
126 case 'c':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000127 options->flags |= CLOCK_OPTION;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100128 options->selectedwindow = CLOCK;
Amit Arora6e774cd2010-10-28 11:31:24 +0530129 break;
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200130 case 'g':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000131 options->flags |= GPIO_OPTION;
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200132 options->selectedwindow = GPIO;
133 break;
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000134 case 'd':
135 options->flags |= DUMP_OPTION;
136 break;
137 case 'v':
138 options->flags |= VERBOSE_OPTION;
139 break;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530140 case 'p':
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100141 options->clkname = strdup(optarg);
142 if (!options->clkname) {
Daniel Lezcano9420fde2011-03-23 14:37:31 +0100143 fprintf(stderr, "failed to allocate memory");
144 return -1;
145 }
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000146 options->flags |= (DUMP_OPTION | CLOCK_OPTION);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530147 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530148 case 't':
Daniel Lezcano7f112da2011-03-23 14:37:32 +0100149 options->ticktime = atoi(optarg);
Amit Arora6e774cd2010-10-28 11:31:24 +0530150 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530151 case 'V':
152 version();
153 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530154 case '?':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100155 fprintf(stderr, "%s: Unknown option %c'.\n",
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600156 argv[0], optopt);
Amit Arora6e774cd2010-10-28 11:31:24 +0530157 default:
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100158 return -1;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530159 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530160 }
Amit Aroraa06a7302010-12-02 15:59:37 +0530161
Daniel Lezcano934fc092011-03-26 22:06:18 +0100162 /* No system specified to be dump, let's default to all */
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000163 if (!(options->flags & DEFAULT_OPTION))
164 options->flags |= DEFAULT_OPTION;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530165
Daniel Lezcanoc9c14622011-03-26 22:06:10 +0100166 if (options->selectedwindow == -1)
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100167 options->selectedwindow = CLOCK;
Amit Arorae9e16b02010-08-03 10:15:20 +0530168
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100169 return 0;
170}
171
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200172static int powerdebug_dump(struct powerdebug_options *options)
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100173{
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000174 if (options->flags & REGULATOR_OPTION)
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200175 regulator_dump();
Daniel Lezcanob5746712011-03-26 22:06:05 +0100176
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000177 if (options->flags & CLOCK_OPTION)
Daniel Lezcano597892a2011-06-15 15:45:12 +0200178 clock_dump(options->clkname);
Daniel Lezcanob5746712011-03-26 22:06:05 +0100179
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000180 if (options->flags & SENSOR_OPTION)
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200181 sensor_dump();
Daniel Lezcanob5746712011-03-26 22:06:05 +0100182
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000183 if (options->flags & GPIO_OPTION)
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200184 gpio_dump();
185
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100186 return 0;
187}
188
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200189static int powerdebug_display(struct powerdebug_options *options)
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100190{
Daniel Lezcano7b3da502011-06-15 15:45:12 +0200191 if (display_init(options->selectedwindow)) {
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100192 printf("failed to initialize display\n");
193 return -1;
194 }
195
Daniel Lezcanodb145802011-06-21 00:57:08 +0200196 if (mainloop(options->ticktime * 1000))
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100197 return -1;
198
199 return 0;
200}
201
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100202static struct powerdebug_options *powerdebug_init(void)
203{
204 struct powerdebug_options *options;
205
Daniel Lezcano188ff0f2016-02-18 15:40:12 +0000206 signal(SIGWINCH, sigwinch_handler);
207
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100208 options = malloc(sizeof(*options));
209 if (!options)
210 return NULL;
211
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100212 return options;
213}
214
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100215int main(int argc, char **argv)
216{
217 struct powerdebug_options *options;
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200218 int ret;
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100219
Sanjay Singh Rawat9fe0c052013-02-22 17:57:18 +0530220#ifdef __ANDROID__
221 if (setenv("TERM", "xterm", 1) < 0) {
222 fprintf(stderr, "setenv failure");
223 return 1;
224 }
225 if (setenv("TERMINFO", "/system/etc/terminfo", 1) < 0) {
226 fprintf(stderr, "setenv failure");
227 return 1;
228 }
229#endif
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100230 options = powerdebug_init();
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100231 if (!options) {
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100232 fprintf(stderr, "not enough memory to allocate options\n");
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100233 return 1;
234 }
235
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100236 if (getoptions(argc, argv, options)) {
237 usage();
238 return 1;
239 }
240
Daniel Lezcanodb145802011-06-21 00:57:08 +0200241 if (mainloop_init()) {
242 fprintf(stderr, "failed to initialize the mainloop\n");
243 return 1;
244 }
245
Daniel Lezcano188ff0f2016-02-18 15:40:12 +0000246 if ((options->flags & REGULATOR_OPTION) && regulator_init()) {
Sanjay Singh Rawat5fef0052013-04-17 15:02:01 +0530247 printf("failed to initialize regulator\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000248 options->flags &= ~REGULATOR_OPTION;
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +0100249 }
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100250
Daniel Lezcano188ff0f2016-02-18 15:40:12 +0000251 if ((options->flags & CLOCK_OPTION) && clock_init()) {
Daniel Lezcanof0e06652011-03-26 22:06:19 +0100252 printf("failed to initialize clock details (check debugfs)\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000253 options->flags &= ~CLOCK_OPTION;
Daniel Lezcanof0e06652011-03-26 22:06:19 +0100254 }
255
Daniel Lezcano188ff0f2016-02-18 15:40:12 +0000256 if ((options->flags & SENSOR_OPTION) && sensor_init()) {
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200257 printf("failed to initialize sensors\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000258 options->flags &= SENSOR_OPTION;
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200259 }
260
Daniel Lezcano188ff0f2016-02-18 15:40:12 +0000261 if ((options->flags & GPIO_OPTION) && gpio_init()) {
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200262 printf("failed to initialize gpios\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000263 options->flags &= GPIO_OPTION;
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200264 }
265
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000266 ret = options->flags & DUMP_OPTION ? powerdebug_dump(options) :
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200267 powerdebug_display(options);
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100268
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100269 return ret < 0;
Amit Arorae9e16b02010-08-03 10:15:20 +0530270}