4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
10 #include <drm/drm_atomic.h>
11 #include <drm/drm_atomic_helper.h>
12 #include <drm/drm_crtc.h>
13 #include <drm/drm_crtc_helper.h>
14 #include <drm/drm_fb_cma_helper.h>
15 #include <drm/drm_gem_cma_helper.h>
16 #include <drm/drm_plane_helper.h>
19 #include "sun8i_vi_layer.h"
20 #include "sun8i_mixer.h"
21 #include "sun8i_vi_scaler.h"
23 static void sun8i_vi_layer_enable(struct sun8i_mixer *mixer, int channel,
24 int overlay, bool enable)
28 DRM_DEBUG_DRIVER("%sabling VI channel %d overlay %d\n",
29 enable ? "En" : "Dis", channel, overlay);
32 val = SUN8I_MIXER_CHAN_VI_LAYER_ATTR_EN;
36 regmap_update_bits(mixer->engine.regs,
37 SUN8I_MIXER_CHAN_VI_LAYER_ATTR(channel, overlay),
38 SUN8I_MIXER_CHAN_VI_LAYER_ATTR_EN, val);
41 val = SUN8I_MIXER_BLEND_PIPE_CTL_EN(channel);
45 regmap_update_bits(mixer->engine.regs,
46 SUN8I_MIXER_BLEND_PIPE_CTL,
47 SUN8I_MIXER_BLEND_PIPE_CTL_EN(channel), val);
50 static int sun8i_vi_layer_update_coord(struct sun8i_mixer *mixer, int channel,
51 int overlay, struct drm_plane *plane)
53 struct drm_plane_state *state = plane->state;
54 const struct drm_format_info *format = state->fb->format;
55 u32 src_w, src_h, dst_w, dst_h;
60 DRM_DEBUG_DRIVER("Updating VI channel %d overlay %d\n",
63 src_w = drm_rect_width(&state->src) >> 16;
64 src_h = drm_rect_height(&state->src) >> 16;
65 dst_w = drm_rect_width(&state->dst);
66 dst_h = drm_rect_height(&state->dst);
68 hphase = state->src.x1 & 0xffff;
69 vphase = state->src.y1 & 0xffff;
71 /* make coordinates dividable by subsampling factor */
72 if (format->hsub > 1) {
75 mask = format->hsub - 1;
76 remainder = (state->src.x1 >> 16) & mask;
77 src_w = (src_w + remainder) & ~mask;
78 hphase += remainder << 16;
81 if (format->vsub > 1) {
84 mask = format->vsub - 1;
85 remainder = (state->src.y1 >> 16) & mask;
86 src_h = (src_h + remainder) & ~mask;
87 vphase += remainder << 16;
90 insize = SUN8I_MIXER_SIZE(src_w, src_h);
91 outsize = SUN8I_MIXER_SIZE(dst_w, dst_h);
93 /* Set height and width */
94 DRM_DEBUG_DRIVER("Layer source offset X: %d Y: %d\n",
95 (state->src.x1 >> 16) & ~(format->hsub - 1),
96 (state->src.y1 >> 16) & ~(format->vsub - 1));
97 DRM_DEBUG_DRIVER("Layer source size W: %d H: %d\n", src_w, src_h);
98 regmap_write(mixer->engine.regs,
99 SUN8I_MIXER_CHAN_VI_LAYER_SIZE(channel, overlay),
101 regmap_write(mixer->engine.regs,
102 SUN8I_MIXER_CHAN_VI_OVL_SIZE(channel),
106 * Scaler must be enabled for subsampled formats, so it scales
107 * chroma to same size as luma.
109 subsampled = format->hsub > 1 || format->vsub > 1;
111 if (insize != outsize || subsampled || hphase || vphase) {
114 DRM_DEBUG_DRIVER("HW scaling is enabled\n");
116 hscale = state->src_w / state->crtc_w;
117 vscale = state->src_h / state->crtc_h;
119 sun8i_vi_scaler_setup(mixer, channel, src_w, src_h, dst_w,
120 dst_h, hscale, vscale, hphase, vphase,
122 sun8i_vi_scaler_enable(mixer, channel, true);
124 DRM_DEBUG_DRIVER("HW scaling is not needed\n");
125 sun8i_vi_scaler_enable(mixer, channel, false);
128 /* Set base coordinates */
129 DRM_DEBUG_DRIVER("Layer destination coordinates X: %d Y: %d\n",
130 state->dst.x1, state->dst.y1);
131 DRM_DEBUG_DRIVER("Layer destination size W: %d H: %d\n", dst_w, dst_h);
132 regmap_write(mixer->engine.regs,
133 SUN8I_MIXER_BLEND_ATTR_COORD(channel),
134 SUN8I_MIXER_COORD(state->dst.x1, state->dst.y1));
135 regmap_write(mixer->engine.regs,
136 SUN8I_MIXER_BLEND_ATTR_INSIZE(channel),
142 static int sun8i_vi_layer_update_formats(struct sun8i_mixer *mixer, int channel,
143 int overlay, struct drm_plane *plane)
145 struct drm_plane_state *state = plane->state;
146 const struct de2_fmt_info *fmt_info;
149 fmt_info = sun8i_mixer_format_info(state->fb->format->format);
151 DRM_DEBUG_DRIVER("Invalid format\n");
155 val = fmt_info->de2_fmt << SUN8I_MIXER_CHAN_VI_LAYER_ATTR_FBFMT_OFFSET;
156 regmap_update_bits(mixer->engine.regs,
157 SUN8I_MIXER_CHAN_VI_LAYER_ATTR(channel, overlay),
158 SUN8I_MIXER_CHAN_VI_LAYER_ATTR_FBFMT_MASK, val);
160 if (fmt_info->csc != SUN8I_CSC_MODE_OFF) {
161 sun8i_csc_set_ccsc_coefficients(mixer, channel, fmt_info->csc);
162 sun8i_csc_enable_ccsc(mixer, channel, true);
164 sun8i_csc_enable_ccsc(mixer, channel, false);
168 val = SUN8I_MIXER_CHAN_VI_LAYER_ATTR_RGB_MODE;
172 regmap_update_bits(mixer->engine.regs,
173 SUN8I_MIXER_CHAN_VI_LAYER_ATTR(channel, overlay),
174 SUN8I_MIXER_CHAN_VI_LAYER_ATTR_RGB_MODE, val);
179 static int sun8i_vi_layer_update_buffer(struct sun8i_mixer *mixer, int channel,
180 int overlay, struct drm_plane *plane)
182 struct drm_plane_state *state = plane->state;
183 struct drm_framebuffer *fb = state->fb;
184 const struct drm_format_info *format = fb->format;
185 struct drm_gem_cma_object *gem;
186 u32 dx, dy, src_x, src_y;
190 /* Adjust x and y to be dividable by subsampling factor */
191 src_x = (state->src.x1 >> 16) & ~(format->hsub - 1);
192 src_y = (state->src.y1 >> 16) & ~(format->vsub - 1);
194 for (i = 0; i < format->num_planes; i++) {
195 /* Get the physical address of the buffer in memory */
196 gem = drm_fb_cma_get_gem_obj(fb, i);
198 DRM_DEBUG_DRIVER("Using GEM @ %pad\n", &gem->paddr);
200 /* Compute the start of the displayed memory */
201 paddr = gem->paddr + fb->offsets[i];
211 /* Fixup framebuffer address for src coordinates */
212 paddr += dx * format->cpp[i];
213 paddr += dy * fb->pitches[i];
215 /* Set the line width */
216 DRM_DEBUG_DRIVER("Layer %d. line width: %d bytes\n",
217 i + 1, fb->pitches[i]);
218 regmap_write(mixer->engine.regs,
219 SUN8I_MIXER_CHAN_VI_LAYER_PITCH(channel,
223 DRM_DEBUG_DRIVER("Setting %d. buffer address to %pad\n",
226 regmap_write(mixer->engine.regs,
227 SUN8I_MIXER_CHAN_VI_LAYER_TOP_LADDR(channel,
229 lower_32_bits(paddr));
235 static int sun8i_vi_layer_atomic_check(struct drm_plane *plane,
236 struct drm_plane_state *state)
238 struct sun8i_vi_layer *layer = plane_to_sun8i_vi_layer(plane);
239 struct drm_crtc *crtc = state->crtc;
240 struct drm_crtc_state *crtc_state;
241 int min_scale, max_scale;
246 crtc_state = drm_atomic_get_existing_crtc_state(state->state, crtc);
247 if (WARN_ON(!crtc_state))
250 min_scale = DRM_PLANE_HELPER_NO_SCALING;
251 max_scale = DRM_PLANE_HELPER_NO_SCALING;
253 if (layer->mixer->cfg->scaler_mask & BIT(layer->channel)) {
254 min_scale = SUN8I_VI_SCALER_SCALE_MIN;
255 max_scale = SUN8I_VI_SCALER_SCALE_MAX;
258 return drm_atomic_helper_check_plane_state(state, crtc_state,
259 min_scale, max_scale,
263 static void sun8i_vi_layer_atomic_disable(struct drm_plane *plane,
264 struct drm_plane_state *old_state)
266 struct sun8i_vi_layer *layer = plane_to_sun8i_vi_layer(plane);
267 struct sun8i_mixer *mixer = layer->mixer;
269 sun8i_vi_layer_enable(mixer, layer->channel, layer->overlay, false);
272 static void sun8i_vi_layer_atomic_update(struct drm_plane *plane,
273 struct drm_plane_state *old_state)
275 struct sun8i_vi_layer *layer = plane_to_sun8i_vi_layer(plane);
276 struct sun8i_mixer *mixer = layer->mixer;
278 if (!plane->state->visible) {
279 sun8i_vi_layer_enable(mixer, layer->channel,
280 layer->overlay, false);
284 sun8i_vi_layer_update_coord(mixer, layer->channel,
285 layer->overlay, plane);
286 sun8i_vi_layer_update_formats(mixer, layer->channel,
287 layer->overlay, plane);
288 sun8i_vi_layer_update_buffer(mixer, layer->channel,
289 layer->overlay, plane);
290 sun8i_vi_layer_enable(mixer, layer->channel, layer->overlay, true);
293 static struct drm_plane_helper_funcs sun8i_vi_layer_helper_funcs = {
294 .atomic_check = sun8i_vi_layer_atomic_check,
295 .atomic_disable = sun8i_vi_layer_atomic_disable,
296 .atomic_update = sun8i_vi_layer_atomic_update,
299 static const struct drm_plane_funcs sun8i_vi_layer_funcs = {
300 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
301 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
302 .destroy = drm_plane_cleanup,
303 .disable_plane = drm_atomic_helper_disable_plane,
304 .reset = drm_atomic_helper_plane_reset,
305 .update_plane = drm_atomic_helper_update_plane,
309 * While all RGB formats are supported, VI planes don't support
310 * alpha blending, so there is no point having formats with alpha
311 * channel if their opaque analog exist.
313 static const u32 sun8i_vi_layer_formats[] = {
349 struct sun8i_vi_layer *sun8i_vi_layer_init_one(struct drm_device *drm,
350 struct sun8i_mixer *mixer,
353 struct sun8i_vi_layer *layer;
356 layer = devm_kzalloc(drm->dev, sizeof(*layer), GFP_KERNEL);
358 return ERR_PTR(-ENOMEM);
360 /* possible crtcs are set later */
361 ret = drm_universal_plane_init(drm, &layer->plane, 0,
362 &sun8i_vi_layer_funcs,
363 sun8i_vi_layer_formats,
364 ARRAY_SIZE(sun8i_vi_layer_formats),
365 NULL, DRM_PLANE_TYPE_OVERLAY, NULL);
367 dev_err(drm->dev, "Couldn't initialize layer\n");
371 /* fixed zpos for now */
372 ret = drm_plane_create_zpos_immutable_property(&layer->plane, index);
374 dev_err(drm->dev, "Couldn't add zpos property\n");
378 drm_plane_helper_add(&layer->plane, &sun8i_vi_layer_helper_funcs);
379 layer->mixer = mixer;
380 layer->channel = index;