2016年2月1日 星期一

[轉] linux下devicetree中常用的of函數

從device_node中獲取信息:
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);
從設備結點np中讀取屬性名為propname,類型為8、16、32、位整型數組的屬性值,並放入out_values,sz指明了要讀取的個數。

static inline int of_property_read_u8(const struct device_node *np,const char *propname,u8 *out_value) 
static inline int of_property_read_u16(const struct device_node *np,const char *propname,u8 *out_value) 
static inline int of_property_read_u32(const struct device_node *np,const char *propname,u8 *out_value) 
從設備結點np中讀取屬性名為propname,類型為8、16、32位的屬性值,並放入out_values。實際上這裡調用的就是sz為1的XXX_array函數。

int of_property_read_u32_index(const struct device_node *np,const char*propname,u32 index, u32 *out_value)
從設備結點np中讀取屬性名為propname的屬性值中第index個u32數值給out_value

int of_property_read_u64(conststruct device_node *np, const char *propname,u64 *out_value)
從設備結點np中讀取屬性名為propname,類型為64位的屬性值,並放入out_values

int of_property_read_string(struct device_node *np, const char *propname,const char**out_string)
從設備結點np中讀取屬性名為propname的字符串型屬性值

int of_property_read_string_index(struct device_node *np, const char *propname,intindex, const char **output)
從設備結點np中讀取屬性名為propname的字符串型屬性值數組中的第index個字符串

int of_property_count_strings(struct device_node *np, const char *propname)
從設備結點np中讀取屬性名為propname的字符串型屬性值的個數

unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
從設備節點dev中讀取第index個irq號

int of_irq_to_resource(struct device_node *dev, int index, struct resource *r)
從設備節點dev中讀取第index個irq號,並填充一個irq資源結構體

int of_irq_count(struct device_node *dev)
獲取設備節點dev的irq個數

static inline bool of_property_read_bool(const struct device_node *np,const char *propname);
如果設備結點np含有propname屬性,則返回true,否則返回false。一般用於檢查空屬性是否存在。

struct property* of_find_property(const struct device_node *np,const char *name,int *lenp)
根據name參數,在指定的設備結點np中查找匹配的property,並返回這個property

const void * of_get_property(const struct device_node *np, const char *name,int *lenp)
根據name參數,在指定的設備結點np中查找匹配的property,並返回這個property的屬性值

struct device_node* of_get_parent(const struct device_node *node)
獲得node節點的父節點的device node

int of_device_is_compatible(const struct device_node *device,const char *compat);
判斷設備結點device的compatible屬性是否包含compat指定的字符串

從of_allnodes中查找信息:
struct device_node* of_find_node_by_path(const char *path)
根據路徑參數,在全局鏈表of_allnodes中,查找匹配的device_node

struct device_node* of_find_node_by_name(struct device_node *from,const char *name)
則根據name在全局鏈表of_allnodes中查找匹配的device_node,若from=NULL表示從頭開始查找

struct device_node* of_find_node_by_type(struct device_node *from,const char *type)
根據設備類型在全局鏈表of_allnodes中查找匹配的device_node

struct device_node * of_find_compatible_node(struct device_node *from, const char*type, const char,*compatible);
根據compatible的屬性值在全局鏈表of_allnodes中查找匹配的device_node,大多數情況下,from、type為NULL。

struct device_node* of_find_node_with_property(struct device_node *from,const char *prop_name)
根據節點屬性的name在全局鏈表of_allnodes中查找匹配的device_node

struct device_node* of_find_node_by_phandle(phandle handle)
根據phandle在全局鏈表of_allnodes中查找匹配的device_node

雜:
void __iomem* of_iomap(struct device_node *node, int index);
通過設備結點直接進行設備內存區間的 ioremap(),index是內存段的索引。若設備結點的reg屬性有多段,可通過index標示要ioremap的是哪一段,只有1段的情況,index為0

unsigned long __init of_get_flat_dt_root(void)
用來查找在dtb中的根節點,好像返回的都是0

int of_alias_get_id(struct device_node *np, const char *stem)
獲取節點np對應的aliasid號

struct device_node* of_node_get(struct device_node *node)
void of_node_put(struct device_node *node)
device node計數增加/減少

const struct of_device_id* of_match_node(const struct of_device_id *matches,const struct device_node*node)
將matches數組中of_device_id結構的name和type與device node的compatible和type匹配,返回匹配度最高的of_device_id結構

platform_device和resource相關:
int of_address_to_resource(struct device_node *dev, int index,struct resource *r)
根據設備節點dev的reg屬性值,填充資源結構體r。Index參數指明了使用reg屬性中第幾個屬性值,一般設置為0,表示第一個。

struct platform_device* of_device_alloc(struct device_node *np,const char *bus_id,struct device *parent)
根據device node,bus_id以及父節點創建該設備的platform_device結構,同時會初始化它的resource成員。

int of_platform_bus_probe(struct device_node *root,const struct of_device_id *matches,struct device *parent)
遍歷of_allnodes中的節點掛接到of_platform_bus_type總線上,由於此時of_platform_bus_type總線上還沒有驅動,所以此時不進行匹配

int of_platform_populate(struct device_node *root,const struct of_device_id *matches,const struct of_dev_auxdata *lookup,struct device *parent)
遍歷of_allnodes中的所有節點,生成並初始化所以節點的platform_device結構

struct platform_device* of_find_device_by_node(struct device_node *np)
根據device_node查找返回該設備對應的platform_device結構