1 // SPDX-License-Identifier: GPL-2.0+
5 * Based on earlier arch/arm/cpu/armv7/sunxi/gpio.c:
7 * (C) Copyright 2007-2011
8 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
18 #include <dt-bindings/gpio/gpio.h>
19 #include <sunxi_gpio.h>
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 * =======================================================================
32 #define GPIO_BANK(pin) ((pin) >> 5)
33 #define GPIO_NUM(pin) ((pin) & 0x1f)
35 #define GPIO_CFG_REG_OFFSET 0x00
36 #define GPIO_CFG_INDEX(pin) (((pin) & 0x1f) >> 3)
37 #define GPIO_CFG_OFFSET(pin) ((((pin) & 0x1f) & 0x7) << 2)
39 #define GPIO_DAT_REG_OFFSET 0x10
41 #define GPIO_DRV_REG_OFFSET 0x14
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)
49 #define GPIO_PULL_REG_OFFSET 0x24
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)
56 #define GPIO_PULL_REG_OFFSET 0x1c
59 #define GPIO_PULL_INDEX(pin) (((pin) & 0x1f) >> 4)
60 #define GPIO_PULL_OFFSET(pin) ((((pin) & 0x1f) & 0xf) << 1)
62 static void* BANK_TO_GPIO(int bank)
66 if (bank < SUNXI_GPIO_L) {
67 pio_base = (void *)(uintptr_t)SUNXI_PIO_BASE;
69 pio_base = (void *)(uintptr_t)SUNXI_R_PIO_BASE;
73 return pio_base + bank * SUNXI_PINCTRL_BANK_SIZE;
76 void sunxi_gpio_set_cfgbank(void *bank_base, int pin_offset, u32 val)
78 u32 index = GPIO_CFG_INDEX(pin_offset);
79 u32 offset = GPIO_CFG_OFFSET(pin_offset);
81 clrsetbits_le32(bank_base + GPIO_CFG_REG_OFFSET + index * 4,
82 0xfU << offset, val << offset);
85 void sunxi_gpio_set_cfgpin(u32 pin, u32 val)
87 u32 bank = GPIO_BANK(pin);
88 void *pio = BANK_TO_GPIO(bank);
90 sunxi_gpio_set_cfgbank(pio, GPIO_NUM(pin), val);
93 int sunxi_gpio_get_cfgbank(void *bank_base, int pin_offset)
95 u32 index = GPIO_CFG_INDEX(pin_offset);
96 u32 offset = GPIO_CFG_OFFSET(pin_offset);
99 cfg = readl(bank_base + GPIO_CFG_REG_OFFSET + index * 4);
105 int sunxi_gpio_get_cfgpin(u32 pin)
107 u32 bank = GPIO_BANK(pin);
108 void *bank_base = BANK_TO_GPIO(bank);
110 return sunxi_gpio_get_cfgbank(bank_base, GPIO_NUM(pin));
113 static void sunxi_gpio_set_value_bank(void *bank_base, int pin, bool set)
115 u32 mask = 1U << pin;
117 clrsetbits_le32(bank_base + GPIO_DAT_REG_OFFSET,
118 set ? 0 : mask, set ? mask : 0);
121 static int sunxi_gpio_get_value_bank(void *bank_base, int pin)
123 return !!(readl(bank_base + GPIO_DAT_REG_OFFSET) & (1U << pin));
126 void sunxi_gpio_set_drv(u32 pin, u32 val)
128 u32 bank = GPIO_BANK(pin);
129 void *bank_base = BANK_TO_GPIO(bank);
131 sunxi_gpio_set_drv_bank(bank_base, GPIO_NUM(pin), val);
134 void sunxi_gpio_set_drv_bank(void *bank_base, u32 pin_offset, u32 val)
136 u32 index = GPIO_DRV_INDEX(pin_offset);
137 u32 offset = GPIO_DRV_OFFSET(pin_offset);
139 clrsetbits_le32(bank_base + GPIO_DRV_REG_OFFSET + index * 4,
140 0x3U << offset, val << offset);
143 void sunxi_gpio_set_pull(u32 pin, u32 val)
145 u32 bank = GPIO_BANK(pin);
146 void *bank_base = BANK_TO_GPIO(bank);
148 sunxi_gpio_set_pull_bank(bank_base, GPIO_NUM(pin), val);
151 void sunxi_gpio_set_pull_bank(void *bank_base, int pin_offset, u32 val)
153 u32 index = GPIO_PULL_INDEX(pin_offset);
154 u32 offset = GPIO_PULL_OFFSET(pin_offset);
156 clrsetbits_le32(bank_base + GPIO_PULL_REG_OFFSET + index * 4,
157 0x3U << offset, val << offset);
161 /* =========== Non-DM code, used by the SPL. ============ */
163 #if !CONFIG_IS_ENABLED(DM_GPIO)
164 static void sunxi_gpio_set_value(u32 pin, bool set)
166 u32 bank = GPIO_BANK(pin);
167 void *pio = BANK_TO_GPIO(bank);
169 sunxi_gpio_set_value_bank(pio, GPIO_NUM(pin), set);
172 static int sunxi_gpio_get_value(u32 pin)
174 u32 bank = GPIO_BANK(pin);
175 void *pio = BANK_TO_GPIO(bank);
177 return sunxi_gpio_get_value_bank(pio, GPIO_NUM(pin));
180 int gpio_request(unsigned gpio, const char *label)
185 int gpio_free(unsigned gpio)
190 int gpio_direction_input(unsigned gpio)
192 sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_INPUT);
197 int gpio_direction_output(unsigned gpio, int value)
199 sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_OUTPUT);
200 sunxi_gpio_set_value(gpio, value);
205 int gpio_get_value(unsigned gpio)
207 return sunxi_gpio_get_value(gpio);
210 int gpio_set_value(unsigned gpio, int value)
212 sunxi_gpio_set_value(gpio, value);
217 int sunxi_name_to_gpio(const char *name)
220 int groupsize = 9 * 32;
224 if (*name == 'P' || *name == 'p')
227 group = *name - (*name > 'a' ? 'a' : 'A');
232 pin = simple_strtol(name, &eptr, 10);
235 if (pin < 0 || pin > groupsize || group >= 9)
237 return group * 32 + pin;
239 #endif /* !DM_GPIO */
241 /* =========== DM code, used by U-Boot proper. ============ */
243 #if CONFIG_IS_ENABLED(DM_GPIO)
245 int sunxi_name_to_gpio(const char *name)
249 #if !defined CONFIG_SPL_BUILD && defined CONFIG_AXP_GPIO
252 if (strcasecmp(name, "AXP0-VBUS-ENABLE") == 0) {
253 sprintf(lookup, SUNXI_GPIO_AXP0_PREFIX "%d",
254 SUNXI_GPIO_AXP0_VBUS_ENABLE);
258 ret = gpio_lookup_name(name, NULL, NULL, &gpio);
260 return ret ? ret : gpio;
263 static int sunxi_gpio_get_value(struct udevice *dev, unsigned offset)
265 struct sunxi_gpio_plat *plat = dev_get_plat(dev);
267 return sunxi_gpio_get_value_bank(plat->regs, offset);
270 static int sunxi_gpio_get_function(struct udevice *dev, unsigned offset)
272 struct sunxi_gpio_plat *plat = dev_get_plat(dev);
275 func = sunxi_gpio_get_cfgbank(plat->regs, offset);
276 if (func == SUNXI_GPIO_OUTPUT)
278 else if (func == SUNXI_GPIO_INPUT)
284 static int sunxi_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
285 struct ofnode_phandle_args *args)
289 ret = device_get_child(dev, args->args[0], &desc->dev);
292 desc->offset = args->args[1];
293 desc->flags = gpio_flags_xlate(args->args[2]);
298 static int sunxi_gpio_set_flags(struct udevice *dev, unsigned int offset,
301 struct sunxi_gpio_plat *plat = dev_get_plat(dev);
303 if (flags & GPIOD_IS_OUT) {
304 u32 value = !!(flags & GPIOD_IS_OUT_ACTIVE);
306 sunxi_gpio_set_value_bank(plat->regs, offset, value);
307 sunxi_gpio_set_cfgbank(plat->regs, offset, SUNXI_GPIO_OUTPUT);
308 } else if (flags & GPIOD_IS_IN) {
311 if (flags & GPIOD_PULL_UP)
313 else if (flags & GPIOD_PULL_DOWN)
315 sunxi_gpio_set_pull_bank(plat->regs, offset, pull);
316 sunxi_gpio_set_cfgbank(plat->regs, offset, SUNXI_GPIO_INPUT);
322 static const struct dm_gpio_ops gpio_sunxi_ops = {
323 .get_value = sunxi_gpio_get_value,
324 .get_function = sunxi_gpio_get_function,
325 .xlate = sunxi_gpio_xlate,
326 .set_flags = sunxi_gpio_set_flags,
329 static int gpio_sunxi_probe(struct udevice *dev)
331 struct sunxi_gpio_plat *plat = dev_get_plat(dev);
332 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
334 /* Tell the uclass how many GPIOs we have */
336 uc_priv->gpio_count = SUNXI_GPIOS_PER_BANK;
337 uc_priv->bank_name = plat->bank_name;
343 U_BOOT_DRIVER(gpio_sunxi) = {
344 .name = "gpio_sunxi",
346 .probe = gpio_sunxi_probe,
347 .ops = &gpio_sunxi_ops,