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