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 | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 11 | #include <asm/gpio.h> |
Patrick Delaunay | 1b4a22f | 2020-01-13 11:35:15 +0100 | [diff] [blame] | 12 | #include <dm/device_compat.h> |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 13 | #include <dm/lists.h> |
Simon Glass | 12faa02 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 14 | #include <dm/of.h> |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 15 | #include <dm/pinctrl.h> |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 16 | #include <dt-bindings/gpio/gpio.h> |
Patrick Delaunay | 23aee61 | 2020-01-13 11:35:13 +0100 | [diff] [blame] | 17 | #include <dt-bindings/gpio/sandbox-gpio.h> |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 18 | |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 19 | |
| 20 | struct gpio_state { |
| 21 | const char *label; /* label given by requester */ |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 22 | ulong dir_flags; /* dir_flags (GPIOD_...) */ |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 23 | }; |
| 24 | |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 25 | /* Access routines for GPIO dir flags */ |
| 26 | static ulong *get_gpio_dir_flags(struct udevice *dev, unsigned int offset) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 27 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 28 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 29 | struct gpio_state *state = dev_get_priv(dev); |
| 30 | |
| 31 | if (offset >= uc_priv->gpio_count) { |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 32 | static ulong invalid_dir_flags; |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 33 | printf("sandbox_gpio: error: invalid gpio %u\n", offset); |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 34 | return &invalid_dir_flags; |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 37 | return &state[offset].dir_flags; |
| 38 | |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 41 | 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] | 42 | { |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 43 | return (*get_gpio_dir_flags(dev, offset) & flag) != 0; |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 46 | 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] | 47 | int value) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 48 | { |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 49 | ulong *gpio = get_gpio_dir_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 | { |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 65 | if (get_gpio_flag(dev, offset, GPIOD_IS_OUT)) |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 66 | debug("sandbox_gpio: get_value on output gpio %u\n", offset); |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 67 | return get_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE); |
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 | { |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 72 | return set_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE, value); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 75 | int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 76 | { |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 77 | return get_gpio_flag(dev, offset, GPIOD_IS_OUT); |
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_direction(struct udevice *dev, unsigned offset, int output) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 81 | { |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 82 | set_gpio_flag(dev, offset, GPIOD_IS_OUT, output); |
| 83 | set_gpio_flag(dev, offset, GPIOD_IS_IN, !(output)); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | ulong sandbox_gpio_get_dir_flags(struct udevice *dev, unsigned int offset) |
| 89 | { |
| 90 | return *get_gpio_dir_flags(dev, offset); |
| 91 | } |
| 92 | |
| 93 | int sandbox_gpio_set_dir_flags(struct udevice *dev, unsigned int offset, |
| 94 | ulong flags) |
| 95 | { |
| 96 | *get_gpio_dir_flags(dev, offset) = flags; |
| 97 | |
| 98 | return 0; |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* |
| 102 | * These functions implement the public interface within U-Boot |
| 103 | */ |
| 104 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 105 | /* set GPIO port 'offset' as an input */ |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 106 | static int sb_gpio_direction_input(struct udevice *dev, unsigned offset) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 107 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 108 | debug("%s: offset:%u\n", __func__, offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 109 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 110 | return sandbox_gpio_set_direction(dev, offset, 0); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 113 | /* set GPIO port 'offset' as an output, with polarity 'value' */ |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 114 | static int sb_gpio_direction_output(struct udevice *dev, unsigned offset, |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 115 | int value) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 116 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 117 | debug("%s: offset:%u, value = %d\n", __func__, offset, value); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 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 | return sandbox_gpio_get_value(dev, offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 131 | /* write GPIO OUT value to port 'offset' */ |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 132 | 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] | 133 | { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 134 | debug("%s: offset:%u, value = %d\n", __func__, offset, value); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 135 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 136 | if (!sandbox_gpio_get_direction(dev, offset)) { |
| 137 | printf("sandbox_gpio: error: set_value on input gpio %u\n", |
| 138 | offset); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 139 | return -1; |
| 140 | } |
| 141 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 142 | return sandbox_gpio_set_value(dev, offset, value); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Simon Glass | 3204863 | 2014-10-04 11:29:45 -0600 | [diff] [blame] | 145 | static int sb_gpio_get_function(struct udevice *dev, unsigned offset) |
| 146 | { |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 147 | if (get_gpio_flag(dev, offset, GPIOD_IS_OUT)) |
Simon Glass | 3204863 | 2014-10-04 11:29:45 -0600 | [diff] [blame] | 148 | return GPIOF_OUTPUT; |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 149 | if (get_gpio_flag(dev, offset, GPIOD_IS_IN)) |
| 150 | return GPIOF_INPUT; |
| 151 | |
| 152 | return GPIOF_INPUT; /*GPIO is not configurated */ |
Simon Glass | 3204863 | 2014-10-04 11:29:45 -0600 | [diff] [blame] | 153 | } |
| 154 | |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 155 | static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc, |
Simon Glass | 12faa02 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 156 | struct ofnode_phandle_args *args) |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 157 | { |
| 158 | desc->offset = args->args[0]; |
| 159 | if (args->args_count < 2) |
| 160 | return 0; |
Patrick Delaunay | 23aee61 | 2020-01-13 11:35:13 +0100 | [diff] [blame] | 161 | /* treat generic binding with gpio uclass */ |
| 162 | gpio_xlate_offs_flags(dev, desc, args); |
| 163 | |
| 164 | /* sandbox test specific, not defined in gpio.h */ |
| 165 | if (args->args[1] & GPIO_IN) |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 166 | desc->flags |= GPIOD_IS_IN; |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 167 | |
Patrick Delaunay | 23aee61 | 2020-01-13 11:35:13 +0100 | [diff] [blame] | 168 | if (args->args[1] & GPIO_OUT) |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 169 | desc->flags |= GPIOD_IS_OUT; |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 170 | |
Patrick Delaunay | 23aee61 | 2020-01-13 11:35:13 +0100 | [diff] [blame] | 171 | if (args->args[1] & GPIO_OUT_ACTIVE) |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 172 | desc->flags |= GPIOD_IS_OUT_ACTIVE; |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 177 | static int sb_gpio_set_dir_flags(struct udevice *dev, unsigned int offset, |
| 178 | ulong flags) |
| 179 | { |
| 180 | ulong *dir_flags; |
| 181 | |
| 182 | debug("%s: offset:%u, dir_flags = %lx\n", __func__, offset, flags); |
| 183 | |
| 184 | dir_flags = get_gpio_dir_flags(dev, offset); |
| 185 | |
| 186 | *dir_flags = flags; |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | static int sb_gpio_get_dir_flags(struct udevice *dev, unsigned int offset, |
| 192 | ulong *flags) |
| 193 | { |
| 194 | debug("%s: offset:%u\n", __func__, offset); |
| 195 | *flags = *get_gpio_dir_flags(dev, offset); |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 200 | static const struct dm_gpio_ops gpio_sandbox_ops = { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 201 | .direction_input = sb_gpio_direction_input, |
| 202 | .direction_output = sb_gpio_direction_output, |
| 203 | .get_value = sb_gpio_get_value, |
| 204 | .set_value = sb_gpio_set_value, |
Simon Glass | 3204863 | 2014-10-04 11:29:45 -0600 | [diff] [blame] | 205 | .get_function = sb_gpio_get_function, |
Simon Glass | 16e1040 | 2015-01-05 20:05:29 -0700 | [diff] [blame] | 206 | .xlate = sb_gpio_xlate, |
Patrick Delaunay | 28bdaa5 | 2020-01-13 11:35:14 +0100 | [diff] [blame] | 207 | .set_dir_flags = sb_gpio_set_dir_flags, |
| 208 | .get_dir_flags = sb_gpio_get_dir_flags, |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 209 | }; |
| 210 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 211 | static int sandbox_gpio_ofdata_to_platdata(struct udevice *dev) |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 212 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 213 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 214 | |
Simon Glass | 9e7ab23 | 2018-02-03 10:36:59 -0700 | [diff] [blame] | 215 | uc_priv->gpio_count = dev_read_u32_default(dev, "sandbox,gpio-count", |
| 216 | 0); |
Simon Glass | 1185fcb | 2017-05-18 20:09:20 -0600 | [diff] [blame] | 217 | uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name"); |
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 | return 0; |
| 220 | } |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 221 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 222 | static int gpio_sandbox_probe(struct udevice *dev) |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 223 | { |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 224 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 225 | |
Simon Glass | 1185fcb | 2017-05-18 20:09:20 -0600 | [diff] [blame] | 226 | if (!dev_of_valid(dev)) |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 227 | /* Tell the uclass how many GPIOs we have */ |
| 228 | uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT; |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 229 | |
| 230 | dev->priv = calloc(sizeof(struct gpio_state), uc_priv->gpio_count); |
| 231 | |
| 232 | return 0; |
Simon Glass | b43d044 | 2012-02-15 15:51:13 -0800 | [diff] [blame] | 233 | } |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 234 | |
Simon Glass | a367f1d | 2014-10-04 11:29:46 -0600 | [diff] [blame] | 235 | static int gpio_sandbox_remove(struct udevice *dev) |
| 236 | { |
| 237 | free(dev->priv); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
Simon Glass | 767827a | 2014-06-11 23:29:45 -0600 | [diff] [blame] | 242 | static const struct udevice_id sandbox_gpio_ids[] = { |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 243 | { .compatible = "sandbox,gpio" }, |
| 244 | { } |
| 245 | }; |
| 246 | |
Walter Lozano | 2901ac6 | 2020-06-25 01:10:04 -0300 | [diff] [blame] | 247 | U_BOOT_DRIVER(sandbox_gpio) = { |
| 248 | .name = "sandbox_gpio", |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 249 | .id = UCLASS_GPIO, |
| 250 | .of_match = sandbox_gpio_ids, |
| 251 | .ofdata_to_platdata = sandbox_gpio_ofdata_to_platdata, |
| 252 | .probe = gpio_sandbox_probe, |
Simon Glass | a367f1d | 2014-10-04 11:29:46 -0600 | [diff] [blame] | 253 | .remove = gpio_sandbox_remove, |
Simon Glass | b4d7070 | 2014-02-26 15:59:25 -0700 | [diff] [blame] | 254 | .ops = &gpio_sandbox_ops, |
| 255 | }; |
Patrick Delaunay | 1b4a22f | 2020-01-13 11:35:15 +0100 | [diff] [blame] | 256 | |
Walter Lozano | 48e5b04 | 2020-06-25 01:10:06 -0300 | [diff] [blame] | 257 | U_BOOT_DRIVER_ALIAS(sandbox_gpio, sandbox_gpio_alias) |
| 258 | |
Patrick Delaunay | 1b4a22f | 2020-01-13 11:35:15 +0100 | [diff] [blame] | 259 | /* pincontrol: used only to check GPIO pin configuration (pinmux command) */ |
| 260 | |
| 261 | struct sb_pinctrl_priv { |
| 262 | int pinctrl_ngpios; |
| 263 | struct list_head gpio_dev; |
| 264 | }; |
| 265 | |
| 266 | struct sb_gpio_bank { |
| 267 | struct udevice *gpio_dev; |
| 268 | struct list_head list; |
| 269 | }; |
| 270 | |
| 271 | static int sb_populate_gpio_dev_list(struct udevice *dev) |
| 272 | { |
| 273 | struct sb_pinctrl_priv *priv = dev_get_priv(dev); |
| 274 | struct udevice *gpio_dev; |
| 275 | struct udevice *child; |
| 276 | struct sb_gpio_bank *gpio_bank; |
| 277 | int ret; |
| 278 | |
| 279 | /* |
| 280 | * parse pin-controller sub-nodes (ie gpio bank nodes) and fill |
| 281 | * a list with all gpio device reference which belongs to the |
| 282 | * current pin-controller. This list is used to find pin_name and |
| 283 | * pin muxing |
| 284 | */ |
| 285 | list_for_each_entry(child, &dev->child_head, sibling_node) { |
| 286 | ret = uclass_get_device_by_name(UCLASS_GPIO, child->name, |
| 287 | &gpio_dev); |
| 288 | if (ret < 0) |
| 289 | continue; |
| 290 | |
| 291 | gpio_bank = malloc(sizeof(*gpio_bank)); |
| 292 | if (!gpio_bank) { |
| 293 | dev_err(dev, "Not enough memory\n"); |
| 294 | return -ENOMEM; |
| 295 | } |
| 296 | |
| 297 | gpio_bank->gpio_dev = gpio_dev; |
| 298 | list_add_tail(&gpio_bank->list, &priv->gpio_dev); |
| 299 | } |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | static int sb_pinctrl_get_pins_count(struct udevice *dev) |
| 305 | { |
| 306 | struct sb_pinctrl_priv *priv = dev_get_priv(dev); |
| 307 | struct gpio_dev_priv *uc_priv; |
| 308 | struct sb_gpio_bank *gpio_bank; |
| 309 | |
| 310 | /* |
| 311 | * if get_pins_count has already been executed once on this |
| 312 | * pin-controller, no need to run it again |
| 313 | */ |
| 314 | if (priv->pinctrl_ngpios) |
| 315 | return priv->pinctrl_ngpios; |
| 316 | |
| 317 | if (list_empty(&priv->gpio_dev)) |
| 318 | sb_populate_gpio_dev_list(dev); |
| 319 | /* |
| 320 | * walk through all banks to retrieve the pin-controller |
| 321 | * pins number |
| 322 | */ |
| 323 | list_for_each_entry(gpio_bank, &priv->gpio_dev, list) { |
| 324 | uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev); |
| 325 | |
| 326 | priv->pinctrl_ngpios += uc_priv->gpio_count; |
| 327 | } |
| 328 | |
| 329 | return priv->pinctrl_ngpios; |
| 330 | } |
| 331 | |
| 332 | static struct udevice *sb_pinctrl_get_gpio_dev(struct udevice *dev, |
| 333 | unsigned int selector, |
| 334 | unsigned int *idx) |
| 335 | { |
| 336 | struct sb_pinctrl_priv *priv = dev_get_priv(dev); |
| 337 | struct sb_gpio_bank *gpio_bank; |
| 338 | struct gpio_dev_priv *uc_priv; |
| 339 | int pin_count = 0; |
| 340 | |
| 341 | if (list_empty(&priv->gpio_dev)) |
| 342 | sb_populate_gpio_dev_list(dev); |
| 343 | |
| 344 | /* look up for the bank which owns the requested pin */ |
| 345 | list_for_each_entry(gpio_bank, &priv->gpio_dev, list) { |
| 346 | uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev); |
| 347 | |
| 348 | if (selector < (pin_count + uc_priv->gpio_count)) { |
| 349 | /* |
| 350 | * we found the bank, convert pin selector to |
| 351 | * gpio bank index |
| 352 | */ |
| 353 | *idx = selector - pin_count; |
| 354 | |
| 355 | return gpio_bank->gpio_dev; |
| 356 | } |
| 357 | pin_count += uc_priv->gpio_count; |
| 358 | } |
| 359 | |
| 360 | return NULL; |
| 361 | } |
| 362 | |
| 363 | static const char *sb_pinctrl_get_pin_name(struct udevice *dev, |
| 364 | unsigned int selector) |
| 365 | { |
| 366 | struct gpio_dev_priv *uc_priv; |
| 367 | struct udevice *gpio_dev; |
| 368 | unsigned int gpio_idx; |
| 369 | static char pin_name[PINNAME_SIZE]; |
| 370 | |
| 371 | /* look up for the bank which owns the requested pin */ |
| 372 | gpio_dev = sb_pinctrl_get_gpio_dev(dev, selector, &gpio_idx); |
| 373 | if (!gpio_dev) { |
| 374 | snprintf(pin_name, PINNAME_SIZE, "Error"); |
| 375 | } else { |
| 376 | uc_priv = dev_get_uclass_priv(gpio_dev); |
| 377 | |
| 378 | snprintf(pin_name, PINNAME_SIZE, "%s%d", |
| 379 | uc_priv->bank_name, |
| 380 | gpio_idx); |
| 381 | } |
| 382 | |
| 383 | return pin_name; |
| 384 | } |
| 385 | |
| 386 | static char *get_dir_flags_string(ulong flags) |
| 387 | { |
| 388 | if (flags & GPIOD_OPEN_DRAIN) |
| 389 | return "drive-open-drain"; |
| 390 | if (flags & GPIOD_OPEN_SOURCE) |
| 391 | return "drive-open-source"; |
| 392 | if (flags & GPIOD_PULL_UP) |
| 393 | return "bias-pull-up"; |
| 394 | if (flags & GPIOD_PULL_DOWN) |
| 395 | return "bias-pull-down"; |
| 396 | return "."; |
| 397 | } |
| 398 | |
| 399 | static int sb_pinctrl_get_pin_muxing(struct udevice *dev, |
| 400 | unsigned int selector, |
| 401 | char *buf, int size) |
| 402 | { |
| 403 | struct udevice *gpio_dev; |
| 404 | unsigned int gpio_idx; |
| 405 | ulong dir_flags; |
| 406 | int function; |
| 407 | |
| 408 | /* look up for the bank which owns the requested pin */ |
| 409 | gpio_dev = sb_pinctrl_get_gpio_dev(dev, selector, &gpio_idx); |
| 410 | if (!gpio_dev) { |
| 411 | snprintf(buf, size, "Error"); |
| 412 | } else { |
| 413 | function = sb_gpio_get_function(gpio_dev, gpio_idx); |
| 414 | dir_flags = *get_gpio_dir_flags(gpio_dev, gpio_idx); |
| 415 | |
| 416 | snprintf(buf, size, "gpio %s %s", |
| 417 | function == GPIOF_OUTPUT ? "output" : "input", |
| 418 | get_dir_flags_string(dir_flags)); |
| 419 | } |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | static int sandbox_pinctrl_probe(struct udevice *dev) |
| 425 | { |
| 426 | struct sb_pinctrl_priv *priv = dev_get_priv(dev); |
| 427 | |
| 428 | INIT_LIST_HEAD(&priv->gpio_dev); |
| 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | static struct pinctrl_ops sandbox_pinctrl_gpio_ops = { |
| 434 | .get_pin_name = sb_pinctrl_get_pin_name, |
| 435 | .get_pins_count = sb_pinctrl_get_pins_count, |
| 436 | .get_pin_muxing = sb_pinctrl_get_pin_muxing, |
| 437 | }; |
| 438 | |
| 439 | static const struct udevice_id sandbox_pinctrl_gpio_match[] = { |
| 440 | { .compatible = "sandbox,pinctrl-gpio" }, |
| 441 | { /* sentinel */ } |
| 442 | }; |
| 443 | |
| 444 | U_BOOT_DRIVER(sandbox_pinctrl_gpio) = { |
| 445 | .name = "sandbox_pinctrl_gpio", |
| 446 | .id = UCLASS_PINCTRL, |
| 447 | .of_match = sandbox_pinctrl_gpio_match, |
| 448 | .ops = &sandbox_pinctrl_gpio_ops, |
| 449 | .bind = dm_scan_fdt_dev, |
| 450 | .probe = sandbox_pinctrl_probe, |
| 451 | .priv_auto_alloc_size = sizeof(struct sb_pinctrl_priv), |
| 452 | }; |