Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Stephen Warren | 0c5403f | 2016-05-25 14:38:51 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2010-2016, NVIDIA CORPORATION. |
| 4 | * (based on tegra_gpio.c) |
Stephen Warren | 0c5403f | 2016-05-25 14:38:51 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
Stephen Warren | 0c5403f | 2016-05-25 14:38:51 -0600 | [diff] [blame] | 7 | #include <dm.h> |
| 8 | #include <malloc.h> |
| 9 | #include <errno.h> |
| 10 | #include <fdtdec.h> |
| 11 | #include <asm/io.h> |
| 12 | #include <asm/bitops.h> |
| 13 | #include <asm/gpio.h> |
| 14 | #include <dm/device-internal.h> |
| 15 | #include <dt-bindings/gpio/gpio.h> |
| 16 | #include "tegra186_gpio_priv.h" |
| 17 | |
Stephen Warren | 0c5403f | 2016-05-25 14:38:51 -0600 | [diff] [blame] | 18 | struct tegra186_gpio_port_data { |
| 19 | const char *name; |
| 20 | uint32_t offset; |
| 21 | }; |
| 22 | |
| 23 | struct tegra186_gpio_ctlr_data { |
| 24 | const struct tegra186_gpio_port_data *ports; |
| 25 | uint32_t port_count; |
| 26 | }; |
| 27 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 28 | struct tegra186_gpio_plat { |
Stephen Warren | 0c5403f | 2016-05-25 14:38:51 -0600 | [diff] [blame] | 29 | const char *name; |
| 30 | uint32_t *regs; |
| 31 | }; |
| 32 | |
| 33 | static uint32_t *tegra186_gpio_reg(struct udevice *dev, uint32_t reg, |
| 34 | uint32_t gpio) |
| 35 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 36 | struct tegra186_gpio_plat *plat = dev_get_plat(dev); |
Stephen Warren | 0c5403f | 2016-05-25 14:38:51 -0600 | [diff] [blame] | 37 | uint32_t index = (reg + (gpio * TEGRA186_GPIO_PER_GPIO_STRIDE)) / 4; |
| 38 | |
| 39 | return &(plat->regs[index]); |
| 40 | } |
| 41 | |
| 42 | static int tegra186_gpio_set_out(struct udevice *dev, unsigned offset, |
| 43 | bool output) |
| 44 | { |
| 45 | uint32_t *reg; |
| 46 | uint32_t rval; |
| 47 | |
| 48 | reg = tegra186_gpio_reg(dev, TEGRA186_GPIO_OUTPUT_CONTROL, offset); |
| 49 | rval = readl(reg); |
| 50 | if (output) |
| 51 | rval &= ~TEGRA186_GPIO_OUTPUT_CONTROL_FLOATED; |
| 52 | else |
| 53 | rval |= TEGRA186_GPIO_OUTPUT_CONTROL_FLOATED; |
| 54 | writel(rval, reg); |
| 55 | |
| 56 | reg = tegra186_gpio_reg(dev, TEGRA186_GPIO_ENABLE_CONFIG, offset); |
| 57 | rval = readl(reg); |
| 58 | if (output) |
| 59 | rval |= TEGRA186_GPIO_ENABLE_CONFIG_OUT; |
| 60 | else |
| 61 | rval &= ~TEGRA186_GPIO_ENABLE_CONFIG_OUT; |
| 62 | rval |= TEGRA186_GPIO_ENABLE_CONFIG_ENABLE; |
| 63 | writel(rval, reg); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | static int tegra186_gpio_set_val(struct udevice *dev, unsigned offset, bool val) |
| 69 | { |
| 70 | uint32_t *reg; |
| 71 | uint32_t rval; |
| 72 | |
| 73 | reg = tegra186_gpio_reg(dev, TEGRA186_GPIO_OUTPUT_VALUE, offset); |
| 74 | rval = readl(reg); |
| 75 | if (val) |
| 76 | rval |= TEGRA186_GPIO_OUTPUT_VALUE_HIGH; |
| 77 | else |
| 78 | rval &= ~TEGRA186_GPIO_OUTPUT_VALUE_HIGH; |
| 79 | writel(rval, reg); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static int tegra186_gpio_direction_input(struct udevice *dev, unsigned offset) |
| 85 | { |
| 86 | return tegra186_gpio_set_out(dev, offset, false); |
| 87 | } |
| 88 | |
| 89 | static int tegra186_gpio_direction_output(struct udevice *dev, unsigned offset, |
| 90 | int value) |
| 91 | { |
| 92 | int ret; |
| 93 | |
| 94 | ret = tegra186_gpio_set_val(dev, offset, value != 0); |
| 95 | if (ret) |
| 96 | return ret; |
| 97 | return tegra186_gpio_set_out(dev, offset, true); |
| 98 | } |
| 99 | |
| 100 | static int tegra186_gpio_get_value(struct udevice *dev, unsigned offset) |
| 101 | { |
| 102 | uint32_t *reg; |
| 103 | uint32_t rval; |
| 104 | |
| 105 | reg = tegra186_gpio_reg(dev, TEGRA186_GPIO_ENABLE_CONFIG, offset); |
| 106 | rval = readl(reg); |
| 107 | |
| 108 | if (rval & TEGRA186_GPIO_ENABLE_CONFIG_OUT) |
| 109 | reg = tegra186_gpio_reg(dev, TEGRA186_GPIO_OUTPUT_VALUE, |
| 110 | offset); |
| 111 | else |
| 112 | reg = tegra186_gpio_reg(dev, TEGRA186_GPIO_INPUT, offset); |
| 113 | |
| 114 | rval = readl(reg); |
| 115 | return !!rval; |
| 116 | } |
| 117 | |
| 118 | static int tegra186_gpio_set_value(struct udevice *dev, unsigned offset, |
| 119 | int value) |
| 120 | { |
| 121 | return tegra186_gpio_set_val(dev, offset, value != 0); |
| 122 | } |
| 123 | |
| 124 | static int tegra186_gpio_get_function(struct udevice *dev, unsigned offset) |
| 125 | { |
| 126 | uint32_t *reg; |
| 127 | uint32_t rval; |
| 128 | |
| 129 | reg = tegra186_gpio_reg(dev, TEGRA186_GPIO_ENABLE_CONFIG, offset); |
| 130 | rval = readl(reg); |
| 131 | if (rval & TEGRA186_GPIO_ENABLE_CONFIG_OUT) |
| 132 | return GPIOF_OUTPUT; |
| 133 | else |
| 134 | return GPIOF_INPUT; |
| 135 | } |
| 136 | |
| 137 | static int tegra186_gpio_xlate(struct udevice *dev, struct gpio_desc *desc, |
Simon Glass | 12faa02 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 138 | struct ofnode_phandle_args *args) |
Stephen Warren | 0c5403f | 2016-05-25 14:38:51 -0600 | [diff] [blame] | 139 | { |
| 140 | int gpio, port, ret; |
| 141 | |
| 142 | gpio = args->args[0]; |
| 143 | port = gpio / TEGRA186_GPIO_PER_GPIO_COUNT; |
| 144 | ret = device_get_child(dev, port, &desc->dev); |
| 145 | if (ret) |
| 146 | return ret; |
| 147 | desc->offset = gpio % TEGRA186_GPIO_PER_GPIO_COUNT; |
| 148 | desc->flags = args->args[1] & GPIO_ACTIVE_LOW ? GPIOD_ACTIVE_LOW : 0; |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static const struct dm_gpio_ops tegra186_gpio_ops = { |
| 154 | .direction_input = tegra186_gpio_direction_input, |
| 155 | .direction_output = tegra186_gpio_direction_output, |
| 156 | .get_value = tegra186_gpio_get_value, |
| 157 | .set_value = tegra186_gpio_set_value, |
| 158 | .get_function = tegra186_gpio_get_function, |
| 159 | .xlate = tegra186_gpio_xlate, |
| 160 | }; |
| 161 | |
| 162 | /** |
| 163 | * We have a top-level GPIO device with no actual GPIOs. It has a child device |
| 164 | * for each port within the controller. |
| 165 | */ |
| 166 | static int tegra186_gpio_bind(struct udevice *parent) |
| 167 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 168 | struct tegra186_gpio_plat *parent_plat = dev_get_plat(parent); |
Stephen Warren | 0c5403f | 2016-05-25 14:38:51 -0600 | [diff] [blame] | 169 | struct tegra186_gpio_ctlr_data *ctlr_data = |
| 170 | (struct tegra186_gpio_ctlr_data *)dev_get_driver_data(parent); |
| 171 | uint32_t *regs; |
| 172 | int port, ret; |
| 173 | |
| 174 | /* If this is a child device, there is nothing to do here */ |
| 175 | if (parent_plat) |
| 176 | return 0; |
| 177 | |
Matthias Schiffer | 4733193 | 2023-09-27 15:33:34 +0200 | [diff] [blame] | 178 | regs = dev_read_addr_name_ptr(parent, "gpio"); |
| 179 | if (!regs) |
Simon Glass | f44b4bf | 2017-09-17 16:54:53 -0600 | [diff] [blame] | 180 | return -EINVAL; |
Stephen Warren | 0c5403f | 2016-05-25 14:38:51 -0600 | [diff] [blame] | 181 | |
| 182 | for (port = 0; port < ctlr_data->port_count; port++) { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 183 | struct tegra186_gpio_plat *plat; |
Stephen Warren | 0c5403f | 2016-05-25 14:38:51 -0600 | [diff] [blame] | 184 | struct udevice *dev; |
| 185 | |
| 186 | plat = calloc(1, sizeof(*plat)); |
| 187 | if (!plat) |
| 188 | return -ENOMEM; |
| 189 | plat->name = ctlr_data->ports[port].name; |
| 190 | plat->regs = &(regs[ctlr_data->ports[port].offset / 4]); |
| 191 | |
Simon Glass | 6996c66 | 2020-11-28 17:50:03 -0700 | [diff] [blame] | 192 | ret = device_bind(parent, parent->driver, plat->name, plat, |
Simon Glass | 9030b39 | 2020-11-28 17:50:04 -0700 | [diff] [blame] | 193 | dev_ofnode(parent), &dev); |
Stephen Warren | 0c5403f | 2016-05-25 14:38:51 -0600 | [diff] [blame] | 194 | if (ret) |
| 195 | return ret; |
Stephen Warren | 0c5403f | 2016-05-25 14:38:51 -0600 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static int tegra186_gpio_probe(struct udevice *dev) |
| 202 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 203 | struct tegra186_gpio_plat *plat = dev_get_plat(dev); |
Stephen Warren | 0c5403f | 2016-05-25 14:38:51 -0600 | [diff] [blame] | 204 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 205 | |
| 206 | /* Only child devices have ports */ |
| 207 | if (!plat) |
| 208 | return 0; |
| 209 | |
| 210 | uc_priv->gpio_count = TEGRA186_GPIO_PER_GPIO_COUNT; |
| 211 | uc_priv->bank_name = plat->name; |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static const struct tegra186_gpio_port_data tegra186_gpio_main_ports[] = { |
| 217 | {"A", 0x2000}, |
| 218 | {"B", 0x3000}, |
| 219 | {"C", 0x3200}, |
| 220 | {"D", 0x3400}, |
| 221 | {"E", 0x2200}, |
| 222 | {"F", 0x2400}, |
| 223 | {"G", 0x4200}, |
| 224 | {"H", 0x1000}, |
| 225 | {"I", 0x0800}, |
| 226 | {"J", 0x5000}, |
| 227 | {"K", 0x5200}, |
| 228 | {"L", 0x1200}, |
| 229 | {"M", 0x5600}, |
| 230 | {"N", 0x0000}, |
| 231 | {"O", 0x0200}, |
| 232 | {"P", 0x4000}, |
| 233 | {"Q", 0x0400}, |
| 234 | {"R", 0x0a00}, |
| 235 | {"T", 0x0600}, |
| 236 | {"X", 0x1400}, |
| 237 | {"Y", 0x1600}, |
| 238 | {"BB", 0x2600}, |
| 239 | {"CC", 0x5400}, |
| 240 | }; |
| 241 | |
| 242 | static const struct tegra186_gpio_ctlr_data tegra186_gpio_main_data = { |
| 243 | .ports = tegra186_gpio_main_ports, |
| 244 | .port_count = ARRAY_SIZE(tegra186_gpio_main_ports), |
| 245 | }; |
| 246 | |
| 247 | static const struct tegra186_gpio_port_data tegra186_gpio_aon_ports[] = { |
| 248 | {"S", 0x0200}, |
| 249 | {"U", 0x0400}, |
| 250 | {"V", 0x0800}, |
| 251 | {"W", 0x0a00}, |
| 252 | {"Z", 0x0e00}, |
| 253 | {"AA", 0x0c00}, |
| 254 | {"EE", 0x0600}, |
| 255 | {"FF", 0x0000}, |
| 256 | }; |
| 257 | |
| 258 | static const struct tegra186_gpio_ctlr_data tegra186_gpio_aon_data = { |
| 259 | .ports = tegra186_gpio_aon_ports, |
| 260 | .port_count = ARRAY_SIZE(tegra186_gpio_aon_ports), |
| 261 | }; |
| 262 | |
| 263 | static const struct udevice_id tegra186_gpio_ids[] = { |
| 264 | { |
| 265 | .compatible = "nvidia,tegra186-gpio", |
| 266 | .data = (ulong)&tegra186_gpio_main_data, |
| 267 | }, |
| 268 | { |
| 269 | .compatible = "nvidia,tegra186-gpio-aon", |
| 270 | .data = (ulong)&tegra186_gpio_aon_data, |
| 271 | }, |
| 272 | { } |
| 273 | }; |
| 274 | |
| 275 | U_BOOT_DRIVER(tegra186_gpio) = { |
| 276 | .name = "tegra186_gpio", |
| 277 | .id = UCLASS_GPIO, |
| 278 | .of_match = tegra186_gpio_ids, |
| 279 | .bind = tegra186_gpio_bind, |
| 280 | .probe = tegra186_gpio_probe, |
| 281 | .ops = &tegra186_gpio_ops, |
Stephen Warren | 0c5403f | 2016-05-25 14:38:51 -0600 | [diff] [blame] | 282 | }; |