Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (C) 2010, Linaro Limited. |
| 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 | * Daniel Lezcano <daniel.lezcano@linaro.org> (IBM Corporation) |
| 13 | * - initial API and implementation |
| 14 | *******************************************************************************/ |
| 15 | |
| 16 | #ifndef _GNU_SOURCE |
| 17 | #define _GNU_SOURCE |
| 18 | #include <stdio.h> |
| 19 | #undef _GNU_SOURCE |
| 20 | #endif |
| 21 | #include <mntent.h> |
| 22 | #include <string.h> |
| 23 | #include <stdbool.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <unistd.h> |
| 26 | #include <sys/param.h> |
| 27 | #include <sys/stat.h> |
| 28 | |
| 29 | #include "powerdebug.h" |
| 30 | #include "display.h" |
| 31 | #include "tree.h" |
| 32 | #include "utils.h" |
| 33 | |
| 34 | #define SYSFS_GPIO "/sys/class/gpio" |
| 35 | |
Shaojie Sun | 564a1fe | 2013-07-25 19:02:08 +0800 | [diff] [blame] | 36 | #define MAX_VALUE_BYTE 10 |
| 37 | |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 38 | struct gpio_info { |
| 39 | bool expanded; |
| 40 | int active_low; |
| 41 | int value; |
Shaojie Sun | 564a1fe | 2013-07-25 19:02:08 +0800 | [diff] [blame] | 42 | char direction[MAX_VALUE_BYTE]; |
| 43 | char edge[MAX_VALUE_BYTE]; |
Daniel Lezcano | 5e26728 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 44 | char *prefix; |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 45 | } *gpios_info; |
| 46 | |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 47 | static struct tree *gpio_tree = NULL; |
| 48 | |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 49 | static struct gpio_info *gpio_alloc(void) |
| 50 | { |
| 51 | struct gpio_info *gi; |
| 52 | |
| 53 | gi = malloc(sizeof(*gi)); |
Daniel Lezcano | 5e26728 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 54 | if (gi) { |
| 55 | memset(gi, -1, sizeof(*gi)); |
Shaojie Sun | 564a1fe | 2013-07-25 19:02:08 +0800 | [diff] [blame] | 56 | memset(gi->direction, 0, MAX_VALUE_BYTE); |
| 57 | memset(gi->edge, 0, MAX_VALUE_BYTE); |
Daniel Lezcano | 5e26728 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 58 | gi->prefix = NULL; |
| 59 | } |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 60 | |
| 61 | return gi; |
| 62 | } |
| 63 | |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 64 | static int gpio_filter_cb(const char *name) |
| 65 | { |
| 66 | /* let's ignore some directories in order to avoid to be |
| 67 | * pulled inside the sysfs circular symlinks mess/hell |
| 68 | * (choose the word which fit better) |
| 69 | */ |
| 70 | if (!strcmp(name, "device")) |
| 71 | return 1; |
| 72 | |
| 73 | if (!strcmp(name, "subsystem")) |
| 74 | return 1; |
| 75 | |
| 76 | if (!strcmp(name, "driver")) |
| 77 | return 1; |
| 78 | |
| 79 | /* we want to ignore the gpio chips */ |
| 80 | if (strstr(name, "chip")) |
| 81 | return 1; |
| 82 | |
| 83 | /* we are not interested by the power value */ |
| 84 | if (!strcmp(name, "power")) |
| 85 | return 1; |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 90 | static inline int read_gpio_cb(struct tree *t, void *data) |
| 91 | { |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 92 | struct gpio_info *gpio = t->private; |
| 93 | |
| 94 | file_read_value(t->path, "active_low", "%d", &gpio->active_low); |
| 95 | file_read_value(t->path, "value", "%d", &gpio->value); |
Shaojie Sun | 564a1fe | 2013-07-25 19:02:08 +0800 | [diff] [blame] | 96 | file_read_value(t->path, "edge", "%8s", &gpio->edge); |
| 97 | file_read_value(t->path, "direction", "%4s", &gpio->direction); |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 98 | |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static int read_gpio_info(struct tree *tree) |
| 103 | { |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 104 | return tree_for_each(tree, read_gpio_cb, NULL); |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static int fill_gpio_cb(struct tree *t, void *data) |
| 108 | { |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 109 | struct gpio_info *gpio; |
| 110 | |
| 111 | gpio = gpio_alloc(); |
| 112 | if (!gpio) |
| 113 | return -1; |
| 114 | t->private = gpio; |
| 115 | |
| 116 | /* we skip the root node but we set it expanded for its children */ |
| 117 | if (!t->parent) { |
| 118 | gpio->expanded = true; |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | return read_gpio_cb(t, data); |
| 123 | |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static int fill_gpio_tree(void) |
| 127 | { |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 128 | return tree_for_each(gpio_tree, fill_gpio_cb, NULL); |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Daniel Lezcano | 5e26728 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 131 | static int dump_gpio_cb(struct tree *t, void *data) |
| 132 | { |
| 133 | struct gpio_info *gpio = t->private; |
| 134 | struct gpio_info *pgpio; |
| 135 | |
| 136 | if (!t->parent) { |
| 137 | printf("/\n"); |
| 138 | gpio->prefix = ""; |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | pgpio = t->parent->private; |
| 143 | |
| 144 | if (!gpio->prefix) |
| 145 | if (asprintf(&gpio->prefix, "%s%s%s", pgpio->prefix, |
| 146 | t->depth > 1 ? " ": "", t->next ? "|" : " ") < 0) |
| 147 | return -1; |
| 148 | |
Daniel Lezcano | 960b4e9 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 149 | printf("%s%s-- %s (", gpio->prefix, !t->next ? "`" : "", t->name); |
| 150 | |
| 151 | if (gpio->active_low != -1) |
| 152 | printf(" active_low:%d", gpio->active_low); |
| 153 | |
| 154 | if (gpio->value != -1) |
| 155 | printf(", value:%d", gpio->value); |
| 156 | |
Shaojie Sun | 564a1fe | 2013-07-25 19:02:08 +0800 | [diff] [blame] | 157 | if (gpio->edge[0] != 0) |
| 158 | printf(", edge:%s", gpio->edge); |
Daniel Lezcano | 960b4e9 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 159 | |
Shaojie Sun | 564a1fe | 2013-07-25 19:02:08 +0800 | [diff] [blame] | 160 | if (gpio->direction[0] != 0) |
| 161 | printf(", direction:%s", gpio->direction); |
Daniel Lezcano | 960b4e9 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 162 | |
| 163 | printf(" )\n"); |
Daniel Lezcano | 5e26728 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | int dump_gpio_info(void) |
| 169 | { |
| 170 | return tree_for_each(gpio_tree, dump_gpio_cb, NULL); |
| 171 | } |
| 172 | |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 173 | int gpio_dump(void) |
| 174 | { |
Daniel Lezcano | 5e26728 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 175 | int ret; |
| 176 | |
| 177 | printf("\nGpio Tree :\n"); |
| 178 | printf("***********\n"); |
| 179 | ret = dump_gpio_info(); |
| 180 | printf("\n\n"); |
| 181 | |
| 182 | return ret; |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Daniel Lezcano | 960b4e9 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 185 | static char *gpio_line(struct tree *t) |
| 186 | { |
| 187 | struct gpio_info *gpio = t->private; |
| 188 | char *gpioline; |
| 189 | |
Shaojie Sun | 564a1fe | 2013-07-25 19:02:08 +0800 | [diff] [blame] | 190 | if (asprintf(&gpioline, "%-20s %-10d %-10d %-10s %-10s", t->name, |
Daniel Lezcano | 960b4e9 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 191 | gpio->value, gpio->active_low, gpio->edge, gpio->direction) < 0) |
| 192 | return NULL; |
| 193 | |
| 194 | return gpioline; |
| 195 | } |
| 196 | |
| 197 | static int _gpio_print_info_cb(struct tree *t, void *data) |
| 198 | { |
| 199 | int *line = data; |
| 200 | char *buffer; |
| 201 | |
| 202 | /* we skip the root node of the tree */ |
| 203 | if (!t->parent) |
| 204 | return 0; |
| 205 | |
| 206 | buffer = gpio_line(t); |
| 207 | if (!buffer) |
| 208 | return -1; |
| 209 | |
| 210 | display_print_line(GPIO, *line, buffer, 0, t); |
| 211 | |
| 212 | (*line)++; |
| 213 | |
| 214 | free(buffer); |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static int gpio_print_info_cb(struct tree *t, void *data) |
| 220 | { |
| 221 | /* we skip the root node of the tree */ |
| 222 | if (!t->parent) |
| 223 | return 0; |
| 224 | |
| 225 | return _gpio_print_info_cb(t, data); |
| 226 | } |
| 227 | |
| 228 | static int gpio_print_header(void) |
| 229 | { |
| 230 | char *buf; |
| 231 | int ret; |
| 232 | |
| 233 | if (asprintf(&buf, "%-20s %-10s %-10s %-10s %-10s", |
| 234 | "Name", "Value", "Active_low", "Edge", "Direction") < 0) |
| 235 | return -1; |
| 236 | |
| 237 | ret = display_column_name(buf); |
| 238 | |
| 239 | free(buf); |
| 240 | |
| 241 | return ret; |
| 242 | } |
| 243 | |
| 244 | static int gpio_print_info(struct tree *tree) |
| 245 | { |
| 246 | int ret, line = 0; |
| 247 | |
| 248 | display_reset_cursor(GPIO); |
| 249 | |
| 250 | gpio_print_header(); |
| 251 | |
| 252 | ret = tree_for_each(tree, gpio_print_info_cb, &line); |
| 253 | |
| 254 | display_refresh_pad(GPIO); |
| 255 | |
| 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | static int gpio_display(bool refresh) |
| 260 | { |
| 261 | if (refresh && read_gpio_info(gpio_tree)) |
| 262 | return -1; |
| 263 | |
| 264 | return gpio_print_info(gpio_tree); |
| 265 | } |
| 266 | |
Shaojie Sun | 8ac4a2e | 2013-07-29 20:11:46 +0800 | [diff] [blame] | 267 | static int gpio_change(int keyvalue) |
| 268 | { |
| 269 | struct tree *t = display_get_row_data(GPIO); |
| 270 | struct gpio_info *gpio = t->private; |
| 271 | |
| 272 | if (!t || !gpio) |
| 273 | return -1; |
| 274 | |
| 275 | switch (keyvalue) { |
| 276 | case 'D': |
| 277 | /* Only change direction when gpio interrupt not set.*/ |
| 278 | if (!strstr(gpio->edge, "none")) |
| 279 | return 0; |
| 280 | |
| 281 | if (strstr(gpio->direction, "in")) |
| 282 | strcpy(gpio->direction, "out"); |
| 283 | else if (strstr(gpio->direction, "out")) |
| 284 | strcpy(gpio->direction, "in"); |
| 285 | file_write_value(t->path, "direction", "%s", &gpio->direction); |
| 286 | file_read_value(t->path, "direction", "%s", &gpio->direction); |
| 287 | |
| 288 | break; |
| 289 | |
| 290 | case 'V': |
| 291 | /* Only change value when gpio direction is out. */ |
| 292 | if (!strstr(gpio->edge, "none") |
| 293 | || !strstr(gpio->direction, "out")) |
| 294 | return 0; |
| 295 | |
| 296 | if (gpio->value) |
| 297 | file_write_value(t->path, "direction", "%s", &"low"); |
| 298 | else |
| 299 | file_write_value(t->path, "direction", "%s", &"high"); |
| 300 | file_read_value(t->path, "value", "%d", &gpio->value); |
| 301 | |
| 302 | break; |
| 303 | |
| 304 | default: |
| 305 | return -1; |
| 306 | } |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
Daniel Lezcano | 960b4e9 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 311 | static struct display_ops gpio_ops = { |
| 312 | .display = gpio_display, |
Shaojie Sun | 8ac4a2e | 2013-07-29 20:11:46 +0800 | [diff] [blame] | 313 | .change = gpio_change, |
Daniel Lezcano | 960b4e9 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 314 | }; |
| 315 | |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 316 | /* |
| 317 | * Initialize the gpio framework |
| 318 | */ |
| 319 | int gpio_init(void) |
| 320 | { |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 321 | gpio_tree = tree_load(SYSFS_GPIO, gpio_filter_cb, false); |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 322 | if (!gpio_tree) |
| 323 | return -1; |
| 324 | |
| 325 | if (fill_gpio_tree()) |
| 326 | return -1; |
| 327 | |
| 328 | return display_register(GPIO, &gpio_ops); |
| 329 | } |