I2C Drivers, Part II

Here's what has to happen to read the sensors that report the temperature, fan speed and other important system health information.

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,

Registering a Chip Driver

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)

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,

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[] =
static unsigned short normal_i2c_range[] =
  { 0x00, 0xff, I2C_CLIENT_END };
static unsigned int normal_isa[] =
static unsigned int normal_isa_range[] =

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.



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

How the adapter driver verifies the specified address

Anonymous's picture

"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

Anonymous's picture

Same question here

To probe or not to probe...

Marc's picture


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

kamou's picture

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,

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 ?