I have read and almost gone through all the linux kernel documentation on the device tree and device tree overlays.I am not able to understand if we have to create a new entree in the device tree of the platform or to create a new overlay for the device for a new driver based on device tree. I am looking for a simple led glowing driver example where led is connected to GPIO pin and its configuration is mentioned in the device tree overlay or device tree fragment on the board's platform.How can it be build/pushed and tested using the user space application.
2 Answers
I created a custom device in my device tree:
my_device@ffdf0000 { compatible = "my_driver"; reg = <0xffdf0000 0x1000> /* * reg = address of device and size * (Minimum is System's Pagesize = 0x1000 Byte in my case */ }
I wrote a Kernel stub for this Device:
(Here I took
kernel_src/drivers/uio/uio_pdrv_genirq.c
andHans J. Koch: Userspace I/O drivers in a realtime context
(device driver tutorial) as basis.)This stub has following two structs:
The of_device_id struct:
static struct of_device_id my_match_table[] = { { .compatible = "my_driver", }, {0} }; MODULE_DEVICE_TABLE(of, my_match_table);
and the driver struct itself:
static struct platform_driver my_platform_driver = { .probe = my_probe, .remove = my_remove, .driver = { .name = "my_driver", .owner = THIS_MODULE, .of_match_table = of_match_ptr(my_match_table), }, };
Now I have access to the properties of the device tree in my probe function:
static int my_probe(struct platform_device *dev) { struct uio_info *uioinfo; struct resource *r = &dev->resource[0]; [...] uioinfo->name = dev->dev.of_node->name /* name from device tree: "my_device" */ uioinfo->mem[0].addr = r->start; /* device address from device tree */ uioinfo->mem[0].size = resource_size(r); /* size from device tree */ [...] }
When there is a match in compatible
with both the kernel stub's entry and the device tree, the probe function is called.
Use QEMU with a minimal custom hardware device
I recommend you code up your own minimal platform device, to also control the hardware and have a full understanding.
I have provided a full runnable Buildroot QEMU example with source code on GitHub as detailed on this answer: How to program Linux .dts device tree files?
The example is also documented on the GitHub repository.
kernel_src/drivers/uio/uio_pdrv_genirq.c
. The information from device tree is received through theof
functions (Open Firmware). You need a Kernel stub as well, where I recommendHans J. Koch: Userspace I/O drivers in a realtime context
(generally this author wrote several papers about UIO). – h0ch5tr4355struct of_device_id
– h0ch5tr4355