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 */
17 #define GPIOF_ODR (1 << 2) /* Currently set to open drain mode */
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_get_uclass_priv(dev);
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);
58 * Back-channel sandbox-internal-only access to GPIO state
61 int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
63 if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
64 debug("sandbox_gpio: get_value on output gpio %u\n", offset);
65 return get_gpio_flag(dev, offset, GPIOF_HIGH);
68 int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
70 return set_gpio_flag(dev, offset, GPIOF_HIGH, value);
73 int sandbox_gpio_get_open_drain(struct udevice *dev, unsigned offset)
75 return get_gpio_flag(dev, offset, GPIOF_ODR);
78 int sandbox_gpio_set_open_drain(struct udevice *dev, unsigned offset, int value)
80 return set_gpio_flag(dev, offset, GPIOF_ODR, value);
83 int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset)
85 return get_gpio_flag(dev, offset, GPIOF_OUTPUT);
88 int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
90 return set_gpio_flag(dev, offset, GPIOF_OUTPUT, output);
94 * These functions implement the public interface within U-Boot
97 /* set GPIO port 'offset' as an input */
98 static int sb_gpio_direction_input(struct udevice *dev, unsigned offset)
100 debug("%s: offset:%u\n", __func__, offset);
102 return sandbox_gpio_set_direction(dev, offset, 0);
105 /* set GPIO port 'offset' as an output, with polarity 'value' */
106 static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
109 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
111 return sandbox_gpio_set_direction(dev, offset, 1) |
112 sandbox_gpio_set_value(dev, offset, value);
115 /* read GPIO IN value of port 'offset' */
116 static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
118 debug("%s: offset:%u\n", __func__, offset);
120 return sandbox_gpio_get_value(dev, offset);
123 /* write GPIO OUT value to port 'offset' */
124 static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
126 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
128 if (!sandbox_gpio_get_direction(dev, offset)) {
129 printf("sandbox_gpio: error: set_value on input gpio %u\n",
134 return sandbox_gpio_set_value(dev, offset, value);
137 /* read GPIO ODR value of port 'offset' */
138 static int sb_gpio_get_open_drain(struct udevice *dev, unsigned offset)
140 debug("%s: offset:%u\n", __func__, offset);
142 return sandbox_gpio_get_open_drain(dev, offset);
145 /* write GPIO ODR value to port 'offset' */
146 static int sb_gpio_set_open_drain(struct udevice *dev, unsigned offset, int value)
148 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
150 if (!sandbox_gpio_get_direction(dev, offset)) {
151 printf("sandbox_gpio: error: set_open_drain on input gpio %u\n",
156 return sandbox_gpio_set_open_drain(dev, offset, value);
159 static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
161 if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
166 static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
167 struct ofnode_phandle_args *args)
169 desc->offset = args->args[0];
170 if (args->args_count < 2)
172 if (args->args[1] & GPIO_ACTIVE_LOW)
173 desc->flags |= GPIOD_ACTIVE_LOW;
174 if (args->args[1] & 2)
175 desc->flags |= GPIOD_IS_IN;
176 if (args->args[1] & 4)
177 desc->flags |= GPIOD_IS_OUT;
178 if (args->args[1] & 8)
179 desc->flags |= GPIOD_IS_OUT_ACTIVE;
184 static const struct dm_gpio_ops gpio_sandbox_ops = {
185 .direction_input = sb_gpio_direction_input,
186 .direction_output = sb_gpio_direction_output,
187 .get_value = sb_gpio_get_value,
188 .set_value = sb_gpio_set_value,
189 .get_open_drain = sb_gpio_get_open_drain,
190 .set_open_drain = sb_gpio_set_open_drain,
191 .get_function = sb_gpio_get_function,
192 .xlate = sb_gpio_xlate,
195 static int sandbox_gpio_ofdata_to_platdata(struct udevice *dev)
197 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
199 uc_priv->gpio_count = dev_read_u32_default(dev, "sandbox,gpio-count",
201 uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
206 static int gpio_sandbox_probe(struct udevice *dev)
208 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
210 if (!dev_of_valid(dev))
211 /* Tell the uclass how many GPIOs we have */
212 uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
214 dev->priv = calloc(sizeof(struct gpio_state), uc_priv->gpio_count);
219 static int gpio_sandbox_remove(struct udevice *dev)
226 static const struct udevice_id sandbox_gpio_ids[] = {
227 { .compatible = "sandbox,gpio" },
231 U_BOOT_DRIVER(gpio_sandbox) = {
232 .name = "gpio_sandbox",
234 .of_match = sandbox_gpio_ids,
235 .ofdata_to_platdata = sandbox_gpio_ofdata_to_platdata,
236 .probe = gpio_sandbox_probe,
237 .remove = gpio_sandbox_remove,
238 .ops = &gpio_sandbox_ops,