Platform驱动代码示例

Platform驱动代码示例

基于总线、设备和驱动这样的驱动框架,Linux内核提出来platform这个虚拟总线,相应的也有platform设备和platform驱动。

在引入设备树后,platform设备的工作已经由设备树完成,只需要编写platform驱动部分代码。

platform驱动的编写在普通驱动编写的基础上加入:

  1. 增加of_match_table、probe函数、remove函数
  2. 声明设备匹配表
  3. 之前模块在init和exit完成的工作分别改到probe和remove函数实现
  4. 在模块的入口和出口函数分别注册和卸载平台设备驱动

参考代码:

/*设备树加入节点*/
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); 
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值