ILD

Platform devices and device trees
作者:YUAN JIANPENG 邮箱:yuanjp@hust.edu.cn
发布时间:2018-7-22 站点:Inside Linux Development

前一篇文章,描述了内核处理不可发现设备的机制:platform devices。platform device scheme有一个很长的历史,而且使用广泛,但是它有一个缺点,必须在代码中实例化这些设备。device tree机制是后续更好的解决方法。


device tree中的设备节点,有一个compatible属性,其值是设备的名字,也用来匹配platform driver。


内核提供一个 of_device_id 结构体:

1
2
3
4
    static const struct of_device_id my_of_ids[] = {
    { .compatible = "long,funky-device-tree-name" },
    { }
    };


初始化platform_driver时,设置的driver成员的of_match_table

1
2
3
4
5
6
7
    static struct platform_driver my_driver = {
    /* ... */
    .driver = {
        .name = "my-driver",
        .of_match_table = my_of_ids
   }
    };


使用下述宏,可以让新的device时,自动加载驱动,并实例化device

1
 MODULE_DEVICE_TABLE(of, my_of_ids);


drivers期待platform data的应该检查dev.platform_data,如果其为非空,则是传统的方式实例化,没有使用device tree。


如果是device tree实例化,则platform_data为空,而dev.of_node包含了设备树中的属性,各种接口可以使用 (of_get_property)来获取配置信息。

Copyright © linuxdev.cc 2017-2024. Some Rights Reserved.