Linux driver subsystem I2C (5)

Source: Internet
Author: User

5 customer-driven

5.1 Overview

The I2C client driver is the implementation of I2C slave device. A specific I2C client driver consists of two parts: the i2c_driver, which is used to connect the device to the I2C bus; the other part is the driver of the device.

The I2C client driver is mainly described by i2c_driver and i2c_client.

 

5.2 instance source code analysis

Now, let's take a closer look at the implementation of the customer's driver code. The drivers/MISC/EEPROM/at24.c file supports the eeprom of most I2C interfaces.

I2c_driver implementation

Static struct i2c_driver at24_driver = {. driver = {. name = "at24 ",. owner = this_module ,},. probe = at24_probe,/* called when the i2c_client matches the i2c_driver */. remove = _ devexit_p (at24_remove),/* called at logout */. id_table = at24_ids,/* i2c_driver supports the i2c_client type */};

Initialization and uninstallation

static int __init at24_init(void){         returni2c_add_driver(&at24_driver);} static void __exit at24_exit(void){         i2c_del_driver(&at24_driver);}

At24_probe Function

static int at24_probe(struct i2c_client*client, const struct i2c_device_id *id){         ……                 /*          * Export the EEPROM bytes through sysfs, sincethat's convenient.          * By default, only root should see the data(maybe passwords etc)          */         sysfs_bin_attr_init(&at24->bin);         at24->bin.attr.name= "eeprom";         at24->bin.attr.mode= chip.flags & AT24_FLAG_IRUGO ? S_IRUGO : S_IRUSR;         at24->bin.read= at24_bin_read;         at24->bin.size= chip.byte_len;          at24->macc.read= at24_macc_read;        writable = !(chip.flags &AT24_FLAG_READONLY);         if(writable) {                   if(!use_smbus || i2c_check_functionality(client->adapter,                                     I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)){                             unsignedwrite_max = chip.page_size;                             at24->macc.write= at24_macc_write;                             at24->bin.write= at24_bin_write;                            at24->bin.attr.mode|= S_IWUSR;                            ……         }……        err = sysfs_create_bin_file(&client->dev.kobj,&at24->bin);         if(err)                   gotoerr_clients;          i2c_set_clientdata(client,at24);         ……}

The main task of the probe function is to create a bin node file under the Sys directory. You can operate the eeprom with this node file and provide the operation method (read, write)

 

5.3 i2c_client implementation

At24c does not depend on the specific CPU and I2C controller hardware features. Therefore, if the circuit board contains this peripherals, you only need to add the corresponding i2c_board_info. The following is the implementation of AT24C08 i2c_client in the Board file:

Static struct at24_platform_data AT24C08 = {. byte_len = sz_8k/8,/* size of the EEPROM, in bytes */. page_size = 16,/* page size byte */}; static struct i2c_board_infomini2440_i2c_devs [] _ initdata = {i2c_board_info ("24c08", 0x50 ), /* 24c08 device name, 0x50 device address */. platform_data = & AT24C08,/* assigned to client-> Dev-> platform_data */},}; static void _ init mini2440_init (void ){...... I2c_register_board_info (0, mini2440_i2c_devs,/* busnum = 0, busnum is the adapter number, used to identify the adapter used by the device */array_size (mini2440_i2c_devs ));......}

The i2c_register_board_info function registers I2C from the hardware feature information of the device to the global linked list _ i2c_board_list. when calling the i2c_add_adapter function, it traverses _ i2c_board_list to obtain the information of the device to construct I.

 

I2c_client Construction

We call the i2c_register_board_info function to register I2C from the hardware feature information of the device to the global linked list _ i2c_board_list. However, we have not constructed an i2c_client struct, nor registered it to the I2C bus. Let's analyze the construction process. When calling the i2c_add_adapter function, it will traverse _ i2c_board_list to obtain the device information and construct i2c_client: i2c_register_adapter ()-> devices ()-> i2c_new_device () -> device_register ().

 

5.4 match between i2c_driver and i2c_client

When i2c_add_driver is called to register i2c_driver and build i2c_client, i2c_device_match ()-> i2c_match_id () registered in I2C bus is called () the function uses i2c_driver-> id_table-> name and client-> name to match

static const struct i2c_device_id*i2c_match_id(const struct i2c_device_id *id,                                                        conststruct i2c_client *client){         while(id->name[0]) {                   if(strcmp(client->name, id->name) == 0)                            returnid;                   id++;         }         returnNULL;}

5.5 Test

The experiment has been successfully performed on mini2440. An EEPROM file is generated under the/sys/bus/I2C/devices/0-0050/directory (50 indicates the address of the slave device, this file is equivalent to the hardware device EEPROM ing. We can operate the eeprom file like a common file, which is essentially a hardware EEPROM operation. Restart the development board and you will find that the modified content of the eeprom file will not change. This proves that the experiment is successful. You must know that the Sys File System cannot store data.

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.