Linux driver subsystem I2C (4)

Source: Internet
Author: User
4 bus driver

4.1 Overview

The I2C bus driver is a software implementation of the I2C Adapter. It provides the ability to complete data communication between the I2C adapter and the slave device, such as the start, stop, response signal, and master_xfer implementation functions.

The I2C bus driver is described by i2c_adapter and i2c_algorithm.

 

4.2 Hardware Description of s3c2440i2c Controller

The S3C2440 processor is integrated with an I2C controller, which is controlled by four registers:

Iiccon I2C control register

Iicstat I2C Status Register

Iicds I2C data transmission and receiving shift register

Iicadd I2C Address Register

Through the iiccon, iicds, and iicadd registers, the start bit, stop bit, data, and address can be generated on the I2C bus, and the transmission status can be obtained through the iicstat register.

 

4.3 i2c-s3c2410 bus driver analysis (platform_driver)

I2C bus driver code in drivers/I2C/busses/i2c-s3c2410.c, this code also supports Samsung series such as S3C2410, s3c246410, s5pc110 chip.

 

Initialize and uninstall modules

static int __init i2c_adap_s3c_init(void){         returnplatform_driver_register(&s3c24xx_i2c_driver);} static void __exit i2c_adap_s3c_exit(void){         platform_driver_unregister(&s3c24xx_i2c_driver);}

Bus drivers are implemented based on platform and comply with the idea of the device driver model.

static struct platform_drivers3c24xx_i2c_driver = {         .probe                = s3c24xx_i2c_probe,         .remove            = s3c24xx_i2c_remove,         .id_table  = s3c24xx_driver_ids,         .driver                = {                   .owner     = THIS_MODULE,                   .name       = "s3c-i2c",                   .pm  = S3C24XX_DEV_PM_OPS,                   .of_match_table= s3c24xx_i2c_match,         },};

S3c24xx_i2c_probe Function

When the platform_driver_register function is called to register the platform_driver struct, if the platformdevice and platform driver match successfully, the probe function is called to initialize the adapter hardware.

Static int s3c24xx_i2c_probe (structplatform_device * pdev ){...... /* Initialize the adapter information */strlcpy (I2C-> ADAP. name, "s3c2410-i2c", sizeof (I2C-> ADAP. name); I2C-> ADAP. owner = this_module; I2C-> ADAP. algo = & s3c24xx_i2c_algorithm; I2C-> ADAP. retries = 2; I2C-> ADAP. class = i2c_class_hwmon | i2c_class_spd; I2C-> tx_setup = 50;/* initialize the spin lock and wait queue header */spin_lock_init (& I2C-> lock ); init_waitqueue_head (& I2C-> wait);/* ing register */RES = platform_get_resource (pdev, ioresource_mem, 0); I2C-> ioarea = Re Quest_mem_region (res-> Start, resource_size (RES), pdev-> name); I2C-> regs = ioremap (res-> Start, resource_size (RES )); /* set the information required by the I2C core */I2C-> ADAP. algo_data = I2C; I2C-> ADAP. dev. parent = & pdev-> dev;/* initialize I2C controller */ret = s3c24xx_i2c_init (I2C);/* request interrupt */I2C-> IRQ = ret = platform_get_irq (pdev, 0); ret = request_irq (I2C-> IRQ, s3c24xx_i2c_irq, 0, dev_name (& pdev-> Dev), I2C);/* Register I2C adapter */ret = i2c_add_numbered _ Adapter (& I2C-> ADAP );......}

Probe is mainly used to handle hardware and request the I/o address and interrupt number used by the I2C adapter, and then add the adapter to the I2C core. I2c_adapter registration process i2c_add_numbered_adapter-> i2c_register_adapter

 

I2C bus Communication Method

static const struct i2c_algorithms3c24xx_i2c_algorithm = {         .master_xfer             = s3c24xx_i2c_xfer,         .functionality             = s3c24xx_i2c_func,};

The s3c24xx_i2c_xfer function is a specific implementation of the bus communication mode. It depends on the s3c24xx_i2c_doxfer and s3c24xx_i2c_message_start functions;

static int s3c24xx_i2c_doxfer(structs3c24xx_i2c *i2c,                                  struct i2c_msg *msgs, int num){         ret =s3c24xx_i2c_set_master(i2c);          i2c->msg     = msgs;         i2c->msg_num= num;         i2c->msg_ptr= 0;         i2c->msg_idx= 0;         i2c->state   = STATE_START;          s3c24xx_i2c_message_start(i2c,msgs);}

First, set the s3c I2C device as the master device, and then call the s3c24xx_i2c_message_start function to start I2C message transmission.

The s3c24xx_i2c_func function returns the communication functions supported by the adapter.

 

4.4 device resource of the adapter (platform_device)

The I2C bus driver of S3C2440 is implemented based on platform. We have analyzed the platformdriver section before. Let's take a look at the platform device section.

The platform_device struct and I2C controller resource information are defined in the arch/ARM/Plat-Samsung/dev-i2c0.c file:

Static struct resource initi_i2c_resource [] = {[0] = {. start = maid ,. end = maid + sz_4k-1 ,. flags = ioresource_mem,}, [1] = {. start = irq_iic ,. end = irq_iic ,. flags = ioresource_irq,},}; struct platform_device initi_device_i2c0 = {. name = "s3c2410-i2c",/* device name */# ifdef config_initi_dev_i2c1. id = 0, # Else. id =-1, # endif. num_resources = array_size (cloud_i2c_resource ),. resource = cloud_i2c_resou RCE,}; struct s3c2410_platform_i2cdefault_i2c_data _ initdata = {. flags = 0 ,. slave_addr = 0x10,/* I2C adapter address */. frequency = 100*1000,/* bus frequency */. sda_delay = 100,/* SDA edge Delay Time NS */}; void _ init jx_i2c0_set_platdata (structs3c2410_platform_i2c * PD) {structs3c2410_platform_i2c * NPD; If (! PD) Pd = & default_i2c_data; NPD = initi_set_platdata (PD, sizeof (struct s3c2410_platform_i2c), & initiate_device_i2c0); If (! NPD-> pai_gpio) NPD-> pai_gpio = maid ;}

Register platform_device into the kernel in the Board file:

static struct platform_device*mini2440_devices[] __initdata = {         ……         &s3c_device_i2c0,……};

Call the initial_i2c0_set_platdata function to assign the specific data of the adapter to Dev. platform_data:

static void __init mini2440_init(void){         ……s3c_i2c0_set_platdata(NULL);}

The I2C bus driver is analyzed here.

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.