Skip to contentSkip to conversation

of_gpio: enchance simple gpio binding with remove function

PATCH Anton Vorontsov 1 participant 1 message Nov 16, 2009 2 messages not in the archive

Subsystems (4)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): 1258379252-30657-1-git-send-email-dbaryshkov-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org

#2 lore raw
Quoted 5 lines · Dmitry Eremin-Solenikov

Add of_simple_gpiochip_remove() - a pair to of_simple_gpiochip_add that can remove simple gpiochip binding.

---

[...]

Quoted 9 lines
(quoted diff) +9 -0
+int of_simple_gpiochip_add(struct device_node *np,
+		       struct of_gpio_chip *of_gc)
+{
+	int ret = -ENOMEM;
+	struct gpio_chip *gc = &of_gc->gc;
+
+	gc->base = -1;
+
+	if (!of_gc->xlate)

Review

Quoted 10 lines
(quoted diff) +10 -0
+		of_gc->xlate = of_gpio_simple_xlate;
+
+	np->data = of_gc;
+
+	/* We don't want to lose the node and its ->data */
+	of_node_get(np);
+
+	ret = gpiochip_add(gc);
+	if (ret)
+		goto err;

I don't think OF stuff should register gpiochips.

Previously we did this, but if you look into platform gpio drivers (e.g. drivers/gpio/pc{a,f}*.c), you'll notice that they already register GPIO chips, and call pdata hooks afterwards.

Our aim is to only register OF part in the pdata hook, and don't mess with generic gpiochip handling.

I started the work, but never actually find time to continue. I'm attaching the unfinished patches as an example of how I think it should be done, but I doubt they apply to the latest kernels, but nonetheless you might want to borrow some ideas.

Thanks!

Signature · 3 lines
Anton Vorontsov
email: [email protected]
irc://irc.freenode.net/bd2
Quoted 1 line

From 3e19b9a6d8c753aaaadc1aad80e055672b888f79 Mon Sep 17 00:00:00 2001

From: Anton Vorontsov <[email protected]>
Date: Fri, 1 May 2009 18:36:09 +0400
Subject: [PATCH 1/4] of/gpio: add support for two-stage registration for the of_gpio_chips

Currently there are two ways to register OF GPIO controllers:
1. Allocating the of_gpio_chip structure and passing the &of_gc->gc pointer to the gpiochip_add. (Can use container_of to convert the gpio_chip to the of_gpio_chip.)

2. Allocating and registering the gpio_chip structure separately from the of_gpio_chip. (Since two allocations are separate, container_of won't work.)

As time goes by we'll kill the first option.

---
 arch/powerpc/platforms/83xx/mcu_mpc8349emitx.c |    1 +
 drivers/of/gpio.c                              |   23 +++++++++++++++++++++--
 include/linux/of_gpio.h                        |    1 +
 3 files changed, 23 insertions(+), 2 deletions(-)
arch/powerpc/platforms/83xx/mcu_mpc8349emitx.c +1 -0
index 82a9bcb..73c7e6b 100644
@@ -93,6 +93,7 @@ static int mcu_gpiochip_add(struct mcu *mcu)
 	gc->base = -1;
 	gc->set = mcu_gpio_set;
 	gc->direction_output = mcu_gpio_dir_out;
+	of_gc->chip = gc;
 	of_gc->gpio_cells = 2;
 	of_gc->xlate = of_gpio_simple_xlate;
 
drivers/of/gpio.c +21 -2
index 6eea601..148030f 100644
@@ -70,7 +70,7 @@ int of_get_gpio_flags(struct device_node *np, int index,
 	if (ret < 0)
 		goto err1;
 
-	ret += of_gc->gc.base;
+	ret += of_gc->chip->base;
 err1:
 	of_node_put(gc);
 err0:
@@ -140,7 +140,7 @@ int of_gpio_simple_xlate(struct of_gpio_chip *of_gc, struct device_node *np,
 		return -EINVAL;
 	}
 
-	if (*gpio > of_gc->gc.ngpio)
+	if (*gpio > of_gc->chip->ngpio)
 		return -EINVAL;
 
 	if (flags)
@@ -178,6 +178,25 @@ int of_mm_gpiochip_add(struct device_node *np,
 	struct of_gpio_chip *of_gc = &mm_gc->of_gc;
 	struct gpio_chip *gc = &of_gc->gc;
 
+	/*
+	 * Currently there are two ways to register OF GPIO controllers:
+	 *
+	 * 1. Allocating the of_gpio_chip structure and passing the
+	 *    &of_gc->gc pointer to the gpiochip_add. (Can use container_of
+	 *    to convert the gpio_chip to the of_gpio_chip.)
+	 *
+	 * 2. Allocating and registering the gpio_chip structure separately
+	 *    from the of_gpio_chip. (Since two allocations are separate,
+	 *    container_of won't work.)
+	 *
+	 * As time goes by we'll kill the first option. For now just check
+	 * if it's "new-style" registration or "old-style" one.
+	 */
+	if (!of_gc->chip)
+		of_gc->chip = gc;
+	else
+		gc = of_gc->chip;
+
 	gc->label = kstrdup(np->full_name, GFP_KERNEL);
 	if (!gc->label)
 		goto err0;
include/linux/of_gpio.h +1 -0
index fc2472c..fb859dd 100644
@@ -37,6 +37,7 @@ enum of_gpio_flags {
  */
 struct of_gpio_chip {
 	struct gpio_chip gc;
+	struct gpio_chip *chip;
 	int gpio_cells;
 	int (*xlate)(struct of_gpio_chip *of_gc, struct device_node *np,
 		     const void *gpio_spec, enum of_gpio_flags *flags);
Signature · 1 line
1.6.3.3
Quoted 1 line

From 265a4a0e3574f5a610a33a04717532cd2603e17b Mon Sep 17 00:00:00 2001

From: Anton Vorontsov <[email protected]>
Date: Fri, 1 May 2009 18:36:09 +0400
Subject: [PATCH 2/4] of/gpio: implement of_dev_gpiochip_{add,remove} calls

And let the gpiolib forward all dev_gpiochip_ calls to of_ versions, there we can glue the gpiochips with the device tree.

---
 drivers/of/gpio.c       |   56 +++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/of_gpio.h |    4 +++
 2 files changed, 60 insertions(+), 0 deletions(-)
drivers/of/gpio.c +56 -0

59 lines not on this page · raw message

include/linux/of_gpio.h +4 -0
index fb859dd..323e6d4 100644
@@ -70,6 +70,10 @@ extern unsigned int of_gpio_count(struct device_node *np);
 
 extern int of_mm_gpiochip_add(struct device_node *np,
 			      struct of_mm_gpio_chip *mm_gc);
+extern int of_gpiochip_register_simple(struct gpio_chip *chip,
+				       struct device_node *np);
+extern void of_gpiochip_unregister(struct gpio_chip *chip,
+				   struct device_node *np);
 extern int of_gpio_simple_xlate(struct of_gpio_chip *of_gc,
 				struct device_node *np,
 				const void *gpio_spec,
Signature · 1 line
1.6.3.3
Quoted 1 line

From 2e27423e56561e74d438814a8ecb757024019d8e Mon Sep 17 00:00:00 2001

From: Anton Vorontsov <[email protected]>
Date: Fri, 1 May 2009 18:36:10 +0400
Subject: [PATCH 3/4] i2c/mcu_mpc8349emitx: Use new OF GPIO helpers

With the new OF GPIO helpers it's much easier to handle I2C GPIO controllers. Now drivers don't need to deal with the OF-specific bits.

---
 arch/powerpc/platforms/83xx/mcu_mpc8349emitx.c |   78 +++++++----------------
 1 files changed, 24 insertions(+), 54 deletions(-)
arch/powerpc/platforms/83xx/mcu_mpc8349emitx.c +24 -54

132 lines not on this page · raw message

Signature · 1 line
1.6.3.3
Quoted 1 line

From 64abcde84f722c280872a389369e2113f507569e Mon Sep 17 00:00:00 2001

From: Anton Vorontsov <[email protected]>
Date: Mon, 16 Nov 2009 17:41:27 +0300
Subject: [PATCH 4/4] gpio/pca953x: pass gpio_chip pointer to the setup/teardown callbacks

This is needed so that the platform code could register the chip with the platform internal structures.

While at it, also change gpio_base to the signed type (we'll pass -1 for the dynamic allocation).

---
 drivers/gpio/pca953x.c      |    7 +++----
 include/linux/i2c/pca953x.h |    8 +++++---
 2 files changed, 8 insertions(+), 7 deletions(-)
drivers/gpio/pca953x.c +3 -4
index 8dc0164..c65a353 100644
@@ -239,8 +239,7 @@ static int __devinit pca953x_probe(struct i2c_client *client,
 		goto out_failed;
 
 	if (pdata->setup) {
-		ret = pdata->setup(client, chip->gpio_chip.base,
-				chip->gpio_chip.ngpio, pdata->context);
+		ret = pdata->setup(client, &chip->gpio_chip, pdata->context);
 		if (ret < 0)
 			dev_warn(&client->dev, "setup failed, %d\n", ret);
 	}
@@ -260,8 +259,8 @@ static int pca953x_remove(struct i2c_client *client)
 	int ret = 0;
 
 	if (pdata->teardown) {
-		ret = pdata->teardown(client, chip->gpio_chip.base,
-				chip->gpio_chip.ngpio, pdata->context);
+		ret = pdata->teardown(client, &chip->gpio_chip,
+				      pdata->context);
 		if (ret < 0) {
 			dev_err(&client->dev, "%s failed, %d\n",
 					"teardown", ret);
include/linux/i2c/pca953x.h +5 -3
index 3c73612..60d60c0 100644
@@ -1,8 +1,10 @@
 /* platform data for the PCA9539 16-bit I/O expander driver */
 
+struct gpio_chip;
+
 struct pca953x_platform_data {
 	/* number of the first GPIO */
-	unsigned	gpio_base;
+	int		gpio_base;
 
 	/* initial polarity inversion setting */
 	uint16_t	invert;
@@ -10,9 +12,9 @@ struct pca953x_platform_data {
 	void		*context;	/* param to setup/teardown */
 
 	int		(*setup)(struct i2c_client *client,
-				unsigned gpio, unsigned ngpio,
+				struct gpio_chip *chip,
 				void *context);
 	int		(*teardown)(struct i2c_client *client,
-				unsigned gpio, unsigned ngpio,
+				struct gpio_chip *chip,
 				void *context);
 };
Signature · 1 line
1.6.3.3

_______________________________________________
devicetree-discuss mailing list devicetree-discuss-uLR06cmDAlY/[email protected]
https://lists.ozlabs.org/listinfo/devicetree-discuss