i2c: virtual i2c adapter support.
Subsystems (3)
PATCH v2 1/1 i2c: virtual i2c adapter support.
Changelog v2:
* i2c-virt: license issue fix up.
* i2c: fix up race condition during virtual adapters removal.
* i2c: better code for virtual adapters definition.
Rodolfo
_______________________________________________
i2c mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/i2c
PATCH 1/1 i2c: virtual i2c adapter support.
In reply to a message not in the archiveSimplifies 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-core.c | 37 +++++++--- drivers/i2c/i2c-virt.c | 189 ++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/i2c-id.h | 3 + include/linux/i2c.h | 18 +++++ 6 files changed, 246 insertions(+), 11 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-core.c +26 -11
@@ -573,34 +573,32 @@ static int i2c_do_del_adapter(struct device_driver *d, void *data) }/** -* i2c_del_adapter - unregister I2C adapter +* i2c_del_adapter_nolock - unregister I2C adapter * @adap: the adapter being unregistered * Context: can sleep * * This unregisters an I2C adapter which was previously registered -* by @i2c_add_adapter or @i2c_add_numbered_adapter. +* by @i2c_add_adapter or @i2c_add_numbered_adapter but without lock +* protection! For normal usage you may wish using @i2c_del_adapter. */ -int i2c_del_adapter (struct i2c_adapter *adap) +int i2c_del_adapter_nolock (struct i2c_adapter *adap) {struct list_head *item, *_n;struct i2c_client *client; -int res =0 ; - -mutex_lock (&core_lock); +int res;/* First make sure that this adapter was ever added */ if (idr_find (&i2c_adapter_idr, adap->nr) != adap) {pr_debug ("i2c-core: attempting to delete unregistered " "adapter [%s]\n" , adap->name); - res = -EINVAL ; - goto out_unlock; +return -EINVAL ; }/* Tell drivers about this removal */ res =bus_for_each_drv (&i2c_bus_type,NULL , adap, i2c_do_del_adapter);if (res) - goto out_unlock; +return res;/* detach any active clients. This must be done first, because * it can fail; in which case we give up. */
@@ -621,7 +619,7 @@ int i2c_del_adapter(struct i2c_adapter *adap)dev_err (&adap->dev,"detach_client failed for client " "[%s] at address 0x%02x\n" , client->name, client->addr); - goto out_unlock; +return res; } }
@@ -637,8 +635,25 @@ int i2c_del_adapter(struct i2c_adapter *adap)dev_dbg (&adap->dev,"adapter [%s] unregistered\n" , adap->name); - out_unlock: +return 0 ; +} +EXPORT_SYMBOL (i2c_del_adapter_nolock); + +/** +* i2c_del_adapter - unregister I2C adapter +* @adap: the adapter being unregistered +* Context: can sleep +* +* The same as @i2c_del_adapter_nolock but with lock protection. +*/ +int i2c_del_adapter (struct i2c_adapter *adap) +{ +int res; + +mutex_lock (&core_lock); + res =i2c_del_adapter_nolock (adap);mutex_unlock (&core_lock); +return res; }EXPORT_SYMBOL (i2c_del_adapter);
drivers/i2c/i2c-virt.c new file +189 -0
189 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 +18 -0
@@ -381,6 +381,7 @@ struct i2c_client_address_data {extern int i2c_add_adapter (struct i2c_adapter *);extern int i2c_del_adapter (struct i2c_adapter *);extern int i2c_add_numbered_adapter (struct i2c_adapter *); +extern int i2c_del_adapter_nolock (struct i2c_adapter *);extern int i2c_register_driver (struct module *,struct i2c_driver *);extern void i2c_del_driver (struct i2c_driver *);
@@ -553,6 +554,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
PATCH 1/1 i2c: multiplexer i2c devices.
In reply to a message not in the archiveDefine 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
PATCH 1/1 i2c: driver for PCA954x I2C multiplexer series.
In reply to a message not in the archive--- 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 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Rodolfo" == Rodolfo Giometti <[email protected]> writes:
Hi,
Rodolfo> +
Rodolfo> +struct i2c_adapter *i2c_add_virt_adapter(struct i2c_adapter *parent,
Rodolfo> + struct i2c_client *client,
Rodolfo> + u32 force_nr, u32 mux_val,
Rodolfo> + int (*select_cb) (struct i2c_adapter *,
Rodolfo> + struct i2c_client *, u32),
Rodolfo> + int (*deselect_cb) (struct i2c_adapter *,
Rodolfo> + struct i2c_client *, u32))
Rodolfo> +{
How about changing the struct i2c_client to an anonymous void *data instead so it can be used for systems where the multiplexing hardware isn't a i2c device? E.G. I have a driver (currently not in mainline) for a I2C multiplexer implemented in a FPGA together with the opencores I2C controller:
/*
* i2c-thinlitemux.c: I2C multiplexer on Barco Thinlite board.
*
* Peter Korsgaard <peter.korsgaard-ob4gmnvZ1/[email protected]> *
* 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/errno.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/i2c.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#define THINLITEMUX_BUSSES 6
#define THINLITEMUX_IDLE 7
struct thinlitemux_i2c {
u16 __iomem *base;
struct i2c_adapter *parent;
struct i2c_adapter adap;
int pos;
};static int thinlitemux_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
int num)
{
struct thinlitemux_i2c *i2c = i2c_get_adapdata(adap);
int ret;
mutex_lock(&i2c->parent->bus_lock);
out_be16(i2c->base, i2c->pos);
ret = i2c->parent->algo->master_xfer(i2c->parent, msgs, num);
out_be16(i2c->base, THINLITEMUX_IDLE);
mutex_unlock(&i2c->parent->bus_lock);
return ret;
}static u32 thinlitemux_func(struct i2c_adapter *adap)
{
struct thinlitemux_i2c *i2c = i2c_get_adapdata(adap);
return i2c->parent->algo->functionality(i2c->parent);
}static struct i2c_algorithm thinlitemux_algorithm = {
.master_xfer = thinlitemux_xfer,
.functionality = thinlitemux_func,
};
static struct i2c_adapter thinlitemux_adapter = {
.owner = THIS_MODULE,
.name = "thinlitemux",
.class = I2C_CLASS_HWMON,
.algo = &thinlitemux_algorithm,
.timeout = 2,
.retries = 1
};static int __devinit thinlitemux_probe(struct platform_device *pdev)
{
struct thinlitemux_i2c *i2c;
struct i2c_adapter *adap;
struct resource *res;
u16 __iomem *base;
int i, j, ret;
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!res)
return -ENODEV;
adap = i2c_get_adapter((int)pdev->dev.platform_data);
if (!adap) {
dev_err(&pdev->dev, "Parent adapter (%d) not found\n",
(int)pdev->dev.platform_data);
return -ENODEV;
}
base = ioremap(res->start, res->end - res->start + 1);
if (!base) {
dev_err(&pdev->dev, "Unable to map registers\n");
ret = -EIO;
goto map_failed;
}
i2c = kzalloc(sizeof(*i2c)*THINLITEMUX_BUSSES, GFP_KERNEL);
if (!i2c) {
ret = -ENOMEM;
goto alloc_failed;
}
for (i=0; i<THINLITEMUX_BUSSES; i++) {
i2c[i].base = base;
i2c[i].pos = i;
i2c[i].parent = adap;
i2c[i].adap = thinlitemux_adapter;
i2c[i].adap.dev.parent = &adap->dev;
i2c[i].adap.nr = i+1;
snprintf(i2c[i].adap.name, I2C_NAME_SIZE, "%s.%d",
thinlitemux_adapter.name, i);
i2c_set_adapdata(&i2c[i].adap, &i2c[i]);
ret = i2c_add_numbered_adapter(&i2c[i].adap);
if (ret) {
dev_err(&pdev->dev, "Failed to add adapter %d\n", i);
goto add_adapter_failed;
}
}
/* disable parent bus so probes won't find devices on it */
out_be16(base, THINLITEMUX_IDLE);
dev_info(&pdev->dev, "%d port mux at 0x%lx on %s adapter\n",
THINLITEMUX_BUSSES, (unsigned long)res->start, adap->name);
platform_set_drvdata(pdev, i2c);
return 0; add_adapter_failed:
for (j=0; j<i; j++) i2c_del_adapter(&i2c[j].adap); alloc_failed: iounmap(base); map_failed: i2c_put_adapter(adap); return ret; }
static int __devexit thinlitemux_remove(struct platform_device* pdev)
{
struct thinlitemux_i2c *i2c = platform_get_drvdata(pdev);
int i;
for (i=0; i<THINLITEMUX_BUSSES; i++)
i2c_del_adapter(&i2c[i].adap);
iounmap(i2c->base);
platform_set_drvdata(pdev, NULL);
i2c_put_adapter(i2c->parent);
kfree(i2c);
return 0;
}static struct platform_driver thinlitemux_driver = {
.probe = thinlitemux_probe,
.remove = __devexit_p(thinlitemux_remove), .driver = {
.owner = THIS_MODULE,
.name = "thinlitei2cmux",
},
};
static int __init thinlitemux_init(void)
{
return platform_driver_register(&thinlitemux_driver);
}static void __exit thinlitemux_exit(void)
{
platform_driver_unregister(&thinlitemux_driver);
}module_init(thinlitemux_init);module_exit(thinlitemux_exit);
MODULE_DESCRIPTION("Barco ThinLite I2C multiplexer driver"); MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard-ob4gmnvZ1/[email protected]>"); MODULE_LICENSE("GPL");
It would be nice to be able to use the i2c-virtual stuff for it.
Signature · 6 lines
Bye, Peter Korsgaard _______________________________________________ i2c mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/i2c
Quoted 14 lines · Peter Korsgaard
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Rodolfo" == Rodolfo Giometti <[email protected]> writes:
Hi,
Rodolfo> +
Rodolfo> +structi2c_adapter*i2c_add_virt_adapter(structi2c_adapter*parent,
Rodolfo> + structi2c_client*client,
Rodolfo> + u32force_nr, u32mux_val,
Rodolfo> + int (*select_cb) (structi2c_adapter*,
Rodolfo> + structi2c_client*, u32),
Rodolfo> + int (*deselect_cb) (structi2c_adapter*,
Rodolfo> + structi2c_client*, u32))
Rodolfo> +{
Quoted 7 lines
How about changing the struct
i2c_clientto an anonymous void *data instead so it can be used for systems where the multiplexing hardware isn't a i2c device? E.G. I have a driver (currently not in mainline) for a I2C multiplexer implemented in a FPGA together with the opencores I2C controller:
[snip]
It would be nice to be able to use the i2c-virtual stuff for it.
Mmm... I suppose is not so easy as we wish since there are several references to the i2c parent struct into the code.
I suppose you can get better result if you define your i2c multiplexer as a dummy i2c device. The you can force it to be connected with a real i2c parent by using the i2c_register_board_info() function.
What do you think about this solution? :)
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
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Rodolfo" == Rodolfo Giometti <giometti-AVVDYK/kqiJWk0Htik3J/[email protected]> writes:
Hi,
Quoted 1 line
Quoted 1 line
It would be nice to be able to use the i2c-virtual stuff for it.
Rodolfo> Mmm... I suppose is not so easy as we wish since there are several Rodolfo> references to the i2c parent struct into the code.
That's not the problem, the struct i2c_client is. The i2c-virtual stuff doesn't do anything with it, it simply forwards it to the select/deselect callbacks - So changing it to a void *data shouldn't really be a problem.
You don't derive the parent i2c bus from the i2c_client.
Rodolfo> I suppose you can get better result if you define your i2c Rodolfo> multiplexer as a dummy i2c device. The you can force it to Rodolfo> be connected with a real i2c parent by using the Rodolfo> i2c_register_board_info() function.
Rodolfo> What do you think about this solution? :)
Sorry, I don't see what the problem is?
Signature · 6 lines
Bye, Peter Korsgaard _______________________________________________ i2c mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/i2c
Quoted 7 lines · Peter Korsgaard
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Rodolfo" == Rodolfo Giometti <giometti-AVVDYK/kqiJWk0Htik3J/[email protected]> writes:
It would be nice to be able to use the i2c-virtual stuff for it.
Rodolfo> Mmm... I suppose is not so easy as we wish since there are several Rodolfo> references to the i2c parent struct into the code.
Quoted 6 lines
That's not the problem, the struct
i2c_clientis. The i2c-virtual stuff doesn't do anything with it, it simply forwards it to the select/deselect callbacks - So changing it to a void *data shouldn't really be a problem.You don't derive the parent i2c bus from the
i2c_client.
Ok, but what do you suggest to put in "struct i2c_adapter *parent"?
Quoted 8 lines
Rodolfo> I suppose you can get better result if you define your i2c Rodolfo> multiplexer as a dummy i2c device. The you can force it to Rodolfo> be connected with a real i2c parent by using the Rodolfo>
i2c_register_board_info()function.Rodolfo> What do you think about this solution? :)
Sorry, I don't see what the problem is?
Maybe if you provide a littke code I may understand exactly what you mean. ;)
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
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Rodolfo" == Rodolfo Giometti <giometti-AVVDYK/kqiJWk0Htik3J/[email protected]> writes:
Hi,
Quoted 6 lines · Peter Korsgaard
Quoted 6 lines
That's not the problem, the struct
i2c_clientis. The i2c-virtual stuff doesn't do anything with it, it simply forwards it to the select/deselect callbacks - So changing it to a void *data shouldn't really be a problem.You don't derive the parent i2c bus from the
i2c_client.
Rodolfo> Ok, but what do you suggest to put in "struct i2c_adapter *parent"?
The real parent bus. The hardware works pretty much like the pcfs, except it has a memory mapped register interface instead of i2c for controlling the mux. See the driver I sent for details.
Rodolfo> What do you think about this solution? :)
Quoted 2 lines
Quoted 2 lines
Sorry, I don't see what the problem is?
Rodolfo> Maybe if you provide a littke code I may understand exactly Rodolfo> what you mean. ;)
But I did?!
Signature · 6 lines
Bye, Peter Korsgaard _______________________________________________ i2c mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/i2c
Quoted 6 lines · Peter Korsgaard
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Rodolfo" == Rodolfo Giometti <giometti-AVVDYK/kqiJWk0Htik3J/[email protected]> writes:
Rodolfo> Maybe if you provide a littke code I may understand exactly Rodolfo> what you mean. ;)
But I did?!
I mean the code that shows how you wish adding your i2c mux by using i2c_add_virt_adapter(). :)
However, we also should make attention at i2c_del_virt_adapter() which must call (new) i2c_del_adapter_nolock() since the core_lock is hold by i2c_del_driver() when the mux is an i2c device.
If you define a virtual adapter as a non-i2c device I suppose we should use the lock...
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
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Rodolfo" == Rodolfo Giometti <giometti-AVVDYK/kqiJWk0Htik3J/[email protected]> writes:
Quoted 2 lines · Peter Korsgaard
Quoted 2 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Rodolfo" == Rodolfo Giometti <giometti-AVVDYK/kqiJWk0Htik3J/[email protected]> writes:
Rodolfo> Maybe if you provide a littke code I may understand exactly Rodolfo> what you mean. ;)
Quoted 2 lines
Quoted 2 lines
But I did?!
Rodolfo> I mean the code that shows how you wish adding your i2c mux Rodolfo> by using i2c_add_virt_adapter(). :)
Pretty much like the pcf driver, E.G. something like (not compile tested):
#define THINLITEMUX_BUSSES 6
#define THINLITEMUX_IDLE 7
struct thinlitemux_i2c {
u16 __iomem *base;
struct i2c_adapter *parent;
struct i2c_adapter *adap{TINLITEMUX_BUSSES];
};static int thinlitemux_select(struct i2c_adapter *adap,
struct i2c_client *client, u32 chan)
{
struct thinlitemux_i2c *i2c = (struct thinlitemux_i2c*)client;
out_be16(i2c->base, chan);
}static int thinlitemux_deselect(struct i2c_adapter *adap,
struct i2c_client *client, u32 value)
{
struct thinlitemux_i2c *i2c = (struct thinlitemux_i2c*)client;
out_be16(i2c->base, THINLITEMUX_IDLE);
}static int __devinit thinlitemux_probe(struct platform_device *pdev)
{
struct thinlitemux_i2c *i2c;
struct i2c_adapter *adap;
struct resource *res;
u16 __iomem *base;
int i, j, ret;
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!res)
return -ENODEV;
i2c = kzalloc(sizeof(*i2c), GFP_KERNEL);
if (!i2c) {
ret = -ENOMEM;
goto alloc_failed;
}
i2c->parent = i2c_get_adapter((int)pdev->dev.platform_data);
if (!i2c->parent) {
dev_err(&pdev->dev, "Parent adapter (%d) not found\n",
(int)pdev->dev.platform_data);
ret = -ENODEV;
goto no_parent;
}
i2c->base = ioremap(res->start, res->end - res->start + 1);
if (!i2c->base) {
dev_err(&pdev->dev, "Unable to map registers\n");
ret = -EIO;
goto map_failed;
}
/* disable parent bus so probes won't find devices on it */
thinlitemux_deselect(i2c->parent, NULL, 0);
for (i=0; i<THINLITEMUX_BUSSES; i++) {
i2c->adap[i] = i2c_add_virt_adapter(i2c->parent, i2c, 0, i,
&thinlitemux_select, &thinlitemux_deselect);
if (!i2c->adap[i]) {
dev_err(&pdev->dev, "Failed to add adapter %d\n", i);
goto add_adapter_failed;
}
}
dev_info(&pdev->dev, "%d port mux at 0x%lx on %s adapter\n",
THINLITEMUX_BUSSES, (unsigned long)res->start, parent->name);
platform_set_drvdata(pdev, i2c);
return 0; add_adapter_failed:
for (j=0; j<i; j++) i2c_del_virt_adapter(&i2c->adap[j]); alloc_failed: iounmap(i2c->base); map_failed: i2c_put_adapter(i2c->parent); no_parent kfree(i2c); return ret; }
static int __devexit thinlitemux_remove(struct platform_device* pdev)
{
struct thinlitemux_i2c *i2c = platform_get_drvdata(pdev);
int i;
for (i=0; i<THINLITEMUX_BUSSES; i++)
i2c_del_virt_adapter(&i2c->adap[i]);
iounmap(i2c->base);
platform_set_drvdata(pdev, NULL);
i2c_put_adapter(i2c->parent);
kfree(i2c);
return 0;
}static struct platform_driver thinlitemux_driver = {
.probe = thinlitemux_probe,
.remove = __devexit_p(thinlitemux_remove), .driver = {
.owner = THIS_MODULE,
.name = "thinlitei2cmux",
},
};
static int __init thinlitemux_init(void)
{
return platform_driver_register(&thinlitemux_driver);
}static void __exit thinlitemux_exit(void)
{
platform_driver_unregister(&thinlitemux_driver);
}module_init(thinlitemux_init);module_exit(thinlitemux_exit);
MODULE_DESCRIPTION("Barco ThinLite I2C multiplexer driver"); MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard-ob4gmnvZ1/[email protected]>"); MODULE_LICENSE("GPL");
Rodolfo> However, we also should make attention at Rodolfo> i2c_del_virt_adapter() which must call (new) Rodolfo> i2c_del_adapter_nolock() since the core_lock is hold by Rodolfo> i2c_del_driver() when the mux is an i2c device.
Rodolfo> If you define a virtual adapter as a non-i2c device I Rodolfo> suppose we should use the lock...
I guess we could do:
if (mutex_is_locked(&core_lock)) ret = i2c_del_adapter_nolock(adap); else ret = i2c_del_adapter(adap);
Except that core_lock is static in i2c-core.c.
Signature · 6 lines
Bye, Peter Korsgaard _______________________________________________ i2c mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/i2c
Hi Rodolfo,
Quoted 23 lines · Rodolfo Giometti
Quoted 14 lines · Peter Korsgaard
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Rodolfo" == Rodolfo Giometti <[email protected]> writes:
Hi,
Rodolfo> +
Rodolfo> +structi2c_adapter*i2c_add_virt_adapter(structi2c_adapter*parent,
Rodolfo> + structi2c_client*client,
Rodolfo> + u32force_nr, u32mux_val,
Rodolfo> + int (*select_cb) (structi2c_adapter*,
Rodolfo> + structi2c_client*, u32),
Rodolfo> + int (*deselect_cb) (structi2c_adapter*,
Rodolfo> + structi2c_client*, u32))
Rodolfo> +{Quoted 7 lines
How about changing the struct
i2c_clientto an anonymous void *data instead so it can be used for systems where the multiplexing hardware isn't a i2c device? E.G. I have a driver (currently not in mainline) for a I2C multiplexer implemented in a FPGA together with the opencores I2C controller:
[snip]
It would be nice to be able to use the i2c-virtual stuff for it.
Quoted 6 lines
Mmm... I suppose is not so easy as we wish since there are several references to the i2c parent struct into the code.
I suppose you can get better result if you define your i2c multiplexer as a dummy i2c device. The you can force it to be connected with a real i2c parent by using the
i2c_register_board_info()function.
Don't even think about it. We've done dummy i2c devices before and it took me 3 years to clean up the mess. Not again, please.
Peter is totally right, there's no reason why I2C multiplexers would always be I2C devices themselves (and when they are, they do not have to live on the bus they are multiplexing, even though this is the most frequent case). That's only one of the possible cases. The core i2c multiplexing code shouldn't assume anything about the nature of the multiplexer, that's something for the actual implementation of specific multiplexer drivers to deal with.
Thanks,
Signature · 6 lines
Jean Delvare _______________________________________________ i2c mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/i2c
Quoted 11 lines · Rodolfo Giometti
Quoted 6 lines · Peter Korsgaard
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Rodolfo" == Rodolfo Giometti <giometti-AVVDYK/kqiJWk0Htik3J/[email protected]> writes:
Rodolfo> Maybe if you provide a littke code I may understand exactly Rodolfo> what you mean. ;)
But I did?!
I mean the code that shows how you wish adding your i2c mux by using
i2c_add_virt_adapter(). :)
Quoted 6 lines
However, we also should make attention at
i2c_del_virt_adapter()which must call (new)i2c_del_adapter_nolock()since thecore_lockis hold byi2c_del_driver()when the mux is an i2c device.If you define a virtual adapter as a non-i2c device I suppose we should use the lock...
My feeling is that we'd be better off killing the legacy i2c binding model before we try to add support for i2c bus multiplexers. This will clean up the i2c-core locking model significantly and limit the number of cases we have to think about for everything we implement.
This isn't so far away, I'm working on it these days and it isn't impossible that kernel 2.6.28 will be completely i2c-legacy-model-free.
Thanks,
Signature · 6 lines
Jean Delvare _______________________________________________ i2c mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/i2c
Hi Peter,
Quoted 9 lines · Peter Korsgaard
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Rodolfo" == Rodolfo Giometti <giometti-AVVDYK/kqiJWk0Htik3J/[email protected]> writes:
Rodolfo> However, we also should make attention at Rodolfo>
i2c_del_virt_adapter()which must call (new) Rodolfo>i2c_del_adapter_nolock()since thecore_lockis hold by Rodolfo>i2c_del_driver()when the mux is an i2c device.Rodolfo> If you define a virtual adapter as a non-i2c device I Rodolfo> suppose we should use the lock...
Quoted 8 lines
I guess we could do:
if (mutex_is_locked(&core_lock)) ret = i2c_del_adapter_nolock(adap); else ret = i2c_del_adapter(adap);Except that
core_lockis static ini2c-core.c.
This construct is broken by design. If you don't know if the mutex is locked, it means that you're not the one holding it. If you're not the one holding it, you don't know when it will be released. So it could be released in the middle of whatever you are doing which requires the lock to be held, and that's bad.
Signature · 6 lines
Jean Delvare _______________________________________________ i2c mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/i2c
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Jean" == Jean Delvare <[email protected]> writes:
Hi,
Quoted 1 line
Quoted 1 line
Except that
core_lockis static ini2c-core.c.
Jean> This construct is broken by design. If you don't know if the Jean> mutex is locked, it means that you're not the one holding Jean> it. If you're not the one holding it, you don't know when it Jean> will be released. So it could be released in the middle of Jean> whatever you are doing which requires the lock to be held, and Jean> that's bad.
Exactly. Either we put it on hold for the core rework or we add a i2c_del_virt_adapter_nolock() variant for the mux-on-i2c case.
Signature · 6 lines
Bye, Peter Korsgaard _______________________________________________ i2c mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/i2c
Quoted 13 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,
Quoted 1 line
Quoted 1 line
Except that
core_lockis static ini2c-core.c.Jean> This construct is broken by design. If you don't know if the Jean> mutex is locked, it means that you're not the one holding Jean> it. If you're not the one holding it, you don't know when it Jean> will be released. So it could be released in the middle of Jean> whatever you are doing which requires the lock to be held, and Jean> that's bad.
Quoted 2 lines
Exactly. Either we put it on hold for the core rework or we add a
i2c_del_virt_adapter_nolock()variant for the mux-on-i2c case.
The problem is that into i2c_del_virt_adapter() we have to call i2c_del_adapter() and the lock is already holded by i2c_del_driver() since the mux is an i2c chip itself.
Do you think that the problem can be resolved if we use mutex_lock_nested(&core_lock, SINGLE_DEPTH_NESTING) into i2c_del_adapter()?
Thanks for your help,
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