blob: f7d2a10b40fd3fcd101ba654ee0009ea76163978 [file] [log] [blame]
Daniel Lezcano03fc66b2011-08-25 15:46:13 +02001/*******************************************************************************
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 Lezcano9d8475b2011-08-25 15:46:13 +020036struct gpio_info {
37 bool expanded;
38 int active_low;
39 int value;
40 int direction;
41 int edge;
Daniel Lezcano5e267282011-08-25 15:46:13 +020042 char *prefix;
Daniel Lezcano9d8475b2011-08-25 15:46:13 +020043} *gpios_info;
44
Daniel Lezcano03fc66b2011-08-25 15:46:13 +020045static struct tree *gpio_tree = NULL;
46
Daniel Lezcano9d8475b2011-08-25 15:46:13 +020047static struct gpio_info *gpio_alloc(void)
48{
49 struct gpio_info *gi;
50
51 gi = malloc(sizeof(*gi));
Daniel Lezcano5e267282011-08-25 15:46:13 +020052 if (gi) {
53 memset(gi, -1, sizeof(*gi));
54 gi->prefix = NULL;
55 }
Daniel Lezcano9d8475b2011-08-25 15:46:13 +020056
57 return gi;
58}
59
Daniel Lezcano9d8475b2011-08-25 15:46:13 +020060static int gpio_filter_cb(const char *name)
61{
62 /* let's ignore some directories in order to avoid to be
63 * pulled inside the sysfs circular symlinks mess/hell
64 * (choose the word which fit better)
65 */
66 if (!strcmp(name, "device"))
67 return 1;
68
69 if (!strcmp(name, "subsystem"))
70 return 1;
71
72 if (!strcmp(name, "driver"))
73 return 1;
74
75 /* we want to ignore the gpio chips */
76 if (strstr(name, "chip"))
77 return 1;
78
79 /* we are not interested by the power value */
80 if (!strcmp(name, "power"))
81 return 1;
82
83 return 0;
84}
85
Daniel Lezcano03fc66b2011-08-25 15:46:13 +020086static inline int read_gpio_cb(struct tree *t, void *data)
87{
Daniel Lezcano9d8475b2011-08-25 15:46:13 +020088 struct gpio_info *gpio = t->private;
89
90 file_read_value(t->path, "active_low", "%d", &gpio->active_low);
91 file_read_value(t->path, "value", "%d", &gpio->value);
92 file_read_value(t->path, "edge", "%d", &gpio->edge);
93 file_read_value(t->path, "direction", "%d", &gpio->direction);
94
Daniel Lezcano03fc66b2011-08-25 15:46:13 +020095 return 0;
96}
97
98static int read_gpio_info(struct tree *tree)
99{
Daniel Lezcano9d8475b2011-08-25 15:46:13 +0200100 return tree_for_each(tree, read_gpio_cb, NULL);
Daniel Lezcano03fc66b2011-08-25 15:46:13 +0200101}
102
103static int fill_gpio_cb(struct tree *t, void *data)
104{
Daniel Lezcano9d8475b2011-08-25 15:46:13 +0200105 struct gpio_info *gpio;
106
107 gpio = gpio_alloc();
108 if (!gpio)
109 return -1;
110 t->private = gpio;
111
112 /* we skip the root node but we set it expanded for its children */
113 if (!t->parent) {
114 gpio->expanded = true;
115 return 0;
116 }
117
118 return read_gpio_cb(t, data);
119
Daniel Lezcano03fc66b2011-08-25 15:46:13 +0200120}
121
122static int fill_gpio_tree(void)
123{
Daniel Lezcano9d8475b2011-08-25 15:46:13 +0200124 return tree_for_each(gpio_tree, fill_gpio_cb, NULL);
Daniel Lezcano03fc66b2011-08-25 15:46:13 +0200125}
126
Daniel Lezcano5e267282011-08-25 15:46:13 +0200127static int dump_gpio_cb(struct tree *t, void *data)
128{
129 struct gpio_info *gpio = t->private;
130 struct gpio_info *pgpio;
131
132 if (!t->parent) {
133 printf("/\n");
134 gpio->prefix = "";
135 return 0;
136 }
137
138 pgpio = t->parent->private;
139
140 if (!gpio->prefix)
141 if (asprintf(&gpio->prefix, "%s%s%s", pgpio->prefix,
142 t->depth > 1 ? " ": "", t->next ? "|" : " ") < 0)
143 return -1;
144
Daniel Lezcano960b4e92011-08-25 15:46:13 +0200145 printf("%s%s-- %s (", gpio->prefix, !t->next ? "`" : "", t->name);
146
147 if (gpio->active_low != -1)
148 printf(" active_low:%d", gpio->active_low);
149
150 if (gpio->value != -1)
151 printf(", value:%d", gpio->value);
152
153 if (gpio->edge != -1)
154 printf(", edge:%d", gpio->edge);
155
156 if (gpio->direction != -1)
157 printf(", direction:%d", gpio->direction);
158
159 printf(" )\n");
Daniel Lezcano5e267282011-08-25 15:46:13 +0200160
161 return 0;
162}
163
164int dump_gpio_info(void)
165{
166 return tree_for_each(gpio_tree, dump_gpio_cb, NULL);
167}
168
Daniel Lezcano03fc66b2011-08-25 15:46:13 +0200169int gpio_dump(void)
170{
Daniel Lezcano5e267282011-08-25 15:46:13 +0200171 int ret;
172
173 printf("\nGpio Tree :\n");
174 printf("***********\n");
175 ret = dump_gpio_info();
176 printf("\n\n");
177
178 return ret;
Daniel Lezcano03fc66b2011-08-25 15:46:13 +0200179}
180
Daniel Lezcano960b4e92011-08-25 15:46:13 +0200181static char *gpio_line(struct tree *t)
182{
183 struct gpio_info *gpio = t->private;
184 char *gpioline;
185
186 if (asprintf(&gpioline, "%-20s %-10d %-10d %-10d %-10d", t-> name,
187 gpio->value, gpio->active_low, gpio->edge, gpio->direction) < 0)
188 return NULL;
189
190 return gpioline;
191}
192
193static int _gpio_print_info_cb(struct tree *t, void *data)
194{
195 int *line = data;
196 char *buffer;
197
198 /* we skip the root node of the tree */
199 if (!t->parent)
200 return 0;
201
202 buffer = gpio_line(t);
203 if (!buffer)
204 return -1;
205
206 display_print_line(GPIO, *line, buffer, 0, t);
207
208 (*line)++;
209
210 free(buffer);
211
212 return 0;
213}
214
215static int gpio_print_info_cb(struct tree *t, void *data)
216{
217 /* we skip the root node of the tree */
218 if (!t->parent)
219 return 0;
220
221 return _gpio_print_info_cb(t, data);
222}
223
224static int gpio_print_header(void)
225{
226 char *buf;
227 int ret;
228
229 if (asprintf(&buf, "%-20s %-10s %-10s %-10s %-10s",
230 "Name", "Value", "Active_low", "Edge", "Direction") < 0)
231 return -1;
232
233 ret = display_column_name(buf);
234
235 free(buf);
236
237 return ret;
238}
239
240static int gpio_print_info(struct tree *tree)
241{
242 int ret, line = 0;
243
244 display_reset_cursor(GPIO);
245
246 gpio_print_header();
247
248 ret = tree_for_each(tree, gpio_print_info_cb, &line);
249
250 display_refresh_pad(GPIO);
251
252 return ret;
253}
254
255static int gpio_display(bool refresh)
256{
257 if (refresh && read_gpio_info(gpio_tree))
258 return -1;
259
260 return gpio_print_info(gpio_tree);
261}
262
263static struct display_ops gpio_ops = {
264 .display = gpio_display,
265};
266
Daniel Lezcano03fc66b2011-08-25 15:46:13 +0200267/*
268 * Initialize the gpio framework
269 */
270int gpio_init(void)
271{
Daniel Lezcano9d8475b2011-08-25 15:46:13 +0200272 gpio_tree = tree_load(SYSFS_GPIO, gpio_filter_cb, false);
Daniel Lezcano03fc66b2011-08-25 15:46:13 +0200273 if (!gpio_tree)
274 return -1;
275
276 if (fill_gpio_tree())
277 return -1;
278
279 return display_register(GPIO, &gpio_ops);
280}