From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7DEE8606A692 for ; Tue, 1 Dec 2026 09:27:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Q0qval9q Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id D809B10F6E7; Tue, 1 Dec 2026 09:27:02 +0000 (UTC) Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) by gabe.freedesktop.org (Postfix) with ESMTPS id A040910E969 for ; Tue, 1 Dec 2026 09:26:56 +0000 (UTC) Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-4bb85e07aadso141664655e9.1 for ; Tue, 01 Dec 2026 01:26:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796117215; x=1796722015; darn=lists.freedesktop.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=1prD4Vhvsm7auaQrOoqrbxSrtm/+6sdmCIz6L3IyICc=; b=Q0qval9qFjbR5eXabh6pFL+hTCrBg6VM73XM0eJf1XSdp7zlAabaItFk3JZxR/azwo DrAmHNa8cLKGnY/ycNGzKJJbshZCrn0I88BvEg9CmXpGxXBjeunk7k0zxQE/Ea2AJ0U5 WD1Doh9a3ckQ6+mgBRO5/qcjJBWUIBZNW+EpEbpFevx8pnavVc4hIdsCVUKjYk7Lp9BE csses55FZAcZfs+F46SkFYAG9GUHGanIjHpz3hbBtfZUvQ6L6NjuNbLK3tohq0WbFKTW HuONYqOLIWsHYLfHD5Pgk78SyJI/04yn/ZlNRvXnGBOIyfKR3ALY9Yf/9x47nn/aWNVX nJAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796117215; x=1796722015; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=1prD4Vhvsm7auaQrOoqrbxSrtm/+6sdmCIz6L3IyICc=; b=ou10iIt2gwK+DG7wBg6t5OdflRFi4gyinattgu8VALUj8IgYeYPyALVRfYEiSXPMy9 NapBuJUPC2yIwtUKZg/nxiyLJNGnxn6uIeIGSk3uxOLQfQfrCygxJYSUWOamMwGce8tE CnllqqIA2+0DdAamzybN1mMWjY4FurrVQaI7DfqaCZs59CRSZOLcI4wsaDyhf2dKZkNN Zd0mN1wUUzDtvWfYkyKgzbDOIDEaFbFZb0j3IOSKzCZZPguIZD54gla2/Xba8L3e2Wzz +3AyG4UdwoPWFUIn9Sq++NfmfO55CtuuWfsRC7KkDys6WS5g3ZcJk/fEtJzkAurghByp 1gvA== X-Forwarded-Encrypted: i=1; AL+hErEFQz/ul/CZs9sVpf3kuyFu88D8ftdUEhdJSrT4bQj99oa2dEkp9BPz9GfTHnPmDVpxuKDC9G1eLL0=@lists.freedesktop.org X-Gm-Message-State: APuU4Z8xUoRIiCeAw6aSibP6V30SUoKiVbVS2FNnaz+f+bagyuhr/zQu UYLdJfMiKfq5KBHInwhWwHBh8/1KPEiG9PJLiZcvCnenxJdXdbp7WM0j X-Gm-Gg: AZFdTZf9+djg5xhrsfEPg81Ga9HiWap84vOgRQJ6aU5ocDtjNzDd1yd5/s6XxM0gqar ThtdIVhqNhIoAaLTH9keVjgN5HlRrdVcHShA+qKfwxeh1xHKhUfDoJ+fKByLRHDS7wDro5qFBLe eZMRnuIiuh0N4YH4/zl79MMpIMC0uJxdtHZw4KQogyI2dV+k3PocZqLjcptbMnY4QsIglA9RVBW G44JEYh19O2iCcwQITUkrlYrgn0P4DAAGcTgj8FXW+YD/jYeaBLz3dHrg8hQpsf2NMmNhKNCLkm c/ikgrdprRThLdHuhw6NsNuqAViyXRuzlm0SXw4P/6S+dgvqkeSEKfoGveCgh0swVZq0BMqNP1t iudb4N6ftgvxusHmbsTNT0Hnk0IHcp7oOwozJR4BQGcHIzRU3xxnjcOAsxDRaxz5IPEz5n67Kb0 SIXQ8M3DQ4A0YBHhonxy+/V8ALPOZQMJgY7U8zC8AeEwrWikD5I47QWPun9VuFifrlp2nQJFGFT yHnnzqYSfKB2WCoeo2ksTMqEl4it4Rt/m8v/MRzWHMTpVLG/4TJ3kFCIrrmM6aijXZzeL8EuKPx 4WDirEN7p76SFei0Iv6/P01tfMrabZOu+YfUoS3m206ZkAjQI+JwrREt X-Received: by 2002:a05:600c:3484:b0:4a2:133:45e1 with SMTP id 5b1f17b1804b1-4bb88812f16mr728168045e9.24.1796117214775; Tue, 01 Dec 2026 01:26:54 -0800 (PST) Received: from ingenieria31.oficinasStQ.local ([79.116.4.158]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab6bc8ce06sm66497083f8f.47.2026.12.01.01.26.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 01:26:54 -0800 (PST) From: =?UTF-8?q?M=C3=A0xim=20Pedraza=20Padilla?= To: Maxime Ripard Cc: Neil Armstrong , Jessica Zhang , Jyri Sarha , Tomi Valkeinen , Maarten Lankhorst , Thomas Zimmermann , David Airlie , Simona Vetter , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH v3 1/2] drm/tilcdc: Implement state readout support Date: Tue, 1 Dec 2026 10:26:50 +0100 Message-Id: <20261201092651.240295-2-maximpedraza@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20261201092651.240295-1-maximpedraza@gmail.com> References: <20261201092651.240295-1-maximpedraza@gmail.com> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "dri-devel" X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 Reconstruct the display mode from the LCDC raster registers and read out the primary plane, to build the initial KMS state from what the bootloader left scanning out instead of resetting the controller. A splash drawn before Linux takes over stays on screen, and the first commit replaces it, with no modeset if it keeps the mode. Some of the state needs interpreting: - The pixel clock is the functional clock rate divided by the LCDC divider. Since the clock tree rarely gives the exact rate a mode asks for, it is matched against the standard refresh rates with drm_atomic_helper_sro_find_refresh_from_clk(), and the mode gets the pixel clock of the matching one. If none matches, nothing is adopted. - The registers only encode the pixel size, not the channel order. The order that displays correctly depends on the panel wiring, described by blue-and-red-wiring, which already selects the formats the driver exposes: the adopted framebuffer gets the one with that pixel size. - tilcdc_crtc_mode_fixup() flips the HSYNC polarity of the adjusted mode, and the readout undoes it. A mode with DRM_MODE_FLAG_PHSYNC and one with no HSYNC flag program the same registers, so the latter is reported. The adopted framebuffer is backed by a GEM object created with drm_atomic_helper_sro_create_gem_dma_object() from the DMA address the bootloader programmed, so that it can be scanned out again after the CRTC is disabled and re-enabled. If the raster isn't running there's nothing to adopt, and the CRTC and the plane are read out as disabled. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Màxim Pedraza Padilla --- drivers/gpu/drm/tilcdc/tilcdc_crtc.c | 182 ++++++++++++++++++++++++ drivers/gpu/drm/tilcdc/tilcdc_drv.c | 12 ++ drivers/gpu/drm/tilcdc/tilcdc_encoder.c | 29 +++- drivers/gpu/drm/tilcdc/tilcdc_plane.c | 133 +++++++++++++++++ 4 files changed, 353 insertions(+), 3 deletions(-) diff --git a/drivers/gpu/drm/tilcdc/tilcdc_crtc.c b/drivers/gpu/drm/tilcdc/tilcdc_crtc.c index f619b7a6e70d..1647b8af7dbb 100644 --- a/drivers/gpu/drm/tilcdc/tilcdc_crtc.c +++ b/drivers/gpu/drm/tilcdc/tilcdc_crtc.c @@ -4,6 +4,7 @@ * Author: Rob Clark */ +#include #include #include #include @@ -11,6 +12,8 @@ #include #include +#include +#include #include #include #include @@ -711,10 +714,189 @@ static void tilcdc_crtc_hw_reset(struct drm_crtc *crtc) pm_runtime_put_sync(dev->dev); } +/* + * Reconstruct the adjusted mode from the LCDC raster registers. This is the + * inverse of the register programming done in tilcdc_crtc_set_mode(): the + * horizontal front/back porch and sync width are stored as (value - 1), the + * pixels-per-line and lines-per-panel as (value - 1) as well, while the + * vertical front/back porch are stored verbatim. On the V2 controller a few + * high order bits live in RASTER_TIMING_2. + * + * tilcdc_crtc_mode_fixup() always sets a HSYNC polarity and the skew, so they + * can be told from the registers. A positive VSYNC can't be told from a mode + * that doesn't specify it: the latter is assumed. + */ +static int tilcdc_crtc_readout_mode(struct drm_crtc *crtc, + struct drm_display_mode *mode) +{ + struct drm_device *dev = crtc->dev; + struct tilcdc_drm_private *priv = ddev_to_tilcdc_priv(dev); + u32 t0, t1, t2, ctrl; + u32 ppl, hsw, hfp, hbp; + u32 lpp, vsw, vfp, vbp; + unsigned long clk_rate; + unsigned int clkdiv; + + t0 = tilcdc_read(dev, LCDC_RASTER_TIMING_0_REG); + t1 = tilcdc_read(dev, LCDC_RASTER_TIMING_1_REG); + t2 = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG); + ctrl = tilcdc_read(dev, LCDC_CTRL_REG); + + /* Horizontal fields, low bits in TIMING_0, V2 high bits in TIMING_2. */ + ppl = (t0 >> 4) & 0x3f; + hsw = (t0 >> 10) & 0x3f; + hfp = (t0 >> 16) & 0xff; + hbp = (t0 >> 24) & 0xff; + if (priv->rev == 2) { + ppl |= ((t0 >> 3) & 0x1) << 6; + hsw |= ((t2 >> 27) & 0xf) << 6; + hfp |= (t2 & 0x3) << 8; + hbp |= ((t2 >> 4) & 0x3) << 8; + } + mode->hdisplay = (ppl + 1) << 4; + hsw += 1; + hfp += 1; + hbp += 1; + + /* Vertical fields, LPP high bit in TIMING_2 on V2. */ + lpp = t1 & 0x3ff; + if (priv->rev == 2 && (t2 & LCDC_LPP_B10)) + lpp |= 0x400; + vsw = ((t1 >> 10) & 0x3f) + 1; + vfp = (t1 >> 16) & 0xff; + vbp = (t1 >> 24) & 0xff; + mode->vdisplay = lpp + 1; + + mode->hsync_start = mode->hdisplay + hfp; + mode->hsync_end = mode->hsync_start + hsw; + mode->htotal = mode->hsync_end + hbp; + + mode->vsync_start = mode->vdisplay + vfp; + mode->vsync_end = mode->vsync_start + vsw; + mode->vtotal = mode->vsync_end + vbp; + + /* + * The pixel clock is the functional clock divided by the CTRL divisor. + * Match the functional clock against the standard refresh rates for a + * line clkdiv times longer, which is the same as matching the pixel + * clock, and report the pixel clock of the matching standard mode. + */ + clkdiv = (ctrl & LCDC_CLK_DIVISOR_MASK) >> 8; + if (!clkdiv) + return -EINVAL; + if (!drm_atomic_helper_sro_find_refresh_from_clk(dev, priv->clk, + mode->htotal * clkdiv, + mode->vtotal, &clk_rate)) + return -EINVAL; + mode->clock = DIV_ROUND_CLOSEST(clk_rate / clkdiv, 1000); + + mode->flags |= t2 & LCDC_INVERT_HSYNC ? DRM_MODE_FLAG_NHSYNC : + DRM_MODE_FLAG_PHSYNC; + if (t2 & LCDC_INVERT_VSYNC) + mode->flags |= DRM_MODE_FLAG_NVSYNC; + + mode->hskew = hsw; + mode->flags |= DRM_MODE_FLAG_HSKEW; + + mode->type |= DRM_MODE_TYPE_DRIVER; + drm_mode_set_name(mode); + drm_mode_set_crtcinfo(mode, 0); + + return 0; +} + +static int tilcdc_crtc_readout_state(struct drm_crtc *crtc, + struct drm_atomic_sro_state *state, + struct drm_crtc_state *crtc_state) +{ + struct drm_device *dev = crtc->dev; + struct drm_display_mode adjusted = {}, mode; + int ret; + + pm_runtime_get_sync(dev->dev); + + /* Nothing to adopt unless the bootloader left the raster running. */ + if (!(tilcdc_read(dev, LCDC_RASTER_CTRL_REG) & LCDC_RASTER_ENABLE)) + goto out; + + ret = tilcdc_crtc_readout_mode(crtc, &adjusted); + if (ret) + goto err; + + /* Undo tilcdc_crtc_mode_fixup(). */ + drm_mode_init(&mode, &adjusted); + mode.hskew = 0; + mode.flags &= ~(DRM_MODE_FLAG_HSKEW | DRM_MODE_FLAG_NHSYNC | + DRM_MODE_FLAG_PHSYNC); + if (adjusted.flags & DRM_MODE_FLAG_PHSYNC) + mode.flags |= DRM_MODE_FLAG_NHSYNC; + drm_mode_set_crtcinfo(&mode, 0); + + ret = drm_atomic_set_mode_for_crtc(crtc_state, &mode); + if (WARN_ON(ret)) + goto err; + + drm_mode_copy(&crtc_state->adjusted_mode, &adjusted); + + crtc_state->active = true; + crtc_state->enable = true; + + /* The active plane is filled in by tilcdc_plane_readout_state(). */ + +out: + pm_runtime_put_sync(dev->dev); + return 0; + +err: + pm_runtime_put_sync(dev->dev); + return ret; +} + +static void tilcdc_crtc_install_state(struct drm_crtc *crtc, + struct drm_crtc_state *crtc_state) +{ + struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc); + struct drm_device *dev = crtc->dev; + struct tilcdc_drm_private *priv = ddev_to_tilcdc_priv(dev); + + if (!crtc_state->active) + return; + + /* + * The display is already scanning out the bootloader's image. Do what + * tilcdc_crtc_enable() and tilcdc_crtc_set_mode() would have done, + * without touching the raster: keep the controller powered, enable its + * interrupts and fill in the software state derived from the mode. A + * commit that doesn't change the mode relies on it, since no modeset + * will update it: set_scanout() uses crtc->mode for the DMA ceiling. + */ + pm_runtime_get_sync(dev->dev); + + mutex_lock(&tilcdc_crtc->enable_lock); + + drm_mode_copy(&crtc->mode, &crtc_state->mode); + drm_mode_copy(&crtc->hwmode, &crtc_state->adjusted_mode); + crtc->enabled = true; + tilcdc_crtc->hvtotal_us = tilcdc_mode_hvtotal(&crtc->hwmode); + tilcdc_crtc->lcd_fck_rate = clk_get_rate(priv->clk); + tilcdc_crtc->last_vblank = ktime_get(); + + tilcdc_crtc_enable_irqs(dev); + + drm_calc_timestamping_constants(crtc, &crtc->hwmode); + drm_crtc_vblank_on(crtc); + + tilcdc_crtc->enabled = true; + mutex_unlock(&tilcdc_crtc->enable_lock); +} + static const struct drm_crtc_funcs tilcdc_crtc_funcs = { .set_config = drm_atomic_helper_set_config, .page_flip = drm_atomic_helper_page_flip, .atomic_create_state = drm_atomic_helper_crtc_create_state, + .atomic_sro_readout_state = tilcdc_crtc_readout_state, + .atomic_sro_install_state = tilcdc_crtc_install_state, + .atomic_sro_compare_state = drm_atomic_helper_crtc_compare_state, .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, .enable_vblank = tilcdc_crtc_enable_vblank, diff --git a/drivers/gpu/drm/tilcdc/tilcdc_drv.c b/drivers/gpu/drm/tilcdc/tilcdc_drv.c index da718d9cb1d6..09623c33906c 100644 --- a/drivers/gpu/drm/tilcdc/tilcdc_drv.c +++ b/drivers/gpu/drm/tilcdc/tilcdc_drv.c @@ -12,6 +12,8 @@ #include #include +#include +#include #include #include #include @@ -75,6 +77,13 @@ static const struct drm_mode_config_funcs mode_config_funcs = { .fb_create = drm_gem_fb_create, .atomic_check = tilcdc_atomic_check, .atomic_commit = drm_atomic_helper_commit, + .atomic_sro_readout_state = drm_atomic_helper_sro_readout_state, + .hw_reset = drm_mode_config_helper_hw_reset, +}; + +static const struct drm_mode_config_helper_funcs mode_config_helper_funcs = { + .atomic_sro_build_state = drm_atomic_helper_sro_build_state, + .atomic_sro_install_state = drm_atomic_helper_sro_install_state, }; static void modeset_init(struct drm_device *dev) @@ -86,6 +95,7 @@ static void modeset_init(struct drm_device *dev) dev->mode_config.max_width = priv->max_width; dev->mode_config.max_height = 2048; dev->mode_config.funcs = &mode_config_funcs; + dev->mode_config.helper_private = &mode_config_helper_funcs; } #ifdef CONFIG_CPU_FREQ @@ -438,6 +448,8 @@ static int tilcdc_pdev_probe(struct platform_device *pdev) stop_poll: drm_kms_helper_poll_fini(ddev); + /* A CRTC adopted from the bootloader holds a runtime PM reference */ + tilcdc_crtc_shutdown(priv->crtc); tilcdc_irq_uninstall(ddev); unregister_cpufreq_notif: #ifdef CONFIG_CPU_FREQ diff --git a/drivers/gpu/drm/tilcdc/tilcdc_encoder.c b/drivers/gpu/drm/tilcdc/tilcdc_encoder.c index ac4b5beb2dd5..b3fe18f69f17 100644 --- a/drivers/gpu/drm/tilcdc/tilcdc_encoder.c +++ b/drivers/gpu/drm/tilcdc/tilcdc_encoder.c @@ -9,12 +9,34 @@ #include #include #include +#include +#include #include -#include #include "tilcdc_drv.h" #include "tilcdc_encoder.h" +/* + * State readout needs to know which CRTC drives this encoder so the connector + * can be tied back to it. tilcdc has a single CRTC, so return the one the + * encoder was made possible for. + */ +static struct drm_crtc * +tilcdc_encoder_get_current_crtc(struct drm_encoder *encoder) +{ + struct drm_crtc *crtc; + + drm_for_each_crtc(crtc, encoder->dev) + if (encoder->possible_crtcs == drm_crtc_mask(crtc)) + return crtc; + + return NULL; +} + +static const struct drm_encoder_funcs tilcdc_encoder_funcs = { + .atomic_sro_get_current_crtc = tilcdc_encoder_get_current_crtc, +}; + static int tilcdc_attach_bridge(struct drm_device *ddev, struct drm_bridge *bridge) { @@ -51,8 +73,9 @@ int tilcdc_encoder_create(struct drm_device *ddev) else if (IS_ERR(bridge)) return PTR_ERR(bridge); - encoder = drmm_simple_encoder_alloc(ddev, struct tilcdc_encoder, - base, DRM_MODE_ENCODER_NONE); + encoder = drmm_encoder_alloc(ddev, struct tilcdc_encoder, base, + &tilcdc_encoder_funcs, + DRM_MODE_ENCODER_NONE, NULL); if (IS_ERR(encoder)) { drm_err(ddev, "drm_encoder_init() failed %pe\n", encoder); return PTR_ERR(encoder); diff --git a/drivers/gpu/drm/tilcdc/tilcdc_plane.c b/drivers/gpu/drm/tilcdc/tilcdc_plane.c index eb0a5562421f..59ae4e074c74 100644 --- a/drivers/gpu/drm/tilcdc/tilcdc_plane.c +++ b/drivers/gpu/drm/tilcdc/tilcdc_plane.c @@ -4,17 +4,150 @@ * Author: Jyri Sarha */ +#include + #include #include +#include +#include +#include #include #include +#include +#include #include "tilcdc_drv.h" +#include "tilcdc_regs.h" + +static const struct drm_framebuffer_funcs tilcdc_plane_readout_fb_funcs = { + .destroy = drm_gem_fb_destroy, +}; + +/* + * The RASTER_CTRL format bits only encode the pixel size, not the channel + * order. The order that displays correctly depends on how the panel is wired + * (blue-and-red-wiring), which already selects the formats the driver + * exposes, so report the one with the size read back. + */ +static const struct drm_format_info * +tilcdc_plane_readout_format(struct drm_device *dev) +{ + struct tilcdc_drm_private *priv = ddev_to_tilcdc_priv(dev); + u32 ctrl = tilcdc_read(dev, LCDC_RASTER_CTRL_REG); + const struct drm_format_info *info; + unsigned int cpp, i; + + if (!(ctrl & LCDC_V2_TFT_24BPP_MODE)) + cpp = 2; + else if (ctrl & LCDC_V2_TFT_24BPP_UNPACK) + cpp = 4; + else + cpp = 3; + + for (i = 0; i < priv->num_pixelformats; i++) { + info = drm_format_info(priv->pixelformats[i]); + if (info->cpp[0] == cpp) + return info; + } + + return NULL; +} + +static int tilcdc_plane_atomic_readout_state(struct drm_plane *plane, + struct drm_atomic_sro_state *state, + struct drm_plane_state *plane_state) +{ + struct drm_device *dev = plane->dev; + const struct drm_format_info *info; + struct drm_crtc_state *crtc_state; + struct drm_gem_dma_object *dma_obj; + struct drm_framebuffer *fb; + dma_addr_t dma_addr; + struct drm_crtc *crtc; + unsigned int w, h; + int ret; + + pm_runtime_get_sync(dev->dev); + + if (!(tilcdc_read(dev, LCDC_RASTER_CTRL_REG) & LCDC_RASTER_ENABLE)) + goto out; + + if (dev->num_crtcs != 1) { + ret = -EINVAL; + goto err; + } + crtc = list_first_entry(&dev->mode_config.crtc_list, struct drm_crtc, + head); + + crtc_state = drm_atomic_sro_get_crtc_state(state, crtc); + if (!crtc_state) { + ret = -ENODEV; + goto err; + } + + /* The tilcdc primary plane always covers the whole active area. */ + w = crtc_state->adjusted_mode.hdisplay; + h = crtc_state->adjusted_mode.vdisplay; + + info = tilcdc_plane_readout_format(dev); + if (!info) { + ret = -EINVAL; + goto err; + } + + fb = kzalloc_obj(*fb); + if (!fb) { + ret = -ENOMEM; + goto err; + } + fb->dev = dev; + fb->format = info; + fb->width = w; + fb->height = h; + fb->pitches[0] = w * (drm_format_info_bpp(info, 0) / 8); + fb->offsets[0] = 0; + + dma_addr = tilcdc_read(dev, LCDC_DMA_FB_BASE_ADDR_0_REG); + dma_obj = drm_atomic_helper_sro_create_gem_dma_object(dev, dma_addr, + (size_t)fb->pitches[0] * h); + if (IS_ERR(dma_obj)) { + kfree(fb); + ret = PTR_ERR(dma_obj); + goto err; + } + fb->obj[0] = &dma_obj->base; + + ret = drm_framebuffer_init(dev, fb, &tilcdc_plane_readout_fb_funcs); + if (ret) { + drm_gem_object_put(fb->obj[0]); + kfree(fb); + goto err; + } + + plane_state->fb = fb; + plane_state->crtc = crtc; + plane_state->src_w = w << 16; + plane_state->src_h = h << 16; + plane_state->crtc_w = w; + plane_state->crtc_h = h; + + crtc_state->plane_mask |= drm_plane_mask(plane); + +out: + pm_runtime_put_sync(dev->dev); + return 0; + +err: + pm_runtime_put_sync(dev->dev); + return ret; +} static const struct drm_plane_funcs tilcdc_plane_funcs = { .update_plane = drm_atomic_helper_update_plane, .disable_plane = drm_atomic_helper_disable_plane, .atomic_create_state = drm_atomic_helper_plane_create_state, + .atomic_sro_readout_state = tilcdc_plane_atomic_readout_state, + .atomic_sro_compare_state = drm_atomic_helper_plane_compare_state, .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, }; -- 2.39.5