基于platform驱动模型完成LED驱动的编写
驱动程序
#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/gpio.h>
#include <linux/timer.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include "head.h"
int major; //字符设备驱动注册主设备号
struct class *cls; //向上提交目录
struct device *dev; //向上提交设备节点
struct gpio_desc* gpiono[3];
//封装操作方法
int mycdev_open(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)
{
int which;
int ret = copy_from_user(&which, (void*)arg,4);
if(ret)
{
printk("copy_from_user failed\n");
return -EIO;
}
switch(cmd)
{
case LED_ON:
switch(which)
{
case 1:
gpiod_set_value(gpiono[0],1);
break;
case 2:
gpiod_set_value(gpiono[1], 1);
break;
case 3:
gpiod_set_value(gpiono[2], 1);
break;
}
break;
case LED_OFF:
switch(which)
{
case 1:
gpiod_set_value(gpiono[0],0);
break;
case 2:
gpiod_set_value(gpiono[1], 0);
break;
case 3:
gpiod_set_value(gpiono[2], 0);
break;
}
break;
}
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;
}
//定义操作方法结构体变量
struct file_operations fops = {
.open = mycdev_open,
.unlocked_ioctl = mycdev_ioctl,
.release = mycdev_close,
};
int pdrv_probe(struct platform_device *pdev)//当驱动和设备匹配成功之后执行
{
//字符设备驱动注册
major = register_chrdev(0, "mycdev",&fops);
if(major < 0)
{
printk("字符设备驱动注册失败\n");
return major;
}
/****************************udev创建设备节点的原************************/
//向上提交目录
cls = class_create(THIS_MODULE, "mycdev");
if(IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
//向上提交设备节点信息
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");
//解析LED设备树节点获取三个节点的gpio信息
for(i=0; i<3; i++)
{
gpiono[i]=gpiod_get_from_of_node(pdev->dev.of_node,"led-gpios", i, GPIOD_OUT_HIGH, NULL);
if(IS_ERR(gpiono[i]))
{
printk("解析GPIO信息失败\n");
return -PTR_ERR(gpiono[i]);
}
printk("解析GPIO信息成功\n");
}
return 0;
}
int pdrv_remove(struct platform_device *pdev)
{
int i;
for(i=0; i<3; i++)
{
gpiod_set_value(gpiono[i], 0);
//释放gpio编号
gpiod_put(gpiono[i]);
//销毁设备节点信息
device_destroy(cls, MKDEV(major, i));
}
//销毁目录
class_destroy(cls);
//注销字符设备驱动
unregister_chrdev(major, "mycdev");
return 0;
}
//构建设备树匹配表
struct of_device_id oftable[] = {
{.compatible="hqyj,myplatform",},
{.compatible="hqyj,myplatform1",},
{}, //防止数组越界
};
//1分配驱动信息对象
struct platform_driver pdrv= {
.probe=pdrv_probe,
.remove=pdrv_remove,
.driver={
.name="bbbbb", //驱动名
.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 <sys/ioctl.h>
#include <fcntl.h>
#include<unistd.h>
#include<string.h>
#include "head.h"
int main(int argc, char const *argv[])
{
int fd = open("/dev/myled0", O_RDWR);
if(fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while(1)
{
int a;
printf("请输入1(开灯)0(关灯)>>");
scanf("%d", &a);
int b;
printf("请输入 1(LED1) 2(LED2) 3(LED3)>>");
scanf("%d", &b);
switch(a)
{
case 1:
ioctl(fd, LED_ON,&b);
break;
case 0:
ioctl(fd, LED_OFF,&b);
break;
}
}
close(fd);
return 0;
}
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#define LED_ON _IOW('l', 1, int)
#define LED_OFF _IOW('l',0, int)
#endif