1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright (C) 2012 Stefan Roese <sr@denx.de> 4 */ 5 6 /* 7 * Driver for SPEAr600 GPIO controller 8 */ 9 10 #include <common.h> 11 #include <asm/arch/hardware.h> 12 #include <asm/gpio.h> 13 #include <asm/io.h> 14 #include <errno.h> 15 16 static int gpio_direction(unsigned gpio, 17 enum gpio_direction direction) 18 { 19 struct gpio_regs *regs = (struct gpio_regs *)CONFIG_GPIO_BASE; 20 u32 val; 21 22 val = readl(®s->gpiodir); 23 24 if (direction == GPIO_DIRECTION_OUT) 25 val |= 1 << gpio; 26 else 27 val &= ~(1 << gpio); 28 29 writel(val, ®s->gpiodir); 30 31 return 0; 32 } 33 34 int gpio_set_value(unsigned gpio, int value) 35 { 36 struct gpio_regs *regs = (struct gpio_regs *)CONFIG_GPIO_BASE; 37 38 if (value) 39 writel(1 << gpio, ®s->gpiodata[DATA_REG_ADDR(gpio)]); 40 else 41 writel(0, ®s->gpiodata[DATA_REG_ADDR(gpio)]); 42 43 return 0; 44 } 45 46 int gpio_get_value(unsigned gpio) 47 { 48 struct gpio_regs *regs = (struct gpio_regs *)CONFIG_GPIO_BASE; 49 u32 val; 50 51 val = readl(®s->gpiodata[DATA_REG_ADDR(gpio)]); 52 53 return !!val; 54 } 55 56 int gpio_request(unsigned gpio, const char *label) 57 { 58 if (gpio >= SPEAR_GPIO_COUNT) 59 return -EINVAL; 60 61 return 0; 62 } 63 64 int gpio_free(unsigned gpio) 65 { 66 return 0; 67 } 68 69 void gpio_toggle_value(unsigned gpio) 70 { 71 gpio_set_value(gpio, !gpio_get_value(gpio)); 72 } 73 74 int gpio_direction_input(unsigned gpio) 75 { 76 return gpio_direction(gpio, GPIO_DIRECTION_IN); 77 } 78 79 int gpio_direction_output(unsigned gpio, int value) 80 { 81 int ret = gpio_direction(gpio, GPIO_DIRECTION_OUT); 82 83 if (ret < 0) 84 return ret; 85 86 gpio_set_value(gpio, value); 87 return 0; 88 } 89