Daniel Lezcano | 7c15fad | 2016-02-18 15:57:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Power debug tool (powerdebug) |
Amit Arora | 39f2954 | 2010-09-14 12:03:22 +0530 | [diff] [blame] | 3 | * |
Daniel Lezcano | 7c15fad | 2016-02-18 15:57:43 +0000 | [diff] [blame] | 4 | * Copyright (C) 2016, Linaro Limited. |
Amit Arora | 39f2954 | 2010-09-14 12:03:22 +0530 | [diff] [blame] | 5 | * |
Daniel Lezcano | 7c15fad | 2016-02-18 15:57:43 +0000 | [diff] [blame] | 6 | * 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 Arora | 39f2954 | 2010-09-14 12:03:22 +0530 | [diff] [blame] | 10 | * |
Daniel Lezcano | 7c15fad | 2016-02-18 15:57:43 +0000 | [diff] [blame] | 11 | * 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 Arora | 39f2954 | 2010-09-14 12:03:22 +0530 | [diff] [blame] | 21 | |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 22 | #include <getopt.h> |
Daniel Lezcano | c5afe83 | 2011-03-23 14:37:36 +0100 | [diff] [blame] | 23 | #include <stdbool.h> |
Daniel Lezcano | 1562748 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <stdio.h> |
| 27 | #include <errno.h> |
| 28 | #include <ncurses.h> |
Sanjay Singh Rawat | 83b37c3 | 2013-04-17 14:57:07 +0530 | [diff] [blame] | 29 | #include <signal.h> |
Daniel Lezcano | 192c1d2 | 2011-03-26 22:06:14 +0100 | [diff] [blame] | 30 | #include "display.h" |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 31 | #include "mainloop.h" |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 32 | #include "powerdebug.h" |
| 33 | |
Sanjay Singh Rawat | 83b37c3 | 2013-04-17 14:57:07 +0530 | [diff] [blame] | 34 | extern void sigwinch_handler(int); |
| 35 | |
Amit Arora | 422c52f | 2010-12-02 16:22:29 +0530 | [diff] [blame] | 36 | void usage(void) |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 37 | { |
Amit Arora | 422c52f | 2010-12-02 16:22:29 +0530 | [diff] [blame] | 38 | printf("Usage: powerdebug [OPTIONS]\n"); |
| 39 | printf("\n"); |
| 40 | printf("powerdebug -d [ -r ] [ -s ] [ -c [ -p <clock-name> ] ] " |
| 41 | "[ -v ]\n"); |
| 42 | printf("powerdebug [ -r | -s | -c ]\n"); |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 43 | printf(" -r, --regulator Show regulator information\n"); |
| 44 | printf(" -s, --sensor Show sensor information\n"); |
| 45 | printf(" -c, --clock Show clock information\n"); |
Amit Arora | 422c52f | 2010-12-02 16:22:29 +0530 | [diff] [blame] | 46 | printf(" -p, --findparents Show all parents for a particular" |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 47 | " clock\n"); |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 48 | printf(" -t, --time Set ticktime in seconds (eg. 10.0)\n"); |
| 49 | printf(" -d, --dump Dump information once (no refresh)\n"); |
Amit Arora | 422c52f | 2010-12-02 16:22:29 +0530 | [diff] [blame] | 50 | printf(" -v, --verbose Verbose mode (use with -r and/or" |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 51 | " -s)\n"); |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 52 | printf(" -V, --version Show Version\n"); |
| 53 | printf(" -h, --help Help\n"); |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 54 | } |
| 55 | |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 56 | void version() |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 57 | { |
Amit Arora | 1755278 | 2010-12-02 12:23:14 +0530 | [diff] [blame] | 58 | printf("powerdebug version %s\n", VERSION); |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 59 | } |
| 60 | |
Daniel Lezcano | 316bcae | 2011-03-23 14:37:30 +0100 | [diff] [blame] | 61 | /* |
| 62 | * Options: |
Daniel Lezcano | 716b23e | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 63 | * -r, --regulator : regulators |
Daniel Lezcano | 316bcae | 2011-03-23 14:37:30 +0100 | [diff] [blame] | 64 | * -s, --sensor : sensors |
| 65 | * -c, --clock : clocks |
Daniel Lezcano | 716b23e | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 66 | * -g, --gpio : gpios |
Daniel Lezcano | 316bcae | 2011-03-23 14:37:30 +0100 | [diff] [blame] | 67 | * -p, --findparents : clockname whose parents have to be found |
| 68 | * -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 | |
| 76 | static struct option long_options[] = { |
| 77 | { "regulator", 0, 0, 'r' }, |
| 78 | { "sensor", 0, 0, 's' }, |
| 79 | { "clock", 0, 0, 'c' }, |
Daniel Lezcano | 716b23e | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 80 | { "gpio", 0, 0, 'g' }, |
Daniel Lezcano | 316bcae | 2011-03-23 14:37:30 +0100 | [diff] [blame] | 81 | { "findparents", 1, 0, 'p' }, |
| 82 | { "time", 1, 0, 't' }, |
| 83 | { "dump", 0, 0, 'd' }, |
| 84 | { "verbose", 0, 0, 'v' }, |
| 85 | { "version", 0, 0, 'V' }, |
| 86 | { "help", 0, 0, 'h' }, |
| 87 | { 0, 0, 0, 0 } |
| 88 | }; |
| 89 | |
Daniel Lezcano | 316bcae | 2011-03-23 14:37:30 +0100 | [diff] [blame] | 90 | int getoptions(int argc, char *argv[], struct powerdebug_options *options) |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 91 | { |
Daniel Lezcano | 316bcae | 2011-03-23 14:37:30 +0100 | [diff] [blame] | 92 | int c; |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 93 | |
Daniel Lezcano | 316bcae | 2011-03-23 14:37:30 +0100 | [diff] [blame] | 94 | memset(options, 0, sizeof(*options)); |
| 95 | options->ticktime = 10; |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 96 | |
Amit Arora | fefe8bf | 2010-08-05 13:31:20 +0530 | [diff] [blame] | 97 | while (1) { |
| 98 | int optindex = 0; |
Amit Arora | fefe8bf | 2010-08-05 13:31:20 +0530 | [diff] [blame] | 99 | |
Daniel Lezcano | 716b23e | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 100 | c = getopt_long(argc, argv, "rscgp:t:dvVh", |
Daniel Lezcano | 316bcae | 2011-03-23 14:37:30 +0100 | [diff] [blame] | 101 | long_options, &optindex); |
Amit Arora | fefe8bf | 2010-08-05 13:31:20 +0530 | [diff] [blame] | 102 | if (c == -1) |
| 103 | break; |
| 104 | |
| 105 | switch (c) { |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 106 | case 'r': |
Daniel Lezcano | 9db095c | 2016-02-18 13:05:01 +0000 | [diff] [blame] | 107 | options->flags |= REGULATOR_OPTION; |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 108 | break; |
| 109 | case 's': |
Daniel Lezcano | 9db095c | 2016-02-18 13:05:01 +0000 | [diff] [blame] | 110 | options->flags |= SENSOR_OPTION; |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 111 | break; |
| 112 | case 'c': |
Daniel Lezcano | 9db095c | 2016-02-18 13:05:01 +0000 | [diff] [blame] | 113 | options->flags |= CLOCK_OPTION; |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 114 | break; |
Daniel Lezcano | 716b23e | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 115 | case 'g': |
Daniel Lezcano | 9db095c | 2016-02-18 13:05:01 +0000 | [diff] [blame] | 116 | options->flags |= GPIO_OPTION; |
Daniel Lezcano | 716b23e | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 117 | break; |
Daniel Lezcano | 9db095c | 2016-02-18 13:05:01 +0000 | [diff] [blame] | 118 | case 'd': |
| 119 | options->flags |= DUMP_OPTION; |
| 120 | break; |
| 121 | case 'v': |
| 122 | options->flags |= VERBOSE_OPTION; |
| 123 | break; |
Amit Arora | f4fb810 | 2010-11-30 13:55:50 +0530 | [diff] [blame] | 124 | case 'p': |
Daniel Lezcano | 6e0c9c8 | 2011-03-26 22:06:07 +0100 | [diff] [blame] | 125 | options->clkname = strdup(optarg); |
| 126 | if (!options->clkname) { |
Daniel Lezcano | 9420fde | 2011-03-23 14:37:31 +0100 | [diff] [blame] | 127 | fprintf(stderr, "failed to allocate memory"); |
| 128 | return -1; |
| 129 | } |
Daniel Lezcano | 9db095c | 2016-02-18 13:05:01 +0000 | [diff] [blame] | 130 | options->flags |= (DUMP_OPTION | CLOCK_OPTION); |
Amit Arora | f4fb810 | 2010-11-30 13:55:50 +0530 | [diff] [blame] | 131 | break; |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 132 | case 't': |
Daniel Lezcano | 7f112da | 2011-03-23 14:37:32 +0100 | [diff] [blame] | 133 | options->ticktime = atoi(optarg); |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 134 | break; |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 135 | case 'V': |
| 136 | version(); |
| 137 | break; |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 138 | case '?': |
Daniel Lezcano | 316bcae | 2011-03-23 14:37:30 +0100 | [diff] [blame] | 139 | fprintf(stderr, "%s: Unknown option %c'.\n", |
Amit Kucheria | a0adae4 | 2011-01-12 10:54:23 -0600 | [diff] [blame] | 140 | argv[0], optopt); |
Amit Arora | 6e774cd | 2010-10-28 11:31:24 +0530 | [diff] [blame] | 141 | default: |
Daniel Lezcano | 316bcae | 2011-03-23 14:37:30 +0100 | [diff] [blame] | 142 | return -1; |
Amit Arora | fefe8bf | 2010-08-05 13:31:20 +0530 | [diff] [blame] | 143 | } |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 144 | } |
Amit Arora | a06a730 | 2010-12-02 15:59:37 +0530 | [diff] [blame] | 145 | |
Daniel Lezcano | 934fc09 | 2011-03-26 22:06:18 +0100 | [diff] [blame] | 146 | /* No system specified to be dump, let's default to all */ |
Daniel Lezcano | 9db095c | 2016-02-18 13:05:01 +0000 | [diff] [blame] | 147 | if (!(options->flags & DEFAULT_OPTION)) |
| 148 | options->flags |= DEFAULT_OPTION; |
Amit Arora | fefe8bf | 2010-08-05 13:31:20 +0530 | [diff] [blame] | 149 | |
Daniel Lezcano | 316bcae | 2011-03-23 14:37:30 +0100 | [diff] [blame] | 150 | return 0; |
| 151 | } |
| 152 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 153 | static int powerdebug_dump(struct powerdebug_options *options) |
Daniel Lezcano | 21c04d4 | 2011-03-26 22:06:04 +0100 | [diff] [blame] | 154 | { |
Daniel Lezcano | 9db095c | 2016-02-18 13:05:01 +0000 | [diff] [blame] | 155 | if (options->flags & REGULATOR_OPTION) |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 156 | regulator_dump(); |
Daniel Lezcano | b574671 | 2011-03-26 22:06:05 +0100 | [diff] [blame] | 157 | |
Daniel Lezcano | 9db095c | 2016-02-18 13:05:01 +0000 | [diff] [blame] | 158 | if (options->flags & CLOCK_OPTION) |
Daniel Lezcano | 597892a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 159 | clock_dump(options->clkname); |
Daniel Lezcano | b574671 | 2011-03-26 22:06:05 +0100 | [diff] [blame] | 160 | |
Daniel Lezcano | 9db095c | 2016-02-18 13:05:01 +0000 | [diff] [blame] | 161 | if (options->flags & SENSOR_OPTION) |
Daniel Lezcano | 3d0aef4 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 162 | sensor_dump(); |
Daniel Lezcano | b574671 | 2011-03-26 22:06:05 +0100 | [diff] [blame] | 163 | |
Daniel Lezcano | 9db095c | 2016-02-18 13:05:01 +0000 | [diff] [blame] | 164 | if (options->flags & GPIO_OPTION) |
Daniel Lezcano | 716b23e | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 165 | gpio_dump(); |
| 166 | |
Daniel Lezcano | 21c04d4 | 2011-03-26 22:06:04 +0100 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 170 | static int powerdebug_display(struct powerdebug_options *options) |
Daniel Lezcano | c08f1f2 | 2011-03-26 22:06:21 +0100 | [diff] [blame] | 171 | { |
Daniel Lezcano | 5e659d7 | 2016-02-19 20:22:12 +0000 | [diff] [blame] | 172 | if (display_init(options)) { |
Daniel Lezcano | c08f1f2 | 2011-03-26 22:06:21 +0100 | [diff] [blame] | 173 | printf("failed to initialize display\n"); |
| 174 | return -1; |
| 175 | } |
| 176 | |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 177 | if (mainloop(options->ticktime * 1000)) |
Daniel Lezcano | c08f1f2 | 2011-03-26 22:06:21 +0100 | [diff] [blame] | 178 | return -1; |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
Daniel Lezcano | 6e0c9c8 | 2011-03-26 22:06:07 +0100 | [diff] [blame] | 183 | static struct powerdebug_options *powerdebug_init(void) |
| 184 | { |
| 185 | struct powerdebug_options *options; |
| 186 | |
Daniel Lezcano | 188ff0f | 2016-02-18 15:40:12 +0000 | [diff] [blame] | 187 | signal(SIGWINCH, sigwinch_handler); |
| 188 | |
Daniel Lezcano | 6e0c9c8 | 2011-03-26 22:06:07 +0100 | [diff] [blame] | 189 | options = malloc(sizeof(*options)); |
| 190 | if (!options) |
| 191 | return NULL; |
| 192 | |
Daniel Lezcano | 6e0c9c8 | 2011-03-26 22:06:07 +0100 | [diff] [blame] | 193 | return options; |
| 194 | } |
| 195 | |
Daniel Lezcano | 0051f4f | 2011-03-23 14:37:34 +0100 | [diff] [blame] | 196 | int main(int argc, char **argv) |
| 197 | { |
| 198 | struct powerdebug_options *options; |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 199 | int ret; |
Daniel Lezcano | 0051f4f | 2011-03-23 14:37:34 +0100 | [diff] [blame] | 200 | |
Sanjay Singh Rawat | 9fe0c05 | 2013-02-22 17:57:18 +0530 | [diff] [blame] | 201 | #ifdef __ANDROID__ |
| 202 | if (setenv("TERM", "xterm", 1) < 0) { |
| 203 | fprintf(stderr, "setenv failure"); |
| 204 | return 1; |
| 205 | } |
| 206 | if (setenv("TERMINFO", "/system/etc/terminfo", 1) < 0) { |
| 207 | fprintf(stderr, "setenv failure"); |
| 208 | return 1; |
| 209 | } |
| 210 | #endif |
Daniel Lezcano | 6e0c9c8 | 2011-03-26 22:06:07 +0100 | [diff] [blame] | 211 | options = powerdebug_init(); |
Daniel Lezcano | 0051f4f | 2011-03-23 14:37:34 +0100 | [diff] [blame] | 212 | if (!options) { |
Daniel Lezcano | 6e0c9c8 | 2011-03-26 22:06:07 +0100 | [diff] [blame] | 213 | fprintf(stderr, "not enough memory to allocate options\n"); |
Daniel Lezcano | 0051f4f | 2011-03-23 14:37:34 +0100 | [diff] [blame] | 214 | return 1; |
| 215 | } |
| 216 | |
Daniel Lezcano | c08f1f2 | 2011-03-26 22:06:21 +0100 | [diff] [blame] | 217 | if (getoptions(argc, argv, options)) { |
| 218 | usage(); |
| 219 | return 1; |
| 220 | } |
| 221 | |
Daniel Lezcano | db14580 | 2011-06-21 00:57:08 +0200 | [diff] [blame] | 222 | if (mainloop_init()) { |
| 223 | fprintf(stderr, "failed to initialize the mainloop\n"); |
| 224 | return 1; |
| 225 | } |
| 226 | |
Daniel Lezcano | b4eec7e | 2016-02-18 16:44:55 +0000 | [diff] [blame] | 227 | if (regulator_init(options)) { |
Sanjay Singh Rawat | 5fef005 | 2013-04-17 15:02:01 +0530 | [diff] [blame] | 228 | printf("failed to initialize regulator\n"); |
Daniel Lezcano | 9db095c | 2016-02-18 13:05:01 +0000 | [diff] [blame] | 229 | options->flags &= ~REGULATOR_OPTION; |
Daniel Lezcano | 4aab2fe | 2011-03-26 22:05:53 +0100 | [diff] [blame] | 230 | } |
Daniel Lezcano | 0051f4f | 2011-03-23 14:37:34 +0100 | [diff] [blame] | 231 | |
Daniel Lezcano | b4eec7e | 2016-02-18 16:44:55 +0000 | [diff] [blame] | 232 | if (clock_init(options)) { |
Daniel Lezcano | f0e0665 | 2011-03-26 22:06:19 +0100 | [diff] [blame] | 233 | printf("failed to initialize clock details (check debugfs)\n"); |
Daniel Lezcano | 9db095c | 2016-02-18 13:05:01 +0000 | [diff] [blame] | 234 | options->flags &= ~CLOCK_OPTION; |
Daniel Lezcano | f0e0665 | 2011-03-26 22:06:19 +0100 | [diff] [blame] | 235 | } |
| 236 | |
Daniel Lezcano | b4eec7e | 2016-02-18 16:44:55 +0000 | [diff] [blame] | 237 | if (sensor_init(options)) { |
Daniel Lezcano | 3d0aef4 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 238 | printf("failed to initialize sensors\n"); |
Daniel Lezcano | 9db095c | 2016-02-18 13:05:01 +0000 | [diff] [blame] | 239 | options->flags &= SENSOR_OPTION; |
Daniel Lezcano | 3d0aef4 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 240 | } |
| 241 | |
Daniel Lezcano | b4eec7e | 2016-02-18 16:44:55 +0000 | [diff] [blame] | 242 | if (gpio_init(options)) { |
Daniel Lezcano | 716b23e | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 243 | printf("failed to initialize gpios\n"); |
Daniel Lezcano | 9db095c | 2016-02-18 13:05:01 +0000 | [diff] [blame] | 244 | options->flags &= GPIO_OPTION; |
Daniel Lezcano | 716b23e | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Daniel Lezcano | 9db095c | 2016-02-18 13:05:01 +0000 | [diff] [blame] | 247 | ret = options->flags & DUMP_OPTION ? powerdebug_dump(options) : |
Daniel Lezcano | b25be4a | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 248 | powerdebug_display(options); |
Daniel Lezcano | 21c04d4 | 2011-03-26 22:06:04 +0100 | [diff] [blame] | 249 | |
Daniel Lezcano | c08f1f2 | 2011-03-26 22:06:21 +0100 | [diff] [blame] | 250 | return ret < 0; |
Amit Arora | e9e16b0 | 2010-08-03 10:15:20 +0530 | [diff] [blame] | 251 | } |