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