platform总线驱动

通过名字匹配设备端
device.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>


//对设备信息进行填充
  struct resource res[]={
      [0]={
         .start=0x12345678,
         .end=0x12345678+49,
         .flags= IORESOURCE_MEM,     
      },
      [1]={
          .start=71,
         .end=71,
         .flags= IORESOURCE_IRQ,      
      },
  };
  
//定义一个relese函数
void pdev_release(struct device *dev)
{
    printk("%s:%d\n",__func__,__LINE__);
}

  //给对象赋值
  struct platform_device pdev={
      .name="aaaaa",
      .id=PLATFORM_DEVID_AUTO,
      .dev={
          .release=pdev_release,      
      },
      .resource=res,
      .num_resources=ARRAY_SIZE(res),        
  };
static int __init mycdev_init(void)
{
    //注册device
    return platform_device_register(&pdev);
}
static void __exit mycdev_exit(void)
{
    //注销device
    platform_device_unregister(&pdev);


}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

drive.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>

 struct resource *res;
 int irqno;
//定义一个probe函数
    int pdrv_probe(struct platform_device *pdev)
    {
//获取mem类型资源
        res = platform_get_resource(pdev,IORESOURCE_MEM, 0);
        if(NULL == res)
        {
            printk("获取资源失败\n");
            return ENODATA;
        }
        //获取irq类型资源
        irqno=platform_get_irq(pdev, 0);
        if(irqno<0)
        {
            printk("获取中断资源失败\n");
            return ENODATA;
        }
        printk("addr::%#llx,irqno::%d\n",res->start, irqno);
           return 0;
    }

    int pdrv_remove(struct platform_device *pdev)
    {
            printk("%s:%d\n",__func__,__LINE__);
            return 0;
    }
    //构建名字表
    struct platform_device_id idtable[]={
        {"hello1", 0},
        {"hello2", 1},
        {"hello3", 2},
        {},    //防止match函数遍历完名字表之后向下遍历脏数据

    };
    //热插拔宏
    MODULE_DEVICE_TABLE(platform,idtable);
    //对象初始化
    struct platform_driver pdrv={
        .probe=pdrv_probe,
        .remove=pdrv_remove,  
        .driver={
            .name="aaaaa",        
        },
        .id_table=idtable,
    };
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");

在这里插入图片描述

驱动端id_table方式匹配设备端

drive.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>

 struct resource *res;
 int irqno;
//定义一个probe函数
    int pdrv_probe(struct platform_device *pdev)
    {
//获取mem类型资源
        res = platform_get_resource(pdev,IORESOURCE_MEM, 0);
        if(NULL == res)
        {
            printk("获取资源失败\n");
            return ENODATA;
        }
        //获取irq类型资源
        irqno=platform_get_irq(pdev, 0);
        if(irqno<0)
        {
            printk("获取中断资源失败\n");
            return ENODATA;
        }
        printk("addr::%#llx,irqno::%d\n",res->start, irqno);
           return 0;
    }

    int pdrv_remove(struct platform_device *pdev)
    {
            printk("%s:%d\n",__func__,__LINE__);
            return 0;
    }
    //构建名字表
    
    //热插拔宏
    //对象初始化
    struct platform_driver pdrv={
        .probe=pdrv_probe,
        .remove=pdrv_remove,  
        .driver={
            .name="aaaaa",        
        },
        
    };
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");

实验现象
在这里插入图片描述

将匹配方式设置为设备树匹配

设备树编写文件

myplatform{
compatible = "hqyj,platform";
            reg = <0x12345678 0x14>;
             interrupt-parent = <&gpiof>;
             interrupts = <9 0>;
            myled1 = <&gpioe 10 0>;
};

drive.c

#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 *gpiono;
//定义一个probe函数
    int pdrv_probe(struct platform_device *pdev)
    {
//获取mem类型资源
        res = platform_get_resource(pdev,IORESOURCE_MEM, 0);
        if(NULL == res)
        {
            printk("获取资源失败\n");
            return ENODATA;
        }
        //获取irq类型资源
        irqno=platform_get_irq(pdev, 0);
        if(irqno<0)
        {
            printk("获取中断资源失败\n");
            return ENODATA;
        }
        printk("addr::%#x,irqno::%d\n",res->start, irqno);
        //解析设备树节点信息获取gpio编号通过pdev->dev.of_node
        gpiono = gpiod_get_from_of_node(pdev->dev.of_node,"myleds",0,GPIOD_OUT_HIGH, NULL);
        if(IS_ERR(gpiono))
        {
            printk("获取节点信息失败\n");
            return PTR_ERR(gpiono);
        }
        printk("获取GPIO编号成功, 点灯\n");
        //gpio输出高电平
        gpiod_set_value(gpiono,1);
        return 0;

    }

    int pdrv_remove(struct platform_device *pdev)
    {
        //熄灯
            gpiod_set_value(gpiono,0);
            gpiod_put(gpiono);
            printk("%s:%d\n",__func__,__LINE__);
            return 0;
    }
      //构建compatible表
    struct of_device_id oftable[]={

        {.compatible="hqyj,platform",},
        {},
    };
    //对象初始化
    struct platform_driver pdrv={
        .probe=pdrv_probe,
        .remove=pdrv_remove,  
        .driver={
            .name="aaaaa",    //设置字匹配
            .of_match_table=oftable,    //设置设备树匹配
        },
        
        
    };
    //一键注册宏(入口,出口)
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");

实验现象
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值