Search code examples
clinuxlinux-device-driverdevicedevice-tree

what is the use of "of_match_ptr()" in device drivers


During my work in device drivers, I came to know that of_match_table() is to point the tuple of compatibility strings.

However, I couldn't find the importance of of_match_ptr().


Solution

  • of_match_ptr() is a preprocessor macro defined in include/linux/of.h:

    #ifdef CONFIG_OF
    #define of_match_ptr(_ptr)    (_ptr)
    #else /* CONFIG_OF */
    #define of_match_ptr(_ptr)    NULL
    #endif /* CONFIG_OF */
    

    When the kernel is compiled with Device Tree and Open Firmware support, i.e. CONFIG_OF=y it will result in the passed pointer. Otherwise it will yield NULL.

    Using the macro instead of directly passing the pointer, is to guard the of_match_table member. There are bus drivers which check of_match_table for NULL to not dereference an invalid pointer.

    However, many drivers are compiled in-tree with a compile switch defined in their corresponding Kconfig that already depends on CONFIG_OF. For example, the compile switch of a driver for analog device ADV748X video decoder has this dependency:

    config VIDEO_ADV748X
        tristate "Analog Devices ADV748x decoder"
        depends on VIDEO_V4L2 && I2C
        depends on OF
        select MEDIA_CONTROLLER
        select VIDEO_V4L2_SUBDEV_API
        select REGMAP_I2C
        select V4L2_FWNODE
        help
          V4L2 subdevice driver for the Analog Devices
          ADV7481 and ADV7482 HDMI/Analog video decoders.
    
          To compile this driver as a module, choose M here: the
          module will be called adv748x.
    

    and omits the macro in drivers/media/i2c/adv748x/adv748x-core.c:

    static struct i2c_driver adv748x_driver = {
        .driver = {
            .name = "adv748x",
            .of_match_table = adv748x_of_table,
            .pm = &adv748x_pm_ops,
        },
        .probe_new = adv748x_probe,
        .remove = adv748x_remove,
    };