This commit is contained in:
2025-06-04 12:50:24 +01:00
parent 1af4490a39
commit f86f43b5fd

View File

@ -17,19 +17,19 @@ static int dt_probe(struct platform_device *pdev);
static void dt_remove(struct platform_device *pdev); static void dt_remove(struct platform_device *pdev);
static struct of_device_id my_driver_ids[] = { static struct of_device_id my_driver_ids[] = {
{ {
.compatible = "hottis,counter", .compatible = "hottis,counter",
}, { /* sentinel */ } }, { /* sentinel */ }
}; };
MODULE_DEVICE_TABLE(of, my_driver_ids); MODULE_DEVICE_TABLE(of, my_driver_ids);
static struct platform_driver my_driver = { static struct platform_driver my_driver = {
.probe = dt_probe, .probe = dt_probe,
.remove = dt_remove, .remove = dt_remove,
.driver = { .driver = {
.name = "my_device_driver", .name = "my_device_driver",
.of_match_table = my_driver_ids, .of_match_table = my_driver_ids,
}, },
}; };
/* GPIO variable */ /* GPIO variable */
@ -41,70 +41,70 @@ static struct gpio_desc *blue_led = NULL;
* @brief This function is called on loading the driver * @brief This function is called on loading the driver
*/ */
static int dt_probe(struct platform_device *pdev) { static int dt_probe(struct platform_device *pdev) {
struct device *dev = &pdev->dev; struct device *dev = &pdev->dev;
printk("counter - probing\n"); printk("counter - probing\n");
if(!device_property_present(dev, "red-led-gpio")) { if(!device_property_present(dev, "red-led-gpio")) {
printk("counter - Error! Device property 'red-led-gpio' not found!\n"); printk("counter - Error! Device property 'red-led-gpio' not found!\n");
return -1; return -1;
} }
if(!device_property_present(dev, "blue-led-gpio")) { if(!device_property_present(dev, "blue-led-gpio")) {
printk("counter - Error! Device property 'blue-led-gpio' not found!\n"); printk("counter - Error! Device property 'blue-led-gpio' not found!\n");
return -1; return -1;
} }
/* Init GPIO */ /* Init GPIO */
red_led = gpiod_get(dev, "red-led", GPIOD_OUT_LOW); red_led = gpiod_get(dev, "red-led", GPIOD_OUT_LOW);
if(IS_ERR(red_led)) { if(IS_ERR(red_led)) {
printk("counter - Error! Could not setup the GPIO red-led\n"); printk("counter - Error! Could not setup the GPIO red-led\n");
return -1 * IS_ERR(red_led); return -1 * IS_ERR(red_led);
} }
blue_led = gpiod_get(dev, "blue-led", GPIOD_OUT_LOW); blue_led = gpiod_get(dev, "blue-led", GPIOD_OUT_LOW);
if(IS_ERR(blue_led)) { if(IS_ERR(blue_led)) {
printk("counter - Error! Could not setup the GPIO blue-led\n"); printk("counter - Error! Could not setup the GPIO blue-led\n");
return -1 * IS_ERR(blue_led); return -1 * IS_ERR(blue_led);
} }
return 0; return 0;
} }
/** /**
* @brief This function is called on unloading the driver * @brief This function is called on unloading the driver
*/ */
static void dt_remove(struct platform_device *pdev) { static void dt_remove(struct platform_device *pdev) {
printk("counter - removing\n"); printk("counter - removing\n");
gpiod_put(red_led); gpiod_put(red_led);
gpiod_put(blue_led); gpiod_put(blue_led);
} }
/** /**
* @brief This function is called, when the module is loaded into the kernel * @brief This function is called, when the module is loaded into the kernel
*/ */
static int __init my_init(void) { static int __init my_init(void) {
printk("counter - Loading the driver...\n"); printk("counter - Loading the driver...\n");
if(platform_driver_register(&my_driver)) { if(platform_driver_register(&my_driver)) {
printk("dt_gpio - Error! Could not load driver\n"); printk("dt_gpio - Error! Could not load driver\n");
return -1; return -1;
} }
printk("counter - red led on\n"); printk("counter - red led on\n");
gpiod_set_value(red_led, 1); gpiod_set_value(red_led, 1);
return 0; return 0;
} }
/** /**
* @brief This function is called, when the module is removed from the kernel * @brief This function is called, when the module is removed from the kernel
*/ */
static void __exit my_exit(void) { static void __exit my_exit(void) {
printk("counter - Unload driver"); printk("counter - Unload driver");
printk("counter - red led off\n"); printk("counter - red led off\n");
gpiod_set_value(red_led, 0); gpiod_set_value(red_led, 0);
platform_driver_unregister(&my_driver); platform_driver_unregister(&my_driver);
} }
module_init(my_init); module_init(my_init);