platform框架点亮led,按键控制风扇

driver.c

#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>
#include <linux/ioctl.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>


#define LED_ON _IOW('a',1,int)
#define LED_OFF _IOW('a',0,int)
struct cdev *cdev;
#if 0
unsigned int major=0;
#else
unsigned int major=500;
#endif
unsigned int i,minor=0;//存放次设备号
dev_t devno;//存储设备号
struct class *cls;
struct device *dev;
struct resource *res;
int irqno;
struct gpio_desc* gpiono1;
struct gpio_desc* gpiono2;
struct gpio_desc* gpiono3;
struct gpio_desc* fan;
//分配对象并初始化
//中断处理函数
irqreturn_t irq1_handler(int irqno,void *arg)
{ 
    gpiod_set_value(fan,!gpiod_get_value(fan));
    return IRQ_HANDLED;
}
long driver_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    int witch;
    copy_from_user(&witch,(void*)arg,sizeof(int));
    switch(cmd)
    {
        case LED_ON:
            switch(witch)
            {
                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(witch)
            {
                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;
}
struct file_operations fops=
{
    .unlocked_ioctl=driver_ioctl,
};
//probe函数
int pdrv_probe(struct platform_device *pdev)
{
    /*
        1.分配对象
        2.对象初始化
        3.设备资源的申请(设备号)
        4.注册
        5.向上提交目录
        6.向上提交节点信息
    */
    int ret;
    cdev=cdev_alloc();
    if(cdev==NULL){
        printk("cdev alloc error!!!\n");
        ret=-ENOMEM;
        goto ERR1;
    }
    cdev_init(cdev,&fops);//初始化字符设备驱动对象
    if(major==0){
        ret=alloc_chrdev_region(&devno,minor,3,"led");
        if(ret){
            printk("alloc chrdev region error!!!\n");
            goto ERR2;
        }
        major=MAJOR(devno);
        minor=MINOR(devno);
    }
    else if(major>0){
        ret=register_chrdev_region(MKDEV(major,minor),3,"led");
        if(ret){
            printk("register chrdev region error!!!\n");
            goto ERR2;
        }
    }
    //将驱动对象注册进内核
    ret=cdev_add(cdev,MKDEV(major,minor),3);
    if(ret){
        printk("cdev add error!!!\n");
        goto ERR3;
    }
    //向上提交目录
    cls=class_create(THIS_MODULE,"led");
    if(IS_ERR(cls)){
        printk("向上提交目录失败\n");
        ret=PTR_ERR(cls);
        goto ERR4;
    }
    //向上提交节点信息
    for(i=0;i<3;i++){
        dev=device_create(cls,NULL,MKDEV(major,i),NULL,"led%d",i);
        if(IS_ERR(dev)){
            printk("向上提交节点信息失败\n");
            ret=PTR_ERR(dev);
            goto ERR5;
        }
    }
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    //获取MEM类型设备资源
    res=platform_get_resource(pdev,IORESOURCE_MEM,0);
    if(res==NULL)
    {
        printk("获取设备信息失败\n");
        return -ENODATA;
    }
    //获取中断类型资源
    irqno=platform_get_irq(pdev,0);
    if(irqno<0)
    {
        printk("获取中断类型资源失败\n");
        return -ENODATA;
    }
    printk("mem类型资源数值为:%x\n",res->start);
    printk("中断类型的资源数值为%d\n",irqno);
    gpiono1=gpiod_get_from_of_node(pdev->dev.of_node,"led1",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono1))
    {
        printk("解析gpio编号失败\n");
        return PTR_ERR(gpiono1);
    }
    gpiod_set_value(gpiono1,0);
    gpiono2=gpiod_get_from_of_node(pdev->dev.of_node,"led2",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono2))
    {
        printk("解析gpio编号失败\n");
        return PTR_ERR(gpiono2);
    }
    gpiod_set_value(gpiono2,0);
    gpiono3=gpiod_get_from_of_node(pdev->dev.of_node,"led3",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono3))
    {
        printk("解析gpio编号失败\n");
        return PTR_ERR(gpiono3);
    }
    gpiod_set_value(gpiono3,0);
    fan=gpiod_get_from_of_node(pdev->dev.of_node,"fan",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(fan))
    {
        printk("解析gpio编号失败\n");
        return PTR_ERR(fan);
    }
    gpiod_set_value(fan,0);
    request_irq(irqno,irq1_handler,IRQF_TRIGGER_FALLING,"keyirq",NULL);
    return 0;
ERR5:
    for(;i>=0;i--)
        device_destroy(cls,MKDEV(major,i));
    class_destroy(cls);
ERR4:
    cdev_del(cdev);
ERR3:
    unregister_chrdev_region(MKDEV(major,minor),3);
ERR2:
    kfree(cdev);
ERR1:
    return ret;
}
//remove函数
int pdrv_remove(struct platform_device *pdev)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    free_irq(irqno,NULL);
    //灭灯
    gpiod_set_value(gpiono1,0);
    gpiod_set_value(gpiono2,0);
    gpiod_set_value(gpiono3,0);
    gpiod_set_value(fan,0);
    gpiod_put(gpiono1);
    gpiod_put(gpiono2);
    gpiod_put(gpiono3);
    gpiod_put(fan);
     /*
        1.销毁设备节点
        2.销毁目录
        3.注销驱动对象
        4.释放设备资源(设备号)
    */
   for(i=0;i<3;i++)
        device_destroy(cls,MKDEV(major,i));
   class_destroy(cls);
   unregister_chrdev_region(MKDEV(major,minor),3);
   kfree(cdev);
    return 0;
}
//构建设备树匹配标配
struct of_device_id oftable[]=
{
    {.compatible="hqyj,platform"},
    {.compatible="hqyj,leds"},
    {},
};
struct platform_driver pdrv={
    .probe=pdrv_probe,
    .remove=pdrv_remove,
    .driver={
        .name="led",
        .of_match_table=oftable,//设置设备树匹配
    },
};

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

test.c:

#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include<string.h>
#include <sys/ioctl.h>  
#define LED_ON _IOW('a',1,int)
#define LED_OFF _IOW('a',0,int)
int main(int argc, char const *argv[])
{
    char buf[128]={0};
    int which=1;
    int fd=open("/dev/led0",O_RDWR);
    if(fd<0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    //在终端输入数据
    while(1)
    {
        ioctl(fd,LED_ON,&which);
        sleep(1);
        ioctl(fd,LED_OFF,&which);
        which=2;
        ioctl(fd,LED_ON,&which);
        sleep(1);
        ioctl(fd,LED_OFF,&which);
        which=3;
        ioctl(fd,LED_ON,&which);
        sleep(1);
        ioctl(fd,LED_OFF,&which);
        which=1;
    }
    close(fd);
    return 0;
}

添加节点:

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值