1、platform总线
platform作为一个虚拟总线和系统的其他总线并列,应当是在系统最初启动时已经添加了该总线,
查看PC系统所有总线结果 ls /sys/bus
ac97 eisa i2c MCA mmc pci_express pnp sdio spi
acpi gameport isa mdio_bus pci platform scsi serio usb
/drivers/base/platform.c 系统默认添加改模块
struct bus_type platform_bus_type = {
.name = "platform",
.dev_attrs = platform_dev_attrs,
.match = platform_match,
.uevent = platform_uevent,
.pm = &platform_dev_pm_ops,
};
EXPORT_SYMBOL_GPL(platform_bus_type);
int __init platform_bus_init(void)
{
int error;
early_platform_cleanup();
error = device_register(&platform_bus);
if (error)
return error;
error = bus_register(&platform_bus_type);
if (error)
device_unregister(&platform_bus);
return error;
}
2、platform设备 platform_device
主要定制设备的资源名称、IO端口、中断等
struct platform_device device1 = {
.name = "my_device1",
.id = 1,
.num_resources = ARRAY_SIZE(resources1),
.resource = resources1,
.dev = {
.release = my_release,
}
};
static struct resource resources1[] = {
{
.start = PHYS,
.end =PHYS + SIZE -1,
.flags = IORESOURCE_MEM,
},
{
.start = xREQ_INT,
.end = P_INT - xREQ_INT +1,
.flags = IORESOURCE_IRQ,
},
};
注册单个设备
int platform_device_register(devices1);
注册批量设备
int platform_add_devices(devices, ARRAY_SIZE(devices));
卸载设备
void platform_device_unregister(devices1);
3、platform驱动 platform_driver
主要实现驱动的具体实现函数
static struct platform_driver my_driver = {
.probe = my_probe,
.remove = my_remove,
.driver = {
.name = "my_device1",
.owner = THIS_MODULE,
},
};
注册驱动
platform_driver_register(&my_driver);
{ 内部执行platform_match,
比较platform_driver.driver.name == platform_device.name
则执行my_probe
}
卸载驱动
platform_driver_unregister(&my_driver);
{
内部执行my_remove
}
4、platform设备与驱动匹配后 执行my_probe
注册实际的设备,及其操作方法
static int my_probe(struct platform_device *pdev)
{
int retval;
retval = register_chrdev(Major, MODULE_NAME, &my_fops);
...
}