c - 设备树在Linux(树莓派)启动时绑定(bind)所需的设备驱动程序

标签 c linux kernel driver

我已经编写了一个平台驱动程序(虚拟驱动程序),并且想知道如何使用设备树或“在哪里添加我的设备节点?”在设备树中,以便我的驱动程序在启动时自动加载并自动调用 prob() 。并且,
我不知道设备树在哪个目录中找到要绑定(bind)的特定驱动程序?
谢谢期待!!

最佳答案

为此,您需要使用包含:

#include <linux/of.h>       
#include <linux/of_device.h>

在您的设备声明中(请注意,我使用 <you thing here> 来更改您的设备名称):

#ifdef CONFIG_OF
static const struct of_device_id <your_device_name_here>_dt_match_table[] = {
    { .compatible = "<your compatible string here>" },
    { },
};
MODULE_DEVICE_TABLE(of, <your_device_name_here>_dt_match_table);
#endif

static struct platform_driver <your_device_name_here>_driver = {
    .probe = <your_device_name_here>_probe,
    .remove = <your_device_name_here>_remove,
    .driver = {
        .name = "<your_device_name_here>",
        .of_match_table = of_match_ptr(clap_sensor_dt_match_table),
    },
};
module_platform_driver(<your_device_name_here>_driver);

在您的驱动程序中使用此内容,您可以在设备树中编写以下内容:

<your_device_name_here> {
        compatible = "<your compatible string here>";
        pinctrl-names = "default";
        pinctrl-0 = <&your_pins_pinctrl>;
        label = "trigger";
        trigger-gpios = <&gpio 23 GPIO_ACTIVE_LOW>;
};

设备树文件和您的设备驱动程序文件上的 Compatible 属性必须相同,这样内核才能将设备树中其节点的信息与您的驱动程序进行匹配。

记住设备树仅用于描述硬件,不要用它来存储软件变量或其他软件内容。

我建议你学习一些有关设备树pinctrl和gpio的知识。 Raspberry Foundation 有一些用于设备树覆盖的 Material : https://www.raspberrypi.org/documentation/configuration/device-tree.md

关于c - 设备树在Linux(树莓派)启动时绑定(bind)所需的设备驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50790670/

相关文章:

c++ - qemu-系统-i386 : Error loading uncompressed kernel without PVH ELF Note

c - 将指针的值分配给二维数组

c - 在用 C 编写 GUI 应用程序时需要帮助

c# - 为什么我们不能在 C# 中执行 'if (object)' 来测试对象是否为空?

linux - 如何编写循环遍历相似字符以执行函数的脚本?

c - `close(fd)` 是否破坏文件表条目和/或 vnode 表条目?

c - 如何使用函数在 header 中声明并在源代码中定义的结构

swift - 生成错误 : missingLinuxMain

mysql - 使用外部文件选择性地加载数据 INFILE

为用户空间和内核空间编译相同的 C 代码