温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么根据设备树文件初始化linux驱动

发布时间:2021-10-22 10:00:06 来源:亿速云 阅读:146 作者:柒染 栏目:互联网科技

这篇文章将为大家详细讲解有关怎么根据设备树文件初始化linux驱动,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

一、前提

新版基于ARM的Linux都会基于Device Tree去代替之前的device驱动。更加多的了解Device Tree可以访问宝哥的Bolg:ARM Linux 3.x的设备树(Device Tree)

这里只是举例在arch/arm/boot/dts中添加dtsi文件并在驱动中读取dtsi中节点信息,并将节点信息写入sys文件系统,至于怎么创建、读写sys文件可以参考: linux sysfs下创建文件

二、举例

1、添加dtsi文件

添加的dtsi文件可以基于你所用的手机或者开发板确定放入什么位置。dts总目录:arch/arm/boot/dts

例中dtsi所放位置位于: 
怎么根据设备树文件初始化linux驱动

xm-test.dtsi:

/ {
	xm-test {
		compatible = "xiaomi,xm-test";
		xm_test_tip;
	};
};

其中xm-test、”xiaomi,xm-test”必须具有唯一性。

注:需要在audio.dtsi文件中添加#include “xm-test.dtsi”具体在哪个文件下添加include根据实际情况而定。

2、驱动

#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/sysfs.h>
#include <linux/slab.h> 
#include <linux/kernel.h>
#include <linux/kobject.h>
#include <linux/string.h>
#include <linux/of.h>

#define HW_TEST "xm_test_tip"
#define i2c_info "xm_test, i2c_show"

static ssize_t show(struct device_driver *driver, char *buf)
{
    if(NULL != buf)
    {
        /* Newline is not appended on purpose, for convenience of reader programs */
        snprintf(buf, PAGE_SIZE, "%s\n", i2c_info);
        return strlen(buf);
    }

    return 0;
}
DRIVER_ATTR(i2c_test, 0444, show, NULL);

static struct attribute *audio_attrs[] = {
    &driver_attr_i2c_test.attr,
    NULL,
};

static struct attribute_group audio_group = {
    .name ="xm_test",
    .attrs = audio_attrs,
}; 

static const struct attribute_group *groups[] = {
    &audio_group,
    NULL,
};

static int xm_test_probe(struct platform_device *pdev)
{
    if(NULL == pdev)
    {
        printk( "xiaomi_test: xm_test_probe failed, pdev is NULL\n");
        return 0;
    }

    if(NULL == pdev->dev.of_node)
    {
        printk( "xiaomi_test: xm_test_probe failed, of_node is NULL\n");
        return 0;
    }
	/* 存在 xm_test_tip 就会在手机/开发板启动的时候打印出来 */
    if(of_property_read_bool(pdev->dev.of_node, HW_TEST))
    {
        printk( "xm_test: %s is existing\n", HW_TEST);
    }

	printk("============== hanshubo ================\n");

    return 0;
}
/* .compatible的信息要与dtsi中的compatible一致 */
static struct of_device_id audio_info_match_table[] = {
    { .compatible = "xiaomi,xm-test",},
    { },
};

static struct platform_driver xm_test = {
	// device_driver
    .driver = {
        /* 这里的name不需要跟dtsi的节点xm-test一致 */
        .name  = "xm-test",
        .of_match_table = audio_info_match_table,
        .owner  = THIS_MODULE,
        .groups = groups,
    },

    .probe = xm_test_probe,
    .remove = NULL,
};

static int __init audio_info_init(void)
{
    return platform_driver_register(&xm_test);
}

static void __exit audio_info_exit(void)
{
    platform_driver_unregister(&xm_test);
}

module_init(audio_info_init);
module_exit(audio_info_exit);
MODULE_LICENSE("GPL");

3、检验sys文件系统中的是否写入成功

在sys/bus/platform/drivers/xm_test中会找到文件i2c_test文件:

# cat i2c_test
# xm_test, i2c_show

注:当使用设备树注册设备时,设备节点名称“xm-test”不必和platform_driver.driver.name 保持一致。

也就是说:总线不会通过此两项,将设备和驱动进行匹配

关于怎么根据设备树文件初始化linux驱动就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI