drm_panel_prepare() cannot report a failure to its caller, so a panel
whose prepare callback failed is left unprepared while the bridge enable
chain still calls drm_panel_enable(). enable() cannot rely on the
prepared flag for this, as some drivers call it without preparing first
(sti_dvo_pre_enable()), so record the failure separately.

Set prepare_failed when the callback fails and skip the enable callback,
its backlight and the followers while it is set. unprepare() clears it,
as none of them ran, and disable() only uses it to stay quiet about
disabling a panel that was never enabled: a panel that was enabled before
the failed prepare is still disabled.

All reads and writes of the flag happen with follower_lock held, which
unprepare() and disable() now take before looking at the panel state.

Signed-off-by: Qianhui Xu <[email protected]>
---
Prepared with AI assistance (pi coding agent); reviewed and edited by me.

 drivers/gpu/drm/drm_panel.c | 39 +++++++++++++++++++++++++++++--------
 include/drm/drm_panel.h     |  8 ++++++++
 2 files changed, 39 insertions(+), 8 deletions(-)

diff --git a/drivers/gpu/drm/drm_panel.c b/drivers/gpu/drm/drm_panel.c
index 26d9378..71ff02c 100644
--- a/drivers/gpu/drm/drm_panel.c
+++ b/drivers/gpu/drm/drm_panel.c
@@ -588,10 +588,13 @@ void drm_panel_prepare(struct drm_panel *panel)
 
        mutex_lock(&panel->follower_lock);
 
+       panel->prepare_failed = false;
        if (panel->funcs && panel->funcs->prepare) {
                ret = panel->funcs->prepare(panel);
-               if (ret < 0)
+               if (ret < 0) {
+                       panel->prepare_failed = true;
                        goto exit;
+               }
        }
        panel->prepared = true;
 
@@ -627,6 +630,18 @@ void drm_panel_unprepare(struct drm_panel *panel)
        if (!panel)
                return;
 
+       mutex_lock(&panel->follower_lock);
+
+       /*
+        * A failed prepare() returned before the panel callbacks and the
+        * followers ran, so there is nothing to undo here. Drop the failure
+        * so that the check below does not report it as a driver bug.
+        */
+       if (panel->prepare_failed) {
+               panel->prepare_failed = false;
+               goto exit;
+       }
+
        /*
         * If you are seeing the warning below it likely means one of two 
things:
         * - Your panel driver incorrectly calls drm_panel_unprepare() in its
@@ -638,11 +653,9 @@ void drm_panel_unprepare(struct drm_panel *panel)
         */
        if (!panel->prepared) {
                dev_warn(panel->dev, "Skipping unprepare of already unprepared 
panel\n");
-               return;
+               goto exit;
        }
 
-       mutex_lock(&panel->follower_lock);
-
        list_for_each_entry(follower, &panel->followers, list) {
                if (!follower->funcs->panel_unpreparing)
                        continue;
@@ -690,6 +703,11 @@ void drm_panel_enable(struct drm_panel *panel)
 
        mutex_lock(&panel->follower_lock);
 
+       if (panel->prepare_failed) {
+               dev_warn(panel->dev, "Skipping enable of panel that failed to 
prepare\n");
+               goto exit;
+       }
+
        if (panel->funcs && panel->funcs->enable) {
                ret = panel->funcs->enable(panel);
                if (ret < 0)
@@ -733,6 +751,8 @@ void drm_panel_disable(struct drm_panel *panel)
        if (!panel)
                return;
 
+       mutex_lock(&panel->follower_lock);
+
        /*
         * If you are seeing the warning below it likely means one of two 
things:
         * - Your panel driver incorrectly calls drm_panel_disable() in its
@@ -743,12 +763,15 @@ void drm_panel_disable(struct drm_panel *panel)
         *   figure out how to reverse the order of the shutdown() callbacks.
         */
        if (!panel->enabled) {
-               dev_warn(panel->dev, "Skipping disable of already disabled 
panel\n");
-               return;
+               /*
+                * A failed prepare() is an expected reason for getting here,
+                * so only the warning is dropped.
+                */
+               if (!panel->prepare_failed)
+                       dev_warn(panel->dev, "Skipping disable of already 
disabled panel\n");
+               goto exit;
        }
 
-       mutex_lock(&panel->follower_lock);
-
        list_for_each_entry(follower, &panel->followers, list) {
                if (!follower->funcs->panel_disabling)
                        continue;
diff --git a/include/drm/drm_panel.h b/include/drm/drm_panel.h
index d9059a9..f218452 100644
--- a/include/drm/drm_panel.h
+++ b/include/drm/drm_panel.h
@@ -280,6 +280,14 @@ struct drm_panel {
         * If true then the panel has been enabled.
         */
        bool enabled;
+
+       /**
+        * @prepare_failed:
+        *
+        * Set when the panel's prepare() callback failed. enable() is skipped
+        * while it is set. A later prepare() or unprepare() clears it.
+        */
+       bool prepare_failed;
 };
 
 void *__devm_drm_panel_alloc(struct device *dev, size_t size, size_t offset,
-- 
2.53.0

Reply via email to