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.10Device TreeLocated in the directory arch/arm/boot/dts
You 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.DriverLocated 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.
This 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).
Next, write a Kconfig file (for generating menuconfig options).
Then, write your own driver file (to implement functionality).
At 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”
Makefile addition:obj-$(CONFIG_POWER_DETEC) += power/
Cross 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.
Before 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.
Directly 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.