0、platform虚拟平台总线
latform总线是一种虚拟总线,主要用于连接集成在片上系统(SoC)内部的、无法通过传统总线(如PCI、USB)枚举发现的设备
常见的使用了platform总线的内核模块或驱动类型:
字符设备驱动:
GPIO驱动:drivers/gpio/gpio-*.c
I2C控制器驱动:drivers/i2c/busses/i2c-*.c
SPI控制器驱动:drivers/spi/spi-*.c
串口驱动:drivers/tty/serial/目录下的许多驱动,如serial_omap.c, amba-pl011.c等。
看门狗驱动:drivers/watchdog/*.c
PWM驱动:drivers/pwm/pwm-*.c
硬件随机数发生器(RNG)驱动。
块设备/存储驱动:
网络设备驱动:
- 许多集成的以太网MAC控制器驱动,例如某些
drivers/net/ethernet/下的驱动。
输入子系统驱动:
电源管理/时钟/复位驱动:
- 这些是更底层的基础设施驱动,也常以platform设备的形式存在。
1、平台总线的注册
/driver/base/platform.c中实现
structbus_typeplatform_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() - 平台总线初始化函数,内核启动后被执行
平台总线的匹配规则:
staticintplatform_match(struct device *dev, struct device_driver *drv){structplatform_device *pdev = to_platform_device(dev);structplatform_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)) return1;/* Then try ACPI style match */if (acpi_driver_match_device(dev, drv)) return1;/* 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.hstructplatform_device {constchar *name; //使用名字来比较是用的最多的int id; //device的id 也是用来匹配去当的bool id_auto;structdevicedev;//基本的设备结构 u32 num_resources; //硬件资源列表的个数structresource *resource;//硬件资源列表conststructplatform_device_id *id_entry;//也可以使用这个id_entry来比较char *driver_override; /* Driver name to force a match 强制匹配,如果设置了这个参数会忽略name 和 id那些参数*//* MFD cell pointer */structmfd_cell *mfd_cell;/* arch specific additions */structpdev_archdataarchdata;};
从下platform_device的struct device dev成员的可以看出,platform_device继承自device(最基本的设备对象)和**struct resource resource*
platform_device作用:关联硬件代码,描述驱动要使用到的硬件资源
2.2、平台设备注册/卸载函数
//注册平台设备externintplatform_device_register(struct platform_device *dev);//卸载平台设备externvoidplatform_device_unregister(struct platform_device *dev);
2.3、硬件资源使用struct resource结构体描述
//资源结构体structresource {resource_size_t start; resource_size_t end; constchar *name; unsignedlong flags; unsignedlong desc;structresource *parent, *sibling, *child;};
2.4、驱动获取设备描述的硬件资源
//给驱动获取平台设备的提供的资源externstruct resource *platform_get_resource(struct platform_device *dev, unsignedint type, unsignedint num);
- num - platform_device字段resources数组中资源编号
3、platform driver 平台驱动
3.1 描述平台驱动结构体
//platform_device.hstructplatform_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 *);structdevice_driverdriver;conststructplatform_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)externint __platform_driver_register(struct platform_driver *,struct module *);