这一节我们讲设备树简单驱动例程
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/of_gpio.h>
#include <linxu/of.h>
#include <stdio.h>
MODULE_LICENSE("Dual BSD/GPL");
#define device_name "test"
static const struct of_device_id of_test_dt_match[] = {
{.compatible = device_name },
{},
};
MODULE_DEVICE_TABLE(of,of_test_dt_match);
static struct platform_driver test_driver=
{
.probe = test_probe,
.remove = test_remove,
.driver=
{
.name=device_name,
.owner= THIS_MODULE,
.of_match_table=
}
};
static int test_remove(struct platform_device * pdev)
{
return 0;
}
static int test_probe(struct platform_device * pdev)
{
return 0;
}
static int test_init(void)
{
printf("init_test\r\n");
return platform_driver_register(&test_driver);
}
static void test_exit(void)
{
printf("init_test\r\n");
platform_driver_unregister(&test_driver);
}
module_init(test_init);
module_exit(test_exit);
下面添加一些属性的获取。
我们在probe添加属性的获取
设备树中使用 device_node 结构体描述节点,of.h中的API需要device_node作为参数传入
struct device_node {
const char *name; //节点名称
const char *type; //设备类型
phandle phandle;
const char *full_name; //全路径节点
struct fwnode_handle fwnode;
struct property *properties;
struct property *deadprops; /* removed properties */
struct device_node *parent; //父节点指针
struct device_node *child; //子节点指针
struct device_node *sibling;
struct kobject kobj;
unsigned long _flags;
void *data;
#if defined(CONFIG_SPARC)
const char *path_component_name;
unsigned int unique_id;
struct of_irq_controller *irq_trans;
#endif
};
驱动中获取device_node方法: struct device_node *node = pdev->dev.of_node;
获取属性参数的函数:
根据属性名称,提取属性值
struct property *of_find_property(const struct device_node *np, const char name, int lenp);
/
- of_find_property - 提取指定属性的值
- @np - 设备树节点指针
- @name - 属性名称
- @lenp - 属性值的字节数
- 返回值:成功,属性值的首地址;失败:NULL
*/
static int test_probe(struct platform_device * pdev)
{
struct device_node *node = pdev->dev.of_node;
printf("node name is %s!\n",node->name);
struct property *comprop = NULL;
comprop = of_find_property(node,"compatible",NULL);
printf("comprop name is %s!\n",comprop->name);
return 0;
}