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 | |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 36 | struct gpio_info { |
| 37 | bool expanded; |
| 38 | int active_low; |
| 39 | int value; |
| 40 | int direction; |
| 41 | int edge; |
Daniel Lezcano | 5e26728 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 42 | char *prefix; |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 43 | } *gpios_info; |
| 44 | |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 45 | static struct tree *gpio_tree = NULL; |
| 46 | |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 47 | static struct gpio_info *gpio_alloc(void) |
| 48 | { |
| 49 | struct gpio_info *gi; |
| 50 | |
| 51 | gi = malloc(sizeof(*gi)); |
Daniel Lezcano | 5e26728 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 52 | if (gi) { |
| 53 | memset(gi, -1, sizeof(*gi)); |
| 54 | gi->prefix = NULL; |
| 55 | } |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 56 | |
| 57 | return gi; |
| 58 | } |
| 59 | |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 60 | static int gpio_display(bool refresh) |
| 61 | { |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int gpio_select(void) |
| 66 | { |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int gpio_find(const char *name) |
| 71 | { |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int gpio_selectf(void) |
| 76 | { |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static struct display_ops gpio_ops = { |
| 81 | .display = gpio_display, |
| 82 | .select = gpio_select, |
| 83 | .find = gpio_find, |
| 84 | .selectf = gpio_selectf, |
| 85 | }; |
| 86 | |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 87 | static int gpio_filter_cb(const char *name) |
| 88 | { |
| 89 | /* let's ignore some directories in order to avoid to be |
| 90 | * pulled inside the sysfs circular symlinks mess/hell |
| 91 | * (choose the word which fit better) |
| 92 | */ |
| 93 | if (!strcmp(name, "device")) |
| 94 | return 1; |
| 95 | |
| 96 | if (!strcmp(name, "subsystem")) |
| 97 | return 1; |
| 98 | |
| 99 | if (!strcmp(name, "driver")) |
| 100 | return 1; |
| 101 | |
| 102 | /* we want to ignore the gpio chips */ |
| 103 | if (strstr(name, "chip")) |
| 104 | return 1; |
| 105 | |
| 106 | /* we are not interested by the power value */ |
| 107 | if (!strcmp(name, "power")) |
| 108 | return 1; |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 113 | static inline int read_gpio_cb(struct tree *t, void *data) |
| 114 | { |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 115 | struct gpio_info *gpio = t->private; |
| 116 | |
| 117 | file_read_value(t->path, "active_low", "%d", &gpio->active_low); |
| 118 | file_read_value(t->path, "value", "%d", &gpio->value); |
| 119 | file_read_value(t->path, "edge", "%d", &gpio->edge); |
| 120 | file_read_value(t->path, "direction", "%d", &gpio->direction); |
| 121 | |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static int read_gpio_info(struct tree *tree) |
| 126 | { |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 127 | return tree_for_each(tree, read_gpio_cb, NULL); |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static int fill_gpio_cb(struct tree *t, void *data) |
| 131 | { |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 132 | struct gpio_info *gpio; |
| 133 | |
| 134 | gpio = gpio_alloc(); |
| 135 | if (!gpio) |
| 136 | return -1; |
| 137 | t->private = gpio; |
| 138 | |
| 139 | /* we skip the root node but we set it expanded for its children */ |
| 140 | if (!t->parent) { |
| 141 | gpio->expanded = true; |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | return read_gpio_cb(t, data); |
| 146 | |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static int fill_gpio_tree(void) |
| 150 | { |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 151 | return tree_for_each(gpio_tree, fill_gpio_cb, NULL); |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Daniel Lezcano | 5e26728 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 154 | static int dump_gpio_cb(struct tree *t, void *data) |
| 155 | { |
| 156 | struct gpio_info *gpio = t->private; |
| 157 | struct gpio_info *pgpio; |
| 158 | |
| 159 | if (!t->parent) { |
| 160 | printf("/\n"); |
| 161 | gpio->prefix = ""; |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | pgpio = t->parent->private; |
| 166 | |
| 167 | if (!gpio->prefix) |
| 168 | if (asprintf(&gpio->prefix, "%s%s%s", pgpio->prefix, |
| 169 | t->depth > 1 ? " ": "", t->next ? "|" : " ") < 0) |
| 170 | return -1; |
| 171 | |
| 172 | printf("%s%s-- %s (active_low:%d)\n", |
| 173 | gpio->prefix, !t->next ? "`" : "", t->name, gpio->active_low); |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | int dump_gpio_info(void) |
| 179 | { |
| 180 | return tree_for_each(gpio_tree, dump_gpio_cb, NULL); |
| 181 | } |
| 182 | |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 183 | int gpio_dump(void) |
| 184 | { |
Daniel Lezcano | 5e26728 | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 185 | int ret; |
| 186 | |
| 187 | printf("\nGpio Tree :\n"); |
| 188 | printf("***********\n"); |
| 189 | ret = dump_gpio_info(); |
| 190 | printf("\n\n"); |
| 191 | |
| 192 | return ret; |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /* |
| 196 | * Initialize the gpio framework |
| 197 | */ |
| 198 | int gpio_init(void) |
| 199 | { |
Daniel Lezcano | 9d8475b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 200 | gpio_tree = tree_load(SYSFS_GPIO, gpio_filter_cb, false); |
Daniel Lezcano | 03fc66b | 2011-08-25 15:46:13 +0200 | [diff] [blame] | 201 | if (!gpio_tree) |
| 202 | return -1; |
| 203 | |
| 204 | if (fill_gpio_tree()) |
| 205 | return -1; |
| 206 | |
| 207 | return display_register(GPIO, &gpio_ops); |
| 208 | } |