Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ian Campbell | abce2c6 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2012 Henrik Nordstrom <henrik@henriknordstrom.net> |
| 4 | * |
| 5 | * Based on earlier arch/arm/cpu/armv7/sunxi/gpio.c: |
| 6 | * |
| 7 | * (C) Copyright 2007-2011 |
| 8 | * Allwinner Technology Co., Ltd. <www.allwinnertech.com> |
| 9 | * Tom Cubie <tangliang@allwinnertech.com> |
Ian Campbell | abce2c6 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <common.h> |
Simon Glass | 7aa9748 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 13 | #include <dm.h> |
| 14 | #include <errno.h> |
| 15 | #include <fdtdec.h> |
| 16 | #include <malloc.h> |
Ian Campbell | abce2c6 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 17 | #include <asm/io.h> |
| 18 | #include <asm/gpio.h> |
Chen-Yu Tsai | 4694dc5 | 2016-07-22 16:12:59 +0800 | [diff] [blame] | 19 | #include <dt-bindings/gpio/gpio.h> |
Andre Przywara | 207ed0a | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 20 | #include <sunxi_gpio.h> |
Ian Campbell | abce2c6 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 21 | |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 22 | /* |
| 23 | * ======================================================================= |
| 24 | * Low level GPIO/pin controller access functions, to be shared by non-DM |
| 25 | * SPL code and the DM pinctrl/GPIO drivers. |
| 26 | * The functions ending in "bank" take a base pointer to a GPIO bank, and |
| 27 | * the pin offset is relative to that bank. |
| 28 | * The functions without "bank" in their name take a linear GPIO number, |
| 29 | * covering all ports, and starting at 0 for PortA. |
| 30 | * ======================================================================= |
| 31 | */ |
| 32 | |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 33 | #define GPIO_BANK(pin) ((pin) >> 5) |
| 34 | #define GPIO_NUM(pin) ((pin) & 0x1f) |
| 35 | |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 36 | #define GPIO_CFG_REG_OFFSET 0x00 |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 37 | #define GPIO_CFG_INDEX(pin) (((pin) & 0x1f) >> 3) |
| 38 | #define GPIO_CFG_OFFSET(pin) ((((pin) & 0x1f) & 0x7) << 2) |
| 39 | |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 40 | #define GPIO_DAT_REG_OFFSET 0x10 |
| 41 | |
| 42 | #define GPIO_DRV_REG_OFFSET 0x14 |
Andre Przywara | 452369c | 2022-09-06 12:12:50 +0100 | [diff] [blame] | 43 | |
| 44 | /* Newer SoCs use a slightly different register layout */ |
| 45 | #ifdef CONFIG_SUNXI_NEW_PINCTRL |
| 46 | /* pin drive strength: 4 bits per pin */ |
| 47 | #define GPIO_DRV_INDEX(pin) ((pin) / 8) |
| 48 | #define GPIO_DRV_OFFSET(pin) (((pin) % 8) * 4) |
| 49 | |
| 50 | #define GPIO_PULL_REG_OFFSET 0x24 |
| 51 | |
| 52 | #else /* older generation pin controllers */ |
| 53 | /* pin drive strength: 2 bits per pin */ |
| 54 | #define GPIO_DRV_INDEX(pin) ((pin) / 16) |
| 55 | #define GPIO_DRV_OFFSET(pin) (((pin) % 16) * 2) |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 56 | |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 57 | #define GPIO_PULL_REG_OFFSET 0x1c |
Andre Przywara | 452369c | 2022-09-06 12:12:50 +0100 | [diff] [blame] | 58 | #endif |
| 59 | |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 60 | #define GPIO_PULL_INDEX(pin) (((pin) & 0x1f) >> 4) |
| 61 | #define GPIO_PULL_OFFSET(pin) ((((pin) & 0x1f) & 0xf) << 1) |
| 62 | |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 63 | static void* BANK_TO_GPIO(int bank) |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 64 | { |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 65 | void *pio_base; |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 66 | |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 67 | if (bank < SUNXI_GPIO_L) { |
| 68 | pio_base = (void *)(uintptr_t)SUNXI_PIO_BASE; |
| 69 | } else { |
| 70 | pio_base = (void *)(uintptr_t)SUNXI_R_PIO_BASE; |
| 71 | bank -= SUNXI_GPIO_L; |
| 72 | } |
| 73 | |
| 74 | return pio_base + bank * SUNXI_PINCTRL_BANK_SIZE; |
| 75 | } |
| 76 | |
| 77 | void sunxi_gpio_set_cfgbank(void *bank_base, int pin_offset, u32 val) |
| 78 | { |
| 79 | u32 index = GPIO_CFG_INDEX(pin_offset); |
| 80 | u32 offset = GPIO_CFG_OFFSET(pin_offset); |
| 81 | |
| 82 | clrsetbits_le32(bank_base + GPIO_CFG_REG_OFFSET + index * 4, |
| 83 | 0xfU << offset, val << offset); |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void sunxi_gpio_set_cfgpin(u32 pin, u32 val) |
| 87 | { |
| 88 | u32 bank = GPIO_BANK(pin); |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 89 | void *pio = BANK_TO_GPIO(bank); |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 90 | |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 91 | sunxi_gpio_set_cfgbank(pio, GPIO_NUM(pin), val); |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 94 | int sunxi_gpio_get_cfgbank(void *bank_base, int pin_offset) |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 95 | { |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 96 | u32 index = GPIO_CFG_INDEX(pin_offset); |
| 97 | u32 offset = GPIO_CFG_OFFSET(pin_offset); |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 98 | u32 cfg; |
| 99 | |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 100 | cfg = readl(bank_base + GPIO_CFG_REG_OFFSET + index * 4); |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 101 | cfg >>= offset; |
| 102 | |
| 103 | return cfg & 0xf; |
| 104 | } |
| 105 | |
| 106 | int sunxi_gpio_get_cfgpin(u32 pin) |
| 107 | { |
| 108 | u32 bank = GPIO_BANK(pin); |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 109 | void *bank_base = BANK_TO_GPIO(bank); |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 110 | |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 111 | return sunxi_gpio_get_cfgbank(bank_base, GPIO_NUM(pin)); |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 114 | static void sunxi_gpio_set_value_bank(void *bank_base, int pin, bool set) |
Andre Przywara | 316ec7f | 2022-09-06 10:07:18 +0100 | [diff] [blame] | 115 | { |
| 116 | u32 mask = 1U << pin; |
| 117 | |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 118 | clrsetbits_le32(bank_base + GPIO_DAT_REG_OFFSET, |
| 119 | set ? 0 : mask, set ? mask : 0); |
Andre Przywara | 316ec7f | 2022-09-06 10:07:18 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 122 | static int sunxi_gpio_get_value_bank(void *bank_base, int pin) |
Andre Przywara | 316ec7f | 2022-09-06 10:07:18 +0100 | [diff] [blame] | 123 | { |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 124 | return !!(readl(bank_base + GPIO_DAT_REG_OFFSET) & (1U << pin)); |
Andre Przywara | 316ec7f | 2022-09-06 10:07:18 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 127 | void sunxi_gpio_set_drv(u32 pin, u32 val) |
| 128 | { |
| 129 | u32 bank = GPIO_BANK(pin); |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 130 | void *bank_base = BANK_TO_GPIO(bank); |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 131 | |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 132 | sunxi_gpio_set_drv_bank(bank_base, GPIO_NUM(pin), val); |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 135 | void sunxi_gpio_set_drv_bank(void *bank_base, u32 pin_offset, u32 val) |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 136 | { |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 137 | u32 index = GPIO_DRV_INDEX(pin_offset); |
| 138 | u32 offset = GPIO_DRV_OFFSET(pin_offset); |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 139 | |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 140 | clrsetbits_le32(bank_base + GPIO_DRV_REG_OFFSET + index * 4, |
| 141 | 0x3U << offset, val << offset); |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void sunxi_gpio_set_pull(u32 pin, u32 val) |
| 145 | { |
| 146 | u32 bank = GPIO_BANK(pin); |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 147 | void *bank_base = BANK_TO_GPIO(bank); |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 148 | |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 149 | sunxi_gpio_set_pull_bank(bank_base, GPIO_NUM(pin), val); |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 150 | } |
| 151 | |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 152 | void sunxi_gpio_set_pull_bank(void *bank_base, int pin_offset, u32 val) |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 153 | { |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 154 | u32 index = GPIO_PULL_INDEX(pin_offset); |
| 155 | u32 offset = GPIO_PULL_OFFSET(pin_offset); |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 156 | |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 157 | clrsetbits_le32(bank_base + GPIO_PULL_REG_OFFSET + index * 4, |
| 158 | 0x3U << offset, val << offset); |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | |
| 162 | /* =========== Non-DM code, used by the SPL. ============ */ |
| 163 | |
Simon Glass | bcee8d6 | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 164 | #if !CONFIG_IS_ENABLED(DM_GPIO) |
Andre Przywara | 316ec7f | 2022-09-06 10:07:18 +0100 | [diff] [blame] | 165 | static void sunxi_gpio_set_value(u32 pin, bool set) |
Ian Campbell | abce2c6 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 166 | { |
Ian Campbell | abce2c6 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 167 | u32 bank = GPIO_BANK(pin); |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 168 | void *pio = BANK_TO_GPIO(bank); |
Ian Campbell | abce2c6 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 169 | |
Andre Przywara | 316ec7f | 2022-09-06 10:07:18 +0100 | [diff] [blame] | 170 | sunxi_gpio_set_value_bank(pio, GPIO_NUM(pin), set); |
Ian Campbell | abce2c6 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Andre Przywara | 316ec7f | 2022-09-06 10:07:18 +0100 | [diff] [blame] | 173 | static int sunxi_gpio_get_value(u32 pin) |
Ian Campbell | abce2c6 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 174 | { |
Ian Campbell | abce2c6 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 175 | u32 bank = GPIO_BANK(pin); |
Andre Przywara | 30097ee | 2022-09-05 18:12:39 +0100 | [diff] [blame] | 176 | void *pio = BANK_TO_GPIO(bank); |
Ian Campbell | abce2c6 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 177 | |
Andre Przywara | 316ec7f | 2022-09-06 10:07:18 +0100 | [diff] [blame] | 178 | return sunxi_gpio_get_value_bank(pio, GPIO_NUM(pin)); |
Ian Campbell | abce2c6 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | int gpio_request(unsigned gpio, const char *label) |
| 182 | { |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | int gpio_free(unsigned gpio) |
| 187 | { |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | int gpio_direction_input(unsigned gpio) |
| 192 | { |
| 193 | sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_INPUT); |
| 194 | |
Axel Lin | b0c4ae1 | 2014-12-20 11:41:25 +0800 | [diff] [blame] | 195 | return 0; |
Ian Campbell | abce2c6 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | int gpio_direction_output(unsigned gpio, int value) |
| 199 | { |
| 200 | sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_OUTPUT); |
Andre Przywara | 316ec7f | 2022-09-06 10:07:18 +0100 | [diff] [blame] | 201 | sunxi_gpio_set_value(gpio, value); |
Ian Campbell | abce2c6 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 202 | |
Andre Przywara | 316ec7f | 2022-09-06 10:07:18 +0100 | [diff] [blame] | 203 | return 0; |
Ian Campbell | abce2c6 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | int gpio_get_value(unsigned gpio) |
| 207 | { |
Andre Przywara | 316ec7f | 2022-09-06 10:07:18 +0100 | [diff] [blame] | 208 | return sunxi_gpio_get_value(gpio); |
Ian Campbell | abce2c6 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | int gpio_set_value(unsigned gpio, int value) |
| 212 | { |
Andre Przywara | 316ec7f | 2022-09-06 10:07:18 +0100 | [diff] [blame] | 213 | sunxi_gpio_set_value(gpio, value); |
| 214 | |
| 215 | return 0; |
Ian Campbell | abce2c6 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | int sunxi_name_to_gpio(const char *name) |
| 219 | { |
| 220 | int group = 0; |
| 221 | int groupsize = 9 * 32; |
| 222 | long pin; |
| 223 | char *eptr; |
Hans de Goede | 6c727e0 | 2014-12-24 19:34:38 +0100 | [diff] [blame] | 224 | |
Ian Campbell | abce2c6 | 2014-06-05 19:00:15 +0100 | [diff] [blame] | 225 | if (*name == 'P' || *name == 'p') |
| 226 | name++; |
| 227 | if (*name >= 'A') { |
| 228 | group = *name - (*name > 'a' ? 'a' : 'A'); |
| 229 | groupsize = 32; |
| 230 | name++; |
| 231 | } |
| 232 | |
| 233 | pin = simple_strtol(name, &eptr, 10); |
| 234 | if (!*name || *eptr) |
| 235 | return -1; |
| 236 | if (pin < 0 || pin > groupsize || group >= 9) |
| 237 | return -1; |
| 238 | return group * 32 + pin; |
| 239 | } |
Andre Przywara | 20b78c5 | 2022-09-06 10:36:38 +0100 | [diff] [blame] | 240 | #endif /* !DM_GPIO */ |
| 241 | |
| 242 | /* =========== DM code, used by U-Boot proper. ============ */ |
Simon Glass | 7aa9748 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 243 | |
Simon Glass | bcee8d6 | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 244 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Simon Glass | a5ab883 | 2015-04-18 11:33:43 -0600 | [diff] [blame] | 245 | /* TODO(sjg@chromium.org): Remove this function and use device tree */ |
| 246 | int sunxi_name_to_gpio(const char *name) |
| 247 | { |
| 248 | unsigned int gpio; |
| 249 | int ret; |
Hans de Goede | f9b7a04 | 2015-04-22 11:31:22 +0200 | [diff] [blame] | 250 | #if !defined CONFIG_SPL_BUILD && defined CONFIG_AXP_GPIO |
| 251 | char lookup[8]; |
Simon Glass | a5ab883 | 2015-04-18 11:33:43 -0600 | [diff] [blame] | 252 | |
Samuel Holland | 09cbd38 | 2023-01-22 17:46:22 -0600 | [diff] [blame] | 253 | if (strcasecmp(name, "AXP0-VBUS-ENABLE") == 0) { |
Hans de Goede | f9b7a04 | 2015-04-22 11:31:22 +0200 | [diff] [blame] | 254 | sprintf(lookup, SUNXI_GPIO_AXP0_PREFIX "%d", |
| 255 | SUNXI_GPIO_AXP0_VBUS_ENABLE); |
| 256 | name = lookup; |
| 257 | } |
| 258 | #endif |
Simon Glass | a5ab883 | 2015-04-18 11:33:43 -0600 | [diff] [blame] | 259 | ret = gpio_lookup_name(name, NULL, NULL, &gpio); |
| 260 | |
| 261 | return ret ? ret : gpio; |
| 262 | } |
| 263 | |
Simon Glass | 7aa9748 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 264 | static int sunxi_gpio_get_value(struct udevice *dev, unsigned offset) |
| 265 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 266 | struct sunxi_gpio_plat *plat = dev_get_plat(dev); |
Simon Glass | 7aa9748 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 267 | |
Andre Przywara | 316ec7f | 2022-09-06 10:07:18 +0100 | [diff] [blame] | 268 | return sunxi_gpio_get_value_bank(plat->regs, offset); |
Simon Glass | 7aa9748 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 269 | } |
| 270 | |
Simon Glass | 7aa9748 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 271 | static int sunxi_gpio_get_function(struct udevice *dev, unsigned offset) |
| 272 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 273 | struct sunxi_gpio_plat *plat = dev_get_plat(dev); |
Simon Glass | 7aa9748 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 274 | int func; |
| 275 | |
| 276 | func = sunxi_gpio_get_cfgbank(plat->regs, offset); |
| 277 | if (func == SUNXI_GPIO_OUTPUT) |
| 278 | return GPIOF_OUTPUT; |
| 279 | else if (func == SUNXI_GPIO_INPUT) |
| 280 | return GPIOF_INPUT; |
| 281 | else |
| 282 | return GPIOF_FUNC; |
| 283 | } |
| 284 | |
Chen-Yu Tsai | 4694dc5 | 2016-07-22 16:12:59 +0800 | [diff] [blame] | 285 | static int sunxi_gpio_xlate(struct udevice *dev, struct gpio_desc *desc, |
Simon Glass | 3a57123 | 2017-05-18 20:09:18 -0600 | [diff] [blame] | 286 | struct ofnode_phandle_args *args) |
Chen-Yu Tsai | 4694dc5 | 2016-07-22 16:12:59 +0800 | [diff] [blame] | 287 | { |
| 288 | int ret; |
| 289 | |
| 290 | ret = device_get_child(dev, args->args[0], &desc->dev); |
| 291 | if (ret) |
| 292 | return ret; |
| 293 | desc->offset = args->args[1]; |
Samuel Holland | 35ae126 | 2021-10-20 23:52:56 -0500 | [diff] [blame] | 294 | desc->flags = gpio_flags_xlate(args->args[2]); |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | static int sunxi_gpio_set_flags(struct udevice *dev, unsigned int offset, |
| 300 | ulong flags) |
| 301 | { |
| 302 | struct sunxi_gpio_plat *plat = dev_get_plat(dev); |
| 303 | |
| 304 | if (flags & GPIOD_IS_OUT) { |
| 305 | u32 value = !!(flags & GPIOD_IS_OUT_ACTIVE); |
Samuel Holland | 35ae126 | 2021-10-20 23:52:56 -0500 | [diff] [blame] | 306 | |
Andre Przywara | 316ec7f | 2022-09-06 10:07:18 +0100 | [diff] [blame] | 307 | sunxi_gpio_set_value_bank(plat->regs, offset, value); |
Samuel Holland | 35ae126 | 2021-10-20 23:52:56 -0500 | [diff] [blame] | 308 | sunxi_gpio_set_cfgbank(plat->regs, offset, SUNXI_GPIO_OUTPUT); |
| 309 | } else if (flags & GPIOD_IS_IN) { |
| 310 | u32 pull = 0; |
| 311 | |
| 312 | if (flags & GPIOD_PULL_UP) |
| 313 | pull = 1; |
| 314 | else if (flags & GPIOD_PULL_DOWN) |
| 315 | pull = 2; |
| 316 | sunxi_gpio_set_pull_bank(plat->regs, offset, pull); |
| 317 | sunxi_gpio_set_cfgbank(plat->regs, offset, SUNXI_GPIO_INPUT); |
| 318 | } |
Chen-Yu Tsai | 4694dc5 | 2016-07-22 16:12:59 +0800 | [diff] [blame] | 319 | |
| 320 | return 0; |
| 321 | } |
| 322 | |
Simon Glass | 7aa9748 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 323 | static const struct dm_gpio_ops gpio_sunxi_ops = { |
Simon Glass | 7aa9748 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 324 | .get_value = sunxi_gpio_get_value, |
Simon Glass | 7aa9748 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 325 | .get_function = sunxi_gpio_get_function, |
Chen-Yu Tsai | 4694dc5 | 2016-07-22 16:12:59 +0800 | [diff] [blame] | 326 | .xlate = sunxi_gpio_xlate, |
Samuel Holland | 35ae126 | 2021-10-20 23:52:56 -0500 | [diff] [blame] | 327 | .set_flags = sunxi_gpio_set_flags, |
Simon Glass | 7aa9748 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 328 | }; |
| 329 | |
Simon Glass | 7aa9748 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 330 | static int gpio_sunxi_probe(struct udevice *dev) |
| 331 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 332 | struct sunxi_gpio_plat *plat = dev_get_plat(dev); |
Simon Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 333 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 7aa9748 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 334 | |
| 335 | /* Tell the uclass how many GPIOs we have */ |
| 336 | if (plat) { |
Samuel Holland | b799eab | 2021-08-12 20:09:43 -0500 | [diff] [blame] | 337 | uc_priv->gpio_count = SUNXI_GPIOS_PER_BANK; |
Simon Glass | 7aa9748 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 338 | uc_priv->bank_name = plat->bank_name; |
| 339 | } |
| 340 | |
| 341 | return 0; |
| 342 | } |
Stephen Warren | 6f82fac | 2016-05-11 15:26:25 -0600 | [diff] [blame] | 343 | |
Simon Glass | 7aa9748 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 344 | U_BOOT_DRIVER(gpio_sunxi) = { |
| 345 | .name = "gpio_sunxi", |
| 346 | .id = UCLASS_GPIO, |
Simon Glass | 7aa9748 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 347 | .probe = gpio_sunxi_probe, |
Samuel Holland | b799eab | 2021-08-12 20:09:43 -0500 | [diff] [blame] | 348 | .ops = &gpio_sunxi_ops, |
Simon Glass | 7aa9748 | 2014-10-30 20:25:49 -0600 | [diff] [blame] | 349 | }; |
Simon Glass | bcee8d6 | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 350 | #endif /* DM_GPIO */ |