sti_gdp_atomic_check() reparents the GDP pixel clock and calls
clk_set_rate() on it with the clock of the mode being checked.

.atomic_check() must not touch the hardware.  A commit with
DRM_MODE_ATOMIC_TEST_ONLY only runs the check hooks, and a commit that
fails a later check is dropped, so in both cases the clock of a mode
that was never applied is programmed on a display that is still scanning
out the previous one.

Move the clock setup to .atomic_update(), where the rest of the plane is
programmed.  A clk_set_rate() failure can no longer reject the commit, so
it is logged once and the plane is left as it was.

The check still rejects a mode the clock cannot reach.  clk_round_rate()
reports the rate that would be selected without touching the hardware,
which is how the connectors already validate their own pixel clocks in
.mode_valid().  It rounds against the parent that is selected now, so the
check is skipped when this commit would reparent the clock.

Fixes: dd86dc2f9ae1 ("drm/sti: implement atomic_check for the planes")
Signed-off-by: Minseo Park <[email protected]>
---
 drivers/gpu/drm/sti/sti_gdp.c | 73 ++++++++++++++++++++++++-----------
 1 file changed, 51 insertions(+), 22 deletions(-)

diff --git a/drivers/gpu/drm/sti/sti_gdp.c b/drivers/gpu/drm/sti/sti_gdp.c
index d0c886029c57..4a01bc64f4ab 100644
--- a/drivers/gpu/drm/sti/sti_gdp.c
+++ b/drivers/gpu/drm/sti/sti_gdp.c
@@ -6,6 +6,7 @@
  *          for STMicroelectronics.
  */
 
+#include <linux/clk.h>
 #include <linux/dma-mapping.h>
 #include <linux/of.h>
 #include <linux/seq_file.h>
@@ -28,6 +29,9 @@
 #define BIGNOTLITTLE    BIT(23)
 #define WAIT_NEXT_VSYNC BIT(31)
 
+/* Same tolerance as the connectors use in their .mode_valid() */
+#define CLK_TOLERANCE_HZ 50
+
 /* GDP color formats */
 #define GDP_RGB565      0x00
 #define GDP_RGB888      0x01
@@ -667,29 +671,28 @@ static int sti_gdp_atomic_check(struct drm_plane 
*drm_plane,
                return -EINVAL;
        }
 
-       /* Set gdp clock */
+       /*
+        * The pixel clock is programmed from .atomic_update().  
clk_round_rate()
+        * reports the rate that would be selected without touching the
+        * hardware, so the mode can still be rejected here -- but it rounds
+        * against the parent that is selected now, so only rely on it when
+        * this commit is not going to reparent the clock.
+        */
        if (mode->clock && gdp->clk_pix) {
-               struct clk *clkp;
-               int rate = mode->clock * 1000;
-               int res;
-
-               /*
-                * According to the mixer used, the gdp pixel clock
-                * should have a different parent clock.
-                */
-               if (mixer->id == STI_MIXER_MAIN)
-                       clkp = gdp->clk_main_parent;
-               else
-                       clkp = gdp->clk_aux_parent;
-
-               if (clkp)
-                       clk_set_parent(gdp->clk_pix, clkp);
-
-               res = clk_set_rate(gdp->clk_pix, rate);
-               if (res < 0) {
-                       DRM_ERROR("Cannot set rate (%dHz) for gdp\n",
-                                 rate);
-                       return -EINVAL;
+               struct clk *clkp = mixer->id == STI_MIXER_MAIN ?
+                                  gdp->clk_main_parent : gdp->clk_aux_parent;
+               int target = mode->clock * 1000;
+               long rounded;
+
+               if (!clkp || clk_is_match(clk_get_parent(gdp->clk_pix), clkp)) {
+                       rounded = clk_round_rate(gdp->clk_pix, target);
+                       if (rounded < target - CLK_TOLERANCE_HZ ||
+                           rounded > target + CLK_TOLERANCE_HZ) {
+                               drm_dbg_kms(drm_plane->dev,
+                                           "gdp pixclk=%d not supported\n",
+                                           target);
+                               return -EINVAL;
+                       }
                }
        }
 
@@ -757,6 +760,32 @@ static void sti_gdp_atomic_update(struct drm_plane 
*drm_plane,
        }
 
        mode = &crtc->mode;
+
+       /* Set gdp clock */
+       if (mode->clock && gdp->clk_pix) {
+               struct sti_mixer *mixer = to_sti_mixer(crtc);
+               int rate = mode->clock * 1000;
+               struct clk *clkp;
+
+               /*
+                * According to the mixer used, the gdp pixel clock
+                * should have a different parent clock.
+                */
+               if (mixer->id == STI_MIXER_MAIN)
+                       clkp = gdp->clk_main_parent;
+               else
+                       clkp = gdp->clk_aux_parent;
+
+               if (clkp)
+                       clk_set_parent(gdp->clk_pix, clkp);
+
+               if (clk_set_rate(gdp->clk_pix, rate) < 0) {
+                       drm_err_once(drm_plane->dev,
+                                    "Cannot set rate (%dHz) for gdp\n", rate);
+                       return;
+               }
+       }
+
        dst_x = newstate->crtc_x;
        dst_y = newstate->crtc_y;
        dst_w = clamp_val(newstate->crtc_w, 0, mode->hdisplay - dst_x);
-- 
2.43.0

Reply via email to