// SPDX-License-Identifier: GPL-2.0 #include #include #include #include #include #include /* Declare the probe and remove function */ static int dt_probe(struct platform_device *pdev); static int dt_remove(struct platform_device *pdev); static struct of_device_id my_driver_ids[] = { { .compatible = "brightlight,mydev", }, { /* sentinal */} }; MODULE_DEVICE_TABLE(of, my_driver_ids); static struct platform_driver my_driver = { .probe = dt_probe, .remove = dt_remove, .driver = { .name = "my_device_driver", .of_match_table = my_driver_ids, }, }; /** * @brief This function is called on loading the driver */ static int dt_probe(struct platform_device *pdev) { struct device *dev = &pdev->dev; const char *label; int my_value, ret; pr_info("dt_probe - Now I am in the probe function"); /* check for device properties */ if(!device_property_present(dev, "label")) { pr_err("dt_probe - Error! Device property 'label' not found\n"); return -1; } if(!device_property_present(dev, "my_value")) { pr_err("dt_probe - Error! Device property 'my_value' not found\n"); return -1; } /* Read device properties */ ret = device_property_read_string(dev, "label", &label); if(ret) { pr_err("dt_probe - Error! could not read 'label'\n"); return -1; } pr_info("dt_probe - Label: %s\n", label); ret = device_property_read_u32(dev, "my_value", &my_value); if(ret) { pr_err("dt_probe - Error! could not read 'my_value'\n"); return -1; } pr_info("dt_probe - 'my_value': %d\n", my_value); return 0; } /** * @brief This function is called on unloading the driver */ static int dt_remove(struct platform_device *pdev) { pr_info("dt_probe - Now I am in the remove function"); return 0; } /** * @brief This function is called when the module is loaded into the kernel */ static int __init my_init(void) { pr_notice("dt_probe - Loading the driver ...\n"); if(platform_driver_register(&my_driver)) { pr_err("dt_probe - Error! could not load driver\n"); return -1; } return 0; } /** * @brief This function is called when the module is removed from the kernel */ static void __exit my_exit(void) { pr_info("Goodbye, Kernel\n"); platform_driver_unregister(&my_driver); } module_init(my_init); module_exit(my_exit); /* Meta Information */ MODULE_LICENSE("GPL v2"); MODULE_AUTHOR("David D'Ulisse"); MODULE_DESCRIPTION("Read a device tree");