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.
Conclusion

This two-part series of articles has explained the basics of how to write a kernel I2C bus driver, I2C algorithm driver and I2C chip driver. A lot of good information on how to write I2C drivers can be found in the Documentation/i2c directory in the kernel tree and on the Lm_sensors Web site (secure.netroedge.com/~lm78).

Greg Kroah-Hartman currently is the Linux kernel maintainer for a variety of different driver subsystems. He works for IBM, doing Linux kernel-related things, and can be reached at greg@kroah.com.

______________________

Comments

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

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

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,
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 ?

Webinar
One Click, Universal Protection: Implementing Centralized Security Policies on Linux Systems

As Linux continues to play an ever increasing role in corporate data centers and institutions, ensuring the integrity and protection of these systems must be a priority. With 60% of the world's websites and an increasing share of organization's mission-critical workloads running on Linux, failing to stop malware and other advanced threats on Linux can increasingly impact an organization's reputation and bottom line.

Learn More

Sponsored by Bit9

Webinar
Linux Backup and Recovery Webinar

Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.

In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.

Learn More

Sponsored by Storix