drm/exynos: fix clipping when scaling is enabled
[deliverable/linux.git] / drivers / gpu / drm / exynos / exynos_drm_plane.c
CommitLineData
864ee9e6
JS
1/*
2 * Copyright (C) 2011 Samsung Electronics Co.Ltd
3 * Authors: Joonyoung Shim <jy0922.shim@samsung.com>
4 *
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.
9 *
10 */
11
760285e7 12#include <drm/drmP.h>
864ee9e6 13
760285e7 14#include <drm/exynos_drm.h>
adf5691c 15#include <drm/drm_plane_helper.h>
4ea9526b 16#include <drm/drm_atomic_helper.h>
864ee9e6 17#include "exynos_drm_drv.h"
080be03d 18#include "exynos_drm_crtc.h"
4070d212
JS
19#include "exynos_drm_fb.h"
20#include "exynos_drm_gem.h"
e30655d0 21#include "exynos_drm_plane.h"
864ee9e6 22
2ab97921
JS
23/*
24 * This function is to get X or Y size shown via screen. This needs length and
25 * start position of CRTC.
26 *
27 * <--- length --->
28 * CRTC ----------------
29 * ^ start ^ end
30 *
60a705a9 31 * There are six cases from a to f.
2ab97921
JS
32 *
33 * <----- SCREEN ----->
34 * 0 last
35 * ----------|------------------|----------
36 * CRTCs
37 * a -------
38 * b -------
39 * c --------------------------
40 * d --------
41 * e -------
42 * f -------
43 */
44static int exynos_plane_get_size(int start, unsigned length, unsigned last)
45{
46 int end = start + length;
47 int size = 0;
48
49 if (start <= 0) {
50 if (end > 0)
51 size = min_t(unsigned, end, last);
52 } else if (start <= last) {
53 size = min_t(unsigned, last - start, length);
54 }
55
56 return size;
57}
58
0114f404
MS
59static void exynos_plane_mode_set(struct exynos_drm_plane_state *exynos_state)
60
adf5691c 61{
0114f404
MS
62 struct drm_plane_state *state = &exynos_state->base;
63 struct drm_crtc *crtc = exynos_state->base.crtc;
020e79de 64 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
0114f404
MS
65 int crtc_x, crtc_y;
66 unsigned int crtc_w, crtc_h;
67 unsigned int src_x, src_y;
68 unsigned int src_w, src_h;
adf5691c
GP
69 unsigned int actual_w;
70 unsigned int actual_h;
71
0114f404
MS
72 /*
73 * The original src/dest coordinates are stored in exynos_state->base,
74 * but we want to keep another copy internal to our driver that we can
75 * clip/modify ourselves.
76 */
77
78 crtc_x = state->crtc_x;
79 crtc_y = state->crtc_y;
80 crtc_w = state->crtc_w;
81 crtc_h = state->crtc_h;
82
83 src_x = state->src_x >> 16;
84 src_y = state->src_y >> 16;
85 src_w = state->src_w >> 16;
86 src_h = state->src_h >> 16;
87
d16a11a0
MS
88 /* set ratio */
89 exynos_state->h_ratio = (src_w << 16) / crtc_w;
90 exynos_state->v_ratio = (src_h << 16) / crtc_h;
91
92 /* clip to visible area */
020e79de
JS
93 actual_w = exynos_plane_get_size(crtc_x, crtc_w, mode->hdisplay);
94 actual_h = exynos_plane_get_size(crtc_y, crtc_h, mode->vdisplay);
2ab97921
JS
95
96 if (crtc_x < 0) {
97 if (actual_w)
d16a11a0 98 src_x += ((-crtc_x) * exynos_state->h_ratio) >> 16;
2ab97921
JS
99 crtc_x = 0;
100 }
101
102 if (crtc_y < 0) {
103 if (actual_h)
d16a11a0 104 src_y += ((-crtc_y) * exynos_state->v_ratio) >> 16;
2ab97921
JS
105 crtc_y = 0;
106 }
4070d212
JS
107
108 /* set drm framebuffer data. */
0114f404
MS
109 exynos_state->src.x = src_x;
110 exynos_state->src.y = src_y;
111 exynos_state->src.w = (actual_w * exynos_state->h_ratio) >> 16;
112 exynos_state->src.h = (actual_h * exynos_state->v_ratio) >> 16;
8837deea
GP
113
114 /* set plane range to be displayed. */
0114f404
MS
115 exynos_state->crtc.x = crtc_x;
116 exynos_state->crtc.y = crtc_y;
117 exynos_state->crtc.w = actual_w;
118 exynos_state->crtc.h = actual_h;
d88d2463 119
8837deea 120 DRM_DEBUG_KMS("plane : offset_x/y(%d,%d), width/height(%d,%d)",
0114f404
MS
121 exynos_state->crtc.x, exynos_state->crtc.y,
122 exynos_state->crtc.w, exynos_state->crtc.h);
123}
124
125static void exynos_drm_plane_reset(struct drm_plane *plane)
126{
127 struct exynos_drm_plane_state *exynos_state;
128
129 if (plane->state) {
130 exynos_state = to_exynos_plane_state(plane->state);
131 if (exynos_state->base.fb)
132 drm_framebuffer_unreference(exynos_state->base.fb);
133 kfree(exynos_state);
134 plane->state = NULL;
135 }
136
137 exynos_state = kzalloc(sizeof(*exynos_state), GFP_KERNEL);
138 if (exynos_state) {
139 plane->state = &exynos_state->base;
140 plane->state->plane = plane;
141 }
142}
143
144static struct drm_plane_state *
145exynos_drm_plane_duplicate_state(struct drm_plane *plane)
146{
147 struct exynos_drm_plane_state *exynos_state;
148 struct exynos_drm_plane_state *copy;
149
150 exynos_state = to_exynos_plane_state(plane->state);
151 copy = kzalloc(sizeof(*exynos_state), GFP_KERNEL);
152 if (!copy)
153 return NULL;
4070d212 154
0114f404
MS
155 __drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
156 return &copy->base;
157}
158
159static void exynos_drm_plane_destroy_state(struct drm_plane *plane,
160 struct drm_plane_state *old_state)
161{
162 struct exynos_drm_plane_state *old_exynos_state =
163 to_exynos_plane_state(old_state);
164 __drm_atomic_helper_plane_destroy_state(plane, old_state);
165 kfree(old_exynos_state);
4070d212
JS
166}
167
864ee9e6 168static struct drm_plane_funcs exynos_plane_funcs = {
910874a8
GP
169 .update_plane = drm_atomic_helper_update_plane,
170 .disable_plane = drm_atomic_helper_disable_plane,
97464d7d 171 .destroy = drm_plane_cleanup,
0114f404
MS
172 .reset = exynos_drm_plane_reset,
173 .atomic_duplicate_state = exynos_drm_plane_duplicate_state,
174 .atomic_destroy_state = exynos_drm_plane_destroy_state,
864ee9e6
JS
175};
176
6178d3d1
MS
177static int
178exynos_drm_plane_check_size(const struct exynos_drm_plane_config *config,
179 struct exynos_drm_plane_state *state)
180{
181 bool width_ok = false, height_ok = false;
182
183 if (config->capabilities & EXYNOS_DRM_PLANE_CAP_SCALE)
184 return 0;
185
186 if (state->src.w == state->crtc.w)
187 width_ok = true;
188
189 if (state->src.h == state->crtc.h)
190 height_ok = true;
191
192 if ((config->capabilities & EXYNOS_DRM_PLANE_CAP_DOUBLE) &&
193 state->h_ratio == (1 << 15))
194 width_ok = true;
195
196 if ((config->capabilities & EXYNOS_DRM_PLANE_CAP_DOUBLE) &&
197 state->v_ratio == (1 << 15))
198 height_ok = true;
199
200 if (width_ok & height_ok)
201 return 0;
202
203 DRM_DEBUG_KMS("scaling mode is not supported");
204 return -ENOTSUPP;
205}
206
43dbdad2
GP
207static int exynos_plane_atomic_check(struct drm_plane *plane,
208 struct drm_plane_state *state)
209{
d5f5223c 210 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
0114f404
MS
211 struct exynos_drm_plane_state *exynos_state =
212 to_exynos_plane_state(state);
213 int ret = 0;
d5f5223c 214
0114f404 215 if (!state->crtc || !state->fb)
d5f5223c
GP
216 return 0;
217
0114f404
MS
218 /* translate state into exynos_state */
219 exynos_plane_mode_set(exynos_state);
220
6178d3d1 221 ret = exynos_drm_plane_check_size(exynos_plane->config, exynos_state);
0114f404 222 return ret;
43dbdad2
GP
223}
224
225static void exynos_plane_atomic_update(struct drm_plane *plane,
226 struct drm_plane_state *old_state)
227{
228 struct drm_plane_state *state = plane->state;
d5f5223c
GP
229 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(state->crtc);
230 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
43dbdad2
GP
231
232 if (!state->crtc)
233 return;
234
0114f404 235 plane->crtc = state->crtc;
822f6dfd
GP
236 exynos_plane->pending_fb = state->fb;
237
9cc7610a 238 if (exynos_crtc->ops->update_plane)
1e1d1393 239 exynos_crtc->ops->update_plane(exynos_crtc, exynos_plane);
43dbdad2
GP
240}
241
b744868c
GP
242static void exynos_plane_atomic_disable(struct drm_plane *plane,
243 struct drm_plane_state *old_state)
244{
245 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
246 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(old_state->crtc);
247
248 if (!old_state->crtc)
249 return;
250
9cc7610a 251 if (exynos_crtc->ops->disable_plane)
0114f404 252 exynos_crtc->ops->disable_plane(exynos_crtc, exynos_plane);
b744868c
GP
253}
254
43dbdad2
GP
255static const struct drm_plane_helper_funcs plane_helper_funcs = {
256 .atomic_check = exynos_plane_atomic_check,
257 .atomic_update = exynos_plane_atomic_update,
b744868c 258 .atomic_disable = exynos_plane_atomic_disable,
43dbdad2
GP
259};
260
6e2a3b66
GP
261static void exynos_plane_attach_zpos_property(struct drm_plane *plane,
262 unsigned int zpos)
00ae67cf
JS
263{
264 struct drm_device *dev = plane->dev;
265 struct exynos_drm_private *dev_priv = dev->dev_private;
266 struct drm_property *prop;
267
00ae67cf
JS
268 prop = dev_priv->plane_zpos_property;
269 if (!prop) {
92104886
GP
270 prop = drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE,
271 "zpos", 0, MAX_PLANE - 1);
00ae67cf
JS
272 if (!prop)
273 return;
274
275 dev_priv->plane_zpos_property = prop;
276 }
277
6e2a3b66 278 drm_object_attach_property(&plane->base, prop, zpos);
00ae67cf
JS
279}
280
7ee14cdc
GP
281int exynos_plane_init(struct drm_device *dev,
282 struct exynos_drm_plane *exynos_plane,
fd2d2fc2
MS
283 unsigned long possible_crtcs,
284 const struct exynos_drm_plane_config *config)
864ee9e6 285{
b5d2eb3b 286 int err;
864ee9e6 287
fd2d2fc2
MS
288 err = drm_universal_plane_init(dev, &exynos_plane->base,
289 possible_crtcs,
290 &exynos_plane_funcs,
291 config->pixel_formats,
292 config->num_pixel_formats,
293 config->type);
294
b5d2eb3b
JS
295 if (err) {
296 DRM_ERROR("failed to initialize plane\n");
7ee14cdc 297 return err;
b5d2eb3b
JS
298 }
299
43dbdad2
GP
300 drm_plane_helper_add(&exynos_plane->base, &plane_helper_funcs);
301
fd2d2fc2
MS
302 exynos_plane->zpos = config->zpos;
303 exynos_plane->config = config;
6e2a3b66 304
fd2d2fc2
MS
305 if (config->type == DRM_PLANE_TYPE_OVERLAY)
306 exynos_plane_attach_zpos_property(&exynos_plane->base,
307 config->zpos);
864ee9e6 308
7ee14cdc 309 return 0;
864ee9e6 310}
This page took 0.223777 seconds and 5 git commands to generate.