My problem:
When I tried to compile my .c file into a .ko file and install it. I get issues with the device tree. So If I run
sudo insmod my_ov5647.ko
I get problems with missing device tree. Also, if I try to rmmod the installed modules. It shows a whole list of dependencies and if it removes the dependent modules as well. I get a tainted kernel and the whole system reboots.
Background
I am working on a driver (linux module) for the camera sensor that already has a driver in the Raspbian release of the raspberrypi/linux repo. It's an I2C-based driver. So, I am trying to develop my own and trying to build and install it on my Pi.
What I tried
So I decided to clone the whole kernel and replace the release .c file with mine and rebuild the kernel. This is mainly because I am trying to use the .dtbs device tree file of this driver.
This is the driver I was trying to replace with mine ov5647.c and its device tree file can be found here ov5647-overlay.dts
Ideally, when you are using the original release driver. You can overlay it with
dtoverlay ov5647
That was my solution. It was to build the whole kernel as such and use the dtoverlay command to install it. This is the commands I used to compile the kernel.
echo "Set the KERNEL variable"
export KERNEL=kernel8
echo "Build the media drivers"
sudo make modules_install
echo "Copy the necessary device tree blobs to /boot directory"
sudo cp arch/arm64/boot/dts/broadcom/*.dtb /boot/
sudo cp arch/arm64/boot/dts/overlays/*.dtb* /boot/overlays/
sudo cp arch/arm64/boot/dts/overlays/README /boot/overlays/
echo "Copy the kernel image to /boot directory"
sudo cp arch/arm64/boot/Image.gz /boot/$KERNEL.img
Also, due to the module dependencies issue. I have to reboot and dtoverlay everytime.
Conclusion
How to compile and install modules that have dependencies and come with a specific device tree?