Read out the state of the bridge from the hardware, so that a video
stream left running by the bootloader is taken over without
initializing the bridge again.

Signed-off-by: Dario Binacchi <[email protected]>

---

Changes in v2:
- Update the atomic_sro_install_state hook to its new prototype
- Save the mode read out from the hardware, as no mode_set is run and
  the PLL is configured from it when the bridge is enabled again

 drivers/gpu/drm/bridge/samsung-dsim.c | 130 +++++++++++++++++++++++---
 1 file changed, 117 insertions(+), 13 deletions(-)

diff --git a/drivers/gpu/drm/bridge/samsung-dsim.c 
b/drivers/gpu/drm/bridge/samsung-dsim.c
index 480e568dd2c9..9b950e2e779d 100644
--- a/drivers/gpu/drm/bridge/samsung-dsim.c
+++ b/drivers/gpu/drm/bridge/samsung-dsim.c
@@ -20,11 +20,15 @@
 #include <linux/of.h>
 #include <linux/phy/phy.h>
 #include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
 #include <linux/units.h>
 
 #include <video/mipi_display.h>
 
 #include <drm/bridge/samsung-dsim.h>
+#include <drm/drm_atomic_state_readout.h>
+#include <drm/drm_atomic_state_readout_helper.h>
+#include <drm/drm_connector.h>
 #include <drm/drm_print.h>
 
 /* returns true iff both arguments logically differs */
@@ -1562,6 +1566,17 @@ static int samsung_dsim_transfer(struct samsung_dsim 
*dsi,
        return xfer->result;
 }
 
+static void samsung_dsim_unmask_irqs(struct samsung_dsim *dsi)
+{
+       unsigned long mask = ~(DSIM_INT_RX_DONE |
+                              DSIM_INT_SFR_FIFO_EMPTY |
+                              DSIM_INT_SFR_HDR_FIFO_EMPTY |
+                              DSIM_INT_RX_ECC_ERR |
+                              DSIM_INT_SW_RST_RELEASE);
+
+       samsung_dsim_write(dsi, DSIM_INTMSK_REG, mask);
+}
+
 static irqreturn_t samsung_dsim_irq(int irq, void *dev_id)
 {
        struct samsung_dsim *dsi = dev_id;
@@ -1578,12 +1593,7 @@ static irqreturn_t samsung_dsim_irq(int irq, void 
*dev_id)
        samsung_dsim_write(dsi, DSIM_INTSRC_REG, status);
 
        if (status & DSIM_INT_SW_RST_RELEASE) {
-               unsigned long mask = ~(DSIM_INT_RX_DONE |
-                                      DSIM_INT_SFR_FIFO_EMPTY |
-                                      DSIM_INT_SFR_HDR_FIFO_EMPTY |
-                                      DSIM_INT_RX_ECC_ERR |
-                                      DSIM_INT_SW_RST_RELEASE);
-               samsung_dsim_write(dsi, DSIM_INTMSK_REG, mask);
+               samsung_dsim_unmask_irqs(dsi);
                complete(&dsi->completed);
                return IRQ_HANDLED;
        }
@@ -1758,14 +1768,9 @@ samsung_dsim_atomic_get_input_bus_fmts(struct drm_bridge 
*bridge,
        return input_fmts;
 }
 
-static int samsung_dsim_atomic_check(struct drm_bridge *bridge,
-                                    struct drm_bridge_state *bridge_state,
-                                    struct drm_crtc_state *crtc_state,
-                                    struct drm_connector_state *conn_state)
+static void samsung_dsim_fixup_mode(struct samsung_dsim *dsi,
+                                   struct drm_display_mode *adjusted_mode)
 {
-       struct samsung_dsim *dsi = bridge_to_dsi(bridge);
-       struct drm_display_mode *adjusted_mode = &crtc_state->adjusted_mode;
-
        /*
         * The i.MX8M Mini/Nano glue logic between LCDIF and DSIM
         * inverts HS/VS/DE sync signals polarity, therefore, while
@@ -1808,6 +1813,16 @@ static int samsung_dsim_atomic_check(struct drm_bridge 
*bridge,
                        adjusted_mode->htotal      += remainder;
                }
        }
+}
+
+static int samsung_dsim_atomic_check(struct drm_bridge *bridge,
+                                    struct drm_bridge_state *bridge_state,
+                                    struct drm_crtc_state *crtc_state,
+                                    struct drm_connector_state *conn_state)
+{
+       struct samsung_dsim *dsi = bridge_to_dsi(bridge);
+
+       samsung_dsim_fixup_mode(dsi, &crtc_state->adjusted_mode);
 
        return 0;
 }
@@ -1831,6 +1846,92 @@ static int samsung_dsim_attach(struct drm_bridge *bridge,
                                 flags);
 }
 
+static bool samsung_dsim_hw_is_running(struct samsung_dsim *dsi)
+{
+       const struct samsung_dsim_driver_data *driver_data = dsi->driver_data;
+       u32 reg;
+
+       if (!(samsung_dsim_read(dsi, DSIM_PLLCTRL_REG) & DSIM_PLL_EN))
+               return false;
+
+       if (driver_data->has_legacy_status_reg)
+               reg = samsung_dsim_read(dsi, DSIM_STATUS_REG);
+       else
+               reg = samsung_dsim_read(dsi, DSIM_LINK_STATUS_REG);
+
+       if (!(reg & BIT(driver_data->pll_stable_bit)))
+               return false;
+
+       return samsung_dsim_read(dsi, DSIM_MDRESOL_REG) & DSIM_MAIN_STAND_BY;
+}
+
+static int samsung_dsim_readout_state(struct drm_bridge *bridge,
+                                     struct drm_atomic_sro_state *state,
+                                     struct drm_bridge_state *bridge_state,
+                                     struct drm_crtc_state *crtc_state,
+                                     struct drm_connector_state *conn_state)
+{
+       struct samsung_dsim *dsi = bridge_to_dsi(bridge);
+       struct drm_display_info *info = &conn_state->connector->display_info;
+       u32 format = MEDIA_BUS_FMT_FIXED;
+       bool running;
+       int ret;
+
+       ret = pm_runtime_resume_and_get(dsi->dev);
+       if (ret < 0)
+               return ret;
+
+       running = samsung_dsim_hw_is_running(dsi);
+       pm_runtime_put(dsi->dev);
+
+       /* The pipeline is active but the video stream is not */
+       if (!running)
+               return -EINVAL;
+
+       /* Rebuild the bus configuration the atomic helpers negotiate */
+       if (info->num_bus_formats)
+               format = info->bus_formats[0];
+
+       bridge_state->output_bus_cfg.format = format;
+       bridge_state->output_bus_cfg.flags = info->bus_flags;
+
+       if (!samsung_dsim_pixel_output_fmt_supported(format))
+               format = MEDIA_BUS_FMT_RGB888_1X24;
+
+       bridge_state->input_bus_cfg.format = format;
+       bridge_state->input_bus_cfg.flags = info->bus_flags;
+
+       /* The mode fixups done in atomic_check can't be read back */
+       samsung_dsim_fixup_mode(dsi, &crtc_state->adjusted_mode);
+
+       return 0;
+}
+
+static void samsung_dsim_install_state(struct drm_bridge *bridge,
+                                      struct drm_bridge_state *bridge_state,
+                                      struct drm_crtc_state *crtc_state,
+                                      struct drm_connector_state *conn_state)
+{
+       struct samsung_dsim *dsi = bridge_to_dsi(bridge);
+
+       /* No mode_set is run for a state read out from the hardware */
+       drm_mode_copy(&dsi->mode, &crtc_state->adjusted_mode);
+
+       /* Take the reference samsung_dsim_atomic_pre_enable() would have */
+       if (pm_runtime_resume_and_get(dsi->dev) < 0) {
+               dev_err(dsi->dev, "failed to enable DSI device.\n");
+               return;
+       }
+
+       samsung_dsim_write(dsi, DSIM_INTSRC_REG,
+                          samsung_dsim_read(dsi, DSIM_INTSRC_REG));
+       samsung_dsim_unmask_irqs(dsi);
+       samsung_dsim_enable_irq(dsi);
+
+       dsi->state |= DSIM_STATE_ENABLED | DSIM_STATE_INITIALIZED |
+                     DSIM_STATE_VIDOUT_AVAILABLE;
+}
+
 static const struct drm_bridge_funcs samsung_dsim_bridge_funcs = {
        .atomic_duplicate_state         = 
drm_atomic_helper_bridge_duplicate_state,
        .atomic_destroy_state           = 
drm_atomic_helper_bridge_destroy_state,
@@ -1843,6 +1944,9 @@ static const struct drm_bridge_funcs 
samsung_dsim_bridge_funcs = {
        .atomic_post_disable            = samsung_dsim_atomic_post_disable,
        .mode_set                       = samsung_dsim_mode_set,
        .attach                         = samsung_dsim_attach,
+       .atomic_sro_readout_state       = samsung_dsim_readout_state,
+       .atomic_sro_install_state       = samsung_dsim_install_state,
+       .atomic_sro_compare_state       = 
drm_atomic_helper_bridge_compare_state,
 };
 
 static irqreturn_t samsung_dsim_te_irq_handler(int irq, void *dev_id)
-- 
2.43.0

Reply via email to