18

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.

6
  • 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). Commented Aug 3, 2016 at 6:18
  • can you please post the dts file content you inserted into the platfrom dtsi?and how did you do and build/test it? Commented Aug 4, 2016 at 8:05
  • 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_id Commented Aug 4, 2016 at 8:24
  • can you please elaborate with the example in the answer section ?About the dtsi file as well? Commented Aug 4, 2016 at 8:27
  • I think my knowledge is too vague and I don't know if it will help you. Commented Aug 5, 2016 at 6:42

2 Answers 2

22
  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.

Sign up to request clarification or add additional context in comments.

2 Comments

please pm me at raulpblooper at the rate gmail dot com with the subject line your user name : h0ch5tr4355 : Device tree.I want to discuss more on it.Thankss for sharing the example !!Waiting for your mail.Thanks once again !
I'm a newbie on device tree as well. I just wonder how you got the address of the reg property?. Or can I set an arbitrary address?
4

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.