Search code examples
driverdevice-treekernel-mode

Does loadable kernel module(LVM) also need device tree?


Recently, I beginning research how to write SPI ADC driver(ADS7950) on the raspberry pi 4 with Linux.

I read this book which tell me I can get good flexibility with LKM driver.

I know every device should be initialized with device tree when Linux boot.

Device tree pass the features into device driver such as clock frequency and device address in memory.

So if I just make LKM module which means driver will initialize after boot.

Should I also make(append) device for LKM drive?


Solution

  • The device tree is loaded separately whether your driver or the applicable device is already present or not. Think of it as a configuration storage for drivers. If your driver is not loaded at boot then the kernel marks the device & driver for "deferred probing" at a later time.

    Your driver should still include the "compatible" id's that you use in your device tree, loaded via MODULE_DEVICE_TABLE(), which is how the OF framework will "know" what to do with your driver once it does appear. The "compatible" string is the name OF uses to link to your driver. Likewise, your driver code can and should reference OF objects in the device tree to set your driver configurations, as required.

    Finally, make sure your drive source includes the "module_platform_driver()" call, or one of the similar calls Linux offers. This replaces the old-style "module_init" and "module_cleanup" calls of old. When your module is loaded, then this macro registers your driver, points where the module_probe() function is, and passes the .of_match_table above to tie it into device tree.