Hello Word Device Tree Based device driver
Asked Answered
O

2

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.

Outleap answered 1/8, 2016 at 13:9 Comment(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).Subteen
can you please post the dts file content you inserted into the platfrom dtsi?and how did you do and build/test it?Outleap
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_idSubteen
can you please elaborate with the example in the answer section ?About the dtsi file as well?Outleap
I think my knowledge is too vague and I don't know if it will help you.Subteen
Still , no harm in sharing ! I will use my discretion :) ! Thanks in advance !Outleap
S
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.

Subteen answered 5/8, 2016 at 9:23 Comment(2)
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 !Outleap
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?Leptospirosis
S
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.

Stupid answered 7/6, 2018 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.