// SPDX-License-Identifier: GPL-2.0
/*
* gpio_led_cdev: /dev/led of exercise 29.4, driving the real LED on GPIO17
* through the GPIO registers instead of a variable.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/string.h>
#include <linux/uaccess.h>
/* GPIO block of BCM2711 (Raspberry Pi 4). */
#define GPIO_PHYS 0xFE200000UL
#define GPIO_SIZE 0x100
/* Register offsets. */
#define GPFSEL1 0x04
#define GPSET0 0x1C
#define GPCLR0 0x28
#define GPLEV0 0x34
/* The LED is wired to GPIO17. */
#define LED_PIN 17
/* Virtual address of the mapped GPIO registers. */
static void __iomem *gpio_base;
/* Device number given by the kernel. */
static dev_t devno;
/* Links the device number to the file operations. */
static struct cdev led_cdev;
/* Class that makes /dev/led appear. */
static struct class *led_class;
/* Simulated LED state from exercise 29.4: replace it with the pin. */
static bool led_on;
static int led_open(struct inode *inode, struct file *filp)
{
pr_info("open by %s pid=%d\n", current->comm, current->pid);
return 0;
}
static int led_release(struct inode *inode, struct file *filp)
{
pr_info("release\n");
return 0;
}
static ssize_t led_read(struct file *filp, char __user *user_buf,
size_t count, loff_t *ppos)
{
/* Step 3: report the real pin level instead of led_on. */
const char *state = led_on ? "on\n" : "off\n";
return simple_read_from_buffer(user_buf, count, ppos, state,
strlen(state));
}
static ssize_t led_write(struct file *filp, const char __user *user_buf,
size_t count, loff_t *ppos)
{
char kbuf[8];
size_t len = min(count, sizeof(kbuf) - 1);
if (copy_from_user(kbuf, user_buf, len))
return -EFAULT;
kbuf[len] = '\0';
/* Step 2: drive the pin instead of setting led_on. */
if (sysfs_streq(kbuf, "on"))
led_on = true;
else if (sysfs_streq(kbuf, "off"))
led_on = false;
else
return -EINVAL;
pr_info("LED %s\n", led_on ? "ON" : "OFF");
return count;
}
static const struct file_operations led_fops = {
.owner = THIS_MODULE,
.open = led_open,
.release = led_release,
.read = led_read,
.write = led_write,
};
static int __init led_init(void)
{
int ret;
/* Step 1: ioremap() the registers and make GPIO17 an output. */
ret = alloc_chrdev_region(&devno, 0, 1, "led");
if (ret)
return ret;
cdev_init(&led_cdev, &led_fops);
led_cdev.owner = THIS_MODULE;
ret = cdev_add(&led_cdev, devno, 1);
if (ret)
goto err_region;
led_class = class_create("led");
if (IS_ERR(led_class)) {
ret = PTR_ERR(led_class);
goto err_cdev;
}
if (IS_ERR(device_create(led_class, NULL, devno, NULL, "led"))) {
ret = -ENODEV;
goto err_class;
}
pr_info("/dev/led ready, major=%d\n", MAJOR(devno));
return 0;
err_class:
class_destroy(led_class);
err_cdev:
cdev_del(&led_cdev);
err_region:
unregister_chrdev_region(devno, 1);
/* Step 4: add an err_map label that calls iounmap(). */
return ret;
}
static void __exit led_exit(void)
{
device_destroy(led_class, devno);
class_destroy(led_class);
cdev_del(&led_cdev);
unregister_chrdev_region(devno, 1);
/* Step 4: turn the LED off, then iounmap(). */
pr_info("unloaded\n");
}
module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("DEVLINUX Academy");
MODULE_DESCRIPTION("/dev/led driving a real GPIO pin");