blob: d86227236f5519f93099565622956f00530b7e24 [file] [log] [blame]
Joonyoung Shim864ee9e2011-12-08 17:54:07 +09001/*
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
David Howells760285e2012-10-02 18:01:07 +010012#include <drm/drmP.h>
Joonyoung Shim864ee9e2011-12-08 17:54:07 +090013
David Howells760285e2012-10-02 18:01:07 +010014#include <drm/exynos_drm.h>
Gustavo Padovanadf56912014-11-27 14:56:09 -020015#include <drm/drm_plane_helper.h>
Gustavo Padovan4ea95262015-06-01 12:04:44 -030016#include <drm/drm_atomic_helper.h>
Joonyoung Shim864ee9e2011-12-08 17:54:07 +090017#include "exynos_drm_drv.h"
Sean Paul080be03d2014-02-19 21:02:55 +090018#include "exynos_drm_crtc.h"
Joonyoung Shim4070d212012-06-27 14:27:05 +090019#include "exynos_drm_fb.h"
20#include "exynos_drm_gem.h"
Mark Browne30655d2013-08-13 00:46:40 +010021#include "exynos_drm_plane.h"
Joonyoung Shim864ee9e2011-12-08 17:54:07 +090022
Joonyoung Shim2ab97922012-09-27 19:25:21 +090023/*
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 *
Joonyoung Shim60a705a2012-12-14 15:48:22 +090031 * There are six cases from a to f.
Joonyoung Shim2ab97922012-09-27 19:25:21 +090032 *
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
Marek Szyprowski0114f402015-11-30 14:53:22 +010059static void exynos_plane_mode_set(struct exynos_drm_plane_state *exynos_state)
60
Gustavo Padovanadf56912014-11-27 14:56:09 -020061{
Marek Szyprowski0114f402015-11-30 14:53:22 +010062 struct drm_plane_state *state = &exynos_state->base;
63 struct drm_crtc *crtc = exynos_state->base.crtc;
Joonyoung Shim020e79d2015-06-02 21:04:42 +090064 struct drm_display_mode *mode = &crtc->state->adjusted_mode;
Marek Szyprowski0114f402015-11-30 14:53:22 +010065 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;
Gustavo Padovanadf56912014-11-27 14:56:09 -020069 unsigned int actual_w;
70 unsigned int actual_h;
71
Marek Szyprowski0114f402015-11-30 14:53:22 +010072 /*
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
Marek Szyprowskid16a11a2015-11-30 14:53:28 +010088 /* 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 */
Joonyoung Shim020e79d2015-06-02 21:04:42 +090093 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);
Joonyoung Shim2ab97922012-09-27 19:25:21 +090095
96 if (crtc_x < 0) {
97 if (actual_w)
Marek Szyprowskid16a11a2015-11-30 14:53:28 +010098 src_x += ((-crtc_x) * exynos_state->h_ratio) >> 16;
Joonyoung Shim2ab97922012-09-27 19:25:21 +090099 crtc_x = 0;
100 }
101
102 if (crtc_y < 0) {
103 if (actual_h)
Marek Szyprowskid16a11a2015-11-30 14:53:28 +0100104 src_y += ((-crtc_y) * exynos_state->v_ratio) >> 16;
Joonyoung Shim2ab97922012-09-27 19:25:21 +0900105 crtc_y = 0;
106 }
Joonyoung Shim4070d212012-06-27 14:27:05 +0900107
108 /* set drm framebuffer data. */
Marek Szyprowski0114f402015-11-30 14:53:22 +0100109 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;
Joonyoung Shim4070d212012-06-27 14:27:05 +0900113
Gustavo Padovan8837dee2014-11-03 18:13:27 -0200114 /* set plane range to be displayed. */
Marek Szyprowski0114f402015-11-30 14:53:22 +0100115 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;
Gustavo Padovand88d2462015-07-16 12:23:38 -0300119
Gustavo Padovan8837dee2014-11-03 18:13:27 -0200120 DRM_DEBUG_KMS("plane : offset_x/y(%d,%d), width/height(%d,%d)",
Marek Szyprowski0114f402015-11-30 14:53:22 +0100121 exynos_state->crtc.x, exynos_state->crtc.y,
122 exynos_state->crtc.w, exynos_state->crtc.h);
123}
Joonyoung Shim4070d212012-06-27 14:27:05 +0900124
Marek Szyprowski0114f402015-11-30 14:53:22 +0100125static void exynos_drm_plane_reset(struct drm_plane *plane)
126{
Marek Szyprowski0ea72402015-12-16 13:21:43 +0100127 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
Marek Szyprowski0114f402015-11-30 14:53:22 +0100128 struct exynos_drm_plane_state *exynos_state;
129
130 if (plane->state) {
131 exynos_state = to_exynos_plane_state(plane->state);
132 if (exynos_state->base.fb)
133 drm_framebuffer_unreference(exynos_state->base.fb);
134 kfree(exynos_state);
135 plane->state = NULL;
136 }
137
138 exynos_state = kzalloc(sizeof(*exynos_state), GFP_KERNEL);
139 if (exynos_state) {
Marek Szyprowski0ea72402015-12-16 13:21:43 +0100140 exynos_state->zpos = exynos_plane->config->zpos;
Marek Szyprowski0114f402015-11-30 14:53:22 +0100141 plane->state = &exynos_state->base;
142 plane->state->plane = plane;
143 }
144}
145
146static struct drm_plane_state *
147exynos_drm_plane_duplicate_state(struct drm_plane *plane)
148{
149 struct exynos_drm_plane_state *exynos_state;
150 struct exynos_drm_plane_state *copy;
151
152 exynos_state = to_exynos_plane_state(plane->state);
153 copy = kzalloc(sizeof(*exynos_state), GFP_KERNEL);
154 if (!copy)
155 return NULL;
156
157 __drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
Marek Szyprowski0ea72402015-12-16 13:21:43 +0100158 copy->zpos = exynos_state->zpos;
Marek Szyprowski0114f402015-11-30 14:53:22 +0100159 return &copy->base;
160}
161
162static void exynos_drm_plane_destroy_state(struct drm_plane *plane,
163 struct drm_plane_state *old_state)
164{
165 struct exynos_drm_plane_state *old_exynos_state =
166 to_exynos_plane_state(old_state);
167 __drm_atomic_helper_plane_destroy_state(plane, old_state);
168 kfree(old_exynos_state);
Joonyoung Shim4070d212012-06-27 14:27:05 +0900169}
170
Marek Szyprowski0ea72402015-12-16 13:21:43 +0100171static int exynos_drm_plane_atomic_set_property(struct drm_plane *plane,
172 struct drm_plane_state *state,
173 struct drm_property *property,
174 uint64_t val)
175{
176 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
177 struct exynos_drm_plane_state *exynos_state =
178 to_exynos_plane_state(state);
179 struct exynos_drm_private *dev_priv = plane->dev->dev_private;
180 const struct exynos_drm_plane_config *config = exynos_plane->config;
181
182 if (property == dev_priv->plane_zpos_property &&
183 (config->capabilities & EXYNOS_DRM_PLANE_CAP_ZPOS))
184 exynos_state->zpos = val;
185 else
186 return -EINVAL;
187
188 return 0;
189}
190
191static int exynos_drm_plane_atomic_get_property(struct drm_plane *plane,
192 const struct drm_plane_state *state,
193 struct drm_property *property,
194 uint64_t *val)
195{
196 const struct exynos_drm_plane_state *exynos_state =
197 container_of(state, const struct exynos_drm_plane_state, base);
198 struct exynos_drm_private *dev_priv = plane->dev->dev_private;
199
200 if (property == dev_priv->plane_zpos_property)
201 *val = exynos_state->zpos;
202 else
203 return -EINVAL;
204
205 return 0;
206}
207
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900208static struct drm_plane_funcs exynos_plane_funcs = {
Gustavo Padovan910874a2015-06-01 12:04:46 -0300209 .update_plane = drm_atomic_helper_update_plane,
210 .disable_plane = drm_atomic_helper_disable_plane,
Gustavo Padovan97464d72015-04-01 13:02:10 -0300211 .destroy = drm_plane_cleanup,
Marek Szyprowski0ea72402015-12-16 13:21:43 +0100212 .set_property = drm_atomic_helper_plane_set_property,
Marek Szyprowski0114f402015-11-30 14:53:22 +0100213 .reset = exynos_drm_plane_reset,
214 .atomic_duplicate_state = exynos_drm_plane_duplicate_state,
215 .atomic_destroy_state = exynos_drm_plane_destroy_state,
Marek Szyprowski0ea72402015-12-16 13:21:43 +0100216 .atomic_set_property = exynos_drm_plane_atomic_set_property,
217 .atomic_get_property = exynos_drm_plane_atomic_get_property,
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900218};
219
Marek Szyprowski6178d3d2015-11-30 14:53:26 +0100220static int
221exynos_drm_plane_check_size(const struct exynos_drm_plane_config *config,
222 struct exynos_drm_plane_state *state)
223{
224 bool width_ok = false, height_ok = false;
225
226 if (config->capabilities & EXYNOS_DRM_PLANE_CAP_SCALE)
227 return 0;
228
229 if (state->src.w == state->crtc.w)
230 width_ok = true;
231
232 if (state->src.h == state->crtc.h)
233 height_ok = true;
234
235 if ((config->capabilities & EXYNOS_DRM_PLANE_CAP_DOUBLE) &&
236 state->h_ratio == (1 << 15))
237 width_ok = true;
238
239 if ((config->capabilities & EXYNOS_DRM_PLANE_CAP_DOUBLE) &&
240 state->v_ratio == (1 << 15))
241 height_ok = true;
242
243 if (width_ok & height_ok)
244 return 0;
245
246 DRM_DEBUG_KMS("scaling mode is not supported");
247 return -ENOTSUPP;
248}
249
Gustavo Padovan43dbdad2015-06-01 12:04:41 -0300250static int exynos_plane_atomic_check(struct drm_plane *plane,
251 struct drm_plane_state *state)
252{
Gustavo Padovand5f52232015-06-01 12:04:49 -0300253 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
Marek Szyprowski0114f402015-11-30 14:53:22 +0100254 struct exynos_drm_plane_state *exynos_state =
255 to_exynos_plane_state(state);
256 int ret = 0;
Gustavo Padovand5f52232015-06-01 12:04:49 -0300257
Marek Szyprowski0114f402015-11-30 14:53:22 +0100258 if (!state->crtc || !state->fb)
Gustavo Padovand5f52232015-06-01 12:04:49 -0300259 return 0;
260
Marek Szyprowski0114f402015-11-30 14:53:22 +0100261 /* translate state into exynos_state */
262 exynos_plane_mode_set(exynos_state);
Gustavo Padovand5f52232015-06-01 12:04:49 -0300263
Marek Szyprowski6178d3d2015-11-30 14:53:26 +0100264 ret = exynos_drm_plane_check_size(exynos_plane->config, exynos_state);
Marek Szyprowski0114f402015-11-30 14:53:22 +0100265 return ret;
Gustavo Padovan43dbdad2015-06-01 12:04:41 -0300266}
267
268static void exynos_plane_atomic_update(struct drm_plane *plane,
269 struct drm_plane_state *old_state)
270{
271 struct drm_plane_state *state = plane->state;
Gustavo Padovand5f52232015-06-01 12:04:49 -0300272 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(state->crtc);
273 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
Gustavo Padovan43dbdad2015-06-01 12:04:41 -0300274
275 if (!state->crtc)
276 return;
277
Marek Szyprowski0114f402015-11-30 14:53:22 +0100278 plane->crtc = state->crtc;
Gustavo Padovan822f6df2015-08-15 13:26:14 -0300279 exynos_plane->pending_fb = state->fb;
280
Gustavo Padovan9cc76102015-08-03 14:38:05 +0900281 if (exynos_crtc->ops->update_plane)
Gustavo Padovan1e1d1392015-08-03 14:39:36 +0900282 exynos_crtc->ops->update_plane(exynos_crtc, exynos_plane);
Gustavo Padovan43dbdad2015-06-01 12:04:41 -0300283}
284
Gustavo Padovanb7448682015-06-01 12:04:42 -0300285static void exynos_plane_atomic_disable(struct drm_plane *plane,
286 struct drm_plane_state *old_state)
287{
288 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
289 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(old_state->crtc);
290
291 if (!old_state->crtc)
292 return;
293
Gustavo Padovan9cc76102015-08-03 14:38:05 +0900294 if (exynos_crtc->ops->disable_plane)
Marek Szyprowski0114f402015-11-30 14:53:22 +0100295 exynos_crtc->ops->disable_plane(exynos_crtc, exynos_plane);
Gustavo Padovanb7448682015-06-01 12:04:42 -0300296}
297
Gustavo Padovan43dbdad2015-06-01 12:04:41 -0300298static const struct drm_plane_helper_funcs plane_helper_funcs = {
299 .atomic_check = exynos_plane_atomic_check,
300 .atomic_update = exynos_plane_atomic_update,
Gustavo Padovanb7448682015-06-01 12:04:42 -0300301 .atomic_disable = exynos_plane_atomic_disable,
Gustavo Padovan43dbdad2015-06-01 12:04:41 -0300302};
303
Gustavo Padovan6e2a3b62015-04-03 21:05:52 +0900304static void exynos_plane_attach_zpos_property(struct drm_plane *plane,
305 unsigned int zpos)
Joonyoung Shim00ae67c2012-06-27 14:27:06 +0900306{
307 struct drm_device *dev = plane->dev;
308 struct exynos_drm_private *dev_priv = dev->dev_private;
309 struct drm_property *prop;
310
Joonyoung Shim00ae67c2012-06-27 14:27:06 +0900311 prop = dev_priv->plane_zpos_property;
312 if (!prop) {
Marek Szyprowski0ea72402015-12-16 13:21:43 +0100313 prop = drm_property_create_range(dev, 0, "zpos",
314 0, MAX_PLANE - 1);
Joonyoung Shim00ae67c2012-06-27 14:27:06 +0900315 if (!prop)
316 return;
317
318 dev_priv->plane_zpos_property = prop;
319 }
320
Gustavo Padovan6e2a3b62015-04-03 21:05:52 +0900321 drm_object_attach_property(&plane->base, prop, zpos);
Joonyoung Shim00ae67c2012-06-27 14:27:06 +0900322}
323
Gustavo Padovan7ee14cd2015-04-03 21:03:40 +0900324int exynos_plane_init(struct drm_device *dev,
325 struct exynos_drm_plane *exynos_plane,
Marek Szyprowski40bdfb02015-12-16 13:21:42 +0100326 unsigned int index, unsigned long possible_crtcs,
Marek Szyprowskifd2d2fc2015-11-30 14:53:25 +0100327 const struct exynos_drm_plane_config *config)
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900328{
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +0900329 int err;
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900330
Marek Szyprowskifd2d2fc2015-11-30 14:53:25 +0100331 err = drm_universal_plane_init(dev, &exynos_plane->base,
332 possible_crtcs,
333 &exynos_plane_funcs,
334 config->pixel_formats,
335 config->num_pixel_formats,
Dave Airlie870a1712015-12-15 10:42:07 +1000336 config->type, NULL);
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +0900337 if (err) {
338 DRM_ERROR("failed to initialize plane\n");
Gustavo Padovan7ee14cd2015-04-03 21:03:40 +0900339 return err;
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +0900340 }
341
Gustavo Padovan43dbdad2015-06-01 12:04:41 -0300342 drm_plane_helper_add(&exynos_plane->base, &plane_helper_funcs);
343
Marek Szyprowski40bdfb02015-12-16 13:21:42 +0100344 exynos_plane->index = index;
Marek Szyprowskifd2d2fc2015-11-30 14:53:25 +0100345 exynos_plane->config = config;
Gustavo Padovan6e2a3b62015-04-03 21:05:52 +0900346
Marek Szyprowski0ea72402015-12-16 13:21:43 +0100347 exynos_plane_attach_zpos_property(&exynos_plane->base, config->zpos);
Joonyoung Shim00ae67c2012-06-27 14:27:06 +0900348
Gustavo Padovan7ee14cd2015-04-03 21:03:40 +0900349 return 0;
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900350}