22071驱动day11

复习platform总线,三种匹配方式代码案例以及现象

#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,
    },
};

//定义一个release函数
void pdev_release(struct device *dev)
{
    printk("%s:%d\n",__func__,__LINE__);
}

//给对象赋值
struct platform_device pdev={
    .name="aa",
    .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");
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>

struct resource *res;
int irqno;
 //定义一个probe函数
    int pdrv_probe(struct platform_device *pdev)
    {

        //获取MEM类型的资源

        res=platform_get_resource(pdev,IORESOURCE_MEM,0);
        if(res==NULL)
        {
            printk("获取MEM资源失败\n");
            return ENODATA;
        }
        //获取中断类型的资源
        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");
#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(res==NULL)
    {
        printk("获取MEM资源失败\n");
        return ENODATA;
    }
    printk("获取MEM资源成功\n");
    //获取中断类型资源
    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},
    {},
};

//热插拔宏
MODULE_DEVICE_TABLE(platform,idtable);

//初始化对象
struct platform_driver pdrv={
    .probe=pdrv_probe,
    .remove=pdrv_remove,
    .driver={
        .name="aa",
    },
    .id_table=idtable,
};

module_platform_driver(pdrv);

MODULE_LICENSE("GPL");
#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>
/*myplatform{
compatible = "hqyj,platform";
            reg = <0x12345678 0x14>;
             interrupt-parent = <&gpiof>;
             interrupts = <9 0>;
            myled1 = <&gpioe 10 0>;
}; */
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(res==NULL)
        {
            printk("获取MEM资源失败\n");
            return ENODATA;
        }
        //获取中断类型的资源
        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,"myled1",0,GPIOD_OUT_HIGH,NULL);
        if(IS_ERR(gpiono))
        {
            printk("获取GPIO编号失败\n");
            return PTR_ERR(gpiono);
        }
        printk("申请GPIO编号成功,点灯");
        gpiod_set_value(gpiono,1);

           return 0;
    }

    int pdrv_remove(struct platform_device *pdev)
    {

             gpiod_set_value(gpiono,0);
             gpiod_put(gpiono);
            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="aa", //设置名字匹配
            .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、付费专栏及课程。

余额充值