Linux System I2C Device driver writing method __linux

Source: Internet
Author: User
Tags data structures imx6

Hardware platform: Freescale IMX6

Kernel version: kernel3.0.35


Linux's I2C subsystem is divided into three layers, I2C core layer, I2C bus driver layer and I2C device drive layer. I2C core layer is provided by kernel developers, I2C bus driver layer is provided by chip manufacturer, and I2C device driver layer is only specific development requirement because of the difference of equipment. And this example is to take the EEPROM chip AT24C02 to carry on the concrete analysis.


I2C core layer manages all data structures on I2C, provides I2C bus driver layer and I2C device driver layer operation interface function, I2C bus driver layer provides I2C core layer access to hardware device specific methods, The I2C device-driven layer adds new data (bus device nodes) to the I2C core layer and provides a user-layer access interface.

Note: I2C core layer has actually provided the user access interface function, each I2C bus corresponds to a device,/dev/i2c-x (0,1,2 ...), generally do not choose to use, but to write their own specific device drivers


This document is only for the I2C device-driven writing method description, while the I2C core layer to achieve visible driver/i2c/i2c-core.c has a specific implementation, I2C bus driver layer specific platform specific analysis, such as IMX6 platform for drivers\i2c\busses/ I2c-imx.c


Here's the start: I2C device driver writing is divided into two parts

First, add I2c_board_info to the platform file board-mx6q_sabresd.c and register it with the I2C core layer

Second, the preparation of I2C device driver


1 adding I2c_board_info to the platform file board-mx6q_sabresd.c and registering it with the I2C core layer

Steps:

A, initializing the I2c_board_info type global variable

Assignment Initialization I2c_board_info
static struct I2c_board_info at24c02[] = {
{
I2c_board_info ("At24c02", 0x50)//"AT24C02" to match
}
};

b, register in the Platform device initialization function (IMX6 Platform for Mx6_sabresd_board_init) i2c_board_info

Registering hardware information for AT24C02
I2c_register_board_info (0,AT24C02, Array_size (AT24C02))//Parameter description, the first argument is BusNum, the second argument is struct i2c_board_info const * Info, that is, the array of device sets hooked up on the bus, the third parameter, the number of array elements


2 Writing I2C device driver

Steps:

A, initialize the i2c_driver variable

static struct i2c_device_id at24c02_id[] = {
{"At24c02", 0},
"AT24C02" must be consistent with the type of i2c_board_info, which is matched by
};

static struct I2c_driver At24c02_drv = {
. Driver = {
. Name = "Tarena"//unimportant, match does not depend on it
},
. Probe = At24c02_probe,//Match executed successfully, I2C-CORE.C probe function will back up this function to perform
. remove = At24c02_remove,
. id_table = at24c02_id
};

b, Register i2c_driver variable

I2c_add_driver (&AT24C02_DRV);

C, get the i2c_client variable in the At24c02_probe function, and register the hybrid device for subsequent data transfer

static struct i2c_client *g_client;

static int At24c02_probe (
struct I2c_client *client,
struct i2c_device_id *id)
{
1. Registration Hybrid Device Driver
Misc_register (&at24c02_dev);
2. Record matching successful i2c_client
g_client = client;
return 0; Successfully returns 0, failure returns negative
}

D, write a hybrid device operation function

static struct Miscdevice At24c02_dev = {
. minor = Misc_dynamic_minor,//automatic allocation of secondary device number
. Name = "At24c02",//DEV/AT24C02
. FoPs = &at24c02_fops
};

static struct File_operations At24c02_fops = {
. Owner = This_module,
. Unlocked_ioctl = At24c02_ioctl
};

E, implement At24c02_ioctl function

The IOCTL function realizes the data transmission interface, uses the SMBus interface to throw the data (address, data, device address (G_CLIENT->ADDR)) to the I2C bus driver, initiates the hardware transmission of the I2C bus.

Specific methods:

(1) Open SMBus Document: Kernel source \documentation\i2c\smbus-protocol, find the corresponding SMBus interface function

(2) Open the chip operation sequence diagram, according to the sequence diagram to find the corresponding SMBus operation function

(3) The matching successful i2c_client in the Addr,data and At24c02_probe functions are dropped to the I2C bus driver by the corresponding function and then the hardware transmission of the I2C bus is initiated.

For example, the reading and writing functions of AT24C02 were

I2c_smbus_read_byte_data (g_client, addr);

I2c_smbus_write_byte_data (g_client, addr, data);


The specific code can be seen in the following address click Open link









Related Article

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.