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> |
| 9 | #include <malloc.h> |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 10 | #include <asm/gpio.h> |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame^] | 11 | #include <dm/lists.h> |
Simon Glass | 3a57123 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 12 | #include <dm/of.h> |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame^] | 13 | #include <dm/pinctrl.h> |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 14 | #include <dt-bindings/gpio/gpio.h> |
Patrick Delaunay | 2c0f782 | 2020-01-13 11:35:13 +0100 | [diff] [blame] | 15 | #include <dt-bindings/gpio/sandbox-gpio.h> |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 16 | |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 17 | |
| 18 | struct gpio_state { |
| 19 | const char *label; /* label given by requester */ |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame^] | 20 | ulong dir_flags; /* dir_flags (GPIOD_...) */ |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 21 | }; |
| 22 | |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame^] | 23 | /* Access routines for GPIO dir flags */ |
| 24 | static ulong *get_gpio_dir_flags(struct udevice *dev, unsigned int offset) |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 25 | { |
Simon Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 26 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 27 | struct gpio_state *state = dev_get_priv(dev); |
| 28 | |
| 29 | if (offset >= uc_priv->gpio_count) { |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame^] | 30 | static ulong invalid_dir_flags; |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 31 | printf("sandbox_gpio: error: invalid gpio %u\n", offset); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame^] | 32 | return &invalid_dir_flags; |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 33 | } |
| 34 | |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame^] | 35 | return &state[offset].dir_flags; |
| 36 | |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame^] | 39 | 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] | 40 | { |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame^] | 41 | return (*get_gpio_dir_flags(dev, offset) & flag) != 0; |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame^] | 44 | 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] | 45 | int value) |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 46 | { |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame^] | 47 | ulong *gpio = get_gpio_dir_flags(dev, offset); |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 48 | |
| 49 | if (value) |
| 50 | *gpio |= flag; |
| 51 | else |
| 52 | *gpio &= ~flag; |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 57 | /* |
| 58 | * Back-channel sandbox-internal-only access to GPIO state |
| 59 | */ |
| 60 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 61 | int sandbox_gpio_get_value(struct udevice *dev, unsigned offset) |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 62 | { |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame^] | 63 | if (get_gpio_flag(dev, offset, GPIOD_IS_OUT)) |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 64 | debug("sandbox_gpio: get_value on output gpio %u\n", offset); |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame^] | 65 | return get_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE); |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 68 | int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value) |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 69 | { |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame^] | 70 | return set_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE, value); |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 73 | int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset) |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 74 | { |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame^] | 75 | return get_gpio_flag(dev, offset, GPIOD_IS_OUT); |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 78 | int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output) |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 79 | { |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame^] | 80 | set_gpio_flag(dev, offset, GPIOD_IS_OUT, output); |
| 81 | set_gpio_flag(dev, offset, GPIOD_IS_IN, !(output)); |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | ulong sandbox_gpio_get_dir_flags(struct udevice *dev, unsigned int offset) |
| 87 | { |
| 88 | return *get_gpio_dir_flags(dev, offset); |
| 89 | } |
| 90 | |
| 91 | int sandbox_gpio_set_dir_flags(struct udevice *dev, unsigned int offset, |
| 92 | ulong flags) |
| 93 | { |
| 94 | *get_gpio_dir_flags(dev, offset) = flags; |
| 95 | |
| 96 | return 0; |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* |
| 100 | * These functions implement the public interface within U-Boot |
| 101 | */ |
| 102 | |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 103 | /* set GPIO port 'offset' as an input */ |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 104 | static int sb_gpio_direction_input(struct udevice *dev, unsigned offset) |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 105 | { |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 106 | debug("%s: offset:%u\n", __func__, offset); |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 107 | |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 108 | return sandbox_gpio_set_direction(dev, offset, 0); |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 111 | /* set GPIO port 'offset' as an output, with polarity 'value' */ |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 112 | static int sb_gpio_direction_output(struct udevice *dev, unsigned offset, |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 113 | int value) |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 114 | { |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 115 | debug("%s: offset:%u, value = %d\n", __func__, offset, value); |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 116 | |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 117 | return sandbox_gpio_set_direction(dev, offset, 1) | |
| 118 | sandbox_gpio_set_value(dev, offset, value); |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 121 | /* read GPIO IN value of port 'offset' */ |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 122 | static int sb_gpio_get_value(struct udevice *dev, unsigned offset) |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 123 | { |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 124 | debug("%s: offset:%u\n", __func__, offset); |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 125 | |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 126 | return sandbox_gpio_get_value(dev, offset); |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 129 | /* write GPIO OUT value to port 'offset' */ |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 130 | 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] | 131 | { |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 132 | debug("%s: offset:%u, value = %d\n", __func__, offset, value); |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 133 | |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 134 | if (!sandbox_gpio_get_direction(dev, offset)) { |
| 135 | printf("sandbox_gpio: error: set_value on input gpio %u\n", |
| 136 | offset); |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 137 | return -1; |
| 138 | } |
| 139 | |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 140 | return sandbox_gpio_set_value(dev, offset, value); |
Simon Glass | 8d30fcd | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Simon Glass | 699ea96 | 2014-10-04 11:29:45 -0600 | [diff] [blame] | 143 | static int sb_gpio_get_function(struct udevice *dev, unsigned offset) |
| 144 | { |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame^] | 145 | if (get_gpio_flag(dev, offset, GPIOD_IS_OUT)) |
Simon Glass | 699ea96 | 2014-10-04 11:29:45 -0600 | [diff] [blame] | 146 | return GPIOF_OUTPUT; |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame^] | 147 | if (get_gpio_flag(dev, offset, GPIOD_IS_IN)) |
| 148 | return GPIOF_INPUT; |
| 149 | |
| 150 | return GPIOF_INPUT; /*GPIO is not configurated */ |
Simon Glass | 699ea96 | 2014-10-04 11:29:45 -0600 | [diff] [blame] | 151 | } |
| 152 | |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 153 | static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc, |
Simon Glass | 3a57123 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 154 | struct ofnode_phandle_args *args) |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 155 | { |
| 156 | desc->offset = args->args[0]; |
| 157 | if (args->args_count < 2) |
| 158 | return 0; |
Patrick Delaunay | 2c0f782 | 2020-01-13 11:35:13 +0100 | [diff] [blame] | 159 | /* treat generic binding with gpio uclass */ |
| 160 | gpio_xlate_offs_flags(dev, desc, args); |
| 161 | |
| 162 | /* sandbox test specific, not defined in gpio.h */ |
| 163 | if (args->args[1] & GPIO_IN) |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 164 | desc->flags |= GPIOD_IS_IN; |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame^] | 165 | |
Patrick Delaunay | 2c0f782 | 2020-01-13 11:35:13 +0100 | [diff] [blame] | 166 | if (args->args[1] & GPIO_OUT) |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 167 | desc->flags |= GPIOD_IS_OUT; |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame^] | 168 | |
Patrick Delaunay | 2c0f782 | 2020-01-13 11:35:13 +0100 | [diff] [blame] | 169 | if (args->args[1] & GPIO_OUT_ACTIVE) |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 170 | desc->flags |= GPIOD_IS_OUT_ACTIVE; |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame^] | 175 | static int sb_gpio_set_dir_flags(struct udevice *dev, unsigned int offset, |
| 176 | ulong flags) |
| 177 | { |
| 178 | ulong *dir_flags; |
| 179 | |
| 180 | debug("%s: offset:%u, dir_flags = %lx\n", __func__, offset, flags); |
| 181 | |
| 182 | dir_flags = get_gpio_dir_flags(dev, offset); |
| 183 | |
| 184 | *dir_flags = flags; |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static int sb_gpio_get_dir_flags(struct udevice *dev, unsigned int offset, |
| 190 | ulong *flags) |
| 191 | { |
| 192 | debug("%s: offset:%u\n", __func__, offset); |
| 193 | *flags = *get_gpio_dir_flags(dev, offset); |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 198 | static const struct dm_gpio_ops gpio_sandbox_ops = { |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 199 | .direction_input = sb_gpio_direction_input, |
| 200 | .direction_output = sb_gpio_direction_output, |
| 201 | .get_value = sb_gpio_get_value, |
| 202 | .set_value = sb_gpio_set_value, |
Simon Glass | 699ea96 | 2014-10-04 11:29:45 -0600 | [diff] [blame] | 203 | .get_function = sb_gpio_get_function, |
Simon Glass | 3669e0e | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 204 | .xlate = sb_gpio_xlate, |
Patrick Delaunay | ff52665 | 2020-01-13 11:35:14 +0100 | [diff] [blame^] | 205 | .set_dir_flags = sb_gpio_set_dir_flags, |
| 206 | .get_dir_flags = sb_gpio_get_dir_flags, |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 207 | }; |
| 208 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 209 | static int sandbox_gpio_ofdata_to_platdata(struct udevice *dev) |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 210 | { |
Simon Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 211 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 212 | |
Simon Glass | 995b60b | 2018-02-03 10:36:59 -0700 | [diff] [blame] | 213 | uc_priv->gpio_count = dev_read_u32_default(dev, "sandbox,gpio-count", |
| 214 | 0); |
Simon Glass | 95795ad | 2017-05-18 20:09:20 -0600 | [diff] [blame] | 215 | uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name"); |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 220 | static int gpio_sandbox_probe(struct udevice *dev) |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 221 | { |
Simon Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 222 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 223 | |
Simon Glass | 95795ad | 2017-05-18 20:09:20 -0600 | [diff] [blame] | 224 | if (!dev_of_valid(dev)) |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 225 | /* Tell the uclass how many GPIOs we have */ |
| 226 | uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT; |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 227 | |
| 228 | dev->priv = calloc(sizeof(struct gpio_state), uc_priv->gpio_count); |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
Simon Glass | 62cb89d | 2014-10-04 11:29:46 -0600 | [diff] [blame] | 233 | static int gpio_sandbox_remove(struct udevice *dev) |
| 234 | { |
| 235 | free(dev->priv); |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
Simon Glass | ae7f451 | 2014-06-11 23:29:45 -0600 | [diff] [blame] | 240 | static const struct udevice_id sandbox_gpio_ids[] = { |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 241 | { .compatible = "sandbox,gpio" }, |
| 242 | { } |
| 243 | }; |
| 244 | |
| 245 | U_BOOT_DRIVER(gpio_sandbox) = { |
| 246 | .name = "gpio_sandbox", |
| 247 | .id = UCLASS_GPIO, |
| 248 | .of_match = sandbox_gpio_ids, |
| 249 | .ofdata_to_platdata = sandbox_gpio_ofdata_to_platdata, |
| 250 | .probe = gpio_sandbox_probe, |
Simon Glass | 62cb89d | 2014-10-04 11:29:46 -0600 | [diff] [blame] | 251 | .remove = gpio_sandbox_remove, |
Simon Glass | e2d8a71 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 252 | .ops = &gpio_sandbox_ops, |
| 253 | }; |