-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio-sysfs.c
248 lines (198 loc) · 6.96 KB
/
gpio-sysfs.c
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* -----------------------------------------------------------------------------
* Filename : gpio-sysfs.c
* Description: A GPIO sysfs driver
* Created : Monday 15 August 2022 01:36:37 IST
* Version : 1.0
* Author : Harish Kumar, Embedded Linux Developer, harishec031@gmail.com
*
* Copyright (c) 2022 Harish Kumar
* ----------------------------------------------------------------------------- */
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/io.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/gpio/consumer.h>
#undef pr_fmt
#define pr_fmt(fmt) "%s :" fmt, __func__
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Harish Kumar <harishec031@gmail.com>");
MODULE_DESCRIPTION("A gpio sysfs driver");
MODULE_INFO(board, "Beaglebone Black REV A5");
/* Device private data structure */
struct gpiodev_private_data {
char label[20];
struct gpio_desc *desc;
};
/* Driver private data structure */
struct gpiodrv_private_data {
int total_devices;
struct class *class_gpio;
struct device **dev;
};
struct gpiodrv_private_data gpio_drv_data;
/* create 3 device attributes */
ssize_t direction_show(struct device *dev, struct device_attribute *attr, char *buf) {
int dir;
char *dir_str;
struct gpiodev_private_data *dev_data = dev_get_drvdata(dev);;
dir = gpiod_get_direction(dev_data->desc);
if(dir < 0 )
return dir;
/* if dir = 0, then show "out", if dir = 1, then show "in" */
dir_str = (dir ? "IN" : "OUT");
return sprintf(buf, "%s\n", dir_str);
}
ssize_t direction_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) {
int ret;
struct gpiodev_private_data *dev_data = dev_get_drvdata(dev);;
if(sysfs_streq(buf, "IN"))
ret = gpiod_direction_input(dev_data->desc);
else if(sysfs_streq(buf, "OUT"))
ret = gpiod_direction_output(dev_data->desc, 0);
else
ret = -EINVAL;
return ret ? : count;
}
ssize_t value_show(struct device *dev, struct device_attribute *attr, char *buf) {
int value;
struct gpiodev_private_data *dev_data = dev_get_drvdata(dev);;
value = gpiod_get_value(dev_data->desc);
return sprintf(buf, "%d\n", value);
}
ssize_t value_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) {
int ret;
long value;
struct gpiodev_private_data *dev_data = dev_get_drvdata(dev);;
ret = kstrtol(buf, 0, &value);
if(ret)
return ret;
gpiod_set_value(dev_data->desc, value);
return count;
}
ssize_t label_show(struct device *dev, struct device_attribute *attr, char *buf) {
struct gpiodev_private_data *dev_data = dev_get_drvdata(dev);
return sprintf(buf, "%s\n", dev_data->label);
}
static DEVICE_ATTR_RW(direction);
static DEVICE_ATTR_RW(value);
static DEVICE_ATTR_RO(label);
static struct attribute *gpio_attrs[] = {
&dev_attr_direction.attr,
&dev_attr_value.attr,
&dev_attr_label.attr,
NULL
};
static struct attribute_group gpio_attr_group = {
.attrs = gpio_attrs
};
static const struct attribute_group *gpio_attr_groups[] = {
&gpio_attr_group,
NULL
};
int gpio_sysfs_probe(struct platform_device *pdev) {
int i = 0, ret = 0;
struct device *dev = &pdev->dev;
const char *name;
struct device *dev_sysfs;
/* parent device node */
struct device_node *parent = pdev->dev.of_node;
struct device_node *child = NULL;
struct gpiodev_private_data *dev_data;
gpio_drv_data.total_devices = of_get_child_count(parent);
if(!gpio_drv_data.total_devices) {
dev_err(dev, "No devices found\n");
return -EINVAL;
}
dev_info(dev, "Total devices found = %d\n", gpio_drv_data.total_devices);
gpio_drv_data.dev = devm_kzalloc(dev, sizeof(struct device *) * gpio_drv_data.total_devices, GFP_KERNEL);
for_each_available_child_of_node(parent, child) {
dev_data = devm_kzalloc(dev, sizeof(*dev_data), GFP_KERNEL);
if(!dev_data) {
dev_err(dev, "Cannot allocate memory\n");
return -ENOMEM;
}
/* get label information of child nodes */
if(of_property_read_string(child, "label", &name)) {
dev_warn(dev, "Missing label information\n");
snprintf(dev_data->label, sizeof(dev_data->label), "unknowngpio%d", i);
} else {
strcpy(dev_data->label, name);
dev_info(dev, "GPIO label: %s\n", dev_data->label);
}
/* acquire a GPIO */
dev_data->desc = devm_fwnode_get_gpiod_from_child(dev, "bone", &child->fwnode, \
GPIOD_ASIS, dev_data->label);
if(IS_ERR(dev_data->desc)) {
ret = PTR_ERR(dev_data->desc);
if(ret == -ENOENT) {
dev_err(dev, "No gpio has been assigned to the requested function and/or index\n");
return ret;
}
}
/* set the GPIO direction to output */
ret = gpiod_direction_output(dev_data->desc, 0);
if(ret) {
dev_err(dev, "gpio direction set failed\n");
return ret;
}
/* create devices under /sys/class/bone_gpios */
gpio_drv_data.dev[i] = device_create_with_groups(gpio_drv_data.class_gpio,
dev,
0,
dev_data,
gpio_attr_groups,
dev_data->label);
if(IS_ERR(dev_sysfs)) {
dev_err(dev, "Error in device_create\n");
return PTR_ERR(dev_sysfs);
}
++i;
}
return 0;
}
int gpio_sysfs_remove(struct platform_device *pdev) {
int i;
dev_info(&pdev->dev, "Remove called\n");
for(i = 0; i < gpio_drv_data.total_devices; i++) {
device_unregister(gpio_drv_data.dev[i]);
}
return 0;
}
struct of_device_id gpio_device_match[] = {
{.compatible = "org,bone-gpio-sysfs"},
{}
};
struct platform_driver gpiosysfs_platform_driver = {
.probe = gpio_sysfs_probe,
.remove = gpio_sysfs_remove,
.driver = {
.name = "bone-gpio-sysfs",
.of_match_table = of_match_ptr(gpio_device_match)
}
};
int __init gpio_sysfs_init(void)
{
/* create a class named bone_gpios */
gpio_drv_data.class_gpio = class_create(THIS_MODULE, "bone_gpios");
if(IS_ERR(gpio_drv_data.class_gpio)) {
pr_err("Error in creating class \n");
return PTR_ERR(gpio_drv_data.class_gpio);
}
platform_driver_register(&gpiosysfs_platform_driver);
pr_info("Module load success\n");
return 0;
}
void __exit gpio_sysfs_exit(void) {
platform_driver_unregister(&gpiosysfs_platform_driver);
class_destroy(gpio_drv_data.class_gpio);
}
module_init(gpio_sysfs_init);
module_exit(gpio_sysfs_exit);