After understanding the datasheet, don't rush to write code, you should first do is to give you will write the driver design a framework.
So what should the framework be based on? Concrete how to build it.
General, from USB drive to I2C Drive, from SPI Drive to serial drive, from PCI driver to DMA driver, and so on, no matter what type of driver, it always has one or several basic fixed routines for you to choose from. If you're going to write a touch drive and this touch hangs on the I2C, then you build the framework based on the one by one kinds of routines written by I2C devices. Concrete how to build, here gives the I2C device driver to write a fixed routine to everyone.
The new method of I2C device-driven writing is the probe method, where the framework of the I2C device-driven probe method is built.
L Create a I2c_driver
static struct I2c_driver Xxxx_driver = {
. Driver = {
. Name= "XXXX",
},
. Probe = Xxxx_probe,/* Call when there is a i2c_client and I2c_driver match
. remove = Xxxx_remove,
. id_table = xxxx_id,/* matching rule * *
};
L Register I2c_driver
static int __init xxxx_init (void)
{
Return I2c_add_driver (&xxxx_driver);
}
Module_init (Xxxx_init);
Registering the I2c_driver process is actually registering the driver on the I2c_bus_type bus. The matching rule for this bus is:
static const struct I2C_DEVICE_ID *i2c_match_id (const struct I2C_DEVICE_ID *id,
const struct I2c_client *client)
{
while (Id->name[0]) {
if (strcmp (client->name, id->name) = = 0)
return ID;
id++;
}
return NULL;
}
The code above actually uses the name of the i2c_client to match the name in the id_table. The id_table here are:
static const struct I2C_DEVICE_ID xxxx_id[] = {
{"XXXX346", 8,},
{"XXXX353", 16,},
{"XXXX388", 8,},
{"XXXX399", 16,},
{"XXXX542", 8,},
{"XXXX551", 16,},
{"XXXX572", 8,},
{ }
};
L Register I2c_board_info
For the probe method, you must complete the registration of I2c_board_info in the platform code. The approach is as follows:
static struct I2c_board_info __initdata tmp_i2c_devices[] = {
{
I2c_board_info ("XXXX9555", 0x27),//xxxx9555 for chip name, 0X27 for chip address
. Platform_data = &xxxx9555_data,
},
{
I2c_board_info ("mt9v022", 0x48),
. Platform_data = &iclink[0],/* with extender * *
},
{