Linux 驱动编写

GPIO驱动编写

  • 头文件
1
2
3
4
5
6
#include <linux/module.h>
#include <linux/init.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/platform_device.h>
#include <dt-bindings/gpio/gpio.h>

module.h :驱动必须加载头文件,动态加载到内核
init.h : 模块的初始化的宏定义 以及一些其他函数的初始化函数,可以使用module_init
of.h ,of_gpio.h:设备树相关头文件,含有对设备数操作函数
platform_device.h:抽象平台设备,用于内核抽象匹配写的驱动

函数解析
  • 加载和卸载驱动

module_init()
module_exit()

这两个函数分别在加载和卸载驱动时被调用。流程如下
insmod -> init_module -> module_init;
rmmod -> cleanup_module -> module_exit;
insmod,rmmod为加载和卸载驱动的指令

  • 加载设备

参数: 设备类型 ,设备表
MODULE_DEVICE_TABLE()

  • 创建设备节点

CLASS_ATTR_RW(test)
其作用相当于生成了以下的设备,linux 下ATTR_ 系列函数可以节省代码量
struct device_attribute dev_attr_(test) = {
.attr = {.name = __stringify(test),
.mode = VERIFY_OCTAL_PERMISSIONS((S_IWUSR | S_IRUGO)) },
.show = test_show,
.store = test_store,
}

示例代码

功能:在/sys/class/ 生成一个cs_gpio 目录,通过cs_gpio_value 控制对应引脚

后续可加入gpio组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <linux/module.h>
#include <linux/init.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <dt-bindings/gpio/gpio.h>


#define DEBUG
//#undef DEBUG
#ifdef DEBUG

#define AXJ_LOG(fmt, args...) pr_info("[E GPIO]: " fmt, ##args)
#else
#define AXJ_LOG(fmt, args...)
#endif

#define HIGH "1"
#define LOW "0"

struct cs_gpio{
int io; // io 设备号
int enable; // 控制引脚
}cs_gpio;

// 声明
static struct class cs_gpio_class;

static int cs_gpio_probe(struct platform_device* pdev)
{
int ret,gpio;
enum of_gpio_flags flag;
struct cs_gpio *gpio_info;
struct device_node *cs_gpio_node = pdev->dev.of_node;

// 申请分配gpio内存
gpio_info = devm_kzalloc(&pdev->dev, sizeof(struct cs_gpio *), GFP_KERNEL);
if(!gpio_info)
return -ENOMEM;

// 获取dts配置 gpio id 和flag
gpio = of_get_named_gpio_flags(cs_gpio_node, "gpios", 0, &flag);
if (!gpio_is_valid(gpio)) {
dev_err(&pdev->dev, "gpios: %d is invalid\n", gpio);
return -ENODEV;
}

// 申请控制gpio
if (gpio_request(gpio, "cs-gpio")) {
dev_err(&pdev->dev, "cs-gpio: %d request failed!\n", gpio);
gpio_free(gpio);
return -ENODEV;
}

// 初始化全局 cs_gpio值
cs_gpio.io = gpio;
cs_gpio.enable = (flag == OF_GPIO_ACTIVE_LOW) ? 0:1;

gpio_info->io = gpio;
gpio_info->enable = (flag == OF_GPIO_ACTIVE_LOW) ? 0:1;

// 设置输出模式
gpio_direction_output(gpio_info->io, gpio_info->enable);
AXJ_LOG("terry gpio set output\n");

// 注册设备
ret= class_register(&cs_gpio_class);
if(ret < 0) {
return -EINVAL;
}


return 0;
}

// 查询gpio引脚
static ssize_t cs_gpio_value_show(struct class *dev,
struct class_attribute *attr, char *buf){

return sprintf(buf, "%d\n", gpio_get_value(cs_gpio.io));

}
// 设置gpio引脚
static ssize_t cs_gpio_value_store(struct class *dev,
struct class_attribute *attr,
const char *buf, size_t count){

if(!strncmp(buf, HIGH, strlen(HIGH))) {
gpio_set_value(cs_gpio.io, 1);

} else if(!strncmp(buf, LOW, strlen(LOW))) {
gpio_set_value(cs_gpio.io, 0);
}

return count;
}
// 自动生成子设备节点,并有show,store两个接口,对应上面两函数
static CLASS_ATTR_RW(cs_gpio_value);


static struct attribute *cs_gpio_class_attrs[] = {
&class_attr_cs_gpio_value.attr,
NULL,
};

ATTRIBUTE_GROUPS(cs_gpio_class);

static struct class cs_gpio_class = {
.name = "cs_gpio",
.owner = THIS_MODULE,
.class_groups = cs_gpio_class_groups,
};
// compatible设置匹配设备树中名称,使用of_match_ptr()匹配
static struct of_device_id cs_gpio_of_match[] = {
{.compatible = "cs-gpio"},
{}
};
// 加载设备 参数: 设备类型 ,设备表
MODULE_DEVICE_TABLE(of, cs_gpio_of_match);

static struct platform_driver cs_gpio_driver = {
.probe = cs_gpio_probe,
.driver = {
.name = "cs_gpio_ctrl",
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(cs_gpio_of_match),
},
};

static int __init cs_gpio_init(void)
{
AXJ_LOG(" gpio init!");
return platform_driver_register(&cs_gpio_driver);
}
module_init(cs_gpio_init);

static void __exit cs_gpio_exit(void)
{
AXJ_LOG(" gpio exit!");
platform_driver_unregister(&cs_gpio_driver);
}
module_exit(cs_gpio_exit);

MODULE_DESCRIPTION("HNCS gpio driver");
MODULE_LICENSE("GPL");