|
| 1 | +/* devicetree.c - Demonstrates device tree interaction with kernel modules */ |
| 2 | + |
| 3 | +#include <linux/init.h> |
| 4 | +#include <linux/kernel.h> |
| 5 | +#include <linux/module.h> |
| 6 | +#include <linux/of.h> |
| 7 | +#include <linux/of_device.h> |
| 8 | +#include <linux/platform_device.h> |
| 9 | +#include <linux/version.h> |
| 10 | + |
| 11 | +#define DRIVER_NAME "lkmpg_devicetree" |
| 12 | + |
| 13 | +/* Structure to hold device-specific data */ |
| 14 | +struct dt_device_data { |
| 15 | + const char *label; |
| 16 | + u32 reg_value; |
| 17 | + u32 custom_value; |
| 18 | + bool has_clock; |
| 19 | +}; |
| 20 | + |
| 21 | +/* Probe function - called when device tree node matches */ |
| 22 | +static int dt_probe(struct platform_device *pdev) |
| 23 | +{ |
| 24 | + struct device *dev = &pdev->dev; |
| 25 | + struct device_node *np = dev->of_node; |
| 26 | + struct dt_device_data *data; |
| 27 | + const char *string_prop; |
| 28 | + u32 value; |
| 29 | + int ret; |
| 30 | + |
| 31 | + pr_info("%s: Device tree probe called for %s\n", DRIVER_NAME, |
| 32 | + np->full_name); |
| 33 | + |
| 34 | + /* Allocate memory for device data */ |
| 35 | + data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
| 36 | + if (!data) |
| 37 | + return -ENOMEM; |
| 38 | + |
| 39 | + /* Read a string property */ |
| 40 | + ret = of_property_read_string(np, "label", &string_prop); |
| 41 | + if (ret == 0) { |
| 42 | + data->label = string_prop; |
| 43 | + pr_info("%s: Found label property: %s\n", DRIVER_NAME, data->label); |
| 44 | + } else { |
| 45 | + data->label = "unnamed"; |
| 46 | + pr_info("%s: No label property found, using default\n", DRIVER_NAME); |
| 47 | + } |
| 48 | + |
| 49 | + /* Read a u32 property */ |
| 50 | + ret = of_property_read_u32(np, "reg", &value); |
| 51 | + if (ret == 0) { |
| 52 | + data->reg_value = value; |
| 53 | + pr_info("%s: Found reg property: 0x%x\n", DRIVER_NAME, data->reg_value); |
| 54 | + } |
| 55 | + |
| 56 | + /* Read a custom u32 property */ |
| 57 | + ret = of_property_read_u32(np, "lkmpg,custom-value", &value); |
| 58 | + if (ret == 0) { |
| 59 | + data->custom_value = value; |
| 60 | + pr_info("%s: Found custom-value property: %u\n", DRIVER_NAME, |
| 61 | + data->custom_value); |
| 62 | + } else { |
| 63 | + data->custom_value = 42; /* Default value */ |
| 64 | + pr_info("%s: No custom-value found, using default: %u\n", DRIVER_NAME, |
| 65 | + data->custom_value); |
| 66 | + } |
| 67 | + |
| 68 | + /* Check for presence of a property */ |
| 69 | + data->has_clock = of_property_read_bool(np, "lkmpg,has-clock"); |
| 70 | + pr_info("%s: has-clock property: %s\n", DRIVER_NAME, |
| 71 | + data->has_clock ? "present" : "absent"); |
| 72 | + |
| 73 | + /* Store device data for later use */ |
| 74 | + platform_set_drvdata(pdev, data); |
| 75 | + |
| 76 | + pr_info("%s: Device probe successful\n", DRIVER_NAME); |
| 77 | + return 0; |
| 78 | +} |
| 79 | + |
| 80 | +/* Remove function - called when device is removed */ |
| 81 | +#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 11, 0) |
| 82 | +static void dt_remove(struct platform_device *pdev) |
| 83 | +{ |
| 84 | + struct dt_device_data *data = platform_get_drvdata(pdev); |
| 85 | + |
| 86 | + pr_info("%s: Removing device %s\n", DRIVER_NAME, data->label); |
| 87 | + /* Cleanup is handled automatically by devm_* functions */ |
| 88 | +} |
| 89 | +#else |
| 90 | +static int dt_remove(struct platform_device *pdev) |
| 91 | +{ |
| 92 | + struct dt_device_data *data = platform_get_drvdata(pdev); |
| 93 | + |
| 94 | + pr_info("%s: Removing device %s\n", DRIVER_NAME, data->label); |
| 95 | + /* Cleanup is handled automatically by devm_* functions */ |
| 96 | + return 0; |
| 97 | +} |
| 98 | +#endif |
| 99 | + |
| 100 | +/* Device tree match table - defines compatible strings this driver supports */ |
| 101 | +static const struct of_device_id dt_match_table[] = { |
| 102 | + { |
| 103 | + .compatible = "lkmpg,example-device", |
| 104 | + }, |
| 105 | + { |
| 106 | + .compatible = "lkmpg,another-device", |
| 107 | + }, |
| 108 | + {} /* Sentinel */ |
| 109 | +}; |
| 110 | +MODULE_DEVICE_TABLE(of, dt_match_table); |
| 111 | + |
| 112 | +/* Platform driver structure */ |
| 113 | +static struct platform_driver dt_driver = { |
| 114 | + .probe = dt_probe, |
| 115 | + .remove = dt_remove, |
| 116 | + .driver = { |
| 117 | + .name = DRIVER_NAME, |
| 118 | + .of_match_table = dt_match_table, |
| 119 | + }, |
| 120 | +}; |
| 121 | + |
| 122 | +/* Module initialization */ |
| 123 | +static int __init dt_init(void) |
| 124 | +{ |
| 125 | + int ret; |
| 126 | + |
| 127 | + pr_info("%s: Initializing device tree example module\n", DRIVER_NAME); |
| 128 | + |
| 129 | + /* Register the platform driver */ |
| 130 | + ret = platform_driver_register(&dt_driver); |
| 131 | + if (ret) { |
| 132 | + pr_err("%s: Failed to register platform driver\n", DRIVER_NAME); |
| 133 | + return ret; |
| 134 | + } |
| 135 | + |
| 136 | + pr_info("%s: Module loaded successfully\n", DRIVER_NAME); |
| 137 | + return 0; |
| 138 | +} |
| 139 | + |
| 140 | +/* Module cleanup */ |
| 141 | +static void __exit dt_exit(void) |
| 142 | +{ |
| 143 | + pr_info("%s: Cleaning up device tree example module\n", DRIVER_NAME); |
| 144 | + platform_driver_unregister(&dt_driver); |
| 145 | +} |
| 146 | + |
| 147 | +module_init(dt_init); |
| 148 | +module_exit(dt_exit); |
| 149 | + |
| 150 | +MODULE_LICENSE("GPL"); |
| 151 | +MODULE_DESCRIPTION("Device tree interaction example for LKMPG"); |
0 commit comments