How to use one I2C device from two modules?
Hello,
On our custom board we have an I2C GPIO port expander. This is used for different things like power supply control and button control. Hence I wrote two modules; pscontrol and button.
In the modules I use i2c_new_device to create the device and to get the i2c_client struct that I later use in the smbus calls. This works fine, one module at the time. Together the last insmod fails, because the device at that address is already created.
But how do I access the same I2C device from several modules? Ideally I want to specify the adapter and slave address. If a client is found, I get the i2c_client otherwise I have to create it by i2c_new_device (or probe it). Is it possible?
Since I know the I2C bus and slave addresses in advance, would it be better to predeclare them by i2c_register_board_info? And in that case, how do I get the i2c_client struct in the module?
Kernel version 2.6.29
Target is m68k (Coldfire)
Thanks and regards,
- Lars
A message that is not in the archive (sent off-list or not yet received): 842687.69100.qm-sMamaaD5nQOvuULXzWHTWIglqE1Y4D90QQ4Iyu8u01E@public.gmane.org
Hi Lars,
Quoted 7 lines · Lars Michael
On our custom board we have an I2C GPIO port expander. This is used for different things like power supply control and button control. Hence I wrote two modules; pscontrol and button.
In the modules I use
i2c_new_deviceto create the device and to get thei2c_clientstruct that I later use in the smbus calls. This works fine, one module at the time. Together the last insmod fails, because the device at that address is already created.But how do I access the same I2C device from several modules? Ideally I want to specify the adapter and slave address. If a client is found, I get the
i2c_clientotherwise I have to create it byi2c_new_device(or probe it). Is it possible?Since I know the I2C bus and slave addresses in advance, would it be better to predeclare them by
i2c_register_board_info? And in that case, how do I get thei2c_clientstruct in the module?
In general I would have pointed you to drivers/mfd and told you to write a mfd core driver for your chip. However in your case I don't think you have a multifunction device. You have a single function device (GPIO) with multiple users. So I suggest that you simply write a proper gpio driver for your chip, and get the device registered as an I2C GPIO device. Then, in your function drivers (power supply control and button control) get a reference to the gpio device in question, and use it.
FWIW, we already have support for many I2C-based GPIO chip families. Take a look in drivers/gpio, files adp5588-gpio.c, max7300.c, max732x.c, pca953x.c, pcf857x.c and sx150x.c. With some luck, your chip is already supported, so all you have to do is instantiate it in your platform code.
Signature · 1 line
Jean Delvare
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 9 lines · Jean Delvare
Quoted 1 line · Lars Michael
But how do I access the same I2C device from several
modules? Ideally I want to specify the adapter and slave address. If a client is found, I get the
i2c_clientotherwise I have to create it byi2c_new_device(or probe it). Is it possible?Quoted 1 line
Quoted 15 lines
In general I would have pointed you to
drivers/mfdand told you to
write a mfd core driver for your chip. However in your case I don't
think you have a multifunction device. You have a single function
device (GPIO) with multiple users. So I suggest that you simply write a
proper gpio driver for your chip, and get the device registered as an
I2C GPIO device. Then, in your function drivers (power supply control
and button control) get a reference to the gpio device in question, and
use it.
Thanks for the advice. I wrote a simple driver for my chip and instantiated it in the platform code. In order to access the device from the function modules, I exported an i2c_client get function, in order to get a reference to the client:
struct i2c_client *pca950x_get_i2c_client(void)
.....EXPORT_SYMBOL(pca950x_get_i2c_client);
Not sure this is the perfect way to do it, but it works.
Thanks and regards,
- Lars
A message that is not in the archive (sent off-list or not yet received): 135179.26946.qm-XzixtO+UlYSvuULXzWHTWIglqE1Y4D90QQ4Iyu8u01E@public.gmane.org
Hi Lars,
Quoted 26 lines · Lars Michael
Quoted 9 lines · Jean Delvare
Quoted 1 line · Lars Michael
But how do I access the same I2C device from several
modules? Ideally I want to specify the adapter and slave address. If a client is found, I get the
i2c_clientotherwise I have to create it byi2c_new_device(or probe it). Is it possible?Quoted 1 line
Quoted 15 lines
In general I would have pointed you to
drivers/mfdand told you to
write a mfd core driver for your chip. However in your case I don't
think you have a multifunction device. You have a single function
device (GPIO) with multiple users. So I suggest that you simply write a
proper gpio driver for your chip, and get the device registered as an
I2C GPIO device. Then, in your function drivers (power supply control
and button control) get a reference to the gpio device in question, and
use it.
Quoted 2 lines
Thanks for the advice. I wrote a simple driver for my chip and instantiated it in the platform code. In order to access the
Please submit your driver for integration in the upstream kernel. Others are certainly using the same device!
Quoted 8 lines
device from the function modules, I exported an
i2c_clientget function, in order to get a reference to the client:struct
i2c_client*pca950x_get_i2c_client(void)
.....EXPORT_SYMBOL(pca950x_get_i2c_client);Not sure this is the perfect way to do it, but it works.
No, this doesn't seem right. A given system may have more than one PCA950x chip. And symbol dependencies are a hell to handle as soon as things get more complex. Such an export will never be accepted upstream.
I can think of 2 ways to handle your problem.
1* Use gpiochip_find(). It is currently only available to drivers if gpiolib is built into the kernel, but I consider this a bug, patch submitted [1] but no reply so far.
2a* Install a bus notifier on i2c bus type, and catch event BUS_NOTIFY_BIND_DRIVER. This only works if you can install the notifier before the pca950x driver binds to the GPIO chip.
2b* Use i2c_for_each_dev() to get a handle to your i2c client. This only works if you are certain that the GPIO chip driver is loaded before the drivers which need it.
Option 1* seems much cleaner to me, as it abstracts the bus type completely. After all, sub-drivers should only care about the GPIO functionality and not the technical implementation details.
If neither works for you for whatever reason, maybe we can add a helper function to i2c-core to retrieve an i2c_client by bus number and address. Maybe others would find it useful as well.
[1] http://marc.info/?l=linux-kernel&m=130293906304999&w=2
Signature · 1 line
Jean Delvare
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 41 lines · Jean Delvare
Quoted 1 line · Lars Michael
--- On Sat, 7/5/11, Jean Delvare <[email protected]>
Quoted 1 line · wrote:
Quoted 1 line
On Fri, 6 May 2011 04:30:33 -0700 (PDT), Lars
Michael
Quoted 2 lines
Quoted 2 lines
Quoted 1 line · wrote:
But how do I access the same I2C device from
several
Quoted 1 line
Quoted 1 line
modules? Ideally I want to specify the adapter
and slave
Quoted 1 line
Quoted 1 line
address. If a client is found, I get the
i2c_clientQuoted 1 line
Quoted 1 line
otherwise I have to create it by
i2c_new_device(or probe
Quoted 4 lines
Quoted 4 lines
it). Is it possible?
Quoted 1 line
In general I would have pointed you to
drivers/mfdand toldQuoted 2 lines
Quoted 2 lines
you to
write a mfd core driver for your chip. However inyour case
Quoted 2 lines
Quoted 2 lines
I don't
think you have a multifunction device. You have asingle
Quoted 2 lines
Quoted 2 lines
function
device (GPIO) with multiple users. So I suggestthat you
Quoted 2 lines
Quoted 2 lines
simply write a
proper gpio driver for your chip, and get thedevice
Quoted 2 lines
Quoted 2 lines
registered as an
I2C GPIO device. Then, in your function drivers(power
Quoted 2 lines
Quoted 2 lines
supply control
and button control) get a reference to the gpiodevice in
Quoted 3 lines
Quoted 2 lines
question, and
use it.
Quoted 8 lines
Quoted 1 line
Thanks for the advice. I wrote a simple driver for my
chip and
Quoted 1 line
instantiated it in the platform code. In order to
access the
Please submit your driver for integration in the upstream kernel.
Others are certainly using the same device!
Please note that my driver has no device specific functions. It only allows the device to be registered so that my function modules can use the smbus commands and the i2c client handle to use the device. The GPIO device/lib is not supported. So, I don't think it's worth submitting it at this point...
Quoted 9 lines
Quoted 1 line
device from the function modules, I exported an
i2c_clientgetQuoted 7 lines
function, in order to get a reference to the client:
struct
i2c_client*pca950x_get_i2c_client(void)
.....EXPORT_SYMBOL(pca950x_get_i2c_client);Not sure this is the perfect way to do it, but it
Quoted 8 lines
works.
No, this doesn't seem right. A given system may have more than one
PCA950x chip. And symbol dependencies are a hell to handle as soon as
things get more complex. Such an export will never be accepted upstream.
You are right, and this quick solution was not meant to be submitted.
Quoted 7 lines
I can think of 2 ways to handle your problem.
If neither works for you for whatever reason, maybe we can add a helper
function to i2c-core to retrieve ani2c_clientby bus number and
address. Maybe others would find it useful as well.
I was looking for such a function, it could be very handy. And btw a get adapter by id already exists.
Thanks and regards,
- Lars
A message that is not in the archive (sent off-list or not yet received): 184526.31465.qm-hgS9n0fW3jyvuULXzWHTWIglqE1Y4D90QQ4Iyu8u01E@public.gmane.org
Hi Lars,
Quoted 10 lines · Lars Michael
Quoted 4 lines · Jean Delvare
No, this doesn't seem right. A given system may have more than one PCA950x chip. And symbol dependencies are a hell to handle as soon as things get more complex. Such an export will never be accepted upstream.
You are right, and this quick solution was not meant to be submitted.
Quoted 2 lines
I can think of 2 ways to handle your problem.
Quoted 7 lines
Quoted 4 lines
If neither works for you for whatever reason, maybe we can add a helper function to i2c-core to retrieve an
i2c_clientby bus number and address. Maybe others would find it useful as well.I was looking for such a function, it could be very handy. And btw a get adapter by id already exists.
This function could certainly be added if someone ever really needs it. But in your case, it seems better to write a proper gpio driver for your device and then have your subdrivers cleanly request the GPIO pins they need. And you don't need to access the i2c_client from the subdrivers if you do this.
Signature · 1 line
Jean Delvare