jz2440_LED_设备树之点亮LED灯

https://blog.csdn.net/huanting_123/article/details/89608501

简介:设备驱动程序中,总线驱动模型和设备树的差别在于平台设备的构建不同,在传统平台设备驱动程序写在C文件中,使用设备树时,平台设备之前不存在,在dts文件中构造节点,节点里面含有资源,设备树详细的内容我会在以后的博客中详细介绍。

 

CPU : jz2440

u-boot: u-boot-1.1.6

kernel: linux-4.19-rc3

root :fs_mini_mdev_new

 1 /* platform_device结构体定义 */
 2 struct platform_device {
 3     const char    *name;
 4     int        id;
 5     bool        id_auto;
 6     struct device    dev;
 7     u32        num_resources;
 8     struct resource    *resource;
 9 
10     const struct platform_device_id    *id_entry;
11     char *driver_override; /* Driver name to force a match */
12 
13     /* MFD cell pointer */
14     struct mfd_cell *mfd_cell;
15 
16     /* arch specific additions */
17     struct pdev_archdata    archdata;
18 };
19 
20 /* platform_device示例 */
21 struct platform_driver led_drv = {
22     .probe        = led_probe,
23     .remove        = led_remove,
24     .driver        = {
25         .name    = "myled",
26     }
27 };
28 
29 /* platform_driver结构体定义 */
30 struct platform_driver {
31     int (*probe)(struct platform_device *);
32     int (*remove)(struct platform_device *);
33     void (*shutdown)(struct platform_device *);
34     int (*suspend)(struct platform_device *, pm_message_t state);
35     int (*resume)(struct platform_device *);
36     struct device_driver driver;
37     const struct platform_device_id *id_table;
38     bool prevent_deferred_probe;
39 };
40 
41 /* platform_driver结构体示例 */
42 struct platform_driver led_drv = {
43     .probe        = led_probe,
44     .remove        = led_remove,
45     .driver        = {
46         .name    = "myled",
47     }
48 };

由于本文档的目的是简单介绍传统的总线驱动模型和设备树的区别,总线驱动模型的详细就不再介绍,详细介绍可以参考linux设备驱动模型之总线、设备、驱动三者的关系

下面简单分析设备树的匹配

static int platform_match(struct device *dev, struct device_driver *drv)
{
    struct platform_device *pdev = to_platform_device(dev);
    struct platform_driver *pdrv = to_platform_driver(drv);

    /* When driver_override is set, only bind to the matching driver */
    if (pdev->driver_override)
        return !strcmp(pdev->driver_override, drv->name);

    /* Attempt an OF style match first */
    if (of_driver_match_device(dev, drv))       //用于判断从设备树得到的platform_device与platform_driver是否匹配
        return 1;

    /* Then try ACPI style match */
    if (acpi_driver_match_device(dev, drv))
        return 1;

    /* Then try to match against the id table */
    if (pdrv->id_table)
        return platform_match_id(pdrv->id_table, pdev) != NULL;

    /* fall-back to driver name match */
    return (strcmp(pdev->name, drv->name) == 0);
}

static inline int of_driver_match_device(struct device *dev,
                     const struct device_driver *drv)
{
    return of_match_device(drv->of_match_table, dev) != NULL;
}

static inline int of_driver_match_device(struct device *dev,
                     const struct device_driver *drv)
{
    return of_match_device(drv->of_match_table, dev) != NULL;
}

根据上面的函数可以看出,从设备树得到的platform_device中的节点和platform_driver中的of_device_id是否匹配,匹配成功之后,就会调用platform_driver中的probe函数。

/dts-v1/;

 

/ {

    model = "SMDK24440";

    compatible = "samsung,smdk2440";

 

    #address-cells = <1>;

    #size-cells = <1>;

 

    memory@30000000 {

    device_type = "memory";

    reg =  <0x30000000 0x4000000>;

    };

    /*

    cpus {
    
        cpu {

        compatible = "arm,arm926ej-s";

        };

    };

*/

    chosen {

    bootargs = "noinitrd root=/dev/mtdblock4 rw init=/linuxrc             
    console=ttySAC0,115200";

    };

 

 

        led {

        compatible = "jz2440_led";  

        pin = <S3C2410_GPF(5)>;

        };

    };        

 

 

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_platform.h>
#include <linux/platform_device.h>


static int led_pin;
static volatile unsigned int *gpio_con;
static volatile unsigned int *gpio_dat;

/* 123. 分配/设置/注册file_operations 
 * 4. 入口
 * 5. 出口
 */

static int major;
static struct class *led_class;

static unsigned int gpio_base[] = {
    0x56000000, /* GPACON */
    0x56000010, /* GPBCON */
    0x56000020, /* GPCCON */
    0x56000030, /* GPDCON */
    0x56000040, /* GPECON */
    0x56000050, /* GPFCON */
    0x56000060, /* GPGCON */
    0x56000070, /* GPHCON */
    0,          /* GPICON */
    0x560000D0, /* GPJCON */
};

static int led_open (struct inode *node, struct file *filp)
{
    /* 把LED引脚配置为输出引脚 */
    /* GPF5 - 0x56000050 */
    int bank = led_pin >> 16;
    int base = gpio_base[bank];

    int pin = led_pin & 0xffff;
    gpio_con = ioremap(base, 8);
    if (gpio_con) {
        printk("ioremap(0x%x) = 0x%x\n", base, gpio_con);
    }
    else {
        return -EINVAL;
    }
    
    gpio_dat = gpio_con + 1;
    
    *gpio_con &= ~(3<<(pin * 2));
    *gpio_con |= (1<<(pin * 2));  

    return 0;
}

static ssize_t led_write (struct file *filp, const char __user *buf, size_t size, loff_t *off)
{
    /* 根据APP传入的值来设置LED引脚 */
    unsigned char val;
    int pin = led_pin & 0xffff;
    
    copy_from_user(&val, buf, 1);

    if (val)
    {
        /* 点灯 */
        *gpio_dat &= ~(1<<pin);
    }
    else
    {
        /* 灭灯 */
        *gpio_dat |= (1<<pin);
    }

    return 22; /* 已写入1个数据 */
}

static int led_release (struct inode *node, struct file *filp)
{
    printk("iounmap(0x%x)\n", gpio_con);
    iounmap(gpio_con);
    return 0;
}


static struct file_operations myled_oprs = {
    .owner = THIS_MODULE,
    .open  = led_open,
    .write = led_write,
    .release = led_release,
};

static int led_probe(struct platform_device *pdev)
{
    struct resource     *res;

    res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
    if (res) {
        led_pin = res->start;
    }
    else {
        /* 获得pin属性 */
        of_property_read_s32(pdev->dev.of_node, "pin", &led_pin);
    }

    if (!led_pin) 
    {
        printk("can not get pin for led\n");
        return -EINVAL;
    }
    
    major = register_chrdev(0, "myled", &myled_oprs);

    led_class = class_create(THIS_MODULE, "myled");
    device_create(led_class, NULL, MKDEV(major, 0), NULL, "led"); /* /dev/led */
    
    return 0;
}

static int led_remove(struct platform_device *pdev)
{
    unregister_chrdev(major, "myled");
    device_destroy(led_class,  MKDEV(major, 0));
    class_destroy(led_class);
    
    return 0;
}


static const struct of_device_id of_match_leds[] = {
    { .compatible = "jz2440_led", .data = NULL },
    { /* Sentinel */ }
};


struct platform_driver led_drv = {
    .probe        = led_probe,
    .remove        = led_remove,
    .driver        = {
        .name    = "myled",
        .of_match_table    = of_match_leds, //能支持哪些来自于dts的platform_device
    }
};

static int myled_init(void)
{
    platform_driver_register(&led_drv);
    return 0;
}

static void myled_exit(void)
{
    platform_driver_unregister(&led_drv);
}

module_init(myled_init);
module_exit(myled_exit);

MODULE_LICENSE("GPL");

 

posted on 2020-04-04 16:15  Red_Point  阅读(476)  评论(0)    收藏  举报

导航