/
rpi_rf_mod_led.c
217 lines (178 loc) · 5.65 KB
/
rpi_rf_mod_led.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
/*-----------------------------------------------------------------------------
* Copyright (c) 2019 by Alexander Reinert
* Author: Alexander Reinert
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*---------------------------------------------------------------------------*/
#include <linux/err.h>
#include <linux/gpio.h>
#include <linux/gpio/consumer.h>
#include <linux/kernel.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/property.h>
#include <linux/slab.h>
static int red_gpio_pin = 0;
static int green_gpio_pin = 0;
static int blue_gpio_pin = 0;
static struct rpi_rf_mod_led_led *red = 0;
static struct rpi_rf_mod_led_led *green = 0;
static struct rpi_rf_mod_led_led *blue = 0;
struct rpi_rf_mod_led_led {
struct led_classdev cdev;
int gpio;
enum led_brightness brightness;
};
static void rpi_rf_mod_led_set_led_brightness(struct led_classdev *cdev, enum led_brightness b)
{
struct rpi_rf_mod_led_led *led = container_of(cdev, struct rpi_rf_mod_led_led, cdev);
led->brightness = b;
if (led->gpio != 0)
{
gpio_set_value(led->gpio, b == LED_OFF ? 0 : 1);
}
}
static enum led_brightness rpi_rf_mod_led_get_led_brightness(struct led_classdev *cdev)
{
struct rpi_rf_mod_led_led *led = container_of(cdev, struct rpi_rf_mod_led_led, cdev);
return led->brightness;
}
static struct rpi_rf_mod_led_led* rpi_rf_mod_led_createled(const char* name, bool initial)
{
struct rpi_rf_mod_led_led* led;
led = kzalloc(sizeof(struct rpi_rf_mod_led_led), GFP_KERNEL);
led->cdev.name = name;
led->cdev.brightness_set = rpi_rf_mod_led_set_led_brightness;
led->cdev.brightness_get = rpi_rf_mod_led_get_led_brightness;
led->cdev.default_trigger = initial ? "default-on" : "none";
led->gpio = 0;
led->brightness = initial ? LED_ON : LED_OFF;
led_classdev_register(NULL, &led->cdev);
return led;
}
static void rpi_rf_mod_led_set_gpio_pin(struct rpi_rf_mod_led_led* led, int gpio)
{
if (led == 0)
return;
if (led->gpio != 0)
{
gpio_free(led->gpio);
}
if (gpio != 0 && gpio_is_valid(gpio))
{
gpio_request(gpio, led->cdev.name);
gpio_direction_output(gpio, false);
gpio_set_value(gpio, led->brightness == LED_OFF ? 0 : 1);
}
else
{
gpio = 0;
}
led->gpio = gpio;
}
static void rpi_rf_mod_led_destroyled(struct rpi_rf_mod_led_led* led)
{
led_classdev_unregister(&led->cdev);
if (led->gpio != 0)
{
gpio_free(led->gpio);
}
kfree(led);
}
static int __init rpi_rf_mod_led_init(void)
{
red = rpi_rf_mod_led_createled("rpi_rf_mod:red", true);
green = rpi_rf_mod_led_createled("rpi_rf_mod:green", true);
blue = rpi_rf_mod_led_createled("rpi_rf_mod:blue", false);
rpi_rf_mod_led_set_gpio_pin(red, red_gpio_pin);
rpi_rf_mod_led_set_gpio_pin(green, green_gpio_pin);
rpi_rf_mod_led_set_gpio_pin(blue, blue_gpio_pin);
return 0;
}
static void __exit rpi_rf_mod_led_exit(void)
{
rpi_rf_mod_led_destroyled(red);
rpi_rf_mod_led_destroyled(green);
rpi_rf_mod_led_destroyled(blue);
}
module_init(rpi_rf_mod_led_init);
module_exit(rpi_rf_mod_led_exit);
static int rpi_rf_mod_led_set_param(const char *val, const struct kernel_param *kp)
{
int gpio, ret;
struct rpi_rf_mod_led_led *led;
ret = kstrtoint(val, 10, &gpio);
if (ret != 0)
return -EINVAL;
if (strcmp(kp->name, "red_gpio_pin") == 0)
{
red_gpio_pin = gpio;
led = red;
}
else if (strcmp(kp->name, "green_gpio_pin") == 0)
{
green_gpio_pin = gpio;
led = green;
}
else if (strcmp(kp->name, "blue_gpio_pin") == 0)
{
blue_gpio_pin = gpio;
led = blue;
}
else
{
return -ENODEV;
}
rpi_rf_mod_led_set_gpio_pin(led, gpio);
return 0;
}
static int rpi_rf_mod_led_get_param(char *buffer, const struct kernel_param *kp)
{
int value = 0;
if (strcmp(kp->name, "red_gpio_pin") == 0)
{
value = red->gpio;
}
else if (strcmp(kp->name, "green_gpio_pin") == 0)
{
value = green->gpio;
}
else if (strcmp(kp->name, "blue_gpio_pin") == 0)
{
value = blue->gpio;
}
else
{
return -ENODEV;
}
return sprintf(buffer, "%d", value);
}
static const struct kernel_param_ops rpi_rf_mod_led_gpio_param_ops = {
.set = rpi_rf_mod_led_set_param,
.get = rpi_rf_mod_led_get_param,
};
module_param_cb(red_gpio_pin, &rpi_rf_mod_led_gpio_param_ops, NULL, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(red_gpio_pin, "GPIO Pin of red LED");
module_param_cb(green_gpio_pin, &rpi_rf_mod_led_gpio_param_ops, NULL, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(green_gpio_pin, "GPIO Pin of green LED");
module_param_cb(blue_gpio_pin, &rpi_rf_mod_led_gpio_param_ops, NULL, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(blue_gpio_pin, "GPIO Pin of blue LED");
MODULE_AUTHOR("Alexander Reinert <alex@areinert.de>");
MODULE_DESCRIPTION("GPIO LED driver for RPI-RF-MOD");
MODULE_VERSION("1.4");
MODULE_LICENSE("GPL");
MODULE_ALIAS("rpi_rf_mod_led");