基于platform驱动模型通过(gpiod_get_from_of_node)函数完成LED驱动的编写

pdri.ko

#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/of.h>
#include <linux/of_gpio.h>

struct resource *res;
unsigned int irq1;
struct gpio_desc *gpiono1;
struct gpio_desc *gpiono2;
struct gpio_desc *gpiono3;

struct platform_device *pdev1;

struct class *cls;
struct device *dev;
int major;
char kbuf[128] = {0};

//定义操作方法
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 ret1=copy_to_user(ubuf,kbuf,size);
    if(ret1)
    {
        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__);
    int ret=copy_from_user(kbuf,ubuf,size);
    if(ret)
    {
        printk("copy_from_user filed\n");
        return -EIO;
    }
    if(!strcmp(kbuf,"on"))
    {        
        gpiod_set_value(gpiono1,1);
        gpiod_set_value(gpiono2,1);
        gpiod_set_value(gpiono3,1);   
    }
    else if(!strcmp(kbuf,"off"))
    {
        gpiod_set_value(gpiono1,0);
        gpiod_set_value(gpiono2,0);
        gpiod_set_value(gpiono3,0);   
    }
    return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
 
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
//定义操作方法结构体变量并赋值
struct file_operations fops = {
    .open = mycdev_open,
    .read = mycdev_read,
    .write = mycdev_write,
    .release = mycdev_close,
};

int pdrv_probe(struct platform_device *pdev)
{
    // 字符设备驱动注册
    pdev1 = 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");

    gpiono1 = gpiod_get_from_of_node(pdev1->dev.of_node,"led1-gpio-no",0,GPIOD_OUT_HIGH,NULL);
    gpiono2 = gpiod_get_from_of_node(pdev1->dev.of_node,"led2-gpio-no",0,GPIOD_OUT_LOW,NULL);
    gpiono3 = gpiod_get_from_of_node(pdev1->dev.of_node,"led3-gpio-no",0,GPIOD_OUT_HIGH,NULL);
    if(IS_ERR(gpiono1))
    {
        printk("解析GPIO on资源1失败\n");
        return -PTR_ERR(gpiono1);
    }
    printk("解析GPIO on资源1成功\n");
    if(IS_ERR(gpiono2))
    {
        printk("解析GPIO on资源2失败\n");
        return -PTR_ERR(gpiono2);
    }
    printk("解析GPIO on资源2成功\n");
    if(IS_ERR(gpiono3))
    {
        printk("解析GPIO on资源3失败\n");
        return -PTR_ERR(gpiono3);
    }
    printk("解析GPIO on资源3成功\n");   
    printk("USB链接成功\n");    


    return 0;
}
int pdrv_remove(struct platform_device *pdev)
{
    //释放资源
    gpiod_set_value(gpiono1,0);
    gpiod_put(gpiono1);
    gpiod_set_value(gpiono2,0);
    gpiod_put(gpiono2);
    gpiod_set_value(gpiono3,0);
    gpiod_put(gpiono3);

    printk("USB设备已弹出\n");
    return 0;
}
struct of_device_id oftable[] = {
        {.compatible = "kpkp,myplatform",},
        {.compatible = "kpkp,myplatform1",},
        {},
};
struct platform_driver pdrv = {
	.probe = pdrv_probe,    //这是个int类型函数名,参数类型为struct platform_device*	
	.remove = pdrv_remove, //这是个int类型函数名,,参数类型为struct platform_device*
	.driver = {
	    .name = "AK47",
        .of_match_table = oftable,
	    },
    };


//一键注册宏
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");

pdev.ko

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

//这是个结构体
struct resource res[] = {
	[0]={
	    .start = 0x50006000,
	    .end = 0x50006000 + 0x400,
	    .flags = IORESOURCE_MEM,
	    },
	[1]={
	    .start = 71,
	    .end = 71,
	    .flags = IORESOURCE_IRQ,	
	    },
    };
void pdev_release(struct device *dev) //这是个函数名
{
    printk("已弹出,期待下次链接\n");
} 
struct platform_device pdev = {
	.name = "AK47",
	.id = PLATFORM_DEVID_AUTO,
	.dev.release = pdev_release,  //这是个函数名
	.num_resources = ARRAY_SIZE(res),
	.resource = res,  //这是个结构体
	};
	
static int __init mycdev_init(void)
{
    //定义设备端对象且初始化
    //注册设备信息进内核
    if(platform_device_register(&pdev) < 0 )
    {
        printk("注册设备信息失败\n");
        return -ENXIO;
    }

    return 0;
}
static void __exit mycdev_exit(void)
{
    //注销设备信息
    platform_device_unregister(&pdev);

}
module_init(mycdev_init);
module_exit(mycdev_exit);


MODULE_LICENSE("GPL");

ledu.c

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main(int argc,const char * argv[])
{
    int a=0;
    char buf[128] = "";
    char buf1[128] = "";
    int fd1=open("/dev/myled0",O_RDWR);
    int fd2=open("/dev/myled1",O_RDWR);
    int fd3=open("/dev/myled2",O_RDWR);
    if(fd1<0)
    {
        printf("打开设备文件1失败\n");
        exit(-1);
    }
    if(fd2<0)
    {
        printf("打开设备文件2失败\n");
        exit(-1);
    }
    if(fd3<0)
    {
        printf("打开设备文件3失败\n");
        exit(-1);
    }

    memset(buf,0,sizeof(buf));
    while(1)
    {
        printf("请输入灯的控制(1开灯/0关灯) => ");
        scanf("%d",&a);
        
        write(fd1,buf,sizeof(buf));
        write(fd2,buf,sizeof(buf));
        write(fd3,buf,sizeof(buf));

        if(a == 1)
        {
            strcpy(buf,"on");
            write(fd1,buf,sizeof(buf));
            write(fd2,buf,sizeof(buf));
            write(fd3,buf,sizeof(buf));            
        }
        else if(a == 0)
        {
            strcpy(buf,"off");
            write(fd1,buf,sizeof(buf));
            write(fd2,buf,sizeof(buf));
            write(fd3,buf,sizeof(buf));             
        }
        read(fd1,buf1,sizeof(buf));
        printf("buf1:%s\n",buf1);
    }
    return 0;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值