Daniel Lezcano | 7c15fad | 2016-02-18 15:57:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Power debug tool (powerdebug) |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 3 | * |
Daniel Lezcano | 7c15fad | 2016-02-18 15:57:43 +0000 | [diff] [blame] | 4 | * Copyright (C) 2016, Linaro Limited. |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [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. |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [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 | */ |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 21 | |
| 22 | #ifndef _GNU_SOURCE |
| 23 | #define _GNU_SOURCE |
| 24 | #include <stdio.h> |
| 25 | #undef _GNU_SOURCE |
| 26 | #endif |
| 27 | #include <mntent.h> |
| 28 | #include <string.h> |
| 29 | #include <stdbool.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <unistd.h> |
| 32 | #include <sys/param.h> |
| 33 | #include <sys/stat.h> |
| 34 | |
| 35 | #include "powerdebug.h" |
| 36 | #include "display.h" |
| 37 | #include "tree.h" |
| 38 | #include "utils.h" |
| 39 | |
| 40 | #define SYSFS_GPIO "/sys/class/gpio" |
| 41 | |
Shaojie Sun | 564a1fe | 2013-07-25 19:02:08 +0800 | [diff] [blame] | 42 | #define MAX_VALUE_BYTE 10 |
| 43 | |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 44 | struct gpio_info { |
| 45 | bool expanded; |
| 46 | int active_low; |
| 47 | int value; |
Shaojie Sun | 564a1fe | 2013-07-25 19:02:08 +0800 | [diff] [blame] | 48 | char direction[MAX_VALUE_BYTE]; |
| 49 | char edge[MAX_VALUE_BYTE]; |
Daniel Lezcano | 5e26728 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 50 | char *prefix; |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 51 | } *gpios_info; |
| 52 | |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 53 | static struct tree *gpio_tree = NULL; |
Sanjay Singh Rawat | 96f6e05 | 2014-05-26 11:35:02 +0530 | [diff] [blame] | 54 | static bool gpio_error = false; |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 55 | |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 56 | static struct gpio_info *gpio_alloc(void) |
| 57 | { |
| 58 | struct gpio_info *gi; |
| 59 | |
| 60 | gi = malloc(sizeof(*gi)); |
Daniel Lezcano | 5e26728 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 61 | if (gi) { |
| 62 | memset(gi, -1, sizeof(*gi)); |
Shaojie Sun | 564a1fe | 2013-07-25 19:02:08 +0800 | [diff] [blame] | 63 | memset(gi->direction, 0, MAX_VALUE_BYTE); |
| 64 | memset(gi->edge, 0, MAX_VALUE_BYTE); |
Daniel Lezcano | 5e26728 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 65 | gi->prefix = NULL; |
| 66 | } |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 67 | |
| 68 | return gi; |
| 69 | } |
| 70 | |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 71 | static int gpio_filter_cb(const char *name) |
| 72 | { |
| 73 | /* let's ignore some directories in order to avoid to be |
| 74 | * pulled inside the sysfs circular symlinks mess/hell |
| 75 | * (choose the word which fit better) |
| 76 | */ |
| 77 | if (!strcmp(name, "device")) |
| 78 | return 1; |
| 79 | |
| 80 | if (!strcmp(name, "subsystem")) |
| 81 | return 1; |
| 82 | |
| 83 | if (!strcmp(name, "driver")) |
| 84 | return 1; |
| 85 | |
| 86 | /* we want to ignore the gpio chips */ |
| 87 | if (strstr(name, "chip")) |
| 88 | return 1; |
| 89 | |
| 90 | /* we are not interested by the power value */ |
| 91 | if (!strcmp(name, "power")) |
| 92 | return 1; |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 97 | static inline int read_gpio_cb(struct tree *t, void *data) |
| 98 | { |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 99 | struct gpio_info *gpio = t->private; |
| 100 | |
| 101 | file_read_value(t->path, "active_low", "%d", &gpio->active_low); |
| 102 | file_read_value(t->path, "value", "%d", &gpio->value); |
Shaojie Sun | 564a1fe | 2013-07-25 19:02:08 +0800 | [diff] [blame] | 103 | file_read_value(t->path, "edge", "%8s", &gpio->edge); |
| 104 | file_read_value(t->path, "direction", "%4s", &gpio->direction); |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 105 | |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static int read_gpio_info(struct tree *tree) |
| 110 | { |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 111 | return tree_for_each(tree, read_gpio_cb, NULL); |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static int fill_gpio_cb(struct tree *t, void *data) |
| 115 | { |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 116 | struct gpio_info *gpio; |
| 117 | |
| 118 | gpio = gpio_alloc(); |
| 119 | if (!gpio) |
| 120 | return -1; |
| 121 | t->private = gpio; |
| 122 | |
| 123 | /* we skip the root node but we set it expanded for its children */ |
| 124 | if (!t->parent) { |
| 125 | gpio->expanded = true; |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | return read_gpio_cb(t, data); |
| 130 | |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static int fill_gpio_tree(void) |
| 134 | { |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 135 | return tree_for_each(gpio_tree, fill_gpio_cb, NULL); |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Daniel Lezcano | 5e26728 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 138 | static int dump_gpio_cb(struct tree *t, void *data) |
| 139 | { |
| 140 | struct gpio_info *gpio = t->private; |
| 141 | struct gpio_info *pgpio; |
| 142 | |
| 143 | if (!t->parent) { |
| 144 | printf("/\n"); |
| 145 | gpio->prefix = ""; |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | pgpio = t->parent->private; |
| 150 | |
| 151 | if (!gpio->prefix) |
| 152 | if (asprintf(&gpio->prefix, "%s%s%s", pgpio->prefix, |
| 153 | t->depth > 1 ? " ": "", t->next ? "|" : " ") < 0) |
| 154 | return -1; |
| 155 | |
Daniel Lezcano | 960b4e9 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 156 | printf("%s%s-- %s (", gpio->prefix, !t->next ? "`" : "", t->name); |
| 157 | |
| 158 | if (gpio->active_low != -1) |
| 159 | printf(" active_low:%d", gpio->active_low); |
| 160 | |
| 161 | if (gpio->value != -1) |
| 162 | printf(", value:%d", gpio->value); |
| 163 | |
Shaojie Sun | 564a1fe | 2013-07-25 19:02:08 +0800 | [diff] [blame] | 164 | if (gpio->edge[0] != 0) |
| 165 | printf(", edge:%s", gpio->edge); |
Daniel Lezcano | 960b4e9 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 166 | |
Shaojie Sun | 564a1fe | 2013-07-25 19:02:08 +0800 | [diff] [blame] | 167 | if (gpio->direction[0] != 0) |
| 168 | printf(", direction:%s", gpio->direction); |
Daniel Lezcano | 960b4e9 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 169 | |
| 170 | printf(" )\n"); |
Daniel Lezcano | 5e26728 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | int dump_gpio_info(void) |
| 176 | { |
| 177 | return tree_for_each(gpio_tree, dump_gpio_cb, NULL); |
| 178 | } |
| 179 | |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 180 | int gpio_dump(void) |
| 181 | { |
Daniel Lezcano | 5e26728 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 182 | int ret; |
| 183 | |
| 184 | printf("\nGpio Tree :\n"); |
| 185 | printf("***********\n"); |
| 186 | ret = dump_gpio_info(); |
| 187 | printf("\n\n"); |
| 188 | |
| 189 | return ret; |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Daniel Lezcano | 960b4e9 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 192 | static char *gpio_line(struct tree *t) |
| 193 | { |
| 194 | struct gpio_info *gpio = t->private; |
| 195 | char *gpioline; |
| 196 | |
Shaojie Sun | 564a1fe | 2013-07-25 19:02:08 +0800 | [diff] [blame] | 197 | if (asprintf(&gpioline, "%-20s %-10d %-10d %-10s %-10s", t->name, |
Daniel Lezcano | 960b4e9 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 198 | gpio->value, gpio->active_low, gpio->edge, gpio->direction) < 0) |
| 199 | return NULL; |
| 200 | |
| 201 | return gpioline; |
| 202 | } |
| 203 | |
| 204 | static int _gpio_print_info_cb(struct tree *t, void *data) |
| 205 | { |
| 206 | int *line = data; |
| 207 | char *buffer; |
| 208 | |
| 209 | /* we skip the root node of the tree */ |
| 210 | if (!t->parent) |
| 211 | return 0; |
| 212 | |
| 213 | buffer = gpio_line(t); |
| 214 | if (!buffer) |
| 215 | return -1; |
| 216 | |
| 217 | display_print_line(GPIO, *line, buffer, 0, t); |
| 218 | |
| 219 | (*line)++; |
| 220 | |
| 221 | free(buffer); |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static int gpio_print_info_cb(struct tree *t, void *data) |
| 227 | { |
| 228 | /* we skip the root node of the tree */ |
| 229 | if (!t->parent) |
| 230 | return 0; |
| 231 | |
| 232 | return _gpio_print_info_cb(t, data); |
| 233 | } |
| 234 | |
| 235 | static int gpio_print_header(void) |
| 236 | { |
| 237 | char *buf; |
| 238 | int ret; |
| 239 | |
| 240 | if (asprintf(&buf, "%-20s %-10s %-10s %-10s %-10s", |
| 241 | "Name", "Value", "Active_low", "Edge", "Direction") < 0) |
| 242 | return -1; |
| 243 | |
| 244 | ret = display_column_name(buf); |
| 245 | |
| 246 | free(buf); |
| 247 | |
| 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | static int gpio_print_info(struct tree *tree) |
| 252 | { |
| 253 | int ret, line = 0; |
| 254 | |
| 255 | display_reset_cursor(GPIO); |
| 256 | |
| 257 | gpio_print_header(); |
| 258 | |
| 259 | ret = tree_for_each(tree, gpio_print_info_cb, &line); |
| 260 | |
| 261 | display_refresh_pad(GPIO); |
| 262 | |
| 263 | return ret; |
| 264 | } |
| 265 | |
| 266 | static int gpio_display(bool refresh) |
| 267 | { |
Sanjay Singh Rawat | 96f6e05 | 2014-05-26 11:35:02 +0530 | [diff] [blame] | 268 | if (gpio_error) { |
| 269 | display_message(GPIO, "error: path " SYSFS_GPIO " not found"); |
| 270 | return -2; |
| 271 | } |
| 272 | |
Daniel Lezcano | 960b4e9 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 273 | if (refresh && read_gpio_info(gpio_tree)) |
| 274 | return -1; |
| 275 | |
| 276 | return gpio_print_info(gpio_tree); |
| 277 | } |
| 278 | |
Shaojie Sun | 8ac4a2e | 2013-07-29 20:11:46 +0800 | [diff] [blame] | 279 | static int gpio_change(int keyvalue) |
| 280 | { |
| 281 | struct tree *t = display_get_row_data(GPIO); |
| 282 | struct gpio_info *gpio = t->private; |
| 283 | |
| 284 | if (!t || !gpio) |
| 285 | return -1; |
| 286 | |
| 287 | switch (keyvalue) { |
| 288 | case 'D': |
| 289 | /* Only change direction when gpio interrupt not set.*/ |
| 290 | if (!strstr(gpio->edge, "none")) |
| 291 | return 0; |
| 292 | |
| 293 | if (strstr(gpio->direction, "in")) |
| 294 | strcpy(gpio->direction, "out"); |
| 295 | else if (strstr(gpio->direction, "out")) |
| 296 | strcpy(gpio->direction, "in"); |
| 297 | file_write_value(t->path, "direction", "%s", &gpio->direction); |
| 298 | file_read_value(t->path, "direction", "%s", &gpio->direction); |
Shaojie Sun | ed97318 | 2013-09-17 14:15:19 +0800 | [diff] [blame] | 299 | file_read_value(t->path, "value", "%d", &gpio->value); |
Shaojie Sun | 8ac4a2e | 2013-07-29 20:11:46 +0800 | [diff] [blame] | 300 | |
| 301 | break; |
| 302 | |
| 303 | case 'V': |
| 304 | /* Only change value when gpio direction is out. */ |
| 305 | if (!strstr(gpio->edge, "none") |
| 306 | || !strstr(gpio->direction, "out")) |
| 307 | return 0; |
| 308 | |
| 309 | if (gpio->value) |
| 310 | file_write_value(t->path, "direction", "%s", &"low"); |
| 311 | else |
| 312 | file_write_value(t->path, "direction", "%s", &"high"); |
| 313 | file_read_value(t->path, "value", "%d", &gpio->value); |
| 314 | |
| 315 | break; |
| 316 | |
| 317 | default: |
| 318 | return -1; |
| 319 | } |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
Daniel Lezcano | 960b4e9 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 324 | static struct display_ops gpio_ops = { |
| 325 | .display = gpio_display, |
Shaojie Sun | 8ac4a2e | 2013-07-29 20:11:46 +0800 | [diff] [blame] | 326 | .change = gpio_change, |
Daniel Lezcano | 960b4e9 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 327 | }; |
| 328 | |
Sanjay Singh Rawat | 75eadf3 | 2014-06-19 17:55:09 +0530 | [diff] [blame] | 329 | void export_free_gpios(void) |
Sanjay Singh Rawat | b212054 | 2014-05-16 10:58:47 +0530 | [diff] [blame] | 330 | { |
| 331 | FILE *fgpio, *fgpio_export; |
Sanjay Singh Rawat | 75eadf3 | 2014-06-19 17:55:09 +0530 | [diff] [blame] | 332 | int i, gpio_max = 0; |
Sanjay Singh Rawat | 03fdbc0 | 2014-05-27 16:52:27 +0530 | [diff] [blame] | 333 | char *line = NULL; |
Daniel Lezcano | 83b28d0 | 2016-02-18 13:49:55 +0000 | [diff] [blame] | 334 | ssize_t read; |
| 335 | size_t len = 0; |
Sanjay Singh Rawat | b212054 | 2014-05-16 10:58:47 +0530 | [diff] [blame] | 336 | |
| 337 | fgpio = fopen("/sys/kernel/debug/gpio", "r"); |
| 338 | if (!fgpio) { |
| 339 | printf("failed to read debugfs gpio file\n"); |
Daniel Lezcano | 83b28d0 | 2016-02-18 13:49:55 +0000 | [diff] [blame] | 340 | return; |
Sanjay Singh Rawat | b212054 | 2014-05-16 10:58:47 +0530 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | fgpio_export = fopen("/sys/class/gpio/export", "w"); |
| 344 | if (!fgpio_export) { |
| 345 | printf("failed to write open gpio-export file\n"); |
Sanjay Singh Rawat | b212054 | 2014-05-16 10:58:47 +0530 | [diff] [blame] | 346 | goto out; |
| 347 | } |
| 348 | |
| 349 | /* export the gpios */ |
Sanjay Singh Rawat | 75eadf3 | 2014-06-19 17:55:09 +0530 | [diff] [blame] | 350 | while ((read = getline(&line, &len, fgpio)) != -1) { |
| 351 | if (strstr(line, "GPIOs")) |
Daniel Lezcano | 83b28d0 | 2016-02-18 13:49:55 +0000 | [diff] [blame] | 352 | sscanf(line, "%*[^-]-%d", &gpio_max); |
Sanjay Singh Rawat | 75eadf3 | 2014-06-19 17:55:09 +0530 | [diff] [blame] | 353 | } |
Sanjay Singh Rawat | b212054 | 2014-05-16 10:58:47 +0530 | [diff] [blame] | 354 | |
Sanjay Singh Rawat | 75eadf3 | 2014-06-19 17:55:09 +0530 | [diff] [blame] | 355 | printf("log: total gpios = %d\n", gpio_max); |
| 356 | for (i = 0 ; i <= gpio_max ; i++) { |
| 357 | char command[50] = ""; |
| 358 | |
| 359 | sprintf(command, "echo %d > /sys/class/gpio/export", i); |
| 360 | if (system(command) < 0) |
| 361 | printf("error: failed to export gpio-%d\n", i); |
Sanjay Singh Rawat | b212054 | 2014-05-16 10:58:47 +0530 | [diff] [blame] | 362 | } |
Meraj | e33055c | 2014-06-30 16:11:45 +0530 | [diff] [blame] | 363 | |
| 364 | free(line); |
Daniel Lezcano | 83b28d0 | 2016-02-18 13:49:55 +0000 | [diff] [blame] | 365 | |
Meraj | e33055c | 2014-06-30 16:11:45 +0530 | [diff] [blame] | 366 | if (fgpio) |
| 367 | fclose(fgpio); |
Daniel Lezcano | 83b28d0 | 2016-02-18 13:49:55 +0000 | [diff] [blame] | 368 | out: |
Meraj | e33055c | 2014-06-30 16:11:45 +0530 | [diff] [blame] | 369 | if (fgpio_export) |
| 370 | fclose(fgpio_export); |
| 371 | |
Sanjay Singh Rawat | b212054 | 2014-05-16 10:58:47 +0530 | [diff] [blame] | 372 | return; |
| 373 | } |
| 374 | |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 375 | /* |
| 376 | * Initialize the gpio framework |
| 377 | */ |
| 378 | int gpio_init(void) |
| 379 | { |
Sanjay Singh Rawat | 96f6e05 | 2014-05-26 11:35:02 +0530 | [diff] [blame] | 380 | int ret = 0; |
| 381 | |
| 382 | ret = display_register(GPIO, &gpio_ops); |
Sanjay Singh Rawat | 03fdbc0 | 2014-05-27 16:52:27 +0530 | [diff] [blame] | 383 | if (ret) |
Sanjay Singh Rawat | 96f6e05 | 2014-05-26 11:35:02 +0530 | [diff] [blame] | 384 | printf("error: gpio display register failed"); |
| 385 | |
| 386 | if (access(SYSFS_GPIO, F_OK)) |
| 387 | gpio_error = true; /* set the flag */ |
| 388 | |
Sanjay Singh Rawat | 75eadf3 | 2014-06-19 17:55:09 +0530 | [diff] [blame] | 389 | export_free_gpios(); |
Sanjay Singh Rawat | b212054 | 2014-05-16 10:58:47 +0530 | [diff] [blame] | 390 | |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 391 | gpio_tree = tree_load(SYSFS_GPIO, gpio_filter_cb, false); |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 392 | if (!gpio_tree) |
| 393 | return -1; |
| 394 | |
| 395 | if (fill_gpio_tree()) |
| 396 | return -1; |
| 397 | |
Sanjay Singh Rawat | 96f6e05 | 2014-05-26 11:35:02 +0530 | [diff] [blame] | 398 | return ret; |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 399 | } |