Skip to contentSkip to conversation

i2c: add generic I2C multiplexer using gpio api

PATCH Superseded Peter Korsgaard 4 participants 9 messages Jul 4, 2010 – Jul 20, 2010 7 messages not in the archive

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: add generic I2C multiplexer using gpio api

Add an i2c bus driver providing virtual i2c busses using a hardware MUX sitting on a master bus and controlled through gpio pins.

E.G. something like:

---------- ---------- Virtual bus 1 - - - - -

 |          | SCL/SDA    |          |-------------- |           |
 |          |------------|          |
 |          |            |          | Virtual bus 2 |           |
 |  Linux   | GPIO 1..N  |   MUX    |---------------   Devices
 |          |------------|          |               |           |
 |          |            |          | Virtual bus M
 |          |            |          |---------------|           |
  ----------              ----------                  - - - - -

SCL/SDA of the master I2C bus is multiplexed to virtual bus 1..M according to the settings of the GPIO pins 1..N.

---
 Documentation/i2c/busses/i2c-gpiomux |   65 +++++++++++
 MAINTAINERS                          |    7 ++
 drivers/i2c/busses/Kconfig           |   12 ++
 drivers/i2c/busses/Makefile          |    1 +
 drivers/i2c/busses/i2c-gpiomux.c     |  196 ++++++++++++++++++++++++++++++++++
 include/linux/i2c-gpiomux.h          |   35 ++++++
 6 files changed, 316 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/i2c/busses/i2c-gpiomux
 create mode 100644 drivers/i2c/busses/i2c-gpiomux.c
 create mode 100644 include/linux/i2c-gpiomux.h
Documentation/i2c/busses/i2c-gpiomux new file +65 -0
new file mode 100644
index 0000000..b0a7746
@@ -0,0 +1,65 @@
+Kernel driver i2c-gpiomux
+
+Author: Peter Korsgaard <peter.korsgaard-ob4gmnvZ1/[email protected]>
+
+Description
+-----------
+
+i2c-gpiomux is an i2c bus driver providing virtual I2C busses from a
+master I2C bus and a hardware MUX controlled through GPIO pins.
+
+E.G.:
+
+  ----------              ----------  Virtual bus 1   - - - - -
+ |          | SCL/SDA    |          |-------------- |           |
+ |          |------------|          |
+ |          |            |          | Virtual bus 2 |           |
+ |  Linux   | GPIO 1..N  |   MUX    |---------------   Devices
+ |          |------------|          |               |           |
+ |          |            |          | Virtual bus M
+ |          |            |          |---------------|           |
+  ----------              ----------                  - - - - -
+
+SCL/SDA of the master I2C bus is multiplexed to virtual bus 1..M
+according to the settings of the GPIO pins 1..N.
+
+Usage
+-----
+
+i2c-gpiomux uses the platform bus, so you need to provide a struct
+platform_device with the platform_data pointing to a struct
+gpiomux_i2c_platform_data with the I2C adapter number of the master
+bus, the number of virtual busses to create and the GPIO pins used
+to control it. See include/linux/i2c-gpiomux.h for details.
+
+E.G. something like this for a MUX providing 4 virtual busses
+controlled through 3 GPIO pins:
+
+#include <linux/i2c-gpiomux.h>
+#include <linux/platform_device.h>
+
+static unsigned myboard_gpiomux_pins[] = {
+	AT91_PIN_PC26, AT91_PIN_PC25, AT91_PIN_PC24
+};
+
+static unsigned myboard_gpiomux_values[] = {
+	0, 1, 2, 3
+};
+
+static struct gpiomux_i2c_platform_data myboard_i2cmux_data = {
+	.parent		= 1,
+	.base_nr	= 2,
+	.busses		= ARRAY_SIZE(myboard_gpiomux_values),
+	.gpios		= ARRAY_SIZE(myboard_gpiomux_pins),
+	.gpio		= myboard_gpiomux_pins,
+	.values		= myboard_gpiomux_values,
+	.idle		= 4,
+};
+
+static struct platform_device myboard_i2cmux = {
+	.name		= "i2c-gpiomux",
+	.id		= 0,
+	.dev		= {
+		.platform_data	= &myboard_i2cmux_data,
+	},
+};
MAINTAINERS +7 -0
index 7642365..f80ad93 100644
@@ -2453,6 +2453,13 @@ S:	Supported
 F:	drivers/i2c/busses/i2c-gpio.c
 F:	include/linux/i2c-gpio.h
 
+GENERIC GPIO I2C MULTIPLEXER DRIVER
+M:	Peter Korsgaard <peter.korsgaard-ob4gmnvZ1/[email protected]>
+L:	[email protected]
+S:	Supported
+F:	drivers/i2c/busses/i2c-gpiomux.c
+F:	include/linux/i2c-gpiomux.h
+
 GENERIC HDLC (WAN) DRIVERS
 M:	Krzysztof Halasa <khc-9GfyWEdoJtJmR6Xm/[email protected]>
 W:	http://www.kernel.org/pub/linux/utils/net/hdlc/
drivers/i2c/busses/Kconfig +12 -0
index bceafbf..055b0f1 100644
@@ -364,6 +364,18 @@ config I2C_GPIO
 	  This is a very simple bitbanging I2C driver utilizing the
 	  arch-neutral GPIO API to control the SCL and SDA lines.
 
+config I2C_GPIOMUX
+	tristate "GPIO-based I2C multiplexer"
+	depends on GENERIC_GPIO
+	help
+	  If you say yes to this option, support will be included for a
+	  GPIO based I2C multiplexer. This driver provides virtual I2C
+	  busses from a master bus through a MUX controlled through
+	  the generic GPIO interface.
+
+	  This driver can also be built as a module.  If so, the module
+	  will be called i2c-gpiomux.
+
 config I2C_HIGHLANDER
 	tristate "Highlander FPGA SMBus interface"
 	depends on SH_HIGHLANDER
drivers/i2c/busses/Makefile +1 -0
index 936880b..5ec15ec 100644
@@ -35,6 +35,7 @@ obj-$(CONFIG_I2C_CPM)		+= i2c-cpm.o
 obj-$(CONFIG_I2C_DAVINCI)	+= i2c-davinci.o
 obj-$(CONFIG_I2C_DESIGNWARE)	+= i2c-designware.o
 obj-$(CONFIG_I2C_GPIO)		+= i2c-gpio.o
+obj-$(CONFIG_I2C_GPIOMUX)	+= i2c-gpiomux.o
 obj-$(CONFIG_I2C_HIGHLANDER)	+= i2c-highlander.o
 obj-$(CONFIG_I2C_IBM_IIC)	+= i2c-ibm_iic.o
 obj-$(CONFIG_I2C_IMX)		+= i2c-imx.o
drivers/i2c/busses/i2c-gpiomux.c new file +196 -0

196 lines not on this page · raw message

include/linux/i2c-gpiomux.h new file +35 -0
new file mode 100644
index 0000000..2388ff2
@@ -0,0 +1,35 @@
+/*
+ * i2c-gpiomux interface to platform code
+ *
+ * Peter Korsgaard <peter.korsgaard-ob4gmnvZ1/[email protected]>
+ *
+ * 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_GPIOMUX_H
+#define _LINUX_I2C_GPIOMUX_H
+
+/**
+ * struct gpiomux_i2c_platform_data - Platform-dependent data for i2c-gpiomux
+ * @parent: Parent I2C bus adapter number
+ * @base_nr: Base bus number value to number adapters from
+ * @busses: Number of multiplexer positions (busses to instantiate)
+ * @gpios: Number of gpios used to control MUX
+ * @gpio: Array of @gpios gpio numbers used to control MUX
+ * @values: Array of @bussed bitmasks of gpio settings (low/high) for each
+ *	position
+ * @idle: Bitmask to write to MUX when idle
+ */
+struct gpiomux_i2c_platform_data {
+	int parent;
+	int base_nr;
+	int busses;
+	int gpios;
+	unsigned *gpio;
+	unsigned *values;
+	unsigned idle;
+};
+
+#endif /* _LINUX_I2C_GPIOMUX_H */
Signature · 1 line
1.7.1
#2 lore raw
In reply to a message not in the archive
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Peter" == Peter Korsgaard <peter.korsgaard-ob4gmnvZ1/[email protected]> writes:

Peter> Add an i2c bus driver providing virtual i2c busses using a hardware Peter> MUX sitting on a master bus and controlled through gpio pins.

Peter> E.G. something like:

Peter> ---------- ---------- Virtual bus 1 - - - - -

 Peter>  |          | SCL/SDA    |          |-------------- |           |
 Peter>  |          |------------|          |
 Peter>  |          |            |          | Virtual bus 2 |           |
 Peter>  |  Linux   | GPIO 1..N  |   MUX    |---------------   Devices
 Peter>  |          |------------|          |               |           |
 Peter>  |          |            |          | Virtual bus M
 Peter>  |          |            |          |---------------|           |

Peter> ---------- ---------- - - - - -

Peter> SCL/SDA of the master I2C bus is multiplexed to virtual bus 1..M Peter> according to the settings of the GPIO pins 1..N.

Peter> Signed-off-by: Peter Korsgaard <peter.korsgaard-ob4gmnvZ1/[email protected]>

Comments?

Signature · 1 line
Bye, Peter Korsgaard
#4 lore raw
In reply to a message not in the archive
Quoted 15 lines · Peter Korsgaard
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Peter" == Peter Korsgaard <peter.korsgaard-ob4gmnvZ1/[email protected]> writes:

Peter> Add an i2c bus driver providing virtual i2c busses using a hardware Peter> MUX sitting on a master bus and controlled through gpio pins.

Peter> E.G. something like:

Peter> ---------- ---------- Virtual bus 1 - - - - -

 Peter>  |          | SCL/SDA    |          |-------------- |           |
 Peter>  |          |------------|          |
 Peter>  |          |            |          | Virtual bus 2 |           |
 Peter>  |  Linux   | GPIO 1..N  |   MUX    |---------------   Devices
 Peter>  |          |------------|          |               |           |
 Peter>  |          |            |          | Virtual bus M
 Peter>  |          |            |          |---------------|           |
Quoted 8 lines

Peter> ---------- ---------- - - - - -

Peter> SCL/SDA of the master I2C bus is multiplexed to virtual bus 1..M Peter> according to the settings of the GPIO pins 1..N.

Peter> Signed-off-by: Peter Korsgaard <peter.korsgaard-ob4gmnvZ1/[email protected]>

Comments?

Michael and myself are in the process of adding core support for bus multiplexing to the i2c subsystem. There is no point in reviewing more specific implementations until the core part is merged (which is scheduled to happen in kernel 2.6.36.)

Signature · 1 line
Jean Delvare
#6 lore raw
In reply to a message not in the archive
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Jean" == Jean Delvare <[email protected]> writes:

Hi,

Jean> Michael and myself are in the process of adding core support for Jean> bus multiplexing to the i2c subsystem. There is no point in Jean> reviewing more specific implementations until the core part is Jean> merged (which is scheduled to happen in kernel 2.6.36.)

But this patch is independent from that work as the mux access isn't through I2C, hence no changes to i2c-core needed.

Do you want me to resend or do you still have the original patch?

Signature · 1 line
Bye, Peter Korsgaard
#8 lore raw
In reply to a message not in the archive
Quoted 11 lines · Peter Korsgaard
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Jean" == Jean Delvare <[email protected]> writes:

Hi,

Jean> Michael and myself are in the process of adding core support for Jean> bus multiplexing to the i2c subsystem. There is no point in Jean> reviewing more specific implementations until the core part is Jean> merged (which is scheduled to happen in kernel 2.6.36.)

But this patch is independent from that work as the mux access isn't through I2C, hence no changes to i2c-core needed.

The problem is exactly that your patch is independent. It _should_ build on top of Michael's work, to avoid redundancy.

Quoted 1 line

Do you want me to resend or do you still have the original patch?

I have it, but I don't intend to look at it. First I get Michael's work upstream, and then I wait for you to submit a new patch taking benefit of it.

Signature · 1 line
Jean Delvare
#11 lore raw
In reply to a message not in the archive

Peter Korsgaard said the following:

Quoted 11 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Jean" == Jean Delvare <[email protected]> writes:

Hi,

Jean> Michael and myself are in the process of adding core support for Jean> bus multiplexing to the i2c subsystem. There is no point in Jean> reviewing more specific implementations until the core part is Jean> merged (which is scheduled to happen in kernel 2.6.36.)

But this patch is independent from that work as the mux access isn't through I2C, hence no changes to i2c-core needed.

i2c-mux patch does not expect path control via i2c. Your scenario fits perfectly.

Signature · 2 lines
KR
Michael Lawnick
#10 lore raw
In reply to a message not in the archive
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Jean" == Jean Delvare <[email protected]> writes:

Jean> The problem is exactly that your patch is independent. It _should_ Jean> build on top of Michael's work, to avoid redundancy.

Why? We've discussed this for years, E.G.:

http://www.mail-archive.com/[email protected]/msg01539.html

None of the locking changes in core are needed when the MUX isn't controlled through I2C. I guess I could use the i2c-mux.c functions, as there's some overlap in the trivial gpiomux_xfer/gpiomux_func functions, but that can easily be done once the mux stuff is merged.

Signature · 1 line
Bye, Peter Korsgaard
#13 lore raw
In reply to a message not in the archive
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Michael" == Michael Lawnick <[email protected]> writes:

But this patch is independent from that work as the mux access isn't through I2C, hence no changes to i2c-core needed.

Michael> i2c-mux patch does not expect path control via i2c. Michael> Your scenario fits perfectly.

Hmm, I'll take a closer look at the last version then - sorry. In the past it afaik did. What is the point of the i2c-core changes if path control isn't via i2c?

Signature · 1 line
Bye, Peter Korsgaard
#15 lore raw
In reply to a message not in the archive

Peter Korsgaard said the following:

Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Michael" == Michael Lawnick <[email protected]> writes:

But this patch is independent from that work as the mux access isn't through I2C, hence no changes to i2c-core needed.

Quoted 7 lines

Michael> i2c-mux patch does not expect path control via i2c. Michael> Your scenario fits perfectly.

Hmm, I'll take a closer look at the last version then - sorry. In the past it afaik did. What is the point of the i2c-core changes if path control isn't via i2c?

The changes in i2c-core are mostly for traversing the tree and check for duplicate addresses.

Quoted 1 line

From i2c-core and user space view there is no logical difference between

a h/w adapter and a multiplexer. They are all handled the same. But I fear your question arises from your different approach. May be it gets more clear after looking into i2c-mux.c and pca954x.c.

Signature · 2 lines
KR
Michael Lawnick