Hi Luca,

On Tue, Feb 03, 2026 at 11:35:25AM +0100, Luca Ceresoli wrote:
> This driver obtains a bridge pointer from of_drm_find_bridge() in the probe
> function and stores it until driver removal. of_drm_find_bridge() is
> deprecated. Move to of_drm_find_and_get_bridge() for the bridge to be
> refcounted and use bridge->next_bridge to put the reference on
> deallocation.
> 
> To keep the code as simple and reliable as possible, get a reference for
> each pointer that stores a drm_bridge address when it is stored and release
> it when the pointer is overwritten or goes out of scope. Also remove the
> intermediate selected_bridge variable to reduce the refcounted variables in
> the function. The involved pointers are:
> 
>  * next_bridge loop-local variable:
>    - get reference by of_drm_find_and_get_bridge()
>    - put reference at the end of the loop iteration (__free)
> 
>  * pl->bridge.next_bridge, tied to struct imx8qxp_pixel_link lifetime:
>    - get reference when assigned (by copy from next_bridge)
>    - put reference before reassignment if reassignment happens
>    - put reference when the struct imx8qxp_pixel_link embedding the
>      struct drm_bridge is destroyed (struct drm_bridge::next_bridge)
> 
> Additionally, split the somewhat complex if() for readability.
> 
> Signed-off-by: Luca Ceresoli <[email protected]>
> 
> ---
> 
> Changes in v5:
> - rewrite commit message after Liu's review to clarify the per-pointer
>   get/put idea
> - split the if()s involved in selcting the bridge
> - remove intermediate selected_bridge pointer
> - removed Maxime's R-by, patch changed
> ---
>  drivers/gpu/drm/bridge/imx/imx8qxp-pixel-link.c | 17 ++++++++++-------
>  1 file changed, 10 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/gpu/drm/bridge/imx/imx8qxp-pixel-link.c 
> b/drivers/gpu/drm/bridge/imx/imx8qxp-pixel-link.c
> index 91e4f4d55469..e29e099b893a 100644
> --- a/drivers/gpu/drm/bridge/imx/imx8qxp-pixel-link.c
> +++ b/drivers/gpu/drm/bridge/imx/imx8qxp-pixel-link.c
> @@ -23,7 +23,6 @@
>  
>  struct imx8qxp_pixel_link {
>       struct drm_bridge bridge;
> -     struct drm_bridge *next_bridge;
>       struct device *dev;
>       struct imx_sc_ipc *ipc_handle;
>       u8 stream_id;
> @@ -140,7 +139,7 @@ static int imx8qxp_pixel_link_bridge_attach(struct 
> drm_bridge *bridge,
>       }
>  
>       return drm_bridge_attach(encoder,
> -                              pl->next_bridge, bridge,
> +                              pl->bridge.next_bridge, bridge,
>                                DRM_BRIDGE_ATTACH_NO_CONNECTOR);
>  }
>  
> @@ -260,7 +259,6 @@ static int imx8qxp_pixel_link_find_next_bridge(struct 
> imx8qxp_pixel_link *pl)
>  {
>       struct device_node *np = pl->dev->of_node;
>       struct device_node *port;
> -     struct drm_bridge *selected_bridge = NULL;
>       u32 port_id;
>       bool found_port = false;
>       int reg;
> @@ -297,7 +295,8 @@ static int imx8qxp_pixel_link_find_next_bridge(struct 
> imx8qxp_pixel_link *pl)
>                       continue;
>               }
>  
> -             struct drm_bridge *next_bridge = of_drm_find_bridge(remote);
> +             struct drm_bridge *next_bridge __free(drm_bridge_put) =
> +                     of_drm_find_and_get_bridge(remote);
>               if (!next_bridge)
>                       return -EPROBE_DEFER;
>  
> @@ -305,12 +304,16 @@ static int imx8qxp_pixel_link_find_next_bridge(struct 
> imx8qxp_pixel_link *pl)
>                * Select the next bridge with companion PXL2DPI if
>                * present, otherwise default to the first bridge
>                */
> -             if (!selected_bridge || of_property_present(remote, 
> "fsl,companion-pxl2dpi"))
> -                     selected_bridge = next_bridge;
> +             if (!pl->bridge.next_bridge)
> +                     pl->bridge.next_bridge = drm_bridge_get(next_bridge);
> +
> +             if (of_property_present(remote, "fsl,companion-pxl2dpi")) {
> +                     drm_bridge_put(pl->bridge.next_bridge);
> +                     pl->bridge.next_bridge = drm_bridge_get(next_bridge);
> +             }

Can you drop the intermediate next_bridge variable to simplify the code?

-8<-
if (!pl->bridge.next_bridge) {
        pl->bridge.next_bridge = of_drm_find_and_get_bridge(remote);
        if (!pl->bridge.next_bridge)
                return -EPROBE_DEFER;
}

if (of_property_present(remote, "fsl,companion-pxl2dpi")) {
        drm_bridge_put(pl->bridge.next_bridge);
        pl->bridge.next_bridge = of_drm_find_and_get_bridge(remote);
        if (!pl->bridge.next_bridge)
                return -EPROBE_DEFER;
}
-8<-

>       }
>  
>       pl->mst_addr = port_id - 1;
> -     pl->next_bridge = selected_bridge;
>  
>       return 0;
>  }
> 

-- 
Regards,
Liu Ying

Reply via email to