帖子:https://blog.csdn.net/lwj103862095/article/details/17957637
1、什么是platform(平台)总线?
相对于USB、PCI、I2C、SPI等物理总线来说,platform总线是一种虚拟、抽象出来的总线,实际中并不存在这样的总线。
2、那为什么需要platform总线呢?
其实是Linux设备驱动模型为了保持设备驱动的统一性而虚拟出来的总线。因为对于usb设备、i2c设备、pci设备、spi设备等等,他们与cpu的通信都是直接挂在相应的总线下面与我们的cpu进行数据交互的,但是在我们的嵌入式系统当中,并不是所有的设备都能够归属于这些常见的总线,在嵌入式系统里面,SoC系统中集成的独立的外设控制器、挂接在SoC内存空间的外设
却不依附与此类总线。所以Linux驱动模型为了保持完整性,将这些设备挂在一条虚拟的总线上(platform总线),而不至于使得有些
设备挂在总线上,另一些设备没有挂在总线上。
platform总线是一种虚拟的总线,相应的设备则为platform_device,而驱动则为platform_driver
过程:
程序分析--------------platform.c-----------------------------
1 #include <linux/init.h>
2 #include <linux/module.h>
3 #include <linux/major.h>
4 #include <linux/cdev.h>
5 #include <linux/device.h>
6 #include <linux/types.h>
7 #include <linux/fs.h>
8 #include <linux/platform_device.h>
9
10 #define TAG "platform"
11
12 /*设备 platform_device结构体的定义
13 static struct platform_device test_plat_dev = {
14 .name = "platform",
15 };
16 /*
17 static int test_plat_probe(struct platform_device *dev)
18 {
19 printk(TAG" FUNC:%s LINE:%d\n", __func__, __LINE__);
20
21 return 0;
22 }
23 /*驱动 判断platform_device和platform_driver成员里的name,如果二者的name字段相同则匹配,如果匹配则调用platform_driver的test_plat_probe函数。
24 static struct platform_driver test_plat_drv = {
25 .probe = test_plat_probe,
26 .driver = {
27 .name = "platform",
28 .owner = THIS_MODULE,
29 },
30 };
31
32 static int test_plat_init(void)
33 {
34 int ret;
35
36 printk(TAG" FUNC:%s LINE:%d\n", __func__, __LINE__);
//平台设备的注册,使用platform_device_register函数
37 platform_device_register(&test_plat_dev);
/*平台驱动的注册使用platform_driver_register函数
38 ret = platform_driver_register(&test_plat_drv);
39
40 return ret;
41 }
42 static void test_plat_exit(void)
43 {
44 printk(TAG" FUNC:%s LINE:%d\n", __func__, __LINE__);
45 /*注销平台驱动的话,使用platform_driver_unregister函数
46 platform_driver_unregister(&test_plat_drv);
/*如果要注销平台设备则使用platform_device_unregister函数
47 platform_device_unregister(&test_plat_dev);
48 }
49 module_init(test_plat_init);
50 module_exit(test_plat_exit);
51 MODULE_LICENSE("GPL");
判断platform_device和platform_driver成员里的name,如果二者的name字段相同则匹配,如果匹配则调用platform_driver的test_plat_probe函数。如下图说明已经匹配
如果把其中一个名字改一下,结果如下: 匹配不上 调用不了test_plat_probe
改回来,结果如下: