I2C Drivers, Part II
In my last column [LJ, December 2003], we discussed how I2C bus drivers and I2C algorithm drivers work. We also described how to make a tiny dummy I2C bus driver. This month, we discuss how an I2C chip driver works and provide an example of one in action.
An I2C chip driver controls the process of talking to an individual I2C device that lives on an I2C bus. I2C chip devices usually monitor a number of different physical devices on a motherboard, such as the different fan speeds, temperature values and voltages.
The struct i2c_driver structure describes a I2C chip driver. This structure is defined in the include/linux/i2c.h file. Only the following fields are necessary to create a working chip driver:
struct module *owner; — set to the value THIS_MODULE that allows the proper module reference counting.
char name[I2C_NAME_SIZE]; — set to a descriptive name of the I2C chip driver. This value shows up in the sysfs file name created for every I2C chip device.
unsigned int flags; — set to the value I2C_DF_NOTIFY in order for the chip driver to be notified of any new I2C devices loaded after this driver is loaded. This field probably will go away soon, as almost all drivers set this field.
int (*attach_adapter)(struct i2c_adapter *); — called whenever a new I2C bus driver is loaded in the system. This function is described in more detail below.
int (*detach_client)(struct i2c_client *); — called when the i2c_client device is to be removed from the system. More information about this function is provided below.
The following code is from an example I2C chip driver called tiny_i2c_chip.c., which is available from the Linux Journal FTP site [ftp.linuxjournal.com/pub/lj/listings/issue118/7252.tgz]. It shows how the struct i2c_driver structure is set up:
static struct i2c_driver chip_driver = {
.owner = THIS_MODULE,
.name = "tiny_chip",
.flags = I2C_DF_NOTIFY,
.attach_adapter = chip_attach_adapter,
.detach_client = chip_detach_client,
};
To register this I2C chip driver, the function i2c_add_driver should be called with a pointer to the struct i2c_driver:
static int __init tiny_init(void)
{
return i2c_add_driver(&chip_driver);
}
To unregister the I2C chip driver, the i2c_del_driver function should be called with the same pointer to the struct i2c_driver:
static void __exit tiny_exit(void)
{
i2c_del_driver(&chip_driver);
}
After the I2C chip driver is registered, the attach_adapter function callback is called when an I2C bus driver is loaded. This function checks to see if any I2C devices are on this I2C bus to which the client driver wants to attach. Almost all I2C chip drivers call the core I2C function i2c_detect to determine this. For example, the tiny_i2c_chip.c driver does this:
static int
chip_attach_adapter(struct i2c_adapter *adapter)
{
return i2c_detect(adapter, &addr_data,
chip_detect);
}
The i2c_detect function probes the I2C adapter, looking for the different addresses specified in the addr_data structure. If a device is found, the chip_detect function then is called.
If you look closely at the source code, you cannot find the addr_data structure anywhere. The reason for this is it is created by the SENSORS_INSMOD_1 macro. This macro is defined in the include/linux/i2c-sensor.h file and is quite complicated. It sets up a static variable called addr_data based on the number of different types of chips that this driver supports and the addresses at which these chips typically are present. It then provides the ability to override these values by using module parameters. An I2C chip driver must provide the variables normal_i2c, normal_i2c_range, normal_isa and normal_isa_range. These variables define the i2c smbus and i2c isa addresses this chip driver supports. They are an array of addresses, all terminated by either the special value I2C_CLIENT_END or I2C_CLIENT_ISA_END. Usually a specific type of I2C chip shows up in only a limited range of addresses. The tiny_i2c_client.c driver defines these variables as:
static unsigned short normal_i2c[] =
{ I2C_CLIENT_END };
static unsigned short normal_i2c_range[] =
{ 0x00, 0xff, I2C_CLIENT_END };
static unsigned int normal_isa[] =
{ I2C_CLIENT_ISA_END };
static unsigned int normal_isa_range[] =
{ I2C_CLIENT_ISA_END };
The normal_i2c_range variable specifies that we can find this chip device at any I2C smbus address. This allows us to test this driver on almost any I2C bus driver.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
- RSS Feeds
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- A Topic for Discussion - Open Source Feature-Richness?
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- What's the tweeting protocol?
- Tech Tip: Really Simple HTTP Server with Python
- Kernel Problem
9 min 47 sec ago - BASH script to log IPs on public web server
4 hours 36 min ago - DynDNS
8 hours 12 min ago - Reply to comment | Linux Journal
8 hours 44 min ago - All the articles you talked
11 hours 8 min ago - All the articles you talked
11 hours 11 min ago - All the articles you talked
11 hours 13 min ago - myip
15 hours 37 min ago - Keeping track of IP address
17 hours 28 min ago - Roll your own dynamic dns
22 hours 42 min ago




Comments
How the adapter driver verifies the specified address
"The i2c_detect function probes the I2C adapter, looking for the different addresses specified in the addr_data structure. If a device is found, the chip_detect function then is called"
Note quite understand, how the adapter driver looks for the address specified. How does it know there is a device at the other end bus would have the address, if it does not read/write some thing from the device. But the adapter driver does not know how to read/write to the device.
RE: How the adapter driver verifies the specified address
Same question here
To probe or not to probe...
kamou:
I'm currently struggling with a similar issue. I'm trying to write a driver for an image sensor which can be controlled via I2C. I'm still trying to wrap my head around how to get a character device and an I2C driver to coexist in a single device driver. I got around the whole SENSORS_INSMOD_1 macro issue by hard coding the I2C address since this is known a priori for my application. Therefore I do not call i2c_detect in the attach_adapter() callback. However, to enable probing, I found that if the normal_i2c, normal_i2c_range, normal_isa, and normal_isa_range macros are populated with proper address data, the invocation of the SENSORS_INSMOD_1 macro will set up the addr_data structure accordingly. The i2c_detect then probes only those addresses in the range specified by these arrays. When one is found the callback provided to i2c_detect (chip_detect in the article's example) is invoked to setup a client for that chip.
My question, however, is there seems to be an m:n ratio of adapters to i2c busses. I'm trying to figure out how to manage that within a single char device driver. I'm currently looking at the i2c-dev implementation, however, this uses the old style of registering character devices which I'm not familiar with so I'm not 100% sure what's going on. Does anyone have any insight and silently using an I2C chip driver from within a char device driver?
trying to write a driver for an audio chip
hello !
I'm trying to write a driver for an audio chip, this chip can be controlled via i2c bus (i2c-0)
the problem is that I don't understand how I can "open" the i2c-0 in kernel space...
I tried to do as le lm75 sensor driver...but it's not better..
I know that I have to register my driver as an i2c driver with the i2c_add_driver function
does this:
static int
chip_attach_adapter(struct i2c_adapter *adapter)
{
return i2c_detect(adapter, &addr_data,
chip_detect);
}
probes every i2c bus on my board ?
and I don't understand what addr_data is or what it does,
I know it is in the SENSORS_INSMOD_1 macro
but I'm not writing a driver for a sensor, but for an audio chip...
can anyone help me understand better all this ?