驱动开发--platform

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


#define LED_ON _IO('l',1)
#define LED_OFF _IO('l',0)

struct resource* res; 
unsigned int irqno; 
struct gpio_desc* gpiono[3]; 
 
struct class* cls;
struct device* dev; 
int i; 
int ret = 0; 
struct cdev* cdev; 
unsigned int major = 500; 
unsigned int minor = 0; 
unsigned int count = 1; 
dev_t devno; 

int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}

int mycdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}

long mycdev_ioctl(struct file* file, unsigned int cmd, unsigned long arg)
{
   switch(cmd)
    {
        case LED_ON:
            gpio_set_value(gpiono[0],1);//开灯
            gpio_set_value(gpiono[1],1);//开灯
            gpio_set_value(gpiono[2],1);//开灯
            break;
        case LED_OFF:
            gpio_free(gpiono[0]);//关灯
            gpio_free(gpiono[1]);//关灯
            gpio_free(gpiono[2]);//关灯
            break;
    }
    return 0;
}
 
//定义操作方法结构体变量
struct file_operations fops = {
    .open=mycdev_open,
    .unlocked_ioctl = mycdev_ioctl,
    .release=mycdev_close,
};
 
// 匹配设备成功后执行probe
int pdrv_probe(struct platform_device* pdev) 
{
    //1.分配对象空间
    cdev = cdev_alloc();
    if (cdev == NULL) {
        printk("分配字符设备驱动对象失败\n");
        ret = -EFAULT;
        goto OUT1;
    }
    printk("分配对象空间成功\n");
    //2.初始化对象
    cdev_init(cdev, &fops);
    //3.申请设备号
    if (major > 0)//静态指定设备号
    {
        ret = register_chrdev_region(MKDEV(major, minor), count, "myled");
        if (ret) {
            printk("静态指定设备号失败\n");
            goto OUT2;
        }
    } else if (major == 0) //动态申请设备号
    {
        ret = alloc_chrdev_region(&devno, minor, count, "myled");
        if (ret) {
            printk("动态申请设备号失败\n");
            goto OUT2;
        }
        //获取主设备号和次设备号
        major = MAJOR(devno);
        minor = MINOR(devno);
    }
    printk("申请设备号成功\n");
 
    //4.注册字符设备驱动对象
    ret = cdev_add(cdev, MKDEV(major, minor), count);
    if (ret) {
        printk("注册字符设备驱动对象失败\n");
        goto OUT3;
    }
    printk("注册字符设备驱动对象成功\n");
 
    //向上提交目录
    cls = class_create(THIS_MODULE, "myled");
    if (IS_ERR(cls)) {
        printk("向上提交目录失败\n");
        ret = -PTR_ERR(cls);
        goto OUT4;
    }
    printk("向上提交目录成功\n");
 
    //向上提交设备节点信息
    for (i = 0; i < count; i++) {
        dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
        if (IS_ERR(dev)) {
            ret = -PTR_ERR(dev);
            goto OUT5;
        }
    }
    printk("向上提交设备节点成功\n");
 
    // 解析MEM类型的资源
    res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
    if (res == NULL) {
        printk("解析资源失败%d\n", __LINE__);
        return -ENOMEM;
    }
 
    // 解析irq类型的资源
    irqno = platform_get_irq(pdev, 0);
    if (irqno < 0) {
        printk("解析资源失败%d\n", __LINE__);
        return -ENOMEM;
    }
    printk("mem:%x  irq:%d\n", res->start, irqno);
 
    //解析GPIO编号
    for (i = 0; i< RRAY_SIZE(gpiono); i++) {
        gpiono[i] = gpiod_get_from_of_node(pdev->dev.of_node, "led", i, GPIOD_OUT_LOW, NULL);
        if (IS_ERR(gpiono[i])) {
            printk("GPIO解析失败\n");
            return PTR_ERR(gpiono[i]);
        }
    }
    printk("GPIO解析成功\n");
    return 0;
 
OUT5:
    //释放已经申请的设备节点信息
    for (--i;i>=0;i--) {
        device_destroy(cls, MKDEV(major, i));
    }
    //释放目录空间
    class_destroy(cls);
OUT4:
    //注销字符设备驱动对象
    cdev_del(cdev);
OUT3:
    //释放设备号
    unregister_chrdev_region(MKDEV(major, minor), count);
OUT2:
    //释放对象空间
    kfree(cdev);
OUT1:
    return ret;
}
 
//和设备分离时执行remove
int pdrv_remove(struct platform_device* pdev) 
{
    for (i = 0; i < ARRAY_SIZE(gpiono); i++) {
        //灭灯
        gpiod_set_value(gpiono[i], 0);
        //释放GPIO编号
        gpiod_put(gpiono[i]);
    }
 
    //销毁设备节点
    for (i = 0; i < count; i++) {
        device_destroy(cls, MKDEV(major, i));
    }
    //释放目录空间
    class_destroy(cls);
    //1.注销字符设备驱动对象
    cdev_del(cdev);
    //2.释放设备号
    unregister_chrdev_region(MKDEV(major, minor), count);
    //3.释放对象空间
    kfree(cdev);
 
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
 
struct of_device_id oftable[] = {
    {
        .compatible = "hqyj,myled",
    },
    {
        .compatible = "hqyj,myled2",
    },
    {
        .compatible = "hqyj,myled3",
    },
    {},
};
// 分配对象并初始化
struct platform_driver pdrv = {
    .probe = pdrv_probe,
    .remove = pdrv_remove,
    .driver = {
        .name = "xzy",
        .of_match_table = oftable, // 指定设备树匹配表的首地址
    },
 
};
 
// 一键注册注销宏
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#define LED_ON _IO('l',1)
#define LED_OFF _IO('l',0)
 
int main(int argc, char const *argv[])
{
    int a;
    char buf[128]={0};
    int fd=open("/dev/mycdev",O_RDWR);
    if(fd<0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    while(1)
    {
        printf("请输入:1(开灯) 0(关灯)>");
        scanf("%d",&a);
        switch(a)
        {
            case 1:
                ioctl(fd,LED_ON);//开灯
                break;
            case 0:
                ioctl(fd,LED_OFF);//关灯
                break;
        }
    }
    close(fd);
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值