-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpioserdev.c
314 lines (265 loc) · 8.74 KB
/
gpioserdev.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/gpio.h>
#include <linux/delay.h>
#include <linux/ioctl.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
// GPIO pin definitions
static int gpioserdev_strobe_pin;
static int gpioserdev_data_pin;
// Bit transaction delay
#define GPIOSERDEV_DELAY_US 750
// IOCTL command definitions
#define GPIOSERDEV_IOC_MAGIC 'k'
#define GPIOSERDEV_STRBPIN _IOW(GPIOSERDEV_IOC_MAGIC, 1, int)
#define GPIOSERDEV_DATAPIN _IOW(GPIOSERDEV_IOC_MAGIC, 2, int)
// Device structure
struct gpioserdev_t {
dev_t devnum;
struct class *class;
struct cdev cdev;
};
struct gpioserdev_t gpioserdev;
// Configurable module parameters
static int delay_us = GPIOSERDEV_DELAY_US;
static char byte_order[4] = "lsb"; // Default value
// Function prototypes
static int gpioserdev_open(struct inode *device_file, struct file *instance);
static int gpioserdev_close(struct inode *device_file, struct file *instance);
static ssize_t gpioserdev_write(struct file *File, const char *user_buffer, size_t count, loff_t *offs);
static long gpioserdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
static int gpioserdev_pinsetup(void);
static void gpioserdev_pinfree(void);
static int gpioserdev_validate_byteorder(const char *val, const struct kernel_param *kp);
static int gpioserdev_get_byteorder(char *buffer, const struct kernel_param *kp);
static struct file_operations gpioserdev_fops = {
.owner = THIS_MODULE,
.open = gpioserdev_open,
.release = gpioserdev_close,
.write = gpioserdev_write,
.unlocked_ioctl = gpioserdev_ioctl,
};
static const struct kernel_param_ops param_ops = {
.set = gpioserdev_validate_byteorder,
.get = gpioserdev_get_byteorder,
};
static int gpioserdev_validate_byteorder(const char *val, const struct kernel_param *kp)
{
if (strcmp(val, "lsb\n") == 0 || strcmp(val, "msb\n") == 0) {
strscpy((char *)kp->arg, val, sizeof(byte_order)); // Update value
return 0;
}
pr_err("Invalid value for %s. Allowed values: lsb, msb\n", kp->name);
return -EINVAL;
}
static int gpioserdev_get_byteorder(char *buffer, const struct kernel_param *kp)
{
return sprintf(buffer, "%s\n", byte_order);
}
/**
* Sets up GPIO pins by getting their numbers from the device tree and configuring them as outputs.
* Handles pin allocation, validation, and initial low-state setup.
*/
static int gpioserdev_pinsetup(void) {
struct device_node *np;
int ret;
np = of_find_node_by_name(NULL, "gpioserdev");
if (!np) {
printk("gpioserdev: device tree node not found\n");
return -ENODEV;
}
gpioserdev_strobe_pin = of_get_named_gpio(np, "strobe-gpios", 0);
if (!gpio_is_valid(gpioserdev_strobe_pin)) {
printk("gpioserdev: invalid strobe GPIO\n");
return -ENODEV;
}
gpioserdev_data_pin = of_get_named_gpio(np, "data-gpios", 0);
if (!gpio_is_valid(gpioserdev_data_pin)) {
printk("gpioserdev: invalid data GPIO\n");
return -ENODEV;
}
ret = gpio_request(gpioserdev_strobe_pin, "gpio-strobe");
if (ret) {
printk("gpioserdev: failed to request strobe GPIO\n");
return ret;
}
ret = gpio_direction_output(gpioserdev_strobe_pin, 0);
if (ret) {
printk("gpioserdev: failed to set strobe GPIO direction\n");
gpio_free(gpioserdev_strobe_pin);
return ret;
}
ret = gpio_request(gpioserdev_data_pin, "gpio-data");
if (ret) {
printk("gpioserdev: failed to request data GPIO\n");
gpio_free(gpioserdev_strobe_pin);
return ret;
}
ret = gpio_direction_output(gpioserdev_data_pin, 0);
if (ret) {
printk("gpioserdev: failed to set data GPIO direction\n");
gpio_free(gpioserdev_strobe_pin);
gpio_free(gpioserdev_data_pin);
return ret;
}
return 0;
}
/**
* Releases GPIO pins, setting them to low and freeing kernel resources.
* Ensures clean GPIO state when the module is unloaded.
*/
static void gpioserdev_pinfree(void) {
gpio_set_value(gpioserdev_strobe_pin, 0);
gpio_set_value(gpioserdev_data_pin, 0);
gpio_free(gpioserdev_strobe_pin);
gpio_free(gpioserdev_data_pin);
}
/**
* Opens the gpioserdev device file.
*/
int gpioserdev_open(struct inode *device_file, struct file *instance) {
return 0;
}
/**
* Closes the gpioserdev device file.
*/
int gpioserdev_close(struct inode *device_file, struct file *instance) {
return 0;
}
// IOCTL handler function
static long gpioserdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
unsigned long value;
if(copy_from_user(&value ,(int32_t*) arg, sizeof(value))) {
printk("data write : Err!\n");
}
switch(cmd) {
case GPIOSERDEV_STRBPIN:
gpio_set_value(gpioserdev_strobe_pin, value);
printk("set strobe_pin value = %ld\n", value);
break;
case GPIOSERDEV_DATAPIN:
gpio_set_value(gpioserdev_data_pin, value);
printk("set data_pin value = %ld\n", value);
break;
default:
break; // Invalid command
}
return 0;
}
/**
* Writes a byte of data to the GPIO pins.
*
* This function takes a byte of data and writes it to the GPIO data pin, one bit
* at a time. It sets the strobe pin high, waits for the delay_us
* delay, then sets the strobe pin low again. This sequence is repeated for each
* bit in the byte, from the least significant bit to the most significant bit.
*
* After all 8 bits have been written, the function sets the data pin low.
*/
void gpioserdev_write_byte(char byte) {
char value;
int i;
if (strcmp(byte_order, "lsb") == 0) {
for (i = 0; i < 8; i++) {
value = (byte >> i) & 0x01;
gpio_set_value(gpioserdev_data_pin, value); // set the data
gpio_set_value(gpioserdev_strobe_pin, 1); // set strobe
udelay(delay_us);
gpio_set_value(gpioserdev_strobe_pin, 0); // clear strobe
udelay(delay_us);
}
} else {
for (i = 7; i >= 0; i--) {
value = (byte >> i) & 0x01;
gpio_set_value(gpioserdev_data_pin, value); // set the data
gpio_set_value(gpioserdev_strobe_pin, 1); // set strobe
udelay(delay_us);
gpio_set_value(gpioserdev_strobe_pin, 0); // clear strobe
udelay(delay_us);
}
}
gpio_set_value(gpioserdev_data_pin, 0);
}
/**
* Handles write system calls to the device file.
* Copies user data and calls byte writing function.
*/
ssize_t gpioserdev_write(struct file *File, const char *user_buffer, size_t count, loff_t *offs) {
int to_copy, not_copied, delta;
char value;
to_copy = min(count, sizeof(value));
not_copied = copy_from_user(&value, user_buffer, to_copy);
gpioserdev_write_byte(value);
delta = to_copy - not_copied;
return delta;
}
/**
* Initialize the gpioserdev module
*/
static int __init gpioserdev_init(void)
{
if(alloc_chrdev_region(&gpioserdev.devnum, 0, 1, "gpioserdev") < 0) {
printk("Device number could not be allocated!\n");
return -1;
}
// Initialize the character device
if(alloc_chrdev_region(&gpioserdev.devnum, 0, 1, "gpioserdev") < 0) {
printk("Device number could not be allocated!\n");
return -1;
}
if((gpioserdev.class = class_create("gpioserdev_class")) == NULL) {
printk("Device class can not be created!\n");
goto class_cleanup;
}
if(device_create(gpioserdev.class, NULL, gpioserdev.devnum, NULL, "gpioserdev") == NULL) {
printk("Can not create device file!\n");
goto device_cleanup;
}
cdev_init(&gpioserdev.cdev, &gpioserdev_fops);
if(cdev_add(&gpioserdev.cdev, gpioserdev.devnum, 1) == -1) {
printk("Registering of device to kernel failed!\n");
goto cdevadd_cleanup;
}
// Setup GPIO pins
if (gpioserdev_pinsetup() != 0)
goto gpiopin_cleanup;
printk("gpioserdev initialized successfully\n");
return 0;
gpiopin_cleanup:
cdev_del(&gpioserdev.cdev);
cdevadd_cleanup:
device_destroy(gpioserdev.class, gpioserdev.devnum);
device_cleanup:
class_destroy(gpioserdev.class);
class_cleanup:
unregister_chrdev_region(gpioserdev.devnum, 1);
return -1;
}
/**
* Cleanup and unregister the gpioserdev module
*/
static void __exit gpioserdev_exit(void)
{
gpioserdev_pinfree();
cdev_del(&gpioserdev.cdev);
device_destroy(gpioserdev.class, gpioserdev.devnum);
class_destroy(gpioserdev.class);
unregister_chrdev_region(gpioserdev.devnum, 1);
printk("gpioserdev module removed\n");
}
module_init(gpioserdev_init);
module_exit(gpioserdev_exit);
module_param(delay_us, int, 0644);
MODULE_PARM_DESC(delay_us, "Bit transaction delay in microseconds");
module_param_cb(data_order, ¶m_ops, byte_order, 0644);
MODULE_PARM_DESC(data_order, "Byte order: lsb or msb");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Pravin Raghul S");
MODULE_DESCRIPTION("A Custom GPIO Serial Communication Driver");