轉自:
https://www.it610.com/article/1282936299518443520.htm
Linux使用設備樹的i2c驅動與設備匹配有3種方式:
- of_driver_match_device
- acpi_driver_match_device
- i2c_match_id
源碼:
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匹配
};