700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Linux 有/无设备树下 platform_driver 驱动框架

Linux 有/无设备树下 platform_driver 驱动框架

时间:2021-08-25 18:35:45

相关推荐

Linux 有/无设备树下 platform_driver 驱动框架

文章目录

platform 驱动框架platform 设备框架有设备树下驱动开发注意事项宏___一行代码创建 platform_driver
platform 驱动框架

首先,定义一个platform_driver结构体变量

/* platform_driver */static struct platform_driver xxx_driver = {.driver = {/* .name: 无设备树匹配方式 *//* .of_match_table:有设备树匹配方式 */},.probe= xxx_probe,.remove = xxx_remove,};

然后,实现结构体中的各个成员变量,重点是实现匹配方法以及probe函数

当驱动和设备匹配成功以后probe函数就会执行

具体的驱动程序在probe函数里面编写,比如字符设备驱动等等

定义并初始化好platform_driver结构体变量以后,要在驱动入口函数里面调用platform_driver_register函数向Linux内核注册一个platform驱动

/** driver:要注册的 platform 驱动* return:负数,失败;0,成功*/int platform_driver_register (struct platform_driver *driver);

在驱动卸载函数中通过platform_driver_unregister函数卸载platform驱动

void platform_driver_unregister(struct platform_driver *drv);

对于一个完整的驱动程序,必须提供有设备树和无设备树两种匹配方法

无设备树:使用xxx_driver.driver.name进行设备匹配

有设备树:使用xxx_driver.driver.of_match_table进行设备匹配

key:匹配列表必须以空项结尾,{ /* Sentinel */ }

/* 匹配列表 */static const struct of_device_id xxx_of_match[] = {{.compatible = "xxx" },{/* Sentinel */ }};/* 声明 inputkey_of_match 设备匹配表 */MODULE_DEVICE_TABLE(of, xxx_of_match);/* * platform 平台驱动结构体*/static struct platform_driver xxx_driver = {.driver = {.name = "xxx",.of_match_table = xxx_of_match,},.probe = xxx_probe,.remove = xxx_remove,};

probe和remove的实现

/* platform probe 函数,驱动与设备匹配成功以后此函数就会执行 */static int xxx_probe(struct platform_device *dev){int ret = 0;.../* 完成字符设备注册初始化等 */...return 0;}/* platform remove 函数 */static int xxx_remove(struct platform_device *dev){int ret = 0;.../* 完成字符设备删除注销等 */...return 0;}

platform 设备框架

platform_device这个结构体表示platform设备

/* @description: 释放flatform设备模块的时候此函数会执行* @param - dev : 要释放的设备 * @return : 无*/static voidxxx_release(struct device *dev){printk("xxx device released!\r\n");}/* 设备资源,xxx 所用寄存器信息 */static struct resource xxx_resources[] = {[0] = {},[1] = {},}/* platform 设备结构体 */static struct platform_device xxx_device = {.name = "xxx", /* 与驱动相匹配 */.id = -1,.dev = {.release = xxx_release,/* 卸载设备时执行 */},.num_resources = ARRAY_SIZE(xxx_resources), /* 设备资源长度 */.resource = xxx_resources,/* 设备资源 */};

platform_device同样是模块初始化-退出框架

static int __init xxxdevide_init(void){/* 注册 platform 设备 */return platform_device_register(&xxx_device);}static void __exit xxxdevide_exit(void){/* 注销 platform 设备 */platform_device_unregister(&xxx_device);}/* 模块入口/出口 */module_init(xxxdevide_init);module_exit(xxxdevide_exit);/* LICENSE AND AUTHOR */MODULE_LICENSE("GPL");MODULE_AUTHOR("tao");MODULE_INFO(intree, "Y");

有设备树下驱动开发注意事项

ST针对STM32MP1提供的Linux系统中,其pinctrl配置的电气属性只能在platform平台下被引用,前面的实验都没用到platform,所以pinctrl配置是不起作用的!

对于STM32MP1来说,在使用pinctrl的时候需要修改一下pinctrl-stm32.c这个文件。

设备树问题

新的设备节点获取方式

宏___一行代码创建 platform_driver

Linux 自带 LED 驱动使用如下一行代码创建一个平台驱动

module_platform_driver(gpio_led_driver);

展开:

static int __init gpio_led_driver_init(void) {return platform_driver_register (&(gpio_led_driver)); } module_init(gpio_led_driver_init); static void __exit gpio_led_driver_exit(void) {platform_driver_unregister (&(gpio_led_driver) ); } module_exit(gpio_led_driver_exit);

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。