Linux驱动-platform驱动模型

一 platform 平台驱动模型简介

采用 设备、驱动 分离的思想。

当向系统注册一个驱动的时候,总线就会在右侧的设备中查找,看看有没有与之匹配的设备,如果有的话就将两者联系起来。
同样的,当向系统中注册一个设备的时候,总线就会在左侧的驱动中查找看有没有与之匹配的设备,有的话也联系起来。

设备驱动的分离,引出了总线(bus)、驱动(driver)和设备(device)模型,比如 I2C、SPI、USB 等总线。

但是在 SOC 中有些外设是没有总线这个概念的,但是又要使用总线、驱动和设备模型该怎么办呢?
为了解决此问题,Linux 提出了 platform 这个虚拟总线,相应的就有 platform_driver 和 platform_device。

在这里插入图片描述
设备驱动的分离,引出了总线(bus)、驱动(driver)和设备(device)模型,比如 I2C、SPI、USB 等总线。
但是在 SOC 中有些外设是没有总线这个概念的,但是又要使用总线、驱动和设备模型该怎么办呢?
为了解决此问题,Linux 提出了 platform 这个虚拟总线,相应的就有 platform_driverplatform_device

二 platform总线

Linux系统内核使用bus_type 结构体表示总线,此结构体定义在文件 include/linux/device.h
bus_type 结构体内容如下:

struct bus_type {
	const char *name; /* 总线名字 */
	const char *dev_name; 
	struct device *dev_root;
	struct device_attribute *dev_attrs;
	const struct attribute_group **bus_groups; /* 总线属性 */
	const struct attribute_group **dev_groups; /* 设备属性 */
	const struct attribute_group **drv_groups; /* 驱动属性 */
	
	int (*match)(struct device *dev, struct device_driver *drv);
	int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
	int (*probe)(struct device *dev);
	int (*remove)(struct device *dev);
	void (*shutdown)(struct device *dev);
	
	int (*online)(struct device *dev);
	int (*offline)(struct device *dev);
	int (*suspend)(struct device *dev, pm_message_t state);
	int (*resume)(struct device *dev);
	const struct dev_pm_ops *pm;
	const struct iommu_ops *iommu_ops;
	struct subsys_private *p;
	struct lock_class_key lock_key;
};

match 函数很重要,用于“匹配、相配”。
此函数就是完成设备和驱动之间匹配的, 总线就是使用 match 函数 根据注册的设备来查找对应的驱动, 或者根据注册的驱动来查找相应的设备, 因此每一条总线都必须实现此函数。
match 函数有两个参数:dev 和 drv,这两个参数分别为 device 和 device_driver 类型,也就是设备和驱动。

此为,platform 总线是 bus_type 的一个具体实例,
定义在文件 drivers/base/platform.c,platform 总线定义如下:

struct bus_type platform_bus_type = {
	.name = "platform",
	.dev_groups = platform_dev_groups,
	.match = platform_match,
	.uevent = platform_uevent,
	.pm = &platform_dev_pm_ops,
};

platform_bus_type 就是 platform 平台总线,其中 platform_match 就是匹配函数。

static int platform_match(struct device *dev,struct device_driver *drv)
{
    struct platform_device *pdev = to_platform_device(dev);
    struct platform_driver *pdrv = to_platform_driver(drv);

    /*When driver_override is set,only bind to the matching driver*/
    if (pdev->driver_override)
        return !strcmp(pdev->driver_override, drv->name);

     /* Attempt an OF style match first */
     if (of_driver_match_device(dev, drv))
         return 1;

     /* Then try ACPI style match */
     if (acpi_driver_match_device(dev, drv))
         return 1;

     /* Then try to match against the id table */
     if (pdrv->id_table)
         return platform_match_id(pdrv->id_table, pdev) != NULL;

     /* fall-back to driver name match */
     return (strcmp(pdev->name, drv->name) == 0);
}

第 11~12 行,第一种匹配方式, OF 类型的匹配,也就是设备树采用的匹配方式。
of_driver_match_device 函数定义在文件 include/linux/of_device.h 中。
device_driver 结构体(表示设备驱动)中有个名为 of_match_table 的成员变量,此成员变量保存着驱动的compatible匹配表。
设备树中的每个设备节点的 compatible 属性会和 of_match_table 表中的所有成员比较,查看是否有相同的条目,如果有的话就表示设备和此驱动匹配,设备和驱动匹配成功以后 probe 函数 就会执行。
在这里插入图片描述
第 15~16 行,第二种匹配方式,ACPI 匹配方式。

第 19~20 行, id_table 匹配。
每个 platform_driver 结构体有一个 id_table成员变量,顾名思义,保存了很多 id 信息。 这些 id 信息存放着这个 platformd 驱动所支持的驱动类型。

第 23 行,第四种匹配方式,如果第三种匹配方式的 id_table 不存在的话就直接比较驱动和 设备的 name 字段,看看是不是相等,如果相等的话就匹配成功。

三 platform设备

platform_device介绍

platform_device 这个结构体表示 platform 设备,
这里要注意,如果内核支持设备树的话就不要再使用 platform_device 来描述设备了,
因为改用设备树去描述了。

include/linux/platform_device.h

struct platform_device {
     const char *name;         //表示设备名字,要和所使用的 platform 驱动的 name 字段相同
     int id; 
     bool id_auto;
     struct device dev;
     u32 num_resources;         //表示资源数量,一般为下一行 resource 资源的大小。
     struct resource *resource;    //表示资源,也就是设备信息,比如外设寄存器等。Linux 内核使用 resource 结构体表示资源

     const struct platform_device_id *id_entry;
     char *driver_override; /* Driver name to force a match */

     /* MFD cell pointer */
     struct mfd_cell *mfd_cell;

     /* arch specific additions */
     struct pdev_archdata archdata;
};

resource 结构体

struct resource {
resource_size_t start;      //start 和 end 分别表示资源的起始和终止信息
resource_size_t end;        //对于内存类的资源,就表示内存起始和终止地址
const char *name;           //表示资源名字
unsigned long flags;        //表示资源类型
struct resource *parent, *sibling, *child;
};

代码示例-platform设备 框架

/* 寄存器地址定义*/
#define PERIPH1_REGISTER_BASE (0X20000000) /* 外设 1 寄存器首地址 */ 
#define PERIPH2_REGISTER_BASE (0X020E0068) /* 外设 2 寄存器首地址 */
#define REGISTER_LENGTH 4

/* 资源,总共有两个资源 分别为设备外设 1 和外设 2 的寄存器信息*/
static struct resource xxx_resources[] = {
[0] = {
.start = PERIPH1_REGISTER_BASE,
.end = (PERIPH1_REGISTER_BASE + REGISTER_LENGTH - 1),
.flags = IORESOURCE_MEM,    //表示资源为内存类型的
}, 
[1] = {
.start = PERIPH2_REGISTER_BASE,
.end = (PERIPH2_REGISTER_BASE + REGISTER_LENGTH - 1),
.flags = IORESOURCE_MEM,    //表示资源为内存类型的
},
};

/* platform 设备结构体 */
static struct platform_device xxxdevice = {
.name = "xxx-gpio",    //name 字段要和所使用的驱动中的 name 字段一致
.id = -1,
.num_resources = ARRAY_SIZE(xxx_resources),
.resource = xxx_resources,
};
 
/* 设备模块加载 */
static int __init xxxdevice_init(void)
{
return platform_device_register(&xxxdevice);    //向 Linux 内核注册 platform 设备。
}

/* 设备模块注销 */
static void __exit xxx_resourcesdevice_exit(void)
{
platform_device_unregister(&xxxdevice);    //从 Linux 内核中卸载 platform 设备。
}

module_init(xxxdevice_init);
module_exit(xxxdevice_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Lelouch");

四 platform 驱动

platform_driver 介绍

platform_driver 结构体表示 platform 驱动,
此结构体定义在文件include/linux/platform_device.h 中,

struct platform_driver {
	int (*probe)(struct platform_device *);
 	int (*remove)(struct platform_device *);
 	void (*shutdown)(struct platform_device *);
 	int (*suspend)(struct platform_device *, pm_message_t state);
 	int (*resume)(struct platform_device *);
 	struct device_driver driver;
 	const struct platform_device_id *id_table;
 	bool prevent_deferred_probe;
};

第 2 行,probe 函数,当驱动与设备匹配成功以后 probe 函数就会执行。
第 7 行,driver 成员,为 device_driver 结构体变量,Linux 内核里面大量使用到了面向对象的思维,device_driver 相当于基类,提供了最基础的驱动框架。plaform_driver 继承了这个基类,然后在此基础上又添加了一些特有的成员变量。
第 8 行,id_table 表, platform 总线匹配驱动和设备的时候采用的第三种方法,id_table 是个表( 也就是数组) ,每个元素的类型为 platform_device_id

代码示例-platform驱动 框架

struct xxx_dev{
struct cdev cdev;//字符设备结构体
/* 设备结构体其他具体内容 */
};

struct xxx_dev xxxdev; /* 定义个设备结构体变量 */

static int xxx_open(struct inode *inode, struct file *filp)
{ 
/* 函数具体内容 */
return 0;
}

static ssize_t xxx_write(struct file *filp, const char __user *buf,size_t cnt, loff_t *offt)
{
/* 函数具体内容 */
return 0;
}

/
* 字符设备驱动操作集
*/
static struct file_operations xxx_fops = {
.owner = THIS_MODULE,
.open = xxx_open,
.write = xxx_write,
};

/*
* platform 驱动的 probe 函数
* 驱动与设备匹配成功以后此函数就会执行
*/
static int xxx_probe(struct platform_device *dev)
{ 
......
cdev_init(&xxxdev.cdev, &xxx_fops); /* 注册字符设备驱动 */
/* 函数具体内容 */
return 0;
}

static int xxx_remove(struct platform_device *dev)
{
......
cdev_del(&xxxdev.cdev);/* 删除 cdev */
/* 函数具体内容 */
return 0;
}

/* 匹配列表 */
static const struct of_device_id xxx_of_match[] = {
{ .compatible = "xxx-gpio" },
{ /* Sentinel */ }
};

/* 
* platform 平台驱动结构体
*/
static struct platform_driver xxx_driver = {
.driver = {
.name = "xxx",
.of_match_table = xxx_of_match,
},
.probe = xxx_probe,
.remove = xxx_remove,
};

/* 驱动模块加载 */
static int __init xxxdriver_init(void)
{
return platform_driver_register(&xxx_driver);
}

/* 驱动模块卸载 */
static void __exit xxxdriver_exit(void)
{
platform_driver_unregister(&xxx_driver);
}

module_init(xxxdriver_init);
module_exit(xxxdriver_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Lelouch");

五 platform + dts

采用设备树 + platform 的设备驱动方式:
platform 驱动框架分为总线、设备和驱动,其中总线不需要我们这些驱动程序员去管理,这个是 Linux 内核提供的,我们在编写驱动的时候只要关注于设备和驱动的具体实现即可。
在没有设备树的 Linux 内核下,我们需要分别编写并注册 platform_device 和 platform_driver,分别代表设备和驱动。
在使用设备树的时候,设备的描述被放到了设备树中,因此 platform_device 就不需要去编写了,只需要实现 platform_driver 即可。
在编写基于设备树的 platform 驱动的时候我们需要注意一下几点:

1 在设备树中创建设备节点
要先在设备树中创建设备节点来描述设备信息,重点是要设置好 compatible属性的值,
因为 platform 总线需要通过设备节点的 compatible 属性值来匹配驱动!
在这里插入图片描述
2 编写 platform 驱动的时候要注意兼容属性
在这里插入图片描述

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值