mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Steffen Trumtrar <s.trumtrar@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Steffen Trumtrar <s.trumtrar@pengutronix.de>
Subject: [PATCH 01/10] add gpiolib support
Date: Fri, 31 Aug 2012 10:54:23 +0200	[thread overview]
Message-ID: <1346403272-24446-2-git-send-email-s.trumtrar@pengutronix.de> (raw)
In-Reply-To: <1346403272-24446-1-git-send-email-s.trumtrar@pengutronix.de>

From: Sascha Hauer <s.hauer@pengutronix.de>

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
---
 drivers/Makefile      |    1 +
 drivers/gpio/Makefile |    1 +
 drivers/gpio/gpio.c   |  134 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/gpio.h        |   32 +++++++++---
 4 files changed, 160 insertions(+), 8 deletions(-)
 create mode 100644 drivers/gpio/Makefile
 create mode 100644 drivers/gpio/gpio.c

diff --git a/drivers/Makefile b/drivers/Makefile
index ea3263f..1d14e6c 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -17,3 +17,4 @@ obj-$(CONFIG_PWM) += pwm/
 obj-y	+= input/
 obj-y	+= dma/
 obj-y  += watchdog/
+obj-y	+= gpio/
diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
new file mode 100644
index 0000000..7837966
--- /dev/null
+++ b/drivers/gpio/Makefile
@@ -0,0 +1 @@
+obj-y += gpio.o
diff --git a/drivers/gpio/gpio.c b/drivers/gpio/gpio.c
new file mode 100644
index 0000000..6ad8d27
--- /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;
+}
diff --git a/include/gpio.h b/include/gpio.h
index b7d8402..903fa06 100644
--- a/include/gpio.h
+++ b/include/gpio.h
@@ -3,12 +3,28 @@
 
 #include <asm/gpio.h>
 
-static inline int gpio_request(unsigned gpio, const char *label)
-{
-	return 0;
-}
-
-static inline void gpio_free(unsigned gpio)
-{
-}
+struct gpio_chip;
+
+struct gpio_ops {
+	int (*direction_input)(struct gpio_chip *chip, unsigned offset);
+	int (*direction_output)(struct gpio_chip *chip, unsigned offset, int value);
+	int (*get)(struct gpio_chip *chip, unsigned offset);
+	void (*set)(struct gpio_chip *chip, unsigned offset, int value);
+};
+
+struct gpio_chip {
+	struct device_d *dev;
+
+	int base;
+	int ngpio;
+
+	struct gpio_ops *ops;
+
+	struct list_head list;
+};
+
+int gpiochip_add(struct gpio_chip *chip);
+
+int gpio_get_num(struct device_d *dev, int gpio);
+
 #endif /* __GPIO_H */
-- 
1.7.10.4


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  reply	other threads:[~2012-08-31  8:54 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-31  8:54 [PATCH 00/10] ARM i.MX: " Steffen Trumtrar
2012-08-31  8:54 ` Steffen Trumtrar [this message]
2012-09-03  7:00   ` [PATCH 01/10] " Sascha Hauer
2012-09-03  7:03   ` Sascha Hauer
2012-08-31  8:54 ` [PATCH 02/10] ARM i.MX: switch to " Steffen Trumtrar
2012-08-31  8:54 ` [PATCH 03/10] ARM i.MX5: add imx-gpio devices Steffen Trumtrar
2012-09-03  7:01   ` Sascha Hauer
2012-08-31  8:54 ` [PATCH 04/10] ARM i.MX6: " Steffen Trumtrar
2012-08-31  8:54 ` [PATCH 05/10] ARM i.MX53: " Steffen Trumtrar
2012-08-31  8:54 ` [PATCH 06/10] ARM i.MX35: " Steffen Trumtrar
2012-08-31  8:54 ` [PATCH 07/10] ARM i.MX27: " Steffen Trumtrar
2012-08-31  8:54 ` [PATCH 08/10] ARM i.MX25: " Steffen Trumtrar
2012-08-31  8:54 ` [PATCH 09/10] ARM i.MX1: " Steffen Trumtrar
2012-08-31  8:54 ` [PATCH 10/10] ARM i.MX21: " Steffen Trumtrar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1346403272-24446-2-git-send-email-s.trumtrar@pengutronix.de \
    --to=s.trumtrar@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox