RK3399 设备树获取子结点信息

驱动测试代码:

#include <linux/kernel.h>
#include <linux/gpio.h>

#include <linux/init.h>
#include <linux/module.h> 
#include <linux/delay.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/of_platform.h>


#include <linux/version.h>

#include <linux/init.h>
#include <linux/fs.h>
#include <linux/sched.h>
#include <linux/pm.h>
#include <linux/sysctl.h>
#include <linux/proc_fs.h>
//#include <linux/platform_device.h>
#include <asm/uaccess.h>
#include <asm/io.h>

#define GPIO_LOW 0
#define GPIO_HIGH 1
int gpio;
int major;
static struct class *cls;
static int led_open(struct inode *inode, struct file *file)
{
    printk(KERN_EMERG "%s-%d: enter\n",__FUNCTION__,__LINE__);
    
    return 0;    
}

static ssize_t led_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
    int val;
    int ret;
    printk(KERN_EMERG "%s-%d: enter\n",__FUNCTION__,__LINE__);

    ret = copy_from_user(&val, buf, count); //    copy_to_user();

    if (val == 1)
    {
        gpio_set_value(gpio,GPIO_LOW);
    }
    else
    {
        gpio_set_value(gpio,GPIO_HIGH);
    }
    
    return 0;
}
static struct file_operations led_fops = {
    .owner  =   THIS_MODULE,   
    .open   =   led_open,     
    .write    =    led_write,       
};

static int led_probe(struct platform_device *pdev)
{
    int ret ;
    int i;

    enum of_gpio_flags flag;
    struct device_node *led_node = pdev->dev.of_node;
    struct device_node *child;
    
    
    printk(KERN_EMERG "%s-%d: enter\n",__FUNCTION__,__LINE__);

    
    for_each_child_of_node(led_node, child)
    {
        printk(KERN_ERR"child->name=%s,child->full_name=%s,%p\n",child->name,child->full_name,child);
        
        gpio = of_get_named_gpio_flags(child,"gpios", 0,&flag);
        if (!gpio_is_valid(gpio)){
            printk(KERN_INFO "hello: invalid gpio : %d\n",gpio);
            return -1;
        } 
        ret = gpio_request(gpio, "led");
        if (ret) {
            gpio_free(gpio);
            return -EIO;
        }
        gpio_direction_output(gpio, GPIO_HIGH);
        gpio_set_value(gpio,GPIO_LOW);
    }

    for(i=0; i < 10; i++)
    {
        gpio_set_value(gpio,GPIO_LOW);
        mdelay(500);
        gpio_set_value(gpio,GPIO_HIGH);
        mdelay(500);
    }
    
    
    major = register_chrdev(0, "myled", &led_fops);

    cls = class_create(THIS_MODULE, "myled");

    device_create(cls, NULL, MKDEV(major, 0), NULL, "led"); 
    
    
    gpio_set_value(gpio,GPIO_LOW);
        
    printk(KERN_INFO "%s-%d: exit\n",__FUNCTION__,__LINE__);
    
    
    
    return 0;  //return Ok
}


static int led_remove(struct platform_device *pdev)

    printk(KERN_INFO "Enter %s\n", __FUNCTION__);
    gpio_free(gpio);
    device_destroy(cls, MKDEV(major, 0));
    class_destroy(cls);
    unregister_chrdev(major, "myled");
    return 0;
}
static const struct of_device_id of_led_match[] = {
    { .compatible = "gpio-leds" },
    { /* Sentinel */ }
};
static struct platform_driver led_driver = {
    .probe        = led_probe,
    .remove        = led_remove,
    .driver        = {
        .name    = "led",
        .owner    = THIS_MODULE,
        .of_match_table    = of_led_match,
    },

};

static int __init led_init(void)
{
    printk(KERN_INFO "Enter %s\n", __FUNCTION__);
    return platform_driver_register(&led_driver);
    return 0;
}

static void __exit led_exit(void)
{
    platform_driver_unregister(&led_driver);
    printk(KERN_INFO "Exit Hello world\n");
}
module_init(led_init);
module_exit(led_exit);


MODULE_LICENSE("GPL");

设备树文件信息:

leds {
                compatible = "gpio-leds";

                led@1 {
                        pinctrl-names = "default";
                        pinctrl-0 = <&led1_ctl>;
                        label = "led1";
                        gpios = <&gpio1 1 GPIO_ACTIVE_LOW>;
                        linux,default-trigger = "heartbeat";
                        default-state = "off";
                };

                led@2 {
                        pinctrl-names = "default";
                        pinctrl-0 = <&led2_ctl>;
                        label = "led2";
                        gpios = <&gpio0 8 GPIO_ACTIVE_LOW>;
                        default-state = "off";
                };

                led@3 {
                        pinctrl-names = "default";
                        pinctrl-0 = <&led3_ctl>;
                        label = "led3";
                        gpios = <&gpio1 23 GPIO_ACTIVE_LOW>;
                        default-state = "on";
                };

                led@4 {
                        pinctrl-names = "default";
                        pinctrl-0 = <&led4_ctl>;
                        label = "led4";
                        gpios = <&gpio0 12 GPIO_ACTIVE_LOW>;
                        default-state = "on";
                };
        };
结果:

四个led全亮,程序结束

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值