Implementing Device Tree Overlay Support in Custom GPIO Serial Communication Device Driver

Implementing Device Tree Overlay Support in Custom GPIO Serial Communication Device Driver

This article is a continuation of our previous article series building a custom GPIO serial communication driver. Please refer the articles links,


Why we Device Tree Overlay in this driver?

  • Runtime hardware configurations, In this driver changing the GPIO pin assignment without rebuilding the driver, enables testing different pins in different hardware by simply updating the overlays.
  • Modularity, hardware description remains separate from the driver implementations.


Device Tree Overlay Implementation

Device tree overlay implementation for this driver is as follows,

/dts-v1/;
/plugin/;

/ {
    compatible = "brcm,bcm2835";

    fragment@0 {
        target = <&soc>;
        __overlay__ {
            gpioserdev {
                compatible = "gpioserdev";
                gpios = <&gpio 16 0>, <&gpio 19 0>;
                strobe-gpios = <&gpio 19 0>;
                data-gpios = <&gpio 16 0>;
            };
        };
    };
};        

Driver support for Device Tree Overlay

The driver has been modified to support device tree configuration, the main changes includes.,

static int gpioserdev_pinsetup(void) {
    struct device_node *np;

    np = of_find_node_by_name(NULL, "gpioserdev");
    if (!np) {
        printk("gpioserdev: device tree node not found\n");
        return -ENODEV;
    }

    gpioserdev_strobe_pin = of_get_named_gpio(np, "strobe-gpios", 0);
    if (!gpio_is_valid(gpioserdev_strobe_pin)) {
        printk("gpioserdev: invalid strobe GPIO\n");
        return -ENODEV;
    }

    gpioserdev_data_pin = of_get_named_gpio(np, "data-gpios", 0);
    if (!gpio_is_valid(gpioserdev_data_pin)) {
        printk("gpioserdev: invalid data GPIO\n");
        return -ENODEV;
    }
    // ... rest of intialization
}        

Testing and Validation

Hardware Setup:

  • GPIO16 (data pin) connects to GPIO20 (test point)
  • GPIO19 (strobe pin) connects to GPIO26 (test point)

Device Tree Overlay verification:

Once the device tree overlay is compiled and installed into the system, we can verify the overlay by,

verification of the device tree overlay loading

Testing the driver:

testing the driver

Conclusion

Implementing device tree overlay support in our Custom GPIO Serial Communication Driver has improved flexibility.

We'll explore the including this driver into a simple build system in the next article. Let me know your thoughts...!


要查看或添加评论,请登录

Pravin Raghul的更多文章

社区洞察

其他会员也浏览了