blob: b626b0561fe1037625c64b8672e330d9ea86f89f [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
Amit Arora422c52f2010-12-02 16:22:29 +053036void usage(void)
Amit Arorae9e16b02010-08-03 10:15:20 +053037{
Amit Arora422c52f2010-12-02 16:22:29 +053038 printf("Usage: powerdebug [OPTIONS]\n");
39 printf("\n");
40 printf("powerdebug -d [ -r ] [ -s ] [ -c [ -p <clock-name> ] ] "
41 "[ -v ]\n");
42 printf("powerdebug [ -r | -s | -c ]\n");
Amit Arora17552782010-12-02 12:23:14 +053043 printf(" -r, --regulator Show regulator information\n");
44 printf(" -s, --sensor Show sensor information\n");
45 printf(" -c, --clock Show clock information\n");
Amit Arora422c52f2010-12-02 16:22:29 +053046 printf(" -p, --findparents Show all parents for a particular"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060047 " clock\n");
Amit Arora17552782010-12-02 12:23:14 +053048 printf(" -t, --time Set ticktime in seconds (eg. 10.0)\n");
49 printf(" -d, --dump Dump information once (no refresh)\n");
Amit Arora422c52f2010-12-02 16:22:29 +053050 printf(" -v, --verbose Verbose mode (use with -r and/or"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060051 " -s)\n");
Amit Arora17552782010-12-02 12:23:14 +053052 printf(" -V, --version Show Version\n");
53 printf(" -h, --help Help\n");
Amit Arorae9e16b02010-08-03 10:15:20 +053054}
55
Amit Arora17552782010-12-02 12:23:14 +053056void version()
Amit Arorae9e16b02010-08-03 10:15:20 +053057{
Amit Arora17552782010-12-02 12:23:14 +053058 printf("powerdebug version %s\n", VERSION);
Amit Arorae9e16b02010-08-03 10:15:20 +053059}
60
Daniel Lezcano316bcae2011-03-23 14:37:30 +010061/*
62 * Options:
Daniel Lezcano716b23e2011-08-25 15:46:13 +020063 * -r, --regulator : regulators
Daniel Lezcano316bcae2011-03-23 14:37:30 +010064 * -s, --sensor : sensors
65 * -c, --clock : clocks
Daniel Lezcano716b23e2011-08-25 15:46:13 +020066 * -g, --gpio : gpios
Daniel Lezcano316bcae2011-03-23 14:37:30 +010067 * -p, --findparents : clockname whose parents have to be found
68 * -t, --time : ticktime
69 * -d, --dump : dump
70 * -v, --verbose : verbose
71 * -V, --version : version
72 * -h, --help : help
73 * no option / default : show usage!
74 */
75
76static struct option long_options[] = {
77 { "regulator", 0, 0, 'r' },
78 { "sensor", 0, 0, 's' },
79 { "clock", 0, 0, 'c' },
Daniel Lezcano716b23e2011-08-25 15:46:13 +020080 { "gpio", 0, 0, 'g' },
Daniel Lezcano316bcae2011-03-23 14:37:30 +010081 { "findparents", 1, 0, 'p' },
82 { "time", 1, 0, 't' },
83 { "dump", 0, 0, 'd' },
84 { "verbose", 0, 0, 'v' },
85 { "version", 0, 0, 'V' },
86 { "help", 0, 0, 'h' },
87 { 0, 0, 0, 0 }
88};
89
Daniel Lezcano316bcae2011-03-23 14:37:30 +010090int getoptions(int argc, char *argv[], struct powerdebug_options *options)
Amit Arorae9e16b02010-08-03 10:15:20 +053091{
Daniel Lezcano316bcae2011-03-23 14:37:30 +010092 int c;
Amit Arorae9e16b02010-08-03 10:15:20 +053093
Daniel Lezcano316bcae2011-03-23 14:37:30 +010094 memset(options, 0, sizeof(*options));
95 options->ticktime = 10;
Daniel Lezcano558a6d52011-03-23 14:37:41 +010096 options->selectedwindow = -1;
Amit Arorae9e16b02010-08-03 10:15:20 +053097
Amit Arorafefe8bf2010-08-05 13:31:20 +053098 while (1) {
99 int optindex = 0;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530100
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200101 c = getopt_long(argc, argv, "rscgp:t:dvVh",
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100102 long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +0530103 if (c == -1)
104 break;
105
106 switch (c) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530107 case 'r':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000108 options->flags |= REGULATOR_OPTION;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100109 options->selectedwindow = REGULATOR;
Amit Arora6e774cd2010-10-28 11:31:24 +0530110 break;
111 case 's':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000112 options->flags |= SENSOR_OPTION;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100113 options->selectedwindow = SENSOR;
Amit Arora6e774cd2010-10-28 11:31:24 +0530114 break;
115 case 'c':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000116 options->flags |= CLOCK_OPTION;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100117 options->selectedwindow = CLOCK;
Amit Arora6e774cd2010-10-28 11:31:24 +0530118 break;
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200119 case 'g':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000120 options->flags |= GPIO_OPTION;
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200121 options->selectedwindow = GPIO;
122 break;
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000123 case 'd':
124 options->flags |= DUMP_OPTION;
125 break;
126 case 'v':
127 options->flags |= VERBOSE_OPTION;
128 break;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530129 case 'p':
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100130 options->clkname = strdup(optarg);
131 if (!options->clkname) {
Daniel Lezcano9420fde2011-03-23 14:37:31 +0100132 fprintf(stderr, "failed to allocate memory");
133 return -1;
134 }
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000135 options->flags |= (DUMP_OPTION | CLOCK_OPTION);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530136 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530137 case 't':
Daniel Lezcano7f112da2011-03-23 14:37:32 +0100138 options->ticktime = atoi(optarg);
Amit Arora6e774cd2010-10-28 11:31:24 +0530139 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530140 case 'V':
141 version();
142 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530143 case '?':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100144 fprintf(stderr, "%s: Unknown option %c'.\n",
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600145 argv[0], optopt);
Amit Arora6e774cd2010-10-28 11:31:24 +0530146 default:
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100147 return -1;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530148 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530149 }
Amit Aroraa06a7302010-12-02 15:59:37 +0530150
Daniel Lezcano934fc092011-03-26 22:06:18 +0100151 /* No system specified to be dump, let's default to all */
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000152 if (!(options->flags & DEFAULT_OPTION))
153 options->flags |= DEFAULT_OPTION;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530154
Daniel Lezcanoc9c14622011-03-26 22:06:10 +0100155 if (options->selectedwindow == -1)
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100156 options->selectedwindow = CLOCK;
Amit Arorae9e16b02010-08-03 10:15:20 +0530157
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100158 return 0;
159}
160
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200161static int powerdebug_dump(struct powerdebug_options *options)
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100162{
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000163 if (options->flags & REGULATOR_OPTION)
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200164 regulator_dump();
Daniel Lezcanob5746712011-03-26 22:06:05 +0100165
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000166 if (options->flags & CLOCK_OPTION)
Daniel Lezcano597892a2011-06-15 15:45:12 +0200167 clock_dump(options->clkname);
Daniel Lezcanob5746712011-03-26 22:06:05 +0100168
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000169 if (options->flags & SENSOR_OPTION)
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200170 sensor_dump();
Daniel Lezcanob5746712011-03-26 22:06:05 +0100171
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000172 if (options->flags & GPIO_OPTION)
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200173 gpio_dump();
174
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100175 return 0;
176}
177
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200178static int powerdebug_display(struct powerdebug_options *options)
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100179{
Daniel Lezcano7b3da502011-06-15 15:45:12 +0200180 if (display_init(options->selectedwindow)) {
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100181 printf("failed to initialize display\n");
182 return -1;
183 }
184
Daniel Lezcanodb145802011-06-21 00:57:08 +0200185 if (mainloop(options->ticktime * 1000))
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100186 return -1;
187
188 return 0;
189}
190
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100191static struct powerdebug_options *powerdebug_init(void)
192{
193 struct powerdebug_options *options;
194
Daniel Lezcano188ff0f2016-02-18 15:40:12 +0000195 signal(SIGWINCH, sigwinch_handler);
196
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100197 options = malloc(sizeof(*options));
198 if (!options)
199 return NULL;
200
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100201 return options;
202}
203
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100204int main(int argc, char **argv)
205{
206 struct powerdebug_options *options;
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200207 int ret;
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100208
Sanjay Singh Rawat9fe0c052013-02-22 17:57:18 +0530209#ifdef __ANDROID__
210 if (setenv("TERM", "xterm", 1) < 0) {
211 fprintf(stderr, "setenv failure");
212 return 1;
213 }
214 if (setenv("TERMINFO", "/system/etc/terminfo", 1) < 0) {
215 fprintf(stderr, "setenv failure");
216 return 1;
217 }
218#endif
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100219 options = powerdebug_init();
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100220 if (!options) {
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100221 fprintf(stderr, "not enough memory to allocate options\n");
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100222 return 1;
223 }
224
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100225 if (getoptions(argc, argv, options)) {
226 usage();
227 return 1;
228 }
229
Daniel Lezcanodb145802011-06-21 00:57:08 +0200230 if (mainloop_init()) {
231 fprintf(stderr, "failed to initialize the mainloop\n");
232 return 1;
233 }
234
Daniel Lezcanob4eec7e2016-02-18 16:44:55 +0000235 if (regulator_init(options)) {
Sanjay Singh Rawat5fef0052013-04-17 15:02:01 +0530236 printf("failed to initialize regulator\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000237 options->flags &= ~REGULATOR_OPTION;
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +0100238 }
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100239
Daniel Lezcanob4eec7e2016-02-18 16:44:55 +0000240 if (clock_init(options)) {
Daniel Lezcanof0e06652011-03-26 22:06:19 +0100241 printf("failed to initialize clock details (check debugfs)\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000242 options->flags &= ~CLOCK_OPTION;
Daniel Lezcanof0e06652011-03-26 22:06:19 +0100243 }
244
Daniel Lezcanob4eec7e2016-02-18 16:44:55 +0000245 if (sensor_init(options)) {
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200246 printf("failed to initialize sensors\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000247 options->flags &= SENSOR_OPTION;
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200248 }
249
Daniel Lezcanob4eec7e2016-02-18 16:44:55 +0000250 if (gpio_init(options)) {
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200251 printf("failed to initialize gpios\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000252 options->flags &= GPIO_OPTION;
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200253 }
254
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000255 ret = options->flags & DUMP_OPTION ? powerdebug_dump(options) :
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200256 powerdebug_display(options);
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100257
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100258 return ret < 0;
Amit Arorae9e16b02010-08-03 10:15:20 +0530259}