Begin work on GPIO Led support

This commit is contained in:
Florian RICHER 2025-02-24 13:43:12 +01:00
parent 04de922e60
commit 7723b2e823
2 changed files with 78 additions and 0 deletions

7
08_gpio_led/Makefile Normal file
View file

@ -0,0 +1,7 @@
obj-m += test_module.o
all:
make -C $(LINUX_MODULES_FOLDER)/build M=$(PWD) modules
clean:
make -C $(LINUX_MODULES_FOLDER)/build M=$(PWD) clean

71
08_gpio_led/test_module.c Normal file
View file

@ -0,0 +1,71 @@
#include <linux/gpio/consumer.h>
#include <linux/gpio/driver.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
static char *device_label = "";
module_param(device_label, charp, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(device_label, "GPIO Device Label");
static char *gpio_pin_id = "";
module_param(gpio_pin_id, charp, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(gpio_pin_id, "GPIO Pin ID");
struct gpio_device *device;
struct gpio_desc *led_desc;
// Copied from https://elixir.bootlin.com/linux/v6.6.79/source/drivers/gpio/gpiolib.c#L1085
// gpio_device_find_by_label it's not available on Linux Kernel 6.6 but only with Linux Kernel 6.7+
// static int gpiochip_match_name(struct gpio_chip *gc, void *data)
// {
// const char *name = data;
// return !strcmp(gc->label, name);
// }
static int __init gpio_led_init(void) {
int ret;
// On Linux Kernel 6.7+
device = gpio_device_find_by_label(device_label);
// Otherwise
// device = gpio_device_find(device_label, gpiochip_match_name);
if (!device) {
pr_err("Failed to get device");
return -ENODEV;
}
led_desc = gpiod_get(device->dev, gpio_pin_id, GPIOD_OUT_LOW);
if (IS_ERR(led_desc)) {
pr_err("Failed to get GPIO: %ld\n", PTR_ERR(led_desc));
ret = PTR_ERR(led_desc);
goto fail;
}
gpiod_set_value(led_desc, 1);
pr_info("Bonjour! Le module est chargé.\n");
return 0;
fail:
gpio_device_put(device);
return -ENODEV;
}
static void __exit gpio_led_exit(void) {
gpiod_set_value(led_desc, 0);
gpiod_put(led_desc);
gpio_device_put(device);
pr_info("Au revoir! Le module est déchargé.\n");
}
module_init(gpio_led_init);
module_exit(gpio_led_exit);
MODULE_LICENSE("MIT License");
MODULE_AUTHOR("Florian RICHER");
MODULE_DESCRIPTION("Un module noyau pour utiliser un PIN GPIO d'une RPI en tant que LED");
MODULE_VERSION("1.0");