设备树的使用(三)

这一节我们讲设备树简单驱动例程

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值