Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 1 | /* exynos_drm_crtc.c |
| 2 | * |
| 3 | * Copyright (c) 2011 Samsung Electronics Co., Ltd. |
| 4 | * Authors: |
| 5 | * Inki Dae <inki.dae@samsung.com> |
| 6 | * Joonyoung Shim <jy0922.shim@samsung.com> |
| 7 | * Seung-Woo Kim <sw0312.kim@samsung.com> |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 10 | * copy of this software and associated documentation files (the "Software"), |
| 11 | * to deal in the Software without restriction, including without limitation |
| 12 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 13 | * and/or sell copies of the Software, and to permit persons to whom the |
| 14 | * Software is furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice (including the next |
| 17 | * paragraph) shall be included in all copies or substantial portions of the |
| 18 | * Software. |
| 19 | * |
| 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 23 | * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 24 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 25 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 26 | * OTHER DEALINGS IN THE SOFTWARE. |
| 27 | */ |
| 28 | |
| 29 | #include "drmP.h" |
| 30 | #include "drm_crtc_helper.h" |
| 31 | |
| 32 | #include "exynos_drm_drv.h" |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 33 | #include "exynos_drm_encoder.h" |
Joonyoung Shim | b5d2eb3 | 2012-06-27 14:27:04 +0900 | [diff] [blame] | 34 | #include "exynos_drm_plane.h" |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 35 | |
| 36 | #define to_exynos_crtc(x) container_of(x, struct exynos_drm_crtc,\ |
| 37 | drm_crtc) |
| 38 | |
| 39 | /* |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 40 | * Exynos specific crtc structure. |
| 41 | * |
| 42 | * @drm_crtc: crtc object. |
Joonyoung Shim | b5d2eb3 | 2012-06-27 14:27:04 +0900 | [diff] [blame] | 43 | * @drm_plane: pointer of private plane object for this crtc |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 44 | * @pipe: a crtc index created at load() with a new crtc object creation |
| 45 | * and the crtc object would be set to private->crtc array |
| 46 | * to get a crtc object corresponding to this pipe from private->crtc |
| 47 | * array when irq interrupt occured. the reason of using this pipe is that |
| 48 | * drm framework doesn't support multiple irq yet. |
| 49 | * we can refer to the crtc to current hardware interrupt occured through |
| 50 | * this pipe value. |
Inki Dae | ec05da9 | 2011-12-06 11:06:54 +0900 | [diff] [blame] | 51 | * @dpms: store the crtc dpms value |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 52 | */ |
| 53 | struct exynos_drm_crtc { |
| 54 | struct drm_crtc drm_crtc; |
Joonyoung Shim | b5d2eb3 | 2012-06-27 14:27:04 +0900 | [diff] [blame] | 55 | struct drm_plane *plane; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 56 | unsigned int pipe; |
Inki Dae | ec05da9 | 2011-12-06 11:06:54 +0900 | [diff] [blame] | 57 | unsigned int dpms; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 58 | }; |
| 59 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 60 | static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode) |
| 61 | { |
Inki Dae | ec05da9 | 2011-12-06 11:06:54 +0900 | [diff] [blame] | 62 | struct drm_device *dev = crtc->dev; |
Joonyoung Shim | d2716c8 | 2011-11-04 17:04:45 +0900 | [diff] [blame] | 63 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 64 | |
Joonyoung Shim | d2716c8 | 2011-11-04 17:04:45 +0900 | [diff] [blame] | 65 | DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode); |
| 66 | |
Inki Dae | ec05da9 | 2011-12-06 11:06:54 +0900 | [diff] [blame] | 67 | if (exynos_crtc->dpms == mode) { |
| 68 | DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n"); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | mutex_lock(&dev->struct_mutex); |
| 73 | |
Joonyoung Shim | d2716c8 | 2011-11-04 17:04:45 +0900 | [diff] [blame] | 74 | switch (mode) { |
| 75 | case DRM_MODE_DPMS_ON: |
Inki Dae | ec05da9 | 2011-12-06 11:06:54 +0900 | [diff] [blame] | 76 | exynos_drm_fn_encoder(crtc, &mode, |
| 77 | exynos_drm_encoder_crtc_dpms); |
| 78 | exynos_crtc->dpms = mode; |
Joonyoung Shim | d2716c8 | 2011-11-04 17:04:45 +0900 | [diff] [blame] | 79 | break; |
| 80 | case DRM_MODE_DPMS_STANDBY: |
| 81 | case DRM_MODE_DPMS_SUSPEND: |
| 82 | case DRM_MODE_DPMS_OFF: |
Inki Dae | ec05da9 | 2011-12-06 11:06:54 +0900 | [diff] [blame] | 83 | exynos_drm_fn_encoder(crtc, &mode, |
| 84 | exynos_drm_encoder_crtc_dpms); |
| 85 | exynos_crtc->dpms = mode; |
Joonyoung Shim | d2716c8 | 2011-11-04 17:04:45 +0900 | [diff] [blame] | 86 | break; |
| 87 | default: |
Inki Dae | ec05da9 | 2011-12-06 11:06:54 +0900 | [diff] [blame] | 88 | DRM_ERROR("unspecified mode %d\n", mode); |
Joonyoung Shim | d2716c8 | 2011-11-04 17:04:45 +0900 | [diff] [blame] | 89 | break; |
| 90 | } |
Inki Dae | ec05da9 | 2011-12-06 11:06:54 +0900 | [diff] [blame] | 91 | |
| 92 | mutex_unlock(&dev->struct_mutex); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static void exynos_drm_crtc_prepare(struct drm_crtc *crtc) |
| 96 | { |
| 97 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 98 | |
| 99 | /* drm framework doesn't check NULL. */ |
| 100 | } |
| 101 | |
| 102 | static void exynos_drm_crtc_commit(struct drm_crtc *crtc) |
| 103 | { |
Joonyoung Shim | d2716c8 | 2011-11-04 17:04:45 +0900 | [diff] [blame] | 104 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); |
| 105 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 106 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 107 | |
Joonyoung Shim | 4070d21 | 2012-06-27 14:27:05 +0900 | [diff] [blame] | 108 | exynos_plane_commit(exynos_crtc->plane); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | static bool |
| 112 | exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc, |
Laurent Pinchart | e811f5a | 2012-07-17 17:56:50 +0200 | [diff] [blame] | 113 | const struct drm_display_mode *mode, |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 114 | struct drm_display_mode *adjusted_mode) |
| 115 | { |
| 116 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 117 | |
| 118 | /* drm framework doesn't check NULL */ |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | static int |
| 123 | exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode, |
| 124 | struct drm_display_mode *adjusted_mode, int x, int y, |
| 125 | struct drm_framebuffer *old_fb) |
| 126 | { |
Joonyoung Shim | aeb2922 | 2012-06-27 14:27:01 +0900 | [diff] [blame] | 127 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); |
Joonyoung Shim | 4070d21 | 2012-06-27 14:27:05 +0900 | [diff] [blame] | 128 | struct drm_plane *plane = exynos_crtc->plane; |
| 129 | unsigned int crtc_w; |
| 130 | unsigned int crtc_h; |
Joonyoung Shim | d249ce0 | 2012-06-27 14:27:02 +0900 | [diff] [blame] | 131 | int pipe = exynos_crtc->pipe; |
Joonyoung Shim | aeb2922 | 2012-06-27 14:27:01 +0900 | [diff] [blame] | 132 | int ret; |
| 133 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 134 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 135 | |
Joonyoung Shim | bebab8f | 2012-06-27 14:27:07 +0900 | [diff] [blame^] | 136 | exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON); |
| 137 | |
Inki Dae | 1de425b | 2012-03-16 18:47:04 +0900 | [diff] [blame] | 138 | /* |
| 139 | * copy the mode data adjusted by mode_fixup() into crtc->mode |
| 140 | * so that hardware can be seet to proper mode. |
| 141 | */ |
| 142 | memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode)); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 143 | |
Joonyoung Shim | 4070d21 | 2012-06-27 14:27:05 +0900 | [diff] [blame] | 144 | crtc_w = crtc->fb->width - x; |
| 145 | crtc_h = crtc->fb->height - y; |
| 146 | |
| 147 | ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h, |
| 148 | x, y, crtc_w, crtc_h); |
Joonyoung Shim | aeb2922 | 2012-06-27 14:27:01 +0900 | [diff] [blame] | 149 | if (ret) |
| 150 | return ret; |
| 151 | |
Joonyoung Shim | 4070d21 | 2012-06-27 14:27:05 +0900 | [diff] [blame] | 152 | plane->crtc = crtc; |
| 153 | plane->fb = crtc->fb; |
| 154 | |
Joonyoung Shim | d249ce0 | 2012-06-27 14:27:02 +0900 | [diff] [blame] | 155 | exynos_drm_fn_encoder(crtc, &pipe, exynos_drm_encoder_crtc_pipe); |
Joonyoung Shim | aeb2922 | 2012-06-27 14:27:01 +0900 | [diff] [blame] | 156 | |
| 157 | return 0; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y, |
| 161 | struct drm_framebuffer *old_fb) |
| 162 | { |
Joonyoung Shim | 4070d21 | 2012-06-27 14:27:05 +0900 | [diff] [blame] | 163 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); |
| 164 | struct drm_plane *plane = exynos_crtc->plane; |
| 165 | unsigned int crtc_w; |
| 166 | unsigned int crtc_h; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 167 | int ret; |
| 168 | |
| 169 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 170 | |
Joonyoung Shim | 4070d21 | 2012-06-27 14:27:05 +0900 | [diff] [blame] | 171 | crtc_w = crtc->fb->width - x; |
| 172 | crtc_h = crtc->fb->height - y; |
| 173 | |
| 174 | ret = exynos_plane_mode_set(plane, crtc, crtc->fb, 0, 0, crtc_w, crtc_h, |
| 175 | x, y, crtc_w, crtc_h); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 176 | if (ret) |
| 177 | return ret; |
| 178 | |
Joonyoung Shim | bebab8f | 2012-06-27 14:27:07 +0900 | [diff] [blame^] | 179 | exynos_drm_crtc_commit(crtc); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 180 | |
Joonyoung Shim | 4070d21 | 2012-06-27 14:27:05 +0900 | [diff] [blame] | 181 | return 0; |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | static void exynos_drm_crtc_load_lut(struct drm_crtc *crtc) |
| 185 | { |
| 186 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 187 | /* drm framework doesn't check NULL */ |
| 188 | } |
| 189 | |
| 190 | static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = { |
| 191 | .dpms = exynos_drm_crtc_dpms, |
| 192 | .prepare = exynos_drm_crtc_prepare, |
| 193 | .commit = exynos_drm_crtc_commit, |
| 194 | .mode_fixup = exynos_drm_crtc_mode_fixup, |
| 195 | .mode_set = exynos_drm_crtc_mode_set, |
| 196 | .mode_set_base = exynos_drm_crtc_mode_set_base, |
| 197 | .load_lut = exynos_drm_crtc_load_lut, |
| 198 | }; |
| 199 | |
| 200 | static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc, |
| 201 | struct drm_framebuffer *fb, |
| 202 | struct drm_pending_vblank_event *event) |
| 203 | { |
| 204 | struct drm_device *dev = crtc->dev; |
| 205 | struct exynos_drm_private *dev_priv = dev->dev_private; |
| 206 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); |
| 207 | struct drm_framebuffer *old_fb = crtc->fb; |
| 208 | int ret = -EINVAL; |
| 209 | |
| 210 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 211 | |
| 212 | mutex_lock(&dev->struct_mutex); |
| 213 | |
Inki Dae | ccf4d88 | 2011-10-14 13:29:51 +0900 | [diff] [blame] | 214 | if (event) { |
| 215 | /* |
| 216 | * the pipe from user always is 0 so we can set pipe number |
| 217 | * of current owner to event. |
| 218 | */ |
| 219 | event->pipe = exynos_crtc->pipe; |
| 220 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 221 | ret = drm_vblank_get(dev, exynos_crtc->pipe); |
| 222 | if (ret) { |
| 223 | DRM_DEBUG("failed to acquire vblank counter\n"); |
Inki Dae | ccf4d88 | 2011-10-14 13:29:51 +0900 | [diff] [blame] | 224 | list_del(&event->base.link); |
| 225 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 226 | goto out; |
| 227 | } |
| 228 | |
Inki Dae | c5614ae | 2012-02-15 11:25:20 +0900 | [diff] [blame] | 229 | list_add_tail(&event->base.link, |
| 230 | &dev_priv->pageflip_event_list); |
| 231 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 232 | crtc->fb = fb; |
Joonyoung Shim | 4070d21 | 2012-06-27 14:27:05 +0900 | [diff] [blame] | 233 | ret = exynos_drm_crtc_mode_set_base(crtc, crtc->x, crtc->y, |
| 234 | NULL); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 235 | if (ret) { |
| 236 | crtc->fb = old_fb; |
| 237 | drm_vblank_put(dev, exynos_crtc->pipe); |
Inki Dae | ccf4d88 | 2011-10-14 13:29:51 +0900 | [diff] [blame] | 238 | list_del(&event->base.link); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 239 | |
| 240 | goto out; |
| 241 | } |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 242 | } |
| 243 | out: |
| 244 | mutex_unlock(&dev->struct_mutex); |
| 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | static void exynos_drm_crtc_destroy(struct drm_crtc *crtc) |
| 249 | { |
| 250 | struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); |
| 251 | struct exynos_drm_private *private = crtc->dev->dev_private; |
| 252 | |
| 253 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 254 | |
| 255 | private->crtc[exynos_crtc->pipe] = NULL; |
| 256 | |
| 257 | drm_crtc_cleanup(crtc); |
| 258 | kfree(exynos_crtc); |
| 259 | } |
| 260 | |
| 261 | static struct drm_crtc_funcs exynos_crtc_funcs = { |
| 262 | .set_config = drm_crtc_helper_set_config, |
| 263 | .page_flip = exynos_drm_crtc_page_flip, |
| 264 | .destroy = exynos_drm_crtc_destroy, |
| 265 | }; |
| 266 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 267 | int exynos_drm_crtc_create(struct drm_device *dev, unsigned int nr) |
| 268 | { |
| 269 | struct exynos_drm_crtc *exynos_crtc; |
| 270 | struct exynos_drm_private *private = dev->dev_private; |
| 271 | struct drm_crtc *crtc; |
| 272 | |
| 273 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 274 | |
| 275 | exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL); |
| 276 | if (!exynos_crtc) { |
| 277 | DRM_ERROR("failed to allocate exynos crtc\n"); |
| 278 | return -ENOMEM; |
| 279 | } |
| 280 | |
| 281 | exynos_crtc->pipe = nr; |
Inki Dae | ec05da9 | 2011-12-06 11:06:54 +0900 | [diff] [blame] | 282 | exynos_crtc->dpms = DRM_MODE_DPMS_OFF; |
Joonyoung Shim | b5d2eb3 | 2012-06-27 14:27:04 +0900 | [diff] [blame] | 283 | exynos_crtc->plane = exynos_plane_init(dev, 1 << nr, true); |
| 284 | if (!exynos_crtc->plane) { |
| 285 | kfree(exynos_crtc); |
| 286 | return -ENOMEM; |
| 287 | } |
| 288 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 289 | crtc = &exynos_crtc->drm_crtc; |
| 290 | |
| 291 | private->crtc[nr] = crtc; |
| 292 | |
| 293 | drm_crtc_init(dev, crtc, &exynos_crtc_funcs); |
| 294 | drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs); |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int crtc) |
| 300 | { |
| 301 | struct exynos_drm_private *private = dev->dev_private; |
Inki Dae | ec05da9 | 2011-12-06 11:06:54 +0900 | [diff] [blame] | 302 | struct exynos_drm_crtc *exynos_crtc = |
| 303 | to_exynos_crtc(private->crtc[crtc]); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 304 | |
| 305 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 306 | |
Inki Dae | ec05da9 | 2011-12-06 11:06:54 +0900 | [diff] [blame] | 307 | if (exynos_crtc->dpms != DRM_MODE_DPMS_ON) |
| 308 | return -EPERM; |
| 309 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 310 | exynos_drm_fn_encoder(private->crtc[crtc], &crtc, |
| 311 | exynos_drm_enable_vblank); |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int crtc) |
| 317 | { |
| 318 | struct exynos_drm_private *private = dev->dev_private; |
Inki Dae | ec05da9 | 2011-12-06 11:06:54 +0900 | [diff] [blame] | 319 | struct exynos_drm_crtc *exynos_crtc = |
| 320 | to_exynos_crtc(private->crtc[crtc]); |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 321 | |
| 322 | DRM_DEBUG_KMS("%s\n", __FILE__); |
| 323 | |
Inki Dae | ec05da9 | 2011-12-06 11:06:54 +0900 | [diff] [blame] | 324 | if (exynos_crtc->dpms != DRM_MODE_DPMS_ON) |
| 325 | return; |
| 326 | |
Inki Dae | 1c248b7 | 2011-10-04 19:19:01 +0900 | [diff] [blame] | 327 | exynos_drm_fn_encoder(private->crtc[crtc], &crtc, |
| 328 | exynos_drm_disable_vblank); |
| 329 | } |