Recently in the study of the Linux I²c Drive, from the lowest level of the I²C controller initialization to the application layer and the interaction of the I²C device is basically opened.
One, the Linux i²c architecture can be represented by:
The IIC adapter corresponds to an I²C bus, which is represented in Linux with the i2c_adapter structure. The I²C device under the bus is represented by the i2c_client structure. Algorithm is an I²C communication protocol, the IIC core is responsible for registration/Logoff I2c_adapter, Registration/Logoff i2c_client, registration of the I²C bus operations. The I2c-dev corresponds to I2C-DEV.C, which is responsible for instantiating the bus as a i2c_client, and the application layer can communicate with any device under the bus by manipulating the corresponding device node. IIC device driver is the driving method of specific devices under the I²c bus.
The specific IIC controller operation and algorithm by the IIC controller driver implementation, common different processors of the I²C bus drive in the kernel source code/driver/i2c/buses/directory. Specific i²c device drive kernel source code is not implemented, need to provide their own.
The following excerpt from Song Baohua Teacher's book, "Linux device driver development in detail," said as an engineer to achieve the main work:
I²c Bus driver:
(1) Provide the hardware driver of the I²C adapter, detect, initialize the I²C adapter (such as the request for the I/O address and the interrupt number), drive the CPU control of the I²C adapter from the hardware to generate a variety of signals and processing i²c interrupt.
(2) Provide the algorithm of the I²C adapter, fill the Master_xfer pointer of I2c_algorithm with the Xxx_xfer () function of the specific adapter, and assign the I2c_algorithm pointer to i2c_adapter pointer.
I²c Device driver:
(1): Implementation of the I2c_driver interface in the I²C device driver, with the specific device yyy Yyy_probe (), Yyy_remove (), Yyy_suspend (), yyy_resume () function pointers and I2c_device_ The ID Device ID table is assigned to the I2c_driver probe, remove, suspend, and id_table pointers.
(2): To achieve the specific type of i²c device driver, i2c_driver only to achieve the device and the bus hook, and the device hooked on the bus varies widely. For example, if it is a character device, implement the file operation interface, that is, implement the device yyy Yyy_read (), Yyy_write () and Yyy_ioctl () function, etc., if it is a sound card, the implementation of ALSA driver.
Second, the core of I²c
(1) Add/Remove I2c_adapter
int I2c_add_adapter (struct i2c_adapter *adapter);
int I2c_del_adapter (struct i2c_adapter *adap);
(2) Add/Remove I2c_driver
int i2c_register_driver (struct module *owner, struct i2c_driver *driver);
void I2c_del_driver (struct i2c_driver *driver);
static inline int i2c_add_driver (struct i2c_driver *driver)
{
Return I2c_register_driver (This_module, driver);
}
(3) I²c transmit, send and receive
int I2c_transfer (struct i2c_adapter *adap, struct i2c_msg *msgs, int num);
int i2c_master_send (const struct i2c_client *client, const char *buf, int count);
int i2c_master_recv (const struct i2c_client *client, char *buf, int count);
Linux-driven I²c learning