#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/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include "led.h"
struct resource *res;
unsigned int irqno;
struct gpio_desc *gpiono[3];
struct device_node *dnode;
int major;
struct class *cls;
struct device *dev;
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 ret;
}
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;
}
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,
};
// 定义相关成员函数
// probe函数用于匹配设备成功后执行
int pdrv_probe(struct platform_device *pdev) // 当和设备匹配成功之后执行probe
{
// 字符设备驱动注册
major = register_chrdev(0, "mycdev", &fops);
if (major < 0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功:major=%d\n", major);
// 向上提交目录
cls = class_create(THIS_MODULE, "mycdev");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录成功\n");
// 向上提交设备节点信息
dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "mycdev");
if (IS_ERR(dev))
{
printk("向上提交设备节点失败\n");
return -PTR_ERR(dev);
}
printk("向上提交设备节点成功\n");
// 解析设备树节点
dnode = of_find_node_by_name(NULL, "myplatform");
if (dnode == NULL)
{
printk("解析设备树节点失败\n");
return -ENOMEM;
}
printk("解析设备树节点成功\n");
// 解析MEM类型的资源
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (res == NULL)
{
printk("解析资源失败%d\n", __LINE__);
return -ENOMEM;
}
// 解析中断类型的资源
irqno = platform_get_irq(pdev, 0);
if (irqno < 0)
{
printk("解析资源失败%d\n", __LINE__);
return -ENOMEM;
}
printk("mem:%x irq:%d\n", res->start, irqno);
// LED1
// 解析出GPIO编号并申请
gpiono[0] = gpiod_get_from_of_node(pdev->dev.of_node, "led1", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono[0]))
{
printk("GPIO信息解析失败\n");
return -PTR_ERR(gpiono[0]);
}
// LED2
gpiono[1] = gpiod_get_from_of_node(pdev->dev.of_node, "led2", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono[1]))
{
printk("GPIO信息解析失败\n");
return -PTR_ERR(gpiono[1]);
}
// LED3
gpiono[2] = gpiod_get_from_of_node(pdev->dev.of_node, "led3", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpiono[2]))
{
printk("GPIO信息解析失败\n");
return -PTR_ERR(gpiono[2]);
}
return 0;
}
// remove用于和设备分离时执行
int pdrv_remove(struct platform_device *pdev) // 当设备和驱动分离时执行remove
{
// 灭灯 LED1
gpiod_set_value(gpiono[0], 0);
// 释放GPIO编号
gpiod_put(gpiono[0]);
// 灭灯 LED2
gpiod_set_value(gpiono[1], 0);
// 释放GPIO编号
gpiod_put(gpiono[1]);
// 灭灯 LED1
gpiod_set_value(gpiono[2], 0);
// 释放GPIO编号
gpiod_put(gpiono[2]);
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
// 在驱动中创建一个用于设备树匹配的表
struct of_device_id oftable[] = {
{
.compatible = "hqyj,myplatform",
},
{
.compatible = "hqyj,myplatform1",
},
{
.compatible = "hqyj,myplatform2",
},
{},
};
// 分配对象并初始化
struct platform_driver pdrv = {
.probe = pdrv_probe,
.remove = pdrv_remove,
.driver = {
.name = "aaaaa",
.of_match_table = oftable, // 指定设备树匹配表的首地址
},
};
// 一键注册宏,代替入口出口
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");
基于platform驱动框架编写LED灯的驱动
最新推荐文章于 2024-08-16 21:46:01 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)