Joonyoung Shim | 864ee9e | 2011-12-08 17:54:07 +0900 | [diff] [blame] | 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 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 12 | #include <drm/drmP.h> |
Joonyoung Shim | 864ee9e | 2011-12-08 17:54:07 +0900 | [diff] [blame] | 13 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 14 | #include <drm/exynos_drm.h> |
Gustavo Padovan | adf5691 | 2014-11-27 14:56:09 -0200 | [diff] [blame] | 15 | #include <drm/drm_plane_helper.h> |
Gustavo Padovan | 4ea9526 | 2015-06-01 12:04:44 -0300 | [diff] [blame] | 16 | #include <drm/drm_atomic_helper.h> |
Joonyoung Shim | 864ee9e | 2011-12-08 17:54:07 +0900 | [diff] [blame] | 17 | #include "exynos_drm_drv.h" |
Sean Paul | 080be03d | 2014-02-19 21:02:55 +0900 | [diff] [blame] | 18 | #include "exynos_drm_crtc.h" |
Joonyoung Shim | 4070d21 | 2012-06-27 14:27:05 +0900 | [diff] [blame] | 19 | #include "exynos_drm_fb.h" |
| 20 | #include "exynos_drm_gem.h" |
Mark Brown | e30655d | 2013-08-13 00:46:40 +0100 | [diff] [blame] | 21 | #include "exynos_drm_plane.h" |
Joonyoung Shim | 864ee9e | 2011-12-08 17:54:07 +0900 | [diff] [blame] | 22 | |
Joonyoung Shim | 2ab9792 | 2012-09-27 19:25:21 +0900 | [diff] [blame] | 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 | * |
Joonyoung Shim | 60a705a | 2012-12-14 15:48:22 +0900 | [diff] [blame] | 31 | * There are six cases from a to f. |
Joonyoung Shim | 2ab9792 | 2012-09-27 19:25:21 +0900 | [diff] [blame] | 32 | * |
| 33 | * <----- SCREEN -----> |
| 34 | * 0 last |
| 35 | * ----------|------------------|---------- |
| 36 | * CRTCs |
| 37 | * a ------- |
| 38 | * b ------- |
| 39 | * c -------------------------- |
| 40 | * d -------- |
| 41 | * e ------- |
| 42 | * f ------- |
| 43 | */ |
| 44 | static 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 Szyprowski | 0114f40 | 2015-11-30 14:53:22 +0100 | [diff] [blame] | 59 | static void exynos_plane_mode_set(struct exynos_drm_plane_state *exynos_state) |
| 60 | |
Gustavo Padovan | adf5691 | 2014-11-27 14:56:09 -0200 | [diff] [blame] | 61 | { |
Marek Szyprowski | 0114f40 | 2015-11-30 14:53:22 +0100 | [diff] [blame] | 62 | struct drm_plane_state *state = &exynos_state->base; |
| 63 | struct drm_crtc *crtc = exynos_state->base.crtc; |
Joonyoung Shim | 020e79d | 2015-06-02 21:04:42 +0900 | [diff] [blame] | 64 | struct drm_display_mode *mode = &crtc->state->adjusted_mode; |
Marek Szyprowski | 0114f40 | 2015-11-30 14:53:22 +0100 | [diff] [blame] | 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; |
Gustavo Padovan | adf5691 | 2014-11-27 14:56:09 -0200 | [diff] [blame] | 69 | unsigned int actual_w; |
| 70 | unsigned int actual_h; |
| 71 | |
Marek Szyprowski | 0114f40 | 2015-11-30 14:53:22 +0100 | [diff] [blame] | 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 | |
Joonyoung Shim | 020e79d | 2015-06-02 21:04:42 +0900 | [diff] [blame] | 88 | actual_w = exynos_plane_get_size(crtc_x, crtc_w, mode->hdisplay); |
| 89 | actual_h = exynos_plane_get_size(crtc_y, crtc_h, mode->vdisplay); |
Joonyoung Shim | 2ab9792 | 2012-09-27 19:25:21 +0900 | [diff] [blame] | 90 | |
| 91 | if (crtc_x < 0) { |
| 92 | if (actual_w) |
| 93 | src_x -= crtc_x; |
Joonyoung Shim | 2ab9792 | 2012-09-27 19:25:21 +0900 | [diff] [blame] | 94 | crtc_x = 0; |
| 95 | } |
| 96 | |
| 97 | if (crtc_y < 0) { |
| 98 | if (actual_h) |
| 99 | src_y -= crtc_y; |
Joonyoung Shim | 2ab9792 | 2012-09-27 19:25:21 +0900 | [diff] [blame] | 100 | crtc_y = 0; |
| 101 | } |
Joonyoung Shim | 4070d21 | 2012-06-27 14:27:05 +0900 | [diff] [blame] | 102 | |
Joonyoung Shim | 3cabaf7 | 2015-04-07 15:59:39 +0900 | [diff] [blame] | 103 | /* set ratio */ |
Marek Szyprowski | 0114f40 | 2015-11-30 14:53:22 +0100 | [diff] [blame] | 104 | exynos_state->h_ratio = (src_w << 16) / crtc_w; |
| 105 | exynos_state->v_ratio = (src_h << 16) / crtc_h; |
Joonyoung Shim | 3cabaf7 | 2015-04-07 15:59:39 +0900 | [diff] [blame] | 106 | |
Joonyoung Shim | 4070d21 | 2012-06-27 14:27:05 +0900 | [diff] [blame] | 107 | /* set drm framebuffer data. */ |
Marek Szyprowski | 0114f40 | 2015-11-30 14:53:22 +0100 | [diff] [blame] | 108 | exynos_state->src.x = src_x; |
| 109 | exynos_state->src.y = src_y; |
| 110 | exynos_state->src.w = (actual_w * exynos_state->h_ratio) >> 16; |
| 111 | exynos_state->src.h = (actual_h * exynos_state->v_ratio) >> 16; |
Joonyoung Shim | 4070d21 | 2012-06-27 14:27:05 +0900 | [diff] [blame] | 112 | |
Gustavo Padovan | 8837dee | 2014-11-03 18:13:27 -0200 | [diff] [blame] | 113 | /* set plane range to be displayed. */ |
Marek Szyprowski | 0114f40 | 2015-11-30 14:53:22 +0100 | [diff] [blame] | 114 | exynos_state->crtc.x = crtc_x; |
| 115 | exynos_state->crtc.y = crtc_y; |
| 116 | exynos_state->crtc.w = actual_w; |
| 117 | exynos_state->crtc.h = actual_h; |
Gustavo Padovan | d88d246 | 2015-07-16 12:23:38 -0300 | [diff] [blame] | 118 | |
Gustavo Padovan | 8837dee | 2014-11-03 18:13:27 -0200 | [diff] [blame] | 119 | DRM_DEBUG_KMS("plane : offset_x/y(%d,%d), width/height(%d,%d)", |
Marek Szyprowski | 0114f40 | 2015-11-30 14:53:22 +0100 | [diff] [blame] | 120 | exynos_state->crtc.x, exynos_state->crtc.y, |
| 121 | exynos_state->crtc.w, exynos_state->crtc.h); |
| 122 | } |
Joonyoung Shim | 4070d21 | 2012-06-27 14:27:05 +0900 | [diff] [blame] | 123 | |
Marek Szyprowski | 0114f40 | 2015-11-30 14:53:22 +0100 | [diff] [blame] | 124 | static void exynos_drm_plane_reset(struct drm_plane *plane) |
| 125 | { |
| 126 | struct exynos_drm_plane_state *exynos_state; |
| 127 | |
| 128 | if (plane->state) { |
| 129 | exynos_state = to_exynos_plane_state(plane->state); |
| 130 | if (exynos_state->base.fb) |
| 131 | drm_framebuffer_unreference(exynos_state->base.fb); |
| 132 | kfree(exynos_state); |
| 133 | plane->state = NULL; |
| 134 | } |
| 135 | |
| 136 | exynos_state = kzalloc(sizeof(*exynos_state), GFP_KERNEL); |
| 137 | if (exynos_state) { |
| 138 | plane->state = &exynos_state->base; |
| 139 | plane->state->plane = plane; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | static struct drm_plane_state * |
| 144 | exynos_drm_plane_duplicate_state(struct drm_plane *plane) |
| 145 | { |
| 146 | struct exynos_drm_plane_state *exynos_state; |
| 147 | struct exynos_drm_plane_state *copy; |
| 148 | |
| 149 | exynos_state = to_exynos_plane_state(plane->state); |
| 150 | copy = kzalloc(sizeof(*exynos_state), GFP_KERNEL); |
| 151 | if (!copy) |
| 152 | return NULL; |
| 153 | |
| 154 | __drm_atomic_helper_plane_duplicate_state(plane, ©->base); |
| 155 | return ©->base; |
| 156 | } |
| 157 | |
| 158 | static void exynos_drm_plane_destroy_state(struct drm_plane *plane, |
| 159 | struct drm_plane_state *old_state) |
| 160 | { |
| 161 | struct exynos_drm_plane_state *old_exynos_state = |
| 162 | to_exynos_plane_state(old_state); |
| 163 | __drm_atomic_helper_plane_destroy_state(plane, old_state); |
| 164 | kfree(old_exynos_state); |
Joonyoung Shim | 4070d21 | 2012-06-27 14:27:05 +0900 | [diff] [blame] | 165 | } |
| 166 | |
Joonyoung Shim | 864ee9e | 2011-12-08 17:54:07 +0900 | [diff] [blame] | 167 | static struct drm_plane_funcs exynos_plane_funcs = { |
Gustavo Padovan | 910874a | 2015-06-01 12:04:46 -0300 | [diff] [blame] | 168 | .update_plane = drm_atomic_helper_update_plane, |
| 169 | .disable_plane = drm_atomic_helper_disable_plane, |
Gustavo Padovan | 97464d7 | 2015-04-01 13:02:10 -0300 | [diff] [blame] | 170 | .destroy = drm_plane_cleanup, |
Marek Szyprowski | 0114f40 | 2015-11-30 14:53:22 +0100 | [diff] [blame] | 171 | .reset = exynos_drm_plane_reset, |
| 172 | .atomic_duplicate_state = exynos_drm_plane_duplicate_state, |
| 173 | .atomic_destroy_state = exynos_drm_plane_destroy_state, |
Joonyoung Shim | 864ee9e | 2011-12-08 17:54:07 +0900 | [diff] [blame] | 174 | }; |
| 175 | |
Marek Szyprowski | 6178d3d | 2015-11-30 14:53:26 +0100 | [diff] [blame^] | 176 | static int |
| 177 | exynos_drm_plane_check_size(const struct exynos_drm_plane_config *config, |
| 178 | struct exynos_drm_plane_state *state) |
| 179 | { |
| 180 | bool width_ok = false, height_ok = false; |
| 181 | |
| 182 | if (config->capabilities & EXYNOS_DRM_PLANE_CAP_SCALE) |
| 183 | return 0; |
| 184 | |
| 185 | if (state->src.w == state->crtc.w) |
| 186 | width_ok = true; |
| 187 | |
| 188 | if (state->src.h == state->crtc.h) |
| 189 | height_ok = true; |
| 190 | |
| 191 | if ((config->capabilities & EXYNOS_DRM_PLANE_CAP_DOUBLE) && |
| 192 | state->h_ratio == (1 << 15)) |
| 193 | width_ok = true; |
| 194 | |
| 195 | if ((config->capabilities & EXYNOS_DRM_PLANE_CAP_DOUBLE) && |
| 196 | state->v_ratio == (1 << 15)) |
| 197 | height_ok = true; |
| 198 | |
| 199 | if (width_ok & height_ok) |
| 200 | return 0; |
| 201 | |
| 202 | DRM_DEBUG_KMS("scaling mode is not supported"); |
| 203 | return -ENOTSUPP; |
| 204 | } |
| 205 | |
Gustavo Padovan | 43dbdad | 2015-06-01 12:04:41 -0300 | [diff] [blame] | 206 | static int exynos_plane_atomic_check(struct drm_plane *plane, |
| 207 | struct drm_plane_state *state) |
| 208 | { |
Gustavo Padovan | d5f5223 | 2015-06-01 12:04:49 -0300 | [diff] [blame] | 209 | struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane); |
Marek Szyprowski | 0114f40 | 2015-11-30 14:53:22 +0100 | [diff] [blame] | 210 | struct exynos_drm_plane_state *exynos_state = |
| 211 | to_exynos_plane_state(state); |
| 212 | int ret = 0; |
Gustavo Padovan | d5f5223 | 2015-06-01 12:04:49 -0300 | [diff] [blame] | 213 | |
Marek Szyprowski | 0114f40 | 2015-11-30 14:53:22 +0100 | [diff] [blame] | 214 | if (!state->crtc || !state->fb) |
Gustavo Padovan | d5f5223 | 2015-06-01 12:04:49 -0300 | [diff] [blame] | 215 | return 0; |
| 216 | |
Marek Szyprowski | 0114f40 | 2015-11-30 14:53:22 +0100 | [diff] [blame] | 217 | /* translate state into exynos_state */ |
| 218 | exynos_plane_mode_set(exynos_state); |
| 219 | |
Marek Szyprowski | 6178d3d | 2015-11-30 14:53:26 +0100 | [diff] [blame^] | 220 | ret = exynos_drm_plane_check_size(exynos_plane->config, exynos_state); |
Marek Szyprowski | 0114f40 | 2015-11-30 14:53:22 +0100 | [diff] [blame] | 221 | return ret; |
Gustavo Padovan | 43dbdad | 2015-06-01 12:04:41 -0300 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | static void exynos_plane_atomic_update(struct drm_plane *plane, |
| 225 | struct drm_plane_state *old_state) |
| 226 | { |
| 227 | struct drm_plane_state *state = plane->state; |
Gustavo Padovan | d5f5223 | 2015-06-01 12:04:49 -0300 | [diff] [blame] | 228 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(state->crtc); |
| 229 | struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane); |
Gustavo Padovan | 43dbdad | 2015-06-01 12:04:41 -0300 | [diff] [blame] | 230 | |
| 231 | if (!state->crtc) |
| 232 | return; |
| 233 | |
Marek Szyprowski | 0114f40 | 2015-11-30 14:53:22 +0100 | [diff] [blame] | 234 | plane->crtc = state->crtc; |
Gustavo Padovan | 822f6df | 2015-08-15 13:26:14 -0300 | [diff] [blame] | 235 | exynos_plane->pending_fb = state->fb; |
| 236 | |
Gustavo Padovan | 9cc7610 | 2015-08-03 14:38:05 +0900 | [diff] [blame] | 237 | if (exynos_crtc->ops->update_plane) |
Gustavo Padovan | 1e1d139 | 2015-08-03 14:39:36 +0900 | [diff] [blame] | 238 | exynos_crtc->ops->update_plane(exynos_crtc, exynos_plane); |
Gustavo Padovan | 43dbdad | 2015-06-01 12:04:41 -0300 | [diff] [blame] | 239 | } |
| 240 | |
Gustavo Padovan | b744868 | 2015-06-01 12:04:42 -0300 | [diff] [blame] | 241 | static void exynos_plane_atomic_disable(struct drm_plane *plane, |
| 242 | struct drm_plane_state *old_state) |
| 243 | { |
| 244 | struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane); |
| 245 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(old_state->crtc); |
| 246 | |
| 247 | if (!old_state->crtc) |
| 248 | return; |
| 249 | |
Gustavo Padovan | 9cc7610 | 2015-08-03 14:38:05 +0900 | [diff] [blame] | 250 | if (exynos_crtc->ops->disable_plane) |
Marek Szyprowski | 0114f40 | 2015-11-30 14:53:22 +0100 | [diff] [blame] | 251 | exynos_crtc->ops->disable_plane(exynos_crtc, exynos_plane); |
Gustavo Padovan | b744868 | 2015-06-01 12:04:42 -0300 | [diff] [blame] | 252 | } |
| 253 | |
Gustavo Padovan | 43dbdad | 2015-06-01 12:04:41 -0300 | [diff] [blame] | 254 | static const struct drm_plane_helper_funcs plane_helper_funcs = { |
| 255 | .atomic_check = exynos_plane_atomic_check, |
| 256 | .atomic_update = exynos_plane_atomic_update, |
Gustavo Padovan | b744868 | 2015-06-01 12:04:42 -0300 | [diff] [blame] | 257 | .atomic_disable = exynos_plane_atomic_disable, |
Gustavo Padovan | 43dbdad | 2015-06-01 12:04:41 -0300 | [diff] [blame] | 258 | }; |
| 259 | |
Gustavo Padovan | 6e2a3b6 | 2015-04-03 21:05:52 +0900 | [diff] [blame] | 260 | static void exynos_plane_attach_zpos_property(struct drm_plane *plane, |
| 261 | unsigned int zpos) |
Joonyoung Shim | 00ae67c | 2012-06-27 14:27:06 +0900 | [diff] [blame] | 262 | { |
| 263 | struct drm_device *dev = plane->dev; |
| 264 | struct exynos_drm_private *dev_priv = dev->dev_private; |
| 265 | struct drm_property *prop; |
| 266 | |
Joonyoung Shim | 00ae67c | 2012-06-27 14:27:06 +0900 | [diff] [blame] | 267 | prop = dev_priv->plane_zpos_property; |
| 268 | if (!prop) { |
Gustavo Padovan | 9210488 | 2015-04-01 13:02:09 -0300 | [diff] [blame] | 269 | prop = drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, |
| 270 | "zpos", 0, MAX_PLANE - 1); |
Joonyoung Shim | 00ae67c | 2012-06-27 14:27:06 +0900 | [diff] [blame] | 271 | if (!prop) |
| 272 | return; |
| 273 | |
| 274 | dev_priv->plane_zpos_property = prop; |
| 275 | } |
| 276 | |
Gustavo Padovan | 6e2a3b6 | 2015-04-03 21:05:52 +0900 | [diff] [blame] | 277 | drm_object_attach_property(&plane->base, prop, zpos); |
Joonyoung Shim | 00ae67c | 2012-06-27 14:27:06 +0900 | [diff] [blame] | 278 | } |
| 279 | |
Gustavo Padovan | 7ee14cd | 2015-04-03 21:03:40 +0900 | [diff] [blame] | 280 | int exynos_plane_init(struct drm_device *dev, |
| 281 | struct exynos_drm_plane *exynos_plane, |
Marek Szyprowski | fd2d2fc | 2015-11-30 14:53:25 +0100 | [diff] [blame] | 282 | unsigned long possible_crtcs, |
| 283 | const struct exynos_drm_plane_config *config) |
Joonyoung Shim | 864ee9e | 2011-12-08 17:54:07 +0900 | [diff] [blame] | 284 | { |
Joonyoung Shim | b5d2eb3 | 2012-06-27 14:27:04 +0900 | [diff] [blame] | 285 | int err; |
Joonyoung Shim | 864ee9e | 2011-12-08 17:54:07 +0900 | [diff] [blame] | 286 | |
Marek Szyprowski | fd2d2fc | 2015-11-30 14:53:25 +0100 | [diff] [blame] | 287 | err = drm_universal_plane_init(dev, &exynos_plane->base, |
| 288 | possible_crtcs, |
| 289 | &exynos_plane_funcs, |
| 290 | config->pixel_formats, |
| 291 | config->num_pixel_formats, |
| 292 | config->type); |
| 293 | |
Joonyoung Shim | b5d2eb3 | 2012-06-27 14:27:04 +0900 | [diff] [blame] | 294 | if (err) { |
| 295 | DRM_ERROR("failed to initialize plane\n"); |
Gustavo Padovan | 7ee14cd | 2015-04-03 21:03:40 +0900 | [diff] [blame] | 296 | return err; |
Joonyoung Shim | b5d2eb3 | 2012-06-27 14:27:04 +0900 | [diff] [blame] | 297 | } |
| 298 | |
Gustavo Padovan | 43dbdad | 2015-06-01 12:04:41 -0300 | [diff] [blame] | 299 | drm_plane_helper_add(&exynos_plane->base, &plane_helper_funcs); |
| 300 | |
Marek Szyprowski | fd2d2fc | 2015-11-30 14:53:25 +0100 | [diff] [blame] | 301 | exynos_plane->zpos = config->zpos; |
| 302 | exynos_plane->config = config; |
Gustavo Padovan | 6e2a3b6 | 2015-04-03 21:05:52 +0900 | [diff] [blame] | 303 | |
Marek Szyprowski | fd2d2fc | 2015-11-30 14:53:25 +0100 | [diff] [blame] | 304 | if (config->type == DRM_PLANE_TYPE_OVERLAY) |
| 305 | exynos_plane_attach_zpos_property(&exynos_plane->base, |
| 306 | config->zpos); |
Joonyoung Shim | 00ae67c | 2012-06-27 14:27:06 +0900 | [diff] [blame] | 307 | |
Gustavo Padovan | 7ee14cd | 2015-04-03 21:03:40 +0900 | [diff] [blame] | 308 | return 0; |
Joonyoung Shim | 864ee9e | 2011-12-08 17:54:07 +0900 | [diff] [blame] | 309 | } |