Files
counter/driver/counter.c
2025-06-13 07:56:37 +01:00

167 lines
4.0 KiB
C

#include <linux/module.h>
#include <linux/init.h>
#include <linux/mod_devicetable.h>
#include <linux/property.h>
#include <linux/platform_device.h>
#include <linux/of_device.h>
#include <linux/gpio/consumer.h>
#include <linux/spi/spi.h>
/* Meta Information */
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Wolfgang Hottgenroth");
MODULE_DESCRIPTION("A simple LKM to parse the device tree for a specific device and its properties");
/* Declate the probe and remove functions */
static int dt_leds_probe(struct platform_device *pdev);
static void dt_leds_remove(struct platform_device *pdev);
static struct of_device_id leds_driver_ids[] = {
{
.compatible = "hottis,leds",
}, { /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, leds_driver_ids);
static struct platform_driver leds_driver = {
.probe = dt_leds_probe,
.remove = dt_leds_remove,
.driver = {
.name = "leds_device_driver",
.of_match_table = leds_driver_ids,
},
};
/* GPIO variable */
static struct gpio_desc *red_led = NULL;
static struct gpio_desc *blue_led = NULL;
/* spi stuff */
static int dt_ls7366r_probe(struct spi_device *client);
static void dt_ls7366r_remove(struct spi_device *client);
static struct of_device_id ls7366r_driver_ids[] = {
{
.compatible = "hottis,ls7366r",
}, {}
};
MODULE_DEVICE_TABLE(of, ls7366r_driver_ids);
static struct spi_device_id ls7366r[] = {
{ "ls7366r", 0 },
{},
};
MODULE_DEVICE_TABLE(spi, ls7366r);
static struct spi_driver ls7366r_driver = {
.probe = dt_ls7366r_probe,
.remove = dt_ls7366r_remove,
.id_table = ls7366r,
.driver = {
.name = "ls7366r_device_driver",
.of_match_table = ls7366r_driver_ids,
},
};
/**
* @brief This function is called on loading the driver
*/
static int dt_leds_probe(struct platform_device *pdev) {
struct device *dev = &pdev->dev;
printk("counter - leds probing\n");
if(!device_property_present(dev, "red-led-gpio")) {
printk("counter - Error! Device property 'red-led-gpio' not found!\n");
return -1;
}
if(!device_property_present(dev, "blue-led-gpio")) {
printk("counter - Error! Device property 'blue-led-gpio' not found!\n");
return -1;
}
/* Init GPIO */
red_led = gpiod_get(dev, "red-led", GPIOD_OUT_LOW);
if(IS_ERR(red_led)) {
printk("counter - Error! Could not setup the GPIO red-led\n");
return -1 * IS_ERR(red_led);
}
blue_led = gpiod_get(dev, "blue-led", GPIOD_OUT_LOW);
if(IS_ERR(blue_led)) {
printk("counter - Error! Could not setup the GPIO blue-led\n");
return -1 * IS_ERR(blue_led);
}
return 0;
}
/**
* @brief This function is called on unloading the driver
*/
static void dt_leds_remove(struct platform_device *pdev) {
printk("counter - leds removing\n");
gpiod_put(red_led);
gpiod_put(blue_led);
}
static int dt_ls7366r_probe(struct spi_device *client) {
printk("counter - ls7366r probing\n");
int ret = spi_setup(client);
if (ret < 0) {
printk("counter - Error! Failed to set up the SPI bus\n");
return ret;
}
return 0;
}
static void dt_ls7366r_remove(struct spi_device *client) {
printk("counter - ls7366r removing\n");
}
/**
* @brief This function is called, when the module is loaded into the kernel
*/
static int __init my_init(void) {
printk("counter - Loading the leds driver...\n");
if(platform_driver_register(&leds_driver)) {
printk("dt_gpio - Error! Could not load driver\n");
return -1;
}
printk("counter - Loading the ls7366r driver...\n");
if (spi_register_driver(&ls7366r_driver)) {
printk("counter - Error! Could not load spi driver\n");
return -1;
}
printk("counter - red led on\n");
gpiod_set_value(red_led, 1);
return 0;
}
/**
* @brief This function is called, when the module is removed from the kernel
*/
static void __exit my_exit(void) {
printk("counter - red led off\n");
gpiod_set_value(red_led, 0);
printk("counter - Unloading the leds driver...\n");
platform_driver_unregister(&leds_driver);
printk("counter - Unloading the ls7366r driver...\n");
spi_unregister_driver(&ls7366r_driver);
}
module_init(my_init);
module_exit(my_exit);