blob: 2f3c9b993acdb37c75b5674a99e2bb95a899ebf9 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Joonyoung Shim864ee9e2011-12-08 17:54:07 +09002/*
3 * Copyright (C) 2011 Samsung Electronics Co.Ltd
4 * Authors: Joonyoung Shim <jy0922.shim@samsung.com>
Joonyoung Shim864ee9e2011-12-08 17:54:07 +09005 */
6
David Howells760285e2012-10-02 18:01:07 +01007#include <drm/drmP.h>
Joonyoung Shim864ee9e2011-12-08 17:54:07 +09008
Andrzej Hajda81e50bc2016-03-15 12:38:02 +01009#include <drm/drm_atomic.h>
Gustavo Padovan4ea95262015-06-01 12:04:44 -030010#include <drm/drm_atomic_helper.h>
Andrzej Hajda81e50bc2016-03-15 12:38:02 +010011#include <drm/drm_plane_helper.h>
12#include <drm/exynos_drm.h>
Joonyoung Shim864ee9e2011-12-08 17:54:07 +090013#include "exynos_drm_drv.h"
Sean Paul080be03d2014-02-19 21:02:55 +090014#include "exynos_drm_crtc.h"
Joonyoung Shim4070d212012-06-27 14:27:05 +090015#include "exynos_drm_fb.h"
16#include "exynos_drm_gem.h"
Mark Browne30655d2013-08-13 00:46:40 +010017#include "exynos_drm_plane.h"
Joonyoung Shim864ee9e2011-12-08 17:54:07 +090018
Joonyoung Shim2ab97922012-09-27 19:25:21 +090019/*
20 * This function is to get X or Y size shown via screen. This needs length and
21 * start position of CRTC.
22 *
23 * <--- length --->
24 * CRTC ----------------
25 * ^ start ^ end
26 *
Joonyoung Shim60a705a2012-12-14 15:48:22 +090027 * There are six cases from a to f.
Joonyoung Shim2ab97922012-09-27 19:25:21 +090028 *
29 * <----- SCREEN ----->
30 * 0 last
31 * ----------|------------------|----------
32 * CRTCs
33 * a -------
34 * b -------
35 * c --------------------------
36 * d --------
37 * e -------
38 * f -------
39 */
40static int exynos_plane_get_size(int start, unsigned length, unsigned last)
41{
42 int end = start + length;
43 int size = 0;
44
45 if (start <= 0) {
46 if (end > 0)
47 size = min_t(unsigned, end, last);
48 } else if (start <= last) {
49 size = min_t(unsigned, last - start, length);
50 }
51
52 return size;
53}
54
Marek Szyprowski0114f402015-11-30 14:53:22 +010055static void exynos_plane_mode_set(struct exynos_drm_plane_state *exynos_state)
Gustavo Padovanadf56912014-11-27 14:56:09 -020056{
Marek Szyprowski0114f402015-11-30 14:53:22 +010057 struct drm_plane_state *state = &exynos_state->base;
Andrzej Hajda81e50bc2016-03-15 12:38:02 +010058 struct drm_crtc *crtc = state->crtc;
59 struct drm_crtc_state *crtc_state =
60 drm_atomic_get_existing_crtc_state(state->state, crtc);
61 struct drm_display_mode *mode = &crtc_state->adjusted_mode;
Marek Szyprowski0114f402015-11-30 14:53:22 +010062 int crtc_x, crtc_y;
63 unsigned int crtc_w, crtc_h;
64 unsigned int src_x, src_y;
65 unsigned int src_w, src_h;
Gustavo Padovanadf56912014-11-27 14:56:09 -020066 unsigned int actual_w;
67 unsigned int actual_h;
68
Marek Szyprowski0114f402015-11-30 14:53:22 +010069 /*
70 * The original src/dest coordinates are stored in exynos_state->base,
71 * but we want to keep another copy internal to our driver that we can
72 * clip/modify ourselves.
73 */
74
75 crtc_x = state->crtc_x;
76 crtc_y = state->crtc_y;
77 crtc_w = state->crtc_w;
78 crtc_h = state->crtc_h;
79
80 src_x = state->src_x >> 16;
81 src_y = state->src_y >> 16;
82 src_w = state->src_w >> 16;
83 src_h = state->src_h >> 16;
84
Marek Szyprowskid16a11a2015-11-30 14:53:28 +010085 /* set ratio */
86 exynos_state->h_ratio = (src_w << 16) / crtc_w;
87 exynos_state->v_ratio = (src_h << 16) / crtc_h;
88
89 /* clip to visible area */
Joonyoung Shim020e79d2015-06-02 21:04:42 +090090 actual_w = exynos_plane_get_size(crtc_x, crtc_w, mode->hdisplay);
91 actual_h = exynos_plane_get_size(crtc_y, crtc_h, mode->vdisplay);
Joonyoung Shim2ab97922012-09-27 19:25:21 +090092
93 if (crtc_x < 0) {
94 if (actual_w)
Marek Szyprowskid16a11a2015-11-30 14:53:28 +010095 src_x += ((-crtc_x) * exynos_state->h_ratio) >> 16;
Joonyoung Shim2ab97922012-09-27 19:25:21 +090096 crtc_x = 0;
97 }
98
99 if (crtc_y < 0) {
100 if (actual_h)
Marek Szyprowskid16a11a2015-11-30 14:53:28 +0100101 src_y += ((-crtc_y) * exynos_state->v_ratio) >> 16;
Joonyoung Shim2ab97922012-09-27 19:25:21 +0900102 crtc_y = 0;
103 }
Joonyoung Shim4070d212012-06-27 14:27:05 +0900104
105 /* set drm framebuffer data. */
Marek Szyprowski0114f402015-11-30 14:53:22 +0100106 exynos_state->src.x = src_x;
107 exynos_state->src.y = src_y;
108 exynos_state->src.w = (actual_w * exynos_state->h_ratio) >> 16;
109 exynos_state->src.h = (actual_h * exynos_state->v_ratio) >> 16;
Joonyoung Shim4070d212012-06-27 14:27:05 +0900110
Gustavo Padovan8837dee2014-11-03 18:13:27 -0200111 /* set plane range to be displayed. */
Marek Szyprowski0114f402015-11-30 14:53:22 +0100112 exynos_state->crtc.x = crtc_x;
113 exynos_state->crtc.y = crtc_y;
114 exynos_state->crtc.w = actual_w;
115 exynos_state->crtc.h = actual_h;
Gustavo Padovand88d2462015-07-16 12:23:38 -0300116
Inki Dae6be90052019-04-15 16:25:12 +0900117 DRM_DEV_DEBUG_KMS(crtc->dev->dev,
118 "plane : offset_x/y(%d,%d), width/height(%d,%d)",
119 exynos_state->crtc.x, exynos_state->crtc.y,
120 exynos_state->crtc.w, exynos_state->crtc.h);
Marek Szyprowski0114f402015-11-30 14:53:22 +0100121}
Joonyoung Shim4070d212012-06-27 14:27:05 +0900122
Marek Szyprowski0114f402015-11-30 14:53:22 +0100123static void exynos_drm_plane_reset(struct drm_plane *plane)
124{
Marek Szyprowski0ea72402015-12-16 13:21:43 +0100125 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
Marek Szyprowski0114f402015-11-30 14:53:22 +0100126 struct exynos_drm_plane_state *exynos_state;
127
128 if (plane->state) {
129 exynos_state = to_exynos_plane_state(plane->state);
Christoph Manszewskia9777262018-09-21 14:24:36 +0200130 __drm_atomic_helper_plane_destroy_state(plane->state);
Marek Szyprowski0114f402015-11-30 14:53:22 +0100131 kfree(exynos_state);
132 plane->state = NULL;
133 }
134
135 exynos_state = kzalloc(sizeof(*exynos_state), GFP_KERNEL);
136 if (exynos_state) {
Christoph Manszewskia9777262018-09-21 14:24:36 +0200137 __drm_atomic_helper_plane_reset(plane, &exynos_state->base);
Marek Szyprowskie47726a2016-05-11 17:16:06 +0200138 plane->state->zpos = exynos_plane->config->zpos;
Marek Szyprowski0114f402015-11-30 14:53:22 +0100139 }
140}
141
142static struct drm_plane_state *
143exynos_drm_plane_duplicate_state(struct drm_plane *plane)
144{
145 struct exynos_drm_plane_state *exynos_state;
146 struct exynos_drm_plane_state *copy;
147
148 exynos_state = to_exynos_plane_state(plane->state);
149 copy = kzalloc(sizeof(*exynos_state), GFP_KERNEL);
150 if (!copy)
151 return NULL;
152
153 __drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
154 return &copy->base;
155}
156
157static void exynos_drm_plane_destroy_state(struct drm_plane *plane,
158 struct drm_plane_state *old_state)
159{
160 struct exynos_drm_plane_state *old_exynos_state =
161 to_exynos_plane_state(old_state);
Daniel Vetter2f701692016-05-09 16:34:10 +0200162 __drm_atomic_helper_plane_destroy_state(old_state);
Marek Szyprowski0114f402015-11-30 14:53:22 +0100163 kfree(old_exynos_state);
Joonyoung Shim4070d212012-06-27 14:27:05 +0900164}
165
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900166static struct drm_plane_funcs exynos_plane_funcs = {
Gustavo Padovan910874a2015-06-01 12:04:46 -0300167 .update_plane = drm_atomic_helper_update_plane,
168 .disable_plane = drm_atomic_helper_disable_plane,
Gustavo Padovan97464d72015-04-01 13:02:10 -0300169 .destroy = drm_plane_cleanup,
Marek Szyprowski0114f402015-11-30 14:53:22 +0100170 .reset = exynos_drm_plane_reset,
171 .atomic_duplicate_state = exynos_drm_plane_duplicate_state,
172 .atomic_destroy_state = exynos_drm_plane_destroy_state,
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900173};
174
Marek Szyprowski6178d3d2015-11-30 14:53:26 +0100175static int
Tobias Jakobif40031c2017-08-22 16:19:37 +0200176exynos_drm_plane_check_format(const struct exynos_drm_plane_config *config,
177 struct exynos_drm_plane_state *state)
178{
179 struct drm_framebuffer *fb = state->base.fb;
Inki Dae6f83d202019-04-15 14:24:36 +0900180 struct drm_device *dev = fb->dev;
Tobias Jakobif40031c2017-08-22 16:19:37 +0200181
182 switch (fb->modifier) {
183 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
184 if (!(config->capabilities & EXYNOS_DRM_PLANE_CAP_TILE))
185 return -ENOTSUPP;
186 break;
187
188 case DRM_FORMAT_MOD_LINEAR:
189 break;
190
191 default:
Inki Dae6f83d202019-04-15 14:24:36 +0900192 DRM_DEV_ERROR(dev->dev, "unsupported pixel format modifier");
Tobias Jakobif40031c2017-08-22 16:19:37 +0200193 return -ENOTSUPP;
194 }
195
196 return 0;
197}
198
199static int
Marek Szyprowski6178d3d2015-11-30 14:53:26 +0100200exynos_drm_plane_check_size(const struct exynos_drm_plane_config *config,
201 struct exynos_drm_plane_state *state)
202{
Inki Dae6be90052019-04-15 16:25:12 +0900203 struct drm_crtc *crtc = state->base.crtc;
Marek Szyprowski6178d3d2015-11-30 14:53:26 +0100204 bool width_ok = false, height_ok = false;
205
206 if (config->capabilities & EXYNOS_DRM_PLANE_CAP_SCALE)
207 return 0;
208
209 if (state->src.w == state->crtc.w)
210 width_ok = true;
211
212 if (state->src.h == state->crtc.h)
213 height_ok = true;
214
215 if ((config->capabilities & EXYNOS_DRM_PLANE_CAP_DOUBLE) &&
216 state->h_ratio == (1 << 15))
217 width_ok = true;
218
219 if ((config->capabilities & EXYNOS_DRM_PLANE_CAP_DOUBLE) &&
220 state->v_ratio == (1 << 15))
221 height_ok = true;
222
Tobias Jakobi39bf860922016-05-25 14:30:07 +0200223 if (width_ok && height_ok)
Marek Szyprowski6178d3d2015-11-30 14:53:26 +0100224 return 0;
225
Inki Dae6be90052019-04-15 16:25:12 +0900226 DRM_DEV_DEBUG_KMS(crtc->dev->dev, "scaling mode is not supported");
Marek Szyprowski6178d3d2015-11-30 14:53:26 +0100227 return -ENOTSUPP;
228}
229
Gustavo Padovan43dbdad2015-06-01 12:04:41 -0300230static int exynos_plane_atomic_check(struct drm_plane *plane,
231 struct drm_plane_state *state)
232{
Gustavo Padovand5f52232015-06-01 12:04:49 -0300233 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
Marek Szyprowski0114f402015-11-30 14:53:22 +0100234 struct exynos_drm_plane_state *exynos_state =
235 to_exynos_plane_state(state);
236 int ret = 0;
Gustavo Padovand5f52232015-06-01 12:04:49 -0300237
Marek Szyprowski0114f402015-11-30 14:53:22 +0100238 if (!state->crtc || !state->fb)
Gustavo Padovand5f52232015-06-01 12:04:49 -0300239 return 0;
240
Marek Szyprowski0114f402015-11-30 14:53:22 +0100241 /* translate state into exynos_state */
242 exynos_plane_mode_set(exynos_state);
Gustavo Padovand5f52232015-06-01 12:04:49 -0300243
Tobias Jakobif40031c2017-08-22 16:19:37 +0200244 ret = exynos_drm_plane_check_format(exynos_plane->config, exynos_state);
245 if (ret)
246 return ret;
247
Marek Szyprowski6178d3d2015-11-30 14:53:26 +0100248 ret = exynos_drm_plane_check_size(exynos_plane->config, exynos_state);
Marek Szyprowski0114f402015-11-30 14:53:22 +0100249 return ret;
Gustavo Padovan43dbdad2015-06-01 12:04:41 -0300250}
251
252static void exynos_plane_atomic_update(struct drm_plane *plane,
253 struct drm_plane_state *old_state)
254{
255 struct drm_plane_state *state = plane->state;
Gustavo Padovand5f52232015-06-01 12:04:49 -0300256 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(state->crtc);
257 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
Gustavo Padovan43dbdad2015-06-01 12:04:41 -0300258
259 if (!state->crtc)
260 return;
261
Gustavo Padovan9cc76102015-08-03 14:38:05 +0900262 if (exynos_crtc->ops->update_plane)
Gustavo Padovan1e1d1392015-08-03 14:39:36 +0900263 exynos_crtc->ops->update_plane(exynos_crtc, exynos_plane);
Gustavo Padovan43dbdad2015-06-01 12:04:41 -0300264}
265
Gustavo Padovanb7448682015-06-01 12:04:42 -0300266static void exynos_plane_atomic_disable(struct drm_plane *plane,
267 struct drm_plane_state *old_state)
268{
269 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
270 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(old_state->crtc);
271
272 if (!old_state->crtc)
273 return;
274
Gustavo Padovan9cc76102015-08-03 14:38:05 +0900275 if (exynos_crtc->ops->disable_plane)
Marek Szyprowski0114f402015-11-30 14:53:22 +0100276 exynos_crtc->ops->disable_plane(exynos_crtc, exynos_plane);
Gustavo Padovanb7448682015-06-01 12:04:42 -0300277}
278
Gustavo Padovan43dbdad2015-06-01 12:04:41 -0300279static const struct drm_plane_helper_funcs plane_helper_funcs = {
280 .atomic_check = exynos_plane_atomic_check,
281 .atomic_update = exynos_plane_atomic_update,
Gustavo Padovanb7448682015-06-01 12:04:42 -0300282 .atomic_disable = exynos_plane_atomic_disable,
Gustavo Padovan43dbdad2015-06-01 12:04:41 -0300283};
284
Gustavo Padovan6e2a3b62015-04-03 21:05:52 +0900285static void exynos_plane_attach_zpos_property(struct drm_plane *plane,
Marek Szyprowskie9dfe832018-05-23 12:15:50 +0200286 int zpos, bool immutable)
Joonyoung Shim00ae67c2012-06-27 14:27:06 +0900287{
Marek Szyprowskie47726a2016-05-11 17:16:06 +0200288 if (immutable)
Marek Szyprowskie9dfe832018-05-23 12:15:50 +0200289 drm_plane_create_zpos_immutable_property(plane, zpos);
Marek Szyprowskie47726a2016-05-11 17:16:06 +0200290 else
Marek Szyprowskie9dfe832018-05-23 12:15:50 +0200291 drm_plane_create_zpos_property(plane, zpos, 0, MAX_PLANE - 1);
Joonyoung Shim00ae67c2012-06-27 14:27:06 +0900292}
293
Gustavo Padovan7ee14cd2015-04-03 21:03:40 +0900294int exynos_plane_init(struct drm_device *dev,
Andrzej Hajda2c826072017-03-15 15:41:05 +0100295 struct exynos_drm_plane *exynos_plane, unsigned int index,
Marek Szyprowskifd2d2fc2015-11-30 14:53:25 +0100296 const struct exynos_drm_plane_config *config)
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900297{
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +0900298 int err;
Christoph Manszewski482582c2018-09-21 14:24:37 +0200299 unsigned int supported_modes = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
300 BIT(DRM_MODE_BLEND_PREMULTI) |
301 BIT(DRM_MODE_BLEND_COVERAGE);
302 struct drm_plane *plane = &exynos_plane->base;
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900303
Marek Szyprowskifd2d2fc2015-11-30 14:53:25 +0100304 err = drm_universal_plane_init(dev, &exynos_plane->base,
Andrzej Hajda2c826072017-03-15 15:41:05 +0100305 1 << dev->mode_config.num_crtc,
Marek Szyprowskifd2d2fc2015-11-30 14:53:25 +0100306 &exynos_plane_funcs,
307 config->pixel_formats,
308 config->num_pixel_formats,
Ben Widawskye6fc3b62017-07-23 20:46:38 -0700309 NULL, config->type, NULL);
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +0900310 if (err) {
Inki Dae6f83d202019-04-15 14:24:36 +0900311 DRM_DEV_ERROR(dev->dev, "failed to initialize plane\n");
Gustavo Padovan7ee14cd2015-04-03 21:03:40 +0900312 return err;
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +0900313 }
314
Gustavo Padovan43dbdad2015-06-01 12:04:41 -0300315 drm_plane_helper_add(&exynos_plane->base, &plane_helper_funcs);
316
Marek Szyprowski40bdfb02015-12-16 13:21:42 +0100317 exynos_plane->index = index;
Marek Szyprowskifd2d2fc2015-11-30 14:53:25 +0100318 exynos_plane->config = config;
Gustavo Padovan6e2a3b62015-04-03 21:05:52 +0900319
Marek Szyprowskie9dfe832018-05-23 12:15:50 +0200320 exynos_plane_attach_zpos_property(&exynos_plane->base, config->zpos,
Marek Szyprowskie47726a2016-05-11 17:16:06 +0200321 !(config->capabilities & EXYNOS_DRM_PLANE_CAP_ZPOS));
Joonyoung Shim00ae67c2012-06-27 14:27:06 +0900322
Christoph Manszewski482582c2018-09-21 14:24:37 +0200323 if (config->capabilities & EXYNOS_DRM_PLANE_CAP_PIX_BLEND)
324 drm_plane_create_blend_mode_property(plane, supported_modes);
325
Christoph Manszewski6ac99a32018-09-21 14:24:38 +0200326 if (config->capabilities & EXYNOS_DRM_PLANE_CAP_WIN_BLEND)
327 drm_plane_create_alpha_property(plane);
328
Gustavo Padovan7ee14cd2015-04-03 21:03:40 +0900329 return 0;
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900330}