1. 程式人生 > >【總結】裝置樹語法及常用API函式

【總結】裝置樹語法及常用API函式

一、DTS編寫語法

二、常用函式

裝置樹函式思路是:
uboot啟動時將裝置樹地址傳給核心,核心解析裝置樹,並建立裝置,初始化相關屬性,驅動中通過of_get_XXX函式去獲取裝置樹載入時建立的裝置。想要知道of函式做了什麼,就去追蹤這個函式最後呼叫了什麼,同時也就知道了核心解析裝置樹的時候為我們建立了什麼。
(1)of_get_named_gpio /**
 * include/of_gpio.h
 * of_get_named_gpio - 從裝置樹中提取gpio口
 * @np - 裝置節點指標
 * @propname - 屬性名
 * @index - gpio口引腳標號 
 * 成功:得到GPIO口編號;失敗:負數,絕對值是錯誤碼
 */
int of_get_named_gpio
(struct device_node *np, const char *propname, int index);

(2)gpio_to_irq
/**
 * include/gpio.h
 * PIN值轉換為相應的IRQ值,中斷編號可以傳給request_irq()和free_irq()
 * @gpio - gpio口引腳標號 
 * 成功:得到GPIO口編號
 */
static inline int gpio_to_irq(unsigned gpio)

(3)devm_request_any_context_irq
/**
 * 註冊中斷
 */
devm_request_any_context_irq


(4)of_match_ptr
/**
 * 匹配裝置樹上的引數,將裝置int_demo_dt_ids與驅動int_demo_driver聯絡起來
 * 系統會根據裝置樹種定義的compatible引數比較驅動中的int_demo_dt_ids中定義的 .compatible 引數
 */
of_match_ptr(int_demo_dt_ids)
例子:
static const struct of_device_id int_demo_dt_ids[] = {  
    { .compatible = "tiny4412,interrupt_demo", },  
    {},  
};  
  
MODULE_DEVICE_TABLE(of, int_demo_dt_ids);  
  
static struct platform_driver int_demo_driver = {  
    .driver        = {  
        .name      = "interrupt_demo",  
        .of_match_table    = of_match_ptr(int_demo_dt_ids),  
    },  
    .probe         = int_demo_probe,  
    .remove        = int_demo_remove,  
};  
(5)of_get_property /*
 */drivers/of/base.c
 * Find a property with a given name for a given node
 * and return the value.
 * 通過給定的裝置節點和屬性名字得到value。
 */
const void *of_get_property(const struct device_node *np, const char *name,
int *lenp)
{
struct property *pp = of_find_property(np, name, lenp);


return pp ? pp->value : NULL;
}
(6)devm_pinctrl_get 獲取一個pinctrl控制代碼,引數是dev是包含這個pin的device結構體即xxx這個裝置的device
獲取裝置操作控制代碼(裝置模型中的struct device)的pin control state holder(struct pinctrl)
/** 
 * struct devm_pinctrl_get() - Resource managed pinctrl_get() 
 * @dev: the device to obtain the handle for 
 * 
 * If there is a need to explicitly destroy the returned struct pinctrl, 
 * devm_pinctrl_put() should be used, rather than plain pinctrl_put(). 
 */  
struct pinctrl *devm_pinctrl_get(struct device *dev)  

(7)pinctrl_lookup_state
獲取這個pin對應pin_state(引腳狀態-turnon_tes/turnoff_tes)
/** 
 * pinctrl_lookup_state() - retrieves a state handle from a pinctrl handle 
 * @p: the pinctrl handle to retrieve the state from 
 * @name: the state name to retrieve 
 */  
struct pinctrl_state *pinctrl_lookup_state(struct pinctrl *p, const char *name)  

(8)pinctrl_select_state
設定引腳為為某個stata -- turnon_tes/turnoff_tes
/** 
 * pinctrl_select_state() - select/activate/program a pinctrl state to HW 
 * @p: the pinctrl handle for the device that requests configuration 
 * @state: the state handle to select/activate/program 
 */  
int pinctrl_select_state(struct pinctrl *p, struct pinctrl_state *state)  

(9)of_get_named_gpio
得到GPIO的編號
./**
 * include/of_gpio.h
 * of_get_named_gpio - 從裝置樹中提取gpio口
 * @np - 裝置節點指標
 * @propname - 屬性名
 * @index - gpio口引腳標號 
 * 成功:得到GPIO口編號int型;失敗:負數,絕對值是錯誤碼
 */
int of_get_named_gpio(struct device_node *np, const char *propname, int index);
of_get_named_gpio:此函式是解析裝置樹的函式,我們通過這個函式去解析裝置樹,tiny4412,int_gpio1 = <&gpx3 2 GPIO_ACTIVE_HIGH>; 
跟蹤下去會發現這個函式掉用了list = of_get_property(np, "tiny4412,int_gpio2", &size);裝置樹解析是創界了裝置節點,現在通過這個函式去獲取屬性。

(10)devm_gpio_request_one
獲取一個GPIO並初始化屬性
/**
 *devm_gpio_request_one - request a single GPIO with initial setup
 *@dev:   device to request for
 *@gpio: the GPIO number
 *@flags: GPIO configuration as specified by GPIOF_*
 *@label: a literal description string of this GPIO
 */
int devm_gpio_request_one(struct device *dev, unsigned gpio,
 unsigned long flags, const char *label)