#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;
int irqno;
struct gpio_desc *desc;
int pdrv_probe(struct platform_device *pdr)
{
//获取设备信息
printk("%s:%d\n",__FILE__,__LINE__);
res=platform_get_resource(pdr,IORESOURCE_MEM,0);
if(res==NULL)
{
printk("获取资源失败\n");
return -ENODATA;
}
irqno=platform_get_irq(pdr,0);
if(irqno<0)
{
printk("获取资源失败\n");
return irqno;
}
printk("%#x %d\n",res->start,irqno);
//获取GPIO编号
desc=gpiod_get_from_of_node(pdr->dev.of_node,"led2",0,GPIOD_OUT_HIGH,NULL);
if(IS_ERR(desc))
{
printk("获取GPIO编号失败\n");
return PTR_ERR(desc);
}
return 0;
}
int pdrv_remove(struct platform_device *pdr)
{
printk("%s:%d\n",__FILE__,__LINE__);
return 0;
}
//定义compatible表
struct of_device_id oftable[]={
{.compatible="hqyj,platform"},
{},
};
MODULE_DEVICE_TABLE(of,oftable);
//定义并初始化对象
struct platform_driver pdrv ={
.probe=pdrv_probe,
.remove=pdrv_remove,
.driver={
.name= "test",
.of_match_table= oftable,//设备树匹配
},
};
static int __init mycdev_init(void)
{
//注册对象
platform_driver_register(&pdrv);
return 0;
}
static void __exit mycdev_exit(void)
{
//释放GPIO编号
gpiod_direction_output(desc,0);
gpiod_put(desc);
//注销
platform_driver_unregister(&pdrv);
}
module_init(mycdev_init);
module_exit(mycdev_exit);