i2c: virtual i2c adapter support.
Subsystems (3)
PATCH 1/3 i2c: virtual i2c adapter support.
Simplifies access to complex multiplexed I2C bus topologies, by presenting each multiplexed bus segment as a virtual I2C adapter. In this manner I2C devices "after" the multiplexer can ba managed as usual.
--- drivers/i2c/Kconfig | 9 +++ drivers/i2c/Makefile | 1 + drivers/i2c/i2c-virt.c | 188 ++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/i2c-id.h | 3 + include/linux/i2c.h | 17 +++++ 5 files changed, 218 insertions(+), 0 deletions(-) create mode 100644 drivers/i2c/i2c-virt.c
drivers/i2c/Kconfig +9 -0
@@ -27,6 +27,15 @@ config I2C_BOARDINFO boolean default y +config I2C_VIRT + tristate "I2C virtual adapter support" + depends on I2C + help + Say Y here if you want the I2C core to support the ability to have + virtual adapters. Virtual adapters are useful to handle multiplexed + I2C bus topologies, by presenting each multiplexed segment as a + I2C adapter. + config I2C_CHARDEV tristate "I2C device interface" help
drivers/i2c/Makefile +1 -0
@@ -4,6 +4,7 @@ obj-$(CONFIG_I2C_BOARDINFO) += i2c-boardinfo.o obj-$(CONFIG_I2C) += i2c-core.o +obj-$(CONFIG_I2C_VIRT) += i2c-virt.o obj-$(CONFIG_I2C_CHARDEV) += i2c-dev.o obj-y += busses/ chips/ algos/
drivers/i2c/i2c-virt.c new file +188 -0
188 lines not on this page · raw message
include/linux/i2c-id.h +3 -0
@@ -176,4 +176,7 @@#define I2C_HW_SAA7146 0x060000/* SAA7146 video decoder bus */ #define I2C_HW_SAA7134 0x090000/* SAA7134 video decoder bus */ +/* --- Virtual adapter mark */ +#define I2C_HW_VIRT 0x80000000 +#endif /* LINUX_I2C_ID_H */
include/linux/i2c.h +17 -0
@@ -553,6 +553,23 @@ union i2c_smbus_data {#define I2C_SMBUS_BLOCK_PROC_CALL 7/* SMBus 2.0 */ #define I2C_SMBUS_I2C_BLOCK_DATA 8 +/* +* Called to create a 'virtual' i2c bus which represents a multiplexed bus +* segment. The client and mux_val are passed to the select and deselect +* callback functions to perform hardware-specific mux control. +* +* The caller is expected to have the core_lists lock +*/ +struct i2c_adapter *i2c_add_virt_adapter (struct i2c_adapter *parent, +struct i2c_client *client, +u32 force_nr,u32 mux_val, +int (*select_cb) (struct i2c_adapter *, +struct i2c_client *,u32 ), +int (*deselect_cb) (struct i2c_adapter *, +struct i2c_client *,u32 )); + +int i2c_del_virt_adapter (struct i2c_adapter *adap); +#ifdef __KERNEL__
Signature · 7 lines
1.5.4.3 _______________________________________________ i2c mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/i2c
A message that is not in the archive (sent off-list or not yet received): 1213794365-8089-1-git-send-email-giometti-k2GhghHVRtY@public.gmane.org
PATCH 2/3 i2c: multiplexer i2c devices.
Define a proper configuration structure and new directory to hold multiplexer i2c device drivers.
--- drivers/i2c/Kconfig | 2 ++ drivers/i2c/Makefile | 2 +- drivers/i2c/muxes/Kconfig | 8 ++++++++ 3 files changed, 11 insertions(+), 1 deletions(-) create mode 100644 drivers/i2c/muxes/Kconfig create mode 100644 drivers/i2c/muxes/Makefile
drivers/i2c/Kconfig +2 -0
@@ -36,6 +36,8 @@ config I2C_VIRT I2C bus topologies, by presenting each multiplexed segment as a I2C adapter. +source drivers/i2c/muxes/Kconfig + config I2C_CHARDEV tristate "I2C device interface" help
drivers/i2c/Makefile +1 -1
@@ -6,7 +6,7 @@ obj-$(CONFIG_I2C_BOARDINFO) += i2c-boardinfo.o obj-$(CONFIG_I2C) += i2c-core.o obj-$(CONFIG_I2C_VIRT) += i2c-virt.o obj-$(CONFIG_I2C_CHARDEV) += i2c-dev.o -obj-y += busses/ chips/ algos/ +obj-y += busses/ chips/ muxes/ algos/ ifeq ($(CONFIG_I2C_DEBUG_CORE),y) EXTRA_CFLAGS += -DDEBUG
drivers/i2c/muxes/Kconfig new file +8 -0
@@ -0,0 +1,8 @@ +# +# Multiplexer I2C chip drivers configuration +# + +menu "Multiplexer I2C Chip support" + depends on I2C && I2C_VIRT && EXPERIMENTAL + +endmenu
drivers/i2c/muxes/Makefile new file +0 -0
Signature · 7 lines
1.5.4.3 _______________________________________________ i2c mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/i2c
A message that is not in the archive (sent off-list or not yet received): 1213794365-8089-2-git-send-email-giometti-k2GhghHVRtY@public.gmane.org
PATCH 3/3 i2c: driver for PCA954x I2C multiplexer series.
--- drivers/i2c/muxes/Kconfig | 9 ++ drivers/i2c/muxes/Makefile | 8 + drivers/i2c/muxes/pca954x.c | 300 +++++++++++++++++++++++++++++++++++++++++++ include/linux/i2c/pca954x.h | 6 + 4 files changed, 323 insertions(+), 0 deletions(-) create mode 100644 drivers/i2c/muxes/pca954x.c create mode 100644 include/linux/i2c/pca954x.h
drivers/i2c/muxes/Kconfig +9 -0
@@ -5,4 +5,13 @@ menu "Multiplexer I2C Chip support" depends on I2C && I2C_VIRT && EXPERIMENTAL +config I2CVIRT_PCA954x + tristate "Philips PCA953x I2C Mux/switches" + help + If you say yes here you get support for the Philips PCA954x + I2C mux/switch devices. + + This driver can also be built as a module. If so, the module + will be called pca954x. + endmenu
drivers/i2c/muxes/Makefile +8 -0
@@ -0,0 +1,8 @@ +# +# Makefile for multiplexer I2C chip drivers. + +obj-$(CONFIG_I2CVIRT_PCA954x) += pca954x.o + +ifeq ($(CONFIG_I2C_DEBUG_CHIP),y) +EXTRA_CFLAGS += -DDEBUG +endif
drivers/i2c/muxes/pca954x.c new file +300 -0
300 lines not on this page · raw message
include/linux/i2c/pca954x.h new file +6 -0
@@ -0,0 +1,6 @@ +/* platform data for the PCA954x I2C multiplexers */ + +struct pca954x_platform_data { +int *adap_ids; +int len; +};
Signature · 7 lines
1.5.4.3 _______________________________________________ i2c mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/i2c
Quoted 109 lines · Rodolfo Giometti
Simplifies access to complex multiplexed I2C bus topologies, by presenting each multiplexed bus segment as a virtual I2C adapter. In this manner I2C devices "after" the multiplexer can ba managed as usual.
Signed-off-by Rodolfo Giometti <[email protected]>--- drivers/i2c/Kconfig | 9 +++ drivers/i2c/Makefile | 1 + drivers/i2c/i2c-virt.c | 188 ++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/i2c-id.h | 3 + include/linux/i2c.h | 17 +++++ 5 files changed, 218 insertions(+), 0 deletions(-) create mode 100644 drivers/i2c/i2c-virt.cdrivers/i2c/Kconfig +9
-0@@ -27,6 +27,15 @@ config I2C_BOARDINFO boolean default y +config I2C_VIRT + tristate "I2C virtual adapter support" + depends on I2C + help + Say Y here if you want the I2C core to support the ability to have + virtual adapters. Virtual adapters are useful to handle multiplexed + I2C bus topologies, by presenting each multiplexed segment as a + I2C adapter. + config I2C_CHARDEV tristate "I2C device interface" helpdrivers/i2c/Makefile +1
-0@@ -4,6 +4,7 @@ obj-$(CONFIG_I2C_BOARDINFO) += i2c-boardinfo.o obj-$(CONFIG_I2C) += i2c-core.o +obj-$(CONFIG_I2C_VIRT) += i2c-virt.o obj-$(CONFIG_I2C_CHARDEV) += i2c-dev.o obj-y += busses/ chips/ algos/drivers/i2c/i2c-virt.c new file +56
-0@@ -0,0 +1,188 @@ +/* +* i2c-virt.c - Virtual I2C bus driver. +* +* Copyright (c) 2008 Rodolfo Giometti <[email protected]> +* Copyright (c) 2008 Eurotech S.p.A. <[email protected]> +* +* Simplifies access to complex multiplexed I2C bus topologies, by presenting +* each multiplexed bus segment as a virtual I2C adapter. Supports multi-level +* mux'ing (mux behind a mux). +* +* Based on: +* i2c-virt.c from Kumar Gala <[email protected]> +* i2c-virtual.c from Copyright (c) 2004 Google, Inc. (Ken Harrenstien) +* i2c-virtual.c from Brian Kuschak <bkuschak-/[email protected]> +* which was: +* Adapted from i2c-adap-ibm_ocp.c +* Original file Copyright 2000-2002 MontaVista Software Inc. +* +* This file is licensed under the terms of the GNU General Public +* License version 2. This program is licensed "as is" without any +* warranty of any kind, whether express or implied. +*/ + +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/i2c.h> +#include <linux/i2c-id.h> + +struct i2c_virt_priv { +struct i2c_adapter *parent_adap; +struct i2c_client *client;/* The mux chip/device */ + +u32 id;/* the mux id */ + +int (*select)(struct i2c_adapter *,struct i2c_client *,u32 ); +/* Enable the mux */ +int (*deselect)(struct i2c_adapter *,struct i2c_client *,u32 ); +/* Disable the mux */ +}; + +#define VIRT_TIMEOUT (HZ/2) +#define VIRT_RETRIES 3 + +static int i2c_virt_master_xfer (struct i2c_adapter *adap, +struct i2c_msg msgs[],int num) +{ +struct i2c_virt_priv *priv = adap->algo_data; +struct i2c_adapter *parent = priv->parent_adap; +int ret; + +/* Grab the lock for the parent adapter. We already hold the lock for +* the virtual adapter. Then select the right mux port and perform +* the transfer. +*/ + +mutex_lock (&parent->bus_lock);
drivers/i2c/i2c-virt.c line 66
Quoted 10 lines
drivers/i2c/i2c-virt.c new file +10
-0+ + ret = priv->select (parent, priv->client, priv->id); +if (ret >=0 ) + ret = parent->algo->master_xfer (parent, msgs, num); + priv->deselect (parent, priv->client, priv->id); + +mutex_unlock (&parent->bus_lock); + +return ret; +}
Out of interest, is it going to be better to hide the parent away completely from the system? I suppose if clients have already bound to it then we're probably going to just have to live with it being available.
Quoted 47 lines
drivers/i2c/i2c-virt.c new file +47
-0+static int i2c_virt_smbus_xfer (struct i2c_adapter *adap, +u16 addr,unsigned short flags, +char read_write,u8 command, +int size,union i2c_smbus_data *data) +{ +struct i2c_virt_priv *priv = adap->algo_data; +struct i2c_adapter *parent = priv->parent_adap; +int ret; + +/* Grab the lock for the parent adapter. We already hold the lock for +* the virtual adapter. Then select the right mux port and perform +* the transfer. +*/ + +mutex_lock (&parent->bus_lock); + + ret = priv->select (parent, priv->client, priv->id); +if (ret ==0 ) + ret = parent->algo->smbus_xfer (parent, addr, flags, + read_write, command, size, data); + priv->deselect (parent, priv->client, priv->id); + +mutex_unlock (&parent->bus_lock); + +return ret; +} + +/* Return the parent's functionality for the virtual adapter */ +static u32 i2c_virt_functionality (struct i2c_adapter *adap) +{ +struct i2c_virt_priv *priv = adap->algo_data; +struct i2c_adapter *parent = priv->parent_adap; + +return parent->algo->functionality (parent); +} + +struct i2c_adapter *i2c_add_virt_adapter (struct i2c_adapter *parent, +struct i2c_client *client, +u32 force_nr,u32 mux_val, +int (*select_cb) (struct i2c_adapter *, +struct i2c_client *,u32 ), +int (*deselect_cb) (struct i2c_adapter *, +struct i2c_client *,u32 )) +{ +struct i2c_adapter *adap; +struct i2c_virt_priv *priv; +struct i2c_algorithm *algo;
drivers/i2c/i2c-virt.c line 124
Quoted 10 lines
drivers/i2c/i2c-virt.c new file +10
-0+int ret; + + adap =kzalloc (sizeof (struct i2c_adapter ) + +sizeof (struct i2c_virt_priv ) + +sizeof (struct i2c_algorithm ),GFP_KERNEL ); +if (!adap) +return NULL ; + + priv = (struct i2c_virt_priv *)(adap +1 ); + algo = (struct i2c_algorithm *)(priv +1 );
you shouldn't need force-typecast here.
you could always make your i2c_virt_priv data contain the i2c_adapter and i2c_algorithm structure so you can easily go between them.
Quoted 5 lines
drivers/i2c/i2c-virt.c new file +5
-0+/* Set up private adapter data */ + priv->parent_adap = parent; + priv->client = client; + priv->id = mux_val; + priv->select = select_cb;
drivers/i2c/i2c-virt.c line 140
Quoted 10 lines
drivers/i2c/i2c-virt.c new file +10
-0+ priv->deselect = deselect_cb; + +/* Need to do algo dynamically because we don't know ahead +* of time what sort of physical adapter we'll be dealing with. +*/ + algo->master_xfer = (parent->algo->master_xfer + ? i2c_virt_master_xfer :NULL ); + algo->smbus_xfer = (parent->algo->smbus_xfer + ? i2c_virt_smbus_xfer :NULL ); + algo->functionality = i2c_virt_functionality;
you're using kzalloc, so this boils down to the neater:
if (parent->algo->master_xfer) algo->master_xfer = i2c_virt_master_xfer;
etc.
drivers/i2c/i2c-virt.c line 151
Quoted 10 lines
drivers/i2c/i2c-virt.c new file +10
-0+/* Now fill out new adapter structure */ +snprintf (adap->name,sizeof (adap->name), +"i2c-%d-virt (mux %02x:%02x)" , +i2c_adapter_id (parent), client->addr, mux_val); + adap->id =I2C_HW_VIRT |i2c_adapter_id (parent); + adap->algo = algo; + adap->algo_data = priv; + adap->timeout =VIRT_TIMEOUT ; + adap->retries =VIRT_RETRIES ; + adap->dev.parent = &parent->dev;
How about creating a struct with the callbacks in, and the data for timeout, retries and any other data that would be needed?
Quoted 26 lines
drivers/i2c/i2c-virt.c new file +26
-0+if (force_nr) { + adap->nr = force_nr; + ret =i2c_add_numbered_adapter (adap); + }else + ret =i2c_add_adapter (adap); +if (ret <0 ) { +kfree (adap); +return NULL ; + } + +dev_info (&parent->dev,"i2c-virt-%d: Virtual I2C bus - " +"physical bus i2c-%d, multiplexer 0x%02x port %d\n" , +i2c_adapter_id (adap),i2c_adapter_id (parent), + client->addr, mux_val); + +return adap; +} +EXPORT_SYMBOL_GPL (i2c_add_virt_adapter); + +int i2c_del_virt_adapter (struct i2c_adapter *adap) +{ +int ret; + + ret =i2c_del_adapter (adap); +if (ret <0 ) +return ret;
drivers/i2c/i2c-virt.c line 188
Quoted 10 lines
drivers/i2c/i2c-virt.c new file +10
-0+kfree (adap); + +return 0 ; +} +EXPORT_SYMBOL_GPL (i2c_del_virt_adapter); + +MODULE_AUTHOR ("Rodolfo Giometti <[email protected], " \ +"Kumar Gala <[email protected]>" ); +MODULE_DESCRIPTION ("Virtual I2C driver for multiplexed I2C busses" ); +MODULE_LICENSE ("GPL" );
"GPL v2" is the definition you probably want here.
Signature · 10 lines
Ben Q: What's a light-year? A: One-third less calories than a regular year. _______________________________________________ i2c mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/i2c
A message that is not in the archive (sent off-list or not yet received): 20080618190008.GK10351-SMNkleLxa3Z6Wcw2j4pizdi2O/[email protected]
Quoted 20 lines · Ben Dooks
Quoted 20 lines
drivers/i2c/i2c-virt.c new file +20
-0+static int i2c_virt_master_xfer (struct i2c_adapter *adap, +struct i2c_msg msgs[],int num) +{ +struct i2c_virt_priv *priv = adap->algo_data; +struct i2c_adapter *parent = priv->parent_adap; +int ret; + +/* Grab the lock for the parent adapter. We already hold the lock for +* the virtual adapter. Then select the right mux port and perform +* the transfer. +*/ + +mutex_lock (&parent->bus_lock); + + ret = priv->select (parent, priv->client, priv->id); +if (ret >=0 ) + ret = parent->algo->master_xfer (parent, msgs, num); + priv->deselect (parent, priv->client, priv->id); + +mutex_unlock (&parent->bus_lock);
Quoted 8 lines
drivers/i2c/i2c-virt.c line 66
Quoted 3 lines
drivers/i2c/i2c-virt.c new file +3
-0+ +return ret; +}Out of interest, is it going to be better to hide the parent away completely from the system? I suppose if clients have already bound to it then we're probably going to just have to live with it being available.
I'm sorry but I don't understand what you mean. =:-o
The parent is strictly connected with the children virtual adapter, how can I hide it?
Quoted 24 lines
Quoted 11 lines
drivers/i2c/i2c-virt.c new file +11
-0+struct i2c_adapter *i2c_add_virt_adapter (struct i2c_adapter *parent, +struct i2c_client *client, +u32 force_nr,u32 mux_val, +int (*select_cb) (struct i2c_adapter *, +struct i2c_client *,u32 ), +int (*deselect_cb) (struct i2c_adapter *, +struct i2c_client *,u32 )) +{ +struct i2c_adapter *adap; +struct i2c_virt_priv *priv; +struct i2c_algorithm *algo;drivers/i2c/i2c-virt.c line 124
Quoted 10 lines
drivers/i2c/i2c-virt.c new file +10
-0+int ret; + + adap =kzalloc (sizeof (struct i2c_adapter ) + +sizeof (struct i2c_virt_priv ) + +sizeof (struct i2c_algorithm ),GFP_KERNEL ); +if (!adap) +return NULL ; + + priv = (struct i2c_virt_priv *)(adap +1 ); + algo = (struct i2c_algorithm *)(priv +1 );you shouldn't need force-typecast here.
Quoted 2 lines
you could always make your
i2c_virt_privdata contain thei2c_adapterandi2c_algorithmstructure so you can easily go between them.
Ok.
Quoted 18 lines
Quoted 5 lines
drivers/i2c/i2c-virt.c new file +5
-0+/* Set up private adapter data */ + priv->parent_adap = parent; + priv->client = client; + priv->id = mux_val; + priv->select = select_cb;drivers/i2c/i2c-virt.c line 140
Quoted 10 lines
drivers/i2c/i2c-virt.c new file +10
-0+ priv->deselect = deselect_cb; + +/* Need to do algo dynamically because we don't know ahead +* of time what sort of physical adapter we'll be dealing with. +*/ + algo->master_xfer = (parent->algo->master_xfer + ? i2c_virt_master_xfer :NULL ); + algo->smbus_xfer = (parent->algo->smbus_xfer + ? i2c_virt_smbus_xfer :NULL ); + algo->functionality = i2c_virt_functionality;you're using kzalloc, so this boils down to the neater:
Quoted 3 lines
if (parent->algo->master_xfer) algo->master_xfer = i2c_virt_master_xfer;etc.
Ok.
Quoted 5 lines
Quoted 5 lines
drivers/i2c/i2c-virt.c new file +5
-0+/* Now fill out new adapter structure */ +snprintf (adap->name,sizeof (adap->name), +"i2c-%d-virt (mux %02x:%02x)" , +i2c_adapter_id (parent), client->addr, mux_val); + adap->id =I2C_HW_VIRT |i2c_adapter_id (parent);
Quoted 8 lines
drivers/i2c/i2c-virt.c line 151
Quoted 5 lines
drivers/i2c/i2c-virt.c new file +5
-0+ adap->algo = algo; + adap->algo_data = priv; + adap->timeout =VIRT_TIMEOUT ; + adap->retries =VIRT_RETRIES ; + adap->dev.parent = &parent->dev;How about creating a struct with the callbacks in, and the data for timeout, retries and any other data that would be needed?
There are few assignments... :)
Quoted 30 lines
Quoted 30 lines
drivers/i2c/i2c-virt.c new file +30
-0+if (force_nr) { + adap->nr = force_nr; + ret =i2c_add_numbered_adapter (adap); + }else + ret =i2c_add_adapter (adap); +if (ret <0 ) { +kfree (adap); +return NULL ; + } + +dev_info (&parent->dev,"i2c-virt-%d: Virtual I2C bus - " +"physical bus i2c-%d, multiplexer 0x%02x port %d\n" , +i2c_adapter_id (adap),i2c_adapter_id (parent), + client->addr, mux_val); + +return adap; +} +EXPORT_SYMBOL_GPL (i2c_add_virt_adapter); + +int i2c_del_virt_adapter (struct i2c_adapter *adap) +{ +int ret; + + ret =i2c_del_adapter (adap); +if (ret <0 ) +return ret; +kfree (adap); + +return 0 ; +}
Quoted 8 lines
drivers/i2c/i2c-virt.c line 188
Quoted 6 lines
drivers/i2c/i2c-virt.c new file +6
-0+EXPORT_SYMBOL_GPL (i2c_del_virt_adapter); + +MODULE_AUTHOR ("Rodolfo Giometti <[email protected], " \ +"Kumar Gala <[email protected]>" ); +MODULE_DESCRIPTION ("Virtual I2C driver for multiplexed I2C busses" ); +MODULE_LICENSE ("GPL" );"GPL v2" is the definition you probably want here.
Ok.
Ciao,
Rodolfo
Signature · 4 lines
GNU/Linux Solutions e-mail: giometti-AVVDYK/kqiJWk0Htik3J/[email protected] Linux Device Driver [email protected] Embedded Systems phone: +39 349 2432127 UNIX programming skype: rodolfo.giometti
_______________________________________________
i2c mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/i2c