获取设备树节点信息
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
9 static int irq;
10
11 //3.中断处理函数
12 irqreturn_t key_2_handler(int irq,void *devid)
13 {
14 static int num = 0;
15 num++;
16 printk("--------------key_pressed-------------%d \n",num);
17 return IRQ_HANDLED;
18 }
19
20
21 static int __init dev_tree_init(void)
22 {
23
24 /*linjun_node@1516360{
25 compatible = "lj,lj_node";
26 reg = <0x12345678 0x24
27 0x87654321 0x24
28 >; */
29 testprop,mytest; // 属性的值为空,作为标记
30 test_list_string = "xue","xin","zhao";
interrupt-parent = <&gpx1>; //key 3
interrupts = < 2 2 >; //gpx1的第二个腿,触发方式是2
31 };*/
32
33 struct device_node *np = NULL;
34
35 np = of_find_node_by_path("/linjun_node@1516360");
36 if(np){
37 printk("of_find_node_by_path ok \n");
38 printk("name : %s\n",np->name);
39 printk("full_name : %s\n",np->full_name);
40
41 }else{
42 printk("of_find_node_by_path FAILED\n");
43 }
44
45 //找节点的属性
46 struct property *prop;
47 prop = of_find_property(np,"compatible",NULL);
48 if(prop){
49 printk("compatible value : %s\n",prop->value);
50 printk("compatible name : %s\n",prop->name);
51 }else{
52 printk("of_find_property fail\n");
53 }
54
55 //直接找属性
56 int m = of_device_is_compatible(np,"lj,lj_node");
57 if(m){
58 printk("yes\n");
59 }
60
61
62 //读取到属性中的整数数组
63 u32 data[4];
64 int i;
65 int ret = of_property_read_u32_array(np,"reg",data,4);
66 if(ret == 0){
67 for(i = 0; i < 4; i++){
68 printk("reg data[%d] : 0x%x\n",i,data[i]);
69 }
70 }else{
71 printk("get reg FAILED\n");
72 }
73
74
75 //读取到属性中的字符串数组
76 const char *ptr[3];
77 for(i = 0; i < 3; i++){
78 ret = of_property_read_string_index(np,"test_list_string",i,&ptr[i]);
79 if(!ret){
80 printk("test_list_string [i] %s\n:",ptr[i]);
81 }else{
82 printk("of_property_read_string_index fail\n");
83 }
84 }
85 //属性为空,实际可以用于设置标志
86 ret = of_find_property(np,"testprop,mytest",NULL);
87 if(ret){
88 printk("is_good \n");
89 }
90
91 //1.获取中断号码,返回值是中断号
92 irq = irq_of_parse_and_map(np,0);
93 printk("-------------irq-------%d\n",irq);
94
95 //2.验证中断号码是否有效
96 ret = request_irq(irq,key_2_handler,IRQF_TRIGGER_FALLING,"key_irq",NULL);
97 if(ret){
98 printk("request_irq fail\n");
99 return -1;
100 }
101
102
103 return 0;
104 }
105
106 static void __exit dev_tree_exit(void)
107 {
108
109 free_irq(irq,NULL);
110
111
112
113 }
114
115 module_init(dev_tree_init);
116 module_exit(dev_tree_exit);
117 MODULE_LICENSE("GPL");