.dts(device tree source)==>.dtb ==> 经过内核解析 ==> device_node ==> platform_device ==> led_dev.c ==>匹配 led_drv.c
设备树是对平台设备总线的一种改进
1.使用设备树时平台设备总线源码分析
平台设备总线分析:https://www.cnblogs.com/zsy12138/p/10391933.html
struct bus_type platform_bus_type = { .name = "platform", .dev_groups = platform_dev_groups, .match = platform_match, /* * platform_match --> platform_match(device,device_driver) --> of_driver_match_device(dev, drv) * --> of_match_device(drv->of_match_table, dev) * 最终用总线的 devices 和总线的 driver->of_match_table 相互比较 * */ .uevent = platform_uevent, .dma_configure = platform_dma_configure, .pm = &platform_dev_pm_ops, }; /* * platform_driver 分配,设置,注册file_operations ,读取platform_device 硬件资源操作硬件 */ 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 (*resume)(struct platform_device *); struct device_driver driver; const struct platform_device_id *id_table; bool prevent_deferred_probe; }; /* * platform_device 指定硬件资源 */ struct platform_device { const char *name; int id; bool id_auto; struct device dev; u32 num_resources; struct resource *resource; const struct platform_device_id *id_entry; char *driver_override; /* Driver name to force a match */ /* MFD cell pointer */ struct mfd_cell *mfd_cell; /* arch specific additions */ struct pdev_archdata archdata; }; 1.编写一个drv.c和.dts 通过比较 platform_device->device(对于.dts生成的platform_device,device含有of_node,of_node含有compatible和pin)->compatible platform_driver->device_driver->of_device_id->compatible 是否相同来决定是否支持该设备 2.编写一个drv.c和dev.c 通过比较 platform_driver->platform_device_id->name platform_device->name 是否相同来决定是否支持该设备
jz2440.dtb 的 led 节点:
#define S3C2410_GPF(_nr) ((5<<16) + (_nr))
led { compatible = "jz2440_led"; reg = <S3C2410_GPF(5) 1>; };
2.如何编译使用dts文件:
1. 将jz2440.dtbs文件传入 linux-4.19-rc3/arch/arm/boot/dts
2. 进入内核根目录,执行 make dtbs
3. 拷贝编译好的dtb文件到网络文件系统 cp linux-4.19-rc3/arch/arm/boot/dts/jz2440.dtb /work/nfs_root/
4. 重启开发板,进入u-boot 命令模式,从网络文件系统下载jz2440.dtb文件 nfs 32000000 192.168.1.123:/work/nfs_root/jz2440.dtb
5. 擦除 device_tree 分区,写入文件 nand erase device_tree & nand write.jiffs2 32000000 device_tree
6. 启动内核 boot
7. 启动后,进入 /sys/devices/platform/50005.led/of_node(open for node) ==> compatible , name , reg
cat compatible ==> jz2440_led/sys/fireware/devicetree/base/led
cat name ==> led/sys/fireware/devicetree/base/led
hexdump -C reg ==> 00 05 00 05 00 00 00 01 (大字节序) <==> S3C2410_GPF(5) 1