>From 2e27423e56561e74d438814a8ecb757024019d8e Mon Sep 17 00:00:00 2001
From: Anton Vorontsov <avorontsov@ru.mvista.com>
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.

Signed-off-by: Anton Vorontsov <cbouatmailru@gmail.com>
---
 arch/powerpc/platforms/83xx/mcu_mpc8349emitx.c |   78 +++++++----------------
 1 files changed, 24 insertions(+), 54 deletions(-)

diff --git a/arch/powerpc/platforms/83xx/mcu_mpc8349emitx.c b/arch/powerpc/platforms/83xx/mcu_mpc8349emitx.c
index 73c7e6b..374c99c 100644
--- a/arch/powerpc/platforms/83xx/mcu_mpc8349emitx.c
+++ b/arch/powerpc/platforms/83xx/mcu_mpc8349emitx.c
@@ -18,7 +18,6 @@
 #include <linux/mutex.h>
 #include <linux/i2c.h>
 #include <linux/gpio.h>
-#include <linux/of.h>
 #include <linux/of_gpio.h>
 #include <asm/prom.h>
 #include <asm/machdep.h>
@@ -36,7 +35,7 @@ struct mcu {
 	struct mutex lock;
 	struct device_node *np;
 	struct i2c_client *client;
-	struct of_gpio_chip of_gc;
+	struct gpio_chip gc;
 	u8 reg_ctrl;
 };
 
@@ -55,8 +54,7 @@ static void mcu_power_off(void)
 
 static void mcu_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
 {
-	struct of_gpio_chip *of_gc = to_of_gpio_chip(gc);
-	struct mcu *mcu = container_of(of_gc, struct mcu, of_gc);
+	struct mcu *mcu = container_of(gc, struct mcu, gc);
 	u8 bit = 1 << (4 + gpio);
 
 	mutex_lock(&mcu->lock);
@@ -75,56 +73,11 @@ static int mcu_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
 	return 0;
 }
 
-static int mcu_gpiochip_add(struct mcu *mcu)
-{
-	struct device_node *np;
-	struct of_gpio_chip *of_gc = &mcu->of_gc;
-	struct gpio_chip *gc = &of_gc->gc;
-	int ret;
-
-	np = of_find_compatible_node(NULL, NULL, "fsl,mcu-mpc8349emitx");
-	if (!np)
-		return -ENODEV;
-
-	gc->owner = THIS_MODULE;
-	gc->label = np->full_name;
-	gc->can_sleep = 1;
-	gc->ngpio = MCU_NUM_GPIO;
-	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;
-
-	np->data = of_gc;
-	mcu->np = np;
-
-	/*
-	 * We don't want to lose the node, its ->data and ->full_name...
-	 * So, if succeeded, we don't put the node here.
-	 */
-	ret = gpiochip_add(gc);
-	if (ret)
-		of_node_put(np);
-	return ret;
-}
-
-static int mcu_gpiochip_remove(struct mcu *mcu)
-{
-	int ret;
-
-	ret = gpiochip_remove(&mcu->of_gc.gc);
-	if (ret)
-		return ret;
-	of_node_put(mcu->np);
-
-	return 0;
-}
-
 static int __devinit mcu_probe(struct i2c_client *client,
 			       const struct i2c_device_id *id)
 {
+	struct device *dev = &client->dev;
+	struct device_node *np = dev_archdata_get_node(&dev->archdata);
 	struct mcu *mcu;
 	int ret;
 
@@ -141,15 +94,28 @@ static int __devinit mcu_probe(struct i2c_client *client,
 		goto err;
 	mcu->reg_ctrl = ret;
 
-	ret = mcu_gpiochip_add(mcu);
+	mcu->gc.owner = THIS_MODULE;
+	mcu->gc.label = client->dev.bus_id;
+	mcu->gc.can_sleep = 1;
+	mcu->gc.ngpio = MCU_NUM_GPIO;
+	mcu->gc.base = -1;
+	mcu->gc.set = mcu_gpio_set;
+	mcu->gc.direction_output = mcu_gpio_dir_out;
+
+	ret = gpiochip_add(&mcu->gc);
 	if (ret)
 		goto err;
 
+	/* The OF interface isn't mandatory (think of sysfs GPIO interface). */
+	ret = of_gpiochip_register_simple(&mcu->gc, np);
+	if (ret)
+		dev_warn(dev, "OF GPIO registration failed: %d\n", ret);
+
 	/* XXX: this is potentially racy, but there is no lock for ppc_md */
 	if (!ppc_md.power_off) {
 		glob_mcu = mcu;
 		ppc_md.power_off = mcu_power_off;
-		dev_info(&client->dev, "will provide power-off service\n");
+		dev_info(dev, "will provide power-off service\n");
 	}
 
 	return 0;
@@ -160,6 +126,8 @@ err:
 
 static int __devexit mcu_remove(struct i2c_client *client)
 {
+	struct device *dev = &client->dev;
+	struct device_node *np = dev_archdata_get_node(&dev->archdata);
 	struct mcu *mcu = i2c_get_clientdata(client);
 	int ret;
 
@@ -168,9 +136,11 @@ static int __devexit mcu_remove(struct i2c_client *client)
 		glob_mcu = NULL;
 	}
 
-	ret = mcu_gpiochip_remove(mcu);
+	ret = gpiochip_remove(&mcu->gc);
 	if (ret)
 		return ret;
+
+	of_gpiochip_unregister(&mcu->gc, np);
 	i2c_set_clientdata(client, NULL);
 	kfree(mcu);
 	return 0;
-- 
1.6.3.3

