Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hello Word Device Tree Based device driver

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.

like image 680
Raulp Avatar asked Aug 01 '16 13:08

Raulp


People also ask

What does a device tree do?

In computing, a devicetree (also written device tree) is a data structure describing the hardware components of a particular computer so that the operating system's kernel can use and manage those components, including the CPU or CPUs, the memory, the buses and the integrated peripherals.

How do I read a DTS file?

dts files are final device trees containing board-level information. The . dtsi extension denotes “device tree source include”. The inclusion works by overlaying the tree of the including file over the tree of the included file, producing a combined compiled binary.


1 Answers

  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.

like image 92
h0ch5tr4355 Avatar answered Sep 18 '22 09:09

h0ch5tr4355