3 * Gabriel Huau <contact@huau-gabriel.fr>
5 * SPDX-License-Identifier: GPL-2.0+
8 #include <asm/arch/s3c2440.h>
13 #define GPIO_INPUT 0x0
14 #define GPIO_OUTPUT 0x1
16 #define S3C_GPIO_CON 0x0
17 #define S3C_GPIO_DAT 0x4
19 static uint32_t s3c_gpio_get_bank_addr(unsigned gpio)
21 /* There is up to 16 pins per bank, one bank is 0x10 big. */
22 uint32_t addr = gpio & ~0xf;
24 if (addr >= 0x80 && addr != 0xd0) { /* Wrong GPIO bank. */
25 printf("Invalid GPIO bank (bank %02x)\n", addr);
29 return addr | S3C24X0_GPIO_BASE;
32 int gpio_set_value(unsigned gpio, int value)
34 uint32_t addr = s3c_gpio_get_bank_addr(gpio);
36 if (addr == 0xffffffff)
40 setbits_le32(addr | S3C_GPIO_DAT, 1 << (gpio & 0xf));
42 clrbits_le32(addr | S3C_GPIO_DAT, 1 << (gpio & 0xf));
47 int gpio_get_value(unsigned gpio)
49 uint32_t addr = s3c_gpio_get_bank_addr(gpio);
51 if (addr == 0xffffffff)
54 return !!(readl(addr | S3C_GPIO_DAT) & (1 << (gpio & 0xf)));
57 int gpio_request(unsigned gpio, const char *label)
62 int gpio_free(unsigned gpio)
67 static int s3c_gpio_direction(unsigned gpio, uint8_t dir)
69 uint32_t addr = s3c_gpio_get_bank_addr(gpio);
70 const uint32_t mask = 0x3 << ((gpio & 0xf) << 1);
71 const uint32_t dirm = dir << ((gpio & 0xf) << 1);
73 if (addr == 0xffffffff)
76 clrsetbits_le32(addr | S3C_GPIO_CON, mask, dirm);
80 int gpio_direction_input(unsigned gpio)
82 return s3c_gpio_direction(gpio, GPIO_INPUT);
85 int gpio_direction_output(unsigned gpio, int value)
87 return s3c_gpio_direction(gpio, GPIO_OUTPUT);