Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the I2C driver Framework of Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to carry out the Linux I2C driver framework analysis, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.

1. Basic concept

Bus

The bus represents the working sequence that the same kind of devices need to follow together. different buses have different requirements for the physical level, and the level maintenance width for each bit is also different, and the commands transmitted on the bus will have their own format constraints. Such as I2C bus, USB bus, PCI bus and so on. Taking the I2C bus as an example, different I2C devices are connected to the same group of I2C buses.

Equipment

The device represents a real and specific physical device, and the device is represented by the unique parameter properties of the device in software. For example, the I2C slave devices connected on the I2C bus have a device address that identifies themselves, and this device address determines whether the command sent by the master device should be answered by it.

Drive

To put it simply, the driver represents the way and process of operating the device.

The working principle of Linux bus device Framework

If you want to understand the I2C driver framework, you must have a deep understanding of the Linux bus device framework. A very important reason for the formation of such a framework is for the reusability of the code. Because the relationship between driver and device is one-to-many, the same set of driver interfaces can be shared for different devices of the same type. In order to improve the portability of drivers, Linux abstracts a set of functions to manage resources. The device is the existing hardware, which contains its own properties as well as the resources that need to be used.

The function of the bus is to manage the device and driver at the software level. If the device wants to make the system aware of its own existence, it needs to register the device with the bus, and the driver also needs to register with the bus. For bus, there are I2C bus, Platform bus and so on. But Platform is a virtual bus.

The bus is responsible for the matching between the device and the driver on the bus. When the device is registered, the bus traverses the driver registered on the bus. If the name is the same, then the match is made, and the probe function of the driver is called at this time. When the same driver is registered, it also traverses the device on the bus, and if it matches (with the same name), it also calls the driver's probe function.

2.I2C transport protocol

For I2C, it has the following characteristics:

1. One serial data line (SDA), one serial clock line (SCL)

two。 Each device connected to the bus can be identified by its unique address using software.

3. Serial 8-bit bidirectional data transmission, bit rate up to 100kbit/s in flag mode and 400kbit/s in fast mode. 3.4Mbit/s can be reached in high speed mode.

Let's take a look at the specific hardware connections.

The above is the connection mode of the camera I2C on the TFS, and only two wires can realize the data transmission. During transmission, you need to pay attention to the following three types of signals:

(1) start signal (S): SCL is high level, SDA jumps from high level to low level, and starts to transmit data.

(2) end signal (P): SCL is high level, SDA jumps from low level to high level, and ends data transmission.

(3) response signal (ACK): after receiving 8-bit data, the receiver lowers the level of SDA in the 9th clock cycle.

The above is the basic overview of the hardware layer and protocol layer of I2C, which can be used as a basic cognition.

The Architecture of I2C driver under 3.Linux

For the I2C driver under Linux, the architecture is mainly divided into three parts.

(1) I2C core: I2C core provides registration of I2C bus driver and device driver, logout method, upper layer code of I2C communication method ("algorithm"), code independent of specific adapter, detection device, upper layer code of detection device address, etc.

(2) I2C bus driver: I2C bus driver is the implementation of the adapter in the I2C hardware architecture, the adapter can be controlled by CPU, and even can be directly integrated in CPU.

(3) I2C device driver: I2C device driver (also known as customer driver) is the implementation of the device side in the I2C hardware architecture. The device is generally connected to the I2C adapter controlled by CPU and exchanges data with CPU through the I2C adapter.

More important documents

\ kernel\ drivers\ I2C\ i2c-core.c

This file implements the functions of the I2C core and / proc/bus/i2c* interface. At the same time, the transceiver function of I2C is encapsulated. Will call i2c_transfer, which implements adap- > algo- > master_xfer (adap, msgs, num)

Kernel\ drivers\ i2C\ i2c-dev.c

This function registers the function of a device file, that is, registers a character device driver, which can be accessed through / dev/i2c-0 (i2c-0, i2c-1,... , i2c-10,...) Find the specific I2C adapter. The major number of this I2C device is 89, and the secondary number is 00255. By accessing this interface, you can access the device through open (), write (), read (), ioctl (), close (), and so on.

Kernel\ drivers\ i2C\ busses\ i2c-v12-jz.c

This function controls the I2C operation of Junzheng x1000 by setting registers. The lowest-level transceiver functions are defined in this file. The important thing is i2c_jz_algorithm, where algorithm implements the operation of the underlying registers.

A more important structure

I2c_driver, i2c_client, i2c_adapter and i2c_algorithm are the four key structures.

I2c_driver

Corresponding to a set of driving methods, it is a purely auxiliary data structure, which does not correspond to any physical entity.

I2c_client

Corresponding to the real physical device, each I2C device needs an i2c_client to describe. I2c_client is generally included in the private information structure of I2C character devices.

I2c_adpater

Used to match i2c_driver and i2c_client. That is, i2c_client is attached to i2c_adpater. Since multiple I2C devices can be connected to an adapter, an i2c_adpater can also be attached to multiple i2c_client, and the i2c_adpater includes a linked list of i2c_client attached to it.

I2c_algorithm

Struct i2c_algorithm {

/ / transceiver function interface in i2C mode

Int (* master_xfer) (struct i2c_adapter * adap, struct i2c_msg * msgs,int num)

/ / used for transceiver function interface in SMBUS mode

Int (* smbus_xfer) (struct i2c_adapter * adap, U16 addr, unsigned short flags, char read_write, U8 command, int size, union i2c_smbus_data * data)

/ / used to check the access interfaces supported by the I2C master controller, such as I2C_FUNC_SMBUS_BYTE, to see if smbus single-byte read and write operations are supported

U32 (* functionality) (struct i2c_adapter *)

}

This function mainly implements the operation of its I2C bottom layer.

Case Analysis of 4.GC0328 camera I2C

For camera drivers, you first need to know how to make the camera work properly.

Step 1: power up the camera

In this step of work, you can control the relevant GPIO to enable the camera, control RESET and POWERON to make the camera work properly.

The power-up sequence of GC0328 is shown in the following figure:

Step 2: provide the camera with a clock

This step is also the key, for the camera, the clock is the heartbeat, if you want the camera to work properly, you need x1000 CIM to provide 24MHz clock to the camera.

Step 3: configure the register of the camera

For a camera sensor, if you need to output pictures of the specified size and format, you need to configure the camera register. The camera register needs to be configured through I2C.

Step 4: configure CIM

The camera interface control module inside X1000 can process camera data, check frame errors and transmit data. This part of the control needs those CIM-related registers to complete.

Step 5: start CIM

After the configuration and initialization are completed, the camera can be started, and CIM is responsible for data transmission and corresponding interruptions.

The above is a complete process of camera initialization. For the camera initialization part, how is I2C initialized and set? This is also the focus of this article.

According to the previous bus device driver framework, if there is driver, then there must be device. The match between the two depends on .id _ table

For gc0308, you can use kernel/arch/mips/xburst/soc-x1000/chip-x1000/halley2/common/i2c_bus.c

You can see that the device registered with the I2C bus is gc0308.

If there is a match, the .probe function of driver is called. Let's take a look at exactly what the function does.

In the probe function, it mainly provides v412_i2c_subdev with an operable client, that is, the interface of the operation function equivalent to I2C is handed over to the V4L2 video driver framework to manage. The functions provided to the V4L2 video driver framework are as follows:

The first structure is the interface for video operation, such as the output format of the device gc0328, the current video output format, and so on.

The second structure is to control the power on and power off of the camera, as well as other parameters that control the white balance and focus.

Set up through the I2C sub-device control of V4L2. Let's take a basic analysis of the calling process:

When the application passes VIDIO_S_FMT through ioctl, the camera output can be formatted.

Then take a look at the process of writing the register.

The i2c_smbus_write_byte_data function is called in kernel\ drivers\ i2c\ i2c-core.c, which enters the i2C bus operation function.

This function calls i2c_smbus_xfer

Why not meet the conditions? you can see the registered i2C platform devices under the kernel\ drivers\ i2C\ busses\ i2c-v12-jz.c path.

There's an i2c_algorithm structure.

Struct i2c_algorithm {

/ / transceiver function interface in i2C mode

Int (* master_xfer) (struct i2c_adapter * adap, struct i2c_msg * msgs,int num)

/ / used for transceiver function interface in SMBUS mode

Int (* smbus_xfer) (struct i2c_adapter * adap, U16 addr, unsigned short flags, char read_write, U8 command, int size, union i2c_smbus_data * data)

/ / used to check the access interfaces supported by the I2C master controller, such as I2C_FUNC_SMBUS_BYTE, to see if smbus single-byte read and write operations are supported

U32 (* functionality) (struct i2c_adapter *)

}

So it will only be executed downward, and when it comes to i2c_smbus_xfer_emulated, it will call

The function calls the

Finally, the lowest level implementation of kernel\ drivers\ I2C\ busses\ i2c-v12-jz.c is called.

In the kernel\ drivers\ i2C\ busses\ i2c-v12-jz.c function

This function points to i2c_jz_xfer

In this function, the read and write of I2C is realized, and the read operation function write operation can be judged according to the passed flag.

The lowest operation register to realize its read and write function

At this point, a complete I2C transmission process is completed.

5. Summary

For the complete transport protocol of I2C, the most important thing is to understand the framework of the bus driver, because I2C also belongs to the bus framework. The model of the I2C bus device framework can be illustrated with the following figure:

That is, device and driver register with the i2C bus at the same time. When registered on the bus, it can be matched by id_table, after which the .probe function of driver will be called. For general I2C devices, you can register a character device driver in the probe function, so that the application layer can open / dev/i2c-0 and other device nodes through the open function. So as to read and write to the I2C device. In the camera part, the control interface is directly transferred to the V4L2 for management, so the camera is adjusted through the video device driver framework, so as to achieve the purpose of control.

The above content is how to analyze the I2C driver framework of Linux. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report