Add working gpio_pin_rpi3
This commit is contained in:
parent
7723b2e823
commit
b920979d5b
3 changed files with 67 additions and 71 deletions
|
@ -1,71 +0,0 @@
|
||||||
#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");
|
|
67
08_gpio_pin_rpi3/test_module.c
Normal file
67
08_gpio_pin_rpi3/test_module.c
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
#include <linux/init.h>
|
||||||
|
#include <linux/module.h>
|
||||||
|
#include <linux/gpio/consumer.h>
|
||||||
|
|
||||||
|
// gpiochip0: GPIOs 512-565, parent: platform/3f200000.gpio, pinctrl-bcm2835
|
||||||
|
// From /sys/kernel/debug/gpio
|
||||||
|
#define RPI3_BCM2835_GPIO_OFFSET 512
|
||||||
|
#define BCM_TO_LINUX_GPIO(bcm) (RPI3_BCM2835_GPIO_OFFSET + (bcm))
|
||||||
|
|
||||||
|
static int gpio_pin = -1;
|
||||||
|
module_param(gpio_pin, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
||||||
|
MODULE_PARM_DESC(gpio_pin, "BCM GPIO pin number (e.g., 17)");
|
||||||
|
|
||||||
|
struct gpio_pin_rpi3 {
|
||||||
|
int linux_gpio;
|
||||||
|
int bcm_gpio;
|
||||||
|
struct gpio_desc *desc;
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct gpio_pin_rpi3 pin;
|
||||||
|
|
||||||
|
static int __init gpio_pin_rpi3_init(void)
|
||||||
|
{
|
||||||
|
if (gpio_pin < 0) {
|
||||||
|
pr_err("gpio_pin_rpi3: You must specify 'gpio_pin' (e.g., gpio_pin=17)\n");
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
pin.bcm_gpio = gpio_pin;
|
||||||
|
pin.linux_gpio = BCM_TO_LINUX_GPIO(pin.bcm_gpio);
|
||||||
|
|
||||||
|
// Récupération du descripteur GPIO à partir du numéro Globale Linux
|
||||||
|
pin.desc = gpio_to_desc(pin.linux_gpio);
|
||||||
|
if (!pin.desc) {
|
||||||
|
pr_err("gpio_pin_rpi3: Le GPIO %d (BCM %d) non trouvé\n", pin.linux_gpio, pin.bcm_gpio);
|
||||||
|
return -ENODEV;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Réclame le PIN GPIO
|
||||||
|
if (gpiod_direction_output(pin.desc, 0)) {
|
||||||
|
pr_err("gpio_pin_rpi3: Impossible de configurer le GPIO %d\n", pin.linux_gpio);
|
||||||
|
gpiod_put(pin.desc);
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allume la LED
|
||||||
|
gpiod_set_value(pin.desc, 1);
|
||||||
|
pr_info("gpio_pin_rpi3: GPIO %d (BCM %d) activé (LED ON)\n", pin.linux_gpio, pin.bcm_gpio);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void __exit gpio_pin_rpi3_exit(void)
|
||||||
|
{
|
||||||
|
// Turn LED off and release GPIO
|
||||||
|
gpiod_set_value(pin.desc, 0);
|
||||||
|
gpiod_put(pin.desc);
|
||||||
|
pr_info("gpio_pin_rpi3: GPIO %d (BCM %d) libéré (LED OFF)\n", pin.linux_gpio, pin.bcm_gpio);
|
||||||
|
}
|
||||||
|
|
||||||
|
module_init(gpio_pin_rpi3_init);
|
||||||
|
module_exit(gpio_pin_rpi3_exit);
|
||||||
|
|
||||||
|
MODULE_LICENSE("GPL");
|
||||||
|
MODULE_AUTHOR("Florian RICHER <florian.richer@protonmail.com>");
|
||||||
|
MODULE_DESCRIPTION("Un module noyau pour utiliser un PIN GPIO d'une RPI");
|
||||||
|
MODULE_VERSION("1.0");
|
Loading…
Add table
Reference in a new issue