blob: 8e7499dac3a24141e9273b988daf6188b812a3ba [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;
42} *gpios_info;
43
Daniel Lezcano03fc66b2011-08-25 15:46:13 +020044static struct tree *gpio_tree = NULL;
45
Daniel Lezcano9d8475b2011-08-25 15:46:13 +020046static struct gpio_info *gpio_alloc(void)
47{
48 struct gpio_info *gi;
49
50 gi = malloc(sizeof(*gi));
51 if (gi)
52 memset(gi, 0, sizeof(*gi));
53
54 return gi;
55}
56
Daniel Lezcano03fc66b2011-08-25 15:46:13 +020057static int gpio_display(bool refresh)
58{
59 return 0;
60}
61
62static int gpio_select(void)
63{
64 return 0;
65}
66
67static int gpio_find(const char *name)
68{
69 return 0;
70}
71
72static int gpio_selectf(void)
73{
74 return 0;
75}
76
77static struct display_ops gpio_ops = {
78 .display = gpio_display,
79 .select = gpio_select,
80 .find = gpio_find,
81 .selectf = gpio_selectf,
82};
83
Daniel Lezcano9d8475b2011-08-25 15:46:13 +020084static int gpio_filter_cb(const char *name)
85{
86 /* let's ignore some directories in order to avoid to be
87 * pulled inside the sysfs circular symlinks mess/hell
88 * (choose the word which fit better)
89 */
90 if (!strcmp(name, "device"))
91 return 1;
92
93 if (!strcmp(name, "subsystem"))
94 return 1;
95
96 if (!strcmp(name, "driver"))
97 return 1;
98
99 /* we want to ignore the gpio chips */
100 if (strstr(name, "chip"))
101 return 1;
102
103 /* we are not interested by the power value */
104 if (!strcmp(name, "power"))
105 return 1;
106
107 return 0;
108}
109
Daniel Lezcano03fc66b2011-08-25 15:46:13 +0200110static inline int read_gpio_cb(struct tree *t, void *data)
111{
Daniel Lezcano9d8475b2011-08-25 15:46:13 +0200112 struct gpio_info *gpio = t->private;
113
114 file_read_value(t->path, "active_low", "%d", &gpio->active_low);
115 file_read_value(t->path, "value", "%d", &gpio->value);
116 file_read_value(t->path, "edge", "%d", &gpio->edge);
117 file_read_value(t->path, "direction", "%d", &gpio->direction);
118
Daniel Lezcano03fc66b2011-08-25 15:46:13 +0200119 return 0;
120}
121
122static int read_gpio_info(struct tree *tree)
123{
Daniel Lezcano9d8475b2011-08-25 15:46:13 +0200124 return tree_for_each(tree, read_gpio_cb, NULL);
Daniel Lezcano03fc66b2011-08-25 15:46:13 +0200125}
126
127static int fill_gpio_cb(struct tree *t, void *data)
128{
Daniel Lezcano9d8475b2011-08-25 15:46:13 +0200129 struct gpio_info *gpio;
130
131 gpio = gpio_alloc();
132 if (!gpio)
133 return -1;
134 t->private = gpio;
135
136 /* we skip the root node but we set it expanded for its children */
137 if (!t->parent) {
138 gpio->expanded = true;
139 return 0;
140 }
141
142 return read_gpio_cb(t, data);
143
Daniel Lezcano03fc66b2011-08-25 15:46:13 +0200144}
145
146static int fill_gpio_tree(void)
147{
Daniel Lezcano9d8475b2011-08-25 15:46:13 +0200148 return tree_for_each(gpio_tree, fill_gpio_cb, NULL);
Daniel Lezcano03fc66b2011-08-25 15:46:13 +0200149}
150
151int gpio_dump(void)
152{
153 return 0;
154}
155
156/*
157 * Initialize the gpio framework
158 */
159int gpio_init(void)
160{
Daniel Lezcano9d8475b2011-08-25 15:46:13 +0200161 gpio_tree = tree_load(SYSFS_GPIO, gpio_filter_cb, false);
Daniel Lezcano03fc66b2011-08-25 15:46:13 +0200162 if (!gpio_tree)
163 return -1;
164
165 if (fill_gpio_tree())
166 return -1;
167
168 return display_register(GPIO, &gpio_ops);
169}