Hello Word device tree device driver

I read and almost went through all the linux kernel documentation on the device tree and device tree overlays. I can’t understand if we need to create a new application in the platform’s device tree or create new device overlays for a new driver based on the device tree. I am looking for a simple example of a LED shining driver in which the LED is connected to the GPIO output and its configuration is mentioned in the overlay of the device tree or fragment of the device tree on the platform platform. How it can be built / pushed and tested using a user space application.

+4
source share
1 answer
  • I created my own device in my device tree:

    my_device@ffdf0000 {
        compatible = "my_driver";
        reg = <0xffdf0000 0x1000> 
        /* 
         * reg = address of device and size 
         * (Minimum is System Pagesize = 0x1000 Byte in my case
         */
    }
    
  • I wrote a kernel stub for this device:

    (Here I took kernel_src/drivers/uio/uio_pdrv_genirq.cand Hans J. Koch: Userspace I/O drivers in a realtime context(device driver tutorial) as the basis.)

    This stub has the following two structures:

    Structure of_device_id:

    static struct of_device_id my_match_table[] = {
         {
                 .compatible = "my_driver",
         },
         {0}
    };
    MODULE_DEVICE_TABLE(of, my_match_table);
    

    and the driver structure 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 sensing 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 */
            [...]
    }
    

If compatiblethere is a match with the kernel record and the device tree, the probe function is called.

+3
source

Source: https://habr.com/ru/post/1649791/


All Articles