Simply put, I2C drive is also three steps, register I2C device, register I2C device driver, create SYSFS file for Upper call.
1. Register I2C equipment.
First define a I2c_board_info
static struct I2c_board_info __initdata xxxx_i2c_info[] = {
i2c_board_info ("xxxx", 0x1d),
. platform_ data = &xxxx_platform_data,
},
Re-registration, will generally register a client.
I2c_register_board_info ()
Strongly suggest interested can look at this function of the source code, can learn a lot.
2. Register I2C Equipment Driver
Define re-registration
static struct I2c_driver Xxx_i2c_driver = {
. id_table = xxx_i2c_id,
. Probe = Xxx_i2c_probe,
. remove = __ Exit_p (Xxx_i2c_remove),
. Driver = {
. name = "xxx",
},
};
Registration function
I2c_add_driver (&xxx_i2c_driver);
In the registration process, the main call probe function, how to run to the probe function also need to follow the code. Not in detail here. Talk about the main probe function.
static int xxx_i2c_probe (struct i2c_client *client,
const struct I2C_DEVICE_ID *id)
{
int rc = 0;
PRINTK ("%s called!\n", __func__);
if (!i2c_check_functionality (Client->adapter, I2C_FUNC_I2C)) {
printk ("i2c_check_functionality failed\n");
Goto probe_failure;
}
XXX_SENSORW = kzalloc (sizeof (struct xxx_work_t), gfp_kernel);
if (!XXX_SENSORW) {
printk ("Kzalloc failed.\n");
rc =-enomem;
Goto probe_failure;
}
I2c_set_clientdata (client, XXX_SENSORW);
Xxx_init_client (client);
xxx_client = client;
Msleep (m);
PRINTK ("%s successed! rc =%d\n ", __func__, RC);
return 0;
Probe_failure:
printk ("%s failed! rc =%d\n ", __func__, RC);
return RC;
}
The probe function is primarily to obtain the client and then transmit the data by means of a adapter in the client:
static int Xxx_i2c_txdata (const uint16_t saddr,
uint8_t *txdata, const uint16_t length)
{
struct i2c_msg msg [] = {
. addr = saddr,
. Flags = 0,
. Len = length,
. buf = TxData,
},
};
if (I2c_transfer (Xxx_client->adapter, MSG, 1) < 0) {
printk ("Xxx_i2c_txdata faild 0x%x\n", saddr);
Return-eio;
}
return 0;
}
3. Create SYSFS for Upper call
You can pass the function
Sysfs_create_group (& (Pdev->dev.kobj), &xxx_attribute_group);
To create.
The above is briefly said, mainly to see the source code can be a deeper understanding of these processes.