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