How to Develop Your Own Kernel Driver and Device Tree on Linux ARM

IntroductionI only have a basic understanding of Linux, and this document serves as my personal memo. If there are any errors, I hope the experts will kindly provide guidance! Thank you!===========================================Source Code Downloadhttps://mirrors.edge.kernel.org/pub/linux/kernel/It is best to use a release version that is permanently maintained.I am using version 5.10How to Develop Your Own Kernel Driver and Device Tree on Linux ARMDevice TreeLocated in the directory arch/arm/boot/dtsHow to Develop Your Own Kernel Driver and Device Tree on Linux ARMYou can create your own device tree files.Device tree files include dtsi and dts files.The difference between the two is:

  • DTS is the source file that describes specific hardware devices, usually corresponding to a specific hardware platform or development board, used to define the device nodes, properties, and connections on that hardware.

    DTS files are used to describe the detailed configuration of a specific hardware platform. They can include one or more <span>include</span> directives to inherit general hardware descriptions and add specific hardware configurations on top of that.

    DTS files are used to describe specific development boards or devices based on that SoC, such as GPIO allocations and peripheral connections for specific boards.

  • DTSI is a file that contains general hardware descriptions, similar to header files in C language, used to share and reuse hardware descriptions across multiple DTS files.

    DTSI files typically contain general hardware descriptions, such as chip-level peripheral definitions and bus configurations, which are common across multiple hardware platforms.

    DTSI files are suitable for describing SoC (System on Chip) level general hardware features, such as processor peripheral interfaces and clock controllers.

For simple device trees, you can directly write a dts file.At this point, the main purpose of the device tree is to define and declare various pin definitions needed in the driver.How to Develop Your Own Kernel Driver and Device Tree on Linux ARMDriverLocated in the /drivers/ directoryEach type of driver hasdifferent directories.For example, character devices are placed in the char directory, and block devices are placed in the block directory.How to Develop Your Own Kernel Driver and Device Tree on Linux ARMThis section introduces two methods for driver development and compilation.1. Compile directly into the kernel (the advantage is that it runs directly without manual startup).Create a new folder /drivers/mfd/powerFirst, create a Makefile (for compiling the driver program).How to Develop Your Own Kernel Driver and Device Tree on Linux ARMNext, write a Kconfig file (for generating menuconfig options).How to Develop Your Own Kernel Driver and Device Tree on Linux ARMThen, write your own driver file (to implement functionality).How to Develop Your Own Kernel Driver and Device Tree on Linux ARMAt this point, it is not finished yet; you need to submit all configurations of your newly created folder to the parent directory’s mfd Kconfig and Makefile.Kconfig addition:source “drivers/mfd/power/Kconfig”How to Develop Your Own Kernel Driver and Device Tree on Linux ARMMakefile addition:obj-$(CONFIG_POWER_DETEC) += power/How to Develop Your Own Kernel Driver and Device Tree on Linux ARMCross CompilationSince it is based on the ARM architecture, cross-compilation is naturally required.You can refer to various online tutorials to write your own configuration file.How to Develop Your Own Kernel Driver and Device Tree on Linux ARMBefore compiling the driver program, source your configuration file.Then run:make -j32 zImage dtbs2. Compile into .ko files (requires manual loading).Refer to the above Makefile; no need to modify the parent directory’s Kconfig and Makefile.How to Develop Your Own Kernel Driver and Device Tree on Linux ARMDirectly use cross-compilation to produce the ko file.Load the driver with sudo insmod hello.koUnload the driver:sudo rmmod helloI personally recommend the first method as it is more practical.

Leave a Comment