blob: dc2c9bf5c28a10801a3bde3c7252f91236c170f0 [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>
Daniel Lezcano192c1d22011-03-26 22:06:14 +010029#include "display.h"
Daniel Lezcanodb145802011-06-21 00:57:08 +020030#include "mainloop.h"
Amit Arorae9e16b02010-08-03 10:15:20 +053031#include "powerdebug.h"
32
Amit Arora422c52f2010-12-02 16:22:29 +053033void usage(void)
Amit Arorae9e16b02010-08-03 10:15:20 +053034{
Amit Arora422c52f2010-12-02 16:22:29 +053035 printf("Usage: powerdebug [OPTIONS]\n");
36 printf("\n");
37 printf("powerdebug -d [ -r ] [ -s ] [ -c [ -p <clock-name> ] ] "
Thara Gopinath7dd9f3c2017-07-14 13:15:49 -040038 "[ -g ] [ -v ]\n");
39 printf("powerdebug [ -r | -s | -c | -g]\n");
Amit Arora17552782010-12-02 12:23:14 +053040 printf(" -r, --regulator Show regulator information\n");
41 printf(" -s, --sensor Show sensor information\n");
42 printf(" -c, --clock Show clock information\n");
Amit Arora422c52f2010-12-02 16:22:29 +053043 printf(" -p, --findparents Show all parents for a particular"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060044 " clock\n");
Thara Gopinath7dd9f3c2017-07-14 13:15:49 -040045 printf(" -g, --gpio Show gpio information\n");
Amit Arora17552782010-12-02 12:23:14 +053046 printf(" -t, --time Set ticktime in seconds (eg. 10.0)\n");
47 printf(" -d, --dump Dump information once (no refresh)\n");
Amit Arora422c52f2010-12-02 16:22:29 +053048 printf(" -v, --verbose Verbose mode (use with -r and/or"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060049 " -s)\n");
Amit Arora17552782010-12-02 12:23:14 +053050 printf(" -V, --version Show Version\n");
51 printf(" -h, --help Help\n");
Amit Arorae9e16b02010-08-03 10:15:20 +053052}
53
Amit Arora17552782010-12-02 12:23:14 +053054void version()
Amit Arorae9e16b02010-08-03 10:15:20 +053055{
Amit Arora17552782010-12-02 12:23:14 +053056 printf("powerdebug version %s\n", VERSION);
Amit Arorae9e16b02010-08-03 10:15:20 +053057}
58
Daniel Lezcano316bcae2011-03-23 14:37:30 +010059/*
60 * Options:
Daniel Lezcano716b23e2011-08-25 15:46:13 +020061 * -r, --regulator : regulators
Daniel Lezcano316bcae2011-03-23 14:37:30 +010062 * -s, --sensor : sensors
63 * -c, --clock : clocks
Daniel Lezcano716b23e2011-08-25 15:46:13 +020064 * -g, --gpio : gpios
Daniel Lezcano316bcae2011-03-23 14:37:30 +010065 * -p, --findparents : clockname whose parents have to be found
66 * -t, --time : ticktime
67 * -d, --dump : dump
68 * -v, --verbose : verbose
69 * -V, --version : version
70 * -h, --help : help
71 * no option / default : show usage!
72 */
73
74static struct option long_options[] = {
75 { "regulator", 0, 0, 'r' },
76 { "sensor", 0, 0, 's' },
77 { "clock", 0, 0, 'c' },
Daniel Lezcano716b23e2011-08-25 15:46:13 +020078 { "gpio", 0, 0, 'g' },
Daniel Lezcano316bcae2011-03-23 14:37:30 +010079 { "findparents", 1, 0, 'p' },
80 { "time", 1, 0, 't' },
81 { "dump", 0, 0, 'd' },
82 { "verbose", 0, 0, 'v' },
83 { "version", 0, 0, 'V' },
84 { "help", 0, 0, 'h' },
85 { 0, 0, 0, 0 }
86};
87
Daniel Lezcano316bcae2011-03-23 14:37:30 +010088int 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));
Daniel Lezcano88b37732016-02-19 21:20:26 +000093 options->ticktime = 1;
Amit Arorae9e16b02010-08-03 10:15:20 +053094
Amit Arorafefe8bf2010-08-05 13:31:20 +053095 while (1) {
96 int optindex = 0;
Amit Arorafefe8bf2010-08-05 13:31:20 +053097
Daniel Lezcano716b23e2011-08-25 15:46:13 +020098 c = getopt_long(argc, argv, "rscgp:t:dvVh",
Daniel Lezcano316bcae2011-03-23 14:37:30 +010099 long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +0530100 if (c == -1)
101 break;
102
103 switch (c) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530104 case 'r':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000105 options->flags |= REGULATOR_OPTION;
Amit Arora6e774cd2010-10-28 11:31:24 +0530106 break;
107 case 's':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000108 options->flags |= SENSOR_OPTION;
Amit Arora6e774cd2010-10-28 11:31:24 +0530109 break;
110 case 'c':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000111 options->flags |= CLOCK_OPTION;
Amit Arora6e774cd2010-10-28 11:31:24 +0530112 break;
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200113 case 'g':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000114 options->flags |= GPIO_OPTION;
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200115 break;
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000116 case 'd':
117 options->flags |= DUMP_OPTION;
118 break;
119 case 'v':
120 options->flags |= VERBOSE_OPTION;
121 break;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530122 case 'p':
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100123 options->clkname = strdup(optarg);
124 if (!options->clkname) {
Daniel Lezcano9420fde2011-03-23 14:37:31 +0100125 fprintf(stderr, "failed to allocate memory");
126 return -1;
127 }
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000128 options->flags |= (DUMP_OPTION | CLOCK_OPTION);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530129 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530130 case 't':
Daniel Lezcano7f112da2011-03-23 14:37:32 +0100131 options->ticktime = atoi(optarg);
Amit Arora6e774cd2010-10-28 11:31:24 +0530132 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530133 case 'V':
134 version();
135 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530136 case '?':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100137 fprintf(stderr, "%s: Unknown option %c'.\n",
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600138 argv[0], optopt);
Amit Arora6e774cd2010-10-28 11:31:24 +0530139 default:
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100140 return -1;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530141 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530142 }
Amit Aroraa06a7302010-12-02 15:59:37 +0530143
Daniel Lezcano934fc092011-03-26 22:06:18 +0100144 /* No system specified to be dump, let's default to all */
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000145 if (!(options->flags & DEFAULT_OPTION))
146 options->flags |= DEFAULT_OPTION;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530147
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100148 return 0;
149}
150
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200151static int powerdebug_dump(struct powerdebug_options *options)
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100152{
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000153 if (options->flags & REGULATOR_OPTION)
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200154 regulator_dump();
Daniel Lezcanob5746712011-03-26 22:06:05 +0100155
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000156 if (options->flags & CLOCK_OPTION)
Daniel Lezcano597892a2011-06-15 15:45:12 +0200157 clock_dump(options->clkname);
Daniel Lezcanob5746712011-03-26 22:06:05 +0100158
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000159 if (options->flags & SENSOR_OPTION)
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200160 sensor_dump();
Daniel Lezcanob5746712011-03-26 22:06:05 +0100161
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000162 if (options->flags & GPIO_OPTION)
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200163 gpio_dump();
164
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100165 return 0;
166}
167
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200168static int powerdebug_display(struct powerdebug_options *options)
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100169{
Daniel Lezcano5e659d72016-02-19 20:22:12 +0000170 if (display_init(options)) {
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100171 printf("failed to initialize display\n");
172 return -1;
173 }
174
Daniel Lezcanodb145802011-06-21 00:57:08 +0200175 if (mainloop(options->ticktime * 1000))
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100176 return -1;
177
178 return 0;
179}
180
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100181static struct powerdebug_options *powerdebug_init(void)
182{
183 struct powerdebug_options *options;
184
185 options = malloc(sizeof(*options));
186 if (!options)
187 return NULL;
188
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100189 return options;
190}
191
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100192int main(int argc, char **argv)
193{
194 struct powerdebug_options *options;
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200195 int ret;
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100196
Sanjay Singh Rawat9fe0c052013-02-22 17:57:18 +0530197#ifdef __ANDROID__
198 if (setenv("TERM", "xterm", 1) < 0) {
199 fprintf(stderr, "setenv failure");
200 return 1;
201 }
202 if (setenv("TERMINFO", "/system/etc/terminfo", 1) < 0) {
203 fprintf(stderr, "setenv failure");
204 return 1;
205 }
206#endif
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100207 options = powerdebug_init();
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100208 if (!options) {
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100209 fprintf(stderr, "not enough memory to allocate options\n");
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100210 return 1;
211 }
212
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100213 if (getoptions(argc, argv, options)) {
214 usage();
215 return 1;
216 }
217
Daniel Lezcanodb145802011-06-21 00:57:08 +0200218 if (mainloop_init()) {
219 fprintf(stderr, "failed to initialize the mainloop\n");
220 return 1;
221 }
222
Daniel Lezcanob4eec7e2016-02-18 16:44:55 +0000223 if (regulator_init(options)) {
Sanjay Singh Rawat5fef0052013-04-17 15:02:01 +0530224 printf("failed to initialize regulator\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000225 options->flags &= ~REGULATOR_OPTION;
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +0100226 }
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100227
Daniel Lezcanob4eec7e2016-02-18 16:44:55 +0000228 if (clock_init(options)) {
Daniel Lezcanof0e06652011-03-26 22:06:19 +0100229 printf("failed to initialize clock details (check debugfs)\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000230 options->flags &= ~CLOCK_OPTION;
Daniel Lezcanof0e06652011-03-26 22:06:19 +0100231 }
232
Daniel Lezcanob4eec7e2016-02-18 16:44:55 +0000233 if (sensor_init(options)) {
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200234 printf("failed to initialize sensors\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000235 options->flags &= SENSOR_OPTION;
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200236 }
237
Daniel Lezcanob4eec7e2016-02-18 16:44:55 +0000238 if (gpio_init(options)) {
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200239 printf("failed to initialize gpios\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000240 options->flags &= GPIO_OPTION;
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200241 }
242
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000243 ret = options->flags & DUMP_OPTION ? powerdebug_dump(options) :
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200244 powerdebug_display(options);
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100245
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100246 return ret < 0;
Amit Arorae9e16b02010-08-03 10:15:20 +0530247}