Linux驱动学习之点灯(五,设备树没用平台设备总线)
创建一个设备树节点
/{led:led{compatible="led";led_pin=<&gpio0 22 GPIO_ACTIVE_HIGH &gpio0 21 GPIO_ACTIVE_HIGH>;status="okay";}
}
OF函数介绍
查找属性
of_gpio_named_count
of_gpio_named_count 函数用于获取设备树某个属性里面定义了几个 GPIO 信息,要注意的是空的 GPIO 信息也会被统计到。函数原型如下
int of_gpio_named_count(struct device_node *np, const char *propname)
参数一:设备节点
参数二:属性名字
of_gpio_count
和 of_gpio_named_count 函数一样,但是不同的地方在于,此函数统计的是 gpios 这个属性的 GPIO 数量,而 of_gpio_named_count 函数可以统计任意属性的 GPIO 信息,函数原型如下所示:
int of_gpio_count(struct device_node *np)
参数一:设备节点
of_get_named_gpio
此函数获取 GPIO 编号,因为 Linux 内核中关于 GPIO 的 API 函数都要使用 GPIO 编号,此函数会将设备树中类似 <&gpio5 7 GPIO_ACTIVE_LOW> 的属性信息转换为对应的 GPIO 编号,此函数在驱动中使用很频繁!函数原型如下:
int of_get_named_gpio(struct device_node *np, const char *propname, int index)
参数1:设备节点
参数2:属性名
参数3:gpio下标
of_get_named_gpio_flag
此函数获取GPIO的描述,可以通过设备树传出高低电平
int of_get_named_gpio_flags(struct device_node *np,const char *list_name, int index, enum of_gpio_flags *flags);
参数1:设备节点
参数2:属性名字
参数3:下标
参数4:传出参数
property *of_find_property(const struct device_node *np,const char *name,int*lenp)
参数1:设备节点。
参数2:节点名儿
参数3:传出参数,属性长度。
返回值返回一个结构体如下图

value就是属性值
int of_property_read_u32_index(const struct device_node *np,const char *propname,u32 index,u32 *out_value)
参数1:设备节点
参数2:属性名
参数3:下标
参数3:传出参数,我们想要的值
返回值:成功0,失败非零
int of_property_read_string(struct device_node *np,const char*propname,const char**out_string)
参数1:设备节点
参数2:属性名儿
参数3:传出参数
返回值:成功0 失败非零
查找节点
struct device_node * of_find_node_by_name(struct device_node *from,const char *name) 参数一:从哪里开始查找,一般填null,从根节点查找
参数二:节点名字
返回值:成功返回设备节点,
struct device_node * of_find_compatible_node (struct device_node*from,const char *type,const char *compatible) 参数1:从哪里开始查找,一般填null,从根节点查找参数2:类型,没有类型填null参数3:compatible属性的值返回值,成功返回设备节点
struct device_node * of_find_node_by_path(cons tchar *path) 根据路径查找,填入路径即可
LED驱动实现
所有函数专栏都有说到,不明的可以返回前面看看
#include "linux/cdev.h"
#include "linux/device.h"
#include "linux/device/class.h"
#include "linux/export.h"
#include "linux/fs.h"
#include "linux/gpio.h"
#include "linux/module.h"
#include "linux/of.h"
#include "linux/of_gpio.h"
#include "linux/printk.h"static dev_t dev_num;
static struct cdev *cdev;
static struct class * cls;
uint32_t pin1,pin2;static int open (struct inode *i, struct file *f)
{gpio_set_value(pin1,0);gpio_set_value(pin2,0);return 0;
}
static int close (struct inode *i, struct file *f)
{gpio_set_value(pin1,1);gpio_set_value(pin2,1);return 0;
}
struct file_operations fops={.owner=THIS_MODULE,.open=open,.release=close,
};
static int __init start(void)
{//of_find_property(0,0, 0);//struct device_node * node=of_find_node_by_name(NULL,"led");struct device_node * node=of_find_compatible_node(NULL,NULL,"led");if(node==NULL){printk("66666\r\n");return -EINVAL; }printk("%s\r\n",node->name);const char * status=NULL;of_property_read_string(node,"status",&status);if(strcmp("okay",status)){printk("66666\r\n");return -EINVAL;}printk("%s\r\n",status);uint32_t gpio_pin1=of_get_named_gpio(node,"led_pin",0);uint32_t gpio_pin2=of_get_named_gpio(node,"led_pin",1);printk("%x\r\n",gpio_pin1);printk("%X\r\n",gpio_pin2);pin1=gpio_pin1;pin2=gpio_pin2;enum of_gpio_flags flag=0;of_get_named_gpio_flags(node,"led",0,&flag);if(flag==OF_GPIO_ACTIVE_LOW){gpio_request(gpio_pin1,"led");gpio_direction_output(gpio_pin1,0);}else{gpio_request(gpio_pin1,"led");gpio_direction_output(gpio_pin1,0);}of_get_named_gpio_flags(node,"led",1,&flag);if(flag==OF_GPIO_ACTIVE_LOW){gpio_request(gpio_pin2,"led");gpio_direction_output(gpio_pin2,0);}else{gpio_request(gpio_pin2,"led");gpio_direction_output(gpio_pin2,0);}alloc_chrdev_region(&dev_num, 0, 1, "led");cdev= cdev_alloc();cdev->ops=&fops;cdev_add(cdev, dev_num,1);cls= class_create(THIS_MODULE, "led");device_create(cls,NULL,dev_num,NULL,"led");printk(KERN_INFO "Hello, world!\n");return 0;
}static void __exit stop(void)
{device_destroy(cls, dev_num);class_destroy(cls);unregister_chrdev_region(dev_num,1);gpio_free(pin1);gpio_free(pin2);printk(KERN_INFO "Goodbye, world!\n");
}module_init(start);
module_exit(stop);
MODULE_LICENSE("GPL");
