通用设备树:
让驱动去操作设备树,可以选择platform架构,也可以不选择platform架构。
vi -t /arch/arm/boot/dts/exynos4412-fs4412中:
566 ofled{
567 led2 = <&gpx2 7 0>;
568 led1 = <&gpx1 0 0>;
569 led3-4 = <&gpf3 4 0>,<&gpf3 5 0>;
570 };
接口:
1 struct device_node *of_find_node_by_path(const char *path)
2 功能:查找设备树节点信息(必须包含路径)
3 返回值:如果在设备树中找到了指定节点,返回值就指向了找到的节点。
1 static inline int of_get_named_gpio(struct device_node *np,const char *propname, int index)
2 参数1:of_find_node_by_path的返回值
3 参数2:属性名称
4 参数3:属性内容的索引值
5 返回值:引脚编号(系统动态分配的一个整数值,让这个整数值和实际的物理引脚建立关系)
----->此时内核还不认识这些引脚编号么
-------------->
1 int gpio_request(unsigned gpio, const char *label)
2 功能:注册引脚编号
3 参数1:引脚编号
4 参数2:引脚的别名
1 static inline int gpio_direction_output(unsigned gpio, int value)
2 功能:设置引脚为输出模式
3 参数1:引脚编号
4 参数2:指定的是数据寄存器中的默认值
1 static inline void gpio_set_value(unsigned gpio, int value)
2 功能:对指定引脚中的数据寄存器设置值
3 参数1:引脚编号
4 参数2:数据值
1 static inline int gpio_to_irq(unsigned int gpio)
2 功能:获取虚拟中断号
3 参数:gpio引脚编号
参考代码LED:
1 #include <linux/init.h> 2 #include <linux/module.h> 3 #include <linux/fs.h> 4 #include <linux/device.h> 5 #include <asm/gpio.h> 6 #include <linux/of.h> 7 #include <linux/of_gpio.h> 8 9 10 int major; 11 struct class *cls; 12 struct device *devs; 13 struct device_node *np; 14 15 int gpx2_7; 16 int gpx1_0; 17 int gpf3_4; 18 int gpf3_5; 19 20 int ofled_open(struct inode *inode,struct file *filp) 21 { 22 gpio_set_value(gpx2_7,1); 23 gpio_set_value(gpx1_0,1); 24 gpio_set_value(gpf3_4,1); 25 gpio_set_value(gpf3_5,1); 26 return 0; 27 } 28 29 int ofled_close(struct inode *inode,struct file *filp) 30 { 31 gpio_set_value(gpx2_7,0); 32 gpio_set_value(gpx1_0,0); 33 gpio_set_value(gpf3_4,0); 34 gpio_set_value(gpf3_5,0); 35 return 0; 36 } 37 38 struct file_operations fops = { 39 .owner = THIS_MODULE, 40 .open = ofled_open, 41 .release = ofled_close, 42 }; 43 44 int led_init(void) 45 { 46 major = register_chrdev(0,"ofled",&fops); 47 cls = class_create(THIS_MODULE,"ofled"); 48 devs = device_create(cls,NULL,MKDEV(major,0),NULL,"ofled"); 49 50 //查找节点名称 51 np = of_find_node_by_path("/ofled"); 52 53 //获取gpio引脚编号 54 gpx2_7 = of_get_named_gpio(np,"led2",0); 55 gpx1_0 = of_get_named_gpio(np,"led1",0); 56 gpf3_4 = of_get_named_gpio(np,"led3-4",0); 57 gpf3_5 = of_get_named_gpio(np,"led3-4",1); 58 59 //注册引脚编号 60 gpio_request(gpx2_7,NULL); 61 gpio_request(gpx1_0,NULL); 62 gpio_request(gpf3_4,NULL); 63 gpio_request(gpf3_5,NULL); 64 65 //设置输出模式 66 gpio_direction_output(gpx2_7,1); 67 gpio_direction_output(gpx1_0,1); 68 gpio_direction_output(gpf3_4,1); 69 gpio_direction_output(gpf3_5,1); 70 return 0; 71 } 72 module_init(led_init); 73 74 void led_exit(void) 75 { 76 return; 77 } 78 module_exit(led_exit); 79 80 MODULE_LICENSE("GPL");