2 * Copyright (C) Jernej Skrabec <jernej.skrabec@siol.net>
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_fb_cma_helper.h>
14 #include <drm/drm_gem_cma_helper.h>
15 #include <drm/drm_gem_framebuffer_helper.h>
16 #include <drm/drm_plane_helper.h>
17 #include <drm/drm_probe_helper.h>
20 #include "sun8i_vi_layer.h"
21 #include "sun8i_mixer.h"
22 #include "sun8i_vi_scaler.h"
24 static void sun8i_vi_layer_enable(struct sun8i_mixer
*mixer
, int channel
,
25 int overlay
, bool enable
, unsigned int zpos
,
26 unsigned int old_zpos
)
28 u32 val
, bld_base
, ch_base
;
30 bld_base
= sun8i_blender_base(mixer
);
31 ch_base
= sun8i_channel_base(mixer
, channel
);
33 DRM_DEBUG_DRIVER("%sabling VI channel %d overlay %d\n",
34 enable
? "En" : "Dis", channel
, overlay
);
37 val
= SUN8I_MIXER_CHAN_VI_LAYER_ATTR_EN
;
41 regmap_update_bits(mixer
->engine
.regs
,
42 SUN8I_MIXER_CHAN_VI_LAYER_ATTR(ch_base
, overlay
),
43 SUN8I_MIXER_CHAN_VI_LAYER_ATTR_EN
, val
);
45 if (!enable
|| zpos
!= old_zpos
) {
46 regmap_update_bits(mixer
->engine
.regs
,
47 SUN8I_MIXER_BLEND_PIPE_CTL(bld_base
),
48 SUN8I_MIXER_BLEND_PIPE_CTL_EN(old_zpos
),
51 regmap_update_bits(mixer
->engine
.regs
,
52 SUN8I_MIXER_BLEND_ROUTE(bld_base
),
53 SUN8I_MIXER_BLEND_ROUTE_PIPE_MSK(old_zpos
),
58 val
= SUN8I_MIXER_BLEND_PIPE_CTL_EN(zpos
);
60 regmap_update_bits(mixer
->engine
.regs
,
61 SUN8I_MIXER_BLEND_PIPE_CTL(bld_base
),
64 val
= channel
<< SUN8I_MIXER_BLEND_ROUTE_PIPE_SHIFT(zpos
);
66 regmap_update_bits(mixer
->engine
.regs
,
67 SUN8I_MIXER_BLEND_ROUTE(bld_base
),
68 SUN8I_MIXER_BLEND_ROUTE_PIPE_MSK(zpos
),
73 static int sun8i_vi_layer_update_coord(struct sun8i_mixer
*mixer
, int channel
,
74 int overlay
, struct drm_plane
*plane
,
77 struct drm_plane_state
*state
= plane
->state
;
78 const struct drm_format_info
*format
= state
->fb
->format
;
79 u32 src_w
, src_h
, dst_w
, dst_h
;
80 u32 bld_base
, ch_base
;
85 DRM_DEBUG_DRIVER("Updating VI channel %d overlay %d\n",
88 bld_base
= sun8i_blender_base(mixer
);
89 ch_base
= sun8i_channel_base(mixer
, channel
);
91 src_w
= drm_rect_width(&state
->src
) >> 16;
92 src_h
= drm_rect_height(&state
->src
) >> 16;
93 dst_w
= drm_rect_width(&state
->dst
);
94 dst_h
= drm_rect_height(&state
->dst
);
96 hphase
= state
->src
.x1
& 0xffff;
97 vphase
= state
->src
.y1
& 0xffff;
99 /* make coordinates dividable by subsampling factor */
100 if (format
->hsub
> 1) {
103 mask
= format
->hsub
- 1;
104 remainder
= (state
->src
.x1
>> 16) & mask
;
105 src_w
= (src_w
+ remainder
) & ~mask
;
106 hphase
+= remainder
<< 16;
109 if (format
->vsub
> 1) {
112 mask
= format
->vsub
- 1;
113 remainder
= (state
->src
.y1
>> 16) & mask
;
114 src_h
= (src_h
+ remainder
) & ~mask
;
115 vphase
+= remainder
<< 16;
118 insize
= SUN8I_MIXER_SIZE(src_w
, src_h
);
119 outsize
= SUN8I_MIXER_SIZE(dst_w
, dst_h
);
121 /* Set height and width */
122 DRM_DEBUG_DRIVER("Layer source offset X: %d Y: %d\n",
123 (state
->src
.x1
>> 16) & ~(format
->hsub
- 1),
124 (state
->src
.y1
>> 16) & ~(format
->vsub
- 1));
125 DRM_DEBUG_DRIVER("Layer source size W: %d H: %d\n", src_w
, src_h
);
126 regmap_write(mixer
->engine
.regs
,
127 SUN8I_MIXER_CHAN_VI_LAYER_SIZE(ch_base
, overlay
),
129 regmap_write(mixer
->engine
.regs
,
130 SUN8I_MIXER_CHAN_VI_OVL_SIZE(ch_base
),
134 * Scaler must be enabled for subsampled formats, so it scales
135 * chroma to same size as luma.
137 subsampled
= format
->hsub
> 1 || format
->vsub
> 1;
139 if (insize
!= outsize
|| subsampled
|| hphase
|| vphase
) {
142 DRM_DEBUG_DRIVER("HW scaling is enabled\n");
144 hscale
= state
->src_w
/ state
->crtc_w
;
145 vscale
= state
->src_h
/ state
->crtc_h
;
147 sun8i_vi_scaler_setup(mixer
, channel
, src_w
, src_h
, dst_w
,
148 dst_h
, hscale
, vscale
, hphase
, vphase
,
150 sun8i_vi_scaler_enable(mixer
, channel
, true);
152 DRM_DEBUG_DRIVER("HW scaling is not needed\n");
153 sun8i_vi_scaler_enable(mixer
, channel
, false);
156 /* Set base coordinates */
157 DRM_DEBUG_DRIVER("Layer destination coordinates X: %d Y: %d\n",
158 state
->dst
.x1
, state
->dst
.y1
);
159 DRM_DEBUG_DRIVER("Layer destination size W: %d H: %d\n", dst_w
, dst_h
);
160 regmap_write(mixer
->engine
.regs
,
161 SUN8I_MIXER_BLEND_ATTR_COORD(bld_base
, zpos
),
162 SUN8I_MIXER_COORD(state
->dst
.x1
, state
->dst
.y1
));
163 regmap_write(mixer
->engine
.regs
,
164 SUN8I_MIXER_BLEND_ATTR_INSIZE(bld_base
, zpos
),
170 static int sun8i_vi_layer_update_formats(struct sun8i_mixer
*mixer
, int channel
,
171 int overlay
, struct drm_plane
*plane
)
173 struct drm_plane_state
*state
= plane
->state
;
174 const struct de2_fmt_info
*fmt_info
;
177 ch_base
= sun8i_channel_base(mixer
, channel
);
179 fmt_info
= sun8i_mixer_format_info(state
->fb
->format
->format
);
181 DRM_DEBUG_DRIVER("Invalid format\n");
185 val
= fmt_info
->de2_fmt
<< SUN8I_MIXER_CHAN_VI_LAYER_ATTR_FBFMT_OFFSET
;
186 regmap_update_bits(mixer
->engine
.regs
,
187 SUN8I_MIXER_CHAN_VI_LAYER_ATTR(ch_base
, overlay
),
188 SUN8I_MIXER_CHAN_VI_LAYER_ATTR_FBFMT_MASK
, val
);
190 if (fmt_info
->csc
!= SUN8I_CSC_MODE_OFF
) {
191 sun8i_csc_set_ccsc_coefficients(mixer
, channel
, fmt_info
->csc
);
192 sun8i_csc_enable_ccsc(mixer
, channel
, true);
194 sun8i_csc_enable_ccsc(mixer
, channel
, false);
198 val
= SUN8I_MIXER_CHAN_VI_LAYER_ATTR_RGB_MODE
;
202 regmap_update_bits(mixer
->engine
.regs
,
203 SUN8I_MIXER_CHAN_VI_LAYER_ATTR(ch_base
, overlay
),
204 SUN8I_MIXER_CHAN_VI_LAYER_ATTR_RGB_MODE
, val
);
206 /* It seems that YUV formats use global alpha setting. */
207 if (mixer
->cfg
->is_de3
)
208 regmap_update_bits(mixer
->engine
.regs
,
209 SUN8I_MIXER_CHAN_VI_LAYER_ATTR(ch_base
,
211 SUN50I_MIXER_CHAN_VI_LAYER_ATTR_ALPHA_MASK
,
212 SUN50I_MIXER_CHAN_VI_LAYER_ATTR_ALPHA(0xff));
217 static int sun8i_vi_layer_update_buffer(struct sun8i_mixer
*mixer
, int channel
,
218 int overlay
, struct drm_plane
*plane
)
220 struct drm_plane_state
*state
= plane
->state
;
221 struct drm_framebuffer
*fb
= state
->fb
;
222 const struct drm_format_info
*format
= fb
->format
;
223 struct drm_gem_cma_object
*gem
;
224 u32 dx
, dy
, src_x
, src_y
;
229 ch_base
= sun8i_channel_base(mixer
, channel
);
231 /* Adjust x and y to be dividable by subsampling factor */
232 src_x
= (state
->src
.x1
>> 16) & ~(format
->hsub
- 1);
233 src_y
= (state
->src
.y1
>> 16) & ~(format
->vsub
- 1);
235 for (i
= 0; i
< format
->num_planes
; i
++) {
236 /* Get the physical address of the buffer in memory */
237 gem
= drm_fb_cma_get_gem_obj(fb
, i
);
239 DRM_DEBUG_DRIVER("Using GEM @ %pad\n", &gem
->paddr
);
241 /* Compute the start of the displayed memory */
242 paddr
= gem
->paddr
+ fb
->offsets
[i
];
252 /* Fixup framebuffer address for src coordinates */
253 paddr
+= dx
* format
->cpp
[i
];
254 paddr
+= dy
* fb
->pitches
[i
];
256 /* Set the line width */
257 DRM_DEBUG_DRIVER("Layer %d. line width: %d bytes\n",
258 i
+ 1, fb
->pitches
[i
]);
259 regmap_write(mixer
->engine
.regs
,
260 SUN8I_MIXER_CHAN_VI_LAYER_PITCH(ch_base
,
264 DRM_DEBUG_DRIVER("Setting %d. buffer address to %pad\n",
267 regmap_write(mixer
->engine
.regs
,
268 SUN8I_MIXER_CHAN_VI_LAYER_TOP_LADDR(ch_base
,
270 lower_32_bits(paddr
));
276 static int sun8i_vi_layer_atomic_check(struct drm_plane
*plane
,
277 struct drm_plane_state
*state
)
279 struct sun8i_vi_layer
*layer
= plane_to_sun8i_vi_layer(plane
);
280 struct drm_crtc
*crtc
= state
->crtc
;
281 struct drm_crtc_state
*crtc_state
;
282 int min_scale
, max_scale
;
287 crtc_state
= drm_atomic_get_existing_crtc_state(state
->state
, crtc
);
288 if (WARN_ON(!crtc_state
))
291 min_scale
= DRM_PLANE_HELPER_NO_SCALING
;
292 max_scale
= DRM_PLANE_HELPER_NO_SCALING
;
294 if (layer
->mixer
->cfg
->scaler_mask
& BIT(layer
->channel
)) {
295 min_scale
= SUN8I_VI_SCALER_SCALE_MIN
;
296 max_scale
= SUN8I_VI_SCALER_SCALE_MAX
;
299 return drm_atomic_helper_check_plane_state(state
, crtc_state
,
300 min_scale
, max_scale
,
304 static void sun8i_vi_layer_atomic_disable(struct drm_plane
*plane
,
305 struct drm_plane_state
*old_state
)
307 struct sun8i_vi_layer
*layer
= plane_to_sun8i_vi_layer(plane
);
308 unsigned int old_zpos
= old_state
->normalized_zpos
;
309 struct sun8i_mixer
*mixer
= layer
->mixer
;
311 sun8i_vi_layer_enable(mixer
, layer
->channel
, layer
->overlay
, false, 0,
315 static void sun8i_vi_layer_atomic_update(struct drm_plane
*plane
,
316 struct drm_plane_state
*old_state
)
318 struct sun8i_vi_layer
*layer
= plane_to_sun8i_vi_layer(plane
);
319 unsigned int zpos
= plane
->state
->normalized_zpos
;
320 unsigned int old_zpos
= old_state
->normalized_zpos
;
321 struct sun8i_mixer
*mixer
= layer
->mixer
;
323 if (!plane
->state
->visible
) {
324 sun8i_vi_layer_enable(mixer
, layer
->channel
,
325 layer
->overlay
, false, 0, old_zpos
);
329 sun8i_vi_layer_update_coord(mixer
, layer
->channel
,
330 layer
->overlay
, plane
, zpos
);
331 sun8i_vi_layer_update_formats(mixer
, layer
->channel
,
332 layer
->overlay
, plane
);
333 sun8i_vi_layer_update_buffer(mixer
, layer
->channel
,
334 layer
->overlay
, plane
);
335 sun8i_vi_layer_enable(mixer
, layer
->channel
, layer
->overlay
,
336 true, zpos
, old_zpos
);
339 static struct drm_plane_helper_funcs sun8i_vi_layer_helper_funcs
= {
340 .prepare_fb
= drm_gem_fb_prepare_fb
,
341 .atomic_check
= sun8i_vi_layer_atomic_check
,
342 .atomic_disable
= sun8i_vi_layer_atomic_disable
,
343 .atomic_update
= sun8i_vi_layer_atomic_update
,
346 static const struct drm_plane_funcs sun8i_vi_layer_funcs
= {
347 .atomic_destroy_state
= drm_atomic_helper_plane_destroy_state
,
348 .atomic_duplicate_state
= drm_atomic_helper_plane_duplicate_state
,
349 .destroy
= drm_plane_cleanup
,
350 .disable_plane
= drm_atomic_helper_disable_plane
,
351 .reset
= drm_atomic_helper_plane_reset
,
352 .update_plane
= drm_atomic_helper_update_plane
,
356 * While all RGB formats are supported, VI planes don't support
357 * alpha blending, so there is no point having formats with alpha
358 * channel if their opaque analog exist.
360 static const u32 sun8i_vi_layer_formats
[] = {
396 struct sun8i_vi_layer
*sun8i_vi_layer_init_one(struct drm_device
*drm
,
397 struct sun8i_mixer
*mixer
,
400 struct sun8i_vi_layer
*layer
;
401 unsigned int plane_cnt
;
404 layer
= devm_kzalloc(drm
->dev
, sizeof(*layer
), GFP_KERNEL
);
406 return ERR_PTR(-ENOMEM
);
408 /* possible crtcs are set later */
409 ret
= drm_universal_plane_init(drm
, &layer
->plane
, 0,
410 &sun8i_vi_layer_funcs
,
411 sun8i_vi_layer_formats
,
412 ARRAY_SIZE(sun8i_vi_layer_formats
),
413 NULL
, DRM_PLANE_TYPE_OVERLAY
, NULL
);
415 dev_err(drm
->dev
, "Couldn't initialize layer\n");
419 plane_cnt
= mixer
->cfg
->ui_num
+ mixer
->cfg
->vi_num
;
421 ret
= drm_plane_create_zpos_property(&layer
->plane
, index
,
424 dev_err(drm
->dev
, "Couldn't add zpos property\n");
428 drm_plane_helper_add(&layer
->plane
, &sun8i_vi_layer_helper_funcs
);
429 layer
->mixer
= mixer
;
430 layer
->channel
= index
;