platform虚拟平台总线
1、平台总线的注册
/driver/base/platform.c中实现
struct bus_type platform_bus_type = {
.name = "platform",
.dev_groups = platform_dev_groups,
.match = platform_match,
.uevent = platform_uevent,
.dma_configure = platform_dma_configure,
.pm = &platform_dev_pm_ops,
};
int __init platform_bus_init(void)
{
...
error = bus_register(&platform_bus_type);
...
return error;
- struct bus_type platform_bus_type - 定义平台总线
- platform_match - 平台总线规则匹配函数
- platform_bus_init() - 平台总线初始化函数,内核启动后被执行
平台总线的匹配规则:
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);
}
- strcmp(pdev->driver_override, drv->name) - 如果设置了设备的driver_override字段则忽略其他匹配方式,进行设备driver_override字段和驱动name字段的匹配
- of_driver_match_device - 设备树匹配
- acpi_driver_match_device - ACPI方式匹配,用的比较少
- platform_match_id - id_table表匹配
- (strcmp(pdev->name, drv->name) == 0) - 设备和驱动名字匹配
2、platform device 平台设备
2.1 、描述平台设备结构体
//platform_device.h
struct platform_device {
const char *name; //使用名字来比较是用的最多的
int id; //device的id 也是用来匹配去当的
bool id_auto;
struct device dev; //基本的设备结构
u32 num_resources; //硬件资源列表的个数
struct resource *resource; //硬件资源列表
const struct platform_device_id *id_entry; //也可以使用这个id_entry来比较
char *driver_override; /* Driver name to force a match 强制匹配,如果设置了这个参数会忽略name 和 id那些参数*/
/* MFD cell pointer */
struct mfd_cell *mfd_cell;
/* arch specific additions */
struct pdev_archdata archdata;
};
从下platform_device的struct device dev成员的可以看出,platform_device继承自device(最基本的设备对象)和*struct resource resource
platform_device作用:关联硬件代码,描述驱动要使用到的硬件资源
2.2、平台设备注册/卸载函数
//注册平台设备
extern int platform_device_register(struct platform_device *dev);
//卸载平台设备
extern void platform_device_unregister(struct platform_device *dev);
2.3、硬件资源使用struct resource resource结构体描述
//资源结构体
struct resource {
resource_size_t start;
resource_size_t end;
const char *name;
unsigned long flags;
unsigned long desc;
struct resource *parent, *sibling, *child;
};
- start - 资源开始值
- end - 资源结束值
- flags - 资源的类型
- IORESOURCE_MEM:内存地址
- IORESOURCE_IO:IO端口
- IORESOURCE_DMA:DMA传输
- IORESOURCE_IRQ:中断
2.4、驱动获取设备描述的硬件资源
//给驱动获取平台设备的提供的资源
extern struct resource *platform_get_resource(struct platform_device *dev, unsigned int type, unsigned int num);
- dev - 平台设备
- type - 资源类型
- num - platform_device字段resources数组中资源编号
3、platform driver 平台驱动
3.1 描述平台驱动结构体
//platform_device.h
struct platform_driver {
int (*probe)(struct platform_device *); //一般是做初始化工作,注册file_operations结构体等
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;
};
platform_driver继承自struct device_driver driver
platform_driver:
关联驱动代码
重设probe()函数指针指针,设备和驱动匹配成功,probe()函数就会被执行,一般在该函数做初始化工作
驱动被卸载时**int (*remove)(struct platform_device *)**函数执行
3.2、平台驱动的注册
include/linux/platform_device.h
#define platform_driver_register(drv) \
__platform_driver_register(drv, THIS_MODULE)
extern int __platform_driver_register(struct platform_driver *,
struct module *);