Linux I2C Driver Analysis

Source: Internet
Author: User
Tags ack

I recently checked the Linux 2.6.21 kernel's I2C driver and checked the relevant information on the Internet. I am also right about the error. I have some experience. Let's record it. There may be many mistakes in it. please correct them.
 
1. I2C Protocol
 
1.1 Working Principle of I2C bus
The I2C bus is a serial bus consisting of the SDA data line and the clock SCL. Various controllers are connected in parallel to this bus, and each device has a unique address recognition, it can be used as a sending device or receiver on the bus (determined by the function of the device)
Signal Status of 1.2 I2C bus
1. Idle status: SDA and SCL are both high.

2. Start condition (s): In high-power mode, SDA changes from high level to low level and starts to transmit data.

3. End condition (P): When the SCL is low, SDA changes from low level to high level and ends data transmission.

4. valid data: SDA remains stable and valid during the high-level period of the SCL. SDA changes can only occur at the bottom level of the SCL.

5. Ack signal: During data transmission, each byte of data received by the receiver generates an ACK signal, which sends a specific low-level pulse to the sending device, indicating that the data has been received.

1.3 Basic I2C bus operations

The I2C bus must be controlled by the master device (usually a microcontroller). The master device generates a serial clock (SCL), controls the transmission direction of the bus, and generates Start and Stop conditions.

In data transmission, the primary device generates the initial condition, followed by the control byte of the device (the first seven digits are the address of the device, and the last one is the read/write bit ). The next step is to read and write the data and the ACK response signal. When data transmission ends, the master device generates a stop condition.

 

2. Linux I2C Structure Analysis

2.1 Hierarchical Analysis

1. I2C Core

I2C core is used to maintain the I2C core of Linux. It maintains two static lists, respectively recording the I2C driver structure and I2C adapter structure in the system.

Static list_head (adapters );

Static list_head (drivers );

I2C core provides interface functions that allow an I2C adatper, I2C driver, and I2C client to be registered in I2C core during initialization and deregister upon exit. For details, see i2c_core.c code.

At the same time, it also provides general interfaces for I2C bus read/write access (the specific implementation is implemented in I2C adapter related to I2C Controller) and is mainly used in I2C device drivers.

Commonly used

I2c_master_send ()

I2c_master_recv ()

I2c_transfer ()

 

2. I2C bus driver

Bus drivers are responsible for adding corresponding read/write methods for each I2C bus in the system. However, the bus driver does not communicate with each other. It only exists and waits for the device driver to call its function.

When the system is started, the I2C bus driver is first loaded. A bus driver is used to support reading and writing of a specific I2C bus. A bus driver usually requires two modules, a struct i2c_adapter and a struct i2c_algorithm to describe:

The I2C bus adapter of PXA is realized in the i2c-pxa.c under the buses directory. the I2C adapter constructs a data structure for the interface of I2C core layer, and registers a controller to I2C core through the interface function. The I2C adapter mainly implements the I2C bus access algorithm. The iic_xfer () function is the implementation of the I2C adapter's underlying read/write method on the I2C bus. At the same time, I2C adpter also implements the handler for I2C controller interruption.

1) The i2c-pxa.c defines i2c_algorithm, and implements the master's sending function i2c_pxa_xfer (), and the device query bus mode function i2c_pxa_functionality ()

Static const struct i2c_algorithm i2c_pxa_algorithm = {
. Master_xfer = i2c_pxa_xfer,
. Functionality = i2c_pxa_functionality,
};

2) The i2c_adapter is implemented in the i2c-pxa.c, Which is initialized when the pxa-i2c is defined, and the parent pointer is filled in the i2c_pxa_probe () and

Ret = i2c_add_adapter (& I2C-> ADAP );

.

Static struct pxa_i2c i2c_pxa = {
. Lock = spin_lock_unlocked,
. ADAP = {
. Owner = this_module,
. Algo = & i2c_pxa_algorithm,
. Name = "pxa2xx-i2c.0 ",
. Retries = 5,
},
};

In general, in the i2c-pxa, the use of platform driver model, completed the I2C bus two modules struct i2c_adapter and struct i2c_algorithm

 

3. I2C Device Driver

Only the bus driver is not enough for I2C and must have a device to work. This is the necessity of I2C device driver. I2C device has two modules to describe, struct i2c_driver and struct i2c_client.

Before introducing Device Driver Under the chips directory, it is necessary to introduce the i2c-dev.c file.

I2c-dev.c provides a general I2C device driver, to achieve the access interface of the character type device, the specific access to the device is achieved through I2C adapter. Construct a data structure for the I2C core layer interface, and register an I2C device driver with the I2C core through the interface function. At the same time, construct a data structure for the user-layer interface, and register it with the kernel as a character-type device with the main device Number of 89 through the interface function.

Static struct i2c_driver i2cdev_driver = {
. Driver = {
. Name = "dev_driver ",
},
. ID = i2c_driverid_i2cdev,
. Attach_adapter = i2cdev_attach_adapter,
. Detach_adapter = i2cdev_detach_adapter,
. Detach_client = i2cdev_detach_client,
};

Struct i2c_dev {
Struct list_head list;
Struct i2c_adapter * ADAP;
Struct device * dev;
};
 

This file provides user-layer access to I2C devices, including common file operations such as open, read, write, ioctl, and release. We can use the open function to open I2C device files, the ioctl function is used to set the address for accessing the slave device. Then, the read and write operations on the I2C device can be performed through the Read and Write Functions.

Static const struct file_operations i2cdev_fops = {
. Owner = this_module,
. Llseek = no_llseek,
. Read = i2cdev_read,
. Write = i2cdev_write,
. IOCTL = i2cdev_ioctl,
. Open = i2cdev_open,
. Release = i2cdev_release,
};

Note: general methods provided by I2C driver can be used to access any I2C device. However, the read, write, ioctl, and other functions are fully implemented based on general devices, all operation data is based on byte streams and has no clear format or meaning. To facilitate and effectively use I2C devices, we can develop specific I2C device drivers for a specific I2C device, complete the interpretation of specific data formats and implement some special functions in the driver.

The chips directory contains various device drivers to complete registration of various slave devices. As a general I2C device, the operation in the i2c-dev.c is enough to complete the operation.

Of course, if it cannot be completed, you need to complete the driver independently. This is the code under the chips directory. Because the i2c-dev.c has implemented the file operation interface of the I2C device, you only need to implement struct i2c_driver. You can use the command interface to control some special operations. Of course, for the FM chip of the i2 interface, put struct i2c_driver under the chips directory of I2C, and put the Code related to the other FM operation under the/Media/Radio directory. In this directory, you need to complete the Read and Write interfaces, most of which use the v4l2 architecture.

Continue analysis ......

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.