blob: 164126d07ce451571a221bbe385f929c9b4fb636 [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> ] ] "
38 "[ -v ]\n");
39 printf("powerdebug [ -r | -s | -c ]\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");
Amit Arora17552782010-12-02 12:23:14 +053045 printf(" -t, --time Set ticktime in seconds (eg. 10.0)\n");
46 printf(" -d, --dump Dump information once (no refresh)\n");
Amit Arora422c52f2010-12-02 16:22:29 +053047 printf(" -v, --verbose Verbose mode (use with -r and/or"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060048 " -s)\n");
Amit Arora17552782010-12-02 12:23:14 +053049 printf(" -V, --version Show Version\n");
50 printf(" -h, --help Help\n");
Amit Arorae9e16b02010-08-03 10:15:20 +053051}
52
Amit Arora17552782010-12-02 12:23:14 +053053void version()
Amit Arorae9e16b02010-08-03 10:15:20 +053054{
Amit Arora17552782010-12-02 12:23:14 +053055 printf("powerdebug version %s\n", VERSION);
Amit Arorae9e16b02010-08-03 10:15:20 +053056}
57
Daniel Lezcano316bcae2011-03-23 14:37:30 +010058/*
59 * Options:
Daniel Lezcano716b23e2011-08-25 15:46:13 +020060 * -r, --regulator : regulators
Daniel Lezcano316bcae2011-03-23 14:37:30 +010061 * -s, --sensor : sensors
62 * -c, --clock : clocks
Daniel Lezcano716b23e2011-08-25 15:46:13 +020063 * -g, --gpio : gpios
Daniel Lezcano316bcae2011-03-23 14:37:30 +010064 * -p, --findparents : clockname whose parents have to be found
65 * -t, --time : ticktime
66 * -d, --dump : dump
67 * -v, --verbose : verbose
68 * -V, --version : version
69 * -h, --help : help
70 * no option / default : show usage!
71 */
72
73static struct option long_options[] = {
74 { "regulator", 0, 0, 'r' },
75 { "sensor", 0, 0, 's' },
76 { "clock", 0, 0, 'c' },
Daniel Lezcano716b23e2011-08-25 15:46:13 +020077 { "gpio", 0, 0, 'g' },
Daniel Lezcano316bcae2011-03-23 14:37:30 +010078 { "findparents", 1, 0, 'p' },
79 { "time", 1, 0, 't' },
80 { "dump", 0, 0, 'd' },
81 { "verbose", 0, 0, 'v' },
82 { "version", 0, 0, 'V' },
83 { "help", 0, 0, 'h' },
84 { 0, 0, 0, 0 }
85};
86
Daniel Lezcano316bcae2011-03-23 14:37:30 +010087int getoptions(int argc, char *argv[], struct powerdebug_options *options)
Amit Arorae9e16b02010-08-03 10:15:20 +053088{
Daniel Lezcano316bcae2011-03-23 14:37:30 +010089 int c;
Amit Arorae9e16b02010-08-03 10:15:20 +053090
Daniel Lezcano316bcae2011-03-23 14:37:30 +010091 memset(options, 0, sizeof(*options));
Daniel Lezcano88b37732016-02-19 21:20:26 +000092 options->ticktime = 1;
Amit Arorae9e16b02010-08-03 10:15:20 +053093
Amit Arorafefe8bf2010-08-05 13:31:20 +053094 while (1) {
95 int optindex = 0;
Amit Arorafefe8bf2010-08-05 13:31:20 +053096
Daniel Lezcano716b23e2011-08-25 15:46:13 +020097 c = getopt_long(argc, argv, "rscgp:t:dvVh",
Daniel Lezcano316bcae2011-03-23 14:37:30 +010098 long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +053099 if (c == -1)
100 break;
101
102 switch (c) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530103 case 'r':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000104 options->flags |= REGULATOR_OPTION;
Amit Arora6e774cd2010-10-28 11:31:24 +0530105 break;
106 case 's':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000107 options->flags |= SENSOR_OPTION;
Amit Arora6e774cd2010-10-28 11:31:24 +0530108 break;
109 case 'c':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000110 options->flags |= CLOCK_OPTION;
Amit Arora6e774cd2010-10-28 11:31:24 +0530111 break;
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200112 case 'g':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000113 options->flags |= GPIO_OPTION;
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200114 break;
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000115 case 'd':
116 options->flags |= DUMP_OPTION;
117 break;
118 case 'v':
119 options->flags |= VERBOSE_OPTION;
120 break;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530121 case 'p':
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100122 options->clkname = strdup(optarg);
123 if (!options->clkname) {
Daniel Lezcano9420fde2011-03-23 14:37:31 +0100124 fprintf(stderr, "failed to allocate memory");
125 return -1;
126 }
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000127 options->flags |= (DUMP_OPTION | CLOCK_OPTION);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530128 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530129 case 't':
Daniel Lezcano7f112da2011-03-23 14:37:32 +0100130 options->ticktime = atoi(optarg);
Amit Arora6e774cd2010-10-28 11:31:24 +0530131 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530132 case 'V':
133 version();
134 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530135 case '?':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100136 fprintf(stderr, "%s: Unknown option %c'.\n",
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600137 argv[0], optopt);
Amit Arora6e774cd2010-10-28 11:31:24 +0530138 default:
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100139 return -1;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530140 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530141 }
Amit Aroraa06a7302010-12-02 15:59:37 +0530142
Daniel Lezcano934fc092011-03-26 22:06:18 +0100143 /* No system specified to be dump, let's default to all */
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000144 if (!(options->flags & DEFAULT_OPTION))
145 options->flags |= DEFAULT_OPTION;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530146
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100147 return 0;
148}
149
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200150static int powerdebug_dump(struct powerdebug_options *options)
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100151{
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000152 if (options->flags & REGULATOR_OPTION)
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200153 regulator_dump();
Daniel Lezcanob5746712011-03-26 22:06:05 +0100154
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000155 if (options->flags & CLOCK_OPTION)
Daniel Lezcano597892a2011-06-15 15:45:12 +0200156 clock_dump(options->clkname);
Daniel Lezcanob5746712011-03-26 22:06:05 +0100157
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000158 if (options->flags & SENSOR_OPTION)
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200159 sensor_dump();
Daniel Lezcanob5746712011-03-26 22:06:05 +0100160
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000161 if (options->flags & GPIO_OPTION)
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200162 gpio_dump();
163
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100164 return 0;
165}
166
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200167static int powerdebug_display(struct powerdebug_options *options)
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100168{
Daniel Lezcano5e659d72016-02-19 20:22:12 +0000169 if (display_init(options)) {
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100170 printf("failed to initialize display\n");
171 return -1;
172 }
173
Daniel Lezcanodb145802011-06-21 00:57:08 +0200174 if (mainloop(options->ticktime * 1000))
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100175 return -1;
176
177 return 0;
178}
179
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100180static struct powerdebug_options *powerdebug_init(void)
181{
182 struct powerdebug_options *options;
183
184 options = malloc(sizeof(*options));
185 if (!options)
186 return NULL;
187
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100188 return options;
189}
190
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100191int main(int argc, char **argv)
192{
193 struct powerdebug_options *options;
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200194 int ret;
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100195
Sanjay Singh Rawat9fe0c052013-02-22 17:57:18 +0530196#ifdef __ANDROID__
197 if (setenv("TERM", "xterm", 1) < 0) {
198 fprintf(stderr, "setenv failure");
199 return 1;
200 }
201 if (setenv("TERMINFO", "/system/etc/terminfo", 1) < 0) {
202 fprintf(stderr, "setenv failure");
203 return 1;
204 }
205#endif
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100206 options = powerdebug_init();
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100207 if (!options) {
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100208 fprintf(stderr, "not enough memory to allocate options\n");
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100209 return 1;
210 }
211
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100212 if (getoptions(argc, argv, options)) {
213 usage();
214 return 1;
215 }
216
Daniel Lezcanodb145802011-06-21 00:57:08 +0200217 if (mainloop_init()) {
218 fprintf(stderr, "failed to initialize the mainloop\n");
219 return 1;
220 }
221
Daniel Lezcanob4eec7e2016-02-18 16:44:55 +0000222 if (regulator_init(options)) {
Sanjay Singh Rawat5fef0052013-04-17 15:02:01 +0530223 printf("failed to initialize regulator\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000224 options->flags &= ~REGULATOR_OPTION;
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +0100225 }
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100226
Daniel Lezcanob4eec7e2016-02-18 16:44:55 +0000227 if (clock_init(options)) {
Daniel Lezcanof0e06652011-03-26 22:06:19 +0100228 printf("failed to initialize clock details (check debugfs)\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000229 options->flags &= ~CLOCK_OPTION;
Daniel Lezcanof0e06652011-03-26 22:06:19 +0100230 }
231
Daniel Lezcanob4eec7e2016-02-18 16:44:55 +0000232 if (sensor_init(options)) {
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200233 printf("failed to initialize sensors\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000234 options->flags &= SENSOR_OPTION;
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200235 }
236
Daniel Lezcanob4eec7e2016-02-18 16:44:55 +0000237 if (gpio_init(options)) {
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200238 printf("failed to initialize gpios\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000239 options->flags &= GPIO_OPTION;
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200240 }
241
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000242 ret = options->flags & DUMP_OPTION ? powerdebug_dump(options) :
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200243 powerdebug_display(options);
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100244
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100245 return ret < 0;
Amit Arorae9e16b02010-08-03 10:15:20 +0530246}