2 * Copyright (c) 2011 The Chromium OS Authors.
3 * See file CREDITS for list of people who contributed to this
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
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.
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., 59 Temple Place, Suite 330, Boston,
25 /* Flags for each GPIO */
26 #define GPIOF_OUTPUT (1 << 0) /* Currently set as an output */
27 #define GPIOF_HIGH (1 << 1) /* Currently set high */
28 #define GPIOF_RESERVED (1 << 2) /* Is in use / requested */
31 const char *label; /* label given by requester */
32 u8 flags; /* flags (GPIOF_...) */
37 * TODO: Put this into sandbox state
39 static struct gpio_state state[CONFIG_SANDBOX_GPIO_COUNT];
41 /* Access routines for GPIO state */
42 static u8 *get_gpio_flags(unsigned gp)
44 /* assert()'s could be disabled, so make sure we handle that */
45 assert(gp < ARRAY_SIZE(state));
46 if (gp >= ARRAY_SIZE(state)) {
47 static u8 invalid_flags;
48 printf("sandbox_gpio: error: invalid gpio %u\n", gp);
49 return &invalid_flags;
52 return &state[gp].flags;
55 static int get_gpio_flag(unsigned gp, int flag)
57 return (*get_gpio_flags(gp) & flag) != 0;
60 static int set_gpio_flag(unsigned gp, int flag, int value)
62 u8 *gpio = get_gpio_flags(gp);
72 static int check_reserved(unsigned gpio, const char *func)
74 if (!get_gpio_flag(gpio, GPIOF_RESERVED)) {
75 printf("sandbox_gpio: %s: error: gpio %u not reserved\n",
84 * Back-channel sandbox-internal-only access to GPIO state
87 int sandbox_gpio_get_value(unsigned gp)
89 if (get_gpio_flag(gp, GPIOF_OUTPUT))
90 debug("sandbox_gpio: get_value on output gpio %u\n", gp);
91 return get_gpio_flag(gp, GPIOF_HIGH);
94 int sandbox_gpio_set_value(unsigned gp, int value)
96 return set_gpio_flag(gp, GPIOF_HIGH, value);
99 int sandbox_gpio_get_direction(unsigned gp)
101 return get_gpio_flag(gp, GPIOF_OUTPUT);
104 int sandbox_gpio_set_direction(unsigned gp, int output)
106 return set_gpio_flag(gp, GPIOF_OUTPUT, output);
110 * These functions implement the public interface within U-Boot
113 /* set GPIO port 'gp' as an input */
114 int gpio_direction_input(unsigned gp)
116 debug("%s: gp:%u\n", __func__, gp);
118 if (check_reserved(gp, __func__))
121 return sandbox_gpio_set_direction(gp, 0);
124 /* set GPIO port 'gp' as an output, with polarity 'value' */
125 int gpio_direction_output(unsigned gp, int value)
127 debug("%s: gp:%u, value = %d\n", __func__, gp, value);
129 if (check_reserved(gp, __func__))
132 return sandbox_gpio_set_direction(gp, 1) |
133 sandbox_gpio_set_value(gp, value);
136 /* read GPIO IN value of port 'gp' */
137 int gpio_get_value(unsigned gp)
139 debug("%s: gp:%u\n", __func__, gp);
141 if (check_reserved(gp, __func__))
144 return sandbox_gpio_get_value(gp);
147 /* write GPIO OUT value to port 'gp' */
148 int gpio_set_value(unsigned gp, int value)
150 debug("%s: gp:%u, value = %d\n", __func__, gp, value);
152 if (check_reserved(gp, __func__))
155 if (!sandbox_gpio_get_direction(gp)) {
156 printf("sandbox_gpio: error: set_value on input gpio %u\n", gp);
160 return sandbox_gpio_set_value(gp, value);
163 int gpio_request(unsigned gp, const char *label)
165 debug("%s: gp:%u, label:%s\n", __func__, gp, label);
167 if (gp >= ARRAY_SIZE(state)) {
168 printf("sandbox_gpio: error: invalid gpio %u\n", gp);
172 if (get_gpio_flag(gp, GPIOF_RESERVED)) {
173 printf("sandbox_gpio: error: gpio %u already reserved\n", gp);
177 state[gp].label = label;
178 return set_gpio_flag(gp, GPIOF_RESERVED, 1);
181 int gpio_free(unsigned gp)
183 debug("%s: gp:%u\n", __func__, gp);
185 if (check_reserved(gp, __func__))
188 state[gp].label = NULL;
189 return set_gpio_flag(gp, GPIOF_RESERVED, 0);
192 /* Display GPIO information */
197 puts("Sandbox GPIOs\n");
199 for (gpio = 0; gpio < ARRAY_SIZE(state); ++gpio) {
200 const char *label = state[gpio].label;
202 printf("%4d: %s: %d [%c] %s\n",
204 sandbox_gpio_get_direction(gpio) ? "out" : " in",
205 sandbox_gpio_get_value(gpio),
206 get_gpio_flag(gpio, GPIOF_RESERVED) ? 'x' : ' ',