platform驱动实现
- match-devicetree.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/gpio.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/interrupt.h>
#include <linux/fs.h>
#include <linux/of_irq.h>
/*myplatform{
compatible = "hqyj,myplatform";
reg=<0X12345678 0X400>;
interrupt-parent=<&gpiof>;
interrupts=<9 0>,<7 0>,<8 0>; //9表示引用中断父节点时的索引信息 0表示默认设置
led1=<&gpioe 10 0>;
led2=<&gpiof 10 0>;
led3=<&gpioe 8 0>;
};*/
struct resource *res;
unsigned int irqno[3] = {0};
struct class *cls;
struct device *dev;
int major; // 用于保存主设备号
struct gpio_desc *gpiono1, *gpiono2, *gpiono3;
// 分配底半部对象
struct tasklet_struct tasklet;
unsigned int number = 0;
// 定义等待队列头
wait_queue_head_t wq_head;
unsigned int condition = 0;
// 定义底半部处理函数
void tasklet_callback(struct tasklet_struct *t)
{
number = !number;
condition = 1; // 表示硬件数据就绪
wake_up_interruptible(&wq_head); // 唤醒休眠的进程
}
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)
{
int ret;
if (sizeof(number) < size)
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 -EIO;
}
condition = 0; // 表示下一次硬件数据没有准备好
return 0;
}
// 中断处理函数
irqreturn_t myirq_handler(int irqnode, void *dev_id)
{
// KEY1控制LED1,KEY2控制LED2,KEY3控制LED3
if (irqnode == irqno[0])
{
gpiod_set_value(gpiono3, !gpiod_get_value(gpiono3));
printk("key1 interrupt\n");
}
else if (irqnode == irqno[1])
{
gpiod_set_value(gpiono2, !gpiod_get_value(gpiono2));
printk("key2 interrupt\n");
}
else if (irqnode == irqno[2])
{
gpiod_set_value(gpiono1, !gpiod_get_value(gpiono1));
printk("key3 interrupt\n");
}
tasklet_schedule(&tasklet);
return IRQ_HANDLED;
}
struct file_operations fops =
{
.open = mycdev_open,
.read = mycdev_read,
};
// probe函数,匹配设备成功执行
int pdrv_probe(struct platform_device *pdev)
{
int ret, i;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
// 获取设备信息
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (res == NULL)
{
printk("获取资源失败\n");
return -ENXIO;
}
printk("获取资源信息成功 %x\n", res->start);
// 初始化等待队列头
init_waitqueue_head(&wq_head);
// 底半部对象初始化
tasklet_setup(&tasklet, tasklet_callback);
for (i = 0; i < 3; i++)
{
// 获取中断号*********************************
irqno[i] = platform_get_irq(pdev, i);
if (!irqno[i])
{
printk("软中断号获取失败\n");
return -ENOMEM;
}
printk("软中断号获取成功 irqno[%d] = %d\n", i, irqno[i]);
// 注册中断************************************
ret = request_irq(irqno[i], myirq_handler, IRQF_TRIGGER_FALLING, "key1", NULL);
if (ret)
{
printk("注册key%d驱动失败\n", i + 1);
for (i--; i >= 0; i--)
{
free_irq(irqno[i], NULL);
}
return ret;
}
printk("key%d中断注册成功\n", i + 1);
}
// 字符设备驱动注册
major = register_chrdev(0, "myplatform", &fops);
if (major < 0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功major=%d\n", major);
// 向上提交目录
cls = class_create(THIS_MODULE, "myplatform");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录成功\n");
// 向上提交设备节点信息
dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "myplatform");
if (IS_ERR(dev))
{
printk("向上提交设备节点信息失败\n");
return -PTR_ERR(dev);
}
printk("向上提交设备节点信息成功\n");
// 申请gpio并向内核注册
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);
}
gpiono2 = gpiod_get_from_of_node(pdev->dev.of_node, "led2", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono2))
{
gpiod_put(gpiono1);
printk("申请gpio失败\n");
return -PTR_ERR(gpiono2);
}
gpiono3 = gpiod_get_from_of_node(pdev->dev.of_node, "led3", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono3))
{
gpiod_put(gpiono1);
gpiod_put(gpiono2);
printk("申请gpio失败\n");
return -PTR_ERR(gpiono3);
}
printk("申请gpio成功\n");
// 亮灯
gpiod_set_value(gpiono1, 1);
gpiod_set_value(gpiono2, 1);
gpiod_set_value(gpiono3, 1);
return 0;
}
// remove 设备和驱动分离时执行
int pdrv_remove(struct platform_device *pdev)
{
int i;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
// 注销中断****************************************
for (i = 0; i < 3; i++)
{
free_irq(irqno[i], NULL);
}
printk("注销中断成功\n");
// 灭灯
gpiod_set_value(gpiono1, 0);
gpiod_set_value(gpiono2, 0);
gpiod_set_value(gpiono3, 0);
// 注销gpio信息
gpiod_put(gpiono1);
gpiod_put(gpiono2);
gpiod_put(gpiono3);
printk("注销gpio信息成功\n");
// 销毁设备节点信息
device_destroy(cls, MKDEV(major, 0));
printk("销毁设备节点信息成功\n");
// 销毁目录
class_destroy(cls);
printk("销毁目录成功\n");
// 字符设备驱动的注销
unregister_chrdev(major, "myplatform");
printk("注销字符设备驱动成功\n");
return 0;
}
// 构建设备树匹配的表
struct of_device_id oftable[] = {
{
.compatible = "hqyj,myplatform",
},
{
.compatible = "hqyj,myplatform1",
},
{},
};
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 <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
unsigned int number;
int main(int argc, char const *argv[])
{
int fd = open("/dev/myplatform", 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;
}
- 现象: