設置 | 登錄 | 註冊

目前共有3篇帖子。

Luckfox RV1103开发板修改设备树的方法

5樓 巨大八爪鱼 2025-3-14 11:05

内核模块test.c的代码:

#include <linux/gpio/consumer.h> // gpiod_set_value
#include <linux/module.h> // MODULE_AUTHOR, MODULE_LICENSE
#include <linux/of.h> // of_match_ptr
#include <linux/platform_device.h> // module_platform_driver

static int mytestdriver_probe(struct platform_device *pdev)
{
    const char *str;
    int ret;
    struct gpio_desc *gpio;
   
    dev_err(&pdev->dev, "probe");
    gpio = devm_gpiod_get(&pdev->dev, NULL, GPIOD_OUT_HIGH); // led on
    if (IS_ERR(gpio))
    {
        dev_err(&pdev->dev, "gpio error\n");
        return -1;
    }
    platform_set_drvdata(pdev, gpio);
   
    ret = of_property_read_string(pdev->dev.of_node, "xxxyyy", &str);
    if (ret == 0)
        dev_err(&pdev->dev, "str=%s\n", str);
    return 0;
}

static int mytestdriver_remove(struct platform_device *pdev)
{
    struct gpio_desc *gpio;
   
    dev_err(&pdev->dev, "remove");
    gpio = platform_get_drvdata(pdev);
    if (gpio != NULL)
    {
        gpiod_set_value(gpio, 0); // led off
        platform_set_drvdata(pdev, NULL);
    }
    return 0;
}

static const struct of_device_id mytestdriver_match_ids[] = {
    {.compatible = "mytest,myboard_20250314"},
    {}
};

static struct platform_driver mytestdriver = {
    .driver = {
        .name = "mytestdriver",
        .owner = THIS_MODULE,
        .of_match_table = of_match_ptr(mytestdriver_match_ids)
    },
    .probe = mytestdriver_probe,
    .remove = mytestdriver_remove
};

module_platform_driver(mytestdriver);

MODULE_AUTHOR("Oct1158");
MODULE_LICENSE("GPL");

巨大八爪鱼 2025-3-14 11:17
用devm_函数获取的资源,会在设备卸载时自动释放,不需要在remove函数里面手动释放。
巨大八爪鱼 2025-3-14 11:21
of_property_read_string的第三个参数返回的字符串可以直接使用,用完不需要释放。
文档上写的:Search for a property in a device tree node and retrieve a null terminated string value (pointer to data, not a copy).

內容轉換:

回覆帖子
內容:
用戶名: 您目前是匿名發表。
驗證碼:
看不清?換一張
©2010-2025 Purasbar Ver3.0 [手機版] [桌面版]
除非另有聲明,本站採用知識共享署名-相同方式共享 3.0 Unported許可協議進行許可。