spi_driver.id_table 和 spi_driver.driver.of_match_table 的区别

Joh*_*ang 4 linux linux-device-driver linux-kernel device-tree

我目前正在尝试了解 linux 驱动程序是如何工作的。据我所知,当内核解析设备树中相应的 .compatible 字符串时,会调用驱动程序的probe/init 函数。但是,在 arizona-spi 驱动程序中,似乎在不同的成员中引用了多个兼容的字符串:

static const struct spi_device_id arizona_spi_ids[] = {
{ "wm5102", WM5102 },
{ "wm5110", WM5110 },
{ },
};
MODULE_DEVICE_TABLE(spi, arizona_spi_ids);

static struct spi_driver arizona_spi_driver = {
.driver = {
    .name   = "arizona",
    .owner  = THIS_MODULE,
    .pm = &arizona_pm_ops,

    // Contains e.g. "wlf,wm5102"
    .of_match_table = of_match_ptr(arizona_of_match),

},
.probe      = arizona_spi_probe,
.remove     = arizona_spi_remove,
.id_table   = arizona_spi_ids,                  // Contains "wm5102" and "wm5110"
};
Run Code Online (Sandbox Code Playgroud)

这是这里的摘录。

那么 arizona_spi_driver.id_table 和 arizona_spi_driver.driver.of_match_table 有什么区别呢?

sam*_*msi 5

驱动程序匹配有多种机制。id_table 旨在用于从剥离的设备树条目(没有供应商部分)中查找匹配项,而 of_match_table 用于从完整设备树条目(具有供应商部分的设备树条目)中查找匹配项。

如果您检查, arizona_of_match 定义如下:

const struct of_device_id arizona_of_match[] = {
    { .compatible = "wlf,wm5102", .data = (void *)WM5102 },
    { .compatible = "wlf,wm5110", .data = (void *)WM5110 },
    { .compatible = "wlf,wm8280", .data = (void *)WM8280 },
    { .compatible = "wlf,wm8997", .data = (void *)WM8997 },
    {},
};
Run Code Online (Sandbox Code Playgroud)

wlf是这种情况下的供应商部分,而 arizona_spi_ids 不包含供应商部分。

因此,如果您的设备树中有这样的东西:

兼容=“我的供应商,wm5102”

您的设备将与 id_table 匹配,不会与 of_match_table匹配,因为供应商不同。

内核将做配套反对of_match_table第一前检查id_table(见spi_get_device_id这里)。设备匹配优先级为:of_match_table > acpi_driver > id_table。