On the AM65x and the AM625, an OLDI transmitter sits between a video
port and the panel or the bridge it drives. Without a readout of the
transmitter, the bridge chain of a pipeline the firmware left running
has no state for it, and the first commit can't be matched against what
is programmed.

Implement the state readout hooks of the OLDI bridge. The
atomic_sro_readout_state hook reads the transmitter configuration
register through tidss_get_oldi_config(), and does nothing if the
transmitter is disabled. Otherwise, it looks up the bus format the LVDS
mapping and the width flag correspond to, since the most significant
bit of the mapping field carries the dual-link flag rather than the
mapping, and reconstructs the bus configuration the atomic helpers
negotiate: the transmitter takes the parallel RGB output of the video
port and serializes it. The DE polarity is the only flag the
transmitter stores, the sampling edges being hardcoded. The readout
fails when the mapping isn't recognized, so that the core falls back to
the regular initialization.

The atomic_sro_connector_readout hook binds the connector to the CRTC
the encoder is currently connected to, and takes the connector reference
that drm_atomic_set_crtc_for_connector() would have taken. Set
DRM_BRIDGE_OP_CONNECTOR_HW_READOUT so that drm_bridge_connector uses
it.

Use drm_atomic_helper_bridge_compare_state() to compare the readout
state with the committed one.

This hasn't been tested yet.

Signed-off-by: Maxime Ripard <[email protected]>
---
 drivers/gpu/drm/tidss/tidss_oldi.c | 157 +++++++++++++++++++++++++++++++++++++
 1 file changed, 157 insertions(+)

diff --git a/drivers/gpu/drm/tidss/tidss_oldi.c 
b/drivers/gpu/drm/tidss/tidss_oldi.c
index f767b0c115ac..0ad4ef34ba64 100644
--- a/drivers/gpu/drm/tidss/tidss_oldi.c
+++ b/drivers/gpu/drm/tidss/tidss_oldi.c
@@ -3,20 +3,25 @@
  * Copyright (C) 2025 - Texas Instruments Incorporated
  *
  * Aradhya Bhatia <[email protected]>
  */
 
+#include <linux/bitfield.h>
 #include <linux/clk.h>
 #include <linux/of.h>
 #include <linux/of_graph.h>
 #include <linux/mfd/syscon.h>
 #include <linux/media-bus-format.h>
 #include <linux/regmap.h>
 
 #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"
 #include "tidss_dispc_regs.h"
 #include "tidss_oldi.h"
 
@@ -348,20 +353,171 @@ tidss_oldi_mode_valid(struct drm_bridge *bridge,
                return MODE_CLOCK_RANGE;
 
        return MODE_OK;
 }
 
+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, flags;
+
+       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;
+
+       /*
+        * The transmitter only stores the DE polarity: OLDI_DEPOL is
+        * set when the DE is active low, and the sampling edges are
+        * hardcoded, the transmitter cannot configure them. TODO:
+        * read them back from the hardware registers if a variant
+        * makes them configurable.
+        *
+        * Everything else in the bus flags can't be read back, but the
+        * atomic check leaves it untouched: the output flags are those
+        * the next element of the chain expects, which are the display
+        * info flags when the elements after the transmitter forward
+        * them unmodified, and tidss_oldi_atomic_check() only replaces
+        * the sampling edges of the input flags. Start from the display
+        * info flags and only reconcile the DE polarity with what the
+        * hardware reports, so that a DE polarity left unspecified is
+        * not reported as an explicit one.
+        */
+       flags = conn_state->connector->display_info.bus_flags;
+
+       if (cfg & OLDI_DEPOL) {
+               flags &= ~DRM_BUS_FLAG_DE_HIGH;
+               flags |= DRM_BUS_FLAG_DE_LOW;
+       } else if (flags & DRM_BUS_FLAG_DE_LOW) {
+               flags &= ~DRM_BUS_FLAG_DE_LOW;
+               flags |= DRM_BUS_FLAG_DE_HIGH;
+       }
+
+       bridge_state->output_bus_cfg.flags = flags;
+
+       flags &= ~(DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE |
+                  DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE);
+       flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE |
+                DRM_BUS_FLAG_SYNC_SAMPLE_POSEDGE;
+       bridge_state->input_bus_cfg.flags = flags;
+
+       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,
        .atomic_check = tidss_oldi_atomic_check,
        .atomic_pre_enable = tidss_oldi_atomic_pre_enable,
        .atomic_post_disable = tidss_oldi_atomic_post_disable,
        .atomic_get_input_bus_fmts = tidss_oldi_atomic_get_input_bus_fmts,
        .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
        .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
        .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,
 };
 
 static int get_oldi_mode(struct device_node *oldi_tx, int *companion_instance)
 {
        struct device_node *companion;
@@ -610,10 +766,11 @@ static int tidss_oldi_init_one(struct tidss_device *tidss,
        }
 
        /* Register the bridge. */
        oldi->bridge.of_node = oldi_tx;
        oldi->bridge.driver_private = oldi;
+       oldi->bridge.ops |= DRM_BRIDGE_OP_CONNECTOR_HW_READOUT;
 
        tidss->oldis[tidss->num_oldis++] = oldi;
        tidss->is_ext_vp_clk[oldi->parent_vp] = true;
        oldi->tidss = tidss;
 

-- 
2.56.1

Reply via email to