设备树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 ...
随机推荐
- MathType插入空格
公式太长,换行后加一些空格,继续录. 将鼠标定位到需要插入空格的位置,此时如果直接按空格键,你会发现并不能插入空格.正确的输入方法有两种: 方法一,在菜单栏中[样式]菜单下选择[文本],随后按空格键即 ...
- 1、Android项目框架搭建 (分析需求、整理资料)
闲来无事.想搭个框架试试 分析一般应用 将资料整理整理 粗略统计 须要下面资料 1.android-pulltorefresh 一个强大的拉动刷新开源项目,支持各种控件下拉刷新 ListView.Vi ...
- LINUX 和WINDOWS下的自动登录小脚本
每天上班第一件事,就是连接公司LAB里面的机器,但首先要过一个防火墙,每次输用户名密码是很累人的事, 以下是两个脚本,可以放在启动项中,开机便自动登录 WINDOWS: @echo off ipcon ...
- Tomcat:IOException while loading persisted sessions: java.io.EOFException 解决
转自:http://www.blogjava.net/apple0668/archive/2007/10/12/152383.html Tomcat启动时如下错误: 严重: IOException w ...
- 详解Python中的迭代器和使用
对于一个列表,a = [1, 2, 3, 4],我们最常见的遍历方式就是: a = [1, 2, 3, 4] for item in a: print item 这里我们研究一种新的方式,就是迭代器. ...
- POJ1274:The Perfect Stall(二分图最大匹配 匈牙利算法)
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17895 Accepted: 814 ...
- Grow heap (frag case) to 6.437MB for 1114126-byte allocation
本篇文章是对Grow heap (frag case) 堆内存过大的问题进行了详细的分析介绍,需要的朋友参考下 对于Android开发者来说虽然使用了可以自动管理内存的Java语言,但是对于内存管理不 ...
- JAVA-IO操作,字节-字符转换流
掌握OutputStreamWriter和InputStreamReader类的作用 一般操作输入输出内容的时候,就需要使用字节或字符流,但是,有些时候,需要将字符流变成字节流形式,或者字节流变成字符 ...
- SQL Server 2008R2发布与订阅的配置
使用SQL Server的发布与订阅可以将一个数据库的数据实时传送到另一个数据库中,使用这种方式与Link Server相比可以减少对数据库的连接次数.下面介绍SQL Server 2008R2发布与 ...
- Srping AOP xml方式
使用aop需要: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="ht ...