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 | |
Gustavo Padovan | d5f5223 | 2015-06-01 12:04:49 -0300 | [diff] [blame] | 59 | static void exynos_plane_mode_set(struct drm_plane *plane, |
| 60 | struct drm_crtc *crtc, |
| 61 | struct drm_framebuffer *fb, |
| 62 | int crtc_x, int crtc_y, |
| 63 | unsigned int crtc_w, unsigned int crtc_h, |
| 64 | uint32_t src_x, uint32_t src_y, |
| 65 | uint32_t src_w, uint32_t src_h) |
Gustavo Padovan | adf5691 | 2014-11-27 14:56:09 -0200 | [diff] [blame] | 66 | { |
| 67 | struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane); |
Joonyoung Shim | 020e79d | 2015-06-02 21:04:42 +0900 | [diff] [blame] | 68 | struct drm_display_mode *mode = &crtc->state->adjusted_mode; |
Gustavo Padovan | adf5691 | 2014-11-27 14:56:09 -0200 | [diff] [blame] | 69 | unsigned int actual_w; |
| 70 | unsigned int actual_h; |
| 71 | |
Joonyoung Shim | 020e79d | 2015-06-02 21:04:42 +0900 | [diff] [blame] | 72 | actual_w = exynos_plane_get_size(crtc_x, crtc_w, mode->hdisplay); |
| 73 | actual_h = exynos_plane_get_size(crtc_y, crtc_h, mode->vdisplay); |
Joonyoung Shim | 2ab9792 | 2012-09-27 19:25:21 +0900 | [diff] [blame] | 74 | |
| 75 | if (crtc_x < 0) { |
| 76 | if (actual_w) |
| 77 | src_x -= crtc_x; |
Joonyoung Shim | 2ab9792 | 2012-09-27 19:25:21 +0900 | [diff] [blame] | 78 | crtc_x = 0; |
| 79 | } |
| 80 | |
| 81 | if (crtc_y < 0) { |
| 82 | if (actual_h) |
| 83 | src_y -= crtc_y; |
Joonyoung Shim | 2ab9792 | 2012-09-27 19:25:21 +0900 | [diff] [blame] | 84 | crtc_y = 0; |
| 85 | } |
Joonyoung Shim | 4070d21 | 2012-06-27 14:27:05 +0900 | [diff] [blame] | 86 | |
Joonyoung Shim | 3cabaf7 | 2015-04-07 15:59:39 +0900 | [diff] [blame] | 87 | /* set ratio */ |
| 88 | exynos_plane->h_ratio = (src_w << 16) / crtc_w; |
| 89 | exynos_plane->v_ratio = (src_h << 16) / crtc_h; |
| 90 | |
Joonyoung Shim | 4070d21 | 2012-06-27 14:27:05 +0900 | [diff] [blame] | 91 | /* set drm framebuffer data. */ |
Joonyoung Shim | cb8a3db | 2015-04-07 15:59:38 +0900 | [diff] [blame] | 92 | exynos_plane->src_x = src_x; |
| 93 | exynos_plane->src_y = src_y; |
Gustavo Padovan | d88d246 | 2015-07-16 12:23:38 -0300 | [diff] [blame] | 94 | exynos_plane->src_w = (actual_w * exynos_plane->h_ratio) >> 16; |
| 95 | exynos_plane->src_h = (actual_h * exynos_plane->v_ratio) >> 16; |
Joonyoung Shim | 4070d21 | 2012-06-27 14:27:05 +0900 | [diff] [blame] | 96 | |
Gustavo Padovan | 8837dee | 2014-11-03 18:13:27 -0200 | [diff] [blame] | 97 | /* set plane range to be displayed. */ |
| 98 | exynos_plane->crtc_x = crtc_x; |
| 99 | exynos_plane->crtc_y = crtc_y; |
Gustavo Padovan | d88d246 | 2015-07-16 12:23:38 -0300 | [diff] [blame] | 100 | exynos_plane->crtc_w = actual_w; |
| 101 | exynos_plane->crtc_h = actual_h; |
| 102 | |
Gustavo Padovan | 8837dee | 2014-11-03 18:13:27 -0200 | [diff] [blame] | 103 | DRM_DEBUG_KMS("plane : offset_x/y(%d,%d), width/height(%d,%d)", |
| 104 | exynos_plane->crtc_x, exynos_plane->crtc_y, |
Gustavo Padovan | d88d246 | 2015-07-16 12:23:38 -0300 | [diff] [blame] | 105 | exynos_plane->crtc_w, exynos_plane->crtc_h); |
Joonyoung Shim | 4070d21 | 2012-06-27 14:27:05 +0900 | [diff] [blame] | 106 | |
Andrzej Hajda | 72ed6cc | 2014-09-19 14:58:53 +0200 | [diff] [blame] | 107 | plane->crtc = crtc; |
Joonyoung Shim | 4070d21 | 2012-06-27 14:27:05 +0900 | [diff] [blame] | 108 | } |
| 109 | |
Joonyoung Shim | 864ee9e | 2011-12-08 17:54:07 +0900 | [diff] [blame] | 110 | static struct drm_plane_funcs exynos_plane_funcs = { |
Gustavo Padovan | 910874a | 2015-06-01 12:04:46 -0300 | [diff] [blame] | 111 | .update_plane = drm_atomic_helper_update_plane, |
| 112 | .disable_plane = drm_atomic_helper_disable_plane, |
Gustavo Padovan | 97464d7 | 2015-04-01 13:02:10 -0300 | [diff] [blame] | 113 | .destroy = drm_plane_cleanup, |
Gustavo Padovan | 4ea9526 | 2015-06-01 12:04:44 -0300 | [diff] [blame] | 114 | .reset = drm_atomic_helper_plane_reset, |
| 115 | .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state, |
| 116 | .atomic_destroy_state = drm_atomic_helper_plane_destroy_state, |
Joonyoung Shim | 864ee9e | 2011-12-08 17:54:07 +0900 | [diff] [blame] | 117 | }; |
| 118 | |
Gustavo Padovan | 43dbdad | 2015-06-01 12:04:41 -0300 | [diff] [blame] | 119 | static int exynos_plane_atomic_check(struct drm_plane *plane, |
| 120 | struct drm_plane_state *state) |
| 121 | { |
Gustavo Padovan | d5f5223 | 2015-06-01 12:04:49 -0300 | [diff] [blame] | 122 | struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane); |
| 123 | int nr; |
| 124 | int i; |
| 125 | |
| 126 | if (!state->fb) |
| 127 | return 0; |
| 128 | |
Joonyoung Shim | faec262 | 2015-09-01 16:22:54 +0900 | [diff] [blame] | 129 | nr = drm_format_num_planes(state->fb->pixel_format); |
Gustavo Padovan | d5f5223 | 2015-06-01 12:04:49 -0300 | [diff] [blame] | 130 | for (i = 0; i < nr; i++) { |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 131 | struct exynos_drm_gem_obj *obj = |
| 132 | exynos_drm_fb_gem_obj(state->fb, i); |
Gustavo Padovan | d5f5223 | 2015-06-01 12:04:49 -0300 | [diff] [blame] | 133 | |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 134 | if (!obj) { |
| 135 | DRM_DEBUG_KMS("gem object is null\n"); |
Gustavo Padovan | d5f5223 | 2015-06-01 12:04:49 -0300 | [diff] [blame] | 136 | return -EFAULT; |
| 137 | } |
| 138 | |
Joonyoung Shim | 2a8cb48 | 2015-08-16 14:38:49 +0900 | [diff] [blame] | 139 | exynos_plane->dma_addr[i] = obj->dma_addr + |
Gustavo Padovan | d5f5223 | 2015-06-01 12:04:49 -0300 | [diff] [blame] | 140 | state->fb->offsets[i]; |
| 141 | |
| 142 | DRM_DEBUG_KMS("buffer: %d, dma_addr = 0x%lx\n", |
| 143 | i, (unsigned long)exynos_plane->dma_addr[i]); |
| 144 | } |
| 145 | |
| 146 | return 0; |
Gustavo Padovan | 43dbdad | 2015-06-01 12:04:41 -0300 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static void exynos_plane_atomic_update(struct drm_plane *plane, |
| 150 | struct drm_plane_state *old_state) |
| 151 | { |
| 152 | struct drm_plane_state *state = plane->state; |
Gustavo Padovan | d5f5223 | 2015-06-01 12:04:49 -0300 | [diff] [blame] | 153 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(state->crtc); |
| 154 | struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane); |
Gustavo Padovan | 43dbdad | 2015-06-01 12:04:41 -0300 | [diff] [blame] | 155 | |
| 156 | if (!state->crtc) |
| 157 | return; |
| 158 | |
Gustavo Padovan | d5f5223 | 2015-06-01 12:04:49 -0300 | [diff] [blame] | 159 | exynos_plane_mode_set(plane, state->crtc, state->fb, |
| 160 | state->crtc_x, state->crtc_y, |
| 161 | state->crtc_w, state->crtc_h, |
| 162 | state->src_x >> 16, state->src_y >> 16, |
| 163 | state->src_w >> 16, state->src_h >> 16); |
| 164 | |
Gustavo Padovan | 822f6df | 2015-08-15 13:26:14 -0300 | [diff] [blame] | 165 | exynos_plane->pending_fb = state->fb; |
| 166 | |
Gustavo Padovan | 9cc7610 | 2015-08-03 14:38:05 +0900 | [diff] [blame] | 167 | if (exynos_crtc->ops->update_plane) |
Gustavo Padovan | 1e1d139 | 2015-08-03 14:39:36 +0900 | [diff] [blame] | 168 | exynos_crtc->ops->update_plane(exynos_crtc, exynos_plane); |
Gustavo Padovan | 43dbdad | 2015-06-01 12:04:41 -0300 | [diff] [blame] | 169 | } |
| 170 | |
Gustavo Padovan | b744868 | 2015-06-01 12:04:42 -0300 | [diff] [blame] | 171 | static void exynos_plane_atomic_disable(struct drm_plane *plane, |
| 172 | struct drm_plane_state *old_state) |
| 173 | { |
| 174 | struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane); |
| 175 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(old_state->crtc); |
| 176 | |
| 177 | if (!old_state->crtc) |
| 178 | return; |
| 179 | |
Gustavo Padovan | 9cc7610 | 2015-08-03 14:38:05 +0900 | [diff] [blame] | 180 | if (exynos_crtc->ops->disable_plane) |
| 181 | exynos_crtc->ops->disable_plane(exynos_crtc, |
Gustavo Padovan | 1e1d139 | 2015-08-03 14:39:36 +0900 | [diff] [blame] | 182 | exynos_plane); |
Gustavo Padovan | b744868 | 2015-06-01 12:04:42 -0300 | [diff] [blame] | 183 | } |
| 184 | |
Gustavo Padovan | 43dbdad | 2015-06-01 12:04:41 -0300 | [diff] [blame] | 185 | static const struct drm_plane_helper_funcs plane_helper_funcs = { |
| 186 | .atomic_check = exynos_plane_atomic_check, |
| 187 | .atomic_update = exynos_plane_atomic_update, |
Gustavo Padovan | b744868 | 2015-06-01 12:04:42 -0300 | [diff] [blame] | 188 | .atomic_disable = exynos_plane_atomic_disable, |
Gustavo Padovan | 43dbdad | 2015-06-01 12:04:41 -0300 | [diff] [blame] | 189 | }; |
| 190 | |
Gustavo Padovan | 6e2a3b6 | 2015-04-03 21:05:52 +0900 | [diff] [blame] | 191 | static void exynos_plane_attach_zpos_property(struct drm_plane *plane, |
| 192 | unsigned int zpos) |
Joonyoung Shim | 00ae67c | 2012-06-27 14:27:06 +0900 | [diff] [blame] | 193 | { |
| 194 | struct drm_device *dev = plane->dev; |
| 195 | struct exynos_drm_private *dev_priv = dev->dev_private; |
| 196 | struct drm_property *prop; |
| 197 | |
Joonyoung Shim | 00ae67c | 2012-06-27 14:27:06 +0900 | [diff] [blame] | 198 | prop = dev_priv->plane_zpos_property; |
| 199 | if (!prop) { |
Gustavo Padovan | 9210488 | 2015-04-01 13:02:09 -0300 | [diff] [blame] | 200 | prop = drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, |
| 201 | "zpos", 0, MAX_PLANE - 1); |
Joonyoung Shim | 00ae67c | 2012-06-27 14:27:06 +0900 | [diff] [blame] | 202 | if (!prop) |
| 203 | return; |
| 204 | |
| 205 | dev_priv->plane_zpos_property = prop; |
| 206 | } |
| 207 | |
Gustavo Padovan | 6e2a3b6 | 2015-04-03 21:05:52 +0900 | [diff] [blame] | 208 | drm_object_attach_property(&plane->base, prop, zpos); |
Joonyoung Shim | 00ae67c | 2012-06-27 14:27:06 +0900 | [diff] [blame] | 209 | } |
| 210 | |
Gustavo Padovan | 323db0e | 2015-09-04 19:05:57 -0300 | [diff] [blame^] | 211 | enum drm_plane_type exynos_plane_get_type(unsigned int zpos, |
| 212 | unsigned int cursor_win) |
| 213 | { |
| 214 | if (zpos == DEFAULT_WIN) |
| 215 | return DRM_PLANE_TYPE_PRIMARY; |
| 216 | else if (zpos == cursor_win) |
| 217 | return DRM_PLANE_TYPE_CURSOR; |
| 218 | else |
| 219 | return DRM_PLANE_TYPE_OVERLAY; |
| 220 | } |
| 221 | |
Gustavo Padovan | 7ee14cd | 2015-04-03 21:03:40 +0900 | [diff] [blame] | 222 | int exynos_plane_init(struct drm_device *dev, |
| 223 | struct exynos_drm_plane *exynos_plane, |
Gustavo Padovan | 6e2a3b6 | 2015-04-03 21:05:52 +0900 | [diff] [blame] | 224 | unsigned long possible_crtcs, enum drm_plane_type type, |
Marek Szyprowski | fbbb1e1 | 2015-08-31 00:53:57 +0900 | [diff] [blame] | 225 | const uint32_t *formats, unsigned int fcount, |
Gustavo Padovan | 6e2a3b6 | 2015-04-03 21:05:52 +0900 | [diff] [blame] | 226 | unsigned int zpos) |
Joonyoung Shim | 864ee9e | 2011-12-08 17:54:07 +0900 | [diff] [blame] | 227 | { |
Joonyoung Shim | b5d2eb3 | 2012-06-27 14:27:04 +0900 | [diff] [blame] | 228 | int err; |
Joonyoung Shim | 864ee9e | 2011-12-08 17:54:07 +0900 | [diff] [blame] | 229 | |
Andrzej Hajda | 72ed6cc | 2014-09-19 14:58:53 +0200 | [diff] [blame] | 230 | err = drm_universal_plane_init(dev, &exynos_plane->base, possible_crtcs, |
Marek Szyprowski | fbbb1e1 | 2015-08-31 00:53:57 +0900 | [diff] [blame] | 231 | &exynos_plane_funcs, formats, fcount, |
| 232 | type); |
Joonyoung Shim | b5d2eb3 | 2012-06-27 14:27:04 +0900 | [diff] [blame] | 233 | if (err) { |
| 234 | DRM_ERROR("failed to initialize plane\n"); |
Gustavo Padovan | 7ee14cd | 2015-04-03 21:03:40 +0900 | [diff] [blame] | 235 | return err; |
Joonyoung Shim | b5d2eb3 | 2012-06-27 14:27:04 +0900 | [diff] [blame] | 236 | } |
| 237 | |
Gustavo Padovan | 43dbdad | 2015-06-01 12:04:41 -0300 | [diff] [blame] | 238 | drm_plane_helper_add(&exynos_plane->base, &plane_helper_funcs); |
| 239 | |
Gustavo Padovan | 6e2a3b6 | 2015-04-03 21:05:52 +0900 | [diff] [blame] | 240 | exynos_plane->zpos = zpos; |
| 241 | |
| 242 | if (type == DRM_PLANE_TYPE_OVERLAY) |
| 243 | exynos_plane_attach_zpos_property(&exynos_plane->base, zpos); |
Joonyoung Shim | 00ae67c | 2012-06-27 14:27:06 +0900 | [diff] [blame] | 244 | |
Gustavo Padovan | 7ee14cd | 2015-04-03 21:03:40 +0900 | [diff] [blame] | 245 | return 0; |
Joonyoung Shim | 864ee9e | 2011-12-08 17:54:07 +0900 | [diff] [blame] | 246 | } |