platform设备驱动
(From:http://www.linuxidc.com/Linux/2011-01/31291.htm)
platform驱动的优势:将设备本身注册到内核,由内核统一管理,驱动程序时调用platform提供的接口就可以,这样方便移植,更安全。
1.platform总线、设备与驱动
在Linux 2.6的设备驱动模型中,关心总线、设备和驱动这3个实体,总线将设备和驱动绑定。在系统每注册一个设备的时候,会寻找与之匹配的驱动;相反的,在系统每注册一个驱动的时候,会寻找与之匹配的设备,而匹配由总线完成。
一个现实的Linux设备和驱动通常都需要挂接在一种总线上,对于本身依附于PCI、USB、I2 C、SPI等的设备而言,这自然不是问题,但是在嵌入式系统里面,SoC系统中集成的独立的外设控制器、挂接在SoC内存空间的外设等确不依附于此类总 线。基于这一背景,Linux发明了一种虚拟的总线,称为platform总线,相应的设备称为platform_device,而驱动成为 platform_driver。
|
2.编写platform驱动程序步骤
一、注册设备
1.为设备编写platform_device设备结构
};
struct resource结构体:
struct resource {
};
注:struct resource结构中我们通常关心start、end和flags这3个字段,www.linuxidc.com分别标明资源的开始值、结束值和类 型,flags可以为IORESOURCE_IO、IORESOURCE_MEM、IORESOURCE_IRQ、IORESOURCE_DMA等。 start、end的含义会随着flags而变更,如当flags为IORESOURCE_MEM时,start、end分别表示该 platform_device占据的内存的开始地址和结束地址;当flags为IORESOURCE_IRQ时,start、end分别表示该 platform_device使用的中断号的开始值和结束值,如果只使用了1个中断号,开始和结束值相同。对于同种类型的资源而言,可以有多份,譬如说 某设备占据了2个内存区域,则可以定义2个IORESOURCE_MEM资源。
注:设备名与id
The platform_device.dev.bus_id is the canonical name for the devices. It's built from two components:
|
These are concatenated, so name/id "serial"/0 indicates bus_id "serial.0", and "serial/3" indicates bus_id "serial.3"; both would use the platform_driver named "serial".
platform设备注册例子:
linux2.6.26.8内核中NAND FLASH设备注册实例:
nand控制器资源:
linux2.6.26.8/arch/arm/plat-s3c24xx/devs.c
static struct resource s3c_nand_resource[] = {
};
struct platform_device s3c_device_nand = {
};
注册nand flash作为platform device:
linux2.6.26.8/arch/arm/plat-s3c24xx/common-smdk.c:
static struct platform_device __initdata *smdk_devs[] = {
};
void __init smdk_machine_init(void)
{
platform_add_devices(smdk_devs,ARRAY_SIZE(smdk_devs));
}
注:
In many cases, the memory and IRQ resources associated with the platform device are not enough to let the device's driver work.
二、注册驱动程序
1.为驱动程序编写platform_driver结构体
struct platform_driver {
|
};
2.注册platform_driver结构体
int platform_driver_register(struct platform_driver *);
platform设备注册例子:
linux2.6.26.8内核中NAND FLASH设备驱动注册实例:
linux/drivers/mtd/nand/s3c2410.c:
static struct platform_driver s3c2410_nand_driver = {
};
static int __init s3c2410_nand_init(void)
{
}
module_init(s3c2410_nand_init);
注:驱动程序绑定由内核自动执行,当内核发现一个驱动程序与一个设备匹配时,将调用驱动程序的probe函数,完成对设备的探测及初始化等工作。
Device
Driver binding is performed automatically by the driver core, invoking driver probe() after finding a match between device and driver.
- Whenever a device is registered, the drivers for that bus are checked for matches.
|
三、注销设备与驱动程序
1.注销驱动程序
void platform_driver_unregister(struct platform_driver *);
例子:
linux/drivers/mtd/nand/s3c2410.c:
static void __exit s3c2410_nand_exit(void)
{
}
module_exit(s3c2410_nand_exit);
2.注销设备
void platform_device_unregister(struct platform_device *pdev);