All of lore.kernel.org
 help / color / mirror / Atom feed
From: Anton Vorontsov <avorontsov@ru.mvista.com>
To: Grant Likely <grant.likely@secretlab.ca>,
	David Brownell <dbrownell@users.sourceforge.net>
Cc: Bill Gatliff <bgat@billgatliff.com>,
	Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>,
	linux-kernel@vger.kernel.org, linuxppc-dev@ozlabs.org,
	Andrew Morton <akpm@linux-foundation.org>
Subject: [PATCH 3/4] of/gpio: Implement GPIOLIB notifier hooks
Date: Mon, 25 Jan 2010 21:11:12 +0300	[thread overview]
Message-ID: <20100125181112.GC13805@oksana.dev.rtsoft.ru> (raw)
In-Reply-To: <20100125180957.GA5380@oksana.dev.rtsoft.ru>

This patch implements GPIOLIB notifier hooks, and thus makes device-enabled
GPIO chips (i.e. the ones that have gpio_chip->dev specified) automatically
attached to the OpenFirmware subsystem. Which means that now we can handle
I2C and SPI GPIO chips almost* transparently.

* "Almost" because some chips still require platform data, and for these
  chips OF-glue is still needed, though with this support the glue will
  be much smaller.

Signed-off-by: Anton Vorontsov <avorontsov@ru.mvista.com>
---
 drivers/of/Kconfig |    1 +
 drivers/of/gpio.c  |  100 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 101 insertions(+), 0 deletions(-)

diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig
index d2fa27c..de9f987 100644
--- a/drivers/of/Kconfig
+++ b/drivers/of/Kconfig
@@ -5,6 +5,7 @@ config OF_DEVICE
 config OF_GPIO
 	def_bool y
 	depends on OF && (PPC_OF || MICROBLAZE) && GPIOLIB
+	select GPIOLIB_NOTIFIER
 	help
 	  OpenFirmware GPIO accessors
 
diff --git a/drivers/of/gpio.c b/drivers/of/gpio.c
index 12c4af0..9d8df77 100644
--- a/drivers/of/gpio.c
+++ b/drivers/of/gpio.c
@@ -13,6 +13,7 @@
 
 #include <linux/kernel.h>
 #include <linux/errno.h>
+#include <linux/notifier.h>
 #include <linux/io.h>
 #include <linux/of.h>
 #include <linux/of_gpio.h>
@@ -236,3 +237,102 @@ err0:
 	return ret;
 }
 EXPORT_SYMBOL(of_mm_gpiochip_add);
+
+/**
+ * of_gpiochip_register_simple - Register a chip with the OF GPIO subsystem
+ * @chip	pointer to a GPIO chip
+ * @np:		device node to register the GPIO chip with
+ *
+ * This function registers a GPIO chip with the OF infrastructure. It is
+ * assumed that the chip was previsously allocated and added to a generic
+ * GPIOLIB framework (using gpiochip_add() function).
+ *
+ * The `simple' name means that the chip is using simple two-cells scheme for
+ * the gpio-specifier.
+ */
+static int of_gpiochip_register_simple(struct gpio_chip *chip,
+				       struct device_node *np)
+{
+	struct of_gpio_chip *of_gc;
+
+	if (np->data) {
+		WARN_ON(1);
+		return -EBUSY;
+	}
+
+	of_gc = kzalloc(sizeof(*of_gc), GFP_KERNEL);
+	if (!of_gc)
+		return -ENOMEM;
+
+	of_gc->gpio_cells = 2;
+	of_gc->xlate = of_gpio_simple_xlate;
+	of_gc->chip = chip;
+	np->data = of_gc;
+	of_node_get(np);
+
+	return 0;
+}
+EXPORT_SYMBOL(of_gpiochip_register_simple);
+
+/**
+ * of_gpiochip_unregister - Unregister a GPIO chip
+ * @chip	pointer to a GPIO chip
+ * @np:		device node for which the GPIO chip was previously registered
+ *
+ * This function unregisters a GPIO chip that was previsously registered
+ * with of_gpiochip_register*().
+ */
+static int of_gpiochip_unregister(struct gpio_chip *chip,
+				  struct device_node *np)
+{
+	struct of_gpio_chip *of_gc = np->data;
+
+	if (!of_gc || of_gc->chip != chip) {
+		WARN_ON(1);
+		return -EINVAL;
+	}
+
+	np->data = NULL;
+	kfree(of_gc);
+	of_node_put(np);
+
+	return 0;
+}
+
+static int of_gpio_notify(struct notifier_block *nb, unsigned long msg,
+			  void *chip)
+{
+	struct gpio_chip *gc = chip;
+	struct device_node *np;
+	int ret = 0;
+
+	if (!gc->dev)
+		return NOTIFY_DONE;
+
+	np = dev_archdata_get_node(&gc->dev->archdata);
+	if (!np)
+		return NOTIFY_DONE;
+
+	switch (msg) {
+	case GPIO_NOTIFY_CHIP_ADDED:
+		ret = of_gpiochip_register_simple(gc, np);
+		break;
+	case GPIO_NOTIFY_CHIP_REMOVE:
+		ret = of_gpiochip_unregister(gc, np);
+		break;
+	default:
+		break;
+	}
+
+	return ret ? notifier_from_errno(ret) : NOTIFY_OK;
+}
+
+static struct notifier_block of_gpio_nb = {
+	.notifier_call = of_gpio_notify,
+};
+
+static int __init of_gpio_notifier_init(void)
+{
+	return blocking_notifier_chain_register(&gpio_notifier, &of_gpio_nb);
+}
+arch_initcall(of_gpio_notifier_init);
-- 
1.6.5.7

WARNING: multiple messages have this Message-ID (diff)
From: Anton Vorontsov <avorontsov@ru.mvista.com>
To: Grant Likely <grant.likely@secretlab.ca>,
	David Brownell <dbrownell@users.sourceforge.net>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Bill Gatliff <bgat@billgatliff.com>,
	Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>,
	Benjamin Herrenschmidt <benh@kernel.crashing.org>,
	linuxppc-dev@ozlabs.org, linux-kernel@vger.kernel.org
Subject: [PATCH 3/4] of/gpio: Implement GPIOLIB notifier hooks
Date: Mon, 25 Jan 2010 21:11:12 +0300	[thread overview]
Message-ID: <20100125181112.GC13805@oksana.dev.rtsoft.ru> (raw)
In-Reply-To: <20100125180957.GA5380@oksana.dev.rtsoft.ru>

This patch implements GPIOLIB notifier hooks, and thus makes device-enabled
GPIO chips (i.e. the ones that have gpio_chip->dev specified) automatically
attached to the OpenFirmware subsystem. Which means that now we can handle
I2C and SPI GPIO chips almost* transparently.

* "Almost" because some chips still require platform data, and for these
  chips OF-glue is still needed, though with this support the glue will
  be much smaller.

Signed-off-by: Anton Vorontsov <avorontsov@ru.mvista.com>
---
 drivers/of/Kconfig |    1 +
 drivers/of/gpio.c  |  100 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 101 insertions(+), 0 deletions(-)

diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig
index d2fa27c..de9f987 100644
--- a/drivers/of/Kconfig
+++ b/drivers/of/Kconfig
@@ -5,6 +5,7 @@ config OF_DEVICE
 config OF_GPIO
 	def_bool y
 	depends on OF && (PPC_OF || MICROBLAZE) && GPIOLIB
+	select GPIOLIB_NOTIFIER
 	help
 	  OpenFirmware GPIO accessors
 
diff --git a/drivers/of/gpio.c b/drivers/of/gpio.c
index 12c4af0..9d8df77 100644
--- a/drivers/of/gpio.c
+++ b/drivers/of/gpio.c
@@ -13,6 +13,7 @@
 
 #include <linux/kernel.h>
 #include <linux/errno.h>
+#include <linux/notifier.h>
 #include <linux/io.h>
 #include <linux/of.h>
 #include <linux/of_gpio.h>
@@ -236,3 +237,102 @@ err0:
 	return ret;
 }
 EXPORT_SYMBOL(of_mm_gpiochip_add);
+
+/**
+ * of_gpiochip_register_simple - Register a chip with the OF GPIO subsystem
+ * @chip	pointer to a GPIO chip
+ * @np:		device node to register the GPIO chip with
+ *
+ * This function registers a GPIO chip with the OF infrastructure. It is
+ * assumed that the chip was previsously allocated and added to a generic
+ * GPIOLIB framework (using gpiochip_add() function).
+ *
+ * The `simple' name means that the chip is using simple two-cells scheme for
+ * the gpio-specifier.
+ */
+static int of_gpiochip_register_simple(struct gpio_chip *chip,
+				       struct device_node *np)
+{
+	struct of_gpio_chip *of_gc;
+
+	if (np->data) {
+		WARN_ON(1);
+		return -EBUSY;
+	}
+
+	of_gc = kzalloc(sizeof(*of_gc), GFP_KERNEL);
+	if (!of_gc)
+		return -ENOMEM;
+
+	of_gc->gpio_cells = 2;
+	of_gc->xlate = of_gpio_simple_xlate;
+	of_gc->chip = chip;
+	np->data = of_gc;
+	of_node_get(np);
+
+	return 0;
+}
+EXPORT_SYMBOL(of_gpiochip_register_simple);
+
+/**
+ * of_gpiochip_unregister - Unregister a GPIO chip
+ * @chip	pointer to a GPIO chip
+ * @np:		device node for which the GPIO chip was previously registered
+ *
+ * This function unregisters a GPIO chip that was previsously registered
+ * with of_gpiochip_register*().
+ */
+static int of_gpiochip_unregister(struct gpio_chip *chip,
+				  struct device_node *np)
+{
+	struct of_gpio_chip *of_gc = np->data;
+
+	if (!of_gc || of_gc->chip != chip) {
+		WARN_ON(1);
+		return -EINVAL;
+	}
+
+	np->data = NULL;
+	kfree(of_gc);
+	of_node_put(np);
+
+	return 0;
+}
+
+static int of_gpio_notify(struct notifier_block *nb, unsigned long msg,
+			  void *chip)
+{
+	struct gpio_chip *gc = chip;
+	struct device_node *np;
+	int ret = 0;
+
+	if (!gc->dev)
+		return NOTIFY_DONE;
+
+	np = dev_archdata_get_node(&gc->dev->archdata);
+	if (!np)
+		return NOTIFY_DONE;
+
+	switch (msg) {
+	case GPIO_NOTIFY_CHIP_ADDED:
+		ret = of_gpiochip_register_simple(gc, np);
+		break;
+	case GPIO_NOTIFY_CHIP_REMOVE:
+		ret = of_gpiochip_unregister(gc, np);
+		break;
+	default:
+		break;
+	}
+
+	return ret ? notifier_from_errno(ret) : NOTIFY_OK;
+}
+
+static struct notifier_block of_gpio_nb = {
+	.notifier_call = of_gpio_notify,
+};
+
+static int __init of_gpio_notifier_init(void)
+{
+	return blocking_notifier_chain_register(&gpio_notifier, &of_gpio_nb);
+}
+arch_initcall(of_gpio_notifier_init);
-- 
1.6.5.7


  parent reply	other threads:[~2010-01-25 18:11 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-01-25 18:09 [PATCH 0/4] OF GPIO integration for I2C/SPI GPIO chips Anton Vorontsov
2010-01-25 18:09 ` Anton Vorontsov
2010-01-25 18:11 ` [PATCH 1/4] gpiolib: Introduce chip addition/removal notifier Anton Vorontsov
2010-01-25 18:11   ` Anton Vorontsov
2010-01-26  6:34   ` David Brownell
2010-01-26  6:34     ` David Brownell
2010-01-26 17:28     ` Anton Vorontsov
2010-01-26 17:28       ` Anton Vorontsov
2010-01-26 21:01       ` David Brownell
2010-01-26 21:01         ` David Brownell
2010-01-25 18:11 ` [PATCH 2/4] of/gpio: Add support for two-stage registration for the of_gpio_chips Anton Vorontsov
2010-01-25 18:11   ` Anton Vorontsov
2010-01-26  6:36   ` David Brownell
2010-01-26  6:36     ` David Brownell
2010-01-26 17:43     ` Anton Vorontsov
2010-01-26 17:43       ` Anton Vorontsov
2010-01-26 21:02       ` David Brownell
2010-01-26 21:02         ` David Brownell
2010-01-25 18:11 ` Anton Vorontsov [this message]
2010-01-25 18:11   ` [PATCH 3/4] of/gpio: Implement GPIOLIB notifier hooks Anton Vorontsov
2010-01-25 18:11 ` [PATCH 4/4] powerpc/mcu_mpc8349emitx: Remove OF GPIO handling stuff Anton Vorontsov
2010-01-25 18:11   ` Anton Vorontsov
2010-01-26  6:43   ` David Brownell
2010-01-26  6:43     ` David Brownell
  -- strict thread matches above, loose matches on Subject: below --
2010-02-05 20:32 [PATCH v2 0/4] OF GPIO integration for I2C/SPI GPIO chips Anton Vorontsov
2010-02-05 20:32 ` [PATCH 3/4] of/gpio: Implement GPIOLIB notifier hooks Anton Vorontsov
2010-02-05 20:32   ` Anton Vorontsov
2010-02-08 21:02   ` Andrew Morton
2010-02-08 21:02     ` Andrew Morton
2010-02-09 17:08   ` Grant Likely
2010-02-09 17:08     ` Grant Likely
2010-02-09 19:06     ` Anton Vorontsov
2010-02-09 19:06       ` Anton Vorontsov
2010-02-09 17:13   ` Grant Likely
2010-02-09 17:13     ` Grant Likely
2010-02-09 19:16     ` Anton Vorontsov
2010-02-09 19:16       ` Anton Vorontsov
     [not found]       ` <20100305120015.a2008f46.akpm@linux-foundation.org>
     [not found]         ` <fa686aa41003051228w38579483yd4e95bb8eacf40f7@mail.gmail.com>
     [not found]           ` <20100305123527.d6d68e56.akpm@linux-foundation.org>
2010-03-05 23:47             ` Grant Likely
2010-03-05 23:47               ` Grant Likely
2010-03-06  0:28               ` Anton Vorontsov
2010-03-06  0:28                 ` Anton Vorontsov
2010-03-06  3:54                 ` Grant Likely
2010-03-06  3:54                   ` Grant Likely
2010-03-06  5:05                   ` Anton Vorontsov
2010-03-06  5:05                     ` Anton Vorontsov
2010-03-06 16:43                     ` Grant Likely
2010-03-06 16:43                       ` Grant Likely
2010-03-07  1:47                       ` Anton Vorontsov
2010-03-07  1:47                         ` Anton Vorontsov
2010-03-07  6:11                         ` Grant Likely
2010-03-07  6:11                           ` Grant Likely
2010-03-12 21:07   ` Andrew Morton
2010-03-12 21:07     ` Andrew Morton
2010-03-12 21:38     ` Grant Likely
2010-03-12 21:38       ` Grant Likely
2010-04-30 17:45       ` Anton Vorontsov
2010-04-30 17:45         ` Anton Vorontsov

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=20100125181112.GC13805@oksana.dev.rtsoft.ru \
    --to=avorontsov@ru.mvista.com \
    --cc=akpm@linux-foundation.org \
    --cc=bgat@billgatliff.com \
    --cc=dbaryshkov@gmail.com \
    --cc=dbrownell@users.sourceforge.net \
    --cc=grant.likely@secretlab.ca \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linuxppc-dev@ozlabs.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.