blob: 6cf3a1b7e42d8ec9ffdc80947c70c85019d40acf [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
Amit Arora422c52f2010-12-02 16:22:29 +053034void usage(void)
Amit Arorae9e16b02010-08-03 10:15:20 +053035{
Amit Arora422c52f2010-12-02 16:22:29 +053036 printf("Usage: powerdebug [OPTIONS]\n");
37 printf("\n");
38 printf("powerdebug -d [ -r ] [ -s ] [ -c [ -p <clock-name> ] ] "
39 "[ -v ]\n");
40 printf("powerdebug [ -r | -s | -c ]\n");
Amit Arora17552782010-12-02 12:23:14 +053041 printf(" -r, --regulator Show regulator information\n");
42 printf(" -s, --sensor Show sensor information\n");
43 printf(" -c, --clock Show clock information\n");
Amit Arora422c52f2010-12-02 16:22:29 +053044 printf(" -p, --findparents Show all parents for a particular"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060045 " clock\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
88struct powerdebug_options {
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010089 bool verbose;
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010090 bool regulators;
91 bool sensors;
92 bool clocks;
Daniel Lezcano716b23e2011-08-25 15:46:13 +020093 bool gpios;
Daniel Lezcanoa70d9492011-03-23 14:37:40 +010094 bool dump;
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010095 unsigned int ticktime;
Daniel Lezcano558a6d52011-03-23 14:37:41 +010096 int selectedwindow;
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +010097 char *clkname;
Daniel Lezcano316bcae2011-03-23 14:37:30 +010098};
99
100int getoptions(int argc, char *argv[], struct powerdebug_options *options)
Amit Arorae9e16b02010-08-03 10:15:20 +0530101{
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100102 int c;
Amit Arorae9e16b02010-08-03 10:15:20 +0530103
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100104 memset(options, 0, sizeof(*options));
105 options->ticktime = 10;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100106 options->selectedwindow = -1;
Amit Arorae9e16b02010-08-03 10:15:20 +0530107
Amit Arorafefe8bf2010-08-05 13:31:20 +0530108 while (1) {
109 int optindex = 0;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530110
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200111 c = getopt_long(argc, argv, "rscgp:t:dvVh",
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100112 long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +0530113 if (c == -1)
114 break;
115
116 switch (c) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530117 case 'r':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100118 options->regulators = true;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100119 options->selectedwindow = REGULATOR;
Amit Arora6e774cd2010-10-28 11:31:24 +0530120 break;
121 case 's':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100122 options->sensors = true;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100123 options->selectedwindow = SENSOR;
Amit Arora6e774cd2010-10-28 11:31:24 +0530124 break;
125 case 'c':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100126 options->clocks = true;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100127 options->selectedwindow = CLOCK;
Amit Arora6e774cd2010-10-28 11:31:24 +0530128 break;
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200129 case 'g':
130 options->gpios = true;
131 options->selectedwindow = GPIO;
132 break;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530133 case 'p':
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100134 options->clkname = strdup(optarg);
135 if (!options->clkname) {
Daniel Lezcano9420fde2011-03-23 14:37:31 +0100136 fprintf(stderr, "failed to allocate memory");
137 return -1;
138 }
Amit Kucheriaeab558a2011-03-25 09:51:41 +0200139 options->dump = true; /* Assume -dc in case of -p */
140 options->clocks = true;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530141 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530142 case 't':
Daniel Lezcano7f112da2011-03-23 14:37:32 +0100143 options->ticktime = atoi(optarg);
Amit Arora6e774cd2010-10-28 11:31:24 +0530144 break;
145 case 'd':
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100146 options->dump = true;
Amit Arora6e774cd2010-10-28 11:31:24 +0530147 break;
148 case 'v':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100149 options->verbose = true;
Amit Arora6e774cd2010-10-28 11:31:24 +0530150 break;
151 case 'V':
152 version();
153 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530154 case '?':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100155 fprintf(stderr, "%s: Unknown option %c'.\n",
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600156 argv[0], optopt);
Amit Arora6e774cd2010-10-28 11:31:24 +0530157 default:
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100158 return -1;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530159 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530160 }
Amit Aroraa06a7302010-12-02 15:59:37 +0530161
Daniel Lezcano934fc092011-03-26 22:06:18 +0100162 /* No system specified to be dump, let's default to all */
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200163 if (!options->regulators && !options->clocks &&
164 !options->sensors && !options->gpios)
165 options->regulators = options->clocks =
166 options->sensors = options->gpios = true;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530167
Daniel Lezcanoc9c14622011-03-26 22:06:10 +0100168 if (options->selectedwindow == -1)
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100169 options->selectedwindow = CLOCK;
Amit Arorae9e16b02010-08-03 10:15:20 +0530170
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100171 return 0;
172}
173
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200174static int powerdebug_dump(struct powerdebug_options *options)
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100175{
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200176 if (options->regulators)
177 regulator_dump();
Daniel Lezcanob5746712011-03-26 22:06:05 +0100178
Daniel Lezcanoc7891942011-06-08 23:30:00 +0200179 if (options->clocks)
Daniel Lezcano597892a2011-06-15 15:45:12 +0200180 clock_dump(options->clkname);
Daniel Lezcanob5746712011-03-26 22:06:05 +0100181
182 if (options->sensors)
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200183 sensor_dump();
Daniel Lezcanob5746712011-03-26 22:06:05 +0100184
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200185 if (options->gpios)
186 gpio_dump();
187
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100188 return 0;
189}
190
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200191static int powerdebug_display(struct powerdebug_options *options)
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100192{
Daniel Lezcano7b3da502011-06-15 15:45:12 +0200193 if (display_init(options->selectedwindow)) {
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100194 printf("failed to initialize display\n");
195 return -1;
196 }
197
Daniel Lezcanodb145802011-06-21 00:57:08 +0200198 if (mainloop(options->ticktime * 1000))
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100199 return -1;
200
201 return 0;
202}
203
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100204static struct powerdebug_options *powerdebug_init(void)
205{
206 struct powerdebug_options *options;
207
208 options = malloc(sizeof(*options));
209 if (!options)
210 return NULL;
211
212 memset(options, 0, sizeof(*options));
Sanjay Singh Rawat83b37c32013-04-17 14:57:07 +0530213 signal(SIGWINCH, sigwinch_handler);
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100214
215 return options;
216}
217
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100218int main(int argc, char **argv)
219{
220 struct powerdebug_options *options;
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200221 int ret;
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100222
Sanjay Singh Rawat9fe0c052013-02-22 17:57:18 +0530223#ifdef __ANDROID__
224 if (setenv("TERM", "xterm", 1) < 0) {
225 fprintf(stderr, "setenv failure");
226 return 1;
227 }
228 if (setenv("TERMINFO", "/system/etc/terminfo", 1) < 0) {
229 fprintf(stderr, "setenv failure");
230 return 1;
231 }
232#endif
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100233 options = powerdebug_init();
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100234 if (!options) {
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100235 fprintf(stderr, "not enough memory to allocate options\n");
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100236 return 1;
237 }
238
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100239 if (getoptions(argc, argv, options)) {
240 usage();
241 return 1;
242 }
243
Daniel Lezcanodb145802011-06-21 00:57:08 +0200244 if (mainloop_init()) {
245 fprintf(stderr, "failed to initialize the mainloop\n");
246 return 1;
247 }
248
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200249 if (regulator_init()) {
Sanjay Singh Rawat5fef0052013-04-17 15:02:01 +0530250 printf("failed to initialize regulator\n");
Daniel Lezcano6e48fa42011-03-26 22:06:23 +0100251 options->regulators = false;
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +0100252 }
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100253
Daniel Lezcanof0e06652011-03-26 22:06:19 +0100254 if (clock_init()) {
255 printf("failed to initialize clock details (check debugfs)\n");
256 options->clocks = false;
257 }
258
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200259 if (sensor_init()) {
260 printf("failed to initialize sensors\n");
261 options->sensors = false;
262 }
263
Daniel Lezcano716b23e2011-08-25 15:46:13 +0200264 if (gpio_init()) {
265 printf("failed to initialize gpios\n");
266 options->gpios = false;
267 }
268
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200269 ret = options->dump ? powerdebug_dump(options) :
270 powerdebug_display(options);
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100271
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100272 return ret < 0;
Amit Arorae9e16b02010-08-03 10:15:20 +0530273}