public inbox for linux-omap@vger.kernel.org
 help / color / mirror / Atom feed
* [WIP] [PATCH] ARM: OMAP: Use new style I2C driver for Menelaus chip.
@ 2007-05-11  9:20 Trilok Soni
  2007-05-11 13:13 ` Trilok Soni
  2007-05-23  6:53 ` David Brownell
  0 siblings, 2 replies; 3+ messages in thread
From: Trilok Soni @ 2007-05-11  9:20 UTC (permalink / raw)
  To: linux-omap-open-source

[-- Attachment #1: Type: text/plain, Size: 552 bytes --]

Tony/David,

I have attached the wip version of converting menelaus chip to use new
style I2C driver bindings. I have tested it on H4 and it is working.
It will break N800 as I have removed the late init functionalities
from driver as it should come from board specific file using new
i2c_register_board_info function.

As I will not have access to h/w for few days, I would appreciate if
some one reviews and make it work for n800 and submit patch for the
same.

Note:
This patch still needs more cosmetic changes and improvements.

-- 
--Trilok Soni

[-- Attachment #2: 0001-ARM-OMAP-Use-new-style-I2C-driver-for-Menelaus-chip.patch --]
[-- Type: text/x-patch, Size: 12680 bytes --]

From 25ecf02fb6af78b2469a4e9d10927ac59a32a507 Mon Sep 17 00:00:00 2001
From: Trilok Soni <soni.trilok@gmail.com>
Date: Fri, 11 May 2007 01:39:08 +0530
Subject: [PATCH] ARM: OMAP: Use new style I2C driver for Menelaus chip.

Signed-off-by: Trilok Soni <soni.trilok@gmail.com>
---
 arch/arm/mach-omap2/board-h4.c |   13 +++
 drivers/i2c/chips/menelaus.c   |  176 ++++++++++++++++------------------------
 2 files changed, 84 insertions(+), 105 deletions(-)

diff --git a/arch/arm/mach-omap2/board-h4.c b/arch/arm/mach-omap2/board-h4.c
index aa5b657..999de13 100644
--- a/arch/arm/mach-omap2/board-h4.c
+++ b/arch/arm/mach-omap2/board-h4.c
@@ -21,6 +21,7 @@
 #include <linux/input.h>
 #include <linux/err.h>
 #include <linux/clk.h>
+#include <linux/i2c.h>
 
 #include <asm/hardware.h>
 #include <asm/mach-types.h>
@@ -492,6 +493,15 @@ static void __init tusb_evm_setup(void)
 
 #endif
 
+static struct i2c_board_info __initdata h4_i2c_board_info[] = {
+#ifdef CONFIG_MENELAUS
+	{
+		I2C_BOARD_INFO("menelaus", 0x72),
+		.irq = INT_24XX_SYS_NIRQ,
+	},
+#endif
+};
+
 static void __init omap_h4_init(void)
 {
 	/*
@@ -520,6 +530,9 @@ static void __init omap_h4_init(void)
 	omap_cfg_reg(V19_24XX_USB1_RCV);
 #endif
 
+	i2c_register_board_info(1, h4_i2c_board_info,
+			ARRAY_SIZE(h4_i2c_board_info));
+
 	platform_add_devices(h4_devices, ARRAY_SIZE(h4_devices));
 	omap_board_config = h4_config;
 	omap_board_config_size = ARRAY_SIZE(h4_config);
diff --git a/drivers/i2c/chips/menelaus.c b/drivers/i2c/chips/menelaus.c
index 5004915..2a3a281 100644
--- a/drivers/i2c/chips/menelaus.c
+++ b/drivers/i2c/chips/menelaus.c
@@ -134,15 +134,9 @@
 
 static void menelaus_work(struct work_struct *_menelaus);
 
-/* Initialized by menelaus_init */
-static unsigned short normal_i2c[] = { MENELAUS_I2C_ADDRESS, I2C_CLIENT_END };
-
-I2C_CLIENT_INSMOD;
-
 struct menelaus_chip {
-	unsigned long		initialized;
 	struct mutex		lock;
-	struct i2c_client	client;
+	struct i2c_client	*client;
 	struct work_struct	work;
 	int			irq;
 	unsigned		vcore_hw_mode:1;
@@ -151,12 +145,11 @@ struct menelaus_chip {
 	void			*mmc_callback_data;
 };
 
-static struct menelaus_chip menelaus;
-static struct menelaus_platform_data *menelaus_pdata;
+static struct menelaus_chip *the_menelaus;
 
 static int menelaus_write_reg(int reg, u8 value)
 {
-	int val = i2c_smbus_write_byte_data(&menelaus.client, reg, value);
+	int val = i2c_smbus_write_byte_data(the_menelaus->client, reg, value);
 
 	if (val < 0) {
 		pr_err("write error");
@@ -168,7 +161,7 @@ static int menelaus_write_reg(int reg, u8 value)
 
 static int menelaus_read_reg(int reg)
 {
-	int val = i2c_smbus_read_byte_data(&menelaus.client, reg);
+	int val = i2c_smbus_read_byte_data(the_menelaus->client, reg);
 
 	if (val < 0)
 		pr_err("read error");
@@ -213,10 +206,10 @@ static int menelaus_add_irq_work(int irq, void * handler)
 {
 	int ret = 0;
 
-	mutex_lock(&menelaus.lock);
-	menelaus.handlers[irq] = handler;
+	mutex_lock(&the_menelaus->lock);
+	the_menelaus->handlers[irq] = handler;
 	ret = menelaus_enable_irq(irq);
-	mutex_unlock(&menelaus.lock);
+	mutex_unlock(&the_menelaus->lock);
 
 	return ret;
 }
@@ -226,10 +219,10 @@ static int menelaus_remove_irq_work(int irq)
 {
 	int ret = 0;
 
-	mutex_lock(&menelaus.lock);
+	mutex_lock(&the_menelaus->lock);
 	ret = menelaus_disable_irq(irq);
-	menelaus.handlers[irq] = NULL;
-	mutex_unlock(&menelaus.lock);
+	the_menelaus->handlers[irq] = NULL;
+	mutex_unlock(&the_menelaus->lock);
 
 	return ret;
 }
@@ -271,10 +264,10 @@ int menelaus_set_mmc_opendrain(int slot, int enable)
 
 	if (slot != 1 && slot != 2)
 		return -EINVAL;
-	mutex_lock(&menelaus.lock);
+	mutex_lock(&the_menelaus->lock);
 	ret = menelaus_read_reg(MENELAUS_MCT_CTRL1);
 	if (ret < 0) {
-		mutex_unlock(&menelaus.lock);
+		mutex_unlock(&the_menelaus->lock);
 		return ret;
 	}
 	val = ret;
@@ -290,7 +283,7 @@ int menelaus_set_mmc_opendrain(int slot, int enable)
 			val &= ~(1 << 3);
 	}
 	ret = menelaus_write_reg(MENELAUS_MCT_CTRL1, val);
-	mutex_unlock(&menelaus.lock);
+	mutex_unlock(&the_menelaus->lock);
 
 	return ret;
 }
@@ -300,7 +293,7 @@ int menelaus_set_slot_sel(int enable)
 {
 	int ret;
 
-	mutex_lock(&menelaus.lock);
+	mutex_lock(&the_menelaus->lock);
 	ret = menelaus_read_reg(MENELAUS_GPIO_CTRL);
 	if (ret < 0)
 		goto out;
@@ -311,7 +304,7 @@ int menelaus_set_slot_sel(int enable)
 		ret &= ~(1 << 5);
 	ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret);
 out:
-	mutex_unlock(&menelaus.lock);
+	mutex_unlock(&the_menelaus->lock);
 	return ret;
 }
 EXPORT_SYMBOL(menelaus_set_slot_sel);
@@ -325,7 +318,7 @@ int menelaus_set_mmc_slot(int slot, int enable, int power, int cd_en)
 	if (power >= 3)
 		return -EINVAL;
 
-	mutex_lock(&menelaus.lock);
+	mutex_lock(&the_menelaus->lock);
 
 	ret = menelaus_read_reg(MENELAUS_MCT_CTRL2);
 	if (ret < 0)
@@ -373,7 +366,7 @@ int menelaus_set_mmc_slot(int slot, int enable, int power, int cd_en)
 	val &= ~(0x03 << 2);
 	ret = menelaus_write_reg(MENELAUS_MCT_CTRL3, val);
 out:
-	mutex_unlock(&menelaus.lock);
+	mutex_unlock(&the_menelaus->lock);
 	return ret;
 }
 EXPORT_SYMBOL(menelaus_set_mmc_slot);
@@ -385,8 +378,8 @@ int menelaus_register_mmc_callback(void (*callback)(void *data, u8 card_mask),
 {
 	int ret = 0;
 
-	menelaus.mmc_callback_data = data;
-	menelaus.mmc_callback = callback;
+	the_menelaus->mmc_callback_data = data;
+	the_menelaus->mmc_callback = callback;
 	ret = menelaus_add_irq_work(MENELAUS_MMC_S1CD_IRQ,
 				    menelaus_mmc_cd_work);
 	if (ret < 0)
@@ -413,8 +406,8 @@ void menelaus_unregister_mmc_callback(void)
 	menelaus_remove_irq_work(MENELAUS_MMC_S1D1_IRQ);
 	menelaus_remove_irq_work(MENELAUS_MMC_S2D1_IRQ);
 
-	menelaus.mmc_callback = NULL;
-	menelaus.mmc_callback_data = 0;
+	the_menelaus->mmc_callback = NULL;
+	the_menelaus->mmc_callback_data = 0;
 }
 EXPORT_SYMBOL(menelaus_unregister_mmc_callback);
 
@@ -436,7 +429,7 @@ static int menelaus_set_voltage(const struct menelaus_vtg *vtg, int mV,
 {
 	int val, ret;
 
-	mutex_lock(&menelaus.lock);
+	mutex_lock(&the_menelaus->lock);
 	if (vtg == 0)
 		goto set_voltage;
 
@@ -455,7 +448,7 @@ static int menelaus_set_voltage(const struct menelaus_vtg *vtg, int mV,
 set_voltage:
 	ret = menelaus_write_reg(vtg->mode_reg, mode);
 out:
-	mutex_unlock(&menelaus.lock);
+	mutex_unlock(&the_menelaus->lock);
 	if (ret == 0) {
 		/* Wait for voltage to stabilize */
 		msleep(1);
@@ -516,11 +509,11 @@ int menelaus_set_vcore_sw(unsigned int mV)
 #endif
 
 	/* Set SW mode and the voltage in one go. */
-	mutex_lock(&menelaus.lock);
+	mutex_lock(&the_menelaus->lock);
 	ret = menelaus_write_reg(MENELAUS_VCORE_CTRL1, val);
 	if (ret == 0)
-		menelaus.vcore_hw_mode = 0;
-	mutex_unlock(&menelaus.lock);
+		the_menelaus->vcore_hw_mode = 0;
+	mutex_unlock(&the_menelaus->lock);
 	msleep(1);
 
 	return ret;
@@ -542,22 +535,22 @@ int menelaus_set_vcore_hw(unsigned int roof_mV, unsigned int floor_mV)
 	       floor_mV, roof_mV);
 #endif
 
-	mutex_lock(&menelaus.lock);
+	mutex_lock(&the_menelaus->lock);
 	ret = menelaus_write_reg(MENELAUS_VCORE_CTRL3, fval);
 	if (ret < 0)
 		goto out;
 	ret = menelaus_write_reg(MENELAUS_VCORE_CTRL4, rval);
 	if (ret < 0)
 		goto out;
-	if (!menelaus.vcore_hw_mode) {
+	if (!the_menelaus->vcore_hw_mode) {
 		val = menelaus_read_reg(MENELAUS_VCORE_CTRL1);
 		val |= ((1 << 7) | (1 << 5)); /* HW mode, turn OFF byte comparator */
 		ret = menelaus_write_reg(MENELAUS_VCORE_CTRL1, val);
-		menelaus.vcore_hw_mode = 1;
+		the_menelaus->vcore_hw_mode = 1;
 	}
 	msleep(1);
 out:
-	mutex_unlock(&menelaus.lock);
+	mutex_unlock(&the_menelaus->lock);
 	return ret;
 }
 
@@ -736,7 +729,7 @@ int menelaus_set_regulator_sleep(int enable, u32 val)
 {
 	int t, ret;
 
-        mutex_lock(&menelaus.lock);
+        mutex_lock(&the_menelaus->lock);
 	ret = menelaus_write_reg(MENELAUS_SLEEP_CTRL2, val);
 	if (ret < 0)
 		goto out;
@@ -753,7 +746,7 @@ int menelaus_set_regulator_sleep(int enable, u32 val)
 		ret &= ~t;
 	ret = menelaus_write_reg(MENELAUS_GPIO_CTRL, ret);
 out:
-	mutex_unlock(&menelaus.lock);
+	mutex_unlock(&the_menelaus->lock);
 	return ret;
 }
 
@@ -808,37 +801,36 @@ static irqreturn_t menelaus_irq(int irq, void *_menelaus)
 
 static struct i2c_driver menelaus_i2c_driver;
 
-static int menelaus_probe(struct i2c_adapter *adapter, int address, int kind)
+static int menelaus_probe(struct i2c_client *client)
 {
-	struct i2c_client	*c;
+	struct menelaus_chip	*menelaus;
 	int			rev = 0, val;
 	int			err = 0;
+	struct menelaus_platform_data *menelaus_pdata =
+					client->dev.platform_data;
 
-	if (test_and_set_bit(0, &menelaus.initialized))
-		return -EBUSY;
+	if (the_menelaus)
+		return -ENODEV;
 
-	c = &menelaus.client;
-	strncpy(c->name, DRIVER_NAME, sizeof(c->name));
-	c->addr		= address;
-	c->adapter	= adapter;
-	c->driver	= &menelaus_i2c_driver;
-	c->flags	= 0;
+	menelaus = kzalloc(sizeof *menelaus, GFP_KERNEL);
+	if (!menelaus)
+		return -ENOMEM;
 
-	if ((err = i2c_attach_client(c)) < 0) {
-		pr_err("couldn't attach\n");
-		goto fail1;
-	}
+	i2c_set_clientdata(client, menelaus);
+
+	the_menelaus = menelaus;
+	menelaus->client = client;
 
 	/* If a true probe check the device */
-	if (kind < 0 && (rev = menelaus_read_reg(MENELAUS_REV)) < 0) {
+	if ((rev = menelaus_read_reg(MENELAUS_REV)) < 0) {
 		pr_err("device not found");
 		err = -ENODEV;
-		goto fail2;
+		goto fail1;
 	}
 
 	/* Most likely Menelaus interrupt is at SYS_NIRQ */
 	omap_cfg_reg(W19_24XX_SYS_NIRQ);
-	menelaus.irq = INT_24XX_SYS_NIRQ;
+		menelaus->irq = client->irq;
 
 	/* Ack and disable all Menelaus interrupts */
 	menelaus_write_reg(MENELAUS_INT_ACK1, 0xff);
@@ -849,81 +841,60 @@ static int menelaus_probe(struct i2c_adapter *adapter, int address, int kind)
 	/* Set output buffer strengths */
 	menelaus_write_reg(MENELAUS_MCT_CTRL1, 0x73);
 
-	err = request_irq(menelaus.irq, menelaus_irq, IRQF_DISABLED,
-			  DRIVER_NAME, &menelaus);
-	if (err) {
-		printk(KERN_ERR "Could not get Menelaus IRQ\n");
-		goto fail2;
+	if (client->irq > 0) {
+		err = request_irq(client->irq, menelaus_irq, IRQF_DISABLED,
+				  DRIVER_NAME, menelaus);
+		if (err) {
+			printk(KERN_ERR "Could not get Menelaus IRQ\n");
+			goto fail1;
+		}
 	}
 
-	mutex_init(&menelaus.lock);
-	INIT_WORK(&menelaus.work, menelaus_work);
+	mutex_init(&menelaus->lock);
+	INIT_WORK(&menelaus->work, menelaus_work);
 
-	if (kind < 0)
-		pr_info("Menelaus rev %d.%d\n", rev >> 4, rev & 0x0f);
+	pr_info("Menelaus rev %d.%d\n", rev >> 4, rev & 0x0f);
 
 	val = menelaus_read_reg(MENELAUS_VCORE_CTRL1);
 	if (val < 0)
-		goto fail3;
+		goto fail2;
 	if (val & (1 << 7))
-		menelaus.vcore_hw_mode = 1;
+		menelaus->vcore_hw_mode = 1;
 	else
-		menelaus.vcore_hw_mode = 0;
+		menelaus->vcore_hw_mode = 0;
 
 	if (menelaus_pdata != NULL && menelaus_pdata->late_init != NULL) {
-		err = menelaus_pdata->late_init(&c->dev);
+		err = menelaus_pdata->late_init(&client->dev);
 		if (err < 0)
-			goto fail3;
+			goto fail2;
 	}
 
 	return 0;
-fail3:
-	free_irq(menelaus.irq, &menelaus);
-	flush_scheduled_work();
 fail2:
-	i2c_detach_client(c);
+	free_irq(client->irq, menelaus);
+	flush_scheduled_work();
 fail1:
-	clear_bit(0, &menelaus.initialized);
+	kfree(menelaus);
 	return err;
 }
 
 static int menelaus_remove(struct i2c_client *client)
 {
-	int err;
-
-	free_irq(menelaus.irq, &menelaus);
-
-	if ((err = i2c_detach_client(client))) {
-		pr_err("client deregistration failed\n");
-		return err;
-	}
-
-	clear_bit(0, &menelaus.initialized);
+	struct menelaus_chip	*menelaus = i2c_get_clientdata(client);
 
+	free_irq(client->irq, menelaus);
+	the_menelaus = NULL;
 	return 0;
 }
 
-/*-----------------------------------------------------------------------*/
-
-static int menelaus_scan_bus(struct i2c_adapter *bus)
-{
-	if (!i2c_check_functionality(bus, I2C_FUNC_SMBUS_BYTE_DATA |
-					  I2C_FUNC_SMBUS_WRITE_BYTE)) {
-		pr_err("invalid i2c bus functionality\n");
-		return -EINVAL;
-	}
-
-	return i2c_probe(bus, &addr_data, menelaus_probe);
-}
-
 static struct i2c_driver menelaus_i2c_driver = {
 	.driver = {
 		.name		= DRIVER_NAME,
 	},
 	.id		= I2C_DRIVERID_MISC, /*FIXME:accroding to i2c-ids.h */
 	.class		= I2C_CLASS_HWMON,
-	.attach_adapter	= menelaus_scan_bus,
-	.detach_client	= menelaus_remove,
+	.probe		= menelaus_probe,
+	.remove		= menelaus_remove,
 };
 
 static int __init menelaus_init(void)
@@ -945,11 +916,6 @@ static void __exit menelaus_exit(void)
 	/* FIXME: Shutdown menelaus parts that can be shut down */
 }
 
-void __init menelaus_set_platform_data(struct menelaus_platform_data *pdata)
-{
-	menelaus_pdata = pdata;
-}
-
 MODULE_AUTHOR("Texas Instruments, Inc.");
 MODULE_DESCRIPTION("I2C interface for Menelaus.");
 MODULE_LICENSE("GPL");
-- 
1.5.0


[-- Attachment #3: Type: text/plain, Size: 0 bytes --]



^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-05-23  6:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-05-11  9:20 [WIP] [PATCH] ARM: OMAP: Use new style I2C driver for Menelaus chip Trilok Soni
2007-05-11 13:13 ` Trilok Soni
2007-05-23  6:53 ` David Brownell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox