This has not been tested, but if it works should be squashed.

Signed-off-by: Maxime Ripard <[email protected]>
---
 drivers/gpu/drm/tidss/tidss_dispc.c |   5 ++
 drivers/gpu/drm/tidss/tidss_dispc.h |   1 +
 drivers/gpu/drm/tidss/tidss_oldi.c  | 138 ++++++++++++++++++++++++++++++++++++
 3 files changed, 144 insertions(+)

diff --git a/drivers/gpu/drm/tidss/tidss_dispc.c 
b/drivers/gpu/drm/tidss/tidss_dispc.c
index c3117745965b..023dd6135cd8 100644
--- a/drivers/gpu/drm/tidss/tidss_dispc.c
+++ b/drivers/gpu/drm/tidss/tidss_dispc.c
@@ -561,4 +561,9 @@ void tidss_disable_oldi(struct tidss_device *tidss, u32 
hw_videoport)
 }
 
+u32 tidss_get_oldi_config(struct tidss_device *tidss, u32 hw_videoport)
+{
+       return dispc_vp_read(tidss->dispc, hw_videoport, DISPC_VP_DSS_OLDI_CFG);
+}
+
 /*
  * TRM gives bitfields as start:end, where start is the higher bit
diff --git a/drivers/gpu/drm/tidss/tidss_dispc.h 
b/drivers/gpu/drm/tidss/tidss_dispc.h
index 159fe3b94b0a..2322abd1c181 100644
--- a/drivers/gpu/drm/tidss/tidss_dispc.h
+++ b/drivers/gpu/drm/tidss/tidss_dispc.h
@@ -107,4 +107,5 @@ int tidss_configure_oldi(struct tidss_device *tidss, u32 
hw_videoport,
                         u32 oldi_cfg);
 void tidss_disable_oldi(struct tidss_device *tidss, u32 hw_videoport);
+u32 tidss_get_oldi_config(struct tidss_device *tidss, u32 hw_videoport);
 unsigned int dispc_pclk_diff(unsigned long rate, unsigned long real_rate);
 
diff --git a/drivers/gpu/drm/tidss/tidss_oldi.c 
b/drivers/gpu/drm/tidss/tidss_oldi.c
index 82b059f85244..7dbfbb380205 100644
--- a/drivers/gpu/drm/tidss/tidss_oldi.c
+++ b/drivers/gpu/drm/tidss/tidss_oldi.c
@@ -6,4 +6,5 @@
  */
 
+#include <linux/bitfield.h>
 #include <linux/clk.h>
 #include <linux/of.h>
@@ -14,6 +15,10 @@
 
 #include <drm/drm_atomic_helper.h>
+#include <drm/drm_atomic_state_readout.h>
+#include <drm/drm_atomic_state_readout_helper.h>
 #include <drm/drm_bridge.h>
+#include <drm/drm_connector.h>
 #include <drm/drm_of.h>
+#include <drm/drm_panel.h>
 
 #include "tidss_dispc.h"
@@ -352,4 +357,133 @@ tidss_oldi_mode_valid(struct drm_bridge *bridge,
 }
 
+static u32 tidss_oldi_read_config(struct tidss_oldi *oldi)
+{
+       u32 cfg;
+
+       tidss_runtime_get(oldi->tidss);
+       cfg = tidss_get_oldi_config(oldi->tidss, oldi->parent_vp);
+       tidss_runtime_put(oldi->tidss);
+
+       return cfg;
+}
+
+static int
+tidss_oldi_bridge_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 tidss_oldi *oldi = drm_bridge_to_tidss_oldi(bridge);
+       const struct oldi_bus_format *bus_fmt = NULL;
+       unsigned int i;
+       u32 cfg, map;
+
+       cfg = tidss_oldi_read_config(oldi);
+
+       /* The transmitter is off, there is nothing to read out. */
+       if (!(cfg & OLDI_ENABLE))
+               return 0;
+
+       /*
+        * The most significant bit of the data mapping field carries
+        * the dual-link flag rather than the LVDS mapping: mask it out
+        * before looking for the bus format the mapping corresponds
+        * to.
+        */
+       map = cfg & (OLDI_MAP & ~BIT(3));
+
+       for (i = 0; i < ARRAY_SIZE(oldi_bus_formats); i++) {
+               if (oldi_bus_formats[i].oldi_mode_reg_val << 1 != map)
+                       continue;
+
+               if ((cfg & OLDI_MSB) ?
+                   oldi_bus_formats[i].data_width != 24 :
+                   oldi_bus_formats[i].data_width != 18)
+                       continue;
+
+               bus_fmt = &oldi_bus_formats[i];
+               break;
+       }
+
+       if (!bus_fmt) {
+               dev_err(oldi->dev,
+                       "OLDI%u: unrecognised mapping in the config register\n",
+                       oldi->oldi_instance);
+               return -EINVAL;
+       }
+
+       /*
+        * Reconstruct the bus configuration the atomic helpers
+        * negotiate: the OLDI transmitter takes the parallel RGB
+        * output of the video port and serialises it with the LVDS
+        * mapping found in the config register.
+        */
+       bridge_state->input_bus_cfg.format = bus_fmt->input_bus_fmt;
+       bridge_state->output_bus_cfg.format = bus_fmt->bus_fmt;
+
+       /*
+        * tidss_oldi_atomic_check() forces positive edge sampling on
+        * the input side, and the DE polarity is programmed from the
+        * negotiated flags: OLDI_DEPOL is set when the DE is active
+        * low.
+        */
+       bridge_state->input_bus_cfg.flags =
+               DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE |
+               DRM_BUS_FLAG_SYNC_SAMPLE_POSEDGE |
+               ((cfg & OLDI_DEPOL) ? DRM_BUS_FLAG_DE_LOW :
+                                     DRM_BUS_FLAG_DE_HIGH);
+
+       bridge_state->output_bus_cfg.flags =
+               (cfg & OLDI_DEPOL) ? DRM_BUS_FLAG_DE_LOW :
+                                    DRM_BUS_FLAG_DE_HIGH;
+
+       return 0;
+}
+
+static int
+tidss_oldi_bridge_connector_readout(struct drm_bridge *bridge,
+                                   struct drm_atomic_sro_state *state,
+                                   struct drm_connector_state *conn_state)
+{
+       struct tidss_oldi *oldi = drm_bridge_to_tidss_oldi(bridge);
+       struct drm_connector *connector = conn_state->connector;
+       struct drm_crtc_state *crtc_state;
+       struct drm_encoder *encoder;
+       struct drm_crtc *crtc;
+
+       /* The transmitter is off, there is nothing to read out. */
+       if (!(tidss_oldi_read_config(oldi) & OLDI_ENABLE))
+               return 0;
+
+       encoder = bridge->encoder;
+       if (!encoder->funcs || !encoder->funcs->atomic_sro_get_current_crtc)
+               return -ENODEV;
+
+       crtc = encoder->funcs->atomic_sro_get_current_crtc(encoder);
+       if (!crtc)
+               return -ENODEV;
+
+       crtc_state = drm_atomic_sro_get_crtc_state(state, crtc);
+       if (!crtc_state)
+               return -ENODEV;
+
+       crtc_state->encoder_mask |= drm_encoder_mask(encoder);
+       crtc_state->connector_mask |= drm_connector_mask(connector);
+
+       /*
+        * drm_atomic_set_crtc_for_connector() takes a connector
+        * reference when setting the crtc pointer, which is later
+        * released by __drm_atomic_helper_connector_destroy_state().
+        * The readout bypasses that path, so take the reference
+        * explicitly.
+        */
+       drm_connector_get(connector);
+       conn_state->crtc = crtc;
+       conn_state->best_encoder = encoder;
+
+       return 0;
+}
+
 static const struct drm_bridge_funcs tidss_oldi_bridge_funcs = {
        .attach = tidss_oldi_bridge_attach,
@@ -362,4 +496,7 @@ static const struct drm_bridge_funcs 
tidss_oldi_bridge_funcs = {
        .atomic_create_state = drm_atomic_helper_bridge_create_state,
        .mode_valid = tidss_oldi_mode_valid,
+       .atomic_sro_readout_state = tidss_oldi_bridge_readout_state,
+       .atomic_sro_compare_state = drm_atomic_helper_bridge_compare_state,
+       .atomic_sro_connector_readout = tidss_oldi_bridge_connector_readout,
 };
 
@@ -622,4 +759,5 @@ int tidss_oldi_init(struct tidss_device *tidss)
                oldi->bridge.of_node = child;
                oldi->bridge.driver_private = oldi;
+               oldi->bridge.ops |= DRM_BRIDGE_OP_CONNECTOR_HW_READOUT;
 
                tidss->oldis[tidss->num_oldis++] = oldi;

-- 
2.55.0

Reply via email to