Platform驱动代码示例
基于总线、设备和驱动这样的驱动框架,Linux内核提出来platform这个虚拟总线,相应的也有platform设备和platform驱动。
在引入设备树后,platform设备的工作已经由设备树完成,只需要编写platform驱动部分代码。
platform驱动的编写在普通驱动编写的基础上加入:
- 增加of_match_table、probe函数、remove函数
- 声明设备匹配表
- 之前模块在init和exit完成的工作分别改到probe和remove函数实现
- 在模块的入口和出口函数分别注册和卸载平台设备驱动
参考代码:
/*设备树加入节点*/
test {
#address-cells = <1>;
#size-cells = <1>;
compatible = "test_id";
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_test>;
test-gpios = <&gpio1 5 GPIO_ACTIVE_LOW>;
status = "okay";
};
/*加入对应的of_match_table*/
static const struct of_device_id test_of_match[] = {
{ .compatible = " test_id " }, /* 兼容属性跟设备树对应 */
{ /* Sentinel */ }
};
/*声明of_match_table*/
MODULE_DEVICE_TABLE(of, test_of_match);
/*probe函数*/
static int test_probe(struct platform_device *dev)
{
return 0;
}
/*remove函数*/
static int test_remove(struct platform_device *dev)
{
return 0;
}
/*平台设备驱动结构体*/
static struct platform_driver test_platform_driver = {
.driver = {
.name = "imx6ul-test",
.of_match_table = test_of_match,
},
.probe = test_probe,
.remove = test_remove,
};
/*模块入口函数*/
static int __init testdriver_init(void)
{
return platform_driver_register(&test_platform_driver);
}
/*模块出口函数*/
static void __exit testdriver_exit(void)
{
platform_driver_unregister(&test_platform_driver);
}