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 * This program is free software; you can redistribute it and/or modify it 10 * under the terms of the GNU General Public License as published by the 11 * Free Software Foundation; either version 2 of the License, or (at your 12 * option) any later version. 13 */ 14 15 #include <drm/drmP.h> 16 #include <drm/drm_crtc_helper.h> 17 18 #include "exynos_drm_crtc.h" 19 #include "exynos_drm_drv.h" 20 #include "exynos_drm_encoder.h" 21 #include "exynos_drm_plane.h" 22 23 static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode) 24 { 25 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); 26 27 DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode); 28 29 if (exynos_crtc->dpms == mode) { 30 DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n"); 31 return; 32 } 33 34 if (mode > DRM_MODE_DPMS_ON) { 35 /* wait for the completion of page flip. */ 36 if (!wait_event_timeout(exynos_crtc->pending_flip_queue, 37 !atomic_read(&exynos_crtc->pending_flip), 38 HZ/20)) 39 atomic_set(&exynos_crtc->pending_flip, 0); 40 drm_crtc_vblank_off(crtc); 41 } 42 43 if (exynos_crtc->ops->dpms) 44 exynos_crtc->ops->dpms(exynos_crtc, mode); 45 46 exynos_crtc->dpms = mode; 47 48 if (mode == DRM_MODE_DPMS_ON) 49 drm_crtc_vblank_on(crtc); 50 } 51 52 static void exynos_drm_crtc_prepare(struct drm_crtc *crtc) 53 { 54 /* drm framework doesn't check NULL. */ 55 } 56 57 static void exynos_drm_crtc_commit(struct drm_crtc *crtc) 58 { 59 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); 60 struct exynos_drm_plane *exynos_plane = to_exynos_plane(crtc->primary); 61 62 exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON); 63 64 if (exynos_crtc->ops->win_commit) 65 exynos_crtc->ops->win_commit(exynos_crtc, exynos_plane->zpos); 66 67 if (exynos_crtc->ops->commit) 68 exynos_crtc->ops->commit(exynos_crtc); 69 } 70 71 static bool 72 exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc, 73 const struct drm_display_mode *mode, 74 struct drm_display_mode *adjusted_mode) 75 { 76 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); 77 78 if (exynos_crtc->ops->mode_fixup) 79 return exynos_crtc->ops->mode_fixup(exynos_crtc, mode, 80 adjusted_mode); 81 82 return true; 83 } 84 85 static int 86 exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode, 87 struct drm_display_mode *adjusted_mode, int x, int y, 88 struct drm_framebuffer *old_fb) 89 { 90 struct drm_framebuffer *fb = crtc->primary->fb; 91 unsigned int crtc_w; 92 unsigned int crtc_h; 93 int ret; 94 95 /* 96 * copy the mode data adjusted by mode_fixup() into crtc->mode 97 * so that hardware can be seet to proper mode. 98 */ 99 memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode)); 100 101 ret = exynos_check_plane(crtc->primary, fb); 102 if (ret < 0) 103 return ret; 104 105 crtc_w = fb->width - x; 106 crtc_h = fb->height - y; 107 exynos_plane_mode_set(crtc->primary, crtc, fb, 0, 0, 108 crtc_w, crtc_h, x, y, crtc_w, crtc_h); 109 110 return 0; 111 } 112 113 static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y, 114 struct drm_framebuffer *old_fb) 115 { 116 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); 117 struct drm_framebuffer *fb = crtc->primary->fb; 118 unsigned int crtc_w; 119 unsigned int crtc_h; 120 121 /* when framebuffer changing is requested, crtc's dpms should be on */ 122 if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) { 123 DRM_ERROR("failed framebuffer changing request.\n"); 124 return -EPERM; 125 } 126 127 crtc_w = fb->width - x; 128 crtc_h = fb->height - y; 129 130 return exynos_update_plane(crtc->primary, crtc, fb, 0, 0, 131 crtc_w, crtc_h, x, y, crtc_w, crtc_h); 132 } 133 134 static void exynos_drm_crtc_disable(struct drm_crtc *crtc) 135 { 136 struct drm_plane *plane; 137 int ret; 138 139 exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF); 140 141 drm_for_each_legacy_plane(plane, &crtc->dev->mode_config.plane_list) { 142 if (plane->crtc != crtc) 143 continue; 144 145 ret = plane->funcs->disable_plane(plane); 146 if (ret) 147 DRM_ERROR("Failed to disable plane %d\n", ret); 148 } 149 } 150 151 static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = { 152 .dpms = exynos_drm_crtc_dpms, 153 .prepare = exynos_drm_crtc_prepare, 154 .commit = exynos_drm_crtc_commit, 155 .mode_fixup = exynos_drm_crtc_mode_fixup, 156 .mode_set = exynos_drm_crtc_mode_set, 157 .mode_set_base = exynos_drm_crtc_mode_set_base, 158 .disable = exynos_drm_crtc_disable, 159 }; 160 161 static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc, 162 struct drm_framebuffer *fb, 163 struct drm_pending_vblank_event *event, 164 uint32_t page_flip_flags) 165 { 166 struct drm_device *dev = crtc->dev; 167 struct exynos_drm_private *dev_priv = dev->dev_private; 168 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); 169 struct drm_framebuffer *old_fb = crtc->primary->fb; 170 unsigned int crtc_w, crtc_h; 171 int ret = -EINVAL; 172 173 /* when the page flip is requested, crtc's dpms should be on */ 174 if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) { 175 DRM_ERROR("failed page flip request.\n"); 176 return -EINVAL; 177 } 178 179 mutex_lock(&dev->struct_mutex); 180 181 if (event) { 182 /* 183 * the pipe from user always is 0 so we can set pipe number 184 * of current owner to event. 185 */ 186 event->pipe = exynos_crtc->pipe; 187 188 ret = drm_vblank_get(dev, exynos_crtc->pipe); 189 if (ret) { 190 DRM_DEBUG("failed to acquire vblank counter\n"); 191 192 goto out; 193 } 194 195 spin_lock_irq(&dev->event_lock); 196 list_add_tail(&event->base.link, 197 &dev_priv->pageflip_event_list); 198 atomic_set(&exynos_crtc->pending_flip, 1); 199 spin_unlock_irq(&dev->event_lock); 200 201 crtc->primary->fb = fb; 202 crtc_w = fb->width - crtc->x; 203 crtc_h = fb->height - crtc->y; 204 ret = exynos_update_plane(crtc->primary, crtc, fb, 0, 0, 205 crtc_w, crtc_h, crtc->x, crtc->y, 206 crtc_w, crtc_h); 207 if (ret) { 208 crtc->primary->fb = old_fb; 209 210 spin_lock_irq(&dev->event_lock); 211 drm_vblank_put(dev, exynos_crtc->pipe); 212 list_del(&event->base.link); 213 atomic_set(&exynos_crtc->pending_flip, 0); 214 spin_unlock_irq(&dev->event_lock); 215 216 goto out; 217 } 218 } 219 out: 220 mutex_unlock(&dev->struct_mutex); 221 return ret; 222 } 223 224 static void exynos_drm_crtc_destroy(struct drm_crtc *crtc) 225 { 226 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); 227 struct exynos_drm_private *private = crtc->dev->dev_private; 228 229 private->crtc[exynos_crtc->pipe] = NULL; 230 231 drm_crtc_cleanup(crtc); 232 kfree(exynos_crtc); 233 } 234 235 static struct drm_crtc_funcs exynos_crtc_funcs = { 236 .set_config = drm_crtc_helper_set_config, 237 .page_flip = exynos_drm_crtc_page_flip, 238 .destroy = exynos_drm_crtc_destroy, 239 }; 240 241 struct exynos_drm_crtc *exynos_drm_crtc_create(struct drm_device *drm_dev, 242 int pipe, 243 enum exynos_drm_output_type type, 244 struct exynos_drm_crtc_ops *ops, 245 void *ctx) 246 { 247 struct exynos_drm_crtc *exynos_crtc; 248 struct drm_plane *plane; 249 struct exynos_drm_private *private = drm_dev->dev_private; 250 struct drm_crtc *crtc; 251 int ret; 252 253 exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL); 254 if (!exynos_crtc) 255 return ERR_PTR(-ENOMEM); 256 257 init_waitqueue_head(&exynos_crtc->pending_flip_queue); 258 atomic_set(&exynos_crtc->pending_flip, 0); 259 260 exynos_crtc->dpms = DRM_MODE_DPMS_OFF; 261 exynos_crtc->pipe = pipe; 262 exynos_crtc->type = type; 263 exynos_crtc->ops = ops; 264 exynos_crtc->ctx = ctx; 265 plane = exynos_plane_init(drm_dev, 1 << pipe, 266 DRM_PLANE_TYPE_PRIMARY); 267 if (IS_ERR(plane)) { 268 ret = PTR_ERR(plane); 269 goto err_plane; 270 } 271 272 crtc = &exynos_crtc->base; 273 274 private->crtc[pipe] = crtc; 275 276 ret = drm_crtc_init_with_planes(drm_dev, crtc, plane, NULL, 277 &exynos_crtc_funcs); 278 if (ret < 0) 279 goto err_crtc; 280 281 drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs); 282 283 return exynos_crtc; 284 285 err_crtc: 286 plane->funcs->destroy(plane); 287 err_plane: 288 kfree(exynos_crtc); 289 return ERR_PTR(ret); 290 } 291 292 int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int pipe) 293 { 294 struct exynos_drm_private *private = dev->dev_private; 295 struct exynos_drm_crtc *exynos_crtc = 296 to_exynos_crtc(private->crtc[pipe]); 297 298 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON) 299 return -EPERM; 300 301 if (exynos_crtc->ops->enable_vblank) 302 exynos_crtc->ops->enable_vblank(exynos_crtc); 303 304 return 0; 305 } 306 307 void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int pipe) 308 { 309 struct exynos_drm_private *private = dev->dev_private; 310 struct exynos_drm_crtc *exynos_crtc = 311 to_exynos_crtc(private->crtc[pipe]); 312 313 if (exynos_crtc->dpms != DRM_MODE_DPMS_ON) 314 return; 315 316 if (exynos_crtc->ops->disable_vblank) 317 exynos_crtc->ops->disable_vblank(exynos_crtc); 318 } 319 320 void exynos_drm_crtc_finish_pageflip(struct drm_device *dev, int pipe) 321 { 322 struct exynos_drm_private *dev_priv = dev->dev_private; 323 struct drm_pending_vblank_event *e, *t; 324 struct drm_crtc *drm_crtc = dev_priv->crtc[pipe]; 325 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(drm_crtc); 326 unsigned long flags; 327 328 spin_lock_irqsave(&dev->event_lock, flags); 329 330 list_for_each_entry_safe(e, t, &dev_priv->pageflip_event_list, 331 base.link) { 332 /* if event's pipe isn't same as crtc then ignore it. */ 333 if (pipe != e->pipe) 334 continue; 335 336 list_del(&e->base.link); 337 drm_send_vblank_event(dev, -1, e); 338 drm_vblank_put(dev, pipe); 339 atomic_set(&exynos_crtc->pending_flip, 0); 340 wake_up(&exynos_crtc->pending_flip_queue); 341 } 342 343 spin_unlock_irqrestore(&dev->event_lock, flags); 344 } 345 346 void exynos_drm_crtc_complete_scanout(struct drm_framebuffer *fb) 347 { 348 struct exynos_drm_crtc *exynos_crtc; 349 struct drm_device *dev = fb->dev; 350 struct drm_crtc *crtc; 351 352 /* 353 * make sure that overlay data are updated to real hardware 354 * for all encoders. 355 */ 356 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 357 exynos_crtc = to_exynos_crtc(crtc); 358 359 /* 360 * wait for vblank interrupt 361 * - this makes sure that overlay data are updated to 362 * real hardware. 363 */ 364 if (exynos_crtc->ops->wait_for_vblank) 365 exynos_crtc->ops->wait_for_vblank(exynos_crtc); 366 } 367 } 368 369 int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev, 370 unsigned int out_type) 371 { 372 struct drm_crtc *crtc; 373 374 list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) { 375 struct exynos_drm_crtc *exynos_crtc; 376 377 exynos_crtc = to_exynos_crtc(crtc); 378 if (exynos_crtc->type == out_type) 379 return exynos_crtc->pipe; 380 } 381 382 return -EPERM; 383 } 384 385 void exynos_drm_crtc_te_handler(struct drm_crtc *crtc) 386 { 387 struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc); 388 389 if (exynos_crtc->ops->te_handler) 390 exynos_crtc->ops->te_handler(exynos_crtc); 391 } 392