2 * Copyright (c) 2011 The Chromium OS Authors.
3 * SPDX-License-Identifier: GPL-2.0+
12 DECLARE_GLOBAL_DATA_PTR;
14 /* Flags for each GPIO */
15 #define GPIOF_OUTPUT (1 << 0) /* Currently set as an output */
16 #define GPIOF_HIGH (1 << 1) /* Currently set high */
17 #define GPIOF_RESERVED (1 << 2) /* Is in use / requested */
20 const char *label; /* label given by requester */
21 u8 flags; /* flags (GPIOF_...) */
24 /* Access routines for GPIO state */
25 static u8 *get_gpio_flags(struct udevice *dev, unsigned offset)
27 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
28 struct gpio_state *state = dev_get_priv(dev);
30 if (offset >= uc_priv->gpio_count) {
31 static u8 invalid_flags;
32 printf("sandbox_gpio: error: invalid gpio %u\n", offset);
33 return &invalid_flags;
36 return &state[offset].flags;
39 static int get_gpio_flag(struct udevice *dev, unsigned offset, int flag)
41 return (*get_gpio_flags(dev, offset) & flag) != 0;
44 static int set_gpio_flag(struct udevice *dev, unsigned offset, int flag,
47 u8 *gpio = get_gpio_flags(dev, offset);
57 static int check_reserved(struct udevice *dev, unsigned offset,
60 if (!get_gpio_flag(dev, offset, GPIOF_RESERVED)) {
61 printf("sandbox_gpio: %s: error: offset %u not reserved\n",
70 * Back-channel sandbox-internal-only access to GPIO state
73 int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
75 if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
76 debug("sandbox_gpio: get_value on output gpio %u\n", offset);
77 return get_gpio_flag(dev, offset, GPIOF_HIGH);
80 int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
82 return set_gpio_flag(dev, offset, GPIOF_HIGH, value);
85 int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset)
87 return get_gpio_flag(dev, offset, GPIOF_OUTPUT);
90 int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
92 return set_gpio_flag(dev, offset, GPIOF_OUTPUT, output);
96 * These functions implement the public interface within U-Boot
99 /* set GPIO port 'offset' as an input */
100 static int sb_gpio_direction_input(struct udevice *dev, unsigned offset)
102 debug("%s: offset:%u\n", __func__, offset);
104 if (check_reserved(dev, offset, __func__))
107 return sandbox_gpio_set_direction(dev, offset, 0);
110 /* set GPIO port 'offset' as an output, with polarity 'value' */
111 static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
114 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
116 if (check_reserved(dev, offset, __func__))
119 return sandbox_gpio_set_direction(dev, offset, 1) |
120 sandbox_gpio_set_value(dev, offset, value);
123 /* read GPIO IN value of port 'offset' */
124 static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
126 debug("%s: offset:%u\n", __func__, offset);
128 if (check_reserved(dev, offset, __func__))
131 return sandbox_gpio_get_value(dev, offset);
134 /* write GPIO OUT value to port 'offset' */
135 static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
137 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
139 if (check_reserved(dev, offset, __func__))
142 if (!sandbox_gpio_get_direction(dev, offset)) {
143 printf("sandbox_gpio: error: set_value on input gpio %u\n",
148 return sandbox_gpio_set_value(dev, offset, value);
151 static int sb_gpio_request(struct udevice *dev, unsigned offset,
154 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
155 struct gpio_state *state = dev_get_priv(dev);
157 debug("%s: offset:%u, label:%s\n", __func__, offset, label);
159 if (offset >= uc_priv->gpio_count) {
160 printf("sandbox_gpio: error: invalid gpio %u\n", offset);
164 if (get_gpio_flag(dev, offset, GPIOF_RESERVED)) {
165 printf("sandbox_gpio: error: gpio %u already reserved\n",
170 state[offset].label = label;
171 return set_gpio_flag(dev, offset, GPIOF_RESERVED, 1);
174 static int sb_gpio_free(struct udevice *dev, unsigned offset)
176 struct gpio_state *state = dev_get_priv(dev);
178 debug("%s: offset:%u\n", __func__, offset);
180 if (check_reserved(dev, offset, __func__))
183 state[offset].label = NULL;
184 return set_gpio_flag(dev, offset, GPIOF_RESERVED, 0);
187 static int sb_gpio_get_state(struct udevice *dev, unsigned int offset,
188 char *buf, int bufsize)
190 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
191 struct gpio_state *state = dev_get_priv(dev);
194 label = state[offset].label;
195 snprintf(buf, bufsize, "%s%d: %s: %d [%c]%s%s",
196 uc_priv->bank_name ? uc_priv->bank_name : "", offset,
197 sandbox_gpio_get_direction(dev, offset) ? "out" : " in",
198 sandbox_gpio_get_value(dev, offset),
199 get_gpio_flag(dev, offset, GPIOF_RESERVED) ? 'x' : ' ',
206 static const struct dm_gpio_ops gpio_sandbox_ops = {
207 .request = sb_gpio_request,
208 .free = sb_gpio_free,
209 .direction_input = sb_gpio_direction_input,
210 .direction_output = sb_gpio_direction_output,
211 .get_value = sb_gpio_get_value,
212 .set_value = sb_gpio_set_value,
213 .get_state = sb_gpio_get_state,
216 static int sandbox_gpio_ofdata_to_platdata(struct udevice *dev)
218 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
220 uc_priv->gpio_count = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
222 uc_priv->bank_name = fdt_getprop(gd->fdt_blob, dev->of_offset,
223 "gpio-bank-name", NULL);
228 static int gpio_sandbox_probe(struct udevice *dev)
230 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
232 if (dev->of_offset == -1) {
233 /* Tell the uclass how many GPIOs we have */
234 uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
237 dev->priv = calloc(sizeof(struct gpio_state), uc_priv->gpio_count);
242 static const struct udevice_id sandbox_gpio_ids[] = {
243 { .compatible = "sandbox,gpio" },
247 U_BOOT_DRIVER(gpio_sandbox) = {
248 .name = "gpio_sandbox",
250 .of_match = sandbox_gpio_ids,
251 .ofdata_to_platdata = sandbox_gpio_ofdata_to_platdata,
252 .probe = gpio_sandbox_probe,
253 .ops = &gpio_sandbox_ops,