在裝置樹中描述platform device

2022-01-11 20:09:26 字數 4928 閱讀 7071

在高通平台做gpio按鍵配置的時候,根據之前的經驗,想從裝置樹中對應的關鍵字找到實際的驅動解析實現,以此加深對裝置樹屬性配置的理解。

看來,對裝置樹的學習還是不能停下。

reference:

定義:在裝置樹的dts檔案裡,帶有compatible屬性的節點就是表示乙個platform_device.

用法:在裝置樹里增加乙個裝置節點,在核心裡的dts檔案裡描述裝置節點;此後,編寫驅動**完成對其的解析即可。

在裝置驅動裡獲取裝置樹中的裝置資源需要一套介面函式來實現:

#include
函式以device開頭表示讀取裝置的屬性, 以fwnode開頭表示讀取子節點的屬性。

引數propname代表指定要獲取值的屬性名。

判斷屬性是否存在

bool device_property_present(struct device *dev, const char *propname);
常見用法1:作為指定的屬性

nvmem->read_only = device_property_present(config->dev, "read-only") |

config->read_only;

//...;

if (nvmem->read_only)

/* retrieve the phy configuration properties */

if (device_property_present(pdata->phy_dev, xgbe_blwc_property))

} else

獲取整數值
int device_property_read_u8_array(struct device *dev, const char *propname,

u8 *val, size_t nval);

int device_property_read_u16_array(struct device *dev, const char *propname,

u16 *val, size_t nval);

int device_property_read_u32_array(struct device *dev, const char *propname,

u32 *val, size_t nval);

int device_property_read_u64_array(struct device *dev, const char *propname,

u64 *val, size_t nval);

nval代表 返回的陣列成員個數。

文字值獲取

int device_property_read_string(struct device *dev, const char *propname,

const char **val);

注:獲取到的值會放進val中;

常見用法:

device_property_read_string(dev, "label", &pdata->name);

my_key ;

判斷文字值是否匹配
int device_property_match_string(struct device *dev,

const char *propname, const char *string);

例如:

if (!device_property_match_string(dev, "rotary-encoder,encoding",

"binary")) else

獲取子節點
/* 獲取個數 */

unsigned int device_get_child_node_count(struct device *dev); //獲取裝置的子節點個數

/* 獲取下乙個子節點 */

struct fwnode_handle *device_get_next_child_node(struct device *dev,

struct fwnode_handle *child);

fwnode是表示子節點的物件位址,因為和上面的用法類似,因此不再詳細說明。

/* 用於獲取裝置子節點的屬性值函式.   */

bool fwnode_property_present(struct fwnode_handle *fwnode, const char *propname);

int fwnode_property_read_u8_array(struct fwnode_handle *fwnode,

const char *propname, u8 *val,

size_t nval);

int fwnode_property_read_u16_array(struct fwnode_handle *fwnode,

const char *propname, u16 *val,

size_t nval);

int fwnode_property_read_u32_array(struct fwnode_handle *fwnode,

const char *propname, u32 *val,

size_t nval);

int fwnode_property_read_u64_array(struct fwnode_handle *fwnode,

const char *propname, u64 *val,

size_t nval);

int fwnode_property_read_string_array(struct fwnode_handle *fwnode,

const char *propname, const char **val,

size_t nval);

int fwnode_property_read_string(struct fwnode_handle *fwnode,

const char *propname, const char **val);

int fwnode_property_match_string(struct fwnode_handle *fwnode,

const char *propname, const char *string);

舉乙個例子,如在某個被編譯的.dts檔案裡加入以下內容:

mynodes@77885566 ;    

btn2 ;

};

增加內容後,則重編裝置樹:

make dtbs arch=arm64 cross_compile=...
再把編譯出來的dtb替換板上所用的dtb檔案,重啟系統後,可以檢視到內容:

# ls /sys/bus/platform/devices/mynodes@77885566/

driver_override of_node/ subsystem/

modalias power/ uevent

# ls /sys/bus/platform/devices/mynodes@77885566/of_node/

autorepeat btn1/ btn2/ compatible name123456

在dst裝置樹檔案描述裝置後就需要與platform_driver進行匹配和驅動了。

用於獲取mynodes裝置資源的驅動原始碼:

/* mydrv.c */

#include #include #include #include //產生乙個for迴圈用於檢查所有的子節點

#define device_for_each_child_node(dev, child) \

for (child = device_get_next_child_node(dev, null); child; \

child = device_get_next_child_node(dev, child))

int myprobe(struct platform_device *pdev)

; return 0;

}int myremove(struct platform_device *pdev)

struct of_device_id ids = ,

{},};struct platform_driver mydrv = ,

};module_platform_driver(mydrv);

module_license("gpl");

編譯驅動模組載入後的輸出結果:

[  111.222065] child node count : 2

[ 111.222429] 1

[ 111.223054] label = btn1

[ 111.223690] code = 11

[ 111.224000] label = btn2

[ 111.224623] code = 22

3 X核心下裝置樹 platform裝置驅動

1。歷史的車輪總是向前,技術更替。在linus 同學發出那句 wfk 後核心進入了裝置樹時代 站在驅動工程師角度 前幾天我已經被mach imx 中的檔案折磨的夜不能眠。我終於在乙個清晨,喝完一杯咖啡後決定放棄蹩腳的傳統device描述方式。這裡我先不討論核心實現流程的源 簡單描述下語法,和我的第乙...

在裝置樹中時鐘的簡單使用

核心中關於時鐘的描述文件 documentation devicetree bindings clock clock bindings.txt documentation devicetree bindings clock samsung,s3c2410 clock.txt a.裝置樹中定義了各種時...

6 3在裝置樹中pinctrl的簡單使用

文件 核心 documentation devicetree bindings pinctrl samsung pinctrl.txt 幾個概念 bank 以引腳名為依據,這些引腳分為若干組,每組稱為乙個bank 比如s3c2440裡有gpa gpb gpc等bank,每個bank中有若干個引腳,比...