OF提供的函數主要集中在drivers/of/目錄下,有address.c,base.c,device.c,fdt.c,irq.c,platform.c等等
1,根據deice_node結構的full_name參數,在全局鏈表of_allnodes中,查找合適的device_node
struct device_node *of_find_node_by_path(const char *path)
2,根據property結構的name參數,在指定的device node中查找合適的property
struct property *of_find_property(const struct device_node *np,const char *name,int *lenp)
3,根據compat參數與device node的compatible匹配,返回匹配度
int of_device_is_compatible(const struct device_node *device,const char *compat)
4,獲得父節點的device node
struct device_node *of_get_parent(const struct device_node *node)
5,根據屬性名propname,讀出該屬性的數組中sz個屬性值給out_values
int of_property_read_u32_array(const struct device_node *np,const char *propname,,u8 *out_values, size_t sz)
6,讀取該設備的第index個irq號
unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
現在要在dts中添加如下節點,并且在代碼中獲取到如下信息:
test_nod@12345678{
compatible = "test,farsight";
reg = <0xa2345678 0x24
0xb3456780 0x24>;
testprop,mytest;
test_list_string = "red fish", "blue fish";
interrupt-parent = <&gpx1>; // 因為按鍵接到了gpx1_1
interrupts = <1 2>;
// 因為按鍵接到了gpx1_1,如果接到gpx2_1,那么就是<2 2>
};
[html] view plain copy
1. #include <linux/init.h>
2. #include <linux/module.h>
3. #include <linux/of.h>
4. #include <linux/of_irq.h>
5. #include <linux/interrupt.h>
6.
7.
8. #define U32_DATA_LEN 4
9.
10. static int is_good;
11. static int irqno;
12.
13. irqreturn_t key_irq_handler(int irqno, void *devid)
14. {
15. printk("------------------------key pressed \n");
16. return IRQ_HANDLED;
17. }
18.
19. static int __init dt_drv_init(void)
20. {
21. /*
22. test_nod@12345678{
23. compatible = "farsight,test";
24. reg = <0x12345678 0x24
25. 0x87654321 0x24>;
26. testprop,mytest;
27. test_list_string = "red fish","fly fish", "blue fish";
28. interrupt-parent = <&gpx1>;
29. interrupts = <1 4>;
30.
31. };
32. */
33.
34. // 在代碼中獲取節點的所有信息
35. //先把節點獲取到
36. struct device_node *np = NULL;
37.
38.
39. np = of_find_node_by_path("/test_nod@12345678");
40. if(np){
41. printk("find test node ok\n");
42. printk("node name = %s\n", np->name);
43. printk("node full name = %s\n", np->full_name);
44.
45. }else{
46. printk("find test node failed\n");
47.
48. }
49.
50. //獲取到節點中的屬性
51. struct property *prop = NULL;
52. prop = of_find_property(np, "compatible",NULL);
53. if(prop)
54. {
55. printk("find compatible ok\n");
56. printk("compatible value = %s\n", prop->value);
57. printk("compatible name = %s\n", prop->name);
58. }else{
59. printk("find compatible failed\n");
60.
61. }
62.
63. if(of_device_is_compatible(np, "farsight,test"))
64. {
65. printk("we have a compatible named farsight,test\n");
66. }
67.
68. //讀取到屬性中的整數的數組
69.
70. u32 regdata[U32_DATA_LEN];
71. int ret;
72.
73. ret = of_property_read_u32_array(np, "reg", regdata, U32_DATA_LEN);
74. if(!ret)
75. {
76. int i;
77. for(i=0; i<U32_DATA_LEN; i++)
78. printk("----regdata[%d] = 0x%x\n", i,regdata[i]);
79.
80. }else{
81. printk("get reg data failed\n");
82. }
83.
84. //讀取到屬性中的字符串的數組
85. const char *pstr[3];
86.
87. int i;
88. for(i=0; i<3; i++)
89. {
90. ret = of_property_read_string_index(np, "test_list_string", i, &pstr[i]);
91. if(!ret)
92. {
93. printk("----pstr[%d] = %s\n", i,pstr[i]);
94. }else{
95. printk("get pstr data failed\n");
96. }
97. }
98.
99. // 屬性的值為空,實際可以用于設置標志
100. if(of_find_property(np, "testprop,mytest", NULL))
101. {
102. is_good = 1;
103. printk("is_good = %d\n", is_good);
104. }
105.
106. // 獲取到中斷的號碼
107. irqno = irq_of_parse_and_map(np, 0);
108. printk("-----irqno = %d\n", irqno);
109.
110. //驗證中斷號碼是否有效
111. ret = request_irq(irqno, key_irq_handler, IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING,
112. "key_irq", NULL);
113. if(ret)
114. {
115. printk("request_irq error\n");
116. return -EBUSY;
117. }
118.
119.
120. return 0;
121.
122. }
123.
124. static void __exit dt_drv_exit(void)
125. {
126. free_irq(irqno, NULL);
127.
128. }
129.
130.
131.
132.
133. module_init(dt_drv_init);
134. module_exit(dt_drv_exit);
135. MODULE_LICENSE("GPL");