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 2/2] gpio: add driver for stmpe io-expander
Date: Fri, 31 Aug 2012 11:14:07 +0200	[thread overview]
Message-ID: <1346404447-31961-3-git-send-email-s.trumtrar@pengutronix.de> (raw)
In-Reply-To: <1346404447-31961-1-git-send-email-s.trumtrar@pengutronix.de>

Add a driver for the stmpe1601 gpio block. The stmpe io-expanders have multiple
blocks (keypad, gpio...) that can be enabled and used. Only gpio is supported atm.

This was only tested on a stmpe connected via i2c. It is also possible to connect
via spi.

Signed-off-by: Steffen Trumtrar <s.trumtrar@pengutronix.de>
---
 drivers/Kconfig           |    1 +
 drivers/gpio/Kconfig      |    8 +++
 drivers/gpio/Makefile     |    1 +
 drivers/gpio/gpio-stmpe.c |  155 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 165 insertions(+)
 create mode 100644 drivers/gpio/Kconfig
 create mode 100644 drivers/gpio/gpio-stmpe.c

diff --git a/drivers/Kconfig b/drivers/Kconfig
index 883b0e7..adf8fcd 100644
--- a/drivers/Kconfig
+++ b/drivers/Kconfig
@@ -18,5 +18,6 @@ source "drivers/input/Kconfig"
 source "drivers/watchdog/Kconfig"
 source "drivers/pwm/Kconfig"
 source "drivers/dma/Kconfig"
+source "drivers/gpio/Kconfig"
 
 endmenu
diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
new file mode 100644
index 0000000..939cda8
--- /dev/null
+++ b/drivers/gpio/Kconfig
@@ -0,0 +1,8 @@
+menu "GPIO                          "
+
+config STMPE_GPIO
+	depends on I2C
+	select I2C_STMPE
+	bool "STMPE GPIO Expander"
+
+endmenu
diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
index 7837966..da1bc21 100644
--- a/drivers/gpio/Makefile
+++ b/drivers/gpio/Makefile
@@ -1 +1,2 @@
 obj-y += gpio.o
+obj-$(CONFIG_STMPE_GPIO) += gpio-stmpe.o
diff --git a/drivers/gpio/gpio-stmpe.c b/drivers/gpio/gpio-stmpe.c
new file mode 100644
index 0000000..44e7ae6
--- /dev/null
+++ b/drivers/gpio/gpio-stmpe.c
@@ -0,0 +1,155 @@
+/*
+ * Copyright (C) 2012 Pengutronix
+ * Steffen Trumtrar <s.trumtrar@pengutronix.de>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include <common.h>
+#include <errno.h>
+#include <io.h>
+#include <gpio.h>
+#include <init.h>
+#include <mfd/stmpe-i2c.h>
+
+#define GPIO_BASE	0x80
+#define GPIO_SET	(GPIO_BASE + 0x02)
+#define GPIO_CLR	(GPIO_BASE + 0x04)
+#define GPIO_MP		(GPIO_BASE + 0x06)
+#define GPIO_SET_DIR	(GPIO_BASE + 0x08)
+#define GPIO_ED		(GPIO_BASE + 0x0a)
+#define GPIO_RE		(GPIO_BASE + 0x0c)
+#define GPIO_FE		(GPIO_BASE + 0x0e)
+#define GPIO_PULL_UP	(GPIO_BASE + 0x10)
+#define GPIO_AF		(GPIO_BASE + 0x12)
+#define GPIO_LT		(GPIO_BASE + 0x16)
+
+#define OFFSET(gpio)	(0xff & (1 << (gpio)) ? 1 : 0)
+
+extern void __iomem *stmpe_gpio_base[];
+extern int stmpe_gpio_count;
+
+struct stmpe_gpio_chip {
+	void __iomem *base;
+	struct gpio_chip chip;
+	void *ci;
+};
+
+static void stmpe_gpio_set_value(struct gpio_chip *chip, unsigned gpio, int value)
+{
+	struct stmpe_gpio_chip *stmpegpio = container_of(chip, struct stmpe_gpio_chip, chip);
+	struct stmpe_client_info *ci = (struct stmpe_client_info *)stmpegpio->ci;
+	int ret;
+	u8 val;
+
+	ci->read_reg(ci->stmpe, GPIO_MP + OFFSET(gpio), &val);
+
+	val |= 1 << (gpio % 8);
+
+	if (value)
+		ret = ci->write_reg(ci->stmpe, GPIO_SET + OFFSET(gpio), val);
+	else
+		ret = ci->write_reg(ci->stmpe, GPIO_CLR + OFFSET(gpio), val);
+
+	if (ret)
+		dev_err(chip->dev, "write failed!\n");
+}
+
+static int stmpe_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
+{
+	struct stmpe_gpio_chip *stmpegpio = container_of(chip, struct stmpe_gpio_chip, chip);
+	struct stmpe_client_info *ci = (struct stmpe_client_info *)stmpegpio->ci;
+	int ret;
+	u8 val;
+
+	ci->read_reg(ci->stmpe, GPIO_SET_DIR + OFFSET(gpio), &val);
+	val &= ~(1 << (gpio % 8));
+	ret = ci->write_reg(ci->stmpe, GPIO_SET_DIR + OFFSET(gpio), val);
+
+	if (ret)
+		dev_err(chip->dev, "couldn't change direction. Write failed!\n");
+
+	return 0;
+}
+
+static int stmpe_gpio_direction_output(struct gpio_chip *chip, unsigned gpio, int value)
+{
+	struct stmpe_gpio_chip *stmpegpio = container_of(chip, struct stmpe_gpio_chip, chip);
+	struct stmpe_client_info *ci = (struct stmpe_client_info *)stmpegpio->ci;
+	int ret;
+	u8 val;
+
+	ci->read_reg(ci->stmpe, GPIO_SET_DIR + OFFSET(gpio), &val);
+	val |= 1 << (gpio % 8);
+	ret = ci->write_reg(ci->stmpe, GPIO_SET_DIR + OFFSET(gpio), val);
+
+	stmpe_gpio_set_value(chip, gpio, value);
+
+	if (ret)
+		dev_err(chip->dev, "couldn't change direction. Write failed!\n");
+
+	return 0;
+}
+
+static int stmpe_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
+{
+	struct stmpe_gpio_chip *stmpegpio = container_of(chip, struct stmpe_gpio_chip, chip);
+	struct stmpe_client_info *ci = (struct stmpe_client_info *)stmpegpio->ci;
+	u8 val;
+	int ret;
+
+	ret = ci->read_reg(ci->stmpe, GPIO_MP + OFFSET(gpio), &val);
+
+	if (ret)
+		dev_err(chip->dev, "read failed\n");
+
+	return val & (1 << (gpio % 8)) ? 1 : 0;
+}
+
+static struct gpio_ops stmpe_gpio_ops = {
+	.direction_input = stmpe_gpio_direction_input,
+	.direction_output = stmpe_gpio_direction_output,
+	.get = stmpe_gpio_get_value,
+	.set = stmpe_gpio_set_value,
+};
+
+static int stmpe_gpio_probe(struct device_d *dev)
+{
+	struct stmpe_gpio_chip *stmpegpio;
+
+	stmpegpio = xzalloc(sizeof(*stmpegpio));
+
+	stmpegpio->base = dev_request_mem_region(dev, 0);
+	stmpegpio->chip.ops = &stmpe_gpio_ops;
+	stmpegpio->chip.base = 224;
+	stmpegpio->chip.ngpio = 16;
+	stmpegpio->chip.dev = dev;
+
+	stmpegpio->ci = dev->platform_data;
+
+	gpiochip_add(&stmpegpio->chip);
+
+	dev_info(dev, "probed stmpe gpiochip%d with base %d\n", dev->id, stmpegpio->chip.base);
+	return 0;
+}
+
+static struct driver_d stmpe_gpio_driver = {
+	.name = "stmpe-gpio",
+	.probe = stmpe_gpio_probe,
+};
+
+static int stmpe_gpio_add(void)
+{
+	register_driver(&stmpe_gpio_driver);
+	return 0;
+}
+coredevice_initcall(stmpe_gpio_add);
-- 
1.7.10.4


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

  parent reply	other threads:[~2012-08-31  9:14 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-31  9:14 [PATCH 0/2] Add stmpe mfd Steffen Trumtrar
2012-08-31  9:14 ` [PATCH 1/2] mfd: add stmpe-i2c driver Steffen Trumtrar
2012-08-31  9:14 ` Steffen Trumtrar [this message]
2012-09-03  7:08   ` [PATCH 2/2] gpio: add driver for stmpe io-expander Sascha Hauer

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=1346404447-31961-3-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