blob: b4575c43f7f6b2d024745827759d991dee162b2e [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>
Daniel Lezcanocac52d92011-03-26 22:05:49 +010023#include "regulator.h"
Daniel Lezcano192c1d22011-03-26 22:06:14 +010024#include "display.h"
Daniel Lezcanof0e06652011-03-26 22:06:19 +010025#include "clocks.h"
Daniel Lezcano597892a2011-06-15 15:45:12 +020026#include "sensor.h"
Daniel Lezcanodb145802011-06-21 00:57:08 +020027#include "mainloop.h"
Amit Arorae9e16b02010-08-03 10:15:20 +053028#include "powerdebug.h"
29
Amit Arora422c52f2010-12-02 16:22:29 +053030void usage(void)
Amit Arorae9e16b02010-08-03 10:15:20 +053031{
Amit Arora422c52f2010-12-02 16:22:29 +053032 printf("Usage: powerdebug [OPTIONS]\n");
33 printf("\n");
34 printf("powerdebug -d [ -r ] [ -s ] [ -c [ -p <clock-name> ] ] "
35 "[ -v ]\n");
36 printf("powerdebug [ -r | -s | -c ]\n");
Amit Arora17552782010-12-02 12:23:14 +053037 printf(" -r, --regulator Show regulator information\n");
38 printf(" -s, --sensor Show sensor information\n");
39 printf(" -c, --clock Show clock information\n");
Amit Arora422c52f2010-12-02 16:22:29 +053040 printf(" -p, --findparents Show all parents for a particular"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060041 " clock\n");
Amit Arora17552782010-12-02 12:23:14 +053042 printf(" -t, --time Set ticktime in seconds (eg. 10.0)\n");
43 printf(" -d, --dump Dump information once (no refresh)\n");
Amit Arora422c52f2010-12-02 16:22:29 +053044 printf(" -v, --verbose Verbose mode (use with -r and/or"
Amit Kucheriaa0adae42011-01-12 10:54:23 -060045 " -s)\n");
Amit Arora17552782010-12-02 12:23:14 +053046 printf(" -V, --version Show Version\n");
47 printf(" -h, --help Help\n");
Amit Arorae9e16b02010-08-03 10:15:20 +053048}
49
Amit Arora17552782010-12-02 12:23:14 +053050void version()
Amit Arorae9e16b02010-08-03 10:15:20 +053051{
Amit Arora17552782010-12-02 12:23:14 +053052 printf("powerdebug version %s\n", VERSION);
Amit Arorae9e16b02010-08-03 10:15:20 +053053}
54
Daniel Lezcano316bcae2011-03-23 14:37:30 +010055/*
56 * Options:
57 * -r, --regulator : regulator
58 * -s, --sensor : sensors
59 * -c, --clock : clocks
60 * -p, --findparents : clockname whose parents have to be found
61 * -t, --time : ticktime
62 * -d, --dump : dump
63 * -v, --verbose : verbose
64 * -V, --version : version
65 * -h, --help : help
66 * no option / default : show usage!
67 */
68
69static struct option long_options[] = {
70 { "regulator", 0, 0, 'r' },
71 { "sensor", 0, 0, 's' },
72 { "clock", 0, 0, 'c' },
73 { "findparents", 1, 0, 'p' },
74 { "time", 1, 0, 't' },
75 { "dump", 0, 0, 'd' },
76 { "verbose", 0, 0, 'v' },
77 { "version", 0, 0, 'V' },
78 { "help", 0, 0, 'h' },
79 { 0, 0, 0, 0 }
80};
81
82struct powerdebug_options {
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010083 bool verbose;
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010084 bool regulators;
85 bool sensors;
86 bool clocks;
Daniel Lezcanoa70d9492011-03-23 14:37:40 +010087 bool dump;
Daniel Lezcanoc5afe832011-03-23 14:37:36 +010088 unsigned int ticktime;
Daniel Lezcano558a6d52011-03-23 14:37:41 +010089 int selectedwindow;
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +010090 char *clkname;
Daniel Lezcano316bcae2011-03-23 14:37:30 +010091};
92
93int getoptions(int argc, char *argv[], struct powerdebug_options *options)
Amit Arorae9e16b02010-08-03 10:15:20 +053094{
Daniel Lezcano316bcae2011-03-23 14:37:30 +010095 int c;
Amit Arorae9e16b02010-08-03 10:15:20 +053096
Daniel Lezcano316bcae2011-03-23 14:37:30 +010097 memset(options, 0, sizeof(*options));
98 options->ticktime = 10;
Daniel Lezcano558a6d52011-03-23 14:37:41 +010099 options->selectedwindow = -1;
Amit Arorae9e16b02010-08-03 10:15:20 +0530100
Amit Arorafefe8bf2010-08-05 13:31:20 +0530101 while (1) {
102 int optindex = 0;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530103
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100104 c = getopt_long(argc, argv, "rscp:t:dvVh",
105 long_options, &optindex);
Amit Arorafefe8bf2010-08-05 13:31:20 +0530106 if (c == -1)
107 break;
108
109 switch (c) {
Amit Arora6e774cd2010-10-28 11:31:24 +0530110 case 'r':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100111 options->regulators = true;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100112 options->selectedwindow = REGULATOR;
Amit Arora6e774cd2010-10-28 11:31:24 +0530113 break;
114 case 's':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100115 options->sensors = true;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100116 options->selectedwindow = SENSOR;
Amit Arora6e774cd2010-10-28 11:31:24 +0530117 break;
118 case 'c':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100119 options->clocks = true;
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100120 options->selectedwindow = CLOCK;
Amit Arora6e774cd2010-10-28 11:31:24 +0530121 break;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530122 case 'p':
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100123 options->clkname = strdup(optarg);
124 if (!options->clkname) {
Daniel Lezcano9420fde2011-03-23 14:37:31 +0100125 fprintf(stderr, "failed to allocate memory");
126 return -1;
127 }
Amit Kucheriaeab558a2011-03-25 09:51:41 +0200128 options->dump = true; /* Assume -dc in case of -p */
129 options->clocks = true;
Amit Aroraf4fb8102010-11-30 13:55:50 +0530130 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530131 case 't':
Daniel Lezcano7f112da2011-03-23 14:37:32 +0100132 options->ticktime = atoi(optarg);
Amit Arora6e774cd2010-10-28 11:31:24 +0530133 break;
134 case 'd':
Daniel Lezcanoa70d9492011-03-23 14:37:40 +0100135 options->dump = true;
Amit Arora6e774cd2010-10-28 11:31:24 +0530136 break;
137 case 'v':
Daniel Lezcanoc5afe832011-03-23 14:37:36 +0100138 options->verbose = true;
Amit Arora6e774cd2010-10-28 11:31:24 +0530139 break;
140 case 'V':
141 version();
142 break;
Amit Arora6e774cd2010-10-28 11:31:24 +0530143 case '?':
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100144 fprintf(stderr, "%s: Unknown option %c'.\n",
Amit Kucheriaa0adae42011-01-12 10:54:23 -0600145 argv[0], optopt);
Amit Arora6e774cd2010-10-28 11:31:24 +0530146 default:
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100147 return -1;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530148 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530149 }
Amit Aroraa06a7302010-12-02 15:59:37 +0530150
Daniel Lezcano934fc092011-03-26 22:06:18 +0100151 /* No system specified to be dump, let's default to all */
152 if (!options->regulators && !options->clocks && !options->sensors)
153 options->regulators = options->clocks = options->sensors = true;
Amit Arorafefe8bf2010-08-05 13:31:20 +0530154
Daniel Lezcanoc9c14622011-03-26 22:06:10 +0100155 if (options->selectedwindow == -1)
Daniel Lezcano558a6d52011-03-23 14:37:41 +0100156 options->selectedwindow = CLOCK;
Amit Arorae9e16b02010-08-03 10:15:20 +0530157
Daniel Lezcano316bcae2011-03-23 14:37:30 +0100158 return 0;
159}
160
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200161static int powerdebug_dump(struct powerdebug_options *options)
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100162{
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200163 if (options->regulators)
164 regulator_dump();
Daniel Lezcanob5746712011-03-26 22:06:05 +0100165
Daniel Lezcanoc7891942011-06-08 23:30:00 +0200166 if (options->clocks)
Daniel Lezcano597892a2011-06-15 15:45:12 +0200167 clock_dump(options->clkname);
Daniel Lezcanob5746712011-03-26 22:06:05 +0100168
169 if (options->sensors)
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200170 sensor_dump();
Daniel Lezcanob5746712011-03-26 22:06:05 +0100171
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100172 return 0;
173}
174
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200175static int powerdebug_display(struct powerdebug_options *options)
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100176{
Daniel Lezcano7b3da502011-06-15 15:45:12 +0200177 if (display_init(options->selectedwindow)) {
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100178 printf("failed to initialize display\n");
179 return -1;
180 }
181
Daniel Lezcanodb145802011-06-21 00:57:08 +0200182 if (mainloop(options->ticktime * 1000))
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100183 return -1;
184
185 return 0;
186}
187
Daniel Lezcano6e0c9c82011-03-26 22:06:07 +0100188static struct powerdebug_options *powerdebug_init(void)
189{
190 struct powerdebug_options *options;
191
192 options = malloc(sizeof(*options));
193 if (!options)
194 return NULL;
195
196 memset(options, 0, sizeof(*options));
197
198 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
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 Lezcanob25be4a2011-06-15 15:45:12 +0200222 if (regulator_init()) {
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +0100223 printf("not enough memory to allocate regulators info\n");
Daniel Lezcano6e48fa42011-03-26 22:06:23 +0100224 options->regulators = false;
Daniel Lezcano4aab2fe2011-03-26 22:05:53 +0100225 }
Daniel Lezcano0051f4f2011-03-23 14:37:34 +0100226
Daniel Lezcanof0e06652011-03-26 22:06:19 +0100227 if (clock_init()) {
228 printf("failed to initialize clock details (check debugfs)\n");
229 options->clocks = false;
230 }
231
Daniel Lezcano3d0aef42011-06-15 15:45:12 +0200232 if (sensor_init()) {
233 printf("failed to initialize sensors\n");
234 options->sensors = false;
235 }
236
Daniel Lezcanob25be4a2011-06-15 15:45:12 +0200237 ret = options->dump ? powerdebug_dump(options) :
238 powerdebug_display(options);
Daniel Lezcano21c04d42011-03-26 22:06:04 +0100239
Daniel Lezcanoc08f1f22011-03-26 22:06:21 +0100240 return ret < 0;
Amit Arorae9e16b02010-08-03 10:15:20 +0530241}