2 * Copyright (c) 2011 The Chromium OS Authors.
3 * SPDX-License-Identifier: GPL-2.0+
9 /* Flags for each GPIO */
10 #define GPIOF_OUTPUT (1 << 0) /* Currently set as an output */
11 #define GPIOF_HIGH (1 << 1) /* Currently set high */
12 #define GPIOF_RESERVED (1 << 2) /* Is in use / requested */
15 const char *label; /* label given by requester */
16 u8 flags; /* flags (GPIOF_...) */
21 * TODO: Put this into sandbox state
23 static struct gpio_state state[CONFIG_SANDBOX_GPIO_COUNT];
25 /* Access routines for GPIO state */
26 static u8 *get_gpio_flags(unsigned gp)
28 /* assert()'s could be disabled, so make sure we handle that */
29 assert(gp < ARRAY_SIZE(state));
30 if (gp >= ARRAY_SIZE(state)) {
31 static u8 invalid_flags;
32 printf("sandbox_gpio: error: invalid gpio %u\n", gp);
33 return &invalid_flags;
36 return &state[gp].flags;
39 static int get_gpio_flag(unsigned gp, int flag)
41 return (*get_gpio_flags(gp) & flag) != 0;
44 static int set_gpio_flag(unsigned gp, int flag, int value)
46 u8 *gpio = get_gpio_flags(gp);
56 static int check_reserved(unsigned gpio, const char *func)
58 if (!get_gpio_flag(gpio, GPIOF_RESERVED)) {
59 printf("sandbox_gpio: %s: error: gpio %u not reserved\n",
68 * Back-channel sandbox-internal-only access to GPIO state
71 int sandbox_gpio_get_value(unsigned gp)
73 if (get_gpio_flag(gp, GPIOF_OUTPUT))
74 debug("sandbox_gpio: get_value on output gpio %u\n", gp);
75 return get_gpio_flag(gp, GPIOF_HIGH);
78 int sandbox_gpio_set_value(unsigned gp, int value)
80 return set_gpio_flag(gp, GPIOF_HIGH, value);
83 int sandbox_gpio_get_direction(unsigned gp)
85 return get_gpio_flag(gp, GPIOF_OUTPUT);
88 int sandbox_gpio_set_direction(unsigned gp, int output)
90 return set_gpio_flag(gp, GPIOF_OUTPUT, output);
94 * These functions implement the public interface within U-Boot
97 /* set GPIO port 'gp' as an input */
98 int gpio_direction_input(unsigned gp)
100 debug("%s: gp:%u\n", __func__, gp);
102 if (check_reserved(gp, __func__))
105 return sandbox_gpio_set_direction(gp, 0);
108 /* set GPIO port 'gp' as an output, with polarity 'value' */
109 int gpio_direction_output(unsigned gp, int value)
111 debug("%s: gp:%u, value = %d\n", __func__, gp, value);
113 if (check_reserved(gp, __func__))
116 return sandbox_gpio_set_direction(gp, 1) |
117 sandbox_gpio_set_value(gp, value);
120 /* read GPIO IN value of port 'gp' */
121 int gpio_get_value(unsigned gp)
123 debug("%s: gp:%u\n", __func__, gp);
125 if (check_reserved(gp, __func__))
128 return sandbox_gpio_get_value(gp);
131 /* write GPIO OUT value to port 'gp' */
132 int gpio_set_value(unsigned gp, int value)
134 debug("%s: gp:%u, value = %d\n", __func__, gp, value);
136 if (check_reserved(gp, __func__))
139 if (!sandbox_gpio_get_direction(gp)) {
140 printf("sandbox_gpio: error: set_value on input gpio %u\n", gp);
144 return sandbox_gpio_set_value(gp, value);
147 int gpio_request(unsigned gp, const char *label)
149 debug("%s: gp:%u, label:%s\n", __func__, gp, label);
151 if (gp >= ARRAY_SIZE(state)) {
152 printf("sandbox_gpio: error: invalid gpio %u\n", gp);
156 if (get_gpio_flag(gp, GPIOF_RESERVED)) {
157 printf("sandbox_gpio: error: gpio %u already reserved\n", gp);
161 state[gp].label = label;
162 return set_gpio_flag(gp, GPIOF_RESERVED, 1);
165 int gpio_free(unsigned gp)
167 debug("%s: gp:%u\n", __func__, gp);
169 if (check_reserved(gp, __func__))
172 state[gp].label = NULL;
173 return set_gpio_flag(gp, GPIOF_RESERVED, 0);
176 /* Display GPIO information */
181 puts("Sandbox GPIOs\n");
183 for (gpio = 0; gpio < ARRAY_SIZE(state); ++gpio) {
184 const char *label = state[gpio].label;
186 printf("%4d: %s: %d [%c] %s\n",
188 sandbox_gpio_get_direction(gpio) ? "out" : " in",
189 sandbox_gpio_get_value(gpio),
190 get_gpio_flag(gpio, GPIOF_RESERVED) ? 'x' : ' ',