Skip to content

Commit 358e76f

Browse files
committed
drm/sun4i: hdmi: Consolidate atomic_check and mode_valid
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. Acked-by: Sui Jingfeng <sui.jingfeng@linux.dev> Reviewed-by: Jernej Skrabec <jernej.skrabec@gmail.com> Signed-off-by: Maxime Ripard <mripard@kernel.org> Link: https://patchwork.freedesktop.org/patch/msgid/20240222-kms-hdmi-connector-state-v7-35-8f4af575fce2@kernel.org
1 parent c6686f2 commit 358e76f

File tree

1 file changed

+47
-27
lines changed

1 file changed

+47
-27
lines changed

drivers/gpu/drm/sun4i/sun4i_hdmi_enc.c

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,6 @@ static int sun4i_hdmi_setup_avi_infoframes(struct sun4i_hdmi *hdmi,
6262
return 0;
6363
}
6464

65-
static int sun4i_hdmi_atomic_check(struct drm_encoder *encoder,
66-
struct drm_crtc_state *crtc_state,
67-
struct drm_connector_state *conn_state)
68-
{
69-
struct drm_display_mode *mode = &crtc_state->mode;
70-
71-
if (mode->flags & DRM_MODE_FLAG_DBLCLK)
72-
return -EINVAL;
73-
74-
return 0;
75-
}
76-
7765
static void sun4i_hdmi_disable(struct drm_encoder *encoder,
7866
struct drm_atomic_state *state)
7967
{
@@ -166,31 +154,61 @@ static void sun4i_hdmi_enable(struct drm_encoder *encoder,
166154
writel(val, hdmi->base + SUN4I_HDMI_VID_CTRL_REG);
167155
}
168156

169-
static enum drm_mode_status sun4i_hdmi_mode_valid(struct drm_encoder *encoder,
170-
const struct drm_display_mode *mode)
157+
static const struct drm_encoder_helper_funcs sun4i_hdmi_helper_funcs = {
158+
.atomic_disable = sun4i_hdmi_disable,
159+
.atomic_enable = sun4i_hdmi_enable,
160+
};
161+
162+
static enum drm_mode_status
163+
sun4i_hdmi_connector_clock_valid(const struct drm_connector *connector,
164+
const struct drm_display_mode *mode,
165+
unsigned long long clock)
171166
{
172-
struct sun4i_hdmi *hdmi = drm_encoder_to_sun4i_hdmi(encoder);
173-
unsigned long rate = mode->clock * 1000;
174-
unsigned long diff = rate / 200; /* +-0.5% allowed by HDMI spec */
167+
const struct sun4i_hdmi *hdmi = drm_connector_to_sun4i_hdmi(connector);
168+
unsigned long diff = clock / 200; /* +-0.5% allowed by HDMI spec */
175169
long rounded_rate;
176170

171+
if (mode->flags & DRM_MODE_FLAG_DBLCLK)
172+
return MODE_BAD;
173+
177174
/* 165 MHz is the typical max pixelclock frequency for HDMI <= 1.2 */
178-
if (rate > 165000000)
175+
if (clock > 165000000)
179176
return MODE_CLOCK_HIGH;
180-
rounded_rate = clk_round_rate(hdmi->tmds_clk, rate);
177+
178+
rounded_rate = clk_round_rate(hdmi->tmds_clk, clock);
181179
if (rounded_rate > 0 &&
182-
max_t(unsigned long, rounded_rate, rate) -
183-
min_t(unsigned long, rounded_rate, rate) < diff)
180+
max_t(unsigned long, rounded_rate, clock) -
181+
min_t(unsigned long, rounded_rate, clock) < diff)
184182
return MODE_OK;
183+
185184
return MODE_NOCLOCK;
186185
}
187186

188-
static const struct drm_encoder_helper_funcs sun4i_hdmi_helper_funcs = {
189-
.atomic_check = sun4i_hdmi_atomic_check,
190-
.atomic_disable = sun4i_hdmi_disable,
191-
.atomic_enable = sun4i_hdmi_enable,
192-
.mode_valid = sun4i_hdmi_mode_valid,
193-
};
187+
static int sun4i_hdmi_connector_atomic_check(struct drm_connector *connector,
188+
struct drm_atomic_state *state)
189+
{
190+
struct drm_connector_state *conn_state =
191+
drm_atomic_get_new_connector_state(state, connector);
192+
struct drm_crtc *crtc = conn_state->crtc;
193+
struct drm_crtc_state *crtc_state = crtc->state;
194+
struct drm_display_mode *mode = &crtc_state->adjusted_mode;
195+
enum drm_mode_status status;
196+
197+
status = sun4i_hdmi_connector_clock_valid(connector, mode,
198+
mode->clock * 1000);
199+
if (status != MODE_OK)
200+
return -EINVAL;
201+
202+
return 0;
203+
}
204+
205+
static enum drm_mode_status
206+
sun4i_hdmi_connector_mode_valid(struct drm_connector *connector,
207+
struct drm_display_mode *mode)
208+
{
209+
return sun4i_hdmi_connector_clock_valid(connector, mode,
210+
mode->clock * 1000);
211+
}
194212

195213
static int sun4i_hdmi_get_modes(struct drm_connector *connector)
196214
{
@@ -236,6 +254,8 @@ static struct i2c_adapter *sun4i_hdmi_get_ddc(struct device *dev)
236254
}
237255

238256
static const struct drm_connector_helper_funcs sun4i_hdmi_connector_helper_funcs = {
257+
.atomic_check = sun4i_hdmi_connector_atomic_check,
258+
.mode_valid = sun4i_hdmi_connector_mode_valid,
239259
.get_modes = sun4i_hdmi_get_modes,
240260
};
241261

0 commit comments

Comments
 (0)