需添加的设备树节点
myplatform{
compatible = "hqyj,myplatform";
reg=<0X12345678 0X400>;
interrupt-parent=<&gpiof>;
interrupts=<9 0>; //9表示引用中断父节点时的索引信息 0表示默认设置
led1=<&gpioe 10 0>;
pdev.c
#include <linux/init.h>
#include <linux/module.h>
#include<linux/platform_device.h>
//release函数,用于释放申请的资源
void pdev_release(struct device *dev)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
}
//资源数组
struct resource res[]={
[0]={
.start=0X12345678,
.end=0X12345678+59,
.flags=IORESOURCE_MEM,
},
[1]={
.start=71,
.end=71,
.flags=IORESOURCE_IRQ,
},
};
//定义并初始化
struct platform_device pdev={
.name="aaaaa",
.id=PLATFORM_DEVID_AUTO,
.dev={
.release=pdev_release,
},
.num_resources=ARRAY_SIZE(res),
.resource=res,
};
static int __init mycdev_init(void)
{
//注册
platform_device_register(&pdev);
return 0;
}
static void __exit mycdev_exit(void)
{
//注销
platform_device_unregister(&pdev);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
pdrv.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/gpio.h>
#include <linux/interrupt.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/of_irq.h>
struct resource *res;
unsigned int irqno;
struct gpio_desc *gpiono;
struct cdev *cdev;
unsigned int major = 500;
unsigned int minor = 0; // 次设备号的起始值
dev_t devno;
struct class *cls;
struct device *dev;
// 定义等待队列头
wait_queue_head_t wq_head;
unsigned int condition = 0;
unsigned int number;
struct device_node *dnode;
// 中断处理函数
irqreturn_t myirq_handler(int irqno, void *dev_id)
{
if (number == 0)
{
number = 1;
gpiod_set_value(gpiono, 1); // 亮灯
}
else
{
number = 0;
gpiod_set_value(gpiono, 0); // 灭灯
}
condition=1;//表示硬件数据就绪
wake_up_interruptible(&wq_head);//唤醒休眠的进程
//printk("number = %d\n", number); // 打印此时的number的值
return IRQ_HANDLED;
}
//封装字符设备驱动相关API
//open()
int mycdev_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
//ioctl()
/*{
//根据给功能码实现相关控制工作
}*/
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
int ret;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
// 向用户空间读取拷贝
if (size > sizeof(number)) // 用户空间期待读取的大小内核满足不了,那就给内核支持的最大大小
size = sizeof(number);
wait_event_interruptible(wq_head, condition); // 将进程切换为休眠
ret = copy_to_user(ubuf, &number, size);
if (ret) // 拷贝失败
{
printk("copy_to_user filed\n");
return ret;
}
condition = 0; // 表示下一次硬件数据没有准备好
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
return 0;
}
//close()
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,
};
//probe函数用于匹配设备成功后执行
int pdrv_probe(struct platform_device *pdev)//当和设备匹配成功之后执行probe
{
int ret, i;
// 初始化等待队列头
init_waitqueue_head(&wq_head);
//1.为字符设备驱动对象分配空间 cdev_alloc()
cdev = cdev_alloc();
if (cdev == NULL)
{
printk("申请字符设备驱动对象空间失败\n");
ret = -EFAULT;
goto out1;
}
printk("字符设备驱动对象申请成功\n");
//2.完成驱动对象部分初始化//cdev_init
cdev_init(cdev, &fops);
//3.申请设备号 register_chrdev_region()/alloc_chrdev_region
if (major > 0) // 静态申请设备号
{
ret = register_chrdev_region(MKDEV(major, minor), 3, "myplatform");
if (ret)
{
printk("静态指定设备号失败\n");
goto out2;
}
}
else // 动态申请设备号
{
ret = alloc_chrdev_region(&devno, minor, 3, "myplatform");
if (ret)
{
printk("动态申请设备号失败\n");
goto out2;
}
major = MAJOR(devno); // 根据设备号得到主设备号
minor = MINOR(devno); // 根据设备号得到次设备号
}
printk("申请设备号成功\n");
//4.注册驱动对象 cdev_add
ret = cdev_add(cdev, MKDEV(major, minor), 3);
if (ret)
{
printk("注册字符设备驱动对象失败\n");
goto out3;
}
printk("注册字符设备驱动对象成功\n");
//5.向上提交目录 class_create()
cls = class_create(THIS_MODULE, "myplatform");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
ret = -PTR_ERR(cls);
goto out4;
}
printk("向上提交目录成功\n");
//6.向上提交设备节点 device_create()
for (i = 0; i < 3; i++)
{
dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myplatform%d", i);
if (IS_ERR(dev))
{
printk("向上提交节点信息失败\n");
ret = -PTR_ERR(dev);
goto out5;
}
}
printk("向上提交设备节点信息成功\n");
//7.根据设备树节点结构体指针解析GPIO信息
dnode = of_find_node_by_name(NULL, "myplatform");
if (dnode == NULL)
{
printk("解析设备树节点失败\n");
return -ENXIO;
}
printk("解析设备树节点成功\n");
// 获取软中断号
irqno = irq_of_parse_and_map(dnode, 0);
if (!irqno)
{
printk("软中断号获取失败\n");
return -ENOMEM;
}
printk("软中断号获取成功 irqno = %d\n", irqno);
// 注册中断
ret = request_irq(irqno, myirq_handler, IRQF_TRIGGER_FALLING, "myplatform1", NULL);
if (ret)
{
printk("注册驱动失败\n");
return ret;
}
printk("key1中断注册成功\n");
// 根据设备树节点解析led1 gpio结构体并向内核注册
gpiono = gpiod_get_from_of_node(dnode,"led1",0,GPIOD_OUT_LOW,NULL);
if (IS_ERR(gpiono))
{
printk("申请gpio失败\n");
return -PTR_ERR(gpiono);
}
gpiod_set_value(gpiono, 0); // 灭灯
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), 3);
out2:
kfree(cdev);
out1:
return ret;
//8.LED灯灭
}
//remove用于和设备分离时执行
int pdrv_remove(struct platform_device *pdev)//当设备和驱动分离时执行remove
{
int i;
//1.释放GPIO信息
gpiod_put(gpiono);
//2.销毁设备信息
for (i = 0; i < 3; i++)
{
device_destroy(cls, MKDEV(major, i));
}
//3.销毁目录信息
class_destroy(cls);
//4.注销驱动对象
cdev_del(cdev);
//5.释放设备号
unregister_chrdev_region(MKDEV(major, minor), 3);
//6.释放字符设备驱动对象空间
kfree(cdev);
return 0;
}
//在驱动中创建一个用于设备树匹配的表
struct of_device_id oftable[]={
{.compatible="hqyj,myled",},
{},
};
//分配对象并初始化
struct platform_driver pdrv={
.probe=pdrv_probe,
.remove=pdrv_remove,
.driver={
.name="aaaaa",
.of_match_table=oftable, //指定设备树匹配表的首地址
},
};
//一键注册宏,代替入口出口
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");
test.c
#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>
int main(int argc, char const *argv[])
{
unsigned int number;
int fd = open("/dev/myplatform0", O_RDWR);
if (fd < 0)
{
printf("打开设备文件失败\n");
exit(-1);
}
while(1)
{
read(fd,&number,sizeof(number));//读取数据
printf("number = %d\n",number);
}
close(fd);
return 0;
}
实验结果