Linux I2C driver architecture

Source: Internet
Author: User

Linux I2C driver architecture
1. Linux I2C driver architecture in Linux, I2C BUS drivers are divided into two parts: BUS and DEVICE drivers ). The role of the bus driver is to add 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.

A device driver is a driver that communicates with a specific device mounted on the I2C bus. Using the functions provided by the I2C bus driver, device drivers can ignore the differences between different bus controllers and communicate with hardware devices without considering their implementation details.

The 1.1 bus driver is first loaded with the I2C bus driver when the system is started. 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:

Static struct i2c_adapter pb1550_board_adapter = {
Name: "pb1550 adapter ",
Id: I2C_HW_AU1550_PSC,
Algo: NULL,
Algo_data: & pb1550_i2c_info,
Inc_use: pb1550_inc_use,
Dec_use: pb1550_dec_use,
Client_register: pb1550_reg,
Client_unregister: pb1550_unreg,
Client_count: 0,
};

In this example, a driver named "pb1550 adapter" is attached. However, this module does not provide read/write functions. The specific read/write method is provided by the second module, struct i2c_algorithm.

Static struct i2c_algorithm au1550_algo = {

. Name = "Au1550 algorithm ",

. Id = I2C_ALGO_AU1550,

. Master_xfer = au1550_xfer,

. Functionality = au1550_func,

};

I2c_adap-> algo = & au1550_algo;

This example adds a read/write "algorithm" to the above bus driver ". Generally, each I2C bus driver defines a read/write algorithm. However, some bus use the same algorithm, so they can share the same set of read/write functions. In this example, the driver defines its own read/write algorithm Module named "Au1550 algorithm ".

After completing all the information, call:

I2c_add_adapter (i2c_adap );

Register the two modules in the operating system, and the bus driver is installed. For AMD au1550, this Part has been provided by AMD.

As mentioned above, the 1.2 device driver only provides a read/write mechanism for a bus, and does not communicate with the driver itself. Communication is implemented by I2C device drivers. Device Drivers communicate with specific devices through I2C bus. A device driver consists of two modules, struct i2c_driver and struct i2c_client.

After the system is started and the I2C bus driver is loaded, the device driver can be loaded. First, load the following structure:

Static struct i2c_driver driver = {

. Name = "i2c TV tuner driver ",

. Id = I2C_DRIVERID_TUNER,

. Flags = I2C_DF_NOTIFY,

. Attach_adapter = tuner_probe,

. Detach_client = tuner_detach,

. Command = tuner_command,

};

I2c_add_driver (& driver );

Once the i2c_driver is loaded, the attach_adapter function will be called. You can traverse each i2c bus driver in the system to detect the devices you want to access:

Static int tuner_probe (struct i2c_adapter * adap)

{

Return i2c_probe (adap, & addr_data, tuner_attach );

}

Note that multiple devices may be found, so not only can one I2C bus be mounted to multiple devices of different types, a device driver can also serve devices mounted on multiple different I2C buses at the same time.

Whenever a device driver detects a device that it supports, it creates a struct i2c_client to identify the device:

New_client-> addr = address;

New_client-> adapter = adapter;

New_client-> driver = & driver;

/* Tell the I2C layer a new client has arrived */

Err = i2c_attach_client (new_client );

If (err)

Goto error;

It can be seen that an i2c_client is located on the adapter bus and the address is address. A device is driven by a driver. It binds the bus driver with the device driver and the device address. An i2c_client represents an I2C device.

After obtaining the I2C device, you can directly read and write the device:

/*

* The master routines are the ones normally used to transmit data to devices

* On a bus (or read from them). Apart from two basic transfer functions

* Transmit one message at a time, a more complex version can be used

* Transmit an arbitrary number of messages without interruption.

*/

Extern int i2c_master_send (struct i2c_client *, const char *, int );

Extern int i2c_master_recv (struct i2c_client *, char *, int );

Like reading and writing functions in the general sense, these two functions read and write int char to the device specified by the i2c_client pointer. The returned value is the number of bytes read/write. For our existing SLIC driver, as long as the data that needs to be read and written to the bus is transmitted to these two functions, the porting is completed, we will get an I2C Device Driver for Linux.

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.