设备树API
引子
首先看一个例子,也可参考linux设备树语法中的gpio示例。该示例选自openwrt的gpio-button-hotblug驱动。
设备树code:
gpio-keys-polled {
compatible = "gpio-keys-polled";
#address-cells = <>;
#size-cells = <>;
poll-interval = <>;
bat {
label = "bat";
gpios = <&gpio0 >;
linux,code = <0x211>;
};
reset {
label = "reset";
gpios = <&gpio0 >;
linux,code = <0x198>;
};
mode {
label = "mode";
gpios = <&gpio0 >;
linux,code = <0x100>;
linux,input-type = <>;
};
};
驱动相关code:
#ifdef CONFIG_OF
static struct gpio_keys_platform_data *
gpio_keys_get_devtree_pdata(struct device *dev)
{
struct device_node *node, *pp;
struct gpio_keys_platform_data *pdata;
struct gpio_keys_button *button;
int error;
int nbuttons;
int i = ; node = dev->of_node;
if (!node)
return NULL; nbuttons = of_get_child_count(node);
if (nbuttons == )
return NULL; pdata = devm_kzalloc(dev, sizeof(*pdata) + nbuttons * (sizeof *button),
GFP_KERNEL);
if (!pdata) {
error = -ENOMEM;
goto err_out;
} pdata->buttons = (struct gpio_keys_button *)(pdata + );
pdata->nbuttons = nbuttons; pdata->rep = !!of_get_property(node, "autorepeat", NULL);
of_property_read_u32(node, "poll-interval", &pdata->poll_interval); for_each_child_of_node(node, pp) {
enum of_gpio_flags flags; if (!of_find_property(pp, "gpios", NULL)) {
pdata->nbuttons--;
dev_warn(dev, "Found button without gpios\n");
continue;
} button = &pdata->buttons[i++]; button->gpio = of_get_gpio_flags(pp, , &flags);
button->active_low = flags & OF_GPIO_ACTIVE_LOW; if (of_property_read_u32(pp, "linux,code", &button->code)) {
dev_err(dev, "Button without keycode: 0x%x\n",
button->gpio);
error = -EINVAL;
goto err_out;
} button->desc = of_get_property(pp, "label", NULL); if (of_property_read_u32(pp, "linux,input-type", &button->type))
button->type = EV_KEY; button->wakeup = !!of_get_property(pp, "gpio-key,wakeup", NULL); if (of_property_read_u32(pp, "debounce-interval",
&button->debounce_interval))
button->debounce_interval = ;
} if (pdata->nbuttons == ) {
error = -EINVAL;
goto err_out;
} return pdata; err_out:
return ERR_PTR(error);
} static struct of_device_id gpio_keys_of_match[] = {
{ .compatible = "gpio-keys", },
{ },
};
MODULE_DEVICE_TABLE(of, gpio_keys_of_match); static struct of_device_id gpio_keys_polled_of_match[] = {
{ .compatible = "gpio-keys-polled", },
{ },
};
MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match); #else static inline struct gpio_keys_platform_data *
gpio_keys_get_devtree_pdata(struct device *dev)
{
return NULL;
}
#endif
static struct platform_driver gpio_keys_driver = {
.probe = gpio_keys_probe,
.remove = gpio_keys_remove,
.driver = {
.name = "gpio-keys",
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(gpio_keys_of_match),
},
};
static struct platform_driver gpio_keys_polled_driver = {
.probe = gpio_keys_polled_probe,
.remove = gpio_keys_remove,
.driver = {
.name = "gpio-keys-polled",
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(gpio_keys_polled_of_match),
},
};
static int __init gpio_button_init(void)
{
int ret; ret = platform_driver_register(&gpio_keys_driver);
if (ret)
return ret; ret = platform_driver_register(&gpio_keys_polled_driver);
if (ret)
platform_driver_unregister(&gpio_keys_driver); return ret;
} static void __exit gpio_button_exit(void)
{
platform_driver_unregister(&gpio_keys_driver);
platform_driver_unregister(&gpio_keys_polled_driver);
} module_init(gpio_button_init);
module_exit(gpio_button_exit);
该驱动同时注册了两种设备驱动:gpio_keys_driver和gpio_keys_polled_driver,前者采用中断方式检测按键状态,后者通过轮询方式检测案件状态。
OF API
设备树API通常以of_开头,实现代码位于drivers/of目录下,drivers/of目录下文件如下:
address.c fdt.c of_mtd.c of_pci_irq.c pdt.c
base.c irq.c of_net.c of_private.h platform.c
device.c of_mdio.c of_pci.c of_reserved_mem.c selftest.c
include/linux/目录下头文件:
of_address.h of_gpio.h of_mdio.h of_pdt.h
of_device.h of.h of_mtd.h of_platform.h
of_dma.h of_iommu.h of_net.h of_reserved_mem.h
of_fdt.h of_irq.h of_pci.h
0. 数据结构
of.h为基础头文件,包含相关数据结构的定义。
typedef u32 phandle;
typedef u32 ihandle; struct property {
char *name;
int length;
void *value;
struct property *next;
unsigned long _flags;
unsigned int unique_id;
};
struct device_node {
const char *name;
const char *type;
phandle phandle;
const char *full_name; struct property *properties;
struct property *deadprops; /* removed properties */
struct device_node *parent;
struct device_node *child;
struct device_node *sibling;
struct device_node *next; /* next device of same type */
struct device_node *allnext; /* next in list of all nodes */
struct proc_dir_entry *pde; /* this node's proc directory */
struct kref kref;
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
}; #define MAX_PHANDLE_ARGS 8
struct of_phandle_args {
struct device_node *np;
int args_count;
uint32_t args[MAX_PHANDLE_ARGS];
};
#define of_match_ptr(_ptr) (_ptr)
1. 寻找节点
int of_device_is_compatible(const struct device_node *device,const char *compat);
判断设备结点的compatible 属性是否包含compat指定的字符串。当一个驱动支持2个或多个设备的时候,这些不同.dts文件中设备的compatible 属性都会进入驱动 OF匹配表。因此驱动可以透过Bootloader传递给内核的Device Tree中的真正结点的compatible 属性以确定究竟是哪一种设备,从而根据不同的设备类型进行不同的处理。
struct device_node *of_find_compatible_node(struct device_node *from, const char *type, const char *compatible);
根据compatible属性,获得设备结点。遍历Device Tree中所有的设备结点,看看哪个结点的类型、compatible属性与本函数的输入参数匹配,大多数情况下,from、type为NULL,则表示遍历所有节点。
2. 读取属性
int of_property_read_u8_array(const struct device_node *np,
const char *propname, u8 *out_values, size_t sz);
int of_property_read_u16_array(const struct device_node *np,
const char *propname, u16 *out_values, size_t sz);
int of_property_read_u32_array(const struct device_node *np,
const char *propname, u32 *out_values, size_t sz);
int of_property_read_u64(const struct device_node *np, const char
*propname, u64 *out_value);
读取设备结点np的属性名为propname,类型为8、16、32、64位整型数组的属性。对于32位处理器来讲,最常用的是of_property_read_u32_array()。
of_property_read_u32_array(np, "arm,data-latency", data, ARRAY_SIZE(data)); of_property_read_u32_array(np, propname, out_value, );
int of_property_read_string(struct device_node *np, const char
*propname, const char **out_string);
int of_property_read_string_index(struct device_node *np, const char
*propname, int index, const char **output);
前者读取字符串属性,后者读取字符串数组属性中的第index个字符串。
static inline bool of_property_read_bool(const struct device_node *np,
const char *propname);
如果设备结点np含有propname属性,则返回true,否则返回false。一般用于检查空属性是否存在。
3. 内存映射
void __iomem *of_iomap(struct device_node *node, int index);
通过设备结点直接进行设备内存区间的 ioremap(),index是内存段的索引。若设备结点的reg属性有多段,可通过index标示要ioremap的是哪一段,只有1段的情 况,index为0。采用Device Tree后,大量的设备驱动通过of_iomap()进行映射,而不再通过传统的ioremap。
4. 解析中断
unsigned int irq_of_parse_and_map(struct device_node *dev, int index);
透过Device Tree或者设备的中断号,实际上是从.dts中的interrupts属性解析出中断号。若设备使用了多个中断,index指定中断的索引号。
5. 获取与节点对应的platform_device
struct platform_device *of_find_device_by_node(struct device_node *np);
在拿到device_node的情况下,反向获取对应的platform_device。
在已知platform_device的情况下,想获取device_node,如下:
static int imx_gpio_probe (struct platform_device *op)
{
struct device_node *dn = op->dev.of_node;
...
}
参考:
1. linux设备树语法
2. ARM Linux 3.x的设备树(Device Tree)
设备树API的更多相关文章
- Linux内核 设备树操作常用API【转】
转自:https://www.linuxidc.com/Linux/2017-02/140818.htm 一文中介绍了设备树的语法,这里主要介绍内核中提供的操作设备树的API,这些API通常都在&qu ...
- Linux内核 设备树操作常用API
Linux设备树语法详解一文中介绍了设备树的语法,这里主要介绍内核中提供的操作设备树的API,这些API通常都在"include/of.h"中声明. device_node 内核中 ...
- 【总结】设备树语法及常用API函数【转】
本文转载自:http://blog.csdn.net/fengyuwuzu0519/article/details/74352188 一.DTS编写语法 二.常用函数 设备树函数思路是:uboot ...
- Linux设备树语法详解
概念 Linux内核从3.x开始引入设备树的概念,用于实现驱动代码与设备信息相分离.在设备树出现以前,所有关于设备的具体信息都要写在驱动里,一旦外围设备变化,驱动代码就要重写.引入了设备树之后,驱动代 ...
- Linux 获取设备树源文件(DTS)里描述的资源
Linux 获取设备树源文件(DTS)里的资源 韩大卫@吉林师范大学 在linux使用platform_driver_register() 注册 platform_driver 时, 需要在 plat ...
- Linux设备树语法详解【转】
转自:http://www.cnblogs.com/xiaojiang1025/p/6131381.html 概念 Linux内核从3.x开始引入设备树的概念,用于实现驱动代码与设备信息相分离.在设备 ...
- Linux 总线、设备、驱动模型 与 设备树
1.总线.设备.驱动模型 本着高内聚.低耦合的原则,Linux 把设备驱动模型分为了总线.设备和驱动三个实体,这三个实体在内核里的职责分别如下: 设备和驱动向总线进行注册,总线负责把设备和对应的驱动绑 ...
- 设备树..ing
.dts==>.dtb ==>device_node ==> platform_device ==> led_dev.c ==>匹配 led_drv.c (设备 ...
- Linux 获取设备树源文件(DTS)里描述的资源【转】
转自:http://www.linuxidc.com/Linux/2013-07/86839.htm 转自:http://blog.sina.com.cn/s/blog_636a55070101mce ...
随机推荐
- 【R笔记】R的内存管理和垃圾清理
笔记: 1.R输入命令时速度不要太快,终究是个统计软件,不是编程! 2.memory.limit()查看当前操作系统分配内存给R的最大限度(单位是M?) 3.要经常 rm(object) 或者 rm( ...
- iOS:CocoaPods详解
原文地址:http://blog.csdn.net/wzzvictory/article/details/18737437 一.什么是CocoaPods 1.为什么需要CocoaPods 在进行iOS ...
- java源码阅读LinkedList
1类签名与注释 public class LinkedList<E> extends AbstractSequentialList<E> implements List< ...
- Solr json,xml等文件数据导入(添加索引)linux下操作
使用solr-5.3.1\example\exampledocs下的post.jar来完成数据导入 1.将想要导入的文件放在solr-5.3.1\example\exampledocs中,如aaa.x ...
- android 博客列表
1. Hongyang http://blog.csdn.net/lmj623565791/article/details/37970961
- 各版本JDK1.5-1.8新特性
概述 一jdk15新特性 泛型 foreach 自动拆箱装箱 枚举 静态导入Static import 元数据Metadata 线程池 Java Generics 二jdk16新特性 Desktop类 ...
- maven命令解释
打包:mvn package编译:mvn compile编译测试程序:mvn test-compile清空:mvn clean运行测试:mvn test生成站点目录: mvn site生成站点目录并发 ...
- mac重置蓝牙模块
升级系统后蓝牙鼠标无法连接,蓝牙模块也无法关闭,通过重置 PRAM和SMC解决 PRAM 重置 关机,拔掉所有外设,接上电源. 启动时同时按住 Command, Option, p, r , 听到三次 ...
- C语言--矩阵置换
//takePlace里的循环写错了,j循环应该是 //for (j=i;j<3;j++) //你那个写的交换了2遍,又变回原来的了.*// #include <stdio.h> ] ...
- JAVA自带监控工具的介绍
转:http://www.alidw.com/?p=326 相信部分同学可能还是不太了解或者很少使用,这些监控工具是jdk5.0以上才会有的,有部分是liunx特有的. 了解这些工具再做压力测试和调优 ...