2 * Copyright (C) 2011 Samsung Electronics Co.Ltd
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
14 #include <drm/drm_atomic.h>
15 #include <drm/drm_atomic_helper.h>
16 #include <drm/drm_plane_helper.h>
17 #include <drm/exynos_drm.h>
18 #include "exynos_drm_drv.h"
19 #include "exynos_drm_crtc.h"
20 #include "exynos_drm_fb.h"
21 #include "exynos_drm_gem.h"
22 #include "exynos_drm_plane.h"
25 * This function is to get X or Y size shown via screen. This needs length and
26 * start position of CRTC.
29 * CRTC ----------------
32 * There are six cases from a to f.
34 * <----- SCREEN ----->
36 * ----------|------------------|----------
40 * c --------------------------
45 static int exynos_plane_get_size(int start, unsigned length, unsigned last)
47 int end = start + length;
52 size = min_t(unsigned, end, last);
53 } else if (start <= last) {
54 size = min_t(unsigned, last - start, length);
60 static void exynos_plane_mode_set(struct exynos_drm_plane_state *exynos_state)
62 struct drm_plane_state *state = &exynos_state->base;
63 struct drm_crtc *crtc = state->crtc;
64 struct drm_crtc_state *crtc_state =
65 drm_atomic_get_existing_crtc_state(state->state, crtc);
66 struct drm_display_mode *mode = &crtc_state->adjusted_mode;
68 unsigned int crtc_w, crtc_h;
69 unsigned int src_x, src_y;
70 unsigned int src_w, src_h;
71 unsigned int actual_w;
72 unsigned int actual_h;
75 * The original src/dest coordinates are stored in exynos_state->base,
76 * but we want to keep another copy internal to our driver that we can
77 * clip/modify ourselves.
80 crtc_x = state->crtc_x;
81 crtc_y = state->crtc_y;
82 crtc_w = state->crtc_w;
83 crtc_h = state->crtc_h;
85 src_x = state->src_x >> 16;
86 src_y = state->src_y >> 16;
87 src_w = state->src_w >> 16;
88 src_h = state->src_h >> 16;
91 exynos_state->h_ratio = (src_w << 16) / crtc_w;
92 exynos_state->v_ratio = (src_h << 16) / crtc_h;
94 /* clip to visible area */
95 actual_w = exynos_plane_get_size(crtc_x, crtc_w, mode->hdisplay);
96 actual_h = exynos_plane_get_size(crtc_y, crtc_h, mode->vdisplay);
100 src_x += ((-crtc_x) * exynos_state->h_ratio) >> 16;
106 src_y += ((-crtc_y) * exynos_state->v_ratio) >> 16;
110 /* set drm framebuffer data. */
111 exynos_state->src.x = src_x;
112 exynos_state->src.y = src_y;
113 exynos_state->src.w = (actual_w * exynos_state->h_ratio) >> 16;
114 exynos_state->src.h = (actual_h * exynos_state->v_ratio) >> 16;
116 /* set plane range to be displayed. */
117 exynos_state->crtc.x = crtc_x;
118 exynos_state->crtc.y = crtc_y;
119 exynos_state->crtc.w = actual_w;
120 exynos_state->crtc.h = actual_h;
122 DRM_DEBUG_KMS("plane : offset_x/y(%d,%d), width/height(%d,%d)",
123 exynos_state->crtc.x, exynos_state->crtc.y,
124 exynos_state->crtc.w, exynos_state->crtc.h);
127 static void exynos_drm_plane_reset(struct drm_plane *plane)
129 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
130 struct exynos_drm_plane_state *exynos_state;
133 exynos_state = to_exynos_plane_state(plane->state);
134 __drm_atomic_helper_plane_destroy_state(plane->state);
139 exynos_state = kzalloc(sizeof(*exynos_state), GFP_KERNEL);
141 __drm_atomic_helper_plane_reset(plane, &exynos_state->base);
142 plane->state->zpos = exynos_plane->config->zpos;
146 static struct drm_plane_state *
147 exynos_drm_plane_duplicate_state(struct drm_plane *plane)
149 struct exynos_drm_plane_state *exynos_state;
150 struct exynos_drm_plane_state *copy;
152 exynos_state = to_exynos_plane_state(plane->state);
153 copy = kzalloc(sizeof(*exynos_state), GFP_KERNEL);
157 __drm_atomic_helper_plane_duplicate_state(plane, ©->base);
161 static void exynos_drm_plane_destroy_state(struct drm_plane *plane,
162 struct drm_plane_state *old_state)
164 struct exynos_drm_plane_state *old_exynos_state =
165 to_exynos_plane_state(old_state);
166 __drm_atomic_helper_plane_destroy_state(old_state);
167 kfree(old_exynos_state);
170 static struct drm_plane_funcs exynos_plane_funcs = {
171 .update_plane = drm_atomic_helper_update_plane,
172 .disable_plane = drm_atomic_helper_disable_plane,
173 .destroy = drm_plane_cleanup,
174 .reset = exynos_drm_plane_reset,
175 .atomic_duplicate_state = exynos_drm_plane_duplicate_state,
176 .atomic_destroy_state = exynos_drm_plane_destroy_state,
180 exynos_drm_plane_check_format(const struct exynos_drm_plane_config *config,
181 struct exynos_drm_plane_state *state)
183 struct drm_framebuffer *fb = state->base.fb;
185 switch (fb->modifier) {
186 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
187 if (!(config->capabilities & EXYNOS_DRM_PLANE_CAP_TILE))
191 case DRM_FORMAT_MOD_LINEAR:
195 DRM_ERROR("unsupported pixel format modifier");
203 exynos_drm_plane_check_size(const struct exynos_drm_plane_config *config,
204 struct exynos_drm_plane_state *state)
206 bool width_ok = false, height_ok = false;
208 if (config->capabilities & EXYNOS_DRM_PLANE_CAP_SCALE)
211 if (state->src.w == state->crtc.w)
214 if (state->src.h == state->crtc.h)
217 if ((config->capabilities & EXYNOS_DRM_PLANE_CAP_DOUBLE) &&
218 state->h_ratio == (1 << 15))
221 if ((config->capabilities & EXYNOS_DRM_PLANE_CAP_DOUBLE) &&
222 state->v_ratio == (1 << 15))
225 if (width_ok && height_ok)
228 DRM_DEBUG_KMS("scaling mode is not supported");
232 static int exynos_plane_atomic_check(struct drm_plane *plane,
233 struct drm_plane_state *state)
235 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
236 struct exynos_drm_plane_state *exynos_state =
237 to_exynos_plane_state(state);
240 if (!state->crtc || !state->fb)
243 /* translate state into exynos_state */
244 exynos_plane_mode_set(exynos_state);
246 ret = exynos_drm_plane_check_format(exynos_plane->config, exynos_state);
250 ret = exynos_drm_plane_check_size(exynos_plane->config, exynos_state);
254 static void exynos_plane_atomic_update(struct drm_plane *plane,
255 struct drm_plane_state *old_state)
257 struct drm_plane_state *state = plane->state;
258 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(state->crtc);
259 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
264 if (exynos_crtc->ops->update_plane)
265 exynos_crtc->ops->update_plane(exynos_crtc, exynos_plane);
268 static void exynos_plane_atomic_disable(struct drm_plane *plane,
269 struct drm_plane_state *old_state)
271 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
272 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(old_state->crtc);
274 if (!old_state->crtc)
277 if (exynos_crtc->ops->disable_plane)
278 exynos_crtc->ops->disable_plane(exynos_crtc, exynos_plane);
281 static const struct drm_plane_helper_funcs plane_helper_funcs = {
282 .atomic_check = exynos_plane_atomic_check,
283 .atomic_update = exynos_plane_atomic_update,
284 .atomic_disable = exynos_plane_atomic_disable,
287 static void exynos_plane_attach_zpos_property(struct drm_plane *plane,
288 int zpos, bool immutable)
291 drm_plane_create_zpos_immutable_property(plane, zpos);
293 drm_plane_create_zpos_property(plane, zpos, 0, MAX_PLANE - 1);
296 int exynos_plane_init(struct drm_device *dev,
297 struct exynos_drm_plane *exynos_plane, unsigned int index,
298 const struct exynos_drm_plane_config *config)
301 unsigned int supported_modes = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
302 BIT(DRM_MODE_BLEND_PREMULTI) |
303 BIT(DRM_MODE_BLEND_COVERAGE);
304 struct drm_plane *plane = &exynos_plane->base;
306 err = drm_universal_plane_init(dev, &exynos_plane->base,
307 1 << dev->mode_config.num_crtc,
309 config->pixel_formats,
310 config->num_pixel_formats,
311 NULL, config->type, NULL);
313 DRM_ERROR("failed to initialize plane\n");
317 drm_plane_helper_add(&exynos_plane->base, &plane_helper_funcs);
319 exynos_plane->index = index;
320 exynos_plane->config = config;
322 exynos_plane_attach_zpos_property(&exynos_plane->base, config->zpos,
323 !(config->capabilities & EXYNOS_DRM_PLANE_CAP_ZPOS));
325 if (config->capabilities & EXYNOS_DRM_PLANE_CAP_PIX_BLEND)
326 drm_plane_create_blend_mode_property(plane, supported_modes);
328 if (config->capabilities & EXYNOS_DRM_PLANE_CAP_WIN_BLEND)
329 drm_plane_create_alpha_property(plane);