驱动开发 platform的3种匹配方式(设备信息<->驱动信息)

华清远见上海中心22071班

通过3种方式匹配platform总线的驱动与设备

1)通过名字方式匹配

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

int pdri_probe(struct platform_device *dev)//匹配到执行
{
    printk("%s %d\n", __func__, __LINE__);
    return 0;
}

int pdri_remove(struct platform_device *dev)//分离时执行
{
    printk("%s %d\n", __func__, __LINE__);
    return 0;
}

struct platform_driver pdri = {
    .probe = pdri_probe,
    .remove = pdri_remove,
    .driver = {
        .name = "aaaaa",//通过名字匹配
    },
};

static int __init pdri_init(void)
{
    platform_driver_register(&pdri);
    return 0;
}

static void __exit pdri_exit(void)
{
    platform_driver_unregister(&pdri);
}

module_init(pdri_init);
module_exit(pdri_exit);
MODULE_LICENSE("GPL");
#include <linux/init.h>
#include <linux/module.h>
#include <linux/of_device.h>

struct resource res[] = {
    [0] = {
        .start = 0x12345678,
        .end = 0x12345678 + 49,
        .flags = IORESOURCE_MEM,
    },
    [1] = {
        .start = 71,
        .end = 71,
        .flags = IORESOURCE_IRQ,
    },
};

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 pdev_init(void)
{
    //将对象注册进内核
    platform_device_register(&pdev);
    return 0;
}

static void __exit pdev_exit(void)
{
    //注销对象
    platform_device_unregister(&pdev);
}

module_init(pdev_init);
module_exit(pdev_exit);
MODULE_LICENSE("GPL");

 2、通过idtable匹配

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

struct resource res[] = {
    [0] = {
        .start = 0x12345678,
        .end = 0x12345678 + 49,
        .flags = IORESOURCE_MEM,
    },
    [1] = {
        .start = 71,
        .end = 71,
        .flags = IORESOURCE_IRQ,
    },
};

void pdev_release(struct device *dev)
{
    printk("%s %d\n", __func__, __LINE__);
}

//分配对象
struct platform_device pdev = {
    .name = "hello1",
    .id = PLATFORM_DEVID_AUTO,
    .dev = {
        .release = pdev_release,
    },
    .resource = res,
    .num_resources = ARRAY_SIZE(res),
};

static int __init pdev_init(void)
{
    //将对象注册进内核
    platform_device_register(&pdev);
    return 0;
}

static void __exit pdev_exit(void)
{
    //注销对象
    platform_device_unregister(&pdev);
}

module_init(pdev_init);
module_exit(pdev_exit);
MODULE_LICENSE("GPL");
#include <linux/module.h>
#include <linux/init.h>
#include <linux/of_device.h>
#include <linux/mod_devicetable.h>

int pdri_probe(struct platform_device *dev)//匹配到执行
{
    printk("%s %d\n", __func__, __LINE__);
    return 0;
}

int pdri_remove(struct platform_device *dev)//分离时执行
{
    printk("%s %d\n", __func__, __LINE__);
    return 0;
}

struct platform_device_id idtable[] = {
    {"hello0", 0},
    {"hello1", 1},
    {},
};

struct platform_driver pdri = {
    .probe = pdri_probe,
    .remove = pdri_remove,
    .driver = {
        .name = "aaaaa",
    },
    .id_table = idtable,//id_table 匹配
};

static int __init pdri_init(void)
{
    platform_driver_register(&pdri);
    return 0;
}

static void __exit pdri_exit(void)
{
    platform_driver_unregister(&pdri);
}

module_init(pdri_init);
module_exit(pdri_exit);
MODULE_LICENSE("GPL");

 3、通过设备树方式匹配

因为通过设备树匹配,不需要写设备文件

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

int pdri_probe(struct platform_device *dev)
{
    printk("%s %d\n", __func__, __LINE__);
    return 0;
}

int pdri_remove(struct platform_device *dev)
{
    printk("%s %d\n", __func__, __LINE__);
    return 0;
}

struct of_device_id oftable[] = {
    {
        .compatible = "hqyj, myplatform",//compatible 名字
    },
    {},
};

struct platform_driver pdri = {
    .probe = pdri_probe,
    .remove = pdri_remove,
    .driver = {
        .name = "aaaaa",
        .of_match_table = oftable,
    },

};

static int __init pdri_init(void)
{
    platform_driver_register(&pdri);
    return 0;
}

static void __exit pdri_exit(void)
{
    platform_driver_unregister(&pdri);
}

module_init(pdri_init);
module_exit(pdri_exit);
MODULE_LICENSE("GPL");

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值