i2c: add generic I2C multiplexer using gpio api
Subsystems (3)
PATCH v4 1/1 i2c: add generic I2C multiplexer using gpio api
Add an i2c mux driver providing access to i2c bus segments using a hardware MUX sitting on a master bus and controlled through gpio pins.
E.G. something like:
---------- ---------- Bus segment 1 - - - - -
| | SCL/SDA | |-------------- | | | |------------| | | | | | Bus segment 2 | | | Linux | GPIO 1..N | MUX |--------------- Devices | |------------| | | | | | | | Bus segment M | | | |---------------| | ---------- ---------- - - - - -
SCL/SDA of the master I2C bus is multiplexed to bus segment 1..M according to the settings of the GPIO pins 1..N.
---
Changes since v3:
- Adjust according to Jean's 2nd comments
Changes since v2:
- Adjust according to Jean's comments, rename to gpio-i2cmux
Changes since v1:
- Use i2c-mux infrastructure
- Move to drivers/i2c/muxes
Documentation/i2c/muxes/gpio-i2cmux | 65 +++++++++++++ MAINTAINERS | 8 ++ drivers/i2c/muxes/Kconfig | 12 +++ drivers/i2c/muxes/Makefile | 1 + drivers/i2c/muxes/gpio-i2cmux.c | 178 +++++++++++++++++++++++++++++++++++ include/linux/gpio-i2cmux.h | 38 ++++++++ 6 files changed, 302 insertions(+), 0 deletions(-) create mode 100644 Documentation/i2c/muxes/gpio-i2cmux create mode 100644 drivers/i2c/muxes/gpio-i2cmux.c create mode 100644 include/linux/gpio-i2cmux.h
Documentation/i2c/muxes/gpio-i2cmux new file +65 -0
@@ -0,0 +1,65 @@ +Kernel driver gpio-i2cmux + +Author: Peter Korsgaard <peter.korsgaard-ob4gmnvZ1/[email protected]> + +Description +----------- + +gpio-i2cmux is an i2c mux driver providing access to I2C bus segments +from a master I2C bus and a hardware MUX controlled through GPIO pins. + +E.G.: + + ---------- ---------- Bus segment 1 - - - - - + | | SCL/SDA | |-------------- | | + | |------------| | + | | | | Bus segment 2 | | + | Linux | GPIO 1..N | MUX |--------------- Devices + | |------------| | | | + | | | | Bus segment M + | | | |---------------| | + ---------- ---------- - - - - - + +SCL/SDA of the master I2C bus is multiplexed to bus segment 1..M +according to the settings of the GPIO pins 1..N. + +Usage +----- + +gpio-i2cmux uses the platform bus, so you need to provide a struct +platform_device with the platform_data pointing to a struct +gpio_i2cmux_platform_data with the I2C adapter number of the master +bus, the number bus segments to create and the GPIO pins used +to control it. See include/linux/gpio-i2cmux.h for details. + +E.G. something like this for a MUX providing 4 bus segments +controlled through 3 GPIO pins: + +#include <linux/gpio-i2cmux.h> +#include <linux/platform_device.h> + +static const unsigned myboard_gpiomux_gpios[] = { + AT91_PIN_PC26, AT91_PIN_PC25, AT91_PIN_PC24 +}; + +static const unsigned myboard_gpiomux_values[] = { + 0, 1, 2, 3 +}; + +static struct gpio_i2cmux_platform_data myboard_i2cmux_data = { + .parent = 1, + .base_nr = 2, /* optional */ + .values = myboard_gpiomux_values, + .n_values = ARRAY_SIZE(myboard_gpiomux_values), + .gpios = myboard_gpiomux_gpios, + .n_gpios = ARRAY_SIZE(myboard_gpiomux_gpios), + .idle = 4, /* optional */ +}; + +static struct platform_device myboard_i2cmux = { + .name = "gpio-i2cmux", + .id = 0, + .dev = { + .platform_data = &myboard_i2cmux_data, + }, +};
MAINTAINERS +8 -0
@@ -2579,6 +2579,14 @@ 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/muxes/gpio-i2cmux.c +F: include/linux/gpio-i2cmux.h +F: Documentation/i2c/muxes/gpio-i2cmux + GENERIC HDLC (WAN) DRIVERS M: Krzysztof Halasa <khc-9GfyWEdoJtJmR6Xm/[email protected]> W: http://www.kernel.org/pub/linux/utils/net/hdlc/
drivers/i2c/muxes/Kconfig +12 -0
@@ -5,6 +5,18 @@ menu "Multiplexer I2C Chip support" depends on I2C_MUX +config I2C_MUX_GPIO + 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 access to + I2C busses connected through a MUX, which is controlled + through GPIO pins. + + This driver can also be built as a module. If so, the module + will be called gpio-i2cmux. + config I2C_MUX_PCA9541 tristate "NXP PCA9541 I2C Master Selector" depends on EXPERIMENTAL
drivers/i2c/muxes/Makefile +1 -0
@@ -1,6 +1,7 @@ # # Makefile for multiplexer I2C chip drivers. +obj-$(CONFIG_I2C_MUX_GPIO) += gpio-i2cmux.o obj-$(CONFIG_I2C_MUX_PCA9541) += pca9541.o obj-$(CONFIG_I2C_MUX_PCA954x) += pca954x.o
drivers/i2c/muxes/gpio-i2cmux.c new file +178 -0
178 lines not on this page · raw message
include/linux/gpio-i2cmux.h new file +38 -0
@@ -0,0 +1,38 @@ +/* +* gpio-i2cmux 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_GPIO_I2CMUX_H +#define _LINUX_GPIO_I2CMUX_H + +/* MUX has no specific idle mode */ +#define GPIO_I2CMUX_NO_IDLE ((unsigned)-1) + +/** +* struct gpio_i2cmux_platform_data - Platform-dependent data for gpio-i2cmux +* @parent: Parent I2C bus adapter number +* @base_nr: Base I2C bus number to number adapters from or zero for dynamic +* @values: Array of bitmasks of GPIO settings (low/high) for each +* position +* @n_values: Number of multiplexer positions (busses to instantiate) +* @gpios: Array of GPIO numbers used to control MUX +* @n_gpios: Number of GPIOs used to control MUX +* @idle: Bitmask to write to MUX when idle or GPIO_I2CMUX_NO_IDLE if not used +*/ +struct gpio_i2cmux_platform_data { +int parent; +int base_nr; +const unsigned *values; +int n_values; +const unsigned *gpios; +int n_gpios; +unsigned idle; +}; + +#endif /* _LINUX_GPIO_I2CMUX_H */
Signature · 1 line
1.7.1
A message that is not in the archive (sent off-list or not yet received): 1291125788-9433-1-git-send-email-peter.korsgaard-ob4gmnvZ1/[email protected]
Hi Peter,
Quoted 15 lines · Peter Korsgaard
Add an i2c mux driver providing access to i2c bus segments using a hardware MUX sitting on a master bus and controlled through gpio pins.
E.G. something like:
---------- ---------- Bus segment 1 - - - - -
| | SCL/SDA | |-------------- | | | |------------| | | | | | Bus segment 2 | | | Linux | GPIO 1..N | MUX |--------------- Devices | |------------| | | | | | | | Bus segment M | | | |---------------| | ---------- ---------- - - - - -
Quoted 2 lines
SCL/SDA of the master I2C bus is multiplexed to bus segment 1..M according to the settings of the GPIO pins 1..N.
I've fixed two remaining minor things myself, and your patch is ready to be applied now, except for one issue I just noticed. See below.
Documentation/i2c/muxes/gpio-i2cmux line 32
Quoted 11 lines
(..)
Documentation/i2c/muxes/gpio-i2cmux new file +7
-0+Usage +----- + +gpio-i2cmux uses the platform bus, so you need to provide a struct +platform_device with the platform_data pointing to a struct +gpio_i2cmux_platform_data with the I2C adapter number of the master +bus, the number bus segments to create and the GPIO pins used
"of" got lost in the battle, I've added it back.
drivers/i2c/muxes/gpio-i2cmux.c line 92
Quoted 11 lines
Documentation/i2c/muxes/gpio-i2cmux new file +1
-0+to control it. See include/linux/gpio-i2cmux.h for details.drivers/i2c/muxes/gpio-i2cmux.c new file +5
-0+for (i =0 ; i < pdata->n_gpios; i++) { + ret =gpio_request (pdata->gpios[i],"gpio-i2cmux" ); +if (ret) + goto err_request_gpio; +gpio_direction_output (pdata->gpios[i], pdata->idle & (1 << i));
This looks wrong if pdata->idle == GPIO_I2CMUX_NO_IDLE. I think we want something along the lines of:
unsigned initial_state; initial_state = pdata->idle == GPIO_I2CMUX_NO_IDLE ? pdata->values[0] : pdata->idle; (...) gpio_direction_output(pdata->gpios[i], initial_state & (1 << i));
What do you think? An alternative is to leave the direction uninitialized and hope it's already OK, but I'm not sure how realistic this is.
drivers/i2c/muxes/gpio-i2cmux.c line 37
Quoted 3 lines
drivers/i2c/muxes/gpio-i2cmux.c new file +3
-0+ } + +
Double blank line, I've removed one.
Quoted 2 lines
drivers/i2c/muxes/gpio-i2cmux.c new file +2
-0+if (pdata->idle !=GPIO_I2CMUX_NO_IDLE ) + deselect = gpiomux_deselect;
Signature · 1 line
Jean Delvare
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Jean" == Jean Delvare <[email protected]> writes:
Hi,
Jean> I've fixed two remaining minor things myself, and your patch is Jean> ready to be applied now, except for one issue I just noticed. See Jean> below.
Great.
Quoted 4 lines
Documentation/i2c/muxes/gpio-i2cmux line 32
Quoted 4 lines
Documentation/i2c/muxes/gpio-i2cmux new file +4
-0+gpio-i2cmux uses the platform bus, so you need to provide a struct +platform_device with the platform_data pointing to a struct +gpio_i2cmux_platform_data with the I2C adapter number of the master +bus, the number bus segments to create and the GPIO pins used
Jean> "of" got lost in the battle, I've added it back.
Ahh yes, thanks.
Quoted 5 lines
drivers/i2c/muxes/gpio-i2cmux.c line 92
Quoted 5 lines
drivers/i2c/muxes/gpio-i2cmux.c new file +5
-0+for (i =0 ; i < pdata->n_gpios; i++) { + ret =gpio_request (pdata->gpios[i],"gpio-i2cmux" ); +if (ret) + goto err_request_gpio; +gpio_direction_output (pdata->gpios[i], pdata->idle & (1 << i));
Jean> This looks wrong if pdata->idle == GPIO_I2CMUX_NO_IDLE. I think we want Jean> something along the lines of:
Jean> unsigned initial_state;
Jean> initial_state = pdata->idle == GPIO_I2CMUX_NO_IDLE ?
Jean> pdata->values[0] : pdata->idle;
Jean> (...)
Jean> gpio_direction_output(pdata->gpios[i], initial_state & (1 << i));
Jean> What do you think? An alternative is to leave the direction Jean> uninitialized and hope it's already OK, but I'm not sure how realistic Jean> this is.
Yeah, going for values[0] is probably the most sensible thing to do if we don't have an idle state. Relying on the firmware to setup the right direction in advance is imho not that nice.
Will you do the change yourself, or should I resend the patch?
Quoted 2 lines
drivers/i2c/muxes/gpio-i2cmux.c line 97
Quoted 2 lines
drivers/i2c/muxes/gpio-i2cmux.c new file +2
-0+if (pdata->idle !=GPIO_I2CMUX_NO_IDLE ) + deselect = gpiomux_deselect;
We could probably move this up and add the assignment of init_state to this contional, instead of testing against GPIO_I2CMUX_NO_IDLE twice.
Signature · 1 line
Bye, Peter Korsgaard
A message that is not in the archive (sent off-list or not yet received): 871v62y4ts.fsf-uXGAPMMVk8amE9MCos8gUmSdvHPH+/[email protected]
Hi Peter,
Quoted 22 lines · Peter Korsgaard
Quoted 6 lines
drivers/i2c/muxes/gpio-i2cmux.c line 92
Quoted 6 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Jean" == Jean Delvare <[email protected]> writes:
drivers/i2c/muxes/gpio-i2cmux.c new file +5
-0+for (i =0 ; i < pdata->n_gpios; i++) { + ret =gpio_request (pdata->gpios[i],"gpio-i2cmux" ); +if (ret) + goto err_request_gpio; +gpio_direction_output (pdata->gpios[i], pdata->idle & (1 << i));Jean> This looks wrong if
pdata->idle==GPIO_I2CMUX_NO_IDLE. I think we want Jean> something along the lines of:Jean> unsigned
initial_state;Jean>
initial_state=pdata->idle==GPIO_I2CMUX_NO_IDLE?
Jean> pdata->values[0] :pdata->idle;Jean> (...)
Jean>gpio_direction_output(pdata->gpios[i], initial_state & (1 << i));Jean> What do you think? An alternative is to leave the direction Jean> uninitialized and hope it's already OK, but I'm not sure how realistic Jean> this is.
Quoted 5 lines
Yeah, going for values[0] is probably the most sensible thing to do if we don't have an idle state. Relying on the firmware to setup the right direction in advance is imho not that nice.
Will you do the change yourself, or should I resend the patch?
I will do it, I was just waiting for you to confirm you agreed with my analysis and proposal.
Quoted 5 lines
Quoted 2 lines
drivers/i2c/muxes/gpio-i2cmux.c line 97
Quoted 2 lines
drivers/i2c/muxes/gpio-i2cmux.c new file +2
-0+if (pdata->idle !=GPIO_I2CMUX_NO_IDLE ) + deselect = gpiomux_deselect;We could probably move this up and add the assignment of
init_stateto this contional, instead of testing againstGPIO_I2CMUX_NO_IDLEtwice.
Yes of course. The above was kind of pseudo code to express my proposal, not the definitive code.
I've added your patch to my i2c tree, it's now scheduled for merge in
kernel 2.6.38-rc1:
ftp://ftp.kernel.org/pub/linux/kernel/people/jdelvare/linux-2.6/jdelvare-i2c/i2c-add-generic-i2c-multiplexer-using-gpio-api.patch
Thanks for your contribution!
Signature · 1 line
Jean Delvare