17
votes

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
I had a similar issue and solved it with the example of kernel_src/drivers/uio/uio_pdrv_genirq.c. The information from device tree is received through the of functions (Open Firmware). You need a Kernel stub as well, where I recommend Hans J. Koch: Userspace I/O drivers in a realtime context (generally this author wrote several papers about UIO).h0ch5tr4355
can you please post the dts file content you inserted into the platfrom dtsi?and how did you do and build/test it?Raulp
I just used a custom dtsi with custom compatible and the appropriate reg properties. In the device driver I fetched the compaitble in the struct of_device_idh0ch5tr4355
can you please elaborate with the example in the answer section ?About the dtsi file as well?Raulp
I think my knowledge is too vague and I don't know if it will help you.h0ch5tr4355

2 Answers

21
votes
  1. 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
         */
    }
    
  2. I wrote a Kernel stub for this Device:

    (Here I took kernel_src/drivers/uio/uio_pdrv_genirq.c and Hans 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),
            },
    };
    
  3. 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.

3
votes

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.