dtsof.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <linux/module.h>
  2. #include <linux/init.h>
  3. #include <linux/of.h>
  4. #include <linux/slab.h>
  5. struct device_node *nd; /* 设备节点 */
  6. struct property *comppro;
  7. static int __init helloworld_init(void) {
  8. int err = 0;
  9. u8 i = 0;
  10. u8 count = 0;
  11. u32 * valueByProperty = 0; // 将 u32* 改为 u32 类型
  12. pr_info("Hello world initialization!\n");
  13. // 从根节点查找 my_backlight 节点
  14. nd = of_find_node_by_path("/my_backlight");
  15. if (nd == NULL) {
  16. printk("my_backlight node not found!\n");
  17. return -EINVAL;
  18. } else {
  19. printk("my_backlight node found!\n");
  20. }
  21. // 查找 compatible 属性
  22. comppro = of_find_property(nd, "compatible", NULL);
  23. if (comppro == NULL) {
  24. printk("Compatible property not found!\n");
  25. return -EINVAL;
  26. } else {
  27. printk("compatible = %s\n", (char *)comppro->value);
  28. }
  29. #if 0
  30. // 读取 brightness-levels 属性的第 1 个元素
  31. err = of_property_read_u32_index(nd, "brightness-levels", 1, &valueByProperty);
  32. if (err == 0) {
  33. printk(KERN_INFO "valueByProperty: %u\n", valueByProperty);
  34. } else {
  35. printk(KERN_ERR "Failed to read brightness-levels property, error code: %d\n", err);
  36. }
  37. #elif 0
  38. // 获取 brightness-levels 数组的元素数量
  39. count = of_property_count_elems_of_size(nd, "brightness-levels", sizeof(u32));
  40. if (count < 0) {
  41. printk(KERN_ERR "Failed to get element count for brightness-levels\n");
  42. return count;
  43. }
  44. printk(KERN_INFO "brightness-levels count: %d\n", count);
  45. // 逐个读取 brightness-levels 数组的元素
  46. for (i = 0; i < count; i++) {
  47. err = of_property_read_u32_index(nd, "brightness-levels", i, &valueByProperty);
  48. if (err) {
  49. printk(KERN_ERR "Failed to read brightness-levels at index %d, error code: %d\n", i, err);
  50. return err;
  51. }
  52. printk(KERN_INFO "brightness-levels[%d]: %u\n", i, value);
  53. }
  54. #else
  55. // 获取 brightness-levels 数组的元素数量
  56. count = of_property_count_elems_of_size(nd, "brightness-levels", sizeof(u32));
  57. if (count < 0) {
  58. printk(KERN_ERR "Failed to get element count for brightness-levels\n");
  59. return count;
  60. }
  61. printk(KERN_INFO "brightness-levels count: %d\n", count);
  62. // 分配内存以存储 brightness-levels 的值
  63. valueByProperty = kmalloc_array(count, sizeof(u32), GFP_KERNEL);
  64. if (!valueByProperty) {
  65. printk(KERN_ERR "Failed to allocate memory for brightness-levels\n");
  66. return -ENOMEM;
  67. }
  68. // 读取 brightness-levels 属性的所有值到数组
  69. err = of_property_read_u32_array(nd, "brightness-levels", valueByProperty, count);
  70. if (err) {
  71. printk(KERN_ERR "Failed to read brightness-levels array, error code: %d\n", err);
  72. kfree(valueByProperty);
  73. return err;
  74. }
  75. // 打印 brightness-levels 数组的值
  76. for (i = 0; i < count; i++) {
  77. printk(KERN_INFO "brightness-levels[%d]: %u\n", i, valueByProperty[i]);
  78. }
  79. // 释放分配的内存
  80. kfree(valueByProperty);
  81. #endif
  82. return 0;
  83. }
  84. static void __exit helloworld_exit(void) {
  85. pr_info("Hello world exit\n");
  86. }
  87. module_init(helloworld_init);
  88. module_exit(helloworld_exit);
  89. MODULE_LICENSE("GPL");
  90. MODULE_AUTHOR("John Madieu <john.madieu@gmail.com>");
  91. MODULE_DESCRIPTION("Linux kernel module skeleton");