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> |
| 11 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 12 | DECLARE_GLOBAL_DATA_PTR; |
| 13 | |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 14 | /* Flags for each GPIO */ |
| 15 | #define GPIOF_OUTPUT (1 << 0) /* Currently set as an output */ |
| 16 | #define GPIOF_HIGH (1 << 1) /* Currently set high */ |
| 17 | #define GPIOF_RESERVED (1 << 2) /* Is in use / requested */ |
| 18 | |
| 19 | struct gpio_state { |
| 20 | const char *label; /* label given by requester */ |
| 21 | u8 flags; /* flags (GPIOF_...) */ |
| 22 | }; |
| 23 | |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 24 | /* Access routines for GPIO state */ |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 25 | static u8 *get_gpio_flags(struct udevice *dev, unsigned offset) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 26 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 27 | struct gpio_dev_priv *uc_priv = dev->uclass_priv; |
| 28 | struct gpio_state *state = dev_get_priv(dev); |
| 29 | |
| 30 | if (offset >= uc_priv->gpio_count) { |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 31 | static u8 invalid_flags; |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 32 | printf("sandbox_gpio: error: invalid gpio %u\n", offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 33 | return &invalid_flags; |
| 34 | } |
| 35 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 36 | return &state[offset].flags; |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 39 | static int get_gpio_flag(struct udevice *dev, unsigned offset, int flag) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 40 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 41 | return (*get_gpio_flags(dev, offset) & flag) != 0; |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 44 | static int set_gpio_flag(struct udevice *dev, unsigned offset, int flag, |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 45 | int value) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 46 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 47 | u8 *gpio = get_gpio_flags(dev, offset); |
Simon Glass | b43d044 | 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 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 57 | static int check_reserved(struct udevice *dev, unsigned offset, |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 58 | const char *func) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 59 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 60 | if (!get_gpio_flag(dev, offset, GPIOF_RESERVED)) { |
| 61 | printf("sandbox_gpio: %s: error: offset %u not reserved\n", |
| 62 | func, offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Back-channel sandbox-internal-only access to GPIO state |
| 71 | */ |
| 72 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 73 | int sandbox_gpio_get_value(struct udevice *dev, unsigned offset) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 74 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 75 | if (get_gpio_flag(dev, offset, GPIOF_OUTPUT)) |
| 76 | debug("sandbox_gpio: get_value on output gpio %u\n", offset); |
| 77 | return get_gpio_flag(dev, offset, GPIOF_HIGH); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 80 | int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 81 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 82 | return set_gpio_flag(dev, offset, GPIOF_HIGH, value); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 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 | if (check_reserved(dev, offset, __func__)) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 105 | return -1; |
| 106 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 107 | return sandbox_gpio_set_direction(dev, offset, 0); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 110 | /* set GPIO port 'offset' as an output, with polarity 'value' */ |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 111 | static int sb_gpio_direction_output(struct udevice *dev, unsigned offset, |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 112 | int value) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 113 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 114 | debug("%s: offset:%u, value = %d\n", __func__, offset, value); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 115 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 116 | if (check_reserved(dev, offset, __func__)) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 117 | return -1; |
| 118 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 119 | return sandbox_gpio_set_direction(dev, offset, 1) | |
| 120 | sandbox_gpio_set_value(dev, offset, value); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 123 | /* read GPIO IN value of port 'offset' */ |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 124 | static int sb_gpio_get_value(struct udevice *dev, unsigned offset) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 125 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 126 | debug("%s: offset:%u\n", __func__, offset); |
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 | if (check_reserved(dev, offset, __func__)) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 129 | return -1; |
| 130 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 131 | return sandbox_gpio_get_value(dev, offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 134 | /* write GPIO OUT value to port 'offset' */ |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 135 | 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] | 136 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 137 | debug("%s: offset:%u, value = %d\n", __func__, offset, value); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 138 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 139 | if (check_reserved(dev, offset, __func__)) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 140 | return -1; |
| 141 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 142 | if (!sandbox_gpio_get_direction(dev, offset)) { |
| 143 | printf("sandbox_gpio: error: set_value on input gpio %u\n", |
| 144 | offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 145 | return -1; |
| 146 | } |
| 147 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 148 | return sandbox_gpio_set_value(dev, offset, value); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 151 | static int sb_gpio_request(struct udevice *dev, unsigned offset, |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 152 | const char *label) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 153 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 154 | struct gpio_dev_priv *uc_priv = dev->uclass_priv; |
| 155 | struct gpio_state *state = dev_get_priv(dev); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 156 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 157 | debug("%s: offset:%u, label:%s\n", __func__, offset, label); |
| 158 | |
| 159 | if (offset >= uc_priv->gpio_count) { |
| 160 | printf("sandbox_gpio: error: invalid gpio %u\n", offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 161 | return -1; |
| 162 | } |
| 163 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 164 | if (get_gpio_flag(dev, offset, GPIOF_RESERVED)) { |
| 165 | printf("sandbox_gpio: error: gpio %u already reserved\n", |
| 166 | offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 167 | return -1; |
| 168 | } |
| 169 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 170 | state[offset].label = label; |
| 171 | return set_gpio_flag(dev, offset, GPIOF_RESERVED, 1); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 174 | static int sb_gpio_free(struct udevice *dev, unsigned offset) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 175 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 176 | struct gpio_state *state = dev_get_priv(dev); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 177 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 178 | debug("%s: offset:%u\n", __func__, offset); |
| 179 | |
| 180 | if (check_reserved(dev, offset, __func__)) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 181 | return -1; |
| 182 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 183 | state[offset].label = NULL; |
| 184 | return set_gpio_flag(dev, offset, GPIOF_RESERVED, 0); |
| 185 | } |
| 186 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 187 | static int sb_gpio_get_state(struct udevice *dev, unsigned int offset, |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 188 | char *buf, int bufsize) |
| 189 | { |
| 190 | struct gpio_dev_priv *uc_priv = dev->uclass_priv; |
| 191 | struct gpio_state *state = dev_get_priv(dev); |
| 192 | const char *label; |
| 193 | |
| 194 | label = state[offset].label; |
| 195 | snprintf(buf, bufsize, "%s%d: %s: %d [%c]%s%s", |
| 196 | uc_priv->bank_name ? uc_priv->bank_name : "", offset, |
| 197 | sandbox_gpio_get_direction(dev, offset) ? "out" : " in", |
| 198 | sandbox_gpio_get_value(dev, offset), |
| 199 | get_gpio_flag(dev, offset, GPIOF_RESERVED) ? 'x' : ' ', |
| 200 | label ? " " : "", |
| 201 | label ? label : ""); |
| 202 | |
| 203 | return 0; |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Simon Glass | 3204863 | 2014-10-04 11:29:45 -0600 | [diff] [blame^] | 206 | static int sb_gpio_get_function(struct udevice *dev, unsigned offset) |
| 207 | { |
| 208 | if (get_gpio_flag(dev, offset, GPIOF_OUTPUT)) |
| 209 | return GPIOF_OUTPUT; |
| 210 | return GPIOF_INPUT; |
| 211 | } |
| 212 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 213 | static const struct dm_gpio_ops gpio_sandbox_ops = { |
| 214 | .request = sb_gpio_request, |
| 215 | .free = sb_gpio_free, |
| 216 | .direction_input = sb_gpio_direction_input, |
| 217 | .direction_output = sb_gpio_direction_output, |
| 218 | .get_value = sb_gpio_get_value, |
| 219 | .set_value = sb_gpio_set_value, |
Simon Glass | 3204863 | 2014-10-04 11:29:45 -0600 | [diff] [blame^] | 220 | .get_function = sb_gpio_get_function, |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 221 | }; |
| 222 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 223 | static int sandbox_gpio_ofdata_to_platdata(struct udevice *dev) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 224 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 225 | struct gpio_dev_priv *uc_priv = dev->uclass_priv; |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 226 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 227 | uc_priv->gpio_count = fdtdec_get_int(gd->fdt_blob, dev->of_offset, |
| 228 | "num-gpios", 0); |
| 229 | uc_priv->bank_name = fdt_getprop(gd->fdt_blob, dev->of_offset, |
| 230 | "gpio-bank-name", NULL); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 231 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 232 | return 0; |
| 233 | } |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 234 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 235 | static int gpio_sandbox_probe(struct udevice *dev) |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 236 | { |
| 237 | struct gpio_dev_priv *uc_priv = dev->uclass_priv; |
| 238 | |
| 239 | if (dev->of_offset == -1) { |
| 240 | /* Tell the uclass how many GPIOs we have */ |
| 241 | uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT; |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 242 | } |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 243 | |
| 244 | dev->priv = calloc(sizeof(struct gpio_state), uc_priv->gpio_count); |
| 245 | |
| 246 | return 0; |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 247 | } |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 248 | |
Simon Glass | 767827a | 2014-06-11 23:29:45 -0600 | [diff] [blame] | 249 | static const struct udevice_id sandbox_gpio_ids[] = { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 250 | { .compatible = "sandbox,gpio" }, |
| 251 | { } |
| 252 | }; |
| 253 | |
| 254 | U_BOOT_DRIVER(gpio_sandbox) = { |
| 255 | .name = "gpio_sandbox", |
| 256 | .id = UCLASS_GPIO, |
| 257 | .of_match = sandbox_gpio_ids, |
| 258 | .ofdata_to_platdata = sandbox_gpio_ofdata_to_platdata, |
| 259 | .probe = gpio_sandbox_probe, |
| 260 | .ops = &gpio_sandbox_ops, |
| 261 | }; |