Linux Kernel I2C subsystem Learning (2)

Source: Internet
Author: User
The following is a detailed analysis of how to write the first part: the I2C driver of the master chip is divided into two steps: Write the bus driver: select a master chip, for example: (self-compiled chip) the I2C support for this chip (bus driver support) is not found in Driver/I2C/busses/i2c-s3c2410.c )... (Unlucky, no good selection of chips, may be the latest model, Linux kernel did not keep up with) prior to the analysis of the i2c-s3c2410.c to complete the work (bus driver ):
  • Design the module loading function corresponding to the i2c_adapter_xxx_init () template and the module uninstalling function corresponding to the i2c_adapter_xxx_exit () function template.
  • Design the communication method function corresponding to the i2c_adapter_xxx_xfer () template of the s3c248900 adapter, for the s3c24xx, 64xx, s5pc1xx, s5p64xx processor functionality () function s3c24xx_i2c-func () i2c_func_i2c | i2c_func_smbus_emul | i2c_func_protocol_mangling indicates the supported functions.
The saying does not find the bus driver support, (this unlucky child) that has to write a similar i2c-s3c8900.c bus driver support, hey, write according to the above function, anyway is the bus driver. What are you waiting !!~~~~~
  • I2C Adapter Driver loading and uninstallation
  1. Initializes the hardware resources used by the I2C adapter, such as applying for an I/O address and interrupt number.
  2. Use i2c_add_adapter to add the i2c_adapter data structure. Of course, the data structure member has been initialized by the corresponding function pointer of the XXX adapter.
  3. In contrast to loading, the I2C bus unmount module determines whether the I2C adapter uses hardware resources and deletes the i2c_adapter data structure through i2c_del_adapter.
The template is as follows: static int _ init i2c_adapter_xxx_init (void) {xxx_adapter_hw_init (); // initialize hardware resources
I2c_add_adapter (& xxx_adapter );
} Static void _ init i2c_adapter_xxx_exit (void ){
Xxx_adapter_hw_free (); // release hardware resources
I2c_del_adapter (& xxx_adapter );
} For details about the CPU usage, refer to the 6410 practice for some platform operations.
  • I2C bus Communication Method
We need to implement the communication method for a specific I2C adapter, mainly to implement the master_xfer () and functionality () functions of i2c_algorithm.
Functionality function is very simple, used to return the communication protocols supported by algorithm, such as: I2C_FUCN-_I2C, i2c_func_10bit_addr, i2c_func_smbus_read_byte, i2c_func_smbus_write_byte, etc.
The master_xfer function completes each I2C message passed to the i2c_msg array on the I2C adapter.
The template is as follows:
Static int i2c_adapter_xxx_xfer (struct i2c_adapter * ADAP, struct i2c_msg * msgs, int num)
{......
For (I = 0; I <num; I ++ ){
I2c_adapter_xxx_start (); // generate the start bit
// If the message is read
If (MSG [I]-> falgs & i2c_m_rd ){
I2c_adapter_xxx_setaddr (MSG-> ADDR <1) | 1); // send the read address from the device
I2c_adapter_xxx_wait_ack (); // obtain the ACK information from the device i2c_adapter_xxx_readbytes (MSGs [I]-> Buf, msgs [I]-> Len ); // read MSG [I]-> Len long data to MSG [I]-> Buf} else {// write message i2c_adapter_xxx_setaddr (MSG-> ADDR <1) | 1); // send from device write address i2c_adapter_xxx_wait_ack (); // obtain from device ack information i2c_adapter_xxx_readbytes (MSGs [I]-> Buf, msgs [I]-> Len); // read MSG [I]-> Len long data to MSG [I]-> Buf} i2c_adapter_xxx_stop (); // generate a stop position} Well, the load and unload are completed, and the two important items of the communication method are completed, so the bus driver structure has been completed. You're exhausted!
The third part is to write a specific driver.

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.