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_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
;
242 struct drm_rect clip
;
247 crtc_state
= drm_atomic_get_existing_crtc_state(state
->state
, crtc
);
248 if (WARN_ON(!crtc_state
))
253 clip
.x2
= crtc_state
->adjusted_mode
.hdisplay
;
254 clip
.y2
= crtc_state
->adjusted_mode
.vdisplay
;
256 min_scale
= DRM_PLANE_HELPER_NO_SCALING
;
257 max_scale
= DRM_PLANE_HELPER_NO_SCALING
;
259 if (layer
->mixer
->cfg
->scaler_mask
& BIT(layer
->channel
)) {
260 min_scale
= SUN8I_VI_SCALER_SCALE_MIN
;
261 max_scale
= SUN8I_VI_SCALER_SCALE_MAX
;
264 return drm_atomic_helper_check_plane_state(state
, crtc_state
, &clip
,
265 min_scale
, max_scale
,
269 static void sun8i_vi_layer_atomic_disable(struct drm_plane
*plane
,
270 struct drm_plane_state
*old_state
)
272 struct sun8i_vi_layer
*layer
= plane_to_sun8i_vi_layer(plane
);
273 struct sun8i_mixer
*mixer
= layer
->mixer
;
275 sun8i_vi_layer_enable(mixer
, layer
->channel
, layer
->overlay
, false);
278 static void sun8i_vi_layer_atomic_update(struct drm_plane
*plane
,
279 struct drm_plane_state
*old_state
)
281 struct sun8i_vi_layer
*layer
= plane_to_sun8i_vi_layer(plane
);
282 struct sun8i_mixer
*mixer
= layer
->mixer
;
284 if (!plane
->state
->visible
) {
285 sun8i_vi_layer_enable(mixer
, layer
->channel
,
286 layer
->overlay
, false);
290 sun8i_vi_layer_update_coord(mixer
, layer
->channel
,
291 layer
->overlay
, plane
);
292 sun8i_vi_layer_update_formats(mixer
, layer
->channel
,
293 layer
->overlay
, plane
);
294 sun8i_vi_layer_update_buffer(mixer
, layer
->channel
,
295 layer
->overlay
, plane
);
296 sun8i_vi_layer_enable(mixer
, layer
->channel
, layer
->overlay
, true);
299 static struct drm_plane_helper_funcs sun8i_vi_layer_helper_funcs
= {
300 .atomic_check
= sun8i_vi_layer_atomic_check
,
301 .atomic_disable
= sun8i_vi_layer_atomic_disable
,
302 .atomic_update
= sun8i_vi_layer_atomic_update
,
305 static const struct drm_plane_funcs sun8i_vi_layer_funcs
= {
306 .atomic_destroy_state
= drm_atomic_helper_plane_destroy_state
,
307 .atomic_duplicate_state
= drm_atomic_helper_plane_duplicate_state
,
308 .destroy
= drm_plane_cleanup
,
309 .disable_plane
= drm_atomic_helper_disable_plane
,
310 .reset
= drm_atomic_helper_plane_reset
,
311 .update_plane
= drm_atomic_helper_update_plane
,
315 * While all RGB formats are supported, VI planes don't support
316 * alpha blending, so there is no point having formats with alpha
317 * channel if their opaque analog exist.
319 static const u32 sun8i_vi_layer_formats
[] = {
355 struct sun8i_vi_layer
*sun8i_vi_layer_init_one(struct drm_device
*drm
,
356 struct sun8i_mixer
*mixer
,
359 struct sun8i_vi_layer
*layer
;
362 layer
= devm_kzalloc(drm
->dev
, sizeof(*layer
), GFP_KERNEL
);
364 return ERR_PTR(-ENOMEM
);
366 /* possible crtcs are set later */
367 ret
= drm_universal_plane_init(drm
, &layer
->plane
, 0,
368 &sun8i_vi_layer_funcs
,
369 sun8i_vi_layer_formats
,
370 ARRAY_SIZE(sun8i_vi_layer_formats
),
371 NULL
, DRM_PLANE_TYPE_OVERLAY
, NULL
);
373 dev_err(drm
->dev
, "Couldn't initialize layer\n");
377 /* fixed zpos for now */
378 ret
= drm_plane_create_zpos_immutable_property(&layer
->plane
, index
);
380 dev_err(drm
->dev
, "Couldn't add zpos property\n");
384 drm_plane_helper_add(&layer
->plane
, &sun8i_vi_layer_helper_funcs
);
385 layer
->mixer
= mixer
;
386 layer
->channel
= index
;