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