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