Linux设备驱动之platform

根据Linux设备模型可知,一个现实的Linux设备和驱动通常都需要挂接在一种总线上,对于本身依附于PCI、USB等的设备而言,这自然不是问题,但是在嵌入式系统里面,SoC系统中集成的独立的外设控制器、挂接在 SoC 内存空间的外设等却不依附于此类总线。基于这一背景,Linux设计了一种虚拟的总线,称为platform总线,相应的设备称为platform_device,而驱动称为platform_driver。

设计目的

兼容设备模型

使得设备被挂接在一个总线上,因此,符合 Linux 2.6 的设备模型。其结果是,配套的sysfs结点、设备电源管理都成为可能。

BSP和驱动隔离

在BSP中定义platform设备和设备使用的资源、设备的具体配置信息。而在驱动中,只需要通过通用API去获取资源和数据,做到了板相关代码和驱动代码的分离,使得驱动具有更好的可扩展性和跨平台性。

软件架构

内核中Platform设备有关的实现位于include/linux/platform_device.h和drivers/base/platform.c两个文件中,它的软件架构如下:

platform architecture

由图片可知,Platform设备在内核中的实现主要包括三个部分:

Platform Bus,基于底层bus模块,抽象出一个虚拟的Platform bus,用于挂载Platform设备;

Platform Device,基于底层device模块,抽象出Platform Device,用于表示Platform设备;

Platform Driver,基于底层device_driver模块,抽象出Platform Driver,用于驱动Platform设备。

platform_device

注意,所谓的platform_device并不是与字符设备、块设备和网络设备并列的概念,而是Linux系统提供的一种附加手段,例如,在S3C2440处理器中,把内部集成的I2C、RTC、SPI、LCD、看门狗等控制器都归纳为platform_device,而它们本身就是字符设备。

/* defined in <linux/platform_device.h> */ struct platform_device { const char * name; / * 设备名 */ u32 id; /* 用于标识该设备的ID */ struct device dev; /* 真正的设备(Platform设备只是一个特殊的设备,因此其核心逻辑还是由底层的模块实现)*/ u32 num_resources; / * 设备所使用各类资源数量 */ struct resource * resource; / * 资源 */ }; /* defined in <linux/ioport.h> */ struct resource { resource_size_t start; /* 资源起始 */ resource_size_t end; /* 结束 */ const char *name; unsigned long flags; /* 类型 */ struct resource *parent, *sibling, *child; }; /* 设备驱动获取BSP定义的resource */ struct resource *platform_get_resource(struct platform_device *, unsigned int flags, unsigned int num); #include <linux/platform_device.h> int platform_device_register(struct platform_device *); void platform_device_unregister(struct platform_device *);

Tip: 和板级紧密相关的资源描述放在dev.paltform_data中。

paltform_driver

platform_driver这个结构体中包含probe()、remove()、shutdown()、suspend()、resume()函数,通常也需要由驱动实现:

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 (*suspend_late)(struct platform_device *, pm_message_t state); int (*resume_early)(struct platform_device *); int (*resume)(struct platform_device *); struct device_driver driver; }; #include <linux/platform_device.h> int platform_driver_register(struct platform_driver *); void platform_driver_unregister(struct platform_driver *); platform_bus

系统中为platform总线定义了一个bus_type的实例platform_bus_type:

struct bus_type platform_bus_type = { .name = "platform", .dev_attrs = platform_dev_attrs, .match = platform_match, .uevent = platform_uevent, .pm = PLATFORM_PM_OPS_PTR, }; EXPORT_SYMBOL_GPL(platform_bus_type);

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/91eb463d818ad1358de472d82e9b4d10.html