Skip to contentSkip to conversation

Bitbanging i2c bus driver using the GPIO API

PATCH In review Haavard Skinnemoen 4 participants 21 messages Apr 14, 2007 – May 6, 2007

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 Bitbanging i2c bus driver using the GPIO API

This is a very simple bitbanging i2c bus driver utilizing the new arch-neutral GPIO API. Useful for chips that don't have a built-in i2c controller, additional i2c busses, or testing purposes.

To use, include something similar to the following in the board-specific setup code:

  #include <linux/i2c-gpio.h>
  static struct i2c_gpio_platform_data i2c_gpio_data = {
	.sda_pin	= GPIO_PIN_FOO,
	.scl_pin	= GPIO_PIN_BAR,
  };
  static struct platform_device i2c_gpio_device = {
	.name		= "i2c-gpio",
	.id		= 0,
	.dev		= {
		.platform_data	= &i2c_gpio_data,
	},

};

Register this platform_device, set up the i2c pins as GPIO if required and you're ready to go. This will use default values for udelay and timeout, and will work with GPIO hardware that does not support open drain mode, but allows changing the direction of the SDA and SCL lines on the fly.

---
Sorry for the long delay. I didn't want to send out a patch just before taking off for two weeks. This patch contains the following changes compared to v2:

o Make udelay and timeout parameterizable and fix comment
o Default to a very low SCL frequency (6.6 kHz) if clock stretching isn't supported
o Document that GPIO hardware must report actual state of sda and scl pins in open drain mode.
o Add myself to MAINTAINERS
o Add KERN_ERR to "probe failed" message
o Use new gpio_direction_output() API

 MAINTAINERS                   |    5 +
 drivers/i2c/busses/Kconfig    |    8 ++
 drivers/i2c/busses/Makefile   |    1 +
 drivers/i2c/busses/i2c-gpio.c |  213 +++++++++++++++++++++++++++++++++++++++++
 include/linux/i2c-gpio.h      |   38 +++++++
 5 files changed, 265 insertions(+), 0 deletions(-)
MAINTAINERS +5 -0
index ef84419..fdecda4 100644
@@ -1429,6 +1429,11 @@ L:	[email protected]
 W:	http://www.icp-vortex.com/
 S:	Supported
 
+GENERIC GPIO I2C DRIVER
+P:	Haavard Skinnemoen
+M:	[email protected]
+S:	Supported
+
 GENERIC HDLC DRIVER, N2, C101, PCI200SYN and WANXL DRIVERS
 P:	Krzysztof Halasa
 M:	[email protected]
drivers/i2c/busses/Kconfig +8 -0
index fb19dbb..52f79d1 100644
@@ -102,6 +102,14 @@ config I2C_ELEKTOR
 	  This support is also available as a module.  If so, the module 
 	  will be called i2c-elektor.
 
+config I2C_GPIO
+	tristate "GPIO-based bitbanging i2c driver"
+	depends on I2C && GENERIC_GPIO
+	select I2C_ALGOBIT
+	help
+	  This is a very simple bitbanging i2c driver utilizing the
+	  arch-neutral GPIO API to control the SCL and SDA lines.
+
 config I2C_HYDRA
 	tristate "CHRP Apple Hydra Mac I/O I2C interface"
 	depends on I2C && PCI && PPC_CHRP && EXPERIMENTAL
drivers/i2c/busses/Makefile +1 -0
index 290b540..68f2b05 100644
@@ -11,6 +11,7 @@ obj-$(CONFIG_I2C_AMD8111)	+= i2c-amd8111.o
 obj-$(CONFIG_I2C_AT91)		+= i2c-at91.o
 obj-$(CONFIG_I2C_AU1550)	+= i2c-au1550.o
 obj-$(CONFIG_I2C_ELEKTOR)	+= i2c-elektor.o
+obj-$(CONFIG_I2C_GPIO)		+= i2c-gpio.o
 obj-$(CONFIG_I2C_HYDRA)		+= i2c-hydra.o
 obj-$(CONFIG_I2C_I801)		+= i2c-i801.o
 obj-$(CONFIG_I2C_I810)		+= i2c-i810.o
drivers/i2c/busses/i2c-gpio.c new file +213 -0

213 lines not on this page · raw message

include/linux/i2c-gpio.h new file +38 -0
new file mode 100644
index 0000000..7812407
@@ -0,0 +1,38 @@
+/*
+ * i2c-gpio interface to platform code
+ *
+ * Copyright (C) 2007 Atmel Corporation
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+#ifndef _LINUX_I2C_GPIO_H
+#define _LINUX_I2C_GPIO_H
+
+/**
+ * struct i2c_gpio_platform_data - Platform-dependent data for i2c-gpio
+ * @sda_pin: GPIO pin ID to use for SDA
+ * @scl_pin: GPIO pin ID to use for SCL
+ * @udelay: signal toggle delay. SCL frequency is (333 / udelay) kHz
+ * @timeout: clock stretching timeout in jiffies. If the slave keeps
+ *	SCL low for longer than this, the transfer will time out.
+ * @sda_is_open_drain: SDA is configured as open drain, i.e. the pin
+ *	isn't actively driven high when setting the output value high.
+ *	gpio_get_value() must return the actual pin state even if the
+ *	pin is configured as an output.
+ * @scl_is_open_drain: SCL is set up as open drain. Same requirements
+ *	as for sda_is_open_drain apply.
+ * @scl_is_output_only: SCL output drivers cannot be turned off.
+ */
+struct i2c_gpio_platform_data {
+	unsigned int	sda_pin;
+	unsigned int	scl_pin;
+	int		udelay;
+	int		timeout;
+	unsigned int	sda_is_open_drain:1;
+	unsigned int	scl_is_open_drain:1;
+	unsigned int	scl_is_output_only:1;
+};
+
+#endif /* _LINUX_I2C_GPIO_H */
Signature · 1 line
1.4.4.4
#1 lore raw
Quoted 2 lines · On Sat, 14 Apr 2007 14:56:47 +0200 Haavard Skinnemoen <[email protected]>…

o Default to a very low SCL frequency (6.6 kHz) if clock stretching isn't supported

This would have been true if I had remembered to save before generating the patch...

Updated patch below. Sorry about the mess.

Haavard

===================[cut here]======================
From: Haavard Skinnemoen <[email protected]>
Subject: [PATCH] Bitbanging i2c bus driver using the GPIO API

This is a very simple bitbanging i2c bus driver utilizing the new arch-neutral GPIO API. Useful for chips that don't have a built-in i2c controller, additional i2c busses, or testing purposes.

To use, include something similar to the following in the board-specific setup code:

  #include <linux/i2c-gpio.h>
  static struct i2c_gpio_platform_data i2c_gpio_data = {
	.sda_pin	= GPIO_PIN_FOO,
	.scl_pin	= GPIO_PIN_BAR,
  };
  static struct platform_device i2c_gpio_device = {
	.name		= "i2c-gpio",
	.id		= 0,
	.dev		= {
		.platform_data	= &i2c_gpio_data,
	},

};

Register this platform_device, set up the i2c pins as GPIO if required and you're ready to go. This will use default values for udelay and timeout, and will work with GPIO hardware that does not support open drain mode, but allows sensing of the SDA and SCL lines even when they are being driven.

---
 MAINTAINERS                   |    5 +
 drivers/i2c/busses/Kconfig    |    8 ++
 drivers/i2c/busses/Makefile   |    1 +
 drivers/i2c/busses/i2c-gpio.c |  215 +++++++++++++++++++++++++++++++++++++++++
 include/linux/i2c-gpio.h      |   38 +++++++
 5 files changed, 267 insertions(+), 0 deletions(-)
MAINTAINERS +5 -0
index ef84419..fdecda4 100644
@@ -1429,6 +1429,11 @@ L:	[email protected]
 W:	http://www.icp-vortex.com/
 S:	Supported
 
+GENERIC GPIO I2C DRIVER
+P:	Haavard Skinnemoen
+M:	[email protected]
+S:	Supported
+
 GENERIC HDLC DRIVER, N2, C101, PCI200SYN and WANXL DRIVERS
 P:	Krzysztof Halasa
 M:	[email protected]
drivers/i2c/busses/Kconfig +8 -0
index fb19dbb..52f79d1 100644
@@ -102,6 +102,14 @@ config I2C_ELEKTOR
 	  This support is also available as a module.  If so, the module 
 	  will be called i2c-elektor.
 
+config I2C_GPIO
+	tristate "GPIO-based bitbanging i2c driver"
+	depends on I2C && GENERIC_GPIO
+	select I2C_ALGOBIT
+	help
+	  This is a very simple bitbanging i2c driver utilizing the
+	  arch-neutral GPIO API to control the SCL and SDA lines.
+
 config I2C_HYDRA
 	tristate "CHRP Apple Hydra Mac I/O I2C interface"
 	depends on I2C && PCI && PPC_CHRP && EXPERIMENTAL
drivers/i2c/busses/Makefile +1 -0
index 290b540..68f2b05 100644
@@ -11,6 +11,7 @@ obj-$(CONFIG_I2C_AMD8111)	+= i2c-amd8111.o
 obj-$(CONFIG_I2C_AT91)		+= i2c-at91.o
 obj-$(CONFIG_I2C_AU1550)	+= i2c-au1550.o
 obj-$(CONFIG_I2C_ELEKTOR)	+= i2c-elektor.o
+obj-$(CONFIG_I2C_GPIO)		+= i2c-gpio.o
 obj-$(CONFIG_I2C_HYDRA)		+= i2c-hydra.o
 obj-$(CONFIG_I2C_I801)		+= i2c-i801.o
 obj-$(CONFIG_I2C_I810)		+= i2c-i810.o
drivers/i2c/busses/i2c-gpio.c new file +215 -0

215 lines not on this page · raw message

include/linux/i2c-gpio.h new file +38 -0
new file mode 100644
index 0000000..7812407
@@ -0,0 +1,38 @@
+/*
+ * i2c-gpio interface to platform code
+ *
+ * Copyright (C) 2007 Atmel Corporation
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+#ifndef _LINUX_I2C_GPIO_H
+#define _LINUX_I2C_GPIO_H
+
+/**
+ * struct i2c_gpio_platform_data - Platform-dependent data for i2c-gpio
+ * @sda_pin: GPIO pin ID to use for SDA
+ * @scl_pin: GPIO pin ID to use for SCL
+ * @udelay: signal toggle delay. SCL frequency is (333 / udelay) kHz
+ * @timeout: clock stretching timeout in jiffies. If the slave keeps
+ *	SCL low for longer than this, the transfer will time out.
+ * @sda_is_open_drain: SDA is configured as open drain, i.e. the pin
+ *	isn't actively driven high when setting the output value high.
+ *	gpio_get_value() must return the actual pin state even if the
+ *	pin is configured as an output.
+ * @scl_is_open_drain: SCL is set up as open drain. Same requirements
+ *	as for sda_is_open_drain apply.
+ * @scl_is_output_only: SCL output drivers cannot be turned off.
+ */
+struct i2c_gpio_platform_data {
+	unsigned int	sda_pin;
+	unsigned int	scl_pin;
+	int		udelay;
+	int		timeout;
+	unsigned int	sda_is_open_drain:1;
+	unsigned int	scl_is_open_drain:1;
+	unsigned int	scl_is_output_only:1;
+};
+
+#endif /* _LINUX_I2C_GPIO_H */
Signature · 1 line
1.4.4.4
#2 lore raw

Hi Haavard,

Quoted 44 lines · Haavard Skinnemoen
Quoted 2 lines · On Sat, 14 Apr 2007 14:56:47 +0200 Haavard Skinnemoen <[email protected]>…

o Default to a very low SCL frequency (6.6 kHz) if clock stretching isn't supported

This would have been true if I had remembered to save before generating the patch...

Updated patch below. Sorry about the mess.

Haavard

===================[cut here]======================
From: Haavard Skinnemoen <[email protected]>
Subject: [PATCH] Bitbanging i2c bus driver using the GPIO API

This is a very simple bitbanging i2c bus driver utilizing the new arch-neutral GPIO API. Useful for chips that don't have a built-in i2c controller, additional i2c busses, or testing purposes.

To use, include something similar to the following in the board-specific setup code:

  #include <linux/i2c-gpio.h>
  static struct i2c_gpio_platform_data i2c_gpio_data = {
	.sda_pin	= GPIO_PIN_FOO,
	.scl_pin	= GPIO_PIN_BAR,
  };
  static struct platform_device i2c_gpio_device = {
	.name		= "i2c-gpio",
	.id		= 0,
	.dev		= {
		.platform_data	= &i2c_gpio_data,
	},

};

Register this platform_device, set up the i2c pins as GPIO if required and you're ready to go. This will use default values for udelay and timeout, and will work with GPIO hardware that does not support open drain mode, but allows sensing of the SDA and SCL lines even when they are being driven.

Quoted 8 lines
---
 MAINTAINERS                   |    5 +
 drivers/i2c/busses/Kconfig    |    8 ++
 drivers/i2c/busses/Makefile   |    1 +
 drivers/i2c/busses/i2c-gpio.c |  215 +++++++++++++++++++++++++++++++++++++++++
 include/linux/i2c-gpio.h      |   38 +++++++
 5 files changed, 267 insertions(+), 0 deletions(-)

Random comments:

Quoted 21 lines
MAINTAINERS +5 -0
index ef84419..fdecda4 100644
@@ -1429,6 +1429,11 @@ L:	[email protected]
 W:	http://www.icp-vortex.com/
 S:	Supported
 
+GENERIC GPIO I2C DRIVER
+P:	Haavard Skinnemoen
+M:	[email protected]
+S:	Supported
+
 GENERIC HDLC DRIVER, N2, C101, PCI200SYN and WANXL DRIVERS
 P:	Krzysztof Halasa
 M:	[email protected]
drivers/i2c/busses/Kconfig +0 -0
index fb19dbb..52f79d1 100644

drivers/i2c/busses/Kconfig config I2C_ELEKTOR · line 106

Quoted 5 lines
drivers/i2c/busses/Kconfig +2 -0
@@ -102,6 +102,14 @@ config I2C_ELEKTOR
 	  This support is also available as a module.  If so, the module 
 	  will be called i2c-elektor.
 
+config I2C_GPIO
+	tristate "GPIO-based bitbanging i2c driver"

I removed "driver" from the option name.

drivers/i2c/busses/Kconfig config I2C_ELEKTOR · line 107

Quoted 1 line
drivers/i2c/busses/Kconfig +1 -0
+	depends on I2C && GENERIC_GPIO

I removed I2C, in accordance with Jan Engelhardt's patch:
http://lkml.org/lkml/2007/4/11/153

Quoted 147 lines
drivers/i2c/busses/Kconfig +5 -0
+	select I2C_ALGOBIT
+	help
+	  This is a very simple bitbanging i2c driver utilizing the
+	  arch-neutral GPIO API to control the SCL and SDA lines.
+
 config I2C_HYDRA
 	tristate "CHRP Apple Hydra Mac I/O I2C interface"
 	depends on I2C && PCI && PPC_CHRP && EXPERIMENTAL
drivers/i2c/busses/Makefile +1 -0
index 290b540..68f2b05 100644
@@ -11,6 +11,7 @@ obj-$(CONFIG_I2C_AMD8111)	+= i2c-amd8111.o
 obj-$(CONFIG_I2C_AT91)		+= i2c-at91.o
 obj-$(CONFIG_I2C_AU1550)	+= i2c-au1550.o
 obj-$(CONFIG_I2C_ELEKTOR)	+= i2c-elektor.o
+obj-$(CONFIG_I2C_GPIO)		+= i2c-gpio.o
 obj-$(CONFIG_I2C_HYDRA)		+= i2c-hydra.o
 obj-$(CONFIG_I2C_I801)		+= i2c-i801.o
 obj-$(CONFIG_I2C_I810)		+= i2c-i810.o
drivers/i2c/busses/i2c-gpio.c new file +121 -0
new file mode 100644
index 0000000..e427bfb
@@ -0,0 +1,215 @@
+/*
+ * Bitbanging i2c bus driver using the GPIO API
+ *
+ * Copyright (C) 2007 Atmel Corporation
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+#include <linux/i2c.h>
+#include <linux/i2c-algo-bit.h>
+#include <linux/i2c-gpio.h>
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+
+#include <asm/gpio.h>
+
+/* Toggle SDA by changing the direction of the pin */
+static void i2c_gpio_setsda_dir(void *data, int state)
+{
+	struct i2c_gpio_platform_data *pdata = data;
+
+	if (state)
+		gpio_direction_input(pdata->sda_pin);
+	else
+		gpio_direction_output(pdata->sda_pin, 0);
+}
+
+/*
+ * Toggle SDA by changing the output value of the pin. This is only
+ * valid for pins configured as open drain (i.e. setting the value
+ * high effectively turns off the output driver.)
+ */
+static void i2c_gpio_setsda_val(void *data, int state)
+{
+	struct i2c_gpio_platform_data *pdata = data;
+
+	gpio_set_value(pdata->sda_pin, state);
+}
+
+/* Toggle SCL by changing the direction of the pin. */
+static void i2c_gpio_setscl_dir(void *data, int state)
+{
+	struct i2c_gpio_platform_data *pdata = data;
+
+	if (state)
+		gpio_direction_input(pdata->scl_pin);
+	else
+		gpio_direction_output(pdata->scl_pin, 0);
+}
+
+/*
+ * Toggle SCL by changing the output value of the pin. This is used
+ * for pins that are configured as open drain and for output-only
+ * pins. The latter case will break the i2c protocol, but it will
+ * often work in practice.
+ */
+static void i2c_gpio_setscl_val(void *data, int state)
+{
+	struct i2c_gpio_platform_data *pdata = data;
+
+	gpio_set_value(pdata->scl_pin, state);
+}
+
+int i2c_gpio_getsda(void *data)
+{
+	struct i2c_gpio_platform_data *pdata = data;
+
+	return gpio_get_value(pdata->sda_pin);
+}
+
+int i2c_gpio_getscl(void *data)
+{
+	struct i2c_gpio_platform_data *pdata = data;
+
+	return gpio_get_value(pdata->scl_pin);
+}
+
+static int __init i2c_gpio_probe(struct platform_device *pdev)
+{
+	struct i2c_gpio_platform_data *pdata;
+	struct i2c_algo_bit_data *bit_data;
+	struct i2c_adapter *adap;
+	int ret;
+
+	pdata = pdev->dev.platform_data;
+	if (!pdata)
+		return -ENXIO;
+
+	ret = -ENOMEM;
+	adap = kzalloc(sizeof(struct i2c_adapter), GFP_KERNEL);
+	if (!adap)
+		goto err_alloc_adap;
+	bit_data = kzalloc(sizeof(struct i2c_algo_bit_data), GFP_KERNEL);
+	if (!bit_data)
+		goto err_alloc_bit_data;
+
+	ret = gpio_request(pdata->sda_pin, "sda");
+	if (ret)
+		goto err_request_sda;
+	ret = gpio_request(pdata->scl_pin, "scl");
+	if (ret)
+		goto err_request_scl;
+
+	if (pdata->sda_is_open_drain) {
+		gpio_direction_output(pdata->sda_pin, 1);
+		bit_data->setsda = i2c_gpio_setsda_val;
+	} else {
+		gpio_direction_input(pdata->sda_pin);
+		bit_data->setsda = i2c_gpio_setsda_dir;
+	}
+
+	if (pdata->scl_is_open_drain || pdata->scl_is_output_only) {
+		gpio_direction_output(pdata->scl_pin, 1);
+		bit_data->setscl = i2c_gpio_setscl_val;
+	} else {
+		gpio_direction_input(pdata->scl_pin);
+		bit_data->setscl = i2c_gpio_setscl_dir;
+	}
+

drivers/i2c/busses/i2c-gpio.c line 131

Quoted 10 lines
drivers/i2c/busses/i2c-gpio.c new file +10 -0
+	if (!pdata->scl_is_output_only)
+		bit_data->getscl = i2c_gpio_getscl;
+	bit_data->getsda = i2c_gpio_getsda;
+
+	if (pdata->udelay)
+		bit_data->udelay = pdata->udelay;
+	else if (pdata->scl_is_output_only)
+		bit_data->udelay = 50;			/* 6.6 kHz */
+	else
+		bit_data->udelay = 5;			/* 66 kHz */

For your information, I've been reworking the i2c-algo-bit driver quite a bit since our last discussion. It now has a 50/50 SCL duty cycle, so udelay = 50 leads to a 10 kHz clock (which is fine, as this is the minimum allowed for SMBus compatibility) and udelay = 5 leads to 100 kHz, the max for standard-mode I2C and SMBus.

http://lists.lm-sensors.org/pipermail/i2c/2007-March/001014.html

So your default values are OK, but I'll update the comments so that they match the behavior of the updated i2c-algo-bit driver.

drivers/i2c/busses/i2c-gpio.c line 141

Quoted 10 lines
drivers/i2c/busses/i2c-gpio.c new file +10 -0
+
+	if (pdata->timeout)
+		bit_data->timeout = pdata->timeout;
+	else
+		bit_data->timeout = HZ / 10;		/* 100 ms */
+
+	bit_data->data = pdata;
+
+	adap->owner = THIS_MODULE;
+	snprintf(adap->name, I2C_NAME_SIZE, "i2c-gpio%d", pdev->id);

Changed I2C_NAME_SIZE to sizeof(adap->name), in accordance to this patch:
http://lists.lm-sensors.org/pipermail/i2c/2007-March/000881.html

drivers/i2c/busses/i2c-gpio.c line 145

Quoted 4 lines
drivers/i2c/busses/i2c-gpio.c new file +4 -0
+	adap->algo_data = bit_data;
+	adap->dev.parent = &pdev->dev;
+
+	ret = i2c_bit_add_bus(adap);

I suspect that you will soon want to use the new i2c_bit_add_numbered_bus() instead, so that you can enumerate I2C devices on the bus directly from the platform code.

http://lists.lm-sensors.org/pipermail/i2c/2007-March/000974.html

Quoted 83 lines
drivers/i2c/busses/i2c-gpio.c new file +70 -0
+	if (ret)
+		goto err_add_bus;
+
+	platform_set_drvdata(pdev, adap);
+
+	dev_info(&pdev->dev, "using pins %u (sda) and %u (scl%s)\n",
+		 pdata->sda_pin, pdata->scl_pin,
+		 pdata->scl_is_output_only
+		 ? ", no clock stretching" : "");
+
+	return 0;
+
+err_add_bus:
+	gpio_free(pdata->scl_pin);
+err_request_scl:
+	gpio_free(pdata->sda_pin);
+err_request_sda:
+	kfree(bit_data);
+err_alloc_bit_data:
+	kfree(adap);
+err_alloc_adap:
+	return ret;
+}
+
+static int __exit i2c_gpio_remove(struct platform_device *pdev)
+{
+	struct i2c_gpio_platform_data *pdata;
+	struct i2c_adapter *adap;
+
+	adap = platform_get_drvdata(pdev);
+	pdata = pdev->dev.platform_data;
+
+	i2c_del_adapter(adap);
+	gpio_free(pdata->scl_pin);
+	gpio_free(pdata->sda_pin);
+	kfree(adap->algo_data);
+	kfree(adap);
+
+	return 0;
+}
+
+static struct platform_driver i2c_gpio_driver = {
+	.driver		= {
+		.name	= "i2c-gpio",
+		.owner	= THIS_MODULE,
+	},
+	.remove		= __exit_p(i2c_gpio_remove),
+};
+
+static int __init i2c_gpio_init(void)
+{
+	int ret;
+
+	ret = platform_driver_probe(&i2c_gpio_driver, i2c_gpio_probe);
+	if (ret)
+		printk(KERN_ERR "i2c-gpio: probe failed: %d\n", ret);
+
+	return ret;
+}
+module_init(i2c_gpio_init);
+
+static void __exit i2c_gpio_exit(void)
+{
+	platform_driver_unregister(&i2c_gpio_driver);
+}
+module_exit(i2c_gpio_exit);
+
+MODULE_AUTHOR("Haavard Skinnemoen <[email protected]>");
+MODULE_DESCRIPTION("Platform-independent bitbanging i2c driver");
+MODULE_LICENSE("GPL");
include/linux/i2c-gpio.h new file +7 -0
new file mode 100644
index 0000000..7812407
@@ -0,0 +1,38 @@
+/*
+ * i2c-gpio interface to platform code
+ *
+ * Copyright (C) 2007 Atmel Corporation
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as

include/linux/i2c-gpio.h line 17

Quoted 10 lines
include/linux/i2c-gpio.h new file +10 -0
+ * published by the Free Software Foundation.
+ */
+#ifndef _LINUX_I2C_GPIO_H
+#define _LINUX_I2C_GPIO_H
+
+/**
+ * struct i2c_gpio_platform_data - Platform-dependent data for i2c-gpio
+ * @sda_pin: GPIO pin ID to use for SDA
+ * @scl_pin: GPIO pin ID to use for SCL
+ * @udelay: signal toggle delay. SCL frequency is (333 / udelay) kHz

I changed that to (500 / udelay).

Quoted 11 lines
include/linux/i2c-gpio.h new file +11 -0
+ * @timeout: clock stretching timeout in jiffies. If the slave keeps
+ *	SCL low for longer than this, the transfer will time out.
+ * @sda_is_open_drain: SDA is configured as open drain, i.e. the pin
+ *	isn't actively driven high when setting the output value high.
+ *	gpio_get_value() must return the actual pin state even if the
+ *	pin is configured as an output.
+ * @scl_is_open_drain: SCL is set up as open drain. Same requirements
+ *	as for sda_is_open_drain apply.
+ * @scl_is_output_only: SCL output drivers cannot be turned off.
+ */
+struct i2c_gpio_platform_data {

include/linux/i2c-gpio.h line 38

Quoted 10 lines
include/linux/i2c-gpio.h new file +10 -0
+	unsigned int	sda_pin;
+	unsigned int	scl_pin;
+	int		udelay;
+	int		timeout;
+	unsigned int	sda_is_open_drain:1;
+	unsigned int	scl_is_open_drain:1;
+	unsigned int	scl_is_output_only:1;
+};
+
+#endif /* _LINUX_I2C_GPIO_H */

Otherwise it looks OK to me, I take the patch. If others have comments or objections, just speak up and submit incremental patches as needed.

Now I would like to see platform code actually using this.

Signature · 1 line
Jean Delvare
#3 lore raw
Quoted 4 lines · Jean Delvare

Otherwise it looks OK to me, I take the patch. If others have comments or objections, just speak up and submit incremental patches as needed.

Now I would like to see platform code actually using this.

Any idea how similar this new driver is to the scx200_i2c driver? As far as I can tell that driver is already a bit banged i2c over gpio lines.

--
Len Sorensen

#4 lore raw

Hi Len,

Quoted 9 lines · Lennart Sorensen
Quoted 4 lines · Jean Delvare

Otherwise it looks OK to me, I take the patch. If others have comments or objections, just speak up and submit incremental patches as needed.

Now I would like to see platform code actually using this.

Any idea how similar this new driver is to the scx200_i2c driver? As far as I can tell that driver is already a bit banged i2c over gpio lines.

The major difference is that the implementation in scx200_i2c is hardware-specific, while the i2c-gpio driver is a generic one, so it's a lot better.

What this means is that i2c-gpio obsoletes scx200_i2c, so I am inclined to delete scx200_i2c right away. I'm not even sure anyone still uses it now that scx200_acb has been fixed and is reported to work very well. If anyone really needs to do I2C over GPIO pins on SCx200, this should be reimplemented on top of i2c-gpio.

It also looks to me like i2c-ixp2000 and i2c-ixp4xx are obsoleted by i2c-gpio. Deepak, can you please try using i2c-gpio instead and confirm that i2c-ixp2000 and i2c-ixp4xx can be deleted?

Thanks,

Signature · 1 line
Jean Delvare
#5 lore raw
Quoted 17 lines · Jean Delvare

Hi Len,

Quoted 9 lines · Lennart Sorensen
Quoted 4 lines · Jean Delvare

Otherwise it looks OK to me, I take the patch. If others have comments or objections, just speak up and submit incremental patches as needed.

Now I would like to see platform code actually using this.

Any idea how similar this new driver is to the scx200_i2c driver? As far as I can tell that driver is already a bit banged i2c over gpio lines.

The major difference is that the implementation in scx200_i2c is hardware-specific, while the i2c-gpio driver is a generic one, so it's a lot better.

Quoted 5 lines

What this means is that i2c-gpio obsoletes scx200_i2c, so I am inclined to delete scx200_i2c right away. I'm not even sure anyone still uses it now that scx200_acb has been fixed and is reported to work very well. If anyone really needs to do I2C over GPIO pins on SCx200, this should be reimplemented on top of i2c-gpio.

It would also force us to move the Geode GPIO pins to the generic infrastructure, so that would be good. I say delete it - if anybody needs that functionality, then we'll do it the right way.

Jordan

#6 lore raw
In reply to Jean Delvare
Quoted 9 lines · Jean Delvare

The major difference is that the implementation in scx200_i2c is hardware-specific, while the i2c-gpio driver is a generic one, so it's a lot better.

What this means is that i2c-gpio obsoletes scx200_i2c, so I am inclined to delete scx200_i2c right away. I'm not even sure anyone still uses it now that scx200_acb has been fixed and is reported to work very well. If anyone really needs to do I2C over GPIO pins on SCx200, this should be reimplemented on top of i2c-gpio.

I use scx200_i2c. The SBC I am using doesn't work with the scx200_acb (different pins on the cpu after all). I would love to see it reimplemented using the generic interface, since there isn't that many lines of code involved as far as I can tell. Well scx200_acb works fine on a Geode LX board, but not on a Geode SC1200 board. Unless it has been fixed since 2.6.18, then it still doesn't work, but scx200_i2c works perfectly fine.

Quoted 3 lines

It also looks to me like i2c-ixp2000 and i2c-ixp4xx are obsoleted by i2c-gpio. Deepak, can you please try using i2c-gpio instead and confirm that i2c-ixp2000 and i2c-ixp4xx can be deleted?

--
Len Sorensen

#7 lore raw

Hi Lennart,

Quoted 11 lines · Lennart Sorensen
Quoted 9 lines · Jean Delvare

The major difference is that the implementation in scx200_i2c is hardware-specific, while the i2c-gpio driver is a generic one, so it's a lot better.

What this means is that i2c-gpio obsoletes scx200_i2c, so I am inclined to delete scx200_i2c right away. I'm not even sure anyone still uses it now that scx200_acb has been fixed and is reported to work very well. If anyone really needs to do I2C over GPIO pins on SCx200, this should be reimplemented on top of i2c-gpio.

Quoted 7 lines

I use scx200_i2c. The SBC I am using doesn't work with the scx200_acb (different pins on the cpu after all). I would love to see it reimplemented using the generic interface, since there isn't that many lines of code involved as far as I can tell. Well scx200_acb works fine on a Geode LX board, but not on a Geode SC1200 board. Unless it has been fixed since 2.6.18, then it still doesn't work, but scx200_i2c works perfectly fine.

The scx200_acb driver was heavily modified in 2.6.17 and 2.6.18, not much since then. I am not familiar with the hardware so I can't comment on which chips are supposed to work and which aren't.

Signature · 1 line
Jean Delvare
#8 lore raw
Quoted 3 lines · Jean Delvare

The scx200_acb driver was heavily modified in 2.6.17 and 2.6.18, not much since then. I am not familiar with the hardware so I can't comment on which chips are supposed to work and which aren't.

Well 2.6.18's scx200_acb works on the geode LX module I have, but no on the geode sc1200 module (where scx200_i2c does work).

scx200_acb doesn't detect any device that it can drive (nothing in dmesg at all when loaded) on the sc1200. I believe the main changes that happened to scx200_acb was adding support for the newer CSxxxx chipsets, such as the one used with the geode LX (which does work now).

--
Len Sorensen

#9 lore raw
Quoted 4 lines · On 22/04/07 11:41 -0400, [email protected] wrote:

scx200_acb doesn't detect any device that it can drive (nothing in dmesg at all when loaded) on the sc1200. I believe the main changes that happened to scx200_acb was adding support for the newer CSxxxx chipsets, such as the one used with the geode LX (which does work now).

That ISA detection is almost definately suffering from severe bit rot. Once you get past that though, you should be fine - the silicon behind the ACB hasn't changed since the MediaGX days.

Jordan

Signature · 4 lines
Jordan Crouse
Senior Linux Engineer
Advanced Micro Devices, Inc.
<www.amd.com/embeddedprocessors>
#10 lore raw
In reply to Lennart Sorensen

Lennart,

Quoted 10 lines · Lennart Sorensen
Quoted 3 lines · Jean Delvare

The scx200_acb driver was heavily modified in 2.6.17 and 2.6.18, not much since then. I am not familiar with the hardware so I can't comment on which chips are supposed to work and which aren't.

Well 2.6.18's scx200_acb works on the geode LX module I have, but no on the geode sc1200 module (where scx200_i2c does work).

scx200_acb doesn't detect any device that it can drive (nothing in dmesg at all when loaded) on the sc1200.

On the SCx200, the scx200_acb driver doesn't actually detect the device. It uses arbitrary I/O addresses and hopes for the best. By default, two buses are created, at 0x820 and 0x840. This might simply not be the right addresses for your board. See Documentation/i2c/busses/scx200_acb.

You should enable CONFIG_I2C_DEBUG_BUS and see what the driver says when being loaded.

I seem to remember there has been a patch floating around to auto-detect the right ports back in June 2006, but it seems to have been lost somehow. Jordan, do you remember?

Quoted 3 lines

I believe the main changes that
happened to scx200_acb was adding support for the newer CSxxxx chipsets, such as the one used with the geode LX (which does work now).

Not only. There was also a log of bug fixing.

Signature · 1 line
Jean Delvare
#11 lore raw
Quoted 3 lines · Jean Delvare

I seem to remember there has been a patch floating around to auto-detect the right ports back in June 2006, but it seems to have been lost somehow. Jordan, do you remember?

I don't remember that, and Google isn't helping, either. Regardless, randomly hitting ISA ports seems scary to me. I know its a pain to use the module params all the time, but thats really probably the cleanest way to to it without carrying around a special patch for your system.

Jordan

Signature · 4 lines
Jordan Crouse
Senior Linux Engineer
Advanced Micro Devices, Inc.
<www.amd.com/embeddedprocessors>
#12 lore raw
Quoted 9 lines · Jordan Crouse
Quoted 3 lines · Jean Delvare

I seem to remember there has been a patch floating around to auto-detect the right ports back in June 2006, but it seems to have been lost somehow. Jordan, do you remember?

I don't remember that, and Google isn't helping, either. Regardless, randomly hitting ISA ports seems scary to me. I know its a pain to use the module params all the time, but thats really probably the cleanest way to to it without carrying around a special patch for your system.

After digging some more, I finally managed to get my hands back on it:
http://lists.lm-sensors.org/pipermail/lm-sensors/2006-April/016009.html
http://lists.lm-sensors.org/pipermail/lm-sensors/2006-May/016053.html

This isn't "randomly hitting ISA ports", this is reading the base I/O addresses from LPC configuration space. That's much better than letting the user set the addresses using module parameters.

Lennart, can you please provide the output of the following commands on your system?

isadump 0x2e 0x2f 5
isadump 0x2e 0x2f 6
isadump 0x4e 0x4f 5
isadump 0x4e 0x4f 6

(isadump is part of the lm_sensors package.)

Signature · 1 line
Jean Delvare
#13 lore raw
Quoted 8 lines · Jean Delvare

After digging some more, I finally managed to get my hands back on it:
http://lists.lm-sensors.org/pipermail/lm-sensors/2006-April/016009.html
http://lists.lm-sensors.org/pipermail/lm-sensors/2006-May/016053.html

This isn't "randomly hitting ISA ports", this is reading the base I/O addresses from LPC configuration space. That's much better than letting the user set the addresses using module parameters.

Quoted 5 lines

Lennart, can you please provide the output of the following commands on your system?

isadump 0x2e 0x2f 5
isadump 0x2e 0x2f 6

These return all 0xff. For example:

router2:~# isadump 0x2e 0x2f 5
WARNING! Running this program can cause system crashes, data loss and worse!
I will probe address register 0x2e and data register 0x2f.
Probing bank 5 using bank register 0x07.
Continue? [Y/n]
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
10: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
20: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
30: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
40: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
50: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
60: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
70: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
80: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
90: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
a0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
b0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
c0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
d0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
e0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
f0: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
Quoted 2 lines

isadump 0x4e 0x4f 5
isadump 0x4e 0x4f 6

These return all 0x00

On the Geode LX800 where scx200_acb does work all 4 commands return all 0xff.

--
Len Sorensen

#14 lore raw
Quoted 5 lines · On 26/04/07 09:29 -0400, [email protected] wrote:

These return all 0x00

On the Geode LX800 where scx200_acb does work all 4 commands return all 0xff.

There is no ISA on the LX.

Signature · 4 lines
Jordan Crouse
Senior Linux Engineer
Advanced Micro Devices, Inc.
<www.amd.com/embeddedprocessors>
#15 lore raw
Quoted 1 line · Jordan Crouse

There is no ISA on the LX.

Isn't LPC a good equivalant?

--
Len Sorensen

#16 lore raw
Quoted 4 lines · Lennart Sorensen
Quoted 1 line · Jordan Crouse

There is no ISA on the LX.

Isn't LPC a good equivalant?

I suspect that Jordan meant that there is no LPC either. In which case, of course, there's no way scx200_acb will ever work on that system, and GPIO-based bitbanging is the way to go. Just using i2c-gpio rather than scx200_i2c.

Signature · 1 line
Jean Delvare
#17 lore raw
Quoted 4 lines · Jean Delvare

I suspect that Jordan meant that there is no LPC either. In which case, of course, there's no way scx200_acb will ever work on that system, and GPIO-based bitbanging is the way to go. Just using i2c-gpio rather than scx200_i2c.

Ehm, there most certainly is LPC on a Geode LX800 + CS5536. I am currently using the LPC bus on it, and scx200_acb in 2.6.18 works perfectly with the I2C on it too.

--
Len Sorensen

#18 lore raw
In reply to Jean Delvare
Quoted 10 lines · Jean Delvare
Quoted 4 lines · Lennart Sorensen
Quoted 1 line · Jordan Crouse

There is no ISA on the LX.

Isn't LPC a good equivalent?

I suspect that Jordan meant that there is no LPC either. In which case, of course, there's no way scx200_acb will ever work on that system, and GPIO-based bitbanging is the way to go. Just using i2c-gpio rather than scx200_i2c.

This is getting confusing... :) On the sc1200 and and GX1, the ACB bus is accessed through ISA ports. There is no ISA on the cs5535/cs5536 companion chips (accompanying the GX and LX processors), and the ACB is accessed through PCI. All the platforms have LPC, but that really doesn't matter for the purposes of this discussion.

The silicon block that implements the ACB has been generally unchanged over the last 6 or 7 years, so the same driver should support any of the platforms, assuming one can invoke the magic spells to get at the hardware. Jean and I have been concentrating most of our effort on getting the GX and LX to work through PCI, and really haven't concentrated our efforts on the older processors. Thats not to say it won't work, but its probably not plug and play.

Jordan

Signature · 4 lines
Jordan Crouse
Senior Linux Engineer
Advanced Micro Devices, Inc.
<www.amd.com/embeddedprocessors>
#19 lore raw
Quoted 6 lines · Jordan Crouse

This is getting confusing... :) On the sc1200 and and GX1, the ACB bus is accessed through ISA ports. There is no ISA on the cs5535/cs5536 companion chips (accompanying the GX and LX processors), and the ACB is accessed through PCI. All the platforms have LPC, but that really doesn't matter for the purposes of this discussion.

Quoted 7 lines

The silicon block that implements the ACB has been generally unchanged over the last 6 or 7 years, so the same driver should support any of the platforms, assuming one can invoke the magic spells to get at the hardware. Jean and I have been concentrating most of our effort on getting the GX and LX to work through PCI, and really haven't concentrated our efforts on the older processors. Thats not to say it won't work, but its probably not plug and play.

Well the SC1200 has an ACB but it is disabled by default through one of the gpio multiplex selector bits. According to the data sheet only the bios knows where that is. So instead the gpio's (12 and 13) that are multiplexed with acb #2 are driven by scx200_i2c using the bit banged interface. Makes it work for those of us that don't have access to changing the bios.

Unfortuantely I don't think the scx200 gpio's have been rewritten to use the new gpio interface. If it was I would try to see if the new i2c driver would work if told to use gpio 12 and 13.

scx200_i2c does depend on i2c-algo-bit so there is really very little code inside scx200_i2c itself. Does the new driver replace i2c-algo-bit as well or does it use it?

--
Len Sorensen

#20 lore raw
Quoted 3 lines · Lennart Sorensen

scx200_i2c does depend on i2c-algo-bit so there is really very little code inside scx200_i2c itself. Does the new driver replace i2c-algo-bit as well or does it use it?

It uses it.

Signature · 1 line
Jean Delvare