Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The Chromium OS Authors. |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 3 | * SPDX-License-Identifier: GPL-2.0+ |
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> |
| 9 | #include <malloc.h> |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 10 | #include <asm/gpio.h> |
Simon Glass | 12faa02 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 11 | #include <dm/of.h> |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 12 | #include <dt-bindings/gpio/gpio.h> |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 13 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 14 | DECLARE_GLOBAL_DATA_PTR; |
| 15 | |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 16 | /* Flags for each GPIO */ |
| 17 | #define GPIOF_OUTPUT (1 << 0) /* Currently set as an output */ |
| 18 | #define GPIOF_HIGH (1 << 1) /* Currently set high */ |
mario.six@gdsys.cc | 90937cc | 2016-05-25 15:15:23 +0200 | [diff] [blame] | 19 | #define GPIOF_ODR (1 << 2) /* Currently set to open drain mode */ |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 20 | |
| 21 | struct gpio_state { |
| 22 | const char *label; /* label given by requester */ |
| 23 | u8 flags; /* flags (GPIOF_...) */ |
| 24 | }; |
| 25 | |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 26 | /* Access routines for GPIO state */ |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 27 | static u8 *get_gpio_flags(struct udevice *dev, unsigned 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 | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 33 | static u8 invalid_flags; |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 34 | printf("sandbox_gpio: error: invalid gpio %u\n", offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 35 | return &invalid_flags; |
| 36 | } |
| 37 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 38 | return &state[offset].flags; |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 41 | static int get_gpio_flag(struct udevice *dev, unsigned offset, int flag) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 42 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 43 | return (*get_gpio_flags(dev, offset) & flag) != 0; |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 46 | static int set_gpio_flag(struct udevice *dev, unsigned offset, int flag, |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 47 | int value) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 48 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 49 | u8 *gpio = get_gpio_flags(dev, offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 50 | |
| 51 | if (value) |
| 52 | *gpio |= flag; |
| 53 | else |
| 54 | *gpio &= ~flag; |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 59 | /* |
| 60 | * Back-channel sandbox-internal-only access to GPIO state |
| 61 | */ |
| 62 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 63 | int sandbox_gpio_get_value(struct udevice *dev, unsigned offset) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 64 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 65 | if (get_gpio_flag(dev, offset, GPIOF_OUTPUT)) |
| 66 | debug("sandbox_gpio: get_value on output gpio %u\n", offset); |
| 67 | return get_gpio_flag(dev, offset, GPIOF_HIGH); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 70 | int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 71 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 72 | return set_gpio_flag(dev, offset, GPIOF_HIGH, value); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 73 | } |
| 74 | |
mario.six@gdsys.cc | 90937cc | 2016-05-25 15:15:23 +0200 | [diff] [blame] | 75 | int sandbox_gpio_get_open_drain(struct udevice *dev, unsigned offset) |
| 76 | { |
| 77 | return get_gpio_flag(dev, offset, GPIOF_ODR); |
| 78 | } |
| 79 | |
| 80 | int sandbox_gpio_set_open_drain(struct udevice *dev, unsigned offset, int value) |
| 81 | { |
| 82 | return set_gpio_flag(dev, offset, GPIOF_ODR, value); |
| 83 | } |
| 84 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 85 | int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 86 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 87 | return get_gpio_flag(dev, offset, GPIOF_OUTPUT); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 90 | int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 91 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 92 | return set_gpio_flag(dev, offset, GPIOF_OUTPUT, output); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /* |
| 96 | * These functions implement the public interface within U-Boot |
| 97 | */ |
| 98 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 99 | /* set GPIO port 'offset' as an input */ |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 100 | static int sb_gpio_direction_input(struct udevice *dev, unsigned offset) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 101 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 102 | debug("%s: offset:%u\n", __func__, offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 103 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 104 | return sandbox_gpio_set_direction(dev, offset, 0); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 107 | /* set GPIO port 'offset' as an output, with polarity 'value' */ |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 108 | static int sb_gpio_direction_output(struct udevice *dev, unsigned offset, |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 109 | int value) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 110 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 111 | debug("%s: offset:%u, value = %d\n", __func__, offset, value); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 112 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 113 | return sandbox_gpio_set_direction(dev, offset, 1) | |
| 114 | sandbox_gpio_set_value(dev, offset, value); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 117 | /* read GPIO IN value of port 'offset' */ |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 118 | static int sb_gpio_get_value(struct udevice *dev, unsigned offset) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 119 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 120 | debug("%s: offset:%u\n", __func__, offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 121 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 122 | return sandbox_gpio_get_value(dev, offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 125 | /* write GPIO OUT value to port 'offset' */ |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 126 | 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] | 127 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 128 | debug("%s: offset:%u, value = %d\n", __func__, offset, value); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 129 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 130 | if (!sandbox_gpio_get_direction(dev, offset)) { |
| 131 | printf("sandbox_gpio: error: set_value on input gpio %u\n", |
| 132 | offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 133 | return -1; |
| 134 | } |
| 135 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 136 | return sandbox_gpio_set_value(dev, offset, value); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 137 | } |
| 138 | |
mario.six@gdsys.cc | 90937cc | 2016-05-25 15:15:23 +0200 | [diff] [blame] | 139 | /* read GPIO ODR value of port 'offset' */ |
| 140 | static int sb_gpio_get_open_drain(struct udevice *dev, unsigned offset) |
| 141 | { |
| 142 | debug("%s: offset:%u\n", __func__, offset); |
| 143 | |
| 144 | return sandbox_gpio_get_open_drain(dev, offset); |
| 145 | } |
| 146 | |
| 147 | /* write GPIO ODR value to port 'offset' */ |
| 148 | static int sb_gpio_set_open_drain(struct udevice *dev, unsigned offset, int value) |
| 149 | { |
| 150 | debug("%s: offset:%u, value = %d\n", __func__, offset, value); |
| 151 | |
| 152 | if (!sandbox_gpio_get_direction(dev, offset)) { |
| 153 | printf("sandbox_gpio: error: set_open_drain on input gpio %u\n", |
| 154 | offset); |
| 155 | return -1; |
| 156 | } |
| 157 | |
| 158 | return sandbox_gpio_set_open_drain(dev, offset, value); |
| 159 | } |
| 160 | |
Simon Glass | 3204863 | 2014-10-04 11:29:45 -0600 | [diff] [blame] | 161 | static int sb_gpio_get_function(struct udevice *dev, unsigned offset) |
| 162 | { |
| 163 | if (get_gpio_flag(dev, offset, GPIOF_OUTPUT)) |
| 164 | return GPIOF_OUTPUT; |
| 165 | return GPIOF_INPUT; |
| 166 | } |
| 167 | |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 168 | static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc, |
Simon Glass | 12faa02 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 169 | struct ofnode_phandle_args *args) |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 170 | { |
| 171 | desc->offset = args->args[0]; |
| 172 | if (args->args_count < 2) |
| 173 | return 0; |
| 174 | if (args->args[1] & GPIO_ACTIVE_LOW) |
| 175 | desc->flags |= GPIOD_ACTIVE_LOW; |
| 176 | if (args->args[1] & 2) |
| 177 | desc->flags |= GPIOD_IS_IN; |
| 178 | if (args->args[1] & 4) |
| 179 | desc->flags |= GPIOD_IS_OUT; |
| 180 | if (args->args[1] & 8) |
| 181 | desc->flags |= GPIOD_IS_OUT_ACTIVE; |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 186 | static const struct dm_gpio_ops gpio_sandbox_ops = { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 187 | .direction_input = sb_gpio_direction_input, |
| 188 | .direction_output = sb_gpio_direction_output, |
| 189 | .get_value = sb_gpio_get_value, |
| 190 | .set_value = sb_gpio_set_value, |
mario.six@gdsys.cc | 90937cc | 2016-05-25 15:15:23 +0200 | [diff] [blame] | 191 | .get_open_drain = sb_gpio_get_open_drain, |
| 192 | .set_open_drain = sb_gpio_set_open_drain, |
Simon Glass | 3204863 | 2014-10-04 11:29:45 -0600 | [diff] [blame] | 193 | .get_function = sb_gpio_get_function, |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 194 | .xlate = sb_gpio_xlate, |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 195 | }; |
| 196 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 197 | static int sandbox_gpio_ofdata_to_platdata(struct udevice *dev) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 198 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 199 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 200 | |
Simon Glass | 1185fcb | 2017-05-18 20:09:20 -0600 | [diff] [blame] | 201 | uc_priv->gpio_count = dev_read_u32_default(dev, "num-gpios", 0); |
| 202 | uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name"); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 203 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 204 | return 0; |
| 205 | } |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 206 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 207 | static int gpio_sandbox_probe(struct udevice *dev) |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 208 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 209 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 210 | |
Simon Glass | 1185fcb | 2017-05-18 20:09:20 -0600 | [diff] [blame] | 211 | if (!dev_of_valid(dev)) |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 212 | /* Tell the uclass how many GPIOs we have */ |
| 213 | uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT; |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 214 | |
| 215 | dev->priv = calloc(sizeof(struct gpio_state), uc_priv->gpio_count); |
| 216 | |
| 217 | return 0; |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 218 | } |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 219 | |
Simon Glass | a367f1d | 2014-10-04 11:29:46 -0600 | [diff] [blame] | 220 | static int gpio_sandbox_remove(struct udevice *dev) |
| 221 | { |
| 222 | free(dev->priv); |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
Simon Glass | 767827a | 2014-06-11 23:29:45 -0600 | [diff] [blame] | 227 | static const struct udevice_id sandbox_gpio_ids[] = { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 228 | { .compatible = "sandbox,gpio" }, |
| 229 | { } |
| 230 | }; |
| 231 | |
| 232 | U_BOOT_DRIVER(gpio_sandbox) = { |
| 233 | .name = "gpio_sandbox", |
| 234 | .id = UCLASS_GPIO, |
| 235 | .of_match = sandbox_gpio_ids, |
| 236 | .ofdata_to_platdata = sandbox_gpio_ofdata_to_platdata, |
| 237 | .probe = gpio_sandbox_probe, |
Simon Glass | a367f1d | 2014-10-04 11:29:46 -0600 | [diff] [blame] | 238 | .remove = gpio_sandbox_remove, |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 239 | .ops = &gpio_sandbox_ops, |
| 240 | }; |