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