Linux使用设备树的i2c驱动与设备匹配有3种方式:
源码:
static int i2c_device_match(struct device *dev, struct device_driver *drv)
{
struct i2c_client *client = i2c_verify_client(dev);
struct i2c_driver *driver;
if (!client)
return 0;
/* Attempt an OF style match */
if (of_driver_match_device(dev, drv))
return 1;
/* Then ACPI style match */
if (acpi_driver_match_device(dev, drv))
return 1;
driver = to_i2c_driver(drv);
/* match on an id table if there is one */
if (driver->id_table)
return i2c_match_id(driver->id_table, client) != NULL;
return 0;
}
一、of_driver_match_device
这种方式是所有驱动匹配通用的,使用of_device_id 的compatible部分字符
struct of_device_id
{
char name[32];
char type[32];
char compatible[128];
const void *data;
};
dts :
i2c0: i2c@01c2ac00 { /* 控制器的设备节点 */
...
mydht12 { /* 增加的设备子节点 */
compatible = "mydht12";
reg = <0x5c>; /* 设备地址 */
};
};
驱动 :
struct of_device_id ids[] = {
{.compatible = "mydht12"},
{},
};
struct i2c_driver mydrv = {
.probe_new = probe,
.remove = remove,
.driver = {
.owner = THIS_MODULE,
.name = "mydrv",
.of_match_table = ids,
},
};
二、acpi_driver_match_device
主要用于电源管理,很少用到,这里不做介绍。
三、i2c_match_id
I2C设备特有匹配方式,使用i2c_device_id :
struct i2c_device_id {
char name[I2C_NAME_SIZE];
kernel_ulong_t driver_data; /* Data private to the driver */
};
struct i2c_device_id ids2[] = {
{"mydht12"},
{},
};
struct i2c_driver mydrv = {
.probe_new = probe,
.remove = remove,
.driver = {
.owner = THIS_MODULE,
.name = "mydrv",
// .of_match_table = ids,
},
.id_table = ids2, //使用id_table匹配
};