Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2011 The Chromium OS Authors. |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 7 | #include <dm.h> |
| 8 | #include <fdtdec.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Simon Glass | 3176b6c | 2020-07-07 13:11:44 -0600 | [diff] [blame] | 11 | #include <acpi/acpi_device.h> |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 12 | #include <asm/gpio.h> |
Simon Glass | 3176b6c | 2020-07-07 13:11:44 -0600 | [diff] [blame] | 13 | #include <dm/acpi.h> |
Patrick Delaunay | 1b4a22f | 2020-01-13 11:35:15 +0100 | [diff] [blame] | 14 | #include <dm/device_compat.h> |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 15 | #include <dm/lists.h> |
Simon Glass | 12faa02 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 16 | #include <dm/of.h> |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 17 | #include <dm/pinctrl.h> |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 18 | #include <dt-bindings/gpio/gpio.h> |
Patrick Delaunay | 23aee61 | 2020-01-13 11:35:13 +0100 | [diff] [blame] | 19 | #include <dt-bindings/gpio/sandbox-gpio.h> |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 20 | |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 21 | |
| 22 | struct gpio_state { |
| 23 | const char *label; /* label given by requester */ |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 24 | ulong dir_flags; /* dir_flags (GPIOD_...) */ |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 25 | }; |
| 26 | |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 27 | /* Access routines for GPIO dir flags */ |
| 28 | static ulong *get_gpio_dir_flags(struct udevice *dev, unsigned int offset) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 29 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 30 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 31 | struct gpio_state *state = dev_get_priv(dev); |
| 32 | |
| 33 | if (offset >= uc_priv->gpio_count) { |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 34 | static ulong invalid_dir_flags; |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 35 | printf("sandbox_gpio: error: invalid gpio %u\n", offset); |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 36 | return &invalid_dir_flags; |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 39 | return &state[offset].dir_flags; |
| 40 | |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 43 | static int get_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 44 | { |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 45 | return (*get_gpio_dir_flags(dev, offset) & flag) != 0; |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 48 | static int set_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag, |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 49 | int value) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 50 | { |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 51 | ulong *gpio = get_gpio_dir_flags(dev, offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 52 | |
| 53 | if (value) |
| 54 | *gpio |= flag; |
| 55 | else |
| 56 | *gpio &= ~flag; |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 61 | /* |
| 62 | * Back-channel sandbox-internal-only access to GPIO state |
| 63 | */ |
| 64 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 65 | int sandbox_gpio_get_value(struct udevice *dev, unsigned offset) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 66 | { |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 67 | if (get_gpio_flag(dev, offset, GPIOD_IS_OUT)) |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 68 | debug("sandbox_gpio: get_value on output gpio %u\n", offset); |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 69 | return get_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 72 | int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 73 | { |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 74 | return set_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE, value); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 77 | int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 78 | { |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 79 | return get_gpio_flag(dev, offset, GPIOD_IS_OUT); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 82 | int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 83 | { |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 84 | set_gpio_flag(dev, offset, GPIOD_IS_OUT, output); |
| 85 | set_gpio_flag(dev, offset, GPIOD_IS_IN, !(output)); |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | ulong sandbox_gpio_get_dir_flags(struct udevice *dev, unsigned int offset) |
| 91 | { |
| 92 | return *get_gpio_dir_flags(dev, offset); |
| 93 | } |
| 94 | |
| 95 | int sandbox_gpio_set_dir_flags(struct udevice *dev, unsigned int offset, |
| 96 | ulong flags) |
| 97 | { |
| 98 | *get_gpio_dir_flags(dev, offset) = flags; |
| 99 | |
| 100 | return 0; |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /* |
| 104 | * These functions implement the public interface within U-Boot |
| 105 | */ |
| 106 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 107 | /* set GPIO port 'offset' as an input */ |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 108 | static int sb_gpio_direction_input(struct udevice *dev, unsigned offset) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 109 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 110 | debug("%s: offset:%u\n", __func__, offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 111 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 112 | return sandbox_gpio_set_direction(dev, offset, 0); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 115 | /* set GPIO port 'offset' as an output, with polarity 'value' */ |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 116 | static int sb_gpio_direction_output(struct udevice *dev, unsigned offset, |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 117 | int value) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 118 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 119 | debug("%s: offset:%u, value = %d\n", __func__, offset, value); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 120 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 121 | return sandbox_gpio_set_direction(dev, offset, 1) | |
| 122 | sandbox_gpio_set_value(dev, offset, value); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 125 | /* read GPIO IN value of port 'offset' */ |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 126 | static int sb_gpio_get_value(struct udevice *dev, unsigned offset) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 127 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 128 | debug("%s: offset:%u\n", __func__, offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 129 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 130 | return sandbox_gpio_get_value(dev, offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 133 | /* write GPIO OUT value to port 'offset' */ |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 134 | static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 135 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 136 | debug("%s: offset:%u, value = %d\n", __func__, offset, value); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 137 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 138 | if (!sandbox_gpio_get_direction(dev, offset)) { |
| 139 | printf("sandbox_gpio: error: set_value on input gpio %u\n", |
| 140 | offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 141 | return -1; |
| 142 | } |
| 143 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 144 | return sandbox_gpio_set_value(dev, offset, value); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Simon Glass | 3204863 | 2014-10-04 11:29:45 -0600 | [diff] [blame] | 147 | static int sb_gpio_get_function(struct udevice *dev, unsigned offset) |
| 148 | { |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 149 | if (get_gpio_flag(dev, offset, GPIOD_IS_OUT)) |
Simon Glass | 3204863 | 2014-10-04 11:29:45 -0600 | [diff] [blame] | 150 | return GPIOF_OUTPUT; |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 151 | if (get_gpio_flag(dev, offset, GPIOD_IS_IN)) |
| 152 | return GPIOF_INPUT; |
| 153 | |
| 154 | return GPIOF_INPUT; /*GPIO is not configurated */ |
Simon Glass | 3204863 | 2014-10-04 11:29:45 -0600 | [diff] [blame] | 155 | } |
| 156 | |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 157 | static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc, |
Simon Glass | 12faa02 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 158 | struct ofnode_phandle_args *args) |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 159 | { |
| 160 | desc->offset = args->args[0]; |
| 161 | if (args->args_count < 2) |
| 162 | return 0; |
Patrick Delaunay | 23aee61 | 2020-01-13 11:35:13 +0100 | [diff] [blame] | 163 | /* treat generic binding with gpio uclass */ |
| 164 | gpio_xlate_offs_flags(dev, desc, args); |
| 165 | |
| 166 | /* sandbox test specific, not defined in gpio.h */ |
| 167 | if (args->args[1] & GPIO_IN) |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 168 | desc->flags |= GPIOD_IS_IN; |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 169 | |
Patrick Delaunay | 23aee61 | 2020-01-13 11:35:13 +0100 | [diff] [blame] | 170 | if (args->args[1] & GPIO_OUT) |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 171 | desc->flags |= GPIOD_IS_OUT; |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 172 | |
Patrick Delaunay | 23aee61 | 2020-01-13 11:35:13 +0100 | [diff] [blame] | 173 | if (args->args[1] & GPIO_OUT_ACTIVE) |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 174 | desc->flags |= GPIOD_IS_OUT_ACTIVE; |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 179 | static int sb_gpio_set_dir_flags(struct udevice *dev, unsigned int offset, |
| 180 | ulong flags) |
| 181 | { |
| 182 | ulong *dir_flags; |
| 183 | |
| 184 | debug("%s: offset:%u, dir_flags = %lx\n", __func__, offset, flags); |
| 185 | |
| 186 | dir_flags = get_gpio_dir_flags(dev, offset); |
| 187 | |
| 188 | *dir_flags = flags; |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static int sb_gpio_get_dir_flags(struct udevice *dev, unsigned int offset, |
| 194 | ulong *flags) |
| 195 | { |
| 196 | debug("%s: offset:%u\n", __func__, offset); |
| 197 | *flags = *get_gpio_dir_flags(dev, offset); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
Simon Glass | 3176b6c | 2020-07-07 13:11:44 -0600 | [diff] [blame] | 202 | #if CONFIG_IS_ENABLED(ACPIGEN) |
| 203 | static int sb_gpio_get_acpi(const struct gpio_desc *desc, |
| 204 | struct acpi_gpio *gpio) |
| 205 | { |
| 206 | int ret; |
| 207 | |
| 208 | /* Note that gpio_get_acpi() zeroes *gpio before calling here */ |
| 209 | gpio->pin_count = 1; |
| 210 | gpio->pins[0] = desc->offset; |
| 211 | ret = acpi_device_scope(desc->dev, gpio->resource, |
| 212 | sizeof(gpio->resource)); |
| 213 | if (ret) |
| 214 | return log_ret(ret); |
| 215 | |
| 216 | /* All of these values are just used for testing */ |
| 217 | if (desc->flags & GPIOD_ACTIVE_LOW) { |
| 218 | gpio->pin0_addr = 0x80012 + desc->offset; |
| 219 | gpio->type = ACPI_GPIO_TYPE_INTERRUPT; |
| 220 | gpio->pull = ACPI_GPIO_PULL_DOWN; |
| 221 | gpio->interrupt_debounce_timeout = 4321; |
| 222 | |
| 223 | /* We use the GpioInt part */ |
| 224 | gpio->irq.pin = desc->offset; |
| 225 | gpio->irq.polarity = ACPI_IRQ_ACTIVE_BOTH; |
| 226 | gpio->irq.shared = ACPI_IRQ_SHARED; |
| 227 | gpio->irq.wake = ACPI_IRQ_WAKE; |
| 228 | |
| 229 | /* The GpioIo part is only used for testing */ |
| 230 | gpio->polarity = ACPI_GPIO_ACTIVE_LOW; |
| 231 | } else { |
| 232 | gpio->pin0_addr = 0xc00dc + desc->offset; |
| 233 | gpio->type = ACPI_GPIO_TYPE_IO; |
| 234 | gpio->pull = ACPI_GPIO_PULL_UP; |
| 235 | gpio->interrupt_debounce_timeout = 0; |
| 236 | |
| 237 | /* The GpioInt part is not used */ |
| 238 | |
| 239 | /* We use the GpioIo part */ |
| 240 | gpio->output_drive_strength = 1234; |
| 241 | gpio->io_shared = true; |
| 242 | gpio->io_restrict = ACPI_GPIO_IO_RESTRICT_INPUT; |
| 243 | gpio->polarity = 0; |
| 244 | } |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static int sb_gpio_get_name(const struct udevice *dev, char *out_name) |
| 250 | { |
| 251 | return acpi_copy_name(out_name, "GPIO"); |
| 252 | } |
| 253 | |
| 254 | struct acpi_ops gpio_sandbox_acpi_ops = { |
| 255 | .get_name = sb_gpio_get_name, |
| 256 | }; |
| 257 | #endif /* ACPIGEN */ |
| 258 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 259 | static const struct dm_gpio_ops gpio_sandbox_ops = { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 260 | .direction_input = sb_gpio_direction_input, |
| 261 | .direction_output = sb_gpio_direction_output, |
| 262 | .get_value = sb_gpio_get_value, |
| 263 | .set_value = sb_gpio_set_value, |
Simon Glass | 3204863 | 2014-10-04 11:29:45 -0600 | [diff] [blame] | 264 | .get_function = sb_gpio_get_function, |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 265 | .xlate = sb_gpio_xlate, |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 266 | .set_dir_flags = sb_gpio_set_dir_flags, |
| 267 | .get_dir_flags = sb_gpio_get_dir_flags, |
Simon Glass | 3176b6c | 2020-07-07 13:11:44 -0600 | [diff] [blame] | 268 | #if CONFIG_IS_ENABLED(ACPIGEN) |
| 269 | .get_acpi = sb_gpio_get_acpi, |
| 270 | #endif |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 271 | }; |
| 272 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 273 | static int sandbox_gpio_ofdata_to_platdata(struct udevice *dev) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 274 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 275 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 276 | |
Simon Glass | 9e7ab23 | 2018-02-03 10:36:59 -0700 | [diff] [blame] | 277 | uc_priv->gpio_count = dev_read_u32_default(dev, "sandbox,gpio-count", |
| 278 | 0); |
Simon Glass | 1185fcb | 2017-05-18 20:09:20 -0600 | [diff] [blame] | 279 | uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name"); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 280 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 281 | return 0; |
| 282 | } |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 283 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 284 | static int gpio_sandbox_probe(struct udevice *dev) |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 285 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 286 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 287 | |
Simon Glass | 1185fcb | 2017-05-18 20:09:20 -0600 | [diff] [blame] | 288 | if (!dev_of_valid(dev)) |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 289 | /* Tell the uclass how many GPIOs we have */ |
| 290 | uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT; |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 291 | |
| 292 | dev->priv = calloc(sizeof(struct gpio_state), uc_priv->gpio_count); |
| 293 | |
| 294 | return 0; |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 295 | } |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 296 | |
Simon Glass | a367f1d | 2014-10-04 11:29:46 -0600 | [diff] [blame] | 297 | static int gpio_sandbox_remove(struct udevice *dev) |
| 298 | { |
| 299 | free(dev->priv); |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
Simon Glass | 767827a | 2014-06-11 23:29:45 -0600 | [diff] [blame] | 304 | static const struct udevice_id sandbox_gpio_ids[] = { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 305 | { .compatible = "sandbox,gpio" }, |
| 306 | { } |
| 307 | }; |
| 308 | |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 309 | U_BOOT_DRIVER(sandbox_gpio) = { |
| 310 | .name = "sandbox_gpio", |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 311 | .id = UCLASS_GPIO, |
| 312 | .of_match = sandbox_gpio_ids, |
| 313 | .ofdata_to_platdata = sandbox_gpio_ofdata_to_platdata, |
| 314 | .probe = gpio_sandbox_probe, |
Simon Glass | a367f1d | 2014-10-04 11:29:46 -0600 | [diff] [blame] | 315 | .remove = gpio_sandbox_remove, |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 316 | .ops = &gpio_sandbox_ops, |
Simon Glass | 3176b6c | 2020-07-07 13:11:44 -0600 | [diff] [blame] | 317 | ACPI_OPS_PTR(&gpio_sandbox_acpi_ops) |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 318 | }; |
Patrick Delaunay | 1b4a22f | 2020-01-13 11:35:15 +0100 | [diff] [blame] | 319 | |
Walter Lozano | 48e5b04 | 2020-06-25 01:10:06 -0300 | [diff] [blame] | 320 | U_BOOT_DRIVER_ALIAS(sandbox_gpio, sandbox_gpio_alias) |
| 321 | |
Patrick Delaunay | 1b4a22f | 2020-01-13 11:35:15 +0100 | [diff] [blame] | 322 | /* pincontrol: used only to check GPIO pin configuration (pinmux command) */ |
| 323 | |
| 324 | struct sb_pinctrl_priv { |
| 325 | int pinctrl_ngpios; |
| 326 | struct list_head gpio_dev; |
| 327 | }; |
| 328 | |
| 329 | struct sb_gpio_bank { |
| 330 | struct udevice *gpio_dev; |
| 331 | struct list_head list; |
| 332 | }; |
| 333 | |
| 334 | static int sb_populate_gpio_dev_list(struct udevice *dev) |
| 335 | { |
| 336 | struct sb_pinctrl_priv *priv = dev_get_priv(dev); |
| 337 | struct udevice *gpio_dev; |
| 338 | struct udevice *child; |
| 339 | struct sb_gpio_bank *gpio_bank; |
| 340 | int ret; |
| 341 | |
| 342 | /* |
| 343 | * parse pin-controller sub-nodes (ie gpio bank nodes) and fill |
| 344 | * a list with all gpio device reference which belongs to the |
| 345 | * current pin-controller. This list is used to find pin_name and |
| 346 | * pin muxing |
| 347 | */ |
| 348 | list_for_each_entry(child, &dev->child_head, sibling_node) { |
| 349 | ret = uclass_get_device_by_name(UCLASS_GPIO, child->name, |
| 350 | &gpio_dev); |
| 351 | if (ret < 0) |
| 352 | continue; |
| 353 | |
| 354 | gpio_bank = malloc(sizeof(*gpio_bank)); |
| 355 | if (!gpio_bank) { |
| 356 | dev_err(dev, "Not enough memory\n"); |
| 357 | return -ENOMEM; |
| 358 | } |
| 359 | |
| 360 | gpio_bank->gpio_dev = gpio_dev; |
| 361 | list_add_tail(&gpio_bank->list, &priv->gpio_dev); |
| 362 | } |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | static int sb_pinctrl_get_pins_count(struct udevice *dev) |
| 368 | { |
| 369 | struct sb_pinctrl_priv *priv = dev_get_priv(dev); |
| 370 | struct gpio_dev_priv *uc_priv; |
| 371 | struct sb_gpio_bank *gpio_bank; |
| 372 | |
| 373 | /* |
| 374 | * if get_pins_count has already been executed once on this |
| 375 | * pin-controller, no need to run it again |
| 376 | */ |
| 377 | if (priv->pinctrl_ngpios) |
| 378 | return priv->pinctrl_ngpios; |
| 379 | |
| 380 | if (list_empty(&priv->gpio_dev)) |
| 381 | sb_populate_gpio_dev_list(dev); |
| 382 | /* |
| 383 | * walk through all banks to retrieve the pin-controller |
| 384 | * pins number |
| 385 | */ |
| 386 | list_for_each_entry(gpio_bank, &priv->gpio_dev, list) { |
| 387 | uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev); |
| 388 | |
| 389 | priv->pinctrl_ngpios += uc_priv->gpio_count; |
| 390 | } |
| 391 | |
| 392 | return priv->pinctrl_ngpios; |
| 393 | } |
| 394 | |
| 395 | static struct udevice *sb_pinctrl_get_gpio_dev(struct udevice *dev, |
| 396 | unsigned int selector, |
| 397 | unsigned int *idx) |
| 398 | { |
| 399 | struct sb_pinctrl_priv *priv = dev_get_priv(dev); |
| 400 | struct sb_gpio_bank *gpio_bank; |
| 401 | struct gpio_dev_priv *uc_priv; |
| 402 | int pin_count = 0; |
| 403 | |
| 404 | if (list_empty(&priv->gpio_dev)) |
| 405 | sb_populate_gpio_dev_list(dev); |
| 406 | |
| 407 | /* look up for the bank which owns the requested pin */ |
| 408 | list_for_each_entry(gpio_bank, &priv->gpio_dev, list) { |
| 409 | uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev); |
| 410 | |
| 411 | if (selector < (pin_count + uc_priv->gpio_count)) { |
| 412 | /* |
| 413 | * we found the bank, convert pin selector to |
| 414 | * gpio bank index |
| 415 | */ |
| 416 | *idx = selector - pin_count; |
| 417 | |
| 418 | return gpio_bank->gpio_dev; |
| 419 | } |
| 420 | pin_count += uc_priv->gpio_count; |
| 421 | } |
| 422 | |
| 423 | return NULL; |
| 424 | } |
| 425 | |
| 426 | static const char *sb_pinctrl_get_pin_name(struct udevice *dev, |
| 427 | unsigned int selector) |
| 428 | { |
| 429 | struct gpio_dev_priv *uc_priv; |
| 430 | struct udevice *gpio_dev; |
| 431 | unsigned int gpio_idx; |
| 432 | static char pin_name[PINNAME_SIZE]; |
| 433 | |
| 434 | /* look up for the bank which owns the requested pin */ |
| 435 | gpio_dev = sb_pinctrl_get_gpio_dev(dev, selector, &gpio_idx); |
| 436 | if (!gpio_dev) { |
| 437 | snprintf(pin_name, PINNAME_SIZE, "Error"); |
| 438 | } else { |
| 439 | uc_priv = dev_get_uclass_priv(gpio_dev); |
| 440 | |
| 441 | snprintf(pin_name, PINNAME_SIZE, "%s%d", |
| 442 | uc_priv->bank_name, |
| 443 | gpio_idx); |
| 444 | } |
| 445 | |
| 446 | return pin_name; |
| 447 | } |
| 448 | |
| 449 | static char *get_dir_flags_string(ulong flags) |
| 450 | { |
| 451 | if (flags & GPIOD_OPEN_DRAIN) |
| 452 | return "drive-open-drain"; |
| 453 | if (flags & GPIOD_OPEN_SOURCE) |
| 454 | return "drive-open-source"; |
| 455 | if (flags & GPIOD_PULL_UP) |
| 456 | return "bias-pull-up"; |
| 457 | if (flags & GPIOD_PULL_DOWN) |
| 458 | return "bias-pull-down"; |
| 459 | return "."; |
| 460 | } |
| 461 | |
| 462 | static int sb_pinctrl_get_pin_muxing(struct udevice *dev, |
| 463 | unsigned int selector, |
| 464 | char *buf, int size) |
| 465 | { |
| 466 | struct udevice *gpio_dev; |
| 467 | unsigned int gpio_idx; |
| 468 | ulong dir_flags; |
| 469 | int function; |
| 470 | |
| 471 | /* look up for the bank which owns the requested pin */ |
| 472 | gpio_dev = sb_pinctrl_get_gpio_dev(dev, selector, &gpio_idx); |
| 473 | if (!gpio_dev) { |
| 474 | snprintf(buf, size, "Error"); |
| 475 | } else { |
| 476 | function = sb_gpio_get_function(gpio_dev, gpio_idx); |
| 477 | dir_flags = *get_gpio_dir_flags(gpio_dev, gpio_idx); |
| 478 | |
| 479 | snprintf(buf, size, "gpio %s %s", |
| 480 | function == GPIOF_OUTPUT ? "output" : "input", |
| 481 | get_dir_flags_string(dir_flags)); |
| 482 | } |
| 483 | |
| 484 | return 0; |
| 485 | } |
Simon Glass | 3176b6c | 2020-07-07 13:11:44 -0600 | [diff] [blame] | 486 | |
| 487 | #if CONFIG_IS_ENABLED(ACPIGEN) |
| 488 | static int sb_pinctrl_get_name(const struct udevice *dev, char *out_name) |
| 489 | { |
| 490 | return acpi_copy_name(out_name, "PINC"); |
| 491 | } |
| 492 | #endif |
Patrick Delaunay | 1b4a22f | 2020-01-13 11:35:15 +0100 | [diff] [blame] | 493 | |
| 494 | static int sandbox_pinctrl_probe(struct udevice *dev) |
| 495 | { |
| 496 | struct sb_pinctrl_priv *priv = dev_get_priv(dev); |
| 497 | |
| 498 | INIT_LIST_HEAD(&priv->gpio_dev); |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | static struct pinctrl_ops sandbox_pinctrl_gpio_ops = { |
| 504 | .get_pin_name = sb_pinctrl_get_pin_name, |
| 505 | .get_pins_count = sb_pinctrl_get_pins_count, |
| 506 | .get_pin_muxing = sb_pinctrl_get_pin_muxing, |
| 507 | }; |
Simon Glass | 3176b6c | 2020-07-07 13:11:44 -0600 | [diff] [blame] | 508 | |
| 509 | #if CONFIG_IS_ENABLED(ACPIGEN) |
| 510 | struct acpi_ops pinctrl_sandbox_acpi_ops = { |
| 511 | .get_name = sb_pinctrl_get_name, |
| 512 | }; |
| 513 | #endif |
Patrick Delaunay | 1b4a22f | 2020-01-13 11:35:15 +0100 | [diff] [blame] | 514 | |
| 515 | static const struct udevice_id sandbox_pinctrl_gpio_match[] = { |
| 516 | { .compatible = "sandbox,pinctrl-gpio" }, |
| 517 | { /* sentinel */ } |
| 518 | }; |
| 519 | |
| 520 | U_BOOT_DRIVER(sandbox_pinctrl_gpio) = { |
| 521 | .name = "sandbox_pinctrl_gpio", |
| 522 | .id = UCLASS_PINCTRL, |
| 523 | .of_match = sandbox_pinctrl_gpio_match, |
| 524 | .ops = &sandbox_pinctrl_gpio_ops, |
| 525 | .bind = dm_scan_fdt_dev, |
| 526 | .probe = sandbox_pinctrl_probe, |
| 527 | .priv_auto_alloc_size = sizeof(struct sb_pinctrl_priv), |
Simon Glass | 3176b6c | 2020-07-07 13:11:44 -0600 | [diff] [blame] | 528 | ACPI_OPS_PTR(&pinctrl_sandbox_acpi_ops) |
Patrick Delaunay | 1b4a22f | 2020-01-13 11:35:15 +0100 | [diff] [blame] | 529 | }; |