内核版本
- linux-v5.6
参考资料
- Documentation/devicetree/usage-model.txt
在“设备树(一):kernel解析dtb文件”中,已经知道dtb文件中的节点与属性信息会被解析填充到kernel的device_node和property结构体中;在start_kernel的后续初始化过程中,device_node还会进一步和各种device进行绑定,有虚拟的platform bus下的platform_device,也有spi、i2c等物理bus下的从设备(client)。
spi、i2c的控制器(master)作为platform_device设备与device_node进行绑定
一、platform_device
在设备树的usage-model.txt文档中有如下描述:
The trick is that the kernel starts at the root of the tree and looks for nodes that have a 'compatible' property.
For each of these nodes, Linux allocates and registers a platform_device, which in turn may get bound to a platform_driver.
也就是说,kernel会为设备树root节点下所有带'compatible' 属性的节点都分配并注册一个platform_device;另外,如果某节点的'compatible' 符合某些matches条件,则会为该节点下所有带'compatible' 属性的子节点(child)也分配并注册一个platform_device。
1.1 数据结构
struct platform_device {
const char *name;
int id;
bool id_auto;
/* 以此挂入统一设备模型 */
struct device dev;
u64 platform_dma_mask;
/* io和irq资源的总数 */
u32 num_resources;
/* 指向resource数组 */
struct resource *resource;
const struct platform_device_id *id_entry;
};
1.2 创建与填充过程
在of_platform_default_populate_init()函数中会对platform_device进行填充,路径如下:
start_kernel
--> arch_call_rest_init
--> rest_init
--> kernel_init
--> kernel_init_freeable
--> do_basic_setup
--> do_initcalls
--> of_platform_default_populate_init
函数实现如下:
/* drivers/of/platform.c */
static int __init of_platform_default_populate_init(void)
{
/* 检查of_root("/"节点)是否为NULL */
if (!of_have_populated_dt())
return -ENODEV;
/* 进行实际的platform_device填充操作 */
of_platform_default_populate(NULL, NULL, NULL);
return 0;
}
/* 在do_initcalls会被调用执行 */
arch_initcall_sync(of_platform_default_po