blob: e18babb251702dcbf9b92c6c82edfcf3e387a84c [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
Andrzej Hajda81e50bc2016-03-15 12:38:02 +010014#include <drm/drm_atomic.h>
Gustavo Padovan4ea95262015-06-01 12:04:44 -030015#include <drm/drm_atomic_helper.h>
Andrzej Hajda81e50bc2016-03-15 12:38:02 +010016#include <drm/drm_plane_helper.h>
17#include <drm/exynos_drm.h>
Joonyoung Shim864ee9e2011-12-08 17:54:07 +090018#include "exynos_drm_drv.h"
Sean Paul080be03d2014-02-19 21:02:55 +090019#include "exynos_drm_crtc.h"
Joonyoung Shim4070d212012-06-27 14:27:05 +090020#include "exynos_drm_fb.h"
21#include "exynos_drm_gem.h"
Mark Browne30655d2013-08-13 00:46:40 +010022#include "exynos_drm_plane.h"
Joonyoung Shim864ee9e2011-12-08 17:54:07 +090023
Joonyoung Shim2ab97922012-09-27 19:25:21 +090024/*
25 * This function is to get X or Y size shown via screen. This needs length and
26 * start position of CRTC.
27 *
28 * <--- length --->
29 * CRTC ----------------
30 * ^ start ^ end
31 *
Joonyoung Shim60a705a2012-12-14 15:48:22 +090032 * There are six cases from a to f.
Joonyoung Shim2ab97922012-09-27 19:25:21 +090033 *
34 * <----- SCREEN ----->
35 * 0 last
36 * ----------|------------------|----------
37 * CRTCs
38 * a -------
39 * b -------
40 * c --------------------------
41 * d --------
42 * e -------
43 * f -------
44 */
45static int exynos_plane_get_size(int start, unsigned length, unsigned last)
46{
47 int end = start + length;
48 int size = 0;
49
50 if (start <= 0) {
51 if (end > 0)
52 size = min_t(unsigned, end, last);
53 } else if (start <= last) {
54 size = min_t(unsigned, last - start, length);
55 }
56
57 return size;
58}
59
Marek Szyprowski0114f402015-11-30 14:53:22 +010060static void exynos_plane_mode_set(struct exynos_drm_plane_state *exynos_state)
Gustavo Padovanadf56912014-11-27 14:56:09 -020061{
Marek Szyprowski0114f402015-11-30 14:53:22 +010062 struct drm_plane_state *state = &exynos_state->base;
Andrzej Hajda81e50bc2016-03-15 12:38:02 +010063 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;
Marek Szyprowski0114f402015-11-30 14:53:22 +010067 int crtc_x, crtc_y;
68 unsigned int crtc_w, crtc_h;
69 unsigned int src_x, src_y;
70 unsigned int src_w, src_h;
Gustavo Padovanadf56912014-11-27 14:56:09 -020071 unsigned int actual_w;
72 unsigned int actual_h;
73
Marek Szyprowski0114f402015-11-30 14:53:22 +010074 /*
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.
78 */
79
80 crtc_x = state->crtc_x;
81 crtc_y = state->crtc_y;
82 crtc_w = state->crtc_w;
83 crtc_h = state->crtc_h;
84
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;
89
Marek Szyprowskid16a11a2015-11-30 14:53:28 +010090 /* set ratio */
91 exynos_state->h_ratio = (src_w << 16) / crtc_w;
92 exynos_state->v_ratio = (src_h << 16) / crtc_h;
93
94 /* clip to visible area */
Joonyoung Shim020e79d2015-06-02 21:04:42 +090095 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);
Joonyoung Shim2ab97922012-09-27 19:25:21 +090097
98 if (crtc_x < 0) {
99 if (actual_w)
Marek Szyprowskid16a11a2015-11-30 14:53:28 +0100100 src_x += ((-crtc_x) * exynos_state->h_ratio) >> 16;
Joonyoung Shim2ab97922012-09-27 19:25:21 +0900101 crtc_x = 0;
102 }
103
104 if (crtc_y < 0) {
105 if (actual_h)
Marek Szyprowskid16a11a2015-11-30 14:53:28 +0100106 src_y += ((-crtc_y) * exynos_state->v_ratio) >> 16;
Joonyoung Shim2ab97922012-09-27 19:25:21 +0900107 crtc_y = 0;
108 }
Joonyoung Shim4070d212012-06-27 14:27:05 +0900109
110 /* set drm framebuffer data. */
Marek Szyprowski0114f402015-11-30 14:53:22 +0100111 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;
Joonyoung Shim4070d212012-06-27 14:27:05 +0900115
Gustavo Padovan8837dee2014-11-03 18:13:27 -0200116 /* set plane range to be displayed. */
Marek Szyprowski0114f402015-11-30 14:53:22 +0100117 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;
Gustavo Padovand88d2462015-07-16 12:23:38 -0300121
Inki Dae6be90052019-04-15 16:25:12 +0900122 DRM_DEV_DEBUG_KMS(crtc->dev->dev,
123 "plane : offset_x/y(%d,%d), width/height(%d,%d)",
124 exynos_state->crtc.x, exynos_state->crtc.y,
125 exynos_state->crtc.w, exynos_state->crtc.h);
Marek Szyprowski0114f402015-11-30 14:53:22 +0100126}
Joonyoung Shim4070d212012-06-27 14:27:05 +0900127
Marek Szyprowski0114f402015-11-30 14:53:22 +0100128static void exynos_drm_plane_reset(struct drm_plane *plane)
129{
Marek Szyprowski0ea72402015-12-16 13:21:43 +0100130 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
Marek Szyprowski0114f402015-11-30 14:53:22 +0100131 struct exynos_drm_plane_state *exynos_state;
132
133 if (plane->state) {
134 exynos_state = to_exynos_plane_state(plane->state);
Christoph Manszewskia9777262018-09-21 14:24:36 +0200135 __drm_atomic_helper_plane_destroy_state(plane->state);
Marek Szyprowski0114f402015-11-30 14:53:22 +0100136 kfree(exynos_state);
137 plane->state = NULL;
138 }
139
140 exynos_state = kzalloc(sizeof(*exynos_state), GFP_KERNEL);
141 if (exynos_state) {
Christoph Manszewskia9777262018-09-21 14:24:36 +0200142 __drm_atomic_helper_plane_reset(plane, &exynos_state->base);
Marek Szyprowskie47726a2016-05-11 17:16:06 +0200143 plane->state->zpos = exynos_plane->config->zpos;
Marek Szyprowski0114f402015-11-30 14:53:22 +0100144 }
145}
146
147static struct drm_plane_state *
148exynos_drm_plane_duplicate_state(struct drm_plane *plane)
149{
150 struct exynos_drm_plane_state *exynos_state;
151 struct exynos_drm_plane_state *copy;
152
153 exynos_state = to_exynos_plane_state(plane->state);
154 copy = kzalloc(sizeof(*exynos_state), GFP_KERNEL);
155 if (!copy)
156 return NULL;
157
158 __drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
159 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);
Daniel Vetter2f701692016-05-09 16:34:10 +0200167 __drm_atomic_helper_plane_destroy_state(old_state);
Marek Szyprowski0114f402015-11-30 14:53:22 +0100168 kfree(old_exynos_state);
Joonyoung Shim4070d212012-06-27 14:27:05 +0900169}
170
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900171static struct drm_plane_funcs exynos_plane_funcs = {
Gustavo Padovan910874a2015-06-01 12:04:46 -0300172 .update_plane = drm_atomic_helper_update_plane,
173 .disable_plane = drm_atomic_helper_disable_plane,
Gustavo Padovan97464d72015-04-01 13:02:10 -0300174 .destroy = drm_plane_cleanup,
Marek Szyprowski0114f402015-11-30 14:53:22 +0100175 .reset = exynos_drm_plane_reset,
176 .atomic_duplicate_state = exynos_drm_plane_duplicate_state,
177 .atomic_destroy_state = exynos_drm_plane_destroy_state,
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900178};
179
Marek Szyprowski6178d3d2015-11-30 14:53:26 +0100180static int
Tobias Jakobif40031c2017-08-22 16:19:37 +0200181exynos_drm_plane_check_format(const struct exynos_drm_plane_config *config,
182 struct exynos_drm_plane_state *state)
183{
184 struct drm_framebuffer *fb = state->base.fb;
Inki Dae6f83d202019-04-15 14:24:36 +0900185 struct drm_device *dev = fb->dev;
Tobias Jakobif40031c2017-08-22 16:19:37 +0200186
187 switch (fb->modifier) {
188 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
189 if (!(config->capabilities & EXYNOS_DRM_PLANE_CAP_TILE))
190 return -ENOTSUPP;
191 break;
192
193 case DRM_FORMAT_MOD_LINEAR:
194 break;
195
196 default:
Inki Dae6f83d202019-04-15 14:24:36 +0900197 DRM_DEV_ERROR(dev->dev, "unsupported pixel format modifier");
Tobias Jakobif40031c2017-08-22 16:19:37 +0200198 return -ENOTSUPP;
199 }
200
201 return 0;
202}
203
204static int
Marek Szyprowski6178d3d2015-11-30 14:53:26 +0100205exynos_drm_plane_check_size(const struct exynos_drm_plane_config *config,
206 struct exynos_drm_plane_state *state)
207{
Inki Dae6be90052019-04-15 16:25:12 +0900208 struct drm_crtc *crtc = state->base.crtc;
Marek Szyprowski6178d3d2015-11-30 14:53:26 +0100209 bool width_ok = false, height_ok = false;
210
211 if (config->capabilities & EXYNOS_DRM_PLANE_CAP_SCALE)
212 return 0;
213
214 if (state->src.w == state->crtc.w)
215 width_ok = true;
216
217 if (state->src.h == state->crtc.h)
218 height_ok = true;
219
220 if ((config->capabilities & EXYNOS_DRM_PLANE_CAP_DOUBLE) &&
221 state->h_ratio == (1 << 15))
222 width_ok = true;
223
224 if ((config->capabilities & EXYNOS_DRM_PLANE_CAP_DOUBLE) &&
225 state->v_ratio == (1 << 15))
226 height_ok = true;
227
Tobias Jakobi39bf860922016-05-25 14:30:07 +0200228 if (width_ok && height_ok)
Marek Szyprowski6178d3d2015-11-30 14:53:26 +0100229 return 0;
230
Inki Dae6be90052019-04-15 16:25:12 +0900231 DRM_DEV_DEBUG_KMS(crtc->dev->dev, "scaling mode is not supported");
Marek Szyprowski6178d3d2015-11-30 14:53:26 +0100232 return -ENOTSUPP;
233}
234
Gustavo Padovan43dbdad2015-06-01 12:04:41 -0300235static int exynos_plane_atomic_check(struct drm_plane *plane,
236 struct drm_plane_state *state)
237{
Gustavo Padovand5f52232015-06-01 12:04:49 -0300238 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
Marek Szyprowski0114f402015-11-30 14:53:22 +0100239 struct exynos_drm_plane_state *exynos_state =
240 to_exynos_plane_state(state);
241 int ret = 0;
Gustavo Padovand5f52232015-06-01 12:04:49 -0300242
Marek Szyprowski0114f402015-11-30 14:53:22 +0100243 if (!state->crtc || !state->fb)
Gustavo Padovand5f52232015-06-01 12:04:49 -0300244 return 0;
245
Marek Szyprowski0114f402015-11-30 14:53:22 +0100246 /* translate state into exynos_state */
247 exynos_plane_mode_set(exynos_state);
Gustavo Padovand5f52232015-06-01 12:04:49 -0300248
Tobias Jakobif40031c2017-08-22 16:19:37 +0200249 ret = exynos_drm_plane_check_format(exynos_plane->config, exynos_state);
250 if (ret)
251 return ret;
252
Marek Szyprowski6178d3d2015-11-30 14:53:26 +0100253 ret = exynos_drm_plane_check_size(exynos_plane->config, exynos_state);
Marek Szyprowski0114f402015-11-30 14:53:22 +0100254 return ret;
Gustavo Padovan43dbdad2015-06-01 12:04:41 -0300255}
256
257static void exynos_plane_atomic_update(struct drm_plane *plane,
258 struct drm_plane_state *old_state)
259{
260 struct drm_plane_state *state = plane->state;
Gustavo Padovand5f52232015-06-01 12:04:49 -0300261 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(state->crtc);
262 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
Gustavo Padovan43dbdad2015-06-01 12:04:41 -0300263
264 if (!state->crtc)
265 return;
266
Gustavo Padovan9cc76102015-08-03 14:38:05 +0900267 if (exynos_crtc->ops->update_plane)
Gustavo Padovan1e1d1392015-08-03 14:39:36 +0900268 exynos_crtc->ops->update_plane(exynos_crtc, exynos_plane);
Gustavo Padovan43dbdad2015-06-01 12:04:41 -0300269}
270
Gustavo Padovanb7448682015-06-01 12:04:42 -0300271static void exynos_plane_atomic_disable(struct drm_plane *plane,
272 struct drm_plane_state *old_state)
273{
274 struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
275 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(old_state->crtc);
276
277 if (!old_state->crtc)
278 return;
279
Gustavo Padovan9cc76102015-08-03 14:38:05 +0900280 if (exynos_crtc->ops->disable_plane)
Marek Szyprowski0114f402015-11-30 14:53:22 +0100281 exynos_crtc->ops->disable_plane(exynos_crtc, exynos_plane);
Gustavo Padovanb7448682015-06-01 12:04:42 -0300282}
283
Gustavo Padovan43dbdad2015-06-01 12:04:41 -0300284static const struct drm_plane_helper_funcs plane_helper_funcs = {
285 .atomic_check = exynos_plane_atomic_check,
286 .atomic_update = exynos_plane_atomic_update,
Gustavo Padovanb7448682015-06-01 12:04:42 -0300287 .atomic_disable = exynos_plane_atomic_disable,
Gustavo Padovan43dbdad2015-06-01 12:04:41 -0300288};
289
Gustavo Padovan6e2a3b62015-04-03 21:05:52 +0900290static void exynos_plane_attach_zpos_property(struct drm_plane *plane,
Marek Szyprowskie9dfe832018-05-23 12:15:50 +0200291 int zpos, bool immutable)
Joonyoung Shim00ae67c2012-06-27 14:27:06 +0900292{
Marek Szyprowskie47726a2016-05-11 17:16:06 +0200293 if (immutable)
Marek Szyprowskie9dfe832018-05-23 12:15:50 +0200294 drm_plane_create_zpos_immutable_property(plane, zpos);
Marek Szyprowskie47726a2016-05-11 17:16:06 +0200295 else
Marek Szyprowskie9dfe832018-05-23 12:15:50 +0200296 drm_plane_create_zpos_property(plane, zpos, 0, MAX_PLANE - 1);
Joonyoung Shim00ae67c2012-06-27 14:27:06 +0900297}
298
Gustavo Padovan7ee14cd2015-04-03 21:03:40 +0900299int exynos_plane_init(struct drm_device *dev,
Andrzej Hajda2c826072017-03-15 15:41:05 +0100300 struct exynos_drm_plane *exynos_plane, unsigned int index,
Marek Szyprowskifd2d2fc2015-11-30 14:53:25 +0100301 const struct exynos_drm_plane_config *config)
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900302{
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +0900303 int err;
Christoph Manszewski482582c2018-09-21 14:24:37 +0200304 unsigned int supported_modes = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
305 BIT(DRM_MODE_BLEND_PREMULTI) |
306 BIT(DRM_MODE_BLEND_COVERAGE);
307 struct drm_plane *plane = &exynos_plane->base;
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900308
Marek Szyprowskifd2d2fc2015-11-30 14:53:25 +0100309 err = drm_universal_plane_init(dev, &exynos_plane->base,
Andrzej Hajda2c826072017-03-15 15:41:05 +0100310 1 << dev->mode_config.num_crtc,
Marek Szyprowskifd2d2fc2015-11-30 14:53:25 +0100311 &exynos_plane_funcs,
312 config->pixel_formats,
313 config->num_pixel_formats,
Ben Widawskye6fc3b62017-07-23 20:46:38 -0700314 NULL, config->type, NULL);
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +0900315 if (err) {
Inki Dae6f83d202019-04-15 14:24:36 +0900316 DRM_DEV_ERROR(dev->dev, "failed to initialize plane\n");
Gustavo Padovan7ee14cd2015-04-03 21:03:40 +0900317 return err;
Joonyoung Shimb5d2eb32012-06-27 14:27:04 +0900318 }
319
Gustavo Padovan43dbdad2015-06-01 12:04:41 -0300320 drm_plane_helper_add(&exynos_plane->base, &plane_helper_funcs);
321
Marek Szyprowski40bdfb02015-12-16 13:21:42 +0100322 exynos_plane->index = index;
Marek Szyprowskifd2d2fc2015-11-30 14:53:25 +0100323 exynos_plane->config = config;
Gustavo Padovan6e2a3b62015-04-03 21:05:52 +0900324
Marek Szyprowskie9dfe832018-05-23 12:15:50 +0200325 exynos_plane_attach_zpos_property(&exynos_plane->base, config->zpos,
Marek Szyprowskie47726a2016-05-11 17:16:06 +0200326 !(config->capabilities & EXYNOS_DRM_PLANE_CAP_ZPOS));
Joonyoung Shim00ae67c2012-06-27 14:27:06 +0900327
Christoph Manszewski482582c2018-09-21 14:24:37 +0200328 if (config->capabilities & EXYNOS_DRM_PLANE_CAP_PIX_BLEND)
329 drm_plane_create_blend_mode_property(plane, supported_modes);
330
Christoph Manszewski6ac99a32018-09-21 14:24:38 +0200331 if (config->capabilities & EXYNOS_DRM_PLANE_CAP_WIN_BLEND)
332 drm_plane_create_alpha_property(plane);
333
Gustavo Padovan7ee14cd2015-04-03 21:03:40 +0900334 return 0;
Joonyoung Shim864ee9e2011-12-08 17:54:07 +0900335}