blob: bc8fc92d831f3354bce929e65ed8cbd47f228d0f [file] [log] [blame]
Amit Arora39f29542010-09-14 12:03:22 +05301/*******************************************************************************
Amit Kucheriac0e17fc2011-01-17 09:35:52 +02002 * Copyright (C) 2010, Linaro Limited.
Amit Arora39f29542010-09-14 12:03:22 +05303 *
4 * This file is part of PowerDebug.
5 *
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of the Eclipse Public License v1.0
8 * which accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 *
11 * Contributors:
12 * Amit Arora <amit.arora@linaro.org> (IBM Corporation)
13 * - initial API and implementation
14 *******************************************************************************/
15
Amit Arorae9e16b02010-08-03 10:15:20 +053016#include <getopt.h>
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010017#include <stdbool.h>
Daniel Lezcano15627482011-06-15 15:45:12 +020018#include <string.h>
19#include <stdlib.h>
20#include <stdio.h>
21#include <errno.h>
22#include <ncurses.h>
Sanjay Singh Rawat83b37c32013-04-17 14:57:07 +053023#include <signal.h>
Daniel Lezcanocac52d92011-03-26 22:05:49 +010024#include "regulator.h"
Daniel Lezcano192c1d22011-03-26 22:06:14 +010025#include "display.h"
Daniel Lezcanof0e06652011-03-26 22:06:19 +010026#include "clocks.h"
Daniel Lezcano597892a2011-06-15 15:45:12 +020027#include "sensor.h"
Daniel Lezcano716b23e2011-08-25 15:46:13 +020028#include "gpio.h"
Daniel Lezcanodb145802011-06-21 00:57:08 +020029#include "mainloop.h"
Amit Arorae9e16b02010-08-03 10:15:20 +053030#include "powerdebug.h"
31
Sanjay Singh Rawat83b37c32013-04-17 14:57:07 +053032extern void sigwinch_handler(int);
33
Daniel Lezcano9db095c2016-02-18 13:05:01 +000034#define REGULATOR_OPTION 0x01
35#define SENSOR_OPTION 0x02
36#define CLOCK_OPTION 0x04
37#define GPIO_OPTION 0x08
38
39#define VERBOSE_OPTION 0x10
40#define DUMP_OPTION 0x20
41
42#define DEFAULT_OPTION (REGULATOR_OPTION | SENSOR_OPTION | \
43 CLOCK_OPTION | GPIO_OPTION)
44
Amit Arora422c52f2010-12-02 16:22:29 +053045void usage(void)
Amit Arorae9e16b02010-08-03 10:15:20 +053046{
Amit Arora422c52f2010-12-02 16:22:29 +053047 printf("Usage: powerdebug [OPTIONS]\n");
48 printf("\n");
49 printf("powerdebug -d [ -r ] [ -s ] [ -c [ -p <clock-name> ] ] "
50 "[ -v ]\n");
51 printf("powerdebug [ -r | -s | -c ]\n");
Amit Arora17552782010-12-02 12:23:14 +053052 printf(" -r, --regulator Show regulator information\n");
53 printf(" -s, --sensor Show sensor information\n");
54 printf(" -c, --clock Show clock information\n");
Amit Arora422c52f2010-12-02 16:22:29 +053055 printf(" -p, --findparents Show all parents for a particular"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060056 " clock\n");
Amit Arora17552782010-12-02 12:23:14 +053057 printf(" -t, --time Set ticktime in seconds (eg. 10.0)\n");
58 printf(" -d, --dump Dump information once (no refresh)\n");
Amit Arora422c52f2010-12-02 16:22:29 +053059 printf(" -v, --verbose Verbose mode (use with -r and/or"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060060 " -s)\n");
Amit Arora17552782010-12-02 12:23:14 +053061 printf(" -V, --version Show Version\n");
62 printf(" -h, --help Help\n");
Amit Arorae9e16b02010-08-03 10:15:20 +053063}
64
Amit Arora17552782010-12-02 12:23:14 +053065void version()
Amit Arorae9e16b02010-08-03 10:15:20 +053066{
Amit Arora17552782010-12-02 12:23:14 +053067 printf("powerdebug version %s\n", VERSION);
Amit Arorae9e16b02010-08-03 10:15:20 +053068}
69
Daniel Lezcano316bcae2011-03-23 14:37:30 +010070/*
71 * Options:
Daniel Lezcano716b23e2011-08-25 15:46:13 +020072 * -r, --regulator : regulators
Daniel Lezcano316bcae2011-03-23 14:37:30 +010073 * -s, --sensor : sensors
74 * -c, --clock : clocks
Daniel Lezcano716b23e2011-08-25 15:46:13 +020075 * -g, --gpio : gpios
Daniel Lezcano316bcae2011-03-23 14:37:30 +010076 * -p, --findparents : clockname whose parents have to be found
77 * -t, --time : ticktime
78 * -d, --dump : dump
79 * -v, --verbose : verbose
80 * -V, --version : version
81 * -h, --help : help
82 * no option / default : show usage!
83 */
84
85static struct option long_options[] = {
86 { "regulator", 0, 0, 'r' },
87 { "sensor", 0, 0, 's' },
88 { "clock", 0, 0, 'c' },
Daniel Lezcano716b23e2011-08-25 15:46:13 +020089 { "gpio", 0, 0, 'g' },
Daniel Lezcano316bcae2011-03-23 14:37:30 +010090 { "findparents", 1, 0, 'p' },
91 { "time", 1, 0, 't' },
92 { "dump", 0, 0, 'd' },
93 { "verbose", 0, 0, 'v' },
94 { "version", 0, 0, 'V' },
95 { "help", 0, 0, 'h' },
96 { 0, 0, 0, 0 }
97};
98
99struct powerdebug_options {
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000100 int flags;
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100101 unsigned int ticktime;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100102 int selectedwindow;
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100103 char *clkname;
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100104};
105
106int getoptions(int argc, char *argv[], struct powerdebug_options *options)
Amit Arorae9e16b02010-08-03 10:15:20 +0530107{
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100108 int c;
Amit Arorae9e16b02010-08-03 10:15:20 +0530109
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100110 memset(options, 0, sizeof(*options));
111 options->ticktime = 10;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100112 options->selectedwindow = -1;
Amit Arorae9e16b02010-08-03 10:15:20 +0530113
Amit Arorafefe8bf2010-08-05 13:31:20 +0530114 while (1) {
115 int optindex = 0;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530116
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200117 c = getopt_long(argc, argv, "rscgp:t:dvVh",
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100118 long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +0530119 if (c == -1)
120 break;
121
122 switch (c) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530123 case 'r':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000124 options->flags |= REGULATOR_OPTION;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100125 options->selectedwindow = REGULATOR;
Amit Arora6e774cd2010-10-28 11:31:24 +0530126 break;
127 case 's':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000128 options->flags |= SENSOR_OPTION;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100129 options->selectedwindow = SENSOR;
Amit Arora6e774cd2010-10-28 11:31:24 +0530130 break;
131 case 'c':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000132 options->flags |= CLOCK_OPTION;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100133 options->selectedwindow = CLOCK;
Amit Arora6e774cd2010-10-28 11:31:24 +0530134 break;
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200135 case 'g':
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000136 options->flags |= GPIO_OPTION;
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200137 options->selectedwindow = GPIO;
138 break;
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000139 case 'd':
140 options->flags |= DUMP_OPTION;
141 break;
142 case 'v':
143 options->flags |= VERBOSE_OPTION;
144 break;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530145 case 'p':
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100146 options->clkname = strdup(optarg);
147 if (!options->clkname) {
Daniel Lezcano9420fde2011-03-23 14:37:31 +0100148 fprintf(stderr, "failed to allocate memory");
149 return -1;
150 }
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000151 options->flags |= (DUMP_OPTION | CLOCK_OPTION);
Amit Aroraf4fb8102010-11-30 13:55:50 +0530152 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530153 case 't':
Daniel Lezcano7f112da2011-03-23 14:37:32 +0100154 options->ticktime = atoi(optarg);
Amit Arora6e774cd2010-10-28 11:31:24 +0530155 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530156 case 'V':
157 version();
158 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530159 case '?':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100160 fprintf(stderr, "%s: Unknown option %c'.\n",
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600161 argv[0], optopt);
Amit Arora6e774cd2010-10-28 11:31:24 +0530162 default:
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100163 return -1;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530164 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530165 }
Amit Aroraa06a7302010-12-02 15:59:37 +0530166
Daniel Lezcano934fc092011-03-26 22:06:18 +0100167 /* No system specified to be dump, let's default to all */
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000168 if (!(options->flags & DEFAULT_OPTION))
169 options->flags |= DEFAULT_OPTION;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530170
Daniel Lezcanoc9c14622011-03-26 22:06:10 +0100171 if (options->selectedwindow == -1)
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100172 options->selectedwindow = CLOCK;
Amit Arorae9e16b02010-08-03 10:15:20 +0530173
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100174 return 0;
175}
176
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200177static int powerdebug_dump(struct powerdebug_options *options)
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100178{
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000179 if (options->flags & REGULATOR_OPTION)
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200180 regulator_dump();
Daniel Lezcanob5746712011-03-26 22:06:05 +0100181
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000182 if (options->flags & CLOCK_OPTION)
Daniel Lezcano597892a2011-06-15 15:45:12 +0200183 clock_dump(options->clkname);
Daniel Lezcanob5746712011-03-26 22:06:05 +0100184
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000185 if (options->flags & SENSOR_OPTION)
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200186 sensor_dump();
Daniel Lezcanob5746712011-03-26 22:06:05 +0100187
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000188 if (options->flags & GPIO_OPTION)
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200189 gpio_dump();
190
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100191 return 0;
192}
193
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200194static int powerdebug_display(struct powerdebug_options *options)
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100195{
Daniel Lezcano7b3da502011-06-15 15:45:12 +0200196 if (display_init(options->selectedwindow)) {
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100197 printf("failed to initialize display\n");
198 return -1;
199 }
200
Daniel Lezcanodb145802011-06-21 00:57:08 +0200201 if (mainloop(options->ticktime * 1000))
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100202 return -1;
203
204 return 0;
205}
206
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100207static struct powerdebug_options *powerdebug_init(void)
208{
209 struct powerdebug_options *options;
210
Daniel Lezcano188ff0f2016-02-18 15:40:12 +0000211 signal(SIGWINCH, sigwinch_handler);
212
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100213 options = malloc(sizeof(*options));
214 if (!options)
215 return NULL;
216
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100217 return options;
218}
219
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100220int main(int argc, char **argv)
221{
222 struct powerdebug_options *options;
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200223 int ret;
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100224
Sanjay Singh Rawat9fe0c052013-02-22 17:57:18 +0530225#ifdef __ANDROID__
226 if (setenv("TERM", "xterm", 1) < 0) {
227 fprintf(stderr, "setenv failure");
228 return 1;
229 }
230 if (setenv("TERMINFO", "/system/etc/terminfo", 1) < 0) {
231 fprintf(stderr, "setenv failure");
232 return 1;
233 }
234#endif
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100235 options = powerdebug_init();
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100236 if (!options) {
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100237 fprintf(stderr, "not enough memory to allocate options\n");
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100238 return 1;
239 }
240
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100241 if (getoptions(argc, argv, options)) {
242 usage();
243 return 1;
244 }
245
Daniel Lezcanodb145802011-06-21 00:57:08 +0200246 if (mainloop_init()) {
247 fprintf(stderr, "failed to initialize the mainloop\n");
248 return 1;
249 }
250
Daniel Lezcano188ff0f2016-02-18 15:40:12 +0000251 if ((options->flags & REGULATOR_OPTION) && regulator_init()) {
Sanjay Singh Rawat5fef0052013-04-17 15:02:01 +0530252 printf("failed to initialize regulator\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000253 options->flags &= ~REGULATOR_OPTION;
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +0100254 }
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100255
Daniel Lezcano188ff0f2016-02-18 15:40:12 +0000256 if ((options->flags & CLOCK_OPTION) && clock_init()) {
Daniel Lezcanof0e06652011-03-26 22:06:19 +0100257 printf("failed to initialize clock details (check debugfs)\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000258 options->flags &= ~CLOCK_OPTION;
Daniel Lezcanof0e06652011-03-26 22:06:19 +0100259 }
260
Daniel Lezcano188ff0f2016-02-18 15:40:12 +0000261 if ((options->flags & SENSOR_OPTION) && sensor_init()) {
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200262 printf("failed to initialize sensors\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000263 options->flags &= SENSOR_OPTION;
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200264 }
265
Daniel Lezcano188ff0f2016-02-18 15:40:12 +0000266 if ((options->flags & GPIO_OPTION) && gpio_init()) {
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200267 printf("failed to initialize gpios\n");
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000268 options->flags &= GPIO_OPTION;
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200269 }
270
Daniel Lezcano9db095c2016-02-18 13:05:01 +0000271 ret = options->flags & DUMP_OPTION ? powerdebug_dump(options) :
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200272 powerdebug_display(options);
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100273
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100274 return ret < 0;
Amit Arorae9e16b02010-08-03 10:15:20 +0530275}