Linux学习_设备树使用示例
设备树节点与platform_device匹配
匹配的条件在于:
- 设备树dts文件中,该节点要有compatible属性
- platform_driver中要有of_match_table,其中一项的.compatible成员设置为一个字符串
- 上述两个compatible要一致
总线结构与设备树
曾经,使用总线结构编码时,我们使用了board_xxx.c
来实现platform_device,使用chip_demo_gpio.c
来实现platform_driver,再用led_drv.c
实现open、read、write函数,调用device和driver
在引入设备树后,我们用设备树.dts
文件来实现platform_device
,其他差别不大,对platform_driver稍作修改即可调用
dts文件代码示例如下:
#define GROUP_PIN(g,p) ((g<<16) | (p))
/ {
100ask_led@0 {
compatible = "100as,leddrv";
pin = <GROUP_PIN(3, 1)>;
};
100ask_led@1 {
compatible = "100as,leddrv";
pin = <GROUP_PIN(5, 8)>;
};
};
修改platform_driver
static const struct of_device_id ask100_leds[] = {
{
.compatible = "100as,leddrv" },
{
},
};
static struct platform_driver chip_demo_gpio_driver = {
.probe = chip_demo_gpio_probe,
.remove = chip_demo_gpio_remove,
.driver = {
.name = "100ask_led",
.of_match_table = ask100_leds,
},
};
static int __init chip_demo_gpio_drv_init(void){
int err;
err = platform_driver_register(&chip_demo_gpio_driver);
register_led_operations(&board_demo_led_opr);
return 0;
}