diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2012-09-03 10:18:08 +0200 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2012-09-04 08:26:49 +0200 |
commit | 2f8fe263824151b55487f701718ac4467240ad7a (patch) | |
tree | 68774350c087425dcb26e8a561eacddbd03260d9 /drivers/gpio | |
parent | f64bd3d932ec033f02715a5e92b57f52821a4b06 (diff) | |
download | barebox-2f8fe263824151b55487f701718ac4467240ad7a.tar.gz barebox-2f8fe263824151b55487f701718ac4467240ad7a.tar.xz |
add gpiolib support
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
Diffstat (limited to 'drivers/gpio')
-rw-r--r-- | drivers/gpio/Kconfig | 2 | ||||
-rw-r--r-- | drivers/gpio/Makefile | 1 | ||||
-rw-r--r-- | drivers/gpio/gpio.c | 134 |
3 files changed, 137 insertions, 0 deletions
diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig new file mode 100644 index 0000000000..a681973d2b --- /dev/null +++ b/drivers/gpio/Kconfig @@ -0,0 +1,2 @@ +config GPIOLIB + bool diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile new file mode 100644 index 0000000000..25abbce345 --- /dev/null +++ b/drivers/gpio/Makefile @@ -0,0 +1 @@ +obj-$(CONFIG_GPIOLIB) += gpio.o diff --git a/drivers/gpio/gpio.c b/drivers/gpio/gpio.c new file mode 100644 index 0000000000..6ad8d274e7 --- /dev/null +++ b/drivers/gpio/gpio.c @@ -0,0 +1,134 @@ +#include <common.h> +#include <gpio.h> +#include <errno.h> + +static LIST_HEAD(chip_list); + +#define ARCH_NR_GPIOS 256 + +static struct gpio_chip *gpio_desc[ARCH_NR_GPIOS]; + +static int gpio_is_valid(unsigned gpio) +{ + if (gpio < ARCH_NR_GPIOS) + return 1; + return 0; +} + +void gpio_set_value(unsigned gpio, int value) +{ + struct gpio_chip *chip = gpio_desc[gpio]; + + if (!gpio_is_valid(gpio)) + return; + if (!chip) + return; + if (!chip->ops->set) + return; + chip->ops->set(chip, gpio - chip->base, value); +} +EXPORT_SYMBOL(gpio_set_value); + +int gpio_get_value(unsigned gpio) +{ + struct gpio_chip *chip = gpio_desc[gpio]; + + if (!gpio_is_valid(gpio)) + return -EINVAL; + if (!chip) + return -ENODEV; + if (!chip->ops->get) + return -ENOSYS; + return chip->ops->get(chip, gpio - chip->base); +} +EXPORT_SYMBOL(gpio_get_value); + +int gpio_direction_output(unsigned gpio, int value) +{ + struct gpio_chip *chip = gpio_desc[gpio]; + + if (!gpio_is_valid(gpio)) + return -EINVAL; + if (!chip) + return -ENODEV; + if (!chip->ops->direction_output) + return -ENOSYS; + return chip->ops->direction_output(chip, gpio - chip->base, value); +} +EXPORT_SYMBOL(gpio_direction_output); + +int gpio_direction_input(unsigned gpio) +{ + struct gpio_chip *chip = gpio_desc[gpio]; + + if (!gpio_is_valid(gpio)) + return -EINVAL; + if (!chip) + return -ENODEV; + if (!chip->ops->direction_input) + return -ENOSYS; + return chip->ops->direction_input(chip, gpio - chip->base); +} +EXPORT_SYMBOL(gpio_direction_input); + +static int gpiochip_find_base(int start, int ngpio) +{ + int i; + int spare = 0; + int base = -ENOSPC; + + if (start < 0) + start = 0; + + for (i = start; i < ARCH_NR_GPIOS; i++) { + struct gpio_chip *chip = gpio_desc[i]; + + if (!chip) { + spare++; + if (spare == ngpio) { + base = i + 1 - ngpio; + break; + } + } else { + spare = 0; + i += chip->ngpio - 1; + } + } + + if (gpio_is_valid(base)) + debug("%s: found new base at %d\n", __func__, base); + return base; +} + +int gpiochip_add(struct gpio_chip *chip) +{ + int base, i; + + base = gpiochip_find_base(chip->base, chip->ngpio); + if (base < 0) + return base; + + if (chip->base >= 0 && chip->base != base) + return -EBUSY; + + chip->base = base; + + list_add_tail(&chip->list, &chip_list); + + for (i = chip->base; i < chip->base + chip->ngpio; i++) + gpio_desc[i] = chip; + + return 0; +} + +int gpio_get_num(struct device_d *dev, int gpio) +{ + struct gpio_chip *chip; + + list_for_each_entry(chip, &chip_list, list) { + if (chip->dev == dev) + return chip->base + gpio; + } + + return -ENODEV; +} |