1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011 The Chromium OS Authors.
12 #include <dt-bindings/gpio/gpio.h>
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 */
19 const char *label; /* label given by requester */
20 u8 flags; /* flags (GPIOF_...) */
23 /* Access routines for GPIO state */
24 static u8 *get_gpio_flags(struct udevice *dev, unsigned offset)
26 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
27 struct gpio_state *state = dev_get_priv(dev);
29 if (offset >= uc_priv->gpio_count) {
30 static u8 invalid_flags;
31 printf("sandbox_gpio: error: invalid gpio %u\n", offset);
32 return &invalid_flags;
35 return &state[offset].flags;
38 static int get_gpio_flag(struct udevice *dev, unsigned offset, int flag)
40 return (*get_gpio_flags(dev, offset) & flag) != 0;
43 static int set_gpio_flag(struct udevice *dev, unsigned offset, int flag,
46 u8 *gpio = get_gpio_flags(dev, offset);
57 * Back-channel sandbox-internal-only access to GPIO state
60 int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
62 if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
63 debug("sandbox_gpio: get_value on output gpio %u\n", offset);
64 return get_gpio_flag(dev, offset, GPIOF_HIGH);
67 int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
69 return set_gpio_flag(dev, offset, GPIOF_HIGH, value);
72 int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset)
74 return get_gpio_flag(dev, offset, GPIOF_OUTPUT);
77 int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
79 return set_gpio_flag(dev, offset, GPIOF_OUTPUT, output);
83 * These functions implement the public interface within U-Boot
86 /* set GPIO port 'offset' as an input */
87 static int sb_gpio_direction_input(struct udevice *dev, unsigned offset)
89 debug("%s: offset:%u\n", __func__, offset);
91 return sandbox_gpio_set_direction(dev, offset, 0);
94 /* set GPIO port 'offset' as an output, with polarity 'value' */
95 static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
98 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
100 return sandbox_gpio_set_direction(dev, offset, 1) |
101 sandbox_gpio_set_value(dev, offset, value);
104 /* read GPIO IN value of port 'offset' */
105 static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
107 debug("%s: offset:%u\n", __func__, offset);
109 return sandbox_gpio_get_value(dev, offset);
112 /* write GPIO OUT value to port 'offset' */
113 static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
115 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
117 if (!sandbox_gpio_get_direction(dev, offset)) {
118 printf("sandbox_gpio: error: set_value on input gpio %u\n",
123 return sandbox_gpio_set_value(dev, offset, value);
126 static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
128 if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
133 static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
134 struct ofnode_phandle_args *args)
136 desc->offset = args->args[0];
137 if (args->args_count < 2)
139 if (args->args[1] & GPIO_ACTIVE_LOW)
140 desc->flags |= GPIOD_ACTIVE_LOW;
141 if (args->args[1] & 2)
142 desc->flags |= GPIOD_IS_IN;
143 if (args->args[1] & 4)
144 desc->flags |= GPIOD_IS_OUT;
145 if (args->args[1] & 8)
146 desc->flags |= GPIOD_IS_OUT_ACTIVE;
151 static const struct dm_gpio_ops gpio_sandbox_ops = {
152 .direction_input = sb_gpio_direction_input,
153 .direction_output = sb_gpio_direction_output,
154 .get_value = sb_gpio_get_value,
155 .set_value = sb_gpio_set_value,
156 .get_function = sb_gpio_get_function,
157 .xlate = sb_gpio_xlate,
160 static int sandbox_gpio_ofdata_to_platdata(struct udevice *dev)
162 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
164 uc_priv->gpio_count = dev_read_u32_default(dev, "sandbox,gpio-count",
166 uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
171 static int gpio_sandbox_probe(struct udevice *dev)
173 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
175 if (!dev_of_valid(dev))
176 /* Tell the uclass how many GPIOs we have */
177 uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
179 dev->priv = calloc(sizeof(struct gpio_state), uc_priv->gpio_count);
184 static int gpio_sandbox_remove(struct udevice *dev)
191 static const struct udevice_id sandbox_gpio_ids[] = {
192 { .compatible = "sandbox,gpio" },
196 U_BOOT_DRIVER(gpio_sandbox) = {
197 .name = "gpio_sandbox",
199 .of_match = sandbox_gpio_ids,
200 .ofdata_to_platdata = sandbox_gpio_ofdata_to_platdata,
201 .probe = gpio_sandbox_probe,
202 .remove = gpio_sandbox_remove,
203 .ops = &gpio_sandbox_ops,