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