2 * Copyright (c) 2011 The Chromium OS Authors.
3 * SPDX-License-Identifier: GPL-2.0+
12 #include <dt-bindings/gpio/gpio.h>
14 DECLARE_GLOBAL_DATA_PTR;
16 /* Flags for each GPIO */
17 #define GPIOF_OUTPUT (1 << 0) /* Currently set as an output */
18 #define GPIOF_HIGH (1 << 1) /* Currently set high */
19 #define GPIOF_ODR (1 << 2) /* Currently set to open drain mode */
22 const char *label; /* label given by requester */
23 u8 flags; /* flags (GPIOF_...) */
26 /* Access routines for GPIO state */
27 static u8 *get_gpio_flags(struct udevice *dev, unsigned offset)
29 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
30 struct gpio_state *state = dev_get_priv(dev);
32 if (offset >= uc_priv->gpio_count) {
33 static u8 invalid_flags;
34 printf("sandbox_gpio: error: invalid gpio %u\n", offset);
35 return &invalid_flags;
38 return &state[offset].flags;
41 static int get_gpio_flag(struct udevice *dev, unsigned offset, int flag)
43 return (*get_gpio_flags(dev, offset) & flag) != 0;
46 static int set_gpio_flag(struct udevice *dev, unsigned offset, int flag,
49 u8 *gpio = get_gpio_flags(dev, offset);
60 * Back-channel sandbox-internal-only access to GPIO state
63 int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
65 if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
66 debug("sandbox_gpio: get_value on output gpio %u\n", offset);
67 return get_gpio_flag(dev, offset, GPIOF_HIGH);
70 int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
72 return set_gpio_flag(dev, offset, GPIOF_HIGH, value);
75 int sandbox_gpio_get_open_drain(struct udevice *dev, unsigned offset)
77 return get_gpio_flag(dev, offset, GPIOF_ODR);
80 int sandbox_gpio_set_open_drain(struct udevice *dev, unsigned offset, int value)
82 return set_gpio_flag(dev, offset, GPIOF_ODR, 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 return sandbox_gpio_set_direction(dev, offset, 0);
107 /* set GPIO port 'offset' as an output, with polarity 'value' */
108 static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
111 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
113 return sandbox_gpio_set_direction(dev, offset, 1) |
114 sandbox_gpio_set_value(dev, offset, value);
117 /* read GPIO IN value of port 'offset' */
118 static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
120 debug("%s: offset:%u\n", __func__, offset);
122 return sandbox_gpio_get_value(dev, offset);
125 /* write GPIO OUT value to port 'offset' */
126 static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
128 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
130 if (!sandbox_gpio_get_direction(dev, offset)) {
131 printf("sandbox_gpio: error: set_value on input gpio %u\n",
136 return sandbox_gpio_set_value(dev, offset, value);
139 /* read GPIO ODR value of port 'offset' */
140 static int sb_gpio_get_open_drain(struct udevice *dev, unsigned offset)
142 debug("%s: offset:%u\n", __func__, offset);
144 return sandbox_gpio_get_open_drain(dev, offset);
147 /* write GPIO ODR value to port 'offset' */
148 static int sb_gpio_set_open_drain(struct udevice *dev, unsigned offset, int value)
150 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
152 if (!sandbox_gpio_get_direction(dev, offset)) {
153 printf("sandbox_gpio: error: set_open_drain on input gpio %u\n",
158 return sandbox_gpio_set_open_drain(dev, offset, value);
161 static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
163 if (get_gpio_flag(dev, offset, GPIOF_OUTPUT))
168 static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
169 struct ofnode_phandle_args *args)
171 desc->offset = args->args[0];
172 if (args->args_count < 2)
174 if (args->args[1] & GPIO_ACTIVE_LOW)
175 desc->flags |= GPIOD_ACTIVE_LOW;
176 if (args->args[1] & 2)
177 desc->flags |= GPIOD_IS_IN;
178 if (args->args[1] & 4)
179 desc->flags |= GPIOD_IS_OUT;
180 if (args->args[1] & 8)
181 desc->flags |= GPIOD_IS_OUT_ACTIVE;
186 static const struct dm_gpio_ops gpio_sandbox_ops = {
187 .direction_input = sb_gpio_direction_input,
188 .direction_output = sb_gpio_direction_output,
189 .get_value = sb_gpio_get_value,
190 .set_value = sb_gpio_set_value,
191 .get_open_drain = sb_gpio_get_open_drain,
192 .set_open_drain = sb_gpio_set_open_drain,
193 .get_function = sb_gpio_get_function,
194 .xlate = sb_gpio_xlate,
197 static int sandbox_gpio_ofdata_to_platdata(struct udevice *dev)
199 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
201 uc_priv->gpio_count = dev_read_u32_default(dev, "sandbox,gpio-count",
203 uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
208 static int gpio_sandbox_probe(struct udevice *dev)
210 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
212 if (!dev_of_valid(dev))
213 /* Tell the uclass how many GPIOs we have */
214 uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
216 dev->priv = calloc(sizeof(struct gpio_state), uc_priv->gpio_count);
221 static int gpio_sandbox_remove(struct udevice *dev)
228 static const struct udevice_id sandbox_gpio_ids[] = {
229 { .compatible = "sandbox,gpio" },
233 U_BOOT_DRIVER(gpio_sandbox) = {
234 .name = "gpio_sandbox",
236 .of_match = sandbox_gpio_ids,
237 .ofdata_to_platdata = sandbox_gpio_ofdata_to_platdata,
238 .probe = gpio_sandbox_probe,
239 .remove = gpio_sandbox_remove,
240 .ops = &gpio_sandbox_ops,