#include <linux/init.h>
#include <linux/module.h>
#include<linux/platform_device.h>
#include<linux/mod_devicetable.h>
#include<linux/of.h>
#include<linux/of_gpio.h>
struct resource *res;
unsigned int irqno;
struct gpio_desc *gpiono[3];
//定义相关成员函数
//probe函数用于匹配设备成功后执行
/* myplatform{
compatible = "hqyj,myplatform";
reg = <0x12345678 0x14>;
interrupt-parent = <&gpiof>;
interrupts = <9 0>,<7 0>,<8 0>;
led1=<&gpioe 10 0>;
led2=<&gpiof 10 0>;
led3=<&gpioe 8 0>;
};
*/
int pdrv_probe(struct platform_device *pdev)//当和设备匹配成功之后执行probe
{
//解析MEM类型的资源
res=platform_get_resource(pdev,IORESOURCE_MEM,0);
if(res==NULL)
{
printk("解析资源失败%d\n",__LINE__);
return -ENOMEM;
}
//解析中断类型的资源
irqno=platform_get_irq(pdev,0);
if(irqno<0)
{
printk("解析资源失败%d\n",__LINE__);
return -ENOMEM;
}
printk("mem:%x irq:%d\n",res->start,irqno);
//解析出GPIO编号并申请
gpiono[0]=gpiod_get_from_of_node(pdev->dev.of_node,"led1",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono))
{
printk("GPIO信息解析失败\n");
return -PTR_ERR(gpiono);
}
gpiono[1]=gpiod_get_from_of_node(pdev->dev.of_node,"led2",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono))
{
printk("GPIO信息解析失败\n");
return -PTR_ERR(gpiono);
}
gpiono[2]=gpiod_get_from_of_node(pdev->dev.of_node,"led3",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(gpiono))
{
printk("GPIO信息解析失败\n");
return -PTR_ERR(gpiono);
}
//亮灯
int i=0;
for(i=0;i<3;i++)
gpiod_set_value(gpiono[i],1);
return 0;
}
//remove用于和设备分离时执行
int pdrv_remove(struct platform_device *pdev)//当设备和驱动分离时执行remove
{
//灭灯
gpiod_set_value(gpiono,0);
//释放GPIO编号
gpiod_put(gpiono);
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
//在驱动中创建一个用于设备树匹配的表
struct of_device_id oftable[]={
{.compatible="hqyj,myplatform",},
{.compatible="hqyj,myplatform1",},
{.compatible="hqyj,myplatform2",},
{},
};
//分配对象并初始化
struct platform_driver pdrv={
.probe=pdrv_probe,
.remove=pdrv_remove,
.driver={
.name="aaaaa",
.of_match_table=oftable, //指定设备树匹配表的首地址
},
};
//一键注册宏,代替入口出口
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");