驱动 - platform总线驱动

#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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coding Peasant

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值