H3 Linux4.11内核设备树设备驱动开发2

版权声明:本文为博主原创文章,未经博主允许转载。 https://blog.csdn.net/jklinux/article/details/81116986

参考:
linux内核device-tree基础

在设备树里描述platform_device


在设备树里描述一个mydt的设备,此设备有多种属性及两个子节点,每个子节点也有多种属性.

修改设备树的文本文件: arch/arm/boot/dts/sun8i-h3-orangepi-lite.dts

 51 / {
 52     model = "Xunlong Orange Pi Lite";
 53     compatible = "xunlong,orangepi-lite", "allwinner,sun8i-h3";
 54 
 55     mydt@11223344{
 56         compatible = "mydt,test";
 57         hello = "hello", "world";
 58         what = "shift";
 59         hehe = <88>, <99>;
 60         haha = <33>;
 61         mymac = [11 22 33 44 55 66];
 62 
 63         dt1 {
 64             hello = "hello", "dt1";
 65             what = "what dt1";
 66             hehe = <22>, <88>;
 67             haha = <99>;
 68             mymac = [11 22 33 44 55 66];
 69         };
 70         dt2 {
 71             hello = "hello", "dt2";
 72             what = "what dt2";
 73             hehe = <11>, <44>;
 74             haha = <55>;
 75             mymac = [11 22 33 44 55 66];
 76         };
 77     };

修改完成后,重编译并更新使用设备树文件:

make dtbs ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-
编译完成后: arch/arm/boot/dts/sun8i-h3-orangepi-lite.dtb就是所需的设备树文件。

使用新的设备树文件启动系统后,可查看到:

/ # ls /sys/bus/platform/devices/mydt@11223344/
driver/          modalias         power/           uevent
driver_override  of_node/         subsystem/
/ # ls /sys/bus/platform/devices/mydt@11223344/of_node/
compatible  dt2/        hehe        mymac       what
dt1/        haha        hello       name
/ # ls /sys/bus/platform/devices/mydt@11223344/of_node/dt1/
haha   hehe   hello  mymac  name   what
/ # ls /sys/bus/platform/devices/mydt@11223344/of_node/dt1/


编写一个与mydt平台设备匹配的平台驱动对象,并获取设备的属性及它的子节点属性值.


#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/property.h>

int myprobe(struct platform_device *pdev)
{
    struct fwnode_handle *child;    
    const char *p, *p2[2];
    int  n, n2[2];
    char mymac[6];

    //取属性what的值
    if (device_property_read_string(&pdev->dev, "what", &p) < 0)
        return -ENODEV;

    //取属性hello的字符串数组值
    if (device_property_read_string_array(&pdev->dev, "hello", p2, 2) < 0)
        return -ENODEV;

    //取属性haha的整型值
    if (device_property_read_u32_array(&pdev->dev, "haha", &n, 1) < 0)
        return -ENODEV; 

    //取属性hehe的整型数组  
    if (device_property_read_u32_array(&pdev->dev, "hehe", n2, 2) < 0)
        return -ENODEV;

    //取属性mymac的字节数组
    if (device_property_read_u8_array(&pdev->dev, "mymac", mymac, 6) < 0)
        return -ENODEV;


    printk(KERN_ERR"in myprobe ...%s\n", p);
    printk(KERN_ERR"%s,%s\n", p2[0], p2[1]);
    printk(KERN_ERR"n = %d\n", n);
    printk(KERN_ERR"n2: %d, %d\n", n2[0], n2[1]);
    printk(KERN_ERR"mymac=%x,%x,%x,%x,%x,%x\n", mymac[0], mymac[1], mymac[2], mymac[3],
                mymac[4], mymac[5]);    
    printk(KERN_ERR"######################################\n");
///////////////////////////////////////////////////////////////////
//获取设备子节点的属性值


    //循环获取每个子节点的属性值
    device_for_each_child_node(&pdev->dev, child)
    {
        //取子节点的属性what的值
        if (0 <= fwnode_property_read_string(child, "what", &p))
            printk(KERN_ERR"child what=%s\n", p);

        //取子节点的属性hello的字符串数组值
        if (0 <= fwnode_property_read_string_array(child, "hello", p2, 2))
            printk(KERN_ERR"child hello=%s, %s\n", p2[0], p2[1]);

        //取子节点的属性haha的整型值
        if (0 <= fwnode_property_read_u32_array(child, "haha", &n, 1))
            printk(KERN_ERR"child haha=%d\n", n);

        //取子节点的属性hehe的整型数组  
        if (0 <= fwnode_property_read_u32_array(child, "hehe", n2, 2))
            printk(KERN_ERR"child hehe=%d,%d\n", n2[0], n2[1]);

        //取子节点属性mymac的字节数组
        if (0 <= fwnode_property_read_u8_array(child, "mymac", mymac, 6))
            printk(KERN_ERR"child mymac=%x,%x,%x,%x,%x,%x\n", mymac[0], mymac[1], mymac[2], mymac[3],
                mymac[4], mymac[5]);    
        printk(KERN_ERR"---------------------\n");
    }

    return 0;
}

int myremove(struct platform_device *pdev)
{
    printk(KERN_ERR"in myremove ...\n");
    return 0;
}


struct of_device_id ids[] = {
    {.compatible = "mydt,test"},
    {},
};

struct platform_driver pdrv = {
    .driver = {
        .name = "mydrv",
        .owner = THIS_MODULE,
        .of_match_table = ids,
    },

    .probe = myprobe,
    .remove = myremove,
};

module_platform_driver(pdrv);
MODULE_LICENSE("GPL");

执行结果:

/mnt/kernel_coding/30device_tree/01mydrv # insmod test.ko 
[ 1930.702739] in myprobe ...shift
[ 1930.705891] hello,world
[ 1930.708362] n = 33
[ 1930.710372] n2: 88, 99
[ 1930.712728] mymac=11,22,33,44,55,66
[ 1930.716210] ######################################
[ 1930.721005] child what=what dt1
[ 1930.724142] child hello=hello, dt1
[ 1930.727553] child haha=99
[ 1930.730170] child hehe=22,88
[ 1930.733048] child mymac=11,22,33,44,55,66
[ 1930.737057] ---------------------
[ 1930.740367] child what=what dt2
[ 1930.743503] child hello=hello, dt2
[ 1930.746898] child haha=55
[ 1930.749521] child hehe=11,44
[ 1930.752398] child mymac=11,22,33,44,55,66
[ 1930.756399] ---------------------

猜你喜欢

转载自blog.csdn.net/jklinux/article/details/81116986
h3