blob: 356a8dbbf11182f3dbf360309b9058567ce72f23 [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));
Daniel Lezcano88b37732016-02-19 21:20:26 +000095 options->ticktime = 1;
Amit Arorae9e16b02010-08-03 10:15:20 +053096
Amit Arorafefe8bf2010-08-05 13:31:20 +053097 while (1) {
98 int optindex = 0;
Amit Arorafefe8bf2010-08-05 13:31:20 +053099
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200100 c = getopt_long(argc, argv, "rscgp:t:dvVh",
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100101 long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +0530102 if (c == -1)
103 break;
104
105 switch (c) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530106 case 'r':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000107 options->flags |= REGULATOR_OPTION;
Amit Arora6e774cd2010-10-28 11:31:24 +0530108 break;
109 case 's':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000110 options->flags |= SENSOR_OPTION;
Amit Arora6e774cd2010-10-28 11:31:24 +0530111 break;
112 case 'c':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000113 options->flags |= CLOCK_OPTION;
Amit Arora6e774cd2010-10-28 11:31:24 +0530114 break;
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200115 case 'g':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000116 options->flags |= GPIO_OPTION;
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200117 break;
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000118 case 'd':
119 options->flags |= DUMP_OPTION;
120 break;
121 case 'v':
122 options->flags |= VERBOSE_OPTION;
123 break;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530124 case 'p':
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100125 options->clkname = strdup(optarg);
126 if (!options->clkname) {
Daniel Lezcano9420fde2011-03-23 14:37:31 +0100127 fprintf(stderr, "failed to allocate memory");
128 return -1;
129 }
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000130 options->flags |= (DUMP_OPTION | CLOCK_OPTION);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530131 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530132 case 't':
Daniel Lezcano7f112da2011-03-23 14:37:32 +0100133 options->ticktime = atoi(optarg);
Amit Arora6e774cd2010-10-28 11:31:24 +0530134 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530135 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 */
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000147 if (!(options->flags & DEFAULT_OPTION))
148 options->flags |= DEFAULT_OPTION;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530149
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100150 return 0;
151}
152
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200153static int powerdebug_dump(struct powerdebug_options *options)
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100154{
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000155 if (options->flags & REGULATOR_OPTION)
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200156 regulator_dump();
Daniel Lezcanob5746712011-03-26 22:06:05 +0100157
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000158 if (options->flags & CLOCK_OPTION)
Daniel Lezcano597892a2011-06-15 15:45:12 +0200159 clock_dump(options->clkname);
Daniel Lezcanob5746712011-03-26 22:06:05 +0100160
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000161 if (options->flags & SENSOR_OPTION)
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200162 sensor_dump();
Daniel Lezcanob5746712011-03-26 22:06:05 +0100163
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000164 if (options->flags & GPIO_OPTION)
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200165 gpio_dump();
166
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100167 return 0;
168}
169
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200170static int powerdebug_display(struct powerdebug_options *options)
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100171{
Daniel Lezcano5e659d72016-02-19 20:22:12 +0000172 if (display_init(options)) {
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100173 printf("failed to initialize display\n");
174 return -1;
175 }
176
Daniel Lezcanodb145802011-06-21 00:57:08 +0200177 if (mainloop(options->ticktime * 1000))
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100178 return -1;
179
180 return 0;
181}
182
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100183static struct powerdebug_options *powerdebug_init(void)
184{
185 struct powerdebug_options *options;
186
Daniel Lezcano188ff0f2016-02-18 15:40:12 +0000187 signal(SIGWINCH, sigwinch_handler);
188
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100189 options = malloc(sizeof(*options));
190 if (!options)
191 return NULL;
192
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100193 return options;
194}
195
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100196int main(int argc, char **argv)
197{
198 struct powerdebug_options *options;
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200199 int ret;
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100200
Sanjay Singh Rawat9fe0c052013-02-22 17:57:18 +0530201#ifdef __ANDROID__
202 if (setenv("TERM", "xterm", 1) < 0) {
203 fprintf(stderr, "setenv failure");
204 return 1;
205 }
206 if (setenv("TERMINFO", "/system/etc/terminfo", 1) < 0) {
207 fprintf(stderr, "setenv failure");
208 return 1;
209 }
210#endif
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100211 options = powerdebug_init();
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100212 if (!options) {
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100213 fprintf(stderr, "not enough memory to allocate options\n");
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100214 return 1;
215 }
216
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100217 if (getoptions(argc, argv, options)) {
218 usage();
219 return 1;
220 }
221
Daniel Lezcanodb145802011-06-21 00:57:08 +0200222 if (mainloop_init()) {
223 fprintf(stderr, "failed to initialize the mainloop\n");
224 return 1;
225 }
226
Daniel Lezcanob4eec7e2016-02-18 16:44:55 +0000227 if (regulator_init(options)) {
Sanjay Singh Rawat5fef0052013-04-17 15:02:01 +0530228 printf("failed to initialize regulator\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000229 options->flags &= ~REGULATOR_OPTION;
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +0100230 }
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100231
Daniel Lezcanob4eec7e2016-02-18 16:44:55 +0000232 if (clock_init(options)) {
Daniel Lezcanof0e06652011-03-26 22:06:19 +0100233 printf("failed to initialize clock details (check debugfs)\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000234 options->flags &= ~CLOCK_OPTION;
Daniel Lezcanof0e06652011-03-26 22:06:19 +0100235 }
236
Daniel Lezcanob4eec7e2016-02-18 16:44:55 +0000237 if (sensor_init(options)) {
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200238 printf("failed to initialize sensors\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000239 options->flags &= SENSOR_OPTION;
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200240 }
241
Daniel Lezcanob4eec7e2016-02-18 16:44:55 +0000242 if (gpio_init(options)) {
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200243 printf("failed to initialize gpios\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000244 options->flags &= GPIO_OPTION;
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200245 }
246
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000247 ret = options->flags & DUMP_OPTION ? powerdebug_dump(options) :
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200248 powerdebug_display(options);
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100249
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100250 return ret < 0;
Amit Arorae9e16b02010-08-03 10:15:20 +0530251}