linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Luotao Fu <l.fu@pengutronix.de>
To: Samuel Ortiz <sameo@linux.intel.com>,
	Dmitry Torokhov <dmitry.torokhov@gmail.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Mark Brown <broonie@opensource.wolfsonmicro.com>
Cc: linux-input@vger.kernel.org, linux-kernel@vger.kernel.org,
	Luotao Fu <l.fu@pengutronix.de>
Subject: [PATCH 2/3 V3] gpio: add STMPE811 gpio controller support
Date: Tue, 15 Jun 2010 11:00:45 +0200	[thread overview]
Message-ID: <1276592446-26909-3-git-send-email-l.fu@pengutronix.de> (raw)
In-Reply-To: <1276511558-27121-1-git-send-email-l.fu@pengutronix.de>

This one adds a driver for STMPE811 GPIO controller. STMPE811 is a
multifunction device. Hence this driver depends on stmpe811_core
driver for core functionalities. The gpio chip is registered to
gpiolib. Interrupts are currently not supported.

Signed-off-by: Luotao Fu <l.fu@pengutronix.de>
---
V2 changes:
* include subsystem headers since they are now removed from the mfd
  core header file

V3 Changes:
* reformated platform data comments to kernel-doc style
* move platform data stuff from core patch to herein.

 drivers/gpio/Kconfig         |   10 ++
 drivers/gpio/Makefile        |    1 +
 drivers/gpio/stmpe811_gpio.c |  238 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/mfd/stmpe811.h |   16 +++
 4 files changed, 265 insertions(+), 0 deletions(-)
 create mode 100644 drivers/gpio/stmpe811_gpio.c

diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
index 724038d..38307e5 100644
--- a/drivers/gpio/Kconfig
+++ b/drivers/gpio/Kconfig
@@ -249,6 +249,16 @@ config GPIO_ADP5588
 	  To compile this driver as a module, choose M here: the module will be
 	  called adp5588-gpio.
 
+config GPIO_STMPE811
+	tristate "STMPE811 I2C MFD GPIO expander"
+	depends on I2C
+	depends on MFD_STMPE811
+	help
+	  This option enables support for 8 GPIOs found
+	  on STMicroelectronics STMPE811 multifunction devices.
+	  To compile this driver as a module, choose M here: the module will be
+	  called stmpe811_gpio.
+
 comment "PCI GPIO expanders:"
 
 config GPIO_CS5535
diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
index 51c3cdd..7b184aa 100644
--- a/drivers/gpio/Makefile
+++ b/drivers/gpio/Makefile
@@ -31,3 +31,4 @@ obj-$(CONFIG_GPIO_WM8994)	+= wm8994-gpio.o
 obj-$(CONFIG_GPIO_SCH)		+= sch_gpio.o
 obj-$(CONFIG_GPIO_RDC321X)	+= rdc321x-gpio.o
 obj-$(CONFIG_GPIO_JANZ_TTL)	+= janz-ttl.o
+obj-$(CONFIG_GPIO_STMPE811)	+= stmpe811_gpio.o
diff --git a/drivers/gpio/stmpe811_gpio.c b/drivers/gpio/stmpe811_gpio.c
new file mode 100644
index 0000000..507fb5b
--- /dev/null
+++ b/drivers/gpio/stmpe811_gpio.c
@@ -0,0 +1,238 @@
+/*
+ * stmpe811_gpio.c  --  gpiolib support for STMicroelectronics STMPE811 chip.
+ *
+ * (c)2010 Luotao Fu <l.fu@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.
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/interrupt.h>
+#include <linux/slab.h>
+#include <linux/gpio.h>
+#include <linux/platform_device.h>
+#include <linux/i2c.h>
+#include <linux/workqueue.h>
+
+#include <linux/mfd/stmpe811.h>
+
+#define STMPE811_GPIO_NAME	"stmpe811-gpio"
+
+struct stmpe811_gpio {
+	struct stmpe811 *stm;
+	struct gpio_chip gpio;
+};
+
+static inline struct stmpe811_gpio *to_stmpe811_gpio(struct gpio_chip *chip)
+{
+	return container_of(chip, struct stmpe811_gpio, gpio);
+}
+
+static int stmpe811_gpio_direction_in(struct gpio_chip *chip, unsigned offset)
+{
+	struct stmpe811_gpio *stm_gpio = to_stmpe811_gpio(chip);
+
+	return stmpe811_reg_clear_bits(stm_gpio->stm,
+			STMPE811_REG_GPIO_DIR, BIT(offset));
+}
+
+static int stmpe811_gpio_get(struct gpio_chip *chip, unsigned offset)
+{
+	struct stmpe811_gpio *stm_gpio = to_stmpe811_gpio(chip);
+	uint8_t gpio_sta;
+	int ret;
+
+	ret = stmpe811_reg_read(stm_gpio->stm,
+			STMPE811_REG_GPIO_MP_STA, &gpio_sta);
+	if (ret)
+		return ret;
+
+	if (gpio_sta & BIT(offset))
+		return 1;
+	else
+		return 0;
+}
+
+static inline int __stmpe811_gpio_set(struct stmpe811 *stm,
+		unsigned offset, int value)
+{
+	int ret;
+
+	if (value)
+		ret = stmpe811_reg_write(stm, STMPE811_REG_GPIO_SET_PIN,
+				BIT(offset));
+	else
+		ret = stmpe811_reg_write(stm, STMPE811_REG_GPIO_CLR_PIN,
+				BIT(offset));
+
+	return ret;
+}
+
+static int stmpe811_gpio_direction_out(struct gpio_chip *chip, unsigned offset,
+				       int value)
+{
+	struct stmpe811_gpio *stm_gpio = to_stmpe811_gpio(chip);
+	struct stmpe811 *stm = stm_gpio->stm;
+	int ret;
+
+	ret = stmpe811_reg_set_bits(stm, STMPE811_REG_GPIO_DIR, BIT(offset));
+	if (ret)
+		goto out;
+
+	ret = __stmpe811_gpio_set(stm, offset, value);
+out:
+	return ret;
+}
+
+static void stmpe811_gpio_set(struct gpio_chip *chip, unsigned offset,
+			      int value)
+{
+	struct stmpe811_gpio *stm_gpio = to_stmpe811_gpio(chip);
+
+	__stmpe811_gpio_set(stm_gpio->stm, offset, value);
+}
+
+static int stmpe811_gpio_request(struct gpio_chip *chip, unsigned offset)
+{
+	struct stmpe811_gpio *stm_gpio = to_stmpe811_gpio(chip);
+	struct stmpe811 *stm = stm_gpio->stm;
+
+	if (offset > 3 && (stm->active_flag & STMPE811_USE_TS))
+		return 1;
+	else
+		return stmpe811_reg_set_bits(stm, STMPE811_REG_GPIO_AF,
+				BIT(offset));
+}
+
+static struct gpio_chip gpio_chip = {
+	.label = STMPE811_GPIO_NAME,
+	.owner = THIS_MODULE,
+	.direction_input = stmpe811_gpio_direction_in,
+	.get = stmpe811_gpio_get,
+	.direction_output = stmpe811_gpio_direction_out,
+	.set = stmpe811_gpio_set,
+	.request = stmpe811_gpio_request,
+	.can_sleep = 1,
+};
+
+static int __devinit stmpe811_gpio_probe(struct platform_device *pdev)
+{
+	struct stmpe811 *stm = dev_get_drvdata(pdev->dev.parent);
+	struct stmpe811_platform_data *pdata = stm->pdata;
+	struct stmpe811_gpio_platform_data *gpio_pdata = NULL;
+	struct stmpe811_gpio *stm_gpio;
+
+	int ret, tmp;
+
+	stm_gpio = kzalloc(sizeof(*stm_gpio), GFP_KERNEL);
+	if (!stm_gpio)
+		return -ENOMEM;
+
+	stm_gpio->stm = stm;
+	stm_gpio->gpio = gpio_chip;
+	stm_gpio->gpio.ngpio = 8;
+	stm_gpio->gpio.dev = &pdev->dev;
+
+	if (pdata)
+		gpio_pdata = pdata->gpio_pdata;
+
+	if (gpio_pdata && gpio_pdata->gpio_base)
+		stm_gpio->gpio.base = gpio_pdata->gpio_base;
+	else
+		stm_gpio->gpio.base = -1;
+
+	ret = gpiochip_add(&stm_gpio->gpio);
+	if (ret < 0) {
+		dev_err(&pdev->dev, "Could not register gpiochip, %d\n", ret);
+		goto err_free_stmgpio;
+	}
+	/* enable clock supply */
+	ret = stmpe811_reg_clear_bits(stm, STMPE811_REG_SYS_CTRL2,
+			STMPE811_SYS_CTRL2_GPIO_OFF);
+	if (ret) {
+		dev_err(&pdev->dev, "Could not enable clock for gpio\n");
+		goto err_free_gpiochip;
+	}
+
+	if (gpio_pdata && gpio_pdata->setup) {
+		ret = gpio_pdata->setup(stm);
+
+		if (ret != 0) {
+			dev_err(&pdev->dev, "setup hook failed, %d\n",
+					ret);
+			goto err_free_gpiochip;
+		}
+	}
+
+	dev_info(&pdev->dev, "registerd gpios %d..%d on a stmpe811\n",
+			stm_gpio->gpio.base,
+			stm_gpio->gpio.base + stm_gpio->gpio.ngpio - 1);
+	platform_set_drvdata(pdev, stm_gpio);
+
+	return ret;
+
+err_free_gpiochip:
+	tmp = gpiochip_remove(&stm_gpio->gpio);
+	if (tmp) {
+		dev_err(&pdev->dev, "Could net remove gpio chip\n");
+		return ret;
+	}
+err_free_stmgpio:
+	kfree(stm_gpio);
+	return ret;
+}
+
+static int __devexit stmpe811_gpio_remove(struct platform_device *pdev)
+{
+	struct stmpe811_gpio *stm_gpio = platform_get_drvdata(pdev);
+	struct stmpe811_platform_data *pdata = stm_gpio->stm->pdata;
+	struct stmpe811_gpio_platform_data *gpio_pdata = NULL;
+	int ret;
+
+	if (pdata)
+		gpio_pdata = pdata->gpio_pdata;
+
+	if (gpio_pdata && gpio_pdata->remove)
+		gpio_pdata->remove(stm_gpio->stm);
+
+	/* disable clock supply */
+	stmpe811_reg_set_bits(stm_gpio->stm, STMPE811_REG_SYS_CTRL2,
+			STMPE811_SYS_CTRL2_GPIO_OFF);
+
+	ret = gpiochip_remove(&stm_gpio->gpio);
+	if (ret == 0)
+		kfree(stm_gpio);
+
+	return ret;
+}
+
+static struct platform_driver stmpe811_gpio_driver = {
+	.driver.name = STMPE811_GPIO_NAME,
+	.driver.owner = THIS_MODULE,
+	.probe = stmpe811_gpio_probe,
+	.remove = __devexit_p(stmpe811_gpio_remove),
+};
+
+static int __init stmpe811_gpio_init(void)
+{
+	return platform_driver_register(&stmpe811_gpio_driver);
+}
+
+subsys_initcall(stmpe811_gpio_init);
+
+static void __exit stmpe811_gpio_exit(void)
+{
+	platform_driver_unregister(&stmpe811_gpio_driver);
+}
+
+module_exit(stmpe811_gpio_exit);
+
+MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
+MODULE_DESCRIPTION("GPIO interface for STMicroelectronics STMPE811");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:" STMPE811_GPIO_NAME);
diff --git a/include/linux/mfd/stmpe811.h b/include/linux/mfd/stmpe811.h
index 8e2b55e..736b4eb 100644
--- a/include/linux/mfd/stmpe811.h
+++ b/include/linux/mfd/stmpe811.h
@@ -94,12 +94,27 @@ struct stmpe811 {
 };
 
 /**
+ * struct stmpe811_gpio_platform_data - stmpe811 gpio controller platform data
+ *
+ * @gpio_base: number of the chip's first GPIO
+ * @setup: optional callback issued once the GPIOs are valid
+ * @remove: optional callback issued before the GPIOs are invalidated
+ *
+ * */
+struct stmpe811_gpio_platform_data {
+	unsigned int gpio_base;
+	int (*setup) (struct stmpe811 *stm);
+	void (*remove) (struct stmpe811 *stm);
+};
+
+/**
  * struct stmpe811_platform_data - stmpe811 core platform data
  *
  * @flags: define which subdevices should be supported
  * @irq_high: IRQ is active high.
  * @irq_rev_pol: IRQ line is connected with reversed polarity
  * @irq_base: board dependt irq number of the first irq for the irq chip
+ * @gpio_pdata: pointer to gpio controller platform data
  * registered by the core.
  *
  * */
@@ -110,6 +125,7 @@ struct stmpe811_platform_data {
 	unsigned int irq_high;
 	unsigned int irq_rev_pol;
 	int irq_base;
+	struct stmpe811_gpio_platform_data *gpio_pdata;
 };
 
 int stmpe811_block_read(struct stmpe811 *stm, u8 reg, uint len, u8 *val);
-- 
1.7.1


  parent reply	other threads:[~2010-06-15  9:01 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-11 10:13 Patch serie for STMPE811 Luotao Fu
2010-06-11 10:13 ` [PATCH 1/3] mfd: add STMPE811 core support Luotao Fu
2010-06-11 11:03   ` Jonathan Cameron
2010-06-11 11:33     ` Luotao Fu
2010-06-11 11:13   ` Mark Brown
2010-06-11 11:38     ` Luotao Fu
2010-06-11 13:28   ` Wolfram Sang
2010-06-11 10:13 ` [PATCH 2/3] gpio: add STMPE811 gpio controller support Luotao Fu
2010-06-11 10:13 ` [PATCH 3/3] input: STMPE811 touch " Luotao Fu
2010-06-11 11:21   ` Mark Brown
2010-06-11 11:45     ` Luotao Fu
2010-06-14 10:32 ` Patch serie for STMPE811 [V2] Luotao Fu
2010-06-15  9:00   ` Patch serie for STMPE811 [V3] Luotao Fu
2010-06-15  9:00   ` [PATCH 1/3 V3] mfd: add STMPE811 core support Luotao Fu
2010-06-15 14:59     ` Mark Brown
2010-06-15  9:00   ` Luotao Fu [this message]
2010-06-15  9:00   ` [PATCH 3/3 V3] input: STMPE811 touch controller support Luotao Fu
2010-06-14 10:32 ` [PATCH 1/3 V2] mfd: add STMPE811 core support Luotao Fu
2010-06-14 11:50   ` Mark Brown
2010-06-14 12:04     ` Luotao Fu
2010-06-14 10:32 ` [PATCH 2/3 V2] gpio: add STMPE811 gpio controller support Luotao Fu
2010-06-14 11:51   ` Mark Brown
2010-06-14 12:09     ` Luotao Fu
2010-06-14 12:13       ` Mark Brown
     [not found]       ` <20100614121349.GC14604@rakim.wolfsonmicro.main>
2010-06-14 12:28         ` Luotao Fu
2010-06-14 12:34           ` Mark Brown
2010-06-14 10:32 ` [PATCH 3/3 V2] input: STMPE811 touch " Luotao Fu

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=1276592446-26909-3-git-send-email-l.fu@pengutronix.de \
    --to=l.fu@pengutronix.de \
    --cc=akpm@linux-foundation.org \
    --cc=broonie@opensource.wolfsonmicro.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sameo@linux.intel.com \
    /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;
as well as URLs for NNTP newsgroup(s).