head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#define LED1 1
#define LED2 2
#define LED3 3
#endif
mychrdev.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include "head.h"
#include <linux/fs.h>
#include <linux/device.h>
/*
leds{
led1-gpios=<&gpioe 10 0>;
led2-gpios=<&gpiof 10 0>;
led3-gpios=<&gpioe 8 0>;
};
*/
int major;
struct class *cls;
struct device *dev;
struct device_node *nod;
struct gpio_desc *gpio1;
struct gpio_desc *gpio2;
struct gpio_desc *gpio3;
//应用层调用的驱动层操作方法
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
switch (cmd)
{
case 1:
gpiod_set_value(gpio1, !gpiod_get_value(gpio1));
break;
case 2:
gpiod_set_value(gpio2, !gpiod_get_value(gpio2));
break;
case 3:
gpiod_set_value(gpio3, !gpiod_get_value(gpio3));
break;
}
return 0;
}
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;
}
// 定义操作方法结构体变量并赋值
struct file_operations fops = {
.open = mycdev_open,
.unlocked_ioctl = mycdev_ioctl,
.release = mycdev_close,
};
static int __init mycdev_init(void)
{
// 字符设备驱动注册
major = register_chrdev(0, "mychrdev_led", &fops);
if (major < 0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功:major=%d\n", major);
// 向上提交目录
cls = class_create(THIS_MODULE, "myled");
if (IS_ERR(cls))
{
printk("向上提交目录失败\n");
return -PTR_ERR(cls);
}
printk("向上提交目录信息成功\n");
// 向上提交设备节点信息
dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "myled");
if (IS_ERR(dev))
{
printk("向上提交设备节点信息失败\n");
return -PTR_ERR(dev);
}
printk("向上提交设备节点成功\n");
// 解析设备树节点信息
nod = of_find_node_by_path("/leds");
if (NULL == nod)
{
printk("解析设备树节点失败\n");
return -EFAULT;
}
printk("解析设备树节点成功\n");
// 在设备树节点中解析出GPIO对象,并向内核申请
gpio1 = gpiod_get_from_of_node(nod, "led1-gpios", 0, GPIOD_OUT_LOW, NULL);
gpio2 = gpiod_get_from_of_node(nod, "led2-gpios", 0, GPIOD_OUT_LOW, NULL);
gpio3 = gpiod_get_from_of_node(nod, "led3-gpios", 0, GPIOD_OUT_LOW, NULL);
if (IS_ERR(gpio1))
{
printk("申请gpio1对象失败\n");
return -PTR_ERR(gpio1);
}
if (IS_ERR(gpio2))
{
printk("申请gpio2对象失败\n");
return -PTR_ERR(gpio2);
}
if (IS_ERR(gpio3))
{
printk("申请gpio3对象失败\n");
return -PTR_ERR(gpio3);
}
printk("申请gpio对象成功\n");
return 0;
}
static void __exit mycdev_exit(void)
{
// 熄灭
gpiod_set_value(gpio1, 0);
gpiod_set_value(gpio2, 0);
gpiod_set_value(gpio3, 0);
// 释放gpio编号
gpiod_put(gpio1);
gpiod_put(gpio2);
gpiod_put(gpio3);
//销毁节点信息
device_destroy(cls,MKDEV(major,0));
//销毁目录信息
class_destroy(cls);
// 注销字符设备驱动
unregister_chrdev(major, "mychrdev_led");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
test.c
#include "head.h"
#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, const char *argv[])
{
int a;
int fd1 = open("/dev/myled", O_RDWR);
while (1)
{
printf("请输入想控制的灯 1(led1) 2(led2) 3(led3)");
printf("请输入>");
scanf("%d", &a);
switch (a)
{
case 1:
ioctl(fd1, LED1);
break;;
case 2:
ioctl(fd1, LED2);
break;
case 3:
ioctl(fd1, LED3);
break;
default:
goto END;
}
}
END:
close(fd1);
return 0;
}