aboutsummaryrefslogtreecommitdiff
path: root/powerdebug.c
blob: 164126d07ce451571a221bbe385f929c9b4fb636 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
 * Power debug tool (powerdebug)
 *
 * Copyright (C) 2016, Linaro Limited.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#include <getopt.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <ncurses.h>
#include "display.h"
#include "mainloop.h"
#include "powerdebug.h"

void usage(void)
{
	printf("Usage: powerdebug [OPTIONS]\n");
	printf("\n");
	printf("powerdebug -d [ -r ] [ -s ] [ -c [ -p <clock-name> ] ] "
		"[ -v ]\n");
	printf("powerdebug [ -r | -s | -c ]\n");
	printf("  -r, --regulator 	Show regulator information\n");
	printf("  -s, --sensor		Show sensor information\n");
	printf("  -c, --clock		Show clock information\n");
	printf("  -p, --findparents	Show all parents for a particular"
		" clock\n");
	printf("  -t, --time		Set ticktime in seconds (eg. 10.0)\n");
	printf("  -d, --dump		Dump information once (no refresh)\n");
	printf("  -v, --verbose		Verbose mode (use with -r and/or"
		" -s)\n");
	printf("  -V, --version		Show Version\n");
	printf("  -h, --help 		Help\n");
}

void version()
{
	printf("powerdebug version %s\n", VERSION);
}

/*
 * Options:
 * -r, --regulator      : regulators
 * -s, --sensor	 	: sensors
 * -c, --clock	  	: clocks
 * -g, --gpio           : gpios
 * -p, --findparents    : clockname whose parents have to be found
 * -t, --time		: ticktime
 * -d, --dump		: dump
 * -v, --verbose	: verbose
 * -V, --version	: version
 * -h, --help		: help
 * no option / default : show usage!
 */

static struct option long_options[] = {
	{ "regulator", 0, 0, 'r' },
	{ "sensor", 0, 0, 's' },
	{ "clock",  0, 0, 'c' },
	{ "gpio",  0, 0, 'g' },
	{ "findparents", 1, 0, 'p' },
	{ "time", 1, 0, 't' },
	{ "dump", 0, 0, 'd' },
	{ "verbose", 0, 0, 'v' },
	{ "version", 0, 0, 'V' },
	{ "help", 0, 0, 'h' },
	{ 0, 0, 0, 0 }
};

int getoptions(int argc, char *argv[], struct powerdebug_options *options)
{
	int c;

	memset(options, 0, sizeof(*options));
	options->ticktime = 1;

	while (1) {
		int optindex = 0;

		c = getopt_long(argc, argv, "rscgp:t:dvVh",
				long_options, &optindex);
		if (c == -1)
			break;

		switch (c) {
		case 'r':
			options->flags |= REGULATOR_OPTION;
			break;
		case 's':
			options->flags |= SENSOR_OPTION;
			break;
		case 'c':
			options->flags |= CLOCK_OPTION;
			break;
		case 'g':
			options->flags |= GPIO_OPTION;
			break;
		case 'd':
			options->flags |= DUMP_OPTION;
			break;
		case 'v':
			options->flags |= VERBOSE_OPTION;
			break;
		case 'p':
			options->clkname = strdup(optarg);
			if (!options->clkname) {
				fprintf(stderr, "failed to allocate memory");
				return -1;
			}
			options->flags |= (DUMP_OPTION | CLOCK_OPTION);
			break;
		case 't':
			options->ticktime = atoi(optarg);
			break;
		case 'V':
			version();
			break;
		case '?':
			fprintf(stderr, "%s: Unknown option %c'.\n",
				argv[0], optopt);
		default:
			return -1;
		}
	}

	/* No system specified to be dump, let's default to all */
	if (!(options->flags & DEFAULT_OPTION))
		options->flags |= DEFAULT_OPTION;

	return 0;
}

static int powerdebug_dump(struct powerdebug_options *options)
{
	if (options->flags & REGULATOR_OPTION)
		regulator_dump();

	if (options->flags & CLOCK_OPTION)
		clock_dump(options->clkname);

	if (options->flags & SENSOR_OPTION)
		sensor_dump();

	if (options->flags & GPIO_OPTION)
		gpio_dump();

	return 0;
}

static int powerdebug_display(struct powerdebug_options *options)
{
	if (display_init(options)) {
		printf("failed to initialize display\n");
		return -1;
	}

	if (mainloop(options->ticktime * 1000))
		return -1;

	return 0;
}

static struct powerdebug_options *powerdebug_init(void)
{
	struct powerdebug_options *options;

	options = malloc(sizeof(*options));
	if (!options)
		return NULL;

	return options;
}

int main(int argc, char **argv)
{
	struct powerdebug_options *options;
	int ret;

#ifdef __ANDROID__
	if (setenv("TERM", "xterm", 1) < 0) {
		fprintf(stderr, "setenv failure");
		return 1;
	}
	if (setenv("TERMINFO", "/system/etc/terminfo", 1) < 0) {
		fprintf(stderr, "setenv failure");
		return 1;
	}
#endif
	options = powerdebug_init();
	if (!options) {
		fprintf(stderr, "not enough memory to allocate options\n");
		return 1;
	}

	if (getoptions(argc, argv, options)) {
		usage();
		return 1;
	}

	if (mainloop_init()) {
		fprintf(stderr, "failed to initialize the mainloop\n");
		return 1;
	}

	if (regulator_init(options)) {
		printf("failed to initialize regulator\n");
		options->flags &= ~REGULATOR_OPTION;
	}

	if (clock_init(options)) {
		printf("failed to initialize clock details (check debugfs)\n");
		options->flags &= ~CLOCK_OPTION;
	}

	if (sensor_init(options)) {
		printf("failed to initialize sensors\n");
		options->flags &= SENSOR_OPTION;
	}

	if (gpio_init(options)) {
		printf("failed to initialize gpios\n");
		options->flags &= GPIO_OPTION;
	}

	ret = options->flags & DUMP_OPTION ? powerdebug_dump(options) :
		powerdebug_display(options);

	return ret < 0;
}