2 I2C Subsystem
2.1 LINUXI2C Subsystem Architecture
The I2C subsystem is already available in the kernel, so you must familiarize yourself with the subsystem before you can do the I2C drive.
2.2 Three major components
1, I2C core (I2c-core)
The I2C core provides I2C bus driver and device-driven registration, logoff methods, I2C communication methods (algorithm), code that is not specific to the adapter, and probe devices, upper-level code for detecting device addresses, and so on.
2, I2C bus driver (I2cadapter/algo driver)
The I2C bus driver is the software implementation of the I2C adapter, which provides the ability of the I2C adapter to complete data communication from the device.
I2C bus drivers are described by I2c_adapter and I2c_algorithm.
3, I2C customer driver (i2cclient driver)
I2C customer driven is the implementation of I2C from the device's software, a specific I2C customer driver consists of two parts: part i2c_driver, used to hook the device to the I2C bus, and the other part of the device itself.
I2C customer drivers are described by I2c_driver and i2c_client
2.3 All I2C driver codes are located in the DRIVERS/I2C directory
I2c-core.c realize the function of I2C core
I2C-DEV.C Universal drive from the device
Chips-specific I2C device driver
Driver for busses I2C adapter
Algos implements a number of I2C bus adapters algorithm
two methods of 2.4 I2C driver writing
From the diagram above we can see two ways to write a drive, One is to use the system provided I2C-DEV.C to implement a I2C adapter device file, and then control the I2C device by operating the I2C adapter in the application layer, and the other is to write a device driver for I2C from the device independently, without the need for i2c-dev.c files.
2.5 Important data Structures
Every time the analysis subsystem analyzes its data structure, OK let's analyze it first.
I2c_adapter structure body represents I2C bus controller
struct i2c_adapter { struct module *owner; unsigned int class; /*classes to allow probing for */ const struct i2c_algorithm* The algorithm of data transmission on algo; /* bus */ void *algo_data; /* algorithm Data */ int timeout; /* injiffies */ int retries; /* Retry times */ struct device dev; /* the adapter device */ int nr; char name[48]; /* Adapter Name */ struct completion dev_released; /* for Sync */ };
I2c_algorithm corresponds to a set of communication methods [CPP] view plaincopy struct I2c_algorithm {int (*master_xfer) (struct i2c_adapter *adap, struct i2c_msg *ms GS, Intnum); Int (*smbus_xfer) (struct i2c_adapter *adap, U16 addr, unsigned short flags, Charread_write, U8 command, int size, unioni2c_smbus_data *data); U32 (*functionality) (Structi2c_adapter *); };
The functionality function is used to return the communication protocols supported by algorithm, such as I2C_FUNC_I2C,I2C_FUNC_10BIT_ADDR.
Master_xfer function realizes the data transmission on the bus, which is related to the specific adapter
master_xfer function Implementation template [CPP] view plaincopy static int I2c_adapter_xxx_xfer (Structi2c_adapter *adap, struct i2c_msg *msgs, int num) { ... for (i = 0; i < num; i++) {I2c_adapter_xxx_start (); /* Generate start bit */if (Msgs[i]->flags & i2c_m_rd) {/* Read/*