驱动开发10 基于platform驱动模型完成LED驱动的编写

pdrv.c

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "head.h"

struct resource *res;
unsigned int irqno;
struct gpio_desc* gpiono1;
struct gpio_desc* gpiono2;
struct gpio_desc* gpiono3;
struct device *dev;
int major;
struct class *cls;
char kbuf[128] = {};

//定义设备树匹配的表
struct of_device_id oftable[]={
    {.compatible="hqyj,myplatform",},
    {.compatible="hqyj,myplatform1",},
    {},//防止数组越界
};
int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size,loff_t *lof)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    int ret = copy_to_user(ubuf,kbuf,size);
    if (ret)
    {
        printk("copy_to_user filed\n");
        return -EIO;
    }
    return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size,loff_t *lof)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}

long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    int which;
    int ret = copy_from_user(&which,(void *)arg,4);
    if(ret)
    {
        printk("copy_form_user success : [%d]\n",which);
    }
    switch(cmd)
    {
        case LED_ON://开灯
            switch(which)
            {
                case 1:
                    gpiod_set_value(gpiono1,1);
                    break;
                case 2:
                    gpiod_set_value(gpiono2,1);
                    break;
                case 3:
                    gpiod_set_value(gpiono3,1);
                    break;
            }
            break;
        case LED_OFF://关灯
            switch(which)
            {
                case 1:
                    gpiod_set_value(gpiono1,0);
                    break;
                case 2:
                    gpiod_set_value(gpiono2,0);
                    break;
                case 3:
                    gpiod_set_value(gpiono3,0);
                    break;
            }
            break;
    }
    return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    int i;
    for(i=0;i<3;i++)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    //销毁目录
    class_destroy(cls);
    //注销驱动
    unregister_chrdev(major,"mychrdev");
    return 0;
}
//定义操作方法结构体变量并赋值
struct file_operations fops={
    .open=mycdev_open,
    .read = mycdev_read,
    .write = mycdev_write,
    .unlocked_ioctl=mycdev_ioctl,
    .release = mycdev_close,
};

//给对象分配空间并且初始化
int pdrv_probe(struct platform_device *pdev)
{
    //字符设备驱动注册
    major = register_chrdev(0,"mychrdev",&fops);
    if(major<0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功:major=%d\n",major);
    //向上提交目录
    cls = class_create(THIS_MODULE,"mychrdev");
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return -PTR_ERR(cls);
    }
    printk("向上提交目录成功\n");
    //向上提交设备节点信息
    int i;//向上提交三次设备节点信息
    for(i=0;i<3;i++)
    {
        dev = device_create(cls,NULL,MKDEV(major,i),NULL,"myled%d",i);
        if(IS_ERR(dev))
        {
            printk("向上提交设备节点失败\n");
            return -PTR_ERR(dev);
        }
    }
    printk("向上提交设备节点成功\n");

    //获取MEM类型的资源
    res = platform_get_resource(pdev,IORESOURCE_MEM,0);
    if(res == NULL)
    {
        printk("获取MEM类型的资源失败\n");
        return -ENXIO;
    }
    //获取中断类型资源
    irqno = platform_get_irq(pdev,0);
    if(irqno < 0)
    {
        printk("获取中断类型资源失败\n");
        return -ENXIO;
    }
    printk("MEM类型资源位%x\n",res->start);
    printk("中断类型资源为%d\n",irqno);

    //4.解析LED设备树节点获取三个关键的GPIO信息
    //获取LED1的gpio信息
    gpiono1 = gpiod_get_from_of_node(pdev->dev.of_node,"led1-gpio",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono1))
    {
        printk("LED1解析GPIO信息失败\n");
        return PTR_ERR(gpiono1);
    }
    //获取LED2的gpio信息
    gpiono2 = gpiod_get_from_of_node(pdev->dev.of_node,"led2-gpio",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono2))
    {
        printk("LED2解析GPIO信息失败\n");
        return PTR_ERR(gpiono2);
    }
    //获取LED3的gpio信息
    gpiono3 = gpiod_get_from_of_node(pdev->dev.of_node,"led3-gpio",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono3))
    {
        printk("LED3解析GPIO信息失败\n");
        return PTR_ERR(gpiono3);
    }
    return 0;
}
int pdrv_remove(struct platform_device *pdev)
{
    //释放GPIO信息
    gpiod_set_value(gpiono1,0);
    gpiod_set_value(gpiono2,0);
    gpiod_set_value(gpiono3,0);
    gpiod_put(gpiono1);
    gpiod_put(gpiono2);
    gpiod_put(gpiono3);
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);

    return 0;
}

//分配驱动信息对象
struct platform_driver pdrv={
    .probe = pdrv_probe,
    .remove = pdrv_remove,
    .driver={
        .name="bbbbb",
        .of_match_table=oftable,
    },
};
//一键注册宏
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");

test.c

#include 
#include 
#include 
#include 
#include 
#include 
#include "head.h"
int main(int argc, char const *argv[])
{
    char buf[128]={0};
    int fd1 = open("/dev/myled0",O_RDWR);
    if(fd1 < 0)
    {
        printf("打开设备文件1失败\n");
        return -1;
    }
    printf("打开设备文件1成功\n");   
    int fd2 = open("/dev/myled1",O_RDWR);
    if(fd2 < 0)
    {
        printf("打开设备文件2失败\n");
        return -1;
    }
    printf("打开设备文件2成功\n"); 
    int fd3 = open("/dev/myled3",O_RDWR);
    if(fd3 < 0)
    {
        printf("打开设备文件3失败\n");
        return -1;
    }
    printf("打开设备文件3成功\n"); 
    while(1)
    {
        int a,b;
        printf("请输入要进行的操作的灯:1(LED1)2(LED2)3(LED3) >>> ");
        scanf("%d",&a);
        printf("请输入要进行的操作:0(关灯) 1(开灯) >>> ");
        scanf("%d", &b);
		// 向设备文件中写
		switch (b)
		{
		case 1:
			switch (a)
			{
			case 1: // LED1
				printf("LED1_ON\n");
				ioctl(fd1, LED_ON,&a);
				break;
			case 2: // LED2
				printf("LED2_ON\n");
				ioctl(fd2, LED_ON,&a);
				break;
			case 3: // LED3
				printf("LED3_ON\n");
				ioctl(fd3, LED_ON,&a);
				break;
			}
			break;
		case 0:
			switch (a)
			{
			case 1: // LED1
				printf("LED1_OFF\n");
				ioctl(fd1, LED_OFF,&a);
				break;
			case 2: // LED2
				printf("LED2_OFF\n");
				ioctl(fd2, LED_OFF,&a);
				break;
			case 3: // LED3
				printf("LED3_OFF\n");
				ioctl(fd3, LED_OFF,&a);
				break;
			}
			break;
		}
    }
    close(fd1);
    close(fd2);
    close(fd3);
    return 0;
}

head.h

#ifndef __HEAD_H__
#define __HEAD_H__
// 构建开灯关灯的功能码
#define LED_ON _IO('l', 1)
#define LED_OFF _IO('l', 0)
#endif

效果

驱动开发10 基于platform驱动模型完成LED驱动的编写_第1张图片

你可能感兴趣的:(驱动开发,驱动开发)