From nobody Wed Dec 31 18:20:31 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 49D7DC4332F for ; Tue, 31 Oct 2023 16:55:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346933AbjJaQzB (ORCPT ); Tue, 31 Oct 2023 12:55:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346946AbjJaQwo (ORCPT ); Tue, 31 Oct 2023 12:52:44 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 045221702; Tue, 31 Oct 2023 09:50:16 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 09D59C433C9; Tue, 31 Oct 2023 16:50:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1698771016; bh=fBonQ0XHrW4s6go71bEhf2QGqRqulcwzMi4AdpPOd0s=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=kGTDETlhJZcjNdA2m+kAk0vPi6Ug4k+NFvJrhxJUdmURYUDtci/7YOVAIojAW5Nv5 wGCYNepfbshFtMlmWTNVqFoIUR6Q2S/RvM5lYISWtIN/4bVSdmI6O3MGdKVRLnjikk ESqMAMlSsqjzcUtJgOwwfn6TjsA0XZWq8/ixAziQBqHU40IYR26yIMbGOuv4JgODtl LQj2OKe+zrvvrmEpYMer2IvJWIYyWa0FULeK4v6TDYJK4ldaif/VPhwEdpWyPmYUoV NEAHJOIa6K4+koeRzNRqIxTrtSsZcmpZlScMPE313Xz4/E/YrBIPe3m7CvtytT9D9C Nq0lHKp4nP6mA== From: Maxime Ripard Date: Tue, 31 Oct 2023 17:48:49 +0100 Subject: [PATCH RFC v3 36/37] drm/sun4i: hdmi: Consolidate atomic_check and mode_valid MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20231031-kms-hdmi-connector-state-v3-36-328b0fae43a7@kernel.org> References: <20231031-kms-hdmi-connector-state-v3-0-328b0fae43a7@kernel.org> In-Reply-To: <20231031-kms-hdmi-connector-state-v3-0-328b0fae43a7@kernel.org> To: Maarten Lankhorst , Thomas Zimmermann , David Airlie , Daniel Vetter , Emma Anholt , Jonathan Corbet , Sandy Huang , =?utf-8?q?Heiko_St=C3=BCbner?= , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland Cc: Hans Verkuil , dri-devel@lists.freedesktop.org, linux-arm-kernel@lists.infradead.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, linux-rockchip@lists.infradead.org, linux-sunxi@lists.linux.dev, Maxime Ripard X-Mailer: b4 0.12.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=4258; i=mripard@kernel.org; h=from:subject:message-id; bh=fBonQ0XHrW4s6go71bEhf2QGqRqulcwzMi4AdpPOd0s=; b=owGbwMvMwCX2+D1vfrpE4FHG02pJDKmO+vfPRQbI+DlG90Vrimxo6GZ7ZCOtf3zi+ujP1ev3Z Yd7JN7sKGVhEONikBVTZIkRNl8Sd2rW6042vnkwc1iZQIYwcHEKwEQs3jD80+jYart3xt87C9p1 Yx7lrbW8WbBk7orz+oXMQkk3i76kNzD8D3CrX67ywji8IWILC3ttTHhv6G6Pp/+2fDH7Zf34i8V LRgA= X-Developer-Key: i=mripard@kernel.org; a=openpgp; fpr=BE5675C37E818C8B5764241C254BCFC56BF6CE8D Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org atomic_check and mode_valid do not check for the same things which can lead to surprising result if the userspace commits a mode that didn't go through mode_valid. Let's merge the two implementations into a function called by both. Signed-off-by: Maxime Ripard --- drivers/gpu/drm/sun4i/sun4i_hdmi_enc.c | 74 +++++++++++++++++++++---------= ---- 1 file changed, 47 insertions(+), 27 deletions(-) diff --git a/drivers/gpu/drm/sun4i/sun4i_hdmi_enc.c b/drivers/gpu/drm/sun4i= /sun4i_hdmi_enc.c index c276d984da6b..b7cf369b1906 100644 --- a/drivers/gpu/drm/sun4i/sun4i_hdmi_enc.c +++ b/drivers/gpu/drm/sun4i/sun4i_hdmi_enc.c @@ -62,18 +62,6 @@ static int sun4i_hdmi_setup_avi_infoframes(struct sun4i_= hdmi *hdmi, return 0; } =20 -static int sun4i_hdmi_atomic_check(struct drm_encoder *encoder, - struct drm_crtc_state *crtc_state, - struct drm_connector_state *conn_state) -{ - struct drm_display_mode *mode =3D &crtc_state->mode; - - if (mode->flags & DRM_MODE_FLAG_DBLCLK) - return -EINVAL; - - return 0; -} - static void sun4i_hdmi_disable(struct drm_encoder *encoder, struct drm_atomic_state *state) { @@ -166,31 +154,61 @@ static void sun4i_hdmi_enable(struct drm_encoder *enc= oder, writel(val, hdmi->base + SUN4I_HDMI_VID_CTRL_REG); } =20 -static enum drm_mode_status sun4i_hdmi_mode_valid(struct drm_encoder *enco= der, - const struct drm_display_mode *mode) +static const struct drm_encoder_helper_funcs sun4i_hdmi_helper_funcs =3D { + .atomic_disable =3D sun4i_hdmi_disable, + .atomic_enable =3D sun4i_hdmi_enable, +}; + +static enum drm_mode_status +sun4i_hdmi_connector_clock_valid(const struct drm_connector *connector, + const struct drm_display_mode *mode, + unsigned long long clock) { - struct sun4i_hdmi *hdmi =3D drm_encoder_to_sun4i_hdmi(encoder); - unsigned long rate =3D mode->clock * 1000; - unsigned long diff =3D rate / 200; /* +-0.5% allowed by HDMI spec */ + const struct sun4i_hdmi *hdmi =3D drm_connector_to_sun4i_hdmi(connector); + unsigned long diff =3D clock / 200; /* +-0.5% allowed by HDMI spec */ long rounded_rate; =20 + if (mode->flags & DRM_MODE_FLAG_DBLCLK) + return MODE_BAD; + /* 165 MHz is the typical max pixelclock frequency for HDMI <=3D 1.2 */ - if (rate > 165000000) + if (clock > 165000000) return MODE_CLOCK_HIGH; - rounded_rate =3D clk_round_rate(hdmi->tmds_clk, rate); + + rounded_rate =3D clk_round_rate(hdmi->tmds_clk, clock); if (rounded_rate > 0 && - max_t(unsigned long, rounded_rate, rate) - - min_t(unsigned long, rounded_rate, rate) < diff) + max_t(unsigned long, rounded_rate, clock) - + min_t(unsigned long, rounded_rate, clock) < diff) return MODE_OK; + return MODE_NOCLOCK; } =20 -static const struct drm_encoder_helper_funcs sun4i_hdmi_helper_funcs =3D { - .atomic_check =3D sun4i_hdmi_atomic_check, - .atomic_disable =3D sun4i_hdmi_disable, - .atomic_enable =3D sun4i_hdmi_enable, - .mode_valid =3D sun4i_hdmi_mode_valid, -}; +static int sun4i_hdmi_connector_atomic_check(struct drm_connector *connect= or, + struct drm_atomic_state *state) +{ + struct drm_connector_state *conn_state =3D + drm_atomic_get_new_connector_state(state, connector); + struct drm_crtc *crtc =3D conn_state->crtc; + struct drm_crtc_state *crtc_state =3D crtc->state; + struct drm_display_mode *mode =3D &crtc_state->adjusted_mode; + enum drm_mode_status status; + + status =3D sun4i_hdmi_connector_clock_valid(connector, mode, + mode->clock * 1000); + if (status !=3D MODE_OK) + return -EINVAL; + + return 0; +} + +static enum drm_mode_status +sun4i_hdmi_connector_mode_valid(struct drm_connector *connector, + struct drm_display_mode *mode) +{ + return sun4i_hdmi_connector_clock_valid(connector, mode, + mode->clock * 1000); +} =20 static int sun4i_hdmi_get_modes(struct drm_connector *connector) { @@ -236,6 +254,8 @@ static struct i2c_adapter *sun4i_hdmi_get_ddc(struct de= vice *dev) } =20 static const struct drm_connector_helper_funcs sun4i_hdmi_connector_helper= _funcs =3D { + .atomic_check =3D sun4i_hdmi_connector_atomic_check, + .mode_valid =3D sun4i_hdmi_connector_mode_valid, .get_modes =3D sun4i_hdmi_get_modes, }; =20 --=20 2.41.0