blob: e18670e617aa0960e926c828a7a52fcc2444ac97 [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");
Thara Gopinathe48aec72017-07-14 13:15:50 -040037 printf("powerdebug -d [ -r ] [ -s ] [ -c [ -f <clock-name> ] ] "
Thara Gopinath9cee8a52017-07-14 13:25:18 -040038 "[ -g ] [ -p ] [ -v ]\n");
39 printf("powerdebug [ -r | -s | -c | -g | -p]\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");
Thara Gopinathe48aec72017-07-14 13:15:50 -040043 printf(" -f, --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");
Thara Gopinath9cee8a52017-07-14 13:25:18 -040046 printf(" -p, --powerdomain Show powerdomain information\n");
Amit Arora17552782010-12-02 12:23:14 +053047 printf(" -t, --time Set ticktime in seconds (eg. 10.0)\n");
48 printf(" -d, --dump Dump information once (no refresh)\n");
Amit Arora422c52f2010-12-02 16:22:29 +053049 printf(" -v, --verbose Verbose mode (use with -r and/or"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060050 " -s)\n");
Amit Arora17552782010-12-02 12:23:14 +053051 printf(" -V, --version Show Version\n");
52 printf(" -h, --help Help\n");
Amit Arorae9e16b02010-08-03 10:15:20 +053053}
54
Amit Arora17552782010-12-02 12:23:14 +053055void version()
Amit Arorae9e16b02010-08-03 10:15:20 +053056{
Amit Arora17552782010-12-02 12:23:14 +053057 printf("powerdebug version %s\n", VERSION);
Amit Arorae9e16b02010-08-03 10:15:20 +053058}
59
Daniel Lezcano316bcae2011-03-23 14:37:30 +010060/*
61 * Options:
Daniel Lezcano716b23e2011-08-25 15:46:13 +020062 * -r, --regulator : regulators
Daniel Lezcano316bcae2011-03-23 14:37:30 +010063 * -s, --sensor : sensors
64 * -c, --clock : clocks
Daniel Lezcano716b23e2011-08-25 15:46:13 +020065 * -g, --gpio : gpios
Thara Gopinath9cee8a52017-07-14 13:25:18 -040066 * -p, --powerdomain : powerdomains
Thara Gopinathe48aec72017-07-14 13:15:50 -040067 * -f, --findparents : clockname whose parents have to be found
Daniel Lezcano316bcae2011-03-23 14:37:30 +010068 * -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' },
Thara Gopinath9cee8a52017-07-14 13:25:18 -040081 { "powerdomain", 0, 0, 'p' },
Thara Gopinathe48aec72017-07-14 13:15:50 -040082 { "findparents", 1, 0, 'f' },
Daniel Lezcano316bcae2011-03-23 14:37:30 +010083 { "time", 1, 0, 't' },
84 { "dump", 0, 0, 'd' },
85 { "verbose", 0, 0, 'v' },
86 { "version", 0, 0, 'V' },
87 { "help", 0, 0, 'h' },
88 { 0, 0, 0, 0 }
89};
90
Daniel Lezcano316bcae2011-03-23 14:37:30 +010091int getoptions(int argc, char *argv[], struct powerdebug_options *options)
Amit Arorae9e16b02010-08-03 10:15:20 +053092{
Daniel Lezcano316bcae2011-03-23 14:37:30 +010093 int c;
Amit Arorae9e16b02010-08-03 10:15:20 +053094
Daniel Lezcano316bcae2011-03-23 14:37:30 +010095 memset(options, 0, sizeof(*options));
Daniel Lezcano88b37732016-02-19 21:20:26 +000096 options->ticktime = 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
Thara Gopinath9cee8a52017-07-14 13:25:18 -0400101 c = getopt_long(argc, argv, "rscgpf: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;
Amit Arora6e774cd2010-10-28 11:31:24 +0530109 break;
110 case 's':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000111 options->flags |= SENSOR_OPTION;
Amit Arora6e774cd2010-10-28 11:31:24 +0530112 break;
113 case 'c':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000114 options->flags |= CLOCK_OPTION;
Amit Arora6e774cd2010-10-28 11:31:24 +0530115 break;
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200116 case 'g':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000117 options->flags |= GPIO_OPTION;
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200118 break;
Thara Gopinath9cee8a52017-07-14 13:25:18 -0400119 case 'p':
120 options->flags |= GENPD_OPTION;
121 break;
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000122 case 'd':
123 options->flags |= DUMP_OPTION;
124 break;
125 case 'v':
126 options->flags |= VERBOSE_OPTION;
127 break;
Thara Gopinathe48aec72017-07-14 13:15:50 -0400128 case 'f':
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100129 options->clkname = strdup(optarg);
130 if (!options->clkname) {
Daniel Lezcano9420fde2011-03-23 14:37:31 +0100131 fprintf(stderr, "failed to allocate memory");
132 return -1;
133 }
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000134 options->flags |= (DUMP_OPTION | CLOCK_OPTION);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530135 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530136 case 't':
Daniel Lezcano7f112da2011-03-23 14:37:32 +0100137 options->ticktime = atoi(optarg);
Amit Arora6e774cd2010-10-28 11:31:24 +0530138 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530139 case 'V':
140 version();
141 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530142 case '?':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100143 fprintf(stderr, "%s: Unknown option %c'.\n",
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600144 argv[0], optopt);
Amit Arora6e774cd2010-10-28 11:31:24 +0530145 default:
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100146 return -1;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530147 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530148 }
Amit Aroraa06a7302010-12-02 15:59:37 +0530149
Daniel Lezcano934fc092011-03-26 22:06:18 +0100150 /* No system specified to be dump, let's default to all */
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000151 if (!(options->flags & DEFAULT_OPTION))
152 options->flags |= DEFAULT_OPTION;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530153
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100154 return 0;
155}
156
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200157static int powerdebug_dump(struct powerdebug_options *options)
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100158{
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000159 if (options->flags & REGULATOR_OPTION)
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200160 regulator_dump();
Daniel Lezcanob5746712011-03-26 22:06:05 +0100161
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000162 if (options->flags & CLOCK_OPTION)
Daniel Lezcano597892a2011-06-15 15:45:12 +0200163 clock_dump(options->clkname);
Daniel Lezcanob5746712011-03-26 22:06:05 +0100164
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000165 if (options->flags & SENSOR_OPTION)
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200166 sensor_dump();
Daniel Lezcanob5746712011-03-26 22:06:05 +0100167
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000168 if (options->flags & GPIO_OPTION)
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200169 gpio_dump();
170
Thara Gopinath9cee8a52017-07-14 13:25:18 -0400171 if (options->flags & GENPD_OPTION)
172 genpd_dump();
173
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100174 return 0;
175}
176
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200177static int powerdebug_display(struct powerdebug_options *options)
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100178{
Daniel Lezcano5e659d72016-02-19 20:22:12 +0000179 if (display_init(options)) {
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100180 printf("failed to initialize display\n");
181 return -1;
182 }
183
Daniel Lezcanodb145802011-06-21 00:57:08 +0200184 if (mainloop(options->ticktime * 1000))
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100185 return -1;
186
187 return 0;
188}
189
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100190static struct powerdebug_options *powerdebug_init(void)
191{
192 struct powerdebug_options *options;
193
194 options = malloc(sizeof(*options));
195 if (!options)
196 return NULL;
197
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100198 return options;
199}
200
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100201int main(int argc, char **argv)
202{
203 struct powerdebug_options *options;
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200204 int ret;
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100205
Sanjay Singh Rawat9fe0c052013-02-22 17:57:18 +0530206#ifdef __ANDROID__
207 if (setenv("TERM", "xterm", 1) < 0) {
208 fprintf(stderr, "setenv failure");
209 return 1;
210 }
211 if (setenv("TERMINFO", "/system/etc/terminfo", 1) < 0) {
212 fprintf(stderr, "setenv failure");
213 return 1;
214 }
215#endif
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100216 options = powerdebug_init();
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100217 if (!options) {
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100218 fprintf(stderr, "not enough memory to allocate options\n");
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100219 return 1;
220 }
221
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100222 if (getoptions(argc, argv, options)) {
223 usage();
224 return 1;
225 }
226
Daniel Lezcanodb145802011-06-21 00:57:08 +0200227 if (mainloop_init()) {
228 fprintf(stderr, "failed to initialize the mainloop\n");
229 return 1;
230 }
231
Daniel Lezcanob4eec7e2016-02-18 16:44:55 +0000232 if (regulator_init(options)) {
Sanjay Singh Rawat5fef0052013-04-17 15:02:01 +0530233 printf("failed to initialize regulator\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000234 options->flags &= ~REGULATOR_OPTION;
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +0100235 }
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100236
Daniel Lezcanob4eec7e2016-02-18 16:44:55 +0000237 if (clock_init(options)) {
Daniel Lezcanof0e06652011-03-26 22:06:19 +0100238 printf("failed to initialize clock details (check debugfs)\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000239 options->flags &= ~CLOCK_OPTION;
Daniel Lezcanof0e06652011-03-26 22:06:19 +0100240 }
241
Daniel Lezcanob4eec7e2016-02-18 16:44:55 +0000242 if (sensor_init(options)) {
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200243 printf("failed to initialize sensors\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000244 options->flags &= SENSOR_OPTION;
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200245 }
246
Daniel Lezcanob4eec7e2016-02-18 16:44:55 +0000247 if (gpio_init(options)) {
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200248 printf("failed to initialize gpios\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000249 options->flags &= GPIO_OPTION;
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200250 }
251
Thara Gopinath9cee8a52017-07-14 13:25:18 -0400252 if (genpd_init(options)) {
253 printf("failed to initialize genpd details\n");
254 options->flags &= GENPD_OPTION;
255 }
256
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000257 ret = options->flags & DUMP_OPTION ? powerdebug_dump(options) :
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200258 powerdebug_display(options);
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100259
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100260 return ret < 0;
Amit Arorae9e16b02010-08-03 10:15:20 +0530261}